Linux s390 Architecture development
 help / color / mirror / Atom feed
* [PATCH net v4] net/smc: bound the DMB-nocopy send-buffer credit by what was sent
@ 2026-11-26 15:06 Piyus Kedia
  2026-11-27 15:06 ` sashiko-bot
  0 siblings, 1 reply; 2+ messages in thread
From: Piyus Kedia @ 2026-11-26 15:06 UTC (permalink / raw)
  To: wenjia, jaka, alibuda, tonylu, guwen
  Cc: netdev, linux-s390, linux-kernel, davem, edumazet, kuba, pabeni,
	horms, kapil, stable

On the SMC-D DMB-nocopy receive path, smc_cdc_msg_recv_action() credits
sndbuf_space from the consumer cursor the peer reported and then assigns
that cursor to tx_curs_fin:

	diff_tx = smc_curs_diff(conn->sndbuf_desc->len, &conn->tx_curs_fin,
				&conn->local_rx_ctrl.cons);
	atomic_add(diff_tx, &conn->sndbuf_space);
	/* guarantee 0 <= sndbuf_space <= sndbuf_desc->len */
	smc_curs_copy(&conn->tx_curs_fin, &conn->local_rx_ctrl.cons, conn);

The peer writes that cursor into the DMB itself, so under DMB-nocopy the
value is remote input and nothing bounds it.  A cursor reporting more
consumed than this side ever sent raises sndbuf_space past
sndbuf_desc->len, and the assignment moves tx_curs_fin past tx_curs_sent,
breaking the fin <= sent <= prep order the three cursors are documented
with.

The peer can only have consumed what was sent, so bound the difference by
the unconfirmed bytes between tx_curs_fin and tx_curs_sent, and advance
tx_curs_fin by the bounded amount instead of assigning the reported
cursor.  Bounding by sndbuf_desc->len - sndbuf_space would also admit
bytes that are only PREPARED, which would move tx_curs_fin past
tx_curs_sent.

Take the two cursors as snapshots and write tx_curs_fin back as one
cursor.  smc_curs_diff() reads wrap and count with plain loads, and this
runs in a tasklet under bh_lock_sock() while smc_tx_rdma_writes() advances
tx_curs_sent under conn->send_lock, so reading it in place can pair a
pre-wrap wrap with a post-wrap count: the difference then either collapses
to zero, losing the credit after local_rx_ctrl.cons has already moved, or
exceeds the buffer and the bound does nothing.  smc_curs_add() sets count
and then wrap with plain stores and transiently leaves
count >= sndbuf_desc->len, so advancing tx_curs_fin in place would replace
an atomic 64-bit update with a torn one.  smc_tx_prepared_sends() takes
the same snapshots for the same reason.

Fixes: cc0ab806fc52 ("net/smc: adapt cursor update when sndbuf and peer DMB are merged")
Co-developed-by: Kapil Vaswani <kapil@presceralabs.ai>
Signed-off-by: Kapil Vaswani <kapil@presceralabs.ai>
Signed-off-by: Piyus Kedia <piyus@iiitd.ac.in>
---
v4: addresses the review of v3
    (https://lore.kernel.org/r/sashiko-outbox-233892@kernel.org)

    The finding is right on both halves, and the remedy is the one it
    names.  v3 passed &conn->tx_curs_sent and &conn->tx_curs_fin straight
    to smc_curs_diff() and advanced tx_curs_fin in place with
    smc_curs_add().  v4 snapshots both with smc_curs_copy(), computes both
    differences from the snapshots, advances the local copy and writes it
    back with smc_curs_copy(), so the cursor update stays a single atomic
    64-bit write and neither read can tear.  The torn read in the diff_tx
    computation predates this series - it read &conn->tx_curs_fin in place
    - and the snapshot closes that too.

    No change to what is bounded or to the bound itself.

v3: bounded the credit by the unconfirmed sent bytes and advanced
    tx_curs_fin by the bounded difference rather than assigning the
    reported cursor.

    How this was found: an AI-driven fuzzing harness built on syzkaller,
    developed jointly by Kapil Vaswani and myself, driving a forged CDC
    stream at an SMC-D link under KASAN on x86_64.  The analysis and this
    changelog were written with the assistance of a large language model
    and checked by hand against the tree; the reproducers in the earlier
    postings were produced by us.  The v4 change comes from addressing an
    AI-generated review of v3 on the list, and the locking and the two
    cursor helpers it described were each read in the tree first.

Link: https://lore.kernel.org/all/179552966151.3135.8162716126718648323@kernel.org/

 net/smc/smc_cdc.c | 47 ++++++++++++++++++++++++++++++++++++++++++++---
 1 file changed, 44 insertions(+), 3 deletions(-)

diff --git a/net/smc/smc_cdc.c b/net/smc/smc_cdc.c
index 32d6d03df321..8f4351e1d4ad 100644
--- a/net/smc/smc_cdc.c
+++ b/net/smc/smc_cdc.c
@@ -333,6 +333,8 @@
 	union smc_host_cursor cons_old, prod_old;
 	struct smc_connection *conn = &smc->conn;
 	int diff_cons, diff_prod, diff_tx;
+	union smc_host_cursor fin, sent;
+	int unconfirmed;
 
 	smc_curs_copy(&prod_old, &conn->local_rx_ctrl.prod, conn);
 	smc_curs_copy(&cons_old, &conn->local_rx_ctrl.cons, conn);
@@ -358,16 +360,55 @@
 			/* Calculate consumed data and
 			 * increment free send buffer space.
 			 */
+			/* Snapshots, not the live cursors.  smc_curs_diff()
+			 * reads wrap and count with plain loads, and this runs
+			 * in a tasklet under bh_lock_sock() while
+			 * smc_tx_rdma_writes() advances tx_curs_sent under
+			 * conn->send_lock, so reading it in place can pair a
+			 * pre-wrap wrap with a post-wrap count.  Either the
+			 * difference collapses to zero and the credit is lost,
+			 * or it exceeds the buffer and the bound below does
+			 * nothing.  smc_tx_prepared_sends() takes the same
+			 * snapshots for the same reason.
+			 */
+			smc_curs_copy(&fin, &conn->tx_curs_fin, conn);
+			smc_curs_copy(&sent, &conn->tx_curs_sent, conn);
 			diff_tx = smc_curs_diff(conn->sndbuf_desc->len,
-						&conn->tx_curs_fin,
+						&fin,
 						&conn->local_rx_ctrl.cons);
+			/* This difference comes from the cursor the peer
+			 * reported, which is taken as it arrives.  The peer
+			 * can only have consumed what this side actually
+			 * sent - smc_tx_rdma_writes() advances tx_curs_sent
+			 * together with the producer cursor the peer sees -
+			 * so bound it by that and advance tx_curs_fin by the
+			 * bounded amount rather than assigning the reported
+			 * cursor.  The invariant below then holds by
+			 * construction, tx_curs_fin stays inside the send
+			 * buffer, and it stays at or behind tx_curs_sent:
+			 * bounding by sndbuf_desc->len - sndbuf_space would
+			 * also admit bytes that are only PREPARED, which
+			 * would move tx_curs_fin past tx_curs_sent and break
+			 * the fin <= sent <= prep order the three cursors are
+			 * documented with.
+			 */
+			unconfirmed = smc_curs_diff(conn->sndbuf_desc->len,
+						    &fin, &sent);
+			diff_tx = min_t(int, diff_tx, unconfirmed);
+
 			/* increase local sndbuf space and fin_curs */
 			smp_mb__before_atomic();
 			atomic_add(diff_tx, &conn->sndbuf_space);
 			/* guarantee 0 <= sndbuf_space <= sndbuf_desc->len */
 			smp_mb__after_atomic();
-			smc_curs_copy(&conn->tx_curs_fin,
-				      &conn->local_rx_ctrl.cons, conn);
+			/* Advance the snapshot and write it back as one
+			 * cursor.  smc_curs_add() sets count and then wrap
+			 * with plain stores, and transiently leaves
+			 * count >= sndbuf_desc->len, so in place it would
+			 * replace an atomic 64-bit update with a torn one.
+			 */
+			smc_curs_add(conn->sndbuf_desc->len, &fin, diff_tx);
+			smc_curs_copy(&conn->tx_curs_fin, &fin, conn);
 
 			smc_tx_sndbuf_nonfull(smc);
 		}
-- 
2.43.0


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

* Re: [PATCH net v4] net/smc: bound the DMB-nocopy send-buffer credit by what was sent
  2026-11-26 15:06 [PATCH net v4] net/smc: bound the DMB-nocopy send-buffer credit by what was sent Piyus Kedia
@ 2026-11-27 15:06 ` sashiko-bot
  0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-11-27 15:06 UTC (permalink / raw)
  To: Piyus Kedia
  Cc: linux-s390, Heiko Carstens, Vasily Gorbik, Alexander Gordeev,
	Christian Borntraeger

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [High] net/smc: unvalidated local_rx_ctrl.cons in
  smc_cdc_msg_recv_action()
