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

The page cache caps folio order at MAX_PAGECACHE_ORDER, and
xas_split_alloc() puts that cap below the PMD order where a PMD is 512M --
arm64 with 64K base pages, as include/linux/pagemap.h says outright.
shmem_huge_global_enabled() then offers no PMD order at all, so
MADV_COLLAPSE of a shmem range answers -EINVAL and khugepaged passes over
it.

The shmem cases nonetheless ask for a PMD-sized shmem folio, so on such a
configuration four of them fail and the run bails out in the middle:

not ok 2 collapse_full not ok 4 collapse_single_pte_entry # Allocate huge
page...Bail out! madvise(MADV_COLLAPSE): Invalid argument (22)

That is the kernel declining something it deliberately does not support,
not a collapse defect.  Skip those cases where the PMD order is not a shmem
order, which thp_shmem_supported_orders() already reports -- it reads the
same per-size shmem_enabled controls the kernel only publishes for orders
the page cache can hold.  A tmpfs-backed file argument is skipped on the
same grounds, and a run left with nothing to collapse into skips outright.

Anonymous collapse is unaffected: its orders are not capped this way, and
the anonymous cases pass at a 512M PMD.

Assisted-by: Claude-Code:claude-opus-5
Signed-off-by: Kiryl Shutsemau (Meta) <[email protected]>
---
 tools/testing/selftests/mm/khugepaged.c | 21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)

diff --git a/tools/testing/selftests/mm/khugepaged.c 
b/tools/testing/selftests/mm/khugepaged.c
index 9213ce1658d0..c5a3c3922581 100644
--- a/tools/testing/selftests/mm/khugepaged.c
+++ b/tools/testing/selftests/mm/khugepaged.c
@@ -1584,6 +1584,27 @@ int main(int argc, char **argv)
        hpage_pmd_nr = hpage_pmd_size / page_size;
        hpage_pmd_order = __builtin_ctz(hpage_pmd_nr);
 
+       /*
+        * The page cache caps folio order at MAX_PAGECACHE_ORDER, which
+        * xas_split_alloc() puts below the PMD order on arm64 with 64K pages.
+        * A PMD-sized page cache folio is then impossible, so the kernel
+        * refuses these collapses by design and there is nothing to test.
+        */
+       if (!(thp_shmem_supported_orders() & (1UL << hpage_pmd_order))) {
+               if (shmem_ops) {
+                       ksft_print_msg("no PMD-order page cache folio: skipping 
shmem\n");
+                       shmem_ops = NULL;
+               }
+               if (finfo.type == VMA_SHMEM && read_only_file_ops) {
+                       ksft_print_msg("no PMD-order page cache folio: skipping 
tmpfs file\n");
+                       read_only_file_ops = NULL;
+                       read_write_file_read_ops = NULL;
+                       read_write_file_write_ops = NULL;
+               }
+               if (!anon_ops && !shmem_ops && !read_only_file_ops)
+                       ksft_exit_skip("Nothing left to collapse into\n");
+       }
+
        if (anon_target_order &&
            !(thp_supported_orders() & (1UL << anon_target_order)))
                ksft_exit_skip("Order %d is not a supported anon THP order\n",
-- 
2.54.0


Reply via email to