bpf: Support BPF ksym variables in kernel modules
authorAndrii Nakryiko <andrii@kernel.org>
Tue, 12 Jan 2021 07:55:18 +0000 (23:55 -0800)
committerAlexei Starovoitov <ast@kernel.org>
Wed, 13 Jan 2021 01:24:30 +0000 (17:24 -0800)
commit541c3bad8dc51b253ba8686d0cd7628e6b9b5f4c
tree4ba7ff0adceb638a353df96eeded5ac9586460b1
parent635599bace259a2c42741c3ea61bfa7be6f15556
bpf: Support BPF ksym variables in kernel modules

Add support for directly accessing kernel module variables from BPF programs
using special ldimm64 instructions. This functionality builds upon vmlinux
ksym support, but extends ldimm64 with src_reg=BPF_PSEUDO_BTF_ID to allow
specifying kernel module BTF's FD in insn[1].imm field.

During BPF program load time, verifier will resolve FD to BTF object and will
take reference on BTF object itself and, for module BTFs, corresponding module
as well, to make sure it won't be unloaded from under running BPF program. The
mechanism used is similar to how bpf_prog keeps track of used bpf_maps.

One interesting change is also in how per-CPU variable is determined. The
logic is to find .data..percpu data section in provided BTF, but both vmlinux
and module each have their own .data..percpu entries in BTF. So for module's
case, the search for DATASEC record needs to look at only module's added BTF
types. This is implemented with custom search function.

Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Acked-by: Yonghong Song <yhs@fb.com>
Acked-by: Hao Luo <haoluo@google.com>
Link: https://lore.kernel.org/bpf/20210112075520.4103414-6-andrii@kernel.org
include/linux/bpf.h
include/linux/bpf_verifier.h
include/linux/btf.h
kernel/bpf/btf.c
kernel/bpf/core.c
kernel/bpf/verifier.c