From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.215.179 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796091739; b=cISk5QHMaxZFCpx7wcRZKS2PspSDtHgTYfDIt/pZZa8+vMHDBspRdcnpLn1xNsumAiKW w8jJaxHvUszPNN2VoGtrtQIHthVSnReL1FGJpnHAUGkIr+MbCCqx9u1226FMOZBgwGdCt Rnftv24ybKyMcvNt7f3rDgB/6GuTO0VawlVFyyKru1qzO68goLU+AyCIbrnWEyPF33tub klDQflm1QRMOkRKJH7lEiCIAKy4VWXn1JyLS+P4IdBfYi+zPA3vKlfUnt5Elkv8cAjvZd ulPVSriEkwJ+96EYJhvm6Umd5ZKk+TdQv8lty34fRQSl0TxMODHEuJ2qWQHgPvrcfIQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796091739; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received:Received: From:To:Cc:Subject:Date:Message-ID:X-Mailer:In-Reply-To:References: MIME-Version:Content-Transfer-Encoding; bh=ciklPKWJvzIHqMlwB7uuSnA28qih0kqUDmvYVLtgOZk=; b=oJcl/avYHIEdhJ+7cuxXO4lemFwJtqRjF5Z6bzRXuCKwtmzLLGzKV6isXbEOBcqe/SBm TlefqtOmOaT8/64lD8d6aPIocBmNyykrnfYpO8F/nN1lS31pkJDx9ILoYp7OFGlFwN/08 7nJ+uCm0BNurXkaTVyURa0kNl4mpJzEWToe/6rCBI3R1Vn1/wLogSgN12Cj2T2/LtRhou H6fvCTJZMJfcyMOyDwD4bGmMegnDHFwLLrKukcmjS3kA/T6bhjBzFMVIWFDKdhBsdfu9A 1ehssj8Tr1zneAe5UfDNENotYdzAxx7Ba1ar4qTbH+bpzutlckkGmr0WmJteQNW7Xuw== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=pFUO7adC; arc=none smtp.remote-ip=209.85.215.179 Received: from mail-pg1-f179.google.com (mail-pg1-f179.google.com [209.85.215.179]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D1618606A325 for ; Tue, 1 Dec 2026 02:22:18 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org D1618606A325 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=pFUO7adC Received: by mail-pg1-f179.google.com with SMTP id 41be03b00d2f7-cde1479f76fso5932479a12.0 for ; Mon, 30 Nov 2026 18:22:18 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796091738; x=1796696538; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=ciklPKWJvzIHqMlwB7uuSnA28qih0kqUDmvYVLtgOZk=; b=pFUO7adCKunNB41ZymV7HMfn3N9MGcV7Gam3cmbTVpNMpykINSY/+ODmHBBCCBrn9H AVP6oSMy67mJGtkZAHilB8jrZcNKYMLuKN1p3uWdffT1RzeVRUcYBlbunYeNVl69USKo aaUOeMnGuXPnVnyVubzLMgIg+3PWfJ70fTInupu4WH0Z0tlw48QdoS58Q5YXGqXscmqF bd8I58wx2E7TDip8zP/liR3q3icPpqnFOzhfmNqMkltqfSJJBUV63f60bdpci+g4Z9IJ BWbxTWOe/jNZXuNXcCNpic5RwLWODBe7NzzP24YHpC5b7pKU23Wn8XmOdONdH+Hi9Cmj rYfw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796091738; x=1796696538; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=ciklPKWJvzIHqMlwB7uuSnA28qih0kqUDmvYVLtgOZk=; b=vKxsaPm5cAC3A3qpOnsUiI97BPyfY7BJhMszwyNLdf0rxQ9fXRfYS1bqlaRJXZs/u7 4gbzgri8Acs41P/MkSpNR5Jee0PFFT9V26CTaWWhCsBJIQTc2vqZ7eG4husNJKHzEFSH m0+YGauhdTzpbPuSe/5junx8BtjrouNDBCrH+tEKIKzjhO+n/cHHEIQc02tsBKrfA6Ad iT3HOOkEVuPRNoN0YzvtzCpYEF5muSe3/QmqJlVbOfm3OIRpAl+TJ08GgdsEzamn+Bs9 5ig5qMTOrc2t3Gh1JJ/alyoD/4pwcQzQnBBHBqqaCPY0HGYjciP6ip2xOsRzO5oxYGUx ckaA== X-Forwarded-Encrypted: i=1; AL+hErEChCSvkZbRlD8LeJM3qbfD5qZIusvCYpQPELQG1JPOKODXhVESuix2yk1KO4B2RAXW9eLdjpDuBdFsBzE=@vger.kernel.org X-Gm-Message-State: APuU4Z95SmFjE/fK1V30WPmJPg/vAU7+2foBiUKBCAfPKRPRjvfz4M8Z 1kclzf+Xh5COnrmv5u2lURaJ7Gss0y1Tzhas4DBBZ+YdOdf7vphY+zWc+/d7CA== X-Gm-Gg: AZFdTZd2kzQO1tl2TdBE2nUKruz7d7G5jSYOFHy5CLmaYLcERPrNBTtkpaLIHIapQSs DkG1a6fMkn4v8ijuk4LVYWB6J8MZuT7k4n+x5Jx2Gm6+iYfxG/Cbv1SkeIhhqTNGBs0b0/o4BVv T7NAWNShZ8na6k4RJaoFmCbtUqkaNfP/TZBjfyycJJJNl2qHRVfpnh5d3qQw5P6h3DZ46QEhmQS zbsNBzdPerQLKWWHgavJgvyXbKuW46fE12VUcnNL/pW+e97O9enGJu9e0hlRNCYQM6qcH10qC9r Y2z+f2IfsAMMjq3hVPJIlHcdG0/pCL+DT4LVbzo0xpfIAfDUfR8b003hHB7dsDeV+/e5WTBaH1L Hd6zolkc14HoGBZR4CNYxW1W3fAjJ26cunYwgE4crG6FC6uK/c17aijZw+JoCgGr41U/L53wlKb uaNPOtaRSK89TOvy1ZUx/6hfEhqZ0/n8ARGYTHROJKFqRvVDHv0cqEODdvzxFMgGqCaAOD0TQAN nAQO6DTxTGd26mhktuvGGy1P3iKXpd0G1OEE+sAIHZsPHVPhtHDO4BRaZJyqcgPd3qBcvrOhzEW X+2+PBwdWT8txaJtZB7hj3klvLJVySueE4+h9QiKnTG8SrzPSz5bvfo+DqIN0hNP5zvcx+my4uq /0wWRZPrwyHVThYx3nrpDq8NfxD0gDdI63HYwTzLpiA== X-Received: by 2002:a17:90b:268f:b0:3b0:d0c5:c110 with SMTP id 98e67ed59e1d1-3b41c95ce7fmr34345709a91.56.1796091737965; Mon, 30 Nov 2026 18:22:17 -0800 (PST) Received: from LDYT9Q2G2M.pvgl.sap.corp ([2a09:bac5:cad0:1caa::2db:4c]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-cde6d05782asm18051921a12.24.2026.11.30.18.22.15 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Mon, 30 Nov 2026 18:22:17 -0800 (PST) From: Zhiqi Zhang To: Tejun Heo Cc: Zhiqi Zhang , Johannes Weiner , Michal Koutny , Shuah Khan , 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 Message-ID: <20261201022211.7609-1-zhiqizhangg@gmail.com> X-Mailer: git-send-email 2.54.0 In-Reply-To: <20261126065958.89207-1-zhiqizhangg@gmail.com> References: <20261126065958.89207-1-zhiqizhangg@gmail.com> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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 --- 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 #include #include +#include #include #include #include @@ -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)