Reputation: 27
I'm trying to create a Linked List using linux/list.h in the Kernel level. My code compiles but when I try to add more than one Nodes into the linked list it causes a Kernel Oops. Here is my Kernel Level Code:
//global defs
struct Node {
char *data;
struct list_head list;
};
LIST_HEAD(mylinkedlist);
DEFINE_MUTEX(mut);
asmlinkage long write(const void __user *data, long len){
//create new space in memory enough to fit data
void *ptr = kmalloc(len, GFP_KERNEL);
//create the user space pointer to kernel space pointer
int verif = copy_from_user(ptr, data, len);
if(verif != 0){
return -EFAULT;
}
struct Node first = {ptr, LIST_HEAD_INIT(first.list)};
//wait for mutex to be available
mutex_lock_interruptible(&mut);
list_add_tail(&first.list, &mylinkedlist);
//release the mutex
mutex_unlock(&mut);
return 0;
and my userland program looks like:
long hello_syscall(void) {
char *arg = "Hello";
return syscall(351, "Hello", sizeof(arg));
}
It all compiles but when I try and run the userland program more than once it gives shows that I have a kernel oops. I've created a gist of the error message that the OS gives me when it occurs: https://gist.github.com/anonymous/7217210
Upvotes: 1
Views: 621
Reputation: 7
Linux kernel is mostly written in the C language. And you can use structure in this shape :
struct _Node
{
char *data;
struct NODE* list;
}NODE,PNODE*;
Upvotes: 0
Reputation: 6984
struct Node first = {ptr, LIST_HEAD_INIT(first.list)};
allocates first
on the stack and it vanishes when function is left. mylinkedlist
will point to garbage hence and next list operation will crash.
Upvotes: 1