我在Linux内核源代码中找到了这个函数:
static int __init clk_disable_unused(void)
{
// some code
}
这里我无法理解__init__
的含义是什么。
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)
这些只是用来将Linux代码的某些部分定位到最后执行二进制文件中特殊区域的宏。
例如,__init
或更好的是__attribute__ ((__section__(".init.text")))
(此宏扩展)指示编译器以特殊方式标记此函数。最后,链接器会将所有带有这个标记的函数收集在二进制文件的末尾(或开头)。
当内核启动时,此代码仅运行一次(初始化)。运行后,内核可以释放此内存以重复使用,并且您将看到内核消息:
释放未使用的内核内存:已释放108k
要使用此功能,您需要一个特殊的链接器脚本文件,该文件告诉链接器在哪里定位所有标记函数。
init
和cleanup
函数定义的变化。 __init
宏会导致对于内置驱动程序,init
函数在完成后被丢弃并释放其内存,但不适用于可加载模块。如果您考虑init
函数何时被调用,这就非常合理。__init是在./include/linux/init.h中定义的宏,它会展开为__attribute__ ((__section__(".init.text")))
。
它指示编译器以一种特殊的方式标记此函数。在链接器结束时,所有带有此标记的函数都会被收集到二进制文件的末尾(或开头)。当内核启动时,这段代码只运行一次(初始化)。运行完毕后,内核可以释放此内存以重用它,并且您将看到内核。
当您编译并插入Linux内核模块到内核中时,第一个执行的函数是__init。这个函数主要用于在执行主要操作(如注册设备驱动程序等)之前进行初始化。还有另一个具有相反效应的函数__exit,在卸载内核模块时调用,用于删除一些已注册的设备或类似功能。