[c] 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 해제

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


답변

이것은 커널 2.2 이상의 기능을 보여줍니다. initcleanup함수 의 정의가 변경되었습니다 . __init매크로는 원인 init폐기하는 기능을하고 메모리는 일단 해제 init드라이버를 내장,하지만로드 가능한 모듈 기능 완료. init함수가 언제 호출 되는지 생각해 보면 이것은 완벽합니다.

출처


답변

__init는 ./include/linux/init.h에 정의 된 매크로로 __attribute__ ((__section__(".init.text"))).

이 함수를 특별한 방법으로 표시하도록 컴파일러에 지시합니다. 마지막에 링커는 이진 파일의 끝 (또는 시작)에이 표시가있는 모든 함수를 수집합니다. 커널이 시작되면이 코드는 한 번만 실행됩니다 (초기화). 실행 후 커널은이 메모리를 해제하여 재사용 할 수 있으며 커널이 표시됩니다.


답변

linux / init.h 에서 주석 (및 동시에 문서) 읽기 .

또한 gcc에는 Linux 커널 코드를 위해 특별히 만들어진 몇 가지 확장이 있으며이 매크로가 그중 하나를 사용하는 것처럼 보입니다.


답변

Linux 커널 모듈을 컴파일하여 커널에 삽입 할 때 가장 먼저 실행되는 함수는 __init입니다.이 함수는 기본적으로 디바이스 드라이버 등록 등의 주요 작업을 수행하기 전에 초기화를 수행하는 데 사용되며 반대 효과가있는 다른 함수가 있습니다. 등록 된 일부 장치 또는 이와 유사한 기능을 제거하는 데 다시 사용되는 커널 모듈을 제거 할 때 호출되는 __exit


답변