Though several APIs to perform memory allocation and freeing exist within the slab layer, there are just a couple of really key ones, with the rest falling into a "convenience or helper" functions category (which we will of course mention later). The key slab allocation APIs for the kernel module or device driver author are as follows:
#include <linux/slab.h>
void *kmalloc(size_t size, gfp_t flags);
void *kzalloc(size_t size, gfp_t flags);
Be sure to include the <linux/slab.h> header file when using any slab allocator APIs.
The kmalloc() and kzalloc() routines tend to be the most frequently used APIs for memory allocation within the kernel. A quick check – we're not aiming to be perfectly precise – with the very useful cscope(1) code browsing utility on the 5.4.0 Linux kernel source tree reveals the (approximate) frequency of usage: kmalloc() is called around 4,600...