Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linux字符设备模块:为什么我们需要*owner和MOD_INC_USE_COUNT?_C_Linux_Linux Kernel_Driver_Kernel Module - Fatal编程技术网

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

Linux字符设备模块:为什么我们需要*owner和MOD_INC_USE_COUNT?,c,linux,linux-kernel,driver,kernel-module,C,Linux,Linux Kernel,Driver,Kernel Module,我目前正在研究字符设备模块(驱动程序),我对为什么文件操作结构中既有使用计数又有*所有者感到困惑 如以下链接所示,文件操作结构为: struct file_operations { struct module *owner; loff_t (*llseek) (struct file *, loff_t, int); ssize_t (*read) (struct file *, char *, size_t, loff_t *); ssi

我目前正在研究字符设备模块(驱动程序),我对为什么文件操作结构中既有使用计数又有*所有者感到困惑

如以下链接所示,文件操作结构为:

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 *);
    };

正如我们在课堂上学到的,*所有者用于确保如果有设备仍在使用模块,则不会卸载模块。 然而,在上面链接的文章后面,他们描述了这是通过使用MOD_INC_use_COUNT等宏来实现的


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

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

MOD_INC_USE_COUNT
间接使用
此模块
指针,它对应于当前编译的模块。换句话说,对于使用此宏的增量模块的用法计数器,您需要从模块的代码中调用它

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


现代内核没有宏
MOD\u INC\u USE\u COUNT
。相反,它定义了函数

void __module_get(struct module* mod);
它直接接受模块参数