From nobody Mon Apr 6 16:31:20 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=1773897873; cv=none; d=zohomail.com; s=zohoarc; b=VjYyi+8Ndt4V1ZTWSXv3KZUFEWrpGgGKSbXTLKToPmRYV8i5wvcFfcX+KsnLEeALEqN0fJN+Zvlt9C/UXBCh5HyakvpI79ir96vJuY1Gi9uqfUNLf7VjUqDBIx6dxCKbdZbY8BQ2A/hoPYYkj/oYqdw+7+XO9uimbKJR2aD4Da8= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1773897873; 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=YG5ARRIGxtGvnSLN/dAt7ETPJ9eFMgzjkatSch2cS6M=; b=UmWVPAMfEcfJ7yh7EjynQ0qIk9jXBUMIFrxNU5gSbQ/7Bazrb2IUNXirNVSKOVmh8JCDojYNRd6ZtgcsWnnSUQkm7hZLwDJehFnBOI/fhz7LUylv1qqZBfWYepYZbhQTQgkWXhwh35Zn0Q/4ccvGFIe+IshutObfxbN/4bpel3I= 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 1773897873571379.50126774177113; Wed, 18 Mar 2026 22:24:33 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1w35r0-0004p5-MA; Thu, 19 Mar 2026 01:23:30 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1w35qo-0004ng-Nf for qemu-devel@nongnu.org; Thu, 19 Mar 2026 01:23:19 -0400 Received: from mgamail.intel.com ([198.175.65.20]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1w35ql-0007tU-H5 for qemu-devel@nongnu.org; Thu, 19 Mar 2026 01:23:18 -0400 Received: from fmviesa006.fm.intel.com ([10.60.135.146]) by orvoesa112.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 18 Mar 2026 22:23:07 -0700 Received: from vkasired-desk2.fm.intel.com ([10.105.128.132]) by fmviesa006-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 18 Mar 2026 22:23:04 -0700 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1773897796; x=1805433796; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=TgTxvHCWi3fXEA43aM8mwolMJTMmN/YWQczQ7WYTzBs=; b=IJ7iaP1xFSM3EY1ukVqetpGAsg2gY50Lx74NUbqnAj4q4ZYNFeRyGk6m tUoa90C3Di0ypJigPWO1owbA4s6FQWJ9PB2hIN3GGz6z/95oPtMXqKKGR N6LBeWvZOCQA6+qduPhApw0cCI9E3TxKTJsVDq58/reTPoNOE/X7KR54O VyDm6dYG8uCVnv/+fsP7c2zQ4wiu84JJlSTYC1KwkTW5W9D1rLS5794XH w87OtEPZUYvlK5XCGTFBfNAtZ8Ej6vzzk7/kaYvvQmM1QFcjtY2aTJP+9 vg+5LQbf9930jGMWqohWxl3z7yTIR2my1fOzpfikXcjcVa1eDIORz9oZg A==; X-CSE-ConnectionGUID: pdLCrS0DTdGejwfvBDWJPA== X-CSE-MsgGUID: v6rZXbBBTPGDrWH4deB82Q== X-IronPort-AV: E=McAfee;i="6800,10657,11733"; a="74660395" X-IronPort-AV: E=Sophos;i="6.23,128,1770624000"; d="scan'208";a="74660395" X-CSE-ConnectionGUID: QMV0mCFZQHOUbcmrwwRrVw== X-CSE-MsgGUID: iAD9roUmQbqj9Denh15ktQ== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.23,128,1770624000"; d="scan'208";a="218355533" From: Vivek Kasireddy To: qemu-devel@nongnu.org Cc: Vivek Kasireddy , Alex Williamson , =?UTF-8?q?C=C3=A9dric=20Le=20Goater?= Subject: [PATCH v12 08/10] vfio/device: Add a helper to mmap a dmabuf Date: Wed, 18 Mar 2026 22:15:57 -0700 Message-ID: <20260319052023.2088685-9-vivek.kasireddy@intel.com> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20260319052023.2088685-1-vivek.kasireddy@intel.com> References: <20260319052023.2088685-1-vivek.kasireddy@intel.com> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=198.175.65.20; envelope-from=vivek.kasireddy@intel.com; helo=mgamail.intel.com X-Spam_score_int: -26 X-Spam_score: -2.7 X-Spam_bar: -- X-Spam_report: (-2.7 / 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.819, RCVD_IN_VALIDITY_SAFE_BLOCKED=0.903, 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: 1773897874646154100 In order to mmap a dmabuf, we first need to create a placeholder mapping that would fit the entire size of the dmabuf. This mapping would then be replaced with smaller mappings of individual dmabuf segments. Cc: Alex Williamson Cc: C=C3=A9dric Le Goater Signed-off-by: Vivek Kasireddy --- include/hw/vfio/vfio-device.h | 16 ++++++++++ hw/vfio/device.c | 59 +++++++++++++++++++++++++++++++++++ hw/vfio/dmabuf-stubs.c | 7 +++++ 3 files changed, 82 insertions(+) diff --git a/include/hw/vfio/vfio-device.h b/include/hw/vfio/vfio-device.h index d46640ff53..14909490ef 100644 --- a/include/hw/vfio/vfio-device.h +++ b/include/hw/vfio/vfio-device.h @@ -331,6 +331,22 @@ int vfio_device_get_irq_info(VFIODevice *vbasedev, int= index, */ int vfio_device_create_dmabuf_fd(struct iovec *iov, unsigned int iov_cnt, size_t total_size, Error **errp); + +/** + * Mmap a dmabuf by first translating the addresses in the iovec + * array into VFIO region offsets and then creating a placeholder + * mapping that would be replaced later with mappings that + * correspond to the dmabuf segments. + * + * @iov: array of iovec entries associated with the dmabuf + * @iov_cnt: number of entries in the iovec array + * @total_size: total size of the dmabuf + * @errp: pointer to Error*, to store an error if it happens. + * + * Returns the mapped pointer on success and NULL on error. + */ +void *vfio_device_mmap_dmabuf(struct iovec *iov, unsigned int iov_cnt, + size_t total_size, Error **errp); #endif =20 /* Returns 0 on success, or a negative errno. */ diff --git a/hw/vfio/device.c b/hw/vfio/device.c index 542c378913..43223ceacd 100644 --- a/hw/vfio/device.c +++ b/hw/vfio/device.c @@ -758,3 +758,62 @@ int vfio_device_create_dmabuf_fd(struct iovec *iov, un= signed int iov_cnt, } return ret; } + +void *vfio_device_mmap_dmabuf(struct iovec *iov, unsigned int iov_cnt, + size_t total_size, Error **errp) +{ + struct vfio_region_info *info =3D NULL; + ram_addr_t offset, len =3D 0; + VFIODevice *vbasedev; + VFIORegion *region; + RAMBlock *first_rb; + void *map, *submap; + int i; + + if (iov_size(iov, iov_cnt) !=3D total_size) { + error_setg(errp, "Total size of iov does not match dmabuf size\n"); + return NULL; + } + + if (!vfio_device_lookup(iov, &vbasedev, &first_rb, errp)) { + return NULL; + } + + /* + * 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, total_size, PROT_NONE, MAP_SHARED | MAP_ANONYMOUS, = -1, 0); + if (map =3D=3D MAP_FAILED) { + error_setg_errno(errp, errno, "Could not reserve placeholder mappi= ng"); + return NULL; + } + + for (i =3D 0; i < iov_cnt; i++) { + if (!vfio_region_lookup(iov[i].iov_base, ®ion, + first_rb, &offset, errp)) { + goto err; + } + + if (vfio_device_get_region_info(vbasedev, region->nr, &info)) { + error_setg(errp, "Cannot find region info for dmabuf segment\n= "); + goto err; + } + + submap =3D mmap(map + len, iov[i].iov_len, PROT_READ, + MAP_SHARED | MAP_FIXED, vbasedev->fd, + info->offset + offset); + if (submap =3D=3D MAP_FAILED) { + error_setg_errno(errp, errno, "Could not mmap dmabuf segment\n= "); + goto err; + } + len +=3D iov[i].iov_len; + } + return map; +err: + munmap(map, total_size); + error_prepend(errp, "VFIO dmabuf mmap failed: "); + return NULL; +} + diff --git a/hw/vfio/dmabuf-stubs.c b/hw/vfio/dmabuf-stubs.c index 374bd2a188..044d1ed058 100644 --- a/hw/vfio/dmabuf-stubs.c +++ b/hw/vfio/dmabuf-stubs.c @@ -15,3 +15,10 @@ int vfio_device_create_dmabuf_fd(struct iovec *iov, unsi= gned int iov_cnt, error_setg(errp, "VFIO dmabuf support is not enabled"); return VFIO_DMABUF_CREATE_HOST_ERROR; } + +void *vfio_device_mmap_dmabuf(struct iovec *iov, unsigned int iov_cnt, + size_t total_size, Error **errp) +{ + error_setg(errp, "VFIO mmap dmabuf support is not enabled"); + return NULL; +} --=20 2.53.0