Commit bb7262b
committed
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.
Reported-by: [email protected]
Reported-by: [email protected]
Fixes: 030dcdd ("timers: Prepare support for PREEMPT_RT")
Signed-off-by: Thomas Gleixner <[email protected]>
Tested-by: Sebastian Andrzej Siewior <[email protected]>
Link: https://lore.kernel.org/r/[email protected]
Cc: [email protected]1 parent ff11764 commit bb7262b
1 file changed
+4
-2
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1265 | 1265 | | |
1266 | 1266 | | |
1267 | 1267 | | |
| 1268 | + | |
1268 | 1269 | | |
1269 | 1270 | | |
| 1271 | + | |
1270 | 1272 | | |
1271 | 1273 | | |
1272 | 1274 | | |
| |||
1457 | 1459 | | |
1458 | 1460 | | |
1459 | 1461 | | |
1460 | | - | |
1461 | 1462 | | |
| 1463 | + | |
1462 | 1464 | | |
1463 | 1465 | | |
1464 | 1466 | | |
| 1467 | + | |
1465 | 1468 | | |
1466 | 1469 | | |
1467 | | - | |
1468 | 1470 | | |
1469 | 1471 | | |
1470 | 1472 | | |
| |||
0 commit comments