Skip to content

Patterns from Linux Kernel

The Linux kernel has been refined over 30+ years. These patterns have survived decades of real-world use across millions of devices.

PatternWhere in LinuxWhat It Does
Bitmaskinclude/uapi/linux/stat.hFile permission bits (rwxrwxrwx)
Min Heapkernel/sched/fair.c (CFS)Completely Fair Scheduler — pick task with lowest vruntime
Ring Bufferinclude/linux/ring_buffer.hftrace event logging, per-CPU lock-free buffers
State Machinenet/ipv4/tcp_input.cTCP connection state machine (SYN_SENT → ESTABLISHED → FIN_WAIT)
Semaphoreinclude/linux/semaphore.hKernel counting semaphore with down()/up()
Backpressurenet/ipv4/tcp_output.cTCP congestion window (cwnd) — flow control backpressure
Free Listmm/slub.cSLUB slab allocator — intrusive free list with XOR-hardened pointers
Trienet/ipv4/fib_trie.cIP routing table as a compressed trie (LC-trie)
Vtableinclude/linux/fs.hfile_operations struct — function pointer vtable for VFS dispatch (.read, .write, .open)
Batch Processingblock/blk-merge.cBlock layer merges adjacent I/O requests to amortize seek time
Rate Limiternet/sched/sch_tbf.cToken bucket filter for kernel traffic control
Reference Countinglib/kobject.ckref provides reference counting for kernel objects

How They Compose: Reading a File

When a process calls read(), multiple patterns activate in a single syscall:

read(fd, buf, count)
1
Vtable

The VFS layer looks up the file's file_operations struct and calls .read(). ext4, NFS, procfs each provide their own implementation behind the same interface.

2
Bitmask

The kernel checks file permission bits (rwxrwxrwx) against the process's UID/GID. A single AND operation decides access.

3
Reference Counting

Opening the file incremented its inode refcount. The kernel won't free the inode while any fd references it.

4
Batch Processing

If the read triggers disk I/O, the block layer merges adjacent requests to minimize seek time before dispatching.

5
Ring Buffer

ftrace logs the syscall entry/exit into a per-CPU ring buffer for tracing.

The "everything is a file" abstraction works because vtable dispatch lets the kernel treat ext4 files, network sockets, and /proc entries identically. The bitmask permission check happens once regardless of filesystem type. And reference counting ensures no resource is freed while in use — even if another process deletes the file.

Further Reading

Released under the MIT License.