Skip to main content

Linux Kernel Architecture: From User Space to Hardware

Explore Linux kernel architecture: user and kernel space, cooperating subsystems, cached and uncached file reads, and how containers share one kernel.

One machine, many programs

A browser, a terminal and a database share the same CPUs, memory and devices. Each needs useful access to that hardware without being able to overwrite another program's memory or take control of the machine.

The kernel supplies that boundary. It schedules tasks, manages memory, exposes files and sockets, and coordinates devices. An application runs its own instructions directly on the CPU, then requests kernel services when it needs them.

Consider ls. The shell starts the ls program; ls asks the kernel for directory entries and metadata, then formats the results and writes its output. Some of that work stays in user space. Some enters the kernel. And cached information may make a storage request unnecessary. Understanding those distinctions is more useful than imagining every operation descending through the same stack.

How the pieces fit

Linux is organized into cooperating subsystems, not a single pipeline. Select a part of the map to see its responsibility and one place it appears in an ordinary program.

A map of the kernel

Kernel space · shared privilege

These are cooperating subsystems. A request uses the parts it needs, rather than passing through every box.

The boundary around kernel space matters more than the order of the boxes. A regular-file read involves VFS and memory management; a TCP send involves sockets and the network stack. Both rely on scheduling and may reach a device driver.

User space and kernel space

User space is where applications and their libraries execute with restricted privileges. Kernel space contains the privileged code that manages the machine. On ordinary x86-64 Linux, these correspond to ring 3 and ring 0. Other CPU architectures express privilege differently; the ring numbers are not a universal OS interface.

A system call is a controlled request to run a kernel handler. It does not give the application arbitrary kernel privileges. The processor and entry code establish the required execution state, the kernel checks the request, and control eventually returns to the caller. Linux's entry documentation also describes interrupts and exceptions: syscalls are not the only way execution enters the kernel.

Switch the execution mode below. The calling thread stays the same even though the instructions it executes have different privileges.

One thread, two privilege levels
Executing in

The dot follows execution, not a new process. Entering the kernel is a mode switch; scheduling another task is a context switch.

A mode switch changes privilege. A context switch changes the running task. A syscall may finish without scheduling another task; a blocking read may put its caller to sleep and let another task run. This distinction explains why a syscall count alone does not tell you how many context switches occurred.

What each subsystem owns

Processes and scheduling

The kernel tracks tasks through structures such as task_struct, including their state and links to resources. The scheduler selects runnable tasks for CPU time. A task waiting for I/O is not competing for the CPU in the same way as a runnable one.

Scheduling policy matters: ordinary workloads, real-time tasks and deadline tasks have different requirements. Modern fair scheduling uses EEVDF; there is no single fixed timeslice or universal nice ratio that describes every workload. Continue with process management for task creation, scheduling and signals.

Memory management

Each process has a virtual address space, with mappings and permissions enforced through page tables and the CPU's memory-management hardware. Two processes can use the same virtual address for different physical pages, or deliberately share a mapping.

A page fault does not automatically mean a crash. The kernel may supply a page on demand or resolve copy-on-write. An invalid access that cannot be resolved can result in SIGSEGV. Reclaim, file-backed pages and swap help manage memory pressure, but overcommit does not guarantee that every allocation can eventually be satisfied. Memory management follows those mechanisms in detail.

Filesystems and VFS

The Virtual File System gives applications a common interface while individual filesystems supply the underlying operations. Four useful objects are:

  • Superblock: the mounted filesystem's state.
  • Inode: an object's metadata and filesystem-specific state.
  • Dentry: a cached relationship between a name and an inode.
  • Open file: the state of an open description, including its current offset and operations.

Opening a path resolves names and obtains an open file. Reading an already-open descriptor uses that file; it does not repeat the whole pathname walk. A read from ext4 and a read from /proc/cpuinfo share an API, but one accesses stored file data and the other can generate its contents. See the VFS documentation and filesystems overview.

Networking and drivers

The network stack connects sockets to transport protocols such as TCP and UDP, IP routing, packet filtering and network devices. TCP manages connection state, reliability and congestion control. Netfilter supplies hooks for facilities such as filtering and NAT. The networking stack page traces that path.

Drivers connect these generic interfaces to specific hardware. Block drivers serve storage, network drivers exchange packets, and character-device interfaces expose facilities such as terminals. A driver can be built into the kernel or loaded as a module. That changes how code is installed, not its privilege level.

Follow a file read

Assume a local regular file is already open as descriptor 3. The program calls read(3, buffer, 4096), and its buffer is resident in memory. The key decision is whether the requested file data is already in the page cache.

Choose Cached or Not cached, then step through the request. A cache hit bypasses the storage path; a miss adds I/O and a wait before the caller can continue.

Trace a buffered read
File data
Step 1 / 6

Ask for bytes

The C library prepares read(3, buffer, 4096). The descriptor already refers to an open file; pathname lookup happened earlier.

Application

User space

Syscall entry

Kernel space

VFS / filesystem

Open file operations

Page cache

File data in RAM

Block layer / driver

Bypassed on this cache hit

Copy to user

User buffer

Code domain
User
Calling task
Running
read() result
Pending

Authored sequence for one successful local-file read, with a resident user buffer. It omits readahead, errors and direct I/O. Step duration is for explanation, not measured latency.

The page cache belongs to the file-data and memory-management path. It is consulted before a buffered read needs block-device I/O, not after the request has already reached the disk driver. Normal file reads use it; direct I/O is a separate case. See the kernel's page-cache documentation.

The example returns all 4096 bytes so the handoff is easy to follow. The actual read() contract permits a shorter successful read, zero at end of file, or an error. Correct callers use the returned count.

Batching can reduce syscall overhead: 1000 one-byte read() calls make more requests than one 1000-byte call. That is a request-count comparison, not a latency prediction. Cache state, scheduling, hardware, mitigations and the operation itself determine the measured cost. Library buffering can combine small application reads into fewer kernel requests.

Monolithic, but modular

Linux places core services and conventional kernel drivers in a shared privileged protection domain. This is the monolithic part: subsystems can use internal calls and shared kernel data without an IPC boundary between each service.

A microkernel keeps a smaller set of mechanisms privileged and moves more services into separate user-space processes. Services communicate through controlled interfaces, often message passing. These boundaries can help contain faults, but recovery must still be designed. IPC has a cost; it does not establish that every microkernel is slow. seL4's architecture and performance discussion illustrates why implementation and workload matter.

Compare where services live, then highlight the schematic read paths. The comparison is about protection and communication boundaries, not benchmark results.

Where do services run?
View

Monolithic · Linux

User space

Application → C library

↓ System call

Kernel space

VFS / filesystem → block layer → driver
Scheduler · memory management · networking

Internal calls share the kernel protection domain. A faulty driver can damage the rest of the kernel.

Microkernel · schematic

Separate user-space services

Application
Filesystem service ↔ driver service

↕ IPC mediated by the kernel

Kernel space

IPC · scheduling · basic memory / protection mechanisms

Service boundaries can contain faults. Recovery and performance depend on the whole system design.

Monolithic describes the protection boundary, not the source layout. A Linux module is separately loadable code, but it still joins the same kernel protection domain.

Loadable modules provide another kind of modularity. A .ko file can add functionality without building it into the kernel image, but its code still runs inside the kernel's protection domain. Loading a driver does not turn it into an isolated user-space service. See kernel modules for loading, dependencies and lifetime.

Isolation within a shared kernel

Containers use the same kernel mechanisms already introduced: processes, memory, files and networking. They add different views of resources and limits on their use, rather than a separate guest kernel.

Namespaces change what a process sees

PID namespaces give a process different identifiers in nested views. Network namespaces separate interfaces, routes and ports. Mount namespaces separate mount views. User namespaces map user and group IDs; the mapping depends on configuration. These are distinct mechanisms, not one blanket security boundary. The namespace manual lists the resource types and their interfaces.

Switch between the host and container views below. The highlighted nginx process is the same task in both views, while its PID and visible network environment differ.

Different views, one kernel
Observe from

PID namespace

PID 1systemd
PID 12345nginx
PID 12346worker

The initial PID namespace can see tasks in descendant PID namespaces.

Network namespace

eth0192.168.1.100
docker0172.17.0.1
lo127.0.0.1

The host bridge connects to a virtual interface in the container namespace.

Kernel instances
1 shared
Container cpu.max
50000 100000

The cgroup has a budget of 50 ms of CPU time per 100 ms period, shared across its tasks. This limits bandwidth; it does not pin them to half a core.

Authored example: nginx has two PIDs in nested namespaces, but it is the same process. Namespace views and cgroup limits do not create another kernel.

cgroups control resource use

Namespaces answer “which resources are visible?” cgroups account for and control how a group of tasks uses resources. A cgroup v2 CPU budget is expressed as quota and period, both in microseconds:

cpu.max
50000 100000

For the fair-class tasks in this example, that is 50 ms of CPU time per 100 ms period, aggregated across the group. It is a bandwidth limit, not a promise of CPU time or a half-core affinity setting. The CPU controller must be enabled in the hierarchy. See the cgroup v2 CPU interface.

Container runtimes combine these mechanisms with capabilities, syscall restrictions, filesystem setup and other security controls. Namespaces, cgroups and containers explain the separate pieces.

eBPF adds controlled hooks

eBPF programs can attach to supported tracing, networking and security hooks. The verifier checks properties such as permitted memory access and program-state constraints before loading. This is a constrained extension mechanism, not permission to attach to every function or a guarantee that the entire kernel is bug-free. The verifier documentation describes its checks.

Where to go next

If you found this explanation helpful, consider sharing it with others.

Mastodon