Program Tip

Linux 커널 코드에서 __init는 무엇을 의미합니까?

programtip 2020. 10. 5. 20:39
반응형

Linux 커널 코드에서 __init는 무엇을 의미합니까?


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)

이것들은 최종 실행 바이너리의 특수 영역으로 리눅스 코드의 일부를 찾는 유일한 매크로입니다. __init, 예를 들어 (또는 __attribute__ ((__section__ (".init.text")))이 매크로가 확장되는 것이 더 좋음 ) 컴파일러가이 함수를 특별한 방식으로 표시하도록 지시합니다. 마지막에 링커는 이진 파일의 끝 (또는 시작)에이 표시가있는 모든 함수를 수집합니다.

커널이 시작되면이 코드는 한 번만 실행됩니다 (초기화). 실행 후 커널은이 메모리를 해제하여 재사용 할 수 있으며 커널 메시지가 표시됩니다.

사용하지 않는 커널 메모리 해제 : 108k 해제

이 기능을 사용하려면 표시된 모든 함수를 찾을 위치를 링커에 알려주는 특수 링커 스크립트 파일이 필요합니다.


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.

source


__init is a macro defined in ./include/linux/init.h which expands to __attribute__ ((__section__(".init.text"))).

It instructs the compiler to mark this function in a special way. At the end the linker collects all functions with this mark at the end (or begin) of the binary file. When the kernel starts, this code runs only once (initialization). After it runs, the kernel can free this memory to reuse it and you will see the kernel


Read comment (and docs at the same time) in linux/init.h.

You should also know that gcc has some extensions made specially for linux kernel code and it looks like this macro uses one of them.


When you compile and insert a Linux kernel module into the kernel the first function to be executed is __init.This function is basically used to perform initialization before you perform the main operations like registering a device driver etc, There is another function with the opposite effect __exit which is called when you remove the kernel module which is again used to remove some registered device or any such similar function

참고URL : https://stackoverflow.com/questions/8832114/what-does-init-mean-in-the-linux-kernel-code

반응형