We'll take a quick deviation – very important, though – before trying out a kernel module using the basic slab APIs. It's important to understand where exactly the memory allocated by the k[m|z]alloc() APIs is coming from. Well, it's from the slab caches, yes, but which ones exactly? A quick grep on the output of sudo vmstat -m reveals this for us (the following screenshot is on our x86_64 Ubuntu guest):
That's very interesting! The kernel has a slew of dedicated slab caches for generic kmalloc memory of varying sizes, ranging from 8,192 bytes down to a mere 8 bytes! This tells us something – with the page allocator, if we had requested, say, 12 bytes of memory, it would have ended up giving us a whole page (4 KB) – the wastage is just too much. Here, with the slab allocator...