Creating a custom slab cache
As explained in detail in the previous chapter, a key design concept behind slab caches is the powerful idea of object caching. By caching frequently used objects – data structures, really – the memory allocation/free work for those objects are much quicker and thus overall performance receives a boost.
So, think about this: what if we’re writing a driver and within it, we notice that a certain data structure (an object) is very frequently allocated and freed? Normally, we would use the usual kzalloc()
(or kmalloc()
) followed by the kfree()
APIs to allocate and free this object. Some good news: the Linux kernel sufficiently exposes the slab layer API to us, the module/driver authors, allowing us to create custom slab caches. In this section, you’ll learn how you can leverage this powerful feature.
Creating and using a custom slab cache within a kernel module
In this section, we will create, use, and subsequently...