In the Linux kernel source code I found this function:
static int __init clk_disable_unused(void) { // some code }
Here I can not understand what does __init
means.
The __init keyword tells the linker to place the code in a dedicated section into the kernel object file. This section is known in advance to the kernel, and freed when the module is loaded and the init function finished. This applies only to built-in drivers, not to loadable modules.
These macros are defined in linux/init. h and serve to free up kernel memory. When you boot your kernel and see something like Freeing unused kernel memory: 236k freed, this is precisely what the kernel is freeing. This macro served the same purpose as __init, but is now very deprecated in favor of __init.
1) Init and Exit Calls Besides notifying or registering with kernel it also allocates any kernel memory required for device and initialize hardware. This function is called at boot time. In the same way we need to detach our driver from the system when our system is shutting down .
The __init__.py file makes Python treat directories containing it as modules. Furthermore, this is the first file to be loaded in a module, so you can use it to execute code that you want to run each time a module is loaded, or specify the submodules to be exported.
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)
These are only macros to locate some parts of the linux code into special areas in the final executing binary. __init
, for example (or better the __attribute__ ((__section__ (".init.text")))
this macro expands to) 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 beginning) 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 message:
Freeing unused kernel memory: 108k freed
To use this feature, you need a special linker script file, that tells the linker where to locate all the marked functions.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With