Linux Kernel Selftest development
 help / color / mirror / Atom feed
* [PATCH v5 0/2] fs/proc/task_mmu: fix PAGEMAP_SCAN written state for PMD holes
@ 2026-07-15 14:42 Kiryl Shutsemau
  2026-07-15 14:42 ` [PATCH v5 1/2] " Kiryl Shutsemau
  2026-07-15 14:42 ` [PATCH v5 2/2] selftests/mm: add PAGEMAP_SCAN test for THP " Kiryl Shutsemau
  0 siblings, 2 replies; 8+ messages in thread
From: Kiryl Shutsemau @ 2026-07-15 14:42 UTC (permalink / raw)
  To: akpm
  Cc: usama.anjum, peterx, liam, ljs, vbabka, jannh, pfalcato, david,
	rppt, surenb, mhocko, zenghui.yu, shuah, linux-mm, linux-kernel,
	linux-fsdevel, linux-kselftest, stable, kernel-team

From: "Kiryl Shutsemau (Meta)" <kas@kernel.org>

Patch 1 fixes PAGEMAP_SCAN reporting a PMD hole in a uffd-wp VMA as
not-written (Cc stable). Patch 2 adds the selftest, split out so it is
not backported.

Changes since v4 [1], addressing David's review:
  - Split the selftest into a separate patch (2/2); only the fix (1/2)
    is Cc stable.
  - Rework the commit message and shorten the code comment.
  - Rework the test: mirror unpopulated_scan_test(), skip-and-continue
    on setup failure, rename to unpopulated_thp_scan_test().

[1] https://lore.kernel.org/all/20260713091710.206548-1-kirill@shutemov.name/

Kiryl Shutsemau (Meta) (2):
  fs/proc/task_mmu: fix PAGEMAP_SCAN written state for PMD holes
  selftests/mm: add PAGEMAP_SCAN test for THP PMD holes

 fs/proc/task_mmu.c                         | 20 ++++++-
 tools/testing/selftests/mm/pagemap_ioctl.c | 66 +++++++++++++++++++++-
 2 files changed, 83 insertions(+), 3 deletions(-)


base-commit: 9795ad96d277c4af049fe30de1cebd4e39d7bcbe
-- 
2.54.0


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

* [PATCH v5 1/2] fs/proc/task_mmu: fix PAGEMAP_SCAN written state for PMD holes
  2026-07-15 14:42 [PATCH v5 0/2] fs/proc/task_mmu: fix PAGEMAP_SCAN written state for PMD holes Kiryl Shutsemau
@ 2026-07-15 14:42 ` Kiryl Shutsemau
  2026-07-16 11:21   ` David Hildenbrand (Arm)
  2026-07-16 12:57   ` Muhammad Usama Anjum
  2026-07-15 14:42 ` [PATCH v5 2/2] selftests/mm: add PAGEMAP_SCAN test for THP " Kiryl Shutsemau
  1 sibling, 2 replies; 8+ messages in thread
From: Kiryl Shutsemau @ 2026-07-15 14:42 UTC (permalink / raw)
  To: akpm
  Cc: usama.anjum, peterx, liam, ljs, vbabka, jannh, pfalcato, david,
	rppt, surenb, mhocko, zenghui.yu, shuah, linux-mm, linux-kernel,
	linux-fsdevel, linux-kselftest, stable, kernel-team

From: "Kiryl Shutsemau (Meta)" <kas@kernel.org>

PAGEMAP_SCAN reports an unpopulated PTE in a uffd-wp VMA as written, but
a range with no page table at all -- a PMD hole -- is skipped:
pagemap_scan_pte_hole() tests p->cur_vma_category, which never carries
PAGE_IS_WRITTEN, so the hole is neither reported nor (under
PM_SCAN_WP_MATCHING) armed.

In a uffd-wp VMA, WP_UNPOPULATED installs uffd-wp markers when protecting
a range, allocating page tables as needed, so an unpopulated slot is
treated as written -- see the pte_none() handling in
pagemap_page_category(). A missing marker therefore means the range was
zapped, e.g. via MADV_DONTNEED. This applies to anon and shmem VMAs.

An anonymous THP is write-protected in place as a huge PMD, so a
full-PMD MADV_DONTNEED clears it to pmd_none -- a hole with no page table
-- and pagemap_scan_pte_hole() misses it. For a MAP_PRIVATE|MAP_ANON
mapping MADV_DONTNEED has fill-with-zeros semantics, so a write-tracking
checkpoint/migration tool (e.g. CRIU) treats the range as unchanged and
keeps its previous contents; after restore or live migration the process
reads stale data instead of zeroes -- data corruption.

Report a hole in a non-hugetlb uffd-wp VMA as written, matching the
pte_none handling in pagemap_page_category(); the existing
PM_SCAN_WP_MATCHING path then arms it via uffd_wp_range().

hugetlb is excluded: pagemap_hugetlb_category() reports an empty hugetlb
entry (huge_pte_none) as not-written, unlike pagemap_page_category(),
which reports pte_none as written. pagemap_scan_pte_hole() fires for a
hugetlb slot only when it has no page table; keeping that not-written
matches how an allocated-but-empty hugetlb entry reads, so the hole and
the empty-entry cases agree within the VMA.

Reported-by: Sashiko AI review <sashiko-bot@kernel.org>
Closes: https://sashiko.dev/#/patchset/20260707151349.92143-1-kirill@shutemov.name
Fixes: 2bad466cc9d9 ("mm/uffd: UFFD_FEATURE_WP_UNPOPULATED")
Cc: Muhammad Usama Anjum <usama.anjum@arm.com>
Cc: Peter Xu <peterx@redhat.com>
Cc: stable@vger.kernel.org
Signed-off-by: Kiryl Shutsemau <kas@kernel.org>
Assisted-by: Claude:claude-fable-5
---
 fs/proc/task_mmu.c | 20 ++++++++++++++++++--
 1 file changed, 18 insertions(+), 2 deletions(-)

diff --git a/fs/proc/task_mmu.c b/fs/proc/task_mmu.c
index d45c729ab6bb..229d1fc3d7f1 100644
--- a/fs/proc/task_mmu.c
+++ b/fs/proc/task_mmu.c
@@ -3049,12 +3049,28 @@ static int pagemap_scan_pte_hole(unsigned long addr, unsigned long end,
 {
 	struct pagemap_scan_private *p = walk->private;
 	struct vm_area_struct *vma = walk->vma;
+	unsigned long categories;
 	int ret, err;
 
-	if (!vma || !pagemap_scan_is_interesting_page(p->cur_vma_category, p))
+	if (!vma)
 		return 0;
 
-	ret = pagemap_scan_output(p->cur_vma_category, p, addr, &end);
+	/*
+	 * In a uffd-wp VMA an unpopulated range is treated as written:
+	 * uffd-wp registration populates page tables and installs markers
+	 * with WP_UNPOPULATED, so a missing marker means the range was
+	 * zapped. See the pte_none() handling in pagemap_page_category().
+	 *
+	 * hugetlb differs, see pagemap_hugetlb_category().
+	 */
+	categories = p->cur_vma_category;
+	if (userfaultfd_wp(vma) && !is_vm_hugetlb_page(vma))
+		categories |= PAGE_IS_WRITTEN;
+
+	if (!pagemap_scan_is_interesting_page(categories, p))
+		return 0;
+
+	ret = pagemap_scan_output(categories, p, addr, &end);
 	if (addr == end)
 		return ret;
 
-- 
2.54.0


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

* [PATCH v5 2/2] selftests/mm: add PAGEMAP_SCAN test for THP PMD holes
  2026-07-15 14:42 [PATCH v5 0/2] fs/proc/task_mmu: fix PAGEMAP_SCAN written state for PMD holes Kiryl Shutsemau
  2026-07-15 14:42 ` [PATCH v5 1/2] " Kiryl Shutsemau
@ 2026-07-15 14:42 ` Kiryl Shutsemau
  2026-07-16 11:25   ` David Hildenbrand (Arm)
  1 sibling, 1 reply; 8+ messages in thread
From: Kiryl Shutsemau @ 2026-07-15 14:42 UTC (permalink / raw)
  To: akpm
  Cc: usama.anjum, peterx, liam, ljs, vbabka, jannh, pfalcato, david,
	rppt, surenb, mhocko, zenghui.yu, shuah, linux-mm, linux-kernel,
	linux-fsdevel, linux-kselftest, stable, kernel-team

From: "Kiryl Shutsemau (Meta)" <kas@kernel.org>

Add coverage for the PMD-hole case fixed by "fs/proc/task_mmu: fix
PAGEMAP_SCAN written state for PMD holes": a MAP_PRIVATE|MAP_ANON THP that
is uffd-wp'd and then dropped with MADV_DONTNEED leaves a pmd_none hole
with no page table, which PAGEMAP_SCAN must still report as written --
checked via both the fast and generic query paths, mirroring
unpopulated_scan_test().

Include <linux/mman.h> for MADV_COLLAPSE; <sys/mman.h> lacks it on older
glibc (e.g. 2.34). Same approach as commit fd5295afae91 ("selftests/mm:
hmm-tests: include linux/mman.h to access MADV_COLLAPSE").

Cc: Muhammad Usama Anjum <usama.anjum@arm.com>
Cc: Peter Xu <peterx@redhat.com>
Cc: Zenghui Yu <zenghui.yu@linux.dev>
Signed-off-by: Kiryl Shutsemau <kas@kernel.org>
Assisted-by: Claude:claude-fable-5
---
 tools/testing/selftests/mm/pagemap_ioctl.c | 66 +++++++++++++++++++++-
 1 file changed, 65 insertions(+), 1 deletion(-)

diff --git a/tools/testing/selftests/mm/pagemap_ioctl.c b/tools/testing/selftests/mm/pagemap_ioctl.c
index 550d1f2c059b..ffa3328185e6 100644
--- a/tools/testing/selftests/mm/pagemap_ioctl.c
+++ b/tools/testing/selftests/mm/pagemap_ioctl.c
@@ -8,6 +8,7 @@
 #include <errno.h>
 #include <malloc.h>
 #include <linux/types.h>
+#include <linux/mman.h>
 #include <linux/memfd.h>
 #include <linux/userfaultfd.h>
 #include <linux/fs.h>
@@ -1102,6 +1103,68 @@ static void unpopulated_scan_test(void)
 	munmap(mem, mem_size);
 }
 
+/*
+ * Like unpopulated_scan_test(), but the range is a MAP_PRIVATE|MAP_ANON THP
+ * that is uffd-wp'd and then dropped with MADV_DONTNEED. That leaves a
+ * pmd_none hole with no page table, which pagemap_page_category() never sees;
+ * PAGEMAP_SCAN must still report it written via pagemap_scan_pte_hole().
+ */
+static void unpopulated_thp_scan_test(void)
+{
+	struct page_region regions[16];
+	long fast = 0, slow = 0, ret;
+	int npages, i;
+	char *area, *mem;
+
+	if (!hpage_size) {
+		ksft_test_result_skip("%s THP not supported\n", __func__);
+		return;
+	}
+	npages = hpage_size / page_size;
+
+	/* Over-allocate so a PMD-aligned, THP-sized range fits inside. */
+	area = mmap(NULL, 2 * hpage_size, PROT_READ | PROT_WRITE,
+		    MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
+	if (area == MAP_FAILED) {
+		ksft_test_result_skip("%s mmap failed\n", __func__);
+		return;
+	}
+	mem = (char *)(((unsigned long)area + hpage_size - 1) & ~(hpage_size - 1));
+
+	wp_init(mem, hpage_size);
+
+	/* Populate, collapse to a THP, then drop: a pmd_none hole, no marker. */
+	memset(mem, 1, hpage_size);
+	if (madvise(mem, hpage_size, MADV_COLLAPSE) ||
+	    !check_huge_anon(mem, 1, hpage_size)) {
+		ksft_test_result_skip("%s could not form a THP\n", __func__);
+		goto out;
+	}
+	if (madvise(mem, hpage_size, MADV_DONTNEED)) {
+		ksft_test_result_skip("%s MADV_DONTNEED failed\n", __func__);
+		goto out;
+	}
+
+	/* Fast path: category_mask == return_mask == PAGE_IS_WRITTEN. */
+	ret = pagemap_ioctl(mem, hpage_size, regions, ARRAY_SIZE(regions), 0, 0,
+			    PAGE_IS_WRITTEN, 0, 0, PAGE_IS_WRITTEN);
+	for (i = 0; ret > 0 && i < ret; i++)
+		fast += LEN(regions[i]);
+
+	/* Generic path: same query expressed via category_anyof_mask. */
+	ret = pagemap_ioctl(mem, hpage_size, regions, ARRAY_SIZE(regions), 0, 0,
+			    0, PAGE_IS_WRITTEN, 0, PAGE_IS_WRITTEN);
+	for (i = 0; ret > 0 && i < ret; i++)
+		slow += LEN(regions[i]);
+
+	ksft_test_result(fast == npages && slow == npages,
+			 "%s pmd-hole reported written by both paths (%ld, %ld of %d)\n",
+			 __func__, fast, slow, npages);
+out:
+	wp_free(mem, hpage_size);
+	munmap(area, 2 * hpage_size);
+}
+
 int sanity_tests(void)
 {
 	unsigned long long mem_size, vec_size;
@@ -1610,7 +1673,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(118);
+	ksft_set_plan(119);
 
 	page_size = getpagesize();
 	hpage_size = read_pmd_pagesize();
@@ -1790,6 +1853,7 @@ int main(int __attribute__((unused)) argc, char *argv[])
 
 	/* 18. Unpopulated pte scan-path consistency */
 	unpopulated_scan_test();
+	unpopulated_thp_scan_test();
 
 	close(pagemap_fd);
 	ksft_finished();
-- 
2.54.0


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

* Re: [PATCH v5 1/2] fs/proc/task_mmu: fix PAGEMAP_SCAN written state for PMD holes
  2026-07-15 14:42 ` [PATCH v5 1/2] " Kiryl Shutsemau
@ 2026-07-16 11:21   ` David Hildenbrand (Arm)
  2026-07-16 12:57   ` Muhammad Usama Anjum
  1 sibling, 0 replies; 8+ messages in thread
From: David Hildenbrand (Arm) @ 2026-07-16 11:21 UTC (permalink / raw)
  To: Kiryl Shutsemau, akpm
  Cc: usama.anjum, peterx, liam, ljs, vbabka, jannh, pfalcato, rppt,
	surenb, mhocko, zenghui.yu, shuah, linux-mm, linux-kernel,
	linux-fsdevel, linux-kselftest, stable, kernel-team

On 7/15/26 16:42, Kiryl Shutsemau wrote:
> From: "Kiryl Shutsemau (Meta)" <kas@kernel.org>
> 
> PAGEMAP_SCAN reports an unpopulated PTE in a uffd-wp VMA as written, but
> a range with no page table at all -- a PMD hole -- is skipped:
> pagemap_scan_pte_hole() tests p->cur_vma_category, which never carries
> PAGE_IS_WRITTEN, so the hole is neither reported nor (under
> PM_SCAN_WP_MATCHING) armed.
> 
> In a uffd-wp VMA, WP_UNPOPULATED installs uffd-wp markers when protecting
> a range, allocating page tables as needed, so an unpopulated slot is
> treated as written -- see the pte_none() handling in
> pagemap_page_category(). A missing marker therefore means the range was
> zapped, e.g. via MADV_DONTNEED. This applies to anon and shmem VMAs.
> 
> An anonymous THP is write-protected in place as a huge PMD, so a
> full-PMD MADV_DONTNEED clears it to pmd_none -- a hole with no page table
> -- and pagemap_scan_pte_hole() misses it. For a MAP_PRIVATE|MAP_ANON
> mapping MADV_DONTNEED has fill-with-zeros semantics, so a write-tracking
> checkpoint/migration tool (e.g. CRIU) treats the range as unchanged and
> keeps its previous contents; after restore or live migration the process
> reads stale data instead of zeroes -- data corruption.
> 
> Report a hole in a non-hugetlb uffd-wp VMA as written, matching the
> pte_none handling in pagemap_page_category(); the existing
> PM_SCAN_WP_MATCHING path then arms it via uffd_wp_range().
> 
> hugetlb is excluded: pagemap_hugetlb_category() reports an empty hugetlb
> entry (huge_pte_none) as not-written, unlike pagemap_page_category(),
> which reports pte_none as written. pagemap_scan_pte_hole() fires for a
> hugetlb slot only when it has no page table; keeping that not-written
> matches how an allocated-but-empty hugetlb entry reads, so the hole and
> the empty-entry cases agree within the VMA.
> 
> Reported-by: Sashiko AI review <sashiko-bot@kernel.org>
> Closes: https://sashiko.dev/#/patchset/20260707151349.92143-1-kirill@shutemov.name
> Fixes: 2bad466cc9d9 ("mm/uffd: UFFD_FEATURE_WP_UNPOPULATED")
> Cc: Muhammad Usama Anjum <usama.anjum@arm.com>
> Cc: Peter Xu <peterx@redhat.com>
> Cc: stable@vger.kernel.org
> Signed-off-by: Kiryl Shutsemau <kas@kernel.org>
> Assisted-by: Claude:claude-fable-5
> ---

Thanks!

Acked-by: David Hildenbrand (Arm) <david@kernel.org>

-- 
Cheers,

David

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

* Re: [PATCH v5 2/2] selftests/mm: add PAGEMAP_SCAN test for THP PMD holes
  2026-07-15 14:42 ` [PATCH v5 2/2] selftests/mm: add PAGEMAP_SCAN test for THP " Kiryl Shutsemau
@ 2026-07-16 11:25   ` David Hildenbrand (Arm)
  2026-07-16 13:03     ` Kiryl Shutsemau
  0 siblings, 1 reply; 8+ messages in thread
From: David Hildenbrand (Arm) @ 2026-07-16 11:25 UTC (permalink / raw)
  To: Kiryl Shutsemau, akpm
  Cc: usama.anjum, peterx, liam, ljs, vbabka, jannh, pfalcato, rppt,
	surenb, mhocko, zenghui.yu, shuah, linux-mm, linux-kernel,
	linux-fsdevel, linux-kselftest, stable, kernel-team


> +	/* Fast path: category_mask == return_mask == PAGE_IS_WRITTEN. */
> +	ret = pagemap_ioctl(mem, hpage_size, regions, ARRAY_SIZE(regions), 0, 0,
> +			    PAGE_IS_WRITTEN, 0, 0, PAGE_IS_WRITTEN);
> +	for (i = 0; ret > 0 && i < ret; i++)
> +		fast += LEN(regions[i]);
> +
> +	/* Generic path: same query expressed via category_anyof_mask. */
> +	ret = pagemap_ioctl(mem, hpage_size, regions, ARRAY_SIZE(regions), 0, 0,
> +			    0, PAGE_IS_WRITTEN, 0, PAGE_IS_WRITTEN);
> +	for (i = 0; ret > 0 && i < ret; i++)
> +		slow += LEN(regions[i]);
> +
> +	ksft_test_result(fast == npages && slow == npages,
> +			 "%s pmd-hole reported written by both paths (%ld, %ld of %d)\n",
> +			 __func__, fast, slow, npages);
> +out:
> +	wp_free(mem, hpage_size);
> +	munmap(area, 2 * hpage_size);
> +}

Sorry if I wasn't clear. I think unpopulated_thp_scan_test() can actually reuse
bits of unpopulated_scan_test().

Either by having a magical "use_thp" parameter or by factoring stuff out into a
helper.

Or am I missing an important point why that would not work or be really ugly?

-- 
Cheers,

David

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

* Re: [PATCH v5 1/2] fs/proc/task_mmu: fix PAGEMAP_SCAN written state for PMD holes
  2026-07-15 14:42 ` [PATCH v5 1/2] " Kiryl Shutsemau
  2026-07-16 11:21   ` David Hildenbrand (Arm)
@ 2026-07-16 12:57   ` Muhammad Usama Anjum
  1 sibling, 0 replies; 8+ messages in thread
From: Muhammad Usama Anjum @ 2026-07-16 12:57 UTC (permalink / raw)
  To: Kiryl Shutsemau, akpm
  Cc: usama.anjum, peterx, liam, ljs, vbabka, jannh, pfalcato, david,
	rppt, surenb, mhocko, zenghui.yu, shuah, linux-mm, linux-kernel,
	linux-fsdevel, linux-kselftest, stable, kernel-team

On 15/07/2026 3:42 pm, Kiryl Shutsemau wrote:
> From: "Kiryl Shutsemau (Meta)" <kas@kernel.org>
> 
> PAGEMAP_SCAN reports an unpopulated PTE in a uffd-wp VMA as written, but
> a range with no page table at all -- a PMD hole -- is skipped:
> pagemap_scan_pte_hole() tests p->cur_vma_category, which never carries
> PAGE_IS_WRITTEN, so the hole is neither reported nor (under
> PM_SCAN_WP_MATCHING) armed.
> 
> In a uffd-wp VMA, WP_UNPOPULATED installs uffd-wp markers when protecting
> a range, allocating page tables as needed, so an unpopulated slot is
> treated as written -- see the pte_none() handling in
> pagemap_page_category(). A missing marker therefore means the range was
> zapped, e.g. via MADV_DONTNEED. This applies to anon and shmem VMAs.
> 
> An anonymous THP is write-protected in place as a huge PMD, so a
> full-PMD MADV_DONTNEED clears it to pmd_none -- a hole with no page table
> -- and pagemap_scan_pte_hole() misses it. For a MAP_PRIVATE|MAP_ANON
> mapping MADV_DONTNEED has fill-with-zeros semantics, so a write-tracking
> checkpoint/migration tool (e.g. CRIU) treats the range as unchanged and
> keeps its previous contents; after restore or live migration the process
> reads stale data instead of zeroes -- data corruption.
> 
> Report a hole in a non-hugetlb uffd-wp VMA as written, matching the
> pte_none handling in pagemap_page_category(); the existing
> PM_SCAN_WP_MATCHING path then arms it via uffd_wp_range().
> 
> hugetlb is excluded: pagemap_hugetlb_category() reports an empty hugetlb
> entry (huge_pte_none) as not-written, unlike pagemap_page_category(),
> which reports pte_none as written. pagemap_scan_pte_hole() fires for a
> hugetlb slot only when it has no page table; keeping that not-written
> matches how an allocated-but-empty hugetlb entry reads, so the hole and
> the empty-entry cases agree within the VMA.
> 
> Reported-by: Sashiko AI review <sashiko-bot@kernel.org>
> Closes: https://sashiko.dev/#/patchset/20260707151349.92143-1-kirill@shutemov.name
> Fixes: 2bad466cc9d9 ("mm/uffd: UFFD_FEATURE_WP_UNPOPULATED")
> Cc: Muhammad Usama Anjum <usama.anjum@arm.com>
> Cc: Peter Xu <peterx@redhat.com>
> Cc: stable@vger.kernel.org
> Signed-off-by: Kiryl Shutsemau <kas@kernel.org>
> Assisted-by: Claude:claude-fable-5
Tested-by: Muhammad Usama Anjum <usama.anjum@arm.com>


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

* Re: [PATCH v5 2/2] selftests/mm: add PAGEMAP_SCAN test for THP PMD holes
  2026-07-16 11:25   ` David Hildenbrand (Arm)
@ 2026-07-16 13:03     ` Kiryl Shutsemau
  2026-07-16 14:40       ` David Hildenbrand (Arm)
  0 siblings, 1 reply; 8+ messages in thread
From: Kiryl Shutsemau @ 2026-07-16 13:03 UTC (permalink / raw)
  To: David Hildenbrand (Arm)
  Cc: akpm, usama.anjum, peterx, liam, ljs, vbabka, jannh, pfalcato,
	rppt, surenb, mhocko, zenghui.yu, shuah, linux-mm, linux-kernel,
	linux-fsdevel, linux-kselftest, stable, kernel-team

On Thu, Jul 16, 2026 at 01:25:22PM +0200, David Hildenbrand (Arm) wrote:
> 
> > +	/* Fast path: category_mask == return_mask == PAGE_IS_WRITTEN. */
> > +	ret = pagemap_ioctl(mem, hpage_size, regions, ARRAY_SIZE(regions), 0, 0,
> > +			    PAGE_IS_WRITTEN, 0, 0, PAGE_IS_WRITTEN);
> > +	for (i = 0; ret > 0 && i < ret; i++)
> > +		fast += LEN(regions[i]);
> > +
> > +	/* Generic path: same query expressed via category_anyof_mask. */
> > +	ret = pagemap_ioctl(mem, hpage_size, regions, ARRAY_SIZE(regions), 0, 0,
> > +			    0, PAGE_IS_WRITTEN, 0, PAGE_IS_WRITTEN);
> > +	for (i = 0; ret > 0 && i < ret; i++)
> > +		slow += LEN(regions[i]);
> > +
> > +	ksft_test_result(fast == npages && slow == npages,
> > +			 "%s pmd-hole reported written by both paths (%ld, %ld of %d)\n",
> > +			 __func__, fast, slow, npages);
> > +out:
> > +	wp_free(mem, hpage_size);
> > +	munmap(area, 2 * hpage_size);
> > +}
> 
> Sorry if I wasn't clear. I think unpopulated_thp_scan_test() can actually reuse
> bits of unpopulated_scan_test().

Ah. Make sense.

Here's my take on this. Looks good?

-- >8 --
Date: Thu, 16 Jul 2026 13:24:29 +0100
Subject: [PATCH v6 2/2] selftests/mm: add PAGEMAP_SCAN test for THP PMD holes

Add coverage for the PMD-hole case fixed by "fs/proc/task_mmu: fix
PAGEMAP_SCAN written state for PMD holes": a MAP_PRIVATE|MAP_ANON THP that
is uffd-wp'd and then dropped with MADV_DONTNEED leaves a pmd_none hole
with no page table, which PAGEMAP_SCAN must still report as written.

Factor the populate/drop/scan-both-paths sequence out of
unpopulated_scan_test() into a helper, and add unpopulated_thp_scan_test()
that reuses it with a THP.

Include <linux/mman.h> for MADV_COLLAPSE; <sys/mman.h> lacks it on older
glibc (e.g. 2.34). Same approach as commit fd5295afae91 ("selftests/mm:
hmm-tests: include linux/mman.h to access MADV_COLLAPSE").

Cc: Muhammad Usama Anjum <usama.anjum@arm.com>
Cc: Peter Xu <peterx@redhat.com>
Cc: Zenghui Yu <zenghui.yu@linux.dev>
Signed-off-by: Kiryl Shutsemau <kas@kernel.org>
Assisted-by: Claude:claude-fable-5
---
 tools/testing/selftests/mm/pagemap_ioctl.c | 118 +++++++++++++++------
 1 file changed, 83 insertions(+), 35 deletions(-)

diff --git a/tools/testing/selftests/mm/pagemap_ioctl.c b/tools/testing/selftests/mm/pagemap_ioctl.c
index 550d1f2c059b..693030f1e631 100644
--- a/tools/testing/selftests/mm/pagemap_ioctl.c
+++ b/tools/testing/selftests/mm/pagemap_ioctl.c
@@ -8,6 +8,7 @@
 #include <errno.h>
 #include <malloc.h>
 #include <linux/types.h>
+#include <linux/mman.h>
 #include <linux/memfd.h>
 #include <linux/userfaultfd.h>
 #include <linux/fs.h>
@@ -1058,50 +1059,96 @@ static void test_simple(void)
  * the generic path (reached e.g. via category_anyof_mask) must report every
  * page written.
  */
+/*
+ * Populate @mem (optionally collapsing it into a THP first), drop it with
+ * MADV_DONTNEED, then check PAGEMAP_SCAN reports the whole range written via
+ * both the fast and generic query paths. A dropped THP leaves a pmd_none hole
+ * with no page table, exercising pagemap_scan_pte_hole(); a base-page range
+ * leaves pte_none entries.
+ */
+static void unpopulated_written_test(const char *name, char *mem, long size,
+				     bool use_thp)
+{
+	long npages = size / page_size, fast = 0, slow = 0, ret;
+	struct page_region regions[16];
+	int i;
+
+	wp_init(mem, size);
+
+	/* Populate, optionally collapse to a THP, then drop it. */
+	memset(mem, 1, size);
+	if (use_thp &&
+	    (madvise(mem, size, MADV_COLLAPSE) ||
+	     !check_huge_anon(mem, size / hpage_size, hpage_size))) {
+		ksft_test_result_skip("%s could not form a THP\n", name);
+		goto out;
+	}
+	if (madvise(mem, size, MADV_DONTNEED)) {
+		ksft_test_result_fail("%s MADV_DONTNEED failed\n", name);
+		goto out;
+	}
+
+	/* Fast path: category_mask == return_mask == PAGE_IS_WRITTEN. */
+	ret = pagemap_ioctl(mem, size, regions, ARRAY_SIZE(regions), 0, 0,
+			    PAGE_IS_WRITTEN, 0, 0, PAGE_IS_WRITTEN);
+	for (i = 0; ret > 0 && i < ret; i++)
+		fast += LEN(regions[i]);
+
+	/* Generic path: same query expressed via category_anyof_mask. */
+	ret = pagemap_ioctl(mem, size, regions, ARRAY_SIZE(regions), 0, 0,
+			    0, PAGE_IS_WRITTEN, 0, PAGE_IS_WRITTEN);
+	for (i = 0; ret > 0 && i < ret; i++)
+		slow += LEN(regions[i]);
+
+	ksft_test_result(fast == npages && slow == npages,
+			 "%s unpopulated range reported written by both paths (%ld, %ld of %ld)\n",
+			 name, fast, slow, npages);
+out:
+	wp_free(mem, size);
+}
+
 static void unpopulated_scan_test(void)
 {
-	int npages = 16, i;
-	long mem_size = npages * page_size;
-	struct page_region regions[16];
-	long fast = 0, slow = 0, ret;
+	long mem_size = 16 * page_size;
 	char *mem;
 
 	mem = mmap(NULL, mem_size, PROT_READ | PROT_WRITE,
 		   MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
-	if (mem == MAP_FAILED)
-		ksft_exit_fail_msg("%s mmap failed\n", __func__);
+	if (mem == MAP_FAILED) {
+		ksft_test_result_skip("%s mmap failed\n", __func__);
+		return;
+	}
 
-	wp_init(mem, mem_size);
-
-	/* Populate, then drop: the ptes become pte_none without a marker. */
-	memset(mem, 1, mem_size);
-	if (madvise(mem, mem_size, MADV_DONTNEED))
-		ksft_exit_fail_msg("%s MADV_DONTNEED failed\n", __func__);
-
-	/* Fast path: category_mask == return_mask == PAGE_IS_WRITTEN. */
-	ret = pagemap_ioctl(mem, mem_size, regions, npages, 0, 0,
-			    PAGE_IS_WRITTEN, 0, 0, PAGE_IS_WRITTEN);
-	if (ret < 0)
-		ksft_exit_fail_msg("%s fast scan failed\n", __func__);
-	for (i = 0; i < ret; i++)
-		fast += LEN(regions[i]);
-
-	/* Generic path: same query expressed via category_anyof_mask. */
-	ret = pagemap_ioctl(mem, mem_size, regions, npages, 0, 0,
-			    0, PAGE_IS_WRITTEN, 0, PAGE_IS_WRITTEN);
-	if (ret < 0)
-		ksft_exit_fail_msg("%s generic scan failed\n", __func__);
-	for (i = 0; i < ret; i++)
-		slow += LEN(regions[i]);
-
-	ksft_test_result(fast == npages && slow == npages,
-			 "%s unpopulated ptes reported written by both paths (%ld, %ld of %d)\n",
-			 __func__, fast, slow, npages);
-
-	wp_free(mem, mem_size);
+	unpopulated_written_test(__func__, mem, mem_size, false);
 	munmap(mem, mem_size);
 }
 
+/*
+ * Same as unpopulated_scan_test(), but the range is a THP: a full-PMD
+ * MADV_DONTNEED leaves a pmd_none hole with no page table.
+ */
+static void unpopulated_thp_scan_test(void)
+{
+	char *area, *mem;
+
+	if (!hpage_size) {
+		ksft_test_result_skip("%s THP not supported\n", __func__);
+		return;
+	}
+
+	/* Over-allocate so a PMD-aligned, THP-sized range fits inside. */
+	area = mmap(NULL, 2 * hpage_size, PROT_READ | PROT_WRITE,
+		    MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
+	if (area == MAP_FAILED) {
+		ksft_test_result_skip("%s mmap failed\n", __func__);
+		return;
+	}
+	mem = (char *)(((unsigned long)area + hpage_size - 1) & ~(hpage_size - 1));
+
+	unpopulated_written_test(__func__, mem, hpage_size, true);
+	munmap(area, 2 * hpage_size);
+}
+
 int sanity_tests(void)
 {
 	unsigned long long mem_size, vec_size;
@@ -1610,7 +1657,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(118);
+	ksft_set_plan(119);
 
 	page_size = getpagesize();
 	hpage_size = read_pmd_pagesize();
@@ -1790,6 +1837,7 @@ int main(int __attribute__((unused)) argc, char *argv[])
 
 	/* 18. Unpopulated pte scan-path consistency */
 	unpopulated_scan_test();
+	unpopulated_thp_scan_test();
 
 	close(pagemap_fd);
 	ksft_finished();
-- 
  Kiryl Shutsemau / Kirill A. Shutemov

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

* Re: [PATCH v5 2/2] selftests/mm: add PAGEMAP_SCAN test for THP PMD holes
  2026-07-16 13:03     ` Kiryl Shutsemau
@ 2026-07-16 14:40       ` David Hildenbrand (Arm)
  0 siblings, 0 replies; 8+ messages in thread
From: David Hildenbrand (Arm) @ 2026-07-16 14:40 UTC (permalink / raw)
  To: Kiryl Shutsemau
  Cc: akpm, usama.anjum, peterx, liam, ljs, vbabka, jannh, pfalcato,
	rppt, surenb, mhocko, zenghui.yu, shuah, linux-mm, linux-kernel,
	linux-fsdevel, linux-kselftest, stable, kernel-team

On 7/16/26 15:03, Kiryl Shutsemau wrote:
> On Thu, Jul 16, 2026 at 01:25:22PM +0200, David Hildenbrand (Arm) wrote:
>>
>>> +	/* Fast path: category_mask == return_mask == PAGE_IS_WRITTEN. */
>>> +	ret = pagemap_ioctl(mem, hpage_size, regions, ARRAY_SIZE(regions), 0, 0,
>>> +			    PAGE_IS_WRITTEN, 0, 0, PAGE_IS_WRITTEN);
>>> +	for (i = 0; ret > 0 && i < ret; i++)
>>> +		fast += LEN(regions[i]);
>>> +
>>> +	/* Generic path: same query expressed via category_anyof_mask. */
>>> +	ret = pagemap_ioctl(mem, hpage_size, regions, ARRAY_SIZE(regions), 0, 0,
>>> +			    0, PAGE_IS_WRITTEN, 0, PAGE_IS_WRITTEN);
>>> +	for (i = 0; ret > 0 && i < ret; i++)
>>> +		slow += LEN(regions[i]);
>>> +
>>> +	ksft_test_result(fast == npages && slow == npages,
>>> +			 "%s pmd-hole reported written by both paths (%ld, %ld of %d)\n",
>>> +			 __func__, fast, slow, npages);
>>> +out:
>>> +	wp_free(mem, hpage_size);
>>> +	munmap(area, 2 * hpage_size);
>>> +}
>>
>> Sorry if I wasn't clear. I think unpopulated_thp_scan_test() can actually reuse
>> bits of unpopulated_scan_test().
> 
> Ah. Make sense.
> 
> Here's my take on this. Looks good?

Yes, thanks!

-- 
Cheers,

David

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

end of thread, other threads:[~2026-07-16 14:40 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-07-15 14:42 [PATCH v5 0/2] fs/proc/task_mmu: fix PAGEMAP_SCAN written state for PMD holes Kiryl Shutsemau
2026-07-15 14:42 ` [PATCH v5 1/2] " Kiryl Shutsemau
2026-07-16 11:21   ` David Hildenbrand (Arm)
2026-07-16 12:57   ` Muhammad Usama Anjum
2026-07-15 14:42 ` [PATCH v5 2/2] selftests/mm: add PAGEMAP_SCAN test for THP " Kiryl Shutsemau
2026-07-16 11:25   ` David Hildenbrand (Arm)
2026-07-16 13:03     ` Kiryl Shutsemau
2026-07-16 14:40       ` David Hildenbrand (Arm)

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox