Netdev List
 help / color / mirror / Atom feed
From: Aibing Chen <chenaibing@cmss.chinamobile.com>
To: Allison Henderson <achender@kernel.org>
Cc: netdev@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: [PATCH net v7 3/3] net/rds: defer the probe cm_id destroy to rds_wq
Date: Fri,  4 Dec 2026 10:22:44 +0800	[thread overview]
Message-ID: <20261204022245.381-4-chenaibing@cmss.chinamobile.com> (raw)
In-Reply-To: <20261204022245.381-1-chenaibing@cmss.chinamobile.com>

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@vger.kernel.org
Signed-off-by: Aibing Chen <chenaibing@cmss.chinamobile.com>
---
 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);
 



  parent reply	other threads:[~2026-12-04  2:22 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-04  2:22 [PATCH net v7 0/3] net/rds: fix the probe cm_id lock ordering Aibing Chen
2026-12-04  2:22 ` [PATCH net v7 1/3] net/rds: register the IB transport last Aibing Chen
2026-12-05  3:05   ` netdev-bot+sashiko
2026-12-04  2:22 ` [PATCH net v7 2/3] net/rds: drop rds_trans_sem around laddr_check() Aibing Chen
2026-12-05  3:05   ` netdev-bot+sashiko
2026-12-04  2:22 ` Aibing Chen [this message]
2026-12-05  3:05   ` [PATCH net v7 3/3] net/rds: defer the probe cm_id destroy to rds_wq netdev-bot+sashiko

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261204022245.381-4-chenaibing@cmss.chinamobile.com \
    --to=chenaibing@cmss.chinamobile.com \
    --cc=achender@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=netdev@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox