Skip to content

Commit 80047d8

Browse files
Peter Zijlstraakpm00
authored andcommitted
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. [[email protected]: 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/[email protected]/ Link: https://lore.kernel.org/linux-next/[email protected]/ Signed-off-by: Finn Thain <[email protected]> Signed-off-by: Peter Zijlstra (Intel) <[email protected]> Suggested-by: Geert Uytterhoeven <[email protected]> Cc: Sasha Levin <[email protected]> Cc: Thomas Gleixner <[email protected]> Cc: Ingo Molnar <[email protected]> Cc: Borislav Petkov <[email protected]> Cc: Dave Hansen <[email protected]> Cc: Ard Biesheuvel <[email protected]> Cc: "H. Peter Anvin" <[email protected]> Cc: Alexei Starovoitov <[email protected]> Cc: Andrii Nakryiko <[email protected]> Cc: Arnd Bergmann <[email protected]> Cc: Boqun Feng <[email protected]> Cc: Daniel Borkman <[email protected]> Cc: Dinh Nguyen <[email protected]> Cc: Eduard Zingerman <[email protected]> Cc: Gary Guo <[email protected]> Cc: Guo Ren <[email protected]> Cc: Hao Luo <[email protected]> Cc: Jiri Olsa <[email protected]> Cc: John Fastabend <[email protected]> Cc: John Paul Adrian Glaubitz <[email protected]> Cc: Jonas Bonn <[email protected]> Cc: KP Singh <[email protected]> Cc: Marc Rutland <[email protected]> Cc: Martin KaFai Lau <[email protected]> Cc: Rich Felker <[email protected]> Cc: Song Liu <[email protected]> Cc: Stafford Horne <[email protected]> Cc: Stanislav Fomichev <[email protected]> Cc: Stefan Kristiansson <[email protected]> Cc: Will Deacon <[email protected]> Cc: Yonghong Song <[email protected]> Cc: Yoshinori Sato <[email protected]> Signed-off-by: Andrew Morton <[email protected]>
1 parent e428b01 commit 80047d8

2 files changed

Lines changed: 21 additions & 0 deletions

File tree

include/linux/instrumented.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#ifndef _LINUX_INSTRUMENTED_H
88
#define _LINUX_INSTRUMENTED_H
99

10+
#include <linux/bug.h>
1011
#include <linux/compiler.h>
1112
#include <linux/kasan-checks.h>
1213
#include <linux/kcsan-checks.h>
@@ -55,6 +56,13 @@ static __always_inline void instrument_read_write(const volatile void *v, size_t
5556
kcsan_check_read_write(v, size);
5657
}
5758

59+
static __always_inline void instrument_atomic_check_alignment(const volatile void *v, size_t size)
60+
{
61+
#ifndef __DISABLE_EXPORTS
62+
WARN_ON_ONCE(IS_ENABLED(CONFIG_DEBUG_ATOMIC) && ((unsigned long)v & (size - 1)));
63+
#endif
64+
}
65+
5866
/**
5967
* instrument_atomic_read - instrument atomic read access
6068
* @v: address of access
@@ -67,6 +75,7 @@ static __always_inline void instrument_atomic_read(const volatile void *v, size_
6775
{
6876
kasan_check_read(v, size);
6977
kcsan_check_atomic_read(v, size);
78+
instrument_atomic_check_alignment(v, size);
7079
}
7180

7281
/**
@@ -81,6 +90,7 @@ static __always_inline void instrument_atomic_write(const volatile void *v, size
8190
{
8291
kasan_check_write(v, size);
8392
kcsan_check_atomic_write(v, size);
93+
instrument_atomic_check_alignment(v, size);
8494
}
8595

8696
/**
@@ -95,6 +105,7 @@ static __always_inline void instrument_atomic_read_write(const volatile void *v,
95105
{
96106
kasan_check_write(v, size);
97107
kcsan_check_atomic_read_write(v, size);
108+
instrument_atomic_check_alignment(v, size);
98109
}
99110

100111
/**

lib/Kconfig.debug

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1360,6 +1360,16 @@ config DEBUG_PREEMPT
13601360
depending on workload as it triggers debugging routines for each
13611361
this_cpu operation. It should only be used for debugging purposes.
13621362

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+
13631373
menu "Lock Debugging (spinlocks, mutexes, etc...)"
13641374

13651375
config LOCK_DEBUGGING_SUPPORT

0 commit comments

Comments
 (0)