[PATCH 2/2] selftests/mm: fix ksm NUMA merge test for systems with memoryless NUMA nodes
From: Sayali Patil <hidden>
Date: 2026-06-25 12:41:33
Also in:
linux-kselftest, linux-mm, lkml
Subsystem:
kernel selftest framework, memory management - misc, the rest · Maintainers:
Shuah Khan, Andrew Morton, David Hildenbrand, Linus Torvalds
The 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 MB
the 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
Fixes: e3820ab252dd ("selftest/vm: fix ksm selftest to run with different NUMA topologies")
Signed-off-by: Sayali Patil <redacted>
---
tools/testing/selftests/mm/ksm_tests.c | 26 ++++++++++++++++++--------
1 file changed, 18 insertions(+), 8 deletions(-)
diff --git a/tools/testing/selftests/mm/ksm_tests.c b/tools/testing/selftests/mm/ksm_tests.c
index a050f4840cfa..d1790b4347d3 100644
--- a/tools/testing/selftests/mm/ksm_tests.c
+++ b/tools/testing/selftests/mm/ksm_tests.c@@ -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 timeo return 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;
--
2.52.0