Skip to content

media: apple: ave: bound the sessions kept for a power cycle beside a running one - #3231

Merged
RyanTheTide merged 3 commits into
mainfrom
fix/ave-bound-stopped-sessions
Dec 1, 2026
Merged

RyanTheTide merged 3 commits into
mainfrom
fix/ave-bound-stopped-sessions

Conversation

@DjDeveloperr

@DjDeveloperr DjDeveloperr commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

What: A stopped AVE session keeps its DMA buffers until the coprocessor is proved to have lost power, which only happens after the last session ends. With one session held open, starting and stopping another beside it kept every stopped session's buffers without bound. Beside a running session, a new one is now refused with -EBUSY once AVE_MAX_SESSIONS stopped sessions wait. If the firmware gives no reset proof, so the list can never be freed this boot, the limit holds even with no session running. Nothing is freed earlier than before.

Tested: the module builds W=1-clean (clang, arm64). The recovery host test gains a case that starts and stops eight sessions beside an open one through the production ave_session_attach(). A second case starts and stops sessions one at a time with no reset proof. Both pass here; the previous revision fails the first case with the stopped sessions growing past the bound (registered as a negative control), and the first commit alone fails the second. All registered AVE host suites pass. J700 (Neo) at 73509b814f71, in the combined candidate 7.3.0-neo-combined2, which carries it unchanged: H.264 1080p encode 300/300 frames, md5 identical to base. Three short encodes run one after another beside a long one: two complete, the third is refused with -EBUSY and the bounded-quarantine message, and after the long encode ends a new one starts normally. The no-reset-proof message never appears. 0 DART faults.

Risk: two-way · every Mac with the AVE encoder. A third start beside a running session, after two stopped ones, now gets -EBUSY until the running session ends. Without a reset proof, the encoder takes no new sessions after two have stopped, for the rest of that boot.

Not yet: the encode smoke and churn row on the M5 Pro.

Details
  • Why the buffers stay: neither an answered Close nor a Halt proves the engines stopped, and freeing after them corrupted memory on the Neo (see the comment above ave_sess_destroy()). So this bounds what waits instead of freeing it sooner.
  • ave->failed_count counts encoder sessions on failed_sessions. MCTF sessions hold only the device pool and aren't counted. ave_sess_reap() clears the count after it frees the list.
  • The check sits in ave_session_attach(), under hw_lock, before Open and Start. A refused session was never handed to the firmware, so ave_sess_destroy() frees it at once.
  • With no session running, the next start restarts the firmware from a proven power loss, which reaps the list, so no limit applies there.
  • The exception is when the firmware never arms the reset witness (witness_unusable). The reap then keeps the list for the whole boot, so the same limit applies with no session running, and it's logged once (second commit).
  • run-recovery.py --ref REV builds that revision's driver against this tree's cases. run-host-tests.py suites can now carry an expectation, which the negative control uses.
Evidence
  • python3 drivers/media/platform/apple/ave/tests/run-recovery.py: ok, including "bounded quarantine beside a running session".
  • run-recovery.py --ref 255761f1d55f8dcf3e2c24d17095439237ad6f72: quarantine_churn: Assertion 'quarantined() <= AVE_MAX_SESSIONS' failed.
  • tools/asahi/run-host-tests.py --only ave: ave-host, ave-input, ave-ipc, ave-recovery, ave-pm and ave-probe-pm ok; ave-recovery-old killed as expected.
  • make M=drivers/media/platform/apple/ave LLVM=1 W=1 modules against a configured 7.3 tree: no warnings in the changed files.

… running one

A session the firmware has seen keeps all its DMA buffers after it
stops, on failed_sessions, until the last session ends and the
coprocessor is proved to have lost power: neither an answered Close nor
a Halt proves the engines stopped, and freeing after them corrupted
memory on the Neo. While one session stays open, that power loss never
comes, and the list had no bound. Starting and stopping another session
beside the open one kept every stopped session's buffers, and each start
allocated a new set, until DMA memory or the encoder's address space ran
out.

Count the encoder sessions on failed_sessions. Beside a running
session, ave_session_attach() now refuses another with -EBUSY once
AVE_MAX_SESSIONS of them wait, before the firmware is given anything,
so the refused session is freed at once. Nothing is freed any earlier
than before. The reap that follows the power loss resets the count.
MCTF sessions hold only the device pool and are not counted.

