* [PATCH] mm/util: fix retry detection and handling in snapshot_page()
@ 2026-11-16 14:12 David Hildenbrand (Arm)
2026-11-16 21:03 ` Luiz Capitulino (Red Hat)
0 siblings, 1 reply; 2+ messages in thread
From: David Hildenbrand (Arm) @ 2026-11-16 14:12 UTC (permalink / raw)
To: Lorenzo Stoakes, Liam R. Howlett, Vlastimil Babka, Mike Rapoport,
Suren Baghdasaryan, Michal Hocko, Andrew Morton, Luiz Capitulino,
Shivank Garg
Cc: linux-mm, linux-kernel, Shao Mingyin, David Hildenbrand (Arm)
snapshot_page() detects races with concurrent folio split+construction by
checking whether the given page still falls into the folio. Unfortunately,
we have an off-by-one error in there, and we should be checking for
"ps->idx >= nr_pages", because idx 0 is valid.
For example, we might currently report a tail page at idx 1 as faithful,
although the snapshotted folio is now a small folio.
However, there is one complication: As we don't hold a folio reference,
folio_nr_pages() can return 0 on races (or when called from page freeing
code). Handle that by just setting it to 1 in that case, so we don't
wrongly report a head page as "page does not match folio".
In addition, we should reset ps->flags when retrying, by just resetting
it to PAGE_SNAPSHOT_FAITHFUL. Otherwise, PAGE_SNAPSHOT_PG_BUDDY and
PAGE_SNAPSHOT_PG_IDLE from a failed attempt could stick around.
These races are fairly rare, so this is not really critical, we simply
report something slightly wrong. No need to CC stable.
Fixes: d863a12108f2 ("mm/util: introduce snapshot_page()")
Reported-by: Shao Mingyin <shao.mingyin@zte.com.cn>
Closes: https://lore.kernel.org/r/20261115004657890Pz8ERDf5nCtE-B6M7KFzu@zte.com.cn
Reviewed-by: Lorenzo Stoakes (ARM) <ljs@kernel.org>
Signed-off-by: David Hildenbrand (Arm) <david@kernel.org>
---
mm/util.c | 7 ++++---
1 file changed, 4 insertions(+), 3 deletions(-)
diff --git a/mm/util.c b/mm/util.c
index 80f6c714844f2..338f259b00351 100644
--- a/mm/util.c
+++ b/mm/util.c
@@ -1406,9 +1406,9 @@ void snapshot_page(struct page_snapshot *ps, const struct page *page)
int loops = 5;
ps->pfn = page_to_pfn(page);
- ps->flags = PAGE_SNAPSHOT_FAITHFUL;
again:
+ ps->flags = PAGE_SNAPSHOT_FAITHFUL;
memset(&ps->folio_snapshot, 0, sizeof(struct folio));
memcpy(&ps->page_snapshot, page, sizeof(*page));
info = ps->page_snapshot.compound_info;
@@ -1437,14 +1437,15 @@ void snapshot_page(struct page_snapshot *ps, const struct page *page)
if (ps->idx < MAX_FOLIO_NR_PAGES) {
memcpy(&ps->folio_snapshot, foliop, 2 * sizeof(struct page));
- nr_pages = folio_nr_pages(&ps->folio_snapshot);
+ /* On races, or when freeing pages, we can read 0. */
+ nr_pages = folio_nr_pages(&ps->folio_snapshot) ?: 1;
if (nr_pages > 2)
memcpy(&ps->folio_snapshot.__page_2, &foliop->__page_2,
sizeof(struct page));
set_ps_flags(ps, foliop, page);
}
- if (ps->idx > nr_pages) {
+ if (ps->idx >= nr_pages) {
if (loops-- > 0)
goto again;
clear_compound_head(&ps->page_snapshot);
---
base-commit: d164c7d754f0e1f020298366ea92430e62476089
change-id: 20261116-snapshot_page_fix-fae185e61c94
Best regards,
--
David Hildenbrand (Arm) <david@kernel.org>
^ permalink raw reply related [flat|nested] 2+ messages in thread
* Re: [PATCH] mm/util: fix retry detection and handling in snapshot_page()
2026-11-16 14:12 [PATCH] mm/util: fix retry detection and handling in snapshot_page() David Hildenbrand (Arm)
@ 2026-11-16 21:03 ` Luiz Capitulino (Red Hat)
0 siblings, 0 replies; 2+ messages in thread
From: Luiz Capitulino (Red Hat) @ 2026-11-16 21:03 UTC (permalink / raw)
To: David Hildenbrand (Arm), Lorenzo Stoakes, Liam R. Howlett,
Vlastimil Babka, Mike Rapoport, Suren Baghdasaryan, Michal Hocko,
Andrew Morton, Shivank Garg
Cc: linux-mm, linux-kernel, Shao Mingyin
On 11/16/26 9:12 AM, David Hildenbrand (Arm) wrote:
> snapshot_page() detects races with concurrent folio split+construction by
> checking whether the given page still falls into the folio. Unfortunately,
> we have an off-by-one error in there, and we should be checking for
> "ps->idx >= nr_pages", because idx 0 is valid.
>
> For example, we might currently report a tail page at idx 1 as faithful,
> although the snapshotted folio is now a small folio.
>
> However, there is one complication: As we don't hold a folio reference,
> folio_nr_pages() can return 0 on races (or when called from page freeing
> code). Handle that by just setting it to 1 in that case, so we don't
> wrongly report a head page as "page does not match folio".
>
> In addition, we should reset ps->flags when retrying, by just resetting
> it to PAGE_SNAPSHOT_FAITHFUL. Otherwise, PAGE_SNAPSHOT_PG_BUDDY and
> PAGE_SNAPSHOT_PG_IDLE from a failed attempt could stick around.
>
> These races are fairly rare, so this is not really critical, we simply
> report something slightly wrong. No need to CC stable.
>
> Fixes: d863a12108f2 ("mm/util: introduce snapshot_page()")
> Reported-by: Shao Mingyin <shao.mingyin@zte.com.cn>
> Closes: https://lore.kernel.org/r/20261115004657890Pz8ERDf5nCtE-B6M7KFzu@zte.com.cn
> Reviewed-by: Lorenzo Stoakes (ARM) <ljs@kernel.org>
> Signed-off-by: David Hildenbrand (Arm) <david@kernel.org>
Acked-by: Luiz Capitulino (Red Hat) <luiz.capitulino@linux.dev>
> ---
> mm/util.c | 7 ++++---
> 1 file changed, 4 insertions(+), 3 deletions(-)
>
> diff --git a/mm/util.c b/mm/util.c
> index 80f6c714844f2..338f259b00351 100644
> --- a/mm/util.c
> +++ b/mm/util.c
> @@ -1406,9 +1406,9 @@ void snapshot_page(struct page_snapshot *ps, const struct page *page)
> int loops = 5;
>
> ps->pfn = page_to_pfn(page);
> - ps->flags = PAGE_SNAPSHOT_FAITHFUL;
>
> again:
> + ps->flags = PAGE_SNAPSHOT_FAITHFUL;
> memset(&ps->folio_snapshot, 0, sizeof(struct folio));
> memcpy(&ps->page_snapshot, page, sizeof(*page));
> info = ps->page_snapshot.compound_info;
> @@ -1437,14 +1437,15 @@ void snapshot_page(struct page_snapshot *ps, const struct page *page)
>
> if (ps->idx < MAX_FOLIO_NR_PAGES) {
> memcpy(&ps->folio_snapshot, foliop, 2 * sizeof(struct page));
> - nr_pages = folio_nr_pages(&ps->folio_snapshot);
> + /* On races, or when freeing pages, we can read 0. */
> + nr_pages = folio_nr_pages(&ps->folio_snapshot) ?: 1;
> if (nr_pages > 2)
> memcpy(&ps->folio_snapshot.__page_2, &foliop->__page_2,
> sizeof(struct page));
> set_ps_flags(ps, foliop, page);
> }
>
> - if (ps->idx > nr_pages) {
> + if (ps->idx >= nr_pages) {
> if (loops-- > 0)
> goto again;
> clear_compound_head(&ps->page_snapshot);
>
> ---
> base-commit: d164c7d754f0e1f020298366ea92430e62476089
> change-id: 20261116-snapshot_page_fix-fae185e61c94
>
> Best regards,
> --
> David Hildenbrand (Arm) <david@kernel.org>
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-11-16 21:03 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-16 14:12 [PATCH] mm/util: fix retry detection and handling in snapshot_page() David Hildenbrand (Arm)
2026-11-16 21:03 ` Luiz Capitulino (Red Hat)
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox