Linux PCI Non-Transparent Bridge framework and drivers
 help / color / mirror / Atom feed
* [RFC PATCH 0/3] NTB: ntb_transport: Lost link-up after a late link cleanup
@ 2026-12-06  7:09 Oguzhan KAYNAK
  2026-12-06  7:09 ` [RFC PATCH 1/3] NTB: ntb_transport: Run a pending link cleanup before the next link setup Oguzhan KAYNAK
                   ` (2 more replies)
  0 siblings, 3 replies; 7+ messages in thread
From: Oguzhan KAYNAK @ 2026-12-06  7:09 UTC (permalink / raw)
  To: Jon Mason, Dave Jiang, Allen Hubbe
  Cc: Koichiro Den, Logan Gunthorpe, Frank Li, ntb, linux-kernel,
	Oguzhan KAYNAK

ntb_transport can stay link-down after both sides have loaded it. The
event at probe time sees the link down and queues link_cleanup; the
peer's link-up queues link_work; if the cleanup runs after that, it
either cancels link_work, or tears the new link down, or clears the
scratchpads while the peer writes them. In each case nothing sets the
link up again until the next link event.

1/3 orders the two works: link_work does nothing while a link-down's
cleanup is pending, and the cleanup queues link_work when it is done.
2/3 retries link setup on a zero peer MW size instead of giving up.
3/3 keeps the scratchpads (except QP_LINKS) in a cleanup before the
first link-up, so it does not clear what the peer is already writing
for this link.

What this does not cover, and why it is an RFC: after the link has been
up, a cleanup still clears all scratchpads and can do so while the peer
rewrites them on a real bounce. The peer keeps retrying until it reads
ours, so with 1/3 and 2/3 it normally writes them again, but a clear
between its last write and its read would leave us with an incomplete
set; and a clear between the high and low halves of an MW size gives a
wrong non-zero size for windows of 4 GiB or more, which 2/3 does not
catch. I did not see either in testing. Also, 3/3 assumes the
scratchpads hold nothing from an earlier link at probe time, normally
true after our ntb_transport_free() or a device reset but not after a
crash or kexec. A clear at probe while the link is still down would
cover that, unless the peer can write them then. Closing all of this
seems to need a change to the handshake (a generation number, or
clearing on the link-down event itself); I would like to hear which
direction you prefer.

This applies on top of Koichiro Den's "[PATCH v4 00/15] NTB:
ntb_transport: Miscellaneous fixes" [1], which reworks the same
functions; the race is older than that series. Not for stable as is.

Tested on two KVM guests joined by an emulated NTB device (qemu-ntb
[2]), not on NTB hardware: guest A loads ntb_transport, guest B 0-30
ms later; the link must come up within 10 s. A test-only change [3]
delays the cleanup work by 200 ms to force the order; kprobe traces
(ktrace=1 in [3]) tell the rounds in which the cleanup ran after the
link-up event on A:

                     rounds   cleanup after   link came up in
                              link-up         those rounds
  v4 series              30   10              0
  + this series         100   100             100

With the v4 series the 10 window rounds were the only failures; with
this series all 100 rounds came up. Without the delay, with four
busy-loop processes on A while it loads, the link did not come up in 4
of 560 rounds with the v4 series and in 3 of 115 rounds without it
(short runs: without the v4 series the guests stop at the removal-time
bugs it fixes). With this series 200 such rounds all came up; such runs
hit the window only once or twice in 200 rounds, so the forced runs are
the real test. With this series the unbind-under-load test (use_dma=1)
and the load/unload test (use_dma=1 and 0) in [3] pass, 100 rounds
each.

AI (Claude) was used to write and run the tests and to write the
reports; I ran the tests and confirmed the results.

[1] https://lore.kernel.org/r/20261127020533.2481725-1-den@valinux.co.jp/
[2] https://github.com/oguzhankynk/qemu-ntb
[3] https://github.com/oguzhankynk/qemu-ntb/blob/ntb-transport-teardown/docs/transport-teardown.md

Oguzhan KAYNAK (3):
  NTB: ntb_transport: Run a pending link cleanup before the next link
    setup
  NTB: ntb_transport: Retry link setup on a zero peer MW size
  NTB: ntb_transport: Keep scratchpads in a cleanup before first link-up

 drivers/ntb/ntb_transport.c | 60 +++++++++++++++++++++++++++++++++----
 1 file changed, 54 insertions(+), 6 deletions(-)


base-commit: 0aa2e6bc39f17e1199345aaf21278e3ea25e6036
prerequisite-patch-id: b954941a7a66ae77636ddc401305db31689138c9
prerequisite-patch-id: e6ed5be0fbead6a3c00bbc42153eabcaf42d11fb
prerequisite-patch-id: 3f510b83fbf9240c64ea6115de25ac8ef8642529
prerequisite-patch-id: 8a4e919a25055c8f3bbed01def07b5bfbbcc25fb
prerequisite-patch-id: 4145d6956e9f03f3c425f093b68f6b617290302b
prerequisite-patch-id: a83d11a915d2edd5ee5ffb82f7656cda47161545
prerequisite-patch-id: 06ff83ea4246b902816f01268849e6f5edc7cf64
prerequisite-patch-id: 1add4889fcfc25e4e111c0103ca947ad301e2e4c
prerequisite-patch-id: cdfac2a4b47b92c9b5c454dbd047577284190c94
prerequisite-patch-id: 1bafe7eb5b5db9a3fe355162d6ac30d3cc77507e
prerequisite-patch-id: 050f5390a0004e80e56187512162195e657dda36
prerequisite-patch-id: 369b25058434db698967792ac21df54d0f6a4dd3
prerequisite-patch-id: 91c68f035371cd778436877a73afba71e07ae881
prerequisite-patch-id: 8e043b113b4d5538a30c4df4babc1d70ba227474
prerequisite-patch-id: bd22304fa63b345a852032f369fa98bc19e7b2ab
-- 
2.34.1


^ permalink raw reply	[flat|nested] 7+ messages in thread

* [RFC PATCH 1/3] NTB: ntb_transport: Run a pending link cleanup before the next link setup
  2026-12-06  7:09 [RFC PATCH 0/3] NTB: ntb_transport: Lost link-up after a late link cleanup Oguzhan KAYNAK
@ 2026-12-06  7:09 ` Oguzhan KAYNAK
  2026-12-06  7:33   ` sashiko-bot
  2026-12-06  7:09 ` [RFC PATCH 2/3] NTB: ntb_transport: Retry link setup on a zero peer MW size Oguzhan KAYNAK
  2026-12-06  7:09 ` [RFC PATCH 3/3] NTB: ntb_transport: Keep scratchpads in a cleanup before first link-up Oguzhan KAYNAK
  2 siblings, 1 reply; 7+ messages in thread
From: Oguzhan KAYNAK @ 2026-12-06  7:09 UTC (permalink / raw)
  To: Jon Mason, Dave Jiang, Allen Hubbe
  Cc: Koichiro Den, Logan Gunthorpe, Frank Li, ntb, linux-kernel,
	Oguzhan KAYNAK

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@gmail.com>
---
 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


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [RFC PATCH 2/3] NTB: ntb_transport: Retry link setup on a zero peer MW size
  2026-12-06  7:09 [RFC PATCH 0/3] NTB: ntb_transport: Lost link-up after a late link cleanup Oguzhan KAYNAK
  2026-12-06  7:09 ` [RFC PATCH 1/3] NTB: ntb_transport: Run a pending link cleanup before the next link setup Oguzhan KAYNAK
@ 2026-12-06  7:09 ` Oguzhan KAYNAK
  2026-12-06  7:32   ` sashiko-bot
  2026-12-06  7:09 ` [RFC PATCH 3/3] NTB: ntb_transport: Keep scratchpads in a cleanup before first link-up Oguzhan KAYNAK
  2 siblings, 1 reply; 7+ messages in thread
From: Oguzhan KAYNAK @ 2026-12-06  7:09 UTC (permalink / raw)
  To: Jon Mason, Dave Jiang, Allen Hubbe
  Cc: Koichiro Den, Logan Gunthorpe, Frank Li, ntb, linux-kernel,
	Oguzhan KAYNAK

ntb_transport_link_work() reads the peer's memory window sizes from the
scratchpads and passes them to ntb_set_mw(), which returns -EINVAL for a
zero size. link_work then frees the MWs and stops retrying, as it does
for an allocation failure, so the link never comes up.

The peer writes VERSION last, but a link cleanup on our side clears all
our scratchpads, and when that lands while the peer is writing them,
VERSION can be set while a size is still zero. That is not a failure:
retry the link setup, the peer writes the scratchpads again on its next
try.

Seen with a test-only delay of the cleanup work on two KVM guests joined
by an emulated NTB device (qemu-ntb), with only the previous patch
applied: a trace showed VERSION read as set and the MW0 size as zero,
after which link_work gave up. The next patch avoids the clear before
the first link-up, but a cleanup after it still clears everything.
A peer that reports a zero size for good now keeps link_work retrying
instead of stopping it.

Written with an AI coding assistant (Claude); not tested on NTB hardware.

Fixes: 84f766855f61 ("ntb: stop link work when we do not have memory")
Signed-off-by: Oguzhan KAYNAK <kaynak.oguzhan0@gmail.com>
---
 drivers/ntb/ntb_transport.c | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/drivers/ntb/ntb_transport.c b/drivers/ntb/ntb_transport.c
index 31c214d14..478f2f6a3 100644
--- a/drivers/ntb/ntb_transport.c
+++ b/drivers/ntb/ntb_transport.c
@@ -1172,6 +1172,14 @@ static void ntb_transport_link_work(struct work_struct *work)
 
 		dev_dbg(&pdev->dev, "Remote MW%d size = %#llx\n", i, val64);
 
+		/*
+		 * A zero size is not a failure: a link cleanup on our side
+		 * cleared the scratchpads while the peer was writing them.
+		 * Retry; the peer writes them again on its next try.
+		 */
+		if (!val64)
+			goto out;
+
 		rc = ntb_set_mw(nt, i, val64);
 		if (rc)
 			goto out1;
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [RFC PATCH 3/3] NTB: ntb_transport: Keep scratchpads in a cleanup before first link-up
  2026-12-06  7:09 [RFC PATCH 0/3] NTB: ntb_transport: Lost link-up after a late link cleanup Oguzhan KAYNAK
  2026-12-06  7:09 ` [RFC PATCH 1/3] NTB: ntb_transport: Run a pending link cleanup before the next link setup Oguzhan KAYNAK
  2026-12-06  7:09 ` [RFC PATCH 2/3] NTB: ntb_transport: Retry link setup on a zero peer MW size Oguzhan KAYNAK
@ 2026-12-06  7:09 ` Oguzhan KAYNAK
  2026-12-06  7:35   ` sashiko-bot
  2 siblings, 1 reply; 7+ messages in thread
From: Oguzhan KAYNAK @ 2026-12-06  7:09 UTC (permalink / raw)
  To: Jon Mason, Dave Jiang, Allen Hubbe
  Cc: Koichiro Den, Logan Gunthorpe, Frank Li, ntb, linux-kernel,
	Oguzhan KAYNAK

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@gmail.com>
---
 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


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* Re: [RFC PATCH 2/3] NTB: ntb_transport: Retry link setup on a zero peer MW size
  2026-12-06  7:09 ` [RFC PATCH 2/3] NTB: ntb_transport: Retry link setup on a zero peer MW size Oguzhan KAYNAK
@ 2026-12-06  7:32   ` sashiko-bot
  0 siblings, 0 replies; 7+ messages in thread
From: sashiko-bot @ 2026-12-06  7:32 UTC (permalink / raw)
  To: Oguzhan KAYNAK; +Cc: ntb

Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [Medium] ntb: link setup livelock in ntb_transport_link_work() on zero
  peer MW size
- [Low] ntb: ntb_transport_link_work() leaves VERSION set on zero MW
  size retry
--

commit a8fba74b998be95cc0493ee4cdfa68d6dd40c92d
Author: Oguzhan KAYNAK <kaynak.oguzhan0@gmail.com>

NTB: ntb_transport: Retry link setup on a zero peer MW size

This commit makes ntb_transport_link_work() retry link setup when a peer's
memory window size scratchpad reads as zero instead of passing zero to
ntb_set_mw() and aborting.

> diff --git a/drivers/ntb/ntb_transport.c b/drivers/ntb/ntb_transport.c
> index 31c214d147c4c..478f2f6a399f5 100644
> --- a/drivers/ntb/ntb_transport.c
> +++ b/drivers/ntb/ntb_transport.c
> @@ -1172,6 +1172,14 @@ static void ntb_transport_link_work(struct work_struct *work)
>  
>  		dev_dbg(&pdev->dev, "Remote MW%d size = %#llx\n", i, val64);
>  
> +		/*
> +		 * A zero size is not a failure: a link cleanup on our side
> +		 * cleared the scratchpads while the peer was writing them.
> +		 * Retry; the peer writes them again on its next try.
> +		 */
> +		if (!val64)
> +			goto out;
> +
>  		rc = ntb_set_mw(nt, i, val64);
>  		if (rc)
>  			goto out1;

[Severity: Medium]
Can this retry livelock forever if the peer has already brought its side of
the link up?

ntb_transport_link_work() writes all local scratchpads including VERSION to
the peer before reading its own.  During a link bounce, a local cleanup can
zero MW0_SZ_HIGH/LOW after the peer writes them, while local link_work still
publishes valid scratchpads to the peer:

Host A                              Host B
ntb_transport_link_cleanup()        ntb_transport_link_work()
  clear VERSION..NUM_MWS            write A's MW0_SZ_HIGH/LOW
  clear MW0_SZ_HIGH/LOW
  schedule link_work
ntb_transport_link_work()
  write B's MW0_SZ..VERSION         write A's NUM_MWS, NUM_QPS, VERSION
                                    read B's scratchpads (valid)
                                    link_is_up = true; return
  read A's VERSION..NUM_MWS
  read A's MW0_SZ == 0
  goto out  <-- retries every 10 ms forever

Once Host B sets link_is_up to true, it returns early from
ntb_transport_link_work() and will never rewrite Host A's MW size
scratchpads.  Host A then retries every 10 ms rewriting peer scratchpads
over MMIO while the transport link never comes up.

[Severity: Low]
Does jumping to out without clearing the local VERSION scratchpad allow a
subsequent retry to read a torn 64-bit MW size?

VERSION acts as a publication barrier for the two 32-bit writes to
MW0_SZ_HIGH and MW0_SZ_LOW.  With VERSION left set, a later retry landing
between those two writes on a peer with a > 4 GiB MW whose lower 32 bits are
non-zero reads a non-zero truncated size ((u64)upper << 32).

Passing that torn size to ntb_set_mw() either fails and aborts link_work, or
if the allocation succeeds, causes ntb_transport_setup_qp_mw() to cast
mw->xlat_size to unsigned int (yielding 0) and underflow rx_size.  Clearing
local VERSION before jumping to out avoids reading a half-written size.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261206070959.8519-1-kaynak.oguzhan0@gmail.com?part=2

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [RFC PATCH 1/3] NTB: ntb_transport: Run a pending link cleanup before the next link setup
  2026-12-06  7:09 ` [RFC PATCH 1/3] NTB: ntb_transport: Run a pending link cleanup before the next link setup Oguzhan KAYNAK
@ 2026-12-06  7:33   ` sashiko-bot
  0 siblings, 0 replies; 7+ messages in thread
