All of lore.kernel.org
 help / color / mirror / Atom feed
From: Minseo Park <hiyoukou@gmail.com>
To: clabbe@baylibre.com, mchehab@kernel.org, hverkuil@kernel.org,
	linux-media@vger.kernel.org
Cc: mjpeg-users@lists.sourceforge.net, linux-kernel@vger.kernel.org,
	Minseo Park <hiyoukou@gmail.com>
Subject: [PATCH 2/4] media: zoran: do not update device state before the format is validated
Date: Wed, 25 Nov 2026 16:05:46 +0900	[thread overview]
Message-ID: <20261125070548.28989-3-hiyoukou@gmail.com> (raw)
In-Reply-To: <20261125070548.28989-1-hiyoukou@gmail.com>

zoran_v4l_set_format() stores the new frame size into zr->buffer_size
before it validates the line alignment, and zoran_s_fmt_vid_cap() stores
the new mapping mode into zr->map_mode before it calls
zoran_v4l_set_format() at all.  Both fields keep their new values when
the alignment check later rejects the format with -EINVAL, while
zr->v4l_settings, which is only committed once every check has passed,
still describes the previously accepted format.

That splits the two values which have to agree on how much data a frame
occupies:

 - zr_vb2_queue_setup() sizes the capture buffers from zr->buffer_size;
 - zr36057_set_memgrab() programs the video front end, and zr_set_buf()
   programs ZR36057_VDTR, ZR36057_VDBR and the ZR36057_VSSFGR stride,
   from zr->v4l_settings.

The ZR36057 has no transfer length register, so those geometry registers
alone bound the write, and nothing compares them against the allocation.
An accepted VIDIOC_S_FMT, followed by one that fails the alignment
check, followed by VIDIOC_REQBUFS therefore allocates buffers for the
rejected format and then lets the chip DMA a frame of the accepted
format into them.  With PAL square pixel timings, 768x576 BGR32 followed
by a rejected 33x24 RGB555 leaves zr->buffer_size at 1584 bytes, so the
queue allocates a single page and the chip writes 768 * 576 * 4 =
1769472 bytes into it.

zr_vb2_prepare() does not catch this: it compares the plane size against
the same zr->buffer_size that was used to allocate the plane.

Commit both fields only once every check has passed, which is what
zoran_s_fmt_vid_out() already does for the JPEG path: it negotiates into
a local struct zoran_jpg_settings, calls zoran_check_jpg_settings() and
only then assigns zr->jpg_settings, zr->map_mode and zr->buffer_size.

Fixes: 11c9c16f7cb5 ("media: zoran: fix use of buffer_size and sizeimage")
Cc: stable@vger.kernel.org
Signed-off-by: Minseo Park <hiyoukou@gmail.com>
---
 drivers/media/pci/zoran/zoran_driver.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)

diff --git a/drivers/media/pci/zoran/zoran_driver.c b/drivers/media/pci/zoran/zoran_driver.c
index a053365922c4..99fedac12b67 100644
--- a/drivers/media/pci/zoran/zoran_driver.c
+++ b/drivers/media/pci/zoran/zoran_driver.c
@@ -177,8 +177,6 @@ static int zoran_v4l_set_format(struct zoran *zr, int width, int height,
 
 	bpp = (format->depth + 7) / 8;
 
-	zr->buffer_size = height * width * bpp;
-
 	/* The video front end needs 4-byte alinged line sizes */
 
 	if ((bpp == 2 && (width & 1)) || (bpp == 3 && (width & 3))) {
@@ -186,6 +184,7 @@ static int zoran_v4l_set_format(struct zoran *zr, int width, int height,
 		return -EINVAL;
 	}
 
+	zr->buffer_size = height * width * bpp;
 	zr->v4l_settings.width = width;
 	zr->v4l_settings.height = height;
 	zr->v4l_settings.format = format;
@@ -530,13 +529,13 @@ static int zoran_s_fmt_vid_cap(struct file *file, void *fh,
 	if (fmt->fmt.pix.width < BUZ_MIN_WIDTH)
 		fmt->fmt.pix.width = BUZ_MIN_WIDTH;
 
-	zr->map_mode = ZORAN_MAP_MODE_RAW;
-
 	res = zoran_v4l_set_format(zr, fmt->fmt.pix.width, fmt->fmt.pix.height,
 				   &zoran_formats[i]);
 	if (res)
 		return res;
 
+	zr->map_mode = ZORAN_MAP_MODE_RAW;
+
 	/* tell the user the results/missing stuff */
 	fmt->fmt.pix.bytesperline = zr->v4l_settings.bytesperline;
 	fmt->fmt.pix.sizeimage = zr->buffer_size;
-- 
2.43.0


  parent reply	other threads:[~2026-11-25  7:06 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-25  7:05 [PATCH 0/4] media: zoran: fix capture buffer size / geometry desync Minseo Park
2026-11-25  7:05 ` [PATCH 1/4] media: zoran: remove always-false buffer size check Minseo Park
2026-11-25  7:05 ` Minseo Park [this message]
2026-11-25  7:05 ` [PATCH 3/4] media: zoran: reject VIDIOC_S_FMT while the queue is busy Minseo Park
2026-11-25  7:05 ` [PATCH 4/4] media: zoran: add KUnit tests for capture format negotiation Minseo Park
2026-12-01  9:00   ` Hans Verkuil
     [not found] ` <6b06984b.d643d4e7.382b4b.1296@mx.google.com>
2026-11-25 10:09   ` [0/4] media: zoran: fix capture buffer size / geometry desync 민서박

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=20261125070548.28989-3-hiyoukou@gmail.com \
    --to=hiyoukou@gmail.com \
    --cc=clabbe@baylibre.com \
    --cc=hverkuil@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-media@vger.kernel.org \
    --cc=mchehab@kernel.org \
    --cc=mjpeg-users@lists.sourceforge.net \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.