Linux Power Management development
 help / color / mirror / Atom feed
From: Sumeet Pawnikar <sumeet4linux@gmail.com>
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	[thread overview]
Message-ID: <20261128182323.7294-1-sumeet4linux@gmail.com> (raw)

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 <sumeet4linux@gmail.com>
---
 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


                 reply	other threads:[~2026-11-28 18:23 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261128182323.7294-1-sumeet4linux@gmail.com \
    --to=sumeet4linux@gmail.com \
    --cc=amit.kucheria@linaro.org \
    --cc=daniel.lezcano@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-pm@vger.kernel.org \
    --cc=rafael@kernel.org \
    --cc=srinivas.pandruvada@linux.intel.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox