Netdev List
 help / color / mirror / Atom feed
* [PATCH net-next v2 0/4] net/smc: stop a silent peer stalling every server handshake
@ 2026-12-03 15:35 Ermenson Junior
  2026-12-03 15:35 ` [PATCH net-next v2 1/4] net/smc: bound the SMC-Rv1 add-link rkey exchange Ermenson Junior
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: Ermenson Junior @ 2026-12-03 15:35 UTC (permalink / raw)
  To: alibuda, dust.li, sidraya, mjambigi
  Cc: tonylu, guwen, davem, edumazet, kuba, pabeni, willemb, horms,
	netdev, linux-rdma, linux-s390

v1: https://lore.kernel.org/netdev/20261202131243.664308-1-ermenson.sec@gmail.com/

v1 serialized per peer with a mutex hashed on proposal fields. Mahanta
asked what a duplicate link group costs and said he would rather see the
exact fix. The Sashiko bot found the hash reading the IP prefix area as
a gid for an SMCv1 proposal. v2 drops the hash and takes the exact
route.

To answer the question in the archive: a duplicate link group is
harmless for correctness, because the ACCEPT carries first_contact_local
and the client creates a matching group, and because a group at
max_conns already makes the server create siblings today. It is not free
in resources: lgr_cnt is a counter and not a cap, and an idle server
group is held SMC_LGR_FREE_DELAY_SERV, 600 seconds.

The series grew to four patches because of the bot's other point. Patch
4 takes the whole of smc_listen_rdma_finish() out from under the global
mutex, including the first-contact ADD_LINK phase, and a joining
connection of the same peer can then block on rmbs_lock or
llc_conf_mutex with no timeout while holding that mutex. Patches 1 and 2
bound the flow it would be waiting on, so they come first. Patch 3 fills
a new link's peer gid and mac at creation, which patch 4 needs because
the new match compares them before the CONFIRM arrives.

Patches 1 and 2 stand alone and fix the High issue the bot raised on v1
as pre-existing.

Ermenson Junior (4):
  net/smc: bound the SMC-Rv1 add-link rkey exchange
  net/smc: check the ADD_LINK_CONT send result in the server rkey
    exchange
  net/smc: set a new link's peer gid and mac at creation
  net/smc: drop smc_server_lgr_pending across the CLC CONFIRM wait

 net/smc/af_smc.c   |  46 ++++++++---
 net/smc/smc_core.c | 191 +++++++++++++++++++++++++++++++++++++++++++++
 net/smc/smc_core.h |  21 +++++
 net/smc/smc_llc.c  |  58 ++++++++++++--
 net/smc/smc_llc.h  |   7 ++
 5 files changed, 304 insertions(+), 19 deletions(-)

-- 
2.43.0


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH net-next v2 1/4] net/smc: bound the SMC-Rv1 add-link rkey exchange
  2026-12-03 15:35 [PATCH net-next v2 0/4] net/smc: stop a silent peer stalling every server handshake Ermenson Junior
@ 2026-12-03 15:35 ` Ermenson Junior
  2026-12-03 15:35 ` [PATCH net-next v2 2/4] net/smc: check the ADD_LINK_CONT send result in the server " Ermenson Junior
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: Ermenson Junior @ 2026-12-03 15:35 UTC (permalink / raw)
  To: alibuda, dust.li, sidraya, mjambigi
  Cc: tonylu, guwen, davem, edumazet, kuba, pabeni, willemb, horms,
	netdev, linux-rdma, linux-s390

smc_llc_srv_rkey_exchange() and smc_llc_cli_rkey_exchange() drive the
SMC-Rv1 ADD_LINK_CONT exchange with a loop whose termination condition
is re-read from the peer on every round:

	do {
		...
		qentry = smc_llc_wait(lgr, link, SMC_LLC_WAIT_TIME,
				      SMC_LLC_ADD_LINK_CONT);
		...
		num_rkeys_recv = addc_llc->num_rkeys;
		max = min_t(u8, num_rkeys_recv,
			    SMC_LLC_RKEYS_PER_CONT_MSG);
		for (i = 0; i < max; i++) {
			smc_rtoken_set(...);
			num_rkeys_recv--;
		}
		...
	} while (num_rkeys_send || num_rkeys_recv);

num_rkeys_recv is taken from each ADD_LINK_CONT message the peer sends
and is decremented by at most SMC_LLC_RKEYS_PER_CONT_MSG, which is 2. A
peer that answers num_rkeys=255 every time leaves it at 253 at the
bottom of every round, so the condition never goes false.
num_rkeys_send does not help: it reaches zero on its own, after which
the local side keeps emitting ADD_LINK_CONT messages with num_rkeys=0
while the peer's half of the condition holds the loop open. There is no
iteration count, no deadline and no total work budget, so the peer
decides both how many rounds the exchange takes and, by replying just
inside SMC_LLC_WAIT_TIME, how long each round lasts.

Both loops run with a module-global mutex held. On the server,
smc_llc_srv_rkey_exchange() is reached from smc_listen_work() through
smc_listen_rdma_finish() and smcr_serv_conf_first_link(), between
mutex_lock(&smc_server_lgr_pending) and its matching unlock, so every
incoming SMC connection on the host waits behind it. On the client,
smcr_clnt_conf_first_link() is called from smc_connect_rdma() inside
the smc_client_lgr_pending region, so a hostile server stalls every
outgoing SMC connection on the host. lgr->rmbs_lock and
lgr->llc_conf_mutex are held across the loop as well.

Bound the exchange in both directions. An ADD_LINK_CONT message carries
at most SMC_LLC_RKEYS_PER_CONT_MSG rkeys and a link group holds at most
SMC_RMBS_PER_LGR_MAX of them, so a peer that keeps to the protocol
never needs more than DIV_ROUND_UP(SMC_RMBS_PER_LGR_MAX,
SMC_LLC_RKEYS_PER_CONT_MSG) messages, which is 128, and anything beyond
that is a protocol error. Cap the rounds at that derived limit and give
the exchange as a whole a deadline: the round cap on its own would
still allow 128 * SMC_LLC_WAIT_TIME of held mutex, and a deadline on
its own would still allow an unbounded number of instant rounds under
it. Neither check rejects a message a conforming peer can send.

SMC_LLC_RKEY_EXCHG_TIME is ten times the existing per-message timeout.
The exchange is reactive on a QP that is already up, so this is
generous; the value is a policy choice and easy to change if a
different one is preferred.

Aborting the exchange costs only the optional second link. No caller
propagates the result: smcr_clnt_conf_first_link() and
smcr_serv_conf_first_link() ignore it, smc_llc_process_cli_add_link()
ignores it, smc_llc_process_srv_add_link() uses it only to decide
whether to drop an asymmetric link, and smc_llc_cli_add_link() already
maps an rkey-exchange failure to rc = 0. A link group that hits either
limit keeps running on its first link instead of failing the
connection.

The SMC-Rv2 counterparts of this same trust in a peer-supplied count
were bounded in commit 2d1e7c5aaa33 ("net/smc: bound the peer rkey
counts in SMC-Rv2 LLC messages"). The v1 loops were left as they were;
this is the same field on the other side of the version test in
smc_llc_srv_add_link().

Not reproduced dynamically, and I want to be explicit about that. The
loop is on the SMC-Rv1 path, which needs a GID of type
IB_GID_TYPE_ROCE (smc_ib_determine_gid_rcu()); the soft-RoCE driver
publishes only IB_GID_TYPE_ROCE_UDP_ENCAP, so SMC-Rv1 cannot be
negotiated on an rxe setup, and I have no RoCEv1 hardware to test on.
The reasoning above is from source reading and the patch is
compile-tested only.

This patch was written with the help of an AI coding assistant (Claude).

Fixes: 87f88cda2128 ("net/smc: rkey processing for a new link as SMC
client")
Fixes: 57b499242cb8 ("net/smc: rkey processing for a new link as SMC
server")

Signed-off-by: Ermenson Junior <ermenson.sec@gmail.com>
---
 net/smc/smc_llc.c | 47 +++++++++++++++++++++++++++++++++++++++++++----
 net/smc/smc_llc.h |  7 +++++++
 2 files changed, 50 insertions(+), 4 deletions(-)

diff --git a/net/smc/smc_llc.c b/net/smc/smc_llc.c
index c9ccd8480369..818dd84e720f 100644
--- a/net/smc/smc_llc.c
+++ b/net/smc/smc_llc.c
@@ -111,6 +111,13 @@ struct smc_llc_msg_req_add_link_v2 {
 
 #define SMC_LLC_RKEYS_PER_CONT_MSG	2
 
+/* An ADD_LINK_CONT message carries at most SMC_LLC_RKEYS_PER_CONT_MSG rkeys and
+ * a link group holds at most SMC_RMBS_PER_LGR_MAX of them, so a peer that keeps
+ * to the protocol never needs more messages than this to finish the exchange.
+ */
+#define SMC_LLC_RKEY_CONT_MSG_MAX					\
+	DIV_ROUND_UP(SMC_RMBS_PER_LGR_MAX, SMC_LLC_RKEYS_PER_CONT_MSG)
+
 struct smc_llc_msg_add_link_cont {	/* type 0x03 */
 	struct smc_llc_hdr hd;
 	u8 link_num;
@@ -914,6 +921,8 @@ static int smc_llc_cli_rkey_exchange(struct smc_link *link,
 	u8 max, num_rkeys_send, num_rkeys_recv;
 	struct smc_llc_qentry *qentry;
 	struct smc_buf_desc *buf_pos;
+	unsigned long expire;
+	int rounds = 0;
 	int buf_lst;
 	int rc = 0;
 	int i;
@@ -921,9 +930,23 @@ static int smc_llc_cli_rkey_exchange(struct smc_link *link,
 	down_write(&lgr->rmbs_lock);
 	num_rkeys_send = lgr->conns_num;
 	buf_pos = smc_llc_get_first_rmb(lgr, &buf_lst);
+	expire = jiffies + SMC_LLC_RKEY_EXCHG_TIME;
 	do {
-		qentry = smc_llc_wait(lgr, NULL, SMC_LLC_WAIT_TIME,
-				      SMC_LLC_ADD_LINK_CONT);
+		long timeo;
+
+		if (rounds++ >= SMC_LLC_RKEY_CONT_MSG_MAX) {
+			rc = -EPROTO;
+			break;
+		}
+		timeo = (long)(expire - jiffies);
+		if (timeo <= 0) {
+			rc = -ETIMEDOUT;
+			break;
+		}
+		if (timeo > SMC_LLC_WAIT_TIME)
+			timeo = SMC_LLC_WAIT_TIME;
+
+		qentry = smc_llc_wait(lgr, NULL, timeo, SMC_LLC_ADD_LINK_CONT);
 		if (!qentry) {
 			rc = -ETIMEDOUT;
 			break;
@@ -1352,6 +1375,8 @@ static int smc_llc_srv_rkey_exchange(struct smc_link *link,
 	u8 max, num_rkeys_send, num_rkeys_recv;
 	struct smc_llc_qentry *qentry = NULL;
 	struct smc_buf_desc *buf_pos;
+	unsigned long expire;
+	int rounds = 0;
 	int buf_lst;
 	int rc = 0;
 	int i;
@@ -1359,11 +1384,25 @@ static int smc_llc_srv_rkey_exchange(struct smc_link *link,
 	down_write(&lgr->rmbs_lock);
 	num_rkeys_send = lgr->conns_num;
 	buf_pos = smc_llc_get_first_rmb(lgr, &buf_lst);
+	expire = jiffies + SMC_LLC_RKEY_EXCHG_TIME;
 	do {
+		long timeo;
+
+		if (rounds++ >= SMC_LLC_RKEY_CONT_MSG_MAX) {
+			rc = -EPROTO;
+			goto out;
+		}
+		timeo = (long)(expire - jiffies);
+		if (timeo <= 0) {
+			rc = -ETIMEDOUT;
+			goto out;
+		}
+		if (timeo > SMC_LLC_WAIT_TIME)
+			timeo = SMC_LLC_WAIT_TIME;
+
 		smc_llc_add_link_cont(link, link_new, &num_rkeys_send,
 				      &buf_lst, &buf_pos);
-		qentry = smc_llc_wait(lgr, link, SMC_LLC_WAIT_TIME,
-				      SMC_LLC_ADD_LINK_CONT);
+		qentry = smc_llc_wait(lgr, link, timeo, SMC_LLC_ADD_LINK_CONT);
 		if (!qentry) {
 			rc = -ETIMEDOUT;
 			goto out;
diff --git a/net/smc/smc_llc.h b/net/smc/smc_llc.h
index 7e7a3162c68b..0a1c6c141a57 100644
--- a/net/smc/smc_llc.h
+++ b/net/smc/smc_llc.h
@@ -21,6 +21,13 @@
 #define SMC_LLC_WAIT_TIME		(2 * HZ)
 #define SMC_LLC_TESTLINK_DEFAULT_TIME	(30 * HZ)
 
+/* Total time budget for the rkey exchange of one SMC-Rv1 add-link flow, across
+ * all ADD_LINK_CONT messages. The peer decides how many messages the exchange
+ * takes, so the exchange as a whole needs a deadline and not only a per-message
+ * timeout.
+ */
+#define SMC_LLC_RKEY_EXCHG_TIME		(SMC_LLC_WAIT_TIME * 10)
+
 enum smc_llc_reqresp {
 	SMC_LLC_REQ,
 	SMC_LLC_RESP
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [PATCH net-next v2 2/4] net/smc: check the ADD_LINK_CONT send result in the server rkey exchange
  2026-12-03 15:35 [PATCH net-next v2 0/4] net/smc: stop a silent peer stalling every server handshake Ermenson Junior
  2026-12-03 15:35 ` [PATCH net-next v2 1/4] net/smc: bound the SMC-Rv1 add-link rkey exchange Ermenson Junior