Add a case to the recovery host test that keeps one session open,
starts and stops eight in the other slot through the production
ave_session_attach(), and checks that what waits stays within
AVE_MAX_SESSIONS. It then ends the last session and checks that the
power loss gives everything back and admits a new session.
run-recovery.py gains --ref to build an older driver against the same
cases. The previous revision is registered as its negative control:
there the stopped sessions grow past the bound.

Signed-off-by: DjDeveloperr <[email protected]>
@DjDeveloperr
DjDeveloperr requested review from a team as code owners November 26, 2026 10:20
@DjDeveloperr

DjDeveloperr commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor Author

Verdict at 73509b814f71: ready

Blockers: none

Checked:

  • The bound caps memory.
    • Beside a running session, the refusal needs at least AVE_MAX_SESSIONS (2) counted sessions waiting, so new stopped sessions can only come from sessions that were already running. With two slots, at most 3 encoder sessions can wait (2, plus the last running one when it stops). After that, the next start finds no session running and goes through ave_fw_ensure_running(), whose power-loss proof reaps the list.
    • MCTF sessions are left out of the count, but AVE_MCTF_POOL_LENT lets only one of them hold the pool, so at most one can wait.
    • The host case quarantine_churn starts and stops eight sessions beside an open one through the real ave_session_attach(). The list stays at 2 or fewer, and the power loss empties it.
  • Without a reset proof (new commit 73509b814f71):
    • Normal path unchanged: the new check is witness_unusable && failed_count >= AVE_MAX_SESSIONS. witness_unusable is set only by ave_fw_boot(), under hw_lock (through ave_fw_ensure_running()), when the firmware leaves BOOT_ARG unarmed, and it is never cleared. On a firmware that arms the witness, the new branch is never taken, and ave_session_attach() behaves as at ac2ef807804d.
    • Bound in that state: the reap never frees anything for the whole boot, so the limit now also holds with no session running. The list stays at 3 encoder sessions or fewer, as above.
    • No log spam: the new message is dev_warn_once, so it prints once per boot. Each refused STREAMON still logs the caller's one-line cannot start an encoding session: -16, as every refused start did before this PR.
    • Trade-off, as the commit says: in that state the encoder takes no new session for the rest of the boot once two have stopped. Before, it kept memory it could never give back.
  • The count can't leak or underflow.
    • failed_count is only raised in ave_sess_destroy(), in the same hw_lock section that pushes the session onto failed_sessions. It is only cleared in ave_sess_reap(), after the loop has emptied that list. Nothing else takes sessions off the list, and it is never decremented.
    • When the reap keeps the list (no reset proof, or a DMA domain that is no longer strict), it returns before the reset, so the count still matches the list.
    • ave_session_attach() reads failed_count, witness_unusable and the session table under hw_lock.
  • -EBUSY doesn't strand a client while the witness works.
    • Both refusals come after the free-slot search and before anything changes: no sessions[idx], no MCTF borrow, no firmware restart, no Open and no Start. handed stays false, so ave_session_free() → ave_sess_destroy() frees the refused session at once.
    • ave_session_attach_cycled() retries only -EAGAIN.
    • With a working witness, the refusal lasts only while another session runs. Once that one ends, the next start takes the reap path, where the limit doesn't apply.
  • Merged-code rules: no runtime switch and no SoC or board gate. The limit reuses AVE_MAX_SESSIONS.
Evidence
  • Build at 73509b814f71 with the asahi profile (LLVM) and W=1: drivers/media/platform/apple/ave/, 0 warnings.
  • tests/run-recovery.py: ok, including "bounded quarantine beside a running session and without a reset proof".
  • With --ref ac2ef807804d (the previous commit): quarantine_no_witness: Assertion 'quarantined() <= AVE_MAX_SESSIONS && !ave_session_count(&ave)' failed.
  • At ac2ef807804d, with --ref 255761f1d55f: quarantine_churn: Assertion 'quarantined() <= AVE_MAX_SESSIONS' failed.
  • run-host-tests.py --only ave: 11/11, including ave-recovery-old (killed as expected).
  • KUnit drivers/media/platform/apple/ave/.kunitconfig at ac2ef807804d (arm64 QEMU, local): 49/49. The new commit changes only ave_session_attach() and the host case.
  • The encode smoke and churn rows on the Neo and the M5 Pro are still listed under "Not yet".

…reset proof

When the firmware does not arm the reset witness (witness_unusable),
ave_sess_reap() keeps every stopped session for the whole boot, since
nothing proves the engines stopped. The limit from the previous commit
applied only beside a running session, so sessions started one at a
time, each halting the firmware when it stopped, still piled up without
bound.

