• Andrii Nakryiko's avatar
    bpf: Support BPF ksym variables in kernel modules · 541c3bad
    Andrii Nakryiko authored
    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: default avatarAndrii Nakryiko <andrii@kernel.org>
    Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
    Acked-by: default avatarYonghong Song <yhs@fb.com>
    Acked-by: default avatarHao Luo <haoluo@google.com>
    Link: https://lore.kernel.org/bpf/20210112075520.4103414-6-andrii@kernel.org
    541c3bad
btf.c 151 KB