I'm developing a module in kernel that uses kmem_cache. While developing and testing, it's possible that something goes wrong or I want to quit the module while there are still some allocations from the cache that weren't freed.
Calling kmem_cache_destroy
with a cache that still has elements throws a protection fault in dmesg: kmem_cache_destroy <my_cache>: Slab cache still has objects
along with the usual huge dump.
Is it possible to somehow remove all elements from a kmem_cache to avoid this issue?
Something like this reproduces the issue:
#include <linux/slab.h>
static struct kmem_cache *cache;
cache = kmem_cache_create("my_cache", 8, 4, 0, null_constructor);
c = kmem_cache_alloc(cmd_cache, GFP_KERNEL);
c1 = kmem_cache_alloc(cmd_cache, GFP_KERNEL);
//TODO: free all elements of the cache, without referencing every single object allocated
kmem_cache_destroy(cache);