[PATCH v2 4/4] procfs: drop parent lock for d_alloc_parallel() in iterate_shared()

Christian Brauner brauner at kernel.org
Fri Nov 20 05:38:43 PST 2026


On Tue, Nov 17, 2026 at 10:55:01AM +1100, NeilBrown wrote:
> +	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);
>  		child = d_alloc_parallel(dir, &qname);
> [...]
> +		inode_lock_shared(dir->d_inode);
> +	}

I think this can deadlock.
If we're colliding with the failed lookup because the fd was closed, the
task exited or the sysctl unregistered then the  dentry is unhashed and
d_alloc_parallel() hands us a new in-lookup dentry.

We then retake the lock while still owning it. But the VFS waits for
in-lookup dentries with the lock held, e.g., open() with O_CREAT:

P1: getdents(/proc/$pid/fd)         P2: open("/proc/$pid/fd/5", O_CREAT)
proc_fill_cache("5")
-> d_alloc_trylock() == -EWOULDBLOCK
-> inode_unlock_shared(dir)
                                    lookup_open()
                                    -> inode_lock(dir)
                                    -> d_lookup("5") == NULL
-> child = d_alloc_parallel("5")
                                    -> d_alloc_parallel("5")
                                       -> d_wait_lookup(child)
-> inode_lock_shared(dir)

Both sleep uninterruptibly.
A shell redirect to /dev/fd/5 is enough for P2.

I don't tink it even needs O_CREAT. Any queued writer like a mkdir in
the directory or chmod on it plus a lookup_slow() of the same name that
holds the lock shared and waits on the child has the asme issue.

And under /proc/sys/net it doesn't need a failing lookup either.
proc_sys_compare() matches any in-lookup dentry but after the wait
sysctl_is_seen() rejects a dentry from another netns and we get our
own.

d_add_ci() in 1/4 gets this right and finishes all in-lookup dentries
before relocking. So I think procfs needs to do the same, sm like
(UNTESTED):

diff --git a/fs/proc/base.c b/fs/proc/base.c
index 5f9ec52..d30f877 100644
--- a/fs/proc/base.c
+++ b/fs/proc/base.c
@@ -2105,19 +2105,19 @@ bool proc_fill_cache(struct file *file, struct dir_context *ctx,
 	struct inode *inode;
 	unsigned type = DT_UNKNOWN;
 	ino_t ino = 1;
+	bool relock = false;
 
 	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.
+		 * Wait for the other lookup without the directory lock and
+		 * don't take it back while @child is in-lookup: lookup_open()
+		 * waits for in-lookup dentries with the lock held exclusive.
+		 * procfs never sets S_DEAD.
 		 */
 		inode_unlock_shared(dir->d_inode);
+		relock = true;
 		child = d_alloc_parallel(dir, &qname);
-		inode_lock_shared(dir->d_inode);
 	}
 	if (IS_ERR(child))
 		goto end_instantiate;
@@ -2137,6 +2137,8 @@ bool proc_fill_cache(struct file *file, struct dir_context *ctx,
 	type = inode->i_mode >> 12;
 	dput(child);
 end_instantiate:
+	if (relock)
+		inode_lock_shared(dir->d_inode);
 	return dir_emit(ctx, name, len, ino, type);
 }
 

The sysctl side needs the same.

This also has the same lookup_one_qstr_excl() problem as the earlier
patch.



More information about the linux-um mailing list