Re: file_operations owner field.

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

 



Yes got it, thanks a lot for your help.

This is how it works

struct file_operations
{
 .......
 struct module *owner;
 .......
};

struct module
{
 .....
 struct module_ref ref[NR_CPUS];
 .....
} ;

Struct module_ref is nothing but an automic variable count.

There are two functions try_module_get() and module_put() which modify this reference count.

int try_module_get (struct module *m)
{
  ret=1 ;
  if ( module )
  {
     get the cpu number and increment the per cpu reference count.
  }
  return ret ;
}

void module_put (struct module *m)
{
  if (module)
  {
    get the cup and decrement the per cpu count;
  }
}


THESE FUNCTIONS ARE USED IN CHARACTER DEVICES AS FOLLOWS

int chrdev_open(struct inode * inode, struct file * filp)
   calls cdev_get()

and cdev_get () --> calls try_module_get(owner)


So, kernel calls try_module_get() amd module_put() by itself, so if the owner field is initialized then the reference counts are managed by kernel. But, if it is not initialized then the reference counting must be done manually.

In 2.4 there where two macors MDO_INC/DEC_COUNT which were used by module developer to do reference counting, it got changed in 2.6.

The link contains the same discussion that went on to change the module reference counting
http://linux.derkeiler.com/Mailing-Lists/Kernel/2003-07/235index.html

Thanks and regards,
Prasad.

On 6/7/07, pgssgp@xxxxxxxxx <pgssgp@xxxxxxxxx> wrote:
Hi,
> Hi All,
>
> I was reading a chapter from LDD3 and came know that
>
> file_operations->owner field is a pointer to the module that owns the
> file_operations structure. This field is used to prevent the module
> from being unloaded while its operation is in use. Almost all the
> time, it is simply initialized to THIS_MODULE, a macro define in
> <linux/module.h>
owner is of type struct module and THIS_MODULE is macro
which is defined as &__this_module. Look at the declaration
of struct module in linux/module.h

-pgssgp


[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