已开启
[sync] PR-209: libbpf: Store zero fd to fd_array for loader kfunc relocation #215
[sync] PR-209: libbpf: Store zero fd to fd_array for loader kfunc relocation #215
已开启
openeuler-ci-bot创建于 6月17日
共 2 个文件变更+69-1
@@ -0,0 +1,64 @@
1+From 4bb0b0ca09d9d0e99a23bf7748d0c23fb4740e9f Mon Sep 17 00:00:00 2001
2+From: Jiri Olsa <jolsa@kernel.org>
3+Date: Mon, 15 May 2023 15:37:47 +0200
4+Subject: [PATCH] libbpf: Store zero fd to fd_array for loader kfunc relocation
5+MIME-Version: 1.0
6+Content-Type: text/plain; charset=UTF-8
7+Content-Transfer-Encoding: 8bit
8+ 
9+When moving some of the test kfuncs to bpf_testmod I hit an issue
10+when some of the kfuncs that object uses are in module and some
11+in vmlinux.
12+ 
13+The problem is that both vmlinux and module kfuncs get allocated
14+btf_fd_idx index into fd_array, but we store to it the BTF fd value
15+only for module's kfunc, not vmlinux's one because (it's zero).
16+ 
17+Then after the program is loaded we check if fd_array[btf_fd_idx] != 0
18+and close the fd.
19+ 
20+When the object has kfuncs from both vmlinux and module, the fd from
21+fd_array[btf_fd_idx] from previous load will be stored in there for
22+vmlinux's kfunc, so we close unrelated fd (of the program we just
23+loaded in my case).
24+ 
25+Fixing this by storing zero to fd_array[btf_fd_idx] for vmlinux
26+kfuncs, so the we won't close stale fd.
27+ 
28+Signed-off-by: Jiri Olsa <jolsa@kernel.org>
29+Link: https://lore.kernel.org/r/20230515133756.1658301-2-jolsa@kernel.org
30+Signed-off-by: Alexei Starovoitov <ast@kernel.org>
31+Signed-off-by: Daniel Müller <deso@posteo.net>
32+---
33+ src/gen_loader.c | 14 +++++++-------
34+ 1 file changed, 7 insertions(+), 7 deletions(-)
35+ 
36+diff --git a/src/gen_loader.c b/src/gen_loader.c
37+index 83e8e3bfd..cf3323fd4 100644
38+--- a/src/gen_loader.c
39++++ b/src/gen_loader.c
40+@@ -703,17 +703,17 @@ static void emit_relo_kfunc_btf(struct bpf_gen *gen, struct ksym_relo_desc *relo
41+ /* obtain fd in BPF_REG_9 */
42+ emit(gen, BPF_MOV64_REG(BPF_REG_9, BPF_REG_7));
43+ emit(gen, BPF_ALU64_IMM(BPF_RSH, BPF_REG_9, 32));
44+- /* jump to fd_array store if fd denotes module BTF */
45+- emit(gen, BPF_JMP_IMM(BPF_JNE, BPF_REG_9, 0, 2));
46+- /* set the default value for off */
47+- emit(gen, BPF_ST_MEM(BPF_H, BPF_REG_8, offsetof(struct bpf_insn, off), 0));
48+- /* skip BTF fd store for vmlinux BTF */
49+- emit(gen, BPF_JMP_IMM(BPF_JA, 0, 0, 4));
50+ /* load fd_array slot pointer */
51+ emit2(gen, BPF_LD_IMM64_RAW_FULL(BPF_REG_0, BPF_PSEUDO_MAP_IDX_VALUE,
52+ 0, 0, 0, blob_fd_array_off(gen, btf_fd_idx)));
53+- /* store BTF fd in slot */
54++ /* store BTF fd in slot, 0 for vmlinux */
55+ emit(gen, BPF_STX_MEM(BPF_W, BPF_REG_0, BPF_REG_9, 0));
56++ /* jump to insn[insn_idx].off store if fd denotes module BTF */
57++ emit(gen, BPF_JMP_IMM(BPF_JNE, BPF_REG_9, 0, 2));
58++ /* set the default value for off */
59++ emit(gen, BPF_ST_MEM(BPF_H, BPF_REG_8, offsetof(struct bpf_insn, off), 0));
60++ /* skip BTF fd store for vmlinux BTF */
61++ emit(gen, BPF_JMP_IMM(BPF_JA, 0, 0, 1));
62+ /* store index into insn[insn_idx].off */
63+ emit(gen, BPF_ST_MEM(BPF_H, BPF_REG_8, offsetof(struct bpf_insn, off), btf_fd_idx));
64+ log:
@@ -4,7 +4,7 @@
4 4 
5Name: %{githubname}5Name: %{githubname}
6Version: %{githubver}6Version: %{githubver}
7-Release: 137+Release: 14
8Summary: Libbpf library8Summary: Libbpf library
9 9 
10License: LGPLv2 or BSD10License: LGPLv2 or BSD
@@ -36,6 +36,7 @@ Patch0019: backport-libbpf-Fix-hypothetical-STT_SECTION-extern-NULL-deref-c
36Patch0020: backport-libbpf-Adaption-for-sched-type-eBPF-program.patch36Patch0020: backport-libbpf-Adaption-for-sched-type-eBPF-program.patch
37Patch0021: backport-libbpf-Fix-buffer-overflow-in-bpf_object__init_prog.patch37Patch0021: backport-libbpf-Fix-buffer-overflow-in-bpf_object__init_prog.patch
38Patch0022: backport-libbpf-Fix-comment-about-arc-and-riscv-arch-in-bpf_tracing.h.patch38Patch0022: backport-libbpf-Fix-comment-about-arc-and-riscv-arch-in-bpf_tracing.h.patch
39+Patch0023: backport-libbpf-Store-zero-fd-to-fd_array-for-loader-kfunc-relocation.patch
39 40 
40# This package supersedes libbpf from kernel-tools,41# This package supersedes libbpf from kernel-tools,
41# which has default Epoch: 0. By having Epoch: 142# which has default Epoch: 0. By having Epoch: 1
@@ -88,6 +89,9 @@ developing applications that use %{name}
88%{_libdir}/libbpf.a89%{_libdir}/libbpf.a
89 90 
90%changelog91%changelog
92+* Fri Jun 12 2026 zhangwenyi <zhangwenyi@xfusion.com> 2:1.2.2-14
93+- libbpf: Store zero fd to fd_array for loader kfunc relocation
94+ 
91* Wed Jun 03 2026 gaoyongtao <gaoyongtao1@xfusion.com> 2:1.2.2-1395* Wed Jun 03 2026 gaoyongtao <gaoyongtao1@xfusion.com> 2:1.2.2-13
92- libbpf: Fix comment about arc and riscv arch in bpf_tracing.h96- libbpf: Fix comment about arc and riscv arch in bpf_tracing.h
93 97