【问题标题】:Linux Character Device Modules: Why do we need both *owner and MOD_INC_USE_COUNT?Linux 字符设备模块:为什么我们需要 *owner 和 MOD_INC_USE_COUNT?
【发布时间】:2016-10-19 13:11:53
【问题描述】:

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

从以下链接可以看出,文件操作结构为:

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

正如我们在课堂上所教的,*owner 用于确保在有设备仍在使用时模块不会被卸载。 但是,在上面链接中的文章的后面,他们描述了这是通过使用诸如 MOD_INC_USE_COUNT 之类的宏来完成的。

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

【问题讨论】:

    标签: c linux linux-kernel driver kernel-module


    【解决方案1】:

    增加模块的使用计数器需要模块的结构正在使用中。

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

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


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

    void __module_get(struct module* mod);
    

    其中直接接受模块参数。

    【讨论】:

      猜你喜欢
      • 2011-01-20
      • 2013-08-04
      • 2012-01-03
      • 2011-02-27
      • 2019-06-09
      • 2014-12-16
      • 2020-09-05
      • 2012-12-03
      • 2016-06-25
      相关资源
      最近更新 更多