From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.128.199 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796153415; b=fHZKcILbXDn9sAQiq34ZG9+rH+7+m3kDy9V2ZJZ7gqdbUhlegmO4MjZ8S9srSBOlT/Ar iYVDEZlpNoMbyVvNpv7hpz5hm3sCafYT6JOtb1GpJ8GZ2Bj+QjEa5j+aHnMt79ic2+F4d Y5ai2oZMW4XVphLJh4zJOdcyqtMgxbtT28Jc8pwwcT6ZDW60N0uNr3eUB/eAgSBRh/1uq Ni86SQk/sKrg+bnCZeJ0ho8w0rt5t83+isdIYn0Se2OPkaAbUf1sbuy4LBRV3Xnfh3b0Z 5lYb89XMV+Lmpolm2Y++H9TN8R7BsZm/BpdhPtR6rTD+YwkTaa9ODN//S42T80AaOmQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796153415; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Received:Date:In-Reply-To: Mime-Version:References:X-Mailer:Message-ID:Subject:From:To:Cc: Content-Type; bh=xH3upsngqMr2NA7CaIPRSnS6MkCF+MMzkheJudRoa2w=; b=nuPG/hSZ/yCUda7KE2/7jsWXFBxhf0mGoi2cetOhWJ2f8PDSaT7WEpqFwrOE4tURc3nQ Ye/mPSzzOTi60pi9y28Dpck40Hrj2dAvoAkg8fglgNq+bF8DA7W1UltJAJQ8elG4Fbc9o PDpfDdLa1s0F1WJRJP7IPoAoqlK94i2pt/+vEdIRhUaghHDC4b9KYVatmNuW04mpo8aLV 92QxLOxXjQHAX9kRVIibEEhiujNRcfOoKdQkPr3UzBMEGP4s639VedL5pSrX1AXLlLPgz zruMAXnJd9T+MgV1sSbfICOF05YE4r8wbxbwq4BLqrdl/DV70ghsspm3w80oPiVzxHA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=google.com; spf=pass smtp.mailfrom=flex--briandaniels.bounces.google.com; dkim=pass header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=c+tMttay; arc=none smtp.remote-ip=209.85.128.199 Received: from mail-yw1-f199.google.com (mail-yw1-f199.google.com [209.85.128.199]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 84DD0606A68E for ; Tue, 1 Dec 2026 19:30:14 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 84DD0606A68E Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--briandaniels.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=c+tMttay Received: by mail-yw1-f199.google.com with SMTP id 00721157ae682-8b30ffeb2f5so169102027b3.0 for ; Tue, 01 Dec 2026 11:30:14 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1796153413; x=1796758213; darn=lists.linux.dev; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:from:to:cc:subject:date:message-id:reply-to :content-type; bh=xH3upsngqMr2NA7CaIPRSnS6MkCF+MMzkheJudRoa2w=; b=c+tMttayG8wvkfDsj4IxQwKjIJMsNG7yIvcYv7gy96lhIXgym8UkxKnkO79kbImKBt LWaUtiG2NRtt5Ka7td4VKflqa3T0wWx9Uy9nfyVejuW42ufwBcHJpvjgylVoImtShh/J +FTCgHScw73Gh9BeUh2JDsx/QAFcmG7UH4mqBCmHx5CiR50HHScXfm73o9LkQF+Yv9KD TNbi74BoOwaJ0QumNpaHQk6n2f/daopUBjIOJwPWYuoq29czmq8/2cdkPS/WBG/YhEeu 46pIiSXOpDW7pisVKzf0/x1bgzTzJiDmjaK/ty7LSXJNbRf/pxXL9N9i4883NWm8A3lK Zi1g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796153413; x=1796758213; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=xH3upsngqMr2NA7CaIPRSnS6MkCF+MMzkheJudRoa2w=; b=EOG6pSWvuAdwmx97+QsycPqhrT93WMBQhZqxIKgNlPj4zCHm6aRz6Pe9CpWQD3k8IO JO1Z9qCK1ndxmjZRfchVdX/LOOZR/ytCi3U92hGW1OlY4lmAKFI1MAY2XNg4eAAdFAvD cE/DbW6u4p5oWLScOUa74+zTUCzwJ7O3BERM1OCMEZLmiNySmEyta1X75Yc7luGARE83 AoJZhkdLUcTXSD/m2itY8JnOSrdcOtJ5zCvGOisgeXCdSZmWHs2YMZSXqwKXwr0Hyyo7 X08M6ebwFx956ycGkL/krAL0CfgUMBxPFqYLQtgpFs38CkrPDwCRlvO0B7CV5YwxSJQ1 6IKw== X-Forwarded-Encrypted: i=1; AL+hErH/8x66LqS1DAgmFrG4KsJ3u0XcUX2+E2lapdjXFPpkWjDRfWXbt2Vypgf5C3FEoyHgnQyvg7dymUUXKjSMVw==@lists.linux.dev X-Gm-Message-State: APuU4Z9tKKnDEWeZfebyGsPxPteZ/aZXEZfaVsdwqedjaqk5LUt9UQ64 JeKq0Ffb863wp/zGDU9jwdiUCG9i0dyVxiF0jN/o6v7eimsPmeZwxlY76+0mbNn5qp8fEOzpH9W pHmgxSQttHW4EJRouiNtV5GNJk3E+ X-Received: from ywbnv11-n1.prod.google.com ([2002:a05:690c:930b:10b0:8b3:a08:ae67]) (user=briandaniels job=prod-delivery.src-stubby-dispatcher) by 2002:a05:690c:6b81:b0:8b0:aba3:a3e2 with SMTP id 00721157ae682-8b5204b13d7mr170099487b3.37.1796153413173; Tue, 01 Dec 2026 11:30:13 -0800 (PST) Date: Tue, 1 Dec 2026 14:29:56 -0500 In-Reply-To: <20261201192957.790894-1-briandaniels@google.com> X-Mailing-List: virtualization@lists.linux.dev List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list Mime-Version: 1.0 References: <20261201192957.790894-1-briandaniels@google.com> X-Mailer: git-send-email 2.56.1.911.g883d8bcb20-goog Message-ID: <20261201192957.790894-6-briandaniels@google.com> Subject: [PATCH v13 5/6] media: virtio: Add MMAP buffer mapping support From: Brian Daniels To: Mauro Carvalho Chehab Cc: 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, Brian Daniels Content-Type: text/plain; charset="UTF-8" From: Alexandre Courbot Support mapping V4L2_MEMORY_MMAP buffers into guest userspace through virtio shared memory region 0. Query the shared memory region during device probe and implement the mmap file operation and VMA close callback using VIRTIO_MEDIA_CMD_MMAP and VIRTIO_MEDIA_CMD_MUNMAP, remapping the host-provided shared memory offset into userspace via io_remap_pfn_range(). Signed-off-by: Alexandre Courbot Co-developed-by: Brian Daniels Signed-off-by: Brian Daniels --- drivers/media/virtio/session.h | 14 +- drivers/media/virtio/virtio_media.h | 13 +- drivers/media/virtio/virtio_media_driver.c | 161 +++++++++++++++++++++ include/uapi/linux/virtio_media.h | 67 +++++++++ 4 files changed, 245 insertions(+), 10 deletions(-) diff --git a/drivers/media/virtio/session.h b/drivers/media/virtio/session.h index d2641e1bf..e0ea03048 100644 --- a/drivers/media/virtio/session.h +++ b/drivers/media/virtio/session.h @@ -63,12 +63,12 @@ struct virtio_media_queue_state { * @nonblocking_dequeue: whether dequeue should block or not (nonblocking if * file opened with O_NONBLOCK). * @uses_mplane: whether the queues for this session use the MPLANE API or not. - * @cmd: union of session commands ``close`` and ``ioctl``. A session can have - * one command currently running. The rest of the commands are handled by - * &struct virtio_media. - * @resp: union of responses to session commands. A session can wait on one - * command only. The rest of the responses are handled by - * &struct virtio_media. + * @cmd: union of session commands ``close``, ``ioctl``, and ``mmap``. A + * session can have one command currently running. The rest of the + * commands are handled by &struct virtio_media. + * @resp: union of responses to session commands ``close``, ``ioctl``, and + * ``mmap``. A session can wait on one command only. The rest of the + * responses are handled by &struct virtio_media. * @shadow_buf: shadow buffer where data to be added to the descriptor chain can * be staged before being sent to the device. * @command_sgs: SG table gathering descriptors for a given command and its @@ -92,10 +92,12 @@ struct virtio_media_session { union { struct virtio_media_cmd_close close; struct virtio_media_cmd_ioctl ioctl; + struct virtio_media_cmd_mmap mmap; } cmd; union { struct virtio_media_resp_ioctl ioctl; + struct virtio_media_resp_mmap mmap; } resp; __dma_from_device_group_end(); diff --git a/drivers/media/virtio/virtio_media.h b/drivers/media/virtio/virtio_media.h index 8b21c1941..7acb2b842 100644 --- a/drivers/media/virtio/virtio_media.h +++ b/drivers/media/virtio/virtio_media.h @@ -26,14 +26,15 @@ * @commandq: virtio command queue. * @eventq: virtio event queue. * @eventq_work: work to run when events are received on @eventq. + * @mmap_region: region into which MMAP buffers are mapped by the host. * @event_buffer: buffer for event descriptors. * @sessions: list of active sessions on the device. * @sessions_lock: protects @sessions and &struct virtio_media_session.list. * @events_lock: prevents concurrent processing of events. - * @cmd: union of device-level commands. Session commands are handled by - * &struct virtio_media_session. - * @resp: union of responses to device-level commands. Session responses are - * handled by &struct virtio_media_session. + * @cmd: union of the device commands ``open`` and ``munmap``. The other + * commands are handled by &struct virtio_media_session + * @resp: union of responses to device commands ``open`` and ``munmap``. The + * other responses are handled by &struct virtio_media_session * @vlock: serializes access to the command queue. * @wq: waitqueue for host responses on the command queue. */ @@ -46,6 +47,8 @@ struct virtio_media { struct virtqueue *eventq; struct work_struct eventq_work; + struct virtio_shm_region mmap_region; + void *event_buffer; struct list_head sessions; @@ -56,10 +59,12 @@ struct virtio_media { __dma_from_device_group_begin(); union { struct virtio_media_cmd_open open; + struct virtio_media_cmd_munmap munmap; } cmd; union { struct virtio_media_resp_open open; + struct virtio_media_resp_munmap munmap; } resp; __dma_from_device_group_end(); diff --git a/drivers/media/virtio/virtio_media_driver.c b/drivers/media/virtio/virtio_media_driver.c index 406d6ffb7..72c234a73 100644 --- a/drivers/media/virtio/virtio_media_driver.c +++ b/drivers/media/virtio/virtio_media_driver.c @@ -6,6 +6,7 @@ * Copyright (c) 2024-2026 Google LLC. */ +#include #include #include #include @@ -20,6 +21,7 @@ #include #include #include +#include #include #include @@ -32,6 +34,12 @@ #define VIRTIO_MEDIA_NUM_EVENT_BUFS 16 +/* ID of the SHM region into which MMAP buffer will be mapped. */ +#define VIRTIO_MEDIA_SHM_MMAP 0 + +/* Bit mask for the VIRTIO_MEDIA_MMAP_FLAG_RW flag */ +#define VIRTIO_MEDIA_MMAP_FLAG_RW_MASK BIT(VIRTIO_MEDIA_MMAP_FLAG_RW) + /** * virtio_media_session_alloc() - Allocate a new session. * @vv: virtio-media device the session belongs to. @@ -703,12 +711,157 @@ static __poll_t virtio_media_device_poll(struct file *file, poll_table *wait) return rc; } +static void virtio_media_vma_close_locked(struct vm_area_struct *vma) +{ + struct virtio_media *vv = vma->vm_private_data; + struct virtio_media_cmd_munmap *cmd_munmap = &vv->cmd.munmap; + struct virtio_media_resp_munmap *resp_munmap = &vv->resp.munmap; + struct scatterlist cmd_sg = {}, resp_sg = {}; + struct scatterlist *sgs[2] = { &cmd_sg, &resp_sg }; + int ret; + + sg_set_buf(&cmd_sg, cmd_munmap, sizeof(*cmd_munmap)); + sg_mark_end(&cmd_sg); + + sg_set_buf(&resp_sg, resp_munmap, sizeof(*resp_munmap)); + sg_mark_end(&resp_sg); + + cmd_munmap->hdr.cmd = cpu_to_le32(VIRTIO_MEDIA_CMD_MUNMAP); + cmd_munmap->hdr.__reserved = 0; + cmd_munmap->driver_addr = + cpu_to_le64((vma->vm_pgoff << PAGE_SHIFT) - + vv->mmap_region.addr); + ret = virtio_media_send_command(vv, sgs, 1, 1, sizeof(*resp_munmap), + NULL); + if (ret < 0) { + v4l2_err(&vv->v4l2_dev, "host failed to unmap buffer: %d\n", + ret); + } +} + +/** + * virtio_media_vma_close() - Close a MMAP buffer mapping. + * @vma: VMA of the mapping to close. + * + * Inform the host that a previously created MMAP mapping is no longer needed + * and can be removed. + */ +static void virtio_media_vma_close(struct vm_area_struct *vma) +{ + struct virtio_media *vv = vma->vm_private_data; + + mutex_lock(&vv->vlock); + virtio_media_vma_close_locked(vma); + mutex_unlock(&vv->vlock); +} + +static const struct vm_operations_struct virtio_media_vm_ops = { + .close = virtio_media_vma_close, +}; + +/** + * virtio_media_device_mmap() - Perform a mmap request from userspace. + * @file: opened file of the session to map for. + * @vma: VM area struct describing the desired mapping. + * + * This requests the host to map a MMAP buffer for us, so we can then make that + * mapping visible into user-space address space. + */ +static int virtio_media_device_mmap(struct file *file, + struct vm_area_struct *vma) +{ + struct video_device *video_dev = video_devdata(file); + struct virtio_media *vv = to_virtio_media(video_dev); + struct virtio_media_session *session = + fh_to_session(file->private_data); + struct virtio_media_cmd_mmap *cmd_mmap = &session->cmd.mmap; + struct virtio_media_resp_mmap *resp_mmap = &session->resp.mmap; + struct scatterlist cmd_sg = {}, resp_sg = {}; + struct scatterlist *sgs[2] = { &cmd_sg, &resp_sg }; + int ret; + + mutex_lock(&session->error_lock); + bool session_error = session->error; + + mutex_unlock(&session->error_lock); + + if (session_error) + return -EIO; + if (!(vma->vm_flags & VM_SHARED)) + return -EINVAL; + if (!(vma->vm_flags & (VM_READ | VM_WRITE))) + return -EINVAL; + + mutex_lock(&vv->vlock); + + cmd_mmap->hdr.cmd = cpu_to_le32(VIRTIO_MEDIA_CMD_MMAP); + cmd_mmap->hdr.__reserved = 0; + cmd_mmap->session_id = cpu_to_le32(session->id); + cmd_mmap->flags = cpu_to_le32((vma->vm_flags & VM_WRITE) ? + VIRTIO_MEDIA_MMAP_FLAG_RW_MASK : 0); + cmd_mmap->offset = cpu_to_le32(vma->vm_pgoff << PAGE_SHIFT); + cmd_mmap->__reserved = 0; + + sg_set_buf(&cmd_sg, cmd_mmap, sizeof(*cmd_mmap)); + sg_mark_end(&cmd_sg); + + sg_set_buf(&resp_sg, resp_mmap, sizeof(*resp_mmap)); + sg_mark_end(&resp_sg); + + /* + * The host performs reference counting and is smart enough to return + * the same guest physical address if this is called several times on + * the same + * buffer. + */ + ret = virtio_media_send_command(vv, sgs, 1, 1, sizeof(*resp_mmap), + NULL); + if (ret < 0) + goto end; + + vma->vm_private_data = vv; + /* + * Keep the guest address at which the buffer is mapped since we will + * use that to unmap. + */ + vma->vm_pgoff = (le64_to_cpu(resp_mmap->driver_addr) + + vv->mmap_region.addr) >> PAGE_SHIFT; + + /* + * We cannot let the mapping be larger than the buffer. + */ + if (vma->vm_end - vma->vm_start > + PAGE_ALIGN(le64_to_cpu(resp_mmap->len))) { + dev_dbg(&video_dev->dev, + "invalid MMAP, as it would overflow buffer length\n"); + ret = -EINVAL; + goto cleanup; + } + + ret = io_remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff, + vma->vm_end - vma->vm_start, + vma->vm_page_prot); + if (ret) { + dev_dbg(&video_dev->dev, "io_remap_pfn_range() failed\n"); + goto cleanup; + } + + vma->vm_ops = &virtio_media_vm_ops; + goto end; +cleanup: + virtio_media_vma_close_locked(vma); +end: + mutex_unlock(&vv->vlock); + return ret; +} + static const struct v4l2_file_operations virtio_media_fops = { .owner = THIS_MODULE, .open = virtio_media_device_open, .release = virtio_media_device_close, .poll = virtio_media_device_poll, .unlocked_ioctl = virtio_media_device_ioctl, + .mmap = virtio_media_device_mmap, }; static void virtio_media_release(struct v4l2_device *v4l2_dev) @@ -779,6 +932,14 @@ static int virtio_media_probe(struct virtio_device *virtio_dev) vv->eventq = vqs[1]; INIT_WORK(&vv->eventq_work, virtio_media_event_work); + /* Get MMAP buffer mapping SHM region */ + if (!virtio_get_shm_region(virtio_dev, &vv->mmap_region, + VIRTIO_MEDIA_SHM_MMAP)) { + dev_err(&virtio_dev->dev, "Failed to get shared memory region"); + ret = -ENOMEM; + goto err_del_vqs; + } + vd = &vv->video_dev; vd->v4l2_dev = &vv->v4l2_dev; vd->ioctl_ops = &virtio_media_ioctl_ops; diff --git a/include/uapi/linux/virtio_media.h b/include/uapi/linux/virtio_media.h index c7a54abb9..b7994d858 100644 --- a/include/uapi/linux/virtio_media.h +++ b/include/uapi/linux/virtio_media.h @@ -171,6 +171,73 @@ struct virtio_media_sg_entry { __le32 __reserved; }; +/** + * VIRTIO_MEDIA_MMAP_FLAG_RW - Bit position of the VIRTIO_MEDIA_MMAP_FLAG_RW + * flag. + */ +#define VIRTIO_MEDIA_MMAP_FLAG_RW 0 + +/** + * VIRTIO_MEDIA_CMD_MMAP - Command for mapping a MMAP buffer into the driver's + * address space. + */ +#define VIRTIO_MEDIA_CMD_MMAP 4 + +/** + * struct virtio_media_cmd_mmap - Driver command for VIRTIO_MEDIA_CMD_MMAP. + * @hdr: header with cmd member set to VIRTIO_MEDIA_CMD_MMAP. + * @session_id: ID of the session we are mapping for. + * @flags: combination of VIRTIO_MEDIA_MMAP_FLAG_*. + * @offset: mem_offset field of the plane to map, as returned by + * VIDIOC_QUERYBUF. + * @__reserved: must be set to zero by the driver. + */ +struct virtio_media_cmd_mmap { + struct virtio_media_cmd_header hdr; + __le32 session_id; + __le32 flags; + __le32 offset; + __le32 __reserved; +}; + +/** + * struct virtio_media_resp_mmap - Device response for VIRTIO_MEDIA_CMD_MMAP. + * @hdr: header containing the status of the command. + * @driver_addr: offset into SHM region 0 of the start of the mapping. + * @len: length of the mapping. + */ +struct virtio_media_resp_mmap { + struct virtio_media_resp_header hdr; + __le64 driver_addr; + __le64 len; +}; + +/** + * VIRTIO_MEDIA_CMD_MUNMAP - Unmap a MMAP buffer previously mapped using + * VIRTIO_MEDIA_CMD_MMAP. + */ +#define VIRTIO_MEDIA_CMD_MUNMAP 5 + +/** + * struct virtio_media_cmd_munmap - Driver command for VIRTIO_MEDIA_CMD_MUNMAP. + * @hdr: header with cmd member set to VIRTIO_MEDIA_CMD_MUNMAP. + * @driver_addr: offset into SHM region 0 at which the buffer has been + * previously mapped. + */ +struct virtio_media_cmd_munmap { + struct virtio_media_cmd_header hdr; + __le64 driver_addr; +}; + +/** + * struct virtio_media_resp_munmap - Device response for + * VIRTIO_MEDIA_CMD_MUNMAP. + * @hdr: header containing the status of the command. + */ +struct virtio_media_resp_munmap { + struct virtio_media_resp_header hdr; +}; + /* The values for these events are set by the virtio-media specification. */ #define VIRTIO_MEDIA_EVT_ERROR 0 #define VIRTIO_MEDIA_EVT_DQBUF 1 -- 2.56.1.911.g883d8bcb20-goog