Reputation: 23921
How to use linux kernel's find_module()
function?
The documentation says "must hold module_mutex".
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.
Upvotes: 6
Views: 2113
Reputation: 40839
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:
Upvotes: 1
Reputation: 4928
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);
Upvotes: 0