powercap: idle_inject: Fix overflow in duration calculations
PATCH 1/1 powercap: idle_inject: Fix overflow in duration calculations
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.
--- drivers/powercap/idle_inject.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-)
drivers/powercap/idle_inject.c +6 -6
@@ -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 ;
Signature · 1 line
2.53.0