Skip to contentSkip to conversation

fs: touch up predicts in path lookup

PATCH Applied Mateusz Guzik 2 participants 4 messages Nov 5, 2025 – Nov 13, 2025 VFS

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 fs: touch up predicts in path lookup

Rationale:
- ND_ROOT_PRESET is only set in a condition already marked unlikely
- LOOKUP_IS_SCOPED already has unlikely on it, but inconsistently applied
- set_root() only fails if there is a bug
- most names are not empty (see !*s)
- most of the time path_init() does not encounter LOOKUP_CACHED without LOOKUP_RCU
- LOOKUP_IN_ROOT is a rarely seen flag

---
 fs/namei.c | 18 +++++++++---------
 1 file changed, 9 insertions(+), 9 deletions(-)
fs/namei.c +9 -9
index 39c4d52f5b54..a9f9d0453425 100644
@@ -951,8 +951,8 @@ static int complete_walk(struct nameidata *nd)
 		 * We don't want to zero nd->root for scoped-lookups or
 		 * externally-managed nd->root.
 		 */
-		if (!(nd->state & ND_ROOT_PRESET))
-			if (!(nd->flags & LOOKUP_IS_SCOPED))
+		if (likely(!(nd->state & ND_ROOT_PRESET)))
+			if (likely(!(nd->flags & LOOKUP_IS_SCOPED)))
 				nd->root.mnt = NULL;
 		nd->flags &= ~LOOKUP_CACHED;
 		if (!try_to_unlazy(nd))
@@ -1034,7 +1034,7 @@ static int nd_jump_root(struct nameidata *nd)
 	}
 	if (!nd->root.mnt) {
 		int error = set_root(nd);
-		if (error)
+		if (unlikely(error))
 			return error;
 	}
 	if (nd->flags & LOOKUP_RCU) {
@@ -2101,7 +2101,7 @@ static const char *handle_dots(struct nameidata *nd, int type)
 
 		if (!nd->root.mnt) {
 			error = ERR_PTR(set_root(nd));
-			if (error)
+			if (unlikely(error))
 				return error;
 		}
 		if (nd->flags & LOOKUP_RCU)
@@ -2543,10 +2543,10 @@ static const char *path_init(struct nameidata *nd, unsigned flags)
 	const char *s = nd->pathname;
 
 	/* LOOKUP_CACHED requires RCU, ask caller to retry */
-	if ((flags & (LOOKUP_RCU | LOOKUP_CACHED)) == LOOKUP_CACHED)
+	if (unlikely((flags & (LOOKUP_RCU | LOOKUP_CACHED)) == LOOKUP_CACHED))
 		return ERR_PTR(-EAGAIN);
 
-	if (!*s)
+	if (unlikely(!*s))
 		flags &= ~LOOKUP_RCU;
 	if (flags & LOOKUP_RCU)
 		rcu_read_lock();
@@ -2560,7 +2560,7 @@ static const char *path_init(struct nameidata *nd, unsigned flags)
 	nd->r_seq = __read_seqcount_begin(&rename_lock.seqcount);
 	smp_rmb();
 
-	if (nd->state & ND_ROOT_PRESET) {
+	if (unlikely(nd->state & ND_ROOT_PRESET)) {
 		struct dentry *root = nd->root.dentry;
 		struct inode *inode = root->d_inode;
 		if (*s && unlikely(!d_can_lookup(root)))
@@ -2579,7 +2579,7 @@ static const char *path_init(struct nameidata *nd, unsigned flags)
 	nd->root.mnt = NULL;
 
 	/* Absolute pathname -- fetch the root (LOOKUP_IN_ROOT uses nd->dfd). */
-	if (*s == '/' && !(flags & LOOKUP_IN_ROOT)) {
+	if (*s == '/' && likely(!(flags & LOOKUP_IN_ROOT))) {
 		error = nd_jump_root(nd);
 		if (unlikely(error))
 			return ERR_PTR(error);
@@ -2632,7 +2632,7 @@ static const char *path_init(struct nameidata *nd, unsigned flags)
 	}
 
 	/* For scoped-lookups we need to set the root to the dirfd as well. */
-	if (flags & LOOKUP_IS_SCOPED) {
+	if (unlikely(flags & LOOKUP_IS_SCOPED)) {
 		nd->root = nd->path;
 		if (flags & LOOKUP_RCU) {
 			nd->root_seq = nd->seq;
Signature · 1 line
2.48.1
#1 lore raw

any opinions on this one

Quoted 92 lines · Mateusz Guzik

Rationale:
- ND_ROOT_PRESET is only set in a condition already marked unlikely
- LOOKUP_IS_SCOPED already has unlikely on it, but inconsistently applied
- set_root() only fails if there is a bug
- most names are not empty (see !*s)
- most of the time path_init() does not encounter LOOKUP_CACHED without LOOKUP_RCU
- LOOKUP_IN_ROOT is a rarely seen flag

---
 fs/namei.c | 18 +++++++++---------
 1 file changed, 9 insertions(+), 9 deletions(-)
fs/namei.c +9 -10
index 39c4d52f5b54..a9f9d0453425 100644
@@ -951,8 +951,8 @@ static int complete_walk(struct nameidata *nd)
                 * We don't want to zero nd->root for scoped-lookups or
                 * externally-managed nd->root.
                 */
-               if (!(nd->state & ND_ROOT_PRESET))
-                       if (!(nd->flags & LOOKUP_IS_SCOPED))
+               if (likely(!(nd->state & ND_ROOT_PRESET)))
+                       if (likely(!(nd->flags & LOOKUP_IS_SCOPED)))
                                nd->root.mnt = NULL;
                nd->flags &= ~LOOKUP_CACHED;
                if (!try_to_unlazy(nd))
@@ -1034,7 +1034,7 @@ static int nd_jump_root(struct nameidata *nd)
        }
        if (!nd->root.mnt) {
                int error = set_root(nd);
-               if (error)
+               if (unlikely(error))
                        return error;
        }
        if (nd->flags & LOOKUP_RCU) {
@@ -2101,7 +2101,7 @@ static const char *handle_dots(struct nameidata *nd, int type)
 
                if (!nd->root.mnt) {
                        error = ERR_PTR(set_root(nd));
-                       if (error)
+                       if (unlikely(error))
                                return error;
                }
                if (nd->flags & LOOKUP_RCU)
@@ -2543,10 +2543,10 @@ static const char *path_init(struct nameidata *nd, unsigned flags)
        const char *s = nd->pathname;
 
        /* LOOKUP_CACHED requires RCU, ask caller to retry */
-       if ((flags & (LOOKUP_RCU | LOOKUP_CACHED)) == LOOKUP_CACHED)
+       if (unlikely((flags & (LOOKUP_RCU | LOOKUP_CACHED)) == LOOKUP_CACHED))
                return ERR_PTR(-EAGAIN);
 
-       if (!*s)
+       if (unlikely(!*s))
                flags &= ~LOOKUP_RCU;
        if (flags & LOOKUP_RCU)
                rcu_read_lock();
@@ -2560,7 +2560,7 @@ static const char *path_init(struct nameidata *nd, unsigned flags)
        nd->r_seq = __read_seqcount_begin(&rename_lock.seqcount);
        smp_rmb();
 
-       if (nd->state & ND_ROOT_PRESET) {
+       if (unlikely(nd->state & ND_ROOT_PRESET)) {
                struct dentry *root = nd->root.dentry;
                struct inode *inode = root->d_inode;
                if (*s && unlikely(!d_can_lookup(root)))
@@ -2579,7 +2579,7 @@ static const char *path_init(struct nameidata *nd, unsigned flags)
        nd->root.mnt = NULL;
 
        /* Absolute pathname -- fetch the root (LOOKUP_IN_ROOT uses nd->dfd). */
-       if (*s == '/' && !(flags & LOOKUP_IN_ROOT)) {
+       if (*s == '/' && likely(!(flags & LOOKUP_IN_ROOT))) {
                error = nd_jump_root(nd);
                if (unlikely(error))
                        return ERR_PTR(error);
@@ -2632,7 +2632,7 @@ static const char *path_init(struct nameidata *nd, unsigned flags)
        }
 
        /* For scoped-lookups we need to set the root to the dirfd as well. */
-       if (flags & LOOKUP_IS_SCOPED) {
+       if (unlikely(flags & LOOKUP_IS_SCOPED)) {
                nd->root = nd->path;
                if (flags & LOOKUP_RCU) {
                        nd->root_seq = nd->seq;
--

2.48.1

#2 lore raw
Quoted 1 line · Mateusz Guzik

any opinions on this one

Yes, looks all sensible to me.

maintainer Applied
#3 lore raw
In reply to Mateusz Guzik
Quoted 11 lines · Mateusz Guzik

Rationale:
- ND_ROOT_PRESET is only set in a condition already marked unlikely
- LOOKUP_IS_SCOPED already has unlikely on it, but inconsistently applied
- set_root() only fails if there is a bug
- most names are not empty (see !*s)
- most of the time path_init() does not encounter LOOKUP_CACHED without LOOKUP_RCU
- LOOKUP_IN_ROOT is a rarely seen flag

[...]

Applied to the vfs-6.19.misc branch of the vfs/vfs.git tree. Patches in the vfs-6.19.misc branch should appear in linux-next soon.

Please report any outstanding bugs that were missed during review in a new review to the original patch series allowing us to drop it.

It's encouraged to provide Acked-bys and Reviewed-bys even though the patch has now been applied. If possible patch trailers will be updated.

Note that commit hashes shown below are subject to change due to rebase, trailer updates or similar. If in doubt, please check the listed branch.

tree:   https://git.kernel.org/pub/scm/linux/kernel/git/vfs/vfs.git
branch: vfs-6.19.misc

[1/1] fs: touch up predicts in path lookup

      https://git.kernel.org/vfs/vfs/c/030e86dfdaa7