linux ebpf/books/learning-ebpf
Linux Kernel
The Linux kernel is the software layer between applications and hardware.
Because applications depend on the kernel, observing their interactions with it can reveal a lot about their behavior.

User Space and Kernel Space
Linux separates execution into two areas:
- User Space (unprivileged): Where normal applications run without direct access to hardware or protected kernel resources.
- Kernel Space (privileged): Where the kernel manages hardware, files, processes, memory, and networking.
Applications request kernel services through the system call (syscall) interface.
Developers rarely invoke syscalls directly. Instead, they use programming languages and standard libraries that provide higher-level abstractions.
sequenceDiagram
actor User
participant App as Application (User Space)
participant Kernel as Linux Kernel (Kernel Space)
participant HW as Hardware (Disk)
User->>App: Read hello.txt
rect rgba(100, 149, 237, 0.15)
Note over App,Kernel: SYSTEM CALL: read()
App->>Kernel: read(fd, buffer, size)
Kernel->>HW: Fetch data (if not cached)
HW-->>Kernel: File contents
Kernel-->>App: Return file data
end
App-->>User: Display file contents
Note over App,Kernel: strace observes the read() syscallFor example, when a Python program reads a file, Python cannot directly access arbitrary disk data. It relies on Linux syscalls to perform the operation.
The application requests the operation in user space, while the kernel performs the privileged work in kernel space.
Syscalls and Strace
strace lets us observe the system calls an application makes.
strace -c echo hello
This summarizes the syscalls made by echo, including their frequency, execution time, and errors.
Common syscalls include:
| Syscall | Purpose |
|---|---|
read | Read data from a file descriptor |
write | Write data to a file descriptor |
close | Close a file descriptor |
fstat | Retrieve file metadata |
mmap | Map memory into a process |
mprotect | Change memory permissions |
brk | Adjust the heap boundary |
access | Check file accessibility |
execve | Execute a program |
arch_prctl | Manage architecture-specific process state |
set_tid_address | Register thread-exit coordination |
openat | Open files |
newfstatat | Retrieve file metadata |
set_robust_list | Register robust futex information |
prlimit64 | Get or set resource limits |
getrandom | Obtain random bytes |
rseq | Register restartable sequences |
stracereveals how an application interacts with the kernel, making it useful for debugging, troubleshooting, and understanding application behavior.

Adding Features to the Kernel
The Linux kernel has a large, complex codebase and a strict review process. Adding features directly can require significant time and effort.
Kernel Modules
Linux supports loadable kernel modules, which extend kernel functionality without rebuilding the entire kernel.
Modules can be loaded or unloaded on demand, making them useful for device drivers and other kernel extensions.
However, modules still execute in kernel space with elevated privileges.
Kernel modules offer flexibility, but writing them is still full-fledged kernel programming.
eBPF provides a safer way to extend kernel functionality without writing traditional kernel modules.