Commit Graph

10 Commits

Author SHA1 Message Date
Artem Savkov d4eaff3be5 libbpf: Support init of inner maps in light skeleton.
Bugzilla: https://bugzilla.redhat.com/2069046

Upstream Status: git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git

commit be05c94476f3cf4fdc29feab4ed1053187323296
Author: Alexei Starovoitov <ast@kernel.org>
Date:   Wed Dec 1 10:10:33 2021 -0800

    libbpf: Support init of inner maps in light skeleton.

    Add ability to initialize inner maps in light skeleton.

    Signed-off-by: Alexei Starovoitov <ast@kernel.org>
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Acked-by: Andrii Nakryiko <andrii@kernel.org>
    Link: https://lore.kernel.org/bpf/20211201181040.23337-11-alexei.starovoitov@gmail.com

Signed-off-by: Artem Savkov <asavkov@redhat.com>
2022-08-24 12:53:43 +02:00
Artem Savkov 733fb7a65c libbpf: Use CO-RE in the kernel in light skeleton.
Bugzilla: https://bugzilla.redhat.com/2069046

Upstream Status: git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git

commit d0e928876e30b18411b80fd2445424bc00e95745
Author: Alexei Starovoitov <ast@kernel.org>
Date:   Wed Dec 1 10:10:32 2021 -0800

    libbpf: Use CO-RE in the kernel in light skeleton.

    Without lskel the CO-RE relocations are processed by libbpf before any other
    work is done. Instead, when lskel is needed, remember relocation as RELO_CORE
    kind. Then when loader prog is generated for a given bpf program pass CO-RE
    relos of that program to gen loader via bpf_gen__record_relo_core(). The gen
    loader will remember them as-is and pass it later as-is into the kernel.

    The normal libbpf flow is to process CO-RE early before call relos happen. In
    case of gen_loader the core relos have to be added to other relos to be copied
    together when bpf static function is appended in different places to other main
    bpf progs. During the copy the append_subprog_relos() will adjust insn_idx for
    normal relos and for RELO_CORE kind too. When that is done each struct
    reloc_desc has good relos for specific main prog.

    Signed-off-by: Alexei Starovoitov <ast@kernel.org>
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Acked-by: Andrii Nakryiko <andrii@kernel.org>
    Link: https://lore.kernel.org/bpf/20211201181040.23337-10-alexei.starovoitov@gmail.com

Signed-off-by: Artem Savkov <asavkov@redhat.com>
2022-08-24 12:53:43 +02:00
Artem Savkov 5cc1ba0a17 libbpf: Unify low-level map creation APIs w/ new bpf_map_create()
Bugzilla: https://bugzilla.redhat.com/2069046

Upstream Status: git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git

commit 992c4225419a38663d6239bc2f525b4ac0429188
Author: Andrii Nakryiko <andrii@kernel.org>
Date:   Wed Nov 24 11:32:30 2021 -0800

    libbpf: Unify low-level map creation APIs w/ new bpf_map_create()

    Mark the entire zoo of low-level map creation APIs for deprecation in
    libbpf 0.7 ([0]) and introduce a new bpf_map_create() API that is
    OPTS-based (and thus future-proof) and matches the BPF_MAP_CREATE
    command name.

    While at it, ensure that gen_loader sends map_extra field. Also remove
    now unneeded btf_key_type_id/btf_value_type_id logic that libbpf is
    doing anyways.

      [0] Closes: https://github.com/libbpf/libbpf/issues/282

    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
    Link: https://lore.kernel.org/bpf/20211124193233.3115996-2-andrii@kernel.org

Signed-off-by: Artem Savkov <asavkov@redhat.com>
2022-08-24 12:53:39 +02:00
Artem Savkov afbb64a94a libbpf: Unify low-level BPF_PROG_LOAD APIs into bpf_prog_load()
Bugzilla: https://bugzilla.redhat.com/2069046

Upstream Status: git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git

