* [PATCH v3 0/3] rpcrdma: Fix device removal races and endpoint cleanup
@ 2026-11-24 2:18 Li Xiasong
2026-11-24 2:18 ` [PATCH v3 1/3] rpcrdma: Serialize notification registration with device removal Li Xiasong
` (2 more replies)
0 siblings, 3 replies; 15+ 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] 15+ 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:18 ` Li Xiasong 2026-11-24 16:52 ` Chuck Lever 2026-11-25 20:29 ` tmenninger 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 2 siblings, 2 replies; 15+ 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] 15+ messages in thread
* Re: [PATCH v3 1/3] rpcrdma: Serialize notification registration with device removal 2026-11-24 2:18 ` [PATCH v3 1/3] rpcrdma: Serialize notification registration with device removal Li Xiasong @ 2026-11-24 16:52 ` Chuck Lever 2026-11-26 11:21 ` Li Xiasong 2026-11-25 20:29 ` tmenninger 1 sibling, 1 reply; 15+ messages in thread From: Chuck Lever @ 2026-11-24 16:52 UTC (permalink / raw) To: Li Xiasong, Jeff Layton, NeilBrown, Olga Kornievskaia, Dai Ngo, Tom Talpey, Trond Myklebust, Anna Schumaker Cc: linux-nfs, linux-rdma, stable, yuehaibing, zhangchangzhong, weiyongjun1 On Mon, Nov 23, 2026, at 9:18 PM, Li Xiasong wrote: > 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> > @@ -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); > } Nit: The conventional practice in the kernel is to release resources outside the mutex. AFAICT that is safe to do so here; only the ib_set_client_data() call needs serialization. -- Chuck Lever ^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: [PATCH v3 1/3] rpcrdma: Serialize notification registration with device removal 2026-11-24 16:52 ` Chuck Lever @ 2026-11-26 11:21 ` Li Xiasong 0 siblings, 0 replies; 15+ messages in thread From: Li Xiasong @ 2026-11-26 11:21 UTC (permalink / raw) To: Chuck Lever, Jeff Layton, NeilBrown, Olga Kornievskaia, Dai Ngo, Tom Talpey, Trond Myklebust, Anna Schumaker Cc: linux-nfs@vger.kernel.org, linux-rdma@vger.kernel.org, stable@vger.kernel.org, yuehaibing, zhangchangzhong, weiyongjun (A) On 11/25/2026 12:52 AM, Chuck Lever wrote: > > On Mon, Nov 23, 2026, at 9:18 PM, Li Xiasong wrote: >> 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> > >> @@ -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); >> } > > Nit: The conventional practice in the kernel is to release resources outside > the mutex. AFAICT that is safe to do so here; only the ib_set_client_data() > call needs serialization. > Agreed. I'll unlock right after ib_set_client_data() and do xa_destroy() and kfree() outside the mutex. Thanks, Li Xiasong ^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: [PATCH v3 1/3] rpcrdma: Serialize notification registration with device removal 2026-11-24 2:18 ` [PATCH v3 1/3] rpcrdma: Serialize notification registration with device removal Li Xiasong 2026-11-24 16:52 ` Chuck Lever @ 2026-11-25 20:29 ` tmenninger 1 sibling, 0 replies; 15+ messages in thread From: tmenninger @ 2026-11-25 20:29 UTC (permalink / raw) To: Li Xiasong Cc: Chuck Lever, Jeff Layton, NeilBrown, Olga Kornievskaia, Dai Ngo, Tom Talpey, Trond Myklebust, Anna Schumaker, linux-nfs, linux-rdma, stable, yuehaibing, zhangchangzhong, weiyongjun1 > @@ -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); Seconding Chuck's nit that you can mutex_unlock before xa_destroy and kfree. Reviewed-by: Tim Menninger <tmenninger@everpuredata.com> -- Tim Menninger <tmenninger@everpuredata.com> ^ permalink raw reply [flat|nested] 15+ 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:18 ` [PATCH v3 1/3] rpcrdma: Serialize notification registration with device removal Li Xiasong @ 2026-11-24 2:18 ` Li Xiasong 2026-11-24 16:56 ` Chuck Lever 2026-11-24 2:18 ` [PATCH v3 3/3] xprtrdma: Wake connect worker on device removal Li Xiasong 2 siblings, 1 reply; 15+ 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] 15+ messages in thread
* Re: [PATCH v3 2/3] rpcrdma: Serialize removal callbacks with unregister 2026-11-24 2:18 ` [PATCH v3 2/3] rpcrdma: Serialize removal callbacks with unregister Li Xiasong @ 2026-11-24 16:56 ` Chuck Lever 2026-11-26 11:57 ` Li Xiasong 0 siblings, 1 reply; 15+ messages in thread From: Chuck Lever @ 2026-11-24 16:56 UTC (permalink / raw) To: Li Xiasong Cc: Jeff Layton, NeilBrown, Olga Kornievskaia, Dai Ngo, Tom Talpey, Trond Myklebust, Anna Schumaker, linux-nfs, linux-rdma, stable, yuehaibing, zhangchangzhong, weiyongjun1 On Tue, 24 Nov 2026 10:18:41 +0800, Li Xiasong wrote: > @@ -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); AFAICT the release hooks now run one at a time inside the walk. A hook that blocks holds off rn_done() for every registrant after it. Before this patch, rpcrdma_remove_one() notified every registrant and then waited once. A client hook can block. When its xprt_put() drops the last reference, xprt_destroy() waits for XPRT_LOCKED. If the connect worker holds that bit in rpcrdma_xprt_connect(), it is waiting on re_connect_wait with no timeout, and only a CM event from the peer ends the wait. 3/3 wakes re_connect_wait from rn_done(). A connect worker still resolving the address or route has not registered its ep yet, though, so rn_done() cannot wake it. On iWARP, waking the connect worker does not release XPRT_LOCKED either. iw_cm_connect() returns with IWCM_F_CONNECT_WAIT set, and only the provider's connect reply clears it. destroy_cm_id() waits for that bit. The woken worker goes on to rpcrdma_xprt_disconnect(), and the rdma_destroy_id() call that 3/3 adds to rpcrdma_ep_destroy() blocks until the peer replies or the provider's connect timeout fires. A release hook waiting on XPRT_LOCKED waits that long too. One stalled transport should not delay the removal notification for all the others. Can rpcrdma_remove_one() call rn_done() for every registrant first, and run the release hooks after the walk finishes? The hooks would have to be collected somewhere, because a registrant can unregister as soon as the mutex is dropped. > @@ -289,12 +289,30 @@ rpcrdma_cm_event_handler(struct rdma_cm_id *id, struct rdma_cm_event *event) [ ... ] > + /* > + * 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; xprt_get() pins a transport whose refcount has reached zero but whose memory is still allocated. It does not help once the rpc_xprt has been freed. At this point in the series an ep can outlive its rpc_xprt. The ESTABLISHED event takes an ep reference that only the DISCONNECTED event drops. When the put in rpcrdma_xprt_disconnect() is not the last one, xprt_rdma_destroy() goes on to xprt_free() while the ep is still registered. If the peer does not respond to the DREQ, the ep stays registered until the CM gives up. A device removal in that window calls xprt_get() on freed memory. I have not reproduced this. The xprt_force_disconnect(ep->re_xprt) call this patch replaces has the same problem, so your patch does not introduce this issue. 3/3 closes this window by moving every ep put out of the CM event handler. But "Pin the client transport during its callback" in the patch description promises more than xprt_get() provides. Let's reword the description or note that 3/3 is what makes ep->re_xprt safe to dereference in this callback. -- Chuck Lever ^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: [PATCH v3 2/3] rpcrdma: Serialize removal callbacks with unregister 2026-11-24 16:56 ` Chuck Lever @ 2026-11-26 11:57 ` Li Xiasong 0 siblings, 0 replies; 15+ messages in thread From: Li Xiasong @ 2026-11-26 11:57 UTC (permalink / raw) To: Chuck Lever Cc: Jeff Layton, NeilBrown, Olga Kornievskaia, Dai Ngo, Tom Talpey, Trond Myklebust, Anna Schumaker, linux-nfs@vger.kernel.org, linux-rdma@vger.kernel.org, stable@vger.kernel.org, yuehaibing, zhangchangzhong, weiyongjun (A) On 11/25/2026 12:56 AM, Chuck Lever wrote: > > Can rpcrdma_remove_one() call rn_done() for every registrant first, > and run the release hooks after the walk finishes? The hooks would > have to be collected somewhere, because a registrant can unregister as > soon as the mutex is dropped. > Thanks, Chuck. I'll run the release hooks after the walk. The server calls svc_xprt_close() from its release hook, so a client xprt_put() that blocks can still delay a server close if the hooks run serially. I'll look at that ordering too. > But "Pin the client transport during its callback" in the patch > description promises more than xprt_get() provides. Let's reword the > description or note that 3/3 is what makes ep->re_xprt safe to > dereference in this callback. > Agreed. xprt_get() assumes ep->re_xprt is already valid. I'll note in the commit message that the teardown change unregisters the endpoint before xprt_free(). Thanks, Li Xiasong ^ permalink raw reply [flat|nested] 15+ 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 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 ` Li Xiasong 2026-11-24 17:01 ` Chuck Lever ` (2 more replies) 2 siblings, 3 replies; 15+ 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] 15+ messages in thread
* Re: [PATCH v3 3/3] xprtrdma: Wake connect worker on device removal 2026-11-24 2:18 ` [PATCH v3 3/3] xprtrdma: Wake connect worker on device removal Li Xiasong @ 2026-11-24 17:01 ` Chuck Lever 2026-11-26 12:08 ` Li Xiasong 2026-11-25 20:29 ` tmenninger 2026-11-29 22:13 ` Trond Myklebust 2 siblings, 1 reply; 15+ messages in thread From: Chuck Lever @ 2026-11-24 17:01 UTC (permalink / raw) To: Li Xiasong Cc: Jeff Layton, NeilBrown, Olga Kornievskaia, Dai Ngo, Tom Talpey, Trond Myklebust, Anna Schumaker, linux-nfs, linux-rdma, stable, yuehaibing, zhangchangzhong, weiyongjun1 On Tue, 24 Nov 2026 10:18:42 +0800, Li Xiasong wrote: > 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. On InfiniBand, waking the connect worker works as described. On iWARP the woken connect worker goes on to rpcrdma_xprt_disconnect(), and the rdma_destroy_id() call in rpcrdma_ep_destroy() waits in destroy_cm_id() until IWCM_F_CONNECT_WAIT clears. Only the provider's connect reply clears that bit. The worker still waits for the peer or for the provider's connect timeout, and it now holds XPRT_LOCKED and rx_unmap_rwsem while it waits. On iWARP, then, this patch moves the wait from re_connect_wait into rdma_destroy_id(). I don't see a way around that in xprtrdma. The patch description should still not claim that removal unblocks endpoint cleanup on every device type. > 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. Removing the get/put pairs from the CM event handler also moves the last ep put into rpcrdma_xprt_disconnect(), before xprt_rdma_destroy() reaches xprt_free(). That ordering closes the stale ep->re_xprt window I described in my reply to patch 2/3. Let's mention that ordering in this patch's commit message, since patch 2/3 relies on it. -- Chuck Lever ^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: [PATCH v3 3/3] xprtrdma: Wake connect worker on device removal 2026-11-24 17:01 ` Chuck Lever @ 2026-11-26 12:08 ` Li Xiasong 0 siblings, 0 replies; 15+ messages in thread From: Li Xiasong @ 2026-11-26 12:08 UTC (permalink / raw) To: Chuck Lever Cc: Jeff Layton, NeilBrown, Olga Kornievskaia, Dai Ngo, Tom Talpey, Trond Myklebust, Anna Schumaker, linux-nfs@vger.kernel.org, linux-rdma@vger.kernel.org, stable@vger.kernel.org, yuehaibing, zhangchangzhong, weiyongjun (A) On 11/25/2026 1:01 AM, Chuck Lever wrote: > On iWARP, then, this patch moves the wait from re_connect_wait into > rdma_destroy_id(). I don't see a way around that in xprtrdma. The > patch description should still not claim that removal unblocks > endpoint cleanup on every device type. > Agreed. The worker can hit that wait in rdma_disconnect(), too: iw_cm_disconnect() waits for IWCM_F_CONNECT_WAIT to clear. I'll note in the commit message that iWARP teardown can still wait for the provider's connect reply after the worker wakes up. > Removing the get/put pairs from the CM event handler also moves the > last ep put into rpcrdma_xprt_disconnect(), before xprt_rdma_destroy() > reaches xprt_free(). That ordering closes the stale ep->re_xprt window > I described in my reply to patch 2/3. Let's mention that ordering in > this patch's commit message, since patch 2/3 relies on it. > I'll spell out that ordering in the commit message. Thanks, Li Xiasong ^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: [PATCH v3 3/3] xprtrdma: Wake connect worker on device removal 2026-11-24 2:18 ` [PATCH v3 3/3] xprtrdma: Wake connect worker on device removal Li Xiasong 2026-11-24 17:01 ` Chuck Lever @ 2026-11-25 20:29 ` tmenninger 2026-11-26 12:20 ` Li Xiasong 2026-11-29 22:13 ` Trond Myklebust 2 siblings, 1 reply; 15+ messages in thread From: tmenninger @ 2026-11-25 20:29 UTC (permalink / raw) To: Li Xiasong Cc: Chuck Lever, Jeff Layton, NeilBrown, Olga Kornievskaia, Dai Ngo, Tom Talpey, Trond Myklebust, Anna Schumaker, 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. A bit of a nit... Could you split these endpoint/CM ID lifetime changes into a separate patch, ahead of the connect-worker wakeup changes? Moving rdma_destroy_id() into the endpoint destructor and removing the CM callback get/put pairs changes the endpoint ownership and teardown rules. That addresses a different failure mode from the connect worker remaining asleep during device removal, and I think it warrants its own commit and explanation. > > 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> > I have no other feedback on this series that wasn't already brought up by Chuck. I'm still working on a reproducer to give you a tested tag. -- Tim Menninger <tmenninger@everpuredata.com> ^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: [PATCH v3 3/3] xprtrdma: Wake connect worker on device removal 2026-11-25 20:29 ` tmenninger @ 2026-11-26 12:20 ` Li Xiasong 2026-11-28 21:28 ` Tim Menninger 0 siblings, 1 reply; 15+ messages in thread From: Li Xiasong @ 2026-11-26 12:20 UTC (permalink / raw) To: tmenninger@purestorage.com Cc: Chuck Lever, Jeff Layton, NeilBrown, Olga Kornievskaia, Dai Ngo, Tom Talpey, Trond Myklebust, Anna Schumaker, linux-nfs@vger.kernel.org, linux-rdma@vger.kernel.org, stable@vger.kernel.org, yuehaibing, zhangchangzhong, weiyongjun (A) On 11/26/2026 4:29 AM, tmenninger@purestorage.com wrote: > Could you split these endpoint/CM ID lifetime changes into a separate > patch, ahead of the connect-worker wakeup changes? That makes sense. I started out fixing the missed wakeup and ended up folding the teardown changes in during the v1 review. I'll give the teardown fix its own patch and put it ahead of the wakeup fix in v4. > > I'm still working on a reproducer to give you a tested tag. > Thanks for working on a reproducer, Tim. Li Xiasong ^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: [PATCH v3 3/3] xprtrdma: Wake connect worker on device removal 2026-11-26 12:20 ` Li Xiasong @ 2026-11-28 21:28 ` Tim Menninger 0 siblings, 0 replies; 15+ messages in thread From: Tim Menninger @ 2026-11-28 21:28 UTC (permalink / raw) To: Li Xiasong Cc: Chuck Lever, Jeff Layton, NeilBrown, Olga Kornievskaia, Dai Ngo, Tom Talpey, Trond Myklebust, Anna Schumaker, linux-nfs, linux-rdma, stable, yuehaibing, zhangchangzhong, weiyongjun (A) On Thu, Nov 26, 2026 at 08:20:21PM +0800, Li Xiasong wrote: > On 11/26/2026 4:29 AM, tmenninger@purestorage.com wrote: >> >> I'm still working on a reproducer to give you a tested tag. >> > > Thanks for working on a reproducer, Tim. On v7.4-rc3, over pNFS/RDMA with one MDS and one DS, under sustained reads, restarting the NFS service on the DS consistently produces this on the first restart: refcount_t: underflow; use-after-free. WARNING: lib/refcount.c:37 at refcount_warn_saturate+0x68/0xb0, CPU#4: kworker/u385:28/21602 ... Call Trace: <TASK> rpcrdma_ep_put+0xec/0x100 [rpcrdma] rpcrdma_xprt_disconnect+0x400/0x4c0 [rpcrdma] xprt_rdma_connect_worker+0xd4/0xe0 [rpcrdma] process_one_work+0x1cc/0x420 worker_thread+0x1a8/0x330 ? __pfx_worker_thread+0x10/0x10 kthread+0x101/0x140 ? __pfx_kthread+0x10/0x10 ret_from_fork+0x2a5/0x360 ? __pfx_kthread+0x10/0x10 ret_from_fork_asm+0x1a/0x30 </TASK> ---[ end trace 0000000000000000 ]--- After applying your series, I no longer see the underflow; use-after-free. This is fixed by your third patch since the same WARNING still appears if I apply only patches 1 and 2. There's no device removal in this setup, so it's presumably the teardown half. In the controlled run (3 below), the failed connect got a DISCONNECTED event without a prior ESTABLISHED, so the put in the DISCONNECTED handler had no matching get. This one hits on an ordinary server restart. Since you're splitting the teardown fix out for v4, could its commit message call out this underflow on a failed connect, and could its Fixes: tag point at where the failed-connect lifetime bug came from? 3/3 currently carries Fixes: 3f4eb9ff9234 ("xprtrdma: Handle device removal outside of the CM event handler") which is the device-removal change (v6.11). If I'm reading the history right, the unbalanced ESTABLISHED get / DISCONNECTED put came in v5.7 with e28ce90083f0 ("xprtrdma: kmalloc rpcrdma_ep separate from rpcrdma_xprt") For the device-removal races the series describes, several thousand unbind/rebind cycles with varying delays never hit the described bugs on v7.4-rc3, so I threw claude at it. I now have reproducers for the failures each patch addresses, and ran them against the unpatched code, 1/3, 1-2/3 and 1-3/3. Each failure reproduces until the patch that targets it is applied, and not after: v7.4-rc3 1/3 1-2/3 1-3/3 registration vs. removal FAIL pass pass pass removal callback vs. unregister FAIL FAIL pass pass removal during a pending connect FAIL FAIL FAIL pass This reproduced with rpcrdma.ko built out of tree for each variant, so every run used the same kernel. NFSv4.1 pNFS flexfiles client with 16 RDMA data-server connections on mlx5 (RoCE). Removal was an unbind of the mlx5_core.rdma.N auxiliary device from mlx5_ib. Races 1 and 2 required a diagnostic patch with one-shot, time-limited holds, placed only where the code can already sleep. Case 3 needed no hold. 1) Registration vs. removal (1/3). I held one rpcrdma_rn_register() between the RPCRDMA_RD_F_REMOVING test and xa_alloc(), until rpcrdma_remove_one() finished its walk. Unpatched: the rn was published after the walk with REMOVING set. That endpoint was never notified, connected on the device being removed, and rpcrdma_remove_one() sat in wait_for_completion() until I forced the transports closed. With 1/3: remove_one blocked on rpcrdma_device_mutex for the hold, the rn was published before REMOVING was set, it was notified, and the unbind completed on its own. 2) Removal callback vs. unregister (2/3). I held the walk just before the first rn_done() call and force-closed that endpoint's transport. If the owner reaches its kfree() during the hold, the diagnostic patch quarantines the endpoint instead (poisons it, does not free it), so the walker can report instead of calling through it. Unpatched and 1/3: the owner unregistered the endpoint and reached the point where it would free it while the walker still held its rn, so rn_done() would have been called through freed memory. With 1-2/3: the close blocked in rpcrdma_rn_unregister() until the walk moved on, and the callback ran on a live endpoint. 3) Removal during a pending connect (3/3). I pointed one transport at an address that is routed over the RoCE port but never answers the CM REQ, waited for the connect worker to sleep in re_connect_wait (re_connect_status == 0), then unbound the device. Base through 1-2/3: the removal callback did not wake the worker. rpcrdma_remove_one() waited about 78s, until the CM reported UNREACHABLE. The failed connect then hit the same refcount underflow as above. A DEVICE_REMOVAL event was delivered to the destroyed endpoint, and its rdma_cm_id was never destroyed, so cma_process_remove() waited forever and the host needed a power cycle. With 1-3/3: the worker woke with -ENODEV within microseconds of the removal callback, remove_one finished in about half a second, and the device rebound normally. I can post the diagnostic patch and scripts if they would be useful. I'll rerun everything on v4 and add a formal Tested-by on each patch then. Thanks, Tim ^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: [PATCH v3 3/3] xprtrdma: Wake connect worker on device removal 2026-11-24 2:18 ` [PATCH v3 3/3] xprtrdma: Wake connect worker on device removal Li Xiasong 2026-11-24 17:01 ` Chuck Lever 2026-11-25 20:29 ` tmenninger @ 2026-11-29 22:13 ` Trond Myklebust 2 siblings, 0 replies; 15+ messages in thread From: Trond Myklebust @ 2026-11-29 22:13 UTC (permalink / raw) To: lixiasong1 Cc: cel, jlayton, neil, okorniev, Dai.Ngo, tom, anna, linux-nfs, linux-rdma, stable, yuehaibing, zhangchangzhong, weiyongjun1 On Tue, 24 Nov 2026 10:18:42 +0800, Li Xiasong <lixiasong1@huawei.com> wrote: > 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 [ ... ] > 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); [ ... ] > 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; [ ... ] > @@ -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); With the ESTABLISHED reference gone, rdma_destroy_qp() now runs from rpcrdma_xprt_disconnect() right after rdma_disconnect() and the drain, while the connection is still closing. On iWARP devices, can this block until the iWARP close completes? rdma_disconnect() starts a graceful close there with iw_cm_disconnect(id, 0), and the iw_cm holds a QP reference from iw_cm_connect() until the IW_CM_EVENT_CLOSE upcall or iw_destroy_cm_id(): drivers/infiniband/core/iwcm.c:iw_cm_connect() { ... cm_id->device->ops.iw_add_ref(qp); cm_id_priv->qp = qp; ... } Provider destroy paths wait for that reference, for example: drivers/infiniband/hw/cxgb4/qp.c:c4iw_destroy_qp() { ... c4iw_qp_rem_ref(ib_qp); wait_for_completion(&qhp->qp_rel_comp); ... } rdma_destroy_id() would drop the iw_cm reference, but it now runs after rdma_destroy_qp(): xprt_autoclose() xprt_rdma_close() rpcrdma_xprt_disconnect() <- takes rx_unmap_rwsem for write rdma_disconnect() <- graceful iWARP close starts rpcrdma_xprt_drain() rpcrdma_ep_put() rpcrdma_ep_destroy() rdma_destroy_qp() c4iw_destroy_qp() <- waits for IW_CM_EVENT_CLOSE rdma_destroy_id() On cxgb4 the close upcall arrives when the peer completes the TCP close, or from the endpoint timer (ep_timeout_secs, 60 seconds by default) when the peer does not answer. XPRT_LOCKED and rx_unmap_rwsem stay held for that time, so reconnects and frwr_unmap_sync() wait as well. Before this patch, the DISCONNECTED handler made the final put after cm_close_handler() had already dropped the iw_cm QP reference. Commit e28ce90083f0 ("xprtrdma: kmalloc rpcrdma_ep separate from rpcrdma_xprt") describes that as intended: Change the rpcrdma_xprt_disconnect() function so that it no longer waits for the DISCONNECTED event. This prevents blocking if the remote is unresponsive. Is the synchronous wait on iWARP intended? IB and RoCE do not appear to be affected, since cm_destroy_id() cancels an outstanding DREQ and enters timewait without waiting for the DREP. [ ... ] I've kept 1/3 and 2/3 in my testing branch, but dropped this one for now. On Mon, Nov 23, 2026 08:53 PM, Li Xiasong <lixiasong1@huawei.com> wrote: > 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 > > -- Trond Myklebust Linux NFS client maintainer, Hammerspace trondmy@kernel.org, trond.myklebust@hammerspace.com ^ permalink raw reply [flat|nested] 15+ messages in thread
end of thread, other threads:[~2026-11-29 22:13 UTC | newest] Thread overview: 15+ 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:18 ` [PATCH v3 1/3] rpcrdma: Serialize notification registration with device removal Li Xiasong 2026-11-24 16:52 ` Chuck Lever 2026-11-26 11:21 ` Li Xiasong 2026-11-25 20:29 ` tmenninger 2026-11-24 2:18 ` [PATCH v3 2/3] rpcrdma: Serialize removal callbacks with unregister Li Xiasong 2026-11-24 16:56 ` Chuck Lever 2026-11-26 11:57 ` Li Xiasong 2026-11-24 2:18 ` [PATCH v3 3/3] xprtrdma: Wake connect worker on device removal Li Xiasong 2026-11-24 17:01 ` Chuck Lever 2026-11-26 12:08 ` Li Xiasong 2026-11-25 20:29 ` tmenninger 2026-11-26 12:20 ` Li Xiasong 2026-11-28 21:28 ` Tim Menninger 2026-11-29 22:13 ` Trond Myklebust
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox