From: "Kiryl Shutsemau (Meta)" <[email protected]>

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 <[email protected]>
Cc: Peter Xu <[email protected]>
Cc: Zenghui Yu <[email protected]>
Signed-off-by: Kiryl Shutsemau <[email protected]>
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


Reply via email to