mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Sidraya Jayagond <sidraya@linux.ibm.com>
To: hexlabsecurity@proton.me,
	Mahanta Jambigi <mjambigi@linux.ibm.com>,
	"D. Wythe" <alibuda@linux.alibaba.com>,
	Eric Dumazet <edumazet@kernel.org>,
	"David S. Miller" <davem@davemloft.net>,
	Paolo Abeni <pabeni@redhat.com>, Jakub Kicinski <kuba@kernel.org>,
	Dust Li <dust.li@linux.alibaba.com>
Cc: Stefan Raspl <raspl@linux.ibm.com>,
	Simon Horman <horms@kernel.org>,
	Guangguan Wang <guangguan.wang@linux.alibaba.com>,
	Wen Gu <guwen@linux.alibaba.com>,
	linux-s390@vger.kernel.org, linux-rdma@vger.kernel.org,
	netdev@vger.kernel.org, linux-kernel@vger.kernel.org,
	Tony Lu <tonylu@linux.alibaba.com>,
	Ursula Braun <ubraun@linux.ibm.com>,
	Hidayath Khan <hidayath@linux.ibm.com>,
	Wenjia Zhang <wenjia@linux.ibm.com>
Subject: Re: [PATCH net v2 6/6] net/smc: update the urgent state under the socket spinlock
Date: Tue, 1 Dec 2026 15:02:23 +0530	[thread overview]
Message-ID: <cba71de0-4d7e-4bb2-8e08-ed7ecf0883f4@linux.ibm.com> (raw)
In-Reply-To: <20261128-b4-disp-360e65c5-v2-6-d9c05004d430@proton.me>



