Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

__init and __exit macros usage for built-in and loadable modules

Tags:

exit

init

I was reading about linux kernel development and I just read some text that I don't understand. Here is the paragraph, which talks about the __init and __exit macros for modules:

This demonstrates a feature of kernel 2.2 and later. Notice the change in the definitions of the init and cleanup functions. The __init macro causes the init function to be discarded and its memory freed once the init function finishes for built−in drivers, but not loadable modules. If you think about when the init function is invoked, this makes perfect sense.

There is also an __initdata which works similarly to __init but for init variables rather than functions.

The __exit macro causes the omission of the function when the module is built into the kernel, and like __exit, has no effect for loadable modules. Again, if you consider when the cleanup function runs

I get the point; the macro __init causes the init function to be discarded and its memory freed once the init function finishes for built-in drivers. But why? not for loadable modules? I couldn't make sense of it.

I know it's a silly thing, but I thought about it for some time and couldn't comprehend it fully. Why for built-in driver but not for loadable modules? Variables, addresses etc assigned in __init would be required for the both, right?

like image 471
Amit Ahire Avatar asked Jul 26 '12 09:07

Amit Ahire


People also ask

What is the use of __ init and __ exit in Linux modular programming?

__init and __exit attributes This section is known in advance to the kernel, and freed when the module is loaded and the init function finished. This applies only to built-in drivers, not to loadable modules. The kernel will run the init function of the driver for the first time during its boot sequence.

What is the use of init and exit functions in a device driver?

1) Init and Exit Calls Besides notifying or registering with kernel it also allocates any kernel memory required for device and initialize hardware. This function is called at boot time. In the same way we need to detach our driver from the system when our system is shutting down .

What is the purpose of the Module_init and Module_exit macros for loadable kernel modules?

1 Answer. Show activity on this post. module_{init,exit}() adds the necessary boilerplate to initialize / cleanup the module and run the entry / exit code, when the module file is loaded / unloaded into or from the kernel space.

What does module init do?

init_module() loads an ELF image into kernel space, performs any necessary symbol relocations, initializes module parameters to values provided by the caller, and then runs the module's init function. This system call requires privilege.


1 Answers

You're right; even in a module there could be functions that you really don't need after initialization, and so they could in principle be removed from memory. The reason __init has no effect for modules is more about how easy it would be to implement.

This answer to a question about the nature of __init sheds some light on the subject. Essentially, the kernel build system looks for all of the functions flagged with __init, across all of the pieces of the kernel, and arranges them so that they will all be in the same block of memory.

Then, when the kernel boots, it can free that one block of memory all at once.

This pre-sorting idea doesn't work so well with modules. The init code has to be loaded when the module is loaded, so it can't share space with other init code. Instead, the kernel would have to pick a few hundred bytes out of each module and free them individually.

However, hardware page sizes are typically 4KB, so it's hard to free up memory in chunks of less than that. So trying to free the __init functions in each individual module is probably more trouble than it's worth.

like image 140
Jander Avatar answered Oct 13 '22 10:10

Jander