How to understand the macro __init?

Ezequiel Garcia elezegarcia at gmail.com
Tue Aug 14 08:09:35 EDT 2012


On Tue, Aug 14, 2012 at 8:46 AM, 王哲 <wangzhe5004 at gmail.com> wrote:
>
>
> 2012/8/14 Ezequiel Garcia <elezegarcia at gmail.com>
>>
>> Hey wanny,
>>
>> On Tue, Aug 14, 2012 at 1:04 AM, Mulyadi Santosa
>> <mulyadi.santosa at gmail.com> wrote:
>> > Hi.. :)
>> >
>> > On Tue, Aug 14, 2012 at 9:14 AM, 王哲 <wangzhe5004 at gmail.com> wrote:
>> >> i use the __init for function print_k.
>> >> in my opinion  after the fisrt invoking the print_k in the hello_init.
>> >> the memory of print_k will be freed,and the second invoking will
>> >> not be executed.but the result of second invoking is executing .
>> >>
>> >> why?
>> >
>> > because you're still in module_init.... :)
>> >
>> >
>>
>> Yes, and the function is still in the stack!
>>
>> On the other hand.. think what would happen if things would work
>> like you say.
>>
>> We would have a *very* strange behavior,
>> and pretty counter-intuitive, don't you think so?
>
>
>    Thank you very much  for reply.
>    as you say,and function is still in the stack,don't be freed
>    in the memory,what is __init had done? who can give a sample example
>   to explain the difference existing __init or not?

This is documented in Documentation/DocBook/....

    After boot, the kernel frees up a special section; functions
    marked with __init and data structures marked with
    __initdata are dropped after boot is complete: similarly
    modules discard this memory after initialization.  __exit
    is used to declare a function which is only required on exit: the
    function will be dropped if this file is not compiled as a module.
    See the header file for use. Note that it makes no sense for a function
    marked with __init to be exported to modules with
    EXPORT_SYMBOL() - this will break.

I'll try to put it in plain english, I'm sure someone will correct
me if I mess up:

When the kernel starts (machine boots), kernel code and data are
loaded onto RAM*.
But some code and some data is only needed for initialization, and
there's no point
on keeping them around after this stage is done. So it gets "dropped",
i.e. it's removed from RAM. But of course, this is done when the kernel
knows it won't need it anymore.

Since a module is like a little piece of kernel that gets loaded dynamically,
I guess you can think it work more or less the same way.

Hope it's clear now (and hope it's correct :-)

Ezequiel.

* This is pretty much like any executable binary, except kernel won't page out
so it has to be completely loaded in RAM.



More information about the Kernelnewbies mailing list