From: Mike Snitzer <snitzer@kernel.org>
To: Matthew Wilcox <willy@infradead.org>, Jan Kara <jack@suse.cz>,
linux-mm@kvack.org, Christian Brauner <brauner@kernel.org>,
linux-fsdevel@vger.kernel.org
Cc: Chuck Lever <cel@kernel.org>, Jeff Layton <jlayton@kernel.org>,
linux-nfs@vger.kernel.org
Subject: [PATCH v11 2/7] mm: take a folio off WB_DONTCACHE_DIRTY only if it was counted there
Date: Tue, 10 Nov 2026 20:56:02 -0500 [thread overview]
Message-ID: <20261111015607.46338-3-snitzer@kernel.org> (raw)
In-Reply-To: <20261111015607.46338-1-snitzer@kernel.org>
A cached lookup that finds a dropbehind folio clears the flag and
subtracts the folio from WB_DONTCACHE_DIRTY if PG_dirty is set. That
test races with the dirtying of the folio. filemap_dirty_folio() sets
PG_dirty before folio_account_dirtied() counts the folio, so a lookup in
between subtracts a folio that was never added. The lookup also does
not hold the folio lock. Every other path that subtracts a dirty folio
from the counter holds it, so the lookup can subtract the folio a second
time. Either way the counter drifts, and once it is negative the
DONTCACHE writeback kick writes nothing.
Fix this by having filemap_clear_dropbehind() clear the dropbehind flag
of a folio that a cached lookup found, and take the folio off
WB_DONTCACHE_DIRTY if it is counted there. With the folio locked and
the i_pages lock held, the folio is counted if both PG_dirty and the
dirty tag are set. A folio that another task has locked keeps the
flag.
Fixes: 88d6f128d06d ("mm: track DONTCACHE dirty pages per bdi_writeback")
Reviewed-by: Jeff Layton <jlayton@kernel.org>
Signed-off-by: Mike Snitzer <snitzer@kernel.org>
---
mm/filemap.c | 42 ++++++++++++++++++++++++++++--------------
1 file changed, 28 insertions(+), 14 deletions(-)
diff --git a/mm/filemap.c b/mm/filemap.c
index 437113addbddf..98ce955404127 100644
--- a/mm/filemap.c
+++ b/mm/filemap.c
@@ -1956,22 +1956,36 @@ void *filemap_get_entry(struct address_space *mapping, pgoff_t index)
return folio;
}
-/* Clear the dropbehind flag of a folio that a cached lookup found. */
+/*
+ * Clear the dropbehind flag of a folio that a cached lookup found, and take
+ * the folio off WB_DONTCACHE_DIRTY if it is counted there. With the folio
+ * locked and the i_pages lock held, the folio is counted if both PG_dirty and
+ * the dirty tag are set. A folio that another task has locked keeps the flag.
+ */
static void filemap_clear_dropbehind(struct address_space *mapping,
- struct folio *folio)
+ struct folio *folio, bool locked)
{
- if (!folio_test_clear_dropbehind(folio))
- return;
- if (folio_test_dirty(folio) && mapping_can_writeback(mapping)) {
- struct inode *inode = mapping->host;
- struct bdi_writeback *wb;
- struct wb_lock_cookie cookie = {};
- long nr = folio_nr_pages(folio);
+ unsigned long flags;
- wb = unlocked_inode_to_wb_begin(inode, &cookie);
- wb_stat_mod(wb, WB_DONTCACHE_DIRTY, -nr);
- unlocked_inode_to_wb_end(inode, &cookie);
+ if (!locked) {
+ if (!folio_trylock(folio))
+ return;
+ if (unlikely(folio->mapping != mapping))
+ goto out;
}
+ if (!mapping_can_writeback(mapping)) {
+ folio_clear_dropbehind(folio);
+ goto out;
+ }
+ xa_lock_irqsave(&mapping->i_pages, flags);
+ if (folio_test_clear_dropbehind(folio) && folio_test_dirty(folio) &&
+ xa_get_mark(&mapping->i_pages, folio->index, PAGECACHE_TAG_DIRTY))
+ wb_stat_mod(inode_to_wb(mapping->host), WB_DONTCACHE_DIRTY,
+ -folio_nr_pages(folio));
+ xa_unlock_irqrestore(&mapping->i_pages, flags);
+out:
+ if (!locked)
+ folio_unlock(folio);
}
/**
@@ -2105,8 +2119,8 @@ 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))
- filemap_clear_dropbehind(mapping, folio);
+ if (!(fgp_flags & FGP_DONTCACHE) && folio_test_dropbehind(folio))
+ filemap_clear_dropbehind(mapping, folio, fgp_flags & FGP_LOCK);
return folio;
}
EXPORT_SYMBOL(__filemap_get_folio_mpol);
--
2.52.0
next prev parent reply other threads:[~2026-11-11 1:56 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-11 1:56 [PATCH v11 0/7] mm, iomap, NFSD: stop DONTCACHE writes reading back shared pages Mike Snitzer
2026-11-11 1:56 ` [PATCH v11 1/7] mm: move clearing the dropbehind flag of a found folio into a helper Mike Snitzer
2026-11-11 1:56 ` Mike Snitzer [this message]
2026-11-11 1:56 ` [PATCH v11 3/7] mm: expose WB_DONTCACHE_DIRTY in bdi debugfs stats Mike Snitzer
2026-11-11 1:56 ` [PATCH v11 4/7] mm: add folio_mark_dropbehind() to mark a folio dropbehind and account it Mike Snitzer
2026-11-11 1:56 ` [PATCH v11 5/7] mm, iomap: keep a partially covered page until a DONTCACHE write completes it Mike Snitzer
2026-11-11 1:56 ` [PATCH v11 6/7] mm, iomap: let a DONTCACHE writer complete its shared pages itself Mike Snitzer
2026-11-11 1:56 ` [PATCH v11 7/7] NFSD: drop a shared boundary page only once both WRITEs have written it Mike Snitzer
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261111015607.46338-3-snitzer@kernel.org \
--to=snitzer@kernel.org \
--cc=brauner@kernel.org \
--cc=cel@kernel.org \
--cc=jack@suse.cz \
--cc=jlayton@kernel.org \
--cc=linux-fsdevel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=linux-nfs@vger.kernel.org \
--cc=willy@infradead.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox