DEV Community

Cover image for eBPF vs Kernel Modules: why one survives kernel updates and the other doesn't
Bhushitha Hashan
Bhushitha Hashan

Posted on

eBPF vs Kernel Modules: why one survives kernel updates and the other doesn't

When I started digging into kernel programming,"eBPF is stable, modules aren't" was the take everyone repeated. But later i came across that it's kinda right, but it skips the explanation on the actual reason. The real question is what each one is allowed to depend on.

Modules are married to one kernel

A kernel module gets compiled against the headers of one exact kernel build. So you're poking at stuff like:

  • internal structs (task_struct, sk_buff)
  • exported functions nobody ever promised to keep
  • .config options and compiler choices that change struct layout

And the kernel has no stable internal ABI, on purpose. The only thing Linux swears to keep stable is the syscall interface to userspace. Modules live inside the kernel, so they get zero promises.

Say a struct gets a new field in the middle. Your hard coded offsets are now wrong, but the module still loads fine. Then it quietly corrupts memory or panics the box. Even two kernels with the same version number can break you if their configs differ.

So a module depends on implementation details, and those can change whenever the kernel devs feel like it.

eBPF depends on a contract

An eBPF program doesn't link against random kernel symbols. It runs inside a restricted VM and can only talk to the kernel through what the kernel exposes, mainly helper functions.

Helpers live in include/uapi/linux/bpf.h. UAPI means the "don't break userspace" rule applies to them, same as syscalls. The stable surface is:

  • BPF instructions
  • program args
  • helpers and their args
  • return codes

The verifier checks your program is safe, and the JIT turns bytecode into native code. You don't care how either works internally. You only depend on the BPF ISA and the helper API, so the same program loads on different kernels.

The catch: eBPF isn't magically portable

A lot of eBPF programs, especially kprobe tracing, read kernel structs. If you read task->pid with a hard-coded offset, you've rebuilt the module problem. Different layout, broken program.

The fix is CO-RE (Compile Once, Run Everywhere) plus BTF, the type info the running kernel exposes:

  1. Clang records which field you wanted, not the raw offset
  2. At load time, libbpf checks the running kernel's BTF
  3. It patches the offsets to match that kernel's real layout
// fragile: offset baked in at compile time
 pid = *(int *)((char *)task + 1234);

// CO-RE: relocated at load time
#include <bpf/bpf_core_read.h>

struct task_struct *task = (void *)bpf_get_current_task();
pid_t pid = BPF_CORE_READ(task, pid);
Enter fullscreen mode Exit fullscreen mode

So the full version of the take: eBPF gives you a stable ISA and stable helpers, but the moment you read kernel data, you need CO-RE too. Without it, a tracing program breaks just like a module would.

eBPF can't replace everything

eBPF is great for observability, networking, security, and packet/tracing logic. You get:

  • safety: the verifier blocks bad memory access, so no crashing the kernel
  • portability: stable helpers plus CO-RE
  • easy deploy: no rebuild per kernel, just load the object via the bpf() syscall

But it has hard limits: instruction count, stack size, restricted pointer math, and only the helpers that exist. You can't write a device driver or a whole subsystem with it, or do anything that needs raw access to kernel internals. For that, you still need a module (ideally upstreamed).

Takeaway

If you can do it with helpers, maps, and CO-RE safe struct access, go eBPF. You're building on something the kernel intends to keep, not on internals it refuses to freeze.

Three rules:

  • helpers for behavior
  • CO-RE for data layout
  • never hard-code struct offsets

Top comments (0)