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.
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:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With