Abhinav
Abhinav

Reputation: 109

Regarding .owner field of struct file_operations

Referring to LDD-3 pg-50. It is written that

  struct module *owner

The first file_operations field is not an operation at all; it is a pointer to the module that “owns” the structure. This field is used to prevent the module from being unloaded while its operations are in use. Almost all the time, it is simply initialized to THIS_MODULE.

If we refer LDD-2 the explanation is

"This field isn’t a method like everything else in the file_operations structure. Instead, it is a pointer to the module that “owns” this structure; it is used by the kernel to maintain the module’s usage count."

Now my question is how this field is actually preventing the module from being unloaded ?

Thanks,

Upvotes: 3

Views: 1369

Answers (1)

Tsyvarev
Tsyvarev

Reputation: 66088

When a file which uses these operations is opened, before .open() file's operation is called, a function try_module_get() is called for the .owner module. This increments the module's usage counter, so the module cannot be unloaded with rmmod command.

When last reference to the file is dropped, and its .release() operation is completed, a function module_put is called for .owner module. This decrements module's usage counter, so the module can be unloaded again (unless its reference counter has been incremented for other reason).

Upvotes: 3

Related Questions