Is __init necessary for loadable modules

What is __init?

__init is a macro defined in linux/init.h file


#define __init      __section(.init.text) __cold  __latent_entropy __noinitretpoline

What is the use of __init?

This macro informs the compiler to put the text for this function in a special section named ".init.text".
This way,  all the functions with __init are placed in a single ELF Section. This section is removed after all the init functions with __init are called.

You can see the following line in "Freeing Unused Kernel memory" in dmesg output, which is nothing but freeing up all the pages of init.text section.


This is performed by function free_initmem() present in arch/x86/mem/init.c, each and every architecture will have their own free_initmem() definition


The linker script merges all "init.text" sections into a single section and assigns an initial address to it.  The linker script is present in "arch/x86/kernel/vmlinux.lds.S" .

You can run the below command to check "init.text" section is present in vmlinux or not

$readelf -S vmlinux | grep init.text

Whether adding __init frees memory for loadable modules?

No, if we add __init to loadable modules, kernel does not free the memory after the initialization function exits, as free_initmem() is called when the kernel starts.


Another reason for not freeing is that the size of init functions will be of hundred bytes, and the hardware page sizes is 4 KB. So, to support freeing few hundred bytes of loadable modules is more complex than for builtin

Comments

Popular posts from this blog

bb.utils.contains yocto

Difference between RDEPENDS and DEPENDS in Yocto

make config vs oldconfig vs defconfig vs menuconfig vs savedefconfig