FAILED: patch "[PATCH] readdir: take no inode lock on an immutable directory" failed to apply to 7.2-stable tree
Subsystems (2)
[email protected]
maintainerThe patch below does not apply to the 7.2-stable tree.
If someone wants it applied there, or to any other stable or longterm tree, then please email the backport, including the original git commit id to <[email protected]>.
To reproduce the conflict and resubmit, you may use the following commands:
git fetch https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/ linux-7.2.y
git checkout FETCH_HEAD
git cherry-pick -x e08f57a7dcb83818c058e415988c23beb6c5a47f
# <resolve conflicts, build, test, etc.>
git commit -s
git send-email --to '<[email protected]>' --in-reply-to '2026101206-reexamine-jolliness-5e12@gregkh' --subject-prefix 'PATCH 7.2.y' 'HEAD^..'
Possible dependencies:
thanks,
greg k-h
------------------ original commit in Linus's tree ------------------
From e08f57a7dcb83818c058e415988c23beb6c5a47f Mon Sep 17 00:00:00 2001
From: Christian Brauner <[email protected]>
Date: Fri, 2 Oct 2026 15:52:50 +0200
Subject: [PATCH] readdir: take no inode lock on an immutable directory
iterate_dir() takes the directory's i_rwsem shared and holds it across ->iterate_shared(). For a directory that never has an entry and is never removed the lock keeps nothing still, it only orders every reader and every writer of that inode behind each other.
For the directory of a nullfs instance that matters. The instance of the initial mount namespace is the root of every empty mount namespace and the private instance is the root of every kernel thread, so one inode is shared across users who have nothing else in common. And a reader can hold the lock for as long as it likes: back the getdents() buffer with a mapping of a file on a FUSE mount of your own, let the copy of "." and ".." fault and let the server wait. Queue an exclusive taker behind it, a mkdir() in that directory goes through start_dirop() before the read-only mount is reported, and from then on every lookup that misses the dcache in that directory, every create and every mount on it waits until the server answers. One user of an empty mount namespace stalls all the others.
Add FOP_IMMUTABLE for the file operations of a directory that never changes and is never removed and let iterate_dir() skip the lock for it. The flag never changes for a file, ->f_pos is protected by f_pos_lock since directories are FMODE_ATOMIC_POS, IS_DEADDIR can't be set on such a directory and neither touch_atime() nor fsnotify take i_rwsem. Set it on the nullfs directory. The placeholder directories of libfs never have an entry either but their owners remove them, so they keep the lock.
fs/nullfs.c +1 -0
@@ -36,6 +36,7 @@ static const struct file_operations nullfs_dir_operations = { .fsync = noop_fsync, .lock = nullfs_nolock, .flock = nullfs_nolock, + .fop_flags = FOP_IMMUTABLE, };static int nullfs_fs_fill_super (struct super_block *s,struct fs_context *fc)
fs/readdir.c +9 -4
@@ -87,6 +87,8 @@ EXPORT_SYMBOL(wrap_directory_iterator);int iterate_dir (struct file *file,struct dir_context *ctx) {struct inode *inode =file_inode (file); +/* never an entry, never removed: nothing for the lock to keep still */ +bool locked = !(file->f_op->fop_flags &FOP_IMMUTABLE );int res = -ENOTDIR ;if (!file->f_op->iterate_shared)
@@ -100,9 +102,11 @@ int iterate_dir(struct file *file, struct dir_context *ctx)if (res) goto out; - res =down_read_killable (&inode->i_rwsem); -if (res) - goto out; +if (locked) { + res =down_read_killable (&inode->i_rwsem); +if (res) + goto out; + } res = -ENOENT ;if (!IS_DEADDIR (inode)) {
@@ -112,7 +116,8 @@ int iterate_dir(struct file *file, struct dir_context *ctx)fsnotify_access (file);file_accessed (file); } -inode_unlock_shared (inode); +if (locked) +inode_unlock_shared (inode); out:return res; }
include/linux/fs.h +2 -0
@@ -1978,6 +1978,8 @@ struct file_operations {#define FOP_ASYNC_LOCK ((__force fop_flags_t)(1 << 6))/* File system supports uncached read/write buffered IO */ #define FOP_DONTCACHE ((__force fop_flags_t)(1 << 7)) +/* Never changes and is never removed, readdir of a directory takes no lock */ +#define FOP_IMMUTABLE ((__force fop_flags_t)(1 << 8))/* Wrap a directory iterator that needs exclusive inode access */ int wrap_directory_iterator(struct file *, struct dir_context *,
Sasha Levin
maintainerPATCH 1/2 nullfs: refuse file locks
From: Christian Brauner <[email protected]>
[ Upstream commit 31a1fb90624d7e275db775a29a0e00a7f8b3df63 ]
Refuse flock() and POSIX locks on nullfs. Its one inode is the root of every kernel thread and the following patches make it the directory that stands in for an unmounted mount, so a lock taken through one such directory would block the locks of every other holder and F_GETLK would tell them the pid of the holder. Give the directory file operations of its own: what libfs gives an empty directory plus ->lock and ->flock that fail with ENOLCK, the way a filesystem without lock support does.
Stable-dep-of: e08f57a7dcb8 ("readdir: take no inode lock on an immutable directory")
--- fs/nullfs.c | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+)
fs/nullfs.c +29 -0
@@ -8,6 +8,34 @@ static const struct super_operations nullfs_super_operations = { .statfs = simple_statfs, }; +static loff_t nullfs_dir_llseek (struct file *file,loff_t offset,int whence) +{ +/* an empty directory has two entries . and .. at offsets 0 and 1 */ +return generic_file_llseek_size (file, offset, whence,2 ,2 ); +} + +static int nullfs_dir_readdir (struct file *file,struct dir_context *ctx) +{ +dir_emit_dots (file, ctx); +return 0 ; +} + +/* the one inode of nullfs is shared by every holder, so no locks on it */ +static int nullfs_nolock (struct file *file,int cmd,struct file_lock *fl) +{ +return -ENOLCK ; +} + +/* what libfs gives an empty directory, plus the refusal of file locks */ +static const struct file_operations nullfs_dir_operations = { + .llseek = nullfs_dir_llseek, + .read = generic_read_dir, + .iterate_shared = nullfs_dir_readdir, + .fsync = noop_fsync, + .lock = nullfs_nolock, + .flock = nullfs_nolock, +}; +static int nullfs_fs_fill_super (struct super_block *s,struct fs_context *fc) {struct inode *inode;
@@ -28,6 +56,7 @@ static int nullfs_fs_fill_super(struct super_block *s, struct fs_context *fc)/* nullfs is permanently empty... */ make_empty_dir_inode (inode); + inode->i_fop = &nullfs_dir_operations;simple_inode_init_ts (inode); inode->i_ino =1 ;/* ... and immutable, reading it leaves no trace either. */
Signature · 1 line
2.53.0
Sasha Levin
maintainerPATCH 2/2 readdir: take no inode lock on an immutable directory
From: Christian Brauner <[email protected]>
[ Upstream commit e08f57a7dcb83818c058e415988c23beb6c5a47f ]
iterate_dir() takes the directory's i_rwsem shared and holds it across ->iterate_shared(). For a directory that never has an entry and is never removed the lock keeps nothing still, it only orders every reader and every writer of that inode behind each other.
For the directory of a nullfs instance that matters. The instance of the initial mount namespace is the root of every empty mount namespace and the private instance is the root of every kernel thread, so one inode is shared across users who have nothing else in common. And a reader can hold the lock for as long as it likes: back the getdents() buffer with a mapping of a file on a FUSE mount of your own, let the copy of "." and ".." fault and let the server wait. Queue an exclusive taker behind it, a mkdir() in that directory goes through start_dirop() before the read-only mount is reported, and from then on every lookup that misses the dcache in that directory, every create and every mount on it waits until the server answers. One user of an empty mount namespace stalls all the others.
Add FOP_IMMUTABLE for the file operations of a directory that never changes and is never removed and let iterate_dir() skip the lock for it. The flag never changes for a file, ->f_pos is protected by f_pos_lock since directories are FMODE_ATOMIC_POS, IS_DEADDIR can't be set on such a directory and neither touch_atime() nor fsnotify take i_rwsem. Set it on the nullfs directory. The placeholder directories of libfs never have an entry either but their owners remove them, so they keep the lock.
--- fs/nullfs.c | 1 + fs/readdir.c | 13 +++++++++---- include/linux/fs.h | 2 ++ 3 files changed, 12 insertions(+), 4 deletions(-)
fs/nullfs.c +1 -0
@@ -34,6 +34,7 @@ static const struct file_operations nullfs_dir_operations = { .fsync = noop_fsync, .lock = nullfs_nolock, .flock = nullfs_nolock, + .fop_flags = FOP_IMMUTABLE, };static int nullfs_fs_fill_super (struct super_block *s,struct fs_context *fc)
fs/readdir.c +9 -4
@@ -87,6 +87,8 @@ EXPORT_SYMBOL(wrap_directory_iterator);int iterate_dir (struct file *file,struct dir_context *ctx) {struct inode *inode =file_inode (file); +/* never an entry, never removed: nothing for the lock to keep still */ +bool locked = !(file->f_op->fop_flags &FOP_IMMUTABLE );int res = -ENOTDIR ;if (!file->f_op->iterate_shared)
@@ -100,9 +102,11 @@ int iterate_dir(struct file *file, struct dir_context *ctx)if (res) goto out; - res =down_read_killable (&inode->i_rwsem); -if (res) - goto out; +if (locked) { + res =down_read_killable (&inode->i_rwsem); +if (res) + goto out; + } res = -ENOENT ;if (!IS_DEADDIR (inode)) {
@@ -112,7 +116,8 @@ int iterate_dir(struct file *file, struct dir_context *ctx)fsnotify_access (file);file_accessed (file); } -inode_unlock_shared (inode); +if (locked) +inode_unlock_shared (inode); out:return res; }
include/linux/fs.h +2 -0
@@ -1980,6 +1980,8 @@ struct file_operations {#define FOP_ASYNC_LOCK ((__force fop_flags_t)(1 << 6))/* File system supports uncached read/write buffered IO */ #define FOP_DONTCACHE ((__force fop_flags_t)(1 << 7)) +/* Never changes and is never removed, readdir of a directory takes no lock */ +#define FOP_IMMUTABLE ((__force fop_flags_t)(1 << 8))/* Wrap a directory iterator that needs exclusive inode access */ int wrap_directory_iterator(struct file *, struct dir_context *,
Signature · 1 line
2.53.0