模块API之module_put/__module_get
2017-11-01 14:55
369 查看
module_put和__module_get 是一对函数,用于减少或者增加模块的引用计数. 具体使用的例子如下: void nfs4_schedule_state_manager(struct nfs_client *clp) { struct task_struct *task; char buf[INET6_ADDRSTRLEN + sizeof("-manager") + 1]; if (test_and_set_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) != 0) return; __module_get(THIS_MODULE); atomic_inc(&clp->cl_count); task = kthread_run(nfs4_run_state_manager, clp, "%s", buf); if (IS_ERR(task)) { printk(KERN_ERR "%s: kthread_run: %ld\n", __func__, PTR_ERR(task)); nfs4_clear_state_manager_bit(clp); nfs_put_client(clp); module_put(THIS_MODULE); } } 其源码如下: void module_put(struct module *module) { int ret; if (module) { preempt_disable(); ret = atomic_dec_if_positive(&module->refcnt); WARN_ON(ret < 0); /* Failed to put refcount */ trace_module_put(module, _RET_IP_); preempt_enable(); } } EXPORT_SYMBOL(module_put); void __module_get(struct module *module) { if (module) { preempt_disable(); atomic_inc(&module->refcnt); trace_module_get(module, _RET_IP_); preempt_enable(); } } EXPORT_SYMBOL(__module_get); 可见在使用module_put和__module_get的时候,要通过preempt_disable()/preempt_enable() 来禁止/使能内核的抢占 module->refcnt是以原子的方式增加或者减小的,因此不用加锁 在module_put 中通过atomic_dec_if_positive 来使module->refcnt 减一时,要先判断减一后是否小于零,也就是当前的module->refcnt == 0.这种情况下就 不能在减一了,从这里也可以知道module->refcnt的最小值是零. static inline int atomic_dec_if_positive(atomic_t *v) { int c, old, dec; c = atomic_read(v); for (;;) { dec = c - 1; if (unlikely(dec < 0)) break; old = atomic_cmpxchg((v), c, dec); if (likely(old == c)) break; c = old; } return dec; }
相关文章推荐
- 模块API之module_get_kallsym
- 模块API之try_module_get
- 模块API之__module_text_address
- GetModuleHandle(NULL)获取当前DLL模块基址?
- linux-2.6内核模块引用计数的实现(try_module_get和module_put)
- 2014-04-30工作日志:提取文件的路径及使用createprocess时,要用getmodulename获取要执行的模块的路径及调用createprocess时要记得关闭新建的进程
- 模块API之__module_address
- 模块API之module_is_live
- DLL--AFX_MANAGE_STATE(AfxGetStaticModuleState())-模块状态切换保护
- linux-2.6内核模块引用计数的实现(try_module_get和module_put)
- 模块API之ref_module
- intel dpdk api interrupt module 中断模块介绍
- 1.GetModuleHandle 获取一个特定的应用程序或动态链接库的模块句柄
- intel dpdk api memory manage module (内存管理模块)
- 2011-1-2----------mage:getModuleDir函数的分析!!--------得到模块的地址
- 解决nginx上传模块nginx_upload_module传递GET参数
- 模块API之find_module
- c#调用API(GetModuleFileName)获取程序路径返回乱码有关问题
- SIP模块版本错误问题:the sip module implements API v??? but XXX module requires API v???
- 模块API之module_address_lookup