Devicetree
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Sofus Forstreuter" <sofus.c@icloud.com>
Cc: devicetree@vger.kernel.org, robh@kernel.org, conor+dt@kernel.org,
	media-ci@linuxtv.org
Subject: Re: [PATCH v4 09/17] media: apple: avd: add hevc support
Date: Sat, 28 Nov 2026 22:46:42 +0000	[thread overview]
Message-ID: <sashiko-outbox-239119@kernel.org> (raw)
In-Reply-To: <20261128-avd-v4-9-35ad7d52c842@icloud.com>

Thank you for your contribution! Sashiko AI review found 7 potential
issues to consider:
- [Critical] media: apple: avd: tile size underflow and heap OOB in
  avd_hevc_compute_tiles()
- [High] media: apple: avd: missing SPS block size and dimension
  validation
- [High] media: apple: avd: wrong collocated MV DMA address in
  stream_slice_mv()
- [High] media: apple: avd: unvalidated slice offsets and sizes in
  stream_slices()
- [Medium] media: apple: avd: wrong tile row bound and tile tracking in
  stream_slices()
- [Medium] media: apple: avd: frame intra state derived only from slice
  0 in set_header()
- [Medium] media: apple: avd: per-frame high-order alloc in
  avd_hevc_compute_tiles()
--

commit 2e52bc9684c90021ba77b2d13dbe5c1acec46975
Author: Sofus Forstreuter <sofus.c@icloud.com>

media: apple: avd: add hevc support

This commit adds stateless V4L2 HEVC decoding support to the Apple Video
Decoder (AVD) driver.

> diff --git a/drivers/media/platform/apple/avd/avd-hevc.c b/drivers/media/platform/apple/avd/avd-hevc.c
> new file mode 100644
> index 0000000000000..fa9fc61a1c07f
> --- /dev/null
> +++ b/drivers/media/platform/apple/avd/avd-hevc.c
> @@ -0,0 +1,1327 @@

[ ... ]

