簡體   English   中英

Linux kernel代碼中的__init是什么意思?

[英]What does __init mean in the Linux kernel code?

在 Linux kernel 源代碼中我找到了這個 function:

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

要使用此功能,您需要一個特殊的鏈接器腳本文件,它告訴鏈接器在哪里可以找到所有標記的函數。

這演示了內核 2.2 及更高版本的功能。 注意initcleanup函數定義的變化。 __init宏會導致init函數被丟棄並在init函數為內置驅動程序完成后釋放其內存,但不可加載模塊。 如果您考慮何時調用init函數,這是完全合理的。

來源

__init 是在 ./include/linux/init.h 中定義的宏,它擴展為__attribute__ ((__section__(".init.text")))

它指示編譯器以特殊方式標記此函數。 最后,鏈接器收集二進制文件末尾(或開頭)帶有此標記的所有函數。 當內核啟動時,這段代碼只運行一次(初始化)。 它運行后,內核可以釋放這塊內存以重用它,您將看到內核

linux/init.h 中閱讀評論(同時閱讀文檔)。

你還應該知道 gcc 有一些專門為 linux 內核代碼制作的擴展,看起來這個宏使用了其中之一。

當你編譯Linux內核模塊並將其插入內核時,首先要執行的函數是__init。這個函數主要用於在執行注冊設備驅動程序等主要操作之前進行初始化,還有另一個函數具有相反的效果__exit 刪除內核模塊時調用,該模塊再次用於刪除某些已注冊的設備或任何此類類似功能

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM