Skip to contentSkip to conversation

bpf: Mark kfuncs as __noclone

PATCH Applied Andrea Righi 2 participants 2 messages Sep 24, 2025 BPF

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 bpf: Mark kfuncs as __noclone

Some distributions (e.g., CachyOS) support building the kernel with -O3, but doing so may break kfuncs, resulting in their symbols not being properly exported.

In fact, with gcc -O3, some kfuncs may be optimized away despite being annotated as noinline. This happens because gcc can still clone the function during IPA optimizations, e.g., by duplicating or inlining it into callers, and then dropping the standalone symbol. This breaks BTF ID resolution since resolve_btfids relies on the presence of a global symbol for each kfunc.

Currently, this is not an issue for upstream, because we don't allow building the kernel with -O3, but it may be safer to address it anyway, to prevent potential issues in the future if compilers become more aggressive with optimizations.

Therefore, add __noclone to __bpf_kfunc to ensure kfuncs are never cloned and remain distinct, globally visible symbols, regardless of the optimization level.

Fixes 57e7c169cd6af ("bpf: Add __bpf_kfunc tag for marking kernel functions as kfuncs")

---
Changes in v2:
- Rebase to bpf-next
- Link to v1: https://lore.kernel.org/all/[email protected]/

 include/linux/btf.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/linux/btf.h +1 -1
index 9eda6b113f9b4..f06976ffb63f9 100644
@@ -86,7 +86,7 @@
  * as to avoid issues such as the compiler inlining or eliding either a static
  * kfunc, or a global kfunc in an LTO build.
  */
-#define __bpf_kfunc __used __retain noinline
+#define __bpf_kfunc __used __retain __noclone noinline
 
 #define __bpf_kfunc_start_defs()					       \
 	__diag_push();							       \
Signature · 1 line
2.51.0
#1 lore raw
This patch was applied to bpf/bpf-next.git (master) by Alexei Starovoitov <[email protected]>:

Hello:

This patch was applied to bpf/bpf-next.git (master) by Alexei Starovoitov <[email protected]>:

Quoted 4 lines · On Wed, 24 Sep 2025 10:14:26 +0200 you wrote:

Some distributions (e.g., CachyOS) support building the kernel with -O3, but doing so may break kfuncs, resulting in their symbols not being properly exported.

Quoted 8 lines

In fact, with gcc -O3, some kfuncs may be optimized away despite being annotated as noinline. This happens because gcc can still clone the function during IPA optimizations, e.g., by duplicating or inlining it into callers, and then dropping the standalone symbol. This breaks BTF ID resolution since resolve_btfids relies on the presence of a global symbol for each kfunc.

[...]

Here is the summary with links:
- [v2,bpf-next] bpf: Mark kfuncs as __noclone

    https://git.kernel.org/bpf/bpf-next/c/d4680a11e14c

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html