In the following code snippet, take a look at the demo kernel module code (found at ch8/slab1/). In the init code, we merely perform a couple of slab layer allocations (via the kmalloc() and kzalloc() APIs), print some information, and free the buffers in the cleanup code path (of course, the full source code is accessible at this book's GitHub repository). Let's look at the relevant parts of the code step by step.
At the start of the init code of this kernel module, we initialize a global pointer (gkptr) by allocating 1,024 bytes to it (remember: pointers have no memory!) via the kmalloc() slab allocation API. Notice that, as we're certainly running in process context here, and it is thus "safe to sleep," we use the GFP_KERNEL flag for the second parameter (just in case you want to refer back, the earlier section, The GFP flags – digging deeper, has it...