Commit Graph

630 Commits

Author SHA1 Message Date
Yauheni Kaliuta 6193c840a6 libbpf: Fix uprobe symbol file offset calculation logic
Bugzilla: https://bugzilla.redhat.com/2120968

commit fe92833524e368e59bba9c57e00f7359f133667f
Author: Andrii Nakryiko <andrii@kernel.org>
Date:   Mon Jun 6 15:01:43 2022 -0700

    libbpf: Fix uprobe symbol file offset calculation logic
    
    Fix libbpf's bpf_program__attach_uprobe() logic of determining
    function's *file offset* (which is what kernel is actually expecting)
    when attaching uprobe/uretprobe by function name. Previously calculation
    was determining virtual address offset relative to base load address,
    which (offset) is not always the same as file offset (though very
    frequently it is which is why this went unnoticed for a while).
    
    Fixes: 433966e3ae04 ("libbpf: Support function name-based attach uprobes")
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
    Cc: Riham Selim <rihams@fb.com>
    Cc: Alan Maguire <alan.maguire@oracle.com>
    Link: https://lore.kernel.org/bpf/20220606220143.3796908-1-andrii@kernel.org

Signed-off-by: Yauheni Kaliuta <ykaliuta@redhat.com>
2022-11-30 12:47:07 +02:00
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 c4cfa2bb92 libbpf: Fix typo in comment
Bugzilla: https://bugzilla.redhat.com/2120968

commit bb412cf1d712656f27b2a08c492ed9d7591485aa
Author: Julia Lawall <Julia.Lawall@inria.fr>
Date:   Sat May 21 13:11:21 2022 +0200

    libbpf: Fix typo in comment
    
    Spelling mistake (triple letters) in comment.
    Detected with the help of Coccinelle.
    
    Signed-off-by: Julia Lawall <Julia.Lawall@inria.fr>
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Acked-by: Daniel Müller <deso@posteo.net>
    Link: https://lore.kernel.org/bpf/20220521111145.81697-71-Julia.Lawall@inria.fr

Signed-off-by: Yauheni Kaliuta <ykaliuta@redhat.com>
2022-11-30 12:47:06 +02:00
Yauheni Kaliuta fe368a2b5a libbpf: fix memory leak in attach_tp for target-less tracepoint program
Bugzilla: https://bugzilla.redhat.com/2120968

commit ac6a65868a5a45db49d5ee8524df3b701110d844
Author: Andrii Nakryiko <andrii@kernel.org>
Date:   Mon May 16 11:45:47 2022 -0700

    libbpf: fix memory leak in attach_tp for target-less tracepoint program
    
    Fix sec_name memory leak if user defines target-less SEC("tp").
    
    Fixes: 9af8efc45eb1 ("libbpf: Allow "incomplete" basic tracing SEC() definitions")
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Acked-by: David Vernet <void@manifault.com>
    Link: https://lore.kernel.org/r/20220516184547.3204674-1-andrii@kernel.org
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>

Signed-off-by: Yauheni Kaliuta <ykaliuta@redhat.com>
2022-11-30 12:47:04 +02:00
Yauheni Kaliuta 7b1740d9da libbpf: Add safer high-level wrappers for map operations
Bugzilla: https://bugzilla.redhat.com/2120968

commit 737d0646a83cdc65c070a9de61a1ef106cca5ff1
Author: Andrii Nakryiko <andrii@kernel.org>
Date:   Thu May 12 15:07:12 2022 -0700

    libbpf: Add safer high-level wrappers for map operations
    
    Add high-level API wrappers for most common and typical BPF map
    operations that works directly on instances of struct bpf_map * (so
    you don't have to call bpf_map__fd()) and validate key/value size
    expectations.
    
    These helpers require users to specify key (and value, where
    appropriate) sizes when performing lookup/update/delete/etc. This forces
    user to actually think and validate (for themselves) those. This is
    a good thing as user is expected by kernel to implicitly provide correct
    key/value buffer sizes and kernel will just read/write necessary amount
    of data. If it so happens that user doesn't set up buffers correctly
    (which bit people for per-CPU maps especially) kernel either randomly
    overwrites stack data or return -EFAULT, depending on user's luck and
    circumstances. These high-level APIs are meant to prevent such
    unpleasant and hard to debug bugs.
    
    This patch also adds bpf_map_delete_elem_flags() low-level API and
    requires passing flags to bpf_map__delete_elem() API for consistency
    across all similar APIs, even though currently kernel doesn't expect
    any extra flags for BPF_MAP_DELETE_ELEM operation.
    
    List of map operations that get these high-level APIs:
    
      - bpf_map_lookup_elem;
      - bpf_map_update_elem;
      - bpf_map_delete_elem;
      - bpf_map_lookup_and_delete_elem;
      - bpf_map_get_next_key.
    
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
    Link: https://lore.kernel.org/bpf/20220512220713.2617964-1-andrii@kernel.org

Signed-off-by: Yauheni Kaliuta <ykaliuta@redhat.com>
2022-11-30 12:47:04 +02:00
Yauheni Kaliuta 778d04d8eb libbpf: Assign cookies to links in libbpf.
Bugzilla: https://bugzilla.redhat.com/2120968

commit 129b9c5ee2c18c3e36ec289140b5149f301118d1
Author: Kui-Feng Lee <kuifeng@fb.com>
Date:   Tue May 10 13:59:22 2022 -0700

    libbpf: Assign cookies to links in libbpf.
    
    Add a cookie field to the attributes of bpf_link_create().
    Add bpf_program__attach_trace_opts() to attach a cookie to a link.
    
    Signed-off-by: Kui-Feng Lee <kuifeng@fb.com>
    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/20220510205923.3206889-5-kuifeng@fb.com

Signed-off-by: Yauheni Kaliuta <ykaliuta@redhat.com>
2022-11-30 12:47:03 +02:00
Yauheni Kaliuta 278de3770c libbpf: Add bpf_program__set_insns function
Bugzilla: https://bugzilla.redhat.com/2120968

commit b63b3c490eeeedd324e194929bd0aa8ba553f875
Author: Jiri Olsa <jolsa@kernel.org>
Date:   Tue May 10 09:46:57 2022 +0200

    libbpf: Add bpf_program__set_insns function
    
    Adding bpf_program__set_insns that allows to set new instructions
    for a BPF program.
    
    This is a very advanced libbpf API and users need to know what
    they are doing. This should be used from prog_prepare_load_fn
    callback only.
    
    We can have changed instructions after calling prog_prepare_load_fn
    callback, reloading them.
    
    One of the users of this new API will be perf's internal BPF prologue
    generation.
    
    Signed-off-by: Jiri Olsa <jolsa@kernel.org>
    Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
    Acked-by: Andrii Nakryiko <andrii@kernel.org>
    Link: https://lore.kernel.org/bpf/20220510074659.2557731-2-jolsa@kernel.org

Signed-off-by: Yauheni Kaliuta <ykaliuta@redhat.com>
2022-11-30 12:47:03 +02:00
Yauheni Kaliuta 3064622c71 libbpf: Clean up ringbuf size adjustment implementation
Bugzilla: https://bugzilla.redhat.com/2120968

commit 5eefe17c7ae41bac4d2d281669e8357a10f4d5a4
Author: Andrii Nakryiko <andrii@kernel.org>
Date:   Tue May 10 11:51:59 2022 -0700

    libbpf: Clean up ringbuf size adjustment implementation
    
    Drop unused iteration variable, move overflow prevention check into the
    for loop.
    
    Fixes: 0087a681fa8c ("libbpf: Automatically fix up BPF_MAP_TYPE_RINGBUF size, if necessary")
    Reported-by: Nathan Chancellor <nathan@kernel.org>
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
    Link: https://lore.kernel.org/bpf/20220510185159.754299-1-andrii@kernel.org

Signed-off-by: Yauheni Kaliuta <ykaliuta@redhat.com>
2022-11-30 12:47:03 +02:00
Yauheni Kaliuta 08b12fb600 libbpf: Automatically fix up BPF_MAP_TYPE_RINGBUF size, if necessary
Bugzilla: https://bugzilla.redhat.com/2120968

commit 0087a681fa8c22f719a567317e8f8f894d734b9c
Author: Andrii Nakryiko <andrii@kernel.org>
Date:   Sun May 8 17:41:47 2022 -0700

    libbpf: Automatically fix up BPF_MAP_TYPE_RINGBUF size, if necessary
    
    Kernel imposes a pretty particular restriction on ringbuf map size. It
    has to be a power-of-2 multiple of page size. While generally this isn't
    hard for user to satisfy, sometimes it's impossible to do this
    declaratively in BPF source code or just plain inconvenient to do at
    runtime.
    
    One such example might be BPF libraries that are supposed to work on
    different architectures, which might not agree on what the common page
    size is.
    
    Let libbpf find the right size for user instead, if it turns out to not
    satisfy kernel requirements. If user didn't set size at all, that's most
    probably a mistake so don't upsize such zero size to one full page,
    though. Also we need to be careful about not overflowing __u32
    max_entries.
    
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
    Link: https://lore.kernel.org/bpf/20220509004148.1801791-9-andrii@kernel.org

Signed-off-by: Yauheni Kaliuta <ykaliuta@redhat.com>
2022-11-30 12:47:02 +02:00
Yauheni Kaliuta 1f1c153aa0 libbpf: Allow to opt-out from creating BPF maps
Bugzilla: https://bugzilla.redhat.com/2120968

commit ec41817b4af5114825621fe9b31cb861480f6cd7
Author: Andrii Nakryiko <andrii@kernel.org>
Date:   Wed Apr 27 21:15:22 2022 -0700

    libbpf: Allow to opt-out from creating BPF maps
    
    Add bpf_map__set_autocreate() API that allows user to opt-out from
    libbpf automatically creating BPF map during BPF object load.
    
    This is a useful feature when building CO-RE-enabled BPF application
    that takes advantage of some new-ish BPF map type (e.g., socket-local
    storage) if kernel supports it, but otherwise uses some alternative way
    (e.g., extra HASH map). In such case, being able to disable the creation
    of a map that kernel doesn't support allows to successfully create and
    load BPF object file with all its other maps and programs.
    
    It's still up to user to make sure that no "live" code in any of their BPF
    programs are referencing such map instance, which can be achieved by
    guarding such code with CO-RE relocation check or by using .rodata
    global variables.
    
    If user fails to properly guard such code to turn it into "dead code",
    libbpf will helpfully post-process BPF verifier log and will provide
    more meaningful error and map name that needs to be guarded properly. As
    such, instead of:
    
      ; value = bpf_map_lookup_elem(&missing_map, &zero);
      4: (85) call unknown#2001000000
      invalid func unknown#2001000000
    
    ... user will see:
    
      ; value = bpf_map_lookup_elem(&missing_map, &zero);
      4: <invalid BPF map reference>
      BPF map 'missing_map' is referenced but wasn't created
    
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>
    Link: https://lore.kernel.org/bpf/20220428041523.4089853-4-andrii@kernel.org

Signed-off-by: Yauheni Kaliuta <ykaliuta@redhat.com>
2022-11-30 12:47:01 +02:00
Yauheni Kaliuta 2a54a315d0 libbpf: Use libbpf_mem_ensure() when allocating new map
Bugzilla: https://bugzilla.redhat.com/2120968

commit 69721203b1f3f9d123ae0f81bbf41f9a85185859
Author: Andrii Nakryiko <andrii@kernel.org>
Date:   Wed Apr 27 21:15:21 2022 -0700

    libbpf: Use libbpf_mem_ensure() when allocating new map
    
    Reuse libbpf_mem_ensure() when adding a new map to the list of maps
    inside bpf_object. It takes care of proper resizing and reallocating of
    map array and zeroing out newly allocated memory.
    
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>
    Link: https://lore.kernel.org/bpf/20220428041523.4089853-3-andrii@kernel.org

Signed-off-by: Yauheni Kaliuta <ykaliuta@redhat.com>
2022-11-30 12:47:01 +02:00
Yauheni Kaliuta e1f507e02c libbpf: Append "..." in fixed up log if CO-RE spec is truncated
Bugzilla: https://bugzilla.redhat.com/2120968

commit b198881d4b4c22c499168421b44eff3913a22fb1
Author: Andrii Nakryiko <andrii@kernel.org>
Date:   Wed Apr 27 21:15:20 2022 -0700

    libbpf: Append "..." in fixed up log if CO-RE spec is truncated
    
    Detect CO-RE spec truncation and append "..." to make user aware that
    there was supposed to be more of the spec there.
    
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>
    Link: https://lore.kernel.org/bpf/20220428041523.4089853-2-andrii@kernel.org

Signed-off-by: Yauheni Kaliuta <ykaliuta@redhat.com>
2022-11-30 12:47:01 +02:00
Yauheni Kaliuta 88727d4d40 libbpf: Support target-less SEC() definitions for BTF-backed programs
Bugzilla: https://bugzilla.redhat.com/2120968

commit cc7d8f2c8ecc003a67d4cf189d04124461524a16
Author: Andrii Nakryiko <andrii@kernel.org>
Date:   Thu Apr 28 11:53:48 2022 -0700

    libbpf: Support target-less SEC() definitions for BTF-backed programs
    
    Similar to previous patch, support target-less definitions like
    SEC("fentry"), SEC("freplace"), etc. For such BTF-backed program types
    it is expected that user will specify BTF target programmatically at
    runtime using bpf_program__set_attach_target() *before* load phase. If
    not, libbpf will report this as an error.
    
    Aslo use SEC_ATTACH_BTF flag instead of explicitly listing a set of
    types that are expected to require attach_btf_id. This was an accidental
    omission during custom SEC() support refactoring.
    
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
    Acked-by: Song Liu <songliubraving@fb.com>
    Link: https://lore.kernel.org/bpf/20220428185349.3799599-3-andrii@kernel.org

Signed-off-by: Yauheni Kaliuta <ykaliuta@redhat.com>
2022-11-30 12:47:01 +02:00
Yauheni Kaliuta 0cbfd0f7e4 libbpf: Allow "incomplete" basic tracing SEC() definitions
Bugzilla: https://bugzilla.redhat.com/2120968

commit 9af8efc45eb12c3f24a7053f994985ad28b4f29b
Author: Andrii Nakryiko <andrii@kernel.org>
Date:   Thu Apr 28 11:53:47 2022 -0700

    libbpf: Allow "incomplete" basic tracing SEC() definitions
    
    In a lot of cases the target of kprobe/kretprobe, tracepoint, raw
    tracepoint, etc BPF program might not be known at the compilation time
    and will be discovered at runtime. This was always a supported case by
    libbpf, with APIs like bpf_program__attach_{kprobe,tracepoint,etc}()
    accepting full target definition, regardless of what was defined in
    SEC() definition in BPF source code.
    
    Unfortunately, up till now libbpf still enforced users to specify at
    least something for the fake target, e.g., SEC("kprobe/whatever"), which
    is cumbersome and somewhat misleading.
    
    This patch allows target-less SEC() definitions for basic tracing BPF
    program types:
    
      - kprobe/kretprobe;
      - multi-kprobe/multi-kretprobe;
      - tracepoints;
      - raw tracepoints.
    
    Such target-less SEC() definitions are meant to specify declaratively
    proper BPF program type only. Attachment of them will have to be handled
    programmatically using correct APIs. As such, skeleton's auto-attachment
    of such BPF programs is skipped and generic bpf_program__attach() will
    fail, if attempted, due to the lack of enough target information.
    
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
    Acked-by: Song Liu <songliubraving@fb.com>
    Link: https://lore.kernel.org/bpf/20220428185349.3799599-2-andrii@kernel.org

Signed-off-by: Yauheni Kaliuta <ykaliuta@redhat.com>
2022-11-30 12:47:01 +02:00
Yauheni Kaliuta ea304e1d78 libbpf: Fix up verifier log for unguarded failed CO-RE relos
Bugzilla: https://bugzilla.redhat.com/2120968

commit 9fdc4273b8dad70dbf8f7fc1b94eadc1c1f6c934
Author: Andrii Nakryiko <andrii@kernel.org>
Date:   Mon Apr 25 17:45:10 2022 -0700

    libbpf: Fix up verifier log for unguarded failed CO-RE relos
    
    Teach libbpf to post-process BPF verifier log on BPF program load
    failure and detect known error patterns to provide user with more
    context.
    
    Currently there is one such common situation: an "unguarded" failed BPF
    CO-RE relocation. While failing CO-RE relocation is expected, it is
    expected to be property guarded in BPF code such that BPF verifier
    always eliminates BPF instructions corresponding to such failed CO-RE
    relos as dead code. In cases when user failed to take such precautions,
    BPF verifier provides the best log it can:
    
      123: (85) call unknown#195896080
      invalid func unknown#195896080
    
    Such incomprehensible log error is due to libbpf "poisoning" BPF
    instruction that corresponds to failed CO-RE relocation by replacing it
    with invalid `call 0xbad2310` instruction (195896080 == 0xbad2310 reads
    "bad relo" if you squint hard enough).
    
    Luckily, libbpf has all the necessary information to look up CO-RE
    relocation that failed and provide more human-readable description of
    what's going on:
    
      5: <invalid CO-RE relocation>
      failed to resolve CO-RE relocation <byte_off> [6] struct task_struct___bad.fake_field_subprog (0:2 @ offset 8)
    
    This hopefully makes it much easier to understand what's wrong with
    user's BPF program without googling magic constants.
    
    This BPF verifier log fixup is setup to be extensible and is going to be
    used for at least one other upcoming feature of libbpf in follow up patches.
    Libbpf is parsing lines of BPF verifier log starting from the very end.
    Currently it processes up to 10 lines of code looking for familiar
    patterns. This avoids wasting lots of CPU processing huge verifier logs
    (especially for log_level=2 verbosity level). Actual verification error
    should normally be found in last few lines, so this should work
    reliably.
    
    If libbpf needs to expand log beyond available log_buf_size, it
    truncates the end of the verifier log. Given verifier log normally ends
    with something like:
    
      processed 2 insns (limit 1000000) max_states_per_insn 0 total_states 0 peak_states 0 mark_read 0
    
    ... truncating this on program load error isn't too bad (end user can
    always increase log size, if it needs to get complete log).
    
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>
    Link: https://lore.kernel.org/bpf/20220426004511.2691730-10-andrii@kernel.org

Signed-off-by: Yauheni Kaliuta <ykaliuta@redhat.com>
2022-11-30 12:47:00 +02:00
Yauheni Kaliuta 30354f0489 libbpf: Record subprog-resolved CO-RE relocations unconditionally
Bugzilla: https://bugzilla.redhat.com/2120968

commit 185cfe837fdbb1fcc0f6b8fbcf5fdd2d1fccd3ad
Author: Andrii Nakryiko <andrii@kernel.org>
Date:   Mon Apr 25 17:45:07 2022 -0700

    libbpf: Record subprog-resolved CO-RE relocations unconditionally
    
    Previously, libbpf recorded CO-RE relocations with insns_idx resolved
    according to finalized subprog locations (which are appended at the end
    of entry BPF program) to simplify the job of light skeleton generator.
    
    This is necessary because once subprogs' instructions are appended to
    main entry BPF program all the subprog instruction indices are shifted
    and that shift is different for each entry (main) BPF program, so it's
    generally impossible to map final absolute insn_idx of the finalized BPF
    program to their original locations inside subprograms.
    
    This information is now going to be used not only during light skeleton
    generation, but also to map absolute instruction index to subprog's
    instruction and its corresponding CO-RE relocation. So start recording
    these relocations always, not just when obj->gen_loader is set.
    
    This information is going to be freed at the end of bpf_object__load()
    step, as before (but this can change in the future if there will be
    a need for this information post load step).
    
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>
    Link: https://lore.kernel.org/bpf/20220426004511.2691730-7-andrii@kernel.org

Signed-off-by: Yauheni Kaliuta <ykaliuta@redhat.com>
2022-11-30 12:47:00 +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 bc1ca1a959 libbpf: Fix logic for finding matching program for CO-RE relocation
Bugzilla: https://bugzilla.redhat.com/2120968

commit 966a7509325395c51c5f6d89e7352b0585e4804b
Author: Andrii Nakryiko <andrii@kernel.org>
Date:   Mon Apr 25 17:45:04 2022 -0700

    libbpf: Fix logic for finding matching program for CO-RE relocation
    
    Fix the bug in bpf_object__relocate_core() which can lead to finding
    invalid matching BPF program when processing CO-RE relocation. IF
    matching program is not found, last encountered program will be assumed
    to be correct program and thus error detection won't detect the problem.
    
    Fixes: 9c82a63cf3 ("libbpf: Fix CO-RE relocs against .text section")
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>
    Link: https://lore.kernel.org/bpf/20220426004511.2691730-4-andrii@kernel.org

Signed-off-by: Yauheni Kaliuta <ykaliuta@redhat.com>
2022-11-30 12:47:00 +02:00
Yauheni Kaliuta 6b18786dec libbpf: Drop unhelpful "program too large" guess
Bugzilla: https://bugzilla.redhat.com/2120968

commit 0994a54c5202114ad0e3b3a0f1326e810b23ad38
Author: Andrii Nakryiko <andrii@kernel.org>
Date:   Mon Apr 25 17:45:03 2022 -0700

    libbpf: Drop unhelpful "program too large" guess
    
    libbpf pretends it knows actual limit of BPF program instructions based
    on UAPI headers it compiled with. There is neither any guarantee that
    UAPI headers match host kernel, nor BPF verifier actually uses
    BPF_MAXINSNS constant anymore. Just drop unhelpful "guess", BPF verifier
    will emit actual reason for failure in its logs anyways.
    
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>
    Link: https://lore.kernel.org/bpf/20220426004511.2691730-3-andrii@kernel.org

Signed-off-by: Yauheni Kaliuta <ykaliuta@redhat.com>
2022-11-30 12:47:00 +02:00
Yauheni Kaliuta 16bab8f155 libbpf: Remove unnecessary type cast
Bugzilla: https://bugzilla.redhat.com/2120968

commit 003fed595c0f37d0ad112447f5f942654979426c
Author: Yuntao Wang <ytcoode@gmail.com>
Date:   Sun Apr 24 22:34:20 2022 +0800

    libbpf: Remove unnecessary type cast
    
    The link variable is already of type 'struct bpf_link *', casting it to
    'struct bpf_link *' is redundant, drop it.
    
    Signed-off-by: Yuntao Wang <ytcoode@gmail.com>
    Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
    Link: https://lore.kernel.org/bpf/20220424143420.457082-1-ytcoode@gmail.com

Signed-off-by: Yauheni Kaliuta <ykaliuta@redhat.com>
2022-11-28 16:52:11 +02:00
Yauheni Kaliuta c8b1585bd6 libbpf: Teach bpf_link_create() to fallback to bpf_raw_tracepoint_open()
Bugzilla: https://bugzilla.redhat.com/2120968

commit 8462e0b46fe2d4c56d0a7de705228e3bf1da03d9
Author: Andrii Nakryiko <andrii@kernel.org>
Date:   Wed Apr 20 20:39:44 2022 -0700

    libbpf: Teach bpf_link_create() to fallback to bpf_raw_tracepoint_open()
    
    Teach bpf_link_create() to fallback to bpf_raw_tracepoint_open() on
    older kernels for programs that are attachable through
    BPF_RAW_TRACEPOINT_OPEN. This makes bpf_link_create() more unified and
    convenient interface for creating bpf_link-based attachments.
    
    With this approach end users can just use bpf_link_create() for
    tp_btf/fentry/fexit/fmod_ret/lsm program attachments without needing to
    care about kernel support, as libbpf will handle this transparently. On
    the other hand, as newer features (like BPF cookie) are added to
    LINK_CREATE interface, they will be readily usable though the same
    bpf_link_create() API without any major refactoring from user's
    standpoint.
    
    bpf_program__attach_btf_id() is now using bpf_link_create() internally
    as well and will take advantaged of this unified interface when BPF
    cookie is added for fentry/fexit.
    
    Doing proactive feature detection of LINK_CREATE support for
    fentry/tp_btf/etc is quite involved. It requires parsing vmlinux BTF,
    determining some stable and guaranteed to be in all kernels versions
    target BTF type (either raw tracepoint or fentry target function),
    actually attaching this program and thus potentially affecting the
    performance of the host kernel briefly, etc. So instead we are taking
    much simpler "lazy" approach of falling back to
    bpf_raw_tracepoint_open() call only if initial LINK_CREATE command
    fails. For modern kernels this will mean zero added overhead, while
    older kernels will incur minimal overhead with a single fast-failing
    LINK_CREATE call.
    
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
    Reviewed-by: Kui-Feng Lee <kuifeng@fb.com>
    Link: https://lore.kernel.org/bpf/20220421033945.3602803-3-andrii@kernel.org

Signed-off-by: Yauheni Kaliuta <ykaliuta@redhat.com>
2022-11-28 16:52:11 +02:00
Yauheni Kaliuta 3cd9d9752c libbpf: Remove redundant non-null checks on obj_elf
Bugzilla: https://bugzilla.redhat.com/2120968

commit b71a2ebf74ef509b6b6926c78549e183c3b63947
Author: Gaosheng Cui <cuigaosheng1@huawei.com>
Date:   Thu Apr 21 11:18:03 2022 +0800

    libbpf: Remove redundant non-null checks on obj_elf
    
    Obj_elf is already non-null checked at the function entry, so remove
    redundant non-null checks on obj_elf.
    
    Signed-off-by: Gaosheng Cui <cuigaosheng1@huawei.com>
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Link: https://lore.kernel.org/bpf/20220421031803.2283974-1-cuigaosheng1@huawei.com

Signed-off-by: Yauheni Kaliuta <ykaliuta@redhat.com>
2022-11-28 16:52:11 +02:00
Yauheni Kaliuta 9b0fe1989e libbpf: Update API functions usage to check error
Bugzilla: https://bugzilla.redhat.com/2120968

commit df286716321350d1ca370d5737acf5a10b20ee9e
Author: Grant Seltzer <grantseltzer@gmail.com>
Date:   Wed Apr 20 12:12:25 2022 -0400

    libbpf: Update API functions usage to check error
    
    This updates usage of the following API functions within
    libbpf so their newly added error return is checked:
    
    - bpf_program__set_expected_attach_type()
    - bpf_program__set_type()
    
    Signed-off-by: Grant Seltzer <grantseltzer@gmail.com>
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
    Link: https://lore.kernel.org/bpf/20220420161226.86803-2-grantseltzer@gmail.com

Signed-off-by: Yauheni Kaliuta <ykaliuta@redhat.com>
2022-11-28 16:52:10 +02:00
Yauheni Kaliuta 6a768d7f28 libbpf: Add error returns to two API functions
Bugzilla: https://bugzilla.redhat.com/2120968

commit 93442f132b94721c7143ace7f43e51a9025f46fd
Author: Grant Seltzer <grantseltzer@gmail.com>
Date:   Wed Apr 20 12:12:24 2022 -0400

    libbpf: Add error returns to two API functions
    
    This adds an error return to the following API functions:
    
    - bpf_program__set_expected_attach_type()
    - bpf_program__set_type()
    
    In both cases, the error occurs when the BPF object has
    already been loaded when the function is called. In this
    case -EBUSY is returned.
    
    Signed-off-by: Grant Seltzer <grantseltzer@gmail.com>
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
    Link: https://lore.kernel.org/bpf/20220420161226.86803-1-grantseltzer@gmail.com

Signed-off-by: Yauheni Kaliuta <ykaliuta@redhat.com>
2022-11-28 16:52:10 +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 9e5dcbbb5a libbpf: Support opting out from autoloading BPF programs declaratively
Bugzilla: https://bugzilla.redhat.com/2120968

commit a3820c48111247f4ec2ca2949597f8fa57d2c424
Author: Andrii Nakryiko <andrii@kernel.org>
Date:   Mon Apr 18 17:24:50 2022 -0700

    libbpf: Support opting out from autoloading BPF programs declaratively
    
    Establish SEC("?abc") naming convention (i.e., adding question mark in
    front of otherwise normal section name) that allows to set corresponding
    program's autoload property to false. This is effectively just
    a declarative way to do bpf_program__set_autoload(prog, false).
    
    Having a way to do this declaratively in BPF code itself is useful and
    convenient for various scenarios. E.g., for testing, when BPF object
    consists of multiple independent BPF programs that each needs to be
    tested separately. Opting out all of them by default and then setting
    autoload to true for just one of them at a time simplifies testing code
    (see next patch for few conversions in BPF selftests taking advantage of
    this new feature).
    
    Another real-world use case is in libbpf-tools for cases when different
    BPF programs have to be picked depending on particulars of the host
    kernel due to various incompatible changes (like kernel function renames
    or signature change, or to pick kprobe vs fentry depending on
    corresponding kernel support for the latter). Marking all the different
    BPF program candidates as non-autoloaded declaratively makes this more
    obvious in BPF source code and allows simpler code in user-space code.
    
    When BPF program marked as SEC("?abc") it is otherwise treated just like
    SEC("abc") and bpf_program__section_name() reported will be "abc".
    
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>
    Link: https://lore.kernel.org/bpf/20220419002452.632125-1-andrii@kernel.org

Signed-off-by: Yauheni Kaliuta <ykaliuta@redhat.com>
2022-11-28 16:52:10 +02:00
Yauheni Kaliuta a9a7f9279e libbpf: Fix a bug with checking bpf_probe_read_kernel() support in old kernels
Bugzilla: https://bugzilla.redhat.com/2120968

commit d252a4a499a07bec21c65873f605c3a1ef52ffed
Author: Runqing Yang <rainkin1993@gmail.com>
Date:   Sat Apr 9 22:49:28 2022 +0800

    libbpf: Fix a bug with checking bpf_probe_read_kernel() support in old kernels
    
    Background:
    Libbpf automatically replaces calls to BPF bpf_probe_read_{kernel,user}
    [_str]() helpers with bpf_probe_read[_str](), if libbpf detects that
    kernel doesn't support new APIs. Specifically, libbpf invokes the
    probe_kern_probe_read_kernel function to load a small eBPF program into
    the kernel in which bpf_probe_read_kernel API is invoked and lets the
    kernel checks whether the new API is valid. If the loading fails, libbpf
    considers the new API invalid and replaces it with the old API.
    
    static int probe_kern_probe_read_kernel(void)
    {
    	struct bpf_insn insns[] = {
    		BPF_MOV64_REG(BPF_REG_1, BPF_REG_10),	/* r1 = r10 (fp) */
    		BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, -8),	/* r1 += -8 */
    		BPF_MOV64_IMM(BPF_REG_2, 8),		/* r2 = 8 */
    		BPF_MOV64_IMM(BPF_REG_3, 0),		/* r3 = 0 */
    		BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, BPF_FUNC_probe_read_kernel),
    		BPF_EXIT_INSN(),
    	};
    	int fd, insn_cnt = ARRAY_SIZE(insns);
    
    	fd = bpf_prog_load(BPF_PROG_TYPE_KPROBE, NULL,
                               "GPL", insns, insn_cnt, NULL);
    	return probe_fd(fd);
    }
    
    Bug:
    On older kernel versions [0], the kernel checks whether the version
    number provided in the bpf syscall, matches the LINUX_VERSION_CODE.
    If not matched, the bpf syscall fails. eBPF However, the
    probe_kern_probe_read_kernel code does not set the kernel version
    number provided to the bpf syscall, which causes the loading process
    alwasys fails for old versions. It means that libbpf will replace the
    new API with the old one even the kernel supports the new one.
    
    Solution:
    After a discussion in [1], the solution is using BPF_PROG_TYPE_TRACEPOINT
    program type instead of BPF_PROG_TYPE_KPROBE because kernel does not
    enfoce version check for tracepoint programs. I test the patch in old
    kernels (4.18 and 4.19) and it works well.
    
      [0] https://elixir.bootlin.com/linux/v4.19/source/kernel/bpf/syscall.c#L1360
      [1] Closes: https://github.com/libbpf/libbpf/issues/473
    
    Signed-off-by: Runqing Yang <rainkin1993@gmail.com>
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Link: https://lore.kernel.org/bpf/20220409144928.27499-1-rainkin1993@gmail.com

Signed-off-by: Yauheni Kaliuta <ykaliuta@redhat.com>
2022-11-28 16:48:58 +02:00
Yauheni Kaliuta 31b83b7644 libbpf: Allow WEAK and GLOBAL bindings during BTF fixup
Bugzilla: https://bugzilla.redhat.com/2120968

commit 3a06ec0a996dc8c4bc518f0b6bedc3587dd15169
Author: Andrii Nakryiko <andrii@kernel.org>
Date:   Thu Apr 7 16:04:46 2022 -0700

    libbpf: Allow WEAK and GLOBAL bindings during BTF fixup
    
    During BTF fix up for global variables, global variable can be global
    weak and will have STB_WEAK binding in ELF. Support such global
    variables in addition to non-weak ones.
    
    This is not the problem when using BPF static linking, as BPF static
    linker "fixes up" BTF during generation so that libbpf doesn't have to
    do it anymore during bpf_object__open(), which led to this not being
    noticed for a while, along with a pretty rare (currently) use of __weak
    variables and maps.
    
    Reported-by: Hengqi Chen <hengqi.chen@gmail.com>
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>
    Link: https://lore.kernel.org/bpf/20220407230446.3980075-2-andrii@kernel.org

Signed-off-by: Yauheni Kaliuta <ykaliuta@redhat.com>
2022-11-28 16:48:58 +02:00
Yauheni Kaliuta e6685aa135 libbpf: Don't error out on CO-RE relos for overriden weak subprogs
Bugzilla: https://bugzilla.redhat.com/2120968

commit e89d57d938c8fa80c457982154ed6110804814fe
Author: Andrii Nakryiko <andrii@kernel.org>
Date:   Fri Apr 8 11:14:23 2022 -0700

    libbpf: Don't error out on CO-RE relos for overriden weak subprogs
    
    During BPF static linking, all the ELF relocations and .BTF.ext
    information (including CO-RE relocations) are preserved for __weak
    subprograms that were logically overriden by either previous weak
    subprogram instance or by corresponding "strong" (non-weak) subprogram.
    This is just how native user-space linkers work, nothing new.
    
    But libbpf is over-zealous when processing CO-RE relocation to error out
    when CO-RE relocation belonging to such eliminated weak subprogram is
    encountered. Instead of erroring out on this expected situation, log
    debug-level message and skip the relocation.
    
    Fixes: db2b8b0642 ("libbpf: Support CO-RE relocations for multi-prog sections")
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
    Link: https://lore.kernel.org/bpf/20220408181425.2287230-2-andrii@kernel.org

Signed-off-by: Yauheni Kaliuta <ykaliuta@redhat.com>
2022-11-28 16:48:58 +02:00
Yauheni Kaliuta c36add1317 libbpf: Improve string parsing for uprobe auto-attach
Bugzilla: https://bugzilla.redhat.com/2120968

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

    libbpf: Improve string parsing for uprobe auto-attach
    
    For uprobe auto-attach, the parsing can be simplified for the SEC()
    name to a single sscanf(); the return value of the sscanf can then
    be used to distinguish between sections that simply specify
    "u[ret]probe" (and thus cannot auto-attach), those that specify
    "u[ret]probe/binary_path:function+offset" etc.
    
    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-3-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 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
Yauheni Kaliuta 62eed92467 libbpf: Support Debian in resolve_full_path()
Bugzilla: https://bugzilla.redhat.com/2120968

commit 568189310c2096e204674edd2f0da036cd50676a
Author: Ilya Leoshkevich <iii@linux.ibm.com>
Date:   Tue Apr 5 00:50:20 2022 +0200

    libbpf: Support Debian in resolve_full_path()
    
    attach_probe selftest fails on Debian-based distros with `failed to
    resolve full path for 'libc.so.6'`. The reason is that these distros
    embraced multiarch to the point where even for the "main" architecture
    they store libc in /lib/<triple>.
    
    This is configured in /etc/ld.so.conf and in theory it's possible to
    replicate the loader's parsing and processing logic in libbpf, however
    a much simpler solution is to just enumerate the known library paths.
    
    Signed-off-by: Ilya Leoshkevich <iii@linux.ibm.com>
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Link: https://lore.kernel.org/bpf/20220404225020.51029-1-iii@linux.ibm.com

Signed-off-by: Yauheni Kaliuta <ykaliuta@redhat.com>
2022-11-28 16:48:56 +02:00
Yauheni Kaliuta 2607864d9b libbpf: Add auto-attach for uprobes based on section name
Bugzilla: https://bugzilla.redhat.com/2120968

commit 39f8dc43b7a05ab0e352655a14a9d613c2308b92
Author: Alan Maguire <alan.maguire@oracle.com>
Date:   Wed Mar 30 16:26:38 2022 +0100

    libbpf: Add auto-attach for uprobes based on section name
    
    Now that u[ret]probes can use name-based specification, it makes
    sense to add support for auto-attach based on SEC() definition.
    The format proposed is
    
            SEC("u[ret]probe/binary:[raw_offset|[function_name[+offset]]")
    
    For example, to trace malloc() in libc:
    
            SEC("uprobe/libc.so.6:malloc")
    
    ...or to trace function foo2 in /usr/bin/foo:
    
            SEC("uprobe//usr/bin/foo:foo2")
    
    Auto-attach is done for all tasks (pid -1).  prog can be an absolute
    path or simply a program/library name; in the latter case, we use
    PATH/LD_LIBRARY_PATH to resolve the full path, falling back to
    standard locations (/usr/bin:/usr/sbin or /usr/lib64:/usr/lib) if
    the file is not found via environment-variable specified locations.
    
    Signed-off-by: Alan Maguire <alan.maguire@oracle.com>
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Link: https://lore.kernel.org/bpf/1648654000-21758-4-git-send-email-alan.maguire@oracle.com

Signed-off-by: Yauheni Kaliuta <ykaliuta@redhat.com>
2022-11-28 16:48:55 +02:00
Yauheni Kaliuta d06e8bea36 libbpf: Support function name-based attach uprobes
Bugzilla: https://bugzilla.redhat.com/2120968

commit 433966e3ae04165811b116af492a684bad7a158c
Author: Alan Maguire <alan.maguire@oracle.com>
Date:   Wed Mar 30 16:26:37 2022 +0100

    libbpf: Support function name-based attach uprobes
    
    kprobe attach is name-based, using lookups of kallsyms to translate
    a function name to an address.  Currently uprobe attach is done
    via an offset value as described in [1].  Extend uprobe opts
    for attach to include a function name which can then be converted
    into a uprobe-friendly offset.  The calcualation is done in
    several steps:
    
    1. First, determine the symbol address using libelf; this gives us
       the offset as reported by objdump
    2. If the function is a shared library function - and the binary
       provided is a shared library - no further work is required;
       the address found is the required address
    3. Finally, if the function is local, subtract the base address
       associated with the object, retrieved from ELF program headers.
    
    The resultant value is then added to the func_offset value passed
    in to specify the uprobe attach address.  So specifying a func_offset
    of 0 along with a function name "printf" will attach to printf entry.
    
    The modes of operation supported are then
    
    1. to attach to a local function in a binary; function "foo1" in
       "/usr/bin/foo"
    2. to attach to a shared library function in a shared library -
       function "malloc" in libc.
    
    [1] https://www.kernel.org/doc/html/latest/trace/uprobetracer.html
    
    Signed-off-by: Alan Maguire <alan.maguire@oracle.com>
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Link: https://lore.kernel.org/bpf/1648654000-21758-3-git-send-email-alan.maguire@oracle.com

Signed-off-by: Yauheni Kaliuta <ykaliuta@redhat.com>
2022-11-28 16:48:55 +02:00
Yauheni Kaliuta c070fbdb1a libbpf: auto-resolve programs/libraries when necessary for uprobes
Bugzilla: https://bugzilla.redhat.com/2120968

commit 1ce3a60e3c287479f15147ffc61c35b2e436a0d5
Author: Alan Maguire <alan.maguire@oracle.com>
Date:   Wed Mar 30 16:26:36 2022 +0100

    libbpf: auto-resolve programs/libraries when necessary for uprobes
    
    bpf_program__attach_uprobe_opts() requires a binary_path argument
    specifying binary to instrument.  Supporting simply specifying
    "libc.so.6" or "foo" should be possible too.
    
    Library search checks LD_LIBRARY_PATH, then /usr/lib64, /usr/lib.
    This allows users to run BPF programs prefixed with
    LD_LIBRARY_PATH=/path2/lib while still searching standard locations.
    Similarly for non .so files, we check PATH and /usr/bin, /usr/sbin.
    
    Path determination will be useful for auto-attach of BPF uprobe programs
    using SEC() definition.
    
    Signed-off-by: Alan Maguire <alan.maguire@oracle.com>
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Link: https://lore.kernel.org/bpf/1648654000-21758-2-git-send-email-alan.maguire@oracle.com

Signed-off-by: Yauheni Kaliuta <ykaliuta@redhat.com>
2022-11-28 16:48:55 +02:00
Jerome Marchand e26f9d7b4b libbpf: Close fd in bpf_object__reuse_map
Bugzilla: https://bugzilla.redhat.com/2120966

commit d0f325c34c2fbe15f6774f2b628224280b571ae9
Author: Hengqi Chen <hengqi.chen@gmail.com>
Date:   Sat Mar 19 11:05:33 2022 +0800

    libbpf: Close fd in bpf_object__reuse_map

    pin_fd is dup-ed and assigned in bpf_map__reuse_fd. Close it
    in bpf_object__reuse_map after reuse.

    Signed-off-by: Hengqi Chen <hengqi.chen@gmail.com>
    Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
    Link: https://lore.kernel.org/bpf/20220319030533.3132250-1-hengqi.chen@gmail.com

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
2022-10-25 14:58:07 +02:00
Jerome Marchand 137bd2d294 libbpf: Avoid NULL deref when initializing map BTF info
Bugzilla: https://bugzilla.redhat.com/2120966

commit a8fee96202e279441d0e52d83eb100bd4a6d6272
Author: Andrii Nakryiko <andrii@kernel.org>
Date:   Sat Mar 19 17:19:11 2022 -0700

    libbpf: Avoid NULL deref when initializing map BTF info

    If BPF object doesn't have an BTF info, don't attempt to search for BTF
    types describing BPF map key or value layout.

    Fixes: 262cfb74ffda ("libbpf: Init btf_{key,value}_type_id on internal map open")
    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/20220320001911.3640917-1-andrii@kernel.org

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
2022-10-25 14:58:06 +02:00
Jerome Marchand 6aa3d22b6b libbpf: Add subskeleton scaffolding
Bugzilla: https://bugzilla.redhat.com/2120966

commit 430025e5dca5ad8902e7c3092b7c975acae154c5
Author: Delyan Kratunov <delyank@fb.com>
Date:   Wed Mar 16 23:37:33 2022 +0000

    libbpf: Add subskeleton scaffolding

    In symmetry with bpf_object__open_skeleton(),
    bpf_object__open_subskeleton() performs the actual walking and linking
    of maps, progs, and globals described by bpf_*_skeleton objects.

    Signed-off-by: Delyan Kratunov <delyank@fb.com>
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Link: https://lore.kernel.org/bpf/6942a46fbe20e7ebf970affcca307ba616985b15.1647473511.git.delyank@fb.com

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
2022-10-25 14:58:05 +02:00
Jerome Marchand e6fef94e55 libbpf: Init btf_{key,value}_type_id on internal map open
Bugzilla: https://bugzilla.redhat.com/2120966

commit 262cfb74ffdaed06e65b8b20e10241768a9a2e18
Author: Delyan Kratunov <delyank@fb.com>
Date:   Wed Mar 16 23:37:30 2022 +0000

    libbpf: Init btf_{key,value}_type_id on internal map open

    For internal and user maps, look up the key and value btf
    types on open() and not load(), so that `bpf_map_btf_value_type_id`
    is usable in `bpftool gen`.

    Signed-off-by: Delyan Kratunov <delyank@fb.com>
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Link: https://lore.kernel.org/bpf/78dbe4e457b4a05e098fc6c8f50014b680c86e4e.1647473511.git.delyank@fb.com

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
2022-10-25 14:58:05 +02:00
Jerome Marchand 7c2b7d7960 libbpf: .text routines are subprograms in strict mode
Bugzilla: https://bugzilla.redhat.com/2120966

commit bc380eb9d04812eda23fd1d2904389012b50d946
Author: Delyan Kratunov <delyank@fb.com>
Date:   Wed Mar 16 23:37:24 2022 +0000

    libbpf: .text routines are subprograms in strict mode

    Currently, libbpf considers a single routine in .text to be a program. This
    is particularly confusing when it comes to library objects - a single routine
    meant to be used as an extern will instead be considered a bpf_program.

    This patch hides this compatibility behavior behind the pre-existing
    SEC_NAME strict mode flag.

    Signed-off-by: Delyan Kratunov <delyank@fb.com>
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Link: https://lore.kernel.org/bpf/018de8d0d67c04bf436055270d35d394ba393505.1647473511.git.delyank@fb.com

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
2022-10-25 14:58:05 +02:00
Jerome Marchand de54ffa247 libbpf: Add bpf_program__attach_kprobe_multi_opts function
Bugzilla: https://bugzilla.redhat.com/2120966

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

    libbpf: Add bpf_program__attach_kprobe_multi_opts function

    Adding bpf_program__attach_kprobe_multi_opts function for attaching
    kprobe program to multiple functions.

      struct bpf_link *
      bpf_program__attach_kprobe_multi_opts(const struct bpf_program *prog,
                                            const char *pattern,
                                            const struct bpf_kprobe_multi_opts *opts);

    User can specify functions to attach with 'pattern' argument that
    allows wildcards (*?' supported) or provide symbols or addresses
    directly through opts argument. These 3 options are mutually
    exclusive.

    When using symbols or addresses, user can also provide cookie value
    for each symbol/address that can be retrieved later in bpf program
    with bpf_get_attach_cookie helper.

      struct bpf_kprobe_multi_opts {
              size_t sz;
              const char **syms;
              const unsigned long *addrs;
              const __u64 *cookies;
              size_t cnt;
              bool retprobe;
              size_t :0;
      };

    Symbols, addresses and cookies are provided through opts object
    (syms/addrs/cookies) as array pointers with specified count (cnt).

    Each cookie value is paired with provided function address or symbol
    with the same array index.

    The program can be also attached as return probe if 'retprobe' is set.

    For quick usage with NULL opts argument, like:

      bpf_program__attach_kprobe_multi_opts(prog, "ksys_*", NULL)

    the 'prog' will be attached as kprobe to 'ksys_*' functions.

    Also adding new program sections for automatic attachment:

      kprobe.multi/<symbol_pattern>
      kretprobe.multi/<symbol_pattern>

    The symbol_pattern is used as 'pattern' argument in
    bpf_program__attach_kprobe_multi_opts function.

    Signed-off-by: Jiri Olsa <jolsa@kernel.org>
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>
    Link: https://lore.kernel.org/bpf/20220316122419.933957-10-jolsa@kernel.org

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
2022-10-25 14:58:05 +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 b8f5a4b6ed libbpf: Support custom SEC() handlers
Bugzilla: https://bugzilla.redhat.com/2120966

commit 697f104db8a6177daa5e1ffadda09c2e9285ad18
Author: Andrii Nakryiko <andrii@kernel.org>
Date:   Fri Mar 4 17:01:28 2022 -0800

    libbpf: Support custom SEC() handlers

    Allow registering and unregistering custom handlers for BPF program.
    This allows user applications and libraries to plug into libbpf's
    declarative SEC() definition handling logic. This allows to offload
    complex and intricate custom logic into external libraries, but still
    provide a great user experience.

    One such example is USDT handling library, which has a lot of code and
    complexity which doesn't make sense to put into libbpf directly, but it
    would be really great for users to be able to specify BPF programs with
    something like SEC("usdt/<path-to-binary>:<usdt_provider>:<usdt_name>")
    and have correct BPF program type set (BPF_PROGRAM_TYPE_KPROBE, as it is
    uprobe) and even support BPF skeleton's auto-attach logic.

    In some cases, it might be even good idea to override libbpf's default
    handling, like for SEC("perf_event") programs. With custom library, it's
    possible to extend logic to support specifying perf event specification
    right there in SEC() definition without burdening libbpf with lots of
    custom logic or extra library dependecies (e.g., libpfm4). With current
    patch it's possible to override libbpf's SEC("perf_event") handling and
    specify a completely custom ones.

    Further, it's possible to specify a generic fallback handling for any
    SEC() that doesn't match any other custom or standard libbpf handlers.
    This allows to accommodate whatever legacy use cases there might be, if
    necessary.

    See doc comments for libbpf_register_prog_handler() and
    libbpf_unregister_prog_handler() for detailed semantics.

    This patch also bumps libbpf development version to v0.8 and adds new
    APIs there.

    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>
    Tested-by: Alan Maguire <alan.maguire@oracle.com>
    Reviewed-by: Alan Maguire <alan.maguire@oracle.com>
    Link: https://lore.kernel.org/bpf/20220305010129.1549719-3-andrii@kernel.org

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
2022-10-25 14:57:55 +02:00
Jerome Marchand b6081d6af6 libbpf: Allow BPF program auto-attach handlers to bail out
Bugzilla: https://bugzilla.redhat.com/2120966

commit 4fa5bcfe07f7e97d9a140c465e1056c91651c41d
Author: Andrii Nakryiko <andrii@kernel.org>
Date:   Fri Mar 4 17:01:27 2022 -0800

    libbpf: Allow BPF program auto-attach handlers to bail out

    Allow some BPF program types to support auto-attach only in subste of
    cases. Currently, if some BPF program type specifies attach callback, it
    is assumed that during skeleton attach operation all such programs
    either successfully attach or entire skeleton attachment fails. If some
    program doesn't support auto-attachment from skeleton, such BPF program
    types shouldn't have attach callback specified.

    This is limiting for cases when, depending on how full the SEC("")
    definition is, there could either be enough details to support
    auto-attach or there might not be and user has to use some specific API
    to provide more details at runtime.

    One specific example of such desired behavior might be SEC("uprobe"). If
    it's specified as just uprobe auto-attach isn't possible. But if it's
    SEC("uprobe/<some_binary>:<some_func>") then there are enough details to
    support auto-attach. Note that there is a somewhat subtle difference
    between auto-attach behavior of BPF skeleton and using "generic"
    bpf_program__attach(prog) (which uses the same attach handlers under the
    cover). Skeleton allow some programs within bpf_object to not have
    auto-attach implemented and doesn't treat that as an error. Instead such
    BPF programs are just skipped during skeleton's (optional) attach step.
    bpf_program__attach(), on the other hand, is called when user *expects*
    auto-attach to work, so if specified program doesn't implement or
    doesn't support auto-attach functionality, that will be treated as an
    error.

    Another improvement to the way libbpf is handling SEC()s would be to not
    require providing dummy kernel function name for kprobe. Currently,
    SEC("kprobe/whatever") is necessary even if actual kernel function is
    determined by user at runtime and bpf_program__attach_kprobe() is used
    to specify it. With changes in this patch, it's possible to support both
    SEC("kprobe") and SEC("kprobe/<actual_kernel_function"), while only in
    the latter case auto-attach will be performed. In the former one, such
    kprobe will be skipped during skeleton attach operation.

    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>
    Tested-by: Alan Maguire <alan.maguire@oracle.com>
    Reviewed-by: Alan Maguire <alan.maguire@oracle.com>
    Link: https://lore.kernel.org/bpf/20220305010129.1549719-2-andrii@kernel.org

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
2022-10-25 14:57:55 +02:00
Jerome Marchand 5a5f6d7ac9 libbpf: Add a check to ensure that page_cnt is non-zero
Bugzilla: https://bugzilla.redhat.com/2120966

commit 41332d6e3a430adc91e0af115b4261b0d2f116ec
Author: Yuntao Wang <ytcoode@gmail.com>
Date:   Thu Mar 3 08:59:21 2022 +0800

    libbpf: Add a check to ensure that page_cnt is non-zero

    The page_cnt parameter is used to specify the number of memory pages
    allocated for each per-CPU buffer, it must be non-zero and a power of 2.

    Currently, the __perf_buffer__new() function attempts to validate that
    the page_cnt is a power of 2 but forgets checking for the case where
    page_cnt is zero, we can fix it by replacing 'page_cnt & (page_cnt - 1)'
    with 'page_cnt == 0 || (page_cnt & (page_cnt - 1))'.

    If so, we also don't need to add a check in perf_buffer__new_v0_6_0() to
    make sure that page_cnt is non-zero and the check for zero in
    perf_buffer__new_raw_v0_6_0() can also be removed.

    The code will be cleaner and more readable.

    Signed-off-by: Yuntao Wang <ytcoode@gmail.com>
    Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
    Link: https://lore.kernel.org/bpf/20220303005921.53436-1-ytcoode@gmail.com

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
2022-10-25 14:57:55 +02:00
Jerome Marchand 71520069e1 libbpf: Deprecate xdp_cpumap, xdp_devmap and classifier sec definitions
Bugzilla: https://bugzilla.redhat.com/2120966

commit 4a4d4cee48e284a2993d417c2f19439c4e1489be
Author: Lorenzo Bianconi <lorenzo@kernel.org>
Date:   Tue Feb 1 15:58:08 2022 +0100

    libbpf: Deprecate xdp_cpumap, xdp_devmap and classifier sec definitions

    Deprecate xdp_cpumap, xdp_devmap and classifier sec definitions.
    Introduce xdp/devmap and xdp/cpumap definitions according to the
    standard for SEC("") in libbpf:
    - prog_type.prog_flags/attach_place

    Signed-off-by: Lorenzo Bianconi <lorenzo@kernel.org>
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Link: https://lore.kernel.org/bpf/5c7bd9426b3ce6a31d9a4b1f97eb299e1467fc52.1643727185.git.lorenzo@kernel.org

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
2022-10-25 14:57:54 +02:00
Jerome Marchand 764aea24db libbpf: Fix BPF_MAP_TYPE_PERF_EVENT_ARRAY auto-pinning
Bugzilla: https://bugzilla.redhat.com/2120966

commit a4fbfdd7a160eccaafc093eb5b34f838b1ca0bf0
Author: Stijn Tintel <stijn@linux-ipv6.be>
Date:   Fri Feb 25 17:23:55 2022 +0200

    libbpf: Fix BPF_MAP_TYPE_PERF_EVENT_ARRAY auto-pinning

    When a BPF map of type BPF_MAP_TYPE_PERF_EVENT_ARRAY doesn't have the
    max_entries parameter set, the map will be created with max_entries set
    to the number of available CPUs. When we try to reuse such a pinned map,
    map_is_reuse_compat will return false, as max_entries in the map
    definition differs from max_entries of the existing map, causing the
    following error:

      libbpf: couldn't reuse pinned map at '/sys/fs/bpf/m_logging': parameter mismatch

    Fix this by overwriting max_entries in the map definition. For this to
    work, we need to do this in bpf_object__create_maps, before calling
    bpf_object__reuse_map.

    Fixes: 57a00f4164 ("libbpf: Add auto-pinning of maps when loading BPF objects")
    Signed-off-by: Stijn Tintel <stijn@linux-ipv6.be>
    Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
    Acked-by: Song Liu <songliubraving@fb.com>
    Link: https://lore.kernel.org/bpf/20220225152355.315204-1-stijn@linux-ipv6.be

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
2022-10-25 14:57:52 +02:00
Jerome Marchand fa54f9c011 libbpf: Simplify the find_elf_sec_sz() function
Bugzilla: https://bugzilla.redhat.com/2120966

commit 08894d9c647aad08ddd19398e03a0aa1a70b7dc8
Author: Yuntao Wang <ytcoode@gmail.com>
Date:   Wed Feb 23 16:52:44 2022 +0800

    libbpf: Simplify the find_elf_sec_sz() function

    The check in the last return statement is unnecessary, we can just return
    the ret variable.

    But we can simplify the function further by returning 0 immediately if we
    find the section size and -ENOENT otherwise.

    Thus we can also remove the ret variable.

    Signed-off-by: Yuntao Wang <ytcoode@gmail.com>
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Link: https://lore.kernel.org/bpf/20220223085244.3058118-1-ytcoode@gmail.com

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
2022-10-25 14:57:51 +02:00
Jerome Marchand ea11b00c34 libbpf: Remove redundant check in btf_fixup_datasec()
Bugzilla: https://bugzilla.redhat.com/2120966

commit 6966d4c4425b6796b1da13a6f86d09825df3d323
Author: Yuntao Wang <ytcoode@gmail.com>
Date:   Sun Feb 20 15:27:50 2022 +0800

    libbpf: Remove redundant check in btf_fixup_datasec()

    The check 't->size && t->size != size' is redundant because if t->size
    compares unequal to 0, we will just skip straight to sorting variables.

    Signed-off-by: Yuntao Wang <ytcoode@gmail.com>
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Link: https://lore.kernel.org/bpf/20220220072750.209215-1-ytcoode@gmail.com

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
2022-10-25 14:57:51 +02:00