From: sashiko-bot @ 2026-12-06  7:33 UTC (permalink / raw)
  To: Oguzhan KAYNAK; +Cc: ntb

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] ntb_transport: unsynchronized link_up check causes spurious
  link teardown
--

commit ff41587a6452a6a2bd40eda8955de637cc27dd31
Author: Oguzhan KAYNAK <kaynak.oguzhan0@gmail.com>

NTB: ntb_transport: Run a pending link cleanup before the next link setup

This commit adds a link_down_pending flag so that ntb_transport_link_work()
defers link setup until a pending ntb_transport_link_cleanup() completes, and
has ntb_transport_link_cleanup_work() reschedule link_work if the hardware
link is up.

> diff --git a/drivers/ntb/ntb_transport.c b/drivers/ntb/ntb_transport.c
> index 3cf2d778bf925..31c214d147c4c 100644
> --- a/drivers/ntb/ntb_transport.c
> +++ b/drivers/ntb/ntb_transport.c

[ ... ]

> @@ -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);
> +	}
>  }

[Severity: Medium]
Can a delayed ntb_transport_event_callback() set link_down_pending after the
link has already come up, tearing down a live link?

ntb_link_event() only holds ntb->ctx_lock around
ntb_transport_event_callback(), which the link work items never take.  If
CPU0 is delayed between reading ntb_link_is_up() == 0 and setting
link_down_pending while an earlier cleanup finishes:

CPU0                              CPU1
ntb_transport_event_callback()    ntb_transport_link_cleanup_work()
  ntb_link_is_up() == 0             ntb_transport_link_cleanup(nt, true)
  /* delayed */                       atomic_xchg(&link_down_pending, 0)
                                      ...
                                  /* hardware link comes up */
                                    ntb_link_is_up() == 1
                                    schedule_delayed_work(&link_work, 0)
                                  ntb_transport_link_work()
                                    link_down_pending == 0, link_is_up == 0
                                    /* completes handshake with peer */
                                    atomic_set_release(&link_is_up, true)
  atomic_set(&link_down_pending, 1)
  schedule_work(&link_cleanup)    ntb_transport_link_cleanup_work()
                                    ntb_transport_link_cleanup(nt, true)
                                      /* tears down QPs/MWs, zeroes spads */
                                        <-- spurious link teardown

The second cleanup zeroes the local scratchpads and reschedules link_work,
which then reads VERSION == 0 and retries indefinitely while the peer still
considers the link up and does not rewrite the scratchpads.  The transport
link stays wedged until another hardware link-down occurs.

Serializing the ntb_link_is_up() check and link_down_pending update against
ntb_transport_link_cleanup_work() and ntb_transport_link_work() would close
this race.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261206070959.8519-1-kaynak.oguzhan0@gmail.com?part=1

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [RFC PATCH 3/3] NTB: ntb_transport: Keep scratchpads in a cleanup before first link-up
  2026-12-06  7:09 ` [RFC PATCH 3/3] NTB: ntb_transport: Keep scratchpads in a cleanup before first link-up Oguzhan KAYNAK
@ 2026-12-06  7:35   ` sashiko-bot
  0 siblings, 0 replies; 7+ messages in thread
From: sashiko-bot @ 2026-12-06  7:35 UTC (permalink / raw)
  To: Oguzhan KAYNAK; +Cc: ntb

Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [High] NTB: ntb_transport_link_cleanup() skips spad clear after
  aborted link setup
- [High] NTB: ntb_transport_probe() leaves stale scratchpads before
  ntb_link_enable()
- [Low] NTB: Fixes tag cites 7b4f2d3c3b82 instead of e26a5843f7f5 for
  probe link event
