From: Jan Kara f2fs periodically calls sync_inode_metadata() for some inodes. The logic in writeback_single_inode() thus periodically calls to redirty_tail() and the inode's dirtied_when timestamp never gets old enough for the inode to be eligible for periodic data writeback. Thus dirty data from it is only ever written on explicit request (fsync(2), sync(2), unmount) or on dirty limit pressure. The redirty_tail() logic in writeback_single_inode() actually should not be necessary. Since this function is used only for once-off writeback, there's no need for any livelock protection among inodes and writeback_single_inode() is only responsible for demoting inode to "less dirty" list based on dirty bits cleared as a result of writeback. So remove the redirty_tail() logic and add a comment explaining the rules. The redirty_tail() did hide a race though, the one fixed by commit 846a3351ddfe ("writeback: Avoid skipping inode writeback"). __writeback_single_inode() sets I_DIRTY_PAGES again when the mapping is still tagged dirty. If the inode had no I_DIRTY bits (e.g. write_inode_now(inode, 0) or sync_inode_metadata(inode, 0) of an inode with only I_DIRTY_TIME), the dirty pages come from a racing writer, whose __mark_inode_dirty() then finds I_DIRTY_PAGES set and does not queue the inode, leaving it on b_dirty_time or b_attached. Call __mark_inode_dirty() in that case instead, so that the inode gets queued on b_dirty. Fixes: 846a3351ddfe ("writeback: Avoid skipping inode writeback") Reported-by: Hongyu Jin Link: https://lore.kernel.org/all/20261023023707.263101-1-hongyu.jin@unisoc.com Cc: stable@vger.kernel.org Signed-off-by: Jan Kara [hongyu.jin: let __mark_inode_dirty() queue an inode whose pages were dirtied while it had no I_DIRTY bits, update comments] Signed-off-by: Hongyu Jin --- fs/fs-writeback.c | 60 +++++++++++++++++++++++++++++++++-------------- 1 file changed, 43 insertions(+), 17 deletions(-) diff --git a/fs/fs-writeback.c b/fs/fs-writeback.c index 58a6403780aa..c7979b0cd375 100644 --- a/fs/fs-writeback.c +++ b/fs/fs-writeback.c @@ -1780,7 +1780,9 @@ bool sync_lazytime(struct inode *inode) * * This doesn't remove the inode from the writeback list it is on, except * potentially to move it from b_dirty_time to b_dirty due to timestamp - * expiration. The caller is otherwise responsible for writeback list handling. + * expiration, or to queue it on b_dirty via __mark_inode_dirty() when its + * pages were dirtied while it had no I_DIRTY bits. The caller is otherwise + * responsible for writeback list handling. * * The caller is also responsible for setting the I_SYNC flag beforehand and * calling inode_sync_complete() to clear it afterwards. @@ -1790,6 +1792,7 @@ __writeback_single_inode(struct inode *inode, struct writeback_control *wbc) { struct address_space *mapping = inode->i_mapping; long nr_to_write = wbc->nr_to_write; + bool mark_pages = false; unsigned dirty; int ret; @@ -1843,12 +1846,23 @@ __writeback_single_inode(struct inode *inode, struct writeback_control *wbc) * still has dirty pages. The flag is reinstated after smp_mb() if * necessary. This guarantees that either __mark_inode_dirty() * sees clear I_DIRTY_PAGES or we see PAGECACHE_TAG_DIRTY. + * + * If the inode had any I_DIRTY bits, __mark_inode_dirty() has already + * queued it (if it is to be queued at all) and we can just reinstate + * I_DIRTY_PAGES. Otherwise the pages were dirtied by a racing writer + * and the inode may sit on b_dirty_time or b_attached. Setting the + * flag here would make the writer's __mark_inode_dirty() bail out + * without queueing the inode, so let __mark_inode_dirty() set it and + * queue the inode instead. */ smp_mb(); - if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY)) - inode_state_set(inode, I_DIRTY_PAGES); - else if (unlikely(inode_state_read(inode) & I_PINNING_NETFS_WB)) { + if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY)) { + if (dirty) + inode_state_set(inode, I_DIRTY_PAGES); + else + mark_pages = true; + } else if (unlikely(inode_state_read(inode) & I_PINNING_NETFS_WB)) { if (!(inode_state_read(inode) & I_DIRTY_PAGES)) { inode_state_clear(inode, I_PINNING_NETFS_WB); wbc->unpinned_netfs_wb = true; @@ -1858,6 +1872,9 @@ __writeback_single_inode(struct inode *inode, struct writeback_control *wbc) spin_unlock(&inode->i_lock); + if (mark_pages) + __mark_inode_dirty(inode, I_DIRTY_PAGES); + /* Don't write the inode if only I_DIRTY_PAGES was set */ if (dirty & ~I_DIRTY_PAGES) { int err = write_inode(inode, wbc); @@ -1946,22 +1963,31 @@ static int writeback_single_inode(struct inode *inode, * as it can be finally deleted at this moment. */ if (!(inode_state_read(inode) & I_FREEING)) { + enum inode_state_flags_enum istate = inode_state_read(inode); + /* - * If the inode is now fully clean, then it can be safely - * removed from its writeback list (if any). Otherwise the - * flusher threads are responsible for the writeback lists. + * __mark_inode_dirty() is responsible for promoting inode to + * the proper list (b_attached -> b_dirty_time -> b_dirty). + * Here we are responsible for demoting inode after some dirty + * bits were cleared. + * + * If the inode is now fully clean, then it can be safely moved + * to b_attached list. If the inode is dirty, we don't want to + * touch it - it should be already put to the dirty list by + * __mark_inode_dirty() and we'd just unnecessarily clobber the + * dirtied_when stamp (which is particularly problematic when + * we were called from sync_inode_metadata()). We also cannot + * touch the dirty list association when I_SYNC_QUEUED is set + * and flush worker is responsible for it. Otherwise move the + * inode to b_dirty_time list as that's the only remaining + * possibility. */ - if (!(inode_state_read(inode) & I_DIRTY_ALL)) + if (!(istate & I_DIRTY_ALL)) inode_cgwb_move_to_attached(inode, wb); - else if (!(inode_state_read(inode) & I_SYNC_QUEUED)) { - if ((inode_state_read(inode) & I_DIRTY)) - 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); - } + 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); } } -- 2.53.0