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 --- 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