[LINUX] A small sample note of list_head

Premise

ubuntu# uname -a
Linux ubuntu 4.4.0-93-generic #116-Ubuntu SMP Fri Aug 11 21:17:51 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux
ubuntu# cat /etc/lsb-release
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=16.04
DISTRIB_CODENAME=xenial
DISTRIB_DESCRIPTION="Ubuntu 16.04.3 LTS"

I don't know how it works, so I'll look into it later. For the time being, I read the Linux code and didn't understand it well, so I made a note of only the minimum working code (no memory release processing).

code

kure.c


#include <linux/module.h>       /* MODULE, THIS_MODULE */
#include <linux/moduleparam.h>
#include <linux/kernel.h>       /* printk() */
#include <linux/errno.h>        /* error codes */
#include <linux/list.h>         /* list_*() */
#include <linux/slab.h>         /* kmalloc() */

MODULE_AUTHOR("kuredev");
MODULE_LICENSE("Dual BSD/GPL");

struct kurest
{
        int no;
        struct list_head list_head_;
};

struct kurest data1 = {
        .no = -1,
};

static int kure_init(void)
{
        printk(KERN_ALERT "Listtest init\n");
        struct kurest *data2, *data3;
        struct list_head *ptr;
        struct kurest *entry;
        INIT_LIST_HEAD(&data1.list_head_);

        data2 = kmalloc(sizeof(struct kurest), GFP_KERNEL);
        data2->no = 2;
        list_add(&data2->list_head_, &data1.list_head_);

        data3 = kmalloc(sizeof(struct kurest), GFP_KERNEL);
        data3->no = 3;
        list_add(&data3->list_head_, &data1.list_head_);

        list_for_each(ptr, &data1.list_head_){
                 entry = list_entry(ptr, struct kurest, list_head_);
                 printk("no: %d\n", entry->no);
        }

        return 0;
}


static void kure_exit(void)
{
        printk(KERN_ALERT "Listtest exit\n");
}

module_init(kure_init);
module_exit(kure_exit);

Makefile


KERNEL_DIR = /lib/modules/$(shell uname -r)/build
BUILD_DIR := $(shell pwd)
VERBOSE   := 0
obj-m := kure.o
all:
        make -C $(KERNEL_DIR) SUBDIRS=$(BUILD_DIR) KBUILD_VERBOSE=$(VERBOSE) modules
clean:
        rm -rf  *.o *.ko *.mod.c *.symvers *.order .tmp_versions .kure.*

Run

# make
# insmod kure.ko
# tail /var/log/syslog
Sep 18 23:12:16 ubuntu kernel: [23064.945863] Listtest init
Sep 18 23:12:16 ubuntu kernel: [23064.945869] no: 3
Sep 18 23:12:16 ubuntu kernel: [23064.945870] no: 2
Sep 18 23:12:21 ubuntu kernel: [23069.543457] Listtest exit

reference

How to use the list provided by the linux kernel --mmitou's diary http://d.hatena.ne.jp/mmitou/20120626/1340731801

Recommended Posts

A small sample note of list_head
A simple sample of pivot_table.
A small memorandum of openpyxl
A small note following printf
Just a note
A note about the python version of python virtualenv
[Note] Beginning of programming
Make a note of the list of basic Pandas usage
A note about __call__
[Note] Contents of shape [0], shape [1], shape [2]
A note about subprocess
A note about mprotect (2)
A note of trying a simple MCMC tutorial on PyMC3
note of bind mount
Python Note: The mystery of assigning a variable to a variable
A note on the default behavior of collate_fn in PyTorch
[Note] Import of a file in the parent directory in Python
A memorandum of kernel compilation
A note about KornShell (ksh)
[Note] CAD query sample code
A note about TensorFlow Introduction
Sample usage of Python pickle
A brief summary of Linux
A memorandum of using eigen3
A note about [python] __debug__
[python] A note that started to understand the behavior of matplotlib.pyplot