* [PATCH 1/4] mm/migrate: preserve PG_dropbehind across folio migration
2026-12-02 14:24 [PATCH 0/4] mm: drop folios under writeback on POSIX_FADV_DONTNEED Usama Arif
@ 2026-12-02 14:24 ` Usama Arif
2026-12-02 14:41 ` Jan Kara
` (3 more replies)
2026-12-02 14:24 ` [PATCH 2/4] mm/filemap: clear PG_dropbehind on lookup under the folio lock Usama Arif
` (3 subsequent siblings)
4 siblings, 4 replies; 21+ messages in thread
From: Usama Arif @ 2026-12-02 14:24 UTC (permalink / raw)
To: Andrew Morton, willy, jack, david, axboe, Christoph Hellwig,
hannes, jlayton, brauner, tz2294, ritesh.list, chentaotao, ziy,
bigeasy, liam, vbabka, rppt, mhocko, apopple, byungchul, gourry,
joshua.hahnjy, matthew.brost, ying.huang, clrkwllms, rostedt,
linux-mm, linux-fsdevel, linux-rt-devel, kas, riel, shakeel.butt,
kernel-team
Cc: Usama Arif, stable
Migration moves PG_dirty to the new folio but not PG_dropbehind, so a
migrated dirty dropbehind folio is cleaned without decrementing
WB_DONTCACHE_DIRTY. The counter leaks, and every later RWF_DONTCACHE
kick asks the flusher for that many more pages. Copy the flag along with
PG_dirty, before the new folio becomes visible.
With RWF_DONTCACHE writes and compaction running for five minutes, 1313
to 1709 pages leaked, all of them migrated.
Fixes: 88d6f128d06d ("mm: track DONTCACHE dirty pages per bdi_writeback")
Cc: stable@vger.kernel.org
Signed-off-by: Usama Arif <usama.arif@linux.dev>
---
mm/migrate.c | 3 +++
1 file changed, 3 insertions(+)
diff --git a/mm/migrate.c b/mm/migrate.c
index 96b66a272379f9..81a8e46778610f 100644
--- a/mm/migrate.c
+++ b/mm/migrate.c
@@ -663,6 +663,9 @@ static int __folio_migrate_mapping(struct address_space *mapping,
folio_clear_dirty(folio);
folio_set_dirty(newfolio);
}
+ /* WB_DONTCACHE_DIRTY counts pages that are both dirty and dropbehind */
+ if (folio_test_dropbehind(folio))
+ folio_set_dropbehind(newfolio);
if (folio_test_swapcache(folio))
__swap_cache_replace_folio(ci, folio, newfolio);
--
2.53.1-Meta
^ permalink raw reply related [flat|nested] 21+ messages in thread* Re: [PATCH 1/4] mm/migrate: preserve PG_dropbehind across folio migration
2026-12-02 14:24 ` [PATCH 1/4] mm/migrate: preserve PG_dropbehind across folio migration Usama Arif
@ 2026-12-02 14:41 ` Jan Kara
2026-12-02 14:56 ` Jeff Layton
` (2 subsequent siblings)
3 siblings, 0 replies; 21+ messages in thread
From: Jan Kara @ 2026-12-02 14:41 UTC (permalink / raw)
To: Usama Arif
Cc: Andrew Morton, willy, jack, david, axboe, Christoph Hellwig,
hannes, jlayton, brauner, tz2294, ritesh.list, chentaotao, ziy,
bigeasy, liam, vbabka, rppt, mhocko, apopple, byungchul, gourry,
joshua.hahnjy, matthew.brost, ying.huang, clrkwllms, rostedt,
linux-mm, linux-fsdevel, linux-rt-devel, kas, riel, shakeel.butt,
kernel-team, stable
On Wed 02-12-26 06:24:36, Usama Arif wrote:
> Migration moves PG_dirty to the new folio but not PG_dropbehind, so a
> migrated dirty dropbehind folio is cleaned without decrementing
> WB_DONTCACHE_DIRTY. The counter leaks, and every later RWF_DONTCACHE
> kick asks the flusher for that many more pages. Copy the flag along with
> PG_dirty, before the new folio becomes visible.
>
> With RWF_DONTCACHE writes and compaction running for five minutes, 1313
> to 1709 pages leaked, all of them migrated.
>
> Fixes: 88d6f128d06d ("mm: track DONTCACHE dirty pages per bdi_writeback")
> Cc: stable@vger.kernel.org
> Signed-off-by: Usama Arif <usama.arif@linux.dev>
Looks good. Feel free to add:
Reviewed-by: Jan Kara <jack@suse.cz>
Honza
> ---
> mm/migrate.c | 3 +++
> 1 file changed, 3 insertions(+)
>
> diff --git a/mm/migrate.c b/mm/migrate.c
> index 96b66a272379f9..81a8e46778610f 100644
> --- a/mm/migrate.c
> +++ b/mm/migrate.c
> @@ -663,6 +663,9 @@ static int __folio_migrate_mapping(struct address_space *mapping,
> folio_clear_dirty(folio);
> folio_set_dirty(newfolio);
> }
> + /* WB_DONTCACHE_DIRTY counts pages that are both dirty and dropbehind */
> + if (folio_test_dropbehind(folio))
> + folio_set_dropbehind(newfolio);
>
> if (folio_test_swapcache(folio))
> __swap_cache_replace_folio(ci, folio, newfolio);
> --
> 2.53.1-Meta
>
--
Jan Kara <jack@suse.com>
SUSE Labs, CR
^ permalink raw reply [flat|nested] 21+ messages in thread* Re: [PATCH 1/4] mm/migrate: preserve PG_dropbehind across folio migration
2026-12-02 14:24 ` [PATCH 1/4] mm/migrate: preserve PG_dropbehind across folio migration Usama Arif
2026-12-02 14:41 ` Jan Kara
@ 2026-12-02 14:56 ` Jeff Layton
2026-12-02 15:03 ` Zi Yan
2026-12-02 15:08 ` David Hildenbrand (Arm)
3 siblings, 0 replies; 21+ messages in thread
From: Jeff Layton @ 2026-12-02 14:56 UTC (permalink / raw)
To: Usama Arif, Andrew Morton, willy, jack, david, axboe,
Christoph Hellwig, hannes, brauner, tz2294, ritesh.list,
chentaotao, ziy, bigeasy, liam, vbabka, rppt, mhocko, apopple,
byungchul, gourry, joshua.hahnjy, matthew.brost, ying.huang,
clrkwllms, rostedt, linux-mm, linux-fsdevel, linux-rt-devel, kas,
riel, shakeel.butt, kernel-team
Cc: stable
On Wed, 2026-12-02 at 06:24 -0800, Usama Arif wrote:
> Migration moves PG_dirty to the new folio but not PG_dropbehind, so a
> migrated dirty dropbehind folio is cleaned without decrementing
> WB_DONTCACHE_DIRTY. The counter leaks, and every later RWF_DONTCACHE
> kick asks the flusher for that many more pages. Copy the flag along with
> PG_dirty, before the new folio becomes visible.
>
> With RWF_DONTCACHE writes and compaction running for five minutes, 1313
> to 1709 pages leaked, all of them migrated.
>
> Fixes: 88d6f128d06d ("mm: track DONTCACHE dirty pages per bdi_writeback")
> Cc: stable@vger.kernel.org
> Signed-off-by: Usama Arif <usama.arif@linux.dev>
> ---
> mm/migrate.c | 3 +++
> 1 file changed, 3 insertions(+)
>
> diff --git a/mm/migrate.c b/mm/migrate.c
> index 96b66a272379f9..81a8e46778610f 100644
> --- a/mm/migrate.c
> +++ b/mm/migrate.c
> @@ -663,6 +663,9 @@ static int __folio_migrate_mapping(struct address_space *mapping,
> folio_clear_dirty(folio);
> folio_set_dirty(newfolio);
> }
> + /* WB_DONTCACHE_DIRTY counts pages that are both dirty and dropbehind */
> + if (folio_test_dropbehind(folio))
> + folio_set_dropbehind(newfolio);
>
> if (folio_test_swapcache(folio))
> __swap_cache_replace_folio(ci, folio, newfolio);
Reviewed-by: Jeff Layton <jlayton@kernel.org>
^ permalink raw reply [flat|nested] 21+ messages in thread* Re: [PATCH 1/4] mm/migrate: preserve PG_dropbehind across folio migration
2026-12-02 14:24 ` [PATCH 1/4] mm/migrate: preserve PG_dropbehind across folio migration Usama Arif
2026-12-02 14:41 ` Jan Kara
2026-12-02 14:56 ` Jeff Layton
@ 2026-12-02 15:03 ` Zi Yan
2026-12-02 15:08 ` David Hildenbrand (Arm)
3 siblings, 0 replies; 21+ messages in thread
From: Zi Yan @ 2026-12-02 15:03 UTC (permalink / raw)
To: Usama Arif, Andrew Morton, willy, jack, david, axboe,
Christoph Hellwig, hannes, jlayton, brauner, tz2294, ritesh.list,
chentaotao, bigeasy, liam, vbabka, rppt, mhocko, apopple,
byungchul, gourry, joshua.hahnjy, matthew.brost, ying.huang,
clrkwllms, rostedt, linux-mm, linux-fsdevel, linux-rt-devel, kas,
riel, shakeel.butt, kernel-team
Cc: stable
On Wed Dec 2, 2026 at 9:24 AM EST, Usama Arif wrote:
> Migration moves PG_dirty to the new folio but not PG_dropbehind, so a
> migrated dirty dropbehind folio is cleaned without decrementing
> WB_DONTCACHE_DIRTY. The counter leaks, and every later RWF_DONTCACHE
> kick asks the flusher for that many more pages. Copy the flag along with
> PG_dirty, before the new folio becomes visible.
>
> With RWF_DONTCACHE writes and compaction running for five minutes, 1313
> to 1709 pages leaked, all of them migrated.
>
> Fixes: 88d6f128d06d ("mm: track DONTCACHE dirty pages per bdi_writeback")
> Cc: stable@vger.kernel.org
> Signed-off-by: Usama Arif <usama.arif@linux.dev>
> ---
> mm/migrate.c | 3 +++
> 1 file changed, 3 insertions(+)
Makes sense to me. Thanks.
Reviewed-by: Zi Yan <ziy@nvidia.com>
>
> diff --git a/mm/migrate.c b/mm/migrate.c
> index 96b66a272379f9..81a8e46778610f 100644
> --- a/mm/migrate.c
> +++ b/mm/migrate.c
> @@ -663,6 +663,9 @@ static int __folio_migrate_mapping(struct address_space *mapping,
> folio_clear_dirty(folio);
> folio_set_dirty(newfolio);
> }
> + /* WB_DONTCACHE_DIRTY counts pages that are both dirty and dropbehind */
> + if (folio_test_dropbehind(folio))
> + folio_set_dropbehind(newfolio);
>
> if (folio_test_swapcache(folio))
> __swap_cache_replace_folio(ci, folio, newfolio);
This "test then set" pattern fills the most of
__folio_migrate_mapping(), I wonder if we can do that in bulk in a
follow-up cleanup.
--
Best Regards,
Yan, Zi
^ permalink raw reply [flat|nested] 21+ messages in thread* Re: [PATCH 1/4] mm/migrate: preserve PG_dropbehind across folio migration
2026-12-02 14:24 ` [PATCH 1/4] mm/migrate: preserve PG_dropbehind across folio migration Usama Arif
` (2 preceding siblings ...)
2026-12-02 15:03 ` Zi Yan
@ 2026-12-02 15:08 ` David Hildenbrand (Arm)
2026-12-02 15:18 ` Usama Arif
3 siblings, 1 reply; 21+ messages in thread
From: David Hildenbrand (Arm) @ 2026-12-02 15:08 UTC (permalink / raw)
To: Usama Arif, Andrew Morton, willy, jack, axboe, Christoph Hellwig,
hannes, jlayton, brauner, tz2294, ritesh.list, chentaotao, ziy,
bigeasy, liam, vbabka, rppt, mhocko, apopple, byungchul, gourry,
joshua.hahnjy, matthew.brost, ying.huang, clrkwllms, rostedt,
linux-mm, linux-fsdevel, linux-rt-devel, kas, riel, shakeel.butt,
kernel-team
Cc: stable
On 12/2/26 15:24, Usama Arif wrote:
> Migration moves PG_dirty to the new folio but not PG_dropbehind, so a
> migrated dirty dropbehind folio is cleaned without decrementing
> WB_DONTCACHE_DIRTY. The counter leaks, and every later RWF_DONTCACHE
> kick asks the flusher for that many more pages. Copy the flag along with
> PG_dirty, before the new folio becomes visible.
>
> With RWF_DONTCACHE writes and compaction running for five minutes, 1313
> to 1709 pages leaked, all of them migrated.
>
> Fixes: 88d6f128d06d ("mm: track DONTCACHE dirty pages per bdi_writeback")
> Cc: stable@vger.kernel.org
> Signed-off-by: Usama Arif <usama.arif@linux.dev>
> ---
> mm/migrate.c | 3 +++
> 1 file changed, 3 insertions(+)
>
> diff --git a/mm/migrate.c b/mm/migrate.c
> index 96b66a272379f9..81a8e46778610f 100644
> --- a/mm/migrate.c
> +++ b/mm/migrate.c
> @@ -663,6 +663,9 @@ static int __folio_migrate_mapping(struct address_space *mapping,
> folio_clear_dirty(folio);
> folio_set_dirty(newfolio);
> }
> + /* WB_DONTCACHE_DIRTY counts pages that are both dirty and dropbehind */
> + if (folio_test_dropbehind(folio))
> + folio_set_dropbehind(newfolio);
I think I assigned Jan a Sashiko report about this yesterday :)
Reviewed-by: David Hildenbrand (Arm) <david@kernel.org>
I assume I can pick this independent of the other ones (this through my tree,
the other stuff through Andrews tree)?
--
Cheers,
David
^ permalink raw reply [flat|nested] 21+ messages in thread* Re: [PATCH 1/4] mm/migrate: preserve PG_dropbehind across folio migration
2026-12-02 15:08 ` David Hildenbrand (Arm)
@ 2026-12-02 15:18 ` Usama Arif
0 siblings, 0 replies; 21+ messages in thread
From: Usama Arif @ 2026-12-02 15:18 UTC (permalink / raw)
To: David Hildenbrand (Arm)
Cc: Usama Arif, Andrew Morton, willy, jack, axboe, Christoph Hellwig,
hannes, jlayton, brauner, tz2294, ritesh.list, chentaotao, ziy,
bigeasy, liam, vbabka, rppt, mhocko, apopple, byungchul, gourry,
joshua.hahnjy, matthew.brost, ying.huang, clrkwllms, rostedt,
linux-mm, linux-fsdevel, linux-rt-devel, kas, riel, shakeel.butt,
kernel-team, stable
On Wed, 2 Dec 2026 16:08:05 +0100 "David Hildenbrand (Arm)" <david@kernel.org> wrote:
> On 12/2/26 15:24, Usama Arif wrote:
> > Migration moves PG_dirty to the new folio but not PG_dropbehind, so a
> > migrated dirty dropbehind folio is cleaned without decrementing
> > WB_DONTCACHE_DIRTY. The counter leaks, and every later RWF_DONTCACHE
> > kick asks the flusher for that many more pages. Copy the flag along with
> > PG_dirty, before the new folio becomes visible.
> >
> > With RWF_DONTCACHE writes and compaction running for five minutes, 1313
> > to 1709 pages leaked, all of them migrated.
> >
> > Fixes: 88d6f128d06d ("mm: track DONTCACHE dirty pages per bdi_writeback")
> > Cc: stable@vger.kernel.org
> > Signed-off-by: Usama Arif <usama.arif@linux.dev>
> > ---
> > mm/migrate.c | 3 +++
> > 1 file changed, 3 insertions(+)
> >
> > diff --git a/mm/migrate.c b/mm/migrate.c
> > index 96b66a272379f9..81a8e46778610f 100644
> > --- a/mm/migrate.c
> > +++ b/mm/migrate.c
> > @@ -663,6 +663,9 @@ static int __folio_migrate_mapping(struct address_space *mapping,
> > folio_clear_dirty(folio);
> > folio_set_dirty(newfolio);
> > }
> > + /* WB_DONTCACHE_DIRTY counts pages that are both dirty and dropbehind */
> > + if (folio_test_dropbehind(folio))
> > + folio_set_dropbehind(newfolio);
>
> I think I assigned Jan a Sashiko report about this yesterday :)
>
> Reviewed-by: David Hildenbrand (Arm) <david@kernel.org>
>
> I assume I can pick this independent of the other ones (this through my tree,
> the other stuff through Andrews tree)?
Thanks for the review!
Yes, the 3 fixes are independent of each other. They should go in before
patch 4 though, as patch 4 makes it more likely to hit these bugs.
>
> --
> Cheers,
>
> David
>
^ permalink raw reply [flat|nested] 21+ messages in thread
* [PATCH 2/4] mm/filemap: clear PG_dropbehind on lookup under the folio lock
2026-12-02 14:24 [PATCH 0/4] mm: drop folios under writeback on POSIX_FADV_DONTNEED Usama Arif
2026-12-02 14:24 ` [PATCH 1/4] mm/migrate: preserve PG_dropbehind across folio migration Usama Arif
@ 2026-12-02 14:24 ` Usama Arif
2026-12-02 14:42 ` Jan Kara
` (2 more replies)
2026-12-02 14:24 ` [PATCH 3/4] mm/filemap: don't drop dropbehind folios with interrupts disabled Usama Arif
` (2 subsequent siblings)
4 siblings, 3 replies; 21+ messages in thread
From: Usama Arif @ 2026-12-02 14:24 UTC (permalink / raw)
To: Andrew Morton, willy, jack, david, axboe, Christoph Hellwig,
hannes, jlayton, brauner, tz2294, ritesh.list, chentaotao, ziy,
bigeasy, liam, vbabka, rppt, mhocko, apopple, byungchul, gourry,
joshua.hahnjy, matthew.brost, ying.huang, clrkwllms, rostedt,
linux-mm, linux-fsdevel, linux-rt-devel, kas, riel, shakeel.butt,
kernel-team
Cc: Usama Arif, stable
Lookups without FGP_LOCK clear PG_dropbehind and then test PG_dirty to
adjust WB_DONTCACHE_DIRTY without holding the folio lock, so they race
with dirtying and cleaning and the counter drifts. Do it under the folio
lock and inside the wb transaction, and leave the flag set if the lock
is contended.
With a 50us delay widening the race, the counter was left at 25 to 522
pages after sync in three five-minute runs; with this it was 0.
Fixes: 88d6f128d06d ("mm: track DONTCACHE dirty pages per bdi_writeback")
Cc: stable@vger.kernel.org
Signed-off-by: Usama Arif <usama.arif@linux.dev>
---
mm/filemap.c | 25 +++++++++++++++++++------
1 file changed, 19 insertions(+), 6 deletions(-)
diff --git a/mm/filemap.c b/mm/filemap.c
index 64eefcdb24b393..a49cca600f8c3e 100644
--- a/mm/filemap.c
+++ b/mm/filemap.c
@@ -2104,19 +2104,32 @@ 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)) {
+ /*
+ * Not an uncached lookup, clear uncached if set. Dirtying and cleaning
+ * normally hold the folio lock; take it to keep WB_DONTCACHE_DIRTY in
+ * sync.
+ */
+ if (!(fgp_flags & FGP_DONTCACHE) && folio_test_dropbehind(folio)) {
+ bool locked = fgp_flags & FGP_LOCK;
+
+ if (!locked && !folio_trylock(folio))
+ return folio;
+ if (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);
+ if (folio_test_clear_dropbehind(folio) &&
+ folio_test_dirty(folio))
+ wb_stat_mod(wb, WB_DONTCACHE_DIRTY,
+ -folio_nr_pages(folio));
unlocked_inode_to_wb_end(inode, &cookie);
+ } else {
+ folio_clear_dropbehind(folio);
}
+ if (!locked)
+ folio_unlock(folio);
}
return folio;
}
--
2.53.1-Meta
^ permalink raw reply related [flat|nested] 21+ messages in thread* Re: [PATCH 2/4] mm/filemap: clear PG_dropbehind on lookup under the folio lock
2026-12-02 14:24 ` [PATCH 2/4] mm/filemap: clear PG_dropbehind on lookup under the folio lock Usama Arif
@ 2026-12-02 14:42 ` Jan Kara
2026-12-02 15:04 ` Jeff Layton
2026-12-02 16:20 ` Christoph Hellwig
2 siblings, 0 replies; 21+ messages in thread
From: Jan Kara @ 2026-12-02 14:42 UTC (permalink / raw)
To: Usama Arif
Cc: Andrew Morton, willy, jack, david, axboe, Christoph Hellwig,
hannes, jlayton, brauner, tz2294, ritesh.list, chentaotao, ziy,
bigeasy, liam, vbabka, rppt, mhocko, apopple, byungchul, gourry,
joshua.hahnjy, matthew.brost, ying.huang, clrkwllms, rostedt,
linux-mm, linux-fsdevel, linux-rt-devel, kas, riel, shakeel.butt,
kernel-team, stable
On Wed 02-12-26 06:24:37, Usama Arif wrote:
> Lookups without FGP_LOCK clear PG_dropbehind and then test PG_dirty to
> adjust WB_DONTCACHE_DIRTY without holding the folio lock, so they race
> with dirtying and cleaning and the counter drifts. Do it under the folio
> lock and inside the wb transaction, and leave the flag set if the lock
> is contended.
>
> With a 50us delay widening the race, the counter was left at 25 to 522
> pages after sync in three five-minute runs; with this it was 0.
>
> Fixes: 88d6f128d06d ("mm: track DONTCACHE dirty pages per bdi_writeback")
> Cc: stable@vger.kernel.org
> Signed-off-by: Usama Arif <usama.arif@linux.dev>
Looks good. Feel free to add:
Reviewed-by: Jan Kara <jack@suse.cz>
Although I have a nagging feeling I've seen a fix for this somewhere
already.
Honza
> ---
> mm/filemap.c | 25 +++++++++++++++++++------
> 1 file changed, 19 insertions(+), 6 deletions(-)
>
> diff --git a/mm/filemap.c b/mm/filemap.c
> index 64eefcdb24b393..a49cca600f8c3e 100644
> --- a/mm/filemap.c
> +++ b/mm/filemap.c
> @@ -2104,19 +2104,32 @@ 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)) {
> + /*
> + * Not an uncached lookup, clear uncached if set. Dirtying and cleaning
> + * normally hold the folio lock; take it to keep WB_DONTCACHE_DIRTY in
> + * sync.
> + */
> + if (!(fgp_flags & FGP_DONTCACHE) && folio_test_dropbehind(folio)) {
> + bool locked = fgp_flags & FGP_LOCK;
> +
> + if (!locked && !folio_trylock(folio))
> + return folio;
> + if (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);
> + if (folio_test_clear_dropbehind(folio) &&
> + folio_test_dirty(folio))
> + wb_stat_mod(wb, WB_DONTCACHE_DIRTY,
> + -folio_nr_pages(folio));
> unlocked_inode_to_wb_end(inode, &cookie);
> + } else {
> + folio_clear_dropbehind(folio);
> }
> + if (!locked)
> + folio_unlock(folio);
> }
> return folio;
> }
> --
> 2.53.1-Meta
>
--
Jan Kara <jack@suse.com>
SUSE Labs, CR
^ permalink raw reply [flat|nested] 21+ messages in thread* Re: [PATCH 2/4] mm/filemap: clear PG_dropbehind on lookup under the folio lock
2026-12-02 14:24 ` [PATCH 2/4] mm/filemap: clear PG_dropbehind on lookup under the folio lock Usama Arif
2026-12-02 14:42 ` Jan Kara
@ 2026-12-02 15:04 ` Jeff Layton
2026-12-02 16:20 ` Christoph Hellwig
2 siblings, 0 replies; 21+ messages in thread
From: Jeff Layton @ 2026-12-02 15:04 UTC (permalink / raw)
To: Usama Arif, Andrew Morton, willy, jack, david, axboe,
Christoph Hellwig, hannes, brauner, tz2294, ritesh.list,
chentaotao, ziy, bigeasy, liam, vbabka, rppt, mhocko, apopple,
byungchul, gourry, joshua.hahnjy, matthew.brost, ying.huang,
clrkwllms, rostedt, linux-mm, linux-fsdevel, linux-rt-devel, kas,
riel, shakeel.butt, kernel-team
Cc: stable
On Wed, 2026-12-02 at 06:24 -0800, Usama Arif wrote:
> Lookups without FGP_LOCK clear PG_dropbehind and then test PG_dirty to
> adjust WB_DONTCACHE_DIRTY without holding the folio lock, so they race
> with dirtying and cleaning and the counter drifts. Do it under the folio
> lock and inside the wb transaction, and leave the flag set if the lock
> is contended.
>
> With a 50us delay widening the race, the counter was left at 25 to 522
> pages after sync in three five-minute runs; with this it was 0.
>
> Fixes: 88d6f128d06d ("mm: track DONTCACHE dirty pages per bdi_writeback")
> Cc: stable@vger.kernel.org
> Signed-off-by: Usama Arif <usama.arif@linux.dev>
> ---
> mm/filemap.c | 25 +++++++++++++++++++------
> 1 file changed, 19 insertions(+), 6 deletions(-)
>
> diff --git a/mm/filemap.c b/mm/filemap.c
> index 64eefcdb24b393..a49cca600f8c3e 100644
> --- a/mm/filemap.c
> +++ b/mm/filemap.c
> @@ -2104,19 +2104,32 @@ 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)) {
> + /*
> + * Not an uncached lookup, clear uncached if set. Dirtying and cleaning
> + * normally hold the folio lock; take it to keep WB_DONTCACHE_DIRTY in
> + * sync.
> + */
> + if (!(fgp_flags & FGP_DONTCACHE) && folio_test_dropbehind(folio)) {
> + bool locked = fgp_flags & FGP_LOCK;
> +
> + if (!locked && !folio_trylock(folio))
> + return folio;
> + if (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);
> + if (folio_test_clear_dropbehind(folio) &&
> + folio_test_dirty(folio))
> + wb_stat_mod(wb, WB_DONTCACHE_DIRTY,
> + -folio_nr_pages(folio));
> unlocked_inode_to_wb_end(inode, &cookie);
> + } else {
> + folio_clear_dropbehind(folio);
> }
> + if (!locked)
> + folio_unlock(folio);
> }
> return folio;
> }
Reviewed-by: Jeff Layton <jlayton@kernel.org>
^ permalink raw reply [flat|nested] 21+ messages in thread* Re: [PATCH 2/4] mm/filemap: clear PG_dropbehind on lookup under the folio lock
2026-12-02 14:24 ` [PATCH 2/4] mm/filemap: clear PG_dropbehind on lookup under the folio lock Usama Arif
2026-12-02 14:42 ` Jan Kara
2026-12-02 15:04 ` Jeff Layton
@ 2026-12-02 16:20 ` Christoph Hellwig
2 siblings, 0 replies; 21+ messages in thread
From: Christoph Hellwig @ 2026-12-02 16:20 UTC (permalink / raw)
To: Usama Arif
Cc: Andrew Morton, willy, jack, david, axboe, Christoph Hellwig,
hannes, jlayton, brauner, tz2294, ritesh.list, chentaotao, ziy,
bigeasy, liam, vbabka, rppt, mhocko, apopple, byungchul, gourry,
joshua.hahnjy, matthew.brost, ying.huang, clrkwllms, rostedt,
linux-mm, linux-fsdevel, linux-rt-devel, kas, riel, shakeel.butt,
kernel-team, stable
On Wed, Dec 02, 2026 at 06:24:37AM -0800, Usama Arif wrote:
> + if (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);
> + if (folio_test_clear_dropbehind(folio) &&
> + folio_test_dirty(folio))
> + wb_stat_mod(wb, WB_DONTCACHE_DIRTY,
> + -folio_nr_pages(folio));
> unlocked_inode_to_wb_end(inode, &cookie);
> + } else {
Not urgent, but at some point it would be really nice to split
this logic into a helper as it really distracts from the main logic
in the function.
^ permalink raw reply [flat|nested] 21+ messages in thread
* [PATCH 3/4] mm/filemap: don't drop dropbehind folios with interrupts disabled
2026-12-02 14:24 [PATCH 0/4] mm: drop folios under writeback on POSIX_FADV_DONTNEED Usama Arif
2026-12-02 14:24 ` [PATCH 1/4] mm/migrate: preserve PG_dropbehind across folio migration Usama Arif
2026-12-02 14:24 ` [PATCH 2/4] mm/filemap: clear PG_dropbehind on lookup under the folio lock Usama Arif
@ 2026-12-02 14:24 ` Usama Arif
2026-12-02 14:51 ` Jan Kara
` (2 more replies)
2026-12-02 14:24 ` [PATCH 4/4] mm/fadvise: drop folios under writeback on POSIX_FADV_DONTNEED Usama Arif
2026-12-02 15:40 ` (subset) [PATCH 0/4] mm: " David Hildenbrand
4 siblings, 3 replies; 21+ messages in thread
From: Usama Arif @ 2026-12-02 14:24 UTC (permalink / raw)
To: Andrew Morton, willy, jack, david, axboe, Christoph Hellwig,
hannes, jlayton, brauner, tz2294, ritesh.list, chentaotao, ziy,
bigeasy, liam, vbabka, rppt, mhocko, apopple, byungchul, gourry,
joshua.hahnjy, matthew.brost, ying.huang, clrkwllms, rostedt,
linux-mm, linux-fsdevel, linux-rt-devel, kas, riel, shakeel.butt,
kernel-team
Cc: Usama Arif, stable
Forced-threaded interrupt handlers pass the in_task() check in
folio_end_dropbehind() but run with interrupts disabled on !PREEMPT_RT,
and invalidating the folio turns them back on. ext4 RWF_DONTCACHE
overwrites hit this with threadirqs until commit 7d7c9c83c822 ("ext4:
complete dropbehind writeback in task context"), and folios marked
dropbehind after their writeback was submitted still can.
Skip the drop when interrupts are disabled. The folio stays cached, as
it does when the completion runs in hardirq context.
Fixes: ae21c0c0ac56 ("ext4: support uncached buffered I/O")
Cc: stable@vger.kernel.org
Signed-off-by: Usama Arif <usama.arif@linux.dev>
---
mm/filemap.c | 5 +++--
1 file changed, 3 insertions(+), 2 deletions(-)
diff --git a/mm/filemap.c b/mm/filemap.c
index a49cca600f8c3e..6674c4ef8fe036 100644
--- a/mm/filemap.c
+++ b/mm/filemap.c
@@ -1663,9 +1663,10 @@ void folio_end_dropbehind(struct folio *folio)
* but can happen if normal writeback just happens to find dirty folios
* that were created as part of uncached writeback, and that writeback
* would otherwise not need non-IRQ handling. Just skip the
- * invalidation in that case.
+ * invalidation in that case, and also when interrupts are disabled,
+ * as they are in forced-threaded interrupt handlers.
*/
- if (in_task() && folio_trylock(folio)) {
+ if (in_task() && !irqs_disabled() && folio_trylock(folio)) {
filemap_end_dropbehind(folio);
folio_unlock(folio);
}
--
2.53.1-Meta
^ permalink raw reply related [flat|nested] 21+ messages in thread* Re: [PATCH 3/4] mm/filemap: don't drop dropbehind folios with interrupts disabled
2026-12-02 14:24 ` [PATCH 3/4] mm/filemap: don't drop dropbehind folios with interrupts disabled Usama Arif
@ 2026-12-02 14:51 ` Jan Kara
2026-12-02 15:04 ` Jeff Layton
2026-12-02 16:21 ` Christoph Hellwig
2 siblings, 0 replies; 21+ messages in thread
From: Jan Kara @ 2026-12-02 14:51 UTC (permalink / raw)
To: Usama Arif
Cc: Andrew Morton, willy, jack, david, axboe, Christoph Hellwig,
hannes, jlayton, brauner, tz2294, ritesh.list, chentaotao, ziy,
bigeasy, liam, vbabka, rppt, mhocko, apopple, byungchul, gourry,
joshua.hahnjy, matthew.brost, ying.huang, clrkwllms, rostedt,
linux-mm, linux-fsdevel, linux-rt-devel, kas, riel, shakeel.butt,
kernel-team, stable
On Wed 02-12-26 06:24:38, Usama Arif wrote:
> Forced-threaded interrupt handlers pass the in_task() check in
> folio_end_dropbehind() but run with interrupts disabled on !PREEMPT_RT,
> and invalidating the folio turns them back on. ext4 RWF_DONTCACHE
> overwrites hit this with threadirqs until commit 7d7c9c83c822 ("ext4:
> complete dropbehind writeback in task context"), and folios marked
> dropbehind after their writeback was submitted still can.
>
> Skip the drop when interrupts are disabled. The folio stays cached, as
> it does when the completion runs in hardirq context.
>
> Fixes: ae21c0c0ac56 ("ext4: support uncached buffered I/O")
> Cc: stable@vger.kernel.org
> Signed-off-by: Usama Arif <usama.arif@linux.dev>
Ah, this checking for proper context is so subtle it makes me wanna cry.
Anyway the fix looks good. Feel free to add:
Reviewed-by: Jan Kara <jack@suse.cz>
Honza
> ---
> mm/filemap.c | 5 +++--
> 1 file changed, 3 insertions(+), 2 deletions(-)
>
> diff --git a/mm/filemap.c b/mm/filemap.c
> index a49cca600f8c3e..6674c4ef8fe036 100644
> --- a/mm/filemap.c
> +++ b/mm/filemap.c
> @@ -1663,9 +1663,10 @@ void folio_end_dropbehind(struct folio *folio)
> * but can happen if normal writeback just happens to find dirty folios
> * that were created as part of uncached writeback, and that writeback
> * would otherwise not need non-IRQ handling. Just skip the
> - * invalidation in that case.
> + * invalidation in that case, and also when interrupts are disabled,
> + * as they are in forced-threaded interrupt handlers.
> */
> - if (in_task() && folio_trylock(folio)) {
> + if (in_task() && !irqs_disabled() && folio_trylock(folio)) {
> filemap_end_dropbehind(folio);
> folio_unlock(folio);
> }
> --
> 2.53.1-Meta
>
--
Jan Kara <jack@suse.com>
SUSE Labs, CR
^ permalink raw reply [flat|nested] 21+ messages in thread* Re: [PATCH 3/4] mm/filemap: don't drop dropbehind folios with interrupts disabled
2026-12-02 14:24 ` [PATCH 3/4] mm/filemap: don't drop dropbehind folios with interrupts disabled Usama Arif
2026-12-02 14:51 ` Jan Kara
@ 2026-12-02 15:04 ` Jeff Layton
2026-12-02 16:21 ` Christoph Hellwig
2 siblings, 0 replies; 21+ messages in thread
From: Jeff Layton @ 2026-12-02 15:04 UTC (permalink / raw)
To: Usama Arif, Andrew Morton, willy, jack, david, axboe,
Christoph Hellwig, hannes, brauner, tz2294, ritesh.list,
chentaotao, ziy, bigeasy, liam, vbabka, rppt, mhocko, apopple,
byungchul, gourry, joshua.hahnjy, matthew.brost, ying.huang,
clrkwllms, rostedt, linux-mm, linux-fsdevel, linux-rt-devel, kas,
riel, shakeel.butt, kernel-team
Cc: stable
On Wed, 2026-12-02 at 06:24 -0800, Usama Arif wrote:
> Forced-threaded interrupt handlers pass the in_task() check in
> folio_end_dropbehind() but run with interrupts disabled on !PREEMPT_RT,
> and invalidating the folio turns them back on. ext4 RWF_DONTCACHE
> overwrites hit this with threadirqs until commit 7d7c9c83c822 ("ext4:
> complete dropbehind writeback in task context"), and folios marked
> dropbehind after their writeback was submitted still can.
>
> Skip the drop when interrupts are disabled. The folio stays cached, as
> it does when the completion runs in hardirq context.
>
> Fixes: ae21c0c0ac56 ("ext4: support uncached buffered I/O")
> Cc: stable@vger.kernel.org
> Signed-off-by: Usama Arif <usama.arif@linux.dev>
> ---
> mm/filemap.c | 5 +++--
> 1 file changed, 3 insertions(+), 2 deletions(-)
>
> diff --git a/mm/filemap.c b/mm/filemap.c
> index a49cca600f8c3e..6674c4ef8fe036 100644
> --- a/mm/filemap.c
> +++ b/mm/filemap.c
> @@ -1663,9 +1663,10 @@ void folio_end_dropbehind(struct folio *folio)
> * but can happen if normal writeback just happens to find dirty folios
> * that were created as part of uncached writeback, and that writeback
> * would otherwise not need non-IRQ handling. Just skip the
> - * invalidation in that case.
> + * invalidation in that case, and also when interrupts are disabled,
> + * as they are in forced-threaded interrupt handlers.
> */
> - if (in_task() && folio_trylock(folio)) {
> + if (in_task() && !irqs_disabled() && folio_trylock(folio)) {
> filemap_end_dropbehind(folio);
> folio_unlock(folio);
> }
Good catch!
Reviewed-by: Jeff Layton <jlayton@kernel.org>
^ permalink raw reply [flat|nested] 21+ messages in thread* Re: [PATCH 3/4] mm/filemap: don't drop dropbehind folios with interrupts disabled
2026-12-02 14:24 ` [PATCH 3/4] mm/filemap: don't drop dropbehind folios with interrupts disabled Usama Arif
2026-12-02 14:51 ` Jan Kara
2026-12-02 15:04 ` Jeff Layton
@ 2026-12-02 16:21 ` Christoph Hellwig
2026-12-02 17:50 ` Usama Arif
2 siblings, 1 reply; 21+ messages in thread
From: Christoph Hellwig @ 2026-12-02 16:21 UTC (permalink / raw)
To: Usama Arif
Cc: Andrew Morton, willy, jack, david, axboe, Christoph Hellwig,
hannes, jlayton, brauner, tz2294, ritesh.list, chentaotao, ziy,
bigeasy, liam, vbabka, rppt, mhocko, apopple, byungchul, gourry,
joshua.hahnjy, matthew.brost, ying.huang, clrkwllms, rostedt,
linux-mm, linux-fsdevel, linux-rt-devel, kas, riel, shakeel.butt,
kernel-team, stable
On Wed, Dec 02, 2026 at 06:24:38AM -0800, Usama Arif wrote:
> - if (in_task() && folio_trylock(folio)) {
> + if (in_task() && !irqs_disabled() && folio_trylock(folio)) {
I wonder if this should use similar logic to bio_in_atomic().
^ permalink raw reply [flat|nested] 21+ messages in thread* Re: [PATCH 3/4] mm/filemap: don't drop dropbehind folios with interrupts disabled
2026-12-02 16:21 ` Christoph Hellwig
@ 2026-12-02 17:50 ` Usama Arif
0 siblings, 0 replies; 21+ messages in thread
From: Usama Arif @ 2026-12-02 17:50 UTC (permalink / raw)
To: Christoph Hellwig
Cc: Andrew Morton, willy, jack, david, axboe, hannes, jlayton,
brauner, tz2294, ritesh.list, chentaotao, ziy, bigeasy, liam,
vbabka, rppt, mhocko, apopple, byungchul, gourry, joshua.hahnjy,
matthew.brost, ying.huang, clrkwllms, rostedt, linux-mm,
linux-fsdevel, linux-rt-devel, kas, riel, shakeel.butt,
kernel-team, stable
On 02/12/2026 08:21, Christoph Hellwig wrote:
> On Wed, Dec 02, 2026 at 06:24:38AM -0800, Usama Arif wrote:
>> - if (in_task() && folio_trylock(folio)) {
>> + if (in_task() && !irqs_disabled() && folio_trylock(folio)) {
>
> I wonder if this should use similar logic to bio_in_atomic().
I don't think it fits here. bio_in_atomic() answers whether the
completion may sleep, and says yes whenever it can't tell, which is
the safe default for deciding to defer. The drop doesn't sleep: dirty
folios are skipped, mapped ones are kept, and filemap_release_folio()
gets no gfp flags. It only needs task context with interrupts on,
because folio_unmap_invalidate() takes the i_pages lock with
xa_lock_irq(), so preemption being disabled doesn't matter.
Copying bio_in_atomic() would also stop the drop entirely without
CONFIG_PREEMPT_COUNT, which is still possible with ARCH_NO_PREEMPT:
there it returns true even in the workqueue that BIO_COMPLETE_IN_TASK
completes from, so RWF_DONTCACHE writes would never be dropped.
^ permalink raw reply [flat|nested] 21+ messages in thread
* [PATCH 4/4] mm/fadvise: drop folios under writeback on POSIX_FADV_DONTNEED
2026-12-02 14:24 [PATCH 0/4] mm: drop folios under writeback on POSIX_FADV_DONTNEED Usama Arif
` (2 preceding siblings ...)
2026-12-02 14:24 ` [PATCH 3/4] mm/filemap: don't drop dropbehind folios with interrupts disabled Usama Arif
@ 2026-12-02 14:24 ` Usama Arif
2026-12-02 15:18 ` Jeff Layton
2026-12-02 16:26 ` Christoph Hellwig
2026-12-02 15:40 ` (subset) [PATCH 0/4] mm: " David Hildenbrand
4 siblings, 2 replies; 21+ messages in thread
From: Usama Arif @ 2026-12-02 14:24 UTC (permalink / raw)
To: Andrew Morton, willy, jack, david, axboe, Christoph Hellwig,
hannes, jlayton, brauner, tz2294, ritesh.list, chentaotao, ziy,
bigeasy, liam, vbabka, rppt, mhocko, apopple, byungchul, gourry,
joshua.hahnjy, matthew.brost, ying.huang, clrkwllms, rostedt,
linux-mm, linux-fsdevel, linux-rt-devel, kas, riel, shakeel.butt,
kernel-team
Cc: Usama Arif
POSIX_FADV_DONTNEED starts writeback on dirty folios but skips folios
under writeback when invalidating. With large folios one writeback bio
can hold hundreds of megabytes, none of which leaves writeback until all
of it completes, so on XFS DONTNEED right after writing a new 256 MiB
file left all of it cached. Syncing first avoids this but blocks the
caller, and RWF_DONTCACHE does not help writers using a shared mapping.
Mark the unmapped dirty folios in the range dropbehind before starting
writeback, and the unmapped writeback folios after it, so that writeback
completion drops them as it does for RWF_DONTCACHE writes. Marking the
dirty ones before submission lets their writeback complete in task
context, where they can be dropped. Folios that were already under
writeback and complete in interrupt context stay cached.
Skip the folio extending past EOF, which an appender calling DONTNEED
after each write would otherwise have to read back. Only do this for
regular files on filesystems that support RWF_DONTCACHE: those are
prepared to drop folios at writeback completion, and unlike block
devices, regular files rarely dirty an unmapped folio without its lock,
which would race with the WB_DONTCACHE_DIRTY accounting.
On XFS and ext4, new files and overwrites, written with write() or
through a shared mapping, were then dropped within a second, apart from
the EOF folio; before, most stayed cached.
Signed-off-by: Usama Arif <usama.arif@linux.dev>
---
mm/fadvise.c | 60 +++++++++++++++++++++++++++++++++++++++++----
mm/internal.h | 1 +
mm/page-writeback.c | 23 +++++++++++++++++
3 files changed, 79 insertions(+), 5 deletions(-)
diff --git a/mm/fadvise.c b/mm/fadvise.c
index b63fe21416ff21..6e7b86f072fe84 100644
--- a/mm/fadvise.c
+++ b/mm/fadvise.c
@@ -13,6 +13,7 @@
#include <linux/fs.h>
#include <linux/mm.h>
#include <linux/pagemap.h>
+#include <linux/folio_batch.h>
#include <linux/backing-dev.h>
#include <linux/fadvise.h>
#include <linux/writeback.h>
@@ -28,6 +29,43 @@
* deactivate the pages and clear PG_Referenced.
*/
+/*
+ * Folios under writeback cannot be invalidated. Mark the unmapped dirty and
+ * writeback ones dropbehind so that writeback completion drops them, as for
+ * RWF_DONTCACHE writes. Only filesystems that support those can do that.
+ */
+static void fadvise_mark_dropbehind(struct address_space *mapping,
+ pgoff_t start, pgoff_t end, xa_mark_t tag)
+{
+ loff_t isize = i_size_read(mapping->host);
+ struct folio_batch fbatch;
+ pgoff_t index = start;
+ unsigned int i;
+
+ folio_batch_init(&fbatch);
+ while (filemap_get_folios_tag(mapping, &index, end, tag, &fbatch)) {
+ for (i = 0; i < folio_batch_count(&fbatch); i++) {
+ struct folio *folio = fbatch.folios[i];
+
+ /* Don't mark the EOF folio, appenders write it again */
+ if (folio->index < start ||
+ folio_next_index(folio) - 1 > end ||
+ folio_next_pos(folio) > isize)
+ continue;
+ if (folio_test_dropbehind(folio) ||
+ !folio_trylock(folio))
+ continue;
+ if (folio->mapping == mapping && !folio_mapped(folio) &&
+ (folio_test_dirty(folio) ||
+ folio_test_writeback(folio)))
+ folio_mark_dropbehind(folio);
+ folio_unlock(folio);
+ }
+ folio_batch_release(&fbatch);
+ cond_resched();
+ }
+}
+
int generic_fadvise(struct file *file, loff_t offset, loff_t len, int advice)
{
struct inode *inode;
@@ -37,6 +75,7 @@ int generic_fadvise(struct file *file, loff_t offset, loff_t len, int advice)
pgoff_t start_index;
pgoff_t end_index;
unsigned long nrpages;
+ bool dropbehind;
inode = file_inode(file);
if (S_ISFIFO(inode->i_mode))
@@ -111,8 +150,6 @@ int generic_fadvise(struct file *file, loff_t offset, loff_t len, int advice)
spin_unlock(&file->f_lock);
break;
case POSIX_FADV_DONTNEED:
- filemap_flush_range(mapping, offset, endbyte);
-
/*
* First and last FULL page! Partial pages are deliberately
* preserved on the expectation that it is better to preserve
@@ -135,14 +172,27 @@ int generic_fadvise(struct file *file, loff_t offset, loff_t len, int advice)
* file cache which is not what was asked.
*/
if (end_index == 0)
- break;
-
- end_index--;
+ start_index = 1;
+ else
+ end_index--;
}
+ dropbehind = end_index >= start_index &&
+ S_ISREG(inode->i_mode) &&
+ (file->f_op->fop_flags & FOP_DONTCACHE);
+ if (dropbehind)
+ fadvise_mark_dropbehind(mapping, start_index, end_index,
+ PAGECACHE_TAG_DIRTY);
+
+ filemap_flush_range(mapping, offset, endbyte);
+
if (end_index >= start_index) {
unsigned long nr_failed = 0;
+ if (dropbehind)
+ fadvise_mark_dropbehind(mapping, start_index,
+ end_index, PAGECACHE_TAG_WRITEBACK);
+
/*
* It's common to FADV_DONTNEED right after
* the read or write that instantiates the
diff --git a/mm/internal.h b/mm/internal.h
index 8942e57744a96e..7a64b5c27f3064 100644
--- a/mm/internal.h
+++ b/mm/internal.h
@@ -626,6 +626,7 @@ static inline void hugetlb_folio_restore_rsv_on_unmap(struct vm_area_struct *vma
#endif
int folio_unmap_invalidate(struct address_space *mapping, struct folio *folio,
gfp_t gfp);
+void folio_mark_dropbehind(struct folio *folio);
void page_cache_ra_order(struct readahead_control *, struct file_ra_state *);
void force_page_cache_ra(struct readahead_control *, unsigned long nr);
diff --git a/mm/page-writeback.c b/mm/page-writeback.c
index 5939f781fc5402..13f4408f068d5e 100644
--- a/mm/page-writeback.c
+++ b/mm/page-writeback.c
@@ -2654,6 +2654,29 @@ void folio_account_cleaned(struct folio *folio, struct bdi_writeback *wb)
task_io_account_cancelled_write(nr * PAGE_SIZE);
}
+/*
+ * Mark a locked page cache folio dropbehind, and account it in
+ * WB_DONTCACHE_DIRTY if it is dirty.
+ */
+void folio_mark_dropbehind(struct folio *folio)
+{
+ struct address_space *mapping = folio->mapping;
+ struct inode *inode = mapping->host;
+ struct wb_lock_cookie cookie = {};
+ struct bdi_writeback *wb;
+
+ VM_WARN_ON_ONCE_FOLIO(!folio_test_locked(folio), folio);
+
+ if (folio_test_dropbehind(folio))
+ return;
+
+ wb = unlocked_inode_to_wb_begin(inode, &cookie);
+ folio_set_dropbehind(folio);
+ if (folio_test_dirty(folio) && mapping_can_writeback(mapping))
+ wb_stat_mod(wb, WB_DONTCACHE_DIRTY, folio_nr_pages(folio));
+ unlocked_inode_to_wb_end(inode, &cookie);
+}
+
/*
* Mark the folio dirty, and set it dirty in the page cache.
*
--
2.53.1-Meta
^ permalink raw reply related [flat|nested] 21+ messages in thread* Re: [PATCH 4/4] mm/fadvise: drop folios under writeback on POSIX_FADV_DONTNEED
2026-12-02 14:24 ` [PATCH 4/4] mm/fadvise: drop folios under writeback on POSIX_FADV_DONTNEED Usama Arif
@ 2026-12-02 15:18 ` Jeff Layton
2026-12-02 16:26 ` Christoph Hellwig
1 sibling, 0 replies; 21+ messages in thread
From: Jeff Layton @ 2026-12-02 15:18 UTC (permalink / raw)
To: Usama Arif, Andrew Morton, willy, jack, david, axboe,
Christoph Hellwig, hannes, brauner, tz2294, ritesh.list,
chentaotao, ziy, bigeasy, liam, vbabka, rppt, mhocko, apopple,
byungchul, gourry, joshua.hahnjy, matthew.brost, ying.huang,
clrkwllms, rostedt, linux-mm, linux-fsdevel, linux-rt-devel, kas,
riel, shakeel.butt, kernel-team
On Wed, 2026-12-02 at 06:24 -0800, Usama Arif wrote:
> POSIX_FADV_DONTNEED starts writeback on dirty folios but skips folios
> under writeback when invalidating. With large folios one writeback bio
> can hold hundreds of megabytes, none of which leaves writeback until all
> of it completes, so on XFS DONTNEED right after writing a new 256 MiB
> file left all of it cached. Syncing first avoids this but blocks the
> caller, and RWF_DONTCACHE does not help writers using a shared mapping.
>
> Mark the unmapped dirty folios in the range dropbehind before starting
> writeback, and the unmapped writeback folios after it, so that writeback
> completion drops them as it does for RWF_DONTCACHE writes. Marking the
> dirty ones before submission lets their writeback complete in task
> context, where they can be dropped. Folios that were already under
> writeback and complete in interrupt context stay cached.
>
> Skip the folio extending past EOF, which an appender calling DONTNEED
> after each write would otherwise have to read back. Only do this for
> regular files on filesystems that support RWF_DONTCACHE: those are
> prepared to drop folios at writeback completion, and unlike block
> devices, regular files rarely dirty an unmapped folio without its lock,
> which would race with the WB_DONTCACHE_DIRTY accounting.
>
> On XFS and ext4, new files and overwrites, written with write() or
> through a shared mapping, were then dropped within a second, apart from
> the EOF folio; before, most stayed cached.
>
> Signed-off-by: Usama Arif <usama.arif@linux.dev>
> ---
> mm/fadvise.c | 60 +++++++++++++++++++++++++++++++++++++++++----
> mm/internal.h | 1 +
> mm/page-writeback.c | 23 +++++++++++++++++
> 3 files changed, 79 insertions(+), 5 deletions(-)
>
> diff --git a/mm/fadvise.c b/mm/fadvise.c
> index b63fe21416ff21..6e7b86f072fe84 100644
> --- a/mm/fadvise.c
> +++ b/mm/fadvise.c
> @@ -13,6 +13,7 @@
> #include <linux/fs.h>
> #include <linux/mm.h>
> #include <linux/pagemap.h>
> +#include <linux/folio_batch.h>
> #include <linux/backing-dev.h>
> #include <linux/fadvise.h>
> #include <linux/writeback.h>
> @@ -28,6 +29,43 @@
> * deactivate the pages and clear PG_Referenced.
> */
>
> +/*
> + * Folios under writeback cannot be invalidated. Mark the unmapped dirty and
> + * writeback ones dropbehind so that writeback completion drops them, as for
> + * RWF_DONTCACHE writes. Only filesystems that support those can do that.
> + */
> +static void fadvise_mark_dropbehind(struct address_space *mapping,
> + pgoff_t start, pgoff_t end, xa_mark_t tag)
> +{
> + loff_t isize = i_size_read(mapping->host);
> + struct folio_batch fbatch;
> + pgoff_t index = start;
> + unsigned int i;
> +
> + folio_batch_init(&fbatch);
> + while (filemap_get_folios_tag(mapping, &index, end, tag, &fbatch)) {
> + for (i = 0; i < folio_batch_count(&fbatch); i++) {
> + struct folio *folio = fbatch.folios[i];
> +
> + /* Don't mark the EOF folio, appenders write it again */
> + if (folio->index < start ||
> + folio_next_index(folio) - 1 > end ||
> + folio_next_pos(folio) > isize)
> + continue;
> + if (folio_test_dropbehind(folio) ||
> + !folio_trylock(folio))
> + continue;
> + if (folio->mapping == mapping && !folio_mapped(folio) &&
> + (folio_test_dirty(folio) ||
> + folio_test_writeback(folio)))
> + folio_mark_dropbehind(folio);
> + folio_unlock(folio);
> + }
> + folio_batch_release(&fbatch);
> + cond_resched();
> + }
> +}
> +
> int generic_fadvise(struct file *file, loff_t offset, loff_t len, int advice)
> {
> struct inode *inode;
> @@ -37,6 +75,7 @@ int generic_fadvise(struct file *file, loff_t offset, loff_t len, int advice)
> pgoff_t start_index;
> pgoff_t end_index;
> unsigned long nrpages;
> + bool dropbehind;
>
> inode = file_inode(file);
> if (S_ISFIFO(inode->i_mode))
> @@ -111,8 +150,6 @@ int generic_fadvise(struct file *file, loff_t offset, loff_t len, int advice)
> spin_unlock(&file->f_lock);
> break;
> case POSIX_FADV_DONTNEED:
> - filemap_flush_range(mapping, offset, endbyte);
> -
> /*
> * First and last FULL page! Partial pages are deliberately
> * preserved on the expectation that it is better to preserve
> @@ -135,14 +172,27 @@ int generic_fadvise(struct file *file, loff_t offset, loff_t len, int advice)
> * file cache which is not what was asked.
> */
> if (end_index == 0)
> - break;
> -
> - end_index--;
> + start_index = 1;
> + else
> + end_index--;
> }
>
> + dropbehind = end_index >= start_index &&
> + S_ISREG(inode->i_mode) &&
> + (file->f_op->fop_flags & FOP_DONTCACHE);
> + if (dropbehind)
> + fadvise_mark_dropbehind(mapping, start_index, end_index,
> + PAGECACHE_TAG_DIRTY);
> +
> + filemap_flush_range(mapping, offset, endbyte);
> +
> if (end_index >= start_index) {
> unsigned long nr_failed = 0;
>
> + if (dropbehind)
> + fadvise_mark_dropbehind(mapping, start_index,
> + end_index, PAGECACHE_TAG_WRITEBACK);
> +
> /*
> * It's common to FADV_DONTNEED right after
> * the read or write that instantiates the
> diff --git a/mm/internal.h b/mm/internal.h
> index 8942e57744a96e..7a64b5c27f3064 100644
> --- a/mm/internal.h
> +++ b/mm/internal.h
> @@ -626,6 +626,7 @@ static inline void hugetlb_folio_restore_rsv_on_unmap(struct vm_area_struct *vma
> #endif
> int folio_unmap_invalidate(struct address_space *mapping, struct folio *folio,
> gfp_t gfp);
> +void folio_mark_dropbehind(struct folio *folio);
>
> void page_cache_ra_order(struct readahead_control *, struct file_ra_state *);
> void force_page_cache_ra(struct readahead_control *, unsigned long nr);
> diff --git a/mm/page-writeback.c b/mm/page-writeback.c
> index 5939f781fc5402..13f4408f068d5e 100644
> --- a/mm/page-writeback.c
> +++ b/mm/page-writeback.c
> @@ -2654,6 +2654,29 @@ void folio_account_cleaned(struct folio *folio, struct bdi_writeback *wb)
> task_io_account_cancelled_write(nr * PAGE_SIZE);
> }
>
> +/*
> + * Mark a locked page cache folio dropbehind, and account it in
> + * WB_DONTCACHE_DIRTY if it is dirty.
> + */
> +void folio_mark_dropbehind(struct folio *folio)
> +{
> + struct address_space *mapping = folio->mapping;
> + struct inode *inode = mapping->host;
> + struct wb_lock_cookie cookie = {};
> + struct bdi_writeback *wb;
> +
> + VM_WARN_ON_ONCE_FOLIO(!folio_test_locked(folio), folio);
> +
> + if (folio_test_dropbehind(folio))
> + return;
> +
> + wb = unlocked_inode_to_wb_begin(inode, &cookie);
> + folio_set_dropbehind(folio);
> + if (folio_test_dirty(folio) && mapping_can_writeback(mapping))
> + wb_stat_mod(wb, WB_DONTCACHE_DIRTY, folio_nr_pages(folio));
> + unlocked_inode_to_wb_end(inode, &cookie);
> +}
> +
> /*
> * Mark the folio dirty, and set it dirty in the page cache.
> *
I like this idea. I think that the only reason we don't mark these
dropbehind today is because all of the DONTCACHE code was added more
recently than the posix_fadvise() handling, and no one thought to do it
until now.
Reviewed-by: Jeff Layton <jlayton@kernel.org>
^ permalink raw reply [flat|nested] 21+ messages in thread* Re: [PATCH 4/4] mm/fadvise: drop folios under writeback on POSIX_FADV_DONTNEED
2026-12-02 14:24 ` [PATCH 4/4] mm/fadvise: drop folios under writeback on POSIX_FADV_DONTNEED Usama Arif
2026-12-02 15:18 ` Jeff Layton
@ 2026-12-02 16:26 ` Christoph Hellwig
2026-12-02 17:52 ` Usama Arif
1 sibling, 1 reply; 21+ messages in thread
From: Christoph Hellwig @ 2026-12-02 16:26 UTC (permalink / raw)
To: Usama Arif
Cc: Andrew Morton, willy, jack, david, axboe, Christoph Hellwig,
hannes, jlayton, brauner, tz2294, ritesh.list, chentaotao, ziy,
bigeasy, liam, vbabka, rppt, mhocko, apopple, byungchul, gourry,
joshua.hahnjy, matthew.brost, ying.huang, clrkwllms, rostedt,
linux-mm, linux-fsdevel, linux-rt-devel, kas, riel, shakeel.butt,
kernel-team
On Wed, Dec 02, 2026 at 06:24:39AM -0800, Usama Arif wrote:
> POSIX_FADV_DONTNEED starts writeback on dirty folios but skips folios
> under writeback when invalidating. With large folios one writeback bio
> can hold hundreds of megabytes, none of which leaves writeback until all
> of it completes, so on XFS DONTNEED right after writing a new 256 MiB
s/XFS/fadvise/ ?
^ permalink raw reply [flat|nested] 21+ messages in thread
* Re: [PATCH 4/4] mm/fadvise: drop folios under writeback on POSIX_FADV_DONTNEED
2026-12-02 16:26 ` Christoph Hellwig
@ 2026-12-02 17:52 ` Usama Arif
0 siblings, 0 replies; 21+ messages in thread
From: Usama Arif @ 2026-12-02 17:52 UTC (permalink / raw)
To: Christoph Hellwig
Cc: Andrew Morton, willy, jack, david, axboe, hannes, jlayton,
brauner, tz2294, ritesh.list, chentaotao, ziy, bigeasy, liam,
vbabka, rppt, mhocko, apopple, byungchul, gourry, joshua.hahnjy,
matthew.brost, ying.huang, clrkwllms, rostedt, linux-mm,
linux-fsdevel, linux-rt-devel, kas, riel, shakeel.butt,
kernel-team
On 02/12/2026 08:26, Christoph Hellwig wrote:
> On Wed, Dec 02, 2026 at 06:24:39AM -0800, Usama Arif wrote:
>> POSIX_FADV_DONTNEED starts writeback on dirty folios but skips folios
>> under writeback when invalidating. With large folios one writeback bio
>> can hold hundreds of megabytes, none of which leaves writeback until all
>> of it completes, so on XFS DONTNEED right after writing a new 256 MiB
>
> s/XFS/fadvise/ ?
XFS is the filesystem where we see this. "on XFS DONTNEED" reads
badly. I've changed it to "DONTNEED right after writing a new 256 MiB
file on XFS left all of it cached" if there is a v2.
Thanks for the review!
^ permalink raw reply [flat|nested] 21+ messages in thread
* Re: (subset) [PATCH 0/4] mm: drop folios under writeback on POSIX_FADV_DONTNEED
2026-12-02 14:24 [PATCH 0/4] mm: drop folios under writeback on POSIX_FADV_DONTNEED Usama Arif
` (3 preceding siblings ...)
2026-12-02 14:24 ` [PATCH 4/4] mm/fadvise: drop folios under writeback on POSIX_FADV_DONTNEED Usama Arif
@ 2026-12-02 15:40 ` David Hildenbrand
4 siblings, 0 replies; 21+ messages in thread
From: David Hildenbrand @ 2026-12-02 15:40 UTC (permalink / raw)
To: Andrew Morton, willy, jack, david, axboe, Christoph Hellwig,
hannes, jlayton, brauner, tz2294, ritesh.list, chentaotao, ziy,
bigeasy, liam, vbabka, rppt, mhocko, apopple, byungchul, gourry,
joshua.hahnjy, matthew.brost, ying.huang, clrkwllms, rostedt,
linux-mm, linux-fsdevel, linux-rt-devel, kas, riel, shakeel.butt,
kernel-team, Usama Arif
On Wed, 02 Dec 2026 06:24:35 -0800, Usama Arif wrote:
> POSIX_FADV_DONTNEED starts writeback on the dirty folios in the range
> without waiting for it, then invalidates what it can and skips folios
> under writeback. With small folios most bios complete while DONTNEED is
> still invalidating. With large folios on XFS a single bio can carry
> hundreds of megabytes, none of which leaves writeback until all of it
> completes, so DONTNEED right after writing a new 256 MiB file leaves all
> of it cached.
>
> [...]
Applied, thanks!
tree: mm/core.git
branch: migrate-7.5.misc
Applied patches will appear soon in mm-next, followed by linux-next.
Please report any outstanding bugs that were missed during review, so we
can either drop the patch series or decide how to fix it up.
It is encouraged to provide Acked-bys, Reviewed-bys, and Tested-bys even
though the patches have already been applied. If possible, patch trailers
will be updated.
Note that commit hashes shown below are subject to change due to rebase,
trailer updates or similar. Further, this is not a guarantee that the
patches will go upstream. If in doubt, please check the listed branch.
[1/4] mm/migrate: preserve PG_dropbehind across folio migration
https://git.kernel.org/mm/core/c/282fe7c7
--
Cheers,
David
^ permalink raw reply [flat|nested] 21+ messages in thread