Skip to contentSkip to conversation

KVM: PPC: E500: Implement MMU Notifiers

PATCH New Alexander Graf 1 participant 6 messages Aug 9, 2012

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

cover KVM: PPC: E500: Implement MMU Notifiers

This patch set adds a very simple implementation of MMU notifiers for the e500 target. Along the way, I stumbled over a few other things that I've put into the same patch set, namely:

* support remote vcpu requests
* hva_to_memslot helper function
* icache flushing on page map
* additional trace points

v1 -> v2:

- remove patches that are already merged into kvm-ppc-next
- split up mmu notifier patch
- remove hva_to_memslot
- add trace points
- change icache flush method to be in arch/powerpc
- make request checks preemptible

Alexander Graf (5):
KVM: PPC: BookE: Add check_requests helper function
KVM: PPC: BookE: Add support for vcpu->mode
KVM: PPC: E500: Implement MMU notifiers
KVM: PPC: Add cache flush on page map
KVM: PPC: BookE: Add some more trace points

 arch/powerpc/include/asm/kvm_host.h   |    4 +-
 arch/powerpc/include/asm/kvm_ppc.h    |   13 ++++++
 arch/powerpc/kvm/Kconfig              |    2 +
 arch/powerpc/kvm/book3s_32_mmu_host.c |    3 +
 arch/powerpc/kvm/book3s_64_mmu_host.c |    2 +
 arch/powerpc/kvm/booke.c              |   44 +++++++++++++++++---
 arch/powerpc/kvm/e500_tlb.c           |   66 +++++++++++++++++++++++++++---
 arch/powerpc/kvm/trace.h              |   71 +++++++++++++++++++++++++++++++++
 arch/powerpc/mm/mem.c                 |    1 +
 9 files changed, 192 insertions(+), 14 deletions(-)
#1 lore raw

PATCH 1/5 KVM: PPC: BookE: Add check_requests helper function

We need a central place to check for pending requests in. Add one that only does the timer check we already do in a different place.

Later, this central function can be extended by more checks.

---
 arch/powerpc/kvm/booke.c |   24 +++++++++++++++++-------
 1 files changed, 17 insertions(+), 7 deletions(-)
arch/powerpc/kvm/booke.c +17 -7
index 1d4ce9a..bcf87fe 100644
@@ -419,13 +419,6 @@ static void kvmppc_core_check_exceptions(struct kvm_vcpu *vcpu)
 	unsigned long *pending = &vcpu->arch.pending_exceptions;
 	unsigned int priority;
 
-	if (vcpu->requests) {
-		if (kvm_check_request(KVM_REQ_PENDING_TIMER, vcpu)) {
-			smp_mb();
-			update_timer_ints(vcpu);
-		}
-	}
-
 	priority = __ffs(*pending);
 	while (priority < BOOKE_IRQPRIO_MAX) {
 		if (kvmppc_booke_irqprio_deliver(vcpu, priority))
@@ -461,6 +454,14 @@ int kvmppc_core_prepare_to_enter(struct kvm_vcpu *vcpu)
 	return r;
 }
 
+static void kvmppc_check_requests(struct kvm_vcpu *vcpu)
+{
+	if (vcpu->requests) {
+		if (kvm_check_request(KVM_REQ_PENDING_TIMER, vcpu))
+			update_timer_ints(vcpu);
+	}
+}
+
 /*
  * Common checks before entering the guest world.  Call with interrupts
  * disabled.
@@ -485,6 +486,15 @@ static int kvmppc_prepare_to_enter(struct kvm_vcpu *vcpu)
 			break;
 		}
 
+		smp_mb();
+		if (vcpu->requests) {
+			/* Make sure we process requests preemptable */
+			local_irq_enable();
+			kvmppc_check_requests(vcpu);
+			local_irq_disable();
+			continue;
+		}
+
 		if (kvmppc_core_prepare_to_enter(vcpu)) {
 			/* interrupts got enabled in between, so we
 			   are back at square 1 */
Signature · 1 line
1.6.0.2
#2 lore raw

PATCH 2/5 KVM: PPC: BookE: Add support for vcpu->mode

In reply to Alexander Graf

Generic KVM code might want to know whether we are inside guest context or outside. It also wants to be able to push us out of guest context.

Add support to the BookE code for the generic vcpu->mode field that describes the above states.

---
 arch/powerpc/kvm/booke.c |   11 +++++++++++
 1 files changed, 11 insertions(+), 0 deletions(-)
arch/powerpc/kvm/booke.c +11 -0
index bcf87fe..70a86c0 100644
@@ -501,6 +501,15 @@ static int kvmppc_prepare_to_enter(struct kvm_vcpu *vcpu)
 			continue;
 		}
 
