[PATCH] cgroup: Preserve sign for fractional input

From: Zhiqi Zhang

Date: Thu Nov 26 2026 - 02:00:18 EST


cgroup_parse_float() parses the whole and fractional parts separately with
sscanf(). The sign is only carried by the whole part, so inputs whose
whole part is zero, such as "-0.5", are composed as positive values.
Inputs such as "-1.5" are also composed as -0.5.

Callers such as cpu.uclamp.* and blk-iocost reject negative values
after parsing. With this behavior, negative fractional input can bypass
those checks and be accepted as a positive value.

Remember whether the input has a leading minus sign and subtract the
fractional part when composing a negative value.

Fixes: a5e112e6424a ("cgroup: add cgroup_parse_float()")
Signed-off-by: Zhiqi Zhang <zhiqizhangg@xxxxxxxxx>
---
AI contribution disclosure:
I found this bug. Codex modified the code, and I reviewed the change.

Validation:
- Built in a Linux container after applying the patch to a fresh shallow
clone from https://github.com/torvalds/linux.git:
make O=/tmp/kbuild ARCH=arm64 defconfig
make O=/tmp/kbuild ARCH=arm64 W=1 kernel/cgroup/cgroup.o
- scripts/checkpatch.pl --strict: clean

Duplicate checks:
- Checked torvalds/linux commit, PR, and code search for cgroup_parse_float
negative, -0.5, and sign-preservation fixes.
- Checked linux-next master and tj/cgroup master; both still compose the
fractional part as positive.

kernel/cgroup/cgroup.c | 7 ++++++-
1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/kernel/cgroup/cgroup.c b/kernel/cgroup/cgroup.c
index b3657e72db..bf7c3267db 100644
--- a/kernel/cgroup/cgroup.c
+++ b/kernel/cgroup/cgroup.c
@@ -7540,6 +7540,7 @@ static u64 power_of_ten(int power)
*/
int cgroup_parse_float(const char *input, unsigned dec_shift, s64 *v)
{
+ bool negative = *skip_spaces(input) == '-';
s64 whole, frac = 0;
int fstart = 0, fend = 0, flen;

@@ -7554,7 +7555,11 @@ int cgroup_parse_float(const char *input, unsigned dec_shift, s64 *v)
else
frac = DIV_ROUND_CLOSEST_ULL(frac, power_of_ten(flen - dec_shift));

- *v = whole * power_of_ten(dec_shift) + frac;
+ if (whole < 0 || negative)
+ *v = whole * power_of_ten(dec_shift) - frac;
+ else
+ *v = whole * power_of_ten(dec_shift) + frac;
+
return 0;
}

--
2.54.0 (Apple Git-157)