* [PATCH v3 01/11] vfs: add super_iter_inodes()
2026-11-20 3:12 [PATCH v3 0/11] vfs: add inode iteration methods Huang Shijie
@ 2026-11-20 3:12 ` Huang Shijie
2026-11-20 14:56 ` Andreas Gruenbacher
2026-11-20 3:12 ` [PATCH v3 02/11] vfs: add super_iter_inodes_locked() Huang Shijie
` (10 subsequent siblings)
11 siblings, 1 reply; 15+ messages in thread
From: Huang Shijie @ 2026-11-20 3:12 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
Iterating the cached inodes of a superblock is open coded in the
kernel. Add super_iter_inodes() for the callers which can work without
holding the sb->s_inode_list_lock.
For super_iter_inodes():
-- By default, it runs @iter_fn with a referenced inode and with
sb->s_inode_list_lock dropped, so the callback is free to block and to
do whatever it wants with the inode except drop the reference the
iterator holds.
-- Three flags change the default behaviour.
INO_ITER_F_REFERENCED restricts the walk to inodes which still have a
reference.
INO_ITER_F_NOREF does the opposite: only inodes whose reference count
has already reached zero are handed out.
INO_ITER_F_NOIGET tells the iterator do not call __iget for the inode.
Used with INO_ITER_F_NOREF in evict_inodes().
The flags are not orthogonal, so super_iter_inodes() warns if an unknown
bit is set or if INO_ITER_F_REFERENCED is combined with either of the
no-reference flags.
Signed-off-by: Huang Shijie <huangsj@hygon.cn>
---
fs/super.c | 87 ++++++++++++++++++++++++++++++++++
include/linux/fs/super_types.h | 29 ++++++++++++
2 files changed, 116 insertions(+)
diff --git a/fs/super.c b/fs/super.c
index c5a0658f7aaf..204c70c7f179 100644
--- a/fs/super.c
+++ b/fs/super.c
@@ -171,6 +171,93 @@ static void super_wake(struct super_block *sb, unsigned int flag)
wake_up_var(&sb->s_flags);
}
+static bool super_iter_check(struct inode *inode, int flags,
+ ino_iter_eligible_fn eligible, void *priv)
+{
+ bool ret = false;
+
+ if ((flags & INO_ITER_F_REFERENCED) && !icount_read_once(inode))
+ return false;
+ if ((flags & INO_ITER_F_NOREF) && icount_read_once(inode))
+ return false;
+
+ spin_lock(&inode->i_lock);
+
+ if ((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 (eligible && !eligible(inode, priv))
+ goto out_unlock;
+
+ if (!(flags & INO_ITER_F_NOIGET))
+ __iget(inode);
+
+ ret = true;
+out_unlock:
+ spin_unlock(&inode->i_lock);
+ return ret;
+}
+
+/**
+ * super_iter_inodes - iterate all the cached inodes on a superblock
+ * @sb: superblock to iterate
+ * @iter_fn: called for each inode, with a reference held
+ * @eligible: optional filter, called before the reference is taken
+ * @priv: caller data, passed to @iter_fn and @eligible
+ * @flags: INO_ITER_F_* control flags
+ *
+ * This function iterates all cached inodes on a superblock that are not in
+ * the process of being initialised or torn down. By default it runs @iter_fn
+ * with a valid, referenced inode, so it is safe for the caller to do anything
+ * it wants with the inode except drop the reference the iterator holds.
+ *
+ * The flag INO_ITER_F_NOIGET changes that contract. It runs @iter_fn without
+ * grabbing the inode with __iget().
+ *
+ * Return INO_ITER_RET_DONE if the iteration runs to the end.
+ * Return other value if the iteration breaks.
+ */
+int super_iter_inodes(struct super_block *sb, ino_iter_fn iter_fn,
+ ino_iter_eligible_fn eligible, void *priv, int flags)
+{
+ struct inode *inode, *old_inode = NULL;
+ int ret;
+
+ WARN_ON_ONCE(flags & ~INO_ITER_F_ALL);
+ WARN_ON_ONCE((flags & INO_ITER_F_REFERENCED) &&
+ (flags & (INO_ITER_F_NOREF | INO_ITER_F_NOIGET)));
+
+ spin_lock(&sb->s_inode_list_lock);
+ list_for_each_entry(inode, &sb->s_inodes, i_sb_list) {
+ if (!super_iter_check(inode, flags, eligible, priv))
+ continue;
+
+ spin_unlock(&sb->s_inode_list_lock);
+
+ if (!(flags & INO_ITER_F_NOIGET))
+ iput(old_inode);
+
+ ret = iter_fn(inode, priv);
+
+ old_inode = inode;
+ if (ret != INO_ITER_RET_NEXT)
+ goto out_loop;
+
+ cond_resched();
+ spin_lock(&sb->s_inode_list_lock);
+ }
+ spin_unlock(&sb->s_inode_list_lock);
+ ret = INO_ITER_RET_DONE;
+out_loop:
+ if (!(flags & INO_ITER_F_NOIGET))
+ iput(old_inode);
+ return ret;
+}
+EXPORT_SYMBOL_GPL(super_iter_inodes);
+
/*
* 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..974d3c087952 100644
--- a/include/linux/fs/super_types.h
+++ b/include/linux/fs/super_types.h
@@ -80,6 +80,35 @@ enum freeze_holder {
FREEZE_EXCL = (1U << 3),
};
+/* Inode iteration callback return values */
+#define INO_ITER_RET_DONE 0
+#define INO_ITER_RET_ABORT 1
+#define INO_ITER_RET_NEXT 2
+
+/* Inode iteration control flags */
+/* Only iterate inodes that still have a reference. */
+#define INO_ITER_F_REFERENCED BIT(0)
+/* Only iterate inodes whose reference count has reached zero. */
+#define INO_ITER_F_NOREF BIT(1)
+/* Do not take a reference on the inodes handed to the callback. */
+#define INO_ITER_F_NOIGET BIT(2)
+
+/* REFERENCED and NOREF ask for opposite things, so only one may be set. */
+#define INO_ITER_F_ALL (INO_ITER_F_REFERENCED | INO_ITER_F_NOREF | \
+ INO_ITER_F_NOIGET)
+
+typedef int (*ino_iter_fn)(struct inode *inode, void *priv);
+
+/*
+ * Optional filter, called from inside the inode's i_lock and before
+ * __iget(), so that an inode can be rejected without paying for a
+ * reference.
+ */
+typedef bool (*ino_iter_eligible_fn)(struct inode *inode, void *priv);
+
+int super_iter_inodes(struct super_block *sb, ino_iter_fn iter_fn,
+ ino_iter_eligible_fn eligible, 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 [flat|nested] 15+ messages in thread* Re: [PATCH v3 01/11] vfs: add super_iter_inodes()
2026-11-20 3:12 ` [PATCH v3 01/11] vfs: add super_iter_inodes() Huang Shijie
@ 2026-11-20 14:56 ` Andreas Gruenbacher
0 siblings, 0 replies; 15+ messages in thread
From: Andreas Gruenbacher @ 2026-11-20 14:56 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
On Fri, Nov 20, 2026 at 4:19 AM Huang Shijie <huangsj@hygon.cn> wrote:
> Iterating the cached inodes of a superblock is open coded in the
> kernel. Add super_iter_inodes() for the callers which can work without
> holding the sb->s_inode_list_lock.
>
> For super_iter_inodes():
> -- By default, it runs @iter_fn with a referenced inode and with
> sb->s_inode_list_lock dropped, so the callback is free to block and to
> do whatever it wants with the inode except drop the reference the
> iterator holds.
>
> -- Three flags change the default behaviour.
>
> INO_ITER_F_REFERENCED restricts the walk to inodes which still have a
> reference.
>
> INO_ITER_F_NOREF does the opposite: only inodes whose reference count
> has already reached zero are handed out.
>
> INO_ITER_F_NOIGET tells the iterator do not call __iget for the inode.
> Used with INO_ITER_F_NOREF in evict_inodes().
>
> The flags are not orthogonal, so super_iter_inodes() warns if an unknown
> bit is set or if INO_ITER_F_REFERENCED is combined with either of the
> no-reference flags.
>
> Signed-off-by: Huang Shijie <huangsj@hygon.cn>
> ---
> fs/super.c | 87 ++++++++++++++++++++++++++++++++++
> include/linux/fs/super_types.h | 29 ++++++++++++
> 2 files changed, 116 insertions(+)
>
> diff --git a/fs/super.c b/fs/super.c
> index c5a0658f7aaf..204c70c7f179 100644
> --- a/fs/super.c
> +++ b/fs/super.c
> @@ -171,6 +171,93 @@ static void super_wake(struct super_block *sb, unsigned int flag)
> wake_up_var(&sb->s_flags);
> }
>
> +static bool super_iter_check(struct inode *inode, int flags,
> + ino_iter_eligible_fn eligible, void *priv)
> +{
> + bool ret = false;
> +
> + if ((flags & INO_ITER_F_REFERENCED) && !icount_read_once(inode))
> + return false;
> + if ((flags & INO_ITER_F_NOREF) && icount_read_once(inode))
> + return false;
> +
> + spin_lock(&inode->i_lock);
> +
> + if ((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 (eligible && !eligible(inode, priv))
> + goto out_unlock;
> +
> + if (!(flags & INO_ITER_F_NOIGET))
> + __iget(inode);
> +
> + ret = true;
> +out_unlock:
> + spin_unlock(&inode->i_lock);
> + return ret;
> +}
> +
> +/**
> + * super_iter_inodes - iterate all the cached inodes on a superblock
> + * @sb: superblock to iterate
> + * @iter_fn: called for each inode, with a reference held
> + * @eligible: optional filter, called before the reference is taken
> + * @priv: caller data, passed to @iter_fn and @eligible
> + * @flags: INO_ITER_F_* control flags
> + *
> + * This function iterates all cached inodes on a superblock that are not in
> + * the process of being initialised or torn down. By default it runs @iter_fn
> + * with a valid, referenced inode, so it is safe for the caller to do anything
> + * it wants with the inode except drop the reference the iterator holds.
> + *
> + * The flag INO_ITER_F_NOIGET changes that contract. It runs @iter_fn without
> + * grabbing the inode with __iget().
> + *
> + * Return INO_ITER_RET_DONE if the iteration runs to the end.
> + * Return other value if the iteration breaks.
> + */
> +int super_iter_inodes(struct super_block *sb, ino_iter_fn iter_fn,
> + ino_iter_eligible_fn eligible, void *priv, int flags)
> +{
> + struct inode *inode, *old_inode = NULL;
> + int ret;
> +
> + WARN_ON_ONCE(flags & ~INO_ITER_F_ALL);
> + WARN_ON_ONCE((flags & INO_ITER_F_REFERENCED) &&
> + (flags & (INO_ITER_F_NOREF | INO_ITER_F_NOIGET)));
> +
> + spin_lock(&sb->s_inode_list_lock);
> + list_for_each_entry(inode, &sb->s_inodes, i_sb_list) {
> + if (!super_iter_check(inode, flags, eligible, priv))
> + continue;
> +
> + spin_unlock(&sb->s_inode_list_lock);
> +
> + if (!(flags & INO_ITER_F_NOIGET))
> + iput(old_inode);
> +
> + ret = iter_fn(inode, priv);
> +
> + old_inode = inode;
> + if (ret != INO_ITER_RET_NEXT)
> + goto out_loop;
> +
> + cond_resched();
> + spin_lock(&sb->s_inode_list_lock);
> + }
> + spin_unlock(&sb->s_inode_list_lock);
> + ret = INO_ITER_RET_DONE;
> +out_loop:
> + if (!(flags & INO_ITER_F_NOIGET))
> + iput(old_inode);
> + return ret;
> +}
> +EXPORT_SYMBOL_GPL(super_iter_inodes);
> +
> /*
> * 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..974d3c087952 100644
> --- a/include/linux/fs/super_types.h
> +++ b/include/linux/fs/super_types.h
> @@ -80,6 +80,35 @@ enum freeze_holder {
> FREEZE_EXCL = (1U << 3),
> };
>
> +/* Inode iteration callback return values */
> +#define INO_ITER_RET_DONE 0
> +#define INO_ITER_RET_ABORT 1
> +#define INO_ITER_RET_NEXT 2
> +
> +/* Inode iteration control flags */
> +/* Only iterate inodes that still have a reference. */
> +#define INO_ITER_F_REFERENCED BIT(0)
> +/* Only iterate inodes whose reference count has reached zero. */
> +#define INO_ITER_F_NOREF BIT(1)
> +/* Do not take a reference on the inodes handed to the callback. */
> +#define INO_ITER_F_NOIGET BIT(2)
> +
> +/* REFERENCED and NOREF ask for opposite things, so only one may be set. */
> +#define INO_ITER_F_ALL (INO_ITER_F_REFERENCED | INO_ITER_F_NOREF | \
> + INO_ITER_F_NOIGET)
> +
> +typedef int (*ino_iter_fn)(struct inode *inode, void *priv);
> +
> +/*
> + * Optional filter, called from inside the inode's i_lock and before
> + * __iget(), so that an inode can be rejected without paying for a
> + * reference.
> + */
> +typedef bool (*ino_iter_eligible_fn)(struct inode *inode, void *priv);
> +
> +int super_iter_inodes(struct super_block *sb, ino_iter_fn iter_fn,
> + ino_iter_eligible_fn eligible, void *priv, int flags);
> +
> struct super_operations {
> struct inode *(*alloc_inode)(struct super_block *sb);
> void (*destroy_inode)(struct inode *inode);
> --
> 2.53.0
Sorry to say, but this has gone quite a bit off course. Can you try a
construct like iomap_iter() instead?
Thanks,
Andreas
^ permalink raw reply [flat|nested] 15+ messages in thread
* [PATCH v3 02/11] vfs: add super_iter_inodes_locked()
2026-11-20 3:12 [PATCH v3 0/11] vfs: add inode iteration methods Huang Shijie
2026-11-20 3:12 ` [PATCH v3 01/11] vfs: add super_iter_inodes() Huang Shijie
@ 2026-11-20 3:12 ` Huang Shijie
2026-11-20 3:12 ` [PATCH v3 03/11] fs/quota: convert remove_dquot_ref() to super_iter_inodes_locked() Huang Shijie
` (9 subsequent siblings)
11 siblings, 0 replies; 15+ messages in thread
From: Huang Shijie @ 2026-11-20 3:12 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
The super_iter_inodes_locked() keeps sb->s_inode_list_lock held
for the whole walk and takes no inode references at all.
Its callback therefore only has an existence guarantee:
it must not block and must do its own inode state checks and
its own reference handling.
The iterator is internal to the VFS, being declared in fs/internal.h.
Signed-off-by: Huang Shijie <huangsj@hygon.cn>
---
fs/internal.h | 31 +++++++++++++++++++++++++++++++
1 file changed, 31 insertions(+)
diff --git a/fs/internal.h b/fs/internal.h
index 967c8556c17f..27fe7948296c 100644
--- a/fs/internal.h
+++ b/fs/internal.h
@@ -139,6 +139,37 @@ 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 INO_ITER_RET_ABORT if the iteration stops early.
+ * Return INO_ITER_RET_DONE if the iteration runs to the end.
+ */
+static inline int super_iter_inodes_locked(struct super_block *sb,
+ ino_iter_fn iter_fn, void *priv)
+{
+ struct inode *inode;
+ int ret = INO_ITER_RET_NEXT;
+
+ 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 == INO_ITER_RET_ABORT)
+ break;
+ }
+ spin_unlock(&sb->s_inode_list_lock);
+
+ /* Mark the end here */
+ if (ret == INO_ITER_RET_NEXT)
+ ret = INO_ITER_RET_DONE;
+
+ 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 [flat|nested] 15+ messages in thread* [PATCH v3 03/11] fs/quota: convert remove_dquot_ref() to super_iter_inodes_locked()
2026-11-20 3:12 [PATCH v3 0/11] vfs: add inode iteration methods Huang Shijie
2026-11-20 3:12 ` [PATCH v3 01/11] vfs: add super_iter_inodes() Huang Shijie
2026-11-20 3:12 ` [PATCH v3 02/11] vfs: add super_iter_inodes_locked() Huang Shijie
@ 2026-11-20 3:12 ` Huang Shijie
2026-11-20 3:12 ` [PATCH v3 04/11] block: convert nr_blockdev_pages() " Huang Shijie
` (8 subsequent siblings)
11 siblings, 0 replies; 15+ messages in thread
From: Huang Shijie @ 2026-11-20 3:12 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 super_iter_inodes_locked().
No functional change.
Signed-off-by: Huang Shijie <huangsj@hygon.cn>
---
fs/quota/dquot.c | 68 ++++++++++++++++++++++++++++--------------------
1 file changed, 40 insertions(+), 28 deletions(-)
diff --git a/fs/quota/dquot.c b/fs/quota/dquot.c
index a3d503591871..9aaf2cfa9e1b 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,46 @@ 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 INO_ITER_RET_NEXT;
+}
+
+static void remove_dquot_ref(struct super_block *sb, int type)
+{
+ struct dquot_ref_data ref = {
+ .type = type,
+ };
+
+ /*
+ * We have to scan 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) so the existence guarantee that
+ * super_iter_inodes_locked() provides inodes passed to
+ * remove_dquot_ref_fn() is sufficient for this operation.
+ */
+ super_iter_inodes_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 [flat|nested] 15+ messages in thread* [PATCH v3 04/11] block: convert nr_blockdev_pages() to super_iter_inodes_locked()
2026-11-20 3:12 [PATCH v3 0/11] vfs: add inode iteration methods Huang Shijie
` (2 preceding siblings ...)
2026-11-20 3:12 ` [PATCH v3 03/11] fs/quota: convert remove_dquot_ref() to super_iter_inodes_locked() Huang Shijie
@ 2026-11-20 3:12 ` Huang Shijie
2026-11-20 3:12 ` [PATCH v3 05/11] fs: convert generic_shutdown_super() poison loop Huang Shijie
` (7 subsequent siblings)
11 siblings, 0 replies; 15+ messages in thread
From: Huang Shijie @ 2026-11-20 3:12 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 super_iter_inodes_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..3c1d38475607 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 INO_ITER_RET_NEXT;
+}
+
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);
-
+ super_iter_inodes_locked(blockdev_superblock, bdev_pages_count, &ret);
return ret;
}
--
2.53.0
^ permalink raw reply [flat|nested] 15+ messages in thread* [PATCH v3 05/11] fs: convert generic_shutdown_super() poison loop
2026-11-20 3:12 [PATCH v3 0/11] vfs: add inode iteration methods Huang Shijie
` (3 preceding siblings ...)
2026-11-20 3:12 ` [PATCH v3 04/11] block: convert nr_blockdev_pages() " Huang Shijie
@ 2026-11-20 3:12 ` Huang Shijie
2026-11-20 3:13 ` [PATCH v3 06/11] fs/inode: convert evict_inodes() to super_iter_inodes() Huang Shijie
` (6 subsequent siblings)
11 siblings, 0 replies; 15+ messages in thread
From: Huang Shijie @ 2026-11-20 3:12 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
super_iter_inodes_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 204c70c7f179..dad7689fd3de 100644
--- a/fs/super.c
+++ b/fs/super.c
@@ -765,6 +765,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 INO_ITER_RET_NEXT;
+}
+
/**
* generic_shutdown_super - common helper for ->kill_sb()
* @sb: superblock to kill
@@ -823,15 +832,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);
+ super_iter_inodes_locked(sb, inode_poison_fn, NULL);
}
}
/*
--
2.53.0
^ permalink raw reply [flat|nested] 15+ messages in thread* [PATCH v3 06/11] fs/inode: convert evict_inodes() to super_iter_inodes()
2026-11-20 3:12 [PATCH v3 0/11] vfs: add inode iteration methods Huang Shijie
` (4 preceding siblings ...)
2026-11-20 3:12 ` [PATCH v3 05/11] fs: convert generic_shutdown_super() poison loop Huang Shijie
@ 2026-11-20 3:13 ` Huang Shijie
2026-11-20 3:13 ` [PATCH v3 07/11] block: convert sync_bdevs() " Huang Shijie
` (5 subsequent siblings)
11 siblings, 0 replies; 15+ messages in thread
From: Huang Shijie @ 2026-11-20 3:13 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 super_iter_inodes() 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 | 61 +++++++++++++++++++++++-------------------------------
1 file changed, 26 insertions(+), 35 deletions(-)
diff --git a/fs/inode.c b/fs/inode.c
index f63535ee3629..be21d1f10007 100644
--- a/fs/inode.c
+++ b/fs/inode.c
@@ -865,6 +865,30 @@ static void dispose_list(struct list_head *head)
}
}
+static int evict_inode_fn(struct inode *inode, void *data)
+{
+ struct list_head *list = data;
+
+ /*
+ * Only drain what previous rounds collected: @inode is the inode the
+ * walk is using as its cursor, and evict() would unlink it from
+ * s_inodes underneath the walk.
+ */
+ if (need_resched()) {
+ cond_resched();
+ dispose_list(list);
+ }
+ list_add(&inode->i_lru, list);
+ return INO_ITER_RET_NEXT;
+}
+
+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
@@ -876,43 +900,10 @@ static void dispose_list(struct list_head *head)
*/
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);
-
- /*
- * 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);
-
+ super_iter_inodes(sb, evict_inode_fn, evict_inode_eligible, &dispose,
+ INO_ITER_F_NOREF | INO_ITER_F_NOIGET);
dispose_list(&dispose);
}
EXPORT_SYMBOL_GPL(evict_inodes);
--
2.53.0
^ permalink raw reply [flat|nested] 15+ messages in thread* [PATCH v3 07/11] block: convert sync_bdevs() to super_iter_inodes()
2026-11-20 3:12 [PATCH v3 0/11] vfs: add inode iteration methods Huang Shijie
` (5 preceding siblings ...)
2026-11-20 3:13 ` [PATCH v3 06/11] fs/inode: convert evict_inodes() to super_iter_inodes() Huang Shijie
@ 2026-11-20 3:13 ` Huang Shijie
2026-11-20 3:13 ` [PATCH v3 08/11] fs/drop_caches: convert drop_pagecache_sb() " Huang Shijie
` (4 subsequent siblings)
11 siblings, 0 replies; 15+ messages in thread
From: Huang Shijie @ 2026-11-20 3:13 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 super_iter_inodes().
No functional change.
Signed-off-by: Huang Shijie <huangsj@hygon.cn>
---
block/bdev.c | 86 +++++++++++++++++++++-------------------------------
1 file changed, 34 insertions(+), 52 deletions(-)
diff --git a/block/bdev.c b/block/bdev.c
index 3c1d38475607..37fa47fb15ac 100644
--- a/block/bdev.c
+++ b/block/bdev.c
@@ -1339,66 +1339,48 @@ void bdev_mark_dead(struct block_device *bdev, bool surprise)
*/
EXPORT_SYMBOL_GPL(bdev_mark_dead);
-void sync_bdevs(bool wait)
+static bool sync_bdev_eligible(struct inode *inode, void *priv)
{
- struct inode *inode, *old_inode = NULL;
+ return inode->i_mapping->nrpages != 0;
+}
- 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;
+static int sync_bdev_fn(struct inode *inode, void *priv)
+{
+ bool wait = *(bool *)priv;
+ struct block_device *bdev;
- 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);
+ 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))
+ return INO_ITER_RET_NEXT;
+ mutex_lock(&bdev->bd_disk->open_mutex);
+ if (!atomic_read(&bdev->bd_openers)) {
+ ; /* skip */
+ } else if (wait) {
/*
- * The inode does not keep the gendisk of a partition alive, the
- * device does.
+ * We keep the error status of individual mapping so
+ * that applications can catch the writeback error using
+ * fsync(2). See filemap_fdatawait_keep_errors() for
+ * details.
*/
- if (!kobject_get_unless_zero(&bdev->bd_device.kobj)) {
- spin_lock(&blockdev_superblock->s_inode_list_lock);
- continue;
- }
+ filemap_fdatawait_keep_errors(inode->i_mapping);
+ } else {
+ filemap_fdatawrite(inode->i_mapping);
+ }
+ mutex_unlock(&bdev->bd_disk->open_mutex);
+ put_device(&bdev->bd_device);
- mutex_lock(&bdev->bd_disk->open_mutex);
- if (!atomic_read(&bdev->bd_openers)) {
- ; /* skip */
- } else if (wait) {
- /*
- * We keep the error status of individual mapping so
- * that applications can catch the writeback error using
- * fsync(2). See filemap_fdatawait_keep_errors() for
- * details.
- */
- filemap_fdatawait_keep_errors(inode->i_mapping);
- } else {
- filemap_fdatawrite(inode->i_mapping);
- }
- mutex_unlock(&bdev->bd_disk->open_mutex);
- put_device(&bdev->bd_device);
+ return INO_ITER_RET_NEXT;
+}
- spin_lock(&blockdev_superblock->s_inode_list_lock);
- }
- spin_unlock(&blockdev_superblock->s_inode_list_lock);
- iput(old_inode);
+void sync_bdevs(bool wait)
+{
+ super_iter_inodes(blockdev_superblock, sync_bdev_fn,
+ sync_bdev_eligible, &wait, 0);
}
/*
--
2.53.0
^ permalink raw reply [flat|nested] 15+ messages in thread* [PATCH v3 08/11] fs/drop_caches: convert drop_pagecache_sb() to super_iter_inodes()
2026-11-20 3:12 [PATCH v3 0/11] vfs: add inode iteration methods Huang Shijie
` (6 preceding siblings ...)
2026-11-20 3:13 ` [PATCH v3 07/11] block: convert sync_bdevs() " Huang Shijie
@ 2026-11-20 3:13 ` Huang Shijie
2026-11-20 3:13 ` [PATCH v3 09/11] fs/quota: convert add_dquot_ref() " Huang Shijie
` (3 subsequent siblings)
11 siblings, 0 replies; 15+ messages in thread
From: Huang Shijie @ 2026-11-20 3:13 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 super_iter_inodes().
No functional change.
Signed-off-by: Huang Shijie <huangsj@hygon.cn>
---
fs/drop_caches.c | 45 +++++++++++++++++----------------------------
1 file changed, 17 insertions(+), 28 deletions(-)
diff --git a/fs/drop_caches.c b/fs/drop_caches.c
index 28c5fb9c7357..68f1926e0c0e 100644
--- a/fs/drop_caches.c
+++ b/fs/drop_caches.c
@@ -17,39 +17,28 @@
/* A global variable is a bit ugly, but it keeps the code simple */
static int sysctl_drop_caches;
-static void drop_pagecache_sb(struct super_block *sb, void *unused)
+static bool invalidate_inode_eligible(struct inode *inode, void *priv)
+{
+ /*
+ * We skip inodes without pages but we deliberately won't
+ * in case we need to reschedule to avoid softlockups.
+ */
+ return !mapping_empty(inode->i_mapping) || need_resched();
+}
+
+static int invalidate_inode_fn(struct inode *inode, void *priv)
{
- struct inode *inode, *toput_inode = NULL;
+ invalidate_mapping_pages(inode->i_mapping, 0, -1);
+ return INO_ITER_RET_NEXT;
+}
+static void drop_pagecache_sb(struct super_block *sb, void *unused)
+{
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);
-
- 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);
+ super_iter_inodes(sb, invalidate_inode_fn,
+ invalidate_inode_eligible, NULL, 0);
}
static int drop_caches_sysctl_handler(const struct ctl_table *table, int write,
--
2.53.0
^ permalink raw reply [flat|nested] 15+ messages in thread* [PATCH v3 09/11] fs/quota: convert add_dquot_ref() to super_iter_inodes()
2026-11-20 3:12 [PATCH v3 0/11] vfs: add inode iteration methods Huang Shijie
` (7 preceding siblings ...)
2026-11-20 3:13 ` [PATCH v3 08/11] fs/drop_caches: convert drop_pagecache_sb() " Huang Shijie
@ 2026-11-20 3:13 ` Huang Shijie
2026-11-20 3:13 ` [PATCH v3 10/11] landlock: convert hook_sb_delete() " Huang Shijie
` (2 subsequent siblings)
11 siblings, 0 replies; 15+ messages in thread
From: Huang Shijie @ 2026-11-20 3:13 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 super_iter_inodes()
No functional change.
Signed-off-by: Huang Shijie <huangsj@hygon.cn>
---
fs/quota/dquot.c | 71 +++++++++++++++++++-----------------------------
1 file changed, 28 insertions(+), 43 deletions(-)
diff --git a/fs/quota/dquot.c b/fs/quota/dquot.c
index 9aaf2cfa9e1b..6453949c8af6 100644
--- a/fs/quota/dquot.c
+++ b/fs/quota/dquot.c
@@ -1066,56 +1066,41 @@ static int dqinit_needed(struct inode *inode, int type)
return 0;
}
-/* This routine is guarded by s_umount semaphore */
-static int add_dquot_ref(struct super_block *sb, int type)
+static bool add_dquot_ref_eligible(struct inode *inode, void *priv)
{
- struct inode *inode, *old_inode = NULL;
-#ifdef CONFIG_QUOTA_DEBUG
- int reserved = 0;
-#endif
- int err = 0;
+ struct dquot_ref_data *ref = priv;
- 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);
+ return atomic_read(&inode->i_writecount) &&
+ dqinit_needed(inode, ref->type);
+}
+
+static int add_dquot_ref_fn(struct inode *inode, void *priv)
+{
+ struct dquot_ref_data *ref = priv;
+ int ret;
#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
- iput(old_inode);
- err = __dquot_initialize(inode, type);
- if (err) {
- iput(inode);
- goto out;
- }
+ ret = __dquot_initialize(inode, ref->type);
+ if (ret)
+ return ret;
+ return INO_ITER_RET_NEXT;
+}
- /*
- * 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:
+/* This routine is guarded by s_umount semaphore */
+static int add_dquot_ref(struct super_block *sb, int type)
+{
+ struct dquot_ref_data ref = {
+ .type = type,
+ };
+ int err;
+
+ err = super_iter_inodes(sb, add_dquot_ref_fn,
+ add_dquot_ref_eligible, &ref, 0);
#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 [flat|nested] 15+ messages in thread* [PATCH v3 10/11] landlock: convert hook_sb_delete() to super_iter_inodes()
2026-11-20 3:12 [PATCH v3 0/11] vfs: add inode iteration methods Huang Shijie
` (8 preceding siblings ...)
2026-11-20 3:13 ` [PATCH v3 09/11] fs/quota: convert add_dquot_ref() " Huang Shijie
@ 2026-11-20 3:13 ` Huang Shijie
2026-11-20 3:13 ` [PATCH v3 11/11] gfs2: convert gfs2_evict_inodes() " Huang Shijie
2026-11-20 14:59 ` [PATCH v3 0/11] vfs: add inode iteration methods Andreas Gruenbacher
11 siblings, 0 replies; 15+ messages in thread
From: Huang Shijie @ 2026-11-20 3:13 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 super_iter_inodes().
No functional change.
Signed-off-by: Huang Shijie <huangsj@hygon.cn>
---
security/landlock/fs.c | 138 ++++++++++++++++-------------------------
1 file changed, 52 insertions(+), 86 deletions(-)
diff --git a/security/landlock/fs.c b/security/landlock/fs.c
index 2ab67bcad198..dae606892d96 100644
--- a/security/landlock/fs.c
+++ b/security/landlock/fs.c
@@ -1380,105 +1380,71 @@ static void hook_inode_free_security_rcu(void *inode_security)
*
* Cf. fsnotify_unmount_inodes() and evict_inodes()
*/
-static void hook_sb_delete(struct super_block *const sb)
+static bool release_inode_eligible(struct inode *inode, void *priv)
{
- struct inode *inode, *prev_inode = NULL;
+ bool ret;
- if (!landlock_initialized)
- return;
+ rcu_read_lock();
+ ret = !!rcu_dereference(landlock_inode(inode)->object);
+ rcu_read_unlock();
- spin_lock(&sb->s_inode_list_lock);
- list_for_each_entry(inode, &sb->s_inodes, i_sb_list) {
- struct landlock_object *object;
+ return ret;
+}
+
+static int release_inode_fn(struct inode *inode, void *priv)
+{
+ struct landlock_object *object;
+
+ rcu_read_lock();
+ object = rcu_dereference(landlock_inode(inode)->object);
+ if (!object) {
+ rcu_read_unlock();
+ return INO_ITER_RET_NEXT;
+ }
- /* Only handles referenced inodes. */
- if (!icount_read_once(inode))
- continue;
+ /*
+ * If there is no concurrent release_inode() ongoing, then we
+ * are in charge of calling iput() on this inode, otherwise we
+ * will just wait for it to finish.
+ */
+ spin_lock(&object->lock);
+ if (object->underobj == inode) {
+ object->underobj = NULL;
+ spin_unlock(&object->lock);
+ rcu_read_unlock();
/*
- * Protects against concurrent modification of inode (e.g.
- * from get_inode_object()).
+ * Because object->underobj was not NULL,
+ * release_inode() and get_inode_object() guarantee
+ * that it is safe to reset
+ * landlock_inode(inode)->object while it is not NULL.
+ * It is therefore not necessary to lock inode->i_lock.
*/
- spin_lock(&inode->i_lock);
+ rcu_assign_pointer(landlock_inode(inode)->object, NULL);
/*
- * 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.
+ * At this point, we own the ihold() reference that was
+ * originally set up by get_inode_object() and the
+ * __iget() reference that we just set in this loop
+ * walk. Therefore there are at least two references
+ * on the inode.
*/
- 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);
+ iput_not_last(inode);
+ } else {
+ spin_unlock(&object->lock);
+ rcu_read_unlock();
+ }
- /*
- * If there is no concurrent release_inode() ongoing, then we
- * are in charge of calling iput() on this inode, otherwise we
- * will just wait for it to finish.
- */
- spin_lock(&object->lock);
- if (object->underobj == inode) {
- object->underobj = NULL;
- spin_unlock(&object->lock);
- rcu_read_unlock();
+ return INO_ITER_RET_NEXT;
+}
- /*
- * Because object->underobj was not NULL,
- * release_inode() and get_inode_object() guarantee
- * that it is safe to reset
- * landlock_inode(inode)->object while it is not NULL.
- * It is therefore not necessary to lock inode->i_lock.
- */
- rcu_assign_pointer(landlock_inode(inode)->object, NULL);
- /*
- * At this point, we own the ihold() reference that was
- * originally set up by get_inode_object() and the
- * __iget() reference that we just set in this loop
- * walk. Therefore there are at least two references
- * on the inode.
- */
- iput_not_last(inode);
- } else {
- spin_unlock(&object->lock);
- rcu_read_unlock();
- }
+static void hook_sb_delete(struct super_block *const sb)
+{
+ if (!landlock_initialized)
+ return;
- 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;
- }
- spin_unlock(&sb->s_inode_list_lock);
+ super_iter_inodes(sb, release_inode_fn, release_inode_eligible,
+ NULL, INO_ITER_F_REFERENCED);
- /* 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 [flat|nested] 15+ messages in thread* [PATCH v3 11/11] gfs2: convert gfs2_evict_inodes() to super_iter_inodes()
2026-11-20 3:12 [PATCH v3 0/11] vfs: add inode iteration methods Huang Shijie
` (9 preceding siblings ...)
2026-11-20 3:13 ` [PATCH v3 10/11] landlock: convert hook_sb_delete() " Huang Shijie
@ 2026-11-20 3:13 ` Huang Shijie
2026-11-20 14:59 ` [PATCH v3 0/11] vfs: add inode iteration methods Andreas Gruenbacher
11 siblings, 0 replies; 15+ messages in thread
From: Huang Shijie @ 2026-11-20 3:13 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 super_iter_inodes().
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 | 29 +++++++----------------------
1 file changed, 7 insertions(+), 22 deletions(-)
diff --git a/fs/gfs2/ops_fstype.c b/fs/gfs2/ops_fstype.c
index 6ec47376aecf..a6f8e95a4187 100644
--- a/fs/gfs2/ops_fstype.c
+++ b/fs/gfs2/ops_fstype.c
@@ -1717,6 +1717,12 @@ static int gfs2_meta_init_fs_context(struct fs_context *fc)
return 0;
}
+/* Nothing to do because we just want to bounce the inode through iput() */
+static int gfs2_evict_inode_fn(struct inode *inode, void *priv)
+{
+ return INO_ITER_RET_NEXT;
+}
+
/**
* gfs2_evict_inodes - evict inodes cooperatively
* @sb: the superblock
@@ -1739,31 +1745,10 @@ 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;
-
- cond_resched();
- spin_lock(&sb->s_inode_list_lock);
- }
- spin_unlock(&sb->s_inode_list_lock);
- iput(toput_inode);
+ super_iter_inodes(sb, gfs2_evict_inode_fn, NULL, NULL, 0);
}
static void gfs2_kill_sb(struct super_block *sb)
--
2.53.0
^ permalink raw reply [flat|nested] 15+ messages in thread* Re: [PATCH v3 0/11] vfs: add inode iteration methods
2026-11-20 3:12 [PATCH v3 0/11] vfs: add inode iteration methods Huang Shijie
` (10 preceding siblings ...)
2026-11-20 3:13 ` [PATCH v3 11/11] gfs2: convert gfs2_evict_inodes() " Huang Shijie
@ 2026-11-20 14:59 ` Andreas Gruenbacher
2026-11-23 2:31 ` Huang Shijie
11 siblings, 1 reply; 15+ messages in thread
From: Andreas Gruenbacher @ 2026-11-20 14:59 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
On Fri, Nov 20, 2026 at 4:13 AM Huang Shijie <huangsj@hygon.cn> wrote:
> This series adds two superblock inode iteration helpers,
> super_iter_inodes() and super_iter_inodes_locked(), and converts the
> open-coded sb->s_inodes walks in the VFS to them. The design is based on
> Dave Chinner's patch set:
> "vfs: improving inode cache iteration scalability"
> https://lore.kernel.org/all/20241002014017.3801899-1-david@fromorbit.com/
>
> About the two helpers:
>
> int super_iter_inodes(struct super_block *sb, ino_iter_fn iter_fn,
> ino_iter_eligible_fn eligible, void *priv,
> int flags);
>
> static inline int super_iter_inodes_locked(struct super_block *sb,
> ino_iter_fn iter_fn,
> void *priv);
>
> - super_iter_inodes() runs @iter_fn with a referenced inode and with
> sb->s_inode_list_lock __dropped__, so the callback is free to block and
> to do whatever it wants with the inode except drop the reference the
> iterator holds. The iterator releases that one reference per inode
> itself, so the callback cannot leave a dangling inode behind.
> It is exported because GFS2 can be built as a module.
>
> - super_iter_inodes_locked() keeps sb->s_inode_list_lock held for the
> whole walk and takes no inode references at all. Its callback
> therefore only has an existence guarantee: it must not block and
> must do its own inode state checks and its own reference handling.
> It is internal to the VFS, being declared in fs/internal.h.
>
> - The callback is separate from the caller's data, so a caller keeps
> its state in its own structure instead of filling in a shared
> context:
>
> typedef int (*ino_iter_fn)(struct inode *inode, void *priv);
>
> - %INO_ITER_F_REFERENCED restricts the safe iterator to inodes that
> still have a reference. It preserves the "only handle referenced
> inodes" rule of landlock's sb-delete walk.
>
> - %INO_ITER_F_NOREF does the opposite: only inodes whose reference
> count has already reached zero are handed out. %INO_ITER_F_NOIGET
> suppresses the reference. Neither keeps the inode alive, so the
> caller must make it unclaimable itself.
>
> Patch 1 adds super_iter_inodes().
> Patch 2 adds super_iter_inodes_locked().
> Patch 3-5 convert the callers which cannot use referenced inodes
> (dquot removal, nr_blockdev_pages() 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 patch set depends a small patch:
> "fs/drop_caches: skip filesystems without fs writeback"
> https://lkml.org/lkml/2026/9/23/675
That link doesn't work for me. When referring to other postings, can
you please use lore links?
There also is no posting of a patch with that title from that day on
https://lore.kernel.org/linux-fsdevel/. You may be referring to
https://lore.kernel.org/linux-fsdevel/20260920072811.2064247-2-huangsj@hygon.cn/,
but that patch queue no longer applies. So please post patch queues in
a state that applies and optionally, make a git branch available.
(Also, enabling "git config --global format.useAutoBase whenAble" is a
possibility, but it wouldn't have helped in this case.
> v2-->v3:
> -the callers' state no longer goes through a shared struct, the
> callback now takes the caller's own @priv.
>
> -super_iter_inodes() takes @eligible directly, so a caller can reject
> an inode before the reference is paid for.
>
> -rename super_iter_inodes_unsafe() to super_iter_inodes_locked() and
> drop the rcu_read_lock(), which the list lock already covers.
>
> -drop @ctx->prev; the resume point is gone with the unsafe iterator.
>
> -evict_inodes() no longer goes through the locked iterator.
> it now uses the unlocked iterator with INO_ITER_F_NOREF | INO_ITER_F_NOIGET.
>
> -each caller gets its own patch.
>
> 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 super_iter_inodes()
> vfs: add super_iter_inodes_locked()
> fs/quota: convert remove_dquot_ref() to super_iter_inodes_locked()
> block: convert nr_blockdev_pages() to super_iter_inodes_locked()
> fs: convert generic_shutdown_super() poison loop
> fs/inode: convert evict_inodes() to super_iter_inodes()
> block: convert sync_bdevs() to super_iter_inodes()
> fs/drop_caches: convert drop_pagecache_sb() to super_iter_inodes()
> fs/quota: convert add_dquot_ref() to super_iter_inodes()
> landlock: convert hook_sb_delete() to super_iter_inodes()
> gfs2: convert gfs2_evict_inodes() to super_iter_inodes()
>
> block/bdev.c | 101 ++++++++++--------------
> fs/drop_caches.c | 45 ++++-------
> fs/gfs2/ops_fstype.c | 29 ++-----
> fs/inode.c | 61 ++++++---------
> fs/internal.h | 31 ++++++++
> fs/quota/dquot.c | 139 ++++++++++++++++-----------------
> fs/super.c | 106 ++++++++++++++++++++++---
> include/linux/fs/super_types.h | 29 +++++++
> security/landlock/fs.c | 138 ++++++++++++--------------------
> 9 files changed, 370 insertions(+), 309 deletions(-)
>
> --
> 2.53.0
Thanks,
Andreas
^ permalink raw reply [flat|nested] 15+ messages in thread* Re: [PATCH v3 0/11] vfs: add inode iteration methods
2026-11-20 14:59 ` [PATCH v3 0/11] vfs: add inode iteration methods Andreas Gruenbacher
@ 2026-11-23 2:31 ` Huang Shijie
0 siblings, 0 replies; 15+ messages in thread
From: Huang Shijie @ 2026-11-23 2:31 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
在 Fri, Nov 20, 2026 at 03:59:10PM +0100,Andreas Gruenbacher 写道:
> On Fri, Nov 20, 2026 at 4:13 AM Huang Shijie <huangsj@hygon.cn> wrote:
> > This series adds two superblock inode iteration helpers,
> > super_iter_inodes() and super_iter_inodes_locked(), and converts the
> > open-coded sb->s_inodes walks in the VFS to them. The design is based on
> > Dave Chinner's patch set:
> > "vfs: improving inode cache iteration scalability"
> > https://lore.kernel.org/all/20241002014017.3801899-1-david@fromorbit.com/
> >
> > About the two helpers:
> >
> > int super_iter_inodes(struct super_block *sb, ino_iter_fn iter_fn,
> > ino_iter_eligible_fn eligible, void *priv,
> > int flags);
> >
> > static inline int super_iter_inodes_locked(struct super_block *sb,
> > ino_iter_fn iter_fn,
> > void *priv);
> >
> > - super_iter_inodes() runs @iter_fn with a referenced inode and with
> > sb->s_inode_list_lock __dropped__, so the callback is free to block and
> > to do whatever it wants with the inode except drop the reference the
> > iterator holds. The iterator releases that one reference per inode
> > itself, so the callback cannot leave a dangling inode behind.
> > It is exported because GFS2 can be built as a module.
> >
> > - super_iter_inodes_locked() keeps sb->s_inode_list_lock held for the
> > whole walk and takes no inode references at all. Its callback
> > therefore only has an existence guarantee: it must not block and
> > must do its own inode state checks and its own reference handling.
> > It is internal to the VFS, being declared in fs/internal.h.
> >
> > - The callback is separate from the caller's data, so a caller keeps
> > its state in its own structure instead of filling in a shared
> > context:
> >
> > typedef int (*ino_iter_fn)(struct inode *inode, void *priv);
> >
> > - %INO_ITER_F_REFERENCED restricts the safe iterator to inodes that
> > still have a reference. It preserves the "only handle referenced
> > inodes" rule of landlock's sb-delete walk.
> >
> > - %INO_ITER_F_NOREF does the opposite: only inodes whose reference
> > count has already reached zero are handed out. %INO_ITER_F_NOIGET
> > suppresses the reference. Neither keeps the inode alive, so the
> > caller must make it unclaimable itself.
> >
> > Patch 1 adds super_iter_inodes().
> > Patch 2 adds super_iter_inodes_locked().
> > Patch 3-5 convert the callers which cannot use referenced inodes
> > (dquot removal, nr_blockdev_pages() 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 patch set depends a small patch:
> > "fs/drop_caches: skip filesystems without fs writeback"
> > https://lkml.org/lkml/2026/9/23/675
>
> That link doesn't work for me. When referring to other postings, can
> you please use lore links?
Okay. No problem.
>
> There also is no posting of a patch with that title from that day on
> https://lore.kernel.org/linux-fsdevel/. You may be referring to
> https://lore.kernel.org/linux-fsdevel/20260920072811.2064247-2-huangsj@hygon.cn/,
> but that patch queue no longer applies. So please post patch queues in
> a state that applies and optionally, make a git branch available.
>
I will add the kernel version in next version.
Thanks
Huang Shijie
^ permalink raw reply [flat|nested] 15+ messages in thread