0

I wrote a simple device driver that makes device file using class_create() and device_create() function. But then in the exit function of my driver, I destroyed the class first and then the driver, i.e. I called class_destroy() first and then device_destroy(). Due to this in kernel logs I saw some error messages and didn't see the output message that I had put in my exit function.

The driver was also not removed stating that it is still in use.

I want to know:

  1. I think I made a mistake by freeing the class first and then removing the device, so error messages were appropriate. But now I cannot remove my driver using rmmod. Is there any way to remove the driver without rebooting the system?

  2. I used lsmod to check which service is using my driver. But instead of showing "used by" count as 1, it is showing -1. What can be the reason for this behaviour?

1

man rmmod:

NAME
       rmmod - Simple program to remove a module from the
       Linux Kernel

SYNOPSIS
       rmmod [-f] [-s] [-v] [modulename]

DESCRIPTION
       rmmod is a trivial program to remove a module (when
       module unloading support is provided) from the kernel.
       Most users will want to use modprobe(8) with the -r
       option instead.

OPTIONS
       -v, --verbose
           Print messages about what the program is doing.
           Usually rmmod prints messages only if something
           goes wrong.

       -f, --force
           This option can be extremely dangerous: it has no
           effect unless CONFIG_MODULE_FORCE_UNLOAD was set
           when the kernel was compiled. With this option,
           you can remove modules which are being used, or
           which are not designed to be removed, or have been
           marked as unsafe (see lsmod(8)).

To be able to use -f, note that your kernel need the CONFIG_MODULE_FORCE_UNLOAD option to be set. To ensure it is set, you may use:

/boot$ grep CONFIG_MODULE_FORCE_UNLOAD config-`uname -r`
CONFIG_MODULE_FORCE_UNLOAD=y

(If your distro installed the config under /boot...)

Regarding the usage count at -1, see this answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.