Skip to contentSkip to conversation

perf/bpf: Fix NULL pointer dereference

PATCH Applied Luo Gengkun 3 participants 13 messages Nov 26 – Nov 27

Subsystems (6)
lore
Share & tools
lore
#0 lore raw

cover perf/bpf: Fix NULL pointer dereference

We hit the following problem on x86:

	BUG: kernel NULL pointer dereference, address: 0000000000000000
	#PF: supervisor instruction fetch in kernel mode
	#PF: error_code(0x0010) - not-present page
	RIP: 0010:0x0
	Call Trace:
	 <IRQ>
	 bpf_overflow_handler+0xa4/0x120
	 __perf_event_overflow+0x11d/0x2f0
	 perf_swevent_hrtimer+0x127/0x140
	 __hrtimer_run_queues+0x108/0x250
	 hrtimer_interrupt+0xfb/0x230
	 sysvec_apic_timer_interrupt+0x69/0x90
	 </IRQ>
	 <TASK>
	 asm_sysvec_apic_timer_interrupt+0x1a/0x20
	 __perf_event_set_bpf_prog+0xb7/0x180
	 _perf_ioctl+0x1f9/0xb40
	 perf_ioctl+0x41/0x70
	 __se_sys_ioctl+0x82/0xc0
	 do_syscall_64+0x44/0xb0
	 entry_SYSCALL_64_after_hwframe+0x78/0xe2
	 </TASK>

RIP is 0 with error_code(0x0010) (instruction fetch, not-present), i.e. an indirect call through a NULL function pointer.

perf_event_set_bpf_handler() records the handler currently installed on the event and replaces it with bpf_overflow_handler():

	event->prog = prog;
	event->bpf_cookie = bpf_cookie;
	event->orig_overflow_handler = READ_ONCE(event->overflow_handler);
	WRITE_ONCE(event->overflow_handler, bpf_overflow_handler);

WRITE_ONCE() is a single-copy atomic access, not a compiler barrier, and the store to orig_overflow_handler is a plain assignment, so the compiler may reorder the two stores. GCC 11.4.0 does; disassembly of __perf_event_set_bpf_prog():

	mov    0x3d0(%rbp),%rax                  ; prev = overflow_handler
	mov    %rsi,0x3e8(%rbp)                  ; event->prog = prog
	mov    %r13,0x3f0(%rbp)                  ; event->bpf_cookie = cookie
	movq   $bpf_overflow_handler,0x3d0(%rbp) ; publish overflow_handler
	mov    %rax,0x3e0(%rbp)                  ; orig_overflow_handler = prev

For a software event the overflow comes from perf_swevent_hrtimer() in hardirq context on the same CPU, so no memory ordering is involved at all, only the store order; the publish lands first and the hrtimer interrupt fires in between. bpf_overflow_handler() then runs the attached BPF program and, for a non-zero return value, calls the still-NULL orig_overflow_handler (struct perf_event is allocated with __GFP_ZERO).

Upstream status:
Not affected. f11f10bfa1ca ("perf/bpf: Call BPF handler directly, not through overflow machinery", v6.10) removed orig_overflow_handler and calls bpf_overflow_handler() directly from __perf_event_overflow(), so the reordering window no longer exists. That rework was not backported to any stable tree, and there is no standalone upstream fix for the ordering itself.

Affected version:
Introduced by aa6a5f3cb2b2 ("perf, bpf: add perf events core support for BPF_PROG_TYPE_PERF_EVENT programs", v4.9) and still present in the 5.10, 5.15, 6.1 and 6.6 LTS trees; v6.10 removed the field altogether.

To backport f11f10bfa1ca ("perf/bpf: Call BPF handler directly, not through overflow machinery"), that commit has to come with the series it was posted in, and with the fixups it later needed. f11f is not a standalone change: it calls bpf_overflow_handler() from __perf_event_overflow(), i.e. from a position the series first moves that function to, and the new call site reads struct perf_event members that the series first un-conditionalizes. On its own it does not produce a tree that builds in every Kconfig combination, and it regresses bpftrace until the follow-up fix is in.

The series f11f was posted in, and which this backport carries in full:

1. 4c03fe11b96b perf/bpf: Reorder bpf_overflow_handler() ahead of
__perf_event_overflow() [prerequisite]
2. 924d934393f9 perf/bpf: Create bpf_overflow_handler() stub for
!CONFIG_BPF_SYSCALL [prerequisite]
3. 14e40a9578b7 perf/bpf: Remove #ifdef CONFIG_BPF_SYSCALL from struct
perf_event members [prerequisite]
4. f11f10bfa1ca perf/bpf: Call BPF handler directly, not through
overflow machinery [the fix]
5. 76f6d5884582 perf/bpf: Remove unneeded uses_default_overflow_handler()
6. c4fcc7d1f415 perf/bpf: Allow a BPF program to suppress all sample
side effects [rest of the series]

f11f (0004 here) is the fix. 0001-0003 are prerequisites: 0001 moves bpf_overflow_handler() ahead of __perf_event_overflow(), which is where 0004 calls it from; 0002 and 0003 make the new call site, which sits outside #ifdef CONFIG_BPF_SYSCALL and reads event->prog unconditionally, work when BPF is disabled. 0005 and 0006 are part of the same series: they clean up what the refactoring left behind and add the ability to suppress side effects, which is the point of the rework but not the reason we are backporting it.

The follow-up fixes, i.e. what upstream had to correct after the series landed:

7. 93d3fde7fd19 perf/bpf: Change the !CONFIG_BPF_SYSCALL stubs to
static inlines [compile fix, Ingo]
8. 854dd99b5ddc perf/bpf: Mark perf_event_set_bpf_handler() and
perf_event_free_bpf_handler() as inline too [compile fix, Ingo]
9. 100bff23818e perf/bpf: Don't call bpf_overflow_handler() for tracing
events [Fixes: f11f10bfa1ca]

The result is a verbatim backport of the series and its follow-up fixes (one hunk re-based, see "Adaptation notes"), so the tree stays identical to mainline and later perf/bpf backports continue to apply.

Adaptation notes
----------------

Only 0005 needs manual handling. 0001-0004 and 0006-0009 apply with plain "git apply". 0005 conflicts on include/linux/perf_event.h because the hunk trailer references perf_event_header__init_id(), which does not exist in 6.6.y (perf_event_header__init() still has the extra type/misc/size arguments). It was applied by replacing exactly the removed block; the resulting file is byte-identical to the mainline result for that hunk.

Ingo Molnar (2):
perf/bpf: Change the !CONFIG_BPF_SYSCALL stubs to static inlines perf/bpf: Mark perf_event_set_bpf_handler() and
perf_event_free_bpf_handler() as inline too

Kyle Huey (7):
perf/bpf: Reorder bpf_overflow_handler() ahead of

    __perf_event_overflow()

perf/bpf: Create bpf_overflow_handler() stub for !CONFIG_BPF_SYSCALL perf/bpf: Remove #ifdef CONFIG_BPF_SYSCALL from struct perf_event
members
perf/bpf: Call BPF handler directly, not through overflow machinery perf/bpf: Remove unneeded uses_default_overflow_handler() perf/bpf: Allow a BPF program to suppress all sample side effects perf/bpf: Don't call bpf_overflow_handler() for tracing events

 arch/arm/kernel/hw_breakpoint.c   |   8 +-
 arch/arm64/kernel/hw_breakpoint.c |   4 +-
 include/linux/perf_event.h        |  23 +---
 kernel/events/core.c              | 193 +++++++++++++++---------------
 4 files changed, 108 insertions(+), 120 deletions(-)
Signature · 1 line
2.34.1
#1 lore raw

PATCH 1/9 perf/bpf: Reorder bpf_overflow_handler() ahead of __perf_event_overflow()

From: Kyle Huey <[email protected]>

[ Upstream commit 4c03fe11b96bda60610aca77002e83f37b4a2242 ]

This will allow __perf_event_overflow() to call bpf_overflow_handler().

---
 kernel/events/core.c | 183 ++++++++++++++++++++++---------------------
 1 file changed, 92 insertions(+), 91 deletions(-)
kernel/events/core.c +92 -91
index e9849303f647..4a3125918330 100644
@@ -9599,6 +9599,98 @@ static inline bool sample_is_allowed(struct perf_event *event, struct pt_regs *r
 	return true;
 }
 
