From nobody Sun Feb 8 22:22:21 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; 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=quarantine dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1769419415; cv=none; d=zohomail.com; s=zohoarc; b=arp0s6zpgMSDuaElDICjoMJMFpfzvZC86g/4D1QrhgeVbadGc7IL+VvT3soEOu2b9zRIbXBK2+ZuDOTR8HA6J7Mc4S27O5c5mGKLJgvQBezecIB6FnzA5p/XWpu5PD/rumUIuxaFSubBZ6pUFq1Ay6k6R8IuEI2DZO/SQG4/6N4= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1769419415; 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=GPa5h3aD/5wLkODm+sqMFrJ0UMHGiuqZciNwX7qEZyg=; b=eJDicimQNuYcjqnhl86hN4P3D01NCf+wsi7ERG7gIVbybepCMEkGO/y+0SjU5wh2138XwIKQ/GXn7GMXFd4jjHFuGhRRCq9DFtpBm4LpM0tBXhS6L0O4qobCOAWQ5LOj+ZISsEezIflV3h7/3aQht/3T26tb2M3Cpgt7Nqzz21o= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; 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=quarantine dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1769419415940333.87724574235665; Mon, 26 Jan 2026 01:23:35 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1vkIo2-00020t-Pe; Mon, 26 Jan 2026 04:22:46 -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 1vkIns-0001v6-CH for qemu-devel@nongnu.org; Mon, 26 Jan 2026 04:22:36 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1vkInq-0005zi-2j for qemu-devel@nongnu.org; Mon, 26 Jan 2026 04:22:36 -0500 Received: from mx-prod-mc-01.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-137-pDHmG497MsuOy6qbr-n5Qg-1; Mon, 26 Jan 2026 04:22:31 -0500 Received: from mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.17]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id EA6201944E45; Mon, 26 Jan 2026 09:22:29 +0000 (UTC) Received: from corto.redhat.com (unknown [10.44.32.175]) by mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 7A3E01956095; Mon, 26 Jan 2026 09:22:27 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1769419353; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=GPa5h3aD/5wLkODm+sqMFrJ0UMHGiuqZciNwX7qEZyg=; b=JwH8cunjg3KkCOnWxA/Sy3yvBOE7YaD77OD9OlTH2XtmOIRocb7gjimp+zLXygdJTafrSA tDCOSkrUL6bOux0AQ3WIwF/L3k4SlmuVoFHCJ7XTVMB+C9auMKcaBWhLny14CXFkKgn056 MpfV4BWTyVnkuOMXeVE8jovwMIAZhtk= X-MC-Unique: pDHmG497MsuOy6qbr-n5Qg-1 X-Mimecast-MFC-AGG-ID: pDHmG497MsuOy6qbr-n5Qg_1769419350 From: =?UTF-8?q?C=C3=A9dric=20Le=20Goater?= To: qemu-devel@nongnu.org Cc: Alex Williamson , Nicolin Chen , Zhenzhong Duan , =?UTF-8?q?C=C3=A9dric=20Le=20Goater?= , Shameer Kolothum , Eric Auger , "Michael S. Tsirkin" Subject: [PULL 5/7] hw/vfio/region: Create dmabuf for PCI BAR per region Date: Mon, 26 Jan 2026 10:22:10 +0100 Message-ID: <20260126092212.503162-6-clg@redhat.com> In-Reply-To: <20260126092212.503162-1-clg@redhat.com> References: <20260126092212.503162-1-clg@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.0 on 10.30.177.17 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=170.10.129.124; envelope-from=clg@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 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_NONE=-0.0001, RCVD_IN_MSPIKE_H2=-0.01, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, RCVD_IN_VALIDITY_SAFE_BLOCKED=0.001, SPF_HELO_PASS=-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 @redhat.com) X-ZM-MESSAGEID: 1769419417587158500 From: Nicolin Chen Linux now provides a VFIO dmabuf exporter to expose PCI BAR memory for P2P use cases. Create a dmabuf for each mapped BAR region after the mmap is set up, and store the returned fd in the region=E2=80=99s RAMBlock. This allows= QEMU to pass the fd to dma_map_file(), enabling iommufd to import the dmabuf and map the BAR correctly in the host IOMMU page table. If the kernel lacks support or dmabuf setup fails, QEMU skips the setup and continues with normal mmap handling. Tested-by: Nicolin Chen Reviewed-by: Zhenzhong Duan Reviewed-by: C=C3=A9dric Le Goater Signed-off-by: Nicolin Chen Signed-off-by: Shameer Kolothum Reviewed-by: Eric Auger Tested-by: Eric Auger Reviewed-by: Michael S. Tsirkin Link: https://lore.kernel.org/qemu-devel/20260121114111.34045-4-skolothumth= o@nvidia.com Signed-off-by: C=C3=A9dric Le Goater --- hw/vfio/region.c | 65 +++++++++++++++++++++++++++++++++++++++++++- hw/vfio/trace-events | 1 + 2 files changed, 65 insertions(+), 1 deletion(-) diff --git a/hw/vfio/region.c b/hw/vfio/region.c index ca75ab1be4d601f5371c929a7784c8ba264df9ae..ab39d77574ccef0ca8d39343597= 9db97bcbd882b 100644 --- a/hw/vfio/region.c +++ b/hw/vfio/region.c @@ -29,6 +29,7 @@ #include "qemu/error-report.h" #include "qemu/units.h" #include "monitor/monitor.h" +#include "system/ramblock.h" #include "vfio-helpers.h" =20 /* @@ -238,13 +239,71 @@ static void vfio_subregion_unmap(VFIORegion *region, = int index) region->mmaps[index].mmap =3D NULL; } =20 +static bool vfio_region_create_dma_buf(VFIORegion *region, Error **errp) +{ + g_autofree struct vfio_device_feature *feature =3D NULL; + VFIODevice *vbasedev =3D region->vbasedev; + struct vfio_device_feature_dma_buf *dma_buf; + size_t total_size; + int i, ret; + + total_size =3D sizeof(*feature) + sizeof(*dma_buf) + + sizeof(struct vfio_region_dma_range) * region->nr_mmaps; + feature =3D g_malloc0(total_size); + *feature =3D (struct vfio_device_feature) { + .argsz =3D total_size, + .flags =3D VFIO_DEVICE_FEATURE_GET | VFIO_DEVICE_FEATURE_DMA_BUF, + }; + + dma_buf =3D (void *)feature->data; + *dma_buf =3D (struct vfio_device_feature_dma_buf) { + .region_index =3D region->nr, + .open_flags =3D O_RDWR, + .nr_ranges =3D region->nr_mmaps, + }; + + for (i =3D 0; i < region->nr_mmaps; i++) { + dma_buf->dma_ranges[i].offset =3D region->mmaps[i].offset; + dma_buf->dma_ranges[i].length =3D region->mmaps[i].size; + } + + ret =3D vfio_device_get_feature(vbasedev, feature); + if (ret < 0) { + if (ret =3D=3D -ENOTTY) { + warn_report_once("VFIO dma-buf not supported in kernel: " + "PCI BAR IOMMU mappings may fail"); + return true; + } + /* P2P DMA or exposing device memory use cases are not supported. = */ + error_setg_errno(errp, -ret, "%s: failed to create dma-buf: " + "PCI BAR IOMMU mappings may fail", + memory_region_name(region->mem)); + return false; + } + + /* Assign the dmabuf fd to associated RAMBlock */ + for (i =3D 0; i < region->nr_mmaps; i++) { + MemoryRegion *mr =3D ®ion->mmaps[i].mem; + RAMBlock *ram_block =3D mr->ram_block; + + ram_block->fd =3D ret; + ram_block->fd_offset =3D region->mmaps[i].offset; + trace_vfio_region_dmabuf(region->vbasedev->name, ret, region->nr, + memory_region_name(region->mem), + region->mmaps[i].offset, + region->mmaps[i].size); + } + return true; +} + int vfio_region_mmap(VFIORegion *region) { int i, ret, prot =3D 0; + Error *local_err =3D NULL; char *name; int fd; =20 - if (!region->mem) { + if (!region->mem || !region->nr_mmaps) { return 0; } =20 @@ -305,6 +364,10 @@ int vfio_region_mmap(VFIORegion *region) region->mmaps[i].size - 1); } =20 + if (!vfio_region_create_dma_buf(region, &local_err)) { + error_report_err(local_err); + } + return 0; =20 no_mmap: diff --git a/hw/vfio/trace-events b/hw/vfio/trace-events index 180e3d526b80bd43d18bbfb9332833df4fbeb720..466695507b77c974ba0e4e61109= c0abe2d06c71c 100644 --- a/hw/vfio/trace-events +++ b/hw/vfio/trace-events @@ -118,6 +118,7 @@ vfio_device_put(int fd) "close vdev->fd=3D%d" vfio_region_write(const char *name, int index, uint64_t addr, uint64_t dat= a, unsigned size) " (%s:region%d+0x%"PRIx64", 0x%"PRIx64 ", %d)" vfio_region_read(char *name, int index, uint64_t addr, unsigned size, uint= 64_t data) " (%s:region%d+0x%"PRIx64", %d) =3D 0x%"PRIx64 vfio_region_setup(const char *dev, int index, const char *name, unsigned l= ong flags, unsigned long offset, unsigned long size) "Device %s, region %d = \"%s\", flags: 0x%lx, offset: 0x%lx, size: 0x%lx" +vfio_region_dmabuf(const char *dev, int fd, int index, const char *name, = unsigned long offset, unsigned long size) "Device %s, dmabuf fd %d region %= d \"%s\", offset: 0x%lx, size: 0x%lx" vfio_region_mmap_fault(const char *name, int index, unsigned long offset, = unsigned long size, int fault) "Region %s mmaps[%d], [0x%lx - 0x%lx], fault= : %d" vfio_region_mmap(const char *name, unsigned long offset, unsigned long end= ) "Region %s [0x%lx - 0x%lx]" vfio_region_exit(const char *name, int index) "Device %s, region %d" --=20 2.52.0