Skip to content

Commit 6c24127

Browse files
committed
split kernel module api docs to README
1 parent 8f37cd0 commit 6c24127

File tree

18 files changed

+490
-296
lines changed

18 files changed

+490
-296
lines changed

README.adoc

Lines changed: 362 additions & 94 deletions
Large diffs are not rendered by default.

kernel_module/README.adoc

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,7 @@
11
= kernel_module
22

3-
. Modules
4-
.. link:params.c[]
5-
.. link:vermagic.c[]
6-
.. link:vermagic_fail.c[]
7-
.. link:module_init.c[]
8-
.. link:module_info.c[]
9-
.. Module dependencies
10-
... link:dep.c[]
11-
... link:dep2.c[]
3+
Our kernel modules!
4+
125
. Asynchronous
136
.. link:irq.c[]
147
.. link:schedule.c[]

kernel_module/dep.c

Lines changed: 6 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,76 +1,22 @@
1-
/*
2-
Exports the lkmc_dep which dep2.ko uses.
1+
/* https://github.com/cirosantilli/linux-kernel-module-cheat#kernel-module-dependencies */
32

4-
insmod /dep.ko
5-
# dmesg => 0
6-
# dmesg => 0
7-
# dmesg => ...
8-
insmod /dep2.ko
9-
# dmesg => 1
10-
# dmesg => 2
11-
# dmesg => ...
12-
rmmod dep
13-
# Fails because dep2 uses it.
14-
rmmod dep2
15-
# Dmesg stops incrementing.
16-
rmmod dep
17-
18-
sys visibility:
19-
20-
dmesg -n 1
21-
insmod /dep.ko
22-
insmod /dep2.ko
23-
ls -l /sys/module/dep/holders
24-
# => ../../dep2
25-
cat refcnt
26-
# => 1
27-
28-
proc visibility:
29-
30-
grep lkmc_dep /proc/kallsyms
31-
32-
Requires "CONFIG_KALLSYMS_ALL=y".
33-
34-
depmod:
35-
36-
grep dep "/lib/module/"*"/depmod"
37-
# extra/dep2.ko: extra/dep.ko
38-
# extra/dep.ko:
39-
modprobe dep
40-
# lsmod
41-
# Both dep and dep2 were loaded.
42-
43-
TODO: at what point does buildroot / busybox generate that file?
44-
*/
45-
46-
#include <linux/delay.h> /* usleep_range */
3+
#include <linux/debugfs.h>
474
#include <linux/kernel.h>
48-
#include <linux/kthread.h>
495
#include <linux/module.h>
506

51-
int lkmc_dep = 0;
7+
u32 lkmc_dep = 0;
528
EXPORT_SYMBOL(lkmc_dep);
53-
static struct task_struct *kthread;
54-
55-
static int work_func(void *data)
56-
{
57-
while (!kthread_should_stop()) {
58-
pr_info("%d\n", lkmc_dep);
59-
usleep_range(1000000, 1000001);
60-
}
61-
return 0;
62-
}
9+
static struct dentry *debugfs_file;
6310

6411
static int myinit(void)
6512
{
66-
kthread = kthread_create(work_func, NULL, "mykthread");
67-
wake_up_process(kthread);
13+
debugfs_file = debugfs_create_u32("lkmc_dep", S_IRUSR | S_IWUSR, NULL, &lkmc_dep);
6814
return 0;
6915
}
7016

7117
static void myexit(void)
7218
{
73-
kthread_stop(kthread);
19+
debugfs_remove(debugfs_file);
7420
}
7521

7622
module_init(myinit)

kernel_module/dep2.c

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,21 @@
1-
#include <linux/delay.h> /* usleep_range */
1+
/* https://github.com/cirosantilli/linux-kernel-module-cheat#kernel-module-dependencies */
2+
3+
#include <linux/debugfs.h>
24
#include <linux/kernel.h>
3-
#include <linux/kthread.h>
45
#include <linux/module.h>
56

6-
extern int lkmc_dep;
7-
static struct task_struct *kthread;
8-
9-
static int work_func(void *data)
10-
{
11-
while (!kthread_should_stop()) {
12-
usleep_range(1000000, 1000001);
13-
lkmc_dep++;
14-
}
15-
return 0;
16-
}
7+
extern u32 lkmc_dep;
8+
static struct dentry *debugfs_file;
179

1810
static int myinit(void)
1911
{
20-
kthread = kthread_create(work_func, NULL, "mykthread");
21-
wake_up_process(kthread);
12+
debugfs_file = debugfs_create_u32("lkmc_dep2", S_IRUSR | S_IWUSR, NULL, &lkmc_dep);
2213
return 0;
2314
}
2415

2516
static void myexit(void)
2617
{
27-
kthread_stop(kthread);
18+
debugfs_remove(debugfs_file);
2819
}
2920

3021
module_init(myinit)

kernel_module/init_module.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/* https://github.com/cirosantilli/linux-kernel-module-cheat#init_module */
2+
3+
#include <linux/module.h>
4+
#include <linux/kernel.h>
5+
6+
int init_module(void)
7+
{
8+
pr_info("init_module\n");
9+
return 0;
10+
}
11+
12+
void cleanup_module(void)
13+
{
14+
pr_info("cleanup_module\n");
15+
}
16+
MODULE_LICENSE("GPL");

kernel_module/module_info.c

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,22 @@
1-
/*
2-
- https://stackoverflow.com/questions/4839024/how-to-find-the-version-of-a-compiled-kernel-module/42556565#42556565
3-
- https://stackoverflow.com/questions/19467150/significance-of-this-module-in-linux-driver/49812248#49812248
4-
5-
Usage:
6-
7-
insmod /module_info.ko
8-
# dmesg => name = module_info
9-
# dmesg => version = 1.0
10-
cat /sys/module/module_info/version
11-
# => module_info
12-
modinfo /module_info.ko | grep -E '^version:'
13-
# => version: 1.0
14-
*/
1+
/* https://github.com/cirosantilli/linux-kernel-module-cheat#module_info */
152

163
#include <linux/module.h>
174
#include <linux/kernel.h>
185

196
static int myinit(void)
207
{
218
/* Set by default based on the module file name. */
22-
pr_info("name = %s\n", THIS_MODULE->name);
9+
pr_info("name = %s\n", THIS_MODULE->name);
2310
pr_info("version = %s\n", THIS_MODULE->version);
11+
/* ERROR: nope, not part of struct module. */
12+
/*pr_info("asdf = %s\n", THIS_MODULE->asdf);*/
2413
return 0;
2514
}
2615

2716
static void myexit(void) {}
2817

2918
module_init(myinit)
3019
module_exit(myexit)
20+
MODULE_INFO(asdf, "qwer");
3121
MODULE_VERSION("1.0");
3222
MODULE_LICENSE("GPL");

kernel_module/module_init.c

Lines changed: 0 additions & 26 deletions
This file was deleted.

kernel_module/params.c

Lines changed: 27 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,53 @@
1-
/*
2-
Allows passing parameters at insertion time.
3-
4-
Those parameters can also be read and modified at runtime from /sys.
5-
6-
insmod /params.ko
7-
# dmesg => 0 0
8-
cd /sys/module/params/parameters
9-
cat i
10-
# => 1 0
11-
printf 1 >i
12-
# dmesg => 1 0
13-
rmmod params
14-
15-
insmod /params.ko i=1 j=1
16-
# dmesg => 1 1
17-
rmmod params
18-
19-
modinfo
20-
/params.ko
21-
# Output contains MODULE_PARAM_DESC descriptions.
22-
23-
modprobe insertion can also set default parameters via the /etc/modprobe.conf file. So:
24-
25-
modprobe params
26-
27-
Outputs:
28-
29-
12 34
30-
*/
1+
/* https://github.com/cirosantilli/linux-kernel-module-cheat#kernel-module-parameters */
312

3+
#include <linux/debugfs.h>
324
#include <linux/delay.h> /* usleep_range */
335
#include <linux/kernel.h>
34-
#include <linux/kthread.h>
356
#include <linux/module.h>
7+
#include <linux/seq_file.h> /* seq_read, seq_lseek, single_release */
368
#include <uapi/linux/stat.h> /* S_IRUSR | S_IWUSR */
379

38-
static int i = 0;
39-
static int j = 0;
10+
static u32 i = 0;
11+
static u32 j = 0;
4012
module_param(i, int, S_IRUSR | S_IWUSR);
4113
module_param(j, int, S_IRUSR | S_IWUSR);
4214
MODULE_PARM_DESC(i, "my favorite int");
4315
MODULE_PARM_DESC(j, "my second favorite int");
4416

45-
static struct task_struct *kthread;
17+
static struct dentry *debugfs_file;
4618

47-
static int work_func(void *data)
19+
static int show(struct seq_file *m, void *v)
4820
{
49-
while (!kthread_should_stop()) {
50-
pr_info("%d %d\n", i, j);
51-
usleep_range(1000000, 1000001);
52-
}
21+
char kbuf[18];
22+
int ret;
23+
24+
ret = snprintf(kbuf, sizeof(kbuf), "%d %d", i, j);
25+
seq_printf(m, kbuf);
5326
return 0;
5427
}
5528

29+
static int open(struct inode *inode, struct file *file)
30+
{
31+
return single_open(file, show, NULL);
32+
}
33+
34+
static const struct file_operations fops = {
35+
.llseek = seq_lseek,
36+
.open = open,
37+
.owner = THIS_MODULE,
38+
.read = seq_read,
39+
.release = single_release,
40+
};
41+
5642
static int myinit(void)
5743
{
58-
kthread = kthread_create(work_func, NULL, "mykthread");
59-
wake_up_process(kthread);
44+
debugfs_file = debugfs_create_file("lkmc_params", S_IRUSR, NULL, NULL, &fops);
6045
return 0;
6146
}
6247

6348
static void myexit(void)
6449
{
65-
kthread_stop(kthread);
50+
debugfs_remove(debugfs_file);
6651
}
6752

6853
module_init(myinit)

kernel_module/user/README.adoc

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,3 @@ These programs can also be compiled and used on host.
2323
.. x86_64
2424
... link:rdtsc.c[]
2525
... link:ring0.c[]
26-
. Module tests
27-
.. link:anonymous_inode.c[]
28-
.. link:ioctl.c[]
29-
.. link:netlink.c[]
30-
.. link:poll.c[]

kernel_module/user/myinsmod.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
/* https://github.com/cirosantilli/linux-kernel-module-cheat#kernel-module-parameters#myinsmod */
2+
13
#define _GNU_SOURCE
24
#include <fcntl.h>
35
#include <stdio.h>

0 commit comments

Comments
 (0)