* [PATCH] cgroup: Preserve sign for fractional input
@ 2026-11-26 6:59 Zhiqi Zhang
2026-11-27 14:49 ` Tejun Heo
2026-11-29 15:01 ` [PATCH v2] " Zhiqi Zhang
0 siblings, 2 replies; 4+ messages in thread
From: Zhiqi Zhang @ 2026-11-26 6:59 UTC (permalink / raw)
To: Tejun Heo, Johannes Weiner, Michal Koutny
Cc: cgroups, linux-kernel, Zhiqi Zhang
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@gmail.com>
---
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)
^ permalink raw reply related [flat|nested] 4+ messages in thread
* Re: [PATCH] cgroup: Preserve sign for fractional input
2026-11-26 6:59 [PATCH] cgroup: Preserve sign for fractional input Zhiqi Zhang
@ 2026-11-27 14:49 ` Tejun Heo
2026-11-29 15:01 ` [PATCH v2] " Zhiqi Zhang
1 sibling, 0 replies; 4+ messages in thread
From: Tejun Heo @ 2026-11-27 14:49 UTC (permalink / raw)
To: Zhiqi Zhang; +Cc: Johannes Weiner, Michal Koutny, cgroups, linux-kernel
Hello, Zhiqi.
On Thu, Nov 26, 2026 at 02:59:58PM +0800, Zhiqi Zhang wrote:
> 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.
This no longer applies. cgroup/for-7.4-fixes now has 4ac4766e93f3 ("cgroup:
Fix divide error on long fractions in cgroup_parse_float()"), which rewrote
the fraction parsing. Can you please rebase on top of it and resend?
Thanks.
--
tejun
^ permalink raw reply [flat|nested] 4+ messages in thread
* [PATCH v2] cgroup: Preserve sign for fractional input
2026-11-26 6:59 [PATCH] cgroup: Preserve sign for fractional input Zhiqi Zhang
2026-11-27 14:49 ` Tejun Heo
@ 2026-11-29 15:01 ` Zhiqi Zhang
2026-11-29 16:58 ` Tejun Heo
1 sibling, 1 reply; 4+ 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] 4+ messages in thread
* Re: [PATCH v2] cgroup: Preserve sign for fractional input
2026-11-29 15:01 ` [PATCH v2] " Zhiqi Zhang
@ 2026-11-29 16:58 ` Tejun Heo
0 siblings, 0 replies; 4+ messages in thread
From: Tejun Heo @ 2026-11-29 16:58 UTC (permalink / raw)
To: Zhiqi Zhang
Cc: Johannes Weiner, Michal Koutny, Shuah Khan, cgroups, linux-kernel,
linux-kselftest
Hello, Zhiqi.
On Sun, Nov 29, 2026 at 11:01:17PM +0800, Zhiqi Zhang wrote:
> + /* The whole part does not retain the sign of "-0". */
> + if (*skip_spaces(input) == '-')
> + frac = -frac;
> +
> *v = whole * power_of_ten(dec_shift) + frac;
Maybe read the sign once at the top and let the whole and fraction parsing
deal only with unsigned numbers, e.g. by switching the whole part to %llu,
and negate the composed result at the end?
While at it, can you also add an overflow check? A large whole part wraps
and gets accepted, e.g. "-18446744073709551615" written to cpu.uclamp.min
reads back as 1.00.
Thanks.
--
tejun
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2026-11-29 16:58 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-26 6:59 [PATCH] cgroup: Preserve sign for fractional input Zhiqi Zhang
2026-11-27 14:49 ` Tejun Heo
2026-11-29 15:01 ` [PATCH v2] " Zhiqi Zhang
2026-11-29 16:58 ` Tejun Heo
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox