开发者

How to remove a failed kernel module

This situation always bother me:

I wrote a kernel module, and sometimes it has a bug(dereference a NULL pointer). After I insmod hello.ko, it shows some kernel errors. Then I change the code, and try to remove the module and install it again. The question is: I don't know how to开发者_Python百科 remove the kernel module.

$ rmmod hello

ERROR: module hello in use

$ rmmod -f hello

ERROR: removing hello: device or resource busy

I always reboot the machine in order to remove the module, which takes too long. Does anyone have a better solution for this? Thanks for any inputs.


Use a virtual machine.

Once you make a NULL dereference or other such mistake, you've put the kernel into an unknown state. Even if you did manage to remove the module (which is unlikely to be possible; a kernel OOPS kills the calling thread, so it'll never have a chance to reduce the reference count - the module will never be removable) there may still be corruption left behind, and your new, 'fixed' module is just as likely to be in trouble.

Much better to just use a fast-to-reboot virtual machine - perhaps with a snapshot, to make restoration even faster.


As bdonlan pointed out, you would be better off with a virtual machine.

However, if you really want to do it your way, you have to:

  1. check that your kernel config has CONFIG_MODULE_FORCE_UNLOAD enabled
  2. write another module that sets the refcount of the broken module to zero. Check out the delete_module system call in kernel/module.c


The reference count or value under the Used by column (see lsmod) for my case was -1. This value can also be found at /sys/module/<kernel_module>/refcnt.

Here's the answer I found that worked for me here: https://askubuntu.com/a/521231

echo -e "blacklist kernel_module\n" | sudo tee -a /etc/modprobe.d/blacklist.conf

Then you have to reboot your machine. Re-compile a clean stable version of your module. Then enter the following command to reload & overwrite the failed module.

insmod kernel_module.ko

Fianlly,

rmmod kernel_module

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