* [PATCH v3 00/11] multiple-filesystems: prepare for changes to VFS locking
@ 2026-11-25 3:16 NeilBrown
2026-11-25 3:16 ` [PATCH v3 01/11] coda: don't d_drop() early NeilBrown
` (10 more replies)
0 siblings, 11 replies; 12+ messages in thread
From: NeilBrown @ 2026-11-25 3:16 UTC (permalink / raw)
To: Alexander Viro, Christian Brauner
Cc: David Howells, Marc Dionne, Jan Harkes, coda, Jan Kara,
Richard Weinberger, Anton Ivanov, Johannes Berg, Jeff Layton,
Amir Goldstein, linux-kernel, codalist, linux-fsdevel, linux-um,
linux-afs
(ACKs for hostfs and afs still sought).
For this version I have included the AFS patches, included
"use d_alloc_parallel() in lookup_one_qstr_excl().
The latter patch is needed to make it safe to use
d_alloc_parallel() outside of i_rwsem. This is, in turn,
needed for the three patch which drop i_rwsem before
calling d_alloc_parallel() (which is preparation for
inverting the order between i_rwsem and d_alloc_parallel()).
I've also reverted the LOOKUP_SHARED patch as I now see that was a
mistake.
Thanks,
NeilBrown
Original description, updated.
I am working on changes to directory locking. The medium term goal is
to lift d_alloc_parallel() locking above i_rwsem, so d_alloc_paralle()
can be run unlocked, and consequently cannot be called when ->i_rwsem is
held.
This patch set makes small changes to a number of filesystems which each
require just one patch. Patch series for nfs, cifs, fuse, cephfs
have been posted separately
The main requirements are:
1/ that d_alloc_parallel() not be called while i_rwsem is held.
Places which currently do this can be changed to use d_alloc_trylock(),
or d_duplicate(), or change to drop and retake the parent lock.
2/ a dentry must not be d_drop()ed while an operation is ongoing, as an
unlocked d_alloc_parallel() would then be able to create a new
dentry with the same name. d_splice_alias() can now work with a
hash dentry, so often the d_drop() can simply be dropped. Other
times it can be moved to after the operation has completed.
I think it would be best for these to land in the VFS tree and would
appreciate Acks from relevant maintainer.
[PATCH v3 01/11] coda: don't d_drop() early.
[PATCH v3 02/11] hostfs: don't d_drop() before d_splice_alias() in
[PATCH v3 03/11] afs: don't unhash/rehash dentries during
[PATCH v3 04/11] afs: use d_splice_alias() in afs_vnode_new_inode()
[PATCH v3 05/11] afs: use d_alloc_trylock in afs_sillyrename()
[PATCH v3 06/11] afs: use d_duplicate()
[PATCH v3 07/11] Revert "VFS: Add LOOKUP_SHARED flag."
[PATCH v3 08/11] VFS: use d_alloc_parallel() in
[PATCH v3 09/11] VFS: drop parent lock across d_alloc_parallel() in
[PATCH v3 10/11] procfs: drop parent lock for d_alloc_parallel() in
[PATCH v3 11/11] afs: lookup_atsys to drop and reclaim lock.
^ permalink raw reply [flat|nested] 12+ messages in thread
* [PATCH v3 01/11] coda: don't d_drop() early.
2026-11-25 3:16 [PATCH v3 00/11] multiple-filesystems: prepare for changes to VFS locking NeilBrown
@ 2026-11-25 3:16 ` NeilBrown
2026-11-25 3:16 ` [PATCH v3 02/11] hostfs: don't d_drop() before d_splice_alias() in hostfs_mkdir() NeilBrown
` (9 subsequent siblings)
10 siblings, 0 replies; 12+ messages in thread
From: NeilBrown @ 2026-11-25 3:16 UTC (permalink / raw)
To: Alexander Viro, Christian Brauner
Cc: David Howells, Marc Dionne, Jan Harkes, coda, Jan Kara,
Richard Weinberger, Anton Ivanov, Johannes Berg, Jeff Layton,
Amir Goldstein, linux-kernel, codalist, linux-fsdevel, linux-um,
linux-afs
From: NeilBrown <neil@brown.name>
Proposed locking changes will mean that calling d_drop() could
effectively unlock the name allowing a parallel lookup to proceed.
For this reason it should only be called *after* the attempt to create a
symlink (in this case) has completed (whether successfully or not).
So move the d_drop() to after the venus_symlink() call.
Doing this also means that if the upcall that handles venus_symlink()
happens to look up the name being created, it will get -ENOENT rather
then a possible deadlock.
Acked-by: Jan Harkes <jaharkes@cs.cmu.edu>
Signed-off-by: NeilBrown <neil@brown.name>
---
fs/coda/dir.c | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)
diff --git a/fs/coda/dir.c b/fs/coda/dir.c
index a85be5962e62..b47299527b65 100644
--- a/fs/coda/dir.c
+++ b/fs/coda/dir.c
@@ -249,13 +249,13 @@ static int coda_symlink(const struct mnt_idmap *idmap,
if (symlen > CODA_MAXPATHLEN)
return -ENAMETOOLONG;
+ error = venus_symlink(dir_inode->i_sb, coda_i2f(dir_inode), name, len,
+ symname, symlen);
/*
- * This entry is now negative. Since we do not create
+ * This entry is still negative. Since we did not create
* an inode for the entry we have to drop it.
*/
d_drop(de);
- error = venus_symlink(dir_inode->i_sb, coda_i2f(dir_inode), name, len,
- symname, symlen);
/* mtime is no good anymore */
if (!error)
base-commit: 4843f6873f7c4835e62f2c73ba0fd538e57a2f22
--
2.50.0.107.gf914562f5916.dirty
^ permalink raw reply [flat|nested] 12+ messages in thread
* [PATCH v3 02/11] hostfs: don't d_drop() before d_splice_alias() in hostfs_mkdir()
2026-11-25 3:16 [PATCH v3 00/11] multiple-filesystems: prepare for changes to VFS locking NeilBrown
2026-11-25 3:16 ` [PATCH v3 01/11] coda: don't d_drop() early NeilBrown
@ 2026-11-25 3:16 ` NeilBrown
2026-11-25 3:16 ` [PATCH v3 03/11] afs: don't unhash/rehash dentries during unlink/rename NeilBrown
` (8 subsequent siblings)
10 siblings, 0 replies; 12+ messages in thread
From: NeilBrown @ 2026-11-25 3:16 UTC (permalink / raw)
To: Alexander Viro, Christian Brauner
Cc: David Howells, Marc Dionne, Jan Harkes, coda, Jan Kara,
Richard Weinberger, Anton Ivanov, Johannes Berg, Jeff Layton,
Amir Goldstein, linux-kernel, codalist, linux-fsdevel, linux-um,
linux-afs
From: NeilBrown <neil@brown.name>
hostfs_mkdir() uses d_drop() and d_splice_alias() to ensure it has the
right dentry after a mkdir.
d_drop() is no longer needed here and will cause problem for future
changes to directory locking. So remove the d_drop().
Signed-off-by: NeilBrown <neil@brown.name>
---
fs/hostfs/hostfs_kern.c | 1 -
1 file changed, 1 deletion(-)
diff --git a/fs/hostfs/hostfs_kern.c b/fs/hostfs/hostfs_kern.c
index 613146e76dec..7cf25c29cb78 100644
--- a/fs/hostfs/hostfs_kern.c
+++ b/fs/hostfs/hostfs_kern.c
@@ -700,7 +700,6 @@ static struct dentry *hostfs_mkdir(const struct mnt_idmap *idmap, struct inode *
dentry = ERR_PTR(err);
} else {
inode = hostfs_iget(dentry->d_sb, file);
- d_drop(dentry);
dentry = d_splice_alias(inode, dentry);
}
__putname(file);
--
2.50.0.107.gf914562f5916.dirty
^ permalink raw reply [flat|nested] 12+ messages in thread
* [PATCH v3 03/11] afs: don't unhash/rehash dentries during unlink/rename
2026-11-25 3:16 [PATCH v3 00/11] multiple-filesystems: prepare for changes to VFS locking NeilBrown
2026-11-25 3:16 ` [PATCH v3 01/11] coda: don't d_drop() early NeilBrown
2026-11-25 3:16 ` [PATCH v3 02/11] hostfs: don't d_drop() before d_splice_alias() in hostfs_mkdir() NeilBrown
@ 2026-11-25 3:16 ` NeilBrown
2026-11-25 3:16 ` [PATCH v3 04/11] afs: use d_splice_alias() in afs_vnode_new_inode() NeilBrown
` (7 subsequent siblings)
10 siblings, 0 replies; 12+ messages in thread
From: NeilBrown @ 2026-11-25 3:16 UTC (permalink / raw)
To: Alexander Viro, Christian Brauner
Cc: David Howells, Marc Dionne, Jan Harkes, coda, Jan Kara,
Richard Weinberger, Anton Ivanov, Johannes Berg, Jeff Layton,
Amir Goldstein, linux-kernel, codalist, linux-fsdevel, linux-um,
linux-afs
From: NeilBrown <neil@brown.name>
afs needs to block lookup of dentries during unlink and rename.
There are two reasons:
1/ If the target is to be removed, not silly-renamed, the subsequent
opens cannot be allowed as the file won't exist on the server,
but they shouldn't fail until the unlink/rename completes.
2/ If the rename source is being moved between directories a lookup,
particularly d_revalidate, might change ->d_time asynchronously
with rename changing ->d_time with possible incorrect results.
afs currently unhashes the dentry to force a lookup which will wait on the
directory lock, and rehashes afterwards. This is incompatible with
proposed changed to directory locking which will require a dentry to
remain hashed throughout rename/unlink/etc operations.
This patch copies a mechanism developed for NFS but uses the new
DCACHE_PRIVATE flag rather then ->d_fsdata. DCACHE_PRIVATE is given
the private name DCACHE_BLOCKED within afs.
The flag is set when lookups and revalidates must be blocked.
d_revalidate checks for this flag and waits for it to become be cleared.
->d_lock is used to ensure d_revalidate never updates the time stamp in
->d_fsdata while DCACHE_BLOCKED is set.
For a ref-walk, ->d_lock provides all needed sequencing. It is taken in
__d_lookup() to increment the refcount and is held by afs when checking
the refcount and setting DCACHE_BLOCKED. d_revalidate() is called after
__d_lookup() so it will see DCACHE_BLOCKED if needed without any extra
barrier.
For RCU-walk, d_revalidate() is called before refcount is incremented,
so as well as setting DCACHE_BLOCKED we must
write_seqcount_invalidate(&dentry->d_seq).
If d_revalidate is called before that write_seqcount_invalidate(),
then the RCU-walk will be retried as ref-walk. If it is called
after, then DCACHE_BLOCKED will be seen providing we use smp_load_acquire().
The unlocking in afs_rename_edit_dir() is not needed. It was previously
needed because it would not be safe to d_rehash() after calling
d_move(). But now that we don't use d_rehash() that is not a concern.
afs_rename_put() is always called and is sufficient for all rename
unlocking.
Signed-off-by: NeilBrown <neil@brown.name>
---
fs/afs/dir.c | 101 +++++++++++++++++++++++++++++++++-------------
fs/afs/internal.h | 5 +--
2 files changed, 74 insertions(+), 32 deletions(-)
diff --git a/fs/afs/dir.c b/fs/afs/dir.c
index 75f8f70cfeff..fbe2a3eb56b0 100644
--- a/fs/afs/dir.c
+++ b/fs/afs/dir.c
@@ -49,6 +49,16 @@ static int afs_rename(const struct mnt_idmap *idmap, struct inode *old_dir,
static int afs_dir_writepages(struct address_space *mapping,
struct writeback_control *wbc);
+/*
+ * This is set to stop d_revalidate looking at, and possibly changing,
+ * ->d_fsdata (a timestamp) on a dentry which is being moved between
+ * directories, and to block lookup and possible open() for a dentry
+ * that is being removed without silly-rename.
+ * When set to block open()s, we also invalidate ->d_seq.
+ * When set only to protect ->d_fsdata, we don't need to invalidate ->d_seq.
+ */
+#define DCACHE_BLOCKED DCACHE_PRIVATE
+
const struct file_operations afs_dir_file_operations = {
.open = afs_dir_open,
.release = afs_release,
@@ -1031,6 +1041,12 @@ static int afs_d_revalidate_rcu(struct afs_vnode *dvnode, struct dentry *dentry)
if (!afs_check_validity(dvnode))
return -ECHILD;
+ /* Is a rename/unlink is pending? We must load d_flags *after*
+ * ->d_seq was sampled in path_init() or similar so use "acquire".
+ */
+ if (smp_load_acquire(&dentry->d_flags) & DCACHE_BLOCKED)
+ return -ECHILD;
+
/* We only need to invalidate a dentry if the server's copy changed
* behind our back. If we made the change, it's no problem. Note that
* on a 32-bit system, we only have 32 bits in the dentry to store the
@@ -1079,6 +1095,10 @@ static int afs_d_revalidate(struct inode *parent_dir, const struct qstr *name,
if (IS_ERR(key))
key = NULL;
+ /* Wait for rename/unlink to complete */
+wait_for_rename:
+ wait_var_event(&dentry->d_flags, !(dentry->d_flags & DCACHE_BLOCKED));
+
/* validate the parent directory */
ret = afs_validate(dir, key);
if (ret == -ERESTARTSYS) {
@@ -1158,7 +1178,13 @@ static int afs_d_revalidate(struct inode *parent_dir, const struct qstr *name,
}
out_valid:
+ spin_lock(&dentry->d_lock);
+ if (dentry->d_flags & DCACHE_BLOCKED) {
+ spin_unlock(&dentry->d_lock);
+ goto wait_for_rename;
+ }
dentry->d_fsdata = (void *)(unsigned long)dir_version;
+ spin_unlock(&dentry->d_lock);
out_valid_noupdate:
key_put(key);
_leave(" = 1 [valid]");
@@ -1533,8 +1559,10 @@ static void afs_unlink_edit_dir(struct afs_operation *op)
static void afs_unlink_put(struct afs_operation *op)
{
_enter("op=%08x", op->debug_id);
- if (op->unlink.need_rehash && afs_op_error(op) < 0 && afs_op_error(op) != -ENOENT)
- d_rehash(op->dentry);
+ spin_lock(&op->dentry->d_lock);
+ store_release_wake_up(&op->dentry->d_flags,
+ op->dentry->d_flags &~ DCACHE_BLOCKED);
+ spin_unlock(&op->dentry->d_lock);
}
static const struct afs_operation_ops afs_unlink_operation = {
@@ -1588,11 +1616,13 @@ static int afs_unlink(struct inode *dir, struct dentry *dentry)
afs_op_set_error(op, afs_sillyrename(dvnode, vnode, dentry, op->key));
goto error;
}
- if (!d_unhashed(dentry)) {
- /* Prevent a race with RCU lookup. */
- __d_drop(dentry);
- op->unlink.need_rehash = true;
- }
+ dentry->d_flags |= DCACHE_BLOCKED;
+ /*
+ * As RCU-walk calls d_revalidate() before incrementing d_count
+ * it may have already run. We need to invalidate d_seq so
+ * legitimize_path() will trigger a retry.
+ */
+ write_seqcount_invalidate(&dentry->d_seq);
spin_unlock(&dentry->d_lock);
op->file[1].vnode = vnode;
@@ -1899,11 +1929,6 @@ static void afs_rename_edit_dir(struct afs_operation *op)
_enter("op=%08x", op->debug_id);
- if (op->rename.rehash) {
- d_rehash(op->rename.rehash);
- op->rename.rehash = NULL;
- }
-
fscache_begin_write_operation(&orig_cres, afs_vnode_cache(orig_dvnode));
if (new_dvnode != orig_dvnode)
fscache_begin_write_operation(&new_cres, afs_vnode_cache(new_dvnode));
@@ -2023,11 +2048,18 @@ static void afs_rename_exchange_edit_dir(struct afs_operation *op)
static void afs_rename_put(struct afs_operation *op)
{
_enter("op=%08x", op->debug_id);
- if (op->rename.rehash)
- d_rehash(op->rename.rehash);
+ if (op->rename.unblock) {
+ spin_lock(&op->rename.unblock->d_lock);
+ store_release_wake_up(&op->rename.unblock->d_flags,
+ op->rename.unblock->d_flags &~ DCACHE_BLOCKED);
+ spin_unlock(&op->rename.unblock->d_lock);
+ op->rename.unblock = NULL;
+ }
+ spin_lock(&op->dentry->d_lock);
+ store_release_wake_up(&op->dentry->d_flags,
+ op->dentry->d_flags &~ DCACHE_BLOCKED);
+ spin_unlock(&op->dentry->d_lock);
dput(op->rename.tmp);
- if (afs_op_error(op))
- d_rehash(op->dentry);
}
static const struct afs_operation_ops afs_rename_operation = {
@@ -2135,7 +2167,11 @@ static int afs_rename(const struct mnt_idmap *idmap, struct inode *old_dir,
op->ops = &afs_rename_noreplace_operation;
} else if (flags & RENAME_EXCHANGE) {
op->ops = &afs_rename_exchange_operation;
- d_drop(new_dentry);
+ /* Block revalidate on new_dentry until rename completes */
+ spin_lock(&new_dentry->d_lock);
+ new_dentry->d_flags |= DCACHE_BLOCKED;
+ op->rename.unblock = new_dentry;
+ spin_unlock(&new_dentry->d_lock);
} else {
/* If we might displace the target, we might need to do silly
* rename.
@@ -2148,15 +2184,10 @@ static int afs_rename(const struct mnt_idmap *idmap, struct inode *old_dir,
* and becomes the new target.
*/
if (d_is_positive(new_dentry) && !d_is_dir(new_dentry)) {
- /* To prevent any new references to the target during
- * the rename, we unhash the dentry in advance.
- */
- if (!d_unhashed(new_dentry)) {
- d_drop(new_dentry);
- op->rename.rehash = new_dentry;
- }
+ spin_lock(&new_dentry->d_lock);
if (d_count(new_dentry) > 2) {
+ spin_unlock(&new_dentry->d_lock);
/* copy the target dentry's name */
op->rename.tmp = d_alloc(new_dentry->d_parent,
&new_dentry->d_name);
@@ -2174,8 +2205,21 @@ static int afs_rename(const struct mnt_idmap *idmap, struct inode *old_dir,
}
op->dentry_2 = op->rename.tmp;
- op->rename.rehash = NULL;
op->rename.new_negative = true;
+ } else {
+ /*
+ * To prevent any new references to the
+ * target during the rename, we set
+ * DCACHE_BLOCKED which afs_d_revalidate
+ * will wait for. d_lock ensures
+ * d_count() and DCACHE_BLOCKED are
+ * consistent, and d_seq ensure RCU-walk
+ * is retried.
+ */
+ new_dentry->d_flags |= DCACHE_BLOCKED;
+ write_seqcount_invalidate(&new_dentry->d_seq);
+ op->rename.unblock = new_dentry;
+ spin_unlock(&new_dentry->d_lock);
}
}
}
@@ -2186,10 +2230,11 @@ static int afs_rename(const struct mnt_idmap *idmap, struct inode *old_dir,
* d_revalidate may see old_dentry between the op having taken place
* and the version being updated.
*
- * So drop the old_dentry for now to make other threads go through
- * lookup instead - which we hold a lock against.
+ * So block revalidate on the old_dentry until the rename completes.
*/
- d_drop(old_dentry);
+ spin_lock(&old_dentry->d_lock);
+ old_dentry->d_flags |= DCACHE_BLOCKED;
+ spin_unlock(&old_dentry->d_lock);
ret = afs_do_sync_operation(op);
if (ret == -ENOTSUPP)
diff --git a/fs/afs/internal.h b/fs/afs/internal.h
index 7cea5ba0d20d..578fe7d35ab4 100644
--- a/fs/afs/internal.h
+++ b/fs/afs/internal.h
@@ -901,10 +901,7 @@ struct afs_operation {
struct afs_symlink *symlink;
} create;
struct {
- bool need_rehash;
- } unlink;
- struct {
- struct dentry *rehash;
+ struct dentry *unblock;
struct dentry *tmp;
unsigned int rename_flags;
bool new_negative;
--
2.50.0.107.gf914562f5916.dirty
^ permalink raw reply [flat|nested] 12+ messages in thread
* [PATCH v3 04/11] afs: use d_splice_alias() in afs_vnode_new_inode()
2026-11-25 3:16 [PATCH v3 00/11] multiple-filesystems: prepare for changes to VFS locking NeilBrown
` (2 preceding siblings ...)
2026-11-25 3:16 ` [PATCH v3 03/11] afs: don't unhash/rehash dentries during unlink/rename NeilBrown
@ 2026-11-25 3:16 ` NeilBrown
2026-11-25 3:16 ` [PATCH v3 05/11] afs: use d_alloc_trylock in afs_sillyrename() NeilBrown
` (6 subsequent siblings)
10 siblings, 0 replies; 12+ messages in thread
From: NeilBrown @ 2026-11-25 3:16 UTC (permalink / raw)
To: Alexander Viro, Christian Brauner
Cc: David Howells, Marc Dionne, Jan Harkes, coda, Jan Kara,
Richard Weinberger, Anton Ivanov, Johannes Berg, Jeff Layton,
Amir Goldstein, linux-kernel, codalist, linux-fsdevel, linux-um,
linux-afs
From: NeilBrown <neil@brown.name>
As afs supports the fhandle interfaces there is a theoretical possibility
that the inode created for mkdir could be found by open_by_handle_at()
and given a dentry before d_instantiate() is called. This would result
in two dentries for the one directory inode, which is not permitted.
So this patch changes afs_mkdir() to use d_splice_alias() and to
return the alternate dentry from ->mkdir() if appropriate.
Signed-off-by: NeilBrown <neil@brown.name>
---
fs/afs/dir.c | 13 ++++++++-----
fs/afs/internal.h | 1 +
2 files changed, 9 insertions(+), 5 deletions(-)
diff --git a/fs/afs/dir.c b/fs/afs/dir.c
index fbe2a3eb56b0..32b05aa8e208 100644
--- a/fs/afs/dir.c
+++ b/fs/afs/dir.c
@@ -1257,7 +1257,7 @@ void afs_check_for_remote_deletion(struct afs_operation *op)
/*
* Create a new inode for create/mkdir/symlink
*/
-static void afs_vnode_new_inode(struct afs_operation *op)
+static struct dentry *afs_vnode_new_inode(struct afs_operation *op)
{
struct afs_vnode_param *dvp = &op->file[0];
struct afs_vnode_param *vp = &op->file[1];
@@ -1274,7 +1274,7 @@ static void afs_vnode_new_inode(struct afs_operation *op)
* the new directory on the server.
*/
afs_op_accumulate_error(op, PTR_ERR(inode), 0);
- return;
+ return NULL;
}
vnode = AFS_FS_I(inode);
@@ -1285,7 +1285,7 @@ static void afs_vnode_new_inode(struct afs_operation *op)
afs_init_new_symlink(vnode, op);
if (!afs_op_error(op))
afs_cache_permit(vnode, op->key, vnode->cb_break, &vp->scb);
- d_instantiate(op->dentry, inode);
+ return d_splice_alias(inode, op->dentry);
}
static void afs_create_success(struct afs_operation *op)
@@ -1294,7 +1294,7 @@ static void afs_create_success(struct afs_operation *op)
op->ctime = op->file[0].scb.status.mtime_client;
afs_vnode_commit_status(op, &op->file[0]);
afs_update_dentry_version(op, &op->file[0], op->dentry);
- afs_vnode_new_inode(op);
+ op->create.ret = afs_vnode_new_inode(op);
}
static void afs_create_edit_dir(struct afs_operation *op)
@@ -1365,7 +1365,10 @@ static struct dentry *afs_mkdir(const struct mnt_idmap *idmap, struct inode *dir
op->ops = &afs_mkdir_operation;
ret = afs_do_sync_operation(op);
afs_dir_unuse_cookie(dvnode, ret);
- return ERR_PTR(ret);
+ if (ret)
+ return ERR_PTR(ret);
+ else
+ return op->create.ret;
}
/*
diff --git a/fs/afs/internal.h b/fs/afs/internal.h
index 578fe7d35ab4..1542b53e2cd8 100644
--- a/fs/afs/internal.h
+++ b/fs/afs/internal.h
@@ -899,6 +899,7 @@ struct afs_operation {
int reason; /* enum afs_edit_dir_reason */
mode_t mode;
struct afs_symlink *symlink;
+ struct dentry *ret;
} create;
struct {
struct dentry *unblock;
--
2.50.0.107.gf914562f5916.dirty
^ permalink raw reply [flat|nested] 12+ messages in thread
* [PATCH v3 05/11] afs: use d_alloc_trylock in afs_sillyrename()
2026-11-25 3:16 [PATCH v3 00/11] multiple-filesystems: prepare for changes to VFS locking NeilBrown
` (3 preceding siblings ...)
2026-11-25 3:16 ` [PATCH v3 04/11] afs: use d_splice_alias() in afs_vnode_new_inode() NeilBrown
@ 2026-11-25 3:16 ` NeilBrown
2026-11-25 3:16 ` [PATCH v3 06/11] afs: use d_duplicate() NeilBrown
` (5 subsequent siblings)
10 siblings, 0 replies; 12+ messages in thread
From: NeilBrown @ 2026-11-25 3:16 UTC (permalink / raw)
To: Alexander Viro, Christian Brauner
Cc: David Howells, Marc Dionne, Jan Harkes, coda, Jan Kara,
Richard Weinberger, Anton Ivanov, Johannes Berg, Jeff Layton,
Amir Goldstein, linux-kernel, codalist, linux-fsdevel, linux-um,
linux-afs
From: NeilBrown <neil@brown.name>
Rather than performing a normal lookup (which will be awkward with future
locking changes) use d_alloc_trylock() to find a dentry for an
unused name, and use an open-coded lookup_slow() to see if it is free on
the server.
Signed-off-by: NeilBrown <neil@brown.name>
---
fs/afs/dir.c | 6 ++---
fs/afs/dir_silly.c | 56 ++++++++++++++++++++++++++++++++--------------
fs/afs/internal.h | 2 ++
3 files changed, 43 insertions(+), 21 deletions(-)
diff --git a/fs/afs/dir.c b/fs/afs/dir.c
index 32b05aa8e208..672bd83ff112 100644
--- a/fs/afs/dir.c
+++ b/fs/afs/dir.c
@@ -19,8 +19,6 @@
#include "afs_fs.h"
#include "xdr_fs.h"
-static struct dentry *afs_lookup(struct inode *dir, struct dentry *dentry,
- unsigned int flags);
static int afs_dir_open(struct inode *inode, struct file *file);
static int afs_readdir(struct file *file, struct dir_context *ctx);
static int afs_d_revalidate(struct inode *dir, const struct qstr *name,
@@ -969,8 +967,8 @@ static struct dentry *afs_lookup_atsys(struct inode *dir, struct dentry *dentry)
/*
* look up an entry in a directory
*/
-static struct dentry *afs_lookup(struct inode *dir, struct dentry *dentry,
- unsigned int flags)
+struct dentry *afs_lookup(struct inode *dir, struct dentry *dentry,
+ unsigned int flags)
{
struct afs_vnode *dvnode = AFS_FS_I(dir);
struct afs_fid fid = {};
diff --git a/fs/afs/dir_silly.c b/fs/afs/dir_silly.c
index 982bb6ec15f0..e07c882c4fc7 100644
--- a/fs/afs/dir_silly.c
+++ b/fs/afs/dir_silly.c
@@ -112,7 +112,8 @@ int afs_sillyrename(struct afs_vnode *dvnode, struct afs_vnode *vnode,
struct dentry *dentry, struct key *key)
{
static unsigned int sillycounter;
- struct dentry *sdentry = NULL;
+ struct dentry *sdentry = NULL, *old;
+ struct inode *dir = dentry->d_parent->d_inode;
unsigned char silly[16];
int ret = -EBUSY;
@@ -122,23 +123,44 @@ int afs_sillyrename(struct afs_vnode *dvnode, struct afs_vnode *vnode,
if (dentry->d_flags & DCACHE_NFSFS_RENAMED)
return -EBUSY;
- sdentry = NULL;
- do {
- dput(sdentry);
- sillycounter++;
-
- /* Create a silly name. Note that the ".__afs" prefix is
- * understood by the salvager and must not be changed.
- */
- scnprintf(silly, sizeof(silly), ".__afs%04X", sillycounter);
- sdentry = lookup_noperm(&QSTR(silly), dentry->d_parent);
+newname:
+ sillycounter++;
- /* N.B. Better to return EBUSY here ... it could be dangerous
- * to delete the file while it's in use.
- */
- if (IS_ERR(sdentry))
- goto out;
- } while (!d_is_negative(sdentry));
+ /*
+ * Create a silly name. Note that the ".__afs" prefix is
+ * understood by the salvager and must not be changed.
+ */
+ scnprintf(silly, sizeof(silly), ".__afs%04X", sillycounter);
+ sdentry = d_alloc_trylock(dentry->d_parent, &QSTR(silly));
+ if (sdentry == ERR_PTR(-EWOULDBLOCK))
+ /* try another name */
+ goto newname;
+ /* N.B. Better to return EBUSY here ... it could be dangerous
+ * to delete the file while it's in use.
+ */
+ if (IS_ERR(sdentry))
+ goto out;
+ if (!d_in_lookup(sdentry)) {
+ if (d_really_is_negative(sdentry)) {
+ /* try to get an in-lookup dentry */
+ d_drop(sdentry);
+ sillycounter--;
+ }
+ dput(sdentry);
+ goto newname;
+ }
+ /* This name isn't known locally - check on server */
+ old = afs_lookup(dir, sdentry, 0);
+ d_lookup_done(sdentry);
+ if (IS_ERR(old)) {
+ dput(sdentry);
+ goto out;
+ }
+ if (old || d_is_positive(sdentry)) {
+ dput(old);
+ dput(sdentry);
+ goto newname;
+ }
ihold(&vnode->netfs.inode);
diff --git a/fs/afs/internal.h b/fs/afs/internal.h
index 1542b53e2cd8..60687671f18b 100644
--- a/fs/afs/internal.h
+++ b/fs/afs/internal.h
@@ -1111,6 +1111,8 @@ ssize_t afs_read_dir(struct afs_vnode *dvnode, struct file *file)
__acquires(&dvnode->validate_lock);
extern void afs_d_release(struct dentry *);
extern void afs_check_for_remote_deletion(struct afs_operation *);
+extern struct dentry *afs_lookup(struct inode *dir, struct dentry *dentry,
+ unsigned int flags);
/*
* dir_edit.c
--
2.50.0.107.gf914562f5916.dirty
^ permalink raw reply [flat|nested] 12+ messages in thread
* [PATCH v3 06/11] afs: use d_duplicate()
2026-11-25 3:16 [PATCH v3 00/11] multiple-filesystems: prepare for changes to VFS locking NeilBrown
` (4 preceding siblings ...)
2026-11-25 3:16 ` [PATCH v3 05/11] afs: use d_alloc_trylock in afs_sillyrename() NeilBrown
@ 2026-11-25 3:16 ` NeilBrown
2026-11-25 3:16 ` [PATCH v3 07/11] Revert "VFS: Add LOOKUP_SHARED flag." NeilBrown
` (4 subsequent siblings)
10 siblings, 0 replies; 12+ messages in thread
From: NeilBrown @ 2026-11-25 3:16 UTC (permalink / raw)
To: Alexander Viro, Christian Brauner
Cc: David Howells, Marc Dionne, Jan Harkes, coda, Jan Kara,
Richard Weinberger, Anton Ivanov, Johannes Berg, Jeff Layton,
Amir Goldstein, linux-kernel, codalist, linux-fsdevel, linux-um,
linux-afs
From: NeilBrown <neil@brown.name>
To prepare for d_alloc_parallel() being permitted without a directory
lock, use d_duplicate() when duplicating a dentry in order to perform a
silly-rename. This creates on in-lookup dentry which will contine to block
concurrent lookups after the original dentry is silly-renamed, and until
the final rename happens.
As the resulting dentry is in-lookup, we need to call d_lookup_done()
before d_put().
Signed-off-by: NeilBrown <neil@brown.name>
---
fs/afs/dir.c | 8 +++++---
1 file changed, 5 insertions(+), 3 deletions(-)
diff --git a/fs/afs/dir.c b/fs/afs/dir.c
index 672bd83ff112..bc90b30d8b57 100644
--- a/fs/afs/dir.c
+++ b/fs/afs/dir.c
@@ -2060,7 +2060,10 @@ static void afs_rename_put(struct afs_operation *op)
store_release_wake_up(&op->dentry->d_flags,
op->dentry->d_flags &~ DCACHE_BLOCKED);
spin_unlock(&op->dentry->d_lock);
- dput(op->rename.tmp);
+ if (op->rename.tmp) {
+ d_lookup_done(op->rename.tmp);
+ dput(op->rename.tmp);
+ }
}
static const struct afs_operation_ops afs_rename_operation = {
@@ -2190,8 +2193,7 @@ static int afs_rename(const struct mnt_idmap *idmap, struct inode *old_dir,
if (d_count(new_dentry) > 2) {
spin_unlock(&new_dentry->d_lock);
/* copy the target dentry's name */
- op->rename.tmp = d_alloc(new_dentry->d_parent,
- &new_dentry->d_name);
+ op->rename.tmp = d_duplicate(new_dentry);
if (!op->rename.tmp) {
afs_op_nomem(op);
goto error;
--
2.50.0.107.gf914562f5916.dirty
^ permalink raw reply [flat|nested] 12+ messages in thread
* [PATCH v3 07/11] Revert "VFS: Add LOOKUP_SHARED flag."
2026-11-25 3:16 [PATCH v3 00/11] multiple-filesystems: prepare for changes to VFS locking NeilBrown
` (5 preceding siblings ...)
2026-11-25 3:16 ` [PATCH v3 06/11] afs: use d_duplicate() NeilBrown
@ 2026-11-25 3:16 ` NeilBrown
2026-11-25 3:16 ` [PATCH v3 08/11] VFS: use d_alloc_parallel() in lookup_one_qstr_excl() NeilBrown
` (3 subsequent siblings)
10 siblings, 0 replies; 12+ messages in thread
From: NeilBrown @ 2026-11-25 3:16 UTC (permalink / raw)
To: Alexander Viro, Christian Brauner
Cc: David Howells, Marc Dionne, Jan Harkes, coda, Jan Kara,
Richard Weinberger, Anton Ivanov, Johannes Berg, Jeff Layton,
Amir Goldstein, linux-kernel, codalist, linux-fsdevel, linux-um,
linux-afs
From: NeilBrown <neil@brown.name>
This reverts commit cc47a1ba1983116ee7a720a79197eb5299ae5d31.
It turns out that we need to make all ->lookup calls happen under shared
lock *before* the places where I planned to use LOOKUP_SHARED.
So LOOKUP_SHARED won't be needed. Let's discard it.
Signed-off-by: NeilBrown <neil@brown.name>
---
fs/namei.c | 20 +++++++++-----------
include/linux/namei.h | 3 +--
2 files changed, 10 insertions(+), 13 deletions(-)
diff --git a/fs/namei.c b/fs/namei.c
index 925aeb63bfa1..6d63a4c08495 100644
--- a/fs/namei.c
+++ b/fs/namei.c
@@ -1939,7 +1939,7 @@ static noinline struct dentry *lookup_slow(const struct qstr *name,
struct inode *inode = dir->d_inode;
struct dentry *res;
inode_lock_shared(inode);
- res = __lookup_slow(name, dir, flags | LOOKUP_SHARED);
+ res = __lookup_slow(name, dir, flags);
inode_unlock_shared(inode);
return res;
}
@@ -1953,7 +1953,7 @@ static struct dentry *lookup_slow_killable(const struct qstr *name,
if (inode_lock_shared_killable(inode))
return ERR_PTR(-EINTR);
- res = __lookup_slow(name, dir, flags | LOOKUP_SHARED);
+ res = __lookup_slow(name, dir, flags);
inode_unlock_shared(inode);
return res;
}
@@ -4446,14 +4446,12 @@ static struct dentry *lookup_open(struct nameidata *nd, struct file *file,
int error, create_error;
umode_t mode;
bool got_write;
- unsigned int shared_flag;
retry:
open_flag = op->open_flag;
got_write = false;
mode = op->mode;
create_error = 0;
- shared_flag = (open_flag & O_CREAT) ? 0 : LOOKUP_SHARED;
if (open_flag & (O_CREAT | O_TRUNC | O_WRONLY | O_RDWR)) {
got_write = !mnt_want_write(nd->path.mnt);
@@ -4462,10 +4460,10 @@ static struct dentry *lookup_open(struct nameidata *nd, struct file *file,
* a different error; we'll be dropping this one anyway.
*/
}
- if (shared_flag)
- inode_lock_shared(dir_inode);
- else
+ if (open_flag & O_CREAT)
inode_lock(dir_inode);
+ else
+ inode_lock_shared(dir_inode);
if (unlikely(IS_DEADDIR(dir_inode))) {
dentry = ERR_PTR(-ENOENT);
@@ -4534,7 +4532,7 @@ static struct dentry *lookup_open(struct nameidata *nd, struct file *file,
if (d_in_lookup(dentry)) {
struct dentry *res = dir_inode->i_op->lookup(dir_inode, dentry,
- nd->flags | shared_flag);
+ nd->flags);
d_lookup_done(dentry);
if (unlikely(res)) {
if (IS_ERR(res)) {
@@ -4582,10 +4580,10 @@ static struct dentry *lookup_open(struct nameidata *nd, struct file *file,
if (file->f_mode & FMODE_OPENED)
fsnotify_open(file);
}
- if (shared_flag)
- inode_unlock_shared(dir_inode);
- else
+ if ((open_flag & O_CREAT) || create_error)
inode_unlock(dir_inode);
+ else
+ inode_unlock_shared(dir_inode);
if (got_write)
mnt_drop_write(nd->path.mnt);
diff --git a/include/linux/namei.h b/include/linux/namei.h
index c4436e5c2ba6..da5b4ccca1b5 100644
--- a/include/linux/namei.h
+++ b/include/linux/namei.h
@@ -32,9 +32,8 @@ enum { MAX_NESTED_LINKS = 8 };
#define LOOKUP_CREATE BIT(17) /* ... in object creation */
#define LOOKUP_EXCL BIT(18) /* ... in target must not exist */
#define LOOKUP_RENAME_TARGET BIT(19) /* ... in destination of rename() */
-#define LOOKUP_SHARED BIT(20) /* Parent lock is held shared */
-/* 3 spare bits for intent */
+/* 4 spare bits for intent */
/* Scoping flags for lookup. */
#define LOOKUP_NO_SYMLINKS BIT(24) /* No symlink crossing. */
--
2.50.0.107.gf914562f5916.dirty
^ permalink raw reply [flat|nested] 12+ messages in thread
* [PATCH v3 08/11] VFS: use d_alloc_parallel() in lookup_one_qstr_excl().
2026-11-25 3:16 [PATCH v3 00/11] multiple-filesystems: prepare for changes to VFS locking NeilBrown
` (6 preceding siblings ...)
2026-11-25 3:16 ` [PATCH v3 07/11] Revert "VFS: Add LOOKUP_SHARED flag." NeilBrown
@ 2026-11-25 3:16 ` NeilBrown
2026-11-25 3:16 ` [PATCH v3 09/11] VFS: drop parent lock across d_alloc_parallel() in d_add_ci() NeilBrown
` (2 subsequent siblings)
10 siblings, 0 replies; 12+ messages in thread
From: NeilBrown @ 2026-11-25 3:16 UTC (permalink / raw)
To: Alexander Viro, Christian Brauner
Cc: David Howells, Marc Dionne, Jan Harkes, coda, Jan Kara,
Richard Weinberger, Anton Ivanov, Johannes Berg, Jeff Layton,
Amir Goldstein, linux-kernel, codalist, linux-fsdevel, linux-um,
linux-afs
From: NeilBrown <neil@brown.name>
lookup_one_qstr_excl() is used for lookups prior to directory
modifications (other than open(O_CREATE)), whether create, remove,
or rename.
A future patch will lift lookup out of the i_rwsem lock that protects
the directory during these operations (only taking a shared lock if the
target name is not yet in the dcache).
To prepare for this change and particularly to allow lookup to
always be done outside the parent i_rwsem, change lookup_one_qstr_excl()
to use d_alloc_parallel().
For the target of create and rename some filesystems skip the
preliminary lookup and combine it with the main operation. This is only
safe if the operation has exclusive access to the dentry. Currently
this is guaranteed by an exclusive lock on the directory.
d_alloc_parallel() provides alternate exclusive access (in the case
where the name isn't in the dcache and ->lookup will be called).
As a result of this change, ->lookup is now only ever called with a
d_in_lookup() dentry. Consequently we can remove the d_in_lookup()
check from d_add_ci() which is only used in ->lookup.
The caller must now ensure d_lookup_done() is called at an appropriate
time, and must not assume that it can test for positive or negative
dentries without confirming that the dentry is no longer d_in_lookup() -
unless it is filesystem code acting on itself and *knows* that
->lookup() always completes the lookup (currently true for all
filesystems other than NFS). This is all handled in start_creating()
and end_dirop() and friends.
At present only LOOKUP_EXCL and LOOKUP_RENAME_TARGET might result in
an in-lookup dentry after a successful ->lookup, but that might change.
Note that as lookup_one_qstr_excl() is called with an exclusive lock on
the directory, d_alloc_parallel() cannot race with another thread and
cannot return a non-in-lookup dentry. However that is expected to
change so that case is handled with this patch.
As mentioned, with this change it is safe for d_alloc_parallel() to be
called without holding ->i_rwsem on the parent. This holds providing
that any filesystem which uses d_alloc() or d_alloc_name() and then
instantiates the dentry, only does so in a context where ->lookup cannot
instantiate a (positive) dentry with the same name without relying
on ->i_rwsem.
This is already the case. e.g.
- some filesystem use simple_lookup which only instantiates negative
dentry. A race with d_alloc() will mean the negative entry added with
simple_lookup() will be later in the hash chain and so never found.
- some filesystems only use d_alloc() during fs creation, before
->lookup is possible.
Signed-off-by: NeilBrown <neil@brown.name>
---
Documentation/filesystems/porting.rst | 14 ++++++++++
fs/dcache.c | 16 +++---------
fs/namei.c | 37 +++++++++++++++++++--------
3 files changed, 45 insertions(+), 22 deletions(-)
diff --git a/Documentation/filesystems/porting.rst b/Documentation/filesystems/porting.rst
index e41466904501..78df8842bddb 100644
--- a/Documentation/filesystems/porting.rst
+++ b/Documentation/filesystems/porting.rst
@@ -1422,3 +1422,17 @@ helpers take a ``const struct mnt_idmap *``. mnt_idmap() and file_mnt_idmap()
return one. The idmapping is immutable so nothing should have modified it
anyway. References are taken and dropped via mnt_idmap_get() and
mnt_idmap_put() as before, both accept a const pointer.
+
+---
+
+**mandatory**
+
+All start_creating and start_renaming functions may return a
+d_in_lookup() dentry. end_dirop() calls the necessary d_lookup_done().
+If the caller *knows* which filesystem is being used, it may know that
+this is not possible. Otherwise it must be careful testing if the
+dentry is positive or negative as the lookup may not have been performed
+yet.
+
+inode_operations.lookup() is now only ever called with a d_in_lookup()
+dentry.
diff --git a/fs/dcache.c b/fs/dcache.c
index ba17f49f8a63..2ad39c1515b7 100644
--- a/fs/dcache.c
+++ b/fs/dcache.c
@@ -2472,18 +2472,10 @@ struct dentry *d_add_ci(struct dentry *dentry, struct inode *inode,
iput(inode);
return found;
}
- if (d_in_lookup(dentry)) {
- found = d_alloc_parallel(dentry->d_parent, name);
- if (IS_ERR(found) || !d_in_lookup(found)) {
- iput(inode);
- return found;
- }
- } else {
- found = d_alloc(dentry->d_parent, name);
- if (!found) {
- iput(inode);
- return ERR_PTR(-ENOMEM);
- }
+ found = d_alloc_parallel(dentry->d_parent, name);
+ if (IS_ERR(found) || !d_in_lookup(found)) {
+ iput(inode);
+ return found;
}
res = d_splice_alias(inode, found);
if (res) {
diff --git a/fs/namei.c b/fs/namei.c
index 6d63a4c08495..cfd37044a516 100644
--- a/fs/namei.c
+++ b/fs/namei.c
@@ -1785,13 +1785,13 @@ static struct dentry *lookup_dcache(const struct qstr *name,
}
/*
- * Parent directory has inode locked exclusive. This is one
- * and only case when ->lookup() gets called on non in-lookup
- * dentries - as the matter of fact, this only gets called
- * when directory is guaranteed to have no in-lookup children
- * at all.
- * Will return -ENOENT if name isn't found and LOOKUP_CREATE wasn't passed.
- * Will return -EEXIST if name is found and LOOKUP_EXCL was passed.
+ * Parent directory has inode locked.
+ * d_lookup_done() must be called before the dentry is dput().
+ * If the dentry is not d_in_lookup():
+ * Will return -ENOENT if name isn't found and LOOKUP_CREATE wasn't passed.
+ * Will return -EEXIST if name is found and LOOKUP_EXCL was passed.
+ * If it is d_in_lookup() then these conditions can only be checked by the
+ * file system when carrying out the intent (create or rename).
*/
static struct dentry *lookup_one_qstr_excl(const struct qstr *name,
struct dentry *base, unsigned int flags)
@@ -1809,14 +1809,24 @@ static struct dentry *lookup_one_qstr_excl(const struct qstr *name,
if (unlikely(IS_DEADDIR(dir)))
return ERR_PTR(-ENOENT);
- dentry = d_alloc(base, name);
- if (unlikely(!dentry))
- return ERR_PTR(-ENOMEM);
+ dentry = d_alloc_parallel(base, name);
+ if (unlikely(IS_ERR(dentry)))
+ return dentry;
+ if (unlikely(!d_in_lookup(dentry)))
+ /* Raced with another thread which did the lookup */
+ goto found;
old = dir->i_op->lookup(dir, dentry, flags);
if (unlikely(old)) {
+ d_lookup_done(dentry);
dput(dentry);
dentry = old;
+ } else if (d_in_lookup(dentry)) {
+ /* We cannot check for errors resulting from LOOKUP_ flags.
+ * The caller will have to wait for any create-etc attempt
+ * to get relevant errors.
+ */
+ return dentry;
}
found:
if (IS_ERR(dentry))
@@ -2938,6 +2948,7 @@ static struct dentry *__start_dirop(struct dentry *parent, struct qstr *name,
* The lookup is performed and necessary locks are taken so that, on success,
* the returned dentry can be operated on safely.
* The qstr must already have the hash value calculated.
+ * The dentry may be d_in_lookup().
*
* Returns: a locked dentry, or an error.
*
@@ -2959,6 +2970,7 @@ void end_dirop(struct dentry *de)
{
if (!IS_ERR(de)) {
inode_unlock(de->d_parent->d_inode);
+ d_lookup_done(de);
dput(de);
}
}
@@ -3918,8 +3930,10 @@ __start_renaming(struct renamedata *rd, int lookup_flags,
return 0;
out_dput_d2:
+ d_lookup_done(d2);
dput(d2);
out_dput_d1:
+ d_lookup_done(d1);
dput(d1);
out_unlock:
unlock_rename(rd->old_parent, rd->new_parent);
@@ -4014,6 +4028,7 @@ __start_renaming_dentry(struct renamedata *rd, int lookup_flags,
return 0;
out_dput_d2:
+ d_lookup_done(d2);
dput(d2);
out_unlock:
unlock_rename(old_dentry->d_parent, rd->new_parent);
@@ -4123,6 +4138,8 @@ EXPORT_SYMBOL(start_renaming_two_dentries);
void end_renaming(struct renamedata *rd)
{
+ d_lookup_done(rd->old_dentry);
+ d_lookup_done(rd->new_dentry);
unlock_rename(rd->old_parent, rd->new_parent);
dput(rd->old_dentry);
dput(rd->new_dentry);
--
2.50.0.107.gf914562f5916.dirty
^ permalink raw reply [flat|nested] 12+ messages in thread
* [PATCH v3 09/11] VFS: drop parent lock across d_alloc_parallel() in d_add_ci()
2026-11-25 3:16 [PATCH v3 00/11] multiple-filesystems: prepare for changes to VFS locking NeilBrown
` (7 preceding siblings ...)
2026-11-25 3:16 ` [PATCH v3 08/11] VFS: use d_alloc_parallel() in lookup_one_qstr_excl() NeilBrown
@ 2026-11-25 3:16 ` NeilBrown
2026-11-25 3:16 ` [PATCH v3 10/11] procfs: drop parent lock for d_alloc_parallel() in iterate_shared() NeilBrown
2026-11-25 3:16 ` [PATCH v3 11/11] afs: lookup_atsys to drop and reclaim lock NeilBrown
10 siblings, 0 replies; 12+ messages in thread
From: NeilBrown @ 2026-11-25 3:16 UTC (permalink / raw)
To: Alexander Viro, Christian Brauner
Cc: David Howells, Marc Dionne, Jan Harkes, coda, Jan Kara,
Richard Weinberger, Anton Ivanov, Johannes Berg, Jeff Layton,
Amir Goldstein, linux-kernel, codalist, linux-fsdevel, linux-um,
linux-afs
From: NeilBrown <neil@brown.name>
A proposed change will invert the lock ordering between
d_alloc_parallel() and inode_lock() on the parent.
When that happens it will not be safe to call d_alloc_parallel() while
holding the parent lock - even shared.
It is already *safe* to call d_alloc_parallel() without holding the
parent lock - providing any filesystem that does so doesn't d_drop or
d_hash in the middle of an operation. Those filesystems which use
d_add_ci() don't d_drop().
We don't need to keep the parent lock held when d_add_ci() is run - the
VFS doesn't need it as dentry is exclusively held due to
DCACHE_PAR_LOOKUP and the filesystem has finished its work.
So drop and reclaim the lock to avoid future deadlock.
Signed-off-by: NeilBrown <neil@brown.name>
---
fs/dcache.c | 17 +++++++++++++++--
1 file changed, 15 insertions(+), 2 deletions(-)
diff --git a/fs/dcache.c b/fs/dcache.c
index 2ad39c1515b7..ed957f090247 100644
--- a/fs/dcache.c
+++ b/fs/dcache.c
@@ -2472,17 +2472,30 @@ struct dentry *d_add_ci(struct dentry *dentry, struct inode *inode,
iput(inode);
return found;
}
+ /*
+ * We are holding parent lock and mustn't wait
+ * for a d_in_lookup() dentry. We can safely drop the
+ * parent lock and reclaim it as we have exclusive
+ * access to dentry as it is d_in_lookup() (so
+ * ->d_parent is stable) and we are near the end
+ * ->lookup() and will shortly drop the lock anyway.
+ * We cannot retake the lock while the new dentry is in-lookup
+ */
+ inode_unlock_shared(d_inode(dentry->d_parent));
found = d_alloc_parallel(dentry->d_parent, name);
if (IS_ERR(found) || !d_in_lookup(found)) {
iput(inode);
- return found;
+ goto out_relock;
}
res = d_splice_alias(inode, found);
if (res) {
d_lookup_done(found);
dput(found);
- return res;
+ found = res;
}
+out_relock:
+ d_lookup_done(dentry);
+ inode_lock_shared(d_inode(dentry->d_parent));
return found;
}
EXPORT_SYMBOL(d_add_ci);
--
2.50.0.107.gf914562f5916.dirty
^ permalink raw reply [flat|nested] 12+ messages in thread
* [PATCH v3 10/11] procfs: drop parent lock for d_alloc_parallel() in iterate_shared()
2026-11-25 3:16 [PATCH v3 00/11] multiple-filesystems: prepare for changes to VFS locking NeilBrown
` (8 preceding siblings ...)
2026-11-25 3:16 ` [PATCH v3 09/11] VFS: drop parent lock across d_alloc_parallel() in d_add_ci() NeilBrown
@ 2026-11-25 3:16 ` NeilBrown
2026-11-25 3:16 ` [PATCH v3 11/11] afs: lookup_atsys to drop and reclaim lock NeilBrown
10 siblings, 0 replies; 12+ messages in thread
From: NeilBrown @ 2026-11-25 3:16 UTC (permalink / raw)
To: Alexander Viro, Christian Brauner
Cc: David Howells, Marc Dionne, Jan Harkes, coda, Jan Kara,
Richard Weinberger, Anton Ivanov, Johannes Berg, Jeff Layton,
Amir Goldstein, linux-kernel, codalist, linux-fsdevel, linux-um,
linux-afs
From: NeilBrown <neil@brown.name>
When procfs finds a name in iterate_shared() that isn't in the dcache it
*must* add it so that it can have a stable inode number to report
(inodes are only accessible from the dcache in procfs).
It uses d_alloc_parallel(). A planned change to locking will make it
unsafe to call d_alloc_parallel() while holding the directory lock.
Other filesystems which prime the dcache in iterate_shared() use
d_alloc_trylock() which is safe but can fail if it races with ->lookup.
As procfs cannot handle failure we need something better.
procfs doesn't *need* the parent to be locked. There are no shared data
structures accessed that don't have their own locking. So it is safe to
drop and re-take the parent lock. We could do this around the whole
iteration, but as failure of d_alloc_trylock() is rare it is more
efficient to drop and retake it just around a call of d_alloc_parallel()
when d_alloc_trylock does fail.
Other code that drops and retakes the lock in iterate_shared needs to
be careful to check S_DEAD which could be set while the lock is
dropped. This is not needed in procfs as the flag is never set.
As d_alloc_trylock() calls try_lookup_noperm(), we can skip that call
and simplify the code.
Signed-off-by: NeilBrown <neil@brown.name>
---
fs/proc/base.c | 41 +++++++++++++++++++------------
fs/proc/proc_sysctl.c | 57 ++++++++++++++++++++++++++-----------------
2 files changed, 59 insertions(+), 39 deletions(-)
diff --git a/fs/proc/base.c b/fs/proc/base.c
index 7685660c3624..cdf0a869f26b 100644
--- a/fs/proc/base.c
+++ b/fs/proc/base.c
@@ -2104,26 +2104,33 @@ bool proc_fill_cache(struct file *file, struct dir_context *ctx,
struct qstr qname = QSTR_INIT(name, len);
struct inode *inode;
unsigned type = DT_UNKNOWN;
+ bool must_relock = false;
ino_t ino = 1;
- child = try_lookup_noperm(&qname, dir);
- if (IS_ERR(child))
- goto end_instantiate;
+ child = d_alloc_trylock(dir, &qname);
- if (!child) {
+ if (child == ERR_PTR(-EWOULDBLOCK)) {
+ /*
+ * Need to drop directory lock, which isn't really
+ * needed here anyway. As rmdir never happens in procfs
+ * we don't need to be concerned about S_DEAD being set
+ * while unlocked.
+ */
+ inode_unlock_shared(dir->d_inode);
+ must_relock = true;
child = d_alloc_parallel(dir, &qname);
- if (IS_ERR(child))
- goto end_instantiate;
- if (d_in_lookup(child)) {
- struct dentry *res;
- res = instantiate(child, task, ptr);
- d_lookup_done(child);
- if (unlikely(res)) {
- dput(child);
- child = res;
- if (IS_ERR(child))
- goto end_instantiate;
- }
+ }
+ if (IS_ERR(child))
+ goto end_instantiate;
+ if (d_in_lookup(child)) {
+ struct dentry *res;
+ res = instantiate(child, task, ptr);
+ d_lookup_done(child);
+ if (unlikely(res)) {
+ dput(child);
+ child = res;
+ if (IS_ERR(child))
+ goto end_instantiate;
}
}
inode = d_inode(child);
@@ -2131,6 +2138,8 @@ bool proc_fill_cache(struct file *file, struct dir_context *ctx,
type = inode->i_mode >> 12;
dput(child);
end_instantiate:
+ if (must_relock)
+ inode_lock_shared(dir->d_inode);
return dir_emit(ctx, name, len, ino, type);
}
diff --git a/fs/proc/proc_sysctl.c b/fs/proc/proc_sysctl.c
index eeebf813668e..ea3287ab9d21 100644
--- a/fs/proc/proc_sysctl.c
+++ b/fs/proc/proc_sysctl.c
@@ -687,38 +687,49 @@ static bool proc_sys_fill_cache(struct file *file,
struct inode *inode;
struct qstr qname;
ino_t ino = 0;
+ bool must_relock = false;
+ bool ret = false;
unsigned type = DT_UNKNOWN;
- qname.name = table->procname;
- qname.len = strlen(table->procname);
- qname.hash = full_name_hash(dir, qname.name, qname.len);
-
- child = d_lookup(dir, &qname);
- if (!child) {
+ qname = QSTR(table->procname);
+ child = d_alloc_trylock(dir, &qname);
+ if (child == ERR_PTR(-EWOULDBLOCK)) {
+ /*
+ * Need to drop directory lock, which isn't really
+ * needed here anyway. As rmdir never happens in procfs
+ * we don't need to be concerned about S_DEAD being set
+ * while unlocked.
+ */
+ inode_unlock_shared(dir->d_inode);
+ must_relock = true;
child = d_alloc_parallel(dir, &qname);
- if (IS_ERR(child))
- return false;
- if (d_in_lookup(child)) {
- struct dentry *res;
- inode = proc_sys_make_inode(dir->d_sb, head, table);
- res = d_splice_alias_ops(inode, child,
- &proc_sys_dentry_operations);
- d_lookup_done(child);
- if (unlikely(res)) {
- dput(child);
-
- if (IS_ERR(res))
- return false;
-
- child = res;
- }
+ }
+ if (IS_ERR(child))
+ goto out;
+ if (d_in_lookup(child)) {
+ struct dentry *res;
+ inode = proc_sys_make_inode(dir->d_sb, head, table);
+ res = d_splice_alias_ops(inode, child,
+ &proc_sys_dentry_operations);
+ d_lookup_done(child);
+ if (unlikely(res)) {
+ dput(child);
+
+ if (IS_ERR(res))
+ goto out;
+
+ child = res;
}
}
inode = d_inode(child);
ino = inode->i_ino;
type = inode->i_mode >> 12;
dput(child);
- return dir_emit(ctx, qname.name, qname.len, ino, type);
+ ret = dir_emit(ctx, qname.name, qname.len, ino, type);
+out:
+ if (must_relock)
+ inode_lock_shared(dir->d_inode);
+ return ret;
}
static bool proc_sys_link_fill_cache(struct file *file,
--
2.50.0.107.gf914562f5916.dirty
^ permalink raw reply [flat|nested] 12+ messages in thread
* [PATCH v3 11/11] afs: lookup_atsys to drop and reclaim lock.
2026-11-25 3:16 [PATCH v3 00/11] multiple-filesystems: prepare for changes to VFS locking NeilBrown
` (9 preceding siblings ...)
2026-11-25 3:16 ` [PATCH v3 10/11] procfs: drop parent lock for d_alloc_parallel() in iterate_shared() NeilBrown
@ 2026-11-25 3:16 ` NeilBrown
10 siblings, 0 replies; 12+ messages in thread
From: NeilBrown @ 2026-11-25 3:16 UTC (permalink / raw)
To: Alexander Viro, Christian Brauner
Cc: David Howells, Marc Dionne, Jan Harkes, coda, Jan Kara,
Richard Weinberger, Anton Ivanov, Johannes Berg, Jeff Layton,
Amir Goldstein, linux-kernel, codalist, linux-fsdevel, linux-um,
linux-afs
From: NeilBrown <neil@brown.name>
If afs is asked to lookup a name ending with @sys, it needs to look up
a different name for which it might need to allocate a dentry with
d_alloc_parallel().
This is done while the parent lock is held which will be a problem in
a future patch where the ordering of the parent lock and
d_alloc_parallel() locking is reversed.
There is no actual need to hold the lock during this nested lookup, so
with this patch we drop the lock and reclaim it, and use
lookup_noperm_unlocked().
Signed-off-by: NeilBrown <neil@brown.name>
---
fs/afs/dir.c | 14 +++++++++++++-
1 file changed, 13 insertions(+), 1 deletion(-)
diff --git a/fs/afs/dir.c b/fs/afs/dir.c
index bc90b30d8b57..7ed5ff63c90e 100644
--- a/fs/afs/dir.c
+++ b/fs/afs/dir.c
@@ -938,6 +938,12 @@ static struct dentry *afs_lookup_atsys(struct inode *dir, struct dentry *dentry)
refcount_inc(&subs->usage);
read_unlock(&net->sysnames_lock);
+ /*
+ * We need the directory to be unlocked so we can perform other lookups.
+ * We don't really need the lock any more. The in-lookup status of
+ * dentry gives us sufficient exclusion.
+ */
+ inode_unlock_shared(dir);
for (i = 0; i < subs->nr; i++) {
name = subs->subs[i];
len = dentry->d_name.len - 4 + strlen(name);
@@ -947,7 +953,7 @@ static struct dentry *afs_lookup_atsys(struct inode *dir, struct dentry *dentry)
}
strcpy(p, name);
- ret = lookup_noperm(&QSTR(buf), dentry->d_parent);
+ ret = lookup_noperm_unlocked(&QSTR(buf), dentry->d_parent);
if (IS_ERR(ret) || d_is_positive(ret))
goto out_s;
dput(ret);
@@ -958,6 +964,12 @@ static struct dentry *afs_lookup_atsys(struct inode *dir, struct dentry *dentry)
*/
ret = NULL;
out_s:
+ /*
+ * Cannot take parent lock while we hold an in-lookup dentry so
+ * must make sure that lookup is done.
+ */
+ d_lookup_done(dentry);
+ inode_lock_shared(dir);
afs_put_sysnames(subs);
kfree(buf);
out_p:
--
2.50.0.107.gf914562f5916.dirty
^ permalink raw reply [flat|nested] 12+ messages in thread
end of thread, other threads:[~2026-11-25 3:47 UTC | newest]
Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-25 3:16 [PATCH v3 00/11] multiple-filesystems: prepare for changes to VFS locking NeilBrown
2026-11-25 3:16 ` [PATCH v3 01/11] coda: don't d_drop() early NeilBrown
2026-11-25 3:16 ` [PATCH v3 02/11] hostfs: don't d_drop() before d_splice_alias() in hostfs_mkdir() NeilBrown
2026-11-25 3:16 ` [PATCH v3 03/11] afs: don't unhash/rehash dentries during unlink/rename NeilBrown
2026-11-25 3:16 ` [PATCH v3 04/11] afs: use d_splice_alias() in afs_vnode_new_inode() NeilBrown
2026-11-25 3:16 ` [PATCH v3 05/11] afs: use d_alloc_trylock in afs_sillyrename() NeilBrown
2026-11-25 3:16 ` [PATCH v3 06/11] afs: use d_duplicate() NeilBrown
2026-11-25 3:16 ` [PATCH v3 07/11] Revert "VFS: Add LOOKUP_SHARED flag." NeilBrown
2026-11-25 3:16 ` [PATCH v3 08/11] VFS: use d_alloc_parallel() in lookup_one_qstr_excl() NeilBrown
2026-11-25 3:16 ` [PATCH v3 09/11] VFS: drop parent lock across d_alloc_parallel() in d_add_ci() NeilBrown
2026-11-25 3:16 ` [PATCH v3 10/11] procfs: drop parent lock for d_alloc_parallel() in iterate_shared() NeilBrown
2026-11-25 3:16 ` [PATCH v3 11/11] afs: lookup_atsys to drop and reclaim lock NeilBrown
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®