In that state, refuse a new session once AVE_MAX_SESSIONS stopped
sessions wait, whether or not another runs, and say so once. The
encoder then stops taking sessions for the rest of that boot instead of
holding more memory it can never give back.

Add a recovery host case that starts and stops sessions one at a time
with no reset proof. The first AVE_MAX_SESSIONS run, the rest are
refused before the firmware sees them, and what waits stays within the
bound. At the previous commit, the list grows past it.

Signed-off-by: DjDeveloperr <[email protected]>
@github-actions github-actions Bot added area:gpu Area from CODEOWNERS (bot) area:media Area from CODEOWNERS (bot) blast:all Macs affected, from Merge risk (bot) ci-failing A check failed at the PR head (bot) door:two-way Revertible or not, from Merge risk (bot) needs-hw-row No hardware row at the PR head (bot) needs-review No approving review from Core or Omarchy Mac yet (bot) labels Nov 26, 2026
@github-actions

github-actions Bot commented Nov 26, 2026 •

Copy link
Copy Markdown

Merge readiness at c9553076: not ready yet

Check Detail
CI at head no failing: apple-build, host-tests, KUnit ane-boot, KUnit ane-pm, KUnit dcp-fabric, KUnit dcp-topology
Review no needs one approving review from Core or Omarchy Mac (not the author)
Requested changes ok none
Hardware row at head ok comment at c9553076
Conflicts ok none
PR text ok Risk: two-way · every Mac with the AVE encoder. A third start beside a running session, after two stopped ones, now gets -EBUSY until the running session ends. Without a reset proof, the encoder takes no new sessions after two have stopped, for the rest of that boot.

Areas: area:gpu, area:media

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

@DjDeveloperr

Copy link
Copy Markdown
Contributor Author

J714s (MacBook Pro 14" M5 Pro, T6050) row at 73509b814f71: PASS.

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

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Verdict: MERGE at 73509b81 (once cancelled CI checks are rerun green)

Blockers: none (code, host tests, and J714s hardware row are ready; rerun the 20 cancelled CI checks before merge).

Checked:

  • drivers/media/platform/apple/ave/ave-sess.c and ave-v4l2.c: verified ave->failed_count tracks non-MCTF stopped sessions under hw_lock, resets to 0 on ave_sess_reap(), and refuses new attachments with -EBUSY once ave->failed_count >= AVE_MAX_SESSIONS either beside a running session (ave_session_count(ave)) or unconditionally when ave->witness_unusable.
  • drivers/media/platform/apple/ave/tests/recovery-test.c and run-recovery.py: verified quarantine_churn, quarantine_no_witness, and the registered negative control against the prior revision.
  • Verified exact-head (73509b814f71) J714s (M5 Pro) hardware row PASS.
Evidence
  • Diff reviewed at 73509b81 (all 7 files, 361 lines).
  • CI at review time: 3 passed, 0 failed, 20 cancelled.

@RyanTheTide

Copy link
Copy Markdown
Contributor

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

@RyanTheTide

Copy link
Copy Markdown
Contributor

Merge-train, point 4: the AVE encoder driver runs on several families, and the J714s row covers one. Please add a row at this head on a second family with AVE (J616s or M1 Pro: encode sessions across a power cycle, 0 errors), then it merges.

@github-actions github-actions Bot added the merge-conflict Conflicts with the base branch (bot) label Nov 29, 2026
Forward-merge main. recovery-test.c keeps main's cpu_dart_filter()
case beside this branch's quarantine_churn() and quarantine_no_witness()
cases, and main() runs all three.

run-recovery.py now extracts main's DAPF filter helpers and
ave_dapf_entry, which 255761f does not have, so the ave-recovery-old
negative control no longer built. Point it at 2fd49a6, the main
this merges: it fails quarantine_churn() there as intended.

Signed-off-by: Ryan Murray <[email protected]>
@RyanTheTide

Copy link
Copy Markdown
Contributor

Forward-merged current main (2fd49a6) into this branch; new head c955307 (no force push). This also gives the PR a fresh CI run in place of the 20 checks the infra sweep cancelled.
ave/tests/recovery-test.c: kept main's cpu_dart_filter() case beside this PR's quarantine_churn()/quarantine_no_witness(), and main() runs all three. The driver files merged cleanly.
run-recovery.py now extracts main's DAPF helpers and ave_dapf_entry, which 255761f lacks, so the ave-recovery-old negative control could not build. It now points at 2fd49a6, the main merged here, and still fails quarantine_churn() there as intended.
Tests: tools/asahi run-host-tests.py --only ave 11/11 ok (ave-recovery, ave-recovery-old negative control, ave-host, pm/probe-pm).
Still needed: green CI at this head, an App re-review, and the second-family AVE row (J616s or J316s: encode sessions across a runtime-PM power cycle, churn past AVE_MAX_SESSIONS, 0 errors).

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

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Verdict: MERGE at c955307 after integration validation.

Checked: The merged recovery fixture runs the CPU DART filter case and both quarantine cases. The negative control now points to the merged base, which has the filter helpers but lacks the quarantine bound, and the reported AVE host run identifies quarantine_churn as the expected failure. Retain hosted CI and the outstanding second-family power-cycle row.

@github-actions github-actions Bot added needs-hw-row No hardware row at the PR head (bot) and removed merge-conflict Conflicts with the base branch (bot) labels Nov 29, 2026
@RyanTheTide

Copy link
Copy Markdown
Contributor

Row (J316s, M1 Pro, T6000) at exact head c955307 in combined candidate 77b87daa (main de965eb + #3277 542bbdc + #3231 c955307 + #3210 69f3a4a), kernel 7.3.0-aurora-crj7+ (fleet edge config), kernel-only one-shot with the saved Stage 2 and the default command line -> PASS (second AVE family, encode sessions across power cycles, 0 errors).

  • Encodes (1080p NV12 testsrc2, 60 frames, h264_v4l2m2m and hevc_v4l2m2m, 8 Mbit/s): rc 0 and 60 frames each, md5 identical to the saved production kernel (H.264 6aba84e0, HEVC 12d19eb6) on a fresh boot, after the encoder runtime-suspended, after one s2idle (PMU RTC wake, 1 success / 0 fail), and after the churn below.
  • 8 encodes one after another (session start/stop with nothing beside them): 8/8 rc 0, all md5 identical.
  • Sessions beside a running encode: three short encodes started next to a paced 20 s 720p encode were each refused at VIDIOC_STREAMON with EBUSY, with no kernel line. The saved production kernel (7.3.0-aurora-edge) refuses them the same way, so on this Mac the second session never reaches the stopped-session bound and the bound message does not appear. The long encode completed (600 frames), an encode started after it ended completed with the same md5, and so did a later H.264 + HEVC pair.
  • Kernel log: 0 AVE/DART error or fault lines, 0 WARN/BUG/Oops. The trial boot was Enforcing with 0 failed units and the saved command line.

Evidence: traces/kernel/20261130-closeout-k114s-j316s-combo/m1pro-ave2/ (the first boot's AVE section in m1pro/ wrote to .bin names that ffmpeg cannot mux, so those encodes never ran; this second boot of the same build replaces it). Saved kernel 7.3.0-aurora-edge restored and verified.

@github-actions github-actions Bot added ci-failing A check failed at the PR head (bot) and removed needs-hw-row No hardware row at the PR head (bot) ci-failing A check failed at the PR head (bot) labels Nov 30, 2026
@RyanTheTide

Copy link
Copy Markdown
Contributor

Merging at c9553076a4f7 (B2 merge train, 12-01 merge bar). Approval at this head: aurora-silicon-review[bot]. Row: the hardware row at c955307 that the readiness check found (PR body or comment). CI: linux train L3 = main 429e7e1 + 28 approved PRs (candidate 45c2fa4b): Apple driver compile pass, Apple DTBs W=1 pass, planned KUnit dcp-fabric/dcp-topology/dcp-display/macsmc/spi-hid-apple/mt7932/asahi/sep pass; ave failed to build only because of #3153 (unused static helper under -Werror; #3153 held out) and passes with #3153 reverted from the candidate; host tests apple-dcp and apple-scaler-recovery pass. The rest of the hosted run is queued behind the backlog. Revert: git revert -m 1 <merge commit>.

@RyanTheTide
RyanTheTide merged commit 7d5b4e3 into main Dec 1, 2026
25 of 44 checks passed
@RyanTheTide
RyanTheTide deleted the fix/ave-bound-stopped-sessions branch December 1, 2026 08:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:gpu Area from CODEOWNERS (bot) area:media Area from CODEOWNERS (bot) blast:all Macs affected, from Merge risk (bot) ci-failing A check failed at the PR head (bot) door:two-way Revertible or not, from Merge risk (bot) needs-review No approving review from Core or Omarchy Mac yet (bot)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants