Netdev List
 help / color / mirror / Atom feed
* [PATCH net-next] net/smc: serialize the server establishment phase per peer, not globally
@ 2026-12-02 13:12 Ermenson Junior
  0 siblings, 0 replies; only message in thread
From: Ermenson Junior @ 2026-12-02 13:12 UTC (permalink / raw)
  To: alibuda, dust.li, sidraya, mjambigi
  Cc: tonylu, guwen, davem, edumazet, kuba, pabeni, willemb, horms,
	netdev, linux-rdma, linux-s390, Ermenson Junior

On the SMC-R server path 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 stays silent holds the
mutex for six seconds or more, and every other server-side handshake on
the host waits behind it. Commit 8270d9c21041 ("net/smc: Limit backlog
connections") already describes the lock as host wide.

The mutex cannot simply be released earlier: 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.

So keep smc_server_lgr_pending for link group creation only, released
once the ACCEPT is out, and hold a per-peer mutex (one of 64, hashed on
the proposal's peer system id and both peer gids) across the whole
establishment phase. Lock order is per-peer, then global.

The key is a heuristic. It is exact when a peer's proposals that match
one link group carry the same gids, which holds for a single-port peer.
A multi-port peer whose proposal falls back from v2 to v1 can hash two
such proposals apart, and the server then creates a second link group
for that peer instead of joining the first. No key built from the
proposal can be exact, because v1 or v2 is chosen later, under the
global mutex; making smcr_lgr_match() see a pending link group would be
exact, and is not attempted here. Connections joining a link group over
different links can also land in different buckets. That is safe because
a joining peer sends CONFIRM_RKEY before its CLC CONFIRM, so
smc_llc_rmt_conf_rkey() has filled the rtoken row before the listen
worker looks for it; a peer that does not can only harm its own link
group.

This confines the CLC CONFIRM wait of an unresponsive peer to that peer.
It does not bound the peer-paced waits that remain under the global
mutex in smc_listen_rdma_reg() for a peer with an existing link group,
and it is no defence against a peer that chooses its key.

Tested on v7.4-rc5 with two kernels differing only by this patch, on
soft-RoCE with two RoCEv2 peers in separate network namespaces: an
unrelated peer's connect() during a silent peer's handshake went from
4024.5 ms to 0.5 ms, while a connect from the same peer still waits.
Details, limits and what was not covered follow the --- line.

The problem was found and this patch, its changelog and the test lab
were written with Claude (Anthropic), used interactively, and reviewed
and tested by the author; Documentation/process/generated-content.rst
asks for this to be stated, and more detail follows the --- line.

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

Notes for reviewers, not for the commit message.

HOW THIS WAS TESTED

Two kernels built from v7.4-rc5 (fe6ff8d5d675) with the same config and the
same script, differing only by this patch, booted in qemu with the same
initramfs. x86_64 defconfig + kvm_guest.config, SMC/SMC_DIAG/INFINIBAND/
RDMA_RXE/DIBS built in, DIBS_LO off so SMC-R is selected rather than SMC-D,
PROVE_LOCKING and DETECT_HUNG_TASK on. Both kernels built with zero warnings.

Topology: two network namespaces joined by a veth pair, a soft-RoCE (rxe)
device on each end, one UEID configured, two AF_SMC listeners on the server.
No RDMA hardware. The silent peer is simulated by dropping its CLC CONFIRM
with an iptables NFQUEUE handler on the server's INPUT chain; the peer keeps
its socket open, so from the server's side this is exactly a peer that
completes the proposal, reads the ACCEPT and then says nothing.

The number that matters is how long an UNRELATED peer's connect() takes while
a silent peer is in flight. All times are the client's measured elapsed_ms,
all connects returned rc=0.

                                                   unpatched      patched
  no silent peer (sanity)                              0.6 ms       0.5 ms
  silent peer; victim is a second RoCEv2 peer,
    own namespace and rxe device, nothing rewritten 4024.5 ms       0.5 ms
  silent peer; victim is the SAME peer (control)    4170.1 ms    4352.1 ms
  silent peer with a rewritten system id (see below) 4196.2 ms      0.8 ms
  after everything drained                             0.6 ms       0.5 ms

The second row is the deployment that matters: two RoCEv2 peers, both
announcing the all-zero system id that a v2-only proposal carries, differing in
gid and in nothing else. The stalls are about four seconds rather than six
because the victim starts two seconds into the silent peer's six-second window.

The third row is the control and it is meant to stall on both kernels: two
first contacts of one peer still have to serialize, so a peer can still delay
itself. If that row had improved too, the improvement in the second row would
not have been attributable to the key.

A fast victim is only evidence if it really negotiated SMC, since a fallback to
TCP also returns rc=0 in well under a millisecond. The smc_switch_to_fallback
tracepoint was enabled for the whole run on both kernels; it fired exactly once
per run, at about 8 s, for the silent peer of an earlier phase timing out its
own CONFIRM_LINK wait (SMC_CLC_DECL_TIMEOUT_CL). No victim fell back.

Two things the lab made obvious, in the order I learned them. First, on
soft-RoCE no SMC-Rv1 can be offered (it needs IB_GID_TYPE_ROCE and rxe only has
RoCEv2 gids), and smc_clc_send_proposal() fills smc_clc_msg_local only when v1
is offered, so every proposal in this lab carries an all-zero id_for_peer. A
first version of this patch keyed the mutex on the system id alone, and the
only way to measure it was to rewrite id_for_peer in the attacker's proposal in
flight. That worked in the lab and would have isolated nothing on a real RoCEv2
deployment, where every peer announces the same zero id. The key now includes
the gid, and the decisive row above has two peers in separate namespaces with
their own rxe devices, differing in gid and in nothing else, with no field
rewritten. Second, that rewrite is itself the demonstration that the key is
chosen by the peer and authenticated by nothing, which is why the changelog
makes no claim against a peer that is trying.

In both kernels the silent peer's connection was confirmed to reach the
server's wait: its proposal was seen and its CONFIRM was dropped eight or
nine times over the 20 s it held the socket, eight on one kernel and nine on
the other. Without that check a fast victim would be indistinguishable from an
attack that never happened.

One thing in the logs I have not investigated: after the test, when the guest's
init runs poweroff with the silent peers' sockets still open, both kernels print
a hung-task report for poweroff in smc_cdc_wait_pend_tx_wr(). It is identical
on the unpatched kernel, so it is not this patch, and it is outside what I set
out to measure; I mention it so nobody finds it in a log and thinks it was
hidden.

No lockdep report of any kind appeared on the patched kernel, which is the
check that matters for the new lock order (per-peer mutex, then
smc_server_lgr_pending).

WHAT I COULD NOT DO, AND ONE MEASUREMENT I AM NOT QUOTING

Both peers are AF_SMC sockets in the same kernel, so a local client doing
first contact waits in smcr_clnt_conf_first_link() for a CONFIRM_LINK that
cannot arrive and holds smc_client_lgr_pending - the other global mutex - for
about five seconds. A remote attacker cannot touch that lock, but in this lab
it hides the effect of this patch: measured that way the victim went from
4333.5 ms to 3052.0 ms, and 3052 ms is within a millisecond of when the
attacker's own client gave up (5052.5 ms after it started, two seconds before
the victim). The rows above avoid this by letting each peer complete one
connection first, so the silent connection that follows is first contact on
neither side; its client returned in 1.6 ms and 1.7 ms and held no client-side
lock. I mention it because that contaminated pair of numbers is what the
obvious version of this experiment produces, and read alone it says the patch
does not work.

Not covered: real RDMA hardware, SMC-D, SMC-v1 (soft-RoCE offers only RoCEv2
GIDs, so a v1 proposal cannot be produced on it), link groups with more than
one link, and s390. The SMC-D path is unchanged by inspection, not by
measurement.

The reproducer is a set of shell and C files rather than a single script. It
is not inlined here; I will send it to whoever wants it.

No Fixes tag and net-next, deliberately: the behaviour is as old as the first
implementation (0cfdd8f92cac, 2017) and was documented as host-wide by the
maintainers in 8270d9c21041, and this is a change to how the handshake is
serialized, not a backport candidate.

Per Documentation/process/security-bugs.rst, a reproducer for an AI-assisted
finding is not posted to a public list. Mine is a few shell and C files for the
namespace lab above; I will send them to any maintainer who asks.

One observation from reading the surrounding code that I have NOT measured and
that this patch does not touch. On the SMC-Rv1 path, smc_llc_srv_rkey_exchange()
re-reads num_rkeys from each ADD_LINK_CONT the peer sends and loops while it is
non-zero, with no iteration cap; max_links is a fixed 2 on a v1 link group, so
every v1 first contact reaches it, and before this patch that was under
smc_server_lgr_pending. Commit 2d1e7c5aaa33 bounded the v2 twin of this loop
this year and left the v1 one as it was; smc_llc_cli_rkey_exchange() has the
same shape on the client side. I cannot measure it here because soft-RoCE
offers no RoCEv1 gid, so SMC-Rv1 cannot be negotiated on this lab. I will send
a bounded-counter patch for it separately rather than fold it in here.

checkpatch --strict reports no errors and no checks. It reports one "Unknown
commit id" warning, for 8270d9c21041, the only commit the changelog cites; git
was not available in the container where checkpatch ran, and the commit
resolves with the title used here.

Tool use, as Documentation/process/generated-content.rst asks. The tool was
Claude (Anthropic), driven interactively by me over one session. It read
net/smc at v7.4-rc5, located the hold across the CONFIRM wait and the
smcr_lgr_match() active-link condition behind it, proposed the per-peer key,
and wrote the hunk in this patch, the changelog, these notes, the kernel build
scripts and the namespace test (the guest init script and the netfilter helper
that drops or rewrites CLC messages in flight); it also ran the two-kernel
measurement. Four adversarial review passes by separate model instances were
run before sending and changed the key (system id alone to system id plus the
gids), the target tree (net to net-next) and several claims in the changelog
that did not survive them. I reviewed the patch and the measurement and
take responsibility for both.

 net/smc/af_smc.c | 62 ++++++++++++++++++++++++++++++++++++++++++------
 1 file changed, 55 insertions(+), 7 deletions(-)

diff --git a/net/smc/af_smc.c b/net/smc/af_smc.c
index bfc0c7e71a12..d2858e75fc1d 100644
--- a/net/smc/af_smc.c
+++ b/net/smc/af_smc.c
@@ -26,6 +26,7 @@
 #include <linux/if_vlan.h>
 #include <linux/rcupdate_wait.h>
 #include <linux/ctype.h>
+#include <linux/jhash.h>
 #include <linux/splice.h>
 
 #include <net/sock.h>
@@ -66,6 +67,34 @@ static DEFINE_MUTEX(smc_client_lgr_pending);	/* serialize link group
 						 * creation on client
 						 */
 
+/* Serialize the server-side establishment phase per peer, hashed on the
+ * fields of the proposal that smcr_lgr_match() compares: peer system id and
+ * both peer gids. The system id alone is all zero in a v2-only proposal.
+ * Keeps further connections of a peer out of smc_conn_create() until a new
+ * link group's first link is active, which is what smc_server_lgr_pending
+ * used to cover across the peer-paced CLC CONFIRM wait. See the changelog
+ * for what the key does not pin.
+ */
+#define SMC_SERVER_PEER_PENDING_BITS	6
+#define SMC_SERVER_PEER_PENDING_SIZE	BIT(SMC_SERVER_PEER_PENDING_BITS)
+static struct mutex smc_server_peer_pending[SMC_SERVER_PEER_PENDING_SIZE];
+
+static struct mutex *
+smc_server_peer_pending_lock(struct smc_clc_msg_proposal *pclc)
+{
+	struct smc_clc_v2_extension *v2_ext = NULL;
+	u32 key;
+
+	key = jhash(pclc->lcl.id_for_peer, SMC_SYSTEMID_LEN, 0);
+	key = jhash(pclc->lcl.gid, SMC_GID_SIZE, key);
+	if (smcr_indicated(pclc->hdr.typev2))
+		v2_ext = smc_get_clc_v2_ext(pclc);
+	if (v2_ext)
+		key = jhash(v2_ext->roce, SMC_GID_SIZE, key);
+
+	return &smc_server_peer_pending[key & (SMC_SERVER_PEER_PENDING_SIZE - 1)];
+}
+
 static struct workqueue_struct	*smc_tcp_ls_wq;	/* wq for tcp listen work */
 struct workqueue_struct	*smc_hs_wq;	/* wq for handshake work */
 struct workqueue_struct	*smc_close_wq;	/* wq for close work */
@@ -2462,6 +2491,7 @@ static void smc_listen_work(struct work_struct *work)
 	struct smc_clc_msg_accept_confirm *cclc;
 	struct smc_clc_msg_proposal_area *buf;
 	struct smc_clc_msg_proposal *pclc;
+	struct mutex *pending_lock = NULL;
 	struct smc_init_info *ini = NULL;
 	u8 proposal_version = SMC_V1;
 	u8 accept_version;
@@ -2524,6 +2554,12 @@ static void smc_listen_work(struct work_struct *work)
 	if (rc)
 		goto out_decl;
 
+	/* The key comes from the proposal, which has already been received and
+	 * validated, so the per-peer lock can be taken first. Lock order is
+	 * always per-peer lock before smc_server_lgr_pending.
+	 */
+	pending_lock = smc_server_peer_pending_lock(pclc);
+	mutex_lock(pending_lock);
 	mutex_lock(&smc_server_lgr_pending);
 	smc_rx_init(new_smc);
 	smc_tx_init(new_smc);
@@ -2531,18 +2567,25 @@ static void smc_listen_work(struct work_struct *work)
 	/* determine ISM or RoCE device used for connection */
 	rc = smc_listen_find_device(new_smc, pclc, ini);
 	if (rc)
-		goto out_unlock;
+		goto out_unlock_global;
 
 	/* send SMC Accept CLC message */
 	accept_version = ini->is_smcd ? ini->smcd_version : ini->smcr_version;
 	rc = smc_clc_send_accept(new_smc, ini->first_contact_local,
 				 accept_version, ini->negotiated_eid, ini);
 	if (rc)
-		goto out_unlock;
+		goto out_unlock_global;
+
+	/* Link group creation is complete, so the global lock is not needed
+	 * across the wait for the peer's CLC CONFIRM below. The per-peer lock
+	 * still keeps further connections of this peer out of
+	 * smc_conn_create() until the first link becomes active.
+	 */
+	mutex_unlock(&smc_server_lgr_pending);
 
-	/* SMC-D does not need this lock any more */
+	/* SMC-D does not need the per-peer lock past the ACCEPT either */
 	if (ini->is_smcd)
-		mutex_unlock(&smc_server_lgr_pending);
+		mutex_unlock(pending_lock);
 
 	/* receive SMC Confirm CLC message */
 	memset(buf, 0, sizeof(*buf));
@@ -2573,7 +2616,7 @@ static void smc_listen_work(struct work_struct *work)
 					    ini->first_contact_local, ini);
 		if (rc)
 			goto out_unlock;
