kcsan, compiler_types: avoid duplicate type issues in BPF Type Format
PATCH 1/1 kcsan, compiler_types: avoid duplicate type issues in BPF Type Format
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.
--- include/linux/compiler_types.h | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-)
include/linux/compiler_types.h +16 -7
@@ -303,6 +303,22 @@ struct ftrace_likely_data {# define __no_kasan_or_inline __always_inline#endif +#ifdef CONFIG_KCSAN +/* +* Type qualifier to mark variables where all data-racy accesses should be +* ignored by KCSAN. Note, the implementation simply marks these variables as +* volatile, since KCSAN will treat such accesses as "marked". +* +* Defined here because defining __data_racy as volatile for KCSAN objects only +* causes problems in BPF Type Format (BTF) generation since struct members +* of core kernel data structs will be volatile in some objects and not in +* others. Instead define it globally for KCSAN kernels. +*/ +# define __data_racy volatile +#else +# define __data_racy +#endif +#ifdef __SANITIZE_THREAD__/* * Clang still emits instrumentation for __tsan_func_{entry,exit}() and builtin
@@ -314,16 +330,9 @@ struct ftrace_likely_data {* disable all instrumentation. See Kconfig.kcsan where this is mandatory. */ # define __no_kcsan __no_sanitize_thread __disable_sanitizer_instrumentation -/* -* Type qualifier to mark variables where all data-racy accesses should be -* ignored by KCSAN. Note, the implementation simply marks these variables as -* volatile, since KCSAN will treat such accesses as "marked". -*/ -# define __data_racy volatile# define __no_sanitize_or_inline __no_kcsan notrace __maybe_unused#else # define __no_kcsan -# define __data_racy#endif #ifdef __SANITIZE_MEMORY__
Signature · 1 line
2.39.3
Quoted 23 lines · Alan Maguire
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 ininclude/linux/compiler_types.h`#ifdef
__SANITIZE_THREAD__
...
`#define__data_racyvolatile
..
`#else
...
`#define__data_racy
...
`#endifBecause some objects in the kernel are compiled without KCSAN flags (
KCSAN_SANITIZE) we sometimes get the empty__data_racyannotation 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.
Quoted 4 lines
Moving the
__data_racydefinition underCONFIG_KCSANavoids this problem, since the volatile modifier will be present for both KCSAN andKCSAN_SANITIZEobjects in aCONFIG_KCSAN=ykernel.
"KCSAN and KCSAN_SANITIZE objects" doesn't make sense. "KCSAN_SANITIZE.. := n" objects?
Or just "instrumented and uninstrumented source files". Anyway, I know what you mean, but others might not. :-)
Quoted 4 lines
Fixes 31f605a308e6 ("kcsan, compiler_types: Introduce __data_racy type qualifier")Reported-by Nilay Shroff <[email protected]>Suggested-by Marco Elver <[email protected]>Signed-off-by Alan Maguire <[email protected]>
Quoted 51 lines
--- include/linux/compiler_types.h | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-)include/linux/compiler_types.h +16
-8@@ -303,6 +303,22 @@ struct ftrace_likely_data {# define __no_kasan_or_inline __always_inline#endif +#ifdef CONFIG_KCSAN +/* +* Type qualifier to mark variables where all data-racy accesses should be +* ignored by KCSAN. Note, the implementation simply marks these variables as +* volatile, since KCSAN will treat such accesses as "marked". +* +* Defined here because defining __data_racy as volatile for KCSAN objects only +* causes problems in BPF Type Format (BTF) generation since struct members +* of core kernel data structs will be volatile in some objects and not in +* others. Instead define it globally for KCSAN kernels. +*/ +# define __data_racy volatile +#else +# define __data_racy +#endif +#ifdef __SANITIZE_THREAD__/* * Clang still emits instrumentation for __tsan_func_{entry,exit}() and builtin @@ -314,16 +330,9 @@ struct ftrace_likely_data {* disable all instrumentation. See Kconfig.kcsan where this is mandatory. */ # define __no_kcsan __no_sanitize_thread __disable_sanitizer_instrumentation -/* -* Type qualifier to mark variables where all data-racy accesses should be -* ignored by KCSAN. Note, the implementation simply marks these variables as -* volatile, since KCSAN will treat such accesses as "marked". -*/ -# define __data_racy volatile# define __no_sanitize_or_inline __no_kcsan notrace __maybe_unused#else # define __no_kcsan -# define __data_racy#endif #ifdef __SANITIZE_MEMORY__ --2.39.3
Quoted 34 lines · Marco Elver
Quoted 23 lines · Alan Maguire
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 ininclude/linux/compiler_types.h`#ifdef
__SANITIZE_THREAD__
...
`#define__data_racyvolatile
..
`#else
...
`#define__data_racy
...
`#endifBecause some objects in the kernel are compiled without KCSAN flags (
KCSAN_SANITIZE) we sometimes get the empty__data_racyannotation 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.Quoted 4 lines
Moving the
__data_racydefinition underCONFIG_KCSANavoids this problem, since the volatile modifier will be present for both KCSAN andKCSAN_SANITIZEobjects in aCONFIG_KCSAN=ykernel."KCSAN and
KCSAN_SANITIZEobjects" doesn't make sense. "KCSAN_SANITIZE.. := n" objects?
Or just "instrumented and uninstrumented source files". Anyway, I know what you mean, but others might not. :-)
Quoted 6 lines
Quoted 4 lines
Fixes 31f605a308e6 ("kcsan, compiler_types: Introduce __data_racy type qualifier")Reported-by Nilay Shroff <[email protected]>Suggested-by Marco Elver <[email protected]>Signed-off-by Alan Maguire <[email protected]>Reviewed-by Marco Elver <[email protected]>
Which tree do compiler_types.h changes go through these days?
Quoted 5 lines · On Wed, 28 Jan 2026 00:08:10 +0100 Marco Elver <[email protected]> wrote:
Quoted 5 lines
"KCSAN and
KCSAN_SANITIZEobjects" doesn't make sense. "KCSAN_SANITIZE.. := n" objects?
Or just "instrumented and uninstrumented source files". Anyway, I know what you mean, but others might not. :-)
Quoted 8 lines
Quoted 6 lines
Quoted 4 lines
Fixes 31f605a308e6 ("kcsan, compiler_types: Introduce __data_racy type qualifier")Reported-by Nilay Shroff <[email protected]>Suggested-by Marco Elver <[email protected]>Signed-off-by Alan Maguire <[email protected]>Reviewed-by Marco Elver <[email protected]>Which tree do
compiler_types.hchanges go through these days?
Thanks for poking.
compiler_types.h appears to be a free-for-all. It's best to view such a thing as a KCSAN patch rather than a compiler_types.h patch - that the patch affects compiler_types.h is incidental.
31f605a308e6 came in via paulmck so convention (which perhaps only I maintain) says "Paul", but whatever - getting the fix merged is the important part.
So I'll grab Alan's patch, shall drop if it pops up in -next via a
different route. Aiming for upstreaming in the next merge window.
It's unclear whether a -stable backport is required. Thoughts on this are sought.
Quoted 23 lines · Andrew Morton
Quoted 5 lines · On Wed, 28 Jan 2026 00:08:10 +0100 Marco Elver <[email protected]> wrote:
Quoted 5 lines
"KCSAN and
KCSAN_SANITIZEobjects" doesn't make sense. "KCSAN_SANITIZE.. := n" objects?
Or just "instrumented and uninstrumented source files". Anyway, I know what you mean, but others might not. :-)Quoted 8 lines
Quoted 6 lines
Quoted 4 lines
Fixes 31f605a308e6 ("kcsan, compiler_types: Introduce __data_racy type qualifier")Reported-by Nilay Shroff <[email protected]>Suggested-by Marco Elver <[email protected]>Signed-off-by Alan Maguire <[email protected]>Reviewed-by Marco Elver <[email protected]>Which tree do
compiler_types.hchanges go through these days?Thanks for poking.
compiler_types.happears to be a free-for-all. It's best to view such a thing as a KCSAN patch rather than acompiler_types.hpatch - that the patch affectscompiler_types.his incidental.
Quoted 6 lines
31f605a308e6came in via paulmck so convention (which perhaps only I maintain) says "Paul", but whatever - getting the fix merged is the important part.So I'll grab Alan's patch, shall drop if it pops up in -next via a
different route. Aiming for upstreaming in the next merge window.
You mean in the upcoming merge window, right?
The severity of the bug is significant. Better to land it sooner rather than later.
Quoted 2 lines
It's unclear whether a -stable backport is required. Thoughts on this are sought.
Yes, since without it kcsan and bpf are mutually exclusive.
Quoted 4 lines · On Tue, 27 Jan 2026 16:02:32 -0800 Alexei Starovoitov…
Quoted 4 lines
compiler_types.happears to be a free-for-all. It's best to view such a thing as a KCSAN patch rather than acompiler_types.hpatch - that the patch affectscompiler_types.his incidental.
Quoted 8 lines
Quoted 6 lines
31f605a308e6came in via paulmck so convention (which perhaps only I maintain) says "Paul", but whatever - getting the fix merged is the important part.So I'll grab Alan's patch, shall drop if it pops up in -next via a
different route. Aiming for upstreaming in the next merge window.You mean in the upcoming merge window, right?
Yes.
It could be added to 6.19-rcX/-stable if there's a need?
Quoted 2 lines
The severity of the bug is significant. Better to land it sooner rather than later.
OK, that wasn't clear to me from the changelog.
Quoted 4 lines
Quoted 2 lines
It's unclear whether a -stable backport is required. Thoughts on this are sought.
Yes, since without it kcsan and bpf are mutually exclusive.
OK, cc:stable added.
Quoted 15 lines · Andrew Morton
Quoted 12 lines · On Wed, 28 Jan 2026 00:08:10 +0100 Marco Elver <[email protected]> wrote:
Quoted 11 lines
"KCSAN and
KCSAN_SANITIZEobjects" doesn't make sense. "KCSAN_SANITIZE.. := n" objects?
Or just "instrumented and uninstrumented source files". Anyway, I know what you mean, but others might not. :-)Quoted 4 lines
Fixes 31f605a308e6 ("kcsan, compiler_types: Introduce __data_racy type qualifier")Reported-by Nilay Shroff <[email protected]>Suggested-by Marco Elver <[email protected]>Signed-off-by Alan Maguire <[email protected]>Reviewed-by Marco Elver <[email protected]>
Quoted 7 lines
Quoted 1 line
Which tree do
compiler_types.hchanges go through these days?Thanks for poking.
compiler_types.happears to be a free-for-all. It's best to view such a thing as a KCSAN patch rather than acompiler_types.hpatch - that the patch affectscompiler_types.his incidental.
I wasn't quite sure, given the conflict potential for this file - but next time I'd take it through the kcsan tree then. This time around, please continue to carry it.
Quoted 6 lines
31f605a308e6came in via paulmck so convention (which perhaps only I maintain) says "Paul", but whatever - getting the fix merged is the important part.So I'll grab Alan's patch, shall drop if it pops up in -next via a
different route. Aiming for upstreaming in the next merge window.
Thanks!
Quoted 2 lines
It's unclear whether a -stable backport is required. Thoughts on this are sought.
I think it'd be appropriate!
Quoted 69 lines · Alan Maguire
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 ininclude/linux/compiler_types.h`#ifdef
__SANITIZE_THREAD__
...
`#define__data_racyvolatile
..
`#else
...
`#define__data_racy
...
`#endifBecause some objects in the kernel are compiled without KCSAN flags (
KCSAN_SANITIZE) we sometimes get the empty__data_racyannotation 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_racydefinition underCONFIG_KCSANavoids this problem, since the volatile modifier will be present for both KCSAN andKCSAN_SANITIZEobjects in aCONFIG_KCSAN=ykernel.Fixes 31f605a308e6 ("kcsan, compiler_types: Introduce __data_racy type qualifier")Reported-by Nilay Shroff <[email protected]>Suggested-by Marco Elver <[email protected]>Signed-off-by Alan Maguire <[email protected]>--- include/linux/compiler_types.h | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-)include/linux/compiler_types.h +16
-3@@ -303,6 +303,22 @@ struct ftrace_likely_data {# define __no_kasan_or_inline __always_inline#endif +#ifdef CONFIG_KCSAN +/* +* Type qualifier to mark variables where all data-racy accesses should be +* ignored by KCSAN. Note, the implementation simply marks these variables as +* volatile, since KCSAN will treat such accesses as "marked". +* +* Defined here because defining __data_racy as volatile for KCSAN objects only +* causes problems in BPF Type Format (BTF) generation since struct members +* of core kernel data structs will be volatile in some objects and not in +* others. Instead define it globally for KCSAN kernels. +*/ +# define __data_racy volatile +#else +# define __data_racy +#endif +#ifdef __SANITIZE_THREAD__/* * Clang still emits instrumentation for __tsan_func_{entry,exit}() and builtin @@ -314,16 +330,9 @@ struct ftrace_likely_data {* disable all instrumentation. See Kconfig.kcsan where this is mandatory. */ # define __no_kcsan __no_sanitize_thread __disable_sanitizer_instrumentation -/* -* Type qualifier to mark variables where all data-racy accesses should be -* ignored by KCSAN. Note, the implementation simply marks these variables as
include/linux/compiler_types.h struct ftrace_likely_data { · line 338
Quoted 10 lines
include/linux/compiler_types.h +0
-4-* volatile, since KCSAN will treat such accesses as "marked". -*/ -# define __data_racy volatile# define __no_sanitize_or_inline __no_kcsan notrace __maybe_unused#else # define __no_kcsan -# define __data_racy#endif #ifdef __SANITIZE_MEMORY__
Thanks Alan for working on this! I tested this change on my system and it works well. So with that,
Quoted 27 lines · Alan Maguire
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 ininclude/linux/compiler_types.h`#ifdef
__SANITIZE_THREAD__
...
`#define__data_racyvolatile
..
`#else
...
`#define__data_racy
...
`#endifBecause some objects in the kernel are compiled without KCSAN flags (
KCSAN_SANITIZE) we sometimes get the empty__data_racyannotation 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_racydefinition underCONFIG_KCSANavoids this problem, since the volatile modifier will be present for both KCSAN andKCSAN_SANITIZEobjects in aCONFIG_KCSAN=ykernel.
Quoted 4 lines
Fixes 31f605a308e6 ("kcsan, compiler_types: Introduce __data_racy type qualifier")Reported-by Nilay Shroff <[email protected]>Suggested-by Marco Elver <[email protected]>Signed-off-by Alan Maguire <[email protected]>
Tested on gcc15 and llvm22 and all works okay. Also the patch itself makes sense, so