selftests/mm: avoid false failures in hugetlb and KSM tests
Subsystems (3)
cover selftests/mm: avoid false failures in hugetlb and KSM tests
Hi all,
This series fixes issues in the hugetlb and KSM MM selftest categories that can report failures when the prerequisites for the tests are not satisfied.
Patch 1 updates the hugetlb selftest helpers to handle -EINVAL when attempting to configure gigantic HugeTLB pages via nr_hugepages. PowerPC hash MMU pSeries systems expose gigantic hugepage sizes but do not allow runtime allocation of such pages, causing the sysfs write to fail. Handle this case gracefully and continue running the test instead of aborting.
Patch 2 fixes the KSM NUMA merge test on systems with memoryless NUMA nodes. The test currently relies on the number of configured NUMA nodes and may attempt allocations on nodes that have no memory, resulting in spurious failures. Count only NUMA nodes that contain memory and skip the test when fewer than two such nodes are available.
These changes improve handling of unsupported test configurations and unmet test prerequisites, avoiding spurious failures.
Thanks,
Sayali
Sayali Patil (2):
selftests/mm: handle EINVAL when configuring gigantic hugepages selftests/mm: fix ksm NUMA merge test for systems with memoryless NUMA
nodes
tools/testing/selftests/mm/ksm_tests.c | 26 ++++++++++++++++++-------- tools/testing/selftests/mm/vm_util.c | 7 +++++++ 2 files changed, 25 insertions(+), 8 deletions(-)
Signature · 1 line
2.52.0
PATCH 1/2 selftests/mm: handle EINVAL when configuring gigantic hugepages
Some MM selftests attempt to configure the amount of HugeTLB pages of different sizes by writing to nr_hugepages.
PowerPC hash MMU pSeries systems advertise gigantic hugepage sizes but do not support runtime allocation of such pages, writes to the corresponding nr_hugepages file fail with -EINVAL. This causes the test to bail out even though the failure is due to a platform limitation rather than the
functionality being tested.
Treat -EINVAL from the sysfs write as a skipped configuration request and continue running the test instead of failing.
Before patch:
-------------------------
running ./hugetlb-madvise
-------------------------
TAP version 13
1..1
[INFO] detected hugetlb page size: 16777216 KiB
[INFO] detected hugetlb page size: 16384 KiB
ok 1 MADV_DONTNEED and MADV_REMOVE on hugetlb
Totals: pass:1 fail:0 xfail:0 xpass:0 skip:0 error:0 Bail out! /sys/kernel/mm/hugepages/hugepages-16777216kB/nr_hugepages write(0) failed: Invalid argument
Totals: pass:0 fail:0 xfail:0 xpass:0 skip:0 error:0 [FAIL]
After patch:
-------------------------
running ./hugetlb-madvise
-------------------------
TAP version 13
1..1
[INFO] detected hugetlb page size: 16777216 KiB [INFO] detected hugetlb page size: 16384 KiB
ok 1 MADV_DONTNEED and MADV_REMOVE on hugetlb
Totals: pass:1 fail:0 xfail:0 xpass:0 skip:0 error:0 /sys/kernel/mm/hugepages/hugepages-16777216kB/nr_hugepages write(0) failed: Invalid argument
[PASS]
--- tools/testing/selftests/mm/vm_util.c | 7 +++++++ 1 file changed, 7 insertions(+)
tools/testing/selftests/mm/vm_util.c +7 -0
@@ -735,6 +735,13 @@ void write_file(const char *path, const char *buf, size_t buflen) saved_errno = errno;close (fd); errno = saved_errno; + +if (numwritten <0 && errno ==EINVAL ) { +ksft_print_msg ("%s write(%.*s) failed: %s\n" , path, + (int )(buflen -1 ), buf,strerror (errno)); +return ; + } +if (numwritten <0 )ksft_exit_fail_msg ("%s write(%.*s) failed: %s\n" , path, (int )(buflen -1 ), buf,strerror (errno));
Signature · 1 line
2.52.0
PATCH 2/2 selftests/mm: fix ksm NUMA merge test for systems with memoryless NUMA nodes
In reply to Sayali PatilThe KSM NUMA merge test allocates identical pages on different NUMA nodes and verifies KSM behavior with merge_across_nodes enabled and disabled.
On systems with memoryless NUMA nodes, for example:
#numactl -H
available: 2 nodes (0,4)
.....
node 0 cpus: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
node 0 size: 14825 MB
node 0 free: 1382 MB
node 4 cpus:
node 4 size: 0 MB
node 4 free: 0 MBthe test may attempt to allocate memory on a node without memory, causing numa_alloc_onnode() to fail and resulting in a spurious test failure.
Add count_mem_nodes() helper to count only nodes with memory and use it instead of numa_num_configured_nodes() to ensure the test runs only on systems with at least two NUMA nodes that have memory. Skip the test otherwise.
Before patch:
---------------------------
running ./ksm_tests -N -m 1
---------------------------
mbind: Invalid argument
ok 1 KSM NUMA merging
Totals: pass:1 fail:0 xfail:0 xpass:0 skip:0 error:0 [PASS]
ok 1 ksm_tests -N -m 1
---------------------------
running ./ksm_tests -N -m 0
---------------------------
mbind: Invalid argument
not ok 1 KSM NUMA merging
Totals: pass:0 fail:1 xfail:0 xpass:0 skip:0 error:0 [FAIL]
not ok 2 ksm_tests -N -m 0 # exit=1
After patch:
---------------------------
running ./ksm_tests -N -m 1
---------------------------
SKIP At least 2 NUMA nodes with memory must be available
Totals: pass:0 fail:0 xfail:0 xpass:0 skip:1 error:0 [SKIP]
ok 1 ksm_tests -N -m 1 # SKIP
---------------------------
running ./ksm_tests -N -m 0
---------------------------
SKIP At least 2 NUMA nodes with memory must be available
Totals: pass:0 fail:0 xfail:0 xpass:0 skip:1 error:0 [SKIP]
ok 2 ksm_tests -N -m 0 # SKIP
--- tools/testing/selftests/mm/ksm_tests.c | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-)
tools/testing/selftests/mm/ksm_tests.c +18 -8
@@ -450,6 +450,18 @@ static int get_first_mem_node(void)return get_next_mem_node (numa_max_node ()); } +static int count_mem_nodes (void ) +{ +int node, count =0 ; + +for (node =0 ; node <=numa_max_node (); node++) { +if (numa_node_size (node,NULL ) >0 ) + count++; + } + +return count; +} +static int check_ksm_numa_merge (int merge_type,int mapping,int prot,int timeout,bool merge_across_nodes,size_t page_size) {
@@ -463,14 +475,12 @@ static int check_ksm_numa_merge(int merge_type, int mapping, int prot, int timeoreturn KSFT_FAIL ; } -if (numa_available () <0 ) { -ksft_print_msg ("NUMA support not enabled\n" ); -return KSFT_SKIP ; - } -if (numa_num_configured_nodes () <=1 ) { -ksft_print_msg ("At least 2 NUMA nodes must be available\n" ); -return KSFT_SKIP ; - } +if (numa_available () <0 ) +ksft_exit_skip ("NUMA support not enabled\n" ); + +if (count_mem_nodes () <=1 ) +ksft_exit_skip ("At least 2 NUMA nodes with memory must be available\n" ); +if (ksm_write_sysfs (KSM_FP ("merge_across_nodes" ), merge_across_nodes))return KSFT_FAIL ;
Signature · 1 line
2.52.0
Re: [PATCH 1/2] selftests/mm: handle EINVAL when configuring gigantic hugepages
In reply to Sayali PatilQuoted 53 lines · On Thu, 25 Jun 2026 18:10:16 +0530 Sayali Patil <[email protected]> wrote:
Some MM selftests attempt to configure the amount of HugeTLB pages of different sizes by writing to
nr_hugepages.PowerPC hash MMU pSeries systems advertise gigantic hugepage sizes but do not support runtime allocation of such pages, writes to the corresponding
nr_hugepagesfile fail with-EINVAL. This causes the test to bail out even though the failure is due to a platform limitation rather than the
functionality being tested.Treat
-EINVALfrom the sysfs write as a skipped configuration request and continue running the test instead of failing.Before patch:
-------------------------
running ./hugetlb-madvise
-------------------------
TAP version 13
1..1
[INFO] detected hugetlb page size: 16777216 KiB
[INFO] detected hugetlb page size: 16384 KiB
ok 1MADV_DONTNEEDandMADV_REMOVEon hugetlb
Totals: pass:1 fail:0 xfail:0 xpass:0 skip:0 error:0 Bail out!/sys/kernel/mm/hugepages/hugepages-16777216kB/nr_hugepageswrite(0) failed: Invalid argument
Totals: pass:0 fail:0 xfail:0 xpass:0 skip:0 error:0 [FAIL]After patch:
-------------------------
running ./hugetlb-madvise
-------------------------
TAP version 13
1..1
[INFO] detected hugetlb page size: 16777216 KiB [INFO] detected hugetlb page size: 16384 KiB
ok 1MADV_DONTNEEDandMADV_REMOVEon hugetlb
Totals: pass:1 fail:0 xfail:0 xpass:0 skip:0 error:0/sys/kernel/mm/hugepages/hugepages-16777216kB/nr_hugepageswrite(0) failed: Invalid argument
[PASS]Fixes 9d07250ea1eb ("selftests/mm: hugepage_settings: add APIs to get and set nr_hugepages")Signed-off-by Sayali Patil <[email protected]>--- tools/testing/selftests/mm/vm_util.c | 7 +++++++ 1 file changed, 7 insertions(+)tools/testing/selftests/mm/vm_util.c +0
-0
tools/testing/selftests/mm/vm_util.c void write_file(const char *path, const char *buf, size_t buflen) · line 744
Quoted 10 lines
tools/testing/selftests/mm/vm_util.c +7
-0@@ -735,6 +735,13 @@ void write_file(const char *path, const char *buf, size_t buflen) saved_errno = errno;close (fd); errno = saved_errno; + +if (numwritten <0 && errno ==EINVAL ) { +ksft_print_msg ("%s write(%.*s) failed: %s\n" , path, + (int )(buflen -1 ), buf,strerror (errno)); +return ; + } +
This makes write_file() silently succeed for every EINVAL, not just the gigantic-hugetlb setup case. Several callers use this helper for writes where EINVAL is a real test failure, for example drop_caches or split huge page setup. Those tests can now continue after a failed setup and report misleading results.
Please keep the common helper strict and ignore EINVAL only in the hugetlb path that is probing unsupported gigantic page runtime allocation.
Quoted 7 lines
tools/testing/selftests/mm/vm_util.c +0
-0if (numwritten <0 )ksft_exit_fail_msg ("%s write(%.*s) failed: %s\n" , path, (int )(buflen -1 ), buf,strerror (errno));Signature · 1 line
2.52.0
David Hildenbrand (Arm)
maintainerRe: [PATCH 2/2] selftests/mm: fix ksm NUMA merge test for systems with memoryless NUMA nodes
In reply to Sayali PatilReview
Quoted 13 lines
(quoted diff) +12
-0+static int count_mem_nodes(void) +{ + int node, count = 0; + + for (node = 0; node <= numa_max_node(); node++) { + if (numa_node_size(node, NULL) > 0) + count++; + } + + return count; +} +
Can we instead build upon our existing helpers get_first_mem_node() + get_next_mem_node() ?
From 432774fb50237519c1c041e402fddfdf4b35aa2c Mon Sep 17 00:00:00 2001
From: "David Hildenbrand (Arm)" <[email protected]>
Date: Fri, 26 Jun 2026 17:52:21 +0200
Subject: [PATCH] tmp
--- tools/testing/selftests/mm/ksm_tests.c | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-)
tools/testing/selftests/mm/ksm_tests.c +9 -7
@@ -440,9 +440,9 @@ static int get_next_mem_node(int node) mem_node = i % (max_node +1 ); node_size =numa_node_size (mem_node,NULL );if (node_size >0 ) -break ; +return mem_node; } -return mem_node; +return -ENODEV ; }static int get_first_mem_node (void )
@@ -455,8 +455,8 @@ static int check_ksm_numa_merge(int merge_type, int mapping, int prot, int timeo {void *numa1_map_ptr, *numa2_map_ptr;struct timespec start_time; +int first_node, second_node;int page_count =2 ; -int first_node;if (clock_gettime (CLOCK_MONOTONIC_RAW , &start_time)) {ksft_perror ("clock_gettime" );
@@ -467,17 +467,19 @@ static int check_ksm_numa_merge(int merge_type, int mapping, int prot, int timeoksft_print_msg ("NUMA support not enabled\n" );return KSFT_SKIP ; } -if (numa_num_configured_nodes () <=1 ) { -ksft_print_msg ("At least 2 NUMA nodes must be available\n" ); + first_node =get_first_mem_node (); + second_node =get_next_mem_node (first_node); + +if (second_node <0 ) { +ksft_print_msg ("At least 2 NUMA nodes with memory must be available\n" );return KSFT_SKIP ; }if (ksm_write_sysfs (KSM_FP ("merge_across_nodes" ), merge_across_nodes))return KSFT_FAIL ;/* allocate 2 pages in 2 different NUMA nodes and fill them with the same data */ - first_node =get_first_mem_node (); numa1_map_ptr =numa_alloc_onnode (page_size, first_node); - numa2_map_ptr =numa_alloc_onnode (page_size,get_next_mem_node (first_node)); + numa2_map_ptr =numa_alloc_onnode (page_size, second_node);if (!numa1_map_ptr || !numa2_map_ptr) {ksft_perror ("numa_alloc_onnode" );return KSFT_FAIL ;
Signature · 7 lines
2.43.0 -- Cheers, David
Re: [PATCH 2/2] selftests/mm: fix ksm NUMA merge test for systems with memoryless NUMA nodes
Quoted 67 lines · David Hildenbrand (Arm)
Review
Quoted 13 lines
(quoted diff) +12
-0+static int count_mem_nodes(void) +{ + int node, count = 0; + + for (node = 0; node <= numa_max_node(); node++) { + if (numa_node_size(node, NULL) > 0) + count++; + } + + return count; +} +Can we instead build upon our existing helpers
get_first_mem_node()+get_next_mem_node()?From
432774fb50237519c1c041e402fddfdf4b35aa2cMon Sep 17 00:00:00 2001
From: "David Hildenbrand (Arm)" <[email protected]>
Date: Fri, 26 Jun 2026 17:52:21 +0200
Subject: [PATCH] tmpSigned-off-by David Hildenbrand (Arm) <[email protected]>--- tools/testing/selftests/mm/ksm_tests.c | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-)tools/testing/selftests/mm/ksm_tests.c +8
-5@@ -440,9 +440,9 @@ static int get_next_mem_node(int node) mem_node = i % (max_node +1 ); node_size =numa_node_size (mem_node,NULL );if (node_size >0 ) -break ; +return mem_node; } -return mem_node; +return -ENODEV ; }static int get_first_mem_node (void )@@ -455,8 +455,8 @@ static int check_ksm_numa_merge(int merge_type, int mapping, int prot, int timeo {void *numa1_map_ptr, *numa2_map_ptr;struct timespec start_time; +int first_node, second_node;int page_count =2 ; -int first_node;if (clock_gettime (CLOCK_MONOTONIC_RAW , &start_time)) {ksft_perror ("clock_gettime" );@@ -467,17 +467,19 @@ static int check_ksm_numa_merge(int merge_type, int mapping, int prot, int timeoksft_print_msg ("NUMA support not enabled\n" );return KSFT_SKIP ; } -if (numa_num_configured_nodes () <=1 ) { -ksft_print_msg ("At least 2 NUMA nodes must be available\n" ); + first_node =get_first_mem_node (); + second_node =get_next_mem_node (first_node); + +if (second_node <0 ) { +ksft_print_msg ("At least 2 NUMA nodes with memory must be available\n" );return KSFT_SKIP ; }if (ksm_write_sysfs (KSM_FP ("merge_across_nodes" ), merge_across_nodes))
tools/testing/selftests/mm/ksm_tests.c static int check_ksm_numa_merge(int merge_type, int mapping, int prot, int timeo · line 485
Quoted 10 lines
tools/testing/selftests/mm/ksm_tests.c +1
-2return KSFT_FAIL ;/* allocate 2 pages in 2 different NUMA nodes and fill them with the same data */ - first_node =get_first_mem_node (); numa1_map_ptr =numa_alloc_onnode (page_size, first_node); - numa2_map_ptr =numa_alloc_onnode (page_size,get_next_mem_node (first_node)); + numa2_map_ptr =numa_alloc_onnode (page_size, second_node);if (!numa1_map_ptr || !numa2_map_ptr) {ksft_perror ("numa_alloc_onnode" );return KSFT_FAIL ;
Hi David,
Thanks for review!
Yes, I updated the implementation in v2 to use the existing get_first_mem_node() + get_next_mem_node() helper as suggested.
Re: [PATCH 1/2] selftests/mm: handle EINVAL when configuring gigantic hugepages
In reply to Usama ArifQuoted 71 lines · Usama Arif
Quoted 53 lines · On Thu, 25 Jun 2026 18:10:16 +0530 Sayali Patil <[email protected]> wrote:
Some MM selftests attempt to configure the amount of HugeTLB pages of different sizes by writing to
nr_hugepages.PowerPC hash MMU pSeries systems advertise gigantic hugepage sizes but do not support runtime allocation of such pages, writes to the corresponding
nr_hugepagesfile fail with-EINVAL. This causes the test to bail out even though the failure is due to a platform limitation rather than the
functionality being tested.Treat
-EINVALfrom the sysfs write as a skipped configuration request and continue running the test instead of failing.Before patch:
-------------------------
running ./hugetlb-madvise
-------------------------
TAP version 13
1..1
[INFO] detected hugetlb page size: 16777216 KiB
[INFO] detected hugetlb page size: 16384 KiB
ok 1MADV_DONTNEEDandMADV_REMOVEon hugetlb
Totals: pass:1 fail:0 xfail:0 xpass:0 skip:0 error:0 Bail out!/sys/kernel/mm/hugepages/hugepages-16777216kB/nr_hugepageswrite(0) failed: Invalid argument
Totals: pass:0 fail:0 xfail:0 xpass:0 skip:0 error:0 [FAIL]After patch:
-------------------------
running ./hugetlb-madvise
-------------------------
TAP version 13
1..1
[INFO] detected hugetlb page size: 16777216 KiB [INFO] detected hugetlb page size: 16384 KiB
ok 1MADV_DONTNEEDandMADV_REMOVEon hugetlb
Totals: pass:1 fail:0 xfail:0 xpass:0 skip:0 error:0/sys/kernel/mm/hugepages/hugepages-16777216kB/nr_hugepageswrite(0) failed: Invalid argument
[PASS]Fixes 9d07250ea1eb ("selftests/mm: hugepage_settings: add APIs to get and set nr_hugepages")Signed-off-by Sayali Patil <[email protected]>--- tools/testing/selftests/mm/vm_util.c | 7 +++++++ 1 file changed, 7 insertions(+)tools/testing/selftests/mm/vm_util.c +0
-0tools/testing/selftests/mm/vm_util.c void write_file(const char *path, const char *buf, size_t buflen) · line 744
Quoted 10 lines
tools/testing/selftests/mm/vm_util.c +7
-0@@ -735,6 +735,13 @@ void write_file(const char *path, const char *buf, size_t buflen) saved_errno = errno;close (fd); errno = saved_errno; + +if (numwritten <0 && errno ==EINVAL ) { +ksft_print_msg ("%s write(%.*s) failed: %s\n" , path, + (int )(buflen -1 ), buf,strerror (errno)); +return ; + } +This makes
write_file()silently succeed for every EINVAL, not just the gigantic-hugetlb setup case. Several callers use this helper for writes where EINVAL is a real test failure, for exampledrop_cachesor split huge page setup. Those tests can now continue after a failed setup and report misleading results.
Quoted 2 lines
Please keep the common helper strict and ignore EINVAL only in the hugetlb path that is probing unsupported gigantic page runtime allocation.
Thanks for review!
In v2, I kept the generic write_file() helper unchanged and added a dedicated hugetlb_write_num() helper to handle the expected -EINVAL returned when configuring gigantic hugepages. This limits the special handling to the hugepage setup path and avoids masking genuine EINVAL failures from other write_file() callers.
Quoted 10 lines
Quoted 9 lines
tools/testing/selftests/mm/vm_util.c +0
-0if (numwritten <0 )ksft_exit_fail_msg ("%s write(%.*s) failed: %s\n" , path, (int )(buflen -1 ), buf,strerror (errno));Signature · 1 line
2.52.0