__init在Linux内核代码中的含义是什么?

时间:2012-01-12 08:38:43

标签: c linux

在Linux内核源代码中,我找到了这个函数:

static int __init clk_disable_unused(void) 
{
   // some code
}

我无法理解__init的含义。

6 个答案:

答案 0 :(得分:63)

include/linux/init.h

/* These macros are used to mark some functions or 
 * initialized data (doesn't apply to uninitialized data)
 * as `initialization' functions. The kernel can take this
 * as hint that the function is used only during the initialization
 * phase and free up used memory resources after
 *
 * Usage:
 * For functions:
 * 
 * You should add __init immediately before the function name, like:
 *
 * static void __init initme(int x, int y)
 * {
 *    extern int z; z = x * y;
 * }
 *
 * If the function has a prototype somewhere, you can also add
 * __init between closing brace of the prototype and semicolon:
 *
 * extern int initialize_foobar_device(int, int, int) __init;
 *
 * For initialized data:
 * You should insert __initdata between the variable name and equal
 * sign followed by value, e.g.:
 *
 * static int init_variable __initdata = 0;
 * static const char linux_logo[] __initconst = { 0x32, 0x36, ... };
 *
 * Don't forget to initialize data not at file scope, i.e. within a function,
 * as gcc otherwise puts the data into the bss section and not into the init
 * section.
 * 
 * Also note, that this data cannot be "const".
 */

/* These are for everybody (although not all archs will actually
   discard it in modules) */
#define __init      __section(.init.text) __cold notrace
#define __initdata  __section(.init.data)
#define __initconst __section(.init.rodata)
#define __exitdata  __section(.exit.data)
#define __exit_call __used __section(.exitcall.exit)

答案 1 :(得分:44)

这些只是将linux代码的某些部分定位为特殊的宏 最终执行二进制文件中的区域。 例如__init(或者这个宏扩展到的__attribute__ ((__section__ (".init.text")))更好)指示编译器标记这个 以特殊的方式运作。最后,链接器收集所有功能 在二进制文件的末尾(或开头)使用此标记。

当内核启动时,此代码只运行一次(初始化)。它运行后, 内核可以释放这个内存来重用它,你会看到内核 消息:

  

释放未使用的内核内存:108k释放

要使用此功能,您需要一个特殊的链接描述文件,告诉您 链接器在哪里找到所有标记的函数。

答案 2 :(得分:4)

这演示了内核2.2及更高版本的功能。请注意initcleanup函数定义的更改。 __init宏导致init函数被丢弃,一旦init函数完成内置驱动程序而不是可加载模块,其内存将被释放。如果你考虑调用init函数的时间,这是完全合理的。

source

答案 3 :(得分:3)

__ init是./include/linux/init.h中定义的宏,扩展为__attribute__ ((__section__(".init.text")))

它指示编译器以特殊方式标记此函数。最后,链接器在二进制文件的末尾(或开头)收集带有此标记的所有函数。内核启动时,此代码只运行一次(初始化)。运行后,内核可以释放这个内存来重用它,你会看到内核

答案 4 :(得分:2)

linux/init.h中阅读评论(和同时提供的文档)。

您还应该知道gcc有一些专门针对Linux内核代码的扩展,看起来这个宏使用其中一个。

答案 5 :(得分:0)

编译Linux内核模块并将其插入内核时,要执行的第一个功能是__init。此功能基本上用于执行初始化,然后再执行诸如注册设备驱动程序之类的主要操作,还有另一个功能删除内核模块时调用的相反效果__exit,该内核模块再次用于删除某些注册的设备或任何类似的功能

相关问题