Skip to content

accel/ane: h16: take a program's process pool before LOAD - #2897

Open
RyanTheTide wants to merge 2 commits into
mainfrom
ane/h16-process-pool-before-load
Open

RyanTheTide wants to merge 2 commits into
mainfrom
ane/h16-process-pool-before-load

Conversation

@RyanTheTide

@RyanTheTide RyanTheTide commented Nov 23, 2026 •

Copy link
Copy Markdown

Problem

Every CREATE makes the H16/H17 firmware ask the host for a 2 MiB process pool (SHAREDMALLOC) and wait for the answer before it replies. The host caps shared allocations at 12 MiB (ANE_FW_SHARED_TOTAL). A request the host cannot serve, because the cap is reached or memory is short, is never answered. The transport fails, the CREATE is lost and the session is quarantined, and every other loaded program goes with it. On H16 the recovery power cycle loses the loaded programs. On H17 the session stays quarantined until reboot.

Change

  1. accel/ane: h16: take a program's process pool before LOAD (Bartosz Nitka's commit from niteria/linux ane/h17-cma-enomem, ported onto the current tip). Before a LOAD or a replay, the host makes sure a retired EXPP allocation is there, allocating one now if needed. The firmware's request then reuses it, the same way it reuses a pool given back by FREE. Without memory or quota, the LOAD fails with -ENOMEM or -ENOSPC before any firmware traffic. The engine stays up and the other programs keep running. The port's only conflicts are with the per-program command records (accel/ane: h16, h17: give program memory and DVA back after close #2240): the replay now takes the pool before its record.
  2. accel/ane: fail the transport when a reserved shared pool is invalid. The reserve path runs the same surface checks as the request path but only returned -EPROTO, leaving the backend's mapping unrecorded, so each later LOAD mapped another one. It now fails the transport and quarantines the session, as the request path does.

No chip or board checks. Selection is by the shared-allocation tag and size the firmware asks for. No new parameters.

Testing

  • KUnit (arm64 qemu, ane.kunitconfig, this head): 303 run, 302 pass, 1 skip (apple_g17_rx_util_min_test, needs CONFIG_UCLAMP_TASK, unrelated).

  • Same run with both fix bodies reverted (tests kept): 4 fail, exactly the new cases: ane_transport_shared_reserve_invalid_test, program_process_pool_quota_test, program_process_pool_nomem_test, program_process_pool_late_free_test.

  • J616s (M4 Pro, H16), fleet81 default kernel, this driver loaded as modules, one boot per arm, default command line apart from the one-shot module blacklist used to swap the driver. Eight programs opened and kept, then churn at the quota edge, then 200 load/run/close cycles:

    • with this PR: four programs resident, the next LOADs fail with -ENOSPC before any firmware traffic, the residents keep running (80/80 exact), 20/20 fresh cycles after close, 150/150 edge churn, cycle200 PASS. No refused SHAREDMALLOC, no quarantine, no engine reset in the journal.
    • without it: every LOAD past the quota is refused by the host inside CREATE (54 times in the run), each one quarantines the session and resets the engine; the residents' EXECs fail with -EIO.
    • s2idle (20 s, rtc1 wake) with this PR: suspend and resume clean, LOAD works after wake.
  • Neo (A18 Pro), fleet82 default kernel, same method and rows:

    • with this PR: four programs resident, the next LOADs fail with -ENOSPC up front, residents 80/80, 20/20 after close, 150/150 edge churn, cycle200 PASS. No refused SHAREDMALLOC, no quarantine.
    • without it: the first refused request quarantines the session for the rest of the boot. Every later LOAD and EXEC returns -EIO (residents 0/80, fresh programs 0/20, cycle200 0/200).
  • J713 (M4 Air), fleet81, same method: user-space rows identical with and without this PR (five resident, the rest -ENOSPC, everything else passes). The journal differs: without it, three refused SHAREDMALLOCs, each quarantining the session and resetting the engine (the replay keeps the residents on this Mac). With it, zero refusals and three clean "no process pool" refusals before CREATE.

Still to run

  • An M5 (H17) row. The H17 firmware asks for the same pool, but none of our Macs has H17.
Reviewer notes
  • Locking: LOAD holds the session lock from the reservation through CREATE, so a second LOAD cannot take the reservation in between. Replay runs with the session asleep.
  • Accounting: the ANE_FW_SHARED_MAX and ANE_FW_SHARED_TOTAL checks moved unchanged into ane_fw_shared_add(). Bytes are counted once at add, never on reuse. The reply handle i + 1 equals the old post-increment shared_count.
  • A reserved pool is retired, so ane_fw_transport_report() and the tagged lookups skip it. It is zeroed again when served. Nothing frees shared backing before teardown.
  • Restart and resume carry every shared allocation over as retired (carry_shared), so a replay of N programs finds N retired EXPP pools and never allocates.
  • An EXPP request of another size, or a second pool per CREATE, would still reach the refusal path. That is the "request the host did not foresee" case, and it fails the transport as before.
  • Without the fix, the containment of an unexpected refusal is linux#2854's subject. This PR does not change it.

niteria and others added 2 commits November 23, 2026 09:41
Every CREATE makes the H16/H17 firmware ask for a 2 MiB EXPP process
pool by SHAREDMALLOC, and it answers the CREATE only once that request
was served. A request the host cannot serve, for lack of memory or of
the 12 MiB shared quota, is never answered: the transport fails, the
CREATE is lost, and the session is quarantined, taking every other
loaded program with it.

The host knows the request is coming, so take the pool first: before
a LOAD (and a replay), make sure a retired EXPP allocation is there,
allocating one now and keeping it retired if not. The firmware's
request then reuses it as it reuses any pool given back by FREE. With
no memory or no quota the LOAD now fails with -ENOMEM or -ENOSPC
before any traffic, the engine stays up and every other program keeps
running. A FREE of a torn-down program's pool that is still posted is
served first, and the reservation tried once more.

The firmware's request still goes through the same checks; a request
the host did not foresee and cannot serve fails the transport as
before.

KUnit: past the quota a LOAD fails with -ENOSPC and no command sent,
the other programs keep running and a closed program's pool serves the
next LOAD; no memory gives -ENOMEM the same way; a FREE posted after
the settle is served by the next LOAD; and at the transport level a
reserved allocation serves its request without an allocation and a
refused reservation leaves the transport working. The fake firmware
now asks for the pool during CREATE and frees it after UNLOAD.

[ported onto the per-program command records: the replay takes the
 pool before its record instead of after an arena room check]
Signed-off-by: Ryan Murray <ryan@aurorasilicon.org>
A shared allocation the backend hands back for a firmware request is
checked (16 KiB aligned DVA, exact size, no overlap with the IPC area,
the command pool or another allocation), and one that fails the check
fails the transport. The same check runs when a LOAD reserves its
process pool ahead of the request, but there it only returned -EPROTO:
the backend's mapping is not recorded, so every later LOAD made and
dropped another 2 MiB mapping that the backend keeps until teardown.

Fail the transport there as well, and quarantine the session when a
reservation finds the transport failed, as the request path does.

KUnit: a misaligned backend surface fails the reservation with -EPROTO,
fails the transport, records nothing, and a second reservation does not
allocate again.

Signed-off-by: Ryan Murray <ryan@aurorasilicon.org>
@RyanTheTide

Copy link
Copy Markdown
Author

Review at 871c584.

Review at 871c584.

Reserving the pool before LOAD is the right shape: the foreseen request can no longer go unanswered. A few problems remain:

  1. ane_h16_program.c:2110: in h16_replay(), an -ENOSPC/-ENOMEM from h16_process_pool() returns before h16_fail(). Both replay callers (ane_h16_backend.c:587, ane_fw_cold.c:814) then log "session quarantined", but the session is not quarantined. Route the error through h16_fail() like the other replay failures, or fix the message.
  2. ane_h16_program.c:646: the reservation guesses the request (SZ_2M, ANE_FW_SHARED_EXPP). If a firmware asks for another size or tag, every LOAD adds a retired pool nothing can reuse. Each one takes 2 MiB of the 12 MiB quota and makes the refusal this PR is meant to prevent more likely. Please reserve what this firmware last asked for in a CREATE (record the size and tag at the first served EXPP request), or say why 2 MiB holds for every H16/H17 firmware this driver takes.
  3. This removes the wedge only for that one foreseen request. Any other request refused inside CREATE/EXEC still reaches the old path, so accel/ane: h16: keep a refused SHAREDMALLOC off the fatal path #2854's containment is still needed. Please say in the body which of the two lands first. They conflict in ane_h16_program_test.c (the fake firmware now asks for the pool in every CREATE).
  4. The ceiling is still the host constant ANE_FW_SHARED_TOTAL (12 MiB), which also fixes the H16 DVA layout (ane_h16_backend.c:1001,1896). That leaves four resident programs for the whole device, and it is not accounted per client: one process with four fds gets -ENOSPC for everyone else. The source branch has a follow-up that raises the cap to 32 MiB. Either carry it, or derive the cap from the DVA window.
  5. ane_h16_program.c:649: every LOAD at the quota edge settles for up to 100 ms with the session lock held, which stalls other clients' EXECs. A client looping LOAD at the edge can stall the engine for everyone.
  6. The retry assumes a closed program's FREE arrives while the engine is idle. The commit's own comment says it often comes "only during a later program". J616s shows that on H16, but H17 (allow_free, DVA|1 FREE reply) is unverified.
  7. Commit 56b0be9 is authored by Bartosz Nitka but carries only the porter's Signed-off-by. It needs the author's Signed-off-by.
  8. Rows owed at this head: H17 (M5) load-to-quota, close/reload and s2idle-with-replay; J713 (H16) load/unload loop and s2idle. The body lists both as still to run.

@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 review at 871c584c86805616005b6dcb62fd018454b32da7 (host-tests: green).

Pre-reserving the process pool before LOAD is the right architectural fix, and the KUnit coverage (302/303 pass, 4/4 mutant kills) plus H16 A/B evidence (J616s, Neo, J713) are strong. Three blockers remain:

Blockers

  1. §4 Missing author Signed-off-by: on commit 56b0be94cc922b045f04de4591a89ecc368e214b:
    The commit is authored by Bartosz Nitka <niteria@gmail.com> (ported from niteria/linux), but carries only Signed-off-by: Ryan Murray <ryan@aurorasilicon.org>. Please include the author's Signed-off-by: Bartosz Nitka <niteria@gmail.com> trailer.
  2. §2 h16_replay() returns without quarantining the session when h16_process_pool() fails (drivers/accel/ane/ane_h16_program.c:2110-2112):
    If h16_process_pool(s) returns -ENOSPC or -ENOMEM (without setting s->transport->error), h16_replay() returns directly instead of calling h16_fail(s, ret). Both callers (ane_h16_backend.c:589, ane_fw_cold.c:816) log "PROGRAM REPLAY FAILED ...; session quarantined" and ane_h16_session_replay()'s docstring requires quarantining on any failed replay, yet s->quarantined remains false after a partial replay. Route the error through h16_fail(s, ret).
  3. §1 / §5 Missing H17 (M5) hardware row & explicit §5 regression counts:
    • The change targets both H16 and H17 (ane_fw_cold.c, allow_free), but no H17 (M5) load-to-quota / close-reload / s2idle-replay row has been run yet (listed under "Still to run").
    • Please include explicit §5 regression counts (0 SELinux denials, 0 failed units, 0 new kernel errors) for the J616s, Neo, and J713 hardware rows.

Non-blocking notes

  • Hardcoded (SZ_2M, ANE_FW_SHARED_EXPP) reservation (ane_h16_program.c:646): If a firmware build ever requests a different EXPP pool size or tag, the first LOAD pins one unused 2 MiB retired slot out of the 12 MiB ANE_FW_SHARED_TOTAL quota (subsequent LOADs see that retired entry in ane_fw_shared_reusable() and do not allocate again, so it doesn't leak per LOAD, but 2 MiB stays wasted). Recording the observed EXPP size/tag or documenting why 2 MiB holds across all H16/H17 builds would make this cleaner.
  • Quota-edge lock hold (ane_h16_program.c:649): Calling ane_fw_transport_settle() (up to 100 ms) under the session lock on every -ENOSPC LOAD can stall concurrent EXECs if a client retries LOAD in a loop at the quota edge.
  • Merge ordering with #2854: Note in the PR body which PR lands first given the overlapping ane_h16_program_test.c fake-firmware changes.

Checklist summary

  • §1 No model gating: Pass in code; blocked on H17 (M5) hardware row.
  • §2 Production-ready: Blocked on h16_replay() missing h16_fail() quarantine path.
  • §3 Safety: Pass.
  • §4 Clean-room & attribution: Blocked on missing author Signed-off-by: in 56b0be94cc92 (zero AI trailers in commits; Assisted-by: in PR body).
  • §5 Evidence: Strong KUnit (302/303 + 4/4 mutant kills) and H16 A/B runs; blocked on H17 (M5) row and explicit §5 regression counts.
  • §6 Naming: Pass.

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) merge-conflict Conflicts with the base branch (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
@github-actions

Copy link
Copy Markdown

Merge readiness at 871c584c: not ready yet

Check Detail
CI at head ok 1 check passed
Review no needs one approving review from Core or Omarchy Mac (not the author)
Requested changes no requested by DjDeveloperr
Hardware row at head no no line with the head SHA 871c584c, a Mac and a result in the PR body or a comment
Conflicts no merge conflicts with main: merge it in (no force push)
PR text adopt when you next edit add a 'Tested:' line; a 'Risk:' line (door and Macs) (guide)

Areas: area:media

Updated by pr-readiness on every push, edit and review, and every 30 minutes. Rules: merge policy · labels.

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) merge-conflict Conflicts with the base branch (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