Skip to contentSkip to conversation

Misc BPF fixes

PATCH Applied Daniel Borkmann 4 participants 8 messages Dec 12, 2017 – Dec 18, 2017

Subsystems (10)
lore
Share & tools
lore

Daniel Borkmann

author maintainer
#0 lore raw

cover 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
author maintainer
#1 lore raw

PATCH 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.

Fixes 20b9d7ac4852 ("bpf: avoid excessive stack usage for perf_sample_data")
---
 kernel/trace/bpf_trace.c | 19 ++++++++++++-------
 1 file changed, 12 insertions(+), 7 deletions(-)
kernel/trace/bpf_trace.c +12 -7
index 0ce99c3..40207c2 100644
@@ -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
author maintainer
#2 lore raw

PATCH 2/3 bpf: fix build issues on um due to mising bpf_perf_event.h

In reply to Daniel Borkmann

Since 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.

Fixes c895f6f703ad ("bpf: correct broken uapi for BPF_PROG_TYPE_PERF_EVENT program type")
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 +1 -0
index 50a32c3..73c57f6 100644
@@ -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
author maintainer
#4 lore raw

PATCH 3/3 bpf: fix broken BPF selftest build

In reply to Daniel Borkmann

At 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.

Fixes 618e165b2a8e ("selftests/bpf: sync kernel headers and introduce arch support in Makefile")
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.h
tools/include/uapi/asm/bpf_perf_event.h new file +7 -0
new file mode 100644
index 0000000..13a5853
@@ -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
index 21a2d76..792af7c 100644
@@ -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
Acked-by on v1 2/3
#3 lore raw

Re: [PATCH bpf 2/3] bpf: fix build issues on um due to mising bpf_perf_event.h

In reply to Daniel Borkmann

Am Dienstag, 12. Dezember 2017, 02:25:31 CET schrieb Daniel Borkmann:

Quoted 36 lines

Since 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.

Fixes c895f6f703ad ("bpf: correct broken uapi for BPF_PROG_TYPE_PERF_EVENT

program type") Reported-by: Randy Dunlap <[email protected]>

---
 arch/um/include/asm/Kbuild | 1 +
 1 file changed, 1 insertion(+)
arch/um/include/asm/Kbuild +0 -0
index 50a32c3..73c57f6 100644

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
#7 lore raw
In reply to Daniel Borkmann
Quoted 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.h uapi corrections. Thanks!

Applied, thanks Daniel!

#5 lore raw

Re: [PATCH bpf 3/3] bpf: fix broken BPF selftest build

In reply to Daniel Borkmann

Hi Daniel,

Quoted 57 lines · Daniel Borkmann

At 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.

Fixes 618e165b2a8e ("selftests/bpf: sync kernel headers and introduce arch support in Makefile")
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.h
tools/include/uapi/asm/bpf_perf_event.h new file +7 -0
new file mode 100644
index 0000000..13a5853
@@ -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
index 21a2d76..792af7c 100644
@@ -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
index cefe7c7cd4f6..0a8e37a519f2 100644
@@ -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;
 
author maintainer
#6 lore raw

Re: [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.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.

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
index cefe7c7cd4f6..0a8e37a519f2 100644
@@ -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;