views:

1094

answers:

4

If I load a kernel module and list the loaded modules with lsmod, I can get the "use count" of the module (number of other modules with a reference to the module). Is there a way to figure out what is using a module, though?

The issue is that a module I am developing insists its use count is 1 and thus I cannot use rmmod to unload it, but its "by" column is empty. This means that every time I want to re-compile and re-load the module, I have to reboot the machine (or, at least, I can't figure out any other way to unload it).

A: 

You might try lsof or fuser.

jedihawk
Did you actually try this?
Robert Gamble
I thought of that initially, but it does't work.
mipadi
+1  A: 

All you get are a list of which modules depend on which other modules (the Used by column in lsmod). You can't write a program to tell why the module was loaded, if it is still needed for anything, or what might break if you unload it and everything that depends on it.

Norman Ramsey
+2  A: 

rmmod has a --force parameter. If you know the stuff your module does, and have a kernel configured to support forcing unload, that might work. That's to save you from having to restart until you fixed the problem with the ref-counting), but it won't show you the cause of the load of your module. I think it's not possible to get why the module was loaded in the first place (i.e which exact code-path). Maybe dmesg has logged something useful or you can add some useful logging into your module code.

Johannes Schaub - litb
A: 

It says on the Linux Kernel Module Programming Guide that the use count of a module is controlled by the functions try_module_get and try_module_put. Perhaps you can find where these functions are called for your module.

haggai_e