From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.82.179 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795890228; b=HgUqiDi8TEfxkx4Tm1AqNgXFBlAVBzxafGACsD3jeYT0dbOS2CXzyqBsKOohDEuYBrHD NgLAsIy9S3guZff73QAe3P4wlFSeiWZIvYZTUGFYFK2crfwr8SAI5JsUpIgBBwT1iZl62 rsgscG2Lb4vyE6yP3PBrvjDqgd18nML9pgqJMthr6X9jIYS80HGFv1vUz0jXV/CppJRGC ifHKlSZyeW4H9L3RAtOq7BLed9fxMXWxGmcxTIds0WjpWhO8HRy7Jqg96fwqz+9ds4pUP JVS/waWNp/xOpBrsXCvKiVBOkobFtZdV6CY8q3yhxAU6daA/HDGwWFlaG7ShkhH9tZA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795890228; 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:MIME-Version: Content-Transfer-Encoding; bh=JQ8uRDUWF+NQ4T0bTDuUTJs9j6sya5WeJf5kOO78yyY=; b=i7fXxAcq6SNASn5+IPI/FKcGg7h5rewyrn+5/s8QoDiFrHiuObnUOT2FJZxXpi9t1YuB datSQbi2JaSsl+0tk2ktJLNsp0eVkwh/sIKA9UlRlooAmsmndH5yDEkg6K9i3AdPcz4U5 DRdKJv+QBUtdc5N37DPQfjH8IxwcC4QNNFvLL/i8KX+huLrvuGjU60H0evLraFHyKxPU0 /+5IJA+HzFUV3dzl+IzyxuWmq0W1ZHHfLxDUwKrVT4VTo8IPcD1BDepZf5BxuARyIJXOy kKU+HoPab+0R+T7Kxci5O9EMMq9zu14bNzdua/VLF4uZf23+SZfY/UOYFbOT9NYCkmw== 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=CPKuNePI; arc=none smtp.remote-ip=74.125.82.179 Received: from mail-dy1-f179.google.com (mail-dy1-f179.google.com [74.125.82.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 30B94606A321 for ; Sat, 28 Nov 2026 18:23:48 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 30B94606A321 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=CPKuNePI Received: by mail-dy1-f179.google.com with SMTP id 5a478bee46e88-386ab9ab619so13031578eec.1 for ; Sat, 28 Nov 2026 10:23:48 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1795890227; x=1796495027; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to:content-type; bh=JQ8uRDUWF+NQ4T0bTDuUTJs9j6sya5WeJf5kOO78yyY=; b=CPKuNePIxfu0Gdzs+QycIJg/y9F64AW/FMTLyXCIM73OdYDJH4NRleQbgaW2tDoVz0 yy4mouM8e9w08eLEwT46IeoRMlo4hjwZpEsFBScuPcJKAmLLwVsgckDsC3jT3XV//kja 0u8dHFB1tUxyxVBO+oPSkdANRfx7M1PwktcY2/gJ0exBomoXo/mUBQcnw3ohfMB9gf/b ztkG32jcs2F+rbam0sxIuHTe0xUt1NKw9+PWc7RjerwUzrBrQ5slxyE2Flvxqjd627XB fUt9eWvAVXVQXUeMRPHdrIX0gwKQs+yEFheX80cSLxyvdOG6svvGmFa9O+CNbJO3FNPo C+NA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1795890227; x=1796495027; h=content-transfer-encoding:mime-version: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=JQ8uRDUWF+NQ4T0bTDuUTJs9j6sya5WeJf5kOO78yyY=; b=ZPGEFpc/2BLm6euKCIV10lnw5ZVSpN1th+khBObYFJtAMtVBquIwGbR9iX7RLmP+j5 FFp9RZOv5M72WV/mbwN2OkQC5GeUjoIdWq75Y/gZowTIl2bEXBESoQrH5p3IkMnM7ic7 vJhysBrZ8Mv8360R6UYh7s2enXo0wEKQk4QQP4m6ymSXJjasT3K6XHPOGH29T4XSasRQ /i0GTOzf4LM4BJAneTaJ25LT2OQYkBz8IooniUtaqn+u5nrqvwjnXndU48pMuPjeFnvl UhdOllnF83EBpdKoEPM4yqOM7vAnu51mQvIuFTX1ttGma54+OM2d1bIKy+O7ZA+11keb iJ9Q== X-Forwarded-Encrypted: i=1; AL+hErFaX9ZqcbTMtc2F8UYBlcMGXjEHG/3yJupkOduLBun5efsdCe69l4xsbs/hVPjmG+feutXAyTPrSg==@vger.kernel.org X-Gm-Message-State: APuU4Z+1ZITnZL9/wFsjD491ULHjnvdLymH07+RJL/CXbJbXX92lLGpt E7nXbpKoCqQth2uOVuaZa8hCI4jRfdVC8D0mTN/KIS9tFg7n2CstituMvjbU8TW9 X-Gm-Gg: AZFdTZeuexbRYeYXbf5V8VjvQISd0kUMf/LF5zd+JdJhMRUzSmUJfLj8j11EOPTzKZ0 N5KG/0cJgY7by9y3OLrgI+Yp3/h1GBT7lwQS2MzxqeArFS5WHz837DKqOe/KieuyW9XWrblOKxe FKJ5WgvHmg8/KBZrgTVxDwiCKCUW8iZ/bq7w1Y5w1/cbagXylZBqh7Z/2XCnpwmhNC15JkV6cew 7Tbu39pBJ7cB3Qcn9/h59D9uNHGDbcsF00nIzYXrHYReLLHhEI12ceyLonLc+enup0lBncp/Kw5 BtCrYfUHkVoNF0pLn1bh4lrkv8tUL1JxJhx+f8DbryEQ7JPrGn1i7V6vJsuWHqaEbY84z+qd8qr bm4PqqeIx7pXdvvfuam9yIpxCoRWMZqLpuElF0pvErQ06OPy7JN0xyb9pQSB2Cq6uLrLWpliqlk orXGMtPpcUjyzCpAL2kYEOyGzBIc4/A24FgRmHQY3tRVOxwE3e6MDak25TZ5a2PsyMOgE6cP4sb u/YvINPx1BIVontU0cqPVOELn/vozfCm6w9lMef7L/Mt91tsEhw03fHoO3/jOPiwk9dLJs/Kw79 rrk3csmIFGW9Q+U4v2FJIhFHNBeiETqP+gaJEydrJdVLga75DWd8 X-Received: by 2002:a05:7300:de44:b0:375:c5e1:931d with SMTP id 5a478bee46e88-3857073ebabmr27949898eec.25.1795890227118; Sat, 28 Nov 2026 10:23:47 -0800 (PST) Received: from sumeet-VirtualBox ([58.84.62.188]) by smtp.gmail.com with ESMTPSA id 5a478bee46e88-385c6830815sm76505422eec.20.2026.11.28.10.23.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 28 Nov 2026 10:23:46 -0800 (PST) From: Sumeet Pawnikar To: rafael@kernel.org, daniel.lezcano@kernel.org, linux-pm@vger.kernel.org Cc: srinivas.pandruvada@linux.intel.com, amit.kucheria@linaro.org, linux-kernel@vger.kernel.org, sumeet4linux@gmail.com Subject: [PATCH] powercap: idle_inject: Fix overflow in duration calculations Date: Sat, 28 Nov 2026 23:53:23 +0530 Message-ID: <20261128182323.7294-1-sumeet4linux@gmail.com> X-Mailer: git-send-email 2.53.0 X-Mailing-List: linux-pm@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 The idle injection API accepts run and idle durations independently as unsigned int microseconds. Their sum can exceed UINT_MAX. For example, a run duration of 1 and an idle duration of UINT_MAX wrap the sum to zero, causing idle_inject_set_duration() to discard both values. Other sums wrap to a short period, so the timer can wake the threads much sooner than the requested interval. Test the durations individually for zero and use u64 for the sum before converting it to a timer interval. The full sum of two unsigned int microsecond values fits in ktime_t after conversion to nanoseconds. The multiplication used to pass idle duration and latency to play_idle_precise() also overflows on 32-bit systems because it is performed before conversion to u64. Widen both operands first. Built bzImage with i386_defconfig and x86_64_defconfig, idle injection enabled, GCC 8.5.0 and W=1. Both kernels passed guest runtime checks in QEMU/TCG for zero checks, normal and maximum periods, periodic callbacks, stop/restart, and overflow during timer rearming. Kprobes verified the actual duration and latency arguments passed to play_idle_precise() on both architectures. An unpatched i386 guest failed the four overflow checks while passing the normal behavior checks. The tests inspected long timer periods without waiting for them to expire; hardware power and thermal behavior were not tested. Backport notes: On trees using ns_to_ktime(), retain that helper and widen the sum before multiplication in idle_inject_start(). Changing the callback accumulator to u64 also widens its multiplication. On trees without c7cd6f04c0df, keep the existing requirement that both durations be nonzero and omit the two guard changes. Neither that 100% idle support change nor the us_to_ktime() cleanup is a backport prerequisite. Fixes: cd4c0763064f ("powercap: idle_inject: Use higher resolution for idle injection") Fixes: c7cd6f04c0df ("powercap: idle_inject: Support 100% idle injection") Fixes: 333cff6c963f ("powercap/drivers/idle_inject: Specify idle state max latency") Signed-off-by: Sumeet Pawnikar --- drivers/powercap/idle_inject.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/drivers/powercap/idle_inject.c b/drivers/powercap/idle_inject.c index a25eb2018acd..b6a52f7a294a 100644 --- a/drivers/powercap/idle_inject.c +++ b/drivers/powercap/idle_inject.c @@ -123,7 +123,7 @@ static void idle_inject_wakeup(struct idle_inject_device *ii_dev) */ static enum hrtimer_restart idle_inject_timer_fn(struct hrtimer *timer) { - unsigned int duration_us; + u64 duration_us; struct idle_inject_device *ii_dev = container_of(timer, struct idle_inject_device, timer); @@ -158,8 +158,8 @@ static void idle_inject_fn(unsigned int cpu) */ iit->should_run = 0; - play_idle_precise(READ_ONCE(ii_dev->idle_duration_us) * NSEC_PER_USEC, - READ_ONCE(ii_dev->latency_us) * NSEC_PER_USEC); + play_idle_precise((u64)READ_ONCE(ii_dev->idle_duration_us) * NSEC_PER_USEC, + (u64)READ_ONCE(ii_dev->latency_us) * NSEC_PER_USEC); } /** @@ -172,7 +172,7 @@ void idle_inject_set_duration(struct idle_inject_device *ii_dev, unsigned int run_duration_us, unsigned int idle_duration_us) { - if (run_duration_us + idle_duration_us) { + if (run_duration_us || idle_duration_us) { WRITE_ONCE(ii_dev->run_duration_us, run_duration_us); WRITE_ONCE(ii_dev->idle_duration_us, idle_duration_us); } @@ -223,7 +223,7 @@ int idle_inject_start(struct idle_inject_device *ii_dev) unsigned int idle_duration_us = READ_ONCE(ii_dev->idle_duration_us); unsigned int run_duration_us = READ_ONCE(ii_dev->run_duration_us); - if (!(idle_duration_us + run_duration_us)) + if (!idle_duration_us && !run_duration_us) return -EINVAL; pr_debug("Starting injecting idle cycles on CPUs '%*pbl'\n", @@ -232,7 +232,7 @@ int idle_inject_start(struct idle_inject_device *ii_dev) idle_inject_wakeup(ii_dev); hrtimer_start(&ii_dev->timer, - us_to_ktime(idle_duration_us + run_duration_us), + us_to_ktime((u64)idle_duration_us + run_duration_us), HRTIMER_MODE_REL); return 0; -- 2.53.0