Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does __init mean in the Linux kernel code?

Tags:

c

linux

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.

like image 428
Jeegar Patel Avatar asked Jan 12 '12 08:01

Jeegar Patel


People also ask

What is __ init in Linux kernel?

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.

What does Linux init h do?

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.

What is the use of init and exit functions in a device driver?

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 .

What does module init do?

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.


2 Answers

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) 
like image 58
Sangeeth Saravanaraj Avatar answered Sep 16 '22 17:09

Sangeeth Saravanaraj


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.

like image 38
sashoalm Avatar answered Sep 18 '22 17:09

sashoalm