qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: John Levon <john.levon@nutanix.com>
To: qemu-devel@nongnu.org
Cc: "Jason Herne" <jjherne@linux.ibm.com>,
	"Thanos Makatos" <thanos.makatos@nutanix.com>,
	"Halil Pasic" <pasic@linux.ibm.com>,
	"Daniel P. Berrangé" <berrange@redhat.com>,
	"Eric Farman" <farman@linux.ibm.com>,
	"Tony Krowiak" <akrowiak@linux.ibm.com>,
	"Thomas Huth" <thuth@redhat.com>,
	qemu-s390x@nongnu.org, "Matthew Rosato" <mjrosato@linux.ibm.com>,
	"John Levon" <john.levon@nutanix.com>,
	"Paolo Bonzini" <pbonzini@redhat.com>,
	"Marc-André Lureau" <marcandre.lureau@redhat.com>,
	"Stefano Garzarella" <sgarzare@redhat.com>,
	"Alex Williamson" <alex.williamson@redhat.com>,
	"David Hildenbrand" <david@redhat.com>,
	"Cédric Le Goater" <clg@redhat.com>,
	"Peter Xu" <peterx@redhat.com>,
	"Philippe Mathieu-Daudé" <philmd@linaro.org>,
	"Michael S. Tsirkin" <mst@redhat.com>,
	"Jagannathan Raman" <jag.raman@oracle.com>,
	"John Johnson" <john.g.johnson@oracle.com>,
	"Elena Ufimtseva" <elena.ufimtseva@oracle.com>
Subject: [PATCH v8 01/28] vfio/container: pass MemoryRegion to DMA operations
Date: Wed, 19 Feb 2025 15:48:31 +0100	[thread overview]
Message-ID: <20250219144858.266455-2-john.levon@nutanix.com> (raw)
In-Reply-To: <20250219144858.266455-1-john.levon@nutanix.com>

From: Jagannathan Raman <jag.raman@oracle.com>

Pass through the MemoryRegion to DMA operation handlers of vfio
containers. The vfio-user container will need this later.

Originally-by: John Johnson <john.g.johnson@oracle.com>
Signed-off-by: Jagannathan Raman <jag.raman@oracle.com>
Signed-off-by: Elena Ufimtseva <elena.ufimtseva@oracle.com>
Signed-off-by: John Levon <john.levon@nutanix.com>
---
 hw/vfio/common.c                      | 17 ++++++++++-------
 hw/vfio/container-base.c              |  4 ++--
 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, 28 insertions(+), 16 deletions(-)

diff --git a/hw/vfio/common.c b/hw/vfio/common.c
index abbdc56b6d..8d3d425c63 100644
--- a/hw/vfio/common.c
+++ b/hw/vfio/common.c
@@ -248,12 +248,12 @@ static bool vfio_listener_skipped_section(MemoryRegionSection *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;
 
     ret = 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
@@ -281,6 +281,7 @@ static void vfio_iommu_map_notify(IOMMUNotifier *n, IOMMUTLBEntry *iotlb)
     VFIOGuestIOMMU *giommu = container_of(n, VFIOGuestIOMMU, n);
     VFIOContainerBase *bcontainer = giommu->bcontainer;
     hwaddr iova = iotlb->iova + giommu->iommu_offset;
+    MemoryRegion *mrp;
     void *vaddr;
     int ret;
     Error *local_err = NULL;
@@ -300,7 +301,8 @@ static void vfio_iommu_map_notify(IOMMUNotifier *n, IOMMUTLBEntry *iotlb)
     if ((iotlb->perm & IOMMU_RW) != IOMMU_NONE) {
         bool read_only;
 
-        if (!vfio_get_xlat_addr(iotlb, &vaddr, NULL, &read_only, &local_err)) {
+        if (!vfio_get_xlat_addr(iotlb, &vaddr, NULL, &read_only, &mrp,
+                                &local_err)) {
             error_report_err(local_err);
             goto out;
         }
@@ -313,7 +315,7 @@ static void vfio_iommu_map_notify(IOMMUNotifier *n, IOMMUTLBEntry *iotlb)
          */
         ret = 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) = %d (%s)",
@@ -378,7 +380,7 @@ static int vfio_ram_discard_notify_populate(RamDiscardListener *rdl,
         vaddr = memory_region_get_ram_ptr(section->mr) + start;
 
         ret = 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);
@@ -675,7 +677,7 @@ static void vfio_listener_region_add(MemoryListener *listener,
     }
 
     ret = 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) = %d (%s)",
@@ -1232,7 +1234,8 @@ static void vfio_iommu_map_dirty_notify(IOMMUNotifier *n, IOMMUTLBEntry *iotlb)
     }
 
     rcu_read_lock();
-    if (!vfio_get_xlat_addr(iotlb, NULL, &translated_addr, NULL, &local_err)) {
+    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 749a3fd29d..5e0c9700d9 100644
--- a/hw/vfio/container-base.c
+++ b/hw/vfio/container-base.c
@@ -17,12 +17,12 @@
 
 int vfio_container_dma_map(VFIOContainerBase *bcontainer,
                            hwaddr iova, ram_addr_t size,
-                           void *vaddr, bool readonly)
+                           void *vaddr, bool readonly, MemoryRegion *mrp)
 {
     VFIOIOMMUClass *vioc = VFIO_IOMMU_GET_CLASS(bcontainer);
 
     g_assert(vioc->dma_map);
-    return vioc->dma_map(bcontainer, iova, size, vaddr, readonly);
+    return vioc->dma_map(bcontainer, iova, size, vaddr, readonly, mrp);
 }
 
 int vfio_container_dma_unmap(VFIOContainerBase *bcontainer,
diff --git a/hw/vfio/container.c b/hw/vfio/container.c
index 7c57bdd27b..0db0055f39 100644
--- a/hw/vfio/container.c
+++ b/hw/vfio/container.c
@@ -175,7 +175,8 @@ static int vfio_legacy_dma_unmap(const VFIOContainerBase *bcontainer,
 }
 
 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 = container_of(bcontainer, VFIOContainer,
                                                   bcontainer);
diff --git a/hw/vfio/iommufd.c b/hw/vfio/iommufd.c
index df61edffc0..583b063707 100644
--- a/hw/vfio/iommufd.c
+++ b/hw/vfio/iommufd.c
@@ -28,7 +28,8 @@
 #include "exec/ram_addr.h"
 
 static int iommufd_cdev_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 VFIOIOMMUFDContainer *container =
         container_of(bcontainer, VFIOIOMMUFDContainer, bcontainer);
diff --git a/hw/virtio/vhost-vdpa.c b/hw/virtio/vhost-vdpa.c
index 3cdaa12ed5..a1866bb396 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) != IOMMU_NONE) {
         bool read_only;
 
-        if (!memory_get_xlat_addr(iotlb, &vaddr, NULL, &read_only, NULL,
+        if (!memory_get_xlat_addr(iotlb, &vaddr, NULL, &read_only, NULL, NULL,
                                   &local_err)) {
             error_report_err(local_err);
             return;
diff --git a/include/exec/memory.h b/include/exec/memory.h
index 9f73b59867..adaf1d1ca4 100644
--- a/include/exec/memory.h
+++ b/include/exec/memory.h
@@ -747,13 +747,15 @@ void ram_discard_manager_unregister_listener(RamDiscardManager *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);
 
 typedef struct CoalescedMemoryRange CoalescedMemoryRange;
 typedef struct MemoryRegionIoeventfd MemoryRegionIoeventfd;
diff --git a/include/hw/vfio/vfio-container-base.h b/include/hw/vfio/vfio-container-base.h
index 4cff9943ab..c9d339383e 100644
--- a/include/hw/vfio/vfio-container-base.h
+++ b/include/hw/vfio/vfio-container-base.h
@@ -73,7 +73,7 @@ typedef struct VFIORamDiscardListener {
 
 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 4c829793a0..de4f955a66 100644
--- a/system/memory.c
+++ b/system/memory.c
@@ -2185,7 +2185,8 @@ void ram_discard_manager_unregister_listener(RamDiscardManager *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;
@@ -2250,6 +2251,10 @@ bool memory_get_xlat_addr(IOMMUTLBEntry *iotlb, void **vaddr,
         *read_only = !writable || mr->readonly;
     }
 
+    if (mrp != NULL) {
+        *mrp = mr;
+    }
+
     return true;
 }
 
-- 
2.34.1



  reply	other threads:[~2025-02-19 14:50 UTC|newest]

Thread overview: 64+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-02-19 14:48 [PATCH v8 00/28] vfio-user client John Levon
2025-02-19 14:48 ` John Levon [this message]
2025-04-02 16:44   ` [PATCH v8 01/28] vfio/container: pass MemoryRegion to DMA operations Cédric Le Goater
2025-02-19 14:48 ` [PATCH v8 02/28] vfio/container: pass listener_begin/commit callbacks John Levon
2025-04-02 12:30   ` Cédric Le Goater
2025-02-19 14:48 ` [PATCH v8 03/28] vfio/container: support VFIO_DMA_UNMAP_FLAG_ALL John Levon
2025-04-02 16:49   ` Cédric Le Goater
2025-04-03  9:45     ` John Levon
2025-04-04 15:43       ` Cédric Le Goater
2025-02-19 14:48 ` [PATCH v8 04/28] vfio: add vfio_attach_device_by_iommu_type() John Levon
2025-04-02 16:52   ` Cédric Le Goater
2025-02-19 14:48 ` [PATCH v8 05/28] vfio: add vfio_prepare_device() John Levon
2025-04-03  9:19   ` Cédric Le Goater
2025-04-03  9:34     ` John Levon
2025-04-04 15:41       ` Cédric Le Goater
2025-04-04 15:45         ` John Levon
2025-02-19 14:48 ` [PATCH v8 06/28] vfio: refactor out vfio_interrupt_setup() John Levon
2025-04-03  9:23   ` Cédric Le Goater
2025-04-03  9:38     ` John Levon
2025-02-19 14:48 ` [PATCH v8 07/28] vfio: refactor out vfio_pci_config_setup() John Levon
2025-04-03  9:30   ` Cédric Le Goater
2025-02-19 14:48 ` [PATCH v8 08/28] vfio: add region cache John Levon
2025-04-03 15:46   ` Cédric Le Goater
2025-04-03 16:00     ` John Levon
2025-04-04 16:57       ` Cédric Le Goater
2025-04-04 17:18         ` John Levon
2025-04-08 13:48           ` John Levon
2025-02-19 14:48 ` [PATCH v8 09/28] vfio: split out VFIOKernelPCIDevice John Levon
2025-04-03 17:13   ` Cédric Le Goater
2025-04-03 18:08     ` John Levon
2025-04-04 12:49       ` Cédric Le Goater
2025-04-04 14:21         ` John Levon
2025-04-04 14:48           ` Cédric Le Goater
2025-04-04 15:44             ` John Levon
2025-02-19 14:48 ` [PATCH v8 10/28] vfio: add device IO ops vector John Levon
2025-04-04 14:36   ` Cédric Le Goater
2025-04-04 15:53     ` John Levon
2025-02-19 14:48 ` [PATCH v8 11/28] vfio-user: introduce vfio-user protocol specification John Levon
2025-02-19 14:48 ` [PATCH v8 12/28] vfio-user: add vfio-user class and container John Levon
2025-02-19 14:48 ` [PATCH v8 13/28] vfio-user: connect vfio proxy to remote server John Levon
2025-02-19 14:48 ` [PATCH v8 14/28] vfio-user: implement message receive infrastructure John Levon
2025-02-19 14:48 ` [PATCH v8 15/28] vfio-user: implement message send infrastructure John Levon
2025-02-19 14:48 ` [PATCH v8 16/28] vfio-user: implement VFIO_USER_DEVICE_GET_INFO John Levon
2025-02-19 14:48 ` [PATCH v8 17/28] vfio-user: implement VFIO_USER_DEVICE_GET_REGION_INFO John Levon
2025-02-19 14:48 ` [PATCH v8 18/28] vfio-user: implement VFIO_USER_REGION_READ/WRITE John Levon
2025-02-19 14:48 ` [PATCH v8 19/28] vfio-user: set up PCI in vfio_user_pci_realize() John Levon
2025-02-19 14:48 ` [PATCH v8 20/28] vfio-user: implement VFIO_USER_DEVICE_GET/SET_IRQ* John Levon
2025-02-19 14:48 ` [PATCH v8 21/28] vfio-user: forward MSI-X PBA BAR accesses to server John Levon
2025-02-19 14:48 ` [PATCH v8 22/28] vfio-user: set up container access to the proxy John Levon
2025-02-19 14:48 ` [PATCH v8 23/28] vfio-user: implement VFIO_USER_DEVICE_RESET John Levon
2025-02-19 14:48 ` [PATCH v8 24/28] vfio-user: implement VFIO_USER_DMA_MAP/UNMAP John Levon
2025-02-19 14:48 ` [PATCH v8 25/28] vfio-user: implement VFIO_USER_DMA_READ/WRITE John Levon
2025-02-19 14:48 ` [PATCH v8 26/28] vfio-user: add 'no-direct-dma' option John Levon
2025-02-19 14:48 ` [PATCH v8 27/28] vfio-user: add 'x-msg-timeout' option John Levon
2025-02-19 14:48 ` [PATCH v8 28/28] vfio-user: add coalesced posted writes John Levon
2025-02-28 17:09 ` [PATCH v8 00/28] vfio-user client Jag Raman
2025-03-03 11:19   ` John Levon
2025-03-03 15:39     ` Jag Raman
2025-03-14 14:25 ` Cédric Le Goater
2025-03-14 14:48   ` Steven Sistare
2025-03-18 10:00     ` Cédric Le Goater
2025-03-14 15:13   ` John Levon
2025-03-18 10:02     ` Cédric Le Goater
2025-04-04 17:21 ` Cédric Le Goater

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20250219144858.266455-2-john.levon@nutanix.com \
    --to=john.levon@nutanix.com \
    --cc=akrowiak@linux.ibm.com \
    --cc=alex.williamson@redhat.com \
    --cc=berrange@redhat.com \
    --cc=clg@redhat.com \
    --cc=david@redhat.com \
    --cc=elena.ufimtseva@oracle.com \
    --cc=farman@linux.ibm.com \
    --cc=jag.raman@oracle.com \
    --cc=jjherne@linux.ibm.com \
    --cc=john.g.johnson@oracle.com \
    --cc=marcandre.lureau@redhat.com \
    --cc=mjrosato@linux.ibm.com \
    --cc=mst@redhat.com \
    --cc=pasic@linux.ibm.com \
    --cc=pbonzini@redhat.com \
    --cc=peterx@redhat.com \
    --cc=philmd@linaro.org \
    --cc=qemu-devel@nongnu.org \
    --cc=qemu-s390x@nongnu.org \
    --cc=sgarzare@redhat.com \
    --cc=thanos.makatos@nutanix.com \
    --cc=thuth@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).