Repository navigation
media: apple: prores: keep several decode jobs in the ring, retire them on the engine's finished-job id - #2491
RyanTheTide wants to merge 4 commits into
Conversation
The engine works through a producer/consumer descriptor ring, but the driver kept one job in it: v4l2-m2m runs one job at a time and the job only finished at its completion interrupt. On an M1 Pro, VideoToolbox decodes the same clips about 2.6 times faster on the same engine, and between two of our jobs the engine sits idle for the interrupt and the workqueue round trip to the next device_run (100 us per 1080p frame). Publish a job's descriptor, move its buffers off the m2m queues into a per-device FIFO tagged with the ring pointer past the descriptor, and finish the m2m job at once so the next one is prepared while the engine works. Up to APR_MAX_INFLIGHT (8) jobs sit in the ring; when it is full the m2m job stays running until a completion makes room, so device_run is only entered with room for one more descriptor. The interrupt reads the tail and completes, in ring order, every job the tail has passed. DONE and ERROR are latched per engine, so an ERROR fails every job in the ring at that moment. A ring that stops moving for a second fails all of its jobs and resets the engine as before; device_run waits for that reset. stop_streaming, suspend and a STOP command with an empty OUTPUT queue wait for the context's jobs in the ring; job_abort marks only that context's jobs cancelled, since encoder and decoder share the ring. Encode stays at one job in the ring: every encode job uses the one shared statistics area and encode already runs at macOS speed. Add KUnit cases for the ring distance, free space, tail validation and in-order retirement across the ring wrap, and for the FIFO limits. Signed-off-by: Ryan Murray <ryan@aurorasilicon.org>
With several decode jobs in the ring, the driver gave a job's buffers back once the consumer tail had passed its descriptor. The tail moves when the engine fetches a descriptor, not when the frame is written, so buffers went back to vb2 while the engine still wrote them: wrong output on 4:4:4 clips and DART faults at the end of a run. The engine reports the id of the last decode job it has finished in the upper half of ST1; the decode command word already carries a 16-bit id. Give decode jobs device-wide ids that skip 0 (ST1 reads 0 after a reset), read ST1 after the status clear, and retire decode jobs in ring order once their id is reported finished. An encode descriptor carries no id, so an encode only enters an empty ring and completes on its interrupt, as before. On an M1 Pro (T6000) with eight jobs in the ring every clip decodes to the same bytes as with one job, with no DART faults; 1080p 422 HQ goes from about 2730 to 2920 fps, 4K 422 HQ from 761 to 775 fps. Signed-off-by: Ryan Murray <ryan@aurorasilicon.org>
DjDeveloperr
left a comment
There was a problem hiding this comment.
Thanks Ryan. Retiring on the engine's finished-job id instead of the fetch pointer is the right
fix, and the success path holds up: ring-order retirement, reading ST1 after the W1C so nothing is
lost, encode kept alone in the ring, held m2m job, stop_streaming/release/suspend waiting on
inflight, and the watchdog resetting before it gives buffers back. A few small things still let
buffers go back while the engine owns them, so requesting changes. All line numbers are at
722d766.
Must fix
-
A stale DONE retires an encode early (
apple-prores-drv.c:1299-1307,:1488,:1516-1540).
If the last decode job finishes between the W1C and the ST1 read, it is retired by id and its
DONE stays latched. A waiting encode can then enter the empty ring, and the next
apr_take_done()retires it on that DONE, because the tail has already passed it at fetch.
Its src/dst go back while the engine still reads and writes them. A stale ERROR does the same.
The window is narrow, but it is the same failure class this PR fixes. Ack DONE|ERROR under
hw_lockbefore publishing into an empty ring. The engine is idle at that point, so anything
latched is stale. Please also fix the comment at:1250, which calls this case fine. -
Validate the ST1 id like the tail (
:1300-1305,apple-prores-desc.c:744). With
apr_job_id_done(), ST1 = 0 retires every outstanding id in 0x8001..0xFFFF (half the id space),
and an id that has not been issued yet retires the whole ring. Treat 0 as no progress, and an id
at or afternext_idas hung so the watchdog resets before buffers return. Add both to
apr_test_job_id. -
Squash the two commits (or put id retirement first). 50d3ab9 on its own retires decode on
the tail, the version that gave DART faults, and its message describes that as correct. It
should not be a bisect point.
Should fix here
-
Failed jobs come back ahead of the ring (
:1462-1513->apr_finish_job()). A frame that
fails to parse (job_readylets it through on purpose) or an interlaced frame (:1138)
completes right away, ahead of up to seven earlier frames of the same context. If it is the
drain'slast_src_buf, LAST and EOS go out first (:1191-1197), and vb2 returns -EPIPE for
the frames still in the ring, so they are lost. Waiting forctx->inflight == 0before
apr_finish_job()in those paths keeps the order. device_run already sleeps there. -
apr_finish_job()removes src and dst outsidehw_lock(:1232-1233). A STOP between the two
can take that dst throughv4l2_update_last_buf_state(). The race is older than this PR, but
apr_stop_cmd()now relies on buffer moves happening underhw_lock, so do the two removes
under it as well.
Before merge
- The decoder is registered on t6000, t6041, t6050 and t8140 (
:2499,:2524-2527), but the
ST1[31:16] meaning and in-order finishing were measured on T6000 and T6040 only. Please run the
Neo row you already planned and an M5 (T6050) row, or keep depth 1 behind a variant flag until
each one is confirmed. With 2 fixed, a different ST1 meaning turns into timeouts instead of
early returns, but out-of-order finishing still would not be caught.
Nits
:1246-1251: the watchdog fires when no job retires forAPR_TIMEOUT_MS, not when the tail
stops moving.apr_test_inflightsays a full ring of decode jobs is retired on the tail. That now applies
only to the lone encode.- Blank line inside the declarations in
apr_job_abort(). Odd continuation indent on
apr_retire(). - Not for this PR: when the engine cannot be reset (
:1418-1428), buffers still go back after
"refusing further jobs", now up to eight jobs. Worth a #2396 follow-up.
Question: 48/48 interrupts retired exactly one job, so that trace never took the multi-retire
path. Did the run with the IRQ bit on every 8th descriptor use ST1 retirement? If so, it covers
that path. Please say so in the description.
With 1 to 5 done and the T8140/T6050 rows (or the gate), I'm happy to approve.
Several paths could still hand buffers back while the engine owned them or would use them: - A DONE or ERROR latched after the last job was retired by id stayed set. An encode published into the then empty ring was taken for finished on it, because the tail passes an encode as soon as it is fetched. The engine is idle while the ring is empty, so clear both status bits before publishing into an empty ring. - ST1 = 0 counted as "finished" for every id in 0x8001..0xffff, and an id not issued yet retired the whole ring. 0 now means no progress, and an id at or after the next one to issue marks the ring hung, so the watchdog resets the engine before any buffer goes back. - A job that fails before it reaches the ring (a frame that does not parse, an interlaced frame) completed at once, ahead of up to seven earlier frames of the same context. As the drain's last source buffer it sent LAST and EOS first and the frames still in the ring were lost. It now waits until the context's jobs in the ring are given back. - apr_finish_job() removed the source and destination buffers outside hw_lock, so a STOP could see the destination half moved. Move both under the lock, as device_run does. Also correct the watchdog comments (it fires when no job finishes, not when the tail stops) and the ring test comment. Signed-off-by: Ryan Murray <ryan@aurorasilicon.org>
…inflight-st1-20261120
|
Items 1-5 and the nits are fixed in b8c05f9, as a new commit on top (no force push). a1ad1a8 merges #2462 so the stack still merges cleanly. The description has the answer to each item, the J616s rows (including a drain test that shows the overtaking on 722d766 and not after the fix), the answer to the IRQ-every-8th question, and the squash commit message. Neo and T6050 rows are pending, so this stays in draft until then. Assisted-by: LLM (Claude:Opus 5.5) |
DjDeveloperr
left a comment
There was a problem hiding this comment.
Re-reviewed at a1ad1a8 (fixes in b8c05f9). Both blockers are closed: DONE|ERROR is acked before a job enters an empty ring (apple-prores-drv.c:1549-1550), and ST1 now goes through apr_job_id_check(), where 0 is no progress, an unissued id marks the engine hung and goes to the watchdog, and only an issued id retires decode jobs (drv.c:1320-1333, desc.c:778). Failed and interlaced jobs wait for the context's in-flight jobs (drv.c:1237), and apr_finish_job() moves its buffers under hw_lock. The J616s drain test showing the overtake on 722d766 and its absence after the fix is the right evidence.
Approving the code. Merge conditions are unchanged: a Neo row and one T6050 row at this head, then squash-merge with the message from the description, so 50d3ab9 never lands on its own. The DART left "TTBR invalid" after a watchdog reset (SCODEC parent domain) is a good separate follow-up.
|
Neo (T8140, 7.3.0-40) row at a1ad1a8, done the same way as the J616s and M1 Pro rows:
That leaves the T6050 row. Assisted-by: LLM (Claude:Opus 5.5) |
|
Claude Opus 5.5 on @satchlj's behalf: I can run the T6050 row on lobata, a J775c Mac Studio with an M5 Max. lobata's J775c DT describes The plan is a one-shot trial boot of this head ( To match your J616s, M1 Pro and Neo rows exactly, I need the kit: Could you post them or point me at where they live? Until then I can run the base-vs-PR comparison on my own clips, but those wouldn't be your row's clips. 🤖 Generated with Claude Code |
|
Claude Opus 5.5 on @satchlj's behalf: T6050 row: J775c (Mac Studio, M5 Max), with the st1fix kit. All pass criteria are met. The one open point is hash agreement with the other SoCs: I don't have those hashes, so the sha256 prefixes are below for you to compare. Setup:
Results:
In every DRAIN row the bad frame comes back as the one error ( Clip SHAs (sha256 prefix, identical across base, pr and full): fps (median and range over 3 interleaved rounds):
Each run takes about 0.1 s, so single values swing a lot. pr and full are at or above base everywhere except 2160p 4444, where the three medians are within 1% and inside base's range. I read that as not worse, within this run's noise. dmesg: the 6 bad lines over the whole run are all Notes:
🤖 Generated with Claude Code |
Stacked on #2462 (parse each decoder source frame once), which is stacked on #2396.
Problem
The decoder kept one job in the ring, so the engine sat idle between the completion interrupt and the next job. Retiring several jobs on the ring tail is unsafe: the tail moves when the engine fetches a descriptor, not when the frame is written. That gave wrong 4:4:4 output and DART faults.
Change
apr_finish_job()happen underhw_lock.Testing
Still to run
Review answers (items 1-6, nits, question)
apr_device_run()writesAPR_IRQ_DONE | APR_IRQ_ERRORto the W1C status underhw_lockwhennjobs == 0, before the descriptor and head write. The engine is idle then, so anything latched is stale. The posted clear reaches the engine before the head write. The comment that called this case fine is rewritten.apr_job_id_check(done_id, next_id)returns NONE (0), ISSUED, or FUTURE (an id at or afternext_id, half the id space). On FUTURE with decode jobs in the ring,apr_take_done()setshungand retires nothing; the watchdog resets the engine before buffers go back.apr_test_job_idcovers:apr_job_id_done(0, 0x8001)still true, which is why 0 is handled first);apr_finish_job()now starts withwait_event(idle_wq, !inflight). device_run runs from the m2m job work or ioctl context, where it already sleeps. The drain rows above show the reordering on 722d766 and its absence now.hw_lock.apr_finish_job()takeshw_lockfor the sequence number and both removes, as device_run does.apr_test_inflightcomment says only the lone encode retires on the tail. The blank line inapr_job_abort()is removed and theapr_retire()indent is fixed.resetsis the parent SCODEC domain, which also covers the DART. Details are on media: apple: prores: hardware ProRes decode on every APR engine #2396.Earlier rows of the in-flight ring (722d766 vs #2462)
Squash commit message
Assisted-by: LLM (Claude:Opus 5.5)