Commit Graph

75 Commits

Author SHA1 Message Date
Yauheni Kaliuta 23969fb532 libbpf: Fix is_pow_of_2
Bugzilla: https://bugzilla.redhat.com/2120968

commit 611edf1bacc51355ccb497915695db7f869cb382
Author: Yuze Chi <chiyuze@google.com>
Date:   Thu Jun 2 22:51:56 2022 -0700

    libbpf: Fix is_pow_of_2
    
    Move the correct definition from linker.c into libbpf_internal.h.
    
    Fixes: 0087a681fa8c ("libbpf: Automatically fix up BPF_MAP_TYPE_RINGBUF size, if necessary")
    Reported-by: Yuze Chi <chiyuze@google.com>
    Signed-off-by: Yuze Chi <chiyuze@google.com>
    Signed-off-by: Ian Rogers <irogers@google.com>
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Link: https://lore.kernel.org/bpf/20220603055156.2830463-1-irogers@google.com

Signed-off-by: Yauheni Kaliuta <ykaliuta@redhat.com>
2022-11-30 12:47:07 +02:00
Yauheni Kaliuta 7abfc3e7ee libbpf: Avoid joining .BTF.ext data with BPF programs by section name
Bugzilla: https://bugzilla.redhat.com/2120968

commit 11d5daa89254ba2233d422777d52dbf24666b280
Author: Andrii Nakryiko <andrii@kernel.org>
Date:   Mon Apr 25 17:45:05 2022 -0700

    libbpf: Avoid joining .BTF.ext data with BPF programs by section name
    
    Instead of using ELF section names as a joining key between .BTF.ext and
    corresponding BPF programs, pre-build .BTF.ext section number to ELF
    section index mapping during bpf_object__open() and use it later for
    matching .BTF.ext information (func/line info or CO-RE relocations) to
    their respective BPF programs and subprograms.
    
    This simplifies corresponding joining logic and let's libbpf do
    manipulations with BPF program's ELF sections like dropping leading '?'
    character for non-autoloaded programs. Original joining logic in
    bpf_object__relocate_core() (see relevant comment that's now removed)
    was never elegant, so it's a good improvement regardless. But it also
    avoids unnecessary internal assumptions about preserving original ELF
    section name as BPF program's section name (which was broken when
    SEC("?abc") support was added).
    
    Fixes: a3820c481112 ("libbpf: Support opting out from autoloading BPF programs declaratively")
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>
    Link: https://lore.kernel.org/bpf/20220426004511.2691730-5-andrii@kernel.org

Signed-off-by: Yauheni Kaliuta <ykaliuta@redhat.com>
2022-11-30 12:47:00 +02:00
Yauheni Kaliuta b58b45669b libbpf: Fix usdt_cookie being cast to 32 bits
Bugzilla: https://bugzilla.redhat.com/2120968

commit 5af25a410acb8d34acb11024d752f0ea3491decf
Author: Pu Lehui <pulehui@huawei.com>
Date:   Tue Apr 19 22:52:37 2022 +0800

    libbpf: Fix usdt_cookie being cast to 32 bits
    
    The usdt_cookie is defined as __u64, which should not be
    used as a long type because it will be cast to 32 bits
    in 32-bit platforms.
    
    Signed-off-by: Pu Lehui <pulehui@huawei.com>
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Link: https://lore.kernel.org/bpf/20220419145238.482134-2-pulehui@huawei.com

Signed-off-by: Yauheni Kaliuta <ykaliuta@redhat.com>
2022-11-28 16:52:10 +02:00
Yauheni Kaliuta 62e1620e1f libbpf: Improve library identification for uprobe binary path resolution
Bugzilla: https://bugzilla.redhat.com/2120968

commit a1c9d61b19cbc0b9618c0a0400c304ecb63221d5
Author: Alan Maguire <alan.maguire@oracle.com>
Date:   Wed Apr 6 12:43:49 2022 +0100

    libbpf: Improve library identification for uprobe binary path resolution
    
    In the process of doing path resolution for uprobe attach, libraries are
    identified by matching a ".so" substring in the binary_path.
    This matches a lot of patterns that do not conform to library.so[.version]
    format, so instead match a ".so" _suffix_, and if that fails match a
    ".so." substring for the versioned library case.
    
    Suggested-by: Andrii Nakryiko <andrii@kernel.org>
    Signed-off-by: Alan Maguire <alan.maguire@oracle.com>
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Link: https://lore.kernel.org/bpf/1649245431-29956-2-git-send-email-alan.maguire@oracle.com

Signed-off-by: Yauheni Kaliuta <ykaliuta@redhat.com>
2022-11-28 16:48:57 +02:00
Yauheni Kaliuta 9a0807fadf libbpf: Wire up USDT API and bpf_link integration
Bugzilla: https://bugzilla.redhat.com/2120968

commit 2e4913e025fdef740972ac70277297436cccb27f
Author: Andrii Nakryiko <andrii@kernel.org>
Date:   Mon Apr 4 16:41:57 2022 -0700

    libbpf: Wire up USDT API and bpf_link integration
    
    Wire up libbpf USDT support APIs without yet implementing all the
    nitty-gritty details of USDT discovery, spec parsing, and BPF map
    initialization.
    
    User-visible user-space API is simple and is conceptually very similar
    to uprobe API.
    
    bpf_program__attach_usdt() API allows to programmatically attach given
    BPF program to a USDT, specified through binary path (executable or
    shared lib), USDT provider and name. Also, just like in uprobe case, PID
    filter is specified (0 - self, -1 - any process, or specific PID).
    Optionally, USDT cookie value can be specified. Such single API
    invocation will try to discover given USDT in specified binary and will
    use (potentially many) BPF uprobes to attach this program in correct
    locations.
    
    Just like any bpf_program__attach_xxx() APIs, bpf_link is returned that
    represents this attachment. It is a virtual BPF link that doesn't have
    direct kernel object, as it can consist of multiple underlying BPF
    uprobe links. As such, attachment is not atomic operation and there can
    be brief moment when some USDT call sites are attached while others are
    still in the process of attaching. This should be taken into
    consideration by user. But bpf_program__attach_usdt() guarantees that
    in the case of success all USDT call sites are successfully attached, or
    all the successfuly attachments will be detached as soon as some USDT
    call sites failed to be attached. So, in theory, there could be cases of
    failed bpf_program__attach_usdt() call which did trigger few USDT
    program invocations. This is unavoidable due to multi-uprobe nature of
    USDT and has to be handled by user, if it's important to create an
    illusion of atomicity.
    
    USDT BPF programs themselves are marked in BPF source code as either
    SEC("usdt"), in which case they won't be auto-attached through
    skeleton's <skel>__attach() method, or it can have a full definition,
    which follows the spirit of fully-specified uprobes:
    SEC("usdt/<path>:<provider>:<name>"). In the latter case skeleton's
    attach method will attempt auto-attachment. Similarly, generic
    bpf_program__attach() will have enought information to go off of for
    parameterless attachment.
    
    USDT BPF programs are actually uprobes, and as such for kernel they are
    marked as BPF_PROG_TYPE_KPROBE.
    
    Another part of this patch is USDT-related feature probing:
      - BPF cookie support detection from user-space;
      - detection of kernel support for auto-refcounting of USDT semaphore.
    
    The latter is optional. If kernel doesn't support such feature and USDT
    doesn't rely on USDT semaphores, no error is returned. But if libbpf
    detects that USDT requires setting semaphores and kernel doesn't support
    this, libbpf errors out with explicit pr_warn() message. Libbpf doesn't
    support poking process's memory directly to increment semaphore value,
    like BCC does on legacy kernels, due to inherent raciness and danger of
    such process memory manipulation. Libbpf let's kernel take care of this
    properly or gives up.
    
    Logistically, all the extra USDT-related infrastructure of libbpf is put
    into a separate usdt.c file and abstracted behind struct usdt_manager.
    Each bpf_object has lazily-initialized usdt_manager pointer, which is
    only instantiated if USDT programs are attempted to be attached. Closing
    BPF object frees up usdt_manager resources. usdt_manager keeps track of
    USDT spec ID assignment and few other small things.
    
    Subsequent patches will fill out remaining missing pieces of USDT
    initialization and setup logic.
    
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>
    Reviewed-by: Alan Maguire <alan.maguire@oracle.com>
    Link: https://lore.kernel.org/bpf/20220404234202.331384-3-andrii@kernel.org

