Some walkers of sb->s_inodes cannot take a reference on the inode they are looking at. Give them their own iterator, which keeps sb->s_inode_list_lock held for the whole walk and takes no reference: for_each_inode_locked(inode, sb) { /* inode is only guaranteed to exist */ } The body runs in atomic context, must not block and must do its own inode state checks. The lock is dropped after the loop on every exit path, so the body must not release or reacquire it. No caller is converted yet, so this is a no-op. Signed-off-by: Huang Shijie --- include/linux/fs.h | 58 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/include/linux/fs.h b/include/linux/fs.h index 383f41c022ec..1f444ad89c17 100644 --- a/include/linux/fs.h +++ b/include/linux/fs.h @@ -3736,12 +3736,70 @@ DEFINE_CLASS(sb_inode_iter, struct sb_inode_iter, !__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);) +struct sb_inode_iter_locked { + struct super_block *sb; + struct inode *cursor; + bool done; +}; + +static inline struct sb_inode_iter_locked +sb_inode_iter_locked_init(struct super_block *sb) +{ + spin_lock(&sb->s_inode_list_lock); + return (struct sb_inode_iter_locked){ .sb = sb }; +} + +static inline void sb_inode_iter_locked_end(struct sb_inode_iter_locked *it) +{ + spin_unlock(&it->sb->s_inode_list_lock); +} + +static inline struct inode * +sb_inode_iter_locked_next(struct sb_inode_iter_locked *it) +{ + struct inode *inode; + + if (it->done) + return NULL; + + if (it->cursor) + inode = list_next_entry(it->cursor, i_sb_list); + else + inode = list_first_entry(&it->sb->s_inodes, struct inode, + i_sb_list); + + if (list_entry_is_head(inode, &it->sb->s_inodes, i_sb_list)) { + it->done = true; + return NULL; + } + + it->cursor = inode; + return inode; +} + +DEFINE_CLASS(sb_inode_iter_locked, struct sb_inode_iter_locked, + sb_inode_iter_locked_end(&_T), + sb_inode_iter_locked_init(sb), + struct super_block *sb) + +/* + * Walk the inodes of @sb with sb->s_inode_list_lock held. + * -- @inode is not referenced: the body only gets an existence + * guarantee, so it must do its own inode state checks. + * -- The body runs in atomic context and must not block. + * -- The lock is released after the loop on every exit path, so the body + * must not release or reacquire it. + */ +#define for_each_inode_locked(inode, sb) \ + for (CLASS(sb_inode_iter_locked, __it)(sb); \ + ((inode) = sb_inode_iter_locked_next(&__it));) + 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