From: sashiko-bot@kernel.org
To: "Tal Zussman" <tz2294@columbia.edu>,
"Matthew Wilcox (Oracle)" <willy@infradead.org>,
"Jan Kara" <jack@suse.cz>,
"Andrew Morton" <akpm@linux-foundation.org>,
"Jens Axboe" <axboe@kernel.dk>
Cc: linux-mm@kvack.org, "Tianyi Chen" <hi@tychen.cc>
Subject: Re: [PATCH] mm/filemap: only drop dropbehind folios on uncached reads
Date: Sat, 21 Nov 2026 19:51:52 +0000 [thread overview]
Message-ID: <sashiko-outbox-227325@kernel.org> (raw)
In-Reply-To: <20261120-filemap-dropbehind-read-v1-1-a373ac3fd993@columbia.edu>
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
prev parent reply other threads:[~2026-11-21 19:51 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
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-24 21:33 ` Tal Zussman
2026-11-21 19:51 ` sashiko-bot [this message]
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=sashiko-outbox-227325@kernel.org \
--to=sashiko-bot@kernel.org \
--cc=akpm@linux-foundation.org \
--cc=axboe@kernel.dk \
--cc=hi@tychen.cc \
--cc=jack@suse.cz \
--cc=linux-mm@kvack.org \
--cc=sashiko-reviews@lists.linux.dev \
--cc=tz2294@columbia.edu \
--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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.