6

如何使用linux内核的find_module()功能?文档说“必须持有 module_mutex”。

  1. 这是否意味着在搜索指向另一个的指针之前我应该​​在我的模块代码中获得一个锁?
  2. 当这个互斥锁被非模块内核代码锁定时?

语境

我正在调试一组一起工作的内核模块。

模块 A 调用模块 B 的函数。在模块 A 的函数 C 中的某个时刻,模块 B 的使用计数变为无效。我已经确定这在模块 B 的函数中没有发生。我想从 C 中调试模块 B 的使用计数。为此,我将使用find_module()来获取指向 B 的指针。

4

2 回答 2

1

我建议在您的代码中更加防御:

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

int do_my_work(void)
{
    struct module *mod;
    char name[MODULE_NAME_LEN];
    int ret, forced = 0;

    if (!capable(CAP_SYS_MODULE) || modules_disabled)
        return -EPERM;

    /* Set up the name, yada yada */
    name[MODULE_NAME_LEN - 1] = '\0';

    /* Unless you absolutely need an uninterruptible wait, do this. */
    if (mutex_lock_interruptible(&module_mutex) != 0) {
        ret = -EINTR;
        goto out_stop;
    }

    mod = find_module(name);
    if (!mod) {
        ret = -ENOENT;
        goto out;
    }

    if (!list_empty(&mod->modules_which_use_me)) {
        /* Debug it. */
    }

out:
    mutex_unlock(&module_mutex);
out_stop:
    return(ret);
}

module_mutex是内核在对模块的各种操作中获取的。它们都在/kernel/module.c中并且是:

  • 单独初始化每个模块以及所有模块时(例如在启动时)。
  • 删除模块
  • 等到没有人引用(使用)模块。
  • 当 /proc 文件系统需要一个模块列表时(oprofile 和 co. 使用它)。
  • 在tracepoint相关代码中;迭代和更新跟踪点。
于 2010-07-28T08:20:47.423 回答
0

1) 是的。module_mutex在调用之前进入你的模块find_module()

2)不在模块代码之外使用

例子:

struct module *mod;

mutex_lock(&module_mutex);

mod = find_module("MODULE_NAME");

if(!mod) {
    printk("Could not find module\n");
    return;
}

mutex_unlock(&module_mutex);
于 2010-07-23T20:22:02.727 回答