Linux-mm Archive on lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] mm/filemap: only drop dropbehind folios on uncached reads
@ 2026-11-20 19:51 Tal Zussman
  2026-11-20 22:16 ` Matthew Wilcox
  2026-11-21 19:51 ` sashiko-bot
  0 siblings, 2 replies; 3+ messages in thread
From: Tal Zussman @ 2026-11-20 19:51 UTC (permalink / raw)
  To: Matthew Wilcox (Oracle), Jan Kara, Andrew Morton, Jens Axboe
  Cc: linux-fsdevel, linux-mm, Tianyi Chen, Tal Zussman

An RWF_DONTCACHE read marks the folios it brings into the page cache as
dropbehind, and filemap_read() drops them from the page cache once it
has copied from them. Readahead can bring in folios past the end of the
read, and those stay in the page cache with the flag set.

filemap_read() does not check whether the read it is serving is
uncached. A plain buffered read that finds one of these leftover folios
drops it as well, so the next read of that data goes to the device
again. This is wrong, as a plain read should leave what it read in the
page cache. The lookup path already does this:
__filemap_get_folio_mpol() clears the flag when a lookup without
FGP_DONTCACHE finds a dropbehind folio.

To fix it, only drop the folio in filemap_read() for an uncached read,
and clear the flag otherwise. Move the flag clearing, with its
WB_DONTCACHE_DIRTY accounting, into a helper for both callers.

With a reproducer that does a 64K RWF_DONTCACHE read and then a plain
read of a page that readahead brought in past the end of it, the page is
gone from the page cache after the plain read without this change, and
stays with it.

Fixes: 8026e49bff9b ("mm/filemap: add read support for RWF_DONTCACHE")
Reported-by: Tianyi Chen <hi@tychen.cc>
Closes: https://lore.kernel.org/linux-mm/20261119035808.431204-1-hi@tychen.cc/
Signed-off-by: Tal Zussman <tz2294@columbia.edu>
---
 mm/filemap.c | 46 ++++++++++++++++++++++++++++++++--------------
 1 file changed, 32 insertions(+), 14 deletions(-)

diff --git a/mm/filemap.c b/mm/filemap.c
index dc0ceaa58a0f..1bd91ff264e8 100644
--- a/mm/filemap.c
+++ b/mm/filemap.c
@@ -1632,6 +1632,28 @@ static void filemap_end_dropbehind(struct folio *folio)
 		folio_unmap_invalidate(mapping, folio, 0);
 }
 
+/*
+ * Clear the dropbehind flag on a folio so that it stays in the page cache.
+ */
+static void filemap_clear_dropbehind(struct address_space *mapping,
+		struct folio *folio)
+{
+	struct inode *inode = mapping->host;
+	struct wb_lock_cookie cookie = {};
+	struct bdi_writeback *wb;
+
+	if (!folio_test_dropbehind(folio))
+		return;
+	if (!folio_test_clear_dropbehind(folio))
+		return;
+	if (!folio_test_dirty(folio) || !mapping_can_writeback(mapping))
+		return;
+
+	wb = unlocked_inode_to_wb_begin(inode, &cookie);
+	wb_stat_mod(wb, WB_DONTCACHE_DIRTY, -folio_nr_pages(folio));
+	unlocked_inode_to_wb_end(inode, &cookie);
+}
+
 /*
  * If folio was marked as dropbehind, then pages should be dropped when writeback
  * completes. Do that now. If we fail, it's likely because of a big folio -
@@ -2089,19 +2111,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) && folio_test_clear_dropbehind(folio)) {
-		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);
-
-			wb = unlocked_inode_to_wb_begin(inode, &cookie);
-			wb_stat_mod(wb, WB_DONTCACHE_DIRTY, -nr);
-			unlocked_inode_to_wb_end(inode, &cookie);
-		}
-	}
+	if (!(fgp_flags & FGP_DONTCACHE))
+		filemap_clear_dropbehind(mapping, folio);
 	return folio;
 }
 EXPORT_SYMBOL(__filemap_get_folio_mpol);
@@ -2918,7 +2929,14 @@ ssize_t filemap_read(struct kiocb *iocb, struct iov_iter *iter,
 		for (i = 0; i < folio_batch_count(&fbatch); i++) {
 			struct folio *folio = fbatch.folios[i];
 
-			filemap_end_dropbehind_read(folio);
+			/*
+			 * An uncached read drops the folio. Any other read
+			 * keeps it in the page cache.
+			 */
+			if (iocb->ki_flags & IOCB_DONTCACHE)
+				filemap_end_dropbehind_read(folio);
+			else
+				filemap_clear_dropbehind(mapping, folio);
 			folio_put(folio);
 		}
 		folio_batch_init(&fbatch);

---
base-commit: 15f114dac9a7bde1796f0e54e22726fd31cfa8a5
change-id: 20261119-filemap-dropbehind-read-db1046d9c23a

Best regards,
--  
Tal Zussman <tz2294@columbia.edu>



^ permalink raw reply related	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-11-21 19:51 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-20 19:51 [PATCH] mm/filemap: only drop dropbehind folios on uncached reads Tal Zussman
2026-11-20 22:16 ` Matthew Wilcox
2026-11-21 19:51 ` sashiko-bot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox