Skip to content

accel/ane: check H16/H17 DMA bases and LOAD sections against the program, and pin EXECUTE buffers before locking - #2876

Open
funkzy wants to merge 3 commits into
aurora-silicon:mainfrom
funkzy:pr/ane-h16h17-checks
Open

funkzy wants to merge 3 commits into
aurora-silicon:mainfrom
funkzy:pr/ane-h16h17-checks

Conversation

@funkzy

@funkzy funkzy commented Nov 22, 2026 •

Copy link
Copy Markdown

Problem

Three gaps in the H16/H17 LOAD and EXECUTE paths:

  • DMA bases. The task-stream validator checks a program's framing, that each record names a register in an allowed block, the BAR table, and the operation section's slot bindings. It does not check the values of the DMA base records: their offset, their slot, whether that slot is bound, or whether the record is relocated at all.
  • LOAD sections. The procedure and text-properties sections go to the firmware after a size check only. The text properties carry the task stream's framing (segment offset, size and task count), which the validator derives on its own, so other values describe a stream it did not check. Reserved bytes in the generic and operation sections are not checked either.
  • EXECUTE copies. Without DRM_ANE_H16_EXEC_IN_PLACE, each input is copied from user memory, and each output back, with the node lock held. A page fault there waits for whatever backs the page, and every other client's LOAD, EXECUTE, mmap and close waits behind it, as does the PM notifier's drain at suspend. The fault also takes the mmap lock, while ane_h16_mmap() takes the file and node locks under it.

Why this shape

  • Where the decisions live: LOAD is the one point where user bytes become firmware input. The validator already walks every task there, and ane_h16_operation_validate() already builds the table of what each slot binds. The DMA check takes its buffer sizes from that table, so no second copy of a size can disagree.
  • Narrower alternatives: checking only particular slots or offsets leaves every other base unchecked. Guard pages between programs do not bound a 64-bit offset.
  • Separate address spaces per client are not available on this path: the firmware and the engine use the same stream on all three ANE DARTs. Running one program at a time with the others' regions unmapped would serialise the pipelined EXECUTEs.
  • The two LOAD sections have one form, which the kernel now derives from the validated program; the user's copy must match it byte for byte. Whether the kernel should build these two sections itself and stop taking them from userspace is a UAPI choice for the maintainers.
  • EXECUTE pins the user pages with no driver lock held, as direct I/O does, and copies through the pinned pages under the lock, where nothing can fault. A buffer in a VM_PFNMAP or VM_IO mapping, which cannot be pinned, is bounced outside both locks instead. Dropping the lock around the copies instead would let another request reuse the surfaces in between.
  • What it gives: the same checks for every SoC on the H16/H17 path. Selection is by the program format and the ioctl, with no chip check. Programs that stay inside their buffers load and run as before, with the same number of copies.

Change

  1. accel/ane: keep every H16/H17 DMA base inside the buffer its slot binds: at LOAD, every DMA base must be a relocated record on a slot the operation section binds. The end of the last 64-byte line its transfer touches must stay inside that buffer, using unsigned, overflow-checked arithmetic. A task that enables a DMA writes its base in the same task. Bases are checked per task and against a running image carried across tasks. DMA registers the check does not model stay refused, and fields it decodes stay within what the compiler writes. Refusals are ENOEXEC, before anything is mapped.
  2. accel/ane: send H16/H17 LOAD only the section bytes the validator checked: the procedure and text-properties sections must equal the form derived from the validated program, and the reserved bytes of the generic and operation sections must be zero.
  3. accel/ane: h16: copy EXECUTE buffers without user faults under the node lock: user pages are pinned before any lock, checked against the loaded program before and again under the file lock, copied under the node lock through kmap_local_page(), and unpinned after both locks are dropped. Buffers in VM_PFNMAP or VM_IO mappings are bounced outside both locks. The comment on ane_h16_export_put() now gives the lock order that must not happen and what its work item still avoids.
The extent rules of the DMA check
  • Input and output DMA: width x element bytes x interleave, plus (rows - 1) x row stride, (channel groups - 1) x plane stride, (depth - 1) x depth stride and (groups - 1) x group stride. The interleave packs that many channels into each plane.
  • Coefficient DMA: the furthest of the 16 lanes (offset plus size, at least the aligned per-channel size), plus each further group and output channel group. A channel group is 16 << (common word 0x40 & 7) channels.
  • Lookup table: its size word, at least 64.
  • Buffer sizes:
    • a surface: its declared size;
    • the scratch: its __bss size;
    • the kernel slot: to the end of the kernel section's last 16 KiB page, which is what that slot maps.
  • Other relocated records: only the cache-allocation words on the custom BAR slot 4. Dense header bits 21, 22 and 30, and a slot without the relocation bit, are refused.
  • Enabled DMAs: a task that enables an input DMA source, the output DMA or the lookup table writes that base in the same task, with a non-zero height and channel count. A zero width, depth or group count reads as one. The output channel group (0x40 & 7) is at most 5, and format bits are limited to the interleave, the element size and the bits every program sets.
  • Task state: both readings are checked. Each task is checked over the engine's defaults, and a running image is carried across the stream, so a base or a geometry left from an earlier task is bounded too. The running image also bounds the coefficient DMA, which lookup-table tasks configure without a base.
  • Other blocks: 0x44d0 stays zero, and the cache DMA block takes only the six words the compiler writes. The L2, planar engine and neural engine blocks configure on-chip buffers and arithmetic, and cannot be relocated.

No register documentation is available. The extent rules are read from the encoder of aurora-silicon/mil-hwx-compiler and checked against programs whose bound lands exactly on their buffer.

Testing

J775D (M5 Ultra, die 0, 27.2 firmware), read-only one-shot boots on the default command line and the published default Aura. Kit anechk6 is this PR on integration/aurora e892113 plus #2293, which describes the T6050 Neural Engine, and e9754164afe3, which enables the die 0 Neural Engine on the J775D. That commit is ours and not posted yet. These are the only additions the row needs. Kit anechk6b is the same base without this PR. Both kits add the ANE KUnit suites as modules.

