__init and __exit using macros for embedded and loadable modules

I read about Linux kernel development, and I just read text that I don't understand. Here is a paragraph that talks about __init and __exit macros for modules:

This demonstrates the kernel feature of 2.2 and later. Note the change in the definitions of the init and cleanup functions. The __init macro discards the init function and frees its memory after the init function ends for built-in drivers, but not loadable modules. If you think about when the init function is called, that makes perfect sense.

There is also __initdata which works similarly to __init, but for init variables, not functions.

The __exit macro causes an omission of a function when a module is built into the kernel, and, like __exit, does not affect loadable modules. Again, if you think the cleanup function is working

I understand the point; The __init macro __init the init function and frees memory after the init function completes for the built-in drivers. But why? not for loadable modules? I could not figure it out.

I know this is stupid, but I thought about it for a while and could not fully understand it. Why for the built-in driver, but not for loadable modules? Variables, addresses, etc. Assigned to __init will be required for both, right?

+4
source share
1 answer

You're right; even in the module there may be functions that you really do not need after initialization, and therefore, in principle, they can be deleted from memory. The reason __init does not affect modules, it is more about how easy it is to implement.

This answer to the question of nature __init sheds light on this topic. Essentially, the kernel build system looks for all the functions marked with __init in all parts of the kernel and arranges them so that they are all in the same memory block.

Then, when the kernel boots, it can immediately free one block of memory.

This pre-sorting does not work very well with modules. The initialization code must be loaded when the module is loaded, so it cannot use the space with another initialization code. Instead, the kernel would have to allocate several hundred bytes from each module and release them separately.

However, the size of the hardware pages is usually 4 KB, so it is difficult to free up memory in smaller pieces. Therefore, trying to free __init functions in each individual module is likely to be more of a problem than it costs.

+3
source

All Articles