Files
nvdriver/tools/run_stage8b.sh
morebrackets 4766555258 Address CodeRabbit follow-up findings on #249
- tools/dual_gpu_parity.py (4778337291, 4778337303): catch SubprocessError/OSError
  per cleanup step in teardown_nvidia_rs() so a TimeoutExpired on unbind still
  attempts driver_override clear and rmmod, and persist DIRTY_SENTINEL inside
  finally before any setup error propagates.
- tools/run_stage8b.sh (4778337321): accurately report that the device may
  remain bound when teardown fails.
- tests/offline/test_parity_and_binding_tools.py: test TimeoutExpired recovery.
2026-12-02 17:04:36 -08:00

111 lines
4.3 KiB
Bash
Executable File

#!/bin/sh
# Safe Stage 8B hardware runner for native-hello on Tesla V100 0000:b3:00.0 via nvidia_rs.
# Enforces AGENTS.md machine-safety gates:
# 1. Checks /proc/sys/kernel/tainted and no D-state nvidia/nv_open_q tasks before starting.
# 2. Runs pre-run stock CUDA baseline (timeout 20s).
# 3. Releases 0000:b3:00.0, loads nvidia_rs.ko (pci_target=0000:b3:00.0 runtime_engines=1),
# binds 0000:b3:00.0, and executes native-hello in a private mount namespace.
# 4. Unbinds 0000:b3:00.0 before rmmod nvidia_rs and captures dmesg.
# 5. Clean-unload safety gate: refuses to rebind stock nvidia if GR unload timed out
# (bringup note 4752001a 00000065), preventing stock nv_queue D-state hangs.
# 6. Restores 0000:b3:00.0 to stock nvidia and runs post-run stock CUDA baseline.
set -eu
BDF="${BDF:-0000:b3:00.0}"
OUT_DIR="${OUT_DIR:-target/stage8b}"
TAG="${1:-latest}"
SEED="${SEED:-317866785}"
mkdir -p "$OUT_DIR"
DMESG_OUT="$OUT_DIR/stage8b-${TAG}.dmesg"
STRACE_OUT="$OUT_DIR/stage8b-${TAG}.strace"
echo "=== HEALTH CHECK ==="
TAINT_BEFORE=$(cat /proc/sys/kernel/tainted)
echo "tainted=$TAINT_BEFORE"
if ps -eo stat,comm | grep -qE '^D.*(nv_open_q|nvidia)'; then
echo "ABORT: D-state nvidia/nv_open_q task detected; refusing to touch GPUs."
exit 1
fi
echo "NO_D_STATE"
echo "=== PRE-RUN STOCK BASELINE ==="
timeout 20 target/hardware-hello/native-hello target/hardware-hello/native_hello.sm70.cubin "$BDF" "$SEED"
echo "=== STAGE 8B ($TAG) ==="
DMESG_START_TS=$(sudo dmesg | awk 'END { if (match($0, /^\[[ ]*[0-9]+\.[0-9]+\]/)) { s = substr($0, RSTART, RLENGTH); gsub(/[^0-9.]/, "", s); print s } else print 0 }')
sudo python3 tools/gpu_binding.py release "$BDF" >/dev/null
INSMOD_DONE=0
teardown_nvidia_rs() {
rc=0
if [ -e "/sys/bus/pci/drivers/nvidia_rs/$BDF" ]; then
if ! echo "$BDF" | sudo tee /sys/bus/pci/drivers/nvidia_rs/unbind >/dev/null; then
rc=1
fi
fi
if [ -e "/sys/bus/pci/devices/$BDF/driver_override" ]; then
if ! echo "" | sudo tee "/sys/bus/pci/devices/$BDF/driver_override" >/dev/null; then
rc=1
fi
fi
if [ "$INSMOD_DONE" -eq 1 ] && [ -d /sys/module/nvidia_rs ]; then
if ! sudo rmmod nvidia_rs; then
rc=1
fi
fi
if [ -e "/sys/bus/pci/drivers/nvidia_rs/$BDF" ] || [ -d /sys/module/nvidia_rs ]; then
rc=1
fi
return "$rc"
}
cleanup_nvidia_rs() {
teardown_nvidia_rs || true
}
trap cleanup_nvidia_rs EXIT
sudo insmod kernel-open/nvidia_rs.ko "pci_target=$BDF" runtime_engines=1
INSMOD_DONE=1
if [ ! -e "/sys/bus/pci/devices/$BDF/driver" ]; then
echo nvidia_rs | sudo tee "/sys/bus/pci/devices/$BDF/driver_override" >/dev/null
echo "$BDF" | sudo tee /sys/bus/pci/drivers/nvidia_rs/bind >/dev/null
fi
set +e
sudo timeout -k 5 30 python3 tools/rmapi_namespace_launcher.py \
--gpu-node 0=/dev/nvidia-rs-rmapi-gpu0 \
--bdf "0=$BDF" \
--strace-out "$STRACE_OUT" \
-- target/hardware-hello/native-hello target/hardware-hello/native_hello.sm70.cubin "$BDF" "$SEED"
HELLO_RC=$?
echo "HELLO_RC=$HELLO_RC"
teardown_nvidia_rs
TEARDOWN_RC=$?
set -e
if [ "$TEARDOWN_RC" -ne 0 ]; then
echo "ERROR: nvidia_rs teardown failed; $BDF may remain bound. Check the device and module state."
exit 2
fi
trap - EXIT
sudo dmesg | awk -v ts="$DMESG_START_TS" '{ if (match($0, /^\[[ ]*[0-9]+\.[0-9]+\]/)) { s = substr($0, RSTART, RLENGTH); gsub(/[^0-9.]/, "", s); if ((s + 0) > (ts + 0)) print } }' | grep -E "nvidia_rs|$BDF" > "$DMESG_OUT" || true
echo "=== KEY DMESG LINES ($DMESG_OUT) ==="
grep -E '474f000|rt |504d0070|4752001a|504d0071|504d0072|41430040|41430041' "$DMESG_OUT" || true
if ! grep -q "4752001a 00000000" "$DMESG_OUT"; then
echo "UNSAFE TO REBIND STOCK NVIDIA: clean GR unload (4752001a 00000000) NOT observed!"
echo "Leaving $BDF unbound. Do NOT open /dev/nvidia* on this GPU until root-caused."
exit 2
fi
echo "=== REBIND STOCK NVIDIA & POST-RUN BASELINE ==="
sudo python3 tools/gpu_binding.py restore "$BDF" >/dev/null
timeout 20 target/hardware-hello/native-hello target/hardware-hello/native_hello.sm70.cubin "$BDF" "$SEED"
TAINT_AFTER=$(cat /proc/sys/kernel/tainted)
echo "tainted=$TAINT_AFTER"
if ps -eo stat,comm | grep -qE '^D.*(nv_open_q|nvidia)'; then
echo "WARNING: D-state nvidia/nv_open_q task detected after restore!"
exit 3
fi
echo "NO_D_STATE"
exit "$HELLO_RC"