0

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.

Long Jack
  • 3
  • 1
  • 2
    Then its like all the rest of the code in the regular text section. – stark May 17 '23 at 10:48
  • 1
    Does this answer your question? [What does \_\_init mean in the Linux kernel code?](https://stackoverflow.com/questions/8832114/what-does-init-mean-in-the-linux-kernel-code) – Tsyvarev May 17 '23 at 17:45

1 Answers1

1

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]
    
Marco Bonelli
  • 63,369
  • 21
  • 118
  • 128