selftests/mm: ksm_tests: skip when not run as root
Subsystems (3)
PATCH 1/1 selftests/mm: ksm_tests: skip when not run as root
ksm_tests writes KSM sysfs knobs under /sys/kernel/mm/ksm, which requires root privileges. When run unprivileged, it fails with permission errors and reports FAIL, which is misleading.
Skip the test early when not run as root to avoid false failures.
--- tools/testing/selftests/mm/ksm_tests.c | 5 +++++ 1 file changed, 5 insertions(+)
tools/testing/selftests/mm/ksm_tests.c +5 -0
@@ -766,6 +766,11 @@ int main(int argc, char *argv[])bool merge_across_nodes =KSM_MERGE_ACROSS_NODES_DEFAULT ;long size_MB =0 ; +if (geteuid () !=0 ) { +printf ("# SKIP ksm_tests requires root privileges\n" ); +return KSFT_SKIP ; + } +while ((opt =getopt (argc, argv,"dha:p:l:z:m:s:t:MUZNPCHD" )) !=-1 ) {switch (opt) {case 'a' :
Signature · 1 line
2.43.0
Quoted 16 lines · On Fri, 9 Jan 2026 17:43:13 +0800 Sun Jian <[email protected]> wrote:
ksm_testswrites KSM sysfs knobs under/sys/kernel/mm/ksm, which requires root privileges. When run unprivileged, it fails with permission errors and reports FAIL, which is misleading.Skip the test early when not run as root to avoid false failures.
Signed-off-by Sun Jian <[email protected]>--- tools/testing/selftests/mm/ksm_tests.c | 5 +++++ 1 file changed, 5 insertions(+)tools/testing/selftests/mm/ksm_tests.c +0
-0
tools/testing/selftests/mm/ksm_tests.c int main(int argc, char *argv[]) · line 771
Quoted 6 lines
tools/testing/selftests/mm/ksm_tests.c +3
-0@@ -766,6 +766,11 @@ int main(int argc, char *argv[])bool merge_across_nodes =KSM_MERGE_ACROSS_NODES_DEFAULT ;long size_MB =0 ; +if (geteuid () !=0 ) { +printf ("# SKIP ksm_tests requires root privileges\n" ); +return KSFT_SKIP ;
What about using ksft_exit_skip() instead, like compaction_test.c does?
Thanks,
SJ
[...]
Quoted 1 line · SeongJae Park
What about using
ksft_exit_skip()instead, likecompaction_test.cdoes?
ksm_tests is a legacy selftest binary and doesn't use kselftest harness/TAP APIs. It only includes kselftest.h for the KSFT_* exit codes.
This patch is a minimal prerequisite fix. Returning KSFT_SKIP already provides the correct semantic to the runner, without pulling ksm_tests into the harness model, which would be a larger refactor.
Regards,
Sun Jian
Quoted 8 lines · On Sat, 10 Jan 2026 20:44:21 +0800 sun jian <[email protected]> wrote:
Quoted 1 line · SeongJae Park
What about using
ksft_exit_skip()instead, likecompaction_test.cdoes?
ksm_testsis a legacy selftest binary and doesn't use kselftest harness/TAP APIs. It only includeskselftest.hfor the KSFT_* exit codes.This patch is a minimal prerequisite fix. Returning
KSFT_SKIPalready provides the correct semantic to the runner, without pullingksm_testsinto the harness model, which would be a larger refactor.
I agree such refactoring would be unnecessary for this small change. But you can use ksft_exit_skip() without such refactoring, isn't it? The function is declared in kselftest.h, not kselftest_harness.h. ksm_functional_tests.c is also using ksft_exit_skip() without the harness model.
Thanks,
SJ
[...]
Andrew Morton
maintainerQuoted 5 lines · On Fri, 9 Jan 2026 17:43:13 +0800 Sun Jian <[email protected]> wrote:
ksm_testswrites KSM sysfs knobs under/sys/kernel/mm/ksm, which requires root privileges. When run unprivileged, it fails with permission errors and reports FAIL, which is misleading.Skip the test early when not run as root to avoid false failures.
Thanks.
When reissuing a patch it's nice to cc those individuals who commented on previous versions.
tools/testing/selftests/mm/ksm_tests.c int main(int argc, char *argv[]) · line 773
Quoted 11 lines
tools/testing/selftests/mm/ksm_tests.c +5
-0@@ -766,6 +766,11 @@ int main(int argc, char *argv[])bool merge_across_nodes =KSM_MERGE_ACROSS_NODES_DEFAULT ;long size_MB =0 ; +if (geteuid () !=0 ) { +printf ("# SKIP ksm_tests requires root privileges\n" ); +return KSFT_SKIP ; + } +
lgtm. selftests/ code performs this test in about 20 places already, all different. In the interests of consistency and code cleanliness, perhaps some kind person will centralize this check in some fashion!