Netdev List
 help / color / mirror / Atom feed
* [PATCH net] net/smc: abort old connection when peer reuses RMB element
@ 2026-11-29  0:17 Andrew Stellman
  0 siblings, 0 replies; only message in thread
From: Andrew Stellman @ 2026-11-29  0:17 UTC (permalink / raw)
  To: D. Wythe, Dust Li, Sidraya Jayagond, Mahanta Jambigi
  Cc: Tony Lu, Wen Gu, David S. Miller, Eric Dumazet, Jakub Kicinski,
	Paolo Abeni, Simon Horman, Willem de Bruijn, linux-rdma,
	linux-s390, netdev

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.

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.

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);
 
 #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);
+		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);
 	return 0;
 }
 
-- 
2.53.0


^ permalink raw reply related	[flat|nested] only message in thread

only message in thread, other threads:[~2026-11-29  0:17 UTC | newest]

Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-29  0:17 [PATCH net] net/smc: abort old connection when peer reuses RMB element Andrew Stellman

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox