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 <erme…@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