@ 2026-12-03 15:35 ` Ermenson Junior
  2026-12-03 15:35 ` [PATCH net-next v2 3/4] net/smc: set a new link's peer gid and mac at creation Ermenson Junior
  2026-12-03 15:35 ` [PATCH net-next v2 4/4] net/smc: drop smc_server_lgr_pending across the CLC CONFIRM wait Ermenson Junior
  3 siblings, 0 replies; 5+ messages in thread
From: Ermenson Junior @ 2026-12-03 15:35 UTC (permalink / raw)
  To: alibuda, dust.li, sidraya, mjambigi
  Cc: tonylu, guwen, davem, edumazet, kuba, pabeni, willemb, horms,
	netdev, linux-rdma, linux-s390

smc_llc_cli_rkey_exchange() checks what smc_llc_add_link_cont() returns
and leaves the loop when the send fails:

	rc = smc_llc_add_link_cont(link, link_new, &num_rkeys_send,
				   &buf_lst, &buf_pos);
	if (rc)
		break;

smc_llc_srv_rkey_exchange() discards it and goes on to wait for a reply
to a message that was never transmitted, so the round can only end in
the wait timing out. Make the server side behave like the client side.

This is independent of the bounding in the previous patch and can be
dropped or deferred without affecting it.

This patch was written with the help of an AI coding assistant (Claude).

Fixes: 57b499242cb8 ("net/smc: rkey processing for a new link as SMC
server")

Signed-off-by: Ermenson Junior <ermenson.sec@gmail.com>
---
 net/smc/smc_llc.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/net/smc/smc_llc.c b/net/smc/smc_llc.c
index 818dd84e720f..e41011d19b73 100644
--- a/net/smc/smc_llc.c
+++ b/net/smc/smc_llc.c
@@ -1400,8 +1400,10 @@ static int smc_llc_srv_rkey_exchange(struct smc_link *link,
 		if (timeo > SMC_LLC_WAIT_TIME)
 			timeo = SMC_LLC_WAIT_TIME;
 
-		smc_llc_add_link_cont(link, link_new, &num_rkeys_send,
-				      &buf_lst, &buf_pos);
+		rc = smc_llc_add_link_cont(link, link_new, &num_rkeys_send,
+					   &buf_lst, &buf_pos);
+		if (rc)
+			goto out;
 		qentry = smc_llc_wait(lgr, link, timeo, SMC_LLC_ADD_LINK_CONT);
 		if (!qentry) {
 			rc = -ETIMEDOUT;
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [PATCH net-next v2 3/4] net/smc: set a new link's peer gid and mac at creation
  2026-12-03 15:35 [PATCH net-next v2 0/4] net/smc: stop a silent peer stalling every server handshake Ermenson Junior
  2026-12-03 15:35 ` [PATCH net-next v2 1/4] net/smc: bound the SMC-Rv1 add-link rkey exchange Ermenson Junior
  2026-12-03 15:35 ` [PATCH net-next v2 2/4] net/smc: check the ADD_LINK_CONT send result in the server " Ermenson Junior
@ 2026-12-03 15:35 ` Ermenson Junior
  2026-12-03 15:35 ` [PATCH net-next v2 4/4] net/smc: drop smc_server_lgr_pending across the CLC CONFIRM wait Ermenson Junior
  3 siblings, 0 replies; 5+ messages in thread
From: Ermenson Junior @ 2026-12-03 15:35 UTC (permalink / raw)
  To: alibuda, dust.li, sidraya, mjambigi
  Cc: tonylu, guwen, davem, edumazet, kuba, pabeni, willemb, horms,
	netdev, linux-rdma, linux-s390

smcr_link_init() leaves lnk->peer_gid and lnk->peer_mac zero. They are
filled only once the peer's CLC accept or confirm has been processed, by
smc_link_save_peer_info(), which copies them out of smc_init_info. Both
values are already in smc_init_info when the link is created, so set them
there. This changes when the fields become valid, not what they hold.
peer_qpn, peer_psn and peer_mtu come from the CLC message and stay where
they are.

The add-link paths pass a zeroed smc_init_info, so the new link gets zeros
as before, and smc_llc_save_add_link_info() fills both from the ADD_LINK
message straight after.

No reader changes behaviour: the QP is programmed from the gid only after
the peer info is saved, smcr_lgr_match() looks at active links, and the
netlink and diag paths display the field.

Signed-off-by: Ermenson Junior <ermenson.sec@gmail.com>
---
 net/smc/smc_core.c | 9 +++++++++
 1 file changed, 9 insertions(+)

diff --git a/net/smc/smc_core.c b/net/smc/smc_core.c
index a3164201c142..d265e83f1845 100644
--- a/net/smc/smc_core.c
+++ b/net/smc/smc_core.c
@@ -824,6 +824,15 @@ int smcr_link_init(struct smc_link_group *lgr, struct smc_link *lnk,
 		lnk->wr_rx_sge_cnt = 1;
 		lnk->wr_rx_buflen = SMC_WR_BUF_SIZE;
 	}
+	/* peer_qpn, peer_psn and peer_mtu come from the peer's CLC CONFIRM and
+	 * are filled later by smc_link_save_peer_info(). The gid and the mac are
+	 * already in ini here, and smc_link_save_peer_info() copies them from the
+	 * same fields, so this changes when they become valid and not what they
+	 * hold. The add-link callers pass a zeroed ini and overwrite both in
+	 * smc_llc_save_add_link_info() straight after.
+	 */
+	memcpy(lnk->peer_gid, ini->peer_gid, SMC_GID_SIZE);
+	memcpy(lnk->peer_mac, ini->peer_mac, sizeof(lnk->peer_mac));
 	get_device(&lnk->smcibdev->ibdev->dev);
 	atomic_inc(&lnk->smcibdev->lnk_cnt);
 	refcount_set(&lnk->refcnt, 1); /* link refcnt is set to 1 */
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [PATCH net-next v2 4/4] net/smc: drop smc_server_lgr_pending across the CLC CONFIRM wait
  2026-12-03 15:35 [PATCH net-next v2 0/4] net/smc: stop a silent peer stalling every server handshake Ermenson Junior
                   ` (2 preceding siblings ...)
  2026-12-03 15:35 ` [PATCH net-next v2 3/4] net/smc: set a new link's peer gid and mac at creation Ermenson Junior
@ 2026-12-03 15:35 ` Ermenson Junior
  3 siblings, 0 replies; 5+ messages in thread
