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() syscall

For 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:

SyscallPurpose
readRead data from a file descriptor
writeWrite data to a file descriptor
closeClose a file descriptor
fstatRetrieve file metadata
mmapMap memory into a process
mprotectChange memory permissions
brkAdjust the heap boundary
accessCheck file accessibility
execveExecute a program
arch_prctlManage architecture-specific process state
set_tid_addressRegister thread-exit coordination
openatOpen files
newfstatatRetrieve file metadata
set_robust_listRegister robust futex information
prlimit64Get or set resource limits
getrandomObtain random bytes
rseqRegister restartable sequences

strace reveals 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.