The entry and exit points of a kernel module are critical aspects that define the module’s behavior and interactions with the operating system’s kernel. These points mark where the kernel hooks into the module’s code during certain events in its lifecycle. The two primary entry and exit points for a kernel module are the “module initialization” and “module cleanup” routines:
-
Module Initialization (Entry Point):
-
Function Name:
module_init() -
Significance:
- The
module_init()function is the entry point for a kernel module and is called when the module is loaded into the kernel. This function typically contains code that sets up the module, allocates resources, registers with the kernel, and performs any necessary initialization tasks. - During module loading, the kernel executes the code in
module_init()to prepare the module for use. This may involve initializing data structures, allocating memory, and registering the module’s functionality with the kernel.
- The
-
Example:
static int __init my_module_init(void) { // Module initialization code return 0; // Return 0 on success } module_init(my_module_init); -
Notes:
- The
__initmacro indicates that the code is only needed during initialization and can be discarded after that phase to save memory. This is relevant for some configurations of the Linux kernel.
- The
-
-
Module Cleanup (Exit Point):
-
Function Name:
module_exit() -
Significance:
- The
module_exit()function is the exit point for a kernel module and is called when the module is unloaded from the kernel, either manually or during system shutdown. This function contains code to release resources, unregister from the kernel, and perform any necessary cleanup tasks. - During module unloading, the kernel executes the code in
module_exit()to gracefully shut down the module and release any resources it acquired during initialization.
- The
-
Example:
static void __exit my_module_exit(void) { // Module cleanup code } module_exit(my_module_exit); -
Notes:
- Similar to
__init, the__exitmacro indicates that the code is only needed during cleanup and can be discarded after that phase.
- Similar to
-
-
Return Values:
-
The return values of the
module_init()andmodule_exit()functions are significant. A return value of 0 typically indicates success, while a non-zero value indicates an error. The kernel uses these return values to determine whether the module initialization or cleanup was successful. -
Example:
static int __init my_module_init(void) { // Module initialization code return 0; // Return 0 on success } module_init(my_module_init); static void __exit my_module_exit(void) { // Module cleanup code } module_exit(my_module_exit); -
Notes:
- The return values are often used by tools like
insmodandrmmodto provide feedback on the success or failure of loading or unloading a module.
- The return values are often used by tools like
-
The entry and exit points of a kernel module (the module_init() and module_exit() functions) are crucial for managing the module’s lifecycle within the kernel. These points allow the module to initialize itself when loaded and perform cleanup when unloaded, ensuring proper integration with the operating system and adherence to kernel conventions.
