Building & Debugging a Linux Kernel Module: Process Logger and Memory Monitor

Search for a command to run...

No comments yet. Be the first to comment.
Modern software systems often separate control logic from high-performance execution logic. This design is common in networking, distributed systems, operating systems, storage engines, and embedded s

Introduction Engineering software often combines multiple programming languages to leverage their individual strengths. A common approach is to implement computational algorithms in native C or C++ wh

Deadlocks are one of the most common synchronization problems encountered in operating systems and concurrent programming. Although the concept is frequently introduced in textbooks, observing it insi

Memory and resource allocation are fundamental operations inside the Linux kernel. Whether assigning device IDs, managing CPU masks, allocating interrupt vectors, or tracking hardware resources, the k

The Linux scheduler is one of the most important components of the operating system. Every running program, background service, and kernel thread eventually interacts with the scheduler. In this artic

Project Name: proc_mem_logger
This project demonstrates how to write, debug, and safely operate a loadable Linux kernel module that periodically inspects system state from kernel space.
The module logs:
All output is written to the kernel log buffer and can be inspected using dmesg or journalctl.
This project is ideal for developers learning Linux kernel internals, kernel timers, process structures, and memory APIs.
Write a loadable kernel module that:
dmesg) No external hardware is required.
Safe to test on:
struct timer_listmod_timer()for_each_processtask->comm)si_meminfo() to read system RAM and swapprintk(KERN_INFO ...)init and exit handlersrmmodLoad the module:
sudo insmod proc_mem_logger.ko
Check kernel output:
sudo dmesg | tail -50
Unload the module:
sudo rmmod proc_mem_logger
Watch logs in real time:
sudo dmesg -W
# or
sudo journalctl -k -f
The module was intended to log:
Symptoms observed:
dmesg0 MBls -l /dev/kmsg
/dev/kmsg is the kernel log buffer interface.
Root must have read/write access to see kernel messages.
echo "TEST_KMSG" | sudo tee /dev/kmsg
Verify:
dmesg | tail -n 20
sudo journalctl -k | tail -n 20
Result:TEST_KMSG appeared, confirming kernel logging was working correctly.
echo 7 | sudo tee /proc/sys/kernel/printk
Explanation:
KERN_INFO messages (level 6) may be hiddensudo rmmod proc_mem_logger 2>/dev/null
sudo insmod proc_mem_logger.ko
sudo dmesg -W
After this, kernel messages began appearing as expected.
si_meminfo() returns values in pages, not bytes.
Incorrect code:
i.freeram >> 20;
This shifts the page count directly, resulting in 0.
(i.freeram * PAGE_SIZE) >> 20;
(i.freeswap * PAGE_SIZE) >> 20;
freeram * PAGE_SIZE โ bytes>> 20 โ megabytes| Command | Purpose | |
ls -l /dev/kmsg | Verify kernel log buffer | |
| `echo "TEST_KMSG" \ | sudo tee /dev/kmsg` | Test kernel logging |
dmesg -W | Watch kernel logs live | |
journalctl -k -f | Follow kernel logs via systemd | |
echo 7 > /proc/sys/kernel/printk | Enable INFO-level logs | |
sudo insmod | Load kernel module | |
sudo rmmod | Remove kernel module |
After applying all fixes:
for_each_process safely iterates through all taskssi_meminfo() reports memory in pages, not bytestask->utime and task->stime/proc/proc_mem_logger instead of printkThis project is an excellent foundation for:
The module is safe to run on:
It can be scaled further with /proc interfaces, CPU tracking, and alerting mechanisms.