Skip to content

Commit

Permalink
bpf: correct broken uapi for BPF_PROG_TYPE_PERF_EVENT program type
Browse files Browse the repository at this point in the history
Commit 0515e59 ("bpf: introduce BPF_PROG_TYPE_PERF_EVENT
program type") introduced the bpf_perf_event_data structure which
exports the pt_regs structure.  This is OK for multiple architectures
but fail for s390 and arm64 which do not export pt_regs.  Programs
using them, for example, the bpf selftest fail to compile on these
architectures.

For s390, exporting the pt_regs is not an option because s390 wants
to allow changes to it.  For arm64, there is a user_pt_regs structure
that covers parts of the pt_regs structure for use by user space.

To solve the broken uapi for s390 and arm64, introduce an abstract
type for pt_regs and add an asm/bpf_perf_event.h file that concretes
the type.  An asm-generic header file covers the architectures that
export pt_regs today.

The arch-specific enablement for s390 and arm64 follows in separate
commits.

Reported-by: Thomas Richter <[email protected]>
Fixes: 0515e59 ("bpf: introduce BPF_PROG_TYPE_PERF_EVENT program type")
Signed-off-by: Hendrik Brueckner <[email protected]>
Reviewed-and-tested-by: Thomas Richter <[email protected]>
Acked-by: Alexei Starovoitov <[email protected]>
Cc: Arnaldo Carvalho de Melo <[email protected]>
Cc: Peter Zijlstra <[email protected]>
Cc: Ingo Molnar <[email protected]>
Cc: Alexander Shishkin <[email protected]>
Cc: Jiri Olsa <[email protected]>
Cc: Namhyung Kim <[email protected]>
Cc: Arnd Bergmann <[email protected]>
Cc: Daniel Borkmann <[email protected]>
Signed-off-by: Daniel Borkmann <[email protected]>
  • Loading branch information
hbrueckner authored and borkmann committed Dec 5, 2017
1 parent 2391f0b commit c895f6f
Show file tree
Hide file tree
Showing 32 changed files with 47 additions and 5 deletions.
2 changes: 2 additions & 0 deletions arch/alpha/include/uapi/asm/Kbuild
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
# UAPI Header export list
include include/uapi/asm-generic/Kbuild.asm

generic-y += bpf_perf_event.h
1 change: 1 addition & 0 deletions arch/arc/include/uapi/asm/Kbuild
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ include include/uapi/asm-generic/Kbuild.asm

generic-y += auxvec.h
generic-y += bitsperlong.h
generic-y += bpf_perf_event.h
generic-y += errno.h
generic-y += fcntl.h
generic-y += ioctl.h
Expand Down
1 change: 1 addition & 0 deletions arch/arm/include/uapi/asm/Kbuild
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ generated-y += unistd-oabi.h
generated-y += unistd-eabi.h

generic-y += bitsperlong.h
generic-y += bpf_perf_event.h
generic-y += errno.h
generic-y += ioctl.h
generic-y += ipcbuf.h
Expand Down
1 change: 1 addition & 0 deletions arch/blackfin/include/uapi/asm/Kbuild
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ include include/uapi/asm-generic/Kbuild.asm

generic-y += auxvec.h
generic-y += bitsperlong.h
generic-y += bpf_perf_event.h
generic-y += errno.h
generic-y += ioctl.h
generic-y += ipcbuf.h
Expand Down
1 change: 1 addition & 0 deletions arch/c6x/include/uapi/asm/Kbuild
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ include include/uapi/asm-generic/Kbuild.asm

generic-y += auxvec.h
generic-y += bitsperlong.h
generic-y += bpf_perf_event.h
generic-y += errno.h
generic-y += fcntl.h
generic-y += ioctl.h
Expand Down
1 change: 1 addition & 0 deletions arch/cris/include/uapi/asm/Kbuild
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ include include/uapi/asm-generic/Kbuild.asm

generic-y += auxvec.h
generic-y += bitsperlong.h
generic-y += bpf_perf_event.h
generic-y += errno.h
generic-y += fcntl.h
generic-y += ioctl.h
Expand Down
2 changes: 2 additions & 0 deletions arch/frv/include/uapi/asm/Kbuild
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
# UAPI Header export list
include include/uapi/asm-generic/Kbuild.asm

generic-y += bpf_perf_event.h
1 change: 1 addition & 0 deletions arch/h8300/include/uapi/asm/Kbuild
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
include include/uapi/asm-generic/Kbuild.asm

generic-y += auxvec.h
generic-y += bpf_perf_event.h
generic-y += errno.h
generic-y += fcntl.h
generic-y += ioctl.h
Expand Down
1 change: 1 addition & 0 deletions arch/hexagon/include/uapi/asm/Kbuild
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
include include/uapi/asm-generic/Kbuild.asm

generic-y += auxvec.h
generic-y += bpf_perf_event.h
generic-y += errno.h
generic-y += fcntl.h
generic-y += ioctl.h
Expand Down
1 change: 1 addition & 0 deletions arch/ia64/include/uapi/asm/Kbuild
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# UAPI Header export list
include include/uapi/asm-generic/Kbuild.asm

generic-y += bpf_perf_event.h
generic-y += kvm_para.h
1 change: 1 addition & 0 deletions arch/m32r/include/uapi/asm/Kbuild
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# UAPI Header export list
include include/uapi/asm-generic/Kbuild.asm

generic-y += bpf_perf_event.h
generic-y += kvm_para.h
generic-y += siginfo.h
1 change: 1 addition & 0 deletions arch/m68k/include/uapi/asm/Kbuild
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ include include/uapi/asm-generic/Kbuild.asm

generic-y += auxvec.h
generic-y += bitsperlong.h
generic-y += bpf_perf_event.h
generic-y += errno.h
generic-y += ioctl.h
generic-y += ipcbuf.h
Expand Down
1 change: 1 addition & 0 deletions arch/metag/include/uapi/asm/Kbuild
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ include include/uapi/asm-generic/Kbuild.asm

generic-y += auxvec.h
generic-y += bitsperlong.h
generic-y += bpf_perf_event.h
generic-y += errno.h
generic-y += fcntl.h
generic-y += ioctl.h
Expand Down
1 change: 1 addition & 0 deletions arch/microblaze/include/uapi/asm/Kbuild
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
include include/uapi/asm-generic/Kbuild.asm

generic-y += bitsperlong.h
generic-y += bpf_perf_event.h
generic-y += errno.h
generic-y += fcntl.h
generic-y += ioctl.h
Expand Down
1 change: 1 addition & 0 deletions arch/mips/include/uapi/asm/Kbuild
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# UAPI Header export list
include include/uapi/asm-generic/Kbuild.asm

generic-y += bpf_perf_event.h
generic-y += ipcbuf.h
1 change: 1 addition & 0 deletions arch/mn10300/include/uapi/asm/Kbuild
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# UAPI Header export list
include include/uapi/asm-generic/Kbuild.asm

generic-y += bpf_perf_event.h
generic-y += siginfo.h
1 change: 1 addition & 0 deletions arch/nios2/include/uapi/asm/Kbuild
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ include include/uapi/asm-generic/Kbuild.asm

generic-y += auxvec.h
generic-y += bitsperlong.h
generic-y += bpf_perf_event.h
generic-y += errno.h
generic-y += fcntl.h
generic-y += ioctl.h
Expand Down
1 change: 1 addition & 0 deletions arch/openrisc/include/uapi/asm/Kbuild
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ include include/uapi/asm-generic/Kbuild.asm

generic-y += auxvec.h
generic-y += bitsperlong.h
generic-y += bpf_perf_event.h
generic-y += errno.h
generic-y += fcntl.h
generic-y += ioctl.h
Expand Down
1 change: 1 addition & 0 deletions arch/parisc/include/uapi/asm/Kbuild
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
include include/uapi/asm-generic/Kbuild.asm

generic-y += auxvec.h
generic-y += bpf_perf_event.h
generic-y += kvm_para.h
generic-y += param.h
generic-y += poll.h
Expand Down
1 change: 1 addition & 0 deletions arch/powerpc/include/uapi/asm/Kbuild
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# UAPI Header export list
include include/uapi/asm-generic/Kbuild.asm

generic-y += bpf_perf_event.h
generic-y += param.h
generic-y += poll.h
generic-y += resource.h
Expand Down
1 change: 1 addition & 0 deletions arch/riscv/include/uapi/asm/Kbuild
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ include include/uapi/asm-generic/Kbuild.asm

generic-y += setup.h
generic-y += unistd.h
generic-y += bpf_perf_event.h
generic-y += errno.h
generic-y += fcntl.h
generic-y += ioctl.h
Expand Down
1 change: 1 addition & 0 deletions arch/score/include/uapi/asm/Kbuild
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# UAPI Header export list
include include/uapi/asm-generic/Kbuild.asm

generic-y += bpf_perf_event.h
generic-y += siginfo.h
1 change: 1 addition & 0 deletions arch/sh/include/uapi/asm/Kbuild
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
include include/uapi/asm-generic/Kbuild.asm

generic-y += bitsperlong.h
generic-y += bpf_perf_event.h
generic-y += errno.h
generic-y += fcntl.h
generic-y += ioctl.h
Expand Down
1 change: 1 addition & 0 deletions arch/sparc/include/uapi/asm/Kbuild
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# UAPI Header export list
include include/uapi/asm-generic/Kbuild.asm

generic-y += bpf_perf_event.h
generic-y += types.h
1 change: 1 addition & 0 deletions arch/tile/include/uapi/asm/Kbuild
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# UAPI Header export list
include include/uapi/asm-generic/Kbuild.asm

generic-y += bpf_perf_event.h
generic-y += errno.h
generic-y += fcntl.h
generic-y += ioctl.h
Expand Down
1 change: 1 addition & 0 deletions arch/unicore32/include/uapi/asm/Kbuild
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ include include/uapi/asm-generic/Kbuild.asm

generic-y += auxvec.h
generic-y += bitsperlong.h
generic-y += bpf_perf_event.h
generic-y += errno.h
generic-y += fcntl.h
generic-y += ioctl.h
Expand Down
1 change: 1 addition & 0 deletions arch/x86/include/uapi/asm/Kbuild
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# UAPI Header export list
include include/uapi/asm-generic/Kbuild.asm

generic-y += bpf_perf_event.h
generated-y += unistd_32.h
generated-y += unistd_64.h
generated-y += unistd_x32.h
1 change: 1 addition & 0 deletions arch/xtensa/include/uapi/asm/Kbuild
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
include include/uapi/asm-generic/Kbuild.asm

generic-y += bitsperlong.h
generic-y += bpf_perf_event.h
generic-y += errno.h
generic-y += fcntl.h
generic-y += ioctl.h
Expand Down
6 changes: 5 additions & 1 deletion include/linux/perf_event.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#define _LINUX_PERF_EVENT_H

#include <uapi/linux/perf_event.h>
#include <uapi/linux/bpf_perf_event.h>

/*
* Kernel-internal data types and definitions:
Expand Down Expand Up @@ -787,7 +788,7 @@ struct perf_output_handle {
};

struct bpf_perf_event_data_kern {
struct pt_regs *regs;
bpf_user_pt_regs_t *regs;
struct perf_sample_data *data;
struct perf_event *event;
};
Expand Down Expand Up @@ -1177,6 +1178,9 @@ extern void perf_bp_event(struct perf_event *event, void *data);
(user_mode(regs) ? PERF_RECORD_MISC_USER : PERF_RECORD_MISC_KERNEL)
# define perf_instruction_pointer(regs) instruction_pointer(regs)
#endif
#ifndef perf_arch_bpf_user_pt_regs
# define perf_arch_bpf_user_pt_regs(regs) regs
#endif

static inline bool has_branch_stack(struct perf_event *event)
{
Expand Down
9 changes: 9 additions & 0 deletions include/uapi/asm-generic/bpf_perf_event.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#ifndef _UAPI__ASM_GENERIC_BPF_PERF_EVENT_H__
#define _UAPI__ASM_GENERIC_BPF_PERF_EVENT_H__

#include <linux/ptrace.h>

/* Export kernel pt_regs structure */
typedef struct pt_regs bpf_user_pt_regs_t;

#endif /* _UAPI__ASM_GENERIC_BPF_PERF_EVENT_H__ */
5 changes: 2 additions & 3 deletions include/uapi/linux/bpf_perf_event.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@
#ifndef _UAPI__LINUX_BPF_PERF_EVENT_H__
#define _UAPI__LINUX_BPF_PERF_EVENT_H__

#include <linux/types.h>
#include <linux/ptrace.h>
#include <asm/bpf_perf_event.h>

struct bpf_perf_event_data {
struct pt_regs regs;
bpf_user_pt_regs_t regs;
__u64 sample_period;
};

Expand Down
2 changes: 1 addition & 1 deletion kernel/events/core.c
Original file line number Diff line number Diff line change
Expand Up @@ -7987,11 +7987,11 @@ static void bpf_overflow_handler(struct perf_event *event,
{
struct bpf_perf_event_data_kern ctx = {
.data = data,
.regs = regs,
.event = event,
};
int ret = 0;

ctx.regs = perf_arch_bpf_user_pt_regs(regs);
preempt_disable();
if (unlikely(__this_cpu_inc_return(bpf_prog_active) != 1))
goto out;
Expand Down

0 comments on commit c895f6f

Please sign in to comment.