rpcrdma_rn_register() checks RPCRDMA_RD_F_REMOVING before publishing the notification and taking a reference on the rpcrdma_device. Nothing serializes these operations with rpcrdma_remove_one(). Device removal can therefore free the private data after the check but before xa_alloc() or kref_get(), resulting in a use-after-free. It can also finish its notification walk before a new registration is published, leaving the removal path waiting for a registrant that was never notified. Use a module-wide mutex to protect the client-data lookup, removal-state check, notification publication and reference acquisition against the transition to REMOVING. Clear client-data under the same mutex before destroying the XArray and freeing the private data, so a later lookup cannot return a stale pointer. Keep notification callbacks and completion waits outside the critical sections. Fixes: 7e86845a0346 ("rpcrdma: Implement generic device removal") Signed-off-by: Li Xiasong --- net/sunrpc/xprtrdma/ib_client.c | 30 ++++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/net/sunrpc/xprtrdma/ib_client.c b/net/sunrpc/xprtrdma/ib_client.c index 188f7a13397f..23ab1dfcb230 100644 --- a/net/sunrpc/xprtrdma/ib_client.c +++ b/net/sunrpc/xprtrdma/ib_client.c @@ -9,6 +9,7 @@ #include #include #include +#include #include #include @@ -28,6 +29,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,15 +62,20 @@ 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. */ 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; + int ret = -ENETUNREACH; + 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 @@ -78,11 +89,15 @@ int rpcrdma_rn_register(struct ib_device *device, 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); + return ret; } static void rpcrdma_rn_release(struct kref *kref) @@ -163,6 +178,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 +190,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 +206,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