rpcrdma_remove_one() obtains notification pointers from rd_xa and invokes their callbacks without preventing a concurrent unregister from freeing the object that embeds the notification. The XArray internal RCU protection keeps its nodes valid, but does not extend the lifetime of stored objects. Add get and put operations to each notification. Acquire a reference while holding rpcrdma_device_mutex, then drop the mutex around the callback and release the reference afterwards. Serialize unregister with the same mutex so either removal pins the object first or unregister removes it before the walk can find it. Use kref_get_unless_zero() so a notification whose owner has already entered its final release is skipped. The removal path then waits for that owner to unregister and release the rpcrdma_device reference. Fixes: 7e86845a0346 ("rpcrdma: Implement generic device removal") Signed-off-by: Li Xiasong --- include/linux/sunrpc/rdma_rn.h | 6 ++++- net/sunrpc/xprtrdma/ib_client.c | 34 +++++++++++++++++++----- net/sunrpc/xprtrdma/svc_rdma_transport.c | 19 ++++++++++++- net/sunrpc/xprtrdma/verbs.c | 21 ++++++++++++++- 4 files changed, 71 insertions(+), 9 deletions(-) diff --git a/include/linux/sunrpc/rdma_rn.h b/include/linux/sunrpc/rdma_rn.h index 7d032ca057af..6122af607120 100644 --- a/include/linux/sunrpc/rdma_rn.h +++ b/include/linux/sunrpc/rdma_rn.h @@ -13,12 +13,16 @@ */ struct rpcrdma_notification { void (*rn_done)(struct rpcrdma_notification *rn); + bool (*rn_get)(struct rpcrdma_notification *rn); + void (*rn_put)(struct rpcrdma_notification *rn); 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), + bool (*get)(struct rpcrdma_notification *rn), + void (*put)(struct rpcrdma_notification *rn)); 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 acd5db729539..651ed6637000 100644 --- a/net/sunrpc/xprtrdma/ib_client.c +++ b/net/sunrpc/xprtrdma/ib_client.c @@ -48,6 +48,8 @@ static struct rpcrdma_device *rpcrdma_get_client_data(struct ib_device *device) * @device: device to monitor * @rn: notification object that wishes to be notified * @done: callback to notify caller of device removal + * @get: callback to acquire a notification object reference + * @put: callback to release a notification object reference * * Returns zero on success. The callback in rn_done is guaranteed * to be invoked when the device is removed, unless this notification @@ -61,7 +63,9 @@ 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), + bool (*get)(struct rpcrdma_notification *rn), + void (*put)(struct rpcrdma_notification *rn)) { struct rpcrdma_device *rd; int ret = -ENETUNREACH; @@ -81,8 +85,12 @@ int rpcrdma_rn_register(struct ib_device *device, * value would unregister an @rn that was never inserted. */ rn->rn_done = done; + rn->rn_get = get; + rn->rn_put = put; if (xa_alloc(&rd->rd_xa, &rn->rn_index, rn, xa_limit_32b, GFP_KERNEL) < 0) { rn->rn_done = NULL; + rn->rn_get = NULL; + rn->rn_put = NULL; ret = -ENOMEM; goto out_unlock; } @@ -115,10 +123,12 @@ static void rpcrdma_rn_release(struct kref *kref) 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 @@ -132,12 +142,16 @@ void rpcrdma_rn_unregister(struct ib_device *device, * rd_kref. */ if (!rn->rn_done) - return; + goto out_unlock; rn->rn_done = NULL; + rn->rn_get = NULL; + rn->rn_put = 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); } /** @@ -178,15 +192,23 @@ static void rpcrdma_remove_one(struct ib_device *device, { struct rpcrdma_device *rd = client_data; struct rpcrdma_notification *rn; + void (*done)(struct rpcrdma_notification *rn); unsigned long index; trace_rpcrdma_client_remove_one(device); mutex_lock(&rpcrdma_device_mutex); set_bit(RPCRDMA_RD_F_REMOVING, &rd->rd_flags); + xa_for_each(&rd->rd_xa, index, rn) { + if (!rn->rn_get(rn)) + continue; + done = rn->rn_done; + mutex_unlock(&rpcrdma_device_mutex); + done(rn); + rn->rn_put(rn); + 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 927269598ac2..93beef3a2c69 100644 --- a/net/sunrpc/xprtrdma/svc_rdma_transport.c +++ b/net/sunrpc/xprtrdma/svc_rdma_transport.c @@ -424,6 +424,22 @@ static void svc_rdma_xprt_done(struct rpcrdma_notification *rn) svc_xprt_close(&rdma->sc_xprt); } +static bool svc_rdma_xprt_get(struct rpcrdma_notification *rn) +{ + struct svcxprt_rdma *rdma = container_of(rn, struct svcxprt_rdma, + sc_rn); + + return kref_get_unless_zero(&rdma->sc_xprt.xpt_ref); +} + +static void svc_rdma_xprt_put(struct rpcrdma_notification *rn) +{ + struct svcxprt_rdma *rdma = container_of(rn, struct svcxprt_rdma, + sc_rn); + + svc_xprt_put(&rdma->sc_xprt); +} + /* * This is the xpo_recvfrom function for listening endpoints. Its * purpose is to accept incoming connections. The CMA callback handler @@ -464,7 +480,8 @@ static struct svc_xprt *svc_rdma_accept(struct svc_xprt *xprt) dev = newxprt->sc_cm_id->device; newxprt->sc_port_num = newxprt->sc_cm_id->port_num; - if (rpcrdma_rn_register(dev, &newxprt->sc_rn, svc_rdma_xprt_done)) + if (rpcrdma_rn_register(dev, &newxprt->sc_rn, svc_rdma_xprt_done, + svc_rdma_xprt_get, svc_rdma_xprt_put)) goto errout; newxprt->sc_max_req_size = svcrdma_max_req_size; diff --git a/net/sunrpc/xprtrdma/verbs.c b/net/sunrpc/xprtrdma/verbs.c index be335eed329d..9b00e71c80a7 100644 --- a/net/sunrpc/xprtrdma/verbs.c +++ b/net/sunrpc/xprtrdma/verbs.c @@ -297,6 +297,22 @@ static void rpcrdma_ep_removal_done(struct rpcrdma_notification *rn) xprt_force_disconnect(ep->re_xprt); } +static bool rpcrdma_ep_removal_get(struct rpcrdma_notification *rn) +{ + struct rpcrdma_ep *ep = container_of(rn, struct rpcrdma_ep, re_rn); + + return kref_get_unless_zero(&ep->re_kref); +} + +static void rpcrdma_ep_removal_put(struct rpcrdma_notification *rn) +{ + struct rpcrdma_ep *ep = container_of(rn, struct rpcrdma_ep, re_rn); + struct rdma_cm_id *id = ep->re_id; + + if (rpcrdma_ep_put(ep)) + rdma_destroy_id(id); +} + static struct rdma_cm_id *rpcrdma_create_id(struct rpcrdma_xprt *r_xprt, struct rpcrdma_ep *ep) { @@ -337,7 +353,10 @@ static struct rdma_cm_id *rpcrdma_create_id(struct rpcrdma_xprt *r_xprt, goto out; ep->re_id = id; - rc = rpcrdma_rn_register(id->device, &ep->re_rn, rpcrdma_ep_removal_done); + rc = rpcrdma_rn_register(id->device, &ep->re_rn, + rpcrdma_ep_removal_done, + rpcrdma_ep_removal_get, + rpcrdma_ep_removal_put); if (rc) goto out; -- 2.34.1