Skip to contentSkip to conversation

KVM: PPC: e500: Shadow TLB Improvements v2

PATCH Superseded Alexander Graf 2 participants 10 messages Jan 18, 2013

Subsystems (3)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

v2 cover KVM: PPC: e500: Shadow TLB Improvements v2

This patch set improves the shadow TLB handling of our e500 target.

The really important bit here is that with these patches applied, we can map guest TLB1 entries into the host's TLB0. This gives a significant performance improvement as you can see below.

Alex

v1 -> v2:

- new patch: Move write_stlbe higher
- new patch: Explicitly mark shadow maps invalid
- new patch: Propagate errors when shadow mapping
- remove force init logic, it's handled by the explicit invalidate now
- leave stlbe uninitialized
- allow mixing of TLB0 and TLB1_BITMAP backing of TLB1 pages

---

without patch, using 4k backed memory:

$ time for i in {1..1000}; do /bin/echo > /dev/null; done
real	0m12.947s
user	0m1.076s
sys	0m9.720s

with hugetlbfs:

$ time for i in {1..1000}; do /bin/echo > /dev/null; done

real 0m3.262s
user 0m0.464s
sys 0m0.236s

with patches applied, using 4k backed memory:

$ time for i in {1..1000}; do /bin/echo > /dev/null; done

real 0m4.446s
user 0m0.380s
sys 0m0.644s

Alexander Graf (6):
KVM: PPC: E500: Move write_stlbe higher
KVM: PPC: E500: Explicitly mark shadow maps invalid
KVM: PPC: E500: Propagate errors when shadow mapping
KVM: PPC: e500: Call kvmppc_mmu_map for initial mapping
KVM: PPC: E500: Split host and guest MMU parts
KVM: PPC: e500: Implement TLB1-in-TLB0 mapping

 arch/powerpc/kvm/Makefile        |    9 +-
 arch/powerpc/kvm/e500.h          |    1 +
 arch/powerpc/kvm/e500_mmu.c      |  811 +++++++++++++++++++++
 arch/powerpc/kvm/e500_mmu_host.c |  699 +++++++++++++++++++
 arch/powerpc/kvm/e500_mmu_host.h |   20 +
 arch/powerpc/kvm/e500_tlb.c      | 1430 --------------------------------------
 6 files changed, 1537 insertions(+), 1433 deletions(-)
 create mode 100644 arch/powerpc/kvm/e500_mmu.c
 create mode 100644 arch/powerpc/kvm/e500_mmu_host.c
 create mode 100644 arch/powerpc/kvm/e500_mmu_host.h
 delete mode 100644 arch/powerpc/kvm/e500_tlb.c
#1 lore raw

PATCH v2 1/6 KVM: PPC: E500: Move write_stlbe higher

Later patches want to call the function and it doesn't have dependencies on anything below write_host_tlbe.

Move it higher up in the file.

---
 arch/powerpc/kvm/e500_tlb.c |   32 ++++++++++++++++----------------
 1 files changed, 16 insertions(+), 16 deletions(-)
arch/powerpc/kvm/e500_tlb.c +16 -16
index cf3f180..d38ad63 100644
@@ -156,6 +156,22 @@ static inline void write_host_tlbe(struct kvmppc_vcpu_e500 *vcpu_e500,
 	}
 }
 
+/* sesel is for tlb1 only */
+static void write_stlbe(struct kvmppc_vcpu_e500 *vcpu_e500,
+			struct kvm_book3e_206_tlb_entry *gtlbe,
+			struct kvm_book3e_206_tlb_entry *stlbe,
+			int stlbsel, int sesel)
+{
+	int stid;
+
+	preempt_disable();
+	stid = kvmppc_e500_get_tlb_stid(&vcpu_e500->vcpu, gtlbe);
+
+	stlbe->mas1 |= MAS1_TID(stid);
+	write_host_tlbe(vcpu_e500, stlbsel, sesel, stlbe);
+	preempt_enable();
+}
+
 #ifdef CONFIG_KVM_E500V2
 void kvmppc_map_magic(struct kvm_vcpu *vcpu)
 {
@@ -834,22 +850,6 @@ int kvmppc_e500_emul_tlbsx(struct kvm_vcpu *vcpu, gva_t ea)
 	return EMULATE_DONE;
 }
 
-/* sesel is for tlb1 only */
-static void write_stlbe(struct kvmppc_vcpu_e500 *vcpu_e500,
-			struct kvm_book3e_206_tlb_entry *gtlbe,
-			struct kvm_book3e_206_tlb_entry *stlbe,
-			int stlbsel, int sesel)
-{
-	int stid;
-
-	preempt_disable();
-	stid = kvmppc_e500_get_tlb_stid(&vcpu_e500->vcpu, gtlbe);
-
-	stlbe->mas1 |= MAS1_TID(stid);
-	write_host_tlbe(vcpu_e500, stlbsel, sesel, stlbe);
-	preempt_enable();
-}
-
 int kvmppc_e500_emul_tlbwe(struct kvm_vcpu *vcpu)
 {
 	struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
Signature · 1 line
1.6.0.2
#2 lore raw

PATCH v2 2/6 KVM: PPC: E500: Explicitly mark shadow maps invalid

In reply to Alexander Graf

When we invalidate shadow TLB maps on the host, we don't mark them as not valid. But we should.

Fix this by removing the E500_TLB_VALID from their flags when invalidating.

---
 arch/powerpc/kvm/e500_tlb.c |   13 ++++++++++---
 1 files changed, 10 insertions(+), 3 deletions(-)
arch/powerpc/kvm/e500_tlb.c +10 -3
index d38ad63..8efb2ac 100644
@@ -204,9 +204,13 @@ static void inval_gtlbe_on_host(struct kvmppc_vcpu_e500 *vcpu_e500,
 {
 	struct kvm_book3e_206_tlb_entry *gtlbe =
 		get_entry(vcpu_e500, tlbsel, esel);
+	struct tlbe_ref *ref = &vcpu_e500->gtlb_priv[tlbsel][esel].ref;
 
-	if (tlbsel == 1 &&
-	    vcpu_e500->gtlb_priv[1][esel].ref.flags & E500_TLB_BITMAP) {
+	/* Don't bother with unmapped entries */
+	if (!(ref->flags & E500_TLB_VALID))
+		return;
+
+	if (tlbsel == 1 && ref->flags & E500_TLB_BITMAP) {
 		u64 tmp = vcpu_e500->g2h_tlb1_map[esel];
 		int hw_tlb_indx;
 		unsigned long flags;
@@ -224,7 +228,7 @@ static void inval_gtlbe_on_host(struct kvmppc_vcpu_e500 *vcpu_e500,
 		}
 		mb();
 		vcpu_e500->g2h_tlb1_map[esel] = 0;
-		vcpu_e500->gtlb_priv[1][esel].ref.flags &= ~E500_TLB_BITMAP;
+		ref->flags &= ~(E500_TLB_BITMAP | E500_TLB_VALID);
 		local_irq_restore(flags);
 
 		return;
@@ -232,6 +236,9 @@ static void inval_gtlbe_on_host(struct kvmppc_vcpu_e500 *vcpu_e500,
 
 	/* Guest tlbe is backed by at most one host tlbe per shadow pid. */
 	kvmppc_e500_tlbil_one(vcpu_e500, gtlbe);
+
+	/* Mark the TLB as not backed by the host anymore */
+	ref->flags &= ~E500_TLB_VALID;
 }
 
 static int tlb0_set_base(gva_t addr, int sets, int ways)
Signature · 1 line
1.6.0.2
#6 lore raw

PATCH v2 3/6 KVM: PPC: E500: Propagate errors when shadow mapping

In reply to Alexander Graf

When shadow mapping a page, mapping this page can fail. In that case we don't have a shadow map.

Take this case into account, otherwise we might end up writing bogus TLB entries into the host TLB.

While at it, also move the write_stlbe() calls into the respective TLBn handlers.

---
 arch/powerpc/kvm/e500_tlb.c |   69 +++++++++++++++++++++++++-----------------
 1 files changed, 41 insertions(+), 28 deletions(-)
arch/powerpc/kvm/e500_tlb.c +41 -28
index 8efb2ac..3777167 100644
@@ -432,7 +432,7 @@ static inline void kvmppc_e500_setup_stlbe(
 #endif
 }
 
-static inline void kvmppc_e500_shadow_map(struct kvmppc_vcpu_e500 *vcpu_e500,
+static inline int kvmppc_e500_shadow_map(struct kvmppc_vcpu_e500 *vcpu_e500,
 	u64 gvaddr, gfn_t gfn, struct kvm_book3e_206_tlb_entry *gtlbe,
 	int tlbsel, struct kvm_book3e_206_tlb_entry *stlbe,
 	struct tlbe_ref *ref)
@@ -551,7 +551,7 @@ static inline void kvmppc_e500_shadow_map(struct kvmppc_vcpu_e500 *vcpu_e500,
 		if (is_error_noslot_pfn(pfn)) {
 			printk(KERN_ERR "Couldn't get real page for gfn %lx!\n",
 					(long)gfn);
-			return;
+			return -EINVAL;
 		}
 
 		/* Align guest and physical address to page map boundaries */
@@ -571,22 +571,33 @@ static inline void kvmppc_e500_shadow_map(struct kvmppc_vcpu_e500 *vcpu_e500,
 
 	/* Drop refcount on page, so that mmu notifiers can clear it */
 	kvm_release_pfn_clean(pfn);
+
+	return 0;
 }
 
 /* XXX only map the one-one case, for now use TLB0 */
-static void kvmppc_e500_tlb0_map(struct kvmppc_vcpu_e500 *vcpu_e500,
-				 int esel,
-				 struct kvm_book3e_206_tlb_entry *stlbe)
+static int kvmppc_e500_tlb0_map(struct kvmppc_vcpu_e500 *vcpu_e500,
+				int esel,
+				struct kvm_book3e_206_tlb_entry *stlbe)
 {
 	struct kvm_book3e_206_tlb_entry *gtlbe;
 	struct tlbe_ref *ref;
+	int stlbsel = 0;
+	int sesel = 0;
+	int r;
 
 	gtlbe = get_entry(vcpu_e500, 0, esel);
 	ref = &vcpu_e500->gtlb_priv[0][esel].ref;
 
-	kvmppc_e500_shadow_map(vcpu_e500, get_tlb_eaddr(gtlbe),
+	r = kvmppc_e500_shadow_map(vcpu_e500, get_tlb_eaddr(gtlbe),
 			get_tlb_raddr(gtlbe) >> PAGE_SHIFT,
 			gtlbe, 0, stlbe, ref);
+	if (r)
+		return r;
+
+	write_stlbe(vcpu_e500, gtlbe, stlbe, stlbsel, sesel);
+
+	return 0;
 }
 
 /* Caller must ensure that the specified guest TLB entry is safe to insert into
@@ -597,25 +608,32 @@ static int kvmppc_e500_tlb1_map(struct kvmppc_vcpu_e500 *vcpu_e500,
 		struct kvm_book3e_206_tlb_entry *stlbe, int esel)
 {
 	struct tlbe_ref *ref;
-	unsigned int victim;
+	unsigned int sesel;
+	int r;
+	int stlbsel = 1;
 
-	victim = vcpu_e500->host_tlb1_nv++;
+	sesel = vcpu_e500->host_tlb1_nv++;
 
 	if (unlikely(vcpu_e500->host_tlb1_nv >= tlb1_max_shadow_size()))
 		vcpu_e500->host_tlb1_nv = 0;
 
-	ref = &vcpu_e500->tlb_refs[1][victim];
-	kvmppc_e500_shadow_map(vcpu_e500, gvaddr, gfn, gtlbe, 1, stlbe, ref);
+	ref = &vcpu_e500->tlb_refs[1][sesel];
+	r = kvmppc_e500_shadow_map(vcpu_e500, gvaddr, gfn, gtlbe, 1, stlbe,
+				   ref);
+	if (r)
+		return r;
 
-	vcpu_e500->g2h_tlb1_map[esel] |= (u64)1 << victim;
+	vcpu_e500->g2h_tlb1_map[esel] |= (u64)1 << sesel;
 	vcpu_e500->gtlb_priv[1][esel].ref.flags |= E500_TLB_BITMAP;
-	if (vcpu_e500->h2g_tlb1_rmap[victim]) {
-		unsigned int idx = vcpu_e500->h2g_tlb1_rmap[victim];
-		vcpu_e500->g2h_tlb1_map[idx] &= ~(1ULL << victim);
+	if (vcpu_e500->h2g_tlb1_rmap[sesel]) {
+		unsigned int idx = vcpu_e500->h2g_tlb1_rmap[sesel];
+		vcpu_e500->g2h_tlb1_map[idx] &= ~(1ULL << sesel);
 	}
-	vcpu_e500->h2g_tlb1_rmap[victim] = esel;
+	vcpu_e500->h2g_tlb1_rmap[sesel] = esel;
 
-	return victim;
+	write_stlbe(vcpu_e500, gtlbe, stlbe, stlbsel, sesel);
+
+	return 0;
 }
 
 static void kvmppc_recalc_tlb1map_range(struct kvmppc_vcpu_e500 *vcpu_e500)
@@ -1034,30 +1052,27 @@ void kvmppc_mmu_map(struct kvm_vcpu *vcpu, u64 eaddr, gpa_t gpaddr,
 	struct kvm_book3e_206_tlb_entry *gtlbe, stlbe;
 	int tlbsel = tlbsel_of(index);
 	int esel = esel_of(index);
-	int stlbsel, sesel;
 
 	gtlbe = get_entry(vcpu_e500, tlbsel, esel);
 
 	switch (tlbsel) {
 	case 0:
-		stlbsel = 0;
-		sesel = 0; /* unused */
 		priv = &vcpu_e500->gtlb_priv[tlbsel][esel];
 
-		/* Only triggers after clear_tlb_refs */
-		if (unlikely(!(priv->ref.flags & E500_TLB_VALID)))
+		/* Triggers after clear_tlb_refs or on initial mapping */
+		if (!(priv->ref.flags & E500_TLB_VALID)) {
 			kvmppc_e500_tlb0_map(vcpu_e500, esel, &stlbe);
-		else
+		} else {
 			kvmppc_e500_setup_stlbe(vcpu, gtlbe, BOOK3E_PAGESZ_4K,
 						&priv->ref, eaddr, &stlbe);
+			write_stlbe(vcpu_e500, gtlbe, &stlbe, 0, 0);
+		}
 		break;
 
 	case 1: {
 		gfn_t gfn = gpaddr >> PAGE_SHIFT;
-
-		stlbsel = 1;
-		sesel = kvmppc_e500_tlb1_map(vcpu_e500, eaddr, gfn,
-					     gtlbe, &stlbe, esel);
+		kvmppc_e500_tlb1_map(vcpu_e500, eaddr, gfn, gtlbe, &stlbe,
+				     esel);
 		break;
 	}
 
@@ -1065,8 +1080,6 @@ void kvmppc_mmu_map(struct kvm_vcpu *vcpu, u64 eaddr, gpa_t gpaddr,
 		BUG();
 		break;
 	}
-
-	write_stlbe(vcpu_e500, gtlbe, &stlbe, stlbsel, sesel);
 }
 
 /************* MMU Notifiers *************/
Signature · 1 line
1.6.0.2
#7 lore raw

PATCH v2 4/6 KVM: PPC: e500: Call kvmppc_mmu_map for initial mapping

In reply to Alexander Graf

When emulating tlbwe, we want to automatically map the entry that just got written in our shadow TLB map, because chances are quite high that it's going to be used very soon.

Today this happens explicitly, duplicating all the logic that is in kvmppc_mmu_map() already. Just call that one instead.

---

v1 -> v2:

- leave stlbe uninitialized
- remove force init logic, it's handled by the explicit invalidate now

---
 arch/powerpc/kvm/e500_tlb.c |   38 +++++++-------------------------------
 1 files changed, 7 insertions(+), 31 deletions(-)
arch/powerpc/kvm/e500_tlb.c +7 -31
index 3777167..48d1a4f 100644
@@ -878,8 +878,8 @@ int kvmppc_e500_emul_tlbsx(struct kvm_vcpu *vcpu, gva_t ea)
 int kvmppc_e500_emul_tlbwe(struct kvm_vcpu *vcpu)
 {
 	struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
-	struct kvm_book3e_206_tlb_entry *gtlbe, stlbe;
-	int tlbsel, esel, stlbsel, sesel;
+	struct kvm_book3e_206_tlb_entry *gtlbe;
+	int tlbsel, esel;
 	int recal = 0;
 
 	tlbsel = get_tlb_tlbsel(vcpu);
@@ -917,40 +917,16 @@ int kvmppc_e500_emul_tlbwe(struct kvm_vcpu *vcpu)
 
 	/* Invalidate shadow mappings for the about-to-be-clobbered TLBE. */
 	if (tlbe_is_host_safe(vcpu, gtlbe)) {
-		u64 eaddr;
-		u64 raddr;
+		u64 eaddr = get_tlb_eaddr(gtlbe);
+		u64 raddr = get_tlb_raddr(gtlbe);
 
-		switch (tlbsel) {
-		case 0:
-			/* TLB0 */
+		if (tlbsel = 0) {
 			gtlbe->mas1 &= ~MAS1_TSIZE(~0);
 			gtlbe->mas1 |= MAS1_TSIZE(BOOK3E_PAGESZ_4K);
-
-			stlbsel = 0;
-			kvmppc_e500_tlb0_map(vcpu_e500, esel, &stlbe);
-			sesel = 0; /* unused */
-
-			break;
-
-		case 1:
-			/* TLB1 */
-			eaddr = get_tlb_eaddr(gtlbe);
-			raddr = get_tlb_raddr(gtlbe);
-
-			/* Create a 4KB mapping on the host.
-			 * If the guest wanted a large page,
-			 * only the first 4KB is mapped here and the rest
-			 * are mapped on the fly. */
-			stlbsel = 1;
-			sesel = kvmppc_e500_tlb1_map(vcpu_e500, eaddr,
-				    raddr >> PAGE_SHIFT, gtlbe, &stlbe, esel);
-			break;
-
-		default:
-			BUG();
 		}
 
-		write_stlbe(vcpu_e500, gtlbe, &stlbe, stlbsel, sesel);
+		/* Premap the faulting page */
+		kvmppc_mmu_map(vcpu, eaddr, raddr, index_of(tlbsel, esel));
 	}
 
 	kvmppc_set_exit_type(vcpu, EMULATED_TLBWE_EXITS);
Signature · 1 line
1.6.0.2
#8 lore raw

PATCH v2 5/6 KVM: PPC: E500: Split host and guest MMU parts

In reply to Alexander Graf

This patch splits the file e500_tlb.c into e500_mmu.c (guest TLB handling) and e500_mmu_host.c (host TLB handling).

The main benefit of this split is readability and maintainability. It's just a lot harder to write dirty code :).

---

v1 -> v2:

- rebase

---
 arch/powerpc/kvm/Makefile        |    9 +-
 arch/powerpc/kvm/e500_mmu.c      |  811 ++++++++++++++++++++++
 arch/powerpc/kvm/e500_mmu_host.c |  672 ++++++++++++++++++
 arch/powerpc/kvm/e500_mmu_host.h |   20 +
 arch/powerpc/kvm/e500_tlb.c      | 1426 --------------------------------------
 5 files changed, 1509 insertions(+), 1429 deletions(-)
 create mode 100644 arch/powerpc/kvm/e500_mmu.c
 create mode 100644 arch/powerpc/kvm/e500_mmu_host.c
 create mode 100644 arch/powerpc/kvm/e500_mmu_host.h
 delete mode 100644 arch/powerpc/kvm/e500_tlb.c
arch/powerpc/kvm/Makefile +6 -3
index 1e473d4..b772ede 100644
@@ -10,7 +10,8 @@ common-objs-y = $(addprefix ../../../virt/kvm/, kvm_main.o coalesced_mmio.o \
 						eventfd.o)
 
 CFLAGS_44x_tlb.o  := -I.
-CFLAGS_e500_tlb.o := -I.
+CFLAGS_e500_mmu.o := -I.
+CFLAGS_e500_mmu_host.o := -I.
 CFLAGS_emulate.o  := -I.
 
 common-objs-y += powerpc.o emulate.o
@@ -35,7 +36,8 @@ kvm-e500-objs := \
 	booke_emulate.o \
 	booke_interrupts.o \
 	e500.o \
-	e500_tlb.o \
+	e500_mmu.o \
+	e500_mmu_host.o \
 	e500_emulate.o
 kvm-objs-$(CONFIG_KVM_E500V2) := $(kvm-e500-objs)
 
@@ -45,7 +47,8 @@ kvm-e500mc-objs := \
 	booke_emulate.o \
 	bookehv_interrupts.o \
 	e500mc.o \
-	e500_tlb.o \
+	e500_mmu.o \
+	e500_mmu_host.o \
 	e500_emulate.o
 kvm-objs-$(CONFIG_KVM_E500MC) := $(kvm-e500mc-objs)
 
arch/powerpc/kvm/e500_mmu.c new file +811 -0

811 lines not on this page · raw message

arch/powerpc/kvm/e500_mmu_host.c new file +672 -0

672 lines not on this page · raw message

arch/powerpc/kvm/e500_mmu_host.h new file +20 -0
new file mode 100644
index 0000000..9e4d4a2
@@ -0,0 +1,20 @@
+/*
+ * Copyright (C) 2008-2013 Freescale Semiconductor, Inc. All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License, version 2, as
+ * published by the Free Software Foundation.
+ */
+
+#ifndef KVM_E500_MMU_HOST_H
+#define KVM_E500_MMU_HOST_H
+
+void inval_gtlbe_on_host(struct kvmppc_vcpu_e500 *vcpu_e500, int tlbsel,
+			 int esel);
+
+void clear_tlb1_bitmap(struct kvmppc_vcpu_e500 *vcpu_e500);
+void clear_tlb_refs(struct kvmppc_vcpu_e500 *vcpu_e500);
+int e500_mmu_host_init(struct kvmppc_vcpu_e500 *vcpu_e500);
+void e500_mmu_host_uninit(struct kvmppc_vcpu_e500 *vcpu_e500);
+
+#endif /* KVM_E500_MMU_HOST_H */
arch/powerpc/kvm/e500_tlb.c deleted +0 -1426

1,426 lines not on this page · raw message

Signature · 1 line
1.6.0.2
#9 lore raw

PATCH v2 6/6 KVM: PPC: e500: Implement TLB1-in-TLB0 mapping

In reply to Alexander Graf

When a host mapping fault happens in a guest TLB1 entry today, we map the translated guest entry into the host's TLB1.

This isn't particularly clever when the guest is mapped by normal 4k pages, since these would be a lot better to put into TLB0 instead.

This patch adds the required logic to map 4k TLB1 shadow maps into the host's TLB0.

---

v1 -> v2:

- rebase
- allow mixing of TLB0 and TLB1_BITMAP backing of TLB1 pages

---
 arch/powerpc/kvm/e500.h          |    1 +
 arch/powerpc/kvm/e500_mmu_host.c |   65 +++++++++++++++++++++++++++-----------
 2 files changed, 47 insertions(+), 19 deletions(-)
arch/powerpc/kvm/e500.h +1 -0
index c70d37e..41cefd4 100644
@@ -28,6 +28,7 @@
 
 #define E500_TLB_VALID 1
 #define E500_TLB_BITMAP 2
+#define E500_TLB_TLB0		(1 << 2)
 
 struct tlbe_ref {
 	pfn_t pfn;
arch/powerpc/kvm/e500_mmu_host.c +46 -19

94 lines not on this page · raw message

Signature · 1 line
1.6.0.2
#3 lore raw

Re: [PATCH 2/6] KVM: PPC: E500: Explicitly mark shadow maps invalid

In reply to Alexander Graf
Quoted 48 lines · Alexander Graf

When we invalidate shadow TLB maps on the host, we don't mark them as not valid. But we should.

Fix this by removing the E500_TLB_VALID from their flags when invalidating.

---
 arch/powerpc/kvm/e500_tlb.c |   13 ++++++++++---
 1 files changed, 10 insertions(+), 3 deletions(-)
arch/powerpc/kvm/e500_tlb.c +6 -2
index d38ad63..8efb2ac 100644
@@ -204,9 +204,13 @@ static void inval_gtlbe_on_host(struct  
 kvmppc_vcpu_e500 *vcpu_e500,
 {
 	struct kvm_book3e_206_tlb_entry *gtlbe =
 		get_entry(vcpu_e500, tlbsel, esel);
+	struct tlbe_ref *ref = &vcpu_e500->gtlb_priv[tlbsel][esel].ref;
 
-	if (tlbsel == 1 &&
-	    vcpu_e500->gtlb_priv[1][esel].ref.flags & E500_TLB_BITMAP) {
+	/* Don't bother with unmapped entries */
+	if (!(ref->flags & E500_TLB_VALID))
+		return;
+
+	if (tlbsel == 1 && ref->flags & E500_TLB_BITMAP) {
 		u64 tmp = vcpu_e500->g2h_tlb1_map[esel];
 		int hw_tlb_indx;
arch/powerpc/kvm/e500_tlb.c +1 -1
 		unsigned long flags;
@@ -224,7 +228,7 @@ static void inval_gtlbe_on_host(struct  
 kvmppc_vcpu_e500 *vcpu_e500,
 		}
 		mb();
 		vcpu_e500->g2h_tlb1_map[esel] = 0;
-		vcpu_e500->gtlb_priv[1][esel].ref.flags &=  
 ~E500_TLB_BITMAP;
+		ref->flags &= ~(E500_TLB_BITMAP | E500_TLB_VALID);
 		local_irq_restore(flags);
arch/powerpc/kvm/e500_tlb.c +0 -0
 		return;
@@ -232,6 +236,9 @@ static void inval_gtlbe_on_host(struct  
 kvmppc_vcpu_e500 *vcpu_e500,
 
 	/* Guest tlbe is backed by at most one host tlbe per shadow  

pid. */

arch/powerpc/kvm/e500_tlb.c static void inval_gtlbe_on_host(struct kvmppc_vcpu_e500 *vcpu_e500, · line 242

Quoted 5 lines
arch/powerpc/kvm/e500_tlb.c +3 -0
 	kvmppc_e500_tlbil_one(vcpu_e500, gtlbe);
+
+	/* Mark the TLB as not backed by the host anymore */
+	ref->flags &= ~E500_TLB_VALID;
 }

Invalidation paths that call kvmppc_e500_tlbil_all(), such as MMUCSR0 and tlbivax, need a call to clear_tlb_refs() in order to get the valid bits cleared.

In looking this up, I also saw that tlbilxlpid (T=0) seems to be broken; it compares PID/TID as if it were T=1. Don't be fooled by the "lpid" in the name; it's still relevant (and different from T=1) in the absence of E.HV, and should be treated as "tlbilx all". Once implemented, that will also presumably use kvmppc_e500_tlbil_all().

-Scott

#4 lore raw

Re: [PATCH 2/6] KVM: PPC: E500: Explicitly mark shadow maps invalid

Quoted 37 lines · Scott Wood
Quoted 36 lines · Alexander Graf

When we invalidate shadow TLB maps on the host, we don't mark them as not valid. But we should.
Fix this by removing the E500_TLB_VALID from their flags when invalidating.

---
arch/powerpc/kvm/e500_tlb.c | 13 ++++++++++---

1 files changed, 10 insertions(+), 3 deletions(-)
arch/powerpc/kvm/e500_tlb.c +7 -3
index d38ad63..8efb2ac 100644
@@ -204,9 +204,13 @@ static void inval_gtlbe_on_host(struct kvmppc_vcpu_e500 *vcpu_e500,
 {
 	struct kvm_book3e_206_tlb_entry *gtlbe =
 		get_entry(vcpu_e500, tlbsel, esel);
+	struct tlbe_ref *ref = &vcpu_e500->gtlb_priv[tlbsel][esel].ref;
-	if (tlbsel == 1 &&
-	    vcpu_e500->gtlb_priv[1][esel].ref.flags & E500_TLB_BITMAP) {
+	/* Don't bother with unmapped entries */
+	if (!(ref->flags & E500_TLB_VALID))
+		return;
+
+	if (tlbsel == 1 && ref->flags & E500_TLB_BITMAP) {
 		u64 tmp = vcpu_e500->g2h_tlb1_map[esel];
 		int hw_tlb_indx;
 		unsigned long flags;
@@ -224,7 +228,7 @@ static void inval_gtlbe_on_host(struct kvmppc_vcpu_e500 *vcpu_e500,
 		}
 		mb();
 		vcpu_e500->g2h_tlb1_map[esel] = 0;
-		vcpu_e500->gtlb_priv[1][esel].ref.flags &= ~E500_TLB_BITMAP;
+		ref->flags &= ~(E500_TLB_BITMAP | E500_TLB_VALID);
 		local_irq_restore(flags);
 		return;
@@ -232,6 +236,9 @@ static void inval_gtlbe_on_host(struct kvmppc_vcpu_e500 *vcpu_e500,
Quoted 8 lines

arch/powerpc/kvm/e500_tlb.c static void inval_gtlbe_on_host(struct kvmppc_vcpu_e500 *vcpu_e500, · line 242

Quoted 6 lines
arch/powerpc/kvm/e500_tlb.c +3 -0
 	/* Guest tlbe is backed by at most one host tlbe per shadow pid. */
 	kvmppc_e500_tlbil_one(vcpu_e500, gtlbe);
+
+	/* Mark the TLB as not backed by the host anymore */
+	ref->flags &= ~E500_TLB_VALID;
 }

Invalidation paths that call kvmppc_e500_tlbil_all(), such as MMUCSR0 and tlbivax, need a call to clear_tlb_refs() in order to get the valid bits cleared.

Yeah, instead of kvmppc_e500_tlbil_all(). This might need a bit of rework, since we end up flushing the full host TLB / cache even when only evicting a single page at times.

So one more question I have here is why kvmppc_core_flush_tlb() calls clear_tlb1_bitmap(). That function looks like a better candidate for "flush all of the pending host translation" than clear_tlb_refs() which to me sounds more like an implementation detail of the host TLB implementation.

After clear_tlb_refs() there shouldn't be any pending bitmaps lying around, right?

Quoted 1 line

In looking this up, I also saw that tlbilxlpid (T=0) seems to be broken; it compares PID/TID as if it were T=1. Don't be fooled by the "lpid" in the name; it's still relevant (and different from T=1) in the absence of E.HV, and should be treated as "tlbilx all". Once implemented, that will also presumably use kvmppc_e500_tlbil_all().

Yay :). That's a follow-up patch though. Let me put it on the todo list.

Alex

#5 lore raw

Re: [PATCH 2/6] KVM: PPC: E500: Explicitly mark shadow maps invalid

Quoted 7 lines · Alexander Graf
Quoted 1 line · Scott Wood

Invalidation paths that call kvmppc_e500_tlbil_all(), such as

MMUCSR0 and tlbivax, need a call to clear_tlb_refs() in order to get the valid bits cleared.

Yeah, instead of kvmppc_e500_tlbil_all(). This might need a bit of rework, since we end up flushing the full host TLB / cache even when only evicting a single page at times.

I assume you're talking about the tlbivax single-page case. That could certainly be improved, but I'd consider it a low priority until we have a guest that actually uses it (Linux does tlbsx+tlbwe to locally invalidate single pages on e500v2).

Quoted 2 lines

So one more question I have here is why kvmppc_core_flush_tlb() calls clear_tlb1_bitmap().
That function looks like a better candidate for "flush all of the pending host translation" than clear_tlb_refs() which to me sounds more like an implementation detail of the host TLB implementation.

I don't quite follow. The implementation of kvmppc_core_flush_tlb() is also an implementation detail of the host TLB implementation.

Quoted 1 line

After clear_tlb_refs() there shouldn't be any pending bitmaps lying around, right?

clear_tlb_refs() doesn't clear the bitmap. It does a bulk invalidation rather than calling inval_gtlbe_on_host(). Perhaps we should move the clear_tlb1_bitmap() call into clear_tlb_refs() and rename the latter to kvmppc_core_flush_tlb(). The dirty_tlb ioctl calls clear_tlb_refs() without clearing the bitmap, but that's probably a bug.

Quoted 4 lines
Quoted 1 line

In looking this up, I also saw that tlbilxlpid (T=0) seems to be

broken; it compares PID/TID as if it were T=1. Don't be fooled by the "lpid" in the name; it's still relevant (and different from T=1) in the absence of E.HV, and should be treated as "tlbilx all". Once implemented, that will also presumably use kvmppc_e500_tlbil_all().

Yay :). That's a follow-up patch though. Let me put it on the todo list.

Never mind, I wasn't reading the code closely enough. T=0 will use inval_gtlbe_on_host() for all entries, though kvmppc_e500_tlbil_all() would be more efficient.

-Scott