Misc BPF fixes
Subsystems (10)
Daniel Borkmann
maintainercover Misc BPF fixes
Couple of outstanding fixes for BPF tree: 1) fixes a perf RB corruption, 2) and 3) fixes a few build issues from the recent bpf_perf_event.h uapi corrections. Thanks!
Daniel Borkmann (3):
bpf: fix corruption on concurrent perf_event_output calls
bpf: fix build issues on um due to mising bpf_perf_event.h
bpf: fix broken BPF selftest build
arch/um/include/asm/Kbuild | 1 + kernel/trace/bpf_trace.c | 19 ++++++++++++------- tools/include/uapi/asm/bpf_perf_event.h | 7 +++++++ tools/testing/selftests/bpf/Makefile | 13 +------------ 4 files changed, 21 insertions(+), 19 deletions(-) create mode 100644 tools/include/uapi/asm/bpf_perf_event.h
Signature · 1 line
2.9.5
Daniel Borkmann
maintainerPATCH 1/3 bpf: fix corruption on concurrent perf_event_output calls
When tracing and networking programs are both attached in the system and both use event-output helpers that eventually call into perf_event_output(), then we could end up in a situation where the tracing attached program runs in user context while a cls_bpf program is triggered on that same CPU out of softirq context.
Since both rely on the same per-cpu perf_sample_data, we could potentially corrupt it. This can only ever happen in a combination of the two types; all tracing programs use a bpf_prog_active counter to bail out in case a program is already running on that CPU out of a different context. XDP and cls_bpf programs by themselves don't have this issue as they run in the same context only. Therefore, split both perf_sample_data so they cannot be accessed from each other.
--- kernel/trace/bpf_trace.c | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-)
kernel/trace/bpf_trace.c +12 -7
@@ -343,14 +343,13 @@ static const struct bpf_func_proto bpf_perf_event_read_value_proto = { .arg4_type = ARG_CONST_SIZE, }; -static DEFINE_PER_CPU(struct perf_sample_data, bpf_sd); +static DEFINE_PER_CPU(struct perf_sample_data, bpf_trace_sd); static __always_inline u64__bpf_perf_event_output (struct pt_regs *regs,struct bpf_map *map, -u64 flags,struct perf_raw_record *raw) +u64 flags,struct perf_sample_data *sd) { struct bpf_array *array = container_of(map, struct bpf_array, map); -struct perf_sample_data *sd =this_cpu_ptr (&bpf_sd);unsigned int cpu =smp_processor_id ();u64 index = flags &BPF_F_INDEX_MASK ;struct bpf_event_entry *ee;
@@ -373,8 +372,6 @@ __bpf_perf_event_output(struct pt_regs *regs, struct bpf_map *map,if (unlikely (event->oncpu != cpu))return -EOPNOTSUPP ; -perf_sample_data_init (sd,0 ,0 ); - sd->raw = raw;perf_event_output (event, sd, regs);return 0 ; }
@@ -382,6 +379,7 @@ __bpf_perf_event_output(struct pt_regs *regs, struct bpf_map *map, BPF_CALL_5(bpf_perf_event_output, struct pt_regs *, regs, struct bpf_map *, map, u64, flags, void *, data, u64, size) { +struct perf_sample_data *sd =this_cpu_ptr (&bpf_trace_sd);struct perf_raw_record raw = { .frag = { .size = size,
@@ -392,7 +390,10 @@ BPF_CALL_5(bpf_perf_event_output, struct pt_regs *, regs, struct bpf_map *, map,if (unlikely (flags & ~(BPF_F_INDEX_MASK )))return -EINVAL ; -return __bpf_perf_event_output (regs, map, flags, &raw); +perf_sample_data_init (sd,0 ,0 ); + sd->raw = &raw; + +return __bpf_perf_event_output (regs, map, flags, sd); }static const struct bpf_func_proto bpf_perf_event_output_proto = {
@@ -407,10 +408,12 @@ static const struct bpf_func_proto bpf_perf_event_output_proto = { }; static DEFINE_PER_CPU(struct pt_regs, bpf_pt_regs); +static DEFINE_PER_CPU(struct perf_sample_data, bpf_misc_sd);u64 bpf_event_output (struct bpf_map *map,u64 flags,void *meta,u64 meta_size,void *ctx,u64 ctx_size,bpf_ctx_copy_t ctx_copy) { +struct perf_sample_data *sd =this_cpu_ptr (&bpf_misc_sd);struct pt_regs *regs =this_cpu_ptr (&bpf_pt_regs);struct perf_raw_frag frag = { .copy = ctx_copy,
@@ -428,8 +431,10 @@ u64 bpf_event_output(struct bpf_map *map, u64 flags, void *meta, u64 meta_size, };perf_fetch_caller_regs (regs); +perf_sample_data_init (sd,0 ,0 ); + sd->raw = &raw; -return __bpf_perf_event_output (regs, map, flags, &raw); +return __bpf_perf_event_output (regs, map, flags, sd); }BPF_CALL_0 (bpf_get_current_task)
Signature · 1 line
2.9.5
Daniel Borkmann
maintainerPATCH 2/3 bpf: fix build issues on um due to mising bpf_perf_event.h
In reply to Daniel BorkmannSince c895f6f703ad ("bpf: correct broken uapi for BPF_PROG_TYPE_PERF_EVENT program type") um (uml) won't build on i386 or x86_64:
[...]
CC init/main.o
In file included from ../include/linux/perf_event.h:18:0,
from ../include/linux/trace_events.h:10,
from ../include/trace/syscall.h:7,
from ../include/linux/syscalls.h:82,
from ../init/main.c:20: ../include/uapi/linux/bpf_perf_event.h:11:32: fatal error:
asm/bpf_perf_event.h: No such file or directory #include <asm/bpf_perf_event.h>
[...]
Lets add missing bpf_perf_event.h also to um arch. This seems to be the only one still missing.
--- arch/um/include/asm/Kbuild | 1 + 1 file changed, 1 insertion(+)
arch/um/include/asm/Kbuild +1 -0
@@ -1,4 +1,5 @@ generic-y += barrier.h +generic-y += bpf_perf_event.h generic-y += bug.h generic-y += clkdev.h generic-y += current.h
Signature · 1 line
2.9.5
Re: [PATCH bpf 2/3] bpf: fix build issues on um due to mising bpf_perf_event.h
Am Dienstag, 12. Dezember 2017, 02:25:31 CET schrieb Daniel Borkmann:
Quoted 36 lines
Since
c895f6f703ad("bpf: correct broken uapi forBPF_PROG_TYPE_PERF_EVENTprogram type") um (uml) won't build on i386 orx86_64:[...]
CCinit/main.o
In file included from../include/linux/perf_event.h:18:0,from ../include/linux/trace_events.h:10, from ../include/trace/syscall.h:7, from ../include/linux/syscalls.h:82, from ../init/main.c:20:
../include/uapi/linux/bpf_perf_event.h:11:32: fatal error:
asm/bpf_perf_event.h: No such file or directory #include <asm/bpf_perf_event.h>
[...]Lets add missing
bpf_perf_event.halso to um arch. This seems to be the only one still missing.Fixes c895f6f703ad ("bpf: correct broken uapi for BPF_PROG_TYPE_PERF_EVENTprogram type") Reported-by: Randy Dunlap <[email protected]>
Suggested-by Richard Weinberger <[email protected]>Signed-off-by Daniel Borkmann <[email protected]>Tested-by Randy Dunlap <[email protected]>Cc Hendrik Brueckner <[email protected]>Cc Richard Weinberger <[email protected]>--- arch/um/include/asm/Kbuild | 1 + 1 file changed, 1 insertion(+)arch/um/include/asm/Kbuild +0
-0
arch/um/include/asm/Kbuild line 5
Quoted 5 lines
arch/um/include/asm/Kbuild +1
-0@@ -1,4 +1,5 @@ generic-y += barrier.h +generic-y += bpf_perf_event.h generic-y += bug.h generic-y += clkdev.h generic-y += current.h
Thanks,
//richard
Signature · 2 lines
sigma star gmbh - Eduard-Bodem-Gasse 6 - 6020 Innsbruck - Austria ATU66964118 - FN 374287y
Daniel Borkmann
maintainerPATCH 3/3 bpf: fix broken BPF selftest build
In reply to Daniel BorkmannAt least on x86_64, the kernel's BPF selftests seemed to have stopped to build due to 618e165b2a8e ("selftests/bpf: sync kernel headers and introduce arch support in Makefile"):
[...]
In file included from test_verifier.c:29:0:
../../../include/uapi/linux/bpf_perf_event.h:11:32:
fatal error: asm/bpf_perf_event.h: No such file or directory
#include <asm/bpf_perf_event.h>
^ compilation terminated.
[...]
While pulling in tools/arch/*/include/uapi/asm/bpf_perf_event.h seems to work fine, there's no automated fall-back logic right now that would do the same out of tools/include/uapi/asm-generic/bpf_perf_event.h. The usual convention today is to add a include/[uapi/]asm/ equivalent that would pull in the correct arch header or generic one as fall-back, all ifdef'ed based on compiler target definition. It's similarly done also in other cases such as tools/include/asm/barrier.h, thus adapt the same here.
--- tools/include/uapi/asm/bpf_perf_event.h | 7 +++++++ tools/testing/selftests/bpf/Makefile | 13 +------------ 2 files changed, 8 insertions(+), 12 deletions(-) create mode 100644 tools/include/uapi/asm/bpf_perf_event.h
tools/include/uapi/asm/bpf_perf_event.h new file +7 -0
@@ -0,0 +1,7 @@ +#if defined(__aarch64__) +#include "../../arch/arm64/include/uapi/asm/bpf_perf_event.h" +#elif defined(__s390__) +#include "../../arch/s390/include/uapi/asm/bpf_perf_event.h" +#else +#include <uapi/asm-generic/bpf_perf_event.h> +#endif
tools/testing/selftests/bpf/Makefile +1 -12
@@ -1,19 +1,8 @@ # SPDX-License-Identifier: GPL-2.0 -ifeq ($(srctree),) -srctree := $(patsubst %/,%,$(dir $(CURDIR))) -srctree := $(patsubst %/,%,$(dir $(srctree))) -srctree := $(patsubst %/,%,$(dir $(srctree))) -srctree := $(patsubst %/,%,$(dir $(srctree))) -endif -include $(srctree)/tools/scripts/Makefile.arch - -$(call detected_var,SRCARCH) - LIBDIR := ../../../lib BPFDIR := $(LIBDIR)/bpf APIDIR := ../../../include/uapi -ASMDIR:= ../../../arch/$(ARCH)/include/uapi GENDIR := ../../../../include/generated GENHDR := $(GENDIR)/autoconf.h
@@ -21,7 +10,7 @@ ifneq ($(wildcard $(GENHDR)),) GENFLAGS := -DHAVE_GENHDR endif -CFLAGS += -Wall -O2 -I$(APIDIR) -I$(ASMDIR) -I$(LIBDIR) -I$(GENDIR) $(GENFLAGS) -I../../../include +CFLAGS += -Wall -O2 -I$(APIDIR) -I$(LIBDIR) -I$(GENDIR) $(GENFLAGS) -I../../../include LDLIBS += -lcap -lelf TEST_GEN_PROGS = test_verifier test_tag test_maps test_lru_map test_lpm_map test_progs \
Signature · 1 line
2.9.5
Re: [PATCH bpf 3/3] bpf: fix broken BPF selftest build
Hi Daniel,
Quoted 57 lines · Daniel Borkmann
At least on
x86_64, the kernel's BPF selftests seemed to have stopped to build due to618e165b2a8e("selftests/bpf: sync kernel headers and introduce arch support inMakefile"):[...]
In file included from test_verifier.c:29:0:
../../../include/uapi/linux/bpf_perf_event.h:11:32:
fatal error:asm/bpf_perf_event.h: No such file or directory#include <asm/bpf_perf_event.h> ^compilation terminated.
[...]While pulling in
tools/arch/*/include/uapi/asm/bpf_perf_event.hseems to work fine, there's no automated fall-back logic right now that would do the same out oftools/include/uapi/asm-generic/bpf_perf_event.h. The usual convention today is to add a include/[uapi/]asm/ equivalent that would pull in the correct arch header or generic one as fall-back, all ifdef'ed based on compiler target definition. It's similarly done also in other cases such astools/include/asm/barrier.h, thus adapt the same here.Fixes 618e165b2a8e ("selftests/bpf: sync kernel headers and introduce arch support in Makefile")Signed-off-by Daniel Borkmann <[email protected]>Cc Hendrik Brueckner <[email protected]>Cc Arnaldo Carvalho de Melo <[email protected]>--- tools/include/uapi/asm/bpf_perf_event.h | 7 +++++++ tools/testing/selftests/bpf/Makefile | 13 +------------ 2 files changed, 8 insertions(+), 12 deletions(-) create mode 100644 tools/include/uapi/asm/bpf_perf_event.htools/include/uapi/asm/bpf_perf_event.h new file +7
-0@@ -0,0 +1,7 @@ +#if defined(__aarch64__) +#include "../../arch/arm64/include/uapi/asm/bpf_perf_event.h" +#elif defined(__s390__) +#include "../../arch/s390/include/uapi/asm/bpf_perf_event.h" +#else +#include <uapi/asm-generic/bpf_perf_event.h> +#endif tools/testing/selftests/bpf/Makefile +0
-4@@ -1,19 +1,8 @@ # SPDX-License-Identifier: GPL-2.0 -ifeq ($(srctree),) -srctree := $(patsubst %/,%,$(dir $(CURDIR))) -srctree := $(patsubst %/,%,$(dir $(srctree))) -srctree := $(patsubst %/,%,$(dir $(srctree)))
tools/testing/selftests/bpf/Makefile line 16
Quoted 10 lines
tools/testing/selftests/bpf/Makefile +0
-7-srctree := $(patsubst %/,%,$(dir $(srctree))) -endif -include $(srctree)/tools/scripts/Makefile.arch - -$(call detected_var,SRCARCH) - LIBDIR := ../../../lib BPFDIR := $(LIBDIR)/bpf APIDIR := ../../../include/uapi -ASMDIR:= ../../../arch/$(ARCH)/include/uapi
This is actually necessary on s390:
cc -Wall -O2 -I../../../include/uapi -I../../../lib -I../../../../include/generated -I../../../include test_verifier.c /root/git/linux/tools/testing/selftests/bpf/libbpf.a /root/git/linux/tools/testing/selftests/bpf/cgroup_helpers.c -lcap -lelf -o /root/git/linux/tools/testing/selftests/bpf/test_verifier
In file included from ../../../include/uapi/asm/bpf_perf_event.h:4:0,
from ../../../include/uapi/linux/bpf_perf_event.h:11,
from test_verifier.c:29:
../../../include/uapi/../../arch/s390/include/uapi/asm/bpf_perf_event.h:7:9: error: unknown type name 'user_pt_regs'
typedef user_pt_regs bpf_user_pt_regs_t;
^~~~~~~~~~~~make: *** [../lib.mk:109: /root/git/linux/tools/testing/selftests/bpf/test_verifier] Error 1
The s390 bpf_perf_event.h header file contains an #include for asm/ptrace.h that defines the user_pt_regs (introduce with my patch set). For building, the ptrace.h header file from the tooling must be used, not the local installed version.
Including the ptrace.h header file directly solves the issue. Feel free to merge below snippet into your patch.
Thanks and kind regards,
Hendrik
---
tools/arch/s390/include/uapi/asm/bpf_perf_event.h +1 -1
@@ -2,7 +2,7 @@#ifndef _UAPI__ASM_BPF_PERF_EVENT_H__#define _UAPI__ASM_BPF_PERF_EVENT_H__ -#include <asm/ptrace.h> +#include "ptrace.h" typedef user_pt_regs bpf_user_pt_regs_t ;
Daniel Borkmann
maintainerRe: [PATCH bpf 3/3] bpf: fix broken BPF selftest build
On 12/18/2017 10:28 AM, Hendrik Brueckner wrote:
[...]
Quoted 11 lines
This is actually necessary on s390:
cc -Wall -O2 -I../../../include/uapi -I../../../lib -I../../../../include/generated -I../../../include
test_verifier.c/root/git/linux/tools/testing/selftests/bpf/libbpf.a/root/git/linux/tools/testing/selftests/bpf/cgroup_helpers.c-lcap -lelf -o/root/git/linux/tools/testing/selftests/bpf/test_verifier
In file included from../../../include/uapi/asm/bpf_perf_event.h:4:0,from ../../../include/uapi/linux/bpf_perf_event.h:11, from test_verifier.c:29: ../../../include/uapi/../../arch/s390/include/uapi/asm/bpf_perf_event.h:7:9: error: unknown type name 'user_pt_regs' typedef user_pt_regs bpf_user_pt_regs_t; ^~~~~~~~~~~~make: *** [
../lib.mk:109:/root/git/linux/tools/testing/selftests/bpf/test_verifier] Error 1
Quoted 7 lines
The s390
bpf_perf_event.hheader file contains an #include forasm/ptrace.hthat defines theuser_pt_regs(introduce with my patch set). For building, theptrace.hheader file from the tooling must be used, not the local installed version.Including the
ptrace.hheader file directly solves the issue. Feel free to merge below snippet into your patch.
Argh, fwiw, I tested on x86 and arm64. :/ Given it already landed in Linus' tree, could you send me the below as an official patch and I'll take it via bpf? I think the below is minimal enough, and should then hopefully be the last remaining fallout on this uapi issue. I think it's fine to do it this way. The other option would be to pull in ptrace.h for all the others as well and map it similarly as with bpf_perf_event.h, but that gets out of hand very quickly (plus we could not have a default fallback include since it would otherwise be recursive). Anyway, could you add a comment over the include below stating that this is necessary to include this way? Just to make sure it doesn't accidentally get changed back until we have a proper framework in tools/ for dealing with asm includes.
Thanks a lot,
Daniel
Quoted 18 lines
Thanks and kind regards,
Hendrik---
tools/arch/s390/include/uapi/asm/bpf_perf_event.h +1
-1@@ -2,7 +2,7 @@#ifndef _UAPI__ASM_BPF_PERF_EVENT_H__#define _UAPI__ASM_BPF_PERF_EVENT_H__ -#include <asm/ptrace.h> +#include "ptrace.h" typedef user_pt_regs bpf_user_pt_regs_t ;
Alexei Starovoitov
AppliedQuoted 3 lines · Daniel Borkmann
Couple of outstanding fixes for BPF tree: 1) fixes a perf RB corruption, 2) and 3) fixes a few build issues from the recent
bpf_perf_event.huapi corrections. Thanks!
Applied, thanks Daniel!