From nobody Fri Oct 18 06:15:15 2024 Delivered-To: importer2@patchew.org Authentication-Results: mx.zohomail.com; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer2=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=movementarian.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1716999994197985.408354925171; Wed, 29 May 2024 09:26:34 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1sCM6u-00018C-G2; Wed, 29 May 2024 12:25:08 -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 1sCM6o-00015b-TJ for qemu-devel@nongnu.org; Wed, 29 May 2024 12:25:05 -0400 Received: from ssh.movementarian.org ([139.162.205.133] helo=movementarian.org) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1sCM6l-0006KV-6k for qemu-devel@nongnu.org; Wed, 29 May 2024 12:25:02 -0400 Received: from movement by movementarian.org with local (Exim 4.95) (envelope-from ) id 1sCM6i-006COY-QZ; Wed, 29 May 2024 17:24:56 +0100 From: John Levon To: qemu-devel@nongnu.org Cc: alex.williamson@redhat.com, clg@redhat.com, jag.raman@oracle.com, thanos.makatos@nutanix.com, John Johnson , Elena Ufimtseva , John Levon Subject: [PATCH 01/26] vfio/container: pass MemoryRegion to DMA operations Date: Wed, 29 May 2024 17:22:54 +0100 Message-Id: <20240529162319.1476680-2-levon@movementarian.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240529162319.1476680-1-levon@movementarian.org> References: <20240529162319.1476680-1-levon@movementarian.org> MIME-Version: 1.0 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+importer2=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=139.162.205.133; envelope-from=movement@movementarian.org; helo=movementarian.org X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 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: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer2=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer2=patchew.org@nongnu.org X-ZM-MESSAGEID: 1716999996160100003 Content-Type: text/plain; charset="utf-8" From: Jagannathan Raman Pass through the MemoryRegion to DMA operation handlers of vfio containers. The vfio-user container will need this later. Originally-by: John Johnson Signed-off-by: Jagannathan Raman Signed-off-by: Elena Ufimtseva Signed-off-by: John Levon --- hw/vfio/common.c | 17 ++++++++++------- hw/vfio/container-base.c | 5 +++-- hw/vfio/container.c | 3 ++- hw/vfio/iommufd.c | 3 ++- hw/virtio/vhost-vdpa.c | 2 +- include/exec/memory.h | 4 +++- include/hw/vfio/vfio-container-base.h | 4 ++-- system/memory.c | 7 ++++++- 8 files changed, 29 insertions(+), 16 deletions(-) diff --git a/hw/vfio/common.c b/hw/vfio/common.c index f9619a1dfb..8eb2ed50dd 100644 --- a/hw/vfio/common.c +++ b/hw/vfio/common.c @@ -254,12 +254,12 @@ static bool vfio_listener_skipped_section(MemoryRegio= nSection *section) /* Called with rcu_read_lock held. */ static bool vfio_get_xlat_addr(IOMMUTLBEntry *iotlb, void **vaddr, ram_addr_t *ram_addr, bool *read_only, - Error **errp) + MemoryRegion **mrp, Error **errp) { bool ret, mr_has_discard_manager; =20 ret =3D memory_get_xlat_addr(iotlb, vaddr, ram_addr, read_only, - &mr_has_discard_manager, errp); + &mr_has_discard_manager, mrp, errp); if (ret && mr_has_discard_manager) { /* * Malicious VMs might trigger discarding of IOMMU-mapped memory. = The @@ -287,6 +287,7 @@ static void vfio_iommu_map_notify(IOMMUNotifier *n, IOM= MUTLBEntry *iotlb) VFIOGuestIOMMU *giommu =3D container_of(n, VFIOGuestIOMMU, n); VFIOContainerBase *bcontainer =3D giommu->bcontainer; hwaddr iova =3D iotlb->iova + giommu->iommu_offset; + MemoryRegion *mrp; void *vaddr; int ret; Error *local_err =3D NULL; @@ -306,7 +307,8 @@ static void vfio_iommu_map_notify(IOMMUNotifier *n, IOM= MUTLBEntry *iotlb) if ((iotlb->perm & IOMMU_RW) !=3D IOMMU_NONE) { bool read_only; =20 - if (!vfio_get_xlat_addr(iotlb, &vaddr, NULL, &read_only, &local_er= r)) { + if (!vfio_get_xlat_addr(iotlb, &vaddr, NULL, &read_only, &mrp, + &local_err)) { error_report_err(local_err); goto out; } @@ -319,7 +321,7 @@ static void vfio_iommu_map_notify(IOMMUNotifier *n, IOM= MUTLBEntry *iotlb) */ ret =3D vfio_container_dma_map(bcontainer, iova, iotlb->addr_mask + 1, vaddr, - read_only); + read_only, mrp); if (ret) { error_report("vfio_container_dma_map(%p, 0x%"HWADDR_PRIx", " "0x%"HWADDR_PRIx", %p) =3D %d (%s)", @@ -384,7 +386,7 @@ static int vfio_ram_discard_notify_populate(RamDiscardL= istener *rdl, vaddr =3D memory_region_get_ram_ptr(section->mr) + start; =20 ret =3D vfio_container_dma_map(bcontainer, iova, next - start, - vaddr, section->readonly); + vaddr, section->readonly, section->mr= ); if (ret) { /* Rollback */ vfio_ram_discard_notify_discard(rdl, section); @@ -686,7 +688,7 @@ static void vfio_listener_region_add(MemoryListener *li= stener, } =20 ret =3D vfio_container_dma_map(bcontainer, iova, int128_get64(llsize), - vaddr, section->readonly); + vaddr, section->readonly, section->mr); if (ret) { error_setg(&err, "vfio_container_dma_map(%p, 0x%"HWADDR_PRIx", " "0x%"HWADDR_PRIx", %p) =3D %d (%s)", @@ -1231,7 +1233,8 @@ static void vfio_iommu_map_dirty_notify(IOMMUNotifier= *n, IOMMUTLBEntry *iotlb) } =20 rcu_read_lock(); - if (!vfio_get_xlat_addr(iotlb, NULL, &translated_addr, NULL, &local_er= r)) { + if (!vfio_get_xlat_addr(iotlb, NULL, &translated_addr, NULL, NULL, + &local_err)) { error_report_err(local_err); goto out_unlock; } diff --git a/hw/vfio/container-base.c b/hw/vfio/container-base.c index 760d9d0622..f16766f490 100644 --- a/hw/vfio/container-base.c +++ b/hw/vfio/container-base.c @@ -17,10 +17,11 @@ =20 int vfio_container_dma_map(VFIOContainerBase *bcontainer, hwaddr iova, ram_addr_t size, - void *vaddr, bool readonly) + void *vaddr, bool readonly, MemoryRegion *mrp) { g_assert(bcontainer->ops->dma_map); - return bcontainer->ops->dma_map(bcontainer, iova, size, vaddr, readonl= y); + return bcontainer->ops->dma_map(bcontainer, iova, size, vaddr, + readonly, mrp); } =20 int vfio_container_dma_unmap(VFIOContainerBase *bcontainer, diff --git a/hw/vfio/container.c b/hw/vfio/container.c index 096cc97258..6e6308382f 100644 --- a/hw/vfio/container.c +++ b/hw/vfio/container.c @@ -177,7 +177,8 @@ static int vfio_legacy_dma_unmap(const VFIOContainerBas= e *bcontainer, } =20 static int vfio_legacy_dma_map(const VFIOContainerBase *bcontainer, hwaddr= iova, - ram_addr_t size, void *vaddr, bool readonly) + ram_addr_t size, void *vaddr, bool readonly, + MemoryRegion *mrp) { const VFIOContainer *container =3D container_of(bcontainer, VFIOContai= ner, bcontainer); diff --git a/hw/vfio/iommufd.c b/hw/vfio/iommufd.c index 554f9a6292..3d08a4f2dc 100644 --- a/hw/vfio/iommufd.c +++ b/hw/vfio/iommufd.c @@ -27,7 +27,8 @@ #include "pci.h" =20 static int iommufd_cdev_map(const VFIOContainerBase *bcontainer, hwaddr io= va, - ram_addr_t size, void *vaddr, bool readonly) + ram_addr_t size, void *vaddr, bool readonly, + MemoryRegion *mrp) { const VFIOIOMMUFDContainer *container =3D container_of(bcontainer, VFIOIOMMUFDContainer, bcontainer); diff --git a/hw/virtio/vhost-vdpa.c b/hw/virtio/vhost-vdpa.c index ed99ab8745..9b5fb384dd 100644 --- a/hw/virtio/vhost-vdpa.c +++ b/hw/virtio/vhost-vdpa.c @@ -228,7 +228,7 @@ static void vhost_vdpa_iommu_map_notify(IOMMUNotifier *= n, IOMMUTLBEntry *iotlb) if ((iotlb->perm & IOMMU_RW) !=3D IOMMU_NONE) { bool read_only; =20 - if (!memory_get_xlat_addr(iotlb, &vaddr, NULL, &read_only, NULL, + if (!memory_get_xlat_addr(iotlb, &vaddr, NULL, &read_only, NULL, N= ULL, &local_err)) { error_report_err(local_err); return; diff --git a/include/exec/memory.h b/include/exec/memory.h index 9cdd64e9c6..81c6d1488a 100644 --- a/include/exec/memory.h +++ b/include/exec/memory.h @@ -783,13 +783,15 @@ void ram_discard_manager_unregister_listener(RamDisca= rdManager *rdm, * @read_only: indicates if writes are allowed * @mr_has_discard_manager: indicates memory is controlled by a * RamDiscardManager + * @mrp: if non-NULL, fill in with MemoryRegion * @errp: pointer to Error*, to store an error if it happens. * * Return: true on success, else false setting @errp with error. */ bool memory_get_xlat_addr(IOMMUTLBEntry *iotlb, void **vaddr, ram_addr_t *ram_addr, bool *read_only, - bool *mr_has_discard_manager, Error **errp); + bool *mr_has_discard_manager, MemoryRegion **mrp, + Error **errp); =20 typedef struct CoalescedMemoryRange CoalescedMemoryRange; typedef struct MemoryRegionIoeventfd MemoryRegionIoeventfd; diff --git a/include/hw/vfio/vfio-container-base.h b/include/hw/vfio/vfio-c= ontainer-base.h index 2776481fc9..129e742643 100644 --- a/include/hw/vfio/vfio-container-base.h +++ b/include/hw/vfio/vfio-container-base.h @@ -72,7 +72,7 @@ typedef struct VFIORamDiscardListener { =20 int vfio_container_dma_map(VFIOContainerBase *bcontainer, hwaddr iova, ram_addr_t size, - void *vaddr, bool readonly); + void *vaddr, bool readonly, MemoryRegion *mrp); int vfio_container_dma_unmap(VFIOContainerBase *bcontainer, hwaddr iova, ram_addr_t size, IOMMUTLBEntry *iotlb); @@ -113,7 +113,7 @@ struct VFIOIOMMUClass { bool (*setup)(VFIOContainerBase *bcontainer, Error **errp); int (*dma_map)(const VFIOContainerBase *bcontainer, hwaddr iova, ram_addr_t size, - void *vaddr, bool readonly); + void *vaddr, bool readonly, MemoryRegion *mrp); int (*dma_unmap)(const VFIOContainerBase *bcontainer, hwaddr iova, ram_addr_t size, IOMMUTLBEntry *iotlb); diff --git a/system/memory.c b/system/memory.c index 9540caa8a1..d6574a88c1 100644 --- a/system/memory.c +++ b/system/memory.c @@ -2179,7 +2179,8 @@ void ram_discard_manager_unregister_listener(RamDisca= rdManager *rdm, /* Called with rcu_read_lock held. */ bool memory_get_xlat_addr(IOMMUTLBEntry *iotlb, void **vaddr, ram_addr_t *ram_addr, bool *read_only, - bool *mr_has_discard_manager, Error **errp) + bool *mr_has_discard_manager, MemoryRegion **mrp, + Error **errp) { MemoryRegion *mr; hwaddr xlat; @@ -2244,6 +2245,10 @@ bool memory_get_xlat_addr(IOMMUTLBEntry *iotlb, void= **vaddr, *read_only =3D !writable || mr->readonly; } =20 + if (mrp !=3D NULL) { + *mrp =3D mr; + } + return true; } =20 --=20 2.34.1