What does __init mean in the Linux kernel code?

CLinux

C Problem Overview


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.

C Solutions


Solution 1 - C

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)

Solution 2 - C

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.

Solution 3 - C

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

Solution 4 - C

__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

Solution 5 - C

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.

Solution 6 - C

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJeegar PatelView Question on Stackoverflow
Solution 1 - CSangeeth SaravanarajView Answer on Stackoverflow
Solution 2 - CsashoalmView Answer on Stackoverflow
Solution 3 - Cnaveen kumar rView Answer on Stackoverflow
Solution 4 - CGeoffroyView Answer on Stackoverflow
Solution 5 - CelmoView Answer on Stackoverflow
Solution 6 - CNnaikView Answer on Stackoverflow