Skip to contentSkip to conversation

multiple-filesystems: prepare for changes to VFS locking

PATCH Superseded NeilBrown 4 participants 14 messages Nov 17 – Nov 25

Subsystems (8)

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

NeilBrown

author
#0 lore raw

v2 cover multiple-filesystems: prepare for changes to VFS locking

In this v2:
- shmem patch is dropped - it isn't necessary and the is a problem with it which isn't worth fixing at this point
- configfs patch is dropped - Al didn't like it and it isn't necessary
- ovl patch is dropped - I probably need to solve that a different way and it is significant enough that it deserves to be separate from this series
- slight renaming of bool var and goto labels in first patch

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, afs, 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.

Thanks,
NeilBrown

[PATCH v2 1/4] VFS/xfs/ntfs: drop parent lock across [PATCH v2 2/4] coda: don't d_drop() early.
[PATCH v2 3/4] hostfs: don't d_drop() before d_splice_alias() in [PATCH v2 4/4] procfs: drop parent lock for d_alloc_parallel() in

author
#1 lore raw

PATCH v2 1/4 VFS/xfs/ntfs: drop parent lock across d_alloc_parallel() in d_add_ci()

From: NeilBrown <[email protected]>

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.

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 (shared or exclusive as determined by LOOKUP_SHARED) to avoid future deadlock.

---
 Documentation/filesystems/porting.rst |  7 ++++++
 fs/dcache.c                           | 32 +++++++++++++++++++++++----
 fs/ntfs/namei.c                       |  2 +-
 fs/xfs/xfs_iops.c                     |  2 +-
 include/linux/dcache.h                |  3 ++-
 5 files changed, 39 insertions(+), 7 deletions(-)
Documentation/filesystems/porting.rst +7 -0
index c38f5df755bb..4cab51e1bd6c 100644
@@ -1422,3 +1422,10 @@ 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**
+
+d_add_ci() must now be passed the flags arguemnt that was given to ->lookup
+
fs/dcache.c +28 -4
index 720b32cb2e03..255ebdad303d 100644
@@ -2419,6 +2419,7 @@ EXPORT_SYMBOL(d_obtain_root);
  * @dentry: the negative dentry that was passed to the parent's lookup func
  * @inode:  the inode case-insensitive lookup has found
  * @name:   the case-exact name to be associated with the returned dentry
+ * @lookup_flags: flags passed to ->lookup
  *
  * This is to avoid filling the dcache with case-insensitive names to the
  * same inode, only the actual correct case is stored in the dcache for
@@ -2431,9 +2432,10 @@ EXPORT_SYMBOL(d_obtain_root);
  * the exact case, and return the spliced entry.
  */
 struct dentry *d_add_ci(struct dentry *dentry, struct inode *inode,
-			struct qstr *name)
+			struct qstr *name, unsigned int lookup_flags)
 {
 	struct dentry *found, *res;
+	bool must_relock = false;
 
 	/*
 	 * First check if a dentry matching the name already exists,
@@ -2445,24 +2447,46 @@ struct dentry *d_add_ci(struct dentry *dentry, struct inode *inode,
 		return found;
 	}
 	if (d_in_lookup(dentry)) {
+		/*
+		 * We are holding parent lock and so don't want to 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
+		 */
+		if (lookup_flags & LOOKUP_SHARED)
+			inode_unlock_shared(d_inode(dentry->d_parent));
+		else
+			inode_unlock(d_inode(dentry->d_parent));
+		must_relock = true;
 		found = d_alloc_parallel(dentry->d_parent, name);
 		if (IS_ERR(found) || !d_in_lookup(found)) {
 			iput(inode);
-			return found;
+			goto out_relock;
 		}
 	} else {
 		found = d_alloc(dentry->d_parent, name);
 		if (!found) {
 			iput(inode);
 			return ERR_PTR(-ENOMEM);
-		} 
+		}
 	}
 	res = d_splice_alias(inode, found);
 	if (res) {
 		d_lookup_done(found);
 		dput(found);
-		return res;
+		found = res;
 	}
+	if (!must_relock)
+		return found;
+out_relock:
+	d_lookup_done(dentry);
+	if (lookup_flags & LOOKUP_SHARED)
+		inode_lock_shared(d_inode(dentry->d_parent));
+	else
+		inode_lock_nested(d_inode(dentry->d_parent), I_MUTEX_PARENT);
 	return found;
 }
 EXPORT_SYMBOL(d_add_ci);
