From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.82.175 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796570205; b=dd+eqz44X4hM8JvJU7fnmW0sy0hX/glXekMPwaHs/eYXyL4nAO0bfPDig/lqwLIcfUTa IAKK9+1dd1qnR+T07UAO6yAsESy7D6rCJfTEHbNBYfYU2fSzDxnrlCfKJBo+VOr97yB4r 9B8xu9wJsHyWBIlIefnPANm63ThbpRRz2eDJ9+QFjo1bzWn92XXTRZIV7bMjr2pxyQgY0 qFyyxfF2i7m3tKW0T98W39YZsQ9LpTCk+lUShNZbJYvz7GLrDy1pRDr974kjX6hoYMS0c BQl7JCE8RHQm2LHKNGXWXm8GLZuI3E+tN6fPvLi9uco+wpsGIpRFuncWvn3mt/HX5Lw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796570205; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received:Received: From:To:Cc:Subject:Date:Message-ID:Content-Type: Content-Transfer-Encoding:MIME-Version; bh=xkVTxyN7bxJq+lXTV5uufJVzNBVba1pBOM+WAjOh6+w=; b=ADgQPwHRNKr+JuPErxNT67DXkjXhyC486BNRp7BUYObSE7bLY50XrkgCef1WV1cByZsY OtlfcSvrxmws3XhdvriJrGupIx6KXRcs53TDqESMdkkl49Ytid1H0WOZsOV7O/uvsYPWC DuUez/dMz7RPFfozLaRz8zpITDP0cBKWxGKTViexMOBb5umC+8oMwACF55h7ghYMZ6ELS TBJPm44gyCNkUfjhEVY9Pw1FlcXllUK2G+vO9AP6NaqDJSaPSPD+SmGfEwQ1/MQXObbiD eWoFV7dMgETPb2aA/cGU41KXOkUMnSPtU9CU9elcQyHNiYRQR8F0kRZ23dJxf2wqkDQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=Xq9uaiVl; arc=none smtp.remote-ip=74.125.82.175 Received: from mail-dy1-f175.google.com (mail-dy1-f175.google.com [74.125.82.175]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 24A88606A321 for ; Sun, 6 Dec 2026 15:16:44 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 24A88606A321 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=Xq9uaiVl Received: by mail-dy1-f175.google.com with SMTP id 5a478bee46e88-3282db206d3so2110557eec.0 for ; Sun, 06 Dec 2026 07:16:43 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796570203; x=1797175003; darn=vger.kernel.org; h=mime-version:content-transfer-encoding:content-type:message-id:date :subject:cc:to:from:from:to:cc:subject:date:message-id:reply-to :content-type; bh=xkVTxyN7bxJq+lXTV5uufJVzNBVba1pBOM+WAjOh6+w=; b=Xq9uaiVl6ZmO6zOhh3SKtvxY92+/JgdGGphlFY9ro1kiH9xVDDwKOWq+OOIU+pqopy hEamdBslqbglWc9ooEH4aKfeGyXEL2Z6CGOCfV1WRlIDn3NtMKx841IGx6WnpiZjEKpv Xxn21WPj/qhsontSIX8PksfS0xWjR3NOK3I7V57EqoQcLItJyvZtHhgplt9Ay3FmkK7P h2VYyU4f6yxnoHGgYOYg5781GLS/yZU0YhXuA2MzFNBjxE/QgeMnZYfaFVLPQsrXop1S TFtpj4wVuw+TBf/VUr3/UBeDxB8nOty0g30RLikhoiskpwTTzUX5Us0R5ZE35xM/qNhD /u3w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796570203; x=1797175003; h=mime-version:content-transfer-encoding:content-type:message-id:date :subject:cc:to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject :date:message-id:reply-to:content-type; bh=xkVTxyN7bxJq+lXTV5uufJVzNBVba1pBOM+WAjOh6+w=; b=HIDOa81tzMZPnPei43j+7+wQd4QpQaKG0UW4qbA3k8Ubdc1EYyQeAknPhP/laTJehd B2GoAi9mcN0lmSs6yCYgmZrOCtPH9xjWIH8u+Kev+TA2d1KSkmZO7OVAPmKS1f4Vge5O vkVN6Oqcd94ZSBRVpHsNQP2BOy3DiemJJzRF+7+QkTjEgg6YbMrVzVNej8LMVN7tChGP +dw8rxRXSU0lWtxfSKEoiinKnqEbAQ2nljg7XZv/3b8rfTRSgB1RzFGjJhFPJzxaW9Il u8kpzaPXqsOQpVvX2ZE2hBjsFl91yrl5d5BSiwerYQtNGHp2oLKpAIqyO8zHZ9BqsYVC zfNg== X-Forwarded-Encrypted: i=1; AL+hErFJPzA/oZHhzqkQ1pnG5M7N7wW14KgUdolwFPT92rqbEb3bLM/G6PanldpW45fRk9S/8gRYcUwXAf/a@vger.kernel.org X-Gm-Message-State: AB4q+uENojUdRTYSBEGBjWbi68arCm55/2oAKzDNxpK9tmQZcVRLAxC6 PAxLYPMBHqmPsB/RJdazl1mTZgP0AbHEbKhQC2TC0US6jAx308QfMPQO X-Gm-Gg: AZFdTZdSlWi3l/iFaU4MxGbPCGqdXsrmWYFiaAZSLsujKG//ap5axN2RvJX2HonftVk /ha7B1/FhSXTAnkN9alNGxyWoZUO4CiQfh0H7IQWoSUdf61zRReaP7uvlOpiC2b+v6v4uRq9ICi 1NjPtMc++dGo7hzNzTSzAs6cdGJHFQ99S3uFJQvAgoDla/ysB0E1WBOkxW8jQLkZO13ER8HV1YI LisZhtlsmpTn8sS0vOqYi1z1keuLC87XG4Ii59aUJsIYpJDxd1jqnKVJfi3W/chj/dtR3yqw9Lm RDi9xHAxpJ6QsrQVv+98Wiwz7NinED6+dxq2GAoeJdPVOWTDfyWdvcvqGF4UcAjmI52x44E8sSI /fdmQPyQOcJ3mWw6uxtwzBdcmkhXmT7a4ldyqmCOybLlsKmkh+OXAtBmpgipvMopmrlR99r7chN VEeWltd2kzT8tHbkSxf5dhAY6958W5dxMZ02vs0h89k5g/kUjfdHecCSFVMtUc/8o/WxkzFWpqz qW3ntUMVWgVX2monuC1lxCve7HbBMmrzX6b7KkiKLfxH91wLsBn93AefKW7KIAToNZu11ymSdkH 9xnQ6WfBPdiNS8r+3A== X-Received: by 2002:a05:7300:ff42:b0:377:678:2309 with SMTP id 5a478bee46e88-387521142a7mr7401489eec.15.1796570202960; Sun, 06 Dec 2026 07:16:42 -0800 (PST) Received: from CanCanNeed-2.local ([2001:df6:11c0:e720::1]) by smtp.gmail.com with ESMTPSA id 5a478bee46e88-3875218bc9fsm30180562eec.6.2026.12.06.07.16.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 06 Dec 2026 07:16:42 -0800 (PST) From: Qican Ma To: alibuda@linux.alibaba.com, dust.li@linux.alibaba.com, sidraya@linux.ibm.com, mjambigi@linux.ibm.com Cc: tonylu@linux.alibaba.com, guwen@linux.alibaba.com, davem@davemloft.net, edumazet@kernel.org, kuba@kernel.org, pabeni@redhat.com, horms@kernel.org, willemb@google.com, linux-rdma@vger.kernel.org, linux-s390@vger.kernel.org, netdev@vger.kernel.org Subject: [PATCH net v5] net/smc: serialize clcsock release against CDC receive path Date: Sun, 06 Dec 2026 23:16:32 +0800 Message-ID: <179657019280.87140.86612621177254460@gmail.com> Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit X-Mailing-List: linux-rdma@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 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. Moving smc_conn_free() ahead of the clcsock release also moves its cancel_work_sync(&conn->abort_work) ahead of the lock-drop window that previously gave a lock-starved abort_work a chance to complete. Since smc_conn_abort_work() itself takes lock_sock(), a work already blocked on the lock would deadlock against the cancel_wait. Cancel abort_work with the lock dropped first, mirroring smc_close_cancel_work(), and compensate the scheduler's sock_hold() when the work is dequeued before running, both here and inside smc_conn_free() where the same leak exists. The pre-existing deadlock pattern in the remaining smc_conn_free() callers is unchanged by this patch and deserves its own fix. The send path can observe the same detached or freed clcsock: smc_tx_is_corked() dereferences smc->clcsock under lock_sock(). Guard it on the same conn->freed check (plus a NULL check); the access itself is race-free because both the writer and this reader run under lock_sock(). This only avoids the clcsock dereference on a dying connection; it does not otherwise prevent transmit activity, which the existing EPIPE and conn->killed checks in smc_tx_sendmsg() continue to govern. 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 --- v5: cancel abort_work with the lock dropped before smc_conn_free() in __smc_release() so the reorder cannot tighten the pre-existing lock_sock()/cancel_work_sync() deadlock, and compensate the scheduler's sock_hold() when abort_work is canceled before running, per the sashiko review. Reword the tx corking paragraph: the guard only avoids the clcsock dereference, per Mahanta Jambigi's review. 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 | 15 +++++++++++++-- net/smc/smc_cdc.c | 3 ++- net/smc/smc_core.c | 10 ++++++---- net/smc/smc_tx.c | 6 +++++- 4 files changed, 26 insertions(+), 8 deletions(-) diff --git a/net/smc/af_smc.c b/net/smc/af_smc.c index bfc0c7e71a1..4fb326d56b2 100644 --- a/net/smc/af_smc.c +++ b/net/smc/af_smc.c @@ -313,13 +313,24 @@ static int __smc_release(struct smc_sock *smc) sk->sk_prot->unhash(sk); if (sk->sk_state == SMC_CLOSED) { + if (!smc->use_fallback) { + /* abort_work takes lock_sock() itself; cancel it with + * the lock dropped so that the cancel_work_sync() in + * smc_conn_free() cannot deadlock against it. The + * sock_put() compensates the sock_hold() of the + * scheduler when the work never ran. + */ + release_sock(sk); + if (cancel_work_sync(&smc->conn.abort_work)) + sock_put(sk); + lock_sock(sk); + 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 32d6d03df32..34c654e6600 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 a3164201c14..25104d85643 100644 --- a/net/smc/smc_core.c +++ b/net/smc/smc_core.c @@ -1276,6 +1276,7 @@ static void smc_buf_unuse(struct smc_connection *conn, /* remove a finished connection from its link group */ void smc_conn_free(struct smc_connection *conn) { + struct smc_sock *smc = container_of(conn, struct smc_sock, conn); struct smc_link_group *lgr = conn->lgr; if (!lgr || conn->freed) @@ -1284,7 +1285,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. @@ -1298,8 +1299,9 @@ void smc_conn_free(struct smc_connection *conn) smcd_buf_detach(conn); } else { smc_cdc_wait_pend_tx_wr(conn); - if (current_work() != &conn->abort_work) - cancel_work_sync(&conn->abort_work); + if (current_work() != &conn->abort_work && + cancel_work_sync(&conn->abort_work)) + sock_put(&smc->sk); } /* The handshake can retry another device or SMC version with this * connection, so it must never stay registered in an lgr it left. @@ -2096,7 +2098,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 0f0c6df3bf2..a69809474df 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; }