停止卸载Linux内核模块

问题描述

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

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

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

我尝试在-1中返回-EBUSYremove(),但仍在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,但应尽可能避免这种情况。

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...