From 4d16530b83ce9921904d560675a9571fc351fe96 Mon Sep 17 00:00:00 2001 From: Usama Arif Date: Sun, 21 Sep 2025 20:59:38 +0100 Subject: [PATCH] src/test: add unittest for MemoryTHP= This checks if the prctl is set correctly when the property name is passed to systemd-run. --- src/test/meson.build | 5 +++ src/test/test-thp.c | 56 ++++++++++++++++++++++++++ test/units/TEST-07-PID1.thp-disable.sh | 25 ++++++++++++ 3 files changed, 86 insertions(+) create mode 100644 src/test/test-thp.c create mode 100755 test/units/TEST-07-PID1.thp-disable.sh diff --git a/src/test/meson.build b/src/test/meson.build index 6a26eb3f87c..44b6d5e4bd3 100644 --- a/src/test/meson.build +++ b/src/test/meson.build @@ -528,6 +528,11 @@ executables += [ 'conditions' : ['BPF_FRAMEWORK'], 'type' : 'manual', }, + core_test_template + { + 'sources' : files('test-thp.c'), + 'dependencies' : common_test_dependencies, + 'type' : 'manual', + }, core_test_template + { 'sources' : files('test-cgroup-cpu.c'), }, diff --git a/src/test/test-thp.c b/src/test/test-thp.c new file mode 100644 index 00000000000..047bd26e894 --- /dev/null +++ b/src/test/test-thp.c @@ -0,0 +1,56 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ + +#include + +#include "tests.h" + +#define PR_THP_DISABLE_NOT_SET 0 +#define PR_THP_DISABLE 1 + +static const char *arg_mode = NULL; + +static int intro(void) { + int r; + + r = prctl(PR_GET_THP_DISABLE, 0, 0, 0, 0); + if (streq_ptr(arg_mode, "no-disable")) { + /* Test case: THPs should not be disabled */ + if (r != PR_THP_DISABLE_NOT_SET) { + log_error("THPs disabled for the process r = %d: %m", r); + return EXIT_FAILURE; + } + } else if (streq_ptr(arg_mode, "disable")) { + /* Test case: THPs should be completely disabled */ + if (r == PR_THP_DISABLE_NOT_SET) + return log_tests_skipped("Disabling THPs completely for the process not supported"); + + if (r != PR_THP_DISABLE) { + log_error("THPs not completely disabled for the process r = %d: %m", r); + return EXIT_FAILURE; + } + } else if (streq_ptr(arg_mode, "madvise")) { + /* Test case: THPs should be only enabled on a madvise basis */ + if (r == PR_THP_DISABLE_NOT_SET) + return log_tests_skipped("Disabling THPs except for madvise not supported"); + + if (r != (PR_THP_DISABLE | PR_THP_DISABLE_EXCEPT_ADVISED)) { + log_error("THPs (except madvise) not completely disabled for the process r = %d: %m", r); + return EXIT_FAILURE; + } + } else { + log_error("Invalid mode: %s (expected: no-disable, disable, or madvise)", strna(arg_mode)); + return EXIT_FAILURE; + } + + return EXIT_SUCCESS; +} + +int main(int argc, char *argv[]) { + if (argc != 2) { + log_error("Invalid number of args passed to the test %d", argc); + return EXIT_FAILURE; + } + arg_mode = argv[1]; + test_setup_logging(LOG_DEBUG); + return intro(); +} diff --git a/test/units/TEST-07-PID1.thp-disable.sh b/test/units/TEST-07-PID1.thp-disable.sh new file mode 100755 index 00000000000..06626ad0918 --- /dev/null +++ b/test/units/TEST-07-PID1.thp-disable.sh @@ -0,0 +1,25 @@ +#!/usr/bin/env bash +# SPDX-License-Identifier: LGPL-2.1-or-later +set -eux +set -o pipefail + +systemd-run --wait \ + /usr/lib/systemd/tests/unit-tests/manual/test-thp no-disable + +systemd-run --wait \ + -p MemoryTHP=disable \ + /usr/lib/systemd/tests/unit-tests/manual/test-thp disable + +# The following test will always return 77 if at compile time the kernel version +# is less than 6.18. If it happens don't let the whole test fail +set +e + +systemd-run --wait \ + -p MemoryTHP=madvise \ + /usr/lib/systemd/tests/unit-tests/manual/test-thp madvise + +if [ $? -eq 77 ]; then + exit 0 +fi + +set -e -- 2.47.3