Skip to contentSkip to conversation

arm/arm64: KVM: Get around cache incoherency

RFC New Jérémy Fanguède 1 participant 5 messages May 5, 2015

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

cover arm/arm64: KVM: Get around cache incoherency

This RFC series shows how we can solve cache incohenrency issues on ARM/ARM64. This implementation uses an ioctl that flushes the data cache for a given range of the guest physical memory, this possible solution was already broached in the previous discussion [1][2].

Each access (read or write) to the guest memory that could lead to inconsistency is flushed if needed. A light optimization is done to minimize the ioctl call.

Currently tested with the following devices: e1000, megasas, lsi53c895a, nec-usb-xhci, usb-ehci (with usb-host, usb-tablet, usb-kbd and usb-mouse), which were failling with KVM and now work fine with this patch.

The kernel needs this patch:
[RFC] ARM/ARM64: KVM: Implement KVM_FLUSH_DCACHE_GPA ioctl

[1] https://lists.cs.columbia.edu/pipermail/kvmarm/2015-February/013593.html
[2] https://lists.nongnu.org/archive/html/qemu-devel/2015-03/msg01254.html

Jérémy Fanguède (4):
linux-headers update
target-arm/kvm: Flush data cache support
kvm-all: Pre-run cache coherency maintenance
exec: Flush data cache when needed

 exec.c                    | 19 ++++++++++++++++++
 include/sysemu/kvm.h      |  3 +++
 kvm-all.c                 |  1 +
 linux-headers/linux/kvm.h |  6 ++++++
 stubs/kvm.c               |  9 +++++++++
 target-arm/kvm.c          | 51 +++++++++++++++++++++++++++++++++++++++++++++++
 6 files changed, 89 insertions(+)
Signature · 6 lines
1.9.1

_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
#1 lore raw

RFC 1/4 linux-headers update

Add KVM_FLUSH_DCACHE_GPA ioctl.

---
 linux-headers/linux/kvm.h | 6 ++++++
 1 file changed, 6 insertions(+)
linux-headers/linux/kvm.h +6 -0
index b96d978..81d2f64 100644
@@ -978,6 +978,11 @@ struct kvm_arm_device_addr {
 	__u64 addr;
 };
 
+struct kvm_mem_addr {
+	__u64 addr;
+	__u32 len;
+};
+
 /*
  * Device control API, available with KVM_CAP_DEVICE_CTRL
  */
@@ -1199,6 +1204,7 @@ struct kvm_s390_ucas_mapping {
 /* Available with KVM_CAP_S390_IRQ_STATE */
 #define KVM_S390_SET_IRQ_STATE	  _IOW(KVMIO, 0xb5, struct kvm_s390_irq_state)
 #define KVM_S390_GET_IRQ_STATE	  _IOW(KVMIO, 0xb6, struct kvm_s390_irq_state)
+#define KVM_FLUSH_DCACHE_GPA     _IOW(KVMIO,  0xb7, struct kvm_mem_addr)
 
 #define KVM_DEV_ASSIGN_ENABLE_IOMMU	(1 << 0)
 #define KVM_DEV_ASSIGN_PCI_2_3		(1 << 1)
Signature · 6 lines
1.9.1

_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
#2 lore raw

RFC 2/4 target-arm/kvm: Flush data cache support

In reply to Jérémy Fanguède

Implement data cache maintenance coherency functions, by using FLUSH_DCACHE_GPA ioctl. Introduce kvm_arm_maintain_cache_coherency() for flushing the data cache if necessary, a very simple logic is implemented to reduce number of flushes due to reads. Two wrapping functions are exposed, for easier usage.

---
 include/sysemu/kvm.h |  3 +++
 stubs/kvm.c          |  9 +++++++++
 target-arm/kvm.c     | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 63 insertions(+)
include/sysemu/kvm.h +3 -0
index 197e6c0..986f365 100644
@@ -451,4 +451,7 @@ int kvm_set_one_reg(CPUState *cs, uint64_t id, void *source);
  * Returns: 0 on success, or a negative errno on failure.
  */
 int kvm_get_one_reg(CPUState *cs, uint64_t id, void *target);
+
+void kvm_arch_cache_flush_needed(hwaddr addr, int len, bool is_write);
+void kvm_arch_cache_coherency_pre_run(void);
 #endif
stubs/kvm.c +9 -0
index e7c60b6..8ed5380 100644
@@ -5,3 +5,12 @@ int kvm_arch_irqchip_create(KVMState *s)
 {
     return 0;
 }
+
+
+void kvm_arch_cache_flush_needed(hwaddr addr, int len, bool is_write)
+{
+}
+
+void kvm_arch_cache_coherency_pre_run(void)
+{
+}
target-arm/kvm.c +51 -0

54 lines not on this page · raw message

Signature · 1 line
1.9.1
#3 lore raw

RFC 3/4 kvm-all: Pre-run cache coherency maintenance

In reply to Jérémy Fanguède

Inform the cache coherency maintenance of an exit from userspace.

---
 kvm-all.c | 1 +
 1 file changed, 1 insertion(+)
kvm-all.c +1 -0
index 28f4589..c4b15b0 100644
@@ -1802,6 +1802,7 @@ int kvm_cpu_exec(CPUState *cpu)
         }
 
         kvm_arch_pre_run(cpu, run);
