Skip to contentSkip to conversation

kernel status update

DISCUSSION Hollis Blanchard 2 participants 3 messages Oct 31, 2008 – Nov 7, 2008

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

I've posted most of the patches here before, but I've built up a small backlog of patches to be pushed upstream. I've been running with them long enough to be happy with them, and I've just posted them all to
http://penguinppc.org/~hollisb/kvm/patches/

There are two really minor patches I want to have acked by the PPC maintainers before I push the whole series to Avi, however. I've submitted those for review already, so hopefully that will be done by early next week.

This patch series has two main components, refactoring and optimization:
* It dramatically restructures some of the code flow and data structures to make supporting other processors possible. There are still a few rough corners though, like the TLB miss handling in booke.c kvmppc_handle_exit(). At this point I'd be OK with duplicating those handlers into core-specific files.
* Based on Christian's analysis and patches, there is some performance optimization for Book E and 440 code.
* Yu's idea about TLB handling was great, and I saw about a 20% performance improvement on a couple small workloads on 440. Very happy about that one. :)

Anyways, just an update to let folks know where things sit on the kernel side.

Signature · 2 lines
Hollis Blanchard
IBM Linux Technology Center
#1 lore raw
Quoted 33 lines

-----Original Message-----
From: [email protected] [mailto:[email protected]] On Behalf Of Hollis Blanchard
Sent: Saturday, November 01, 2008 3:44 AM
To: kvm-ppc
Subject: kernel status update

I've posted most of the patches here before, but I've built up a small backlog of patches to be pushed upstream. I've been running with them long enough to be happy with them, and I've just posted them all to
http://penguinppc.org/~hollisb/kvm/patches/

There are two really minor patches I want to have acked by the PPC maintainers before I push the whole series to Avi, however. I've submitted those for review already, so hopefully that will be done by early next week.

This patch series has two main components, refactoring and optimization:
* It dramatically restructures some of the code flow and data structures to make supporting other processors possible. There are still a few rough corners though, like the TLB
miss handling
in booke.c kvmppc_handle_exit(). At this point I'd be OK with duplicating those handlers into core-specific files.
* Based on Christian's analysis and patches, there is some performance optimization for Book E and 440 code.
* Yu's idea about TLB handling was great, and I saw about a 20% performance improvement on a couple small workloads
on 440. Very
happy about that one. :)

Quoted 4 lines

Anyways, just an update to let folks know where things sit on the kernel
side.

Good to know that!

#2 lore raw
In reply to Hollis Blanchard
Quoted 4 lines · Hollis Blanchard

* Yu's idea about TLB handling was great, and I saw about a 20% performance improvement on a couple small workloads on 440. Very happy about that one. :)

This patch, which I'll call the "direct TLB" patch, improved performance for me, but I didn't test it hard enough. Christian saw a 20% *regression* with it, because although it does slightly reduce exit handling time across the board, it also dramatically increases the number of TLB misses. I don't think he's posted his data here yet though.

I guess this is because we don't make any effort to reload the TLB when re-entering the guest, so it's far more likely to fault. However, I was actually just thinking that we may be able to take a hybrid approach:
keep the shadow TLB, but only save/restore it on vcpu_get() and vcpu_put(). That way, we wouldn't pay the per-exit cost, but we'd also have a more populated TLB present when we enter the guest. I may try that as soon as I get out of virtio page size hell.

Anyways, the patch was posted at
http://penguinppc.org/~hollisb/kvm/patches/kvmppc_no_shadow_tlb.diff,
but I'll repost it here in case anybody has any thoughts. (Because it can degrade performance, I did not include it in my recent push to Avi.)

-----

kvm: ppc: insert shadow mappings directly into the MMU

The overhead of checking for TLB updates after every exit handler outweighs any benefits. Instead of keeping a shadow TLB data structure (which wouldn't work well for larger TLBs anyways), insert shadow mappings directly into the hardware TLB.

(This means that host mappings may overwrite shadow mappings, which on a subsequent fault will cause the shadow mapping to be reinserted elsewhere (and a corresponding increase to the guest page's refcount). However, we won't lose the page pointer, so we still properly free it.)

The net is about a 20% performance improvement (!).

Thanks to Liu Yu <[email protected]> for the idea.

arch/powerpc/include/asm/kvm_44x.h +14 -8
@@ -22,19 +22,25 @@
 
 #include <linux/kvm_host.h>
 
-/* XXX Can't include mmu-44x.h because it redefines struct mm_context. */
 #define PPC44x_TLB_SIZE 64
+
+/* If the guest is expecting it, this can be as large as we like; we'd just
+ * need to find some way of advertising it. */
+#define KVM44x_GUEST_TLB_SIZE 64
+
+struct kvmppc_44x_shadow_ref {
+	struct page *page;
+	u16 gtlb_index;
+	u8 writeable;
+	u8 tid;
+};
 
 struct kvmppc_vcpu_44x {
 	/* Unmodified copy of the guest's TLB. */
-	struct kvmppc_44x_tlbe guest_tlb[PPC44x_TLB_SIZE];
-	/* TLB that's actually used when the guest is running. */
-	struct kvmppc_44x_tlbe shadow_tlb[PPC44x_TLB_SIZE];
-	/* Pages which are referenced in the shadow TLB. */
-	struct page *shadow_pages[PPC44x_TLB_SIZE];
+	struct kvmppc_44x_tlbe guest_tlb[KVM44x_GUEST_TLB_SIZE];
 
-	/* Track which TLB entries we've modified in the current exit. */
-	u8 shadow_tlb_mod[PPC44x_TLB_SIZE];
+	/* Shadow pages referenced in the real TLB. */
+	struct kvmppc_44x_shadow_ref tlb_shadow[PPC44x_TLB_SIZE];
 
 	struct kvm_vcpu vcpu;
 };
arch/powerpc/include/asm/kvm_ppc.h +2 -2
@@ -52,8 +52,8 @@ extern int kvmppc_emulate_mmio(struct kv
 extern int kvmppc_emulate_mmio(struct kvm_run *run, struct kvm_vcpu *vcpu);
 extern void kvmppc_emulate_dec(struct kvm_vcpu *vcpu);
 
-extern void kvmppc_mmu_map(struct kvm_vcpu *vcpu, u64 gvaddr, gfn_t gfn,
-                           u64 asid, u32 flags);
+extern void kvmppc_mmu_map(struct kvm_vcpu *vcpu, unsigned int gtlb_idx,
+                           gva_t gvaddr, gfn_t gfn, u64 asid, u32 flags);
 extern void kvmppc_mmu_priv_switch(struct kvm_vcpu *vcpu, int usermode);
 extern void kvmppc_mmu_switch_pid(struct kvm_vcpu *vcpu, u32 pid);
 
arch/powerpc/kernel/asm-offsets.c +0 -6
@@ -357,12 +357,6 @@ int main(void)
 #ifdef CONFIG_KVM
 	DEFINE(TLBE_BYTES, sizeof(struct kvmppc_44x_tlbe));
 
-	DEFINE(VCPU_TO_44X, offsetof(struct kvmppc_vcpu_44x, vcpu));
-	DEFINE(VCPU44x_SHADOW_TLB,
-	       offsetof(struct kvmppc_vcpu_44x, shadow_tlb));
-	DEFINE(VCPU44x_SHADOW_MOD,
-	       offsetof(struct kvmppc_vcpu_44x, shadow_tlb_mod));
-
 	DEFINE(VCPU_HOST_STACK, offsetof(struct kvm_vcpu, arch.host_stack));
 	DEFINE(VCPU_HOST_PID, offsetof(struct kvm_vcpu, arch.host_pid));
 	DEFINE(VCPU_GPRS, offsetof(struct kvm_vcpu, arch.gpr));
arch/powerpc/kvm/44x.c +8 -11
@@ -96,21 +96,14 @@ void kvmppc_core_load_guest_debugstate(s
 
 void kvmppc_core_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
 {
-	int i;
-
-	/* Mark every guest entry in the shadow TLB entry modified, so that they
-	 * will all be reloaded on the next vcpu run (instead of being
-	 * demand-faulted). */
-	for (i = 0; i <= tlb_44x_hwater; i++)
-		kvmppc_tlbe_set_modified(vcpu, i);
 }
 
 void kvmppc_core_vcpu_put(struct kvm_vcpu *vcpu)
 {
-	/* Don't leave guest TLB entries resident when being de-scheduled. */
-	/* XXX It would be nice to differentiate between heavyweight exit and
-	 * sched_out here, since we could avoid the TLB flush for heavyweight
-	 * exits. */
+	/* XXX Since every guest uses TS=1 TID=0/1 mappings, we can't leave any TLB
+	 * entries around when we're descheduled, so we must completely flush the
+	 * TLB of all guest mappings. On the other hand, if there is only one
+	 * guest, this flush is completely unnecessary. */
 	_tlbia();
 }
 
@@ -130,6 +123,7 @@ int kvmppc_core_vcpu_setup(struct kvm_vc
 {
 	struct kvmppc_vcpu_44x *vcpu_44x = to_44x(vcpu);
 	struct kvmppc_44x_tlbe *tlbe = &vcpu_44x->guest_tlb[0];
+	int i;
 
 	tlbe->tid = 0;
 	tlbe->word0 = PPC44x_TLB_16M | PPC44x_TLB_VALID;
@@ -147,6 +141,9 @@ int kvmppc_core_vcpu_setup(struct kvm_vc
 	 * real timebase frequency. Accordingly, it must see the state of
 	 * CCR1[TCS]. */
 	vcpu->arch.ccr1 = mfspr(SPRN_CCR1);
+
+	for (i = 0; i < ARRAY_SIZE(vcpu_44x->tlb_shadow); i++)
+		vcpu_44x->tlb_shadow[i].gtlb_index = -1;
 
 	return 0;
 }
arch/powerpc/kvm/44x_tlb.c +138 -145

416 lines not on this page · raw message

arch/powerpc/kvm/44x_tlb.h +2 -5
@@ -25,11 +25,8 @@
 
 extern int kvmppc_44x_tlb_index(struct kvm_vcpu *vcpu, gva_t eaddr,
                                 unsigned int pid, unsigned int as);
-extern struct kvmppc_44x_tlbe *kvmppc_44x_dtlb_search(struct kvm_vcpu *vcpu,
-                                                      gva_t eaddr);
-extern struct kvmppc_44x_tlbe *kvmppc_44x_itlb_search(struct kvm_vcpu *vcpu,
-                                                      gva_t eaddr);
-extern void kvmppc_tlbe_set_modified(struct kvm_vcpu *vcpu, unsigned int i);
+extern int kvmppc_44x_dtlb_index(struct kvm_vcpu *vcpu, gva_t eaddr);
+extern int kvmppc_44x_itlb_index(struct kvm_vcpu *vcpu, gva_t eaddr);
 
 extern int kvmppc_44x_emul_tlbsx(struct kvm_vcpu *vcpu, u8 rt, u8 ra, u8 rb,
                                  u8 rc);
arch/powerpc/kvm/booke.c +17 -12
@@ -24,10 +24,12 @@
 #include <linux/module.h>
 #include <linux/vmalloc.h>
 #include <linux/fs.h>
+
 #include <asm/cputable.h>
 #include <asm/uaccess.h>
+#include <asm/cacheflush.h>
 #include <asm/kvm_ppc.h>
-#include <asm/cacheflush.h>
+#include <asm/kvm_44x.h>
 
 #include "booke.h"
 #include "44x_tlb.h"
@@ -206,11 +208,6 @@ int kvmppc_handle_exit(struct kvm_run *r
 		/* Since we switched IVPR back to the host's value, the host
 		 * handled this interrupt the moment we enabled interrupts.
 		 * Now we just offer it a chance to reschedule the guest. */
-
-		/* XXX At this point the TLB still holds our shadow TLB, so if
-		 * we do reschedule the host will fault over it. Perhaps we
-		 * should politely restore the host's entries to minimize
-		 * misses before ceding control. */
 		vcpu->stat.dec_exits++;
 		if (need_resched())
 			cond_resched();
@@ -281,14 +278,17 @@ int kvmppc_handle_exit(struct kvm_run *r
 		r = RESUME_GUEST;
 		break;
 
+	/* XXX move to a 440-specific file. */
 	case BOOKE_INTERRUPT_DTLB_MISS: {
+		struct kvmppc_vcpu_44x *vcpu_44x = to_44x(vcpu);
 		struct kvmppc_44x_tlbe *gtlbe;
 		unsigned long eaddr = vcpu->arch.fault_dear;
+		int gtlb_index;
 		gfn_t gfn;
 
 		/* Check the guest TLB. */
-		gtlbe = kvmppc_44x_dtlb_search(vcpu, eaddr);
-		if (!gtlbe) {
+		gtlb_index = kvmppc_44x_dtlb_index(vcpu, eaddr);
+		if (gtlb_index < 0) {
 			/* The guest didn't have a mapping for it. */
 			kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_DTLB_MISS);
 			vcpu->arch.dear = vcpu->arch.fault_dear;
@@ -298,6 +298,7 @@ int kvmppc_handle_exit(struct kvm_run *r
 			break;
 		}
 
+		gtlbe = &vcpu_44x->guest_tlb[gtlb_index];
 		vcpu->arch.paddr_accessed = tlb_xlate(gtlbe, eaddr);
 		gfn = vcpu->arch.paddr_accessed >> PAGE_SHIFT;
 
@@ -308,7 +309,7 @@ int kvmppc_handle_exit(struct kvm_run *r
 			 * b) the guest used a large mapping which we're faking
 			 * Either way, we need to satisfy the fault without
 			 * invoking the guest. */
-			kvmppc_mmu_map(vcpu, eaddr, gfn, gtlbe->tid,
+			kvmppc_mmu_map(vcpu, gtlb_index, eaddr, gfn, gtlbe->tid,
 			               gtlbe->word2);
 			vcpu->stat.dtlb_virt_miss_exits++;
 			r = RESUME_GUEST;
@@ -322,16 +323,19 @@ int kvmppc_handle_exit(struct kvm_run *r
 		break;
 	}
 
+	/* XXX move to a 440-specific file. */
 	case BOOKE_INTERRUPT_ITLB_MISS: {
+		struct kvmppc_vcpu_44x *vcpu_44x = to_44x(vcpu);
 		struct kvmppc_44x_tlbe *gtlbe;
 		unsigned long eaddr = vcpu->arch.pc;
 		gfn_t gfn;
+		int gtlb_index;
 
 		r = RESUME_GUEST;
 
 		/* Check the guest TLB. */
-		gtlbe = kvmppc_44x_itlb_search(vcpu, eaddr);
-		if (!gtlbe) {
+		gtlb_index = kvmppc_44x_itlb_index(vcpu, eaddr);
+		if (gtlb_index < 0) {
 			/* The guest didn't have a mapping for it. */
 			kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_ITLB_MISS);
 			vcpu->stat.itlb_real_miss_exits++;
@@ -340,6 +344,7 @@ int kvmppc_handle_exit(struct kvm_run *r
 
 		vcpu->stat.itlb_virt_miss_exits++;
 
+		gtlbe = &vcpu_44x->guest_tlb[gtlb_index];
 		gfn = tlb_xlate(gtlbe, eaddr) >> PAGE_SHIFT;
 
 		if (kvm_is_visible_gfn(vcpu->kvm, gfn)) {
@@ -349,7 +354,7 @@ int kvmppc_handle_exit(struct kvm_run *r
 			 * b) the guest used a large mapping which we're faking
 			 * Either way, we need to satisfy the fault without
 			 * invoking the guest. */
-			kvmppc_mmu_map(vcpu, eaddr, gfn, gtlbe->tid,
+			kvmppc_mmu_map(vcpu, gtlb_index, eaddr, gfn, gtlbe->tid,
 			               gtlbe->word2);
 		} else {
 			/* Guest mapped and leaped at non-RAM! */
arch/powerpc/kvm/booke_interrupts.S +0 -48

54 lines not on this page · raw message

Signature · 2 lines
Hollis Blanchard
IBM Linux Technology Center