On 28/11/26 7:53 pm, Bryam Vargas via B4 Relay wrote:
> From: Bryam Vargas <hexlabsecurity@proton.me>
> 
> smc_cdc_msg_recv_action() sets a new urgent mark, byte and state under
> the socket spinlock, even while a reader owns the socket, and the
> reader tests and changes urg_state under the socket lock only. In
> smc_rx_recv_urg() a second urgent byte that arrives after the state was
> set to SMC_URG_READ is returned in place of the first, and the next
> recv(MSG_OOB) returns it again. In smc_rx_update_consumer() a mark set
> after the test is overwritten with SMC_URG_READ: its byte is neither
> returned nor skipped, and the sender, which waits for its urgent data
> to be consumed, stalls.
> 
> Take the socket spinlock around both test-and-set sequences.
> smc_rx_update_consumer() takes it only after a test without the lock
> finds a mark or a pending skip, so a read with no urgent data is
> unchanged. The lines of smc_rx_recv_urg() after its SMC_URG_VALID block
> could only be reached through this race; drop them.
> 
> Fixes: de8474eb9d50 ("net/smc: urgent data support")
> Reported-by: Sashiko <sashiko-bot@kernel.org>
> Closes: https://lore.kernel.org/all/sashiko-outbox-215220@kernel.org/
> Cc: stable@vger.kernel.org
> Signed-off-by: Bryam Vargas <hexlabsecurity@proton.me>
> ---
>  net/smc/smc_rx.c | 102 +++++++++++++++++++++++++++++++++----------------------
>  1 file changed, 62 insertions(+), 40 deletions(-)
> 
> diff --git a/net/smc/smc_rx.c b/net/smc/smc_rx.c
> index 564b976426ca..2680b91e882b 100644
> --- a/net/smc/smc_rx.c
> +++ b/net/smc/smc_rx.c
> @@ -67,26 +67,38 @@ static int smc_rx_update_consumer(struct smc_sock *smc,
>  
>  	smc_curs_add(conn->rmb_desc->len, &cons, len);
>  
> -	/* did we process urgent data? */
> +	/* did we process urgent data? smc_cdc_msg_recv_action() sets a new
> +	 * mark under the socket spinlock, even while we own the socket:
> +	 * test again under it. A mark set after the first test is left to
> +	 * the next call, like one set after the unlock.
> +	 */
>  	if (conn->urg_state == SMC_URG_VALID || conn->urg_rx_skip_pend) {
> -		diff = smc_curs_sdiff(conn->rmb_desc->len, &cons,
> -				      &conn->urg_curs);
> -		if (sock_flag(sk, SOCK_URGINLINE)) {
> -			if (diff == 0) {
> -				force = true;
> -				rc = 1;
> -				conn->urg_state = SMC_URG_READ;
> +		local_bh_disable();
> +		bh_lock_sock(sk);
> +		if (conn->urg_state == SMC_URG_VALID ||
> +		    conn->urg_rx_skip_pend) {
> +			diff = smc_curs_sdiff(conn->rmb_desc->len, &cons,
> +					      &conn->urg_curs);
> +			if (sock_flag(sk, SOCK_URGINLINE)) {
> +				if (diff == 0) {
> +					force = true;
> +					rc = 1;
> +					conn->urg_state = SMC_URG_READ;
> +				}
> +			} else {
> +				if (diff == 1) {
> +					/* skip urgent byte */
> +					force = true;
> +					smc_curs_add(conn->rmb_desc->len,
> +						     &cons, 1);
> +					conn->urg_rx_skip_pend = false;
> +				} else if (diff < -1)
> +					/* we read past urgent byte */
> +					conn->urg_state = SMC_URG_READ;
>  			}
> -		} else {
> -			if (diff == 1) {
> -				/* skip urgent byte */
> -				force = true;
> -				smc_curs_add(conn->rmb_desc->len, &cons, 1);
> -				conn->urg_rx_skip_pend = false;
> -			} else if (diff < -1)
> -				/* we read past urgent byte */
> -				conn->urg_state = SMC_URG_READ;
>  		}
> +		bh_unlock_sock(sk);
> +		local_bh_enable();
>  	}
>  
>  	smc_curs_copy(&conn->local_tx_ctrl.cons, &cons, conn);
> @@ -314,42 +326,52 @@ static int smc_rx_recv_urg(struct smc_sock *smc, struct msghdr *msg, int len,
>  	struct smc_connection *conn = &smc->conn;
>  	union smc_host_cursor cons;
>  	struct sock *sk = &smc->sk;
> +	bool valid;
> +	char byte;
>  	int rc = 0;
>  
> -	if (sock_flag(sk, SOCK_URGINLINE) ||
> -	    !(conn->urg_state == SMC_URG_VALID) ||
> -	    conn->urg_state == SMC_URG_READ)
> +	if (sock_flag(sk, SOCK_URGINLINE))
>  		return -EINVAL;
>  
> -	SMC_STAT_INC(smc, urg_data_cnt);
> -	if (conn->urg_state == SMC_URG_VALID) {
> -		if (!(flags & MSG_PEEK))
> -			smc->conn.urg_state = SMC_URG_READ;
> -		msg->msg_flags |= MSG_OOB;
> +	/* smc_cdc_msg_recv_action() sets a new mark and byte under the
> +	 * socket spinlock, even while we own the socket: take the byte
> +	 * together with the state it belongs to.
> +	 */
> +	local_bh_disable();
> +	bh_lock_sock(sk);
> +	valid = conn->urg_state == SMC_URG_VALID;
> +	if (valid) {
> +		byte = conn->urg_rx_byte;
>  		if (len > 0) {
> -			if (!(flags & MSG_TRUNC))
> -				rc = memcpy_to_msg(msg, &conn->urg_rx_byte, 1);
> -			len = 1;
>  			smc_curs_copy(&cons, &conn->local_tx_ctrl.cons, conn);
>  			if (smc_curs_sdiff(conn->rmb_desc->len, &cons,
>  					   &conn->urg_curs) > 1)
>  				conn->urg_rx_skip_pend = true;
> -			/* Urgent Byte was already accounted for, but trigger
> -			 * skipping the urgent byte in non-inline case
> -			 */
> -			if (!(flags & MSG_PEEK))
> -				smc_rx_update_consumer(smc, cons, 0);
> -		} else {
> -			msg->msg_flags |= MSG_TRUNC;
>  		}
> -
> -		return rc ? -EFAULT : len;
> +		if (!(flags & MSG_PEEK))
> +			conn->urg_state = SMC_URG_READ;
>  	}
> +	bh_unlock_sock(sk);
> +	local_bh_enable();
> +	if (!valid)
> +		return -EINVAL;
>  
> -	if (sk->sk_state == SMC_CLOSED || sk->sk_shutdown & RCV_SHUTDOWN)
> -		return 0;
> +	SMC_STAT_INC(smc, urg_data_cnt);
> +	msg->msg_flags |= MSG_OOB;
> +	if (len > 0) {
> +		if (!(flags & MSG_TRUNC))
> +			rc = memcpy_to_msg(msg, &byte, 1);
> +		len = 1;
> +		/* Urgent Byte was already accounted for, but trigger
> +		 * skipping the urgent byte in non-inline case
> +		 */
> +		if (!(flags & MSG_PEEK))
> +			smc_rx_update_consumer(smc, cons, 0);
> +	} else {
> +		msg->msg_flags |= MSG_TRUNC;
> +	}
>  
> -	return -EAGAIN;
> +	return rc ? -EFAULT : len;
>  }
>  
>  static bool smc_rx_recvmsg_data_available(struct smc_sock *smc, size_t peeked)
> 

The locking looks right to me. One thing, though: since this block is
being rewritten anyway, the "> 1" here is the bug in the last item of
your "Not fixed here", and it is not hard to hit.

If the urgent byte is the only unread byte, e.g. after a one-byte
send(MSG_OOB) to a reader that has drained everything, cons is
urg_curs - 1 and the distance is 1. skip_pend stays false and
urg_state becomes SMC_URG_READ, so the gate in smc_rx_update_consumer()
is false and the byte is never skipped. smc_rx_recvmsg_data_available()
only skips while the state is SMC_URG_VALID. The sender keeps
urg_tx_pend set because peer_rmbe_space never gets back to
peer_rmbe_size, and its next send() waits. That is just the normal
poll(POLLPRI) + recv(MSG_OOB) pattern, and it matches your own test
in the cover letter. No race or wrap is needed.

Changing the test to "> 0" fixes that case. smc_rx_update_consumer()
then sees skip_pend and takes its diff == 1 branch.

A zero-length recv(MSG_OOB) still sets SMC_URG_READ, but it never
computes skip_pend or calls smc_rx_update_consumer(), so it leaves
the byte behind at any distance. Doing both outside the len > 0
check would cover that as well.

Could you add this as a follow-up patch with the same Fixes tag? It's
not a reason to hold this one.

Reviewed-by: Sidraya Jayagond <sidraya@linux.ibm.com>

Thanks,
Sidraya

      reply	other threads:[~2026-12-01  9:32 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-28 14:23 [PATCH net v2 0/6] net/smc: fix the failover cursor switch and wrap rollover Bryam Vargas via B4 Relay
2026-11-28 14:23 ` [PATCH net v2 1/6] net/smc: take the socket spinlock across the failover cursor switch Bryam Vargas via B4 Relay
2026-12-01  8:47   ` Sidraya Jayagond
2026-11-28 14:23 ` [PATCH net v2 2/6] net/smc: do not credit stale CDC completions after a link switch Bryam Vargas via B4 Relay
2026-12-01  9:00   ` Sidraya Jayagond
2026-11-28 14:23 ` [PATCH net v2 3/6] net/smc: store each cursor once in smc_switch_cursor() Bryam Vargas via B4 Relay
2026-12-01  9:03   ` Sidraya Jayagond
2026-11-28 14:23 ` [PATCH net v2 4/6] net/smc: fix the failover catch-up across the cursor wrap rollover Bryam Vargas via B4 Relay
2026-12-01  9:08   ` Sidraya Jayagond
2026-11-28 14:23 ` [PATCH net v2 5/6] net/smc: order the urgent mark " Bryam Vargas via B4 Relay
2026-12-01  8:18   ` Sidraya Jayagond
2026-11-28 14:23 ` [PATCH net v2 6/6] net/smc: update the urgent state under the socket spinlock Bryam Vargas via B4 Relay
2026-12-01  9:32   ` Sidraya Jayagond [this message]

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=cba71de0-4d7e-4bb2-8e08-ed7ecf0883f4@linux.ibm.com \
    --to=sidraya@linux.ibm.com \
    --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=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

all inboxes | Powered by JetHome®