omega-gpu-driver
aien-dev/omega-gpu-driver
Brought by drakestapleton
omega-gpu-driver / window / d2-pre.sh
| 1 | #!/bin/sh |
| 2 | # window/d2-pre.sh -- W0 and W1 of the OwnDriver window. Status: W0 EXERCISED read-only on the host; W1 NOT_RUN. |
| 3 | # no argument: W0, read-only checks, safe while the 35B runs. Changes nothing except its own log. |
| 4 | # --go W0: identity only, previously running service restored, two reboots at most. |
| 5 | # --go [D2|D3]: W1, needs $APPROVAL. Stops the 35B on 18091, writes the one-boot nvidia hold-back file and the |
| 6 | # boot unit, sets state "run D2" (default) or "run D3", reboots. The unit then runs W2.. by itself |
| 7 | # (window/d2-run.sh), and after the second reboot R (window/d2-restore.sh). |
| 8 | # Abort before the reboot: the window files are removed and window/d2-restore.sh restarts the 35B. |
| 9 | set -eu |
| 10 | R=$(CDPATH= cd -- "$(dirname "$0")/.." && pwd) |
| 11 | . "$R/window/common.sh" |
| 12 | LOG=$LOGDIR/d2-$(date -u +%Y%m%dT%H%M%SZ)-pre.log |
| 13 | GO=${1:-} |
| 14 | STAGE=${2:-D2} |
| 15 | case $STAGE in W0 | D2 | D3) ;; *) die "stage must be W0, D2 or D3" ;; esac |
| 16 | if [ "$STAGE" = W0 ]; then |
| 17 | [ ! -e "$BUILD/ogd" ] && [ ! -L "$BUILD/ogd" ] || die "identity-only checkout contains build/ogd" |
| 18 | [ "${RESTORE_PATH:-reboot}" = reboot ] || die "identity-only window requires reboot restore" |
| 19 | fi |
| 20 | |
| 21 | log "W0 checks (repo $(git -C "$R" rev-parse HEAD 2>/dev/null || echo no-git), dirty files: $(git -C "$R" status --porcelain 2>/dev/null | wc -l))" |
| 22 | echo "$FW_SHA $FW" | sha256sum -c --quiet || die "firmware sha256 mismatch" |
| 23 | log "firmware $FW sha256 ok" |
| 24 | for f in $HASHED; do |
| 25 | if [ -e "$R/$f" ]; then log "sha256 $(sha256sum "$R/$f" | cut -c1-64) $f"; else log "missing $f"; fi |
| 26 | done |
| 27 | omc_pin=$(sed -n 's/^OMC_SHA256=//p' "$R/tools/omc.pin") |
| 28 | [ "$(sha256sum "$R/build/omc" | cut -c1-64)" = "$omc_pin" ] || die "build/omc does not match tools/omc.pin" |
| 29 | [ -x "$BUILD/w0" ] || die "build/w0 missing: run sh build.sh" |
| 30 | if [ -e "$BUILD/fw/fw.manifest" ]; then |
| 31 | # fwprep writes fw.manifest last, with one 'out NAME bytes=N sha256=H' line per output file. |
| 32 | fwsha=$(sha256sum "$FW" | cut -c1-64) |
| 33 | grep -q "^fw_file bytes=[0-9]* sha256=$fwsha\$" "$BUILD/fw/fw.manifest" || |
| 34 | die "build/fw/fw.manifest was made from a different firmware file: rerun sh build.sh" |
| 35 | sed -n 's/^out \([a-z.]*\) bytes=[0-9]* sha256=\([0-9a-f]\{64\}\)$/\2 \1/p' "$BUILD/fw/fw.manifest" > "$LOGDIR/fw.check" |
| 36 | [ "$(wc -l < "$LOGDIR/fw.check")" = 5 ] && grep -qx end "$BUILD/fw/fw.manifest" || |
| 37 | die "build/fw/fw.manifest is incomplete: rerun sh build.sh" |
| 38 | (cd "$BUILD/fw" && sha256sum -c --quiet "$LOGDIR/fw.check") || |
| 39 | die "build/fw files do not match build/fw/fw.manifest: rerun sh build.sh" |
| 40 | rm -f "$LOGDIR/fw.check" |
| 41 | log "build/fw matches fw.manifest" |
| 42 | fi |
| 43 | if [ -x "$BUILD/ogd" ]; then |
| 44 | log "ogd modes: $("$BUILD/ogd" modes | tr '\n' ' ')" |
| 45 | fi |
| 46 | [ "$(cat "$DEV/vendor"):$(cat "$DEV/device")" = 0x10de:0x2e12 ] || die "$BDF is not 10de:2e12" |
| 47 | [ "$(basename "$(readlink "$DEV/iommu_group")")" = "$GRP" ] || die "iommu group is not $GRP" |
| 48 | [ "$(ls "$IOMMU/$GRP/devices")" = "$BDF" ] || die "group $GRP is not $BDF alone" |
| 49 | log "device $(cat "$DEV/vendor"):$(cat "$DEV/device") driver=$(drv) reset_method=$(cat "$DEV/reset_method") power=$(cat "$DEV/power/control")/$(cat "$DEV/power/runtime_status")" |
| 50 | log "modules: $(lsmod | awk '/^nvidia/{printf "%s(%s) ", $1, $3}')" |
| 51 | log "cmdline: $(cat /proc/cmdline)" |
| 52 | log "sysctl: kernel.panic=$(sysctl -n kernel.panic) kernel.panic_on_oops=$(sysctl -n kernel.panic_on_oops)" |
| 53 | for f in "$HOLD" "$UNIT" "$STATE" "$IDENTITY" "$W0SUM"; do |
| 54 | [ ! -e "$f" ] || die "$f already exists (an earlier window did not finish): run window/d2-restore.sh --go" |
| 55 | done |
| 56 | # No window is open (checked above), so the current privilege state is the baseline R4 must return to. |
| 57 | if [ -e "$PRIVBASE" ]; then |
| 58 | privstate | diff "$PRIVBASE" - | sed 's/^/baseline change since last W0: /' | tee -a "$LOG" || true |
| 59 | fi |
| 60 | privstate > "$PRIVBASE" |
| 61 | log "privilege baseline: $(wc -l < "$PRIVBASE") entries in $PRIVBASE (sha256 $(sha256sum "$PRIVBASE" | cut -c1-64))" |
| 62 | privcheck || die "privilege state not clean before the window (see PRIV lines)" |
| 63 | log "privilege state clean: /dev/vfio groups root 0600, no vfio udev/memlock file, user not in group vfio, no driver_override, no window files" |
| 64 | # nvgrace-gpu-vfio-pci (the upstream variant for Grace C2C GPUs) must not claim this device: plain vfio-pci. |
| 65 | if modinfo nvgrace-gpu-vfio-pci 2>/dev/null | grep -qi 'd00002E12'; then |
| 66 | die "nvgrace-gpu-vfio-pci now lists 2E12: re-decide variant vs plain vfio-pci before binding" |
| 67 | fi |
| 68 | log "nvgrace-gpu-vfio-pci aliases ($(modinfo nvgrace-gpu-vfio-pci 2>/dev/null | grep -c '^alias')) do not list 10de:2e12: plain vfio-pci" |
| 69 | listing=$(mktemp "$LOGDIR/initramfs.XXXXXX") |
| 70 | if ! lsinitramfs "/boot/initrd.img-$(uname -r)" > "$listing" || [ ! -s "$listing" ]; then |
| 71 | rm -f "$listing" |
| 72 | die "could not inspect initramfs" |
| 73 | fi |
| 74 | if grep -Eq '/nvidia(_uvm|_modeset|_drm)?\.ko' "$listing"; then |
| 75 | rm -f "$listing" |
| 76 | die "initramfs carries nvidia.ko: a modprobe.d hold-back would not be enough" |
| 77 | fi |
| 78 | rm -f "$listing" |
| 79 | log "initramfs has no nvidia/nvidia_uvm/nvidia_modeset/nvidia_drm" |
| 80 | modinfo -n vfio-pci >/dev/null 2>&1 || die "vfio-pci module not available" |
| 81 | p=$(live_pid) |
| 82 | if [ -n "$p" ] && [ "$STAGE" = W0 ]; then |
| 83 | # An unrelated service can have sensitive command-line options. Compare in memory. |
| 84 | [ "$(snap35b "$p")" = "$(snap_expected)" ] || die "port 18091 belongs to another launch recipe. Wait for its owner" |
| 85 | snap_expected > "$SNAP" |
| 86 | log "35B pid $p matches the restore recipe" |
| 87 | elif [ -n "$p" ]; then |
| 88 | snap35b "$p" > "$SNAP.now" |
| 89 | snap_expected > "$SNAP.want" |
| 90 | if cmp -s "$SNAP.now" "$SNAP.want"; then |
| 91 | mv "$SNAP.now" "$SNAP"; rm -f "$SNAP.want" |
| 92 | log "35B pid $p: launch record equals the restore recipe ($SNAP)" |
| 93 | else |
| 94 | diff "$SNAP.want" "$SNAP.now" | tee -a "$LOG" || true |
| 95 | die "the live 35B was not started the way d2-restore.sh restarts it: update snap_expected and R3 first" |
| 96 | fi |
| 97 | else |
| 98 | log "35B not running" |
| 99 | fi |
| 100 | log "GPU device users: $(sudo -n fuser "$ROOT"/dev/nvidia* 2>/dev/null | tr -s ' ' | tr ' ' '\n' | sort -u | while read -r q; do [ -n "$q" ] && printf '%s(%s) ' "$q" "$(cat "$PROC/$q/comm" 2>/dev/null)"; done)" |
| 101 | log "chip lock (read from the kernel lock table, not taken): $(spark-window locks gb10 2>&1 | head -2 | tr '\n' ' ')" |
| 102 | |
| 103 | if [ "$GO" != --go ]; then |
| 104 | log "W0 PASS (checks only; nothing changed)" |
| 105 | exit 0 |
| 106 | fi |
| 107 | |
| 108 | # W1 -------------------------------------------------------------------------- |
| 109 | # Only substitute checkout paths that are literal in both sed and systemd. |
| 110 | case $R in *[!a-zA-Z0-9/._-]* | /) die "unsupported checkout path in temporary boot unit" ;; esac |
| 111 | [ -r "$R/window/d2-boot.sh" ] || die "boot helper is missing" |
| 112 | rendered=$(sed "s|^WorkingDirectory=@CHECKOUT@$|WorkingDirectory=$R|" "$R/window/aien-owndriver-window.service") |
| 113 | printf '%s\n' "$rendered" | grep -Fxq "WorkingDirectory=$R" || die "could not render boot unit" |
| 114 | need_approval |
| 115 | # build/ogd is optional: without it the window runs W2, W3 (build/w0), W7 and R, and logs the driver steps NOT_RUN. |
| 116 | if [ "$STAGE" != W0 ]; then |
| 117 | [ "$STAGE" = D2 ] || has_mode exec || die "D3 needs build/ogd with a successful exact 'exec' mode" |
| 118 | fi |
| 119 | case ${RESTORE_PATH:-reboot} in |
| 120 | reboot) rm -f "$RPATH" ;; |
| 121 | rebind) echo rebind > "$RPATH" ;; |
| 122 | *) die "RESTORE_PATH must be reboot or rebind" ;; |
| 123 | esac |
| 124 | log "restore path after W7: ${RESTORE_PATH:-reboot}" |
| 125 | if [ "$STAGE" = D3 ]; then |
| 126 | [ -n "${D3_CMD:-}" ] || die "D3 needs D3_CMD: the CUDApp gate command line (window/README.md, D3)" |
| 127 | printf '%s\n' "$D3_CMD" > "$LOGDIR/d3-cmd" |
| 128 | log "D3 command: $D3_CMD" |
| 129 | fi |
| 130 | # The chip lock: no other GPU job may be running; it is held until the reboot ends this process. |
| 131 | exec 9>>"$GB10_LOCK" |
| 132 | flock -n 9 || die "$GB10_LOCK is held by another GPU job: wait for it" |
| 133 | if [ "$STAGE" = W0 ]; then |
| 134 | p=$(live_pid) |
| 135 | if [ -n "$p" ]; then |
| 136 | [ "$(snap35b "$p")" = "$(snap_expected)" ] || die "service changed before the identity window" |
| 137 | before=up |
| 138 | else |
| 139 | listeners=$(ss -Hltn 'sport = :18091') || die "could not inspect port 18091" |
| 140 | [ -z "$listeners" ] || die "unrecognized listener on port 18091" |
| 141 | before=down |
| 142 | fi |
| 143 | apps=$(nvidia-smi --query-compute-apps=pid --format=csv,noheader) || die "could not inspect GPU clients" |
| 144 | [ -z "$apps" ] || [ "$apps" = "$p" ] || die "another GPU client is active" |
| 145 | sha256sum "$BUILD/w0" > "$W0SUM" || die "could not seal identity executable" |
| 146 | echo "$before" > "$IDENTITY" |
| 147 | fi |
| 148 | abort_w1() { |
| 149 | rc=$? |
| 150 | trap - EXIT |
| 151 | [ "$rc" -eq 0 ] && return |
| 152 | sudo -n systemctl disable aien-owndriver-window.service >/dev/null 2>&1 || true |
| 153 | sudo -n rm -f "$HOLD" "$UNIT" |
| 154 | sudo -n systemctl daemon-reload || true |
| 155 | if [ "$STAGE" = W0 ]; then echo "restore W0" > "$STATE"; else echo restore > "$STATE"; fi |
| 156 | log "W1 aborted before reboot (rc=$rc); window files removed; restoring the 35B" |
| 157 | sh "$R/window/d2-restore.sh" --go || true |
| 158 | } |
| 159 | trap 'abort_w1' EXIT |
| 160 | |
| 161 | touch "$LIVE_MARKER" |
| 162 | board "WINDOW OPEN $STAGE (own driver): stopping 18091, reboot with nvidia held back; every agent session on the Spark ends at the reboot. Log $LOG" |
| 163 | if [ "$STAGE" = W0 ]; then |
| 164 | [ "$(live_pid)" = "$p" ] || die "service changed while arming the identity window" |
| 165 | else |
| 166 | p=$(live_pid) |
| 167 | fi |
| 168 | if [ -n "$p" ]; then |
| 169 | log "TERM 35B pid $p" |
| 170 | kill -TERM "$p" |
| 171 | fi |
| 172 | i=0 |
| 173 | while [ -n "$(live_pid)" ] || [ -n "$(nvidia-smi --query-compute-apps=pid --format=csv,noheader)" ]; do |
| 174 | i=$((i + 1)); [ "$i" -le 60 ] || die "the 35B or another compute app is still on the GPU after 60 s" |
| 175 | sleep 1 |
| 176 | done |
| 177 | log "no compute apps on the GPU" |
| 178 | printf 'kernel.panic=%s\nkernel.panic_on_oops=%s\n' "$(sysctl -n kernel.panic)" "$(sysctl -n kernel.panic_on_oops)" > "$SAVED" |
| 179 | sudo -n tee "$HOLD" >/dev/null <<'EOF' |
| 180 | # aien OwnDriver window: hold the nvidia modules back for ONE boot. |
| 181 | # window/d2-run.sh deletes this file first thing after the reboot; if it is still here, delete it and reboot. |
| 182 | blacklist nvidia |
| 183 | blacklist nvidia_uvm |
| 184 | blacklist nvidia_modeset |
| 185 | blacklist nvidia_drm |
| 186 | install nvidia /bin/false |
| 187 | install nvidia_uvm /bin/false |
| 188 | install nvidia_modeset /bin/false |
| 189 | install nvidia_drm /bin/false |
| 190 | EOF |
| 191 | printf '%s\n' "$rendered" | sudo -n tee "$UNIT" >/dev/null |
| 192 | sudo -n chmod 0644 "$UNIT" |
| 193 | sudo -n systemctl daemon-reload |
| 194 | sudo -n systemctl enable aien-owndriver-window.service |
| 195 | echo "run $STAGE" > "$STATE" |
| 196 | log "wrote $HOLD, installed and enabled $UNIT, state 'run $STAGE'" |
| 197 | board "$STAGE W1: rebooting with nvidia held back; the boot unit runs window/d2-run.sh, then R after the next reboot" |
| 198 | sync |
| 199 | log "reboot" |
| 200 | sudo -n systemctl reboot |
| 201 | trap - EXIT |