Chapter7:Modules
Ensure module is not used
sys_delete_module
find_module
mod->exit
free_module
Figure 7-6: Code flow diagram for
sys_delete_module.
The system call identifies a module by its name, which must be passed as a parameter^18 :
kernel/module.c
asmlinkage long
sys_delete_module(const char __user *name_user, unsigned int flags)
First, the kernel must find the matchingmoduleinstance by usingfind_moduleto look through the list
of all registered modules.
It must then be ensured that the module is not required by any other modules:
kernel/module.c
if (!list_empty(&mod->modules_which_use_me)) {
/* Other modules depend on us: get rid of them first. */
ret = -EWOULDBLOCK;
goto out;
}
It is sufficient to check whether the list is empty because a link is automatically established via the
modules_which_use_meelement described above each time a symbol is referenced by another module.
Once it has been established that the reference counter has returned to 0, the module-specific clean-up
function is called, and memory space occupied by the module data is freed by means offree_module.
7.4 Automation and Hotplugging
Modules can be loaded not only on the initiative of the user or by means of an automated script, but can
also be requested by the kernel itself. There are two situations where this kind of loading is useful:
- The kernel establishes that a required function is not available. For example, a filesystem
needs to be mounted but is not supported by the kernel.
The kernel can attempt to load the required module and then retry file mounting. - A new device is connected to a hotpluggable bus (USB, FireWire, PCI, etc.). The kernel
detects the new device and automatically loads the module with the appropriate driver.
(^18) Two flags can be passed in addition to the name:O_TRUNC, which indicates that the module may also be removed from the kernel
‘‘by force‘‘ (despite, e.g., the fact that the reference counter is positive);O_NONBLOCK, which specifies that the operation must be
performed without blocking. To keep things simple, the flags are not discussed here.