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

余生长醉 提交于 2019-12-10 16:14:08

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/11680641/init-and-exit-macros-usage-for-built-in-and-loadable-modules

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!