+#ifdef CONFIG_BPF_SYSCALL
+static void bpf_overflow_handler(struct perf_event *event,
+				 struct perf_sample_data *data,
+				 struct pt_regs *regs)
+{
+	struct bpf_perf_event_data_kern ctx = {
+		.data = data,
+		.event = event,
+	};
+	struct bpf_prog *prog;
+	int ret = 0;
+
+	ctx.regs = perf_arch_bpf_user_pt_regs(regs);
+	if (unlikely(__this_cpu_inc_return(bpf_prog_active) != 1))
+		goto out;
+	rcu_read_lock();
+	prog = READ_ONCE(event->prog);
+	if (prog) {
+		perf_prepare_sample(data, event, regs);
+		ret = bpf_prog_run(prog, &ctx);
+	}
+	rcu_read_unlock();
+out:
+	__this_cpu_dec(bpf_prog_active);
+	if (!ret)
+		return;
+
+	event->orig_overflow_handler(event, data, regs);
+}
+
+static int perf_event_set_bpf_handler(struct perf_event *event,
+				      struct bpf_prog *prog,
+				      u64 bpf_cookie)
+{
+	if (event->overflow_handler_context)
+		/* hw breakpoint or kernel counter */
+		return -EINVAL;
+
+	if (event->prog)
+		return -EEXIST;
+
+	if (prog->type != BPF_PROG_TYPE_PERF_EVENT)
+		return -EINVAL;
+
+	if (event->attr.precise_ip &&
+	    prog->call_get_stack &&
+	    (!(event->attr.sample_type & PERF_SAMPLE_CALLCHAIN) ||
+	     event->attr.exclude_callchain_kernel ||
+	     event->attr.exclude_callchain_user)) {
+		/*
+		 * On perf_event with precise_ip, calling bpf_get_stack()
+		 * may trigger unwinder warnings and occasional crashes.
+		 * bpf_get_[stack|stackid] works around this issue by using
+		 * callchain attached to perf_sample_data. If the
+		 * perf_event does not full (kernel and user) callchain
+		 * attached to perf_sample_data, do not allow attaching BPF
+		 * program that calls bpf_get_[stack|stackid].
+		 */
+		return -EPROTO;
+	}
+
+	event->prog = prog;
+	event->bpf_cookie = bpf_cookie;
+	event->orig_overflow_handler = READ_ONCE(event->overflow_handler);
+	WRITE_ONCE(event->overflow_handler, bpf_overflow_handler);
+	return 0;
+}
+
+static void perf_event_free_bpf_handler(struct perf_event *event)
+{
+	struct bpf_prog *prog = event->prog;
+
+	if (!prog)
+		return;
+
+	WRITE_ONCE(event->overflow_handler, event->orig_overflow_handler);
+	event->prog = NULL;
+	bpf_prog_put(prog);
+}
+#else
+static int perf_event_set_bpf_handler(struct perf_event *event,
+				      struct bpf_prog *prog,
+				      u64 bpf_cookie)
+{
+	return -EOPNOTSUPP;
+}
+
+static void perf_event_free_bpf_handler(struct perf_event *event)
+{
+}
+#endif
+
 /*
  * Generic event overflow handling, sampling.
  */
@@ -10513,97 +10605,6 @@ static void perf_event_free_filter(struct perf_event *event)
 	ftrace_profile_free_filter(event);
 }
 
-#ifdef CONFIG_BPF_SYSCALL
-static void bpf_overflow_handler(struct perf_event *event,
-				 struct perf_sample_data *data,
-				 struct pt_regs *regs)
-{
-	struct bpf_perf_event_data_kern ctx = {
-		.data = data,
-		.event = event,
-	};
-	struct bpf_prog *prog;
-	int ret = 0;
-
-	ctx.regs = perf_arch_bpf_user_pt_regs(regs);
-	if (unlikely(__this_cpu_inc_return(bpf_prog_active) != 1))
-		goto out;
-	rcu_read_lock();
-	prog = READ_ONCE(event->prog);
-	if (prog) {
-		perf_prepare_sample(data, event, regs);
-		ret = bpf_prog_run(prog, &ctx);
-	}
-	rcu_read_unlock();
-out:
-	__this_cpu_dec(bpf_prog_active);
-	if (!ret)
-		return;
-
-	event->orig_overflow_handler(event, data, regs);
-}
-
-static int perf_event_set_bpf_handler(struct perf_event *event,
-				      struct bpf_prog *prog,
-				      u64 bpf_cookie)
-{
-	if (event->overflow_handler_context)
-		/* hw breakpoint or kernel counter */
-		return -EINVAL;
-
-	if (event->prog)
-		return -EEXIST;
-
-	if (prog->type != BPF_PROG_TYPE_PERF_EVENT)
-		return -EINVAL;
-
-	if (event->attr.precise_ip &&
-	    prog->call_get_stack &&
-	    (!(event->attr.sample_type & PERF_SAMPLE_CALLCHAIN) ||
-	     event->attr.exclude_callchain_kernel ||
-	     event->attr.exclude_callchain_user)) {
-		/*
-		 * On perf_event with precise_ip, calling bpf_get_stack()
-		 * may trigger unwinder warnings and occasional crashes.
-		 * bpf_get_[stack|stackid] works around this issue by using
-		 * callchain attached to perf_sample_data. If the
-		 * perf_event does not full (kernel and user) callchain
-		 * attached to perf_sample_data, do not allow attaching BPF
-		 * program that calls bpf_get_[stack|stackid].
-		 */
-		return -EPROTO;
-	}
-
-	event->prog = prog;
-	event->bpf_cookie = bpf_cookie;
-	event->orig_overflow_handler = READ_ONCE(event->overflow_handler);
-	WRITE_ONCE(event->overflow_handler, bpf_overflow_handler);
-	return 0;
-}
-
-static void perf_event_free_bpf_handler(struct perf_event *event)
-{
-	struct bpf_prog *prog = event->prog;
-
-	if (!prog)
-		return;
-
-	WRITE_ONCE(event->overflow_handler, event->orig_overflow_handler);
-	event->prog = NULL;
-	bpf_prog_put(prog);
-}
-#else
-static int perf_event_set_bpf_handler(struct perf_event *event,
-				      struct bpf_prog *prog,
-				      u64 bpf_cookie)
-{
-	return -EOPNOTSUPP;
-}
-static void perf_event_free_bpf_handler(struct perf_event *event)
-{
-}
-#endif
-
 /*
  * returns true if the event is a tracepoint, or a kprobe/upprobe created
  * with perf_event_open()
Signature · 1 line
2.34.1
#2 lore raw

PATCH 2/9 perf/bpf: Create bpf_overflow_handler() stub for !CONFIG_BPF_SYSCALL

In reply to Luo Gengkun

From: Kyle Huey <[email protected]>

[ Upstream commit 924d934393f98fa6a41d6ea27352faf79c2bbaf6 ]

This will allow __perf_event_overflow() (which is independent of CONFIG_BPF_SYSCALL) to call bpf_overflow_handler().

---
 kernel/events/core.c | 6 ++++++
 1 file changed, 6 insertions(+)
kernel/events/core.c +6 -0
index 4a3125918330..523d9bbc305b 100644
@@ -9679,6 +9679,12 @@ static void perf_event_free_bpf_handler(struct perf_event *event)
 	bpf_prog_put(prog);
 }
 #else
+static void bpf_overflow_handler(struct perf_event *event,
+				 struct perf_sample_data *data,
+				 struct pt_regs *regs)
+{
+}
+
 static int perf_event_set_bpf_handler(struct perf_event *event,
 				      struct bpf_prog *prog,
 				      u64 bpf_cookie)
Signature · 1 line
2.34.1
#3 lore raw

PATCH 3/9 perf/bpf: Remove #ifdef CONFIG_BPF_SYSCALL from struct perf_event members

In reply to Luo Gengkun

From: Kyle Huey <[email protected]>

[ Upstream commit 14e40a9578b70cc5323e55f61292a7e021f6037c ]

This will allow __perf_event_overflow() (which is independent of CONFIG_BPF_SYSCALL) to use struct perf_event's prog to decide whether to call bpf_overflow_handler().

---
 include/linux/perf_event.h | 2 --
 1 file changed, 2 deletions(-)
include/linux/perf_event.h +0 -2
index 1a0df11dddc1..b504d7eec86f 100644
@@ -809,11 +809,9 @@ struct perf_event {
 	u64				(*clock)(void);
 	perf_overflow_handler_t		overflow_handler;
 	void				*overflow_handler_context;
-#ifdef CONFIG_BPF_SYSCALL
 	perf_overflow_handler_t		orig_overflow_handler;
 	struct bpf_prog			*prog;
 	u64				bpf_cookie;
-#endif
 
 #ifdef CONFIG_EVENT_TRACING
 	struct trace_event_call		*tp_event;
Signature · 1 line
2.34.1
#4 lore raw

PATCH 4/9 perf/bpf: Call BPF handler directly, not through overflow machinery

In reply to Luo Gengkun

From: Kyle Huey <[email protected]>

[ Upstream commit f11f10bfa1ca23b32020b2073aa13131a27978fe ]

To ultimately allow BPF programs attached to perf events to completely suppress all of the effects of a perf event overflow (rather than just the sample output, as they do today), call bpf_overflow_handler() from __perf_event_overflow() directly rather than modifying struct perf_event's overflow_handler. Return the BPF program's return value from bpf_overflow_handler() so that __perf_event_overflow() knows how to proceed. Remove the now unnecessary orig_overflow_handler from struct perf_event.

This patch is solely a refactoring and results in no behavior change.

---
 include/linux/perf_event.h |  6 +-----
 kernel/events/core.c       | 27 +++++++++++----------------
 2 files changed, 12 insertions(+), 21 deletions(-)
include/linux/perf_event.h +1 -5
index b504d7eec86f..f3bf97c4877b 100644
@@ -809,7 +809,6 @@ struct perf_event {
 	u64				(*clock)(void);
 	perf_overflow_handler_t		overflow_handler;
 	void				*overflow_handler_context;
-	perf_overflow_handler_t		orig_overflow_handler;
 	struct bpf_prog			*prog;
 	u64				bpf_cookie;
 
@@ -1352,10 +1351,7 @@ __is_default_overflow_handler(perf_overflow_handler_t overflow_handler)
 #ifdef CONFIG_BPF_SYSCALL
 static inline bool uses_default_overflow_handler(struct perf_event *event)
 {
-	if (likely(is_default_overflow_handler(event)))
-		return true;
-
-	return __is_default_overflow_handler(event->orig_overflow_handler);
+	return is_default_overflow_handler(event);
 }
 #else
 #define uses_default_overflow_handler(event) \
kernel/events/core.c +11 -16
index 523d9bbc305b..ba9cbf50773e 100644
@@ -9600,9 +9600,9 @@ static inline bool sample_is_allowed(struct perf_event *event, struct pt_regs *r
 }
 
 #ifdef CONFIG_BPF_SYSCALL
-static void bpf_overflow_handler(struct perf_event *event,
-				 struct perf_sample_data *data,
-				 struct pt_regs *regs)
+static int bpf_overflow_handler(struct perf_event *event,
+				struct perf_sample_data *data,
+				struct pt_regs *regs)
 {
 	struct bpf_perf_event_data_kern ctx = {
 		.data = data,
@@ -9623,10 +9623,8 @@ static void bpf_overflow_handler(struct perf_event *event,
 	rcu_read_unlock();
 out:
 	__this_cpu_dec(bpf_prog_active);
-	if (!ret)
-		return;
 
-	event->orig_overflow_handler(event, data, regs);
+	return ret;
 }
 
 static int perf_event_set_bpf_handler(struct perf_event *event,
@@ -9662,8 +9660,6 @@ static int perf_event_set_bpf_handler(struct perf_event *event,
 
 	event->prog = prog;
 	event->bpf_cookie = bpf_cookie;
-	event->orig_overflow_handler = READ_ONCE(event->overflow_handler);
-	WRITE_ONCE(event->overflow_handler, bpf_overflow_handler);
 	return 0;
 }
 
@@ -9674,15 +9670,15 @@ static void perf_event_free_bpf_handler(struct perf_event *event)
 	if (!prog)
 		return;
 
-	WRITE_ONCE(event->overflow_handler, event->orig_overflow_handler);
 	event->prog = NULL;
 	bpf_prog_put(prog);
 }
 #else
-static void bpf_overflow_handler(struct perf_event *event,
-				 struct perf_sample_data *data,
-				 struct pt_regs *regs)
+static int bpf_overflow_handler(struct perf_event *event,
+				struct perf_sample_data *data,
+				struct pt_regs *regs)
 {
+	return 1;
 }
 
 static int perf_event_set_bpf_handler(struct perf_event *event,
@@ -9766,7 +9762,8 @@ static int __perf_event_overflow(struct perf_event *event,
 		irq_work_queue(&event->pending_irq);
 	}
 
-	READ_ONCE(event->overflow_handler)(event, data, regs);
+	if (!(event->prog && !bpf_overflow_handler(event, data, regs)))
+		READ_ONCE(event->overflow_handler)(event, data, regs);
 
 	if (*perf_event_fasync(event) && event->pending_kill) {
 		event->pending_wakeup = 1;
@@ -12121,13 +12118,11 @@ perf_event_alloc(struct perf_event_attr *attr, int cpu,
 		overflow_handler = parent_event->overflow_handler;
 		context = parent_event->overflow_handler_context;
 #if defined(CONFIG_BPF_SYSCALL) && defined(CONFIG_EVENT_TRACING)
-		if (overflow_handler == bpf_overflow_handler) {
+		if (parent_event->prog) {
 			struct bpf_prog *prog = parent_event->prog;
 
 			bpf_prog_inc(prog);
 			event->prog = prog;
-			event->orig_overflow_handler =
-				parent_event->orig_overflow_handler;
 		}
 #endif
 	}
Signature · 1 line
2.34.1
#5 lore raw

Re: [PATCH stable/linux-6.6.y 4/9] perf/bpf: Call BPF handler directly, not through overflow machinery

Thank you for your contribution! Sashiko AI review found no new issues in this patch, but noticed 1 related pre-existing issue it does not introduce: - [High] perf: missing SLAB_ACCOUNT in…

Thank you for your contribution! Sashiko AI review found no new issues in this patch, but noticed 1 related pre-existing issue it does not introduce:
- [High] perf: missing SLAB_ACCOUNT in perf_event_init() [1] --

commit 4af8ba4aec15ed1b6050b9027e93e306d86849aa
Author: Kyle Huey <[email protected]>

perf/bpf: Call BPF handler directly, not through overflow machinery

This commit refactors BPF perf event overflow handling to invoke bpf_overflow_handler() directly from __perf_event_overflow() instead of replacing event->overflow_handler, and removes orig_overflow_handler from struct perf_event.

Quoted 4 lines
kernel/events/core.c +0 -0
index 523d9bbc305bd..ba9cbf50773e8 100644

[ ... ]

Quoted 5 lines
kernel/events/core.c +0 -1
@@ -12121,13 +12118,11 @@ perf_event_alloc(struct perf_event_attr *attr, int cpu,
 		overflow_handler = parent_event->overflow_handler;
 		context = parent_event->overflow_handler_context;
 #if defined(CONFIG_BPF_SYSCALL) && defined(CONFIG_EVENT_TRACING)
-		if (overflow_handler == bpf_overflow_handler) {

kernel/events/core.c perf_event_alloc(struct perf_event_attr *attr, int cpu, · line 12128

Quoted 10 lines
kernel/events/core.c +1 -2
+		if (parent_event->prog) {
 			struct bpf_prog *prog = parent_event->prog;
 
 			bpf_prog_inc(prog);
 			event->prog = prog;
-			event->orig_overflow_handler =
-				parent_event->orig_overflow_handler;
 		}
 #endif
 	}

[Severity: High]
This problem wasn't introduced by this patch, but should perf_event_cache use SLAB_ACCOUNT (or GFP_KERNEL_ACCOUNT in perf_event_alloc()) so that struct perf_event allocations are charged to the calling process's memory cgroup [1]?

perf_event_init() creates perf_event_cache with KMEM_CACHE(perf_event, SLAB_PANIC), and perf_event_alloc() allocates from it with GFP_KERNEL | __GFP_ZERO. A containerized task can repeatedly call perf_event_open() to allocate unaccounted perf_event objects, bypassing its memcg limits and exhausting host kernel memory.

[1] https://sashiko.dev/#/bug/linux-83d96148-5109-41b8-9975-d17680148a68

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
#6 lore raw

PATCH 5/9 perf/bpf: Remove unneeded uses_default_overflow_handler()

In reply to Luo Gengkun

From: Kyle Huey <[email protected]>

[ Upstream commit 76f6d58845829e5d6ef55532e67a323e7d30c26e ]

Now that struct perf_event's orig_overflow_handler is gone, there's no need for the functions and macros to support looking past overflow_handler to orig_overflow_handler.

This patch is solely a refactoring and results in no behavior change.

---
 arch/arm/kernel/hw_breakpoint.c   |  8 ++++----
 arch/arm64/kernel/hw_breakpoint.c |  4 ++--
 include/linux/perf_event.h        | 17 +++--------------
 3 files changed, 9 insertions(+), 20 deletions(-)
arch/arm/kernel/hw_breakpoint.c +4 -4
index dc0fb7a81371..054e9199f30d 100644
@@ -626,7 +626,7 @@ int hw_breakpoint_arch_parse(struct perf_event *bp,
 	hw->address &= ~alignment_mask;
 	hw->ctrl.len <<= offset;
 
-	if (uses_default_overflow_handler(bp)) {
+	if (is_default_overflow_handler(bp)) {
 		/*
 		 * Mismatch breakpoints are required for single-stepping
 		 * breakpoints.
@@ -798,7 +798,7 @@ static void watchpoint_handler(unsigned long addr, unsigned int fsr,
 		 * Otherwise, insert a temporary mismatch breakpoint so that
 		 * we can single-step over the watchpoint trigger.
 		 */
-		if (!uses_default_overflow_handler(wp))
+		if (!is_default_overflow_handler(wp))
 			continue;
 step:
 		enable_single_step(wp, instruction_pointer(regs));
@@ -811,7 +811,7 @@ static void watchpoint_handler(unsigned long addr, unsigned int fsr,
 		info->trigger = addr;
 		pr_debug("watchpoint fired: address = 0x%x\n", info->trigger);
 		perf_bp_event(wp, regs);
-		if (uses_default_overflow_handler(wp))
+		if (is_default_overflow_handler(wp))
 			enable_single_step(wp, instruction_pointer(regs));
 	}
 
@@ -886,7 +886,7 @@ static void breakpoint_handler(unsigned long unknown, struct pt_regs *regs)
 			info->trigger = addr;
 			pr_debug("breakpoint fired: address = 0x%x\n", addr);
 			perf_bp_event(bp, regs);
-			if (uses_default_overflow_handler(bp))
+			if (is_default_overflow_handler(bp))
 				enable_single_step(bp, addr);
 			goto unlock;
 		}
arch/arm64/kernel/hw_breakpoint.c +2 -2
index c94dcffb7e83..559bf7b5f0f0 100644
@@ -663,7 +663,7 @@ static int breakpoint_handler(unsigned long unused, unsigned long esr,
 		perf_bp_event(bp, regs);
 
 		/* Do we need to handle the stepping? */
-		if (uses_default_overflow_handler(bp))
+		if (is_default_overflow_handler(bp))
 			step = 1;
 unlock:
 		rcu_read_unlock();
@@ -742,7 +742,7 @@ static u64 get_distance_from_watchpoint(unsigned long addr, u64 val,
 static int watchpoint_report(struct perf_event *wp, unsigned long addr,
 			     struct pt_regs *regs)
 {
-	int step = uses_default_overflow_handler(wp);
+	int step = is_default_overflow_handler(wp);
 	struct arch_hw_breakpoint *info = counter_arch_bp(wp);
 
 	info->trigger = addr;
include/linux/perf_event.h +3 -14
index f3bf97c4877b..43033a651bc0 100644
@@ -1336,8 +1336,10 @@ extern int perf_event_output(struct perf_event *event,
 			     struct pt_regs *regs);
 
 static inline bool
-__is_default_overflow_handler(perf_overflow_handler_t overflow_handler)
+is_default_overflow_handler(struct perf_event *event)
 {
+	perf_overflow_handler_t overflow_handler = event->overflow_handler;
+
 	if (likely(overflow_handler == perf_event_output_forward))
 		return true;
 	if (unlikely(overflow_handler == perf_event_output_backward))
@@ -1345,19 +1347,6 @@ __is_default_overflow_handler(perf_overflow_handler_t overflow_handler)
 	return false;
 }
 
-#define is_default_overflow_handler(event) \
-	__is_default_overflow_handler((event)->overflow_handler)
-
-#ifdef CONFIG_BPF_SYSCALL
-static inline bool uses_default_overflow_handler(struct perf_event *event)
-{
-	return is_default_overflow_handler(event);
-}
-#else
-#define uses_default_overflow_handler(event) \
-	is_default_overflow_handler(event)
-#endif
-
 extern void
 perf_event_header__init(struct perf_event_header *header,
 			struct perf_sample_data *data,
Signature · 1 line
2.34.1
#7 lore raw

PATCH 6/9 perf/bpf: Allow a BPF program to suppress all sample side effects

In reply to Luo Gengkun

From: Kyle Huey <[email protected]>

[ Upstream commit c4fcc7d1f41532e878087c7c43f4cf247604d68b ]

Returning zero from a BPF program attached to a perf event already suppresses any data output. Return early from __perf_event_overflow() in this case so it will also suppress event_limit accounting, SIGTRAP generation, and F_ASYNC signalling.

---
 kernel/events/core.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
kernel/events/core.c +4 -2
index ba9cbf50773e..a6d7fc2a9ab2 100644
@@ -9713,6 +9713,9 @@ static int __perf_event_overflow(struct perf_event *event,
 
 	ret = __perf_event_account_interrupt(event, throttle);
 
+	if (event->prog && !bpf_overflow_handler(event, data, regs))
+		return ret;
+
 	/*
 	 * XXX event_limit might not quite work as expected on inherited
 	 * events
@@ -9762,8 +9765,7 @@ static int __perf_event_overflow(struct perf_event *event,
 		irq_work_queue(&event->pending_irq);
 	}
 
-	if (!(event->prog && !bpf_overflow_handler(event, data, regs)))
-		READ_ONCE(event->overflow_handler)(event, data, regs);
+	READ_ONCE(event->overflow_handler)(event, data, regs);
 
 	if (*perf_event_fasync(event) && event->pending_kill) {
 		event->pending_wakeup = 1;
Signature · 1 line
2.34.1
#8 lore raw

PATCH 7/9 perf/bpf: Change the !CONFIG_BPF_SYSCALL stubs to static inlines

In reply to Luo Gengkun

From: Ingo Molnar <[email protected]>

[ Upstream commit 93d3fde7fd19c2e2cde7220e7986f9a75e9c5680 ]

Otherwise the compiler will be unhappy if they go unused, which they do on allnoconfigs.

---
 kernel/events/core.c | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)
kernel/events/core.c +7 -7
index a6d7fc2a9ab2..edced196597c 100644
@@ -9674,21 +9674,21 @@ static void perf_event_free_bpf_handler(struct perf_event *event)
 	bpf_prog_put(prog);
 }
 #else
-static int bpf_overflow_handler(struct perf_event *event,
-				struct perf_sample_data *data,
-				struct pt_regs *regs)
+static inline int bpf_overflow_handler(struct perf_event *event,
+				       struct perf_sample_data *data,
+				       struct pt_regs *regs)
 {
 	return 1;
 }
 
-static int perf_event_set_bpf_handler(struct perf_event *event,
-				      struct bpf_prog *prog,
-				      u64 bpf_cookie)
+static inline int perf_event_set_bpf_handler(struct perf_event *event,
+					     struct bpf_prog *prog,
+					     u64 bpf_cookie)
 {
 	return -EOPNOTSUPP;
 }
 
-static void perf_event_free_bpf_handler(struct perf_event *event)
+static inline void perf_event_free_bpf_handler(struct perf_event *event)
 {
 }
 #endif
Signature · 1 line
2.34.1
#9 lore raw

PATCH 8/9 perf/bpf: Mark perf_event_set_bpf_handler() and perf_event_free_bpf_handler() as inline too

In reply to Luo Gengkun

From: Ingo Molnar <[email protected]>

[ Upstream commit 854dd99b5ddc9d90e31e5f112462a5994dd31810 ]

They can be unused with certain Kconfig variations:

kernel/events/core.c:9622:13: warning: ‘perf_event_free_bpf_handler’ defined but not used [-Wunused-function] kernel/events/core.c:9586:12: warning: ‘perf_event_set_bpf_handler’ defined but not used [-Wunused-function]

Since they are both single-use, mark them inline.

---
 kernel/events/core.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
kernel/events/core.c +4 -4
index edced196597c..c56c7ca511c1 100644
@@ -9627,9 +9627,9 @@ static int bpf_overflow_handler(struct perf_event *event,
 	return ret;
 }
 
-static int perf_event_set_bpf_handler(struct perf_event *event,
-				      struct bpf_prog *prog,
-				      u64 bpf_cookie)
+static inline int perf_event_set_bpf_handler(struct perf_event *event,
+					     struct bpf_prog *prog,
+					     u64 bpf_cookie)
 {
 	if (event->overflow_handler_context)
 		/* hw breakpoint or kernel counter */
@@ -9663,7 +9663,7 @@ static int perf_event_set_bpf_handler(struct perf_event *event,
 	return 0;
 }
 
-static void perf_event_free_bpf_handler(struct perf_event *event)
+static inline void perf_event_free_bpf_handler(struct perf_event *event)
 {
 	struct bpf_prog *prog = event->prog;
 
Signature · 1 line
2.34.1
#10 lore raw

PATCH 9/9 perf/bpf: Don't call bpf_overflow_handler() for tracing events

In reply to Luo Gengkun

From: Kyle Huey <[email protected]>

[ Upstream commit 100bff23818eb61751ed05d64a7df36ce9728a4d ]

The regressing commit is new in 6.10. It assumed that anytime event->prog is set bpf_overflow_handler() should be invoked to execute the attached bpf program. This assumption is false for tracing events, and as a result the regressing commit broke bpftrace by invoking the bpf handler with garbage inputs on overflow.

Prior to the regression the overflow handlers formed a chain (of length 0, 1, or 2) and perf_event_set_bpf_handler() (the !tracing case) added bpf_overflow_handler() to that chain, while perf_event_attach_bpf_prog() (the tracing case) did not. Both set event->prog. The chain of overflow handlers was replaced by a single overflow handler slot and a fixed call to bpf_overflow_handler() when appropriate. This modifies the condition there to check event->prog->type == BPF_PROG_TYPE_PERF_EVENT, restoring the previous behavior and fixing bpftrace.

---
 kernel/events/core.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
kernel/events/core.c +2 -1
index c56c7ca511c1..bdc4bcb91e0f 100644
@@ -9713,7 +9713,8 @@ static int __perf_event_overflow(struct perf_event *event,
 
 	ret = __perf_event_account_interrupt(event, throttle);
 
-	if (event->prog && !bpf_overflow_handler(event, data, regs))
+	if (event->prog && event->prog->type == BPF_PROG_TYPE_PERF_EVENT &&
+	    !bpf_overflow_handler(event, data, regs))
 		return ret;
 
 	/*
Signature · 1 line
2.34.1
#11 lore raw
In reply to Luo Gengkun
Sashiko has finished reviewing this 9-patch series:

Sashiko has finished reviewing this 9-patch series:

- Patches 1-3 and 5-9: no issues found.
- Patch 4 ("perf/bpf: Call BPF handler directly, not through ove..."): 1 related pre-existing bug noted

A detailed review has been sent as a separate reply to patch 4.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]
maintainer Applied
#12 lore raw
In reply to Luo Gengkun
Quoted 3 lines

We hit the following problem on x86:

	BUG: kernel NULL pointer dereference, address: 0000000000000000

Queued the series for 6.6, thanks.

Signature · 2 lines
Thanks,
Sasha