From 2144b43774e1e279f93fab88dec607711f7b94f5 Mon Sep 17 00:00:00 2001 From: Ryan Murray Date: Mon, 23 Nov 2026 08:14:17 +1100 Subject: [PATCH 01/26] media: apple: avd: keep instructions inside the job table 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 --- drivers/media/platform/apple/avd/Makefile | 2 +- .../platform/apple/avd/avd-backend-open.c | 3 + drivers/media/platform/apple/avd/avd-drv.c | 2 + drivers/media/platform/apple/avd/avd-h264.c | 1 + drivers/media/platform/apple/avd/avd-inst.h | 12 ++- .../media/platform/apple/avd/avd-job-test.c | 85 +++++++++++++++++++ drivers/media/platform/apple/avd/avd.h | 5 ++ 7 files changed, 108 insertions(+), 2 deletions(-) create mode 100644 drivers/media/platform/apple/avd/avd-job-test.c diff --git a/drivers/media/platform/apple/avd/Makefile b/drivers/media/platform/apple/avd/Makefile index 66b8e3c61356a..278cf2da78886 100644 --- a/drivers/media/platform/apple/avd/Makefile +++ b/drivers/media/platform/apple/avd/Makefile @@ -4,5 +4,5 @@ apple-avd-y := avd-drv.o avd-v4l2.o avd-hw.o avd-pm.o avd-h264.o avd-vp9.o \ avd-hevc.o avd-av1.o avd-av1-entropymode.o avd-queue.o avd-backend-open.o \ avd-backend-apple.o avd-backend-queue.o avd-backend-kopsia.o apple-avd-$(CONFIG_VIDEO_APPLE_AVD_KUNIT_TEST) += avd-h264-test.o avd-queue-test.o \ - avd-pm-test.o + avd-pm-test.o avd-job-test.o obj-$(CONFIG_VIDEO_APPLE_AVD) += apple-avd.o diff --git a/drivers/media/platform/apple/avd/avd-backend-open.c b/drivers/media/platform/apple/avd/avd-backend-open.c index 13508cc5d5272..2681eeb63b777 100644 --- a/drivers/media/platform/apple/avd/avd-backend-open.c +++ b/drivers/media/platform/apple/avd/avd-backend-open.c @@ -29,6 +29,9 @@ int avd_open_build_program(struct avd_ctx *ctx) if (!f || !job->segments) return -EINVAL; + /* The builder ran past the table or a segment; nothing fits. */ + if (job->overflow || job->num >= job->max) + return -EOVERFLOW; for (i = 0; i <= job->num; i++) { if (job->segments[i].num > AVD_MAX_INST || check_add_overflow(count, job->segments[i].num + !!i, &count)) diff --git a/drivers/media/platform/apple/avd/avd-drv.c b/drivers/media/platform/apple/avd/avd-drv.c index 3de3f241430f5..c79379d2a50f2 100644 --- a/drivers/media/platform/apple/avd/avd-drv.c +++ b/drivers/media/platform/apple/avd/avd-drv.c @@ -100,9 +100,11 @@ int avd_init_job(struct avd_ctx *ctx, enum avd_codec codec, size_t segments) job->codec = codec; job->num = 0; + job->overflow = false; /* A table that was built but never submitted still belongs to us. */ kvfree(job->segments); job->segments = kvcalloc(segments, sizeof(*job->segments), GFP_KERNEL); + job->max = job->segments ? segments : 0; if (!job->segments) ret = -ENOMEM; return ret; diff --git a/drivers/media/platform/apple/avd/avd-h264.c b/drivers/media/platform/apple/avd/avd-h264.c index db4830e7aca22..6d2b4ff0643c3 100644 --- a/drivers/media/platform/apple/avd/avd-h264.c +++ b/drivers/media/platform/apple/avd/avd-h264.c @@ -854,6 +854,7 @@ static int avd_h264_realloc_slices(struct avd_ctx *ctx, size_t max_slices) memcpy(ctx->job.segments, tmp, sizeof(*ctx->job.segments) * (ctx->job.num + 1)); + ctx->job.max = alloc_slice_num + 1; kvfree(tmp); return 0; diff --git a/drivers/media/platform/apple/avd/avd-inst.h b/drivers/media/platform/apple/avd/avd-inst.h index adf670a79645c..d35efe68ebd51 100644 --- a/drivers/media/platform/apple/avd/avd-inst.h +++ b/drivers/media/platform/apple/avd/avd-inst.h @@ -169,7 +169,17 @@ static inline bool boolify(u32 v) static inline void push(struct avd_ctx *ctx, u32 inst) { struct avd_job *job = &ctx->job; - struct avd_segment *seg = &job->segments[job->num]; + struct avd_segment *seg; + + if (job->num >= job->max) { + job->overflow = true; + return; + } + seg = &job->segments[job->num]; + if (seg->num >= AVD_MAX_INST) { + job->overflow = true; + return; + } seg->instructions[seg->num++] = inst; } diff --git a/drivers/media/platform/apple/avd/avd-job-test.c b/drivers/media/platform/apple/avd/avd-job-test.c new file mode 100644 index 0000000000000..8263cddfd88ec --- /dev/null +++ b/drivers/media/platform/apple/avd/avd-job-test.c @@ -0,0 +1,85 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * KUnit tests for the instruction job table of the Apple video decoder. + */ + +#include + +#include "avd.h" +#include "avd-inst.h" + +static void avd_job_free(void *data) +{ + struct avd_ctx *ctx = data; + + kvfree(ctx->job.segments); +} + +static struct avd_ctx *avd_job_test_ctx(struct kunit *test, size_t segments) +{ + struct avd_ctx *ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL); + + KUNIT_ASSERT_NOT_NULL(test, ctx); + KUNIT_ASSERT_EQ(test, avd_init_job(ctx, AVD_CODEC_H264, segments), 0); + KUNIT_ASSERT_EQ(test, kunit_add_action_or_reset(test, avd_job_free, ctx), + 0); + KUNIT_EXPECT_EQ(test, ctx->job.max, segments); + KUNIT_EXPECT_FALSE(test, ctx->job.overflow); + return ctx; +} + +/* A full segment takes no more instructions. */ +static void avd_job_segment_full_test(struct kunit *test) +{ + struct avd_ctx *ctx = avd_job_test_ctx(test, 2); + int i; + + for (i = 0; i < AVD_MAX_INST; i++) + push(i, "fill"); + KUNIT_EXPECT_EQ(test, ctx->job.segments[0].num, AVD_MAX_INST); + KUNIT_EXPECT_FALSE(test, ctx->job.overflow); + + push(0xdeadbeef, "one too many"); + KUNIT_EXPECT_EQ(test, ctx->job.segments[0].num, AVD_MAX_INST); + KUNIT_EXPECT_TRUE(test, ctx->job.overflow); + /* The next segment was not written either. */ + KUNIT_EXPECT_EQ(test, ctx->job.segments[1].num, 0); +} + +/* Segments past the allocated table are never written. */ +static void avd_job_table_full_test(struct kunit *test) +{ + struct avd_ctx *ctx = avd_job_test_ctx(test, 2); + int i; + + push(1, "header"); + ctx->job.num++; + push(2, "first segment"); + KUNIT_EXPECT_FALSE(test, ctx->job.overflow); + + for (i = 0; i < 8; i++) { + ctx->job.num++; + push(3, "past the table"); + } + KUNIT_EXPECT_TRUE(test, ctx->job.overflow); + KUNIT_EXPECT_EQ(test, ctx->job.segments[0].num, 1); + KUNIT_EXPECT_EQ(test, ctx->job.segments[1].num, 1); + + /* A new job starts clean. */ + KUNIT_ASSERT_EQ(test, avd_init_job(ctx, AVD_CODEC_H264, 1), 0); + KUNIT_EXPECT_FALSE(test, ctx->job.overflow); + KUNIT_EXPECT_EQ(test, ctx->job.max, 1); +} + +static struct kunit_case avd_job_test_cases[] = { + KUNIT_CASE(avd_job_segment_full_test), + KUNIT_CASE(avd_job_table_full_test), + {} +}; + +static struct kunit_suite avd_job_test_suite = { + .name = "apple-avd-job", + .test_cases = avd_job_test_cases, +}; + +kunit_test_suite(avd_job_test_suite); diff --git a/drivers/media/platform/apple/avd/avd.h b/drivers/media/platform/apple/avd/avd.h index 7ec7d5a9fcd30..c77f18f7ad288 100644 --- a/drivers/media/platform/apple/avd/avd.h +++ b/drivers/media/platform/apple/avd/avd.h @@ -302,7 +302,12 @@ struct avd_segment { struct avd_job { enum avd_codec codec; + /* index of the segment being written */ size_t num; + /* segments allocated */ + size_t max; + /* an instruction did not fit; the job must not be submitted */ + bool overflow; struct avd_segment *segments; }; From 259b966a937758c7f7d619c87ea34363297a61af Mon Sep 17 00:00:00 2001 From: Ryan Murray Date: Mon, 23 Nov 2026 08:12:13 +1100 Subject: [PATCH 02/26] media: apple: avd: hevc: check the tile layout against the picture 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 --- drivers/media/platform/apple/avd/Makefile | 4 +- .../media/platform/apple/avd/avd-hevc-test.c | 153 ++++++++++++++++++ drivers/media/platform/apple/avd/avd-hevc.c | 125 ++++++++++++-- drivers/media/platform/apple/avd/avd.h | 6 + 4 files changed, 277 insertions(+), 11 deletions(-) create mode 100644 drivers/media/platform/apple/avd/avd-hevc-test.c diff --git a/drivers/media/platform/apple/avd/Makefile b/drivers/media/platform/apple/avd/Makefile index 278cf2da78886..9c32e537a4f83 100644 --- a/drivers/media/platform/apple/avd/Makefile +++ b/drivers/media/platform/apple/avd/Makefile @@ -3,6 +3,6 @@ apple-avd-y := avd-drv.o avd-v4l2.o avd-hw.o avd-pm.o avd-h264.o avd-vp9.o \ avd-hevc.o avd-av1.o avd-av1-entropymode.o avd-queue.o avd-backend-open.o \ avd-backend-apple.o avd-backend-queue.o avd-backend-kopsia.o -apple-avd-$(CONFIG_VIDEO_APPLE_AVD_KUNIT_TEST) += avd-h264-test.o avd-queue-test.o \ - avd-pm-test.o avd-job-test.o +apple-avd-$(CONFIG_VIDEO_APPLE_AVD_KUNIT_TEST) += avd-h264-test.o \ + avd-queue-test.o avd-pm-test.o avd-hevc-test.o avd-job-test.o obj-$(CONFIG_VIDEO_APPLE_AVD) += apple-avd.o diff --git a/drivers/media/platform/apple/avd/avd-hevc-test.c b/drivers/media/platform/apple/avd/avd-hevc-test.c new file mode 100644 index 0000000000000..50db548b19e28 --- /dev/null +++ b/drivers/media/platform/apple/avd/avd-hevc-test.c @@ -0,0 +1,153 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * KUnit tests for the HEVC picture, tile and slice checks of the Apple video + * decoder. + */ + +#include + +#include "avd.h" + +/* 64x64 luma samples in 16x16 CTBs: 4x4 CTBs. */ +static void avd_hevc_test_sps(struct v4l2_ctrl_hevc_sps *sps) +{ + *sps = (struct v4l2_ctrl_hevc_sps) { + .pic_width_in_luma_samples = 64, + .pic_height_in_luma_samples = 64, + .log2_min_luma_coding_block_size_minus3 = 0, + .log2_diff_max_min_luma_coding_block_size = 1, + }; +} + +struct avd_hevc_geometry_case { + const char *desc; + u16 width; + u16 height; + u8 min_cb_minus3; + u8 diff; + int ret; +}; + +static const struct avd_hevc_geometry_case avd_hevc_geometry_cases[] = { + { "16x16 CTBs", 64, 64, 0, 1, 0 }, + { "32x32 CTBs", 1920, 1080, 0, 2, 0 }, + { "64x64 CTBs", 4096, 2160, 0, 3, 0 }, + { "64x64 CTBs from 16x16 coding blocks", 4096, 2160, 1, 2, 0 }, + { "8x8 CTBs", 64, 64, 0, 0, -EINVAL }, + { "128x128 CTBs", 64, 64, 0, 4, -EINVAL }, + { "coding block size beyond any CTB", 64, 64, 255, 255, -EINVAL }, + { "zero width", 0, 64, 0, 1, -EINVAL }, + { "zero height", 64, 0, 0, 1, -EINVAL }, +}; + +KUNIT_ARRAY_PARAM_DESC(avd_hevc_geometry, avd_hevc_geometry_cases, desc); + +static void avd_hevc_geometry_test(struct kunit *test) +{ + const struct avd_hevc_geometry_case *c = test->param_value; + struct v4l2_ctrl_hevc_sps sps = { + .pic_width_in_luma_samples = c->width, + .pic_height_in_luma_samples = c->height, + .log2_min_luma_coding_block_size_minus3 = c->min_cb_minus3, + .log2_diff_max_min_luma_coding_block_size = c->diff, + }; + + KUNIT_EXPECT_EQ(test, avd_hevc_check_sps_geometry(&sps), c->ret); +} + +struct avd_hevc_tiles_case { + const char *desc; + bool uniform; + u8 cols_minus1; + u8 rows_minus1; + u8 col_width_minus1[2]; + u8 row_height_minus1[2]; + int ret; + /* tile id of every CTB in tile scan order, if accepted */ + u32 ids[16]; +}; + +static const struct avd_hevc_tiles_case avd_hevc_tiles_cases[] = { + { "explicit 2x2 tiles", false, 1, 1, { 0 }, { 1 }, 0, + { 0, 0, 1, 1, 1, 1, 1, 1, 2, 2, 3, 3, 3, 3, 3, 3 } }, + { "explicit 3x1 tiles", false, 2, 0, { 1, 0 }, { 0 }, 0, + { 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2 } }, + { "uniform 3x1 tiles", true, 2, 0, { 0 }, { 0 }, 0, + { 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2 } }, + { "uniform 4x4 tiles", true, 3, 3, { 0 }, { 0 }, 0, + { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 } }, + { "explicit columns leave the last one empty", false, 1, 0, { 3 }, + { 0 }, -EINVAL }, + { "explicit columns wider than the picture", false, 1, 0, { 4 }, + { 0 }, -EINVAL }, + { "explicit columns far wider than the picture", false, 2, 0, + { 200, 200 }, { 0 }, -EINVAL }, + { "explicit rows taller than the picture", false, 0, 1, { 0 }, + { 4 }, -EINVAL }, + { "explicit rows leave the last one empty", false, 0, 2, { 0 }, + { 1, 1 }, -EINVAL }, + { "uniform, more columns than CTBs", true, 4, 0, { 0 }, { 0 }, + -EINVAL }, + { "uniform, more rows than CTBs", true, 0, 4, { 0 }, { 0 }, + -EINVAL }, +}; + +KUNIT_ARRAY_PARAM_DESC(avd_hevc_tiles, avd_hevc_tiles_cases, desc); + +static void avd_hevc_tiles_test(struct kunit *test) +{ + const struct avd_hevc_tiles_case *c = test->param_value; + struct v4l2_ctrl_hevc_sps sps; + struct v4l2_ctrl_hevc_pps pps = { + .flags = V4L2_HEVC_PPS_FLAG_TILES_ENABLED | + (c->uniform ? V4L2_HEVC_PPS_FLAG_UNIFORM_SPACING : 0), + .num_tile_columns_minus1 = c->cols_minus1, + .num_tile_rows_minus1 = c->rows_minus1, + }; + u32 ids[16]; + + avd_hevc_test_sps(&sps); + memcpy(pps.column_width_minus1, c->col_width_minus1, + sizeof(c->col_width_minus1)); + memcpy(pps.row_height_minus1, c->row_height_minus1, + sizeof(c->row_height_minus1)); + + KUNIT_EXPECT_EQ(test, avd_hevc_check_tiles(&sps, &pps), c->ret); + + /* The table builder must refuse the same layouts, before any write. */ + memset(ids, 0xff, sizeof(ids)); + KUNIT_ASSERT_EQ(test, + avd_hevc_kunit_compute_tiles(&sps, &pps, ids, + ARRAY_SIZE(ids)), + c->ret); + if (!c->ret) + KUNIT_EXPECT_MEMEQ(test, ids, c->ids, sizeof(ids)); +} + +static void avd_hevc_no_tiles_test(struct kunit *test) +{ + struct v4l2_ctrl_hevc_sps sps; + struct v4l2_ctrl_hevc_pps pps = {}; + u32 ids[16]; + + avd_hevc_test_sps(&sps); + KUNIT_EXPECT_EQ(test, avd_hevc_check_tiles(&sps, &pps), 0); + KUNIT_EXPECT_EQ(test, + avd_hevc_kunit_compute_tiles(&sps, &pps, ids, + ARRAY_SIZE(ids)), + 0); +} + +static struct kunit_case avd_hevc_test_cases[] = { + KUNIT_CASE_PARAM(avd_hevc_geometry_test, avd_hevc_geometry_gen_params), + KUNIT_CASE_PARAM(avd_hevc_tiles_test, avd_hevc_tiles_gen_params), + KUNIT_CASE(avd_hevc_no_tiles_test), + {} +}; + +static struct kunit_suite avd_hevc_test_suite = { + .name = "apple-avd-hevc", + .test_cases = avd_hevc_test_cases, +}; + +kunit_test_suite(avd_hevc_test_suite); diff --git a/drivers/media/platform/apple/avd/avd-hevc.c b/drivers/media/platform/apple/avd/avd-hevc.c index 04d43b759f0d7..58e90ebc7c403 100644 --- a/drivers/media/platform/apple/avd/avd-hevc.c +++ b/drivers/media/platform/apple/avd/avd-hevc.c @@ -13,6 +13,7 @@ * Tomasz Figa */ +#include #include #include "avd.h" @@ -832,20 +833,25 @@ static void compute_rs_to_ts(struct avd_hevc_run *run, u32 pic_in_ctbs_size, } } -static void compute_tile_ids(struct avd_hevc_run *run, u32 pic_in_ctbs_width, - u32 *col_bd, u32 *row_bd, u32 *ctb_addr_rs_to_ts, - u32 *tile_ids) +static void compute_tile_ids(struct avd_hevc_run *run, u32 pic_in_ctbs_size, + u32 pic_in_ctbs_width, u32 *col_bd, u32 *row_bd, + u32 *ctb_addr_rs_to_ts, u32 *tile_ids) { const struct v4l2_ctrl_hevc_pps *pps = run->pps; int j, i, y, x; - u32 tile_idx; + u32 tile_idx, rs, ts; + for (j = 0, tile_idx = 0; j <= pps->num_tile_rows_minus1; j++) for (i = 0; i <= pps->num_tile_columns_minus1; i++, tile_idx++) for (y = row_bd[j]; y < row_bd[j + 1]; y++) - for (x = col_bd[i]; x < col_bd[i + 1]; x++) - tile_ids[ctb_addr_rs_to_ts - [y * pic_in_ctbs_width + - x]] = tile_idx; + for (x = col_bd[i]; x < col_bd[i + 1]; x++) { + rs = y * pic_in_ctbs_width + x; + if (rs >= pic_in_ctbs_size) + continue; + ts = ctb_addr_rs_to_ts[rs]; + if (ts < pic_in_ctbs_size) + tile_ids[ts] = tile_idx; + } } struct sl_ctx { @@ -1046,9 +1052,76 @@ static enum avd_image_fmt avd_hevc_get_image_fmt(struct avd_ctx *ctx, return AVD_IMG_FMT_ANY; } +/* + * The picture must have at least one sample in each direction and the CTB + * size must be one of those H.265 allows (CtbLog2SizeY 4 to 6, 7.4.3.2.1): + * the picture size in CTBs sizes the tile tables and divides slice addresses. + */ +VISIBLE_IF_KUNIT int +avd_hevc_check_sps_geometry(const struct v4l2_ctrl_hevc_sps *sps) +{ + unsigned int ctb_log2 = sps->log2_min_luma_coding_block_size_minus3 + 3 + + sps->log2_diff_max_min_luma_coding_block_size; + + if (!sps->pic_width_in_luma_samples || !sps->pic_height_in_luma_samples) + return -EINVAL; + if (ctb_log2 < 4 || ctb_log2 > 6) + return -EINVAL; + return 0; +} +EXPORT_SYMBOL_IF_KUNIT(avd_hevc_check_sps_geometry); + +static void avd_hevc_pic_in_ctbs(const struct v4l2_ctrl_hevc_sps *sps, + u32 *width, u32 *height) +{ + u32 ctb = 1 << (sps->log2_min_luma_coding_block_size_minus3 + 3 + + sps->log2_diff_max_min_luma_coding_block_size); + + *width = DIV_ROUND_UP(sps->pic_width_in_luma_samples, ctb); + *height = DIV_ROUND_UP(sps->pic_height_in_luma_samples, ctb); +} + +/* + * The tile columns and rows must cover the picture, each at least one CTB + * wide or high (H.265 7.4.3.3.1). Explicitly sized tiles give all but the + * last width and height; the last one takes the rest and must not be empty. + * avd_hevc_check_sps_geometry() must have accepted the SPS. + */ +VISIBLE_IF_KUNIT int avd_hevc_check_tiles(const struct v4l2_ctrl_hevc_sps *sps, + const struct v4l2_ctrl_hevc_pps *pps) +{ + u32 width, height, sum; + int i; + + if (!(pps->flags & V4L2_HEVC_PPS_FLAG_TILES_ENABLED)) + return 0; + + avd_hevc_pic_in_ctbs(sps, &width, &height); + if (pps->num_tile_columns_minus1 >= width || + pps->num_tile_rows_minus1 >= height) + return -EINVAL; + if (pps->flags & V4L2_HEVC_PPS_FLAG_UNIFORM_SPACING) + return 0; + + for (i = 0, sum = 0; i < pps->num_tile_columns_minus1; i++) + sum += pps->column_width_minus1[i] + 1; + if (sum >= width) + return -EINVAL; + + for (i = 0, sum = 0; i < pps->num_tile_rows_minus1; i++) + sum += pps->row_height_minus1[i] + 1; + if (sum >= height) + return -EINVAL; + + return 0; +} +EXPORT_SYMBOL_IF_KUNIT(avd_hevc_check_tiles); + static int avd_hevc_validate_sps(struct avd_ctx *ctx, const struct v4l2_ctrl_hevc_sps *sps) { + if (avd_hevc_check_sps_geometry(sps)) + return -EINVAL; if (sps->pic_width_in_luma_samples > ctx->coded_fmt.fmt.pix_mp.width || sps->pic_height_in_luma_samples > ctx->coded_fmt.fmt.pix_mp.height) return -EINVAL; @@ -1292,6 +1365,9 @@ static int avd_hevc_compute_tiles(struct avd_ctx *ctx, struct avd_hevc_run *run) tiles_enabled = !!(pps->flags & V4L2_HEVC_PPS_FLAG_TILES_ENABLED); + if (avd_hevc_check_tiles(sps, pps)) + return -EINVAL; + tile_info->ctb_addr_rs_to_ts = kzalloc( sizeof(*tile_info->ctb_addr_rs_to_ts) * pic_in_ctbs_size, GFP_KERNEL); @@ -1332,7 +1408,8 @@ static int avd_hevc_compute_tiles(struct avd_ctx *ctx, struct avd_hevc_run *run) tile_info->col_bd, tile_info->row_bd, tile_info->col_width, tile_info->row_height, tile_info->ctb_addr_rs_to_ts); - compute_tile_ids(run, pic_in_ctbs_width, tile_info->col_bd, + compute_tile_ids(run, pic_in_ctbs_size, pic_in_ctbs_width, + tile_info->col_bd, tile_info->row_bd, tile_info->ctb_addr_rs_to_ts, tile_info->tile_ids); @@ -1346,6 +1423,36 @@ static int avd_hevc_compute_tiles(struct avd_ctx *ctx, struct avd_hevc_run *run) return 0; } +#if IS_ENABLED(CONFIG_KUNIT) +/* Build the tile tables of a picture and copy out tile_ids in tile scan order. */ +int avd_hevc_kunit_compute_tiles(const struct v4l2_ctrl_hevc_sps *sps, + const struct v4l2_ctrl_hevc_pps *pps, + u32 *tile_ids, size_t n) +{ + struct avd_hevc_run *run; + u32 width, height; + int ret; + + run = kzalloc(sizeof(*run), GFP_KERNEL); + if (!run) + return -ENOMEM; + run->sps = sps; + run->pps = pps; + ret = avd_hevc_compute_tiles(NULL, run); + if (!ret) { + avd_hevc_pic_in_ctbs(sps, &width, &height); + memcpy(tile_ids, run->tile_info.tile_ids, + min_t(size_t, n, (size_t)width * height) * + sizeof(*tile_ids)); + } + kfree(run->tile_info.ctb_addr_rs_to_ts); + kfree(run->tile_info.tile_ids); + kfree(run); + return ret; +} +EXPORT_SYMBOL_IF_KUNIT(avd_hevc_kunit_compute_tiles); +#endif + /* * The slice builder below expects the GStreamer layout: the slices tile the * OUTPUT buffer as whole NALs, bit_size covers the whole NAL and diff --git a/drivers/media/platform/apple/avd/avd.h b/drivers/media/platform/apple/avd/avd.h index c77f18f7ad288..7d10b2733a2ab 100644 --- a/drivers/media/platform/apple/avd/avd.h +++ b/drivers/media/platform/apple/avd/avd.h @@ -377,6 +377,12 @@ bool avd_h264_slice_fits(const struct v4l2_ctrl_h264_sps *sps, size_t held, size_t held_bytes, u32 len, size_t decoded_size); bool avd_h264_scaling_is_flat(const struct v4l2_ctrl_h264_pps *pps, const struct v4l2_ctrl_h264_scaling_matrix *scaling); +int avd_hevc_check_sps_geometry(const struct v4l2_ctrl_hevc_sps *sps); +int avd_hevc_check_tiles(const struct v4l2_ctrl_hevc_sps *sps, + const struct v4l2_ctrl_hevc_pps *pps); +int avd_hevc_kunit_compute_tiles(const struct v4l2_ctrl_hevc_sps *sps, + const struct v4l2_ctrl_hevc_pps *pps, + u32 *tile_ids, size_t n); #endif extern const struct avd_coded_fmt_ops avd_h264_fmt_ops; From 966778e4384097d84a6a57e142e4bf20fef6b9c9 Mon Sep 17 00:00:00 2001 From: Ryan Murray Date: Mon, 23 Nov 2026 08:12:17 +1100 Subject: [PATCH 03/26] media: apple: avd: vp9: bound the tile counts 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 --- drivers/media/platform/apple/avd/Makefile | 3 +- .../media/platform/apple/avd/avd-vp9-test.c | 61 +++++++++++++++++++ drivers/media/platform/apple/avd/avd-vp9.c | 46 ++++++++++++-- drivers/media/platform/apple/avd/avd.h | 1 + 4 files changed, 104 insertions(+), 7 deletions(-) create mode 100644 drivers/media/platform/apple/avd/avd-vp9-test.c diff --git a/drivers/media/platform/apple/avd/Makefile b/drivers/media/platform/apple/avd/Makefile index 9c32e537a4f83..be42b7f645da8 100644 --- a/drivers/media/platform/apple/avd/Makefile +++ b/drivers/media/platform/apple/avd/Makefile @@ -4,5 +4,6 @@ apple-avd-y := avd-drv.o avd-v4l2.o avd-hw.o avd-pm.o avd-h264.o avd-vp9.o \ avd-hevc.o avd-av1.o avd-av1-entropymode.o avd-queue.o avd-backend-open.o \ avd-backend-apple.o avd-backend-queue.o avd-backend-kopsia.o apple-avd-$(CONFIG_VIDEO_APPLE_AVD_KUNIT_TEST) += avd-h264-test.o \ - avd-queue-test.o avd-pm-test.o avd-hevc-test.o avd-job-test.o + avd-queue-test.o avd-pm-test.o avd-hevc-test.o avd-job-test.o \ + avd-vp9-test.o obj-$(CONFIG_VIDEO_APPLE_AVD) += apple-avd.o diff --git a/drivers/media/platform/apple/avd/avd-vp9-test.c b/drivers/media/platform/apple/avd/avd-vp9-test.c new file mode 100644 index 0000000000000..469de40988618 --- /dev/null +++ b/drivers/media/platform/apple/avd/avd-vp9-test.c @@ -0,0 +1,61 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * KUnit tests for the VP9 frame checks of the Apple video decoder. + */ + +#include + +#include "avd.h" + +struct avd_vp9_tiling_case { + const char *desc; + u16 width; + u8 tile_rows_log2; + u8 tile_cols_log2; + int ret; +}; + +static const struct avd_vp9_tiling_case avd_vp9_tiling_cases[] = { + { "one tile", 1920, 0, 0, 0 }, + { "two tile rows", 1920, 1, 0, 0 }, + { "four tile rows", 1920, 2, 0, 0 }, + { "eight tile rows", 1920, 3, 0, -EINVAL }, + { "2^26 tile rows", 4096, 26, 6, -EINVAL }, + { "2^31 tile rows", 4096, 31, 1, -EINVAL }, + { "255 tile rows log2", 1920, 255, 0, -EINVAL }, + { "four tile columns at 1920", 1920, 0, 2, 0 }, + { "eight tile columns at 1920", 1920, 0, 3, -EINVAL }, + { "sixteen tile columns at 4096", 4096, 2, 4, 0 }, + { "32 tile columns at 4096", 4096, 0, 5, -EINVAL }, + { "64 tile columns at 4096", 4096, 0, 6, -EINVAL }, + { "two tile columns at 64", 64, 0, 1, -EINVAL }, + { "one tile column at 8192", 8192, 0, 0, -EINVAL }, + { "two tile columns at 8192", 8192, 0, 1, 0 }, +}; + +KUNIT_ARRAY_PARAM_DESC(avd_vp9_tiling, avd_vp9_tiling_cases, desc); + +static void avd_vp9_tiling_test(struct kunit *test) +{ + const struct avd_vp9_tiling_case *c = test->param_value; + struct v4l2_ctrl_vp9_frame frame = { + .frame_width_minus_1 = c->width - 1, + .frame_height_minus_1 = 1079, + .tile_rows_log2 = c->tile_rows_log2, + .tile_cols_log2 = c->tile_cols_log2, + }; + + KUNIT_EXPECT_EQ(test, avd_vp9_check_tiling(&frame), c->ret); +} + +static struct kunit_case avd_vp9_test_cases[] = { + KUNIT_CASE_PARAM(avd_vp9_tiling_test, avd_vp9_tiling_gen_params), + {} +}; + +static struct kunit_suite avd_vp9_test_suite = { + .name = "apple-avd-vp9", + .test_cases = avd_vp9_test_cases, +}; + +kunit_test_suite(avd_vp9_test_suite); diff --git a/drivers/media/platform/apple/avd/avd-vp9.c b/drivers/media/platform/apple/avd/avd-vp9.c index a596b420ac2c7..769383460342b 100644 --- a/drivers/media/platform/apple/avd/avd-vp9.c +++ b/drivers/media/platform/apple/avd/avd-vp9.c @@ -14,6 +14,7 @@ * Alpha Lin */ +#include #include #include @@ -780,6 +781,40 @@ static int avd_vp9_alloc_bufs(struct avd_ctx *ctx, struct avd_vp9_bank *bank) return 0; } +/* + * VP9 6.2.13 tile_info(): tile_rows_log2 is 0, 1 or 2, and tile_cols_log2 + * lies between calc_min_log2_tile_cols() and calc_max_log2_tile_cols() of + * 6.2.14, which follow from the frame width. The job has one segment per + * tile and the per-column scratch is sized for the column count this allows. + */ +#define VP9_MIN_TILE_WIDTH_B64 4 +#define VP9_MAX_TILE_WIDTH_B64 64 +#define VP9_MAX_TILE_ROWS_LOG2 2 + +VISIBLE_IF_KUNIT int +avd_vp9_check_tiling(const struct v4l2_ctrl_vp9_frame *frame) +{ + /* 6.2.6 Compute image size syntax */ + u32 sb_64_cols = (((frame->frame_width_minus_1 + 8) >> 3) + 7) >> 3; + u32 min_log2 = 0, max_log2 = 1; + + if (frame->tile_rows_log2 > VP9_MAX_TILE_ROWS_LOG2) + return -EINVAL; + + while ((VP9_MAX_TILE_WIDTH_B64 << min_log2) < sb_64_cols) + min_log2++; + while ((sb_64_cols >> max_log2) >= VP9_MIN_TILE_WIDTH_B64) + max_log2++; + max_log2--; + + if (frame->tile_cols_log2 < min_log2 || + frame->tile_cols_log2 > max_log2) + return -EINVAL; + + return 0; +} +EXPORT_SYMBOL_IF_KUNIT(avd_vp9_check_tiling); + /* * Only VP9 profiles 0 and 2 (4:2:0, 8 and 10 bits) decode. A profile 1 * frame (4:2:2 or 4:4:4) is never completed: it times out and the whole @@ -796,7 +831,7 @@ static int avd_vp9_validate_frame(struct avd_ctx *ctx, if (frame->bit_depth != (frame->profile == 2 ? 10 : 8)) return -EINVAL; - return 0; + return avd_vp9_check_tiling(frame); } static int avd_vp9_run_preamble(struct avd_ctx *ctx, struct avd_vp9_run *run) @@ -880,11 +915,10 @@ static int avd_vp9_run(struct avd_ctx *ctx) return ret; } - ret = avd_init_job( - ctx, AVD_CODEC_VP9, - (1 << run.decode_params->tile_rows_log2) * - (1 << run.decode_params->tile_cols_log2) + - 1); + /* One segment per tile, bounded by avd_vp9_check_tiling(), and the header. */ + ret = avd_init_job(ctx, AVD_CODEC_VP9, + ((size_t)1 << (run.decode_params->tile_rows_log2 + + run.decode_params->tile_cols_log2)) + 1); if (ret) { avd_run_postamble(ctx, &run.base); return ret; diff --git a/drivers/media/platform/apple/avd/avd.h b/drivers/media/platform/apple/avd/avd.h index 7d10b2733a2ab..6fee27521eed2 100644 --- a/drivers/media/platform/apple/avd/avd.h +++ b/drivers/media/platform/apple/avd/avd.h @@ -383,6 +383,7 @@ int avd_hevc_check_tiles(const struct v4l2_ctrl_hevc_sps *sps, int avd_hevc_kunit_compute_tiles(const struct v4l2_ctrl_hevc_sps *sps, const struct v4l2_ctrl_hevc_pps *pps, u32 *tile_ids, size_t n); +int avd_vp9_check_tiling(const struct v4l2_ctrl_vp9_frame *frame); #endif extern const struct avd_coded_fmt_ops avd_h264_fmt_ops; From 35ffdcf01277e5d32a9b836792a5ab1187a71a62 Mon Sep 17 00:00:00 2001 From: Ryan Murray Date: Mon, 23 Nov 2026 08:12:24 +1100 Subject: [PATCH 04/26] media: v4l2-ctrls: validate VP9 tile_rows_log2 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 --- drivers/media/v4l2-core/v4l2-ctrls-core.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/drivers/media/v4l2-core/v4l2-ctrls-core.c b/drivers/media/v4l2-core/v4l2-ctrls-core.c index 648b88c868bc6..18718345a7ddd 100644 --- a/drivers/media/v4l2-core/v4l2-ctrls-core.c +++ b/drivers/media/v4l2-core/v4l2-ctrls-core.c @@ -634,6 +634,10 @@ validate_vp9_frame(struct v4l2_ctrl_vp9_frame *frame) if (frame->tile_cols_log2 > 6) return -EINVAL; + /* tile_rows_log2 is coded as up to two one-bit increments. */ + if (frame->tile_rows_log2 > 2) + return -EINVAL; + if (frame->reference_mode > V4L2_VP9_REFERENCE_MODE_SELECT) return -EINVAL; From 276447127c767f01aa8055fd5c1322c26288a2cc Mon Sep 17 00:00:00 2001 From: Ryan Murray Date: Mon, 23 Nov 2026 09:50:45 +1100 Subject: [PATCH 05/26] media: apple: avd: build HEVC and VP9 jobs from copies of their controls 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 --- drivers/media/platform/apple/avd/avd-hevc.c | 64 +++++++++++++++------ drivers/media/platform/apple/avd/avd-vp9.c | 41 ++++++++----- 2 files changed, 71 insertions(+), 34 deletions(-) diff --git a/drivers/media/platform/apple/avd/avd-hevc.c b/drivers/media/platform/apple/avd/avd-hevc.c index 58e90ebc7c403..9c52f50586f88 100644 --- a/drivers/media/platform/apple/avd/avd-hevc.c +++ b/drivers/media/platform/apple/avd/avd-hevc.c @@ -1498,9 +1498,34 @@ static void avd_hevc_normalize_slices(struct avd_hevc_run *run, u32 payload) } } +/* + * A copy of a control's current value. The caller holds the control lock, + * so the control must have been looked up before (v4l2_ctrl_find() takes + * that lock too). + */ +static void *avd_hevc_ctrl_dup(struct v4l2_ctrl *ctrl) +{ + if (!ctrl) + return NULL; + return kmemdup(ctrl->p_cur.p, ctrl->elems * ctrl->elem_size, + GFP_KERNEL); +} + +static void avd_hevc_free_run(struct avd_hevc_run *run) +{ + kfree(run->tile_info.ctb_addr_rs_to_ts); + kfree(run->tile_info.tile_ids); + kfree(run->sl); + kfree(run->entry_point_offsets); + kfree(run->decode); + kfree(run->sps); + kfree(run->pps); + kfree(run->scaling_matrix); +} + static int avd_hevc_run_preamble(struct avd_ctx *ctx, struct avd_hevc_run *run) { - struct v4l2_ctrl *ctrl, *sl, *ep; + struct v4l2_ctrl *sl, *ep, *decode, *sps, *pps, *scaling; u32 dst_len, mv_color_len; if (!v4l2_ctrl_find(&ctx->ctrl_hdl, V4L2_CID_STATELESS_HEVC_SLICE_PARAMS) || @@ -1518,20 +1543,15 @@ static int avd_hevc_run_preamble(struct avd_ctx *ctx, struct avd_hevc_run *run) if (run->base.setup_err) return run->base.setup_err; - ctrl = v4l2_ctrl_find(&ctx->ctrl_hdl, - V4L2_CID_STATELESS_HEVC_DECODE_PARAMS); - run->decode = ctrl ? ctrl->p_cur.p : NULL; - ctrl = v4l2_ctrl_find(&ctx->ctrl_hdl, V4L2_CID_STATELESS_HEVC_SPS); - run->sps = ctrl ? ctrl->p_cur.p : NULL; - ctrl = v4l2_ctrl_find(&ctx->ctrl_hdl, V4L2_CID_STATELESS_HEVC_PPS); - run->pps = ctrl ? ctrl->p_cur.p : NULL; - ctrl = v4l2_ctrl_find(&ctx->ctrl_hdl, - V4L2_CID_STATELESS_HEVC_SCALING_MATRIX); - run->scaling_matrix = ctrl ? ctrl->p_cur.p : NULL; - sl = v4l2_ctrl_find(&ctx->ctrl_hdl, V4L2_CID_STATELESS_HEVC_SLICE_PARAMS); ep = v4l2_ctrl_find(&ctx->ctrl_hdl, V4L2_CID_STATELESS_HEVC_ENTRY_POINT_OFFSETS); + decode = v4l2_ctrl_find(&ctx->ctrl_hdl, + V4L2_CID_STATELESS_HEVC_DECODE_PARAMS); + sps = v4l2_ctrl_find(&ctx->ctrl_hdl, V4L2_CID_STATELESS_HEVC_SPS); + pps = v4l2_ctrl_find(&ctx->ctrl_hdl, V4L2_CID_STATELESS_HEVC_PPS); + scaling = v4l2_ctrl_find(&ctx->ctrl_hdl, + V4L2_CID_STATELESS_HEVC_SCALING_MATRIX); /* * Setting a larger dynamic array on a queued request reallocates the @@ -1539,15 +1559,24 @@ static int avd_hevc_run_preamble(struct avd_ctx *ctx, struct avd_hevc_run *run) * the run needs while holding the control lock. */ v4l2_ctrl_lock(sl); + /* + * A plain VIDIOC_S_EXT_CTRLS can also change the picture controls + * while the job is built. The checks below and the builder must see + * the same values, so take copies of those too. + */ + run->decode = avd_hevc_ctrl_dup(decode); + run->sps = avd_hevc_ctrl_dup(sps); + run->pps = avd_hevc_ctrl_dup(pps); + run->scaling_matrix = avd_hevc_ctrl_dup(scaling); run->num_slices = sl->elems; run->sl = kmemdup(sl->p_cur.p, sl->elems * sl->elem_size, GFP_KERNEL); run->num_entry_point_offsets = ep->elems; run->entry_point_offsets = kmemdup(ep->p_cur.p, ep->elems * ep->elem_size, GFP_KERNEL); v4l2_ctrl_unlock(sl); - if (!run->sl || !run->entry_point_offsets) { - kfree(run->sl); - kfree(run->entry_point_offsets); + if (!run->sl || !run->entry_point_offsets || !run->decode || + !run->sps || !run->pps || !run->scaling_matrix) { + avd_hevc_free_run(run); avd_run_postamble(ctx, &run->base); return -ENOMEM; } @@ -1612,10 +1641,7 @@ static int avd_hevc_run(struct avd_ctx *ctx) /* The request was applied; complete it on every way out. */ avd_run_postamble(ctx, &run.base); done: - kfree(run.tile_info.ctb_addr_rs_to_ts); - kfree(run.tile_info.tile_ids); - kfree(run.sl); - kfree(run.entry_point_offsets); + avd_hevc_free_run(&run); return ret; } diff --git a/drivers/media/platform/apple/avd/avd-vp9.c b/drivers/media/platform/apple/avd/avd-vp9.c index 769383460342b..71b80796fd090 100644 --- a/drivers/media/platform/apple/avd/avd-vp9.c +++ b/drivers/media/platform/apple/avd/avd-vp9.c @@ -836,7 +836,7 @@ static int avd_vp9_validate_frame(struct avd_ctx *ctx, static int avd_vp9_run_preamble(struct avd_ctx *ctx, struct avd_vp9_run *run) { - struct v4l2_ctrl *ctrl; + struct v4l2_ctrl *ctrl, *hdr; const struct v4l2_ctrl_vp9_frame *dec_params; struct avd_vp9_ctx *vp9_ctx = ctx->priv; unsigned int fctx_idx; @@ -846,16 +846,25 @@ static int avd_vp9_run_preamble(struct avd_ctx *ctx, struct avd_vp9_run *run) if (run->base.setup_err) return run->base.setup_err; - ctrl = v4l2_ctrl_find(&ctx->ctrl_hdl, - V4L2_CID_STATELESS_VP9_COMPRESSED_HDR); - if (WARN_ON(!ctrl)) - return -EINVAL; - run->prob_updates = ctrl->p_cur.p; - + hdr = v4l2_ctrl_find(&ctx->ctrl_hdl, + V4L2_CID_STATELESS_VP9_COMPRESSED_HDR); ctrl = v4l2_ctrl_find(&ctx->ctrl_hdl, V4L2_CID_STATELESS_VP9_FRAME); - if (WARN_ON(!ctrl)) + if (WARN_ON(!hdr || !ctrl)) return -EINVAL; - dec_params = ctrl->p_cur.p; + + /* + * A plain VIDIOC_S_EXT_CTRLS can change the controls while the job is + * built; check and build from copies taken under the control lock. + */ + v4l2_ctrl_lock(ctrl); + run->prob_updates = kmemdup(hdr->p_cur.p, sizeof(*run->prob_updates), + GFP_KERNEL); + run->decode_params = kmemdup(ctrl->p_cur.p, + sizeof(*run->decode_params), GFP_KERNEL); + v4l2_ctrl_unlock(ctrl); + if (!run->prob_updates || !run->decode_params) + return -ENOMEM; + dec_params = run->decode_params; ret = validate_dec_params(ctx, dec_params); if (ret) @@ -866,8 +875,6 @@ static int avd_vp9_run_preamble(struct avd_ctx *ctx, struct avd_vp9_run *run) if (ret) return ret; - run->decode_params = dec_params; - if (avd_backend_is_qfw(ctx->dev)) { struct avd_vp9_bank *bank = NULL; @@ -904,7 +911,7 @@ static void avd_vp9_update_probs(struct avd_ctx *ctx, static int avd_vp9_run(struct avd_ctx *ctx) { - struct avd_vp9_run run; + struct avd_vp9_run run = {}; struct avd_vp9_ctx *vp9_ctx; struct avd_decoded_buffer *dst; int ret; @@ -912,7 +919,7 @@ static int avd_vp9_run(struct avd_ctx *ctx) ret = avd_vp9_run_preamble(ctx, &run); if (ret) { avd_run_postamble(ctx, &run.base); - return ret; + goto out; } /* One segment per tile, bounded by avd_vp9_check_tiling(), and the header. */ @@ -921,7 +928,7 @@ static int avd_vp9_run(struct avd_ctx *ctx) run.decode_params->tile_cols_log2)) + 1); if (ret) { avd_run_postamble(ctx, &run.base); - return ret; + goto out; } init_probs(ctx, &run); @@ -943,7 +950,11 @@ static int avd_vp9_run(struct avd_ctx *ctx) update_ctx_last_info(vp9_ctx); ctx->job_fifo = vp9_ctx->b->bufs.inst.addr; } - return avd_submit_job(ctx); + ret = avd_submit_job(ctx); +out: + kfree(run.decode_params); + kfree(run.prob_updates); + return ret; } #define copy_tx_and_skip(p1, p2) \ From be33a283e68fead18b2ccacb8e4cbf3a84b8e31d Mon Sep 17 00:00:00 2001 From: Ryan Murray Date: Mon, 23 Nov 2026 08:13:32 +1100 Subject: [PATCH 06/26] media: apple: avd: hevc: check slice addresses, references and data The slice builder indexed the CTB tables with each slice's slice_segment_addr, the reference list with collocated_ref_idx and the DPB with the list entry it found there, none of which were compared with the size of what they index. It also handed the decoder slice and entry point offsets and sizes without comparing them with the OUTPUT payload, so a size could underflow or point past the buffer. Before building the job, check every slice: its address must be a CTB of the picture, the collocated reference index must fit the reference list (and, with temporal MV prediction, its active part, H.265 7.4.7.1), the entry it selects must be a DPB entry, and each slice segment's data must lie inside the payload, using the same segment layout as the builder. Also stop building when the tile position runs past the tile boundary tables. Add KUnit cases for accepted and refused slices. Signed-off-by: Ryan Murray --- .../media/platform/apple/avd/avd-hevc-test.c | 143 ++++++++++++++++++ drivers/media/platform/apple/avd/avd-hevc.c | 110 ++++++++++++-- drivers/media/platform/apple/avd/avd.h | 6 + 3 files changed, 250 insertions(+), 9 deletions(-) diff --git a/drivers/media/platform/apple/avd/avd-hevc-test.c b/drivers/media/platform/apple/avd/avd-hevc-test.c index 50db548b19e28..efb0cf13b0d8f 100644 --- a/drivers/media/platform/apple/avd/avd-hevc-test.c +++ b/drivers/media/platform/apple/avd/avd-hevc-test.c @@ -138,10 +138,153 @@ static void avd_hevc_no_tiles_test(struct kunit *test) 0); } +struct avd_hevc_slices_case { + const char *desc; + bool tiles; + u8 type; + u32 flags; + u32 addr; + u8 col_idx; + u8 l0_last; + u8 l0_ref; + u32 bytes; + u32 data_offset; + u32 num_entry_points; + u32 entry_point; + u32 available_entry_points; + u32 payload; + int ret; +}; + +#define AVD_TMVP V4L2_HEVC_SLICE_PARAMS_FLAG_SLICE_TEMPORAL_MVP_ENABLED + +static const struct avd_hevc_slices_case avd_hevc_slices_cases[] = { + { "I slice at the first CTB", .type = V4L2_HEVC_SLICE_TYPE_I, + .bytes = 100, .data_offset = 10, .payload = 100 }, + { "I slice at the last CTB", .type = V4L2_HEVC_SLICE_TYPE_I, + .addr = 15, .bytes = 100, .data_offset = 10, .payload = 100 }, + { "slice address past the picture", .type = V4L2_HEVC_SLICE_TYPE_I, + .addr = 16, .bytes = 100, .payload = 100, .ret = -EINVAL }, + { "slice address far past the picture", .type = V4L2_HEVC_SLICE_TYPE_I, + .addr = U32_MAX, .bytes = 100, .payload = 100, .ret = -EINVAL }, + { "slice data ends one byte after the payload", + .type = V4L2_HEVC_SLICE_TYPE_I, .bytes = 100, .data_offset = 10, + .payload = 99, .ret = -EINVAL }, + { "slice header longer than the slice", .type = V4L2_HEVC_SLICE_TYPE_I, + .bytes = 10, .data_offset = 11, .payload = 100, .ret = -EINVAL }, + { "payload beyond the 31-bit range", .type = V4L2_HEVC_SLICE_TYPE_I, + .bytes = 100, .payload = U32_MAX, .ret = -EINVAL }, + { "P slice, collocated picture in the list", + .type = V4L2_HEVC_SLICE_TYPE_P, .flags = AVD_TMVP, .col_idx = 1, + .l0_last = 1, .l0_ref = 15, .bytes = 100, .payload = 100 }, + { "P slice, collocated index past the active list", + .type = V4L2_HEVC_SLICE_TYPE_P, .flags = AVD_TMVP, .col_idx = 2, + .l0_last = 1, .bytes = 100, .payload = 100, .ret = -EINVAL }, + { "P slice without TMVP, unused collocated index", + .type = V4L2_HEVC_SLICE_TYPE_P, .col_idx = 2, .l0_last = 1, + .bytes = 100, .payload = 100 }, + { "P slice, collocated index past the list array", + .type = V4L2_HEVC_SLICE_TYPE_P, .col_idx = 16, .l0_last = 15, + .bytes = 100, .payload = 100, .ret = -EINVAL }, + { "P slice, missing reference marked past the DPB", + .type = V4L2_HEVC_SLICE_TYPE_P, .l0_ref = 255, .bytes = 100, + .payload = 100 }, + { "B slice, missing L1 reference marked past the DPB", + .type = V4L2_HEVC_SLICE_TYPE_B, .l0_ref = 255, .bytes = 100, + .payload = 100 }, + { "two tiles, entry point inside the slice", .tiles = true, + .type = V4L2_HEVC_SLICE_TYPE_I, .bytes = 100, .data_offset = 10, + .num_entry_points = 1, .entry_point = 40, + .available_entry_points = 1, .payload = 100 }, + { "two tiles, entry point past the payload", .tiles = true, + .type = V4L2_HEVC_SLICE_TYPE_I, .bytes = 100, .data_offset = 10, + .num_entry_points = 1, .entry_point = 91, + .available_entry_points = 1, .payload = 100, .ret = -EINVAL }, + { "two tiles, entry point far past the payload", .tiles = true, + .type = V4L2_HEVC_SLICE_TYPE_I, .bytes = 100, .data_offset = 10, + .num_entry_points = 1, .entry_point = U32_MAX, + .available_entry_points = 1, .payload = 100, .ret = -EINVAL }, + { "two tiles, more entry points than given", .tiles = true, + .type = V4L2_HEVC_SLICE_TYPE_I, .bytes = 100, .data_offset = 10, + .num_entry_points = 2, .entry_point = 40, + .available_entry_points = 1, .payload = 100, .ret = -EINVAL }, +}; + +KUNIT_ARRAY_PARAM_DESC(avd_hevc_slices, avd_hevc_slices_cases, desc); + +static void avd_hevc_slices_test(struct kunit *test) +{ + const struct avd_hevc_slices_case *c = test->param_value; + struct v4l2_ctrl_hevc_decode_params *decode; + struct v4l2_ctrl_hevc_slice_params *sl; + struct v4l2_ctrl_hevc_sps sps; + struct v4l2_ctrl_hevc_pps pps = {}; + u32 ep[2] = { c->entry_point, c->entry_point }; + + decode = kunit_kzalloc(test, sizeof(*decode), GFP_KERNEL); + sl = kunit_kzalloc(test, sizeof(*sl), GFP_KERNEL); + KUNIT_ASSERT_NOT_NULL(test, decode); + KUNIT_ASSERT_NOT_NULL(test, sl); + + avd_hevc_test_sps(&sps); + if (c->tiles) { + pps.flags = V4L2_HEVC_PPS_FLAG_TILES_ENABLED | + V4L2_HEVC_PPS_FLAG_UNIFORM_SPACING; + pps.num_tile_columns_minus1 = 1; + } + decode->num_active_dpb_entries = 2; + sl->slice_type = c->type; + sl->flags = c->flags; + sl->slice_segment_addr = c->addr; + sl->collocated_ref_idx = c->col_idx; + sl->num_ref_idx_l0_active_minus1 = c->l0_last; + sl->num_ref_idx_l1_active_minus1 = c->l0_last; + memset(sl->ref_idx_l0, c->l0_ref, sizeof(sl->ref_idx_l0)); + memset(sl->ref_idx_l1, c->l0_ref, sizeof(sl->ref_idx_l1)); + sl->bit_size = c->bytes * 8; + sl->data_byte_offset = c->data_offset; + sl->num_entry_point_offsets = c->num_entry_points; + + KUNIT_EXPECT_EQ(test, + avd_hevc_check_slices(&sps, &pps, decode, sl, 1, ep, + c->available_entry_points, + c->payload), + c->ret); +} + +/* A slice only fits after the ones before it. */ +static void avd_hevc_two_slices_test(struct kunit *test) +{ + struct v4l2_ctrl_hevc_decode_params *decode; + struct v4l2_ctrl_hevc_slice_params *sl; + struct v4l2_ctrl_hevc_sps sps; + struct v4l2_ctrl_hevc_pps pps = {}; + u32 ep = 0; + + decode = kunit_kzalloc(test, sizeof(*decode), GFP_KERNEL); + sl = kunit_kcalloc(test, 2, sizeof(*sl), GFP_KERNEL); + KUNIT_ASSERT_NOT_NULL(test, decode); + KUNIT_ASSERT_NOT_NULL(test, sl); + + avd_hevc_test_sps(&sps); + sl[0].slice_type = V4L2_HEVC_SLICE_TYPE_I; + sl[0].bit_size = 60 * 8; + sl[0].data_byte_offset = 5; + sl[1] = sl[0]; + sl[1].slice_segment_addr = 8; + + KUNIT_EXPECT_EQ(test, avd_hevc_check_slices(&sps, &pps, decode, sl, 2, + &ep, 1, 120), 0); + KUNIT_EXPECT_EQ(test, avd_hevc_check_slices(&sps, &pps, decode, sl, 2, + &ep, 1, 119), -EINVAL); +} + static struct kunit_case avd_hevc_test_cases[] = { KUNIT_CASE_PARAM(avd_hevc_geometry_test, avd_hevc_geometry_gen_params), KUNIT_CASE_PARAM(avd_hevc_tiles_test, avd_hevc_tiles_gen_params), KUNIT_CASE(avd_hevc_no_tiles_test), + KUNIT_CASE_PARAM(avd_hevc_slices_test, avd_hevc_slices_gen_params), + KUNIT_CASE(avd_hevc_two_slices_test), {} }; diff --git a/drivers/media/platform/apple/avd/avd-hevc.c b/drivers/media/platform/apple/avd/avd-hevc.c index 9c52f50586f88..1f283c2058e3a 100644 --- a/drivers/media/platform/apple/avd/avd-hevc.c +++ b/drivers/media/platform/apple/avd/avd-hevc.c @@ -599,8 +599,9 @@ static void stream_slice_mv(struct avd_ctx *ctx, struct avd_hevc_run *run, { const struct v4l2_ctrl_hevc_decode_params *decode = run->decode; struct avd_decoded_buffer *dst, *ref; - bool ref_valid; + bool ref_valid, col_in_dpb; const u8 *ref_list; + u8 col; if (sl->slice_type == V4L2_HEVC_SLICE_TYPE_I) { push(AVD_OP_SL_REF | @@ -617,11 +618,17 @@ static void stream_slice_mv(struct avd_ctx *ctx, struct avd_hevc_run *run, sl->ref_idx_l1; dst = vb2_to_avd_decoded_buf(&run->base.bufs.dst->vb2_buf); - ref = avd_get_ref_buf( - ctx, &dst->base.vb, - decode->dpb[ref_list[sl->collocated_ref_idx]].timestamp); + /* + * Clients mark a reference they do not have with an index past the + * DPB (0xff); there is no collocated picture to take MVs from then. + */ + col = ref_list[sl->collocated_ref_idx]; + col_in_dpb = col < ARRAY_SIZE(decode->dpb); + ref = col_in_dpb ? + avd_get_ref_buf(ctx, &dst->base.vb, decode->dpb[col].timestamp) : + dst; - ref_valid = !(sl->flags & + ref_valid = col_in_dpb && !(sl->flags & V4L2_HEVC_SLICE_PARAMS_FLAG_DEPENDENT_SLICE_SEGMENT) && (sl->flags & V4L2_HEVC_SLICE_PARAMS_FLAG_SLICE_TEMPORAL_MVP_ENABLED) && @@ -662,8 +669,7 @@ static void stream_slice_mv(struct avd_ctx *ctx, struct avd_hevc_run *run, mv_color_size(fmt_width(ctx), fmt_height(ctx)), AVD_ALIGN); pusha(mv_color_addr, "slc_bd4_sps_tile_addr2_lsb8", - decode->dpb[ref_list[sl->collocated_ref_idx]] - .pic_order_cnt_val); + decode->dpb[col].pic_order_cnt_val); } } @@ -958,6 +964,10 @@ static int stream_slices(struct avd_ctx *ctx, struct avd_hevc_run *run) row = pos / num_cols; col = pos % num_cols; + /* More tiles than the boundary tables describe */ + if (tiles_enabled && (slice_flag & NEW_TILE_ID) && + row + 1 >= ARRAY_SIZE(tile_info->row_bd)) + return -EINVAL; /* * not a fan of this, in JCT-VC-HEVC_V1 only TILES_B_Cisco_1 @@ -1117,6 +1127,82 @@ VISIBLE_IF_KUNIT int avd_hevc_check_tiles(const struct v4l2_ctrl_hevc_sps *sps, } EXPORT_SYMBOL_IF_KUNIT(avd_hevc_check_tiles); +/* + * Check what the slice builder takes from the slice parameters before it + * uses them: the slice address must be a CTB of the picture, the collocated + * reference must be an entry of its reference list (H.265 7.4.7.1), and + * every slice segment's data must lie in the payload. The + * segment layout is the one stream_slices() programs. + * avd_hevc_check_sps_geometry() must have accepted the SPS. + */ +VISIBLE_IF_KUNIT int +avd_hevc_check_slices(const struct v4l2_ctrl_hevc_sps *sps, + const struct v4l2_ctrl_hevc_pps *pps, + const struct v4l2_ctrl_hevc_decode_params *decode, + const struct v4l2_ctrl_hevc_slice_params *slices, + u32 num_slices, const u32 *entry_points, + u32 num_entry_points, u32 payload) +{ + bool tiles = pps->flags & V4L2_HEVC_PPS_FLAG_TILES_ENABLED; + u32 width, height, ep = 0, s, i, to; + s64 offset = 0, seg_offset, size, next; + + if (payload > INT_MAX) + return -EINVAL; + avd_hevc_pic_in_ctbs(sps, &width, &height); + + for (s = 0; s < num_slices; s++) { + const struct v4l2_ctrl_hevc_slice_params *sl = &slices[s]; + s64 bytes = sl->bit_size / 8; + + if (sl->slice_segment_addr >= width * height) + return -EINVAL; + + if (sl->slice_type != V4L2_HEVC_SLICE_TYPE_I) { + bool l0 = sl->slice_type == V4L2_HEVC_SLICE_TYPE_P || + sl->flags & + V4L2_HEVC_SLICE_PARAMS_FLAG_COLLOCATED_FROM_L0; + u8 last = l0 ? sl->num_ref_idx_l0_active_minus1 : + sl->num_ref_idx_l1_active_minus1; + + if (sl->collocated_ref_idx >= ARRAY_SIZE(sl->ref_idx_l0)) + return -EINVAL; + if ((sl->flags & + V4L2_HEVC_SLICE_PARAMS_FLAG_SLICE_TEMPORAL_MVP_ENABLED) && + sl->collocated_ref_idx > last) + return -EINVAL; + } + + to = tiles ? sl->num_entry_point_offsets + 1 : 1; + if (tiles && sl->num_entry_point_offsets > + num_entry_points - ep) + return -EINVAL; + + seg_offset = 0; + for (i = 0; i < to; i++) { + if (to > 1 && i < sl->num_entry_point_offsets) { + size = entry_points[ep++]; + next = size; + } else if (to > 1) { + size = bytes - sl->data_byte_offset - seg_offset; + next = size + sl->data_byte_offset; + } else { + size = bytes - sl->data_byte_offset; + next = size + sl->data_byte_offset; + } + if (size < 0 || + offset + seg_offset + sl->data_byte_offset + size > + payload) + return -EINVAL; + seg_offset += next; + } + offset += bytes; + } + + return 0; +} +EXPORT_SYMBOL_IF_KUNIT(avd_hevc_check_slices); + static int avd_hevc_validate_sps(struct avd_ctx *ctx, const struct v4l2_ctrl_hevc_sps *sps) { @@ -1595,6 +1681,7 @@ static int avd_hevc_run(struct avd_ctx *ctx) struct avd_hevc_ctx *hevc_ctx = ctx->priv; struct avd_hevc_run run = {}; struct avd_decoded_buffer *dst; + u32 payload; int ret; ret = avd_hevc_run_preamble(ctx, &run); @@ -1606,8 +1693,13 @@ static int avd_hevc_run(struct avd_ctx *ctx) if (ret) goto complete; - avd_hevc_normalize_slices(&run, - vb2_get_plane_payload(&run.base.bufs.src->vb2_buf, 0)); + payload = vb2_get_plane_payload(&run.base.bufs.src->vb2_buf, 0); + avd_hevc_normalize_slices(&run, payload); + ret = avd_hevc_check_slices(run.sps, run.pps, run.decode, run.sl, + run.num_slices, run.entry_point_offsets, + run.num_entry_point_offsets, payload); + if (ret) + goto complete; dst = vb2_to_avd_decoded_buf(&run.base.bufs.dst->vb2_buf); update_dec_buf_info(dst, &run.sl[0]); diff --git a/drivers/media/platform/apple/avd/avd.h b/drivers/media/platform/apple/avd/avd.h index 6fee27521eed2..fdf6be6cbe054 100644 --- a/drivers/media/platform/apple/avd/avd.h +++ b/drivers/media/platform/apple/avd/avd.h @@ -380,6 +380,12 @@ bool avd_h264_scaling_is_flat(const struct v4l2_ctrl_h264_pps *pps, int avd_hevc_check_sps_geometry(const struct v4l2_ctrl_hevc_sps *sps); int avd_hevc_check_tiles(const struct v4l2_ctrl_hevc_sps *sps, const struct v4l2_ctrl_hevc_pps *pps); +int avd_hevc_check_slices(const struct v4l2_ctrl_hevc_sps *sps, + const struct v4l2_ctrl_hevc_pps *pps, + const struct v4l2_ctrl_hevc_decode_params *decode, + const struct v4l2_ctrl_hevc_slice_params *slices, + u32 num_slices, const u32 *entry_points, + u32 num_entry_points, u32 payload); int avd_hevc_kunit_compute_tiles(const struct v4l2_ctrl_hevc_sps *sps, const struct v4l2_ctrl_hevc_pps *pps, u32 *tile_ids, size_t n); From 8e206693b3cabb99adb2927ee1e8ec177577e132 Mon Sep 17 00:00:00 2001 From: Ryan Murray Date: Mon, 23 Nov 2026 08:13:25 +1100 Subject: [PATCH 07/26] media: apple: avd: vp9: keep the tiles within the bitstream set_tiles() takes the tile data offset from the two header sizes of the frame control and reads the size of every tile but the last from the OUTPUT buffer, without comparing either with the payload. The remaining size is a u32 that wraps when the header sizes exceed the payload, and the "tile_size > size - 4" check wraps when fewer than four bytes are left, so the walk can read and hand to the decoder offsets and sizes outside the buffer. The buffer mapping is not checked for NULL either. Factor the step into avd_vp9_next_tile(): it requires a mapping, the tile offset within the payload, four bytes for a tile size and the tile within what is left, all without wrapping, and a non-empty tile (VP9 9.2.1). Every tile handed to the decoder, the last one included, then lies within the payload. On a bad layout set_tiles() fails the frame with -EINVAL instead of submitting the tiles it has so far. Add KUnit tests for the tile walk. Signed-off-by: Ryan Murray --- .../media/platform/apple/avd/avd-vp9-test.c | 78 +++++++++++++++++++ drivers/media/platform/apple/avd/avd-vp9.c | 66 ++++++++++++---- drivers/media/platform/apple/avd/avd.h | 2 + 3 files changed, 130 insertions(+), 16 deletions(-) diff --git a/drivers/media/platform/apple/avd/avd-vp9-test.c b/drivers/media/platform/apple/avd/avd-vp9-test.c index 469de40988618..f0b501dffde17 100644 --- a/drivers/media/platform/apple/avd/avd-vp9-test.c +++ b/drivers/media/platform/apple/avd/avd-vp9-test.c @@ -48,8 +48,86 @@ static void avd_vp9_tiling_test(struct kunit *test) KUNIT_EXPECT_EQ(test, avd_vp9_check_tiling(&frame), c->ret); } +#define AVD_TEST_MAX_TILES 2 + +struct avd_vp9_tiles_case { + const char *desc; + const u8 *data; + u32 payload; + u32 header; + unsigned int tiles; + int ret; + u32 offsets[AVD_TEST_MAX_TILES]; + u32 sizes[AVD_TEST_MAX_TILES]; +}; + +/* Two tiles after a 2-byte header: a 3-byte tile, then the rest. */ +static const u8 avd_test_two_tiles[] = { + 0xaa, 0xbb, 0x00, 0x00, 0x00, 0x03, 0x01, 0x02, 0x03, 0x04, 0x05, +}; + +/* The first tile's size reaches one byte past the payload. */ +static const u8 avd_test_long_tile[] = { + 0xaa, 0xbb, 0x00, 0x00, 0x00, 0x06, 0x01, 0x02, 0x03, 0x04, 0x05, +}; + +/* The first tile's size is the largest u32. */ +static const u8 avd_test_huge_tile[] = { + 0xaa, 0xbb, 0xff, 0xff, 0xff, 0xff, 0x01, 0x02, 0x03, 0x04, 0x05, +}; + +/* The first tile fills the payload and leaves nothing for the last one. */ +static const u8 avd_test_no_last_tile[] = { + 0xaa, 0xbb, 0x00, 0x00, 0x00, 0x05, 0x01, 0x02, 0x03, 0x04, 0x05, +}; + +/* An empty first tile. */ +static const u8 avd_test_empty_tile[] = { + 0xaa, 0xbb, 0x00, 0x00, 0x00, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, +}; + +static const struct avd_vp9_tiles_case avd_vp9_tiles_cases[] = { + { "one tile", avd_test_two_tiles, 11, 2, 1, 0, { 2 }, { 9 } }, + { "two tiles", avd_test_two_tiles, 11, 2, 2, 0, { 6, 9 }, { 3, 2 } }, + { "headers beyond the payload", avd_test_two_tiles, 11, 12, 1, -EINVAL }, + { "headers fill the payload", avd_test_two_tiles, 11, 11, 1, -EINVAL }, + { "headers beyond the payload, two tiles", avd_test_two_tiles, 11, + 0xffff + 0xffff, 2, -EINVAL }, + { "three bytes for a tile size", avd_test_two_tiles, 5, 2, 2, -EINVAL }, + { "no bytes for a tile size", avd_test_two_tiles, 2, 2, 2, -EINVAL }, + { "tile beyond the payload", avd_test_long_tile, 11, 2, 2, -EINVAL }, + { "tile size wraps", avd_test_huge_tile, 11, 2, 2, -EINVAL }, + { "no data for the last tile", avd_test_no_last_tile, 11, 2, 2, -EINVAL, + { 6 }, { 5 } }, + { "empty tile", avd_test_empty_tile, 11, 2, 2, -EINVAL }, + { "no mapping", NULL, 11, 2, 1, -EINVAL }, +}; + +KUNIT_ARRAY_PARAM_DESC(avd_vp9_tiles, avd_vp9_tiles_cases, desc); + +/* Walks the tiles as set_tiles() does. */ +static void avd_vp9_tiles_test(struct kunit *test) +{ + const struct avd_vp9_tiles_case *c = test->param_value; + u32 offset = c->header, size; + int ret = 0; + + for (unsigned int i = 0; i < c->tiles; i++) { + ret = avd_vp9_next_tile(c->data, c->payload, i == c->tiles - 1, + &offset, &size); + if (ret) + break; + KUNIT_EXPECT_EQ(test, offset, c->offsets[i]); + KUNIT_EXPECT_EQ(test, size, c->sizes[i]); + KUNIT_EXPECT_LE(test, (u64)offset + size, (u64)c->payload); + offset += size; + } + KUNIT_EXPECT_EQ(test, ret, c->ret); +} + static struct kunit_case avd_vp9_test_cases[] = { KUNIT_CASE_PARAM(avd_vp9_tiling_test, avd_vp9_tiling_gen_params), + KUNIT_CASE_PARAM(avd_vp9_tiles_test, avd_vp9_tiles_gen_params), {} }; diff --git a/drivers/media/platform/apple/avd/avd-vp9.c b/drivers/media/platform/apple/avd/avd-vp9.c index 71b80796fd090..e72aeb8dc2c2f 100644 --- a/drivers/media/platform/apple/avd/avd-vp9.c +++ b/drivers/media/platform/apple/avd/avd-vp9.c @@ -493,19 +493,53 @@ static void set_header(struct avd_ctx *ctx, struct avd_vp9_run *run) set_refs(ctx, run); } -static void set_tiles(struct avd_ctx *ctx, struct avd_vp9_run *run) +/* + * Find the next tile in the frame (VP9 6.4 decode_tiles()): at @offset each + * tile but the last starts with its size as a 32-bit big-endian word, the + * last one takes the rest of the payload. Returns the tile's data offset in + * @offset and its size in @size, both within @payload. A tile holds at least + * one byte (9.2.1: sz is at least 1). + */ +VISIBLE_IF_KUNIT int avd_vp9_next_tile(const u8 *data, u32 payload, bool last, + u32 *offset, u32 *size) +{ + u32 off = *offset, tile_size; + + if (!data || off > payload) + return -EINVAL; + + if (last) { + tile_size = payload - off; + } else { + if (payload - off < 4) + return -EINVAL; + tile_size = get_unaligned_be32(&data[off]); + off += 4; + if (tile_size > payload - off) + return -EINVAL; + } + if (!tile_size) + return -EINVAL; + + *offset = off; + *size = tile_size; + return 0; +} +EXPORT_SYMBOL_IF_KUNIT(avd_vp9_next_tile); + +static int set_tiles(struct avd_ctx *ctx, struct avd_vp9_run *run) { const struct v4l2_ctrl_vp9_frame *frame = run->decode_params; struct avd_vp9_ctx *vp9_ctx = ctx->priv; struct vb2_v4l2_buffer *src = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx); const u8 *data = vb2_plane_vaddr(&src->vb2_buf, 0); - + u32 payload = vb2_get_plane_payload(&src->vb2_buf, 0); u32 offset = frame->uncompressed_header_size + frame->compressed_header_size; - u32 size = vb2_get_plane_payload(&src->vb2_buf, 0) - offset; u32 num_tile_rows = 1 << frame->tile_rows_log2; u32 num_tile_cols = 1 << frame->tile_cols_log2; u32 tile_size; + int ret; /* 6.2.6 Compute image size syntax */ u32 sb_64_cols = (((frame->frame_width_minus_1 + 8) >> 3) + 7) >> 3; @@ -513,18 +547,15 @@ static void set_tiles(struct avd_ctx *ctx, struct avd_vp9_run *run) for (int row = 0; row < num_tile_rows; row++) for (int col = 0; col < num_tile_cols; col++) { + /* Every tile lies within the payload. */ + ret = avd_vp9_next_tile(data, payload, + row == num_tile_rows - 1 && + col == num_tile_cols - 1, + &offset, &tile_size); + if (ret) + return ret; + ctx->job.num++; - if (row == num_tile_rows - 1 && - col == num_tile_cols - 1) { - tile_size = size; - } else { - tile_size = get_unaligned_be32(&data[offset]); - /* i have crashed my computer because of this */ - if (tile_size > size - 4) - return; - offset += 4; - size -= 4; - } push(AVD_OP_CODED_DATA | AVD_OP_CODED_DATA_ADDR( run->base.coded_in >> 32), @@ -547,9 +578,10 @@ static void set_tiles(struct avd_ctx *ctx, struct avd_vp9_run *run) "til_ac0_tile_dims"); offset += tile_size; - size -= tile_size; vp9_ctx->b->submit_num++; } + + return 0; } static void update_dec_buf_info(struct avd_decoded_buffer *buf, @@ -940,8 +972,10 @@ static int avd_vp9_run(struct avd_ctx *ctx) set_header(ctx, &run); vp9_ctx->b->submit_num = 0; - set_tiles(ctx, &run); + ret = set_tiles(ctx, &run); avd_run_postamble(ctx, &run.base); + if (ret) + goto out; if (avd_backend_is_qfw(ctx->dev)) { /* Forward updates need no hardware result; publish before next prepare. */ diff --git a/drivers/media/platform/apple/avd/avd.h b/drivers/media/platform/apple/avd/avd.h index fdf6be6cbe054..73af6ba6f3eaf 100644 --- a/drivers/media/platform/apple/avd/avd.h +++ b/drivers/media/platform/apple/avd/avd.h @@ -390,6 +390,8 @@ int avd_hevc_kunit_compute_tiles(const struct v4l2_ctrl_hevc_sps *sps, const struct v4l2_ctrl_hevc_pps *pps, u32 *tile_ids, size_t n); int avd_vp9_check_tiling(const struct v4l2_ctrl_vp9_frame *frame); +int avd_vp9_next_tile(const u8 *data, u32 payload, bool last, u32 *offset, + u32 *size); #endif extern const struct avd_coded_fmt_ops avd_h264_fmt_ops; From 22919336fc6f232797fb4e8cc5d324dc6251e8b2 Mon Sep 17 00:00:00 2001 From: Ryan Murray Date: Mon, 23 Nov 2026 08:11:39 +1100 Subject: [PATCH 08/26] media: apple: avd: av1: bound tile_row and tile_col of the tile entries set_tiles() looks up mi_row_starts[], mi_col_starts[], height_in_sbs_minus_1[] and width_in_sbs_minus_1[] of the frame's tile_info with the tile_row and tile_col fields of each tile-group entry. The control framework does not validate V4L2_CTRL_TYPE_AV1_TILE_GROUP_ENTRY, and avd_av1_check_tiles() only checked each tile's offset and size, so values past tile_rows/tile_cols indexed past the ends of those arrays. In a conformant stream TileRow < TileRows and TileCol < TileCols (AV1 5.11.1). Reject a frame whose tile entries are outside the frame's tile grid, and also check tile_rows and tile_cols against V4L2_AV1_MAX_TILE_ROWS/COLS in the same place so the helper does not depend on the core's frame validation for the array sizes. Make avd_av1_check_tiles() take the tile grid, the entries and the payload size directly so it can be exercised from KUnit, and add avd-av1-test.c covering the grid bounds, the entry count and the offset/size check. Signed-off-by: Ryan Murray --- drivers/media/platform/apple/avd/Makefile | 2 +- .../media/platform/apple/avd/avd-av1-test.c | 126 ++++++++++++++++++ drivers/media/platform/apple/avd/avd-av1.c | 44 ++++-- drivers/media/platform/apple/avd/avd.h | 4 + 4 files changed, 162 insertions(+), 14 deletions(-) create mode 100644 drivers/media/platform/apple/avd/avd-av1-test.c diff --git a/drivers/media/platform/apple/avd/Makefile b/drivers/media/platform/apple/avd/Makefile index be42b7f645da8..e7d7da9f671a5 100644 --- a/drivers/media/platform/apple/avd/Makefile +++ b/drivers/media/platform/apple/avd/Makefile @@ -5,5 +5,5 @@ apple-avd-y := avd-drv.o avd-v4l2.o avd-hw.o avd-pm.o avd-h264.o avd-vp9.o \ avd-backend-apple.o avd-backend-queue.o avd-backend-kopsia.o apple-avd-$(CONFIG_VIDEO_APPLE_AVD_KUNIT_TEST) += avd-h264-test.o \ avd-queue-test.o avd-pm-test.o avd-hevc-test.o avd-job-test.o \ - avd-vp9-test.o + avd-av1-test.o avd-vp9-test.o obj-$(CONFIG_VIDEO_APPLE_AVD) += apple-avd.o diff --git a/drivers/media/platform/apple/avd/avd-av1-test.c b/drivers/media/platform/apple/avd/avd-av1-test.c new file mode 100644 index 0000000000000..33ad22165ce79 --- /dev/null +++ b/drivers/media/platform/apple/avd/avd-av1-test.c @@ -0,0 +1,126 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * KUnit tests for the AV1 tile checks of the Apple video decoder. + */ + +#include + +#include "avd.h" + +#define AVD_TEST_PAYLOAD 4096 + +/* A rows x cols layout, entries in raster order, each tile 100 bytes. */ +static void avd_av1_test_layout(struct v4l2_ctrl_av1_tile_group_entry *e, + unsigned int rows, unsigned int cols) +{ + unsigned int r, c; + + for (r = 0; r < rows; r++) + for (c = 0; c < cols; c++, e++) { + e->tile_offset = (r * cols + c) * 100; + e->tile_size = 100; + e->tile_row = r; + e->tile_col = c; + } +} + +static void avd_av1_tiles_valid_test(struct kunit *test) +{ + struct v4l2_ctrl_av1_tile_group_entry e[6] = {}; + + avd_av1_test_layout(e, 2, 3); + KUNIT_EXPECT_EQ(test, avd_av1_check_tiles(NULL, 2, 3, e, 6, + AVD_TEST_PAYLOAD), 0); + /* Entries past the frame's tiles are not looked at. */ + e[5].tile_row = U32_MAX; + KUNIT_EXPECT_EQ(test, avd_av1_check_tiles(NULL, 1, 3, e, 6, + AVD_TEST_PAYLOAD), 0); + /* No tiles, no entries. */ + KUNIT_EXPECT_EQ(test, avd_av1_check_tiles(NULL, 0, 0, e, 0, + AVD_TEST_PAYLOAD), 0); +} + +static void avd_av1_tiles_count_test(struct kunit *test) +{ + struct v4l2_ctrl_av1_tile_group_entry e[6] = {}; + + avd_av1_test_layout(e, 2, 3); + KUNIT_EXPECT_EQ(test, avd_av1_check_tiles(NULL, 2, 3, e, 5, + AVD_TEST_PAYLOAD), -EINVAL); + KUNIT_EXPECT_EQ(test, avd_av1_check_tiles(NULL, V4L2_AV1_MAX_TILE_ROWS + 1, + 1, e, 6, AVD_TEST_PAYLOAD), + -EINVAL); + KUNIT_EXPECT_EQ(test, avd_av1_check_tiles(NULL, 1, + V4L2_AV1_MAX_TILE_COLS + 1, + e, 6, AVD_TEST_PAYLOAD), + -EINVAL); +} + +static void avd_av1_tiles_extent_test(struct kunit *test) +{ + struct v4l2_ctrl_av1_tile_group_entry e[6] = {}; + + avd_av1_test_layout(e, 2, 3); + /* The last tile ends exactly at the payload's end. */ + KUNIT_EXPECT_EQ(test, avd_av1_check_tiles(NULL, 2, 3, e, 6, 600), 0); + KUNIT_EXPECT_EQ(test, avd_av1_check_tiles(NULL, 2, 3, e, 6, 599), + -EINVAL); + e[2].tile_offset = U32_MAX; + KUNIT_EXPECT_EQ(test, avd_av1_check_tiles(NULL, 2, 3, e, 6, + AVD_TEST_PAYLOAD), -EINVAL); + e[2].tile_offset = 200; + e[2].tile_size = U32_MAX; + KUNIT_EXPECT_EQ(test, avd_av1_check_tiles(NULL, 2, 3, e, 6, + AVD_TEST_PAYLOAD), -EINVAL); +} + +struct avd_av1_rowcol_case { + const char *desc; + unsigned int idx; + u32 tile_row; + u32 tile_col; + int ret; +}; + +/* Entry idx of a 2x3 layout gets tile_row/tile_col; every other entry is valid. */ +static const struct avd_av1_rowcol_case avd_av1_rowcol_cases[] = { + { "last row and column", 5, 1, 2, 0 }, + { "row equals tile_rows", 5, 2, 2, -EINVAL }, + { "column equals tile_cols", 5, 1, 3, -EINVAL }, + { "first entry, row equals tile_rows", 0, 2, 0, -EINVAL }, + { "row past the tile_info arrays", 3, V4L2_AV1_MAX_TILE_ROWS + 1, 0, -EINVAL }, + { "column past the tile_info arrays", 3, 0, V4L2_AV1_MAX_TILE_COLS + 1, -EINVAL }, + { "huge row", 1, 0x3fffffff, 0, -EINVAL }, + { "huge column", 1, 0, 0x3fffffff, -EINVAL }, + { "row U32_MAX", 4, U32_MAX, 1, -EINVAL }, + { "column U32_MAX", 4, 1, U32_MAX, -EINVAL }, +}; + +KUNIT_ARRAY_PARAM_DESC(avd_av1_rowcol, avd_av1_rowcol_cases, desc); + +static void avd_av1_tiles_rowcol_test(struct kunit *test) +{ + const struct avd_av1_rowcol_case *c = test->param_value; + struct v4l2_ctrl_av1_tile_group_entry e[6] = {}; + + avd_av1_test_layout(e, 2, 3); + e[c->idx].tile_row = c->tile_row; + e[c->idx].tile_col = c->tile_col; + KUNIT_EXPECT_EQ(test, avd_av1_check_tiles(NULL, 2, 3, e, 6, + AVD_TEST_PAYLOAD), c->ret); +} + +static struct kunit_case avd_av1_test_cases[] = { + KUNIT_CASE(avd_av1_tiles_valid_test), + KUNIT_CASE(avd_av1_tiles_count_test), + KUNIT_CASE(avd_av1_tiles_extent_test), + KUNIT_CASE_PARAM(avd_av1_tiles_rowcol_test, avd_av1_rowcol_gen_params), + {} +}; + +static struct kunit_suite avd_av1_test_suite = { + .name = "apple-avd-av1", + .test_cases = avd_av1_test_cases, +}; + +kunit_test_suite(avd_av1_test_suite); diff --git a/drivers/media/platform/apple/avd/avd-av1.c b/drivers/media/platform/apple/avd/avd-av1.c index 8b3c2f34c542c..c39c63adb4f08 100644 --- a/drivers/media/platform/apple/avd/avd-av1.c +++ b/drivers/media/platform/apple/avd/avd-av1.c @@ -16,6 +16,7 @@ * Tomasz Figa */ +#include #include #include "avd.h" @@ -1396,34 +1397,48 @@ static void update_dec_buf_info(struct avd_decoded_buffer *buf, buf->av1.ref_frame_idx[i] = frame->ref_frame_idx[i]; } -/* The engine fetches each tile from these: bad values make it read wild addresses. */ -static int avd_av1_check_tiles(struct avd_ctx *ctx, struct avd_av1_run *run, - unsigned int entries) +/* + * The engine fetches each tile from these: bad values make it read wild + * addresses. set_tiles() also looks each entry's tile_row and tile_col up in + * the frame's tile_info arrays; in a conformant stream they are below + * TileRows and TileCols (AV1 5.11.1, tile_group_obu()), and TileRows and + * TileCols are at most MAX_TILE_ROWS and MAX_TILE_COLS (AV1 section 3). + */ +VISIBLE_IF_KUNIT int +avd_av1_check_tiles(struct device *dev, unsigned int tile_rows, + unsigned int tile_cols, + const struct v4l2_ctrl_av1_tile_group_entry *tile_group, + unsigned int entries, size_t payload) { - unsigned int tiles = run->frame->tile_info.tile_cols * - run->frame->tile_info.tile_rows; + unsigned int tiles = tile_cols * tile_rows; unsigned int t; - size_t payload; + if (tile_rows > V4L2_AV1_MAX_TILE_ROWS || + tile_cols > V4L2_AV1_MAX_TILE_COLS) + return -EINVAL; if (entries < tiles) { - dev_err_ratelimited(ctx->dev->dev, "AV1: %u tiles but %u tile entries\n", + dev_err_ratelimited(dev, "AV1: %u tiles but %u tile entries\n", tiles, entries); return -EINVAL; } - if (!tiles) - return 0; - payload = vb2_get_plane_payload(&run->base.bufs.src->vb2_buf, 0); for (t = 0; t < tiles; t++) { - const struct v4l2_ctrl_av1_tile_group_entry *e = &run->tile_group[t]; + const struct v4l2_ctrl_av1_tile_group_entry *e = &tile_group[t]; if ((u64)e->tile_offset + e->tile_size > payload) { - dev_err_ratelimited(ctx->dev->dev, "AV1: tile %u at %u+%u past %zu bytes\n", + dev_err_ratelimited(dev, "AV1: tile %u at %u+%u past %zu bytes\n", t, e->tile_offset, e->tile_size, payload); return -EINVAL; } + if (e->tile_row >= tile_rows || e->tile_col >= tile_cols) { + dev_err_ratelimited(dev, "AV1: tile %u at row %u col %u of %ux%u\n", + t, e->tile_row, e->tile_col, + tile_rows, tile_cols); + return -EINVAL; + } } return 0; } +EXPORT_SYMBOL_IF_KUNIT(avd_av1_check_tiles); /* * Only the AV1 Main profile at 4:2:0 (8 and 10 bits) decodes. A High @@ -1498,7 +1513,10 @@ static int avd_av1_run_preamble(struct avd_ctx *ctx, struct avd_av1_run *run) if (WARN_ON(!ctrl)) return -EINVAL; run->tile_group = ctrl->p_cur.p; - ret = avd_av1_check_tiles(ctx, run, ctrl->elems); + ret = avd_av1_check_tiles(ctx->dev->dev, run->frame->tile_info.tile_rows, + run->frame->tile_info.tile_cols, + run->tile_group, ctrl->elems, + vb2_get_plane_payload(&run->base.bufs.src->vb2_buf, 0)); if (ret) return ret; diff --git a/drivers/media/platform/apple/avd/avd.h b/drivers/media/platform/apple/avd/avd.h index 73af6ba6f3eaf..45469d512e3cd 100644 --- a/drivers/media/platform/apple/avd/avd.h +++ b/drivers/media/platform/apple/avd/avd.h @@ -389,6 +389,10 @@ int avd_hevc_check_slices(const struct v4l2_ctrl_hevc_sps *sps, int avd_hevc_kunit_compute_tiles(const struct v4l2_ctrl_hevc_sps *sps, const struct v4l2_ctrl_hevc_pps *pps, u32 *tile_ids, size_t n); +int avd_av1_check_tiles(struct device *dev, unsigned int tile_rows, + unsigned int tile_cols, + const struct v4l2_ctrl_av1_tile_group_entry *tile_group, + unsigned int entries, size_t payload); int avd_vp9_check_tiling(const struct v4l2_ctrl_vp9_frame *frame); int avd_vp9_next_tile(const u8 *data, u32 payload, bool last, u32 *offset, u32 *size); From 765f07af1a6d5ade49c6a010e65b7be1c91cc986 Mon Sep 17 00:00:00 2001 From: Ryan Murray Date: Mon, 23 Nov 2026 08:16:05 +1100 Subject: [PATCH 09/26] media: apple: avd: Keep TRY_FMT(CAPTURE) from changing the context avd_fill_decoded_pixfmt() stored the compressed reference layout of the format it was given in ctx->comp. TRY_FMT(CAPTURE) shares it with S_FMT, so trying a format replaced the layout of the negotiated one, also while CAPTURE buffers were allocated or streaming. The run path places the compressed area at ctx->comp.start_offset in each CAPTURE buffer and hands ctx->comp.offsets to the decoder, so the decoder then used a layout that did not match the buffers, and the check that lets a format control change on a busy queue compared against the tried format. Compute the layout into a caller provided struct avd_comp. TRY_FMT discards it; ctx->comp is only stored by avd_reset_decoded_fmt() and avd_s_capture_fmt(), which do not run with CAPTURE buffers allocated, so ctx->comp always describes ctx->decoded_fmt. Add KUnit tests that TRY_FMT(CAPTURE) leaves ctx->comp and the decoded format unchanged, busy or not, and that S_FMT(CAPTURE) stores the layout of the format it sets. Signed-off-by: Ryan Murray --- drivers/media/platform/apple/avd/Makefile | 2 +- .../media/platform/apple/avd/avd-v4l2-test.c | 150 ++++++++++++++++++ drivers/media/platform/apple/avd/avd-v4l2.c | 42 +++-- drivers/media/platform/apple/avd/avd.h | 2 + 4 files changed, 183 insertions(+), 13 deletions(-) create mode 100644 drivers/media/platform/apple/avd/avd-v4l2-test.c diff --git a/drivers/media/platform/apple/avd/Makefile b/drivers/media/platform/apple/avd/Makefile index e7d7da9f671a5..926ebb1d8a73a 100644 --- a/drivers/media/platform/apple/avd/Makefile +++ b/drivers/media/platform/apple/avd/Makefile @@ -5,5 +5,5 @@ apple-avd-y := avd-drv.o avd-v4l2.o avd-hw.o avd-pm.o avd-h264.o avd-vp9.o \ avd-backend-apple.o avd-backend-queue.o avd-backend-kopsia.o apple-avd-$(CONFIG_VIDEO_APPLE_AVD_KUNIT_TEST) += avd-h264-test.o \ avd-queue-test.o avd-pm-test.o avd-hevc-test.o avd-job-test.o \ - avd-av1-test.o avd-vp9-test.o + avd-av1-test.o avd-v4l2-test.o avd-vp9-test.o obj-$(CONFIG_VIDEO_APPLE_AVD) += apple-avd.o diff --git a/drivers/media/platform/apple/avd/avd-v4l2-test.c b/drivers/media/platform/apple/avd/avd-v4l2-test.c new file mode 100644 index 0000000000000..0c057d567b615 --- /dev/null +++ b/drivers/media/platform/apple/avd/avd-v4l2-test.c @@ -0,0 +1,150 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * KUnit tests for the format ioctls of the Apple video decoder. + */ + +#include +#include +#include + +#include "avd.h" + +static const struct avd_coded_fmt_ops avd_test_ops = {}; + +static const struct avd_decoded_fmt_desc avd_test_decoded_fmts[] = { + { .fourcc = V4L2_PIX_FMT_NV12, .image_fmt = AVD_IMG_FMT_420_8BIT }, +}; + +static const struct avd_coded_fmt_desc avd_test_coded_desc = { + .fourcc = V4L2_PIX_FMT_H264_SLICE, + .frmsize = { + .min_width = 64, .max_width = 4096, .step_width = 64, + .min_height = 64, .max_height = 4096, .step_height = 16, + }, + .ops = &avd_test_ops, + .num_decoded_fmts = ARRAY_SIZE(avd_test_decoded_fmts), + .decoded_fmts = avd_test_decoded_fmts, +}; + +struct avd_test_fmt { + struct avd_ctx *ctx; + struct file *file; + struct vb2_queue *cap_q; +}; + +/* A 64x64 context with its default CAPTURE format, as after open(). */ +static void avd_test_fmt_init(struct kunit *test, struct avd_test_fmt *t) +{ + struct v4l2_m2m_ctx *m2m_ctx; + + t->ctx = kunit_kzalloc(test, sizeof(*t->ctx), GFP_KERNEL); + KUNIT_ASSERT_NOT_NULL(test, t->ctx); + m2m_ctx = kunit_kzalloc(test, sizeof(*m2m_ctx), GFP_KERNEL); + KUNIT_ASSERT_NOT_NULL(test, m2m_ctx); + t->file = kunit_kzalloc(test, sizeof(*t->file), GFP_KERNEL); + KUNIT_ASSERT_NOT_NULL(test, t->file); + + t->ctx->fh.m2m_ctx = m2m_ctx; + t->file->private_data = &t->ctx->fh; + t->cap_q = &m2m_ctx->cap_q_ctx.q; + + t->ctx->coded_fmt_desc = &avd_test_coded_desc; + t->ctx->coded_fmt.fmt.pix_mp.width = 64; + t->ctx->coded_fmt.fmt.pix_mp.height = 64; + t->ctx->image_fmt = AVD_IMG_FMT_420_8BIT; + avd_reset_decoded_fmt(t->ctx); +} + +static void avd_test_capture_fmt(struct v4l2_format *f, u32 width, u32 height) +{ + memset(f, 0, sizeof(*f)); + f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE; + f->fmt.pix_mp.pixelformat = V4L2_PIX_FMT_NV12; + f->fmt.pix_mp.width = width; + f->fmt.pix_mp.height = height; +} + +static void avd_test_expect_comp(struct kunit *test, + const struct avd_comp *got, + const struct avd_comp *want) +{ + KUNIT_EXPECT_EQ(test, got->size, want->size); + KUNIT_EXPECT_EQ(test, got->start_offset, want->start_offset); + KUNIT_EXPECT_MEMEQ(test, got->offsets, want->offsets, + sizeof(want->offsets)); +} + +/* TRY_FMT(CAPTURE) must leave the context's buffer layout alone. */ +static void avd_try_capture_fmt_test(struct kunit *test) +{ + struct avd_test_fmt t; + struct v4l2_format f, decoded; + struct avd_comp comp; + + avd_test_fmt_init(test, &t); + comp = t.ctx->comp; + decoded = t.ctx->decoded_fmt; + KUNIT_ASSERT_NE(test, comp.size, 0); + + /* CAPTURE buffers allocated, as when streaming. */ + t.cap_q->is_busy = 1; + avd_test_capture_fmt(&f, 4096, 4096); + KUNIT_EXPECT_EQ(test, avd_try_capture_fmt(t.file, NULL, &f), 0); + KUNIT_EXPECT_EQ(test, f.fmt.pix_mp.width, 4096); + KUNIT_EXPECT_GT(test, f.fmt.pix_mp.plane_fmt[0].sizeimage, + decoded.fmt.pix_mp.plane_fmt[0].sizeimage); + avd_test_expect_comp(test, &t.ctx->comp, &comp); + KUNIT_EXPECT_MEMEQ(test, &t.ctx->decoded_fmt, &decoded, sizeof(decoded)); + + /* S_FMT is refused on the busy queue and changes nothing either. */ + avd_test_capture_fmt(&f, 4096, 4096); + KUNIT_EXPECT_EQ(test, avd_s_capture_fmt(t.file, NULL, &f), -EBUSY); + avd_test_expect_comp(test, &t.ctx->comp, &comp); + + /* The same holds with no buffers. */ + t.cap_q->is_busy = 0; + avd_test_capture_fmt(&f, 1920, 1088); + KUNIT_EXPECT_EQ(test, avd_try_capture_fmt(t.file, NULL, &f), 0); + avd_test_expect_comp(test, &t.ctx->comp, &comp); + KUNIT_EXPECT_MEMEQ(test, &t.ctx->decoded_fmt, &decoded, sizeof(decoded)); +} + +/* S_FMT(CAPTURE) lays out the compressed area for the format it sets. */ +static void avd_s_capture_fmt_test(struct kunit *test) +{ + struct avd_test_fmt t; + struct v4l2_format f; + struct avd_comp want = {}; + + avd_test_fmt_init(test, &t); + + avd_test_capture_fmt(&f, 1920, 1088); + KUNIT_ASSERT_EQ(test, avd_s_capture_fmt(t.file, NULL, &f), 0); + + want.start_offset = 1920 * 1088 * 3 / 2; + fill_comp(&want, AVD_IMG_FMT_420_8BIT, 1920, 1088); + avd_test_expect_comp(test, &t.ctx->comp, &want); + KUNIT_EXPECT_EQ(test, t.ctx->decoded_fmt.fmt.pix_mp.width, 1920); + KUNIT_EXPECT_EQ(test, t.ctx->decoded_fmt.fmt.pix_mp.height, 1088); + KUNIT_EXPECT_EQ(test, + t.ctx->decoded_fmt.fmt.pix_mp.plane_fmt[0].sizeimage, + want.start_offset + want.size); + + /* A later TRY_FMT reports its own layout but stores nothing. */ + avd_test_capture_fmt(&f, 4096, 2176); + KUNIT_EXPECT_EQ(test, avd_try_capture_fmt(t.file, NULL, &f), 0); + avd_test_expect_comp(test, &t.ctx->comp, &want); +} + +static struct kunit_case avd_v4l2_test_cases[] = { + KUNIT_CASE(avd_try_capture_fmt_test), + KUNIT_CASE(avd_s_capture_fmt_test), + {} +}; + +static struct kunit_suite avd_v4l2_test_suite = { + .name = "apple-avd-v4l2", + .test_cases = avd_v4l2_test_cases, +}; + +kunit_test_suite(avd_v4l2_test_suite); diff --git a/drivers/media/platform/apple/avd/avd-v4l2.c b/drivers/media/platform/apple/avd/avd-v4l2.c index 2a4903ee76aaf..5aabdefffc38d 100644 --- a/drivers/media/platform/apple/avd/avd-v4l2.c +++ b/drivers/media/platform/apple/avd/avd-v4l2.c @@ -11,6 +11,7 @@ * Copyright (C) 2011 Samsung Electronics Co., Ltd. */ +#include #include #include @@ -103,15 +104,21 @@ static bool avd_is_valid_fmt(struct avd_ctx *ctx, u32 fourcc, return false; } +/* + * Lay out a decoded buffer: the picture, then the compressed reference area + * described by @comp, then any codec specific tail. Only @pix_mp and @comp + * are written, so TRY_FMT can use it on a context that is streaming. + */ static void avd_fill_decoded_pixfmt(struct avd_ctx *ctx, - struct v4l2_pix_format_mplane *pix_mp) + struct v4l2_pix_format_mplane *pix_mp, + struct avd_comp *comp) { v4l2_fill_pixfmt_mp(pix_mp, pix_mp->pixelformat, pix_mp->width, pix_mp->height); - ctx->comp.start_offset = pix_mp->plane_fmt[0].sizeimage; - fill_comp(&ctx->comp, ctx->image_fmt, pix_mp->width, pix_mp->height); - pix_mp->plane_fmt[0].sizeimage += ctx->comp.size; + comp->start_offset = pix_mp->plane_fmt[0].sizeimage; + fill_comp(comp, ctx->image_fmt, pix_mp->width, pix_mp->height); + pix_mp->plane_fmt[0].sizeimage += comp->size; if (ctx->coded_fmt_desc->ops->adjust_decoded_fmt) ctx->coded_fmt_desc->ops->adjust_decoded_fmt(ctx, pix_mp); } @@ -138,7 +145,7 @@ void avd_reset_decoded_fmt(struct avd_ctx *ctx) f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE; f->fmt.pix_mp.width = ctx->coded_fmt.fmt.pix_mp.width; f->fmt.pix_mp.height = ctx->coded_fmt.fmt.pix_mp.height; - avd_fill_decoded_pixfmt(ctx, &f->fmt.pix_mp); + avd_fill_decoded_pixfmt(ctx, &f->fmt.pix_mp, &ctx->comp); } static int avd_try_ctrl(struct v4l2_ctrl *ctrl) @@ -566,11 +573,10 @@ static int avd_querycap(struct file *file, void *priv, return 0; } -static int avd_try_capture_fmt(struct file *file, void *priv, - struct v4l2_format *f) +static int avd_try_decoded_fmt(struct avd_ctx *ctx, struct v4l2_format *f, + struct avd_comp *comp) { struct v4l2_pix_format_mplane *pix_mp = &f->fmt.pix_mp; - struct avd_ctx *ctx = file_to_ctx(file); const struct avd_coded_fmt_desc *coded_desc; /* @@ -594,12 +600,21 @@ static int avd_try_capture_fmt(struct file *file, void *priv, v4l2_apply_frmsize_constraints(&pix_mp->width, &pix_mp->height, &coded_desc->frmsize); - avd_fill_decoded_pixfmt(ctx, pix_mp); + avd_fill_decoded_pixfmt(ctx, pix_mp, comp); pix_mp->field = V4L2_FIELD_NONE; return 0; } +VISIBLE_IF_KUNIT int avd_try_capture_fmt(struct file *file, void *priv, + struct v4l2_format *f) +{ + struct avd_comp comp; + + return avd_try_decoded_fmt(file_to_ctx(file), f, &comp); +} +EXPORT_SYMBOL_IF_KUNIT(avd_try_capture_fmt); + static int avd_try_output_fmt(struct file *file, void *priv, struct v4l2_format *f) { @@ -627,10 +642,11 @@ static int avd_try_output_fmt(struct file *file, void *priv, return 0; } -static int avd_s_capture_fmt(struct file *file, void *priv, - struct v4l2_format *f) +VISIBLE_IF_KUNIT int avd_s_capture_fmt(struct file *file, void *priv, + struct v4l2_format *f) { struct avd_ctx *ctx = file_to_ctx(file); + struct avd_comp comp; struct vb2_queue *vq; int ret; @@ -640,13 +656,15 @@ static int avd_s_capture_fmt(struct file *file, void *priv, if (vb2_is_busy(vq)) return -EBUSY; - ret = avd_try_capture_fmt(file, priv, f); + ret = avd_try_decoded_fmt(ctx, f, &comp); if (ret) return ret; ctx->decoded_fmt = *f; + ctx->comp = comp; return 0; } +EXPORT_SYMBOL_IF_KUNIT(avd_s_capture_fmt); static int avd_s_output_fmt(struct file *file, void *priv, struct v4l2_format *f) diff --git a/drivers/media/platform/apple/avd/avd.h b/drivers/media/platform/apple/avd/avd.h index 45469d512e3cd..45e2da9edbe36 100644 --- a/drivers/media/platform/apple/avd/avd.h +++ b/drivers/media/platform/apple/avd/avd.h @@ -393,6 +393,8 @@ int avd_av1_check_tiles(struct device *dev, unsigned int tile_rows, unsigned int tile_cols, const struct v4l2_ctrl_av1_tile_group_entry *tile_group, unsigned int entries, size_t payload); +int avd_try_capture_fmt(struct file *file, void *priv, struct v4l2_format *f); +int avd_s_capture_fmt(struct file *file, void *priv, struct v4l2_format *f); int avd_vp9_check_tiling(const struct v4l2_ctrl_vp9_frame *frame); int avd_vp9_next_tile(const u8 *data, u32 payload, bool last, u32 *offset, u32 *size); From 88bd419f6593f58c054df8d7575095872aa5a7b6 Mon Sep 17 00:00:00 2001 From: Ryan Murray Date: Mon, 23 Nov 2026 08:16:25 +1100 Subject: [PATCH 10/26] media: apple: avd: h264: Check the SPS picture size against the format The picture size the hardware is programmed with comes from the SPS (pic_width_in_mbs_minus1, pic_height_in_map_units_minus1), while the CAPTURE buffers, the compressed reference and motion vector areas in them, and the per-row scratch are sized from the OUTPUT format. avd_h264_validate_sps() did not compare the two, so a picture larger than the negotiated format was decoded into buffers too small for it. Refuse an SPS whose picture (H.264 7.4.2.1.1: PicWidthInMbs by FrameHeightInMbs macroblocks) is larger than the OUTPUT format, as the HEVC SPS check does. S_FMT(OUTPUT) rounds the format up to whole macroblocks, so a 1920x1080 stream with its 1088 line coded picture still fits. The check applies when the control is set, at STREAMON and again when a request's controls are applied at run time. Add a KUnit test for pictures that fit, including 1080p with the OUTPUT format set to 1920x1080 or 1920x1088, and for pictures that are one macroblock or more too large. Signed-off-by: Ryan Murray --- .../media/platform/apple/avd/avd-h264-test.c | 54 +++++++++++++++++++ drivers/media/platform/apple/avd/avd-h264.c | 19 ++++++- drivers/media/platform/apple/avd/avd.h | 2 + 3 files changed, 73 insertions(+), 2 deletions(-) diff --git a/drivers/media/platform/apple/avd/avd-h264-test.c b/drivers/media/platform/apple/avd/avd-h264-test.c index 54d2114f45e98..bdeb90594b1d5 100644 --- a/drivers/media/platform/apple/avd/avd-h264-test.c +++ b/drivers/media/platform/apple/avd/avd-h264-test.c @@ -5,6 +5,7 @@ #include #include +#include #include "avd.h" @@ -107,6 +108,58 @@ static void avd_slice_fits_test(struct kunit *test) c->fits); } +/* The H.264 OUTPUT frame size limits in avd-v4l2.c */ +static const struct v4l2_frmsize_stepwise avd_test_h264_frmsize = { + .min_width = 64, .max_width = 4096, .step_width = 64, + .min_height = 64, .max_height = 4096, .step_height = 16, +}; + +struct avd_sps_size_case { + const char *desc; + /* OUTPUT format as the client asks for it */ + u32 width; + u32 height; + u16 width_mbs; + u16 height_map_units; + int ret; +}; + +static const struct avd_sps_size_case avd_sps_size_cases[] = { + { "1080p, OUTPUT 1920x1080", 1920, 1080, 120, 68, 0 }, + { "1080p, OUTPUT 1920x1088", 1920, 1088, 120, 68, 0 }, + { "720p", 1280, 720, 80, 45, 0 }, + { "720p, one row over", 1280, 720, 80, 46, -EINVAL }, + { "720p, one column over", 1280, 720, 81, 45, -EINVAL }, + { "picture smaller than the format", 1920, 1088, 40, 30, 0 }, + { "64x64, matching picture", 64, 64, 4, 4, 0 }, + { "64x64, 4096x4096 picture", 64, 64, 256, 256, -EINVAL }, + { "64x64, largest SPS size", 64, 64, U16_MAX, U16_MAX, -EINVAL }, +}; + +KUNIT_ARRAY_PARAM_DESC(avd_sps_size, avd_sps_size_cases, desc); + +static void avd_sps_size_test(struct kunit *test) +{ + const struct avd_sps_size_case *c = test->param_value; + struct v4l2_ctrl_h264_sps sps = { + .chroma_format_idc = 1, + .pic_width_in_mbs_minus1 = c->width_mbs - 1, + .pic_height_in_map_units_minus1 = c->height_map_units - 1, + .flags = V4L2_H264_SPS_FLAG_FRAME_MBS_ONLY, + }; + struct avd_ctx *ctx; + u32 w = c->width, h = c->height; + + ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL); + KUNIT_ASSERT_NOT_NULL(test, ctx); + /* As S_FMT(OUTPUT) does */ + v4l2_apply_frmsize_constraints(&w, &h, &avd_test_h264_frmsize); + ctx->coded_fmt.fmt.pix_mp.width = w; + ctx->coded_fmt.fmt.pix_mp.height = h; + + KUNIT_EXPECT_EQ(test, avd_h264_validate_sps(ctx, &sps), c->ret); +} + static void avd_scaling_flat_test(struct kunit *test) { struct v4l2_ctrl_h264_pps pps = {}; @@ -134,6 +187,7 @@ static struct kunit_case avd_h264_test_cases[] = { KUNIT_CASE(avd_scaling_flat_test), KUNIT_CASE_PARAM(avd_slice_data_test, avd_slice_data_gen_params), KUNIT_CASE_PARAM(avd_slice_fits_test, avd_slice_fits_gen_params), + KUNIT_CASE_PARAM(avd_sps_size_test, avd_sps_size_gen_params), {} }; diff --git a/drivers/media/platform/apple/avd/avd-h264.c b/drivers/media/platform/apple/avd/avd-h264.c index 6d2b4ff0643c3..66592f75dacb4 100644 --- a/drivers/media/platform/apple/avd/avd-h264.c +++ b/drivers/media/platform/apple/avd/avd-h264.c @@ -672,9 +672,23 @@ static int avd_h264_validate_slice(struct avd_ctx *ctx, return 0; } -static int avd_h264_validate_sps(struct avd_ctx *ctx, - const struct v4l2_ctrl_h264_sps *sps) +/* + * H.264 7.4.2.1.1: the picture is PicWidthInMbs macroblocks wide and + * FrameHeightInMbs = (2 - frame_mbs_only_flag) * PicHeightInMapUnits high. + * The decoded buffers and the per-row scratch are sized from the OUTPUT + * format, which S_FMT rounds up to whole macroblocks, so a picture must fit + * in it. + */ +VISIBLE_IF_KUNIT int avd_h264_validate_sps(struct avd_ctx *ctx, + const struct v4l2_ctrl_h264_sps *sps) { + u32 width = (sps->pic_width_in_mbs_minus1 + 1) * 16; + u32 height = (sps->pic_height_in_map_units_minus1 + 1) * 16 * + (sps->flags & V4L2_H264_SPS_FLAG_FRAME_MBS_ONLY ? 1 : 2); + + if (width > ctx->coded_fmt.fmt.pix_mp.width || + height > ctx->coded_fmt.fmt.pix_mp.height) + return -EINVAL; if (sps->chroma_format_idc > 2) /* Only 4:0:0, 4:2:0 and 4:2:2 are supported */ return -EINVAL; @@ -697,6 +711,7 @@ static int avd_h264_validate_sps(struct avd_ctx *ctx, return 0; } +EXPORT_SYMBOL_IF_KUNIT(avd_h264_validate_sps); /* Free the slices held for one bank's picture. */ static void avd_h264_drop_bank_slices(struct avd_ctx *ctx, struct avd_bank *bank) diff --git a/drivers/media/platform/apple/avd/avd.h b/drivers/media/platform/apple/avd/avd.h index 45e2da9edbe36..bb6e6c680f99c 100644 --- a/drivers/media/platform/apple/avd/avd.h +++ b/drivers/media/platform/apple/avd/avd.h @@ -393,6 +393,8 @@ int avd_av1_check_tiles(struct device *dev, unsigned int tile_rows, unsigned int tile_cols, const struct v4l2_ctrl_av1_tile_group_entry *tile_group, unsigned int entries, size_t payload); +int avd_h264_validate_sps(struct avd_ctx *ctx, + const struct v4l2_ctrl_h264_sps *sps); int avd_try_capture_fmt(struct file *file, void *priv, struct v4l2_format *f); int avd_s_capture_fmt(struct file *file, void *priv, struct v4l2_format *f); int avd_vp9_check_tiling(const struct v4l2_ctrl_vp9_frame *frame); From 74837459057b4305a5cf0da5c68cee1a9f9a3453 Mon Sep 17 00:00:00 2001 From: Ryan Murray Date: Mon, 23 Nov 2026 08:14:34 +1100 Subject: [PATCH 11/26] media: apple: avd: vp9: refuse frames larger than the coded format avd_vp9_alloc_bufs() sizes the per-row and per-column scratch (color, ip_above, lf_above, seg, lf_left, az_left, above_info, ...) at STREAMON from the OUTPUT format. validate_dec_params() ties the frame size only to the CAPTURE format, which avd_try_capture_fmt() lets be larger than the coded size and which can be changed while OUTPUT streams, so a frame can be larger than the scratch was sized for. Also require the frame to fit the coded format, which cannot change while the scratch exists, as HEVC does for its SPS. Clients set the coded format to the stream size, and the CAPTURE format is at least the coded size, so conformant streams are not affected. Add a KUnit test for the size check. Signed-off-by: Ryan Murray --- .../media/platform/apple/avd/avd-vp9-test.c | 30 +++++++++++++++++++ drivers/media/platform/apple/avd/avd-vp9.c | 17 +++++++++++ drivers/media/platform/apple/avd/avd.h | 2 ++ 3 files changed, 49 insertions(+) diff --git a/drivers/media/platform/apple/avd/avd-vp9-test.c b/drivers/media/platform/apple/avd/avd-vp9-test.c index f0b501dffde17..4e0bcd09ed31c 100644 --- a/drivers/media/platform/apple/avd/avd-vp9-test.c +++ b/drivers/media/platform/apple/avd/avd-vp9-test.c @@ -125,7 +125,37 @@ static void avd_vp9_tiles_test(struct kunit *test) KUNIT_EXPECT_EQ(test, ret, c->ret); } +/* The frame size against a 1920x1088 coded format. */ +static void avd_vp9_frame_fits_test(struct kunit *test) +{ + struct v4l2_ctrl_vp9_frame frame = { + .frame_width_minus_1 = 1919, + .frame_height_minus_1 = 1079, + }; + + KUNIT_EXPECT_TRUE(test, avd_vp9_frame_fits(&frame, 1920, 1088)); + frame.frame_height_minus_1 = 1087; + KUNIT_EXPECT_TRUE(test, avd_vp9_frame_fits(&frame, 1920, 1088)); + frame.frame_width_minus_1 = 959; + frame.frame_height_minus_1 = 539; + KUNIT_EXPECT_TRUE(test, avd_vp9_frame_fits(&frame, 1920, 1088)); + + frame.frame_width_minus_1 = 1920; + frame.frame_height_minus_1 = 1079; + KUNIT_EXPECT_FALSE(test, avd_vp9_frame_fits(&frame, 1920, 1088)); + frame.frame_width_minus_1 = 1919; + frame.frame_height_minus_1 = 1088; + KUNIT_EXPECT_FALSE(test, avd_vp9_frame_fits(&frame, 1920, 1088)); + frame.frame_width_minus_1 = 4095; + frame.frame_height_minus_1 = 2175; + KUNIT_EXPECT_FALSE(test, avd_vp9_frame_fits(&frame, 64, 64)); + frame.frame_width_minus_1 = 0xffff; + frame.frame_height_minus_1 = 0xffff; + KUNIT_EXPECT_FALSE(test, avd_vp9_frame_fits(&frame, 4096, 4096)); +} + static struct kunit_case avd_vp9_test_cases[] = { + KUNIT_CASE(avd_vp9_frame_fits_test), KUNIT_CASE_PARAM(avd_vp9_tiling_test, avd_vp9_tiling_gen_params), KUNIT_CASE_PARAM(avd_vp9_tiles_test, avd_vp9_tiles_gen_params), {} diff --git a/drivers/media/platform/apple/avd/avd-vp9.c b/drivers/media/platform/apple/avd/avd-vp9.c index e72aeb8dc2c2f..a0de2f7612f48 100644 --- a/drivers/media/platform/apple/avd/avd-vp9.c +++ b/drivers/media/platform/apple/avd/avd-vp9.c @@ -698,6 +698,20 @@ static void init_probs(struct avd_ctx *ctx, const struct avd_vp9_run *run) sizeof(avd_probs->seg.tree_probs)); } +/* + * The row and column scratch is sized at STREAMON from the OUTPUT (coded) + * format, which cannot change while it streams. The CAPTURE format can be + * larger and can change meanwhile, so the frame must fit the coded size. + */ +VISIBLE_IF_KUNIT bool +avd_vp9_frame_fits(const struct v4l2_ctrl_vp9_frame *frame, u32 width, + u32 height) +{ + return frame->frame_width_minus_1 + 1 <= width && + frame->frame_height_minus_1 + 1 <= height; +} +EXPORT_SYMBOL_IF_KUNIT(avd_vp9_frame_fits); + static int validate_dec_params(struct avd_ctx *ctx, const struct v4l2_ctrl_vp9_frame *dec_params) { @@ -716,6 +730,9 @@ static int validate_dec_params(struct avd_ctx *ctx, dec_params->frame_height_minus_1 + 1 < 64)) return -EINVAL; + if (!avd_vp9_frame_fits(dec_params, fmt_width(ctx), fmt_height(ctx))) + return -EINVAL; + aligned_width = max_t(unsigned int, round_up(dec_params->frame_width_minus_1 + 1, 64), 64); diff --git a/drivers/media/platform/apple/avd/avd.h b/drivers/media/platform/apple/avd/avd.h index bb6e6c680f99c..e36f0d60c2d86 100644 --- a/drivers/media/platform/apple/avd/avd.h +++ b/drivers/media/platform/apple/avd/avd.h @@ -400,6 +400,8 @@ int avd_s_capture_fmt(struct file *file, void *priv, struct v4l2_format *f); int avd_vp9_check_tiling(const struct v4l2_ctrl_vp9_frame *frame); int avd_vp9_next_tile(const u8 *data, u32 payload, bool last, u32 *offset, u32 *size); +bool avd_vp9_frame_fits(const struct v4l2_ctrl_vp9_frame *frame, u32 width, + u32 height); #endif extern const struct avd_coded_fmt_ops avd_h264_fmt_ops; From 83452e5df2aaa29adf0ae85383dc3a72071537ab Mon Sep 17 00:00:00 2001 From: Ryan Murray Date: Mon, 23 Nov 2026 08:19:40 +1100 Subject: [PATCH 12/26] media: apple: avd: check the picture format against the buffers at run time The CAPTURE buffers, their compressed reference area and the per-context scratch are sized for ctx->image_fmt, but each codec programs the bit depth and chroma format from the current value of its format control (the H.264 or HEVC SPS, the VP9 frame, the AV1 sequence). The two were only compared in s_ctrl, which the control framework skips for an unchanged value and which ignores a control of a codec that is not selected, while an OUTPUT format change resets ctx->image_fmt and keeps the control. A job could then program, for example, a 10-bit picture into buffers sized for 8 bits. When a job is built, derive the picture format from the control value the job uses and refuse the job unless it is the one the buffers were sized for (ANY sizes them as 8-bit 4:2:0) and the negotiated CAPTURE format fits it. The format each codec derives is unchanged; get_image_fmt now shares it. Add KUnit cases for the check. Signed-off-by: Ryan Murray --- drivers/media/platform/apple/avd/Makefile | 2 +- drivers/media/platform/apple/avd/avd-av1.c | 21 +-- .../media/platform/apple/avd/avd-fmt-test.c | 128 ++++++++++++++++++ drivers/media/platform/apple/avd/avd-h264.c | 37 +++-- drivers/media/platform/apple/avd/avd-hevc.c | 21 ++- drivers/media/platform/apple/avd/avd-v4l2.c | 26 ++++ drivers/media/platform/apple/avd/avd-vp9.c | 20 +-- drivers/media/platform/apple/avd/avd.h | 1 + 8 files changed, 216 insertions(+), 40 deletions(-) create mode 100644 drivers/media/platform/apple/avd/avd-fmt-test.c diff --git a/drivers/media/platform/apple/avd/Makefile b/drivers/media/platform/apple/avd/Makefile index 926ebb1d8a73a..f4e0864a52b0e 100644 --- a/drivers/media/platform/apple/avd/Makefile +++ b/drivers/media/platform/apple/avd/Makefile @@ -5,5 +5,5 @@ apple-avd-y := avd-drv.o avd-v4l2.o avd-hw.o avd-pm.o avd-h264.o avd-vp9.o \ avd-backend-apple.o avd-backend-queue.o avd-backend-kopsia.o apple-avd-$(CONFIG_VIDEO_APPLE_AVD_KUNIT_TEST) += avd-h264-test.o \ avd-queue-test.o avd-pm-test.o avd-hevc-test.o avd-job-test.o \ - avd-av1-test.o avd-v4l2-test.o avd-vp9-test.o + avd-fmt-test.o avd-av1-test.o avd-v4l2-test.o avd-vp9-test.o obj-$(CONFIG_VIDEO_APPLE_AVD) += apple-avd.o diff --git a/drivers/media/platform/apple/avd/avd-av1.c b/drivers/media/platform/apple/avd/avd-av1.c index c39c63adb4f08..851eebce2fee9 100644 --- a/drivers/media/platform/apple/avd/avd-av1.c +++ b/drivers/media/platform/apple/avd/avd-av1.c @@ -1448,6 +1448,14 @@ EXPORT_SYMBOL_IF_KUNIT(avd_av1_check_tiles); * when the sequence is set and again when a frame runs, so it never * reaches the decoder. */ +static enum avd_image_fmt +avd_av1_seq_image_fmt(const struct v4l2_ctrl_av1_sequence *seq) +{ + /* avd_av1_validate_sequence() admits only Main profile 4:2:0. */ + return seq->bit_depth == 8 ? AVD_IMG_FMT_420_8BIT : + AVD_IMG_FMT_420_10BIT; +} + static int avd_av1_validate_sequence(struct avd_ctx *ctx, const struct v4l2_ctrl_av1_sequence *seq) { @@ -1482,6 +1490,9 @@ static int avd_av1_run_preamble(struct avd_ctx *ctx, struct avd_av1_run *run) /* The sequence may have been set, unchecked, while another codec was selected. */ ret = avd_av1_validate_sequence(ctx, run->seq); + if (!ret) + ret = avd_check_run_image_fmt(ctx, + avd_av1_seq_image_fmt(run->seq)); if (ret) return ret; @@ -1899,18 +1910,10 @@ static int avd_av1_start(struct avd_ctx *ctx) static enum avd_image_fmt avd_av1_get_image_fmt(struct avd_ctx *ctx, struct v4l2_ctrl *ctrl) { -#define BIT_DEPTH(chroma) \ - (seq->bit_depth == 8 ? AVD_IMG_FMT_##chroma##_8BIT : \ - AVD_IMG_FMT_##chroma##_10BIT) - - const struct v4l2_ctrl_av1_sequence *seq = ctrl->p_new.p_av1_sequence; - if (ctrl->id != V4L2_CID_STATELESS_AV1_SEQUENCE) return AVD_IMG_FMT_ANY; - /* avd_av1_validate_sequence() admits only Main profile 4:2:0. */ - return BIT_DEPTH(420); -#undef BIT_DEPTH + return avd_av1_seq_image_fmt(ctrl->p_new.p_av1_sequence); } static unsigned int avd_av1_pp_kicks(struct avd_ctx *ctx) diff --git a/drivers/media/platform/apple/avd/avd-fmt-test.c b/drivers/media/platform/apple/avd/avd-fmt-test.c new file mode 100644 index 0000000000000..ca6ec6e4a8608 --- /dev/null +++ b/drivers/media/platform/apple/avd/avd-fmt-test.c @@ -0,0 +1,128 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * KUnit tests for the run-time picture format check of the Apple video + * decoder. + */ + +#include +#include +#include + +#include "avd.h" + +static const struct avd_decoded_fmt_desc avd_fmt_test_decoded[] = { + { .fourcc = V4L2_PIX_FMT_NV12, .image_fmt = AVD_IMG_FMT_420_8BIT }, + { .fourcc = V4L2_PIX_FMT_P010, .image_fmt = AVD_IMG_FMT_420_10BIT }, + { .fourcc = V4L2_PIX_FMT_NV16, .image_fmt = AVD_IMG_FMT_422_8BIT }, +}; + +static const struct avd_coded_fmt_desc avd_fmt_test_desc = { + .num_decoded_fmts = ARRAY_SIZE(avd_fmt_test_decoded), + .decoded_fmts = avd_fmt_test_decoded, +}; + +/* A context whose CAPTURE format was negotiated for @sized as @fourcc. */ +static struct avd_ctx *avd_fmt_test_ctx(struct kunit *test, + enum avd_image_fmt sized, u32 fourcc) +{ + struct avd_ctx *ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL); + struct avd_dev *avd = kunit_kzalloc(test, sizeof(*avd), GFP_KERNEL); + struct v4l2_pix_format_mplane *pix; + + KUNIT_ASSERT_NOT_NULL(test, ctx); + KUNIT_ASSERT_NOT_NULL(test, avd); + avd->dev = kunit_device_register(test, "avd-fmt-test"); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, avd->dev); + ctx->dev = avd; + ctx->coded_fmt_desc = &avd_fmt_test_desc; + ctx->image_fmt = sized; + pix = &ctx->decoded_fmt.fmt.pix_mp; + pix->pixelformat = fourcc; + pix->width = 64; + pix->height = 64; + fill_comp(&ctx->comp, sized, pix->width, pix->height); + return ctx; +} + +static void avd_fmt_any_test(struct kunit *test) +{ + struct avd_ctx *ctx = avd_fmt_test_ctx(test, AVD_IMG_FMT_ANY, + V4L2_PIX_FMT_NV12); + + /* Buffers sized for "any" are 8-bit 4:2:0. */ + KUNIT_EXPECT_EQ(test, avd_check_run_image_fmt(ctx, AVD_IMG_FMT_ANY), 0); + KUNIT_EXPECT_EQ(test, + avd_check_run_image_fmt(ctx, AVD_IMG_FMT_420_8BIT), 0); + KUNIT_EXPECT_EQ(test, + avd_check_run_image_fmt(ctx, AVD_IMG_FMT_420_10BIT), + -EINVAL); + KUNIT_EXPECT_EQ(test, + avd_check_run_image_fmt(ctx, AVD_IMG_FMT_422_8BIT), + -EINVAL); +} + +/* "Any" with a 10-bit CAPTURE fourcc still has 8-bit sized storage. */ +static void avd_fmt_any_p010_test(struct kunit *test) +{ + struct avd_ctx *ctx = avd_fmt_test_ctx(test, AVD_IMG_FMT_ANY, + V4L2_PIX_FMT_P010); + + KUNIT_EXPECT_EQ(test, + avd_check_run_image_fmt(ctx, AVD_IMG_FMT_420_10BIT), + -EINVAL); +} + +static void avd_fmt_10bit_test(struct kunit *test) +{ + struct avd_ctx *ctx = avd_fmt_test_ctx(test, AVD_IMG_FMT_420_10BIT, + V4L2_PIX_FMT_P010); + + KUNIT_EXPECT_EQ(test, + avd_check_run_image_fmt(ctx, AVD_IMG_FMT_420_10BIT), 0); + KUNIT_EXPECT_EQ(test, + avd_check_run_image_fmt(ctx, AVD_IMG_FMT_420_8BIT), + -EINVAL); +} + +static void avd_fmt_422_test(struct kunit *test) +{ + struct avd_ctx *ctx = avd_fmt_test_ctx(test, AVD_IMG_FMT_422_8BIT, + V4L2_PIX_FMT_NV16); + + KUNIT_EXPECT_EQ(test, + avd_check_run_image_fmt(ctx, AVD_IMG_FMT_422_8BIT), 0); + KUNIT_EXPECT_EQ(test, + avd_check_run_image_fmt(ctx, AVD_IMG_FMT_420_8BIT), + -EINVAL); + KUNIT_EXPECT_EQ(test, + avd_check_run_image_fmt(ctx, AVD_IMG_FMT_422_10BIT), + -EINVAL); +} + +/* Compressed storage laid out for another size does not fit either. */ +static void avd_fmt_comp_mismatch_test(struct kunit *test) +{ + struct avd_ctx *ctx = avd_fmt_test_ctx(test, AVD_IMG_FMT_420_8BIT, + V4L2_PIX_FMT_NV12); + + fill_comp(&ctx->comp, AVD_IMG_FMT_420_8BIT, 32, 32); + KUNIT_EXPECT_EQ(test, + avd_check_run_image_fmt(ctx, AVD_IMG_FMT_420_8BIT), + -EINVAL); +} + +static struct kunit_case avd_fmt_test_cases[] = { + KUNIT_CASE(avd_fmt_any_test), + KUNIT_CASE(avd_fmt_any_p010_test), + KUNIT_CASE(avd_fmt_10bit_test), + KUNIT_CASE(avd_fmt_422_test), + KUNIT_CASE(avd_fmt_comp_mismatch_test), + {} +}; + +static struct kunit_suite avd_fmt_test_suite = { + .name = "apple-avd-fmt", + .test_cases = avd_fmt_test_cases, +}; + +kunit_test_suite(avd_fmt_test_suite); diff --git a/drivers/media/platform/apple/avd/avd-h264.c b/drivers/media/platform/apple/avd/avd-h264.c index 66592f75dacb4..940412dd6c020 100644 --- a/drivers/media/platform/apple/avd/avd-h264.c +++ b/drivers/media/platform/apple/avd/avd-h264.c @@ -672,6 +672,24 @@ static int avd_h264_validate_slice(struct avd_ctx *ctx, return 0; } +static enum avd_image_fmt +avd_h264_sps_image_fmt(const struct v4l2_ctrl_h264_sps *sps) +{ + if (sps->bit_depth_luma_minus8 == 0) { + if (sps->chroma_format_idc == 2) + return AVD_IMG_FMT_422_8BIT; + else + return AVD_IMG_FMT_420_8BIT; + } else if (sps->bit_depth_luma_minus8 == 2) { + if (sps->chroma_format_idc == 2) + return AVD_IMG_FMT_422_10BIT; + else + return AVD_IMG_FMT_420_10BIT; + } + + return AVD_IMG_FMT_ANY; +} + /* * H.264 7.4.2.1.1: the picture is PicWidthInMbs macroblocks wide and * FrameHeightInMbs = (2 - frame_mbs_only_flag) * PicHeightInMapUnits high. @@ -1024,6 +1042,9 @@ static int avd_h264_run(struct avd_ctx *ctx) /* Recheck controls applied from this request before building instructions. */ ret = avd_h264_validate_sps(ctx, run.sps); + if (!ret) + ret = avd_check_run_image_fmt(ctx, + avd_h264_sps_image_fmt(run.sps)); if (!ret) ret = avd_h264_validate_pps(ctx, run.pps); if (!ret) @@ -1182,24 +1203,10 @@ static void avd_h264_done(struct avd_ctx *ctx, struct vb2_v4l2_buffer *src_buf, static enum avd_image_fmt avd_h264_get_image_fmt(struct avd_ctx *ctx, struct v4l2_ctrl *ctrl) { - const struct v4l2_ctrl_h264_sps *sps = ctrl->p_new.p_h264_sps; - if (ctrl->id != V4L2_CID_STATELESS_H264_SPS) return AVD_IMG_FMT_ANY; - if (sps->bit_depth_luma_minus8 == 0) { - if (sps->chroma_format_idc == 2) - return AVD_IMG_FMT_422_8BIT; - else - return AVD_IMG_FMT_420_8BIT; - } else if (sps->bit_depth_luma_minus8 == 2) { - if (sps->chroma_format_idc == 2) - return AVD_IMG_FMT_422_10BIT; - else - return AVD_IMG_FMT_420_10BIT; - } - - return AVD_IMG_FMT_ANY; + return avd_h264_sps_image_fmt(ctrl->p_new.p_h264_sps); } static void avd_h264_adjust_decoded_fmt(struct avd_ctx *ctx, diff --git a/drivers/media/platform/apple/avd/avd-hevc.c b/drivers/media/platform/apple/avd/avd-hevc.c index 1f283c2058e3a..21c74fc653869 100644 --- a/drivers/media/platform/apple/avd/avd-hevc.c +++ b/drivers/media/platform/apple/avd/avd-hevc.c @@ -1038,14 +1038,9 @@ static void avd_hevc_adjust_decoded_fmt(struct avd_ctx *ctx, mv_color_size(pix_mp->width, pix_mp->height); } -static enum avd_image_fmt avd_hevc_get_image_fmt(struct avd_ctx *ctx, - struct v4l2_ctrl *ctrl) +static enum avd_image_fmt +avd_hevc_sps_image_fmt(const struct v4l2_ctrl_hevc_sps *sps) { - const struct v4l2_ctrl_hevc_sps *sps = ctrl->p_new.p_hevc_sps; - - if (ctrl->id != V4L2_CID_STATELESS_HEVC_SPS) - return AVD_IMG_FMT_ANY; - /* avd_hevc_validate_sps() refuses 4:4:4 and depths other than 8/10. */ if (sps->bit_depth_luma_minus8 == 0) { if (sps->chroma_format_idc == 2) @@ -1062,6 +1057,15 @@ static enum avd_image_fmt avd_hevc_get_image_fmt(struct avd_ctx *ctx, return AVD_IMG_FMT_ANY; } +static enum avd_image_fmt avd_hevc_get_image_fmt(struct avd_ctx *ctx, + struct v4l2_ctrl *ctrl) +{ + if (ctrl->id != V4L2_CID_STATELESS_HEVC_SPS) + return AVD_IMG_FMT_ANY; + + return avd_hevc_sps_image_fmt(ctrl->p_new.p_hevc_sps); +} + /* * The picture must have at least one sample in each direction and the CTB * size must be one of those H.265 allows (CtbLog2SizeY 4 to 6, 7.4.3.2.1): @@ -1690,6 +1694,9 @@ static int avd_hevc_run(struct avd_ctx *ctx) /* Also validate controls applied from a queued request before submission. */ ret = avd_hevc_validate_sps(ctx, run.sps); + if (!ret) + ret = avd_check_run_image_fmt(ctx, + avd_hevc_sps_image_fmt(run.sps)); if (ret) goto complete; diff --git a/drivers/media/platform/apple/avd/avd-v4l2.c b/drivers/media/platform/apple/avd/avd-v4l2.c index 5aabdefffc38d..b540eb551a6b0 100644 --- a/drivers/media/platform/apple/avd/avd-v4l2.c +++ b/drivers/media/platform/apple/avd/avd-v4l2.c @@ -66,6 +66,32 @@ static bool avd_image_fmt_fits_capture(struct avd_ctx *ctx, !memcmp(comp.offsets, ctx->comp.offsets, sizeof(comp.offsets)); } +/* + * Whether a job may program @image_fmt, the picture format that the current + * value of the codec's format control describes. The CAPTURE buffers and the + * per-context scratch were sized for ctx->image_fmt (ANY sizes them as 8-bit + * 4:2:0), and the control's value can differ from it: it may have been set + * while another codec was selected, or kept over an OUTPUT format change, and + * an unchanged value never reaches s_ctrl. Refuse the job unless both are the + * same and the CAPTURE buffers fit it. + */ +int avd_check_run_image_fmt(struct avd_ctx *ctx, enum avd_image_fmt image_fmt) +{ + enum avd_image_fmt sized = ctx->image_fmt; + + if (sized == AVD_IMG_FMT_ANY) + sized = AVD_IMG_FMT_420_8BIT; + if (image_fmt == AVD_IMG_FMT_ANY) + image_fmt = AVD_IMG_FMT_420_8BIT; + if (image_fmt != sized || !avd_image_fmt_fits_capture(ctx, image_fmt)) { + dev_dbg_ratelimited(ctx->dev->dev, + "picture format %d does not fit the buffers (%d)\n", + image_fmt, ctx->image_fmt); + return -EINVAL; + } + return 0; +} + static u32 avd_enum_decoded_fmt(struct avd_ctx *ctx, int index, enum avd_image_fmt image_fmt) { diff --git a/drivers/media/platform/apple/avd/avd-vp9.c b/drivers/media/platform/apple/avd/avd-vp9.c index a0de2f7612f48..09f8567169109 100644 --- a/drivers/media/platform/apple/avd/avd-vp9.c +++ b/drivers/media/platform/apple/avd/avd-vp9.c @@ -698,6 +698,14 @@ static void init_probs(struct avd_ctx *ctx, const struct avd_vp9_run *run) sizeof(avd_probs->seg.tree_probs)); } +static enum avd_image_fmt +avd_vp9_frame_image_fmt(const struct v4l2_ctrl_vp9_frame *frame) +{ + /* avd_vp9_try_ctrl() admits only 4:2:0 (profiles 0 and 2). */ + return frame->bit_depth == 8 ? AVD_IMG_FMT_420_8BIT : + AVD_IMG_FMT_420_10BIT; +} + /* * The row and column scratch is sized at STREAMON from the OUTPUT (coded) * format, which cannot change while it streams. The CAPTURE format can be @@ -921,6 +929,9 @@ static int avd_vp9_run_preamble(struct avd_ctx *ctx, struct avd_vp9_run *run) /* The frame may have been set, unchecked, while another codec was selected. */ ret = avd_vp9_validate_frame(ctx, dec_params); + if (!ret) + ret = avd_check_run_image_fmt(ctx, + avd_vp9_frame_image_fmt(dec_params)); if (ret) return ret; @@ -1286,17 +1297,10 @@ static int avd_vp9_start(struct avd_ctx *ctx) static enum avd_image_fmt avd_vp9_get_image_fmt(struct avd_ctx *ctx, struct v4l2_ctrl *ctrl) { -#define BIT_DEPTH(chroma) \ - (frame->bit_depth == 8 ? AVD_IMG_FMT_##chroma##_8BIT : \ - AVD_IMG_FMT_##chroma##_10BIT) - const struct v4l2_ctrl_vp9_frame *frame = ctrl->p_new.p_vp9_frame; - if (ctrl->id != V4L2_CID_STATELESS_VP9_FRAME) return AVD_IMG_FMT_ANY; - /* avd_vp9_try_ctrl() admits only 4:2:0 (profiles 0 and 2). */ - return BIT_DEPTH(420); -#undef BIT_DEPTH + return avd_vp9_frame_image_fmt(ctrl->p_new.p_vp9_frame); } static int avd_vp9_try_ctrl(struct avd_ctx *ctx, struct v4l2_ctrl *ctrl) diff --git a/drivers/media/platform/apple/avd/avd.h b/drivers/media/platform/apple/avd/avd.h index e36f0d60c2d86..7b1b6992b9621 100644 --- a/drivers/media/platform/apple/avd/avd.h +++ b/drivers/media/platform/apple/avd/avd.h @@ -368,6 +368,7 @@ void avd_job_finish_no_pm(struct avd_ctx *ctx, enum vb2_buffer_state result); void avd_job_finish(struct avd_ctx *ctx, enum vb2_buffer_state result); void avd_run_preamble(struct avd_ctx *ctx, struct avd_run *run); +int avd_check_run_image_fmt(struct avd_ctx *ctx, enum avd_image_fmt image_fmt); void avd_run_postamble(struct avd_ctx *ctx, struct avd_run *run); #if IS_ENABLED(CONFIG_KUNIT) From 4f35e8c1859a7a940b1f700da9c21f7ac45dd419 Mon Sep 17 00:00:00 2001 From: Ryan Murray Date: Mon, 23 Nov 2026 08:12:51 +1100 Subject: [PATCH 13/26] media: apple: avd: av1: copy the tile-group entries for the run V4L2_CID_STATELESS_AV1_TILE_GROUP_ENTRY is a dynamic array. Setting a larger array through VIDIOC_S_EXT_CTRLS reallocates the control's storage and frees the old one under the control lock, but the run kept ctrl->p_cur.p and ctrl->elems and read the entries without that lock, in avd_av1_check_tiles() and again later in set_tiles(). The entries used to build the tiles could therefore be freed memory, or differ from the ones that were checked. Copy the entries and their count under v4l2_ctrl_lock(), as the HEVC code does for its dynamic arrays, check the copy and build the tiles from it, and free it on every exit of avd_av1_run(). Also take the tile grid (tile_rows, tile_cols) from the frame control once and use that same grid for the check, the job size and set_tiles(), so the number of entries read can not exceed the number that was checked if the frame control is set at the same time. Add a KUnit test that the copy holds the entries and count after the control's storage has been replaced and freed. Signed-off-by: Ryan Murray --- .../media/platform/apple/avd/avd-av1-test.c | 62 +++++++++++++++++ drivers/media/platform/apple/avd/avd-av1.c | 69 ++++++++++++++----- drivers/media/platform/apple/avd/avd.h | 2 + 3 files changed, 114 insertions(+), 19 deletions(-) diff --git a/drivers/media/platform/apple/avd/avd-av1-test.c b/drivers/media/platform/apple/avd/avd-av1-test.c index 33ad22165ce79..34768e5877ff8 100644 --- a/drivers/media/platform/apple/avd/avd-av1-test.c +++ b/drivers/media/platform/apple/avd/avd-av1-test.c @@ -4,6 +4,7 @@ */ #include +#include #include "avd.h" @@ -110,11 +111,72 @@ static void avd_av1_tiles_rowcol_test(struct kunit *test) AVD_TEST_PAYLOAD), c->ret); } +/* + * The run takes its own copy of the tile-group entries: replacing and freeing + * the control's storage afterwards, as setting a larger array does, leaves + * the copy and its count intact. + */ +static void avd_av1_copy_tile_group_test(struct kunit *test) +{ + struct v4l2_ctrl_av1_tile_group_entry expect[6] = {}; + struct v4l2_ctrl_av1_tile_group_entry *src, *bigger, *copy; + struct v4l2_ctrl_handler *hdl; + struct v4l2_ctrl *ctrl; + unsigned int entries = 0; + + hdl = kunit_kzalloc(test, sizeof(*hdl), GFP_KERNEL); + KUNIT_ASSERT_NOT_NULL(test, hdl); + ctrl = kunit_kzalloc(test, sizeof(*ctrl), GFP_KERNEL); + KUNIT_ASSERT_NOT_NULL(test, ctrl); + KUNIT_ASSERT_EQ(test, v4l2_ctrl_handler_init(hdl, 0), 0); + + src = kcalloc(ARRAY_SIZE(expect), sizeof(*src), GFP_KERNEL); + bigger = kcalloc(V4L2_AV1_MAX_TILE_COUNT, sizeof(*bigger), GFP_KERNEL); + if (!src || !bigger) { + kfree(src); + kfree(bigger); + v4l2_ctrl_handler_free(hdl); + KUNIT_FAIL(test, "out of memory"); + return; + } + avd_av1_test_layout(expect, 2, 3); + memcpy(src, expect, sizeof(expect)); + ctrl->handler = hdl; + ctrl->elems = ARRAY_SIZE(expect); + ctrl->elem_size = sizeof(*src); + ctrl->p_cur.p = src; + + copy = avd_av1_copy_tile_group(ctrl, &entries); + KUNIT_EXPECT_EQ(test, entries, ARRAY_SIZE(expect)); + + /* A larger array replaces and frees the old storage. */ + memset(bigger, 0xff, V4L2_AV1_MAX_TILE_COUNT * sizeof(*bigger)); + ctrl->p_cur.p = bigger; + ctrl->elems = V4L2_AV1_MAX_TILE_COUNT; + memset(src, 0xff, sizeof(expect)); + kfree(src); + + if (copy) { + KUNIT_EXPECT_MEMEQ(test, copy, expect, sizeof(expect)); + KUNIT_EXPECT_EQ(test, avd_av1_check_tiles(NULL, 2, 3, copy, + entries, + AVD_TEST_PAYLOAD), 0); + } else { + KUNIT_FAIL(test, "no copy"); + } + KUNIT_EXPECT_EQ(test, entries, ARRAY_SIZE(expect)); + + kfree(copy); + kfree(bigger); + v4l2_ctrl_handler_free(hdl); +} + static struct kunit_case avd_av1_test_cases[] = { KUNIT_CASE(avd_av1_tiles_valid_test), KUNIT_CASE(avd_av1_tiles_count_test), KUNIT_CASE(avd_av1_tiles_extent_test), KUNIT_CASE_PARAM(avd_av1_tiles_rowcol_test, avd_av1_rowcol_gen_params), + KUNIT_CASE(avd_av1_copy_tile_group_test), {} }; diff --git a/drivers/media/platform/apple/avd/avd-av1.c b/drivers/media/platform/apple/avd/avd-av1.c index 851eebce2fee9..1bbf54f9b83aa 100644 --- a/drivers/media/platform/apple/avd/avd-av1.c +++ b/drivers/media/platform/apple/avd/avd-av1.c @@ -221,7 +221,12 @@ struct avd_av1_run { const struct v4l2_ctrl_av1_sequence *seq; const struct v4l2_ctrl_av1_frame *frame; - const struct v4l2_ctrl_av1_tile_group_entry *tile_group; + /* Private copy of the tile-group control, freed by avd_av1_run(). */ + struct v4l2_ctrl_av1_tile_group_entry *tile_group; + unsigned int tile_group_entries; + /* The tile grid tile_group was checked against */ + unsigned int tile_rows; + unsigned int tile_cols; const struct v4l2_ctrl_av1_film_grain *grain; }; @@ -1139,12 +1144,12 @@ static void set_tiles(struct avd_ctx *ctx, struct avd_av1_run *run) seq->flags & V4L2_AV1_SEQUENCE_FLAG_USE_128X128_SUPERBLOCK ? 5 : 4; - av1_ctx->submit_num = tile_info->tile_cols * tile_info->tile_rows; + av1_ctx->submit_num = run->tile_cols * run->tile_rows; - for (row = 0; row < tile_info->tile_rows; row++) { - for (col = 0; col < tile_info->tile_cols; col++) { + for (row = 0; row < run->tile_rows; row++) { + for (col = 0; col < run->tile_cols; col++) { ctx->job.num++; - tile_id = row * tile_info->tile_cols + col; + tile_id = row * run->tile_cols + col; tile_group = &run->tile_group[tile_id]; push(AVD_OP_CODED_DATA | @@ -1440,6 +1445,25 @@ avd_av1_check_tiles(struct device *dev, unsigned int tile_rows, } EXPORT_SYMBOL_IF_KUNIT(avd_av1_check_tiles); +/* + * Setting a larger tile-group array reallocates the control's storage and + * frees the old one under the control lock, which the run does not otherwise + * hold. Copy the entries under that lock; the run checks and uses the copy. + */ +VISIBLE_IF_KUNIT struct v4l2_ctrl_av1_tile_group_entry * +avd_av1_copy_tile_group(struct v4l2_ctrl *ctrl, unsigned int *entries) +{ + struct v4l2_ctrl_av1_tile_group_entry *copy; + + v4l2_ctrl_lock(ctrl); + *entries = ctrl->elems; + copy = kmemdup_array(ctrl->p_cur.p, ctrl->elems, ctrl->elem_size, + GFP_KERNEL); + v4l2_ctrl_unlock(ctrl); + return copy; +} +EXPORT_SYMBOL_IF_KUNIT(avd_av1_copy_tile_group); + /* * Only the AV1 Main profile at 4:2:0 (8 and 10 bits) decodes. A High * profile (4:4:4) frame is never completed: it times out and the whole @@ -1523,10 +1547,14 @@ static int avd_av1_run_preamble(struct avd_ctx *ctx, struct avd_av1_run *run) V4L2_CID_STATELESS_AV1_TILE_GROUP_ENTRY); if (WARN_ON(!ctrl)) return -EINVAL; - run->tile_group = ctrl->p_cur.p; - ret = avd_av1_check_tiles(ctx->dev->dev, run->frame->tile_info.tile_rows, - run->frame->tile_info.tile_cols, - run->tile_group, ctrl->elems, + run->tile_group = avd_av1_copy_tile_group(ctrl, &run->tile_group_entries); + if (!run->tile_group) + return -ENOMEM; + /* The frame control can be set meanwhile too: use one tile grid throughout. */ + run->tile_rows = run->frame->tile_info.tile_rows; + run->tile_cols = run->frame->tile_info.tile_cols; + ret = avd_av1_check_tiles(ctx->dev->dev, run->tile_rows, run->tile_cols, + run->tile_group, run->tile_group_entries, vb2_get_plane_payload(&run->base.bufs.src->vb2_buf, 0)); if (ret) return ret; @@ -1694,7 +1722,7 @@ static int avd_av1_alloc_work_bufs(struct avd_ctx *ctx, struct avd_av1_run *run) static int avd_av1_run(struct avd_ctx *ctx) { - struct avd_av1_run run; + struct avd_av1_run run = {}; struct avd_av1_stream *av1_ctx = ((struct avd_av1_ctx *)ctx->priv)->stream; struct avd_decoded_buffer *dst, *ref; int ret; @@ -1702,7 +1730,7 @@ static int avd_av1_run(struct avd_ctx *ctx) ret = avd_av1_run_preamble(ctx, &run); if (ret) { avd_run_postamble(ctx, &run.base); - return ret; + goto out; } /* The CDF and segmentation map copies need the CPU mappings. */ @@ -1716,7 +1744,8 @@ static int avd_av1_run(struct avd_ctx *ctx) if (!vb2_plane_vaddr(&dst->base.vb.vb2_buf, 0) || !vb2_plane_vaddr(&ref->base.vb.vb2_buf, 0)) { avd_run_postamble(ctx, &run.base); - return -ENOMEM; + ret = -ENOMEM; + goto out; } if (av1_ctx->nbanks > 1) { struct avd_av1_bank *bank = NULL; @@ -1728,7 +1757,8 @@ static int avd_av1_run(struct avd_ctx *ctx) } if (!bank) { avd_run_postamble(ctx, &run.base); - return -EBUSY; + ret = -EBUSY; + goto out; } av1_ctx->b = bank; smp_store_release(&ctx->priv, &bank->codec); @@ -1743,18 +1773,16 @@ static int avd_av1_run(struct avd_ctx *ctx) } ret = avd_init_job(ctx, AVD_CODEC_AV1, - run.frame->tile_info.tile_cols * - run.frame->tile_info.tile_rows + - 1); + run.tile_cols * run.tile_rows + 1); if (ret) { avd_run_postamble(ctx, &run.base); - return ret; + goto out; } ret = avd_av1_alloc_work_bufs(ctx, &run); if (ret) { avd_run_postamble(ctx, &run.base); - return ret; + goto out; } update_dec_buf_info(dst, run.seq, run.frame); @@ -1771,7 +1799,10 @@ static int avd_av1_run(struct avd_ctx *ctx) avd_run_postamble(ctx, &run.base); - return avd_submit_job(ctx); + ret = avd_submit_job(ctx); +out: + kfree(run.tile_group); + return ret; } diff --git a/drivers/media/platform/apple/avd/avd.h b/drivers/media/platform/apple/avd/avd.h index 7b1b6992b9621..2f77cf975a9d0 100644 --- a/drivers/media/platform/apple/avd/avd.h +++ b/drivers/media/platform/apple/avd/avd.h @@ -394,6 +394,8 @@ int avd_av1_check_tiles(struct device *dev, unsigned int tile_rows, unsigned int tile_cols, const struct v4l2_ctrl_av1_tile_group_entry *tile_group, unsigned int entries, size_t payload); +struct v4l2_ctrl_av1_tile_group_entry * +avd_av1_copy_tile_group(struct v4l2_ctrl *ctrl, unsigned int *entries); int avd_h264_validate_sps(struct avd_ctx *ctx, const struct v4l2_ctrl_h264_sps *sps); int avd_try_capture_fmt(struct file *file, void *priv, struct v4l2_format *f); From 9ffbad7fbea51c71a1d8b709a4485a2e846a371d Mon Sep 17 00:00:00 2001 From: Ryan Murray Date: Mon, 23 Nov 2026 08:16:35 +1100 Subject: [PATCH 14/26] media: apple: avd: h264: Bound the slice table made at STREAMON avd_h264_start() sized each bank's slice table, and through it the job table of the first picture, at one slice per row of the SPS, without the avd_h264_max_slices() limit the run path applies when the table grows. Take the smaller of the two, as avd_h264_realloc_slices() does. These tables and the codec context are allocated in the context of the STREAMON caller; charge them to it with GFP_KERNEL_ACCOUNT. Add a KUnit test that the initial count is one per row and never exceeds the slice table limit, whatever the SPS geometry. Signed-off-by: Ryan Murray --- .../media/platform/apple/avd/avd-h264-test.c | 22 +++++++++++++++++++ drivers/media/platform/apple/avd/avd-h264.c | 19 ++++++++++++---- drivers/media/platform/apple/avd/avd.h | 1 + 3 files changed, 38 insertions(+), 4 deletions(-) diff --git a/drivers/media/platform/apple/avd/avd-h264-test.c b/drivers/media/platform/apple/avd/avd-h264-test.c index bdeb90594b1d5..3b7fec1d7b51b 100644 --- a/drivers/media/platform/apple/avd/avd-h264-test.c +++ b/drivers/media/platform/apple/avd/avd-h264-test.c @@ -160,6 +160,27 @@ static void avd_sps_size_test(struct kunit *test) KUNIT_EXPECT_EQ(test, avd_h264_validate_sps(ctx, &sps), c->ret); } +static void avd_start_slices_test(struct kunit *test) +{ + struct v4l2_ctrl_h264_sps sps = { + .flags = V4L2_H264_SPS_FLAG_FRAME_MBS_ONLY, + }; + + /* One slice per row */ + sps.pic_width_in_mbs_minus1 = 119; + sps.pic_height_in_map_units_minus1 = 67; + KUNIT_EXPECT_EQ(test, avd_h264_start_slices(&sps), 68); + + /* No more rows than macroblocks, and never more than the table */ + sps.pic_width_in_mbs_minus1 = 0; + sps.pic_height_in_map_units_minus1 = U16_MAX; + KUNIT_EXPECT_LE(test, avd_h264_start_slices(&sps), AVD_TEST_MAX_SLICES); + sps.pic_width_in_mbs_minus1 = U16_MAX; + KUNIT_EXPECT_LE(test, avd_h264_start_slices(&sps), AVD_TEST_MAX_SLICES); + sps.flags = 0; + KUNIT_EXPECT_LE(test, avd_h264_start_slices(&sps), AVD_TEST_MAX_SLICES); +} + static void avd_scaling_flat_test(struct kunit *test) { struct v4l2_ctrl_h264_pps pps = {}; @@ -188,6 +209,7 @@ static struct kunit_case avd_h264_test_cases[] = { KUNIT_CASE_PARAM(avd_slice_data_test, avd_slice_data_gen_params), KUNIT_CASE_PARAM(avd_slice_fits_test, avd_slice_fits_gen_params), KUNIT_CASE_PARAM(avd_sps_size_test, avd_sps_size_gen_params), + KUNIT_CASE(avd_start_slices_test), {} }; diff --git a/drivers/media/platform/apple/avd/avd-h264.c b/drivers/media/platform/apple/avd/avd-h264.c index 940412dd6c020..b5f87f6def914 100644 --- a/drivers/media/platform/apple/avd/avd-h264.c +++ b/drivers/media/platform/apple/avd/avd-h264.c @@ -769,6 +769,9 @@ static void avd_h264_stop(struct avd_ctx *ctx) ctx->priv = NULL; } +VISIBLE_IF_KUNIT size_t +avd_h264_start_slices(const struct v4l2_ctrl_h264_sps *sps); + static int avd_h264_start(struct avd_ctx *ctx) { struct avd_h264_ctx *h264_ctx; @@ -783,7 +786,7 @@ static int avd_h264_start(struct avd_ctx *ctx) if (ret) return ret; - h264_ctx = kzalloc(sizeof(*h264_ctx), GFP_KERNEL); + h264_ctx = kzalloc(sizeof(*h264_ctx), GFP_KERNEL_ACCOUNT); if (!h264_ctx) return -ENOMEM; @@ -795,11 +798,10 @@ static int avd_h264_start(struct avd_ctx *ctx) for (unsigned int i = 0; i < h264_ctx->nbanks; i++) { struct avd_bank *bank = &h264_ctx->bank[i]; - /* assume one slice per row */ bank->alloc_slice_num = - ctrl->p_new.p_h264_sps->pic_height_in_map_units_minus1 + 1; + avd_h264_start_slices(ctrl->p_new.p_h264_sps); bank->slices = kcalloc(bank->alloc_slice_num, - sizeof(*bank->slices), GFP_KERNEL); + sizeof(*bank->slices), GFP_KERNEL_ACCOUNT); if (!bank->slices) { ret = -ENOMEM; goto err_free_ctx; @@ -956,6 +958,15 @@ static size_t avd_h264_max_slices(const struct v4l2_ctrl_h264_sps *sps) return min_t(size_t, mbs, MAX_SLICES); } +/* Slice table entries made at STREAMON: one slice per row to begin with. */ +VISIBLE_IF_KUNIT size_t +avd_h264_start_slices(const struct v4l2_ctrl_h264_sps *sps) +{ + return min_t(size_t, sps->pic_height_in_map_units_minus1 + 1, + avd_h264_max_slices(sps)); +} +EXPORT_SYMBOL_IF_KUNIT(avd_h264_start_slices); + /* * Coded data held for one picture. A picture of PCM macroblocks is about the * size of the decoded one and emulation prevention adds at most one byte for diff --git a/drivers/media/platform/apple/avd/avd.h b/drivers/media/platform/apple/avd/avd.h index 2f77cf975a9d0..54d9e8a48289b 100644 --- a/drivers/media/platform/apple/avd/avd.h +++ b/drivers/media/platform/apple/avd/avd.h @@ -398,6 +398,7 @@ struct v4l2_ctrl_av1_tile_group_entry * avd_av1_copy_tile_group(struct v4l2_ctrl *ctrl, unsigned int *entries); int avd_h264_validate_sps(struct avd_ctx *ctx, const struct v4l2_ctrl_h264_sps *sps); +size_t avd_h264_start_slices(const struct v4l2_ctrl_h264_sps *sps); int avd_try_capture_fmt(struct file *file, void *priv, struct v4l2_format *f); int avd_s_capture_fmt(struct file *file, void *priv, struct v4l2_format *f); int avd_vp9_check_tiling(const struct v4l2_ctrl_vp9_frame *frame); From b0471fb1fe907faed5897264752b1490ef130ae8 Mon Sep 17 00:00:00 2001 From: Ryan Murray Date: Mon, 23 Nov 2026 08:16:35 +1100 Subject: [PATCH 15/26] media: apple: avd: Allocate the context FIFO at STREAMON Every open() allocated the context's 12 MB coherent instruction FIFO, although it is only handed to the hardware for frames, which run while the OUTPUT queue streams. Allocate it when OUTPUT starts streaming, before the codec's start(), and free it when OUTPUT stops, after the context's frames have been drained and the codec's own buffers freed. release() still frees it for a context closed while streaming. Signed-off-by: Ryan Murray --- drivers/media/platform/apple/avd/avd-drv.c | 5 ----- drivers/media/platform/apple/avd/avd-v4l2.c | 11 ++++++++++- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/drivers/media/platform/apple/avd/avd-drv.c b/drivers/media/platform/apple/avd/avd-drv.c index c79379d2a50f2..c96a9e3b1c571 100644 --- a/drivers/media/platform/apple/avd/avd-drv.c +++ b/drivers/media/platform/apple/avd/avd-drv.c @@ -190,10 +190,6 @@ static int avd_open(struct file *filp) ctx->dev = avd; - ret = avd_buf_alloc(avd, &ctx->inst, fifo_size()); - if (ret) - goto err_free_ctx; - INIT_LIST_HEAD(&ctx->queue_node); ctx->next_dep = AVD_DEP_PREV_DONE; @@ -221,7 +217,6 @@ static int avd_open(struct file *filp) v4l2_m2m_ctx_release(ctx->fh.m2m_ctx); err_free_ctx: - avd_buf_free(avd, &ctx->inst); kfree(ctx); return ret; } diff --git a/drivers/media/platform/apple/avd/avd-v4l2.c b/drivers/media/platform/apple/avd/avd-v4l2.c index b540eb551a6b0..1cd84c6259d43 100644 --- a/drivers/media/platform/apple/avd/avd-v4l2.c +++ b/drivers/media/platform/apple/avd/avd-v4l2.c @@ -19,6 +19,7 @@ #include #include "avd.h" +#include "avd-inst.h" static bool avd_image_fmt_match(enum avd_image_fmt fmt1, enum avd_image_fmt fmt2) @@ -972,14 +973,21 @@ static int avd_start_streaming(struct vb2_queue *q, unsigned int count) goto err_return_buffers; } + /* The context's instruction FIFO is only used while OUTPUT streams. */ + ret = avd_buf_alloc(ctx->dev, &ctx->inst, fifo_size()); + if (ret) + goto err_return_buffers; + if (desc->ops->start) { ret = desc->ops->start(ctx); if (ret) - goto err_return_buffers; + goto err_free_inst; } return 0; +err_free_inst: + avd_buf_free(ctx->dev, &ctx->inst); err_return_buffers: /* Keep requests queued for a later STREAMON retry. */ while ((vbuf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx))) @@ -1021,6 +1029,7 @@ static void avd_stop_streaming(struct vb2_queue *q) if (desc->ops->stop) desc->ops->stop(ctx); + avd_buf_free(ctx->dev, &ctx->inst); /* Drop a job table left over from a frame that never finished. */ kvfree(ctx->job.segments); From e3ad16ebf95086ad77cec05075cef2bd0799fa7b Mon Sep 17 00:00:00 2001 From: Ryan Murray Date: Mon, 23 Nov 2026 10:52:06 +1100 Subject: [PATCH 16/26] media: apple: avd: refuse pictures deeper than the scratch was sized for H.264 and VP9 size their per-stream scratch rows at STREAMON(OUTPUT) from the picture format of that moment. s_ctrl only refuses a format change while CAPTURE buffers are allocated, so a format control set after STREAMON(OUTPUT) and before the CAPTURE buffers, or after they were freed, changed the picture format without resizing the scratch. Record the format the scratch was sized for, and have the run-time picture format check refuse a job whose bit depth differs from it. H.264 STREAMON now also checks and sizes from the SPS's current value rather than from the last value tried. Add a KUnit case for the scratch check. Signed-off-by: Ryan Murray --- .../media/platform/apple/avd/avd-fmt-test.c | 22 +++++++++++++++++++ drivers/media/platform/apple/avd/avd-h264.c | 8 +++++-- drivers/media/platform/apple/avd/avd-v4l2.c | 13 +++++++++-- drivers/media/platform/apple/avd/avd-vp9.c | 4 ++++ drivers/media/platform/apple/avd/avd.h | 6 +++++ 5 files changed, 49 insertions(+), 4 deletions(-) diff --git a/drivers/media/platform/apple/avd/avd-fmt-test.c b/drivers/media/platform/apple/avd/avd-fmt-test.c index ca6ec6e4a8608..63a935a2ab047 100644 --- a/drivers/media/platform/apple/avd/avd-fmt-test.c +++ b/drivers/media/platform/apple/avd/avd-fmt-test.c @@ -111,12 +111,34 @@ static void avd_fmt_comp_mismatch_test(struct kunit *test) -EINVAL); } +/* Scratch sized at STREAMON for 8 bits does not take a 10-bit picture. */ +static void avd_fmt_scratch_depth_test(struct kunit *test) +{ + struct avd_ctx *ctx = avd_fmt_test_ctx(test, AVD_IMG_FMT_420_10BIT, + V4L2_PIX_FMT_P010); + + ctx->scratch_sized = true; + ctx->scratch_fmt = AVD_IMG_FMT_ANY; + KUNIT_EXPECT_EQ(test, + avd_check_run_image_fmt(ctx, AVD_IMG_FMT_420_10BIT), + -EINVAL); + ctx->scratch_fmt = AVD_IMG_FMT_420_10BIT; + KUNIT_EXPECT_EQ(test, + avd_check_run_image_fmt(ctx, AVD_IMG_FMT_420_10BIT), 0); + /* Codecs that size no scratch from the format are not limited. */ + ctx->scratch_sized = false; + ctx->scratch_fmt = AVD_IMG_FMT_ANY; + KUNIT_EXPECT_EQ(test, + avd_check_run_image_fmt(ctx, AVD_IMG_FMT_420_10BIT), 0); +} + static struct kunit_case avd_fmt_test_cases[] = { KUNIT_CASE(avd_fmt_any_test), KUNIT_CASE(avd_fmt_any_p010_test), KUNIT_CASE(avd_fmt_10bit_test), KUNIT_CASE(avd_fmt_422_test), KUNIT_CASE(avd_fmt_comp_mismatch_test), + KUNIT_CASE(avd_fmt_scratch_depth_test), {} }; diff --git a/drivers/media/platform/apple/avd/avd-h264.c b/drivers/media/platform/apple/avd/avd-h264.c index b5f87f6def914..e936875f2c9c3 100644 --- a/drivers/media/platform/apple/avd/avd-h264.c +++ b/drivers/media/platform/apple/avd/avd-h264.c @@ -748,6 +748,7 @@ static void avd_h264_stop(struct avd_ctx *ctx) unsigned int i; ctx->max_frames = 1; + ctx->scratch_sized = false; if (!h264_ctx) return; @@ -782,7 +783,7 @@ static int avd_h264_start(struct avd_ctx *ctx) if (!ctrl) return -EINVAL; - ret = avd_h264_validate_sps(ctx, ctrl->p_new.p_h264_sps); + ret = avd_h264_validate_sps(ctx, ctrl->p_cur.p_h264_sps); if (ret) return ret; @@ -799,7 +800,7 @@ static int avd_h264_start(struct avd_ctx *ctx) struct avd_bank *bank = &h264_ctx->bank[i]; bank->alloc_slice_num = - avd_h264_start_slices(ctrl->p_new.p_h264_sps); + avd_h264_start_slices(ctrl->p_cur.p_h264_sps); bank->slices = kcalloc(bank->alloc_slice_num, sizeof(*bank->slices), GFP_KERNEL_ACCOUNT); if (!bank->slices) { @@ -812,6 +813,9 @@ static int avd_h264_start(struct avd_ctx *ctx) goto err_free_ctx; } ctx->max_frames = h264_ctx->nbanks; + /* avd_h264_alloc_bufs() sized the scratch rows for this depth */ + ctx->scratch_fmt = ctx->image_fmt; + ctx->scratch_sized = true; return 0; diff --git a/drivers/media/platform/apple/avd/avd-v4l2.c b/drivers/media/platform/apple/avd/avd-v4l2.c index 1cd84c6259d43..3177ae15ed3fd 100644 --- a/drivers/media/platform/apple/avd/avd-v4l2.c +++ b/drivers/media/platform/apple/avd/avd-v4l2.c @@ -74,8 +74,15 @@ static bool avd_image_fmt_fits_capture(struct avd_ctx *ctx, * 4:2:0), and the control's value can differ from it: it may have been set * while another codec was selected, or kept over an OUTPUT format change, and * an unchanged value never reaches s_ctrl. Refuse the job unless both are the - * same and the CAPTURE buffers fit it. + * same and the CAPTURE buffers fit it, and unless the scratch a codec sized at + * STREAMON has the bit depth of the picture (s_ctrl only refuses a change + * while CAPTURE buffers are allocated, not while OUTPUT streams). */ +static bool avd_image_fmt_10bit(enum avd_image_fmt fmt) +{ + return fmt == AVD_IMG_FMT_420_10BIT || fmt == AVD_IMG_FMT_422_10BIT; +} + int avd_check_run_image_fmt(struct avd_ctx *ctx, enum avd_image_fmt image_fmt) { enum avd_image_fmt sized = ctx->image_fmt; @@ -84,7 +91,9 @@ int avd_check_run_image_fmt(struct avd_ctx *ctx, enum avd_image_fmt image_fmt) sized = AVD_IMG_FMT_420_8BIT; if (image_fmt == AVD_IMG_FMT_ANY) image_fmt = AVD_IMG_FMT_420_8BIT; - if (image_fmt != sized || !avd_image_fmt_fits_capture(ctx, image_fmt)) { + if (image_fmt != sized || !avd_image_fmt_fits_capture(ctx, image_fmt) || + (ctx->scratch_sized && + avd_image_fmt_10bit(ctx->scratch_fmt) != avd_image_fmt_10bit(image_fmt))) { dev_dbg_ratelimited(ctx->dev->dev, "picture format %d does not fit the buffers (%d)\n", image_fmt, ctx->image_fmt); diff --git a/drivers/media/platform/apple/avd/avd-vp9.c b/drivers/media/platform/apple/avd/avd-vp9.c index 09f8567169109..e3590e27d9c53 100644 --- a/drivers/media/platform/apple/avd/avd-vp9.c +++ b/drivers/media/platform/apple/avd/avd-vp9.c @@ -1264,6 +1264,7 @@ static void avd_vp9_stop(struct avd_ctx *ctx) /* drain() excludes readiness callbacks before private storage is freed. */ avd_vp9_free(ctx, ctx->priv); ctx->priv = NULL; + ctx->scratch_sized = false; } static int avd_vp9_start(struct avd_ctx *ctx) @@ -1284,6 +1285,9 @@ static int avd_vp9_start(struct avd_ctx *ctx) avd_init_v4l2_vp9_count_tbl(&vp9_ctx->bank[i]); } ctx->max_frames = vp9_ctx->nbanks; + /* avd_vp9_alloc_bufs() sized the scratch for this depth */ + ctx->scratch_fmt = ctx->image_fmt; + ctx->scratch_sized = true; /* job_ready can run while STREAMON is still initializing this codec. */ smp_store_release(&ctx->priv, vp9_ctx); diff --git a/drivers/media/platform/apple/avd/avd.h b/drivers/media/platform/apple/avd/avd.h index 54d9e8a48289b..a9c5f03e81d52 100644 --- a/drivers/media/platform/apple/avd/avd.h +++ b/drivers/media/platform/apple/avd/avd.h @@ -345,6 +345,12 @@ struct avd_ctx { void *priv; struct avd_comp comp; + /* + * Picture format the codec sized its per-stream scratch for at + * STREAMON, if it sizes any from the picture format. + */ + bool scratch_sized; + enum avd_image_fmt scratch_fmt; int fifo_idx; /* Pictures of this context the queue may hold at once (0 or 1: one). */ unsigned int max_frames; From beb13940004d4587b7c9889b2abeaa789c4a2cf2 Mon Sep 17 00:00:00 2001 From: Ryan Murray Date: Mon, 23 Nov 2026 09:54:59 +1100 Subject: [PATCH 17/26] media: apple: avd: add a helper to copy a control's value for a run A run reads the codec's parameters from the current value of the controls. The request's controls are applied before that, but a control can also be set directly while the run is in progress, and its value is then rewritten under the control lock, which the run does not hold. A run that checks a value and later reads it again from the control can see two different values. Add avd_ctrl_copy(), which copies the current value of a fixed-size control under the control lock and refuses a destination of another size, so that a run can check and use one private copy. The dynamic arrays are already copied this way. v4l2_ctrl_find() takes the same lock, so the lookup is done first and avd_ctrl_copy_value() copies a control that was already found. The KUnit cases check that a copy keeps its value when the control is set again, that the size is checked, and that the copy waits for the holder of the control lock. Signed-off-by: Ryan Murray --- .../media/platform/apple/avd/avd-v4l2-test.c | 129 +++++++++++++++++- drivers/media/platform/apple/avd/avd-v4l2.c | 34 +++++ drivers/media/platform/apple/avd/avd.h | 2 + 3 files changed, 164 insertions(+), 1 deletion(-) diff --git a/drivers/media/platform/apple/avd/avd-v4l2-test.c b/drivers/media/platform/apple/avd/avd-v4l2-test.c index 0c057d567b615..3b2e09b6f6d9c 100644 --- a/drivers/media/platform/apple/avd/avd-v4l2-test.c +++ b/drivers/media/platform/apple/avd/avd-v4l2-test.c @@ -1,10 +1,13 @@ // SPDX-License-Identifier: GPL-2.0 /* - * KUnit tests for the format ioctls of the Apple video decoder. + * KUnit tests for the format ioctls and the control helpers of the Apple + * video decoder. */ #include +#include #include +#include #include #include "avd.h" @@ -136,9 +139,133 @@ static void avd_s_capture_fmt_test(struct kunit *test) avd_test_expect_comp(test, &t.ctx->comp, &want); } +/* A context whose control handler holds the VP9 frame control. */ +static struct v4l2_ctrl *avd_test_ctrl_init(struct kunit *test, + struct avd_ctx **ctxp) +{ + static const struct v4l2_ctrl_config cfg = { + .id = V4L2_CID_STATELESS_VP9_FRAME, + }; + struct avd_ctx *ctx; + struct v4l2_ctrl *ctrl; + + ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL); + KUNIT_ASSERT_NOT_NULL(test, ctx); + v4l2_ctrl_handler_init(&ctx->ctrl_hdl, 1); + ctrl = v4l2_ctrl_new_custom(&ctx->ctrl_hdl, &cfg, NULL); + KUNIT_ASSERT_EQ(test, ctx->ctrl_hdl.error, 0); + KUNIT_ASSERT_NOT_NULL(test, ctrl); + + *ctxp = ctx; + return ctrl; +} + +static int avd_test_set_vp9_width(struct v4l2_ctrl *ctrl, u16 width) +{ + struct v4l2_ctrl_vp9_frame frame = *ctrl->p_cur.p_vp9_frame; + + frame.frame_width_minus_1 = width - 1; + frame.frame_height_minus_1 = width - 1; + return v4l2_ctrl_s_ctrl_compound(ctrl, V4L2_CTRL_TYPE_VP9_FRAME, + &frame); +} + +/* A copy keeps the value it was taken from when the control is set again. */ +static void avd_ctrl_copy_test(struct kunit *test) +{ + struct v4l2_ctrl_vp9_frame *copy; + struct v4l2_ctrl *ctrl; + struct avd_ctx *ctx; + + ctrl = avd_test_ctrl_init(test, &ctx); + copy = kunit_kzalloc(test, sizeof(*copy) + 1, GFP_KERNEL); + KUNIT_ASSERT_NOT_NULL(test, copy); + + KUNIT_ASSERT_EQ(test, avd_test_set_vp9_width(ctrl, 64), 0); + KUNIT_ASSERT_EQ(test, avd_ctrl_copy(ctx, V4L2_CID_STATELESS_VP9_FRAME, + copy, sizeof(*copy)), 0); + KUNIT_EXPECT_MEMEQ(test, copy, ctrl->p_cur.p, sizeof(*copy)); + KUNIT_EXPECT_TRUE(test, avd_vp9_frame_fits(copy, 64, 64)); + + KUNIT_ASSERT_EQ(test, avd_test_set_vp9_width(ctrl, 4096), 0); + KUNIT_EXPECT_FALSE(test, avd_vp9_frame_fits(ctrl->p_cur.p_vp9_frame, + 64, 64)); + KUNIT_EXPECT_EQ(test, copy->frame_width_minus_1, 63); + KUNIT_EXPECT_EQ(test, copy->frame_height_minus_1, 63); + KUNIT_EXPECT_TRUE(test, avd_vp9_frame_fits(copy, 64, 64)); + + /* Only a destination of the control's own size is filled. */ + memset(copy, 0, sizeof(*copy)); + KUNIT_EXPECT_EQ(test, avd_ctrl_copy(ctx, V4L2_CID_STATELESS_VP9_FRAME, + copy, sizeof(*copy) - 1), -EINVAL); + KUNIT_EXPECT_EQ(test, avd_ctrl_copy(ctx, V4L2_CID_STATELESS_VP9_FRAME, + copy, sizeof(*copy) + 1), -EINVAL); + KUNIT_EXPECT_EQ(test, copy->frame_width_minus_1, 0); + /* So is a control that the handler does not have. */ + KUNIT_EXPECT_EQ(test, + avd_ctrl_copy(ctx, V4L2_CID_STATELESS_VP9_COMPRESSED_HDR, + copy, sizeof(*copy)), -EINVAL); + + v4l2_ctrl_handler_free(&ctx->ctrl_hdl); +} + +struct avd_test_copy_work { + struct work_struct work; + struct completion done; + struct v4l2_ctrl *ctrl; + struct v4l2_ctrl_vp9_frame copy; + int ret; +}; + +static void avd_test_copy_work_fn(struct work_struct *work) +{ + struct avd_test_copy_work *w = + container_of(work, struct avd_test_copy_work, work); + + w->ret = avd_ctrl_copy_value(w->ctrl, &w->copy, sizeof(w->copy)); + complete(&w->done); +} + +/* The copy waits for whoever holds the control lock and sees its whole update. */ +static void avd_ctrl_copy_locked_test(struct kunit *test) +{ + struct avd_test_copy_work *w; + struct v4l2_ctrl *ctrl; + struct avd_ctx *ctx; + + ctrl = avd_test_ctrl_init(test, &ctx); + w = kunit_kzalloc(test, sizeof(*w), GFP_KERNEL); + KUNIT_ASSERT_NOT_NULL(test, w); + w->ctrl = ctrl; + w->ret = -EINPROGRESS; + init_completion(&w->done); + INIT_WORK(&w->work, avd_test_copy_work_fn); + + KUNIT_ASSERT_EQ(test, avd_test_set_vp9_width(ctrl, 64), 0); + + v4l2_ctrl_lock(ctrl); + ctrl->p_cur.p_vp9_frame->frame_width_minus_1 = 4095; + schedule_work(&w->work); + KUNIT_EXPECT_EQ(test, + wait_for_completion_timeout(&w->done, + msecs_to_jiffies(200)), 0); + KUNIT_EXPECT_EQ(test, w->ret, -EINPROGRESS); + ctrl->p_cur.p_vp9_frame->frame_height_minus_1 = 4095; + v4l2_ctrl_unlock(ctrl); + + flush_work(&w->work); + KUNIT_EXPECT_EQ(test, w->ret, 0); + KUNIT_EXPECT_EQ(test, w->copy.frame_width_minus_1, 4095); + KUNIT_EXPECT_EQ(test, w->copy.frame_height_minus_1, 4095); + + v4l2_ctrl_handler_free(&ctx->ctrl_hdl); +} + static struct kunit_case avd_v4l2_test_cases[] = { KUNIT_CASE(avd_try_capture_fmt_test), KUNIT_CASE(avd_s_capture_fmt_test), + KUNIT_CASE(avd_ctrl_copy_test), + KUNIT_CASE(avd_ctrl_copy_locked_test), {} }; diff --git a/drivers/media/platform/apple/avd/avd-v4l2.c b/drivers/media/platform/apple/avd/avd-v4l2.c index 3177ae15ed3fd..eafafb622295c 100644 --- a/drivers/media/platform/apple/avd/avd-v4l2.c +++ b/drivers/media/platform/apple/avd/avd-v4l2.c @@ -1116,6 +1116,40 @@ void avd_run_postamble(struct avd_ctx *ctx, struct avd_run *run) v4l2_ctrl_request_complete(src_req, &ctx->ctrl_hdl); } +/* + * Copy the current value of a fixed-size control for a run. Requests are + * applied to the controls before a run reads them, but a control can also be + * set directly at any time, which rewrites its value under the control lock + * only. A run that checks a value and then reads it again from the control + * may see two different values. Take one copy under the lock; the run checks + * and uses that copy. @size must be the size of the control's value. + */ +VISIBLE_IF_KUNIT int avd_ctrl_copy_value(struct v4l2_ctrl *ctrl, void *dst, + size_t size) +{ + int ret = -EINVAL; + + v4l2_ctrl_lock(ctrl); + if ((size_t)ctrl->elems * ctrl->elem_size == size) { + memcpy(dst, ctrl->p_cur.p, size); + ret = 0; + } + v4l2_ctrl_unlock(ctrl); + + return ret; +} +EXPORT_SYMBOL_IF_KUNIT(avd_ctrl_copy_value); + +/* v4l2_ctrl_find() takes the control lock itself, so look up before copying. */ +int avd_ctrl_copy(struct avd_ctx *ctx, u32 id, void *dst, size_t size) +{ + struct v4l2_ctrl *ctrl = v4l2_ctrl_find(&ctx->ctrl_hdl, id); + + if (!ctrl) + return -EINVAL; + return avd_ctrl_copy_value(ctrl, dst, size); +} + static int avd_add_ctrls(struct avd_ctx *ctx, const struct avd_ctrls *ctrls) { unsigned int i; diff --git a/drivers/media/platform/apple/avd/avd.h b/drivers/media/platform/apple/avd/avd.h index a9c5f03e81d52..af595381e3f47 100644 --- a/drivers/media/platform/apple/avd/avd.h +++ b/drivers/media/platform/apple/avd/avd.h @@ -376,8 +376,10 @@ void avd_job_finish(struct avd_ctx *ctx, enum vb2_buffer_state result); void avd_run_preamble(struct avd_ctx *ctx, struct avd_run *run); int avd_check_run_image_fmt(struct avd_ctx *ctx, enum avd_image_fmt image_fmt); void avd_run_postamble(struct avd_ctx *ctx, struct avd_run *run); +int avd_ctrl_copy(struct avd_ctx *ctx, u32 id, void *dst, size_t size); #if IS_ENABLED(CONFIG_KUNIT) +int avd_ctrl_copy_value(struct v4l2_ctrl *ctrl, void *dst, size_t size); bool avd_h264_find_slice_data(const u8 *data, u32 len, u32 header_len, u32 *off); bool avd_h264_slice_fits(const struct v4l2_ctrl_h264_sps *sps, size_t held, From 575874da58d5cadfdf8f8f497e1143d6f1c28455 Mon Sep 17 00:00:00 2001 From: Ryan Murray Date: Mon, 23 Nov 2026 09:55:01 +1100 Subject: [PATCH 18/26] media: apple: avd: h264: use copies of the controls for a run The run validates the SPS, PPS and slice parameters and then builds the instructions from the same controls, reading them from the controls' current values each time. A control that is set directly meanwhile changes those values, so the builders could use parameters other than the ones that were checked. Copy the decode parameters, SPS, PPS, slice parameters, scaling matrix and prediction weights with avd_ctrl_copy() once the request has been applied, into storage that belongs to the context, and point the run at the copies. Signed-off-by: Ryan Murray --- drivers/media/platform/apple/avd/avd-h264.c | 79 +++++++++++++-------- 1 file changed, 51 insertions(+), 28 deletions(-) diff --git a/drivers/media/platform/apple/avd/avd-h264.c b/drivers/media/platform/apple/avd/avd-h264.c index e936875f2c9c3..73d86270ca80c 100644 --- a/drivers/media/platform/apple/avd/avd-h264.c +++ b/drivers/media/platform/apple/avd/avd-h264.c @@ -102,6 +102,16 @@ struct avd_h264_ctx { struct avd_bank bank[AVD_H264_BANKS]; /* bank of the picture being built */ struct avd_bank *b; + + /* What the run being built took from the controls, see avd_ctrl_copy(). */ + struct { + struct v4l2_ctrl_h264_decode_params decode_params; + struct v4l2_ctrl_h264_sps sps; + struct v4l2_ctrl_h264_pps pps; + struct v4l2_ctrl_h264_slice_params slice_params; + struct v4l2_ctrl_h264_scaling_matrix scaling_matrix; + struct v4l2_ctrl_h264_pred_weights pred_weights; + } params; }; /* ffmpeg submits wrong timestamp, use first_mb_in_slice as a workaround */ @@ -824,28 +834,11 @@ static int avd_h264_start(struct avd_ctx *ctx) return ret; } -static void avd_h264_run_preamble(struct avd_ctx *ctx, struct avd_h264_run *run) +static int avd_h264_run_preamble(struct avd_ctx *ctx, struct avd_h264_run *run) { - struct v4l2_ctrl *ctrl; + struct avd_h264_ctx *h264_ctx = ctx->priv; u32 dst_len, mv_color_len; - ctrl = v4l2_ctrl_find(&ctx->ctrl_hdl, - V4L2_CID_STATELESS_H264_DECODE_PARAMS); - run->decode_params = ctrl ? ctrl->p_cur.p : NULL; - ctrl = v4l2_ctrl_find(&ctx->ctrl_hdl, V4L2_CID_STATELESS_H264_SPS); - run->sps = ctrl ? ctrl->p_cur.p : NULL; - ctrl = v4l2_ctrl_find(&ctx->ctrl_hdl, V4L2_CID_STATELESS_H264_PPS); - run->pps = ctrl ? ctrl->p_cur.p : NULL; - ctrl = v4l2_ctrl_find(&ctx->ctrl_hdl, - V4L2_CID_STATELESS_H264_SLICE_PARAMS); - run->slice_params = ctrl ? ctrl->p_cur.p : NULL; - ctrl = v4l2_ctrl_find(&ctx->ctrl_hdl, - V4L2_CID_STATELESS_H264_SCALING_MATRIX); - run->scaling_matrix = ctrl ? ctrl->p_cur.p : NULL; - ctrl = v4l2_ctrl_find(&ctx->ctrl_hdl, - V4L2_CID_STATELESS_H264_PRED_WEIGHTS); - run->pred_weights = ctrl ? ctrl->p_cur.p : NULL; - avd_run_preamble(ctx, &run->base); dst_len = run->base.bufs.dst->vb2_buf.planes[0].length; @@ -853,6 +846,43 @@ static void avd_h264_run_preamble(struct avd_ctx *ctx, struct avd_h264_run *run) mv_color_len = mv_color_size(ctx, fmt_width(ctx), fmt_height(ctx)); run->addresses.mv_color = run->base.y_out + (dst_len - mv_color_len); + + if (run->base.setup_err) + return run->base.setup_err; + + /* + * The checks in avd_h264_run() and the builders must see the same + * parameters, and the controls can be set again at any time: use + * copies. + */ + if (avd_ctrl_copy(ctx, V4L2_CID_STATELESS_H264_DECODE_PARAMS, + &h264_ctx->params.decode_params, + sizeof(h264_ctx->params.decode_params)) || + avd_ctrl_copy(ctx, V4L2_CID_STATELESS_H264_SPS, + &h264_ctx->params.sps, + sizeof(h264_ctx->params.sps)) || + avd_ctrl_copy(ctx, V4L2_CID_STATELESS_H264_PPS, + &h264_ctx->params.pps, + sizeof(h264_ctx->params.pps)) || + avd_ctrl_copy(ctx, V4L2_CID_STATELESS_H264_SLICE_PARAMS, + &h264_ctx->params.slice_params, + sizeof(h264_ctx->params.slice_params)) || + avd_ctrl_copy(ctx, V4L2_CID_STATELESS_H264_SCALING_MATRIX, + &h264_ctx->params.scaling_matrix, + sizeof(h264_ctx->params.scaling_matrix)) || + avd_ctrl_copy(ctx, V4L2_CID_STATELESS_H264_PRED_WEIGHTS, + &h264_ctx->params.pred_weights, + sizeof(h264_ctx->params.pred_weights))) + return -EINVAL; + + run->decode_params = &h264_ctx->params.decode_params; + run->sps = &h264_ctx->params.sps; + run->pps = &h264_ctx->params.pps; + run->slice_params = &h264_ctx->params.slice_params; + run->scaling_matrix = &h264_ctx->params.scaling_matrix; + run->pred_weights = &h264_ctx->params.pred_weights; + + return 0; } static int avd_h264_realloc_slices(struct avd_ctx *ctx, size_t max_slices) @@ -1044,16 +1074,9 @@ static int avd_h264_run(struct avd_ctx *ctx) h264_ctx->b->slice_num : 0; int ret; - avd_h264_run_preamble(ctx, &run); - - if (run.base.setup_err) { - ret = run.base.setup_err; - goto err_free_jobs; - } - if (!run.sps || !run.pps || !run.slice_params || !run.decode_params) { - ret = -EINVAL; + ret = avd_h264_run_preamble(ctx, &run); + if (ret) goto err_free_jobs; - } /* Recheck controls applied from this request before building instructions. */ ret = avd_h264_validate_sps(ctx, run.sps); From 4d9cdada98788f803756cdbc6ae345939333b758 Mon Sep 17 00:00:00 2001 From: Ryan Murray Date: Mon, 23 Nov 2026 09:55:02 +1100 Subject: [PATCH 19/26] media: apple: avd: av1: use copies of the controls for a run The run checks the sequence and the frame and then builds the instructions from the same controls, reading them from the controls' current values each time. A control that is set directly meanwhile changes those values, so the builders could use parameters other than the ones that were checked. Copy the sequence, frame and film grain with avd_ctrl_copy() once the request has been applied, into storage that belongs to the context, and point the run at the copies. The tile group entries are already a private copy. Signed-off-by: Ryan Murray --- drivers/media/platform/apple/avd/avd-av1.c | 43 ++++++++++++++-------- 1 file changed, 27 insertions(+), 16 deletions(-) diff --git a/drivers/media/platform/apple/avd/avd-av1.c b/drivers/media/platform/apple/avd/avd-av1.c index 1bbf54f9b83aa..a94dbe0a491f0 100644 --- a/drivers/media/platform/apple/avd/avd-av1.c +++ b/drivers/media/platform/apple/avd/avd-av1.c @@ -273,6 +273,13 @@ struct avd_av1_stream { unsigned int nbanks; struct avd_av1_bank bank[AVD_AV1_BANKS]; struct avd_av1_bank *b; + + /* What the run being built took from the controls, see avd_ctrl_copy(). */ + struct { + struct v4l2_ctrl_av1_sequence seq; + struct v4l2_ctrl_av1_frame frame; + struct v4l2_ctrl_av1_film_grain grain; + } params; }; /* @@ -1500,6 +1507,7 @@ static int avd_av1_validate_sequence(struct avd_ctx *ctx, static int avd_av1_run_preamble(struct avd_ctx *ctx, struct avd_av1_run *run) { + struct avd_av1_stream *av1_ctx = ((struct avd_av1_ctx *)ctx->priv)->stream; struct v4l2_ctrl *ctrl; int dst_len, ret, tlb_len; @@ -1507,10 +1515,25 @@ static int avd_av1_run_preamble(struct avd_ctx *ctx, struct avd_av1_run *run) if (run->base.setup_err) return run->base.setup_err; - ctrl = v4l2_ctrl_find(&ctx->ctrl_hdl, V4L2_CID_STATELESS_AV1_SEQUENCE); - if (WARN_ON(!ctrl)) - return -EINVAL; - run->seq = ctrl->p_cur.p; + /* + * The checks below and the builders must see the same parameters, and + * the controls can be set again at any time: use copies. + */ + ret = avd_ctrl_copy(ctx, V4L2_CID_STATELESS_AV1_SEQUENCE, + &av1_ctx->params.seq, sizeof(av1_ctx->params.seq)); + if (!ret) + ret = avd_ctrl_copy(ctx, V4L2_CID_STATELESS_AV1_FRAME, + &av1_ctx->params.frame, + sizeof(av1_ctx->params.frame)); + if (!ret) + ret = avd_ctrl_copy(ctx, V4L2_CID_STATELESS_AV1_FILM_GRAIN, + &av1_ctx->params.grain, + sizeof(av1_ctx->params.grain)); + if (WARN_ON(ret)) + return ret; + run->seq = &av1_ctx->params.seq; + run->frame = &av1_ctx->params.frame; + run->grain = &av1_ctx->params.grain; /* The sequence may have been set, unchecked, while another codec was selected. */ ret = avd_av1_validate_sequence(ctx, run->seq); @@ -1520,11 +1543,6 @@ static int avd_av1_run_preamble(struct avd_ctx *ctx, struct avd_av1_run *run) if (ret) return ret; - ctrl = v4l2_ctrl_find(&ctx->ctrl_hdl, V4L2_CID_STATELESS_AV1_FRAME); - if (WARN_ON(!ctrl)) - return -EINVAL; - run->frame = ctrl->p_cur.p; - /* The map, CDFs and color data sit at the buffer's end, sized by the frame. */ if (run->frame->frame_width_minus_1 + 1 > ctx->decoded_fmt.fmt.pix_mp.width || run->frame->frame_height_minus_1 + 1 > ctx->decoded_fmt.fmt.pix_mp.height) @@ -1550,7 +1568,6 @@ static int avd_av1_run_preamble(struct avd_ctx *ctx, struct avd_av1_run *run) run->tile_group = avd_av1_copy_tile_group(ctrl, &run->tile_group_entries); if (!run->tile_group) return -ENOMEM; - /* The frame control can be set meanwhile too: use one tile grid throughout. */ run->tile_rows = run->frame->tile_info.tile_rows; run->tile_cols = run->frame->tile_info.tile_cols; ret = avd_av1_check_tiles(ctx->dev->dev, run->tile_rows, run->tile_cols, @@ -1559,12 +1576,6 @@ static int avd_av1_run_preamble(struct avd_ctx *ctx, struct avd_av1_run *run) if (ret) return ret; - ctrl = v4l2_ctrl_find(&ctx->ctrl_hdl, - V4L2_CID_STATELESS_AV1_FILM_GRAIN); - if (WARN_ON(!ctrl)) - return -EINVAL; - run->grain = ctrl->p_cur.p; - dst_len = run->base.bufs.dst->vb2_buf.planes[0].length; tlb_len = avd_color_size(run->frame->frame_width_minus_1 + 1, run->frame->frame_height_minus_1 + 1); From 30b1cc4784241734f2aadab71718b538c3cc984f Mon Sep 17 00:00:00 2001 From: Ryan Murray Date: Mon, 23 Nov 2026 09:55:45 +1100 Subject: [PATCH 20/26] media: apple: avd: av1: bound the upscaled width The frame's upscaled_width is written to the frame header and divides the superres scale factors, but nothing checked it. Only the frame width and height were compared with the CAPTURE format. Superres only widens a frame, from frame_width to upscaled_width, and without it the two are equal. Require, on the run's copy of the frame, that upscaled_width is at least the frame width and at most the CAPTURE width, and that it equals the frame width when superres is off. Move the existing frame size check into the same helper and compare the minus-one fields directly, so that the largest value cannot wrap to a size of zero. With superres the denominator is also written to the header less one; the stream codes it as 9 to 16 and the control core only bounds it from above, so require at least 9. Add KUnit cases for the helper. Signed-off-by: Ryan Murray --- .../media/platform/apple/avd/avd-av1-test.c | 57 ++++++++++++++++++- drivers/media/platform/apple/avd/avd-av1.c | 46 +++++++++++++-- drivers/media/platform/apple/avd/avd.h | 2 + 3 files changed, 100 insertions(+), 5 deletions(-) diff --git a/drivers/media/platform/apple/avd/avd-av1-test.c b/drivers/media/platform/apple/avd/avd-av1-test.c index 34768e5877ff8..feb87609ba937 100644 --- a/drivers/media/platform/apple/avd/avd-av1-test.c +++ b/drivers/media/platform/apple/avd/avd-av1-test.c @@ -1,6 +1,7 @@ // SPDX-License-Identifier: GPL-2.0 /* - * KUnit tests for the AV1 tile checks of the Apple video decoder. + * KUnit tests for the AV1 tile and frame size checks of the Apple video + * decoder. */ #include @@ -171,12 +172,66 @@ static void avd_av1_copy_tile_group_test(struct kunit *test) v4l2_ctrl_handler_free(hdl); } +struct avd_av1_size_case { + const char *desc; + u32 width; + u32 height; + u32 upscaled_width; + bool superres; + int ret; + u8 denom; +}; + +/* Frames for 1920x1088 CAPTURE buffers. */ +static const struct avd_av1_size_case avd_av1_size_cases[] = { + { "frame of the buffer size", 1920, 1088, 1920, false, 0 }, + { "smaller frame", 1280, 720, 1280, false, 0 }, + { "one sample", 1, 1, 1, false, 0 }, + { "superres to the buffer width", 960, 1080, 1920, true, 0, 16 }, + { "superres to a smaller width", 1000, 1080, 1280, true, 0, 16 }, + { "superres with equal widths", 1920, 1080, 1920, true, 0, 16 }, + { "frame wider than the buffer", 1921, 1080, 1921, false, -EINVAL }, + { "frame higher than the buffer", 1920, 1089, 1920, false, -EINVAL }, + { "frame width wraps", 0, 1080, 1920, false, -EINVAL }, + { "frame height wraps", 1920, 0, 1920, false, -EINVAL }, + { "no upscaled width", 1920, 1080, 0, false, -EINVAL }, + { "no upscaled width, superres", 1920, 1080, 0, true, -EINVAL, 16 }, + { "upscaled width below the frame's", 1920, 1080, 1919, true, -EINVAL, 16 }, + { "upscaled width past the buffer", 960, 1080, 1921, true, -EINVAL, 16 }, + { "huge upscaled width", 960, 1080, U32_MAX, true, -EINVAL, 16 }, + { "upscaled width without superres", 960, 1080, 1920, false, -EINVAL }, + { "superres, smallest denominator", 1072, 1080, 1920, true, 0, 9 }, + { "superres, no denominator", 960, 1080, 1920, true, -EINVAL, 0 }, + { "superres, denominator below 9", 960, 1080, 1920, true, -EINVAL, 8 }, +}; + +KUNIT_ARRAY_PARAM_DESC(avd_av1_size, avd_av1_size_cases, desc); + +static void avd_av1_frame_size_test(struct kunit *test) +{ + const struct avd_av1_size_case *c = test->param_value; + struct v4l2_ctrl_av1_frame *frame; + + frame = kunit_kzalloc(test, sizeof(*frame), GFP_KERNEL); + KUNIT_ASSERT_NOT_NULL(test, frame); + frame->frame_width_minus_1 = c->width - 1; + frame->frame_height_minus_1 = c->height - 1; + frame->upscaled_width = c->upscaled_width; + frame->superres_denom = c->denom; + if (c->superres) + frame->flags |= V4L2_AV1_FRAME_FLAG_USE_SUPERRES; + + KUNIT_EXPECT_EQ(test, avd_av1_check_frame_size(frame, 1920, 1088), + c->ret); +} + static struct kunit_case avd_av1_test_cases[] = { KUNIT_CASE(avd_av1_tiles_valid_test), KUNIT_CASE(avd_av1_tiles_count_test), KUNIT_CASE(avd_av1_tiles_extent_test), KUNIT_CASE_PARAM(avd_av1_tiles_rowcol_test, avd_av1_rowcol_gen_params), KUNIT_CASE(avd_av1_copy_tile_group_test), + KUNIT_CASE_PARAM(avd_av1_frame_size_test, avd_av1_size_gen_params), {} }; diff --git a/drivers/media/platform/apple/avd/avd-av1.c b/drivers/media/platform/apple/avd/avd-av1.c index a94dbe0a491f0..106024cfd806a 100644 --- a/drivers/media/platform/apple/avd/avd-av1.c +++ b/drivers/media/platform/apple/avd/avd-av1.c @@ -1505,6 +1505,43 @@ static int avd_av1_validate_sequence(struct avd_ctx *ctx, return 0; } +/* + * The map, CDFs and color data sit at the end of the CAPTURE buffer, sized by + * the frame, so the frame must fit the CAPTURE format. So must the width it + * is upscaled to, which goes into the header as it is and divides the scale + * factors: superres only widens the frame, from frame_width to upscaled_width + * (AV1 7.16), and without it the two are equal (AV1 5.9.8, superres_params()). + */ +VISIBLE_IF_KUNIT int +avd_av1_check_frame_size(const struct v4l2_ctrl_av1_frame *frame, u32 width, + u32 height) +{ + u32 frame_width; + + if (frame->frame_width_minus_1 >= width || + frame->frame_height_minus_1 >= height) + return -EINVAL; + + frame_width = frame->frame_width_minus_1 + 1; + if (frame->upscaled_width < frame_width || + frame->upscaled_width > width) + return -EINVAL; + if (!(frame->flags & V4L2_AV1_FRAME_FLAG_USE_SUPERRES) && + frame->upscaled_width != frame_width) + return -EINVAL; + /* + * With superres the denominator goes into the header less one; the + * stream codes it as 9 to 16 (AV1 5.9.8), the control core only + * bounds it from above. + */ + if ((frame->flags & V4L2_AV1_FRAME_FLAG_USE_SUPERRES) && + frame->superres_denom < 9) + return -EINVAL; + + return 0; +} +EXPORT_SYMBOL_IF_KUNIT(avd_av1_check_frame_size); + static int avd_av1_run_preamble(struct avd_ctx *ctx, struct avd_av1_run *run) { struct avd_av1_stream *av1_ctx = ((struct avd_av1_ctx *)ctx->priv)->stream; @@ -1543,10 +1580,11 @@ static int avd_av1_run_preamble(struct avd_ctx *ctx, struct avd_av1_run *run) if (ret) return ret; - /* The map, CDFs and color data sit at the buffer's end, sized by the frame. */ - if (run->frame->frame_width_minus_1 + 1 > ctx->decoded_fmt.fmt.pix_mp.width || - run->frame->frame_height_minus_1 + 1 > ctx->decoded_fmt.fmt.pix_mp.height) - return -EINVAL; + ret = avd_av1_check_frame_size(run->frame, + ctx->decoded_fmt.fmt.pix_mp.width, + ctx->decoded_fmt.fmt.pix_mp.height); + if (ret) + return ret; /* * The CDF and segmentation map loads index the reference arrays with diff --git a/drivers/media/platform/apple/avd/avd.h b/drivers/media/platform/apple/avd/avd.h index af595381e3f47..3f0e2b0eaa517 100644 --- a/drivers/media/platform/apple/avd/avd.h +++ b/drivers/media/platform/apple/avd/avd.h @@ -404,6 +404,8 @@ int avd_av1_check_tiles(struct device *dev, unsigned int tile_rows, unsigned int entries, size_t payload); struct v4l2_ctrl_av1_tile_group_entry * avd_av1_copy_tile_group(struct v4l2_ctrl *ctrl, unsigned int *entries); +int avd_av1_check_frame_size(const struct v4l2_ctrl_av1_frame *frame, + u32 width, u32 height); int avd_h264_validate_sps(struct avd_ctx *ctx, const struct v4l2_ctrl_h264_sps *sps); size_t avd_h264_start_slices(const struct v4l2_ctrl_h264_sps *sps); From b36ff9b1dc3d79cf698e53482cbd59876bf52c64 Mon Sep 17 00:00:00 2001 From: Ryan Murray Date: Mon, 23 Nov 2026 09:56:36 +1100 Subject: [PATCH 21/26] media: apple: avd: check the VP9 and AV1 frame size when the control is set The VP9 and AV1 frame sizes are only compared with the formats when a frame runs. Compare them when the frame control is set as well, with the checks the run uses, so that a frame that cannot be decoded is refused at the control like an H.264 SPS is. A control may be set before the formats, so only check against a format that can no longer change: the OUTPUT format once the OUTPUT queue streams for VP9, whose scratch is sized from it, and the CAPTURE format once CAPTURE buffers are allocated for AV1, whose frame must fit them. The AV1 frame control gets the driver's control ops for this. Add KUnit cases for both. Signed-off-by: Ryan Murray --- .../media/platform/apple/avd/avd-av1-test.c | 50 +++++++++++++++++++ drivers/media/platform/apple/avd/avd-av1.c | 15 ++++++ drivers/media/platform/apple/avd/avd-v4l2.c | 1 + .../media/platform/apple/avd/avd-vp9-test.c | 49 ++++++++++++++++++ drivers/media/platform/apple/avd/avd-vp9.c | 19 +++++-- 5 files changed, 131 insertions(+), 3 deletions(-) diff --git a/drivers/media/platform/apple/avd/avd-av1-test.c b/drivers/media/platform/apple/avd/avd-av1-test.c index feb87609ba937..c88815e38db47 100644 --- a/drivers/media/platform/apple/avd/avd-av1-test.c +++ b/drivers/media/platform/apple/avd/avd-av1-test.c @@ -6,6 +6,7 @@ #include #include +#include #include "avd.h" @@ -225,6 +226,54 @@ static void avd_av1_frame_size_test(struct kunit *test) c->ret); } +/* + * A frame that does not fit the CAPTURE format is refused when the control + * is set, once CAPTURE buffers are allocated and the format can no longer + * follow it. + */ +static void avd_av1_try_frame_size_test(struct kunit *test) +{ + static const struct v4l2_ctrl_config cfg = { + .id = V4L2_CID_STATELESS_AV1_FRAME, + }; + struct v4l2_m2m_ctx *m2m_ctx; + struct v4l2_ctrl_av1_frame *frame; + struct v4l2_ctrl *ctrl; + struct avd_ctx *ctx; + + ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL); + KUNIT_ASSERT_NOT_NULL(test, ctx); + m2m_ctx = kunit_kzalloc(test, sizeof(*m2m_ctx), GFP_KERNEL); + KUNIT_ASSERT_NOT_NULL(test, m2m_ctx); + ctx->fh.m2m_ctx = m2m_ctx; + ctx->decoded_fmt.fmt.pix_mp.width = 1920; + ctx->decoded_fmt.fmt.pix_mp.height = 1088; + + v4l2_ctrl_handler_init(&ctx->ctrl_hdl, 1); + ctrl = v4l2_ctrl_new_custom(&ctx->ctrl_hdl, &cfg, NULL); + KUNIT_ASSERT_EQ(test, ctx->ctrl_hdl.error, 0); + KUNIT_ASSERT_NOT_NULL(test, ctrl); + frame = ctrl->p_new.p_av1_frame; + + /* No buffers: the format may still be set to the stream's size. */ + frame->frame_width_minus_1 = 3839; + frame->frame_height_minus_1 = 2159; + frame->upscaled_width = 3840; + KUNIT_EXPECT_EQ(test, avd_av1_fmt_ops.try_ctrl(ctx, ctrl), 0); + + m2m_ctx->cap_q_ctx.q.is_busy = 1; + KUNIT_EXPECT_EQ(test, avd_av1_fmt_ops.try_ctrl(ctx, ctrl), -EINVAL); + frame->frame_width_minus_1 = 1919; + frame->frame_height_minus_1 = 1079; + KUNIT_EXPECT_EQ(test, avd_av1_fmt_ops.try_ctrl(ctx, ctrl), -EINVAL); + frame->upscaled_width = 1920; + KUNIT_EXPECT_EQ(test, avd_av1_fmt_ops.try_ctrl(ctx, ctrl), 0); + frame->upscaled_width = 0; + KUNIT_EXPECT_EQ(test, avd_av1_fmt_ops.try_ctrl(ctx, ctrl), -EINVAL); + + v4l2_ctrl_handler_free(&ctx->ctrl_hdl); +} + static struct kunit_case avd_av1_test_cases[] = { KUNIT_CASE(avd_av1_tiles_valid_test), KUNIT_CASE(avd_av1_tiles_count_test), @@ -232,6 +281,7 @@ static struct kunit_case avd_av1_test_cases[] = { KUNIT_CASE_PARAM(avd_av1_tiles_rowcol_test, avd_av1_rowcol_gen_params), KUNIT_CASE(avd_av1_copy_tile_group_test), KUNIT_CASE_PARAM(avd_av1_frame_size_test, avd_av1_size_gen_params), + KUNIT_CASE(avd_av1_try_frame_size_test), {} }; diff --git a/drivers/media/platform/apple/avd/avd-av1.c b/drivers/media/platform/apple/avd/avd-av1.c index 106024cfd806a..04d55dd7b0aad 100644 --- a/drivers/media/platform/apple/avd/avd-av1.c +++ b/drivers/media/platform/apple/avd/avd-av1.c @@ -2036,10 +2036,25 @@ static void avd_av1_adjust_decoded_fmt(struct avd_ctx *ctx, static int avd_av1_try_ctrl(struct avd_ctx *ctx, struct v4l2_ctrl *ctrl) { + struct vb2_queue *vq; + if (ctrl->id == V4L2_CID_STATELESS_AV1_SEQUENCE) return avd_av1_validate_sequence(ctx, ctrl->p_new.p_av1_sequence); /* width should also be > 8 */ + if (ctrl->id != V4L2_CID_STATELESS_AV1_FRAME) + return 0; + + /* + * The CAPTURE format is fixed while its buffers are allocated: a frame + * that does not fit them cannot be decoded, as the run finds too. + * Before that the format may still be set to the stream's. + */ + vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE); + if (vb2_is_busy(vq)) + return avd_av1_check_frame_size(ctrl->p_new.p_av1_frame, + ctx->decoded_fmt.fmt.pix_mp.width, + ctx->decoded_fmt.fmt.pix_mp.height); return 0; } diff --git a/drivers/media/platform/apple/avd/avd-v4l2.c b/drivers/media/platform/apple/avd/avd-v4l2.c index eafafb622295c..5c010a7c9c66d 100644 --- a/drivers/media/platform/apple/avd/avd-v4l2.c +++ b/drivers/media/platform/apple/avd/avd-v4l2.c @@ -439,6 +439,7 @@ static const struct avd_ctrl_desc avd_av1_ctrl_descs[] = { }, { .cfg.id = V4L2_CID_STATELESS_AV1_FRAME, + .cfg.ops = &avd_ctrl_ops, }, { .cfg.id = V4L2_CID_STATELESS_AV1_FILM_GRAIN, diff --git a/drivers/media/platform/apple/avd/avd-vp9-test.c b/drivers/media/platform/apple/avd/avd-vp9-test.c index 4e0bcd09ed31c..7bf89193a8f40 100644 --- a/drivers/media/platform/apple/avd/avd-vp9-test.c +++ b/drivers/media/platform/apple/avd/avd-vp9-test.c @@ -4,6 +4,7 @@ */ #include +#include #include "avd.h" @@ -154,8 +155,56 @@ static void avd_vp9_frame_fits_test(struct kunit *test) KUNIT_EXPECT_FALSE(test, avd_vp9_frame_fits(&frame, 4096, 4096)); } +/* + * A frame larger than the OUTPUT format is refused when the control is set, + * once the OUTPUT queue streams and the format can no longer follow it. + */ +static void avd_vp9_try_frame_size_test(struct kunit *test) +{ + static const struct v4l2_ctrl_config cfg = { + .id = V4L2_CID_STATELESS_VP9_FRAME, + }; + struct v4l2_m2m_ctx *m2m_ctx; + struct v4l2_ctrl_vp9_frame *frame; + struct v4l2_ctrl *ctrl; + struct avd_ctx *ctx; + + ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL); + KUNIT_ASSERT_NOT_NULL(test, ctx); + m2m_ctx = kunit_kzalloc(test, sizeof(*m2m_ctx), GFP_KERNEL); + KUNIT_ASSERT_NOT_NULL(test, m2m_ctx); + ctx->fh.m2m_ctx = m2m_ctx; + ctx->coded_fmt.fmt.pix_mp.width = 1920; + ctx->coded_fmt.fmt.pix_mp.height = 1088; + + v4l2_ctrl_handler_init(&ctx->ctrl_hdl, 1); + ctrl = v4l2_ctrl_new_custom(&ctx->ctrl_hdl, &cfg, NULL); + KUNIT_ASSERT_EQ(test, ctx->ctrl_hdl.error, 0); + KUNIT_ASSERT_NOT_NULL(test, ctrl); + frame = ctrl->p_new.p_vp9_frame; + + /* Not streaming: the format may still be set to the stream's size. */ + frame->frame_width_minus_1 = 4095; + frame->frame_height_minus_1 = 2159; + KUNIT_EXPECT_EQ(test, avd_vp9_fmt_ops.try_ctrl(ctx, ctrl), 0); + + m2m_ctx->out_q_ctx.q.streaming = 1; + KUNIT_EXPECT_EQ(test, avd_vp9_fmt_ops.try_ctrl(ctx, ctrl), -EINVAL); + frame->frame_width_minus_1 = 1919; + frame->frame_height_minus_1 = 1088; + KUNIT_EXPECT_EQ(test, avd_vp9_fmt_ops.try_ctrl(ctx, ctrl), -EINVAL); + frame->frame_height_minus_1 = 1087; + KUNIT_EXPECT_EQ(test, avd_vp9_fmt_ops.try_ctrl(ctx, ctrl), 0); + frame->frame_width_minus_1 = 1279; + frame->frame_height_minus_1 = 719; + KUNIT_EXPECT_EQ(test, avd_vp9_fmt_ops.try_ctrl(ctx, ctrl), 0); + + v4l2_ctrl_handler_free(&ctx->ctrl_hdl); +} + static struct kunit_case avd_vp9_test_cases[] = { KUNIT_CASE(avd_vp9_frame_fits_test), + KUNIT_CASE(avd_vp9_try_frame_size_test), KUNIT_CASE_PARAM(avd_vp9_tiling_test, avd_vp9_tiling_gen_params), KUNIT_CASE_PARAM(avd_vp9_tiles_test, avd_vp9_tiles_gen_params), {} diff --git a/drivers/media/platform/apple/avd/avd-vp9.c b/drivers/media/platform/apple/avd/avd-vp9.c index e3590e27d9c53..695ba69d9b03f 100644 --- a/drivers/media/platform/apple/avd/avd-vp9.c +++ b/drivers/media/platform/apple/avd/avd-vp9.c @@ -1309,10 +1309,23 @@ static enum avd_image_fmt avd_vp9_get_image_fmt(struct avd_ctx *ctx, static int avd_vp9_try_ctrl(struct avd_ctx *ctx, struct v4l2_ctrl *ctrl) { - if (ctrl->id == V4L2_CID_STATELESS_VP9_FRAME) - return avd_vp9_validate_frame(ctx, ctrl->p_new.p_vp9_frame); + const struct v4l2_ctrl_vp9_frame *frame = ctrl->p_new.p_vp9_frame; + struct vb2_queue *vq; - return 0; + if (ctrl->id != V4L2_CID_STATELESS_VP9_FRAME) + return 0; + + /* + * The OUTPUT format is fixed while its queue streams and the scratch + * is sized from it: a larger frame cannot be decoded, as the run + * finds too. Before that the format may still be set to the stream's. + */ + vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE); + if (vb2_is_streaming(vq) && + !avd_vp9_frame_fits(frame, fmt_width(ctx), fmt_height(ctx))) + return -EINVAL; + + return avd_vp9_validate_frame(ctx, frame); } static unsigned int avd_vp9_pp_kicks(struct avd_ctx *ctx) From ae956871a89e6a13caef3055dba825d210514761 Mon Sep 17 00:00:00 2001 From: Ryan Murray Date: Mon, 23 Nov 2026 09:57:28 +1100 Subject: [PATCH 22/26] media: apple: avd: do not submit without the context FIFO The legacy transport programs the address of the context's instruction FIFO into the decoder for every submission. The FIFO is allocated when the OUTPUT queue starts streaming and freed when it stops, so the address is only valid in between. No path submits outside of that, but nothing in the submission says so. Refuse the submission with -EINVAL when the FIFO is not allocated, together with the other checks of the frame and before any register is written. Add a KUnit case. Signed-off-by: Ryan Murray --- .../platform/apple/avd/avd-backend-open.c | 3 ++ .../media/platform/apple/avd/avd-queue-test.c | 43 +++++++++++++++++++ 2 files changed, 46 insertions(+) diff --git a/drivers/media/platform/apple/avd/avd-backend-open.c b/drivers/media/platform/apple/avd/avd-backend-open.c index 2681eeb63b777..6f6028ad5895e 100644 --- a/drivers/media/platform/apple/avd/avd-backend-open.c +++ b/drivers/media/platform/apple/avd/avd-backend-open.c @@ -94,6 +94,9 @@ int avd_open_submit(struct avd_dev *avd, struct avd_frame *f) if (codec > AVD_CODEC_AV1 || !words || !f->prog_words || f->prog_words > f->prog->size / sizeof(u32)) return -EINVAL; + /* The context FIFO only exists while the OUTPUT queue streams. */ + if (!ctx->inst.cpu) + return -EINVAL; if (!(avd->capabilities & BIT(codec))) return -EOPNOTSUPP; for (i = 0; i < codec; i++) diff --git a/drivers/media/platform/apple/avd/avd-queue-test.c b/drivers/media/platform/apple/avd/avd-queue-test.c index 8d343b1b93399..7de5e49bdae26 100644 --- a/drivers/media/platform/apple/avd/avd-queue-test.c +++ b/drivers/media/platform/apple/avd/avd-queue-test.c @@ -207,6 +207,48 @@ static void avd_submit_order(struct kunit *test) KUNIT_EXPECT_FALSE(test, avd_slot_submit_first(pool, &pool[1])); } +/* + * The legacy transport programs the context FIFO, which only exists while + * the OUTPUT queue streams. With no codec capability the submission stops + * before it touches the hardware either way. + */ +static void avd_open_submit_needs_fifo(struct kunit *test) +{ + static const struct avd_coded_fmt_desc desc = {}; + struct avd_dev *avd; + struct avd_ctx *ctx; + struct avd_frame *f; + struct avd_buf *prog; + u32 *words; + + avd = kunit_kzalloc(test, sizeof(*avd), GFP_KERNEL); + ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL); + f = kunit_kzalloc(test, sizeof(*f), GFP_KERNEL); + prog = kunit_kzalloc(test, sizeof(*prog), GFP_KERNEL); + words = kunit_kcalloc(test, 4, sizeof(*words), GFP_KERNEL); + KUNIT_ASSERT_NOT_NULL(test, avd); + KUNIT_ASSERT_NOT_NULL(test, ctx); + KUNIT_ASSERT_NOT_NULL(test, f); + KUNIT_ASSERT_NOT_NULL(test, prog); + KUNIT_ASSERT_NOT_NULL(test, words); + + ctx->dev = avd; + ctx->coded_fmt_desc = &desc; + prog->cpu = words; + prog->size = 4 * sizeof(*words); + f->ctx = ctx; + f->prog = prog; + f->prog_words = 1; + f->cmd[0] = AVD_CODEC_H264; + + KUNIT_EXPECT_EQ(test, avd_open_backend.submit(avd, f), -EINVAL); + KUNIT_EXPECT_NULL(test, avd->compat_active); + + ctx->inst.cpu = words; + KUNIT_EXPECT_EQ(test, avd_open_backend.submit(avd, f), -EOPNOTSUPP); + KUNIT_EXPECT_NULL(test, avd->compat_active); +} + static struct kunit_case avd_queue_cases[] = { KUNIT_CASE(avd_slots_and_tags), KUNIT_CASE(avd_retirement_order), @@ -215,6 +257,7 @@ static struct kunit_case avd_queue_cases[] = { KUNIT_CASE(avd_command_builders), KUNIT_CASE(avd_dispatch_reservation), KUNIT_CASE(avd_submit_order), + KUNIT_CASE(avd_open_submit_needs_fifo), {} }; From 752c8813f2603a208ab5065db5ac3272096b32a6 Mon Sep 17 00:00:00 2001 From: Ryan Murray Date: Mon, 23 Nov 2026 09:57:28 +1100 Subject: [PATCH 23/26] media: apple: avd: hevc: test entry point offsets that exceed their slice avd_hevc_check_slices() gives the last tile of a slice the bytes that the entry point offsets leave. Cover offsets that add up to more than the slice's data while each of them, and their sum, still lies inside the payload: in a single slice, and in the first and the second of two slices. Signed-off-by: Ryan Murray --- .../media/platform/apple/avd/avd-hevc-test.c | 59 +++++++++++++++++++ 1 file changed, 59 insertions(+) diff --git a/drivers/media/platform/apple/avd/avd-hevc-test.c b/drivers/media/platform/apple/avd/avd-hevc-test.c index efb0cf13b0d8f..d6c454005e370 100644 --- a/drivers/media/platform/apple/avd/avd-hevc-test.c +++ b/drivers/media/platform/apple/avd/avd-hevc-test.c @@ -279,12 +279,71 @@ static void avd_hevc_two_slices_test(struct kunit *test) &ep, 1, 119), -EINVAL); } +/* + * The entry point offsets split a slice's data into its tiles; the last tile + * takes what the others leave. Offsets that add up to more than the slice + * holds leave it less than nothing, even when each one fits the payload. + */ +static void avd_hevc_entry_points_sum_test(struct kunit *test) +{ + struct v4l2_ctrl_hevc_decode_params *decode; + struct v4l2_ctrl_hevc_slice_params *sl; + struct v4l2_ctrl_hevc_sps sps; + struct v4l2_ctrl_hevc_pps pps = { + .flags = V4L2_HEVC_PPS_FLAG_TILES_ENABLED | + V4L2_HEVC_PPS_FLAG_UNIFORM_SPACING, + .num_tile_columns_minus1 = 3, + }; + u32 ep[6] = { 20, 20, 20, 20, 20, 20 }; + + decode = kunit_kzalloc(test, sizeof(*decode), GFP_KERNEL); + sl = kunit_kcalloc(test, 2, sizeof(*sl), GFP_KERNEL); + KUNIT_ASSERT_NOT_NULL(test, decode); + KUNIT_ASSERT_NOT_NULL(test, sl); + + avd_hevc_test_sps(&sps); + sl[0].slice_type = V4L2_HEVC_SLICE_TYPE_I; + sl[0].bit_size = 100 * 8; + sl[0].data_byte_offset = 10; + sl[0].num_entry_point_offsets = 3; + sl[1] = sl[0]; + sl[1].slice_segment_addr = 8; + + /* 10 header bytes, three tiles of 20 bytes and a last one of 30. */ + KUNIT_EXPECT_EQ(test, avd_hevc_check_slices(&sps, &pps, decode, sl, 1, + ep, 3, 100), 0); + KUNIT_EXPECT_EQ(test, avd_hevc_check_slices(&sps, &pps, decode, sl, 2, + ep, 6, 200), 0); + + /* One byte more than the 90 data bytes of the slice. */ + ep[0] = 30; + ep[1] = 30; + ep[2] = 31; + KUNIT_EXPECT_EQ(test, avd_hevc_check_slices(&sps, &pps, decode, sl, 1, + ep, 3, 100), -EINVAL); + /* + * The same in the first of two slices: every offset and their sum + * stay inside the payload, but not inside the slice. + */ + KUNIT_EXPECT_EQ(test, avd_hevc_check_slices(&sps, &pps, decode, sl, 2, + ep, 6, 200), -EINVAL); + + /* And in the second slice. */ + ep[0] = 20; + ep[1] = 20; + ep[2] = 20; + ep[5] = 51; + KUNIT_EXPECT_EQ(test, avd_hevc_check_slices(&sps, &pps, decode, sl, 2, + ep, 6, 200), -EINVAL); +} + static struct kunit_case avd_hevc_test_cases[] = { KUNIT_CASE_PARAM(avd_hevc_geometry_test, avd_hevc_geometry_gen_params), KUNIT_CASE_PARAM(avd_hevc_tiles_test, avd_hevc_tiles_gen_params), KUNIT_CASE(avd_hevc_no_tiles_test), KUNIT_CASE_PARAM(avd_hevc_slices_test, avd_hevc_slices_gen_params), KUNIT_CASE(avd_hevc_two_slices_test), + KUNIT_CASE(avd_hevc_entry_points_sum_test), {} }; From 15d9bac9d8ff38ae334705feb635f67cbd7450da Mon Sep 17 00:00:00 2001 From: Ryan Murray Date: Mon, 23 Nov 2026 17:52:25 +1100 Subject: [PATCH 24/26] media: apple: avd: tests: follow the job bound and the SPS geometry check 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 --- .../media/platform/apple/avd/tests/av1-test.py | 2 +- .../media/platform/apple/avd/tests/host_source.py | 3 ++- .../platform/apple/avd/tests/program-test.py | 15 +++++++++------ .../media/platform/apple/avd/tests/vp9-test.py | 2 +- 4 files changed, 13 insertions(+), 9 deletions(-) diff --git a/drivers/media/platform/apple/avd/tests/av1-test.py b/drivers/media/platform/apple/avd/tests/av1-test.py index b073c8929fe69..b133465c5a1b4 100644 --- a/drivers/media/platform/apple/avd/tests/av1-test.py +++ b/drivers/media/platform/apple/avd/tests/av1-test.py @@ -106,7 +106,7 @@ static void avd_run_postamble(struct avd_ctx *ctx,struct avd_run *run){(void)ctx;(void)run;} static int avd_init_job(struct avd_ctx *ctx,int codec,size_t segments){ free(ctx->job.segments);ctx->job.codec=codec;ctx->job.num=0; - ctx->job.segments=calloc(segments,sizeof(*ctx->job.segments));return ctx->job.segments?0:-ENOMEM; + ctx->job.segments=calloc(segments,sizeof(*ctx->job.segments));ctx->job.overflow=false;ctx->job.max=ctx->job.segments?segments:0;return ctx->job.segments?0:-ENOMEM; } static int avd_submit_job(struct avd_ctx *ctx); ''' diff --git a/drivers/media/platform/apple/avd/tests/host_source.py b/drivers/media/platform/apple/avd/tests/host_source.py index b473b65e3c4ed..0251d1e3b833c 100644 --- a/drivers/media/platform/apple/avd/tests/host_source.py +++ b/drivers/media/platform/apple/avd/tests/host_source.py @@ -4,7 +4,8 @@ def function(source, name): - match = re.search(r"^[A-Za-z_][\w *]*\b" + re.escape(name) + + # The return type may sit on the line before the name. + match = re.search(r"^(?:[A-Za-z_][\w *]*\n)?(?:[A-Za-z_][\w *]*\b)?" + re.escape(name) + r"\([^;{}]*?\)\n\{", source, re.M) if not match: raise ValueError(name) diff --git a/drivers/media/platform/apple/avd/tests/program-test.py b/drivers/media/platform/apple/avd/tests/program-test.py index 5502470ca6aae..8d681bda35886 100755 --- a/drivers/media/platform/apple/avd/tests/program-test.py +++ b/drivers/media/platform/apple/avd/tests/program-test.py @@ -91,7 +91,7 @@ dma_addr_t fifo_iova; u32 cmd[24], tag; unsigned long deadline; int state,status; struct avd_ctx *ctx; }; struct avd_segment { u32 instructions[AVD_MAX_INST]; size_t num; }; -struct avd_job { struct avd_segment *segments; unsigned int num, codec; }; +struct avd_job { struct avd_segment *segments; unsigned int num, codec; size_t max; bool overflow; }; struct avd_dev; struct avd_variant { unsigned int quirks, revision, fifo_slots, vp_slots[4], vp_slot_base[4]; void (*configure_stream)(struct avd_dev *,dma_addr_t,u8,u32); }; @@ -265,7 +265,7 @@ decode.flags=idx%3?0:V4L2_H264_DECODE_PARAM_FLAG_IDR_PIC; decode.dpb[0].flags=idx%3?V4L2_H264_DPB_ENTRY_FLAG_VALID:0; sl.slice_type=idx%3==0?V4L2_H264_SLICE_TYPE_I:idx%3==1?V4L2_H264_SLICE_TYPE_P:V4L2_H264_SLICE_TYPE_B; - ctx.job.segments=calloc(3,sizeof(*ctx.job.segments)); assert(ctx.job.segments); + ctx.job.segments=calloc(3,sizeof(*ctx.job.segments)); assert(ctx.job.segments); ctx.job.max=3; stream_hdr(&ctx,&run); ctx.job.num=1;sl.first_mb_in_slice=0;slice.addr=0x200800000ULL; stream_slice(&ctx,&run); size_t continuation=1+ctx.job.segments[0].num+ctx.job.segments[1].num; @@ -425,8 +425,9 @@ def h264_prelude(): static size_t avd_h264_max_slices(const struct v4l2_ctrl_h264_sps *s) { (void)s;return 20; } static int avd_h264_realloc_slices(struct avd_ctx *c,size_t n) { (void)c;(void)n;abort(); } static int avd_init_job(struct avd_ctx *c,unsigned int codec,size_t n) { - free(c->job.segments);c->job.codec=codec;c->job.num=0; - c->job.segments=calloc(n,sizeof(*c->job.segments));return c->job.segments?0:-ENOMEM; + free(c->job.segments);c->job.codec=codec;c->job.num=0;c->job.overflow=false; + c->job.segments=calloc(n,sizeof(*c->job.segments));c->job.max=c->job.segments?n:0; + return c->job.segments?0:-ENOMEM; } static void avd_job_finish(struct avd_ctx *c,enum vb2_buffer_state s) { (void)c;assert(s==VB2_BUF_STATE_DONE);held_finishes++; } static int avd_submit_job(struct avd_ctx *c) { submissions++;return avd_open_build_program(c); } @@ -457,6 +458,7 @@ def h264_prelude(): } /* Applied SPS controls reject unequal depths for both codecs. */ struct v4l2_ctrl_hevc_sps hevc={.chroma_format_idc=1, + .log2_min_luma_coding_block_size_minus3=1,.log2_diff_max_min_luma_coding_block_size=2, .pic_width_in_luma_samples=1920,.pic_height_in_luma_samples=1088}; ctx.coded_fmt.fmt.pix_mp.width=1920;ctx.coded_fmt.fmt.pix_mp.height=1088; for(unsigned int luma=0;luma<5;luma++) for(unsigned int chroma=0;chroma<5;chroma++) { @@ -475,7 +477,7 @@ def h264_prelude(): assert(!avd_h264_run(&ctx));assert(submissions==1&&hc.bank[0].submitted&&dst[0].bank==1); /* Compare the complete header to one built with the emitted VALID count. */ struct avd_segment expected={0};struct avd_job saved=ctx.job; - ctx.job=(struct avd_job){.segments=&expected};fixture_run.num_valid=2;fixture_run.cur_poc=40; + ctx.job=(struct avd_job){.segments=&expected,.max=1};fixture_run.num_valid=2;fixture_run.cur_poc=40; stream_hdr(&ctx,&fixture_run); assert(!memcmp((u32 *)programs[0].cpu+1,expected.instructions,expected.num*sizeof(u32))); ctx.job=saved; @@ -546,6 +548,7 @@ def prepare_regressions(out): 'avd_h264_abort_picture', 'avd_h264_refs_valid', 'avd_h264_done'): code += function(source, name) code += function((ROOT / 'avd-backend-open.c').read_text(), 'avd_open_build_program') + code += function((ROOT / 'avd-hevc.c').read_text(), 'avd_hevc_check_sps_geometry') code += function((ROOT / 'avd-hevc.c').read_text(), 'avd_hevc_validate_sps') code += RUN_FIXTURE + function(source, 'avd_h264_run') + RUN_MAIN dest = out / 'prepare.c' @@ -568,7 +571,7 @@ def prepare_regressions(out): struct avd_decoded_buffer destination = {0}; struct avd_segment segments[2]; struct avd_ctx ctx_storage = { .priv = &hc, .dev = &dev, - .fh.m2m_ctx = &m2m, .job.segments = segments, .decomp = true }; + .fh.m2m_ctx = &m2m, .job.segments = segments, .job.max = 2, .decomp = true }; struct avd_ctx *ctx = &ctx_storage; struct v4l2_ctrl_h264_decode_params dec = {0}; struct v4l2_ctrl_h264_sps sps = { .chroma_format_idc = 1, diff --git a/drivers/media/platform/apple/avd/tests/vp9-test.py b/drivers/media/platform/apple/avd/tests/vp9-test.py index 083c06e095a1f..c6ccf836b4636 100644 --- a/drivers/media/platform/apple/avd/tests/vp9-test.py +++ b/drivers/media/platform/apple/avd/tests/vp9-test.py @@ -84,7 +84,7 @@ static void avd_run_postamble(struct avd_ctx *ctx,struct avd_run *run) { (void)ctx;(void)run; } static int avd_init_job(struct avd_ctx *ctx,int codec,size_t segments) { free(ctx->job.segments);ctx->job.codec=codec;ctx->job.num=0; - ctx->job.segments=calloc(segments,sizeof(*ctx->job.segments));return ctx->job.segments?0:-ENOMEM; + ctx->job.segments=calloc(segments,sizeof(*ctx->job.segments));ctx->job.overflow=false;ctx->job.max=ctx->job.segments?segments:0;return ctx->job.segments?0:-ENOMEM; } static int avd_submit_job(struct avd_ctx *ctx); ''' From 5e263083adeb6e0f710aab4a5ddc271bd4f6ae5c Mon Sep 17 00:00:00 2001 From: Ryan Murray Date: Mon, 23 Nov 2026 17:52:26 +1100 Subject: [PATCH 25/26] media: apple: avd: hevc: bound the tile counts by the PPS arrays 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 --- .../media/platform/apple/avd/avd-hevc-test.c | 29 +++++++++++++++++++ drivers/media/platform/apple/avd/avd-hevc.c | 4 +++ 2 files changed, 33 insertions(+) diff --git a/drivers/media/platform/apple/avd/avd-hevc-test.c b/drivers/media/platform/apple/avd/avd-hevc-test.c index 50db548b19e28..31aee3e1d542b 100644 --- a/drivers/media/platform/apple/avd/avd-hevc-test.c +++ b/drivers/media/platform/apple/avd/avd-hevc-test.c @@ -124,6 +124,34 @@ static void avd_hevc_tiles_test(struct kunit *test) KUNIT_EXPECT_MEMEQ(test, ids, c->ids, sizeof(ids)); } +/* + * More tile columns or rows than the PPS control has size entries for are + * refused by the check itself, even for a picture wide or tall enough. + */ +static void avd_hevc_tiles_array_test(struct kunit *test) +{ + struct v4l2_ctrl_hevc_sps sps; + struct v4l2_ctrl_hevc_pps pps = { + .flags = V4L2_HEVC_PPS_FLAG_TILES_ENABLED | + V4L2_HEVC_PPS_FLAG_UNIFORM_SPACING, + }; + + avd_hevc_test_sps(&sps); + sps.pic_width_in_luma_samples = 8192; + sps.pic_height_in_luma_samples = 4320; + + pps.num_tile_columns_minus1 = ARRAY_SIZE(pps.column_width_minus1) - 1; + pps.num_tile_rows_minus1 = ARRAY_SIZE(pps.row_height_minus1) - 1; + KUNIT_EXPECT_EQ(test, avd_hevc_check_tiles(&sps, &pps), 0); + + pps.num_tile_columns_minus1 = ARRAY_SIZE(pps.column_width_minus1); + KUNIT_EXPECT_EQ(test, avd_hevc_check_tiles(&sps, &pps), -EINVAL); + + pps.num_tile_columns_minus1 = ARRAY_SIZE(pps.column_width_minus1) - 1; + pps.num_tile_rows_minus1 = ARRAY_SIZE(pps.row_height_minus1); + KUNIT_EXPECT_EQ(test, avd_hevc_check_tiles(&sps, &pps), -EINVAL); +} + static void avd_hevc_no_tiles_test(struct kunit *test) { struct v4l2_ctrl_hevc_sps sps; @@ -141,6 +169,7 @@ static void avd_hevc_no_tiles_test(struct kunit *test) static struct kunit_case avd_hevc_test_cases[] = { KUNIT_CASE_PARAM(avd_hevc_geometry_test, avd_hevc_geometry_gen_params), KUNIT_CASE_PARAM(avd_hevc_tiles_test, avd_hevc_tiles_gen_params), + KUNIT_CASE(avd_hevc_tiles_array_test), KUNIT_CASE(avd_hevc_no_tiles_test), {} }; diff --git a/drivers/media/platform/apple/avd/avd-hevc.c b/drivers/media/platform/apple/avd/avd-hevc.c index 9c52f50586f88..d655ba8c648bf 100644 --- a/drivers/media/platform/apple/avd/avd-hevc.c +++ b/drivers/media/platform/apple/avd/avd-hevc.c @@ -1096,6 +1096,10 @@ VISIBLE_IF_KUNIT int avd_hevc_check_tiles(const struct v4l2_ctrl_hevc_sps *sps, if (!(pps->flags & V4L2_HEVC_PPS_FLAG_TILES_ENABLED)) return 0; + if (pps->num_tile_columns_minus1 >= ARRAY_SIZE(pps->column_width_minus1) || + pps->num_tile_rows_minus1 >= ARRAY_SIZE(pps->row_height_minus1)) + return -EINVAL; + avd_hevc_pic_in_ctbs(sps, &width, &height); if (pps->num_tile_columns_minus1 >= width || pps->num_tile_rows_minus1 >= height) From 305193da4ba7e79eecfb444394faf5cbc79344c6 Mon Sep 17 00:00:00 2001 From: Ryan Murray Date: Mon, 23 Nov 2026 17:57:58 +1100 Subject: [PATCH 26/26] media: apple: avd: tests: follow the run-time copies and checks The H.264 and AV1 runs now take copies of their controls, check the picture format against the buffers and, for AV1, check the frame size and copy the tile group under the control lock. Update the host fixtures to match: - avd_h264_run_preamble() returns an int; avd_h264_sps_image_fmt() is extracted, and the fixtures use the driver's image format enum. - avd_check_run_image_fmt() is stubbed; avd-fmt-test.c covers it. - The AV1 fixtures provide the stream's control copies, the control lock, kmemdup_array() and the OUTPUT buffer the preamble reads, and set upscaled_width, which must now equal the frame width without superres. - The AV1 tail test extracts the new helpers, and its frame size mutation drops the frame size check in avd_av1_check_frame_size() as a whole: the upscaled width bound alone also bounds the width. Signed-off-by: Ryan Murray --- .../platform/apple/avd/tests/av1-test.py | 13 +++++- .../platform/apple/avd/tests/program-test.py | 10 +++-- tools/asahi/avd-av1-tail-tests.py | 44 ++++++++++++++----- 3 files changed, 51 insertions(+), 16 deletions(-) diff --git a/drivers/media/platform/apple/avd/tests/av1-test.py b/drivers/media/platform/apple/avd/tests/av1-test.py index b133465c5a1b4..df9a0e4ae1afe 100644 --- a/drivers/media/platform/apple/avd/tests/av1-test.py +++ b/drivers/media/platform/apple/avd/tests/av1-test.py @@ -77,7 +77,14 @@ static unsigned long vb2_get_plane_payload(struct vb2_buffer *b,unsigned int p){(void)b;(void)p;return 1UL<<20;} static struct vb2_v4l2_buffer *current_src,*current_dst; static struct vb2_v4l2_buffer *v4l2_m2m_next_src_buf(void *m){(void)m;return current_src;} -struct v4l2_ctrl {unsigned int id; unsigned int elems; union {void *p;} p_cur;}; +struct device; +struct v4l2_ctrl {unsigned int id; unsigned int elems, elem_size; union {void *p;} p_cur;}; +static void v4l2_ctrl_lock(struct v4l2_ctrl *c){(void)c;} +static void v4l2_ctrl_unlock(struct v4l2_ctrl *c){(void)c;} +static void *kmemdup_array(const void *p,size_t n,size_t size,int f){ + (void)f;void *d=calloc(n?n:1,size);if(d)memcpy(d,p,n*size);return d;} +/* The run-time format check has its own KUnit suite (avd-fmt-test.c). */ +static int avd_check_run_image_fmt(struct avd_ctx *ctx,int f){(void)ctx;(void)f;return 0;} struct v4l2_ctrl_ref {struct v4l2_ctrl_ref *next; struct v4l2_ctrl *ctrl; bool p_req_valid; union {void *p;} p_req;}; struct v4l2_ctrl_handler {struct v4l2_ctrl_ref *ctrl_refs;}; #define list_for_each_entry(ref,head,node) for((ref)=*(head);(ref);(ref)=(ref)->next) @@ -92,12 +99,14 @@ static struct v4l2_ctrl_handler *v4l2_ctrl_request_hdl_find(void *req,void *h){(void)h;assert(req==&request_hdl);request_gets++;return &request_hdl;} static void v4l2_ctrl_request_hdl_put(struct v4l2_ctrl_handler *h){assert(h==&request_hdl);request_puts++;} static struct v4l2_ctrl *v4l2_ctrl_find(void *h,u32 id){ - (void)h;controls[0].p_cur.p=&seq;controls[1].p_cur.p=¶ms;controls[2].p_cur.p=&tile;controls[2].elems=1;controls[3].p_cur.p=&grain; + (void)h;controls[0].p_cur.p=&seq;controls[1].p_cur.p=¶ms;controls[2].p_cur.p=&tile;controls[2].elems=1;controls[2].elem_size=sizeof(tile);controls[3].p_cur.p=&grain; if(id==V4L2_CID_STATELESS_AV1_SEQUENCE)return &controls[0]; if(id==V4L2_CID_STATELESS_AV1_FRAME)return &controls[1]; if(id==V4L2_CID_STATELESS_AV1_TILE_GROUP_ENTRY)return &controls[2]; return &controls[3]; } +static int avd_ctrl_copy(struct avd_ctx *ctx,u32 id,void *dst,size_t size){ + (void)ctx;memcpy(dst,v4l2_ctrl_find(NULL,id)->p_cur.p,size);return 0;} static void avd_run_preamble(struct avd_ctx *ctx,struct avd_run *run){ (void)ctx;memset(run,0,sizeof(*run));run->bufs.src=current_src;run->bufs.dst=current_dst; run->coded_in=current_src->vb2_buf.dma;run->comp_out=current_dst->vb2_buf.dma; diff --git a/drivers/media/platform/apple/avd/tests/program-test.py b/drivers/media/platform/apple/avd/tests/program-test.py index 8d681bda35886..a0ee9b1d3b95d 100755 --- a/drivers/media/platform/apple/avd/tests/program-test.py +++ b/drivers/media/platform/apple/avd/tests/program-test.py @@ -55,8 +55,8 @@ #define AVD_MAX_INST 512 #define AVD_CODEC_AV1 3 #define AVD_MAX_FRAMES 16 -#define AVD_IMG_FMT_420_10BIT 1 -#define AVD_IMG_FMT_422_10BIT 2 +enum avd_image_fmt { AVD_IMG_FMT_ANY, AVD_IMG_FMT_420_8BIT, AVD_IMG_FMT_420_10BIT, + AVD_IMG_FMT_422_8BIT, AVD_IMG_FMT_422_10BIT }; #define AVD_FRAME_SUBMITTED 2 #define AVD_FRAME_DONE 3 #define IRQ_HANDLED 1 @@ -418,7 +418,9 @@ def h264_prelude(): static struct vb2_v4l2_buffer *v4l2_m2m_next_src_buf(void *m) { (void)m;return fixture_run.base.bufs.src; } static size_t vb2_get_plane_payload(struct vb2_buffer *b,int p) { (void)b;(void)p;return valid_data?16:2; } static void *vb2_plane_vaddr(struct vb2_buffer *b,int p) { (void)b;(void)p;static u8 data[16];return data; } -static void avd_h264_run_preamble(struct avd_ctx *ctx,struct avd_h264_run *r) { (void)ctx;*r=fixture_run; } +static int avd_h264_run_preamble(struct avd_ctx *ctx,struct avd_h264_run *r) { (void)ctx;*r=fixture_run;return 0; } +/* The run-time format check has its own KUnit suite (avd-fmt-test.c). */ +static int avd_check_run_image_fmt(struct avd_ctx *ctx,int f) { (void)ctx;(void)f;return 0; } static void avd_run_postamble(struct avd_ctx *ctx,struct avd_run *r) { (void)ctx;(void)r;postambles++; } static bool avd_h264_slice_fits(const struct v4l2_ctrl_h264_sps *s,size_t n,size_t b,u32 l,size_t z) { (void)s;(void)b;(void)l;(void)z;return n<20; } @@ -550,7 +552,7 @@ def prepare_regressions(out): code += function((ROOT / 'avd-backend-open.c').read_text(), 'avd_open_build_program') code += function((ROOT / 'avd-hevc.c').read_text(), 'avd_hevc_check_sps_geometry') code += function((ROOT / 'avd-hevc.c').read_text(), 'avd_hevc_validate_sps') - code += RUN_FIXTURE + function(source, 'avd_h264_run') + RUN_MAIN + code += RUN_FIXTURE + function(source, 'avd_h264_sps_image_fmt') + function(source, 'avd_h264_run') + RUN_MAIN dest = out / 'prepare.c' dest.write_text(code) exe = dest.with_suffix('') diff --git a/tools/asahi/avd-av1-tail-tests.py b/tools/asahi/avd-av1-tail-tests.py index 6f1d5a98b8f3f..df866290b8422 100644 --- a/tools/asahi/avd-av1-tail-tests.py +++ b/tools/asahi/avd-av1-tail-tests.py @@ -15,7 +15,8 @@ def function(source, name): - match = re.search(r'^(?:static )?(?:int|void|u64)\s+' + name + r'\(', source, re.M) + # The return type may sit on the line before the name. + match = re.search(r'^(?:static |VISIBLE_IF_KUNIT )?(?:int|void|u64|enum \w+|struct \w+ \*)\s+' + name + r'\(', source, re.M) if not match: raise ValueError(name) start = match.start() @@ -55,6 +56,10 @@ def structure(source, name): #define WARN_ON(x) (x) #define DIV_ROUND_UP(n,d) (((n)+(d)-1)/(d)) #define dev_err_ratelimited(...) ((void)0) +#define VISIBLE_IF_KUNIT +#define GFP_KERNEL 0 +struct device; +enum avd_image_fmt { AVD_IMG_FMT_ANY, AVD_IMG_FMT_420_8BIT, AVD_IMG_FMT_420_10BIT }; @ALIGNMENT@ @CDF_HEADER@ @CDF_DEFAULTS@ @@ -67,10 +72,22 @@ def structure(source, name): struct avd_buf {void *cpu; dma_addr_t addr;}; struct avd_run {struct {struct vb2_v4l2_buffer *src,*dst;} bufs; dma_addr_t y_out; int setup_err;}; @RUN_STRUCT@ +/* The preamble only uses the stream's copies of the controls. */ +struct avd_av1_stream { + struct {struct v4l2_ctrl_av1_sequence seq; struct v4l2_ctrl_av1_frame frame; struct v4l2_ctrl_av1_film_grain grain;} params; +}; @CTX_STRUCT@ -struct v4l2_ctrl {unsigned int elems; struct {void *p;} p_cur;}; +struct v4l2_ctrl {unsigned int elems, elem_size; struct {void *p;} p_cur;}; +static void v4l2_ctrl_lock(struct v4l2_ctrl *c){(void)c;} +static void v4l2_ctrl_unlock(struct v4l2_ctrl *c){(void)c;} +/* avd_av1_run() frees the run's copy; these tests only run the preamble. */ +static struct v4l2_ctrl_av1_tile_group_entry tile_copy[4]; +static void *kmemdup_array(const void *p,size_t n,size_t size,int f){(void)f;assert(n*size<=sizeof(tile_copy));memcpy(tile_copy,p,n*size);return tile_copy;} +/* The run-time format check has its own KUnit suite (avd-fmt-test.c). */ +static int avd_check_run_image_fmt(void *ctx,enum avd_image_fmt f){(void)ctx;(void)f;return 0;} +struct avd_dev {struct device *dev;}; struct v4l2_pix_format_mplane {unsigned int width,height; struct {unsigned int sizeimage;} plane_fmt[1];}; -struct avd_ctx {void *priv; int ctrl_hdl; struct {struct {struct {unsigned int width,height;} pix_mp;} fmt;} decoded_fmt;}; +struct avd_ctx {struct avd_dev *dev; void *priv; int ctrl_hdl; struct {struct {struct {unsigned int width,height;} pix_mp;} fmt;} decoded_fmt;}; static struct avd_decoded_buffer *reference; static struct v4l2_ctrl_av1_frame frame; static struct v4l2_ctrl_av1_sequence sequence={.bit_depth=8,.flags=V4L2_AV1_SEQUENCE_FLAG_SUBSAMPLING_X|V4L2_AV1_SEQUENCE_FLAG_SUBSAMPLING_Y}; @@ -83,7 +100,9 @@ def structure(source, name): static dma_addr_t vb2_dma_contig_plane_dma_addr(struct vb2_buffer *b,unsigned int p){assert(p==0);return b->dma;} static struct avd_decoded_buffer *vb2_to_avd_decoded_buf(struct vb2_buffer *b){return (void *)b;} static struct avd_decoded_buffer *avd_get_ref_buf(struct avd_ctx *ctx,struct vb2_v4l2_buffer *dst,uint64_t timestamp){(void)ctx;(void)dst;assert(timestamp==1000);lookup_calls++;return reference;} -static void avd_run_preamble(struct avd_ctx *ctx,struct avd_run *run){(void)ctx;(void)run;preamble_calls++;} +/* Like the real one, this hands the run its OUTPUT buffer. */ +static struct vb2_v4l2_buffer fixture_src; +static void avd_run_preamble(struct avd_ctx *ctx,struct avd_run *run){(void)ctx;if(!run->bufs.src)run->bufs.src=&fixture_src;preamble_calls++;} static struct v4l2_ctrl *v4l2_ctrl_find(int *h,unsigned int id){ (void)h; if(id==V4L2_CID_STATELESS_AV1_SEQUENCE)return &controls[0]; @@ -92,13 +111,17 @@ def structure(source, name): if(id==V4L2_CID_STATELESS_AV1_FILM_GRAIN)return &controls[3]; return NULL; } +static int avd_ctrl_copy(struct avd_ctx *ctx,unsigned int id,void *dst,size_t size){ + struct v4l2_ctrl *c=v4l2_ctrl_find(&ctx->ctrl_hdl,id);if(!c)return -EINVAL;memcpy(dst,c->p_cur.p,size);return 0;} ''' TESTS = r''' static size_t aligned_cdf(size_t length,size_t color){return (length&~255UL)-((color+255)&~255UL)-9216;} /* 3 bits per 4x4 block over 128x128 superblocks, 256-byte aligned. */ static size_t seg_bytes(size_t w,size_t h){return ALIGN(ALIGN(w,128)*ALIGN(h,128)/128*3,256);} -static void set_frame_ctx(struct avd_ctx *ctx){ctx->decoded_fmt.fmt.pix_mp.width=1920;ctx->decoded_fmt.fmt.pix_mp.height=1080;controls[0].p_cur.p=&sequence;controls[1].p_cur.p=&frame;controls[2].p_cur.p=&tile;controls[3].p_cur.p=&grain;} +static struct avd_dev fixture_dev; +static struct avd_av1_stream fixture_stream; +static void set_frame_ctx(struct avd_ctx *ctx){ctx->dev=&fixture_dev;((struct avd_av1_ctx *)ctx->priv)->stream=&fixture_stream;ctx->decoded_fmt.fmt.pix_mp.width=1920;ctx->decoded_fmt.fmt.pix_mp.height=1080;controls[0].p_cur.p=&sequence;controls[1].p_cur.p=&frame;controls[2].p_cur.p=&tile;controls[3].p_cur.p=&grain;} static void read_exact(const char *path,void *data,size_t n){FILE *f=fopen(path,"rb");assert(f);assert(fread(data,1,n,f)==n);assert(fgetc(f)==EOF);assert(fclose(f)==0);} static void inheritance(unsigned int residue,unsigned int mode,const char *neighbors,bool check_addresses){ const size_t ref_len=6964992+residue,dst_len=7000064+(residue?((residue+79)%256):0); @@ -112,7 +135,7 @@ def structure(source, name): avd_av1_default_coeff_probs(47,&expected);avd_av1_set_default_cdfs(&expected); if(neighbors){unsigned char observation[9728];read_exact(neighbors,observation,sizeof(observation));assert(residue==64);memcpy(src.base.vb.vb2_buf.memory+6817344,observation,sizeof(observation));memcpy(&expected,observation+192,sizeof(expected));} else {expected.intra_inter_cdf[0]^=37;expected.coeff_base_cdf[0][0][0][0]^=59;memcpy(src.base.vb.vb2_buf.memory+ref_offset,&expected,sizeof(expected));} - memset(&frame,0,sizeof(frame));frame.frame_width_minus_1=1919;frame.frame_height_minus_1=1079;frame.quantization.base_q_idx=47; + memset(&frame,0,sizeof(frame));frame.frame_width_minus_1=1919;frame.upscaled_width=1920;frame.frame_height_minus_1=1079;frame.quantization.base_q_idx=47; frame.frame_type=V4L2_AV1_INTER_FRAME;frame.primary_ref_frame=0;frame.ref_frame_idx[0]=0;frame.reference_frame_ts[0]=1000; if(mode){frame.frame_type=mode==1?V4L2_AV1_KEY_FRAME:mode==2?V4L2_AV1_INTRA_ONLY_FRAME:V4L2_AV1_INTER_FRAME;if(mode==3)frame.flags=V4L2_AV1_FRAME_FLAG_ERROR_RESILIENT_MODE;if(mode==4)frame.primary_ref_frame=7;memset(&expected,0,sizeof(expected));avd_av1_default_coeff_probs(47,&expected);avd_av1_set_default_cdfs(&expected);} controls[0].p_cur.p=&sequence;controls[1].p_cur.p=&frame;controls[2].p_cur.p=&tile;controls[3].p_cur.p=&grain; @@ -141,7 +164,7 @@ def structure(source, name): static int preamble(unsigned int w,unsigned int h,unsigned char primary,signed char idx){ struct avd_decoded_buffer dst={0};struct avd_av1_ctx av1={0};struct avd_ctx ctx={.priv=&av1};struct avd_av1_run run={0};set_frame_ctx(&ctx); dst.base.vb.vb2_buf.planes[0].length=7000064;memset(&frame,0,sizeof(frame)); - frame.frame_width_minus_1=w-1;frame.frame_height_minus_1=h-1;frame.primary_ref_frame=primary;if(primary<7)frame.ref_frame_idx[primary]=idx; + frame.frame_width_minus_1=w-1;frame.upscaled_width=w;frame.frame_height_minus_1=h-1;frame.primary_ref_frame=primary;if(primary<7)frame.ref_frame_idx[primary]=idx; run.base.bufs.dst=&dst.base.vb;run.base.y_out=0x120000000UL;return avd_av1_run_preamble(&ctx,&run); } static void guards(void){ @@ -165,7 +188,7 @@ def structure(source, name): memset(dst.base.vb.vb2_buf.memory,0xa5,len);memset(src.base.vb.vb2_buf.memory,0x3c,ref_len); for(size_t i=0;i', 'UNALIGNED_SEG_OFFSET(dst->'), 'seg-any-size': ('avd_av1_set_seg', 'DIV_ROUND_UP(ref->av1.width + 1, 8)', 'DIV_ROUND_UP(frame->frame_width_minus_1 + 1, 8)'), 'ref-index-upper': ('avd_av1_run_preamble', 'idx < 0 || idx >= V4L2_AV1_TOTAL_REFS_PER_FRAME', 'idx < 0'), - 'frame-width-unchecked': ('avd_av1_run_preamble', 'run->frame->frame_width_minus_1 + 1 > ctx->decoded_fmt.fmt.pix_mp.width ||', '0 ||'), + # The upscaled width bound also covers the frame width, so drop the frame size check as a whole. + 'frame-width-unchecked': ('avd_av1_check_frame_size', 'frame->frame_width_minus_1 >= width ||\n\t frame->frame_height_minus_1 >= height', '0'), } if args.mutation: target, old, new = mutations[args.mutation]