anechk6b (base) anechk6 (this PR)
ANE KUnit (on the J775D) 270 pass, 0 fail, 1 skip 279 pass, 0 fail, 1 skip
add [1,128,1,1] x1000, every run compared 1000/1000 bit exact 1000/1000 bit exact
add [1,128,16,16] x200, every run compared 200/200 bit exact 200/200 bit exact
mobilenet, 16 images 16/16, p50 2.56 ms 16/16, p50 2.52 ms
EXECUTE from a dma-buf system heap mmap (all buffers, inputs only, output only; both add programs) bit exact, 0 errors bit exact, 0 errors
DMA bases outside their bound buffer, five cases not run refused at LOAD (ENOEXEC); the in-range control runs
reserved byte set in the generic header, a generic entry or an operation record loads and runs refused at LOAD (ENOEXEC)
procedure or text-properties sections in another form (four cases) not run on the base refused at LOAD (ENOEXEC)
unchanged sections after the refusals loads, exact loads, exact
a second client's LOAD while one client's input page fault is held 5 s 4705 ms (waits for the fault) 4.7 ms; its 20 EXECUTEs finish in 27 ms while the first client still waits
failed units, kernel errors 2 (iwd, networkd storage), 6 2 (the same), 6

ALARM has no SELinux, so there is no AVC count. In the main row, each arm logged one quarantine line and one DART fault line, the same on both, none of them during the LOAD-refusal or page-fault tests; the separate two-task probe windows below are counted there. Neither arm logged an SError.

Lock order: kit anechkld5 is extra evidence, the previous revision of this PR (head ca3e2c851fcc) on integration/aurora da67077 with the same needs, built with CONFIG_PROVE_LOCKING and run with the GPU module blacklisted (modprobe.blacklist=aurora), which trips lockdep at boot on its own. It differs from this head only in how the bounce copy is split out of ane_h16_user_get() and in that function's KUnit case. Over three rounds of EXECUTE followed by mmap() of a surface on one file, it reports nothing, and lockdep stays on. The first revision's base kit reported possible circular locking dependency there (&f->lock from ane_h16_mmap() under the mmap lock, against __might_fault() in ane_h16_exec_ioctl()).

Host check over 224 H16G and H17 programs built with aurora-silicon/mil-hwx-compiler (its fixtures and examples, a mobilenet in every schedule it accepts, and the two KUnit fixtures):

  • DMA rules: 215 pass, most with the bound exactly equal to the buffer, including every H17 program. The running image refuses 8 H16G programs that leave out default words after an earlier task set them: space-to-depth, depth-to-space, an attention fixture and five L2-resident schedules of the linked mobilenet. One H16G matrix-multiply example is refused under both readings: its output-group stride is larger than its whole scratch.
  • Sections: for all of them, h16-sections.py builds the procedure and text-properties sections byte-for-byte equal to the kernel's form.

Per-task register reset probe on J775D H17 (windows anerow5 on anechk6 and anerow6 on anechk6b, two-task variants of [1,128,16,16] fp16 add where task 0 zeroes output0 with full registers and task 1 omits one register):

  • Control (all registers written in task 1): rows 0..15 = [1.0..16.0] bit-exact, 0 DART faults.
  • Omit output row stride 0x5110 (anechk6): rows 0..15 = [16.0, 0.0, ..., 0.0] (resets to 0; 0 faults, 0 timeouts).
  • Omit input0 row stride 0x4d18 (anechk6): rows 0..15 = [1.0, 1.0, ..., 1.0] (resets to 0; 0 faults, 0 timeouts).
  • Omit common widths 0x04/0x14 (anechk6): EXECUTE timed out and the firmware quarantined the program (firmware_stage program wedged, quarantine count 1), with 0 DART faults (widths do not carry).
  • Omit output DMA base 0x5108 (anechk6b): output0 stayed 0.0 with IOMMU write faults from IOVA 0x0 (resets to 0).
  • Omit input0 base 0x4d10 (anechk6b): inconclusive. output0 stayed 0.0, which carrying task 0's zero input base also predicts, and the DART fault log was rate-limited after the 10 lines from 0x5108 immediately before it.
    The running image stays enabled on every chip: on H17 it refuses nothing, and it is what bounds the coefficient DMA (and any unproven input-base carry).

Still to run

  • Programs from other compilers: 8-bit surfaces (accepted since accel/ane: h16, h17: load programs with int8 and uint8 surfaces #2748) and the programs from the other compiler that accel/ane: accept H17 programs from Apple's zin_ane_compiler 10.205 (on #1790) #1882 admits have not been run through either check, on the host or on hardware. The DMA check refuses registers and field values it does not model, so this shows whether any of them need a rule.
  • Other machines: an H17 row on a tree-supported M5 without extra DT, and H16 rows (M4 Pro, M4) with a real model and the two-task per-task reset check, from someone with the hardware.
  • The refused H16G matmul example: whether it reads past its scratch or uses a stride unit the model lacks needs a run on H16.
  • H17 zero-width behavior: omitting common widths on H17 stalled the engine and quarantined the program rather than running as width 1. The check reads a zero width as one to match the H16 matmul receipt; whether an H17 zero width can reach past its buffer before stalling needs checking.

Assisted-by: LLM (Claude:Opus 5.5), LLM (Gemini:Neon)

@RyanTheTide

Copy link
Copy Markdown

Review at b29dac1.

Thanks for this, the DMA model and the lockdep/fault-hold rows are well done. Some things I'd like settled before merge:

  1. ane_h16_tasks.h:269-274, 336-399: each task's bases are checked against that task's words over the engine defaults, and the PR says carry-over between tasks was not checked. If the engine does keep registers, the check can be bypassed: task N sets large strides/dims and writes no base (so nothing is checked), then task N+1 writes an in-range base and leaves the strides out (defaults, small extent). It also works the other way round, with the base kept and the geometry changed. Could you either show on hardware that each task starts from defaults (a two-task program that would fault if state carried over), or also check each written base against a running image across tasks? Real programs repeat every word, so the running image should only refuse programs that are already out of bounds. A KUnit case for either form would help.
  2. ane_h16_tasks.h:392-394: offset + round_up(extent, 64) is only the end of the last line touched when the offset is 64-byte aligned. With offset 1 and extent 64 the engine reads [0, 128) but the check computes 65. Either refuse unaligned bases or compare ALIGN(offset + extent, 64). With bytes == allocation the extra bytes go into the next DVA page.
  3. Compatibility: the check refuses every DMA register it does not model (bias, post-scale and palette tables, compression, crop and so on). The accel/ane: accept H17 programs from Apple's zin_ane_compiler 10.205 (on #1790) #1882 zin 10.205 programs and the accel/ane: h16, h17: load programs with int8 and uint8 surfaces #2748 int8/uint8 programs are real inputs that load today and have not been run through it. Quantized or palettized models seem the most likely to use those registers. I think that corpus run (and the same corpus against the exact-form procedure/text-properties check) is needed before merge, not after. Separately, is the refused H16G matmul example a real out-of-bounds write by the compiler (did it run correctly before?) or a gap in the model? Either answer is useful, but it should be stated.
  4. ane_h16_accel.c:193: pin_user_pages_fast() fails on VM_PFNMAP/VM_IO mappings, where copy_from_user() used to work. drm_gem_shmem mmaps (drm_gem_shmem_helper.c:1044, used by the GPU driver) and CMA-heap dma-buf mmaps are VM_PFNMAP, so EXECUTE from a mapped GPU buffer now returns EFAULT. If that's intended, please say so in the commit. Otherwise, falling back to a bounce copy done outside both locks for such ranges would keep the old behaviour.
  5. ane_h16_program.c:968-971: the new comment says the node lock must never be taken under the mmap lock, but ane_h16_mmap() (ane_h16_accel.c:401-417) takes f->lock and, via ane_h16_session_enter(), the node lock under it. The constraint after this change is the reverse: nothing may fault or take the mmap lock while holding the node lock. Could the comment say what the work-item deferral actually still avoids?
  6. Rows: the only hardware row is J775D die 0, with arm64: dts: apple: t6050: share the Neural Engine with every board #2293 and an unposted DT commit on top. Still needed at this head: an H17 row on a tree-supported M5 without extra DT, and H16 rows (J616s and J713) running a real model (mobilenet plus one zin-compiled program) bit-exact, plus one refused out-of-bounds LOAD. The H16 path has only the host corpus check so far.

The pin/unpin error paths look right to me: every exit after a pin goes through unpin:, the second check under f->lock covers a reload in between, and outputs are only marked dirty after a successful run.

@RyanTheTide

Copy link
Copy Markdown

Review notes on the DMA base check (commit 1). I read it; no build.

  1. Please also require, in every task, a relocated base record for each DMA that the task enables (TileDMA source/destination, KernelDMA, NLut). As written, the extent check runs only where a relocated base pair is present (ane_h16_tasks.h around 381-395), so a task that enables a DMA without writing its base is not bounded. The base it uses then depends on engine defaults or on the previous task, which the PR text says was not checked.
  2. The allowed register blocks that the check does not model (0x4100-0x41a4, 0x44d0, 0x4500-0x453c, 0x4900-0x4930, 0x5900-0x5930) accept any value. Could these be held to zero as the other unmodelled DMA registers are, or could the commit message say why each holds no address?
  3. Small hardening, same reasoning as the strict choice for unmodelled registers: refuse zero dimensions instead of treating them as 1, bound the channel-group field (common 0x40 & 7) to what the compiler emits, and refuse format bits the compiler never sets, so the extent rules cannot be under-estimated by a decoding difference.

Commits 2 and 3 look right to me. A minor point: buffers backed by VM_PFNMAP mappings now fail to pin where they used to be copied. That seems acceptable, but it might be worth a line in the commit message.

A task names each buffer the engine reads or writes as a relocated
[lo, hi] record, whose 64-bit offset is taken relative to the DVA of the
buffer bound to the record's BAR slot. The validator checked that each
register lies in a known block and that the operation section binds
each slot to the right resource, but not the offset, nor that the slot
is bound, nor that a base is relocated at all.

At LOAD, once the operation section is checked and before anything is
mapped, walk every task:

- A DMA base (the input DMA's sources 1 and 2, the output DMA, the
  coefficient DMA and the lookup table) is written only by a relocated
  record covering exactly its lo and hi words, on a slot the operation
  section binds.
- The end of the last 64-byte line its transfer touches,
  ALIGN(offset + extent, 64), stays inside that buffer: a surface's
  declared size, the scratch's __bss size, or the kernel section to the
  end of its last 16 KiB page, which is what the kernel slot maps. The
  offset is unsigned, so a negative one fails; products and sums are
  overflow-checked.
- An input or output extent is width x element bytes x interleave, plus
  (rows - 1) x row stride, (channel groups - 1) x plane stride,
  (depth - 1) x depth stride and (groups - 1) x group stride, the
  interleave packing that many channels per plane. The coefficient
  extent is its furthest lane (offset plus size, at least the aligned
  per-channel size) plus each further group and output channel group, a
  channel group being 16 << (common word 0x40 & 7) channels. The lookup
  table is its size word, at least 64.
- A task that enables an input DMA source, the output DMA or the lookup
  table (a non-zero configuration word) writes that DMA's base in the
  same task, and a height and channel count that are not zero. A zero
  width, depth or group count is read as one: the compiler writes 0
  there, and a zero width is in a 256 x 256 matmul that runs correctly
  on an M4 Pro. Lookup-table tasks configure the coefficient DMA without
  a base, so that DMA is bounded by the running image below instead.
- Fields the extent rules decode stay within what the compiler writes:
  the output channel group (0x40 & 7) at most 5, and no format bits
  besides the interleave, the element size and the bits every program
  sets.
- The only other relocated records are the cache-allocation words on
  the custom BAR slot 4. Dense header bits 21, 22 and 30, and a slot
  without the relocation bit, are refused.
- The DMA registers the bound does not model stay zero: metadata and
  compression, texture, crop and wrap, the bias, post-scale and
  palette tables, the destination pixel offset, and the wrap and
  offset fields beside the cache hints and formats. They stay refused
  until a program needs them. Outside the DMA blocks, 0x44d0 stays zero
  and the cache DMA block takes only the six words the compiler
  writes. The L2, planar engine and neural engine blocks configure
  on-chip buffers and arithmetic, and cannot be relocated, so they hold
  no buffer address.

Whether the engine starts each task from its defaults or keeps
registers from the task before is not known, so both readings are
checked: each task over the defaults, and a running image carried
across the stream, against which every base written so far is checked
after each task with the geometry in force then. Programs leave a word
out wherever it holds the default, also when an earlier task set
another value. Where that leaves an earlier base with a larger extent,
the stream fits only the first reading and is refused. Refusals are
ENOEXEC, as for the validator's other program checks.

No register documentation is available. The extent rules are read from
the encoder of aurora-silicon/mil-hwx-compiler and checked against
programs whose bound lands exactly on their buffer.

Of the 224 H16G and H17 programs that aurora-silicon/mil-hwx-compiler
builds from its fixtures and examples, a mobilenet in every schedule it
accepts and the two KUnit fixtures, 215 pass, most of them with the
bound exactly equal to the buffer, and every H17 program among them.
The running image refuses 8 H16G programs (space-to-depth,
depth-to-space, an attention fixture and five L2-resident schedules of
the linked mobilenet). One H16G matrix-multiply example is refused
under both readings: it gives two 128-channel output groups a channel
group stride of 0x40000 over a 0x20000 scratch.

KUnit: ane_hx_dma_bound_test and ane_hx_dma_kernel_test cover a base
exactly at the end of its buffer, one line past it, negative, wrapping
64 bits, on an unbound slot, unrelocated, scattered, relocated off its
pair, and beside an unmodelled register. ane_hx_dma_carry_test covers
geometry in one task with the bases in the next, and a base kept while
a later task enlarges its geometry. ane_hx_dma_strict_test covers an
enabled DMA without its base or with a zero size, the channel group,
a format bit and the registers outside the DMA blocks.
ane_hx_dma_align_test covers a lookup table at offset 1, which touches
two lines. program_add128_dma_test runs the cases through the add-128
fixture's LOAD checks. The mobilenet and add-128 fixture cases still
pass.

On a J775D (die 0, 27.2 firmware) add-128 x1000 and a [1,128,16,16] add
x200 stay bit exact, the linked mobilenet program classifies 16 of 16
images, and programs whose bases leave their buffers are refused at LOAD
before anything is mapped.

Reported-by: Erik Bourget <erik@ebourget.net>
Suggested-by: Erik Bourget <erik@ebourget.net>
Signed-off-by: Jeff Queisser <funkzy@users.noreply.github.com>
…cked

H16_LOAD takes four host-built sections. The generic and operation
sections were checked against the HWX field by field, but not their
reserved bytes, and the procedure and text-properties sections only for
their size. The text properties give the firmware the task stream's
framing: segment offset, segment size and task count. The kernel
validates the stream as it frames it, from its prologue with the HWX's
task count, so properties that frame it differently describe a stream
nobody checked.

Both sections have one form, which the selftests' h16-sections.py writes
and the kernel now derives from the validated program: one procedure,
and one text segment covering the whole task stream with its task count.
H16_LOAD refuses any other size or byte. The reserved bytes of the
generic header and of each entry, and an operation record's bytes after
its last reference, must be zero, and an entry's tail word must be
ANE_H16_GENERIC_ENTRY_TAIL, as h16-sections.py writes them. Refusals are
ENOEXEC, before anything is mapped or sent.

The add-128 and mobilenet sections that h16-sections.py builds, which
the KUnit fixtures carry, are exactly the kernel's form.

KUnit: program_sections_exact_test refuses another task count, segment
offset or size, a longer section, a changed procedure, and a nonzero
reserved byte in each place above, and refuses such a LOAD before any
mapping or firmware traffic. The session tests now pass the procedure
and text properties h16-sections.py writes instead of zeroes.

On a J775D (die 0, 27.2 firmware) add-128 x1000 and a [1,128,16,16]
add x200 stay bit exact and the linked mobilenet program classifies 16
of 16 images; sections in any other form are refused at LOAD.

Reported-by: Erik Bourget <erik@ebourget.net>
Suggested-by: Erik Bourget <erik@ebourget.net>
Signed-off-by: Jeff Queisser <funkzy@users.noreply.github.com>

@DjDeveloperr DjDeveloperr 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.

Formal §1–§6 review at b29dac1a746c.

The overall architecture (LOAD-time DMA base bounding, exact-form section validation, and lock-free user page pinning in EXECUTE) is strong, lockdep/fault-hold testing on J775D is thorough, and §1 (no chip gates), §4 (human commits + Signed-off-by/Reported-by/Suggested-by, clean-room, PR Assisted-by), §6 (naming), and CI (host-tests) pass.

Blockers to address before merge:

  1. Blocker — Commit 1 (ane_h16_tasks.h:269-274, 336-399): Unverified cross-task register carry-over and missing required-base enforcement:
    • ane_hx_dma_image() resets img[] to defaults on every task, and the PR notes that register carry-over across tasks was not checked on hardware.
    • If the engine retains state across tasks, the validator is bypassable in both directions: task N writes large dims/strides with no base record (nothing checked), and task N+1 writes a base while omitting strides (validated against small defaults, executed with task N's strides), or vice versa.
    • Similarly, ane_hx_task_stream_dma_validate() only checks extent where a relocated [lo, hi] base record is written, without requiring every DMA enabled by a task to write a relocated base.
    • Fix: either prove on hardware (e.g. a two-task fault test) that hardware resets DMA/common state per task and require a relocated base for each enabled DMA, or maintain a running register image across tasks and validate every enabled DMA on every task.
  2. Blocker — Commit 1 (ane_h16_tasks.h:392-394): Unaligned base offset breaks 64-byte line rounding:
    • Checking offset + ALIGN(extent, 64) <= bound[slot] under-estimates the last byte touched when offset is not 64-byte aligned (e.g. offset = 1, extent = 64 computes last = 65, while 64-byte DMA lines covering [offset, offset + extent) span [0, 128)). Either reject unaligned offsets (offset & 63) or bound ALIGN(offset + extent, 64) with overflow checking.
  3. Blocker — Commit 1 compatibility corpus & unmodelled blocks:
    • The strict allowlist rejects registers it does not model, yet existing supported #2748 int8/uint8 programs and #1882 zin-compiled programs have not been checked against the new DMA and exact-section validators, and the refused H16G matmul example has not been diagnosed (real compiler OOB vs. extent-model gap). Please run that corpus check before merge so working userspace is not regressed.
    • Also document (or bound) why the other allowed blocks in ane_h16_register_valid() (0x4100-0x41a4, 0x44d0, 0x4500-0x453c, 0x4900-0x4930, 0x5900-0x5930) cannot carry DMA addresses or offsets.
  4. Blocker — Commit 3 (ane_h16_accel.c:193, ane_h16_user_pin wrap edge case, ane_h16_program.c:968-971):
    • pin_user_pages_fast() returns -EFAULT on VM_PFNMAP/VM_IO mappings (such as drm_gem_shmem GPU mmaps or CMA dma-buf mmaps) where copy_from_user()/copy_to_user() previously worked. Either explicitly document this UAPI change in the commit message or provide an unlocked bounce-copy fallback for VM_PFNMAP ranges.
    • In ane_h16_user_pin() (ane_h16_accel.c:42-45): DIV_ROUND_UP(u->offset + size, PAGE_SIZE) > INT_MAX can wrap to 0 when u->offset + size > SIZE_MAX - (PAGE_SIZE - 1) (e.g. ptr = PAGE_SIZE - 1, size = SIZE_MAX - (PAGE_SIZE - 1) passes ptr + size >= ptr, overflows DIV_ROUND_UP to 0, and returns 0 with u->count = 0). Bounded by surface size in the ioctl path today, but the helper itself should check size > (size_t)INT_MAX * PAGE_SIZE - u->offset (or check_add_overflow) cleanly.
    • ane_h16_program.c:968-971: the updated comment says the node lock must never be taken under the mmap lock, which contradicts ane_h16_mmap() (ane_h16_accel.c:300-316) taking f->lock and a->lock under the mmap lock. The actual ordering after commit 3 is mmap_lock -> f->lock -> a->lock (nothing may fault/take mmap_lock under a->lock, and ane_h16_export_put() defers because h16_give_back() calls it under a->lock and vma_close under mmap_lock).
  5. Blocker — §5 hardware coverage:
    • Only J775D die 0 (with #2293 + an unposted DT commit, ALARM without SELinux AVC counts) has been tested. Still needed: a tree-supported H17/M5 row without out-of-tree DT, and H16 hardware rows (M4 / M4 Pro) running real models (MobileNet + zin/int8) plus out-of-bounds LOAD refusal with full regression counts.

Assisted-by: LLM (Gemini 4 Neon)

@funkzy
funkzy force-pushed the pr/ane-h16h17-checks branch from b29dac1 to 1c027e7 Compare November 23, 2026 16:36
@funkzy

funkzy commented Nov 23, 2026

Copy link
Copy Markdown
Author

Thanks to both of you. This covers Ryan's two comments and dj's review together. The new head is 1c027e7. Points follow Ryan's first comment; his second is marked R2, and dj's blockers 1-5 map to Ryan's 1, 2, 3 with R2-2, 4 with 5, and 6.

1, R2-1: task state and enabled DMAs. Both readings are now checked. Each task is checked on its own words over the defaults, as before. A running image carried across the stream is checked as well: after each task, every base written so far is checked against the geometry in force then. In addition, a task that enables an input DMA source, the output DMA or the lookup table (any non-zero configuration word) must write that DMA's base in the same task, with a non-zero height and channel count. ane_hx_dma_carry_test covers your two forms. Geometry in task N with the bases alone in N+1 is now refused by the enable rule. A base kept while task N+1 enlarges the geometry is refused by the running image.

Two limits:

  • Real programs don't repeat every word, it turns out. The encoder leaves out words that hold the default, even when an earlier task set another value. The running image therefore refuses 8 of the 224 compiler programs: H16G space-to-depth, depth-to-space, an attention fixture and five L2-resident schedules of the linked mobilenet. Every H17 program and the default H16G mobilenet pass. The commit message says so.
  • The coefficient DMA can't be held to the enable rule. Lookup-table tasks set its configuration word without writing a base (46 tasks across the corpus). It is bounded by the running image instead.

To check per-task register reset directly on the J775D's H17, I ran two-task variants of the [1,128,16,16] fp16 add program (task 0 computes z + z = 0 into output0 with full registers; task 1 computes x + z with x rows 0..15 set to 1.0..16.0, omitting one register at a time):

  • Control (task 1 writes all registers): rows 0..15 = [1.0, 2.0, ..., 16.0] bit-exact, 0 DART faults.
  • Omit output row stride 0x5110 in task 1 (on anechk6): rows 0..15 = [16.0, 0.0, ..., 0.0] (all 16 rows wrote to row 0, leaving task 0's zeros in rows 1..15; 0 faults, 0 timeouts).
  • Omit input0 row stride 0x4d18 in task 1 (on anechk6): rows 0..15 = [1.0, 1.0, ..., 1.0] (task 1 read row 0 for all 16 rows; 0 faults, 0 timeouts).
  • Omit common widths 0x04/0x14 in task 1 (on anechk6): EXECUTE timed out and the firmware quarantined the program (firmware_stage program wedged, quarantine count 1), with 0 DART faults (widths did not carry 16 from task 0). That also leaves an open question in Still to run: on H17 an omitted width stalls rather than running as width 1, and whether width 0 can reach past its buffer before stalling is not yet checked.
  • Omit output DMA base 0x5108 in task 1 (on base kernel anechk6b): output0 stayed 0.0 with IOMMU write faults from IOVA 0x0, showing 0x5108 reset to 0 at the task boundary.
  • Omit input0 DMA base 0x4d10 in task 1 (on anechk6b): inconclusive. output0 stayed 0.0, which carrying task 0's zero input base also predicts, and the IOMMU fault log was rate-limited after 0x5108 logged 10 lines immediately before it.

This confirms H17 resets the output DMA base and the output and input0 row strides, and does not carry common widths, while input-base reset remains unproven. The running image stays enabled on every chip (no chip gate): on H17 it refuses nothing, and it is what bounds the coefficient DMA in lookup-table tasks as well as any unproven input-base carry.

2: line end. ALIGN(offset + extent, 64) (overflow-checked) is now compared with the buffer size. ane_hx_dma_align_test covers a 64-byte table at offset 1: refused at 100 bytes, accepted at 128. It fails on the old check.

R2-2: unmodelled blocks. Zero isn't possible for most of them, since every program writes them. Instead:

  • 0x44d0 is never written by the compiler and must now be zero.
  • The cache DMA block (0x5900-0x5930) takes only the six words the compiler writes. None of them is an address.
  • The L2 (0x4100), planar engine (0x4500) and neural engine (0x4900) blocks configure on-chip buffers and arithmetic: L2 offsets and strides, operation selectors, fp32 and fp16 constants, mode bits. None of their records may carry the relocation bit, so none of them is given a buffer's address.

The commit message gives the per-block reason.

R2-3: decode hardening.

  • An enabled DMA with a zero height or channel count is refused.
  • A zero width, depth or group count is still read as one. The compiler writes 0 for depth and groups in nearly every task, and width 0 appears in the 256 x 256 matmul that ran correctly on an M4 Pro (the compiler's J616s receipt), so refusing it would refuse a working program.
  • The output channel group (0x40 & 7) above 5 is refused; 5 is the largest the corpus uses.
  • Format bits are now limited to the interleave, the element size (kept for 8-bit surfaces) and the bits every program sets.

ane_hx_dma_strict_test covers each refusal.

3: corpus. I don't have the #1882 programs or the #2748 int8/uint8 programs. The tree has only synthetic fixtures for both, and mil-hwx-compiler builds int8 only for H13/H14. So neither ran here, on the host or on hardware. This stays open before merge.

  • DMA check, with everything above: 215 of 224 compiler programs pass. The 8 are listed in point 1, plus the matmul below.
  • Exact-form sections: for all 225 programs (the 224 plus the rank-3 matmul below), the tree's h16-sections.py builds the procedure and text-properties sections byte-for-byte equal to the form the kernel derives.

The refused H16G matmul (s_mm_256, one of the compiler's chain examples) has no hardware run, so I can't say whether it ran correctly before. The same-size rank-3 matmul that ran correctly on an M4 Pro (mm256 in that receipt, rebuilt bit-identical) passes. In the coefficient registers, the refused program differs from it only in bit 2 of word 0x3c and in an output-group stride of 0x40000 against 0x10000, the group size. If that bit changes the stride's unit, this is a gap in the model, and the fix is a rule taken from the encoder. Otherwise the program reads past its scratch. A run of s_mm_256 on an M4 under a kernel without the check, compared with a CPU reference, would settle it; it is in the H16 rows below.

4, R2 note: unpinnable buffers. Not intended, thanks. When pinning fails and the range reaches a VM_PFNMAP or VM_IO mapping, the buffer is bounced:

  • An input is copied into a kernel buffer when it is taken, with no lock held.
  • An output is copied back after both locks drop, and only if the request ran.
  • Unmapped memory and read-only outputs still fail before the request.

ane_h16_accel_user_bounce_test marks a mapping VM_IO to cover this.

dj's wrap case in ane_h16_user_pin() is fixed. The size is now checked against INT_MAX pages before the page count is computed, so DIV_ROUND_UP() can no longer wrap to 0. ane_h16_accel_user_pages_test covers your example (ptr = PAGE_SIZE - 1, size = SIZE_MAX - (PAGE_SIZE - 1)) and one page past the limit. On the J775D, EXECUTE stays bit exact with its buffers in a dma-buf system heap mmap: all buffers there, only the inputs, or only the output.

5: comment. The comment now gives the order that must not happen: the mmap lock, or a user fault, taken under the node lock. Taking the node lock under the mmap lock is what ane_h16_mmap() does. It also says what the work item still avoids: a last put from h16_give_back(), which already holds the node lock, and a munmap() or exit that would otherwise wait under the mmap lock for a running request.

6: rows. The J775D row at the new head is in the body. KUnit passes 279/0/1. The add programs stay bit exact, the mobilenet classifies 16/16, and every out-of-bounds and section case is refused at LOAD. A second client's LOAD takes 4.7 ms while one client's page fault is held, and the lockdep run reports nothing. It still needs #2293 and our J775D die 0 Neural Engine opt-in, e9754164afe3. That is a DT-only commit enabling die 0's ANE in t6050-j775d.dts; it is not posted, and nothing else in the row depends on it. Still to run: the H16 rows (J616s and J713, the mobilenet and one program from the #1882 set, bit-exact, plus one refused out-of-bounds LOAD, the two-task per-task reset probe above, and s_mm_256 under a kernel without the check for point 3), an H17 row on a tree-supported M5 without extra DT, and the #1882 and #2748 programs through both checks. I'll add each as it comes in.

Assisted-by: LLM (Claude:Opus 5.5), LLM (Gemini:Neon)

@DjDeveloperr DjDeveloperr 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.

Re-review of 1c027e781aaa86cde7e21f92135b6f4467281a77 against pre-pr-checklist.md (§1–§6) (CI: Host tests / host-tests SUCCESS; MERGEABLE / CLEAN, non-draft).

Resolved since b29dac1a746c

  • Dual per-task + running-image DMA checks, enabled-DMA base requirement, overflow-checked ALIGN(offset + extent, 64), unmodelled-block audit (0x44d0 zeroed, cache DMA allowlist, non-relocatable L2/PE/NE blocks), decode hardening (ane_hx_dma_strict_test), ane_h16_user_pin() INT_MAX * PAGE_SIZE wrap fix, VM_PFNMAP/VM_IO bounce path, corrected ane_h16_export_put() lock-order comment, and J775D per-task reset probes are all solid improvements.
  • §1, §4, §6: Clean — no chip gates; all 3 commits carry human author/committer + human Signed-off-by: (Reported-by:/Suggested-by:) only, zero AI commit trailers; PR body discloses Assisted-by:; naming clean.

Remaining blockers before approval

  1. New code bug — drivers/accel/ane/ane_h16_accel.c:74-125 (ane_h16_user_unpinnable() / ane_h16_user_bounce() / ane_h16_user_get()):
    • ane_h16_user_unpinnable(ptr, size) returns true as soon as it sees any VM_PFNMAP | VM_IO VMA in [ptr, ptr + size) without checking VM_WRITE or verifying that the entire [ptr, ptr + size) range is mapped with no holes.
    • For a read-only VM_PFNMAP/VM_IO output (or a VM_PFNMAP/VM_IO VMA followed by an unmapped hole or read-only VMA), ane_h16_user_pin(..., write=true) returns -EFAULT, ane_h16_user_unpinnable() returns true, and ane_h16_user_bounce(..., write=true) skips copy_from_user() and returns 0. The ANE request then executes on hardware before ane_h16_user_put() fails on copy_to_user(), contradicting the commit message and ane_h16_user_get() doc comment ("Unmapped memory and read-only outputs still fail before the request"). (ane_h16_accel_user_bounce_test missed this because ro did not have VM_IO set.)
    • Fix: pass write to ane_h16_user_unpinnable(), walk the full [ptr, ptr + size) range with no gaps, require VM_WRITE on every VMA when write is true (and VM_READ when !write), return true only if the entire range is validly mapped and at least one VMA has VM_PFNMAP | VM_IO, and add KUnit coverage for a read-only VM_IO output and a VM_IO + unmapped-hole range.
  2. Compatibility corpus blockers (open before merge):
    • #1882 (zin-compiled) programs and #2748 (int8/uint8) programs have still not been run through the DMA or exact-section checks (host or hardware).
    • The running image refuses 8 mil-hwx-compiler H16G programs (space-to-depth, depth-to-space, attention fixture, and 5 L2-resident MobileNet schedules) that omit default words after an earlier task set them, and the refused H16G matmul s_mm_256 remains undiagnosed (real compiler OOB vs missing stride-unit rule for word 0x3c bit 2).
  3. §3 H17 zero-width hazard: The J775D probe showed omitting common widths (0x04/0x14) on H17 stalls and quarantines the program (firmware_stage program wedged), while ane_hx_dma_tile() still accepts width 0 as 1; confirm whether width 0 on H17 can reach past its buffer before stalling (or tighten accordingly once H16 mm256 vs H17 behavior is settled).
  4. §5 Hardware coverage: Still only tested on J775D die 0 with #2293 + an unposted out-of-tree DT commit (e9754164afe3, ALARM/no SELinux). Still needed before merge: a tree-supported H17/M5 row without out-of-tree DT, and H16 hardware rows (M4 / M4 Pro, e.g., J616s and J713) running MobileNet + #1882/#2748 programs bit-exact, refused OOB LOAD, s_mm_256 baseline check, and the two-task per-task reset probe with full regression counts.

Assisted-by: LLM (Gemini 4 Neon)

…de lock

Without DRM_ANE_H16_EXEC_IN_PLACE, H16_EXEC copied each input from user
memory into its surface, and each output back, with the node lock held
and the request counted active. A page fault in those copies waits for
whatever backs the page, and for that long every other client's LOAD,
EXECUTE, mmap and close waited on the lock, as did the PM notifier's
drain at suspend, which waits for no request to be active. The fault
also takes the mmap lock, while mmap() of a surface takes the file and
node locks under it.

Pin the user pages of every buffer with no driver lock held: outputs
with FOLL_WRITE, so a buffer that cannot be written fails here with
EFAULT instead of after the request ran. The buffers are checked against
the loaded program before pinning, and again under the file lock after
it, since the program may have changed in between. Under the node lock
the copies go through the kernel mapping of the pinned pages and cannot
fault; the pages are unpinned, outputs marked dirty, after both locks
are dropped. There is still one copy each way.

Pages in a VM_PFNMAP or VM_IO mapping cannot be pinned, and EXECUTE
from a mapped GEM-shmem or dma-buf heap buffer worked with
copy_from_user() before. A buffer that reaches such a mapping is bounced
instead: an input is copied into a kernel buffer when it is taken, with
no lock held, and an output is copied back to user memory after both
locks are dropped, only if the request ran. Unmapped memory and
read-only outputs still fail before the request.

The comment on ane_h16_export_put() said an EXECUTE may wait for the
mmap lock while holding the node lock, which is no longer true. It now
gives the order that must not happen, the mmap lock or a user fault
under the node lock, and what the deferral to a work item still avoids:
a last put from h16_give_back(), which holds the node lock, and a
munmap() that would wait under the mmap lock for a running request.

KUnit: ane_h16_accel_user_pages_test pins an input and an output that
cross a page boundary, compares the copies with copy_from_user() and
copy_to_user(), and fails the pin, holding nothing, for an unmapped
buffer, a read-only output, a range that wraps and a size whose page
count would wrap;
ane_h16_accel_user_bounce_test finds a VM_IO mapping in a range, takes
an input and an output through the bounce copy, leaves an output the
request did not write unchanged, and still fails a read-only output
when taken.

On a J775D (die 0, 27.2 firmware) another client's LOAD and EXECUTEs
complete while one client's input page fault is outstanding, where they
waited for it before. The add programs stay bit exact, and the
mobilenet program classifies 16 of 16 images at a p50 request latency
of 2.55 ms, as before (2.56 to 2.59 ms). EXECUTE with its buffers in a
dma-buf system heap mmap stays bit exact (all buffers, only the inputs,
only the output).

Reported-by: Erik Bourget <erik@ebourget.net>
Suggested-by: Erik Bourget <erik@ebourget.net>
Suggested-by: Bartosz Nitka <niteria@gmail.com>
Signed-off-by: Jeff Queisser <funkzy@users.noreply.github.com>
@funkzy
funkzy force-pushed the pr/ane-h16h17-checks branch from 1c027e7 to 35b9d3b Compare November 23, 2026 23:56
@funkzy

funkzy commented Nov 23, 2026

Copy link
Copy Markdown
Author

Updated in 35b9d3b40af5 to fix item 1 (ane_h16_user_unpinnable()):

  • ane_h16_user_unpinnable(ptr, size, write) now walks the full [ptr, ptr + size) range with no gaps (!vma || vma->vm_start > addr), requires VM_WRITE when write is set (and VM_READ otherwise) on every VMA in the range, and returns true only if the entire range is validly mapped and at least one VMA carries VM_PFNMAP | VM_IO. Read-only VM_IO/VM_PFNMAP outputs and VM_IO ranges extending into an unmapped hole now fail ane_h16_user_get() with -EFAULT before bouncing or executing.
  • ane_h16_accel_user_bounce_test (ane-h16-accel) now covers both a read-only VM_IO output and a VM_IO mapping followed by an unmapped hole, verifying ane_h16_user_unpinnable() and ane_h16_user_get(..., true) == -EFAULT with u.bounce == NULL and u.pages == NULL.

Arm64 KUnit on 35b9d3b40af5 (base 110861f4333e4, CONFIG_DRM_ACCEL_ANE=y with DRM_ACCEL_ANE_KUNIT_TEST, DRM_ACCEL_ANE_FW_KUNIT_TEST, and DRM_ACCEL_ANE_PM_KUNIT_TEST built in):

  • ANE suites (18/18): 302 of 303 cases pass, 0 fail, 1 skip (ane-fw-cold/ane_cold_no_clpc_test: CLPC enabled; absence is covered by the CLPC suite).
  • Suites modified by this PR: ane-h16-accel 4/4 (ane_h16_accel_user_pages_test, ane_h16_accel_user_bounce_test pass), ane-h16-tasks 11/11, ane-h16-program 61/61.
  • Full boot KUnit total: 28 suites, 344 of 346 cases pass, 0 fail, 2 skip (apple_mbox_g17_stall/apple_g17_rx_util_min_test needing CONFIG_UCLAMP_TASK, plus ane_cold_no_clpc_test above).

Items 2–4 (the #1882/#2748 corpus checks, the 8 refused mil-hwx-compiler H16G programs and s_mm_256 diagnosis, the H17 zero-width check, and the remaining H16 and tree-supported H17 hardware rows) remain open and will be added as they come in.

Assisted-by: LLM (Gemini:Neon)

@DjDeveloperr DjDeveloperr 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.

Formal §1–§6 pre-PR checklist re-review at verified live head 35b9d3b40af538b0a912a2d2851a125149920806 (MERGEABLE / CLEAN, CI 1/1 green: Host tests / host-tests).

Resolved Since 1c027e781aaa

  • Prior Blocker #1 (ane_h16_user_unpinnable() / bounce validation) — FIXED: drivers/accel/ane/ane_h16_accel.c:77-100 now walks the entire [ptr, ptr + size) range with no gaps (!vma || vma->vm_start > addr), requires VM_WRITE when write is set (VM_READ otherwise) on every VMA in the range, and returns true only when the entire range is validly mapped and at least one VMA carries VM_PFNMAP | VM_IO. ane_h16_accel_user_bounce_test covers both a read-only VM_IO output and a VM_IO + unmapped-hole range, and arm64 KUnit at 35b9d3b40af5 passes 302/303 ANE cases (0 fail, 1 skip) including ane-h16-accel 4/4, ane-h16-tasks 11/11, ane-h16-program 61/61.

Checklist Summary

  • §1 No model gating: Pass (program-format/ioctl-driven, no chip gates).
  • §2 Production-ready, no hacks: Code in commit 3 is clean; compatibility corpus questions remain open (Blocker 1).
  • §3 Safety: Strong LOAD-time DMA bounding and lock-free user pinning/bouncing; H17 zero-width hazard remains open (Blocker 2).
  • §4 Clean-room & attribution: Pass — all 3 commits carry human author/committer (Jeff Queisser <funkzy@users.noreply.github.com>) and human Signed-off-by: / Reported-by: / Suggested-by: only, zero AI trailers in commits; PR body disclosure present.
  • §5 Evidence: Blocked — see Blockers 1–3.
  • §6 Naming: Pass.

Remaining Blockers (acknowledged open in 23:57Z comment)

  1. §2 / §5 — Compatibility corpus & refused H16G programs:
    • #1882 (zin-compiled) and #2748 (int8/uint8) programs have still not been run through the DMA or exact-section validators (host or hardware).
    • 8 mil-hwx-compiler H16G programs (space-to-depth, depth-to-space, attention fixture, 5 L2-resident MobileNet schedules) are refused by the running-image check, and the refused H16G matmul s_mm_256 remains undiagnosed (real compiler OOB vs missing stride-unit rule for word 0x3c bit 2).
  2. §3 — H17 zero-width behavior: Omitting common widths (0x04/0x14) on J775D H17 stalled and quarantined the program (firmware_stage program wedged), while ane_hx_dma_tile() accepts width 0 as 1. Confirm whether width 0 on H17 can touch past its buffer before stalling (or tighten once H16 mm256 vs H17 behavior is settled).
  3. §5 — Multi-Mac & exact-head hardware coverage: No hardware run at 35b9d3b40af5 yet; still needed before merge: a tree-supported H17/M5 row without out-of-tree DT, and H16 hardware rows (M4 / M4 Pro, e.g., J616s and J713) running MobileNet + #1882/#2748 programs bit-exact, refused OOB LOAD, s_mm_256 baseline check, and the two-task per-task reset probe with full regression counts.

Assisted-by: LLM (Gemini 4 Neon)

@github-actions github-actions Bot added area:media Area from CODEOWNERS (bot) changes-requested A reviewer requested changes (bot) needs-hw-row No hardware row at the PR head (bot) needs-review No approving review from Core or Omarchy Mac yet (bot) needs-template PR body lacks Tested or Risk (bot) labels Nov 24, 2026
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) changes-requested A reviewer requested changes (bot) needs-hw-row No hardware row at the PR head (bot) needs-review No approving review from Core or Omarchy Mac yet (bot) needs-template PR body lacks Tested or Risk (bot)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants