Adding Patch to Kernel Module

In this section, you are going to learn

How to create patches ?

How to add patches to kernel module ?

How to add patches using diff and patch commands ?

How to add patches using quilt ?

Let us answer few basic questions

What is a patch ?

What are the ways to generate and apply patch ?

What is use of diff command ?

What is use of patch command ?

What is the command to generate patch file ?

What is the command to apply the patches to source code ?

What is the command to remove the patches from source code ?

What is quilt ?

What quilt init does ?

What quilt new does ?

What quilt add does ?

What quilt refresh does ?

What quilt series does ?

What quilt push does ?

What is quilt pop does ?

What quilt top does ?

Let us now explore it in depth !

  • Step 1: Edit the file which needs to be modified.

  • Step 2: Generate a patch file using the diff command.

diff -u <original_file_name> <modified_file_name> > <patch_file_name>
  • Step 3: Now the patches to the file can be applied using patch command.

patch <orginal_file_name> < <patch_file_name>
  • Now a patch is created and the changes is applied to the file.

 1#include <linux/init.h>
 2#include <linux/module.h>
 3
 4MODULE_LICENSE("GPL");
 5
 6static int __init helloworld_init(void)
 7{
 8	printk(KERN_INFO "Hello world\n");
 9
10	return 0;
11}
12
13static void __exit helloworld_exit(void)
14{
15	printk(KERN_INFO "Exit\n");
16}
17
18module_init(helloworld_init);
19module_exit(helloworld_exit);
20
 1#include <linux/init.h>
 2#include <linux/module.h>
 3
 4MODULE_LICENSE("GPL");
 5
 6static int __init helloworld_init(void)
 7{
 8	printk(KERN_INFO "Hello world\n");
 9	printk(KERN_INFO "Patch added to kernel module\n");
10	return 0;
11}
12
13static void __exit helloworld_exit(void)
14{
15	printk(KERN_INFO "Exit\n");
16}
17
18module_init(helloworld_init);
19module_exit(helloworld_exit);
20
1obj-m += helloworld_original.o
2
3all:
4	make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
5
6clean:
7	make -C /lib/modules/$(shell uname -r)/buld M=$(PWD) clean
$ diff -u helloworld_original.c helloworld_modified.c < add_patch_to_kernel_mod.diff
$ patch helloworld_original.c < add_patch_to_kernel_mod.diff
patching file helloworld_original.c
 1--- helloworld_original.c	2024-01-02 16:42:13.460928842 +0530
 2+++ helloworld_modified.c	2024-01-02 16:43:11.604579126 +0530
 3 -6,7 +6,7 
 4 static int __init helloworld_init(void)
 5 {
 6 	printk(KERN_INFO "Hello world\n");
 7-
 8+	printk(KERN_INFO "Patch added to kernel module\n");
 9 	return 0;
10 }
11 
$ make
$ sudo insmod ./helloworld_original.ko
$ sudo rmmmod helloworld_original
$ dmesg
[1233082.552450] Hello world
[1233082.552451] Patch added to kernel module
[1233093.272275] Exit
  • Step 1: create a new quilt series using quilt init command.

$ quilt init
The quilt meta-data is now initialized.
  • Step 2: create a patch file using quilt new command.

$ quilt new add_hello_world_patch.patch
Patch patches/add_hello_world_patch.patch is now on top
  • Step 3: add the file which needs to be modified using quilt add command.

$ quilt add helloworld.c
File helloworld.c added to patch patches/add_hello_world_patch.patch
  • Step 4: edit the file and make the modifications needed.

$ vi helloworld.c
  • Step 5: once the changes is added run quilt refresh to update the series with latest modifications.

$ quilt refresh
Refreshed patch patches/add_hello_world_patch.patch
  • Now a patch is created and changes has been added to the series.

 1#include <linux/init.h>
 2#include <linux/module.h>
 3
 4MODULE_LICENSE("GPL");
 5MODULE_AUTHOR("Your Name");
 6MODULE_DESCRIPTION("Hello World Kernel Module");
 7
 8static int __init hello_world_init(void) {
 9    printk(KERN_INFO "Hello World module loaded\n");
10    return 0;
11}
12
13static void __exit hello_world_exit(void) {
14    printk(KERN_INFO "Hello World module unloaded\n");
15}
16
17module_init(hello_world_init);
18module_exit(hello_world_exit);
19
 1obj-m += helloworld.o
 2KDIR := /lib/modules/$(shell uname -r)/build
 3
 4QUILT_SERIES = my_patches
 5PATCHES_DIR = patches
 6
 7all: apply_patches
 8	$(MAKE) -C $(KDIR) M=$(PWD) modules
 9
10apply_patches:
11	quilt push || true
12
13.PHONY: clean
14clean:
15	quilt pop -a || true
16	$(MAKE) -C $(KDIR) M=$(PWD) clean
17
$ quilt init
The quilt meta-data is now initialized.
$ quilt new add_hello_world_patch.patch
Patch patches/add_hello_world_patch.patch is now on top
$ quilt add helloworld.c
File helloworld.c added to patch patches/add_hello_world_patch.patch
$ vi helloworld.c
$ quilt refresh
Refreshed patch patches/add_hello_world_patch.patch
 1Index: app/helloworld.c
 2===================================================================
 3--- app.orig/helloworld.c
 4+++ app/helloworld.c
 5
 6 
 7 static int __init hello_world_init(void) {
 8     printk(KERN_INFO "Hello World!\n");
 9+    printk(KERN_INFO "patches applied to kernel module!\n");
10     return 0;
11 }
12 
$ make
sudo insmod ./helloworld.ko
$ dmesg
[10472.955996] Hello World module loaded
[10955.989570] patches applied to kernel module!
$ lsmod | grep helloworld
helloworld             16384  0
$ sudo rmmod ./helloworld
$ dmesg
[10516.629413] Hello World module unloaded