Sandy
Sandy

Reputation: 65

Usage of __init & __exit attributes

What happens if __init & __exit attributes are used in intialization and exit modules & what happens if i don't use them. Example as shown below.

Using Attributes

static __init int myinit(void)
{}
static __exit void myexit(void)
{}

Witout attributes

static int myinit(void)
{}
static void myexit(void)
{}

Upvotes: 6

Views: 2288

Answers (2)

vinay hunachyal
vinay hunachyal

Reputation: 3901

The main difference is freeing of memory .

The__init token in the it is a hint to the kernel that the givenfunction is used only at initialization time. The module loader drops the initialization function after the module is loaded, making its memory available for other uses. There is a similar tag (__initdata)for data used only during initialization. Use of __init and __initdata is optional, but it is worth the trouble. Just be sure not to use them for any function (or data structure)you will be using after initialization completes.

use of the __init family of macros to place one-time initialization routines into a common section in the object file.Its cousin __initdata , used to mark one-time-use data items . Functions and data marked as initialization using these macros are collected into a specially named ELF section.

Later, after these one-time initialization functions and data objects have been used, the kernel frees the memory occupied by these items. You might have seen the familiar kernel message near the final part of the boot process saying, "Freeing init memory: 296K." .

The purpose of placing this function into a special section of the object file is so the memory space that it occupies can be reclaimed when it is no longer needed.

Upvotes: 4

Gautham Kantharaju
Gautham Kantharaju

Reputation: 1763

@Sandy, The __init macro causes the init function to be discarded and its memory(vmalloc) freed once the init function finishes for built-in drivers. The __exit macro causes the omission of the function when the module is built into the kernel. Both __init and __exit will not hold good for the LKM. Also go through these links What does __init mean in the Linux kernel code? http://amar-techbits.blogspot.in/2012/08/understanding-macro-init-and-exit-in.html

Upvotes: 5

Related Questions