* [PATCH] mm/madvise: swap in CoW'd MAP_PRIVATE-file mappings on MADV_WILLNEED
@ 2026-09-04 15:12 Lorenzo Stoakes (ARM)
2026-09-04 15:24 ` Pedro Falcato
0 siblings, 1 reply; 11+ messages in thread
From: Lorenzo Stoakes (ARM) @ 2026-09-04 15:12 UTC (permalink / raw)
To: Andrew Morton, Liam R. Howlett, David Hildenbrand,
Vlastimil Babka, Jann Horn
Cc: linux-mm, linux-kernel, Mike Kaplinskiy, zhaozhengzhuo,
Lorenzo Stoakes (ARM)
Currently MADV_WILLNEED treats file-backed and pure anonymous mappings
entirely separately - using POSIX_FADV_WILLNEED (equivalent of a readahead)
for the former and a tree walk and swap in to swap cache for the latter.
MAP_PRIVATE-file backed mappings straddle the two and currently get treated
as if they were purely file-backed, meaning any swapped out private pages
remain swapped out.
Resolve the issue by explicitly checking for CoW'd MAP_PRIVATE-file backed
mappings and performing both walks in this case.
Since the logic checks for vma->anon_vma this means un-CoW'd
MAP_PRIVATE-file backed mappings retain only the single file walk.
Reported-by: Mike Kaplinskiy <mike@recall.ai>
Closes: https://lore.kernel.org/all/CABeknB_S2XJSHFgnHdgnN0rjzHhH4oQJs_APq9fvxHztQ_pgiA@mail.gmail.com/
Signed-off-by: Lorenzo Stoakes (ARM) <ljs@kernel.org>
---
mm/madvise.c | 6 ++++--
1 file changed, 4 insertions(+), 2 deletions(-)
diff --git a/mm/madvise.c b/mm/madvise.c
index 73c2901b9adb..d0510dd49dde 100644
--- a/mm/madvise.c
+++ b/mm/madvise.c
@@ -297,10 +297,12 @@ static long madvise_willneed(struct madvise_behavior *madv_behavior)
loff_t offset;
#ifdef CONFIG_SWAP
- if (!file) {
+ if (!file || (vma_is_cow_mapping(vma) && vma->anon_vma)) {
walk_page_range_vma(vma, start, end, &swapin_walk_ops, vma);
lru_add_drain(); /* Push any new pages onto the LRU now */
- return 0;
+
+ if (!file)
+ return 0;
}
if (shmem_mapping(file->f_mapping)) {
---
base-commit: e3b5239afe1b8f0194db7436b17c33e94c1988c4
change-id: 20260903-madv-will-need-map-private-125e3e2f70c7
Best regards,
--
Lorenzo Stoakes (ARM) <ljs@kernel.org>
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [PATCH] mm/madvise: swap in CoW'd MAP_PRIVATE-file mappings on MADV_WILLNEED
2026-09-04 15:12 [PATCH] mm/madvise: swap in CoW'd MAP_PRIVATE-file mappings on MADV_WILLNEED Lorenzo Stoakes (ARM)
@ 2026-09-04 15:24 ` Pedro Falcato
2026-09-04 16:01 ` Lorenzo Stoakes (ARM)
0 siblings, 1 reply; 11+ messages in thread
From: Pedro Falcato @ 2026-09-04 15:24 UTC (permalink / raw)
To: Lorenzo Stoakes (ARM)
Cc: Andrew Morton, Liam R. Howlett, David Hildenbrand,
Vlastimil Babka, Jann Horn, linux-mm, linux-kernel,
Mike Kaplinskiy, zhaozhengzhuo
On Fri, Sep 04, 2026 at 04:12:53PM +0100, Lorenzo Stoakes (ARM) wrote:
> Currently MADV_WILLNEED treats file-backed and pure anonymous mappings
> entirely separately - using POSIX_FADV_WILLNEED (equivalent of a readahead)
> for the former and a tree walk and swap in to swap cache for the latter.
>
> MAP_PRIVATE-file backed mappings straddle the two and currently get treated
> as if they were purely file-backed, meaning any swapped out private pages
> remain swapped out.
>
> Resolve the issue by explicitly checking for CoW'd MAP_PRIVATE-file backed
> mappings and performing both walks in this case.
>
> Since the logic checks for vma->anon_vma this means un-CoW'd
> MAP_PRIVATE-file backed mappings retain only the single file walk.
>
> Reported-by: Mike Kaplinskiy <mike@recall.ai>
> Closes: https://lore.kernel.org/all/CABeknB_S2XJSHFgnHdgnN0rjzHhH4oQJs_APq9fvxHztQ_pgiA@mail.gmail.com/
> Signed-off-by: Lorenzo Stoakes (ARM) <ljs@kernel.org>
> ---
> mm/madvise.c | 6 ++++--
> 1 file changed, 4 insertions(+), 2 deletions(-)
>
> diff --git a/mm/madvise.c b/mm/madvise.c
> index 73c2901b9adb..d0510dd49dde 100644
> --- a/mm/madvise.c
> +++ b/mm/madvise.c
> @@ -297,10 +297,12 @@ static long madvise_willneed(struct madvise_behavior *madv_behavior)
> loff_t offset;
>
> #ifdef CONFIG_SWAP
> - if (!file) {
> + if (!file || (vma_is_cow_mapping(vma) && vma->anon_vma)) {
Couldn't this all be simplified to
if (vma->anon_vma) {
? swapin needs anon pages to have been faulted-in. Non-cow mappings won't
have an anon_vma, nor will fully empty anonymous VMAs (and that's fine).
Right?
Regardless, looks cromulent
Reviewed-by: Pedro Falcato <pfalcato@suse.de>
--
Pedro
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [PATCH] mm/madvise: swap in CoW'd MAP_PRIVATE-file mappings on MADV_WILLNEED
2026-09-04 15:24 ` Pedro Falcato
@ 2026-09-04 16:01 ` Lorenzo Stoakes (ARM)
2026-09-06 0:17 ` Andrew Morton
` (3 more replies)
0 siblings, 4 replies; 11+ messages in thread
From: Lorenzo Stoakes (ARM) @ 2026-09-04 16:01 UTC (permalink / raw)
To: Pedro Falcato
Cc: Andrew Morton, Liam R. Howlett, David Hildenbrand,
Vlastimil Babka, Jann Horn, linux-mm, linux-kernel,
Mike Kaplinskiy, zhaozhengzhuo
On Fri, Sep 04, 2026 at 04:24:59PM +0100, Pedro Falcato wrote:
> On Fri, Sep 04, 2026 at 04:12:53PM +0100, Lorenzo Stoakes (ARM) wrote:
> > Currently MADV_WILLNEED treats file-backed and pure anonymous mappings
> > entirely separately - using POSIX_FADV_WILLNEED (equivalent of a readahead)
> > for the former and a tree walk and swap in to swap cache for the latter.
> >
> > MAP_PRIVATE-file backed mappings straddle the two and currently get treated
> > as if they were purely file-backed, meaning any swapped out private pages
> > remain swapped out.
> >
> > Resolve the issue by explicitly checking for CoW'd MAP_PRIVATE-file backed
> > mappings and performing both walks in this case.
> >
> > Since the logic checks for vma->anon_vma this means un-CoW'd
> > MAP_PRIVATE-file backed mappings retain only the single file walk.
> >
> > Reported-by: Mike Kaplinskiy <mike@recall.ai>
> > Closes: https://lore.kernel.org/all/CABeknB_S2XJSHFgnHdgnN0rjzHhH4oQJs_APq9fvxHztQ_pgiA@mail.gmail.com/
> > Signed-off-by: Lorenzo Stoakes (ARM) <ljs@kernel.org>
> > ---
> > mm/madvise.c | 6 ++++--
> > 1 file changed, 4 insertions(+), 2 deletions(-)
> >
> > diff --git a/mm/madvise.c b/mm/madvise.c
> > index 73c2901b9adb..d0510dd49dde 100644
> > --- a/mm/madvise.c
> > +++ b/mm/madvise.c
> > @@ -297,10 +297,12 @@ static long madvise_willneed(struct madvise_behavior *madv_behavior)
> > loff_t offset;
> >
> > #ifdef CONFIG_SWAP
> > - if (!file) {
> > + if (!file || (vma_is_cow_mapping(vma) && vma->anon_vma)) {
>
> Couldn't this all be simplified to
>
> if (vma->anon_vma) {
>
> ? swapin needs anon pages to have been faulted-in. Non-cow mappings won't
> have an anon_vma, nor will fully empty anonymous VMAs (and that's fine).
> Right?
Hmm good point :)
Though the
if (!file)
return 0;
Would have to be outside of the block to avoid an anon unfaulted (nop) from
being skipped.
And it's a real improvement to have unfaulted anon skip...
But I find that version is documenting what's going on a lot less.
Since both anon and MAP_PRIVATE file-backed are CoW mappings we could just
reference that.
Andrew - could you swap the patch out in-place with below? Thanks!
Cheers, Lorenzo
----8<----
From a706374a1186b6b4409a0e760e8728bcadba0c96 Mon Sep 17 00:00:00 2001
From: "Lorenzo Stoakes (ARM)" <ljs@kernel.org>
Date: Thu, 3 Sep 2026 20:08:39 +0100
Subject: [PATCH] mm/madvise: swap in CoW'd MAP_PRIVATE-file mappings on
MADV_WILLNEED
Currently MADV_WILLNEED treats file-backed and pure anonymous mappings
entirely separately - using POSIX_FADV_WILLNEED (equivalent of a readahead)
for the former and a tree walk and swap in to swap cache for the latter.
MAP_PRIVATE-file backed mappings straddle the two and currently get treated
as if they were purely file-backed, meaning any swapped out private pages
remain swapped out.
Resolve the issue by explicitly checking for CoW'd MAP_PRIVATE-file backed
mappings and performing both walks in this case.
Since the logic checks for vma->anon_vma this means un-CoW'd
MAP_PRIVATE-file backed mappings retain only the single file walk.
Reported-by: Mike Kaplinskiy <mike@recall.ai>
Closes: https://lore.kernel.org/all/CABeknB_S2XJSHFgnHdgnN0rjzHhH4oQJs_APq9fvxHztQ_pgiA@mail.gmail.com/
Signed-off-by: Lorenzo Stoakes (ARM) <ljs@kernel.org>
---
mm/madvise.c | 5 +++--
1 file changed, 3 insertions(+), 2 deletions(-)
diff --git a/mm/madvise.c b/mm/madvise.c
index 73c2901b9adb..963337f93a7a 100644
--- a/mm/madvise.c
+++ b/mm/madvise.c
@@ -297,11 +297,12 @@ static long madvise_willneed(struct madvise_behavior *madv_behavior)
loff_t offset;
#ifdef CONFIG_SWAP
- if (!file) {
+ if (vma_is_cow_mapping(vma) && vma->anon_vma) {
walk_page_range_vma(vma, start, end, &swapin_walk_ops, vma);
lru_add_drain(); /* Push any new pages onto the LRU now */
- return 0;
}
+ if (!file)
+ return 0;
if (shmem_mapping(file->f_mapping)) {
shmem_swapin_range(vma, start, end, file->f_mapping);
--
2.55.0
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [PATCH] mm/madvise: swap in CoW'd MAP_PRIVATE-file mappings on MADV_WILLNEED
2026-09-04 16:01 ` Lorenzo Stoakes (ARM)
@ 2026-09-06 0:17 ` Andrew Morton
2026-09-07 12:32 ` Lorenzo Stoakes (ARM)
2026-09-07 11:51 ` David Hildenbrand (Arm)
` (2 subsequent siblings)
3 siblings, 1 reply; 11+ messages in thread
From: Andrew Morton @ 2026-09-06 0:17 UTC (permalink / raw)
To: Lorenzo Stoakes (ARM)
Cc: Pedro Falcato, Liam R. Howlett, David Hildenbrand,
Vlastimil Babka, Jann Horn, linux-mm, linux-kernel,
Mike Kaplinskiy, zhaozhengzhuo
On Fri, 4 Sep 2026 17:01:37 +0100 "Lorenzo Stoakes (ARM)" <ljs@kernel.org> wrote:
> Andrew - could you swap the patch out in-place with below? Thanks!
>
np.
> Currently MADV_WILLNEED treats file-backed and pure anonymous mappings
> entirely separately - using POSIX_FADV_WILLNEED (equivalent of a readahead)
> for the former and a tree walk and swap in to swap cache for the latter.
>
> MAP_PRIVATE-file backed mappings straddle the two and currently get treated
> as if they were purely file-backed, meaning any swapped out private pages
> remain swapped out.
>
> Resolve the issue by explicitly checking for CoW'd MAP_PRIVATE-file backed
> mappings and performing both walks in this case.
>
> Since the logic checks for vma->anon_vma this means un-CoW'd
> MAP_PRIVATE-file backed mappings retain only the single file walk.
>
> Reported-by: Mike Kaplinskiy <mike@recall.ai>
> Closes: https://lore.kernel.org/all/CABeknB_S2XJSHFgnHdgnN0rjzHhH4oQJs_APq9fvxHztQ_pgiA@mail.gmail.com/
> Signed-off-by: Lorenzo Stoakes (ARM) <ljs@kernel.org>
The failure mode looks pretty obscure, so I'm thinking that no backport
is needed.
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [PATCH] mm/madvise: swap in CoW'd MAP_PRIVATE-file mappings on MADV_WILLNEED
2026-09-04 16:01 ` Lorenzo Stoakes (ARM)
2026-09-06 0:17 ` Andrew Morton
@ 2026-09-07 11:51 ` David Hildenbrand (Arm)
2026-09-07 12:33 ` Lorenzo Stoakes (ARM)
2026-09-07 12:39 ` Pedro Falcato
2026-09-07 15:05 ` Vlastimil Babka (SUSE)
3 siblings, 1 reply; 11+ messages in thread
From: David Hildenbrand (Arm) @ 2026-09-07 11:51 UTC (permalink / raw)
To: Lorenzo Stoakes (ARM), Pedro Falcato
Cc: Andrew Morton, Liam R. Howlett, Vlastimil Babka, Jann Horn,
linux-mm, linux-kernel, Mike Kaplinskiy, zhaozhengzhuo
> And it's a real improvement to have unfaulted anon skip...
>
> But I find that version is documenting what's going on a lot less.
>
> Since both anon and MAP_PRIVATE file-backed are CoW mappings we could just
> reference that.
>
> Andrew - could you swap the patch out in-place with below? Thanks!
A proper v2 would have been nice ;)
(and would trigger sashiko and friends properly)
>
> Cheers, Lorenzo
>
> ----8<----
> From a706374a1186b6b4409a0e760e8728bcadba0c96 Mon Sep 17 00:00:00 2001
> From: "Lorenzo Stoakes (ARM)" <ljs@kernel.org>
> Date: Thu, 3 Sep 2026 20:08:39 +0100
> Subject: [PATCH] mm/madvise: swap in CoW'd MAP_PRIVATE-file mappings on
> MADV_WILLNEED
>
> Currently MADV_WILLNEED treats file-backed and pure anonymous mappings
> entirely separately - using POSIX_FADV_WILLNEED (equivalent of a readahead)
> for the former and a tree walk and swap in to swap cache for the latter.
>
> MAP_PRIVATE-file backed mappings straddle the two and currently get treated
> as if they were purely file-backed, meaning any swapped out private pages
> remain swapped out.
>
> Resolve the issue by explicitly checking for CoW'd MAP_PRIVATE-file backed
> mappings and performing both walks in this case.
>
> Since the logic checks for vma->anon_vma this means un-CoW'd
> MAP_PRIVATE-file backed mappings retain only the single file walk.
>
> Reported-by: Mike Kaplinskiy <mike@recall.ai>
> Closes: https://lore.kernel.org/all/CABeknB_S2XJSHFgnHdgnN0rjzHhH4oQJs_APq9fvxHztQ_pgiA@mail.gmail.com/
> Signed-off-by: Lorenzo Stoakes (ARM) <ljs@kernel.org>
> ---
> mm/madvise.c | 5 +++--
> 1 file changed, 3 insertions(+), 2 deletions(-)
>
> diff --git a/mm/madvise.c b/mm/madvise.c
> index 73c2901b9adb..963337f93a7a 100644
> --- a/mm/madvise.c
> +++ b/mm/madvise.c
> @@ -297,11 +297,12 @@ static long madvise_willneed(struct madvise_behavior *madv_behavior)
> loff_t offset;
>
> #ifdef CONFIG_SWAP
> - if (!file) {
> + if (vma_is_cow_mapping(vma) && vma->anon_vma) {
> walk_page_range_vma(vma, start, end, &swapin_walk_ops, vma);
> lru_add_drain(); /* Push any new pages onto the LRU now */
> - return 0;
> }
> + if (!file)
> + return 0;
>
LGTM
Acked-by: David Hildenbrand (Arm) <david@kernel.org>
--
Cheers,
David
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [PATCH] mm/madvise: swap in CoW'd MAP_PRIVATE-file mappings on MADV_WILLNEED
2026-09-06 0:17 ` Andrew Morton
@ 2026-09-07 12:32 ` Lorenzo Stoakes (ARM)
2026-09-07 15:09 ` Vlastimil Babka (SUSE)
0 siblings, 1 reply; 11+ messages in thread
From: Lorenzo Stoakes (ARM) @ 2026-09-07 12:32 UTC (permalink / raw)
To: Andrew Morton
Cc: Pedro Falcato, Liam R. Howlett, David Hildenbrand,
Vlastimil Babka, Jann Horn, linux-mm, linux-kernel,
Mike Kaplinskiy, zhaozhengzhuo
On Sat, Sep 05, 2026 at 05:17:57PM -0700, Andrew Morton wrote:
> On Fri, 4 Sep 2026 17:01:37 +0100 "Lorenzo Stoakes (ARM)" <ljs@kernel.org> wrote:
>
> > Andrew - could you swap the patch out in-place with below? Thanks!
> >
>
> np.
>
> > Currently MADV_WILLNEED treats file-backed and pure anonymous mappings
> > entirely separately - using POSIX_FADV_WILLNEED (equivalent of a readahead)
> > for the former and a tree walk and swap in to swap cache for the latter.
> >
> > MAP_PRIVATE-file backed mappings straddle the two and currently get treated
> > as if they were purely file-backed, meaning any swapped out private pages
> > remain swapped out.
> >
> > Resolve the issue by explicitly checking for CoW'd MAP_PRIVATE-file backed
> > mappings and performing both walks in this case.
> >
> > Since the logic checks for vma->anon_vma this means un-CoW'd
> > MAP_PRIVATE-file backed mappings retain only the single file walk.
> >
> > Reported-by: Mike Kaplinskiy <mike@recall.ai>
> > Closes: https://lore.kernel.org/all/CABeknB_S2XJSHFgnHdgnN0rjzHhH4oQJs_APq9fvxHztQ_pgiA@mail.gmail.com/
> > Signed-off-by: Lorenzo Stoakes (ARM) <ljs@kernel.org>
>
> The failure mode looks pretty obscure, so I'm thinking that no backport
> is needed.
Well it's not even really a failure mode so much as a 'known limitation' :)
So yeah agreed.
--
Cheers, Lorenzo
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [PATCH] mm/madvise: swap in CoW'd MAP_PRIVATE-file mappings on MADV_WILLNEED
2026-09-07 11:51 ` David Hildenbrand (Arm)
@ 2026-09-07 12:33 ` Lorenzo Stoakes (ARM)
0 siblings, 0 replies; 11+ messages in thread
From: Lorenzo Stoakes (ARM) @ 2026-09-07 12:33 UTC (permalink / raw)
To: David Hildenbrand (Arm)
Cc: Pedro Falcato, Andrew Morton, Liam R. Howlett, Vlastimil Babka,
Jann Horn, linux-mm, linux-kernel, Mike Kaplinskiy,
zhaozhengzhuo
On Mon, Sep 07, 2026 at 01:51:20PM +0200, David Hildenbrand (Arm) wrote:
> > And it's a real improvement to have unfaulted anon skip...
> >
> > But I find that version is documenting what's going on a lot less.
> >
> > Since both anon and MAP_PRIVATE file-backed are CoW mappings we could just
> > reference that.
> >
> > Andrew - could you swap the patch out in-place with below? Thanks!
>
> A proper v2 would have been nice ;)
>
> (and would trigger sashiko and friends properly)
Ack, though it's kinda obviously functionally equivalent (or so I felt :P).
I had a local LLM check it FWIW.
>
> >
> > Cheers, Lorenzo
> >
> > ----8<----
> > From a706374a1186b6b4409a0e760e8728bcadba0c96 Mon Sep 17 00:00:00 2001
> > From: "Lorenzo Stoakes (ARM)" <ljs@kernel.org>
> > Date: Thu, 3 Sep 2026 20:08:39 +0100
> > Subject: [PATCH] mm/madvise: swap in CoW'd MAP_PRIVATE-file mappings on
> > MADV_WILLNEED
> >
> > Currently MADV_WILLNEED treats file-backed and pure anonymous mappings
> > entirely separately - using POSIX_FADV_WILLNEED (equivalent of a readahead)
> > for the former and a tree walk and swap in to swap cache for the latter.
> >
> > MAP_PRIVATE-file backed mappings straddle the two and currently get treated
> > as if they were purely file-backed, meaning any swapped out private pages
> > remain swapped out.
> >
> > Resolve the issue by explicitly checking for CoW'd MAP_PRIVATE-file backed
> > mappings and performing both walks in this case.
> >
> > Since the logic checks for vma->anon_vma this means un-CoW'd
> > MAP_PRIVATE-file backed mappings retain only the single file walk.
> >
> > Reported-by: Mike Kaplinskiy <mike@recall.ai>
> > Closes: https://lore.kernel.org/all/CABeknB_S2XJSHFgnHdgnN0rjzHhH4oQJs_APq9fvxHztQ_pgiA@mail.gmail.com/
> > Signed-off-by: Lorenzo Stoakes (ARM) <ljs@kernel.org>
> > ---
> > mm/madvise.c | 5 +++--
> > 1 file changed, 3 insertions(+), 2 deletions(-)
> >
> > diff --git a/mm/madvise.c b/mm/madvise.c
> > index 73c2901b9adb..963337f93a7a 100644
> > --- a/mm/madvise.c
> > +++ b/mm/madvise.c
> > @@ -297,11 +297,12 @@ static long madvise_willneed(struct madvise_behavior *madv_behavior)
> > loff_t offset;
> >
> > #ifdef CONFIG_SWAP
> > - if (!file) {
> > + if (vma_is_cow_mapping(vma) && vma->anon_vma) {
> > walk_page_range_vma(vma, start, end, &swapin_walk_ops, vma);
> > lru_add_drain(); /* Push any new pages onto the LRU now */
> > - return 0;
> > }
> > + if (!file)
> > + return 0;
> >
> LGTM
>
> Acked-by: David Hildenbrand (Arm) <david@kernel.org>
Thanks!
>
> --
> Cheers,
>
> David
--
Cheers, Lorenzo
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [PATCH] mm/madvise: swap in CoW'd MAP_PRIVATE-file mappings on MADV_WILLNEED
2026-09-04 16:01 ` Lorenzo Stoakes (ARM)
2026-09-06 0:17 ` Andrew Morton
2026-09-07 11:51 ` David Hildenbrand (Arm)
@ 2026-09-07 12:39 ` Pedro Falcato
2026-09-07 15:05 ` Vlastimil Babka (SUSE)
3 siblings, 0 replies; 11+ messages in thread
From: Pedro Falcato @ 2026-09-07 12:39 UTC (permalink / raw)
To: Lorenzo Stoakes (ARM)
Cc: Andrew Morton, Liam R. Howlett, David Hildenbrand,
Vlastimil Babka, Jann Horn, linux-mm, linux-kernel,
Mike Kaplinskiy, zhaozhengzhuo
On Fri, Sep 04, 2026 at 05:01:37PM +0100, Lorenzo Stoakes (ARM) wrote:
> On Fri, Sep 04, 2026 at 04:24:59PM +0100, Pedro Falcato wrote:
> > On Fri, Sep 04, 2026 at 04:12:53PM +0100, Lorenzo Stoakes (ARM) wrote:
> > > Currently MADV_WILLNEED treats file-backed and pure anonymous mappings
> > > entirely separately - using POSIX_FADV_WILLNEED (equivalent of a readahead)
> > > for the former and a tree walk and swap in to swap cache for the latter.
> > >
> > > MAP_PRIVATE-file backed mappings straddle the two and currently get treated
> > > as if they were purely file-backed, meaning any swapped out private pages
> > > remain swapped out.
> > >
> > > Resolve the issue by explicitly checking for CoW'd MAP_PRIVATE-file backed
> > > mappings and performing both walks in this case.
> > >
> > > Since the logic checks for vma->anon_vma this means un-CoW'd
> > > MAP_PRIVATE-file backed mappings retain only the single file walk.
> > >
> > > Reported-by: Mike Kaplinskiy <mike@recall.ai>
> > > Closes: https://lore.kernel.org/all/CABeknB_S2XJSHFgnHdgnN0rjzHhH4oQJs_APq9fvxHztQ_pgiA@mail.gmail.com/
> > > Signed-off-by: Lorenzo Stoakes (ARM) <ljs@kernel.org>
> > > ---
> > > mm/madvise.c | 6 ++++--
> > > 1 file changed, 4 insertions(+), 2 deletions(-)
> > >
> > > diff --git a/mm/madvise.c b/mm/madvise.c
> > > index 73c2901b9adb..d0510dd49dde 100644
> > > --- a/mm/madvise.c
> > > +++ b/mm/madvise.c
> > > @@ -297,10 +297,12 @@ static long madvise_willneed(struct madvise_behavior *madv_behavior)
> > > loff_t offset;
> > >
> > > #ifdef CONFIG_SWAP
> > > - if (!file) {
> > > + if (!file || (vma_is_cow_mapping(vma) && vma->anon_vma)) {
> >
> > Couldn't this all be simplified to
> >
> > if (vma->anon_vma) {
> >
> > ? swapin needs anon pages to have been faulted-in. Non-cow mappings won't
> > have an anon_vma, nor will fully empty anonymous VMAs (and that's fine).
> > Right?
>
> Hmm good point :)
>
> Though the
>
> if (!file)
> return 0;
>
> Would have to be outside of the block to avoid an anon unfaulted (nop) from
> being skipped.
>
> And it's a real improvement to have unfaulted anon skip...
>
> But I find that version is documenting what's going on a lot less.
>
> Since both anon and MAP_PRIVATE file-backed are CoW mappings we could just
> reference that.
>
> Andrew - could you swap the patch out in-place with below? Thanks!
>
> Cheers, Lorenzo
>
> ----8<----
> From a706374a1186b6b4409a0e760e8728bcadba0c96 Mon Sep 17 00:00:00 2001
> From: "Lorenzo Stoakes (ARM)" <ljs@kernel.org>
> Date: Thu, 3 Sep 2026 20:08:39 +0100
> Subject: [PATCH] mm/madvise: swap in CoW'd MAP_PRIVATE-file mappings on
> MADV_WILLNEED
>
> Currently MADV_WILLNEED treats file-backed and pure anonymous mappings
> entirely separately - using POSIX_FADV_WILLNEED (equivalent of a readahead)
> for the former and a tree walk and swap in to swap cache for the latter.
>
> MAP_PRIVATE-file backed mappings straddle the two and currently get treated
> as if they were purely file-backed, meaning any swapped out private pages
> remain swapped out.
>
> Resolve the issue by explicitly checking for CoW'd MAP_PRIVATE-file backed
> mappings and performing both walks in this case.
>
> Since the logic checks for vma->anon_vma this means un-CoW'd
> MAP_PRIVATE-file backed mappings retain only the single file walk.
>
> Reported-by: Mike Kaplinskiy <mike@recall.ai>
> Closes: https://lore.kernel.org/all/CABeknB_S2XJSHFgnHdgnN0rjzHhH4oQJs_APq9fvxHztQ_pgiA@mail.gmail.com/
> Signed-off-by: Lorenzo Stoakes (ARM) <ljs@kernel.org>
LGTM, thanks!
Reviewed-by: Pedro Falcato <pfalcato@suse.de>
--
Pedro
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [PATCH] mm/madvise: swap in CoW'd MAP_PRIVATE-file mappings on MADV_WILLNEED
2026-09-04 16:01 ` Lorenzo Stoakes (ARM)
` (2 preceding siblings ...)
2026-09-07 12:39 ` Pedro Falcato
@ 2026-09-07 15:05 ` Vlastimil Babka (SUSE)
3 siblings, 0 replies; 11+ messages in thread
From: Vlastimil Babka (SUSE) @ 2026-09-07 15:05 UTC (permalink / raw)
To: Lorenzo Stoakes (ARM), Pedro Falcato
Cc: Andrew Morton, Liam R. Howlett, David Hildenbrand, Jann Horn,
linux-mm, linux-kernel, Mike Kaplinskiy, zhaozhengzhuo
On 9/4/26 18:01, Lorenzo Stoakes (ARM) wrote:
> On Fri, Sep 04, 2026 at 04:24:59PM +0100, Pedro Falcato wrote:
> Andrew - could you swap the patch out in-place with below? Thanks!
>
> Cheers, Lorenzo
>
> ----8<----
> From a706374a1186b6b4409a0e760e8728bcadba0c96 Mon Sep 17 00:00:00 2001
> From: "Lorenzo Stoakes (ARM)" <ljs@kernel.org>
> Date: Thu, 3 Sep 2026 20:08:39 +0100
> Subject: [PATCH] mm/madvise: swap in CoW'd MAP_PRIVATE-file mappings on
> MADV_WILLNEED
>
> Currently MADV_WILLNEED treats file-backed and pure anonymous mappings
> entirely separately - using POSIX_FADV_WILLNEED (equivalent of a readahead)
> for the former and a tree walk and swap in to swap cache for the latter.
>
> MAP_PRIVATE-file backed mappings straddle the two and currently get treated
> as if they were purely file-backed, meaning any swapped out private pages
> remain swapped out.
>
> Resolve the issue by explicitly checking for CoW'd MAP_PRIVATE-file backed
> mappings and performing both walks in this case.
>
> Since the logic checks for vma->anon_vma this means un-CoW'd
> MAP_PRIVATE-file backed mappings retain only the single file walk.
>
> Reported-by: Mike Kaplinskiy <mike@recall.ai>
> Closes: https://lore.kernel.org/all/CABeknB_S2XJSHFgnHdgnN0rjzHhH4oQJs_APq9fvxHztQ_pgiA@mail.gmail.com/
> Signed-off-by: Lorenzo Stoakes (ARM) <ljs@kernel.org>
Acked-by: Vlastimil Babka (SUSE) <vbabka@kernel.org>
> ---
> mm/madvise.c | 5 +++--
> 1 file changed, 3 insertions(+), 2 deletions(-)
>
> diff --git a/mm/madvise.c b/mm/madvise.c
> index 73c2901b9adb..963337f93a7a 100644
> --- a/mm/madvise.c
> +++ b/mm/madvise.c
> @@ -297,11 +297,12 @@ static long madvise_willneed(struct madvise_behavior *madv_behavior)
> loff_t offset;
>
> #ifdef CONFIG_SWAP
> - if (!file) {
> + if (vma_is_cow_mapping(vma) && vma->anon_vma) {
> walk_page_range_vma(vma, start, end, &swapin_walk_ops, vma);
> lru_add_drain(); /* Push any new pages onto the LRU now */
> - return 0;
> }
> + if (!file)
> + return 0;
>
> if (shmem_mapping(file->f_mapping)) {
> shmem_swapin_range(vma, start, end, file->f_mapping);
> --
> 2.55.0
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [PATCH] mm/madvise: swap in CoW'd MAP_PRIVATE-file mappings on MADV_WILLNEED
2026-09-07 12:32 ` Lorenzo Stoakes (ARM)
@ 2026-09-07 15:09 ` Vlastimil Babka (SUSE)
2026-09-07 15:27 ` Lorenzo Stoakes (ARM)
0 siblings, 1 reply; 11+ messages in thread
From: Vlastimil Babka (SUSE) @ 2026-09-07 15:09 UTC (permalink / raw)
To: Lorenzo Stoakes (ARM), Andrew Morton
Cc: Pedro Falcato, Liam R. Howlett, David Hildenbrand, Jann Horn,
linux-mm, linux-kernel, Mike Kaplinskiy, zhaozhengzhuo
On 9/7/26 14:32, Lorenzo Stoakes (ARM) wrote:
> On Sat, Sep 05, 2026 at 05:17:57PM -0700, Andrew Morton wrote:
>> On Fri, 4 Sep 2026 17:01:37 +0100 "Lorenzo Stoakes (ARM)" <ljs@kernel.org> wrote:
>>
>> > Andrew - could you swap the patch out in-place with below? Thanks!
>> >
>>
>> np.
>>
>> > Currently MADV_WILLNEED treats file-backed and pure anonymous mappings
>> > entirely separately - using POSIX_FADV_WILLNEED (equivalent of a readahead)
>> > for the former and a tree walk and swap in to swap cache for the latter.
>> >
>> > MAP_PRIVATE-file backed mappings straddle the two and currently get treated
>> > as if they were purely file-backed, meaning any swapped out private pages
>> > remain swapped out.
>> >
>> > Resolve the issue by explicitly checking for CoW'd MAP_PRIVATE-file backed
>> > mappings and performing both walks in this case.
>> >
>> > Since the logic checks for vma->anon_vma this means un-CoW'd
>> > MAP_PRIVATE-file backed mappings retain only the single file walk.
>> >
>> > Reported-by: Mike Kaplinskiy <mike@recall.ai>
>> > Closes: https://lore.kernel.org/all/CABeknB_S2XJSHFgnHdgnN0rjzHhH4oQJs_APq9fvxHztQ_pgiA@mail.gmail.com/
>> > Signed-off-by: Lorenzo Stoakes (ARM) <ljs@kernel.org>
>>
>> The failure mode looks pretty obscure, so I'm thinking that no backport
>> is needed.
>
> Well it's not even really a failure mode so much as a 'known limitation' :)
> So yeah agreed.
Yeah, as we know madvise() just an advice after all, the kernel is free to
ignore it :)
(MADV_DONTNEED and friends enter the chat)
> --
> Cheers, Lorenzo
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [PATCH] mm/madvise: swap in CoW'd MAP_PRIVATE-file mappings on MADV_WILLNEED
2026-09-07 15:09 ` Vlastimil Babka (SUSE)
@ 2026-09-07 15:27 ` Lorenzo Stoakes (ARM)
0 siblings, 0 replies; 11+ messages in thread
From: Lorenzo Stoakes (ARM) @ 2026-09-07 15:27 UTC (permalink / raw)
To: Vlastimil Babka (SUSE)
Cc: Andrew Morton, Pedro Falcato, Liam R. Howlett, David Hildenbrand,
Jann Horn, linux-mm, linux-kernel, Mike Kaplinskiy,
zhaozhengzhuo
On Mon, Sep 07, 2026 at 05:09:34PM +0200, Vlastimil Babka (SUSE) wrote:
> On 9/7/26 14:32, Lorenzo Stoakes (ARM) wrote:
> > On Sat, Sep 05, 2026 at 05:17:57PM -0700, Andrew Morton wrote:
> >> On Fri, 4 Sep 2026 17:01:37 +0100 "Lorenzo Stoakes (ARM)" <ljs@kernel.org> wrote:
> >>
> >> > Andrew - could you swap the patch out in-place with below? Thanks!
> >> >
> >>
> >> np.
> >>
> >> > Currently MADV_WILLNEED treats file-backed and pure anonymous mappings
> >> > entirely separately - using POSIX_FADV_WILLNEED (equivalent of a readahead)
> >> > for the former and a tree walk and swap in to swap cache for the latter.
> >> >
> >> > MAP_PRIVATE-file backed mappings straddle the two and currently get treated
> >> > as if they were purely file-backed, meaning any swapped out private pages
> >> > remain swapped out.
> >> >
> >> > Resolve the issue by explicitly checking for CoW'd MAP_PRIVATE-file backed
> >> > mappings and performing both walks in this case.
> >> >
> >> > Since the logic checks for vma->anon_vma this means un-CoW'd
> >> > MAP_PRIVATE-file backed mappings retain only the single file walk.
> >> >
> >> > Reported-by: Mike Kaplinskiy <mike@recall.ai>
> >> > Closes: https://lore.kernel.org/all/CABeknB_S2XJSHFgnHdgnN0rjzHhH4oQJs_APq9fvxHztQ_pgiA@mail.gmail.com/
> >> > Signed-off-by: Lorenzo Stoakes (ARM) <ljs@kernel.org>
> >>
> >> The failure mode looks pretty obscure, so I'm thinking that no backport
> >> is needed.
> >
> > Well it's not even really a failure mode so much as a 'known limitation' :)
> > So yeah agreed.
>
> Yeah, as we know madvise() just an advice after all, the kernel is free to
> ignore it :)
>
> (MADV_DONTNEED and friends enter the chat)
Haha yeah, another brilliant footgun invented by the proud folks at kernel mm,
catch us next time with some prctl's and BPF APIs! ;)
>
> > --
> > Cheers, Lorenzo
>
--
Cheers, Lorenzo
^ permalink raw reply [flat|nested] 11+ messages in thread
end of thread, other threads:[~2026-09-07 15:27 UTC | newest]
Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-09-04 15:12 [PATCH] mm/madvise: swap in CoW'd MAP_PRIVATE-file mappings on MADV_WILLNEED Lorenzo Stoakes (ARM)
2026-09-04 15:24 ` Pedro Falcato
2026-09-04 16:01 ` Lorenzo Stoakes (ARM)
2026-09-06 0:17 ` Andrew Morton
2026-09-07 12:32 ` Lorenzo Stoakes (ARM)
2026-09-07 15:09 ` Vlastimil Babka (SUSE)
2026-09-07 15:27 ` Lorenzo Stoakes (ARM)
2026-09-07 11:51 ` David Hildenbrand (Arm)
2026-09-07 12:33 ` Lorenzo Stoakes (ARM)
2026-09-07 12:39 ` Pedro Falcato
2026-09-07 15:05 ` Vlastimil Babka (SUSE)
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®