Files
nvdriver/tools/dev_reload.sh

186 lines
7.9 KiB
Bash
Executable File

#!/bin/sh
# Replaces the loaded nvidia_rs with a fresh build, without a reboot.
#
# NV_RS_DEV_RELOAD_BDF=0000:01:00.0 sh tools/dev_reload.sh [path/to/nvidia_rs.ko] [extra module parameters...]
#
# NV_RS_DEV_RELOAD_BDF is required and names the only GPU this script may unbind; it also becomes
# pci_target. Nothing else is bound or reset. Extra parameters are passed to insmod as given
# (for example dyndbg=+p, which needs CONFIG_DYNAMIC_DEBUG).
#
# Needs development mode (tools/dev_mode.sh on): any process holding a node pins the module.
# The runtime thread keeps a module reference while a GPU is bound, so the script first
# unbinds the GPU from nvidia_rs (the driver's own remove/teardown path) and then rmmods.
# Only pci_target and runtime_engines are defaulted (modprobe.d is not read); pass overrides as
# arguments, with or without a leading module path.
# A wedged GSP-RM makes unbind/rmmod wait forever; the script then reports it and leaves
# the stuck command behind.
set -u
cd "$(dirname "$0")/.." || exit 2
# Opt-in: the reload unbinds a GPU from nvidia_rs, so it must be told which single device it may touch.
# On a multi-GPU or data-center host this script never runs unless that is set deliberately.
NV_RS_DEV_RELOAD_BDF=${NV_RS_DEV_RELOAD_BDF:-}
case "$NV_RS_DEV_RELOAD_BDF" in
[0-9a-f][0-9a-f][0-9a-f][0-9a-f]:[0-9a-f][0-9a-f]:[0-9a-f][0-9a-f].[0-7]) ;;
*) echo "refusing: set NV_RS_DEV_RELOAD_BDF=<lowercase domain:bus:dev.fn> (the one GPU this reload may unbind)" >&2; exit 2 ;;
esac
[ -d "/sys/bus/pci/devices/$NV_RS_DEV_RELOAD_BDF" ] || { echo "refusing: PCI device $NV_RS_DEV_RELOAD_BDF does not exist" >&2; exit 2; }
for d in /sys/bus/pci/drivers/nvidia_rs/*:*:*.*; do
[ -e "$d" ] || continue
[ "${d##*/}" = "$NV_RS_DEV_RELOAD_BDF" ] || { echo "refusing: nvidia_rs is also bound to ${d##*/}; only $NV_RS_DEV_RELOAD_BDF is allowed"; exit 2; }
done
KO=kernel-open/nvidia_rs.ko
# A leading name=value is a module parameter, not the module path.
case "${1:-}" in ""|*=*) ;; *) KO=$1; shift ;; esac
KO_DIR=$(dirname "$KO")
MODESET_KO="$KO_DIR/nvidia_rs_modeset.ko"
DRM_KO="$KO_DIR/nvidia_rs_drm.ko"
set -- "pci_target=$NV_RS_DEV_RELOAD_BDF" runtime_engines=1 "$@"
STAMP=$(date '+%Y-%m-%d %H:%M:%S')
[ -f "$KO" ] || { echo "missing $KO (make CONFIG_DEBUG_INFO_BTF_MODULES= modules)"; exit 2; }
RELOAD_MODESET=0
RELOAD_DRM=0
MODESET_ARGS=""
DRM_ARGS=""
read_bool_param() {
if [ -r "$1" ]; then
case "$(cat "$1" 2>/dev/null)" in
Y|y|1) echo 1 ;;
N|n|0) echo 0 ;;
esac
fi
}
if [ -e /sys/module/nvidia_rs_modeset ]; then
[ -f "$MODESET_KO" ] || { echo "missing $MODESET_KO (needed to reload loaded nvidia_rs_modeset)"; exit 2; }
RELOAD_MODESET=1
v=$(read_bool_param /sys/module/nvidia_rs_modeset/parameters/virtual_display)
[ -n "$v" ] && MODESET_ARGS="$MODESET_ARGS virtual_display=$v"
if [ -r /sys/module/nvidia_rs_modeset/parameters/max_alloc_mb ]; then
v=$(cat /sys/module/nvidia_rs_modeset/parameters/max_alloc_mb 2>/dev/null)
case "$v" in ''|*[!0-9]*) ;; *) MODESET_ARGS="$MODESET_ARGS max_alloc_mb=$v" ;; esac
fi
fi
if [ -e /sys/module/nvidia_rs_drm ]; then
[ -f "$DRM_KO" ] || { echo "missing $DRM_KO (needed to reload loaded nvidia_rs_drm)"; exit 2; }
RELOAD_DRM=1
for p in modeset fbdev compat_drm_name; do
v=$(read_bool_param "/sys/module/nvidia_rs_drm/parameters/$p")
[ -n "$v" ] && DRM_ARGS="$DRM_ARGS $p=$v"
done
fi
sh tools/dev_health.sh pre || { echo "health check failed: no reload attempted"; exit 1; }
holders() {
for p in /proc/[0-9]*; do
if sudo ls -l "$p/fd" 2>/dev/null | grep -Eq 'nvidia-rs|/dev/nvidia(ctl|[0-9]|-uvm|-modeset)'; then
echo " ${p#/proc/} $(cat "$p/comm" 2>/dev/null)"
fi
done
}
rmmod_wait() {
mod="$1"
sudo rmmod "$mod" &
RMMOD=$!
i=0
while kill -0 "$RMMOD" 2>/dev/null && [ "$i" -lt 60 ]; do sleep 1; i=$((i + 1)); done
if kill -0 "$RMMOD" 2>/dev/null; then
echo "rmmod $mod has not returned after 60 s."
sudo journalctl -k --no-pager --since "$STAMP" | tail -20
return 124
fi
wait "$RMMOD" || {
echo "rmmod $mod failed"
sudo journalctl -k --no-pager --since "$STAMP" | tail -20
return 1
}
[ -e "/sys/module/$mod" ] && { echo "$mod is still loaded"; return 1; }
return 0
}
REBIND=0
if [ -e /sys/module/nvidia_rs ]; then
PERSISTED=0
systemctl is-active --quiet nvidia-persistenced 2>/dev/null && PERSISTED=1
sudo systemctl stop nvidia-persistenced 2>/dev/null
H=$(holders)
if [ -n "$H" ]; then
echo "these processes hold nvidia_rs nodes; close them first:"
echo "$H"
# Nothing was torn down yet: put the service back.
[ "$PERSISTED" -eq 0 ] || sudo systemctl start nvidia-persistenced 2>/dev/null
exit 1
fi
# The standard-path aliases keep the old inodes across a reload; drop them now and rebind after insmod.
mountpoint -q /dev/nvidiactl 2>/dev/null && REBIND=1
if ! sudo bash tools/bind_dev_nodes.sh --unbind; then
echo "failed to unbind /dev/nvidia* node aliases; aborting reload" >&2
exit 1
fi
if [ "$RELOAD_DRM" -ne 0 ]; then
if ! rmmod_wait nvidia_rs_drm; then
[ "$REBIND" -eq 0 ] || sudo bash tools/bind_dev_nodes.sh --bind
[ "$PERSISTED" -eq 0 ] || sudo systemctl start nvidia-persistenced 2>/dev/null
exit 1
fi
fi
if [ "$RELOAD_MODESET" -ne 0 ]; then
rmmod_rc=0
rmmod_wait nvidia_rs_modeset || rmmod_rc=$?
if [ "$rmmod_rc" -ne 0 ]; then
if [ "$rmmod_rc" -ne 124 ] && [ "$RELOAD_DRM" -ne 0 ] && [ -e /sys/module/nvidia_rs_modeset ]; then
# shellcheck disable=SC2086
sudo insmod "$DRM_KO" $DRM_ARGS || echo "failed to restore $DRM_KO after nvidia_rs_modeset rmmod failure" >&2
fi
[ "$REBIND" -eq 0 ] || sudo bash tools/bind_dev_nodes.sh --bind
[ "$PERSISTED" -eq 0 ] || sudo systemctl start nvidia-persistenced 2>/dev/null
exit 1
fi
fi
# Every PCI domain, not only 0000.
for d in /sys/bus/pci/drivers/nvidia_rs/*:*:*.*; do
[ -e "$d" ] || continue
echo "${d##*/}" | sudo tee /sys/bus/pci/drivers/nvidia_rs/unbind >/dev/null &
UNBIND=$!
i=0
while kill -0 "$UNBIND" 2>/dev/null && [ "$i" -lt 60 ]; do sleep 1; i=$((i + 1)); done
if kill -0 "$UNBIND" 2>/dev/null; then
echo "unbind of ${d##*/} has not returned after 60 s; not attempting rmmod or insmod."
sudo journalctl -k --no-pager --since "$STAMP" | tail -20
exit 1
fi
if ! wait "$UNBIND" || [ -e "$d" ]; then
echo "unbind of ${d##*/} failed; not attempting rmmod or insmod." >&2
sudo journalctl -k --no-pager --since "$STAMP" | tail -20
exit 1
fi
done
rmmod_wait nvidia_rs || exit 1
fi
sudo insmod "$KO" "$@" || { echo "insmod failed"; sudo journalctl -k --no-pager --since "$STAMP" | tail -30; exit 1; }
if [ "$RELOAD_MODESET" -ne 0 ]; then
# shellcheck disable=SC2086
sudo insmod "$MODESET_KO" $MODESET_ARGS || { echo "insmod $MODESET_KO failed"; sudo journalctl -k --no-pager --since "$STAMP" | tail -30; exit 1; }
fi
if [ "$RELOAD_DRM" -ne 0 ]; then
# shellcheck disable=SC2086
sudo insmod "$DRM_KO" $DRM_ARGS || { echo "insmod $DRM_KO failed"; sudo journalctl -k --no-pager --since "$STAMP" | tail -30; exit 1; }
fi
sudo udevadm settle
if [ "$REBIND" -ne 0 ]; then
sudo bash tools/bind_dev_nodes.sh --bind || { echo "failed to rebind /dev/nvidia* node aliases" >&2; exit 1; }
fi
sudo journalctl -k --no-pager --since "$STAMP" -g 'nvidia_rs' | grep -v 'TRACE' | tail -40
if [ -e /dev/nvidia-modeset ]; then
ls -l /dev/nvidiactl /dev/nvidia0 /dev/nvidia-uvm /dev/nvidia-modeset 2>&1
else
ls -l /dev/nvidiactl /dev/nvidia0 /dev/nvidia-uvm 2>&1
fi
sh tools/dev_health.sh post