Linux NFS development
 help / color / mirror / Atom feed
From: Jeff Layton <jlayton@kernel.org>
To: Anna Schumaker <anna@kernel.org>,
	linux-nfs@vger.kernel.org,  trond.myklebust@hammerspace.com
Subject: Re: [PATCH 3/4] SUNRPC: Create a helper function for accessing the rpc_clnt's xprt_switch
Date: Sun, 03 Dec 2023 07:24:54 -0500	[thread overview]
Message-ID: <79944d1c5e20173de5e3b818816a1c3ce172968a.camel@kernel.org> (raw)
In-Reply-To: <20231201211549.126941-4-anna@kernel.org>

On Fri, 2023-12-01 at 16:15 -0500, Anna Schumaker wrote:
> From: Anna Schumaker <Anna.Schumaker@Netapp.com>
> 
> This function takes the necessary rcu read lock to dereference the
> client's rpc_xprt_switch and bump the reference count so it doesn't
> disappear underneath us before returning. This does mean that callers
> are responsible for calling xprt_switch_put() on the returned object
> when they are done with it.
> 
> Signed-off-by: Anna Schumaker <Anna.Schumaker@Netapp.com>
> ---
>  net/sunrpc/clnt.c | 34 +++++++++++++++++++++-------------
>  1 file changed, 21 insertions(+), 13 deletions(-)
> 
> diff --git a/net/sunrpc/clnt.c b/net/sunrpc/clnt.c
> index 8df944444e9b..0b2c4b5484f5 100644
> --- a/net/sunrpc/clnt.c
> +++ b/net/sunrpc/clnt.c
> @@ -797,15 +797,24 @@ int rpc_switch_client_transport(struct rpc_clnt *clnt,
>  }
>  EXPORT_SYMBOL_GPL(rpc_switch_client_transport);
>  
> +static struct rpc_xprt_switch *rpc_clnt_xprt_switch_get(struct rpc_clnt *clnt)
> +{
> +	struct rpc_xprt_switch *xps;
> +
> +	rcu_read_lock();
> +	xps = xprt_switch_get(rcu_dereference(clnt->cl_xpi.xpi_xpswitch));
> +	rcu_read_unlock();
> +
> +	return xps;
> +}
> +
>  static
>  int _rpc_clnt_xprt_iter_init(struct rpc_clnt *clnt, struct rpc_xprt_iter *xpi,
>  			     void func(struct rpc_xprt_iter *xpi, struct rpc_xprt_switch *xps))
>  {
>  	struct rpc_xprt_switch *xps;
>  
> -	rcu_read_lock();
> -	xps = xprt_switch_get(rcu_dereference(clnt->cl_xpi.xpi_xpswitch));
> -	rcu_read_unlock();
> +	xps = rpc_clnt_xprt_switch_get(clnt);
>  	if (xps == NULL)
>  		return -EAGAIN;
>  	func(xpi, xps);
> @@ -2206,9 +2215,7 @@ call_connect_status(struct rpc_task *task)
>  			struct rpc_xprt *saved = task->tk_xprt;
>  			struct rpc_xprt_switch *xps;
>  
> -			rcu_read_lock();
> -			xps = xprt_switch_get(rcu_dereference(clnt->cl_xpi.xpi_xpswitch));
> -			rcu_read_unlock();
> +			xps = rpc_clnt_xprt_switch_get(clnt);
>  			if (xps->xps_nxprts > 1) {
>  				long value;
>  
> @@ -3251,22 +3258,23 @@ void rpc_clnt_xprt_set_online(struct rpc_clnt *clnt, struct rpc_xprt *xprt)
>  {
>  	struct rpc_xprt_switch *xps;
>  
> -	rcu_read_lock();
> -	xps = rcu_dereference(clnt->cl_xpi.xpi_xpswitch);
> -	rcu_read_unlock();
> +	xps = rpc_clnt_xprt_switch_get(clnt);
>  	xprt_set_online_locked(xprt, xps);
> +	xprt_switch_put(xps);
>  }

FWIW, it looks like the above fixes a real bug. It's almost certainly
not safe to dereference xps once you drop the rcu_read_lock there.

>  
>  void rpc_clnt_xprt_switch_add_xprt(struct rpc_clnt *clnt, struct rpc_xprt *xprt)
>  {
> +	struct rpc_xprt_switch *xps;
> +
>  	if (rpc_clnt_xprt_switch_has_addr(clnt,
>  		(const struct sockaddr *)&xprt->addr)) {
>  		return rpc_clnt_xprt_set_online(clnt, xprt);
>  	}
> -	rcu_read_lock();
> -	rpc_xprt_switch_add_xprt(rcu_dereference(clnt->cl_xpi.xpi_xpswitch),
> -				 xprt);
> -	rcu_read_unlock();
> +
> +	xps = rpc_clnt_xprt_switch_get(clnt);
> +	rpc_xprt_switch_add_xprt(xps, xprt);
> +	xprt_switch_put(xps);
>  }
>  EXPORT_SYMBOL_GPL(rpc_clnt_xprt_switch_add_xprt);
>  

Reviewed-by: Jeff Layton <jlayton@kernel.org>

  reply	other threads:[~2023-12-03 12:24 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-12-01 21:15 [PATCH 0/4] SUNRPC: Various RCU fixes Anna Schumaker
2023-12-01 21:15 ` [PATCH 1/4] SUNRPC: Clean up unused variable in rpc_xprt_probe_trunked() Anna Schumaker
2023-12-03 12:18   ` Jeff Layton
2023-12-01 21:15 ` [PATCH 2/4] SUNRPC: Remove unused function rpc_clnt_xprt_switch_put() Anna Schumaker
2023-12-03 12:19   ` Jeff Layton
2023-12-01 21:15 ` [PATCH 3/4] SUNRPC: Create a helper function for accessing the rpc_clnt's xprt_switch Anna Schumaker
2023-12-03 12:24   ` Jeff Layton [this message]
2023-12-01 21:15 ` [PATCH 4/4] SUNRPC: Fix a suspicious RCU usage warning Anna Schumaker
2023-12-03 12:30   ` Jeff Layton
2023-12-04 17:00     ` Anna Schumaker

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=79944d1c5e20173de5e3b818816a1c3ce172968a.camel@kernel.org \
    --to=jlayton@kernel.org \
    --cc=anna@kernel.org \
    --cc=linux-nfs@vger.kernel.org \
    --cc=trond.myklebust@hammerspace.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