From nobody Sat Apr 11 23:04:16 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=1772694091; cv=none; d=zohomail.com; s=zohoarc; b=E46E2kMfPnX7o844NcUCBpNF23XEm73oAiB4IBbLQpwQp+HeZSNBXJuQAhhPLr+yta6acloymumRCMB6X4wciF5CE8jTi8rZJ6nOv7cHPSWkDlWlO5leCAEp+quHHscwvZmKgwPRtw2tKTyqUICe5ivCbvPZ6yldUkkyXh30CF4= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1772694091; 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=25A+y7ap2R6Nq4CWTqZp4faqqZSKyYra0hzvjJ8fQo8=; b=Hfi8rKbTmIBua3CQ6nqnIua7XHvvcO/fRUn2vBDwWIYw7H2cuXoOU9EAcbD1as3X2TCzUWF3BOYzG2juzUGcDGqeEzIbfMaL89RFgDdLxmhtTKm5G8nTs7QcC3CbLA4f2/nWTciD6sgRK9i4oxttCz6ld9fgdGFXCqNBjKk+BeY= 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 1772694091692847.269453238504; Wed, 4 Mar 2026 23:01:31 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1vy2gR-0003TX-0d; Thu, 05 Mar 2026 01:59:43 -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 1vy2gO-0003Ss-T6 for qemu-devel@nongnu.org; Thu, 05 Mar 2026 01:59:40 -0500 Received: from mgamail.intel.com ([192.198.163.8]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1vy2gN-0007qJ-2a for qemu-devel@nongnu.org; Thu, 05 Mar 2026 01:59:40 -0500 Received: from orviesa007.jf.intel.com ([10.64.159.147]) by fmvoesa102.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 04 Mar 2026 22:59:30 -0800 Received: from vkasired-desk2.fm.intel.com ([10.105.128.132]) by orviesa007-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 04 Mar 2026 22:59:30 -0800 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1772693979; x=1804229979; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=Hhz5My/Nci4MkLLcv1gOxQ5MgaITXA44LkA+ZiKWCXU=; b=VBoeo7GJavGfAyNG3dyv+BPnXVWmgXk1y/elTHkzR0Hq+fkofC2rJ4HK 7uMrB5Vd5ejcTeQYWoPuuU206poEr5z1t9W7v/uF+eiUROPL4Xpq4CgBN Uqmkin/Wy8zt9g6DT432aAuKipOrpRaMIt5xDh3qidGibE0g7iFQtHxqC T/gd+YF8TaWWLTicUciZYASRczab5KUvQ879d046FLEZ0g3vliXdhAiO7 Ltwq9FZUSKYKm+vUx6XWyi/MgxD5074x66WZR6pdF6onojHfRRNED8znW iVkjxx1902A1QOM8pRuQrpbr0SOfGe/TDQUJhk4pOUjSwlN5q+e+JgWzI Q==; X-CSE-ConnectionGUID: 7BQB39tISb6YstSDUAFG6A== X-CSE-MsgGUID: v7lNkjysQ1CluaGLhir8Vg== X-IronPort-AV: E=McAfee;i="6800,10657,11719"; a="91343521" X-IronPort-AV: E=Sophos;i="6.21,325,1763452800"; d="scan'208";a="91343521" X-CSE-ConnectionGUID: 7L3GAONQTVaLrUQO3YOeMg== X-CSE-MsgGUID: 2cGyclmARNScgmt565GUcg== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.21,325,1763452800"; d="scan'208";a="218725681" From: Vivek Kasireddy To: qemu-devel@nongnu.org Cc: Vivek Kasireddy , Alex Williamson , =?UTF-8?q?C=C3=A9dric=20Le=20Goater?= Subject: [PATCH v9 08/10] vfio/device: Add a helper to mmap a dmabuf Date: Wed, 4 Mar 2026 22:54:39 -0800 Message-ID: <20260305065700.1155146-9-vivek.kasireddy@intel.com> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20260305065700.1155146-1-vivek.kasireddy@intel.com> References: <20260305065700.1155146-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.8; envelope-from=vivek.kasireddy@intel.com; helo=mgamail.intel.com X-Spam_score_int: -22 X-Spam_score: -2.3 X-Spam_bar: -- X-Spam_report: (-2.3 / 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.703, RCVD_IN_VALIDITY_SAFE_BLOCKED=1.386, 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: 1772694093588154100 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 --- hw/vfio/device.c | 65 +++++++++++++++++++++++++++++++++++ hw/vfio/dmabuf-stubs.c | 7 ++++ include/hw/vfio/vfio-device.h | 17 +++++++++ 3 files changed, 89 insertions(+) diff --git a/hw/vfio/device.c b/hw/vfio/device.c index 2d01059be0..592d901c77 100644 --- a/hw/vfio/device.c +++ b/hw/vfio/device.c @@ -725,3 +725,68 @@ int vfio_device_create_dmabuf_fd(struct iovec *iov, un= signed int iov_cnt, } return ret; } + +bool vfio_device_mmap_dmabuf(struct iovec *iov, unsigned int iov_cnt, + void **addr, size_t total_size, Error **errp) +{ + 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, index; + + if (!vfio_device_lookup(iov, &vbasedev, errp)) { + return false; + } + + /* + * 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 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 mmap dmabuf with different regions\n"= ); + goto err; + } + + index =3D vfio_get_region_index_from_mr(rb->mr); + if (index < 0) { + error_setg(errp, "Cannot find region index for dmabuf segment\= n"); + goto err; + } + + if (vfio_device_get_region_info(vbasedev, index, &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(errp, "Could not mmap dmabuf segment\n"); + goto err; + } + + len +=3D iov[i].iov_len; + } + *addr =3D map; + return true; +err: + munmap(map, total_size); + error_prepend(errp, "VFIO dmabuf mmap failed: "); + return false; +} + diff --git a/hw/vfio/dmabuf-stubs.c b/hw/vfio/dmabuf-stubs.c index e71e2b68dd..c3aa37233d 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; } + +bool vfio_device_mmap_dmabuf(struct iovec *iov, unsigned int iov_cnt, + void **addr, size_t total_size, Error **errp) +{ + error_setg(errp, "VFIO mmap dmabuf support is not enabled"); + return false; +} diff --git a/include/hw/vfio/vfio-device.h b/include/hw/vfio/vfio-device.h index 566e5d8bc0..2de21a3047 100644 --- a/include/hw/vfio/vfio-device.h +++ b/include/hw/vfio/vfio-device.h @@ -340,6 +340,23 @@ int vfio_get_region_index_from_mr(MemoryRegion *mr); */ int vfio_device_create_dmabuf_fd(struct iovec *iov, unsigned int iov_cnt, 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 + * @addr: pointer to store the resulting mmap address + * @total_size: total size of the dmabuf + * @errp: pointer to Error*, to store an error if it happens. + * + * Returns true on success and false in case of an error. + */ +bool vfio_device_mmap_dmabuf(struct iovec *iov, unsigned int iov_cnt, + void **addr, size_t total_size, Error **errp); #endif =20 /* Returns 0 on success, or a negative errno. */ --=20 2.53.0