Automate NVIDIA uvm Reload on Wake: A Simple Hack for Seamless CUDA Operation

If you’re running Debian family system and frequently putting your system to sleep, you might have encountered an issue where CUDA becomes unavailable after the computer wakes up. This happens because the NVIDIA uvm (Unified Virtual Memory) module doesn’t reload automatically on resume. While rebooting the computer resolves the issue, it’s not an ideal solution for day-to-day use.

In this article, I’ll walk you through an automated solution to ensure that your NVIDIA uvm module is reloaded every time your system wakes up—without any manual intervention.

The Problem

On many Debian systems, especially those using systemd, the NVIDIA drivers include a sleep script located in the /lib/systemd/system-sleep/ directory called nvidia. This script manages certain NVIDIA-related tasks when your computer suspends and resumes. However, it does not handle the reloading of the nvidia_uvm module, which is critical for CUDA functionality.

The workaround many users apply is to manually run:

sudo modprobe -r nvidia_uvm && sudo modprobe nvidia_uvm

after waking up the system. But why do it manually every time when we can automate the process?

The Automated Solution

Systemd provides a convenient mechanism to execute scripts during sleep and wake cycles. Scripts placed in /lib/systemd/system-sleep/ are executed in lexical (alphabetical) order. By naming our script appropriately, we can ensure that it runs after the existing NVIDIA sleep script.

Step 1: Create Your Script

Create a new file in the /lib/systemd/system-sleep/ directory with a name that sorts after the NVIDIA script. For example, naming it nvidia-uvm.sh ensures it runs later in the sequence:

#!/bin/sh
# /lib/systemd/system-sleep/nvidia-uvm.sh
#
# This script automatically reloads the nvidia_uvm module upon system wake,
# ensuring CUDA remains functional without manual intervention.

case "$1" in
    post)
        /sbin/modprobe -r nvidia_uvm && /sbin/modprobe nvidia_uvm
        ;;
esac

Step 2: Set Execute Permissions

Once the script is in place, make sure it is executable:

sudo chmod +x /lib/systemd/system-sleep/nvidia-uvm.sh

How It Works

  • Systemd Sleep Hooks: When your system goes to sleep or wakes up, systemd runs all the scripts in /lib/systemd/system-sleep/ in alphabetical order.
  • Ensuring Order: By naming your script with a prefix or postfix, you guarantee that it will be executed after the pre-existing NVIDIA sleep script (usually named nvidia).
  • Reloading Module: The script checks the action parameter (post indicates wake-up) and then executes the commands to remove and reload the nvidia_uvm module.

Conclusion

This solution ensures that your NVIDIA uvm module is properly reloaded every time your Debian system wakes up, maintaining CUDA functionality without any manual steps. By leveraging systemd’s sleep hook mechanism and careful naming conventions, you can automate this process reliably.

Implementing this simple hack can save you time and keep your CUDA-dependent applications running smoothly—every time you wake up your computer.

Leave a Reply

Your email address will not be published. Required fields are marked *