Quickly reload kernel modules

A long running annoying issues with my Thinkpad Yoga 12 is that the touchpad intermittently stops working. This is reproducible to some degree through putting the laptop in a certain angle, but also happens seemingly randomly. When the touchpad fails I see various error messages in journalctl -k prompting me about rmi4_physical failing in some way. I have followed up on these, but none of the solutions I have tried so far has resolved the issue.

The workaround I have for the moment is to simply reload the mouse kernel module, which works most of the time. This requires root privileges so when the touchpad fails often, it becomes annoying to open a terminal, type the command, and then password. Instead, I have granted root access without password to this particular command and bound a hotkey to the execution of the command.

The command I need to run to remove the module from the kernel and then add it back in (effectively reloading the module) is:

sudo modprobe -r psmouse && sudo modprobe psmouse

To be able to run this without a password prompt, create a new file in /etc/sudoers.d and give it a prefix number higher than any previous files in this folder (the prefix number is important since the files are loaded in order and if more than one rule match the same user, the most recently loaded will take precedence). To create and edit this file, use visudo to prevent saving incorrectly formatted files, which can lead to issues with the sudo command.

sudo visudo -f /etc/sudoers.d/20-no-sudo-touchpad

In this file, add the following

username ALL=(root) NOPASSWD: /usr/sbin/modprobe psmouse, /usr/sbin/modprobe -r psmouse

Reboot or log out, and you should be able to to run these two commands as sudo without requiring your password.

To bind this to a hotkey, first create a script with the commands

#!/usr/bin/env bash
sudo modprobe -r psmouse && sudo modprobe psmouse

The procedure for binding a hotkey to this script will depend on the desktop environment. For i3, add the following to .config/i3/config

bindsym $mod+i exec --no-startup-id /path/to/script

I have $mod specified as the superkey in my i3-config, so every time I press super + i, I reload the psmouse kernel module.

Resources

PhD candidate in stem cell bioengineering

Developmental biology researcher and data science educator