[PATCH net v7 3/3] net/rds: defer the probe cm_id destroy to rds_wq
From: Aibing Chen
Date: Thu Dec 03 2026 - 21:22:59 EST
rds_ib_laddr_check() is called from rds_trans_get_preferred(), which runs
both on the bind path and from the passive connection path, where
rdma_cm's handler_mutex is already held:
cma_ib_req_handler()
rds_ib_cm_handle_connect()
rds_conn_create()
rds_trans_get_preferred()
rds_ib_laddr_check()
rdma_destroy_id()
mutex_lock(&id_priv->handler_mutex)
rdma_destroy_id() takes the handler mutex of the probe id, which is a
different instance of the same lock class, so lockdep reports a recursive
locking dependency as soon as a connection is established.
Destroy the probe id from rds_wq instead, so that the destroy runs without
either rds_trans_sem or handler_mutex held. The work item is allocated
before rdma_create_id(), so an allocation failure fails the address check
instead of falling back to the synchronous destroy, and the number of
pending destroys is bounded by RDS_IB_MAX_PROBE_PENDING (64). Above the
bound the check fails with -ENOMEM; callers treat that like any other
unsuccessful laddr_check().
rds_ib_exit() flushes rds_wq before the transport is torn down, so a
queued destroy cannot run after the module text is gone.
Fixes: ec16227e1414 ("RDS/IB: Infiniband transport")
Cc: stable@xxxxxxxxxxxxxxx
Signed-off-by: Aibing Chen <chenaibing@xxxxxxxxxxxxxxxxxxxx>
---
net/rds/ib.c | 53 +++++++++++++++++++++++++++++++++++++++++++--
net/rds/transport.c | 7 +++---
2 files changed, 55 insertions(+), 5 deletions(-)
diff --git a/net/rds/ib.c b/net/rds/ib.c
index 784d358fe..a3ee52fa9 100644
--- a/net/rds/ib.c
+++ b/net/rds/ib.c
@@ -391,6 +391,31 @@ static void rds6_ib_ic_info(struct socket *sock, unsigned int len,
}
#endif
+/*
+ * The probe cm_id must not be destroyed from the caller's context:
+ * rds_ib_laddr_check() may run while rdma_cm's handler_mutex is held (the
+ * passive connection path), and rdma_destroy_id() takes the handler_mutex
+ * of the probe id, which is a different instance of the same lock class.
+ * Queue the destruction on rds_wq, which runs without either that mutex
+ * or rds_trans_sem.
+ */
+struct rds_ib_probe_work {
+ struct work_struct work;
+ struct rdma_cm_id *cm_id;
+};
+
+static atomic_t rds_ib_probe_pending = ATOMIC_INIT(0);
+#define RDS_IB_MAX_PROBE_PENDING 64
+
+static void rds_ib_probe_work_fn(struct work_struct *work)
+{
+ struct rds_ib_probe_work *pw =
+ container_of(work, struct rds_ib_probe_work, work);
+
+ rdma_destroy_id(pw->cm_id);
+ kfree(pw);
+ atomic_dec(&rds_ib_probe_pending);
+}
/*
* Early RDS/IB was built to only bind to an address if there is an IPoIB
* device with that address set.
@@ -406,6 +431,7 @@ static int rds_ib_laddr_check_cm(struct net *net, const struct in6_addr *addr,
{
int ret;
struct rdma_cm_id *cm_id;
+ struct rds_ib_probe_work *pw;
#if IS_ENABLED(CONFIG_IPV6)
struct sockaddr_in6 sin6;
#endif
@@ -414,11 +440,25 @@ static int rds_ib_laddr_check_cm(struct net *net, const struct in6_addr *addr,
bool isv4;
isv4 = ipv6_addr_v4mapped(addr);
+
+ pw = kzalloc_obj(*pw);
+ if (!pw)
+ return -ENOMEM;
+
+ if (atomic_inc_return(&rds_ib_probe_pending) > RDS_IB_MAX_PROBE_PENDING) {
+ atomic_dec(&rds_ib_probe_pending);
+ kfree(pw);
+ return -ENOMEM;
+ }
+
/* Create a CMA ID restricted to IB devices and try to bind it. */
cm_id = rdma_create_id(&init_net, rds_rdma_cm_event_handler,
NULL, RDMA_PS_TCP, IB_QPT_RC);
- if (IS_ERR(cm_id))
+ if (IS_ERR(cm_id)) {
+ kfree(pw);
+ atomic_dec(&rds_ib_probe_pending);
return PTR_ERR(cm_id);
+ }
ret = rdma_restrict_node_type(cm_id, RDMA_NODE_IB_CA);
if (ret)
goto out;
@@ -484,7 +524,9 @@ static int rds_ib_laddr_check_cm(struct net *net, const struct in6_addr *addr,
cm_id->device ? cm_id->device->node_type : -1);
out:
- rdma_destroy_id(cm_id);
+ INIT_WORK(&pw->work, rds_ib_probe_work_fn);
+ pw->cm_id = cm_id;
+ queue_work(rds_wq, &pw->work);
return ret;
}
@@ -541,6 +583,13 @@ void rds_ib_exit(void)
rds_ib_destroy_nodev_conns();
rds_ib_sysctl_exit();
rds_ib_recv_exit();
+
+ /*
+ * rds_ib_laddr_check_cm() may have queued a probe destroy on rds_wq;
+ * wait for it before the module text goes away.
+ */
+ flush_workqueue(rds_wq);
+
rds_ib_mr_exit();
}
diff --git a/net/rds/transport.c b/net/rds/transport.c
index 4e667580f..cb37d9b08 100644
--- a/net/rds/transport.c
+++ b/net/rds/transport.c
@@ -105,9 +105,10 @@ struct rds_transport *rds_trans_get_preferred(struct net *net,
continue;
/*
- * ->laddr_check() may create an rdma_cm id and take
- * rdma_cm's handler_mutex. Don't hold rds_trans_sem
- * across it: the module reference keeps trans alive.
+ * ->laddr_check() creates an rdma_cm id and may reach into
+ * rdma_cm's locking. Its destruction is deferred to rds_wq,
+ * but don't hold rds_trans_sem across the check itself: the
+ * module reference keeps trans alive.
*/
up_read(&rds_trans_sem);