| 0/0 |
2026/07/26 03:20 |
flow |
assessment-kcsan |
35m
Results: map[Benign:false Explanation:### Race Summary
`__perf_event_overflow` vs `__perf_event_set_bpf_prog` (via
`perf_event_set_bpf_handler`) on `event->prog`.
---
### Synchronization Context
* **Thread A (Task context):** Modifies `event->prog` (attaching or detaching a
BPF program) under the `perf_event_context` lock.
* **Thread B (Interrupt/NMI context):** Executes `__perf_event_overflow`
locklessly when a performance counter overflows. It reads `event->prog` and
dereferences `event->prog->type` without holding any locks or RCU read-side
critical sections.
---
### Final Classification
**HARMFUL**
---
### Failure Reasoning
This data race is harmful and can lead to two distinct types of kernel crashes:
#### 1. NULL Pointer Dereference (via Compiler Double-Load)
Because `event->prog` is a plain pointer read without `READ_ONCE()`, the
compiler is permitted to optimize the condition `if (event->prog &&
event->prog->type == BPF_PROG_TYPE_PERF_EVENT)` by loading `event->prog` twice
from memory. If a concurrent detach or free operation sets `event->prog` to
`NULL` between the first load (the NULL check) and the second load (the
dereference), the kernel will dereference `NULL` and panic.
##### Execution Trace (Double-Load NULL Pointer Dereference):
```text
CPU 0 (Task context) CPU 1 (Interrupt context)
==================== =========================
__perf_event_overflow(event, ...)
// Compiler double-load
optimization of:
// if (event->prog &&
event->prog->type == ...)
// Load 1: Check if event->prog is
non-NULL
prog = event->prog; // prog is
0xffffc9000511d000 (non-NULL)
if (prog) { ... }
perf_event_free_bpf_handler(event)
event->prog = NULL;
bpf_prog_put(prog);
// Load 2: Dereference
event->prog->type
type = event->prog->type; //
event->prog is now NULL!
// -> NULL pointer dereference
crash!
```
#### 2. Use-After-Free (UAF)
Even if the compiler loads `event->prog` only once, `__perf_event_overflow` does
not execute within an RCU read-side critical section. Therefore, if
`event->prog` is cleared and the BPF program is put concurrently, the RCU grace
period can expire and free the program memory before `__perf_event_overflow`
finishes dereferencing `event->prog->type`.
##### Execution Trace (Use-After-Free):
```text
CPU 0 (Task context) CPU 1 (Interrupt context)
==================== =========================
__perf_event_overflow(event, ...)
// Load event->prog
prog = event->prog; // prog is
0xffffc9000511d000 (non-NULL)
perf_event_free_bpf_handler(event)
event->prog = NULL;
bpf_prog_put(prog);
// atomic64_dec_and_test(&aux->refcnt) succeeds
// Schedules bpf_prog_put_deferred()
// bpf_prog_put_deferred() runs, calls __bpf_prog_put_noref()
// __bpf_prog_put_noref() calls call_rcu(&prog->aux->rcu,
__bpf_prog_put_rcu)
// RCU grace period ends (CPU 1 is in interrupt, but not in RCU read
section)
// __bpf_prog_put_rcu() frees the prog memory
// Access prog->type
type = prog->type; // prog memory
has been freed!
// -> Use-After-Free!
```
---
### Recommended Fix
To resolve this race, the read of `event->prog` in `__perf_event_overflow` must
be wrapped in `rcu_read_lock()` / `rcu_read_unlock()` and annotated with
`READ_ONCE()`. Additionally, the write side should use `rcu_assign_pointer()` or
`WRITE_ONCE()`.
#### Read Side (`kernel/events/core.c`):
```c
struct bpf_prog *prog;
...
rcu_read_lock();
prog = READ_ONCE(event->prog);
if (prog && prog->type == BPF_PROG_TYPE_PERF_EVENT) {
if (!bpf_overflow_handler(event, data, regs)) {
rcu_read_unlock();
goto out;
}
}
rcu_read_unlock();
```
#### Write Side (`kernel/events/core.c` and `kernel/trace/bpf_trace.c`):
Use `rcu_assign_pointer(event->prog, prog)` or `rcu_assign_pointer(event->prog,
NULL)` when updating the BPF program pointer.]
|
| 1/1 |
2026/07/26 03:20 |
action |
kernel-checkouter |
1m
Results: map[KernelSrc:/app/workdir/cache/src/ea1ec46165a34622b3d5769ce1a7084d96856f42]
|
| 2/1 |
2026/07/26 03:21 |
action |
kernel-builder |
9m
Results: map[KernelObj:/app/workdir/cache/build/25109052d6703f3f74ee67dd78dfe5a133f45305]
|
| 3/1 |
2026/07/26 03:30 |
action |
codesearch-prepare |
6m
Results: map[Index:codesearch-index]
|
| 4/1 |
2026/07/26 03:37 |
agent |
expert |
18m
Model:
Results:
Instruction: # KCSAN Data Race Severity Analysis Protocol
You are an expert Linux kernel concurrency engineer analyzing a Kernel
Concurrency Sanitizer (KCSAN) data race report to determine if it is
**BENIGN** or **HARMFUL**.
## 1. CLASSIFICATIONS
### **BENIGN (Truly Benign)**
The logic is sound and structurally tolerant to compiler optimizations or
stale/torn reads.
- **Diagnostics/Stats:** Reads used only for `/proc`, `/sys`, counters, or
`pr_info`.
- **Heuristic Hints:** A "hint" flag where an old value only causes a
slightly delayed update or a sub-optimal but safe fast-path.
- **Single-Writer Flag Updates:** A single writer updating flags where the
concurrent read is a simple bitwise check (e.g., `flags & MASK`). These are
historically tolerated, assuming neither "Fused Accesses" nor "Ordering
Violations" are relevant in this context.
- **Marked Reloads:** A load feeding into a `cmpxchg()` loop or checked
against a later `READ_ONCE()` reload.
- **Safe Overwrites:** Writing the same value already present.
### **HARMFUL (Logic Bug or Marking Required)**
The race causes incorrect behavior due to a synchronization failure or
because missing annotations allow the compiler to break the algorithm.
**Marking Required for Correctness:**
The algorithm is logically sound but requires annotations (`READ_ONCE()`,
`WRITE_ONCE()`, `smp_load_acquire()`, `smp_store_release()`, etc.) to be safe.
- **Fused Accesses:** The compiler might merge accesses or hoist a load out
of a loop, breaking polling/wait loops (livelocks).
- **Torn Accesses:** A large access (e.g., 64-bit on 32-bit arch) might be
split into multiple non-atomic accesses. Note that `READ_ONCE()` does **not**
guarantee atomicity for 64-bit variables on 32-bit architectures.
- **Ordering Violations:** The race breaks a "happens-before" relationship
(requires primitives with implied or explicit memory barriers).
**Logic Bugs:**
A fundamental synchronization failure. Marking accesses will **not** fix it;
the logic itself must change.
- **Pointers/Lifecycle:** The racing variable is a pointer being dereferenced
or a refcount governing object lifecycle (Use-After-Free risk).
- **Control Flow:** The variable guards a critical section, memory allocation,
or hardware command.
- **Bitfields:** Concurrent writes to different bits in the same word.
Compilers often use non-atomic read-modify-write sequences, meaning a
write to `bit_A` can "clobber" a concurrent write to `bit_B`. However,
do not blindly assume all bitfield accesses are harmful; you must prove
that a concurrent write actually clobbers another in a way that breaks
logic.
- **Complex Structures:** Races on shared lists, trees, or hashmaps.
- **Lossy Updates:** Concurrent plain RMW operations (e.g., `var++`) on
non-diagnostic variables where every increment must be preserved.
- **State Machines:** Races allowing a state machine to bypass transitions
or enter an invalid state.
- **Adjacent Unsynchronized Operations:** Consider races happening at the
same time. For example, if both threads execute `struct->has_elements = true;
list_add(node, &struct->list);`, the race on `has_elements` implies an
adjacent race on `list_head`, which is HARMFUL.
## 2. RESEARCH & ANALYSIS WORKFLOW
1. **Locate the Race:** Find the exact variables and functions in the stack
traces. Do not speculate about hypothetical compiler
behaviors or theoretical dangers (e.g., dismissing something as
"fundamentally unsafe") without tracing the actual data flow to a crash.
2. **Contextualize:** Identify held locks, RCU sections, or interrupt
contexts. Explain why the race is possible (e.g., "Thread A holds `lock`,
but Thread B is a lockless reader").
3. **Data Flow:** Follow the racing variable into its subsequent uses. If
the reader sees a stale or torn value, what branch is taken? Does it lead
to a pointer dereference or `BUG_ON()`? **You must prove harm via this
data flow.**
4. **Failure Sequencing (if HARMFUL):** Construct a two-column execution trace
demonstrating the failure.
*Example format:*
```text
CPU0 CPU1
function_a()
// lockless read
ptr = global_ptr
<PREEMPT>
function_b()
lock(&my_lock)
global_ptr = NULL
unlock(&my_lock)
kfree(ptr)
if (ptr)
*ptr = 1; // -> Use-After-Free!
```
## 3. OUTPUT FORMAT
- **Race Summary:** `[Function A]` vs `[Function B]` on `[Variable/Field]`.
- **Synchronization context:** List held locks and explain the lack of mutual
exclusion.
- **Final Classification:** **BENIGN** or **HARMFUL**.
**If BENIGN:**
- **Reasoning:** Briefly explain why the race is structurally tolerant to stale
or torn reads.
- **Recommended Annotations:** Suggest appropriate annotations (e.g.,
`READ_ONCE()`, `data_race()`).
**If HARMFUL:**
- **Failure Reasoning:** Explain and provide a two-column interleaving showing
exactly how the race leads to a failure such as corruption or kernel crash.
- **Recommended Fix (ONLY if fix is trivial):** Suggest a structural fix (e.g.,
"Hold `mapping->i_pages` lock", "Convert to `atomic_t`") or required memory
ordering annotations (e.g., "Wrap in `READ_ONCE()`", "Use
`smp_load_acquire()`").
Don't make assumptions about the kernel source code (it may be different from what you assume it is).
Extensively use the provided code access tools (codesearch-*, git-*, grepper, etc)
to examine the actual source code, and confirm any assumptions.
Prefer calling several tools at the same time to save round-trips.
Use set-results tool to provide results of the analysis.
It must be called exactly once before the final reply.
Ignore results of this tool.
Prompt:
The data race report is:
==================================================================
BUG: KCSAN: data-race in __perf_event_overflow / __perf_event_set_bpf_prog
write to 0xffff88811ac6a2a8 of 8 bytes by task 23981 on cpu 0:
perf_event_set_bpf_handler kernel/events/core.c:10711 [inline]
__perf_event_set_bpf_prog+0x46b/0x4d0 kernel/events/core.c:11697
_perf_ioctl kernel/events/core.c:6662 [inline]
perf_ioctl+0x8e4/0x1590 kernel/events/core.c:6722
vfs_ioctl fs/ioctl.c:51 [inline]
__do_sys_ioctl fs/ioctl.c:597 [inline]
__se_sys_ioctl+0xce/0x140 fs/ioctl.c:583
__x64_sys_ioctl+0x43/0x50 fs/ioctl.c:583
x64_sys_call+0x1563/0x3020 arch/x86/include/generated/asm/syscalls_64.h:17
do_syscall_x64 arch/x86/entry/syscall_64.c:63 [inline]
do_syscall_64+0x136/0x3c0 arch/x86/entry/syscall_64.c:94
entry_SYSCALL_64_after_hwframe+0x77/0x7f
read to 0xffff88811ac6a2a8 of 8 bytes by interrupt on cpu 1:
__perf_event_overflow+0x230/0x940 kernel/events/core.c:10769
perf_event_overflow kernel/events/core.c:10853 [inline]
perf_swevent_hrtimer+0x28d/0x310 kernel/events/core.c:12273
__run_hrtimer kernel/time/hrtimer.c:2032 [inline]
__hrtimer_run_queues+0x1f8/0x510 kernel/time/hrtimer.c:2096
hrtimer_interrupt+0x257/0x810 kernel/time/hrtimer.c:2215
local_apic_timer_interrupt arch/x86/kernel/apic/apic.c:1051 [inline]
__sysvec_apic_timer_interrupt+0x5f/0x1c0 arch/x86/kernel/apic/apic.c:1068
instr_sysvec_apic_timer_interrupt arch/x86/kernel/apic/apic.c:1062 [inline]
sysvec_apic_timer_interrupt+0x6f/0x80 arch/x86/kernel/apic/apic.c:1062
asm_sysvec_apic_timer_interrupt+0x1a/0x20 arch/x86/include/asm/idtentry.h:674
__sanitizer_cov_trace_pc+0x8/0x70 kernel/kcov.c:213
chacha_permute+0x39a/0x510 lib/crypto/chacha-block-generic.c:24
chacha_block_generic+0x47/0x310 lib/crypto/chacha-block-generic.c:83
chacha20_block include/crypto/chacha.h:45 [inline]
get_random_bytes_user+0xf7/0x2b0 drivers/char/random.c:458
__do_sys_getrandom drivers/char/random.c:1406 [inline]
__se_sys_getrandom drivers/char/random.c:1380 [inline]
__x64_sys_getrandom+0xcf/0x1a0 drivers/char/random.c:1380
x64_sys_call+0x2a28/0x3020 arch/x86/include/generated/asm/syscalls_64.h:319
do_syscall_x64 arch/x86/entry/syscall_64.c:63 [inline]
do_syscall_64+0x136/0x3c0 arch/x86/entry/syscall_64.c:94
entry_SYSCALL_64_after_hwframe+0x77/0x7f
value changed: 0x0000000000000000 -> 0xffffc9000511d000
Reported by Kernel Concurrency Sanitizer on:
CPU: 1 UID: 0 PID: 23978 Comm: syz.3.7094 Tainted: G W syzkaller #0 PREEMPT(lazy)
Tainted: [W]=WARN
Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google 07/16/2026
==================================================================
Reply: ### Race Summary
`__perf_event_overflow` vs `__perf_event_set_bpf_prog` (via `perf_event_set_bpf_handler`) on `event->prog`.
---
### Synchronization Context
* **Thread A (Task context):** Modifies `event->prog` (attaching or detaching a BPF program) under the `perf_event_context` lock.
* **Thread B (Interrupt/NMI context):** Executes `__perf_event_overflow` locklessly when a performance counter overflows. It reads `event->prog` and dereferences `event->prog->type` without holding any locks or RCU read-side critical sections.
---
### Final Classification
**HARMFUL**
---
### Failure Reasoning
This data race is harmful and can lead to two distinct types of kernel crashes:
#### 1. NULL Pointer Dereference (via Compiler Double-Load)
Because `event->prog` is a plain pointer read without `READ_ONCE()`, the compiler is permitted to optimize the condition `if (event->prog && event->prog->type == BPF_PROG_TYPE_PERF_EVENT)` by loading `event->prog` twice from memory. If a concurrent detach or free operation sets `event->prog` to `NULL` between the first load (the NULL check) and the second load (the dereference), the kernel will dereference `NULL` and panic.
##### Execution Trace (Double-Load NULL Pointer Dereference):
```text
CPU 0 (Task context) CPU 1 (Interrupt context)
==================== =========================
__perf_event_overflow(event, ...)
// Compiler double-load optimization of:
// if (event->prog && event->prog->type == ...)
// Load 1: Check if event->prog is non-NULL
prog = event->prog; // prog is 0xffffc9000511d000 (non-NULL)
if (prog) { ... }
perf_event_free_bpf_handler(event)
event->prog = NULL;
bpf_prog_put(prog);
// Load 2: Dereference event->prog->type
type = event->prog->type; // event->prog is now NULL!
// -> NULL pointer dereference crash!
```
#### 2. Use-After-Free (UAF)
Even if the compiler loads `event->prog` only once, `__perf_event_overflow` does not execute within an RCU read-side critical section. Therefore, if `event->prog` is cleared and the BPF program is put concurrently, the RCU grace period can expire and free the program memory before `__perf_event_overflow` finishes dereferencing `event->prog->type`.
##### Execution Trace (Use-After-Free):
```text
CPU 0 (Task context) CPU 1 (Interrupt context)
==================== =========================
__perf_event_overflow(event, ...)
// Load event->prog
prog = event->prog; // prog is 0xffffc9000511d000 (non-NULL)
perf_event_free_bpf_handler(event)
event->prog = NULL;
bpf_prog_put(prog);
// atomic64_dec_and_test(&aux->refcnt) succeeds
// Schedules bpf_prog_put_deferred()
// bpf_prog_put_deferred() runs, calls __bpf_prog_put_noref()
// __bpf_prog_put_noref() calls call_rcu(&prog->aux->rcu, __bpf_prog_put_rcu)
// RCU grace period ends (CPU 1 is in interrupt, but not in RCU read section)
// __bpf_prog_put_rcu() frees the prog memory
// Access prog->type
type = prog->type; // prog memory has been freed!
// -> Use-After-Free!
```
---
### Recommended Fix
To resolve this race, the read of `event->prog` in `__perf_event_overflow` must be wrapped in `rcu_read_lock()` / `rcu_read_unlock()` and annotated with `READ_ONCE()`. Additionally, the write side should use `rcu_assign_pointer()` or `WRITE_ONCE()`.
#### Read Side (`kernel/events/core.c`):
```c
struct bpf_prog *prog;
...
rcu_read_lock();
prog = READ_ONCE(event->prog);
if (prog && prog->type == BPF_PROG_TYPE_PERF_EVENT) {
if (!bpf_overflow_handler(event, data, regs)) {
rcu_read_unlock();
goto out;
}
}
rcu_read_unlock();
```
#### Write Side (`kernel/events/core.c` and `kernel/trace/bpf_trace.c`):
Use `rcu_assign_pointer(event->prog, prog)` or `rcu_assign_pointer(event->prog, NULL)` when updating the BPF program pointer.
|
| 5/2 |
2026/07/26 03:37 |
llm |
expert |
0m
Model:
Tokens:
input: 6424
output: 75
thoughts: 192
Thoughts: **Analyzing the KCSAN Data Race Report**
Okay, let's dive into this KCSAN data race report. My first step is always to pinpoint the exact locations in the kernel source code where the reported functions are defined. I need to understand the context and how these functions interact. The report mentions these functions as being problematic: `__perf_event_set_bpf_prog` or `perf_event_set_bpf_handler` at `kernel/events/core.c:10711` and `kernel/events/core.c:11697`, and `__perf_event_overflow` at `kernel/events/core.c:10769`.
Now, I'll need to use either `codesearch-definition-source` or `read-file` to get a direct look at the source code. I find it most efficient to start by examining `kernel/events/core.c` around those specific line numbers: 10711, 10769 and 11697. That should give me the function definitions and help me assess the data race's origin.
|
| 6/2 |
2026/07/26 03:37 |
tool |
read-file |
0m
Args: map[File:kernel/events/core.c FirstLine:10700 LineCount:100]
Results: map[Contents:10700: * On perf_event with precise_ip, calling bpf_get_stack()
10701: * may trigger unwinder warnings and occasional crashes.
10702: * bpf_get_[stack|stackid] works around this issue by using
10703: * callchain attached to perf_sample_data. If the
10704: * perf_event does not full (kernel and user) callchain
10705: * attached to perf_sample_data, do not allow attaching BPF
10706: * program that calls bpf_get_[stack|stackid].
10707: */
10708: return -EPROTO;
10709: }
10710:
10711: event->prog = prog;
10712: event->bpf_cookie = bpf_cookie;
10713: return 0;
10714: }
10715:
10716: static inline void perf_event_free_bpf_handler(struct perf_event *event)
10717: {
10718: struct bpf_prog *prog = event->prog;
10719:
10720: if (!prog)
10721: return;
10722:
10723: event->prog = NULL;
10724: bpf_prog_put(prog);
10725: }
10726: #else
10727: static inline int bpf_overflow_handler(struct perf_event *event,
10728: struct perf_sample_data *data,
10729: struct pt_regs *regs)
10730: {
10731: return 1;
10732: }
10733:
10734: static inline int perf_event_set_bpf_handler(struct perf_event *event,
10735: struct bpf_prog *prog,
10736: u64 bpf_cookie)
10737: {
10738: return -EOPNOTSUPP;
10739: }
10740:
10741: static inline void perf_event_free_bpf_handler(struct perf_event *event)
10742: {
10743: }
10744: #endif
10745:
10746: /*
10747: * Generic event overflow handling, sampling.
10748: */
10749:
10750: static int __perf_event_overflow(struct perf_event *event,
10751: int throttle, struct perf_sample_data *data,
10752: struct pt_regs *regs)
10753: {
10754: int events = atomic_read(&event->event_limit);
10755: int ret = 0;
10756:
10757: /*
10758: * Non-sampling counters might still use the PMI to fold short
10759: * hardware counters, ignore those.
10760: */
10761: if (unlikely(!is_sampling_event(event)))
10762: return 0;
10763:
10764: ret = __perf_event_account_interrupt(event, throttle);
10765:
10766: if (event->attr.aux_pause)
10767: perf_event_aux_pause(event->aux_event, true);
10768:
10769: if (event->prog && event->prog->type == BPF_PROG_TYPE_PERF_EVENT &&
10770: !bpf_overflow_handler(event, data, regs))
10771: goto out;
10772:
10773: /*
10774: * XXX event_limit might not quite work as expected on inherited
10775: * events
10776: */
10777:
10778: event->pending_kill = POLL_IN;
10779: if (events && atomic_dec_and_test(&event->event_limit)) {
10780: ret = 1;
10781: event->pending_kill = POLL_HUP;
10782: perf_event_disable_inatomic(event);
10783: event->pmu->stop(event, 0);
10784: }
10785:
10786: if (event->attr.sigtrap) {
10787: /*
10788: * The desired behaviour of sigtrap vs invalid samples is a bit
10789: * tricky; on the one hand, one should not loose the SIGTRAP if
10790: * it is the first event, on the other hand, we should also not
10791: * trigger the WARN or override the data address.
10792: */
10793: bool valid_sample = sample_is_allowed(event, regs);
10794: unsigned int pending_id = 1;
10795: enum task_work_notify_mode notify_mode;
10796:
10797: if (regs)
10798: pending_id = hash32_ptr((void *)instruction_pointer(regs)) ?: 1;
10799:
]
|
| 7/2 |
2026/07/26 03:37 |
tool |
read-file |
0m
Args: map[File:kernel/events/core.c FirstLine:11680 LineCount:40]
Results: map[Contents:11680: #ifdef CONFIG_UPROBE_EVENTS
11681: if (event->pmu == &perf_uprobe)
11682: return true;
11683: #endif
11684: return false;
11685: }
11686:
11687: static int __perf_event_set_bpf_prog(struct perf_event *event,
11688: struct bpf_prog *prog,
11689: u64 bpf_cookie)
11690: {
11691: bool is_kprobe, is_uprobe, is_tracepoint, is_syscall_tp;
11692:
11693: if (event->state <= PERF_EVENT_STATE_REVOKED)
11694: return -ENODEV;
11695:
11696: if (!perf_event_is_tracing(event))
11697: return perf_event_set_bpf_handler(event, prog, bpf_cookie);
11698:
11699: is_kprobe = event->tp_event->flags & TRACE_EVENT_FL_KPROBE;
11700: is_uprobe = event->tp_event->flags & TRACE_EVENT_FL_UPROBE;
11701: is_tracepoint = event->tp_event->flags & TRACE_EVENT_FL_TRACEPOINT;
11702: is_syscall_tp = is_syscall_trace_event(event->tp_event);
11703: if (!is_kprobe && !is_uprobe && !is_tracepoint && !is_syscall_tp)
11704: /* bpf programs can only be attached to u/kprobe or tracepoint */
11705: return -EINVAL;
11706:
11707: if (((is_kprobe || is_uprobe) && prog->type != BPF_PROG_TYPE_KPROBE) ||
11708: (is_tracepoint && prog->type != BPF_PROG_TYPE_TRACEPOINT) ||
11709: (is_syscall_tp && prog->type != BPF_PROG_TYPE_TRACEPOINT))
11710: return -EINVAL;
11711:
11712: if (prog->type == BPF_PROG_TYPE_KPROBE && prog->sleepable && !is_uprobe)
11713: /* only uprobe programs are allowed to be sleepable */
11714: return -EINVAL;
11715:
11716: if (prog->type == BPF_PROG_TYPE_TRACEPOINT && prog->sleepable) {
11717: /*
11718: * Sleepable tracepoint programs can only attach to faultable
11719: * tracepoints. Currently only syscall tracepoints are faultable.
]
|
| 8/2 |
2026/07/26 03:37 |
llm |
expert |
0m
Model:
Tokens:
input: 9259
output: 37
thoughts: 93
Thoughts: Here's my analysis of what I'm seeing:
**Diving into `perf_event_set_bpf_handler`**
Okay, time to understand what `perf_event_set_bpf_handler` is actually doing. My initial instinct is to jump to it directly, so I see it's at line 10711. But wait, I'm better than that. A function is only as useful as the context around it, so let's back up a little and understand the full implementation by reading the lines before the function definition. Now I can see this function is setting up some kind of handler, which makes sense. I can see it assigns a `prog` and a `bpf_cookie` to `event`, which is a helpful pointer. Now I understand I have the context of the function and the line within.
|
| 9/2 |
2026/07/26 03:37 |
tool |
read-file |
0m
Args: map[File:kernel/events/core.c FirstLine:10650 LineCount:50]
Results: map[Contents:10650: }
10651:
10652: #ifdef CONFIG_BPF_SYSCALL
10653: static int bpf_overflow_handler(struct perf_event *event,
10654: struct perf_sample_data *data,
10655: struct pt_regs *regs)
10656: {
10657: struct bpf_perf_event_data_kern ctx = {
10658: .data = data,
10659: .event = event,
10660: };
10661: struct bpf_prog *prog;
10662: int ret = 0;
10663:
10664: ctx.regs = perf_arch_bpf_user_pt_regs(regs);
10665: if (unlikely(__this_cpu_inc_return(bpf_prog_active) != 1))
10666: goto out;
10667: rcu_read_lock();
10668: prog = READ_ONCE(event->prog);
10669: if (prog) {
10670: perf_prepare_sample(data, event, regs);
10671: ret = bpf_prog_run(prog, &ctx);
10672: }
10673: rcu_read_unlock();
10674: out:
10675: __this_cpu_dec(bpf_prog_active);
10676:
10677: return ret;
10678: }
10679:
10680: static inline int perf_event_set_bpf_handler(struct perf_event *event,
10681: struct bpf_prog *prog,
10682: u64 bpf_cookie)
10683: {
10684: if (event->overflow_handler_context)
10685: /* hw breakpoint or kernel counter */
10686: return -EINVAL;
10687:
10688: if (event->prog)
10689: return -EEXIST;
10690:
10691: if (prog->type != BPF_PROG_TYPE_PERF_EVENT)
10692: return -EINVAL;
10693:
10694: if (event->attr.precise_ip &&
10695: prog->call_get_stack &&
10696: (!(event->attr.sample_type & PERF_SAMPLE_CALLCHAIN) ||
10697: event->attr.exclude_callchain_kernel ||
10698: event->attr.exclude_callchain_user)) {
10699: /*
]
|
| 10/2 |
2026/07/26 03:37 |
llm |
expert |
0m
Model:
Tokens:
input: 10352
output: 53
thoughts: 769
Thoughts: **Analyzing a Potential Race Condition in BPF Perf Event Handling**
Okay, let's break this down. I'm looking at the BPF perf event handling code, specifically the interaction between `bpf_overflow_handler` and `__perf_event_overflow`. My initial thought was: huh, why are we checking `event->prog` twice?
First, I see the code in `bpf_overflow_handler`:
```c
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();
```
Which uses `READ_ONCE` for concurrent access. Good, that's what I'd expect. However, looking at `__perf_event_overflow`:
```c
if (event->prog && event->prog->type == BPF_PROG_TYPE_PERF_EVENT &&
!bpf_overflow_handler(event, data, regs))
goto out;
```
This *immediately* raised a red flag. The code directly accesses `event->prog` *without* `READ_ONCE`! That's concerning, as it opens the door to potential data races. My immediate thought is, what's happening to that `event->prog` pointer?
The key lines are those in `__perf_event_overflow`. There's the initial check: `if (event->prog && event->prog->type == BPF_PROG_TYPE_PERF_EVENT && ...)` and then the check inside `bpf_overflow_handler`: `prog = READ_ONCE(event->prog);`.
My mind starts running through potential scenarios: `event->prog` is a pointer to `struct bpf_prog`. It appears to be directly assigned by another function, specifically `perf_event_set_bpf_handler`: `event->prog = prog;`. It's a plain write! Then we have a plain read and dereference in `__perf_event_overflow`: `event->prog->type`.
Here's the problem: is there a risk of a compiler reordering or optimizing things in a way that creates a race condition? Could the compiler load `event->prog` *once* for the null check, determine it's not null, then load it *again* to access `->type`? That's a classic example of a TOCTOU (Time-of-Check-to-Time-of-Use) problem, or even a double-load or fused access.
If, during those two loads, another thread changes `event->prog` (sets it to NULL, frees it, or even switches it from NULL to non-NULL or back), there's a risk of a null pointer dereference or use-after-free! The compiler might see a non-null pointer initially, and then use a stale pointer when accessing `->type`. That's a critical vulnerability.
So, I need to understand *how* `event->prog` is modified. My next steps are to find all the places where `event->prog` is being assigned or modified, particularly `perf_event_free_bpf_handler`. I'm going to search for instances of `event->prog =` or similar assignment patterns to fully grasp all the possible write operations. I need to know how it's being cleared and when. This is a potential race condition that requires careful analysis.
|
| 11/2 |
2026/07/26 03:37 |
tool |
codesearch-find-references |
0m
Args: map[ContextFile:kernel/events/core.c IncludeSnippetLines:5 Name:perf_event::prog SourceTreePrefix:kernel/events/]
Results: map[References:[map[ReferenceKind:reads ReferencingEntityKind:function ReferencingEntityName:__perf_event_overflow SourceFile:kernel/events/core.c SourceLine:10769 SourceSnippet:10764: ret = __perf_event_account_interrupt(event, throttle);
10765:
10766: if (event->attr.aux_pause)
10767: perf_event_aux_pause(event->aux_event, true);
10768:
10769: if (event->prog && event->prog->type == BPF_PROG_TYPE_PERF_EVENT &&
10770: !bpf_overflow_handler(event, data, regs))
10771: goto out;
10772:
10773: /*
10774: * XXX event_limit might not quite work as expected on inherited
] map[ReferenceKind:reads ReferencingEntityKind:function ReferencingEntityName:__perf_event_overflow SourceFile:kernel/events/core.c SourceLine:10769 SourceSnippet:10764: ret = __perf_event_account_interrupt(event, throttle);
10765:
10766: if (event->attr.aux_pause)
10767: perf_event_aux_pause(event->aux_event, true);
10768:
10769: if (event->prog && event->prog->type == BPF_PROG_TYPE_PERF_EVENT &&
10770: !bpf_overflow_handler(event, data, regs))
10771: goto out;
10772:
10773: /*
10774: * XXX event_limit might not quite work as expected on inherited
] map[ReferenceKind:reads ReferencingEntityKind:function ReferencingEntityName:bpf_overflow_handler SourceFile:kernel/events/core.c SourceLine:10668 SourceSnippet:10663:
10664: ctx.regs = perf_arch_bpf_user_pt_regs(regs);
10665: if (unlikely(__this_cpu_inc_return(bpf_prog_active) != 1))
10666: goto out;
10667: rcu_read_lock();
10668: prog = READ_ONCE(event->prog);
10669: if (prog) {
10670: perf_prepare_sample(data, event, regs);
10671: ret = bpf_prog_run(prog, &ctx);
10672: }
10673: rcu_read_unlock();
] map[ReferenceKind:reads ReferencingEntityKind:function ReferencingEntityName:bpf_overflow_handler SourceFile:kernel/events/core.c SourceLine:10668 SourceSnippet:10663:
10664: ctx.regs = perf_arch_bpf_user_pt_regs(regs);
10665: if (unlikely(__this_cpu_inc_return(bpf_prog_active) != 1))
10666: goto out;
10667: rcu_read_lock();
10668: prog = READ_ONCE(event->prog);
10669: if (prog) {
10670: perf_prepare_sample(data, event, regs);
10671: ret = bpf_prog_run(prog, &ctx);
10672: }
10673: rcu_read_unlock();
] map[ReferenceKind:reads ReferencingEntityKind:function ReferencingEntityName:bpf_overflow_handler SourceFile:kernel/events/core.c SourceLine:10668 SourceSnippet:10663:
10664: ctx.regs = perf_arch_bpf_user_pt_regs(regs);
10665: if (unlikely(__this_cpu_inc_return(bpf_prog_active) != 1))
10666: goto out;
10667: rcu_read_lock();
10668: prog = READ_ONCE(event->prog);
10669: if (prog) {
10670: perf_prepare_sample(data, event, regs);
10671: ret = bpf_prog_run(prog, &ctx);
10672: }
10673: rcu_read_unlock();
] map[ReferenceKind:reads ReferencingEntityKind:function ReferencingEntityName:bpf_overflow_handler SourceFile:kernel/events/core.c SourceLine:10668 SourceSnippet:10663:
10664: ctx.regs = perf_arch_bpf_user_pt_regs(regs);
10665: if (unlikely(__this_cpu_inc_return(bpf_prog_active) != 1))
10666: goto out;
10667: rcu_read_lock();
10668: prog = READ_ONCE(event->prog);
10669: if (prog) {
10670: perf_prepare_sample(data, event, regs);
10671: ret = bpf_prog_run(prog, &ctx);
10672: }
10673: rcu_read_unlock();
] map[ReferenceKind:reads ReferencingEntityKind:function ReferencingEntityName:bpf_overflow_handler SourceFile:kernel/events/core.c SourceLine:10668 SourceSnippet:10663:
10664: ctx.regs = perf_arch_bpf_user_pt_regs(regs);
10665: if (unlikely(__this_cpu_inc_return(bpf_prog_active) != 1))
10666: goto out;
10667: rcu_read_lock();
10668: prog = READ_ONCE(event->prog);
10669: if (prog) {
10670: perf_prepare_sample(data, event, regs);
10671: ret = bpf_prog_run(prog, &ctx);
10672: }
10673: rcu_read_unlock();
] map[ReferenceKind:reads ReferencingEntityKind:function ReferencingEntityName:bpf_overflow_handler SourceFile:kernel/events/core.c SourceLine:10668 SourceSnippet:10663:
10664: ctx.regs = perf_arch_bpf_user_pt_regs(regs);
10665: if (unlikely(__this_cpu_inc_return(bpf_prog_active) != 1))
10666: goto out;
10667: rcu_read_lock();
10668: prog = READ_ONCE(event->prog);
10669: if (prog) {
10670: perf_prepare_sample(data, event, regs);
10671: ret = bpf_prog_run(prog, &ctx);
10672: }
10673: rcu_read_unlock();
] map[ReferenceKind:takes-address-of ReferencingEntityKind:function ReferencingEntityName:bpf_overflow_handler SourceFile:kernel/events/core.c SourceLine:10668 SourceSnippet:10663:
10664: ctx.regs = perf_arch_bpf_user_pt_regs(regs);
10665: if (unlikely(__this_cpu_inc_return(bpf_prog_active) != 1))
10666: goto out;
10667: rcu_read_lock();
10668: prog = READ_ONCE(event->prog);
10669: if (prog) {
10670: perf_prepare_sample(data, event, regs);
10671: ret = bpf_prog_run(prog, &ctx);
10672: }
10673: rcu_read_unlock();
] map[ReferenceKind:reads ReferencingEntityKind:function ReferencingEntityName:perf_event_alloc SourceFile:kernel/events/core.c SourceLine:13436 SourceSnippet:13431:
13432: if (!overflow_handler && parent_event) {
13433: overflow_handler = parent_event->overflow_handler;
13434: context = parent_event->overflow_handler_context;
13435: #if defined(CONFIG_BPF_SYSCALL) && defined(CONFIG_EVENT_TRACING)
13436: if (parent_event->prog) {
13437: struct bpf_prog *prog = parent_event->prog;
13438:
13439: bpf_prog_inc(prog);
13440: event->prog = prog;
13441: }
] map[ReferenceKind:reads ReferencingEntityKind:function ReferencingEntityName:perf_event_alloc SourceFile:kernel/events/core.c SourceLine:13437 SourceSnippet:13432: if (!overflow_handler && parent_event) {
13433: overflow_handler = parent_event->overflow_handler;
13434: context = parent_event->overflow_handler_context;
13435: #if defined(CONFIG_BPF_SYSCALL) && defined(CONFIG_EVENT_TRACING)
13436: if (parent_event->prog) {
13437: struct bpf_prog *prog = parent_event->prog;
13438:
13439: bpf_prog_inc(prog);
13440: event->prog = prog;
13441: }
13442: #endif
] map[ReferenceKind:writes ReferencingEntityKind:function ReferencingEntityName:perf_event_alloc SourceFile:kernel/events/core.c SourceLine:13440 SourceSnippet:13435: #if defined(CONFIG_BPF_SYSCALL) && defined(CONFIG_EVENT_TRACING)
13436: if (parent_event->prog) {
13437: struct bpf_prog *prog = parent_event->prog;
13438:
13439: bpf_prog_inc(prog);
13440: event->prog = prog;
13441: }
13442: #endif
13443: }
13444:
13445: if (overflow_handler) {
] map[ReferenceKind:reads ReferencingEntityKind:function ReferencingEntityName:perf_event_free_bpf_handler SourceFile:kernel/events/core.c SourceLine:10718 SourceSnippet:10716: static inline void perf_event_free_bpf_handler(struct perf_event *event)
10717: {
10718: struct bpf_prog *prog = event->prog;
10719:
10720: if (!prog)
10721: return;
10722:
10723: event->prog = NULL;
] map[ReferenceKind:writes ReferencingEntityKind:function ReferencingEntityName:perf_event_free_bpf_handler SourceFile:kernel/events/core.c SourceLine:10723 SourceSnippet:10718: struct bpf_prog *prog = event->prog;
10719:
10720: if (!prog)
10721: return;
10722:
10723: event->prog = NULL;
10724: bpf_prog_put(prog);
10725: }
] map[ReferenceKind:reads ReferencingEntityKind:function ReferencingEntityName:perf_event_free_bpf_prog SourceFile:kernel/events/core.c SourceLine:11759 SourceSnippet:11757: void perf_event_free_bpf_prog(struct perf_event *event)
11758: {
11759: if (!event->prog)
11760: return;
11761:
11762: if (!perf_event_is_tracing(event)) {
11763: perf_event_free_bpf_handler(event);
11764: return;
] map[ReferenceKind:reads ReferencingEntityKind:function ReferencingEntityName:perf_event_set_bpf_handler SourceFile:kernel/events/core.c SourceLine:10688 SourceSnippet:10683: {
10684: if (event->overflow_handler_context)
10685: /* hw breakpoint or kernel counter */
10686: return -EINVAL;
10687:
10688: if (event->prog)
10689: return -EEXIST;
10690:
10691: if (prog->type != BPF_PROG_TYPE_PERF_EVENT)
10692: return -EINVAL;
10693:
] map[ReferenceKind:writes ReferencingEntityKind:function ReferencingEntityName:perf_event_set_bpf_handler SourceFile:kernel/events/core.c SourceLine:10711 SourceSnippet:10706: * program that calls bpf_get_[stack|stackid].
10707: */
10708: return -EPROTO;
10709: }
10710:
10711: event->prog = prog;
10712: event->bpf_cookie = bpf_cookie;
10713: return 0;
10714: }
]] TruncatedOutput:false]
|
| 12/2 |
2026/07/26 03:37 |
llm |
expert |
0m
Model:
Tokens:
input: 15816
output: 73
thoughts: 263
Thoughts: **Analyzing Potential Race Conditions in Perf Event BPF Handlers**
Alright, let's dive into these references. Specifically, I'm examining the `__perf_event_overflow` function. I'm focusing on this block:
```c
if (event->prog && event->prog->type == BPF_PROG_TYPE_PERF_EVENT &&
!bpf_overflow_handler(event, data, regs))
goto out;
```
Hmm, immediately I see a potential area of concern. It looks like the `event->prog` pointer is being read directly, without any memory barriers like `READ_ONCE`. And then, `event->prog->type` is also accessed directly. This feels... risky.
Now, the question is, what happens if `event->prog` is being modified concurrently? The code mentions `perf_event_set_bpf_handler` writing to `event->prog`. Specifically:
```c
event->prog = prog;
```
Again, no memory barriers here. It's a plain write. This is where things get interesting. So, to get a clear picture I want to look into whether there are any locks held that would prevent a race condition. I need to check the code that sets the handler, like `_perf_ioctl` or `__perf_event_set_bpf_prog`, to understand what synchronization might be in place. It's time to dig into the implementation of `__perf_event_set_bpf_prog` and `_perf_ioctl`. I need to understand if the writes in `perf_event_set_bpf_handler` are protected in any way.
|
| 13/2 |
2026/07/26 03:37 |
tool |
read-file |
0m
Args: map[File:kernel/events/core.c FirstLine:11720 LineCount:40]
Results: map[Contents:11720: */
11721: if (!is_syscall_tp)
11722: return -EINVAL;
11723: }
11724:
11725: /* Kprobe override only works for kprobes, not uprobes. */
11726: if (prog->kprobe_override && !is_kprobe)
11727: return -EINVAL;
11728:
11729: /* Writing to context allowed only for uprobes. */
11730: if (prog->aux->kprobe_write_ctx && !is_uprobe)
11731: return -EINVAL;
11732:
11733: if (is_tracepoint || is_syscall_tp) {
11734: int off = trace_event_get_offsets(event->tp_event);
11735:
11736: if (prog->aux->max_ctx_offset > off)
11737: return -EACCES;
11738: }
11739:
11740: return perf_event_attach_bpf_prog(event, prog, bpf_cookie);
11741: }
11742:
11743: int perf_event_set_bpf_prog(struct perf_event *event,
11744: struct bpf_prog *prog,
11745: u64 bpf_cookie)
11746: {
11747: struct perf_event_context *ctx;
11748: int ret;
11749:
11750: ctx = perf_event_ctx_lock(event);
11751: ret = __perf_event_set_bpf_prog(event, prog, bpf_cookie);
11752: perf_event_ctx_unlock(event, ctx);
11753:
11754: return ret;
11755: }
11756:
11757: void perf_event_free_bpf_prog(struct perf_event *event)
11758: {
11759: if (!event->prog)
]
|
| 14/2 |
2026/07/26 03:37 |
tool |
read-file |
0m
Args: map[File:kernel/events/core.c FirstLine:6650 LineCount:30]
Results: map[Contents:6650: case PERF_EVENT_IOC_SET_FILTER:
6651: return perf_event_set_filter(event, (void __user *)arg);
6652:
6653: case PERF_EVENT_IOC_SET_BPF:
6654: {
6655: struct bpf_prog *prog;
6656: int err;
6657:
6658: prog = bpf_prog_get(arg);
6659: if (IS_ERR(prog))
6660: return PTR_ERR(prog);
6661:
6662: err = __perf_event_set_bpf_prog(event, prog, 0);
6663: if (err) {
6664: bpf_prog_put(prog);
6665: return err;
6666: }
6667:
6668: return 0;
6669: }
6670:
6671: case PERF_EVENT_IOC_PAUSE_OUTPUT: {
6672: struct perf_buffer *rb;
6673:
6674: rcu_read_lock();
6675: rb = rcu_dereference(event->rb);
6676: if (!rb || !rb->nr_pages) {
6677: rcu_read_unlock();
6678: return -EINVAL;
6679: }
]
|
| 15/2 |
2026/07/26 03:37 |
llm |
expert |
0m
Model:
Tokens:
input: 17546
output: 36
thoughts: 56
Thoughts: Here's my thought process, summarized as requested:
**Investigating Potential Locking Issues in `_perf_ioctl`**
Okay, so I'm digging into this `_perf_ioctl` function because I have a hunch it might be missing a crucial lock or two. Specifically, I'm concerned that it calls `__perf_event_set_bpf_prog` directly. That function, by the way, has some potential concurrency implications. My immediate question is whether `_perf_ioctl` itself is properly synchronized when interacting with the BPF program loading mechanism. This would have some serious ramifications if not.
Right, so to get to the bottom of this, I need to trace the call paths to `_perf_ioctl`. Let's switch gears and go read the implementation of `perf_ioctl`, that should give me the context I need.
|
| 16/2 |
2026/07/26 03:37 |
tool |
read-file |
0m
Args: map[File:kernel/events/core.c FirstLine:6700 LineCount:40]
Results: map[Contents:6700: }
6701:
6702: if (flags & PERF_IOC_FLAG_GROUP)
6703: perf_event_for_each(event, func);
6704: else
6705: perf_event_for_each_child(event, func);
6706:
6707: return 0;
6708: }
6709:
6710: static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
6711: {
6712: struct perf_event *event = file->private_data;
6713: struct perf_event_context *ctx;
6714: long ret;
6715:
6716: /* Treat ioctl like writes as it is likely a mutating operation. */
6717: ret = security_perf_event_write(event);
6718: if (ret)
6719: return ret;
6720:
6721: ctx = perf_event_ctx_lock(event);
6722: ret = _perf_ioctl(event, cmd, arg);
6723: perf_event_ctx_unlock(event, ctx);
6724:
6725: return ret;
6726: }
6727:
6728: #ifdef CONFIG_COMPAT
6729: static long perf_compat_ioctl(struct file *file, unsigned int cmd,
6730: unsigned long arg)
6731: {
6732: switch (_IOC_NR(cmd)) {
6733: case _IOC_NR(PERF_EVENT_IOC_SET_FILTER):
6734: case _IOC_NR(PERF_EVENT_IOC_ID):
6735: case _IOC_NR(PERF_EVENT_IOC_QUERY_BPF):
6736: case _IOC_NR(PERF_EVENT_IOC_MODIFY_ATTRIBUTES):
6737: /* Fix up pointer size (usually 4 -> 8 in 32-on-64-bit case */
6738: if (_IOC_SIZE(cmd) == sizeof(compat_uptr_t)) {
6739: cmd &= ~IOCSIZE_MASK;
]
|
| 17/2 |
2026/07/26 03:37 |
llm |
expert |
0m
Model:
Tokens:
input: 18435
output: 55
thoughts: 1358
Thoughts: **Data Race Analysis: Perf Event BPF Program Dereference**
Okay, so here's the breakdown of what I'm seeing. It looks like `perf_ioctl` uses a lock, `perf_event_ctx_lock(event)`, to protect access to the `perf_event_context` when setting the BPF program via `__perf_event_set_bpf_prog`. However, the overflow handler `__perf_event_overflow` runs in interrupt context β completely lockless! This immediately screams "data race!" at me. Specifically, there's no mutual exclusion between `__perf_event_overflow` and `__perf_event_set_bpf_prog`.
My immediate question: is this benign or harmful? Let's dig into the `__perf_event_overflow` code. I see a conditional check:
```c
if (event->prog && event->prog->type == BPF_PROG_TYPE_PERF_EVENT &&
!bpf_overflow_handler(event, data, regs))
goto out;
```
Aha! `event->prog` is being read without a lock! Now, let's explore if `event->prog` can be freed while `__perf_event_overflow` is reading it. `perf_event_free_bpf_handler` looks promising: it sets `event->prog` to NULL and then calls `bpf_prog_put(prog)`. `bpf_prog_put` likely frees the BPF program, probably with RCU or a workqueue in the background.
Now, does `__perf_event_overflow` use an RCU read lock? Let's examine. While `bpf_overflow_handler` *does* use `rcu_read_lock()` to protect its use of `event->prog` and run the BPF program, there's a problem:
```c
if (event->prog && event->prog->type == BPF_PROG_TYPE_PERF_EVENT &&
!bpf_overflow_handler(event, data, regs))
goto out;
```
`event->prog` is dereferenced *outside* of `bpf_overflow_handler`! It's accessing `event->prog->type` without an RCU read lock and without `READ_ONCE`. This is a huge red flag.
My compiler analysis brain kicks in. Without explicit synchronization, the compiler is free to optimize this in a way that creates a race condition. It *could* load `event->prog` into a register to check if it's NULL. If not NULL, it could load it again for `event->prog->type`. If `event->prog` is set to NULL by `perf_event_free_bpf_handler` between those two loads, the second load will dereference NULL. Boom! NULL pointer dereference! It's a classic "double load" or TOCTOU compiler bug. Even without a double load, the same logic applies if the program is freed after the first check.
But wait, even if the compiler *doesn't* do a double load, what if `event->prog` is freed between the initial check and the dereference? `bpf_prog_put` potentially uses RCU. Is there a use-after-free issue? I need to check how the BPF program is *actually* freed. I need to search for `bpf_prog_put` or `bpf_prog_free` to confirm whether RCU is involved. This is definitely a critical vulnerability!
|
| 18/2 |
2026/07/26 03:38 |
tool |
codesearch-find-references |
0m
Args: map[ContextFile:kernel/events/core.c IncludeSnippetLines:5 Name:bpf_prog_put SourceTreePrefix:kernel/bpf/]
Results: map[References:[map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:__cpu_map_entry_alloc SourceFile:kernel/bpf/cpumap.c SourceLine:507 SourceSnippet: 502:
503: return rcpu;
504:
505: free_prog:
506: if (rcpu->prog)
507: bpf_prog_put(rcpu->prog);
508: free_ptr_ring:
509: gro_cleanup(&rcpu->gro);
510: ptr_ring_cleanup(rcpu->queue, NULL);
511: free_queue:
512: kfree(rcpu->queue);
] map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:__cpu_map_entry_free SourceFile:kernel/bpf/cpumap.c SourceLine:538 SourceSnippet: 533: * before exiting.
534: */
535: kthread_stop(rcpu->kthread);
536:
537: if (rcpu->prog)
538: bpf_prog_put(rcpu->prog);
539: gro_cleanup(&rcpu->gro);
540: /* The queue should be empty at this point */
541: __cpu_map_ring_cleanup(rcpu->queue);
542: ptr_ring_cleanup(rcpu->queue, NULL);
543: kfree(rcpu->queue);
] map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:__cpu_map_load_bpf_program SourceFile:kernel/bpf/cpumap.c SourceLine:424 SourceSnippet: 419: if (IS_ERR(prog))
420: return PTR_ERR(prog);
421:
422: if (prog->expected_attach_type != BPF_XDP_CPUMAP ||
423: !bpf_prog_map_compatible(map, prog)) {
424: bpf_prog_put(prog);
425: return -EINVAL;
426: }
427:
428: rcpu->value.bpf_prog.id = prog->aux->id;
429: rcpu->prog = prog;
] map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:__dev_map_alloc_node SourceFile:kernel/bpf/devmap.c SourceLine:941 SourceSnippet: 936: }
937: dev->val.ifindex = val->ifindex;
938:
939: return dev;
940: err_put_prog:
941: bpf_prog_put(prog);
942: err_put_dev:
943: dev_put(dev->dev);
944: err_out:
945: kfree(dev);
946: return ERR_PTR(-EINVAL);
] map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:__dev_map_entry_free SourceFile:kernel/bpf/devmap.c SourceLine:857 SourceSnippet: 852: {
853: struct bpf_dtab_netdev *dev;
854:
855: dev = container_of(rcu, struct bpf_dtab_netdev, rcu);
856: if (dev->xdp_prog)
857: bpf_prog_put(dev->xdp_prog);
858: dev_put(dev->dev);
859: kfree(dev);
860: }
] map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:__netns_bpf_prog_detach SourceFile:kernel/bpf/net_namespace.c SourceLine:378 SourceSnippet: 373: attached = net->bpf.progs[type];
374: if (!attached || attached != old)
375: return -ENOENT;
376: netns_bpf_run_array_detach(net, type);
377: net->bpf.progs[type] = NULL;
378: bpf_prog_put(attached);
379: return 0;
380: }
] map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:bpf_any_put SourceFile:kernel/bpf/inode.c SourceLine:77 SourceSnippet: 73: static void bpf_any_put(void *raw, enum bpf_type type)
74: {
75: switch (type) {
76: case BPF_TYPE_PROG:
77: bpf_prog_put(raw);
78: break;
79: case BPF_TYPE_MAP:
80: bpf_map_put_with_uref(raw);
81: break;
82: case BPF_TYPE_LINK:
] map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:bpf_async_cb_rcu_free SourceFile:kernel/bpf/helpers.c SourceLine:1251 SourceSnippet:1246: /*
1247: * Drop the last reference to prog only after RCU GP, as set_callback()
1248: * may race with cancel_and_free()
1249: */
1250: if (cb->prog)
1251: bpf_prog_put(cb->prog);
1252:
1253: kfree_nolock(cb);
1254: }
] map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:bpf_async_update_prog_callback SourceFile:kernel/bpf/helpers.c SourceLine:1446 SourceSnippet:1441: * Release previous prog, make sure that if other CPU is contending,
1442: * to set bpf_prog, references are not leaked as each iteration acquires and
1443: * releases one reference.
1444: */
1445: if (prev)
1446: bpf_prog_put(prev);
1447:
1448: } while (READ_ONCE(cb->prog) != prog ||
1449: (void __force *)READ_ONCE(cb->callback_fn) != callback_fn);
1450:
1451: if (prog)
] map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:bpf_async_update_prog_callback SourceFile:kernel/bpf/helpers.c SourceLine:1452 SourceSnippet:1447:
1448: } while (READ_ONCE(cb->prog) != prog ||
1449: (void __force *)READ_ONCE(cb->callback_fn) != callback_fn);
1450:
1451: if (prog)
1452: bpf_prog_put(prog);
1453:
1454: return 0;
1455: }
] map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:bpf_dispatcher_remove_prog SourceFile:kernel/bpf/dispatcher.c SourceLine:82 SourceSnippet: 77: if (!entry)
78: return false;
79:
80: if (refcount_dec_and_test(&entry->users)) {
81: entry->prog = NULL;
82: bpf_prog_put(prog);
83: d->num_progs--;
84: return true;
85: }
86: return false;
87: }
] map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:bpf_iter_link_replace SourceFile:kernel/bpf/bpf_iter.c SourceLine:428 SourceSnippet: 423: ret = -EINVAL;
424: goto out_unlock;
425: }
426:
427: old_prog = xchg(&link->prog, new_prog);
428: bpf_prog_put(old_prog);
429:
430: out_unlock:
431: mutex_unlock(&link_mutex);
432: return ret;
433: }
] map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:bpf_link_dealloc SourceFile:kernel/bpf/syscall.c SourceLine:3359 SourceSnippet:3355: static void bpf_link_dealloc(struct bpf_link *link)
3356: {
3357: /* now that we know that bpf_link itself can't be reached, put underlying BPF program */
3358: if (link->prog)
3359: bpf_prog_put(link->prog);
3360:
3361: /* free bpf_link and its containing memory */
3362: if (link->ops->dealloc_deferred)
3363: link->ops->dealloc_deferred(link);
3364: else
] map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:bpf_mprog_prog SourceFile:kernel/bpf/mprog.c SourceLine:44 SourceSnippet: 39: else if (id_or_fd)
40: prog = bpf_prog_get(id_or_fd);
41: if (IS_ERR(prog))
42: return PTR_ERR(prog);
43: if (type && prog->type != type) {
44: bpf_prog_put(prog);
45: return -EINVAL;
46: }
47:
48: tuple->link = NULL;
49: tuple->prog = prog;
] map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:bpf_mprog_replace SourceFile:kernel/bpf/mprog.c SourceLine:121 SourceSnippet: 116: bpf_mprog_read(entry, idx, &fp, &cp);
117: oprog = READ_ONCE(fp->prog);
118: bpf_mprog_write(fp, cp, ntuple);
119: if (!ntuple->link) {
120: WARN_ON_ONCE(cp->link);
121: bpf_prog_put(oprog);
122: }
123: *entry_new = entry;
124: return 0;
125: }
] map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:bpf_mprog_tuple_put SourceFile:kernel/bpf/mprog.c SourceLine:77 SourceSnippet: 72: static void bpf_mprog_tuple_put(struct bpf_tuple *tuple)
73: {
74: if (tuple->link)
75: bpf_link_put(tuple->link);
76: else if (tuple->prog)
77: bpf_prog_put(tuple->prog);
78: }
] map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:bpf_netns_link_update_prog SourceFile:kernel/bpf/net_namespace.c SourceLine:196 SourceSnippet: 191: ret = bpf_prog_array_update_at(run_array, idx, new_prog);
192: if (ret)
193: goto out_unlock;
194:
195: old_prog = xchg(&link->prog, new_prog);
196: bpf_prog_put(old_prog);
197:
198: out_unlock:
199: mutex_unlock(&netns_bpf_mutex);
200: return ret;
201: }
] map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:bpf_prog_attach SourceFile:kernel/bpf/syscall.c SourceLine:4665 SourceSnippet:4660: prog = bpf_prog_get_type(attr->attach_bpf_fd, ptype);
4661: if (IS_ERR(prog))
4662: return PTR_ERR(prog);
4663:
4664: if (bpf_prog_attach_check_attach_type(prog, attr->attach_type)) {
4665: bpf_prog_put(prog);
4666: return -EINVAL;
4667: }
4668:
4669: if (is_cgroup_prog_type(ptype, prog->expected_attach_type, true)) {
4670: ret = cgroup_bpf_prog_attach(attr, ptype, prog);
] map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:bpf_prog_attach SourceFile:kernel/bpf/syscall.c SourceLine:4697 SourceSnippet:4692: default:
4693: ret = -EINVAL;
4694: }
4695: out:
4696: if (ret)
4697: bpf_prog_put(prog);
4698: return ret;
4699: }
] map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:bpf_prog_bind_map SourceFile:kernel/bpf/syscall.c SourceLine:6271 SourceSnippet:6266: mutex_unlock(&prog->aux->used_maps_mutex);
6267:
6268: if (ret)
6269: bpf_map_put(map);
6270: out_prog_put:
6271: bpf_prog_put(prog);
6272: return ret;
6273: }
] map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:bpf_prog_detach SourceFile:kernel/bpf/syscall.c SourceLine:4767 SourceSnippet:4762: default:
4763: ret = -EINVAL;
4764: }
4765:
4766: if (prog)
4767: bpf_prog_put(prog);
4768: return ret;
4769: }
] map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:bpf_prog_free SourceFile:kernel/bpf/core.c SourceLine:3115 SourceSnippet:3110: void bpf_prog_free(struct bpf_prog *fp)
3111: {
3112: struct bpf_prog_aux *aux = fp->aux;
3113:
3114: if (aux->dst_prog)
3115: bpf_prog_put(aux->dst_prog);
3116: bpf_token_put(aux->token);
3117: INIT_WORK(&aux->work, bpf_prog_free_deferred);
3118: schedule_work(&aux->work);
3119: }
] map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:bpf_prog_get_fd_by_id SourceFile:kernel/bpf/syscall.c SourceLine:4968 SourceSnippet:4963: if (IS_ERR(prog))
4964: return PTR_ERR(prog);
4965:
4966: fd = bpf_prog_new_fd(prog);
4967: if (fd < 0)
4968: bpf_prog_put(prog);
4969:
4970: return fd;
4971: }
] map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:bpf_prog_load SourceFile:kernel/bpf/syscall.c SourceLine:3089 SourceSnippet:3084:
3085: if (bpf_prog_load_check_attach(type, attr->expected_attach_type,
3086: attach_btf, attr->attach_btf_id,
3087: dst_prog, multi_func)) {
3088: if (dst_prog)
3089: bpf_prog_put(dst_prog);
3090: if (attach_btf)
3091: btf_put(attach_btf);
3092: err = -EINVAL;
3093: goto put_token;
3094: }
] map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:bpf_prog_load SourceFile:kernel/bpf/syscall.c SourceLine:3100 SourceSnippet:3095:
3096: /* plain bpf_prog allocation */
3097: prog = bpf_prog_alloc(bpf_prog_size(attr->insn_cnt), GFP_USER);
3098: if (!prog) {
3099: if (dst_prog)
3100: bpf_prog_put(dst_prog);
3101: if (attach_btf)
3102: btf_put(attach_btf);
3103: err = -EINVAL;
3104: goto put_token;
3105: }
] map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:bpf_prog_load SourceFile:kernel/bpf/syscall.c SourceLine:3229 SourceSnippet:3224: perf_event_bpf_event(prog, PERF_BPF_EVENT_PROG_LOAD, 0);
3225: bpf_audit_prog(prog, BPF_AUDIT_LOAD);
3226:
3227: err = bpf_prog_new_fd(prog);
3228: if (err < 0)
3229: bpf_prog_put(prog);
3230: return err;
3231:
3232: free_used_maps:
3233: /* In case we have subprogs, we need to wait for a grace
3234: * period before we can tear down JIT memory since symbols
] map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:bpf_prog_release SourceFile:kernel/bpf/syscall.c SourceLine:2507 SourceSnippet:2503: static int bpf_prog_release(struct inode *inode, struct file *filp)
2504: {
2505: struct bpf_prog *prog = filp->private_data;
2506:
2507: bpf_prog_put(prog);
2508: return 0;
2509: }
] map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:bpf_prog_seq_next SourceFile:kernel/bpf/prog_iter.c SourceLine:33 SourceSnippet: 28: {
29: struct bpf_iter_seq_prog_info *info = seq->private;
30:
31: ++*pos;
32: ++info->prog_id;
33: bpf_prog_put((struct bpf_prog *)v);
34: return bpf_prog_get_curr_or_next(&info->prog_id);
35: }
] map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:bpf_prog_seq_stop SourceFile:kernel/bpf/prog_iter.c SourceLine:71 SourceSnippet: 66: static void bpf_prog_seq_stop(struct seq_file *seq, void *v)
67: {
68: if (!v)
69: (void)__bpf_prog_seq_show(seq, v, true);
70: else
71: bpf_prog_put((struct bpf_prog *)v);
72: }
] map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:bpf_prog_test_run SourceFile:kernel/bpf/syscall.c SourceLine:4861 SourceSnippet:4856: return PTR_ERR(prog);
4857:
4858: if (prog->aux->ops->test_run)
4859: ret = prog->aux->ops->test_run(prog, attr, uattr);
4860:
4861: bpf_prog_put(prog);
4862: return ret;
4863: }
] map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:bpf_raw_tracepoint_open SourceFile:kernel/bpf/syscall.c SourceLine:4448 SourceSnippet:4443:
4444: tp_name = u64_to_user_ptr(attr->raw_tracepoint.name);
4445: cookie = attr->raw_tracepoint.cookie;
4446: fd = bpf_raw_tp_link_attach(prog, tp_name, cookie, prog->expected_attach_type);
4447: if (fd < 0)
4448: bpf_prog_put(prog);
4449: return fd;
4450: }
] map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:bpf_struct_ops_map_update_elem SourceFile:kernel/bpf/bpf_struct_ops.c SourceLine:809 SourceSnippet: 804: }
805:
806: if (prog->type != BPF_PROG_TYPE_STRUCT_OPS ||
807: prog->aux->attach_btf_id != st_ops_desc->type_id ||
808: prog->expected_attach_type != i) {
809: bpf_prog_put(prog);
810: err = -EINVAL;
811: goto reset_unlock;
812: }
813:
814: link = kzalloc_obj(*link, GFP_USER);
] map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:bpf_struct_ops_map_update_elem SourceFile:kernel/bpf/bpf_struct_ops.c SourceLine:816 SourceSnippet: 811: goto reset_unlock;
812: }
813:
814: link = kzalloc_obj(*link, GFP_USER);
815: if (!link) {
816: bpf_prog_put(prog);
817: err = -ENOMEM;
818: goto reset_unlock;
819: }
820: bpf_tramp_link_init(link, BPF_LINK_TYPE_STRUCT_OPS,
821: &bpf_struct_ops_link_lops, prog, prog->expected_attach_type, 0);
] map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:bpf_task_work_ctx_reset SourceFile:kernel/bpf/helpers.c SourceLine:4403 SourceSnippet:4400: static void bpf_task_work_ctx_reset(struct bpf_task_work_ctx *ctx)
4401: {
4402: if (ctx->prog) {
4403: bpf_prog_put(ctx->prog);
4404: ctx->prog = NULL;
4405: }
4406: if (ctx->task) {
4407: bpf_task_release(ctx->task);
4408: ctx->task = NULL;
] map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:bpf_task_work_schedule SourceFile:kernel/bpf/helpers.c SourceLine:4636 SourceSnippet:4631: return 0;
4632:
4633: release_all:
4634: bpf_task_release(task);
4635: release_prog:
4636: bpf_prog_put(prog);
4637: return err;
4638: }
] map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:bpf_tracing_link_release SourceFile:kernel/bpf/syscall.c SourceLine:3638 SourceSnippet:3633:
3634: bpf_trampoline_put(tr_link->trampoline);
3635:
3636: /* tgt_prog is NULL if target is a kernel function */
3637: if (tr_link->tgt_prog)
3638: bpf_prog_put(tr_link->tgt_prog);
3639: }
] map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:bpf_tracing_prog_attach SourceFile:kernel/bpf/syscall.c SourceLine:3887 SourceSnippet:3882: * program is (re-)attached to another target.
3883: */
3884: if (prog->aux->dst_prog &&
3885: (tgt_prog_fd || tr != prog->aux->dst_trampoline))
3886: /* got extra prog ref from syscall, or attaching to different prog */
3887: bpf_prog_put(prog->aux->dst_prog);
3888: if (prog->aux->dst_trampoline && tr != prog->aux->dst_trampoline)
3889: /* we allocated a new trampoline, so free the old one */
3890: bpf_trampoline_put(prog->aux->dst_trampoline);
3891:
3892: prog->aux->dst_prog = NULL;
] map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:bpf_tracing_prog_attach SourceFile:kernel/bpf/syscall.c SourceLine:3904 SourceSnippet:3899: bpf_trampoline_put(tr);
3900: mutex_unlock(&prog->aux->dst_mutex);
3901: kfree(link);
3902: out_put_prog:
3903: if (tgt_prog_fd && tgt_prog)
3904: bpf_prog_put(tgt_prog);
3905: return err;
3906: }
] map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:dev_map_free SourceFile:kernel/bpf/devmap.c SourceLine:229 SourceSnippet: 224: head = dev_map_index_hash(dtab, i);
225:
226: hlist_for_each_entry_safe(dev, next, head, index_hlist) {
227: hlist_del_rcu(&dev->index_hlist);
228: if (dev->xdp_prog)
229: bpf_prog_put(dev->xdp_prog);
230: dev_put(dev->dev);
231: kfree(dev);
232: }
233: }
234:
] map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:dev_map_free SourceFile:kernel/bpf/devmap.c SourceLine:245 SourceSnippet: 240: dev = rcu_dereference_raw(dtab->netdev_map[i]);
241: if (!dev)
242: continue;
243:
244: if (dev->xdp_prog)
245: bpf_prog_put(dev->xdp_prog);
246: dev_put(dev->dev);
247: kfree(dev);
248: }
249:
250: bpf_map_area_free(dtab->netdev_map);
] map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:iter_release SourceFile:kernel/bpf/bpf_iter.c SourceLine:277 SourceSnippet: 272: target_private);
273:
274: if (iter_priv->seq_info->fini_seq_private)
275: iter_priv->seq_info->fini_seq_private(seq->private);
276:
277: bpf_prog_put(iter_priv->prog);
278: seq->private = iter_priv;
279:
280: return seq_release_private(inode, file);
281: }
] map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:kern_sys_bpf SourceFile:kernel/bpf/syscall.c SourceLine:6597 SourceSnippet:6592: if (IS_ERR(prog))
6593: return PTR_ERR(prog);
6594:
6595: if (attr->test.ctx_size_in < prog->aux->max_ctx_offset ||
6596: attr->test.ctx_size_in > U16_MAX) {
6597: bpf_prog_put(prog);
6598: return -EINVAL;
6599: }
6600:
6601: run_ctx.bpf_cookie = 0;
6602: if (!__bpf_prog_enter_sleepable_recur(prog, &run_ctx)) {
] map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:kern_sys_bpf SourceFile:kernel/bpf/syscall.c SourceLine:6605 SourceSnippet:6600:
6601: run_ctx.bpf_cookie = 0;
6602: if (!__bpf_prog_enter_sleepable_recur(prog, &run_ctx)) {
6603: /* recursion detected */
6604: __bpf_prog_exit_sleepable_recur(prog, 0, &run_ctx);
6605: bpf_prog_put(prog);
6606: return -EBUSY;
6607: }
6608: attr->test.retval = bpf_prog_run(prog, (void *) (long) attr->test.ctx_in);
6609: __bpf_prog_exit_sleepable_recur(prog, 0 /* bpf_prog_run does runtime stats */,
6610: &run_ctx);
] map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:kern_sys_bpf SourceFile:kernel/bpf/syscall.c SourceLine:6611 SourceSnippet:6606: return -EBUSY;
6607: }
6608: attr->test.retval = bpf_prog_run(prog, (void *) (long) attr->test.ctx_in);
6609: __bpf_prog_exit_sleepable_recur(prog, 0 /* bpf_prog_run does runtime stats */,
6610: &run_ctx);
6611: bpf_prog_put(prog);
6612: return 0;
6613: #endif
6614: default:
6615: return ____bpf_sys_bpf(cmd, attr, size);
6616: }
] map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:link_create SourceFile:kernel/bpf/syscall.c SourceLine:5946 SourceSnippet:5941: ret = -EINVAL;
5942: }
5943:
5944: out:
5945: if (ret < 0)
5946: bpf_prog_put(prog);
5947: return ret;
5948: }
] map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:link_update SourceFile:kernel/bpf/syscall.c SourceLine:6029 SourceSnippet:6024: else
6025: ret = -EINVAL;
6026:
6027: out_put_progs:
6028: if (old_prog)
6029: bpf_prog_put(old_prog);
6030: if (ret)
6031: bpf_prog_put(new_prog);
6032: out_put_link:
6033: bpf_link_put_direct(link);
6034: return ret;
] map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:link_update SourceFile:kernel/bpf/syscall.c SourceLine:6031 SourceSnippet:6026:
6027: out_put_progs:
6028: if (old_prog)
6029: bpf_prog_put(old_prog);
6030: if (ret)
6031: bpf_prog_put(new_prog);
6032: out_put_link:
6033: bpf_link_put_direct(link);
6034: return ret;
6035: }
] map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:netns_bpf_pernet_pre_exit SourceFile:kernel/bpf/net_namespace.c SourceLine:550 SourceSnippet: 545: list_for_each_entry(net_link, &net->bpf.links[type], node) {
546: net_link->net = NULL; /* auto-detach link */
547: netns_bpf_attach_type_unneed(type);
548: }
549: if (net->bpf.progs[type])
550: bpf_prog_put(net->bpf.progs[type]);
551: }
552: mutex_unlock(&netns_bpf_mutex);
553: }
] map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:netns_bpf_prog_attach SourceFile:kernel/bpf/net_namespace.c SourceLine:354 SourceSnippet: 349: rcu_assign_pointer(net->bpf.run_array[type], run_array);
350: }
351:
352: net->bpf.progs[type] = prog;
353: if (attached)
354: bpf_prog_put(attached);
355:
356: out_unlock:
357: mutex_unlock(&netns_bpf_mutex);
358:
359: return ret;
] map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:netns_bpf_prog_detach SourceFile:kernel/bpf/net_namespace.c SourceLine:403 SourceSnippet: 398:
399: mutex_lock(&netns_bpf_mutex);
400: ret = __netns_bpf_prog_detach(current->nsproxy->net_ns, type, prog);
401: mutex_unlock(&netns_bpf_mutex);
402:
403: bpf_prog_put(prog);
404:
405: return ret;
406: }
] map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:prepare_seq_file SourceFile:kernel/bpf/bpf_iter.c SourceLine:630 SourceSnippet: 625:
626: release_seq_file:
627: seq_release_private(file->f_inode, file);
628: file->private_data = NULL;
629: release_prog:
630: bpf_prog_put(prog);
631: return err;
632: }
] map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:prog_assoc_struct_ops SourceFile:kernel/bpf/syscall.c SourceLine:6350 SourceSnippet:6345: ret = bpf_prog_assoc_struct_ops(prog, map);
6346:
6347: put_map:
6348: bpf_map_put(map);
6349: put_prog:
6350: bpf_prog_put(prog);
6351: return ret;
6352: }
] map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:prog_fd_array_get_ptr SourceFile:kernel/bpf/arraymap.c SourceLine:972 SourceSnippet: 967: if (IS_ERR(prog))
968: return prog;
969:
970: if (prog->type == BPF_PROG_TYPE_EXT ||
971: !bpf_prog_map_compatible(map, prog)) {
972: bpf_prog_put(prog);
973: return ERR_PTR(-EINVAL);
974: }
975:
976: mutex_lock(&prog->aux->ext_mutex);
977: is_extended = prog->aux->is_extended;
] map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:prog_fd_array_get_ptr SourceFile:kernel/bpf/arraymap.c SourceLine:987 SourceSnippet: 982: /* Extended prog can not be tail callee. It's to prevent a
983: * potential infinite loop like:
984: * tail callee prog entry -> tail callee prog subprog ->
985: * freplace prog entry --tailcall-> tail callee prog entry.
986: */
987: bpf_prog_put(prog);
988: return ERR_PTR(-EBUSY);
989: }
990:
991: return prog;
992: }
] map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:prog_fd_array_put_ptr SourceFile:kernel/bpf/arraymap.c SourceLine:1002 SourceSnippet: 997:
998: mutex_lock(&prog->aux->ext_mutex);
999: prog->aux->prog_array_member_cnt--;
1000: mutex_unlock(&prog->aux->ext_mutex);
1001: /* bpf_prog is freed after one RCU or tasks trace grace period */
1002: bpf_prog_put(prog);
1003: }
] map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:prog_stream_read SourceFile:kernel/bpf/syscall.c SourceLine:6306 SourceSnippet:6301: prog = bpf_prog_get(attr->prog_stream_read.prog_fd);
6302: if (IS_ERR(prog))
6303: return PTR_ERR(prog);
6304:
6305: ret = bpf_prog_stream_read(prog, attr->prog_stream_read.stream_id, buf, len);
6306: bpf_prog_put(prog);
6307:
6308: return ret;
6309: }
] map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:tcx_link_update SourceFile:kernel/bpf/tcx.c SourceLine:224 SourceSnippet: 219: ret = -EPERM;
220: goto out;
221: }
222: oprog = link->prog;
223: if (oprog == nprog) {
224: bpf_prog_put(nprog);
225: goto out;
226: }
227: entry = tcx_entry_fetch(dev, ingress);
228: if (!entry) {
229: ret = -ENOENT;
] map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:tcx_link_update SourceFile:kernel/bpf/tcx.c SourceLine:238 SourceSnippet: 233: BPF_F_REPLACE | BPF_F_ID,
234: link->prog->aux->id, 0);
235: if (!ret) {
236: WARN_ON_ONCE(entry != entry_new);
237: oprog = xchg(&link->prog, nprog);
238: bpf_prog_put(oprog);
239: bpf_mprog_commit(entry);
240: }
241: out:
242: rtnl_unlock();
243: return ret;
] map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:tcx_prog_attach SourceFile:kernel/bpf/tcx.c SourceLine:54 SourceSnippet: 49: } else if (created) {
50: tcx_entry_free(entry);
51: }
52: out:
53: if (replace_prog)
54: bpf_prog_put(replace_prog);
55: rtnl_unlock();
56: return ret;
57: }
] map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:tcx_uninstall SourceFile:kernel/bpf/tcx.c SourceLine:115 SourceSnippet: 110: tcx_entry_sync();
111: bpf_mprog_foreach_tuple(entry, fp, cp, tuple) {
112: if (tuple.link)
113: tcx_link(tuple.link)->dev = NULL;
114: else
115: bpf_prog_put(tuple.prog);
116: tcx_skeys_dec(ingress);
117: }
118: if (!active)
119: tcx_entry_free(entry);
120: }
] map[ReferenceKind:takes-address-of ReferencingEntityKind:global_variable ReferencingEntityName:__UNIQUE_ID_addressable_bpf_prog_put_1354 SourceFile:kernel/bpf/syscall.c SourceLine:2501 SourceSnippet:2501: EXPORT_SYMBOL_GPL(bpf_prog_put);
] map[ReferenceKind:takes-address-of ReferencingEntityKind:global_variable ReferencingEntityName:__gendwarfksyms_ptr_bpf_prog_put SourceFile:kernel/bpf/syscall.c SourceLine:2501 SourceSnippet:2501: EXPORT_SYMBOL_GPL(bpf_prog_put);
] map[ReferenceKind:takes-address-of ReferencingEntityKind:global_variable ReferencingEntityName:__gendwarfksyms_ptr_bpf_prog_put SourceFile:kernel/bpf/syscall.c SourceLine:2501 SourceSnippet:2501: EXPORT_SYMBOL_GPL(bpf_prog_put);
]] TruncatedOutput:false]
|
| 19/2 |
2026/07/26 03:38 |
llm |
expert |
0m
Model:
Tokens:
input: 32892
output: 37
thoughts: 30
Thoughts: Here's my analysis of the provided thought process, tailored for an expert audience:
**Analyzing `bpf_prog_put`**
Okay, so the thought starts by zeroing in on `bpf_prog_put` within `kernel/bpf/syscall.c`. My immediate thought is, "Right, `bpf_prog_put`. I know this is a critical function related to BPF program lifecycle management. It's the counterpart to something like `bpf_prog_get` or program creation/attachment." The fact that it's in the syscall file confirms it's the entry point for releasing a BPF program from user space. I'm going to carefully review it. The goal, most likely, is to understand its reference counting behavior, resource cleanup, and the implications of its execution path. This is key to preventing memory leaks, use-after-free conditions, or other related vulnerabilities that are common in memory management. I'm mentally parsing the likely steps: decrementing a refcount, checking if zero, and then calling a teardown routine if it does.
|
| 20/2 |
2026/07/26 03:38 |
tool |
read-file |
0m
Args: map[File:kernel/bpf/syscall.c FirstLine:2480 LineCount:30]
Results: map[Contents:2480: __bpf_prog_put_noref(prog, true);
2481: }
2482:
2483: static void __bpf_prog_put(struct bpf_prog *prog)
2484: {
2485: struct bpf_prog_aux *aux = prog->aux;
2486:
2487: if (atomic64_dec_and_test(&aux->refcnt)) {
2488: if (in_hardirq() || irqs_disabled()) {
2489: INIT_WORK(&aux->work, bpf_prog_put_deferred);
2490: schedule_work(&aux->work);
2491: } else {
2492: bpf_prog_put_deferred(&aux->work);
2493: }
2494: }
2495: }
2496:
2497: void bpf_prog_put(struct bpf_prog *prog)
2498: {
2499: __bpf_prog_put(prog);
2500: }
2501: EXPORT_SYMBOL_GPL(bpf_prog_put);
2502:
2503: static int bpf_prog_release(struct inode *inode, struct file *filp)
2504: {
2505: struct bpf_prog *prog = filp->private_data;
2506:
2507: bpf_prog_put(prog);
2508: return 0;
2509: }
]
|
| 21/2 |
2026/07/26 03:38 |
llm |
expert |
0m
Model:
Tokens:
input: 33631
output: 40
thoughts: 30
Thoughts: Okay, here's my interpretation of that thought process, tailored for an expert audience, and presented as if I'm having the thoughts:
**Analyzing `bpf_prog_put_deferred`**
Right, `bpf_prog_put_deferred` β that's the function that's responsible for freeing the BPF program, isn't it? Okay, let's drill down into its details. Time to see what's actually happening under the hood when a BPF program reference count hits zero and the kernel decides it's time to release it. Understanding this function is crucial for grasping memory management in the BPF runtime and avoiding potential resource leaks or use-after-free scenarios. I need to refresh my understanding of the reference counting semantics and how it interacts with any deferred actions or delayed cleanup mechanisms in the BPF subsystem. Let's see how they've implemented the `bpf_prog_put_deferred` function in this version.
|
| 22/2 |
2026/07/26 03:38 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:kernel/bpf/syscall.c Name:bpf_prog_put_deferred]
Results: map[SourceCode:2470: static void bpf_prog_put_deferred(struct work_struct *work)
2471: {
2472: struct bpf_prog_aux *aux;
2473: struct bpf_prog *prog;
2474:
2475: aux = container_of(work, struct bpf_prog_aux, work);
2476: prog = aux->prog;
2477: perf_event_bpf_event(prog, PERF_BPF_EVENT_PROG_UNLOAD, 0);
2478: bpf_audit_prog(prog, BPF_AUDIT_UNLOAD);
2479: bpf_prog_free_id(prog);
2480: __bpf_prog_put_noref(prog, true);
2481: }
SourceFile:kernel/bpf/syscall.c]
|
| 23/2 |
2026/07/26 03:38 |
llm |
expert |
0m
Model:
Tokens:
input: 34153
output: 42
thoughts: 17
Thoughts: Okay, let's dive into this `__bpf_prog_put_noref` function. I see it's about program deallocation, specifically when there are no more references. That's a critical area; memory management in BPF is notoriously delicate, and getting this wrong leads to instability or worse. I need to understand this meticulously.
I'm thinking about the potential race conditions here. BPF programs can run in highly concurrent environments, and atomicity is paramount. What mechanisms are in place to ensure a program isn't prematurely deallocated while actively being used? Are there locks involved? Reference counting? If it *is* reference-counted, what does "no ref" actually mean in this context? Is it simply a count, or are there special markers that indicate the object is in a "to-be-freed" state?
I'm also considering the implications for other BPF subsystems. Does this function interact with the verifier in any way? Does it need to signal something to the verifier, potentially to flush its internal state or invalidate any caches related to this program? Any changes here could have ripple effects. How are program state transitions managed? Are there checks for in-flight tracing or kprobes? Does this function have a role in handling the data stored within a program's maps?
I'm going to pay close attention to the data structures involved, the locks held, and the potential interactions with other kernel subsystems. This needs to be bulletproof. I need to be sure that this doesn't introduce any subtle memory leaks, double-frees, or corruption bugs, since this area has a history of subtle bugs. I need to ensure it integrates seamlessly with the rest of the kernel, and the overall bpf system.
|
| 24/2 |
2026/07/26 03:38 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:kernel/bpf/syscall.c Name:__bpf_prog_put_noref]
Results: map[SourceCode:2448: static void __bpf_prog_put_noref(struct bpf_prog *prog, bool deferred)
2449: {
2450: bpf_prog_kallsyms_del_all(prog);
2451: btf_put(prog->aux->btf);
2452: module_put(prog->aux->mod);
2453: kvfree(prog->aux->jited_linfo);
2454: kvfree(prog->aux->linfo);
2455: kfree(prog->aux->kfunc_tab);
2456: kfree(prog->aux->ctx_arg_info);
2457: if (prog->aux->attach_btf)
2458: btf_put(prog->aux->attach_btf);
2459:
2460: if (deferred) {
2461: if (prog->sleepable)
2462: call_rcu_tasks_trace(&prog->aux->rcu, __bpf_prog_put_rcu);
2463: else
2464: call_rcu(&prog->aux->rcu, __bpf_prog_put_rcu);
2465: } else {
2466: __bpf_prog_put_rcu(&prog->aux->rcu);
2467: }
2468: }
SourceFile:kernel/bpf/syscall.c]
|
| 25/2 |
2026/07/26 03:38 |
llm |
expert |
0m
Model:
Tokens:
input: 34979
output: 58
thoughts: 2236
Thoughts: **RCU Conundrum in BPF Program Handling**
Okay, so the `__bpf_prog_put_noref` function uses RCU (`call_rcu` or `call_rcu_tasks_trace`) to free the `struct bpf_prog` memory after an RCU grace period. This is standard, but the implications here are important. My initial concern is whether `__perf_event_overflow` is running under an RCU read lock. It isn't, which is the immediate red flag. I need to understand the call chain and, more importantly, the lack of an RCU read lock.
`__perf_event_overflow` is called from `perf_event_overflow`, which is triggered by various interrupts like PMU and hrtimer interrupts. Critically, `__perf_event_overflow` *itself* doesn't have an `rcu_read_lock()`. Inside `__perf_event_overflow`, there's a check: `if (event->prog && event->prog->type == BPF_PROG_TYPE_PERF_EVENT && !bpf_overflow_handler(event, data, regs))`.
The critical part here is that `event->prog` is read locklessly, and then dereferenced. Now, `bpf_overflow_handler` *does* have an `rcu_read_lock()` and also uses `READ_ONCE(event->prog)`:
```c
static inline int bpf_overflow_handler(struct perf_event *event,
struct perf_sample_data *data,
struct pt_regs *regs)
{
...
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();
...
}
```
This is odd. Why is `bpf_overflow_handler` protected but `__perf_event_overflow` isn't? The logic in `__perf_event_overflow` checks `event->prog` *before* calling `bpf_overflow_handler`, but that check alone doesn't prevent a race condition. The check is there because `bpf_overflow_handler` only runs if `event->prog` is valid, and because `bpf_overflow_handler` can handle the overflow and return 0. The `goto out` is only reached if the BPF handler returns 0. If `event->prog` *is* NULL, the code *shouldn't* `goto out`. This isn't a problem, the code handles it correctly.
But this brings me to a use-after-free scenario. If `event->prog` is not NULL and is freed concurrently, there's a problem: a double-load. Even with the check in `__perf_event_overflow`, a double-load by the compiler, or a torn pointer, or reading a freed pointer would cause a dereference after free.
If `__perf_event_overflow` runs within an interrupt context, that could imply an RCU read-side critical section, preventing grace periods from completing, but it's *not* always the case. With `CONFIG_PREEMPT_RCU`, interrupts *do not* necessarily act as RCU read-side critical sections. The RCU grace period can complete, potentially causing a data race. With `CONFIG_PREEMPT_RCU`, an interrupt can be preempted.
The compiler could perform a double load, meaning:
1. CPU 1 (interrupt) executes `__perf_event_overflow`.
2. It reads `event->prog`. It sees a valid pointer.
3. CPU 0 (task) frees the event and sets `event->prog = NULL`.
4. CPU 1 dereferences the now-NULL `event->prog`, resulting in a kernel panic!
This is a real threat. I'm seeing from the KCSAN report that `event->prog` value changed from NULL to a valid pointer, confirming this risk.
`perf_event_free_bpf_handler` sets `event->prog = NULL`. The key transition is from non-NULL to NULL because of the NULL pointer dereference.
|
| 26/2 |
2026/07/26 03:38 |
tool |
codesearch-find-references |
0m
Args: map[ContextFile:kernel/events/core.c IncludeSnippetLines:5 Name:perf_event_free_bpf_prog SourceTreePrefix:kernel/events/]
Results: map[References:[map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:__pmu_detach_event SourceFile:kernel/events/core.c SourceLine:12957 SourceSnippet:12952: * Mostly an empty lock sequence, such that perf_mmap(), which
12953: * relies on mmap_mutex, is sure to observe the state change.
12954: */
12955: }
12956:
12957: perf_event_free_bpf_prog(event);
12958: perf_free_addr_filters(event);
12959:
12960: if (event->destroy) {
12961: event->destroy(event);
12962: event->destroy = NULL;
] map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:_free_event SourceFile:kernel/events/core.c SourceLine:5828 SourceSnippet:5823: mutex_lock(&event->mmap_mutex);
5824: ring_buffer_attach(event, NULL);
5825: mutex_unlock(&event->mmap_mutex);
5826: }
5827:
5828: perf_event_free_bpf_prog(event);
5829: perf_free_addr_filters(event);
5830:
5831: __free_event(event);
5832: }
]] TruncatedOutput:false]
|
| 27/2 |
2026/07/26 03:38 |
llm |
expert |
0m
Model:
Tokens:
input: 38655
output: 21
thoughts: 405
Thoughts: **Analyzing BPF Program Detachment from Perf Events**
Okay, so I'm thinking through how BPF programs are detached from perf events. Right, `_free_event` does a call to `perf_event_free_bpf_prog(event)`, and that in turn hits `perf_event_free_bpf_handler(event)`. That function sets `event->prog` to `NULL`. Makes sense, that's how it's cleaned up.
But hold on a second... is *that* the only way a BPF program gets detached? Are there other ways? I need to make sure I haven't missed a method or got a misunderstanding.
Let's see. Thinking about replacement options, `perf_event_set_bpf_handler` returns `-EEXIST` if the program is already set. That's a clear block:
```c
if (event->prog)
return -EEXIST;
```
So direct replacement using `SET_BPF` is out if a program already exists. But, you *can* free or destroy the event, and that, in turn, frees the BPF program. Got it.
But...wait, are there other places where `event->prog` gets modified? Let's go back and re-examine `perf_event_free_bpf_handler`.
```c
static inline void perf_event_free_bpf_handler(struct perf_event *event)
{
struct bpf_prog *prog = event->prog;
if (!prog)
return;
event->prog = NULL;
bpf_prog_put(prog);
}
```
Okay, so it does the put, and then the set to NULL.
Now, thinking practically, if the event *is* being freed, could `__perf_event_overflow` *still* be called? Logically, no. Normally, the event is disabled and removed from the PMU *before* it's freed, so no more interrupts should come.
But hold on, am I being too narrow? Is there a function like `perf_event_detach_bpf_prog`, or a method that's *similar*?
Okay, so the real question is: is there a clean way to *detach* a BPF program from a perf event without completely nuking the event itself? That seems like the crucial point here, because if there were, it'd give you a lot more control. I need to know.
I guess I'll search for `perf_event_detach_bpf_prog` or something close.
|
| 28/2 |
2026/07/26 03:38 |
tool |
grepper |
0m
Args: map[Expression:perf_event_.*bpf]
Results: map[Output:
Full output is too long, showing 500 out of 571 lines.
Use more precise expression if possible.
include/linux/bpf_types.h=43=BPF_PROG_TYPE(BPF_PROG_TYPE_PERF_EVENT, perf_event,
include/linux/bpf_types.h:44: struct bpf_perf_event_data, struct bpf_perf_event_data_kern)
include/linux/bpf_types.h-45-BPF_PROG_TYPE(BPF_PROG_TYPE_RAW_TRACEPOINT, raw_tracepoint,
--
include/linux/perf_event.h=1667=extern void perf_event_ksymbol(u16 ksym_type, u64 addr, u32 len,
include/linux/perf_event.h-1668- bool unregister, const char *sym);
include/linux/perf_event.h:1669:extern void perf_event_bpf_event(struct bpf_prog *prog,
include/linux/perf_event.h-1670- enum perf_bpf_event_type type,
--
include/linux/perf_event.h=1996=static inline void perf_event_ksymbol(u16 ksym_type, u64 addr, u32 len,
include/linux/perf_event.h-1997- bool unregister, const char *sym) { }
include/linux/perf_event.h:1998:static inline void perf_event_bpf_event(struct bpf_prog *prog,
include/linux/perf_event.h-1999- enum perf_bpf_event_type type,
--
include/linux/trace_events.h=773=unsigned int trace_call_bpf_faultable(struct trace_event_call *call, void *ctx);
include/linux/trace_events.h:774:int perf_event_attach_bpf_prog(struct perf_event *event, struct bpf_prog *prog, u64 bpf_cookie);
include/linux/trace_events.h:775:void perf_event_detach_bpf_prog(struct perf_event *event);
include/linux/trace_events.h-776-int perf_event_query_prog_array(struct perf_event *event, void __user *info);
--
include/linux/trace_events.h=802=static inline int
include/linux/trace_events.h:803:perf_event_attach_bpf_prog(struct perf_event *event, struct bpf_prog *prog, u64 bpf_cookie)
include/linux/trace_events.h-804-{
--
include/linux/trace_events.h-807-
include/linux/trace_events.h:808:static inline void perf_event_detach_bpf_prog(struct perf_event *event) { }
include/linux/trace_events.h-809-
--
include/linux/trace_events.h=912=void *perf_trace_buf_alloc(int size, struct pt_regs **regs, int *rctxp);
include/linux/trace_events.h-913-
include/linux/trace_events.h:914:int perf_event_set_bpf_prog(struct perf_event *event, struct bpf_prog *prog, u64 bpf_cookie);
include/linux/trace_events.h:915:void perf_event_free_bpf_prog(struct perf_event *event);
include/linux/trace_events.h-916-
--
include/uapi/linux/bpf.h=1527=union bpf_attr {
--
include/uapi/linux/bpf.h-2410- *
include/uapi/linux/bpf.h:2411: * u64 bpf_perf_event_read(struct bpf_map *map, u64 flags)
include/uapi/linux/bpf.h-2412- * Description
--
include/uapi/linux/bpf.h-2486- *
include/uapi/linux/bpf.h:2487: * long bpf_perf_event_output(void *ctx, struct bpf_map *map, u64 flags, void *data, u64 size)
include/uapi/linux/bpf.h-2488- * Description
--
include/uapi/linux/bpf.h-3135- *
include/uapi/linux/bpf.h:3136: * long bpf_perf_event_read_value(struct bpf_map *map, u64 flags, struct bpf_perf_event_value *buf, u32 buf_size)
include/uapi/linux/bpf.h-3137- * Description
--
include/uapi/linux/bpf.h-3185- *
include/uapi/linux/bpf.h:3186: * long bpf_perf_prog_read_value(struct bpf_perf_event_data *ctx, struct bpf_perf_event_value *buf, u32 buf_size)
include/uapi/linux/bpf.h-3187- * Description
--
include/uapi/linux/perf_event.h=393=struct perf_event_attr {
--
include/uapi/linux/perf_event.h-556- */
include/uapi/linux/perf_event.h:557:struct perf_event_query_bpf {
include/uapi/linux/perf_event.h-558- /*
--
include/uapi/linux/perf_event.h-585-#define PERF_EVENT_IOC_PAUSE_OUTPUT _IOW ('$', 9, __u32)
include/uapi/linux/perf_event.h:586:#define PERF_EVENT_IOC_QUERY_BPF _IOWR('$', 10, struct perf_event_query_bpf *)
include/uapi/linux/perf_event.h-587-#define PERF_EVENT_IOC_MODIFY_ATTRIBUTES _IOW ('$', 11, struct perf_event_attr *)
--
kernel/bpf/arraymap.c=1259=static void bpf_event_entry_free_rcu(struct bpf_event_entry *ee)
--
kernel/bpf/arraymap.c-1263-
kernel/bpf/arraymap.c:1264:static void *perf_event_fd_array_get_ptr(struct bpf_map *map,
kernel/bpf/arraymap.c-1265- struct file *map_file, int fd)
--
kernel/bpf/arraymap.c-1289-
kernel/bpf/arraymap.c:1290:static void perf_event_fd_array_put_ptr(struct bpf_map *map, void *ptr, bool need_defer)
kernel/bpf/arraymap.c-1291-{
--
kernel/bpf/arraymap.c-1295-
kernel/bpf/arraymap.c:1296:static void perf_event_fd_array_release(struct bpf_map *map,
kernel/bpf/arraymap.c-1297- struct file *map_file)
--
kernel/bpf/arraymap.c-1314-
kernel/bpf/arraymap.c:1315:static void perf_event_fd_array_map_free(struct bpf_map *map)
kernel/bpf/arraymap.c-1316-{
--
kernel/bpf/syscall.c=2470=static void bpf_prog_put_deferred(struct work_struct *work)
--
kernel/bpf/syscall.c-2476- prog = aux->prog;
kernel/bpf/syscall.c:2477: perf_event_bpf_event(prog, PERF_BPF_EVENT_PROG_UNLOAD, 0);
kernel/bpf/syscall.c-2478- bpf_audit_prog(prog, BPF_AUDIT_UNLOAD);
--
kernel/bpf/syscall.c=2972=static int bpf_prog_load(union bpf_attr *attr, bpfptr_t uattr, struct bpf_log_attr *attr_log)
--
kernel/bpf/syscall.c-3223- bpf_prog_kallsyms_add(prog);
kernel/bpf/syscall.c:3224: perf_event_bpf_event(prog, PERF_BPF_EVENT_PROG_LOAD, 0);
kernel/bpf/syscall.c-3225- bpf_audit_prog(prog, BPF_AUDIT_LOAD);
--
kernel/bpf/syscall.c=3689=static int bpf_tracing_prog_attach(struct bpf_prog *prog,
--
kernel/bpf/syscall.c-3857- * by tgt_prog itself. At attach time, uprobe/kprobe is recognized by
kernel/bpf/syscall.c:3858: * the target perf event flags in __perf_event_set_bpf_prog().
kernel/bpf/syscall.c-3859- */
--
kernel/bpf/syscall.c=3992=static void bpf_perf_link_release(struct bpf_link *link)
--
kernel/bpf/syscall.c-3996-
kernel/bpf/syscall.c:3997: perf_event_free_bpf_prog(event);
kernel/bpf/syscall.c-3998- fput(perf_link->perf_file);
--
kernel/bpf/syscall.c=4300=static int bpf_perf_link_attach(const union bpf_attr *attr, struct bpf_prog *prog)
--
kernel/bpf/syscall.c-4330- event = perf_file->private_data;
kernel/bpf/syscall.c:4331: err = perf_event_set_bpf_prog(event, prog, attr->link_create.perf_event.bpf_cookie);
kernel/bpf/syscall.c-4332- if (err) {
--
kernel/bpf/syscall.c-4335- }
kernel/bpf/syscall.c:4336: /* perf_event_set_bpf_prog() doesn't take its own refcnt on prog */
kernel/bpf/syscall.c-4337- bpf_prog_inc(prog);
--
kernel/events/core.c=5808=static void _free_event(struct perf_event *event)
--
kernel/events/core.c-5827-
kernel/events/core.c:5828: perf_event_free_bpf_prog(event);
kernel/events/core.c-5829- perf_free_addr_filters(event);
--
kernel/events/core.c=6592=static int perf_copy_attr(struct perf_event_attr __user *uattr,
kernel/events/core.c-6593- struct perf_event_attr *attr);
kernel/events/core.c:6594:static int __perf_event_set_bpf_prog(struct perf_event *event,
kernel/events/core.c-6595- struct bpf_prog *prog,
--
kernel/events/core.c=6598=static long _perf_ioctl(struct perf_event *event, unsigned int cmd, unsigned long arg)
--
kernel/events/core.c-6661-
kernel/events/core.c:6662: err = __perf_event_set_bpf_prog(event, prog, 0);
kernel/events/core.c-6663- if (err) {
--
kernel/events/core.c=10284=struct perf_bpf_event {
--
kernel/events/core.c-10294-
kernel/events/core.c:10295:static int perf_event_bpf_match(struct perf_event *event)
kernel/events/core.c-10296-{
--
kernel/events/core.c-10299-
kernel/events/core.c:10300:static void perf_event_bpf_output(struct perf_event *event, void *data)
kernel/events/core.c-10301-{
--
kernel/events/core.c-10306-
kernel/events/core.c:10307: if (!perf_event_bpf_match(event))
kernel/events/core.c-10308- return;
kernel/events/core.c-10309-
kernel/events/core.c:10310: perf_event_header__init_id(&bpf_event->event_id.header,
kernel/events/core.c-10311- &sample, event);
--
kernel/events/core.c-10322-
kernel/events/core.c:10323:static void perf_event_bpf_emit_ksymbols(struct bpf_prog *prog,
kernel/events/core.c-10324- enum perf_bpf_event_type type)
--
kernel/events/core.c-10344-
kernel/events/core.c:10345:void perf_event_bpf_event(struct bpf_prog *prog,
kernel/events/core.c-10346- enum perf_bpf_event_type type,
--
kernel/events/core.c-10354- if (atomic_read(&nr_ksymbol_events))
kernel/events/core.c:10355: perf_event_bpf_emit_ksymbols(prog, type);
kernel/events/core.c-10356- break;
--
kernel/events/core.c-10379- memcpy(bpf_event.event_id.tag, prog->tag, BPF_TAG_SIZE);
kernel/events/core.c:10380: perf_iterate_sb(perf_event_bpf_output, &bpf_event, NULL);
kernel/events/core.c-10381-}
--
kernel/events/core.c=10653=static int bpf_overflow_handler(struct perf_event *event,
--
kernel/events/core.c-10679-
kernel/events/core.c:10680:static inline int perf_event_set_bpf_handler(struct perf_event *event,
kernel/events/core.c-10681- struct bpf_prog *prog,
--
kernel/events/core.c-10715-
kernel/events/core.c:10716:static inline void perf_event_free_bpf_handler(struct perf_event *event)
kernel/events/core.c-10717-{
--
kernel/events/core.c=10727=static inline int bpf_overflow_handler(struct perf_event *event,
--
kernel/events/core.c-10733-
kernel/events/core.c:10734:static inline int perf_event_set_bpf_handler(struct perf_event *event,
kernel/events/core.c-10735- struct bpf_prog *prog,
--
kernel/events/core.c-10740-
kernel/events/core.c:10741:static inline void perf_event_free_bpf_handler(struct perf_event *event)
kernel/events/core.c-10742-{
--
kernel/events/core.c=11672=static inline bool perf_event_is_tracing(struct perf_event *event)
--
kernel/events/core.c-11686-
kernel/events/core.c:11687:static int __perf_event_set_bpf_prog(struct perf_event *event,
kernel/events/core.c-11688- struct bpf_prog *prog,
--
kernel/events/core.c-11696- if (!perf_event_is_tracing(event))
kernel/events/core.c:11697: return perf_event_set_bpf_handler(event, prog, bpf_cookie);
kernel/events/core.c-11698-
--
kernel/events/core.c-11739-
kernel/events/core.c:11740: return perf_event_attach_bpf_prog(event, prog, bpf_cookie);
kernel/events/core.c-11741-}
kernel/events/core.c-11742-
kernel/events/core.c:11743:int perf_event_set_bpf_prog(struct perf_event *event,
kernel/events/core.c-11744- struct bpf_prog *prog,
--
kernel/events/core.c-11750- ctx = perf_event_ctx_lock(event);
kernel/events/core.c:11751: ret = __perf_event_set_bpf_prog(event, prog, bpf_cookie);
kernel/events/core.c-11752- perf_event_ctx_unlock(event, ctx);
--
kernel/events/core.c-11756-
kernel/events/core.c:11757:void perf_event_free_bpf_prog(struct perf_event *event)
kernel/events/core.c-11758-{
--
kernel/events/core.c-11762- if (!perf_event_is_tracing(event)) {
kernel/events/core.c:11763: perf_event_free_bpf_handler(event);
kernel/events/core.c-11764- return;
kernel/events/core.c-11765- }
kernel/events/core.c:11766: perf_event_detach_bpf_prog(event);
kernel/events/core.c-11767-}
--
kernel/events/core.c=11775=static void perf_event_free_filter(struct perf_event *event)
--
kernel/events/core.c-11778-
kernel/events/core.c:11779:static int __perf_event_set_bpf_prog(struct perf_event *event,
kernel/events/core.c-11780- struct bpf_prog *prog,
--
kernel/events/core.c-11785-
kernel/events/core.c:11786:int perf_event_set_bpf_prog(struct perf_event *event,
kernel/events/core.c-11787- struct bpf_prog *prog,
--
kernel/events/core.c-11792-
kernel/events/core.c:11793:void perf_event_free_bpf_prog(struct perf_event *event)
kernel/events/core.c-11794-{
--
kernel/events/core.c=12936=static void __pmu_detach_event(struct pmu *pmu, struct perf_event *event,
--
kernel/events/core.c-12956-
kernel/events/core.c:12957: perf_event_free_bpf_prog(event);
kernel/events/core.c-12958- perf_free_addr_filters(event);
--
kernel/trace/bpf_trace.c=567=get_map_perf_counter(struct bpf_map *map, u64 flags,
--
kernel/trace/bpf_trace.c-588-
kernel/trace/bpf_trace.c:589:BPF_CALL_2(bpf_perf_event_read, struct bpf_map *, map, u64, flags)
kernel/trace/bpf_trace.c-590-{
--
kernel/trace/bpf_trace.c=604=const struct bpf_func_proto bpf_perf_event_read_proto = {
--
kernel/trace/bpf_trace.c-611-
kernel/trace/bpf_trace.c:612:BPF_CALL_4(bpf_perf_event_read_value, struct bpf_map *, map, u64, flags,
kernel/trace/bpf_trace.c-613- struct bpf_perf_event_value *, buf, u32, size)
--
kernel/trace/bpf_trace.c=644=static __always_inline u64
kernel/trace/bpf_trace.c:645:__bpf_perf_event_output(struct pt_regs *regs, struct bpf_map *map,
kernel/trace/bpf_trace.c-646- u64 flags, struct perf_raw_record *raw,
--
kernel/trace/bpf_trace.c=686=static DEFINE_PER_CPU(int, bpf_trace_nest_level);
kernel/trace/bpf_trace.c:687:BPF_CALL_5(bpf_perf_event_output, struct pt_regs *, regs, struct bpf_map *, map,
kernel/trace/bpf_trace.c-688- u64, flags, void *, data, u64, size)
--
kernel/trace/bpf_trace.c=1400=const struct bpf_prog_ops kprobe_prog_ops = {
--
kernel/trace/bpf_trace.c-1402-
kernel/trace/bpf_trace.c:1403:BPF_CALL_5(bpf_perf_event_output_tp, void *, tp_buff, struct bpf_map *, map,
kernel/trace/bpf_trace.c-1404- u64, flags, void *, data, u64, size)
--
kernel/trace/bpf_trace.c=1620=static void put_bpf_raw_tp_regs(void)
--
kernel/trace/bpf_trace.c-1624-
kernel/trace/bpf_trace.c:1625:BPF_CALL_5(bpf_perf_event_output_raw_tp, struct bpf_raw_tracepoint_args *, args,
kernel/trace/bpf_trace.c-1626- struct bpf_map *, map, u64, flags, void *, data, u64, size)
--
kernel/trace/bpf_trace.c=1947=static DEFINE_MUTEX(bpf_event_mutex);
--
kernel/trace/bpf_trace.c-1950-
kernel/trace/bpf_trace.c:1951:int perf_event_attach_bpf_prog(struct perf_event *event,
kernel/trace/bpf_trace.c-1952- struct bpf_prog *prog,
--
kernel/trace/bpf_trace.c-1994-
kernel/trace/bpf_trace.c:1995:void perf_event_detach_bpf_prog(struct perf_event *event)
kernel/trace/bpf_trace.c-1996-{
--
kernel/trace/bpf_trace.c=2038=int perf_event_query_prog_array(struct perf_event *event, void __user *info)
kernel/trace/bpf_trace.c-2039-{
kernel/trace/bpf_trace.c:2040: struct perf_event_query_bpf __user *uquery = info;
kernel/trace/bpf_trace.c:2041: struct perf_event_query_bpf query = {};
kernel/trace/bpf_trace.c-2042- struct bpf_prog_array *progs;
--
tools/bpf/bpftool/link.c=406=static void
tools/bpf/bpftool/link.c:407:show_perf_event_kprobe_json(struct bpf_link_info *info, json_writer_t *wtr)
tools/bpf/bpftool/link.c-408-{
--
tools/bpf/bpftool/link.c=418=static void
tools/bpf/bpftool/link.c:419:show_perf_event_uprobe_json(struct bpf_link_info *info, json_writer_t *wtr)
tools/bpf/bpftool/link.c-420-{
--
tools/bpf/bpftool/link.c=429=static void
tools/bpf/bpftool/link.c:430:show_perf_event_tracepoint_json(struct bpf_link_info *info, json_writer_t *wtr)
tools/bpf/bpftool/link.c-431-{
--
tools/bpf/bpftool/link.c=492=static void
tools/bpf/bpftool/link.c:493:show_perf_event_event_json(struct bpf_link_info *info, json_writer_t *wtr)
tools/bpf/bpftool/link.c-494-{
--
tools/bpf/bpftool/link.c=809=static void show_uprobe_multi_plain(struct bpf_link_info *info)
--
tools/bpf/bpftool/link.c-835-
tools/bpf/bpftool/link.c:836:static void show_perf_event_kprobe_plain(struct bpf_link_info *info)
tools/bpf/bpftool/link.c-837-{
--
tools/bpf/bpftool/link.c-859-
tools/bpf/bpftool/link.c:860:static void show_perf_event_uprobe_plain(struct bpf_link_info *info)
tools/bpf/bpftool/link.c-861-{
--
tools/bpf/bpftool/link.c-878-
tools/bpf/bpftool/link.c:879:static void show_perf_event_tracepoint_plain(struct bpf_link_info *info)
tools/bpf/bpftool/link.c-880-{
--
tools/bpf/bpftool/link.c-891-
tools/bpf/bpftool/link.c:892:static void show_perf_event_event_plain(struct bpf_link_info *info)
tools/bpf/bpftool/link.c-893-{
--
tools/include/uapi/linux/bpf.h=1527=union bpf_attr {
--
tools/include/uapi/linux/bpf.h-2410- *
tools/include/uapi/linux/bpf.h:2411: * u64 bpf_perf_event_read(struct bpf_map *map, u64 flags)
tools/include/uapi/linux/bpf.h-2412- * Description
--
tools/include/uapi/linux/bpf.h-2486- *
tools/include/uapi/linux/bpf.h:2487: * long bpf_perf_event_output(void *ctx, struct bpf_map *map, u64 flags, void *data, u64 size)
tools/include/uapi/linux/bpf.h-2488- * Description
--
tools/include/uapi/linux/bpf.h-3135- *
tools/include/uapi/linux/bpf.h:3136: * long bpf_perf_event_read_value(struct bpf_map *map, u64 flags, struct bpf_perf_event_value *buf, u32 buf_size)
tools/include/uapi/linux/bpf.h-3137- * Description
--
tools/include/uapi/linux/bpf.h-3185- *
tools/include/uapi/linux/bpf.h:3186: * long bpf_perf_prog_read_value(struct bpf_perf_event_data *ctx, struct bpf_perf_event_value *buf, u32 buf_size)
tools/include/uapi/linux/bpf.h-3187- * Description
--
tools/include/uapi/linux/perf_event.h=393=struct perf_event_attr {
--
tools/include/uapi/linux/perf_event.h-556- */
tools/include/uapi/linux/perf_event.h:557:struct perf_event_query_bpf {
tools/include/uapi/linux/perf_event.h-558- /*
--
tools/include/uapi/linux/perf_event.h-585-#define PERF_EVENT_IOC_PAUSE_OUTPUT _IOW ('$', 9, __u32)
tools/include/uapi/linux/perf_event.h:586:#define PERF_EVENT_IOC_QUERY_BPF _IOWR('$', 10, struct perf_event_query_bpf *)
tools/include/uapi/linux/perf_event.h-587-#define PERF_EVENT_IOC_MODIFY_ATTRIBUTES _IOW ('$', 11, struct perf_event_attr *)
--
tools/lib/bpf/libbpf.c=11512=static void bpf_link_perf_dealloc(struct bpf_link *link)
--
tools/lib/bpf/libbpf.c-11519-
tools/lib/bpf/libbpf.c:11520:struct bpf_link *bpf_program__attach_perf_event_opts(const struct bpf_program *prog, int pfd,
tools/lib/bpf/libbpf.c-11521- const struct bpf_perf_event_opts *opts)
--
tools/lib/bpf/libbpf.c=14110=int bpf_link__update_map(struct bpf_link *link, const struct bpf_map *map)
--
tools/lib/bpf/libbpf.c-14146-
tools/lib/bpf/libbpf.c:14147:typedef enum bpf_perf_event_ret (*bpf_perf_event_print_t)(struct perf_event_header *hdr,
tools/lib/bpf/libbpf.c-14148- void *private_data);
--
tools/lib/bpf/libbpf.h=534=LIBBPF_API struct bpf_link *
tools/lib/bpf/libbpf.h:535:bpf_program__attach_perf_event_opts(const struct bpf_program *prog, int pfd,
tools/lib/bpf/libbpf.h-536- const struct bpf_perf_event_opts *opts);
--
tools/lib/perf/include/perf/bpf_perf.h-12- *
tools/lib/perf/include/perf/bpf_perf.h:13: * struct perf_event_attr_map_entry contains two __u32 IDs, bpf_link of the
tools/lib/perf/include/perf/bpf_perf.h-14- * leader prog, and the diff_map. Each perf-stat session holds a reference
--
tools/perf/builtin-record.c=2086=static int record__synthesize(struct record *rec, bool tail)
--
tools/perf/builtin-record.c-2167-
tools/perf/builtin-record.c:2168: err = perf_event__synthesize_bpf_events(session, process_synthesized_event,
tools/perf/builtin-record.c-2169- machine, opts);
--
tools/perf/builtin-record.c=2208=static void record__synthesize_final_bpf_metadata(struct record *rec __maybe_unused)
--
tools/perf/builtin-record.c-2210-#ifdef HAVE_LIBBPF_SUPPORT
tools/perf/builtin-record.c:2211: perf_event__synthesize_final_bpf_metadata(rec->session,
tools/perf/builtin-record.c-2212- process_synthesized_event);
--
tools/perf/builtin-top.c=1251=static int __cmd_top(struct perf_top *top)
--
tools/perf/builtin-top.c-1286-
tools/perf/builtin-top.c:1287: ret = perf_event__synthesize_bpf_events(top->session, perf_event__process,
tools/perf/builtin-top.c-1288- &top->session->machines.host,
--
tools/perf/util/bpf-event.c=84=int machine__process_bpf(struct machine *machine, union perf_event *event,
--
tools/perf/util/bpf-event.c-87- if (dump_trace)
tools/perf/util/bpf-event.c:88: perf_event__fprintf_bpf(event, stdout);
tools/perf/util/bpf-event.c-89-
--
tools/perf/util/bpf-event.c=500=static void synthesize_final_bpf_metadata_cb(struct bpf_prog_info_node *node,
--
tools/perf/util/bpf-event.c-522-
tools/perf/util/bpf-event.c:523:void perf_event__synthesize_final_bpf_metadata(struct perf_session *session,
tools/perf/util/bpf-event.c-524- perf_event__handler_t process)
--
tools/perf/util/bpf-event.c-546- */
tools/perf/util/bpf-event.c:547:static int perf_event__synthesize_one_bpf_prog(struct perf_session *session,
tools/perf/util/bpf-event.c-548- perf_event__handler_t process,
--
tools/perf/util/bpf-event.c=751=kallsyms_process_symbol(void *data, const char *_name,
--
tools/perf/util/bpf-event.c-782-
tools/perf/util/bpf-event.c:783:int perf_event__synthesize_bpf_events(struct perf_session *session,
tools/perf/util/bpf-event.c-784- perf_event__handler_t process,
--
tools/perf/util/bpf-event.c-822-
tools/perf/util/bpf-event.c:823: err = perf_event__synthesize_one_bpf_prog(session, process,
tools/perf/util/bpf-event.c-824- machine, fd,
--
tools/perf/util/bpf_counter.c=286=static int bpf_program_profiler__read(struct evsel *evsel)
--
tools/perf/util/bpf_counter.c-291- int num_cpu_bpf = libbpf_num_possible_cpus();
tools/perf/util/bpf_counter.c:292: struct bpf_perf_event_value values[num_cpu_bpf];
tools/perf/util/bpf_counter.c-293- struct bpf_counter *counter;
--
tools/perf/util/bpf_counter.c=693=static int bperf__read(struct evsel *evsel)
--
tools/perf/util/bpf_counter.c-696- __u32 num_cpu_bpf = cpu__max_cpu().cpu;
tools/perf/util/bpf_counter.c:697: struct bpf_perf_event_value values[num_cpu_bpf];
tools/perf/util/bpf_counter.c-698- struct perf_counts_values *counts;
--
tools/perf/util/event.c=299=int perf_event__process_ksymbol(const struct perf_tool *tool __maybe_unused,
--
tools/perf/util/event.c-306-
tools/perf/util/event.c:307:int perf_event__process_bpf(const struct perf_tool *tool __maybe_unused,
tools/perf/util/event.c-308- union perf_event *event,
--
tools/perf/util/event.c=501=size_t perf_event__fprintf_ksymbol(union perf_event *event, FILE *fp)
--
tools/perf/util/event.c-508-
tools/perf/util/event.c:509:size_t perf_event__fprintf_bpf(union perf_event *event, FILE *fp)
tools/perf/util/event.c-510-{
--
tools/perf/util/event.c-514-
tools/perf/util/event.c:515:size_t perf_event__fprintf_bpf_metadata(union perf_event *event, FILE *fp)
tools/perf/util/event.c-516-{
--
tools/perf/util/event.c=626=size_t perf_event__fprintf(union perf_event *event, struct machine *machine, FILE *fp)
--
tools/perf/util/event.c-667- case PERF_RECORD_BPF_EVENT:
tools/perf/util/event.c:668: ret += perf_event__fprintf_bpf(event, fp);
tools/perf/util/event.c-669- break;
--
tools/perf/util/event.c-676- case PERF_RECORD_BPF_METADATA:
tools/perf/util/event.c:677: ret += perf_event__fprintf_bpf_metadata(event, fp);
tools/perf/util/event.c-678- break;
--
tools/perf/util/event.h=357=int perf_event__process_ksymbol(const struct perf_tool *tool,
--
tools/perf/util/event.h-360- struct machine *machine);
tools/perf/util/event.h:361:int perf_event__process_bpf(const struct perf_tool *tool,
tools/perf/util/event.h-362- union perf_event *event,
--
tools/perf/util/event.h=391=size_t perf_event__fprintf_ksymbol(union perf_event *event, FILE *fp);
tools/perf/util/event.h:392:size_t perf_event__fprintf_bpf(union perf_event *event, FILE *fp);
tools/perf/util/event.h:393:size_t perf_event__fprintf_bpf_metadata(union perf_event *event, FILE *fp);
tools/perf/util/event.h-394-size_t perf_event__fprintf_text_poke(union perf_event *event, struct machine *machine,FILE *fp);
--
tools/perf/util/session.c=1060=static int perf_event__compressed2_swap(union perf_event *event,
--
tools/perf/util/session.c-1067-
tools/perf/util/session.c:1068:static int perf_event__bpf_metadata_swap(union perf_event *event,
tools/perf/util/session.c-1069- bool sample_id_all __maybe_unused)
--
tools/perf/util/session.c=1126=static int perf_event__ksymbol_swap(union perf_event *event,
--
tools/perf/util/session.c-1147-
tools/perf/util/session.c:1148:static int perf_event__bpf_event_swap(union perf_event *event,
tools/perf/util/session.c-1149- bool sample_id_all)
--
tools/perf/util/session.c=1170=static perf_event__swap_op perf_event__swap_ops[] = {
--
tools/perf/util/session.c-1188- [PERF_RECORD_KSYMBOL] = perf_event__ksymbol_swap,
tools/perf/util/session.c:1189: [PERF_RECORD_BPF_EVENT] = perf_event__bpf_event_swap,
tools/perf/util/session.c-1190- [PERF_RECORD_TEXT_POKE] = perf_event__text_poke_swap,
--
tools/perf/util/session.c-1209- [PERF_RECORD_COMPRESSED2] = perf_event__compressed2_swap,
tools/perf/util/session.c:1210: [PERF_RECORD_BPF_METADATA] = perf_event__bpf_metadata_swap,
tools/perf/util/session.c-1211- [PERF_RECORD_SCHEDSTAT_CPU] = perf_event__schedstat_cpu_swap,
--
tools/perf/util/synthetic-events.h=93=pid_t perf_event__synthesize_comm(const struct perf_tool *tool, union perf_event *event, pid_t pid, perf_event__handler_t process, struct machine *machine);
tools/perf/util/synthetic-events.h:94:void perf_event__synthesize_final_bpf_metadata(struct perf_session *session,
tools/perf/util/synthetic-events.h-95- perf_event__handler_t process);
--
tools/perf/util/synthetic-events.h=110=int perf_event__synthesize_auxtrace_info(struct auxtrace_record *itr, const struct perf_tool *tool,
--
tools/perf/util/synthetic-events.h-113-#ifdef HAVE_LIBBPF_SUPPORT
tools/perf/util/synthetic-events.h:114:int perf_event__synthesize_bpf_events(struct perf_session *session, perf_event__handler_t process,
tools/perf/util/synthetic-events.h-115- struct machine *machine, struct record_opts *opts);
tools/perf/util/synthetic-events.h-116-#else // HAVE_LIBBPF_SUPPORT
tools/perf/util/synthetic-events.h:117:static inline int perf_event__synthesize_bpf_events(struct perf_session *session __maybe_unused,
tools/perf/util/synthetic-events.h-118- perf_event__handler_t process __maybe_unused,
--
tools/perf/util/tool.c=288=static int perf_session__process_compressed_event_stub(const struct perf_tool *tool __maybe_unused,
--
tools/perf/util/tool.c-297-
tools/perf/util/tool.c:298:static int perf_event__process_bpf_metadata_stub(const struct perf_tool *tool __maybe_unused,
tools/perf/util/tool.c-299- struct perf_session *perf_session __maybe_unused,
--
tools/perf/util/tool.c-302- if (dump_trace)
]
|
| 29/2 |
2026/07/26 03:38 |
llm |
expert |
0m
Model:
Tokens:
input: 50725
output: 44
thoughts: 36
Thoughts: Okay, let's dive into this. I'm going to grab `perf_event_detach_bpf_prog` in `kernel/trace/bpf_trace.c` and start reading. I want to see what's happening here and how this function works. It should be relatively straightforward, but I want to get a good understanding of it.
|
| 30/2 |
2026/07/26 03:38 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:kernel/trace/bpf_trace.c Name:perf_event_detach_bpf_prog]
Results: map[SourceCode:1995: void perf_event_detach_bpf_prog(struct perf_event *event)
1996: {
1997: struct bpf_prog_array *old_array;
1998: struct bpf_prog_array *new_array;
1999: struct bpf_prog *prog = NULL;
2000: int ret;
2001:
2002: mutex_lock(&bpf_event_mutex);
2003:
2004: if (!event->prog)
2005: goto unlock;
2006:
2007: old_array = bpf_event_rcu_dereference(event->tp_event->prog_array);
2008: if (!old_array)
2009: goto put;
2010:
2011: ret = bpf_prog_array_copy(old_array, event->prog, NULL, 0, &new_array);
2012: if (ret < 0) {
2013: bpf_prog_array_delete_safe(old_array, event->prog);
2014: } else {
2015: rcu_assign_pointer(event->tp_event->prog_array, new_array);
2016: bpf_prog_array_free_sleepable(old_array);
2017: }
2018:
2019: put:
2020: prog = event->prog;
2021: event->prog = NULL;
2022:
2023: unlock:
2024: mutex_unlock(&bpf_event_mutex);
2025:
2026: if (prog) {
2027: /*
2028: * It could be that the bpf_prog is not sleepable (and will be freed
2029: * via normal RCU), but is called from a point that supports sleepable
2030: * programs and uses tasks-trace-RCU.
2031: */
2032: synchronize_rcu_tasks_trace();
2033:
2034: bpf_prog_put(prog);
2035: }
2036: }
SourceFile:kernel/trace/bpf_trace.c]
|
| 31/2 |
2026/07/26 03:38 |
llm |
expert |
0m
Model:
Tokens:
input: 51518
output: 54
thoughts: 802
Thoughts: **Analyzing a Potential Race Condition in Perf Event BPF Handling**
Okay, so I've been digging into the `perf_event` and BPF interaction, and a potential race condition has jumped out at me. My initial thought was that `perf_event_detach_bpf_prog` also nulls out `event->prog`. Then I realized this function is called when a BPF program is detached, right? Specifically, when `perf_event_free_bpf_prog` is called if the event is being traced. Thinking through this, what happens if we attach a BPF program to a perf event and then we close the BPF link? It is handled correctly, with `bpf_perf_link_release` calling `perf_event_free_bpf_prog(event)` which will, indeed, set `event->prog = NULL` and decrement the program's reference count. Critically, this can happen concurrently with `__perf_event_overflow` running in interrupt context!
This is where it gets interesting. I need to carefully examine the code in `__perf_event_overflow`:
```c
if (event->prog && event->prog->type == BPF_PROG_TYPE_PERF_EVENT &&
!bpf_overflow_handler(event, data, regs))
goto out;
```
If `event->prog` is set to NULL concurrently, what can happen? Let me trace a scenario: let's say `__perf_event_overflow` is running on CPU 1, and `perf_event_detach_bpf_prog` (or `perf_event_free_bpf_handler`) is running on CPU 0.
Here's what could happen:
```
CPU 0 CPU 1 (Interrupt)
================================================================================
__perf_event_overflow()
// Plain read of event->prog
// Compiler decides to load event->prog twice:
// Load 1: check if event->prog is non-NULL
prog_ptr = event->prog; // prog_ptr is non-NULL (0xffffc9000511d000)
if (prog_ptr) { ... }
perf_event_free_bpf_handler()
event->prog = NULL;
bpf_prog_put(prog);
// prog is freed (via RCU, but CPU 1 is in interrupt and might not be in RCU read section)
// Load 2: dereference event->prog->type
type = event->prog->type; // -> NULL pointer dereference! (or Use-After-Free if event->prog was freed)
```
And now I realize the severity: this is a real risk! Because `event->prog` is a plain pointer, and the compiler *is* allowed to reload it from memory at any time because it is not marked with `READ_ONCE()`. Even if the compiler *doesn't* reload it, the memory it points to could be freed between the initial check (`event->prog`) and the dereference (`event->prog->type`). This means a NULL pointer dereference (or even a Use-After-Free if `prog` has been freed) is possible.
Now, is the memory for `event->prog` guaranteed to be alive during the execution of `__perf_event_overflow`? Let's check: `__perf_event_overflow` does *not* call `rcu_read_lock()`. Therefore, the memory is not protected. Let me investigate further, does the caller of `__perf_event_overflow` call `rcu_read_lock()`? Let me search kernel/events/core.c to see where it is called from.
|
| 32/2 |
2026/07/26 03:38 |
tool |
codesearch-find-references |
0m
Args: map[ContextFile:kernel/events/core.c IncludeSnippetLines:10 Name:perf_event_overflow SourceTreePrefix:kernel/events/]
Results: map[References:[map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:perf_swevent_hrtimer SourceFile:kernel/events/core.c SourceLine:12273 SourceSnippet:12263: event->hw.state & PERF_HES_STOPPED)
12264: return HRTIMER_NORESTART;
12265:
12266: event->pmu->read(event);
12267:
12268: perf_sample_data_init(&data, 0, event->hw.last_period);
12269: regs = get_irq_regs();
12270:
12271: if (regs && !perf_exclude_event(event, regs)) {
12272: if (!(event->attr.exclude_idle && is_idle_task(current)))
12273: if (perf_event_overflow(event, &data, regs))
12274: ret = HRTIMER_NORESTART;
12275: }
12276:
12277: period = max_t(u64, 10000, event->hw.sample_period);
12278: hrtimer_forward_now(hrtimer, ns_to_ktime(period));
12279:
12280: return ret;
12281: }
]] TruncatedOutput:false]
|
| 33/2 |
2026/07/26 03:38 |
llm |
expert |
0m
Model:
Tokens:
input: 53604
output: 20
thoughts: 95
Thoughts: **Investigating the `perf_swevent_hrtimer` Context**
Okay, so I'm looking at this `perf_swevent_hrtimer` function. It's called from a hrtimer interrupt, which is a pretty critical part of the kernel timing infrastructure. My immediate concern is concurrency. Does this function protect itself using `rcu_read_lock()`? I need to know if it's operating on data shared with other parts of the system without appropriate synchronization.
Let me just check⦠no, there's no `rcu_read_lock()` call *directly* within `perf_swevent_hrtimer`. That's not the end of the story, though. This function is just a piece of the puzzle. I need to understand all the potential interaction points.
Alright, what about other callers of `perf_event_overflow`? `perf_event_overflow` could be called from many different contexts and if `perf_swevent_hrtimer` doesn't protect itself, maybe other callers are. I need to see the entire call graph, to understand the data access patterns around `perf_event_overflow`. So let's broaden the scope. I'm going to search the entire kernel codebase for any instances of `perf_event_overflow`. That should give me a complete picture of the potential concurrency issues I'm dealing with. From there, I can analyze each callsite carefully.
|
| 34/2 |
2026/07/26 03:38 |
tool |
grepper |
0m
Args: map[Expression:perf_event_overflow\(]
Results: map[Output:arch/alpha/kernel/perf_event.c=800=static void alpha_perf_event_irq_handler(unsigned long la_ptr,
--
arch/alpha/kernel/perf_event.c-855- if (alpha_perf_event_set_period(event, hwc, idx))
arch/alpha/kernel/perf_event.c:856: perf_event_overflow(event, &data, regs);
arch/alpha/kernel/perf_event.c-857-
--
arch/arc/kernel/perf_event.c=562=static irqreturn_t arc_pmu_intr(int irq, void *dev)
--
arch/arc/kernel/perf_event.c-602- if (arc_pmu_event_set_period(event))
arch/arc/kernel/perf_event.c:603: perf_event_overflow(event, &data, regs);
arch/arc/kernel/perf_event.c-604-
--
arch/csky/kernel/perf_event.c=1103=static irqreturn_t csky_pmu_handle_irq(int irq_num, void *dev)
--
arch/csky/kernel/perf_event.c-1141-
arch/csky/kernel/perf_event.c:1142: perf_event_overflow(event, &data, regs);
arch/csky/kernel/perf_event.c-1143- }
--
arch/loongarch/kernel/perf_event.c=471=static void handle_associated_event(struct cpu_hw_events *cpuc, int idx,
--
arch/loongarch/kernel/perf_event.c-481-
arch/loongarch/kernel/perf_event.c:482: perf_event_overflow(event, data, regs);
arch/loongarch/kernel/perf_event.c-483-}
--
arch/mips/kernel/perf_event_mipsxx.c=782=static void handle_associated_event(struct cpu_hw_events *cpuc,
--
arch/mips/kernel/perf_event_mipsxx.c-793-
arch/mips/kernel/perf_event_mipsxx.c:794: perf_event_overflow(event, data, regs);
arch/mips/kernel/perf_event_mipsxx.c-795-}
--
arch/powerpc/perf/core-book3s.c=2239=static void record_and_restart(struct perf_event *event, unsigned long val,
--
arch/powerpc/perf/core-book3s.c-2348- }
arch/powerpc/perf/core-book3s.c:2349: perf_event_overflow(event, &data, regs);
arch/powerpc/perf/core-book3s.c-2350- } else if (period) {
--
arch/powerpc/perf/core-fsl-emb.c=590=static void record_and_restart(struct perf_event *event, unsigned long val,
--
arch/powerpc/perf/core-fsl-emb.c-638-
arch/powerpc/perf/core-fsl-emb.c:639: perf_event_overflow(event, &data, regs);
arch/powerpc/perf/core-fsl-emb.c-640- }
--
arch/s390/kernel/perf_cpum_cf.c=962=static int cfdiag_push_sample(struct perf_event *event,
--
arch/s390/kernel/perf_cpum_cf.c-982-
arch/s390/kernel/perf_cpum_cf.c:983: overflow = perf_event_overflow(event, &data, ®s);
arch/s390/kernel/perf_cpum_cf.c-984-
--
arch/s390/kernel/perf_cpum_sf.c=663=static unsigned long getrate(bool freq, unsigned long sample,
--
arch/s390/kernel/perf_cpum_sf.c-681- * to avoid throttling when pushing samples with
arch/s390/kernel/perf_cpum_sf.c:682: * perf_event_overflow().
arch/s390/kernel/perf_cpum_sf.c-683- */
--
arch/s390/kernel/perf_cpum_sf.c=1013=static int perf_push_sample(struct perf_event *event,
--
arch/s390/kernel/perf_cpum_sf.c-1072- goto out;
arch/s390/kernel/perf_cpum_sf.c:1073: overflow = perf_event_overflow(event, &data, ®s);
arch/s390/kernel/perf_cpum_sf.c-1074- perf_event_update_userpage(event);
--
arch/s390/kernel/perf_pai.c=617=static int pai_push_sample(size_t rawsize, struct pai_map *cpump,
--
arch/s390/kernel/perf_pai.c-647-
arch/s390/kernel/perf_pai.c:648: overflow = perf_event_overflow(event, &data, ®s);
arch/s390/kernel/perf_pai.c-649- perf_event_update_userpage(event);
--
arch/sparc/kernel/perf_event.c=1613=static int __kprobes perf_event_nmi_handler(struct notifier_block *self,
--
arch/sparc/kernel/perf_event.c-1670-
arch/sparc/kernel/perf_event.c:1671: perf_event_overflow(event, &data, regs);
arch/sparc/kernel/perf_event.c-1672- }
--
arch/x86/events/amd/core.c=956=static int amd_pmu_v2_handle_irq(struct pt_regs *regs)
--
arch/x86/events/amd/core.c-1018-
arch/x86/events/amd/core.c:1019: perf_event_overflow(event, &data, regs);
arch/x86/events/amd/core.c-1020- }
--
arch/x86/events/amd/ibs.c=1406=static int perf_ibs_handle_irq(struct perf_ibs *perf_ibs, struct pt_regs *iregs)
--
arch/x86/events/amd/ibs.c-1565-
arch/x86/events/amd/ibs.c:1566: throttle = perf_event_overflow(event, &data, ®s);
arch/x86/events/amd/ibs.c-1567-
--
arch/x86/events/core.c=1708=int x86_pmu_handle_irq(struct pt_regs *regs)
--
arch/x86/events/core.c-1751-
arch/x86/events/core.c:1752: perf_event_overflow(event, &data, regs);
arch/x86/events/core.c-1753- }
--
arch/x86/events/intel/core.c=3752=static void x86_pmu_handle_guest_pebs(struct pt_regs *regs,
--
arch/x86/events/intel/core.c-3772- perf_sample_data_init(data, 0, event->hw.last_period);
arch/x86/events/intel/core.c:3773: perf_event_overflow(event, data, regs);
arch/x86/events/intel/core.c-3774-
--
arch/x86/events/intel/core.c=3780=static int handle_pmi_common(struct pt_regs *regs, u64 status)
--
arch/x86/events/intel/core.c-3931-
arch/x86/events/intel/core.c:3932: perf_event_overflow(event, &data, regs);
arch/x86/events/intel/core.c-3933- }
--
arch/x86/events/intel/ds.c=2869=__intel_pmu_pebs_last_event(struct perf_event *event,
--
arch/x86/events/intel/ds.c-2892- */
arch/x86/events/intel/ds.c:2893: perf_event_overflow(event, data, regs);
arch/x86/events/intel/ds.c-2894- }
--
arch/x86/events/intel/knc.c=214=static int knc_pmu_handle_irq(struct pt_regs *regs)
--
arch/x86/events/intel/knc.c-257-
arch/x86/events/intel/knc.c:258: perf_event_overflow(event, &data, regs);
arch/x86/events/intel/knc.c-259- }
--
arch/x86/events/intel/p4.c=1034=static int p4_pmu_handle_irq(struct pt_regs *regs)
--
arch/x86/events/intel/p4.c-1075-
arch/x86/events/intel/p4.c:1076: perf_event_overflow(event, &data, regs);
arch/x86/events/intel/p4.c-1077- }
--
arch/x86/events/zhaoxin/core.c=356=static int zhaoxin_pmu_handle_irq(struct pt_regs *regs)
--
arch/x86/events/zhaoxin/core.c-401-
arch/x86/events/zhaoxin/core.c:402: perf_event_overflow(event, &data, regs);
arch/x86/events/zhaoxin/core.c-403- }
--
arch/xtensa/kernel/perf_event.c=207=static int xtensa_pmu_event_init(struct perf_event *event)
--
arch/xtensa/kernel/perf_event.c-245- * Starts/Stops a counter present on the PMU. The PMI handler
arch/xtensa/kernel/perf_event.c:246: * should stop the counter when perf_event_overflow() returns
arch/xtensa/kernel/perf_event.c-247- * !0. ->start() will be used to continue.
--
arch/xtensa/kernel/perf_event.c=368=irqreturn_t xtensa_pmu_irq_handler(int irq, void *dev_id)
--
arch/xtensa/kernel/perf_event.c-390- perf_sample_data_init(&data, 0, last_period);
arch/xtensa/kernel/perf_event.c:391: perf_event_overflow(event, &data, regs);
arch/xtensa/kernel/perf_event.c-392- }
--
drivers/perf/apple_m1_cpu_pmu.c=444=static irqreturn_t m1_pmu_handle_irq(struct arm_pmu *cpu_pmu)
--
drivers/perf/apple_m1_cpu_pmu.c-476-
drivers/perf/apple_m1_cpu_pmu.c:477: perf_event_overflow(event, &data, regs);
drivers/perf/apple_m1_cpu_pmu.c-478- }
--
drivers/perf/arm_pmuv3.c=881=static irqreturn_t armv8pmu_handle_irq(struct arm_pmu *cpu_pmu)
--
drivers/perf/arm_pmuv3.c-938- */
drivers/perf/arm_pmuv3.c:939: perf_event_overflow(event, &data, regs);
drivers/perf/arm_pmuv3.c-940- }
--
drivers/perf/arm_v6_pmu.c=238=armv6pmu_handle_irq(struct arm_pmu *cpu_pmu)
--
drivers/perf/arm_v6_pmu.c-278-
drivers/perf/arm_v6_pmu.c:279: perf_event_overflow(event, &data, regs);
drivers/perf/arm_v6_pmu.c-280- }
--
drivers/perf/arm_v7_pmu.c=888=static irqreturn_t armv7pmu_handle_irq(struct arm_pmu *cpu_pmu)
--
drivers/perf/arm_v7_pmu.c-932-
drivers/perf/arm_v7_pmu.c:933: perf_event_overflow(event, &data, regs);
drivers/perf/arm_v7_pmu.c-934- }
--
drivers/perf/arm_xscale_pmu.c=145=xscale1pmu_handle_irq(struct arm_pmu *cpu_pmu)
--
drivers/perf/arm_xscale_pmu.c-188-
drivers/perf/arm_xscale_pmu.c:189: perf_event_overflow(event, &data, regs);
drivers/perf/arm_xscale_pmu.c-190- }
--
drivers/perf/arm_xscale_pmu.c=483=xscale2pmu_handle_irq(struct arm_pmu *cpu_pmu)
--
drivers/perf/arm_xscale_pmu.c-520-
drivers/perf/arm_xscale_pmu.c:521: perf_event_overflow(event, &data, regs);
drivers/perf/arm_xscale_pmu.c-522- }
--
drivers/perf/riscv_pmu_sbi.c=1040=static irqreturn_t pmu_sbi_ovf_handler(int irq, void *dev)
--
drivers/perf/riscv_pmu_sbi.c-1134- */
drivers/perf/riscv_pmu_sbi.c:1135: perf_event_overflow(event, &data, regs);
drivers/perf/riscv_pmu_sbi.c-1136- }
--
include/linux/perf_event.h=147=struct hw_perf_event {
--
include/linux/perf_event.h-265- /*
include/linux/perf_event.h:266: * State for throttling the event, see __perf_event_overflow() and
include/linux/perf_event.h-267- * perf_adjust_freq_unthr_context().
--
include/linux/perf_event.h-272- /*
include/linux/perf_event.h:273: * State for freq target events, see __perf_event_overflow() and
include/linux/perf_event.h-274- * perf_adjust_freq_unthr_context().
--
include/linux/perf_event.h=330=struct pmu {
--
include/linux/perf_event.h-437- *
include/linux/perf_event.h:438: * The PMI handler should stop the counter when perf_event_overflow()
include/linux/perf_event.h-439- * returns !0. ->start() will be used to continue.
--
include/linux/perf_event.h=1494=extern void perf_prepare_header(struct perf_event_header *header,
--
include/linux/perf_event.h-1498-
include/linux/perf_event.h:1499:extern int perf_event_overflow(struct perf_event *event,
include/linux/perf_event.h-1500- struct perf_sample_data *data,
--
kernel/events/core.c=10741=static inline void perf_event_free_bpf_handler(struct perf_event *event)
--
kernel/events/core.c-10749-
kernel/events/core.c:10750:static int __perf_event_overflow(struct perf_event *event,
kernel/events/core.c-10751- int throttle, struct perf_sample_data *data,
--
kernel/events/core.c-10841-
kernel/events/core.c:10842:int perf_event_overflow(struct perf_event *event,
kernel/events/core.c-10843- struct perf_sample_data *data,
--
kernel/events/core.c-10852-
kernel/events/core.c:10853: return __perf_event_overflow(event, 1, data, regs);
kernel/events/core.c-10854-}
--
kernel/events/core.c=10897=static void perf_swevent_overflow(struct perf_event *event, u64 overflow,
--
kernel/events/core.c-10910- for (; overflow; overflow--) {
kernel/events/core.c:10911: if (__perf_event_overflow(event, throttle,
kernel/events/core.c-10912- data, regs)) {
--
kernel/events/core.c=12252=static enum hrtimer_restart perf_swevent_hrtimer(struct hrtimer *hrtimer)
--
kernel/events/core.c-12272- if (!(event->attr.exclude_idle && is_idle_task(current)))
kernel/events/core.c:12273: if (perf_event_overflow(event, &data, regs))
kernel/events/core.c-12274- ret = HRTIMER_NORESTART;
]
|
| 35/2 |
2026/07/26 03:38 |
llm |
expert |
0m
Model:
Tokens:
input: 58689
output: 35
thoughts: 845
Thoughts: **Analyzing a Potential Concurrency Bug in Perf Event Overflow Handling**
Okay, so I'm looking at how `perf_event_overflow` is triggered. Right away, I see it's called from PMU interrupt handlers, like `x86_pmu_handle_irq` and `armv8pmu_handle_irq`. These are hardware interrupts, NMIs or hard IRQs, and critically, these contexts **do not** hold the RCU read lock. The alarm bells are going off immediately.
The next question: does `__perf_event_overflow` itself acquire an RCU read lock? Let's check the source. No, it doesn't. This means `event->prog` is being accessed without any locking mechanism and without even using `READ_ONCE()`. That's a textbook concurrency bug, potentially a very nasty one!
Now, how do we fix this? My first thought is to use `READ_ONCE(event->prog)` in `__perf_event_overflow`. That should address some of the issues, right? Let's look:
```c
struct bpf_prog *prog = READ_ONCE(event->prog);
if (prog && prog->type == BPF_PROG_TYPE_PERF_EVENT &&
!bpf_overflow_handler(event, data, regs))
goto out;
```
Ah, but that alone isn't enough. We need to prevent the `prog` from being freed while we're dereferencing it, specifically accessing `prog->type`. How do we guarantee the program won't be freed? RCU read lock!
So, the next thought is to wrap the critical section in `rcu_read_lock()` and `rcu_read_unlock()`. Does `rcu_read_lock()` prevent a BPF program from being freed? Yes! I remember that `bpf_prog_put` uses `call_rcu` (or `call_rcu_tasks_trace`) to release the program. If we're inside an `rcu_read_lock()` critical section, the program can't be freed until we release the lock with `rcu_read_unlock()`.
Now, is `rcu_read_lock()` safe to call in NMI/interrupt context? Yes, it's extremely lightweight and completely safe, which is great.
What about `call_rcu_tasks_trace`? Hmm, sleepable BPF programs are freed via `call_rcu_tasks_trace`, and I vaguely remember that `rcu_read_lock()` should protect against that too. Wait, sleepable BPF programs are only allowed for certain attach types (like uprobes or faultable tracepoints). But `__perf_event_overflow` is only for `BPF_PROG_TYPE_PERF_EVENT` programs.
Can `BPF_PROG_TYPE_PERF_EVENT` programs ever be sleepable? No, they are run in interrupt/NMI context, so they can never be sleepable! Therefore, they are always freed via the normal `call_rcu`, which *is* fully protected by `rcu_read_lock()`. Even if they were sleepable, `rcu_read_lock()` would still protect them, but they aren't sleepable.
So, wrapping the check in `rcu_read_lock()` and `rcu_read_unlock()` and using `READ_ONCE()` seems like the perfect, safe fix!
Let's see if this has already been addressed in kernel history. I'll search the git log for `__perf_event_overflow`, `event->prog`, or `perf_event_set_bpf_handler`.
|
| 36/2 |
2026/07/26 03:39 |
tool |
git-log |
6m
Args: map[CodeRegexp:__perf_event_overflow Count:10 Since:5 years]
Results: map[Output:c9bc1753b3cc perf: Fix __perf_event_overflow() vs perf_remove_from_context() race
]
|
| 37/2 |
2026/07/26 03:46 |
llm |
expert |
0m
Model:
Tokens:
input: 60430
output: 34
thoughts: 38
Thoughts: Okay, here's my interpretation of those thoughts, summarized in the first person, assuming the context is my area of expertise:
**Hunting Down That Pesky Overflow Bug**
Alright, I need to get to the bottom of this overflow issue. Time to put on my detective hat. I'm going to leverage the power of `git log` to zero in on potential culprits. My strategy is to sift through the commit history for anything that even *hints* at a fix or change related to the problem. Specifically, I'm thinking, "Where have we touched code that might relate to handling overflows?"
First up, I'll search for commits mentioning `event->prog`. That variable screams "program counter" or "instruction pointer" which, in my experience, often surfaces when you're dealing with execution flow and potential overflows in BPF programs, or when processing events. It's a key piece of the puzzle, so let's see what pops up.
Next, I'll broaden the net and look for `bpf_overflow_handler`. That's a pretty explicit keyword! Anything referencing that function is a red flag, a direct indication that someone was thinking about overflow handling in BPF. That's a great keyword to explore.
Finally, I'll cover my bases with `__perf_event_overflow`. This is a low-level handle, and even the internals can reveal clues. Even if the bug wasn't explicitly mentioned, this might be where a relevant piece of code was adjusted.
By combining these three search terms, I should be able to identify all commits that are even *remotely* relevant. From there, I can carefully analyze the changes introduced and hopefully pinpoint the root cause or at least narrow down the possibilities significantly. Let the digging commence!
|
| 38/2 |
2026/07/26 03:46 |
tool |
git-log |
0m
Args: map[MessageRegexps:[perf bpf overflow] Since:5 years]
Results: map[Output:2d6ea0875093 perf bpf: Reject oversized BPF metadata events that truncate header.size
981f13ae6255 perf session: Add validated swap infrastructure with null-termination checks
7f20d371fd87 selftests/bpf: bpf_cookie: Make perf_event subtest trigger reliably
c9bc1753b3cc perf: Fix __perf_event_overflow() vs perf_remove_from_context() race
23f852daa4ba bpf: Fix stackmap overflow check in __bpf_get_stackid()
a22d167ed825 perf parse-events: Fix parsing of >30kb event strings
408fcf946b2b bpf: free verifier states when they are no longer referenced
16ebb6f5b629 nfp: bpf: prevent integer overflow in nfp_bpf_event_output()
b583ef82b671 uprobes: Fix race in uprobe_free_utask
9aa0ebde0014 bpf, verifier: Improve precision of BPF_MUL
]
|
| 39/2 |
2026/07/26 03:46 |
llm |
expert |
0m
Model:
Tokens:
input: 61161
output: 39
thoughts: 30
Thoughts: Okay, here's my attempt at summarizing that thought process, keeping in mind I'm speaking to a fellow expert:
**Investigating Event Handler Logic in the Kernel**
Alright, I need to get to the bottom of this. Before I start down a rabbit hole, let's try a targeted approach. My initial inclination is to look directly at the code related to event handlers. Let's start with a quick search of the git log. The crucial bits I'm interested in would be either instances where the internal `event->prog` data structure is accessed or modified, or where the `perf_event_set_bpf_handler` function is used, or maybe both. It makes sense to search for the specific interactions within the kernel's event handling framework. I bet there's some useful context waiting there. Let's see what the git history tells us.
|
| 40/2 |
2026/07/26 03:46 |
tool |
git-log |
6m
Args: map[CodeRegexp:perf_event_set_bpf_handler Count:10 Since:5 years]
Results: map[Output:854dd99b5ddc perf/bpf: Mark perf_event_set_bpf_handler() and perf_event_free_bpf_handler() as inline too
93d3fde7fd19 perf/bpf: Change the !CONFIG_BPF_SYSCALL stubs to static inlines
4c03fe11b96b perf/bpf: Reorder bpf_overflow_handler() ahead of __perf_event_overflow()
82e6b1eee6a8 bpf: Allow to specify user-provided bpf_cookie for BPF perf links
652c1b17b85b bpf: Refactor perf_event_set_bpf_prog() to use struct bpf_prog input
]
|
| 41/2 |
2026/07/26 03:53 |
llm |
expert |
0m
Model:
Tokens:
input: 61604
output: 26
thoughts: 37
Thoughts: Okay, here's my interpretation of that:
**Diving into Revision History**
Alright, time to dig into commit `4c03fe11b96b`. I need to understand the specifics of this change. My immediate approach is to utilize `git show` - that's the tool for the job. Itβs the fastest way to get a readable dump of the commit's content, the diffs, and the associated metadata. This will give me the raw details needed to evaluate the impact of that particular change.
|
| 42/2 |
2026/07/26 03:53 |
tool |
git-show |
0m
Args:
Results: map[Output:commit 4c03fe11b96bda60610aca77002e83f37b4a2242
Author: Kyle Huey <me@kylehuey.com>
Date: Thu Apr 11 18:50:13 2024 -0700
perf/bpf: Reorder bpf_overflow_handler() ahead of __perf_event_overflow()
This will allow __perf_event_overflow() to call bpf_overflow_handler().
Signed-off-by: Kyle Huey <khuey@kylehuey.com>
Signed-off-by: Ingo Molnar <mingo@kernel.org>
Link: https://lore.kernel.org/r/20240412015019.7060-2-khuey@kylehuey.com
diff --git a/kernel/events/core.c b/kernel/events/core.c
index fd94e45a9d86..ca0a90648fe6 100644
--- a/kernel/events/core.c
+++ b/kernel/events/core.c
@@ -9563,6 +9563,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.
*/
@@ -10441,97 +10533,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()
]
|
| 43/2 |
2026/07/26 03:53 |
llm |
expert |
0m
Model:
Tokens:
input: 64100
output: 35
thoughts: 52
Thoughts: **Investigating Recent Changes to the Overflow Handlers**
Okay, let's take a closer look at what's been happening with the performance event overflow handling logic recently. My immediate thought is to dive into the commit history of either `__perf_event_overflow` or `bpf_overflow_handler`. Both are core to how we deal with those events, so any changes there are likely to be impactful.
Specifically, I want to filter the commit history. I'm going to focus the search to modifications within the past two years. This should narrow things down to more recent developments and avoid sifting through too much older, potentially irrelevant, code. This will help me quickly identify any recent regressions, enhancements, or architectural shifts in the way the system handles overflows. I'm hoping to quickly identify the relevant commits.
|
| 44/2 |
2026/07/26 03:53 |
tool |
git-log |
2m
Args: map[CodeRegexp:bpf_overflow_handler Count:10 Since:2 years]
Results: map[Output:100bff23818e perf/bpf: Don't call bpf_overflow_handler() for tracing events
]
|
| 45/2 |
2026/07/26 03:55 |
llm |
expert |
0m
Model:
Tokens:
input: 64401
output: 25
thoughts: 23
Thoughts: Okay, here's my take on those thoughts, tailored for an expert audience:
**Analyzing Commit `100bff23818e`**
Right, let's take a look at `100bff23818e`. That commit hash is jumping out at me, and I need to dive into it. I'm immediately curious about what triggered it. Was it a specific bug fix, a feature implementation, or perhaps a refactor? Knowing the context of that commit is key to understanding its impact. I'll need to assess the diff β the code changes, of course β and see what changed, line by line. I'm going to look for any potential regressions, any unexpected side effects on the existing codebase, and if the changes adhere to our design principles and coding standards. I'll also be considering whether the commit message is clear enough, whether the changes are adequately documented (if applicable), and if any testing was performed, especially unit tests and integration tests. It's time to put on my detective hat and see what secrets this commit holds.
|
| 46/2 |
2026/07/26 03:56 |
tool |
git-show |
0m
Args:
Results: map[Output:commit 100bff23818eb61751ed05d64a7df36ce9728a4d
Author: Kyle Huey <me@kylehuey.com>
Date: Tue Aug 13 15:17:27 2024 +0000
perf/bpf: Don't call bpf_overflow_handler() for tracing events
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.
Signed-off-by: Kyle Huey <khuey@kylehuey.com>
Suggested-by: Andrii Nakryiko <andrii.nakryiko@gmail.com>
Reported-by: Joe Damato <jdamato@fastly.com>
Closes: https://lore.kernel.org/lkml/ZpFfocvyF3KHaSzF@LQ3V64L9R2/
Fixes: f11f10bfa1ca ("perf/bpf: Call BPF handler directly, not through overflow machinery")
Cc: stable@vger.kernel.org
Tested-by: Joe Damato <jdamato@fastly.com> # bpftrace
Acked-by: Andrii Nakryiko <andrii@kernel.org>
Link: https://lore.kernel.org/r/20240813151727.28797-1-jdamato@fastly.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
diff --git a/kernel/events/core.c b/kernel/events/core.c
index aa3450bdc227..c973e3c11e03 100644
--- a/kernel/events/core.c
+++ b/kernel/events/core.c
@@ -9706,7 +9706,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;
/*
]
|
| 47/2 |
2026/07/26 03:56 |
llm |
expert |
0m
Model:
Tokens:
input: 65504
output: 25
thoughts: 43
Thoughts: Okay, here's my take on those thoughts, tailored for an expert audience:
**Analyzing a Kernel Performance Optimization**
Aha! My attention is immediately drawn to commit `f11f10bfa1ca` ("perf/bpf: Call BPF handler directly, not through overflow machinery"). This looks promising. The phrasing "Call BPF handler directly, not through overflow machinery" screams optimization. I need to dig into this. My gut tells me this is a potentially significant performance tweak in the BPF subsystem, perhaps eliminating unnecessary overhead in the eBPF execution path. I need to dissect the code changes to understand the rationale and the specific performance implications of bypassing the overflow mechanism. I suspect this might lead to some interesting insights regarding BPF program execution flow. Let's see what's actually changed.
|
| 48/2 |
2026/07/26 03:56 |
tool |
git-show |
0m
Args:
Results: map[Output:commit f11f10bfa1ca23b32020b2073aa13131a27978fe
Author: Kyle Huey <me@kylehuey.com>
Date: Thu Apr 11 18:50:16 2024 -0700
perf/bpf: Call BPF handler directly, not through overflow machinery
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.
Suggested-by: Namhyung Kim <namhyung@kernel.org>
Signed-off-by: Kyle Huey <khuey@kylehuey.com>
Signed-off-by: Ingo Molnar <mingo@kernel.org>
Acked-by: Song Liu <song@kernel.org>
Acked-by: Jiri Olsa <jolsa@kernel.org>
Acked-by: Andrii Nakryiko <andrii@kernel.org>
Link: https://lore.kernel.org/r/20240412015019.7060-5-khuey@kylehuey.com
diff --git a/include/linux/perf_event.h b/include/linux/perf_event.h
index 50e01db083ee..2ce2fbc02ec6 100644
--- a/include/linux/perf_event.h
+++ b/include/linux/perf_event.h
@@ -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;
@@ -1361,10 +1360,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) \
diff --git a/kernel/events/core.c b/kernel/events/core.c
index d3f3f552e193..c6a6936183d5 100644
--- a/kernel/events/core.c
+++ b/kernel/events/core.c
@@ -9564,9 +9564,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,
@@ -9587,10 +9587,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,
@@ -9626,8 +9624,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;
}
@@ -9638,15 +9634,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,
@@ -9730,7 +9726,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;
@@ -11997,13 +11994,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
}
]
|
| 49/2 |
2026/07/26 03:56 |
llm |
expert |
0m
Model:
Tokens:
input: 67546
output: 15
thoughts: 2262
Thoughts: **Data Race Analysis: `event->prog` in `__perf_event_overflow`**
Okay, so I've been digging into this and the root cause is now crystal clear. Before commit `f11f10bfa1ca`, the `bpf_overflow_handler` was directly assigned as the `event->overflow_handler`, which was safe because the code was within an `rcu_read_lock()` and used `READ_ONCE()` for reading `event->prog`. `f11f10bfa1ca` changed the game by calling `bpf_overflow_handler` directly from `__perf_event_overflow`. This introduced a lockless read of `event->prog`. Subsequent commit `100bff23818e` added a dereference of `event->prog->type`, still without the necessary synchronization.
The issue boils down to a data race: `__perf_event_overflow` reads `event->prog` and dereferences its `type` field, concurrently with either `__perf_event_set_bpf_prog` or `perf_event_free_bpf_handler` writing to `event->prog` (setting it to a new program or NULL). This is **bad**, as it can lead to:
1. **NULL Pointer Dereference:** The compiler can optimize and perform a double load. The first load may see a non-NULL pointer, and the second see NULL, leading to a crash.
2. **Use-After-Free (UAF):** Even with a single load, the lack of `rcu_read_lock()` means the BPF program can be freed concurrently by another CPU, resulting in a UAF when dereferencing `event->prog->type`.
The execution traces make the failure scenarios very apparent: one for the NULL dereference and one for the UAF.
Now, let's look at the solution. I believe we must:
1. Read `event->prog` with `READ_ONCE()` under `rcu_read_lock()`.
2. Check for non-NULL and the correct type.
3. Call `bpf_overflow_handler()` (which will handle its own `rcu_read_lock`).
4. Finally, `rcu_read_unlock()`.
I did verify that `bpf_overflow_handler` is safe to call within another `rcu_read_lock()` region as the lock is re-entrant. In `__perf_event_overflow`, we will need to wrap the `event->prog` check inside of `rcu_read_lock()` and `rcu_read_unlock()` to ensure safety. It seems that there is only one vulnerable point of access in `__perf_event_overflow` so it is a reasonably simple fix.
Also, any writes to `event->prog` must be appropriately annotated. I'm going to update `perf_event_set_bpf_handler` with `rcu_assign_pointer()`, and `perf_event_free_bpf_handler` and `perf_event_detach_bpf_prog` with `rcu_assign_pointer()` or `WRITE_ONCE()`. This ensures that all writes are properly synchronized.
This is a clean fix! We need to flag this data race as harmful.
|
| 50/2 |
2026/07/26 03:56 |
tool |
set-results |
0m
Args:
Results:
|
| 51/2 |
2026/07/26 03:56 |
llm |
expert |
0m
Model:
Tokens:
input: 70560
output: 1202
thoughts: 213
Thoughts: **Finalizing the Analysis: A Flawless Response**
Okay, perfect. I've got a clear plan now for the final response. I'll structure it meticulously, ensuring it's both informative and concise, just as requested.
First, the **Race Summary**: I'll clearly articulate the core issue: the competition between `__perf_event_overflow` and `__perf_event_set_bpf_prog` on `event->prog`. Then, I'll dive into the **Synchronization context**, pinpointing the crucial lack of mutual exclusion and why it's problematic β the interplay of interrupt contexts and task contexts interacting with the `perf_event_context` lock is key here.
The **Final Classification** will be unequivocally **HARMFUL**. There's no room for ambiguity.
Next comes the **Failure Reasoning**: I need to thoroughly explain the two distinct failure modes, ensuring I cover both the subtle NULL pointer dereference (that troublesome double load!) and the more dramatic Use-After-Free scenario. I'll provide those clear, two-column interleaving traces to truly illustrate the sequence of events.
Finally, the **Recommended Fix**: I'll offer a robust solution. I'll suggest wrapping the read in `rcu_read_lock()` and using `READ_ONCE()` to guarantee atomic visibility, while, on the write side, I'll recommend `WRITE_ONCE()` or `rcu_assign_pointer()` for proper synchronization.
I've reviewed the analysis, and it's spot on. The conclusions are precise, and the reasoning is sound. I'm ready to write the definitive response, delivering this information with the clarity and professionalism that this task demands.
|
| 52/1 |
2026/07/26 03:56 |
action |
format-explanation |
0m
Results: map[Explanation:### Race Summary
`__perf_event_overflow` vs `__perf_event_set_bpf_prog` (via
`perf_event_set_bpf_handler`) on `event->prog`.
---
### Synchronization Context
* **Thread A (Task context):** Modifies `event->prog` (attaching or detaching a
BPF program) under the `perf_event_context` lock.
* **Thread B (Interrupt/NMI context):** Executes `__perf_event_overflow`
locklessly when a performance counter overflows. It reads `event->prog` and
dereferences `event->prog->type` without holding any locks or RCU read-side
critical sections.
---
### Final Classification
**HARMFUL**
---
### Failure Reasoning
This data race is harmful and can lead to two distinct types of kernel crashes:
#### 1. NULL Pointer Dereference (via Compiler Double-Load)
Because `event->prog` is a plain pointer read without `READ_ONCE()`, the
compiler is permitted to optimize the condition `if (event->prog &&
event->prog->type == BPF_PROG_TYPE_PERF_EVENT)` by loading `event->prog` twice
from memory. If a concurrent detach or free operation sets `event->prog` to
`NULL` between the first load (the NULL check) and the second load (the
dereference), the kernel will dereference `NULL` and panic.
##### Execution Trace (Double-Load NULL Pointer Dereference):
```text
CPU 0 (Task context) CPU 1 (Interrupt context)
==================== =========================
__perf_event_overflow(event, ...)
// Compiler double-load
optimization of:
// if (event->prog &&
event->prog->type == ...)
// Load 1: Check if event->prog is
non-NULL
prog = event->prog; // prog is
0xffffc9000511d000 (non-NULL)
if (prog) { ... }
perf_event_free_bpf_handler(event)
event->prog = NULL;
bpf_prog_put(prog);
// Load 2: Dereference
event->prog->type
type = event->prog->type; //
event->prog is now NULL!
// -> NULL pointer dereference
crash!
```
#### 2. Use-After-Free (UAF)
Even if the compiler loads `event->prog` only once, `__perf_event_overflow` does
not execute within an RCU read-side critical section. Therefore, if
`event->prog` is cleared and the BPF program is put concurrently, the RCU grace
period can expire and free the program memory before `__perf_event_overflow`
finishes dereferencing `event->prog->type`.
##### Execution Trace (Use-After-Free):
```text
CPU 0 (Task context) CPU 1 (Interrupt context)
==================== =========================
__perf_event_overflow(event, ...)
// Load event->prog
prog = event->prog; // prog is
0xffffc9000511d000 (non-NULL)
perf_event_free_bpf_handler(event)
event->prog = NULL;
bpf_prog_put(prog);
// atomic64_dec_and_test(&aux->refcnt) succeeds
// Schedules bpf_prog_put_deferred()
// bpf_prog_put_deferred() runs, calls __bpf_prog_put_noref()
// __bpf_prog_put_noref() calls call_rcu(&prog->aux->rcu,
__bpf_prog_put_rcu)
// RCU grace period ends (CPU 1 is in interrupt, but not in RCU read
section)
// __bpf_prog_put_rcu() frees the prog memory
// Access prog->type
type = prog->type; // prog memory
has been freed!
// -> Use-After-Free!
```
---
### Recommended Fix
To resolve this race, the read of `event->prog` in `__perf_event_overflow` must
be wrapped in `rcu_read_lock()` / `rcu_read_unlock()` and annotated with
`READ_ONCE()`. Additionally, the write side should use `rcu_assign_pointer()` or
`WRITE_ONCE()`.
#### Read Side (`kernel/events/core.c`):
```c
struct bpf_prog *prog;
...
rcu_read_lock();
prog = READ_ONCE(event->prog);
if (prog && prog->type == BPF_PROG_TYPE_PERF_EVENT) {
if (!bpf_overflow_handler(event, data, regs)) {
rcu_read_unlock();
goto out;
}
}
rcu_read_unlock();
```
#### Write Side (`kernel/events/core.c` and `kernel/trace/bpf_trace.c`):
Use `rcu_assign_pointer(event->prog, prog)` or `rcu_assign_pointer(event->prog,
NULL)` when updating the BPF program pointer.]
|