A minimal Linux kernel module that demonstrates how the Linux kernel manages frequently allocated objects using a SLAB cache and linked-list based object tracking.
This project is designed as a learning-oriented introduction to:
- Linux kernel memory allocation
- SLAB allocator fundamentals
- Kernel object lifecycle
- Linked list management
- Memory cleanup
- Cache-friendly object allocation
The goal is to provide a simple but correct kernel-style implementation that helps understand the foundations of many kernel subsystems such as:
- inode cache
- dentry cache
- device registries
- object tracking systems
- embedded Linux memory management
In real Linux systems, creating and destroying objects repeatedly can be expensive.
Examples:
- Files
- Inodes
- Device structures
- Network objects
- Process-related structures
Instead of allocating everything from scratch every time, Linux often keeps reusable objects in memory.
This project demonstrates that concept using a tiny cache layer.
+--------------------+
| SLAB CACHE |
+----------+---------+
|
|
kmem_cache_alloc()
|
v
+----------------------+
| cache_obj |
+----------------------+
| id |
| name |
| created |
| list |
+----------------------+
|
|
v
Kernel Linked List
+-------+ +-------+
|alpha | -> | beta |
+-------+ +-------+
When loaded:
1. Create a SLAB cache
2. Allocate objects
3. Initialize objects
4. Insert objects into linked list
5. Print kernel log messages
When unloaded:
1. Walk linked list
2. Remove objects
3. Free objects
4. Destroy slab cache
5. Release all memory
Example object:
struct cache_obj {
int id;
char name[32];
unsigned long created;
struct list_head list;
};Each object contains:
| Field | Purpose |
|---|---|
| id | unique identifier |
| name | object name |
| created | creation timestamp |
| list | linked-list connection |
The kernel rarely uses ordinary heap allocation patterns for frequently reused kernel objects.
SLAB provides:
- object reuse
- reduced fragmentation
- improved cache locality
- predictable allocation behavior
- faster allocation paths
Instead of:
allocate
free
allocate
free
SLAB can reuse existing objects.
Module Load
|
v
Create SLAB Cache
|
v
Allocate Objects
|
v
Insert Into List
|
v
Use Objects
|
v
Module Unload
|
v
Free Objects
|
v
Destroy Cache
Generic memory allocator.
obj = kmalloc(sizeof(*obj), GFP_KERNEL);Use when:
- allocating buffers
- variable-sized memory
- one-time allocations
Free with:
kfree(obj);Object-oriented allocator.
obj = kmem_cache_alloc(cachep, GFP_KERNEL);Use when:
- allocating many objects of the same type
- building kernel subsystems
- improving cache locality
- reducing allocator overhead
Free with:
kmem_cache_free(cachep, obj);kmalloc()
Request
|
v
Allocate N Bytes
|
v
Return Memory
kmem_cache_alloc()
Request Object
|
v
+----------------+
| Slab Cache |
| free obj |
| free obj |
| free obj |
+----------------+
|
v
Return Object
SLAB caches may initialize objects automatically.
Example:
static void cache_ctor(void *obj)
{
struct cache_obj *c = obj;
c->id = -1;
c->created = jiffies;
INIT_LIST_HEAD(&c->list);
}Benefits:
- consistent initialization
- fewer bugs
- cleaner allocation code
Always verify allocation success.
Correct:
obj = kmem_cache_alloc(cachep, GFP_KERNEL);
if (!obj)
return -ENOMEM;Reason:
Allocation Failure
|
v
obj == NULL
|
v
Dereference?
|
v
Kernel Crash
-ENOMEMOut of memory.
-EINVALInvalid argument.
-EFAULTBad memory address.
-EBUSYResource busy.
Build module:
makeExpected output:
simple_cache.ko
If Secure Boot is enabled:
sudo /usr/src/linux-headers-$(uname -r)/scripts/sign-file \
sha256 \
~/kernel_keys/MOK.key \
~/kernel_keys/MOK.crt \
simple_cache.koInsert module:
sudo insmod simple_cache.koVerify:
lsmod | grep simple_cacheWatch logs in real time:
sudo dmesg -wExample:
CACHE: created object 1 alpha
CACHE: created object 2 beta
Unload module:
sudo rmmod simple_cacheObserve cleanup:
CACHE: freeing object 1
CACHE: freeing object 2
CACHE: cache destroyed
After studying this project, you should understand:
- kernel memory allocation
- object lifecycle management
- linked lists in kernel space
- slab allocator basics
- object reuse
- cleanup patterns
- constructor-based initialization
- safe error handling
This project models concepts used inside:
| Linux Component | Similar Concept |
|---|---|
| inode cache | cached objects |
| dentry cache | object registry |
| SLUB/SLAB | allocation backend |
| VFS metadata | object structures |
| kernel cleanup | destroy_all() |
.
├── Makefile
├── simple_cache.c
├── README.md
└── LICENSE
This project is intentionally small.
Its purpose is not to build a production cache but to demonstrate the core memory-management ideas that appear repeatedly throughout Linux kernel development.
Understanding this module provides a strong foundation for studying:
- SLAB/SLUB internals
- inode caches
- dentry caches
- device model internals
- networking object lifecycles
- advanced kernel memory management