Skip to content

aj333git/linux_kernel_kobjex2

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Embedded Linux Caching Layer

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

Motivation

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.


Architecture

                +--------------------+
                |    SLAB CACHE      |
                +----------+---------+
                           |
                           |
                 kmem_cache_alloc()
                           |
                           v

              +----------------------+
              |      cache_obj       |
              +----------------------+
              | id                   |
              | name                 |
              | created              |
              | list                 |
              +----------------------+
                           |
                           |
                           v

                   Kernel Linked List

              +-------+    +-------+
              |alpha  | -> | beta  |
              +-------+    +-------+

What This Module Does

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

Object Structure

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

Why Use SLAB?

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.


Lifecycle Overview

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

kmalloc() vs kmem_cache_alloc()

kmalloc()

Generic memory allocator.

obj = kmalloc(sizeof(*obj), GFP_KERNEL);

Use when:

  • allocating buffers
  • variable-sized memory
  • one-time allocations

Free with:

kfree(obj);

kmem_cache_alloc()

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);

Internal Comparison

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

Constructor Support

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

Error Handling

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

Common Error Codes

-ENOMEM

Out of memory.

-EINVAL

Invalid argument.

-EFAULT

Bad memory address.

-EBUSY

Resource busy.


Building

Build module:

make

Expected output:

simple_cache.ko

Secure Boot Module Signing

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.ko

Loading Module

Insert module:

sudo insmod simple_cache.ko

Verify:

lsmod | grep simple_cache

Monitor Kernel Messages

Watch logs in real time:

sudo dmesg -w

Example:

CACHE: created object 1 alpha
CACHE: created object 2 beta

Removing Module

Unload module:

sudo rmmod simple_cache

Observe cleanup:

CACHE: freeing object 1
CACHE: freeing object 2
CACHE: cache destroyed

Learning Objectives

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

Real Kernel Mapping

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()

Repository Structure

.
├── Makefile
├── simple_cache.c
├── README.md
└── LICENSE

Key Takeaway

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

About

process and thread

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors