Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

atomic: add alignment check to instrumented atomic operations

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@linux-m68k.org: added __DISABLE_EXPORTS conditional and refactored as helper function]
Link: https://lkml.kernel.org/r/51ebf844e006ca0de408f5d3a831e7b39d7fc31c.1768281748.git.fthain@linux-m68k.org
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>
Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Suggested-by: Geert Uytterhoeven <geert@linux-m68k.org>
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>
Cc: Alexei Starovoitov <ast@kernel.org>
Cc: Andrii Nakryiko <andrii@kernel.org>
Cc: Arnd Bergmann <arnd@arndb.de>
Cc: Boqun Feng <boqun.feng@gmail.com>
Cc: Daniel Borkman <daniel@iogearbox.net>
Cc: Dinh Nguyen <dinguyen@kernel.org>
Cc: Eduard Zingerman <eddyz87@gmail.com>
Cc: Gary Guo <gary@garyguo.net>
Cc: Guo Ren <guoren@kernel.org>
Cc: Hao Luo <haoluo@google.com>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: John Fastabend <john.fastabend@gmail.com>
Cc: John Paul Adrian Glaubitz <glaubitz@physik.fu-berlin.de>
Cc: Jonas Bonn <jonas@southpole.se>
Cc: KP Singh <kpsingh@kernel.org>
Cc: Marc Rutland <mark.rutland@arm.com>
Cc: Martin KaFai Lau <martin.lau@linux.dev>
Cc: Rich Felker <dalias@libc.org>
Cc: Song Liu <song@kernel.org>
Cc: Stafford Horne <shorne@gmail.com>
Cc: Stanislav Fomichev <sdf@fomichev.me>
Cc: Stefan Kristiansson <stefan.kristiansson@saunalahti.fi>
Cc: Will Deacon <will@kernel.org>
Cc: Yonghong Song <yonghong.song@linux.dev>
Cc: Yoshinori Sato <ysato@users.sourceforge.jp>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>

authored by

Peter Zijlstra and committed by
Andrew Morton
80047d84 e428b013

+21
+11
include/linux/instrumented.h
··· 7 7 #ifndef _LINUX_INSTRUMENTED_H 8 8 #define _LINUX_INSTRUMENTED_H 9 9 10 + #include <linux/bug.h> 10 11 #include <linux/compiler.h> 11 12 #include <linux/kasan-checks.h> 12 13 #include <linux/kcsan-checks.h> ··· 56 55 kcsan_check_read_write(v, size); 57 56 } 58 57 58 + static __always_inline void instrument_atomic_check_alignment(const volatile void *v, size_t size) 59 + { 60 + #ifndef __DISABLE_EXPORTS 61 + WARN_ON_ONCE(IS_ENABLED(CONFIG_DEBUG_ATOMIC) && ((unsigned long)v & (size - 1))); 62 + #endif 63 + } 64 + 59 65 /** 60 66 * instrument_atomic_read - instrument atomic read access 61 67 * @v: address of access ··· 75 67 { 76 68 kasan_check_read(v, size); 77 69 kcsan_check_atomic_read(v, size); 70 + instrument_atomic_check_alignment(v, size); 78 71 } 79 72 80 73 /** ··· 90 81 { 91 82 kasan_check_write(v, size); 92 83 kcsan_check_atomic_write(v, size); 84 + instrument_atomic_check_alignment(v, size); 93 85 } 94 86 95 87 /** ··· 105 95 { 106 96 kasan_check_write(v, size); 107 97 kcsan_check_atomic_read_write(v, size); 98 + instrument_atomic_check_alignment(v, size); 108 99 } 109 100 110 101 /**
+10
lib/Kconfig.debug
··· 1360 1360 depending on workload as it triggers debugging routines for each 1361 1361 this_cpu operation. It should only be used for debugging purposes. 1362 1362 1363 + config DEBUG_ATOMIC 1364 + bool "Debug atomic variables" 1365 + depends on DEBUG_KERNEL 1366 + help 1367 + If you say Y here then the kernel will add a runtime alignment check 1368 + to atomic accesses. Useful for architectures that do not have trap on 1369 + mis-aligned access. 1370 + 1371 + This option has potentially significant overhead. 1372 + 1363 1373 menu "Lock Debugging (spinlocks, mutexes, etc...)" 1364 1374 1365 1375 config LOCK_DEBUGGING_SUPPORT