How to use find_module?

How to use linux kernel find_module() function? The documentation states that "must contain module_mutex".

  • Does this mean that I have to acquire a lock in my modular code before looking for a pointer to another?
  • When is this mutex blocked by non-modular kernel code?

Context

I am debugging a set of kernel modules working together.

Module. The calling functions of module B. At some point, the function C of module A uses the usage counter of module B is invalid. I determined that this does not happen in the function of module B. I would like to debug the use of counting module B from C. To do this, I am going to use find_module () to get a pointer to B.

+6
linux-kernel kernel-module
source share
2 answers

I would suggest being a little more protective 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 module operations. They are all located in /kernel/module.c and are as follows:

  • When initializing each module separately, as well as all modules (for example, when loading).
  • Removing a module
  • Waiting until no one refers to the module (in use).
  • When the / proc file system requires a list of modules (oprofile and co. Use this).
  • In trace-related code; Iterate and update trace points.
+1
source share

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

2) It is 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); 
0
source share

All Articles