> +static void set_header(struct avd_ctx *ctx, struct avd_hevc_run *run)
> +{
> +	const struct v4l2_ctrl_hevc_sps *sps = run->sps;
> +	struct avd_dev *avd = ctx->dev;
> +	struct avd_hevc_ctx *hevc_ctx = ctx->priv;
> +	u32 bytesperline;
> +	u32 width = sps->pic_width_in_luma_samples;
> +	u32 height = sps->pic_height_in_luma_samples;
> +
> +	bool is_intra = (*run->sl)[0].slice_type == V4L2_HEVC_SLICE_TYPE_I;
> +
> +	push(AVD_OP_HDR | AVD_OP_HDR_FLAG_DECOMP(ctx->decomp) |
> +		     AVD_OP_HDR_FLAG_INTRA(is_intra) | AVD_OP_HDR_CONST |
> +		     AVD_OP_HDR_FLAG_HIST(
> +			     !(avd->variant->quirks & AVD_QUIRK_NO_HISTOGRAM)),
> +	     "hdr_34_start_hdr");

[ ... ]

> +	if (!is_intra)
> +		stream_refs(ctx, run);

[Severity: Medium]
Does checking only (*run->sl)[0].slice_type in set_header() and
update_dec_buf_info() misclassify a multi-slice frame when slice 0 is an I
slice and a later slice is a P or B slice?  In that case set_header() sets
AVD_OP_HDR_FLAG_INTRA(true) and skips stream_refs(), omitting reference frame
and motion vector addresses needed by the later inter slices, and
update_dec_buf_info() marks dst->hevc.is_intra = true, which disables
temporal MVP when later frames reference this buffer.

[ ... ]

> +static void stream_slice_mv(struct avd_ctx *ctx, struct avd_hevc_run *run,
> +			    const struct v4l2_ctrl_hevc_slice_params *sl,
> +			    bool is_first)
> +{

[ ... ]

> +	if (ref_valid) {
> +		dma_addr_t mv_color_addr = ref->dma.y +
> +			(ref->base.vb.planes[0].length -
> +			 mv_color_size(fmt_width(ctx), fmt_height(ctx)));
> +		pusha(mv_color_addr, "mv_colo", 0);
> +	}
> +}

[Severity: High]
Should stream_slice_mv() push ref->dma.aux_start here, matching
dst->dma.aux_start in stream_refs()?

ref->base.vb is struct vb2_v4l2_buffer, so when a VB2_MEMORY_DMABUF capture
buffer is queued with v4l2_plane.length == 0, __prepare_dmabuf() populates
vb2_buf.planes[0].length while leaving ref->base.vb.planes[0].length at 0.
Subtracting mv_color_size() then underflows in u32 and points mv_color_addr
~4 GiB past ref->dma.y, causing an IOMMU fault or reading wrong collocated
motion vectors (which also happens whenever the buffer is page-aligned or an
Apple interchange pixel format is used).

[ ... ]

> +static void compute_tiles_non_uniform(struct avd_hevc_run *run,
> +				      u16 log2_min_cb_size, u16 width,
> +				      u16 height, s32 pic_in_cts_width,
> +				      s32 pic_in_cts_height, u16 *column_width,
> +				      u16 *row_height)
> +{
> +	const struct v4l2_ctrl_hevc_pps *pps = run->pps;
> +	unsigned int num_cols = v4l2_hevc_pps_num_tile_columns(pps);
> +	unsigned int num_rows = v4l2_hevc_pps_num_tile_rows(pps);
> +	s32 sum = 0;
> +	int i;
> +
> +	/* The last tile entry is written after the loop, so iterate one less. */
> +	for (i = 0; i < num_cols - 1; i++) {
> +		column_width[i] = pps->column_width_minus1[i] + 1;
> +		sum += column_width[i];
> +	}
> +	column_width[i] = pic_in_cts_width - sum;
> +
> +	sum = 0;
> +	for (i = 0; i < num_rows - 1; i++) {
> +		row_height[i] = pps->row_height_minus1[i] + 1;
> +		sum += row_height[i];
> +	}
> +	row_height[i] = pic_in_cts_height - sum;
> +}

[ ... ]

> +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)
> +{
> +	const struct v4l2_ctrl_hevc_pps *pps = run->pps;
> +	unsigned int rows = v4l2_hevc_pps_num_tile_rows(pps);
> +	unsigned int cols = v4l2_hevc_pps_num_tile_columns(pps);
> +	int j, i, y, x;
> +	u32 tile_idx;
> +
> +	for (j = 0, tile_idx = 0; j < rows; j++)
> +		for (i = 0; i < cols; 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;
> +}

[Severity: Critical]
Can column_width[i] and row_height[i] underflow in
compute_tiles_non_uniform() and cause out-of-bounds heap writes to tile_ids[]
in compute_tile_ids()?

Nothing checks that the sum of pps->column_width_minus1[i] + 1 is less than
pic_in_cts_width (or that the sum of pps->row_height_minus1[i] + 1 is less
than pic_in_cts_height).  When sum exceeds pic_in_cts_width or
pic_in_cts_height, assigning pic_in_cts_width - sum to a u16 wraps to a large
value up to 65535, so col_bd[cols] and row_bd[rows] exceed the picture CTB
dimensions while ctb_addr_rs_to_ts and tile_ids are only allocated with
pic_in_ctbs_size elements.

[ ... ]

> +static int stream_slices(struct avd_ctx *ctx, struct avd_hevc_run *run)
> +{

[ ... ]

> +	for (s = 0; s < run->num_slices; s++) {
> +		sl = &(*run->sl)[s];
> +		slice_segment_offset = 0;
> +
> +		/* entry_points are not needed for WPP */
> +		to = tiles_enabled ? sl->num_entry_point_offsets + 1 : 1;
> +
> +		for (i = 0; i < to; i++) {
> +			first_segment = i == 0;
> +			first_slice = s == 0;
> +
> +			if (tiles_enabled && to > 1) {
> +				if (i < sl->num_entry_point_offsets) {
> +					size = (*run->entry_point_offsets)
> +						       [entry_point_idx++];
> +					new_offset = size;
> +				} else {
> +					size = sl->bit_size / 8 -
> +					       sl->data_byte_offset -
> +					       slice_segment_offset;
> +					new_offset =
> +						size + sl->data_byte_offset;
> +				}
> +			} else {
> +				size = (sl->bit_size) / 8 -
> +				       sl->data_byte_offset;
> +				new_offset = size + sl->data_byte_offset;
> +			}

[Severity: High]
Can size underflow or exceed the source buffer payload here before being
passed to set_slice()?

If sl->data_byte_offset + slice_segment_offset > sl->bit_size / 8, size is
negative and wraps to a large u32 in set_slice(), and neither stream_slices()
nor set_slice() validates that offset + slice_segment_offset +
sl->data_byte_offset + size fits within
vb2_get_plane_payload(&run->base.bufs.src->vb2_buf, 0), allowing the hardware
DMA engine to read past the end of the OUTPUT buffer.

> +
> +			if (sl->slice_segment_addr >= pic_in_ctbs_size)
> +				return -EINVAL;
> +
> +			tile_id = tile_info->tile_ids
> +					  [tile_info->ctb_addr_rs_to_ts
> +						   [sl->slice_segment_addr]];
> +			last_tile_id =
> +				first_slice ?
> +					-1 :
> +					tile_info->tile_ids
> +						[tile_info->ctb_addr_rs_to_ts
> +							 [(*run->sl)[s - 1]
> +								  .slice_segment_addr]];
> +
> +			slice_flag = 0;
> +			if (first_slice || tile_id != last_tile_id ||
> +			    !first_segment)
> +				slice_flag |= NEW_TILE_ID;
> +
> +			if (first_segment)
> +				slice_flag |= NEW_SLICE;
> +
> +			vflip = false;
> +			hflip = false;
> +
> +			row = pos / num_cols;
> +			col = pos % num_cols;
> +
> +			if (row >= HEVC_MAX_TILE_ROWS)
> +				return -EINVAL;

[Severity: Medium]
Should row be checked against v4l2_hevc_pps_num_tile_rows(pps) rather than
HEVC_MAX_TILE_ROWS, and should last_tile_id track the tile where the previous
segment ended instead of (*run->sl)[s - 1].slice_segment_addr?

compute_bd() only populates tile_info->row_bd[0..rows], so when rows <
HEVC_MAX_TILE_ROWS and pos reaches rows * num_cols (which can also happen if
slice s - 1 spanned multiple tiles and slice s starts in the tile where slice
s - 1 ended, falsely setting NEW_TILE_ID and incrementing pos),
submit_slice_segment() reads row_bd[row + 1] == 0 and underflows
AVD_SL_DIM_END_Y(row_bd[row + 1] - 1) to 0xfff.

[ ... ]

> +static int avd_hevc_validate_sps(struct avd_ctx *ctx,
> +				 const struct v4l2_ctrl_hevc_sps *sps)
> +{
> +	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;
> +
> +	if (sps->bit_depth_chroma_minus8 != sps->bit_depth_luma_minus8)
> +		return -EINVAL;
> +
> +	if (sps->bit_depth_luma_minus8 > 2)
> +		/* not implemented */
> +		return -EINVAL;
> +
> +	return 0;
> +}

[ ... ]

> +static int avd_hevc_compute_tiles(struct avd_ctx *ctx, struct avd_hevc_run *run)
> +{
> +	const struct v4l2_ctrl_hevc_sps *sps = run->sps;
> +	const struct v4l2_ctrl_hevc_pps *pps = run->pps;
> +	bool tiles_enabled;
> +	struct avd_hevc_tile_info *tile_info = &run->tile_info;
> +	u16 log2_min_cb_size, width, height;
> +	u32 max_cu_width, pic_in_ctbs_width, pic_in_ctbs_height,
> +		pic_in_ctbs_size;
> +
> +	width = sps->pic_width_in_luma_samples;
> +	height = sps->pic_height_in_luma_samples;
> +
> +	log2_min_cb_size = sps->log2_min_luma_coding_block_size_minus3 + 3;
> +
> +	max_cu_width = 1 << (sps->log2_diff_max_min_luma_coding_block_size +
> +			     log2_min_cb_size);
> +	pic_in_ctbs_width = (width + max_cu_width - 1) / max_cu_width;
> +	pic_in_ctbs_height = (height + max_cu_width - 1) / max_cu_width;
> +	pic_in_ctbs_size = pic_in_ctbs_height * pic_in_ctbs_width;

[Severity: High]
Can unvalidated SPS dimensions or coding block sizes cause shift overflow,
division by zero, or ZERO_SIZE_PTR dereferences in avd_hevc_compute_tiles()?

avd_hevc_validate_sps() does not reject zero pic_width_in_luma_samples or
pic_height_in_luma_samples (which is also the initial zeroed state if
decoding starts before V4L2_CID_STATELESS_HEVC_SPS is set), nor does it bound
log2_min_luma_coding_block_size_minus3 and
log2_diff_max_min_luma_coding_block_size.  Because avd_hevc_compute_tiles()
runs before the !max_cu_width check in avd_hevc_alloc_scratch(), a large shift
exponent overflows 1 << (...) and can divide by zero, while zero width or
height yields pic_in_ctbs_size == 0 so kzalloc_objs() returns ZERO_SIZE_PTR,
which compute_tile_ids() dereferences.  In addition, avd_hevc_validate_sps()
is only called from avd_hevc_try_ctrl(), so shrinking coded_fmt via
VIDIOC_S_FMT after setting the SPS control leaves sps dimensions larger than
 the active format.

> +
> +	tiles_enabled = !!(pps->flags & V4L2_HEVC_PPS_FLAG_TILES_ENABLED);
> +
> +	tile_info->ctb_addr_rs_to_ts = kzalloc_objs(
> +		*tile_info->ctb_addr_rs_to_ts, pic_in_ctbs_size, GFP_KERNEL);
> +	if (!tile_info->ctb_addr_rs_to_ts)
> +		return -ENOMEM;
> +
> +	tile_info->tile_ids = kzalloc_objs(*tile_info->tile_ids,
> +					   pic_in_ctbs_size, GFP_KERNEL);
> +	if (!tile_info->tile_ids)
> +		return -ENOMEM;

[Severity: Medium]
Can we avoid allocating ctb_addr_rs_to_ts and tile_ids on every frame in
avd_hevc_compute_tiles()?

Both arrays are allocated with kzalloc_objs(..., GFP_KERNEL) on every decode
run even when tiles_enabled is false (reaching order-5 allocations for 4K
with 16x16 CTBs, and up to 4-16 MB at the 16384x16384 max resolution).  When
tiles_enabled is true, stream_slices() only uses them as
tile_info->tile_ids[tile_info->ctb_addr_rs_to_ts[sl->slice_segment_addr]],
which just evaluates the tile index (tile_y * cols + tile_x) of
sl->slice_segment_addr and can be computed directly from col_bd and row_bd
without allocating either array or scanning all CTBs in the frame.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261128-avd-v4-0-35ad7d52c842@icloud.com?part=9

  reply	other threads:[~2026-11-28 22:46 UTC|newest]

Thread overview: 33+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-28 22:02 [PATCH v4 00/17] media: apple: add avd driver Sofus Forstreuter
2026-11-28 22:02 ` [PATCH v4 01/17] dt-bindings: media: add apple,avd Sofus Forstreuter
2026-11-28 22:08   ` sashiko-bot
2026-11-28 22:02 ` [PATCH v4 02/17] media: v4l2: Add P210 pixel format Sofus Forstreuter
2026-11-28 22:02 ` [PATCH v4 03/17] media: v4l2: Add Apple interchange pixel formats Sofus Forstreuter
2026-11-28 22:02 ` [PATCH v4 04/17] media: v4l2-ctrls: validate AV1 context_update_tile_id Sofus Forstreuter
2026-11-28 22:39   ` sashiko-bot
2026-11-28 22:02 ` [PATCH v4 05/17] media: v4l2-ctrls: validate vp9 tile_rows_log2 Sofus Forstreuter
2026-11-28 22:08   ` sashiko-bot
2026-11-28 22:02 ` [PATCH v4 06/17] media: apple: add avd driver Sofus Forstreuter
2026-11-28 22:42   ` sashiko-bot
2026-11-28 22:02 ` [PATCH v4 07/17] media: apple: avd: add h264 support Sofus Forstreuter
2026-11-28 22:43   ` sashiko-bot
2026-11-28 22:02 ` [PATCH v4 08/17] media: apple: avd: add vp9 support Sofus Forstreuter
2026-11-28 22:49   ` sashiko-bot
2026-11-28 22:02 ` [PATCH v4 09/17] media: apple: avd: add hevc support Sofus Forstreuter
2026-11-28 22:46   ` sashiko-bot [this message]
2026-11-28 22:02 ` [PATCH v4 10/17] media: apple: avd: add av1 support Sofus Forstreuter
2026-11-28 22:39   ` sashiko-bot
2026-11-28 22:02 ` [PATCH v4 11/17] arm64: dts: apple: t8103: add avd nodes Sofus Forstreuter
2026-11-28 22:36   ` sashiko-bot
2026-11-28 22:02 ` [PATCH v4 12/17] arm64: dts: apple: t8112: " Sofus Forstreuter
2026-11-28 22:21   ` sashiko-bot
2026-11-28 22:02 ` [PATCH v4 13/17] arm64: dts: apple: t8122: " Sofus Forstreuter
2026-11-28 22:37   ` sashiko-bot
2026-11-28 22:02 ` [PATCH v4 14/17] arm64: dts: apple: t600x: " Sofus Forstreuter
2026-11-28 22:35   ` sashiko-bot
2026-11-28 22:02 ` [PATCH v4 15/17] arm64: dts: apple: t602x: " Sofus Forstreuter
2026-11-28 22:20   ` sashiko-bot
2026-11-28 22:02 ` [PATCH v4 16/17] arm64: dts: apple: t6030: " Sofus Forstreuter
2026-11-28 22:42   ` sashiko-bot
2026-11-28 22:02 ` [PATCH v4 17/17] arm64: dts: apple: t6031: " Sofus Forstreuter
2026-11-28 22:42   ` sashiko-bot

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=sashiko-outbox-239119@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=conor+dt@kernel.org \
    --cc=devicetree@vger.kernel.org \
    --cc=media-ci@linuxtv.org \
    --cc=robh@kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=sofus.c@icloud.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox