[PATCH] selftests/mm: ksm_tests: skip when not run as root

Sun Jian posted 1 patch 4 weeks, 1 day ago
tools/testing/selftests/mm/ksm_tests.c | 5 +++++
1 file changed, 5 insertions(+)
[PATCH] selftests/mm: ksm_tests: skip when not run as root
Posted by Sun Jian 4 weeks, 1 day ago
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.

Signed-off-by: Sun Jian <sun.jian.kdev@gmail.com>
---
 tools/testing/selftests/mm/ksm_tests.c | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/tools/testing/selftests/mm/ksm_tests.c b/tools/testing/selftests/mm/ksm_tests.c
index a0b48b839d54..c22cd9c61711 100644
--- a/tools/testing/selftests/mm/ksm_tests.c
+++ b/tools/testing/selftests/mm/ksm_tests.c
@@ -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':
-- 
2.43.0
Re: [PATCH] selftests/mm: ksm_tests: skip when not run as root
Posted by Andrew Morton 3 weeks, 6 days ago
On Fri,  9 Jan 2026 17:43:13 +0800 Sun Jian <sun.jian.kdev@gmail.com> 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.

> --- a/tools/testing/selftests/mm/ksm_tests.c
> +++ b/tools/testing/selftests/mm/ksm_tests.c
> @@ -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!
Re: [PATCH] selftests/mm: ksm_tests: skip when not run as root
Posted by SeongJae Park 4 weeks ago
On Fri,  9 Jan 2026 17:43:13 +0800 Sun Jian <sun.jian.kdev@gmail.com> 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.
> 
> Signed-off-by: Sun Jian <sun.jian.kdev@gmail.com>
> ---
>  tools/testing/selftests/mm/ksm_tests.c | 5 +++++
>  1 file changed, 5 insertions(+)
> 
> diff --git a/tools/testing/selftests/mm/ksm_tests.c b/tools/testing/selftests/mm/ksm_tests.c
> index a0b48b839d54..c22cd9c61711 100644
> --- a/tools/testing/selftests/mm/ksm_tests.c
> +++ b/tools/testing/selftests/mm/ksm_tests.c
> @@ -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

[...]
Re: [PATCH] selftests/mm: ksm_tests: skip when not run as root
Posted by sun jian 4 weeks ago
On Sat, Jan 10, 2026 at 9:16 AM SeongJae Park <sj@kernel.org> wrote:
> 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
Re: [PATCH] selftests/mm: ksm_tests: skip when not run as root
Posted by SeongJae Park 3 weeks, 6 days ago
On Sat, 10 Jan 2026 20:44:21 +0800 sun jian <sun.jian.kdev@gmail.com> wrote:

> On Sat, Jan 10, 2026 at 9:16 AM SeongJae Park <sj@kernel.org> wrote:
> > 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

[...]