From nobody Sat Feb 7 07:15:14 2026 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=1770104033; cv=none; d=zohomail.com; s=zohoarc; b=W5qvbei7em2Qo9m15n6d9OxunPTAWy7pEwEoGkzh1HcGlvzBG35W40d58oLDi5pwWBx5CL+TZJpfZVeb6fwjrZQ21621vjOoQuLo9a+QLomFKRa+M9p8nAPrOfyR4Nx3DMC3bN+LweorZUpwxql5/OMdjy120zWKMo2XFRBuqpg= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1770104033; 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=Za2qhe1y+HmtK8JUlxXFkybmLOSt8TA8eqoYAC3piks=; b=Cj0c7fY55lGSpk/jlmm8qu3+0CCf2Wy5TQk/16j3Wnt/jMlDR9lXqh7bLCHnr+YtyMUCXqy8DlXELcCvQKLAq4HY2ypvGL4gvpBT6/JkI1zFc4amRbwNoA9d7CiOTp0t1WeuJExv59bwZEAoLXX7bOVrSBuNaFVSDUUO2yDeJko= 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 1770104033390569.8155318957811; Mon, 2 Feb 2026 23:33:53 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1vnAuF-0007VO-G3; Tue, 03 Feb 2026 02:33:03 -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 1vnAuD-0007Uz-UW for qemu-devel@nongnu.org; Tue, 03 Feb 2026 02:33:01 -0500 Received: from mgamail.intel.com ([192.198.163.10]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1vnAuC-0006DN-2U for qemu-devel@nongnu.org; Tue, 03 Feb 2026 02:33:01 -0500 Received: from orviesa001.jf.intel.com ([10.64.159.141]) by fmvoesa104.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 02 Feb 2026 23:32:37 -0800 Received: from vkasired-desk2.fm.intel.com ([10.105.128.132]) by smtpauth.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 02 Feb 2026 23:32:37 -0800 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1770103980; x=1801639980; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=7KFkFFpt1PDG4pLWKDo1miZZKiFJdXxbhssCm6V///U=; b=BFtkVS9RILxehg+i9t0lMM6fUuTboNp+bSVzU9iuElPQ9D56bm8RzaCK zejgi5WFSPLBU2R4wvJrK3TlLmK3E9qbRlgKVMIZbeaHTqZqWWQkqFmi+ ayjJ6yKuw8MwhO1E1ntbrWI4mVdET1oFlqK9mOjQm38RBF39DQ4Ftnmnx NrazYFDItRo1wgjCys66KI4BIPvrsbG0bxZgJqVUNPQ5aIWCm+xB7IfNE DZdgCIaHXuNyiZgweq+bPXV4u5upe7UnKVs6hHXKkw1NbWpCjaMBlZa98 TWjVt33RJMjhxIt1Z2bBZm6KsBlgqg5GH6v2I1q9JVo5VjNGjaSckMBx1 Q==; X-CSE-ConnectionGUID: 2FoN7TGUT2a/qfetPNmeCw== X-CSE-MsgGUID: Aqn9MNt6RpWHzIYNSLp0SA== X-IronPort-AV: E=McAfee;i="6800,10657,11690"; a="82640925" X-IronPort-AV: E=Sophos;i="6.21,270,1763452800"; d="scan'208";a="82640925" X-CSE-ConnectionGUID: i1kzHAZoQkqDWh8UP3W3rg== X-CSE-MsgGUID: U/1RTMMjQN+0fRQaGmc9yQ== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.21,270,1763452800"; d="scan'208";a="247373116" From: Vivek Kasireddy To: qemu-devel@nongnu.org Cc: Vivek Kasireddy , Alex Williamson , =?UTF-8?q?C=C3=A9dric=20Le=20Goater?= Subject: [PATCH v5 10/12] vfio/device: Add support for creating dmabuf from multiple ranges Date: Mon, 2 Feb 2026 23:30:17 -0800 Message-ID: <20260203073200.848183-11-vivek.kasireddy@intel.com> X-Mailer: git-send-email 2.50.1 In-Reply-To: <20260203073200.848183-1-vivek.kasireddy@intel.com> References: <20260203073200.848183-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.10; 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_RPBL_BLOCKED=0.001, RCVD_IN_VALIDITY_SAFE_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: qemu development 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: 1770104122833158501 In order to create a dmabuf associated with a buffer that spans multiple ranges, 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 so that we can invoke the VFIO_DEVICE_FEATURE_DMA_BUF feature to create the dmabuf. Cc: Alex Williamson Cc: C=C3=A9dric Le Goater Signed-off-by: Vivek Kasireddy --- hw/vfio/device.c | 58 +++++++++++++++++++++++++++++++++++ include/hw/vfio/vfio-device.h | 14 +++++++++ 2 files changed, 72 insertions(+) diff --git a/hw/vfio/device.c b/hw/vfio/device.c index ecb3581fcc..a7603600f3 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/core/iommu.h" @@ -667,3 +668,60 @@ static bool vfio_device_lookup(struct iovec *iov, VFIO= Device **vbasedevp, error_setg(errp, "No VFIO device found to create dmabuf from\n"); return false; } + +bool vfio_device_create_dmabuf(struct iovec *iov, unsigned int iov_cnt, + int *fd, Error **errp) +{ + g_autofree struct vfio_device_feature *feature =3D NULL; + struct vfio_device_feature_dma_buf *dma_buf; + RAMBlock *rb, *first_rb; + VFIODevice *vbasedev; + ram_addr_t offset; + int i, index; + size_t argsz; + + argsz =3D sizeof(*feature) + sizeof (*dma_buf) + + sizeof(struct vfio_region_dma_range) * iov_cnt; + feature =3D g_malloc0(argsz); + *feature =3D (struct vfio_device_feature) { + .argsz =3D argsz, + .flags =3D VFIO_DEVICE_FEATURE_GET | VFIO_DEVICE_FEATURE_DMA_BUF, + }; + dma_buf =3D (struct vfio_device_feature_dma_buf *)feature->data; + + if (!vfio_device_lookup(iov, &vbasedev, errp)) { + return false; + } + + for (i =3D 0; i < iov_cnt; i++) { + rb =3D qemu_ram_block_from_host(iov[i].iov_base, false, &offset); + if (i =3D=3D 0) { + first_rb =3D rb; + } + if (!rb || rb !=3D first_rb) { + error_setg(errp, "Cannot create dmabuf with different regions\= n"); + return false; + } + + index =3D vfio_get_region_index_from_mr(rb->mr); + if (index < 0) { + error_setg(errp, "Cannot find region index for dmabuf segment\= n"); + return false; + } + + 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; + + *fd =3D vfio_device_get_feature(vbasedev, feature); + if (*fd < 0) { + error_setg_errno(errp, -(*fd), + "Could not create dmabuf fd via VFIO device"); + return false; + } + return true; +} diff --git a/include/hw/vfio/vfio-device.h b/include/hw/vfio/vfio-device.h index 596c7f5a10..003bac10b5 100644 --- a/include/hw/vfio/vfio-device.h +++ b/include/hw/vfio/vfio-device.h @@ -318,6 +318,20 @@ 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); + +/** + * Create 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. + * + * @iov: array of iovec entries associated with a buffer + * @iov_cnt: number of entries in the iovec array + * @fd: pointer to store the resulting dmabuf fd + * + * Returns true on success and false in case of an error. + */ +bool vfio_device_create_dmabuf(struct iovec *iov, unsigned int iov_cnt, + int *fd, Error **errp); #endif =20 /* Returns 0 on success, or a negative errno. */ --=20 2.50.1