--

commit a47271b7e81e3aca5ca10dfa516b5fde9599703f
Author: Piyus Kedia <piyus@iiitd.ac.in>

net/smc: bound the DMB-nocopy send-buffer credit by what was sent

This commit snapshots tx_curs_fin and tx_curs_sent on the SMC-D
DMB-nocopy receive path, bounds the send-buffer credit by the
unconfirmed sent bytes, and advances tx_curs_fin by that bounded amount.

Link: https://lore.kernel.org/all/179552966151.3135.8162716126718648323@kernel.org/

> diff --git a/net/smc/smc_cdc.c b/net/smc/smc_cdc.c
> index 32d6d03df3214..8f4351e1d4adc 100644
> --- a/net/smc/smc_cdc.c
> +++ b/net/smc/smc_cdc.c
> @@ -333,6 +333,8 @@ static void smc_cdc_msg_recv_action(struct smc_sock *smc,
>  	union smc_host_cursor cons_old, prod_old;
>  	struct smc_connection *conn = &smc->conn;
>  	int diff_cons, diff_prod, diff_tx;
> +	union smc_host_cursor fin, sent;
> +	int unconfirmed;
>  
>  	smc_curs_copy(&prod_old, &conn->local_rx_ctrl.prod, conn);
>  	smc_curs_copy(&cons_old, &conn->local_rx_ctrl.cons, conn);
> @@ -358,16 +360,55 @@ static void smc_cdc_msg_recv_action(struct smc_sock *smc,
>  			/* Calculate consumed data and
>  			 * increment free send buffer space.
>  			 */
> +			/* Snapshots, not the live cursors.  smc_curs_diff()
> +			 * reads wrap and count with plain loads, and this runs
> +			 * in a tasklet under bh_lock_sock() while
> +			 * smc_tx_rdma_writes() advances tx_curs_sent under
> +			 * conn->send_lock, so reading it in place can pair a
> +			 * pre-wrap wrap with a post-wrap count.  Either the
> +			 * difference collapses to zero and the credit is lost,
> +			 * or it exceeds the buffer and the bound below does
> +			 * nothing.  smc_tx_prepared_sends() takes the same
> +			 * snapshots for the same reason.
> +			 */
> +			smc_curs_copy(&fin, &conn->tx_curs_fin, conn);
> +			smc_curs_copy(&sent, &conn->tx_curs_sent, conn);
>  			diff_tx = smc_curs_diff(conn->sndbuf_desc->len,
> -						&conn->tx_curs_fin,
> +						&fin,
>  						&conn->local_rx_ctrl.cons);
> +			/* This difference comes from the cursor the peer
> +			 * reported, which is taken as it arrives.  The peer
> +			 * can only have consumed what this side actually
> +			 * sent - smc_tx_rdma_writes() advances tx_curs_sent
> +			 * together with the producer cursor the peer sees -
> +			 * so bound it by that and advance tx_curs_fin by the
> +			 * bounded amount rather than assigning the reported
> +			 * cursor.  The invariant below then holds by
> +			 * construction, tx_curs_fin stays inside the send
> +			 * buffer, and it stays at or behind tx_curs_sent:
> +			 * bounding by sndbuf_desc->len - sndbuf_space would
> +			 * also admit bytes that are only PREPARED, which
> +			 * would move tx_curs_fin past tx_curs_sent and break
> +			 * the fin <= sent <= prep order the three cursors are
> +			 * documented with.
> +			 */
> +			unconfirmed = smc_curs_diff(conn->sndbuf_desc->len,
> +						    &fin, &sent);
> +			diff_tx = min_t(int, diff_tx, unconfirmed);
> +
>  			/* increase local sndbuf space and fin_curs */
>  			smp_mb__before_atomic();
>  			atomic_add(diff_tx, &conn->sndbuf_space);
>  			/* guarantee 0 <= sndbuf_space <= sndbuf_desc->len */
>  			smp_mb__after_atomic();
> -			smc_curs_copy(&conn->tx_curs_fin,
> -				      &conn->local_rx_ctrl.cons, conn);
> +			/* Advance the snapshot and write it back as one
> +			 * cursor.  smc_curs_add() sets count and then wrap
> +			 * with plain stores, and transiently leaves
> +			 * count >= sndbuf_desc->len, so in place it would
> +			 * replace an atomic 64-bit update with a torn one.
> +			 */
> +			smc_curs_add(conn->sndbuf_desc->len, &fin, diff_tx);
> +			smc_curs_copy(&conn->tx_curs_fin, &fin, conn);
>  
>  			smc_tx_sndbuf_nonfull(smc);
>  		}

[Severity: High]
Does clamping only diff_tx and conn->tx_curs_fin leave
conn->local_rx_ctrl.cons out of sync and unvalidated?

Earlier in smc_cdc_msg_recv_action(), smc_cdc_msg_to_host() copies the
peer's raw consumer cursor directly into conn->local_rx_ctrl.cons, and
the DMB-nocopy block only runs when diff_cons is non-zero:

net/smc/smc_cdc.c:smc_cdc_msg_recv_action() {
	...
	smc_curs_copy(&cons_old, &conn->local_rx_ctrl.cons, conn);
	smc_cdc_msg_to_host(&conn->local_rx_ctrl, cdc, conn);

	diff_cons = smc_curs_diff(conn->peer_rmbe_size, &cons_old,
				  &conn->local_rx_ctrl.cons);
	if (diff_cons) {
		...
		atomic_add(diff_cons, &conn->peer_rmbe_space);
	...
}

When a peer reports a consumer cursor ahead of conn->tx_curs_sent,
conn->tx_curs_fin stops at conn->tx_curs_sent while
conn->local_rx_ctrl.cons keeps the over-reported value.  Subsequent CDC
messages acknowledging newly sent bytes up to that value then compute
diff_cons == 0 and skip the DMB-nocopy block, losing send-buffer credit
and wedging the sender in smc_tx_wait() once conn->sndbuf_space fills.

Passing the unvalidated conn->local_rx_ctrl.cons to smc_curs_diff() can
also wrap 32-bit unsigned arithmetic or overflow the signed int cast in
max_t(int, 0, ...) when local_rx_ctrl.cons.count >= 0x80000000,
collapsing diff_tx to 0 after conn->local_rx_ctrl.cons has already moved,
while a backward cursor with a different wrap yields a positive diff_tx
and credits unconfirmed bytes.  In addition, diff_cons is still added
unbounded to conn->peer_rmbe_space before this block, allowing a peer to
push peer_rmbe_space past peer_rmbe_size or overflow it negative and halt
transmission in smc_tx_sndbuf_nonempty().

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261126150617.2113159-1-piyus@iiitd.ac.in?part=1

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

end of thread, other threads:[~2026-11-27 15:06 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-26 15:06 [PATCH net v4] net/smc: bound the DMB-nocopy send-buffer credit by what was sent Piyus Kedia
2026-11-27 15:06 ` sashiko-bot

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