From nobody Mon Sep 8 11:55:12 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass header.i=@intel.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; dmarc=pass(p=none dis=none) header.from=intel.com ARC-Seal: i=1; a=rsa-sha256; t=1756878502; cv=none; d=zohomail.com; s=zohoarc; b=cFrq3eewhbNxVgA6mUs1b8YZy/LiT5tDY1qUFSM6saXzaR2O7bdd6EGiabEdWBUu6l2pL9ganBtf+e//9QEp19uImv0DOpJUuLoOkOGFKLjUhoaPQXW57jmANA6kjZcrep46sN5b+6sC2PPrt4ajh1u6MXSwUT38mFVwTes9FO0= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1756878502; h=Content-Type: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=eHYJ2CAFolp7Bm+UoHdo2TUgYGvj4YVwMpmxcaXPiD8=; b=h0k3T2lH8vZ/YC81ch02hbx+CgaqauwNAwVboJ4zBdOAwyPpsorcWW9sfzAitiBVM7hZwgyecOmw6UAn8gEYJlLiEg7pQM64EsCo4wLul0/UAkimDSPbHCW9yVaRgnmAigOJ3zkpOChRmLRss2gAB1Jgm+F8XzjomiAjFk7kR5Q= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass header.i=@intel.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; 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 1756878502034615.1274636970011; Tue, 2 Sep 2025 22:48:22 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1utgLB-0005Vh-EH; Wed, 03 Sep 2025 01:47:29 -0400 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 1utgL2-0005ST-F9 for qemu-devel@nongnu.org; Wed, 03 Sep 2025 01:47:22 -0400 Received: from mgamail.intel.com ([198.175.65.20]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1utgL0-000206-3w for qemu-devel@nongnu.org; Wed, 03 Sep 2025 01:47:19 -0400 Received: from orviesa003.jf.intel.com ([10.64.159.143]) by orvoesa112.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 02 Sep 2025 22:47:09 -0700 Received: from vkasired-desk2.fm.intel.com ([10.105.128.132]) by ORVIESA003-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 02 Sep 2025 22:47:08 -0700 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1756878439; x=1788414439; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=nHIkY6xxQVZuAgYEJkhfdggVCrRA/bv+587dx6C5wpY=; b=bZe8AXlatlIXGKYw0aqcHG1tO7zjoRm0Yckf/tSEhJCELEvzLfiVuxtn T+mSAiBk8MhG32hEk0bzV39UekVxVHollO7GG9MiezNumLr8D0SsPSm4l 65WF1+DWJmrqTJRCbtJyiPOGDX+YW0WBsTrDcYubUNax/0soS0Qp7H3ax ZiNeFRiwbPfhAr4J+eS+55wB7xmR+e6fPckqWN12VXXvcxr2FDtYW7b2U 1LxA9g0yn7Iz/ApwoSA9j7MXjnsdTmWoZt6ty+3o2arAPn0GeAUugmwTV tcG/xYEXNnbOUOFKz4Cb5kPkHNzhZA6ZMMptmJgfSCC6YvExKzXVoVoZi A==; X-CSE-ConnectionGUID: Iyh0kuPzQ8SjL+lkDOnCDA== X-CSE-MsgGUID: 2WXy0sgQTzey/2B50phL4g== X-IronPort-AV: E=McAfee;i="6800,10657,11541"; a="58878194" X-IronPort-AV: E=Sophos;i="6.18,233,1751266800"; d="scan'208";a="58878194" X-CSE-ConnectionGUID: i10GpvOySWysoFeNqw6gXw== X-CSE-MsgGUID: fHiCdSqcTdWD5ho2YkXW+Q== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.18,233,1751266800"; d="scan'208";a="175634770" From: Vivek Kasireddy To: qemu-devel@nongnu.org Cc: Vivek Kasireddy , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= , =?UTF-8?q?Alex=20Benn=C3=A9e?= , Akihiko Odaki , Dmitry Osipenko Subject: [RFC 3/6] virtio-gpu-udmabuf: Create dmabuf for blobs associated with VFIO devices Date: Tue, 2 Sep 2025 22:42:16 -0700 Message-ID: <20250903054438.1179384-4-vivek.kasireddy@intel.com> X-Mailer: git-send-email 2.50.1 In-Reply-To: <20250903054438.1179384-1-vivek.kasireddy@intel.com> References: <20250903054438.1179384-1-vivek.kasireddy@intel.com> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable 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=198.175.65.20; envelope-from=vivek.kasireddy@intel.com; helo=mgamail.intel.com X-Spam_score_int: -43 X-Spam_score: -4.4 X-Spam_bar: ---- X-Spam_report: (-4.4 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_MED=-2.3, 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 @intel.com) X-ZM-MESSAGEID: 1756878503282116600 In addition to memfd, a blob resource can also have its backing storage in a VFIO device region. Therefore, we first need to figure out if the blob is backed by a VFIO device region or a memfd before we can call the right API to get a dmabuf fd created. So, once we have the ramblock and the associated mr, we rely on memory_region_is_ram_device() to tell us where the backing storage is located. If the blob resource is VFIO backed, we try to find the right VFIO device that contains the blob and then invoke the API vfio_create_dmabuf(). Note that we only call virtio_gpu_remap_udmabuf() if the blob is backed by a memfd. This is because the VFIO dmabuf implementation may not support mmap. Cc: Marc-Andr=C3=A9 Lureau Cc: Alex Benn=C3=A9e Cc: Akihiko Odaki Cc: Dmitry Osipenko Signed-off-by: Vivek Kasireddy --- hw/display/virtio-gpu-udmabuf.c | 60 ++++++++++++++++++++++++++++----- 1 file changed, 52 insertions(+), 8 deletions(-) diff --git a/hw/display/virtio-gpu-udmabuf.c b/hw/display/virtio-gpu-udmabu= f.c index d804f321aa..0390a8f488 100644 --- a/hw/display/virtio-gpu-udmabuf.c +++ b/hw/display/virtio-gpu-udmabuf.c @@ -18,6 +18,7 @@ #include "ui/console.h" #include "hw/virtio/virtio-gpu.h" #include "hw/virtio/virtio-gpu-pixman.h" +#include "hw/vfio/vfio-device.h" #include "trace.h" #include "system/ramblock.h" #include "system/hostmem.h" @@ -27,6 +28,32 @@ #include "standard-headers/linux/udmabuf.h" #include "standard-headers/drm/drm_fourcc.h" =20 +static void vfio_create_dmabuf(VFIODevice *vdev, + struct virtio_gpu_simple_resource *res) +{ + res->dmabuf_fd =3D vfio_device_create_dmabuf(vdev, res->iov, res->iov_= cnt); + if (res->dmabuf_fd < 0) { + warn_report("%s: VFIO_DEVICE_FEATURE_DMA_BUF: %s", __func__, + strerror(errno)); + } +} + +static VFIODevice *vfio_device_lookup(MemoryRegion *mr) +{ + VFIODevice *vdev; + + if (QLIST_EMPTY(&vfio_device_list)) { + return NULL; + } + + QLIST_FOREACH(vdev, &vfio_device_list, next) { + if (vdev->dev =3D=3D mr->dev) { + return vdev; + } + } + return NULL; +} + static void virtio_gpu_create_udmabuf(struct virtio_gpu_simple_resource *r= es) { struct udmabuf_create_list *list; @@ -130,6 +157,9 @@ bool virtio_gpu_have_udmabuf(void) =20 void virtio_gpu_init_udmabuf(struct virtio_gpu_simple_resource *res) { + bool memfd_blob =3D false; + ram_addr_t offset; + RAMBlock *rb; void *pdata =3D NULL; =20 res->dmabuf_fd =3D -1; @@ -137,15 +167,31 @@ void virtio_gpu_init_udmabuf(struct virtio_gpu_simple= _resource *res) res->iov[0].iov_len < 4096) { pdata =3D res->iov[0].iov_base; } else { - virtio_gpu_create_udmabuf(res); + rb =3D qemu_ram_block_from_host(res->iov[0].iov_base, false, &offs= et); + if (rb && memory_region_is_ram_device(rb->mr)) { + VFIODevice *vdev =3D vfio_device_lookup(rb->mr); + + if (!vdev) { + warn_report("Could not find device to create dmabuf"); + return; + } + vfio_create_dmabuf(vdev, res); + } else { + virtio_gpu_create_udmabuf(res); + memfd_blob =3D true; + } + if (res->dmabuf_fd < 0) { return; } - virtio_gpu_remap_udmabuf(res); - if (!res->remapped) { - return; + + if (memfd_blob) { + virtio_gpu_remap_udmabuf(res); + if (!res->remapped) { + return; + } + pdata =3D res->remapped; } - pdata =3D res->remapped; } =20 res->blob =3D pdata; @@ -153,9 +199,7 @@ void virtio_gpu_init_udmabuf(struct virtio_gpu_simple_r= esource *res) =20 void virtio_gpu_fini_udmabuf(struct virtio_gpu_simple_resource *res) { - if (res->remapped) { - virtio_gpu_destroy_udmabuf(res); - } + virtio_gpu_destroy_udmabuf(res); } =20 static void virtio_gpu_free_dmabuf(VirtIOGPU *g, VGPUDMABuf *dmabuf) --=20 2.50.1