Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 953525
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T00:03:58+00:00 2026-05-16T00:03:58+00:00

How to use linux kernel’s find_module() function? The documentation says must hold module_mutex. Does

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T00:03:58+00:00Added an answer on May 16, 2026 at 12:03 am

    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.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Does anyone know about the smallest Linux kernel I can use to play with?
Does the Linux kernel have a generic hash table implementation for use in kernel
I'm writing a linux kernel module that makes use of the exported symbol open_exec
I writing a linux kernel module that does some work with /proc... I'm trying
I would like to port a few applications that I use on Linux to
I will use the Linux NTFS driver as an example. The Linux kernel NTFS
I'm reading Understanding Linux Kernel. This is the snippet that explains how Linux uses
How does Linux determine the next PID it will use for a process? The
I did some of the modification in sched_setschedule() function of the linux kernel. successfully
On an x86 system, I have a Linux kernel module (watcher module) that gets

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.