Linux Device Driver Tutorial Part 18 – Linked List in Linux Kernel Part 2

This is the Series on Linux Device Driver. The aim of this series is to provide easy and practical examples that anyone can understand. In our previous tutorial, we have seen the functions used in Kernel Linked List. So this is the Linux Device Driver Tutorial Part 18 – Example Linked List in Linux Kernel which is the continuation (Part 2) of the Previous Tutorial.

Linux Device Driver Tutorial Part 18 – Example Linked List in Linux Kernel

If you don’t know the functions used in the linked list, please refer to this previous tutorial for the detailed explanation about all linked list functions.

So now we can directly enter into the Linux Linked List Kernel programming. I took the source code form the previous tutorial. First, I will explain how this code works.

  1. When we write the value to our device file using echo value > /dev/etx_value, it will invoke the interrupt. Because we configured the interrupt by using the software. If you don’t know how it works, please refer to this tutorial.
  2. The interrupt will invoke the ISR function.
  3. In ISR we are allocating work to the Workqueue.
  4. Whenever Workqueue executing, we are creating the Linked List Node and adding the Node to the Linked List.
  5. When we are reading the driver using cat /dev/etx_device, printing all the nodes which are present in the Linked List using traverse.
  6. When we are removing the driver using rmmod, it will remove all the nodes in Linked List and free the memory.

Note: We are not using the sysfs functions. So I kept empty sysfs functions.

Creating Head Node

This will create the head node in the name of Head_Node and initialize that.

Creating Node and add that into Linked List

This will create the node, assign the data to its members. Then finally add that node to the Linked List using list_add_tail. (This part will be present in the workqueue function)

Traversing Linked List

Here, we are traversing each node using list_for_each_entry and print those values. (This part will be present in the read function)

Deleting Linked List

This will traverse each node using  list_for_each_entry_safe and delete that using list_del. Finally, we need to free the memory which is allocated using kmalloc.


Driver Source Code


Building and Testing Driver

  • Build the driver by using Makefile (sudo make)
  • Load the driver using sudo insmod driver.ko
  • sudo su
  • To trigger the interrupt read device file (cat /dev/etx_device)
  • Now see the Dmesg (dmesg)

  • By this time there are no nodes available.
  • So now write the value to driver using echo 10 > /dev/etx_device
  • By this time, One node has been added to the linked list.
  • To test that read the device file using cat /dev/etx_device
  • Now see the Dmesg (dmesg)

  • Our value has added to the list.
  • You can also write many times to create and add the node to the linked list
  • Unload the module using rmmod driver

In our next tutorial, we will discuss kernel threads.

0 0 vote
Article Rating
Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Newest Most Voted
Inline Feedbacks
View all comments
August 13, 2019 2:30 AM

where can i find my inserted values except dmesg ?
and where and when we should use this linux list ?

Kunapareddy Jeevan
Kunapareddy Jeevan
July 8, 2020 9:47 PM

please add circular buffers syntax and sample code for it

Would love your thoughts, please comment.x
%d bloggers like this: