Scoob Scoob - 5 months ago 25
Linux Question

Linux: re-loading kernel modules

I'm doing some development on a GPU kernel module. It gets compiled only on 'make modules' and not on 'make', so I can say it is a dynamically loaded module that loads on Linux boot. I would like to rapidly recompile and reinstall the module without installing a whole new kernel. Is it ok to simply 'make modules' and then replace the existing .ko file in /lib/modules/... and then reboot? If not (perhaps it is in use) can I boot into a different kernel, do the replacement, and reboot back in? Is it possible even to just insmod it?

Answer

It gets compiled only on 'make modules' and not on 'make', so I can say it is a dynamically loaded module that loads on Linux boot.

You should check where the module is placed. It can be stored only in /lib/modules/...kernel-version../, or in /lib/modules and inside initramfs (initrd). In second case you need regenerate initramfs image after updating ko in /lib/modules.

I would like to rapidly recompile and reinstall the module without installing a whole new kernel. Is it ok to simply 'make modules' and then replace the existing .ko file in /lib/modules/... and then reboot?

Yes, this is allowed to change .ko file, even when module is loaded. (Loading of the module with init_module is done from in-memory copy of .ko ELF file, so used version of module will still use old data).

On reboot you will have all modules and their files unused too.

Is it possible even to just insmod it?

It is not allowed to do insmod of module which is already loaded (compared by name - check EEXIST error in man finit_module). You may try to load it with different name, but older module has ownership of hardware and newer module will have no access to PCI/PCIe device.

So, you should unload older module by rmmod modulename before inserting newer version. Unloading is possible only for unused modules (man delete_module, check usage counter in lsmod output) - there should be no other modules depending on your, no processes should use it. After unloading you can do insmod modulename.ko or modprobe modulename.ko.

Reboot will unload older module and load newer module.

In case of GPU driver you should stop the X.org server and stop system console which uses graphics too (so, you can't switch to text console and reload gpu module interactively; it can be done remotely with ssh or with shell script with unbind/unload/load), check this question http://askubuntu.com/questions/418296/fastest-way-to-reload-graphics-driver-module

sudo /etc/init.d/lightdm stop
echo 0 > /sys/class/vtconsole/vtcon1/bind
Comments