From: Ermenson Junior @ 2026-12-03 15:35 UTC (permalink / raw)
  To: alibuda, dust.li, sidraya, mjambigi
  Cc: tonylu, guwen, davem, edumazet, kuba, pabeni, willemb, horms,
	netdev, linux-rdma, linux-s390

smc_listen_work() holds the module-global smc_server_lgr_pending across the
wait for the peer's CLC CONFIRM, which smc_clc_wait_msg() bounds only by
CLC_WAIT_TIME per receive. A peer that sends its proposal, reads the ACCEPT
and then goes silent holds the mutex for six seconds, and every other
server-side handshake on the host queues behind it. A lossy path does this
by accident. Commit 8270d9c21041 ("net/smc: Limit backlog connections")
already describes the lock as host wide.

Releasing it earlier was not possible: a first-contact link group has no
active link until smcr_serv_conf_first_link() has run, after the CONFIRM,
and smcr_lgr_match() skips inactive links, so a second connection of the
same peer would create a duplicate link group.

Make that link group findable instead. smcr_lgr_pending_match() matches a
link group with no active link whose link is still SMC_LNK_ACTIVATING,
comparing what smcr_lgr_match() compares. A connection that finds one
defers: smc_conn_create() returns with ini->pending_lgr holding a
reference, smc_listen_work() drops the mutex, waits, and looks again.
Waiters are woken where a link leaves SMC_LNK_ACTIVATING, in
smc_llc_link_active() and smcr_link_clear().

The mutex still covers smc_lgr_create(), which is load bearing: until the
list_add_tail() there the new link group is not on smc_lgr_list, so a
second connection in that window would find nothing to defer to.

What moves out from under the mutex is all of smc_listen_rdma_finish(),
not only the CONFIRM receive: smc_link_save_peer_info(),
smc_rmb_rtoken_handling(), smc_ib_ready_link() and
smcr_serv_conf_first_link(), which includes the LLC CONFIRM_LINK wait and,
for max_links > 1, the first-contact ADD_LINK phase. smc_llc_link_active()
runs before that phase, so once it has run a second connection of the peer
matches the link group normally and no longer defers. It can then block in
smc_buf_get_slot() on rmbs_lock or in smcr_buf_map_usable_links() on
llc_conf_mutex while holding smc_server_lgr_pending, waiting on the
first-contact worker's add-link flow. Those two waits have no timeout, so
this is only acceptable because the flow they wait on is bounded, which is
what the first two patches of this series do.

Deferrals repeat only inside a window of two CLC_WAIT_TIME opened by the
first, with each wait clipped to what remains, so a peer cannot park a
listen worker for longer than that window. Past it the worker creates a
link group as before. The window is wider than the longest a link can stay
in SMC_LNK_ACTIVATING, CLC_WAIT_TIME + SMC_LLC_WAIT_FIRST_TIME, so today
that fallback is not reachable. The margin is one second and depends on
those two constants.

Tested on v7.4-rc5, two kernels differing only by this series with
identical configs, soft-RoCE, peers in separate netns. An unrelated peer's
connect() during a silent peer's handshake: 4397.8 ms to 1.5 ms. In the
patched kernel a second connection of the same peer during a first contact
still waits 689.3 ms, which is what makes the first number attributable.
No lockdep report with PROVE_LOCKING and DEBUG_ATOMIC_SLEEP on.

Found and written with Claude (Anthropic), used interactively, reviewed and
tested by the author. Documentation/process/generated-content.rst asks for
this to be stated.

Signed-off-by: Ermenson Junior <ermenson.sec@gmail.com>
---

Notes for reviewers.

Lab: two kernels from v7.4-rc5 built by one script, empty .config diff
between them. defconfig + kvm_guest.config, SMC/SMC_DIAG/DIBS/RDMA_RXE in,
DIBS_LO off, PROVE_LOCKING + DEBUG_ATOMIC_SLEEP + DETECT_HUNG_TASK on, zero
build warnings. Peers in separate netns with an rxe device each. The silent
peer is its CLC CONFIRM dropped by NFQUEUE while it holds the socket open.

                                        unpatched    patched
  sanity, no silent peer                   1.7 ms     0.7 ms
  silent peer, unrelated victim         4397.8 ms     1.5 ms
  after drain                              1.0 ms     0.8 ms

v1's control does not work here. Its same-peer connection is not a first
contact, so v2 neither defers nor takes the mutex and it returns in 0.6 ms
against 4163.4 ms unpatched. The control that does discriminate is a second
connection of the same peer during a first contact: 689.3 ms in the same
patched kernel where the unrelated peer saw 1.5 ms.

Link group ids, from SMC_NETLINK_GET_LGR_SMCR (ss --smc does not carry the
id). Both kernels, same result: first contact succeeds, one group with two
connections. First contact never completes, group torn down and a new one
built, one live at a time. Three connections of one peer, one group with
three connections.

Patch 1 is confirmed by that dumper: the ACTIVATING server link shows the
real peer gid with it and zeros without it, and without it the new match
never fires in the window this series opens.

Not measured. No tracepoint on the deferral path, so the single-group
results are consistent with it working and do not prove it ran. The
window-exceeded case is untested: I tried to stall ADD_LINK by dropping
RoCE
traffic and lost the race by 6 ms, so those phases are inconclusive, not
clean. One run per phase. Not covered: real hardware, SMC-D, SMC-Rv1
(soft-RoCE has only RoCEv2 gids), multi-link groups, s390.

A poweroff hung-task in smc_cdc_wait_pend_tx_wr appears on both kernels
with
an identical stack after the scenario ends. Not this series.

Per security-bugs.rst a reproducer for an AI-assisted finding is not posted
publicly. Mine is a few shell and C files including the netlink dumper, and
I will send them to any maintainer who asks.

net-next and no Fixes tag: the hold dates from 0cfdd8f92cac (2017) and was
documented as host wide in 8270d9c21041, so this changes how the handshake
is serialized rather than fixing a regression.

Tool use. Claude (Anthropic), driven interactively. It read net/smc, found
the hold and the active-link condition behind it, proposed the pending
match, and wrote the hunks, the lab and the measurement. Three independent
review passes ran before sending: the first found the new match comparing a
gid that is zero until after the CONFIRM, which patch 1 fixes, the second
found the wrong saved version restored on retry and that ini->ib_dev is
never cleared, the third cleared it.

 net/smc/af_smc.c   |  46 +++++++----
 net/smc/smc_core.c | 190 ++++++++++++++++++++++++++++++++++++++++++++-
 net/smc/smc_core.h |  21 +++++
 net/smc/smc_llc.c  |   5 ++
 4 files changed, 245 insertions(+), 17 deletions(-)

