From: Dmitry Osipenko <dmitry.osipenko@collabora.com>
To: "Akihiko Odaki" <odaki@rsg.ci.i.u-tokyo.ac.jp>,
"Huang Rui" <ray.huang@amd.com>,
"Marc-André Lureau" <marcandre.lureau@redhat.com>,
"Philippe Mathieu-Daudé" <philmd@linaro.org>,
"Gerd Hoffmann" <kraxel@redhat.com>,
"Alex Bennée" <alex.bennee@linaro.org>,
"Pierre-Eric Pelloux-Prayer" <pierre-eric.pelloux-prayer@amd.com>,
"Michael S . Tsirkin" <mst@redhat.com>,
"Paolo Bonzini" <pbonzini@redhat.com>,
"Yiwei Zhang" <zzyiwei@gmail.com>,
"Sergio Lopez Pascual" <slp@redhat.com>
Cc: "Gert Wollny" <gert.wollny@collabora.com>,
qemu-devel@nongnu.org,
"Gurchetan Singh" <gurchetansingh@chromium.org>,
"Alyssa Ross" <hi@alyssa.is>,
"Roger Pau Monné" <roger.pau@citrix.com>,
"Alex Deucher" <alexander.deucher@amd.com>,
"Stefano Stabellini" <stefano.stabellini@amd.com>,
"Christian König" <christian.koenig@amd.com>,
"Xenia Ragiadakou" <xenia.ragiadakou@amd.com>,
"Honglei Huang" <honglei1.huang@amd.com>,
"Julia Zhang" <julia.zhang@amd.com>,
"Chen Jiqian" <Jiqian.Chen@amd.com>,
"Rob Clark" <robdclark@gmail.com>,
"Robert Beckett" <bob.beckett@collabora.com>
Subject: [PATCH v20 21/21] virtio-gpu: Support mapping hostmem blobs with map_fixed
Date: Sun, 1 Mar 2026 23:48:38 +0300 [thread overview]
Message-ID: <20260301204838.596994-22-dmitry.osipenko@collabora.com> (raw)
In-Reply-To: <20260301204838.596994-1-dmitry.osipenko@collabora.com>
Support mapping virgl blobs to a fixed location of a hostmem memory
region using new virglrenderer MAP_FIXED API.
This new feature closes multiple problems for virtio-gpu on QEMU:
- Having dedicated memory region for each mapped blob works notoriously
slow due to QEMU's memory region software design built around RCU that
isn't optimized for frequent removal of the regions
- KVM isn't optimized for a frequent slot changes too
- QEMU/KVM has a limit for a total number of created memory regions,
crashing QEMU when limit is reached
This patch makes virtio-gpu-gl to pre-create a single anonymous memory
region covering whole hostmem area to which blobs will be mapped using
the MAP_FIXED API.
Not all virgl resources will support mapping at a fixed memory address. For
them, we will continue to create individual nested memory sub-regions. In
particular, vrend resources may not have MAP_FIXED capability.
Venus and DRM native contexts will largely benefit from the MAP_FIXED
feature in terms of performance and stability improvement.
Tested-by: Yiwei Zhang <zzyiwei@gmail.com>
Signed-off-by: Dmitry Osipenko <dmitry.osipenko@collabora.com>
---
hw/display/virtio-gpu-gl.c | 37 ++++++++++++++++++++-
hw/display/virtio-gpu-virgl.c | 59 +++++++++++++++++++++++++++++++++-
include/hw/virtio/virtio-gpu.h | 3 ++
3 files changed, 97 insertions(+), 2 deletions(-)
diff --git a/hw/display/virtio-gpu-gl.c b/hw/display/virtio-gpu-gl.c
index 8b71dd6fc26f..2b7a41c46643 100644
--- a/hw/display/virtio-gpu-gl.c
+++ b/hw/display/virtio-gpu-gl.c
@@ -13,6 +13,7 @@
#include "qemu/osdep.h"
#include "qemu/iov.h"
+#include "qemu/mmap-alloc.h"
#include "qemu/module.h"
#include "qemu/error-report.h"
#include "qapi/error.h"
@@ -106,7 +107,12 @@ static void virtio_gpu_gl_reset(VirtIODevice *vdev)
static void virtio_gpu_gl_device_realize(DeviceState *qdev, Error **errp)
{
ERRP_GUARD();
- VirtIOGPU *g = VIRTIO_GPU(qdev);
+ VirtIOGPUBase *b = VIRTIO_GPU_BASE(qdev);
+ VirtIOGPU *g = VIRTIO_GPU(b);
+#if !defined(CONFIG_WIN32)
+ VirtIOGPUGL *gl = VIRTIO_GPU_GL(g);
+ void *map;
+#endif
#if HOST_BIG_ENDIAN
error_setg(errp, "virgl is not supported on bigendian platforms");
@@ -136,6 +142,24 @@ static void virtio_gpu_gl_device_realize(DeviceState *qdev, Error **errp)
g->parent_obj.conf.flags |= 1 << VIRTIO_GPU_FLAG_CONTEXT_INIT_ENABLED;
#endif
+#if !defined(CONFIG_WIN32)
+ if (virtio_gpu_hostmem_enabled(b->conf)) {
+ map = qemu_ram_mmap(-1, b->conf.hostmem, qemu_real_host_page_size(),
+ 0, 0);
+ if (map == MAP_FAILED) {
+ error_setg_errno(errp, errno,
+ "virgl hostmem region could not be initialized");
+ return;
+ }
+
+ gl->hostmem_mmap = map;
+ memory_region_init_ram_ptr(&gl->hostmem_background, NULL,
+ "hostmem-background", b->conf.hostmem,
+ gl->hostmem_mmap);
+ memory_region_add_subregion(&b->hostmem, 0, &gl->hostmem_background);
+ }
+#endif
+
virtio_gpu_device_realize(qdev, errp);
}
@@ -172,6 +196,17 @@ static void virtio_gpu_gl_device_unrealize(DeviceState *qdev)
gl->renderer_state = RS_START;
g_array_unref(g->capset_ids);
+
+ /*
+ * It is not guaranteed that the memory region will be finalized
+ * immediately with memory_region_del_subregion(), there can be
+ * a remaining reference to gl->hostmem_mmap. VirtIO-GPU is not
+ * hotpluggable, hence no need to worry about the leaked mapping.
+ *
+ * The memory_region_del_subregion(gl->hostmem_background) is unnecessary
+ * because b->hostmem and gl->hostmem_background belong to the same
+ * device and will be gone at the same time.
+ */
}
static void virtio_gpu_gl_class_init(ObjectClass *klass, const void *data)
diff --git a/hw/display/virtio-gpu-virgl.c b/hw/display/virtio-gpu-virgl.c
index f0441470b98a..8617ba5222fd 100644
--- a/hw/display/virtio-gpu-virgl.c
+++ b/hw/display/virtio-gpu-virgl.c
@@ -41,9 +41,13 @@
VIRGL_VERSION_MICRO >= (micro))
#endif
+#define VIRGL_HAS_MAP_FIXED \
+ (VIRGL_CHECK_VERSION(1, 3, 0) && !IS_ENABLED(CONFIG_WIN32))
+
struct virtio_gpu_virgl_resource {
struct virtio_gpu_simple_resource base;
MemoryRegion *mr;
+ void *map_fixed;
};
static struct virtio_gpu_virgl_resource *
@@ -155,6 +159,9 @@ virtio_gpu_virgl_map_resource_blob(VirtIOGPU *g,
g_autofree char *name = NULL;
struct virtio_gpu_virgl_hostmem_region *vmr;
VirtIOGPUBase *b = VIRTIO_GPU_BASE(g);
+#if VIRGL_HAS_MAP_FIXED
+ VirtIOGPUGL *gl = VIRTIO_GPU_GL(g);
+#endif
MemoryRegion *mr;
uint64_t size;
void *data;
@@ -165,6 +172,41 @@ virtio_gpu_virgl_map_resource_blob(VirtIOGPU *g,
return -EOPNOTSUPP;
}
+#if VIRGL_HAS_MAP_FIXED
+ /*
+ * virgl_renderer_resource_map_fixed() allows to create multiple
+ * mappings of the same resource, while virgl_renderer_resource_map()
+ * not. Don't allow mapping same resource twice.
+ */
+ if (res->map_fixed || res->mr) {
+ qemu_log_mask(LOG_GUEST_ERROR,
+ "%s: failed to map(fixed) virgl resource: already mapped\n",
+ __func__);
+ return -EBUSY;
+ }
+
+ ret = virgl_renderer_resource_map_fixed(res->base.resource_id,
+ gl->hostmem_mmap + offset);
+ switch (ret) {
+ case 0:
+ res->map_fixed = gl->hostmem_mmap + offset;
+ return 0;
+
+ case -EOPNOTSUPP:
+ /*
+ * MAP_FIXED is unsupported by this resource.
+ * Mapping falls back to a blob subregion method in that case.
+ */
+ break;
+
+ default:
+ qemu_log_mask(LOG_GUEST_ERROR,
+ "%s: failed to map(fixed) virgl resource: %s\n",
+ __func__, strerror(-ret));
+ return ret;
+ }
+#endif
+
ret = virgl_renderer_resource_map(res->base.resource_id, &data, &size);
if (ret) {
qemu_log_mask(LOG_GUEST_ERROR, "%s: failed to map virgl resource: %s\n",
@@ -181,7 +223,7 @@ virtio_gpu_virgl_map_resource_blob(VirtIOGPU *g,
mr = &vmr->mr;
memory_region_init_ram_ptr(mr, OBJECT(vmr), "mr", size, data);
- memory_region_add_subregion(&b->hostmem, offset, mr);
+ memory_region_add_subregion_overlap(&b->hostmem, offset, mr, 1);
res->mr = mr;
@@ -200,6 +242,21 @@ virtio_gpu_virgl_unmap_resource_blob(VirtIOGPU *g,
MemoryRegion *mr = res->mr;
int ret;
+#if VIRGL_HAS_MAP_FIXED
+ if (res->map_fixed) {
+ if (mmap(res->map_fixed, res->base.blob_size, PROT_READ | PROT_WRITE,
+ MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED,
+ -1, 0) == MAP_FAILED) {
+ ret = -errno;
+ error_report("%s: failed to unmap(fixed) virgl resource: %s",
+ __func__, strerror(-ret));
+ return ret;
+ }
+
+ res->map_fixed = NULL;
+ }
+#endif
+
if (!mr) {
return 0;
}
diff --git a/include/hw/virtio/virtio-gpu.h b/include/hw/virtio/virtio-gpu.h
index 8f29062eaa59..6126811ad32e 100644
--- a/include/hw/virtio/virtio-gpu.h
+++ b/include/hw/virtio/virtio-gpu.h
@@ -265,6 +265,9 @@ struct VirtIOGPUGL {
QEMUBH *async_fence_bh;
QSLIST_HEAD(, virtio_gpu_virgl_context_fence) async_fenceq;
+
+ MemoryRegion hostmem_background;
+ void *hostmem_mmap;
};
struct VhostUserGPU {
--
2.52.0
prev parent reply other threads:[~2026-03-01 20:52 UTC|newest]
Thread overview: 27+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-03-01 20:48 [PATCH v20 00/21] Support virtio-gpu DRM native context and MAP_FIXED API Dmitry Osipenko
2026-03-01 20:48 ` [PATCH v20 01/21] ui/gtk: Don't disable scanout when display is refreshed Dmitry Osipenko
2026-03-01 20:48 ` [PATCH v20 02/21] ui/gtk: Restore original context after new context creation Dmitry Osipenko
2026-03-02 5:58 ` Akihiko Odaki
2026-03-02 12:01 ` Dmitry Osipenko
2026-03-01 20:48 ` [PATCH v20 03/21] ui/gdk: " Dmitry Osipenko
2026-03-02 6:17 ` Akihiko Odaki
2026-03-01 20:48 ` [PATCH v20 04/21] ui/sdl2: Don't disable scanout when display is refreshed Dmitry Osipenko
2026-03-01 20:48 ` [PATCH v20 05/21] ui/sdl2: Restore original context after new context creation Dmitry Osipenko
2026-03-01 20:48 ` [PATCH v20 06/21] ui/sdl2: Implement dpy dmabuf functions Dmitry Osipenko
2026-03-01 20:48 ` [PATCH v20 07/21] ui/egl-headless: Restore original context after new context creation Dmitry Osipenko
2026-03-01 20:48 ` [PATCH v20 08/21] ui/spice: " Dmitry Osipenko
2026-03-01 20:48 ` [PATCH v20 09/21] ui/dbus: " Dmitry Osipenko
2026-03-01 20:48 ` [PATCH v20 10/21] virtio-gpu: Bounce virtio_gpu_ctrl_bh() to main-loop thread Dmitry Osipenko
2026-03-02 5:57 ` Akihiko Odaki
2026-03-02 11:59 ` Dmitry Osipenko
2026-03-01 20:48 ` [PATCH v20 11/21] virtio-gpu: Handle virgl fence creation errors Dmitry Osipenko
2026-03-01 20:48 ` [PATCH v20 12/21] virtio-gpu: Support asynchronous fencing Dmitry Osipenko
2026-03-01 20:48 ` [PATCH v20 13/21] virtio-gpu: Support DRM native context Dmitry Osipenko
2026-03-01 20:48 ` [PATCH v20 14/21] docs/system: virtio-gpu: Add link to Mesa VirGL doc Dmitry Osipenko
2026-03-01 20:48 ` [PATCH v20 15/21] docs/system: virtio-gpu: Update Venus link Dmitry Osipenko
2026-03-01 20:48 ` [PATCH v20 16/21] docs/system: virtio-gpu: Document host/guest requirements Dmitry Osipenko
2026-03-01 20:48 ` [PATCH v20 17/21] virtio-gpu: Remove superfluous memory_region_set_enabled() Dmitry Osipenko
2026-03-01 20:48 ` [PATCH v20 18/21] virtio-gpu: Validate hostmem mapping offset Dmitry Osipenko
2026-03-01 20:48 ` [PATCH v20 19/21] virtio-gpu: Replace finish_unmapping with mapping_state Dmitry Osipenko
2026-03-01 20:48 ` [PATCH v20 20/21] virtio-gpu: Destroy virgl resources on virtio-gpu reset Dmitry Osipenko
2026-03-01 20:48 ` Dmitry Osipenko [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=20260301204838.596994-22-dmitry.osipenko@collabora.com \
--to=dmitry.osipenko@collabora.com \
--cc=Jiqian.Chen@amd.com \
--cc=alex.bennee@linaro.org \
--cc=alexander.deucher@amd.com \
--cc=bob.beckett@collabora.com \
--cc=christian.koenig@amd.com \
--cc=gert.wollny@collabora.com \
--cc=gurchetansingh@chromium.org \
--cc=hi@alyssa.is \
--cc=honglei1.huang@amd.com \
--cc=julia.zhang@amd.com \
--cc=kraxel@redhat.com \
--cc=marcandre.lureau@redhat.com \
--cc=mst@redhat.com \
--cc=odaki@rsg.ci.i.u-tokyo.ac.jp \
--cc=pbonzini@redhat.com \
--cc=philmd@linaro.org \
--cc=pierre-eric.pelloux-prayer@amd.com \
--cc=qemu-devel@nongnu.org \
--cc=ray.huang@amd.com \
--cc=robdclark@gmail.com \
--cc=roger.pau@citrix.com \
--cc=slp@redhat.com \
--cc=stefano.stabellini@amd.com \
--cc=xenia.ragiadakou@amd.com \
--cc=zzyiwei@gmail.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 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.