Linux中的模块编程 [英] Module Programming in linux

查看:135
本文介绍了Linux中的模块编程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是简单的模块程序代码.

here is the simple module program code.

#include <linux/module.h>   /* Needed by all modules */
#include <linux/kernel.h>   /* Needed for KERN_INFO */
#include <linux/init.h>     /* Needed for the macros */

static int hello3_data __initdata = 3;

static int __init hello_3_init(void)
{
    printk(KERN_INFO "Hello, world %d\n", hello3_data);
    return 0;
}

static void __exit hello_3_exit(void)
{
    printk(KERN_INFO "Goodbye, world %d\n",hello3_data);
}

module_init(hello_3_init);
module_exit(hello_3_exit);

我已经使用__initdata宏初始化了一个变量,并使用了__init初始化了一个函数.当我执行insmod时,模块被插入,我能够看到日志消息(/var/log/messages).但是,当我执行rmmod时,它无法将其删除,并且显示为Resource busy.

I've initialized a variable with __initdata macro and a function with __init. when I did insmod the module was inserted and i was able to see the log message(/var/log/messages). But, when I did rmmod it was unable to remove it, and it says Resource busy.

My question is does the `modules_init` function cleans the memory of `hello3_data`??

or it is done by `module_exit`??.  

请有人解释.

推荐答案

在声明变量hello3_data时,请使用__initdata修饰符,该修饰符指定仅在__init函数中使用该变量.

Please when declaring the varialble hello3_data you're using the __initdata modifier which specifies that the variable shall be used in only the __init function.

尝试使用make CONFIG_DEBUG_SECTION_MISMATCH=y之类的选项重新编译内核模块,您将看到如下警告:

Try recompile your kernel module with options like make CONFIG_DEBUG_SECTION_MISMATCH=y and you shall see warnings like this:

警告:/home/pengyu/temp/yusen/test_mod.o(.exit.text+0x3):从函数cleanup_module()到变量.init.data:hello3_data
的引用节不匹配 函数__exit cleanup_module()引用 变量__initdata hello3_data.
在退出函数中进行错误处理时,通常会看到这种情况 在初始化路径中使用功能.
解决方法通常是删除__initdata批注 hello3_data,因此可以在init节之外使用.

WARNING: /home/pengyu/temp/yusen/test_mod.o(.exit.text+0x3): Section mismatch in reference from the function cleanup_module() to the variable .init.data:hello3_data
The function __exit cleanup_module() references a variable __initdata hello3_data.
This is often seen when error handling in the exit function uses functionality in the init path.
The fix is often to remove the __initdata annotation of hello3_data so it may be used outside an init section.

您只需删除__initdata然后重试.

在这里,我试图提供进一步的解释.内核模块本身的格式为 ELF (Executable and Linkable Format) (其中包含一些特定于内核模块的部分). .init.fini部分的功能受链接器和加载器(包括insmod)支持.

Here I'm trying to provide further explanations. The kernel module itself is in the format of ELF (Executable and Linkable Format) (with some kernel-module-specific sections). Feature of the .init and .fini sections is supported by the linkers and loaders including insmod.

在这种情况下,属性#define __initdata __section(.init.data)的作用与__attribute__((section(".init.data")))相似,该属性明确指定应将数据/函数放入哪个部分.

In this case the attribute #define __initdata __section(.init.data) works like __attribute__((section(".init.data"))) which explicitly specifies which section the data/function shall be put in.

作为内核模块,不能保证在模块初始化后保留.init节,并且不应在初始化函数之外引用该节内的任何内容.请参见 Linux设备驱动程序,第三版的第31页:

As a kernel module the section of .init is not guaranteed to be kept after module initialization, and anything inside that section is not supposed to be referenced outside the initialization function. See page 31 of Linux Device Drivers, Third Edition:

定义中的_ init标记可能看起来有些奇怪;这向内核暗示了给定的功能仅在初始化时使用.模块加载器在模块加载后会放弃初始化功能,从而使其内存可用于其他用途.对于仅在初始化期间使用的数据,有一个类似的标记( _initdata). __init和__initdata的使用是可选的,但这是值得的.只需确保不要将它们用于初始化完成后将要使用的任何功能(或数据结构)

The _init token in the definition may look a little strange; it is a hint to the kernel that the given function 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

这篇关于Linux中的模块编程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