Skip to contentSkip to conversation

selftests/mm: ksm_tests: skip when not run as root

PATCH In review Sun Jian 3 participants 5 messages Jan 9 – Jan 11

Subsystems (3)
lore
Share & tools
lore

Sun Jian

author
#0 lore raw

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
index a0b48b839d54..c22cd9c61711 100644
@@ -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
#1 lore raw
Quoted 16 lines · On Fri, 9 Jan 2026 17:43:13 +0800 Sun Jian <[email protected]> wrote:

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 +0 -0
index a0b48b839d54..c22cd9c61711 100644

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

[...]

author
#2 lore raw
Quoted 1 line · SeongJae Park

What about using ksft_exit_skip() instead, like compaction_test.c does?

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

#3 lore raw
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, like compaction_test.c does?

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.

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

[...]

maintainer
#4 lore raw
In reply to Sun Jian
Quoted 5 lines · On Fri, 9 Jan 2026 17:43:13 +0800 Sun Jian <[email protected]> wrote:

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.

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!