Re: [PATCH] locking/spinlock/debug: snapshot lock fields

From: Peter Zijlstra
Date: Mon Oct 23 2017 - 09:46:35 EST


On Mon, Oct 23, 2017 at 01:29:10PM +0100, Mark Rutland wrote:
> Currently, the lock debug code doesn't use {READ,WRITE}_ONCE() to access
> lock fields, and thus may observe torn values given a suitably unhelpful
> compiler. These could result in false positives and/or false negatives
> for some sanity checks.
>
> Further, as we don't snapshot the values of various fields, these might
> change between the time they are sanity checked and the time they are
> logged, making debugging difficult.
>
> This patch ensures that lock fields are accessed with
> {READ,WRITE}_ONCE(), and uses a snapshot of the lock to ensure that
> logged values are the same as those used for the sanity checks.
>
> Signed-off-by: Mark Rutland <mark.rutland@xxxxxxx>
> Cc: Peter Zijlstra <peterz@xxxxxxxxxxxxx>
> Cc: Ingo Molnar <mingo@xxxxxxxxxx>

ACK, I'll try and remember to look at this if there's anything further
we can remove from this file.

> +static inline raw_spinlock_t
> +debug_spin_lock_snapshot(raw_spinlock_t *lockp)
> +{
> + raw_spinlock_t lock;
> +
> + lock.raw_lock = READ_ONCE(lockp->raw_lock);

I think you want to make that an smp_load_acquire(), such that we
preserve the release-acquire relation for the lock.

Since this happens _before_ the regular lock, but debug_spin_unlock()
also happens before the regular unlock. So the unlock release is still
placed right. But we've messed up our acquire.

> + lock.magic = READ_ONCE(lockp->magic);
> + lock.owner_cpu = READ_ONCE(lockp->owner_cpu);
> + lock.owner = READ_ONCE(lockp->owner);
> +
> + return lock;
> +}