'Not able to remove a loadable kernel module

I'm writing a loadable kernel module and trying to test it. After inserting it I was trying to remove it using rmmod xxx command, but I get an error saying module xxx is in use and the module gets stuck and I can't remove it. Any idea how to remove the module without rebooting the entire machine ? (linux Kernel v. 3.5.0)

Note: rmmod -f prints Error: device or resource busy



Solution 1:[1]

This only happens to me when there is a bug in my driver which is causing the code in the module to panic or crash in some way. In my experience once this happens reboot is the only possible course.

As I said, the kernel usually panics so you should check out dmesg after inserting it or running you application to exercise it. I'm not certain but it might be possible that if the driver doesn't release a mutex this behavior will happen as well.

Solution 2:[2]

Maybe you forget to provide module_exit, so your module doesn't know how to exit.

Solution 3:[3]

Check your module_exit function is proper. You may need to compile your kernel to have "remove the module without rebooting the entire machine" with MODULE_FORCE_UNLOAD=yes.

Solution 4:[4]

I fix the same error by using the same GCC version as which have compiled the running kernel to compile my module, both 8.3.1; Please check on yours.

    [root@centos fishing]# dmesg | grep gcc
    [    0.000000] Linux version 4.18.0-80.7.2.el7.aarch64 ([email protected]) (gcc version 8.3.1 20190311 (Red Hat 8.3.1-3) (GCC)) #1 SMP Thu Sep 12 16:13:20 UTC 2019
    [root@centos fishing]# gcc -v
    gcc version 8.3.1 20190311 (Red Hat 8.3.1-3) (GCC)
    [root@centos fishing]#

Otherwise, I had errors:

    [root@centos fishing]# rmmod fishing
    rmmod: ERROR: could not remove 'fishing': Device or resource busy
    rmmod: ERROR: could not remove module fishing: Device or resource busy
    [root@centos fishing]#

the kernel module, fishing code is from http://books.gigatux.nl/mirror/kerneldevelopment/0672327201/ch16lev2sec1.html

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2
Solution 3 Nirmoy
Solution 4 kucer