Walking sb->s_inodes requires taking 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 the inode state checks and the reference count. Add a caller-owned cursor wrapped in a cleanup class, so that the iterator goes out of scope as soon as the loop ends: for_each_inode(inode, sb) { /* inode is referenced and unlocked here */ } A precondition is passed as the third argument and evaluated with both sb->s_inode_list_lock and inode->i_lock held, before a reference is paid for. It sees the candidate in @inode and may use it, so it can depend on state that is only valid while the inode is locked: for_each_inode(inode, sb, cond) { /* cond was true */ } The condition is expanded at the call site, so it can use the caller's own variables without any context argument. Dropping the previous inode's reference may sleep, so the walk cannot be used from atomic context. Signed-off-by: Huang Shijie --- include/linux/fs.h | 72 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) diff --git a/include/linux/fs.h b/include/linux/fs.h index 5b3a100aba77..383f41c022ec 100644 --- a/include/linux/fs.h +++ b/include/linux/fs.h @@ -3664,12 +3664,84 @@ extern int vfs_fadvise(struct file *file, loff_t offset, loff_t len, int advice); extern int generic_fadvise(struct file *file, loff_t offset, loff_t len, int advice); int generic_atomic_write_valid(struct kiocb *iocb, struct iov_iter *iter); +/* Caller-owned state for walking sb->s_inodes. */ +struct sb_inode_iter { + struct super_block *sb; + struct inode *cursor; /* holds a reference */ + bool done; +}; + +static inline void sb_inode_iter_end(struct sb_inode_iter *iter) +{ + iput(iter->cursor); + iter->cursor = NULL; + iter->done = true; +} + +static inline struct sb_inode_iter sb_inode_iter_init(struct super_block *sb) +{ + return (struct sb_inode_iter){ .sb = sb }; +} + +DEFINE_CLASS(sb_inode_iter, struct sb_inode_iter, + sb_inode_iter_end(&_T), + sb_inode_iter_init(sb), + struct super_block *sb) + +#define __sb_inode_iter_step(iter, inode, cond) \ +({ \ + struct inode *__prev = (iter)->cursor; \ + struct super_block *__sb = (iter)->sb; \ + bool __found = false; \ + \ + 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) { \ + spin_lock(&inode->i_lock); \ + if ((inode_state_read(inode) & \ + (I_NEW | I_FREEING | I_WILL_FREE)) || \ + !(cond)) { \ + spin_unlock(&inode->i_lock); \ + continue; \ + } \ + __iget(inode); \ + spin_unlock(&inode->i_lock); \ + __found = true; \ + break; \ + } \ + spin_unlock(&__sb->s_inode_list_lock); \ + (iter)->cursor = __found ? inode : NULL; \ + (iter)->done = !__found; \ + iput(__prev); \ + (iter)->cursor; \ +}) + +/* + * Walk the inodes of @sb. + * -- @inode is referenced and unlocked in the body. + * -- @cond, when given, is evaluated with sb->s_inode_list_lock and + * inode->i_lock held, and before the reference is taken. + * -- Dropping the previous inode's reference may + * sleep, so this cannot be used in atomic context. + */ +#define for_each_inode(...) \ + CONCATENATE(__for_each_inode_, COUNT_ARGS(__VA_ARGS__))(__VA_ARGS__) + +#define __for_each_inode_2(inode, sb) \ + for (CLASS(sb_inode_iter, __it)(sb); \ + !__it.done && __sb_inode_iter_step(&__it, inode, true);) + +#define __for_each_inode_3(inode, sb, cond) \ + for (CLASS(sb_inode_iter, __it)(sb); \ + !__it.done && __sb_inode_iter_step(&__it, inode, cond);) + static inline bool extensible_ioctl_valid(unsigned int cmd_a, unsigned int cmd_b, size_t min_size) { if (_IOC_DIR(cmd_a) != _IOC_DIR(cmd_b)) return false; if (_IOC_TYPE(cmd_a) != _IOC_TYPE(cmd_b)) -- 2.34.1