Posts

Showing posts from 2018

Notification Chains in Linux Kernel

Notification chains in Linux kernel is a mechanism for the subsystems being informed by the events generated by the other subsystems. In other words, if you are developing a new driver in Linux and you want to listen some events from other driver and handle these events in your own driver, you need to use the Linux's notification chains mechanism. As an example, if you develop a driver and this driver needs to be informed about the net device events such as net device UP, DOWN events, the driver needs to register itself to the net device notifications with the  register_netdevice_notifier function. Similarly, it unregisters itself with the unregister_netdevice_notifier function. These functions are provided by the net device subsystem. The APIs and the data structures of notification chains are defined in the include/linux/notifier.h and  kernel/notifier.c files under linux source code. In this post, I am going to explain how this notification chains mechanism wo...

List Handling in Linux Kernel Development

If you want to get a brief information about how to use Linux's linked list data structure, please see the below sample code. You can check-out the code from the github location, build and run on your own Linux development environment. /* * my_object structure. */ struct my_object { int data; struct list_head list; }; /* * list_init() */ static int list_init ( void ) { struct my_object *tmp; /* Temporary object created for each list member */ struct my_object obj_list; /* List constructed with the tmp objects */ struct list_head *pos, *n; /* Position pointers in the list */ int i; printk(KERN_INFO "list init \n " ); /* * Initialize the list object. This object is used only as the * head of the list. It doesn't have any data in it. */ INIT_LIST_HEAD(&obj_list.li...

How to Use container_of in Linux Kernel Development

In this post, I will explain the usage of linux's container_of macro with a sample code. If you know address of a data structure's field, you can get the address of the instance of that data structure by using the container_of macro. This is the simplest explanation of this macro. Let's say you have a function called with a pointer parameter and you know that the pointer is pointing an address of a data structure field. struct my_object { int a; int b; } obj; function(&obj->b); If you want to access the other field "a" in the function, you need to use container_of macro. Let's see the below sample code and it's output to understand it easily. /* * my_object structure. */ struct my_object { int a; int b; int c; }; /* * container_test() * Test function for usage of container_of macro. * * This function is passed a pointer which points a field of * an allocated my_obj...