Search code examples
linux-kernelinitializationkernel-moduleinit

Why is __exit used in kernel module programming?


If there isn't going to be any memory allocation in case the module is built into the kernel or if module unloading is not allowed then why use it in the first place ? Please correct me if I am wrong/assuming anything wrong.

Edit - I am also not able to understand what purpose does __exit serve. module_exit() should be enough or not ?


Solution

  • Simple answer: it's there so you can make a module unloadable without imposing a penalty if your module turns out not to be unloadable (e.g. it's built-in, or the kernel doesn't support module unloading).

    Not all modules make sense to unload, but if your driver could feasibly be unloaded, adding support for that makes sense and will make others (who use your drivers) happier.

    As for the macro itself: __exit expands to a special directive that tells the compiler to discard the function if the module cannot be unloaded. This saves on code space if the exit functions won't be called.