+		if (vcpu->mode == EXITING_GUEST_MODE) {
+			r = 1;
+			break;
+		}
+
+		/* Going into guest context! Yay! */
+		vcpu->mode = IN_GUEST_MODE;
+		smp_wmb();
+
 		break;
 	}
 
@@ -572,6 +581,8 @@ int kvmppc_vcpu_run(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu)
 	kvm_guest_exit();
 
 out:
+	vcpu->mode = OUTSIDE_GUEST_MODE;
+	smp_wmb();
 	local_irq_enable();
 	return ret;
 }
Signature · 1 line
1.6.0.2
#3 lore raw

PATCH 3/5 KVM: PPC: E500: Implement MMU notifiers

In reply to Alexander Graf

The e500 target has lived without mmu notifiers ever since it got introduced, but fails for the user space check on them with hugetlbfs.

So in order to get that one working, implement mmu notifiers in a reasonably dumb fashion and be happy. On embedded hardware, we almost never end up with mmu notifier calls, since most people don't overcommit.

---

v1 -> v2:

- drop refcount for in-use pages
- mark pages dirty at mapping time, not unmapping time
- drop hva_to_memslot usage

---
 arch/powerpc/include/asm/kvm_host.h |    3 +-
 arch/powerpc/include/asm/kvm_ppc.h  |    1 +
 arch/powerpc/kvm/Kconfig            |    2 +
 arch/powerpc/kvm/booke.c            |    6 +++
 arch/powerpc/kvm/e500_tlb.c         |   60 +++++++++++++++++++++++++++++++---
 5 files changed, 65 insertions(+), 7 deletions(-)
arch/powerpc/include/asm/kvm_host.h +2 -1
index 572ad01..ed75bc9 100644
@@ -45,7 +45,8 @@
 #define KVM_COALESCED_MMIO_PAGE_OFFSET 1
 #endif
 
-#ifdef CONFIG_KVM_BOOK3S_64_HV
+#if defined(CONFIG_KVM_BOOK3S_64_HV) || defined(CONFIG_KVM_E500V2) || \
+    defined(CONFIG_KVM_E500MC)
 #include <linux/mmu_notifier.h>
 
 #define KVM_ARCH_WANT_MMU_NOTIFIER
arch/powerpc/include/asm/kvm_ppc.h +1 -0
index 0124937..c38e824 100644
@@ -104,6 +104,7 @@ extern void kvmppc_core_queue_external(struct kvm_vcpu *vcpu,
                                        struct kvm_interrupt *irq);
 extern void kvmppc_core_dequeue_external(struct kvm_vcpu *vcpu,
                                          struct kvm_interrupt *irq);
+extern void kvmppc_core_flush_tlb(struct kvm_vcpu *vcpu);
 
 extern int kvmppc_core_emulate_op(struct kvm_run *run, struct kvm_vcpu *vcpu,
                                   unsigned int op, int *advance);
arch/powerpc/kvm/Kconfig +2 -0
index f4dacb9..40cad8c 100644
@@ -123,6 +123,7 @@ config KVM_E500V2
 	depends on EXPERIMENTAL && E500 && !PPC_E500MC
 	select KVM
 	select KVM_MMIO
+	select MMU_NOTIFIER
 	---help---
 	  Support running unmodified E500 guest kernels in virtual machines on
 	  E500v2 host processors.
@@ -138,6 +139,7 @@ config KVM_E500MC
 	select KVM
 	select KVM_MMIO
 	select KVM_BOOKE_HV
+	select MMU_NOTIFIER
 	---help---
 	  Support running unmodified E500MC/E5500 (32-bit) guest kernels in
 	  virtual machines on E500MC/E5500 host processors.
arch/powerpc/kvm/booke.c +6 -0
index 70a86c0..52f6cbb 100644
@@ -459,6 +459,10 @@ static void kvmppc_check_requests(struct kvm_vcpu *vcpu)
 	if (vcpu->requests) {
 		if (kvm_check_request(KVM_REQ_PENDING_TIMER, vcpu))
 			update_timer_ints(vcpu);
+#if defined(CONFIG_KVM_E500V2) || defined(CONFIG_KVM_E500MC)
+		if (kvm_check_request(KVM_REQ_TLB_FLUSH, vcpu))
+			kvmppc_core_flush_tlb(vcpu);
+#endif
 	}
 }
 
@@ -579,6 +583,8 @@ int kvmppc_vcpu_run(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu)
 #endif
 
 	kvm_guest_exit();
+	vcpu->mode = OUTSIDE_GUEST_MODE;
+	smp_wmb();
 
 out:
 	vcpu->mode = OUTSIDE_GUEST_MODE;
arch/powerpc/kvm/e500_tlb.c +54 -6

90 lines not on this page · raw message

Signature · 1 line
1.6.0.2
#4 lore raw

PATCH 4/5 KVM: PPC: Add cache flush on page map

In reply to Alexander Graf

When we map a page that wasn't icache cleared before, do so when first mapping it in KVM using the same information bits as the Linux mapping logic. That way we are 100% sure that any page we map does not have stale entries in the icache.

---

v1 -> v2:

- move code to arch/powerpc

---
 arch/powerpc/include/asm/kvm_host.h   |    1 +
 arch/powerpc/include/asm/kvm_ppc.h    |   12 ++++++++++++
 arch/powerpc/kvm/book3s_32_mmu_host.c |    3 +++
 arch/powerpc/kvm/book3s_64_mmu_host.c |    2 ++
 arch/powerpc/kvm/e500_tlb.c           |    3 +++
 arch/powerpc/mm/mem.c                 |    1 +
 6 files changed, 22 insertions(+), 0 deletions(-)
arch/powerpc/include/asm/kvm_host.h +1 -0
index ed75bc9..f5c289b 100644
@@ -33,6 +33,7 @@
 #include <asm/kvm_asm.h>
 #include <asm/processor.h>
 #include <asm/page.h>
+#include <asm/cacheflush.h>
 
 #define KVM_MAX_VCPUS		NR_CPUS
 #define KVM_MAX_VCORES		NR_CPUS
arch/powerpc/include/asm/kvm_ppc.h +12 -0
index c38e824..88de314 100644
@@ -220,4 +220,16 @@ void kvmppc_claim_lpid(long lpid);
 void kvmppc_free_lpid(long lpid);
 void kvmppc_init_lpid(unsigned long nr_lpids);
 
+static inline void kvmppc_mmu_flush_icache(pfn_t pfn)
+{
+	/* Clear i-cache for new pages */
+	struct page *page;
+	page = pfn_to_page(pfn);
+	if (!test_bit(PG_arch_1, &page->flags)) {
+		flush_dcache_icache_page(page);
+		set_bit(PG_arch_1, &page->flags);
+	}
+}
+
+
 #endif /* __POWERPC_KVM_PPC_H__ */
arch/powerpc/kvm/book3s_32_mmu_host.c +3 -0
index f922c29..837f13e 100644
@@ -211,6 +211,9 @@ next_pteg:
 		pteg1 |= PP_RWRX;
 	}
 
