]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
ksm_tests: skip hugepage test when Transparent Hugepages are disabled
authorLi Wang <liwang@redhat.com>
Tue, 24 Jun 2025 03:27:48 +0000 (11:27 +0800)
committerAndrew Morton <akpm@linux-foundation.org>
Thu, 10 Jul 2025 05:42:23 +0000 (22:42 -0700)
Some systems (e.g.  minimal or real-time kernels) may not enable
Transparent Hugepages (THP), causing MADV_HUGEPAGE to return EINVAL.  This
patch introduces a runtime check using the existing THP sysfs interface
and skips the hugepage merging test (`-H`) when THP is not available.

To avoid those failures:

  # -----------------------------
  # running ./ksm_tests -H -s 100
  # -----------------------------
  # ksm_tests: MADV_HUGEPAGE: Invalid argument
  # [FAIL]
  not ok 1 ksm_tests -H -s 100 # exit=2

  # --------------------
  # running ./khugepaged
  # --------------------
  # Reading PMD pagesize failed# [FAIL]
  not ok 1 khugepaged # exit=1

  # --------------------
  # running ./soft-dirty
  # --------------------
  # TAP version 13
  # 1..15
  # ok 1 Test test_simple
  # ok 2 Test test_vma_reuse dirty bit of allocated page
  # ok 3 Test test_vma_reuse dirty bit of reused address page
  # Bail out! Reading PMD pagesize failed# Planned tests != run tests (15 != 3)
  # # Totals: pass:3 fail:0 xfail:0 xpass:0 skip:0 error:0
  # [FAIL]
  not ok 1 soft-dirty # exit=1
  # SUMMARY: PASS=0 SKIP=0 FAIL=1

  # -------------------
  # running ./migration
  # -------------------
  # TAP version 13
  # 1..3
  # # Starting 3 tests from 1 test cases.
  # #  RUN           migration.private_anon ...
  # #            OK  migration.private_anon
  # ok 1 migration.private_anon
  # #  RUN           migration.shared_anon ...
  # #            OK  migration.shared_anon
  # ok 2 migration.shared_anon
  # #  RUN           migration.private_anon_thp ...
  # # migration.c:196:private_anon_thp:Expected madvise(ptr, TWOMEG, MADV_HUGEPAGE) (-1) == 0 (0)
  # # private_anon_thp: Test terminated by assertion
  # #          FAIL  migration.private_anon_thp
  # not ok 3 migration.private_anon_thp
  # # FAILED: 2 / 3 tests passed.
  # # Totals: pass:2 fail:1 xfail:0 xpass:0 skip:0 error:0
  # [FAIL]
  not ok 1 migration # exit=1

It's true that CONFIG_TRANSPARENT_HUGEPAGE=y is explicitly enabled in
tools/testing/selftests/mm/config, so ideally the runtime environment
should also support THP.

However, in practice, we've found that on some systems:

- THP is disabled at boot time (transparent_hugepage=never)
- Or manually disabled via sysfs
- Or unavailable in RT kernels, containers, or minimal CI environments

In these cases, the test will fail with EINVAL on madvise(MADV_HUGEPAGE),
even though the kernel config is correct.

To make the test suite more robust and avoid false negatives, this patch
adds a runtime check for /sys/kernel/mm/transparent_hugepage/enabled.

If THP is not available, the hugepage test (-H) is skipped with a clear
message.

Link: https://lkml.kernel.org/r/20250624032748.393836-1-liwang@redhat.com
Signed-off-by: Li Wang <liwang@redhat.com>
Cc: Aruna Ramakrishna <aruna.ramakrishna@oracle.com>
Cc: Bagas Sanjaya <bagasdotme@gmail.com>
Cc: Catalin Marinas <catalin.marinas@arm.com>
Cc: Dave Hansen <dave.hansen@linux.intel.com>
Cc: David Hildenbrand <david@redhat.com>
Cc: Joey Gouly <joey.gouly@arm.com>
Cc: Johannes Weiner <hannes@cmpxchg.org>
Cc: Keith Lucas <keith.lucas@oracle.com>
Cc: Ryan Roberts <ryan.roberts@arm.com>
Cc: Shuah Khan <shuah@kernel.org>
Cc: Lorenzo Stoakes <lorenzo.stoakes@oracle.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
tools/testing/selftests/mm/khugepaged.c
tools/testing/selftests/mm/ksm_tests.c
tools/testing/selftests/mm/migration.c
tools/testing/selftests/mm/soft-dirty.c
tools/testing/selftests/mm/thp_settings.c
tools/testing/selftests/mm/thp_settings.h

index 4341ce6b3b382bae1d4b02002ecf6e256398ca3e..a18c50d5114186a6652bd3a3bf5ced370b17f842 100644 (file)
@@ -1188,6 +1188,11 @@ int main(int argc, char **argv)
                .read_ahead_kb = 0,
        };
 
+       if (!thp_is_enabled()) {
+               printf("Transparent Hugepages not available\n");
+               return KSFT_SKIP;
+       }
+
        parse_test_type(argc, argv);
 
        setbuf(stdout, NULL);
index e80deac1436be29dff4a442393da9e1570726f47..b77462b5c240bff064b31492ecab795da93354e1 100644 (file)
@@ -15,6 +15,7 @@
 #include "../kselftest.h"
 #include <include/vdso/time64.h>
 #include "vm_util.h"
+#include "thp_settings.h"
 
 #define KSM_SYSFS_PATH "/sys/kernel/mm/ksm/"
 #define KSM_FP(s) (KSM_SYSFS_PATH s)
@@ -527,6 +528,11 @@ static int ksm_merge_hugepages_time(int merge_type, int mapping, int prot,
        unsigned long scan_time_ns;
        int pagemap_fd, n_normal_pages, n_huge_pages;
 
+       if (!thp_is_enabled()) {
+               printf("Transparent Hugepages not available\n");
+               return KSFT_SKIP;
+       }
+
        map_size *= MB;
        size_t len = map_size;
 
index 1e3a595fbf012f3b0d9a1a2c98f482368bfce0c8..a306f8bab0870019092a64c15eb7b32bf6c919bc 100644 (file)
@@ -5,6 +5,8 @@
  */
 
 #include "../kselftest_harness.h"
+#include "thp_settings.h"
+
 #include <strings.h>
 #include <pthread.h>
 #include <numa.h>
@@ -185,6 +187,9 @@ TEST_F_TIMEOUT(migration, private_anon_thp, 2*RUNTIME)
        uint64_t *ptr;
        int i;
 
+       if (!thp_is_enabled())
+               SKIP(return, "Transparent Hugepages not available");
+
        if (self->nthreads < 2 || self->n1 < 0 || self->n2 < 0)
                SKIP(return, "Not enough threads or NUMA nodes available");
 
@@ -214,6 +219,9 @@ TEST_F_TIMEOUT(migration, shared_anon_thp, 2*RUNTIME)
        uint64_t *ptr;
        int i;
 
+       if (!thp_is_enabled())
+               SKIP(return, "Transparent Hugepages not available");
+
        if (self->nthreads < 2 || self->n1 < 0 || self->n2 < 0)
                SKIP(return, "Not enough threads or NUMA nodes available");
 
index 8e1462ce0532657d4502518d653815ef53861890..8a3f2b4b218698775313c5ca4ea7003482446043 100644 (file)
@@ -6,8 +6,10 @@
 #include <stdint.h>
 #include <malloc.h>
 #include <sys/mman.h>
+
 #include "../kselftest.h"
 #include "vm_util.h"
+#include "thp_settings.h"
 
 #define PAGEMAP_FILE_PATH "/proc/self/pagemap"
 #define TEST_ITERATIONS 10000
@@ -78,8 +80,13 @@ static void test_hugepage(int pagemap_fd, int pagesize)
 {
        char *map;
        int i, ret;
-       size_t hpage_len = read_pmd_pagesize();
 
+       if (!thp_is_enabled()) {
+               ksft_test_result_skip("Transparent Hugepages not available\n");
+               return;
+       }
+
+       size_t hpage_len = read_pmd_pagesize();
        if (!hpage_len)
                ksft_exit_fail_msg("Reading PMD pagesize failed");
 
index ad872af1c81aa23312e2b2de77bbe1b2c5ceda96..bad60ac52874a58a50ac01be0055e7470bed5fdf 100644 (file)
@@ -381,3 +381,14 @@ unsigned long thp_shmem_supported_orders(void)
 {
        return __thp_supported_orders(true);
 }
+
+bool thp_is_enabled(void)
+{
+       if (access(THP_SYSFS, F_OK) != 0)
+               return false;
+
+       int mode = thp_read_string("enabled", thp_enabled_strings);
+
+       /* THP is considered enabled if it's either "always" or "madvise" */
+       return mode == 1 || mode == 3;
+}
index fc131d23d59308a8cc0bdd60f4f694af01ac856f..6c07f70beee973f6f14866a235e5719cee3d1ade 100644 (file)
@@ -84,4 +84,6 @@ void thp_set_read_ahead_path(char *path);
 unsigned long thp_supported_orders(void);
 unsigned long thp_shmem_supported_orders(void);
 
+bool thp_is_enabled(void);
+
 #endif /* __THP_SETTINGS_H__ */