Signed-off-by: Yauheni Kaliuta <ykaliuta@redhat.com>
2022-11-28 16:48:56 +02:00
Jerome Marchand a7cd0c515c libbpf: Add libbpf_kallsyms_parse function
Bugzilla: https://bugzilla.redhat.com/2120966

commit 85153ac06283408e6ccaf002b02fd85f0bdab94b
Author: Jiri Olsa <jolsa@kernel.org>
Date:   Wed Mar 16 13:24:13 2022 +0100

    libbpf: Add libbpf_kallsyms_parse function

    Move the kallsyms parsing in internal libbpf_kallsyms_parse
    function, so it can be used from other places.

    It will be used in following changes.

    Signed-off-by: Jiri Olsa <jolsa@kernel.org>
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>
    Acked-by: Andrii Nakryiko <andrii@kernel.org>
    Link: https://lore.kernel.org/bpf/20220316122419.933957-8-jolsa@kernel.org

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
2022-10-25 14:58:04 +02:00
Jerome Marchand afb792fe6c libbpf: Expose bpf_core_{add,free}_cands() to bpftool
Bugzilla: https://bugzilla.redhat.com/2120966

commit 8de6cae40bce6e19f39de60056cad39a7274169d
Author: Mauricio Vásquez <mauricio@kinvolk.io>
Date:   Tue Feb 15 17:58:51 2022 -0500

    libbpf: Expose bpf_core_{add,free}_cands() to bpftool

    Expose bpf_core_add_cands() and bpf_core_free_cands() to handle
    candidates list.

    Signed-off-by: Mauricio Vásquez <mauricio@kinvolk.io>
    Signed-off-by: Rafael David Tinoco <rafael.tinoco@aquasec.com>
    Signed-off-by: Lorenzo Fontana <lorenzo.fontana@elastic.co>
    Signed-off-by: Leonardo Di Donato <leonardo.didonato@elastic.co>
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Acked-by: Andrii Nakryiko <andrii@kernel.org>
    Link: https://lore.kernel.org/bpf/20220215225856.671072-3-mauricio@kinvolk.io

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
2022-10-25 14:57:50 +02:00
Jerome Marchand 7fab43dbfd libbpf: hide and discourage inconsistently named getters
Bugzilla: https://bugzilla.redhat.com/2120966

commit 20eccf29e2979a18411517061998bac7d12c8543
Author: Andrii Nakryiko <andrii@kernel.org>
Date:   Mon Jan 24 11:42:48 2022 -0800

    libbpf: hide and discourage inconsistently named getters

    Move a bunch of "getters" into libbpf_legacy.h to keep them there in
    libbpf 1.0. See [0] for discussion of "Discouraged APIs". These getters
    don't add any maintenance burden and are simple alias, but they are
    inconsistent in naming. So keep them in libbpf_legacy.h instead of
    libbpf.h to "hide" them in favor of preferred getters ([1]). Also add two
    missing getters: bpf_program__type() and bpf_program__expected_attach_type().

      [0] https://github.com/libbpf/libbpf/wiki/Libbpf:-the-road-to-v1.0#handling-deprecation-of-apis-and-functionality
      [1] Closes: https://github.com/libbpf/libbpf/issues/307

    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Link: https://lore.kernel.org/r/20220124194254.2051434-2-andrii@kernel.org
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
2022-10-25 14:57:43 +02:00
Artem Savkov a2ab03a151 libbpf: Improve LINUX_VERSION_CODE detection
Bugzilla: https://bugzilla.redhat.com/2069046

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

commit 5b3d72987701d51bf31823b39db49d10970f5c2d
Author: Andrii Nakryiko <andrii@kernel.org>
Date:   Wed Dec 22 15:10:03 2021 -0800

    libbpf: Improve LINUX_VERSION_CODE detection

    Ubuntu reports incorrect kernel version through uname(), which on older
    kernels leads to kprobe BPF programs failing to load due to the version
    check mismatch.

    Accommodate Ubuntu's quirks with LINUX_VERSION_CODE by using
    Ubuntu-specific /proc/version_code to fetch major/minor/patch versions
    to form LINUX_VERSION_CODE.

    While at it, consolide libbpf's kernel version detection code between
    libbpf.c and libbpf_probes.c.

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

    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>
    Acked-by: Yonghong Song <yhs@fb.com>
    Link: https://lore.kernel.org/bpf/20211222231003.2334940-1-andrii@kernel.org

Signed-off-by: Artem Savkov <asavkov@redhat.com>
2022-08-24 12:53:51 +02:00
Artem Savkov 136c624b20 libbpf: Auto-bump RLIMIT_MEMLOCK if kernel needs it for BPF
Bugzilla: https://bugzilla.redhat.com/2069046

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

commit e542f2c4cd16d49392abf3349341d58153d3c603
Author: Andrii Nakryiko <andrii@kernel.org>
Date:   Tue Dec 14 11:59:03 2021 -0800

    libbpf: Auto-bump RLIMIT_MEMLOCK if kernel needs it for BPF

    The need to increase RLIMIT_MEMLOCK to do anything useful with BPF is
    one of the first extremely frustrating gotchas that all new BPF users go
    through and in some cases have to learn it a very hard way.

    Luckily, starting with upstream Linux kernel version 5.11, BPF subsystem
    dropped the dependency on memlock and uses memcg-based memory accounting
    instead. Unfortunately, detecting memcg-based BPF memory accounting is
    far from trivial (as can be evidenced by this patch), so in practice
    most BPF applications still do unconditional RLIMIT_MEMLOCK increase.

    As we move towards libbpf 1.0, it would be good to allow users to forget
    about RLIMIT_MEMLOCK vs memcg and let libbpf do the sensible adjustment
    automatically. This patch paves the way forward in this matter. Libbpf
    will do feature detection of memcg-based accounting, and if detected,
    will do nothing. But if the kernel is too old, just like BCC, libbpf
    will automatically increase RLIMIT_MEMLOCK on behalf of user
    application ([0]).

    As this is technically a breaking change, during the transition period
    applications have to opt into libbpf 1.0 mode by setting
    LIBBPF_STRICT_AUTO_RLIMIT_MEMLOCK bit when calling
    libbpf_set_strict_mode().

    Libbpf allows to control the exact amount of set RLIMIT_MEMLOCK limit
    with libbpf_set_memlock_rlim_max() API. Passing 0 will make libbpf do
    nothing with RLIMIT_MEMLOCK. libbpf_set_memlock_rlim_max() has to be
    called before the first bpf_prog_load(), bpf_btf_load(), or
    bpf_object__load() call, otherwise it has no effect and will return
    -EBUSY.

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

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

Signed-off-by: Artem Savkov <asavkov@redhat.com>
2022-08-24 12:53:48 +02:00
Artem Savkov 8b7632b54c libbpf: Add sane strncpy alternative and use it internally
Bugzilla: https://bugzilla.redhat.com/2069046

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

commit 9fc205b413b3f3e9502fa92151fba63b91230454
Author: Andrii Nakryiko <andrii@kernel.org>
Date:   Fri Dec 10 16:40:43 2021 -0800

    libbpf: Add sane strncpy alternative and use it internally

    strncpy() has a notoriously error-prone semantics which makes GCC
    complain about it a lot (and quite often completely completely falsely
    at that). Instead of pleasing GCC all the time (-Wno-stringop-truncation
    is unfortunately only supported by GCC, so it's a bit too messy to just
    enable it in Makefile), add libbpf-internal libbpf_strlcpy() helper
    which follows what FreeBSD's strlcpy() does and what most people would
    expect from strncpy(): copies up to N-1 first bytes from source string
    into destination string and ensures zero-termination afterwards.

    Replace all the relevant uses of strncpy/strncat/memcpy in libbpf with
    libbpf_strlcpy().

    This also fixes the issue reported by Emmanuel Deloget in xsk.c where
    memcpy() could access source string beyond its end.

    Fixes: 2f6324a393 (libbpf: Support shared umems between queues and devices)
    Reported-by: Emmanuel Deloget <emmanuel.deloget@eho.link>
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
    Link: https://lore.kernel.org/bpf/20211211004043.2374068-1-andrii@kernel.org

Signed-off-by: Artem Savkov <asavkov@redhat.com>
2022-08-24 12:53:48 +02:00
Artem Savkov fea0f73c03 libbpf: Allow passing preallocated log_buf when loading BTF into kernel
Bugzilla: https://bugzilla.redhat.com/2069046

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

commit 1a190d1e8eb9ff84354e38f7482dc77b626f3cc9
Author: Andrii Nakryiko <andrii@kernel.org>
Date:   Thu Dec 9 11:38:31 2021 -0800

    libbpf: Allow passing preallocated log_buf when loading BTF into kernel

    Add libbpf-internal btf_load_into_kernel() that allows to pass
    preallocated log_buf and custom log_level to be passed into kernel
    during BPF_BTF_LOAD call. When custom log_buf is provided,
    btf_load_into_kernel() won't attempt an retry with automatically
    allocated internal temporary buffer to capture BTF validation log.

    It's important to note the relation between log_buf and log_level, which
    slightly deviates from stricter kernel logic. From kernel's POV, if
    log_buf is specified, log_level has to be > 0, and vice versa. While
    kernel has good reasons to request such "sanity, this, in practice, is
    a bit unconvenient and restrictive for libbpf's high-level bpf_object APIs.

    So libbpf will allow to set non-NULL log_buf and log_level == 0. This is
    fine and means to attempt to load BTF without logging requested, but if
    it failes, retry the load with custom log_buf and log_level 1. Similar
    logic will be implemented for program loading. In practice this means
    that users can provide custom log buffer just in case error happens, but
    not really request slower verbose logging all the time. This is also
    consistent with libbpf behavior when custom log_buf is not set: libbpf
    first tries to load everything with log_level=0, and only if error
    happens allocates internal log buffer and retries with log_level=1.

    Also, while at it, make BTF validation log more obvious and follow the log
    pattern libbpf is using for dumping BPF verifier log during
    BPF_PROG_LOAD. BTF loading resulting in an error will look like this:

    libbpf: BTF loading error: -22
    libbpf: -- BEGIN BTF LOAD LOG ---
    magic: 0xeb9f
    version: 1
    flags: 0x0
    hdr_len: 24
    type_off: 0
    type_len: 1040
    str_off: 1040
    str_len: 2063598257
    btf_total_size: 1753
    Total section length too long
    -- END BTF LOAD LOG --
    libbpf: Error loading .BTF into kernel: -22. BTF is optional, ignoring.

    This makes it much easier to find relevant parts in libbpf log output.

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

Signed-off-by: Artem Savkov <asavkov@redhat.com>
2022-08-24 12:53:45 +02:00
Artem Savkov e8268612e0 libbpf: Replace btf__type_by_id() with btf_type_by_id().
Bugzilla: https://bugzilla.redhat.com/2069046

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

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

    libbpf: Replace btf__type_by_id() with btf_type_by_id().

    To prepare relo_core.c to be compiled in the kernel and the user space
    replace btf__type_by_id with btf_type_by_id.

    In libbpf btf__type_by_id and btf_type_by_id have different behavior.

    bpf_core_apply_relo_insn() needs behavior of uapi btf__type_by_id
    vs internal btf_type_by_id, but type_id range check is already done
    in bpf_core_apply_relo(), so it's safe to replace it everywhere.
    The kernel btf_type_by_id() does the check anyway. It doesn't hurt.

    Suggested-by: Andrii Nakryiko <andrii@kernel.org>
    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-2-alexei.starovoitov@gmail.com

Signed-off-by: Artem Savkov <asavkov@redhat.com>
2022-08-24 12:53:42 +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 88a2fca700 libbpf: Support BTF_KIND_TYPE_TAG
Bugzilla: https://bugzilla.redhat.com/2069046

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

commit 2dc1e488e5cdfd937554ca81fd46ad874d244b3f
Author: Yonghong Song <yhs@fb.com>
Date:   Thu Nov 11 17:26:14 2021 -0800

    libbpf: Support BTF_KIND_TYPE_TAG

    Add libbpf support for BTF_KIND_TYPE_TAG.

    Signed-off-by: Yonghong Song <yhs@fb.com>
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>
    Acked-by: Andrii Nakryiko <andrii@kernel.org>
    Link: https://lore.kernel.org/bpf/20211112012614.1505315-1-yhs@fb.com

Signed-off-by: Artem Savkov <asavkov@redhat.com>
2022-08-24 12:53:36 +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 98e337968d libbpf: Ensure that BPF syscall fds are never 0, 1, or 2
Bugzilla: http://bugzilla.redhat.com/2069045

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

    libbpf: Ensure that BPF syscall fds are never 0, 1, or 2
    
    Add a simple wrapper for passing an fd and getting a new one >= 3 if it
    is one of 0, 1, or 2. There are two primary reasons to make this change:
    First, libbpf relies on the assumption a certain BPF fd is never 0 (e.g.
    most recently noticed in [0]). Second, Alexei pointed out in [1] that
    some environments reset stdin, stdout, and stderr if they notice an
    invalid fd at these numbers. To protect against both these cases, switch
    all internal BPF syscall wrappers in libbpf to always return an fd >= 3.
    We only need to modify the syscall wrappers and not other code that
    assumes a valid fd by doing >= 0, to avoid pointless churn, and because
    it is still a valid assumption. The cost paid is two additional syscalls
    if fd is in range [0, 2].
    
      [0]: e31eec77e4ab ("bpf: selftests: Fix fd cleanup in get_branch_snapshot")
      [1]: https://lore.kernel.org/bpf/CAADnVQKVKY8o_3aU8Gzke443+uHa-eGoM0h7W4srChMXU1S4Bg@mail.gmail.com
    
    Signed-off-by: Kumar Kartikeya Dwivedi <memxor@gmail.com>
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>
    Acked-by: Song Liu <songliubraving@fb.com>
    Acked-by: Andrii Nakryiko <andrii@kernel.org>
    Link: https://lore.kernel.org/bpf/20211028063501.2239335-5-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 b7085850a5 libbpf: Deprecate btf__finalize_data() and move it into libbpf.c
Bugzilla: http://bugzilla.redhat.com/2069045

commit b96c07f3b5ae6944eb52fd96a322340aa80aef5d
Author: Andrii Nakryiko <andrii@kernel.org>
Date:   Wed Oct 20 18:43:55 2021 -0700

    libbpf: Deprecate btf__finalize_data() and move it into libbpf.c
    
    There isn't a good use case where anyone but libbpf itself needs to call
    btf__finalize_data(). It was implemented for internal use and it's not
    clear why it was made into public API in the first place. To function, it
    requires active ELF data, which is stored inside bpf_object for the
    duration of opening phase only. But the only BTF that needs bpf_object's
    ELF is that bpf_object's BTF itself, which libbpf fixes up automatically
    during bpf_object__open() operation anyways. There is no need for any
    additional fix up and no reasonable scenario where it's useful and
    appropriate.
    
    Thus, btf__finalize_data() is just an API atavism and is better removed.
    So this patch marks it as deprecated immediately (v0.6+) and moves the
    code from btf.c into libbpf.c where it's used in the context of
    bpf_object opening phase. Such code co-location allows to make code
    structure more straightforward and remove bpf_object__section_size() and
    bpf_object__variable_offset() internal helpers from libbpf_internal.h,
    making them static. Their naming is also adjusted to not create
    a wrong illusion that they are some sort of method of bpf_object. They
    are internal helpers and are called appropriately.
    
    This is part of libbpf 1.0 effort ([0]).
    
      [0] Closes: https://github.com/libbpf/libbpf/issues/276
    
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>
    Acked-by: Song Liu <songliubraving@fb.com>
    Link: https://lore.kernel.org/bpf/20211021014404.2635234-2-andrii@kernel.org

Signed-off-by: Yauheni Kaliuta <ykaliuta@redhat.com>
2022-06-03 17:23:44 +03:00
Yauheni Kaliuta 3100283a2a libbpf: Use Elf64-specific types explicitly for dealing with ELF
Bugzilla: http://bugzilla.redhat.com/2069045

commit ad23b7238474c6319bf692ae6ce037d9696df1d1
Author: Andrii Nakryiko <andrii@kernel.org>
Date:   Wed Oct 20 18:43:57 2021 -0700

    libbpf: Use Elf64-specific types explicitly for dealing with ELF
    
    Minimize the usage of class-agnostic gelf_xxx() APIs from libelf. These
    APIs require copying ELF data structures into local GElf_xxx structs and
    have a more cumbersome API. BPF ELF file is defined to be always 64-bit
    ELF object, even when intended to be run on 32-bit host architectures,
    so there is no need to do class-agnostic conversions everywhere. BPF
    static linker implementation within libbpf has been using Elf64-specific
    types since initial implementation.
    
    Add two simple helpers, elf_sym_by_idx() and elf_rel_by_idx(), for more
    succinct direct access to ELF symbol and relocation records within ELF
    data itself and switch all the GElf_xxx usage into Elf64_xxx
    equivalents. The only remaining place within libbpf.c that's still using
    gelf API is gelf_getclass(), as there doesn't seem to be a direct way to
    get underlying ELF bitness.
    
    No functional changes intended.
    
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>
    Acked-by: Song Liu <songliubraving@fb.com>
    Link: https://lore.kernel.org/bpf/20211021014404.2635234-4-andrii@kernel.org

Signed-off-by: Yauheni Kaliuta <ykaliuta@redhat.com>
2022-06-03 17:23:43 +03:00
Yauheni Kaliuta fdbdd94ffe bpf: Rename BTF_KIND_TAG to BTF_KIND_DECL_TAG
Bugzilla: http://bugzilla.redhat.com/2069045

commit 223f903e9c832699f4e5f422281a60756c1c6cfe
Author: Yonghong Song <yhs@fb.com>
Date:   Tue Oct 12 09:48:38 2021 -0700

    bpf: Rename BTF_KIND_TAG to BTF_KIND_DECL_TAG
    
    Patch set [1] introduced BTF_KIND_TAG to allow tagging
    declarations for struct/union, struct/union field, var, func
    and func arguments and these tags will be encoded into
    dwarf. They are also encoded to btf by llvm for the bpf target.
    
    After BTF_KIND_TAG is introduced, we intended to use it
    for kernel __user attributes. But kernel __user is actually
    a type attribute. Upstream and internal discussion showed
    it is not a good idea to mix declaration attribute and
    type attribute. So we proposed to introduce btf_type_tag
    as a type attribute and existing btf_tag renamed to
    btf_decl_tag ([2]).
    
    This patch renamed BTF_KIND_TAG to BTF_KIND_DECL_TAG and some
    other declarations with *_tag to *_decl_tag to make it clear
    the tag is for declaration. In the future, BTF_KIND_TYPE_TAG
    might be introduced per [3].
    
     [1] https://lore.kernel.org/bpf/20210914223004.244411-1-yhs@fb.com/
     [2] https://reviews.llvm.org/D111588
     [3] https://reviews.llvm.org/D111199
    
    Fixes: b5ea834dde6b ("bpf: Support for new btf kind BTF_KIND_TAG")
    Fixes: 5b84bd10363e ("libbpf: Add support for BTF_KIND_TAG")
    Fixes: 5c07f2fec003 ("bpftool: Add support for BTF_KIND_TAG")
    Signed-off-by: Yonghong Song <yhs@fb.com>
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>
    Link: https://lore.kernel.org/bpf/20211012164838.3345699-1-yhs@fb.com

Signed-off-by: Yauheni Kaliuta <ykaliuta@redhat.com>
2022-06-03 17:23:42 +03:00
Yauheni Kaliuta c4ff36ae66 libbpf: Support kernel module function calls
Bugzilla: http://bugzilla.redhat.com/2069045

commit 9dbe6015636c19f929a7f7b742f27f303ff6069d
Author: Kumar Kartikeya Dwivedi <memxor@gmail.com>
Date:   Sat Oct 2 06:47:54 2021 +0530

    libbpf: Support kernel module function calls
    
    This patch adds libbpf support for kernel module function call support.
    The fd_array parameter is used during BPF program load to pass module
    BTFs referenced by the program. insn->off is set to index into this
    array, but starts from 1, because insn->off as 0 is reserved for
    btf_vmlinux.
    
    We try to use existing insn->off for a module, since the kernel limits
    the maximum distinct module BTFs for kfuncs to 256, and also because
    index must never exceed the maximum allowed value that can fit in
    insn->off (INT16_MAX). In the future, if kernel interprets signed offset
    as unsigned for kfunc calls, this limit can be increased to UINT16_MAX.
    
    Also introduce a btf__find_by_name_kind_own helper to start searching
    from module BTF's start id when we know that the BTF ID is not present
    in vmlinux BTF (in find_ksym_btf_id).
    
    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-7-memxor@gmail.com

Signed-off-by: Yauheni Kaliuta <ykaliuta@redhat.com>
2022-06-03 17:23:38 +03:00
Yauheni Kaliuta faf53499b1 libbpf: Reduce reliance of attach_fns on sec_def internals
Bugzilla: http://bugzilla.redhat.com/2069045

commit 13d35a0cf1741431333ba4aa9bce9c5bbc88f63b
Author: Andrii Nakryiko <andrii@kernel.org>
Date:   Tue Sep 28 09:19:42 2021 -0700

    libbpf: Reduce reliance of attach_fns on sec_def internals
    
    Move closer to not relying on bpf_sec_def internals that won't be part
    of public API, when pluggable SEC() handlers will be allowed. Drop
    pre-calculated prefix length, and in various helpers don't rely on this
    prefix length availability. Also minimize reliance on knowing
    bpf_sec_def's prefix for few places where section prefix shortcuts are
    supported (e.g., tp vs tracepoint, raw_tp vs raw_tracepoint).
    
    Given checking some string for having a given string-constant prefix is
    such a common operation and so annoying to be done with pure C code, add
    a small macro helper, str_has_pfx(), and reuse it throughout libbpf.c
    where prefix comparison is performed. With __builtin_constant_p() it's
    possible to have a convenient helper that checks some string for having
    a given prefix, where prefix is either string literal (or compile-time
    known string due to compiler optimization) or just a runtime string
    pointer, which is quite convenient and saves a lot of typing and string
    literal duplication.
    
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>
    Acked-by: Dave Marchevsky <davemarchevsky@fb.com>
    Link: https://lore.kernel.org/bpf/20210928161946.2512801-7-andrii@kernel.org

Signed-off-by: Yauheni Kaliuta <ykaliuta@redhat.com>
2022-06-03 17:16:17 +03:00
Yauheni Kaliuta 0881d7268d libbpf: Add support for BTF_KIND_TAG
Bugzilla: http://bugzilla.redhat.com/2069045

commit 5b84bd10363e36ceb7c4c1ae749a3fc8adf8df45
Author: Yonghong Song <yhs@fb.com>
Date:   Tue Sep 14 15:30:25 2021 -0700

    libbpf: Add support for BTF_KIND_TAG
    
    Add BTF_KIND_TAG support for parsing and dedup.
    Also added sanitization for BTF_KIND_TAG. If BTF_KIND_TAG is not
    supported in the kernel, sanitize it to INTs.
    
    Signed-off-by: Yonghong Song <yhs@fb.com>
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>
    Acked-by: Andrii Nakryiko <andrii@kernel.org>
    Link: https://lore.kernel.org/bpf/20210914223025.246687-1-yhs@fb.com

Signed-off-by: Yauheni Kaliuta <ykaliuta@redhat.com>
2022-06-03 17:16:12 +03:00
Yauheni Kaliuta 5c60c7d608 libbpf: Fix build with latest gcc/binutils with LTO
Bugzilla: http://bugzilla.redhat.com/2069045

commit 006a5099fc18a43792b01d002e1e45c5541ea666
Author: Andrii Nakryiko <andrii@kernel.org>
Date:   Tue Sep 7 15:10:23 2021 -0700

    libbpf: Fix build with latest gcc/binutils with LTO
    
    After updating to binutils 2.35, the build began to fail with an
    assembler error. A bug was opened on the Red Hat Bugzilla a few days
    later for the same issue.
    
    Work around the problem by using the new `symver` attribute (introduced
    in GCC 10) as needed instead of assembler directives.
    
    This addresses Red Hat ([0]) and OpenSUSE ([1]) bug reports, as well as libbpf
    issue ([2]).
    
      [0]: https://bugzilla.redhat.com/show_bug.cgi?id=1863059
      [1]: https://bugzilla.opensuse.org/show_bug.cgi?id=1188749
      [2]: Closes: https://github.com/libbpf/libbpf/issues/338
    
    Co-developed-by: Patrick McCarty <patrick.mccarty@intel.com>
    Co-developed-by: Michal Suchanek <msuchanek@suse.de>
    Signed-off-by: Patrick McCarty <patrick.mccarty@intel.com>
    Signed-off-by: Michal Suchanek <msuchanek@suse.de>
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>
    Link: https://lore.kernel.org/bpf/20210907221023.2660953-1-andrii@kernel.org

Signed-off-by: Yauheni Kaliuta <ykaliuta@redhat.com>
2022-06-03 17:15:38 +03:00
Jerome Marchand 610ff02061 libbpf: Add bpf_cookie support to bpf_link_create() API
Bugzilla: http://bugzilla.redhat.com/2041365

commit 3ec84f4b1638495ebff068a668dc417b4de5727e
Author: Andrii Nakryiko <andrii@kernel.org>
Date:   Sun Aug 15 00:06:03 2021 -0700

    libbpf: Add bpf_cookie support to bpf_link_create() API

    Add ability to specify bpf_cookie value when creating BPF perf link with
    bpf_link_create() low-level API.

    Given BPF_LINK_CREATE command is growing and keeps getting new fields that are
    specific to the type of BPF_LINK, extend libbpf side of bpf_link_create() API
    and corresponding OPTS struct to accomodate such changes. Add extra checks to
    prevent using incompatible/unexpected combinations of fields.

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

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
2022-04-29 18:14:41 +02:00
Jerome Marchand 8119c855cd libbpf: Split CO-RE logic into relo_core.c.
Bugzilla: http://bugzilla.redhat.com/2041365

commit b0588390dbcedcd74fab6ffb8afe8d52380fd8b6
Author: Alexei Starovoitov <ast@kernel.org>
Date:   Tue Jul 20 17:08:22 2021 -0700

    libbpf: Split CO-RE logic into relo_core.c.

    Move CO-RE logic into separate file.
    The internal interface between libbpf and CO-RE is through
    bpf_core_apply_relo_insn() function and few structs defined in relo_core.h.

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

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
2022-04-29 18:14:35 +02:00
Jerome Marchand d1346edc61 libbpf: Move CO-RE types into relo_core.h.
Bugzilla: http://bugzilla.redhat.com/2041365

commit 301ba4d710284e088d278adc477b7edad834577f
Author: Alexei Starovoitov <ast@kernel.org>
Date:   Tue Jul 20 17:08:21 2021 -0700

    libbpf: Move CO-RE types into relo_core.h.

    In order to make a clean split of CO-RE logic move its types
    into independent header file.

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

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
2022-04-29 18:14:35 +02:00
David S. Miller a52171ae7b Merge git://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf-next
Daniel Borkmann says:

====================
pull-request: bpf-next 2021-06-17

The following pull-request contains BPF updates for your *net-next* tree.

We've added 50 non-merge commits during the last 25 day(s) which contain
a total of 148 files changed, 4779 insertions(+), 1248 deletions(-).

The main changes are:

1) BPF infrastructure to migrate TCP child sockets from a listener to another
   in the same reuseport group/map, from Kuniyuki Iwashima.

2) Add a provably sound, faster and more precise algorithm for tnum_mul() as
   noted in https://arxiv.org/abs/2105.05398, from Harishankar Vishwanathan.

3) Streamline error reporting changes in libbpf as planned out in the
   'libbpf: the road to v1.0' effort, from Andrii Nakryiko.

4) Add broadcast support to xdp_redirect_map(), from Hangbin Liu.

5) Extends bpf_map_lookup_and_delete_elem() functionality to 4 more map
   types, that is, {LRU_,PERCPU_,LRU_PERCPU_,}HASH, from Denis Salopek.

6) Support new LLVM relocations in libbpf to make them more linker friendly,
   also add a doc to describe the BPF backend relocations, from Yonghong Song.

7) Silence long standing KUBSAN complaints on register-based shifts in
   interpreter, from Daniel Borkmann and Eric Biggers.

8) Add dummy PT_REGS macros in libbpf to fail BPF program compilation when
   target arch cannot be determined, from Lorenz Bauer.

9) Extend AF_XDP to support large umems with 1M+ pages, from Magnus Karlsson.

10) Fix two minor libbpf tc BPF API issues, from Kumar Kartikeya Dwivedi.

11) Move libbpf BPF_SEQ_PRINTF/BPF_SNPRINTF macros that can be used by BPF
    programs to bpf_helpers.h header, from Florent Revest.
====================

Signed-off-by: David S. Miller <davem@davemloft.net>
2021-06-17 11:54:56 -07:00
Jakub Kicinski 5ada57a9a6 Merge git://git.kernel.org/pub/scm/linux/kernel/git/netdev/net
cdc-wdm: s/kill_urbs/poison_urbs/ to fix build

Signed-off-by: Jakub Kicinski <kuba@kernel.org>
2021-05-27 09:55:10 -07:00
Andrii Nakryiko e9fc3ce99b libbpf: Streamline error reporting for high-level APIs
Implement changes to error reporting for high-level libbpf APIs to make them
less surprising and less error-prone to users:
  - in all the cases when error happens, errno is set to an appropriate error
    value;
  - in libbpf 1.0 mode, all pointer-returning APIs return NULL on error and
    error code is communicated through errno; this applies both to APIs that
    already returned NULL before (so now they communicate more detailed error
    codes), as well as for many APIs that used ERR_PTR() macro and encoded
    error numbers as fake pointers.
  - in legacy (default) mode, those APIs that were returning ERR_PTR(err),
    continue doing so, but still set errno.

With these changes, errno can be always used to extract actual error,
regardless of legacy or libbpf 1.0 modes. This is utilized internally in
libbpf in places where libbpf uses it's own high-level APIs.
libbpf_get_error() is adapted to handle both cases completely transparently to
end-users (and is used by libbpf consistently as well).

More context, justification, and discussion can be found in "Libbpf: the road
to v1.0" document ([0]).

  [0] https://docs.google.com/document/d/1UyjTZuPFWiPFyKk1tV5an11_iaRuec6U-ZESZ54nNTY

Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Acked-by: John Fastabend <john.fastabend@gmail.com>
Acked-by: Toke Høiland-Jørgensen <toke@redhat.com>
Link: https://lore.kernel.org/bpf/20210525035935.1461796-5-andrii@kernel.org
2021-05-25 17:32:35 -07:00
Andrii Nakryiko f12b654327 libbpf: Streamline error reporting for low-level APIs
Ensure that low-level APIs behave uniformly across the libbpf as follows:
  - in case of an error, errno is always set to the correct error code;
  - when libbpf 1.0 mode is enabled with LIBBPF_STRICT_DIRECT_ERRS option to
    libbpf_set_strict_mode(), return -Exxx error value directly, instead of -1;
  - by default, until libbpf 1.0 is released, keep returning -1 directly.

More context, justification, and discussion can be found in "Libbpf: the road
to v1.0" document ([0]).

  [0] https://docs.google.com/document/d/1UyjTZuPFWiPFyKk1tV5an11_iaRuec6U-ZESZ54nNTY

Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Acked-by: John Fastabend <john.fastabend@gmail.com>
Acked-by: Toke Høiland-Jørgensen <toke@redhat.com>
Link: https://lore.kernel.org/bpf/20210525035935.1461796-4-andrii@kernel.org
2021-05-25 17:32:35 -07:00
Yonghong Song 9f0c317f6a libbpf: Add support for new llvm bpf relocations
LLVM patch https://reviews.llvm.org/D102712
narrowed the scope of existing R_BPF_64_64
and R_BPF_64_32 relocations, and added three
new relocations, R_BPF_64_ABS64, R_BPF_64_ABS32
and R_BPF_64_NODYLD32. The main motivation is
to make relocations linker friendly.

This change, unfortunately, breaks libbpf build,
and we will see errors like below:
  libbpf: ELF relo #0 in section #6 has unexpected type 2 in
     /home/yhs/work/bpf-next/tools/testing/selftests/bpf/bpf_tcp_nogpl.o
  Error: failed to link
     '/home/yhs/work/bpf-next/tools/testing/selftests/bpf/bpf_tcp_nogpl.o':
     Unknown error -22 (-22)
The new relocation R_BPF_64_ABS64 is generated
and libbpf linker sanity check doesn't understand it.
Relocation section '.rel.struct_ops' at offset 0x1410 contains 1 entries:
    Offset             Info             Type               Symbol's Value  Symbol's Name
0000000000000018  0000000700000002 R_BPF_64_ABS64         0000000000000000 nogpltcp_init

Look at the selftests/bpf/bpf_tcp_nogpl.c,
  void BPF_STRUCT_OPS(nogpltcp_init, struct sock *sk)
  {
  }

  SEC(".struct_ops")
  struct tcp_congestion_ops bpf_nogpltcp = {
          .init           = (void *)nogpltcp_init,
          .name           = "bpf_nogpltcp",
  };
The new llvm relocation scheme categorizes 'nogpltcp_init' reference
as R_BPF_64_ABS64 instead of R_BPF_64_64 which is used to specify
ld_imm64 relocation in the new scheme.

Let us fix the linker sanity checking by including
R_BPF_64_ABS64 and R_BPF_64_ABS32. There is no need to
check R_BPF_64_NODYLD32 which is used for .BTF and .BTF.ext.

Signed-off-by: Yonghong Song <yhs@fb.com>
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Acked-by: John Fastabend <john.fastabend@gmail.com>
Link: https://lore.kernel.org/bpf/20210522162341.3687617-1-yhs@fb.com
2021-05-24 21:03:05 -07: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
Arnaldo Carvalho de Melo 67e7ec0bd4 libbpf: Provide GELF_ST_VISIBILITY() define for older libelf
Where that macro isn't available.

Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Link: https://lore.kernel.org/bpf/YJaspEh0qZr4LYOc@kernel.org
2021-05-11 23:07:33 +02:00
Andrii Nakryiko 42869d2852 libbpf: Make few internal helpers available outside of libbpf.c
Make skip_mods_and_typedefs(), btf_kind_str(), and btf_func_linkage() helpers
available outside of libbpf.c, to be used by static linker code.

Also do few cleanups (error code fixes, comment clean up, etc) that don't
deserve their own commit.

Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Acked-by: Yonghong Song <yhs@fb.com>
Link: https://lore.kernel.org/bpf/20210423181348.1801389-9-andrii@kernel.org
2021-04-23 14:05:26 -07:00
Andrii Nakryiko c7ef5ec957 libbpf: Refactor BTF map definition parsing
Refactor BTF-defined maps parsing logic to allow it to be nicely reused by BPF
static linker. Further, at least for BPF static linker, it's important to know
which attributes of a BPF map were defined explicitly, so provide a bit set
for each known portion of BTF map definition. This allows BPF static linker to
do a simple check when dealing with extern map declarations.

The same capabilities allow to distinguish attributes explicitly set to zero
(e.g., __uint(max_entries, 0)) vs the case of not specifying it at all (no
max_entries attribute at all). Libbpf is currently not utilizing that, but it
could be useful for backwards compatibility reasons later.

Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Acked-by: Yonghong Song <yhs@fb.com>
Link: https://lore.kernel.org/bpf/20210423181348.1801389-7-andrii@kernel.org
2021-04-23 14:05:26 -07:00
Andrii Nakryiko aea28a602f libbpf: Mark BPF subprogs with hidden visibility as static for BPF verifier
Define __hidden helper macro in bpf_helpers.h, which is a short-hand for
__attribute__((visibility("hidden"))). Add libbpf support to mark BPF
subprograms marked with __hidden as static in BTF information to enforce BPF
verifier's static function validation algorithm, which takes more information
(caller's context) into account during a subprogram validation.

Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Acked-by: Yonghong Song <yhs@fb.com>
Link: https://lore.kernel.org/bpf/20210423181348.1801389-5-andrii@kernel.org
2021-04-23 14:05:26 -07:00
Andrii Nakryiko faf6ed321c libbpf: Add BPF static linker APIs
Introduce BPF static linker APIs to libbpf. BPF static linker allows to
perform static linking of multiple BPF object files into a single combined
resulting object file, preserving all the BPF programs, maps, global
variables, etc.

Data sections (.bss, .data, .rodata, .maps, maps, etc) with the same name are
concatenated together. Similarly, code sections are also concatenated. All the
symbols and ELF relocations are also concatenated in their respective ELF
sections and are adjusted accordingly to the new object file layout.

Static variables and functions are handled correctly as well, adjusting BPF
instructions offsets to reflect new variable/function offset within the
combined ELF section. Such relocations are referencing STT_SECTION symbols and
that stays intact.

Data sections in different files can have different alignment requirements, so
that is taken care of as well, adjusting sizes and offsets as necessary to
satisfy both old and new alignment requirements.

DWARF data sections are stripped out, currently. As well as LLLVM_ADDRSIG
section, which is ignored by libbpf in bpf_object__open() anyways. So, in
a way, BPF static linker is an analogue to `llvm-strip -g`, which is a pretty
nice property, especially if resulting .o file is then used to generate BPF
skeleton.

Original string sections are ignored and instead we construct our own set of
unique strings using libbpf-internal `struct strset` API.

To reduce the size of the patch, all the .BTF and .BTF.ext processing was
moved into a separate patch.

The high-level API consists of just 4 functions:
  - bpf_linker__new() creates an instance of BPF static linker. It accepts
    output filename and (currently empty) options struct;
  - bpf_linker__add_file() takes input filename and appends it to the already
    processed ELF data; it can be called multiple times, one for each BPF
    ELF object file that needs to be linked in;
  - bpf_linker__finalize() needs to be called to dump final ELF contents into
    the output file, specified when bpf_linker was created; after
    bpf_linker__finalize() is called, no more bpf_linker__add_file() and
    bpf_linker__finalize() calls are allowed, they will return error;
  - regardless of whether bpf_linker__finalize() was called or not,
    bpf_linker__free() will free up all the used resources.

Currently, BPF static linker doesn't resolve cross-object file references
(extern variables and/or functions). This will be added in the follow up patch
set.

Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Link: https://lore.kernel.org/bpf/20210318194036.3521577-7-andrii@kernel.org
2021-03-18 16:14:22 -07:00
Andrii Nakryiko 3b029e06f6 libbpf: Rename internal memory-management helpers
Rename btf_add_mem() and btf_ensure_mem() helpers that abstract away details
of dynamically resizable memory to use libbpf_ prefix, as they are not
BTF-specific. No functional changes.

Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Link: https://lore.kernel.org/bpf/20210318194036.3521577-4-andrii@kernel.org
2021-03-18 16:14:22 -07:00
Andrii Nakryiko f36e99a45d libbpf: Generalize BTF and BTF.ext type ID and strings iteration
Extract and generalize the logic to iterate BTF type ID and string offset
fields within BTF types and .BTF.ext data. Expose this internally in libbpf
for re-use by bpf_linker.

Additionally, complete strings deduplication handling for BTF.ext (e.g., CO-RE
access strings), which was previously missing. There previously was no
case of deduplicating .BTF.ext data, but bpf_linker is going to use it.

Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Link: https://lore.kernel.org/bpf/20210318194036.3521577-3-andrii@kernel.org
2021-03-18 16:14:22 -07:00
Andrii Nakryiko e14ef4bf01 libbpf: Expose btf_type_by_id() internally
btf_type_by_id() is internal-only convenience API returning non-const pointer
to struct btf_type. Expose it outside of btf.c for re-use.

Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Link: https://lore.kernel.org/bpf/20210318194036.3521577-2-andrii@kernel.org
2021-03-18 16:14:22 -07:00
Ilya Leoshkevich 22541a9eeb libbpf: Add BTF_KIND_FLOAT support
The logic follows that of BTF_KIND_INT most of the time. Sanitization
replaces BTF_KIND_FLOATs with equally-sized empty BTF_KIND_STRUCTs on
older kernels, for example, the following:

    [4] FLOAT 'float' size=4

becomes the following:

    [4] STRUCT '(anon)' size=4 vlen=0

With dwarves patch [1] and this patch, the older kernels, which were
failing with the floating-point-related errors, will now start working
correctly.

[1] https://github.com/iii-i/dwarves/commit/btf-kind-float-v2

Signed-off-by: Ilya Leoshkevich <iii@linux.ibm.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Acked-by: Andrii Nakryiko <andrii@kernel.org>
Link: https://lore.kernel.org/bpf/20210226202256.116518-4-iii@linux.ibm.com
2021-03-04 17:58:15 -08:00
Andrii Nakryiko 91abb4a6d7 libbpf: Support attachment of BPF tracing programs to kernel modules
Teach libbpf to search for BTF types in kernel modules for tracing BPF
programs. This allows attachment of raw_tp/fentry/fexit/fmod_ret/etc BPF
program types to tracepoints and functions in kernel modules.

Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Link: https://lore.kernel.org/bpf/20201203204634.1325171-13-andrii@kernel.org
2020-12-03 17:38:21 -08:00
Andrii Nakryiko 6aef10a481 libbpf: Factor out low-level BPF program loading helper
Refactor low-level API for BPF program loading to not rely on public API
types. This allows painless extension without constant efforts to cleverly not
break backwards compatibility.

Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Link: https://lore.kernel.org/bpf/20201203204634.1325171-12-andrii@kernel.org
2020-12-03 17:38:21 -08:00
Andrii Nakryiko a19f93cfaf libbpf: Add internal helper to load BTF data by FD
Add a btf_get_from_fd() helper, which constructs struct btf from in-kernel BTF
data by FD. This is used for loading module BTFs.

Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Link: https://lore.kernel.org/bpf/20201203204634.1325171-4-andrii@kernel.org
2020-12-03 17:38:20 -08:00
Andrii Nakryiko 9c6c5c48d7 libbpf: Make btf_dump work with modifiable BTF
Ensure that btf_dump can accommodate new BTF types being appended to BTF
instance after struct btf_dump was created. This came up during attemp to
use btf_dump for raw type dumping in selftests, but given changes are not
excessive, it's good to not have any gotchas in API usage, so I decided to
support such use case in general.

Signed-off-by: Andrii Nakryiko <andriin@fb.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Link: https://lore.kernel.org/bpf/20200929232843.1249318-2-andriin@fb.com
2020-09-30 12:30:22 -07:00
Andrii Nakryiko 192f5a1fe6 libbpf: Generalize common logic for managing dynamically-sized arrays
Managing dynamically-sized array is a common, but not trivial functionality,
which significant amount of logic and code to implement properly. So instead
of re-implementing it all the time, extract it into a helper function ans
reuse.

Signed-off-by: Andrii Nakryiko <andriin@fb.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Acked-by: John Fastabend <john.fastabend@gmail.com>
Link: https://lore.kernel.org/bpf/20200926011357.2366158-4-andriin@fb.com
2020-09-28 17:27:31 -07:00
Song Liu 88f7fe7233 libbpf: Support test run of raw tracepoint programs
Add bpf_prog_test_run_opts() with support of new fields in bpf_attr.test,
namely, flags and cpu. Also extend _opts operations to support outputs via
opts.

Signed-off-by: Song Liu <songliubraving@fb.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Acked-by: Andrii Nakryiko <andriin@fb.com>
Link: https://lore.kernel.org/bpf/20200925205432.1777-3-songliubraving@fb.com
2020-09-28 21:52:36 +02:00
Andrii Nakryiko dda1ec9fc7 libbpf: Fix libbpf build on compilers missing __builtin_mul_overflow
GCC compilers older than version 5 don't support __builtin_mul_overflow yet.
Given GCC 4.9 is the minimal supported compiler for building kernel and the
fact that libbpf is a dependency of resolve_btfids, which is dependency of
CONFIG_DEBUG_INFO_BTF=y, this needs to be handled. This patch fixes the issue
by falling back to slower detection of integer overflow in such cases.

Fixes: 029258d7b2 ("libbpf: Remove any use of reallocarray() in libbpf")
Signed-off-by: Andrii Nakryiko <andriin@fb.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Acked-by: Yonghong Song <yhs@fb.com>
Link: https://lore.kernel.org/bpf/20200820061411.1755905-2-andriin@fb.com
2020-08-20 16:45:09 +02:00