From: Trond Myklebust <trondmy@gmail.com>
To: lixiasong1@huawei.com
Cc: cel@kernel.org, jlayton@kernel.org, neil@brown.name,
okorniev@redhat.com, Dai.Ngo@oracle.com, tom@talpey.com,
anna@kernel.org, linux-nfs@vger.kernel.org,
linux-rdma@vger.kernel.org, stable@vger.kernel.org,
yuehaibing@huawei.com, zhangchangzhong@huawei.com,
weiyongjun1@huawei.com
Subject: Re: [PATCH v3 3/3] xprtrdma: Wake connect worker on device removal
Date: Sun, 29 Nov 2026 17:13:03 -0500 [thread overview]
Message-ID: <308f5aff56453bbaaff95d79a1cd78e45f0dc93b.camel@gmail.com> (raw)
In-Reply-To: <20261124021842.130141-4-lixiasong1@huawei.com>
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
prev parent reply other threads:[~2026-11-29 22:13 UTC|newest]
Thread overview: 16+ messages / expand[flat|nested] mbox.gz Atom feed top
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 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 message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=308f5aff56453bbaaff95d79a1cd78e45f0dc93b.camel@gmail.com \
--to=trondmy@gmail.com \
--cc=Dai.Ngo@oracle.com \
--cc=anna@kernel.org \
--cc=cel@kernel.org \
--cc=jlayton@kernel.org \
--cc=linux-nfs@vger.kernel.org \
--cc=linux-rdma@vger.kernel.org \
--cc=lixiasong1@huawei.com \
--cc=neil@brown.name \
--cc=okorniev@redhat.com \
--cc=stable@vger.kernel.org \
--cc=tom@talpey.com \
--cc=weiyongjun1@huawei.com \
--cc=yuehaibing@huawei.com \
--cc=zhangchangzhong@huawei.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox