Skip to content

Commit

Permalink
libbpf: fix __arg_ctx type enforcement for perf_event programs
Browse files Browse the repository at this point in the history
Adjust PERF_EVENT type enforcement around __arg_ctx to match exactly
what kernel is doing.

Fixes: 76ec90a ("libbpf: warn on unexpected __arg_ctx type when rewriting BTF")
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Link: https://lore.kernel.org/r/20240125205510.3642094-3-andrii@kernel.org
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
  • Loading branch information
Andrii Nakryiko authored and Alexei Starovoitov committed Jan 29, 2024
1 parent 0e6d0a9 commit 9eea8fa
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion tools/lib/bpf/libbpf.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include <linux/filter.h>
#include <linux/limits.h>
#include <linux/perf_event.h>
#include <linux/bpf_perf_event.h>
#include <linux/ring_buffer.h>
#include <sys/epoll.h>
#include <sys/ioctl.h>
Expand Down Expand Up @@ -6339,6 +6340,14 @@ static struct {
/* all other program types don't have "named" context structs */
};

/* forward declarations for arch-specific underlying types of bpf_user_pt_regs_t typedef,
* for below __builtin_types_compatible_p() checks;
* with this approach we don't need any extra arch-specific #ifdef guards
*/
struct pt_regs;
struct user_pt_regs;
struct user_regs_struct;

static bool need_func_arg_type_fixup(const struct btf *btf, const struct bpf_program *prog,
const char *subprog_name, int arg_idx,
int arg_type_id, const char *ctx_name)
Expand Down Expand Up @@ -6379,11 +6388,21 @@ static bool need_func_arg_type_fixup(const struct btf *btf, const struct bpf_pro
/* special cases */
switch (prog->type) {
case BPF_PROG_TYPE_KPROBE:
case BPF_PROG_TYPE_PERF_EVENT:
/* `struct pt_regs *` is expected, but we need to fix up */
if (btf_is_struct(t) && strcmp(tname, "pt_regs") == 0)
return true;
break;
case BPF_PROG_TYPE_PERF_EVENT:
if (__builtin_types_compatible_p(bpf_user_pt_regs_t, struct pt_regs) &&
btf_is_struct(t) && strcmp(tname, "pt_regs") == 0)
return 0;
if (__builtin_types_compatible_p(bpf_user_pt_regs_t, struct user_pt_regs) &&
btf_is_struct(t) && strcmp(tname, "user_pt_regs") == 0)
return 0;
if (__builtin_types_compatible_p(bpf_user_pt_regs_t, struct user_regs_struct) &&
btf_is_struct(t) && strcmp(tname, "user_regs_struct") == 0)
return 0;
break;
case BPF_PROG_TYPE_RAW_TRACEPOINT:
case BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE:
/* allow u64* as ctx */
Expand Down

0 comments on commit 9eea8fa

Please sign in to comment.