timers: Fix base clock forwarding issues
cover timers: Fix base clock forwarding issues
Several people have reported USB timeout issues starting in 4.8 which are related to the timer wheel overhaul.
The following two patches adress the brown paperbag bugs in that code.
Thanks,
tglx
PATCH 1/2 timers: Prevent base clock rewind when forwarding clock
Ashton and Michal reported, that kernel versions 4.8 and later suffer from USB timeouts which are caused by the timer wheel rework.
This is caused by a bug in the base clock forwarding mechanism, which leads to timers expiring early. The scenario which leads to this is:
run_timers()
while (jiffies >= base->clk) {
collect_expired_timers();
base->clk++;
expire_timers();}
So base->clk = jiffies + 1. Now the cpu goes idle:
idle()
get_next_timer_interrupt()
nextevt = __next_time_interrupt();
if (time_after(nextevt, base->clk))
base->clk = jiffies;jiffies has not advanced since run_timers(), so this assignment effectively decrements base->clk by one.
base->clk is the index into the timer wheel arrays. So let's assume the following state after the base->clk increment in run_timers():
jiffies = 0
base->clk = 1
A timer gets enqueued with an expiry delta of 63 ticks (which is the case with the USB timeout and HZ=250) so the resulting bucket index is:
base->clk + delta = 1 + 63 = 64
The timer goes into the first wheel level. The array size is 64 so it ends up in bucket 0, which is correct as it takes 63 ticks to advance base->clk to index into bucket 0 again.
If the cpu goes idle before jiffies advance, then the bug in the forwarding mechanism sets base->clk back to 0, so the next invocation of run_timers() at the next tick will index into bucket 0 and therefore expire the timer 62 ticks too early.
Instead of blindly setting base->clk to jiffies we must make the forwarding conditional on jiffies > base->clk, but we cannot use jiffies for this as we might run into the following issue:
if (time_after(jiffies, base->clk) {
if (time_after(nextevt, base->clk))
base->clk = jiffies;jiffies can increment between the check and the assigment far enough to advance beyond nextevt. So we need to use a stable value for checking.
get_next_timer_interrupt() has the basej argument which is the jiffies value snapshot taken in the calling code. So we can just that.
Thanks to Ashton for bisecting and providing trace data!
--- kernel/time/timer.c | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-)
kernel/time/timer.c +9 -5
@@ -1510,12 +1510,16 @@ u64 get_next_timer_interrupt(unsigned lo is_max_delta = (nextevt == base->clk +NEXT_TIMER_MAX_DELTA ); base->next_expiry = nextevt;/* -* We have a fresh next event. Check whether we can forward the base: +* We have a fresh next event. Check whether we can forward the +* base. We can only do that when @basej is past base->clk +* otherwise we might rewind base->clk. */ -if (time_after (nextevt, jiffies)) - base->clk = jiffies; -else if (time_after (nextevt, base->clk)) - base->clk = nextevt; +if (time_after (basej, base->clk)) { +if (time_after (nextevt, basej)) + base->clk = basej; +else if (time_after (nextevt, base->clk)) + base->clk = nextevt; + }if (time_before_eq (nextevt, basej)) { expires = basem;
tip-bot for Thomas Gleixner
merge bot[tip:timers/urgent] timers: Prevent base clock rewind when forwarding clock
timers: Prevent base clock rewind when forwarding clock
Commit-ID: 041ad7bc758db259bb960ef795197dd14aab19a6 Gitweb: http://git.kernel.org/tip/041ad7bc758db259bb960ef795197dd14aab19a6 Author: Thomas Gleixner <[email protected]> AuthorDate: Sat, 22 Oct 2016 11:07:35 +0000 Committer: Thomas Gleixner <[email protected]> CommitDate: Tue, 25 Oct 2016 16:32:50 +0200
timers: Prevent base clock rewind when forwarding clock
Ashton and Michael reported, that kernel versions 4.8 and later suffer from USB timeouts which are caused by the timer wheel rework.
This is caused by a bug in the base clock forwarding mechanism, which leads to timers expiring early. The scenario which leads to this is:
run_timers()
while (jiffies >= base->clk) {
collect_expired_timers();
base->clk++;
expire_timers();}
So base->clk = jiffies + 1. Now the cpu goes idle:
idle()
get_next_timer_interrupt()
nextevt = __next_time_interrupt();
if (time_after(nextevt, base->clk))
base->clk = jiffies;jiffies has not advanced since run_timers(), so this assignment effectively decrements base->clk by one.
base->clk is the index into the timer wheel arrays. So let's assume the following state after the base->clk increment in run_timers():
jiffies = 0
base->clk = 1
A timer gets enqueued with an expiry delta of 63 ticks (which is the case with the USB timeout and HZ=250) so the resulting bucket index is:
base->clk + delta = 1 + 63 = 64
The timer goes into the first wheel level. The array size is 64 so it ends up in bucket 0, which is correct as it takes 63 ticks to advance base->clk to index into bucket 0 again.
If the cpu goes idle before jiffies advance, then the bug in the forwarding mechanism sets base->clk back to 0, so the next invocation of run_timers() at the next tick will index into bucket 0 and therefore expire the timer 62 ticks too early.
Instead of blindly setting base->clk to jiffies we must make the forwarding conditional on jiffies > base->clk, but we cannot use jiffies for this as we might run into the following issue:
if (time_after(jiffies, base->clk) {
if (time_after(nextevt, base->clk))
base->clk = jiffies;jiffies can increment between the check and the assigment far enough to advance beyond nextevt. So we need to use a stable value for checking.
get_next_timer_interrupt() has the basej argument which is the jiffies value snapshot taken in the calling code. So we can just that.
Thanks to Ashton for bisecting and providing trace data!
--- kernel/time/timer.c | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-)
kernel/time/timer.c +9 -5
@@ -1523,12 +1523,16 @@ u64 get_next_timer_interrupt(unsigned long basej, u64 basem) is_max_delta = (nextevt == base->clk +NEXT_TIMER_MAX_DELTA ); base->next_expiry = nextevt;/* -* We have a fresh next event. Check whether we can forward the base: +* We have a fresh next event. Check whether we can forward the +* base. We can only do that when @basej is past base->clk +* otherwise we might rewind base->clk. */ -if (time_after (nextevt, jiffies)) - base->clk = jiffies; -else if (time_after (nextevt, base->clk)) - base->clk = nextevt; +if (time_after (basej, base->clk)) { +if (time_after (nextevt, basej)) + base->clk = basej; +else if (time_after (nextevt, base->clk)) + base->clk = nextevt; + }if (time_before_eq (nextevt, basej)) { expires = basem;
PATCH 2/2 timers: Prevent base clock corruption when forwarding
In reply to Thomas GleixnerWhen a timer is enqueued we try to forward the timer base clock. This mechanism has two issues:
1) Forwarding a remote base unlocked
The forwarding function is called from get_target_base() with the current timer base lock held. But if the new target base is a different base than the current base (can happen with NOHZ, sigh!) then the forwarding is done on an unlocked base. This can lead to corruption of base->clk.
Solution is simple: Invoke the forwarding after the target base is locked.
2) Possible corruption due to jiffies advancing
This is similar to the issue in get_net_timer_interrupt() which was fixed in the previous patch. jiffies can advance between check and assignement and therefor advancing base->clk after the next expiry value.
So we need to read jiffies once into a local variable once and do the
checks and assignment with the local copy.
--- kernel/time/timer.c | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-)
kernel/time/timer.c +10 -13
@@ -878,7 +878,7 @@ static inline struct timer_base *get_tim#ifdef CONFIG_NO_HZ_COMMON static inline struct timer_base * -__get_target_base (struct timer_base *base,unsigned tflags) +get_target_base (struct timer_base *base,unsigned tflags) {#ifdef CONFIG_SMP if ((tflags &TIMER_PINNED ) || !base->migration_enabled)
@@ -891,25 +891,27 @@ static inline struct timer_base *static inline void forward_timer_base (struct timer_base *base) { +unsigned long jnow =READ_ONCE (jiffies); +/* * We only forward the base when it's idle and we have a delta between * base clock and jiffies. */ -if (!base->is_idle || (long ) (jiffies - base->clk) <2 ) +if (!base->is_idle || (long ) (jnow - base->clk) <2 )return ;/* * If the next expiry value is > jiffies, then we fast forward to * jiffies otherwise we forward to the next expiry value. */ -if (time_after (base->next_expiry, jiffies)) - base->clk = jiffies; +if (time_after (base->next_expiry, jnow)) + base->clk = jnow;else base->clk = base->next_expiry; }#else static inline struct timer_base * -__get_target_base (struct timer_base *base,unsigned tflags) +get_target_base (struct timer_base *base,unsigned tflags) {return get_timer_this_cpu_base (tflags); }
@@ -917,14 +919,6 @@ static inline struct timer_base *static inline void forward_timer_base (struct timer_base *base) { }#endif -static inline struct timer_base * -get_target_base (struct timer_base *base,unsigned tflags) -{ -struct timer_base *target =__get_target_base (base, tflags); - -forward_timer_base (target); -return target; -}/* * We are using hashed locking: Holding per_cpu(timer_bases[x]).lock means
@@ -1025,6 +1019,9 @@ static inline int } } +/* Try to forward a stale timer base clock */ +forward_timer_base (base); + timer->expires = expires;/* * If 'idx' was calculated above and the base time did not advance
tip-bot for Thomas Gleixner
merge bot[tip:timers/urgent] timers: Prevent base clock corruption when forwarding
timers: Prevent base clock corruption when forwarding
Commit-ID: 6bad6bccf2d717f652d37e63cf261eaa23466009 Gitweb: http://git.kernel.org/tip/6bad6bccf2d717f652d37e63cf261eaa23466009 Author: Thomas Gleixner <[email protected]> AuthorDate: Sat, 22 Oct 2016 11:07:37 +0000 Committer: Thomas Gleixner <[email protected]> CommitDate: Tue, 25 Oct 2016 16:32:50 +0200
timers: Prevent base clock corruption when forwarding
When a timer is enqueued we try to forward the timer base clock. This mechanism has two issues:
1) Forwarding a remote base unlocked
The forwarding function is called from get_target_base() with the current timer base lock held. But if the new target base is a different base than the current base (can happen with NOHZ, sigh!) then the forwarding is done on an unlocked base. This can lead to corruption of base->clk.
Solution is simple: Invoke the forwarding after the target base is locked.
2) Possible corruption due to jiffies advancing
This is similar to the issue in get_net_timer_interrupt() which was fixed in the previous patch. jiffies can advance between check and assignement and therefore advancing base->clk beyond the next expiry value.
So we need to read jiffies into a local variable once and do the checks and assignment with the local copy.
--- kernel/time/timer.c | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-)
kernel/time/timer.c +10 -13
@@ -878,7 +878,7 @@ static inline struct timer_base *get_timer_base(u32 tflags)#ifdef CONFIG_NO_HZ_COMMON static inline struct timer_base * -__get_target_base (struct timer_base *base,unsigned tflags) +get_target_base (struct timer_base *base,unsigned tflags) {#ifdef CONFIG_SMP if ((tflags &TIMER_PINNED ) || !base->migration_enabled)
@@ -891,25 +891,27 @@ __get_target_base(struct timer_base *base, unsigned tflags)static inline void forward_timer_base (struct timer_base *base) { +unsigned long jnow =READ_ONCE (jiffies); +/* * We only forward the base when it's idle and we have a delta between * base clock and jiffies. */ -if (!base->is_idle || (long ) (jiffies - base->clk) <2 ) +if (!base->is_idle || (long ) (jnow - base->clk) <2 )return ;/* * If the next expiry value is > jiffies, then we fast forward to * jiffies otherwise we forward to the next expiry value. */ -if (time_after (base->next_expiry, jiffies)) - base->clk = jiffies; +if (time_after (base->next_expiry, jnow)) + base->clk = jnow;else base->clk = base->next_expiry; }#else static inline struct timer_base * -__get_target_base (struct timer_base *base,unsigned tflags) +get_target_base (struct timer_base *base,unsigned tflags) {return get_timer_this_cpu_base (tflags); }
@@ -917,14 +919,6 @@ __get_target_base(struct timer_base *base, unsigned tflags)static inline void forward_timer_base (struct timer_base *base) { }#endif -static inline struct timer_base * -get_target_base (struct timer_base *base,unsigned tflags) -{ -struct timer_base *target =__get_target_base (base, tflags); - -forward_timer_base (target); -return target; -}/* * We are using hashed locking: Holding per_cpu(timer_bases[x]).lock means
@@ -1037,6 +1031,9 @@ __mod_timer(struct timer_list *timer, unsigned long expires, bool pending_only) } } +/* Try to forward a stale timer base clock */ +forward_timer_base (base); + timer->expires = expires;/* * If 'idx' was calculated above and the base time did not advance
A message that is not in the archive (sent off-list or not yet received): CA+OSeukz5OSzHdezcUpL1hYj9FurYxu8Nwm5CToLjvyCNvesbQ@mail.gmail.com
Quoted 7 lines · Ashton Holmes
Quoted 5 lines · Thomas Gleixner
Several people have reported USB timeout issues starting in 4.8 which are related to the timer wheel overhaul.
The following two patches adress the brown paperbag bugs in that code.
Quoted 7 lines
Quoted 4 lines
Thanks,
tglx
Sorry it took me so long to reply. I've been rather busy. I've been running kernel 4.8.4 with these two patches for the past 2 days and have yet to encounter any issues. Thanks.
Thanks for confirmation and all the help with tracking this down!
tglx