Linux RDMA and InfiniBand development
 help / color / mirror / Atom feed
* [PATCH] RDMA/cma: Cancel the route query before dropping handler_mutex on removal
@ 2026-12-06 13:14 Jiayi Zhang
  2026-12-06 13:33 ` sashiko-bot
  0 siblings, 1 reply; 2+ messages in thread
From: Jiayi Zhang @ 2026-12-06 13:14 UTC (permalink / raw)
  To: Jason Gunthorpe, Leon Romanovsky
  Cc: Shay Drory, linux-rdma, linux-kernel, syzbot+9a9d0804227064a915d3,
	syzkaller-bugs

When an RDMA device is removed, cma_send_device_removal_put() moves the
ID to RDMA_CM_DEVICE_REMOVAL, delivers the event to the ULP, drops
handler_mutex and then calls cma_cancel_operation() with the previous
state.

The ULP may destroy the ID as soon as handler_mutex is dropped; ucma
does this from its close_work. destroy_id_handler_unlock() sees
RDMA_CM_DEVICE_REMOVAL, leaves the cancel to the removal thread, and
_destroy_id() calls cma_release_dev(), which sets id_priv->id.device to
NULL. The reference held by the removal thread keeps id_priv alive, but
if the previous state was RDMA_CM_ROUTE_QUERY, cma_cancel_route() then
reads the cleared device:

  KASAN: null-ptr-deref in range [0x0000000000000798-0x000000000000079f]
  RIP: 0010:rdma_cap_ib_sa include/rdma/ib_verbs.h:3563 [inline]
  RIP: 0010:cma_cancel_route drivers/infiniband/core/cma.c:1924 [inline]
  RIP: 0010:cma_cancel_operation drivers/infiniband/core/cma.c:1979 [inline]
  RIP: 0010:cma_send_device_removal_put drivers/infiniband/core/cma.c:5356 [inline]
  RIP: 0010:cma_process_remove+0x6ae/0xc50 drivers/infiniband/core/cma.c:5372
  Call Trace:
   cma_remove_one+0x18a/0x340 drivers/infiniband/core/cma.c:5479
   ...
   nldev_dellink+0x3a4/0x440 drivers/infiniband/core/nldev.c:1906

Before commit 3647a28de1ad ("RDMA/cma: Using the standard locking
pattern when delivering the removal event"), the removal path cancelled
the operation before it delivered the event, so a destroy that the
event itself triggers, such as ucma's close_work, could not run first.
That commit moved the cancel to after handler_mutex is dropped. Since commit
889d916b6f8a ("RDMA/core: Don't access cm_id after its destruction"),
cma_release_dev() also clears id.device, which turns the late cancel
into a NULL pointer dereference.

Cancel the route query before handler_mutex is dropped. rdma_destroy_id()
takes handler_mutex before it releases the device, so id.device is still
set at that point. ib_sa_cancel_query() does not wait for the query
callback, and the callback does not take handler_mutex (it queues the
work that does), so it can be called with the mutex held.
rdma_addr_cancel() waits for the address resolution work, which takes
handler_mutex, so it stays after the unlock.

The other case of cma_cancel_operation(), RDMA_CM_LISTEN, only acts on
a wildcard listener. A user listener on the device's list has a
specific address, and the wildcard per-device listeners on that list
return non-zero from cma_listen_handler() for the removal event, so
they are destroyed above and never get here. Calling
rdma_addr_cancel() directly therefore does not change the behaviour
for that state.

Fixes: 3647a28de1ad ("RDMA/cma: Using the standard locking pattern when delivering the removal event")
Fixes: 889d916b6f8a ("RDMA/core: Don't access cm_id after its destruction")
Reported-by: syzbot+9a9d0804227064a915d3@syzkaller.appspotmail.com
Closes: https://lore.kernel.org/all/6b14cd41.7375a9b9.1d867b.10ff.GAE@google.com/
Signed-off-by: Jiayi Zhang <zhangjiayi1014@gmail.com>
---
The patch applies to rdma/for-next as well; cma.c there is the same as
in mainline.

syzbot posted the report with a C reproducer; the tests below were done
before that, without it. Tested in QEMU (2 CPUs, the
syzbot config, kernel 693c2e04e3f1) with a test program that creates an
rxe device on eth0 through RDMA netlink, resolves 64 ucma IDs to the
local address, and then issues RESOLVE_ROUTE on all of them in one
thread while another thread deletes the rxe device, in a loop. Without
a delay the window is too narrow (no crash in 3 instances of 10
minutes). Two test-only delays widen it: msleep(50) after the unlock in
cma_send_device_removal_put() when the old state is
RDMA_CM_ROUTE_QUERY, and msleep(20) at the start of cma_work_handler()
for route resolution work, so that more IDs are still in
RDMA_CM_ROUTE_QUERY when the device goes away. With the delays the same
null-ptr-deref (same address and call path) happened in all 5
instances; with this patch and the same delays, in none of 5 instances
of 10 minutes, and there was no other crash. The config has lockdep
enabled. rxe is RoCE, so the test covers the NULL dereference but not
ib_sa_cancel_query() with handler_mutex held; that part rests on the
code: it only takes spinlocks and queues the callback.

AI tools assisted with the analysis, the code and the changelog.

 drivers/infiniband/core/cma.c |   11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)

diff --git a/drivers/infiniband/core/cma.c b/drivers/infiniband/core/cma.c
index 337a49d..6b36d98 100644
--- a/drivers/infiniband/core/cma.c
+++ b/drivers/infiniband/core/cma.c
@@ -5347,13 +5347,22 @@ static void cma_send_device_removal_put(struct rdma_id_private *id_priv)
 		_destroy_id(id_priv, state);
 		return;
 	}
+
+	/*
+	 * Cancel any pending SA route query while handler_mutex is still held
+	 * so a concurrent destroy_id_handler_unlock() cannot clear
+	 * id_priv->id.device via cma_release_dev() first.
+	 */
+	if (state == RDMA_CM_ROUTE_QUERY)
+		cma_cancel_route(id_priv);
 	mutex_unlock(&id_priv->handler_mutex);
 
 	/*
 	 * If this races with destroy then the thread that first assigns state
 	 * to a destroying does the cancel.
 	 */
-	cma_cancel_operation(id_priv, state);
+	if (state == RDMA_CM_ADDR_QUERY)
+		rdma_addr_cancel(&id_priv->id.route.addr.dev_addr);
 	cma_id_put(id_priv);
 }
 
-- 
2.53.0


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

end of thread, other threads:[~2026-12-06 13:33 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-06 13:14 [PATCH] RDMA/cma: Cancel the route query before dropping handler_mutex on removal Jiayi Zhang
2026-12-06 13:33 ` sashiko-bot

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