002 - Code of the first kernel module

What is a kernel module

Kernel module can be simply understood as a program running in kernel space. Like user space applications, it has a fixed pattern.

First kernel module

Let's start with a standard application

  • HelloWorld.c
#include "stdio.h"
int main(int argc, char** argv)
	printf("Hello World!");

The above code will not be strange to you. Remember the mood when learning this program for the first time? Most people may not understand why it is written like this, but just remember its format. The same is true for the kernel module. At first, we don't need to understand so much. We just need to remember the format of the kernel module.

First kernel module

  • HelloKernelModule.c
#include "linux/init.h"
#include "linux/module.h"

static int __init hello_module_init(void)
	printk("Initialize module\n");
	return 0;

static void __exit hello_module_exit(void)
	printk("Exit module\n"); 



module_init() and module_exit()

module_init() is used to declare the entry function of the module. The entry function name of the user application is fixed as main, which is specified in the c language standard. The entry function name of the kernel module can be named as needed. Just use module_init() can be declared once.
module_exit() is used to declare the functions to be executed when the kernel module exits (uninstalls).

If you know C + +, you can put module_ Init() and module_ The functions declared by exit () are regarded as constructors and destructors of C + + classes. The module is executed when the module is loaded_ The function declared by init() (hello_module_init() in the code) will execute module when the module is unloaded_ The function declared by exit() (hello_module_exit() in the code).


The standard function of C language is built on the outer API of the operating system, and the kernel module belongs to the inner layer of the operating system. Therefore, the standard function of C language cannot be called directly, such as printf(). printk() is required to print information in the kernel. The usage of this function can be found on the Internet. I won't repeat it here.


MODULE_LICENSE is used to declare the open source protocol standard executed by the current module. Because the kernel is an open source project, if the kernel module you write is not compatible with the GPL protocol, the kernel will think that the module you write 'pollutes' the kernel. Therefore, module is generally required_ License declares the protocol of the module.

You can see the module in the kernel code include/linux/module.h_ License, and the protocols it can use.

 * The following license idents are currently accepted as indicating free
 * software modules
 *	"GPL"				[GNU Public License v2 or later]
 *	"GPL v2"			[GNU Public License v2]
 *	"GPL and additional rights"	[GNU Public License v2 rights and more]
 *	"Dual BSD/GPL"			[GNU Public License v2
 *					 or BSD license choice]
 *	"Dual MIT/GPL"			[GNU Public License v2
 *					 or MIT license choice]
 *	"Dual MPL/GPL"			[GNU Public License v2
 *					 or Mozilla license choice]
 * The following other idents are available
 *	"Proprietary"			[Non free products]
 * There are dual licensed components, but when running with Linux it is the
 * GPL that is relevant so this is a non issue. Similarly LGPL linked with GPL
 * is a GPL combined work.
 * This exists for several reasons
 * 1.	So modinfo can show license info for users wanting to vet their setup
 *	is free
 * 2.	So the community can ignore bug reports including proprietary modules
 * 3.	So vendors can do likewise based on their own policies
#define MODULE_LICENSE(_license) MODULE_INFO(license, _license)

Keywords: kernel Module

Added by menelaus8888 on Sat, 23 Oct 2021 12:30:26 +0300