Linux kernel linked list

This post gives a sample file used to play with Linux kernel linked list <linux/list.h>, as well as a makefile used to build this kernel module. lsmod, insmod, rmmod and dmesg are needed. The reference is the book “Operating System Concepts” 9E (chapter 2).

[root@daveti kernel_module_list]# cat birthdayList.c
/*
 * A kernel linked list usage example with kernel module
 * Reference: OSC9E-CH2
 * April 2, 2013
 * daveti
 *
 */
#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/list.h>
#include <linux/slab.h>

#define NUM_OF_PERSON    5

/* Example struct we will use. */
typedef struct _birthday {
    int day;
    int month;
    int year;
    struct list_head list;
}birthday;

/* Declare and init the head of the linked list. */
LIST_HEAD(birthday_list);

/* This function is called when the module is loaded. */
int birthdayList_init(void)
{
           printk(KERN_INFO “Loading Modulen”);

    /* Allocate 5 birthdays from kernel */
    birthday *person;
    int i = 0;
    for(i = 0; i < NUM_OF_PERSON; i++)
    {
        /* Request malloc to the kernel. */
        person = kmalloc(sizeof(*person), GFP_KERNEL);
        /* Assign value to the struct. */
        person->day = i+1;
        person->month = i+1;
        person->year = i+1;
        /* Init the list within the struct. */
        INIT_LIST_HEAD(&person->list);
        /* Add this struct to the tail of the list. */
        list_add_tail(&person->list, &birthday_list);
    }

    printk(KERN_INFO “Display the list:n”);

    /* Go thru the list and print. */
    birthday *ptr;
    list_for_each_entry(ptr, &birthday_list, list)
    {
        printk(KERN_INFO “day: %d, month: %d, year: %dn”,
                ptr->day,
                ptr->month,
                ptr->year);
    }

    printk(KERN_INFO “Display donen”);

           return 0;
}

/* This function is called when the module is removed. */
void birthdayList_exit(void)
{
    printk(KERN_INFO “Removing Modulen”);

    /* Go thru the list and free the memory. */
    birthday *ptr, *next;
    list_for_each_entry_safe(ptr, next, &birthday_list, list)
    {
        printk(KERN_INFO “Removing – day: %d, month: %d, year: %dn”,
                ptr->day,
                ptr->month,
                ptr->year);
        list_del(&ptr->list);
        kfree(ptr);
    }

    printk(KERN_INFO “Memory free donen”);
}

/* Macros for registering module entry and exit points. */
module_init( birthdayList_init );
module_exit( birthdayList_exit );

MODULE_LICENSE(“GPL”);
MODULE_DESCRIPTION(“BirthdayList Module”);
MODULE_AUTHOR(“daveti”);

[root@daveti kernel_module_list]# cat Makefile
obj-m += birthdayList.o
all:
    make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
clean:
    make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
[root@daveti kernel_module_list]#

About daveti

Interested in kernel hacking, compilers, machine learning and guitars.
This entry was posted in OS and tagged , , , . Bookmark the permalink.

2 Responses to Linux kernel linked list

  1. Muhammad Abdul Haseeb says:

    Can we also do this task without using for loop ? if Yes, then how it will be done.

  2. James says:

    Compilation results in errors galore

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

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