mm/util: fix off-by-one in snapshot_page() retry check
Subsystems (2)
PATCH 1/1 mm/util: fix off-by-one in snapshot_page() retry check
snapshot_page() computes ps->idx, the index of @page within the folio it appeared to belong to, and nr_pages, the size of that folio as read from a copy of its head page. Because the folio can be split concurrently, the two can disagree, in which case the snapshot is retried and finally marked as unfaithful.
Valid indices are 0 to nr_pages - 1, but the check only catches ps->idx > nr_pages. If the folio is split such that ps->idx equals nr_pages, @page is no longer part of the folio that was copied, yet the snapshot is returned as faithful with an out-of-range index.
Use >= so that this case is retried as well.
The race can be hit from userspace. On a kernel with a debug-only check for this condition, one thread repeatedly faulted in a PMD-sized THP and split it to order 0 through the split_huge_pages debugfs file, while three threads read the /proc/kpageflags entry of the THP's second page. In 90 seconds (about 181,000 splits), 14 snapshots of that page were returned as faithful with idx 1 and nr_pages 1. With this change, a second 90-second run (about 138,000 splits) hit the idx == nr_pages case 41 times; each one was retried, and no snapshot was returned with an out-of-range index.
This issue was found by an AI coding assistant (Gemini) while reviewing snapshot_page(). The fix and the reproducer were written with its help.
--- mm/util.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
mm/util.c +1 -1
@@ -1444,7 +1444,7 @@ void snapshot_page(struct page_snapshot *ps, const struct page *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);
Signature · 1 line
2.56.1.750.gf70dc682cf-goog
Quoted 15 lines · Nilay Vaish
[You don't often get email from [email protected]. Learn why this is important at https://aka.ms/LearnAboutSenderIdentification ]
snapshot_page()computesps->idx, the index of @page within the folio it appeared to belong to, andnr_pages, the size of that folio as read from a copy of its head page. Because the folio can be split concurrently, the two can disagree, in which case the snapshot is retried and finally marked as unfaithful.Valid indices are 0 to
nr_pages- 1, but the check only catchesps->idx>nr_pages. If the folio is split such thatps->idxequalsnr_pages, @page is no longer part of the folio that was copied, yet the snapshot is returned as faithful with an out-of-range index.Use >= so that this case is retried as well.
Quoted 9 lines
The race can be hit from userspace. On a kernel with a debug-only check for this condition, one thread repeatedly faulted in a PMD-sized THP and split it to order 0 through the
split_huge_pagesdebugfs file, while three threads read the/proc/kpageflagsentry of the THP's second page. In 90 seconds (about 181,000 splits), 14 snapshots of that page were returned as faithful with idx 1 andnr_pages1. With this change, a second 90-second run (about 138,000 splits) hit the idx ==nr_pagescase 41 times; each one was retried, and no snapshot was returned with an out-of-range index.
Hi Nilay,
This one is already posted [1] and applied in mm tree.
Quoted 4 lines
This issue was found by an AI coding assistant (Gemini) while reviewing
snapshot_page(). The fix and the reproducer were written with its help.
As a tip for AI-assisted reviews: semcode [2] has an MCP server with lore search, which makes it easy to check whether a fix has already been posted before sending. It has saved me from duplicates a few times.
[1] https://lore.kernel.org/all/[email protected]
[2] https://github.com/facebookexperimental/semcode
Thanks,
Shivank
Quoted 21 lines
Fixes d863a12108f2 ("mm/util: introduce snapshot_page()")Signed-off-by Nilay Vaish <[email protected]>--- mm/util.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)mm/util.c +1
-2@@ -1444,7 +1444,7 @@ void snapshot_page(struct page_snapshot *ps, const struct page *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); --2.56.1.750.gf70dc682cf-goog