net/smc: serialize clcsock release against CDC receive path
Subsystems (5)
PATCH v2 1/1 net/smc: serialize clcsock release against CDC receive path
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 = 1 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.
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.
---
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 ++- 2 files changed, 4 insertions(+), 3 deletions(-)
net/smc/af_smc.c +2 -2
@@ -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;
net/smc/smc_cdc.c +2 -1
@@ -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 (!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 */ }
Signature · 1 line
2.50.1 (Apple Git-155)
[email protected]
AI reviewSashiko has reviewed this patch and found no issues.
Quoted 18 lines
In
smc_cdc_msg_recv_action(), receiving a close or senddone flag writesRCV_SHUTDOWNtosmc->clcsock->sk->sk_shutdownwhile holding onlybh_lock_sock(&smc->sk). Meanwhile__smc_release()drops the socket lock and callssmc_clcsock_release()(which clearssmc->clcsockand callssock_release()underclcsock_release_lock) before callingsmc_conn_free(&smc->conn). Even on paths that callsmc_conn_free()first, an in-flight SMC-R softirq that already looked up the connection insmc_cdc_rx_handler()can still entersmc_cdc_msg_recv()concurrently withsmc_clcsock_release()and dereference or write through a freed clcsock.Free the SMC connection before releasing clcsock in
__smc_release(), matchingsmc_close_active_abort()andsmc_close_passive_work(). Becausesmc_conn_free()setsconn->freed= 1 whilelock_sock(sk)is held andrelease_sock(sk)synchronizes onsk->sk_lock.slockbeforesmc_clcsock_release()runs, checking !smc->conn.freed underbh_lock_sock(&smc->sk)insmc_cdc_msg_recv()guarantees that no CDC handler accessessmc->clcsockoncesmc_clcsock_release()can start.
Quoted 7 lines
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 b38d732477e4 ("smc: socket closing and linkgroup cleanup")Signed-off-by Qican Ma <[email protected]>
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1