[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: MOD_INC_USE_COUNT



Hello.

T.L.Madhu wrote:

> On Mon, 9 Dec 2002, Edward Gerhold wrote:
> 
> 
>> 
>> Jose Luis Alarcon wrote:
>> 
>> 
>>>   Hi, kernel developers.
>>> 
>>>   I has a big surprise when, adding at the beginning of init_module()
>>> the macro MOD_INC_USE_COUNT, for have a module use counter. My program
>>> get compile well and insmod work too.
>>> 
>>>   But the surprise was when i did rmmod, cos the output said:
>>> 
>>> FATAL: the module is in use
>>> 
>>>   I did lsmod, and i got that my module was used by [unsafe].
>>> 
>>>   Any kernel programmer with experience knows why happen this?.
>>> 
>>>   IMHO a use counter is a very few dangerous thing. So i was trying with.
>>> 
>>>   Thanks you, very much, in advance.
>>> 
>>>   Regards.
>>> 
>>>   Jose.
>>> 
>> 
>> I don't know why this happens,
>> but can it be, that you forgot MOD_DEC_USE_COUNT in your cleanup routine ?
>> 
>> bye
>> Edward
>> 
> 
> 
> Basically, adding MOD_INC_USE_COUNT in init_module() is not a right
> idea, usage counts are used to keep track of current users of the module &
> the system doesn't allow removing the module if usage count is non-zero. 
> 
> In your case, 
> - You have incremented usage count to 1 during init_module()
> - When you run rmmod, it calls delete_module() system call which 
>   checks for the usage count, if it is zero, then ONLY it calls the
>   cleanup routine of corresponding module, if it is non-zero, system
>   call returns EBUSY error thinking someone is using this module, so 
>   even if you do add MOD_DEC_USE_COUNT in clenup routine, it's of no
>   use. For more info. look for implementation of sys_delete_module().
> 
> bye,
> Madhu
> 

Hmmm. That 's new for me. In my Books ldd (german) on page 36
and lnx-networkarchitecture (german) on page 37 they write, we
have to use the MOD_INC_.. and MOD_DEC_.. macros to set MOD_IN_USE.
MOD_INC_USE_COUNT has to be used _before_ opening any devices.
MOD_DEC_USE_COUNT has to be used _after_ closing any devices.
(Wehrle writes: A dev->open() increases the usage-counter by 1,
  a dev->close() decreases the counter by 1 - both automatically)
MOD_IN_USE is used to prevent from a module_cleanup when any devices
which need the module are busy. Having the device busy, but the
module removed, would cause segmentation faults or kernel panics.
MOD_IN_USE can be influenced via ioctl.

Now I don't understand why i shouldn't use the macros. I've read now
a few times that i'll have to (i remember also lkmpg gives the advice
to use them, the book linux-kernelprogramming (german) gives the advice,
too). Who can lead me out ?

But anyways. In my case, i think i've made a mistake.
MOD_INC_USE_COUNT hasn't to be used as first in module_init().
MOD_DEC_USE_COUNT hasn't to be used as last in module_cleanup().
(You explained why the module_cleanup isn't called.)
They have to be used, if i open and close my devices (if it's not
done automatically) or if i work on the other ressources...
(turn me around, the other ressources work with my module)

kernel/module.c:
sys_delete_module() is clear now. I've read it, and i've understood
the if-blocks, data, flags. But it doesn't help against my general
confusion about "use the macros always or use the macros never" now.
Who can lead me out ?


Thanks a lot.
Edward

--
Kernelnewbies: Help each other learn about the Linux kernel.
Archive:       http://mail.nl.linux.org/kernelnewbies/
FAQ:           http://kernelnewbies.org/faq/