Linux Kernel Dynamically Loaded and detached on demand

Source: Internet
Author: User
Article Title: Linux kernel dynamically loads and dismounts modules as needed. 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.

Linux is a single-kernel structure, that is, it is a large program, and any function can access the public data structure and other function calls. (As an operating system) another possible structure is multi-core. Each function block is a self-contained one, and each other is connected by a strict communication mechanism. When adding a new module to a single-core structure, one way is to re-adjust the settings, which is very time-consuming. For example, if you want to add an NCR 810 SCSI driver to the kernel, you must reset it to recreate the kernel. Another way is to allow Linux to dynamically load and unload modules. the Linux module is a piece of code that can be dynamically connected at any time after the machine starts. 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. The kernel itself can also call the kernel resident program (Kerneld) to load and unload modules as needed.

The On-Demand dynamic loading module can minimize the kernel and provide more flexibility. my current Intel kernel uses a large number of Dynamic Loading modules, which only contains 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 I unload the VFAT partition, the kernel will detect it and automatically unload the VFAT file system. when testing a new program, if you don't want to rebuild the kernel every time, Dynamic Loading modules are very useful. however, the use of modules will consume more memory and affect the speed. in addition, the module loader is a piece of code, and its data will occupy a portion of memory. in this way, the kernel resources cannot be accessed directly and the efficiency is not high.

Once the Linux module is loaded, it will enjoy the same access permissions for other kernel codes as the general kernel code. In other words, Linux kernel modules can crash the system like other kernel code or drivers. The module can use kernel resources, but first it needs to know how to call it. for example, a module calls Kmalloc () (kernel memory allocation program ). however, when a module is created, it does not know where to find Kmalloc (). Therefore, when it is loaded, the kernel must first set all function pointers called by Kmalloc () in the module. the kernel has a list of all resource calls. When the module is loaded, the kernel resets the function pointers for all resource calls. linux allows a stack module, that is, one module calls the functions of another module. for example, because the VFAT file system can be considered as a superset of the FAT file system, the VFAT file system module needs to call the services provided by the FAT file system. Resources of one module calling another module are similar to those of calling the kernel. The only difference is that the called module needs to be loaded first. After a module is loaded, the kernel will modify its kernel symbol TABLE and add all the resources and symbols provided by the new loading module. When another module is loaded, it can call the services provided by all 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. Before the module is detached from the kernel, the module must release all occupied system resources. For example, memory or interruption. When a module is detached, the kernel deletes all resources provided by this module from the kernel symbol table.

If the module code is not rigorous, it will crash the entire operating system. Another problem is, what if you load modules that serve other versions? For example, a module uses an internal function but provides incorrect input parameters, which leads to running errors. However, the kernel can choose to pass strict version checks when the module is loaded.

There are two methods to load a module. The first method is to use the INSTALL command for LOADING. The other smarter method is to automatically load the module when it is called, which is called the 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 when the system is started), it opens an inter-process communication CHANNEL (ipc channel) with the kernel ). The kernel will use this 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 to enable or disable a ddd link through the serial port. KERNELD does not complete these tasks on its own. It will call commands such as INSMOD to complete the task. KERNELD is just a kernel proxy and coordinates the tasks.

When loading a module, the INSMODE command must first find the module to be loaded. MODULES that can be loaded are usually stored in/LIB/MODULES/KERNEL-VERSION. These MODULES and general system programs are connected to the target code, the difference is that the module is a relocated image file. That is to say, the module is not executed from a fixed address. The module can be a. out or the target code in ELF format. INSMODE uses a system-authorized call to find resources that can be called in the kernel.

The system (Resource) symbol consists of two parts: Name and value. The kernel uses the MODULE_LIST pointer to point to the linked list of all modules it manages. The output symbol table of the kernel is in the first MODULE data structure. Not all the symbols of the kernel can be called by the MODULE. The callable symbols must be added to the output symbol table, the output symbol table is compiled and connected with the kernel. For example, if a driver wants to control a system interruption, she needs to call a system function such as "REQUEST_IRQ". 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 can display the values of all kernel output symbols or the output symbols of the load module. When the INSMOD module is loaded, it first loads the module into the virtual memory and resets the pointer of all kernel Resource function calls based on the kernel output symbol. That is, the physical address of the corresponding symbol is written to the function call of the module.

 

[1] [2] Next page

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.