diff --git a/net/smc/af_smc.c b/net/smc/af_smc.c
index bfc0c7e71a12..fb815b141ddd 100644
--- a/net/smc/af_smc.c
+++ b/net/smc/af_smc.c
@@ -2415,6 +2415,14 @@ static int smc_listen_find_device(struct smc_sock *new_smc,
 	smc_find_rdma_v2_device_serv(new_smc, pclc, ini);
 	if (ini->smcrv2.ib_dev_v2)
 		return 0;
+	/* A first contact of this peer is still establishing. Return now:
+	 * trying V1 below would call smc_conn_create() a second time, and with
+	 * the deferral already spent it would create a link group and leave
+	 * this connection registered on it while the caller still waits and
+	 * retries.
+	 */
+	if (ini->pending_lgr)
+		return ini->rc;
 
 	/* check if RDMA V1 is available */
 	if (!prfx_rc) {
@@ -2524,12 +2532,27 @@ static void smc_listen_work(struct work_struct *work)
 	if (rc)
 		goto out_decl;
 
+	/* smc_find_rdma_v2_device_serv() rewrites ini->smcr_version on both its
+	 * paths, so a retry has to start from the value negotiated above
+	 */
+	ini->pending_smcr_version = ini->smcr_version;
+
+again:
 	mutex_lock(&smc_server_lgr_pending);
 	smc_rx_init(new_smc);
 	smc_tx_init(new_smc);
 
 	/* determine ISM or RoCE device used for connection */
 	rc = smc_listen_find_device(new_smc, pclc, ini);
+	if (ini->pending_lgr) {
+		/* a first contact of this peer is still establishing, so this
+		 * connection must not create a second link group for it. Wait
+		 * for that one with no lock held and look again.
+		 */
+		mutex_unlock(&smc_server_lgr_pending);
+		smcr_wait_pending_lgr(ini);
+		goto again;
+	}
 	if (rc)
 		goto out_unlock;
 
@@ -2540,27 +2563,25 @@ static void smc_listen_work(struct work_struct *work)
 	if (rc)
 		goto out_unlock;
 
-	/* SMC-D does not need this lock any more */
-	if (ini->is_smcd)
-		mutex_unlock(&smc_server_lgr_pending);
+	/* Link group creation is complete, so this lock is not needed across
+	 * the peer-paced wait for the CLC CONFIRM below. A further connection
+	 * of this peer now finds the link group through
+	 * smcr_lgr_pending_match() and waits for it, which is what holding
+	 * this mutex until the first link became active used to achieve.
+	 */
+	mutex_unlock(&smc_server_lgr_pending);
 
 	/* receive SMC Confirm CLC message */
 	memset(buf, 0, sizeof(*buf));
 	cclc = (struct smc_clc_msg_accept_confirm *)buf;
 	rc = smc_clc_wait_msg(new_smc, cclc, sizeof(*buf),
 			      SMC_CLC_CONFIRM, CLC_WAIT_TIME);
-	if (rc) {
-		if (!ini->is_smcd)
-			goto out_unlock;
+	if (rc)
 		goto out_decl;
-	}
 
 	rc = smc_clc_v2x_features_confirm_check(cclc, ini);
-	if (rc) {
-		if (!ini->is_smcd)
-			goto out_unlock;
+	if (rc)
 		goto out_decl;
-	}
 
 	/* fce smc release version is needed in smc_listen_rdma_finish,
 	 * so save fce info here.
@@ -2572,8 +2593,7 @@ static void smc_listen_work(struct work_struct *work)
 		rc = smc_listen_rdma_finish(new_smc, cclc,
 					    ini->first_contact_local, ini);
 		if (rc)
-			goto out_unlock;
-		mutex_unlock(&smc_server_lgr_pending);
+			goto out_decl;
 	}
 	smc_conn_save_peer_info(new_smc, cclc);
 
diff --git a/net/smc/smc_core.c b/net/smc/smc_core.c
index d265e83f1845..60e97155fa52 100644
--- a/net/smc/smc_core.c
+++ b/net/smc/smc_core.c
@@ -826,10 +826,11 @@ int smcr_link_init(struct smc_link_group *lgr, struct smc_link *lnk,
 	}
 	/* peer_qpn, peer_psn and peer_mtu come from the peer's CLC CONFIRM and
 	 * are filled later by smc_link_save_peer_info(). The gid and the mac are
-	 * already in ini here, and smc_link_save_peer_info() copies them from the
-	 * same fields, so this changes when they become valid and not what they
-	 * hold. The add-link callers pass a zeroed ini and overwrite both in
-	 * smc_llc_save_add_link_info() straight after.
+	 * already known here, from the proposal on the server and from the
+	 * accept on the client, and smc_link_save_peer_info() copies them from
+	 * the same ini fields, so setting them now changes when they become
+	 * valid and not what they become. The add-link callers pass a zeroed
+	 * ini and overwrite both in smc_llc_save_add_link_info() afterwards.
 	 */
 	memcpy(lnk->peer_gid, ini->peer_gid, SMC_GID_SIZE);
 	memcpy(lnk->peer_mac, ini->peer_mac, sizeof(lnk->peer_mac));
@@ -1393,6 +1394,10 @@ static void __smcr_link_clear(struct smc_link *lnk)
 	lnk->state = SMC_LNK_UNUSED;
 	if (!atomic_dec_return(&smcibdev->lnk_cnt))
 		wake_up(&smcibdev->lnks_deleted);
+	/* a listen worker may be deferring to this link group, see
+	 * smcr_wait_pending_lgr()
+	 */
+	wake_up_all(&lgr->first_contact_waiter);
 	smc_lgr_put(lgr); /* lgr_hold in smcr_link_init() */
 }
 
@@ -2007,6 +2012,153 @@ static bool smcr_lgr_match(struct smc_link_group *lgr, u8 smcr_version,
 	return false;
 }
 
+/* Like smcr_lgr_match(), but for a link group whose first link has not become
+ * active yet. Such a link group is invisible to smcr_lgr_match(), which skips
+ * inactive links, so without this a second connection of the same peer creates
+ * a duplicate link group. The fields compared are the ones smcr_lgr_match()
+ * compares, taken from the link that is being activated. The peer_qpn
+ * clause of smcr_lgr_match() is absent because it only applies when the
+ * role is not SMC_SERV, and this only matches server side link groups.
+ */
+static bool smcr_lgr_pending_match(struct smc_link_group *lgr,
+				   struct smc_init_info *ini, struct net *net)
+{
+	struct smc_link *lnk;
+	int i;
+
+	if (lgr->is_smcd || lgr->role != SMC_SERV ||
+	    lgr->sync_err || lgr->terminating || lgr->freeing)
+		return false;
+	if (memcmp(lgr->peer_systemid, ini->peer_systemid, SMC_SYSTEMID_LEN))
+		return false;
+	/* the same clause the join loop in smc_conn_create() applies */
+	if (ini->smcd_version != SMC_V2 && lgr->vlan_id != ini->vlan_id)
+		return false;
+
+	/* Only a first contact is deferred to. A link group that is adding a
+	 * further link also has a link in SMC_LNK_ACTIVATING, and a peer that
+	 * has filled that group would then be delayed before creating the new
+	 * group it needs.
+	 */
+	for (i = 0; i < SMC_LINKS_PER_LGR_MAX; i++)
+		if (smc_link_active(&lgr->lnk[i]))
+			return false;
+
+	for (i = 0; i < SMC_LINKS_PER_LGR_MAX; i++) {
+		lnk = &lgr->lnk[i];
+
+		if (lnk->state != SMC_LNK_ACTIVATING)
+			continue;
+		if (!rdma_dev_access_netns(lnk->smcibdev->ibdev, net))
+			return false;
+		if (!memcmp(lnk->peer_gid, ini->peer_gid, SMC_GID_SIZE) &&
+		    (ini->smcr_version == SMC_V2 ||
+		     !memcmp(lnk->peer_mac, ini->peer_mac, ETH_ALEN)))
+			return true;
+	}
+	return false;
+}
+
+/* True while a link group still has a link being activated and has not failed.
+ * Every outcome of a first contact leaves SMC_LNK_ACTIVATING: success through
+ * smc_llc_link_active(), failure through smcr_link_clear().
+ */
+static bool smcr_lgr_activating(struct smc_link_group *lgr)
+{
+	int i;
+
+	/* lnk[] is in the SMC-R arm of the link group union, so check the
+	 * variant here rather than trusting the caller
+	 */
+	if (lgr->is_smcd)
+		return false;
+	if (READ_ONCE(lgr->sync_err) || READ_ONCE(lgr->terminating) ||
+	    READ_ONCE(lgr->freeing))
+		return false;
+	for (i = 0; i < SMC_LINKS_PER_LGR_MAX; i++)
+		if (READ_ONCE(lgr->lnk[i].state) == SMC_LNK_ACTIVATING)
+			return true;
+	return false;
+}
+
+/* A deferral parks this listen worker, so repeated deferrals need a bound.
+ * The window opens at the first deferral, not at the first call, and it is
+ * two CLC_WAIT_TIME wide so that it covers a first contact failing on its own
+ * CONFIRM timeout: without that room the second connection's wait always ends
+ * past the deadline and a third connection could still create the duplicate
+ * this is here to prevent. Past the window the worker creates a link group as
+ * it did before this patch.
+ */
+#define SMC_LGR_PENDING_WINDOW	(2 * CLC_WAIT_TIME)
+
+static bool smcr_may_defer(struct smc_init_info *ini)
+{
+	return !ini->pending_armed ||
+	       time_before(jiffies, ini->pending_deadline);
+}
+
+/* Find a link group of this peer whose first link is still being activated,
+ * and return it with a reference held, or NULL.
+ */
+static struct smc_link_group *smcr_lgr_find_pending(struct smc_init_info *ini,
+						    struct net *net)
+{
+	struct smc_link_group *lgr, *found = NULL;
+
+	spin_lock_bh(&smc_lgr_list.lock);
+	list_for_each_entry(lgr, &smc_lgr_list.list, list) {
+		if (smcr_lgr_pending_match(lgr, ini, net)) {
+			smc_lgr_hold(lgr);
+			found = lgr;
+			break;
+		}
+	}
+	spin_unlock_bh(&smc_lgr_list.lock);
+	return found;
+}
+
+/* Wait for the link group that smc_conn_create() deferred to, then drop its
+ * reference. The caller must hold neither a link group lock nor
+ * smc_server_lgr_pending, so that the first contact being waited for can make
+ * progress. A timeout is not an error: the caller retries, and once the retry
+ * budget in smc_conn_create() is spent it creates a link group as before.
+ */
+void smcr_wait_pending_lgr(struct smc_init_info *ini)
+{
+	struct smc_link_group *lgr = ini->pending_lgr;
+	unsigned long left;
+
+	ini->pending_lgr = NULL;
+	/* smc_init_info_store_rc() keeps the first value stored, so the
+	 * deferral's own rc has to go or it would mask the retry's. A decline
+	 * reason an earlier ISM attempt stored is dropped with it, and the
+	 * retry derives its own.
+	 */
+	ini->rc = 0;
+	if (!lgr)
+		return;
+	/* smc_find_rdma_v2_device_serv() leaves ini->smcr_version rewritten,
+	 * and nothing in net/smc ever clears ini->ib_dev, so a device that went
+	 * away during the sleep would still pass the !ini->ib_dev test in
+	 * smc_find_rdma_device(). Put both back to the state the first pass
+	 * started from.
+	 */
+	ini->smcr_version = ini->pending_smcr_version;
+	ini->ib_dev = NULL;
+	ini->ib_port = 0;
+	memset(ini->ib_gid, 0, sizeof(ini->ib_gid));
+
+	/* clipped to the window, so several deferrals cannot add up to more
+	 * parked time than one deferral is allowed
+	 */
+	left = ini->pending_deadline - jiffies;
+	if ((long)left > 0)
+		wait_event_timeout(lgr->first_contact_waiter,
+				   !smcr_lgr_activating(lgr),
+				   min_t(unsigned long, left, CLC_WAIT_TIME));
+	smc_lgr_put(lgr);
+}
+
 static bool smcd_lgr_match(struct smc_link_group *lgr,
 			   struct smcd_dev *smcismdev,
 			   struct smcd_gid *peer_gid)
@@ -2084,6 +2236,36 @@ int smc_conn_create(struct smc_sock *smc, struct smc_init_info *ini)
 		return SMC_CLC_DECL_SYNCERR;
 	}
 
