From nobody Sun Jan 25 12:00:08 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=1769149412; cv=none; d=zohomail.com; s=zohoarc; b=DFH0rR4BLw5pYx6oOrz3K+N31gSewV2waJzpBlLxalXwEDjd58eNcsf3oajH3aoAKJqFK/ZF7Eu9rLT8/aH+lmS7IrF+cqdyrxC8hrSvDHc91wNIqKxQbh+DzHzoI5R3QnH5feuXi7zc3lhowTVTrLa7pESB+yYBPIEWPAwXjVY= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1769149412; 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=GYn6HbcHJYhdAYEpyxkmqNdfzV1wASWBnLNtHnpxVl8=; b=TyFprLHhZN0itOFCI5jcIbBRJm6rLD0Uuvgo28M/5LB7xi4HD8AuOV2h4nxIUUzfZZLCoFiv8seLpph2FnrUyfgG8oVCb6SUXUb7dgJd2PjhS1H5QfGe/QsTFttJmw3sqETleDj7E/sd95WgMWmf2opVFLv04pNyQ2PB5u+n7Jw= 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 176914941278112.513508112405702; Thu, 22 Jan 2026 22:23:32 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1vjAYm-0003Bl-FG; Fri, 23 Jan 2026 01:22:20 -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 1vjAYl-0003Ak-4j for qemu-devel@nongnu.org; Fri, 23 Jan 2026 01:22:19 -0500 Received: from mgamail.intel.com ([198.175.65.14]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1vjAYj-0000rq-AQ for qemu-devel@nongnu.org; Fri, 23 Jan 2026 01:22:18 -0500 Received: from fmviesa010.fm.intel.com ([10.60.135.150]) by orvoesa106.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 22 Jan 2026 22:22:06 -0800 Received: from unknown (HELO vkasired-desk2.fm.intel.com) ([10.105.128.132]) by fmviesa010-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 22 Jan 2026 22:22:05 -0800 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1769149337; x=1800685337; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=9z4wt3h8g6G2FMrJhvHLEY2taO5XQrbNTl+7oirh0+8=; b=LmiSUKCVrtBFmThNzuMROr15WvMo54YmE59lruugEvEjJeG6KSsUM2GS 4b1s5vT21rPCMs64THo/0GTOGsewe/WJNxnkNGID6GjlkkZ9QjXvd663g 7I9CxsWKQTtUtqK/jJESjee9u6H5n7g1M6PgPuomyfdvdzVhqP7PeiYR4 ZiSKNTaWxLPKFdYPZ6CCYXDuSm/DXI5eZ9OJlvDd2xUL2V0GirZspCf3h ZH6XkRRHbykpwNOPhB7Cv6tHMm8il8EIZXEQ+Yo8SwDS+EQ3w8gSmxcXI Lrn4VkrELYfvcl7Gnb51ApdsbUcOCPMFbEmek/Avd65bN6btZhjBAibbV w==; X-CSE-ConnectionGUID: SQbDNshpRTu+io+vW17cQQ== X-CSE-MsgGUID: nZM3YcQeRtWHqtRz6qhYZQ== X-IronPort-AV: E=McAfee;i="6800,10657,11679"; a="74254206" X-IronPort-AV: E=Sophos;i="6.21,247,1763452800"; d="scan'208";a="74254206" X-CSE-ConnectionGUID: V2H2UuOOQgSZ0Wt5SURKFw== X-CSE-MsgGUID: WhwcxvSqQ4i5cJQp3trm8w== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.21,247,1763452800"; d="scan'208";a="207287414" From: Vivek Kasireddy To: qemu-devel@nongnu.org Cc: Vivek Kasireddy , Alex Williamson , =?UTF-8?q?C=C3=A9dric=20Le=20Goater?= Subject: [PATCH v4 6/8] vfio/device: Add support for creating dmabuf from multiple ranges Date: Thu, 22 Jan 2026 22:17:45 -0800 Message-ID: <20260123062042.568703-7-vivek.kasireddy@intel.com> X-Mailer: git-send-email 2.50.1 In-Reply-To: <20260123062042.568703-1-vivek.kasireddy@intel.com> References: <20260123062042.568703-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.14; envelope-from=vivek.kasireddy@intel.com; helo=mgamail.intel.com X-Spam_score_int: -44 X-Spam_score: -4.5 X-Spam_bar: ---- X-Spam_report: (-4.5 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.07, 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: 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: 1769149414511158500 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 | 44 +++++++++++++++++++++++++++++++++++ include/hw/vfio/vfio-device.h | 14 +++++++++++ 2 files changed, 58 insertions(+) diff --git a/hw/vfio/device.c b/hw/vfio/device.c index afeff0f034..90d7758d0d 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" @@ -656,3 +657,46 @@ 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 =3D NULL; + 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); + *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; + + 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) { + 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; + return vfio_device_get_feature(vbasedev, feature); +} diff --git a/include/hw/vfio/vfio-device.h b/include/hw/vfio/vfio-device.h index ccf2fd887c..2f7e50f7ce 100644 --- a/include/hw/vfio/vfio-device.h +++ b/include/hw/vfio/vfio-device.h @@ -327,6 +327,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