Skip to contentSkip to conversation

drop writes to read-only ram device & vfio regions

PATCH Superseded Yan Zhao 2 participants 8 messages Apr 3, 2020 – Apr 7, 2020

v2 of 6 Superseded Latest is v6 →

lore
Share & tools
lore

Yan Zhao

author
#0 lore raw

v2 cover drop writes to read-only ram device & vfio regions

patch 1 modifies handler of ram device memory regions to drop guest writes to read-only ram device memory regions

patch 2 modifies handler of non-mmap'd read-only vfio regions to drop guest writes to those regions

patch 3 let mmap'd read-only vfio regions be able to generate vmexit for guest write. so, without patch 1, host qemu would crash on guest write to this read-only region. with patch 1, host qemu would drop the writes.

Yan Zhao (3):
memory: drop guest writes to read-only ram device regions hw/vfio: drop guest writes to ro regions hw/vfio: let read-only flag take effect for mmap'd regions

 hw/vfio/common.c     | 12 +++++++++++-
 hw/vfio/trace-events |  2 +-
 memory.c             |  6 +++++-
 trace-events         |  2 +-
 4 files changed, 18 insertions(+), 4 deletions(-)
Signature · 1 line
2.17.1
author
#1 lore raw

PATCH v2 1/3 memory: drop guest writes to read-only ram device regions

for ram device regions, drop guest writes if the regions is read-only.

---
 memory.c     | 6 +++++-
 trace-events | 2 +-
 2 files changed, 6 insertions(+), 2 deletions(-)
memory.c +5 -1
index 601b749906..a380b59980 100644
@@ -1312,7 +1312,11 @@ static void memory_region_ram_device_write(void *opaque, hwaddr addr,
 {
     MemoryRegion *mr = opaque;
 
-    trace_memory_region_ram_device_write(get_cpu_index(), mr, addr, data, size);
+    trace_memory_region_ram_device_write(get_cpu_index(), mr, addr, data,
+                                         size, mr->readonly);
+    if (mr->readonly) {
+        return;
+    }
 
     switch (size) {
     case 1:
trace-events +1 -1
index 42107ebc69..e1de662973 100644
@@ -61,7 +61,7 @@ memory_region_ops_write(int cpu_index, void *mr, uint64_t addr, uint64_t value,
 memory_region_subpage_read(int cpu_index, void *mr, uint64_t offset, uint64_t value, unsigned size) "cpu %d mr %p offset 0x%"PRIx64" value 0x%"PRIx64" size %u"
 memory_region_subpage_write(int cpu_index, void *mr, uint64_t offset, uint64_t value, unsigned size) "cpu %d mr %p offset 0x%"PRIx64" value 0x%"PRIx64" size %u"
 memory_region_ram_device_read(int cpu_index, void *mr, uint64_t addr, uint64_t value, unsigned size) "cpu %d mr %p addr 0x%"PRIx64" value 0x%"PRIx64" size %u"
-memory_region_ram_device_write(int cpu_index, void *mr, uint64_t addr, uint64_t value, unsigned size) "cpu %d mr %p addr 0x%"PRIx64" value 0x%"PRIx64" size %u"
+memory_region_ram_device_write(int cpu_index, void *mr, uint64_t addr, uint64_t value, unsigned size, bool readonly) "cpu %d mr %p addr 0x%"PRIx64" value 0x%"PRIx64" size %u" " is_readonly_region=%d"
 flatview_new(void *view, void *root) "%p (root %p)"
 flatview_destroy(void *view, void *root) "%p (root %p)"
 flatview_destroy_rcu(void *view, void *root) "%p (root %p)"
Signature · 1 line
2.17.1
author
#2 lore raw

PATCH v2 2/3 hw/vfio: drop guest writes to ro regions

In reply to Yan Zhao

for vfio regions that are without write permission, drop guest writes to those regions.

---
 hw/vfio/common.c     | 8 +++++++-
 hw/vfio/trace-events | 2 +-
 2 files changed, 8 insertions(+), 2 deletions(-)
hw/vfio/common.c +7 -1
index 0b3593b3c0..fd6ee1fe3e 100644
@@ -190,6 +190,11 @@ void vfio_region_write(void *opaque, hwaddr addr,
         uint64_t qword;
     } buf;
 
+    if (!(region->flags & VFIO_REGION_INFO_FLAG_WRITE)) {
+        trace_vfio_region_write(vbasedev->name, region->nr,
+                                   addr, data, size, true);
+        return;
+    }
     switch (size) {
     case 1:
         buf.byte = data;
@@ -215,7 +220,8 @@ void vfio_region_write(void *opaque, hwaddr addr,
                      addr, data, size);
     }
 
-    trace_vfio_region_write(vbasedev->name, region->nr, addr, data, size);
+    trace_vfio_region_write(vbasedev->name, region->nr, addr, data, size,
+                            false);
 
     /*
      * A read or write to a BAR always signals an INTx EOI.  This will
hw/vfio/trace-events +1 -1
index b1ef55a33f..fb9ff604e6 100644
@@ -91,7 +91,7 @@ vfio_pci_nvlink2_setup_quirk_ssatgt(const char *name, uint64_t tgt, uint64_t siz
 vfio_pci_nvlink2_setup_quirk_lnkspd(const char *name, uint32_t link_speed) "%s link_speed=0x%x"
 
 # common.c
-vfio_region_write(const char *name, int index, uint64_t addr, uint64_t data, unsigned size) " (%s:region%d+0x%"PRIx64", 0x%"PRIx64 ", %d)"
+vfio_region_write(const char *name, int index, uint64_t addr, uint64_t data, unsigned size, bool readonly) " (%s:region%d+0x%"PRIx64", 0x%"PRIx64 ", %d)" " is_readonly_region=%d."
 vfio_region_read(char *name, int index, uint64_t addr, unsigned size, uint64_t data) " (%s:region%d+0x%"PRIx64", %d) = 0x%"PRIx64
 vfio_iommu_map_notify(const char *op, uint64_t iova_start, uint64_t iova_end) "iommu %s @ 0x%"PRIx64" - 0x%"PRIx64
 vfio_listener_region_add_skip(uint64_t start, uint64_t end) "SKIPPING region_add 0x%"PRIx64" - 0x%"PRIx64
Signature · 1 line
2.17.1
author
#3 lore raw

PATCH v2 3/3 hw/vfio: let read-only flag take effect for mmap'd regions

In reply to Yan Zhao

along side setting host page table to be read-only, the memory regions are also required to be read-only, so that when guest writes to the read-only & mmap'd regions, vmexits would happen and region write handlers are called.

---
 hw/vfio/common.c | 4 ++++
 1 file changed, 4 insertions(+)
hw/vfio/common.c +4 -0
index fd6ee1fe3e..fc7618e041 100644
@@ -977,6 +977,10 @@ int vfio_region_mmap(VFIORegion *region)
                                           name, region->mmaps[i].size,
                                           region->mmaps[i].mmap);
         g_free(name);
+
+        if (!(region->flags & VFIO_REGION_INFO_FLAG_WRITE)) {
+            memory_region_set_readonly(&region->mmaps[i].mem, true);
+        }
         memory_region_add_subregion(region->mem, region->mmaps[i].offset,
                                     &region->mmaps[i].mem);
 
Signature · 1 line
2.17.1
author
#4 lore raw

v2 cover drop writes to read-only ram device & vfio regions

In reply to Yan Zhao

patch 1 modifies handler of ram device memory regions to drop guest writes to read-only ram device memory regions

patch 2 modifies handler of non-mmap'd read-only vfio regions to drop guest writes to those regions

patch 3 let mmap'd read-only vfio regions be able to generate vmexit for guest write. so, without patch 1, host qemu would crash on guest write to this read-only region. with patch 1, host qemu would drop the writes.

Changelog:
v2:
-split one big patches into smaller ones (Philippe)
-modify existing trace to record guest writes to read-only memory (Alex) -modify vfio_region_write() to drop guest writes to non-mmap'd read-only
region (Alex)

Yan Zhao (3):
memory: drop guest writes to read-only ram device regions hw/vfio: drop guest writes to ro regions hw/vfio: let read-only flag take effect for mmap'd regions

 hw/vfio/common.c     | 12 +++++++++++-
 hw/vfio/trace-events |  2 +-
 memory.c             |  6 +++++-
 trace-events         |  2 +-
 4 files changed, 18 insertions(+), 4 deletions(-)
Signature · 1 line
2.17.1
author
#5 lore raw

Forgot the changelog, so sent this patch 0/3 again. sorry about that.

Quoted 31 lines · Yan Y

patch 1 modifies handler of ram device memory regions to drop guest writes to read-only ram device memory regions

patch 2 modifies handler of non-mmap'd read-only vfio regions to drop guest writes to those regions

patch 3 let mmap'd read-only vfio regions be able to generate vmexit for guest write. so, without patch 1, host qemu would crash on guest write to this read-only region. with patch 1, host qemu would drop the writes.

Changelog:
v2:

(quoted diff) +0 -3
-split one big patches into smaller ones (Philippe)
-modify existing trace to record guest writes to read-only memory (Alex)
-modify vfio_region_write() to drop guest writes to non-mmap'd read-only
 region (Alex)

Yan Zhao (3):
memory: drop guest writes to read-only ram device regions hw/vfio: drop guest writes to ro regions hw/vfio: let read-only flag take effect for mmap'd regions

 hw/vfio/common.c     | 12 +++++++++++-
 hw/vfio/trace-events |  2 +-
 memory.c             |  6 +++++-
 trace-events         |  2 +-
 4 files changed, 18 insertions(+), 4 deletions(-)
Signature · 1 line
2.17.1
#6 lore raw
In reply to Yan Zhao
Quoted 10 lines · Yan Zhao

patch 1 modifies handler of ram device memory regions to drop guest writes to read-only ram device memory regions

patch 2 modifies handler of non-mmap'd read-only vfio regions to drop guest writes to those regions

patch 3 let mmap'd read-only vfio regions be able to generate vmexit for guest write. so, without patch 1, host qemu would crash on guest write to this read-only region. with patch 1, host qemu would drop the writes.

Just FYI, there seems to be a minor clock or timezone issue with however you're sending these emails: the Date header you sent reads "Date: Fri, 3 Apr 2020 16:56:57 +0000" but that time in the UTC +0000 timezone is (as I write this) still several hours in the future. This isn't a big deal but you probably want to look into fixing it.

(Noticed because the wrong-date makes your patches stick to the top of the https://patchew.org/QEMU/ list even after other patches arrive after them.)

thanks
-- PMM

author
#7 lore raw
Quoted 19 lines · Peter Maydell
Quoted 10 lines · Yan Zhao

patch 1 modifies handler of ram device memory regions to drop guest writes to read-only ram device memory regions

patch 2 modifies handler of non-mmap'd read-only vfio regions to drop guest writes to those regions

patch 3 let mmap'd read-only vfio regions be able to generate vmexit for guest write. so, without patch 1, host qemu would crash on guest write to this read-only region. with patch 1, host qemu would drop the writes.

Just FYI, there seems to be a minor clock or timezone issue with however you're sending these emails: the Date header you sent reads "Date: Fri, 3 Apr 2020 16:56:57 +0000" but that time in the UTC +0000 timezone is (as I write this) still several hours in the future. This isn't a big deal but you probably want to look into fixing it.

Quoted 4 lines

(Noticed because the wrong-date makes your patches stick to the top of the https://patchew.org/QEMU/ list even after other patches arrive after them.)

thanks for informing of it! will pay attention to it. Thanks
Yan