Skip to contentSkip to conversation

vfs: avoid spurious dentry ref/unref cycle on open

PATCH New Mateusz Guzik 1 participant 1 message Mar 4, 2025 VFS

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 vfs: avoid spurious dentry ref/unref cycle on open

Opening a file grabs a reference on the terminal dentry in __legitimize_path(), then another one in do_dentry_open() and finally drops the initial reference in terminate_walk().

That's 2 modifications which don't need to be there -- do_dentry_open() can consume the already held reference instead.

When benchmarking on a 20-core vm using will-it-scale's open3_processes ("Same file open/close"), the results are (ops/s):

before: 3087010
after:  4173977 (+35%)

---

Al, while I originally brought this up you objected to my patch, which I then rewrote to the form below. You wrote your own more invovled variant, but the effort to get this in seem stalled:
https://lore.kernel.org/linux-fsdevel/20240822003359.GO504335@ZenIV/

(I think there was a bigger git branch somewhere, but now I can't find it.)

The lockref thing is increasingly getting in the way of some other stuff and is just overhead which is not need to be there.

If you don't have time to finish your patches, how about the variant below? This is rebased v2 I sent a while back and which got no feedback.

I am indifferent as to what lands, as long as the extra ref cycle is eliminated.

 fs/internal.h |  3 ++-
 fs/namei.c    | 15 ++++++++++++---
 fs/open.c     | 44 +++++++++++++++++++++++++++++++++++++++++---
 3 files changed, 55 insertions(+), 7 deletions(-)
fs/internal.h +2 -1
index 3d05a989e4fa..1d0eb25a7598 100644
@@ -193,7 +193,8 @@ int chmod_common(const struct path *path, umode_t mode);
 int do_fchownat(int dfd, const char __user *filename, uid_t user, gid_t group,
 		int flag);
 int chown_common(const struct path *path, uid_t user, gid_t group);
-extern int vfs_open(const struct path *, struct file *);
+int vfs_open_consume(struct path *, struct file *);
+int vfs_open(const struct path *, struct file *);
 
 /*
  * inode.c
fs/namei.c +12 -3
index d00443e38d3a..8ce8e6038346 100644
@@ -3796,6 +3796,7 @@ static const char *open_last_lookups(struct nameidata *nd,
 static int do_open(struct nameidata *nd,
 		   struct file *file, const struct open_flags *op)
 {
+	struct vfsmount *mnt;
 	struct mnt_idmap *idmap;
 	int open_flag = op->open_flag;
 	bool do_truncate;
@@ -3833,11 +3834,17 @@ static int do_open(struct nameidata *nd,
 		error = mnt_want_write(nd->path.mnt);
 		if (error)
 			return error;
+		/*
+		 * We grab an additional reference here because after the call to
+		 * vfs_open_consume() we no longer own the reference in nd->path.mnt
+		 * while we need to undo write access below.
+		 */
+		mnt = mntget(nd->path.mnt);
 		do_truncate = true;
 	}
 	error = may_open(idmap, &nd->path, acc_mode, open_flag);
 	if (!error && !(file->f_mode & FMODE_OPENED))
-		error = vfs_open(&nd->path, file);
+		error = vfs_open_consume(&nd->path, file);
 	if (!error)
 		error = security_file_post_open(file, op->acc_mode);
 	if (!error && do_truncate)
@@ -3846,8 +3853,10 @@ static int do_open(struct nameidata *nd,
 		WARN_ON(1);
 		error = -EINVAL;
 	}
-	if (do_truncate)
-		mnt_drop_write(nd->path.mnt);
+	if (do_truncate) {
+		mnt_drop_write(mnt);
+		mntput(mnt);
+	}
 	return error;
 }
 
fs/open.c +41 -3

87 lines not on this page · raw message

Signature · 1 line
2.43.0