locking/atomic: add CONFIG_DEBUG_ATOMIC_ALIGNMENT

All of the Linux atomic APIs require that the memory location an atomic
is performed upon is a naturally-aligned location whose size is at most
the machine's native word size.

Attempting to perform an atomic operation on a misaligned location may
result in a number of outcomes, including but not limited to:

* An exception being raised.

* The atomic implementation succeeding, with a performance penalty.

* The atomic implementation succeeding without the expected atomicity
  guarantees.

* The atomic implementation livelocking.

This patch adds a new CONFIG_DEBUG_ATOMIC_ALIGNMENT option, which has
the atomic implementation check the alignment of memory locations before
attempting to perform an atomic operation, so that these cases can be
caught when hardware does not report this synchronously.

Note that include/asm-generic/atomic-instrumented.h is generated by
scripts/atomic/gen-atomic-instrumented.sh.

TODO:
* Move IS_ALIGNED() into !kernel.h so we can safely include it
* Have a PTR_IS_ALIGNED()?
* Hope it's safe to include <linux/bug.h> for WARN_ON().

Suggested-by: Peter Zijlstra <peterz@infradead.org>
Signed-off-by: Mark Rutland <mark.rutland@arm.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Will Deacon <will.deacon@arm.com>
3 files changed