Linux RDMA and InfiniBand development
 help / color / mirror / Atom feed
* [PATCH net v4] net/rds: defer CM id destruction to a workqueue
@ 2026-11-22 14:51 Aibing Chen
  2026-11-23 14:52 ` sashiko-bot
  0 siblings, 1 reply; 2+ messages in thread
From: Aibing Chen @ 2026-11-22 14:51 UTC (permalink / raw)
  To: Allison Henderson
  Cc: David S . Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni,
	Simon Horman, netdev, linux-rdma, rds-devel, linux-kernel

rds_ib_laddr_check_cm() destroys the RDMA CM id it created while
rds_trans_get_preferred() holds rds_trans_sem for reading.  The bind
path then takes rds_trans_sem and afterwards handler_mutex in
rdma_destroy_id(), while the CM callback path takes handler_mutex in
cma_ib_req_handler() and afterwards rds_trans_sem through
rds_conn_create().  lockdep reports the resulting cycle.

Queue the destruction on rds_wq instead, so that rds_trans_sem is
released before handler_mutex is taken.  The work item is allocated
before the CM id is created and owns the id until rdma_destroy_id() has
run; if the allocation fails, the address check fails with -ENOMEM
rather than falling back to the synchronous destroy, which would bring
the deadlock back under memory pressure.

Deferring the destruction keeps the CM id alive after bind() returns:
until the worker runs it pins a cma_dev reference and an RDMA_PS_TCP
port, and cma_process_remove() waits for every pending destroy.  An
unprivileged user can call bind() in a loop, so bound the number of
pending probes with rds_ib_pending_probes and fail the address check
with -EAGAIN above RDS_IB_MAX_PENDING_PROBES.

rds_ib_exit() flushes rds_wq after rds_trans_unregister() so that no
queued destroy runs after the module text is gone.

Reported-by: syzbot+ccdb5058c3df2d94811c@syzkaller.appspotmail.com
Closes: https://syzkaller.appspot.com/bug?extid=ccdb5058c3df2d94811c
Suggested-by: Hillf Danton <hdanton@sina.com>
Suggested-by: Jason Gunthorpe <jgg@ziepe.ca>
Suggested-by: Allison Henderson <achender@kernel.org>
Fixes: ec16227e1414 ("RDS/IB: Infiniband transport")
Cc: stable@vger.kernel.org
Signed-off-by: Aibing Chen <chenaibing@cmss.chinamobile.com>
---
v4:
- bound the pending probe ids with an atomic counter and return -EAGAIN
  above RDS_IB_MAX_PENDING_PROBES, since each pending id pins a
  cma_dev reference and an RDMA_PS_TCP port until the worker runs.
- document that exposure in the commit message.

v3:
- allocate the work item before rdma_create_id() and fail with -ENOMEM
  instead of falling back to the synchronous destroy.
- free the work item when rdma_create_id() fails and drop the now
  redundant fallback in the out path.

v2:
- move the workqueue from RDMA core into RDS, as suggested in review.
- flush rds_wq in rds_ib_exit() so a queued destroy cannot run after
  the module text is freed.
- add the Fixes tag, drop the Tested-by tags.

 net/rds/ib.c | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 58 insertions(+), 2 deletions(-)

diff --git a/net/rds/ib.c b/net/rds/ib.c
index 4ea9838d0..c4c10e3ea 100644
--- a/net/rds/ib.c
+++ b/net/rds/ib.c
@@ -391,6 +391,30 @@ static void rds6_ib_ic_info(struct socket *sock, unsigned int len,
 }
 #endif
 
+/*
+ * Maximum number of probe cm_ids whose destruction is still pending on
+ * rds_wq.  Each of them pins a cma_dev reference and an RDMA_PS_TCP port,
+ * so the number of in-flight probes has to be bounded.
+ */
+#define RDS_IB_MAX_PENDING_PROBES	64
+
+static atomic_t rds_ib_pending_probes = ATOMIC_INIT(0);
+
+struct rds_ib_destroy_work {
+	struct work_struct work;
+	struct rdma_cm_id *cm_id;
+};
+
+static void rds_ib_destroy_id_work(struct work_struct *work)
+{
+	struct rds_ib_destroy_work *w =
+		container_of(work, struct rds_ib_destroy_work, work);
+
+	rdma_destroy_id(w->cm_id);
+	kfree(w);
+	atomic_dec(&rds_ib_pending_probes);
+}
+
 /*
  * Early RDS/IB was built to only bind to an address if there is an IPoIB
  * device with that address set.
@@ -411,14 +435,39 @@ static int rds_ib_laddr_check_cm(struct net *net, const struct in6_addr *addr,
 #endif
 	struct sockaddr_in sin;
 	struct sockaddr *sa;
+	struct rds_ib_destroy_work *w;
 	bool isv4;
 
 	isv4 = ipv6_addr_v4mapped(addr);
+
+	/*
+	 * The id is destroyed from rds_wq: rds_trans_get_preferred() calls this
+	 * function with rds_trans_sem held for reading, and rdma_destroy_id()
+	 * takes handler_mutex, which the CM callback path acquires before
+	 * taking rds_trans_sem.  Reserve a pending-probe slot first, so that a
+	 * loop of bind() calls cannot pile up ids that pin a cma_dev reference
+	 * and an RDMA_PS_TCP port.
+	 */
+	if (atomic_inc_return(&rds_ib_pending_probes) >
+	    RDS_IB_MAX_PENDING_PROBES) {
+		atomic_dec(&rds_ib_pending_probes);
+		return -EAGAIN;
+	}
+
+	w = kzalloc_obj(*w, GFP_KERNEL);
+	if (!w) {
+		atomic_dec(&rds_ib_pending_probes);
+		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)) {
+		atomic_dec(&rds_ib_pending_probes);
+		kfree(w);
 		return PTR_ERR(cm_id);
+	}
 	ret = rdma_restrict_node_type(cm_id, RDMA_NODE_IB_CA);
 	if (ret)
 		goto out;
@@ -484,7 +533,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(&w->work, rds_ib_destroy_id_work);
+	w->cm_id = cm_id;
+	queue_work(rds_wq, &w->work);
 
 	return ret;
 }
@@ -542,6 +593,11 @@ void rds_ib_exit(void)
 	rds_ib_sysctl_exit();
 	rds_ib_recv_exit();
 	rds_trans_unregister(&rds_ib_transport);
+	/*
+	 * rds_ib_laddr_check_cm() may have queued rdma_destroy_id() on rds_wq;
+	 * wait for it before the module text goes away.
+	 */
+	flush_workqueue(rds_wq);
 	rds_ib_mr_exit();
 }
 



^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH net v4] net/rds: defer CM id destruction to a workqueue
  2026-11-22 14:51 [PATCH net v4] net/rds: defer CM id destruction to a workqueue Aibing Chen
@ 2026-11-23 14:52 ` sashiko-bot
  0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-11-23 14:52 UTC (permalink / raw)
  To: Aibing Chen; +Cc: linux-rdma

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] net/rds: silent TCP fallback or wrong errno on
  rds_ib_laddr_check_cm() -EAGAIN
