[RFC PATCH 3/3] NTB: ntb_transport: Keep scratchpads in a cleanup before first link-up

From: Oguzhan KAYNAK

Date: Sun Dec 06 2026 - 02:11:24 EST


ntb_transport_link_cleanup() clears our scratchpads, which hold what the
peer writes for the link setup. Right after probe, the cleanup for the
link-down seen at probe time can run while the peer is already writing
them for the new link. If it clears them in the middle of the peer's
writes and our link_work then writes ours, the peer reads ours, finishes
its setup and does not write again; our link_work keeps reading an
incomplete set and the link stays down.

In a cleanup work before the transport link has been up for the first
time, keep the scratchpads, except QP_LINKS: a peer that stayed loaded
may have set a bit in it for an earlier link, and it sets the bit again
only after reading the information we write once the cleanup is done.
After the first link-up a cleanup clears everything as before;
ntb_transport_free() always does.

This assumes the scratchpads hold nothing from an earlier link at probe
time. That is normally true after our ntb_transport_free() or a device
reset, but not after a crash or kexec; there the probe-time cleanup used
to clear them.

Seen with a test-only delay of the cleanup work on two KVM guests joined
by an emulated NTB device (qemu-ntb): traces showed the peer finishing
its setup while our MW sizes stayed zero.

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 | 13 ++++++++++++-
1 file changed, 12 insertions(+), 1 deletion(-)

diff --git a/drivers/ntb/ntb_transport.c b/drivers/ntb/ntb_transport.c
index 478f2f6a3..5734a7a84 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;
+ /* The link has been up since probe (under link_event_lock) */
+ bool link_was_up;
/* Set on a link-down event, cleared when its cleanup runs */
atomic_t link_down_pending;
struct delayed_work link_work;
@@ -1056,8 +1058,16 @@ static void ntb_transport_link_cleanup(struct ntb_transport_ctx *nt,

/* The scratchpad registers keep the values if the remote side
* goes down, blast them now to give them a sane value the next
- * time they are accessed
+ * time they are accessed. Before the first link-up, keep the link
+ * information the peer may already be writing for this link, but
+ * clear QP_LINKS: a peer that stayed loaded may have set a bit in
+ * it for an earlier link, and it sets it again only after reading
+ * the information we write once this cleanup is done.
*/
+ if (for_event && !nt->link_was_up) {
+ ntb_spad_write(nt->ndev, QP_LINKS, 0);
+ return;
+ }
count = ntb_spad_count(nt->ndev);
for (i = 0; i < count; i++)
ntb_spad_write(nt->ndev, i, 0);
@@ -1193,6 +1203,7 @@ static void ntb_transport_link_work(struct work_struct *work)
}

/* Publish the link only after every QP has been set up. */
+ nt->link_was_up = true;
atomic_set_release(&nt->link_is_up, true);

/*
--
2.34.1