00. Table of Contents
Article directory
01. Overview
We have compiled our own kernel module as expected, and now it is time to understand how to use this kernel module. Copy test.ko to the development board through scp or NFS. Let's explain these kernel module related tools one by one.
02. insmod
BusyBox v1.27.2 (2023-08-17 12:27:26 CST) multi-call binary.
Usage: insmod FILE [SYMBOL=VALUE]...
Load kernel module
Application examples:
[root@rk3399:/mnt/a72/code/1module]# insmod test.ko
[16933.818461] Hello Module Init
[root@rk3399:/mnt/a72/code/1module]#
calculation.ko relies on parameters and functions in parametermodule.ko, so manually load parametermodule.ko first, and then load calculation.ko.
Modprobe and insmod have the same functions and can also load modules into the kernel. In addition, modprobe can also check the dependencies between modules and load these dependencies in order, which can be understood as executing insmod multiple times in order.
calculation.ko and parametermodule.ko need to be loaded in order, and using the modprobe tool, parametermodule.ko can be loaded directly. Of course, before modprobe, you need to use depmod -a to establish the dependency between modules. ( Note: To use depmod -a
the command to establish dependencies, you need to copy calculation.ko and parametermodule.ko to /lib/modules/内核版本
the directory; in addition, when using modprobe
to load the driver, the driver name is not included .ko
, otherwise the following error will appear modprobe: FATAL: Module calculation.ko not found in directory /lib/modules/4.19.35-imx6
, and the same is true when uninstalling the driver)
03. lsmod
BusyBox v1.27.2 (2023-08-17 12:27:26 CST) multi-call binary.
Usage: lsmod
List loaded kernel modules
lsmod lists all modules in the current kernel and displays them in the terminal formatted. The principle is to adjust the format of the information in /proc/modules for output. The lsmod output list has a column of Used by, which indicates that this module is being used by other modules and shows the dependencies between modules.
If you want to load a module into the kernel, insmod is the simplest way. insmod + the full path of the module can achieve the goal. The premise is that your module does not depend on other modules, and you need sudo permissions. If you are not sure whether to use symbols from other modules, you can also try modprobe, which will be used in detail later.
Load the test.ko memory module through the insmod command. When loading the memory module, the memory module will automatically execute the module_init() function to perform initialization operations. The function prints 'hello module init'. Check the kernel module loaded into the system again, and we will find test.ko in the list.
Application examples
[root@rk3399:/mnt/a72/code/1module]# lsmod
Module Size Used by Tainted: G
test 16384 0
[root@rk3399:/mnt/a72/code/1module]#
04. depmod
Usage:
depmod -[aA] [options] [forced_version]
If no arguments (except options) are given, "depmod -a" is assumed
depmod will output a dependency list suitable for the modprobe utility.
Options:
-a, --all Probe all modules
-A, --quick Only does the work if there's a new module
-e, --errsyms Report not supplied symbols
-n, --show Write the dependency file on stdout only
-P, --symbol-prefix Architecture symbol prefix
-C, --config=PATH Read configuration from PATH
-v, --verbose Enable verbose mode
-w, --warn Warn on duplicates
-V, --version show version
-h, --help show this help
The following options are useful for people managing distributions:
-b, --basedir=DIR Use an image of a module tree.
-F, --filesyms=FILE Use the file instead of the
current kernel symbols.
-E, --symvers=FILE Use Module.symvers file to check
symbol versions.
deng@local:~/a72/x3399/kernel/include/linux$
How does modprobe know which other modules a given module depends on? In this process, depend plays a decisive role. When modprobe is executed, it will search for the module.dep file in the module's installation directory. This is the module dependency file created by depmod.
05. rmmod
Usage:
rmmod [options] modulename ...
Options:
-f, --force forces a module unload and may crash your
machine. This requires Forced Module Removal
option in your kernel. DANGEROUS
-s, --syslog print to syslog, not stderr
-v, --verbose enables more messages
-V, --version show version
-h, --help show this help
deng@local:~/a72/x3399/kernel/include/linux$
The rmmod tool only deletes the modules running in the kernel. You only need to pass it the path.
When the rmmod command uninstalls a memory module, the memory module will automatically execute the *_exit() function to perform cleaning operations. The *_exit() function in our hellomodule prints a line of content, but it is not displayed on the console. You can use dmesg to view it. , the reason why it is not displayed is related to the printing level of printk. The printk function is explained in detail earlier. rmmod will not uninstall the modules that a module depends on. It needs to be uninstalled in sequence. Of course, you can use modprobe -r to uninstall them with one click.
Application examples:
[root@rk3399:/mnt/a72/code/1module]# rmmod test
[17225.438774] Hello Module Exit
[root@rk3399:/mnt/a72/code/1module]#
06. modinfo
Usage:
modinfo [options] filename [args]
Options:
-a, --author Print only 'author'
-d, --description Print only 'description'
-l, --license Print only 'license'
-p, --parameters Print only 'parm'
-n, --filename Print only 'filename'
-0, --null Use \0 instead of \n
-F, --field=FIELD Print only provided FIELD
-k, --set-version=VERSION Use VERSION instead of `uname -r`
-b, --basedir=DIR Use DIR as filesystem root for /lib/modules
-V, --version Show version
-h, --help Show this help
deng@local:~/a72/x3399/kernel/include/linux$
modinfo is used to display several macros we defined in the kernel module. We can check it through modinfo. From the printed output information, we can know that the module follows the GPL protocol, the author information of the module, etc. This information is declared in the module code by the relevant kernel module information declaration function
deng@local:~/code/test/1module$ modinfo test.ko
filename: /home/deng/a72/code/1module/test.ko
alias: test_module
description: hello module
author: uplooking
license: GPL v2
depends:
vermagic: 4.4.189 SMP mod_unload aarch64
deng@local:~/code/test/1module$
07. The system automatically loads modules
We have written a module ourselves, or how to make it load automatically when the board is turned on? Here you need to use the above-mentioned depmod and modprobe tools.
First, we need to put the modules we want to load automatically into the "/lib/modules/kernel version" directory. Use 'uname -r' to query the kernel version; secondly, use depmod to establish dependencies between modules, command 'depmod - a'; At this time we can see the module dependencies in modules.dep, which can be viewed using the following command;
cat /lib/modules/内核版本/modules.dep | grep calculation
Finally, add our own module to /etc/modules. Note that in the configuration file, the module is not written in the form of .ko, which means that the module is tightly coupled with the kernel. Some systems must be tightly coupled with the kernel, such as the mm subsystem. Generally It is better to write it in .ko form. If an error occurs, it will not cause a panic error in the kernel. If integrated into the kernel, panic will occur if an error occurs.
deng@local:~/a72/x3399/kernel/include/linux$ cat /etc/modules
# /etc/modules: kernel modules to load at boot time.
#
# This file contains the names of kernel modules that should be loaded
# at boot time, one per line. Lines beginning with "#" are ignored.
Then restart the development board, and lsmod can see that our module is loaded into the kernel upon startup.