commit d10ef2b825cffd0807dd733fdfd6a5bea32270d7
Author: Andrii Nakryiko <andrii@kernel.org>
Date:   Wed Nov 3 15:08:36 2021 -0700

    libbpf: Unify low-level BPF_PROG_LOAD APIs into bpf_prog_load()

    Add a new unified OPTS-based low-level API for program loading,
    bpf_prog_load() ([0]).  bpf_prog_load() accepts few "mandatory"
    parameters as input arguments (program type, name, license,
    instructions) and all the other optional (as in not required to specify
    for all types of BPF programs) fields into struct bpf_prog_load_opts.

    This makes all the other non-extensible APIs variant for BPF_PROG_LOAD
    obsolete and they are slated for deprecation in libbpf v0.7:
      - bpf_load_program();
      - bpf_load_program_xattr();
      - bpf_verify_program().

    Implementation-wise, internal helper libbpf__bpf_prog_load is refactored
    to become a public bpf_prog_load() API. struct bpf_prog_load_params used
    internally is replaced by public struct bpf_prog_load_opts.

    Unfortunately, while conceptually all this is pretty straightforward,
    the biggest complication comes from the already existing bpf_prog_load()
    *high-level* API, which has nothing to do with BPF_PROG_LOAD command.

    We try really hard to have a new API named bpf_prog_load(), though,
    because it maps naturally to BPF_PROG_LOAD command.

    For that, we rename old bpf_prog_load() into bpf_prog_load_deprecated()
    and mark it as COMPAT_VERSION() for shared library users compiled
    against old version of libbpf. Statically linked users and shared lib
    users compiled against new version of libbpf headers will get "rerouted"
    to bpf_prog_deprecated() through a macro helper that decides whether to
    use new or old bpf_prog_load() based on number of input arguments (see
    ___libbpf_overload in libbpf_common.h).

    To test that existing
    bpf_prog_load()-using code compiles and works as expected, I've compiled
    and ran selftests as is. I had to remove (locally) selftest/bpf/Makefile
    -Dbpf_prog_load=bpf_prog_test_load hack because it was conflicting with
    the macro-based overload approach. I don't expect anyone else to do
    something like this in practice, though. This is testing-specific way to
    replace bpf_prog_load() calls with special testing variant of it, which
    adds extra prog_flags value. After testing I kept this selftests hack,
    but ensured that we use a new bpf_prog_load_deprecated name for this.

    This patch also marks bpf_prog_load() and bpf_prog_load_xattr() as deprecated.
    bpf_object interface has to be used for working with struct bpf_program.
    Libbpf doesn't support loading just a bpf_program.

    The silver lining is that when we get to libbpf 1.0 all these
    complication will be gone and we'll have one clean bpf_prog_load()
    low-level API with no backwards compatibility hackery surrounding it.

      [0] Closes: https://github.com/libbpf/libbpf/issues/284

    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>
    Link: https://lore.kernel.org/bpf/20211103220845.2676888-4-andrii@kernel.org

Signed-off-by: Artem Savkov <asavkov@redhat.com>
2022-08-24 12:53:33 +02:00
Yauheni Kaliuta 1a99baba3a libbpf: Perform map fd cleanup for gen_loader in case of error
Bugzilla: http://bugzilla.redhat.com/2069045

commit ba05fd36b8512d6aeefe9c2c5b6a25b726c4bfff
Author: Kumar Kartikeya Dwivedi <memxor@gmail.com>
Date:   Sat Nov 13 04:50:22 2021 +0530

    libbpf: Perform map fd cleanup for gen_loader in case of error
    
    Alexei reported a fd leak issue in gen loader (when invoked from
    bpftool) [0]. When adding ksym support, map fd allocation was moved from
    stack to loader map, however I missed closing these fds (relevant when
    cleanup label is jumped to on error). For the success case, the
    allocated fd is returned in loader ctx, hence this problem is not
    noticed.
    
    Make three changes, first MAX_USED_MAPS in MAX_FD_ARRAY_SZ instead of
    MAX_USED_PROGS, the braino was not a problem until now for this case as
    we didn't try to close map fds (otherwise use of it would have tried
    closing 32 additional fds in ksym btf fd range). Then, do a cleanup for
    all nr_maps fds in cleanup label code, so that in case of error all
    temporary map fds from bpf_gen__map_create are closed.
    
    Then, adjust the cleanup label to only generate code for the required
    number of program and map fds.  To trim code for remaining program
    fds, lay out prog_fd array in stack in the end, so that we can
    directly skip the remaining instances.  Still stack size remains same,
    since changing that would require changes in a lot of places
    (including adjustment of stack_off macro), so nr_progs_sz variable is
    only used to track required number of iterations (and jump over
    cleanup size calculated from that), stack offset calculation remains
    unaffected.
    
    The difference for test_ksyms_module.o is as follows:
    libbpf: //prog cleanup iterations: before = 34, after = 5
    libbpf: //maps cleanup iterations: before = 64, after = 2
    
    Also, move allocation of gen->fd_array offset to bpf_gen__init. Since
    offset can now be 0, and we already continue even if add_data returns 0
    in case of failure, we do not need to distinguish between 0 offset and
    failure case 0, as we rely on bpf_gen__finish to check errors. We can
    also skip check for gen->fd_array in add_*_fd functions, since
    bpf_gen__init will take care of it.
    
      [0]: https://lore.kernel.org/bpf/CAADnVQJ6jSitKSNKyxOrUzwY2qDRX0sPkJ=VLGHuCLVJ=qOt9g@mail.gmail.com
    
    Fixes: 18f4fccbf314 ("libbpf: Update gen_loader to emit BTF_KIND_FUNC relocations")
    Reported-by: Alexei Starovoitov <ast@kernel.org>
    Signed-off-by: Kumar Kartikeya Dwivedi <memxor@gmail.com>
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>
    Link: https://lore.kernel.org/bpf/20211112232022.899074-1-memxor@gmail.com

Signed-off-by: Yauheni Kaliuta <ykaliuta@redhat.com>
2022-06-03 17:23:53 +03:00
Yauheni Kaliuta 541e80729c libbpf: Add typeless ksym support to gen_loader
Bugzilla: http://bugzilla.redhat.com/2069045

commit c24941cd3766b6de682dbe6809bd6af12271ab5b
Author: Kumar Kartikeya Dwivedi <memxor@gmail.com>
Date:   Thu Oct 28 12:04:55 2021 +0530

    libbpf: Add typeless ksym support to gen_loader
    
    This uses the bpf_kallsyms_lookup_name helper added in previous patches
    to relocate typeless ksyms. The return value ENOENT can be ignored, and
    the value written to 'res' can be directly stored to the insn, as it is
    overwritten to 0 on lookup failure. For repeating symbols, we can simply
    copy the previously populated bpf_insn.
    
    Also, we need to take care to not close fds for typeless ksym_desc, so
    reuse the 'off' member's space to add a marker for typeless ksym and use
    that to skip them in cleanup_relos.
    
    We add a emit_ksym_relo_log helper that avoids duplicating common
    logging instructions between typeless and weak ksym (for future commit).
    
    Signed-off-by: Kumar Kartikeya Dwivedi <memxor@gmail.com>
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>
    Link: https://lore.kernel.org/bpf/20211028063501.2239335-3-memxor@gmail.com

Signed-off-by: Yauheni Kaliuta <ykaliuta@redhat.com>
2022-06-03 17:23:48 +03:00
Yauheni Kaliuta cb058f57c3 libbpf: Add "map_extra" as a per-map-type extra flag
Bugzilla: http://bugzilla.redhat.com/2069045

commit 47512102cde2d252d7b984d9675cfd3420b48ad9
Author: Joanne Koong <joannekoong@fb.com>
Date:   Wed Oct 27 16:45:01 2021 -0700

    libbpf: Add "map_extra" as a per-map-type extra flag
    
    This patch adds the libbpf infrastructure for supporting a
    per-map-type "map_extra" field, whose definition will be
    idiosyncratic depending on map type.
    
    For example, for the bloom filter map, the lower 4 bits of
    map_extra is used to denote the number of hash functions.
    
    Please note that until libbpf 1.0 is here, the
    "bpf_create_map_params" struct is used as a temporary
    means for propagating the map_extra field to the kernel.
    
    Signed-off-by: Joanne Koong <joannekoong@fb.com>
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>
    Acked-by: Andrii Nakryiko <andrii@kernel.org>
    Link: https://lore.kernel.org/bpf/20211027234504.30744-3-joannekoong@fb.com

Signed-off-by: Yauheni Kaliuta <ykaliuta@redhat.com>
2022-06-03 17:23:48 +03:00
Yauheni Kaliuta 0aaee4947e libbpf: Update gen_loader to emit BTF_KIND_FUNC relocations
Bugzilla: http://bugzilla.redhat.com/2069045

commit 18f4fccbf314fdb07d276f4cd3eaf53f1825550d
Author: Kumar Kartikeya Dwivedi <memxor@gmail.com>
Date:   Sat Oct 2 06:47:56 2021 +0530

    libbpf: Update gen_loader to emit BTF_KIND_FUNC relocations
    
    This change updates the BPF syscall loader to relocate BTF_KIND_FUNC
    relocations, with support for weak kfunc relocations. The general idea
    is to move map_fds to loader map, and also use the data for storing
    kfunc BTF fds. Since both reuse the fd_array parameter, they need to be
    kept together.
    
    For map_fds, we reserve MAX_USED_MAPS slots in a region, and for kfunc,
    we reserve MAX_KFUNC_DESCS. This is done so that insn->off has more
    chances of being <= INT16_MAX than treating data map as a sparse array
    and adding fd as needed.
    
    When the MAX_KFUNC_DESCS limit is reached, we fall back to the sparse
    array model, so that as long as it does remain <= INT16_MAX, we pass an
    index relative to the start of fd_array.
    
    We store all ksyms in an array where we try to avoid calling the
    bpf_btf_find_by_name_kind helper, and also reuse the BTF fd that was
    already stored. This also speeds up the loading process compared to
    emitting calls in all cases, in later tests.
    
    Signed-off-by: Kumar Kartikeya Dwivedi <memxor@gmail.com>
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>
    Link: https://lore.kernel.org/bpf/20211002011757.311265-9-memxor@gmail.com

Signed-off-by: Yauheni Kaliuta <ykaliuta@redhat.com>
2022-06-03 17:23:38 +03:00
Alexei Starovoitov 30f51aedab libbpf: Cleanup temp FDs when intermediate sys_bpf fails.
Fix loader program to close temporary FDs when intermediate
sys_bpf command fails.

Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Acked-by: Andrii Nakryiko <andrii@kernel.org>
Link: https://lore.kernel.org/bpf/20210514003623.28033-16-alexei.starovoitov@gmail.com
2021-05-19 00:40:44 +02:00
Alexei Starovoitov 6723474373 libbpf: Generate loader program out of BPF ELF file.
The BPF program loading process performed by libbpf is quite complex
and consists of the following steps:
"open" phase:
- parse elf file and remember relocations, sections
- collect externs and ksyms including their btf_ids in prog's BTF
- patch BTF datasec (since llvm couldn't do it)
- init maps (old style map_def, BTF based, global data map, kconfig map)
- collect relocations against progs and maps
"load" phase:
- probe kernel features
- load vmlinux BTF
- resolve externs (kconfig and ksym)
- load program BTF
- init struct_ops
- create maps
- apply CO-RE relocations
- patch ld_imm64 insns with src_reg=PSEUDO_MAP, PSEUDO_MAP_VALUE, PSEUDO_BTF_ID
- reposition subprograms and adjust call insns
- sanitize and load progs

During this process libbpf does sys_bpf() calls to load BTF, create maps,
populate maps and finally load programs.
Instead of actually doing the syscalls generate a trace of what libbpf
would have done and represent it as the "loader program".
The "loader program" consists of single map with:
- union bpf_attr(s)
- BTF bytes
- map value bytes
- insns bytes
and single bpf program that passes bpf_attr(s) and data into bpf_sys_bpf() helper.
Executing such "loader program" via bpf_prog_test_run() command will
replay the sequence of syscalls that libbpf would have done which will result
the same maps created and programs loaded as specified in the elf file.
The "loader program" removes libelf and majority of libbpf dependency from
program loading process.

kconfig, typeless ksym, struct_ops and CO-RE are not supported yet.

The order of relocate_data and relocate_calls had to change, so that
bpf_gen__prog_load() can see all relocations for a given program with
correct insn_idx-es.

Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Acked-by: Andrii Nakryiko <andrii@kernel.org>
Link: https://lore.kernel.org/bpf/20210514003623.28033-15-alexei.starovoitov@gmail.com
2021-05-19 00:39:40 +02:00