* Re: [PATCH net] net/smc: abort old connection when peer reuses RMB element
2026-11-29 0:17 [PATCH net] net/smc: abort old connection when peer reuses RMB element Andrew Stellman
@ 2026-11-30 0:17 ` sashiko-bot
2026-12-01 8:39 ` Mahanta Jambigi
1 sibling, 0 replies; 3+ messages in thread
From: sashiko-bot @ 2026-11-30 0:17 UTC (permalink / raw)
To: Andrew Stellman
Cc: linux-rdma, linux-s390, Heiko Carstens, Vasily Gorbik,
Alexander Gordeev, Christian Borntraeger
Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [High] net/smc: leak of lgr buffers and rkeys when smc_conn_kill()
aborts conn
- [Medium] net/smc: race in smc_listen_work() misses rtoken conflict
before peer_rmbe_idx set
- [Medium] net/smc: lockdep subclass and work-flush inversions in
smc_rtoken_resolve_conflict()
It also noticed 1 related pre-existing issue this patch does not
introduce:
- [Critical] net/smc: use-after-free in smc_conn_create() [1]
--
commit 4e5079bdaa97283bcbcfcf6f66e253a14973ca98
Author: Andrew Stellman <astellman@stellman-greene.com>
net/smc: abort old connection when peer reuses RMB element
This commit aborts any existing SMC-R connection in a link group whose
peer RMB rtoken and element index are reused by a later CLC handshake.
> diff --git a/net/smc/smc_core.c b/net/smc/smc_core.c
> index a3164201c1420..cb45c36cdce66 100644
> --- a/net/smc/smc_core.c
> +++ b/net/smc/smc_core.c
[ ... ]
> @@ -2024,6 +2024,7 @@ int smc_conn_create(struct smc_sock *smc, struct smc_init_info *ini)
> spinlock_t *lgr_lock;
> int rc = 0;
>
> + conn->rtoken_idx = -1;
> lgr_list = ini->is_smcd ? &ini->ism_dev[ini->ism_selected]->lgr_list :
> &smc_lgr_list.list;
> lgr_lock = ini->is_smcd ? &ini->ism_dev[ini->ism_selected]->lgr_lock :
[Severity: Critical]
This problem wasn't introduced by this patch, but can smc_conn_create() hit a
use-after-free on lgr [1] when smc_lgr_register_conn() fails concurrently
with link group teardown?
CPU0 CPU1
smc_conn_create()
smc_lgr_create()
// lgr list refcnt = 1
smc_smcr_terminate_all()
list_del_init(&lgr->list)
__smc_lgr_terminate(lgr, false)
write_lock_bh(&lgr->conns_lock)
smc_lgr_register_conn() // fails
write_unlock_bh(&lgr->conns_lock)
read_lock_bh(&lgr->conns_lock)
// 0 connections found
read_unlock_bh(&lgr->conns_lock)
smc_lgr_free(lgr)
kfree(lgr)
if (rc)
smc_lgr_cleanup_early(lgr) <-- use-after-free
smc_lgr_create() publishes lgr on lgr_list with only the list reference, and
smc_conn_create() does not call smc_lgr_hold() until after
smc_lgr_register_conn() succeeds:
net/smc/smc_core.c:smc_conn_create() {
...
if (ini->first_contact_local) {
rc = smc_lgr_create(smc, ini);
if (rc)
goto out;
lgr = conn->lgr;
write_lock_bh(&lgr->conns_lock);
rc = smc_lgr_register_conn(conn, true);
write_unlock_bh(&lgr->conns_lock);
if (rc) {
smc_lgr_cleanup_early(lgr);
goto out;
}
}
...
}
If teardown unlinks lgr while smc_lgr_register_conn() fails,
__smc_lgr_terminate() sees an empty conns_all tree and frees lgr before
smc_conn_create() runs smc_lgr_cleanup_early(lgr).
> @@ -2757,6 +2758,42 @@ int smc_rtoken_delete(struct smc_link *lnk, __be32 nw_rkey)
> return -ENOENT;
> }
>
> +/* Abort a connection if the peer reuses its RMB element for this handshake. */
> +static void smc_rtoken_resolve_conflict(struct smc_connection *conn, u8 rmbe_idx)
> +{
> + struct smc_link_group *lgr = conn->lgr;
> + struct smc_connection *old;
> + struct smc_sock *old_smc;
> + struct rb_node *node;
> +
> + for (;;) {
> + read_lock_bh(&lgr->conns_lock);
> + for (node = rb_first(&lgr->conns_all); node; node = rb_next(node)) {
> + old = rb_entry(node, struct smc_connection, alert_node);
> + if (old != conn && !old->freed &&
> + old->rtoken_idx >= 0 &&
> + old->rtoken_idx == conn->rtoken_idx &&
> + old->peer_rmbe_idx == rmbe_idx)
> + break;
> + }
[Severity: Medium]
Can a concurrent server handshake on smc_hs_wq miss a conflicting connection
here because peer_rmbe_idx is written only after smc_rmb_rtoken_handling() and
mutex_unlock(&smc_server_lgr_pending)?
CPU0 (smc_listen_work #1) CPU1 (smc_listen_work #2)
mutex_lock(&smc_server_lgr_pending)
smc_listen_rdma_finish()
smc_rmb_rtoken_handling()
old->rtoken_idx = idx
mutex_unlock(&smc_server_lgr_pending)
mutex_lock(&smc_server_lgr_pending)
smc_listen_rdma_finish()
smc_rmb_rtoken_handling()
smc_rtoken_resolve_conflict()
old->peer_rmbe_idx == rmbe_idx
// still 0, no match
smc_conn_save_peer_info()
old->peer_rmbe_idx = rmbe_idx
In smc_listen_work(), smc_listen_rdma_finish() sets new_smc->conn.rtoken_idx
and smc_server_lgr_pending is released before smc_conn_save_peer_info() stores
new_smc->conn.peer_rmbe_idx:
net/smc/af_smc.c:smc_listen_work() {
...
if (!ini->is_smcd) {
rc = smc_listen_rdma_finish(new_smc, cclc,
ini->first_contact_local, ini);
if (rc)
goto out_unlock;
mutex_unlock(&smc_server_lgr_pending);
}
smc_conn_save_peer_info(new_smc, cclc);
...
}
Because the rbtree scan under lgr->conns_lock checks old->peer_rmbe_idx before
taking old_smc->sk, the second server worker sees 0 instead of the 1-based
rmbe_idx and leaves both connections sharing the same peer RMB element.
> + if (!node) {
> + read_unlock_bh(&lgr->conns_lock);
> + return;
> + }
> + old_smc = container_of(old, struct smc_sock, conn);
> + sock_hold(&old_smc->sk);
> + read_unlock_bh(&lgr->conns_lock);
> +
> + lock_sock_nested(&old_smc->sk, SINGLE_DEPTH_NESTING);
[Severity: Medium]
Does locking old_smc->sk with SINGLE_DEPTH_NESTING and running
smc_close_active_abort() while the caller still holds lock_sock() on the new
socket invert the sk_lock-AF_SMC lockdep hierarchy and flush work items under
the outer socket lock?
CPU0 CPU1
---- ----
lock_sock_nested(listen_sk, 1)
lock_sock(new_sk)
lock_sock(child_sk)
lock_sock_nested(&old_smc->sk, 1)
*** DEADLOCK ***
CPU0: smc_release() -> __smc_release() -> smc_close_active() ->
smc_close_cleanup_listen() -> smc_close_non_accepted()
CPU1: smc_connect_rdma() / smc_listen_work() -> smc_rmb_rtoken_handling() ->
smc_rtoken_resolve_conflict()
In addition, smc_close_cancel_work(old_smc, true) and smc_clcsock_release()
only release old_smc->sk (subclass 1) while the caller still holds the new
socket lock (sk_lock-AF_SMC subclass 0) and smc_server_lgr_pending across
cancel_work_sync(&close_work), cancel_delayed_work_sync(&tx_work), and
cancel_work_sync(&smc_listen_work), whose callbacks acquire sk_lock-AF_SMC
subclass 0 (and smc_server_lgr_pending).
> + if (old->lgr == lgr && old->alert_token_local && !old->freed &&
> + old->rtoken_idx == conn->rtoken_idx &&
> + old->peer_rmbe_idx == rmbe_idx)
> + smc_conn_kill(old, true, true);
[Severity: High]
Does killing old on a live link group leak old->sndbuf_desc and old->rmb_desc
in lgr?
smc_conn_kill() calls smc_lgr_unregister_conn(old), which clears
old->alert_token_local = 0 without calling smc_buf_unuse(). When
smc_conn_free() later runs for old, smc_conn_lgr_valid(old) is already false,
so it jumps past smc_buf_unuse():
net/smc/smc_core.c:smc_conn_free() {
...
conn->freed = 1;
if (!smc_conn_lgr_valid(conn))
/* Connection has already unregistered from
* link group.
*/
goto lgr_put;
...
if (!list_empty(&lgr->list))
smc_buf_unuse(conn, lgr); /* allow buffer reuse */
...
}
Unlike __smc_lgr_terminate(), where the entire link group is being torn down,
lgr stays active here, so old's send buffer and RMB remain marked used for the
rest of the link group's lifetime and their rkeys and rmbuse counters are not
released.
[1] https://sashiko.dev/#/bug/linux-7780bd9b-22d1-46dc-9181-5d1b941b3794
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261129001734.78551-1-bugs@stellman-greene.com?part=1
^ permalink raw reply [flat|nested] 3+ messages in thread* Re: [PATCH net] net/smc: abort old connection when peer reuses RMB element
2026-11-29 0:17 [PATCH net] net/smc: abort old connection when peer reuses RMB element Andrew Stellman
2026-11-30 0:17 ` sashiko-bot
@ 2026-12-01 8:39 ` Mahanta Jambigi
1 sibling, 0 replies; 3+ messages in thread
From: Mahanta Jambigi @ 2026-12-01 8:39 UTC (permalink / raw)
To: Andrew Stellman, D. Wythe, Dust Li, Sidraya Jayagond
Cc: Tony Lu, Wen Gu, David S. Miller, Eric Dumazet, Jakub Kicinski,
Paolo Abeni, Simon Horman, Willem de Bruijn, linux-rdma,
linux-s390, netdev
On 29/11/26 5:47 am, Andrew Stellman wrote:
> From: Andrew Stellman <astellman@stellman-greene.com>
>
> If a peer repeats an RMB rkey and DMA address in a later CLC handshake,
> smc_rtoken_add() reuses the existing rtoken slot. If the peer also
> repeats the RMB element index, two connections in the link group write
> to the same peer buffer.
The rkey and DMA address are not per connection. The peer registers its
RMB once per link, and smc_tx_rdma_write() adds tx_off and the element
offset to lgr->rtokens[conn->rtoken_idx][link->link_idx].dma_addr. Every
connection to the same peer on that link therefore shares the rtoken
slot by design, and the duplicate that matters is only peer_rmbe_idx. As
written the paragraph reads like the shared rtoken is the bug; worth
rewording around the element index.
>
> After resolving the rtoken, abort any existing connection that has the
> same rtoken and element index. Set rtoken_idx to -1 in
> smc_conn_create() so a connection still in its handshake can't match.
> Both handshake paths already hold the new socket's lock, so take the
> old one with lock_sock_nested(), and keep that lock class when
> smc_close_active_abort() drops and retakes it.
>
> The abort is synchronous because, from the source, queueing the old
> connection's abort_work instead looks unsafe: a close path can call
> smc_conn_free() with the socket lock held and wait in
> cancel_work_sync() for that work, which needs the same lock, and a
> cancelled work item never drops the socket reference taken for it.
That cancelled-work reference leak is being fixed separately in
"net/smc: fix sock reference leak on abort_work cancel". Once it lands, the
asynchronous variant deserves another look, since it would remove the
lock nesting that the comment above is about.
>
> This changes existing behaviour: when a peer presents an rtoken and
> element index that another connection on the link group is using, the
> older connection is now aborted.
>
> Tested on mainline in arm64 QEMU with KASAN, lockdep and SoftRoCE. With
> a test-only peer that repeated the first connection's rtoken and element
> index in a second ACCEPT and a second CONFIRM, the old server socket
> read the second connection's data before the patch; after it, send() on
> both old sockets failed with EPIPE, with no lockdep or KASAN warning.
> Ordinary two-connection traffic passed with and without the patch.
>
> The issue was found by Claude Opus 5 running Quality Playbook, an
> LLM-driven code review tool:
> https://github.com/andrewstellman/quality-playbook
> The fix was drafted with OpenAI GPT-6 (Codex) and this changelog with
> Claude Opus 5.5, and both were reviewed by me.
>
> Fixes: bd4ad57718cc ("smc: initialize IB transport incl. PD, MR, QP, CQ, event, WR")
> Signed-off-by: Andrew Stellman <astellman@stellman-greene.com>
> ---
> A reproducer is available on request.
>
> net/smc/af_smc.c | 2 +-
> net/smc/smc_close.c | 20 +++++++++++++-------
> net/smc/smc_close.h | 2 +-
> net/smc/smc_core.c | 46 +++++++++++++++++++++++++++++++++++++++++----
> 4 files changed, 57 insertions(+), 13 deletions(-)
>
> diff --git a/net/smc/af_smc.c b/net/smc/af_smc.c
> index bfc0c7e71a12b4..fbf710f4308818 100644
> --- a/net/smc/af_smc.c
> +++ b/net/smc/af_smc.c
> @@ -356,7 +356,7 @@ int smc_release(struct socket *sock)
>
> if (old_state == SMC_INIT && sk->sk_state == SMC_ACTIVE &&
> !smc->use_fallback)
> - smc_close_active_abort(smc);
> + smc_close_active_abort(smc, false);
>
> rc = __smc_release(smc);
>
> diff --git a/net/smc/smc_close.c b/net/smc/smc_close.c
> index 736abfbd2aafaa..88c82a7b74efd9 100644
> --- a/net/smc/smc_close.c
> +++ b/net/smc/smc_close.c
> @@ -111,7 +111,7 @@ int smc_close_abort(struct smc_connection *conn)
> return smc_cdc_get_slot_and_msg_send(conn);
> }
>
> -static void smc_close_cancel_work(struct smc_sock *smc)
> +static void smc_close_cancel_work(struct smc_sock *smc, bool nested)
> {
> struct sock *sk = &smc->sk;
>
> @@ -120,13 +120,16 @@ static void smc_close_cancel_work(struct smc_sock *smc)
> sock_put(sk);
> if (cancel_delayed_work_sync(&smc->conn.tx_work))
> sock_put(sk);
> - lock_sock(sk);
> + if (nested)
> + lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
> + else
> + lock_sock(sk);
> }
>
> /* terminate smc socket abnormally - active abort
> * link group is terminated, i.e. RDMA communication no longer possible
> */
> -void smc_close_active_abort(struct smc_sock *smc)
> +void smc_close_active_abort(struct smc_sock *smc, bool nested)
> {
> struct sock *sk = &smc->sk;
> bool release_clcsock = false;
> @@ -141,7 +144,7 @@ void smc_close_active_abort(struct smc_sock *smc)
> case SMC_APPCLOSEWAIT1:
> case SMC_APPCLOSEWAIT2:
> sk->sk_state = SMC_PEERABORTWAIT;
> - smc_close_cancel_work(smc);
> + smc_close_cancel_work(smc, nested);
> if (sk->sk_state != SMC_PEERABORTWAIT)
> break;
> sk->sk_state = SMC_CLOSED;
> @@ -151,7 +154,7 @@ void smc_close_active_abort(struct smc_sock *smc)
> case SMC_PEERCLOSEWAIT2:
> case SMC_PEERFINCLOSEWAIT:
> sk->sk_state = SMC_PEERABORTWAIT;
> - smc_close_cancel_work(smc);
> + smc_close_cancel_work(smc, nested);
> if (sk->sk_state != SMC_PEERABORTWAIT)
> break;
> sk->sk_state = SMC_CLOSED;
> @@ -163,7 +166,7 @@ void smc_close_active_abort(struct smc_sock *smc)
> case SMC_PROCESSABORT:
> case SMC_APPFINCLOSEWAIT:
> sk->sk_state = SMC_PEERABORTWAIT;
> - smc_close_cancel_work(smc);
> + smc_close_cancel_work(smc, nested);
> if (sk->sk_state != SMC_PEERABORTWAIT)
> break;
> sk->sk_state = SMC_CLOSED;
> @@ -183,7 +186,10 @@ void smc_close_active_abort(struct smc_sock *smc)
> if (release_clcsock) {
> release_sock(sk);
> smc_clcsock_release(smc);
> - lock_sock(sk);
> + if (nested)
> + lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
> + else
> + lock_sock(sk);
> }
> }
>
> diff --git a/net/smc/smc_close.h b/net/smc/smc_close.h
> index 634fea2b7c95b1..1fa9bc211f4751 100644
> --- a/net/smc/smc_close.h
> +++ b/net/smc/smc_close.h
> @@ -25,6 +25,6 @@ int smc_close_shutdown_write(struct smc_sock *smc);
> void smc_close_init(struct smc_sock *smc);
> void smc_clcsock_release(struct smc_sock *smc);
> int smc_close_abort(struct smc_connection *conn);
> -void smc_close_active_abort(struct smc_sock *smc);
> +void smc_close_active_abort(struct smc_sock *smc, bool nested);
Not a bug, but the flag is threaded through three shared helpers for one
new caller. A private __smc_close_active_abort(smc, nested) would keep
the existing signature and callers untouched.
>
> #endif /* SMC_CLOSE_H */
> diff --git a/net/smc/smc_core.c b/net/smc/smc_core.c
> index a3164201c14204..cb45c36cdce66d 100644
> --- a/net/smc/smc_core.c
> +++ b/net/smc/smc_core.c
> @@ -1537,7 +1537,7 @@ static void smc_sk_wake_ups(struct smc_sock *smc)
> }
>
> /* kill a connection */
> -static void smc_conn_kill(struct smc_connection *conn, bool soft)
> +static void smc_conn_kill(struct smc_connection *conn, bool soft, bool nested)
> {
> struct smc_sock *smc = container_of(conn, struct smc_sock, conn);
>
> @@ -1571,7 +1571,7 @@ static void smc_conn_kill(struct smc_connection *conn, bool soft)
> smc_cdc_wait_pend_tx_wr(conn);
> }
> smc_lgr_unregister_conn(conn);
> - smc_close_active_abort(smc);
> + smc_close_active_abort(smc, nested);
> }
>
> static void smc_lgr_cleanup(struct smc_link_group *lgr)
> @@ -1625,7 +1625,7 @@ static void __smc_lgr_terminate(struct smc_link_group *lgr, bool soft)
> * link group by a retried handshake in the meantime.
> */
> if (conn->lgr == lgr)
> - smc_conn_kill(conn, soft);
> + smc_conn_kill(conn, soft, false);
> release_sock(&smc->sk);
> sock_put(&smc->sk); /* sock_hold above */
> read_lock_bh(&lgr->conns_lock);
> @@ -1788,7 +1788,7 @@ static void smc_conn_abort_work(struct work_struct *work)
> struct smc_sock *smc = container_of(conn, struct smc_sock, conn);
>
> lock_sock(&smc->sk);
> - smc_conn_kill(conn, true);
> + smc_conn_kill(conn, true, false);
> release_sock(&smc->sk);
> sock_put(&smc->sk); /* sock_hold done by schedulers of abort_work */
> }
> @@ -2024,6 +2024,7 @@ int smc_conn_create(struct smc_sock *smc, struct smc_init_info *ini)
> spinlock_t *lgr_lock;
> int rc = 0;
>
> + conn->rtoken_idx = -1;
> lgr_list = ini->is_smcd ? &ini->ism_dev[ini->ism_selected]->lgr_list :
> &smc_lgr_list.list;
> lgr_lock = ini->is_smcd ? &ini->ism_dev[ini->ism_selected]->lgr_lock :
> @@ -2757,6 +2758,42 @@ int smc_rtoken_delete(struct smc_link *lnk, __be32 nw_rkey)
> return -ENOENT;
> }
>
> +/* Abort a connection if the peer reuses its RMB element for this handshake. */
> +static void smc_rtoken_resolve_conflict(struct smc_connection *conn, u8 rmbe_idx)
> +{
> + struct smc_link_group *lgr = conn->lgr;
> + struct smc_connection *old;
> + struct smc_sock *old_smc;
> + struct rb_node *node;
> +
> + for (;;) {
> + read_lock_bh(&lgr->conns_lock);
> + for (node = rb_first(&lgr->conns_all); node; node = rb_next(node)) {
> + old = rb_entry(node, struct smc_connection, alert_node);
> + if (old != conn && !old->freed &&
> + old->rtoken_idx >= 0 &&
> + old->rtoken_idx == conn->rtoken_idx &&
> + old->peer_rmbe_idx == rmbe_idx)
> + break;
> + }
> + if (!node) {
> + read_unlock_bh(&lgr->conns_lock);
> + return;
> + }
> + old_smc = container_of(old, struct smc_sock, conn);
> + sock_hold(&old_smc->sk);
> + read_unlock_bh(&lgr->conns_lock);
> +
> + lock_sock_nested(&old_smc->sk, SINGLE_DEPTH_NESTING);
Can two handshakes deadlock here?
Both handshake workers hold their own socket lock for the whole handshake:
smc_connect_work() takes smc->sk before __smc_connect(), and
smc_listen_work() holds new_smc->sk until smc_listen_out(). This takes
the second socket's lock while the first is still held, and
SINGLE_DEPTH_NESTING is a lockdep annotation, not a deadlock prevention.
Two connections that resolve the same peer element at the same time then
form a cycle: A holds sk(A) and takes sk(B), B holds sk(B) and takes
sk(A). The window is small, rtoken_idx is set just before the scan, but
the peer controls the timing: two connections opened together with the
same rmbe_idx put both workers into it, and both sleep in D state with
their locks held. That is a remote hang against smc_hs_wq, and it comes
from exactly the duplicated element this patch is about. A correct peer
never hands one element to two live connections, so this is buggy or
hostile peer territory rather than normal traffic, but it is the case
the patch has to survive.
> + if (old->lgr == lgr && old->alert_token_local && !old->freed &&
> + old->rtoken_idx == conn->rtoken_idx &&
> + old->peer_rmbe_idx == rmbe_idx)
> + smc_conn_kill(old, true, true);
> + release_sock(&old_smc->sk);
> + sock_put(&old_smc->sk);
> + }
> +}
> +
> /* save rkey and dma_addr received from peer during clc handshake */
> int smc_rmb_rtoken_handling(struct smc_connection *conn,
> struct smc_link *lnk,
> @@ -2766,6 +2803,7 @@ int smc_rmb_rtoken_handling(struct smc_connection *conn,
> clc->r0.rmb_rkey);
> if (conn->rtoken_idx < 0)
> return conn->rtoken_idx;
> + smc_rtoken_resolve_conflict(conn, clc->r0.rmbe_idx);
On the client this runs with smc_client_lgr_pending held:
smc_connect_rdma() takes the mutex before smc_conn_create() and releases
it after smc_rmb_rtoken_handling(). If the resolve blocks on an old
connection's socket lock, for example a passive connection still waiting
for its CONFIRM, which holds that lock for the CLC timeout, then every
client-side connect stalls behind the global mutex. Either move the
resolve outside the mutex or make it non-blocking for handshaking
connections, as above.
^ permalink raw reply [flat|nested] 3+ messages in thread