All writings · tech · eBPF

Introduction to eBPF

What is eBPF

Taken from the book Learning eBPF, by Liz Rice, “eBPF allows developers to write custom code that can be loaded into the kernel dynamically, changing the way the kernel behaves.”

To fully grasp this concept, it's essential to first understand a few key elements. Let's break them down one by one.

Kernel Space vs User Space

The Linux kernel is the core part of the Linux operating system. It acts like a bridge between the hardware (like your computer's memory, processor, and devices) and the software (your apps and programs). It manages resources and ensures different tasks work smoothly without interfering with each other.

Applications run in an unprivileged layer called the user space, which is not allowed to access hardware directly. Instead these applications make requests to the kernel using the system call interface, for it to act on the application’s behalf.

Kernel Space vs User Space

Let’s take an example where you double click on a file to open it.

Kernel Space has Superpowers

Since the kernel operates with the highest privileges and has complete control over system resources, it's an ideal place for tasks like:

By running programs in the kernel space, we have access to system wide observability without the limitations of user-space applications.

Programs in Kernel Space

There are different ways you can run custom logic in kernel space depending upon the use cases and how deeply you want the program to integrate with the kernel. Some of the ways are listed below with very brief introductions.

1. Kernel Modules (Loadable Kernel Modules or LKMs)

ProsCons
Loadable and unloadable withoutRequires kernel knowledge, can cause
needing to reboot the system, well-crashes, needs maintenance for
supported, ideal for device drivers.version compatibility.

2. Modifying the Kernel Source (Adding a Program via a Kernel Patch)

ProsCons
Full control, ideal for deepRequires kernel recompilation, can
integrations, can be mergedbe unstable, maintenance-heavy,
upstream.hard to distribute, time consuming,
patch might not get approved.

3. Using Kernel Hooks

ProsCons
Clean extension points, supportedLimited by hook availability,
in specific subsystems, tested,limited configuration, misuse can
more safe.cause instability.

4. System Tap and DTrace

ProsCons
Easy to trace kernel events, minimalLimited to monitoring/tracing,
performance impact, no kernelrequires elevated permissions, not for
modification needed.custom Kernel logic, and some
platform limitations.

5. eBPF (Extended Berkeley Packet Filter)

Focus on eBPF

eBPF is a standout option because it allows running custom logic in kernel space safely and efficiently, without needing to modify the kernel or load heavy modules. It has grown to support not only network tracing but also system performance monitoring, security filtering, and even application tracing—making it one of the most versatile tools for kernel-level operations today. One of the biggest features include the ability to write code in high level languages like Go, Python etc. We will see that in the upcoming articles.

Connecting the dots

Now, if we go back to the first statement in the article, we understand that with eBPF we can write code in high level languages and run it in the privileged Kernel space, in a safe sandbox that ensures that the code you wrote doesn’t break the kernel.

This is the introductory article about what you need to know to fully appreciate what eBPF offers and how wide it’s use cases are, in the next articles, we will dive deeper and start writing eBPF code that runs.

In the meantime, if you want more examples and are a visual learner, go through the following resources:


Originally published at prateeksingh.life/writings/tech/ebpf/introduction-to-ebpf. Save to Instapaper.