Skip to contentSkip to conversation

memory: Convert skip_dump to ram_device and avoid memcpy

PATCH In review Alex Williamson 3 participants 5 messages Oct 25, 2016 – Oct 28, 2016

lore
Share & tools
lore
#0 lore raw

v2 cover memory: Convert skip_dump to ram_device and avoid memcpy

v2: retain ram_device flag to avoid extra cache miss, per Paolo.

Paolo, posting for completeness, I can merge through my tree if you want to Ack. Thanks,

Alex

---

Alex Williamson (2):
memory: Replace skip_dump flag with "ram_device"
memory: Don't use memcpy for ram_device regions

 hw/vfio/common.c      |    9 ++----
 hw/vfio/spapr.c       |    2 +
 include/exec/memory.h |   47 ++++++++++++++++++++---------
 memory.c              |   80 +++++++++++++++++++++++++++++++++++++++++++++++--
 memory_mapping.c      |    2 +
 trace-events          |    2 +
 6 files changed, 116 insertions(+), 26 deletions(-)
#1 lore raw

PATCH v2 1/2 memory: Replace skip_dump flag with "ram_device"

Setting skip_dump on a MemoryRegion allows us to modify one specific code path, but the restriction we're trying to address encompasses more than that. If we have a RAM MemoryRegion backed by a physical device, it not only restricts our ability to dump that region, but also affects how we should manipulate it. Here we recognize that MemoryRegions do not change to sometimes allow dumps and other times not, so we replace setting the skip_dump flag with a new initializer so that we know exactly the type of region to which we're applying this behavior.

---
 hw/vfio/common.c      |    9 ++++-----
 hw/vfio/spapr.c       |    2 +-
 include/exec/memory.h |   41 ++++++++++++++++++++++++++++-------------
 memory.c              |   13 +++++++++----
 memory_mapping.c      |    2 +-
 5 files changed, 43 insertions(+), 24 deletions(-)
hw/vfio/common.c +4 -5
index 9505fb3..c764cb3 100644
@@ -724,12 +724,11 @@ int vfio_region_mmap(VFIORegion *region)
 
         name = g_strdup_printf("%s mmaps[%d]",
                                memory_region_name(region->mem), i);
-        memory_region_init_ram_ptr(&region->mmaps[i].mem,
-                                   memory_region_owner(region->mem),
-                                   name, region->mmaps[i].size,
-                                   region->mmaps[i].mmap);
+        memory_region_init_ram_device_ptr(&region->mmaps[i].mem,
+                                          memory_region_owner(region->mem),
+                                          name, region->mmaps[i].size,
+                                          region->mmaps[i].mmap);
         g_free(name);
-        memory_region_set_skip_dump(&region->mmaps[i].mem);
         memory_region_add_subregion(region->mem, region->mmaps[i].offset,
                                     &region->mmaps[i].mem);
 
hw/vfio/spapr.c +1 -1
index 7443d34..4409bcc 100644
@@ -25,7 +25,7 @@ static bool vfio_prereg_listener_skipped_section(MemoryRegionSection *section)
     }
 
     return !memory_region_is_ram(section->mr) ||
-            memory_region_is_skip_dump(section->mr);
+            memory_region_is_ram_device(section->mr);
 }
 
 static void *vfio_prereg_gpa_to_vaddr(MemoryRegionSection *section, hwaddr gpa)
include/exec/memory.h +28 -13

63 lines not on this page · raw message

memory.c +9 -4
index edbc701..7ffcff1 100644
@@ -1355,9 +1355,14 @@ void memory_region_init_ram_ptr(MemoryRegion *mr,
     mr->ram_block = qemu_ram_alloc_from_ptr(size, ptr, mr, &error_fatal);
 }
 
