mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] cgroup: Preserve sign for fractional input
@ 2026-11-26  6:59 Zhiqi Zhang
  2026-11-27 14:49 ` Tejun Heo
  0 siblings, 1 reply; 2+ 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	[flat|nested] 2+ 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
  0 siblings, 0 replies; 2+ 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] 2+ messages in thread

end of thread, other threads:[~2026-11-27 14:49 UTC | newest]

Thread overview: 2+ 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

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®