From nobody Mon Feb 9 14:33:18 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass header.i=dmitry.osipenko@collabora.com; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; arc=pass (i=1 dmarc=pass fromdomain=collabora.com); dmarc=pass(p=none dis=none) header.from=collabora.com ARC-Seal: i=2; a=rsa-sha256; t=1764038247; cv=pass; d=zohomail.com; s=zohoarc; b=EMu1AgIyaSU+uU1l0+wMG6K9FhhBcmwO1VywFlnKMmTklcKGyPKEib5yTCfT1QrdRD4gOvwZQMCuUN6zWRAOZvvNvdhYB7/T9SziHns/jUbN+Nkj0qcGFI8Hykh8drGqwIy67nr1PxgwyOQcod+IpYYZM3ZnOgmu2RtCoG9gZrM= ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1764038247; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=qYsTCziv1rWIzyxYEug6rTq2AvTzUYRi9nyYR5kx0ow=; b=J4fEYOjFvI4LidCaMQv8/ax/WDEPth0CJ2V0ccv9t8ctgsEXecuAc3elKNWvGr+6G0ahV35cmLhUcZxO4NYj8nSFUaGW8eNtfWSTaLFKhXlEnQJXDLwB8Bkjrf/N9dPqIIb/3lIs8NltuSdpaR4Rb8ItRg97/3YK/V2A15NrQ20= ARC-Authentication-Results: i=2; mx.zohomail.com; dkim=pass header.i=dmitry.osipenko@collabora.com; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; arc=pass (i=1 dmarc=pass fromdomain=collabora.com); dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1764038247053737.4127719883943; Mon, 24 Nov 2025 18:37:27 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1vNivM-0004Bv-Rm; Mon, 24 Nov 2025 21:37:04 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1vNivE-0004Ab-1j for qemu-devel@nongnu.org; Mon, 24 Nov 2025 21:36:52 -0500 Received: from sender3-pp-f112.zoho.com ([136.143.184.112]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1vNivC-0001FC-8B for qemu-devel@nongnu.org; Mon, 24 Nov 2025 21:36:51 -0500 Received: by mx.zohomail.com with SMTPS id 1764038194356208.33316025653096; Mon, 24 Nov 2025 18:36:34 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1764038196; cv=none; d=zohomail.com; s=zohoarc; b=EN/UK/cZhsUPdVsbDF1ca/Zl1RRLiXNvYJbjKa7FM/He9S8JlTsxAqpoiEaUWMKLw7QAkvkgZvJyvWMYkE5xfFInraF8klEzWK6cgkzmNdLwxtCJQoTrq2e7fiI3/lU32BHcWRm0x3WFMAImoVeWMrYsdjYFBSIY3H+9gK7GTzI= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1764038196; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:MIME-Version:Message-ID:References:Subject:Subject:To:To:Message-Id:Reply-To; bh=qYsTCziv1rWIzyxYEug6rTq2AvTzUYRi9nyYR5kx0ow=; b=NIegVHuRYe9USEOYG2HBauvC1TOVOyJv87Ji+7nw4wQgI8W4hLx4UKIDM7vMDjKpReuLfkFhrNjF+YPlzDWrnkZMBYDQZ108qmtODTnIWsQakCFxiNYcOcCHFm2SEcuu+ZV2hYM5LbkSN1DBnXeaYx3e2j6vcK8IuVdKo9GAA5g= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass header.i=collabora.com; spf=pass smtp.mailfrom=dmitry.osipenko@collabora.com; dmarc=pass header.from= DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; t=1764038196; s=zohomail; d=collabora.com; i=dmitry.osipenko@collabora.com; h=From:From:To:To:Cc:Cc:Subject:Subject:Date:Date:Message-ID:In-Reply-To:References:MIME-Version:Content-Transfer-Encoding:Message-Id:Reply-To; bh=qYsTCziv1rWIzyxYEug6rTq2AvTzUYRi9nyYR5kx0ow=; b=QVko3cGoznJhf1Riqpwd5zXWpKaDnOU8uYE+NJyLKFp1g81dz5fI6u24G9JE3Mf0 VXHmbVzKkdltyA3oWFoZGKch/agcxwrhO0k94H3KUIstUNWN1aPVMnaFpdPS422u7sj 5sH24QcjKxgSWKNDpmOQl9iXmCvmpYUgC2MqGyjE= From: Dmitry Osipenko To: Akihiko Odaki , Huang Rui , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Gerd Hoffmann , =?UTF-8?q?Alex=20Benn=C3=A9e?= , Pierre-Eric Pelloux-Prayer , "Michael S . Tsirkin" , Paolo Bonzini , Yiwei Zhang , Sergio Lopez Pascual Cc: Gert Wollny , qemu-devel@nongnu.org, Gurchetan Singh , Alyssa Ross , =?UTF-8?q?Roger=20Pau=20Monn=C3=A9?= , Alex Deucher , Stefano Stabellini , =?UTF-8?q?Christian=20K=C3=B6nig?= , Xenia Ragiadakou , Honglei Huang , Julia Zhang , Chen Jiqian , Rob Clark , Robert Beckett Subject: [RFC PATCH v4 7/7] virtio-gpu: Support mapping hostmem blobs with map_fixed Date: Tue, 25 Nov 2025 05:35:17 +0300 Message-ID: <20251125023517.177714-8-dmitry.osipenko@collabora.com> X-Mailer: git-send-email 2.51.1 In-Reply-To: <20251125023517.177714-1-dmitry.osipenko@collabora.com> References: <20251125023517.177714-1-dmitry.osipenko@collabora.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMailClient: External Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=136.143.184.112; envelope-from=dmitry.osipenko@collabora.com; helo=sender3-pp-f112.zoho.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, RCVD_IN_VALIDITY_CERTIFIED_BLOCKED=0.001, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity dmitry.osipenko@collabora.com) X-ZM-MESSAGEID: 1764038248330019202 Content-Type: text/plain; charset="utf-8" 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 Signed-off-by: Dmitry Osipenko --- hw/display/virtio-gpu-gl.c | 41 +++++++++++++++++++++++- hw/display/virtio-gpu-virgl.c | 58 +++++++++++++++++++++++++++++++++- include/hw/virtio/virtio-gpu.h | 3 ++ 3 files changed, 100 insertions(+), 2 deletions(-) diff --git a/hw/display/virtio-gpu-gl.c b/hw/display/virtio-gpu-gl.c index d65da4863923..8a7a33946085 100644 --- a/hw/display/virtio-gpu-gl.c +++ b/hw/display/virtio-gpu-gl.c @@ -13,6 +13,7 @@ =20 #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" @@ -124,7 +125,12 @@ static void virtio_gpu_gl_reset(VirtIODevice *vdev) static void virtio_gpu_gl_device_realize(DeviceState *qdev, Error **errp) { ERRP_GUARD(); - VirtIOGPU *g =3D VIRTIO_GPU(qdev); + VirtIOGPUBase *b =3D VIRTIO_GPU_BASE(qdev); + VirtIOGPU *g =3D VIRTIO_GPU(b); +#if !defined(CONFIG_WIN32) + VirtIOGPUGL *gl =3D VIRTIO_GPU_GL(g); + void *map; +#endif =20 #if HOST_BIG_ENDIAN error_setg(errp, "virgl is not supported on bigendian platforms"); @@ -155,6 +161,28 @@ static void virtio_gpu_gl_device_realize(DeviceState *= qdev, Error **errp) #endif =20 virtio_gpu_device_realize(qdev, errp); + if (*errp) { + return; + } + +#if !defined(CONFIG_WIN32) + if (virtio_gpu_hostmem_enabled(b->conf)) { + map =3D qemu_ram_mmap(-1, b->conf.hostmem, qemu_real_host_page_siz= e(), + 0, 0); + if (map =3D=3D MAP_FAILED) { + error_setg(errp, + "virgl hostmem region could not be initialized: %s", + strerror(errno)); + return; + } + + gl->hostmem_mmap =3D 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_backgroun= d); + } +#endif } =20 static const Property virtio_gpu_gl_properties[] =3D { @@ -190,6 +218,17 @@ static void virtio_gpu_gl_device_unrealize(DeviceState= *qdev) gl->renderer_state =3D RS_START; =20 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 unnecess= ary + * because b->hostmem and gl->hostmem_background belong to the same + * device and will be gone at the same time. + */ } =20 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 9b36b378c2fd..987980e8a49d 100644 --- a/hw/display/virtio-gpu-virgl.c +++ b/hw/display/virtio-gpu-virgl.c @@ -41,9 +41,13 @@ VIRGL_VERSION_MICRO >=3D (micro)) #endif =20 +#define VIRGL_HAS_MAP_FIXED \ + (VIRGL_CHECK_VERSION(1, 2, 1) && !IS_ENABLED(CONFIG_WIN32)) + struct virtio_gpu_virgl_resource { struct virtio_gpu_simple_resource base; MemoryRegion *mr; + void *map_fixed; }; =20 static struct virtio_gpu_virgl_resource * @@ -116,6 +120,9 @@ virtio_gpu_virgl_map_resource_blob(VirtIOGPU *g, { struct virtio_gpu_virgl_hostmem_region *vmr; VirtIOGPUBase *b =3D VIRTIO_GPU_BASE(g); +#if VIRGL_HAS_MAP_FIXED + VirtIOGPUGL *gl =3D VIRTIO_GPU_GL(g); +#endif MemoryRegion *mr; uint64_t size; void *data; @@ -134,6 +141,41 @@ virtio_gpu_virgl_map_resource_blob(VirtIOGPU *g, return -EINVAL; } =20 +#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 ma= pped\n", + __func__); + return -EBUSY; + } + + ret =3D virgl_renderer_resource_map_fixed(res->base.resource_id, + gl->hostmem_mmap + offset); + switch (ret) { + case 0: + res->map_fixed =3D 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 -1; + } +#endif + ret =3D virgl_renderer_resource_map(res->base.resource_id, &data, &siz= e); if (ret) { qemu_log_mask(LOG_GUEST_ERROR, "%s: failed to map virgl resource: = %s\n", @@ -146,7 +188,7 @@ virtio_gpu_virgl_map_resource_blob(VirtIOGPU *g, =20 mr =3D &vmr->mr; memory_region_init_ram_ptr(mr, OBJECT(mr), "blob", size, data); - memory_region_add_subregion(&b->hostmem, offset, mr); + memory_region_add_subregion_overlap(&b->hostmem, offset, mr, 1); =20 /* * MR could outlive the resource if MR's reference is held outside of @@ -173,6 +215,20 @@ virtio_gpu_virgl_unmap_resource_blob(VirtIOGPU *g, MemoryRegion *mr =3D res->mr; int ret; =20 +#if VIRGL_HAS_MAP_FIXED + if (res->map_fixed) { + if (mmap(res->map_fixed, res->base.blob_size, PROT_READ | PROT_WRI= TE, + MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED, + -1, 0) =3D=3D MAP_FAILED) { + error_report("%s: failed to unmap(fixed) virgl resource: %s", + __func__, strerror(errno)); + return -1; + } + + res->map_fixed =3D NULL; + } +#endif + if (!mr) { return 0; } diff --git a/include/hw/virtio/virtio-gpu.h b/include/hw/virtio/virtio-gpu.h index 9e1473d1bb66..420c6e2a2515 100644 --- a/include/hw/virtio/virtio-gpu.h +++ b/include/hw/virtio/virtio-gpu.h @@ -263,6 +263,9 @@ struct VirtIOGPUGL { =20 QEMUBH *async_fence_bh; QSLIST_HEAD(, virtio_gpu_virgl_context_fence) async_fenceq; + + MemoryRegion hostmem_background; + void *hostmem_mmap; }; =20 struct VhostUserGPU { --=20 2.51.1