* [PATCH v6 0/4] Align atomic storage
@ 2025-12-31 8:25 Finn Thain
2025-12-31 8:25 ` [PATCH v6 3/4] atomic: Add alignment check to instrumented atomic operations Finn Thain
0 siblings, 1 reply; 4+ messages in thread
From: Finn Thain @ 2025-12-31 8:25 UTC (permalink / raw)
To: Andrew Morton, Peter Zijlstra, Will Deacon
Cc: Andrii Nakryiko, Ard Biesheuvel, Arnd Bergmann,
Alexei Starovoitov, Boqun Feng, Borislav Petkov, bpf, Rich Felker,
Daniel Borkmann, Dave Hansen, Dinh Nguyen, Eduard Zingerman,
Gary Guo, Geert Uytterhoeven, John Paul Adrian Glaubitz, Guo Ren,
Hao Luo, H. Peter Anvin, John Fastabend, Jiri Olsa, Jonas Bonn,
KP Singh, linux-arch, linux-csky, linux-efi, linux-kernel,
linux-m68k, linux-openrisc, linux-sh, Mark Rutland,
Martin KaFai Lau, Ingo Molnar, Sasha Levin, Stanislav Fomichev,
Stafford Horne, Song Liu, Stefan Kristiansson, Thomas Gleixner,
x86, Yonghong Song, Yoshinori Sato
This series adds the __aligned attribute to atomic_t and atomic64_t
definitions in include/linux and include/asm-generic (respectively)
to get natural alignment of both types on csky, m68k, microblaze,
nios2, openrisc and sh.
This series also adds Kconfig options to enable a new run-time warning
to help reveal misaligned atomic accesses on platforms which don't
trap that.
The performance impact is expected to vary across platforms and workloads.
The measurements I made on m68k show that some workloads run faster and
others slower.
---
Changed since v5:
- Added acked-by and revewed-by tags.
- Added a new macro to inhibit emission of __bug_table section, for the
benefit of pre-boot code like the EFI stub loader.
Changed since v4:
- Dropped parisc header file patch as it's been merged already.
- Submitted as PATCH instead of RFC.
Changed since v3:
- Rebased on v6.17.
- New patch to resolve header dependency issue on parisc.
- Dropped documentation patch.
Changed since v2:
- Specify natural alignment for atomic64_t.
- CONFIG_DEBUG_ATOMIC checks for natural alignment again.
- New patch to add weakened alignment check.
- New patch for explicit alignment in BPF header.
---
Finn Thain (3):
bpf: Explicitly align bpf_res_spin_lock
atomic: Specify alignment for atomic_t and atomic64_t
atomic: Add option for weaker alignment check
Peter Zijlstra (1):
atomic: Add alignment check to instrumented atomic operations
arch/x86/boot/compressed/Makefile | 1 +
drivers/firmware/efi/libstub/Makefile | 1 +
include/asm-generic/atomic64.h | 2 +-
include/asm-generic/rqspinlock.h | 2 +-
include/linux/instrumented.h | 17 +++++++++++++++++
include/linux/types.h | 2 +-
kernel/bpf/rqspinlock.c | 1 -
lib/Kconfig.debug | 18 ++++++++++++++++++
8 files changed, 40 insertions(+), 4 deletions(-)
--
2.49.1
^ permalink raw reply [flat|nested] 4+ messages in thread
* [PATCH v6 3/4] atomic: Add alignment check to instrumented atomic operations
2025-12-31 8:25 [PATCH v6 0/4] Align atomic storage Finn Thain
@ 2025-12-31 8:25 ` Finn Thain
2026-01-05 16:06 ` Peter Zijlstra
0 siblings, 1 reply; 4+ messages in thread
From: Finn Thain @ 2025-12-31 8:25 UTC (permalink / raw)
To: Andrew Morton, Peter Zijlstra, Will Deacon
Cc: Arnd Bergmann, Boqun Feng, Gary Guo, Mark Rutland, linux-arch,
linux-kernel, linux-m68k, Sasha Levin, Thomas Gleixner,
Ingo Molnar, Borislav Petkov, Dave Hansen, x86, Ard Biesheuvel,
H. Peter Anvin, linux-efi
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_BUG_TABLE macro.]
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: x86@kernel.org
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 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.
---
arch/x86/boot/compressed/Makefile | 1 +
drivers/firmware/efi/libstub/Makefile | 1 +
include/linux/instrumented.h | 10 ++++++++++
lib/Kconfig.debug | 10 ++++++++++
4 files changed, 22 insertions(+)
diff --git a/arch/x86/boot/compressed/Makefile b/arch/x86/boot/compressed/Makefile
index 68f9d7a1683b..122967c80e48 100644
--- a/arch/x86/boot/compressed/Makefile
+++ b/arch/x86/boot/compressed/Makefile
@@ -42,6 +42,7 @@ KBUILD_CFLAGS += -Wno-microsoft-anon-tag
endif
KBUILD_CFLAGS += -Wno-pointer-sign
KBUILD_CFLAGS += -fno-asynchronous-unwind-tables
+KBUILD_CFLAGS += -D__DISABLE_BUG_TABLE
KBUILD_CFLAGS += -D__DISABLE_EXPORTS
# Disable relocation relaxation in case the link is not PIE.
KBUILD_CFLAGS += $(call cc-option,-Wa$(comma)-mrelax-relocations=no)
diff --git a/drivers/firmware/efi/libstub/Makefile b/drivers/firmware/efi/libstub/Makefile
index 7d15a85d579f..ac3e7c64aedb 100644
--- a/drivers/firmware/efi/libstub/Makefile
+++ b/drivers/firmware/efi/libstub/Makefile
@@ -42,6 +42,7 @@ KBUILD_CFLAGS := $(subst $(CC_FLAGS_FTRACE),,$(cflags-y)) \
-ffreestanding \
-fno-stack-protector \
$(call cc-option,-fno-addrsig) \
+ -D__DISABLE_BUG_TABLE \
-D__DISABLE_EXPORTS
#
diff --git a/include/linux/instrumented.h b/include/linux/instrumented.h
index 711a1f0d1a73..bcd1113b55a1 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>
@@ -67,6 +68,9 @@ static __always_inline void instrument_atomic_read(const volatile void *v, size_
{
kasan_check_read(v, size);
kcsan_check_atomic_read(v, size);
+#ifndef __DISABLE_BUG_TABLE
+ WARN_ON_ONCE(IS_ENABLED(CONFIG_DEBUG_ATOMIC) && ((unsigned long)v & (size - 1)));
+#endif
}
/**
@@ -81,6 +85,9 @@ static __always_inline void instrument_atomic_write(const volatile void *v, size
{
kasan_check_write(v, size);
kcsan_check_atomic_write(v, size);
+#ifndef __DISABLE_BUG_TABLE
+ WARN_ON_ONCE(IS_ENABLED(CONFIG_DEBUG_ATOMIC) && ((unsigned long)v & (size - 1)));
+#endif
}
/**
@@ -95,6 +102,9 @@ static __always_inline void instrument_atomic_read_write(const volatile void *v,
{
kasan_check_write(v, size);
kcsan_check_atomic_read_write(v, size);
+#ifndef __DISABLE_BUG_TABLE
+ WARN_ON_ONCE(IS_ENABLED(CONFIG_DEBUG_ATOMIC) && ((unsigned long)v & (size - 1)));
+#endif
}
/**
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
^ permalink raw reply related [flat|nested] 4+ messages in thread
* Re: [PATCH v6 3/4] atomic: Add alignment check to instrumented atomic operations
2025-12-31 8:25 ` [PATCH v6 3/4] atomic: Add alignment check to instrumented atomic operations Finn Thain
@ 2026-01-05 16:06 ` Peter Zijlstra
2026-01-05 21:22 ` Finn Thain
0 siblings, 1 reply; 4+ messages in thread
From: Peter Zijlstra @ 2026-01-05 16:06 UTC (permalink / raw)
To: Finn Thain
Cc: Andrew Morton, Will Deacon, Arnd Bergmann, Boqun Feng, Gary Guo,
Mark Rutland, linux-arch, linux-kernel, linux-m68k, Sasha Levin,
Thomas Gleixner, Ingo Molnar, Borislav Petkov, Dave Hansen, x86,
Ard Biesheuvel, H. Peter Anvin, linux-efi
On Wed, Dec 31, 2025 at 07:25:42PM +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_BUG_TABLE macro.]
>
> 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: x86@kernel.org
> 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 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'
Urgh, so why not simply use __DISABLE_EXPORTS, that's typically (ab)used
for these things?
Also, unless __DISABLE_BUG_TABLE goes live inside asm/bug.h and kills
all __bug_table emissions, its a misnomer.
Furthermore, that SEV thing is broken and needs to be fixed anyway, this
isn't helping it much. noinstr code should not be using instrumented
things to begin with.
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH v6 3/4] atomic: Add alignment check to instrumented atomic operations
2026-01-05 16:06 ` Peter Zijlstra
@ 2026-01-05 21:22 ` Finn Thain
0 siblings, 0 replies; 4+ messages in thread
From: Finn Thain @ 2026-01-05 21:22 UTC (permalink / raw)
To: Peter Zijlstra
Cc: Andrew Morton, Will Deacon, Arnd Bergmann, Boqun Feng, Gary Guo,
Mark Rutland, linux-arch, linux-kernel, linux-m68k, Sasha Levin,
Thomas Gleixner, Ingo Molnar, Borislav Petkov, Dave Hansen, x86,
Ard Biesheuvel, H. Peter Anvin, linux-efi
On Mon, 5 Jan 2026, Peter Zijlstra wrote:
> On Wed, Dec 31, 2025 at 07:25:42PM +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_BUG_TABLE macro.]
> >
> > 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: x86@kernel.org
> > 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 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'
>
> Urgh, so why not simply use __DISABLE_EXPORTS, that's typically (ab)used
> for these things?
>
OK, I'll change it back to __DISABLE_EXPORTS.
> Also, unless __DISABLE_BUG_TABLE goes live inside asm/bug.h and kills
> all __bug_table emissions, its a misnomer.
>
Yes, __DISABLE_BUG_TABLE is certainly a misnomer, since what it actually
does is to elide code that would emit bug table entries. I would argue
that this distinction is splitting hairs but it's moot.
> Furthermore, that SEV thing is broken and needs to be fixed anyway, this
> isn't helping it much. noinstr code should not be using instrumented
> things to begin with.
>
The problem is not confined to x86. I needed something that would work for
loongarch, arm, riscv etc.
Anyway, thanks for your review. I will make the necessary changes.
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2026-01-05 21:22 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-12-31 8:25 [PATCH v6 0/4] Align atomic storage Finn Thain
2025-12-31 8:25 ` [PATCH v6 3/4] atomic: Add alignment check to instrumented atomic operations Finn Thain
2026-01-05 16:06 ` Peter Zijlstra
2026-01-05 21:22 ` Finn Thain
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox