Repository navigation
media: apple: prores: hardware ProRes decode on every APR engine, several jobs in the ring - #3160
Conversation
Add a compatible-gated stateful decoder alongside the existing encoder. Validate complete ProRes frames and pack the 0x100-byte non-RAW descriptor. Share the APR ring with generation-aware variable-size accounting. Snapshot completion diagnostics before the W1C/tail handoff. Preserve reset-based timeout recovery. Cover descriptor layout, parser bounds, and mixed ring wrap with KUnit. Signed-off-by: Ryan Murray <[email protected]>
Apple lists each ProRes engine in its SoCs as a ProRes encode and decode engine, and the decode descriptor runs on the same ring as the encoder's. The decoder node was registered only on the T6000 and T8140 variants. Register it for every compatible the driver binds (T6041 and T6050 as well) and drop the per-variant flag. Signed-off-by: Ryan Murray <[email protected]>
The decoder parsed a source frame three times per job: in buf_queue for the source-change check, in job_ready and in device_run. Each parse walks the whole slice-size table (1020 entries for a 1080p frame, 4080 at 2160p) with 16-bit loads from the vb2 dma-contig mapping, which is uncached because the engine is not DMA-coherent, so every entry is a round trip to memory. job_ready also runs in atomic context, from v4l2_m2m_job_finish() in the completion interrupt. With one job in the ring this time sits between the interrupt and the next job. On an M1 Pro (T6000), function_graph shows 76 us per parse of a 1080p ProRes 422 HQ frame and 304 us at 2160p, three times per frame, and the engine idles 100 us (1080p) and 331 us (2160p) between jobs. Parse the frame once, in buf_prepare, and keep the result with the buffer. A frame that does not parse still fails its own job rather than QBUF, as before. Copy the slice table in 256-byte blocks and check the copy, so the CPU reads it with wide loads: one parse now takes 9 us at 1080p and 61 us at 2160p, and the gap between jobs is 3-5 us. Decode throughput on the M1 Pro, depth 1, same clips, output unchanged byte for byte: 1080p 422 HQ 1870 -> 2780 fps, 2160p 422 HQ 495 -> 765 fps, 1080p 4444 1470 -> 1880 fps. Add a KUnit case with a 1080p table that spans several blocks, with a bad entry in the first full and the last partial block. Signed-off-by: Ryan Murray <[email protected]>
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 <[email protected]>
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 <[email protected]>
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 <[email protected]>
The decode descriptor has no input length, so the engine reads as far as the frame's own sizes take it. apr_parse_frame() already holds the frame size, frame and picture header sizes, picture sizes, slice table and slice sizes to the payload, but two in-band fields the engine may also follow were never checked: - the picture header's slice count, which could ask for more slice table entries than the picture geometry gives; - each slice header, whose coded Y and Cb sizes (and Cr size, with alpha) could run past the end of the slice and so, for the last slice, past the buffer. Accept a slice count of 0, which our encoder writes, or the count the geometry gives. Require every slice header to fit its slice (8 bytes with alpha, where the Cr size is explicit, 6 otherwise) and its coded component sizes to fit the rest of the slice. Every ffmpeg-made test clip and every frame from the hardware encoder passes these checks. Read each slice size once so that the parse itself stays inside the payload even if userspace rewrites the buffer while it runs. Give the KUnit fixture valid 6-byte slice headers. Signed-off-by: Ryan Murray <[email protected]>
The decode descriptor carries no input length, so a frame must not get past apr_parse_frame() if any size the engine follows points beyond the queued payload. Add KUnit cases for frames that do: a picture size 1 byte, 64 KiB and 4 GiB past the buffer; a frame size larger than bytesused; a payload truncated by one byte; a frame header past the frame; a slice table ending past the picture; a slice count asking for more table entries; slice sizes summing one byte past the picture; slice headers whose coded sizes run past their slice; and a bottom-field picture or slice past the buffer. The exact-fit frame and one that ends on the boundary byte are accepted. Each frame is parsed from an allocation exactly as long as the payload, so with KASAN an over-read in the parser is reported as well. Signed-off-by: Ryan Murray <[email protected]>
The decoder sends V4L2_EVENT_SOURCE_CHANGE when the stream's size or chroma changes, but its OUTPUT format description only carried V4L2_FMT_FLAG_COMPRESSED. The uAPI defines V4L2_FMT_FLAG_DYN_RESOLUTION for a stateful decoder that does this. Without the flag, GStreamer's v4l2 decoder sets up the CAPTURE queue before the first event and then takes that event as a mid-stream resolution change. It mistakes the real end-of-stream LAST buffer for the end of that change and waits forever, so every v4l2proresdec pipeline hangs at EOS even though its output is complete. FFmpeg does not look at the flag. Signed-off-by: Ryan Murray <[email protected]>
When apr_take_done() finds a tail outside the ring or a finished id that was never issued, it marks the ring hung and queues the watchdog, which fails every job and resets the engine before their buffers go back. Four paths broke that order: - the engine interrupt stayed enabled, and apr_take_done() did not check the flag, so a second interrupt before the watchdog ran retired jobs by the untrusted ST1 id and handed their buffers back to vb2 while the engine might still use them; - the watchdog itself called apr_take_done() on a hung ring; - if that left the ring empty, the watchdog returned early, skipping the reset and leaving the flag set for the next job; - device_run only waited for a reset in progress, so a job could be published into the hung ring before the watchdog took the lock. Mask the engine interrupt when the ring is declared hung, take no completions while it is, and have the watchdog reset a hung ring even when no job is left in it. device_run now waits until the ring is neither hung nor being reset. A runtime suspend powers the engine off, which resets it, so it clears the flag too. Signed-off-by: Ryan Murray <[email protected]>
The decode descriptor has no input length: the engine follows the frame's in-band sizes (frame and picture size, the slice table and each slice header). Those sizes were checked once, in buf_prepare, on the OUTPUT buffer itself, which userspace can still write while the buffer is queued and while the job sits in the ring, up to eight jobs deep: it is an MMAP buffer or a DMABUF another process may hold. Rewriting a size after QBUF let the engine read up to several GiB past the buffer's IOVA. Every context of both nodes shares the engine's DART, so that read could land in another context's buffers, such as another user's encode source frames, and decode them into the attacker's CAPTURE buffer, or fault on an unmapped IOVA and reset the engine under every other context's jobs. Give each slot of the job FIFO a driver-owned source copy. device_run copies the frame into the copy of the slot its job will take, parses that copy and points DD_INPUT at it; the engine never reads the OUTPUT buffer. Only device_run, which v4l2-m2m runs one at a time and only with room in the FIFO, fills a copy, and the engine is done with a slot's copy once its job is taken off the ring or the engine is reset. The copies are cached non-contiguous DMA allocations, so the parse runs on cached memory, with a quarter of headroom so a stream rarely reallocates them; they are freed when the engine runtime-suspends. At most APR_MAX_INFLIGHT copies exist per engine. The parse at QBUF stays, for the source-change check and to fail a malformed frame early; a frame changed after QBUF so that it no longer parses fails its own job. device_run also checks that the CAPTURE buffer is large enough for the current format. Signed-off-by: Ryan Murray <[email protected]>
The decoder advertises V4L2_FMT_FLAG_DYN_RESOLUTION, but a frame of a new size queued while CAPTURE was set up raised SOURCE_CHANGE at QBUF time, before decode reached it, left the CAPTURE format at the old size while CAPTURE was busy, and never marked the last old-size CAPTURE buffer LAST. job_ready just stalled on the new frame. A client following the stateful decoder sequence (drain to LAST, STREAMOFF and G_FMT on CAPTURE) waited for a LAST that never came, or read the old format back. Handle a change where Documentation/userspace-api/media/v4l/dev-decoder.rst puts it, in device_run when decode reaches the frame: give back every earlier frame of the context, switch the OUTPUT and CAPTURE formats to the new geometry, queue SOURCE_CHANGE, and return the next CAPTURE buffer empty with LAST. The new frame stays first on OUTPUT and no job runs until CAPTURE is restarted or a START command resumes decoding; START now also clears vb2's LAST-dequeued state, so DQBUF works again after it. The formats change under hw_lock, which G_FMT and TRY_FMT take, as device_run does not hold vdev_lock. Before CAPTURE streams, the next frame to decode still sets the formats at QBUF, now including the CAPTURE format while CAPTURE buffers exist: device_run refuses a CAPTURE buffer smaller than the format needs. Signed-off-by: Ryan Murray <[email protected]>
Kconfig and the decode descriptor comment still said decode exists only on T6000 and T8140, but every APR variant has had a decoder node since "media: apple: prores: give every APR engine a decoder node". The frame parser bounded the second field of an interlaced frame to 16384 slices and the first to 262144, with no reason given: both fields have the same geometry. Use one named bound for both. Interlaced frames are still refused before a descriptor is built, so nothing decodes differently. Signed-off-by: Ryan Murray <[email protected]>
…hange At a resolution change device_run switches the CAPTURE format before the client has seen SOURCE_CHANGE; the client is still dequeuing the last old-size frames and queues their buffers again. buf_prepare checked every CAPTURE buffer against the current format, so on a change to a larger size that QBUF failed with -EINVAL. dev-decoder.rst has the client queue those buffers as usual; one of them carries the change's LAST flag. On a Neo (T8140) a 1080p to 2160p and a 4:2:2 to 4:4:4 change failed this way; the downsizing changes passed only because the old buffers were large enough. Do not check a decoder's CAPTURE buffers against the current format at QBUF: queue_setup sized each for the format of its time. device_run gives back a buffer too small for the current format with ERROR and no payload, after the context's frames in the ring, and keeps the source frame queued for one that fits, instead of failing the frame. Signed-off-by: Ryan Murray <[email protected]>
|
ProRes KUnit at b5cc590 (QEMU arm64, KASAN on): 36/36 pass. W=1 clang build of the prores directory: no warnings. |
DjDeveloperr
left a comment
There was a problem hiding this comment.
Audited all 14 commits and full diff at b5cc590: clean signed-off replay of #2620 onto main preserving T8132 support. Driver-owned bounce buffer copy + re-parse closes userspace TOCTOU on in-band ProRes sizes, slice bounds/chunked reads and ring/ST1 done-id tracking + watchdog reset/suspend drain + stateful DRC handling all look solid, and ProRes KUnit passes 36/36 under KASAN. Holding merge for CI and the exact-head Neo + M1 Pro/J616s hardware rows.
|
Merge readiness at
Areas: Updated by pr-readiness on pushes, edits, reviews and comments, and on a schedule. Rules: merge policy · labels. |
|
b5cc590 J616s (MacBook Pro 16" M4 Pro, T6040, APR
|
What: Add ProRes decoding to every registered APR engine with one parse per source frame, up to eight jobs in the ring, slice and frame bounds checks, and stateful resolution changes. This replays the 14 signed-off changes from #2620 onto main and keeps the shared decoder probe for T8132.
Tested: J616s M4 Pro at
b5cc5906a955passed 6/6 ProRes frame digest comparisons, concurrent encode and decode, and 1/1 RTC wake with the same decode digest after wake. Restored fleet89 with SELinux Enforcing, zero AVC denials and zero failed units. ProRes KUnit passed 36/36 under KASAN. J616s hardware row · KUnit resultsRisk: Two-way door · Macs with ProRes engines (M1 Pro, M4, M4 Pro, M5, M5 Pro/Max and Neo). A plain revert undoes the change. The encode paths are unchanged from #2620.
Not yet: Neo J700 at this head still needs the resolution-change and frame-bounds checks.
Replay details
The branch replays the 14 real changes from #2620 on main
569ff3d0. The four unsigned commits in #2620 were merge commits. T8132 support fromf8cc03c1remains, and T8132 gets the decoder node through the shared probe path. The Kconfig help names it next to the decoder description.The replay kept the combined Kconfig wording, main's
apr_t8142variant alongsidestruct apr_bufandstruct apr_ctxinapple-prores-drv.c, the slice header bounds change from #2620's mergea1ad1a89inapple-prores-desc.c, and both sets of cases inapple-prores-test.c. The final tree matches the merge preview of main and the #2620 head except for the Kconfig help, where it retains both descriptions.Build and test evidence
The
W=1Clang build ofdrivers/media/platform/apple/prores/with the fleet89 configuration produced no warnings. Strict checkpatch produced zero errors and zero warnings. The ProRes KUnit run used arm64 QEMU with KASAN. The earlier M1 Pro hardware row belongs to #2620 and predates the T8132 change and replay. The J616s row above uses an equivalent build for every file under the Apple ProRes driver, with the saved kernel command line unchanged.