[PATCH v4] cgroup: Preserve sign for fractional input

From: Zhiqi Zhang

Date: Wed Dec 02 2026 - 02:18:03 EST


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.

Read the leading sign once, after skipping whitespace, and parse the
whole part with unsigned sscanf(). Compose the scaled unsigned magnitude
with overflow checks, reject results above S64_MAX, and then apply the
sign once at the end. This also rejects a large whole part that used to
wrap during composition and be accepted as a small positive value.

Extend the cpu.uclamp.min parsing selftest to cover negative fractions,
rounding across a whole unit, rounding to zero, long negative fractions,
whitespace after the sign, and whole-part overflow.

Fixes: a5e112e6424a ("cgroup: add cgroup_parse_float()")
Signed-off-by: Zhiqi Zhang <zhiqizhangg@xxxxxxxxx>
---
Changes since v3:
- Keep sscanf() for the whole part, using %llu after reading the sign,
as requested by Tejun. Whitespace after the minus sign is accepted.
- Check the composed magnitude against S64_MAX for both signs and remove
the S64_MIN special case, as requested by Tejun.
- Add cpu.uclamp.min cases for whitespace after the minus sign.

Changes since v2:
- Parse the sign separately and apply it after composing an unsigned
magnitude, with overflow checks for scaling and addition.
- Add a cpu.uclamp.min case for the whole-part overflow reported by Tejun.

Changes since v1:
- Rebase onto cgroup/for-7.4-fixes at 05afa0c55015, including
4ac4766e93f3 ("cgroup: Fix divide error on long fractions in
cgroup_parse_float()").
- Extend the existing cpu.uclamp.min parsing selftest with negative
fractions, rounding and long fractional input.

Validation:
- arm64 defconfig and W=1 kernel/cgroup/cgroup.o with GCC 13.3.0.
- Built tools/testing/selftests/cgroup/test_cpu.
- Ran 221,127 userspace cases against extracted kernel parser code,
including the kernel sscanf implementation, with ASan and UBSan.
All matched an independent integer oracle. Compared with v3, the only
differences were accepting whitespace after the minus sign and
rejecting magnitudes that would require the S64_MIN special case.
- scripts/checkpatch.pl --strict: clean.

Runtime validation:
- Built and booted both the base and patched arm64 kernels under QEMU.
- Ran the unchanged test_cpucg_uclamp_parse() from this revision. Its 19
cases passed on the patched kernel; the base failed on "-0.5".
- Ran the same 198 additional write/readback checks for cpu.uclamp.min,
cpu.uclamp.max and io.cost.qos rpct/wpct/min/max in both guests.
The base failed 68 checks; the patched kernel passed all 198, including
exact errno and unchanged state after rejected writes.

AI assistance: I used Codex to adapt the fix to Tejun's review, prepare
regression tests, and draft this revision's description.

kernel/cgroup/cgroup.c | 25 ++++++++++++++++++++---
tools/testing/selftests/cgroup/test_cpu.c | 9 ++++++++
2 files changed, 31 insertions(+), 3 deletions(-)

diff --git a/kernel/cgroup/cgroup.c b/kernel/cgroup/cgroup.c
index f6df192417..e9625c92c3 100644
--- a/kernel/cgroup/cgroup.c
+++ b/kernel/cgroup/cgroup.c
@@ -36,6 +36,7 @@
#include <linux/magic.h>
#include <linux/mutex.h>
#include <linux/mount.h>
+#include <linux/overflow.h>
#include <linux/pagemap.h>
#include <linux/proc_fs.h>
#include <linux/rcupdate.h>
@@ -7541,12 +7542,19 @@ static u64 power_of_ten(int power)
*/
int cgroup_parse_float(const char *input, unsigned dec_shift, s64 *v)
{
- s64 whole, frac = 0;
+ u64 whole, frac = 0, scale, abs_v;
int wlen = 0;
unsigned int i;
const char *p;
+ bool negative = false;

- if (sscanf(input, "%lld%n", &whole, &wlen) != 1)
+ input = skip_spaces(input);
+ if (*input == '-') {
+ negative = true;
+ input++;
+ }
+
+ if (sscanf(input, "%llu%n", &whole, &wlen) != 1)
return -EINVAL;

p = input + wlen;
@@ -7565,7 +7573,18 @@ int cgroup_parse_float(const char *input, unsigned dec_shift, s64 *v)
frac++;
}

- *v = whole * power_of_ten(dec_shift) + frac;
+ scale = power_of_ten(dec_shift);
+ if (check_mul_overflow(whole, scale, &abs_v) ||
+ check_add_overflow(abs_v, frac, &abs_v))
+ return -ERANGE;
+
+ if (abs_v > S64_MAX)
+ return -ERANGE;
+
+ if (negative)
+ *v = -(s64)abs_v;
+ else
+ *v = abs_v;
return 0;
}

diff --git a/tools/testing/selftests/cgroup/test_cpu.c b/tools/testing/selftests/cgroup/test_cpu.c
index 369d4930c1..b8105bb000 100644
--- a/tools/testing/selftests/cgroup/test_cpu.c
+++ b/tools/testing/selftests/cgroup/test_cpu.c
@@ -819,6 +819,15 @@ 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.5", NULL },
+ { "- 0.004", "0.00\n" },
+ { "-0.5000000000000000000000000000000000", NULL },
+ { "-18446744073709551615", NULL },
{ "100.01", NULL },
{ "abc", NULL },
{ "0.5000000000000", "0.50\n" },

base-commit: 05afa0c55015b3c7f3c2136dde7fbcfe5195b21f
--
2.54.0 (Apple Git-157)