directory

Module

Load module

Uninstall the module

Viewing Module Information

Linux kernel module program structure

1. Module loading function

2. Module unloading function

3. Module license declaration

4. Module parameters (optional)

5. Module export symbol (optional)

6. Module author and other information declaration (optional)

Module usage count

7. Compilation of modules


Module

The overall architecture of the Linux kernel is very large and contains many components. How do we include all the parts we need in the kernel? One way is to compile all the required functionality into the Linux kernel. This leads to two problems:

1. The generated kernel will be large

2. If we were to add or remove functionality from an existing kernel, we would have to recompile the kernel.

Is there another mechanism by which the compiled kernel does not need to contain all the functionality itself, but the corresponding code is dynamically loaded into the kernel when it needs to be used? Linux provides such a mechanism, called a Module. Modules have the following characteristics:

The module itself is not compiled into the kernel image, thus controlling the size of the kernel. Once a module is loaded, it looks exactly like any other part of the kernel.

Load module

Simple command: iinsmod modName

Module dependency command: modprobe (load the module and other modules it depends on)

Uninstall the module

Simple command: rmmod modName

Module dependency command: modprobe -r modName (uninstall the module and other modules it depends on)

Viewing Module Information

modinfo  modName

The information includes the module author, description of the module, parameters supported by the module, and VerMagic

 

Linux kernel module program structure

1. Module loading function

When the kernel module is loaded by insmod or modprobe command, the loading function of the module will be automatically executed by the kernel to complete the initialization of the module.

static int _ _init initialization_function(void)
{
/* Initialize code */
}
module_init(initialization_function);
Copy the code

2. Module unloading function

When a module is uninstalled using the rmmod command, the uninstallation function of the module is automatically executed by the kernel to perform the opposite function.

static void _ _exit cleanup_function(void)
{
/* Release code */
}
module_exit(cleanup_function);
Copy the code

3. Module license declaration

The LICENSE statement describes the LICENSE permission of the Kernel module. If the LICENSE is not specified, the module will receive a warning of Kernel contamination (Kernel Tainted) when it is loaded. In the Linux kernel module domain, acceptable licenses include GPL, GPL V2, GPL and Additional Rights, Dual BSD/GPL, Dual MPL/GPL, and Proprietary.

4. Module parameters (optional)

Module parameters are values that can be passed to a module when it is loaded, which themselves correspond to global variables inside the module.

static char *book_name = "dissecting Linux Device Driver";
module_param(book_name, charp, S_IRUGO);
static int book_num = 4000;
module_param(book_num, int, S_IRUGO);
Copy the code

Call command:

insmod book.ko book_name='GoodBook' book_num=5000
Copy the code

5. Module export symbol (optional)

A symbol (corresponding to a function or variable) that can be exported by a kernel module and used by other modules.

EXPORT_SYMBOL(symbol name);EXPORT_SYMBOL_GPL(symbol name);#include <linux/init.h>
#include <linux/module.h>

int add_integar(int a, int b)
{
return a + b;
}
EXPORT_SYMBOL_GPL(add_integar);

int sub_integar(int a, int b)
{
    return a - b;
}
EXPORT_SYMBOL_GPL(sub_integar);
MODULE_LICENSE("GPL v2");
Copy the code

Exported symbols can be used by other modules, as long as they are declared before use

6. Module author and other information declaration (optional)

MODULE_AUTHOR(author);
MODULE_DESCRIPTION(description);
MODULE_VERSION(version_string);
MODULE_DEVICE_TABLE(table_info);
MODULE_ALIAS(alternate_name);
Copy the code

Module usage count

The value is used to increase the number of modules used. If 0 is returned, the call fails and the desired module is not loaded or is being unloaded.

int try_module_get(struct module *module);
Copy the code

Used to reduce module usage count.

void module_put(struct module *module);
Copy the code

7. Compilation of modules

Compile using a Makefile

KVERS = $(shell uname -r) # Kernel modules obj-m += hello.o # Specify flags for the module compilation. #EXTRA_CFLAGS=-g  -O0 build: kernel_modules kernel_modules: make -C /lib/modules/$(KVERS)/build M=$(CURDIR) modules clean: make -C /lib/modules/$(KVERS)/build M=$(CURDIR) cleanCopy the code