Skip to contentSkip to conversation

kvm/ppc/e500: TLB bugfixes

PATCH Applied Scott Wood 2 participants 7 messages Feb 14, 2013 – Mar 8, 2013

Subsystems (3)
lore
Share & tools
lore

Scott Wood

author
#0 lore raw

cover kvm/ppc/e500: TLB bugfixes

Scott Wood (3):
kvm/ppc/e500: h2g_tlb1_rmap: esel 0 is valid kvm/ppc/e500: g2h_tlb1_map: clear old bit before setting new bit kvm/ppc/e500: eliminate tlb_refs

 arch/powerpc/kvm/e500.h          |   24 ++++-------
 arch/powerpc/kvm/e500_mmu_host.c |   85 +++++++++++++-------------------------
 2 files changed, 37 insertions(+), 72 deletions(-)
Signature · 1 line
1.7.9.5
author
#1 lore raw

PATCH 1/3 kvm/ppc/e500: h2g_tlb1_rmap: esel 0 is valid

Add one to esel values in h2g_tlb1_rmap, so that "no mapping" can be distinguished from "esel 0". Note that we're not saved by the fact that host esel 0 is reserved for non-KVM use, because KVM host esel numbering is not the raw host numbering (see to_htlb1_esel).

---
 arch/powerpc/kvm/e500_mmu_host.c |    4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
arch/powerpc/kvm/e500_mmu_host.c +2 -2
index a222edf..35fb80e 100644
@@ -511,10 +511,10 @@ static int kvmppc_e500_tlb1_map_tlb1(struct kvmppc_vcpu_e500 *vcpu_e500,
 	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[sesel]) {
-		unsigned int idx = vcpu_e500->h2g_tlb1_rmap[sesel];
+		unsigned int idx = vcpu_e500->h2g_tlb1_rmap[sesel] - 1;
 		vcpu_e500->g2h_tlb1_map[idx] &= ~(1ULL << sesel);
 	}
-	vcpu_e500->h2g_tlb1_rmap[sesel] = esel;
+	vcpu_e500->h2g_tlb1_rmap[sesel] = esel + 1;
 
 	return sesel;
 }
Signature · 1 line
1.7.9.5
author
#2 lore raw

PATCH 2/3 kvm/ppc/e500: g2h_tlb1_map: clear old bit before setting new bit

In reply to Scott Wood

It's possible that we're using the same host TLB1 slot to map (a presumably different portion of) the same guest TLB1 entry. Clear the bit in the map before setting it, so that if the esels are the same the bit will remain set.

---
 arch/powerpc/kvm/e500_mmu_host.c |    7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
arch/powerpc/kvm/e500_mmu_host.c +4 -3
index 35fb80e..8e72b21 100644
@@ -507,13 +507,14 @@ static int kvmppc_e500_tlb1_map_tlb1(struct kvmppc_vcpu_e500 *vcpu_e500,
 	if (unlikely(vcpu_e500->host_tlb1_nv >= tlb1_max_shadow_size()))
 		vcpu_e500->host_tlb1_nv = 0;
 
-	vcpu_e500->tlb_refs[1][sesel] = *ref;
-	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[sesel]) {
 		unsigned int idx = vcpu_e500->h2g_tlb1_rmap[sesel] - 1;
 		vcpu_e500->g2h_tlb1_map[idx] &= ~(1ULL << sesel);
 	}
+
+	vcpu_e500->tlb_refs[1][sesel] = *ref;
+	vcpu_e500->gtlb_priv[1][esel].ref.flags |= E500_TLB_BITMAP;
+	vcpu_e500->g2h_tlb1_map[esel] |= (u64)1 << sesel;
 	vcpu_e500->h2g_tlb1_rmap[sesel] = esel + 1;
 
 	return sesel;
Signature · 1 line
1.7.9.5
author
#3 lore raw

PATCH 3/3 kvm/ppc/e500: eliminate tlb_refs

In reply to Scott Wood

Commit 523f0e5421c12610527c620b983b443f329e3a32 ("KVM: PPC: E500:
Explicitly mark shadow maps invalid") began using E500_TLB_VALID for guest TLB1 entries, and skipping invalidations if it's not set.

However, when E500_TLB_VALID was set for such entries, it was on a fake local ref, and so the invalidations never happen. gtlb_privs is documented as being only for guest TLB0, though we already violate that with E500_TLB_BITMAP.

Now that we have MMU notifiers, and thus don't need to actually retain a reference to the mapped pages, get rid of tlb_refs, and use gtlb_privs for E500_TLB_VALID in TLB1.

Since we can have more than one host TLB entry for a given tlbe_ref, be careful not to clear existing flags that are relevant to other host TLB entries when preparing a new host TLB entry.

---
 arch/powerpc/kvm/e500.h          |   24 ++++--------
 arch/powerpc/kvm/e500_mmu_host.c |   78 ++++++++++++--------------------------
 2 files changed, 33 insertions(+), 69 deletions(-)
arch/powerpc/kvm/e500.h +8 -16
index 41cefd4..33db48a 100644
@@ -26,17 +26,20 @@
 #define E500_PID_NUM   3
 #define E500_TLB_NUM   2
 
-#define E500_TLB_VALID 1
-#define E500_TLB_BITMAP 2
+/* entry is mapped somewhere in host TLB */
+#define E500_TLB_VALID		(1 << 0)
+/* TLB1 entry is mapped by host TLB1, tracked by bitmaps */
+#define E500_TLB_BITMAP		(1 << 1)
+/* TLB1 entry is mapped by host TLB0 */
 #define E500_TLB_TLB0		(1 << 2)
 
 struct tlbe_ref {
-	pfn_t pfn;
-	unsigned int flags; /* E500_TLB_* */
+	pfn_t pfn;		/* valid only for TLB0, except briefly */
+	unsigned int flags;	/* E500_TLB_* */
 };
 
 struct tlbe_priv {
-	struct tlbe_ref ref; /* TLB0 only -- TLB1 uses tlb_refs */
+	struct tlbe_ref ref;
 };
 
 #ifdef CONFIG_KVM_E500V2
@@ -63,17 +66,6 @@ struct kvmppc_vcpu_e500 {
 
 	unsigned int gtlb_nv[E500_TLB_NUM];
 
-	/*
-	 * information associated with each host TLB entry --
-	 * TLB1 only for now.  If/when guest TLB1 entries can be
-	 * mapped with host TLB0, this will be used for that too.
-	 *
-	 * We don't want to use this for guest TLB0 because then we'd
-	 * have the overhead of doing the translation again even if
-	 * the entry is still in the guest TLB (e.g. we swapped out
-	 * and back, and our host TLB entries got evicted).
-	 */
-	struct tlbe_ref *tlb_refs[E500_TLB_NUM];
 	unsigned int host_tlb1_nv;
 
 	u32 svr;
arch/powerpc/kvm/e500_mmu_host.c +25 -53

167 lines not on this page · raw message

Signature · 1 line
1.7.9.5
author
#4 lore raw

PATCH v2 3/3 kvm/ppc/e500: eliminate tlb_refs

Commit 523f0e5421c12610527c620b983b443f329e3a32 ("KVM: PPC: E500:
Explicitly mark shadow maps invalid") began using E500_TLB_VALID for guest TLB1 entries, and skipping invalidations if it's not set.

However, when E500_TLB_VALID was set for such entries, it was on a fake local ref, and so the invalidations never happen. gtlb_privs is documented as being only for guest TLB0, though we already violate that with E500_TLB_BITMAP.

Now that we have MMU notifiers, and thus don't need to actually retain a reference to the mapped pages, get rid of tlb_refs, and use gtlb_privs for E500_TLB_VALID in TLB1.

Since we can have more than one host TLB entry for a given tlbe_ref, be careful not to clear existing flags that are relevant to other host TLB entries when preparing a new host TLB entry.

---
v2: Remove these (harmless, but unnecessary) lines accidentally added due to a patch shuffling accident in v1:

+ vcpu_e500->g2h_tlb1_map[esel] |= (u64)1 << sesel;
+ ref->flags |= E500_TLB_BITMAP;

 arch/powerpc/kvm/e500.h          |   24 ++++--------
 arch/powerpc/kvm/e500_mmu_host.c |   75 +++++++++++---------------------------
 2 files changed, 30 insertions(+), 69 deletions(-)
arch/powerpc/kvm/e500.h +8 -16
index 41cefd4..33db48a 100644
@@ -26,17 +26,20 @@
 #define E500_PID_NUM   3
 #define E500_TLB_NUM   2
 
-#define E500_TLB_VALID 1
-#define E500_TLB_BITMAP 2
+/* entry is mapped somewhere in host TLB */
+#define E500_TLB_VALID		(1 << 0)
+/* TLB1 entry is mapped by host TLB1, tracked by bitmaps */
+#define E500_TLB_BITMAP		(1 << 1)
+/* TLB1 entry is mapped by host TLB0 */
 #define E500_TLB_TLB0		(1 << 2)
 
 struct tlbe_ref {
-	pfn_t pfn;
-	unsigned int flags; /* E500_TLB_* */
+	pfn_t pfn;		/* valid only for TLB0, except briefly */
+	unsigned int flags;	/* E500_TLB_* */
 };
 
 struct tlbe_priv {
-	struct tlbe_ref ref; /* TLB0 only -- TLB1 uses tlb_refs */
+	struct tlbe_ref ref;
 };
 
 #ifdef CONFIG_KVM_E500V2
@@ -63,17 +66,6 @@ struct kvmppc_vcpu_e500 {
 
 	unsigned int gtlb_nv[E500_TLB_NUM];
 
-	/*
-	 * information associated with each host TLB entry --
-	 * TLB1 only for now.  If/when guest TLB1 entries can be
-	 * mapped with host TLB0, this will be used for that too.
-	 *
-	 * We don't want to use this for guest TLB0 because then we'd
-	 * have the overhead of doing the translation again even if
-	 * the entry is still in the guest TLB (e.g. we swapped out
-	 * and back, and our host TLB entries got evicted).
-	 */
-	struct tlbe_ref *tlb_refs[E500_TLB_NUM];
 	unsigned int host_tlb1_nv;
 
 	u32 svr;
arch/powerpc/kvm/e500_mmu_host.c +22 -53

159 lines not on this page · raw message

Signature · 1 line
1.7.9.5
author
#5 lore raw
In reply to Scott Wood
Quoted 9 lines · Scott Wood

Scott Wood (3):
kvm/ppc/e500: h2g_tlb1_rmap: esel 0 is valid kvm/ppc/e500: g2h_tlb1_map: clear old bit before setting new bit kvm/ppc/e500: eliminate tlb_refs

 arch/powerpc/kvm/e500.h          |   24 ++++-------
 arch/powerpc/kvm/e500_mmu_host.c |   85  
+++++++++++++-------------------------
 2 files changed, 37 insertions(+), 72 deletions(-)

Sigh, and now I'm seeing some intermittent guest userspace segfaults with hugetlbfs. :-P

-Scott

#6 lore raw
In reply to Scott Wood
Quoted 8 lines · Scott Wood

Scott Wood (3):
kvm/ppc/e500: h2g_tlb1_rmap: esel 0 is valid kvm/ppc/e500: g2h_tlb1_map: clear old bit before setting new bit kvm/ppc/e500: eliminate tlb_refs

arch/powerpc/kvm/e500.h          |   24 ++++-------
arch/powerpc/kvm/e500_mmu_host.c |   85 +++++++++++++-------------------------
2 files changed, 37 insertions(+), 72 deletions(-)

Thanks, applied all (taking v2 for 3/3) to kvm-ppc-3.9.

Alex