Linux kernel and device drivers for NXP i.MX platforms
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Jiaqi Shi" <shijiaqi_develop@163.com>, imx@lists.linux.dev
Cc: Frank.Li@kernel.org, media-ci@linuxtv.org
Subject: Re: [PATCH 1/2] media: imx: remove legacy video node control and format handling
Date: Sat, 21 Nov 2026 17:26:10 +0000	[thread overview]
Message-ID: <sashiko-outbox-227206@kernel.org> (raw)
In-Reply-To: <20261121170232.414316-2-shijiaqi_develop@163.com>

Thank you for your contribution! Sashiko AI review found 6 potential
issues to consider:
- [High] media: imx: broken pixel format validation in
  capture_find_format()
- [High] media: imx: 32-bit planar sizeimage overflow in
  __capture_try_fmt()
- [Medium] media: imx: __capture_try_fmt() fails with -EINVAL on large
  dimensions
- [Medium] media: imx: unsanitized field handling in __capture_try_fmt()
- [Medium] media: imx: wrong default RGB quantization in
  __capture_try_fmt()
- [Medium] media: imx: callers of imx_media_capture_device_init() set
  legacy_api
--

commit 110cb63254b68ca17643e4ce4ba642ee6210e734
Author: Jiaqi Shi <shijiaqi_develop@163.com>

media: imx: remove legacy video node control and format handling

This commit removes control inheritance from pipeline subdevices to capture
video nodes and switches capture format handling to source-independent format
ioctls initialized to a default UYVY 640x480 format.

> diff --git a/drivers/staging/media/imx/imx-ic-prpencvf.c b/drivers/staging/media/imx/imx-ic-prpencvf.c
> index 3430ddeac3569..372d25c791d35 100644
> --- a/drivers/staging/media/imx/imx-ic-prpencvf.c
> +++ b/drivers/staging/media/imx/imx-ic-prpencvf.c
> @@ -1280,13 +1280,13 @@ static int prp_registered(struct v4l2_subdev *sd)
>  
>  	priv->vdev = imx_media_capture_device_init(ic_priv->ipu_dev,
>  						   &ic_priv->sd,
> -						   PRPENCVF_SRC_PAD, true);
> +						   PRPENCVF_SRC_PAD, true, true);

[Severity: Medium]
Should prp_registered() and csi_registered() pass legacy_api = false (or
remove the legacy_api parameter from imx_media_capture_device_init()) now
that legacy format and control handling is being removed?

Because both callers still pass legacy_api = true, imx_media_capture_device_init()
never sets V4L2_CAP_IO_MC on any capture video node, which causes
v4l_enum_fmt() in v4l2-core to zero f->mbus_code before calling
capture_enum_fmt_vid_cap().  It also leaves capture_legacy_ioctl_ops active
(while capture_ioctl_ops is unused), so VIDIOC_ENUM_FRAMESIZES still calls
capture_legacy_enum_framesizes(), which queries the source subdev's active
format and fails with -EINVAL on IC capture nodes for any pixel format whose
cc->codes[0] is not an IPU-internal media bus code.

[ ... ]

> diff --git a/drivers/staging/media/imx/imx-media-capture.c b/drivers/staging/media/imx/imx-media-capture.c
> index 9216a221b72f4..adc524038ed81 100644
> --- a/drivers/staging/media/imx/imx-media-capture.c
> +++ b/drivers/staging/media/imx/imx-media-capture.c

[ ... ]

> @@ -74,7 +70,11 @@ static const struct imx_media_pixfmt *capture_find_format(u32 code, u32 fourcc)
>  		return cc;
>  	}
>  
> -	return imx_media_find_mbus_format(code, PIXFMT_SEL_ANY);
> +	cc = imx_media_find_mbus_format(code, PIXFMT_SEL_ANY);
> +	if (cc && fourcc && cc->fourcc != fourcc)
> +		return NULL;
> +
> +	return cc;
>  }

[Severity: High]
Does capture_find_format() misvalidate both passthrough and IPU-internal
formats when called from capture_validate_fmt() at stream start?

In the non-IPU branch above, imx_media_find_mbus_format(code, PIXFMT_SEL_ANY)
returns only the first entry in pixel_formats[] matching code:

