Skip to content

media: apple: avd: check HEVC tile layouts and VP9 tile counts before building jobs - #2945

Merged
RyanTheTide merged 7 commits into
integration/aurorafrom
avd/tile-bounds
Nov 23, 2026
Merged

RyanTheTide merged 7 commits into
integration/aurorafrom
avd/tile-bounds

Conversation

@RyanTheTide

Copy link
Copy Markdown

Problem

Two codecs size kernel tables from one control and then fill them using another, unchecked one:

  • HEVC sizes its CTB raster-to-tile-scan and tile id tables from the SPS picture size. The explicit tile column widths and row heights come from the PPS and were never compared with that size.
  • VP9 sizes the job table from tile_rows_log2 and tile_cols_log2. tile_rows_log2 has no bound in the driver or in the V4L2 core, and the segment count was an int product.

push() also wrote into job->segments[job->num] without checking that the segment exists or has room.

Change

  • push() drops an instruction that has no segment or no room in it, and marks the job. A marked job is refused with -EOVERFLOW and never becomes a program. The job records how many segments were allocated, and the H.264 path updates that count when it grows the table.
  • HEVC: before building the tables, require what H.265 7.4.3.3.1 requires. There may be no more tile columns or rows than the picture has CTBs, and explicit widths and heights must leave a non-empty last column and row. An SPS with a zero picture size, or a CTB size other than 16, 32 or 64 (7.4.3.2.1), is refused. The tile id builder also skips any index outside the tables.
  • VP9: tile_rows_log2 must be 0 to 2 (6.2.13) and tile_cols_log2 must lie in the range the frame width allows (6.2.14). The check runs at control set and again at run time, and the segment count is now computed in size_t.
  • V4L2 core: validate_vp9_frame() now refuses tile_rows_log2 > 2, next to the existing tile_cols_log2 check.
  • HEVC and VP9 build each job from copies of their picture controls, taken under the control lock, so the values checked are the values used even when a plain VIDIOC_S_EXT_CTRLS sets the controls while a job is being built.

Conformant streams are unaffected. No new parameters, gates or tables.

Testing

  • KUnit (arm64 QEMU, KASAN, avd-pm.kunitconfig): new suites apple-avd-hevc, apple-avd-job and apple-avd-vp9 pass, and so does the rest of the module's KUnit.
  • The new tests fail without the fixes. The HEVC tile cases hit a KASAN slab-out-of-bounds in avd_hevc_compute_tiles, and the job-table and VP9 tiling cases fail their expectations.
  • Hardware: M2 (T8112), default cmdline: stock and patched modules give identical output on 9 decode clips and the 1080p/4K bench; malformed layouts are refused without a hang. Details below.

Still to run

  • Rows on an M1-family and an M4-family Mac (the change is codec-generic, not tied to the M2).

Overlap

#2755 also adds an HEVC tile-layout check as part of a larger audit series; whichever lands first, the other rebases onto it.

Commits
  1. media: apple: avd: keep instructions inside the job table
  2. media: apple: avd: hevc: check the tile layout against the picture
  3. media: apple: avd: vp9: bound the tile counts
  4. media: v4l2-ctrls: validate VP9 tile_rows_log2
  5. media: apple: avd: build HEVC and VP9 jobs from copies of their controls
Test details

KUnit (arm64 QEMU, KASAN, avd-pm.kunitconfig) at the PR head on integration/aurora b10280c: 79/79 pass (apple-avd-h264, -hevc, -job, -pm, -queue, -vp9). With the fixes reverted, the HEVC tile cases hit a KASAN slab-out-of-bounds write in avd_hevc_compute_tiles and the job-table and VP9 tiling cases fail.

Hardware, M2 (T8112), default boot 7.3.0-aurora-fleet82, default cmdline: the stock apple-avd.ko against a module built from the fleet82 AVD source plus these five commits (same signing key), swapped in user space:

  • 9 decode clips (HEVC uniform and non-uniform tiles, multi-slice, WPP; VP9 tile rows and columns; AV1 tiles; H.264 multi-slice and baseline): output md5 identical between stock and patched, and again after the malformed-input cases.
  • avd-bench H.264/HEVC 1080p and 4K, 2 passes: bit-exact on both, throughput unchanged (e.g. HEVC 4K 490/490 fps stock, 490/490 patched).
  • Malformed tile layouts and tile counts: refused (error buffer or -EINVAL at control set), no hang; 0 oops/KASAN/DART faults.

Evidence (our own captures): traces/codec/20261123-avd-tile-bounds-m2.

Assisted-by: LLM (Claude:Opus 5.5)

push() wrote the next instruction into job->segments[job->num] without
checking that the segment exists or has room. The codecs size the table
from the request before building it, so an error in that sizing became a
write past the table, and a segment that filled up wrote into the next
one. avd_open_build_program() only noticed a full segment after the
writes.

Record the number of allocated segments in the job, and have push() drop
an instruction that has no segment or no room and mark the job. A marked
job, or one whose segment index is past the table, is refused with
-EOVERFLOW instead of being turned into a program. The H.264 path, which
grows the table when a picture needs more slices, updates the count.

Add KUnit cases for a full segment and a full table.

Signed-off-by: Ryan Murray <ryan@aurorasilicon.org>
The tile tables are sized from the SPS picture size in CTBs, but explicitly
sized tile columns and rows were taken from the PPS without comparing them
with that size. The last column or row then took a wrapped, very large
width, and building the CTB raster-to-tile-scan and tile id tables wrote
outside both tables.

Before building the tables, require what H.265 7.4.3.3.1 requires: no more
tile columns or rows than the picture has CTBs, and explicit widths and
heights that leave a non-empty last column and row. Also refuse an SPS
with a zero picture width or height, or a CTB size other than 16, 32 or 64
(7.4.3.2.1), which the tables and the slice address arithmetic divide by.
As a second line, the tile id table builder skips any index outside the
tables.

Add KUnit cases for the SPS geometry and tile layout checks that also build
the tables, for accepted and refused layouts.

Signed-off-by: Ryan Murray <ryan@aurorasilicon.org>
The VP9 job is sized with one segment per tile, computed in int from
tile_rows_log2 and tile_cols_log2, and set_tiles() then walks the tile
grid with u32 counts. The V4L2 core bounds tile_cols_log2 to 6 but not
tile_rows_log2, and the driver checked neither, so the int product can
wrap and the job be allocated smaller than the grid that is filled in.

Check the tiling in avd_vp9_validate_frame(), which runs when the frame
control is set and again when the frame runs: tile_rows_log2 is at most
2 and tile_cols_log2 lies between calc_min_log2_tile_cols() and
calc_max_log2_tile_cols() for the frame width (VP9 6.2.13 tile_info()
and 6.2.14). With the 4096-pixel coded width limit that also keeps the
column count within the VP9_MAX_TILE_COLS the scratch is sized for.
Compute the segment count as a size_t shift of the summed log2 values.

Add a KUnit test for the tiling check.

Signed-off-by: Ryan Murray <ryan@aurorasilicon.org>
validate_vp9_frame() checks tile_cols_log2 against its maximum but
accepts any tile_rows_log2. The VP9 tile_info() syntax (section 6.2.13)
codes it as at most two one-bit increments, so it is 0, 1 or 2. Reject
larger values so drivers do not each have to.

Signed-off-by: Ryan Murray <ryan@aurorasilicon.org>
The HEVC and VP9 run paths kept pointers into the current values of the
picture controls (HEVC SPS, PPS, decode parameters and scaling matrix, VP9
frame and compressed header) while they checked those values and then built
the job from them. A VIDIOC_S_EXT_CTRLS that sets current values, not a
request, is not serialized with the decode work, so the values could change
between the checks and their use, and the tile tables and job table could
be built from values that were never checked.

Copy the controls under the control lock when the job starts, as the HEVC
path already does for its slice and entry point arrays, and check and
build from the copies.

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

@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 review at 276447127c76.

The kernel changes across all 5 commits look solid: push() bounds-checking with job->max/job->overflow, HEVC SPS CTB geometry and tile layout validation, VP9 tile_rows_log2/tile_cols_log2 bounds (both driver and v4l2-ctrls-core.c), and locked control copies for HEVC and VP9 runs are clean, overflow-safe, and leak-free. §1–§4 and §6 pass (no model gating, default cmdline, human author/committer and Signed-off-by: only, zero AI trailers in commits).

Blocker

  1. Failing CI (host-tests / avd-codec-tail-tests, run 45113950356, 460/461 passed):
    • drivers/media/platform/apple/avd/tests/program-test.py:549 extracts avd_hevc_validate_sps() without extracting the new avd_hevc_check_sps_geometry(), causing -Werror=implicit-function-declaration when compiling prepare.c.
    • Even after extracting avd_hevc_check_sps_geometry(), program-test.py:459-460 initializes struct v4l2_ctrl_hevc_sps hevc with log2_min_luma_coding_block_size_minus3 = 0 and log2_diff_max_min_luma_coding_block_size = 0 (ctb_log2 == 3), which avd_hevc_check_sps_geometry() now rejects with -EINVAL (breaking the depth-validation loop at line 467).
    • program-test.py:94 defines struct avd_job without the new size_t max; bool overflow; fields now used by avd-inst.h:push() and avd-backend-open.c:avd_open_build_program(), and its avd_init_job() / MAIN / RUN_MAIN / CONFORMANCE_MAIN fixtures (plus av1-test.py / vp9-test.py which import PRELUDE) need job.max initialized so push() does not flag overflow on every segment.

Non-blocking note

  • avd_hevc_check_tiles() currently relies on v4l2-ctrls-core.c to enforce num_tile_columns_minus1 < ARRAY_SIZE(pps->column_width_minus1) (20) and num_tile_rows_minus1 < ARRAY_SIZE(pps->row_height_minus1) (22); checking those bounds inside avd_hevc_check_tiles() too would keep the helper self-contained if called outside the V4L2 control path.

Please update the AVD host test harness so host-tests passes green before merge.

Assisted-by: LLM (Gemini 4 Neon)

…heck

The host program test models struct avd_job without the new max and
overflow fields, so push() treats every job as full. Give the model
the fields and set max wherever a test builds a segment table.

avd_hevc_validate_sps() now calls avd_hevc_check_sps_geometry(), which
the test did not extract, and the depth fixture used 8x8 CTBs, which
the check refuses. Extract the helper and use 64x64 CTBs. Let the
extractor find functions whose return type is on the previous line.

Signed-off-by: Ryan Murray <ryan@aurorasilicon.org>
avd_hevc_check_tiles() reads column_width_minus1[] and
row_height_minus1[] up to the tile counts and relied on the V4L2
control code to keep the counts below the array sizes. Check that in
the helper too, so it holds for any caller.

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

Copy link
Copy Markdown
Author

Thanks for the review. Pushed two commits (5e26308), no history rewrite:

  • 15d9bac9 tests: the host program model of struct avd_job now has max/overflow, and every fixture that builds a segment table sets max (including avd_init_job() in program-test, vp9-test and av1-test). avd_hevc_check_sps_geometry() is extracted, and the depth fixture uses 64x64 CTBs. host_source.function() now also finds functions whose return type is on the previous line (VISIBLE_IF_KUNIT int / static enum ...).
  • 5e263083 your non-blocking note: avd_hevc_check_tiles() checks the tile counts against ARRAY_SIZE(column_width_minus1/row_height_minus1) itself. New KUnit case avd_hevc_tiles_array_test (8192x4320, 20/22 accepted, 21/23 refused; without the check the refused ones return 0).

Local results at 5e26308: run-host-tests.py --only avd 12/12 (codec tail incl. mutations, AV1 tail incl. 6 mutations); KUnit+KASAN (avd-pm.kunitconfig, arm64) 80/80.

@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 re-review at 5e263083adeb6e0f710aab4a5ddc271bd4f6ae5c.

Follow-up Verification (since 276447127c76)

  • Prior Blocker 1 (host-tests / avd-codec-tail-tests) — Resolved: Commit 15d9bac9d8ff updates drivers/media/platform/apple/avd/tests/host_source.py to match multiline return types, adds max/overflow to struct avd_job and initializes them across program-test.py, av1-test.py, and vp9-test.py, extracts avd_hevc_check_sps_geometry(), and uses a valid 64×64 CTB in the HEVC depth-validation fixture. Live CI host-tests (run 45127918674) is green: 463/463 passed, including all 12 AVD host tests (avd-codec-tail-tests + --mutations, avd-av1-tail-tests + cases + all 6 mutations).
  • Prior Non-blocking Note (avd_hevc_check_tiles() PPS array bounds) — Resolved: Commit 5e263083adeb adds explicit ARRAY_SIZE(pps->column_width_minus1) / ARRAY_SIZE(pps->row_height_minus1) checks in avd_hevc_check_tiles() along with KUnit case avd_hevc_tiles_array_test (80/80 KUnit+KASAN pass locally).

Checklist Summary (§1–§6)

  • §1 No model gating: Pass — codec-generic V4L2/AVD bounds checks with zero chip, SoC, board, or firmware gates.
  • §2 Production-ready: Pass — works on the default command line with no new parameters, delays, or lab-only paths.
  • §3 Safety: Pass — push() bounds-checks job->num >= job->max and seg->num >= AVD_MAX_INST (avd_open_build_program() returns -EOVERFLOW), HEVC checks SPS geometry + PPS array bounds + CTB tile layout, VP9 bounds tile_rows_log2 <= 2 and tile_cols_log2 in both the driver and v4l2-ctrls-core.c using size_t sizing, and HEVC/VP9 jobs run from locked control copies with leak-free cleanup.
  • §4 Clean-room & attribution: Pass — all 7 commits carry human author/committer (Ryan Murray <ryan@aurorasilicon.org>) and human Signed-off-by: only, zero AI trailers in commits; PR body includes Assisted-by:; clean-room.
  • §5 Evidence & CI: Pass — host-tests CI green (463/463); KUnit+KASAN (avd-pm.kunitconfig, arm64 QEMU) 80/80 pass (all new tests confirmed failing without fixes); M2 (T8112, default boot 7.3.0-aurora-fleet82, default cmdline) A/B confirms bit-exact output across 9 decode clips and 1080p/4K avd-bench (490/490 fps unchanged) plus clean refusal of malformed tile layouts/counts with 0 oops/KASAN/DART faults.
  • §6 Naming: Pass.

Non-blocking Notes

  • M1-family / M4-family follow-up rows (listed under Still to run) and an explicit SELinux/systemd regression tuple (0 SELinux denials, 0 failed units, 0 new kernel errors) are welcome when convenient.

Verdict

APPROVE — prior blocker and non-blocking note cleanly resolved, CI green, ready for MERGETRAIN-LINUX4.

Assisted-by: LLM (Gemini 4 Neon)

@RyanTheTide

Copy link
Copy Markdown
Author

Merge-train review at 5e26308 (non-author pass): no blockers.

I went through the five kernel commits looking for valid streams that now get refused, overflows that are still open, and error paths. I found nothing that blocks a merge. Every new limit comes from the codec spec: HEVC CtbLog2SizeY 4..6 and the 7.4.3.3.1 tile layout, VP9 6.2.13/6.2.14 tile counts. There are no chip, board or firmware conditions and no new parameters, and the commit trailers are human-only (author, committer and Signed-off-by). The stack still merges cleanly onto integration/aurora 5441428; the 36 commits the base has gained since b10280c do not touch avd or v4l2-core. Host tests and KUnit pass at head, and all seven mutations I made to the checks were caught by the tests.

The hardware row on M2 (T8112) covers the decode path at head: the fleet82 AVD codec files are identical to b10280c, and the only kernel change after the row (5e26308) adds a check the V4L2 core already enforces. That row has HEVC and VP9 clips with output, and H.264 through clip1080-h264 and the bench. A second-generation row (M1-family or M4-family) is still listed under "Still to run". It is wanted, but I don't hold this PR on it, because the change only adds bounds and does not alter how valid jobs are built.

What I checked

False positives (valid streams refused):

  • HEVC tiles: avd_hevc_check_tiles() matches 7.4.3.3.1. Uniform spacing only needs cols <= PicWidthInCtbs and rows <= PicHeightInCtbs, which is the spec range. With explicit spacing, the sum of the first n-1 widths/heights must be less than the picture size in CTBs. The u8 sizes times 20/22 entries cannot overflow a u32. CTB sizes 16/32/64 are accepted and 8 and 128 are refused (7.4.3.2.1). Picture sizes that are not a multiple of the CTB size use DIV_ROUND_UP, so 1080 with 64x64 CTBs is fine.
  • VP9: avd_vp9_check_tiling() follows calc_min/max_log2_tile_cols exactly (for 1920 wide, at most 4 columns, the same as libvpx). tile_rows_log2 <= 2 is a hard syntax limit, so the v4l2-ctrls-core addition is also safe for the other VP9 drivers.
  • push()/job->max: the H.264 table growth updates max, and the VP9 count is computed in size_t. Job sizes for valid streams are unchanged.

Overflow and race paths: compute_tile_ids() now bounds both rs and ts. HEVC and VP9 copy their controls under the handler lock (a mutex, so GFP_KERNEL is fine). The copies are freed on every exit, including the kmemdup failure path (the preamble returns before done:, so nothing is freed twice).

Error handling: a refused job returns from ->run(). avd_queue's fail: path then marks the frame DONE with that status and detaches the m2m job, the request is completed through avd_run_postamble(), and no job is left stuck.

Tests (scratch worktree, detached at 5e26308):

Rows: the M2 A/B row (stock vs patched module, the same 9 clips and the bench) is identical on both arms. Note that 8 of the 9 clip md5s also differ from the software reference on the stock module, so this row proves no regression but not correctness. Two clips (av1-tiles-1080, h264-slices) produced no output at all on either arm (md5 of empty input): the M2 block has no AV1, and the H.264 multi-slice pipeline gave nothing. Second-generation row recipe: see the comment on #2946.

Assisted-by: LLM (Claude:Opus 5.5)

@RyanTheTide
RyanTheTide merged commit a4b51cd into integration/aurora Nov 23, 2026
1 check passed
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