-		mutex_unlock(&smc_server_lgr_pending);
+		mutex_unlock(pending_lock);
 	}
 	smc_conn_save_peer_info(new_smc, cclc);
 
@@ -2589,8 +2632,10 @@ static void smc_listen_work(struct work_struct *work)
 	smc_listen_out_connected(new_smc);
 	goto out_free;
 
-out_unlock:
+out_unlock_global:
 	mutex_unlock(&smc_server_lgr_pending);
+out_unlock:
+	mutex_unlock(pending_lock);
 out_decl:
 	smc_listen_decline(new_smc, rc, ini ? ini->first_contact_local : 0,
 			   proposal_version);
@@ -3463,7 +3508,10 @@ static struct pernet_operations smc_net_stat_ops = {
 
 static int __init smc_init(void)
 {
-	int rc;
+	int i, rc;
+
+	for (i = 0; i < SMC_SERVER_PEER_PENDING_SIZE; i++)
+		mutex_init(&smc_server_peer_pending[i]);
 
 	rc = register_pernet_subsys(&smc_net_ops);
 	if (rc)
-- 
2.43.0


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

only message in thread, other threads:[~2026-12-02 13:13 UTC | newest]

Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-02 13:12 [PATCH net-next] net/smc: serialize the server establishment phase per peer, not globally Ermenson Junior

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