Skip to content

Commit dd54038

Browse files
Frederic WeisbeckerPeter Zijlstra
authored andcommitted
sched/cpuidle: Comment about timers requirements VS idle handler
Add missing explanation concerning IRQs re-enablement constraints in the cpuidle path against timers. Signed-off-by: Frederic Weisbecker <frederic@kernel.org> Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org> Acked-by: Rafael J. Wysocki <rafael@kernel.org> Link: https://lkml.kernel.org/r/20231114193840.4041-2-frederic@kernel.org
1 parent 63ba842 commit dd54038

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

kernel/sched/idle.c

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,36 @@ static void do_idle(void)
258258
while (!need_resched()) {
259259
rmb();
260260

261+
/*
262+
* Interrupts shouldn't be re-enabled from that point on until
263+
* the CPU sleeping instruction is reached. Otherwise an interrupt
264+
* may fire and queue a timer that would be ignored until the CPU
265+
* wakes from the sleeping instruction. And testing need_resched()
266+
* doesn't tell about pending needed timer reprogram.
267+
*
268+
* Several cases to consider:
269+
*
270+
* - SLEEP-UNTIL-PENDING-INTERRUPT based instructions such as
271+
* "wfi" or "mwait" are fine because they can be entered with
272+
* interrupt disabled.
273+
*
274+
* - sti;mwait() couple is fine because the interrupts are
275+
* re-enabled only upon the execution of mwait, leaving no gap
276+
* in-between.
277+
*
278+
* - ROLLBACK based idle handlers with the sleeping instruction
279+
* called with interrupts enabled are NOT fine. In this scheme
280+
* when the interrupt detects it has interrupted an idle handler,
281+
* it rolls back to its beginning which performs the
282+
* need_resched() check before re-executing the sleeping
283+
* instruction. This can leak a pending needed timer reprogram.
284+
* If such a scheme is really mandatory due to the lack of an
285+
* appropriate CPU sleeping instruction, then a FAST-FORWARD
286+
* must instead be applied: when the interrupt detects it has
287+
* interrupted an idle handler, it must resume to the end of
288+
* this idle handler so that the generic idle loop is iterated
289+
* again to reprogram the tick.
290+
*/
261291
local_irq_disable();
262292

263293
if (cpu_is_offline(cpu)) {

0 commit comments

Comments
 (0)