问题描述
我有一个基于平台驱动程序的Linux内核模块。
我在那里实现了probe()
和remove()
方法。
struct platform_driver {
int (*probe)(struct platform_device *);
int (*remove)(struct platform_device *);
}
现在,当用户执行rmmod <myModule>
时,
remove()
方法正在被调用。在这里,我执行了一些条件检查,然后知道用户不应在此处执行rmmod
。在这里,我不想执行任何清理操作并使此rmmod失败。
我尝试在-1
中返回-EBUSY
或remove()
,但仍在rmmod <myModule>
之后将其卸载,并且不会显示在lsmod
的输出中。 / p>
有什么方法可以停止用remove()
方法卸载模块?
解决方法
已经无法通过rmmod
(或通过其他方式)启动的模块卸载(取消(或 stop ))。但是可以通过在其上调用try_module_get
来防止模块卸载:
// Before your module enters into the state,when its unloading is not desirable.
// Prevent unloading of the module
if(!try_module_get(THIS_MODULE)) {
<failed to prevent module unloading>
}
<....> // This code will be protected from the module's unloading
// Allow the module to be unloaded again
module_put(THIS_MODULE);
对try_module_get
的调用(成功)有效时,rmmod
立即拒绝模块卸载,而不执行任何模块的代码。
我不确定从try_module_get
函数调用module_init
是否会成功,而从module_exit
函数调用它肯定会失败。但在所有其他地方,此调用应会成功。
对于module_put
调用,不需要从调用try_module_get
的同一函数中执行。您根本无法致电module_put
,但应尽可能避免这种情况。