What are the __init* and __exit* macros ?

本文详细解释了Linux内核中init与exit宏的作用及使用方法。init宏将函数或变量放置于特殊的.init.text或.init.data段,以便在内核启动后释放这些一次性使用的代码和数据;而exit宏则用于模块卸载时调用的函数,它们被置于.exit.text或.exit.data段。文章还提供了一个模块的实例代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

What are the __init* and __exit* macros ?

The ''init* and exit* macros are widely used in the kernel. These macros are defined in include/linux/init.h :

#define __init          __attribute__ ((__section__ (".init.text")))
#define __initdata      __attribute__ ((__section__ (".init.data")))
#define __exitdata      __attribute__ ((__section__(".exit.data")))
#define __exit_call     __attribute_used__ __attribute__ ((__section__ (".exitcall.exit")))
 
#ifdef MODULE
#define __exit          __attribute__ ((__section__(".exit.text")))
#else
#define __exit          __attribute_used__ __attribute__ ((__section__(".exit.text")))
#endif

__init* macros

It tells the compiler to put the variable or the function in a special section, which is declared in vmlinux.lds. init puts the function in the ".init.text" section and initdata puts the data in the ".init.data" section.

For example, the following declaration means that the variable md_setup_ents will be put in the the init data section.

static int md_setup_ents __initdata;

But why must you use these macros ?

Let's take an example, with the following function, defined in mm/slab.c :

void __init kmem_cache_init(void)

This function initializes the slab system : it's only used once, at the boot of the kernel. So the code of this function should be freed from the memory after the first call. It's the goal of free_initmem().

  • The function free_initmem() will free the entire text and data init sections and so the code of your function,, if it has been declared as init.

__exit* macro

The exit macro tells the compiler to put the function in the ".exit.text" section. The exit_data macro tells the compiler to put the function in the ".exit.data" section.

exit.* sections make sense only for the modules : exit functions will never be called if compiled statically. That's why there is a ifdef : exit.* sections will be discarded only if modules support is disabled.

Prototype of a module

A module must use the init and exit macros. Here is a prototype of a module :

#include <linux/module.h>
#include <linux/kernel.h>

#define MODULE_AUTHOR "tyler@agat.net"
#define MODULE_DESC "Description of the module"

int __init init_function(void)
{
        /* Do something */
        if (err)
                return -ERR;
        return 0;
}

void __exit exit_function()
{
        /* Do something */
}

module_init(init_function);
module_exit(exit_function);

MODULE_LICENSE("GPL");
MODULE_AUTHOR(MODULE_AUTHOR);
MODULE_DESCRIPTION(MODULE_DESC);
http://kernelnewbies.org/FAQ/InitExitMacros


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值