fs/ntfs/namei.c +1 -1
index b46fe44b63b5..16ed6027fa13 100644
@@ -332,7 +332,7 @@ static struct dentry *ntfs_lookup(struct inode *dir_ino, struct dentry *dent,
 		}
 		nls_name.hash = full_name_hash(dent, nls_name.name, nls_name.len);
 
-		dent = d_add_ci(dent, dent_inode, &nls_name);
+		dent = d_add_ci(dent, dent_inode, &nls_name, flags);
 		kfree(nls_name.name);
 		return dent;
 
fs/xfs/xfs_iops.c +1 -1
index d307ec97fac8..ec08c882c6f7 100644
@@ -368,7 +368,7 @@ xfs_vn_ci_lookup(
 	/* else case-insensitive match... */
 	dname.name = ci_name.name;
 	dname.len = ci_name.len;
-	dentry = d_add_ci(dentry, VFS_I(ip), &dname);
+	dentry = d_add_ci(dentry, VFS_I(ip), &dname, flags);
 	kfree(ci_name.name);
 	return dentry;
 }
include/linux/dcache.h +2 -1
index aaa4ba7a1299..9dd91ba2fe33 100644
@@ -267,7 +267,8 @@ struct dentry *d_duplicate(struct dentry *dentry);
 /* weird procfs mess; *NOT* exported */
 extern struct dentry * d_splice_alias_ops(struct inode *, struct dentry *,
 					  const struct dentry_operations *);
-extern struct dentry * d_add_ci(struct dentry *, struct inode *, struct qstr *);
+extern struct dentry * d_add_ci(struct dentry *, struct inode *, struct qstr *,
+				unsigned int);
 extern bool d_same_name(const struct dentry *dentry, const struct dentry *parent,
 			const struct qstr *name);
 extern struct dentry *d_find_any_alias(struct inode *inode);

base-commit: 7475ea8b58616b33c04afb976cf0fbaad9dd63fb

Signature · 1 line
2.50.0.107.gf914562f5916.dirty
#2 lore raw

Re: [PATCH v2 1/4] VFS/xfs/ntfs: drop parent lock across d_alloc_parallel() in d_add_ci()

Quoted 3 lines · NeilBrown

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.

I'm not sure that holds? Only d_alloc_parallel() honours DCACHE_PAR_LOOKUP. lookup_one_qstr_excl() still uses d_alloc() and relies on the exclusive lock to guarantee that there are no in-lookup children at all. That stops being true here:

