mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Jonathan Flynn <jonathan.flynn@hammerspace.com>
To: NeilBrown <neilb@ownmail.net>
Cc: Trond Myklebust <trondmy@kernel.org>,
	Anna Schumaker <anna@kernel.org>,
	Alexander Viro <viro@zeniv.linux.org.uk>,
	Christian Brauner <brauner@kernel.org>,
	Jeff Layton <jlayton@kernel.org>, Jan Kara <jack@suse.cz>,
	linux-fsdevel@vger.kernel.org, linux-nfs@vger.kernel.org,
	linux-kernel@vger.kernel.org
Subject: [PATCH] nfs: set NFS_FSDATA_BLOCKED before invalidating d_seq
Date: Fri, 23 Oct 2026 13:22:15 -0600	[thread overview]
Message-ID: <20261023192215.1795577-1-jonathan.flynn@hammerspace.com> (raw)
In-Reply-To: <20261023015239.2163480-2-neilb@ownmail.net>

block_revalidate() calls write_seqcount_invalidate(&dentry->d_seq) and
only then sets dentry->d_fsdata to NFS_FSDATA_BLOCKED.  Because ->d_seq
is invalidated before NFS_FSDATA_BLOCKED is set, an RCU-walk open can
get past an unlink that is in progress.

The barrier in write_seqcount_invalidate() is issued before the
increment, so it orders only the stores made before the call.  An
RCU-walk lookup that samples ->d_seq after the increment can still read
the old ->d_fsdata, and so ->d_revalidate() accepts the dentry.  The
walker then waits for ->d_lock in lockref_get_not_dead(), takes its
reference, and passes the ->d_seq recheck in __legitimize_path(),
because ->d_seq still holds the value the walker sampled.  By then
nfs_unlink() has seen d_count() == 1 and sends the REMOVE instead of
silly-renaming, so the open proceeds on a file the server is removing.

Set NFS_FSDATA_BLOCKED first, then call write_seqcount_invalidate().  A
walker that samples the new ->d_seq then also sees NFS_FSDATA_BLOCKED
and returns -ECHILD.  A walker that sampled the old ->d_seq fails the
recheck and retries in ref-walk, which waits for the unlink to finish.
__d_drop() follows the same order: unhash the dentry, then invalidate
->d_seq.

Also correct the comment in block_revalidate(): the ->d_seq recheck is
in __legitimize_path() and try_to_unlazy_next(), not in
legitimize_mnt().

Signed-off-by: Jonathan Flynn <jonathan.flynn@hammerspace.com>
---
Neil, this answers Sashiko's second point on 1/9: v3 still calls
write_seqcount_invalidate() before it sets NFS_FSDATA_BLOCKED.  This
patch applies on top of v3; fold it into 1/9 if you prefer.

The window with the v3 order:

  CPU0: nfs_unlink(), ->d_lock held     CPU1: RCU-walk open, same name
  d_count() == 1, no silly-rename
  write_seqcount_invalidate(&d_seq)
                                        __d_lookup_rcu() samples the
                                          new ->d_seq
                                        ->d_revalidate() reads ->d_fsdata,
                                          not yet NFS_FSDATA_BLOCKED
  d_fsdata = NFS_FSDATA_BLOCKED
  spin_unlock(&dentry->d_lock)
  REMOVE sent                           lockref_get_not_dead() takes a ref
                                        read_seqcount_retry() passes
                                        the open proceeds

Your afs 1/5 also sets DCACHE_BLOCKED after write_seqcount_invalidate().

 fs/nfs/dir.c | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)

diff --git a/fs/nfs/dir.c b/fs/nfs/dir.c
index 53cd3adf3ffe..79331e0fa044 100644
--- a/fs/nfs/dir.c
+++ b/fs/nfs/dir.c
@@ -1879,15 +1879,20 @@ static void block_revalidate(struct dentry *dentry)
 	/* Any new reference that could lead to an open
 	 * will either:
 	 *  - take ->d_lock in lookup_open() -> d_lookup() or
-	 *  - will check d_seq in legitimize_mnt()
+	 *  - will check d_seq in __legitimize_path() or try_to_unlazy_next()
 	 *
 	 * Holding this lock and invalidating ->d_seq ensures we cannot
 	 * race with __nfs_lookup_revalidate().
 	 */
 	lockdep_assert_held(&dentry->d_lock);
-	write_seqcount_invalidate(&dentry->d_seq);
-
 	dentry->d_fsdata = NFS_FSDATA_BLOCKED;
+
+	/*
+	 * Set NFS_FSDATA_BLOCKED before invalidating ->d_seq: the barrier in
+	 * write_seqcount_invalidate() orders only earlier stores, so an
+	 * RCU-walk that samples the new ->d_seq also sees NFS_FSDATA_BLOCKED.
+	 */
+	write_seqcount_invalidate(&dentry->d_seq);
 }
 
 static void unblock_revalidate(struct dentry *dentry)
-- 
2.43.0


  reply	other threads:[~2026-10-23 19:22 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-10-23  1:50 [PATCH v3 0/9] nfs: prepare for locking changes NeilBrown
2026-10-23  1:50 ` [PATCH v3 1/9] nfs: fix open-blocking with d_fsdata NeilBrown
2026-10-23 19:22   ` Jonathan Flynn [this message]
2026-10-23  1:50 ` [PATCH v3 2/9] nfs: fix nfs_call_unlink() NeilBrown
2026-10-23  1:50 ` [PATCH v3 3/9] nfs: remove d_drop()/d_alloc_parallel() from nfs_atomic_open() NeilBrown
2026-10-23  1:50 ` [PATCH v3 4/9] nfs: use d_splice_alias() in nfs_link() NeilBrown
2026-10-23  1:50 ` [PATCH v3 5/9] nfs: don't d_drop() before d_splice_alias() NeilBrown
2026-10-23  1:50 ` [PATCH v3 6/9] nfs: don't d_drop() before d_splice_alias() in atomic_create NeilBrown
2026-10-23  1:50 ` [PATCH v3 7/9] nfs: Use d_alloc_trylock() in nfs_prime_dcache() NeilBrown
2026-10-23  1:50 ` [PATCH v3 8/9] nfs: use d_alloc_trylock() in silly-rename NeilBrown
2026-10-23  1:50 ` [PATCH v3 9/9] nfs: use d_duplicate() NeilBrown

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=20261023192215.1795577-1-jonathan.flynn@hammerspace.com \
    --to=jonathan.flynn@hammerspace.com \
    --cc=anna@kernel.org \
    --cc=brauner@kernel.org \
    --cc=jack@suse.cz \
    --cc=jlayton@kernel.org \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-nfs@vger.kernel.org \
    --cc=neilb@ownmail.net \
    --cc=trondmy@kernel.org \
    --cc=viro@zeniv.linux.org.uk \
    /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

all inboxes | Powered by JetHome®