Skip to contentSkip to conversation

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

PATCH New Piyus Kedia 3 participants 3 messages Nov 26 – Nov 27

Subsystems (5)
lore
Share & tools
lore
#0 lore raw

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

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")

---
v4: addresses the review of v3
(https://lore.kernel.org/r/[email protected])

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.

 net/smc/smc_cdc.c | 47 ++++++++++++++++++++++++++++++++++++++++++++---
 1 file changed, 44 insertions(+), 3 deletions(-)
net/smc/smc_cdc.c +44 -3
index 32d6d03df321..8f4351e1d4ad 100644
@@ -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);
 		}
Signature · 1 line
2.43.0
#1 lore raw
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() --

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 <[email protected]>

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.

Quoted 62 lines
net/smc/smc_cdc.c +37 -3
index 32d6d03df3214..8f4351e1d4adc 100644
@@ -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

net/smc/smc_cdc.c static void smc_cdc_msg_recv_action(struct smc_sock *smc, · line 414

Quoted 10 lines
net/smc/smc_cdc.c +7 -0
+			 * 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().

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#2 lore raw
In reply to Piyus Kedia
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider.

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider.

Critical: 0 (1) · High: 0 · Medium: 1 · Low: 2

If you believe none of the issues are legitimate, please reply to each issue of Medium severity or higher and explain why the AI is wrong. If any of them are legitimate and you plan to respin, please let patchwork know by including "pw-bot: cr" as a separate line at the end of your reply (one such reply per series is enough).

- [Medium] The new clamp makes the sndbuf_space credit depend on the tasklet seeing a tx_curs_sent at least as new as every prod the peer…
- [Low] The commit message treats the DMB-nocopy consumer cursor as untrusted remote input that "nothing bounds", and the Fixes: tag sends…
- [Low] The new comment in smc_cdc_msg_recv_action() (net/smc/smc_cdc.c:392-393) says the bound preserves "the fin <= sent…

Pre-existing issues:
- [Critical] This problem wasn't introduced by this patch, but the producer cursor (prod) from the same CDC message, handled in the same…

--

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

This patch changes the SMC-D DMB-nocopy path in smc_cdc_msg_recv_action(). The sndbuf_space credit is now bounded by the bytes between tx_curs_fin and tx_curs_sent, and tx_curs_fin is advanced by that bounded amount instead of being assigned the consumer cursor the peer reported. Both cursors are snapshotted with smc_curs_copy() before use.

Quoted 6 lines

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.

[Severity: Low]
Does "remote input" describe where this cursor comes from accurately?

smc_ism_support_dmb_nocopy() is only true when the dibs device implements ops->support_mmapped_rdmb:

net/smc/smc_ism.c:smc_ism_support_dmb_nocopy() {
	/* for now only loopback-ism supports
	 * merging sndbuf with peer DMB to avoid
	 * data copies between them.
	 */
	return (smcd->dibs->ops->support_mmapped_rdmb &&
		smcd->dibs->ops->support_mmapped_rdmb(smcd->dibs));
}

The only in-tree provider is in drivers/dibs/dibs_loopback.c:

.support_mmapped_rdmb = dibs_lo_support_dmb_nocopy,

So the peer that writes the cursor is the same kernel's SMC stack. It goes through smcd_cdc_msg_send()->smcd_tx_ism_write()->dibs_lo_move_data().

The CDC header sits at offset 0 of the DMB. smcd_buf_attach() places the ghost sndbuf after sizeof(struct smcd_cdc_msg), so user data written through sendmsg cannot overwrite the header.

The peer only advances cons through recvmsg. recvmsg is limited by bytes_to_rcv, which comes from the prod the peer received, and that prod is at or behind our tx_curs_sent.

As a result, diff_tx <= unconfirmed always holds and the new min_t() never clamps. smc_curs_add(fin, diff_tx) gives the same tx_curs_fin as the old assignment from local_rx_ctrl.cons. Producing the forged CDC stream mentioned in the notes would need kernel-level injection.

Could the changelog say that this path is only reachable with loopback-ism, and call the change hardening? If so, is the Fixes: tag still appropriate, since it sends this to stable as a bug fix?

net/smc/smc_cdc.c static void smc_cdc_msg_recv_action(struct smc_sock *smc, · line 340

Quoted 13 lines
net/smc/smc_cdc.c +2 -0
index 32d6d03df3214..8f4351e1d4adc 100644
@@ -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);

[Severity: Critical]
This isn't a bug introduced by this patch, but the producer cursor from the same CDC message is never range-checked. It is handled a few lines further down in smc_cdc_msg_recv_action(). Can a remote peer use it to read past the end of the RMB?

smcd_cdc_msg_to_host() copies peer->prod.wrap and count straight from the DMB. On SMC-R, smc_cdc_cursor_to_host() is reached from smc_cdc_rx_handler(), and it only rejects backward moves:

	if ((old.wrap > temp.wrap) && temp.wrap)
		return;
	if ((old.wrap == temp.wrap) &&
	    (old.count > temp.count))
		return;
	smc_curs_copy(local, &temp, conn);

Any forward prod is accepted, for example the same wrap with count = 0x70000000. diff_prod then approaches INT_MAX and is added to bytes_to_rcv without a check, even though the comment says:

	atomic_add(diff_prod, &conn->bytes_to_rcv);
	/* guarantee 0 <= bytes_to_rcv <= rmb_desc->len */

If the peer also sets prod_flags.urg_data_present, smc_cdc_handle_urg_data_arrival() does this:

	base = (char *)conn->rmb_desc->cpu_addr + conn->rx_off;
	if (conn->urg_curs.count)
		conn->urg_rx_byte = *(base + conn->urg_curs.count - 1);

urg_curs.count is never compared against rmb_desc->len. The read happens in softirq context and can land up to about 2 GiB past the RMB. It either oopses or leaks one byte to user space through recv(MSG_OOB) in smc_rx_recv_urg().

There is a second path in smc_rx_recvmsg(). There, copylen is bounded only by the inflated bytes_to_rcv. When copylen > rmb_desc->len, the second chunk is:

chunk_len = copylen - chunk_len; /* remainder */ chunk_len_sum += chunk_len;
chunk_off = 0; /* modulo offset in recv ring buffer */

memcpy_to_msg() or smc_rx_splice() copies it from the start of the RMB, and the copy runs past the end of the buffer.

Should prod be checked against rmb_desc->len, and diff_prod against the free space in the RMB, before either is used here?

Quoted 4 lines
net/smc/smc_cdc.c +0 -0
@@ -358,16 +360,55 @@ static void smc_cdc_msg_recv_action(struct smc_sock *smc,
 			/* Calculate consumed data and
 			 * increment free send buffer space.
 			 */

[ ... ]

Quoted 7 lines
net/smc/smc_cdc.c +4 -1
+			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

[ ... ]

net/smc/smc_cdc.c line 394

Quoted 4 lines
net/smc/smc_cdc.c +4 -0
+			 * would move tx_curs_fin past tx_curs_sent and break
+			 * the fin <= sent <= prep order the three cursors are
+			 * documented with.
+			 */

[Severity: Low]
Where is the fin <= sent <= prep ordering documented? Under net/smc/, this new comment is the only place the phrase appears.

The field comments in struct smc_connection in net/smc/smc.h only give loose TCP analogies and state no invariant:

union smc_host_cursor tx_curs_prep; /* tx - prepared data
* snd_max..wmem_alloc */
union smc_host_cursor tx_curs_sent; /* tx - sent data
* snd_nxt ? */
union smc_host_cursor tx_curs_fin; /* tx - confirmed by peer
* snd-wnd-begin ? */

Could the invariant be written down next to these fields? Otherwise, could this comment and the matching sentence in the changelog be reworded so they don't say it is documented?

net/smc/smc_cdc.c line 397

Quoted 3 lines
net/smc/smc_cdc.c +3 -0
+			unconfirmed = smc_curs_diff(conn->sndbuf_desc->len,
+						    &fin, &sent);
+			diff_tx = min_t(int, diff_tx, unconfirmed);

[Severity: Medium]
Can this clamp drop a legitimate credit when the sent snapshot is stale?

The bound assumes this tasklet sees a tx_curs_sent at least as new as any prod the peer has seen. On the writer side, nothing orders those two stores:

smcd_tx_sndbuf_nonempty() /* holds conn->send_lock */
smc_tx_rdma_writes()

    smc_curs_copy(&conn->tx_curs_sent, &sent, conn);
      /* relaxed atomic64_set() on 64-bit */
  smcd_cdc_msg_send()
    smcd_tx_ism_write()
      dibs_lo_move_data()
        read_lock_bh(&ldev->dmb_ht_lock);    /* acquire only */
        memcpy((char *)rmb_node->cpu_addr + offset, data, size);
          /* publishes prod into the peer DMB */

On weakly ordered architectures such as arm64, ppc and riscv, the prod store can become visible before the tx_curs_sent store.

Suppose the peer's rx tasklet is already running on another CPU. It reads the new prod in smcd_cdc_rx_tsklet(), consumes the data and reports cons back, all before the tx_curs_sent store has propagated. The sent snapshot is then behind, so unconfirmed < diff_tx and min_t() drops part of the credit.

local_rx_ctrl.cons has already moved, and this block only runs when diff_cons is non-zero. The dropped part is only credited when the peer moves cons again.

Take a sender that fills the whole sndbuf (sndbuf_space == 0), sends it in one smc_tx_rdma_writes() call and blocks in smc_tx_wait(). If the snapshot is a full write behind, unconfirmed is 0 and nothing is credited. The peer has nothing more to consume, so it sends no further cons update. Does the sender then stall for good?

The old code assigned cons directly and never read tx_curs_sent here. The changelog covers tearing but not the order in which other CPUs see the stores. The timing is very tight and this hasn't been shown on real hardware, but LKMM allows it.

Is an smp_wmb() or a store-release needed between the tx_curs_sent update and publishing the CDC? Alternatively, could the bound avoid depending on tx_curs_sent?

Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261126150617.2113159-1-piyus%40iiitd.ac.in