Chris Low-level systems developer
← Back to all posts

The Allocator That Stores Its Free List in the Free Pages

ISA

not at ISA because every verified claim is about the freelist algorithm and its guards, which hold under any machine contract: the drained page count, the LIFO return order, the 0x01/0x05 junk fills, and the three guard panics are all properties of the list mechanics, and the article asserts no instruction encoding, register, or ABI behavior.

MICROARCH

not at Microarch because nothing was timed and no pipeline, cache, or execution behavior was measured: the verified properties are which page comes back and in what order, which hold on any implementation.

FIRMWARE

not at Firmware because no boot, machine-mode, or supervisor-interface claim is made: kinit, freerange, kalloc, and kfree are kernel functions exercised against a host arena, and the PHYSTOP bound comes from memlayout.h read as a constant, not executed as firmware.

KERNEL

matters at Kernel because kalloc.c is the kernel's physical page allocator: kinit, freerange, kalloc, and kfree are kernel functions, and the verified claims are about kernel memory management, the exact list that feeds user pages, kernel stacks, page-table pages, and pipe buffers.

ALGORITHM

matters at Algorithm because the verified mechanism is the freelist itself: a stack threaded through free pages with LIFO return order, PGROUNDUP rounding on unaligned ranges, and one pointer read or write per operation, all proved by the harness independent of the kernel it came from.

I pointed xv6's page allocator at a 64 MiB arena this morning and watched it drain 16383 pages, every one distinct, aligned, and inside the range. Then I freed three pages, a, b, and c, and asked for three back. It returned c, b, a. The freelist is a stack: last freed, first allocated.

Empty wooden archive shelves in a 1930s reading room
The free list lives in the free pages themselves.

The allocator is 76 lines of C in kernel/kalloc.c, and it stores no metadata of its own. That sounds impossible until you read the struct it is built on, and then the lines that prove it:

struct run {
  struct run *next;    /* stored in the free page itself */
};
r = (struct run *)pa;  /* freeing page pa: the page becomes its own list node */
r->next = kmem.freelist;
kmem.freelist = r;

A free page's first eight bytes hold the link to the next free page. Nothing else is stored in a free page, so the list node lives in the page itself. Allocating is popping the head of the list, one pointer read. Freeing is pushing onto the head, one pointer write, under a single spinlock. Every allocator faces the bootstrap question of where to keep its metadata; xv6 answers by storing it in the free pages themselves.

The shape works because everything xv6 hands out comes in whole 4096-byte pages: user pages, kernel stacks, page-table pages, pipe buffers. The allocator never thinks smaller than a page, so one link per page is all the metadata it needs, and there is nothing to allocate in order to store it, which matters when the allocator itself is what provides memory. The obvious alternatives each fail one requirement. A bump allocator cannot free, and every page must come back. A buddy allocator coalesces, but costs complexity this teaching kernel refuses to pay.

The honest cost is the single global list under one lock, and MIT's own locks lab exists to fix exactly that, having students split the allocator into per-CPU freelists with stealing because kalloctest contends on kmem.lock. The file also carries its own tripwires. Freeing fills the page with 0x01 and allocating refills it with 0x05, junk that makes a dangling reference visible. Freeing a misaligned address, an address below the kernel's end, or one at or past the 128 MiB mark panics, all three verified this run. One sentence to keep: xv6's physical memory is a linked list threaded through the free pages themselves, so the allocator stores no separate metadata, and handing you a page costs one pointer read.

Related work: crash triage is $250/symptom, C audit from $350. .

Choose how to get in touch

The project brief is filled in for you.

Compose in Gmail Use my email app