eBPF runs user-written programs at event points inside the kernel, without patching kernel source or loading a module. Code running inside the kernel must not hang it or corrupt its memory, so the kernel verifies each program at load time and runs only the ones that pass. To make verification possible, it also narrows what a program can do: it may call only a fixed set of kernel functions, and it keeps state only in maps, key-value stores inside the kernel.
Cilium, a Kubernetes network plugin (CNI), and many observability tools are described as “running on eBPF,” and I wanted to know what “loading a program into the kernel” actually involves. I walked through the load path and checked it hands-on with bpftrace in a colima VM.
Load Path
The steps on ebpf.io, in order:
- Compile: the kernel accepts eBPF programs as bytecode. Code close to C is compiled to bytecode with clang and LLVM
- Load: the bytecode is handed to the kernel through the
bpf()syscall - Verify: the verifier checks that the program is safe. If it fails, the load fails
- JIT compile: the bytecode is translated into machine code for the CPU architecture
- Attach: the program is attached to a hook. From then on it runs every time the kernel or an application reaches that point
Results the program collects go into maps, and user-space tools read those maps back through the same bpf() syscall. Data between the in-kernel program and user space mostly flows through maps.
bpftrace compresses this path into a one-line command. Its README says “bpftrace uses LLVM as a compiler backend, and libbpf for interacting with the Linux BPF subsystem.” It turns the script into LLVM IR, compiles it to bytecode, loads it, and prints the maps when it exits. BCC has you write the kernel side in C and the user side in Python or Lua. For one-line observations bpftrace is the quicker tool. When the kernel-side logic gets long, or results need post-processing in user space, I think writing both sides yourself with BCC or libbpf is the better fit.
Verifier
Once a kernel module is loaded, the kernel does not check what it does. eBPF checks the program at load time instead and refuses to load it if it cannot prove the program safe. Loading itself requires privileges too: according to ebpf.io, unless unprivileged eBPF is enabled, the loader needs root or CAP_BPF.
The checks described on ebpf.io and in the kernel verifier docs:
- Termination: a program must run to completion. Loops are allowed only when the verifier can confirm that the exit condition is guaranteed to become true (bounded loops)
- Uninitialized reads: a register that was never written cannot be read, and only stack slots that were written first can be read
- Out-of-bounds access: memory accesses are bounds- and alignment-checked
- NULL checks: a map lookup returns
PTR_TO_MAP_VALUE_OR_NULL, and the value can be accessed only after the program checks it is not NULL - Complexity: every execution path must be analyzable within a fixed limit
Starting from the first instruction, the verifier walks every possible path and tracks the state of registers and the stack. Paths multiply at every branch, so when the current state is contained in a state it has already checked, it stops following that path. The kernel docs call this state pruning. The termination check relies on the same path walking: termination is proven only if the verifier can follow a loop to its end, and if the analysis exceeds its limit, the program is rejected.
Maps
An eBPF program starts fresh every time its hook fires. To keep values between invocations or send results to user space, it writes to a map. According to the kernel docs, user space creates maps and looks up, updates, and deletes elements through the bpf() syscall. Creating a map returns a file descriptor, and later commands refer to the map by that fd. Programs inside the kernel reach the same map through helpers.
| Type | Use |
|---|---|
HASH · PERCPU_HASH · LRU_HASH | Key-value aggregation. PERCPU keeps a copy per CPU |
ARRAY · PERCPU_ARRAY | Fixed-size array indexed by integer |
RINGBUF | Kernel → user space event delivery |
LPM_TRIE | Longest-prefix match (IP range lookups) |
SOCKMAP · XSKMAP | Networking. Handing packets to sockets |
RINGBUF is an alternative to the perf buffer, which keeps one buffer per CPU. The ringbuf docs describe it as an MPSC (multi-producer, single-consumer) buffer shared across CPUs. The docs say this uses memory more efficiently and preserves event ordering.
Helpers and kfuncs
An eBPF program cannot call kernel functions directly. It can call only the helpers and kfuncs the kernel exposes for eBPF.
- Helpers: functions the kernel provides to eBPF, such as
bpf_map_lookup_elemandbpf_get_current_comm. ebpf.io calls them “a well-known and stable API.” The list lives in bpf-helpers(7) - kfuncs: kernel functions exposed for eBPF programs to call. The kfunc docs say: “Unlike normal BPF helpers, kfuncs do not have a stable interface and can change from one kernel release to another.” A kfunc scheduled for removal is flagged
KF_DEPRECATED, and once it is removed the verifier rejects programs that call it
Calls are verified too. The verifier checks that the argument registers match the helper’s argument constraints, and after the call it sets R0 to the helper’s return type. My understanding is that the verifier can keep tracking state across a call because helper and kfunc argument and return types are declared — and that is also why arbitrary kernel function calls are not allowed.
If the same program has to run on several kernel versions, I think the safer route is to check first whether helpers cover the need. kfuncs are for a fixed target kernel version.
Hooks
A hook is the point where a program runs. Tracepoints and kprobes are the usual choices for observability; tc and XDP sit on the network path.
- tracepoint: a static point defined in kernel code ahead of time, such as syscall entry and exit, the scheduler, and block I/O. Its arguments have a fixed format, which
bpftrace -lvshows - kprobe · kretprobe: attach dynamically to the entry or return of a kernel function. According to the kernel kprobes docs, kprobes can probe most of the kernel except itself
- tc: runs per packet in the traffic control layer
- XDP: runs in the receive path of the network card (NIC) driver
The main difference between tracepoints and kprobes is stability. The bpftrace reference guide describes static tracing as “Since these are fixed, they may be provided as part of a stable API, and documented,” and dynamic tracing as “it is not considered a stable API, and the target functions may not be documented outside of their source code.” The kernel functions kprobes attach to are not a stable API, so their names or arguments can change between kernel versions. For inlined functions, a kprobe can also miss some calls. When a tracepoint provides the same information, I would reach for the tracepoint first.
The number of available points on the VM used for these experiments:
$ sudo bpftrace -l 'tracepoint:*' | wc -l
1517
$ sudo bpftrace -l 'tracepoint:syscalls:sys_enter_*' | wc -l
311
$ sudo bpftrace -l 'kprobe:*' | wc -l
61927
Hands-on With bpftrace
The environment is a colima VM (Ubuntu 24.04, kernel 6.8.0, aarch64) with bpftrace v0.20.2 and bpftool v7.4.0 installed from apt. The VM also runs k8s components such as kubelet and containerd. The JIT is enabled.
$ cat /proc/sys/net/core/bpf_jit_enable
1
The load is dd writing 20,000 blocks of 4 KB to a file and reading them back. The first exercise also loops ls and cat 200 times.
$ dd if=/dev/zero of=/tmp/ebpf-dd bs=4k count=20000
$ dd if=/tmp/ebpf-dd of=/dev/null bs=4k
Counting syscalls
The raw_syscalls:sys_enter tracepoint fires on every syscall entry. The script counts them keyed by process name (comm).
$ sudo bpftrace -e 'tracepoint:raw_syscalls:sys_enter { @[comm] = count(); }
interval:s:5 { exit(); }'
...
@[containerd]: 9370
@[bash]: 9748
@[compile]: 14375
@[cat]: 17131
@[containerd-shim]: 17253
@[ls]: 22332
@[kubelet]: 39721
@[dd]: 80227
Filtering to dd and splitting by syscall shows that read and write dominate. probe is the full name of the probe that fired. Writing and reading 20,000 blocks comes to about 40,000 each of read and write.
$ sudo bpftrace -e 'tracepoint:syscalls:sys_enter_* /comm == "dd"/ { @[probe] = count(); }
interval:s:15 { exit(); }'
@[tracepoint:syscalls:sys_enter_munmap]: 6
@[tracepoint:syscalls:sys_enter_brk]: 6
@[tracepoint:syscalls:sys_enter_mprotect]: 8
@[tracepoint:syscalls:sys_enter_newfstat]: 32
@[tracepoint:syscalls:sys_enter_mmap]: 38
@[tracepoint:syscalls:sys_enter_close]: 42
@[tracepoint:syscalls:sys_enter_openat]: 64
@[tracepoint:syscalls:sys_enter_write]: 40000
@[tracepoint:syscalls:sys_enter_read]: 40003
Latency histogram
A kprobe records the entry time of vfs_read in a map keyed by thread id (tid), and a kretprobe feeds the difference to hist(), which counts values in power-of-two buckets.
$ sudo bpftrace -e 'kprobe:vfs_read /comm == "dd"/ { @start[tid] = nsecs; }
kretprobe:vfs_read /@start[tid]/ { @ns = hist(nsecs - @start[tid]); delete(@start[tid]); }
interval:s:5 { exit(); }
END { clear(@start); }'
Attaching 4 probes...
@ns:
[128, 256) 3 | |
[256, 512) 7025 |@@@@@@@@@@@@@@@@@@@@@@@@@ |
[512, 1K) 14443 |@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@|
[1K, 2K) 12284 |@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ |
[2K, 4K) 3968 |@@@@@@@@@@@@@@ |
[4K, 8K) 1445 |@@@@@ |
[8K, 16K) 341 |@ |
[16K, 32K) 229 | |
[32K, 64K) 129 | |
[64K, 128K) 67 | |
[128K, 256K) 48 | |
[256K, 512K) 12 | |
[512K, 1M) 7 | |
[1M, 2M) 1 | |
[2M, 4M) 0 | |
[4M, 8M) 0 | |
[8M, 16M) 0 | |
[16M, 32M) 1 | |
Of the 40,000 reads, 84% finished between 256 ns and 2 µs. 136 took longer than 64 µs, and one of those took over 16 ms. The histogram shows a tail that a single average would hide. The @start map carries the entry time to the return probe. The program starts fresh on every call, so state survives only in maps.
Inspecting the loaded program
While bpftrace is running, bpftool shows the programs and maps loaded into the kernel. I left a script running that counts openat calls per process.
$ sudo bpftool prog list
...
2796: tracepoint name tracepoint_syscalls_sys_enter_openat tag afe1b01453072a6e gpl
loaded_at 2026-11-16T20:53:14+0900 uid 0
xlated 232B jited 280B memlock 4096B map_ids 703
btf_id 420
xlated is the bytecode size and jited is the size of the arm64 machine code from JIT compilation. The list also includes programs the VM agent loaded earlier, such as lima_ticker. The bytecode shows the helper call and the NULL-check branch.
$ sudo bpftool prog dump xlated id 2796
int64 tracepoint_syscalls_sys_enter_openat(int8 * var1):
...
7: (85) call bpf_get_current_comm#279472
8: (18) r1 = map[id:703]
10: (bf) r2 = r6
11: (85) call htab_percpu_map_lookup_elem#342680
12: (15) if r0 == 0x0 goto pc+4
13: (79) r1 = *(u64 *)(r0 +0)
14: (07) r1 += 1
15: (7b) *(u64 *)(r0 +0) = r1
16: (05) goto pc+10
17: (b7) r1 = 1
18: (7b) *(u64 *)(r10 -8) = r1
19: (18) r1 = map[id:703]
21: (bf) r2 = r10
22: (07) r2 += -24
23: (bf) r3 = r10
24: (07) r3 += -8
25: (b7) r4 = 1
26: (85) call htab_percpu_map_update_elem#352224
27: (b7) r0 = 1
28: (95) exit
- Instruction 7 reads the process name through a helper
- Instruction 11 looks up the map, and 12 jumps to 17 when the result is NULL. Otherwise instructions 13 through 15 increment the value
- On NULL, instruction 17 writes the value 1 to the stack and instruction 26 inserts it as a new element
The map that map_ids 703 points to is the @ variable.
$ sudo bpftool map list
...
703: percpu_hash name AT_ flags 0x0
key 16B value 8B max_entries 4096 memlock 623808B
704: ringbuf name ringbuf flags 0x0
key 0B value 0B max_entries 262144 memlock 275864B
bpftrace turned @[comm] = count() into a PERCPU_HASH map. The 16-byte key is the size of comm. Judging by its name, the ringbuf created alongside it carries output such as printf to user space.
Verifier rejection
A loop with a constant bound passes.
$ sudo bpftrace -e 'BEGIN { $i = 0; while ($i < 10) { $i++; } printf("i=%d\n", $i); exit(); }'
i=10
My first attempt was a loop that never ends, while ($i >= 0) { $i++; }, and it was not rejected. The verifier log from -v showed only 24 processed instructions. It looks like LLVM removed the loop at compile time because it had no effect. So I made the bound a runtime value (nsecs) and updated a map inside the loop so LLVM could not remove it.
$ sudo bpftrace -e 'BEGIN { $i = 0; $n = (int64)nsecs;
while ($i < $n) { @calls = count(); $i++; } printf("i=%d\n", $i); exit(); }'
Attaching 1 probe...
ERROR: Error loading program: BEGIN (try -v)
Running it again with -v prints the verifier log. The log ran past ten thousand lines, and bpftrace also reported that its log buffer was too small. The tail:
25: (85) call bpf_map_lookup_elem#1 ; R0_w=map_value_or_null(id=8194,map=AT_calls,ks=4,vs=8)
26: (15) if r0 == 0x0 goto pc-19 ; R0_w=map_value(map=AT_calls,ks=4,vs=8)
27: (79) r1 = *(u64 *)(r0 +0) ; R0_w=map_value(map=AT_calls,ks=4,vs=8) R1_w=scalar()
28: (07) r1 += 1 ; R1_w=scalar()
29: (7b) *(u64 *)(r0 +0) = r1 ; R0_w=map_value(map=AT_calls,ks=4,vs=8) R1_w=scalar()
30: (07) r6 += 1 ; R6_w=8191
31: (6d) if r8 s> r6 goto pc-12
The sequence of 8193 jumps is too complex.
processed 98378 insns (limit 1000000) max_states_per_insn 4 total_states 1237 peak_states 1237 mark_read 3
- Tracking the NULL check: right after the lookup at instruction 25, R0 is
map_value_or_null; past the branch at instruction 26 it becomesmap_value. Only after that branch can the value be read - Walking each iteration: R6, which holds the counter
$i, is tracked as the concrete value8191. With the bound$n(R8) unknown, the verifier appears to have treated every iteration as a new path and kept following it - Rejected at the complexity limit: after 8,193 consecutive jumps it rejected the program as “too complex,” before reaching the instruction limit of 1,000,000
The verifier could not prove that this loop ends. It followed the iterations one by one, stopped at its analysis limit, and rejected the program without ever running it. To get the same logic loaded, the loop needs a constant bound or fewer iterations so that the verifier can analyze it.
Summary
An eBPF program enters the kernel as bytecode and has to pass the verifier before it is JIT-compiled and attached to a hook. The verifier is what guarantees safety. Calling only helpers and kfuncs and keeping state only in maps are the conditions that let the verifier follow every path. A bpftrace one-liner follows the same path: @ variables become maps, a lookup is followed by a NULL branch, and a loop that cannot be analyzed to its end is rejected at load time.
References
- TCP and UDP — the TCP/IP stack and packet flow where tc and XDP sit