Linux Kernel Modules: When to use try_module_get / module_put

前端 未结 1 1647
独厮守ぢ
独厮守ぢ 2020-12-14 02:17

I was reading the LKMPG ( See Section 4.1.4. Unregistering A Device ) and it wasn\'t clear to me when to use the try_module_get / module_put functions. Some of

相关标签:
1条回答
  • 2020-12-14 02:57

    You should essentially never have to use try_module_get(THIS_MODULE); pretty much all such uses are unsafe since if you are already in your module, it's too late to bump the reference count -- there will always be a (small) window where you are executing code in your module but haven't incremented the reference count. If someone removes the module exactly in that window, then you're in the bad situation of running code in an unloaded module.

    The particular example you linked in LKMPG where the code does try_module_get() in the open() method would be handled in the modern kernel by setting the .owner field in struct file_operations:

    struct file_operations fops = {
            .owner = THIS_MODULE,
            .open = device_open,
            //...
    };
    

    this will make the VFS code take a reference to the module before calling into it, which eliminates the unsafe window -- either the try_module_get() will succeed before the call to .open(), or the try_module_get() will fail and the VFS will never call into the module. In either case, we never run code from a module that has already been unloaded.

    The only good time to use try_module_get() is when you want to take a reference on a different module before calling into it or using it in some way (eg as the file open code does in the example I explained above). There are a number of uses of try_module_get(THIS_MODULE) in the kernel source but most if not all of them are latent bugs that should be cleaned up.

    The reason you were not able to unload the sched example is that your

    $ tail /proc/sched -f &
    

    command keeps /proc/sched open, and because of

            Our_Proc_File->owner = THIS_MODULE;
    

    in the sched.c code, opening /proc/sched increments the reference count of the sched module, which accounts for the 1 reference that your lsmod shows. From a quick skim of the rest of the code, I think if you release /proc/sched by killing your tail command, you would be able to remove the sched module.

    0 讨论(0)
提交回复
热议问题