Skip to content

fix(cuda): let the PTX disk cache trust a container volume root - #3509

Merged
inureyes merged 7 commits into
mainfrom
fix/issue-3503-ptx-cache-trusted-root
Nov 28, 2026
Merged

inureyes merged 7 commits into
mainfrom
fix/issue-3503-ptx-cache-trusted-root

Conversation

@inureyes

@inureyes inureyes commented Nov 28, 2026 •

Copy link
Copy Markdown
Member

For #3484 and #3486 (read first)

  • The env name is unchanged: MLXCEL_CACHE_TRUSTED_ROOT.
  • The default should not be MLXCEL_CACHE_DIR=/cache with the trusted root /cache. Use MLXCEL_CACHE_DIR=/cache/mlxcel with MLXCEL_CACHE_TRUSTED_ROOT=/cache, so the model store does not sit beside cuda-ptx directly in the trusted root (see the residual below). Measured working on GB10.
  • Rule 1's private-group proof reads the image's account files and does not see pod-spec groups. The chart must not give the pod's fsGroup, runAsGroup or supplementalGroups to a container running as another uid on the cache volume.
  • Unverified: with the default fsGroupChangePolicy: Always the kubelet's recursive relabel may make cache directories and entries group-writable at each mount, which both rules then refuse or miss. Check on kind; OnRootMismatch or no fsGroup on the cache volume would avoid it.

Summary

The CUDA kernel (PTX) disk cache refused any directory under a group- or other-writable path component, so on a Kubernetes volume (root-owned 0777, or group-writable through fsGroup) it was always off and every pod start compiled its kernels again.

  • Rule 1, no opt-in. A component only its group can write passes when that group is the gid ensure_persistent_ptx_cache hands to the overlay through a new cxx-bridge setter: the effective gid, only when private_group proves it private, set before the MLX_PTX_CACHE_DIR early return.
  • Rule 2, opt-in. MLXCEL_CACHE_TRUSTED_ROOT=<absolute dir> names an ancestor that is not judged by owner or mode. Every component below it is created 0700 and must be a directory owned by the current user (root does not count) with no group or other write. An unusable value prints mlxcel: MLXCEL_CACHE_TRUSTED_ROOT ignored: <reason> once and the strict walk applies.
  • The POSIX rules moved to ptx_cache_trust.h, which scripts/mlxcelverse/cuda_runtime_dir_test.sh (CI job overlay-host-tests) runs against real directories without a GPU.

Beyond the issue's design

  • Rule 2 is an openat(O_NOFOLLOW) descent over the cache path as given, not flags on the canonical-path walk, which would accept <root>/cuda-ptx after it was replaced by a symlink to another directory the user owns. The cache directory's descriptor is kept, the device subdirectory is opened from it, and entries are read, published, discarded and swept through that descriptor. With the variable set, the Rust side leaves directory creation to the overlay.
  • New refusal reason below a trusted root: <path> cannot be created (<error>).

Open items for the owner

  • Rule 2 residual (documented). Another uid that can write the trusted root, or a directory above it, can rename a sibling directory the user owns into the cache's first component any time before the process first resolves the cache (its first kernel compile or load). Owner checks pass and files there named like cache entries would load. No sound in-code mitigation was found; the layout above removes the practical case.
  • Rule 1 pins nothing (documented, not fixed). A group member the proof cannot see can do the same swap, also while the process runs. Options not taken here: run the pinned descent below the topmost Rule 1 component, or limit Rule 1 to ancestors above the cache directory as fix(security): make the XLA artifact caches private to the user #3248 did.
  • The Windows branch of jit_module.cpp is compiled by no CI job; the new code is POSIX-only and the Windows publish branch was restructured without a behavior change, by reading only.
  • The changes made after review (kept descriptor, descriptor sweep, Rust-side skip) were tested but not reviewed a second time.
  • The ROCm hsaco follow-up issue is not filed (issue creation was blocked in this run).

Test plan

  • macOS: host test script (four deliberate rule breaks fail it), cargo test --release -p mlxcel-core ptx_cache_dirs::, clippy on mlxcel-core, fmt, env-var and native-env checkers.
  • GB10, --features cuda: tests/cuda_ptx_cache_trust.rs under the GPU lock, 30 passed (13 new).
  • GB10, mlxcel-server, Qwen3-4B-4bit, five runs per cell at the final code: second start hits (no JIT notice, no cache file written) under a root-owned 0777 root with the opt-in and a root-owned 2775 root of the private gid, about 2 s against 18 s to first token; main refuses both. Record: docs/benchmark_results/ptx-cache-trust-gb10-2026-11-28.md.
  • Not run: a real Kubernetes pod, a minimal image where Rule 1 fails closed, a Windows build.

Closes #3503

@inureyes inureyes added status:review Under review type:bug Bug fixes, error corrections, or issue resolutions priority:medium Medium priority platform:linux Linux (CUDA / packaging) specific labels Nov 28, 2026
inureyes added a commit that referenced this pull request Nov 28, 2026
Review of #3509 found that ptx_cache_dir() closed the descriptor its descent opened and each device subdirectory descended again from the root by name, so the sibling-rename window lasted until the first JIT of every device key, not until start as documented. The cache directory's descriptor is now kept and the device subdirectory is opened from it.

Also from review: the stale-temporary sweep of a pinned directory goes through its descriptor; with MLXCEL_CACHE_TRUSTED_ROOT set, ensure_persistent_ptx_cache no longer creates or repairs directories by path (which followed a planted symlink) and leaves that to the overlay's no-follow descent; the pinned-entry helpers moved to ptx_cache_trust.h with host tests for prefix matching, nested names and the swap-after-descent case; the host script's needles are unique per call site.

Docs now state the limit of Rule 1 (the private-group proof reads account files, not a pod's fsGroup or supplementalGroups), that directories above the trusted root are unjudged too, and recommend MLXCEL_CACHE_DIR one level below the trusted root so the model store does not sit beside the cache. The unverified fsGroup recursive relabel is listed as not measured.

Refs #3503
@inureyes inureyes added status:done Completed and removed status:review Under review labels Nov 28, 2026
The cache directory walk refused any group- or other-writable path component, so a cache on a Kubernetes volume (root-owned 0777, or group-writable through fsGroup) was always off and every pod start compiled its kernels again. The process cannot chmod a root-owned root.

Rule 1: a component only its group can write passes when that group is the gid handed over by set_ptx_cache_trusted_group. ensure_persistent_ptx_cache passes the effective gid only when private_group proves it private, through the cxx bridge rather than an environment variable, before the MLX_PTX_CACHE_DIR early return.

Rule 2: MLXCEL_CACHE_TRUSTED_ROOT names an ancestor that is not judged by owner or mode. The cache path is split on the path as given, and each component below the root is created 0700, opened with openat(O_NOFOLLOW) from its parent, and must be owned by the current user with no group or other write. The device directory's descriptor is kept and entries are read, published and discarded through it, so a symlink or rename at the first component cannot redirect a process, before or after the check. An unusable value prints one "ignored" line and the strict walk applies.

The POSIX rules moved to ptx_cache_trust.h, which the host test runs against real directories without CUDA. Not covered by Rule 2, and documented: another uid that can write the root can rename a sibling directory of the user into the cache's place before startup.

Validated on macOS with the host test (and four mutations of it) and the ptx_cache_dirs unit tests. The CUDA build and child-process tests run on GB10 next.

Refs #3503
scripts/bench/ptx_cache_trust_ttft.py times mlxcel-server from start to first token over a cold and a warm kernel cache, and refuses to print a table unless the JIT notice and the cache files' inodes and mtimes agree with the arm, so a table always compares a miss with a hit.

The fsGroup-shaped child test compared all mode bits of the created directories with 0700; under a setgid parent they inherit that bit (02700), found on GB10. It now compares the permission bits.

Refs #3503
Both rules of #3503 were run with mlxcel-server and Qwen3-4B-4bit on directories shaped like volume roots (root-owned 0777 with the opt-in, root-owned 2775 of the user's private group without it), five runs per cell under the GPU lock. The second start hit the cache in every run by the JIT notice and by unchanged cache files, reaching the first token in about 2 s against about 19 s cold. main, and the branch without the opt-in, refused the same directories and compiled on every start. The record names what was not run: a real Kubernetes pod, a minimal image, a quiet host.

Refs #3503
Review of #3509 found that ptx_cache_dir() closed the descriptor its descent opened and each device subdirectory descended again from the root by name, so the sibling-rename window lasted until the first JIT of every device key, not until start as documented. The cache directory's descriptor is now kept and the device subdirectory is opened from it.

Also from review: the stale-temporary sweep of a pinned directory goes through its descriptor; with MLXCEL_CACHE_TRUSTED_ROOT set, ensure_persistent_ptx_cache no longer creates or repairs directories by path (which followed a planted symlink) and leaves that to the overlay's no-follow descent; the pinned-entry helpers moved to ptx_cache_trust.h with host tests for prefix matching, nested names and the swap-after-descent case; the host script's needles are unique per call site.

Docs now state the limit of Rule 1 (the private-group proof reads account files, not a pod's fsGroup or supplementalGroups), that directories above the trusted root are unjudged too, and recommend MLXCEL_CACHE_DIR one level below the trusted root so the model store does not sit beside the cache. The unverified fsGroup recursive relabel is listed as not measured.

Refs #3503
After the review fixes the 29 CUDA child tests pass again on GB10, and a two-run cold and warm pass per arm still shows a second-start hit under the opt-in, under Rule 1, and with MLXCEL_CACHE_DIR one level below the trusted root (the layout the docs recommend for a root that may be shared).

Refs #3503
Under MLXCEL_CACHE_TRUSTED_ROOT the sweep lists and deletes through the device directory's descriptor, a branch the existing sweep test (no trusted root) never takes and the host test cannot reach because it lives in jit_module.cpp.

Refs #3503
The five-run table was re-measured on GB10 with the binary built after the review changes (2a62154): warm 2.10 s and 1.99 s to first token against 17.98 s and 17.96 s cold under the opt-in and under Rule 1, with main and the branch without the opt-in refusing the same directories. The record also notes the 30-scenario child test run and the two-run pass of the layout with mlxcel one level below the trusted root.

Refs #3503
@inureyes
inureyes force-pushed the fix/issue-3503-ptx-cache-trusted-root branch from 26d1a1e to eebb51c Compare November 28, 2026 12:16
@inureyes
inureyes marked this pull request as ready for review November 28, 2026 12:16
@inureyes
inureyes merged commit ec87f9d into main Nov 28, 2026
1 check passed
@inureyes
inureyes deleted the fix/issue-3503-ptx-cache-trusted-root branch November 28, 2026 12:16
inureyes pushed a commit that referenced this pull request Nov 28, 2026
…reads (#3496)

The CUDA `custom_kernel.cpp` overlay now charges the fused decode-MoE gate/up and down kernels for the expert slices their index names instead of the whole expert stacks, so MLX no longer commits the captured graph in every MoE layer on sm_121. A single-request decode step launches 62 graphs instead of 147 on Qwen3-30B-A3B 4-bit and 30 instead of 48 on LFM2-8B-A1B 4-bit. A stack is still charged whole when the index names every expert or the input layout is unrecognised. `scripts/nsys_cuda_graph_launches.py` counts graphs per decode step from an nsys trace.

Hardening review: merged main locally (conflicts with #3509 kept both sides; the CHANGELOG entry moved out of the tagged v0.9.0-beta.3 section into `[Unreleased]`), checked that the audited kernels read only device buffers indexed below K, ran fmt, clippy and the CUDA graph, fused MoE, budget and paged suites, a revert experiment (the graph-commit test fails with main's overlay), and real-model checks where Qwen3-30B-A3B and LFM2-8B-A1B greedy text is byte-identical before and after.

Closes #3387
inureyes added a commit that referenced this pull request Nov 28, 2026
)

## Summary

The ROCm kernel (hsaco) disk cache refused any directory under a group- or other-writable component, so on a Kubernetes volume root (root-owned `0777`, or group-writable through `fsGroup`) it was always off and every pod start compiled its kernels again. This applies the two rules of #3503 (merged in #3509) to it, POSIX only.

- **Rule 1, no opt-in.** A component that only its group can write passes when that group is the gid handed over through a new cxx-bridge setter, `set_hsaco_cache_trusted_group`. `mlxcel_core::ensure_hsaco_cache_trust()` calls it at startup of `mlxcel` and `mlxcel-server` with the effective gid, only when `private_group` proves it private. The ROCm header resolves its directory itself, so this is a separate startup call next to `ensure_persistent_ptx_cache`.
- **Rule 2, opt-in.** `MLXCEL_CACHE_TRUSTED_ROOT` is split on the cache path as given, before anything is resolved, created or repaired. Every component below the root is opened with `openat(O_NOFOLLOW)` and created `0700` if missing. Each must be owned by the current user (root does not count) with no group or other write. The directory above `<arch>` is opened once per process and kept, so a second GPU architecture is opened from it instead of descending from the root again. The `<arch>` descriptor is pinned, and entries are read, published (`openat`, `renameat`, `unlinkat`) and nested directories created through it. An unusable value prints `mlxcel: MLXCEL_CACHE_TRUSTED_ROOT ignored: <reason>` once and the walk applies.
- The descent lives in the new `hsaco_cache_dir_trust.h`, a copy of the CUDA `ptx_cache_trust.h` adapted to this cache. It is not shared because a ROCm build never sees the CUDA overlay, and only `overlays/core` (upstream-provenance files) reaches every build. CUDA code is untouched. The Windows branch keeps its signatures and is unchanged.
- The documented CUDA limits carry over: the sibling-rename residual before the first resolve, and Rule 1's proof not seeing pod-spec groups. One ROCm-specific case is documented too: with `MLX_HSACO_CACHE_DIR` equal to the trusted root, each `<arch>` sits directly in the root, so that window stays open until each architecture is first used.

## For the owner

- `docs/installation.md` was edited beyond the two `environment-variables.md` rows the issue names. The ROCm "Kernel disk cache" section said a group-writable explicit directory is always refused, which is no longer true. One paragraph was added there, which may touch the same file as #3484.
- The halo volume roots were owned by the test user, not by root: no sudo, docker or `newuidmap` on the host. Rule 2 never judges the root's owner, and the walk treats root and the current user alike, so the mode is what decides. The root-owned cases (`02775` with the trusted gid, a root-owned component below the trusted root) are covered by the host test.

## Changes during review

- `ensure_hsaco_cache_trust` keeps its `rocm` check inside the Linux block, so a macOS clippy run does not reject the body as `needless_return` (found by review, checked with a standalone crate; the halo clippy run predates this reshape).
- Security review found nothing CRITICAL or HIGH; its four LOW notes are in the PR comment and are unreachable by another uid today.

## Test plan

- [x] `scripts/mlxcelverse/rocm_hsaco_cache_test.sh` (macOS clang and Debian gcc 14, `-Werror`). New `rocm_hsaco_cache_test_trust.inc` covers both rules: a symlink planted at the first component below the root, a root-owned component below it, entries read and published through the pinned descriptor after the path is swapped, a second architecture from the kept parent, and a root that is the direct parent of `<arch>`. Eight deliberate rule breaks each fail it.
- [x] `scripts/mlxcelverse/cuda_runtime_dir_test.sh`, `make verify-rocm-overlay`, env-var and native-env checkers, `cargo clippy --release --features rocm -p mlxcel-core --lib -- -D warnings` on halo.
- [x] halo (gfx1151), `mlxcel-server` with Qwen3-4B-4bit, five runs per cell. On the second start, under a `0777` root with the opt-in and under a `2775` root of the private gid, the server read the three published cache files and changed none. `main` and the branch without the opt-in refused with one line and cached nothing. `mlxcel generate` was checked the same way. Timing showed no difference because this model JIT-compiles one module. Record: `docs/benchmark_results/hsaco-cache-trust-gfx1151-2026-11-29.md`.
- [ ] Not run: a Windows build or the Windows host test, a CUDA or Metal build of the bridge and `lib.rs` change, a multi-architecture GPU host, and a real Kubernetes pod.

Closes #3527
inureyes added a commit that referenced this pull request Nov 28, 2026
Rewrite the GB10 record for a build of main at 15e6395 (includes #3508, #3509 and #3540). A cold-cache Qwen3-4B-4bit generation inside the runtime image under strace opened 11 CUDA toolkit headers, all in the allow-list, and succeeded; the server smoke (uid 65532, chat completion, CLI generate, docker stop drain, PTX cache reuse on a named volume, a root:root 0777 bind mount and a private-group 2775 bind mount) passed again. The record also states the license basis, the amd64 cross builds on Ubuntu 26.04 with CUDA 13.5, and what was not run.

Refs #3484
inureyes added a commit that referenced this pull request Nov 28, 2026
…3543)

## Summary

Adds `docker/Dockerfile.cuda`: a shell-less chiseled Ubuntu `runtime` image and a `debug` image (the whole `nvidia/cuda` `cudnn-devel` image plus mlxcel) for `linux/amd64` and `linux/arm64`, running as uid 65532 with `/models` and `/cache` volumes. The previous attempt stopped at the CUDA header license gate; #3540 removed the non-redistributable includes, so the runtime image now ships.

## What changed

- The runtime image copies exactly `scripts/ci/nvrtc_header_allowlist.txt` into `/usr/local/cuda/include` (`docker/copy-cuda-headers.sh`, no second list). The build fails if a listed header is missing or any other file is there. `docker/copy-cuda-headers_test.sh` covers the guard and runs in CI.
- CUDA and cuDNN files come from NVIDIA's CUDA apt repository. The signing key fingerprints are pinned, and the packages are unpacked without installing. Nothing is copied out of an `nvidia/cuda` image, because the NGC container license allows a derived image only when it includes the whole container (1.c, 4.c). Package versions go to `/opt/mlxcel/share/cuda-packages.txt`.
- Libraries come from a `readelf` closure plus a measured dlopen set, with a glibc/libstdc++ version guard.
- amd64 defaults to Ubuntu 26.04 with CUDA 13.5, because the x86_64 release runner builds with CUDA 13.5 and its binaries need `GLIBC_2.43`. arm64 stays on Ubuntu 24.04 with CUDA 13.0.
- `NOTICE` records the CUDA EULA and cuDNN distribution basis and adds the bundled CCCL (Apache-2.0 with LLVM exceptions) and CUTLASS/CuTe (BSD-3-Clause) headers. `README.md` and `docs/installation.md` describe the image.
- The `/health` wording is corrected, not the server: with `-m` the listener binds only after load and warmup, so probes are refused until the server returns 200. The image has no `HEALTHCHECK`.
- Security review: `HOME=/cache` means `-m owner/name` reads `/cache/.cache/huggingface/hub` before `/models` without an owner check, so a `/cache` other host uids can write lets them substitute a model. The docs now recommend a 65532-owned or private-group bind; a resolver fix is left for a follow-up.

## Test plan

GB10, local build of `main` at `15e63955` (#3508, #3509, #3540), arm64 runtime image, all GPU runs under `gpu-lock`.

- [x] Cold-cache `mlxcel generate` under strace inside the runtime image: exit 0, 0 to 12 PTX files. It opened 11 toolkit headers, all allow-listed, and no header outside the toolkit and bundled include roots.
- [x] Server smoke as 65532, named volume, `root:root 0777` bind with the trusted root, and `65532:65532 2775` bind with the trusted root unset. In every case the chat completion returned text, `docker stop` took under 1 s with exit 0, and the second start compiled nothing and left the cache identical. CLI `generate` worked and `--entrypoint sh` failed.
- [x] The literal `--platform linux/amd64,linux/arm64` build from v0.8.1, cache-only, for `runtime` and `debug`; amd64 runtime also from v0.9.0-beta.3 (glibc 2.43 rootfs).
- [x] The GPU runs used the image before the review follow-up `3ab35c1f` (copyright files, build-step hardening); that version was only rebuilt, not run.
- [x] `docker/copy-cuda-headers_test.sh` (5 cases, including a planted `math_constants.h` and a symlink).
- [ ] Not run: any amd64 container or x86_64 GPU (including NVRTC 13.5), release binaries at run time (no release has #3508/#3509/#3540 yet), either debug image at run time, kernels Qwen3 does not reach. Record: `docs/benchmark_results/docker-cuda-smoke-gb10-2026-11-29.md`.

Owner review requested: the license basis in `NOTICE` (CUDA EULA Attachment A and 1.1.2, cuDNN supplement, NGC container license 1.c/4.c/7) is my reading of those documents, not legal advice.

Closes #3484
inureyes added a commit that referenced this pull request Nov 30, 2026
Bilingual reports for the eight PRs of epic #3488: #3508 (graceful drain), #3509 (CUDA PTX cache trust), #3530 (ROCm hsaco cache trust), #3540 (NVRTC redistributable headers), #3543 (CUDA container image), #3549 (Helm chart), #3550 (GHCR image publishing) and #3554 (chart OCI publishing and docs).

Each report names what the PR record leaves unvalidated: a real GHCR push, a GPU pod on a cluster, the amd64 and debug images. The HuggingFace cache follow-up is #3570.
@inureyes inureyes added this to the 0.9 milestone Nov 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

platform:linux Linux (CUDA / packaging) specific priority:medium Medium priority status:done Completed type:bug Bug fixes, error corrections, or issue resolutions

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix(cuda): let the PTX disk cache trust a container volume root

1 participant