From nobody Sun Dec 14 06:35:53 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=1763797789; cv=none; d=zohomail.com; s=zohoarc; b=aRW5chiC2hovxdR437GVMQ/YMwYzdLkaFi5+wQlCSn6dM0lT04slUzwGeOkq7Dy1pvu6YbS6zq+KYh+rS7zwvaibRxhXCqQjp/6A/WEqv6lpowtIDdXzzvvstEPlXzvkjPX/KwDcMcmvMeORKDUpEXnp7Ne2VP8/juPrk3yBU8U= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1763797789; 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=V9fow2zuX739i6sqEESzb8pOkj90/vIb4ai7DYy9REE=; b=QO0kKADycrj8oZm28Qkyd/Z0y3SCQqsbN88VKRdQbLCe44a/9ZvN2O0WIH09HGIUpn6XSsJko2ii51q0PbjXdI7Nk7Rpis7w/HTqTvE5G0D25MfXJFNnUWVse3kPPSnvOuza0RerDlXJVHA3jLZgfjN/yWxlcKBn9le0eU/D6Hk= 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 1763797789056736.181841739748; Fri, 21 Nov 2025 23:49:49 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1vMiK1-00088S-AS; Sat, 22 Nov 2025 02:46:18 -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 1vMhTG-0000bk-9e for qemu-devel@nongnu.org; Sat, 22 Nov 2025 01:51:54 -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 1vMhSk-0000qe-VG for qemu-devel@nongnu.org; Sat, 22 Nov 2025 01:51:44 -0500 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:22 -0800 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 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1763794275; x=1795330275; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=zdHvub0fJfbNNGqYIUwGIo1RD75YRQG0yqOJDJhrBGo=; b=bkLDAHiwLP458WIZAPcck9/bNrZoJ94iLu322y7NNuT6gmdDcgx40Wgw DKcUpwUbfWvWIcVRMwrDUXbCZi58KWLYU0ncV2e8Wrthx6ZlI+WH7Pfw3 L455V1zJpRyiEpuGVnoZi6BPF4gMtPJGL53Aa1plpdCKPeWTYJumuvgbq kf1yQ1VA1tiIFeo8Oq+rNcfQY7jGlN5dH6u0QgT7dTEtyy0+pskX4Vbk3 +tN7IkP+IWNWzfkiOSa0l18KQFI32iQcf9V29bGWicFOgCuRev77ieaeO xMDvimFiRhcRahmrFAiS6z9F4lHSOnqFzzqCtODZbcmee/VZ0xAXuWJHk Q==; X-CSE-ConnectionGUID: 0OrZmn0rQk+sBC1BQI4SuA== X-CSE-MsgGUID: O8qSPh2JSKGEWDpa4DmUKQ== X-IronPort-AV: E=McAfee;i="6800,10657,11620"; a="65924060" X-IronPort-AV: E=Sophos;i="6.20,217,1758610800"; d="scan'208";a="65924060" X-CSE-ConnectionGUID: 6CCEXX52QieXWPnoCwbmSw== X-CSE-MsgGUID: Jv4eG5KQSeC9ioWW7rgx7Q== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.20,217,1758610800"; d="scan'208";a="191064602" 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?= Subject: [PATCH v3 1/9] virtio-gpu: Recreate the resource's dmabuf if new backing is attached Date: Fri, 21 Nov 2025 22:46:22 -0800 Message-ID: <20251122064936.2948632-2-vivek.kasireddy@intel.com> X-Mailer: git-send-email 2.50.1 In-Reply-To: <20251122064936.2948632-1-vivek.kasireddy@intel.com> References: <20251122064936.2948632-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=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+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @intel.com) X-ZM-MESSAGEID: 1763797790779018900 There are cases when a blob resource's backing might get detached and re-attached again such as when the underlying object is getting migrated in the Guest. In these situations, we need to obtain a new dmabuf fd, which can be done by calling virtio_gpu_init_udmabuf(). Cc: Marc-Andr=C3=A9 Lureau Cc: Alex Benn=C3=A9e Cc: Akihiko Odaki Cc: Dmitry Osipenko Cc: Alex Williamson Cc: C=C3=A9dric Le Goater Reviewed-by: Akihiko Odaki Signed-off-by: Vivek Kasireddy --- hw/display/virtio-gpu.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/hw/display/virtio-gpu.c b/hw/display/virtio-gpu.c index 43e88a4daf..199b18c746 100644 --- a/hw/display/virtio-gpu.c +++ b/hw/display/virtio-gpu.c @@ -937,6 +937,10 @@ virtio_gpu_resource_attach_backing(VirtIOGPU *g, cmd->error =3D VIRTIO_GPU_RESP_ERR_UNSPEC; return; } + + if (res->blob_size) { + virtio_gpu_init_udmabuf(res); + } } =20 static void --=20 2.50.1 From nobody Sun Dec 14 06:35:53 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=1763797777; cv=none; d=zohomail.com; s=zohoarc; b=hkczDihcjDPQmP2S6IIDYd/Okf3ZNVc46urz9Mik8JX0JrbfgjeEATC2bXdul1BNCgH1xMhro0aejggB2kL/sDI/n3BfYxETQ9TlrVTO53b00RossUpO3UG0JyPQMtprnhclbbk+IBq+VDdUX2j7poKwbhgWpcyIxiZG4nIZSWM= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1763797777; 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=d+jCy0qwNf8ePQUAAvtrSr+dowCxSc7PLBKr/Hdqq0Q=; b=fHS5aeDoVhvFUuxcFH21gaky6zYrGIm3V01XqSuPTNUdwHV5yanLB8EG8IrwszQEABNZUMicdA2A1mZ/Aj38BttiA+8Nw1Qdwip07NQmWRnRCXxnYvKpn3dl21pgVEcth6cIvdxPlBzytQzi0a3F7D8soOPLOp77TxqZiiEvuiA= 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 176379777700540.04438080012403; Fri, 21 Nov 2025 23:49:37 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1vMiJo-0007z9-VX; Sat, 22 Nov 2025 02:46:06 -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 1vMhTH-0000bl-BW for qemu-devel@nongnu.org; Sat, 22 Nov 2025 01:51:56 -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 1vMhSt-0001CG-HV for qemu-devel@nongnu.org; Sat, 22 Nov 2025 01:51:44 -0500 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:22 -0800 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 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1763794283; x=1795330283; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=rZXZ/RLaaszNM3P54xGv99y8k/xIqGDEb321sti71BU=; b=MS437qWYo5bVXZxx8svGdOGpeNBOE1u18uVkOEA8gfcX4fSkHzdw8+4P ZBClhnt7+Vckkq/Vp0XbU4eFAJrv6fIGfU0ixTfftwuHUFgJa3IQxyHh1 Ps4+kCIkimRkXkAcsSnHhASLiODVT9c6A8phy3O+47sSSg08dQDEqMzMn 5/dYUECkcq0nV08kWukWN8ZHUNERX+nOkjyKluNRA/XQv3hR7qE6autQl tI1IKl3FbMQQlKOj41bjoJ24GZg0smxPKwzLoKroAYPovT5DOUbvJfoEp 0iNvtD60UQrAbT+el5SFAdSy23w9NL9k55Poiga5hJDSUIog7Ejns7mAm A==; X-CSE-ConnectionGUID: P2ScUBLzRY62h4oaHWg1zw== X-CSE-MsgGUID: 6lQHVaywT920nX7jXfyR0g== X-IronPort-AV: E=McAfee;i="6800,10657,11620"; a="65924065" X-IronPort-AV: E=Sophos;i="6.20,217,1758610800"; d="scan'208";a="65924065" X-CSE-ConnectionGUID: gw+9KSDCSXKfXqxve99/zw== X-CSE-MsgGUID: 8QGQgzomSG683EBwUcGJGw== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.20,217,1758610800"; d="scan'208";a="191064605" 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?= Subject: [PATCH v3 2/9] virtio-gpu: Find hva for Guest's DMA addr associated with a ram device Date: Fri, 21 Nov 2025 22:46:23 -0800 Message-ID: <20251122064936.2948632-3-vivek.kasireddy@intel.com> X-Mailer: git-send-email 2.50.1 In-Reply-To: <20251122064936.2948632-1-vivek.kasireddy@intel.com> References: <20251122064936.2948632-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=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+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @intel.com) X-ZM-MESSAGEID: 1763797777940018900 If the Guest provides a DMA address that is associated with a ram device (such as a PCI device region and not its system memory), then we can obtain the hva (host virtual address) by invoking address_space_translate() followed by memory_region_get_ram_ptr(). This is because the ram device's address space is not accessible to virtio-gpu directly and hence dma_memory_map() cannot be used. Therefore, we first need to identify the memory region associated with the DMA address and add the offset to the pointer returned from memory_region_get_ram_ptr() to obtain the host address. Note that we take a reference on the memory region but we would still eventually call dma_memory_unmap() (to unref the mr). Cc: Marc-Andr=C3=A9 Lureau Cc: Alex Benn=C3=A9e Cc: Akihiko Odaki Cc: Dmitry Osipenko Cc: Alex Williamson Cc: C=C3=A9dric Le Goater Signed-off-by: Vivek Kasireddy --- hw/display/virtio-gpu.c | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/hw/display/virtio-gpu.c b/hw/display/virtio-gpu.c index 199b18c746..d746c5f426 100644 --- a/hw/display/virtio-gpu.c +++ b/hw/display/virtio-gpu.c @@ -798,6 +798,23 @@ static void virtio_gpu_set_scanout_blob(VirtIOGPU *g, &fb, res, &ss.r, &cmd->error); } =20 +static void *virtio_gpu_dma_memory_map(VirtIOGPU *g, + struct virtio_gpu_ctrl_command *cmd, + uint64_t a, hwaddr *len) +{ + MemoryRegion *mr; + hwaddr xlat; + + mr =3D address_space_translate(VIRTIO_DEVICE(g)->dma_as, a, &xlat, len, + DMA_DIRECTION_TO_DEVICE, + MEMTXATTRS_UNSPECIFIED); + if (memory_region_is_ram(mr)) { + memory_region_ref(mr); + return memory_region_get_ram_ptr(mr) + xlat; + } + return NULL; +} + int virtio_gpu_create_mapping_iov(VirtIOGPU *g, uint32_t nr_entries, uint32_t offset, struct virtio_gpu_ctrl_command *cmd, @@ -839,9 +856,7 @@ int virtio_gpu_create_mapping_iov(VirtIOGPU *g, =20 do { len =3D l; - map =3D dma_memory_map(VIRTIO_DEVICE(g)->dma_as, a, &len, - DMA_DIRECTION_TO_DEVICE, - MEMTXATTRS_UNSPECIFIED); + map =3D virtio_gpu_dma_memory_map(g, cmd, a, &len); if (!map) { qemu_log_mask(LOG_GUEST_ERROR, "%s: failed to map MMIO mem= ory for" " element %d\n", __func__, e); --=20 2.50.1 From nobody Sun Dec 14 06:35:53 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=1763798050; cv=none; d=zohomail.com; s=zohoarc; b=kkRwRrr8x+yBwK7KdLMKbX/gG+J7MqvkXe1GWpGp6sj79sNhBirckA/FtDTtj6bsg3eXRjNzJQZRjzwI+85aMJDADklvZyRWYVGu3Hz47ixaZ270uLqms8dEFN4tzHXqxi8LZVLtm1jG8o5Gh/SgdGipntVVEsJIr+Tn9TEnrO8= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1763798050; 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=l0edKEj1cDRgowC+S9LNtkf2YnLKZ9jW9Ik6C51ICHA=; b=ndBkiRlMvTm5eMXIMUXVegrQqzLT0+PKHCvW25MPe4Tu3hoelEF40wEmalgPia9ajfeAOeDEghoRNo5CfuPmcXTmoJeFEjGIu1AN5Uz0J229PTsLoFClvtbA4c4CdM/gzyxXn/XmRbPlMFudZM7nJWgqGC6dirKqxtcZttxh/xA= 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 176379805079852.78046743825348; Fri, 21 Nov 2025 23:54:10 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1vMiKl-0008Fw-RB; Sat, 22 Nov 2025 02:47:10 -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 1vMhTk-0000gN-R0 for qemu-devel@nongnu.org; Sat, 22 Nov 2025 01:52:23 -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 1vMhTN-0000qe-LE for qemu-devel@nongnu.org; Sat, 22 Nov 2025 01:52:12 -0500 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:22 -0800 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 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1763794313; x=1795330313; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=Upq2JY7yf0Ys2TZJHDdbUd1IroM2T22AHjG73dw2axg=; b=hTvILl+OcEQmMgPbob8C7E9jzu9uKCTL171OhUf4igCDqUMNrjoAKPrv oNrqKi5BH5ooJvaZDNi8348JUBXx0dOCURKOIkbNSyyGmlqjbZY9XNIoZ fiOHIineYnw5nxBf9sCNO+MAkUOBPFTZfYo7hBa5I/9s1rbFKo6uwBlRX MEFCBSrVeVeCAuQKMwbbM98p5jsUzCy0cKoL/kXu1OIs3aqoE5rvnP4Ek NeK/3Q7d0kvgZS4Budh4ny8xT8KaD03mZIgzDVe8dnFVgnmFZv0Bt8lG3 q994GVkmg834bYy9CuqwFIcv3z9LK7eyjD+q9ccj8PPGNVQfg3nyNIYCF w==; X-CSE-ConnectionGUID: /APFKRm+T4+SGcnMeRRlew== X-CSE-MsgGUID: ZuzzI3T9Rce2jXkIurom/Q== X-IronPort-AV: E=McAfee;i="6800,10657,11620"; a="65924069" X-IronPort-AV: E=Sophos;i="6.20,217,1758610800"; d="scan'208";a="65924069" X-CSE-ConnectionGUID: Ml6EIHS3R2e/NRoXnyyfgQ== X-CSE-MsgGUID: bSPxbtQ2QLSZDAups5WFxA== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.20,217,1758610800"; d="scan'208";a="191064608" From: Vivek Kasireddy To: qemu-devel@nongnu.org Cc: Vivek Kasireddy , Alex Williamson , =?UTF-8?q?C=C3=A9dric=20Le=20Goater?= Subject: [PATCH v3 3/9] vfio: Document vfio_device_get_region_info() Date: Fri, 21 Nov 2025 22:46:24 -0800 Message-ID: <20251122064936.2948632-4-vivek.kasireddy@intel.com> X-Mailer: git-send-email 2.50.1 In-Reply-To: <20251122064936.2948632-1-vivek.kasireddy@intel.com> References: <20251122064936.2948632-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=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+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @intel.com) X-ZM-MESSAGEID: 1763798052636018900 Add documentation for vfio_device_get_region_info() and clarify the expectations around its usage. Cc: Alex Williamson Cc: C=C3=A9dric Le Goater Signed-off-by: Vivek Kasireddy --- include/hw/vfio/vfio-device.h | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/include/hw/vfio/vfio-device.h b/include/hw/vfio/vfio-device.h index 0fe6c60ba2..bb28123faf 100644 --- a/include/hw/vfio/vfio-device.h +++ b/include/hw/vfio/vfio-device.h @@ -257,6 +257,19 @@ void vfio_device_prepare(VFIODevice *vbasedev, VFIOCon= tainer *bcontainer, =20 void vfio_device_unprepare(VFIODevice *vbasedev); =20 +/** + * Return the region info for a given region index. The region info includ= es + * details such as size, offset, and capabilities. Note that the returned + * info pointer is either a cached copy or newly allocated by + * vfio_device_get_region_info(), so the caller is not expected to allocate + * or free it. + * + * @vbasedev: #VFIODevice to use + * @index: region index + * @info: pointer to store the region info + * + * Returns 0 on success or a negative value on error. + */ int vfio_device_get_region_info(VFIODevice *vbasedev, int index, struct vfio_region_info **info); int vfio_device_get_region_info_type(VFIODevice *vbasedev, uint32_t type, --=20 2.50.1 From nobody Sun Dec 14 06:35:53 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=1763797825; cv=none; d=zohomail.com; s=zohoarc; b=nWV+mZF8anRYM5icn4okveGLU+jCKgqy+8GwBzHcimhhPDL4YZBdkx8xlfE1nCGTYN/42ZaUWtRovmPmYex2YFdZmVvtcJnHmbRKaBbd8qQdFI0TFg1RV9U2JH5UVy0RvbloeZf1KcyBvbd9tRWi8XtcL7Zmh6Ic3fZb00YaBCI= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1763797825; 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=k60R7GlBT09baUZXv/2iYkFlo3wkaplvcUlJ/OAiPIw=; b=CWMQN1vNkeIsSotVhMcc8TT1sjRS1EKouSLD4erLPEsO+Gv81Q/4QICHTplh4WY64EUKLr+scDEhZmp+06ec+Rh4mMKOhRnqg8Nt+4EvT1jVua5x2HJEumE1bTZ9+JpHv/Bqr9Zq+/5gcY42/CkoC3LeJ4H7Y6c+O0um8+j44Nk= 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 176379782579854.17837078188154; Fri, 21 Nov 2025 23:50:25 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1vMiKE-0008Di-Rd; Sat, 22 Nov 2025 02:46:31 -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 1vMhTk-0000gO-RA for qemu-devel@nongnu.org; Sat, 22 Nov 2025 01:52:26 -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 1vMhTN-0001CG-NN for qemu-devel@nongnu.org; Sat, 22 Nov 2025 01:52:12 -0500 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:22 -0800 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 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1763794314; x=1795330314; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=fwkIF75YKfczbZku0xCM5nGDSNWKts3J6DNrrIfsjiY=; b=XH2CzjNQSVktr894n5Qgj5O6OV1ft56ErId7X6eaNQIw3BHaXQUT4WOF 1Mb5qmwY+8CkdnBHxAjY6ILnpPADLz62FMr/L/YnJpBWj2f3xMYQvXSLK IfBEIZMA7W+uEN0wKgMiij4uSYHwOE38f+MT3FLzXDORXCtxZQUK6fSKp AL0gOzC7SXroceyoFegJKG7rBl1lpCm2GP3RPjQA2vOWPzj3gQv5PJ2St ZGmOAt08z2n7xBEKQI8eZj0QM1vXtYYwkmTTq/6wrrLXwszprzMK5VCjt OI+mx5oy1wC5W7VX07operx9+tkCfPj26QzZBoibNirua1xRvvSVMaF0b A==; X-CSE-ConnectionGUID: uIdmJ5/PQxuM1ckAOQWveA== X-CSE-MsgGUID: 6HMquwMgQ16pXOv8z2TTaw== X-IronPort-AV: E=McAfee;i="6800,10657,11620"; a="65924073" X-IronPort-AV: E=Sophos;i="6.20,217,1758610800"; d="scan'208";a="65924073" X-CSE-ConnectionGUID: +SIWi5PVTa6cawuWtXe10w== X-CSE-MsgGUID: Vdd8VcNAQkGrTZYcQ9EsYw== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.20,217,1758610800"; d="scan'208";a="191064612" From: Vivek Kasireddy To: qemu-devel@nongnu.org Cc: Vivek Kasireddy , Alex Williamson , =?UTF-8?q?C=C3=A9dric=20Le=20Goater?= Subject: [PATCH v3 4/9] vfio/region: Add a helper to get region index from memory region Date: Fri, 21 Nov 2025 22:46:25 -0800 Message-ID: <20251122064936.2948632-5-vivek.kasireddy@intel.com> X-Mailer: git-send-email 2.50.1 In-Reply-To: <20251122064936.2948632-1-vivek.kasireddy@intel.com> References: <20251122064936.2948632-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=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+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @intel.com) X-ZM-MESSAGEID: 1763797827947018900 Having a way to figure out the region index (or bar) associated with a memory region is helpful in various scenarios. For example, this capability can be useful in retrieving the region info needed for mapping a part of a VFIO region or creating a dmabuf. Cc: Alex Williamson Cc: C=C3=A9dric Le Goater Signed-off-by: Vivek Kasireddy --- hw/vfio/region.c | 14 ++++++++++++++ include/hw/vfio/vfio-device.h | 10 ++++++++++ 2 files changed, 24 insertions(+) diff --git a/hw/vfio/region.c b/hw/vfio/region.c index b165ab0b93..046adfaa2c 100644 --- a/hw/vfio/region.c +++ b/hw/vfio/region.c @@ -398,3 +398,17 @@ void vfio_region_mmaps_set_enabled(VFIORegion *region,= bool enabled) trace_vfio_region_mmaps_set_enabled(memory_region_name(region->mem), enabled); } + +int vfio_get_region_index_from_mr(MemoryRegion *mr) +{ + VFIORegion *region; + + while (mr->container) { + if (mr->ops =3D=3D &vfio_region_ops) { + region =3D mr->opaque; + return region->nr; + } + mr =3D mr->container; + } + return -1; +} diff --git a/include/hw/vfio/vfio-device.h b/include/hw/vfio/vfio-device.h index bb28123faf..44cacd3728 100644 --- a/include/hw/vfio/vfio-device.h +++ b/include/hw/vfio/vfio-device.h @@ -290,6 +290,16 @@ bool vfio_device_has_region_cap(VFIODevice *vbasedev, = int region, uint16_t cap_t =20 int vfio_device_get_irq_info(VFIODevice *vbasedev, int index, struct vfio_irq_info *info); + +/** + * Return the region index associated with a given MemoryRegion. The index + * can be useful in retrieving region info. + * + * @mr: MemoryRegion to use + * + * Returns the region index or -1 on error. + */ +int vfio_get_region_index_from_mr(MemoryRegion *mr); #endif =20 /* Returns 0 on success, or a negative errno. */ --=20 2.50.1 From nobody Sun Dec 14 06:35:53 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=1763797689; cv=none; d=zohomail.com; s=zohoarc; b=RWFHKrdhcs3ieMOJWp6CJ9i5RoD5XnFZULIWVmGN3UeaJdmcqofSr23TzPHpv9WH5HbRGE9iQdeb9CqosCSJQ+SKjn7sQyhvX7KcciBbJrvmsS440C6oPFkWRZuzp97F5zHk+rjjW79wtYjS36/BYcFubPz0BiqUl21ufVcruo0= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1763797689; 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=9657iDYsvI8R/nLNywaujI2iJupA6OztS163Y8Jb8pE=; b=db3avY0RtwEEJpFNV5tXzaNn/EcxFQHPJmhkyLyNRyAycL3hU+auGDCzaEA/ARTveL6iXuvlZbQsG9zbKc/p1jnMpV6/AQ7VN/8LEp2v9N0O3t1VLD3C7Rjk6wk5KGEMoK1ixXcM6dCkqk7DPYjDlEkKlerxtDIrXiWoX/SUxhk= 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 1763797689260710.3717143261316; Fri, 21 Nov 2025 23:48:09 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1vMiHu-0007Sy-Ms; Sat, 22 Nov 2025 02:44: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 1vMhUI-0000kK-Jp for qemu-devel@nongnu.org; Sat, 22 Nov 2025 01:52:51 -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 1vMhTw-0000qe-HL for qemu-devel@nongnu.org; Sat, 22 Nov 2025 01:52:47 -0500 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:22 -0800 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 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1763794348; x=1795330348; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=xcklOL67otmqpeg2fnsITiUmdKSHZVSNYVszE/Pxnvc=; b=HtlPFKlKV/HTkMsCUDQAfHLalMpzzMNwNp+2YLVBI7cR4oAIdPtlZkOM laaM7Bh4y1ijNPmXzd/ppZqqD7rHOEY4PDIY2nObYArTedcc7NGDv7Kds e3QNiMslhXtZuo2bnhoLTwDvYTwhS98aptOOO4goMJB8OeUOr5dbyotHS qHjwJx7yjzN8Qow5dNJL96NpoVJs+2rTSbjsjEF/ahu3SIRbRr89uA8dZ cHMsz/0w9E1T+7hwSOMcx4M/FILQ+cUz5Ha3tQ0W+AFI8kUIPVrLbHwR9 cyv6501cO0hMxxPC0VLA04xGjqkXSuCSXiZAUY1o/IeWJtMvzEc8HKifM A==; X-CSE-ConnectionGUID: Ur6U1mCmQXOpL4+Iqyp4mA== X-CSE-MsgGUID: WAR+DQfdTgqX/HE6U9phxA== X-IronPort-AV: E=McAfee;i="6800,10657,11620"; a="65924081" X-IronPort-AV: E=Sophos;i="6.20,217,1758610800"; d="scan'208";a="65924081" X-CSE-ConnectionGUID: XMdUcYobS7yqqBHu5m1Yag== X-CSE-MsgGUID: T83Tey3ZQlqTwgdTcFVCOg== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.20,217,1758610800"; d="scan'208";a="191064615" From: Vivek Kasireddy To: qemu-devel@nongnu.org Cc: Vivek Kasireddy , Alex Williamson , =?UTF-8?q?C=C3=A9dric=20Le=20Goater?= Subject: [PATCH v3 5/9] vfio/device: Add a helper to lookup VFIODevice from memory region Date: Fri, 21 Nov 2025 22:46:26 -0800 Message-ID: <20251122064936.2948632-6-vivek.kasireddy@intel.com> X-Mailer: git-send-email 2.50.1 In-Reply-To: <20251122064936.2948632-1-vivek.kasireddy@intel.com> References: <20251122064936.2948632-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=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+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @intel.com) X-ZM-MESSAGEID: 1763797691624018900 Instead of iterating over all QOM devices to find the VFIODevice associated with a memory region, it is faster to just use the vfio_device_list to lookup the VFIODevice. Cc: Alex Williamson Cc: C=C3=A9dric Le Goater Signed-off-by: Vivek Kasireddy --- hw/vfio/device.c | 12 ++++++++++++ include/hw/vfio/vfio-device.h | 9 +++++++++ 2 files changed, 21 insertions(+) diff --git a/hw/vfio/device.c b/hw/vfio/device.c index 76869828fc..9ff73f9941 100644 --- a/hw/vfio/device.c +++ b/hw/vfio/device.c @@ -603,3 +603,15 @@ static VFIODeviceIOOps vfio_device_io_ops_ioctl =3D { .region_read =3D vfio_device_io_region_read, .region_write =3D vfio_device_io_region_write, }; + +VFIODevice *vfio_device_lookup(MemoryRegion *mr) +{ + VFIODevice *vbasedev; + + QLIST_FOREACH(vbasedev, &vfio_device_list, next) { + if (vbasedev->dev =3D=3D mr->dev) { + return vbasedev; + } + } + return NULL; +} diff --git a/include/hw/vfio/vfio-device.h b/include/hw/vfio/vfio-device.h index 44cacd3728..2f8087f133 100644 --- a/include/hw/vfio/vfio-device.h +++ b/include/hw/vfio/vfio-device.h @@ -300,6 +300,15 @@ int vfio_device_get_irq_info(VFIODevice *vbasedev, int= index, * Returns the region index or -1 on error. */ int vfio_get_region_index_from_mr(MemoryRegion *mr); + +/** + * Return the VFIO device associated with a given MemoryRegion. + * + * @mr: MemoryRegion to use + * + * Returns the VFIO device if found or NULL. + */ +VFIODevice *vfio_device_lookup(MemoryRegion *mr); #endif =20 /* Returns 0 on success, or a negative errno. */ --=20 2.50.1 From nobody Sun Dec 14 06:35:53 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=1763797657; cv=none; d=zohomail.com; s=zohoarc; b=QKc4UE/wUm8vQibIVMrLdkYxcAXjXYE6CvFKoolkSd0eW15LzVeXzDULk5vj48XBexPWiqMWmnLMg0+4+FurLi/2utNjsqy1UwNdJNQq7pSmcdudcJ9ZCVQGqfIy8/JP2/mwhkqTVfywTBXWxNWRJobaKY7sBGpCB0hLS622p7A= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1763797657; 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=xSuwL98DsKB5x67Tjjby6HWj6UKShLztYvAH+9gQRhY=; b=mspfhjmwa9Ifl+AHkpirb/5gg57pQooFFM6PYr2y4GmrDUlwfsXS2JY0/1ItT6JcTsO6rV5JTZLwWqYwdM6HRLtYe0FeIPTFYVnkUXzmT2396+nMzyNMiCQ0vCEpVhFVkFusmbFILeYPoPaNcRXbtwGjagk7dE0mwgliwgX+sDI= 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 1763797657901397.8378971065; Fri, 21 Nov 2025 23:47:37 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1vMiHk-0007QQ-V7; Sat, 22 Nov 2025 02:43:57 -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 1vMhTw-0000hN-Js for qemu-devel@nongnu.org; Sat, 22 Nov 2025 01:52:31 -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 1vMhTS-0001Uz-9n for qemu-devel@nongnu.org; Sat, 22 Nov 2025 01:52:19 -0500 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:22 -0800 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 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1763794318; x=1795330318; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=NdIef43F78Ag8foHW0KVu5bgf8tD7h+BlEm9bwncXbc=; b=MCAYVIlGN/sdCmuFK0VGeuPgtauKej7WGNOONJqSvvNADRJ3/Q2/wPBd gm32bBEvwWyLVQAJU0ZKjeXcN3s1WECyDFda9z6dnpilo9Et51JqanMQj Ao2ZJ0hwTtxozWtEYizusVl2jfL8AY3Ur0mbGHygevHnFZuLNPZs3/jE1 YTcW3vdEb3kJAczJTVM20AgLAmejSpuZV0wayLX27HDxs3RIiWeCbSM5u 24QIUF5WnUdW2x0hPtZYsZEWgO2mPJoXrXGhqAEk0u/2Ri+88gWR6vFWe i5qCNSwZx9oXqDAL5nYoQRlFFN1kr0s89+HGP0yChHftz2x86v2kufBkE g==; X-CSE-ConnectionGUID: Dku2uadiS2WMzQkcxxJxcw== X-CSE-MsgGUID: qYHvB93rROe1+ag79rFviA== X-IronPort-AV: E=McAfee;i="6800,10657,11620"; a="65924079" X-IronPort-AV: E=Sophos;i="6.20,217,1758610800"; d="scan'208";a="65924079" X-CSE-ConnectionGUID: Pc3LCWpAQ86St8E/BX1+Xg== X-CSE-MsgGUID: NFv+/6uURSSG4KTaSdPtMg== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.20,217,1758610800"; d="scan'208";a="191064618" From: Vivek Kasireddy To: qemu-devel@nongnu.org Cc: Vivek Kasireddy , Alex Williamson , =?UTF-8?q?C=C3=A9dric=20Le=20Goater?= Subject: [PATCH v3 6/9] vfio/device: Add support for VFIO_DEVICE_FEATURE_DMA_BUF Date: Fri, 21 Nov 2025 22:46:27 -0800 Message-ID: <20251122064936.2948632-7-vivek.kasireddy@intel.com> X-Mailer: git-send-email 2.50.1 In-Reply-To: <20251122064936.2948632-1-vivek.kasireddy@intel.com> References: <20251122064936.2948632-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=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+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @intel.com) X-ZM-MESSAGEID: 1763797659413018900 In order to implement VFIO_DEVICE_FEATURE_DMA_BUF, we first need to identify the VFIO region and index the buffer (represented by iovec) belongs to and then translate its addresses to offsets within that region. The qemu_ram_block_from_host() API gives us both the region and the offset info we need to populate the dma ranges in order to invoke this feature. Cc: Alex Williamson Cc: C=C3=A9dric Le Goater Signed-off-by: Vivek Kasireddy --- hw/vfio/device.c | 45 +++++++++++++++++++++++++++++++++++ include/hw/vfio/vfio-device.h | 14 +++++++++++ 2 files changed, 59 insertions(+) diff --git a/hw/vfio/device.c b/hw/vfio/device.c index 9ff73f9941..0679e55e8c 100644 --- a/hw/vfio/device.c +++ b/hw/vfio/device.c @@ -21,6 +21,7 @@ #include "qemu/osdep.h" #include =20 +#include "system/ramblock.h" #include "hw/vfio/vfio-device.h" #include "hw/vfio/pci.h" #include "hw/hw.h" @@ -615,3 +616,47 @@ VFIODevice *vfio_device_lookup(MemoryRegion *mr) } return NULL; } + +int vfio_device_create_dmabuf_fd(VFIODevice *vbasedev, + struct iovec *iov, unsigned int iov_cnt) +{ + g_autofree struct vfio_device_feature *feature =3D NULL; + struct vfio_device_feature_dma_buf *dma_buf; + RAMBlock *rb, *first_rb; + ram_addr_t offset; + size_t argsz; + int i, index; + + argsz =3D sizeof(*feature) + sizeof (*dma_buf) + + sizeof(struct vfio_region_dma_range) * iov_cnt; + feature =3D g_malloc0(argsz); + dma_buf =3D (struct vfio_device_feature_dma_buf *)feature->data; + + first_rb =3D qemu_ram_block_from_host(iov[0].iov_base, false, &offset); + if (!first_rb) { + return -1; + } + + for (i =3D 0; i < iov_cnt; i++) { + rb =3D qemu_ram_block_from_host(iov[i].iov_base, false, &offset); + if (rb !=3D first_rb) { + return -1; + } + + index =3D vfio_get_region_index_from_mr(rb->mr); + if (index < 0) { + return -1; + } + + dma_buf->region_index =3D index; + dma_buf->dma_ranges[i].offset =3D offset; + dma_buf->dma_ranges[i].length =3D iov[i].iov_len; + } + + dma_buf->nr_ranges =3D iov_cnt; + dma_buf->open_flags =3D O_RDONLY | O_CLOEXEC; + feature->argsz =3D argsz; + feature->flags =3D VFIO_DEVICE_FEATURE_GET | VFIO_DEVICE_FEATURE_DMA_B= UF; + + return vbasedev->io_ops->device_feature(vbasedev, feature); +} diff --git a/include/hw/vfio/vfio-device.h b/include/hw/vfio/vfio-device.h index 2f8087f133..7fc2912f15 100644 --- a/include/hw/vfio/vfio-device.h +++ b/include/hw/vfio/vfio-device.h @@ -309,6 +309,20 @@ int vfio_get_region_index_from_mr(MemoryRegion *mr); * Returns the VFIO device if found or NULL. */ VFIODevice *vfio_device_lookup(MemoryRegion *mr); + +/** + * Create and return a dmabuf fd by first translating the addresses in the + * iovec array into VFIO region offsets and then invoking the + * VFIO_DEVICE_FEATURE_DMA_BUF feature. + * + * @vbasedev: #VFIODevice to use + * @iov: array of iovec entries associated with a buffer + * @iov_cnt: number of entries in the iovec array + * + * Returns the newly created dmabuf fd or -1 on error. + */ +int vfio_device_create_dmabuf_fd(VFIODevice *vbasedev, + struct iovec *iov, unsigned int iov_cnt); #endif =20 /* Returns 0 on success, or a negative errno. */ --=20 2.50.1 From nobody Sun Dec 14 06:35:53 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=1763797620; cv=none; d=zohomail.com; s=zohoarc; b=dg848mFUEMItMvoupcNv+iHIvt5bHDTohSEmYxOn/MLW5ESHPQ4AXtC9V5bZC/7HBlE0rmgaPuNvL8BLzpm8/kjrG2IUvo6mKcfZyV55qLCep8+JRP0wU1PnIbP8+EYNcShmpRVsP8+p8q2LiXiehCSCi32m3URUlhuGwSyBZ4w= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1763797620; 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=nZe4BNrnU7vMmKEaLRoqssfR5n7jSEdGdOzL/jifCkI=; b=IATNS5xqKJmfXxqUV8uIzpIoLe0tj0hs7mtSQJKkij/loUkIuROJU8a1UZtSF+yCe2cguCjwi5DNIRhKWRbxKW/IX+4FiT6nEuCDVruAa7iJ339/MW5qdjg1r0ASD/7ZoaPRxNyZ+FTPmO/f4SHLlngJxG12haEZCylnKLAJA1Y= 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 1763797620139848.9032155598894; Fri, 21 Nov 2025 23:47:00 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1vMiHK-0007Ij-Mv; Sat, 22 Nov 2025 02:43:31 -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 1vMhUI-0000kL-Jp for qemu-devel@nongnu.org; Sat, 22 Nov 2025 01:52:51 -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 1vMhTw-0001CG-Gs for qemu-devel@nongnu.org; Sat, 22 Nov 2025 01:52:47 -0500 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:22 -0800 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:23 -0800 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1763794348; x=1795330348; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=yrEJwSrBS5NSdtOP0SPNq0ppkXr4cjPLmNjHkU7a0gg=; b=SYkZ+Wdp5C5O2vLw6B64L8yHZhMRXGprW8Bkl6AXPcIDs8Gf9EfzKgTh XbJhMzD7c8RHScFoDAAuA0oajvmiPM94+z28pQSP+jXUGtDAehgH4PPSc a35rxiD+qWR7bPLzEEEYXcsxxLBYSKqYl1mdnUrrtEBeHE+gCnPArPIRb 4xqpt28AyMwL2GdD1gIF1gKRsZPE2WxJSdJ4hiz3GV01n1xDXJkofgQir pYMfHxZqwf3JFZ/D88DCmGLkIyXuPG+cyKIj8BO6FcAWaXDnJmqfaLZn3 h9vKEB4ljtjEa4ZQxPZMUjyoAbcfGLp2z86FHzlDhn0aStSJ1kTbbugtg g==; X-CSE-ConnectionGUID: 2XJ1YC1YRbOpMInb7/ULMg== X-CSE-MsgGUID: JTD+W/m9S5qoFFvg84CFBg== X-IronPort-AV: E=McAfee;i="6800,10657,11620"; a="65924086" X-IronPort-AV: E=Sophos;i="6.20,217,1758610800"; d="scan'208";a="65924086" X-CSE-ConnectionGUID: MXBUZ324SzqXo4AeDWnJiw== X-CSE-MsgGUID: FNgMqc24Rna55GSFHojajw== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.20,217,1758610800"; d="scan'208";a="191064621" 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?= Subject: [PATCH v3 7/9] virtio-gpu: Rename udmabuf files and helpers to dmabuf Date: Fri, 21 Nov 2025 22:46:28 -0800 Message-ID: <20251122064936.2948632-8-vivek.kasireddy@intel.com> X-Mailer: git-send-email 2.50.1 In-Reply-To: <20251122064936.2948632-1-vivek.kasireddy@intel.com> References: <20251122064936.2948632-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=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+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @intel.com) X-ZM-MESSAGEID: 1763797622131018900 This is prep-work for adding the ability to create dmabuf fds from VFIO devices in addition to udmabuf. Cc: Marc-Andr=C3=A9 Lureau Cc: Alex Benn=C3=A9e Cc: Akihiko Odaki Cc: Dmitry Osipenko Cc: Alex Williamson Cc: C=C3=A9dric Le Goater Reviewed-by: Akihiko Odaki Signed-off-by: Vivek Kasireddy --- hw/display/meson.build | 4 ++-- ...gpu-udmabuf-stubs.c =3D> virtio-gpu-dmabuf-stubs.c} | 4 ++-- .../{virtio-gpu-udmabuf.c =3D> virtio-gpu-dmabuf.c} | 12 ++++++------ hw/display/virtio-gpu.c | 8 ++++---- include/hw/virtio/virtio-gpu.h | 6 +++--- 5 files changed, 17 insertions(+), 17 deletions(-) rename hw/display/{virtio-gpu-udmabuf-stubs.c =3D> virtio-gpu-dmabuf-stubs= .c} (79%) rename hw/display/{virtio-gpu-udmabuf.c =3D> virtio-gpu-dmabuf.c} (94%) diff --git a/hw/display/meson.build b/hw/display/meson.build index 90e6c041bd..f5f92b1068 100644 --- a/hw/display/meson.build +++ b/hw/display/meson.build @@ -69,9 +69,9 @@ if config_all_devices.has_key('CONFIG_VIRTIO_GPU') virtio_gpu_ss.add(when: 'CONFIG_VIRTIO_GPU', if_true: [files('virtio-gpu-base.c', 'virtio-gpu.c'), = pixman]) if host_os =3D=3D 'linux' - virtio_gpu_ss.add(files('virtio-gpu-udmabuf.c')) + virtio_gpu_ss.add(files('virtio-gpu-dmabuf.c')) else - virtio_gpu_ss.add(files('virtio-gpu-udmabuf-stubs.c')) + virtio_gpu_ss.add(files('virtio-gpu-dmabuf-stubs.c')) endif virtio_gpu_ss.add(when: 'CONFIG_VHOST_USER_GPU', if_true: files('vhost-u= ser-gpu.c')) hw_display_modules +=3D {'virtio-gpu': virtio_gpu_ss} diff --git a/hw/display/virtio-gpu-udmabuf-stubs.c b/hw/display/virtio-gpu-= dmabuf-stubs.c similarity index 79% rename from hw/display/virtio-gpu-udmabuf-stubs.c rename to hw/display/virtio-gpu-dmabuf-stubs.c index f692e13510..01067e246d 100644 --- a/hw/display/virtio-gpu-udmabuf-stubs.c +++ b/hw/display/virtio-gpu-dmabuf-stubs.c @@ -7,12 +7,12 @@ bool virtio_gpu_have_udmabuf(void) return false; } =20 -void virtio_gpu_init_udmabuf(struct virtio_gpu_simple_resource *res) +void virtio_gpu_init_dmabuf(struct virtio_gpu_simple_resource *res) { /* nothing (stub) */ } =20 -void virtio_gpu_fini_udmabuf(struct virtio_gpu_simple_resource *res) +void virtio_gpu_fini_dmabuf(struct virtio_gpu_simple_resource *res) { /* nothing (stub) */ } diff --git a/hw/display/virtio-gpu-udmabuf.c b/hw/display/virtio-gpu-dmabuf= .c similarity index 94% rename from hw/display/virtio-gpu-udmabuf.c rename to hw/display/virtio-gpu-dmabuf.c index d804f321aa..c34d4c85bc 100644 --- a/hw/display/virtio-gpu-udmabuf.c +++ b/hw/display/virtio-gpu-dmabuf.c @@ -68,7 +68,7 @@ static void virtio_gpu_create_udmabuf(struct virtio_gpu_s= imple_resource *res) g_free(list); } =20 -static void virtio_gpu_remap_udmabuf(struct virtio_gpu_simple_resource *re= s) +static void virtio_gpu_remap_dmabuf(struct virtio_gpu_simple_resource *res) { res->remapped =3D mmap(NULL, res->blob_size, PROT_READ, MAP_SHARED, res->dmabuf_fd, 0); @@ -79,7 +79,7 @@ static void virtio_gpu_remap_udmabuf(struct virtio_gpu_si= mple_resource *res) } } =20 -static void virtio_gpu_destroy_udmabuf(struct virtio_gpu_simple_resource *= res) +static void virtio_gpu_destroy_dmabuf(struct virtio_gpu_simple_resource *r= es) { if (res->remapped) { munmap(res->remapped, res->blob_size); @@ -128,7 +128,7 @@ bool virtio_gpu_have_udmabuf(void) return memfd_backend; } =20 -void virtio_gpu_init_udmabuf(struct virtio_gpu_simple_resource *res) +void virtio_gpu_init_dmabuf(struct virtio_gpu_simple_resource *res) { void *pdata =3D NULL; =20 @@ -141,7 +141,7 @@ void virtio_gpu_init_udmabuf(struct virtio_gpu_simple_r= esource *res) if (res->dmabuf_fd < 0) { return; } - virtio_gpu_remap_udmabuf(res); + virtio_gpu_remap_dmabuf(res); if (!res->remapped) { return; } @@ -151,10 +151,10 @@ void virtio_gpu_init_udmabuf(struct virtio_gpu_simple= _resource *res) res->blob =3D pdata; } =20 -void virtio_gpu_fini_udmabuf(struct virtio_gpu_simple_resource *res) +void virtio_gpu_fini_dmabuf(struct virtio_gpu_simple_resource *res) { if (res->remapped) { - virtio_gpu_destroy_udmabuf(res); + virtio_gpu_destroy_dmabuf(res); } } =20 diff --git a/hw/display/virtio-gpu.c b/hw/display/virtio-gpu.c index d746c5f426..681702eea9 100644 --- a/hw/display/virtio-gpu.c +++ b/hw/display/virtio-gpu.c @@ -360,7 +360,7 @@ static void virtio_gpu_resource_create_blob(VirtIOGPU *= g, return; } =20 - virtio_gpu_init_udmabuf(res); + virtio_gpu_init_dmabuf(res); QTAILQ_INSERT_HEAD(&g->reslist, res, next); } =20 @@ -917,7 +917,7 @@ void virtio_gpu_cleanup_mapping(VirtIOGPU *g, res->addrs =3D NULL; =20 if (res->blob) { - virtio_gpu_fini_udmabuf(res); + virtio_gpu_fini_dmabuf(res); } } =20 @@ -954,7 +954,7 @@ virtio_gpu_resource_attach_backing(VirtIOGPU *g, } =20 if (res->blob_size) { - virtio_gpu_init_udmabuf(res); + virtio_gpu_init_dmabuf(res); } } =20 @@ -1440,7 +1440,7 @@ static int virtio_gpu_blob_load(QEMUFile *f, void *op= aque, size_t size, return -EINVAL; } =20 - virtio_gpu_init_udmabuf(res); + virtio_gpu_init_dmabuf(res); =20 resource_id =3D qemu_get_be32(f); } diff --git a/include/hw/virtio/virtio-gpu.h b/include/hw/virtio/virtio-gpu.h index 58e0f91fda..e3d351a2d4 100644 --- a/include/hw/virtio/virtio-gpu.h +++ b/include/hw/virtio/virtio-gpu.h @@ -354,10 +354,10 @@ bool virtio_gpu_scanout_blob_to_fb(struct virtio_gpu_= framebuffer *fb, struct virtio_gpu_set_scanout_blob *ss, uint64_t blob_size); =20 -/* virtio-gpu-udmabuf.c */ +/* virtio-gpu-dmabuf.c */ bool virtio_gpu_have_udmabuf(void); -void virtio_gpu_init_udmabuf(struct virtio_gpu_simple_resource *res); -void virtio_gpu_fini_udmabuf(struct virtio_gpu_simple_resource *res); +void virtio_gpu_init_dmabuf(struct virtio_gpu_simple_resource *res); +void virtio_gpu_fini_dmabuf(struct virtio_gpu_simple_resource *res); int virtio_gpu_update_dmabuf(VirtIOGPU *g, uint32_t scanout_id, struct virtio_gpu_simple_resource *res, --=20 2.50.1 From nobody Sun Dec 14 06:35:53 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=1763798006; cv=none; d=zohomail.com; s=zohoarc; b=Ti33LPOTeOQFV2d86gfpKnWavIqxYeiBVRKA0+aQ08U16ushQSndt6kPsYQKe8mybU1kgyczvvrZecN5hFZayW31gdz6Sb6Sf4VXdA8y77+HVB/ikznrPE0PPlgnRYvE+SaByUyUJvjTqdcQm4s0yuz7PXgDCS7IQNpFHZ1O/2Y= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1763798006; 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=MKQNIuJKQEPowgiBH3CeQj2ybbNKTLVDQcrIsMhp+gE=; b=JyLl8Lhr0pzB/1JVAJk9MvrqcAzWm8010wtUtbQNm4JdAQ2enAEacACGGrI7nibIXcggTMUsSAzyXY7BwGpggkIZDyD45uwCwuqQtlw72rG0esrv625v9K6D9OVlMJWZlrHmg0MYf8WaHkdsfp0ibc5yH9g8U4u9wUwoMWHMbgM= 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 17637980069270.697781230948749; Fri, 21 Nov 2025 23:53:26 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1vMiMF-0000U3-27; Sat, 22 Nov 2025 02:48:37 -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 1vMhUR-0000lS-0t for qemu-devel@nongnu.org; Sat, 22 Nov 2025 01:53:00 -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 1vMhU4-0001Uz-2n for qemu-devel@nongnu.org; Sat, 22 Nov 2025 01:52:55 -0500 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:22 -0800 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:23 -0800 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1763794356; x=1795330356; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=kMX3gK3C2jrvs4wVLl8xztI2o+0UwVJ4qGV1poeLrvc=; b=P1XeBPryrbtoy3ub9dhZJLx+P8uRRjLPMDk0O38Wek1WgflRGHn33UHB 2DnXQICpkaZHJfHw884tILtqzeRgWymtErDAFa4mnnJDCTYD3MX0f7w/t vX4BjKKG/srnaEkNwSwd2/1cruSVMnrROGgCNhmtCQVMp/6Or8xJAGNKQ PGTe1tMoyVmA1ZrWF9/fOQmolg5lnxy7iYRUHXO94YjZp3YgErnbmOUB2 gOFKVx6bjsVm9SQtb8G06Qp6KLWWuqdv97mbtt1Jork7xpsDoQxw6uTc+ QIXq4/J++XFOsrwd4/K1bhFZCNCCnJSt/EE8yja+sO759T9TkuHLMbLC4 Q==; X-CSE-ConnectionGUID: cIcslnDwTqyD9jVRcbAhvw== X-CSE-MsgGUID: Gc1HiX2uRH2Qs75EvC/WoQ== X-IronPort-AV: E=McAfee;i="6800,10657,11620"; a="65924088" X-IronPort-AV: E=Sophos;i="6.20,217,1758610800"; d="scan'208";a="65924088" X-CSE-ConnectionGUID: uCCP/QygS7OqN4c4ELGArg== X-CSE-MsgGUID: 1KYwNuBNT8OPsvPWQ383vg== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.20,217,1758610800"; d="scan'208";a="191064624" 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?= Subject: [PATCH v3 8/9] virtio-gpu-dmabuf: Introduce ram_block_is_memfd_backed() helper Date: Fri, 21 Nov 2025 22:46:29 -0800 Message-ID: <20251122064936.2948632-9-vivek.kasireddy@intel.com> X-Mailer: git-send-email 2.50.1 In-Reply-To: <20251122064936.2948632-1-vivek.kasireddy@intel.com> References: <20251122064936.2948632-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=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+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @intel.com) X-ZM-MESSAGEID: 1763798008651018900 This helper function provides a way to determine if a ram block is backed by memfd by checking the SEALS on the associated fd. This is useful in scenarios where we need to quickly verify if a given memory region is associated with a memfd or not. Cc: Marc-Andr=C3=A9 Lureau Cc: Alex Benn=C3=A9e Cc: Akihiko Odaki Cc: Dmitry Osipenko Cc: Alex Williamson Cc: C=C3=A9dric Le Goater Signed-off-by: Vivek Kasireddy --- hw/display/virtio-gpu-dmabuf.c | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/hw/display/virtio-gpu-dmabuf.c b/hw/display/virtio-gpu-dmabuf.c index c34d4c85bc..258c48d31b 100644 --- a/hw/display/virtio-gpu-dmabuf.c +++ b/hw/display/virtio-gpu-dmabuf.c @@ -27,6 +27,19 @@ #include "standard-headers/linux/udmabuf.h" #include "standard-headers/drm/drm_fourcc.h" =20 +static bool ram_block_is_memfd_backed(RAMBlock *rb) +{ + int ret; + + if (rb && rb->fd > 0) { + ret =3D fcntl(rb->fd, F_GET_SEALS); + if (ret > 0) { + return true; + } + } + return false; +} + static void virtio_gpu_create_udmabuf(struct virtio_gpu_simple_resource *r= es) { struct udmabuf_create_list *list; @@ -94,20 +107,14 @@ static void virtio_gpu_destroy_dmabuf(struct virtio_gp= u_simple_resource *res) static int find_memory_backend_type(Object *obj, void *opaque) { bool *memfd_backend =3D opaque; - int ret; =20 if (object_dynamic_cast(obj, TYPE_MEMORY_BACKEND)) { HostMemoryBackend *backend =3D MEMORY_BACKEND(obj); - RAMBlock *rb =3D backend->mr.ram_block; =20 - if (rb && rb->fd > 0) { - ret =3D fcntl(rb->fd, F_GET_SEALS); - if (ret > 0) { - *memfd_backend =3D true; - } + if (ram_block_is_memfd_backed(backend->mr.ram_block)) { + *memfd_backend =3D true; } } - return 0; } =20 --=20 2.50.1 From nobody Sun Dec 14 06:35:53 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=1763797689; cv=none; d=zohomail.com; s=zohoarc; b=X3jf7yfV7xKCZ/lFqIikJYC6/Oct3JacT58lNuOvEdAMxVa0pgTv2xHsXWgeWZ3C4MkG7BhjfFejC53FtSBdwKbrISa7b+itzyg5xDrYSuMKpJ5gZ6VssUgin/32BVmrmTuYx9/wKNXgs0z7+2wMyT81bX782JeVqNaEzVpWfrk= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1763797689; 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=DpKioF80o8wiJUxat7ISAomtXL1640lwjMZVtE4yuiM=; b=VI40ILuzUdcrdBcPJhfvylTr2uQNtXtcKciEse1pBDjwfowdcsTbddvhh4DNarihK8lcKnSXJxfEZGyANIQULSY4Em8a7uFXRbPOkzYt34CDhdA4OzrzAUqeQQ1DpMG8uKktYuKywVgTf+Xn9cArLKDv9AusKJc4F9+qupxCC3s= 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 176379768935028.50707601555473; Fri, 21 Nov 2025 23:48:09 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1vMiI2-0007VZ-AE; Sat, 22 Nov 2025 02:44:15 -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 1vMhUj-0000p0-FE for qemu-devel@nongnu.org; Sat, 22 Nov 2025 01:53:19 -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 1vMhUO-0000qe-UW for qemu-devel@nongnu.org; Sat, 22 Nov 2025 01:53:15 -0500 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:23 -0800 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:23 -0800 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1763794377; x=1795330377; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=smgGgL/ZYVI5ce8SjP1EJqdFG4Iis2gp0xNgwEfl7zA=; b=RxS6V2WLFE9zQc1wtCVrsVOlJE52yLO8ktYd+fAVsrH3w59aHl6zdJAM 2llOHACswvlWFMrXoWHpBXUvA8bvgFJ6jZMByidvGqgvLZ0w6koUesyGB 4ppaBlL957U1X8tx/baskzoP8yZnzS50G8Yg4vtRJ4Acc8dFUFbBLGyBc tNuINIBMPBWwKpVkznEsVJanKdiimGTKrq98vI+WBsZufHOoR12vK1hZ7 T7fGfECW5I80K4XxNp+fNG87+vctT2Pv0+MbbLU7YJ2aFge2HV5x3StZ7 v5bmuREmNaK4J0nWcoTWqkLAc2Ulc90435PDAhA+fCS/ezMq3KZOvL5jg Q==; X-CSE-ConnectionGUID: quCGB9HeTOyXssqArUxK+w== X-CSE-MsgGUID: kfODCzTfQ+GdaBqrQxlNqA== X-IronPort-AV: E=McAfee;i="6800,10657,11620"; a="65924090" X-IronPort-AV: E=Sophos;i="6.20,217,1758610800"; d="scan'208";a="65924090" X-CSE-ConnectionGUID: K8HnQu+PSq+t2Q1flUW3GA== X-CSE-MsgGUID: Yi1ZZQkDSdGKGvPhuKzq5w== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.20,217,1758610800"; d="scan'208";a="191064629" 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?= Subject: [PATCH v3 9/9] virtio-gpu-dmabuf: Create dmabuf for blobs associated with VFIO devices Date: Fri, 21 Nov 2025 22:46:30 -0800 Message-ID: <20251122064936.2948632-10-vivek.kasireddy@intel.com> X-Mailer: git-send-email 2.50.1 In-Reply-To: <20251122064936.2948632-1-vivek.kasireddy@intel.com> References: <20251122064936.2948632-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=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+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @intel.com) X-ZM-MESSAGEID: 1763797691745018900 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, we first call virtio_gpu_create_udmabuf() which further calls ram_block_is_memfd_backed() to check if the blob's backing storage is located in a memfd or not. If it is not, we call vfio_create_dmabuf() which identifies the right VFIO device and eventually invokes the API vfio_device_create_dmabuf_fd() to have a dmabuf fd created. Note that in virtio_gpu_remap_dmabuf(), we first try to test if the VFIO dmabuf exporter supports mmap or not. If it doesn't, we use the VFIO device fd directly to create the CPU mapping. While at it, remove the unnecessary rcu_read_lock/rcu_read_unlock from virtio_gpu_create_udmabuf(). Cc: Marc-Andr=C3=A9 Lureau Cc: Alex Benn=C3=A9e Cc: Akihiko Odaki Cc: Dmitry Osipenko Cc: Alex Williamson Cc: C=C3=A9dric Le Goater Signed-off-by: Vivek Kasireddy --- hw/display/Kconfig | 5 ++ hw/display/virtio-gpu-dmabuf.c | 122 ++++++++++++++++++++++++++++++--- 2 files changed, 119 insertions(+), 8 deletions(-) diff --git a/hw/display/Kconfig b/hw/display/Kconfig index 1e95ab28ef..0d090f25f5 100644 --- a/hw/display/Kconfig +++ b/hw/display/Kconfig @@ -106,6 +106,11 @@ config VIRTIO_VGA depends on VIRTIO_PCI select VGA =20 +config VIRTIO_GPU_VFIO_BLOB + bool + default y + depends on VFIO + config VHOST_USER_GPU bool default y diff --git a/hw/display/virtio-gpu-dmabuf.c b/hw/display/virtio-gpu-dmabuf.c index 258c48d31b..d121a2c9a7 100644 --- a/hw/display/virtio-gpu-dmabuf.c +++ b/hw/display/virtio-gpu-dmabuf.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" @@ -40,10 +41,42 @@ static bool ram_block_is_memfd_backed(RAMBlock *rb) return false; } =20 +static void vfio_create_dmabuf(struct virtio_gpu_simple_resource *res) +{ +#if defined(VIRTIO_GPU_VFIO_BLOB) + VFIODevice *vbasedev; + RAMBlock *first_rb; + ram_addr_t offset; + + first_rb =3D qemu_ram_block_from_host(res->iov[0].iov_base, false, &of= fset); + if (!first_rb) { + qemu_log_mask(LOG_GUEST_ERROR, + "%s: Could not find ramblock\n", __func__); + return; + } + + vbasedev =3D vfio_device_lookup(first_rb->mr); + if (!vbasedev) { + qemu_log_mask(LOG_GUEST_ERROR, + "%s: No VFIO device found to create dmabuf from\n", + __func__); + return; + } + + res->dmabuf_fd =3D vfio_device_create_dmabuf_fd(vbasedev, + res->iov, res->iov_cnt); + if (res->dmabuf_fd < 0) { + qemu_log_mask(LOG_GUEST_ERROR, + "%s: VFIO_DEVICE_FEATURE_DMA_BUF: %s\n", + __func__, strerror(errno)); + } +#endif +} + static void virtio_gpu_create_udmabuf(struct virtio_gpu_simple_resource *r= es) { struct udmabuf_create_list *list; - RAMBlock *rb; + RAMBlock *rb, *first_rb; ram_addr_t offset; int udmabuf, i; =20 @@ -52,15 +85,17 @@ static void virtio_gpu_create_udmabuf(struct virtio_gpu= _simple_resource *res) return; } =20 + first_rb =3D qemu_ram_block_from_host(res->iov[0].iov_base, false, &of= fset); + if (!ram_block_is_memfd_backed(first_rb)) { + return; + } + list =3D g_malloc0(sizeof(struct udmabuf_create_list) + sizeof(struct udmabuf_create_item) * res->iov_cnt); =20 for (i =3D 0; i < res->iov_cnt; i++) { - rcu_read_lock(); rb =3D qemu_ram_block_from_host(res->iov[i].iov_base, false, &offs= et); - rcu_read_unlock(); - - if (!rb || rb->fd < 0) { + if (rb !=3D first_rb) { g_free(list); return; } @@ -81,11 +116,77 @@ static void virtio_gpu_create_udmabuf(struct virtio_gp= u_simple_resource *res) g_free(list); } =20 +static void *vfio_dmabuf_mmap(struct virtio_gpu_simple_resource *res) +{ + struct vfio_region_info *info =3D NULL; + VFIODevice *vbasedev =3D NULL; + ram_addr_t offset, len =3D 0; + RAMBlock *first_rb, *rb; + void *map, *submap; + int i, ret =3D -1; + + first_rb =3D qemu_ram_block_from_host(res->iov[0].iov_base, false, &of= fset); + if (!first_rb) { + return MAP_FAILED; + } +#if defined(VIRTIO_GPU_VFIO_BLOB) + vbasedev =3D vfio_device_lookup(first_rb->mr); +#endif + if (!vbasedev) { + return MAP_FAILED; + } + + /* + * We first reserve a contiguous chunk of address space for the entire + * dmabuf, then replace it with smaller mappings that correspond to the + * individual segments of the dmabuf. + */ + map =3D mmap(NULL, res->blob_size, PROT_READ, MAP_SHARED, vbasedev->fd= , 0); + if (map =3D=3D MAP_FAILED) { + return map; + } + + for (i =3D 0; i < res->iov_cnt; i++) { + rb =3D qemu_ram_block_from_host(res->iov[i].iov_base, false, &offs= et); + if (rb !=3D first_rb) { + goto err; + } +#if defined(VIRTIO_GPU_VFIO_BLOB) + ret =3D vfio_get_region_index_from_mr(rb->mr); + if (ret < 0) { + goto err; + } + + ret =3D vfio_device_get_region_info(vbasedev, ret, &info); +#endif + if (ret < 0 || !info) { + goto err; + } + + submap =3D mmap(map + len, res->iov[i].iov_len, PROT_READ, + MAP_SHARED | MAP_FIXED, vbasedev->fd, + info->offset + offset); + if (submap =3D=3D MAP_FAILED) { + goto err; + } + + len +=3D res->iov[i].iov_len; + } + return map; +err: + munmap(map, res->blob_size); + return MAP_FAILED; +} + static void virtio_gpu_remap_dmabuf(struct virtio_gpu_simple_resource *res) { res->remapped =3D mmap(NULL, res->blob_size, PROT_READ, MAP_SHARED, res->dmabuf_fd, 0); if (res->remapped =3D=3D MAP_FAILED) { + res->remapped =3D vfio_dmabuf_mmap(res); + if (res->remapped !=3D MAP_FAILED) { + return; + } warn_report("%s: dmabuf mmap failed: %s", __func__, strerror(errno)); res->remapped =3D NULL; @@ -146,6 +247,13 @@ void virtio_gpu_init_dmabuf(struct virtio_gpu_simple_r= esource *res) } else { virtio_gpu_create_udmabuf(res); if (res->dmabuf_fd < 0) { + vfio_create_dmabuf(res); + } + + if (res->dmabuf_fd < 0) { + qemu_log_mask(LOG_GUEST_ERROR, + "%s: memory region cannot be used to create dmab= uf\n", + __func__); return; } virtio_gpu_remap_dmabuf(res); @@ -160,9 +268,7 @@ void virtio_gpu_init_dmabuf(struct virtio_gpu_simple_re= source *res) =20 void virtio_gpu_fini_dmabuf(struct virtio_gpu_simple_resource *res) { - if (res->remapped) { - virtio_gpu_destroy_dmabuf(res); - } + virtio_gpu_destroy_dmabuf(res); } =20 static void virtio_gpu_free_dmabuf(VirtIOGPU *g, VGPUDMABuf *dmabuf) --=20 2.50.1