Linux 字符设备模块:为什么我们需要 *owner 和 MOD_INC_USE_COUNT?

Linux Character Device Modules: Why do we need both *owner and MOD_INC_USE_COUNT?

我目前正在研究字符设备模块(驱动程序),我对为什么同时存在使用计数和 *owner(在文件操作结构中)感到困惑。

如下link可以看出,文件操作结构为:

struct file_operations {
       struct module *owner;
       loff_t (*llseek) (struct file *, loff_t, int);
       ssize_t (*read) (struct file *, char *, size_t, loff_t *);
       ssize_t (*write) (struct file *, const char *, size_t, loff_t *);
       int (*readdir) (struct file *, void *, filldir_t);
       unsigned int (*poll) (struct file *, struct poll_table_struct *);
       int (*ioctl) (struct inode *, struct file *, unsigned int, unsigned long);
       int (*mmap) (struct file *, struct vm_area_struct *);
       int (*open) (struct inode *, struct file *);
       int (*flush) (struct file *);
       int (*release) (struct inode *, struct file *);
       int (*fsync) (struct file *, struct dentry *, int datasync);
       int (*fasync) (int, struct file *, int);
       int (*lock) (struct file *, int, struct file_lock *);
         ssize_t (*readv) (struct file *, const struct iovec *, unsigned long,
          loff_t *);
         ssize_t (*writev) (struct file *, const struct iovec *, unsigned long,
          loff_t *);
    };

http://www.tldp.org/LDP/lkmpg/2.4/html/c577.htm

正如我们在 class 中学到的,*owner 用于确保在有设备仍在使用模块时不会卸载该模块。 然而,在上面 link 的文章中,他们描述了这是通过使用 MOD_INC_USE_COUNT.

等宏来实现的

因此,我的问题是:为什么我们需要两者?宏是否使用指针来访问此计数器?

递增模块的使用计数器需要使用模块的结构。

MOD_INC_USE_COUNT间接使用了THIS_MODULE指针,对应当前编译的模块。换句话说,要使用此宏增加模块的使用计数器,您需要从 模块的代码 .

中调用它

另一方面,文件操作是从模块外部发出的(在虚拟文件系统层,VFS)。所以防止模块卸载应该在模块外部进行,否则竞争条件是不可避免的[考虑从模块外部调用.open函数和模块卸载同时进行时间]。这就是为什么需要 .owner 字段。


现代内核没有宏MOD_INC_USE_COUNT。相反,它定义了函数

void __module_get(struct module* mod);

直接接受模块参数。