+	/* A first contact of this peer may still be establishing. Its link is
+	 * not active yet, so the loop above cannot see its link group and
+	 * would create a duplicate. Defer to it instead; the caller waits
+	 * without holding smc_server_lgr_pending and retries.
+	 */
+	if (role == SMC_SERV && !ini->is_smcd && ini->first_contact_local &&
+	    smcr_may_defer(ini)) {
+		struct smc_link_group *pending;
+
+		pending = smcr_lgr_find_pending(ini, net);
+		if (pending) {
+			if (!ini->pending_armed) {
+				ini->pending_armed = 1;
+				ini->pending_deadline = jiffies +
+						SMC_LGR_PENDING_WINDOW;
+			}
+			ini->pending_lgr = pending;
+			/* ini->pending_lgr is the signal the listen worker
+			 * acts on. The return value only has to be non-zero,
+			 * and it must stay a valid CLC decline reason:
+			 * smc_init_info_store_rc() takes a u32 and keeps the
+			 * first value stored, and smc_listen_find_device() can
+			 * return ini->rc for smc_listen_decline() to put on
+			 * the wire. smcr_wait_pending_lgr() clears ini->rc
+			 * before the retry, so this is never sent today.
+			 */
+			return SMC_CLC_DECL_INTERR;
+		}
+	}
+
 create:
 	if (ini->first_contact_local) {
 		rc = smc_lgr_create(smc, ini);
diff --git a/net/smc/smc_core.h b/net/smc/smc_core.h
index bff4c9e17792..2dd9be7ef4f6 100644
--- a/net/smc/smc_core.h
+++ b/net/smc/smc_core.h
@@ -349,6 +349,10 @@ struct smc_link_group {
 						/* w4 next llc event */
 			wait_queue_head_t	llc_msg_waiter;
 						/* w4 next llc msg */
+			wait_queue_head_t	first_contact_waiter;
+						/* w4 1st link to leave
+						 * SMC_LNK_ACTIVATING
+						 */
 			struct smc_llc_flow	llc_flow_lcl;
 						/* llc local control field */
 			struct smc_llc_flow	llc_flow_rmt;
@@ -442,6 +446,22 @@ struct smc_init_info {
 	u8			ib_port;
 	u32			ib_clcqpn;
 	struct smc_init_info_smcrv2 smcrv2;
+	u8			pending_armed;
+				/* a deferral has happened and
+				 * pending_deadline is set
+				 */
+	u8			pending_smcr_version;
+				/* smcr_version before the first deferral */
+	unsigned long		pending_deadline;
+				/* end of the window in which further
+				 * deferrals are allowed, see
+				 * smcr_may_defer()
+				 */
+	struct smc_link_group	*pending_lgr;
+				/* a first contact for this peer is still
+				 * establishing; reference held, see
+				 * smcr_wait_pending_lgr()
+				 */
 	/* SMC-D */
 	struct smcd_gid		ism_peer_gid[SMC_MAX_V2_ISM_DEVS + 1];
 	struct smcd_dev		*ism_dev[SMC_MAX_V2_ISM_DEVS + 1];
@@ -597,6 +617,7 @@ int smc_vlan_by_tcpsk(struct socket *clcsock, struct smc_init_info *ini);
 
 void smc_conn_free(struct smc_connection *conn);
 int smc_conn_create(struct smc_sock *smc, struct smc_init_info *ini);
+void smcr_wait_pending_lgr(struct smc_init_info *ini);
 int smc_core_init(void);
 void smc_core_exit(void);
 
diff --git a/net/smc/smc_llc.c b/net/smc/smc_llc.c
index e41011d19b73..b6582f8dc3a9 100644
--- a/net/smc/smc_llc.c
+++ b/net/smc/smc_llc.c
@@ -2268,6 +2268,7 @@ void smc_llc_lgr_init(struct smc_link_group *lgr, struct smc_sock *smc)
 	spin_lock_init(&lgr->llc_flow_lock);
 	init_waitqueue_head(&lgr->llc_flow_waiter);
 	init_waitqueue_head(&lgr->llc_msg_waiter);
+	init_waitqueue_head(&lgr->first_contact_waiter);
 	init_rwsem(&lgr->llc_conf_mutex);
 	lgr->llc_testlink_time = READ_ONCE(net->smc.sysctl_smcr_testlink_time);
 	lgr->max_send_wr = (u16)(READ_ONCE(net->smc.sysctl_smcr_max_send_wr));
@@ -2306,6 +2307,10 @@ void smc_llc_link_active(struct smc_link *link)
 			    SMC_LGR_ID_SIZE, &link->peer_link_uid,
 			    link->smcibdev->ibdev->name, link->ibport);
 	link->state = SMC_LNK_ACTIVE;
+	/* a listen worker may be waiting for this link group to stop being a
+	 * pending first contact, see smcr_wait_pending_lgr()
+	 */
+	wake_up_all(&link->lgr->first_contact_waiter);
 	if (link->lgr->llc_testlink_time) {
 		link->llc_testlink_time = link->lgr->llc_testlink_time;
 		schedule_delayed_work(&link->llc_testlink_wrk,
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2026-12-03 15:40 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-03 15:35 [PATCH net-next v2 0/4] net/smc: stop a silent peer stalling every server handshake Ermenson Junior
2026-12-03 15:35 ` [PATCH net-next v2 1/4] net/smc: bound the SMC-Rv1 add-link rkey exchange Ermenson Junior
2026-12-03 15:35 ` [PATCH net-next v2 2/4] net/smc: check the ADD_LINK_CONT send result in the server " Ermenson Junior
2026-12-03 15:35 ` [PATCH net-next v2 3/4] net/smc: set a new link's peer gid and mac at creation Ermenson Junior
2026-12-03 15:35 ` [PATCH net-next v2 4/4] net/smc: drop smc_server_lgr_pending across the CLC CONFIRM wait Ermenson Junior

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