Skip to content

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

Open
niteria wants to merge 5 commits into
aurora-silicon:integration/aurorafrom
niteria:ane/h17-iova-free
Open

niteria wants to merge 5 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).

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

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): 259/259 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.

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

The two H16 commits are compile- and review-checked only. They have not run on hardware: I have no H16 machine running Linux. Rows to run on an M4 (T8132/T6040/T6041), with dmesg watched for H16/ane/dart and MemAvailable noted around each step:

Test Expected
Bring-up H16 stage 5 program window X..X+3fffffff, the same range as before, no WARN
2000 × (load, run, close) of a small program All exact. The H16 program mapping <dva> lines stay at the bottom of the window (the same few DVAs), no -ENOSPC, no program unmap short, no WARN
Random conv shapes, well past 1 GiB of surfaces in total No -ENOSPC (the old code fails every LOAD after about 1 GiB), MemAvailable back near the baseline at the end
Large programs under memory pressure (e.g. a stress-ng --vm hog) -ENOMEM maps flush the spares and retry; once the hog stops, the memory comes back and nothing stays leaked from failed maps
One deep sleep with a program open, then close it; a hang recovery if one can be triggered Replays exact, the close after resume unmaps, held programs are released by the next LOAD after a recovery
Two clients: one in a load/close loop, one running a loaded program Exact results, no lockdep splat (SHAREDMALLOC maps overlap the unmaps)

Notes

The commits have Assisted-by: LLM and no Signed-off-by yet. niteria will add the sign-off before this leaves draft.

🤖 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 #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>
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.

2 participants