An interrupt is a signal that tells the CPU that a significant event has occurred and needs the attention of the CPU. There are two types of interrupts that are provided in the Linux Operating system. Hardware Interrupts – If the Interrupt is coming from an external hardware device connected to the CPU, the interrupt […]
Sys File System (sysfs) in the Linux Kernel
The sysfs is a RAM based virtual file system and provides details on various kernel objects. It provides a means to export kernel data structures, attributes and their linkages between the kernel objects and the user space. if “CONFIG_SYSFS” is enabled during build, “sysfs” is in-built into the linux image. The file system can also […]
Example Linux Memory Allocation Code
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. […]