0

I'm struggling to understand how to use in the proper way the

try_module_get()

I found this interesting post: How to put a check in the code to ensure the inter kernel module dependency - Linux Kernel?

But I miss one point: I can get the request_module working, but I don' get how to use the try_module_get

I explain:

If I use

ret = request_module("ipt_conntrack")

the module xt_conntrack is correctly inserted, but marked as no used, because accordingly to the above post I didn't used the try_module_get.

But how can I call the try_module_get? The function requires a struct module parameter, which I don't know how to populate for the xt_conntrack module. I found several examples, but all related to the "THIS_MODULE" parameter, which does not apply in this case. Can you point me in the right direction for this?

Thanks for your help

cips
  • 95
  • 1
  • 8
  • I'm not sure what you are trying to do but perhaps you should be using `symbol_request()` and `symbol_put()`? However, the "xt_conntrack" module doesn't export any symbols, so maybe not. – Ian Abbott May 13 '20 at 14:41
  • Needs of `try_module_get` depends on what actually you want to do with the requested module (`ipt_conntrack`). Many use cases calls `try_module_get` automatically. Note also, that `request_module` way is used only in a very specific cases. So, make sure that you actually need to use this approach. – Tsyvarev May 13 '20 at 15:56
  • Yes, I definitely need it: if the xt_conntrack is loaded but not used, not all the connection tracking information are available. For example the call to "nf_ct_get "returns NULL. If the module is loaded and "registered" to be used (for example using an iptables rule) the infos are available. An old doc I found (http://inai.de/documents/Netfilter_Modules.pdf) explain it at chapter 4.3, but this is an outdated info right now, not working with newer kernels. That's why I thought to use "try_module_get". If you have any other way to do it, any suggestion is welcome – cips May 13 '20 at 16:08

1 Answers1

1

Perhaps something like this would work. I haven't tested it.

/**
 * 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;
}
Ian Abbott
  • 15,083
  • 19
  • 33
  • THANKS! I'm giving to this a first try and it seems to work....Now, I need to some some more extensive tests, but in any case that's exactly what I was looking for...even more to tell the true....;-) Thanks again!! I'll let you know anyway.... – cips May 13 '20 at 16:31
  • I confirm it works perfectly. Than I now have another issue, but the load and the try_module_get works perfectly. Thanks again – cips May 14 '20 at 11:52