Linux kernel module User Guide

Source: Internet
Author: User
Article title: Linux kernel module User Guide. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.
Author: sss
  
I. module Introduction
  
Windows NT is a micro-kernel structure. its functional blocks are divided into independent modules, and there is a strict communication mechanism between these functional blocks. Linux is different, it is a monolithic (single block) structure, that is, the entire kernel is a separate, very large program. In this structure, the addition and deletion of components are quite troublesome and the kernel needs to be re-compiled. To solve this problem, we do not know which kernel version starts. Linux introduces a technology called module, some functional code can be dynamically loaded into the kernel as a module.
  
A module is a target object file that needs to be executed in the kernel space. it can be viewed as a group of programs that have been compiled and linked to executable files. When necessary, the kernel will use some method to call these programs to execute specific operations and implement specific functions. The kernel maintains a chain table of modules in the kernel symbol table. each symbol table corresponds to a module and correctly explains the module when it is loaded into the kernel, the module is executed as part of the kernel. the modules loaded into the kernel have all kernel permissions. The module can be loaded to the system at system startup, or at any time during system running. when not needed, the module can be dynamically detached. In this way, you do not need to re-compile the kernel every time you modify the system configuration.
  
II. Advantages and disadvantages of modules
  
This dynamic loading feature of the kernel module has the following advantages:
1. you can minimize the number of kernel image files. When compiling the kernel, you can choose to compile part of the content as a module. in this way, you can not include this part in the final generated kernel image file to generate the smallest kernel image file.
2. good flexibility. If you need to use a new module, you don't have to re-compile the kernel. you just need to compile the new module and load it into the system. If you modify the kernel source program and do not need to re-compile the entire kernel, you only need to modify the corresponding part.
  
However, the introduction of kernel modules also brings about some problems:
1. this dynamic loading feature is not conducive to the system performance and memory utilization, and will have a negative impact.
2. modules loaded into the kernel have the highest permissions as other kernel components. Improper use may cause system crash.
3. incompatibility between the kernel version and the module version will also cause system crashes. Therefore, strict version checks must be performed to make the module compilation more complex.
4. some modules need to use the content of other modules (such as VFAT requires FAT), and there is a certain dependency between modules, so that the practicality of the modules is complicated.
  
Most of the device drivers in Linux are written by the module due to the dynamic loading/uninstallation feature of the module, such as file systems (minix, msdos, isofs, smbms, nfs, proc, etc.), SCSI device drivers, Ethernet drivers, CD-ROM drivers and so on. The following describes how to use the module.
  
III. use of modules
  
1. module query
  
We can use the lsmod command to understand which modules are currently loaded in the system. For example, the result on the author's machine is (note that the following commands (including lsmod) can only be executed by super users ):
  
Module Size Used
Lockd 30344 1 (autoclean)
Sunrpc 52132 1 (autoclean) [lockd]
Rtl8139 11748 1 (autoclean)
  
The Module column is the name of the Module, and the Size column is the Size of the displayed Module. the Used by column indicates the number of references. the autoclean column in parentheses indicates that the Module can be automatically detached when it is idle, [lockd] in brackets indicates that the module lockd references the content of the sunrpc module.
  
  
2. module loading
  
There are two ways to load a module: one is to use the insmod command to manually load the module, and the other is to use the kernel daemon kerneld to dynamically load the module as needed. The format of the insmod command is:
  
Insmod/ /Modulename. o
  
It is worth noting that the insmod command needs to know the module storage location so that it can be parsed in the kernel symbol table. The module can be located in the current path, or the absolute path can be specified in the insmod command. In addition, there are several related configuration files that can describe the module location (see the introduction below ).
  
Kerneld is a standard daemon with super user permissions. its main function is to load and uninstall core modules, but it can also execute other tasks, for example, establish a PPP connection through a serial line and disable it as appropriate. Kerneld itself does not execute these tasks. It does this through some programs such as insmod. It is only the agent of the kernel and is scheduled for the kernel. This daemon is only a common user process with super user permissions. When the system is started, it is also started and opens an inter-process communication (IPC) channel for the kernel. When the kernel needs to execute various tasks, this IPC is used to send messages to kerneld. For example, if the kernel request is not yet loaded to the file system, notify kerneld to load the file system, and then the kernel can use the file system. When the module is idle (that is, when no other process uses this module), kerneld can also dynamically uninstall this module.
  