-void memory_region_set_skip_dump(MemoryRegion *mr)
+void memory_region_init_ram_device_ptr(MemoryRegion *mr,
+                                       Object *owner,
+                                       const char *name,
+                                       uint64_t size,
+                                       void *ptr)
 {
-    mr->skip_dump = true;
+    memory_region_init_ram_ptr(mr, owner, name, size, ptr);
+    mr->ram_device = true;
 }
 
 void memory_region_init_alias(MemoryRegion *mr,
@@ -1491,9 +1496,9 @@ const char *memory_region_name(const MemoryRegion *mr)
     return mr->name;
 }
 
-bool memory_region_is_skip_dump(MemoryRegion *mr)
+bool memory_region_is_ram_device(MemoryRegion *mr)
 {
-    return mr->skip_dump;
+    return mr->ram_device;
 }
 
 uint8_t memory_region_get_dirty_log_mask(MemoryRegion *mr)
memory_mapping.c +1 -1
index e3e0d95..6a39d71 100644
@@ -206,7 +206,7 @@ static void guest_phys_blocks_region_add(MemoryListener *listener,
 
     /* we only care about RAM */
     if (!memory_region_is_ram(section->mr) ||
-        memory_region_is_skip_dump(section->mr)) {
+        memory_region_is_ram_device(section->mr)) {
         return;
     }
 
#2 lore raw

PATCH v2 2/2 memory: Don't use memcpy for ram_device regions

In reply to Alex Williamson

With a vfio assigned device we lay down a base MemoryRegion registered as an IO region, giving us read & write accessors. If the region supports mmap, we lay down a higher priority sub-region MemoryRegion on top of the base layer initialized as a RAM device pointer to the mmap. Finally, if we have any quirks for the device (ie. address ranges that need additional virtualization support), we put another IO sub-region on top of the mmap MemoryRegion. When this is flattened, we now potentially have sub-page mmap MemoryRegions exposed which cannot be directly mapped through KVM.

This is as expected, but a subtle detail of this is that we end up with two different access mechanisms through QEMU. If we disable the mmap MemoryRegion, we make use of the IO MemoryRegion and service accesses using pread and pwrite to the vfio device file descriptor. If the mmap MemoryRegion is enabled and results in one of these sub-page gaps, QEMU handles the access as RAM, using memcpy to the mmap. Using either pread/pwrite or the mmap directly should be correct, but using memcpy causes us problems. I expect that not only does memcpy not necessarily honor the original width and alignment in performing a copy, but it potentially also uses processor instructions not intended for MMIO spaces. It turns out that this has been a problem for Realtek NIC assignment, which has such a quirk that creates a sub-page mmap MemoryRegion access.

To resolve this, we disable memory_access_is_direct() for ram_device regions since QEMU assumes that it can use memcpy for those regions. Instead we access through MemoryRegionOps, which replaces the memcpy with simple de-references of standard sizes to the host memory.

With this patch we attempt to provide unrestricted access to the RAM device, allowing byte through qword access as well as unaligned access. The assumption here is that accesses initiated by the VM are driven by a device specific driver, which knows the device capabilities. If unaligned accesses are not supported by the device, we don't want them to work in a VM by performing multiple aligned accesses to compose the unaligned access. A down-side of this philosophy is that the xp command from the monitor attempts to use the largest available access weidth, unaware of the underlying device. Using memcpy had this same restriction, but at least now an operator can dump individual registers, even if blocks of device memory may result in access widths beyond the capabilities of a given device (RTL NICs only support up to dword).

---
 include/exec/memory.h |    6 +++-
 memory.c              |   67 +++++++++++++++++++++++++++++++++++++++++++++++++
 trace-events          |    2 +
 3 files changed, 73 insertions(+), 2 deletions(-)
include/exec/memory.h +4 -2
index a75b8c3..9728a2f 100644
@@ -1480,9 +1480,11 @@ void *qemu_map_ram_ptr(RAMBlock *ram_block, ram_addr_t addr);
 static inline bool memory_access_is_direct(MemoryRegion *mr, bool is_write)
 {
     if (is_write) {
-        return memory_region_is_ram(mr) && !mr->readonly;
+        return memory_region_is_ram(mr) &&
+               !mr->readonly && !memory_region_is_ram_device(mr);
     } else {
-        return memory_region_is_ram(mr) || memory_region_is_romd(mr);
+        return (memory_region_is_ram(mr) && !memory_region_is_ram_device(mr)) ||
+               memory_region_is_romd(mr);
     }
 }
 
memory.c +67 -0

79 lines not on this page · raw message

trace-events +2 -0
index 8ecded5..f74e1d3 100644
@@ -121,6 +121,8 @@ memory_region_subpage_read(int cpu_index, void *mr, uint64_t offset, uint64_t va
 memory_region_subpage_write(int cpu_index, void *mr, uint64_t offset, uint64_t value, unsigned size) "cpu %d mr %p offset %#"PRIx64" value %#"PRIx64" size %u"
 memory_region_tb_read(int cpu_index, uint64_t addr, uint64_t value, unsigned size) "cpu %d addr %#"PRIx64" value %#"PRIx64" size %u"
 memory_region_tb_write(int cpu_index, uint64_t addr, uint64_t value, unsigned size) "cpu %d addr %#"PRIx64" value %#"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 %#"PRIx64" value %#"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 %#"PRIx64" value %#"PRIx64" size %u"
 
 ### Guest events, keep at bottom
 
Acked-by on the v2 series
#3 lore raw
In reply to Alex Williamson
Quoted 4 lines · Alex Williamson

v2: retain ram_device flag to avoid extra cache miss, per Paolo.

Paolo, posting for completeness, I can merge through my tree if you want to Ack. Thanks,

Great, thanks.

Quoted 18 lines

Alex

---

Alex Williamson (2):
memory: Replace skip_dump flag with "ram_device"
memory: Don't use memcpy for ram_device regions

 hw/vfio/common.c      |    9 ++----
 hw/vfio/spapr.c       |    2 +
 include/exec/memory.h |   47 ++++++++++++++++++++---------
 memory.c              |   80 +++++++++++++++++++++++++++++++++++++++++++++++--
 memory_mapping.c      |    2 +
 trace-events          |    2 +
 6 files changed, 116 insertions(+), 26 deletions(-)
#4 lore raw
In reply to Alex Williamson

I tested these
https://lists.nongnu.org/archive/html/qemu-devel/2016-10/msg05900.html
https://lists.nongnu.org/archive/html/qemu-devel/2016-10/msg05901.html
https://lists.nongnu.org/archive/html/qemu-devel/2016-10/msg05902.html

together with [Qemu-devel] [PULL 18/19] vfio/pci: Fix vfio_rtl8168_quirk_data_read add
https://lists.nongnu.org/archive/html/qemu-devel/2016-10/msg03911.html

on top of qemu-stable-2.7.0

successfully on IPFire (Linux) and Windows 10 x64 with the Realtek driver.

I suggest all 3 commits (msg05901, msg05902, and msg03911) should become serious candidates for qemu-2.7.1.

Regards,

Thorsten

Am 25.10.2016 um 20:17 schrieb Alex Williamson:

Quoted 22 lines

v2: retain ram_device flag to avoid extra cache miss, per Paolo.

Paolo, posting for completeness, I can merge through my tree if you want to Ack. Thanks,

Alex

---

Alex Williamson (2):
memory: Replace skip_dump flag with "ram_device"
memory: Don't use memcpy for ram_device regions

 hw/vfio/common.c      |    9 ++----
 hw/vfio/spapr.c       |    2 +
 include/exec/memory.h |   47 ++++++++++++++++++++---------
 memory.c              |   80 +++++++++++++++++++++++++++++++++++++++++++++++--
 memory_mapping.c      |    2 +
 trace-events          |    2 +
 6 files changed, 116 insertions(+), 26 deletions(-)