• A
    libbpf: Refactor internal sec_def handling to enable pluggability · 119bc7f3
    Andrii Nakryiko 提交于
    mainline inclusion
    from mainline-5.16-rc1
    commit 12d9466d
    category: feature
    bugzilla: https://gitee.com/openeuler/kernel/issues/I5EUVD
    CVE: NA
    
    Reference: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=12d9466d8bf3d1d4b4fd0f5733b6fa0cc5ee1013
    
    -------------------------------------------------
    
    Refactor internals of libbpf to allow adding custom SEC() handling logic
    easily from outside of libbpf. To that effect, each SEC()-handling
    registration sets mandatory program type/expected attach type for
    a given prefix and can provide three callbacks called at different
    points of BPF program lifetime:
    
      - init callback for right after bpf_program is initialized and
      prog_type/expected_attach_type is set. This happens during
      bpf_object__open() step, close to the very end of constructing
      bpf_object, so all the libbpf APIs for querying and updating
      bpf_program properties should be available;
    
      - pre-load callback is called right before BPF_PROG_LOAD command is
      called in the kernel. This callbacks has ability to set both
      bpf_program properties, as well as program load attributes, overriding
      and augmenting the standard libbpf handling of them;
    
      - optional auto-attach callback, which makes a given SEC() handler
      support auto-attachment of a BPF program through bpf_program__attach()
      API and/or BPF skeletons <skel>__attach() method.
    
    Each callbacks gets a `long cookie` parameter passed in, which is
    specified during SEC() handling. This can be used by callbacks to lookup
    whatever additional information is necessary.
    
    This is not yet completely ready to be exposed to the outside world,
    mainly due to non-public nature of struct bpf_prog_load_params. Instead
    of making it part of public API, we'll wait until the planned low-level
    libbpf API improvements for BPF_PROG_LOAD and other typical bpf()
    syscall APIs, at which point we'll have a public, probably OPTS-based,
    way to fully specify BPF program load parameters, which will be used as
    an interface for custom pre-load callbacks.
    
    But this change itself is already a good first step to unify the BPF
    program hanling logic even within the libbpf itself. As one example, all
    the extra per-program type handling (sleepable bit, attach_btf_id
    resolution, unsetting optional expected attach type) is now more obvious
    and is gathered in one place.
    Signed-off-by: NAndrii Nakryiko <andrii@kernel.org>
    Signed-off-by: NAlexei Starovoitov <ast@kernel.org>
    Acked-by: NDave Marchevsky <davemarchevsky@fb.com>
    Link: https://lore.kernel.org/bpf/20210928161946.2512801-6-andrii@kernel.org
    (cherry picked from commit 12d9466d)
    Signed-off-by: NWang Yufen <wangyufen@huawei.com>
    
    Conflicts:
    	tools/lib/bpf/libbpf.c
    Signed-off-by: NWang Yufen <wangyufen@huawei.com>
    119bc7f3
libbpf.c 273.4 KB