From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795906003; b=b971f+P+vEZOM3Uc4GvwVq8bCqjiinGTsIBHPKt2QV13klvztOxYaBPUyywW67lLQQ/9 f64X7BEK4j7UDp9iU5uR1MnxzfPx5oj8XJA2PnHQ8rA4V/lJTQOXu6HwgK4tbuFHNfbIs pmaP6fTpLjZ7GiOCXMXSOnbX8+tB1ztIF1CN9VTAAEvqxGPgCFIVkKPcAqHZY/LFQo5h4 onJPZhYzS2BEJVZcyXAjlH67KhKiA+ZPywquUCk0NTmXSXz93xw8l27VtJBZhqV3P3DMY PFS6TcUD7DxqR6vErCH086RgWj5IP9rDPeDHJYPDp7IYOJ9bggKi7Ssgkqj0wcv0vAA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795906003; h=Received:DKIM-Signature:Message-ID:From:Subject:Reply-To:To:Cc: In-Reply-To:References:Content-Type:Content-Transfer-Encoding:Date; bh=Nq+IEwrc0baG4mIsoa7naF9cSD+Eexm/EjnnbeKhWHo=; b=DrRDngqmqMHE6pK57fnzUWgjxvKLF6Wl+8OuuNYbOQ+LO06c+TE0eleT9cMYE6Ay03oP Vd4XjnVxbgcQ+sNAnVeSv+jW8XdITJ/DfJd4k70lYY1ctbH6MGsConsgtcs8PdlJYB/7Q 4IlZ5TN6Jz21Un5z3UNj6xQB+jqUHvYtPTJmOyO6TD26FzuEmRTW8D6qKv1m1+mtcp8ap 4oldQ9P39ZheXWJfnU674bIvuyKNl/qT2MhxNqSvIZFMRYjQGedpQgCBzpFxnDqpZEnd/ +5T/Y9iF5bPnsX4bK/ILOT2lLm+6LFlkbWLk5QRGvBJZ4qUxm/3gXx44IhkWl2WVvXg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b="Cd8F/JK4"; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8F5F5606A31C for ; Sat, 28 Nov 2026 22:46:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=Cd8F/JK4 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 9CEAA1F00897; Sat, 28 Nov 2026 22:46:42 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1795906002; bh=Nq+IEwrc0baG4mIsoa7naF9cSD+Eexm/EjnnbeKhWHo=; h=From:Subject:Reply-To:To:Cc:In-Reply-To:References:Date; b=Cd8F/JK47ph417T3tEp0v2oGwht8NPS4CNu7yXF2VBa6Yc+j7lR5Eldd1itIFKa7c uYZB09yEaWLZdxxeNBA1jBlttB4U9FYALd5bv7+WKwiwZwe4VpEp/Q1e+CF6Je1MML TWWFDcna9AuVLb9SgOQh2ABleUgOg+HjBbrg2SJCNtVqa2oWlpSfM+JtahehImQ2Zo bpICk6b91xQgLwQhm2glM9SmjtWCcIydSMrZ/YM0nX/pG6kxI+9sq5/X+F4OTzsxun s2vq8RPsBzwK7UktKz2P0ejcG/CzCn/+DLHXh6YCmMuVUNcb5Zz6RWyhzUwSCLzPAk xXXz75zuhYhnw== Message-ID: From: sashiko-bot@kernel.org Subject: Re: [PATCH v4 09/17] media: apple: avd: add hevc support Reply-To: sashiko-reviews@lists.linux.dev To: "Sofus Forstreuter" Cc: devicetree@vger.kernel.org, robh@kernel.org, conor+dt@kernel.org, media-ci@linuxtv.org In-Reply-To: <20261128-avd-v4-9-35ad7d52c842@icloud.com> References: <20261128-avd-v4-0-35ad7d52c842@icloud.com> <20261128-avd-v4-9-35ad7d52c842@icloud.com> Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Date: Sat, 28 Nov 2026 22:46:42 +0000 X-Mailing-List: devicetree@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list 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 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 =3D run->sps; > + struct avd_dev *avd =3D ctx->dev; > + struct avd_hevc_ctx *hevc_ctx =3D ctx->priv; > + u32 bytesperline; > + u32 width =3D sps->pic_width_in_luma_samples; > + u32 height =3D sps->pic_height_in_luma_samples; > + > + bool is_intra =3D (*run->sl)[0].slice_type =3D=3D 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 fra= me and motion vector addresses needed by the later inter slices, and update_dec_buf_info() marks dst->hevc.is_intra =3D true, which disables temporal MVP when later frames reference this buffer. [ ... ] > +static void stream_slice_mv(struct avd_ctx *ctx, struct avd_hevc_run *ru= n, > + const struct v4l2_ctrl_hevc_slice_params *sl, > + bool is_first) > +{ [ ... ] > + if (ref_valid) { > + dma_addr_t mv_color_addr =3D 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 =3D=3D 0, __prepare_dmabuf() popula= tes 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 =3D run->pps; > + unsigned int num_cols =3D v4l2_hevc_pps_num_tile_columns(pps); > + unsigned int num_rows =3D v4l2_hevc_pps_num_tile_rows(pps); > + s32 sum =3D 0; > + int i; > + > + /* The last tile entry is written after the loop, so iterate one less. = */ > + for (i =3D 0; i < num_cols - 1; i++) { > + column_width[i] =3D pps->column_width_minus1[i] + 1; > + sum +=3D column_width[i]; > + } > + column_width[i] =3D pic_in_cts_width - sum; > + > + sum =3D 0; > + for (i =3D 0; i < num_rows - 1; i++) { > + row_height[i] =3D pps->row_height_minus1[i] + 1; > + sum +=3D row_height[i]; > + } > + row_height[i] =3D pic_in_cts_height - sum; > +} [ ... ] > +static void compute_tile_ids(struct avd_hevc_run *run, u32 pic_in_ctbs_w= idth, > + u32 *col_bd, u32 *row_bd, u32 *ctb_addr_rs_to_ts, > + u32 *tile_ids) > +{ > + const struct v4l2_ctrl_hevc_pps *pps =3D run->pps; > + unsigned int rows =3D v4l2_hevc_pps_num_tile_rows(pps); > + unsigned int cols =3D v4l2_hevc_pps_num_tile_columns(pps); > + int j, i, y, x; > + u32 tile_idx; > + > + for (j =3D 0, tile_idx =3D 0; j < rows; j++) > + for (i =3D 0; i < cols; i++, tile_idx++) > + for (y =3D row_bd[j]; y < row_bd[j + 1]; y++) > + for (x =3D col_bd[i]; x < col_bd[i + 1]; x++) > + tile_ids[ctb_addr_rs_to_ts > + [y * pic_in_ctbs_width + > + x]] =3D 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 lar= ge 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 =3D 0; s < run->num_slices; s++) { > + sl =3D &(*run->sl)[s]; > + slice_segment_offset =3D 0; > + > + /* entry_points are not needed for WPP */ > + to =3D tiles_enabled ? sl->num_entry_point_offsets + 1 : 1; > + > + for (i =3D 0; i < to; i++) { > + first_segment =3D i =3D=3D 0; > + first_slice =3D s =3D=3D 0; > + > + if (tiles_enabled && to > 1) { > + if (i < sl->num_entry_point_offsets) { > + size =3D (*run->entry_point_offsets) > + [entry_point_idx++]; > + new_offset =3D size; > + } else { > + size =3D sl->bit_size / 8 - > + sl->data_byte_offset - > + slice_segment_offset; > + new_offset =3D > + size + sl->data_byte_offset; > + } > + } else { > + size =3D (sl->bit_size) / 8 - > + sl->data_byte_offset; > + new_offset =3D 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 hardwa= re DMA engine to read past the end of the OUTPUT buffer. > + > + if (sl->slice_segment_addr >=3D pic_in_ctbs_size) > + return -EINVAL; > + > + tile_id =3D tile_info->tile_ids > + [tile_info->ctb_addr_rs_to_ts > + [sl->slice_segment_addr]]; > + last_tile_id =3D > + first_slice ? > + -1 : > + tile_info->tile_ids > + [tile_info->ctb_addr_rs_to_ts > + [(*run->sl)[s - 1] > + .slice_segment_addr]]; > + > + slice_flag =3D 0; > + if (first_slice || tile_id !=3D last_tile_id || > + !first_segment) > + slice_flag |=3D NEW_TILE_ID; > + > + if (first_segment) > + slice_flag |=3D NEW_SLICE; > + > + vflip =3D false; > + hflip =3D false; > + > + row =3D pos / num_cols; > + col =3D pos % num_cols; > + > + if (row >=3D 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 previo= us 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 sli= ce s - 1 ended, falsely setting NEW_TILE_ID and incrementing pos), submit_slice_segment() reads row_bd[row + 1] =3D=3D 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 !=3D 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_r= un *run) > +{ > + const struct v4l2_ctrl_hevc_sps *sps =3D run->sps; > + const struct v4l2_ctrl_hevc_pps *pps =3D run->pps; > + bool tiles_enabled; > + struct avd_hevc_tile_info *tile_info =3D &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 =3D sps->pic_width_in_luma_samples; > + height =3D sps->pic_height_in_luma_samples; > + > + log2_min_cb_size =3D sps->log2_min_luma_coding_block_size_minus3 + 3; > + > + max_cu_width =3D 1 << (sps->log2_diff_max_min_luma_coding_block_size + > + log2_min_cb_size); > + pic_in_ctbs_width =3D (width + max_cu_width - 1) / max_cu_width; > + pic_in_ctbs_height =3D (height + max_cu_width - 1) / max_cu_width; > + pic_in_ctbs_size =3D 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 bou= nd 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 sh= ift exponent overflows 1 << (...) and can divide by zero, while zero width or height yields pic_in_ctbs_size =3D=3D 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 =3D !!(pps->flags & V4L2_HEVC_PPS_FLAG_TILES_ENABLED); > + > + tile_info->ctb_addr_rs_to_ts =3D 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 =3D 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. --=20 Sashiko AI review =C2=B7 https://sashiko.dev/#/patchset/20261128-avd-v4-0-3= 5ad7d52c842@icloud.com?part=3D9