[PATCH v7 3/4] atomic: Add alignment check to instrumented atomic operations

Finn Thain posted 4 patches 3 weeks, 4 days ago
[PATCH v7 3/4] atomic: Add alignment check to instrumented atomic operations
Posted by Finn Thain 3 weeks, 4 days ago
From: Peter Zijlstra <peterz@infradead.org>

Add a Kconfig option for debug builds which logs a warning when an
instrumented atomic operation takes place that's misaligned.
Some platforms don't trap for this.

[ fthain: added __DISABLE_EXPORTS conditional and refactored as helper
function. ]

Cc: Sasha Levin <sashal@kernel.org>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Borislav Petkov <bp@alien8.de>
Cc: Dave Hansen <dave.hansen@linux.intel.com>
Cc: Ard Biesheuvel <ardb@kernel.org>
Cc: "H. Peter Anvin" <hpa@zytor.com>
Link: https://lore.kernel.org/lkml/20250901093600.GF4067720@noisy.programming.kicks-ass.net/
Link: https://lore.kernel.org/linux-next/df9fbd22-a648-ada4-fee0-68fe4325ff82@linux-m68k.org/
Signed-off-by: Finn Thain <fthain@linux-m68k.org>

---
Checkpatch.pl says...
ERROR: Missing Signed-off-by: line by nominal patch author 'Peter Ziljstra <peterz@infradead.org>'

---
Changed since v6:
 - Implemented helper function earlier in patch series, as requested by Ard.
 - Dropped __DISABLE_BUG_TABLE macro in favour of __DISABLE_EXPORTS, as
 requested by Peter.

Changed since v5:
 - Add new __DISABLE_BUG_TABLE macro to prevent a build failure on those
architectures which use atomics in pre-boot code like the EFI stub loader:

x86_64-linux-gnu-ld: error: unplaced orphan section `__bug_table' from `arch/x86/boot/compressed/sev-handle-vc.o'

Changed since v2:
 - Always check for natural alignment.
---
 include/linux/instrumented.h | 11 +++++++++++
 lib/Kconfig.debug            | 10 ++++++++++
 2 files changed, 21 insertions(+)

diff --git a/include/linux/instrumented.h b/include/linux/instrumented.h
index 711a1f0d1a73..e34b6a557e0a 100644
--- a/include/linux/instrumented.h
+++ b/include/linux/instrumented.h
@@ -7,6 +7,7 @@
 #ifndef _LINUX_INSTRUMENTED_H
 #define _LINUX_INSTRUMENTED_H
 
+#include <linux/bug.h>
 #include <linux/compiler.h>
 #include <linux/kasan-checks.h>
 #include <linux/kcsan-checks.h>
@@ -55,6 +56,13 @@ static __always_inline void instrument_read_write(const volatile void *v, size_t
 	kcsan_check_read_write(v, size);
 }
 
+static __always_inline void instrument_atomic_check_alignment(const volatile void *v, size_t size)
+{
+#ifndef __DISABLE_EXPORTS
+	WARN_ON_ONCE(IS_ENABLED(CONFIG_DEBUG_ATOMIC) && ((unsigned long)v & (size - 1)));
+#endif
+}
+
 /**
  * instrument_atomic_read - instrument atomic read access
  * @v: address of access
@@ -67,6 +75,7 @@ static __always_inline void instrument_atomic_read(const volatile void *v, size_
 {
 	kasan_check_read(v, size);
 	kcsan_check_atomic_read(v, size);
+	instrument_atomic_check_alignment(v, size);
 }
 
 /**
@@ -81,6 +90,7 @@ static __always_inline void instrument_atomic_write(const volatile void *v, size
 {
 	kasan_check_write(v, size);
 	kcsan_check_atomic_write(v, size);
+	instrument_atomic_check_alignment(v, size);
 }
 
 /**
@@ -95,6 +105,7 @@ static __always_inline void instrument_atomic_read_write(const volatile void *v,
 {
 	kasan_check_write(v, size);
 	kcsan_check_atomic_read_write(v, size);
+	instrument_atomic_check_alignment(v, size);
 }
 
 /**
diff --git a/lib/Kconfig.debug b/lib/Kconfig.debug
index ba36939fda79..4b4d1445ef9c 100644
--- a/lib/Kconfig.debug
+++ b/lib/Kconfig.debug
@@ -1359,6 +1359,16 @@ config DEBUG_PREEMPT
 	  depending on workload as it triggers debugging routines for each
 	  this_cpu operation. It should only be used for debugging purposes.
 
+config DEBUG_ATOMIC
+	bool "Debug atomic variables"
+	depends on DEBUG_KERNEL
+	help
+	  If you say Y here then the kernel will add a runtime alignment check
+	  to atomic accesses. Useful for architectures that do not have trap on
+	  mis-aligned access.
+
+	  This option has potentially significant overhead.
+
 menu "Lock Debugging (spinlocks, mutexes, etc...)"
 
 config LOCK_DEBUGGING_SUPPORT
-- 
2.49.1
Re: [PATCH v7 3/4] atomic: Add alignment check to instrumented atomic operations
Posted by Peter Zijlstra 3 weeks, 3 days ago
On Tue, Jan 13, 2026 at 04:22:28PM +1100, Finn Thain wrote:
> From: Peter Zijlstra <peterz@infradead.org>
> 
> Add a Kconfig option for debug builds which logs a warning when an
> instrumented atomic operation takes place that's misaligned.
> Some platforms don't trap for this.
> 
> [ fthain: added __DISABLE_EXPORTS conditional and refactored as helper
> function. ]
> 
> Cc: Sasha Levin <sashal@kernel.org>
> Cc: Thomas Gleixner <tglx@linutronix.de>
> Cc: Ingo Molnar <mingo@redhat.com>
> Cc: Borislav Petkov <bp@alien8.de>
> Cc: Dave Hansen <dave.hansen@linux.intel.com>
> Cc: Ard Biesheuvel <ardb@kernel.org>
> Cc: "H. Peter Anvin" <hpa@zytor.com>
> Link: https://lore.kernel.org/lkml/20250901093600.GF4067720@noisy.programming.kicks-ass.net/
> Link: https://lore.kernel.org/linux-next/df9fbd22-a648-ada4-fee0-68fe4325ff82@linux-m68k.org/
> Signed-off-by: Finn Thain <fthain@linux-m68k.org>
> 
> ---
> Checkpatch.pl says...
> ERROR: Missing Signed-off-by: line by nominal patch author 'Peter Ziljstra <peterz@infradead.org>'

Feel free to add:

Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Re: [PATCH v7 3/4] atomic: Add alignment check to instrumented atomic operations
Posted by Geert Uytterhoeven 3 weeks, 4 days ago
Hi Finn,

On Tue, 13 Jan 2026 at 06:39, Finn Thain <fthain@linux-m68k.org> wrote:
> From: Peter Zijlstra <peterz@infradead.org>
>
> Add a Kconfig option for debug builds which logs a warning when an
> instrumented atomic operation takes place that's misaligned.
> Some platforms don't trap for this.
>
> [ fthain: added __DISABLE_EXPORTS conditional and refactored as helper
> function. ]
>
> Cc: Sasha Levin <sashal@kernel.org>
> Cc: Thomas Gleixner <tglx@linutronix.de>
> Cc: Ingo Molnar <mingo@redhat.com>
> Cc: Borislav Petkov <bp@alien8.de>
> Cc: Dave Hansen <dave.hansen@linux.intel.com>
> Cc: Ard Biesheuvel <ardb@kernel.org>
> Cc: "H. Peter Anvin" <hpa@zytor.com>
> Link: https://lore.kernel.org/lkml/20250901093600.GF4067720@noisy.programming.kicks-ass.net/
> Link: https://lore.kernel.org/linux-next/df9fbd22-a648-ada4-fee0-68fe4325ff82@linux-m68k.org/
> Signed-off-by: Finn Thain <fthain@linux-m68k.org>
>
> ---
> Checkpatch.pl says...
> ERROR: Missing Signed-off-by: line by nominal patch author 'Peter Ziljstra <peterz@infradead.org>'

Alternatively, you can credit Peter using

    Suggested-by: Peter Zijlstra <peterz@infradead.org>

just before the Link-header pointing to his suggestion.

Gr{oetje,eeting}s,

                        Geert

-- 
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- geert@linux-m68k.org

In personal conversations with technical people, I call myself a hacker. But
when I'm talking to journalists I just say "programmer" or something like that.
                                -- Linus Torvalds
Re: [PATCH v7 3/4] atomic: Add alignment check to instrumented atomic operations
Posted by Finn Thain 3 weeks, 3 days ago
On Tue, 13 Jan 2026, Geert Uytterhoeven wrote:

> >
> > ---
> > Checkpatch.pl says...
> > ERROR: Missing Signed-off-by: line by nominal patch author 'Peter Ziljstra <peterz@infradead.org>'
> 
> Alternatively, you can credit Peter using
> 
>     Suggested-by: Peter Zijlstra <peterz@infradead.org>
> 
> just before the Link-header pointing to his suggestion.
> 

I'll leave that up to Peter as he's both author and maintainer.

If there was to be another revision, perhaps I can add --

    Suggested-by-suggested-by: Geert Uytterhoeven <geert@linux-m68k.org>

;-)
Re: [PATCH v7 3/4] atomic: Add alignment check to instrumented atomic operations
Posted by Andrew Morton 3 weeks, 2 days ago
On Wed, 14 Jan 2026 14:53:33 +1100 (AEDT) Finn Thain <fthain@linux-m68k.org> wrote:

> 
> On Tue, 13 Jan 2026, Geert Uytterhoeven wrote:
> 
> > >
> > > ---
> > > Checkpatch.pl says...
> > > ERROR: Missing Signed-off-by: line by nominal patch author 'Peter Ziljstra <peterz@infradead.org>'
> > 
> > Alternatively, you can credit Peter using
> > 
> >     Suggested-by: Peter Zijlstra <peterz@infradead.org>
> > 
> > just before the Link-header pointing to his suggestion.
> > 
> 
> I'll leave that up to Peter as he's both author and maintainer.

We have his signoff.

> If there was to be another revision, perhaps I can add --
> 
>     Suggested-by-suggested-by: Geert Uytterhoeven <geert@linux-m68k.org>

edited and added, thanks.