如何使用find_module?

Bas*_*evs 6 kernel-module linux-kernel

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

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

上下文

我正在调试一组协同工作的内核模块.

模块A模块B的调用功能.在模块A的功能C中的某一点,模块B的使用计数无效.我已经确定这不是在模块B的功能中发生的.我想从C调试模块B的使用计数.为此,我将使用find_module()来获取指向B的指针.

Mic*_*kis 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);
}
Run Code Online (Sandbox Code Playgroud)

module_mutex由内核在对模块的各种操作中获取。所有这些都位于/kernel/module.c中,并且是:

  • 单独初始化每个模块以及所有模块时(例如在启动时)。
  • 删除模块
  • 等待直到没有人引用(使用)模块。
  • 当 /proc 文件系统需要模块列表时(oprofile 和 co. 利用了这个)。
  • 在tracepoint相关代码中;迭代并更新跟踪点。