linux-nfs.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Jeff Layton <jlayton@kernel.org>
To: NeilBrown <neilb@suse.de>
Cc: chuck.lever@oracle.com, linux-nfs@vger.kernel.org
Subject: Re: [PATCH 1/3] nfsd: nfsd_do_file_acquire should hold rcu_read_lock while getting refs
Date: Sat, 01 Oct 2022 05:47:40 -0400	[thread overview]
Message-ID: <63286cf2604c30671e9cc2b73467f28e9dd96064.camel@kernel.org> (raw)
In-Reply-To: <166459949085.17572.9387753773056673569@noble.neil.brown.name>

On Sat, 2022-10-01 at 14:44 +1000, NeilBrown wrote:
> On Sat, 01 Oct 2022, Jeff Layton wrote:
> > nfsd_file is RCU-freed, so it's possible that one could be found that's
> > in the process of being freed and the memory recycled. Ensure we hold
> > the rcu_read_lock while attempting to get a reference on the object.
> > 
> > Signed-off-by: Jeff Layton <jlayton@kernel.org>
> > ---
> >  fs/nfsd/filecache.c | 9 ++++++++-
> >  1 file changed, 8 insertions(+), 1 deletion(-)
> > 
> > diff --git a/fs/nfsd/filecache.c b/fs/nfsd/filecache.c
> > index d5c57360b418..6237715bd23e 100644
> > --- a/fs/nfsd/filecache.c
> > +++ b/fs/nfsd/filecache.c
> > @@ -1077,10 +1077,12 @@ nfsd_file_do_acquire(struct svc_rqst *rqstp, struct svc_fh *fhp,
> >  
> >  retry:
> >  	/* Avoid allocation if the item is already in cache */
> > +	rcu_read_lock();
> >  	nf = rhashtable_lookup_fast(&nfsd_file_rhash_tbl, &key,
> >  				    nfsd_file_rhash_params);
> >  	if (nf)
> >  		nf = nfsd_file_get(nf);
> > +	rcu_read_unlock();
> 
> Looks good.
> 
> >  	if (nf)
> >  		goto wait_for_construction;
> >  
> > @@ -1090,16 +1092,21 @@ nfsd_file_do_acquire(struct svc_rqst *rqstp, struct svc_fh *fhp,
> >  		goto out_status;
> >  	}
> >  
> > +	rcu_read_lock();
> >  	nf = rhashtable_lookup_get_insert_key(&nfsd_file_rhash_tbl,
> >  					      &key, &new->nf_rhash,
> >  					      nfsd_file_rhash_params);
> >  	if (!nf) {
> > +		rcu_read_unlock();
> >  		nf = new;
> >  		goto open_file;
> >  	}
> > -	if (IS_ERR(nf))
> > +	if (IS_ERR(nf)) {
> > +		rcu_read_unlock();
> >  		goto insert_err;
> > +	}
> >  	nf = nfsd_file_get(nf);
> > +	rcu_read_unlock();
> 
> Ugh.
> Could we make this:
>    rcu_read_lock()
>    nf = r_l_g_i_k()
>    if (!IS_ERR_OR_NULL(nf))
>         nf = nfsd_file_get(nf);
>    rcu_read_unlock()
>    ...
> ??
> 
> NeilBrown
> 

Good point. I'll resend a v2 here.

> >  	if (nf == NULL) {
> >  		nf = new;
> >  		goto open_file;
> > -- 
> > 2.37.3
> > 
> > 

-- 
Jeff Layton <jlayton@kernel.org>

  reply	other threads:[~2022-10-01  9:47 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-09-30 19:15 [PATCH 0/3] nfsd: filecache fixes Jeff Layton
2022-09-30 19:15 ` [PATCH 1/3] nfsd: nfsd_do_file_acquire should hold rcu_read_lock while getting refs Jeff Layton
2022-09-30 19:20   ` Chuck Lever III
2022-09-30 19:33     ` Jeff Layton
2022-09-30 20:06       ` Chuck Lever III
2022-10-01  4:44   ` NeilBrown
2022-10-01  9:47     ` Jeff Layton [this message]
2022-09-30 19:15 ` [PATCH 2/3] nfsd: fix potential race in nfsd_file_close Jeff Layton
2022-09-30 20:58   ` Jeff Layton
2022-09-30 20:59     ` Chuck Lever III
2022-10-01  5:03   ` NeilBrown
2022-10-01  9:55     ` Jeff Layton
2022-09-30 19:15 ` [PATCH 3/3] nfsd: fix nfsd_file_unhash_and_dispose Jeff Layton
2022-09-30 19:29   ` Chuck Lever III
2022-09-30 19:42     ` Jeff Layton
2022-09-30 20:23       ` Chuck Lever III

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=63286cf2604c30671e9cc2b73467f28e9dd96064.camel@kernel.org \
    --to=jlayton@kernel.org \
    --cc=chuck.lever@oracle.com \
    --cc=linux-nfs@vger.kernel.org \
    --cc=neilb@suse.de \
    /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;
as well as URLs for NNTP newsgroup(s).