Skip to content

Commit ff229ee

Browse files
edumazetKAGA-KOKO
authored andcommitted
hrtimer: Annotate lockless access to timer->base
Followup to commit dd2261e ("hrtimer: Protect lockless access to timer->base") lock_hrtimer_base() fetches timer->base without lock exclusion. Compiler is allowed to read timer->base twice (even if considered dumb) which could end up trying to lock migration_base and return &migration_base. base = timer->base; if (likely(base != &migration_base)) { /* compiler reads timer->base again, and now (base == &migration_base) raw_spin_lock_irqsave(&base->cpu_base->lock, *flags); if (likely(base == timer->base)) return base; /* == &migration_base ! */ Similarly the write sides must use WRITE_ONCE() to avoid store tearing. Signed-off-by: Eric Dumazet <[email protected]> Signed-off-by: Thomas Gleixner <[email protected]> Link: https://lkml.kernel.org/r/[email protected]
1 parent 4f5cafb commit ff229ee

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

kernel/time/hrtimer.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ struct hrtimer_clock_base *lock_hrtimer_base(const struct hrtimer *timer,
164164
struct hrtimer_clock_base *base;
165165

166166
for (;;) {
167-
base = timer->base;
167+
base = READ_ONCE(timer->base);
168168
if (likely(base != &migration_base)) {
169169
raw_spin_lock_irqsave(&base->cpu_base->lock, *flags);
170170
if (likely(base == timer->base))
@@ -244,7 +244,7 @@ switch_hrtimer_base(struct hrtimer *timer, struct hrtimer_clock_base *base,
244244
return base;
245245

246246
/* See the comment in lock_hrtimer_base() */
247-
timer->base = &migration_base;
247+
WRITE_ONCE(timer->base, &migration_base);
248248
raw_spin_unlock(&base->cpu_base->lock);
249249
raw_spin_lock(&new_base->cpu_base->lock);
250250

@@ -253,10 +253,10 @@ switch_hrtimer_base(struct hrtimer *timer, struct hrtimer_clock_base *base,
253253
raw_spin_unlock(&new_base->cpu_base->lock);
254254
raw_spin_lock(&base->cpu_base->lock);
255255
new_cpu_base = this_cpu_base;
256-
timer->base = base;
256+
WRITE_ONCE(timer->base, base);
257257
goto again;
258258
}
259-
timer->base = new_base;
259+
WRITE_ONCE(timer->base, new_base);
260260
} else {
261261
if (new_cpu_base != this_cpu_base &&
262262
hrtimer_check_target(timer, new_base)) {

0 commit comments

Comments
 (0)