Linux s390 Architecture development
 help / color / mirror / Atom feed
* [PATCH net v4] net/smc: drop the connection when the peer's failover cursor is impossible
@ 2026-11-26 15:08 Piyus Kedia
  2026-11-27 15:09 ` sashiko-bot
  2026-11-27 15:23 ` netdev-bot+sashiko
  0 siblings, 2 replies; 3+ messages in thread
From: Piyus Kedia @ 2026-11-26 15:08 UTC (permalink / raw)
  To: wenjia, jaka, alibuda, tonylu, guwen
  Cc: netdev, linux-s390, linux-kernel, davem, edumazet, kuba, pabeni,
	horms, kapil, stable

smc_switch_cursor() repairs the send cursors when a connection moves to an
alternate link, and the repair trusts conn->local_rx_ctrl.cons, which the
peer wrote:

	diff = smc_curs_diff(conn->peer_rmbe_size, &fin, &cons);
	smc_curs_add(conn->sndbuf_desc->len, &conn->tx_curs_sent, diff);
	atomic_add(diff, &conn->sndbuf_space);
	smc_curs_add(conn->peer_rmbe_size, &conn->local_tx_ctrl.prod, diff);

A cursor reporting more consumed than this side ever sent credits
sndbuf_space past sndbuf_desc->len, breaking the
0 <= sndbuf_space <= sndbuf_desc->len the tx path depends on, and pushes
tx_curs_sent out of the ring, which smc_tx_rdma_writes() uses as a source
offset.

Capping diff is not enough: the cap would leave local_tx_ctrl.prod behind
local_rx_ctrl.cons, which is the state this block exists to repair, so
smc_write_space() could return a negative peer_rmbe_space and the
connection would stop sending for good.  A peer that reports consuming
what was never sent has broken the protocol, so drop the connection, as
smc_cdc_msg_validate() does for a sequence number that cannot be honest.

Three things make the cursor impossible, and smc_cursor_cannot_be_honest()
checks all three:

 - cons.count at or past peer_rmbe_size.  smc_cdc_cursor_to_host() does
   not bound it.
 - a difference of peer_rmbe_size or more.  smc_curs_add() subtracts the
   size at most once, so a larger difference leaves
   local_tx_ctrl.prod.count outside the peer's RMBE.  Below peer_rmbe_size
   the repair is exact: prod starts at fin and diff is their distance, so
   prod lands on cons rather than behind it.
 - a difference past the bytes that were SENT and not yet confirmed, taken
   as the distance between two snapshots of tx_curs_fin and tx_curs_sent.

The check runs before anything is rewound, and the connection is dropped
with the cursors as they were found.  Doing it after the rewind is what
would make it unsafe: tx_curs_sent and local_tx_ctrl.prod are set back to
fin at the top of the function, and returning from there leaves prod behind
local_rx_ctrl.cons with peer_rmbe_space still holding the credit the forged
message produced.  The tx path tests conn->killed and
local_rx_ctrl.conn_state_flags.peer_conn_abort, not out_of_sync or the
local_tx_ctrl flag set here, so a send under lock_sock() can reach
smc_tx_rdma_writes() with prod < cons before abort_work gets the socket
lock - and with the wraps differing it takes the dst_len = len branch
without clamping to peer_rmbe_size - prod.count, writing past the end of
the peer RMBE.

Fixes: c6f02ebeea3a ("net/smc: switch connections to alternate link")
Cc: stable@vger.kernel.org
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-233843@kernel.org)

    All three findings are right.

    - the check moved ahead of the rewind.  v3 ran it inside the fix-up
      block and returned 0 from there, after tx_curs_sent and
      local_tx_ctrl.prod had been set back to fin, which is the window the
      review describes: the tx path does not look at out_of_sync or at the
      local_tx_ctrl abort flag, so a concurrent send could reach
      smc_tx_rdma_writes() with prod < cons before abort_work took the
      socket lock.  Nothing is modified now before the connection is
      dropped.
    - the bound is the SENT bytes, not sndbuf_desc->len - sndbuf_space.
      That counter is the PREPARED bytes, so v3 would have advanced
      tx_curs_sent and tx_curs_fin over data still in sndbuf, and it is
      also where the false positive came from: smc_cdc_tx_handler()
      credits sndbuf_space before updating local_tx_ctrl_fin and this
      holds only send_lock, so an honest completion between the two could
      make diff exceed it.  Both differences now come from cursor
      snapshots, so they are consistent with each other.
    - cons.count and diff are bounded by peer_rmbe_size, which v3 left to
      smc_curs_add()'s single subtraction.

    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-R 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 changes come from addressing an
    AI-generated review of v3 on the list, and the tx-path guards, the
    dst_len branch and the two cursor helpers it named were each read in
    the tree first.

Link: https://lore.kernel.org/all/20261122053223.2497832-2-piyus@iiitd.ac.in/

 net/smc/smc_core.c | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++----
 1 file changed, 66 insertions(+), 2 deletions(-)

diff --git a/net/smc/smc_core.c b/net/smc/smc_core.c
index 9974149659c2..31300ecd6b12 100644
--- a/net/smc/smc_core.c
+++ b/net/smc/smc_core.c
@@ -1030,6 +1030,48 @@
 	return space;
 }
 
+/* Whether the peer's consumer cursor can honestly have reached where it says.
+ *
+ * smc_switch_cursor() repairs the send cursors from conn->local_rx_ctrl.cons,
+ * which the peer wrote.  A cursor reporting more consumed than this side sent
+ * credits sndbuf_space past sndbuf_desc->len and pushes tx_curs_sent out of
+ * the ring, which smc_tx_rdma_writes() uses as a source offset.  Three things
+ * make it impossible:
+ *
+ *  - cons.count at or past peer_rmbe_size, which smc_cdc_cursor_to_host()
+ *    does not bound;
+ *  - a difference of peer_rmbe_size or more, which would survive the single
+ *    subtraction smc_curs_add() does and leave local_tx_ctrl.prod.count
+ *    outside the peer's RMBE.  Below that the repair is exact: prod starts at
+ *    fin and diff is their distance, so prod lands on cons, not behind it;
+ *  - a difference past what was SENT and not yet confirmed.  That is the
+ *    distance between two cursor snapshots, not
+ *    sndbuf_desc->len - sndbuf_space: the counter covers PREPARED bytes, so
+ *    bounding by it would advance the cursors over data still in sndbuf, and
+ *    comparing a cursor difference against it is a race in its own right -
+ *    smc_cdc_tx_handler() credits sndbuf_space before it updates
+ *    local_tx_ctrl_fin, and the caller holds only send_lock, so an honest
+ *    completion in between could make diff exceed it.
+ */
+static bool smc_cursor_cannot_be_honest(struct smc_connection *conn,
+					union smc_host_cursor *fin,
+					union smc_host_cursor *cons)
+{
+	union smc_host_cursor tx_fin, tx_sent;
+	int diff, unconfirmed;
+
+	if (smc_curs_comp(conn->peer_rmbe_size, cons, fin) >= 0)
+		return false;
+
+	smc_curs_copy(&tx_fin, &conn->tx_curs_fin, conn);
+	smc_curs_copy(&tx_sent, &conn->tx_curs_sent, conn);
+	diff = smc_curs_diff(conn->peer_rmbe_size, fin, cons);
+	unconfirmed = smc_curs_diff(conn->sndbuf_desc->len, &tx_fin, &tx_sent);
+
+	return cons->count >= (u32)conn->peer_rmbe_size ||
+	       diff >= conn->peer_rmbe_size || diff > unconfirmed;
+}
+
 static int smc_switch_cursor(struct smc_sock *smc, struct smc_cdc_tx_pend *pend,
 			     struct smc_wr_buf *wr_buf)
 {
@@ -1038,11 +1080,33 @@
 	int rc = 0;
 	int diff;
 
-	smc_curs_copy(&conn->tx_curs_sent, &conn->tx_curs_fin, conn);
+	/* Before anything is rewound.  Returning from inside the repair below
+	 * would leave local_tx_ctrl.prod behind local_rx_ctrl.cons with
+	 * peer_rmbe_space still holding the credit the forged message made,
+	 * and the tx path tests conn->killed and
+	 * local_rx_ctrl.conn_state_flags.peer_conn_abort rather than
+	 * out_of_sync or the flag set here - so a send under lock_sock() could
+	 * reach smc_tx_rdma_writes() with prod < cons before abort_work takes
+	 * the socket lock, and with the wraps differing that takes the
+	 * dst_len = len branch without clamping to peer_rmbe_size -
+	 * prod.count and writes past the end of the peer RMBE.
+	 */
 	smc_curs_copy(&fin, &conn->local_tx_ctrl_fin, conn);
+	smc_curs_copy(&cons, &conn->local_rx_ctrl.cons, conn);
+	if (smc_cursor_cannot_be_honest(conn, &fin, &cons)) {
+		conn->out_of_sync = 1;	/* prevent any further receives */
+		conn->local_tx_ctrl.conn_state_flags.peer_conn_abort = 1;
+		smc_wr_tx_put_slot(conn->lnk,
+				   (struct smc_wr_tx_pend_priv *)pend);
+		sock_hold(&smc->sk); /* sock_put in abort_work */
+		if (!queue_work(smc_close_wq, &conn->abort_work))
+			sock_put(&smc->sk);
+		return 0;
+	}
+
+	smc_curs_copy(&conn->tx_curs_sent, &conn->tx_curs_fin, conn);
 	/* set prod cursor to old state, enforce tx_rdma_writes() */
 	smc_curs_copy(&conn->local_tx_ctrl.prod, &fin, conn);
-	smc_curs_copy(&cons, &conn->local_rx_ctrl.cons, conn);
 
 	if (smc_curs_comp(conn->peer_rmbe_size, &cons, &fin) < 0) {
 		/* cons cursor advanced more than fin, and prod was set
-- 
2.43.0


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

* Re: [PATCH net v4] net/smc: drop the connection when the peer's failover cursor is impossible
  2026-11-26 15:08 [PATCH net v4] net/smc: drop the connection when the peer's failover cursor is impossible Piyus Kedia
@ 2026-11-27 15:09 ` sashiko-bot
  2026-11-27 15:23 ` netdev-bot+sashiko
  1 sibling, 0 replies; 3+ messages in thread
From: sashiko-bot @ 2026-11-27 15:09 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 4 potential
issues to consider:
- [High] net/smc: early smc_curs_comp() return in
  smc_cursor_cannot_be_honest() bypasses cons->count bounds check
- [High] net/smc: missing cons->wrap check in
  smc_cursor_cannot_be_honest() allows multi-wrap bypass
- [High] net/smc: abort path in smc_switch_cursor() still allows
  concurrent TX with prod < cons
- [Medium] net/smc: reversed fin cursor snapshots in smc_switch_cursor()
  race with smc_cdc_tx_handler()
--

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

net/smc: drop the connection when the peer's failover cursor is impossible

This commit adds smc_cursor_cannot_be_honest() to validate the peer's
consumer cursor in smc_switch_cursor() before rewinding and repairing
the send cursors during link failover, aborting the connection if the
cursor is invalid.

Link: https://lore.kernel.org/all/20261122053223.2497832-2-piyus@iiitd.ac.in/

> diff --git a/net/smc/smc_core.c b/net/smc/smc_core.c
> index a3164201c1420..e15629b4e384b 100644
> --- a/net/smc/smc_core.c
> +++ b/net/smc/smc_core.c
> @@ -1050,6 +1050,48 @@ static int smc_write_space(struct smc_connection *conn)
>  	return space;
>  }
>  
> +/* Whether the peer's consumer cursor can honestly have reached where it says.
[ ... ]
> + */
> +static bool smc_cursor_cannot_be_honest(struct smc_connection *conn,
> +					union smc_host_cursor *fin,
> +					union smc_host_cursor *cons)
> +{
> +	union smc_host_cursor tx_fin, tx_sent;
> +	int diff, unconfirmed;
> +
> +	if (smc_curs_comp(conn->peer_rmbe_size, cons, fin) >= 0)
> +		return false;

[Severity: High]
Can the early return on smc_curs_comp() >= 0 bypass the cons->count
bounds check below?

smc_cdc_cursor_to_host() does not bound cons->count, and smc_curs_diff()
casts unsigned 32-bit cursor arithmetic to signed int inside
max_t(int, 0, ...).  If a peer sends cons->count >= 0x80000000U with
cons->wrap > fin->wrap so that (size - fin->count) + cons->count has bit
31 set, smc_curs_diff() clamps the negative int to 0 and smc_curs_comp()
returns 0.  smc_cursor_cannot_be_honest() then returns false without
checking cons->count >= (u32)conn->peer_rmbe_size, leaving
smc_switch_cursor() to skip the repair with prod.wrap != cons.wrap and
compute a positive peer_rmbe_space so smc_tx_rdma_writes() takes the
unclamped dst_len = len branch past the end of the peer RMBE.

> +
> +	smc_curs_copy(&tx_fin, &conn->tx_curs_fin, conn);
> +	smc_curs_copy(&tx_sent, &conn->tx_curs_sent, conn);
> +	diff = smc_curs_diff(conn->peer_rmbe_size, fin, cons);
> +	unconfirmed = smc_curs_diff(conn->sndbuf_desc->len, &tx_fin, &tx_sent);
> +
> +	return cons->count >= (u32)conn->peer_rmbe_size ||
> +	       diff >= conn->peer_rmbe_size || diff > unconfirmed;
> +}

[Severity: High]
Can a peer with cons->wrap more than one wrap ahead of fin->wrap pass
smc_cursor_cannot_be_honest() and leave conn->local_tx_ctrl.prod behind
conn->local_rx_ctrl.cons?

smc_curs_diff() only checks old->wrap != new->wrap and ignores how many
wraps separate them:

net/smc/smc_cdc.h:smc_curs_diff() {
	if (old->wrap != new->wrap)
		return max_t(int, 0,
			     ((size - old->count) + new->count));
...
}

If the peer sends cons->wrap >= fin->wrap + 2 with cons->count <
fin->count and diff <= unconfirmed, smc_cursor_cannot_be_honest() returns
false, but smc_curs_add() in smc_switch_cursor() increments
conn->local_tx_ctrl.prod.wrap at most once (to fin->wrap + 1).  That
leaves prod.wrap != cons.wrap after the repair; any subsequent CDC update
that advances cons credits peer_rmbe_space > 0 while prod.wrap !=
cons.wrap, allowing smc_tx_rdma_writes() to take the unclamped
dst_len = len branch and write past the end of the peer RMBE.

> +
>  static int smc_switch_cursor(struct smc_sock *smc, struct smc_cdc_tx_pend *pend,
>  			     struct smc_wr_buf *wr_buf)
>  {
> @@ -1058,11 +1100,33 @@ static int smc_switch_cursor(struct smc_sock *smc, struct smc_cdc_tx_pend *pend,
>  	int rc = 0;
>  	int diff;
>  
> -	smc_curs_copy(&conn->tx_curs_sent, &conn->tx_curs_fin, conn);
> +	/* Before anything is rewound.  Returning from inside the repair below
> +	 * would leave local_tx_ctrl.prod behind local_rx_ctrl.cons with
> +	 * peer_rmbe_space still holding the credit the forged message made,
> +	 * and the tx path tests conn->killed and
> +	 * local_rx_ctrl.conn_state_flags.peer_conn_abort rather than
> +	 * out_of_sync or the flag set here - so a send under lock_sock() could
> +	 * reach smc_tx_rdma_writes() with prod < cons before abort_work takes
> +	 * the socket lock, and with the wraps differing that takes the
> +	 * dst_len = len branch without clamping to peer_rmbe_size -
> +	 * prod.count and writes past the end of the peer RMBE.
> +	 */
>  	smc_curs_copy(&fin, &conn->local_tx_ctrl_fin, conn);
> +	smc_curs_copy(&cons, &conn->local_rx_ctrl.cons, conn);
> +	if (smc_cursor_cannot_be_honest(conn, &fin, &cons)) {

[Severity: Medium]
Can reading conn->local_tx_ctrl_fin before conn->tx_curs_fin race with
smc_cdc_tx_handler() and falsely abort a healthy connection?

smc_switch_cursor() holds conn->send_lock, while smc_cdc_tx_handler()
runs under bh_lock_sock() without conn->send_lock and updates the two
cursors in the opposite order:

CPU0                                  CPU1 (<softirq>)
smc_switch_cursor()                   smc_cdc_tx_handler()
  smc_curs_copy(&fin,
    &conn->local_tx_ctrl_fin, conn)
                                        smc_curs_copy(&conn->tx_curs_fin,
                                          &cdcpend->cursor, conn)
                                        smc_curs_copy(&conn->local_tx_ctrl_fin,
                                          &cdcpend->p_cursor, conn)
  smc_cursor_cannot_be_honest()
    smc_curs_copy(&tx_fin,
      &conn->tx_curs_fin, conn)
    diff > unconfirmed  <-- false positive abort

Because fin has the pre-completion value while tx_fin has the
post-completion value, diff includes the newly confirmed bytes that an
honest peer may already have consumed, while unconfirmed has already
subtracted them.  Snapshotting conn->tx_curs_fin before
conn->local_tx_ctrl_fin avoids the false positive, though a completion
after the check can still cause the repair block below to use the older
fin snapshot.

> +		conn->out_of_sync = 1;	/* prevent any further receives */
> +		conn->local_tx_ctrl.conn_state_flags.peer_conn_abort = 1;
> +		smc_wr_tx_put_slot(conn->lnk,
> +				   (struct smc_wr_tx_pend_priv *)pend);
> +		sock_hold(&smc->sk); /* sock_put in abort_work */
> +		if (!queue_work(smc_close_wq, &conn->abort_work))
> +			sock_put(&smc->sk);
> +		return 0;
> +	}

[Severity: High]
Can a concurrent sender under lock_sock() still reach smc_tx_rdma_writes()
with prod < cons after this abort branch returns?

When the peer forges cons past what was ever sent (diff > unconfirmed),
smc_cdc_msg_recv_action() has already written the forged cursor to
conn->local_rx_ctrl.cons and credited conn->peer_rmbe_space, so
conn->local_tx_ctrl.prod (which sits at fin + unconfirmed) is already
behind conn->local_rx_ctrl.cons before smc_switch_cursor() runs.
smc_switch_conns() switches conn->lnk to the new active link right before
calling smc_switch_cursor():

CPU0                                  CPU1
smc_tx_sendmsg()
  lock_sock(&smc->sk)
                                      smc_switch_conns()
                                        spin_lock_bh(&conn->send_lock)
                                        smc_switch_link_and_count(conn, to_lnk)
                                        smc_switch_cursor()
                                          conn->out_of_sync = 1
                                          queue_work(..., &conn->abort_work)
                                        spin_unlock_bh(&conn->send_lock)
  smc_tx_sndbuf_nonempty()
    spin_lock_bh(&conn->send_lock)
    smc_tx_rdma_writes()  <-- prod < cons on to_lnk

Because smc_tx_sndbuf_nonempty() checks only peer_rmbe_space <= 0,
conn->killed, and conn->local_rx_ctrl.conn_state_flags.peer_conn_abort,
CPU0 is not stopped before abort_work acquires lock_sock(&smc->sk), and
can take the unclamped dst_len = len branch when prod.wrap != cons.wrap.
Setting conn->killed = 1 or atomic_set(&conn->peer_rmbe_space, 0) before
dropping conn->send_lock would block the TX path immediately.

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

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

* Re: [PATCH net v4] net/smc: drop the connection when the peer's failover cursor is impossible
  2026-11-26 15:08 [PATCH net v4] net/smc: drop the connection when the peer's failover cursor is impossible Piyus Kedia
  2026-11-27 15:09 ` sashiko-bot
@ 2026-11-27 15:23 ` netdev-bot+sashiko
  1 sibling, 0 replies; 3+ messages in thread
From: netdev-bot+sashiko @ 2026-11-27 15:23 UTC (permalink / raw)
  To: piyus
  Cc: wenjia, jaka, alibuda, tonylu, guwen, netdev, linux-s390,
	linux-kernel, davem, edumazet, kuba, pabeni, horms, kapil, stable

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

Critical: 0 · High: 3 · Medium: 3 · Low: 0

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

- [High] The patch's safety argument does not hold: the state it says the
  early check avoids already exists before smc_switch_cursor() runs, and…
- [High] In smc_cursor_cannot_be_honest() (net/smc/smc_core.c:1083-1084),
  `if (smc_curs_comp(conn->peer_rmbe_size, cons, fin) >= 0) return…
- [High] The check runs on snapshots, but the repair then modifies live
  cursors that smc_cdc_tx_handler() can change in between, so the…
- [Medium] The honesty test in smc_cursor_cannot_be_honest() compares two
  distances built from separately read cursors, so an honest send…
- [Medium] smc_cursor_cannot_be_honest() measures how far cons is ahead
  of fin with smc_curs_diff(), which only sees whether the wraps differ,…
- [Medium] Off-by-one in smc_cursor_cannot_be_honest(): `diff >=
  conn->peer_rmbe_size` (smc_core.c:1092) rejects diff ==…

--

net/smc: drop the connection when the peer's failover cursor is impossible

This commit adds smc_cursor_cannot_be_honest(), which checks the peer's
consumer cursor before smc_switch_cursor() repairs the send cursors during
link failover. If the cursor is judged impossible, the connection is
marked out_of_sync, the abort flag is set, and abort_work is queued before
any cursor is rewound.

Link: https://lore.kernel.org/all/20261122053223.2497832-2-piyus@iiitd.ac.in/

> diff --git a/net/smc/smc_core.c b/net/smc/smc_core.c
> index a3164201c1420..e15629b4e384b 100644
> --- a/net/smc/smc_core.c
> +++ b/net/smc/smc_core.c
> @@ -1050,6 +1050,48 @@ static int smc_write_space(struct smc_connection *conn)

[ ... ]

> +static bool smc_cursor_cannot_be_honest(struct smc_connection *conn,
> +					union smc_host_cursor *fin,
> +					union smc_host_cursor *cons)
> +{
> +	union smc_host_cursor tx_fin, tx_sent;
> +	int diff, unconfirmed;
> +
> +	if (smc_curs_comp(conn->peer_rmbe_size, cons, fin) >= 0)
> +		return false;

[Severity: High]

Can the cons->count bound below be reached when cons's wrap is behind
fin's? The commit message says "Three things make the cursor impossible,
and smc_cursor_cannot_be_honest() checks all three". However, this early
return skips the count check whenever smc_curs_comp() is >= 0.

smc_curs_comp() returns >= 0 for any cons with cons.wrap < fin.wrap,
whatever cons.count is. At the same wrap, smc_cdc_cursor_to_host() accepts
any count that is not smaller than the old one:

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

So a peer could install cons = (fin.wrap - 1, peer_rmbe_size + X). The
helper returns false, prod is set to fin, and smc_switch_cursor() then
does:

	atomic_set(&smc->conn.peer_rmbe_space, smc_write_space(conn));

In smc_write_space(), the unsigned arithmetic in smc_curs_diff() wraps:
(size - (size + X)) + F gives F - X, so space becomes size - F + X. Then
tx_work is queued. smc_tx_rdma_writes() sees prod.wrap != cons.wrap and
takes this branch:

		dst_len = len;

Here dst_off = F and len can be as large as size - F + X. Would that write
X bytes past the end of the peer RMBE?

There is a second variant. If cons.wrap = fin.wrap + 1 and the count is
huge, the max_t(int, 0, ...) in smc_curs_diff() clamps to 0, so
smc_curs_comp() returns 0. smc_write_space() then stores a negative
peer_rmbe_space and the connection stops sending for good. The commit
message says a simple cap would cause exactly this.

Should cons.count be checked before any comparison?

> +
> +	smc_curs_copy(&tx_fin, &conn->tx_curs_fin, conn);
> +	smc_curs_copy(&tx_sent, &conn->tx_curs_sent, conn);

[Severity: Medium]

Can an honest send completion that races with these reads make a good
connection look dishonest?

smc_switch_cursor() reads fin from conn->local_tx_ctrl_fin before these
reads of tx_curs_fin and tx_curs_sent. The only lock held is
conn->send_lock, taken by smc_switch_conns(). smc_cdc_tx_handler() runs
under bh_lock_sock() only and writes the cursors in the opposite order:

		atomic_add(diff, &cdcpend->conn->sndbuf_space);
		...
		smc_curs_copy(&conn->tx_curs_fin, &cdcpend->cursor, conn);
		smc_curs_copy(&conn->local_tx_ctrl_fin, &cdcpend->p_cursor,
			      conn);

If a completion lands between the fin read and the tx_fin read, fin is
stale and tx_fin is fresh. diff = cons - fin_old stays large, while
unconfirmed = tx_sent - tx_fin_new shrinks.

For example, say the peer honestly consumed everything (cons == prod) and
the last in-flight CDC completes inside that window. unconfirmed becomes
0 and diff > unconfirmed, so the connection gets out_of_sync,
peer_conn_abort and abort_work.

Successful completions on the old link during the switch look possible.
smc_llc_process_cli_delete_link(), smc_llc_process_srv_delete_link() and
smc_llc_delete_asym_link() call smc_switch_conns() on a QP that still
works, and only drain pending sends afterwards.

Given this, is the v4 changelog statement "Both differences now come from
cursor snapshots, so they are consistent with each other" accurate?

> +	diff = smc_curs_diff(conn->peer_rmbe_size, fin, cons);

[Severity: Medium]

Does this distance handle a cons that is two or more wraps ahead of fin?
smc_curs_diff() only checks whether the wraps differ, not by how much, and
smc_cdc_cursor_to_host() accepts any larger wrap.

Take cons = (fin.wrap + 2, c) with c < fin.count = f. smc_curs_comp() is
negative, so the check runs, and:

  diff = (size - f) + c < size
  cons.count = c < size
  diff <= unconfirmed, as long as size - f + c bytes are in flight

All three tests pass. The repair then sets prod = (fin.wrap + 1, c), which
is exactly one full RMBE behind cons, and smc_write_space() returns 0. The
helper's comment says "prod lands on cons, not behind it", which this
seems to contradict.

prod.wrap would then never match cons.wrap. Would the next normal-looking
cons advance credit peer_rmbe_space and send smc_tx_rdma_writes() down the
unclamped dst_len = len branch past the end of the RMBE?

The diff >= peer_rmbe_size test also seems wrong in the other direction.
Once cons.count < size is known, f + diff = size + c < 2 * size, so the
single subtraction in smc_curs_add() always keeps prod.count in range.
What the test does reject is cons.wrap == fin.wrap + 1 with c >= f. The
repair would handle that case exactly (prod == cons).

If sndbuf_desc->len > peer_rmbe_size and send completions lag, could an
honest connection reach that state and be aborted?

> +	unconfirmed = smc_curs_diff(conn->sndbuf_desc->len, &tx_fin, &tx_sent);
> +
> +	return cons->count >= (u32)conn->peer_rmbe_size ||
> +	       diff >= conn->peer_rmbe_size || diff > unconfirmed;

[Severity: Medium]

Is diff >= conn->peer_rmbe_size off by one? diff == peer_rmbe_size is the
largest value an honest peer can produce, and the existing repair handles
it exactly.

This can happen in the following case:

- sndbuf_desc->len >= peer_rmbe_size, for example equal buffer sizes.
- A full peer RMBE was written after local_tx_ctrl_fin.
- The peer consumed all of it, so cons = (fin.wrap + 1, fin.count).
- The send CQEs for those CDCs were not processed yet, or were flushed on
  link failure.

Then unconfirmed == size, so diff > unconfirmed is false and only this
test fires.

The repair is exact in that state:

- smc_curs_add(size, &prod, size) gives (fin.wrap + 1, fin.count), which
  equals cons.
- tx_curs_sent needs only one subtraction, since
  tx_fin.count + size < 2 * len.
- sndbuf_space goes from len - size back to at most len.

That failover worked before this patch. With the patch, the connection is
aborted. The helper's comment says a difference of peer_rmbe_size "would
survive the single subtraction smc_curs_add() does", which also looks
wrong for diff == size.

Should this be diff > conn->peer_rmbe_size?

> +}
> +
>  static int smc_switch_cursor(struct smc_sock *smc, struct smc_cdc_tx_pend *pend,
>  			     struct smc_wr_buf *wr_buf)
>  {
> @@ -1058,11 +1100,33 @@ static int smc_switch_cursor(struct smc_sock *smc, struct smc_cdc_tx_pend *pend,
>  	int rc = 0;
>  	int diff;
>  
> -	smc_curs_copy(&conn->tx_curs_sent, &conn->tx_curs_fin, conn);
> +	/* Before anything is rewound.  Returning from inside the repair below
> +	 * would leave local_tx_ctrl.prod behind local_rx_ctrl.cons with
> +	 * peer_rmbe_space still holding the credit the forged message made,
> +	 * and the tx path tests conn->killed and
> +	 * local_rx_ctrl.conn_state_flags.peer_conn_abort rather than
> +	 * out_of_sync or the flag set here - so a send under lock_sock() could
> +	 * reach smc_tx_rdma_writes() with prod < cons before abort_work takes
> +	 * the socket lock, and with the wraps differing that takes the
> +	 * dst_len = len branch without clamping to peer_rmbe_size -
> +	 * prod.count and writes past the end of the peer RMBE.
> +	 */
>  	smc_curs_copy(&fin, &conn->local_tx_ctrl_fin, conn);
> +	smc_curs_copy(&cons, &conn->local_rx_ctrl.cons, conn);
> +	if (smc_cursor_cannot_be_honest(conn, &fin, &cons)) {
> +		conn->out_of_sync = 1;	/* prevent any further receives */
> +		conn->local_tx_ctrl.conn_state_flags.peer_conn_abort = 1;
> +		smc_wr_tx_put_slot(conn->lnk,
> +				   (struct smc_wr_tx_pend_priv *)pend);
> +		sock_hold(&smc->sk); /* sock_put in abort_work */
> +		if (!queue_work(smc_close_wq, &conn->abort_work))
> +			sock_put(&smc->sk);
> +		return 0;
> +	}

[Severity: High]

Does checking early actually prevent the overrun described in the comment
above and in the commit message?

The normal receive path stores and credits the forged cons, with or
without a failover. smc_cdc_msg_recv_action() does:

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

Nothing checks that cons is at or behind local_tx_ctrl.prod, or that
cons.count < peer_rmbe_size. smc_cdc_cursor_to_host() only rejects
cursors that move backwards.

So a forged wrap bump or over-consumption already leaves prod < cons with
inflated credit. The same function then starts the sender:

	if ((diff_cons && smc_tx_prepared_sends(conn)) ||
	...

That reaches the unclamped dst_len = len branch with no link switch
involved:

smc_tx_pending()
  smc_tx_sndbuf_nonempty()
    smcr_tx_sndbuf_nonempty()
      smc_tx_rdma_writes()

When a failover does happen, this branch returns before peer_rmbe_space
is recomputed. It sets only out_of_sync and
local_tx_ctrl.conn_state_flags.peer_conn_abort. The tx gate in
smc_tx_sndbuf_nonempty() checks neither of them:

	if (conn->killed ||
	    conn->local_rx_ctrl.conn_state_flags.peer_conn_abort) {

Could a sender on the new link still overrun the peer RMBE until
abort_work runs?

Would it be better to validate the peer cursor where it is accepted, in
smc_cdc_msg_recv_action() or smc_cdc_cursor_to_host()? That would cover
every connection and make the failover-time check unnecessary.

The commit message does not mention this receive path. A stable
backporter could read it as fixing the forged-cons RMBE overrun.

> +
> +	smc_curs_copy(&conn->tx_curs_sent, &conn->tx_curs_fin, conn);

[Severity: High]

smc_cursor_cannot_be_honest() ran on snapshots of tx_curs_fin and
tx_curs_sent, but the repair here works on the live cursors. Can
smc_cdc_tx_handler() change them in between?

The repair copies the live conn->tx_curs_fin into tx_curs_sent. It then
advances tx_curs_fin and local_tx_ctrl_fin by diff and does
atomic_add(diff, &conn->sndbuf_space), holding only send_lock.

smc_cdc_tx_handler() runs under bh_lock_sock(). Between the snapshot and
the repair it can confirm d bytes, add d to sndbuf_space, and move
tx_curs_fin forward by d.

Suppose tx_curs_prep == tx_curs_sent and diff == unconfirmed. An honest
peer that consumed everything gets there, and so does a peer that sets
cons to exactly that value. sndbuf_space then ends at:

  len - unconfirmed + d + unconfirmed = len + d

and tx_curs_sent and tx_curs_fin end at sent + d, past tx_curs_prep.

smc_tx_sendmsg() then computes:

		copylen = min_t(size_t, send_remaining, writespace);

The second memcpy_from_msg() chunk, at offset 0, could then be up to
prep.count + d long. Could that write user data past the end of the
sndbuf allocation, or overwrite data that has not been sent yet?

The old link can still complete sends successfully during the switch.
smc_llc_process_srv_delete_link() does:

		if (smc_switch_conns(lgr, lnk_del, false))
			smc_wr_tx_wait_no_pending_sends(lnk_del);

The underlying race existed before this patch. Still, the patch says it
restores 0 <= sndbuf_space <= sndbuf_desc->len and discusses the
concurrency with smc_cdc_tx_handler(), yet it applies a diff validated on
snapshots to live state.

>  	/* set prod cursor to old state, enforce tx_rdma_writes() */
>  	smc_curs_copy(&conn->local_tx_ctrl.prod, &fin, conn);
> -	smc_curs_copy(&cons, &conn->local_rx_ctrl.cons, conn);

[ ... ]

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

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

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

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-26 15:08 [PATCH net v4] net/smc: drop the connection when the peer's failover cursor is impossible Piyus Kedia
2026-11-27 15:09 ` sashiko-bot
2026-11-27 15:23 ` netdev-bot+sashiko

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