When inode has dirtied timestamps, we currently call sync_lazytime() on last iput. This is done because inode with any dirty bit set is not inserted into LRU and dirty timestamps expire only after many (12 by default) hours so these inodes would be sitting outside of LRU aging for a really long time. However this can result in doing IO and consequently GFP_NOFAIL allocations from dentry reclaim making MM complain. Sample trace for ext4 is: prune_dcache_sb shrink_dentry_list __dentry_kill iput sync_lazytime __mark_inode_dirty ext4_dirty_inode __ext4_mark_inode_dirty ext4_reserve_inode_write ext4_get_inode_loc bdev_getblk __filemap_get_folio_mpol Avoid this dirtying on last iput by reshuffling unused inodes to the beginning of b_dirty_time list and clobbering dirtied_time_when instead so that they get written during next periodic writeback. Signed-off-by: Jan Kara --- fs/fs-writeback.c | 68 ++++++++++++++++++++++++++++++++++++++++++++--- fs/inode.c | 15 +++++------ fs/internal.h | 1 + 3 files changed, 72 insertions(+), 12 deletions(-) diff --git a/fs/fs-writeback.c b/fs/fs-writeback.c index ec65ec6e48e7..44f68a8c146f 100644 --- a/fs/fs-writeback.c +++ b/fs/fs-writeback.c @@ -1541,6 +1541,24 @@ void sb_clear_inode_writeback(struct inode *inode) } } +static void inode_add_dirtytime_head(struct inode *inode, + struct bdi_writeback *wb) +{ + unsigned long new_time; + + new_time = jiffies - dirtytime_expire_interval * HZ; + if (!list_empty(&wb->b_dirty_time)) { + struct inode *first = wb_inode(wb->b_dirty_time.prev); + unsigned long first_time = READ_ONCE(first->dirtied_time_when); + + if (time_before(first_time, new_time)) + new_time = first_time; + } + inode->dirtied_when = new_time; + inode->dirtied_time_when = new_time; + list_move_tail(&inode->i_io_list, &wb->b_dirty_time); +} + static void move_to_dirtytime_locked(struct inode *inode, struct bdi_writeback *wb) { @@ -1854,8 +1872,7 @@ static void requeue_inode(struct inode *inode, struct bdi_writeback *wb, */ redirty_tail_locked(inode, wb); } else if (inode_state_read(inode) & I_DIRTY_TIME) { - inode->dirtied_when = jiffies; - inode_io_list_move_locked(inode, wb, &wb->b_dirty_time); + move_to_dirtytime_locked(inode, wb); inode_state_clear(inode, I_SYNC_QUEUED); } else { /* The inode is clean. Remove from writeback lists. */ @@ -2096,8 +2113,7 @@ static int writeback_single_inode(struct inode *inode, inode_cgwb_move_to_attached(inode, wb); else if (!(istate & I_SYNC_QUEUED) && !(istate & I_DIRTY)) { WARN_ON_ONCE(!(istate & I_DIRTY_TIME)); - inode->dirtied_when = jiffies; - inode_io_list_move_locked(inode, wb, &wb->b_dirty_time); + move_to_dirtytime_locked(inode, wb); } } @@ -2986,6 +3002,50 @@ void __mark_inode_dirty(struct inode *inode, int flags) } EXPORT_SYMBOL(__mark_inode_dirty); +/* + * If inode has dirty timestamps to write out, make sure flush worker writes + * them out during its next periodic writeback writeout. + */ +void queue_dirtytime_writeback(struct inode *inode) +{ + struct bdi_writeback *wb; + unsigned int i_state; + + lockdep_assert_held(&inode->i_lock); + + wb = locked_inode_to_wb_and_lock_list(inode); + spin_lock(&inode->i_lock); + i_state = inode_state_read(inode); + /* Written back while we dropped i_lock? */ + if (!(i_state & I_DIRTY_TIME)) + goto out_wb_lock; + + /* + * If the inode is dirty, next writeback will come soon so we don't + * need to do anything. If it is under writeback, we cannot touch dirty + * lists now so we handle moving the inode after writeback is complete. + */ + if (i_state & (I_SYNC_QUEUED | I_SYNC | I_DIRTY)) { + /* + * Clobber dirtied_time_when so that timestamps get synced on + * next inode writeback. + */ + inode->dirtied_time_when = jiffies - + dirtytime_expire_interval * HZ; + goto out_wb_lock; + } + + /* + * Move inode to the beginning of dirty queue so that it gets written + * out during the next periodic writeback. + */ + inode_add_dirtytime_head(inode, wb); + /* Wake up flush worker to ensure timely flush of the inode */ + wb_wakeup_delayed(wb); +out_wb_lock: + spin_unlock(&wb->list_lock); +} + /* * The @s_sync_lock is used to serialise concurrent sync operations * to avoid lock contention problems with concurrent wait_sb_inodes() calls. diff --git a/fs/inode.c b/fs/inode.c index bf9403c32188..a9f757fba23b 100644 --- a/fs/inode.c +++ b/fs/inode.c @@ -2051,7 +2051,6 @@ void iput(struct inode *inode) if (unlikely(!inode)) return; -retry: lockdep_assert_not_held(&inode->i_lock); VFS_BUG_ON_INODE(inode_state_read_once(inode) & (I_FREEING | I_CLEAR), inode); /* @@ -2064,14 +2063,14 @@ void iput(struct inode *inode) if (atomic_add_unless(&inode->i_count, -1, 1)) return; - if (inode->i_nlink && sync_lazytime(inode)) - goto retry; - spin_lock(&inode->i_lock); - if (unlikely((inode_state_read(inode) & I_DIRTY_TIME) && inode->i_nlink)) { - spin_unlock(&inode->i_lock); - goto retry; - } + /* + * If inode has timestamp updates pending, queue flushing them now as + * otherwise the dirtiness could be preventing the inode from entering + * LRU for hours. + */ + if (inode->i_nlink && inode_state_read(inode) & I_DIRTY_TIME) + queue_dirtytime_writeback(inode); if (!atomic_dec_and_test(&inode->i_count)) { spin_unlock(&inode->i_lock); diff --git a/fs/internal.h b/fs/internal.h index 9580afef293e..855a80f4b879 100644 --- a/fs/internal.h +++ b/fs/internal.h @@ -222,6 +222,7 @@ bool in_group_or_capable(const struct mnt_idmap *idmap, */ long get_nr_dirty_inodes(void); bool sync_lazytime(struct inode *inode); +void queue_dirtytime_writeback(struct inode *inode); /* * dcache.c -- 2.53.0