Linux Kernel Module for Memory and Per-Process RSS via /proc

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

User-space utilities such as top, htop, and free present a polished view of system memory usage, but they abstract away substantial kernel-level mechanics. To understand how Linux actually tracks memory—both globally and per process—I implemented a Linux kernel module that exposes system-wide memory statistics and per-process Resident Set Size (RSS) directly through /proc.
The result is a minimal, educational kernel-space alternative to user-space monitoring tools, designed explicitly to reinforce core Linux kernel concepts rather than replace existing utilities.
The module registers a virtual /proc entry that reports:
All data is gathered inside the kernel, without relying on /proc/meminfo or /proc/[pid]/status as intermediaries.
/proc InterfaceThe module creates a virtual proc entry:
This file does not exist on disk. Its contents are generated dynamically each time it is read.
The kernel’s global memory state is retrieved using si_meminfo():
These values come directly from kernel accounting structures, ensuring accuracy and eliminating any user-space interpretation.
To report per-process memory usage, the module:
for_each_processget_mm_rss(task->mm)For each eligible process, the output includes:
comm)This mirrors how tools like top internally derive memory usage, but without leaving kernel space.
seq_fileThe module uses the seq_file API to emit output:
This is the canonical mechanism for /proc entries that may grow beyond a single page.
Building this module clarified several core Linux internals that are often hidden by user-space abstractions.
mm_structmm_struct levelmm_structUnderstanding this explains why some processes appear in ps but not in memory listings.
/proc/proc files are generated on demandThis reinforces that /proc is an interface—not a filesystem in the traditional sense.
for_each_process walks the global task listEven read-only inspection requires disciplined kernel programming.
seq_file Is Mandatory for Real ModulesManual string buffers are unsafe for variable-length output. The seq_file API:
/proc modulesOn UEFI systems with Secure Boot enabled:
Resolution involved:
sign-filemokutilThis highlighted the security model modern kernels enforce by default.
vermagic MismatchesSeveral build failures were traced to:
This reinforced the importance of:
uname -r exactlyKernel builds exposed why:
Mixing human-oriented build steps with kernel build logic leads to fragile modules.
User-space tools provide convenience. Kernel modules provide truth.
By implementing this module, the following became concrete rather than theoretical:
This project was not about performance or production deployment—it was about understanding Linux from the inside out.
This kernel module demonstrates:
/proc entrysi_meminfofor_each_processmm_structseq_file for robust outputFor anyone serious about systems programming, kernel development, or low-level performance work, projects like this provide insight that no user-space API can fully replicate.
The complete source code for this Linux kernel module is available on GitHub Repository
If needed, this can be extended further to:
debugfs