From: Nick Rogers <nick@getfieldwork.ai>
To: Brian Daniels <briandaniels@google.com>
Cc: Mauro Carvalho Chehab <mchehab@kernel.org>,
adelva@google.com, aesteve@redhat.com, changyeon@google.com,
daniel.almeida@collabora.com, eperezma@redhat.com,
gnurou@gmail.com, gurchetansingh@google.com, hverkuil@xs4all.nl,
linux-kernel@vger.kernel.org, linux-media@vger.kernel.org,
mst@redhat.com, nicolas.dufresne@collabora.com,
virtualization@lists.linux.dev, xuanzhuo@linux.alibaba.com,
dbassey@redhat.com, laurent.pinchart@ideasonboard.com
Subject: Re: [PATCH v9 4/4] media: virtio: Add ioctl operations and driver logic
Date: Wed, 23 Sep 2026 17:29:28 +0100 [thread overview]
Message-ID: <20260923162928.73497-1-nick@getfieldwork.ai> (raw)
In-Reply-To: <20260917171921.2810550-5-briandaniels@google.com>
Hi Brian, Alexandre,
We have been running v9 on 6.18 in lighter, a macOS VMM, against a
host-side stateful decoder and encoder backed by VideoToolbox, with
ffmpeg 5.1 and 7.1 and GStreamer 1.26 in the guest. It works well; we
found three problems along the way, all in this patch, and have been
carrying the fixes below.
1. queued_bufs can drift until poll stops reporting the queue writable
virtio_media_qbuf() increments queue->queued_bufs after the device has
replied, without queues_lock. A device that completes the buffer before
it replies (ours decodes within the command) sends the DQBUF event while
the QBUF caller is still waiting, and the event's decrement races the
unlocked increment. When the decrement is lost, queued_bufs creeps up
until it equals allocated_bufs and the OUTPUT queue is never reported
writable again. With our device this stalled ffmpeg about once a minute.
Counting the buffer before the device can see it, under the lock, and
undoing that if the command fails:
--- a/drivers/media/virtio/virtio_media_ioctls.c
+++ b/drivers/media/virtio/virtio_media_ioctls.c
@@ -899,15 +899,20 @@
old_flags = buffer->buffer.flags;
buffer->buffer.flags = V4L2_BUF_FLAG_QUEUED;
+ mutex_lock(&session->queues_lock);
+ queue->queued_bufs += 1;
+ mutex_unlock(&session->queues_lock);
+
ret = virtio_media_send_buffer_ioctl(vfh, VIDIOC_QBUF, b);
if (ret) {
/* Rollback the previous flags as the buffer is not queued. */
+ mutex_lock(&session->queues_lock);
+ queue->queued_bufs -= 1;
+ mutex_unlock(&session->queues_lock);
buffer->buffer.flags = old_flags;
return ret;
}
- queue->queued_bufs += 1;
-
return 0;
}
2. poll does not report the CAPTURE queue readable after the LAST buffer
Once the LAST buffer has been dequeued, DQBUF on the CAPTURE queue
returns -EPIPE, which is how clients such as ffmpeg's v4l2m2m wrapper
learn the stream has ended. vb2 reports the queue readable in that
state (vb2_core_poll() checks last_buffer_dequeued) so the client goes
on to call DQBUF; virtio_media_device_poll() does not, so a client that
polls once more after a drain waits forever. Debian's ffmpeg 5.1 does
exactly that at the end of a stream with no B-frames:
--- a/drivers/media/virtio/virtio_media_driver.c
+++ b/drivers/media/virtio/virtio_media_driver.c
@@ -633,7 +633,8 @@
(capture_queue->queued_bufs == 0 &&
list_empty(&capture_queue->pending_dqbufs)))
rc |= EPOLLERR;
- else if (!list_empty(&capture_queue->pending_dqbufs))
+ else if (!list_empty(&capture_queue->pending_dqbufs) ||
+ capture_queue->is_capture_last)
rc |= EPOLLIN | EPOLLRDNORM;
}
if (req_events & (EPOLLOUT | EPOLLWRNORM)) {
3. VIDIOC_G_CTRL and VIDIOC_S_CTRL fail with -EINVAL
The driver has no control handler, so the V4L2 core turns G_CTRL and
S_CTRL into a single extended control and calls the driver's
g/s_ext_ctrls. That control is built on the core's stack, and its size
field is never initialized; virtio_media_send_ext_controls_ioctl() takes
a nonzero size as a payload to copy from userspace, and the ioctl fails.
GStreamer's V4L2 encoders set their profile with S_CTRL and cannot
negotiate, and v4l2-ctl cannot read the MIN_BUFFERS controls.
The fault is really in the core, which should hand drivers a zeroed
structure, and I have sent a patch for that separately [1]. Until it
lands the driver sees garbage there, so you may also want to guard
against it; we have been clearing size when the controls array is on
the stack, which only the core's G/S_CTRL translation produces.
[1] https://lore.kernel.org/all/20260923160936.33445-1-nick@getfieldwork.ai/
Thanks for the driver,
Nick Rogers
prev parent reply other threads:[~2026-09-23 16:29 UTC|newest]
Thread overview: 16+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-09-17 17:19 [PATCH v9 0/4] media: add virtio-media driver Brian Daniels
2026-09-17 17:19 ` [PATCH v9 1/4] media: virtio: Add skeleton " Brian Daniels
2026-09-18 15:10 ` Albert Esteve
2026-09-22 18:33 ` Brian Daniels
2026-09-19 19:52 ` Michael S. Tsirkin
2026-09-22 18:36 ` Brian Daniels
2026-09-19 20:03 ` Michael S. Tsirkin
2026-09-22 18:37 ` Brian Daniels
2026-09-17 17:19 ` [PATCH v9 2/4] media: virtio: Add session management Brian Daniels
2026-09-18 15:12 ` Albert Esteve
2026-09-19 20:00 ` Michael S. Tsirkin
2026-09-17 17:19 ` [PATCH v9 3/4] media: virtio: Add scatterlist builder Brian Daniels
2026-09-19 20:01 ` Michael S. Tsirkin
2026-09-17 17:19 ` [PATCH v9 4/4] media: virtio: Add ioctl operations and driver logic Brian Daniels
2026-09-18 15:15 ` Albert Esteve
2026-09-23 16:29 ` Nick Rogers [this message]
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=20260923162928.73497-1-nick@getfieldwork.ai \
--to=nick@getfieldwork.ai \
--cc=adelva@google.com \
--cc=aesteve@redhat.com \
--cc=briandaniels@google.com \
--cc=changyeon@google.com \
--cc=daniel.almeida@collabora.com \
--cc=dbassey@redhat.com \
--cc=eperezma@redhat.com \
--cc=gnurou@gmail.com \
--cc=gurchetansingh@google.com \
--cc=hverkuil@xs4all.nl \
--cc=laurent.pinchart@ideasonboard.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-media@vger.kernel.org \
--cc=mchehab@kernel.org \
--cc=mst@redhat.com \
--cc=nicolas.dufresne@collabora.com \
--cc=virtualization@lists.linux.dev \
--cc=xuanzhuo@linux.alibaba.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
all inboxes | Powered by JetHome®