mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH v2 1/2] fs/proc/task_mmu: reset walk_end before each pagemap walk
  2026-11-18  0:26 [PATCH v2 0/2] fs/proc: fix PAGEMAP_SCAN walk_end after a buffer refill Matthew Salsamendi via B4 Relay
@ 2026-11-18  0:26 ` Matthew Salsamendi via B4 Relay
  2026-11-18  0:26 ` [PATCH v2 2/2] selftests/mm: cover PAGEMAP_SCAN refill and HugeTLB retry Matthew Salsamendi via B4 Relay
  1 sibling, 0 replies; 3+ messages in thread
From: Matthew Salsamendi via B4 Relay @ 2026-11-18  0:26 UTC (permalink / raw)
  To: Andrew Morton, Liam R. Howlett, Lorenzo Stoakes
  Cc: Chris Down, Vlastimil Babka, Muhammad Usama Anjum, linux-mm,
	linux-kernel, matthewsalsamendi, Jann Horn, Pedro Falcato,
	linux-fsdevel, stable

From: Matthew Salsamendi <matthewsalsamendi@gmail.com>

When PAGEMAP_SCAN resumes after filling its buffer, it can return ranges
beyond a stale walk_end. This aborts a production game server during
dirty-page tracking.

Reset walk_end before each walk so a completed scan reports the end and
a partial HugeTLB write-protect request keeps its retry address.

Codex assisted with diagnosis, validation and this message.

Fixes: 52526ca7fdb9 ("fs/proc/task_mmu: implement IOCTL to get and optionally clear info about PTEs")
Suggested-by: Chris Down <chris@chrisdown.name>
Suggested-by: Lorenzo Stoakes <ljs@kernel.org>
Cc: stable@vger.kernel.org # 6.7+
Signed-off-by: Matthew Salsamendi <matthewsalsamendi@gmail.com>
---
v2: Use the per-walk reset suggested by Chris and Lorenzo.
    Verified the 513-range refill and 2 MiB HugeTLB retry on x86-64.

Stable: The bug is present in v6.7. The hunk needs a context adjustment
on v6.7 and applies unchanged to v6.8.

 fs/proc/task_mmu.c | 1 +
 1 file changed, 1 insertion(+)

diff --git a/fs/proc/task_mmu.c b/fs/proc/task_mmu.c
index b87d3e86442d..8723ffbff77c 100644
--- a/fs/proc/task_mmu.c
+++ b/fs/proc/task_mmu.c
@@ -3155,6 +3155,7 @@ static long do_pagemap_scan(struct mm_struct *mm, unsigned long uarg)
 			mmu_notifier_invalidate_range_start(&range);
 		}
 
+		p.arg.walk_end = 0; /* Reset partial walks. */
 		ret = walk_page_range(mm, walk_start, p.arg.end,
 				      &pagemap_scan_ops, &p);
 



^ permalink raw reply	[flat|nested] 3+ messages in thread

* [PATCH v2 0/2] fs/proc: fix PAGEMAP_SCAN walk_end after a buffer refill
@ 2026-11-18  0:26 Matthew Salsamendi via B4 Relay
  2026-11-18  0:26 ` [PATCH v2 1/2] fs/proc/task_mmu: reset walk_end before each pagemap walk Matthew Salsamendi via B4 Relay
  2026-11-18  0:26 ` [PATCH v2 2/2] selftests/mm: cover PAGEMAP_SCAN refill and HugeTLB retry Matthew Salsamendi via B4 Relay
  0 siblings, 2 replies; 3+ messages in thread
From: Matthew Salsamendi via B4 Relay @ 2026-11-18  0:26 UTC (permalink / raw)
  To: Andrew Morton, Liam R. Howlett, Lorenzo Stoakes,
	David Hildenbrand, Shuah Khan
  Cc: Chris Down, Vlastimil Babka, Muhammad Usama Anjum, linux-mm,
	linux-kernel, matthewsalsamendi, Jann Horn, Pedro Falcato,
	linux-fsdevel, Mike Rapoport, Suren Baghdasaryan, Michal Hocko,
	linux-kselftest

Hey all,

Thanks for the feedback. I agree with the proposed change to the patch.
Apologies for the delayed reply.

You'll find the updated patch and the requested selftest below.

On x86-64, the refill test fails on the unpatched kernel and the
HugeTLB retry test fails on v1. All 121 tests pass on v2.

Matt

v1: https://lore.kernel.org/linux-mm/CAMPWk9NE_epOdOCbs1PGh8OQP4wjTObvjU-1Wopmu7SSNAMXhw@mail.gmail.com/

Matthew Salsamendi (2):
  fs/proc/task_mmu: reset walk_end before each pagemap walk
  selftests/mm: cover PAGEMAP_SCAN refill and HugeTLB retry

 fs/proc/task_mmu.c                         |  1 +
 tools/testing/selftests/mm/pagemap_ioctl.c | 61 +++++++++++++++++++++-
 2 files changed, 61 insertions(+), 1 deletion(-)


base-commit: 7475ea8b58616b33c04afb976cf0fbaad9dd63fb



^ permalink raw reply	[flat|nested] 3+ messages in thread

* [PATCH v2 2/2] selftests/mm: cover PAGEMAP_SCAN refill and HugeTLB retry
  2026-11-18  0:26 [PATCH v2 0/2] fs/proc: fix PAGEMAP_SCAN walk_end after a buffer refill Matthew Salsamendi via B4 Relay
  2026-11-18  0:26 ` [PATCH v2 1/2] fs/proc/task_mmu: reset walk_end before each pagemap walk Matthew Salsamendi via B4 Relay
@ 2026-11-18  0:26 ` Matthew Salsamendi via B4 Relay
  1 sibling, 0 replies; 3+ messages in thread
From: Matthew Salsamendi via B4 Relay @ 2026-11-18  0:26 UTC (permalink / raw)
  To: Andrew Morton, Liam R. Howlett, Lorenzo Stoakes,
	David Hildenbrand, Shuah Khan
  Cc: Chris Down, Vlastimil Babka, Muhammad Usama Anjum, linux-mm,
	linux-kernel, matthewsalsamendi, Mike Rapoport,
	Suren Baghdasaryan, Michal Hocko, linux-kselftest

From: Matthew Salsamendi <matthewsalsamendi@gmail.com>

Check that scanning 513 disjoint dirty pages returns every range and
sets walk_end to the end of the scan. For a written HugeTLB page, limit
the scan to one base page, then retry from walk_end and check that the
whole page is returned and write-protected.

Codex assisted with the selftests, validation and this message.

Suggested-by: Andrew Morton <akpm@linux-foundation.org>
Suggested-by: Chris Down <chris@chrisdown.name>
Signed-off-by: Matthew Salsamendi <matthewsalsamendi@gmail.com>
---
 tools/testing/selftests/mm/pagemap_ioctl.c | 61 +++++++++++++++++++++-
 1 file changed, 60 insertions(+), 1 deletion(-)

diff --git a/tools/testing/selftests/mm/pagemap_ioctl.c b/tools/testing/selftests/mm/pagemap_ioctl.c
index 03898b4f6cda..02062b178aa5 100644
--- a/tools/testing/selftests/mm/pagemap_ioctl.c
+++ b/tools/testing/selftests/mm/pagemap_ioctl.c
@@ -682,6 +682,62 @@ static int sanity_tests_sd(void)
 	return 0;
 }
 
+static void test_walk_end_after_refill(void)
+{
+	/* One more range than the internal buffer holds on x86-64. */
+	struct page_region vec[513];
+	size_t len = 2 * ARRAY_SIZE(vec) * page_size;
+	long ret, walk_end;
+	char *mem;
+	bool ok;
+	size_t i;
+
+	mem = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
+	if (mem == MAP_FAILED)
+		ksft_exit_fail_msg("mmap: %s\n", strerror(errno));
+	if (madvise(mem, len, MADV_NOHUGEPAGE) && errno != EINVAL)
+		ksft_exit_fail_msg("MADV_NOHUGEPAGE: %s\n", strerror(errno));
+
+	wp_init(mem, len);
+	for (i = 0; i < ARRAY_SIZE(vec); i++)
+		mem[2 * i * page_size] = 1;
+
+	ret = pagemap_ioc(mem, len, vec, ARRAY_SIZE(vec), 0, 0,
+			  PAGE_IS_WRITTEN, 0, 0, PAGE_IS_WRITTEN, &walk_end);
+	ok = ret == ARRAY_SIZE(vec) && walk_end == (long)(mem + len);
+	for (i = 0; ok && i < ARRAY_SIZE(vec); i++) {
+		uintptr_t addr = (uintptr_t)(mem + 2 * i * page_size);
+
+		ok = vec[i].start == addr && vec[i].end == addr + page_size;
+	}
+	ksft_test_result(ok, "PAGEMAP_SCAN walk_end after buffer refill\n");
+
+	wp_free(mem, len);
+	munmap(mem, len);
+}
+
+static void test_hugetlb_walk_end(char *mem, size_t len)
+{
+	const int flags = PM_SCAN_WP_MATCHING | PM_SCAN_CHECK_WPASYNC;
+	struct page_region range;
+	long ret, walk_end;
+	bool ok;
+
+	mem[0] = 1;
+	ret = pagemap_ioc(mem, len, &range, 1, flags, 1, PAGE_IS_WRITTEN,
+			  0, 0, PAGE_IS_WRITTEN, &walk_end);
+	ok = ret == 0 && walk_end == (long)mem &&
+	     !(pagemap_get_entry(pagemap_fd, mem) & PM_UFFD_WP);
+	if (ok) {
+		ret = pagemap_ioc((void *)walk_end, len, &range, 1, flags, len / page_size,
+				  PAGE_IS_WRITTEN, 0, 0, PAGE_IS_WRITTEN, &walk_end);
+		ok = ret == 1 && walk_end == (long)(mem + len) &&
+		     range.start == (uintptr_t)mem && range.end == (uintptr_t)(mem + len) &&
+		     (pagemap_get_entry(pagemap_fd, mem) & PM_UFFD_WP);
+	}
+	ksft_test_result(ok, "PAGEMAP_SCAN HugeTLB write-protect retry\n");
+}
+
 static int base_tests(char *prefix, char *mem, size_t mem_size, int skip)
 {
 	size_t vec_size;
@@ -1661,7 +1717,7 @@ int main(int __attribute__((unused)) argc, char *argv[])
 	if (!hugetlb_setup_default(4))
 		ksft_print_msg("HugeTLB test will be skipped\n");
 
-	ksft_set_plan(119);
+	ksft_set_plan(121);
 
 	pagemap_fd = open(PAGEMAP, O_RDONLY);
 	if (pagemap_fd < 0)
@@ -1669,6 +1725,7 @@ int main(int __attribute__((unused)) argc, char *argv[])
 
 	/* 1. Sanity testing */
 	sanity_tests_sd();
+	test_walk_end_after_refill();
 
 	/* 2. Normal page testing */
 	mem_size = 10 * page_size;
@@ -1730,10 +1787,12 @@ int main(int __attribute__((unused)) argc, char *argv[])
 		wp_addr_range(mem, mem_size);
 
 		base_tests("Hugetlb mem testing:", mem, mem_size, 0);
+		test_hugetlb_walk_end(mem, mem_size);
 
 		wp_free(mem, mem_size);
 	} else {
 		base_tests("Hugetlb mem testing:", NULL, 0, 1);
+		ksft_test_result_skip("PAGEMAP_SCAN HugeTLB write-protect retry: no huge page\n");
 	}
 
 	/* 7. File Hugetlb testing */



^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-11-18  0:26 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-18  0:26 [PATCH v2 0/2] fs/proc: fix PAGEMAP_SCAN walk_end after a buffer refill Matthew Salsamendi via B4 Relay
2026-11-18  0:26 ` [PATCH v2 1/2] fs/proc/task_mmu: reset walk_end before each pagemap walk Matthew Salsamendi via B4 Relay
2026-11-18  0:26 ` [PATCH v2 2/2] selftests/mm: cover PAGEMAP_SCAN refill and HugeTLB retry Matthew Salsamendi via B4 Relay

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®