2

this is my first time posting a question on here, so be gentle. I'm delving into the interesting world of operating systems and wanted to try my hand at trying to write a linux kernel module. I came across this exercise in a textbook on the subject and wrote the following code in C:

#include<linux/list.h>
#include<linux/init.h>
#include<linux/kernel.h>
#include<linux/module.h>
#include<linux/types.h>
#include<linux/slab.h>

struct birthday {
    int day;
    int month;
    int year;
    struct list_head list;
}

static LIST_HEAD(birthday_list);

int simple_init(void) {
    struct birthday *ptr;
    int i;
    for(i = 0; i < 5; i++) {
        // create 5 birthday structs and add them to the list

        struct birthday *person;
        person = kmalloc(sizeof(*person), GFP_KERNEL);
        person->day = 22;
        person->month = 11;
        person->year = 1981;
        INIT_LIST_HEAD(&person->list);

        list_add_tail(&person->list, &birthday_list);
    }

    list_for_each_entry(ptr, &birthday_list, list) {
        // print the info from the structs to the log
        printk(KERN_INFO "%d, %d %d", ptr->month, ptr->day, ptr->year);
    }

    return 0;
}


void simple_exit(void) {
    struct birthday *ptr, *next;
    list_for_each_entry_safe(ptr, next, &birthday_list, list) {
        // delete structs and return memory
        list_del(&ptr->list);
        kfree(ptr);
    }
}

module_init(simple_init);
module_exit(simple_exit);

The issue I'm having is that the above code won't compile and I get the following errors:

In file included from /home/parv112281/Documents/operating-systems/chap-2/list-struct/list-struct.c:1:0:
include/linux/list.h:22:2: error: expected ‘;’, identifier or ‘(’ before ‘struct’
  struct list_head name = LIST_HEAD_INIT(name)
  ^
/home/parv112281/Documents/operating-systems/chap-2/list-struct/list-struct.c:15:8: note: in expansion of macro ‘LIST_HEAD’
 static LIST_HEAD(birthday_list);
        ^
make[2]: *** [/home/parv112281/Documents/operating-systems/chap-2/list-struct/list-struct.o] Error 1
make[1]: *** [_module_/home/parv112281/Documents/operating-systems/chap-2/list-struct] Error 2
make[1]: Leaving directory `/usr/src/linux-headers-3.16.0-30-generic'
make: *** [all] Error 2

The error that the compiler seems to be complaining about is in the list.h header file that defines a doubly linked list data structure for the linux kernel. I'm doubting there is an actual error in the kernel code here and I suspect that i'm just utilizing some function or macro here incorrectly. I would appreciate any help in fixing this problem.

Thanks, Parv

1 Answer 1

1

Two issues:

  1. To use kernel linked list, you need to include linux/list.h

  2. You forgot a ; when declaring struct birthday.

So this should work:

#include <linux/list.h>

struct birthday {
    int day;
    int month;
    int year;
    struct list_head list;
};
Sign up to request clarification or add additional context in comments.

3 Comments

I didn't change the struct order in the Q. And admittedly I'm not sure why it's critical to place struct list_head first. Could you elaborate a bit more?
The above was what I replied to a comment, which basically saying that it's critical to put the list_head at the beginning of the struct. That comment was then somehow deleted.
It makes pointer arithmetic easier: container_of() will be utilized just to static cast.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.