Commit Graph

23 Commits

Author SHA1 Message Date
Jerome Marchand acf56c233e libbpf: Add BPF_KPROBE_SYSCALL macro
Bugzilla: https://bugzilla.redhat.com/2120966

commit 816ae109554756ce5e22e3aabde10161c4d0a4f7
Author: Hengqi Chen <hengqi.chen@gmail.com>
Date:   Mon Feb 7 22:31:33 2022 +0800

    libbpf: Add BPF_KPROBE_SYSCALL macro

    Add syscall-specific variant of BPF_KPROBE named BPF_KPROBE_SYSCALL ([0]).
    The new macro hides the underlying way of getting syscall input arguments.
    With the new macro, the following code:

        SEC("kprobe/__x64_sys_close")
        int BPF_KPROBE(do_sys_close, struct pt_regs *regs)
        {
            int fd;

            fd = PT_REGS_PARM1_CORE(regs);
            /* do something with fd */
        }

    can be written as:

        SEC("kprobe/__x64_sys_close")
        int BPF_KPROBE_SYSCALL(do_sys_close, int fd)
        {
            /* do something with fd */
        }

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

    Signed-off-by: Hengqi Chen <hengqi.chen@gmail.com>
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Link: https://lore.kernel.org/bpf/20220207143134.2977852-2-hengqi.chen@gmail.com

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
2022-10-25 14:57:49 +02:00
Jerome Marchand 825486589c libbpf: Fix accessing the first syscall argument on s390
Bugzilla: https://bugzilla.redhat.com/2120966

commit 1f22a6f9f9a0f50218a11a0554709fd34a821fa3
Author: Ilya Leoshkevich <iii@linux.ibm.com>
Date:   Wed Feb 9 03:17:45 2022 +0100

    libbpf: Fix accessing the first syscall argument on s390

    On s390, the first syscall argument should be accessed via orig_gpr2
    (see arch/s390/include/asm/syscall.h). Currently gpr[2] is used
    instead, leading to bpf_syscall_macro test failure.

    orig_gpr2 cannot be added to user_pt_regs, since its layout is a part
    of the ABI. Therefore provide access to it only through
    PT_REGS_PARM1_CORE_SYSCALL() by using a struct pt_regs flavor.

    Reported-by: Andrii Nakryiko <andrii@kernel.org>
    Signed-off-by: Ilya Leoshkevich <iii@linux.ibm.com>
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Link: https://lore.kernel.org/bpf/20220209021745.2215452-11-iii@linux.ibm.com

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
2022-10-25 14:57:48 +02:00
Jerome Marchand b6dcea014e libbpf: Fix accessing the first syscall argument on arm64
Bugzilla: https://bugzilla.redhat.com/2120966

commit fbca4a2f649730b67488a8b36140ce4d2cf13c63
Author: Ilya Leoshkevich <iii@linux.ibm.com>
Date:   Wed Feb 9 03:17:44 2022 +0100

    libbpf: Fix accessing the first syscall argument on arm64

    On arm64, the first syscall argument should be accessed via orig_x0
    (see arch/arm64/include/asm/syscall.h). Currently regs[0] is used
    instead, leading to bpf_syscall_macro test failure.

    orig_x0 cannot be added to struct user_pt_regs, since its layout is a
    part of the ABI. Therefore provide access to it only through
    PT_REGS_PARM1_CORE_SYSCALL() by using a struct pt_regs flavor.

    Reported-by: Heiko Carstens <hca@linux.ibm.com>
    Signed-off-by: Ilya Leoshkevich <iii@linux.ibm.com>
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Link: https://lore.kernel.org/bpf/20220209021745.2215452-10-iii@linux.ibm.com

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
2022-10-25 14:57:48 +02:00
Jerome Marchand 73f6a29832 libbpf: Allow overriding PT_REGS_PARM1{_CORE}_SYSCALL
Bugzilla: https://bugzilla.redhat.com/2120966

commit 60d16c5ccb811c9817bb0a71644b9ba14115f68e
Author: Ilya Leoshkevich <iii@linux.ibm.com>
Date:   Wed Feb 9 03:17:43 2022 +0100

    libbpf: Allow overriding PT_REGS_PARM1{_CORE}_SYSCALL

    arm64 and s390 need a special way to access the first syscall argument.

    Signed-off-by: Ilya Leoshkevich <iii@linux.ibm.com>
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Link: https://lore.kernel.org/bpf/20220209021745.2215452-9-iii@linux.ibm.com

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
2022-10-25 14:57:48 +02:00
Jerome Marchand e2105b3892 libbpf: Fix accessing syscall arguments on powerpc
Bugzilla: https://bugzilla.redhat.com/2120966

commit f07f1503469b11b739892d50c836992ffbe026ee
Author: Ilya Leoshkevich <iii@linux.ibm.com>
Date:   Wed Feb 9 03:17:39 2022 +0100

    libbpf: Fix accessing syscall arguments on powerpc

    powerpc does not select ARCH_HAS_SYSCALL_WRAPPER, so its syscall
    handlers take "unpacked" syscall arguments. Indicate this to libbpf
    using PT_REGS_SYSCALL_REGS macro.

    Reported-by: Heiko Carstens <hca@linux.ibm.com>
    Signed-off-by: Ilya Leoshkevich <iii@linux.ibm.com>
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Tested-by: Naveen N. Rao <naveen.n.rao@linux.vnet.ibm.com>
    Link: https://lore.kernel.org/bpf/20220209021745.2215452-5-iii@linux.ibm.com

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
2022-10-25 14:57:48 +02:00
Jerome Marchand 6e13f9c2d3 libbpf: Add PT_REGS_SYSCALL_REGS macro
Bugzilla: https://bugzilla.redhat.com/2120966

commit c5a1ffa0da76da02afc20a0946b594830488b324
Author: Ilya Leoshkevich <iii@linux.ibm.com>
Date:   Wed Feb 9 03:17:37 2022 +0100

    libbpf: Add PT_REGS_SYSCALL_REGS macro

    Architectures that select ARCH_HAS_SYSCALL_WRAPPER pass a pointer to
    struct pt_regs to syscall handlers, others unpack it into individual
    function parameters. Introduce a macro to describe what a particular
    arch does.

    Signed-off-by: Ilya Leoshkevich <iii@linux.ibm.com>
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Link: https://lore.kernel.org/bpf/20220209021745.2215452-3-iii@linux.ibm.com

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
2022-10-25 14:57:48 +02:00
Jerome Marchand 4f7162568b libbpf: Fix the incorrect register read for syscalls on x86_64
Bugzilla: https://bugzilla.redhat.com/2120966

commit d084df3b7a4c49fb2abec55f8d512c51d643c408
Author: Kenta Tada <Kenta.Tada@sony.com>
Date:   Mon Jan 24 23:16:21 2022 +0900

    libbpf: Fix the incorrect register read for syscalls on x86_64

    Currently, rcx is read as the fourth parameter of syscall on x86_64.
    But x86_64 Linux System Call convention uses r10 actually.
    This commit adds the wrapper for users who want to access to
    syscall params to analyze the user space.

    Signed-off-by: Kenta Tada <Kenta.Tada@sony.com>
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Link: https://lore.kernel.org/bpf/20220124141622.4378-3-Kenta.Tada@sony.com

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
2022-10-25 14:57:43 +02:00
Artem Savkov 83d327b2e4 libbpf: Use 100-character limit to make bpf_tracing.h easier to read
Bugzilla: https://bugzilla.redhat.com/2069046

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

commit f60edf5b53848f2cf53e7e4b716ed8e45563bb12
Author: Andrii Nakryiko <andrii@kernel.org>
Date:   Wed Dec 22 13:39:24 2021 -0800

    libbpf: Use 100-character limit to make bpf_tracing.h easier to read

    Improve bpf_tracing.h's macro definition readability by keeping them
    single-line and better aligned. This makes it easier to follow all those
    variadic patterns.

    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/20211222213924.1869758-2-andrii@kernel.org

Signed-off-by: Artem Savkov <asavkov@redhat.com>
2022-08-24 12:53:51 +02:00
Artem Savkov d24d3168ce libbpf: Normalize PT_REGS_xxx() macro definitions
Bugzilla: https://bugzilla.redhat.com/2069046

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

Conflicts: missing riscv commits

Omitted-fix: 5c101153bfd6 libbpf: Fix riscv register names
    unsupported arch

commit 3cc31d794097a0de5ac619d4a20b1975139e6b05
Author: Andrii Nakryiko <andrii@kernel.org>
Date:   Wed Dec 22 13:39:23 2021 -0800

    libbpf: Normalize PT_REGS_xxx() macro definitions

    Refactor PT_REGS macros definitions in  bpf_tracing.h to avoid excessive
    duplication. We currently have classic PT_REGS_xxx() and CO-RE-enabled
    PT_REGS_xxx_CORE(). We are about to add also _SYSCALL variants, which
    would require excessive copying of all the per-architecture definitions.

    Instead, separate architecture-specific field/register names from the
    final macro that utilize them. That way for upcoming _SYSCALL variants
    we'll be able to just define x86_64 exception and otherwise have one
    common set of _SYSCALL macro definitions common for all architectures.

    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>
    Tested-by: Ilya Leoshkevich <iii@linux.ibm.com>
    Acked-by: Yonghong Song <yhs@fb.com>
    Acked-by: Ilya Leoshkevich <iii@linux.ibm.com>
    Link: https://lore.kernel.org/bpf/20211222213924.1869758-1-andrii@kernel.org

Signed-off-by: Artem Savkov <asavkov@redhat.com>
2022-08-24 12:53:50 +02:00
Lorenz Bauer 4a638d581a libbpf: Fail compilation if target arch is missing
bpf2go is the Go equivalent of libbpf skeleton. The convention is that
the compiled BPF is checked into the repository to facilitate distributing
BPF as part of Go packages. To make this portable, bpf2go by default
generates both bpfel and bpfeb variants of the C.

Using bpf_tracing.h is inherently non-portable since the fields of
struct pt_regs differ between platforms, so CO-RE can't help us here.
The only way of working around this is to compile for each target
platform independently. bpf2go can't do this by default since there
are too many platforms.

Define the various PT_... macros when no target can be determined and
turn them into compilation failures. This works because bpf2go always
compiles for bpf targets, so the compiler fallback doesn't kick in.
Conditionally define __BPF_MISSING_TARGET so that we can inject a
more appropriate error message at build time. The user can then
choose which platform to target explicitly.

Signed-off-by: Lorenz Bauer <lmb@cloudflare.com>
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Link: https://lore.kernel.org/bpf/20210616083635.11434-1-lmb@cloudflare.com
2021-06-16 20:15:30 -07:00
Florent Revest d6a6a55518 libbpf: Move BPF_SEQ_PRINTF and BPF_SNPRINTF to bpf_helpers.h
These macros are convenient wrappers around the bpf_seq_printf and
bpf_snprintf helpers. They are currently provided by bpf_tracing.h which
targets low level tracing primitives. bpf_helpers.h is a better fit.

The __bpf_narg and __bpf_apply are needed in both files and provided
twice. __bpf_empty isn't used anywhere and is removed from bpf_tracing.h

Reported-by: Andrii Nakryiko <andrii@kernel.org>
Signed-off-by: Florent Revest <revest@chromium.org>
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Link: https://lore.kernel.org/bpf/20210526164643.2881368-1-revest@chromium.org
2021-05-26 10:45:41 -07:00
Florent Revest 58c2b1f5e0 libbpf: Introduce a BPF_SNPRINTF helper macro
Similarly to BPF_SEQ_PRINTF, this macro turns variadic arguments into an
array of u64, making it more natural to call the bpf_snprintf helper.

Signed-off-by: Florent Revest <revest@chromium.org>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Acked-by: Andrii Nakryiko <andrii@kernel.org>
Link: https://lore.kernel.org/bpf/20210419155243.1632274-6-revest@chromium.org
2021-04-19 15:27:37 -07:00
Florent Revest 83cd92b464 libbpf: Initialize the bpf_seq_printf parameters array field by field
When initializing the __param array with a one liner, if all args are
const, the initial array value will be placed in the rodata section but
because libbpf does not support relocation in the rodata section, any
pointer in this array will stay NULL.

Fixes: c09add2fbc ("tools/libbpf: Add bpf_iter support")
Signed-off-by: Florent Revest <revest@chromium.org>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Acked-by: Andrii Nakryiko <andrii@kernel.org>
Link: https://lore.kernel.org/bpf/20210419155243.1632274-5-revest@chromium.org
2021-04-19 15:27:37 -07:00
Andrii Nakryiko 70785cfb19 libbpf: Switch tracing and CO-RE helper macros to bpf_probe_read_kernel()
Now that libbpf can automatically fallback to bpf_probe_read() on old kernels
not yet supporting bpf_probe_read_kernel(), switch libbpf BPF-side helper
macros to use appropriate BPF helper for reading kernel data.

Signed-off-by: Andrii Nakryiko <andriin@fb.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Cc: Ilya Leoshkevich <iii@linux.ibm.com>
Link: https://lore.kernel.org/bpf/20200818213356.2629020-7-andriin@fb.com
2020-08-18 17:16:15 -07:00
Jerry Crunchtime 1acf8f90ea libbpf: Fix register in PT_REGS MIPS macros
The o32, n32 and n64 calling conventions require the return
value to be stored in $v0 which maps to $2 register, i.e.,
the register 2.

Fixes: c1932cd ("bpf: Add MIPS support to samples/bpf.")
Signed-off-by: Jerry Crunchtime <jerry.c.t@web.de>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Acked-by: Andrii Nakryiko <andriin@fb.com>
Link: https://lore.kernel.org/bpf/43707d31-0210-e8f0-9226-1af140907641@web.de
2020-07-31 17:20:49 +02:00
David S. Miller da07f52d3c Merge git://git.kernel.org/pub/scm/linux/kernel/git/netdev/net
Move the bpf verifier trace check into the new switch statement in
HEAD.

Resolve the overlapping changes in hinic, where bug fixes overlap
the addition of VF support.

Signed-off-by: David S. Miller <davem@davemloft.net>
2020-05-15 13:48:59 -07:00
Sumanth Korikkar 516d8d497c libbpf: Fix register naming in PT_REGS s390 macros
Fix register naming in PT_REGS s390 macros

Fixes: b8ebce86ff ("libbpf: Provide CO-RE variants of PT_REGS macros")
Signed-off-by: Sumanth Korikkar <sumanthk@linux.ibm.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Reviewed-by: Julian Wiedmann <jwi@linux.ibm.com>
Acked-by: Andrii Nakryiko <andriin@fb.com>
Link: https://lore.kernel.org/bpf/20200513154414.29972-1-sumanthk@linux.ibm.com
2020-05-14 12:44:17 -07:00
Yonghong Song c09add2fbc tools/libbpf: Add bpf_iter support
Two new libbpf APIs are added to support bpf_iter:
  - bpf_program__attach_iter
    Given a bpf program and additional parameters, which is
    none now, returns a bpf_link.
  - bpf_iter_create
    syscall level API to create a bpf iterator.

The macro BPF_SEQ_PRINTF are also introduced. The format
looks like:
  BPF_SEQ_PRINTF(seq, "task id %d\n", pid);

This macro can help bpf program writers with
nicer bpf_seq_printf syntax similar to the kernel one.

Signed-off-by: Yonghong Song <yhs@fb.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Acked-by: Andrii Nakryiko <andriin@fb.com>
Link: https://lore.kernel.org/bpf/20200509175917.2476936-1-yhs@fb.com
2020-05-09 17:05:27 -07:00
Wenbo Zhang 483d7a30f5 bpf, libbpf: Fix ___bpf_kretprobe_args1(x) macro definition
Use PT_REGS_RC instead of PT_REGS_RET to get ret correctly.

Fixes: df8ff35311 ("libbpf: Merge selftests' bpf_trace_helpers.h into libbpf's bpf_tracing.h")
Signed-off-by: Wenbo Zhang <ethercflow@gmail.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Acked-by: Andrii Nakryiko <andriin@fb.com>
Link: https://lore.kernel.org/bpf/20200315083252.22274-1-ethercflow@gmail.com
2020-03-17 19:33:40 +01:00
Andrii Nakryiko b8ebce86ff libbpf: Provide CO-RE variants of PT_REGS macros
Syscall raw tracepoints have struct pt_regs pointer as tracepoint's first
argument. After that, reading any of pt_regs fields requires bpf_probe_read(),
even for tp_btf programs. Due to that, PT_REGS_PARMx macros are not usable as
is. This patch adds CO-RE variants of those macros that use BPF_CORE_READ() to
read necessary fields. This provides relocatable architecture-agnostic pt_regs
field accesses.

Signed-off-by: Andrii Nakryiko <andriin@fb.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Acked-by: Martin KaFai Lau <kafai@fb.com>
Link: https://lore.kernel.org/bpf/20200313172336.1879637-4-andriin@fb.com
2020-03-13 23:30:53 +01:00
Andrii Nakryiko df8ff35311 libbpf: Merge selftests' bpf_trace_helpers.h into libbpf's bpf_tracing.h
Move BPF_PROG, BPF_KPROBE, and BPF_KRETPROBE macro into libbpf's bpf_tracing.h
header to make it available for non-selftests users.

Signed-off-by: Andrii Nakryiko <andriin@fb.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Link: https://lore.kernel.org/bpf/20200229231112.1240137-5-andriin@fb.com
2020-03-02 16:25:14 -08:00
Andrii Nakryiko fd56e00584 libbpf: Fix use of PT_REGS_PARM macros with vmlinux.h
Add detection of vmlinux.h to bpf_tracing.h header for PT_REGS macro.
Currently, BPF applications have to define __KERNEL__ symbol to use correct
definition of struct pt_regs on x86 arch. This is due to different field names
under internal kernel vs UAPI conditions. To make this more transparent for
users, detect vmlinux.h by checking __VMLINUX_H__ symbol.

Signed-off-by: Andrii Nakryiko <andriin@fb.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Link: https://lore.kernel.org/bpf/20200229231112.1240137-3-andriin@fb.com
2020-03-02 16:25:14 -08:00
Andrii Nakryiko e01a75c159 libbpf: Move bpf_{helpers, helper_defs, endian, tracing}.h into libbpf
Move bpf_helpers.h, bpf_tracing.h, and bpf_endian.h into libbpf. Move
bpf_helper_defs.h generation into libbpf's Makefile. Ensure all those
headers are installed along the other libbpf headers. Also, adjust
selftests and samples include path to include libbpf now.

Signed-off-by: Andrii Nakryiko <andriin@fb.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Acked-by: Song Liu <songliubraving@fb.com>
Link: https://lore.kernel.org/bpf/20191008175942.1769476-6-andriin@fb.com
2019-10-08 23:16:03 +02:00