RE: More about MOD_INC_USE_COUNT

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

 



No, you don't want to put MOD_INC_USE_COUNT in init_module(), thats doomed
for failure.

You want to put it in your open handler.  This is the handler that gets
called when a user mode app opens a handle (file descriptor) to your driver
using fd=open("/dev/mydriver",...).

Your driver open() handler should get called and look something like:

static int mydriver_open(struct inode* inode, struct file *file)
{
	// Open Device Handle.
	// Do what ever you need to here....

	// Increment usage counter.
	MOD_INC_USE_COUNT;
	return 0;
}

When the user closes the drivers handle (e.g. close(fd) ), your driver will
get its release() handler called, at which point it should call
MOD_DEC_USE_COUNT.
So it should look something like this:

static int mydriver_release(struct inode* inode, struct file *file)
{
	// Close Device Handle.
	
	// Cleanup code goes here....

	// Decrement usage counter.
	MOD_DEC_USE_COUNT;
	return 0;
}

My understanding is that if you do a:
# rmmod mydriver
and a user has not closed a handle to your driver then the kernel will fail
the rmmod and not call your drivers release handler, which makes sense
right.

I hope now you can see now why calling MOD_INC_USE_COUNT from your
init_module() func is so bad.

John Levon says you should not call MOD_INC_USE_COUNT in the 2.5 kernel at
all.
I am interested to know why he said that, is the 2.5 kernel keeping track of
this internally now John ?

HTH
dom



> -----Original Message-----
> From: Jose Luis Alarcon [mailto:jlalarcon@tiggerfan.com]
> Sent: Sunday, December 08, 2002 4:22 PM
> To: kernelnewbies@nl.linux.org
> Subject: More about MOD_INC_USE_COUNT
> 
> 
> --- Jose Luis Alarcon <jlalarcon@tiggerfan.com> wrote:
> >--- John Levon <levon@movementarian.org> wrote:
> >>On Sun, Dec 08, 2002 at 12:51:43PM -0800, Jose Luis Alarcon wrote:
> >>
> >>>   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
> >>
> >>Why are you surprised ? You marked the module as used by 
> something when
> >>you load it, of course it can't be unloaded.
> >>
> >
> >  Jonh: first, thank you very much for answer my question.
> >
> >  I am surprised cos if the line MOD_INC_USE_COUNT; is 
> commented, then
> >the lsmod command say that the module is used by 0 (unused, 
> i think) and
> >the rmmod command uninstall the module without nothing to say.  
> >
> >>>   I did lsmod, and i got that my module was used by [unsafe].
> >>
> >>You should not be using MOD_INC_USE_COUNT in 2.5 kernels. And you
> >>definitely shouldn't be doing it in init_module(), that 
> makes no sense.
> >>
> >
> >  Can you explain, please, why the macro have no sense in 
> init_module()?, maybe
> >(i am thinking about it) use this counter make no sense in 
> any place at the 
> >module, but i am learning and i only want to see it working.
> >
> >>What do you actually believe MOD_INC_USE_COUNT does ?
> >>
> >
> >  I was reading that create a counter that count the number 
> of times that the
> >module is used, is it right?
> >
> >>regards
> >>john
> >>
> >
> >  Thank you, Jonh, again.
> >
> >  Regards.
> >
> >  Jose.
> >
> 
>   One more thing about this:
> 
>   I put 'MOD_INC_USE_COUNT;' like the last line just before 
> the key (}) that
> ends the cleanup_module() void function....and the program 
> load with insmod
> and unload with rmmod without problems. lsmod tells that the 
> module is used 
> by 0 (unused). All this with the 2.5.49 kernel. Maybe was a 
> problem of the
> "situation" of the line.......
> 
>   Regards.
> 
>   Jose.
> 
> Debian GNU/Linux 'Sid' Kernel 2.4.19 Ext3.
> ESware Linux 365 Kernel 2.5.49 ReiserFS.
> Registered Linux User #213309.
> Memories..... You are talking about memories.
> Rick Deckard. Blade Runner.
> 
> _____________________________________________________________
> Get your own free tiggerfan.com email address!!
> DisneySites!! - http://www.disneysites.com/webmail/tiggerfan
> 
> _____________________________________________________________
> Select your own custom email address for FREE! Get 
> you@yourchoice.com w/No Ads, 6MB, POP & more! 
http://www.everyone.net/selectmail?campaign=tag
--
Kernelnewbies: Help each other learn about the Linux kernel.
Archive:       http://mail.nl.linux.org/kernelnewbies/
FAQ:           http://kernelnewbies.org/faq/
--
Kernelnewbies: Help each other learn about the Linux kernel.
Archive:       http://mail.nl.linux.org/kernelnewbies/
FAQ:           http://kernelnewbies.org/faq/


[Index of Archives]     [Newbies FAQ]     [Linux Kernel Mentors]     [Linux Kernel Development]     [IETF Annouce]     [Git]     [Networking]     [Security]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux SCSI]     [Linux ACPI]
  Powered by Linux