The below code provides an example allocation of “kmalloc“, “vmalloc“, “kcalloc” and “kzalloc” APIs in the Linux kernel code. for the different APIs and information about the APIs, refer the article – Allocating Memory in the Linux Kernel Module The “kmalloc” API provides a contiguous block of memory in both virtual and physical address space […]
Linux Kernel – Adding Debugfs support to a Linux Kernel Module
The below code creates a directory in “/sys/kernel/debug” and a value is written to file and read from file. The code and the “Makefile” for compiling the code are provided below: In the above code example, the file created is “value” inside the folder “/sys/kernel/debug/debug_dir_example“. When “debugfs_create_file” API is invoked, the “data” parameter (accessed in […]
Mutex API List and Sample API Code
A few of the mutex APIs are provided below #define mutex_init(mutex) Initialize the mutex to unlocked state. This Macro invokes the function __mutex_init void __sched mutex_lock(struct mutex *lock) Acquire the lock void __sched mutex_unlock(struct mutex *lock) Release the Mutex int __sched mutex_lock_interruptible(struct mutex *lock) Acquire the Mutex, interruptible by signals. If a signal is delivered […]
Semaphore Example implementation
The below code examples provide a simple usage of a semaphore for static and dynamic initialization. This code does not provide an actual use case but only indicates the manner in which the API needs to be invoked. Static semaphore initialization and use: Dynamic Semaphore initialization and use In the following articles, we will look […]
Semaphore Structures and Semaphore APIs in Linux Kernel
The Semaphore structure is defined in the header file /include/linux/semaphore.h. The structure is shown below lock – spinlock that is to acquire the resource count – The count of the number of instances of the resource the semaphore provides access wait_list – a list of processes that are waiting to obtain the lock The semaphore […]
Semaphores in the Linux Kernel
Another synchronization mechanism that is provided in the Linux kernel is the Semaphore. It provides the ability for multiple kernel processes/threads to access a resource simultaneously using a “count” value. If the “count” value is 1, then the semaphore is similar to a mutual exclusion lock (Mutex). However, a Semaphore does not have the same […]
Fast Path, Mid Path and Slow Path in Mutex
When the Kernel code is invoked to gain control of a Mutex lock, the kernel attempts it in three ways one after the other. They are Fast Path Mid Path Slow Path In Order to understand the above mechanisms, it is necessary to look at the mutex structure and a few other mutex related structures […]
Mutexes in the Linux kernel
Mutex means mutual Exclusion. The mutex provides a mechanism to protect critical data. The mutex allows a process/thread to go to sleep state in case the lock cannot be acquired immediately. This is in contrast to a spinlock wherein the calling process/thread keeps spinning at obtaining the lock and does not go down to sleep. […]
Mutex API
The Mutex API can be defined statically using the “DEFINE_MUTEX” MACRO. It can be dynamically defined during process/thread run using the “mutex_init” MACRO. Both methods are shown below DEFINE_MUTEX(mutex_name); /*statically assign mutex by name- mutex_name*/ mutex_init(mutex); /* dynamically assign the mutex */ Both these Macros are placed in the header file – mutex.h. The code […]
Linux Kernel – Debug File System
The Linux Debug File System is a virtual memory File system that was developed so that debug kernel information can be accessed in user space whenever desired. It is a RAM based File system. The Debug File system will be enabled in the Linux kernel build if “CONFIG_DEBUG_FS” is enabled in the kernel config. The […]