* [PATCH v2] cgroup: Preserve sign for fractional input
[not found] <20261126065958.89207-1-zhiqizhangg@gmail.com>
@ 2026-11-29 15:01 ` Zhiqi Zhang
2026-11-29 16:58 ` Tejun Heo
0 siblings, 1 reply; 2+ messages in thread
From: Zhiqi Zhang @ 2026-11-29 15:01 UTC (permalink / raw)
To: Tejun Heo
Cc: Zhiqi Zhang, Johannes Weiner, Michal Koutny, Shuah Khan, cgroups,
linux-kernel, linux-kselftest
cgroup_parse_float() parses the whole part with sscanf() and accumulates
and rounds the fractional digits separately. The sign is only carried by
the whole part, so "-0.5" is composed as 0.5 and "-1.5" as -0.5. If the
fraction rounds up to a whole unit, "-1.999" at two decimal places is
composed as zero.
Callers such as cpu.uclamp.* and io.cost.qos reject negative parsed
values. Losing the sign lets negative fractional input bypass those
checks and be accepted as a nonnegative value.
Use the leading sign of the input, after skipping whitespace, to negate
the rounded fractional part before adding it to the scaled whole part.
This also handles negative input whose whole part is zero, while keeping
the bounded fractional parsing and rounding unchanged.
Extend the cpu.uclamp.min parsing selftest to cover negative fractions,
rounding across a whole unit, rounding to zero and long negative
fractions.
Fixes: a5e112e6424a ("cgroup: add cgroup_parse_float()")
Signed-off-by: Zhiqi Zhang <zhiqizhangg@gmail.com>
---
Changes since v1:
- Rebase onto cgroup/for-7.4-fixes at 05afa0c55015, which includes
4ac4766e93f3 ("cgroup: Fix divide error on long fractions in
cgroup_parse_float()"), as requested by Tejun.
- Negate the rounded fractional part before composing the result, keeping
the new bounded digit-by-digit parsing unchanged.
- Extend the existing cpu.uclamp.min parsing selftest with six cases for
negative fractions, rounding and long fractional input.
Validation:
- arm64 defconfig and W=1 kernel/cgroup/cgroup.o with GCC 16.2.0.
- Built tools/testing/selftests/cgroup/test_cpu.
- Ran 195,068 userspace cases against extracted kernel parser code,
including the kernel sscanf implementation, with ASan and UBSan.
All matched an independent integer oracle; 80,320 cases reproduced
incorrect negative results on the unpatched base.
- scripts/checkpatch.pl --strict: clean.
I have not booted the patched kernel to run the cgroup interface selftest.
AI assistance: I used Codex to adapt the fix to the new parser, prepare
regression tests, and draft this revision's description.
kernel/cgroup/cgroup.c | 4 ++++
tools/testing/selftests/cgroup/test_cpu.c | 6 ++++++
2 files changed, 10 insertions(+)
diff --git a/kernel/cgroup/cgroup.c b/kernel/cgroup/cgroup.c
index f6df192417..f0f329eca5 100644
--- a/kernel/cgroup/cgroup.c
+++ b/kernel/cgroup/cgroup.c
@@ -7565,6 +7565,10 @@ int cgroup_parse_float(const char *input, unsigned dec_shift, s64 *v)
frac++;
}
+ /* The whole part does not retain the sign of "-0". */
+ if (*skip_spaces(input) == '-')
+ frac = -frac;
+
*v = whole * power_of_ten(dec_shift) + frac;
return 0;
}
diff --git a/tools/testing/selftests/cgroup/test_cpu.c b/tools/testing/selftests/cgroup/test_cpu.c
index 369d4930c1..f1f91ab588 100644
--- a/tools/testing/selftests/cgroup/test_cpu.c
+++ b/tools/testing/selftests/cgroup/test_cpu.c
@@ -819,6 +819,12 @@ static int test_cpucg_uclamp_parse(const char *root)
{ "50", "50.00\n" },
{ "0.125", "0.13\n" },
{ "0.999", "1.00\n" },
+ { "-0.5", NULL },
+ { "-0.005", NULL },
+ { "-0.999", NULL },
+ { "-1.999", NULL },
+ { "-0.004", "0.00\n" },
+ { "-0.5000000000000000000000000000000000", NULL },
{ "100.01", NULL },
{ "abc", NULL },
{ "0.5000000000000", "0.50\n" },
base-commit: 05afa0c55015b3c7f3c2136dde7fbcfe5195b21f
--
2.54.0 (Apple Git-157)
^ permalink raw reply related [flat|nested] 2+ messages in thread