[tip: timers/urgent] sched/idle: Use hrtimer cleanup class in play_idle_precise()

From: tip-bot2 for Hui Su

Date: Sun Oct 25 2026 - 17:47:30 EST


The following commit has been merged into the timers/urgent branch of tip:

Commit-ID: bd091346971c88fccd44afd2547d28366f824572
Gitweb: https://git.kernel.org/tip/bd091346971c88fccd44afd2547d28366f824572
Author: Hui Su <sh_def@xxxxxxx>
AuthorDate: Fri, 16 Oct 2026 10:09:44 +09:00
Committer: Thomas Gleixner <tglx@xxxxxxxxxx>
CommitterDate: Sun, 25 Oct 2026 22:43:19 +01:00

sched/idle: Use hrtimer cleanup class in play_idle_precise()

Pair on-stack setup with destruction using the hrtimer_on_stack cleanup
class in play_idle_precise().

Keep the cleanup object's scope around timer start and the idle wait. Start
the timer in pinned hard mode, so its callback runs as a hardirq on the
current CPU. The interrupted task observes it.done only after the callback
returns. Since the callback returns HRTIMER_NORESTART, the timer is not
requeued before the cleanup object tears down its on-stack debug object.

Fixes: c1de45ca831a ("sched/idle: Add support for tasks that inject idle")
Signed-off-by: Hui Su <sh_def@xxxxxxx>
Signed-off-by: Thomas Gleixner <tglx@xxxxxxxxxx>
Link: https://patch.msgid.link/eccd97b2003a16291817974798cbe98b25b582a0.1792083743.git.sh_def@xxxxxxx
---
kernel/sched/idle.c | 16 ++++++++++------
1 file changed, 10 insertions(+), 6 deletions(-)

diff --git a/kernel/sched/idle.c b/kernel/sched/idle.c
index eb73b65..2880e01 100644
--- a/kernel/sched/idle.c
+++ b/kernel/sched/idle.c
@@ -429,13 +429,17 @@ void play_idle_precise(u64 duration_ns, u64 latency_ns)
cpuidle_use_deepest_state(latency_ns);

it.done = 0;
- hrtimer_setup_on_stack(&it.timer, idle_inject_timer_fn, CLOCK_MONOTONIC,
- HRTIMER_MODE_REL_HARD);
- hrtimer_start(&it.timer, ns_to_ktime(duration_ns),
- HRTIMER_MODE_REL_PINNED_HARD);
+ {
+ CLASS(hrtimer_on_stack, timer)(&it.timer, idle_inject_timer_fn,
+ CLOCK_MONOTONIC,
+ HRTIMER_MODE_REL_HARD);

- while (!READ_ONCE(it.done))
- do_idle();
+ hrtimer_start(timer, ns_to_ktime(duration_ns),
+ HRTIMER_MODE_REL_PINNED_HARD);
+
+ while (!READ_ONCE(it.done))
+ do_idle();
+ }

cpuidle_use_deepest_state(0);
current->flags &= ~PF_IDLE;