eBPF

Also known as: extended Berkeley Packet Filter

On this page 7

eBPF (extended Berkeley Packet Filter) is a Linux kernel technology that runs small, verified programs inside the kernel in response to events such as system calls, function entries, network packets and scheduler activity. In Linux monitoring, it gives administrators and engineers deep observability with low overhead and without loading custom kernel modules or rebooting the system.

The original Berkeley Packet Filter was a narrow tool for filtering packets in programs such as tcpdump. The extended version, merged into Linux from kernel 3.18 onward and greatly expanded since, turned it into a general-purpose in-kernel virtual machine. Today eBPF underpins tracing tools such as bpftrace, parts of perf, container networking stacks and runtime security agents.

How eBPF programs run inside the kernel

An eBPF program follows a load, verify, compile and attach life cycle. A user-space tool compiles restricted C or a higher-level script into eBPF bytecode, loads it through the bpf() system call, and attaches it to a hook. When the hooked event occurs, the kernel executes the program and the program records or acts on what it observes.

Safety comes from the in-kernel verifier. Before any program runs, the verifier walks every possible execution path and rejects code that could read invalid memory, loop without bound or exceed the instruction complexity limit of one million verified instructions on modern kernels. Accepted bytecode is then translated by a just-in-time (JIT) compiler into native machine code, so it runs at nearly the speed of compiled kernel code.

Programs exchange data with user space through eBPF maps, which are kernel-resident key-value structures. A monitoring tool typically aggregates counts or histograms in a map inside the kernel and reads only the summary, which avoids copying every event across the kernel and user-space boundary.

The table below summarizes the main building blocks and the role each plays.

ComponentRoleWhy it matters
BytecodePortable instruction set of 11 registersSame program runs on many architectures
VerifierStatic analysis before loadingPrevents crashes and unsafe memory access
JIT compilerConverts bytecode to native codeKeeps per-event cost very low
MapsShared hash, array, ring buffer and other storesMoves aggregated data to user space
Helpers and kfuncsApproved kernel functions callable from programsLimits what a program may touch
BTF and CO-REType information and relocationLets one binary run on many kernel versions
Core components of the eBPF runtime

Hook points used for Linux monitoring

The value of eBPF in monitoring depends on where a program attaches. Each hook type exposes a different layer of the system, and the choice affects both stability across kernel versions and the amount of overhead.

Hook typeWhat it observesStability across kernels
TracepointsStatic, documented kernel eventsHigh, defined as a stable interface
kprobes and kretprobesEntry and return of almost any kernel functionLow, function names and arguments change
fentry and fexitKernel functions via BPF trampolinesMedium, faster than kprobes
uprobesFunctions in user-space binaries and librariesDepends on the application build
USDT probesStatic markers compiled into applicationsHigh when the application maintains them
perf eventsCPU sampling and hardware countersHigh
XDP and tcNetwork packets at the driver or traffic-control layerHigh
Common eBPF hook types and their monitoring uses

Experienced practitioners prefer tracepoints for long-lived dashboards, because a tracepoint keeps its meaning across releases, and reserve kprobes for ad hoc investigation, where flexibility outweighs the risk that the probed function is renamed or inlined in the next kernel.

eBPF compared with classic monitoring tools

Classic Linux tools such as top, vmstat and iostat read periodic counters from procfs, while strace intercepts system calls through ptrace. eBPF differs because it observes individual events in the kernel and summarizes them in place, which reveals latency distributions and causes that counters cannot show.

ApproachData sourceTypical overheadBest use
procfs-based toolsKernel counters sampled at intervalsNegligibleQuick health overview
straceptrace stops on every system callHigh, often several times slowerDebugging one process
perfSampling, counters and tracepointsLow to moderateCPU profiling
eBPF via bpftrace or BCCProgrammable in-kernel probesLow with in-kernel aggregationLatency and causal analysis
Kernel modulesArbitrary kernel codeLow, but unsafeLegacy or special drivers
eBPF and related monitoring approaches

In practice eBPF complements rather than replaces these tools. A quick look at uptime or vmstat shows that something is wrong, and an eBPF tool such as biolatency or runqlat explains where the time is going.

Tooling and practical deployment

Most administrators never write raw bytecode. They use front ends that generate eBPF programs: bpftrace for one-line scripts, the BCC collection of ready-made tools, and libbpf-based binaries that ship as compiled, portable programs. Observability agents, container network plugins and runtime security products also embed eBPF.

  • Kernel version matters: a kernel of 5.8 or later with BTF enabled supports most modern features, including ring buffers and CO-RE.
  • Privileges are required: loading tracing programs normally needs CAP_BPF together with CAP_PERFMON, or full root.
  • Unprivileged eBPF is disabled by default on most distributions through the kernel.unprivileged_bpf_disabled setting.
  • Pinned programs and maps in the BPF file system survive the exit of the loading process.

Limitations, risks and common mistakes

eBPF is safe by design but not free of cost or risk. A probe on a function that fires millions of times per second adds measurable CPU load, and the verifier guarantees safety, not correctness. The following table lists frequent problems and their remedies.

ProblemCauseRemedy
Noticeable CPU overheadProbing a very hot function or sending every event to user spaceAggregate in maps and sample where possible
Program fails to loadVerifier rejects complex loops or missing bounds checksAdd explicit bounds and simplify logic
Tool breaks after upgradekprobe target renamed or inlinedSwitch to tracepoints or CO-RE builds
Lost eventsPerf or ring buffer too smallEnlarge the buffer and reduce event volume
Security exposureOverly broad privileges for eBPF loadersRestrict capabilities and audit loaded programs
Typical eBPF pitfalls in monitoring

A further concern is that attackers can also use eBPF to hide activity or read sensitive data once they hold root privileges. Security teams therefore inventory loaded programs with bpftool prog list and treat unexpected eBPF programs as an indicator of compromise.

In everyday language

The Linux kernel is the core of the system. It handles every disk read, network packet and program start. Normally it is hard to see inside it. eBPF lets you place tiny, checked sensors at chosen spots in the kernel.

Each sensor is a small program. Before it runs, the kernel inspects it for safety. If it could crash the system, it is refused. Once accepted, it counts or measures events very quickly. It then reports only a short summary. It works like fitting a traffic counter at one junction instead of filming the whole city.

Seen in the real world

An operations engineer runs a PostgreSQL database on a Linux server where query times randomly jump from 5 ms to 200 ms. The tools top and iostat show average disk usage of only 30 percent, so the cause is unclear. The engineer runs the eBPF tool biolatency for 60 seconds and sees that most disk requests finish in under 1 ms, but a small tail exceeds 150 ms.

A second eBPF tool, runqlat, shows normal scheduler delay, which rules out CPU contention. Tracing block requests per process then reveals that a nightly backup job issues large synchronous writes at the same moments. After the backup is moved to a different volume and given a lower I/O priority, the latency spikes disappear. The whole investigation runs on the production server with less than 2 percent extra CPU load and no restart.