Skip to content

Commit 0a5d325

Browse files
guidosarducciborkmann
authored andcommitted
compiler_types.h: Define __retain for __attribute__((__retain__))
Some code includes the __used macro to prevent functions and data from being optimized out. This macro implements __attribute__((__used__)), which operates at the compiler and IR-level, and so still allows a linker to remove objects intended to be kept. Compilers supporting __attribute__((__retain__)) can address this gap by setting the flag SHF_GNU_RETAIN on the section of a function/variable, indicating to the linker the object should be retained. This attribute is available since gcc 11, clang 13, and binutils 2.36. Provide a __retain macro implementing __attribute__((__retain__)), whose first user will be the '__bpf_kfunc' tag. [ Additional remark from discussion: Why is CONFIG_LTO_CLANG added here? The __used macro permits garbage collection at section level, so CLANG_LTO_CLANG without CONFIG_LD_DEAD_CODE_DATA_ELIMINATION should not change final section dynamics? The conditional guard was included to ensure consistent behaviour between __retain and other features forcing split sections. In particular, the same guard is used in vmlinux.lds.h to merge split sections where needed. For example, using __retain in LLVM builds without CONFIG_LTO was failing CI tests on kernel-patches/bpf because the kernel didn't boot properly. And in further testing, the kernel had no issues loading BPF kfunc modules with such split sections, so the module (partial) linking scripts were left alone. ] Signed-off-by: Tony Ambardar <tony.ambardar@gmail.com> Signed-off-by: Daniel Borkmann <daniel@iogearbox.net> Cc: Yonghong Song <yonghong.song@linux.dev> Link: https://lore.kernel.org/bpf/ZlmGoT9KiYLZd91S@krava/T/ Link: https://lore.kernel.org/bpf/b31bca5a5e6765a0f32cc8c19b1d9cdbfaa822b5.1717477560.git.Tony.Ambardar@gmail.com
1 parent 2bbe3e5 commit 0a5d325

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

include/linux/compiler_types.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,29 @@ static inline void __chk_io_ptr(const volatile void __iomem *ptr) { }
143143
# define __preserve_most
144144
#endif
145145

146+
/*
147+
* Annotating a function/variable with __retain tells the compiler to place
148+
* the object in its own section and set the flag SHF_GNU_RETAIN. This flag
149+
* instructs the linker to retain the object during garbage-cleanup or LTO
150+
* phases.
151+
*
152+
* Note that the __used macro is also used to prevent functions or data
153+
* being optimized out, but operates at the compiler/IR-level and may still
154+
* allow unintended removal of objects during linking.
155+
*
156+
* Optional: only supported since gcc >= 11, clang >= 13
157+
*
158+
* gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-retain-function-attribute
159+
* clang: https://clang.llvm.org/docs/AttributeReference.html#retain
160+
*/
161+
#if __has_attribute(__retain__) && \
162+
(defined(CONFIG_LD_DEAD_CODE_DATA_ELIMINATION) || \
163+
defined(CONFIG_LTO_CLANG))
164+
# define __retain __attribute__((__retain__))
165+
#else
166+
# define __retain
167+
#endif
168+
146169
/* Compiler specific macros. */
147170
#ifdef __clang__
148171
#include <linux/compiler-clang.h>

0 commit comments

Comments
 (0)