* [PATCH net v4] net/smc: serialize clcsock release against CDC receive path
@ 2026-12-04 2:20 Qican Ma
2026-12-04 6:36 ` Mahanta Jambigi
0 siblings, 1 reply; 2+ messages in thread
From: Qican Ma @ 2026-12-04 2:20 UTC (permalink / raw)
To: alibuda, dust.li, sidraya, mjambigi
Cc: tonylu, guwen, davem, edumazet, kuba, pabeni, horms, willemb,
linux-rdma, linux-s390, netdev
In smc_cdc_msg_recv_action(), receiving a close or senddone flag writes
RCV_SHUTDOWN to smc->clcsock->sk->sk_shutdown while holding only
bh_lock_sock(&smc->sk). Meanwhile __smc_release() drops the socket lock
and calls smc_clcsock_release() (which clears smc->clcsock and calls
sock_release() under clcsock_release_lock) before calling
smc_conn_free(&smc->conn). Even on paths that call smc_conn_free() first,
an in-flight SMC-R softirq that already looked up the connection in
smc_cdc_rx_handler() can still enter smc_cdc_msg_recv() concurrently with
smc_clcsock_release() and dereference or write through a freed clcsock.
Free the SMC connection before releasing clcsock in __smc_release(),
matching smc_close_active_abort() and smc_close_passive_work(). Because
smc_conn_free() sets conn->freed while lock_sock(sk) is held and
release_sock(sk) synchronizes on sk->sk_lock.slock before
smc_clcsock_release() runs, checking !smc->conn.freed under
bh_lock_sock(&smc->sk) in smc_cdc_msg_recv() guarantees that no CDC
handler accesses smc->clcsock once smc_clcsock_release() can start.
The send path has the same class of race: smc_tx_is_corked() dereferences
smc->clcsock under lock_sock(), and a sendmsg() that acquires the socket
lock in the release window can still observe a detached or freed clcsock.
Guard it on the same conn->freed check (plus a NULL check); the access is
race-free because both the writer and this reader run under lock_sock().
A dying connection is reported as corked so no further RDMA writes are
triggered while teardown is in progress.
The freed flag is written under lock_sock() but read under bh_lock_sock()
in softirq context, so the writer side and the smc_conn_create()
initializer use WRITE_ONCE() and the CDC reader uses READ_ONCE() to avoid
a KCSAN data race, as pointed out by Willem de Bruijn.
Found with an LLM-assisted code review tool and verified by inspection on
v7.4-rc4 (b6a9fa34ce4). Build-tested only; not runtime-tested due to lack
of an SMC-R/SMC-D setup.
Fixes: 46c28dbd4c23 ("net/smc: no socket state changes in tasklet context")
Cc: stable@vger.kernel.org
Signed-off-by: Qican Ma <goonsomeway@gmail.com>
---
v4: correct the Fixes: tag to 46c28dbd4c23, which introduced the
tasklet-context clcsock write, and extend the conn->freed guard to
the tx corking path, per Mahanta Jambigi's review.
v3: use WRITE_ONCE() for conn->freed in smc_conn_free() and
smc_conn_create(), READ_ONCE() for the CDC-side read, per Willem de
Bruijn's KCSAN data race review.
v2: resend via direct SMTP to fix line-wrapping corruption in v1;
verified clean git am on net/main both standalone and on top of
Bryam Vargas's v9 CDC cursor bounds series.
net/smc/af_smc.c | 4 ++--
net/smc/smc_cdc.c | 3 ++-
net/smc/smc_core.c | 4 ++--
net/smc/smc_tx.c | 6 +++++-
4 files changed, 11 insertions(+), 6 deletions(-)
diff --git a/net/smc/af_smc.c b/net/smc/af_smc.c
index bfc0c7e71a12..1d8a602729c1 100644
--- a/net/smc/af_smc.c
+++ b/net/smc/af_smc.c
@@ -313,13 +313,13 @@ static int __smc_release(struct smc_sock *smc)
sk->sk_prot->unhash(sk);
if (sk->sk_state == SMC_CLOSED) {
+ if (!smc->use_fallback)
+ smc_conn_free(&smc->conn);
if (smc->clcsock) {
release_sock(sk);
smc_clcsock_release(smc);
lock_sock(sk);
}
- if (!smc->use_fallback)
- smc_conn_free(&smc->conn);
}
return rc;
diff --git a/net/smc/smc_cdc.c b/net/smc/smc_cdc.c
index 32d6d03df321..34c654e6600b 100644
--- a/net/smc/smc_cdc.c
+++ b/net/smc/smc_cdc.c
@@ -428,7 +428,8 @@ static void smc_cdc_msg_recv(struct smc_sock *smc, struct smc_cdc_msg *cdc)
{
sock_hold(&smc->sk);
bh_lock_sock(&smc->sk);
- smc_cdc_msg_recv_action(smc, cdc);
+ if (!READ_ONCE(smc->conn.freed))
+ smc_cdc_msg_recv_action(smc, cdc);
bh_unlock_sock(&smc->sk);
sock_put(&smc->sk); /* no free sk in softirq-context */
}
diff --git a/net/smc/smc_core.c b/net/smc/smc_core.c
index a3164201c142..92efb1b457a9 100644
--- a/net/smc/smc_core.c
+++ b/net/smc/smc_core.c
@@ -1284,7 +1284,7 @@ void smc_conn_free(struct smc_connection *conn)
*/
return;
- conn->freed = 1;
+ WRITE_ONCE(conn->freed, 1);
if (!smc_conn_lgr_valid(conn))
/* Connection has already unregistered from
* link group.
@@ -2096,7 +2096,7 @@ int smc_conn_create(struct smc_sock *smc, struct smc_init_info *ini)
smc_lgr_hold(conn->lgr); /* lgr_put in smc_conn_free() */
if (!conn->lgr->is_smcd)
smcr_link_hold(conn->lnk); /* link_put in smc_conn_free() */
- conn->freed = 0;
+ WRITE_ONCE(conn->freed, 0);
conn->local_tx_ctrl.common.type = SMC_CDC_MSG_TYPE;
conn->local_tx_ctrl.len = SMC_WR_TX_SIZE;
conn->urg_state = SMC_URG_READ;
diff --git a/net/smc/smc_tx.c b/net/smc/smc_tx.c
index 0f0c6df3bf2c..a69809474df5 100644
--- a/net/smc/smc_tx.c
+++ b/net/smc/smc_tx.c
@@ -126,8 +126,12 @@ static int smc_tx_wait(struct smc_sock *smc, int flags)
static bool smc_tx_is_corked(struct smc_sock *smc)
{
- struct tcp_sock *tp = tcp_sk(smc->clcsock->sk);
+ struct tcp_sock *tp;
+ if (smc->conn.freed || !smc->clcsock)
+ return true;
+
+ tp = tcp_sk(smc->clcsock->sk);
return (tp->nonagle & TCP_NAGLE_CORK) ? true : false;
}
^ permalink raw reply related [flat|nested] 2+ messages in thread
* Re: [PATCH net v4] net/smc: serialize clcsock release against CDC receive path
2026-12-04 2:20 [PATCH net v4] net/smc: serialize clcsock release against CDC receive path Qican Ma
@ 2026-12-04 6:36 ` Mahanta Jambigi
0 siblings, 0 replies; 2+ messages in thread
From: Mahanta Jambigi @ 2026-12-04 6:36 UTC (permalink / raw)
To: Qican Ma, alibuda, dust.li, sidraya
Cc: tonylu, guwen, davem, edumazet, kuba, pabeni, horms, willemb,
linux-rdma, linux-s390, netdev
Hi Qican,
v4 fixes both accessors I flagged; the __smc_release() reorder and the
Fixes tag are right.
One thing: smc_tx_is_corked() returning true does not mean no RDMA write
happens. smc_tx_should_cork() also requires sndbuf_space, so with a full
sndbuf it still calls smc_tx_sndbuf_nonempty(), and the sndbuf copy
happens before the cork check. If the goal is only to avoid the clcsock
deref, reword that sentence; if it is to keep tx off a dying connection,
check earlier and return an error.
Thanks,
Mahanta
On 04/12/26 7:50 am, Qican Ma wrote:
> In smc_cdc_msg_recv_action(), receiving a close or senddone flag writes
> RCV_SHUTDOWN to smc->clcsock->sk->sk_shutdown while holding only
> bh_lock_sock(&smc->sk). Meanwhile __smc_release() drops the socket lock
> and calls smc_clcsock_release() (which clears smc->clcsock and calls
> sock_release() under clcsock_release_lock) before calling
> smc_conn_free(&smc->conn). Even on paths that call smc_conn_free() first,
> an in-flight SMC-R softirq that already looked up the connection in
> smc_cdc_rx_handler() can still enter smc_cdc_msg_recv() concurrently with
> smc_clcsock_release() and dereference or write through a freed clcsock.
>
> Free the SMC connection before releasing clcsock in __smc_release(),
> matching smc_close_active_abort() and smc_close_passive_work(). Because
> smc_conn_free() sets conn->freed while lock_sock(sk) is held and
> release_sock(sk) synchronizes on sk->sk_lock.slock before
> smc_clcsock_release() runs, checking !smc->conn.freed under
> bh_lock_sock(&smc->sk) in smc_cdc_msg_recv() guarantees that no CDC
> handler accesses smc->clcsock once smc_clcsock_release() can start.
>
> The send path has the same class of race: smc_tx_is_corked() dereferences
> smc->clcsock under lock_sock(), and a sendmsg() that acquires the socket
> lock in the release window can still observe a detached or freed clcsock.
> Guard it on the same conn->freed check (plus a NULL check); the access is
> race-free because both the writer and this reader run under lock_sock().
> A dying connection is reported as corked so no further RDMA writes are
> triggered while teardown is in progress.
>
> The freed flag is written under lock_sock() but read under bh_lock_sock()
> in softirq context, so the writer side and the smc_conn_create()
> initializer use WRITE_ONCE() and the CDC reader uses READ_ONCE() to avoid
> a KCSAN data race, as pointed out by Willem de Bruijn.
>
> Found with an LLM-assisted code review tool and verified by inspection on
> v7.4-rc4 (b6a9fa34ce4). Build-tested only; not runtime-tested due to lack
> of an SMC-R/SMC-D setup.
>
> Fixes: 46c28dbd4c23 ("net/smc: no socket state changes in tasklet context")
> Cc: stable@vger.kernel.org
> Signed-off-by: Qican Ma <goonsomeway@gmail.com>
> ---
> v4: correct the Fixes: tag to 46c28dbd4c23, which introduced the
> tasklet-context clcsock write, and extend the conn->freed guard to
> the tx corking path, per Mahanta Jambigi's review.
> v3: use WRITE_ONCE() for conn->freed in smc_conn_free() and
> smc_conn_create(), READ_ONCE() for the CDC-side read, per Willem de
> Bruijn's KCSAN data race review.
> v2: resend via direct SMTP to fix line-wrapping corruption in v1;
> verified clean git am on net/main both standalone and on top of
> Bryam Vargas's v9 CDC cursor bounds series.
>
> net/smc/af_smc.c | 4 ++--
> net/smc/smc_cdc.c | 3 ++-
> net/smc/smc_core.c | 4 ++--
> net/smc/smc_tx.c | 6 +++++-
> 4 files changed, 11 insertions(+), 6 deletions(-)
>
> diff --git a/net/smc/af_smc.c b/net/smc/af_smc.c
> index bfc0c7e71a12..1d8a602729c1 100644
> --- a/net/smc/af_smc.c
> +++ b/net/smc/af_smc.c
> @@ -313,13 +313,13 @@ static int __smc_release(struct smc_sock *smc)
> sk->sk_prot->unhash(sk);
>
> if (sk->sk_state == SMC_CLOSED) {
> + if (!smc->use_fallback)
> + smc_conn_free(&smc->conn);
> if (smc->clcsock) {
> release_sock(sk);
> smc_clcsock_release(smc);
> lock_sock(sk);
> }
> - if (!smc->use_fallback)
> - smc_conn_free(&smc->conn);
> }
>
> return rc;
> diff --git a/net/smc/smc_cdc.c b/net/smc/smc_cdc.c
> index 32d6d03df321..34c654e6600b 100644
> --- a/net/smc/smc_cdc.c
> +++ b/net/smc/smc_cdc.c
> @@ -428,7 +428,8 @@ static void smc_cdc_msg_recv(struct smc_sock *smc, struct smc_cdc_msg *cdc)
> {
> sock_hold(&smc->sk);
> bh_lock_sock(&smc->sk);
> - smc_cdc_msg_recv_action(smc, cdc);
> + if (!READ_ONCE(smc->conn.freed))
> + smc_cdc_msg_recv_action(smc, cdc);
> bh_unlock_sock(&smc->sk);
> sock_put(&smc->sk); /* no free sk in softirq-context */
> }
> diff --git a/net/smc/smc_core.c b/net/smc/smc_core.c
> index a3164201c142..92efb1b457a9 100644
> --- a/net/smc/smc_core.c
> +++ b/net/smc/smc_core.c
> @@ -1284,7 +1284,7 @@ void smc_conn_free(struct smc_connection *conn)
> */
> return;
>
> - conn->freed = 1;
> + WRITE_ONCE(conn->freed, 1);
> if (!smc_conn_lgr_valid(conn))
> /* Connection has already unregistered from
> * link group.
> @@ -2096,7 +2096,7 @@ int smc_conn_create(struct smc_sock *smc, struct smc_init_info *ini)
> smc_lgr_hold(conn->lgr); /* lgr_put in smc_conn_free() */
> if (!conn->lgr->is_smcd)
> smcr_link_hold(conn->lnk); /* link_put in smc_conn_free() */
> - conn->freed = 0;
> + WRITE_ONCE(conn->freed, 0);
> conn->local_tx_ctrl.common.type = SMC_CDC_MSG_TYPE;
> conn->local_tx_ctrl.len = SMC_WR_TX_SIZE;
> conn->urg_state = SMC_URG_READ;
> diff --git a/net/smc/smc_tx.c b/net/smc/smc_tx.c
> index 0f0c6df3bf2c..a69809474df5 100644
> --- a/net/smc/smc_tx.c
> +++ b/net/smc/smc_tx.c
> @@ -126,8 +126,12 @@ static int smc_tx_wait(struct smc_sock *smc, int flags)
>
> static bool smc_tx_is_corked(struct smc_sock *smc)
> {
> - struct tcp_sock *tp = tcp_sk(smc->clcsock->sk);
> + struct tcp_sock *tp;
>
> + if (smc->conn.freed || !smc->clcsock)
> + return true;
> +
> + tp = tcp_sk(smc->clcsock->sk);
> return (tp->nonagle & TCP_NAGLE_CORK) ? true : false;
> }
>
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-12-04 6:37 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-04 2:20 [PATCH net v4] net/smc: serialize clcsock release against CDC receive path Qican Ma
2026-12-04 6:36 ` Mahanta Jambigi
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox