]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
fs: touch up predicts in path lookup
authorMateusz Guzik <mjguzik@gmail.com>
Wed, 5 Nov 2025 15:06:30 +0000 (16:06 +0100)
committerChristian Brauner <brauner@kernel.org>
Thu, 13 Nov 2025 13:22:25 +0000 (14:22 +0100)
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

Signed-off-by: Mateusz Guzik <mjguzik@gmail.com>
Link: https://patch.msgid.link/20251105150630.756606-1-mjguzik@gmail.com
Signed-off-by: Christian Brauner <brauner@kernel.org>
fs/namei.c

index 25cc9680e6dced3dcff3148a1ef6df2bfdcb0d6f..a5bffc2a29f6cf4369d057092243df591644e208 100644 (file)
@@ -990,8 +990,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))
@@ -1073,7 +1073,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) {
@@ -2140,7 +2140,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)
@@ -2582,10 +2582,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();
@@ -2599,7 +2599,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)))
@@ -2618,7 +2618,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);
@@ -2671,7 +2671,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;