From 2144b43774e1e279f93fab88dec607711f7b94f5 Mon Sep 17 00:00:00 2001 From: Ryan Murray Date: Mon, 23 Nov 2026 08:14:17 +1100 Subject: [PATCH 1/7] 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 2/7] 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 3/7] 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 4/7] 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 5/7] 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 15d9bac9d8ff38ae334705feb635f67cbd7450da Mon Sep 17 00:00:00 2001 From: Ryan Murray Date: Mon, 23 Nov 2026 17:52:25 +1100 Subject: [PATCH 6/7] 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 7/7] 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)