drivers/staging/media/imx/imx-media-utils.c:pixel_formats[] {
    ...
	}, {
		.fourcc = V4L2_PIX_FMT_GREY,
		.codes = IMX_BUS_FMTS(
			MEDIA_BUS_FMT_Y8_1X8,
			MEDIA_BUS_FMT_Y10_1X10,
			MEDIA_BUS_FMT_Y12_1X12
		),
		.cs     = IPUV3_COLORSPACE_RGB,
		.bpp    = 8,
		.bayer  = true,
	}, {
		.fourcc = V4L2_PIX_FMT_Y10,
		.codes = IMX_BUS_FMTS(MEDIA_BUS_FMT_Y10_1X10),
		.cs     = IPUV3_COLORSPACE_RGB,
		.bpp    = 16,
		.bayer  = true,
    ...
}

Because V4L2_PIX_FMT_GREY precedes V4L2_PIX_FMT_Y10 and V4L2_PIX_FMT_Y12 and
lists MEDIA_BUS_FMT_Y10_1X10 and MEDIA_BUS_FMT_Y12_1X12,
imx_media_find_mbus_format() returns V4L2_PIX_FMT_GREY for those codes.
Checking cc->fourcc != fourcc then rejects valid V4L2_PIX_FMT_Y10 and
V4L2_PIX_FMT_Y12 capture with -EPIPE while accepting 8-bpp V4L2_PIX_FMT_GREY
buffers for 16-bit passthrough DMA.

In the IPU branch of capture_find_format(), when fourcc is not found in
fmt_sel, it falls back to the first format in fmt_sel instead of returning
NULL:

drivers/staging/media/imx/imx-media-capture.c:capture_find_format() {
	cc = imx_media_find_ipu_format(code, PIXFMT_SEL_YUV_RGB);
	if (cc) {
		enum imx_pixfmt_sel fmt_sel = cc->cs == IPUV3_COLORSPACE_YUV
					    ? PIXFMT_SEL_YUV : PIXFMT_SEL_RGB;

		cc = imx_media_find_pixel_format(fourcc, fmt_sel);
		if (!cc) {
			imx_media_enum_pixel_formats(&fourcc, 0, fmt_sel, 0);
			cc = imx_media_find_pixel_format(fourcc, fmt_sel);
		}

		return cc;
	}
    ...
}

Because all Bayer and greyscale entries in pixel_formats[] have
.cs = IPUV3_COLORSPACE_RGB, configuring a Bayer or greyscale pixel format on
a video node fed by MEDIA_BUS_FMT_ARGB8888_1X32 falls back to a
PIXFMT_SEL_RGB descriptor with cc->cs == IPUV3_COLORSPACE_RGB, passes
priv->vdev.cc->cs != cc->cs in capture_validate_fmt(), and reaches
ipu_cpmem_set_image(), triggering WARN_ON(1) for V4L2_PIX_FMT_Y10/Y12 or
leaving the CPMEM channel at 32 bpp after an unchecked ipu_cpmem_set_fmt()
-EINVAL error and overflowing 8-bpp or 16-bpp DMA buffers.

> @@ -136,10 +136,12 @@ static int capture_g_fmt_vid_cap(struct file *file, void *fh,
>  }
>  
>  static const struct imx_media_pixfmt *
> -__capture_try_fmt(struct v4l2_pix_format *pixfmt, struct v4l2_rect *compose)
> +__capture_try_fmt(struct capture_priv *priv, struct v4l2_pix_format *pixfmt,
> +		  struct v4l2_rect *compose)
>  {
>  	struct v4l2_mbus_framefmt fmt_src;
>  	const struct imx_media_pixfmt *cc;
> +	u64 sizeimage;
>  
>  	/*
>  	 * Find the pixel format, default to the first supported format if not
> @@ -167,9 +169,21 @@ __capture_try_fmt(struct v4l2_pix_format *pixfmt, struct v4l2_rect *compose)
>  		}
>  	}

[Severity: Medium]
Now that all capture nodes use __capture_try_fmt(), does this leave
pixfmt->field unsanitized and unvalidated against the source subdev?

In __capture_try_fmt(), the V4L2_FIELD_SEQ_TB and V4L2_FIELD_SEQ_BT cases sit
inside if (V4L2_FIELD_IS_INTERLACED(pixfmt->field)), which is false for
sequential fields, making that switch unreachable.  No other check sanitizes
pixfmt->field, so V4L2_FIELD_ANY or invalid field values are returned as-is
from VIDIOC_TRY_FMT and VIDIOC_S_FMT, and capture_validate_fmt() does not
verify priv->vdev.fmt.field against fmt_src.format.field before configuring
IDMAC interweaving.

> -	v4l2_fill_mbus_format(&fmt_src, pixfmt, 0);
> +	/* Clamp dimensions to prevent zero-sized buffers. */
> +	pixfmt->width = clamp(pixfmt->width, 1, 65535);
> +	pixfmt->height = clamp(pixfmt->height, 1, 65535);
> +
> +	v4l2_fill_mbus_format(&fmt_src, pixfmt,
> +			      cc->codes ? cc->codes[0] : 0);
> +	imx_media_try_colorimetry(&fmt_src, priv->ic_route);

[Severity: Medium]
Does passing cc->codes ? cc->codes[0] : 0 give in-memory-only RGB formats the
wrong default quantization?

V4L2_PIX_FMT_BGR24, V4L2_PIX_FMT_XBGR32, V4L2_PIX_FMT_BGRX32 and
V4L2_PIX_FMT_RGBX32 have .codes = NULL in pixel_formats[].  When fmt_src.code
is 0, imx_media_try_colorimetry() cannot find a format descriptor, leaves
is_rgb = false, and maps V4L2_QUANTIZATION_DEFAULT to
V4L2_QUANTIZATION_LIM_RANGE instead of V4L2_QUANTIZATION_FULL_RANGE.

>  	imx_media_mbus_fmt_to_pix_fmt(pixfmt, &fmt_src, cc);
>  
> +	sizeimage = (u64)pixfmt->bytesperline * pixfmt->height;
> +	if (cc->planar)
> +		sizeimage = sizeimage * cc->bpp / 8;
> +	if (sizeimage > U32_MAX)
> +		return NULL;

[Severity: High]
Can pixfmt->sizeimage wrap to 0 for planar formats even when the 64-bit
sizeimage check here succeeds?

imx_media_mbus_fmt_to_pix_fmt() computes pix->sizeimage in 32-bit arithmetic
before shifting right by 3:

drivers/staging/media/imx/imx-media-utils.c:imx_media_mbus_fmt_to_pix_fmt() {
    ...
	pix->sizeimage = cc->planar ? ((stride * pix->height * cc->bpp) >> 3) :
			 stride * pix->height;
}

For a 16-bpp planar format such as V4L2_PIX_FMT_NV16 or V4L2_PIX_FMT_YUV422P
at 16384x16384, stride * pix->height * cc->bpp is 2^32, so pixfmt->sizeimage
wraps to 0 in u32 while the 64-bit sizeimage in __capture_try_fmt() is
512 MiB (<= U32_MAX) and is never written back to pixfmt->sizeimage.  A
subsequent VIDIOC_REQBUFS or VIDIOC_CREATE_BUFS then divides by zero in
capture_queue_setup() at VID_MEM_LIMIT / pix->sizeimage.

> @@ -181,7 +195,11 @@ __capture_try_fmt(struct v4l2_pix_format *pixfmt, struct v4l2_rect *compose)
>  static int capture_try_fmt_vid_cap(struct file *file, void *fh,
>  				   struct v4l2_format *f)
>  {
> -	__capture_try_fmt(&f->fmt.pix, NULL);
> +	struct capture_priv *priv = video_drvdata(file);
> +
> +	if (!__capture_try_fmt(priv, &f->fmt.pix, NULL))
> +		return -EINVAL;
> +
>  	return 0;
>  }

[Severity: Medium]
Should VIDIOC_TRY_FMT and VIDIOC_S_FMT clamp width and height to dimensions
that fit within sizeimage rather than returning -EINVAL when sizeimage
exceeds U32_MAX?

capture_enum_framesizes() advertises dimensions up to 65535x65535, and
__capture_try_fmt() clamps width and height to 65535 (rounded up to 65536 in
imx_media_mbus_fmt_to_pix_fmt()).  For any format with bpp >= 12 (including
the default UYVY), requesting 65535x65535 or probing maximum dimensions with
~0U x ~0U yields sizeimage > U32_MAX and causes VIDIOC_TRY_FMT and
VIDIOC_S_FMT to fail with -EINVAL instead of adjusting the dimensions into a
valid range.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261121170232.414316-1-shijiaqi_develop@163.com?part=1

  reply	other threads:[~2026-11-21 17:26 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-21 17:02 [PATCH v2 0/2] media: imx: staging cleanup and FIM export to v4l2-core Jiaqi Shi
2026-11-21 17:02 ` [PATCH 1/2] media: imx: remove legacy video node control and format handling Jiaqi Shi
2026-11-21 17:26   ` sashiko-bot [this message]
2026-11-21 17:02 ` [PATCH 2/2] media: v4l2-core: add frame interval monitor helper Jiaqi Shi
2026-11-21 17:13   ` 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-227206@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=Frank.Li@kernel.org \
    --cc=imx@lists.linux.dev \
    --cc=media-ci@linuxtv.org \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=shijiaqi_develop@163.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