fs/dcache.c struct dentry *d_add_ci(struct dentry *dentry, struct inode *inode, · line 2464

Quoted 6 lines
fs/dcache.c +5 -0
+		if (lookup_flags & LOOKUP_SHARED)
+			inode_unlock_shared(d_inode(dentry->d_parent));
+		else
+			inode_unlock(d_inode(dentry->d_parent));
+		must_relock = true;
 		found = d_alloc_parallel(dentry->d_parent, name);

So if "Foo" exists on an ascii-ci xfs:

P1: stat("FOO") P2: unlink("Foo")
lookup_slow()
-> xfs_vn_ci_lookup()
-> d_add_ci(dentry, X, "Foo")

      -> inode_unlock_shared(dir)
      -> found = d_alloc_parallel()
                                     start_dirop()
                                     -> inode_lock_nested(dir)
                                     -> lookup_one_qstr_excl("Foo")
                                        -> d2 = d_alloc()
                                        -> xfs_vn_ci_lookup()
                                           -> d_splice_alias(X, d2)
                                     vfs_unlink()
                                     -> xfs_vn_unlink()
                                        -> d_invalidate(d2)
                                     inode_unlock(dir)
      -> d_splice_alias(X, found)

IOW, "Foo" ends up as a hashed positive dentry for an unlinked inode that nothing revalidates.

open("Foo", O_CREAT) then opens the orphan instead of creating a new file and O_EXCL fails with EEXIST. Do a rename() instead of the unlink() and "Foo" keeps pointing to what is now "Bar". ntfs gets there the same way.

Before this patch lookup_slow() held the lock shared across all of that so start_dirop() couldn't get in between.

So afaict this needs "VFS: use d_alloc_parallel() in lookup_one_qstr_excl()" from your RFC to go in first. Unless I'm missing something obvious.

author
#3 lore raw

Re: [PATCH v2 1/4] VFS/xfs/ntfs: drop parent lock across d_alloc_parallel() in d_add_ci()

Quoted 45 lines · Christian Brauner
Quoted 3 lines · NeilBrown

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.

I'm not sure that holds? Only d_alloc_parallel() honours DCACHE_PAR_LOOKUP. lookup_one_qstr_excl() still uses d_alloc() and relies on the exclusive lock to guarantee that there are no in-lookup children at all. That stops being true here:

fs/dcache.c struct dentry *d_add_ci(struct dentry *dentry, struct inode *inode, · line 2464

Quoted 6 lines
fs/dcache.c +5 -0
+		if (lookup_flags & LOOKUP_SHARED)
+			inode_unlock_shared(d_inode(dentry->d_parent));
+		else
+			inode_unlock(d_inode(dentry->d_parent));
+		must_relock = true;
 		found = d_alloc_parallel(dentry->d_parent, name);

So if "Foo" exists on an ascii-ci xfs:

P1: stat("FOO") P2: unlink("Foo")
lookup_slow()
-> xfs_vn_ci_lookup()
-> d_add_ci(dentry, X, "Foo")

      -> inode_unlock_shared(dir)
      -> found = d_alloc_parallel()
                                     start_dirop()
                                     -> inode_lock_nested(dir)
                                     -> lookup_one_qstr_excl("Foo")
                                        -> d2 = d_alloc()
                                        -> xfs_vn_ci_lookup()
                                           -> d_splice_alias(X, d2)
                                     vfs_unlink()
                                     -> xfs_vn_unlink()
                                        -> d_invalidate(d2)
                                     inode_unlock(dir)
      -> d_splice_alias(X, found)

IOW, "Foo" ends up as a hashed positive dentry for an unlinked inode that nothing revalidates.

open("Foo", O_CREAT) then opens the orphan instead of creating a new file and O_EXCL fails with EEXIST. Do a rename() instead of the unlink() and "Foo" keeps pointing to what is now "Bar". ntfs gets there the same way.

Quoted 7 lines

Before this patch lookup_slow() held the lock shared across all of that so start_dirop() couldn't get in between.

So afaict this needs "VFS: use d_alloc_parallel() in lookup_one_qstr_excl()" from your RFC to go in first. Unless I'm missing something obvious.

Yes, I see that now - thanks.
And that means we don't need LOOKUP_SHARED at all.

The AFS patches also drop and retake the lock, so they need to be after d_alloc_parallel() is used in lookup_one_qstr_excl(), and also after the d_drop() changes. So I think I should combine the AFS patch set with this one. I'll try that and check that it still makes sense.

Thanks,
NeilBrown

#4 lore raw

Re: [PATCH v2 1/4] VFS/xfs/ntfs: drop parent lock across d_alloc_parallel() in d_add_ci()

In reply to NeilBrown

include/linux/dcache.h struct dentry *d_duplicate(struct dentry *dentry); · line 270

Quoted 3 lines · NeilBrown
include/linux/dcache.h +1 -1
 					  const struct dentry_operations *);
-extern struct dentry * d_add_ci(struct dentry *, struct inode *, struct qstr *);
+extern struct dentry * d_add_ci(struct dentry *, struct inode *, struct qstr *,

Please drop the extern and fix up the formatting if you touch the code. Bonus points for spelling out the arguments to actually make it readable.

author
#5 lore raw

Re: [PATCH v2 1/4] VFS/xfs/ntfs: drop parent lock across d_alloc_parallel() in d_add_ci()

Quoted 8 lines · Christoph Hellwig

include/linux/dcache.h struct dentry *d_duplicate(struct dentry *dentry); · line 270

Quoted 3 lines · NeilBrown
include/linux/dcache.h +1 -1
 					  const struct dentry_operations *);
-extern struct dentry * d_add_ci(struct dentry *, struct inode *, struct qstr *);
+extern struct dentry * d_add_ci(struct dentry *, struct inode *, struct qstr *,

Please drop the extern and fix up the formatting if you touch the code. Bonus points for spelling out the arguments to actually make it readable.

I think inconsistent formatting is not much better than bad formatting. Maybe I'll insert a patch which tidies up the whole stanza.

NeilBrown

#6 lore raw

Re: [PATCH v2 1/4] VFS/xfs/ntfs: drop parent lock across d_alloc_parallel() in d_add_ci()

Quoted 10 lines · NeilBrown
Quoted 8 lines · Christoph Hellwig

include/linux/dcache.h struct dentry *d_duplicate(struct dentry *dentry); · line 270

Quoted 3 lines · NeilBrown
include/linux/dcache.h +1 -1
 					  const struct dentry_operations *);
-extern struct dentry * d_add_ci(struct dentry *, struct inode *, struct qstr *);
+extern struct dentry * d_add_ci(struct dentry *, struct inode *, struct qstr *,

Please drop the extern and fix up the formatting if you touch the code. Bonus points for spelling out the arguments to actually make it readable.

Quoted 2 lines

I think inconsistent formatting is not much better than bad formatting. Maybe I'll insert a patch which tidies up the whole stanza.

Well, if you keep this mess it will never get fixed. So please fix it up on the go as we do in most places, which makes a final cleanup pass easier and more likely to actually happen.

author
#7 lore raw

PATCH v2 2/4 coda: don't d_drop() early.

In reply to NeilBrown

From: NeilBrown <[email protected]>

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.

---
 fs/coda/dir.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
fs/coda/dir.c +3 -3
index a85be5962e62..b47299527b65 100644
@@ -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)
Signature · 1 line
2.50.0.107.gf914562f5916.dirty
maintainer Acked-by on v2 2/4
#8 lore raw

Re: [PATCH v2 2/4] coda: don't d_drop() early.

Quoted 7 lines · NeilBrown

From: NeilBrown <[email protected]>

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).

Quoted 7 lines

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.

I was sort of already assuming that the userspace cache manager should never ever traverse into the mounted /coda file system tree because there is only a finite pool of worker threads. But it is good to know that this change would fix at least one possible deadlock.

Quoted 28 lines
---
 fs/coda/dir.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
fs/coda/dir.c +3 -3
index a85be5962e62..b47299527b65 100644
@@ -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)
Signature · 1 line
2.50.0.107.gf914562f5916.dirty
author
#9 lore raw

Re: [PATCH v2 2/4] coda: don't d_drop() early.

Quoted 10 lines · Jan Harkes
Quoted 9 lines · NeilBrown

From: NeilBrown <[email protected]>

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.

Quoted 7 lines
Quoted 5 lines

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.

Thanks,
NeilBrown

Quoted 35 lines

I was sort of already assuming that the userspace cache manager should never ever traverse into the mounted /coda file system tree because there is only a finite pool of worker threads. But it is good to know that this change would fix at least one possible deadlock.

Quoted 28 lines
---
 fs/coda/dir.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
fs/coda/dir.c +3 -3
index a85be5962e62..b47299527b65 100644
@@ -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)
Signature · 1 line
2.50.0.107.gf914562f5916.dirty
author
#10 lore raw

PATCH v2 3/4 hostfs: don't d_drop() before d_splice_alias() in hostfs_mkdir()

In reply to NeilBrown

From: NeilBrown <[email protected]>

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().

---
 fs/hostfs/hostfs_kern.c | 1 -
 1 file changed, 1 deletion(-)
fs/hostfs/hostfs_kern.c +0 -1
index 613146e76dec..7cf25c29cb78 100644
@@ -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);
Signature · 1 line
2.50.0.107.gf914562f5916.dirty
author
#11 lore raw

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

In reply to NeilBrown

From: NeilBrown <[email protected]>

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.

---
 fs/proc/base.c        | 38 +++++++++++++++++++--------------
 fs/proc/proc_sysctl.c | 49 ++++++++++++++++++++++++-------------------
 2 files changed, 49 insertions(+), 38 deletions(-)
fs/proc/base.c +22 -16
index 7685660c3624..5f9ec524ae2a 100644
@@ -2106,24 +2106,30 @@ bool proc_fill_cache(struct file *file, struct dir_context *ctx,
 	unsigned type = DT_UNKNOWN;
 	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);
 		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;
-			}
+		inode_lock_shared(dir->d_inode);
+	}
+	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);
fs/proc/proc_sysctl.c +27 -22

56 lines not on this page · raw message

Signature · 1 line
2.50.0.107.gf914562f5916.dirty
#12 lore raw

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

Review

Quoted 12 lines · NeilBrown
(quoted diff) +10 -0
+	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):

fs/proc/base.c +8 -6
index 5f9ec52..d30f877 100644
@@ -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.

author
#13 lore raw

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

Quoted 41 lines · Christian Brauner

Review

Quoted 12 lines · NeilBrown
(quoted diff) +10 -0
+	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.

Quoted 8 lines

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):

Yes - definitely. Mustn't retake i_rwsem while holding an in-lookup dentry.
Your draft looks good.

Thanks,
NeilBrown

Quoted 46 lines
fs/proc/base.c +8 -6
index 5f9ec52..d30f877 100644
@@ -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.