Netdev List
 help / color / mirror / Atom feed
From: netdev-bot+sashiko@kernel.org
To: hexlabsecurity@proton.me
Cc: mjambigi@linux.ibm.com, dust.li@linux.alibaba.com,
	edumazet@kernel.org, pabeni@redhat.com, davem@davemloft.net,
	kuba@kernel.org, alibuda@linux.alibaba.com,
	sidraya@linux.ibm.com, ubraun@linux.ibm.com,
	wenjia@linux.ibm.com, raspl@linux.ibm.com,
	linux-s390@vger.kernel.org, linux-kernel@vger.kernel.org,
	guwen@linux.alibaba.com, tonylu@linux.alibaba.com,
	horms@kernel.org, guangguan.wang@linux.alibaba.com,
	linux-rdma@vger.kernel.org, netdev@vger.kernel.org,
	hidayath@linux.ibm.com
Subject: Re: [PATCH net v9 3/6] net/smc: bound the send length to the send buffer in smc_tx_sendmsg()
Date: Mon, 16 Nov 2026 13:00:37 +0000	[thread overview]
Message-ID: <179483403722.3136.1364122923722036240@kernel.org> (raw)
In-Reply-To: <20261112-b4-disp-0287c16e-v9-3-e5e8fa688177@proton.me>

Thank you for your contribution! Sashiko AI review found more severe
issue(s) in other patches of this series. This mail is only an FYI, in case
you want to address the nit picks below when you respin.

Low priority / nit picks for your consideration:

- [Low] The patch changes the sndbuf_space invariant comment to "can be
  out of range" in smc_cdc_tx_handler() (net/smc/smc_cdc.c:46) and after…

Pre-existing issues:
- [High] This problem wasn't introduced by this patch, but the clamp in
  smc_tx_sendmsg() limits writespace to sndbuf_desc->len (the allocation…
- [High] This problem wasn't introduced by this patch, but the failover
  catch-up in smc_switch_cursor() and the completion handler…

--

net/smc: bound the send length to the send buffer in smc_tx_sendmsg()

This commit clamps writespace in smc_tx_sendmsg() to sndbuf_desc->len,
negative values included. The goal is to keep the copy inside the send
buffer when conn->sndbuf_space has been over-credited after an SMC-R
link failover or through SMC-D DMB merge. It also rewrites the
sndbuf_space invariant comments in smc_cdc_tx_handler() and
smc_tx_sendmsg().

> diff --git a/net/smc/smc_cdc.c b/net/smc/smc_cdc.c
> index e2c765ff2ba35..90a79a983b485 100644
> --- a/net/smc/smc_cdc.c
> +++ b/net/smc/smc_cdc.c
> @@ -43,7 +43,7 @@ static void smc_cdc_tx_handler(struct smc_wr_tx_pend_priv *pnd_snd,
>  		/* sndbuf_space is decreased in smc_sendmsg */
>  		smp_mb__before_atomic();
>  		atomic_add(diff, &cdcpend->conn->sndbuf_space);
> -		/* guarantee 0 <= sndbuf_space <= sndbuf_desc->len */
> +		/* can be out of range, see smc_tx_sendmsg() */

[Severity: Low]
The comment is updated here and after the atomic_sub() in
smc_tx_sendmsg(). Two other sndbuf_space credit sites in smc_cdc.c still
have the old wording.

The dmb_nocopy branch of smc_cdc_msg_recv_action():

    atomic_add(diff_tx, &conn->sndbuf_space);
    /* guarantee 0 <= sndbuf_space <= sndbuf_desc->len */

and smcd_cdc_msg_send():

    atomic_add(diff, &conn->sndbuf_space);
    /* guarantee 0 <= sndbuf_space <= sndbuf_desc->len */

The new comment in smc_tx_sendmsg() and the commit message both list the
DMB merge path as a way for sndbuf_space to go out of range ("with SMC-D
DMB merge it follows the peer's consumer cursor"). That makes the comment
in smc_cdc_msg_recv_action() contradict them.

Should these two comments be updated too? As far as I can see, no later
patch in the series touches smc_cdc.c, so both are still there at the
end of the series.

>  		smp_mb__after_atomic();
>  		smc_curs_copy(&conn->tx_curs_fin, &cdcpend->cursor, conn);
>  		smc_curs_copy(&conn->local_tx_ctrl_fin, &cdcpend->p_cursor,

[Severity: High]
This is a pre-existing issue and was not introduced by this patch. Is
the failover catch-up in smc_switch_cursor() serialized against this
handler at all?

smc_switch_conns() holds only the send_lock:

    spin_lock_bh(&conn->send_lock);
    smc_switch_link_and_count(conn, to_lnk);
    rc = smc_switch_cursor(smc, pend, wr_buf);
    spin_unlock_bh(&conn->send_lock);

smc_cdc_tx_handler() runs from the old link's CQ tasklet and holds only
bh_lock_sock(&smc->sk). Both read tx_curs_fin and local_tx_ctrl_fin, and
both credit conn->sndbuf_space.

Can this interleaving credit the same bytes twice?

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

CPU2 smc_cdc_tx_handler() (late completion from the old link)
    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);

CPU1
    diff = smc_curs_diff(conn->peer_rmbe_size, &fin, &cons);
    ...
    atomic_add(diff, &conn->sndbuf_space);

CPU1 computes diff from its stale fin snapshot, so it credits again the
bytes that CPU2 already credited.

In this commit, smc_switch_cursor() also updates tx_curs_fin and
local_tx_ctrl_fin with a plain read-modify-write smc_curs_add(), and
that can tear against the handler's smc_curs_copy(). The later patch in
this series, "net/smc: bound the link failover cursor advance to the
send buffer", replaces those adds with smc_curs_copy() and fixes the
tearing. Its commit message says the double credit remains:

  "The switch is still not serialized against the handler: a completion
  handled inside it is still credited more than once and can make the
  next write hand the peer bytes it has already read."

The result is an inflated sndbuf_space. The clamp in smc_tx_sendmsg()
only bounds it where it is used.

> diff --git a/net/smc/smc_tx.c b/net/smc/smc_tx.c
> index 0f0c6df3bf2ce..3913ef103b356 100644
> --- a/net/smc/smc_tx.c
> +++ b/net/smc/smc_tx.c
> @@ -233,6 +233,14 @@ int smc_tx_sendmsg(struct smc_sock *smc, struct msghdr *msg, size_t len)
>  		/* initialize variables for 1st iteration of subsequent loop */
>  		/* could be just 1 byte, even after smc_tx_wait above */
>  		writespace = atomic_read(&conn->sndbuf_space);
> +		/* sndbuf_space can be out of range: late completions of an
> +		 * old SMC-R link credit it again after a failover, and with
> +		 * SMC-D DMB merge it follows the peer's consumer cursor.
> +		 * Bound writespace, which sizes the copy; a negative value
> +		 * would become huge in the size_t min_t() below.
> +		 */
> +		if (writespace < 0 || writespace > conn->sndbuf_desc->len)
> +			writespace = conn->sndbuf_desc->len;

[Severity: High]
This isn't a bug introduced by this patch, but with writespace clamped
only to sndbuf_desc->len, can the send ring still overwrite data that
has not been sent or acknowledged?

The clamp limits the copy to the size of the allocation. It does not
limit it to the space that is actually free between tx_curs_fin and
tx_curs_prep.

Suppose sndbuf_space holds S > len. Each loop iteration copies up to len
bytes starting at tx_curs_prep.count, then does:

    atomic_sub(copylen, &conn->sndbuf_space);

That leaves S - len > 0, so this check stops neither the loop nor later
sendmsg() calls:

    if (!atomic_read(&conn->sndbuf_space) || conn->urg_tx_pend) {

A few consequences seem to follow.

First, memcpy_from_msg() overwrites [tx_curs_fin, tx_curs_prep). That
range holds bytes that are prepared but not yet sent and, for SMC-R,
bytes still being read by posted RDMA WRITEs. With SMC-D dmb_nocopy the
range is the peer's DMB. Either way, the peer silently receives the
wrong data.

Second, tx_curs_prep can get more than one ring ahead of tx_curs_sent.
smc_curs_diff() handles only a single wrap:

    if (old->wrap != new->wrap)
        return max_t(int, 0,
                     ((size - old->count) + new->count));

so to_send in smc_tx_rdma_writes() comes out wrong, and bytes get
duplicated or lost. When peer_rmbe_size > sndbuf_desc->len, a single
write can carry more than sndbuf_desc->len bytes. The source reads stay
in bounds because the second source chunk is at most prep.count, which
is less than len.

Third, smc_tx_should_cork() pushes only once sndbuf_space reaches 0:

    if ((msg->msg_flags & MSG_MORE ||
         smc_tx_is_corked(smc)) &&
        atomic_read(&conn->sndbuf_space))
        return true;

With MSG_MORE or TCP_CORK set and an inflated value, corked data can be
overwritten before it is flushed.

A conforming peer can still trigger the over-credit at the end of the
series. A late completion from the old link in smc_cdc_tx_handler()
does smc_curs_copy(&conn->tx_curs_fin, &cdcpend->cursor, conn), which
moves fin back. The next completion then credits the same bytes again.
The commit message acknowledges this: "send() can still overwrite data
in the send buffer that the peer has not received".

Could writespace also be bounded by len minus the bytes outstanding
between tx_curs_fin and tx_curs_prep? Or is a follow-up planned that
fixes the credit in smc_cdc_tx_handler()?

>  		/* not more than what user space asked for */
>  		copylen = min_t(size_t, send_remaining, writespace);
>  		/* determine start of sndbuf */

[ ... ]

-- 
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261112-b4-disp-0287c16e-v9-0-e5e8fa688177%40proton.me

  reply	other threads:[~2026-11-16 13:00 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-12 12:59 [PATCH net v9 0/6] net/smc: bound wire-controlled CDC cursors against the local buffers Bryam Vargas via B4 Relay
2026-11-12 12:59 ` [PATCH net v9 1/6] net/smc: bound the wire-controlled producer cursor to the RMB Bryam Vargas via B4 Relay
2026-11-12 12:59 ` [PATCH net v9 2/6] net/smc: bound the receive length " Bryam Vargas via B4 Relay
2026-11-16 13:00   ` netdev-bot+sashiko
2026-11-12 12:59 ` [PATCH net v9 3/6] net/smc: bound the send length to the send buffer in smc_tx_sendmsg() Bryam Vargas via B4 Relay
2026-11-16 13:00   ` netdev-bot+sashiko [this message]
2026-11-12 12:59 ` [PATCH net v9 4/6] net/smc: keep the source of an RDMA write inside the send buffer Bryam Vargas via B4 Relay
2026-11-12 12:59 ` [PATCH net v9 5/6] net/smc: keep an RDMA write inside the peer's RMBE Bryam Vargas via B4 Relay
2026-11-16 13:00   ` netdev-bot+sashiko
2026-11-12 12:59 ` [PATCH net v9 6/6] net/smc: bound the link failover cursor advance to the send buffer Bryam Vargas via B4 Relay
2026-11-16 13:00   ` netdev-bot+sashiko

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=179483403722.3136.1364122923722036240@kernel.org \
    --to=netdev-bot+sashiko@kernel.org \
    --cc=alibuda@linux.alibaba.com \
    --cc=davem@davemloft.net \
    --cc=dust.li@linux.alibaba.com \
    --cc=edumazet@kernel.org \
    --cc=guangguan.wang@linux.alibaba.com \
    --cc=guwen@linux.alibaba.com \
    --cc=hexlabsecurity@proton.me \
    --cc=hidayath@linux.ibm.com \
    --cc=horms@kernel.org \
    --cc=kuba@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-rdma@vger.kernel.org \
    --cc=linux-s390@vger.kernel.org \
    --cc=mjambigi@linux.ibm.com \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    --cc=raspl@linux.ibm.com \
    --cc=sidraya@linux.ibm.com \
    --cc=tonylu@linux.alibaba.com \
    --cc=ubraun@linux.ibm.com \
    --cc=wenjia@linux.ibm.com \
    /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