Skip to content
/ linux Public

Commit 3713af7

Browse files
alan-maguireSasha Levin
authored andcommitted
kcsan, compiler_types: avoid duplicate type issues in BPF Type Format
[ Upstream commit 9dc0522 ] Enabling KCSAN is causing a large number of duplicate types in BTF for core kernel structs like task_struct [1]. This is due to the definition in include/linux/compiler_types.h `#ifdef __SANITIZE_THREAD__ ... `#define __data_racy volatile .. `#else ... `#define __data_racy ... `#endif Because some objects in the kernel are compiled without KCSAN flags (KCSAN_SANITIZE) we sometimes get the empty __data_racy annotation for objects; as a result we get multiple conflicting representations of the associated structs in DWARF, and these lead to multiple instances of core kernel types in BTF since they cannot be deduplicated due to the additional modifier in some instances. Moving the __data_racy definition under CONFIG_KCSAN avoids this problem, since the volatile modifier will be present for both KCSAN and KCSAN_SANITIZE objects in a CONFIG_KCSAN=y kernel. Link: https://lkml.kernel.org/r/20260116091730.324322-1-alan.maguire@oracle.com Fixes: 31f605a ("kcsan, compiler_types: Introduce __data_racy type qualifier") Signed-off-by: Alan Maguire <alan.maguire@oracle.com> Reported-by: Nilay Shroff <nilay@linux.ibm.com> Tested-by: Nilay Shroff <nilay@linux.ibm.com> Suggested-by: Marco Elver <elver@google.com> Reviewed-by: Marco Elver <elver@google.com> Acked-by: Yonghong Song <yonghong.song@linux.dev> Cc: Alexei Starovoitov <ast@kernel.org> Cc: Andrii Nakryiko <andrii.nakryiko@gmail.com> Cc: Bart van Assche <bvanassche@acm.org> Cc: Daniel Borkman <daniel@iogearbox.net> Cc: Eduard Zingerman <eddyz87@gmail.com> Cc: Hao Luo <haoluo@google.com> Cc: Heiko Carstens <hca@linux.ibm.com> Cc: "H. Peter Anvin" <hpa@zytor.com> Cc: Jason A. Donenfeld <jason@zx2c4.com> Cc: Jiri Olsa <jolsa@kernel.org> Cc: John Fastabend <john.fastabend@gmail.com> Cc: Kees Cook <kees@kernel.org> Cc: KP Singh <kpsingh@kernel.org> Cc: Martin KaFai Lau <martin.lau@linux.dev> Cc: Miguel Ojeda <ojeda@kernel.org> Cc: Naman Jain <namjain@linux.microsoft.com> Cc: Nathan Chancellor <nathan@kernel.org> Cc: "Paul E . McKenney" <paulmck@kernel.org> Cc: Peter Zijlstra <peterz@infradead.org> Cc: Stanislav Fomichev <sdf@fomichev.me> Cc: Uros Bizjak <ubizjak@gmail.com> Cc: <stable@vger.kernel.org> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Sasha Levin <sashal@kernel.org>
1 parent 04d24a3 commit 3713af7

File tree

1 file changed

+16
-7
lines changed

1 file changed

+16
-7
lines changed

include/linux/compiler_types.h

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,22 @@ struct ftrace_likely_data {
297297
# define __no_kasan_or_inline __always_inline
298298
#endif
299299

300+
#ifdef CONFIG_KCSAN
301+
/*
302+
* Type qualifier to mark variables where all data-racy accesses should be
303+
* ignored by KCSAN. Note, the implementation simply marks these variables as
304+
* volatile, since KCSAN will treat such accesses as "marked".
305+
*
306+
* Defined here because defining __data_racy as volatile for KCSAN objects only
307+
* causes problems in BPF Type Format (BTF) generation since struct members
308+
* of core kernel data structs will be volatile in some objects and not in
309+
* others. Instead define it globally for KCSAN kernels.
310+
*/
311+
# define __data_racy volatile
312+
#else
313+
# define __data_racy
314+
#endif
315+
300316
#ifdef __SANITIZE_THREAD__
301317
/*
302318
* Clang still emits instrumentation for __tsan_func_{entry,exit}() and builtin
@@ -308,16 +324,9 @@ struct ftrace_likely_data {
308324
* disable all instrumentation. See Kconfig.kcsan where this is mandatory.
309325
*/
310326
# define __no_kcsan __no_sanitize_thread __disable_sanitizer_instrumentation
311-
/*
312-
* Type qualifier to mark variables where all data-racy accesses should be
313-
* ignored by KCSAN. Note, the implementation simply marks these variables as
314-
* volatile, since KCSAN will treat such accesses as "marked".
315-
*/
316-
# define __data_racy volatile
317327
# define __no_sanitize_or_inline __no_kcsan notrace __maybe_unused
318328
#else
319329
# define __no_kcsan
320-
# define __data_racy
321330
#endif
322331

323332
#ifdef __SANITIZE_MEMORY__

0 commit comments

Comments
 (0)