Skip to content

accel/ane: h16: check admission before importing the HWX and account the copy - #3194

Merged
RyanTheTide merged 4 commits into
mainfrom
fix/ane-h16-load-admission
Nov 28, 2026
Merged

RyanTheTide merged 4 commits into
mainfrom
fix/ane-h16-load-admission

Conversation

@DjDeveloperr

@DjDeveloperr DjDeveloperr commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

What: ANE_H16_LOAD now checks whether the file can load before it reads anything from userspace, and the kernel's copies of the HWX and the four sections are charged to the caller's memory cgroup. Before, a LOAD copied, validated and hashed the whole HWX (up to 257 MiB) first, and only then failed with -EBUSY if the file already had a program. Any number of LOADs could do that at once on one file, and none of that memory was charged to the caller.
Tested: J713 (M4, H16): MobileNet-v2 16/16 before and after s2idle, and h16-load-admission.py 5/5: the LOAD charge shows in the cgroup, a second LOAD with faulting pointers gets EBUSY (stock gives EFAULT), and the charge is returned on close; 0 ANE errors. J714s (M5, H17): add-128 16/16 exact, the same EBUSY-vs-EFAULT result, and the ANE resumes after deep sleep with 0 ANE errors or warnings. Both rows ran at af175d2. Its kernel commit is identical to this head's; only the selftest has changed since. J714s then ran this head's selftest on the same kernel and passed: 64.0 MiB charged and EBUSY. Stock fails it: nothing charged, and EFAULT. CI KUnit ane-boot passed 282/282 at af175d2, including the three new cases. checkpatch --strict is clean, and this head merges cleanly with #2876 (git merge-tree).
Risk: two-way door · every Mac whose ANE uses the H16 backend: the M3 family (H15), the M4 family (H16) and the M5 family (H17)
Not yet: nothing

Details

LOAD now runs in three steps:

  1. Admission (ane_h16_load_admit()), under the file lock and the node lock as before. A file that has a program from this firmware boot, or that has another LOAD in flight, fails with -EBUSY. An O_NONBLOCK LOAD during system sleep fails with -EAGAIN. A program lost across system sleep is released here, before the copy instead of after it. An admitted LOAD marks the file loading.
  2. Copy. The sections and the HWX are read with no lock of ours held, as before. mmap() of the node takes the file and node locks under the process mmap lock, so holding the file lock across a user fault would invert that order.
  3. Load. The program is created under both locks as before, and loading is cleared on every path.

The copies are now charged to the caller's memory cgroup:

  • kvmalloc(GFP_USER | __GFP_ACCOUNT | __GFP_NOWARN) plus copy_from_user() replaces vmemdup_user() for the HWX.
  • kmalloc(GFP_USER | __GFP_ACCOUNT | __GFP_NOWARN) plus copy_from_user() replaces memdup_user() for the sections.

These are the flags those helpers use, plus __GFP_ACCOUNT. A loaded program keeps its HWX snapshot, so the charge stays with the cgroup that loaded it for the program's lifetime.

The size cap stays at 256 MiB plus 1 MiB (ANE_H16_HWX_MAX). The validator accepts a single segment of up to 256 MiB, and the cap is one such segment plus room for the load commands. A lower cap would refuse containers the validator accepts, and we have no measured model sizes to pick a lower value from.

There is no device-wide cap on imports in flight. With one, a single client that stalls its imports would block every other client's LOAD. Instead, each import is charged to its caller's cgroup, and each file has at most one in flight.

What userspace can see: only which error comes back from a LOAD that fails anyway. A LOAD on a busy file now returns -EBUSY even when its pointers are bad, where it used to return -EFAULT or -EINVAL. The documented behavior, -EBUSY for a second LOAD, is unchanged.

How this composes with #2876: #2876 changes EXECUTE (it pins buffers before locking) and the validator. This PR changes the order of LOAD and its copy helpers. The hunks are separate, and git merge-tree of this head with #2876's head cd4666bbd23e is clean. In the merged tree, both LOAD and EXECUTE read user memory with no lock of ours held. Either PR can merge first.

New KUnit cases:

  • ane-h16-accel / ane_h16_load_admission_test:
    • a file with a program, or with a LOAD in flight, fails with -EBUSY even when every pointer faults (the old order failed with -EFAULT);
    • an admitted LOAD that faults leaves the file able to load again and holds no lock;
    • O_NONBLOCK during sleep gives -EAGAIN, and nonzero flags give -EINVAL.
  • ane-h16-accel / ane_h16_load_size_test:
    • an HWX of ANE_H16_HWX_MAX + 1 bytes and one of 31 bytes fail with -EINVAL after the sections are read;
    • a page within the bounds is read, refused by the validator (-ENOEXEC) and freed.
  • ane-h16-hwx / hwx_import_user_test:
    • an import from user memory equals the original bytes and passes the digest check;
    • a copy that faults part way keeps nothing.

New selftest, tools/testing/selftests/drivers/ane/h16-load-admission.py (H16G or H17). Run it in its own cgroup (systemd-run --scope -p MemoryAccounting=yes). On one open file of the node, it checks that:

  1. an oversized LOAD fails with EINVAL;
  2. a 64 MiB LOAD of zero bytes fails validation, and the cgroup's memory.peak rose by at least 48 MiB while the kernel held its copy;
  3. a valid LOAD succeeds;
  4. a second LOAD whose pointers all fault fails with EBUSY, not EFAULT;
  5. a second 64 MiB LOAD fails with EBUSY, and the peak rises by less than 16 MiB.

The 64 MiB come from a private mapping that was never written. Userspace reads them from the zero page, so only the kernel's copy is charged. A small HWX can't show the charge, because memory cgroups batch charges per CPU.

Thanks to erikwb for the report and the analysis.

…the copy

H16_LOAD copied its four sections and the whole HWX, up to
ANE_H16_HWX_MAX, before it checked whether the file could load at all.
A LOAD on a file that already had a program allocated, copied, validated
and hashed a full HWX only to fail with -EBUSY afterwards, any number of
LOADs could do that at once on one file, and none of that memory was
charged to the caller.

Admit the LOAD first, under the file and node locks. A file that has a
program, or another LOAD in flight, now fails with -EBUSY before anything
is read, and a program lost across system sleep is released there, as
before. The admitted LOAD marks the file loading, then reads its sections
and HWX with no lock of ours held, as before, so a file has at most one
import in flight.

Charge the copies to the caller's memory cgroup: kvmalloc() with
__GFP_ACCOUNT and copy_from_user() replace vmemdup_user() for the HWX,
and kmalloc() with __GFP_ACCOUNT replaces memdup_user() for the sections.
A loaded program's HWX snapshot stays charged to the cgroup that loaded
it for the program's lifetime.

ANE_H16_HWX_MAX stays at 256 MiB plus 1 MiB: the validator accepts one
segment of up to 256 MiB, and the cap is that segment plus room for the
load commands.

Signed-off-by: Dj <[email protected]>
@DjDeveloperr
DjDeveloperr requested review from a team as code owners November 26, 2026 05:49
@github-actions github-actions Bot added area:media Area from CODEOWNERS (bot) blast:all Macs affected, from Merge risk (bot) door:two-way Revertible or not, from Merge risk (bot) needs-hw-row No hardware row at the PR head (bot) needs-review No approving review from Core or Omarchy Mac yet (bot) labels Nov 26, 2026
@github-actions

github-actions Bot commented Nov 26, 2026 •

Copy link
Copy Markdown

Merge readiness at c2addc86: not ready yet

Check Detail
CI at head running running: KUnit scaler, KUnit macsmc, KUnit clpc, KUnit trusted-keys
Review no needs one approving review from Core or Omarchy Mac (not the author)
Requested changes ok none
Hardware row at head no no line with the head SHA c2addc86, a Mac and a result in the PR body or a comment
Conflicts ok none
PR text ok Risk: two-way · every Mac whose ANE uses the H16 backend: the M3 family (H15), the M4 family (H16) and the M5 family (H17)

Areas: area:media

Updated by pr-readiness on pushes, edits, reviews and comments, and on a schedule. Rules: merge policy · labels.

h16-load-admission.py loads one H16G or H17 HWX on one open file of the
accel node and checks that an oversized LOAD fails with EINVAL; that the
valid LOAD grows the caller's cgroup kernel memory by at least the HWX
size; and that a second LOAD fails with EBUSY, both one whose pointers
all fault, so admission comes before any copy, and a valid one, which
leaves kernel memory unchanged.

Run it in a cgroup of its own (systemd-run --scope) so the kernel-memory
numbers are the test's only.

Signed-off-by: Dj <[email protected]>
@github-actions github-actions Bot added the ci-failing A check failed at the PR head (bot) label Nov 26, 2026
@DjDeveloperr

Copy link
Copy Markdown
Contributor Author

Verdict at af175d28ad3e: ready

Blockers: none

Checked:

  • Admission comes first. ane_h16_load_admit() takes the file lock and enters the session, which takes the node lock, before any user memory is read. A file with a program, or with loading set, gets -EBUSY. A program lost across sleep is released there, as before.
  • Every exit path is balanced.
    • A refused admission drops only the file lock: session_enter() failed or was never called, so it holds no node lock.
    • After the copy, every path clears loading under the file lock. On a copy fault, session-enter failure, load failure or success, the node lock is dropped exactly once. The KUnit case checks that neither lock is left held after a fault.
  • Nothing else races with an in-flight LOAD. f->program is set only by LOAD, and EXECUTE reads it under the file lock, so it sees NULL and returns -EINVAL. Postclose cannot run while an ioctl holds the file.
  • The copies are charged to the caller.
    • The sections use kmalloc(GFP_USER | __GFP_ACCOUNT | __GFP_NOWARN), and aux[kind] is set before copy_from_user(), so a fault frees it through the caller's loop.
    • The HWX uses kvmalloc() with the same flags and frees itself on a fault. The vmalloc fallback honours __GFP_ACCOUNT.
    • The size bound is checked before the allocation, as before.
  • It merges with accel/ane: check H16/H17 DMA bases and LOAD sections against the program, and pin EXECUTE buffers before locking #2876. I merged accel/ane: check H16/H17 DMA bases and LOAD sections against the program, and pin EXECUTE buffers before locking #2876's head cd4666bbd23e into this head locally. The merge is clean, and accel/ane: check H16/H17 DMA bases and LOAD sections against the program, and pin EXECUTE buffers before locking #2876's EXECUTE still reads f->program only under the file lock. The merged tree builds and passes the ANE KUnit run (below).
  • Merged-code rules: no runtime switch and no SoC or board gate. ANE_H16_HWX_MAX is unchanged, and the PR explains its basis.
  • Not blocking: across many open files, the in-flight copies are bounded by the caller's memory cgroup, not by a device-wide cap. The PR explains why it chose that.
Evidence
  • Build at af175d28ad3e with the asahi profile (LLVM): drivers/accel/ane/, exit 0, no warnings.
  • The same build on the local merge of this head with accel/ane: check H16/H17 DMA bases and LOAD sections against the program, and pin EXECUTE buffers before locking #2876 (cd4666bbd23e): exit 0.
  • KUnit drivers/accel/ane/boot.kunitconfig (arm64 QEMU, KASAN and UBSAN) on that merged tree: 297/297, including ane_h16_load_admission_test, ane_h16_load_size_test and hwx_import_user_test. No KASAN or UBSAN report.
  • CI KUnit ane-boot at this head alone: 282/282.
  • The J714s and M4 MacBook Air rows are still listed under "Not yet".

@RyanTheTide

Copy link
Copy Markdown
Contributor

af175d2 J713 (M4 Air, T8132/H16, batched 0-diff merge onto main) PASS: ANE bound (/dev/accel/accel0, H16 T8132 27.2 26B123 J713 accepted), MobileNet-v2 top-1 16/16 PASS pre-sleep (p50 0.332 ms vs 0.322 ms fleet85) and post-s2idle 1/1 (p50 0.334 ms); h16-load-admission.py on MobileNet HWX (8.58 MiB) PASS 5/5 (EINVAL oversized, +16 MiB cgroup charge on LOAD, EBUSY on faulting 2nd LOAD vs EFAULT on fleet85, EBUSY/+0 on valid 2nd LOAD, -16 MiB on close; 48 KiB add128.hwx passes admission EBUSY/EINVAL but sits below 16K-page per-CPU memcg batch), 0 ANE errors, Enforcing, 0 failed; fleet85 restored.

@DjDeveloperr

DjDeveloperr commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor Author

J714s (MacBook Pro 14" M5 Pro, G17S B1, H17 ANE) row at 01652ea7421f: PASS. (Edited: the selftest from 01652ea7421f replaces the earlier af175d28ad3e run.)

The charge check compared the cgroup's kernel memory before and after a
valid LOAD. With a small HWX such as add-128 (48 KiB) that reads +0 on
every kernel: memory cgroups batch charges per CPU, and memory.stat is
flushed lazily.

Measure a 64 MiB LOAD of zero bytes instead, read from a private
anonymous mapping that was never written, so that only the kernel's
copy is charged. The import fails validation, and the cgroup's
memory.peak, reset for the test's file descriptor, shows whether the
copy was charged while the kernel held it. A second such LOAD on the
busy file must fail with EBUSY without raising the peak, since
admission now comes before the copy.

Signed-off-by: DjDeveloperr <[email protected]>

@aurora-silicon-review aurora-silicon-review Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verdict: MERGE at c2addc86 (once cancelled KUnit CI checks are rerun green)

Blockers: none (code and two-generation hardware rows are ready; rerun the 12 cancelled KUnit checks before merge).

Checked:

  • drivers/accel/ane/ane_h16_accel.c:39-185: verified ane_h16_load_admit() checks f->loading and f->program (releasing sleep-lost programs) under f->lock and a->lock, sets f->loading = true, and drops both locks before copying user memory; verified every error/success path resets f->loading = false under f->lock with balanced locking and cleanup of snapshot/aux.
  • drivers/accel/ane/ane_h16_accel.c:22 and ane_h16_hwx.c:304: verified both section copies (kmalloc) and HWX snapshots (kvmalloc) charge the caller's memory cgroup via GFP_USER | __GFP_ACCOUNT | __GFP_NOWARN and free cleanly on copy_from_user fault.
  • ane_h16_accel_test.c, ane_h16_hwx_test.c, and h16-load-admission.py: verified KUnit and selftest coverage.
  • Verified hardware rows on both J713 (M4/H16, MobileNet-v2 16/16 + post-s2idle + selftest 5/5) and J714s (M5/H17, add-128 16/16 + post-sleep + selftest 5/5).
Evidence
  • Diff reviewed at c2addc86 (all 6 files, 622 lines).
  • CI at review time: 7 passed, 0 failed, 12 cancelled.

@RyanTheTide

Copy link
Copy Markdown
Contributor

Merge-train status at c2addc86 (App approval noted; rows: 12 cancelled KUnit; J713 + J714s PASS). @DjDeveloperr code-owner ping (6-point rule, point 6): merge earliest 17:15 AEDT once full CI is green (I re-ran cancelled jobs) and the second-family row point holds. Revert: git revert -m 1 <merge>.

@github-actions github-actions Bot removed the ci-failing A check failed at the PR head (bot) label Nov 28, 2026
@RyanTheTide

Copy link
Copy Markdown
Contributor

Merging under the 6-point rule (Ryan 09:29, permanent), at c2addc86. (1) The review App approved at this head. (2) Full CI is green. (3/4) Rows: J713+J714s. (5) There's no blocking review. (6) Dj was pinged here 463 min ago with no objection. Revert: git revert -m 1 <merge>.

@RyanTheTide
RyanTheTide merged commit 35bc137 into main Nov 28, 2026
37 of 101 checks passed
@RyanTheTide
RyanTheTide deleted the fix/ane-h16-load-admission branch November 28, 2026 12:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:media Area from CODEOWNERS (bot) blast:all Macs affected, from Merge risk (bot) door:two-way Revertible or not, from Merge risk (bot) needs-hw-row No hardware row at the PR head (bot) needs-review No approving review from Core or Omarchy Mac yet (bot)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants