I have known that the __init will put the linux module init function into a special ELF section and can be overwrite by kernel after loading the module. But my question is that what if I remove this __init mark and what will happen? I try to write a simple demo without the __init prefix. So it only will keep the module init function code all the time until it is unloaded?
I tried search on google, stackoverflow and others, without finding same question and answer. I expecting the answer.
If you don't mark it as __init
the kernel build system will simply not know that it is an init function. As a result of this, it will not be placed in .init.text
, but in .text
along with other functions.
When the module is loaded into the kernel, after calling the function registered as module_init()
, the kernel will remove the .init.text
section, so in case your function is not marked as __init
it will keep existing in memory.
A quick test with /proc/kallsyms
confirms this.
With a modinit
function marked as __init
:
/ # insmod test.ko
/ # cat /proc/kallsyms | grep -F [test]
ffffffffc0000000 t modexit [test]
ffffffffc0000000 t cleanup_module [test]
With a modinit
function not marked as __init
:
/ # insmod test.ko
/ # cat /proc/kallsyms | grep -F [test]
ffffffffc0000000 t modinit [test]
ffffffffc0000003 t modexit [test]
ffffffffc0000003 t cleanup_module [test]
ffffffffc0000000 t init_module [test]