Two allocators
The kernel needs memory for its own things: page tables, process records, file records. It has no malloc, which is a user library. Linux uses two layers: a buddy allocator hands out whole pages, and a slab allocator cuts pages into small objects.
Memory is cut into pages of 4096 bytes. The buddy allocator keeps free blocks of 1, 2, 4, 8 ... pages, one free list per size, called the order. Our toy zone has 8 pages: one free block of order 3, 32 KiB.
A request
Kernel code calls kmalloc(400). There is no 400-byte size, so the request goes to the next size up, the 512-byte cache kmalloc-512. That cache has no free slot yet, so it asks the buddy allocator for one page.
No order-0 block is free, so the allocator splits the 8-page block in half again and again: 8 into 4 + 4, 4 into 2 + 2, 2 into 1 + 1. Page 0 goes to the cache; spare halves join their free lists.
The two halves of a split are buddies, and their page numbers differ in exactly one bit. Buddy = page number XOR block size: page 0 pairs with page 1, block 0-1 with block 2-3, block 0-3 with block 4-7. No searching needed.
Slabs of objects
The cache turns page 0 into a slab: 4096 bytes cut into 8 equal slots of 512 bytes, all on the slab's own free list. Equal slots leave no odd-sized gaps between objects, so nothing is lost to external fragmentation.
The cache hands slot 0 to the caller, and kmalloc returns its address in p. The caller uses 400 of the 512 bytes; the other 112 are internal fragmentation, the price of fixed sizes. The next 512-byte request just takes a free slot.
Giving it back
kfree(p) puts slot 0 back on the slab's free list. The page stays with the cache, so the next 512-byte request is quick and the buddy allocator is not involved at all.
No object in the slab is in use now, so the cache can give the page back. The buddy allocator checks page 0's buddy: free, so they merge into 2 pages, then 4, then 8. One whole 32 KiB block is free again.
The buddy allocator keeps pages in blocks that split and merge cheaply, so big runs of pages can be rebuilt. Slab caches serve the many small, same-sized kernel objects quickly, without spending a whole page on each.
What happens
;; step 1/10 · two-layers who asks what it needs page tables whole pages process records small objects, thousands of them file records small objects (inodes, dentries) layer 1 buddy allocator pages, in blocks of 1, 2, 4, 8 ... layer 2 slab allocator small same-size objects, cut from pages xv6 (MIT 6.1810) has only layer 1: a free list of 4096-byte pages.
The zone is a toy of 8 pages so every block fits on the table. Real Linux zones hold millions of pages and keep free lists for orders 0 to 10; /proc/buddyinfo shows how many free blocks of each order there are. The figure's slab is one page. Linux's allocator today is SLUB, and it often uses slabs of several pages even for 512-byte objects; the pagesperslab column of /proc/slabinfo shows the real number. Bonwick's original slab is "one or more pages".