Skip to content

fix(rocm): let the hsaco disk cache trust a container volume root - #3530

Merged
inureyes merged 5 commits into
mainfrom
fix/issue-3527-rocm-hsaco-trusted-root
Nov 28, 2026
Merged

inureyes merged 5 commits into
mainfrom
fix/issue-3527-rocm-hsaco-trusted-root

Conversation

@inureyes

@inureyes inureyes commented Nov 28, 2026 •

Copy link
Copy Markdown
Member

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 feat(docker): Add CUDA container image with mlxcel and mlxcel-server #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

  • 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.
  • 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.
  • 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 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

Copy link
Copy Markdown
Member Author

Implementation Review Summary

Intent

The ROCm hsaco kernel disk cache applies the two trust rules of #3503 (Rule 1 private group, Rule 2 MLXCEL_CACHE_TRUSTED_ROOT descent) so a cache under a container volume root can be hit.

Findings Addressed

  • ensure_hsaco_cache_trust reduced to a lone if !cfg!(feature = "rocm") { return; } off Linux, which clippy::needless_return rejects under -D warnings (reproduced in a standalone crate on macOS). PR CI clippy is Linux-only, so this would first break nightly macOS make verify. Fixed in 6b348e8 with no behavior change. (HIGH)

Remaining Items

  • With MLX_HSACO_CACHE_DIR equal to the trusted root, each <arch> is a first component directly in the unjudged root, so the sibling-rename residual reopens at every new architecture's first resolve, not only at the process's first cache resolve as the header and env-var row say. Doc precision only; the recommended layout (MLXCEL_CACHE_DIR=<root>/mlxcel) is not affected. (LOW)
  • resolve_hsaco_trusted_root omits the CUDA comment that .. in the cache path is normalized lexically, not through symlinks as the walk does. Behavior matches CUDA. (LOW)

Verification

  • All stated requirements implemented
  • No placeholder/mock code remaining
  • Integrated into project code flow
  • Project conventions followed
  • Existing modules reused where applicable
  • No unintended structural changes
  • Tests pass

Notes: the descent, root split and entry-ref code is a faithful copy of ptx_cache_trust.h (body diff). Every entry operation in the ROCm jit_module.cpp (read, nested mkdir, publish) goes through the pinned descriptor; ROCm has no discard or sweep path. Call sites match ensure_persistent_ptx_cache in both binaries. Windows: the trust header and its callers are all under !_WIN32, the Windows resolve_hsaco_cache_dir signature is unchanged, and the shared CacheDirComponent initializers were updated; the Windows host-test job passes, and the Windows compile-only job was still pending at review time. Locally: rocm_hsaco_cache_test.sh, cuda_runtime_dir_test.sh, make verify-rocm-overlay and check_env_var_docs.py pass, and three mutations (dropping the descent's uid check, dropping O_NOFOLLOW, routing entries by path) each fail the host test. caa962de after the GPU-validated 51ed364d touched only tests and the record.

@inureyes

Copy link
Copy Markdown
Member Author

Security and performance review

No CRITICAL or HIGH findings, so no commits from this pass.

Checked: the trusted-root split on the path as given (shortest matching prefix), the openat(O_NOFOLLOW) descent with checks on the descriptor, the pinned parent and <arch> descriptors (never closed, so never reused), entry read, publish and nested mkdir through hsaco_cache_entry_ref, Rule 1 in cache_component_refusal, and the gid handover. Only in-process code can call the setter, it is read under the hsaco_cache_dir() mutex, and it defaults to -1. read_cached_hsaco and write_cached_hsaco return early on an empty directory, so a refused cache never falls back to relative paths. Nothing in Rust creates anything under hsaco/ by path. The test .inc swaps the first component and <arch> after the pin and asserts that I/O stays on the checked tree.

LOW (no fix, none reachable by another uid today):

  • hsaco_cache_entry_ref fails open: when the lexical prefix does not match, it falls back to AT_FDCWD by-path I/O. A fail-closed variant would refuse by-path I/O while a trusted root is in force.
  • Names are not validated: safe_filename only truncates, and arch reaches open_hsaco_cache_components as a path. A .. or / in either would resolve relative to the pinned descriptor. Both come from in-process sources (kernel names, gcnArchName).
  • A one-off resolve_hsaco_cache_dir (no parent) still adds its <arch> descriptor to the process-wide pin list. Only tests call it that way.
  • Someone who can write an ancestor of the as-given cache path can make a prefix canonicalize to the trusted root. The descent then applies where the walk would refuse. It still needs private directories owned by the victim, so it is the same class as the accepted sibling-rename residual.

Verification: scripts/mlxcelverse/rocm_hsaco_cache_test.sh and make verify-rocm-overlay pass on macOS. The Linux errno paths were read, not run. No ROCm GPU was available and no cargo build was run; the Rust handover was reviewed by inspection.

@inureyes inureyes added status:done Completed and removed status:review Under review labels Nov 28, 2026
The ROCm kernel 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 recompiled its kernels. This applies the two rules of #3503 to it.

- Rule 1: a component only its group can write passes when that group is the gid handed over by the new set_hsaco_cache_trusted_group bridge setter, which ensure_hsaco_cache_trust calls at startup of mlxcel and mlxcel-server with the effective gid only when private_group proves it private.
- Rule 2: MLXCEL_CACHE_TRUSTED_ROOT is split on the cache path as given; the new hsaco_cache_dir_trust.h (a copy of the CUDA ptx_cache_trust.h, since a ROCm build never sees the CUDA overlay) descends below it with openat(O_NOFOLLOW), keeps the directory above <arch> so later architectures open from it, and pins the <arch> descriptor so entries are read and published, and nested directories created, through it.

The sibling-rename residual and Rule 1's blindness to pod-spec groups carry over and are documented. The host test gains rocm_hsaco_cache_test_trust.inc for both rules, and the TTFT harness takes --backend rocm.

Refs #3527
Five runs per cell of mlxcel-server with Qwen3-4B-4bit on Strix Halo: both rules of #3527 turn a refused ROCm kernel cache into a second-start hit, shown by the cache files (published on every cold run, untouched on every warm run), and main refuses the same directories. The model JIT-compiles one module there, so no time saving was measurable. Root-owned roots could not be made on the host (no sudo, docker or newuidmap).

Refs #3527
With MLX_HSACO_CACHE_DIR equal to MLXCEL_CACHE_TRUSTED_ROOT the kept parent is the root's own descriptor. The host test now checks that entries are read and published through the pinned <arch> after it is swapped, and that a second architecture opens from the root. The benchmark record states what was observed (one published module) and adds the mlxcel generate start-up check.

Refs #3527
Off Linux the cfg-gated block drops out and the function body was only `if !cfg!(feature = "rocm") { return; }`, which clippy's needless_return rejects under `-D warnings`. The PR's clippy job runs on Linux only, so this would first fail in the nightly macOS `make verify` and for anyone running the documented lint locally on Apple Silicon.

The feature check now sits inside the Linux block, so behavior is unchanged on every platform. Checked with a standalone crate of the same shape: the old form fails `cargo clippy -- -D warnings` on macOS, the new form passes with and without the feature, and with the gate emulated as `unix`.

Refs #3527
With MLX_HSACO_CACHE_DIR equal to MLXCEL_CACHE_TRUSTED_ROOT each <arch> sits directly in the root, so the sibling-rename window reopens for every architecture until it is first resolved. The header and the env-var row say so, and the header notes that the cache path is normalized lexically, as in the CUDA copy. Raised as LOW in review.

Refs #3527
@inureyes
inureyes force-pushed the fix/issue-3527-rocm-hsaco-trusted-root branch from d887133 to 2feeca7 Compare November 28, 2026 19:32
@inureyes
inureyes marked this pull request as ready for review November 28, 2026 19:32
@inureyes
inureyes merged commit 76599fc into main Nov 28, 2026
15 checks passed
@inureyes
inureyes deleted the fix/issue-3527-rocm-hsaco-trusted-root branch November 28, 2026 19:32
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(rocm): let the hsaco disk cache trust a container volume root

1 participant