• Andrii Nakryiko's avatar
    libbpf: improve BPF_KPROBE_SYSCALL macro and rename it to BPF_KSYSCALL · 6f5d467d
    Andrii Nakryiko authored
    Improve BPF_KPROBE_SYSCALL (and rename it to shorter BPF_KSYSCALL to
    match libbpf's SEC("ksyscall") section name, added in next patch) to use
    __kconfig variable to determine how to properly fetch syscall arguments.
    
    Instead of relying on hard-coded knowledge of whether kernel's
    architecture uses syscall wrapper or not (which only reflects the latest
    kernel versions, but is not necessarily true for older kernels and won't
    necessarily hold for later kernel versions on some particular host
    architecture), determine this at runtime by attempting to create
    perf_event (with fallback to kprobe event creation through tracefs on
    legacy kernels, just like kprobe attachment code is doing) for kernel
    function that would correspond to bpf() syscall on a system that has
    CONFIG_ARCH_HAS_SYSCALL_WRAPPER set (e.g., for x86-64 it would try
    '__x64_sys_bpf').
    
    If host kernel uses syscall wrapper, syscall kernel function's first
    argument is a pointer to struct pt_regs that then contains syscall
    arguments. In such case we need to use bpf_probe_read_kernel() to fetch
    actual arguments (which we do through BPF_CORE_READ() macro) from inner
    pt_regs.
    
    But if the kernel doesn't use syscall wrapper approach, input
    arguments can be read from struct pt_regs directly with no probe reading.
    
    All this feature detection is done without requiring /proc/config.gz
    existence and parsing, and BPF-side helper code uses newly added
    LINUX_HAS_SYSCALL_WRAPPER virtual __kconfig extern to keep in sync with
    user-side feature detection of libbpf.
    
    BPF_KSYSCALL() macro can be used both with SEC("kprobe") programs that
    define syscall function explicitly (e.g., SEC("kprobe/__x64_sys_bpf"))
    and SEC("ksyscall") program added in the next patch (which are the same
    kprobe program with added benefit of libbpf determining correct kernel
    function name automatically).
    
    Kretprobe and kretsyscall (added in next patch) programs don't need
    BPF_KSYSCALL as they don't provide access to input arguments. Normal
    BPF_KRETPROBE is completely sufficient and is recommended.
    Tested-by: default avatarAlan Maguire <alan.maguire@oracle.com>
    Signed-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
    Link: https://lore.kernel.org/r/20220714070755.3235561-4-andrii@kernel.orgSigned-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
    6f5d467d
bpf_tracing.h 21.5 KB