[RFC PATCH 1/3] NTB: ntb_transport: Run a pending link cleanup before the next link setup
From: Oguzhan KAYNAK
Date: Sun Dec 06 2026 - 02:11:03 EST
ntb_transport_event_callback() queues link_cleanup when the link is down
and link_work when it is up. The two are separate work items, so after a
link-down event followed by a link-up event they can run in either
order:
- link_cleanup runs first and finds nt->link_is_up false, so it cancels
the link_work the link-up event queued. Nothing queues it again.
- link_work runs first and sets the link up, then link_cleanup tears it
down again.
Either way the transport link stays down until the next link event. It
happens when ntb_transport probes shortly before the peer enables its
link: the event at probe time sees the link down and queues a cleanup,
and the peer's link-up arrives before that cleanup has run.
Set a link_down_pending flag in the event callback before queueing the
cleanup, and have link_work do nothing while it is set. The cleanup
clears the flag under link_event_lock before its teardown, and queues
link_work at the end if the link is up. A cleanup that finds the flag
already clear was queued for an event an earlier cleanup has handled,
and leaves the link alone. link_work also does nothing if the link is
already up, so the requeue cannot set up a live link a second time.
ntb_transport_free() disables both works before it runs the cleanup
itself, so nothing is queued during removal.
Tested on two KVM guests joined by an emulated NTB device (qemu-ntb),
with a test-only 200 ms delay of the cleanup work to force the order.
In 100 rounds the cleanup ran after the link-up event in all 100, and
with this patch and the next two the link came up in all of them;
without them it came up in none of the 10 such rounds out of 30.
Written with an AI coding assistant (Claude); not tested on NTB hardware.
Fixes: 7b4f2d3c3b82 ("NTB: No sleeping in interrupt context")
Signed-off-by: Oguzhan KAYNAK <kaynak.oguzhan0@xxxxxxxxx>
---
drivers/ntb/ntb_transport.c | 39 ++++++++++++++++++++++++++++++++-----
1 file changed, 34 insertions(+), 5 deletions(-)
diff --git a/drivers/ntb/ntb_transport.c b/drivers/ntb/ntb_transport.c
index 3cf2d778b..31c214d14 100644
--- a/drivers/ntb/ntb_transport.c
+++ b/drivers/ntb/ntb_transport.c
@@ -253,6 +253,8 @@ struct ntb_transport_ctx {
u64 msi_db_mask;
atomic_t link_is_up;
+ /* Set on a link-down event, cleared when its cleanup runs */
+ atomic_t link_down_pending;
struct delayed_work link_work;
struct work_struct link_cleanup;
@@ -1016,7 +1018,8 @@ static void ntb_qp_link_down(struct ntb_transport_qp *qp)
schedule_work(&qp->link_cleanup);
}
-static void ntb_transport_link_cleanup(struct ntb_transport_ctx *nt)
+static void ntb_transport_link_cleanup(struct ntb_transport_ctx *nt,
+ bool for_event)
{
struct ntb_transport_qp *qp;
u64 qp_bitmap_alloc;
@@ -1027,6 +1030,14 @@ static void ntb_transport_link_cleanup(struct ntb_transport_ctx *nt)
guard(mutex)(&nt->link_event_lock);
+ /*
+ * Clear the flag before the teardown: a link-down event that arrives
+ * while it runs sets it again and queues another cleanup. If it is
+ * already clear, an earlier cleanup has handled this event and the
+ * link may be up again: leave it.
+ */
+ if (for_event && !atomic_xchg(&nt->link_down_pending, 0))
+ return;
atomic_set(&nt->link_is_up, false);
qp_bitmap_alloc = nt->qp_bitmap & ~nt->qp_bitmap_free;
@@ -1057,17 +1068,27 @@ static void ntb_transport_link_cleanup_work(struct work_struct *work)
struct ntb_transport_ctx *nt =
container_of(work, struct ntb_transport_ctx, link_cleanup);
- ntb_transport_link_cleanup(nt);
+ ntb_transport_link_cleanup(nt, true);
+
+ /*
+ * ntb_transport_link_work() does nothing while a cleanup is pending,
+ * and the cleanup may have cancelled it: set the link up now if the
+ * link came back.
+ */
+ if (ntb_link_is_up(nt->ndev, NULL, NULL) == 1)
+ schedule_delayed_work(&nt->link_work, 0);
}
static void ntb_transport_event_callback(void *data)
{
struct ntb_transport_ctx *nt = data;
- if (ntb_link_is_up(nt->ndev, NULL, NULL) == 1)
+ if (ntb_link_is_up(nt->ndev, NULL, NULL) == 1) {
schedule_delayed_work(&nt->link_work, 0);
- else
+ } else {
+ atomic_set(&nt->link_down_pending, 1);
schedule_work(&nt->link_cleanup);
+ }
}
static void ntb_transport_link_work(struct work_struct *work)
@@ -1082,6 +1103,14 @@ static void ntb_transport_link_work(struct work_struct *work)
guard(mutex)(&nt->link_event_lock);
+ /*
+ * The cleanup of an earlier link-down must run first: it frees the MWs
+ * and clears the scratchpads this would set up. It queues this work
+ * again when it is done. A link that is already up needs no setup.
+ */
+ if (atomic_read(&nt->link_down_pending) || atomic_read(&nt->link_is_up))
+ return;
+
/* send the local info, in the opposite order of the way we read it */
if (nt->use_msi) {
@@ -1500,7 +1529,7 @@ static void ntb_transport_free(struct ntb_client *self, struct ntb_dev *ndev)
ntb_transport_free_queue(qp);
}
- ntb_transport_link_cleanup(nt);
+ ntb_transport_link_cleanup(nt, false);
ntb_link_disable(ndev);
ntb_clear_ctx(ndev);
--
2.34.1