--

commit 0fa9c18ea14b2dcf84bc7348aae49eb950b85bed
Author: Oguzhan KAYNAK <kaynak.oguzhan0@gmail.com>

> NTB: ntb_transport: Keep scratchpads in a cleanup before first link-up
>
> 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@gmail.com>

[Severity: Low]
Should the Fixes tag point to e26a5843f7f5 ("NTB: Split ntb_hw_intel and
ntb_transport drivers") instead?  In 7b4f2d3c3b82 ("NTB: No sleeping in
interrupt context"), probe only scheduled link_work when the link was up;
the probe-time ntb_link_event() that queues link_cleanup came later.

> diff --git a/drivers/ntb/ntb_transport.c b/drivers/ntb/ntb_transport.c
> index 478f2f6a399f5..5734a7a841b31 100644
> --- a/drivers/ntb/ntb_transport.c
> +++ b/drivers/ntb/ntb_transport.c

[ ... ]

> @@ -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);

[Severity: High]
Can skipping the probe-time scratchpad clear leave stale values from
before probe in place?

ntb_transport_probe() enables the link without zeroing local scratchpads.
Stale values survive without a crash or kexec: ntb_perf fills local
scratchpads with 0xffffffff and only clears PERF_SPAD_CMD on disable, and
ntb_transport_free() clears them before calling ntb_link_disable().

When local use_msi is false while the peer has MSI enabled,
ntb_transport_setup_qp_msi() returns early without clearing the MSI slots.
The peer's ntb_transport_setup_qp_peer_msi() then reads 0xffffffff as
addr_offset and ntb_msi_peer_trigger() writes out of bounds past peer_mws.

Zeroing local scratchpads in ntb_transport_probe() before
ntb_link_enable() and only scheduling link_work if ntb_link_is_up() is
true would avoid both the stale scratchpads and the probe-time race.

> @@ -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);
>  
>  	/*

[Severity: High]
Because nt->link_was_up is only set after full setup completes, can a
failed or aborted initial link-up leave peer-written scratchpads uncleaned
across a real link-down?

If the peer writes MW0_SZ_*, NUM_MWS, NUM_QPS, and VERSION and then
ntb_transport_link_work() exits early (for example when ntb_set_mw() fails
with -ENOMEM or the link drops mid-handshake), the subsequent link-down
cleanup still sees !nt->link_was_up and skips clearing those scratchpads.

On the next link-up after the peer reconfigures (such as reducing
max_mw_size), VERSION still matches NTB_TRANSPORT_VERSION.
ntb_transport_link_work() can read stale or torn MW sizes, repeating the
-ENOMEM bailout or configuring the inbound MW and RX ring for the old size.

Similarly, if ntb_msi_setup_mws() fails on the retry and clears
nt->use_msi, ntb_transport_setup_qp_msi() returns early and leaves the
earlier local MSI scratchpads set, so the peer enables MSI against an
unregistered window.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261206070959.8519-1-kaynak.oguzhan0@gmail.com?part=3

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2026-12-06  7:35 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-06  7:09 [RFC PATCH 0/3] NTB: ntb_transport: Lost link-up after a late link cleanup Oguzhan KAYNAK
2026-12-06  7:09 ` [RFC PATCH 1/3] NTB: ntb_transport: Run a pending link cleanup before the next link setup Oguzhan KAYNAK
2026-12-06  7:33   ` sashiko-bot
2026-12-06  7:09 ` [RFC PATCH 2/3] NTB: ntb_transport: Retry link setup on a zero peer MW size Oguzhan KAYNAK
2026-12-06  7:32   ` sashiko-bot
2026-12-06  7:09 ` [RFC PATCH 3/3] NTB: ntb_transport: Keep scratchpads in a cleanup before first link-up Oguzhan KAYNAK
2026-12-06  7:35   ` sashiko-bot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox