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 --- 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 /** - * 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 +#include #include #include #include @@ -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