Linux Kernel Selftest development
 help / color / mirror / Atom feed
* [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
  0 siblings, 1 reply; 2+ 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] 2+ 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; 2+ 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] 2+ messages in thread

end of thread, other threads:[~2026-11-29 16:58 UTC | newest]

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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox