内置和可加载模块的__init和__exit宏用法 [英] __init and __exit macros usage for built-in and loadable modules

查看:190
本文介绍了内置和可加载模块的__init和__exit宏用法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读有关Linux内核开发的信息,我只是阅读了一些我不理解的文本.这是讨论模块的__init和__exit宏的段落:

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:

这演示了内核2.2及更高版本的功能.注意变化 在初始化和清除函数的定义中. __init宏 导致初始化函数被丢弃,并且一旦 内置驱动程序的init函数完成,但可加载模块的init函数完成. 如果您考虑何时调用init函数,这将使 完美的感觉.

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.

还有一个__initdata 它与__init相似,但用于init变量而不是 功能.

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

__ exit宏导致在以下情况下省略该功能 该模块内置在内核中, __exit,对可加载模块无效.同样,如果您考虑何时运行清除功能

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

我明白了;一旦内置驱动程序的初始化功能完成,宏__init会导致该初始化功能被丢弃并释放其内存.但是为什么?不适用于可装载模块?我无法理解.

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.

我知道这是一个愚蠢的事情,但是我考虑了一段时间,无法完全理解它.为什么要使用内置驱动程序,而不要使用可加载模块?两者都需要在__init中分配的变量,地址等,对吧?

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?

推荐答案

您是对的;即使在模块中,也可能存在初始化后实际上并不需要的功能,因此原则上可以从内存中删除它们. __init对模块不起作用的原因更多地在于实现起来有多么容易.

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.

此问题有关__init 性质的问题的答案为该问题提供了一些启示.本质上,内核构建系统会在内核的所有部分中查找标记为__init的所有功能,并对其进行排列,以使它们全部位于同一内存块中.

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.

但是,硬件页面大小通常为4KB,因此很难以小于此大小的块来释放内存.因此,尝试在每个单独的模块中释放__init函数可能比它值得的麻烦更多.

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.

这篇关于内置和可加载模块的__init和__exit宏用法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