CUDApp
CUDA++: AIEN’s GPU machine-code toolchain, runtime and driver client for NVIDIA GPUs without the CUDA toolkit. Hardware coverage and driver replacement limits are explicit.
Brought by drakestapleton
| 1 | #!/bin/bash |
| 2 | # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 3 | # cudapp: swap NVIDIA's CUDA stack for CUDA++ and back, one command each way. |
| 4 | # |
| 5 | # cudapp swap status what is installed, what is loaded, what the loader finds, who holds the GPU |
| 6 | # cudapp swap user [--yes] user space: programs load CUDA++'s libcuda.so.1 instead of NVIDIA's |
| 7 | # cudapp swap kernel [--yes] kernel: NVIDIA's modules held back, AIEN's aien_gpu driver loads (next boot) |
| 8 | # cudapp swap all [--yes] both |
| 9 | # cudapp swap back [--yes] NVIDIA's stack exactly as it was (every touched file checked by SHA-256) |
| 10 | # cudapp run CMD... one program on CUDA++'s libcuda.so.1, nothing on the system changed |
| 11 | # |
| 12 | # Without --yes every level is a dry run: it checks, prints what it would change, and changes nothing. |
| 13 | # Every file it changes is copied first into /var/lib/cudapp-swap/backup-<UTC>-<level>/ with a manifest of |
| 14 | # path, state and SHA-256; `back` copies them back and checks every hash. NVIDIA's own files are never edited or |
| 15 | # removed: the user level only adds a library directory the loader searches first, the kernel level only adds a |
| 16 | # hold-back file and our module. It never reboots unless --reboot is given. |
| 17 | # |
| 18 | # Exit codes: 0 done (or dry run printed), 1 failed (back: a hash did not match), 2 usage, 3 refused by a check. |
| 19 | # tests/swap/test_swap_cli.sh runs every level against a fake machine root. |
| 20 | # |
| 21 | # Sources: --lib PATH (CUDA++ libcuda.so.1), --driver-src PATH (aien_gpu source), --kdir PATH (kernel build dir). |
| 22 | # CUDAPP_LIB, CUDAPP_DRIVER_SRC and CUDAPP_KDIR are the defaults for a run that is not a delegated root build. |
| 23 | # Escalation runs `sudo "$0" swap ...` WITHOUT -E. A sudoers rule that names only this script rejects the |
| 24 | # caller's environment (BASH_ENV, SUDO_UID, CUDAPP_DRIVER_SRC, sudo -E); it still allows arguments. Those |
| 25 | # arguments must not choose the tree make runs as root. A delegated kernel swap (SUDO_UID set to a non-root |
| 26 | # user, or this script's own sudo re-entry) builds /usr/src/aien-gpu and /lib/modules/$(uname -r)/build. |
| 27 | # Both must be owned by root and not writable by group or other, and neither may carry an ACL. The module |
| 28 | # source must contain no symlink. /lib -> usr/lib and the kernel's build symlink are followed when the link |
| 29 | # is owned by root and the resolved directory stays under /lib/modules, /usr/lib/modules or /usr/src. |
| 30 | # make and cc are taken from a fixed PATH whose directories and resolved binaries are root-owned and |
| 31 | # not group/other-writable, so a caller cannot plant a compiler via PATH, CC or MAKEFLAGS. |
| 32 | # A root shell with SUDO_UID unset may still pass --driver-src and --kdir; a shell started by sudo keeps |
| 33 | # SUDO_UID, so unset it before a custom-source build. Do not grant SETENV on the sudoers rule. |
| 34 | # The sudoers rule must name a root-owned install path. Delegated mode refuses unless this script |
| 35 | # ($SELF) and every ancestor is owned by root and not group/other-writable. A delegated user or all |
| 36 | # swap applies the same ownership check to --lib and its ancestors. |
| 37 | set -u |
| 38 | DRIVER_SRC_ENV_SET=0; [ "${CUDAPP_DRIVER_SRC+x}" = x ] && DRIVER_SRC_ENV_SET=1 |
| 39 | KDIR_ENV_SET=0; [ "${CUDAPP_KDIR+x}" = x ] && KDIR_ENV_SET=1 |
| 40 | ROOT=${CUDAPP_ROOT:-} |
| 41 | STATE=$ROOT/var/lib/cudapp-swap |
| 42 | SELF=$(readlink -f "$0") |
| 43 | SELF_DIR=$(cd "$(dirname "$SELF")" && pwd) |
| 44 | REPO_DIR=$(cd "$SELF_DIR/.." && pwd -P) |
| 45 | LIB_SRC=${CUDAPP_LIB:-$SELF_DIR/../build/swap/libcuda.so.1} |
| 46 | DRIVER_SRC=${CUDAPP_DRIVER_SRC:-$HOME/workspace/aienos-own-gpu-driver/native/linux/aien-gpu} |
| 47 | KVER=$(uname -r) |
| 48 | KDIR=${CUDAPP_KDIR:-/lib/modules/$KVER/build} |
| 49 | |
| 50 | OUR_LIBDIR=/usr/lib/cudapp |
| 51 | LDCONF=/etc/ld.so.conf.d/00000-cudapp-swap.conf # sorts before every other entry in C and UTF-8 collation |
| 52 | LDCACHE=/etc/ld.so.cache |
| 53 | MODCONF=/etc/modprobe.d/cudapp-swap-kernel.conf |
| 54 | MODLOAD=/etc/modules-load.d/cudapp-aien-gpu.conf |
| 55 | MODDIR=/lib/modules/$KVER/updates/cudapp |
| 56 | INITRD=/boot/initrd.img-$KVER |
| 57 | NV_MODULES="nvidia nvidia_uvm nvidia_modeset nvidia_drm" |
| 58 | |
| 59 | YES=0; REBOOT=0; HOLDERS_OK=0 |
| 60 | DRIVER_SRC_OPTION=0; KDIR_OPTION=0 |
| 61 | die() { echo "cudapp: $*" >&2; exit 1; } |
| 62 | refuse() { echo "REFUSED: $*" >&2; exit 3; } |
| 63 | # A machine that schedules GPU work through spark-window (the invoking user has the tool) reboots only inside a window. |
| 64 | window_host() { |
| 65 | command -v spark-window >/dev/null 2>&1 && return 0 |
| 66 | [ -n "${SUDO_USER:-}" ] || return 1 |
| 67 | local h; h=$(getent passwd "$SUDO_USER" | cut -d: -f6) |
| 68 | [ -n "$h" ] && [ -x "$h/.local/bin/spark-window" ] |
| 69 | } |
| 70 | usage() { sed -n '3,9p' "$0" | sed 's/^# \{0,1\}//' >&2; exit 2; } |
| 71 | sha() { if [ -e "$1" ] || [ -L "$1" ]; then sha256sum < "$1" | cut -d' ' -f1; else echo -; fi; } |
| 72 | # sudo sets SUDO_UID itself. A sudoers rule without SETENV rejects `sudo SUDO_UID=...`; do not trust a |
| 73 | # value the caller could have kept only when SETENV is granted (that grant also allows BASH_ENV). |
| 74 | sudo_origin() { [ -n "${SUDO_UID:-}" ] && [ "$SUDO_UID" != 0 ]; } |
| 75 | caller_selected_build() { |
| 76 | [ "$DRIVER_SRC_OPTION" -eq 1 ] || [ "$KDIR_OPTION" -eq 1 ] || \ |
| 77 | [ "$DRIVER_SRC_ENV_SET" -eq 1 ] || [ "$KDIR_ENV_SET" -eq 1 ] |
| 78 | } |
| 79 | delegated_run() { |
| 80 | sudo_origin && { [ "$(id -u)" -eq 0 ] || [ -n "$ROOT" ]; } |
| 81 | } |
| 82 | delegated_build() { |
| 83 | case $level in kernel|all) ;; *) return 1;; esac |
| 84 | delegated_run |
| 85 | } |
| 86 | # A sudoers rule that names this script would otherwise exec a user-writable checkout as root. |
| 87 | # On the real system, $SELF and (for user|all) the resolved --lib must be root-owned all the way |
| 88 | # to /. The fixture (CUDAPP_ROOT set) cannot walk $SELF: the tool under test lives outside the |
| 89 | # fake machine root. It still walks --lib inside that root, with the fixture uid standing in for 0. |
| 90 | require_delegated_install() { |
| 91 | local owner stop lib |
| 92 | if [ -n "$ROOT" ]; then |
| 93 | stop=${ROOT%/} |
| 94 | owner=$(stat -c %u "$stop") || refuse "cannot stat the test machine root" |
| 95 | case $level in |
| 96 | user|all) |
| 97 | lib=$(readlink -e "$LIB_SRC") || refuse "--lib $LIB_SRC does not exist" |
| 98 | [ -f "$lib" ] || refuse "--lib $lib is not a regular file" |
| 99 | case $lib in |
| 100 | "$stop"|"$stop"/*) ;; |
| 101 | *) refuse "--lib $lib is outside the test machine root";; |
| 102 | esac |
| 103 | walk_owned "$lib" "$stop" "$owner" |
| 104 | LIB_SRC=$lib |
| 105 | ;; |
| 106 | esac |
| 107 | return 0 |
| 108 | fi |
| 109 | walk_owned "$SELF" / 0 |
| 110 | case $level in |
| 111 | user|all) |
| 112 | lib=$(readlink -e "$LIB_SRC") || refuse "--lib $LIB_SRC does not exist" |
| 113 | case $lib in "$REPO_DIR"/*) ;; *) refuse "--lib $lib is outside the CUDA++ repository $REPO_DIR";; esac |
| 114 | [ -f "$lib" ] || refuse "--lib $lib is not a regular file" |
| 115 | walk_owned "$lib" / 0 |
| 116 | LIB_SRC=$lib |
| 117 | ;; |
| 118 | esac |
| 119 | } |
| 120 | # $1 path, $2 owner uid. A symlink's mode is ignored by the kernel, so only its owner is checked here. |
| 121 | # An ACL can grant write without setting the mode bits, and this helper has no getfacl. GNU ls prints '+' |
| 122 | # after the mode when an ACL is present ('.' is a security context and is left alone). Any ACL is refused. |
| 123 | path_owner_mode() { |
| 124 | local p=$1 owner=$2 uid mode perm mark |
| 125 | [ -e "$p" ] || [ -L "$p" ] || refuse "trusted path does not exist: $p" |
| 126 | uid=$(stat -c %u "$p") || refuse "cannot stat $p" |
| 127 | [ "$uid" = "$owner" ] || refuse "trusted path is not owned by uid $owner: $p" |
| 128 | if [ -L "$p" ]; then return 0; fi |
| 129 | mode=$(stat -c %a "$p") || refuse "cannot stat $p" |
| 130 | perm=$((8#$mode & 8#022)) |
| 131 | [ "$perm" -eq 0 ] || refuse "trusted path is writable by group or other: $p" |
| 132 | mark=$(ls -ld "$p" | awk '{print $1}') || refuse "cannot list $p" |
| 133 | case $mark in ??????????+*) refuse "trusted path has an ACL: $p";; esac |
| 134 | } |
| 135 | # Collapse "." and ".." without following symlinks. |
| 136 | normalize() { |
| 137 | local in=$1 part out= |
| 138 | case $in in /*) ;; *) in=/$in;; esac |
| 139 | while [ "$in" != / ] && [ -n "$in" ]; do |
| 140 | in=${in#/} |
| 141 | part=${in%%/*} |
| 142 | if [ "$part" = "$in" ]; then in=; else in=${in#*/}; fi |
| 143 | case $part in |
| 144 | ''|.) ;; |
| 145 | ..) [ -n "$out" ] && out=${out%/*};; |
| 146 | *) out=$out/$part;; |
| 147 | esac |
| 148 | done |
| 149 | [ -n "$out" ] || out=/ |
| 150 | printf '%s\n' "$out" |
| 151 | } |
| 152 | # Walk $1 up through $2 inclusive. No component may be a symlink. |
| 153 | walk_owned() { |
| 154 | local p=$1 stop=$2 owner=$3 |
| 155 | while :; do |
| 156 | [ ! -L "$p" ] || refuse "trusted path contains a symlink: $p" |
| 157 | path_owner_mode "$p" "$owner" |
| 158 | [ "$p" = "$stop" ] && break |
| 159 | [ "$p" = / ] && break |
| 160 | p=${p%/*}; [ -n "$p" ] || p=/ |
| 161 | done |
| 162 | } |
| 163 | apply_delegated_sources() { |
| 164 | local owner stop src kd target shown |
| 165 | if sudo_origin && [ "$EUID" -eq 0 ] && [ -n "$ROOT" ]; then |
| 166 | refuse "CUDAPP_ROOT is refused for a delegated root swap" |
| 167 | fi |
| 168 | if caller_selected_build; then |
| 169 | refuse "a delegated kernel swap cannot take --driver-src, --kdir, CUDAPP_DRIVER_SRC or CUDAPP_KDIR; root uses /usr/src/aien-gpu and /lib/modules/<uname -r>/build" |
| 170 | fi |
| 171 | # The fixture tests are not root. A real delegated root build also pins make and cc. |
| 172 | if [ "$EUID" -eq 0 ]; then |
| 173 | require_trusted_make_path |
| 174 | fi |
| 175 | if [ -n "$ROOT" ]; then |
| 176 | stop=${ROOT%/} |
| 177 | owner=$(stat -c %u "$stop") || refuse "cannot stat the test machine root" |
| 178 | src=$stop/usr/src/aien-gpu |
| 179 | kd=$stop/lib/modules/$KVER/build |
| 180 | else |
| 181 | stop=/ |
| 182 | owner=0 |
| 183 | src=/usr/src/aien-gpu |
| 184 | kd=/lib/modules/$KVER/build |
| 185 | fi |
| 186 | [ -d "$src" ] || [ -L "$src" ] || refuse "trusted aien_gpu source not found at /usr/src/aien-gpu" |
| 187 | walk_owned "$src" "$stop" "$owner" |
| 188 | shown=$(find -P "$src" \( -type l -o ! -uid "$owner" -o -perm /022 -o ! \( -type f -o -type d \) \) -print -quit 2>/dev/null) || \ |
| 189 | refuse "cannot inspect trusted aien_gpu source at /usr/src/aien-gpu" |
| 190 | [ -z "$shown" ] || refuse "trusted aien_gpu source must contain no symlink, be owned by uid $owner, and not be writable by group or other: $shown" |
| 191 | [ -f "$src/Kbuild" ] && [ ! -L "$src/Kbuild" ] || refuse "trusted aien_gpu source has no regular Kbuild at /usr/src/aien-gpu" |
| 192 | while IFS= read -r shown; do |
| 193 | [ -n "$shown" ] || continue |
| 194 | refuse "trusted aien_gpu source has an ACL: $shown" |
| 195 | done < <(find -P "$src" \( -type f -o -type d \) -exec ls -ld {} + | awk 'substr($1, 11, 1) == "+" { print $NF; exit }') |
| 196 | # /lib is a symlink to usr/lib on a merged /usr, and the kernel build dir is often itself a symlink |
| 197 | # into /usr/src. Follow those links. Each link is owned by the trusted uid, and every real directory |
| 198 | # on the resolved path is owned by that uid and not group/other-writable. The result stays under the |
| 199 | # machine root, and on the real system under /lib/modules, /usr/lib/modules or /usr/src. |
| 200 | [ -e "$kd" ] || [ -L "$kd" ] || refuse "kernel headers for $KVER not found at /lib/modules/$KVER/build" |
| 201 | follow_trusted "$kd" "$stop" "$owner" |
| 202 | target=$RESOLVED |
| 203 | if [ -z "$ROOT" ]; then |
| 204 | case $target in /lib/modules/*|/usr/lib/modules/*|/usr/src/*) ;; *) refuse "kernel build dir $target is not under /lib/modules or /usr/src";; esac |
| 205 | fi |
| 206 | [ -d "$target" ] || refuse "kernel build dir $target is not a directory" |
| 207 | DRIVER_SRC=$src |
| 208 | KDIR=$target |
| 209 | } |
| 210 | # Resolve $1 by following symlinks. Sets RESOLVED. Components at or above $2 are the trust boundary |
| 211 | # (the fixture root in a test, / on the machine) and are not walked. A link that leaves $2 is refused. |
| 212 | follow_trusted() { |
| 213 | local path=$1 stop=$2 owner=$3 |
| 214 | local rest cur part next link dest suffix old hops=0 |
| 215 | if [ "$stop" = / ]; then |
| 216 | case $path in /*) ;; *) refuse "trusted path is outside the machine root: $path";; esac |
| 217 | else |
| 218 | case $path in |
| 219 | "$stop"|"$stop"/*) ;; |
| 220 | *) refuse "trusted path is outside the machine root: $path";; |
| 221 | esac |
| 222 | fi |
| 223 | if [ "$stop" = / ]; then |
| 224 | rest=${path#/} |
| 225 | cur= |
| 226 | else |
| 227 | path_owner_mode "$stop" "$owner" |
| 228 | rest=${path#"$stop"} |
| 229 | rest=${rest#/} |
| 230 | cur=$stop |
| 231 | fi |
| 232 | while [ -n "$rest" ]; do |
| 233 | part=${rest%%/*} |
| 234 | if [ "$part" = "$rest" ]; then rest=; else rest=${rest#*/}; fi |
| 235 | case $part in |
| 236 | ''|.) continue;; |
| 237 | ..) |
| 238 | if [ "$cur" = "$stop" ] || [ -z "$cur" ] || [ "$cur" = / ]; then |
| 239 | refuse "trusted path climbs above its root: $path" |
| 240 | fi |
| 241 | cur=${cur%/*} |
| 242 | [ -n "$cur" ] || cur=/ |
| 243 | continue;; |
| 244 | esac |
| 245 | if [ -z "$cur" ] || [ "$cur" = / ]; then next=/$part; else next=$cur/$part; fi |
| 246 | if [ -L "$next" ]; then |
| 247 | hops=$((hops + 1)) |
| 248 | [ "$hops" -le 16 ] || refuse "trusted path has too many symlinks: $path" |
| 249 | path_owner_mode "$next" "$owner" |
| 250 | link=$(readlink "$next") || refuse "cannot read symlink $next" |
| 251 | case $link in |
| 252 | /*) dest=$link;; |
| 253 | *) dest=$(dirname "$next")/$link;; |
| 254 | esac |
| 255 | dest=$(normalize "$dest") |
| 256 | old=$rest |
| 257 | if [ "$stop" = / ]; then |
| 258 | rest=${dest#/} |
| 259 | cur= |
| 260 | else |
| 261 | case $dest in |
| 262 | "$stop"|"$stop"/*) ;; |
| 263 | *) refuse "trusted path resolves outside the machine root: $dest";; |
| 264 | esac |
| 265 | suffix=${dest#"$stop"} |
| 266 | suffix=${suffix#/} |
| 267 | rest=$suffix |
| 268 | cur=$stop |
| 269 | fi |
| 270 | if [ -n "$old" ]; then |
| 271 | if [ -n "$rest" ]; then rest=$rest/$old; else rest=$old; fi |
| 272 | fi |
| 273 | continue |
| 274 | fi |
| 275 | path_owner_mode "$next" "$owner" |
| 276 | cur=$next |
| 277 | done |
| 278 | [ -n "$cur" ] || cur=/ |
| 279 | RESOLVED=$cur |
| 280 | } |
| 281 | # Directories and binaries a delegated root build may execute. Caller environment is not consulted. |
| 282 | require_trusted_make_path() { |
| 283 | local d tool path dirs |
| 284 | TRUSTED_PATH_OK= |
| 285 | IFS=: read -r -a dirs <<'EOF' |
| 286 | /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin |
| 287 | EOF |
| 288 | for d in "${dirs[@]}"; do |
| 289 | [ -e "$d" ] || [ -L "$d" ] || continue |
| 290 | follow_trusted "$d" / 0 |
| 291 | if [ -n "$TRUSTED_PATH_OK" ]; then TRUSTED_PATH_OK=$TRUSTED_PATH_OK:$RESOLVED |
| 292 | else TRUSTED_PATH_OK=$RESOLVED; fi |
| 293 | done |
| 294 | [ -n "$TRUSTED_PATH_OK" ] || refuse "no trusted directory on the make PATH" |
| 295 | for tool in make cc; do |
| 296 | path=$(PATH=$TRUSTED_PATH_OK command -v "$tool") || refuse "$tool is not on the trusted PATH" |
| 297 | follow_trusted "$path" / 0 |
| 298 | [ -f "$RESOLVED" ] && [ ! -L "$RESOLVED" ] || refuse "$tool does not resolve to a regular file: $path" |
| 299 | done |
| 300 | } |
| 301 | |
| 302 | # ---------------------------------------------------------------- facts |
| 303 | gpu_ids() { # prints "vendor:device" of every NVIDIA PCI function |
| 304 | local d |
| 305 | for d in "$ROOT"/sys/bus/pci/devices/*; do |
| 306 | [ -r "$d/vendor" ] || continue |
| 307 | [ "$(cat "$d/vendor")" = 0x10de ] || continue |
| 308 | echo "10de:$(sed 's/^0x//' "$d/device")" |
| 309 | done |
| 310 | } |
| 311 | is_gb10() { gpu_ids | grep -qx "10de:2e12"; } |
| 312 | nv_version() { cat "$ROOT/sys/module/nvidia/version" 2>/dev/null || echo "not loaded"; } |
| 313 | secure_boot() { local s; s=$(mokutil --sb-state 2>/dev/null); case $s in *enabled*) echo enabled;; *disabled*) echo disabled;; *) echo unknown;; esac; } |
| 314 | holders() { fuser "$ROOT"/dev/nvidia* 2>/dev/null | tr -s ' :' '\n' | grep -E '^[0-9]+$' | sort -un | tr '\n' ' '; } |
| 315 | free_mb() { df -Pm "${ROOT:-/}" 2>/dev/null | awk 'NR==2{print $4}'; } |
| 316 | loader_choice() { # which libcuda.so.1 the dynamic loader finds first |
| 317 | if [ -n "$ROOT" ]; then |
| 318 | if [ -f "$ROOT$LDCONF" ] && [ -e "$ROOT$OUR_LIBDIR/libcuda.so.1" ]; then echo "$OUR_LIBDIR/libcuda.so.1"; return; fi |
| 319 | echo "/usr/lib/aarch64-linux-gnu/libcuda.so.1"; return |
| 320 | fi |
| 321 | ldconfig -p 2>/dev/null | awk '$1=="libcuda.so.1"{print $NF; exit}' |
| 322 | } |
| 323 | user_swapped() { [ -f "$ROOT$LDCONF" ]; } |
| 324 | kernel_staged() { [ -f "$ROOT$MODCONF" ]; } |
| 325 | nv_in_initramfs(){ lsinitramfs "$ROOT$INITRD" 2>/dev/null | grep -qE '/nvidia(_uvm|_modeset|_drm)?\.ko'; } |
| 326 | |
| 327 | status() { |
| 328 | local ids; ids=$(gpu_ids | tr '\n' ' ') |
| 329 | if is_gb10; then echo "GPU: NVIDIA GB10 (10de:2e12)"; else echo "GPU: ${ids:-no NVIDIA GPU found} (not a GB10)"; fi |
| 330 | echo "NVIDIA kernel driver: $(nv_version)" |
| 331 | echo "Secure Boot: $(secure_boot)" |
| 332 | echo "free disk: $(free_mb) MB" |
| 333 | local h; h=$(holders); echo "holding /dev/nvidia*: ${h:-nobody}" |
| 334 | local c; c=$(loader_choice) |
| 335 | case $c in *cudapp*) echo "libcuda.so.1: CUDA++ ($c)";; "") echo "libcuda.so.1: none found";; *) echo "libcuda.so.1: NVIDIA ($c)";; esac |
| 336 | if user_swapped; then echo "user level: SWAPPED (CUDA++ libcuda.so.1 first in the loader's search)"; else echo "user level: NVIDIA"; fi |
| 337 | if kernel_staged; then |
| 338 | if [ -d "$ROOT/sys/module/aien_gpu" ] && [ ! -d "$ROOT/sys/module/nvidia" ]; then echo "kernel level: SWAPPED (aien_gpu loaded, NVIDIA held back)" |
| 339 | else echo "kernel level: STAGED (takes effect at the next boot)"; fi |
| 340 | else echo "kernel level: NVIDIA"; fi |
| 341 | local b; b=$(ls -d "$STATE"/backup-* 2>/dev/null | tr '\n' ' '); echo "backups: ${b:-none}" |
| 342 | } |
| 343 | |
| 344 | # ---------------------------------------------------------------- backup + restore |
| 345 | BK="" |
| 346 | backup_begin() { # $1 = level |
| 347 | BK=$STATE/backup-$(date -u +%Y%m%dT%H%M%SZ)-$1 |
| 348 | mkdir -p "$BK/files" || die "cannot create $BK" |
| 349 | : > "$BK/manifest.tsv" |
| 350 | } |
| 351 | backup_file() { # $1 = path (system path, without ROOT): copy + record state and hash before we touch it |
| 352 | local p=$1 f=$ROOT$1 |
| 353 | grep -q "^$p " "$BK/manifest.tsv" 2>/dev/null && return 0 |
| 354 | if [ -L "$f" ]; then |
| 355 | printf '%s\tlink\t%s\n' "$p" "$(readlink "$f")" >> "$BK/manifest.tsv" |
| 356 | elif [ -e "$f" ]; then |
| 357 | mkdir -p "$BK/files$(dirname "$p")"; cp -p "$f" "$BK/files$p" || die "backup of $p failed" |
| 358 | printf '%s\tfile\t%s\n' "$p" "$(sha "$f")" >> "$BK/manifest.tsv" |
| 359 | else |
| 360 | printf '%s\tabsent\t-\n' "$p" >> "$BK/manifest.tsv" |
| 361 | fi |
| 362 | } |
| 363 | backup_done() { echo applied > "$BK/APPLIED"; echo "backup: $BK ($(wc -l < "$BK/manifest.tsv") files recorded)"; } |
| 364 | |
| 365 | restore_one() { # $1 = backup dir; restores and checks; prints MISMATCH lines; returns count of mismatches |
| 366 | local b=$1 bad=0 p kind val f |
| 367 | while IFS=$'\t' read -r p kind val; do |
| 368 | f=$ROOT$p |
| 369 | case $kind in |
| 370 | absent) rm -f "$f" |
| 371 | if [ -e "$f" ] || [ -L "$f" ]; then echo "MISMATCH $p: still present"; bad=$((bad+1)); fi ;; |
| 372 | link) rm -f "$f"; ln -s "$val" "$f" |
| 373 | [ "$(readlink "$f")" = "$val" ] || { echo "MISMATCH $p: link"; bad=$((bad+1)); } ;; |
| 374 | file) mkdir -p "$(dirname "$f")"; rm -f "$f"; cp -p "$b/files$p" "$f" |
| 375 | [ "$(sha "$f")" = "$val" ] || { echo "MISMATCH $p: sha256 $(sha "$f") != recorded $val"; bad=$((bad+1)); } ;; |
| 376 | esac |
| 377 | done < <(tac "$b/manifest.tsv") |
| 378 | rmdir "$ROOT$OUR_LIBDIR" "$ROOT$MODDIR" 2>/dev/null # only the directories this tool creates, and only if empty |
| 379 | return $bad |
| 380 | } |
| 381 | |
| 382 | # ---------------------------------------------------------------- levels |
| 383 | plan() { if [ $YES -eq 1 ]; then echo " * $*"; else echo " - would: $*"; fi; } |
| 384 | |
| 385 | user_level() { |
| 386 | echo "== user level: CUDA++ libcuda.so.1 replaces NVIDIA's for every program the loader starts" |
| 387 | [ -f "$LIB_SRC" ] || refuse "CUDA++ libcuda.so.1 not built at $LIB_SRC (run: make libcuda)" |
| 388 | user_swapped && { echo "already swapped ($LDCONF present); nothing to do"; return 0; } |
| 389 | local want; want=$(sha "$LIB_SRC") |
| 390 | plan "install $LIB_SRC as $OUR_LIBDIR/libcuda.so.1 (sha256 $(cut -c1-16 <<<"$want"))" |
| 391 | plan "write $LDCONF -> $OUR_LIBDIR, so the loader searches it before $(loader_choice | xargs -r dirname)" |
| 392 | plan "run ldconfig (rewrites $LDCACHE; the old one is kept in the backup)" |
| 393 | echo " NVIDIA's libcuda files stay installed and unchanged; 'cudapp swap back' restores the old search order." |
| 394 | [ $YES -eq 1 ] || return 0 |
| 395 | backup_begin user |
| 396 | backup_file "$OUR_LIBDIR/libcuda.so.1"; backup_file "$LDCONF"; backup_file "$LDCACHE" |
| 397 | mkdir -p "$ROOT$OUR_LIBDIR" && install -m 0755 "$LIB_SRC" "$ROOT$OUR_LIBDIR/libcuda.so.1" || die "install failed" |
| 398 | local got; got=$(sha "$ROOT$OUR_LIBDIR/libcuda.so.1") |
| 399 | if [ "$got" != "$want" ]; then |
| 400 | restore_one "$BK"; mv "$BK" "$BK.undone" |
| 401 | die "installed libcuda.so.1 sha256 $got != $want planned from $LIB_SRC (source changed during install); restored" |
| 402 | fi |
| 403 | printf '%s %s\n' "$want" "$OUR_LIBDIR/libcuda.so.1" > "$BK/installed.sha256" |
| 404 | echo "$OUR_LIBDIR" > "$ROOT$LDCONF" |
| 405 | ldconfig ${ROOT:+-r "$ROOT"} || die "ldconfig failed" |
| 406 | backup_done |
| 407 | local c; c=$(loader_choice) |
| 408 | case $c in *cudapp*) echo "loader now finds: $c";; *) echo "loader still finds $c: restoring"; restore_one "$BK"; mv "$BK" "$BK.undone"; die "swap did not take";; esac |
| 409 | } |
| 410 | |
| 411 | kernel_level() { |
| 412 | echo "== kernel level: AIEN's aien_gpu driver (ledger L332) instead of NVIDIA's open kernel modules" |
| 413 | is_gb10 || refuse "no GB10 here (found: $(gpu_ids | tr '\n' ' ')); aien_gpu drives the GB10 (10de:2e12) only" |
| 414 | local sb; sb=$(secure_boot) |
| 415 | [ "$sb" = enabled ] && refuse "Secure Boot is enabled: the kernel would refuse the unsigned aien_gpu module and boot with no GPU driver. Disable Secure Boot or enrol a signing key first." |
| 416 | [ -f "$DRIVER_SRC/Kbuild" ] || refuse "aien_gpu source not found at $DRIVER_SRC (set CUDAPP_DRIVER_SRC)" |
| 417 | [ -d "$KDIR" ] || refuse "kernel headers for $KVER not found at $KDIR (install linux-headers-$KVER)" |
| 418 | local h; h=$(holders) |
| 419 | if [ -n "$h" ]; then |
| 420 | echo " processes holding /dev/nvidia* (they lose the GPU at the reboot): $h" |
| 421 | [ $YES -eq 1 ] && [ $HOLDERS_OK -eq 0 ] && refuse "processes hold /dev/nvidia*: $h. Stop them (or pass --holders-ok to stage the switch for the next boot anyway)." |
| 422 | fi |
| 423 | local need=$(( $(stat -c %s "$ROOT$INITRD" 2>/dev/null || echo 0) / 1048576 * 2 + 64 )) |
| 424 | [ "$(free_mb)" -ge "$need" ] 2>/dev/null || refuse "not enough free disk: need $need MB for the module and the initramfs backup, have $(free_mb) MB" |
| 425 | plan "build aien_gpu.ko from $DRIVER_SRC for kernel $KVER and install it as $MODDIR/aien_gpu.ko" |
| 426 | plan "write $MODCONF: blacklist + 'install <m> /bin/false' for $NV_MODULES (nothing loads them, not even an explicit modprobe)" |
| 427 | plan "write $MODLOAD: load aien_gpu at boot (options aien_gpu allow_bind=1)" |
| 428 | plan "run depmod -a $KVER (modules.* files are kept in the backup)" |
| 429 | if nv_in_initramfs; then plan "run update-initramfs -u -k $KVER (NVIDIA modules are in $INITRD; the old initramfs is kept in the backup)" |
| 430 | else echo " initramfs: no NVIDIA module in $INITRD, the hold-back file is enough (not rebuilt)"; fi |
| 431 | echo " takes effect at the next reboot: NVIDIA's modules then stay unloaded and aien_gpu owns the GB10." |
| 432 | echo " Why a reboot: unloading nvidia_modeset can hang on this machine (L158), so the switch never unloads a live driver." |
| 433 | echo " NVIDIA's signed GSP firmware stays installed; aien_gpu loads it (the chip checks its signature)." |
| 434 | [ $YES -eq 1 ] || return 0 |
| 435 | backup_begin kernel |
| 436 | local m |
| 437 | backup_file "$MODDIR/aien_gpu.ko"; backup_file "$MODCONF"; backup_file "$MODLOAD" |
| 438 | for m in "$ROOT"/lib/modules/$KVER/modules.*; do [ -e "$m" ] && backup_file "${m#$ROOT}"; done |
| 439 | nv_in_initramfs && backup_file "$INITRD" |
| 440 | local B; B=$(mktemp -d) && cp -a -- "$DRIVER_SRC"/. "$B"/ || die "copy of driver source failed" |
| 441 | # A delegated root build must not inherit CC, MAKEFLAGS or LD_PRELOAD from `sudo VAR=value`. |
| 442 | # sudoers without SETENV already rejects those assignments; this is the same boundary for make. |
| 443 | if [ "$EUID" -eq 0 ] && sudo_origin; then |
| 444 | env -i PATH="$TRUSTED_PATH_OK" HOME="${HOME:-/root}" \ |
| 445 | make -C "$KDIR" M="$B" modules > "$BK/build.log" 2>&1 || { cat "$BK/build.log" >&2; die "aien_gpu build failed (log $BK/build.log)"; } |
| 446 | else |
| 447 | make -C "$KDIR" M="$B" modules > "$BK/build.log" 2>&1 || { cat "$BK/build.log" >&2; die "aien_gpu build failed (log $BK/build.log)"; } |
| 448 | fi |
| 449 | local vm; vm=$(modinfo -F vermagic "$B/aien_gpu.ko" 2>/dev/null; modinfo "$B/aien_gpu.ko" 2>/dev/null | awk '/^vermagic/{print $2}') |
| 450 | grep -q "$KVER" <<<"$vm" || die "built module vermagic '$vm' does not match running kernel $KVER" |
| 451 | mkdir -p "$ROOT$MODDIR" && install -m 0644 "$B/aien_gpu.ko" "$ROOT$MODDIR/aien_gpu.ko" || die "module install failed" |
| 452 | rm -rf "$B" |
| 453 | { echo "# cudapp swap kernel: NVIDIA's kernel modules held back, AIEN aien_gpu drives the GB10. Undo: cudapp swap back" |
| 454 | for m in $NV_MODULES; do echo "blacklist $m"; echo "install $m /bin/false"; done |
| 455 | echo "options aien_gpu allow_bind=1"; } > "$ROOT$MODCONF" |
| 456 | { echo "# cudapp swap kernel"; echo "aien_gpu"; } > "$ROOT$MODLOAD" |
| 457 | depmod -a "$KVER" || die "depmod failed" |
| 458 | if nv_in_initramfs || [ -f "$BK/files$INITRD" ]; then update-initramfs -u -k "$KVER" || die "update-initramfs failed"; fi |
| 459 | backup_done |
| 460 | echo "kernel level STAGED: reboot to switch. To return: cudapp swap back --yes, then reboot." |
| 461 | if [ $REBOOT -eq 1 ]; then |
| 462 | window_host && [ -z "${SPARK_WINDOW:-}" ] && refuse "on the Spark the reboot runs only inside a scheduled spark-window" |
| 463 | echo "rebooting (--reboot)"; reboot |
| 464 | fi |
| 465 | } |
| 466 | |
| 467 | back_level() { |
| 468 | local list; list=$(ls -d "$STATE"/backup-* 2>/dev/null | grep -v -e '\.undone$' -e '\.restored$' | LC_ALL=C sort -r) |
| 469 | [ -n "$list" ] || { echo "nothing to restore: no applied backup in $STATE"; return 0; } |
| 470 | echo "== back: restore NVIDIA's stack from"; echo "$list" | sed 's/^/ /' |
| 471 | local b |
| 472 | for b in $list; do [ -f "$b/APPLIED" ] && sed 's/^/ - restore: /' "$b/manifest.tsv"; done |
| 473 | [ $YES -eq 1 ] || { echo "DRY RUN: nothing changed. Run again with --yes."; return 0; } |
| 474 | local total=0 bad=0 n |
| 475 | for b in $list; do |
| 476 | [ -f "$b/APPLIED" ] || continue |
| 477 | restore_one "$b"; n=$?; bad=$((bad+n)); total=$((total+$(wc -l < "$b/manifest.tsv"))) |
| 478 | [ $n -eq 0 ] && mv "$b" "$b.restored" |
| 479 | done |
| 480 | if [ $bad -eq 0 ]; then |
| 481 | echo "BACK PASS: $total files match their pre-swap state (sha256)" |
| 482 | [ -d "$ROOT/sys/module/aien_gpu" ] && echo "aien_gpu is loaded now: reboot to give the GB10 back to NVIDIA's driver." |
| 483 | return 0 |
| 484 | fi |
| 485 | echo "BACK FAIL: $bad of $total files do not match; backups kept in $STATE"; return 1 |
| 486 | } |
| 487 | |
| 488 | # ---------------------------------------------------------------- main |
| 489 | [ $# -ge 1 ] || usage |
| 490 | cmd=$1; shift |
| 491 | if [ "$cmd" = run ]; then |
| 492 | [ $# -ge 1 ] || usage |
| 493 | d=$(cd "$(dirname "$LIB_SRC")" 2>/dev/null && pwd) || refuse "CUDA++ libcuda.so.1 not built at $LIB_SRC (run: make libcuda)" |
| 494 | LD_LIBRARY_PATH=$d${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH} exec "$@" |
| 495 | fi |
| 496 | [ "$cmd" = swap ] || usage |
| 497 | [ $# -ge 1 ] || usage |
| 498 | level=$1; shift |
| 499 | while [ $# -gt 0 ]; do |
| 500 | case $1 in |
| 501 | --yes) YES=1;; --reboot) REBOOT=1;; --holders-ok) HOLDERS_OK=1;; |
| 502 | --in-spark-window) SPARK_WINDOW=1;; |
| 503 | --lib|--driver-src|--kdir) |
| 504 | [ $# -ge 2 ] && [ -n "$2" ] || usage |
| 505 | case $1 in |
| 506 | --lib) LIB_SRC=$2;; |
| 507 | --driver-src) DRIVER_SRC=$2; DRIVER_SRC_OPTION=1;; |
| 508 | --kdir) KDIR=$2; KDIR_OPTION=1;; |
| 509 | esac |
| 510 | shift;; |
| 511 | *) usage;; |
| 512 | esac |
| 513 | shift |
| 514 | done |
| 515 | case $level in status|user|kernel|all|back) ;; *) usage;; esac |
| 516 | if [ $YES -eq 1 ] && [ -z "$ROOT" ] && [ "$(id -u)" -ne 0 ]; then |
| 517 | # No -E: root gets a clean environment. The module source and the kernel build dir are chosen |
| 518 | # again in the root process; they are not arguments a delegated caller can set. |
| 519 | case $level in |
| 520 | kernel|all) |
| 521 | if caller_selected_build; then |
| 522 | refuse "a delegated kernel swap cannot take --driver-src, --kdir, CUDAPP_DRIVER_SRC or CUDAPP_KDIR; root uses /usr/src/aien-gpu and /lib/modules/<uname -r>/build" |
| 523 | fi |
| 524 | ;; |
| 525 | esac |
| 526 | set -- --yes |
| 527 | [ $REBOOT -eq 1 ] && set -- "$@" --reboot |
| 528 | [ $HOLDERS_OK -eq 1 ] && set -- "$@" --holders-ok |
| 529 | [ -n "${SPARK_WINDOW:-}" ] && set -- "$@" --in-spark-window |
| 530 | case $level in user|all) set -- "$@" --lib "$(readlink -m "$LIB_SRC")";; esac |
| 531 | exec sudo "$SELF" swap "$level" "$@" |
| 532 | fi |
| 533 | if delegated_run; then |
| 534 | require_delegated_install |
| 535 | if delegated_build; then |
| 536 | apply_delegated_sources |
| 537 | fi |
| 538 | fi |
| 539 | # As root on the real system, refuse sources outside the places a root install may read from. |
| 540 | if [ -z "$ROOT" ] && [ "$(id -u)" -eq 0 ]; then |
| 541 | case $level in |
| 542 | user|all) |
| 543 | lib=$(readlink -e "$LIB_SRC") || refuse "--lib $LIB_SRC does not exist" |
| 544 | case $lib in "$REPO_DIR"/*) ;; *) refuse "--lib $lib is outside the CUDA++ repository $REPO_DIR";; esac |
| 545 | [ -f "$lib" ] || refuse "--lib $lib is not a regular file" |
| 546 | LIB_SRC=$lib ;; |
| 547 | esac |
| 548 | case $level in |
| 549 | kernel|all) |
| 550 | kd=$(readlink -e "$KDIR") || refuse "--kdir $KDIR does not exist" |
| 551 | case $kd in /lib/modules/*|/usr/lib/modules/*|/usr/src/*) ;; *) refuse "--kdir $kd is not under /lib/modules or /usr/src";; esac |
| 552 | KDIR=$kd ;; |
| 553 | esac |
| 554 | fi |
| 555 | case $level in |
| 556 | status) status ;; |
| 557 | user) user_level ;; |
| 558 | kernel) kernel_level ;; |
| 559 | all) user_level && kernel_level ;; |
| 560 | back) back_level ;; |
| 561 | esac |
| 562 | rc=$? |
| 563 | [ $YES -eq 0 ] && [ "$level" != status ] && [ "$level" != back ] && echo "DRY RUN: nothing changed. Run again with --yes." |
| 564 | exit $rc |