protect access to bc_serv
Subsystems (3)
Olga Kornievskaia
reviewerv2 cover protect access to bc_serv
This patch series tries to address the problem that can arise when the client is shutting down but gets a backchannel request. As it stands now, In nfs4_shutdown_client() we call nfs4_destroy_callback() which would destroy/free the bc_serv structure stored in an xprt structure. But the xprt structure is still around for a bit until it's destroyed. If we get backchannel request after svc_destroy() is called, then in xprt_complete_bc_request() we are accessing freed memory.
I propose to use bc_pa_lock lock to protect access to the bc_serv structure and make sure we free/nullify bc_serv under the lock and then access it under a lock in xs_complete_bs_request() and rpcrdma_bc_receive_call().
-- v2 fixing kernel test bot reported compile error in usage of xprt_svc_destroy_nullify_bc (CONFIG_SUNRPC_BACKCHANNEL toggle related)
Olga Kornievskaia (3):
NFSv4.1: pass transport for callback shutdown
SUNRPC: protect access to rpc_xprt bc_serv NFSv4.1: protect destroying and nullifying bc_serv structure
fs/nfs/callback.c | 11 +++++++++-- fs/nfs/callback.h | 3 ++- fs/nfs/nfs4client.c | 9 +++++++-- include/linux/sunrpc/bc_xprt.h | 1 + net/sunrpc/backchannel_rqst.c | 27 ++++++++++++++++++++++++--- net/sunrpc/xprtrdma/backchannel.c | 11 +++++++---- 6 files changed, 50 insertions(+), 12 deletions(-)
Signature · 1 line
2.47.1
Olga Kornievskaia
reviewerPATCH v2 1/3 NFSv4.1: pass transport for callback shutdown
When we are setting up the 4.1 callback server, we pass in the appropriate rpc_xprt transport pointer with which to associate the callback server structure. Similarly, pass in the rpc_xprt pointer for when we are shutting down the callback. This will be used to make sure that we free the server structure and then clear the rpc_xprt's bc_server pointer in a safe manner.
--- fs/nfs/callback.c | 2 +- fs/nfs/callback.h | 3 ++- fs/nfs/nfs4client.c | 9 +++++++-- 3 files changed, 10 insertions(+), 4 deletions(-)
fs/nfs/callback.c +1 -1
@@ -258,7 +258,7 @@ int nfs_callback_up(u32 minorversion, struct rpc_xprt *xprt)/* * Kill the callback thread if it's no longer being used. */ -void nfs_callback_down (int minorversion,struct net *net) +void nfs_callback_down (int minorversion,struct net *net,struct rpc_xprt *xprt) {struct nfs_callback_data *cb_info = &nfs_callback_info[minorversion];struct svc_serv *serv;
fs/nfs/callback.h +2 -1
@@ -188,7 +188,8 @@ extern __be32 nfs4_callback_recall(void *argp, void *resp, struct cb_process_state *cps);#if IS_ENABLED (CONFIG_NFS_V4 )extern int nfs_callback_up (u32 minorversion,struct rpc_xprt *xprt); -extern void nfs_callback_down (int minorversion,struct net *net); +extern void nfs_callback_down (int minorversion,struct net *net, +struct rpc_xprt *xprt);#endif /* CONFIG_NFS_V4 */ /* * nfs41: Callbacks are expected to not cause substantial latency,
fs/nfs/nfs4client.c +7 -2
@@ -280,8 +280,13 @@ struct nfs_client *nfs4_alloc_client(const struct nfs_client_initdata *cl_init)*/ static void nfs4_destroy_callback (struct nfs_client *clp) { -if (__test_and_clear_bit (NFS_CS_CALLBACK , &clp->cl_res_state)) -nfs_callback_down (clp->cl_mvops->minor_version, clp->cl_net); +if (__test_and_clear_bit (NFS_CS_CALLBACK , &clp->cl_res_state)) { +struct rpc_xprt *xprt; + + xprt =rcu_dereference_raw (clp->cl_rpcclient->cl_xprt); +nfs_callback_down (clp->cl_mvops->minor_version, clp->cl_net, + xprt); + } }static void nfs4_shutdown_client (struct nfs_client *clp)
Signature · 1 line
2.47.1
Olga Kornievskaia
reviewerPATCH v2 2/3 SUNRPC: protect access to rpc_xprt bc_serv
In reply to Olga KornievskaiaWe need to protect access to bc_serv usage thru the bc_pa_lock and make sure that backchannel request processing is accessing a valid pointer.
--- include/linux/sunrpc/bc_xprt.h | 1 + net/sunrpc/backchannel_rqst.c | 27 ++++++++++++++++++++++++--- net/sunrpc/xprtrdma/backchannel.c | 11 +++++++---- 3 files changed, 32 insertions(+), 7 deletions(-)
include/linux/sunrpc/bc_xprt.h +1 -0
@@ -31,6 +31,7 @@ int xprt_setup_bc(struct rpc_xprt *xprt, unsigned int min_reqs);void xprt_destroy_bc (struct rpc_xprt *xprt,unsigned int max_reqs);void xprt_free_bc_rqst (struct rpc_rqst *req);unsigned int xprt_bc_max_slots (struct rpc_xprt *xprt); +void xprt_svc_destroy_nullify_bc (struct rpc_xprt *xprt,struct svc_serv **serv);/* * Determine if a shared backchannel is in use
net/sunrpc/backchannel_rqst.c +24 -3
@@ -24,6 +24,22 @@ unsigned int xprt_bc_max_slots(struct rpc_xprt *xprt)return BC_MAX_SLOTS ; } +/* +* Helper function to nullify backchannel server pointer in transport. +* We need to synchronize setting the pointer to NULL (done so after +* the backchannel server is shutdown) with the usage of that pointer +* by the backchannel request processing routines +* xprt_complete_bc_request() and rpcrdma_bc_receive_call(). +*/ +void xprt_svc_destroy_nullify_bc (struct rpc_xprt *xprt,struct svc_serv **serv) +{ +spin_lock (&xprt->bc_pa_lock); +svc_destroy (serv); + xprt->bc_serv =NULL ; +spin_unlock (&xprt->bc_pa_lock); +} +EXPORT_SYMBOL_GPL (xprt_svc_destroy_nullify_bc); +/* * Helper routines that track the number of preallocation elements * on the transport.
@@ -354,7 +370,7 @@ struct rpc_rqst *xprt_lookup_bc_request(struct rpc_xprt *xprt, __be32 xid)void xprt_complete_bc_request (struct rpc_rqst *req,uint32_t copied) {struct rpc_xprt *xprt = req->rq_xprt; -struct svc_serv *bc_serv = xprt->bc_serv; +struct svc_serv *bc_serv;spin_lock (&xprt->bc_pa_lock);list_del (&req->rq_bc_pa_list);
@@ -366,6 +382,11 @@ void xprt_complete_bc_request(struct rpc_rqst *req, uint32_t copied)dprintk ("RPC: add callback request to list\n" );xprt_get (xprt); -lwq_enqueue (&req->rq_bc_list, &bc_serv->sv_cb_list); -svc_pool_wake_idle_thread (&bc_serv->sv_pools[0 ]); +spin_lock (&xprt->bc_pa_lock); + bc_serv = xprt->bc_serv; +if (bc_serv) { +lwq_enqueue (&req->rq_bc_list, &bc_serv->sv_cb_list); +svc_pool_wake_idle_thread (&bc_serv->sv_pools[0 ]); + } +spin_unlock (&xprt->bc_pa_lock); }
net/sunrpc/xprtrdma/backchannel.c +7 -4
@@ -261,11 +261,14 @@ void rpcrdma_bc_receive_call(struct rpcrdma_xprt *r_xprt,trace_xprtrdma_cb_call (r_xprt, rqst);/* Queue rqst for ULP's callback service */ - bc_serv = xprt->bc_serv;xprt_get (xprt); -lwq_enqueue (&rqst->rq_bc_list, &bc_serv->sv_cb_list); - -svc_pool_wake_idle_thread (&bc_serv->sv_pools[0 ]); +spin_lock (&xprt->bc_pa_lock); + bc_serv = xprt->bc_serv; +if (bc_serv) { +lwq_enqueue (&rqst->rq_bc_list, &bc_serv->sv_cb_list); +svc_pool_wake_idle_thread (&bc_serv->sv_pools[0 ]); + } +spin_lock (&xprt->bc_pa_lock); r_xprt->rx_stats.bcall_count++;return ;
Signature · 1 line
2.47.1
Re: [PATCH v2 2/3] SUNRPC: protect access to rpc_xprt bc_serv
Hi Olga,
Quoted 62 lines · Olga Kornievskaia
We need to protect access to
bc_servusage thru thebc_pa_lockand make sure that backchannel request processing is accessing a valid pointer.Signed-off-by Olga Kornievskaia <[email protected]>--- include/linux/sunrpc/bc_xprt.h | 1 + net/sunrpc/backchannel_rqst.c | 27 ++++++++++++++++++++++++--- net/sunrpc/xprtrdma/backchannel.c | 11 +++++++---- 3 files changed, 32 insertions(+), 7 deletions(-)include/linux/sunrpc/bc_xprt.h +1
-0@@ -31,6 +31,7 @@ int xprt_setup_bc(struct rpc_xprt *xprt, unsigned int min_reqs);void xprt_destroy_bc (struct rpc_xprt *xprt,unsigned int max_reqs);void xprt_free_bc_rqst (struct rpc_rqst *req);unsigned int xprt_bc_max_slots (struct rpc_xprt *xprt); +void xprt_svc_destroy_nullify_bc (struct rpc_xprt *xprt,struct svc_serv **serv);/* * Determine if a shared backchannel is in use net/sunrpc/backchannel_rqst.c +17
-1@@ -24,6 +24,22 @@ unsigned int xprt_bc_max_slots(struct rpc_xprt *xprt)return BC_MAX_SLOTS ; } +/* +* Helper function to nullify backchannel server pointer in transport. +* We need to synchronize setting the pointer to NULL (done so after +* the backchannel server is shutdown) with the usage of that pointer +* by the backchannel request processing routines +* xprt_complete_bc_request() and rpcrdma_bc_receive_call(). +*/ +void xprt_svc_destroy_nullify_bc (struct rpc_xprt *xprt,struct svc_serv **serv) +{ +spin_lock (&xprt->bc_pa_lock); +svc_destroy (serv); + xprt->bc_serv =NULL ; +spin_unlock (&xprt->bc_pa_lock); +} +EXPORT_SYMBOL_GPL (xprt_svc_destroy_nullify_bc); +/* * Helper routines that track the number of preallocation elements * on the transport. @@ -354,7 +370,7 @@ struct rpc_rqst *xprt_lookup_bc_request(struct rpc_xprt *xprt, __be32 xid)void xprt_complete_bc_request (struct rpc_rqst *req,uint32_t copied) {struct rpc_xprt *xprt = req->rq_xprt; -struct svc_serv *bc_serv = xprt->bc_serv; +struct svc_serv *bc_serv;spin_lock (&xprt->bc_pa_lock);list_del (&req->rq_bc_pa_list);@@ -366,6 +382,11 @@ void xprt_complete_bc_request(struct rpc_rqst *req, uint32_t copied)dprintk ("RPC: add callback request to list\n" );
net/sunrpc/backchannel_rqst.c void xprt_complete_bc_request(struct rpc_rqst *req, uint32_t copied) · line 391
Quoted 10 lines
net/sunrpc/backchannel_rqst.c +7
-2xprt_get (xprt); -lwq_enqueue (&req->rq_bc_list, &bc_serv->sv_cb_list); -svc_pool_wake_idle_thread (&bc_serv->sv_pools[0 ]); +spin_lock (&xprt->bc_pa_lock); + bc_serv = xprt->bc_serv; +if (bc_serv) { +lwq_enqueue (&req->rq_bc_list, &bc_serv->sv_cb_list); +svc_pool_wake_idle_thread (&bc_serv->sv_pools[0 ]); + } +spin_unlock (&xprt->bc_pa_lock);
Can this be done in a helper function that is also called from rpcrdma_bc_receive_call() to reduce code duplication?
Thanks,
Anna
Quoted 24 lines
net/sunrpc/backchannel_rqst.c +0
-0}net/sunrpc/xprtrdma/backchannel.c +7
-4@@ -261,11 +261,14 @@ void rpcrdma_bc_receive_call(struct rpcrdma_xprt *r_xprt,trace_xprtrdma_cb_call (r_xprt, rqst);/* Queue rqst for ULP's callback service */ - bc_serv = xprt->bc_serv;xprt_get (xprt); -lwq_enqueue (&rqst->rq_bc_list, &bc_serv->sv_cb_list); - -svc_pool_wake_idle_thread (&bc_serv->sv_pools[0 ]); +spin_lock (&xprt->bc_pa_lock); + bc_serv = xprt->bc_serv; +if (bc_serv) { +lwq_enqueue (&rqst->rq_bc_list, &bc_serv->sv_cb_list); +svc_pool_wake_idle_thread (&bc_serv->sv_pools[0 ]); + } +spin_lock (&xprt->bc_pa_lock); r_xprt->rx_stats.bcall_count++;return ;
Olga Kornievskaia
reviewerPATCH v2 3/3 NFSv4.1: protect destroying and nullifying bc_serv structure
In reply to Olga KornievskaiaWhen we are shutting down the client, we free the callback server structure and then at a later pointer we free the transport used by the client. Yet, it's possible that after the callback server is freed, the transport receives a backchannel request at which point we can dereferene freed memory. Instead, do the freeing the bc server and nullying bc_serv under the lock.
--- fs/nfs/callback.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-)
fs/nfs/callback.c +8 -1
@@ -270,7 +270,14 @@ void nfs_callback_down(int minorversion, struct net *net, struct rpc_xprt *xprt)if (cb_info->users ==0 ) {svc_set_num_threads (serv,NULL ,0 );dprintk ("nfs_callback_down: service destroyed\n" ); -svc_destroy (&cb_info->serv); +if (!minorversion) +svc_destroy (&cb_info->serv); + else +#ifdef CONFIG_SUNRPC_BACKCHANNEL +xprt_svc_destroy_nullify_bc (xprt, &cb_info->serv); +#else +svc_destroy (&cb_info->serv); +#endif }mutex_unlock (&nfs_callback_mutex); }
Signature · 1 line
2.47.1
Re: [PATCH v2 3/3] NFSv4.1: protect destroying and nullifying bc_serv structure
Hi Olga,
Quoted 20 lines · Olga Kornievskaia
When we are shutting down the client, we free the callback server structure and then at a later pointer we free the transport used by the client. Yet, it's possible that after the callback server is freed, the transport receives a backchannel request at which point we can dereferene freed memory. Instead, do the freeing the bc server and nullying
bc_servunder the lock.Signed-off-by Olga Kornievskaia <[email protected]>--- fs/nfs/callback.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-)fs/nfs/callback.c +0
-0@@ -270,7 +270,14 @@ void nfs_callback_down(int minorversion, struct net *net, struct rpc_xprt *xprt)if (cb_info->users ==0 ) {svc_set_num_threads (serv,NULL ,0 );
fs/nfs/callback.c void nfs_callback_down(int minorversion, struct net *net, struct rpc_xprt *xprt) · line 280
Quoted 10 lines
fs/nfs/callback.c +8
-1dprintk ("nfs_callback_down: service destroyed\n" ); -svc_destroy (&cb_info->serv); +if (!minorversion) +svc_destroy (&cb_info->serv); + else +#ifdef CONFIG_SUNRPC_BACKCHANNEL +xprt_svc_destroy_nullify_bc (xprt, &cb_info->serv); +#else +svc_destroy (&cb_info->serv); +#endif
Is there any way to do this inside the sunrpc layer? Perhaps in the include/linux/sunrpc/bc_xprt.h file which already has a #ifdef CONFIG_SUNRPC_BACKCHANNEL section in it?
Thanks,
Anna
Quoted 3 lines
fs/nfs/callback.c +0
-0}mutex_unlock (&nfs_callback_mutex); }