media: zoran: fix capture buffer size / geometry desync
Subsystems (2)
cover media: zoran: fix capture buffer size / geometry desync
zoran_v4l_set_format() updates zr->buffer_size before it validates the line alignment, and zoran_s_fmt_vid_cap() updates zr->map_mode before it calls zoran_v4l_set_format() at all. When the alignment check rejects the format with -EINVAL both fields keep their new values, while zr->v4l_settings, which is only committed after every check has passed, still describes the format that was accepted before.
That matters because the two sides of the frame size contract come from different fields:
zr_vb2_queue_setup() -> zr->buffer_size zr36057_set_memgrab() -> zr->v4l_settings zr_set_buf() -> zr->v4l_settings
The ZR36057 has no transfer length register, so the geometry and stride registers alone bound the write, and nothing compares them against the size the buffers were allocated with. An accepted VIDIOC_S_FMT, a VIDIOC_S_FMT that fails the alignment check, and then VIDIOC_REQBUFS is enough to allocate buffers for the rejected format and have the chip DMA a frame of the accepted format into them. 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 it, because it compares the plane size against the same zr->buffer_size that was used to allocate the plane.
Patch 1 removes the buffer size check in zoran_v4l_set_format(), which has not been able to fire since the assignment above it was introduced by commit 11c9c16f7cb5. The comparison used to be made against fh->buffers.buffer_size, the size of the already allocated buffers, and was a real bounds check.
Patch 2 defers the zr->buffer_size and zr->map_mode updates until every check has passed. This 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 commits.
Patch 3 rejects VIDIOC_S_FMT while the queue is busy, which the driver never checked.
Patch 4 adds KUnit tests for the above.
Verified on v7.0 with:
./tools/testing/kunit/kunit.py run --arch=x86_64 \
--kconfig_add CONFIG_PCI=y --kconfig_add CONFIG_I2C=y \
--kconfig_add '# CONFIG_I2C_HELPER_AUTO is not set' \
--kconfig_add CONFIG_I2C_ALGOBIT=y \
--kconfig_add CONFIG_MEDIA_SUPPORT=y \
--kconfig_add '# CONFIG_MEDIA_SUPPORT_FILTER is not set' \
--kconfig_add CONFIG_MEDIA_CAMERA_SUPPORT=y \
--kconfig_add CONFIG_MEDIA_PCI_SUPPORT=y \
--kconfig_add CONFIG_VIDEO_DEV=y --kconfig_add CONFIG_DEBUG_FS=y \
--kconfig_add CONFIG_VIDEO_ZORAN=y \
--kconfig_add CONFIG_VIDEO_ZORAN_KUNIT_TEST=y zoranWith only patch 4 applied:
# zoran_set_format_rejected_keeps_state: EXPECTATION FAILED at drivers/media/pci/zoran/zoran_driver_test.c:70 Expected 768u * 576 * 4 == zr->buffer_size, but
768u * 576 * 4 == 1769472 (0x1b0000)
zr->buffer_size == 1584 (0x630) [FAILED] zoran_set_format_rejected_keeps_state [PASSED] zoran_set_format_accepted_is_consistent [PASSED] zoran_set_format_out_of_range
# Totals: pass:2 fail:1 skip:0 total:3
With the whole series applied:
[PASSED] zoran_set_format_rejected_keeps_state [PASSED] zoran_set_format_accepted_is_consistent [PASSED] zoran_set_format_out_of_range
# Totals: pass:3 fail:0 skip:0 total:3
A W=1 build of drivers/media/pci/zoran/ is warning free with the series applied.
I do not have the hardware, so the DMA overrun itself is derived from reading zr_set_buf() and zr36057_set_memgrab() rather than observed, and only the state desync is covered by the tests. Testing on a real card would be appreciated.
Disclosure: this series was prepared with AI assistance (Claude Opus 5). I reviewed every patch and take responsibility for the submission. I left the Assisted-by tag out of the commit messages because checkpatch.pl flags it as an error in v7.0.
Note also that VIDIOC_S_FMT rejects a misaligned width while zoran_try_fmt_vid_cap() adjusts it through v4l_bound_align_image(). Making VIDIOC_S_FMT adjust the width the same way would remove the failing path altogether, but it changes what existing applications get back, so I left it out of this series.
Minseo Park (4):
media: zoran: remove always-false buffer size check
media: zoran: do not update device state before the format is validated
media: zoran: reject VIDIOC_S_FMT while the queue is busy
media: zoran: add KUnit tests for capture format negotiation
drivers/media/pci/zoran/Kconfig | 14 ++++ drivers/media/pci/zoran/zoran_driver.c | 21 +++-- drivers/media/pci/zoran/zoran_driver_test.c | 123 ++++++++++++++++++++++++++++ 3 files changed, 147 insertions(+), 11 deletions(-) create mode 100644 drivers/media/pci/zoran/zoran_driver_test.c
PATCH 1/4 media: zoran: remove always-false buffer size check
zoran_v4l_set_format() assigns zr->buffer_size and then immediately compares the very same expression against it:
zr->buffer_size = height * width * bpp;
/* Check against available buffer size */
if (height * width * bpp > zr->buffer_size) {The condition can never be true, so the check and its error path are dead code.
The comparison used to be made against fh->buffers.buffer_size, the size of the buffers that had already been allocated for the file handle, which made it a real bounds check. The assignment on the line above turned the right hand side into the value that had just been computed, so nothing is verified any more.
Remove the dead check. No functional change.
--- drivers/media/pci/zoran/zoran_driver.c | 7 ------- 1 file changed, 7 deletions(-)
drivers/media/pci/zoran/zoran_driver.c +0 -7
@@ -179,13 +179,6 @@ static int zoran_v4l_set_format(struct zoran *zr, int width, int height, zr->buffer_size = height * width * bpp; -/* Check against available buffer size */ -if (height * width * bpp > zr->buffer_size) { -pci_dbg (zr->pci_dev,"%s - video buffer size (%d kB) is too small\n" , - __func__, zr->buffer_size >>10 ); -return -EINVAL ; - } -/* The video front end needs 4-byte alinged line sizes */ if ((bpp ==2 && (width &1 )) || (bpp ==3 && (width &3 ))) {
Signature · 1 line
2.43.0
PATCH 2/4 media: zoran: do not update device state before the format is validated
In reply to Minseo Parkzoran_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.
--- drivers/media/pci/zoran/zoran_driver.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-)
drivers/media/pci/zoran/zoran_driver.c +3 -4
@@ -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;
Signature · 1 line
2.43.0
PATCH 3/4 media: zoran: reject VIDIOC_S_FMT while the queue is busy
In reply to Minseo Parkzoran_s_fmt_vid_cap() updates zr->v4l_settings, zr->jpg_settings, zr->map_mode and zr->buffer_size without checking whether the queue already owns buffers, so the format can be changed underneath an allocated or streaming queue. The size the queue was set up with and the geometry the hardware is programmed with then describe a format the queue no longer matches.
Return -EBUSY while the queue is busy, as VIDIOC_S_FMT is specified to do.
--- drivers/media/pci/zoran/zoran_driver.c | 3 +++ 1 file changed, 3 insertions(+)
drivers/media/pci/zoran/zoran_driver.c +3 -0
@@ -506,6 +506,9 @@ static int zoran_s_fmt_vid_cap(struct file *file, void *fh,int i;int res =0 ; +if (vb2_is_busy (&zr->vq)) +return -EBUSY ; +if (fmt->fmt.pix.pixelformat ==V4L2_PIX_FMT_MJPEG )return zoran_s_fmt_vid_out (file, fh, fmt);
Signature · 1 line
2.43.0
PATCH 4/4 media: zoran: add KUnit tests for capture format negotiation
In reply to Minseo ParkCheck that zoran_v4l_set_format() leaves zr->buffer_size and zr->v4l_settings describing the last accepted format when a format is rejected, and that an accepted format leaves the two consistent with each other.
zr_vb2_queue_setup() sizes the capture buffers from zr->buffer_size while zr36057_set_memgrab() and zr_set_buf() program the chip from zr->v4l_settings, so a disagreement between them makes the chip DMA a frame that does not fit the buffers it was given.
The tests are included into zoran_driver.c because the function under test is static.
--- drivers/media/pci/zoran/Kconfig | 14 +++ drivers/media/pci/zoran/zoran_driver.c | 4 + drivers/media/pci/zoran/zoran_driver_test.c | 123 ++++++++++++++++++++ 3 files changed, 141 insertions(+) create mode 100644 drivers/media/pci/zoran/zoran_driver_test.c
drivers/media/pci/zoran/Kconfig +14 -0
@@ -24,6 +24,20 @@ config VIDEO_ZORAN To compile this driver as a module, choose M here: the module will be called zr36067. +config VIDEO_ZORAN_KUNIT_TEST + bool "KUnit tests for the Zoran driver" if !KUNIT_ALL_TESTS + depends on VIDEO_ZORAN && KUNIT + default KUNIT_ALL_TESTS + help + Enable the KUnit tests for the Zoran capture format negotiation. + They check that a format which fails validation leaves the buffer + size and the capture geometry describing the previously accepted + format. + + For more information on KUnit see Documentation/dev-tools/kunit. + + If you are unsure, say N. + config VIDEO_ZORAN_DC30 bool "Pinnacle/Miro DC30(+) support" depends on VIDEO_ZORAN
drivers/media/pci/zoran/zoran_driver.c +4 -0
@@ -976,3 +976,7 @@ void zoran_queue_exit(struct zoran *zr) {vb2_queue_release (zr->video_dev->queue); } + +#if IS_ENABLED (CONFIG_VIDEO_ZORAN_KUNIT_TEST ) +#include "zoran_driver_test.c" +#endif
drivers/media/pci/zoran/zoran_driver_test.c new file +123 -0
123 lines not on this page · raw message
Signature · 1 line
2.43.0
A message that is not in the archive (sent off-list or not yet received): [email protected]
2026년 11월 25일 (수) 오후 5:11, Patchwork Integration <[email protected]>님이 작성:
Quoted 26 lines
Dear Minseo Park:
Thanks for your patches! Unfortunately the Media CI robot detected some issues:
# Test checkpatch:./0004-media-zoran-add-KUnit-tests-for-capture-format-negot.patch checkpatch
WARNING: added, moved or deleted file(s), doesMAINTAINERSneed updating?
#67:
new file mode 100644total: 0 errors, 1 warnings, 0 checks, 150 lines checked
Please fix your series, and upload a new version. If you have a patchwork account, do not forget to mark the current series as Superseded.
For more details, check the full report at:
https://linux-media.pages.freedesktop.org/-/users/patchwork/-/jobs/117190231/artifacts/report.htm .Best regards, and Happy Hacking!
Media CI robot on behalf of the linux-media community.
Quoted 8 lines
---
Check the latest rules for contributing your patches at:
https://docs.kernel.org/driver-api/media/maintainer-entry-profile.htmlIf you believe that the CI is wrong, kindly open an issue at
https://gitlab.freedesktop.org/linux-media/media-ci/-/issues or reply-all to this message.
The MAINTAINERS warning on patch 4 is a false positive. The ZORAN entry already covers the new file with a directory pattern:
F: drivers/media/pci/zoran/
Running get_maintainer.pl on drivers/media/pci/zoran/zoran_driver_test.c resolves to that entry, so there is nothing to add.
Resending would not clear it either: the file is still new in a v2, so checkpatch emits the same warning again.
One thing worth flagging though. This would be the first KUnit test under drivers/media. KUnit itself is well established elsewhere in drivers/ (clk, gpu, iio, input, iommu, mmc, net and scsi among others), but if you would rather not start that here, I am happy to drop patch 4 and resend 1-3 on their own. The fixes do not depend on it; it only covers the state desync that patch 2 fixes.
Thanks,
Minseo