§ MEMORY MANAGEMENT ● A VISUAL FIELD GUIDE LINUX KERNEL / mm
Chapter One — The Problem of Pages

The Buddy
Allocator

How the Linux kernel slices and recombines physical memory into power-of-two blocks — fast, fragmentation-resistant, and elegantly recursive. Watch the algorithm split, allocate, and merge in real time.

FIG. 0 — MEMORY UNDER LOAD
§ 01 / The Core Idea

Memory in powers of two.

The kernel manages physical RAM in fixed-size pages — usually 4 KiB each. The buddy allocator groups them into blocks whose size is always 2ⁿ × pages. We call n the order.

An order-0 block is one page. Order-1 is two contiguous pages. Order-2 is four. Linux typically goes up to order 10 — a 4 MiB chunk of 1024 pages. Every block at order n has a buddy: the adjacent block it would merge with to form a single order-n+1 block.

FIG. 1
A 16-page region — every order is a power of two
ORDER 4
16 pages
ORDER 3
8 pages
ORDER 2
4 pages
ORDER 1
2 pages
ORDER 0
1 page
§ 02 / Bookkeeping

An array of free lists.

For each order, the allocator keeps a doubly-linked list of currently free blocks. The whole structure lives in struct zone.free_area[MAX_ORDER] — one entry per order. Allocating a block is a head-pop; freeing is a head-push.

Initially, all of memory might sit as a single huge block at the top order. As demand arrives, blocks get split downward; as memory is released, they get merged upward.

FIG. 2
free_area[] — one list per order
order 4
block @ 0x000
order 3
∅ (empty)
order 2
∅ (empty)
order 1
∅ (empty)
order 0
∅ (empty)
§ 03 / Allocation

Split until it fits.

Suppose you ask for an order-1 block (2 pages). The allocator looks at free_area[1]. If it's empty, it walks upward until it finds a non-empty list — say, order 4. It pops that block, then splits it in half: one half goes back onto free_area[3], the other gets split again, and again, until we have a block of the requested order.

Press step below to watch a request for an order-1 block cascade down through three splits.

DEMO 1
Allocating a 2-page (order 1) block
ORDER 4
ORDER 3
ORDER 2
ORDER 1
step 0 One free order-4 block. A request for order 1 arrives.
walk through →
§ 04 / Why "Buddy"?

One bit of address, flipped.

Two blocks of order n are buddies if their starting addresses differ in exactly one bit — the bit at position n (counting in pages, from zero). To find the buddy of a block at index i, you simply XOR with 2ⁿ:

◇ Buddy formula

buddy(i, n) = i XOR 2ⁿ

// example, order 1:
block @ 0100 (idx 4)
buddy @ 0110 (idx 6)

◇ Why it matters

On free, the allocator computes the buddy's address in O(1), checks if it's also free at the same order, and if so —

→ removes the buddy from its list
→ merges the two into one order n+1 block
→ recursively tries to merge again
§ 05 / Deallocation

Merge until the buddy is busy.

Freeing is the mirror image of allocation. When a block is released, the allocator looks at its buddy. If the buddy is also free at the same order, they coalesce into a block one order larger. Then it checks that block's buddy — and the merge can ripple all the way to the top.

This is what keeps fragmentation in check: every freed block is greedily reabsorbed into the largest contiguous chunk it can join.

DEMO 2
Freeing a block — three cascading merges
ORDER 4
ORDER 3
ORDER 2
ORDER 1
step 0 An allocated order-1 block (rightmost). Its buddy is free. Press step to release it.
walk through →
§ 06 / Playground

Now you try.

A live 16-page region with a complete buddy allocator running in your browser. Click any button below to allocate; click an allocated block to free it. Watch the free lists and the trace log update in real time.

LIVE
interactive — 16 pages, max order 4
allocate →