Skip to content

accel/ane: h16, h17: give program memory and DVA back after close - #2240

Merged
DjDeveloperr merged 7 commits into
aurora-silicon:integration/aurorafrom
niteria:ane/h17-iova-free
Nov 20, 2026
Merged

DjDeveloperr merged 7 commits into
aurora-silicon:integration/aurorafrom
niteria:ane/h17-iova-free

Conversation

@niteria

@niteria niteria commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

On H17 (ane_fw_cold.c), program memory is never given back. Every ane_fw_program_map() and ane_fw_alloc() takes DVA from one 512 MiB window through a bump pointer (b->next_dva) that nothing ever lowers, and every mapping stays on b->mappings with its pages until reboot. The session layer (ane_h16_program.c) reuses a closed program's surfaces only for a later mapping of the same size and permission, and drops them once its 64-entry spare table is full. A workload with varied shapes takes fresh DVA for almost every program. On a J700, a sweep of random conv shapes made every H16_LOAD return -ENOSPC after about 340 loads, even for a 6 KB program that had passed earlier. The memory behind the window stayed allocated too. The H16 backend (ane_h16_backend.c, M4 class) has the same pattern with a 1 GiB window.

A second, smaller limit: LOAD, CREATE, TERMINATE, UNLOAD and gate commands are never released from the 256 KiB command arena (only EXECUTE batches are). That is 0x48c bytes per program, so about 210 load/close cycles fit in one firmware boot. After that h16_pool_room() returns -ENOSPC until something re-inits the transport: a system sleep, a recovery, or an idle park (#1863).

Six commits, one change each:

  • h16: give back the command arena of unloaded programs. At a LOAD that finds no program alive, the session records the arena mark. When the last program is torn down cleanly (nothing in flight, not quarantined), the arena goes back to that mark. A new firmware boot (session_restart) drops the mark.
  • h16: free spare program memory through the backend. Adds an optional ops->unmap.
    • Spares are kept oldest first. After every give-back and every LOAD, spares beyond 32 entries or 128 MiB are freed, and a full table frees instead of dropping.
    • A map that fails with -ENOSPC/-ENOMEM frees all spares and retries once.
    • A load that fails before the firmware saw it gives its memory back and frees the program. A LOAD or CREATE the firmware refused goes on s->held, so a recovery releases it.
    • Nothing is unmapped while the session is asleep or parked, because the DART flush must reach a live engine. The next LOAD trims instead.
    • Without ops->unmap, behaviour is unchanged. Both backends implement it in the commits below.
  • h17: return program DVA ranges and memory.
    • The window becomes a drm_mm (DRM_MM_INSERT_LOW, 16K alignment) under a new window_lock, which also guards b->mappings.
    • ane_fw_program_unmap() calls iommu_unmap() (the DART TLB invalidation happens inside it), then frees the pages and the range.
    • On a short unmap it keeps everything.
    • SHAREDMALLOC, pool, IPC, heap and preload are never unmapped.
  • h16: free private memory that never reached the IOMMU. ane_h16_alloc_prot() put a mapping on h->mappings before it allocated anything. When a 16K block, the vmap() or the sg table failed, the blocks already allocated stayed on the list, and after RUN nothing freed them: every program mapping that failed with -ENOMEM leaked its partial memory until reboot. The memory is now freed at once, and the mapping is recorded only just before iommu_map_sgtable(). A mapping that iommu_map_sgtable() refused is still kept until reset, as before.
  • h16: return program DVA ranges and memory. Same shape as the H17 commit.
    • The 1 GiB program window (a bump pointer, h->program_next) becomes a drm_mm of offsets from h->program_start (DRM_MM_INSERT_LOW, 16K alignment). A new window_lock guards it and h->mappings, since SHAREDMALLOC can be mapped while a program surface is unmapped.
    • ane_h16_program_unmap() (ops->unmap) unlinks the program mapping, calls iommu_unmap() (the TLB invalidation of the three DARTs happens inside it), and only then frees the pages and the range. On a short unmap it keeps everything.
    • SHAREDMALLOC, preload, heap tail and identity MMIO mappings have no range in the window and are never unmapped.
    • Every path that powers the engine down (system sleep, cold or warm, the quarantined path, the recovery power cycle) puts the session to sleep first, and H16 never parks, so the session never calls ops->unmap with the engine off.
  • h16: give each program's commands a record of their own (Fixes: the arena commit). A program still loaded pinned the rewind mark, so with one resident program every other client's load/close leaked 0x48c bytes again. A program's LOAD and CREATE now go into one of 128 fixed records (1088 bytes each, 136 KiB) taken from the arena at the first LOAD of a firmware boot, and the record is freed once the UNLOAD is acknowledged. A gate outside a batch, and TERMINATE + UNLOAD at a clean close, are released from the end of the arena once acknowledged, so the end of the arena is back at the records whenever nothing runs. A new firmware boot frees all records.

The arena rewind assumes the firmware keeps no pointer into LOAD/CREATE command buffers once every program is UNLOADed. The EXECUTE arena already relies on the same assumption after completion. The device test below checks it.

Testing

KUnit (arm64 QEMU, KASAN/UBSAN): 265/265 on this branch. The new cases in ane_h16_program_test.c are:

Case What it checks
pool_rewind 1000 load+close cycles keep the arena at its base. A loaded program pins its commands, and quarantine keeps the arena. Fails without the rewind.
unmap_window 16 MiB window with a full spare table: the old code hits -ENOSPC, and 2000 cycles pass with ops->unmap.
unmap_pressure -ENOSPC frees the spares and retries.
unmap_bytes The 128 MiB cap frees the oldest spares first.
unmap_parked Nothing is unmapped while parked, and the next LOAD trims.
unmap_failed_map A failed map gives its memory back and frees the program.
unmap_refused_load A refused load is held, and recovery releases it.
pool_resident One program held and run every cycle while another client does 1000 load/run/close cycles: the arena stays put and every EXECUTE passes. Fails at cycle 198 with only the rewind commit.
pool_records_reuse, pool_records_full, pool_records_no_room, pool_placed_bounds, pool_records_restart Out-of-order closes reuse freed records; all 128 taken gives a clean -ENOSPC; no room at the first LOAD; placement bounds; a cold boot re-takes records and a lost program can't free a replayed program's record.

ane_fw_cold.c and ane_h16_backend.c are not built under KUnit; the backends' ops->unmap implements the contract these cases test with a fake backend. A W=1 build of drivers/accel/ane/ (the Neo config builds the H16 backend into ane_t6021) gives no warnings in the touched files, at every commit. The full tools/asahi/run-host-tests.py gives the same result on this branch as on its base.

H17

On a J700 (MacBook Neo, T8140), Aura 1.0.0b9, with ANE built in. These rows ran on a combined kernel, not on this exact head: it also carried #1768 (hang recovery), #1863 (idle park, with the close-while-parked fix) and a local non-contiguous allocation change. The third commit was resolved against that change: pages are freed with dma_unmap_sgtable + vunmap and mapped with iommu_map_sgtable(). On plain integration/aurora it applies as in this PR. Booted without cma=.

Test Result
2000 × (load, run, close) of an elementwise add, no idle gap, idle_park_ms=0 2000/2000 exact, all in one firmware boot. The old kernel fails at about 210 (arena).
500 × two programs alternating (A load, B load, close A, close B) 500/500 exact, no firmware complaints, 3000 loads in one firmware boot
About 610 loads of random conv shapes, well over 512 MiB of surfaces in total No -ENOSPC, no short unmap. The 6 KB program that used to fail still passes at the end, with byte-identical output.
camera denoise at 960x540 (37–66 MB programs), switching between three sizes, 11 runs All exact
Idle park, close while parked, one RTC deep sleep with two programs open, a deliberate hang Replays exact, the program closed while parked is released at the next wake, and the hang recovers in 141 ms

Kernel memory outside every /proc/meminfo bucket stayed flat across the run (about 104 MB before, 56–83 MB after 48 large loads). Nothing logged a WARN, an oops or a quarantine apart from the deliberate hang. (Two of my own compiler's conv tilings also hung the engine during the sweep. That was a bug in the tilings, not in this code, and the driver recovered both.)

H16

Run on an M4 MacBook Air (J713, T8132; firmware 26.6.2 / 25G83), with the Air's kernel (07e8b02, before #2135) plus these commits cherry-picked, Aura Stage 2 with aura#260 for the firmware hand-off. Programs with surfaces that are not a 16 KiB multiple were run with 16 KiB-sized sections: on H16 the driver currently passes the exact section size in EXECUTE, which the firmware rejects for those surfaces (a separate bug, fixed by fbalazsy's 90cf680 from #1916).

Test Result
Bring-up H16 stage 5 program window 10001f00000..10041efffff, the same as without the PR, no WARN
2000 × (load, run, close) of add-128 2000/2000 exact. Without the PR: -ENOSPC on EXECUTE after about 200 cycles (the command arena; H16 never parks, so nothing re-inits it)
51 conv programs cycled to 3.12 GiB of surfaces No -ENOSPC, 23 distinct DVAs, MemAvailable back within 8 MB
1047 programs (80 GiB of surfaces) under an 18 GB stress-ng --vm hog No -ENOMEM reached userspace, no OOM, memory fully back
One rtc1-woken deep sleep with a program open, then close Replayed cold (11 ms), 700 000 runs exact, clean close
Hang recovery (a hanging program) Recovered in 113–119 ms, later loads exact
Two clients, 120 s: one program held and run while the other cycles (and the reverse) With the last commit: 8340 and 2846 cycles, every held run exact. Without it: -ENOSPC at cycle 198
500 alternating rounds 500/500 exact

Notes

The commits have Assisted-by: LLM and niteria's Signed-off-by.

🤖 Generated with Claude Code

@RyanTheTide

Copy link
Copy Markdown
Contributor

Thanks, this is a clear fix for both limits. The arena rewind helps every backend. The give-back is opt-in through ops->unmap, so every H17 user gets it, and the ordering is right: nothing is unmapped while the engine is asleep or parked, and pages are freed only after iommu_unmap() returns in full.

The code looks good to us. Two things before it leaves draft:

  1. The H16 backend (ane_h16_backend.c) keeps the never-free window. We have an M4 Pro running Linux. If you add ops->unmap there in the same shape, we will run your 2000-cycle and random-shape rows on it. If you prefer, we can do that as a follow-up instead.
  2. Your device run carried accel/ane: h17: recover a hung engine and retry a failed boot #1768, accel/ane: h17: park an idle engine through runtime PM #1863 and a local allocation change. We will run the rows on our A18 Pro (Neo) at this exact head on plain integration: 2000 load/run/close cycles in one firmware boot, 500 alternating two-program rounds, about 600 random conv shapes with no -ENOSPC and the 6 KB program still exact, and one sleep with two programs open. We will post the results here.

And the Signed-off-by, as you noted.

@niteria niteria changed the title accel/ane: h17: give program memory and DVA back after close accel/ane: h16, h17: give program memory and DVA back after close Nov 19, 2026
@niteria

niteria commented Nov 19, 2026

Copy link
Copy Markdown
Contributor Author

Thank you for the review and for offering both runs.

  1. I've added the H16 change to this PR, in the same shape as H17:

    • 630d494 "accel/ane: h16: free private memory that never reached the IOMMU": a mapping that failed before iommu_map_sgtable() used to leak its partial memory after RUN.
    • 24073e1 "accel/ane: h16: return program DVA ranges and memory": a drm_mm program window and ops->unmap.

    They are compile- and review-checked only (W=1 clean, KUnit 259/259, host tests the same as the base) and haven't run on hardware. Your M4 Pro rows would be very welcome. The H16 rows I would expect are in the PR description.

  2. A plain-integration run on your Neo at this head would be great. The push was a fast-forward that only adds the two H16 commits, so the three H17/session commits are unchanged from a0dd6fb.

niteria will add the Signed-off-by before this leaves draft.

@RyanTheTide

Copy link
Copy Markdown
Contributor

Thanks, the two H16 commits match the H17 shape well. The range goes into h->mappings only just before iommu_map_sgtable(), the unmap path releases pages and range only after a full iommu_unmap(), and window_lock covers the SHAREDMALLOC path too. Fixing the partial-allocation leak is a good catch.

We will run the H17 rows on our A18 Pro (Neo) at this head on plain integration, and your H16 table on our M4 Pro, including the two-client case with lockdep on. Results will follow here.

The transport copies every command into a 256 KiB pool arena. Each
EXECUTE sequence gives its space back once the firmware reported the
request complete, but LOAD, CREATE, TERMINATE, UNLOAD and the gating
commands outside a request stay until the next firmware boot: 1164
bytes per program loaded and closed. About 210 programs into one
firmware boot, h16_pool_room() refuses every LOAD with -ENOSPC. On H17
an idle park or a system sleep boots the firmware again and starts a
new arena; a client that keeps the engine busy, or a platform that
does not park, runs into it.

Take the arena position at a LOAD that finds no program alive in this
firmware boot. When the last program is torn down cleanly, with
nothing in flight and the session not quarantined, the firmware is
done with every command published since, so release the arena to that
position. A program still loaded keeps its commands and every later
one; a new firmware boot drops the position.

Assisted-by: LLM
Signed-off-by: Bartosz Nitka <niteria@gmail.com>
Program memory never goes back before reset. A closed program's
surfaces become spares, which serve only a later mapping of the same
size and permission; the table holds 64, and anything beyond it, or a
LOAD that failed before or at the firmware, keeps its memory mapped and
unused. A client that loads programs of many shapes (a sweep of
held-out conv layers, camera frames at several sizes) thus fills the
backend's DVA window: on the J700, 337 programs into a boot, every
H16_LOAD failed with -ENOSPC until reboot, and the memory behind the
window stayed allocated too.

Add an optional ops->unmap that unmaps a surface, frees its memory and
its DVA range, and use it for memory the firmware no longer uses:

- Keep the spares oldest first, and free the oldest beyond 32 entries
  or 128 MiB after every give-back and LOAD. A spare table that is full
  frees the surface instead of keeping it.
- When a mapping finds no room (-ENOSPC or -ENOMEM), free every spare
  and try once more.
- A LOAD that fails before the firmware saw the program gives its
  memory back and frees the program. One the firmware refused, at
  LOAD or CREATE, joins the programs held for the verified power cycle
  of the recovery, which gives them back.

The DART invalidation of an unmap must reach the engine, so nothing is
freed while the session is asleep or parked; the spares keep it until
the next LOAD, which runs with the engine up. Every caller holds the
session lock. Without ops->unmap nothing changes.

Assisted-by: LLM
Signed-off-by: Bartosz Nitka <niteria@gmail.com>
The H17 backend hands out program and SHAREDMALLOC mappings by bumping
a pointer through a 512 MiB DVA window, and never gives anything back:
once the window is used up, every program mapping fails with -ENOSPC
until reboot. On the J700 that happened after 337 program loads of a
held-out layer sweep and some camera-denoise trials, and from then on
every H16_LOAD failed, even of a 6 KB program that had run before.

Allocate window ranges from a drm_mm instead, and implement the
session's ops->unmap for program surfaces: find the mapping, unmap it
from the DARTs (iommu_unmap() invalidates their TLBs before it
returns), then free its pages and its range. A short unmap keeps the
mapping, its memory and its range, as a failed mapping does.
SHAREDMALLOC, pool, IPC, heap and preload mappings are never unmapped.

A failed iommu_map() no longer leaves a partial mapping at the DVA the
next allocation is given, since its range stays allocated with it.

Assisted-by: LLM
Signed-off-by: Bartosz Nitka <niteria@gmail.com>
ane_h16_alloc_prot() puts a mapping on h->mappings before it allocates
anything, so a 16K block, vmap() or sg table that fails leaves the
mapping and the blocks it already got on the list. Before RUN the
rollback frees them; from RUN on nothing does, and every program
mapping that fails with -ENOMEM (a fragmented buddy allocator, a
camera-sized surface) keeps its partial memory until reboot.

Free the memory of a mapping that fails before iommu_map_sgtable()
right away: nothing of it is visible to the engine. A mapping that
iommu_map_sgtable() refused is still recorded and kept until reset, as
before. The rollback frees mappings through the same helper.

Assisted-by: LLM
Signed-off-by: Bartosz Nitka <niteria@gmail.com>
The H16 backend hands out program mappings by bumping a pointer through
a 1 GiB DVA window and never gives anything back: once the window is
used up, every program mapping fails with -ENOSPC until reboot. A
client that loads programs of many shapes gets there, as the H17
backend did on the J700 after 337 loads of a held-out layer sweep.

Allocate program ranges from a drm_mm instead, and implement the
session's ops->unmap: find the program mapping, unmap it from the DARTs
(iommu_unmap() invalidates their TLBs before it returns), then free its
pages and its range. A short unmap keeps the mapping, its memory and
its range, as a failed mapping does. SHAREDMALLOC, preload, heap tail
and identity MMIO mappings have no range in the window and are never
unmapped.

The session calls ops->unmap only with the engine up, never while the
session is asleep for system sleep or a recovery power cycle, so the
invalidation reaches a powered engine. A new lock guards the mappings
list and the window, since SHAREDMALLOC can be mapped while a program
is unmapped.

Assisted-by: LLM
Signed-off-by: Bartosz Nitka <niteria@gmail.com>
@RyanTheTide

Copy link
Copy Markdown
Contributor

H17 rows on our A18 Pro (MacBook Neo, J700, T8140) at this head (049ae04), on plain integration as promised: integration/aurora 0abc2f3 with this PR merged. The test kernel also carried #1065, #1881 and #2143, which touch other drivers. It did not carry #1768 or #1863, and had no local allocation change. Firmware 25G83. Everything below ran in one firmware boot, with dmesg checked after each row.

Four of the five rows pass. The two-client row fails.

Row Result
Baseline: production kernel (no #2240), 400 × (load, run, close) of a 64-lane elementwise add Fails at load 190: -ENOSPC on every LOAD after that. This is the arena limit you described.
2000 × (load, run, close) of the add, no idle gap Pass. 2000/2000 exact against the expected output, 0 errors, 31.7 s. MemAvailable 5740 → 5935 MiB.
500 × two programs alternating (A load, B load, run A, run B, close A, close B; add + relu) Pass. 1000 loads, 1000/1000 exact.
600 mixed loads: MobileNet (8.5 MB HWX) + add + relu, cycled, 2179 MiB of surfaces in total (over 4× the 512 MiB window) Pass. No -ENOSPC, no short unmap. Every run of each program was byte-identical to its first run. Afterwards the small program loaded and ran exact.
One deep s2r (PMU RTC wake) with two programs open (add + MobileNet), then replay on the same handles Pass. Both replays were identical to before the sleep, and the ANE logged resume ok.
Two clients for 120 s: client 1 loops (load, run, close) on the add. Client 2 keeps MobileNet loaded and runs it in a loop. Fail. Client 1 gets -ENOSPC at load 189, the same point as the unpatched baseline. From then on client 2's EXECUTEs on its already-loaded program also fail with -ENOSPC (926 good runs, then nothing). Once both clients closed, the next rows ran normally, so the arena does come back when nothing is loaded.

No WARN, oops, quarantine, DART fault or short unmap was logged in any row.

Two-client finding. The arena rewind only happens when the last program is torn down ("At a LOAD that finds no program alive, the session records the arena mark"). Any long-lived program, such as a daemon or a model kept resident, therefore pins the mark. Every other client's load/close cycle then leaks its LOAD/CREATE/TERMINATE/UNLOAD commands (0x48c bytes each), exactly as before this PR. After about 190 cycles the arena is full, and it stops the resident client's EXECUTE batches too. So one client's churn can starve every other client until all programs close.

The rewind fixes the single-client case. The multi-client case needs the commands of an unloaded program freed per program, rather than a single mark. For example:

  • a small free list or bitmap allocator for the LOAD/CREATE/UNLOAD records in the 256 KiB arena, released at UNLOAD once the firmware has acknowledged it; or
  • a separate arena for program-lifetime commands, so EXECUTE space can never be eaten by them.

Either way, an EXECUTE on a loaded program should not fail because another client loaded programs. A KUnit case for this would be one program held while another does N > 210 load/close cycles.

The M4 Pro (H16) table, including this two-client case with lockdep on, follows when our slot on that machine comes up.

Raw row lines
base (production 7.3.0-40, no #2240):
FINAL base-cycle loads=191 runs=189 errors={'EXEC:ENOSPC': 2, 'LOAD:ENOSPC': 209} first_fail=[EXEC:ENOSPC at load 190]
test kernel (0abc2f385e54 + #2240 049ae047):
FINAL cycle2000 loads=2000 runs=2000 errors=0 mismatches=0 surfaces_MiB=281 MemAvailable_MiB=5740->5935 t=31.7s  PASS
FINAL alt500 loads=1000 runs=1000 errors=0 mismatches=0 surfaces_MiB=97 MemAvailable_MiB=5935->5938 t=14.3s  PASS
SET distinct_programs=3 min_KiB=56 max_MiB=8
FINAL-PROGRAM add64/program.hwx exact
FINAL mixed600 loads=601 runs=602 errors=0 mismatches=0 surfaces_MiB=2179 MemAvailable_MiB=5934->5886 t=17.6s  PASS
ACROSS rc=0 (deep s2r, PMU RTC wake)
FINAL hold-s2r loads=2 runs=4 errors=0 mismatches=0  PASS
two clients, 120 s:
FINAL two-loop loads=190 runs=188 errors={'EXEC:ENOSPC': 2, 'LOAD:ENOSPC': 1014362} first_fail=[EXEC:ENOSPC at load 189]  FAIL
FINAL two-runs loads=1 runs=926 errors={'EXEC:ENOSPC': 2094506} first_fail=[EXEC:ENOSPC at load 1]  FAIL
kernel per row: warn=0 oops=0 short=0 quarantine=0 dart=0

The rows use a small ABI 3 runner: open, H16_LOAD, H16_EXEC, close per cycle. Outputs are compared exactly against the expected file where there is one, otherwise against the program's first run.

Assisted-by: LLM (Claude:Opus 5.5)

The command arena went back to its start only once the last program of
the firmware boot was torn down. A program that stays loaded, such as a
model kept resident by a daemon, therefore pinned it: every other
client's load/close cycle still left its LOAD, CREATE, TERMINATE and
UNLOAD (1164 bytes) behind. After about 190 cycles every LOAD failed with
-ENOSPC, and so did every EXECUTE of the resident program, until all
programs were closed.

Keep a program's LOAD and CREATE, which the firmware may read while the
program lives, in a record of their own: 128 records of 1088 bytes,
taken from the arena at the first LOAD of each firmware boot. A record is
free again once the firmware acknowledged the program's UNLOAD, after
which it keeps no pointer into the program's commands (the previous
rewind relied on the same). A new firmware boot frees every record; a
refused LOAD or a quarantined session keeps its record until then.

Everything else goes to the end of the arena and now comes back as soon
as the firmware is done with it: an EXECUTE batch when it completes, as
before, and a gate, TERMINATE or UNLOAD outside a batch once
acknowledged. EXECUTE batches thus never compete with loaded programs for
space, and a LOAD with every record taken is a clean -ENOSPC before
anything is mapped or sent.

The transport gains a placed range for this: ane_fw_transport_reserve()
takes it once per firmware boot, ane_fw_transport_publish_at() copies a
command to a slot inside it, and ane_fw_transport_release() never gives
back below it.

Fixes: 0fb022b ("accel/ane: h16: give back the command arena of unloaded programs")
Assisted-by: LLM
Signed-off-by: Bartosz Nitka <niteria@gmail.com>
@niteria

niteria commented Nov 19, 2026

Copy link
Copy Markdown
Contributor Author

Thank you for running these, and for the two-client row in particular. You're right: the rewind only helps when every program goes away. One resident program pinned the mark, and every other client's load/close cycle then left its LOAD, CREATE, TERMINATE and UNLOAD in the arena, as before the PR. Your numbers match a KUnit replay of your row on 049ae04, and the same row on our M4 Air: the cycling client's LOAD and the resident program's EXECUTE both get -ENOSPC at cycle 198.

What changed. One new commit on top, f1623ba "accel/ane: h16: give each program's commands a record of their own" (Fixes: the arena commit). It goes on top so you can review it against what you tested; we can squash it into the arena commit before the merge if you prefer.

  • A program's LOAD and CREATE now live in a record of their own: 128 fixed records of 1088 bytes (136 KiB of the 256 KiB arena), taken once at the first LOAD of each firmware boot. A record is free again once the firmware has acknowledged the program's UNLOAD. A new firmware boot frees all of them. A refused LOAD and a quarantined session keep theirs until that boot, as they keep everything else.
  • Everything else still goes to the end of the arena, and now comes back as soon as the firmware is done with it. An EXECUTE batch is released when it completes, unchanged. A gate, TERMINATE or UNLOAD outside a batch is released once acknowledged. So the end of the arena is back at the records' end whenever nothing runs, and EXECUTE never competes with loaded programs for space. With every record taken, a LOAD gets a clean -ENOSPC before anything is mapped or sent. Records aren't the real limit, though: the shared-allocation cap (2 MiB EXPP pool per program) stops new programs long before 128.
  • The assumption is the same one the rewind relied on: once the firmware acknowledges a program's UNLOAD, it holds no pointer into that program's LOAD, CREATE, TERMINATE or UNLOAD. While the program is alive, its LOAD and CREATE stay in place, untouched.
  • Transport: ane_fw_transport_reserve() takes the range once per firmware boot. ane_fw_transport_publish_at() copies a command to a slot inside it. ane_fw_transport_release() refuses any mark below it.

Our earlier Air two-client row was flawed, so it didn't catch this. It ran h16-loop.sh 500 (load, run, close; a new Python process per cycle) next to ane-run.py --runs 20000, which held one program. The held program finished its 20000 runs in about 3 s (mean 146 us per run). That window covered only the loop's first few dozen cycles, far short of the ~200 it takes to fill the arena. The loop's remaining cycles ran alone, so the rewind hid the leak. Our new runner checks three things: the held program is loaded before the first cycle and closed after the last; the cycling client gets past 210 cycles; and no firmware boot or recovery falls inside the window, since a boot would reset the arena.

Tests

  • KUnit (arm64 QEMU, KASAN + UBSAN): 265/265. Six cases are new or rewritten:
    • Your row: one program held and run every cycle, while another client does 1000 load/run/close cycles. Each cycle leaves the arena where it was, the held program's LOAD and CREATE bytes stay unchanged, and every EXECUTE passes.
    • Single client, 1000 cycles: the arena is back at the records' end after each one.
    • Out-of-order closes across 24 programs over 50 rounds: freed records are reused lowest first, and the programs still loaded keep running.
    • Every record taken: a clean -ENOSPC with nothing sent or mapped, EXECUTE still works, and a record freed in the middle is reused in place.
    • No room for the records at the first LOAD: a clean -ENOSPC.
    • Placement bounds, and a release below the range is refused.
    • Cold boot: replayed programs take records again, and a program closed while asleep, lost with the old firmware, doesn't free a record that a replayed program now holds.
    • The resident-program case fails on 049ae04 (cycle 198, as above).
  • The full run-host-tests.py: 427/427 on the branch and 427/427 on 049ae04.
  • A W=1 build of drivers/accel/ane/ has no new warnings, and checkpatch --strict is clean.

M4 Air (J713, T8132, H16 firmware 25G83). Our Air kernel, which carries this PR's first five commits (19f7319), with and without this commit (the same patch, cherry-picked). Each row ran in one firmware boot. The window between the runner's kmsg marks was checked in the kernel journal (the ring buffer overflows during a row): no firmware boot or recovery, no -ENOSPC, quarantine or WARN. That Air kernel has no lockdep. Programs: add-128 (checked against its CPU reference) and a depthwise convolution, 128 channels at 64x64 (1 MiB surfaces; checked against its first output, so every run must be bit-identical).

Row Before this commit With this commit
Your row: hold the depthwise program and run it in a loop; the other client does load/run/close of add-128 for 120 s Cycle 198: EXECUTE then LOAD -ENOSPC, and the held program's EXECUTEs fail from then on (cycle 201 with a 19.6 MB held program) 8340 cycles, the held program ran 3948 times over the whole window, all exact
Reverse: hold add-128, cycle the depthwise program for 120 s 2846 cycles, the held program ran 96275 times, all exact
2000 × (load, run, close) of add-128 2000/2000 exact
500 alternating rounds: A (add-128) loads and runs, B (depthwise) loads and runs, A runs and closes, B runs and closes 500/500 exact

We haven't rerun the H17 rows on our Neo with this commit yet. Could you rerun the two-client row on your Neo at the new head? And when your M4 Pro slot comes up, it would help to include the same case in the H16 table, with lockdep on. The other four H17 rows exercise code this commit only touches at LOAD and close, but they're cheap to repeat if you're running anyway.

@RyanTheTide

Copy link
Copy Markdown
Contributor

H16 rows on our M4 Pro (J616s, T6040) at this head (049ae04). Kernel: integration/aurora 0abc2f3 with this PR merged (also #1065, #1881 and #2143, which touch other drivers), built with PROVE_LOCKING, DEBUG_LOCKDEP and DEBUG_ATOMIC_SLEEP. It booted twice; the second boot was used to run the two-client row with lockdep still armed (see the lockdep note below). dmesg was checked after each row and MemAvailable noted around each one.

The new H16 code works for single clients: give-back, DVA reuse and memory return. The two-client row fails the same way as on H17. Lockdep found one report, and it is not from this PR.

Row (your H16 table) Result
Bring-up H16 stage 5 program window 10001f00000..10041efffff, the same range as the production kernel on this machine. No WARN from the ANE.
Baseline: production kernel, (load, run, close) of a small program Fails at load 202 with -ENOSPC.
2000 × (load, run, close) of a small program Pass. 2000/2000, 0 errors, 23.3 s. MemAvailable 21123 → 21377 MiB.
Random conv shapes well past 1 GiB Pass. 121 loads cycling 29 conv programs from our open compiler (960x540 denoise layers and 2–8 layer chains, 9–63 MiB of surfaces each as LOAD reports them) plus MobileNet: 4334 MiB in total, over 4× the window. No -ENOSPC, no short unmap, every repeat run byte-identical to its first run, and the small program exact afterwards. Every H16 program mapping line in the log is in the lowest ~135 MiB of the window (47 distinct DVAs), so ranges are being reused. MemAvailable dropped 21406 → 19243 MiB during the row and was back at 21305 MiB afterwards.
Large programs under memory pressure (stress-ng --vm 2 --vm-bytes 75%) 61 loads (2167 MiB) while MemAvailable sat at 6–7 GiB: 0 errors, memory back afterwards. This box has 48 GB, so maps never hit -ENOMEM and the flush-and-retry path was not exercised. It needs a tighter hog, or a fault-injection hook, to cover it.
One s2idle (RTC wake) with two programs open, then replay and close Pass. Replays identical, closes clean.
Hang recovery Not run: we had no hang-inducing H16 program in this set.
Two clients (two load/close loops plus one client running a resident MobileNet), 180 s, lockdep armed Fail. The loop clients get -ENOSPC at load 133 (boot 1) and 135 (boot 2). After that the resident client's EXECUTEs fail with -ENOSPC too (277–285 good runs, then nothing). This is the same arena problem as on H17. No lockdep report in this row, and debug_locks stayed 1, so the SHAREDMALLOC maps overlapping the unmaps are lockdep-clean.

Lockdep (not from this PR). On the first boot the s2idle row produced one report, possible circular locking dependency detected:

  • ane_h16_pin() calls register_pm_notifier(), which takes the PM chain rwsem, while ane_h16_lock is held (via ane_h16_scratch_boot ← ane_h16_advance ← ane_h16_start_work).
  • ane_h16_pm_event() takes ane_h16_lock from inside the PM notifier chain.

That is an ABBA between bring-up and a suspend that starts at the same moment. The code is identical on the base and on our current production kernel, so it predates this PR. Registering the notifier once at probe, or outside ane_h16_lock, would remove it. I can open that as a separate PR if you like. Because lockdep switches itself off after the first report, I rebooted and ran the two-client row first, with lockdep still armed. That is the run in the table.

So for H16 the summary is the same as for H17: one client at a time is fixed, but a resident program still lets another client's load/close cycles fill the command arena and starve everyone. The per-program command release suggested in the H17 comment covers both backends, since the arena code is shared.

Raw row lines
production fleet75: FINAL base-cycle loads=204 runs=201 errors={'EXEC:ENOSPC': 3, 'LOAD:ENOSPC': 196} first_fail=[EXEC:ENOSPC at load 202]
boot 1 (ff5935c4):
FINAL cycle2000 loads=2000 runs=2000 errors=0 mismatches=0 surfaces_MiB=281 MemAvailable_MiB=21123->21377 t=23.3s  PASS
ACROSS rc=0 (s2idle, rtc wake)
FINAL hold-s2idle loads=2 runs=4 errors=0 mismatches=0  PASS   (lockdep report during this suspend, see above)
FINAL two-loop loads=134 errors={'EXEC:ENOSPC': 2, 'LOAD:ENOSPC': 1482189} first_fail=[EXEC:ENOSPC at load 133]  FAIL
FINAL two-runs loads=1 runs=285 errors={'EXEC:ENOSPC': 3070690}  FAIL
boot 2 (4ccd58c6), lockdep armed (debug_locks=1 throughout):
FINAL two-loop loads=136 errors={'EXEC:ENOSPC': 2, 'LOAD:ENOSPC': 1409644} first_fail=[EXEC:ENOSPC at load 135]  FAIL
FINAL two-runs loads=1 runs=277 errors={'EXEC:ENOSPC': 2497306}  FAIL
SET distinct_programs=30 distinct_surfaces_MiB=1083 min_KiB=9126 max_MiB=63
FINAL convset loads=121 runs=122 errors=0 mismatches=0 surfaces_MiB=4334 MemAvailable_MiB=21406->19243  PASS (small program exact)
FINAL pressure loads=61 runs=62 errors=0 mismatches=0 surfaces_MiB=2167 MemAvailable_MiB=7445->6033  PASS (no -ENOMEM reached)
after pressure: MemAvailable=21305MiB; debug_locks=1; avc=0
kernel per row: warn=0 oops=0 short=0 quarantine=0 dart=0

Assisted-by: LLM (Claude:Opus 5.5)

@RyanTheTide

Copy link
Copy Markdown
Contributor

The two-client row now passes on both chips at f1623ba. Thanks for the record-per-program change.

Kernels: integration/aurora 2c35f50 with this PR (f1623ba) merged, plus #2505 (the lockdep fix below), with PROVE_LOCKING. Each machine ran everything in one boot.

Row A18 Pro (Neo, J700, H17) M4 Pro (J616s, T6040, H16)
Two clients, 180 s: one loop of (load, run, close) on the small add; one MobileNet kept loaded and run in a loop 9183 cycles, 37554 held runs, 0 errors 14437 cycles, 28548 held runs, 0 errors
Three clients, 180 s: add loop + MobileNet loop + held MobileNet 3544 + 3193 cycles (28 GiB of surfaces), 12049 held runs, 0 errors 4997 + 3246 cycles (28.9 GiB), 9908 held runs, 0 errors
Two programs held across one system sleep (Neo deep s2r, J616s s2idle), then replay Identical, resume ok Identical, cold replay of 2 programs in 11 ms
Two clients, 120 s, after the wake 6151 cycles, 25642 held runs, 0 errors 9548 cycles, 19186 held runs, 0 errors

No row logged a WARN from the ANE, an oops, -ENOSPC, a short unmap, a quarantine, a DART fault or a lockdep report. Before this head, the same row failed at load 189 (H17) and 133/135 (H16).

The lockdep report we mentioned in the M4 Pro results (register_pm_notifier() under ane_h16_lock) is pre-existing and not from this PR. H17 has the same inversion under b->lock. Both are fixed in #2505, which merges cleanly with this branch.

Still not run on our side: the H16 -ENOMEM flush-and-retry path (the 75 % stress-ng hog on the 48 GB machine never got there) and a hang-recovery row on this head.

Raw row lines
Neo, 7.3.0-aurora-anefix1 (dba39b4e = 2c35f509 + #2240 f1623ba3 + #2505):
boot-lockdep: reports=0 debug_locks=1
two-a  FINAL two-a-loop loads=9183 runs=9183 errors=0 mismatches=0 surfaces_MiB=1291 MemAvailable_MiB=5744->5827 t=180.0s
       FINAL two-a-runs loads=1 runs=37554 errors=0 mismatches=0 t=180.1s
two-b  FINAL two-b-loopbig loads=3193 runs=3193 errors=0 surfaces_MiB=28456 t=180.0s
       FINAL two-b-loop loads=3544 runs=3544 errors=0 surfaces_MiB=498 t=180.0s
       FINAL two-b-runs loads=1 runs=12049 errors=0 t=180.0s
hold   ACROSS rc=0 (deep s2r, PMU RTC wake) FINAL hold-sleep loads=2 runs=4 errors=0 mismatches=0
two-c  FINAL two-c-loop loads=6151 runs=6151 errors=0 t=120.0s; FINAL two-c-runs loads=1 runs=25642 errors=0
END debug_locks=1 lockdep_all=0 avc=0

J616s, 7.3.0-aurora-anefix2 (same source, fleet config):
boot-lockdep: reports=0 debug_locks=1
two-a  FINAL two-a-loop loads=14437 runs=14437 errors=0 mismatches=0 surfaces_MiB=2030 MemAvailable_MiB=21221->21386 t=180.0s
       FINAL two-a-runs loads=1 runs=28548 errors=0 t=180.0s
two-b  FINAL two-b-loopbig loads=3246 runs=3246 errors=0 surfaces_MiB=28928 t=180.0s
       FINAL two-b-loop loads=4997 runs=4997 errors=0 surfaces_MiB=702 t=180.0s
       FINAL two-b-runs loads=1 runs=9908 errors=0 t=180.0s
hold   ACROSS rc=0 (s2idle, rtc1) FINAL hold-sleep loads=2 runs=4 errors=0 mismatches=0
       H16 ANE: resume ok: cold, firmware ready 10 ms, first program ready 10 ms, node reopened 11 ms, 2 programs replayed, 0 restarts
two-c  FINAL two-c-loop loads=9548 runs=9548 errors=0 t=120.0s; FINAL two-c-runs loads=1 runs=19186 errors=0
END debug_locks=1 lockdep_all=0 avc=0

The runner is the same ABI 3 tool as before: open, H16_LOAD, H16_EXEC, close per cycle; the held program's output is compared with its first run, the add with the expected file.

Assisted-by: LLM (Claude:Opus 5.5)

@DjDeveloperr DjDeveloperr left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks @niteria, this is a really nice piece of work, and thanks for turning the two-client failure around so quickly with the per-program records.

I read the series at f1623ba. The lifetime rules hold up:

  • Surfaces only reach ops->unmap from the spare table.
  • Spares only come from programs whose TERMINATE/UNLOAD was acknowledged, that the firmware never saw, that were lost with an old firmware, or that were held until a verified power cycle.
  • Mmapped surfaces wait for their last export.
  • Both backends free pages and the range only after a full iommu_unmap().

The records allocator, the epoch guard in h16_record_put() and ane_fw_transport_release() refusing to go below the placed range all look right to me. window_lock stays a leaf in both backends. Ryan's rows on the Neo and the M4 Pro at this head (two and three clients, sleep, lockdep together with #2505) cover what I was worried about. It merges cleanly with #2505 (now on integration as 1473d15), so no rebase is needed.

One small thing I'd like, as a fixup here or a follow-up:

  1. h16_can_unmap() (ane_h16_program.c:743) treats "not asleep and not parked" as "engine up". On H17 a quarantined engine can be powered off while the session is open:

    • The QUARANTINED branch of ane_fw_sleep_prepare() (ane_fw_cold.c:1068) powers down without ane_h16_session_sleep(true).
    • A failed wake (ane_fw_cold.c:1033-1039) reopens the session after ane_fw_quarantine().

    If an export is dropped in that state, h16_export_work() can trim spares and reach iommu_unmap() and the DART invalidation. ane_fw_power_down_staged() says that must not happen until ANE_SYS is back. It's narrow (it needs a lost program with a live mmap), but && !s->quarantined closes it, and it also matches "a quarantined session keeps every mapping". Once the quarantine ends, the next LOAD trims as it does after a park. An EXPECT_FALSE(f->s.quarantined) in fw_unmap() would pin it.

Follow-ups, not blocking:

  1. When the spare table is full and unmap isn't allowed, h16_give_spare() (ane_h16_program.c:786) still drops the surface for good. ane_h16_session_recovered() gives back every held program while the session is asleep, so a recovery with more than about 32 held surfaces leaks their memory and DVA until reboot. Keeping the overflow on a list for the next trim, or giving held programs back at the reopen, would close the last leak.
  2. ane_h16_alloc_prot() keeps the pages and the range when iommu_map_sgtable() fails (ane_h16_backend.c:1100), even though the call already unmapped what it mapped. Under real memory pressure each failed retry pins size bytes until reboot. Freeing them there looks safe to me.
  3. The first bullet of the PR description (and the message of the first commit) still describes the single rewind mark that the last commit replaced. Our merges keep the in-series Fixes: valid, so squashing is up to you, but please refresh the description so it matches the code.
  4. A merge note: #2505 added a notifier-failure path in ane_h16_backend_probe() that does kfree(h) without the drm_mm_takedown() you added to the other two. It's harmless (the window is empty there); I'm only mentioning it for symmetry.

Two rows are still open, and both are fine to run after merge: the H16 -ENOMEM flush-and-retry on hardware (on the session side, unmap_pressure covers the same branch) and a hang-recovery row at this head.

Approving. Thanks again!

@RyanTheTide

Copy link
Copy Markdown
Contributor

Build check of head 811f6d3 (the merge with integration/aurora). The integration CI build does not compile drivers/accel/ane, so this covers it.

  • arm64, LLVM=1 (clang/LLD 22.1.6, rustc 1.97.1). The config is the fleet kernel config with debug info off. CONFIG_DRM_ACCEL_ANE=m with the H16 validation and KUnit modules, plus CONFIG_DRM_ASAHI=m. Image modules dtbs builds cleanly.
  • W=1 on drivers/accel/ane: 0 warnings in the touched files (and 0 in the whole directory, 40 objects). The .cmd files confirm -Wextra and -Wmissing-prototypes were in effect.
  • The full build prints 26 rustc dead-code warnings, all in drivers/gpu/drm/asahi and drivers/soc/apple. This PR touches neither, and none are in drivers/accel/ane.

Full build log, W=1 log and all module hashes: https://gist.github.com/RyanTheTide/53fde0a01ba687cd1204b44dc08daab2

Module (7.3.0-aurora-anebuild) sha256
ane.ko 5c5de0cfb14917f0cbcae022d3f9690575c9818dfe519d135fa1d3d2c0cb5460
ane_t6021.ko 3e833b509a37da29590b900378fad4de3a82569ef75133c0c05d5166e849d2a5
ane_h16_program.ko cb6b6890c0d8509186b6f62c97f37c59eb0e4d9ac9fdf264a7aae489a628c773
ane_h16_load.ko 7882b2365644eea6285202a07a13c4c7fe5dc6e5136e761a7b6770d7ceeda2a3
ane_h16_preload.ko f259d47f5fa98cd232b27093086c10601eff84f7495de652ba74154924884912
ane_h16_hwx.ko 83a747c1ab1a1b7c193c3cf0f418e98e454ddc9b25875ef0cdf217346d47f62e
ane_h16_program_test.ko fa7e06480d5c6d4084f63901456923f421171c3b1593b1367972387eb383d21e
aurora.ko 2504bde8f328e57e7eebcdb40334ababd7a4184084e5b9d2ce73d321ce2115c2

This head's tree (17024f51) is identical to the local merge we ran the two-client, three-client and sleep rows on (comment above). We did not run a separate hardware boot of this exact commit.

@DjDeveloperr
DjDeveloperr merged commit 0a7c49b into aurora-silicon:integration/aurora Nov 20, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants