免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2153 | 回复: 2
打印 上一主题 下一主题

[内核入门] 3.3.2内核模块中引用计数问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2012-11-25 22:02 |只看该作者 |倒序浏览
本帖最后由 wang_nets 于 2012-11-26 11:05 编辑

今天网上看到篇帖子讲到修改模块引用计数来强制删除内核模块,网上用的是2.6.28内核,其中struct module中struct module_ref是这样定义的:
  1. struct module_ref{
  2.     local_t count;
  3. }
复制代码
但是,我看了下手头上的3.1.10版本内核,这个结构体被重新定义了:
  1. struct module_ref {
  2.         unsigned long incs;
  3.         unsigned long decs;
  4. }
复制代码
看了下注释,没太看明白,请问下这个要修改引用计数要同时修改这两个值?谢了

论坛徽章:
0
2 [报告]
发表于 2012-11-28 12:58 |只看该作者
  1. 723unsigned long module_refcount(struct module *mod)
  2. 724{
  3. 725        unsigned long incs = 0, decs = 0;
  4. 726        int cpu;
  5. 727
  6. 728        for_each_possible_cpu(cpu)
  7. 729                decs += per_cpu_ptr(mod->refptr, cpu)->decs;
  8. 730        /*
  9. 731         * ensure the incs are added up after the decs.
  10. 732         * module_put ensures incs are visible before decs with smp_wmb.
  11. 733         *
  12. 734         * This 2-count scheme avoids the situation where the refcount
  13. 735         * for CPU0 is read, then CPU0 increments the module refcount,
  14. 736         * then CPU1 drops that refcount, then the refcount for CPU1 is
  15. 737         * read. We would record a decrement but not its corresponding
  16. 738         * increment so we would see a low count (disaster).
  17. 739         *
  18. 740         * Rare situation? But module_refcount can be preempted, and we
  19. 741         * might be tallying up 4096+ CPUs. So it is not impossible.
  20. 742         */
  21. 743        smp_rmb();
  22. 744        for_each_possible_cpu(cpu)
  23. 745                incs += per_cpu_ptr(mod->refptr, cpu)->incs;
  24. 746        return incs - decs;
  25. 747}
  26. 748EXPORT_SYMBOL(module_refcount);
复制代码
这里有注释啊,如果不采用这种分开dec 和inc + rmb wmb的方式,因为module_ref 是个 percpu变量,由于多cpu的读写顺序同步的问题,导致(module_refcount) 统计出来的引用计数是错的。可能人家本来还有引用的,统计出来的就说没有引用了。自己看上面注释说的那个情况。

论坛徽章:
0
3 [报告]
发表于 2012-12-04 15:45 |只看该作者
回复 2# hmsghnh
好的,多谢...明白了

   
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP