All of lore.kernel.org
 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: bound the DMB-nocopy send-buffer credit by what was sent
Date: Wed, 25 Nov 2026 18:03:13 +0000	[thread overview]
Message-ID: <sashiko-outbox-233892@kernel.org> (raw)
In-Reply-To: <20261124180257.622064-1-piyus@iiitd.ac.in>

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] net/smc: torn cursor read and non-atomic update in
  smc_cdc_msg_recv_action()
--

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

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

This commit bounds the send-buffer credit on the SMC-D DMB-nocopy receive
path by the unconfirmed sent bytes between tx_curs_fin and tx_curs_sent,
and advances tx_curs_fin by the bounded difference with smc_curs_add().

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..ecac93023d327 100644
> --- a/net/smc/smc_cdc.c
> +++ b/net/smc/smc_cdc.c
> @@ -333,6 +333,7 @@ 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;
> +	int unconfirmed;
>  
>  	smc_curs_copy(&prod_old, &conn->local_rx_ctrl.prod, conn);
>  	smc_curs_copy(&cons_old, &conn->local_rx_ctrl.cons, conn);
> @@ -361,13 +362,34 @@ static void smc_cdc_msg_recv_action(struct smc_sock *smc,
>  			diff_tx = smc_curs_diff(conn->sndbuf_desc->len,
>  						&conn->tx_curs_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,
> +						    &conn->tx_curs_fin,
> +						    &conn->tx_curs_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);
> +			smc_curs_add(conn->sndbuf_desc->len,
> +				     &conn->tx_curs_fin, diff_tx);
>  
>  			smc_tx_sndbuf_nonfull(smc);
>  		}

[Severity: Medium]
Can passing &conn->tx_curs_sent directly to smc_curs_diff() tear across its
wrap and count fields while smc_tx_rdma_writes() updates conn->tx_curs_sent?

smc_cdc_msg_recv() runs in tasklet context under bh_lock_sock(&smc->sk)
without conn->send_lock, while process-context smc_tx_sendmsg() or
smc_tx_work() holds lock_sock(&smc->sk) (which releases sk_lock.slock) and
conn->send_lock when advancing conn->tx_curs_sent via smc_curs_copy():

CPU0 (process)                    CPU1 (tasklet)
smc_tx_sendmsg()                  smcd_cdc_rx_tsklet()
  lock_sock(&smc->sk)               ...
  ...                               bh_lock_sock(&smc->sk)
  smc_tx_rdma_writes()              smc_cdc_msg_recv_action()
                                      smc_curs_diff(..., &conn->tx_curs_sent)
                                        reads tx_curs_sent.wrap (old W)
    smc_curs_add(len, &sent, ...)
      /* wraps: wrap = W + 1,
       * count = C_new < fin.count
       */
    smc_curs_copy(&conn->tx_curs_sent,
                  &sent, conn)
                                        reads tx_curs_sent.count (C_new)
                                        /* old->wrap == new->wrap,
                                         * C_new < fin.count -> returns 0
                                         */  <-- torn read

If smc_curs_diff() observes the pre-wrap wrap counter with the post-wrap
count, unconfirmed evaluates to 0 and clamps diff_tx to 0.  Because
conn->local_rx_ctrl.cons has already been updated to the new consumer
cursor, the credit is lost andconn->tx_curs_fin is not advanced, which can
stall a sender waiting for send-buffer space if no further consumer updates
arrive.  Conversely, if the plain loads are reordered (or on
!KERNEL_HAS_ATOMIC64 systems where conn->acurs_lock is bypassed) so that
the post-wrap wrap counter is paired with the pre-wrap count, unconfirmed
exceeds conn->sndbuf_desc->len and defeats the credit bound.

In addition, replacing smc_curs_copy() on conn->tx_curs_fin with in-place
smc_curs_add() updates tx_curs_fin.count and tx_curs_fin.wrap with
non-atomic stores (transiently writing count >= sndbuf_desc->len before
wrapping) rather than an atomic 64-bit cursor write.

Other unlocked readers such as smc_tx_prepared_sends() snapshot the cursor
into a local union smc_host_cursor via smc_curs_copy() before calling
smc_curs_diff(), and advancing a local copy of tx_curs_fin before writing
it back with smc_curs_copy() would keep the update atomic as well.

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

  reply	other threads:[~2026-11-25 18:03 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-24 18:02 [PATCH net v3] net/smc: bound the DMB-nocopy send-buffer credit by what was sent Piyus Kedia
2026-11-25 18:03 ` sashiko-bot [this message]
2026-11-26 15:06   ` Piyus Kedia
2026-11-28 18:17 ` 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=sashiko-outbox-233892@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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.