Linux RDMA and InfiniBand development
 help / color / mirror / Atom feed
* Re: [PATCH v3 0/3] rpcrdma: Fix device removal races and endpoint cleanup
  2026-11-24  2:18 [PATCH v3 0/3] rpcrdma: Fix device removal races and endpoint cleanup Li Xiasong
@ 2026-11-24  2:13 ` sashiko-bot
  2026-11-24  2:18 ` [PATCH v3 1/3] rpcrdma: Serialize notification registration with device removal Li Xiasong
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: sashiko-bot @ 2026-11-24  2:13 UTC (permalink / raw)
  To: Li Xiasong; +Cc: linux-rdma

Sashiko has reviewed this 3-patch series and found no issues.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261124021842.130141-1-lixiasong1@huawei.com

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

* [PATCH v3 0/3] rpcrdma: Fix device removal races and endpoint cleanup
@ 2026-11-24  2:18 Li Xiasong
  2026-11-24  2:13 ` sashiko-bot
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: Li Xiasong @ 2026-11-24  2:18 UTC (permalink / raw)
  To: Chuck Lever, Jeff Layton, NeilBrown, Olga Kornievskaia, Dai Ngo,
	Tom Talpey, Trond Myklebust, Anna Schumaker
  Cc: linux-nfs, linux-rdma, stable, yuehaibing, zhangchangzhong,
	weiyongjun1

This series fixes races and endpoint cleanup issues in the RPC-over-RDMA
device removal paths.

Notification registration is not serialized with device removal. Removal
can free the per-device private data while registration is still using it,
or finish walking the notification array before a new registration is
published. In addition, the notification walk does not hold references to
the objects containing the notifications, allowing concurrent unregister
and teardown to free an object before its removal callback is invoked.

On the client side, the device removal callback only forces a transport
disconnect. A connect worker waiting for re_connect_status to change can
remain asleep, preventing endpoint cleanup. Removal that races with
connection establishment can also be overwritten by a late ESTABLISHED
event, and releasing the endpoint from a removal callback or address
change can free its CM ID context before CMA has stopped dispatching
events to it.

Patch 1 serializes notification registration with the transition to device
removal and protects access to the per-device private data. The critical
section runs in a NOIO allocation scope so reclaim cannot wait for RPC I/O
on a transport whose reconnect needs the same mutex.

Patch 2 serializes removal callbacks with unregister. Short removal
callbacks run under the device mutex, so an owner cannot be unregistered
and freed while its callback is running. Callbacks that must release a
reference do so outside the mutex through an independently owned release
callback, keeping a final transport put from waiting on a connect worker
that needs the mutex.

Patch 3 records device removal in the endpoint connection status and wakes
a waiting connect worker. Atomic state transitions give -ENODEV precedence
and force an established connection to disconnect without a late
ESTABLISHED event masking the error. It drops the connection-management
callback get/put pairs and destroys the CM ID in the endpoint destructor,
before freeing its context, so that active and future callbacks are fenced.
Device removal during setup is translated to -ENOTCONN so pending RPCs
retry.

The patches are ordered by dependency: patch 2 uses the serialization
introduced by patch 1, and patch 3 relies on the callback lifetime
protection added by patch 2.

Changes since v2:
- Run the registration critical section under memalloc_noio_save()/
  memalloc_noio_restore() so the module-wide mutex is never held across
  reclaim that may wait for RPC I/O.
- Drop the rn_get/rn_put callbacks and rpcrdma_notification_ref. Removal
  callbacks now run under the device mutex (they are short and do not
  sleep), so a pinned owner is unnecessary. Transport references are moved
  back into verbs.c and svc_rdma_transport.c and released outside the
  mutex via a new rpcrdma_notification_release output parameter.
- Make rpcrdma_ep_disconnect() use a try_cmpxchg loop so a connection that
  is already established transitions to the error state and is forced to
  disconnect, instead of leaving re_connect_status at 1.
- Have RDMA_CM_EVENT_ESTABLISHED return early when the status is already
  non-zero, avoiding a forced disconnect on an unpinned transport.
- Translate the -ENODEV connection status to -ENOTCONN so pending RPCs
  retry on a hard mount when the device disappears during setup.
- Link to v2: https://lore.kernel.org/all/20261103032758.1302493-1-lixiasong1@huawei.com/

Li Xiasong (3):
  rpcrdma: Serialize notification registration with device removal
  rpcrdma: Serialize removal callbacks with unregister
  xprtrdma: Wake connect worker on device removal

 include/linux/sunrpc/rdma_rn.h           |  27 ++++-
 net/sunrpc/xprtrdma/ib_client.c          |  86 +++++++++++---
 net/sunrpc/xprtrdma/svc_rdma_transport.c |  24 +++-
 net/sunrpc/xprtrdma/transport.c          |   2 +-
 net/sunrpc/xprtrdma/verbs.c              | 138 +++++++++++++++--------
 5 files changed, 208 insertions(+), 69 deletions(-)

-- 
2.34.1


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

* [PATCH v3 1/3] rpcrdma: Serialize notification registration with device removal
  2026-11-24  2:18 [PATCH v3 0/3] rpcrdma: Fix device removal races and endpoint cleanup Li Xiasong
  2026-11-24  2:13 ` sashiko-bot
@ 2026-11-24  2:18 ` Li Xiasong
  2026-11-24  2:18 ` [PATCH v3 2/3] rpcrdma: Serialize removal callbacks with unregister Li Xiasong
  2026-11-24  2:18 ` [PATCH v3 3/3] xprtrdma: Wake connect worker on device removal Li Xiasong
  3 siblings, 0 replies; 5+ messages in thread
From: Li Xiasong @ 2026-11-24  2:18 UTC (permalink / raw)
  To: Chuck Lever, Jeff Layton, NeilBrown, Olga Kornievskaia, Dai Ngo,
	Tom Talpey, Trond Myklebust, Anna Schumaker
  Cc: linux-nfs, linux-rdma, stable, yuehaibing, zhangchangzhong,
	weiyongjun1

Registration can race with device removal between the client-data
lookup, the removing-state check, XArray publication and device
reference acquisition. Serialize these operations and client-data
teardown with a module-wide mutex, keeping callbacks and completion
waits outside it.

Enter a NOIO allocation scope before taking the mutex to prevent reclaim
from waiting for RPC I/O on a transport whose reconnect needs the same
mutex.

Fixes: 7e86845a0346 ("rpcrdma: Implement generic device removal")
Signed-off-by: Li Xiasong <lixiasong1@huawei.com>
---
 net/sunrpc/xprtrdma/ib_client.c | 47 +++++++++++++++++++++++++--------
 1 file changed, 36 insertions(+), 11 deletions(-)

diff --git a/net/sunrpc/xprtrdma/ib_client.c b/net/sunrpc/xprtrdma/ib_client.c
index 188f7a13397f..23fbaf406a22 100644
--- a/net/sunrpc/xprtrdma/ib_client.c
+++ b/net/sunrpc/xprtrdma/ib_client.c
@@ -9,6 +9,8 @@
 #include <linux/types.h>
 #include <linux/kref.h>
 #include <linux/completion.h>
+#include <linux/mutex.h>
+#include <linux/sched/mm.h>
 
 #include <linux/sunrpc/svc_rdma.h>
 #include <linux/sunrpc/rdma_rn.h>
@@ -28,6 +30,11 @@ struct rpcrdma_device {
 #define RPCRDMA_RD_F_REMOVING	(0)
 
 static struct ib_client rpcrdma_ib_client;
+/*
+ * Serialize registration with the removal transition and client-data teardown.
+ * Invoke rn_done callbacks and wait for completion outside this mutex.
+ */
+static DEFINE_MUTEX(rpcrdma_device_mutex);
 
 /*
  * Listeners have no associated device, so we never register them.
@@ -56,33 +63,44 @@ static struct rpcrdma_device *rpcrdma_get_client_data(struct ib_device *device)
  * cleared again if xa_alloc fails), so the @rn may safely be
  * passed to rpcrdma_rn_unregister() without a separate
  * registered/unregistered flag in the caller.
+ *
+ * Context: May sleep. Takes rpcrdma_device_mutex in a NOIO allocation scope.
  */
 int rpcrdma_rn_register(struct ib_device *device,
 			struct rpcrdma_notification *rn,
 			void (*done)(struct rpcrdma_notification *rn))
 {
-	struct rpcrdma_device *rd = rpcrdma_get_client_data(device);
+	struct rpcrdma_device *rd;
+	unsigned int noio_flags;
+	int ret = -ENETUNREACH;
 
+	/* Reclaim must not wait for RPC I/O on a transport whose reconnect
+	 * needs this mutex to register its notification.
+	 */
+	noio_flags = memalloc_noio_save();
+	mutex_lock(&rpcrdma_device_mutex);
+	rd = rpcrdma_get_client_data(device);
 	if (!rd || test_bit(RPCRDMA_RD_F_REMOVING, &rd->rd_flags))
-		return -ENETUNREACH;
+		goto out_unlock;
 
 	/*
-	 * Arm rn_done before xa_alloc() publishes @rn: once @rn is
-	 * visible in rd_xa, a concurrent rpcrdma_remove_one() can
-	 * call rn->rn_done(), so the pointer must already be set.
-	 *
-	 * Restore NULL if xa_alloc() fails. rn_done doubles as the
-	 * registration sentinel for rpcrdma_rn_unregister(); a stale
-	 * value would unregister an @rn that was never inserted.
+	 * The removal walk cannot see @rn until registration drops the
+	 * device mutex. Clear rn_done if xa_alloc() fails so unregister
+	 * does not treat @rn as registered.
 	 */
 	rn->rn_done = done;
 	if (xa_alloc(&rd->rd_xa, &rn->rn_index, rn, xa_limit_32b, GFP_KERNEL) < 0) {
 		rn->rn_done = NULL;
-		return -ENOMEM;
+		ret = -ENOMEM;
+		goto out_unlock;
 	}
 	kref_get(&rd->rd_kref);
 	trace_rpcrdma_client_register(device, rn);
-	return 0;
+	ret = 0;
+out_unlock:
+	mutex_unlock(&rpcrdma_device_mutex);
+	memalloc_noio_restore(noio_flags);
+	return ret;
 }
 
 static void rpcrdma_rn_release(struct kref *kref)
@@ -163,6 +181,8 @@ static int rpcrdma_add_one(struct ib_device *device)
  *
  * Upon return, all transports associated with @device have divested
  * themselves from IB hardware resources.
+ *
+ * Context: May sleep. Takes rpcrdma_device_mutex.
  */
 static void rpcrdma_remove_one(struct ib_device *device,
 			       void *client_data)
@@ -173,7 +193,9 @@ static void rpcrdma_remove_one(struct ib_device *device,
 
 	trace_rpcrdma_client_remove_one(device);
 
+	mutex_lock(&rpcrdma_device_mutex);
 	set_bit(RPCRDMA_RD_F_REMOVING, &rd->rd_flags);
+	mutex_unlock(&rpcrdma_device_mutex);
 	xa_for_each(&rd->rd_xa, index, rn)
 		rn->rn_done(rn);
 
@@ -187,8 +209,11 @@ static void rpcrdma_remove_one(struct ib_device *device,
 	}
 
 	trace_rpcrdma_client_remove_one_done(device);
+	mutex_lock(&rpcrdma_device_mutex);
+	ib_set_client_data(device, &rpcrdma_ib_client, NULL);
 	xa_destroy(&rd->rd_xa);
 	kfree(rd);
+	mutex_unlock(&rpcrdma_device_mutex);
 }
 
 static struct ib_client rpcrdma_ib_client = {
-- 
2.34.1


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

* [PATCH v3 2/3] rpcrdma: Serialize removal callbacks with unregister
  2026-11-24  2:18 [PATCH v3 0/3] rpcrdma: Fix device removal races and endpoint cleanup Li Xiasong
  2026-11-24  2:13 ` sashiko-bot
  2026-11-24  2:18 ` [PATCH v3 1/3] rpcrdma: Serialize notification registration with device removal Li Xiasong
@ 2026-11-24  2:18 ` Li Xiasong
  2026-11-24  2:18 ` [PATCH v3 3/3] xprtrdma: Wake connect worker on device removal Li Xiasong
  3 siblings, 0 replies; 5+ messages in thread
From: Li Xiasong @ 2026-11-24  2:18 UTC (permalink / raw)
  To: Chuck Lever, Jeff Layton, NeilBrown, Olga Kornievskaia, Dai Ngo,
	Tom Talpey, Trond Myklebust, Anna Schumaker
  Cc: linux-nfs, linux-rdma, stable, yuehaibing, zhangchangzhong,
	weiyongjun1

The device removal walk does not pin notification owners, so unregister
can remove and free an owner before its callback is invoked. Run short
removal callbacks under the device mutex and serialize unregister with
that mutex.

Pin the server transport with kref_get_unless_zero() and close it outside
the mutex using svc_xprt_close(). This avoids recursive unregister while
preserving direct deletion of idle transports if a pool loses its last
worker. Wake both SQ waitqueues before dropping the reference, and let
zero-ref transports finish their existing final-release path.

Pin the client transport during its callback and release the reference
outside the mutex, since the final xprt_put can wait for a connect
worker that needs the mutex.

Fixes: 7e86845a0346 ("rpcrdma: Implement generic device removal")
Signed-off-by: Li Xiasong <lixiasong1@huawei.com>
---
 include/linux/sunrpc/rdma_rn.h           | 27 +++++++++++++--
 net/sunrpc/xprtrdma/ib_client.c          | 43 +++++++++++++++++++-----
 net/sunrpc/xprtrdma/svc_rdma_transport.c | 24 +++++++++++--
 net/sunrpc/xprtrdma/verbs.c              | 22 ++++++++++--
 4 files changed, 101 insertions(+), 15 deletions(-)

diff --git a/include/linux/sunrpc/rdma_rn.h b/include/linux/sunrpc/rdma_rn.h
index 7d032ca057af..2861122bfd7a 100644
--- a/include/linux/sunrpc/rdma_rn.h
+++ b/include/linux/sunrpc/rdma_rn.h
@@ -9,16 +9,37 @@
 #include <rdma/ib_verbs.h>
 
 /**
- * rpcrdma_notification - request removal notification
+ * struct rpcrdma_notification_release - cleanup after a removal callback
+ * @release: optional cleanup to invoke outside the device mutex
+ * @data: independently owned argument passed to @release
+ *
+ * Initialized to zero before rn_done(). The callback may pass an independent
+ * reference to @release, which runs after the device mutex is dropped. The
+ * hook owns that reference and may drop it on its last use of @data, even if
+ * that frees the object before the hook returns. Neither @release nor @data
+ * may depend on the notification remaining registered or its owner staying
+ * alive beyond the lifetime guaranteed by the reference.
+ */
+struct rpcrdma_notification_release {
+	void			(*release)(void *data);
+	void			*data;
+};
+
+/**
+ * struct rpcrdma_notification - request removal notification
+ * @rn_done: device removal callback
+ * @rn_index: index of this notification in the device's XArray
  */
 struct rpcrdma_notification {
-	void			(*rn_done)(struct rpcrdma_notification *rn);
+	void			(*rn_done)(struct rpcrdma_notification *rn,
+					   struct rpcrdma_notification_release *release);
 	u32			rn_index;
 };
 
 int rpcrdma_rn_register(struct ib_device *device,
 			struct rpcrdma_notification *rn,
-			void (*done)(struct rpcrdma_notification *rn));
+			void (*done)(struct rpcrdma_notification *rn,
+				     struct rpcrdma_notification_release *release));
 void rpcrdma_rn_unregister(struct ib_device *device,
 			   struct rpcrdma_notification *rn);
 int rpcrdma_ib_client_register(void);
diff --git a/net/sunrpc/xprtrdma/ib_client.c b/net/sunrpc/xprtrdma/ib_client.c
index 23fbaf406a22..c44cbbad63ca 100644
--- a/net/sunrpc/xprtrdma/ib_client.c
+++ b/net/sunrpc/xprtrdma/ib_client.c
@@ -31,8 +31,8 @@ struct rpcrdma_device {
 
 static struct ib_client rpcrdma_ib_client;
 /*
- * Serialize registration with the removal transition and client-data teardown.
- * Invoke rn_done callbacks and wait for completion outside this mutex.
+ * Serialize registration, notification callbacks, unregister and client-data
+ * teardown. Reference release and completion waits run outside this mutex.
  */
 static DEFINE_MUTEX(rpcrdma_device_mutex);
 
@@ -54,6 +54,14 @@ static struct rpcrdma_device *rpcrdma_get_client_data(struct ib_device *device)
  * @rn: notification object that wishes to be notified
  * @done: callback to notify caller of device removal
  *
+ * @done runs under rpcrdma_device_mutex, serialized with unregister. It must
+ * not sleep or call the registration interfaces. The owner must unregister
+ * before freeing @rn or any resources accessed by @done.
+ *
+ * @done may return an independently owned reference in the zero-initialized
+ * &struct rpcrdma_notification_release. Its release callback runs outside
+ * the mutex and must not access @rn or unpinned owner resources.
+ *
  * Returns zero on success. The callback in rn_done is guaranteed
  * to be invoked when the device is removed, unless this notification
  * is unregistered first.
@@ -68,7 +76,8 @@ static struct rpcrdma_device *rpcrdma_get_client_data(struct ib_device *device)
  */
 int rpcrdma_rn_register(struct ib_device *device,
 			struct rpcrdma_notification *rn,
-			void (*done)(struct rpcrdma_notification *rn))
+			void (*done)(struct rpcrdma_notification *rn,
+				     struct rpcrdma_notification_release *release))
 {
 	struct rpcrdma_device *rd;
 	unsigned int noio_flags;
@@ -120,14 +129,22 @@ static void rpcrdma_rn_release(struct kref *kref)
  * It is safe to call this on an @rn whose registration never
  * completed or failed; rn_done == NULL is treated as
  * never-registered and the call is a no-op.
+ * Upon return, rn_done() is not running and cannot start for @rn.
+ * Resources used by rn_done() must remain valid until this returns.
+ * This does not wait for the independent release hook, which may still
+ * run or start after this returns.
+ *
+ * Context: May sleep. Takes rpcrdma_device_mutex.
  */
 void rpcrdma_rn_unregister(struct ib_device *device,
 			   struct rpcrdma_notification *rn)
 {
-	struct rpcrdma_device *rd = rpcrdma_get_client_data(device);
+	struct rpcrdma_device *rd;
 
+	mutex_lock(&rpcrdma_device_mutex);
+	rd = rpcrdma_get_client_data(device);
 	if (!rd)
-		return;
+		goto out_unlock;
 
 	/*
 	 * rn_done is the registration sentinel: rpcrdma_rn_register
@@ -141,12 +158,14 @@ void rpcrdma_rn_unregister(struct ib_device *device,
 	 * rd_kref.
 	 */
 	if (!rn->rn_done)
-		return;
+		goto out_unlock;
 	rn->rn_done = NULL;
 
 	trace_rpcrdma_client_unregister(device, rn);
 	xa_erase(&rd->rd_xa, rn->rn_index);
 	kref_put(&rd->rd_kref, rpcrdma_rn_release);
+out_unlock:
+	mutex_unlock(&rpcrdma_device_mutex);
 }
 
 /**
@@ -195,9 +214,17 @@ static void rpcrdma_remove_one(struct ib_device *device,
 
 	mutex_lock(&rpcrdma_device_mutex);
 	set_bit(RPCRDMA_RD_F_REMOVING, &rd->rd_flags);
+	xa_for_each(&rd->rd_xa, index, rn) {
+		struct rpcrdma_notification_release release = {};
+
+		rn->rn_done(rn, &release);
+		if (!release.release)
+			continue;
+		mutex_unlock(&rpcrdma_device_mutex);
+		release.release(release.data);
+		mutex_lock(&rpcrdma_device_mutex);
+	}
 	mutex_unlock(&rpcrdma_device_mutex);
-	xa_for_each(&rd->rd_xa, index, rn)
-		rn->rn_done(rn);
 
 	/*
 	 * Wait only if there are still outstanding notification
diff --git a/net/sunrpc/xprtrdma/svc_rdma_transport.c b/net/sunrpc/xprtrdma/svc_rdma_transport.c
index f949601b2144..003a9259a51a 100644
--- a/net/sunrpc/xprtrdma/svc_rdma_transport.c
+++ b/net/sunrpc/xprtrdma/svc_rdma_transport.c
@@ -43,6 +43,7 @@
  */
 
 #include <linux/interrupt.h>
+#include <linux/kref.h>
 #include <linux/module.h>
 #include <linux/sched.h>
 #include <linux/slab.h>
@@ -414,14 +415,33 @@ static struct svc_xprt *svc_rdma_create(struct svc_serv *serv,
 	return &cma_xprt->sc_xprt;
 }
 
-static void svc_rdma_xprt_done(struct rpcrdma_notification *rn)
+/* Run outside rpcrdma_device_mutex: close and the final put can unregister. */
+static void svc_rdma_xprt_close(void *data)
+{
+	struct svc_xprt *xprt = data;
+	struct svcxprt_rdma *rdma = container_of(xprt, struct svcxprt_rdma,
+						 sc_xprt);
+
+	/* Idle transports must close even if a pool loses its last worker. */
+	svc_xprt_close(xprt);
+	wake_up_all(&rdma->sc_sq_ticket_wait);
+	wake_up_all(&rdma->sc_send_wait);
+	svc_xprt_put(xprt);
+}
+
+static void svc_rdma_xprt_done(struct rpcrdma_notification *rn,
+			       struct rpcrdma_notification_release *release)
 {
 	struct svcxprt_rdma *rdma = container_of(rn, struct svcxprt_rdma,
 						 sc_rn);
 	struct rdma_cm_id *id = rdma->sc_cm_id;
 
 	trace_svcrdma_device_removal(id);
-	svc_xprt_close(&rdma->sc_xprt);
+	/* A zero refcount means svc_rdma_free() is already unregistering. */
+	if (!kref_get_unless_zero(&rdma->sc_xprt.xpt_ref))
+		return;
+	release->release = svc_rdma_xprt_close;
+	release->data = &rdma->sc_xprt;
 }
 
 /*
diff --git a/net/sunrpc/xprtrdma/verbs.c b/net/sunrpc/xprtrdma/verbs.c
index 620afff79eff..b8dfdabd8a5a 100644
--- a/net/sunrpc/xprtrdma/verbs.c
+++ b/net/sunrpc/xprtrdma/verbs.c
@@ -289,12 +289,30 @@ rpcrdma_cm_event_handler(struct rdma_cm_id *id, struct rdma_cm_event *event)
 	return 0;
 }
 
-static void rpcrdma_ep_removal_done(struct rpcrdma_notification *rn)
+/* Drop the reference acquired by rpcrdma_ep_removal_done() outside the mutex. */
+static void rpcrdma_xprt_removal_put(void *data)
+{
+	xprt_put(data);
+}
+
+static void rpcrdma_ep_removal_done(struct rpcrdma_notification *rn,
+				    struct rpcrdma_notification_release *release)
 {
 	struct rpcrdma_ep *ep = container_of(rn, struct rpcrdma_ep, re_rn);
+	struct rpc_xprt *xprt;
 
 	trace_xprtrdma_device_removal(ep->re_id);
-	xprt_force_disconnect(ep->re_xprt);
+	/*
+	 * Pin the transport while the removal callback runs. Release the
+	 * reference outside rpcrdma_device_mutex: a final xprt_put() can wait
+	 * for the connect worker, which may need the mutex to unregister.
+	 */
+	xprt = xprt_get(ep->re_xprt);
+	if (!xprt)
+		return;
+	release->release = rpcrdma_xprt_removal_put;
+	release->data = xprt;
+	rpcrdma_force_disconnect(ep);
 }
 
 static struct rdma_cm_id *rpcrdma_create_id(struct rpcrdma_xprt *r_xprt,
-- 
2.34.1


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

* [PATCH v3 3/3] xprtrdma: Wake connect worker on device removal
  2026-11-24  2:18 [PATCH v3 0/3] rpcrdma: Fix device removal races and endpoint cleanup Li Xiasong
                   ` (2 preceding siblings ...)
  2026-11-24  2:18 ` [PATCH v3 2/3] rpcrdma: Serialize removal callbacks with unregister Li Xiasong
@ 2026-11-24  2:18 ` Li Xiasong
  3 siblings, 0 replies; 5+ messages in thread
From: Li Xiasong @ 2026-11-24  2:18 UTC (permalink / raw)
  To: Chuck Lever, Jeff Layton, NeilBrown, Olga Kornievskaia, Dai Ngo,
	Tom Talpey, Trond Myklebust, Anna Schumaker
  Cc: linux-nfs, linux-rdma, stable, yuehaibing, zhangchangzhong,
	weiyongjun1

Device removal only forces a transport disconnect, leaving a connect
worker asleep while re_connect_status is zero and blocking endpoint
cleanup.

Set -ENODEV and wake pending connects on removal. Give -ENODEV
precedence over other failures so late CM events cannot mask device
removal. Force established connections to disconnect and use atomic
transitions to prevent late ESTABLISHED events from overwriting errors.
Handle REJECTED with an atomic status update. Wake the connect worker
if the connection is still pending, or force an established connection
to disconnect without relying on CQ flushes.

The final endpoint put can free id->context before rdma_destroy_id()
synchronizes with CM callbacks. Destroy the CM ID in the endpoint
destructor before freeing its context to close this window. Remove the
CM callback get/put pairs so final puts occur outside CM callbacks.
Initialize the connect waitqueue before notification registration.

If transport pinning fails, fail and wake a pending connect under the
device mutex before skipping the notification. The final transport put
can be waiting for that worker to release XPRT_LOCKED.

Translate -ENODEV from the connection status to -ENOTCONN so pending
RPCs retry on a hard mount when the device disappears during setup.

Fixes: 3f4eb9ff9234 ("xprtrdma: Handle device removal outside of the CM event handler")
Signed-off-by: Li Xiasong <lixiasong1@huawei.com>
---
 net/sunrpc/xprtrdma/transport.c |   2 +-
 net/sunrpc/xprtrdma/verbs.c     | 120 ++++++++++++++++++++------------
 2 files changed, 75 insertions(+), 47 deletions(-)

diff --git a/net/sunrpc/xprtrdma/transport.c b/net/sunrpc/xprtrdma/transport.c
index e38637c2ec78..88bf9c9d9cf5 100644
--- a/net/sunrpc/xprtrdma/transport.c
+++ b/net/sunrpc/xprtrdma/transport.c
@@ -488,7 +488,7 @@ xprt_rdma_connect(struct rpc_xprt *xprt, struct rpc_task *task)
 	WARN_ON_ONCE(!xprt_lock_connect(xprt, task, r_xprt));
 
 	delay = 0;
-	if (ep && ep->re_connect_status != 0) {
+	if (ep && READ_ONCE(ep->re_connect_status)) {
 		delay = xprt_reconnect_delay(xprt);
 		xprt_reconnect_backoff(xprt, RPCRDMA_INIT_REEST_TO);
 	}
diff --git a/net/sunrpc/xprtrdma/verbs.c b/net/sunrpc/xprtrdma/verbs.c
index b8dfdabd8a5a..1f2a3cea073f 100644
--- a/net/sunrpc/xprtrdma/verbs.c
+++ b/net/sunrpc/xprtrdma/verbs.c
@@ -75,7 +75,7 @@ static void rpcrdma_reps_unmap(struct rpcrdma_xprt *r_xprt);
 static void rpcrdma_mrs_create(struct rpcrdma_xprt *r_xprt);
 static void rpcrdma_mrs_destroy(struct rpcrdma_xprt *r_xprt);
 static void rpcrdma_ep_get(struct rpcrdma_ep *ep);
-static int rpcrdma_ep_put(struct rpcrdma_ep *ep);
+static void rpcrdma_ep_put(struct rpcrdma_ep *ep);
 static struct rpcrdma_regbuf *
 rpcrdma_regbuf_alloc_node(size_t size, enum dma_data_direction direction,
 			  int node);
@@ -124,6 +124,23 @@ void rpcrdma_force_disconnect(struct rpcrdma_ep *ep)
 		xprt_force_disconnect(ep->re_xprt);
 }
 
+/* Device removal takes precedence over other connection failures. */
+static void rpcrdma_ep_disconnect(struct rpcrdma_ep *ep, int status,
+				  bool force_disconnect)
+{
+	int old = READ_ONCE(ep->re_connect_status);
+
+	do {
+		if (old == -ENODEV || (old < 0 && status != -ENODEV))
+			return;
+	} while (!try_cmpxchg(&ep->re_connect_status, &old, status));
+
+	if (!old)
+		wake_up_all(&ep->re_connect_wait);
+	else if (old == 1 && force_disconnect)
+		rpcrdma_force_disconnect(ep);
+}
+
 /**
  * rpcrdma_flush_disconnect - Disconnect on flushed completion
  * @r_xprt: transport to disconnect
@@ -222,13 +239,14 @@ static void rpcrdma_update_cm_private(struct rpcrdma_ep *ep,
  * @id: rdma_cm_id on which an event has occurred
  * @event: details of the event
  *
- * Called with @id's mutex held. Returns 1 if caller should
- * destroy @id, otherwise 0.
+ * Called with @id's mutex held. Always returns 0: the endpoint owner
+ * destroys @id and waits for these callbacks before freeing @id's context.
  */
 static int
 rpcrdma_cm_event_handler(struct rdma_cm_id *id, struct rdma_cm_event *event)
 {
 	struct rpcrdma_ep *ep = id->context;
+	int status;
 
 	might_sleep();
 
@@ -247,41 +265,32 @@ rpcrdma_cm_event_handler(struct rdma_cm_id *id, struct rdma_cm_event *event)
 		complete(&ep->re_done);
 		return 0;
 	case RDMA_CM_EVENT_ADDR_CHANGE:
-		switch (xchg(&ep->re_connect_status, -ENODEV)) {
-		case 0:
-			goto wake_connect_worker;
-		case 1:
-			/* The later DISCONNECTED event balances the
-			 * ESTABLISHED get; do not put here.
-			 */
-			rpcrdma_force_disconnect(ep);
-			return 0;
-		}
+		rpcrdma_ep_disconnect(ep, -ENODEV, true);
 		return 0;
 	case RDMA_CM_EVENT_ESTABLISHED:
-		rpcrdma_ep_get(ep);
-		ep->re_connect_status = 1;
+		if (READ_ONCE(ep->re_connect_status))
+			return 0;
 		rpcrdma_update_cm_private(ep, &event->param.conn);
+		if (cmpxchg(&ep->re_connect_status, 0, 1))
+			return 0;
 		trace_xprtrdma_inline_thresh(ep);
 		wake_up_all(&ep->re_connect_wait);
-		break;
+		return 0;
 	case RDMA_CM_EVENT_CONNECT_ERROR:
-		ep->re_connect_status = -ENOTCONN;
-		goto wake_connect_worker;
+		rpcrdma_ep_disconnect(ep, -ENOTCONN, true);
+		return 0;
 	case RDMA_CM_EVENT_UNREACHABLE:
-		ep->re_connect_status = -ENETUNREACH;
-		goto wake_connect_worker;
+		rpcrdma_ep_disconnect(ep, -ENETUNREACH, true);
+		return 0;
 	case RDMA_CM_EVENT_REJECTED:
-		ep->re_connect_status = -ECONNREFUSED;
+		status = -ECONNREFUSED;
 		if (event->status == IB_CM_REJ_STALE_CONN)
-			ep->re_connect_status = -ENOTCONN;
-wake_connect_worker:
-		wake_up_all(&ep->re_connect_wait);
+			status = -ENOTCONN;
+		rpcrdma_ep_disconnect(ep, status, true);
 		return 0;
 	case RDMA_CM_EVENT_DISCONNECTED:
-		ep->re_connect_status = -ECONNABORTED;
-		rpcrdma_force_disconnect(ep);
-		return rpcrdma_ep_put(ep);
+		rpcrdma_ep_disconnect(ep, -ECONNABORTED, true);
+		return 0;
 	default:
 		break;
 	}
@@ -308,11 +317,18 @@ static void rpcrdma_ep_removal_done(struct rpcrdma_notification *rn,
 	 * for the connect worker, which may need the mutex to unregister.
 	 */
 	xprt = xprt_get(ep->re_xprt);
-	if (!xprt)
+	if (!xprt) {
+		/*
+		 * The final xprt_put() can be waiting for the connect worker's
+		 * XPRT_LOCKED bit. The device mutex protects @ep until
+		 * unregister. Wake pending connects without using the transport.
+		 */
+		rpcrdma_ep_disconnect(ep, -ENODEV, false);
 		return;
+	}
 	release->release = rpcrdma_xprt_removal_put;
 	release->data = xprt;
-	rpcrdma_force_disconnect(ep);
+	rpcrdma_ep_disconnect(ep, -ENODEV, true);
 }
 
 static struct rdma_cm_id *rpcrdma_create_id(struct rpcrdma_xprt *r_xprt,
@@ -369,6 +385,7 @@ static struct rdma_cm_id *rpcrdma_create_id(struct rpcrdma_xprt *r_xprt,
 static void rpcrdma_ep_destroy(struct kref *kref)
 {
 	struct rpcrdma_ep *ep = container_of(kref, struct rpcrdma_ep, re_kref);
+	struct ib_device *device = ep->re_id->device;
 
 	if (ep->re_id->qp) {
 		rdma_destroy_qp(ep->re_id);
@@ -386,7 +403,14 @@ static void rpcrdma_ep_destroy(struct kref *kref)
 		ib_dealloc_pd(ep->re_pd);
 	ep->re_pd = NULL;
 
-	rpcrdma_rn_unregister(ep->re_id->device, &ep->re_rn);
+	/* Removal callbacks also access re_id, including from tracepoints. */
+	rpcrdma_rn_unregister(device, &ep->re_rn);
+
+	/*
+	 * All puts occur outside CM callbacks. Fence queued and running
+	 * callbacks before freeing the context, even if no connection formed.
+	 */
+	rdma_destroy_id(ep->re_id);
 
 	kfree(ep);
 	module_put(THIS_MODULE);
@@ -397,13 +421,10 @@ static noinline void rpcrdma_ep_get(struct rpcrdma_ep *ep)
 	kref_get(&ep->re_kref);
 }
 
-/* Returns:
- *     %0 if @ep still has a positive kref count, or
- *     %1 if @ep was destroyed successfully.
- */
-static noinline int rpcrdma_ep_put(struct rpcrdma_ep *ep)
+/* Must not be called from an RDMA CM event handler. */
+static noinline void rpcrdma_ep_put(struct rpcrdma_ep *ep)
 {
-	return kref_put(&ep->re_kref, rpcrdma_ep_destroy);
+	kref_put(&ep->re_kref, rpcrdma_ep_destroy);
 }
 
 static int rpcrdma_ep_create(struct rpcrdma_xprt *r_xprt)
@@ -419,6 +440,7 @@ static int rpcrdma_ep_create(struct rpcrdma_xprt *r_xprt)
 		return -ENOTCONN;
 	ep->re_xprt = &r_xprt->rx_xprt;
 	kref_init(&ep->re_kref);
+	init_waitqueue_head(&ep->re_connect_wait);
 
 	id = rpcrdma_create_id(r_xprt, ep);
 	if (IS_ERR(id)) {
@@ -446,7 +468,6 @@ static int rpcrdma_ep_create(struct rpcrdma_xprt *r_xprt)
 
 	ep->re_send_batch = ep->re_max_requests >> 3;
 	ep->re_send_count = ep->re_send_batch;
-	init_waitqueue_head(&ep->re_connect_wait);
 
 	ep->re_attr.send_cq = ib_alloc_cq_any(device, r_xprt,
 					      ep->re_attr.cap.max_send_wr,
@@ -514,7 +535,6 @@ static int rpcrdma_ep_create(struct rpcrdma_xprt *r_xprt)
 
 out_destroy:
 	rpcrdma_ep_put(ep);
-	rdma_destroy_id(id);
 	return rc;
 }
 
@@ -550,12 +570,17 @@ int rpcrdma_xprt_connect(struct rpcrdma_xprt *r_xprt)
 
 	if (xprt->reestablish_timeout < RPCRDMA_INIT_REEST_TO)
 		xprt->reestablish_timeout = RPCRDMA_INIT_REEST_TO;
-	wait_event_interruptible(ep->re_connect_wait,
-				 ep->re_connect_status != 0);
-	if (ep->re_connect_status <= 0) {
-		rc = ep->re_connect_status;
+	rc = wait_event_interruptible(ep->re_connect_wait,
+				      READ_ONCE(ep->re_connect_status));
+	if (rc)
+		goto out;
+	/* The ESTABLISHED cmpxchg publishes the negotiated CM parameters. */
+	rc = smp_load_acquire(&ep->re_connect_status);
+	/* Let pending RPC tasks retry after the device disappears. */
+	if (rc == -ENODEV)
+		rc = -ENOTCONN;
+	if (rc < 0)
 		goto out;
-	}
 
 	rc = rpcrdma_sendctxs_create(r_xprt);
 	if (rc) {
@@ -622,8 +647,11 @@ void rpcrdma_xprt_disconnect(struct rpcrdma_xprt *r_xprt)
 	rpcrdma_reqs_reset(r_xprt);
 	rpcrdma_mrs_destroy(r_xprt);
 
-	if (rpcrdma_ep_put(ep))
-		rdma_destroy_id(id);
+	/*
+	 * The destructor destroys the CM ID, fencing any callback still
+	 * returning before the endpoint context is freed.
+	 */
+	rpcrdma_ep_put(ep);
 
 	r_xprt->rx_ep = NULL;
 
@@ -920,7 +948,7 @@ void rpcrdma_mrs_refresh(struct rpcrdma_xprt *r_xprt)
 	/* If there is no underlying connection, it's no use
 	 * to wake the refresh worker.
 	 */
-	if (ep->re_connect_status != 1)
+	if (READ_ONCE(ep->re_connect_status) != 1)
 		return;
 	queue_work(system_highpri_wq, &buf->rb_refresh_worker);
 }
-- 
2.34.1


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

end of thread, other threads:[~2026-11-24  2:13 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-24  2:18 [PATCH v3 0/3] rpcrdma: Fix device removal races and endpoint cleanup Li Xiasong
2026-11-24  2:13 ` sashiko-bot
2026-11-24  2:18 ` [PATCH v3 1/3] rpcrdma: Serialize notification registration with device removal Li Xiasong
2026-11-24  2:18 ` [PATCH v3 2/3] rpcrdma: Serialize removal callbacks with unregister Li Xiasong
2026-11-24  2:18 ` [PATCH v3 3/3] xprtrdma: Wake connect worker on device removal Li Xiasong

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