Commit Graph

17 Commits

Author SHA1 Message Date
Viktor Malik 306e6f87be
selftests/bpf: Add read_trace_pipe_iter function
JIRA: https://issues.redhat.com/browse/RHEL-30773

commit 4d4992ff587604455e8843a0e76dce0b99175319
Author: Jiri Olsa <jolsa@kernel.org>
Date:   Wed Apr 10 16:09:52 2024 +0200

    selftests/bpf: Add read_trace_pipe_iter function
    
    We have two printk tests reading trace_pipe in non blocking way,
    with the very same code. Moving that in new read_trace_pipe_iter
    function.
    
    Current read_trace_pipe is used from samples/bpf and needs to
    do blocking read and printf of the trace_pipe data, using new
    read_trace_pipe_iter to implement that.
    
    Both printk tests do early checks for the number of found messages
    and can bail earlier, but I did not find any speed difference w/o
    that condition, so I did not complicate the change more for that.
    
    Some of the samples/bpf programs use read_trace_pipe function,
    so I kept that interface untouched. I did not see any issues with
    affected samples/bpf programs other than there's slight change in
    read_trace_pipe output. The current code uses puts that adds new
    line after the printed string, so we would occasionally see extra
    new line. With this patch we read output per lines, so there's no
    need to use puts and we can use just printf instead without extra
    new line.
    
    Signed-off-by: Jiri Olsa <jolsa@kernel.org>
    Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
    Acked-by: Yonghong Song <yonghong.song@linux.dev>
    Link: https://lore.kernel.org/bpf/20240410140952.292261-1-jolsa@kernel.org

Signed-off-by: Viktor Malik <vmalik@redhat.com>
2024-11-11 07:44:47 +01:00
Viktor Malik 657aaea41e
selftests/bpf: Add {load,search}_kallsyms_custom_local()
JIRA: https://issues.redhat.com/browse/RHEL-30773

commit d1f02581059e42d8daf944aae2a296254cc7a5d5
Author: Yonghong Song <yonghong.song@linux.dev>
Date:   Mon Mar 25 21:15:13 2024 -0700

    selftests/bpf: Add {load,search}_kallsyms_custom_local()
    
    These two functions allow selftests to do loading/searching
    kallsyms based on their specific compare functions.
    
    Signed-off-by: Yonghong Song <yonghong.song@linux.dev>
    Link: https://lore.kernel.org/r/20240326041513.1199440-1-yonghong.song@linux.dev
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>

Signed-off-by: Viktor Malik <vmalik@redhat.com>
2024-11-07 13:58:38 +01:00
Viktor Malik 9379ef92f6
selftests/bpf: Refactor trace helper func load_kallsyms_local()
JIRA: https://issues.redhat.com/browse/RHEL-30773

commit 9475dacb75e0b5efae086dc904f4d27c31f15157
Author: Yonghong Song <yonghong.song@linux.dev>
Date:   Mon Mar 25 21:15:08 2024 -0700

    selftests/bpf: Refactor trace helper func load_kallsyms_local()
    
    Refactor trace helper function load_kallsyms_local() such that
    it invokes a common function with a compare function as input.
    The common function will be used later for other local functions.
    
    Signed-off-by: Yonghong Song <yonghong.song@linux.dev>
    Link: https://lore.kernel.org/r/20240326041508.1199239-1-yonghong.song@linux.dev
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>

Signed-off-by: Viktor Malik <vmalik@redhat.com>
2024-11-07 13:58:38 +01:00
Jerome Marchand 05ca9c497a selftests/bpf: trace_helpers.c: Optimize kallsyms cache
JIRA: https://issues.redhat.com/browse/RHEL-10691

commit c698eaebdf4759d297343f20e00172610207b754
Author: Rong Tao <rongtao@cestc.cn>
Date:   Thu Sep 7 09:59:13 2023 +0800

    selftests/bpf: trace_helpers.c: Optimize kallsyms cache

    Static ksyms often have problems because the number of symbols exceeds the
    MAX_SYMS limit. Like changing the MAX_SYMS from 300000 to 400000 in
    commit e76a014334a6("selftests/bpf: Bump and validate MAX_SYMS") solves
    the problem somewhat, but it's not the perfect way.

    This commit uses dynamic memory allocation, which completely solves the
    problem caused by the limitation of the number of kallsyms. At the same
    time, add APIs:

        load_kallsyms_local()
        ksym_search_local()
        ksym_get_addr_local()
        free_kallsyms_local()

    There are used to solve the problem of selftests/bpf updating kallsyms
    after attach new symbols during testmod testing.

    Signed-off-by: Rong Tao <rongtao@cestc.cn>
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Acked-by: Jiri Olsa <jolsa@kernel.org>
    Acked-by: Stanislav Fomichev <sdf@google.com>
    Link: https://lore.kernel.org/bpf/tencent_C9BDA68F9221F21BE4081566A55D66A9700A@qq.com

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
2023-12-15 09:29:03 +01:00
Artem Savkov c199838e98 selftests/bpf: Add read_build_id function
Bugzilla: https://bugzilla.redhat.com/2221599

commit 88dc8b3605b38a440fba45edcc53a6c7a98eee3b
Author: Jiri Olsa <jolsa@kernel.org>
Date:   Fri Mar 31 11:31:56 2023 +0200

    selftests/bpf: Add read_build_id function
    
    Adding read_build_id function that parses out build id from
    specified binary.
    
    It will replace extract_build_id and also be used in following
    changes.
    
    Signed-off-by: Jiri Olsa <jolsa@kernel.org>
    Link: https://lore.kernel.org/r/20230331093157.1749137-3-jolsa@kernel.org
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>

Signed-off-by: Artem Savkov <asavkov@redhat.com>
2023-09-22 09:12:24 +02:00
Jerome Marchand 9248b349e2 selftests/bpf: Add load_kallsyms_refresh function
Bugzilla: https://bugzilla.redhat.com/2177177

commit 10705b2b7a8e4eb46ab5bf1b9ee354cb9a929428
Author: Jiri Olsa <jolsa@kernel.org>
Date:   Tue Oct 25 15:41:45 2022 +0200

    selftests/bpf: Add load_kallsyms_refresh function

    Adding load_kallsyms_refresh function to re-read symbols from
    /proc/kallsyms file.

    This will be needed to get proper functions addresses from
    bpf_testmod.ko module, which is loaded/unloaded several times
    during the tests run, so symbols might be already old when
    we need to use them.

    Acked-by: Song Liu <song@kernel.org>
    Signed-off-by: Jiri Olsa <jolsa@kernel.org>
    Link: https://lore.kernel.org/r/20221025134148.3300700-6-jolsa@kernel.org
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
2023-04-28 11:42:56 +02:00
Jerome Marchand 21d6169b96 selftests/bpf: fix uprobe offset calculation in selftests
Bugzilla: https://bugzilla.redhat.com/2120966

Conflicts:
Replace u32 by uint32_t to fix a build failure on ppc64.

commit ff943683f8a6dbf887c16275d0e80c1c5391b7bb
Author: Andrii Nakryiko <andrii@kernel.org>
Date:   Wed Jan 26 11:30:58 2022 -0800

    selftests/bpf: fix uprobe offset calculation in selftests

    Fix how selftests determine relative offset of a function that is
    uprobed. Previously, there was an assumption that uprobed function is
    always in the first executable region, which is not always the case
    (libbpf CI hits this case now). So get_base_addr() approach in isolation
    doesn't work anymore. So teach get_uprobe_offset() to determine correct
    memory mapping and calculate uprobe offset correctly.

    While at it, I merged together two implementations of
    get_uprobe_offset() helper, moving powerpc64-specific logic inside (had
    to add extra {} block to avoid unused variable error for insn).

    Also ensured that uprobed functions are never inlined, but are still
    static (and thus local to each selftest), by using a no-op asm volatile
    block internally. I didn't want to keep them global __weak, because some
    tests use uprobe's ref counter offset (to test USDT-like logic) which is
    not compatible with non-refcounted uprobe. So it's nicer to have each
    test uprobe target local to the file and guaranteed to not be inlined or
    skipped by the compiler (which can happen with static functions,
    especially if compiling selftests with -O2).

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

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
2022-10-25 14:57:44 +02:00
Jerome Marchand 36158f8423 selftests/bpf: Add ref_ctr_offset selftests
Bugzilla: http://bugzilla.redhat.com/2041365

commit 4bd11e08e0bb1862fe72495014e33795e412bffb
Author: Andrii Nakryiko <andrii@kernel.org>
Date:   Sun Aug 15 00:06:09 2021 -0700

    selftests/bpf: Add ref_ctr_offset selftests

    Extend attach_probe selftests to specify ref_ctr_offset for uprobe/uretprobe
    and validate that its value is incremented from zero.

    Turns out that once uprobe is attached with ref_ctr_offset, uretprobe for the
    same location/function *has* to use ref_ctr_offset as well, otherwise
    perf_event_open() fails with -EINVAL. So this test uses ref_ctr_offset for
    both uprobe and uretprobe, even though for the purpose of test uprobe would be
    enough.

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

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
2022-04-29 18:14:42 +02:00
Jerome Marchand 2b2f9de787 Bselftests/bpf: Extract uprobe-related helpers into trace_helpers.{c,h}
Bugzilla: http://bugzilla.redhat.com/2041365

Conflicts: Use uint32_t type instead of u32 type to fix a build failure on ppc64.

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

    selftests/bpf: Extract uprobe-related helpers into trace_helpers.{c,h}

    Extract two helpers used for working with uprobes into trace_helpers.{c,h} to
    be re-used between multiple uprobe-using selftests. Also rename get_offset()
    into more appropriate get_uprobe_offset().

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

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
2022-04-29 18:14:42 +02:00
Hao Luo 2c2f6abeff selftests/bpf: Ksyms_btf to test typed ksyms
Selftests for typed ksyms. Tests two types of ksyms: one is a struct,
the other is a plain int. This tests two paths in the kernel. Struct
ksyms will be converted into PTR_TO_BTF_ID by the verifier while int
typed ksyms will be converted into PTR_TO_MEM.

Signed-off-by: Hao Luo <haoluo@google.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Acked-by: Andrii Nakryiko <andriin@fb.com>
Link: https://lore.kernel.org/bpf/20200929235049.2533242-4-haoluo@google.com
2020-10-02 14:59:25 -07:00
Daniel T. Lee 24a6034acc samples, bpf: Move read_trace_pipe to trace_helpers
To reduce the reliance of trace samples (trace*_user) on bpf_load,
move read_trace_pipe to trace_helpers. By moving this bpf_loader helper
elsewhere, trace functions can be easily migrated to libbbpf.

Signed-off-by: Daniel T. Lee <danieltimlee@gmail.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Acked-by: Andrii Nakryiko <andriin@fb.com>
Link: https://lore.kernel.org/bpf/20200321100424.1593964-2-danieltimlee@gmail.com
2020-03-23 22:27:51 +01:00
Toke Høiland-Jørgensen 3e689141e6 selftests: Use consistent include paths for libbpf
Fix all selftests to include libbpf header files with the bpf/ prefix, to
be consistent with external users of the library. Also ensure that all
includes of exported libbpf header files (those that are exported on 'make
install' of the library) use bracketed includes instead of quoted.

To not break the build, keep the old include path until everything has been
changed to the new one; a subsequent patch will remove that.

Fixes: 6910d7d386 ("selftests/bpf: Ensure bpf_helper_defs.h are taken from selftests dir")
Signed-off-by: Toke Høiland-Jørgensen <toke@redhat.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Acked-by: Andrii Nakryiko <andriin@fb.com>
Link: https://lore.kernel.org/bpf/157952560568.1683545.9649335788846513446.stgit@toke.dk
2020-01-20 16:37:45 -08:00
Andrii Nakryiko 47da6e4dc3 selftests/bpf: remove perf buffer helpers
libbpf's perf_buffer API supersedes trace_helper.h's helpers.
Remove those helpers after all existing users were already moved to
perf_buffer API.

Signed-off-by: Andrii Nakryiko <andriin@fb.com>
Acked-by: Song Liu <songliubraving@fb.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
2019-07-23 16:05:42 -07:00
Toke Høiland-Jørgensen 22adedd304 trace_helpers.c: Add helpers to poll multiple perf FDs for events
Add two new helper functions to trace_helpers that supports polling
multiple perf file descriptors for events. These are used to the XDP
perf_event_output example, which needs to work with one perf fd per CPU.

Reviewed-by: Jakub Kicinski <jakub.kicinski@netronome.com>
Signed-off-by: Toke Høiland-Jørgensen <toke@toke.dk>
Acked-by: Song Liu <songliubraving@fb.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
2018-06-27 11:01:03 +02:00
Yonghong Song 73bc4d9fc0 tools/bpf: add ksym_get_addr() in trace_helpers
Given a kernel function name, ksym_get_addr() will return the kernel
address for this function, or 0 if it cannot find this function name
in /proc/kallsyms. This function will be used later when a kernel
address is used to initiate a kprobe perf event.

Acked-by: Martin KaFai Lau <kafai@fb.com>
Signed-off-by: Yonghong Song <yhs@fb.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
2018-05-24 18:18:20 -07:00
Jakub Kicinski d0cabbb021 tools: bpf: move the event reading loop to libbpf
There are two copies of event reading loop - in bpftool and
trace_helpers "library".  Consolidate them and move the code
to libbpf.  Return codes from trace_helpers are kept, but
renamed to include LIBBPF prefix.

Suggested-by: Alexei Starovoitov <ast@kernel.org>
Signed-off-by: Jakub Kicinski <jakub.kicinski@netronome.com>
Reviewed-by: Quentin Monnet <quentin.monnet@netronome.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
2018-05-11 01:40:52 +02:00
Yonghong Song 28dbf861de samples/bpf: move common-purpose trace functions to selftests
There is no functionality change in this patch. The common-purpose
trace functions, including perf_event polling and ksym lookup,
are moved from trace_output_user.c and bpf_load.c to
selftests/bpf/trace_helpers.c so that these function can
be reused later in selftests.

Acked-by: Alexei Starovoitov <ast@fb.com>
Signed-off-by: Yonghong Song <yhs@fb.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
2018-04-29 08:45:54 -07:00