compiler: Simplify generic RELOC_HIDE()
Subsystems (3)
Marco Elver
maintainerPATCH 1/1 compiler: Simplify generic RELOC_HIDE()
When enabling Context Analysis (CONTEXT_ANALYSIS := y) in arch/x86/kvm code, Clang's Thread Safety Analysis failed to recognize that identical per_cpu() accesses refer to the same lock:
| CC [M] arch/x86/kvm/vmx/posted_intr.o | arch/x86/kvm/vmx/posted_intr.c:186:2: error: releasing raw_spinlock '__ptr + __per_cpu_offset[vcpu->cpu]' that was not held [-Werror,-Wthread-safety-analysis] | 186 | raw_spin_unlock(&per_cpu(wakeup_vcpus_on_cpu_lock, vcpu->cpu)); | | ^ | ./include/linux/spinlock.h:276:32: note: expanded from macro 'raw_spin_unlock' | 276 | #define raw_spin_unlock(lock) _raw_spin_unlock(lock) | | ^ | arch/x86/kvm/vmx/posted_intr.c:207:1: error: raw_spinlock '__ptr + __per_cpu_offset[vcpu->cpu]' is still held at the end of function [-Werror,-Wthread-safety-analysis] | 207 | } | | ^ | arch/x86/kvm/vmx/posted_intr.c:182:2: note: raw_spinlock acquired here | 182 | raw_spin_lock_nested(&per_cpu(wakeup_vcpus_on_cpu_lock, vcpu->cpu), | | ^ | ./include/linux/spinlock.h:235:2: note: expanded from macro 'raw_spin_lock_nested' | 235 | _raw_spin_lock(((void)(subclass), (lock))) | | ^ | 2 errors generated.
This occurred because the default RELOC_HIDE() implementation (used by the per-CPU macros) is a statement expression containing an intermediate 'unsigned long' variable (this version appears to predate Git history).
While the analysis strips away inner casts when resolving pointer aliases, it stops when encountering intermediate non-pointer variables (this is Thread Safety Analysis specific and irrelevant for codegen). This prevents the analysis from concluding that the pointers passed to e.g. raw_spin_lock() and raw_spin_unlock() were identical when per-CPU accessors are used.
Simplify RELOC_HIDE() to a single expression. This preserves the intent of obfuscating UB-introducing out-of-bounds pointer calculations from the compiler via the 'unsigned long' cast, but allows the alias analysis to successfully resolve the pointers.
Using a recent Clang version, I observe that generated code remains the same for vmlinux; the intermediate variable was already being optimized away (for any respectable modern compiler, not doing so would be an optimizer bug). Note that GCC provides its own version of RELOC_HIDE(), so this change only affects Clang builds.
Add a test case to lib/test_context-analysis.c to catch any regressions.
--- include/linux/compiler.h | 5 +---- lib/test_context-analysis.c | 11 +++++++++++ 2 files changed, 12 insertions(+), 4 deletions(-)
include/linux/compiler.h +1 -4
@@ -149,10 +149,7 @@ void ftrace_likely_update(struct ftrace_likely_data *f, int val,#endif #ifndef RELOC_HIDE -# define RELOC_HIDE (ptr, off) \ - ({ unsigned long __ptr; \ - __ptr = (unsigned long) (ptr); \ - (typeof(ptr)) (__ptr + (off)); }) +# define RELOC_HIDE (ptr, off) ((typeof(ptr))((unsigned long)(ptr) + (off)))#endif #define absolute_pointer (val) RELOC_HIDE((void *)(val), 0)
lib/test_context-analysis.c +11 -0
@@ -596,3 +596,14 @@ static void __used test_ww_mutex_lock_ctx(struct test_ww_mutex_data *d)ww_mutex_destroy (&d->mtx); } + +static DEFINE_PER_CPU(raw_spinlock_t, test_per_cpu_lock); + +static void __used test_per_cpu (int cpu) +{ +raw_spin_lock (&per_cpu (test_per_cpu_lock, cpu)); +raw_spin_unlock (&per_cpu (test_per_cpu_lock, cpu)); + +raw_spin_lock (per_cpu_ptr (&test_per_cpu_lock, cpu)); +raw_spin_unlock (per_cpu_ptr (&test_per_cpu_lock, cpu)); +}
Signature · 1 line
2.53.0.851.ga537e3e6e9-goog
Quoted 44 lines · Marco Elver
When enabling Context Analysis (
CONTEXT_ANALYSIS:= y) inarch/x86/kvmcode, Clang's Thread Safety Analysis failed to recognize that identicalper_cpu()accesses refer to the same lock:| CC [M] arch/x86/kvm/vmx/posted_intr.o | arch/x86/kvm/vmx/posted_intr.c:186:2: error: releasing raw_spinlock '__ptr + __per_cpu_offset[vcpu->cpu]' that was not held [-Werror,-Wthread-safety-analysis] | 186 | raw_spin_unlock(&per_cpu(wakeup_vcpus_on_cpu_lock, vcpu->cpu)); | | ^ | ./include/linux/spinlock.h:276:32: note: expanded from macro 'raw_spin_unlock' | 276 | #define raw_spin_unlock(lock) _raw_spin_unlock(lock) | | ^ | arch/x86/kvm/vmx/posted_intr.c:207:1: error: raw_spinlock '__ptr + __per_cpu_offset[vcpu->cpu]' is still held at the end of function [-Werror,-Wthread-safety-analysis] | 207 | } | | ^ | arch/x86/kvm/vmx/posted_intr.c:182:2: note: raw_spinlock acquired here | 182 | raw_spin_lock_nested(&per_cpu(wakeup_vcpus_on_cpu_lock, vcpu->cpu), | | ^ | ./include/linux/spinlock.h:235:2: note: expanded from macro 'raw_spin_lock_nested' | 235 | _raw_spin_lock(((void)(subclass), (lock))) | | ^ | 2 errors generated.This occurred because the default
RELOC_HIDE()implementation (used by the per-CPU macros) is a statement expression containing an intermediate 'unsigned long' variable (this version appears to predate Git history).While the analysis strips away inner casts when resolving pointer aliases, it stops when encountering intermediate non-pointer variables (this is Thread Safety Analysis specific and irrelevant for codegen). This prevents the analysis from concluding that the pointers passed to e.g.
raw_spin_lock()andraw_spin_unlock()were identical when per-CPU accessors are used.Simplify
RELOC_HIDE()to a single expression. This preserves the intent of obfuscating UB-introducing out-of-bounds pointer calculations from the compiler via the 'unsigned long' cast, but allows the alias analysis to successfully resolve the pointers.Using a recent Clang version, I observe that generated code remains the same for vmlinux; the intermediate variable was already being optimized away (for any respectable modern compiler, not doing so would be an optimizer bug). Note that GCC provides its own version of
RELOC_HIDE(), so this change only affects Clang builds.
Quoted 6 lines
Add a test case to
lib/test_context-analysis.cto catch any regressions.Reported-by Bart Van Assche <[email protected]>Reported-by Sean Christopherson <[email protected]>Signed-off-by Marco Elver <[email protected]>
Quoted 43 lines
--- include/linux/compiler.h | 5 +---- lib/test_context-analysis.c | 11 +++++++++++ 2 files changed, 12 insertions(+), 4 deletions(-)include/linux/compiler.h +1
-4@@ -149,10 +149,7 @@ void ftrace_likely_update(struct ftrace_likely_data *f, int val,#endif #ifndef RELOC_HIDE -# define RELOC_HIDE (ptr, off) \ - ({ unsigned long __ptr; \ - __ptr = (unsigned long) (ptr); \ - (typeof(ptr)) (__ptr + (off)); }) +# define RELOC_HIDE (ptr, off) ((typeof(ptr))((unsigned long)(ptr) + (off)))#endif #define absolute_pointer (val) RELOC_HIDE((void *)(val), 0)lib/test_context-analysis.c +11
-0@@ -596,3 +596,14 @@ static void __used test_ww_mutex_lock_ctx(struct test_ww_mutex_data *d)ww_mutex_destroy (&d->mtx); } + +static DEFINE_PER_CPU(raw_spinlock_t, test_per_cpu_lock); + +static void __used test_per_cpu (int cpu) +{ +raw_spin_lock (&per_cpu (test_per_cpu_lock, cpu)); +raw_spin_unlock (&per_cpu (test_per_cpu_lock, cpu)); + +raw_spin_lock (per_cpu_ptr (&test_per_cpu_lock, cpu)); +raw_spin_unlock (per_cpu_ptr (&test_per_cpu_lock, cpu)); +}Signature · 1 line
2.53.0.851.ga537e3e6e9-goog
Quoted 45 lines · Nathan Chancellor
Quoted 44 lines · Marco Elver
When enabling Context Analysis (
CONTEXT_ANALYSIS:= y) inarch/x86/kvmcode, Clang's Thread Safety Analysis failed to recognize that identicalper_cpu()accesses refer to the same lock:| CC [M] arch/x86/kvm/vmx/posted_intr.o | arch/x86/kvm/vmx/posted_intr.c:186:2: error: releasing raw_spinlock '__ptr + __per_cpu_offset[vcpu->cpu]' that was not held [-Werror,-Wthread-safety-analysis] | 186 | raw_spin_unlock(&per_cpu(wakeup_vcpus_on_cpu_lock, vcpu->cpu)); | | ^ | ./include/linux/spinlock.h:276:32: note: expanded from macro 'raw_spin_unlock' | 276 | #define raw_spin_unlock(lock) _raw_spin_unlock(lock) | | ^ | arch/x86/kvm/vmx/posted_intr.c:207:1: error: raw_spinlock '__ptr + __per_cpu_offset[vcpu->cpu]' is still held at the end of function [-Werror,-Wthread-safety-analysis] | 207 | } | | ^ | arch/x86/kvm/vmx/posted_intr.c:182:2: note: raw_spinlock acquired here | 182 | raw_spin_lock_nested(&per_cpu(wakeup_vcpus_on_cpu_lock, vcpu->cpu), | | ^ | ./include/linux/spinlock.h:235:2: note: expanded from macro 'raw_spin_lock_nested' | 235 | _raw_spin_lock(((void)(subclass), (lock))) | | ^ | 2 errors generated.This occurred because the default
RELOC_HIDE()implementation (used by the per-CPU macros) is a statement expression containing an intermediate 'unsigned long' variable (this version appears to predate Git history).While the analysis strips away inner casts when resolving pointer aliases, it stops when encountering intermediate non-pointer variables (this is Thread Safety Analysis specific and irrelevant for codegen). This prevents the analysis from concluding that the pointers passed to e.g.
raw_spin_lock()andraw_spin_unlock()were identical when per-CPU accessors are used.Simplify
RELOC_HIDE()to a single expression. This preserves the intent of obfuscating UB-introducing out-of-bounds pointer calculations from the compiler via the 'unsigned long' cast, but allows the alias analysis to successfully resolve the pointers.Using a recent Clang version, I observe that generated code remains the same for vmlinux; the intermediate variable was already being optimized away (for any respectable modern compiler, not doing so would be an optimizer bug). Note that GCC provides its own version of
RELOC_HIDE(), so this change only affects Clang builds.
Quoted 8 lines
Quoted 6 lines
Add a test case to
lib/test_context-analysis.cto catch any regressions.Reported-by Bart Van Assche <[email protected]>Reported-by Sean Christopherson <[email protected]>Signed-off-by Marco Elver <[email protected]>Reviewed-by Nathan Chancellor <[email protected]>
Thanks, I'll go stick it in tip/locking/core.
The following commit has been merged into the locking/core branch of tip:
The following commit has been merged into the locking/core branch of tip:
Commit-ID: a21c1e961de28b95099a9ca2c3774b2eee1a33bb Gitweb: https://git.kernel.org/tip/a21c1e961de28b95099a9ca2c3774b2eee1a33bb Author: Marco Elver <[email protected]> AuthorDate: Thu, 19 Mar 2026 14:52:38 +01:00 Committer: Peter Zijlstra <[email protected]> CommitterDate: Tue, 24 Mar 2026 15:08:05 +01:00
compiler: Simplify generic RELOC_HIDE()
When enabling Context Analysis (CONTEXT_ANALYSIS := y) in arch/x86/kvm code, Clang's Thread Safety Analysis failed to recognize that identical per_cpu() accesses refer to the same lock:
| CC [M] arch/x86/kvm/vmx/posted_intr.o | arch/x86/kvm/vmx/posted_intr.c:186:2: error: releasing raw_spinlock '__ptr + __per_cpu_offset[vcpu->cpu]' that was not held [-Werror,-Wthread-safety-analysis] | 186 | raw_spin_unlock(&per_cpu(wakeup_vcpus_on_cpu_lock, vcpu->cpu)); | | ^ | ./include/linux/spinlock.h:276:32: note: expanded from macro 'raw_spin_unlock' | 276 | #define raw_spin_unlock(lock) _raw_spin_unlock(lock) | | ^ | arch/x86/kvm/vmx/posted_intr.c:207:1: error: raw_spinlock '__ptr + __per_cpu_offset[vcpu->cpu]' is still held at the end of function [-Werror,-Wthread-safety-analysis] | 207 | } | | ^ | arch/x86/kvm/vmx/posted_intr.c:182:2: note: raw_spinlock acquired here | 182 | raw_spin_lock_nested(&per_cpu(wakeup_vcpus_on_cpu_lock, vcpu->cpu), | | ^ | ./include/linux/spinlock.h:235:2: note: expanded from macro 'raw_spin_lock_nested' | 235 | _raw_spin_lock(((void)(subclass), (lock))) | | ^ | 2 errors generated.
This occurred because the default RELOC_HIDE() implementation (used by the per-CPU macros) is a statement expression containing an intermediate 'unsigned long' variable (this version appears to predate Git history).
While the analysis strips away inner casts when resolving pointer aliases, it stops when encountering intermediate non-pointer variables (this is Thread Safety Analysis specific and irrelevant for codegen). This prevents the analysis from concluding that the pointers passed to e.g. raw_spin_lock() and raw_spin_unlock() were identical when per-CPU accessors are used.
Simplify RELOC_HIDE() to a single expression. This preserves the intent of obfuscating UB-introducing out-of-bounds pointer calculations from the compiler via the 'unsigned long' cast, but allows the alias analysis to successfully resolve the pointers.
Using a recent Clang version, I observe that generated code remains the same for vmlinux; the intermediate variable was already being optimized away (for any respectable modern compiler, not doing so would be an optimizer bug). Note that GCC provides its own version of RELOC_HIDE(), so this change only affects Clang builds.
Add a test case to lib/test_context-analysis.c to catch any regressions.
--- include/linux/compiler.h | 5 +---- lib/test_context-analysis.c | 11 +++++++++++ 2 files changed, 12 insertions(+), 4 deletions(-)
include/linux/compiler.h +1 -4
@@ -149,10 +149,7 @@ void ftrace_likely_update(struct ftrace_likely_data *f, int val,#endif #ifndef RELOC_HIDE -# define RELOC_HIDE (ptr, off) \ - ({ unsigned long __ptr; \ - __ptr = (unsigned long) (ptr); \ - (typeof(ptr)) (__ptr + (off)); }) +# define RELOC_HIDE (ptr, off) ((typeof(ptr))((unsigned long)(ptr) + (off)))#endif #define absolute_pointer (val) RELOC_HIDE((void *)(val), 0)
lib/test_context-analysis.c +11 -0
@@ -596,3 +596,14 @@ static void __used test_ww_mutex_lock_ctx(struct test_ww_mutex_data *d)ww_mutex_destroy (&d->mtx); } + +static DEFINE_PER_CPU(raw_spinlock_t, test_per_cpu_lock); + +static void __used test_per_cpu (int cpu) +{ +raw_spin_lock (&per_cpu (test_per_cpu_lock, cpu)); +raw_spin_unlock (&per_cpu (test_per_cpu_lock, cpu)); + +raw_spin_lock (per_cpu_ptr (&test_per_cpu_lock, cpu)); +raw_spin_unlock (per_cpu_ptr (&test_per_cpu_lock, cpu)); +}