* [PATCH v4 00/11] vfs: add inode iteration methods
@ 2026-11-25 9:11 Huang Shijie
2026-11-25 9:11 ` [PATCH v4 01/11] vfs: add sb_inode_iter_next() Huang Shijie
` (11 more replies)
0 siblings, 12 replies; 14+ messages in thread
From: Huang Shijie @ 2026-11-25 9:11 UTC (permalink / raw)
To: viro, brauner, jack, hch, agruenba, mic, axboe
Cc: gnoack, paul, jmorris, serge, legion, aleksa, libaokun, ebiggers,
djwong, sandeen, zhongyuan, fangbaoshun, yingzhiwei, wujianyong,
shijie8, linux-fsdevel, linux-kernel, linux-block, gfs2,
linux-security-module, Huang Shijie
This series adds a superblock inode iterator and converts the open-coded
sb->s_inodes walks in the VFS to it. The design follows the cursor-based
construct proposed by Christian Brauner:
https://lore.kernel.org/all/20261120-mitverantwortlich-drehbaren-kantholz-241300d20168@brauner/
and is the same direction as Dave Chinner's patch set:
"vfs: improving inode cache iteration scalability"
https://lore.kernel.org/all/20241002014017.3801899-1-david@fromorbit.com/
The caller owns the iterator state and drives the walk itself:
CLASS(sb_inode_iter, iter)(sb, eligible, priv, flags);
while ((inode = sb_inode_iter_next(&iter)))
/* do stuff */;
The lock does not span iterations. sb_inode_iter_next() takes
sb->s_inode_list_lock, takes a reference on the next inode, drops the lock
again and hands the inode out; the reference is what keeps the cursor on
sb->s_inodes so that the next call can resume from it. As the iterator
is a cleanup class, a caller that breaks out early does not have to call
sb_inode_iter_end() explicitly.
Two optional channels let a caller keep its own state instead of filling in
a shared context:
- iter->eligible is called with inode->i_lock held, so that a caller can
reject an inode before a reference is paid for.
- iter->status carries an error out of the walk. Setting it to a
negative value makes sb_inode_iter_next() return NULL.
Most callers just want a referenced inode and use no flags at all. Three
flags cover the walks that need something else:
- %INO_ITER_F_REFERENCED restricts the walk to inodes that still have a
reference, preserving the "only handle referenced inodes" rule of
landlock's sb-delete walk.
- %INO_ITER_F_NOREF hands out only inodes whose reference count has
already reached zero, and %INO_ITER_F_NOIGET suppresses the reference.
Neither keeps the inode alive, so the caller has to make the inode
unclaimable itself.
A second, internal iterator covers the walks that must not take a
reference at all:
static inline int sb_inode_iter_locked(struct super_block *sb,
int (*iter_fn)(struct inode *,
void *),
void *priv);
- It keeps sb->s_inode_list_lock held for the whole walk and takes no
inode reference, so @iter_fn only has an existence guarantee: it must
not block, and it must do its own inode state checks and its own
reference handling. A callback returns a negative value to stop the
walk with that error, and 0 to continue.
- It lives in fs/internal.h as a static inline rather than being
exported: it is a VFS internal facility, and the small callbacks
inline into their callers.
It serves the three walks that cannot use sb_inode_iter_next():
- nr_blockdev_pages() is reached from si_meminfo(), which kdb calls, and
the iput() that the referenced iterator would do can sleep.
- remove_dquot_ref() has to scan I_NEW inodes as well, which the
referenced iterator filters out; it only touches the quota pointers,
and those have their own locking in dq_data_lock.
- the generic_shutdown_super() poisoning loop has to touch every inode
still on the list, including the I_NEW, I_FREEING and I_WILL_FREE
ones that sb_inode_iter_next() filters out, and it must not bounce
them through __iget()/iput(): the loop overwrites i_sb with
VFS_PTR_POISON, and evict() dereferences i_sb->s_op on the way out.
Patch 1 adds sb_inode_iter_next().
Patch 2 adds sb_inode_iter_locked().
Patch 3-5 convert the callers which cannot use referenced inodes
(nr_blockdev_pages(), dquot removal and the
generic_shutdown_super() poisoning loop).
Patch 6-11 convert the remaining callers (inode eviction, sync_bdevs,
drop_pagecache_sb, add_dquot_ref, landlock's hook_sb_delete
and gfs2_evict_inodes()).
BTW:
This series is based on v7.4-rc2, and applies on top of the last commit
of that base, which is the small patch this series depends on:
v7.4-rc2-22-ga02161f70fcf
commit a02161f70fcf ("fs/drop_caches: skip filesystems without page cache writeback")
That patch was posted as:
https://lore.kernel.org/all/20260923070116.3226846-1-huangsj@hygon.cn/
v3-->v4:
-the interface is redone as a caller-owned cursor, following Christian's
sb_inode_iter construct: sb_inode_iter_next() returns a referenced
inode with sb->s_inode_list_lock dropped, and the reference keeps the
cursor on sb->s_inodes. The callback-based interface of v3, with its
ino_iter_fn and INO_ITER_RET_* return values, is gone.
-the iterator is wrapped in a cleanup class, so a caller that breaks out
of the walk does not have to call sb_inode_iter_end().
-an inode can be rejected before a reference is paid for: @eligible is
called with inode->i_lock held.
-the error channel is now iter->status rather than a three-valued
callback return.
-super_iter_inodes() and super_iter_inodes_locked() are renamed to
sb_inode_iter_next() and sb_inode_iter_locked(), and the latter stays
in fs/internal.h for the three walks that must not take a reference.
-evict_inodes() uses INO_ITER_F_NOREF | INO_ITER_F_NOIGET: it only
takes inodes whose reference count has reached zero, pins each one by
setting I_FREEING and dropping it from the LRU, and runs dispose_list()
with the list lock dropped.
v3: https://lore.kernel.org/all/20261120031305.3561045-1-huangsj@hygon.cn/
v2: https://lore.kernel.org/all/20261113115702.586594-1-huangsj@hygon.cn/
v1: https://lore.kernel.org/all/20261015093854.2082600-1-huangsj@hygon.cn/
Huang Shijie (11):
vfs: add sb_inode_iter_next()
vfs: add sb_inode_iter_locked()
block: convert nr_blockdev_pages() to sb_inode_iter_locked()
fs/quota: convert remove_dquot_ref() to sb_inode_iter_locked()
fs: convert generic_shutdown_super() poison loop
fs/inode: convert evict_inodes() to sb_inode_iter
block: convert sync_bdevs() to sb_inode_iter
fs/drop_caches: convert drop_pagecache_sb() to sb_inode_iter
fs/quota: convert add_dquot_ref() to sb_inode_iter
landlock: convert hook_sb_delete() to sb_inode_iter
gfs2: convert gfs2_evict_inodes() to sb_inode_iter
block/bdev.c | 58 ++++++----------
fs/drop_caches.c | 36 ++++------
fs/gfs2/ops_fstype.c | 26 ++-----
fs/inode.c | 33 +++------
fs/internal.h | 25 +++++++
fs/quota/dquot.c | 120 +++++++++++++++-----------------
fs/super.c | 121 ++++++++++++++++++++++++++++++---
include/linux/fs/super_types.h | 40 +++++++++++
security/landlock/fs.c | 67 +++++-------------
9 files changed, 297 insertions(+), 229 deletions(-)
--
2.53.0
^ permalink raw reply [flat|nested] 14+ messages in thread
* [PATCH v4 01/11] vfs: add sb_inode_iter_next()
2026-11-25 9:11 [PATCH v4 00/11] vfs: add inode iteration methods Huang Shijie
@ 2026-11-25 9:11 ` Huang Shijie
2026-11-25 9:11 ` [PATCH v4 02/11] vfs: add sb_inode_iter_locked() Huang Shijie
` (10 subsequent siblings)
11 siblings, 0 replies; 14+ messages in thread
From: Huang Shijie @ 2026-11-25 9:11 UTC (permalink / raw)
To: viro, brauner, jack, hch, agruenba, mic, axboe
Cc: gnoack, paul, jmorris, serge, legion, aleksa, libaokun, ebiggers,
djwong, sandeen, zhongyuan, fangbaoshun, yingzhiwei, wujianyong,
shijie8, linux-fsdevel, linux-kernel, linux-block, gfs2,
linux-security-module, Huang Shijie
Walking sb->s_inodes requires taking sb->s_inode_list_lock, grabbing a
reference on the next inode, dropping the lock and then working on the
inode. That sequence is open coded in several places, each with its own
subtleties around rescheduling and iput().
Add struct sb_inode_iter, which the caller declares and owns, plus
sb_inode_iter_next() to step through the list and sb_inode_iter_end() to
release the last reference.
As the cursor inode is referenced or pinned by other method, it cannot be
unlinked from sb->s_inodes, so the walk resumes from it after the caller
has dropped the lock.
Callers that need to filter inodes can provide iter->eligible,
which is called with inode->i_lock held, and callers that need to
stop early can set iter->status.
Signed-off-by: Huang Shijie <huangsj@hygon.cn>
---
fs/super.c | 102 +++++++++++++++++++++++++++++++++
include/linux/fs/super_types.h | 40 +++++++++++++
2 files changed, 142 insertions(+)
diff --git a/fs/super.c b/fs/super.c
index c5a0658f7aaf..077e772fa19b 100644
--- a/fs/super.c
+++ b/fs/super.c
@@ -171,6 +171,108 @@ static void super_wake(struct super_block *sb, unsigned int flag)
wake_up_var(&sb->s_flags);
}
+static bool sb_inode_iter_check(struct sb_inode_iter *iter, struct inode *inode)
+{
+ bool ret = false;
+
+ if ((iter->flags & INO_ITER_F_REFERENCED) && !icount_read_once(inode))
+ return false;
+
+ if ((iter->flags & INO_ITER_F_NOREF) && icount_read_once(inode))
+ return false;
+
+ spin_lock(&inode->i_lock);
+
+ if ((iter->flags & INO_ITER_F_NOREF) && icount_read(inode))
+ goto out_unlock;
+
+ if (inode_state_read(inode) & (I_NEW | I_FREEING | I_WILL_FREE))
+ goto out_unlock;
+
+ if (iter->eligible && !iter->eligible(inode, iter->priv))
+ goto out_unlock;
+
+ if (!(iter->flags & INO_ITER_F_NOIGET))
+ __iget(inode);
+
+ ret = true;
+out_unlock:
+ spin_unlock(&inode->i_lock);
+ return ret;
+}
+
+static inline bool sb_inode_iter_flags_valid(int flags)
+{
+ switch (flags) {
+ case 0:
+ case INO_ITER_F_REFERENCED:
+ case INO_ITER_F_NOREF | INO_ITER_F_NOIGET:
+ /* only used by evict_inodes() */
+ return true;
+ default:
+ return false;
+ }
+}
+
+/**
+ * sb_inode_iter_next - return the next inode of @iter->sb
+ * @iter: iterator state
+ *
+ * The returned inode stays on sb->s_inodes for the next round
+ * without holding sb->s_inode_list_lock:
+ * -- By default, this inode has an increased reference count.
+ * -- If INO_ITER_F_NOIGET is set, this inode will not change
+ * its reference count.
+ *
+ * Return the next inode, or NULL once the walk is over or @iter->status is
+ * negative.
+ */
+struct inode *sb_inode_iter_next(struct sb_inode_iter *iter)
+{
+ struct super_block *sb = iter->sb;
+ struct inode *prev = iter->inode, *inode;
+
+ if (!sb_inode_iter_flags_valid(iter->flags)) {
+ VFS_WARN_ON_ONCE(1);
+ iter->status = -EINVAL;
+ return NULL;
+ }
+
+ if (iter->status || iter->done)
+ return NULL;
+
+ spin_lock(&sb->s_inode_list_lock);
+ VFS_WARN_ON_ONCE(prev && list_empty(&prev->i_sb_list));
+ inode = list_prepare_entry(prev, &sb->s_inodes, i_sb_list);
+ list_for_each_entry_continue(inode, &sb->s_inodes, i_sb_list) {
+ if (!sb_inode_iter_check(iter, inode))
+ continue;
+
+ spin_unlock(&sb->s_inode_list_lock);
+ iter->inode = inode;
+ if (!(iter->flags & INO_ITER_F_NOIGET))
+ iput(prev);
+ return inode;
+ }
+ spin_unlock(&sb->s_inode_list_lock);
+
+ iter->inode = NULL;
+ iter->done = true;
+ if (!(iter->flags & INO_ITER_F_NOIGET))
+ iput(prev);
+ return NULL;
+}
+EXPORT_SYMBOL_GPL(sb_inode_iter_next);
+
+void sb_inode_iter_end(struct sb_inode_iter *iter)
+{
+ if (!(iter->flags & INO_ITER_F_NOIGET))
+ iput(iter->inode);
+ iter->inode = NULL;
+ iter->done = true;
+}
+EXPORT_SYMBOL_GPL(sb_inode_iter_end);
+
/*
* One thing we have to be careful of with a per-sb shrinker is that we don't
* drop the last active reference to the superblock from within the shrinker.
diff --git a/include/linux/fs/super_types.h b/include/linux/fs/super_types.h
index 72f4288e16c5..b844e5259360 100644
--- a/include/linux/fs/super_types.h
+++ b/include/linux/fs/super_types.h
@@ -80,6 +80,46 @@ enum freeze_holder {
FREEZE_EXCL = (1U << 3),
};
+/* Caller-owned state for walking sb->s_inodes. */
+struct sb_inode_iter {
+ struct super_block *sb;
+ struct inode *inode;
+ bool (*eligible)(struct inode *inode, void *priv);
+ void *priv;
+ bool done;
+ int status;
+ int flags;
+};
+
+/* Only hand out inodes that still have a reference. */
+#define INO_ITER_F_REFERENCED BIT(0)
+/* Only hand out inodes whose reference count has reached zero. */
+#define INO_ITER_F_NOREF BIT(1)
+/* Do not take a reference on the inodes handed out. */
+#define INO_ITER_F_NOIGET BIT(2)
+
+struct inode *sb_inode_iter_next(struct sb_inode_iter *iter);
+void sb_inode_iter_end(struct sb_inode_iter *iter);
+
+static inline struct sb_inode_iter
+sb_inode_iter_init(struct super_block *sb,
+ bool (*eligible)(struct inode *inode, void *priv),
+ void *priv, int flags)
+{
+ return (struct sb_inode_iter){
+ .sb = sb,
+ .eligible = eligible,
+ .priv = priv,
+ .flags = flags,
+ };
+}
+
+DEFINE_CLASS(sb_inode_iter, struct sb_inode_iter,
+ sb_inode_iter_end(&_T),
+ sb_inode_iter_init(sb, eligible, priv, flags),
+ struct super_block *sb, bool (*eligible)(struct inode *, void *),
+ void *priv, int flags)
+
struct super_operations {
struct inode *(*alloc_inode)(struct super_block *sb);
void (*destroy_inode)(struct inode *inode);
--
2.53.0
^ permalink raw reply related [flat|nested] 14+ messages in thread
* [PATCH v4 02/11] vfs: add sb_inode_iter_locked()
2026-11-25 9:11 [PATCH v4 00/11] vfs: add inode iteration methods Huang Shijie
2026-11-25 9:11 ` [PATCH v4 01/11] vfs: add sb_inode_iter_next() Huang Shijie
@ 2026-11-25 9:11 ` Huang Shijie
2026-11-25 9:11 ` [PATCH v4 03/11] block: convert nr_blockdev_pages() to sb_inode_iter_locked() Huang Shijie
` (9 subsequent siblings)
11 siblings, 0 replies; 14+ messages in thread
From: Huang Shijie @ 2026-11-25 9:11 UTC (permalink / raw)
To: viro, brauner, jack, hch, agruenba, mic, axboe
Cc: gnoack, paul, jmorris, serge, legion, aleksa, libaokun, ebiggers,
djwong, sandeen, zhongyuan, fangbaoshun, yingzhiwei, wujianyong,
shijie8, linux-fsdevel, linux-kernel, linux-block, gfs2,
linux-security-module, Huang Shijie
Add sb_inode_iter_locked(), which keeps sb->s_inode_list_lock held
across the whole walk and takes no reference, so each callback only gets
an existence guarantee. It must do its own inode state checks and must
not block.
It lives in fs/internal.h as a static inline rather than being exported,
so that it stays a VFS internal facility and the small callbacks inline
into their callers. A callback returns a negative value to stop the walk
with that error, and 0 to continue.
Signed-off-by: Huang Shijie <huangsj@hygon.cn>
---
fs/internal.h | 25 +++++++++++++++++++++++++
1 file changed, 25 insertions(+)
diff --git a/fs/internal.h b/fs/internal.h
index 967c8556c17f..0479094e1330 100644
--- a/fs/internal.h
+++ b/fs/internal.h
@@ -139,6 +139,31 @@ struct super_block *user_get_super(dev_t, bool excl);
void put_super(struct super_block *sb);
extern bool mount_capable(struct fs_context *);
+/*
+ * Iteration for internal VFS use only, with sb->s_inode_list_lock held
+ * across the whole walk and no inode reference taken. @iter_fn runs in
+ * atomic context with only an inode existence guarantee, so it must not
+ * block and must perform its own inode state checks.
+ *
+ * Return a negative value to stop the walk with that error, 0 to continue.
+ */
+static inline int sb_inode_iter_locked(struct super_block *sb,
+ int (*iter_fn)(struct inode *inode, void *priv), void *priv)
+{
+ struct inode *inode;
+ int ret = 0;
+
+ spin_lock(&sb->s_inode_list_lock);
+ list_for_each_entry(inode, &sb->s_inodes, i_sb_list) {
+ ret = iter_fn(inode, priv);
+ if (ret)
+ break;
+ }
+ spin_unlock(&sb->s_inode_list_lock);
+
+ return ret;
+}
+
/*
* Prepare superblock for changing its read-only state (i.e., either remount
* read-write superblock read-only or vice versa). After this function returns
--
2.53.0
^ permalink raw reply related [flat|nested] 14+ messages in thread
* [PATCH v4 03/11] block: convert nr_blockdev_pages() to sb_inode_iter_locked()
2026-11-25 9:11 [PATCH v4 00/11] vfs: add inode iteration methods Huang Shijie
2026-11-25 9:11 ` [PATCH v4 01/11] vfs: add sb_inode_iter_next() Huang Shijie
2026-11-25 9:11 ` [PATCH v4 02/11] vfs: add sb_inode_iter_locked() Huang Shijie
@ 2026-11-25 9:11 ` Huang Shijie
2026-11-25 9:11 ` [PATCH v4 04/11] fs/quota: convert remove_dquot_ref() " Huang Shijie
` (8 subsequent siblings)
11 siblings, 0 replies; 14+ messages in thread
From: Huang Shijie @ 2026-11-25 9:11 UTC (permalink / raw)
To: viro, brauner, jack, hch, agruenba, mic, axboe
Cc: gnoack, paul, jmorris, serge, legion, aleksa, libaokun, ebiggers,
djwong, sandeen, zhongyuan, fangbaoshun, yingzhiwei, wujianyong,
shijie8, linux-fsdevel, linux-kernel, linux-block, gfs2,
linux-security-module, Huang Shijie
Convert nr_blockdev_pages() to sb_inode_iter_locked().
No functional change.
Signed-off-by: Huang Shijie <huangsj@hygon.cn>
---
block/bdev.c | 15 +++++++++------
1 file changed, 9 insertions(+), 6 deletions(-)
diff --git a/block/bdev.c b/block/bdev.c
index f20f91b3176e..938396c6559d 100644
--- a/block/bdev.c
+++ b/block/bdev.c
@@ -558,16 +558,19 @@ void bdev_drop(struct block_device *bdev)
iput(BD_INODE(bdev));
}
+static int bdev_pages_count(struct inode *inode, void *priv)
+{
+ long *pages = priv;
+
+ *pages += inode->i_mapping->nrpages;
+ return 0;
+}
+
long nr_blockdev_pages(void)
{
- struct inode *inode;
long ret = 0;
- spin_lock(&blockdev_superblock->s_inode_list_lock);
- list_for_each_entry(inode, &blockdev_superblock->s_inodes, i_sb_list)
- ret += inode->i_mapping->nrpages;
- spin_unlock(&blockdev_superblock->s_inode_list_lock);
-
+ sb_inode_iter_locked(blockdev_superblock, bdev_pages_count, &ret);
return ret;
}
--
2.53.0
^ permalink raw reply related [flat|nested] 14+ messages in thread
* [PATCH v4 04/11] fs/quota: convert remove_dquot_ref() to sb_inode_iter_locked()
2026-11-25 9:11 [PATCH v4 00/11] vfs: add inode iteration methods Huang Shijie
` (2 preceding siblings ...)
2026-11-25 9:11 ` [PATCH v4 03/11] block: convert nr_blockdev_pages() to sb_inode_iter_locked() Huang Shijie
@ 2026-11-25 9:11 ` Huang Shijie
2026-11-25 9:11 ` [PATCH v4 05/11] fs: convert generic_shutdown_super() poison loop Huang Shijie
` (7 subsequent siblings)
11 siblings, 0 replies; 14+ messages in thread
From: Huang Shijie @ 2026-11-25 9:11 UTC (permalink / raw)
To: viro, brauner, jack, hch, agruenba, mic, axboe
Cc: gnoack, paul, jmorris, serge, legion, aleksa, libaokun, ebiggers,
djwong, sandeen, zhongyuan, fangbaoshun, yingzhiwei, wujianyong,
shijie8, linux-fsdevel, linux-kernel, linux-block, gfs2,
linux-security-module, Huang Shijie
Convert remove_dquot_ref() to sb_inode_iter_locked().
No functional change.
Signed-off-by: Huang Shijie <huangsj@hygon.cn>
---
fs/quota/dquot.c | 66 ++++++++++++++++++++++++++++--------------------
1 file changed, 38 insertions(+), 28 deletions(-)
diff --git a/fs/quota/dquot.c b/fs/quota/dquot.c
index a3d503591871..538578c79b5c 100644
--- a/fs/quota/dquot.c
+++ b/fs/quota/dquot.c
@@ -83,6 +83,8 @@
#include <linux/uaccess.h>
+#include "../internal.h"
+
/*
* There are five quota SMP locks:
* * dq_list_lock protects all lists with quotas and quota formats.
@@ -165,6 +167,11 @@ static struct kmem_cache *dquot_cachep;
/* workqueue for work quota_release_work*/
static struct workqueue_struct *quota_unbound_wq;
+struct dquot_ref_data {
+ int type;
+ int reserved;
+};
+
void register_quota_format(struct quota_format_type *fmt)
{
spin_lock(&dq_list_lock);
@@ -1117,41 +1124,44 @@ static int add_dquot_ref(struct super_block *sb, int type)
return err;
}
-static void remove_dquot_ref(struct super_block *sb, int type)
+static int remove_dquot_ref_fn(struct inode *inode, void *priv)
{
- struct inode *inode;
-#ifdef CONFIG_QUOTA_DEBUG
- int reserved = 0;
-#endif
+ struct dquot_ref_data *ref = priv;
- spin_lock(&sb->s_inode_list_lock);
- list_for_each_entry(inode, &sb->s_inodes, i_sb_list) {
- /*
- * We have to scan also I_NEW inodes because they can already
- * have quota pointer initialized. Luckily, we need to touch
- * only quota pointers and these have separate locking
- * (dq_data_lock).
- */
- spin_lock(&dq_data_lock);
- if (!IS_NOQUOTA(inode)) {
- struct dquot __rcu **dquots = i_dquot(inode);
- struct dquot *dquot = srcu_dereference_check(
- dquots[type], &dquot_srcu,
- lockdep_is_held(&dq_data_lock));
+ spin_lock(&dq_data_lock);
+ if (!IS_NOQUOTA(inode)) {
+ struct dquot __rcu **dquots = i_dquot(inode);
+ struct dquot *dquot = srcu_dereference_check(
+ dquots[ref->type], &dquot_srcu,
+ lockdep_is_held(&dq_data_lock));
#ifdef CONFIG_QUOTA_DEBUG
- if (unlikely(inode_get_rsv_space(inode) > 0))
- reserved = 1;
+ if (unlikely(inode_get_rsv_space(inode) > 0))
+ ref->reserved++;
#endif
- rcu_assign_pointer(dquots[type], NULL);
- if (dquot)
- dqput(dquot);
- }
- spin_unlock(&dq_data_lock);
+ rcu_assign_pointer(dquots[ref->type], NULL);
+ if (dquot)
+ dqput(dquot);
}
- spin_unlock(&sb->s_inode_list_lock);
+ spin_unlock(&dq_data_lock);
+ return 0;
+}
+
+static void remove_dquot_ref(struct super_block *sb, int type)
+{
+ struct dquot_ref_data ref = {
+ .type = type,
+ };
+
+ /*
+ * We have to scan also I_NEW inodes because they can already
+ * have quota pointer initialized. Luckily, we need to touch
+ * only quota pointers and these have separate locking
+ * (dq_data_lock).
+ */
+ sb_inode_iter_locked(sb, remove_dquot_ref_fn, &ref);
#ifdef CONFIG_QUOTA_DEBUG
- if (reserved) {
+ if (ref.reserved) {
printk(KERN_WARNING "VFS (%s): Writes happened after quota"
" was disabled thus quota information is probably "
"inconsistent. Please run quotacheck(8).\n", sb->s_id);
--
2.53.0
^ permalink raw reply related [flat|nested] 14+ messages in thread
* [PATCH v4 05/11] fs: convert generic_shutdown_super() poison loop
2026-11-25 9:11 [PATCH v4 00/11] vfs: add inode iteration methods Huang Shijie
` (3 preceding siblings ...)
2026-11-25 9:11 ` [PATCH v4 04/11] fs/quota: convert remove_dquot_ref() " Huang Shijie
@ 2026-11-25 9:11 ` Huang Shijie
2026-11-25 9:11 ` [PATCH v4 06/11] fs/inode: convert evict_inodes() to sb_inode_iter Huang Shijie
` (6 subsequent siblings)
11 siblings, 0 replies; 14+ messages in thread
From: Huang Shijie @ 2026-11-25 9:11 UTC (permalink / raw)
To: viro, brauner, jack, hch, agruenba, mic, axboe
Cc: gnoack, paul, jmorris, serge, legion, aleksa, libaokun, ebiggers,
djwong, sandeen, zhongyuan, fangbaoshun, yingzhiwei, wujianyong,
shijie8, linux-fsdevel, linux-kernel, linux-block, gfs2,
linux-security-module, Huang Shijie
Convert generic_shutdown_super() poison loop with sb_inode_iter_locked().
No functional change.
Signed-off-by: Huang Shijie <huangsj@hygon.cn>
---
fs/super.c | 19 ++++++++++---------
1 file changed, 10 insertions(+), 9 deletions(-)
diff --git a/fs/super.c b/fs/super.c
index 077e772fa19b..bf7fac5ebcbb 100644
--- a/fs/super.c
+++ b/fs/super.c
@@ -780,6 +780,15 @@ void retire_super(struct super_block *sb)
}
EXPORT_SYMBOL(retire_super);
+static int inode_poison_fn(struct inode *inode, void *priv)
+{
+ inode->i_op = VFS_PTR_POISON;
+ inode->i_sb = VFS_PTR_POISON;
+ inode->i_mapping = VFS_PTR_POISON;
+
+ return 0;
+}
+
/**
* generic_shutdown_super - common helper for ->kill_sb()
* @sb: superblock to kill
@@ -838,15 +847,7 @@ void generic_shutdown_super(struct super_block *sb)
* we can at least make it more likely that a later
* iput_final() or such crashes cleanly.
*/
- struct inode *inode;
-
- spin_lock(&sb->s_inode_list_lock);
- list_for_each_entry(inode, &sb->s_inodes, i_sb_list) {
- inode->i_op = VFS_PTR_POISON;
- inode->i_sb = VFS_PTR_POISON;
- inode->i_mapping = VFS_PTR_POISON;
- }
- spin_unlock(&sb->s_inode_list_lock);
+ sb_inode_iter_locked(sb, inode_poison_fn, NULL);
}
}
/*
--
2.53.0
^ permalink raw reply related [flat|nested] 14+ messages in thread
* [PATCH v4 06/11] fs/inode: convert evict_inodes() to sb_inode_iter
2026-11-25 9:11 [PATCH v4 00/11] vfs: add inode iteration methods Huang Shijie
` (4 preceding siblings ...)
2026-11-25 9:11 ` [PATCH v4 05/11] fs: convert generic_shutdown_super() poison loop Huang Shijie
@ 2026-11-25 9:11 ` Huang Shijie
2026-11-25 9:11 ` [PATCH v4 07/11] block: convert sync_bdevs() " Huang Shijie
` (5 subsequent siblings)
11 siblings, 0 replies; 14+ messages in thread
From: Huang Shijie @ 2026-11-25 9:11 UTC (permalink / raw)
To: viro, brauner, jack, hch, agruenba, mic, axboe
Cc: gnoack, paul, jmorris, serge, legion, aleksa, libaokun, ebiggers,
djwong, sandeen, zhongyuan, fangbaoshun, yingzhiwei, wujianyong,
shijie8, linux-fsdevel, linux-kernel, linux-block, gfs2,
linux-security-module, Huang Shijie
Use sb_inode_iter with %INO_ITER_F_NOREF | %INO_ITER_F_NOIGET, which
hands out only zero-count inodes and takes no reference on them.
Signed-off-by: Huang Shijie <huangsj@hygon.cn>
---
fs/inode.c | 33 ++++++++++-----------------------
1 file changed, 10 insertions(+), 23 deletions(-)
diff --git a/fs/inode.c b/fs/inode.c
index f63535ee3629..0830a11a37e9 100644
--- a/fs/inode.c
+++ b/fs/inode.c
@@ -865,6 +865,13 @@ static void dispose_list(struct list_head *head)
}
}
+static bool evict_inode_eligible(struct inode *inode, void *data)
+{
+ inode_state_set(inode, I_FREEING);
+ inode_lru_list_del(inode);
+ return true;
+}
+
/**
* evict_inodes - evict all evictable inodes for a superblock
* @sb: superblock to operate on
@@ -879,40 +886,20 @@ void evict_inodes(struct super_block *sb)
struct inode *inode;
LIST_HEAD(dispose);
- spin_lock(&sb->s_inode_list_lock);
- list_for_each_entry(inode, &sb->s_inodes, i_sb_list) {
- if (icount_read_once(inode))
- continue;
-
- spin_lock(&inode->i_lock);
- if (icount_read(inode)) {
- spin_unlock(&inode->i_lock);
- continue;
- }
- if (inode_state_read(inode) & (I_NEW | I_FREEING | I_WILL_FREE)) {
- spin_unlock(&inode->i_lock);
- continue;
- }
-
- inode_state_set(inode, I_FREEING);
- inode_lru_list_del(inode);
- spin_unlock(&inode->i_lock);
-
+ CLASS(sb_inode_iter, iter)(sb, evict_inode_eligible, &dispose,
+ INO_ITER_F_NOREF | INO_ITER_F_NOIGET);
+ while ((inode = sb_inode_iter_next(&iter))) {
/*
* Keep this inode out of dispose so it stays on s_inodes while
* the list lock is dropped. I_FREEING prevents new references
* and leaves eviction to us, so we can resume the walk from it.
*/
if (need_resched()) {
- spin_unlock(&sb->s_inode_list_lock);
cond_resched();
dispose_list(&dispose);
- spin_lock(&sb->s_inode_list_lock);
}
list_add(&inode->i_lru, &dispose);
}
- spin_unlock(&sb->s_inode_list_lock);
-
dispose_list(&dispose);
}
EXPORT_SYMBOL_GPL(evict_inodes);
--
2.53.0
^ permalink raw reply related [flat|nested] 14+ messages in thread
* [PATCH v4 07/11] block: convert sync_bdevs() to sb_inode_iter
2026-11-25 9:11 [PATCH v4 00/11] vfs: add inode iteration methods Huang Shijie
` (5 preceding siblings ...)
2026-11-25 9:11 ` [PATCH v4 06/11] fs/inode: convert evict_inodes() to sb_inode_iter Huang Shijie
@ 2026-11-25 9:11 ` Huang Shijie
2026-11-25 9:11 ` [PATCH v4 08/11] fs/drop_caches: convert drop_pagecache_sb() " Huang Shijie
` (4 subsequent siblings)
11 siblings, 0 replies; 14+ messages in thread
From: Huang Shijie @ 2026-11-25 9:11 UTC (permalink / raw)
To: viro, brauner, jack, hch, agruenba, mic, axboe
Cc: gnoack, paul, jmorris, serge, legion, aleksa, libaokun, ebiggers,
djwong, sandeen, zhongyuan, fangbaoshun, yingzhiwei, wujianyong,
shijie8, linux-fsdevel, linux-kernel, linux-block, gfs2,
linux-security-module, Huang Shijie
Convert sync_bdevs() to sb_inode_iter.
No functional change.
Signed-off-by: Huang Shijie <huangsj@hygon.cn>
---
block/bdev.c | 43 +++++++++++--------------------------------
1 file changed, 11 insertions(+), 32 deletions(-)
diff --git a/block/bdev.c b/block/bdev.c
index 938396c6559d..5b399fb7cd63 100644
--- a/block/bdev.c
+++ b/block/bdev.c
@@ -1339,44 +1339,27 @@ void bdev_mark_dead(struct block_device *bdev, bool surprise)
*/
EXPORT_SYMBOL_GPL(bdev_mark_dead);
+static bool sync_bdev_eligible(struct inode *inode, void *priv)
+{
+ return inode->i_mapping->nrpages != 0;
+}
+
void sync_bdevs(bool wait)
{
- struct inode *inode, *old_inode = NULL;
+ struct inode *inode;
- spin_lock(&blockdev_superblock->s_inode_list_lock);
- list_for_each_entry(inode, &blockdev_superblock->s_inodes, i_sb_list) {
- struct address_space *mapping = inode->i_mapping;
- struct block_device *bdev;
+ CLASS(sb_inode_iter, iter)(blockdev_superblock, sync_bdev_eligible,
+ NULL, 0);
- spin_lock(&inode->i_lock);
- if (inode_state_read(inode) & (I_FREEING | I_WILL_FREE | I_NEW) ||
- mapping->nrpages == 0) {
- spin_unlock(&inode->i_lock);
- continue;
- }
- __iget(inode);
- spin_unlock(&inode->i_lock);
- spin_unlock(&blockdev_superblock->s_inode_list_lock);
- /*
- * We hold a reference to 'inode' so it couldn't have been
- * removed from s_inodes list while we dropped the
- * s_inode_list_lock We cannot iput the inode now as we can
- * be holding the last reference and we cannot iput it under
- * s_inode_list_lock. So we keep the reference and iput it
- * later.
- */
- iput(old_inode);
- old_inode = inode;
- bdev = I_BDEV(inode);
+ while ((inode = sb_inode_iter_next(&iter))) {
+ struct block_device *bdev = I_BDEV(inode);
/*
* The inode does not keep the gendisk of a partition alive, the
* device does.
*/
- if (!kobject_get_unless_zero(&bdev->bd_device.kobj)) {
- spin_lock(&blockdev_superblock->s_inode_list_lock);
+ if (!kobject_get_unless_zero(&bdev->bd_device.kobj))
continue;
- }
mutex_lock(&bdev->bd_disk->open_mutex);
if (!atomic_read(&bdev->bd_openers)) {
@@ -1394,11 +1377,7 @@ void sync_bdevs(bool wait)
}
mutex_unlock(&bdev->bd_disk->open_mutex);
put_device(&bdev->bd_device);
-
- spin_lock(&blockdev_superblock->s_inode_list_lock);
}
- spin_unlock(&blockdev_superblock->s_inode_list_lock);
- iput(old_inode);
}
/*
--
2.53.0
^ permalink raw reply related [flat|nested] 14+ messages in thread
* [PATCH v4 08/11] fs/drop_caches: convert drop_pagecache_sb() to sb_inode_iter
2026-11-25 9:11 [PATCH v4 00/11] vfs: add inode iteration methods Huang Shijie
` (6 preceding siblings ...)
2026-11-25 9:11 ` [PATCH v4 07/11] block: convert sync_bdevs() " Huang Shijie
@ 2026-11-25 9:11 ` Huang Shijie
2026-11-25 9:11 ` [PATCH v4 09/11] fs/quota: convert add_dquot_ref() " Huang Shijie
` (3 subsequent siblings)
11 siblings, 0 replies; 14+ messages in thread
From: Huang Shijie @ 2026-11-25 9:11 UTC (permalink / raw)
To: viro, brauner, jack, hch, agruenba, mic, axboe
Cc: gnoack, paul, jmorris, serge, legion, aleksa, libaokun, ebiggers,
djwong, sandeen, zhongyuan, fangbaoshun, yingzhiwei, wujianyong,
shijie8, linux-fsdevel, linux-kernel, linux-block, gfs2,
linux-security-module, Huang Shijie
Convert drop_pagecache_sb() to sb_inode_iter.
No functional change.
Signed-off-by: Huang Shijie <huangsj@hygon.cn>
---
fs/drop_caches.c | 36 ++++++++++++------------------------
1 file changed, 12 insertions(+), 24 deletions(-)
diff --git a/fs/drop_caches.c b/fs/drop_caches.c
index 28c5fb9c7357..bfe9b8cbb45c 100644
--- a/fs/drop_caches.c
+++ b/fs/drop_caches.c
@@ -17,39 +17,27 @@
/* A global variable is a bit ugly, but it keeps the code simple */
static int sysctl_drop_caches;
+static bool invalidate_inode_eligible(struct inode *inode, void *priv)
+{
+ /*
+ * Skip inodes without pages, unless we need to reschedule: then take
+ * one anyway so that a long scan of empty inodes cannot stall.
+ */
+ return !mapping_empty(inode->i_mapping) || need_resched();
+}
+
static void drop_pagecache_sb(struct super_block *sb, void *unused)
{
- struct inode *inode, *toput_inode = NULL;
+ struct inode *inode;
if (sb->s_bdi == &noop_backing_dev_info)
return;
- spin_lock(&sb->s_inode_list_lock);
- list_for_each_entry(inode, &sb->s_inodes, i_sb_list) {
- spin_lock(&inode->i_lock);
- /*
- * We must skip inodes in unusual state. We may also skip
- * inodes without pages but we deliberately won't in case
- * we need to reschedule to avoid softlockups.
- */
- if ((inode_state_read(inode) & (I_FREEING | I_WILL_FREE | I_NEW)) ||
- (mapping_empty(inode->i_mapping) && !need_resched())) {
- spin_unlock(&inode->i_lock);
- continue;
- }
- __iget(inode);
- spin_unlock(&inode->i_lock);
- spin_unlock(&sb->s_inode_list_lock);
-
+ CLASS(sb_inode_iter, iter)(sb, invalidate_inode_eligible, NULL, 0);
+ while ((inode = sb_inode_iter_next(&iter))) {
invalidate_mapping_pages(inode->i_mapping, 0, -1);
- iput(toput_inode);
- toput_inode = inode;
-
cond_resched();
- spin_lock(&sb->s_inode_list_lock);
}
- spin_unlock(&sb->s_inode_list_lock);
- iput(toput_inode);
}
static int drop_caches_sysctl_handler(const struct ctl_table *table, int write,
--
2.53.0
^ permalink raw reply related [flat|nested] 14+ messages in thread
* [PATCH v4 09/11] fs/quota: convert add_dquot_ref() to sb_inode_iter
2026-11-25 9:11 [PATCH v4 00/11] vfs: add inode iteration methods Huang Shijie
` (7 preceding siblings ...)
2026-11-25 9:11 ` [PATCH v4 08/11] fs/drop_caches: convert drop_pagecache_sb() " Huang Shijie
@ 2026-11-25 9:11 ` Huang Shijie
2026-11-25 9:11 ` [PATCH v4 10/11] landlock: convert hook_sb_delete() " Huang Shijie
` (2 subsequent siblings)
11 siblings, 0 replies; 14+ messages in thread
From: Huang Shijie @ 2026-11-25 9:11 UTC (permalink / raw)
To: viro, brauner, jack, hch, agruenba, mic, axboe
Cc: gnoack, paul, jmorris, serge, legion, aleksa, libaokun, ebiggers,
djwong, sandeen, zhongyuan, fangbaoshun, yingzhiwei, wujianyong,
shijie8, linux-fsdevel, linux-kernel, linux-block, gfs2,
linux-security-module, Huang Shijie
Convert add_dquot_ref() to sb_inode_iter.
No functional change.
Signed-off-by: Huang Shijie <huangsj@hygon.cn>
---
fs/quota/dquot.c | 54 ++++++++++++++++--------------------------------
1 file changed, 18 insertions(+), 36 deletions(-)
diff --git a/fs/quota/dquot.c b/fs/quota/dquot.c
index 538578c79b5c..d047a3353b79 100644
--- a/fs/quota/dquot.c
+++ b/fs/quota/dquot.c
@@ -1066,56 +1066,38 @@ static int dqinit_needed(struct inode *inode, int type)
return 0;
}
+static bool add_dquot_ref_eligible(struct inode *inode, void *priv)
+{
+ struct dquot_ref_data *ref = priv;
+
+ return atomic_read(&inode->i_writecount) &&
+ dqinit_needed(inode, ref->type);
+}
+
/* This routine is guarded by s_umount semaphore */
static int add_dquot_ref(struct super_block *sb, int type)
{
- struct inode *inode, *old_inode = NULL;
-#ifdef CONFIG_QUOTA_DEBUG
- int reserved = 0;
-#endif
+ struct dquot_ref_data ref = {
+ .type = type,
+ };
+ struct inode *inode;
int err = 0;
- spin_lock(&sb->s_inode_list_lock);
- list_for_each_entry(inode, &sb->s_inodes, i_sb_list) {
- spin_lock(&inode->i_lock);
- if ((inode_state_read(inode) & (I_FREEING | I_WILL_FREE | I_NEW)) ||
- !atomic_read(&inode->i_writecount) ||
- !dqinit_needed(inode, type)) {
- spin_unlock(&inode->i_lock);
- continue;
- }
- __iget(inode);
- spin_unlock(&inode->i_lock);
- spin_unlock(&sb->s_inode_list_lock);
-
+ CLASS(sb_inode_iter, iter)(sb, add_dquot_ref_eligible, &ref, 0);
+ while ((inode = sb_inode_iter_next(&iter))) {
#ifdef CONFIG_QUOTA_DEBUG
if (unlikely(inode_get_rsv_space(inode) > 0))
- reserved = 1;
+ ref.reserved++;
#endif
- iput(old_inode);
err = __dquot_initialize(inode, type);
if (err) {
- iput(inode);
- goto out;
+ iter.status = err;
+ break;
}
-
- /*
- * We hold a reference to 'inode' so it couldn't have been
- * removed from s_inodes list while we dropped the
- * s_inode_list_lock. We cannot iput the inode now as we can be
- * holding the last reference and we cannot iput it under
- * s_inode_list_lock. So we keep the reference and iput it
- * later.
- */
- old_inode = inode;
cond_resched();
- spin_lock(&sb->s_inode_list_lock);
}
- spin_unlock(&sb->s_inode_list_lock);
- iput(old_inode);
-out:
#ifdef CONFIG_QUOTA_DEBUG
- if (reserved) {
+ if (ref.reserved) {
quota_error(sb, "Writes happened before quota was turned on "
"thus quota information is probably inconsistent. "
"Please run quotacheck(8)");
--
2.53.0
^ permalink raw reply related [flat|nested] 14+ messages in thread
* [PATCH v4 10/11] landlock: convert hook_sb_delete() to sb_inode_iter
2026-11-25 9:11 [PATCH v4 00/11] vfs: add inode iteration methods Huang Shijie
` (8 preceding siblings ...)
2026-11-25 9:11 ` [PATCH v4 09/11] fs/quota: convert add_dquot_ref() " Huang Shijie
@ 2026-11-25 9:11 ` Huang Shijie
2026-11-25 9:11 ` [PATCH v4 11/11] gfs2: convert gfs2_evict_inodes() " Huang Shijie
2026-11-25 22:25 ` [PATCH v4 00/11] vfs: add inode iteration methods Andreas Gruenbacher
11 siblings, 0 replies; 14+ messages in thread
From: Huang Shijie @ 2026-11-25 9:11 UTC (permalink / raw)
To: viro, brauner, jack, hch, agruenba, mic, axboe
Cc: gnoack, paul, jmorris, serge, legion, aleksa, libaokun, ebiggers,
djwong, sandeen, zhongyuan, fangbaoshun, yingzhiwei, wujianyong,
shijie8, linux-fsdevel, linux-kernel, linux-block, gfs2,
linux-security-module, Huang Shijie
Convert hook_sb_delete() to sb_inode_iter.
No functional change.
Signed-off-by: Huang Shijie <huangsj@hygon.cn>
---
security/landlock/fs.c | 67 ++++++++++--------------------------------
1 file changed, 16 insertions(+), 51 deletions(-)
diff --git a/security/landlock/fs.c b/security/landlock/fs.c
index 2ab67bcad198..36e22be99acc 100644
--- a/security/landlock/fs.c
+++ b/security/landlock/fs.c
@@ -1380,49 +1380,35 @@ static void hook_inode_free_security_rcu(void *inode_security)
*
* Cf. fsnotify_unmount_inodes() and evict_inodes()
*/
+static bool release_inode_eligible(struct inode *inode, void *priv)
+{
+ bool ret;
+
+ rcu_read_lock();
+ ret = !!rcu_dereference(landlock_inode(inode)->object);
+ rcu_read_unlock();
+
+ return ret;
+}
+
static void hook_sb_delete(struct super_block *const sb)
{
- struct inode *inode, *prev_inode = NULL;
+ struct inode *inode;
if (!landlock_initialized)
return;
- spin_lock(&sb->s_inode_list_lock);
- list_for_each_entry(inode, &sb->s_inodes, i_sb_list) {
+ CLASS(sb_inode_iter, iter)(sb, release_inode_eligible, NULL,
+ INO_ITER_F_REFERENCED);
+ while ((inode = sb_inode_iter_next(&iter))) {
struct landlock_object *object;
- /* Only handles referenced inodes. */
- if (!icount_read_once(inode))
- continue;
-
- /*
- * Protects against concurrent modification of inode (e.g.
- * from get_inode_object()).
- */
- spin_lock(&inode->i_lock);
- /*
- * Checks I_FREEING and I_WILL_FREE to protect against a race
- * condition when release_inode() just called iput(), which
- * could lead to a NULL dereference of inode->security or a
- * second call to iput() for the same Landlock object. Also
- * checks I_NEW because such inode cannot be tied to an object.
- */
- if (inode_state_read(inode) &
- (I_FREEING | I_WILL_FREE | I_NEW)) {
- spin_unlock(&inode->i_lock);
- continue;
- }
-
rcu_read_lock();
object = rcu_dereference(landlock_inode(inode)->object);
if (!object) {
rcu_read_unlock();
- spin_unlock(&inode->i_lock);
continue;
}
- /* Keeps a reference to this inode until the next loop walk. */
- __iget(inode);
- spin_unlock(&inode->i_lock);
/*
* If there is no concurrent release_inode() ongoing, then we
@@ -1455,30 +1441,9 @@ static void hook_sb_delete(struct super_block *const sb)
spin_unlock(&object->lock);
rcu_read_unlock();
}
-
- if (prev_inode) {
- /*
- * At this point, we still own the __iget() reference
- * that we just set in this loop walk. Therefore we
- * can drop the list lock and know that the inode won't
- * disappear from under us until the next loop walk.
- */
- spin_unlock(&sb->s_inode_list_lock);
- /*
- * We can now actually put the inode reference from the
- * previous loop walk, which is not needed anymore.
- */
- iput(prev_inode);
- cond_resched();
- spin_lock(&sb->s_inode_list_lock);
- }
- prev_inode = inode;
+ cond_resched();
}
- spin_unlock(&sb->s_inode_list_lock);
- /* Puts the inode reference from the last loop walk, if any. */
- if (prev_inode)
- iput(prev_inode);
/* Waits for pending iput() in release_inode(). */
wait_var_event(&landlock_superblock(sb)->inode_refs,
!atomic_long_read(&landlock_superblock(sb)->inode_refs));
--
2.53.0
^ permalink raw reply related [flat|nested] 14+ messages in thread
* [PATCH v4 11/11] gfs2: convert gfs2_evict_inodes() to sb_inode_iter
2026-11-25 9:11 [PATCH v4 00/11] vfs: add inode iteration methods Huang Shijie
` (9 preceding siblings ...)
2026-11-25 9:11 ` [PATCH v4 10/11] landlock: convert hook_sb_delete() " Huang Shijie
@ 2026-11-25 9:11 ` Huang Shijie
2026-11-25 22:25 ` [PATCH v4 00/11] vfs: add inode iteration methods Andreas Gruenbacher
11 siblings, 0 replies; 14+ messages in thread
From: Huang Shijie @ 2026-11-25 9:11 UTC (permalink / raw)
To: viro, brauner, jack, hch, agruenba, mic, axboe
Cc: gnoack, paul, jmorris, serge, legion, aleksa, libaokun, ebiggers,
djwong, sandeen, zhongyuan, fangbaoshun, yingzhiwei, wujianyong,
shijie8, linux-fsdevel, linux-kernel, linux-block, gfs2,
linux-security-module, Huang Shijie
Convert gfs2_evict_inodes to sb_inode_iter.
The original code also handed out an inode which was already being
torn down when need_resched() was set. That is not preserved: taking a
reference on an I_FREEING inode whose count has already reached zero
lets a second iput() run on it once the reference is dropped again.
Signed-off-by: Huang Shijie <huangsj@hygon.cn>
---
fs/gfs2/ops_fstype.c | 26 ++++++--------------------
1 file changed, 6 insertions(+), 20 deletions(-)
diff --git a/fs/gfs2/ops_fstype.c b/fs/gfs2/ops_fstype.c
index 6ec47376aecf..130b116627a3 100644
--- a/fs/gfs2/ops_fstype.c
+++ b/fs/gfs2/ops_fstype.c
@@ -1739,31 +1739,17 @@ static int gfs2_meta_init_fs_context(struct fs_context *fc)
*/
static void gfs2_evict_inodes(struct super_block *sb)
{
- struct inode *inode, *toput_inode = NULL;
struct gfs2_sbd *sdp = sb->s_fs_info;
set_bit(SDF_EVICTING, &sdp->sd_flags);
- spin_lock(&sb->s_inode_list_lock);
- list_for_each_entry(inode, &sb->s_inodes, i_sb_list) {
- spin_lock(&inode->i_lock);
- if ((inode_state_read(inode) & (I_FREEING | I_WILL_FREE | I_NEW)) &&
- !need_resched()) {
- spin_unlock(&inode->i_lock);
- continue;
- }
- __iget(inode);
- spin_unlock(&inode->i_lock);
- spin_unlock(&sb->s_inode_list_lock);
-
- iput(toput_inode);
- toput_inode = inode;
-
+ /*
+ * Nothing to do with each inode: we only want to bounce it through
+ * iput() so that it is evicted while the list lock is dropped.
+ */
+ CLASS(sb_inode_iter, iter)(sb, NULL, NULL, 0);
+ while (sb_inode_iter_next(&iter))
cond_resched();
- spin_lock(&sb->s_inode_list_lock);
- }
- spin_unlock(&sb->s_inode_list_lock);
- iput(toput_inode);
}
static void gfs2_kill_sb(struct super_block *sb)
--
2.53.0
^ permalink raw reply related [flat|nested] 14+ messages in thread
* Re: [PATCH v4 00/11] vfs: add inode iteration methods
2026-11-25 9:11 [PATCH v4 00/11] vfs: add inode iteration methods Huang Shijie
` (10 preceding siblings ...)
2026-11-25 9:11 ` [PATCH v4 11/11] gfs2: convert gfs2_evict_inodes() " Huang Shijie
@ 2026-11-25 22:25 ` Andreas Gruenbacher
2026-11-26 12:15 ` Huang Shijie
11 siblings, 1 reply; 14+ messages in thread
From: Andreas Gruenbacher @ 2026-11-25 22:25 UTC (permalink / raw)
To: Huang Shijie
Cc: viro, brauner, jack, hch, mic, axboe, gnoack, paul, jmorris,
serge, legion, aleksa, libaokun, ebiggers, djwong, sandeen,
zhongyuan, fangbaoshun, yingzhiwei, wujianyong, shijie8,
linux-fsdevel, linux-kernel, linux-block, gfs2,
linux-security-module
Huang,
I have some thoughts ...
On Wed, 25 Nov 2026 at 10:18, Huang Shijie <huangsj@hygon.cn> wrote:
> This series adds a superblock inode iterator and converts the open-coded
> sb->s_inodes walks in the VFS to it. The design follows the cursor-based
> construct proposed by Christian Brauner:
>
> https://lore.kernel.org/all/20261120-mitverantwortlich-drehbaren-kantholz-241300d20168@brauner/
>
> and is the same direction as Dave Chinner's patch set:
>
> "vfs: improving inode cache iteration scalability"
> https://lore.kernel.org/all/20241002014017.3801899-1-david@fromorbit.com/
>
> The caller owns the iterator state and drives the walk itself:
>
> CLASS(sb_inode_iter, iter)(sb, eligible, priv, flags);
> while ((inode = sb_inode_iter_next(&iter)))
> /* do stuff */;
The pattern should be:
for (CLASS(...); ...; ...) {
/* ... */
if (/* end iteration early */)
break;
/* ... */
}
so that the iterator goes out of scope immediately when the loop ends.
Hide the details of the loop header in a macro and that this becomes:
for_each_inode(inode, sb) {
/* inode is referenced and unlocked here */
}
Use that to convert the simple cases.
And then we've got preconditions that are checked while the inode is
locked. Those can probably go in an expression:
for_each_inode(inode, sb, expression) {
/* expression was true */
/* inode is referenced and unlocked here */
}
This unfortunately cannot be implemented on top of the
sb_inode_iter_next() pattern, at least not in a reasonable way, so we
actually shouldn't use that pattern.
The locked iterator would look like this:
for_each_inode_locked(inode, sb) {
/* inode is locked here */
}
This doesn't cover all existing cases, but that's okay. Don't force
things into this pattern that are not a match.
We can always extend things further if it makes sense.
> The lock does not span iterations. sb_inode_iter_next() takes
> sb->s_inode_list_lock, takes a reference on the next inode, drops the lock
> again and hands the inode out; the reference is what keeps the cursor on
> sb->s_inodes so that the next call can resume from it. As the iterator
> is a cleanup class, a caller that breaks out early does not have to call
> sb_inode_iter_end() explicitly.
>
> Two optional channels let a caller keep its own state instead of filling in
> a shared context:
>
> - iter->eligible is called with inode->i_lock held, so that a caller can
> reject an inode before a reference is paid for.
>
> - iter->status carries an error out of the walk. Setting it to a
> negative value makes sb_inode_iter_next() return NULL.
Nobody needs that. Just break out of the loop.
> Most callers just want a referenced inode and use no flags at all. Three
> flags cover the walks that need something else:
>
> - %INO_ITER_F_REFERENCED restricts the walk to inodes that still have a
> reference, preserving the "only handle referenced inodes" rule of
> landlock's sb-delete walk.
>
> - %INO_ITER_F_NOREF hands out only inodes whose reference count has
> already reached zero, and %INO_ITER_F_NOIGET suppresses the reference.
> Neither keeps the inode alive, so the caller has to make the inode
> unclaimable itself.
No flags.
> A second, internal iterator covers the walks that must not take a
> reference at all:
>
> static inline int sb_inode_iter_locked(struct super_block *sb,
> int (*iter_fn)(struct inode *,
> void *),
> void *priv);
>
> - It keeps sb->s_inode_list_lock held for the whole walk and takes no
> inode reference, so @iter_fn only has an existence guarantee: it must
> not block, and it must do its own inode state checks and its own
> reference handling. A callback returns a negative value to stop the
> walk with that error, and 0 to continue.
>
> - It lives in fs/internal.h as a static inline rather than being
> exported: it is a VFS internal facility, and the small callbacks
> inline into their callers.
>
> It serves the three walks that cannot use sb_inode_iter_next():
>
> - nr_blockdev_pages() is reached from si_meminfo(), which kdb calls, and
> the iput() that the referenced iterator would do can sleep.
>
> - remove_dquot_ref() has to scan I_NEW inodes as well, which the
> referenced iterator filters out; it only touches the quota pointers,
> and those have their own locking in dq_data_lock.
>
> - the generic_shutdown_super() poisoning loop has to touch every inode
> still on the list, including the I_NEW, I_FREEING and I_WILL_FREE
> ones that sb_inode_iter_next() filters out, and it must not bounce
> them through __iget()/iput(): the loop overwrites i_sb with
> VFS_PTR_POISON, and evict() dereferences i_sb->s_op on the way out.
>
> Patch 1 adds sb_inode_iter_next().
> Patch 2 adds sb_inode_iter_locked().
> Patch 3-5 convert the callers which cannot use referenced inodes
> (nr_blockdev_pages(), dquot removal and the
> generic_shutdown_super() poisoning loop).
> Patch 6-11 convert the remaining callers (inode eviction, sync_bdevs,
> drop_pagecache_sb, add_dquot_ref, landlock's hook_sb_delete
> and gfs2_evict_inodes()).
>
>
> BTW:
> This series is based on v7.4-rc2, and applies on top of the last commit
> of that base, which is the small patch this series depends on:
>
> v7.4-rc2-22-ga02161f70fcf
> commit a02161f70fcf ("fs/drop_caches: skip filesystems without page cache writeback")
>
> That patch was posted as:
> https://lore.kernel.org/all/20260923070116.3226846-1-huangsj@hygon.cn/
>
> v3-->v4:
> -the interface is redone as a caller-owned cursor, following Christian's
> sb_inode_iter construct: sb_inode_iter_next() returns a referenced
> inode with sb->s_inode_list_lock dropped, and the reference keeps the
> cursor on sb->s_inodes. The callback-based interface of v3, with its
> ino_iter_fn and INO_ITER_RET_* return values, is gone.
>
> -the iterator is wrapped in a cleanup class, so a caller that breaks out
> of the walk does not have to call sb_inode_iter_end().
>
> -an inode can be rejected before a reference is paid for: @eligible is
> called with inode->i_lock held.
>
> -the error channel is now iter->status rather than a three-valued
> callback return.
>
> -super_iter_inodes() and super_iter_inodes_locked() are renamed to
> sb_inode_iter_next() and sb_inode_iter_locked(), and the latter stays
> in fs/internal.h for the three walks that must not take a reference.
>
> -evict_inodes() uses INO_ITER_F_NOREF | INO_ITER_F_NOIGET: it only
> takes inodes whose reference count has reached zero, pins each one by
> setting I_FREEING and dropping it from the LRU, and runs dispose_list()
> with the list lock dropped.
>
> v3: https://lore.kernel.org/all/20261120031305.3561045-1-huangsj@hygon.cn/
> v2: https://lore.kernel.org/all/20261113115702.586594-1-huangsj@hygon.cn/
> v1: https://lore.kernel.org/all/20261015093854.2082600-1-huangsj@hygon.cn/
>
> Huang Shijie (11):
> vfs: add sb_inode_iter_next()
> vfs: add sb_inode_iter_locked()
> block: convert nr_blockdev_pages() to sb_inode_iter_locked()
> fs/quota: convert remove_dquot_ref() to sb_inode_iter_locked()
> fs: convert generic_shutdown_super() poison loop
> fs/inode: convert evict_inodes() to sb_inode_iter
> block: convert sync_bdevs() to sb_inode_iter
> fs/drop_caches: convert drop_pagecache_sb() to sb_inode_iter
> fs/quota: convert add_dquot_ref() to sb_inode_iter
> landlock: convert hook_sb_delete() to sb_inode_iter
> gfs2: convert gfs2_evict_inodes() to sb_inode_iter
>
> block/bdev.c | 58 ++++++----------
> fs/drop_caches.c | 36 ++++------
> fs/gfs2/ops_fstype.c | 26 ++-----
> fs/inode.c | 33 +++------
> fs/internal.h | 25 +++++++
> fs/quota/dquot.c | 120 +++++++++++++++-----------------
> fs/super.c | 121 ++++++++++++++++++++++++++++++---
> include/linux/fs/super_types.h | 40 +++++++++++
> security/landlock/fs.c | 67 +++++-------------
> 9 files changed, 297 insertions(+), 229 deletions(-)
>
> --
> 2.53.0
>
>
>
>
^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: [PATCH v4 00/11] vfs: add inode iteration methods
2026-11-25 22:25 ` [PATCH v4 00/11] vfs: add inode iteration methods Andreas Gruenbacher
@ 2026-11-26 12:15 ` Huang Shijie
0 siblings, 0 replies; 14+ messages in thread
From: Huang Shijie @ 2026-11-26 12:15 UTC (permalink / raw)
To: Andreas Gruenbacher
Cc: viro, brauner, jack, hch, mic, axboe, gnoack, paul, jmorris,
serge, legion, aleksa, libaokun, ebiggers, djwong, sandeen,
zhongyuan, fangbaoshun, yingzhiwei, wujianyong, shijie8,
linux-fsdevel, linux-kernel, linux-block, gfs2,
linux-security-module
在 Wed, Nov 25, 2026 at 11:25:12PM +0100,Andreas Gruenbacher 写道:
> Huang,
>
> I have some thoughts ...
>
> On Wed, 25 Nov 2026 at 10:18, Huang Shijie <huangsj@hygon.cn> wrote:
> > This series adds a superblock inode iterator and converts the open-coded
> > sb->s_inodes walks in the VFS to it. The design follows the cursor-based
> > construct proposed by Christian Brauner:
> >
> > https://lore.kernel.org/all/20261120-mitverantwortlich-drehbaren-kantholz-241300d20168@brauner/
> >
> > and is the same direction as Dave Chinner's patch set:
> >
> > "vfs: improving inode cache iteration scalability"
> > https://lore.kernel.org/all/20241002014017.3801899-1-david@fromorbit.com/
> >
> > The caller owns the iterator state and drives the walk itself:
> >
> > CLASS(sb_inode_iter, iter)(sb, eligible, priv, flags);
> > while ((inode = sb_inode_iter_next(&iter)))
> > /* do stuff */;
>
> The pattern should be:
>
> for (CLASS(...); ...; ...) {
> /* ... */
> if (/* end iteration early */)
> break;
> /* ... */
> }
>
> so that the iterator goes out of scope immediately when the loop ends.
> Hide the details of the loop header in a macro and that this becomes:
>
> for_each_inode(inode, sb) {
The fs/jffs2/build.c has used this for_each_inode.
Maybe change to for_each_sb_inode?
> /* inode is referenced and unlocked here */
> }
>
> Use that to convert the simple cases.
>
> And then we've got preconditions that are checked while the inode is
> locked. Those can probably go in an expression:
>
> for_each_inode(inode, sb, expression) {
We still need add flags here.
In evict_inodes() and hook_sb_delete(), only the expression is not enough.
How about add a raw code like this:
for_each_inode_raw(inode, sb, expression, flags) {
}
And add a macro for_each_inode:
#define for_each_inode(inode, sb, expression) for_each_inode_raw(inode, sb, expression, 0)
Most of the cases, we can use the for_each_inode.
But in the evict_inodes() and hook_sb_delete() we can use the for_each_inode_raw.
> /* expression was true */
> /* inode is referenced and unlocked here */
> }
>
> This unfortunately cannot be implemented on top of the
> sb_inode_iter_next() pattern, at least not in a reasonable way, so we
> actually shouldn't use that pattern.
>
> The locked iterator would look like this:
>
> for_each_inode_locked(inode, sb) {
I tried this way today.
The macro for_each_inode_locked is complicated.
When I tried to add more hooks to super_operations to implement the
procfs issue, the for_each_inode_locked becomes more complicated.
BTW: the procfs issue see the link:
https://lore.kernel.org/all/20260920072811.2064247-1-huangsj@hygon.cn/
> /* inode is locked here */
> }
>
> This doesn't cover all existing cases, but that's okay. Don't force
> things into this pattern that are not a match.
>
> We can always extend things further if it makes sense.
>
> > The lock does not span iterations. sb_inode_iter_next() takes
> > sb->s_inode_list_lock, takes a reference on the next inode, drops the lock
> > again and hands the inode out; the reference is what keeps the cursor on
> > sb->s_inodes so that the next call can resume from it. As the iterator
> > is a cleanup class, a caller that breaks out early does not have to call
> > sb_inode_iter_end() explicitly.
> >
> > Two optional channels let a caller keep its own state instead of filling in
> > a shared context:
> >
> > - iter->eligible is called with inode->i_lock held, so that a caller can
> > reject an inode before a reference is paid for.
> >
> > - iter->status carries an error out of the walk. Setting it to a
> > negative value makes sb_inode_iter_next() return NULL.
>
> Nobody needs that. Just break out of the loop.
>
> > Most callers just want a referenced inode and use no flags at all. Three
> > flags cover the walks that need something else:
> >
> > - %INO_ITER_F_REFERENCED restricts the walk to inodes that still have a
> > reference, preserving the "only handle referenced inodes" rule of
> > landlock's sb-delete walk.
> >
> > - %INO_ITER_F_NOREF hands out only inodes whose reference count has
> > already reached zero, and %INO_ITER_F_NOIGET suppresses the reference.
> > Neither keeps the inode alive, so the caller has to make the inode
> > unclaimable itself.
>
> No flags.
If we do not add flags, we cannot convert the evict_inodes() and hook_sb_delete().
We'd better convert evict_inodes/hook_sb_delete this time, and then
we can add more hooks to super_operations{}, such as sb_add_inode/sb_del_inode/sb_iter_inode.
With these hooks in super_operations, we can add more optimization code for different FSs.
For example:
"vfs: improving inode cache iteration scalability"
https://lore.kernel.org/all/20241002014017.3801899-1-david@fromorbit.com/
and
https://lore.kernel.org/all/20260920072811.2064247-1-huangsj@hygon.cn/
Thanks
Huang Shijie
^ permalink raw reply [flat|nested] 14+ messages in thread
end of thread, other threads:[~2026-11-26 12:15 UTC | newest]
Thread overview: 14+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-25 9:11 [PATCH v4 00/11] vfs: add inode iteration methods Huang Shijie
2026-11-25 9:11 ` [PATCH v4 01/11] vfs: add sb_inode_iter_next() Huang Shijie
2026-11-25 9:11 ` [PATCH v4 02/11] vfs: add sb_inode_iter_locked() Huang Shijie
2026-11-25 9:11 ` [PATCH v4 03/11] block: convert nr_blockdev_pages() to sb_inode_iter_locked() Huang Shijie
2026-11-25 9:11 ` [PATCH v4 04/11] fs/quota: convert remove_dquot_ref() " Huang Shijie
2026-11-25 9:11 ` [PATCH v4 05/11] fs: convert generic_shutdown_super() poison loop Huang Shijie
2026-11-25 9:11 ` [PATCH v4 06/11] fs/inode: convert evict_inodes() to sb_inode_iter Huang Shijie
2026-11-25 9:11 ` [PATCH v4 07/11] block: convert sync_bdevs() " Huang Shijie
2026-11-25 9:11 ` [PATCH v4 08/11] fs/drop_caches: convert drop_pagecache_sb() " Huang Shijie
2026-11-25 9:11 ` [PATCH v4 09/11] fs/quota: convert add_dquot_ref() " Huang Shijie
2026-11-25 9:11 ` [PATCH v4 10/11] landlock: convert hook_sb_delete() " Huang Shijie
2026-11-25 9:11 ` [PATCH v4 11/11] gfs2: convert gfs2_evict_inodes() " Huang Shijie
2026-11-25 22:25 ` [PATCH v4 00/11] vfs: add inode iteration methods Andreas Gruenbacher
2026-11-26 12:15 ` Huang Shijie
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox