Re: [PATCH] powercap: idle_inject: Fix overflow in duration calculations

From: Rafael J. Wysocki (Intel)

Date: Wed Dec 02 2026 - 15:45:00 EST


On Sat, Nov 28, 2026 at 7:23 PM Sumeet Pawnikar <sumeet4linux@xxxxxxxxx> wrote:
>
> 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@xxxxxxxxx>

Applied as 7.5 material, thanks!

> ---
> 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
>
>