Skip to content

Instantly share code, notes, and snippets.

@dhilst
Last active December 19, 2015 14:48
Show Gist options
  • Save dhilst/5971438 to your computer and use it in GitHub Desktop.
Save dhilst/5971438 to your computer and use it in GitHub Desktop.
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/list.h>
#include <linux/slab.h>
#define MODULE_NAME "llist"
#define DEBUG(msg, args...) printk(KERN_INFO MODULE_NAME " %s " msg, __func__, ##args)
struct foo_s {
int index;
struct list_head head;
};
/*
* Meh big memory leak here I KNOW!
*/
int init_module(void)
{
int i;
struct foo_s *foo = kmalloc(sizeof(struct foo_s), GFP_KERNEL);
struct foo_s *tmp;
struct list_head *lptr;
DEBUG("");
if (!foo) {
DEBUG("kmalloc fail");
return -1;
}
INIT_LIST_HEAD(&foo->head);
foo->index = 0;
for (i = 1; i < 10; i++) {
DEBUG("for(%d)", i);
tmp = kmalloc(sizeof(struct foo_s),
GFP_KERNEL);
if (!tmp)
return -1;
INIT_LIST_HEAD(&tmp->head);
tmp->index = i;
list_add_tail(&tmp->head, &foo->head);
}
list_for_each(lptr, &foo->head) {
struct foo_s *fptr = list_entry(lptr,
struct foo_s,
head);
DEBUG("list_for_each(%d)", fptr->index);
}
return 0;
}
void cleanup_module(void)
{
return;
}
obj-m += llist.o
KDIR ?= /lib/modules/$(shell uname -r)/build
all:
make -C $(KDIR) M=$(PWD) modules
clean:
make -C $(KDIR) M=$(PWD) clean
[ 8467.394522] llist init_module
[ 8467.394527] llist init_module for(1)
[ 8467.394534] llist init_module for(2)
[ 8467.394535] llist init_module for(3)
[ 8467.394538] llist init_module for(4)
[ 8467.394540] llist init_module for(5)
[ 8467.394543] llist init_module for(6)
[ 8467.394545] llist init_module for(7)
[ 8467.394547] llist init_module for(8)
[ 8467.394549] llist init_module for(9)
[ 8467.394552] llist init_module list_for_each(1)
[ 8467.394554] llist init_module list_for_each(2)
[ 8467.394556] llist init_module list_for_each(3)
[ 8467.394558] llist init_module list_for_each(4)
[ 8467.394561] llist init_module list_for_each(5)
[ 8467.394563] llist init_module list_for_each(6)
[ 8467.394566] llist init_module list_for_each(7)
[ 8467.394567] llist init_module list_for_each(8)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment