From nobody Thu Nov 20 14:04:05 2025 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=1763611746; cv=pass; d=zohomail.com; s=zohoarc; b=lGlOI5OO5iKmEkKqYDwzsrKPnqaPmAw/Qlz8EnLt33vSTiZxeTMzWs74HghhxGdq0jw9ea56m50f/WaPkuMDQY5c9DFaVj8godbA9VCa49ue1xNdHtKUaDTdolvJorj+swz0pi1ZTcQGtd4Kr0PTxdbNbTlLUfBcr2fnv/9QhHY= ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1763611746; 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=34/rI6ayV66+i8J7+YXq/OWo+uOrrIkWLj9aObPDzLo=; b=LTMb9gDnzSeQt6sjiDXgEtoPumakHbj6jG9FOSsadJ6Gh+Bhis12feoM4V+kC9AAuKmZBLYS3AzDWCi/LDyoKyHy7+QKzhAyLKoNRoHHEarp9NG8cbYjGfrBPNsDuHr1JEi/FF6iI3lokLs8WT8LK0cfrPH6G0N0DXKqCDDNPHc= 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 1763611746427742.4058656707049; Wed, 19 Nov 2025 20:09:06 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1vLvyG-0001BK-M7; Wed, 19 Nov 2025 23:08:36 -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 1vLvyC-0001Ae-Mo for qemu-devel@nongnu.org; Wed, 19 Nov 2025 23:08:32 -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 1vLvy8-0000ap-4D for qemu-devel@nongnu.org; Wed, 19 Nov 2025 23:08:30 -0500 Received: by mx.zohomail.com with SMTPS id 1763611691208788.0479786596771; Wed, 19 Nov 2025 20:08:11 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1763611693; cv=none; d=zohomail.com; s=zohoarc; b=J1ZhZUxjZR3RxayYI67GLTx1g5ZZWB2X9nfZV1mkod4+nYFFn3eWWxGNJZVd0gUqPMpLwJGhRhrMMj6tS2PyGKtwmJ+BVUlFxxPUE0MAC90qchiOpprUz98uvrvxIY+Xz0UZM0KztN9lfSUsdBEFNll6Jl0csU/SycvoE75Erzw= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1763611693; 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=34/rI6ayV66+i8J7+YXq/OWo+uOrrIkWLj9aObPDzLo=; b=bUxYdXXJFEM6HtUYttuLt/mmhktNj2tffGk8+gmQIVoA5DnHi45nVb2XI4qrRGDQUOdhw7T27lT4RedfDktL79GW8kBdqSPokfJP6Vf41JNj2PwzYnYXsjPPukXwugo7esnUPvP2t5bbw7CRqUtGlv9SLboKk0A0DOt+cysVtTA= 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=1763611693; 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=34/rI6ayV66+i8J7+YXq/OWo+uOrrIkWLj9aObPDzLo=; b=U2SXDhH5C0MO1JAo5v03aOiR9RY/l10voG05G1b3VrekVeU4iShkO1amH0TmPl/a Z4cMay4C50FCWSrkpYBQUktSHJ1pkqgLOFh55WU+LP8D9fxw0fRzNKLo6svTrjLm1jB Of5eH3JGglNbUR+kSWPuRJ1HkvbUjvZi0P0QgxWA= 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 v3 5/7] virtio-gpu: Destroy virgl resources on virtio-gpu reset Date: Thu, 20 Nov 2025 07:06:30 +0300 Message-ID: <20251120040632.4036204-6-dmitry.osipenko@collabora.com> X-Mailer: git-send-email 2.51.1 In-Reply-To: <20251120040632.4036204-1-dmitry.osipenko@collabora.com> References: <20251120040632.4036204-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: 1763611747669018900 Content-Type: text/plain; charset="utf-8" Properly destroy virgl resources on virtio-gpu reset to not leak resources on a hot reboot of a VM. Suggested-by: Akihiko Odaki Signed-off-by: Dmitry Osipenko --- hw/display/virtio-gpu-gl.c | 6 ++- hw/display/virtio-gpu-virgl.c | 79 +++++++++++++++++++++++++--------- include/hw/virtio/virtio-gpu.h | 5 ++- 3 files changed, 67 insertions(+), 23 deletions(-) diff --git a/hw/display/virtio-gpu-gl.c b/hw/display/virtio-gpu-gl.c index b640900fc6f1..bca7d489c1e3 100644 --- a/hw/display/virtio-gpu-gl.c +++ b/hw/display/virtio-gpu-gl.c @@ -72,7 +72,10 @@ static void virtio_gpu_gl_handle_ctrl(VirtIODevice *vdev= , VirtQueue *vq) =20 switch (gl->renderer_state) { case RS_RESET: - virtio_gpu_virgl_reset(g); + if (virtio_gpu_virgl_reset(g) < 0) { + gl->renderer_state =3D RS_INIT_FAILED; + return; + } /* fallthrough */ case RS_START: if (virtio_gpu_virgl_init(g)) { @@ -201,6 +204,7 @@ static void virtio_gpu_gl_class_init(ObjectClass *klass= , const void *data) vgc->process_cmd =3D virtio_gpu_virgl_process_cmd; vgc->update_cursor_data =3D virtio_gpu_gl_update_cursor_data; =20 + vgc->resource_destroy =3D virtio_gpu_virgl_resource_destroy; vdc->realize =3D virtio_gpu_gl_device_realize; vdc->unrealize =3D virtio_gpu_gl_device_unrealize; vdc->reset =3D virtio_gpu_gl_reset; diff --git a/hw/display/virtio-gpu-virgl.c b/hw/display/virtio-gpu-virgl.c index ae8de8ee47da..24d329022da9 100644 --- a/hw/display/virtio-gpu-virgl.c +++ b/hw/display/virtio-gpu-virgl.c @@ -311,14 +311,44 @@ static void virgl_cmd_create_resource_3d(VirtIOGPU *g, virgl_renderer_resource_create(&args, NULL, 0); } =20 +static int +virtio_gpu_virgl_resource_unref(VirtIOGPU *g, + struct virtio_gpu_virgl_resource *res, + bool *cmd_suspended) +{ + struct iovec *res_iovs =3D NULL; + int num_iovs =3D 0; + +#if VIRGL_VERSION_MAJOR >=3D 1 + if (virtio_gpu_virgl_unmap_resource_blob(g, res, cmd_suspended) < 0) { + return -1; + } + if (*cmd_suspended) { + return 0; + } +#endif + + virgl_renderer_resource_detach_iov(res->base.resource_id, + &res_iovs, + &num_iovs); + if (res_iovs !=3D NULL && num_iovs !=3D 0) { + virtio_gpu_cleanup_mapping_iov(g, res_iovs, num_iovs); + } + virgl_renderer_resource_unref(res->base.resource_id); + + QTAILQ_REMOVE(&g->reslist, &res->base, next); + + g_free(res); + + return 0; +} + static void virgl_cmd_resource_unref(VirtIOGPU *g, struct virtio_gpu_ctrl_command *cmd, bool *cmd_suspended) { struct virtio_gpu_resource_unref unref; struct virtio_gpu_virgl_resource *res; - struct iovec *res_iovs =3D NULL; - int num_iovs =3D 0; =20 VIRTIO_GPU_FILL_CMD(unref); trace_virtio_gpu_cmd_res_unref(unref.resource_id); @@ -331,27 +361,21 @@ static void virgl_cmd_resource_unref(VirtIOGPU *g, return; } =20 -#if VIRGL_VERSION_MAJOR >=3D 1 - if (virtio_gpu_virgl_unmap_resource_blob(g, res, cmd_suspended) < 0) { - cmd->error =3D VIRTIO_GPU_RESP_ERR_UNSPEC; - return; - } - if (*cmd_suspended) { - return; - } -#endif + virtio_gpu_virgl_resource_unref(g, res, cmd_suspended); +} =20 - virgl_renderer_resource_detach_iov(unref.resource_id, - &res_iovs, - &num_iovs); - if (res_iovs !=3D NULL && num_iovs !=3D 0) { - virtio_gpu_cleanup_mapping_iov(g, res_iovs, num_iovs); - } - virgl_renderer_resource_unref(unref.resource_id); +void virtio_gpu_virgl_resource_destroy(VirtIOGPU *g, + struct virtio_gpu_simple_resource *= base, + Error **errp) +{ + struct virtio_gpu_virgl_resource *res; + bool suspended =3D false; =20 - QTAILQ_REMOVE(&g->reslist, &res->base, next); + res =3D container_of(base, struct virtio_gpu_virgl_resource, base); =20 - g_free(res); + if (virtio_gpu_virgl_resource_unref(g, res, &suspended) < 0) { + error_setg(errp, "failed to destroy virgl resource"); + } } =20 static void virgl_cmd_context_create(VirtIOGPU *g, @@ -1270,11 +1294,24 @@ void virtio_gpu_virgl_reset_scanout(VirtIOGPU *g) } } =20 -void virtio_gpu_virgl_reset(VirtIOGPU *g) +int virtio_gpu_virgl_reset(VirtIOGPU *g) { + struct virtio_gpu_simple_resource *res, *tmp; + + QTAILQ_FOREACH_SAFE(res, &g->reslist, next, tmp) { + virtio_gpu_virgl_resource_destroy(g, res, NULL); + } + + if (!QTAILQ_EMPTY(&g->reslist)) { + error_report("failed to reset virgl resources"); + return -1; + } + virgl_renderer_reset(); =20 virtio_gpu_virgl_reset_async_fences(g); + + return 0; } =20 int virtio_gpu_virgl_init(VirtIOGPU *g) diff --git a/include/hw/virtio/virtio-gpu.h b/include/hw/virtio/virtio-gpu.h index 718332284305..9e1473d1bb66 100644 --- a/include/hw/virtio/virtio-gpu.h +++ b/include/hw/virtio/virtio-gpu.h @@ -389,9 +389,12 @@ void virtio_gpu_virgl_process_cmd(VirtIOGPU *g, struct virtio_gpu_ctrl_command *cmd); void virtio_gpu_virgl_fence_poll(VirtIOGPU *g); void virtio_gpu_virgl_reset_scanout(VirtIOGPU *g); -void virtio_gpu_virgl_reset(VirtIOGPU *g); +int virtio_gpu_virgl_reset(VirtIOGPU *g); int virtio_gpu_virgl_init(VirtIOGPU *g); GArray *virtio_gpu_virgl_get_capsets(VirtIOGPU *g); void virtio_gpu_virgl_reset_async_fences(VirtIOGPU *g); +void virtio_gpu_virgl_resource_destroy(VirtIOGPU *g, + struct virtio_gpu_simple_resource *= res, + Error **errp); =20 #endif --=20 2.51.1