[PATCH v2 4/7] media: verisilicon: Refuse JPEG encode jobs with inconsistent geometry

Nicolas Dufresne nicolas.dufresne at collabora.com
Mon Nov 23 19:11:54 PST 2026


Le jeudi 12 novembre 2026 à 08:31 +0000, Arun Iyengar via B4 Relay a écrit :
> From: Arun Iyengar <aiyengar1990 at gmail.com>
> 
> The JPEG encoders program the hardware with the macroblock count of the
> OUTPUT format and treat the difference between the OUTPUT and CAPTURE
> sizes as the crop inside the last macroblock, which is written into
> narrow register fields. They also write the JPEG header into the
> CAPTURE buffer without checking its size.
> 
> When the two formats disagree beyond that, as could happen before the
> coded size was kept in sync with the OUTPUT format, the encoder on
> RK3588 generated IOMMU page faults close to address 0, timed out, and
> left the IOMMU unable to stall, requiring a reboot.
> 
> The format ioctls are now expected to keep the formats consistent, but
> as getting this wrong takes down the whole encoder, check the geometry
> and the CAPTURE buffer size before programming the hardware and fail
> the job instead.
> 
> Signed-off-by: Arun Iyengar <aiyengar1990 at gmail.com>
> ---
>  .../platform/verisilicon/hantro_h1_jpeg_enc.c      |  9 ++++++
>  drivers/media/platform/verisilicon/hantro_jpeg.c   | 37 ++++++++++++++++++++++
>  drivers/media/platform/verisilicon/hantro_jpeg.h   |  5 +++
>  .../verisilicon/rockchip_vpu2_hw_jpeg_enc.c        |  9 ++++++
>  4 files changed, 60 insertions(+)
> 
> diff --git a/drivers/media/platform/verisilicon/hantro_h1_jpeg_enc.c b/drivers/media/platform/verisilicon/hantro_h1_jpeg_enc.c
> index 86cc1a07026f..b2f97f45e0ac 100644
> --- a/drivers/media/platform/verisilicon/hantro_h1_jpeg_enc.c
> +++ b/drivers/media/platform/verisilicon/hantro_h1_jpeg_enc.c
> @@ -109,10 +109,19 @@ int hantro_h1_jpeg_enc_run(struct hantro_ctx *ctx)
>  	struct vb2_v4l2_buffer *src_buf, *dst_buf;
>  	struct hantro_jpeg_ctx jpeg_ctx;
>  	u32 reg;
> +	int ret;
>  
>  	src_buf = hantro_get_src_buf(ctx);
>  	dst_buf = hantro_get_dst_buf(ctx);
>  
> +	ret = hantro_jpeg_enc_check_job(ctx, &dst_buf->vb2_buf);
> +	if (ret) {
> +		/* Complete the controls of the request, the job is not run */
> +		v4l2_ctrl_request_complete(src_buf->vb2_buf.req_obj.req,
> +					   &ctx->ctrl_handler);
> +		return ret;
> +	}
> +
>  	hantro_start_prepare_run(ctx);
>  
>  	memset(&jpeg_ctx, 0, sizeof(jpeg_ctx));
> diff --git a/drivers/media/platform/verisilicon/hantro_jpeg.c b/drivers/media/platform/verisilicon/hantro_jpeg.c
> index 13a60638e43f..2f2afd632c1b 100644
> --- a/drivers/media/platform/verisilicon/hantro_jpeg.c
> +++ b/drivers/media/platform/verisilicon/hantro_jpeg.c
> @@ -14,6 +14,7 @@
>  #include <media/v4l2-jpeg.h>
>  #include "hantro_jpeg.h"
>  #include "hantro.h"
> +#include "hantro_hw.h"
>  
>  #define LUMA_QUANT_OFF		25
>  #define CHROMA_QUANT_OFF	90
> @@ -243,3 +244,39 @@ void hantro_jpeg_header_assemble(struct hantro_jpeg_ctx *ctx)
>  
>  	jpeg_set_quality(ctx);
>  }
> +
> +/*
> + * Check that the formats describe a frame the JPEG encoders can be
> + * programmed with: the coded size may only crop the OUTPUT frame inside
> + * the right-most and bottom-most macroblocks, and the CAPTURE buffer
> + * must have room for the header and some payload. The V4L2 ioctls are
> + * expected to guarantee this, but programming the hardware with
> + * anything else makes it access memory outside the buffers, so refuse
> + * the job instead.
> + */
> +int hantro_jpeg_enc_check_job(struct hantro_ctx *ctx,
> +			      struct vb2_buffer *dst_buf)
> +{
> +	const struct v4l2_pix_format_mplane *src = &ctx->src_fmt;
> +	const struct v4l2_pix_format_mplane *dst = &ctx->dst_fmt;
> +
> +	if (!dst->width || !dst->height ||
> +	    !IS_ALIGNED(src->width, MB_DIM) ||
> +	    !IS_ALIGNED(src->height, MB_DIM) ||

The alignment should be controlled by the s_fmt function, and zeros should be
impossible.

> +	    dst->width > src->width || dst->height > src->height ||
> +	    src->width - dst->width >= MB_DIM ||
> +	    src->height - dst->height >= MB_DIM ||
> +	    !IS_ALIGNED(src->width - dst->width, 4)) {
> +		vpu_err("inconsistent geometry: OUTPUT %ux%u, CAPTURE %ux%u\n",
> +			src->width, src->height, dst->width, dst->height);
> +		return -EINVAL;
> +	}

Thinking out loud, spec says the dst_fmt width/height is ignored/read-only. But
then resetting the OUTPUT, which isn't done properly in this driver it seems,
will always go back to a "default size". And later on, the spec has a huge gab
for the case application did not pick sizeimage (what you are implementing). 

I think there is no choice but to first complete the spec here. Its going to be
tricky to stay backward compatible, but I think we can assume that only
application passing a sizeimage > 0 worked.

Please make a propsal to try and fix the spec, and we can then review
accordingly to the updadated spec.

Nicolas

> +
> +	if (vb2_plane_size(dst_buf, 0) <= ctx->vpu_dst_fmt->header_size) {
> +		vpu_err("CAPTURE buffer too small: %lu bytes\n",
> +			vb2_plane_size(dst_buf, 0));
> +		return -EINVAL;
> +	}
> +
> +	return 0;
> +}
> diff --git a/drivers/media/platform/verisilicon/hantro_jpeg.h b/drivers/media/platform/verisilicon/hantro_jpeg.h
> index 0b49d0b82caa..296ad1e821c7 100644
> --- a/drivers/media/platform/verisilicon/hantro_jpeg.h
> +++ b/drivers/media/platform/verisilicon/hantro_jpeg.h
> @@ -1,5 +1,8 @@
>  /* SPDX-License-Identifier: GPL-2.0+ */
>  
> +struct hantro_ctx;
> +struct vb2_buffer;
> +
>  #define JPEG_HEADER_SIZE	624
>  #define JPEG_QUANT_SIZE		64
>  
> @@ -13,3 +16,5 @@ struct hantro_jpeg_ctx {
>  };
>  
>  void hantro_jpeg_header_assemble(struct hantro_jpeg_ctx *ctx);
> +int hantro_jpeg_enc_check_job(struct hantro_ctx *ctx,
> +			      struct vb2_buffer *dst_buf);
> diff --git a/drivers/media/platform/verisilicon/rockchip_vpu2_hw_jpeg_enc.c b/drivers/media/platform/verisilicon/rockchip_vpu2_hw_jpeg_enc.c
> index 61621b1be8a2..c7a1323506f5 100644
> --- a/drivers/media/platform/verisilicon/rockchip_vpu2_hw_jpeg_enc.c
> +++ b/drivers/media/platform/verisilicon/rockchip_vpu2_hw_jpeg_enc.c
> @@ -135,10 +135,19 @@ int rockchip_vpu2_jpeg_enc_run(struct hantro_ctx *ctx)
>  	struct vb2_v4l2_buffer *src_buf, *dst_buf;
>  	struct hantro_jpeg_ctx jpeg_ctx;
>  	u32 reg;
> +	int ret;
>  
>  	src_buf = hantro_get_src_buf(ctx);
>  	dst_buf = hantro_get_dst_buf(ctx);
>  
> +	ret = hantro_jpeg_enc_check_job(ctx, &dst_buf->vb2_buf);
> +	if (ret) {
> +		/* Complete the controls of the request, the job is not run */
> +		v4l2_ctrl_request_complete(src_buf->vb2_buf.req_obj.req,
> +					   &ctx->ctrl_handler);
> +		return ret;
> +	}
> +
>  	hantro_start_prepare_run(ctx);
>  
>  	memset(&jpeg_ctx, 0, sizeof(jpeg_ctx));
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 228 bytes
Desc: This is a digitally signed message part
URL: <http://lists.infradead.org/pipermail/linux-rockchip/attachments/20261123/ae204de8/attachment-0001.sig>


More information about the Linux-rockchip mailing list