Why the kernel is separate from user space
3 min readThis lesson opens M5, Linux Device Drivers, and it deliberately starts away from devices. A driver is a plugin that lives inside the the privileged core of the operating system that manages hardware and shared resources. Before you write one, you need to understand the engine it plugs into. If you completed the xv6 journey in M3, you already know most of these ideas in miniature: a process table, a scheduler loop, a page-table walk and a file layer. Linux is the same architecture scaled up by a thousand. If you skipped M3, nothing here assumes it. This lesson draws a map of the four subsystems a driver writer meets every day: tasks, the scheduler, memory and the virtual filesystem.
The split between kernel and user space is not a suggestion. It is enforced by hardware. Every modern CPU has at least two privilege levels. On x86 these are ring 0 (kernel) and ring 3 (user). On ARM they are called exception levels: an ARM privilege level, where EL0 runs user code and EL1 runs the kernel runs user code, EL1 runs the kernel, and higher levels exist for virtualization (EL2) and firmware such as TrustZone (EL3). Linux uses two of them. A user program in ring 3 or EL0 cannot read kernel memory, cannot issue privileged instructions, and cannot touch hardware registers directly. It reaches the kernel through exactly three doors: a a controlled request from a user program to the kernel, such as read() or open(), an exception (a fault such as a page fault or a divide-by-zero), or an interrupt raised by hardware.
Why split at all? Three reasons. First, isolation: a buggy or malicious user program must not be able to corrupt other programs or the kernel. Second, abstraction: programs see a clean, uniform interface (files, sockets, processes) instead of raw hardware. Third, feasibility: virtual memory and multitasking are only possible if one trusted component arbitrates every access. The cost is a mode switch on every system call, but modern CPUs make that switch fast.
Linux is a monolithic kernel, not a microkernel. All core services, the scheduler, memory manager, VFS, network stack and device drivers, run together in one address space at the highest privilege level. This is fast because subsystems call each other as plain C functions with no message passing. The flexibility that a monolithic design would otherwise lack is restored by loadable modules: drivers can be inserted and removed at runtime without rebooting. You will write a module in the next session.
A Raspberry Pi or an STM32MP1 runs the same monolithic kernel with the same EL0/EL1 split. What changes is the other side: user space may be a single BusyBox binary, there may be no swap, and RAM is measured in megabytes. The kernel you are about to study is identical in design; only its configuration, its drivers and its patience for waste differ.
Why should an engineer care? Because a driver runs with the master keys. A null-pointer dereference in user space kills one program. The same bug in a driver stops the whole board. That asymmetry explains the rest of this module: kernel code must not sleep where sleeping is forbidden, must not trust user pointers, and must release every lock it takes. The debugging tools you will meet later, oops messages, KASAN, lockdep, exist because kernel bugs are expensive.
The four subsystems and how Linux got here
4 min readLinux did not invent its shape. It inherited it from Unix. Unix appeared in 1969 at Bell Labs, written by Ken Thompson and Dennis Ritchie. Two ideas from that era still define Linux. First, the fork/exec model: a process is created by copying an existing one, then a new program replaces the copy. Second, the everything-is-a-file principle, where devices, pipes and regular files share one interface. Linus Torvalds released the first Linux kernel in 1991 as a monolithic Unix-like kernel for x86 PCs. A famous 1992 exchange with Andrew Tanenbaum, author of the microkernel Minix, argued exactly the trade-off from the previous section: microkernels are cleaner, Tanenbaum said, but monolithic kernels are faster. Linux stayed monolithic and added loadable modules in 1995 to get flexibility back.
The scheduler tells the story of pressure forcing change. The early scheduler was simple and slow on many tasks. The 2.6 kernel in 2003 introduced the O(1) scheduler, which ran in constant time but handled interactive desktops poorly. In 2.6.23, released in 2007, it was replaced by the Completely Fair Scheduler (CFS), which ran normal processes for sixteen years until Linux 6.6 in 2023 switched the fair class to the EEVDF algorithm. Memory management and the VFS matured more quietly but just as deeply.
Browse the history below — select an event for its detail:
Where does Linux run now? Everywhere. It is the kernel of most web servers, of Android phones, of most home routers and set-top boxes, of industrial controllers, of cars, and of every one of the TOP500 supercomputers, a clean sweep since November 2017. The embedded angle is worth stating plainly: the kernel on your lab board is the same kernel as on a data-center server. What differs is the configuration (fewer features, smaller), the drivers (for your specific SoC), and the constraints (no swap, small RAM, power limits). This is why learning the internals once pays off across every target.
Going deeperMonolithic versus microkernel, the old debate
In a microkernel, device drivers, filesystems and the network stack run as separate user-space servers, and the kernel only passes messages between them. The claimed win is isolation: a crashed driver server can be restarted. The cost is performance, because every service call crosses a privilege boundary and copies data. Tanenbaum argued for this in 1992; Torvalds argued that in practice a monolithic kernel was faster and simpler to develop. Linux chose monolith with modules, and this is not a settled argument so much as a spectrum: some designs, such as running drivers in user space with UIO or VFIO, reintroduce microkernel ideas selectively. On an embedded product, where a millisecond of latency matters, the monolithic choice usually wins.
The rest of this lesson is organized as the kernel itself is: tasks first, then the scheduler that runs them, then the memory they use, then the files they read. Each section names the data structures and functions you will meet again and again while writing drivers.
The task: struct task_struct and task states
4 min readThe kernel calls processes and threads alike a the kernel's word for a process or thread, represented by struct task_struct. The descriptor for every task is struct task_struct, defined in include/linux/sched.h. It is one of the largest structures in the kernel, but its fields are the complete record of one running program. The important ones for a driver writer:
pidis the task's thread ID.tgidis the thread group ID, the number user space calls the process ID and the valuegetpid()returns. For a single-threaded process the two are equal.parentandreal_parentpoint to the parent task, andchildrenlists the children.mmpoints to the address space (thestruct mm_struct). It is NULL for kernel threads.fspoints to the filesystem context, the root and current working directory.filespoints to the table of open file descriptors.credpoints to the credentials: the user and group IDs and the capability set.commis the short name shown byps, stored in a 16-byte array.stateandexit_staterecord where in its life the task is.
Every task descriptor is allocated from a dedicated slab cache called task_struct_cachep, a detail you will revisit in the memory section. Kernel code that needs the current task reads it through the current macro, which each architecture implements efficiently (on x86 it is derived from the stack pointer, on some architectures a register holds it).
The state field holds one of a small set of values. TASK_RUNNING means runnable, either actually on a CPU or queued waiting to run. TASK_INTERRUPTIBLE means sleeping, waiting for an event, and it will wake early if a signal arrives. TASK_UNINTERRUPTIBLE means sleeping but ignoring signals, used for short waits that must not be interrupted. TASK_STOPPED means execution stopped, typically after SIGSTOP. TASK_TRACED means a debugger is tracing it. Two more values live in the separate exit_state field: EXIT_ZOMBIE for a task that has exited but whose parent has not yet collected its status, and EXIT_DEAD once it is fully released.
Step through the task states below:
You can read all of this from user space. The ps STAT column uses one letter per state: R, S, D, T, Z and X. /proc/PID/status and /proc/PID/stack show more. Typical output, yours will differ:
Name: sensor-loop
State: S (sleeping)
Tgid: 1142
Pid: 1142
PPid: 1
A task stuck in TASK_UNINTERRUPTIBLE shows as D in ps. It ignores even SIGKILL, so it cannot be killed. This is by design: the task is usually waiting for an unresponsive storage device and must not be interrupted mid-operation. When you see D-state tasks piling up on a board, suspect the storage, not the process. Inspect /proc/PID/stack and /proc/PID/wchan to see where each one is stuck.
One naming trap: until kernel 5.14 the field was literally called state; since then it has been renamed __state and code should use accessors such as set_current_state() and READ_ONCE(current->__state) rather than poking the field directly. You will see both spellings in older drivers.
Creating tasks: fork, execve, copy-on-write and kernel threads
4 min readA process is created by fork(). The new child is an almost exact copy of the parent, differing only in its PID, its parent PID and a few bookkeeping fields. fork() returns twice: the parent gets the child's PID, the child gets 0. Immediately after, the child usually calls a member of the exec() family, which replaces the address space with a new program. Separating the two is the Unix trick that makes shells possible: the shell forks, then the child execs the command.
Linux implements fork() through the clone() system call, which then calls copy_process(). A normal fork() is clone(SIGCHLD). The work of copy_process() is to copy or share each resource according to the clone flags, allocate a PID, and set the child's state to TASK_UNINTERRUPTIBLE so it does not run yet. The child is deliberately scheduled before the parent on the common path, because the child usually calls exec() straight away.
The clever part is sharing pages read-only between parent and child, and copying a page only when one of them writes to it. Instead of copying the whole address space, fork() copies only the page tables and marks the data pages read-only and shared. When either process writes to such a page, the CPU raises a page fault, and the fault handler makes a private copy, updates the page table, and lets the write proceed. A page that is never written is never copied. Because exec() throws the child's address space away anyway, the common fork() then exec() path copies almost nothing.
Parent calls fork(): the C library turns this into a clone() system call with the SIGCHLD flag.
copy_process() runs: the kernel allocates a new task_struct, copies the page tables, and marks writable data pages read-only and shared.
The child is woken: the scheduler runs the child first, which usually calls exec() to load a new program.
Someone writes a shared page: the write triggers a page fault because the page is read-only.
The fault handler copies the page: each process now has its own writable copy, and the write completes.
A a task that shares its address space and other resources with sibling tasks is, to Linux, just a task that shares more. There is no special thread type. A thread is created with clone(CLONE_VM | CLONE_FS | CLONE_FILES | CLONE_SIGHAND), meaning it shares the address space, filesystem context, open-file table and signal handlers with its siblings. CLONE_THREAD places the new task in the same thread group, so all threads of one process report the same tgid (and thus the same getpid()). This design means the scheduler schedules tasks, and it neither knows nor cares which of them are threads.
The kernel also runs tasks of its own, called a task that runs only in kernel space and has no user address space, with mm set to NULL. Kernel threads do background work such as flushing dirty pages and servicing interrupts. They are created only by other kernel threads, ultimately by kthreadd (PID 2). The API is kthread_create() to build one in a sleeping state and kthread_run() to build and start it in one call; the thread function loops until kthread_should_stop() returns true, and kthread_stop() ends it. The lab for this lesson has you write exactly this:
#include <linux/kthread.h>
#include <linux/sched.h>
#include <linux/delay.h>
static int sensor_loop(void *data)
{
while (!kthread_should_stop()) {
pr_info("pid=%d tgid=%d comm=%s state=%ld\n",
current->pid, current->tgid,
current->comm, current->__state);
msleep(1000);
}
return 0;
}
A kernel thread has no mm, so when it runs the kernel reuses the address space of whatever task ran before it, which is fine because it never touches user memory. This is one of the most important rules you will carry into driver writing: kernel threads live entirely in kernel space.
The scheduler: classes, CFS, EEVDF and real-time policies
5 min readThe scheduler divides processor time among runnable tasks. Linux preempts tasks: a running task can be involuntarily suspended so another can run. The scheduler is modular, built from scheduler classes. Each class describes a policy, and the classes are ordered by priority. schedule() asks each class in order, starting with the highest, to supply a task, and the first non-empty class wins.
The highest classes are stop_sched_class, used to force a CPU to stop everything (for migration and CPU hotplug), then dl_sched_class for the deadline policy, then rt_sched_class for the real-time policies, then fair_sched_class, the fair class that schedules normal tasks, and finally idle_sched_class, which runs the idle task when nothing else is runnable.
The fair class has had two designs, and you need both, because older kernels on real boards still run the first. From 2.6.23 (2007) to 6.5 it was CFS, the Completely Fair Scheduler. Its idea is to model an ideal processor that could run every runnable task simultaneously, each getting an equal share, with no fixed time slices. Each task carries a virtual runtime called the running time of a task normalized by its weight, used by the fair scheduler to track how much of its share a task has used. When a task runs, its vruntime grows, weighted so that a lower nice value (higher priority) makes it grow more slowly and thus lets the task run more. The CFS rule is one line: run the task with the smallest vruntime. It is implemented with a red-black tree keyed by vruntime, one per CPU, whose leftmost node is the next task, reached through a cached pointer. Each runnable task is represented by a struct sched_entity embedded in its task_struct.
Since Linux 6.6 (October 2023) the fair class picks tasks with Earliest Eligible Virtual Deadline First, the algorithm that replaced CFS's pick rule in Linux 6.6 instead. EEVDF keeps vruntime and the red-black tree, but adds two ideas. A task is eligible only if it has received no more than its fair share so far (its lag is not negative). Each task also gets a virtual deadline, its eligible time plus a requested slice scaled by its weight. The rule becomes: among eligible tasks, run the one with the earliest virtual deadline. The tree is augmented so this search stays fast. The practical gain is better latency for tasks that ask for short slices, without CFS's pile of wakeup heuristics. Ubuntu 24.04 (kernel 6.8) and current Raspberry Pi OS kernels run EEVDF; a board vendor kernel based on 5.x or 6.1 still runs CFS. Most kernel documentation, function names and tools still say "CFS" for the fair class, so you will see both names.
The nice value, from -20 (highest) to +19 (lowest), is not a time slice but a weight. It controls the proportion of processor time, and only the relative difference between nice values matters, under both CFS and EEVDF.
Real-time tasks use the other classes. SCHED_FIFO runs until the task blocks or yields; it has no time slice. SCHED_RR is the same but round-robins between tasks of equal priority. Real-time priorities run from 1 to 99, and any real-time task preempts every normal task. SCHED_DEADLINE schedules by an explicit contract of runtime, deadline and period, using an earliest-deadline-first policy with a constant bandwidth server. You set these with chrt:
sudo chrt -f 50 ./busy-loop # run at SCHED_FIFO priority 50
chrt -p 1142 # show the policy and priority of a task
On an embedded control loop, SCHED_FIFO or SCHED_DEADLINE gives bounded scheduling latency for the hot task. But these policies give soft real time only: the stock kernel does not guarantee deadlines. Bounded worst-case latency needs PREEMPT_RT, which was merged into the mainline kernel in Linux 6.12 (2024) after nearly twenty years as an out-of-tree patch set; M15 covers it in depth, and which you will hear about again in the next section.
Going deeperPELT and CFS bandwidth control
CFS needs to know how loaded each task is to balance work across CPUs. It uses per-entity load tracking (PELT), which estimates each sched_entity's contribution to CPU load and decays it geometrically with a 32 ms half-life. This load feeds the load balancer, which migrates tasks between CPUs, and supports group scheduling, where a cgroup of tasks is treated as one entity and gets a fair share against other groups. On top of that, the cgroup CPU controller enforces bandwidth control: cpu.cfs_period_us and cpu.cfs_quota_us (surfaced together as cpu.max) cap how much CPU time a group may consume per period, throttling it when it exceeds the quota. These are the same knobs systemd and container runtimes use to limit a service or container. You will meet them again in the lab when you change nice values and watch perf sched.
Preemption and the context switch
3 min readA context switch is the act of swapping one task for another, and it is the scheduler's most expensive operation. schedule() picks the next task, then calls context_switch(), which does two things: switch_mm() loads the new task's page tables (its address space), and switch_to() saves the old task's registers and stack and restores the new one's. After the switch, the new task resumes as if nothing happened.
The kernel must know when to call schedule(). A per-task flag, TIF_NEED_RESCHED in thread_info->flags, records that a reschedule is pending. Two places set it: the periodic scheduler tick, when a task has run long enough, and try_to_wake_up(), when a higher-priority task becomes runnable. The flag is checked when returning to user space and when returning to kernel space.
User preemption is simple. When the kernel is about to return to user space, after a system call or an interrupt, it checks the flag and calls schedule() if it is set. This is always safe because a task returning to user space is at a clean boundary.
Kernel preemption is the subtle case, and it is what makes Linux a preemptive kernel. A task running in the kernel can also be preempted, but only when it holds no locks. The kernel tracks this with a per-task counter, preempt_count, which is incremented for each held lock and decremented on release. On return from an interrupt to kernel space, the kernel checks both TIF_NEED_RESCHED and preempt_count: if the flag is set and the counter is zero, it is safe, and schedule() runs. If the counter is non-zero, the task holds a lock and the switch is deferred until the lock is released.
How eagerly the kernel preempts is a compile-time choice. CONFIG_PREEMPT_NONE disables kernel preemption entirely (low latency overhead, coarse response). CONFIG_PREEMPT_VOLUNTARY adds explicit preemption points. CONFIG_PREEMPT allows preemption anywhere a lock is not held, and is the desktop and embedded default. A separate out-of-tree patch set, PREEMPT_RT, makes almost all spinlocks preemptible to bound worst-case latency; it is the basis of real-time Linux and is gradually merging into mainline.
Kernel preemption can therefore happen in four situations: returning to user space from a system call, returning to user space from an interrupt, when kernel code becomes preemptible again (a lock is released), and when a task blocks or calls schedule() directly. The rule a driver writer internalizes: if you hold a spinlock, you cannot be preempted and you must not sleep, because sleeping while holding a spinlock deadlocks the CPU.
Physical memory: zones, the buddy allocator, slab and vmalloc
5 min readThe kernel treats the physical page as the basic unit of memory. Every physical page has a struct page describing its state, its owner and its reference count. On a 4 GB system the page structures themselves cost tens of megabytes, a price the kernel pays to track every page. Pages are grouped into zones because not all pages are equal: some devices can only DMA to low addresses, and on 32-bit systems some memory is not permanently mapped into the kernel.
The classic zones are ZONE_DMA (the lowest pages, for legacy devices), ZONE_DMA32 (addressable by 32-bit devices), ZONE_NORMAL (regular directly mapped pages) and ZONE_HIGHMEM (on 32-bit systems, memory that is not permanently mapped). Modern 64-bit kernels have no ZONE_HIGHMEM because they can map all of memory, and they add ZONE_MOVABLE for migration and ZONE_DEVICE for device memory. An allocation can request a specific zone but cannot cross zone boundaries.
The buddy allocator hands out contiguous runs of pages in power-of-two sizes called orders. Order 0 is one page, order 1 is two pages, order 2 is four, and so on up to the maximum order. Each zone keeps a free list per order, and the allocator splits and merges blocks to resist fragmentation.
1Check the requested free list
The allocator looks at the free list for the requested order and takes a block if one is free.
2Split a larger block if needed
If that list is empty, the allocator takes a block of the next order up, splits it in half, and repeats until it has a block of the right size, returning the unused halves to their free lists.
3Hand over the pages
The block is marked allocated and returned to the caller, either as struct page pointers or as a kernel virtual address.
4Coalesce on free
When pages are freed, the allocator checks whether the adjacent block, its buddy, is also free, and if so merges the two into one block an order larger, repeating upward. This split-and-coalesce behaviour is what resists fragmentation.
The low-level interfaces are alloc_pages() and alloc_page() (which return a struct page), and __get_free_pages(), __get_free_page() and get_zeroed_page() (which return a kernel virtual address). Pages are returned with __free_pages() and free_page().
Most driver code never touches pages directly. It uses kmalloc(), which allocates byte-sized, physically contiguous memory, and kfree() to return it. kzalloc() zeroes the memory, which matters because a page may contain another process's data. The companion flags describe what the allocator may do. GFP_KERNEL is the normal flag and may sleep to reclaim memory. GFP_ATOMIC must not sleep, so it is the only choice in interrupt handlers, softirqs and anywhere a spinlock is held. Two intermediate flags exist for filesystem and block code: GFP_NOFS and GFP_NOIO, which may block but must not recurse into the filesystem or start disk I/O.
kmalloc() is built on the slab layer, a cache of objects of a fixed type. The slab layer organizes objects into caches; each cache holds slabs, and each slab holds objects in one of three states: full, partial or empty. When a driver frequently creates and destroys a structure, it should create its own cache with kmem_cache_create() and allocate from it with kmem_cache_alloc() and kmem_cache_free(). Modern kernels use the SLUB variant as the default implementation; /proc/slabinfo lists the caches. This is exactly how task_struct is allocated, as you saw earlier.
Finally, vmalloc() allocates memory that is virtually contiguous but not physically contiguous. It is useful for large buffers that only software touches, because hardware on the other side of the MMU needs physically contiguous memory. vmalloc() has to set up page-table entries for each page, which thrashes the TLB, so drivers use it only for large or non-contiguous needs and use vfree() to release it.
With no swap and a few hundred megabytes of RAM, GFP flag choice stops being academic. A GFP_ATOMIC allocation has no ability to reclaim memory, so it fails sooner under pressure. Drivers that DMA must request DMA-able zones with GFP_DMA or GFP_DMA32 for devices that cannot reach all of memory, a constraint that bites far more often on embedded SoCs with quirky DMA engines than on desktops.
Going deeperNUMA nodes, zonelists and huge pages
Large machines are organized as NUMA nodes, where each node has CPUs and local memory, and accessing another node's memory is slower. Each node has its own set of zones, and allocations prefer the node local to the requesting CPU. Each zone keeps a zonelist, an ordered list of zones to try when the preferred zone is exhausted, which encodes the fallback order. On memory pressure the kernel reclaims per node. At the other extreme, transparent huge pages (THP) let the kernel use 2 MB (or 1 GB) pages instead of 4 KB pages to reduce page-table depth and TLB pressure, at the cost of internal fragmentation and, historically, occasional latency spikes. Embedded boards rarely enable THP for that latency reason; they value predictability over peak throughput.
Address spaces, page tables and page faults
3 min readThere are three distinct address spaces in a Linux system. User virtual addresses are what a process sees, private to that process. Kernel virtual addresses are where the kernel lives, including a direct-map region that maps all physical memory. Physical addresses are what the RAM and devices actually use. The MMU translates user and kernel virtual addresses to physical ones on every access.
The translation uses a multi-level page table. On 64-bit systems there are five levels: the page global directory (PGD), the page-4 directory (P4D), the page upper directory (PUD), the page middle directory (PMD) and the page table entries (PTE) that finally point to physical pages. Each level indexes into the next, which is what allows a sparse address space without a giant flat table. Because walking several levels on every access would be slow, the CPU caches recent translations in the translation lookaside buffer, the TLB. A TLB miss is expensive, and a context switch that flushes or invalidates TLB entries pays that cost, which is why switch_mm() matters.
A process's address space is described by its struct mm_struct, and the regions within it by struct vm_area_struct, or VMA. Each VMA is a contiguous range with permissions and, optionally, a backing file. The VMAs of a process are visible in /proc/PID/maps and, with per-region accounting, in /proc/PID/smaps. The kernel keeps VMAs both in a list, for traversing all of them, and in a red-black tree, for finding the one that contains a given address.
Memory is allocated lazily, which is the entire point of the the CPU exception raised when a virtual address has no valid mapping, handled by the kernel to bring the page in. When a process touches an address with no valid page-table entry, the CPU faults into the kernel, and the fault handler resolves it. A minor fault means the page is already in memory and only the page-table entry needs filling, cheap and common. A major fault means the page must be read from disk, expensive. When a write hits a read-only copy-on-write page, the handler copies the page, a COW break. Anonymous mappings (stack, heap, malloc) are backed by zeroed pages or swap, while file-backed mappings are backed by a file and ultimately by the page cache. Demand paging is the name for this whole scheme: pages are only materialized when touched.
The same pgd/pud/pmd/pte walk applies on ARM64, with one embedded twist: page size is configurable. ARM64 supports 4 KB, 16 KB and 64 KB pages, chosen at kernel build time. The bigger the page, the shorter the walk and the bigger the minimum allocation granularity, so a 64 KB-page build wastes more RAM on tiny allocations. On a memory-tight board this is a real product decision made long before any driver code is written.
For a driver writer, the practical takeaways are few but sharp. First, user memory is not guaranteed to be resident: copy_to_user() and copy_from_user() can themselves fault, which is one reason they may sleep. Second, if a driver maps a device or a buffer with mmap(), it is the VMA and its vm_operations_struct that connect user addresses to the driver's pages, a topic expanded in a later session on mmap.
The VFS and the page cache
4 min readThe virtual filesystem, the VFS, is why open(), read() and write() work on ext4, tmpfs, procfs and a network share without the caller caring which one it is. The VFS defines a common file model and lets each filesystem implement it. Its four primary objects are: the superblock (struct super_block), representing one mounted filesystem; the inode (struct inode), representing one file and its metadata; the dentry (struct dentry), representing one component of a path; and the file object (struct file), representing one open file with its current offset.
Each object carries a table of function pointers the filesystem fills in. The one a driver writer meets first is struct file_operations, whose members are the familiar operations: read, write, llseek, ioctl, mmap, open and release. When user space calls read() on a file, the VFS dispatches to the filesystem's read method through this table. This is the same mechanism by which a character device driver later exposes its read and write.
Opening a path means walking it: the VFS resolves each component into a dentry, consulting the dentry cache (the dcache) before touching the disk. A dentry that maps to nothing is cached as a negative dentry so that repeated lookups of a missing file stay fast. This caching is why a path lookup is cheap after the first time.
File data is cached separately in the the kernel cache that stores file contents in pages keyed by inode and offset, absorbing reads and writes. Each inode has an address_space, and its pages are keyed by offset within the file. A buffered read first looks in the page cache; a hit copies straight to user space. A miss allocates a page and issues readpage() to fill it from the device. A buffered write marks pages dirty and returns immediately; a background flusher thread writes them back to the device later. Read-ahead prefetches sequential pages ahead of the reader.
Step a page through its cache states below:
User calls read(): the VFS finds the file object and its file_operations.
Look up the page cache: the kernel checks the inode's address_space for the page at the file offset.
Cache hit: if the page is present, the data is copied to user space and the call returns.
Cache miss: the kernel allocates a page and issues readpage() to read it from the device.
Data arrives: the page is inserted into the cache and copied to user space, ready for the next read.
On an embedded board with eMMC or raw NAND, the page cache is a double-edged sword. It hides slow flash behind fast RAM, but a power cut can lose dirty pages that were never written back, and a burst of writeback can stall the whole system. Embedded products therefore think hard about when to sync, and some use direct I/O or O_DIRECT to bypass the cache for data that must reach the device immediately.
The VFS and the page cache are the glue that turns the abstract file model into real bytes on a device, and they are the machinery a block or filesystem driver plugs into. For a character driver the page cache is usually irrelevant, but the VFS object model is not: your driver is a set of file_operations behind a device inode, and everything you learn here transfers directly.
System calls, copy_from_user, RCU and common mistakes
5 min readA system call is how a user program asks the kernel to act. The program places the system call number and arguments in registers and executes a trap instruction: on x86-64 the syscall instruction, on ARM64 the svc instruction, which drops from EL0 to EL1. The kernel saves the registers into a struct pt_regs, looks the number up in the architecture's sys_call_table, and calls the handler.
System calls are defined with the SYSCALL_DEFINE macros. SYSCALL_DEFINE0(getpid) expands to sys_getpid(void); the digit is the argument count, and the macro generates the argument marshalling for you. The syscall number is a frozen ABI: once assigned it never changes, and removed calls leave a sys_ni_syscall() stub that returns -ENOSYS. A driver almost never adds a system call; it adds a file_operations method instead. But every driver handles data that crossed this boundary.
A pointer that arrived from user space may be invalid, unmapped, or may point at kernel memory. Dereferencing it directly crashes the kernel or, worse, leaks data. The kernel must always copy through copy_from_user() to read from user space and copy_to_user() to write to it. Both return the number of bytes not copied, and the caller returns -EFAULT on failure. The __user annotation on such pointers lets the sparse checker flag direct dereferences at build time.
Here is the shape of the check every such path performs, the same pattern you saw in the books:
SYSCALL_DEFINE3(dev_write, unsigned int, minor,
const char __user *, buf, size_t, count)
{
char kbuf[64];
if (count > sizeof(kbuf))
return -EINVAL;
if (copy_from_user(kbuf, buf, count))
return -EFAULT;
/* kbuf is now safe kernel memory */
return count;
}
Two more checks complete the discipline. Access control uses capable() rather than a raw root check, so a call such as capable(CAP_SYS_NICE) decides whether the caller may change priorities. And the data itself must be validated, not just the pointer, because a user can pass any value.
Going deeperRCU: reading without locks
Read-copy-update, RCU, is the kernel's tool for making lock-free reads cheap. A reader calls rcu_read_lock(), reads shared data, and calls rcu_read_unlock(), paying almost nothing. A writer never modifies data in place; it copies, updates the copy, then publishes it with a single pointer store. Old copies are freed only after a grace period, the time until every CPU has passed through a quiescent state such as a context switch or an rcu_read_unlock(). Writers wait with synchronize_rcu() or schedule the free with call_rcu(). RCU is why the task list, the dentry cache and network routing tables can be read at very high rates without a single lock on the read path. You will use it less often in drivers than in core subsystems, but understanding the grace-period idea explains a lot of kernel design.
Common mistakes and how to debug them
A short catalogue of the failures you will meet, each with its symptom, cause, diagnosis and fix.
D-state tasks pile up and nothing can kill them. Symptom: ps shows many processes in D state, and shutdown hangs. Cause: tasks blocked in TASK_UNINTERRUPTIBLE on a storage device that never completes. Diagnose: read /proc/PID/stack and /proc/PID/wchan to see the call site, then check dmesg for storage errors. Fix: fix the storage or driver, not the process; the D state is a symptom of a stuck I/O path.
A kernel oops reports sleeping in an invalid context. Symptom: a backtrace ending in schedule() with a warning such as "sleeping function called from invalid context". Cause: calling a function that may sleep, such as kmalloc(..., GFP_KERNEL) or copy_from_user(), while holding a spinlock or running in an interrupt. Diagnose: read the oops backtrace to find the call chain and check which context you were in. Fix: switch to GFP_ATOMIC, drop the lock before sleeping, or defer the work to a context that may sleep.
A user pointer dereference crashes or leaks data. Symptom: an oops in copy_from_user-free code, or a security report. Cause: dereferencing a __user pointer directly, trusting a pointer from user space. Diagnose: sparse and static analyzers flag missing __user annotations; the oops shows the faulting address. Fix: route every user pointer through copy_from_user() and copy_to_user(), and check their return values for -EFAULT.
A small buffer overflow corrupts random kernel state. Symptom: crashes that move around, or data corruption with no obvious cause. Cause: a write past a kmalloced buffer, an off-by-one, or a missing length check. Diagnose: rebuild with KASAN and reproduce; KASAN pinpoints the exact read or write and its allocation site. Fix: validate lengths, use kzalloc and bounded copy helpers, and prefer the checked interfaces throughout.
These four failures explain most of the rules this module teaches. The kernel trusts its own code, so the bugs do not fail safely; they corrupt state and stop the board.
Try it
1 min readReinforce this lesson with the lab and practice pack before the next session. Trace a real fork() and execve() with ftrace or perf sched, and correlate what you see with the state machine and the context-switch path from this lesson. Read /proc/PID/status, /proc/PID/stack and /proc/PID/smaps for a process you start yourself, and identify its VMAs. Then build the small module that prints current->pid, current->comm and current->__state from a kernel thread, which is the first code you will compile for the kernel. The next session turns that module into a repeatable QEMU development loop.
Key takeaways
- The kernel runs at a higher privilege level and is entered only through system calls, exceptions and interrupts.
- Every process and thread is a struct task_struct; threads differ only by sharing mm, files, fs and signal handlers via clone() flags.
- CFS (2.6.23 to 6.5) ran the task with the smallest vruntime; since 6.6, EEVDF runs the eligible task with the earliest virtual deadline.
- Linux is a preemptive kernel: a running task can be preempted whenever preempt_count is zero and need_resched is set.
- Pages come from the buddy allocator, small objects from slab caches, and large non-contiguous buffers from vmalloc().
- Virtual memory is lazy: page faults drive demand paging, and copy-on-write defers copying until a page is actually written.
- The VFS turns open/read/write into per-filesystem operations and caches file data in the page cache keyed by address_space.
- System calls are declared with SYSCALL_DEFINE macros and must never trust user pointers without copy_from_user or copy_to_user.
Reflect
- Explain to a colleague, in two sentences: describe the kernel/user split, privilege levels, and why Linux is monolithic with loadable modules
- Which objective are you least sure about? Re-read that section, then do its exercise in the practice pack.
- What surprised you when you ran the commands yourself?
On a real board
Repeat the key commands on your Raspberry Pi or lab board over SSH or the serial console. Note anything that behaves differently from your PC, such as missing tools, a BusyBox shell or a read-only filesystem.
References and further reading
- Official docsThe Linux kernel documentation
- Official docsCFS scheduler design documentation
- Man pagesched(7) process scheduling overview
- Man pageproc(5) process information pseudo-filesystem
- ArticleWhat is RCU, Fundamentally?
- ArticleBootlin Linux kernel and driver development training slides
Official documentation first. Links checked when the lesson was published.