[PATCH v2 1/3] vfs: add inode iteration methods

Huang Shijie huangsj at hygon.cn
Fri Nov 20 11:43:01 UTC 2026


在 Fri, Nov 20, 2026 at 12:14:24PM +0100,Christian Brauner 写道:
> On Mon, Nov 16, 2026 at 02:45:44PM +0800, Huang Shijie wrote:
> > > 	struct inode_iter_contex ctx = INODE_ITER_CTX_INIT;
> > > 
> > > 	while ((inode = sb_inode_iter()))) {
> > > 		/* do stuff */
> > > 	}
> > > 
> > How to manage the sb->s_inode_list_lock in this code?
> > 
> > I have no idea how to hide the sb->s_inode_list_lock in this code.
> 
> I think Christoph is right. The lock shouldn't be held across
> iterations. Let the iterator take it, grab a reference to the next
> inode, drop the lock again and hand the inode out. The reference keeps the
okay. 

If we can accept this method, I can change the code following it in future.

> inode on s_inodes so the next call can continue from there.
> 
> This is how drop_pagecache_sb() does it today. Sm like (UNTESTED and not
> nice-ified):
> 
> struct sb_inode_iter {
> 	struct super_block *sb;
> 	struct inode *inode;
> 	bool done;
> };
> 
> struct inode *sb_inode_iter_next(struct sb_inode_iter *it)
> {
> 	struct super_block *sb = it->sb;
> 	struct inode *prev = it->inode, *inode;
> 
> 	if (it->done)
> 		return NULL;
> 
> 	spin_lock(&sb->s_inode_list_lock);
> 	inode = list_prepare_entry(prev, &sb->s_inodes, i_sb_list);
> 	list_for_each_entry_continue(inode, &sb->s_inodes, i_sb_list) {
> 		spin_lock(&inode->i_lock);
> 		if (inode_state_read(inode) & (I_FREEING | I_WILL_FREE | I_NEW)) {
> 			spin_unlock(&inode->i_lock);
> 			continue;
> 		}
> 		__iget(inode);
> 		spin_unlock(&inode->i_lock);
> 		spin_unlock(&sb->s_inode_list_lock);
> 		it->inode = inode;
> 		iput(prev);
> 		return inode;
> 	}
> 	spin_unlock(&sb->s_inode_list_lock);
> 	it->inode = NULL;
> 	it->done = true;
> 	iput(prev);
> 	return NULL;
> }
> 
> void sb_inode_iter_end(struct sb_inode_iter *it)
> {
> 	iput(it->inode);
> 	it->inode = NULL;
> 	it->done = true;
> }
> 
> and then drop_pagecache_sb() becomes:
> 
> 	struct sb_inode_iter it = { .sb = sb };
> 	struct inode *inode;
> 
> 	while ((inode = sb_inode_iter_next(&it))) {
> 		invalidate_mapping_pages(inode->i_mapping, 0, -1);
> 		cond_resched();
> 	}
> 
> Make it a cleanup class and a caller that breaks out early doesn't have
> to remember sb_inode_iter_end() either.
> 
> This will end because new inodes are added at the head and the cursor
> stays on the list. So these assumptions should get
> VFS_WARN_ON_ONCE()-es.
> 
> Walkers that can't take a reference might need different solution.
> I'm not sure if there are any?
I will think about it.

Thanks
Huang Shijie




More information about the Linux-security-module-archive mailing list