OSDN Git Service

libbpf: Store zero fd to fd_array for loader kfunc relocation
authorJiri Olsa <jolsa@kernel.org>
Mon, 15 May 2023 13:37:47 +0000 (15:37 +0200)
committerAlexei Starovoitov <ast@kernel.org>
Wed, 17 May 2023 05:09:23 +0000 (22:09 -0700)
commit10cb8622b6958c2d47961d6a42c76e6c9f1c08f3
treef63562bbb75646e53aa233cd40d9563bd9ba2275
parentde58ef414d8d7a0a635cd331b3b013d8216c4e60
libbpf: Store zero fd to fd_array for loader kfunc relocation

When moving some of the test kfuncs to bpf_testmod I hit an issue
when some of the kfuncs that object uses are in module and some
in vmlinux.

The problem is that both vmlinux and module kfuncs get allocated
btf_fd_idx index into fd_array, but we store to it the BTF fd value
only for module's kfunc, not vmlinux's one because (it's zero).

Then after the program is loaded we check if fd_array[btf_fd_idx] != 0
and close the fd.

When the object has kfuncs from both vmlinux and module, the fd from
fd_array[btf_fd_idx] from previous load will be stored in there for
vmlinux's kfunc, so we close unrelated fd (of the program we just
loaded in my case).

Fixing this by storing zero to fd_array[btf_fd_idx] for vmlinux
kfuncs, so the we won't close stale fd.

Signed-off-by: Jiri Olsa <jolsa@kernel.org>
Link: https://lore.kernel.org/r/20230515133756.1658301-2-jolsa@kernel.org
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
tools/lib/bpf/gen_loader.c