[PATCH v3 2/2] selftests/cgroup: Test parsing of long fractions in cpu.uclamp.min

From: Chris Down

Date: Thu Nov 26 2026 - 00:41:25 EST


Add a test to test_cpu that writes values to cpu.uclamp.min and reads
them back, checking that they are rounded to two decimal places and
that malformed or out of range values are rejected.

Signed-off-by: Chris Down <chris@xxxxxxxxxxxxxx>
---
tools/testing/selftests/cgroup/config | 4 ++
tools/testing/selftests/cgroup/test_cpu.c | 71 +++++++++++++++++++++++
2 files changed, 75 insertions(+)

diff --git a/tools/testing/selftests/cgroup/config b/tools/testing/selftests/cgroup/config
index 39f979690dd3..c8b2c95a186e 100644
--- a/tools/testing/selftests/cgroup/config
+++ b/tools/testing/selftests/cgroup/config
@@ -2,5 +2,9 @@ CONFIG_CGROUPS=y
CONFIG_CGROUP_CPUACCT=y
CONFIG_CGROUP_FREEZER=y
CONFIG_CGROUP_SCHED=y
+CONFIG_CPU_FREQ=y
+CONFIG_CPU_FREQ_GOV_SCHEDUTIL=y
CONFIG_MEMCG=y
CONFIG_PAGE_COUNTER=y
+CONFIG_UCLAMP_TASK=y
+CONFIG_UCLAMP_TASK_GROUP=y
diff --git a/tools/testing/selftests/cgroup/test_cpu.c b/tools/testing/selftests/cgroup/test_cpu.c
index 3456e3e988c6..369d4930c1f4 100644
--- a/tools/testing/selftests/cgroup/test_cpu.c
+++ b/tools/testing/selftests/cgroup/test_cpu.c
@@ -809,6 +809,76 @@ cleanup:
return ret;
}

+static int test_cpucg_uclamp_parse(const char *root)
+{
+ static const struct {
+ char *input;
+ char *expected; /* NULL if the write must fail */
+ } cases[] = {
+ { "12.34", "12.34\n" },
+ { "50", "50.00\n" },
+ { "0.125", "0.13\n" },
+ { "0.999", "1.00\n" },
+ { "100.01", NULL },
+ { "abc", NULL },
+ { "0.5000000000000", "0.50\n" },
+ { "0.123456789012", "0.12\n" },
+ { "0.99999999999999999999", "1.00\n" },
+ { "0.0000000000000000000000000000000000", "0.00\n" },
+ };
+ int ret = KSFT_FAIL;
+ char buf[64];
+ char *cpucg;
+ int i, err;
+
+ cpucg = cg_name(root, "cpucg_test");
+ if (!cpucg)
+ goto cleanup;
+
+ if (cg_create(cpucg))
+ goto cleanup;
+
+ if (cg_read(cpucg, "cpu.uclamp.min", buf, sizeof(buf))) {
+ ret = KSFT_SKIP;
+ goto cleanup;
+ }
+
+ for (i = 0; i < ARRAY_SIZE(cases); i++) {
+ err = cg_write(cpucg, "cpu.uclamp.min", cases[i].input);
+
+ if (!cases[i].expected) {
+ if (!err) {
+ ksft_print_msg("%s was accepted\n", cases[i].input);
+ goto cleanup;
+ }
+ continue;
+ }
+
+ if (err) {
+ ksft_print_msg("%s was rejected: %s\n", cases[i].input,
+ strerror(-err));
+ goto cleanup;
+ }
+
+ if (cg_read(cpucg, "cpu.uclamp.min", buf, sizeof(buf)))
+ goto cleanup;
+
+ if (strcmp(buf, cases[i].expected)) {
+ ksft_print_msg("%s reads back as %s", cases[i].input,
+ buf);
+ goto cleanup;
+ }
+ }
+
+ ret = KSFT_PASS;
+
+cleanup:
+ cg_destroy(cpucg);
+ free(cpucg);
+
+ return ret;
+}
+
#define T(x) { x, #x }
struct cpucg_test {
int (*fn)(const char *root);
@@ -823,6 +893,7 @@ struct cpucg_test {
T(test_cpucg_nested_weight_underprovisioned),
T(test_cpucg_max),
T(test_cpucg_max_nested),
+ T(test_cpucg_uclamp_parse),
};
#undef T

--
2.56.1