Skip to content

Lesson 3 of 9 · 27m

Maps, Events, and Overhead: The Data Path Is the Design

Design kernel-side aggregation and userspace event flow so probes answer questions without overwhelming the host.

The most expensive eBPF bug is not a verifier rejection. It is a production probe that emits an event per packet, per syscall, or per string operation and quietly turns observability into a denial-of-service amplifier. The data path is the design. Decide what must cross into userspace before you decide how pretty the output should look.

Aggregation belongs in the kernel

If the question is "which processes open the most files," you do not need every open event in userspace. Increment a counter in a hash map and read the map periodically:

struct key_t {
    __u32 pid;
    char comm[TASK_COMM_LEN];
};
BPF_HASH(open_count, struct key_t, __u64);
int trace_open(struct pt_regs *ctx) {
    struct key_t key = {};
    key.pid = bpf_get_current_pid_tgid() >> 32;
    bpf_get_current_comm(&key.comm, sizeof(key.comm));
    open_count.increment(key);
    return 0;
}

The kernel map now contains the answer shape. Userspace renders it. This is the core performance advantage.

Ring buffers beat naive event copying

Modern kernels favor BPF_MAP_TYPE_RINGBUF for event streams. Older examples use perf buffers. Both can lose events under pressure, but ring buffers have cleaner memory behavior and ordering properties.

Loss is not a bug you eliminate by hoping. Size the buffer, measure drops, expose a drop counter, and design detection logic that degrades honestly.

Cardinality is a bill you pay in RAM

A hash keyed by process ID may be manageable. A hash keyed by process ID, filename, destination IP, user ID, container ID, and syscall number can explode. Worse, attackers can sometimes influence keys. Never let untrusted input create unbounded map cardinality.

Bound max_entries, prefill known keys when possible, and prefer coarse dimensions in kernel. Enrich in userspace where memory pressure is easier to observe and control.

Measure overhead where it matters

Probe cost is not one number. It depends on event frequency, work per event, map contention, cache effects, and whether output wakes userspace constantly. Measure CPU and latency with the probe enabled versus disabled.

Also inspect /proc/sys/kernel/perf_event_paranoid, ulimit -l / memlock behavior on older kernels, and whether the program is JIT-enabled:

sudo sysctl net.core.bpf_jit_enable
sudo bpftool prog list

Sampling and tail latency are different problems

A 1% sample can answer fleet-wide aggregate questions but miss rare exploitation. Continuous aggregation can find hot paths but hide exact sequences. Event capture can prove behavior but cannot be applied indiscriminately. Choose the evidence needed for the question; do not turn every investigation into full-fidelity capture.

Field notes

  • Every event schema needs versioned ownership. Security teams will build parsers around whatever shape you emit.
  • Include dropped-event counters in the same output stream; silent loss creates false confidence.
  • Map updates on hot paths contend across CPUs. Per-CPU maps often scale better but complicate aggregation.
  • eBPF shifts cost; it does not remove cost. Prove the host budget before enabling a probe fleet-wide.

Was this lesson helpful?

Sign in to rate