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