停止卸载Linux内核模块

Jeegar Patel:

我有一个基于平台驱动程序的Linux内核模块。
我已经在那里实现了probe()remove()方法。

struct platform_driver {
    int (*probe)(struct platform_device *);
    int (*remove)(struct platform_device *);
}

现在,当用户执行rmmod <myModule>随后remove()被调用的方法。在这里,我执行了一些条件检查,并得知用户不应该在rmmod这里执行在这里,我不想执行任何清理并使此rmmod失败。

我曾尝试返回-1-EBUSYin,remove()但仍在rmmod <myModule>卸载后仍然显示在lsmod的输出中。

有什么方法可以停止在remove()方法中卸载模块吗?

齐瓦列夫:

一个人不能取消(或停止)已经由rmmod(或其他方式)发起的模块卸载但是可以通过调用防止模块卸载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_getmodule_init函数调用是否成功,从函数调用module_exit肯定会失败。但在所有其他地方,此调用应会成功。

至于module_put调用,则无需从调用的同一函数执行该调用try_module_get您可能根本不会打电话module_put,但是如果可能的话,应该避免这种情况。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章