Skip to contentSkip to conversation

sched/idle: Destroy on-stack hrtimer in play_idle_precise()

PATCH In review Hui Su 3 participants 3 messages Oct 15 Scheduler

lore
Share & tools
lore

Hui Su

author
#0 lore raw

PATCH 1/1 sched/idle: Destroy on-stack hrtimer in play_idle_precise()

play_idle_precise() initializes it.timer with hrtimer_setup_on_stack(), but does not call destroy_hrtimer_on_stack() before the stack object goes out of scope.

With CONFIG_DEBUG_OBJECTS_TIMERS, hrtimer_setup_on_stack() registers the timer as an on-stack debug object. Such objects must be removed before leaving the function which allocated them.

Call destroy_hrtimer_on_stack() after the expiry loop to balance the on-stack timer lifetime.

Fixes c1de45ca831a ("sched/idle: Add support for tasks that inject idle")

---
Tested on x86_64 combined candidate tree:

- hrtimer setup/free oracle: PASS (127/127 and 129/129)

- bzImage and modules build: PASS

- Branch-specific validation after split: NOT RUN

 kernel/sched/idle.c | 1 +
 1 file changed, 1 insertion(+)
kernel/sched/idle.c +1 -0
index eb73b65ce6c4..96adec3f0a6d 100644
@@ -436,6 +436,7 @@ void play_idle_precise(u64 duration_ns, u64 latency_ns)
 
 	while (!READ_ONCE(it.done))
 		do_idle();
+	destroy_hrtimer_on_stack(&it.timer);
 
 	cpuidle_use_deepest_state(0);
 	current->flags &= ~PF_IDLE;

base-commit: 0fcb05b72344f40806cbe810de8872f1698bf053

maintainer
#1 lore raw
Quoted 10 lines · Hui Su

play_idle_precise() initializes it.timer with hrtimer_setup_on_stack(), but does not call destroy_hrtimer_on_stack() before the stack object goes out of scope.

With CONFIG_DEBUG_OBJECTS_TIMERS, hrtimer_setup_on_stack() registers the timer as an on-stack debug object. Such objects must be removed before leaving the function which allocated them.

Call destroy_hrtimer_on_stack() after the expiry loop to balance the on-stack timer lifetime.

Maybe, but I'm hating on this.

For one, the hrtimer_setup_on_stack() comment doesn't mention destroy_hrtimer_on_stack(), so how are we to know.

Secondly, we have __cleanup(), which gives us the opportunity to actually do the right thing and create a better interface.

reviewer Reviewed-by Tested-by on v1 1/1
#2 lore raw
In reply to Hui Su

Hello Hui,

Quoted 8 lines · Hui Su

play_idle_precise() initializes it.timer with hrtimer_setup_on_stack(), but does not call destroy_hrtimer_on_stack() before the stack object goes out of scope.

With CONFIG_DEBUG_OBJECTS_TIMERS, hrtimer_setup_on_stack() registers the timer as an on-stack debug object. Such objects must be removed before leaving the function which allocated them.

Quoted 5 lines

Call destroy_hrtimer_on_stack() after the expiry loop to balance the on-stack timer lifetime.

Fixes c1de45ca831a ("sched/idle: Add support for tasks that inject idle")

Feel free to include:

Quoted 17 lines

---
Tested on x86_64 combined candidate tree:

- hrtimer setup/free oracle: PASS (127/127 and 129/129)

- bzImage and modules build: PASS

- Branch-specific validation after split: NOT RUN

 kernel/sched/idle.c | 1 +
 1 file changed, 1 insertion(+)
kernel/sched/idle.c +0 -0
index eb73b65ce6c4..96adec3f0a6d 100644

kernel/sched/idle.c void play_idle_precise(u64 duration_ns, u64 latency_ns) · line 438

Quoted 3 lines
kernel/sched/idle.c +0 -0
@@ -436,6 +436,7 @@ void play_idle_precise(u64 duration_ns, u64 latency_ns)
 
 	while (!READ_ONCE(it.done))
 		do_idle();

nit. I would rather have that newline before destroy_hrtimer_on_stack() than after but that is just me :-)

No need to resend for that nit.

Quoted 7 lines
kernel/sched/idle.c +1 -0
+	destroy_hrtimer_on_stack(&it.timer);
 
 	cpuidle_use_deepest_state(0);
 	current->flags &= ~PF_IDLE;

base-commit: 0fcb05b72344f40806cbe810de8872f1698bf053

Signature · 2 lines
Thanks and Regards,
Prateek