Skip to content

Commit 30c66fc

Browse files
Frederic WeisbeckerKAGA-KOKO
authored andcommitted
timer: Prevent base->clk from moving backward
When a timer is enqueued with a negative delta (ie: expiry is below base->clk), it gets added to the wheel as expiring now (base->clk). Yet the value that gets stored in base->next_expiry, while calling trigger_dyntick_cpu(), is the initial timer->expires value. The resulting state becomes: base->next_expiry < base->clk On the next timer enqueue, forward_timer_base() may accidentally rewind base->clk. As a possible outcome, timers may expire way too early, the worst case being that the highest wheel levels get spuriously processed again. To prevent from that, make sure that base->next_expiry doesn't get below base->clk. Fixes: a683f39 ("timers: Forward the wheel clock whenever possible") Signed-off-by: Frederic Weisbecker <[email protected]> Signed-off-by: Thomas Gleixner <[email protected]> Reviewed-by: Anna-Maria Behnsen <[email protected]> Tested-by: Juri Lelli <[email protected]> Cc: [email protected] Link: https://lkml.kernel.org/r/[email protected]
1 parent dcb7fd8 commit 30c66fc

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

kernel/time/timer.c

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -584,7 +584,15 @@ trigger_dyntick_cpu(struct timer_base *base, struct timer_list *timer)
584584
* Set the next expiry time and kick the CPU so it can reevaluate the
585585
* wheel:
586586
*/
587-
base->next_expiry = timer->expires;
587+
if (time_before(timer->expires, base->clk)) {
588+
/*
589+
* Prevent from forward_timer_base() moving the base->clk
590+
* backward
591+
*/
592+
base->next_expiry = base->clk;
593+
} else {
594+
base->next_expiry = timer->expires;
595+
}
588596
wake_up_nohz_cpu(base->cpu);
589597
}
590598

@@ -896,10 +904,13 @@ static inline void forward_timer_base(struct timer_base *base)
896904
* If the next expiry value is > jiffies, then we fast forward to
897905
* jiffies otherwise we forward to the next expiry value.
898906
*/
899-
if (time_after(base->next_expiry, jnow))
907+
if (time_after(base->next_expiry, jnow)) {
900908
base->clk = jnow;
901-
else
909+
} else {
910+
if (WARN_ON_ONCE(time_before(base->next_expiry, base->clk)))
911+
return;
902912
base->clk = base->next_expiry;
913+
}
903914
#endif
904915
}
905916

0 commit comments

Comments
 (0)