views:

64

answers:

2

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.

A: 

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);
JayM
A: 

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.
Michael Foukarakis