Linux s390 Architecture development
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Piyus Kedia" <piyus@iiitd.ac.in>
Cc: linux-s390@vger.kernel.org, "Heiko Carstens" <hca@linux.ibm.com>,
	"Vasily Gorbik" <gor@linux.ibm.com>,
	"Alexander Gordeev" <agordeev@linux.ibm.com>,
	"Christian Borntraeger" <borntraeger@linux.ibm.com>
Subject: Re: [PATCH net v3] net/smc: drop the connection when the peer's failover cursor is impossible
Date: Wed, 25 Nov 2026 17:31:05 +0000	[thread overview]
Message-ID: <sashiko-outbox-233843@kernel.org> (raw)
In-Reply-To: <20261124173044.591937-1-piyus@iiitd.ac.in>

Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [High] net/smc: premature cursor rewind before abort check in
  smc_switch_cursor()
- [High] net/smc: incomplete peer consumer cursor validation in
  smc_switch_cursor()
- [Medium] net/smc: false-positive abort in smc_switch_cursor() racing
  tx completion
--

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

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

This commit aborts the SMC connection in smc_switch_cursor() when the
peer's consumer cursor advances past the unconfirmed send buffer bytes
during link failover.

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..ccfc3b1dc8498 100644
> --- a/net/smc/smc_core.c
> +++ b/net/smc/smc_core.c
> @@ -1055,6 +1055,7 @@ static int smc_switch_cursor(struct smc_sock *smc, struct smc_cdc_tx_pend *pend,
>  {
>  	struct smc_connection *conn = &smc->conn;
>  	union smc_host_cursor cons, fin;
> +	int unconfirmed;
>  	int rc = 0;
>  	int diff;
>  
> @@ -1069,6 +1070,38 @@ static int smc_switch_cursor(struct smc_sock *smc, struct smc_cdc_tx_pend *pend,
>  		 * fin above, so now prod is smaller than cons. Fix that.
>  		 */
>  		diff = smc_curs_diff(conn->peer_rmbe_size, &fin, &cons);
> +		/* The peer's consumer cursor is taken as it arrives, so diff
> +		 * is a value the peer chose.  It cannot honestly exceed what
> +		 * this side has prepared and not yet had confirmed, which is
> +		 * sndbuf_desc->len - sndbuf_space: smc_tx_sendmsg() lowers
> +		 * sndbuf_space as data is copied in, and smc_cdc_tx_handler()
> +		 * raises it again as the peer confirms.  A larger diff credits
> +		 * sndbuf_space past the end of the send buffer - breaking the
> +		 * "0 <= sndbuf_space <= sndbuf_desc->len" that the tx path
> +		 * depends on - and pushes tx_curs_sent out of the ring, which
> +		 * smc_tx_rdma_writes() then uses as a source offset.
> +		 *
> +		 * Simply 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 prepared has broken the protocol, so drop the
> +		 * connection, as smc_cdc_msg_validate() does for a sequence
> +		 * number that cannot be honest.
> +		 */
> +		unconfirmed = conn->sndbuf_desc->len -
> +			      atomic_read(&conn->sndbuf_space);
> +		if (diff > max_t(int, unconfirmed, 0)) {

[Severity: High]
Is checking diff against unconfirmed sufficient to validate the peer's
consumer cursor?

First, when conn->sndbuf_desc->len exceeds conn->peer_rmbe_size,
unconfirmed can be larger than conn->peer_rmbe_size (or even twice that).
A forged diff in (conn->peer_rmbe_size, unconfirmed] passes this check,
and because smc_curs_add(conn->peer_rmbe_size, &conn->local_tx_ctrl.prod,
diff) subtracts peer_rmbe_size at most once, local_tx_ctrl.prod.count can
be left >= conn->peer_rmbe_size.

Second, unconfirmed is tx_curs_prep - tx_curs_fin, which includes data
copied into the send buffer that has not been sent yet.  A diff greater
than tx_curs_sent - tx_curs_fin advances tx_curs_sent and tx_curs_fin past
unsent bytes in sndbuf, silently dropping them.

Third, smc_cdc_cursor_to_host() does not enforce cons.count <
conn->peer_rmbe_size or bound cons.wrap jumps, and smc_curs_diff() ignores
multi-wrap differences and uses 32-bit arithmetic.  A peer can send a
cursor with cons.wrap >= fin.wrap + 2 or an out-of-range cons.count that
wraps in smc_curs_diff() to a small diff <= unconfirmed; advancing
local_tx_ctrl.prod by diff then leaves it behind local_rx_ctrl.cons so
smc_write_space() stays <= 0 and halts sending.

[Severity: Medium]
Can an in-flight TX completion on the old link race with this check and
abort a valid connection?

smc_switch_cursor() holds only spin_lock_bh(&conn->send_lock), whereas
smc_cdc_tx_handler() runs under bh_lock_sock(&smc->sk) and credits
conn->sndbuf_space before updating conn->local_tx_ctrl_fin:

CPU0                                  CPU1 (<softirq>)
smc_switch_cursor()                   smc_cdc_tx_handler()
  smc_curs_copy(&fin,
    &conn->local_tx_ctrl_fin, conn)
                                        atomic_add(diff,
                                          &cdcpend->conn->sndbuf_space)
                                        smc_curs_copy(
                                          &conn->local_tx_ctrl_fin, ...)
  diff = smc_curs_diff(..., &fin, &cons)
  unconfirmed = len -
    atomic_read(&conn->sndbuf_space)
  if (diff > max_t(int, unconfirmed, 0))
    queue_work(..., &conn->abort_work)  <-- false-positive abort

Here diff is computed from the pre-completion fin cursor while unconfirmed
reflects the post-completion sndbuf_space, so diff exceeds unconfirmed on
an honest connection.

> +			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 returning early here leave conn->tx_curs_sent and
conn->local_tx_ctrl.prod rewound while TX can still run before abort_work
acquires the socket lock?

At the start of smc_switch_cursor(), before this check runs, the cursors
are already overwritten:

net/smc/smc_core.c:smc_switch_cursor() {
	...
	smc_curs_copy(&conn->tx_curs_sent, &conn->tx_curs_fin, conn);
	smc_curs_copy(&fin, &conn->local_tx_ctrl_fin, conn);
	/* set prod cursor to old state, enforce tx_rdma_writes() */
	smc_curs_copy(&conn->local_tx_ctrl.prod, &fin, conn);
	...
}

Returning here leaves local_tx_ctrl.prod behind local_rx_ctrl.cons and
skips recalculating conn->peer_rmbe_space, which still holds the positive
credit computed when the forged CDC message arrived.  Meanwhile, the TX
path checks conn->killed and local_rx_ctrl.conn_state_flags.peer_conn_abort
rather than conn->out_of_sync or
local_tx_ctrl.conn_state_flags.peer_conn_abort:

net/smc/smc_tx.c:smc_tx_sndbuf_nonempty() {
	...
	if (conn->killed ||
	    conn->local_rx_ctrl.conn_state_flags.peer_conn_abort) {
		rc = -EPIPE;    /* connection being aborted */
		goto out;
	}
	...
}

Until conn->abort_work runs and takes lock_sock(&smc->sk) to call
smc_conn_kill(), concurrent TX under lock_sock(&smc->sk) (such as
smc_tx_sendmsg(), smc_release_cb(), or smc_tx_work()) can call
smc_tx_rdma_writes() with prod < cons.  When prod.wrap != cons.wrap,
smc_tx_rdma_writes() takes the "dst_len = len" branch without clamping to
conn->peer_rmbe_size - prod.count, posting an RDMA write past the end of
the peer RMBE.

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

  reply	other threads:[~2026-11-25 17:31 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-24 17:30 [PATCH net v3] net/smc: drop the connection when the peer's failover cursor is impossible Piyus Kedia
2026-11-25 17:31 ` sashiko-bot [this message]
2026-11-26 15:08   ` Piyus Kedia

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=sashiko-outbox-233843@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=agordeev@linux.ibm.com \
    --cc=borntraeger@linux.ibm.com \
    --cc=gor@linux.ibm.com \
    --cc=hca@linux.ibm.com \
    --cc=linux-s390@vger.kernel.org \
    --cc=piyus@iiitd.ac.in \
    --cc=sashiko-reviews@lists.linux.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox