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

* Re: [PATCH] mm/filemap: only drop dropbehind folios on uncached reads
  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
  1 sibling, 0 replies; 3+ messages in thread
From: Matthew Wilcox @ 2026-11-20 22:16 UTC (permalink / raw)
  To: Tal Zussman
  Cc: Jan Kara, Andrew Morton, Jens Axboe, linux-fsdevel, linux-mm,
	Tianyi Chen

On Fri, Nov 20, 2026 at 02:51:17PM -0500, Tal Zussman wrote:
> 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.

I like your patch, but now I'm wondering whether RWF_DONTCACHE should
trigger readahead.  Yes, I'm sure we want to use the ->readahead entry
point for filesystems, but I think that an uncached read should not
bring in pages that weren't specifically requested.

Something like this, maybe?

+++ b/mm/readahead.c
@@ -590,7 +590,7 @@ void page_cache_sync_ra(struct readahead_control *ractl,
         * readahead will do the right thing and limit the read to just the
         * requested range, which we'll set to 1 page for this case.
         */
-       if (!ra->ra_pages || blk_cgroup_congested()) {
+       if (!ra->ra_pages || blk_cgroup_congested() || ractl->dropbehind) {
                if (!ractl->file)
                        return;
                req_count = 1;



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

* Re: [PATCH] mm/filemap: only drop dropbehind folios on uncached reads
  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
  1 sibling, 0 replies; 3+ messages in thread
From: sashiko-bot @ 2026-11-21 19:51 UTC (permalink / raw)
  To: Tal Zussman, Matthew Wilcox (Oracle), Jan Kara, Andrew Morton,
	Jens Axboe
  Cc: linux-mm, Tianyi Chen

Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [High] mm/filemap: unlocked filemap_clear_dropbehind() races with
  dirty accounting
- [Medium] mm/filemap: filemap_clear_dropbehind() in filemap_read()
  skips LRU access
--

commit 9ff8c01502e3877207be0dcbfaa0bc2369238702
Author: Tal Zussman <tz2294@columbia.edu>

mm/filemap: only drop dropbehind folios on uncached reads

This commit restricts dropping dropbehind folios in filemap_read() to
IOCB_DONTCACHE reads and extracts a filemap_clear_dropbehind() helper to
clear PG_dropbehind on cached reads and lookups.

> diff --git a/mm/filemap.c b/mm/filemap.c
> index dc0ceaa58a0f7..1bd91ff264e8a 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);
> +}

[Severity: High]
Can calling filemap_clear_dropbehind() on an unlocked folio race with
folio dirtying and writeback to dereference a NULL wb or corrupt
WB_DONTCACHE_DIRTY?

In filemap_read(), the folios in fbatch are not locked when
filemap_clear_dropbehind() runs.  When a concurrent write dirties a
dropbehind folio, filemap_dirty_folio() sets PG_dirty before
__folio_mark_dirty() takes mapping->i_pages.xa_lock and calls
folio_account_dirtied():

CPU0                                  CPU1
filemap_dirty_folio()
  folio_test_set_dirty(folio)
                                      filemap_read()
                                        filemap_clear_dropbehind()
                                          folio_test_clear_dropbehind()
                                          folio_test_dirty(folio) == true
                                          wb = unlocked_inode_to_wb_begin()
                                          wb_stat_mod(wb, ...)
                                            <-- NULL pointer dereference
  __folio_mark_dirty()
    folio_account_dirtied()
      inode_attach_wb(inode, folio)
      if (folio_test_dropbehind(folio))
        wb_stat_mod(wb, WB_DONTCACHE_DIRTY, nr)

Under CONFIG_CGROUP_WRITEBACK, inode->i_wb is NULL on an inode being
dirtied for the first time until inode_attach_wb() attaches it inside
folio_account_dirtied(), so wb_stat_mod() on CPU1 dereferences NULL.
When inode->i_wb is already attached, CPU1 decrements WB_DONTCACHE_DIRTY
and CPU0 then sees PG_dropbehind clear and skips the increment,
underflowing the counter.

Because PG_dropbehind and PG_dirty are cleared in opposite orders without
the folio lock, filemap_clear_dropbehind() also races with
folio_clear_dirty_for_io() (and __folio_cancel_dirty()):

CPU0                                  CPU1
folio_clear_dirty_for_io()            filemap_clear_dropbehind()
  folio_test_clear_dirty(folio)         folio_test_clear_dropbehind(folio)
  folio_test_dropbehind(folio)          folio_test_dirty(folio)
    --> false                             --> false

Neither side decrements WB_DONTCACHE_DIRTY, leaking the count and causing
wb_check_start_dontcache() to over-flush unrelated dirty pages on later
RWF_DONTCACHE writes.  In addition, clearing PG_dropbehind before
unlocked_inode_to_wb_begin() lets a concurrent inode_do_switch_wbs() see
PG_dropbehind already clear and skip migrating the folio's count from
old_wb to new_wb, after which filemap_clear_dropbehind() decrements
new_wb.

[ ... ]

> @@ -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);

[Severity: Medium]
Does clearing PG_dropbehind in the put_folios loop skip marking the folio
as accessed on the LRU?

filemap_read() calls folio_mark_accessed() before copying data to the
iterator, while PG_dropbehind is still set on a readahead folio left by an
earlier RWF_DONTCACHE read:

mm/folio.c:folio_mark_accessed() {
	if (folio_test_dropbehind(folio))
		return;
    ...
}

Because folio_mark_accessed() returns early when PG_dropbehind is set, the
folio is kept in the page cache without being marked referenced or
activated on the LRU, leaving it cold for reclaim.

Also, because put_folios iterates over the entire fbatch, an early exit
from the copy loop (such as iocb->ki_pos >= isize, end_offset <
folio_pos(folio), or a short copy_folio_to_iter()) will clear
PG_dropbehind on folios in the batch that were never read.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261120-filemap-dropbehind-read-v1-1-a373ac3fd993@columbia.edu?part=1


^ permalink raw reply	[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