[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <20251106180103.923856-2-mjguzik@gmail.com>
Date: Thu, 6 Nov 2025 19:00:59 +0100
From: Mateusz Guzik <mjguzik@...il.com>
To: brauner@...nel.org
Cc: viro@...iv.linux.org.uk,
jack@...e.cz,
linux-kernel@...r.kernel.org,
linux-fsdevel@...r.kernel.org,
linux-ext4@...r.kernel.org,
tytso@....edu,
torvalds@...ux-foundation.org,
josef@...icpanda.com,
linux-btrfs@...r.kernel.org,
Mateusz Guzik <mjguzik@...il.com>
Subject: [PATCH v2 1/4] fs: speed up path lookup with cheaper MAY_EXEC checks
Vast majority of real-world lookups happen on directories which are
traversable by anyone. Figuring out that this holds for a given inode
can be done when instantiating it or changing permissions, avoiding the
overhead during lookup. Stats below.
A simple microbench of stating /usr/include/linux/fs.h on ext4 in a loop
on Sapphire Rapids (ops/s):
before: 3640352
after: 3797258 (+4%)
Filesystems interested in utilizing the feature call inode_enable_fast_may_exec().
Explicit opt-in is necessary as some filesystems have custom inode
permission check hooks which happen to be of no significance for
MAY_EXEC. With an opt-in we know it can be safely ignored. Otherwise any
inode with such a func present would need to be excluded.
inodes which end up skipping perm checks during kernel build grouped
per-fs (0 means checks executed, 1 means skipped):
@[devpts]:
[0, 1) 2 |@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@|
@[cgroup2]:
[0, 1) 68 |@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@|
@[tmpfs]:
[0, 1) 84 |@@@@@@@@@ |
[1, ...) 451 |@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@|
@[sysfs]:
[0, 1) 4532 |@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@|
@[devtmpfs]:
[0, 1) 3609 |@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ |
[1, ...) 3790 |@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@|
@[proc]:
[0, 1) 19855 |@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@|
@[ext4]:
[0, 1) 484292 |@@@ |
[1, ...) 7775413 |@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@|
@[btrfs]:
[0, 1) 5628821 |@@@@@ |
[1, ...) 52551904 |@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@|
Note that devpts, cgroup2, sysfs and proc are not opting in and thus got
no hits.
gathered with:
bpftrace -e 'kprobe:security_inode_permission { @[str(((struct inode *)arg0)->i_sb->s_type->name)] = lhist(((struct inode *)arg0)->i_opflags & 0x80 ? 1 : 0, 0, 1, 1); }'
Signed-off-by: Mateusz Guzik <mjguzik@...il.com>
---
fs/attr.c | 1 +
fs/namei.c | 95 +++++++++++++++++++++++++++++++++++++++++++++-
fs/posix_acl.c | 1 +
fs/xattr.c | 1 +
include/linux/fs.h | 21 +++++++---
5 files changed, 111 insertions(+), 8 deletions(-)
diff --git a/fs/attr.c b/fs/attr.c
index 795f231d00e8..572363ff9c6d 100644
--- a/fs/attr.c
+++ b/fs/attr.c
@@ -549,6 +549,7 @@ int notify_change(struct mnt_idmap *idmap, struct dentry *dentry,
if (!error) {
fsnotify_change(dentry, ia_valid);
+ inode_recalc_fast_may_exec(inode);
security_inode_post_setattr(idmap, dentry, ia_valid);
}
diff --git a/fs/namei.c b/fs/namei.c
index a9f9d0453425..bc4bd9114c49 100644
--- a/fs/namei.c
+++ b/fs/namei.c
@@ -602,6 +602,97 @@ int inode_permission(struct mnt_idmap *idmap,
}
EXPORT_SYMBOL(inode_permission);
+/**
+ * inode_permission_may_exec - Check traversal right for given inode
+ *
+ * This is a special case routine for may_lookup(). Use inode_permission()
+ * instead even if MAY_EXEC is the only thing you want to check for.
+ */
+static __always_inline int inode_permission_may_exec(struct mnt_idmap *idmap,
+ struct inode *inode, int mask)
+{
+ mask |= MAY_EXEC;
+
+ if (!(READ_ONCE(inode->i_opflags) & IOP_FAST_MAY_EXEC))
+ return inode_permission(idmap, inode, mask);
+
+#ifdef CONFIG_DEBUG_VFS
+ /*
+ * We expect everyone has the execute permission and that there are no
+ * acls. For debug purposes we validate this indeed holds.
+ *
+ * However, we may be racing against setattr and/or setacl, in which case
+ * we will have to redo the check with the appropriate lock held to avoid
+ * false-positives.
+ */
+ unsigned int mode = READ_ONCE(inode->i_mode);
+
+ VFS_BUG_ON_INODE(!S_ISDIR(mode), inode);
+ if (((mode & 0111) != 0111) || !no_acl_inode(inode)) {
+ /*
+ * If we are in RCU mode may_lookup() will unlazy and try
+ * again. Worst case if we are still racing the lock will be
+ * taken below when we get back here.
+ */
+ if (mask & MAY_NOT_BLOCK)
+ return -ECHILD;
+ inode_lock(inode);
+ if (inode->i_opflags & IOP_FAST_MAY_EXEC) {
+ VFS_BUG_ON_INODE((inode->i_mode & 0111) != 0111, inode);
+ VFS_BUG_ON_INODE(!no_acl_inode(inode), inode);
+ }
+ inode_unlock(inode);
+ return inode_permission(idmap, inode, mask);
+ }
+#endif
+ return security_inode_permission(inode, mask);
+}
+
+/**
+ * inode_recalc_fast_may_exec - recalc IOP_FAST_MAY_EXEC
+ * @inode: Inode to set/unset the bit on
+ *
+ * To be called if the fs considers the inode eligible for short-circuited
+ * permission checks.
+ */
+void inode_recalc_fast_may_exec(struct inode *inode)
+{
+ unsigned int mode;
+ bool wantbit = false;
+
+ if (!(inode_state_read_once(inode) & I_NEW))
+ lockdep_assert_held_write(&inode->i_rwsem);
+
+ if (!(inode->i_flags & S_CAN_FAST_MAY_EXEC)) {
+ VFS_BUG_ON_INODE(inode->i_opflags & IOP_FAST_MAY_EXEC, inode);
+ return;
+ }
+
+ mode = inode->i_mode;
+ if (!S_ISDIR(mode)) {
+ VFS_BUG_ON_INODE(inode->i_opflags & IOP_FAST_MAY_EXEC, inode);
+ return;
+ }
+
+ if (((mode & 0111) == 0111) && no_acl_inode(inode))
+ wantbit = true;
+
+ if (wantbit) {
+ if (inode->i_opflags & IOP_FAST_MAY_EXEC)
+ return;
+ spin_lock(&inode->i_lock);
+ inode->i_opflags |= IOP_FAST_MAY_EXEC;
+ spin_unlock(&inode->i_lock);
+ } else {
+ if (!(inode->i_opflags & IOP_FAST_MAY_EXEC))
+ return;
+ spin_lock(&inode->i_lock);
+ inode->i_opflags &= ~IOP_FAST_MAY_EXEC;
+ spin_unlock(&inode->i_lock);
+ }
+}
+EXPORT_SYMBOL(inode_recalc_fast_may_exec);
+
/**
* path_get - get a reference to a path
* @path: path to get the reference to
@@ -1855,7 +1946,7 @@ static inline int may_lookup(struct mnt_idmap *idmap,
int err, mask;
mask = nd->flags & LOOKUP_RCU ? MAY_NOT_BLOCK : 0;
- err = inode_permission(idmap, nd->inode, mask | MAY_EXEC);
+ err = inode_permission_may_exec(idmap, nd->inode, mask);
if (likely(!err))
return 0;
@@ -1870,7 +1961,7 @@ static inline int may_lookup(struct mnt_idmap *idmap,
if (err != -ECHILD) // hard error
return err;
- return inode_permission(idmap, nd->inode, MAY_EXEC);
+ return inode_permission_may_exec(idmap, nd->inode, 0);
}
static int reserve_stack(struct nameidata *nd, struct path *link)
diff --git a/fs/posix_acl.c b/fs/posix_acl.c
index 4050942ab52f..da27dd536058 100644
--- a/fs/posix_acl.c
+++ b/fs/posix_acl.c
@@ -1135,6 +1135,7 @@ int vfs_set_acl(struct mnt_idmap *idmap, struct dentry *dentry,
error = -EIO;
if (!error) {
fsnotify_xattr(dentry);
+ inode_recalc_fast_may_exec(inode);
security_inode_post_set_acl(dentry, acl_name, kacl);
}
diff --git a/fs/xattr.c b/fs/xattr.c
index 8851a5ef34f5..917946a7f367 100644
--- a/fs/xattr.c
+++ b/fs/xattr.c
@@ -235,6 +235,7 @@ int __vfs_setxattr_noperm(struct mnt_idmap *idmap,
size, flags);
if (!error) {
fsnotify_xattr(dentry);
+ inode_recalc_fast_may_exec(inode);
security_inode_post_setxattr(dentry, name, value,
size, flags);
}
diff --git a/include/linux/fs.h b/include/linux/fs.h
index 03e450dd5211..4f9962dfe2e6 100644
--- a/include/linux/fs.h
+++ b/include/linux/fs.h
@@ -647,13 +647,14 @@ is_uncached_acl(struct posix_acl *acl)
return (long)acl & 1;
}
-#define IOP_FASTPERM 0x0001
-#define IOP_LOOKUP 0x0002
-#define IOP_NOFOLLOW 0x0004
-#define IOP_XATTR 0x0008
+#define IOP_FASTPERM 0x0001
+#define IOP_LOOKUP 0x0002
+#define IOP_NOFOLLOW 0x0004
+#define IOP_XATTR 0x0008
#define IOP_DEFAULT_READLINK 0x0010
-#define IOP_MGTIME 0x0020
-#define IOP_CACHED_LINK 0x0040
+#define IOP_MGTIME 0x0020
+#define IOP_CACHED_LINK 0x0040
+#define IOP_FAST_MAY_EXEC 0x0080
/*
* Inode state bits. Protected by inode->i_lock
@@ -2128,6 +2129,7 @@ extern loff_t vfs_dedupe_file_range_one(struct file *src_file, loff_t src_pos,
#define S_VERITY (1 << 16) /* Verity file (using fs/verity/) */
#define S_KERNEL_FILE (1 << 17) /* File is in use by the kernel (eg. fs/cachefiles) */
#define S_ANON_INODE (1 << 19) /* Inode is an anonymous inode */
+#define S_CAN_FAST_MAY_EXEC (1 << 20) /* Inode is eligible for IOP_FAST_MAY_EXEC */
/*
* Note that nosuid etc flags are inode-specific: setting some file-system
@@ -2904,6 +2906,13 @@ static inline int inode_init_always(struct super_block *sb, struct inode *inode)
return inode_init_always_gfp(sb, inode, GFP_NOFS);
}
+void inode_recalc_fast_may_exec(struct inode *);
+static inline void inode_enable_fast_may_exec(struct inode *inode)
+{
+ inode->i_flags |= S_CAN_FAST_MAY_EXEC;
+ inode_recalc_fast_may_exec(inode);
+}
+
extern void inode_init_once(struct inode *);
extern void address_space_init_once(struct address_space *mapping);
extern struct inode * igrab(struct inode *);
--
2.48.1
Powered by blists - more mailing lists