From: sashiko-bot@kernel.org
To: "Andrew Stellman" <bugs@stellman-greene.com>
Cc: linux-rdma@vger.kernel.org, linux-s390@vger.kernel.org,
"Heiko Carstens" <hca@linux.ibm.com>,
"Vasily Gorbik" <gor@linux.ibm.com>,
"Alexander Gordeev" <agordeev@linux.ibm.com>,
"Christian Borntraeger" <borntraeger@linux.ibm.com>
Subject: Re: [PATCH net] net/smc: abort old connection when peer reuses RMB element
Date: Mon, 30 Nov 2026 00:17:51 +0000 [thread overview]
Message-ID: <sashiko-outbox-240129@kernel.org> (raw)
In-Reply-To: <20261129001734.78551-1-bugs@stellman-greene.com>
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
prev parent reply other threads:[~2026-11-30 0:17 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
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 [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=sashiko-outbox-240129@kernel.org \
--to=sashiko-bot@kernel.org \
--cc=agordeev@linux.ibm.com \
--cc=borntraeger@linux.ibm.com \
--cc=bugs@stellman-greene.com \
--cc=gor@linux.ibm.com \
--cc=hca@linux.ibm.com \
--cc=linux-rdma@vger.kernel.org \
--cc=linux-s390@vger.kernel.org \
--cc=sashiko-reviews@lists.linux.dev \
/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