* [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
2026-12-01 2:22 ` [PATCH v3] " Zhiqi Zhang
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] cgroup: Preserve sign for fractional input 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
* [PATCH v3] cgroup: Preserve sign for fractional input
[not found] <20261126065958.89207-1-zhiqizhangg@gmail.com>
2026-11-29 15:01 ` [PATCH v2] cgroup: Preserve sign for fractional input Zhiqi Zhang
@ 2026-12-01 2:22 ` Zhiqi Zhang
2026-12-01 7:31 ` Tejun Heo
1 sibling, 1 reply; 4+ messages in thread
From: Zhiqi Zhang @ 2026-12-01 2:22 UTC (permalink / raw)
To: Tejun Heo
Cc: Zhiqi Zhang, Johannes Weiner, Michal Koutny, Shuah Khan, cgroups,
linux-kernel, linux-kselftest
cgroup_parse_float() used to parse the whole part with sscanf() and
accumulate and round the fractional digits separately. The sign was only
carried by the whole part, so "-0.5" was composed as 0.5 and "-1.5" as
-0.5. If the fraction rounded up to a whole unit, "-1.999" at two
decimal places was 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 let the whole
and fractional parsing operate on an unsigned magnitude. Compose the
scaled unsigned result with overflow checks, verify that it fits in s64,
and then apply the sign once at the end.
This also catches 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,
and whole-part overflow.
Fixes: a5e112e6424a ("cgroup: add cgroup_parse_float()")
Signed-off-by: Zhiqi Zhang <zhiqizhangg@gmail.com>
---
Changes since v2:
- Read the sign once and parse the whole part as an unsigned magnitude,
instead of fixing up only the fractional part after signed sscanf().
- Add overflow checks for whole-part accumulation and scaled composition,
rejecting the large whole-part wraparound case reported by Tejun.
- Add a cpu.uclamp.min selftest case for whole-part overflow.
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.
- Extend the existing cpu.uclamp.min parsing selftest with cases for
negative fractions, rounding and long fractional input.
Validation:
- git diff --check: clean.
- scripts/checkpatch.pl --strict: clean.
- Built and ran a small userspace C harness with ASan and UBSan for the
parser edge cases, including S64_MIN/S64_MAX boundaries and the reported
whole-part overflow case.
I have not booted the patched kernel to run the cgroup interface selftest.
AI assistance: I used Codex to adapt the fix to Tejun's v2 review, prepare
regression tests, and draft this revision's description.
kernel/cgroup/cgroup.c | 38 ++++++++++++++++++++---
tools/testing/selftests/cgroup/test_cpu.c | 7 +++++
2 files changed, 40 insertions(+), 5 deletions(-)
diff --git a/kernel/cgroup/cgroup.c b/kernel/cgroup/cgroup.c
index f6df192417..c94e7488cb 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,15 +7542,30 @@ static u64 power_of_ten(int power)
*/
int cgroup_parse_float(const char *input, unsigned dec_shift, s64 *v)
{
- s64 whole, frac = 0;
- int wlen = 0;
+ u64 whole, frac = 0, scale, abs_v, max_abs;
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++;
+ }
+
+ whole = 0;
+ p = input;
+ if (!isdigit(*p))
return -EINVAL;
- p = input + wlen;
+ while (isdigit(*p)) {
+ int digit = *p++ - '0';
+
+ if (check_mul_overflow(whole, 10, &whole) ||
+ check_add_overflow(whole, digit, &whole))
+ return -ERANGE;
+ }
+
if (*p == '.')
p++;
@@ -7565,7 +7581,19 @@ 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;
+
+ max_abs = negative ? (u64)S64_MAX + 1 : S64_MAX;
+ if (abs_v > max_abs)
+ return -ERANGE;
+
+ if (negative)
+ *v = abs_v == max_abs ? S64_MIN : -(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..b622f99796 100644
--- a/tools/testing/selftests/cgroup/test_cpu.c
+++ b/tools/testing/selftests/cgroup/test_cpu.c
@@ -819,6 +819,13 @@ 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 },
+ { "-18446744073709551615", 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 v3] cgroup: Preserve sign for fractional input
2026-12-01 2:22 ` [PATCH v3] " Zhiqi Zhang
@ 2026-12-01 7:31 ` Tejun Heo
0 siblings, 0 replies; 4+ messages in thread
From: Tejun Heo @ 2026-12-01 7:31 UTC (permalink / raw)
To: Zhiqi Zhang
Cc: Johannes Weiner, Michal Koutny, Shuah Khan, cgroups, linux-kernel,
linux-kselftest
Hello, Zhiqi.
On Tue, Dec 01, 2026 at 10:22:11AM +0800, Zhiqi Zhang wrote:
> + whole = 0;
> + p = input;
> + if (!isdigit(*p))
> return -EINVAL;
>
> - p = input + wlen;
> + while (isdigit(*p)) {
> + int digit = *p++ - '0';
> +
> + if (check_mul_overflow(whole, 10, &whole) ||
> + check_add_overflow(whole, digit, &whole))
> + return -ERANGE;
> + }
Can you keep sscanf() for the whole part and just switch it to %llu after
reading the sign? That would accept whitespace between the sign and the
digits but that's fine.
> + max_abs = negative ? (u64)S64_MAX + 1 : S64_MAX;
> + if (abs_v > max_abs)
> + return -ERANGE;
> +
> + if (negative)
> + *v = abs_v == max_abs ? S64_MIN : -(s64)abs_v;
> + else
> + *v = abs_v;
Maybe just range check against S64_MAX and negate if negative? No need to
handle S64_MIN.
Thanks.
--
tejun
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2026-12-01 7:31 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
[not found] <20261126065958.89207-1-zhiqizhangg@gmail.com>
2026-11-29 15:01 ` [PATCH v2] cgroup: Preserve sign for fractional input Zhiqi Zhang
2026-11-29 16:58 ` Tejun Heo
2026-12-01 2:22 ` [PATCH v3] " Zhiqi Zhang
2026-12-01 7:31 ` Tejun Heo
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox