How does the Linux kernel dynamically load and unload modules as needed?
Source: Internet
Author: User
Linux Kernel dynamically loads and dismounts modules as needed-general Linux technology-Linux programming and kernel information. For more information, see the following. Linux is a single-kernel structure, that is, it is a large program, and any function can access the public
Data structures and other function calls. (As the operating system) another possible structure is multi-core
The function blocks are self-contained and are connected by strict communication mechanisms. When adding a new module
The method is to re-adjust the settings, so it is very time-consuming. For example, you want to add an NCR 810 SCSI
You must re-set and re-build the kernel. This also has another way, Linux allows
The Linux module is a generation that can be dynamically connected at any time after the machine starts.
Code. They can be detached from the kernel when they are not needed. Most Linux modules are device drivers or
Pseudo-device drivers, such as network drivers and file systems.
You can use the insmod and rmmod commands to load and unload Linux modules. You can also call
Use the kernel resident program (Kerneld) to load and unload modules as needed.
The On-Demand dynamic loading module can minimize the kernel size and make it more flexible.
A large number of Dynamic Loading modules are used, with only 406 kb. For example, I seldom use the VFAT file system,
So I asked the Linux kernel to automatically upload the VFAT file system only when I load the VFAT partition.
When the VFAT partition is detached, the kernel detects and automatically unmounts the VFAT file system. When a new program is tested
If you don't want to re-build the kernel every time, the dynamic loading module is very useful.
It consumes some memory and affects the speed. In addition, the module loader is a piece of code, and its data will
Occupy a portion of the memory. This will also cause the inability to directly access the kernel resources, resulting in low efficiency.
Once the Linux module is loaded, it will be the same as the general kernel code.
. In other words, the Linux kernel module can make
System crash.
A module can use kernel resources, but first it needs to know how to call it. For example, a module needs to call Kmalloc ()
(Kernel memory allocation Program). But when the module is created, it does not know where to find Kmalloc (), so
When it is loaded, the kernel must first set all function pointers called by Kmalloc () in the module. The kernel has
List of all resource calls. When the module is loaded, the kernel resets the function pointer for all resource calls. Linux
Stack-type modules are allowed, that is, one module calls the functions of another module. For example, because the VFAT file system can
As a superset of the FAT file system, the VFAT file system module needs to call the FAT file system.
Provided services. Resources of one module calling another module are similar to those of calling the kernel. The only difference is that
The called module must be loaded first. After a module is loaded, the kernel will modify its kernel symbol table (KERAEL
And add all the resources and symbols provided by the new loading module. So another module is loaded.
It can call all services provided by loaded modules.
When a module is detached, the kernel first determines that the module will not be called, and then notifies it in some way. In
Before the module is detached from the kernel, it must release all occupied system resources. For example, memory or interruption,
After the module is detached, the kernel deletes all resources provided by the module from the kernel symbol table.
If the module code is not rigorous, it will crash the entire operating system. Another problem is that if you load
What should I do for modules of other service versions? For example, a module uses an internal function but provides errors.
. However, the kernel can selectively pass the strict version when the module is loaded.
This check is used to prevent this problem.
There are two methods to load a module. The first method is to load data using the INSTALL command;
Another smarter method is to automatically load a module when it is called, which is called the required load (demand loading ).
For example, when a user installs a file system that is not in the kernel, the kernel automatically calls the kernel resident program (KERNELD)
To load the corresponding processing module.
The kernel resident program is a common user program with the superuser limit. When it is started (usually in the system
Qi
It opens an ipc channel between processes and the kernel ). The kernel will use this
Entries
Pipeline to notify the process resident program to complete various tasks.
The main task of the kernel resident program is to load and unload the module, and it can also complete some of its tasks. If you need
Enable and disable a ddd link through the serial port. KERNELD does not complete these tasks on its own. It will call
Run commands such as INSMOD. KERNELD is just a kernel proxy and coordinates various tasks.
When loading a module, the INSMODE command must first find the module to be loaded. Modules that can be loaded
Put it in/LIB/MODULES/KERNEL-VERSION. These MODULES are connected to common system programs.
The difference is that the module is a relocated image file. That is to say, the module is not from a fixed
The specified address starts execution. The module can be a. out or the target code in ELF format. INSMODE
Find the resources that can be called in the kernel through a system-authorized call.
The system (Resource) symbol consists of two parts: Name and value. The kernel uses the MODULE_LIST pointer to point to all of its managed
The linked list serialized by the module. The output symbol table of the kernel is not in the data structure of the first MODULE.
Some symbols can be called by the module. The callable symbols must be added to the output symbol table, while the output symbol table is
Compiled and connected together with the kernel. For example, when a driver wants to control a system interruption, it needs to call"
REQUEST_IRQ "is a system function. In the kernel of my machine, its current value is 0x0010cd30,
You can view the/PROC/KSYMS file or use KSYMS to query. The KSYMS command displays all kernel output characters.
It can also display the value of the output symbol of the load module. When INSMOD is loaded into the module, it first loads the module
Load the VM and reset the pointer of all kernel Resource function calls based on the kernel output symbol. That is, the module function
Number of calls are written to the physical address of the corresponding symbol.
After INSMOD resets the kernel output symbol address, it will call a system function to obtain the kernel allocation.
Sufficient space. Memory will allocate a new MODULE data structure and enough memory to load this new MODULE.
And put the MODULE data structure at the end of the MODULE linked list to UNINITALIZED ).
Table 12. 1 shows the Chain List of modules after the kernel loads the FAT and VFAT modules. The first module of the linked list does not
It is a pseudo-module that is only used to record the output symbol table of the kernel. You can use ISMOD
To list all load modules and their relationships. ISMOD is only a formatted output record of the kernel linked list
/PROC/MODULES file. INSMOD can access the memory allocated by the kernel to the newly loaded module.
Write this memory, and then relocate it so that the module can start from this address. Because
Every time a module is loaded, it is unlikely that it will be allocated to the same memory address, regardless of whether it is on the same machine,
Therefore, relocation (that is, resetting its function pointer) is required.
The new loading module can also output symbols. INSMOD will create a table for these symbols. In addition, each module must
You must have your own initialization and cleaning (that is, destructor) functions. These two functions cannot be output, but their addresses are
INSMOD is used to transmit data to the kernel.
When a new module is loaded into the kernel, it must update the system symbol table and the modules it calls. Called in Kernel
All modules must retain the pointer of a column pointing to the calling module at the end of the symbol table. 2. 1. Display VFAT files
The system depends on the FAT file system. Therefore, the FAT module has a pointer to VFAT.
It is added when VFAT is loaded. The kernel will call the initial function of the module. If successful, it will continue
To install the new module. The address of the cleanup function of the MODULE will be stored in its MODULE data structure. When
When the module is detached, it is called. Here, the module status is set to "RUNNING ).
12. 2. Uninstall the module
The RMMOD command can be used to unload a specified module. However, when the module is loaded as needed, it will be automatically detached by the kernel,
Each time KERNELD is activated, it calls a system function to unload all useless modules from the kernel. Example
For example, if you have installed a CDROM of and its file system is an on-demand loading module
Soon after you detach the CDROM, The ISO9660 file system will also be detached from the kernel. You can start KERNELD
Set the time interval for activating my KERNELD. It is activated once every 180 seconds.
When being called by another module, the module cannot be detached. For example, when you are still using the VFAT File System
The VFAT module is not detached. When you look at the output of the ISMOD command, you will find that each module has
One counter. This counter record depends on the number of modules of this module. In the above example, VFAT and
MSDOS depends on the FAT module, so the counter of the FAT module is 2, and the VFAT and MODOS are both 1, indicating
Only file systems depend on them. If I mount another VFAT file system, the VFAT module counter will
To 2. The module counter is the first LONGWORD of its image ).
This long word also records the AUTOCLEAN and VISITED logos, which are used only when the module is loaded on demand.
These two logos. AUTOCLEAN enables the system to identify which module needs to be automatically detached. VISITED flag table
Indicates whether the module is still called by other modules. Each time KERNELD tries to unload useless On-Demand Loading modules
The system checks all modules. It only indicates the module that is AUTOCLEAN and is running. If this module
If the VISTIED flag is not set, it will be detached. Otherwise, the system will clear the VISTIED mark and continue the check.
Next module.
When a module can be detached, the system will call its cleaning function to release all system resources it occupies.
Source.
The MODULE data structure of this MODULE is marked as DELEDTED and removed from the blockchain table.
The module modifies their pointers, indicating that the module no longer depends on them. All the memory occupied by this module will
Released.
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