Kernel Linux内核:如何使用request_module()和try_module_get()

Kernel Linux内核:如何使用request_module()和try_module_get(),kernel,linux-device-driver,kernel-module,linuxkit,Kernel,Linux Device Driver,Kernel Module,Linuxkit,我正在努力理解如何正确使用 try_module_get() 我发现这个有趣的帖子: 但我忽略了一点:我可以让request_模块工作,但我不知道如何使用try_模块 我解释: 如果我使用 ret=请求模块输入跟踪 模块xt_conntrack已正确插入,但标记为未使用,因为根据上面的帖子,我没有使用try_模块 但是我如何调用try_模块?该函数需要一个struct module参数,我不知道如何为xt_conntrack模块填充该参数。我找到了几个示例,但都与THIS_MODULE参数有关

我正在努力理解如何正确使用

try_module_get()
我发现这个有趣的帖子:

但我忽略了一点:我可以让request_模块工作,但我不知道如何使用try_模块

我解释:

如果我使用

ret=请求模块输入跟踪

模块xt_conntrack已正确插入,但标记为未使用,因为根据上面的帖子,我没有使用try_模块

但是我如何调用try_模块?该函数需要一个struct module参数,我不知道如何为xt_conntrack模块填充该参数。我找到了几个示例,但都与THIS_MODULE参数有关,该参数在本例中不适用。 你能为我指出正确的方向吗


谢谢你的帮助

也许像这样的方法会奏效。我还没有测试过

/**
 * try_find_module_get() - Try and get reference to named module
 * @name: Name of module
 *
 * Attempt to find the named module.  If not found, attempt to request the module by
 * name and try to find it again.  If the module is found (possibly after requesting the
 * module), try and get a reference to it.
 *
 * Return:
 * * pointer to module if found and got a reference.
 * * NULL if module not found or failed to get a reference.
 */
static struct module *try_find_module_get(const char *name)
{
    struct module *mod;

    mutex_lock(&module_mutex);
    /* Try and find the module. */
    mod = find_module(name);
    if (!mod) {
        mutex_unlock(&module_mutex);
        /* Not found.  Try and request it. */
        if (request_module(name))
            return NULL;  /* Failed to request module. */
        mutex_lock(&module_mutex);
        /* Module requested.  Try and find it again. */
        mod = find_module(name);
    }
    /* Try and get a reference if module found. */
    if (mod && !try_module_get(mod))
        mod = NULL;  /* Failed to get a reference. */
    mutex_unlock(&module_mutex);
    return mod;
}

我不知道你想做什么,但也许你应该使用symbol\u request和symbol\u put?但是,xt_conntrack模块不会导出任何符号,因此可能不会。try_module_get的需要取决于您实际希望对请求的模块ipt_conntrack执行什么操作。许多用例调用try\u模块自动获取。另外请注意,请求模块方式仅在非常特定的情况下使用。因此,请确保您确实需要使用这种方法。是的,我确实需要它:如果xt_conntrack已加载但未使用,则并非所有连接跟踪信息都可用。例如,对nf_ct_get的调用返回NULL。如果加载并注册模块以供使用,例如使用iptables规则,则信息可用。我在第4.3章找到了一个老文档解释了这一点,但现在这是一个过时的信息,不适用于新内核。这就是为什么我想使用try_module_get。如果你有其他的方法,欢迎你的建议!我是第一次尝试,它似乎是有效的…现在,我需要一些更广泛的测试,但在任何情况下,这正是我所寻找的…甚至更多地告诉真实情况….;-再次感谢!!无论如何,我会让你知道的……我确认它工作得很好。现在我有另一个问题,但是加载和try_模块_get工作得很好。再次感谢