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 Closes: https://lore.kernel.org/linux-mm/20261119035808.431204-1-hi@tychen.cc/ Signed-off-by: Tal Zussman --- 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