Lesson 1 of 9 · 25m
The eBPF Programming Model Without the Hype
Build the mental model of verified kernel programs, hooks, helpers, and maps before touching tracing syntax.
The first time people hear eBPF described as "JavaScript for the kernel," they either become excited or alarmed. Both reactions are healthy. eBPF is not a scripting language loosely glued into Linux; it is a small, verified instruction set running inside one of the most privileged execution environments you own. Treat it like kernel code with a safety gate, because that is what it is.
The shape of an eBPF program
An eBPF program is compiled bytecode loaded with the bpf(2) syscall, verified, optionally JIT-compiled, and then attached to a hook:
userspace tool
|
| bpf(2): load program, create maps, attach
v
kernel verifier -> JIT -> hook fires -> program runs
|
+-> helpers, maps, eventsThe hook determines what the program can see. A kprobe sees kernel function arguments. A tracepoint sees stable kernel event fields. An XDP program sees a raw packet early in the network path. A tc classifier sees packets later, with more metadata. There is no generic "observe everything" hook.
The verifier is the first boss
Before loading, the verifier walks possible execution paths and proves constraints such as bounded loops, valid memory access, correct helper arguments, and program termination. It is conservative by design. Code that would work in C can fail because the verifier cannot prove pointer bounds or because a loop is too complex.
Typical verifier failure output looks like this:
invalid memory access
R1 type=ctx expected=fp
last_idx 7 first_idx 0
regs=1 invariant=0New practitioners read this as gibberish. Senior practitioners read it as the verifier telling you exactly which register stopped being trustworthy. Your job is to make memory lifetimes and bounds obvious to a machine that does not care how clever you felt.
Helpers and maps are the real API
eBPF programs cannot call arbitrary kernel functions. They use whitelisted helpers such as bpf_probe_read_kernel, bpf_get_current_pid_tgid, bpf_map_lookup_elem, and bpf_perf_event_output. Maps are shared key/value structures used for configuration, aggregation, and state exchange with userspace.
struct {
__uint(type, BPF_MAP_TYPE_HASH);
__uint(max_entries, 1024);
__type(key, __u32);
__type(value, __u64);
} syscall_count SEC(".maps");This is the central design pattern: kernel-side code aggregates cheaply; userspace reads results and does the expensive presentation work.
Why this matters operationally
Traditional agents copy huge amounts of data out of the kernel. eBPF lets you filter and aggregate before the copy. That difference is why a syscall counter can run continuously while a naive strace of the same workload would destroy latency. But eBPF is still privileged instrumentation: a bad probe can crash assumptions, distort scheduling, or expose sensitive data even if it cannot corrupt arbitrary memory.
Field notes
- eBPF safety is mostly memory and termination safety, not confidentiality policy. Your program can still collect secrets.
- Kernel version matters. Features, helpers, program types, and verifier behavior differ across releases.
/proc/sys/kernel/unprivileged_bpf_disabledaffects whether unprivileged users can load BPF in many distributions.- Do not cargo-cult a program type. Start with the event you need, then choose the narrowest hook that exposes it.
Was this lesson helpful?