Linux: re-loading kernel modules - linux

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?

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 https://askubuntu.com/questions/418296/fastest-way-to-reload-graphics-driver-module
sudo /etc/init.d/lightdm stop
echo 0 > /sys/class/vtconsole/vtcon1/bind

Related

Linux: load module based on another module

Is it possible to dynamically load (via MOD_ALIAS() maybe?) a module that requires another module to first be loaded?
Background: I have a USB->I2C bridge on my system, and attached to the i2c end is a touchscreen. The kernel module that brings up the i2c automatically loads/unloads whenever the USB cable is connected/disconnected. I'm looking for a way to also load/unload the touchscreen driver on the same events.
You're talking about module dependencies, generated by depmod -A. The actual dependency information is stored in /lib/modules/version/modules.dep.
If /lib/modules/2.6.29/kernel/a.ko depends on b.ko (in the same directory) you could add the line:
/lib/modules/2.6.29/kernel/a.ko: /lib/modules/2.6.29/kernel/b.ko
To create the dependency.
What you want is modprobe.d
Add a <module>.conf file where <module> is the name of your kernel module that is dynamically loaded.
Define the install and remove options within the above conf file to run the relevant commands instead of the modprobe (in the required order).
If you want to automatically load the module bob after loading the module alice,
# /etc/modprobe.d/alice.conf
install alice /sbin/modprobe --ignore-install alice; /sbin/modprobe bob;"
The --ignore-install, stops the modprobe from running the same install command again.
Similarly define the remove section within the same conf file.
For more details, checkout the man page of modprobe.d.

How do I add a kernel module not compiled in my current kernel in linux

I found kernel modules "ceph" and "rbd" is configured as "Y" or "M" in my centos 7 by looking /boot/config-xxxx file.
Does any guys know what's the best way to add these modules back ?
Do I need to manually download the kernel source code and configure these modules to "Y/M"?
Or do I just need to build there two kernel modules and add them to /lib64/modules ?
Firstly,
I found kernel modules "ceph" and "rbd" is configured as "Y" or "M"
A kernel module is configured as either "Y" or "M". If you grep for the module under /boot/config it would be marked either of those.
If marked "Y" then the module is already built into the kernel as a STATIC MODULE. You need not rebuild it once again.
If marked "M" then the module is still built, but as DYNAMIC MODULE. You can find the module by using "$ lsmod" command or can be found in /lib/modules
So, if you don't find your module listed under /boot/config*** only then you will have to consider building that module
Next, if you don't find the modules listed then you will have to download the kernel source corresponding to the version on your PC and then compile only those modules and install them

Error inserting scsi_wait_scan - Invalid module format

The system is CentOS 6.3.
I've compile a new kernel and the resulting rpm installed on a target machine.
When booting from the kernel I've receive the error in a title of the question.
I've extracted corresponding initramfs and compared output of:
modprobe --dump-modversions /path/to/scsi_wait_scan.ko
with entries in corresponding /boot/symvers-*. All symbols checksums fit, including of module_layout.
Is there a way to extract symvers from kernel itself?
I've found the problem.
Short answer
The problem was that I installed kernel rpm (B) over already installed kernel rpm (A),
without removing it first.
Detaild answer
scsi_mod.ko was owned only by (A). While installing (B), scsi_mod.ko was in /lib/modules/.
When intramfs was created in (B)'s postinstall script. depmod decided that scsi_wait_scan.ko depends on scsi_mod.ko, while both build against different configurations.
Later when booting the machine, kernel started run initramfs. This in turn modprob'ed scsi_wait_scan.ko. modprobe tried loaded as a consiquence scsi_mod.ko, which is not appropriate to the current kernel, thus resulting to error I saw.

Quickest way to modify/reload existing Linux driver

I am trying to understand an existing Linux Wi-Fi driver for a USB Wi-Fi adapter. While I can read the C code, I would also like to be able to insert debug/print statements at certain critical spots in the driver to see how it behaves when executed. On a Linux system, after modifying the driver code, how does one go about loading it into the kernel in such a way that it replaces the old driver? Is there a way to "hotplug substitute" it straight over the old driver, or is it more complicated than that?
I intend on doing this inside of an expendible virtual machine, so I am not concerned about messing up the original driver, for what it counts.
If the driver is compiled as a module, all you need to do is to add your debug print outs, compile the module, rmmod the original module, insmod your new module and initiate the WLAN connection as usual.
If you want to test and edit on the fly :
lsmod to find the module name
rmmod it
edit source
Make sur you got a Makefile : obj-m := module_name.o
recompile (on Ubuntu) : make -C /usr/src/linux-headers-'uname -r' M='pwd' modules
insmod module_name.ko
If it's a device module, you might want to rm any devices in /dev, then do a mknod to remake them (see mknod man) and finally chmod to correct rights.

How to get complete dependency list of kernel modules at runtime

Is there a way to list down from command line the complete dependency list of kernel modules loaded in the kernel at runtime?
Let me clarify my question a little bit.
What I am trying to do:
I get a list of all loaded modules using lsmod
Then remove/unload currently loaded kernel modules by running a loop. I am using modprobe -r. This step fails (obviously) as some modules are in use.
(I can switch to using rmmod -r, but don't want to as it is unsafe and can crash the system.)
Then I want to load the modules one by one again.
It is step 2 that is failing, as I cannot get all the module dependencies before using modprobe -r.
Any ideas, suggestions or comments ?
However lsmod o/p is sometimes incomplete. It also does not always indicate all the modules dependent on a given module.
What you see in lsmod in the "Used by" column are merely the static symbol dependencies that you can also look at using modinfo.
If however a piece of kernel code takes a reference on a module using (try_)module_get, the caller will not be recorded. References do not have an owner (moduleA could pass the pointer to moduleB, which then module_puts it..), there is nothing to record for the Used by column.
To get a list of module dependencies as would be used by modprobe (i.e. this should normally be the full list, but see the answer by user502515), use
$ modprobe --show-depends <module>
Note that this command shows more information than modinfo's depends: line, as it lists dependencies recursively (i.e. dependencies of dependencies).
It also takes into account alias commands in modprobe configuration files.
Tested using:
$ modprobe -V
kmod version 14
man lsmod: lsmod is a trivial program which nicely formats the contents of the /proc/modules, showing what kernel modules are currently loaded.
Edited:
see also: depmod -n

Resources