From: Zhiqi Zhang <zhiqizhangg@gmail.com>
To: Tejun Heo <tj@kernel.org>
Cc: Zhiqi Zhang <zhiqizhangg@gmail.com>,
Johannes Weiner <hannes@cmpxchg.org>,
Michal Koutny <mkoutny@suse.com>, Shuah Khan <shuah@kernel.org>,
cgroups@vger.kernel.org, linux-kernel@vger.kernel.org,
linux-kselftest@vger.kernel.org
Subject: [PATCH v3] cgroup: Preserve sign for fractional input
Date: Tue, 1 Dec 2026 10:22:11 +0800 [thread overview]
Message-ID: <20261201022211.7609-1-zhiqizhangg@gmail.com> (raw)
In-Reply-To: <20261126065958.89207-1-zhiqizhangg@gmail.com>
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)
next prev parent reply other threads:[~2026-12-01 2:22 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-26 6:59 [PATCH] " 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
2026-12-01 2:22 ` Zhiqi Zhang [this message]
2026-12-01 7:31 ` [PATCH v3] " Tejun Heo
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261201022211.7609-1-zhiqizhangg@gmail.com \
--to=zhiqizhangg@gmail.com \
--cc=cgroups@vger.kernel.org \
--cc=hannes@cmpxchg.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-kselftest@vger.kernel.org \
--cc=mkoutny@suse.com \
--cc=shuah@kernel.org \
--cc=tj@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®