Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 104F9CF64B1 for ; Sat, 22 Nov 2025 07:46:48 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1vMiGw-0007Cr-0V; Sat, 22 Nov 2025 02:43:07 -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 1vMhSd-0000QP-27 for qemu-devel@nongnu.org; Sat, 22 Nov 2025 01:51:08 -0500 Received: from mgamail.intel.com ([192.198.163.14]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1vMhSA-0000qe-Tg for qemu-devel@nongnu.org; Sat, 22 Nov 2025 01:51:04 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1763794239; x=1795330239; h=from:to:cc:subject:date:message-id:mime-version: content-transfer-encoding; bh=fc0Le/gVDdvOZ4rSppSsmapPVlWe7tTXdKc8UONphy8=; b=itfMiSvJSa4HoRGO/vjCPhg6OwloKOsUjKHx/6L7G99VPUJf/fApmMJO FffBvi+DH79vmkOcZtniJdMsG8qzuRv6WJ3UV8MtHJnpcHrT57sTNZQEZ 9pG1qks6URBPNAvsnme2ZzRytUcLU/ZfX0KA7xYuSMZeTclumMtEJdsOh PDEvMVzUMaVrv+ws1EGitttn9h0YeCDrVAK+V8yvrAEZApGte/Qz8xMfU BcnjwEv3pa9cO5m5kJnnGG59cWMTcRcAfSh/aB562YGCxAFYsl1CXZYYV Z8DbsTyjZWvmT2UqRyBPFulxjrakRJCnUXhVMx0oChTiuRtXpYMtZLL/K w==; X-CSE-ConnectionGUID: 2zpAoiVYTD6h+Lu83t3BRA== X-CSE-MsgGUID: dgs8zZriRr6/ZSCrMuXbHg== X-IronPort-AV: E=McAfee;i="6800,10657,11620"; a="65924054" X-IronPort-AV: E=Sophos;i="6.20,217,1758610800"; d="scan'208";a="65924054" Received: from orviesa006.jf.intel.com ([10.64.159.146]) by fmvoesa108.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 21 Nov 2025 22:50:21 -0800 X-CSE-ConnectionGUID: 3QN0RySHS729CwMTIcJbig== X-CSE-MsgGUID: MW9PK85XT1erIP1EPDFwtQ== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.20,217,1758610800"; d="scan'208";a="191064598" Received: from vkasired-desk2.fm.intel.com ([10.105.128.132]) by orviesa006-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 21 Nov 2025 22:50:22 -0800 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 , Alex Williamson , =?UTF-8?q?C=C3=A9dric=20Le=20Goater?= , Leon Romanovsky , Leon Romanovsky , Jason Gunthorpe , Dongwon Kim Subject: [PATCH v3 0/9] vfio: Implement VFIO_DEVICE_FEATURE_DMA_BUF and use it in virtio-gpu Date: Fri, 21 Nov 2025 22:46:21 -0800 Message-ID: <20251122064936.2948632-1-vivek.kasireddy@intel.com> X-Mailer: git-send-email 2.50.1 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Received-SPF: pass client-ip=192.198.163.14; envelope-from=vivek.kasireddy@intel.com; helo=mgamail.intel.com X-Spam_score_int: -16 X-Spam_score: -1.7 X-Spam_bar: - X-Spam_report: (-1.7 / 5.0 requ) BAYES_00=-1.9, DKIM_INVALID=0.1, DKIM_SIGNED=0.1, T_SPF_HELO_TEMPERROR=0.01, T_SPF_TEMPERROR=0.01 autolearn=no 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+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org The virtio-gpu driver running in the Guest VM can create Guest blob resources (by importing dmabufs) that are backed by System RAM. This is made possible by making use of memfd memory backend and udmabuf driver on the Host side. However, in order to create Guest blobs that are backed by vfio-pci device regions (which happens when virtio-gpu imports dmabufs from devices that have local memory such as dGPU VFs), we have to implement VFIO_DEVICE_FEATURE_DMA_BUF and leverage it in virtio-gpu. So, while creating the blobs we use ram_block_is_memfd_backed() to figure out if the blob is backed by memfd or a vfio-pci device. If it is determined that the blob is backed by vfio-pci device region, instead of calling into udmabuf driver to create a dmabuf fd we would now call into vfio-pci driver to have a dmabuf fd created on the Host. Changelog: v2 -> v3: - Use memory_region_get_ram_ptr() to obtain hva for both memfd and VFIO backed memory regions (Akihiko) - Drop the patch that introduced qemu_iovec_same_memory_regions() helper and move the "same memory region" check into virtio_gpu_create_udmabuf(), vfio_device_create_dmabuf_fd() (Akihiko) - Refactor virtio_gpu_init_dmabuf() to not rely on helpers such as memory_region_is_ram_device() to identify memory regions and instead call virtio_gpu_create_udmabuf() unconditionally (Akihiko) - Add a patch to introduce ram_block_is_memfd_backed() helper v1 -> v2: - Drop the patch that uses res->blob_size instead of res->blob to identify blob resources (Akihiko) - Remove the res->dmabuf_fd < 0 check while attaching backing to a resource (Akihiko) - Remove cmd->cmd_hdr.type != VIRTIO_GPU_CMD_RESOURCE_CREATE_BLOB check while attaching backing (Akihiko) - Improve vfio_get_region_index_from_mr() and add documentation (Cedric) - Remove rcu_read_lock/unlock around qemu_ram_block_from_host() (Akihiko, Cedric) - Improve, document and rename vfio_device_create_dmabuf() to vfio_device_create_dmabuf_fd() (Cedric) - Add a new helper to lookup VFIO device from memory region (Cedric) - Document vfio_device_get_region_info() (Cedric) - Ensure that info variable (in vfio_dmabuf_mmap()) is initialized before use (Cedric) - Rename udmabuf files and helpers to dmabuf (Akihiko) - Remove the redundant check for virtio_gpu_have_udmabuf() in virtio_gpu_init_dmabuf() (Akihiko) - Add a helper to check whether all the entries of a dmabuf belong to a single memory region or not (Akihiko) RFC -> v1: - Create the CPU mapping using vfio device fd if the dmabuf exporter (vfio-pci) does not provide mmap() support (Akihiko) - Log a warning with LOG_GUEST_ERROR instead of warn_report() when dmabuf cannot be created using Guest provided addresses (Akihiko) - Use address_space_translate() instead of gpa2hva() to obtain the Host addresses (Akihiko) - Rearrange the patches and improve the commit messages (Akihiko) - Fix compilation error when VFIO is not enabled (Alex) - Add a new helper to obtain VFIO region index from memory region - Move vfio_device_create_dmabuf() to hw/vfio/device.c Tested with an SRIOV enabled Intel dGPU (B60) by running Gnome Wayland (in the VM) and Qemu with the following (relevant) parameters: -device vfio-pci,host=0000:03:00.1 -device virtio-vga,max_outputs=1,xres=1920,yres=1080,blob=true -display gtk,gl=on Associated vfio-pci kernel driver series: https://lore.kernel.org/dri-devel/20251120-dmabuf-vfio-v9-0-d7f71607f371@nvidia.com/ Associated virtio-gpu kernel driver series (merged): https://lore.kernel.org/dri-devel/20241126031643.3490496-1-vivek.kasireddy@intel.com/ --- Cc: Marc-André Lureau Cc: Alex Bennée Cc: Akihiko Odaki Cc: Dmitry Osipenko Cc: Alex Williamson Cc: Cédric Le Goater Cc: Leon Romanovsky Cc: Leon Romanovsky Cc: Jason Gunthorpe Cc: Dongwon Kim Vivek Kasireddy (9): virtio-gpu: Recreate the resource's dmabuf if new backing is attached virtio-gpu: Find hva for Guest's DMA addr associated with a ram device vfio: Document vfio_device_get_region_info() vfio/region: Add a helper to get region index from memory region vfio/device: Add a helper to lookup VFIODevice from memory region vfio/device: Add support for VFIO_DEVICE_FEATURE_DMA_BUF virtio-gpu: Rename udmabuf files and helpers to dmabuf virtio-gpu-dmabuf: Introduce ram_block_is_memfd_backed() helper virtio-gpu-dmabuf: Create dmabuf for blobs associated with VFIO devices hw/display/Kconfig | 5 + hw/display/meson.build | 4 +- ...abuf-stubs.c => virtio-gpu-dmabuf-stubs.c} | 4 +- ...rtio-gpu-udmabuf.c => virtio-gpu-dmabuf.c} | 155 +++++++++++++++--- hw/display/virtio-gpu.c | 31 +++- hw/vfio/device.c | 57 +++++++ hw/vfio/region.c | 14 ++ include/hw/vfio/vfio-device.h | 46 ++++++ include/hw/virtio/virtio-gpu.h | 6 +- 9 files changed, 288 insertions(+), 34 deletions(-) rename hw/display/{virtio-gpu-udmabuf-stubs.c => virtio-gpu-dmabuf-stubs.c} (79%) rename hw/display/{virtio-gpu-udmabuf.c => virtio-gpu-dmabuf.c} (58%) -- 2.50.1