如何使用find_module?

发布于 2024-09-11 02:37:24 字数 531 浏览 2 评论 0原文

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

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

上下文

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

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

How to use linux kernel's find_module() function?
The documentation says "must hold module_mutex".

  1. Does that mean that I should acquire
    a lock in my module code before
    searching for a pointer to another?
  2. When this mutex is locked by
    non-module kernel code?

Context

I'm debugging a set of kernel modules working together.

Module A call functions of module B. At some point in function C of module A a use count of module B goes invalid. I've determined that this is not happening in function of module B. I'd like to debug use count of module B from C. To do this I'm going to use find_module() to obtain a pointer to B.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦中的蝴蝶 2024-09-18 02:37:24

我建议在代码中采取更多的防御措施:

#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相关代码中;迭代并更新跟踪点。

I would suggest being a little more defensive in your code:

#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 is acquired by the kernel in various operations on modules. All of them are in /kernel/module.c and are:

  • When initializing each module individually, as well as all the modules (at boot, for instance).
  • Deleting a module
  • Waiting until a module is referenced (used) by nobody.
  • When the /proc filesystem needs a list of modules (oprofile and co. makes use of this).
  • In tracepoint related code; iterating through and updating tracepoints.
我一直都在从未离去 2024-09-18 02:37:24

1)是的。在调用 find_module() 之前在模块中获取 module_mutex

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);

1) Yes. Get module_mutex in your module before calling find_module()

2) It's not used outside the module code

Example:

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文