+	if (orig_pte->may_execute)
+		kvmppc_mmu_flush_icache(hpaddr >> PAGE_SHIFT);
+
 	local_irq_disable();
 
 	if (pteg[rr]) {
arch/powerpc/kvm/book3s_64_mmu_host.c +2 -0
index 10fc8ec..0688b6b 100644
@@ -126,6 +126,8 @@ int kvmppc_mmu_map_page(struct kvm_vcpu *vcpu, struct kvmppc_pte *orig_pte)
 
 	if (!orig_pte->may_execute)
 		rflags |= HPTE_R_N;
+	else
+		kvmppc_mmu_flush_icache(hpaddr >> PAGE_SHIFT);
 
 	hash = hpt_hash(va, PTE_SIZE, MMU_SEGSIZE_256M);
 
arch/powerpc/kvm/e500_tlb.c +3 -0
index c439754..56bc92c 100644
@@ -544,6 +544,9 @@ static inline void kvmppc_e500_shadow_map(struct kvmppc_vcpu_e500 *vcpu_e500,
 	kvmppc_e500_setup_stlbe(&vcpu_e500->vcpu, gtlbe, tsize,
 				ref, gvaddr, stlbe);
 
+	/* Clear i-cache for new pages */
+	kvmppc_mmu_flush_icache(pfn);
+
 	/* Drop refcount on page, so that mmu notifiers can clear it */
 	kvm_release_pfn_clean(pfn);
 }
arch/powerpc/mm/mem.c +1 -0
index baaafde..fbdad0e 100644
@@ -469,6 +469,7 @@ void flush_dcache_icache_page(struct page *page)
 	__flush_dcache_icache_phys(page_to_pfn(page) << PAGE_SHIFT);
 #endif
 }
+EXPORT_SYMBOL(flush_dcache_icache_page);
 
 void clear_user_page(void *page, unsigned long vaddr, struct page *pg)
 {
Signature · 1 line
1.6.0.2
#5 lore raw

PATCH 5/5 KVM: PPC: BookE: Add some more trace points

In reply to Alexander Graf

Without trace points, debugging what exactly is going on inside guest code can be very tricky. Add a few more trace points at places that hopefully tell us more when things go wrong.

---
 arch/powerpc/kvm/booke.c    |    3 ++
 arch/powerpc/kvm/e500_tlb.c |    3 ++
 arch/powerpc/kvm/trace.h    |   71 +++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 77 insertions(+), 0 deletions(-)
arch/powerpc/kvm/booke.c +3 -0
index 52f6cbb..00bcc57 100644
@@ -143,6 +143,7 @@ void kvmppc_set_msr(struct kvm_vcpu *vcpu, u32 new_msr)
 static void kvmppc_booke_queue_irqprio(struct kvm_vcpu *vcpu,
                                        unsigned int priority)
 {
+	trace_kvm_booke_queue_irqprio(vcpu, priority);
 	set_bit(priority, &vcpu->arch.pending_exceptions);
 }
 
@@ -457,6 +458,8 @@ int kvmppc_core_prepare_to_enter(struct kvm_vcpu *vcpu)
 static void kvmppc_check_requests(struct kvm_vcpu *vcpu)
 {
 	if (vcpu->requests) {
+		trace_kvm_check_requests(vcpu);
+
 		if (kvm_check_request(KVM_REQ_PENDING_TIMER, vcpu))
 			update_timer_ints(vcpu);
 #if defined(CONFIG_KVM_E500V2) || defined(CONFIG_KVM_E500MC)
arch/powerpc/kvm/e500_tlb.c +3 -0
index 56bc92c..f5e531f 100644
@@ -312,6 +312,7 @@ static inline void kvmppc_e500_ref_setup(struct tlbe_ref *ref,
 static inline void kvmppc_e500_ref_release(struct tlbe_ref *ref)
 {
 	if (ref->flags & E500_TLB_VALID) {
+		trace_kvm_booke206_ref_release(ref->pfn, ref->flags);
 		ref->flags = 0;
 	}
 }
@@ -1076,6 +1077,8 @@ void kvmppc_mmu_map(struct kvm_vcpu *vcpu, u64 eaddr, gpa_t gpaddr,
 
 int kvm_unmap_hva(struct kvm *kvm, unsigned long hva)
 {
+	trace_kvm_unmap_hva(hva);
+
 	/*
 	 * Flush all shadow tlb entries everywhere. This is slow, but
 	 * we are 100% sure that we catch the to be unmapped page
arch/powerpc/kvm/trace.h +71 -0

89 lines not on this page · raw message

Signature · 1 line
1.6.0.2