Re: [PATCH v4 00/11] vfs: add inode iteration methods
From: Huang Shijie
Date: Thu Nov 26 2026 - 07:16:09 EST
在 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@xxxxxxxx> 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@xxxxxxxxxxxxx/
> >
> > 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@xxxxxxxx/
> /* 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@xxxxxxxxxxxxx/
and
https://lore.kernel.org/all/20260920072811.2064247-1-huangsj@xxxxxxxx/
Thanks
Huang Shijie