--

commit e574b9774f231da9aa189fbad06e4a6b85eb1372
Author: Aibing Chen <chenaibing@cmss.chinamobile.com>

net/rds: defer CM id destruction to a workqueue

This commit defers rdma_destroy_id() from rds_ib_laddr_check_cm() to rds_wq to
avoid a lockdep cycle between rds_trans_sem and handler_mutex, and bounds the
number of pending probe CM IDs to 64.

> diff --git a/net/rds/ib.c b/net/rds/ib.c
> index 4ea9838d090c3..c4c10e3eabced 100644
> --- a/net/rds/ib.c
> +++ b/net/rds/ib.c

[ ... ]

> @@ -411,14 +435,39 @@ static int rds_ib_laddr_check_cm(struct net *net, const struct in6_addr *addr,
>  #endif
>  	struct sockaddr_in sin;
>  	struct sockaddr *sa;
> +	struct rds_ib_destroy_work *w;
>  	bool isv4;
>  
>  	isv4 = ipv6_addr_v4mapped(addr);
> +
> +	/*
> +	 * The id is destroyed from rds_wq: rds_trans_get_preferred() calls this
> +	 * function with rds_trans_sem held for reading, and rdma_destroy_id()
> +	 * takes handler_mutex, which the CM callback path acquires before
> +	 * taking rds_trans_sem.  Reserve a pending-probe slot first, so that a
> +	 * loop of bind() calls cannot pile up ids that pin a cma_dev reference
> +	 * and an RDMA_PS_TCP port.
> +	 */
> +	if (atomic_inc_return(&rds_ib_pending_probes) >
> +	    RDS_IB_MAX_PENDING_PROBES) {
> +		atomic_dec(&rds_ib_pending_probes);
> +		return -EAGAIN;
> +	}

[Severity: Medium]
When rds_ib_laddr_check_cm() returns -EAGAIN (or -ENOMEM), does
rds_trans_get_preferred() silently fall back to rds_tcp_transport instead of
propagating the error?

net/rds/transport.c:rds_trans_get_preferred() {
	for (i = 0; i < RDS_TRANS_COUNT; i++) {
		trans = transports[i];

		if (trans && (trans->laddr_check(net, addr, scope_id) == 0) &&
		    (!trans->t_owner || try_module_get(trans->t_owner))) {
			ret = trans;
			break;
		}
	}
}

rds_trans_get_preferred() treats any non-zero return from trans->laddr_check()
as a non-match and continues to the next transport.  Because
rds_tcp_laddr_check() accepts any local address on the host, a socket binding
to a local IPoIB address while RDS_IB_MAX_PENDING_PROBES is exceeded will be
bound to rds_tcp_transport instead of rds_ib_transport, causing subsequent
RDMA operations such as RDS_GET_MR to fail with -EOPNOTSUPP.  In rds_bind(),
-EAGAIN is also overwritten with -EADDRNOTAVAIL when no fallback transport is
loaded, or with -ENOPROTOOPT when rs->rs_transport was preset.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261122145148.1250-1-chenaibing@cmss.chinamobile.com?part=1

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-11-23 14:52 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-22 14:51 [PATCH net v4] net/rds: defer CM id destruction to a workqueue Aibing Chen
2026-11-23 14:52 ` sashiko-bot

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