Lookups without FGP_LOCK clear PG_dropbehind and then test PG_dirty to adjust WB_DONTCACHE_DIRTY without holding the folio lock, so they race with dirtying and cleaning and the counter drifts. Do it under the folio lock and inside the wb transaction, and leave the flag set if the lock is contended. With a 50us delay widening the race, the counter was left at 25 to 522 pages after sync in three five-minute runs; with this it was 0. Fixes: 88d6f128d06d ("mm: track DONTCACHE dirty pages per bdi_writeback") Cc: stable@vger.kernel.org Signed-off-by: Usama Arif --- mm/filemap.c | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/mm/filemap.c b/mm/filemap.c index 64eefcdb24b393..a49cca600f8c3e 100644 --- a/mm/filemap.c +++ b/mm/filemap.c @@ -2104,19 +2104,32 @@ struct folio *__filemap_get_folio_mpol(struct address_space *mapping, if (!folio) return ERR_PTR(-ENOENT); - /* not an uncached lookup, clear uncached if set */ - if (!(fgp_flags & FGP_DONTCACHE) && folio_test_clear_dropbehind(folio)) { - if (folio_test_dirty(folio) && - mapping_can_writeback(mapping)) { + /* + * Not an uncached lookup, clear uncached if set. Dirtying and cleaning + * normally hold the folio lock; take it to keep WB_DONTCACHE_DIRTY in + * sync. + */ + if (!(fgp_flags & FGP_DONTCACHE) && folio_test_dropbehind(folio)) { + bool locked = fgp_flags & FGP_LOCK; + + if (!locked && !folio_trylock(folio)) + return folio; + if (mapping_can_writeback(mapping)) { struct inode *inode = mapping->host; struct bdi_writeback *wb; struct wb_lock_cookie cookie = {}; - long nr = folio_nr_pages(folio); wb = unlocked_inode_to_wb_begin(inode, &cookie); - wb_stat_mod(wb, WB_DONTCACHE_DIRTY, -nr); + if (folio_test_clear_dropbehind(folio) && + folio_test_dirty(folio)) + wb_stat_mod(wb, WB_DONTCACHE_DIRTY, + -folio_nr_pages(folio)); unlocked_inode_to_wb_end(inode, &cookie); + } else { + folio_clear_dropbehind(folio); } + if (!locked) + folio_unlock(folio); } return folio; } -- 2.53.1-Meta