Re: [PATCH] spinlock_debug: Fix various data races

From: Ingo Molnar
Date: Thu Nov 21 2019 - 13:33:03 EST



* Marco Elver <elver@xxxxxxxxxx> wrote:

> static inline void debug_spin_lock_after(raw_spinlock_t *lock)
> {
> - lock->owner_cpu = raw_smp_processor_id();
> - lock->owner = current;
> + WRITE_ONCE(lock->owner_cpu, raw_smp_processor_id());
> + WRITE_ONCE(lock->owner, current);
> }

debug_spin_lock_after() runs inside the spinlock itself - why do these
writes have to be WRITE_ONCE()?

> @@ -197,8 +197,8 @@ static inline void debug_write_unlock(rwlock_t *lock)
> RWLOCK_BUG_ON(lock->owner != current, lock, "wrong owner");
> RWLOCK_BUG_ON(lock->owner_cpu != raw_smp_processor_id(),
> lock, "wrong CPU");
> - lock->owner = SPINLOCK_OWNER_INIT;
> - lock->owner_cpu = -1;
> + WRITE_ONCE(lock->owner, SPINLOCK_OWNER_INIT);
> + WRITE_ONCE(lock->owner_cpu, -1);
> }

This too is running inside the critical section of the spinlock - why are
the WRITE_ONCE() calls necessary?

Thanks,

Ingo