Device drivers and the I/O stack
About this lesson
When a program calls read(), nothing in it knows what kind of disk the bytes are on. The request goes down a stack of kernel layers: the file system turns a file and an offset into a disk block, the block layer queues the request, and the device driver, the only code that knows this device, writes its registers. Then the program sleeps, the SSD copies the block into memory by itself with DMA, and an interrupt brings the answer back up. Follow one 4096-byte read from a C program all the way down and back. The Hardware lesson Interrupts and I/O: how devices get the CPU's attention covers polling, interrupts and DMA from the hardware side, System calls: crossing into the kernel shows how read() enters the kernel, and File systems on SSDs shows what sits just above the block layer.
A stack of layers
A program never talks to a disk. Its read() goes down a stack of kernel layers: the file system, the block layer, then the device driver. Each layer talks only to the one below it, and only the driver touches the device.
user your program read(fd, buf, 4096)
--------- system call -----------------------------
kernel file system (VFS) files and offsets
block layer block requests, queues
device driver this device's registers
--------- hardware --------------------------------
device SSD controller does the work, then interruptsRead the lesson step by step
A stack of layers
A program never talks to a disk. Its read() goes down a stack of kernel layers: the file system, the block layer, then the device driver. Each layer talks only to the one below it, and only the driver touches the device.
user your program read(fd, buf, 4096) --------- system call ----------------------------- kernel file system (VFS) files and offsets block layer block requests, queues device driver this device's registers --------- hardware -------------------------------- device SSD controller does the work, then interruptsWhat a driver is
A device driver is the kernel code that knows one kind of device. It writes the device's registers to give orders and handles its interrupts. Above it, the kernel uses one standard interface for every disk.
a driver (MIT xv6 book, ch. 5): configures the device hardware tells the device to perform operations handles the resulting interrupts interacts with processes waiting for I/O canonical device registers (OSTEP ch. 36): STATUS is it busy or done? COMMAND what to do DATA data in or out (or, with DMA, where in RAM) in Linux, over 70% of the code is drivers (OSTEP)Line 1: open the file
open() is a system call. The file system looks up the name a.txt, checks you may read it, and gives back a small number, the file descriptor. 0, 1 and 2 are taken, so it returns 3.
the whole program (C99, POSIX): #define _POSIX_C_SOURCE 200809L #include <fcntl.h> #include <stdio.h> #include <unistd.h> int main(void) { static char buf[4096]; int fd; ssize_t n; fd = open("a.txt", O_RDONLY); n = read(fd, buf, 4096); printf("%d %zd\n", fd, n); close(fd); return 0; } line 1 fd = open("a.txt", O_RDONLY); fd = 3 (0, 1, 2 = stdin, stdout, stderr)Line 2: read, cache miss
read() traps into the kernel, asking for 4096 bytes. The file system first checks the page cache, kernel memory holding recently read blocks. This part of a.txt is not there yet: a miss. It has to come from the device.
line 2 n = read(fd, buf, 4096); ask fd 3, offset 0, 4096 bytes page cache lookup ... not there (miss) (a later read of the same bytes would hit and never reach the device)From file to block
The file system knows where files live on the disk. Byte 0 of a.txt is in block 8200, so it asks the block layer for that block, 4096 bytes. From here down, nobody knows about files.
file a.txt, bytes 0 .. 4095 block 8200 (example number; 4096 bytes = 8 sectors of 512) request read block 8200 into a page of the page cache Linux: the page cache calls the file system's read_folio / readahead, which builds a bioThe block layer queues it
The block layer works for every disk the same way. It puts the request in a queue, may merge it with neighbouring blocks or reorder it, then hands it to the driver for this SSD.
Linux blk-mq: bio -> request, into a per-CPU staging queue may merge adjacent sectors, may be reordered by an I/O scheduler (or not: "none") -> hardware dispatch queue -> driver's queue_rq(request), with a tagThe driver gives orders
Only the driver knows this device's registers. It writes the block number 8200 and the RAM address to copy into, then one final write, READ, tells the device to start.
driver writes the device registers: BLOCK = 8200 ADDR = the page cache page in RAM CMD = READ <- this write starts the device writes are privileged: only the kernel may do them (port I/O instructions or memory-mapped registers)Sleep, not spin
The device needs time. Instead of checking it over and over (polling), the kernel puts your program to sleep and lets the CPU run another program meanwhile.
polling CPU asks "done yet?" in a loop: wasted interrupts program sleeps (blocked), CPU runs others CPU 1 1 1 2 2 2 2 2 2 1 1 (OSTEP timeline) SSD 1 1 1 1 1 1 = your program, 2 = the other programDMA: the device copies
The SSD reads block 8200 from its flash and copies the 4096 bytes straight into the page cache in RAM. This is direct memory access, DMA: the CPU does not move a single byte.
device reads block 8200 from flash DMA 4096 bytes -> RAM at ADDR (the page cache) CPU still running the other program without DMA (programmed I/O) the CPU would copy every word itselfThe interrupt
Done, the SSD raises an interrupt. The CPU pauses the other program and jumps to the driver's interrupt handler. It checks the status, marks the request complete and wakes your sleeping program.
device STATUS = DONE, raises its interrupt line CPU traps to the kernel, calls the driver's handler handler reads STATUS: ok completes the request (by its tag) wakes the process waiting for it top half the read() path that started the I/O bottom half the handler that finishes itCopy out and return
When your program runs again, the kernel copies the 4096 bytes from the page cache into buf, in the program's own memory. read() returns 4096, the number of bytes read.
copy page cache -> buf 4096 bytes return n = 4096 (read returns the bytes read) compiled and run with a 4096-byte a.txt, prints: 3 4096Why layers?
Read it again and the page cache answers: the device is never touched. Because each layer hides the one below, the same read() works on any disk, and a new device needs only a new driver.
the stack as a Python simulation: disk = {8200: bytes(4096)} # the device's storage cache = {} # page cache: block -> data regs = {} # the device's registers def device_start(): # the device works alone cache[regs["BLOCK"]] = disk[regs["BLOCK"]] # DMA interrupt() def interrupt(): # driver, at interrupt time print("irq: block", regs["BLOCK"], "done") def driver(block): # only code that knows regs regs["BLOCK"] = block regs["CMD"] = "READ" # this write starts it device_start() def file_system(offset): block = 8200 + offset // 4096 if block not in cache: print("miss: block", block) driver(block) # via the block layer return cache[block] def read(count): # the system call return len(file_system(0)[:count]) print("read returned", read(4096)) print("again:", read(4096)) prints: miss: block 8200 irq: block 8200 done read returned 4096 again: 4096
5 quick questions
Choose one answer per question. You can change your choices before checking them.
01Which part of the kernel knows the registers of one particular SSD?
Only the driver knows the device's interface: which registers to write and how to handle its interrupts. The file system and the block layer use one standard interface for every disk, so they never need to know which disk it is.
02The file system turns "byte 0 of a.txt" into something the layers below understand. What?
Below the file system nobody knows about files. It works out which disk block holds those bytes (block 8200 in the example) and asks the block layer for that block.
03While the SSD is reading, what does the CPU do in the interrupt-driven design?
Polling would waste the CPU on asking "done yet?". Instead the kernel puts the reading program to sleep and runs another one. With DMA the SSD also copies the bytes into RAM itself, so the CPU moves none of them.
04What is DMA?
Direct memory access: the driver tells the device where in RAM the data should go and how much, then the device does the copy and raises an interrupt when it is finished.
05The program calls read(fd, buf, 4096) again for the same bytes, right away. What happens?
The first read left the block in the page cache, kernel memory that holds recently read data. A second read hits the cache and never goes down to the block layer, the driver or the SSD.
Glossary
- Device driver
- Kernel code that manages one kind of device: it configures it, gives it orders by writing its registers, handles its interrupts and wakes processes waiting for it.
- I/O stack
- The layers a request passes through on its way to a device: system call, file system (behind the VFS), block layer and device driver.
- Block layer
- The part of the kernel that queues read and write requests for whole blocks, for any kind of disk, and hands them to the right driver.
- Page cache
- Kernel memory in RAM that holds recently read or written parts of files, so repeated reads do not need the device.
- Device register
- A small storage place on the device that the driver reads or writes to check its status, tell it what to do, or pass data or addresses.
- DMA (direct memory access)
- The device copies data to or from main memory itself, so the CPU does not move each byte.
- Interrupt
- A signal from a device that makes the CPU stop what it is doing and run a kernel handler, here the driver's interrupt handler.
- Polling
- Checking a device's status over and over in a loop until it is done. Simple, but it wastes the CPU while a slow device works.
Notes and sources
- The SSD here has three simple registers, like the canonical device in OSTEP. A real NVMe SSD takes its commands from queues the driver builds in RAM, and one register write tells it new commands are waiting, but the idea is the same: the driver prepares the work, one write starts it, the device copies by DMA and interrupts when done.
- Block 8200 is an example: where a file's bytes live depends on the file system and the disk. 4096 bytes is one 4 KiB block and one memory page, 8 sectors of 512 bytes.
- Linux has more layers than shown: the VFS above every file system, the page cache beside it, and schedulers in the block layer. Some programs skip the page cache (O_DIRECT) or the file system (raw block devices), but the driver is always the last step.
- The driver code on the way down (started by read) is often called the top half, and the interrupt handler the bottom half. Linux uses the same words a little differently; the lesson on interrupt handlers covers that.
- OSTEP reports that over 70% of the Linux kernel source is device drivers (Chou et al., SOSP 2001), and that drivers have had several times more bugs than the rest of the kernel. Any one computer runs only a few of them.
- R. and A. Arpaci-Dusseau, Operating Systems: Three Easy Pieces, ch. 36: I/O Devices
- MIT 6.1810, xv6: a simple, Unix-like teaching operating system (RISC-V), ch. 5: Interrupts and device drivers
- Berkeley CS162, Lecture 18: General I/O (device drivers, life cycle of an I/O request)
- Berkeley CS162, Lecture 19: Device Drivers, Storage Devices, Performance
- Linux kernel documentation: Overview of the Linux Virtual File System
- Linux kernel documentation: Multi-Queue Block IO Queueing Mechanism (blk-mq)
- Linux man-pages: open(2)
- Linux man-pages: read(2)
Quiz Unlocks after the last step.