* [PATCH v3 1/2] cgroup: Fix divide error on long fractions in cgroup_parse_float()
2026-11-26 5:41 [PATCH v3 0/2] cgroup: Fix cgroup_mutex deadlock through cgroup_parse_float() oops Chris Down
@ 2026-11-26 5:41 ` Chris Down
2026-11-26 5:41 ` [PATCH v3 2/2] selftests/cgroup: Test parsing of long fractions in cpu.uclamp.min Chris Down
2026-11-26 11:32 ` [PATCH v3 0/2] cgroup: Fix cgroup_mutex deadlock through cgroup_parse_float() oops Tejun Heo
2 siblings, 0 replies; 4+ messages in thread
From: Chris Down @ 2026-11-26 5:41 UTC (permalink / raw)
To: tj, hannes, mkoutny, shakeel.butt; +Cc: cgroups, linux-kernel
cgroup_parse_float() parses the decimal values written to
cpu.uclamp.min, cpu.uclamp.max and io.cost.qos. When there are too many
digits, it rounds the fraction with:
frac = DIV_ROUND_CLOSEST_ULL(frac, power_of_ten(flen - dec_shift));
do_div() takes a 32-bit divisor, so power_of_ten() is
truncated to its low 32 bits. That gives the wrong divisor from
10^10 onwards and zero at 10^32, because 2^32 divides 10^32.
This means that as it is now, writing "0." followed by 34 zeros to
cpu.uclamp.min or cpu.uclamp.max divides by zero.
The files belong to whoever owns the cgroup, so the owner of a
delegated subtree can do the same without any privilege:
# mkdir /sys/fs/cgroup/d
# cd /sys/fs/cgroup/d
# chown 1000:1000 . cgroup.procs cgroup.subtree_control cgroup.threads
# echo +cpu > cgroup.subtree_control
# setpriv --reuid=1000 --regid=1000 --clear-groups sh
$ mkdir x
$ echo 0.0000000000000000000000000000000000 > x/cpu.uclamp.min
Oops: divide error: 0000 [#1] SMP KASAN NOPTI
CPU: 3 UID: 1000 PID: 178 Comm: sh Not tainted 7.4.0-rc1
RIP: 0010:cgroup_parse_float (kernel/cgroup/cgroup.c:7553)
RCX: 0000000000000000
Call Trace:
cpu_uclamp_write (kernel/sched/core.c:9677 kernel/sched/core.c:9700)
kernfs_fop_write_iter (fs/kernfs/file.c:345)
vfs_write (fs/read_write.c:595 fs/read_write.c:687)
ksys_write (fs/read_write.c:739)
do_syscall_64 (arch/x86/entry/syscall_64.c:84)
entry_SYSCALL_64_after_hwframe (arch/x86/entry/entry_64.S:121)
Unfortunately the writer dies holding the kernfs active reference on
cpu.uclamp.min, so subsequent operations deadlock in kernfs_drain(),
even in unrelated cgroups:
task:rmdir state:D
kernfs_drain (fs/kernfs/dir.c:539)
__kernfs_remove (fs/kernfs/dir.c:1578)
kernfs_remove_by_name_ns (fs/kernfs/dir.c:1796)
cgroup_addrm_files (kernel/cgroup/cgroup.c:4596)
css_clear_dir (kernel/cgroup/cgroup.c:1770)
kill_css_sync (kernel/cgroup/cgroup.c:6274)
cgroup_destroy_locked (kernel/cgroup/cgroup.c:6394)
cgroup_rmdir (kernel/cgroup/cgroup.c:6442)
[...]
task:mkdir state:D
__mutex_lock (kernel/locking/mutex.c:821)
cgroup_kn_lock_live (kernel/cgroup/cgroup.c:1713)
cgroup_mkdir (kernel/cgroup/cgroup.c:6161)
[...]
To fix this, stop scanning the fraction as a number, and leave any
trailing digits alone. That avoids needing to divide, and thus the risk
of overflow.
(Dividing with div64_u64() and capping the exponent would also solve
this, but it would keep scanning the fraction into an s64, which long
fractions can still overflow. kstrtodec64() truncates rather than
rounds and is inconveniently too new for most stable trees, so let's do
it this way.)
Fixes: a5e112e6424a ("cgroup: add cgroup_parse_float()")
Cc: stable@vger.kernel.org
Signed-off-by: Chris Down <chris@chrisdown.name>
---
kernel/cgroup/cgroup.c | 29 ++++++++++++++++++++---------
1 file changed, 20 insertions(+), 9 deletions(-)
diff --git a/kernel/cgroup/cgroup.c b/kernel/cgroup/cgroup.c
index b3657e72db99..f6df19241772 100644
--- a/kernel/cgroup/cgroup.c
+++ b/kernel/cgroup/cgroup.c
@@ -29,6 +29,7 @@
#include <linux/bpf-cgroup.h>
#include <linux/cred.h>
+#include <linux/ctype.h>
#include <linux/errno.h>
#include <linux/init_task.h>
#include <linux/kernel.h>
@@ -7541,18 +7542,28 @@ static u64 power_of_ten(int power)
int cgroup_parse_float(const char *input, unsigned dec_shift, s64 *v)
{
s64 whole, frac = 0;
- int fstart = 0, fend = 0, flen;
+ int wlen = 0;
+ unsigned int i;
+ const char *p;
- if (!sscanf(input, "%lld.%n%lld%n", &whole, &fstart, &frac, &fend))
- return -EINVAL;
- if (frac < 0)
+ if (sscanf(input, "%lld%n", &whole, &wlen) != 1)
return -EINVAL;
- flen = fend > fstart ? fend - fstart : 0;
- if (flen < dec_shift)
- frac *= power_of_ten(dec_shift - flen);
- else
- frac = DIV_ROUND_CLOSEST_ULL(frac, power_of_ten(flen - dec_shift));
+ p = input + wlen;
+ if (*p == '.')
+ p++;
+
+ for (i = 0; i <= dec_shift; i++) {
+ int digit = 0;
+
+ if (isdigit(*p))
+ digit = *p++ - '0';
+
+ if (i < dec_shift)
+ frac = frac * 10 + digit;
+ else if (digit >= 5)
+ frac++;
+ }
*v = whole * power_of_ten(dec_shift) + frac;
return 0;
--
2.56.1
^ permalink raw reply related [flat|nested] 4+ messages in thread* [PATCH v3 2/2] selftests/cgroup: Test parsing of long fractions in cpu.uclamp.min
2026-11-26 5:41 [PATCH v3 0/2] cgroup: Fix cgroup_mutex deadlock through cgroup_parse_float() oops Chris Down
2026-11-26 5:41 ` [PATCH v3 1/2] cgroup: Fix divide error on long fractions in cgroup_parse_float() Chris Down
@ 2026-11-26 5:41 ` Chris Down
2026-11-26 11:32 ` [PATCH v3 0/2] cgroup: Fix cgroup_mutex deadlock through cgroup_parse_float() oops Tejun Heo
2 siblings, 0 replies; 4+ messages in thread
From: Chris Down @ 2026-11-26 5:41 UTC (permalink / raw)
To: tj, hannes, mkoutny, shakeel.butt; +Cc: cgroups, linux-kernel
Add a test to test_cpu that writes values to cpu.uclamp.min and reads
them back, checking that they are rounded to two decimal places and
that malformed or out of range values are rejected.
Signed-off-by: Chris Down <chris@chrisdown.name>
---
tools/testing/selftests/cgroup/config | 4 ++
tools/testing/selftests/cgroup/test_cpu.c | 71 +++++++++++++++++++++++
2 files changed, 75 insertions(+)
diff --git a/tools/testing/selftests/cgroup/config b/tools/testing/selftests/cgroup/config
index 39f979690dd3..c8b2c95a186e 100644
--- a/tools/testing/selftests/cgroup/config
+++ b/tools/testing/selftests/cgroup/config
@@ -2,5 +2,9 @@ CONFIG_CGROUPS=y
CONFIG_CGROUP_CPUACCT=y
CONFIG_CGROUP_FREEZER=y
CONFIG_CGROUP_SCHED=y
+CONFIG_CPU_FREQ=y
+CONFIG_CPU_FREQ_GOV_SCHEDUTIL=y
CONFIG_MEMCG=y
CONFIG_PAGE_COUNTER=y
+CONFIG_UCLAMP_TASK=y
+CONFIG_UCLAMP_TASK_GROUP=y
diff --git a/tools/testing/selftests/cgroup/test_cpu.c b/tools/testing/selftests/cgroup/test_cpu.c
index 3456e3e988c6..369d4930c1f4 100644
--- a/tools/testing/selftests/cgroup/test_cpu.c
+++ b/tools/testing/selftests/cgroup/test_cpu.c
@@ -809,6 +809,76 @@ cleanup:
return ret;
}
+static int test_cpucg_uclamp_parse(const char *root)
+{
+ static const struct {
+ char *input;
+ char *expected; /* NULL if the write must fail */
+ } cases[] = {
+ { "12.34", "12.34\n" },
+ { "50", "50.00\n" },
+ { "0.125", "0.13\n" },
+ { "0.999", "1.00\n" },
+ { "100.01", NULL },
+ { "abc", NULL },
+ { "0.5000000000000", "0.50\n" },
+ { "0.123456789012", "0.12\n" },
+ { "0.99999999999999999999", "1.00\n" },
+ { "0.0000000000000000000000000000000000", "0.00\n" },
+ };
+ int ret = KSFT_FAIL;
+ char buf[64];
+ char *cpucg;
+ int i, err;
+
+ cpucg = cg_name(root, "cpucg_test");
+ if (!cpucg)
+ goto cleanup;
+
+ if (cg_create(cpucg))
+ goto cleanup;
+
+ if (cg_read(cpucg, "cpu.uclamp.min", buf, sizeof(buf))) {
+ ret = KSFT_SKIP;
+ goto cleanup;
+ }
+
+ for (i = 0; i < ARRAY_SIZE(cases); i++) {
+ err = cg_write(cpucg, "cpu.uclamp.min", cases[i].input);
+
+ if (!cases[i].expected) {
+ if (!err) {
+ ksft_print_msg("%s was accepted\n", cases[i].input);
+ goto cleanup;
+ }
+ continue;
+ }
+
+ if (err) {
+ ksft_print_msg("%s was rejected: %s\n", cases[i].input,
+ strerror(-err));
+ goto cleanup;
+ }
+
+ if (cg_read(cpucg, "cpu.uclamp.min", buf, sizeof(buf)))
+ goto cleanup;
+
+ if (strcmp(buf, cases[i].expected)) {
+ ksft_print_msg("%s reads back as %s", cases[i].input,
+ buf);
+ goto cleanup;
+ }
+ }
+
+ ret = KSFT_PASS;
+
+cleanup:
+ cg_destroy(cpucg);
+ free(cpucg);
+
+ return ret;
+}
+
#define T(x) { x, #x }
struct cpucg_test {
int (*fn)(const char *root);
@@ -823,6 +893,7 @@ struct cpucg_test {
T(test_cpucg_nested_weight_underprovisioned),
T(test_cpucg_max),
T(test_cpucg_max_nested),
+ T(test_cpucg_uclamp_parse),
};
#undef T
--
2.56.1
^ permalink raw reply related [flat|nested] 4+ messages in thread