timers: Move clearing of base::timer_running under base::lock
syzbot reported KCSAN data races vs. timer_base::timer_running being set to NULL without holding base::lock in expire_timers().
This looks innocent and most reads are clearly not problematic but for a non-RT kernel it's completely irrelevant whether the store happens before or after taking the lock. For an RT kernel moving the store under the lock requires an extra unlock/lock pair in the case that there is a waiter for the timer. But that's not the end of the world and definitely not worth the trouble of adding boatloads of comments and annotations to the code. Famous last words...
--- kernel/time/timer.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)
kernel/time/timer.c +4 -2
@@ -1263,8 +1263,10 @@ static inline void timer_base_unlock_expstatic void timer_sync_wait_running (struct timer_base *base) {if (atomic_read (&base->timer_waiters)) { +raw_spin_unlock_irq (&base->lock);spin_unlock (&base->expiry_lock);spin_lock (&base->expiry_lock); +raw_spin_lock_irq (&base->lock); } }
@@ -1448,14 +1450,14 @@ static void expire_timers(struct timer_bif (timer->flags &TIMER_IRQSAFE ) {raw_spin_unlock (&base->lock);call_timer_fn (timer, fn, baseclk); - base->running_timer =NULL ;raw_spin_lock (&base->lock); + base->running_timer =NULL ; }else {raw_spin_unlock_irq (&base->lock);call_timer_fn (timer, fn, baseclk); +raw_spin_lock_irq (&base->lock); base->running_timer =NULL ;timer_sync_wait_running (base); -raw_spin_lock_irq (&base->lock); } } }
Frederic Weisbecker
maintainerQuoted 10 lines · Thomas Gleixner
syzbot reported KCSAN data races vs.
timer_base::timer_runningbeing set to NULL without holdingbase::lockinexpire_timers().This looks innocent and most reads are clearly not problematic but for a non-RT kernel it's completely irrelevant whether the store happens before or after taking the lock. For an RT kernel moving the store under the lock requires an extra unlock/lock pair in the case that there is a waiter for the timer. But that's not the end of the world and definitely not worth the trouble of adding boatloads of comments and annotations to the code. Famous last words...
There is another thing I noticed lately wrt. del_timer_sync() VS timer execution:
int data = 0;
void timer_func(struct timer_list *t)
{
data = 1;
}
CPU 0 CPU 1
------------------------------ --------------------------
base = lock_timer_base(timer, &flags); raw_spin_unlock(&base->lock);
if (base->running_timer != timer) call_timer_fn(timer, fn, baseclk);
ret = detach_if_pending(timer, base, true); base->running_timer = NULL;
raw_spin_unlock_irqrestore(&base->lock, flags); raw_spin_lock(&base->lock);
x = data;Here if the timer has previously executed on CPU 1 and then CPU 0 sees base->running_timer == NULL, it will return, assuming the timer has completed. But there is nothing to enforce the fact that x will be equal to 1. Enforcing that is a behaviour I would expect in this case since this is a kind of "wait for completion" function. But perhaps it doesn't apply here, in fact I have no idea...
But if we recognize that as an issue, we would need a mirroring load_acquire()/store_release() on base->running_timer.
Quoted 14 lines · Frederic Weisbecker
Quoted 10 lines · Thomas Gleixner
syzbot reported KCSAN data races vs.
timer_base::timer_runningbeing set to NULL without holdingbase::lockinexpire_timers().This looks innocent and most reads are clearly not problematic but for a non-RT kernel it's completely irrelevant whether the store happens before or after taking the lock. For an RT kernel moving the store under the lock requires an extra unlock/lock pair in the case that there is a waiter for the timer. But that's not the end of the world and definitely not worth the trouble of adding boatloads of comments and annotations to the code. Famous last words...
There is another thing I noticed lately wrt.
del_timer_sync()VS timer execution:
Quoted 7 lines
Here if the timer has previously executed on CPU 1 and then CPU 0 sees
base->running_timer== NULL, it will return, assuming the timer has completed. But there is nothing to enforce the fact that x will be equal to 1. Enforcing that is a behaviour I would expect in this case since this is a kind of "wait for completion" function. But perhaps it doesn't apply here, in fact I have no idea...But if we recognize that as an issue, we would need a mirroring load_acquire()/store_release() on
base->running_timer.
Yeah, I think you're right. del_timer_sync() explicitly states it waits for completion of the handler, so it isn't weird to then also expect to be able to observe the results of the handler.
Thomas' patch fixes this.
Frederic Weisbecker
maintainerQuoted 20 lines · Peter Zijlstra
Quoted 19 lines · Frederic Weisbecker
Quoted 10 lines · Thomas Gleixner
syzbot reported KCSAN data races vs.
timer_base::timer_runningbeing set to NULL without holdingbase::lockinexpire_timers().This looks innocent and most reads are clearly not problematic but for a non-RT kernel it's completely irrelevant whether the store happens before or after taking the lock. For an RT kernel moving the store under the lock requires an extra unlock/lock pair in the case that there is a waiter for the timer. But that's not the end of the world and definitely not worth the trouble of adding boatloads of comments and annotations to the code. Famous last words...
There is another thing I noticed lately wrt.
del_timer_sync()VS timer execution:Here if the timer has previously executed on CPU 1 and then CPU 0 sees
base->running_timer== NULL, it will return, assuming the timer has completed. But there is nothing to enforce the fact that x will be equal to 1. Enforcing that is a behaviour I would expect in this case since this is a kind of "wait for completion" function. But perhaps it doesn't apply here, in fact I have no idea...
Quoted 8 lines
Quoted 2 lines
But if we recognize that as an issue, we would need a mirroring load_acquire()/store_release() on
base->running_timer.Yeah, I think you're right.
del_timer_sync()explicitly states it waits for completion of the handler, so it isn't weird to then also expect to be able to observe the results of the handler.Thomas' patch fixes this.
Indeed!
Thanks.
Quoted 11 lines · Thomas Gleixner
syzbot reported KCSAN data races vs.
timer_base::timer_runningbeing set to NULL without holdingbase::lockinexpire_timers().This looks innocent and most reads are clearly not problematic but for a non-RT kernel it's completely irrelevant whether the store happens before or after taking the lock. For an RT kernel moving the store under the lock requires an extra unlock/lock pair in the case that there is a waiter for the timer. But that's not the end of the world and definitely not worth the trouble of adding boatloads of comments and annotations to the code. Famous last words...
Quoted 3 lines
Reported-by [email protected]Reported-by [email protected]Signed-off-by Thomas Gleixner <[email protected]>
One thing I noticed while testing it is that the "corner" case in timer_sync_wait_running() is quite reliably hit by rcu_preempt rcu_gp_fqs_loop() -> swait_event_idle_timeout_exclusive() invocation.
Sebastian
Quoted 12 lines · Sebastian Andrzej Siewior
Quoted 11 lines · Thomas Gleixner
syzbot reported KCSAN data races vs.
timer_base::timer_runningbeing set to NULL without holdingbase::lockinexpire_timers().This looks innocent and most reads are clearly not problematic but for a non-RT kernel it's completely irrelevant whether the store happens before or after taking the lock. For an RT kernel moving the store under the lock requires an extra unlock/lock pair in the case that there is a waiter for the timer. But that's not the end of the world and definitely not worth the trouble of adding boatloads of comments and annotations to the code. Famous last words...
Quoted 7 lines
Quoted 3 lines
Reported-by [email protected]Reported-by [email protected]Signed-off-by Thomas Gleixner <[email protected]>One thing I noticed while testing it is that the "corner" case in
timer_sync_wait_running()is quite reliably hit byrcu_preemptrcu_gp_fqs_loop()->swait_event_idle_timeout_exclusive()invocation.
I assume it's something like this:
timeout -> wakeup
->preemption
del_timer_sync()
.....Thanks,
tglx
Quoted 5 lines · Thomas Gleixner
Quoted 3 lines · Sebastian Andrzej Siewior
One thing I noticed while testing it is that the "corner" case in
timer_sync_wait_running()is quite reliably hit byrcu_preemptrcu_gp_fqs_loop()->swait_event_idle_timeout_exclusive()invocation.
Quoted 7 lines
I assume it's something like this:
timeout -> wakeup->preemption
del_timer_sync() .....
Yes, but it triggers frequently. Like `rcuc' is somehow is aligned with the timeout.
| <idle>-0 [007] dN.h4.. 46.299705: sched_wakeup: comm=rcuc/7 pid=53 prio=98 target_cpu=007 | <idle>-0 [007] d...2.. 46.299728: sched_switch: prev_comm=swapper/7 prev_pid=0 prev_prio=120 prev_state=R ==> next_comm=rcuc/7 next_pid=53 next_prio=98 | rcuc/7-53 [007] d...2.. 46.299742: sched_switch: prev_comm=rcuc/7 prev_pid=53 prev_prio=98 prev_state=S ==> next_comm=ksoftirqd/7 next_pid=54 next_prio=120 | ksoftirqd/7-54 [007] .....13 46.299750: timer_expire_entry: timer=000000003bd1e045 function=process_timeout now=4294903802 baseclk=4294903802 | ksoftirqd/7-54 [007] d...213 46.299750: sched_waking: comm=rcu_preempt pid=11 prio=98 target_cpu=007 | ksoftirqd/7-54 [007] dN..313 46.299754: sched_wakeup: comm=rcu_preempt pid=11 prio=98 target_cpu=007 | ksoftirqd/7-54 [007] dN..213 46.299756: sched_stat_runtime: comm=ksoftirqd/7 pid=54 runtime=13265 [ns] vruntime=3012610540 [ns] | ksoftirqd/7-54 [007] d...213 46.299760: sched_switch: prev_comm=ksoftirqd/7 prev_pid=54 prev_prio=120 prev_state=R+ ==> next_comm=rcu_preempt next_pid=11 next_prio=98 | rcu_preempt-11 [007] d...311 46.299766: sched_pi_setprio: comm=ksoftirqd/7 pid=54 oldprio=120 newprio=98 del_timer_sync() | rcu_preempt-11 [007] d...211 46.299773: sched_switch: prev_comm=rcu_preempt prev_pid=11 prev_prio=98 prev_state=R+ ==> next_comm=ksoftirqd/7 next_pid=54 next_prio=98 | ksoftirqd/7-54 [007] .....13 46.299774: timer_expire_exit: timer=000000003bd1e045 | ksoftirqd/7-54 [007] dN..311 46.299784: sched_pi_setprio: comm=ksoftirqd/7 pid=54 oldprio=98 newprio=120 | ksoftirqd/7-54 [007] dN..311 46.299788: sched_waking: comm=rcu_preempt pid=11 prio=98 target_cpu=007 | ksoftirqd/7-54 [007] dN..411 46.299790: sched_wakeup: comm=rcu_preempt pid=11 prio=98 target_cpu=007 | ksoftirqd/7-54 [007] dN..311 46.299792: sched_stat_runtime: comm=ksoftirqd/7 pid=54 runtime=7404 [ns] vruntime=3012617944 [ns] | ksoftirqd/7-54 [007] d...2.. 46.299797: sched_switch: prev_comm=ksoftirqd/7 prev_pid=54 prev_prio=120 prev_state=S ==> next_comm=rcu_preempt next_pid=11 next_prio=98
Quoted 3 lines
Thanks,
tglx
Sebastian
Quoted 8 lines · Sebastian Andrzej Siewior
Quoted 7 lines · Thomas Gleixner
Quoted 3 lines · Sebastian Andrzej Siewior
One thing I noticed while testing it is that the "corner" case in
timer_sync_wait_running()is quite reliably hit byrcu_preemptrcu_gp_fqs_loop()->swait_event_idle_timeout_exclusive()invocation.I assume it's something like this:
Quoted 8 lines
Quoted 5 lines
timeout -> wakeup->preemption
del_timer_sync() .....Yes, but it triggers frequently. Like `rcuc' is somehow is aligned with the timeout.
Given that a lot of RCU processing is event-driven based on timers, and given that the scheduling-clock interrupts are synchronized for energy-efficiency reasons on many configs, maybe this alignment is expected behavior?
Thanx, Paul
Quoted 24 lines
| <idle>-0 [007] dN.h4.. 46.299705: sched_wakeup: comm=rcuc/7 pid=53 prio=98 target_cpu=007 | <idle>-0 [007] d...2.. 46.299728: sched_switch: prev_comm=swapper/7 prev_pid=0 prev_prio=120 prev_state=R ==> next_comm=rcuc/7 next_pid=53 next_prio=98 | rcuc/7-53 [007] d...2.. 46.299742: sched_switch: prev_comm=rcuc/7 prev_pid=53 prev_prio=98 prev_state=S ==> next_comm=ksoftirqd/7 next_pid=54 next_prio=120 | ksoftirqd/7-54 [007] .....13 46.299750: timer_expire_entry: timer=000000003bd1e045 function=process_timeout now=4294903802 baseclk=4294903802 | ksoftirqd/7-54 [007] d...213 46.299750: sched_waking: comm=rcu_preempt pid=11 prio=98 target_cpu=007 | ksoftirqd/7-54 [007] dN..313 46.299754: sched_wakeup: comm=rcu_preempt pid=11 prio=98 target_cpu=007 | ksoftirqd/7-54 [007] dN..213 46.299756: sched_stat_runtime: comm=ksoftirqd/7 pid=54 runtime=13265 [ns] vruntime=3012610540 [ns] | ksoftirqd/7-54 [007] d...213 46.299760: sched_switch: prev_comm=ksoftirqd/7 prev_pid=54 prev_prio=120 prev_state=R+ ==> next_comm=rcu_preempt next_pid=11 next_prio=98 | rcu_preempt-11 [007] d...311 46.299766: sched_pi_setprio: comm=ksoftirqd/7 pid=54 oldprio=120 newprio=98 del_timer_sync() | rcu_preempt-11 [007] d...211 46.299773: sched_switch: prev_comm=rcu_preempt prev_pid=11 prev_prio=98 prev_state=R+ ==> next_comm=ksoftirqd/7 next_pid=54 next_prio=98 | ksoftirqd/7-54 [007] .....13 46.299774: timer_expire_exit: timer=000000003bd1e045 | ksoftirqd/7-54 [007] dN..311 46.299784: sched_pi_setprio: comm=ksoftirqd/7 pid=54 oldprio=98 newprio=120 | ksoftirqd/7-54 [007] dN..311 46.299788: sched_waking: comm=rcu_preempt pid=11 prio=98 target_cpu=007 | ksoftirqd/7-54 [007] dN..411 46.299790: sched_wakeup: comm=rcu_preempt pid=11 prio=98 target_cpu=007 | ksoftirqd/7-54 [007] dN..311 46.299792: sched_stat_runtime: comm=ksoftirqd/7 pid=54 runtime=7404 [ns] vruntime=3012617944 [ns] | ksoftirqd/7-54 [007] d...2.. 46.299797: sched_switch: prev_comm=ksoftirqd/7 prev_pid=54 prev_prio=120 prev_state=S ==> next_comm=rcu_preempt next_pid=11 next_prio=98Quoted 3 lines
Thanks,
tglx
Sebastian
Quoted 7 lines · Paul E. McKenney
Quoted 2 lines
Yes, but it triggers frequently. Like `rcuc' is somehow is aligned with the timeout.
Given that a lot of RCU processing is event-driven based on timers, and given that the scheduling-clock interrupts are synchronized for energy-efficiency reasons on many configs, maybe this alignment is expected behavior?
No, it is the fact that rcu_preempt has a higher priority than ksoftirqd. So immediately after the wakeup (of rcu_preempt) there is a context switch and expire_timers() has this:
| raw_spin_unlock_irq(&base->lock); | call_timer_fn(timer, fn, baseclk); | raw_spin_lock_irq(&base->lock); | base->running_timer = NULL; | timer_sync_wait_running(base);
So ->running_timer isn't reset and try_to_del_timer_sync() (that del_timer_sync() from schedule_timeout()) returns -1 and then the corner case is handled where `expiry_lock' is acquired. So everything goes as expected.
Quoted 1 line
Thanx, Paul
Sebastian
Quoted 19 lines · Sebastian Andrzej Siewior
Quoted 7 lines · Paul E. McKenney
Quoted 2 lines
Yes, but it triggers frequently. Like `rcuc' is somehow is aligned with the timeout.
Given that a lot of RCU processing is event-driven based on timers, and given that the scheduling-clock interrupts are synchronized for energy-efficiency reasons on many configs, maybe this alignment is expected behavior?
No, it is the fact that
rcu_preempthas a higher priority than ksoftirqd. So immediately after the wakeup (ofrcu_preempt) there is a context switch andexpire_timers()has this:| raw_spin_unlock_irq(&base->lock); | call_timer_fn(timer, fn, baseclk); | raw_spin_lock_irq(&base->lock); | base->running_timer = NULL; | timer_sync_wait_running(base);
Quoted 4 lines
So ->running_timer isn't reset and
try_to_del_timer_sync()(thatdel_timer_sync()fromschedule_timeout()) returns -1 and then the corner case is handled where `expiry_lock' is acquired. So everything goes as expected.
Makes sense! Thank you for the explanation!
Thanx, Paul
Quoted 19 lines · Sebastian Andrzej Siewior
Quoted 7 lines · Paul E. McKenney
Quoted 2 lines
Yes, but it triggers frequently. Like `rcuc' is somehow is aligned with the timeout.
Given that a lot of RCU processing is event-driven based on timers, and given that the scheduling-clock interrupts are synchronized for energy-efficiency reasons on many configs, maybe this alignment is expected behavior?
No, it is the fact that
rcu_preempthas a higher priority than ksoftirqd. So immediately after the wakeup (ofrcu_preempt) there is a context switch andexpire_timers()has this:| raw_spin_unlock_irq(&base->lock); | call_timer_fn(timer, fn, baseclk); | raw_spin_lock_irq(&base->lock); | base->running_timer = NULL; | timer_sync_wait_running(base);
Quoted 4 lines
So ->running_timer isn't reset and
try_to_del_timer_sync()(thatdel_timer_sync()fromschedule_timeout()) returns -1 and then the corner case is handled where `expiry_lock' is acquired. So everything goes as expected.
Well, but even without that change you have the same situation:
timer_fn()
wakeup()
-->preemption
del_timer_sync()
if (running)
wait_for_running()
lock(expiry)
running = NULL
sync_wait_running()
unlock(expiry)
wakeup_lock()
-->preemption
...
lock(base)So the change at hand does not make things worse, right?
Thanks,
tglx
Quoted 1 line · Thomas Gleixner
So the change at hand does not make things worse, right?
Correct. This *ping*-*pong* was there before this patch because ->running_timer is always cleared after the callback finishes.
I was just curious why out of a sudden there are *that* many users of the corner case.
Quoted 3 lines
Thanks,
tglx
Sebastian
[tip: timers/urgent] timers: Move clearing of base::timer_running under base:: Lock
In reply to Thomas GleixnerThe following commit has been merged into the timers/urgent branch of tip:
The following commit has been merged into the timers/urgent branch of tip:
Commit-ID: bb7262b295472eb6858b5c49893954794027cd84 Gitweb: https://git.kernel.org/tip/bb7262b295472eb6858b5c49893954794027cd84 Author: Thomas Gleixner <[email protected]> AuthorDate: Sun, 06 Dec 2020 22:40:07 +01:00 Committer: Thomas Gleixner <[email protected]> CommitterDate: Tue, 27 Jul 2021 20:57:44 +02:00
timers: Move clearing of base::timer_running under base:: Lock
syzbot reported KCSAN data races vs. timer_base::timer_running being set to NULL without holding base::lock in expire_timers().
This looks innocent and most reads are clearly not problematic, but Frederic identified an issue which is:
int data = 0;
void timer_func(struct timer_list *t)
{
data = 1;}
CPU 0 CPU 1 ------------------------------ -------------------------- base = lock_timer_base(timer, &flags); raw_spin_unlock(&base->lock); if (base->running_timer != timer) call_timer_fn(timer, fn, baseclk); ret = detach_if_pending(timer, base, true); base->running_timer = NULL; raw_spin_unlock_irqrestore(&base->lock, flags); raw_spin_lock(&base->lock);
x = data;
If the timer has previously executed on CPU 1 and then CPU 0 can observe base->running_timer == NULL and returns, assuming the timer has completed, but it's not guaranteed on all architectures. The comment for del_timer_sync() makes that guarantee. Moving the assignment under base->lock prevents this.
For non-RT kernel it's performance wise completely irrelevant whether the store happens before or after taking the lock. For an RT kernel moving the store under the lock requires an extra unlock/lock pair in the case that there is a waiter for the timer, but that's not the end of the world.
--- kernel/time/timer.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)
kernel/time/timer.c +4 -2
@@ -1265,8 +1265,10 @@ static inline void timer_base_unlock_expiry(struct timer_base *base)static void timer_sync_wait_running (struct timer_base *base) {if (atomic_read (&base->timer_waiters)) { +raw_spin_unlock_irq (&base->lock);spin_unlock (&base->expiry_lock);spin_lock (&base->expiry_lock); +raw_spin_lock_irq (&base->lock); } }
@@ -1457,14 +1459,14 @@ static void expire_timers(struct timer_base *base, struct hlist_head *head)if (timer->flags &TIMER_IRQSAFE ) {raw_spin_unlock (&base->lock);call_timer_fn (timer, fn, baseclk); - base->running_timer =NULL ;raw_spin_lock (&base->lock); + base->running_timer =NULL ; }else {raw_spin_unlock_irq (&base->lock);call_timer_fn (timer, fn, baseclk); +raw_spin_lock_irq (&base->lock); base->running_timer =NULL ;timer_sync_wait_running (base); -raw_spin_lock_irq (&base->lock); } } }