Skip to content

Commit 5057032

Browse files
committed
fix kernel modules build after updating linux to v5.9.2
- `dep.c` and `dep2.c`: `debugfs_create_u32` does not return anymore, not sure why: https://unix.stackexchange.com/questions/593983/creating-a-debugfs-file-that-is-used-to-read-write-u32-value/621282#621282 So just doing `debugfs_lookup` for now - vermagic.c: cirosantilli/linux@51161bf prevents its usage in v5.8. Just migrating to `init_utsname` for now - procfs.c: `struct file_operations` moved to a new `struct proc_ops` at: cirosantilli/linux@b567e07 - myprintk.c: `pr_warning` dropped for `pr_warn`: cirosantilli/linux@61ff72f - `poll.c`: `kthread_func` is not defined in kernel, prefix with lkmc to avoid conflict Fix #136 Fix #137
1 parent fa8c2ee commit 5057032

File tree

9 files changed

+65
-34
lines changed

9 files changed

+65
-34
lines changed

README.adoc

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6765,7 +6765,22 @@ Bibliography:
67656765

67666766
==== vermagic
67676767

6768-
Vermagic is a magic string present in the kernel and on <<module-info>> of kernel modules. It is used to verify that the kernel module was compiled against a compatible kernel version and relevant configuration:
6768+
link:kernel_modules/vermagic.c[]
6769+
6770+
As of kernel v5.8, you can't use `VERMAGIC_STRING` string from modules anymore as per: https://github.com/cirosantilli/linux/commit/51161bfc66a68d21f13d15a689b3ea7980457790[]. So instead we just showcase `init_utsname`.
6771+
6772+
Sample insmod output as of LKMC fa8c2ee521ea83a74a2300e7a3be9f9ab86e2cb6 + 1 aarch64:
6773+
6774+
....
6775+
<6>[ 25.180697] sysname = Linux
6776+
<6>[ 25.180697] nodename = buildroot
6777+
<6>[ 25.180697] release = 5.9.2
6778+
<6>[ 25.180697] version = #1 SMP Thu Jan 1 00:00:00 UTC 1970
6779+
<6>[ 25.180697] machine = aarch64
6780+
<6>[ 25.180697] domainname = (none)
6781+
....
6782+
6783+
Vermagic is a magic string present in the kernel and previously visible in <<module-info>> on kernel modules. It is used to verify that the kernel module was compiled against a compatible kernel version and relevant configuration:
67696784

67706785
....
67716786
insmod vermagic.ko
@@ -6777,8 +6792,6 @@ Possible dmesg output:
67776792
VERMAGIC_STRING = 4.17.0 SMP mod_unload modversions
67786793
....
67796794

6780-
Source: link:kernel_modules/vermagic.c[]
6781-
67826795
If we artificially create a mismatch with `MODULE_INFO(vermagic`, the insmod fails with:
67836796

67846797
....

kernel_modules/debugfs.c

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,17 +33,12 @@ static const struct file_operations fops = {
3333

3434
static int myinit(void)
3535
{
36-
struct dentry *file;
3736
dir = debugfs_create_dir("lkmc_debugfs", 0);
3837
if (!dir) {
3938
pr_alert("debugfs_create_dir failed");
4039
return -1;
4140
}
42-
file = debugfs_create_u32("myfile", S_IRUSR | S_IWUSR, dir, &value);
43-
if (!file) {
44-
pr_alert("debugfs_create_u32 failed");
45-
return -1;
46-
}
41+
debugfs_create_u32("myfile", S_IRUSR | S_IWUSR, dir, &value);
4742

4843
/* Created on the toplevel of the debugfs mount,
4944
* and with explicit fops instead of a fixed integer value.

kernel_modules/dep.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,18 @@
66

77
u32 lkmc_dep = 0;
88
EXPORT_SYMBOL(lkmc_dep);
9-
static struct dentry *debugfs_file;
9+
static char *debugfs_filename = "lkmc_dep";
1010

1111
static int myinit(void)
1212
{
13-
debugfs_file = debugfs_create_u32("lkmc_dep", S_IRUSR | S_IWUSR, NULL, &lkmc_dep);
13+
debugfs_create_u32(debugfs_filename, S_IRUSR | S_IWUSR, NULL, &lkmc_dep);
1414
return 0;
1515
}
1616

1717
static void myexit(void)
1818
{
19-
debugfs_remove(debugfs_file);
19+
20+
debugfs_remove(debugfs_lookup(debugfs_filename, NULL));
2021
}
2122

2223
module_init(myinit)

kernel_modules/dep2.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,17 @@
55
#include <linux/module.h>
66

77
extern u32 lkmc_dep;
8-
static struct dentry *debugfs_file;
8+
static char *debugfs_filename = "lkmc_dep2";
99

1010
static int myinit(void)
1111
{
12-
debugfs_file = debugfs_create_u32("lkmc_dep2", S_IRUSR | S_IWUSR, NULL, &lkmc_dep);
12+
debugfs_create_u32(debugfs_filename, S_IRUSR | S_IWUSR, NULL, &lkmc_dep);
1313
return 0;
1414
}
1515

1616
static void myexit(void)
1717
{
18-
debugfs_remove(debugfs_file);
18+
debugfs_remove(debugfs_lookup(debugfs_filename, NULL));
1919
}
2020

2121
module_init(myinit)

kernel_modules/mmap.c

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/* https://cirosantilli.com/linux-kernel-module-cheat#mmap */
22

3+
#include <asm-generic/io.h> /* virt_to_phys */
34
#include <linux/fs.h>
45
#include <linux/init.h>
56
#include <linux/kernel.h> /* min */
@@ -120,17 +121,17 @@ static int release(struct inode *inode, struct file *filp)
120121
return 0;
121122
}
122123

123-
static const struct file_operations fops = {
124-
.mmap = mmap,
125-
.open = open,
126-
.release = release,
127-
.read = read,
128-
.write = write,
124+
static const struct proc_ops pops = {
125+
.proc_mmap = mmap,
126+
.proc_open = open,
127+
.proc_release = release,
128+
.proc_read = read,
129+
.proc_write = write,
129130
};
130131

131132
static int myinit(void)
132133
{
133-
proc_create(filename, 0, NULL, &fops);
134+
proc_create(filename, 0, NULL, &pops);
134135
return 0;
135136
}
136137

kernel_modules/myprintk.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ static int myinit(void)
88
pr_alert("pr_alert\n");
99
pr_crit("pr_crit\n");
1010
pr_err("pr_err");
11-
pr_warning("pr_warning\n");
11+
pr_warn("pr_warning\n");
1212
pr_notice("pr_notice\n");
1313
pr_info("pr_info\n");
1414
pr_debug("pr_debug\n");

kernel_modules/poll.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ unsigned int poll(struct file *filp, struct poll_table_struct *wait)
5454
}
5555
}
5656

57-
static int kthread_func(void *data)
57+
static int lkmc_kthread_func(void *data)
5858
{
5959
while (!kthread_should_stop()) {
6060
readbuflen = snprintf(
@@ -81,7 +81,7 @@ static int myinit(void)
8181
debugfs_file = debugfs_create_file(
8282
"lkmc_poll", S_IRUSR | S_IWUSR, NULL, NULL, &fops);
8383
init_waitqueue_head(&waitqueue);
84-
kthread = kthread_create(kthread_func, NULL, "mykthread");
84+
kthread = kthread_create(lkmc_kthread_func, NULL, "mykthread");
8585
wake_up_process(kthread);
8686
return 0;
8787
}

kernel_modules/procfs.c

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
/* https://cirosantilli.com/linux-kernel-module-cheat#procfs */
22

3-
#include <linux/debugfs.h>
43
#include <linux/module.h>
54
#include <linux/proc_fs.h>
65
#include <linux/seq_file.h> /* seq_read, seq_lseek, single_open, single_release */
@@ -19,17 +18,16 @@ static int open(struct inode *inode, struct file *file)
1918
return single_open(file, show, NULL);
2019
}
2120

22-
static const struct file_operations fops = {
23-
.llseek = seq_lseek,
24-
.open = open,
25-
.owner = THIS_MODULE,
26-
.read = seq_read,
27-
.release = single_release,
21+
static const struct proc_ops pops = {
22+
.proc_lseek = seq_lseek,
23+
.proc_open = open,
24+
.proc_read = seq_read,
25+
.proc_release = single_release,
2826
};
2927

3028
static int myinit(void)
3129
{
32-
proc_create(filename, 0, NULL, &fops);
30+
proc_create(filename, 0, NULL, &pops);
3331
return 0;
3432
}
3533

kernel_modules/vermagic.c

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,34 @@
22

33
#include <linux/module.h>
44
#include <linux/kernel.h>
5-
#include <linux/vermagic.h>
5+
#include <linux/utsname.h>
6+
#if 0
7+
#include <linux/vermagic.h> /* VERMAGIC_STRING */
8+
#endif
69

710
static int myinit(void)
811
{
12+
struct new_utsname *uts = init_utsname();
13+
pr_info(
14+
"sysname = %s\n"
15+
"nodename = %s\n"
16+
"release = %s\n"
17+
"version = %s\n"
18+
"machine = %s\n"
19+
"domainname = %s\n",
20+
uts->sysname,
21+
uts->nodename,
22+
uts->release,
23+
uts->version,
24+
uts->machine,
25+
uts->domainname
26+
);
27+
28+
#if 0
29+
/* Possible before v5.8, but was buggy apparently, not sure why:
30+
* https://github.com/cirosantilli/linux/commit/51161bfc66a68d21f13d15a689b3ea7980457790 */
931
pr_info("VERMAGIC_STRING = " VERMAGIC_STRING "\n");
32+
#endif
1033
/* Nice try, but it is not a member. */
1134
/*pr_info("THIS_MODULE->vermagic = %s\n", THIS_MODULE->vermagic);*/
1235
return 0;

0 commit comments

Comments
 (0)