Note that if there is a reference relationship between modules, the module must be loaded in a certain order. For example, if the lockd module in the result displayed by lsmod references the content of sunrpc, it must first load sunrpc before loading lockd. Otherwise, an error occurs.
  
  
3. unmount a module
  
We can use the rmmod command to unmount the module from the system. the format of this command is:
  
Rmmod modulename
  
Note that the module can be detached from the system only when it is idle. The Used by column in The lsmod output results shows the current status of the module. If the value is not 0, the module is busy and cannot be detached; otherwise, the value is 0, indicating that the module is idle and can be detached from the system. For busy devices, we must first disconnect the corresponding device before deleting the corresponding module. For example, to uninstall the rtl8139 module (this module is the corresponding module of the Ethernet card of the rtl8139 in the user's machine), we must first disconnect the network:
  
Ifdown eth0/* eth0 is the first network adapter in my Machine */
  
The output result of executing the lsmod command is:
  
Module Size Used
Lockd 30344 1 (autoclean)
Sunrpc 52132 1 (autoclean) [lockd]
Rtl8139 11748 0 (autoclean)
  
It indicates that no device has used rtl8139. we can use
  
Rmmod rtl8139
  
Command to detach it from the system.
  
The modules marked as autoclean (see the introduction to lsmod) can be automatically uninstalled. As mentioned above, there may be reference relationships between modules. If module A references module B, module A must be loaded before it can be loaded, otherwise, the system will crash (of course, if the module source code is correct, Module B cannot be detached before it is detached ).
  
4. module utility
  
). It is best to ensure that the version number of the module utility in your system (you can use the modinfo-V command to view it) is no lower than the kernel version number (you can use uname-r to view it ). The modules in version 1.3.57 contain commands such as modprobe, depmod, genksyms, makecrc32, insmod, rmmod, lsmod, ksyms, and kerneld. The modprobe command is similar to the insmod command, but it depends on the relevant configuration file. the depmod command is used to generate the module dependency file/lib/modules/kernel-version/modules. dep; genksyms and ksyms are related to the version number of kernel functions (kernel functions of different versions are different due to continuous kernel updates. in order not to cause system crashes, the version number of the kernel function must be strictly controlled in the kernel source program ). In a later version of the utility, kmod is used to replace kerneld. The function of kmod is similar to that of kerneld, but it cannot automatically uninstall the module. The reason for using kmod is that kerneld is implemented through the IPC channel, which is equivalent to a layer of processing. In addition, the kerneld code is also complicated, and the number of kmod code is much smaller than kerneld.
  
  
5. kernel compilation options and processes related to modules
  
When you use make confing/make menuconfig/make xconfig to configure the kernel, the module-related options include:
Code maturity level options -->
Prompt for development and/or incomplete code/drivers
This option is the degree of code maturity. All new designs and improvements are first available in the development version (version: x. y. z, where y is an odd number) in the trial, after the verification technology is feasible, and then in the stable version (version number is x. y. z, where y is an even number. Immature or imperfect technologies won't be compiled into the kernel by default, if you want to experiment with such content (for example, 2. 4. * In the khttpd or IPV6 version), select this option.
>
Loadable module support -->
Enable module support
Set version information on all module symbols
Kernel module loader
This option is supported for the loadable kernel, the version number of the module symbol, and the kernel module loader. For most other options, you can compile the corresponding code into the kernel (using the build-in method), or compile them into modules (using modules.
  
If you use the module method, you must use
  
Make; make install
  
Command to compile/install the kernel, you must also use
  
Make modules; make modules_install
  
To compile/install the kernel module. The compiled module is installed in the/lib/modules/kernel-version/directory.
  
During compilation, another
  
Depmod-
  
Command. This command generates the module dependency file, that is,/lib/modules/kernel-version/modules. dep. the file format is:
  
/Lib/modules/2.2.14-5.0/fs/autofs. o:
  
/Lib/modules/2.2.14-5.0/fs/binfmt_aout.o:
  
/Lib/modules/2.2.14-5.0/fs/binfmt_java.o:
  
/Lib/modules/2.2.14-5.0/fs/binfmt_misc.o:
  
/Lib/modules/2.2.14-5.0/fs/coda. o:
  
/Lib/modules/2.2.14-5.0/fs/fa
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.