+        kvm_arch_cache_coherency_pre_run();
         if (cpu->exit_request) {
             DPRINTF("interrupt exit requested\n");
             /*
Signature · 1 line
1.9.1
#4 lore raw

RFC 4/4 exec: Flush data cache when needed

In reply to Jérémy Fanguède

Flush the data cache when accesses occur in the guest ram memory.

---
 exec.c | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)
exec.c +19 -0
index ae37b98..0f859a3 100644
@@ -2372,6 +2372,9 @@ MemTxResult address_space_rw(AddressSpace *as, hwaddr addr, MemTxAttrs attrs,
                 ptr = qemu_get_ram_ptr(addr1);
                 memcpy(ptr, buf, l);
                 invalidate_and_set_dirty(addr1, l);
+                if (kvm_enabled()) {
+                    kvm_arch_cache_flush_needed(addr, l, is_write);
+                }
             }
         } else {
             if (!memory_access_is_direct(mr, is_write)) {
@@ -2408,6 +2411,9 @@ MemTxResult address_space_rw(AddressSpace *as, hwaddr addr, MemTxAttrs attrs,
             } else {
                 /* RAM case */
                 ptr = qemu_get_ram_ptr(mr->ram_addr + addr1);
+                if (kvm_enabled()) {
+                    kvm_arch_cache_flush_needed(addr, l, is_write);
+                }
                 memcpy(buf, ptr, l);
             }
         }
@@ -2646,6 +2652,14 @@ void *address_space_map(AddressSpace *as,
         return bounce.buffer;
     }
 
+    /* Need to be flushed only if we are reading */
+    if (!is_write) {
+        /* Don't flush if it's a cpu_physical_memory_map call */
+        if (kvm_enabled() && as != &address_space_memory) {
+            kvm_arch_cache_flush_needed(addr, l, is_write);
+        }
+    }
+
     base = xlat;
     raddr = memory_region_get_ram_addr(mr);
 
@@ -2679,6 +2693,7 @@ void address_space_unmap(AddressSpace *as, void *buffer, hwaddr len,
     if (buffer != bounce.buffer) {
         MemoryRegion *mr;
         ram_addr_t addr1;
+        hwaddr base;
 
         mr = qemu_ram_addr_from_host(buffer, &addr1);
         assert(mr != NULL);
@@ -2688,6 +2703,10 @@ void address_space_unmap(AddressSpace *as, void *buffer, hwaddr len,
         if (xen_enabled()) {
             xen_invalidate_map_cache_entry(buffer);
         }
+        if (kvm_enabled() && as != &address_space_memory) {
+            base = object_property_get_int(OBJECT(mr), "addr", NULL);
+            kvm_arch_cache_flush_needed(addr1 + base, access_len, is_write);
+        }
         memory_region_unref(mr);
         return;
     }
Signature · 6 lines
1.9.1

_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm