Skip to contentSkip to conversation

Faster MMU lookups for Book3s

PATCH New Alexander Graf 4 participants 18 messages Jun 25, 2010 – Jun 29, 2010 10 messages not in the archive

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

PATCH 1/1 Faster MMU lookups for Book3s

Book3s suffered from my really bad shadow MMU implementation so far. So I finally got around to implement a combined hash and list mechanism that allows for much faster lookup of mapped pages.

To show that it really is faster, I tried to run simple process spawning code inside the guest with and without these patches:

[without]

debian-powerpc:~# time for i in {1..1000}; do /bin/echo hello > /dev/null; done
real    0m20.235s
user    0m10.418s
sys     0m9.766s

[with]

debian-powerpc:~# time for i in {1..1000}; do /bin/echo hello > /dev/null; done
real    0m14.659s
user    0m8.967s
sys     0m5.688s

So as you can see, performance improved significantly.

Alexander Graf (2):
KVM: PPC: Add generic hpte management functions
KVM: PPC: Make use of hash based Shadow MMU

 arch/powerpc/include/asm/kvm_book3s.h |    7 +
 arch/powerpc/include/asm/kvm_host.h   |   18 ++-
 arch/powerpc/kvm/Makefile             |    2 +
 arch/powerpc/kvm/book3s_32_mmu_host.c |  104 ++-----------
 arch/powerpc/kvm/book3s_64_mmu_host.c |   98 +-----------
 arch/powerpc/kvm/book3s_mmu_hpte.c    |  286 +++++++++++++++++++++++++++++++++
 6 files changed, 327 insertions(+), 188 deletions(-)
 create mode 100644 arch/powerpc/kvm/book3s_mmu_hpte.c
#1 lore raw

PATCH 1/1 KVM: PPC: Add generic hpte management functions

Currently the shadow paging code keeps an array of entries it knows about. Whenever the guest invalidates an entry, we loop through that entry, trying to invalidate matching parts.

While this is a really simple implementation, it is probably the most ineffective one possible. So instead, let's keep an array of lists around that are indexed by a hash. This way each PTE can be added by 4 list_add, removed by 4 list_del invocations and the search only needs to loop through entries that share the same hash.

This patch implements said lookup and exports generic functions that both the 32-bit and 64-bit backend can use.

---

v1 -> v2:

- remove hpte_all list
- lookup all using vpte_long lists
- decrease size of vpte_long hash
- fix missing brackets

---
 arch/powerpc/kvm/book3s_mmu_hpte.c |  286 ++++++++++++++++++++++++++++++++++++
 1 files changed, 286 insertions(+), 0 deletions(-)
 create mode 100644 arch/powerpc/kvm/book3s_mmu_hpte.c
arch/powerpc/kvm/book3s_mmu_hpte.c new file +286 -0
new file mode 100644
index 0000000..5826e61
@@ -0,0 +1,286 @@
+/*
+ * Copyright (C) 2010 SUSE Linux Products GmbH. All rights reserved.
+ *
+ * Authors:
+ *     Alexander Graf <[email protected]>
+ *
+ * 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.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ */
+
+#include <linux/kvm_host.h>
+#include <linux/hash.h>
+#include <linux/slab.h>
+
+#include <asm/kvm_ppc.h>
+#include <asm/kvm_book3s.h>
+#include <asm/machdep.h>
+#include <asm/mmu_context.h>
+#include <asm/hw_irq.h>
+
+#define PTE_SIZE	12
+
+/* #define DEBUG_MMU */
+
+#ifdef DEBUG_MMU
+#define dprintk_mmu(a, ...) printk(KERN_INFO a, __VA_ARGS__)
+#else
+#define dprintk_mmu(a, ...) do { } while(0)
+#endif
+
+static inline u64 kvmppc_mmu_hash_pte(u64 eaddr) {
+	return hash_64(eaddr >> PTE_SIZE, HPTEG_HASH_BITS_PTE);
+}
+
+static inline u64 kvmppc_mmu_hash_vpte(u64 vpage) {
+	return hash_64(vpage & 0xfffffffffULL, HPTEG_HASH_BITS_VPTE);
+}
+
+static inline u64 kvmppc_mmu_hash_vpte_long(u64 vpage) {
+	return hash_64((vpage & 0xffffff000ULL) >> 12,
+		       HPTEG_HASH_BITS_VPTE_LONG);
+}
+
+void kvmppc_mmu_hpte_cache_map(struct kvm_vcpu *vcpu, struct hpte_cache *pte)
+{
+	u64 index;
+
+	/* Add to ePTE list */
+	index = kvmppc_mmu_hash_pte(pte->pte.eaddr);
+	list_add(&pte->list_pte, &vcpu->arch.hpte_hash_pte[index]);
+
+	/* Add to vPTE list */
+	index = kvmppc_mmu_hash_vpte(pte->pte.vpage);
+	list_add(&pte->list_vpte, &vcpu->arch.hpte_hash_vpte[index]);
+
+	/* Add to vPTE_long list */
+	index = kvmppc_mmu_hash_vpte_long(pte->pte.vpage);
+	list_add(&pte->list_vpte_long, &vcpu->arch.hpte_hash_vpte_long[index]);
+}
+
+static void invalidate_pte(struct kvm_vcpu *vcpu, struct hpte_cache *pte)
+{
+	dprintk_mmu("KVM: Flushing SPT: 0x%lx (0x%llx) -> 0x%llx\n",
+		    pte->pte.eaddr, pte->pte.vpage, pte->host_va);
+
+	/* Different for 32 and 64 bit */
+	kvmppc_mmu_invalidate_pte(vcpu, pte);
+
+	if (pte->pte.may_write)
+		kvm_release_pfn_dirty(pte->pfn);
+	else
+		kvm_release_pfn_clean(pte->pfn);
+
+	list_del(&pte->list_pte);
+	list_del(&pte->list_vpte);
+	list_del(&pte->list_vpte_long);
+
+	kmem_cache_free(vcpu->arch.hpte_cache, pte);
+}
+
+static void kvmppc_mmu_pte_flush_all(struct kvm_vcpu *vcpu)
+{
+	struct hpte_cache *pte, *tmp;
+	int i;
+
+	for (i = 0; i < HPTEG_HASH_NUM_VPTE_LONG; i++) {
+		struct list_head *list = &vcpu->arch.hpte_hash_vpte_long[i];
+
+		list_for_each_entry_safe(pte, tmp, list, list_vpte_long) {
+			/* Jump over the helper entry */
+			if (&pte->list_vpte_long = list)
+				continue;
+
+			invalidate_pte(vcpu, pte);
+		}
+	}
+}
+
+void kvmppc_mmu_pte_flush(struct kvm_vcpu *vcpu, ulong guest_ea, ulong ea_mask)
+{
+	u64 i;
+
+	dprintk_mmu("KVM: Flushing %d Shadow PTEs: 0x%lx & 0x%lx\n",
+		    vcpu->arch.hpte_cache_count, guest_ea, ea_mask);
+
+	guest_ea &= ea_mask;
+
+	switch (ea_mask) {
+	case ~0xfffUL:
+	{
+		struct list_head *list;
+		struct hpte_cache *pte, *tmp;
+
+		/* Find the list of entries in the map */
+		list = &vcpu->arch.hpte_hash_pte[kvmppc_mmu_hash_pte(guest_ea)];
+
+		/* Check the list for matching entries */
+		list_for_each_entry_safe(pte, tmp, list, list_pte) {
+			/* Jump over the helper entry */
+			if (&pte->list_pte = list)
+				continue;
+
+			/* Invalidate matching PTE */
+			if ((pte->pte.eaddr & ~0xfffUL) = guest_ea)
+				invalidate_pte(vcpu, pte);
+		}
+		break;
+	}
+	case 0x0ffff000:
+		/* 32-bit flush w/o segment, go through all possible segments */
+		for (i = 0; i < 0x100000000ULL; i += 0x10000000ULL)
+			kvmppc_mmu_pte_flush(vcpu, guest_ea | i, ~0xfffUL);
+		break;
+	case 0:
+		/* Doing a complete flush -> start from scratch */
+		kvmppc_mmu_pte_flush_all(vcpu);
+		break;
+	default:
+		WARN_ON(1);
+		break;
+	}
+}
+
+/* Flush with mask 0xfffffffff */
+static void kvmppc_mmu_pte_vflush_short(struct kvm_vcpu *vcpu, u64 guest_vp)
+{
+	struct list_head *list;
+	struct hpte_cache *pte, *tmp;
+	u64 vp_mask = 0xfffffffffULL;
+
+	list = &vcpu->arch.hpte_hash_vpte[kvmppc_mmu_hash_vpte(guest_vp)];
+
+	/* Check the list for matching entries */
+	list_for_each_entry_safe(pte, tmp, list, list_vpte) {
+		/* Jump over the helper entry */
+		if (&pte->list_vpte = list)
+			continue;
+
+		/* Invalidate matching PTEs */
+		if ((pte->pte.vpage & vp_mask) = guest_vp)
+			invalidate_pte(vcpu, pte);
+	}
+}
+
+/* Flush with mask 0xffffff000 */
+static void kvmppc_mmu_pte_vflush_long(struct kvm_vcpu *vcpu, u64 guest_vp)
+{
+	struct list_head *list;
+	struct hpte_cache *pte, *tmp;
+	u64 vp_mask = 0xffffff000ULL;
+
+	list = &vcpu->arch.hpte_hash_vpte_long[
+		kvmppc_mmu_hash_vpte_long(guest_vp)];
+
+	/* Check the list for matching entries */
+	list_for_each_entry_safe(pte, tmp, list, list_vpte_long) {
+		/* Jump over the helper entry */
+		if (&pte->list_vpte_long = list)
+			continue;
+
+		/* Invalidate matching PTEs */
+		if ((pte->pte.vpage & vp_mask) = guest_vp)
+			invalidate_pte(vcpu, pte);
+	}
+}
+
+void kvmppc_mmu_pte_vflush(struct kvm_vcpu *vcpu, u64 guest_vp, u64 vp_mask)
+{
+	dprintk_mmu("KVM: Flushing %d Shadow vPTEs: 0x%llx & 0x%llx\n",
+		    vcpu->arch.hpte_cache_count, guest_vp, vp_mask);
+	guest_vp &= vp_mask;
+
+	switch(vp_mask) {
+	case 0xfffffffffULL:
+		kvmppc_mmu_pte_vflush_short(vcpu, guest_vp);
+		break;
+	case 0xffffff000ULL:
+		kvmppc_mmu_pte_vflush_long(vcpu, guest_vp);
+		break;
+	default:
+		WARN_ON(1);
+		return;
+	}
+}
+
+void kvmppc_mmu_pte_pflush(struct kvm_vcpu *vcpu, ulong pa_start, ulong pa_end)
+{
+	struct hpte_cache *pte, *tmp;
+	int i;
+
+	dprintk_mmu("KVM: Flushing %d Shadow pPTEs: 0x%lx - 0x%lx\n",
+		    vcpu->arch.hpte_cache_count, pa_start, pa_end);
+
+	for (i = 0; i < HPTEG_HASH_NUM_VPTE_LONG; i++) {
+		struct list_head *list = &vcpu->arch.hpte_hash_vpte_long[i];
+
+		list_for_each_entry_safe(pte, tmp, list, list_vpte_long) {
+			/* Jump over the helper entry */
+			if (&pte->list_vpte_long = list)
+				continue;
+
+			if ((pte->pte.raddr >= pa_start) &&
+			    (pte->pte.raddr < pa_end)) {
+				invalidate_pte(vcpu, pte);
+			}
+		}
+	}
+}
+
+struct hpte_cache *kvmppc_mmu_hpte_cache_next(struct kvm_vcpu *vcpu)
+{
+	struct hpte_cache *pte;
+
+	pte = kmem_cache_zalloc(vcpu->arch.hpte_cache, GFP_KERNEL);
+	vcpu->arch.hpte_cache_count++;
+
+	if (vcpu->arch.hpte_cache_count = HPTEG_CACHE_NUM)
+		kvmppc_mmu_pte_flush_all(vcpu);
+
+	return pte;
+}
+
+void kvmppc_mmu_hpte_destroy(struct kvm_vcpu *vcpu)
+{
+	kvmppc_mmu_pte_flush(vcpu, 0, 0);
+	kmem_cache_destroy(vcpu->arch.hpte_cache);
+}
+
+static void kvmppc_mmu_hpte_init_hash(struct list_head *hash_list, int len)
+{
+	int i;
+
+	for (i = 0; i < len; i++) {
+		INIT_LIST_HEAD(&hash_list[i]);
+	}
+}
+
+int kvmppc_mmu_hpte_init(struct kvm_vcpu *vcpu)
+{
+	char kmem_name[128];
+
+	/* init hpte slab cache */
+	snprintf(kmem_name, 128, "kvm-spt-%p", vcpu);
+	vcpu->arch.hpte_cache = kmem_cache_create(kmem_name,
+		sizeof(struct hpte_cache), sizeof(struct hpte_cache), 0, NULL);
+
+	/* init hpte lookup hashes */
+	kvmppc_mmu_hpte_init_hash(vcpu->arch.hpte_hash_pte,
+				  ARRAY_SIZE(vcpu->arch.hpte_hash_pte));
+	kvmppc_mmu_hpte_init_hash(vcpu->arch.hpte_hash_vpte,
+				  ARRAY_SIZE(vcpu->arch.hpte_hash_vpte));
+	kvmppc_mmu_hpte_init_hash(vcpu->arch.hpte_hash_vpte_long,
+				  ARRAY_SIZE(vcpu->arch.hpte_hash_vpte_long));
+
+	return 0;
+}
Signature · 1 line
1.6.0.2
#27 lore raw

PATCH 1/1 KVM: PPC: Make use of hash based Shadow MMU

In reply to a message not in the archive

We just introduced generic functions to handle shadow pages on PPC. This patch makes the respective backends make use of them, getting rid of a lot of duplicate code along the way.

---
 arch/powerpc/include/asm/kvm_book3s.h |    7 ++
 arch/powerpc/include/asm/kvm_host.h   |   18 +++++-
 arch/powerpc/kvm/Makefile             |    2 +
 arch/powerpc/kvm/book3s_32_mmu_host.c |  104 +++-----------------------------
 arch/powerpc/kvm/book3s_64_mmu_host.c |   98 ++----------------------------
 5 files changed, 41 insertions(+), 188 deletions(-)
arch/powerpc/include/asm/kvm_book3s.h +7 -0
index 4e99559..a96e405 100644
@@ -115,6 +115,13 @@ extern void kvmppc_mmu_book3s_32_init(struct kvm_vcpu *vcpu);
 extern int kvmppc_mmu_map_page(struct kvm_vcpu *vcpu, struct kvmppc_pte *pte);
 extern int kvmppc_mmu_map_segment(struct kvm_vcpu *vcpu, ulong eaddr);
 extern void kvmppc_mmu_flush_segments(struct kvm_vcpu *vcpu);
+
+extern void kvmppc_mmu_hpte_cache_map(struct kvm_vcpu *vcpu, struct hpte_cache *pte);
+extern struct hpte_cache *kvmppc_mmu_hpte_cache_next(struct kvm_vcpu *vcpu);
+extern void kvmppc_mmu_hpte_destroy(struct kvm_vcpu *vcpu);
+extern int kvmppc_mmu_hpte_init(struct kvm_vcpu *vcpu);
+extern void kvmppc_mmu_invalidate_pte(struct kvm_vcpu *vcpu, struct hpte_cache *pte);
+
 extern int kvmppc_ld(struct kvm_vcpu *vcpu, ulong *eaddr, int size, void *ptr, bool data);
 extern int kvmppc_st(struct kvm_vcpu *vcpu, ulong *eaddr, int size, void *ptr, bool data);
 extern void kvmppc_book3s_queue_irqprio(struct kvm_vcpu *vcpu, unsigned int vec);
arch/powerpc/include/asm/kvm_host.h +15 -3
index 0c9ad86..895eb63 100644
@@ -38,7 +38,13 @@
 #define KVM_NR_PAGE_SIZES	1
 #define KVM_PAGES_PER_HPAGE(x)	(1UL<<31)
 
-#define HPTEG_CACHE_NUM 1024
+#define HPTEG_CACHE_NUM			(1 << 15)
+#define HPTEG_HASH_BITS_PTE		13
+#define HPTEG_HASH_BITS_VPTE		13
+#define HPTEG_HASH_BITS_VPTE_LONG	5
+#define HPTEG_HASH_NUM_PTE		(1 << HPTEG_HASH_BITS_PTE)
+#define HPTEG_HASH_NUM_VPTE		(1 << HPTEG_HASH_BITS_VPTE)
+#define HPTEG_HASH_NUM_VPTE_LONG	(1 << HPTEG_HASH_BITS_VPTE_LONG)
 
 struct kvm;
 struct kvm_run;
@@ -151,6 +157,9 @@ struct kvmppc_mmu {
 };
 
 struct hpte_cache {
+	struct list_head list_pte;
+	struct list_head list_vpte;
+	struct list_head list_vpte_long;
 	u64 host_va;
 	u64 pfn;
 	ulong slot;
@@ -282,8 +291,11 @@ struct kvm_vcpu_arch {
 	unsigned long pending_exceptions;
 
 #ifdef CONFIG_PPC_BOOK3S
-	struct hpte_cache hpte_cache[HPTEG_CACHE_NUM];
-	int hpte_cache_offset;
+	struct kmem_cache *hpte_cache;
+	struct list_head hpte_hash_pte[HPTEG_HASH_NUM_PTE];
+	struct list_head hpte_hash_vpte[HPTEG_HASH_NUM_VPTE];
+	struct list_head hpte_hash_vpte_long[HPTEG_HASH_NUM_VPTE_LONG];
+	int hpte_cache_count;
 #endif
 };
 
arch/powerpc/kvm/Makefile +2 -0
index ff43606..d45c818 100644
@@ -45,6 +45,7 @@ kvm-book3s_64-objs := \
 	book3s.o \
 	book3s_emulate.o \
 	book3s_interrupts.o \
+	book3s_mmu_hpte.o \
 	book3s_64_mmu_host.o \
 	book3s_64_mmu.o \
 	book3s_32_mmu.o
@@ -57,6 +58,7 @@ kvm-book3s_32-objs := \
 	book3s.o \
 	book3s_emulate.o \
 	book3s_interrupts.o \
+	book3s_mmu_hpte.o \
 	book3s_32_mmu_host.o \
 	book3s_32_mmu.o
 kvm-objs-$(CONFIG_KVM_BOOK3S_32) := $(kvm-book3s_32-objs)
arch/powerpc/kvm/book3s_32_mmu_host.c +10 -94

148 lines not on this page · raw message

arch/powerpc/kvm/book3s_64_mmu_host.c +7 -91

131 lines not on this page · raw message

Signature · 1 line
1.6.0.2
#2 lore raw

Re: [PATCH] KVM: PPC: Add generic hpte management functions

In reply to Alexander Graf
Quoted 4 lines · Alexander Graf

Currently the shadow paging code keeps an array of entries it knows about. Whenever the guest invalidates an entry, we loop through that entry, trying to invalidate matching parts.

Quoted 8 lines

While this is a really simple implementation, it is probably the most ineffective one possible. So instead, let's keep an array of lists around that are indexed by a hash. This way each PTE can be added by 4 list_add, removed by 4 list_del invocations and the search only needs to loop through entries that share the same hash.

This patch implements said lookup and exports generic functions that both the 32-bit and 64-bit backend can use.

Yikes - I forgot -n.

This is patch 1/2.

Alex

#4 lore raw

Re: [PATCH] KVM: PPC: Add generic hpte management functions

In reply to a message not in the archive
Quoted 18 lines · Alexander Graf

Currently the shadow paging code keeps an array of entries it knows about. Whenever the guest invalidates an entry, we loop through that entry, trying to invalidate matching parts.

While this is a really simple implementation, it is probably the most ineffective one possible. So instead, let's keep an array of lists around that are indexed by a hash. This way each PTE can be added by 4 list_add, removed by 4 list_del invocations and the search only needs to loop through entries that share the same hash.

This patch implements said lookup and exports generic functions that both the 32-bit and 64-bit backend can use.

arch/powerpc/kvm/book3s_mmu_hpte.c new file +3 -0
+
+static inline u64 kvmppc_mmu_hash_pte(u64 eaddr) {
+	return hash_64(eaddr>>  PTE_SIZE, HPTEG_HASH_BITS_PTE);

arch/powerpc/kvm/book3s_mmu_hpte.c line 52

Quoted 10 lines
arch/powerpc/kvm/book3s_mmu_hpte.c new file +10 -0
+}
+
+static inline u64 kvmppc_mmu_hash_vpte(u64 vpage) {
+	return hash_64(vpage&  0xfffffffffULL, HPTEG_HASH_BITS_VPTE);
+}
+
+static inline u64 kvmppc_mmu_hash_vpte_long(u64 vpage) {
+	return hash_64((vpage&  0xffffff000ULL)>>  12,
+		       HPTEG_HASH_BITS_VPTE_LONG);
+}

Still with the wierd coding style?

arch/powerpc/kvm/book3s_mmu_hpte.c line 102

Quoted 13 lines
arch/powerpc/kvm/book3s_mmu_hpte.c new file +12 -0
+static void kvmppc_mmu_pte_flush_all(struct kvm_vcpu *vcpu)
+{
+	struct hpte_cache *pte, *tmp;
+	int i;
+
+	for (i = 0; i<  HPTEG_HASH_NUM_VPTE_LONG; i++) {
+		struct list_head *list =&vcpu->arch.hpte_hash_vpte_long[i];
+
+		list_for_each_entry_safe(pte, tmp, list, list_vpte_long) {
+			/* Jump over the helper entry */
+			if (&pte->list_vpte_long == list)
+				continue;

I don't think l_f_e_e_s() will ever give you the head back.

arch/powerpc/kvm/book3s_mmu_hpte.c line 105

Quoted 4 lines
arch/powerpc/kvm/book3s_mmu_hpte.c new file +3 -0
+
+			invalidate_pte(vcpu, pte);
+		}

Does invalidate_pte() remove the pte? doesn't seem so, so you can drop the _safe iteration.

Quoted 17 lines
arch/powerpc/kvm/book3s_mmu_hpte.c new file +16 -0
+	}
+}
+
+void kvmppc_mmu_pte_flush(struct kvm_vcpu *vcpu, ulong guest_ea, ulong ea_mask)
+{
+	u64 i;
+
+	dprintk_mmu("KVM: Flushing %d Shadow PTEs: 0x%lx&  0x%lx\n",
+		    vcpu->arch.hpte_cache_count, guest_ea, ea_mask);
+
+	guest_ea&= ea_mask;
+
+	switch (ea_mask) {
+	case ~0xfffUL:
+	{
+		struct list_head *list;

arch/powerpc/kvm/book3s_mmu_hpte.c line 131

Quoted 10 lines
arch/powerpc/kvm/book3s_mmu_hpte.c new file +10 -0
+		struct hpte_cache *pte, *tmp;
+
+		/* Find the list of entries in the map */
+		list =&vcpu->arch.hpte_hash_pte[kvmppc_mmu_hash_pte(guest_ea)];
+
+		/* Check the list for matching entries */
+		list_for_each_entry_safe(pte, tmp, list, list_pte) {
+			/* Jump over the helper entry */
+			if (&pte->list_pte == list)
+				continue;

Same here.

arch/powerpc/kvm/book3s_mmu_hpte.c line 138

Quoted 8 lines
arch/powerpc/kvm/book3s_mmu_hpte.c new file +7 -0
+
+			/* Invalidate matching PTE */
+			if ((pte->pte.eaddr&  ~0xfffUL) == guest_ea)
+				invalidate_pte(vcpu, pte);
+		}
+		break;
+	}

Would be nice to put this block into a function.

Quoted 20 lines
arch/powerpc/kvm/book3s_mmu_hpte.c new file +19 -0
+	case 0x0ffff000:
+		/* 32-bit flush w/o segment, go through all possible segments */
+		for (i = 0; i<  0x100000000ULL; i += 0x10000000ULL)
+			kvmppc_mmu_pte_flush(vcpu, guest_ea | i, ~0xfffUL);
+		break;
+	case 0:
+		/* Doing a complete flush ->  start from scratch */
+		kvmppc_mmu_pte_flush_all(vcpu);
+		break;
+	default:
+		WARN_ON(1);
+		break;
+	}
+}
+
+/* Flush with mask 0xfffffffff */
+static void kvmppc_mmu_pte_vflush_short(struct kvm_vcpu *vcpu, u64 guest_vp)
+{
+	struct list_head *list;

arch/powerpc/kvm/book3s_mmu_hpte.c line 167

Quoted 10 lines
arch/powerpc/kvm/book3s_mmu_hpte.c new file +10 -0
+	struct hpte_cache *pte, *tmp;
+	u64 vp_mask = 0xfffffffffULL;
+
+	list =&vcpu->arch.hpte_hash_vpte[kvmppc_mmu_hash_vpte(guest_vp)];
+
+	/* Check the list for matching entries */
+	list_for_each_entry_safe(pte, tmp, list, list_vpte) {
+		/* Jump over the helper entry */
+		if (&pte->list_vpte == list)
+			continue;

list cannot contain list. Or maybe I don't understand the data structure. Isn't it multiple hash tables with lists holding matching ptes?

Quoted 20 lines
arch/powerpc/kvm/book3s_mmu_hpte.c new file +18 -0
+
+		/* Invalidate matching PTEs */
+		if ((pte->pte.vpage&  vp_mask) == guest_vp)
+			invalidate_pte(vcpu, pte);
+	}
+}
+
 
+
+void kvmppc_mmu_pte_pflush(struct kvm_vcpu *vcpu, ulong pa_start, ulong pa_end)
+{
+	struct hpte_cache *pte, *tmp;
+	int i;
+
+	dprintk_mmu("KVM: Flushing %d Shadow pPTEs: 0x%lx - 0x%lx\n",
+		    vcpu->arch.hpte_cache_count, pa_start, pa_end);
+
+	for (i = 0; i<  HPTEG_HASH_NUM_VPTE_LONG; i++) {
+		struct list_head *list =&vcpu->arch.hpte_hash_vpte_long[i];

arch/powerpc/kvm/book3s_mmu_hpte.c line 235

Quoted 10 lines
arch/powerpc/kvm/book3s_mmu_hpte.c new file +10 -0
+
+		list_for_each_entry_safe(pte, tmp, list, list_vpte_long) {
+			/* Jump over the helper entry */
+			if (&pte->list_vpte_long == list)
+				continue;
+
+			if ((pte->pte.raddr>= pa_start)&&
+			    (pte->pte.raddr<  pa_end)) {
+				invalidate_pte(vcpu, pte);
+			}

Extra braces.

Quoted 5 lines
arch/powerpc/kvm/book3s_mmu_hpte.c new file +4 -0
+		}
+	}
+}
+

arch/powerpc/kvm/book3s_mmu_hpte.c line 266

Quoted 10 lines
arch/powerpc/kvm/book3s_mmu_hpte.c new file +9 -0
 
+
+static void kvmppc_mmu_hpte_init_hash(struct list_head *hash_list, int len)
+{
+	int i;
+
+	for (i = 0; i<  len; i++) {
+		INIT_LIST_HEAD(&hash_list[i]);
+	}
+}

Extra braces.

arch/powerpc/kvm/book3s_mmu_hpte.c line 275

Quoted 10 lines
arch/powerpc/kvm/book3s_mmu_hpte.c new file +9 -0
+
+int kvmppc_mmu_hpte_init(struct kvm_vcpu *vcpu)
+{
+	char kmem_name[128];
+
+	/* init hpte slab cache */
+	snprintf(kmem_name, 128, "kvm-spt-%p", vcpu);
+	vcpu->arch.hpte_cache = kmem_cache_create(kmem_name,
+		sizeof(struct hpte_cache), sizeof(struct hpte_cache), 0, NULL);

Why not one global cache?

Quoted 12 lines
arch/powerpc/kvm/book3s_mmu_hpte.c new file +11 -0
+
+	/* init hpte lookup hashes */
+	kvmppc_mmu_hpte_init_hash(vcpu->arch.hpte_hash_pte,
+				  ARRAY_SIZE(vcpu->arch.hpte_hash_pte));
+	kvmppc_mmu_hpte_init_hash(vcpu->arch.hpte_hash_vpte,
+				  ARRAY_SIZE(vcpu->arch.hpte_hash_vpte));
+	kvmppc_mmu_hpte_init_hash(vcpu->arch.hpte_hash_vpte_long,
+				  ARRAY_SIZE(vcpu->arch.hpte_hash_vpte_long));
+
+	return 0;
+}
Signature · 1 line
error compiling committee.c: too many arguments to function
#6 lore raw

Re: [PATCH] KVM: PPC: Add generic hpte management functions

In reply to a message not in the archive
Quoted 15 lines · Avi Kivity
Quoted 14 lines · Alexander Graf

Currently the shadow paging code keeps an array of entries it knows about. Whenever the guest invalidates an entry, we loop through that entry, trying to invalidate matching parts.

While this is a really simple implementation, it is probably the most ineffective one possible. So instead, let's keep an array of lists around that are indexed by a hash. This way each PTE can be added by 4 list_add, removed by 4 list_del invocations and the search only needs to loop through entries that share the same hash.

This patch implements said lookup and exports generic functions that both the 32-bit and 64-bit backend can use.

Quoted 16 lines
Quoted 4 lines
arch/powerpc/kvm/book3s_mmu_hpte.c new file +3 -0
+
+static inline u64 kvmppc_mmu_hash_pte(u64 eaddr) {
+	return hash_64(eaddr>>  PTE_SIZE, HPTEG_HASH_BITS_PTE);

arch/powerpc/kvm/book3s_mmu_hpte.c line 52

Quoted 10 lines
arch/powerpc/kvm/book3s_mmu_hpte.c new file +10 -0
+}
+
+static inline u64 kvmppc_mmu_hash_vpte(u64 vpage) {
+	return hash_64(vpage&  0xfffffffffULL, HPTEG_HASH_BITS_VPTE);
+}
+
+static inline u64 kvmppc_mmu_hash_vpte_long(u64 vpage) {
+	return hash_64((vpage&  0xffffff000ULL)>>  12,
+		       HPTEG_HASH_BITS_VPTE_LONG);
+}

Still with the wierd coding style?

Not sure what's going on there. My editor displays it normally. Weird.

Quoted 16 lines
Quoted 4 lines
arch/powerpc/kvm/book3s_mmu_hpte.c new file +2 -0
+static void kvmppc_mmu_pte_flush_all(struct kvm_vcpu *vcpu)
+{

arch/powerpc/kvm/book3s_mmu_hpte.c line 102

Quoted 10 lines
arch/powerpc/kvm/book3s_mmu_hpte.c new file +10 -0
+	struct hpte_cache *pte, *tmp;
+	int i;
+
+	for (i = 0; i<  HPTEG_HASH_NUM_VPTE_LONG; i++) {
+		struct list_head *list =&vcpu->arch.hpte_hash_vpte_long[i];
+
+		list_for_each_entry_safe(pte, tmp, list, list_vpte_long) {
+			/* Jump over the helper entry */
+			if (&pte->list_vpte_long == list)
+				continue;

I don't think l_f_e_e_s() will ever give you the head back.

Uh. Usually you have struct list_head in a struct and you point to the first entry to loop over all. So if it doesn't return the first entry, that would seem very counter-intuitive.

Quoted 7 lines

arch/powerpc/kvm/book3s_mmu_hpte.c line 105

Quoted 5 lines
arch/powerpc/kvm/book3s_mmu_hpte.c new file +3 -0
+
+			invalidate_pte(vcpu, pte);
+		}

Does invalidate_pte() remove the pte? doesn't seem so, so you can drop the _safe iteration.

Yes, it does.

Quoted 31 lines
Quoted 18 lines
arch/powerpc/kvm/book3s_mmu_hpte.c new file +16 -0
+	}
+}
+
+void kvmppc_mmu_pte_flush(struct kvm_vcpu *vcpu, ulong guest_ea, ulong ea_mask)
+{
+	u64 i;
+
+	dprintk_mmu("KVM: Flushing %d Shadow PTEs: 0x%lx&  0x%lx\n",
+		    vcpu->arch.hpte_cache_count, guest_ea, ea_mask);
+
+	guest_ea&= ea_mask;
+
+	switch (ea_mask) {
+	case ~0xfffUL:
+	{
+		struct list_head *list;

arch/powerpc/kvm/book3s_mmu_hpte.c line 131

Quoted 10 lines
arch/powerpc/kvm/book3s_mmu_hpte.c new file +10 -0
+		struct hpte_cache *pte, *tmp;
+
+		/* Find the list of entries in the map */
+		list =&vcpu->arch.hpte_hash_pte[kvmppc_mmu_hash_pte(guest_ea)];
+
+		/* Check the list for matching entries */
+		list_for_each_entry_safe(pte, tmp, list, list_pte) {
+			/* Jump over the helper entry */
+			if (&pte->list_pte == list)
+				continue;

Same here.

Quoted 10 lines

arch/powerpc/kvm/book3s_mmu_hpte.c line 138

Quoted 8 lines
arch/powerpc/kvm/book3s_mmu_hpte.c new file +7 -0
+
+			/* Invalidate matching PTE */
+			if ((pte->pte.eaddr&  ~0xfffUL) == guest_ea)
+				invalidate_pte(vcpu, pte);
+		}
+		break;
+	}

Would be nice to put this block into a function.

Yup.

Quoted 25 lines
Quoted 25 lines
arch/powerpc/kvm/book3s_mmu_hpte.c new file +24 -0
+	case 0x0ffff000:
+		/* 32-bit flush w/o segment, go through all possible segments */
+		for (i = 0; i<  0x100000000ULL; i += 0x10000000ULL)
+			kvmppc_mmu_pte_flush(vcpu, guest_ea | i, ~0xfffUL);
+		break;
+	case 0:
+		/* Doing a complete flush ->  start from scratch */
+		kvmppc_mmu_pte_flush_all(vcpu);
+		break;
+	default:
+		WARN_ON(1);
+		break;
+	}
+}
+
+/* Flush with mask 0xfffffffff */
+static void kvmppc_mmu_pte_vflush_short(struct kvm_vcpu *vcpu, u64 guest_vp)
+{
+	struct list_head *list;
+	struct hpte_cache *pte, *tmp;
+	u64 vp_mask = 0xfffffffffULL;
+
+	list =&vcpu->arch.hpte_hash_vpte[kvmppc_mmu_hash_vpte(guest_vp)];
+
Quoted 8 lines

arch/powerpc/kvm/book3s_mmu_hpte.c line 167

Quoted 6 lines
arch/powerpc/kvm/book3s_mmu_hpte.c new file +5 -0
+	/* Check the list for matching entries */
+	list_for_each_entry_safe(pte, tmp, list, list_vpte) {
+		/* Jump over the helper entry */
+		if (&pte->list_vpte == list)
+			continue;

list cannot contain list. Or maybe I don't understand the data structure. Isn't it multiple hash tables with lists holding matching ptes?

It is multiple hash tables with list_heads that are one element of a list that contains the matching ptes. Usually you'd have

struct x {
struct list_head;
int foo;
char bar;
};

and you loop through each of those elements. What we have here is

struct list_head hash[..];

and some loose struct x's. The hash's "next" element is a struct x.

The "normal" way would be to have "struct x hash[..];" but I figured that eats too much space.

Quoted 25 lines
Quoted 25 lines
arch/powerpc/kvm/book3s_mmu_hpte.c new file +23 -0
+
+		/* Invalidate matching PTEs */
+		if ((pte->pte.vpage&  vp_mask) == guest_vp)
+			invalidate_pte(vcpu, pte);
+	}
+}
+
 
+
+void kvmppc_mmu_pte_pflush(struct kvm_vcpu *vcpu, ulong pa_start, ulong pa_end)
+{
+	struct hpte_cache *pte, *tmp;
+	int i;
+
+	dprintk_mmu("KVM: Flushing %d Shadow pPTEs: 0x%lx - 0x%lx\n",
+		    vcpu->arch.hpte_cache_count, pa_start, pa_end);
+
+	for (i = 0; i<  HPTEG_HASH_NUM_VPTE_LONG; i++) {
+		struct list_head *list =&vcpu->arch.hpte_hash_vpte_long[i];
+
+		list_for_each_entry_safe(pte, tmp, list, list_vpte_long) {
+			/* Jump over the helper entry */
+			if (&pte->list_vpte_long == list)
+				continue;
Quoted 8 lines

arch/powerpc/kvm/book3s_mmu_hpte.c line 105

Quoted 6 lines
arch/powerpc/kvm/book3s_mmu_hpte.c new file +5 -0
+
+			if ((pte->pte.raddr>= pa_start)&&
+			    (pte->pte.raddr<  pa_end)) {
+				invalidate_pte(vcpu, pte);
+			}

Extra braces.

Yeah, for two-lined if's I find it more readable that way. Is it forbidden?

Quoted 6 lines
Quoted 6 lines
arch/powerpc/kvm/book3s_mmu_hpte.c new file +4 -0
+		}
+	}
+}
+
Quoted 12 lines

arch/powerpc/kvm/book3s_mmu_hpte.c line 266

Quoted 10 lines
arch/powerpc/kvm/book3s_mmu_hpte.c new file +9 -0
+
+static void kvmppc_mmu_hpte_init_hash(struct list_head *hash_list, int len)
+{
+	int i;
+
+	for (i = 0; i<  len; i++) {
+		INIT_LIST_HEAD(&hash_list[i]);
+	}
+}

Extra braces.

Yup.

Quoted 13 lines

arch/powerpc/kvm/book3s_mmu_hpte.c line 275

Quoted 11 lines
arch/powerpc/kvm/book3s_mmu_hpte.c new file +9 -0
+
+int kvmppc_mmu_hpte_init(struct kvm_vcpu *vcpu)
+{
+	char kmem_name[128];
+
+	/* init hpte slab cache */
+	snprintf(kmem_name, 128, "kvm-spt-%p", vcpu);
+	vcpu->arch.hpte_cache = kmem_cache_create(kmem_name,
+		sizeof(struct hpte_cache), sizeof(struct hpte_cache), 0, NULL);

Why not one global cache?

You mean over all vcpus? Or over all VMs? Because this way they don't interfere. An operation on one vCPU doesn't inflict anything on another. There's also no locking necessary this way.

Alex

#8 lore raw

Re: [PATCH] KVM: PPC: Add generic hpte management functions

In reply to a message not in the archive
Quoted 18 lines · Alexander Graf
Quoted 18 lines
Quoted 6 lines
arch/powerpc/kvm/book3s_mmu_hpte.c new file +3 -0
+
+static inline u64 kvmppc_mmu_hash_pte(u64 eaddr) {
+	return hash_64(eaddr>>   PTE_SIZE, HPTEG_HASH_BITS_PTE);

arch/powerpc/kvm/book3s_mmu_hpte.c line 52

Quoted 10 lines
arch/powerpc/kvm/book3s_mmu_hpte.c new file +10 -0
+}
+
+static inline u64 kvmppc_mmu_hash_vpte(u64 vpage) {
+	return hash_64(vpage&   0xfffffffffULL, HPTEG_HASH_BITS_VPTE);
+}
+
+static inline u64 kvmppc_mmu_hash_vpte_long(u64 vpage) {
+	return hash_64((vpage&   0xffffff000ULL)>>   12,
+		       HPTEG_HASH_BITS_VPTE_LONG);
+}

Still with the wierd coding style?

Quoted 2 lines

Not sure what's going on there. My editor displays it normally. Weird.

Try hitting 'save'.

Quoted 16 lines
Quoted 7 lines
Quoted 7 lines
arch/powerpc/kvm/book3s_mmu_hpte.c new file +7 -0
+static void kvmppc_mmu_pte_flush_all(struct kvm_vcpu *vcpu)
+{
+	struct hpte_cache *pte, *tmp;
+	int i;
+
+	for (i = 0; i<   HPTEG_HASH_NUM_VPTE_LONG; i++) {
+		struct list_head *list =&vcpu->arch.hpte_hash_vpte_long[i];
Quoted 9 lines

arch/powerpc/kvm/book3s_mmu_hpte.c line 102

Quoted 7 lines
arch/powerpc/kvm/book3s_mmu_hpte.c new file +5 -0
+
+		list_for_each_entry_safe(pte, tmp, list, list_vpte_long) {
+			/* Jump over the helper entry */
+			if (&pte->list_vpte_long == list)
+				continue;

I don't think l_f_e_e_s() will ever give you the head back.

Quoted 2 lines

Uh. Usually you have struct list_head in a struct and you point to the first entry to loop over all. So if it doesn't return the first entry, that would seem very counter-intuitive.

Linux list_heads aren't intuitive. The same structure is used for the container and for the nodes. Would have been better (and more typesafe) to have separate list_heads and list_nodes.

Quoted 9 lines
Quoted 7 lines

arch/powerpc/kvm/book3s_mmu_hpte.c line 105

Quoted 5 lines
arch/powerpc/kvm/book3s_mmu_hpte.c new file +3 -0
+
+			invalidate_pte(vcpu, pte);
+		}

Does invalidate_pte() remove the pte? doesn't seem so, so you can drop the _safe iteration.

Yes, it does.

I don't see it?

Quoted 15 lines

static void invalidate_pte(struct hpte_cache *pte)

{
    dprintk_mmu("KVM: Flushing SPT: 0x%lx (0x%llx) -> 0x%llx\n",
            pte->pte.eaddr, pte->pte.vpage, pte->host_va);

    ppc_md.hpte_invalidate(pte->slot, pte->host_va,
                   MMU_PAGE_4K, MMU_SEGSIZE_256M,
                   false);
    pte->host_va = 0;

    if (pte->pte.may_write)
        kvm_release_pfn_dirty(pte->pfn);
    else
        kvm_release_pfn_clean(pte->pfn);
}

Am I looking at old code?

Quoted 27 lines
Quoted 10 lines
Quoted 10 lines
arch/powerpc/kvm/book3s_mmu_hpte.c new file +10 -0
+
+/* Flush with mask 0xfffffffff */
+static void kvmppc_mmu_pte_vflush_short(struct kvm_vcpu *vcpu, u64 guest_vp)
+{
+	struct list_head *list;
+	struct hpte_cache *pte, *tmp;
+	u64 vp_mask = 0xfffffffffULL;
+
+	list =&vcpu->arch.hpte_hash_vpte[kvmppc_mmu_hash_vpte(guest_vp)];
+
Quoted 9 lines

arch/powerpc/kvm/book3s_mmu_hpte.c line 167

Quoted 7 lines
arch/powerpc/kvm/book3s_mmu_hpte.c new file +5 -0
+	/* Check the list for matching entries */
+	list_for_each_entry_safe(pte, tmp, list, list_vpte) {
+		/* Jump over the helper entry */
+		if (&pte->list_vpte == list)
+			continue;

list cannot contain list. Or maybe I don't understand the data structure. Isn't it multiple hash tables with lists holding matching ptes?

It is multiple hash tables with list_heads that are one element of a list that contains the matching ptes. Usually you'd have

struct x {
   struct list_head;
   int foo;
   char bar;
};
Quoted 8 lines

and you loop through each of those elements. What we have here is

struct list_head hash[..];

and some loose struct x's. The hash's "next" element is a struct x.

The "normal" way would be to have "struct x hash[..];" but I figured that eats too much space.

No, what you describe is quite normal. In fact, x86 kvm mmu is exactly like that, except we only have a single hash:

Quoted 1 line
    struct hlist_head mmu_page_hash[KVM_NUM_MMU_PAGES];

(another difference is using struct hlist_head instead of list_head, which I recommend since it saves space)

Quoted 11 lines
Quoted 9 lines

arch/powerpc/kvm/book3s_mmu_hpte.c line 105

Quoted 7 lines
arch/powerpc/kvm/book3s_mmu_hpte.c new file +5 -0
+
+			if ((pte->pte.raddr>= pa_start)&&
+			    (pte->pte.raddr<   pa_end)) {
+				invalidate_pte(vcpu, pte);
+			}

Extra braces.

Yeah, for two-lined if's I find it more readable that way. Is it forbidden?

It's not forbidden, but it tends to attract "cleanup" patches, which are annoying. Best to conform to the coding style if there isn't a good reason not to.

Personally I prefer braces for one-liners (yes they're ugly, but they're safer and easier to patch).

Quoted 12 lines
Quoted 12 lines

arch/powerpc/kvm/book3s_mmu_hpte.c line 275

Quoted 10 lines
arch/powerpc/kvm/book3s_mmu_hpte.c new file +8 -0
+int kvmppc_mmu_hpte_init(struct kvm_vcpu *vcpu)
+{
+	char kmem_name[128];
+
+	/* init hpte slab cache */
+	snprintf(kmem_name, 128, "kvm-spt-%p", vcpu);
+	vcpu->arch.hpte_cache = kmem_cache_create(kmem_name,
+		sizeof(struct hpte_cache), sizeof(struct hpte_cache), 0, NULL);

Why not one global cache?

Quoted 1 line

You mean over all vcpus? Or over all VMs?

Totally global. As in 'static struct kmem_cache *kvm_hpte_cache;'.

Quoted 2 lines

Because this way they don't interfere. An operation on one vCPU doesn't inflict anything on another. There's also no locking necessary this way.

The slab writers have solved this for everyone, not just us. kmem_cache_alloc() will usually allocate from a per-cpu cache, so no interference and/or locking. See ____cache_alloc().

If there's a problem in kmem_cache_alloc(), solve it there, don't introduce workarounds.

Signature · 1 line
error compiling committee.c: too many arguments to function
#10 lore raw

Re: [PATCH] KVM: PPC: Add generic hpte management functions

In reply to a message not in the archive

Am 28.06.2010 um 11:12 schrieb Avi Kivity <[email protected]>:

Quoted 19 lines · Alexander Graf
Quoted 18 lines
Quoted 18 lines
Quoted 6 lines
(quoted diff) +3 -0
+
+static inline u64 kvmppc_mmu_hash_pte(u64 eaddr) {
+    return hash_64(eaddr>>   PTE_SIZE, HPTEG_HASH_BITS_PTE);

Review

Quoted 10 lines
(quoted diff) +10 -0
+}
+
+static inline u64 kvmppc_mmu_hash_vpte(u64 vpage) {
+    return hash_64(vpage&   0xfffffffffULL, HPTEG_HASH_BITS_VPTE);
+}
+
+static inline u64 kvmppc_mmu_hash_vpte_long(u64 vpage) {
+    return hash_64((vpage&   0xffffff000ULL)>>   12,
+               HPTEG_HASH_BITS_VPTE_LONG);
+}

Still with the wierd coding style?

Quoted 4 lines
Quoted 2 lines

Not sure what's going on there. My editor displays it normally. Weird.

Try hitting 'save'.

Thanks for the hint :). No really, no idea what's going on here.

Quoted 18 lines
Quoted 18 lines
Quoted 18 lines
Quoted 11 lines
(quoted diff) +7 -0
+static void kvmppc_mmu_pte_flush_all(struct kvm_vcpu *vcpu)
+{
+    struct hpte_cache *pte, *tmp;
+    int i;
+
+    for (i = 0; i<   HPTEG_HASH_NUM_VPTE_LONG; i++) {
+        struct list_head *list =&vcpu->arch.hpte_hash_vpte_long 

[i];

Review

Quoted 5 lines
(quoted diff) +5 -0
+
+        list_for_each_entry_safe(pte, tmp, list, list_vpte_long) {
+            /* Jump over the helper entry */
+            if (&pte->list_vpte_long == list)
+                continue;

I don't think l_f_e_e_s() will ever give you the head back.

Quoted 4 lines
Quoted 2 lines

Uh. Usually you have struct list_head in a struct and you point to the first entry to loop over all. So if it doesn't return the first entry, that would seem very counter-intuitive.

Linux list_heads aren't intuitive. The same structure is used for the container and for the nodes. Would have been better (and more typesafe) to have separate list_heads and list_nodes.

Hrm. Ok, I'll check by reading the source.

Quoted 23 lines
Quoted 10 lines
Quoted 8 lines

Review

Quoted 6 lines
(quoted diff) +3 -0
+
+            invalidate_pte(vcpu, pte);
+        }

Does invalidate_pte() remove the pte? doesn't seem so, so you can drop the _safe iteration.

Yes, it does.

I don't see it?

Quoted 10 lines

static void invalidate_pte(struct hpte_cache *pte)

{
   dprintk_mmu("KVM: Flushing SPT: 0x%lx (0x%llx) -> 0x%llx\n",
           pte->pte.eaddr, pte->pte.vpage, pte->host_va);

   ppc_md.hpte_invalidate(pte->slot, pte->host_va,
                  MMU_PAGE_4K, MMU_SEGSIZE_256M,
                  false);
   pte->host_va = 0;
Quoted 7 lines
Quoted 5 lines
   if (pte->pte.may_write)
       kvm_release_pfn_dirty(pte->pfn);
   else
       kvm_release_pfn_clean(pte->pfn);
}

Am I looking at old code?

Apparently. Check book3s_mmu_*.c

Quoted 32 lines
Quoted 32 lines
Quoted 13 lines
Quoted 13 lines
(quoted diff) +3 -0
+
+/* Flush with mask 0xfffffffff */
+static void kvmppc_mmu_pte_vflush_short(struct kvm_vcpu *vcpu,  

u64 guest_vp)

(quoted diff) +6 -0
+{
+    struct list_head *list;
+    struct hpte_cache *pte, *tmp;
+    u64 vp_mask = 0xfffffffffULL;
+
+    list =&vcpu->arch.hpte_hash_vpte[kvmppc_mmu_hash_vpte 

(guest_vp)];
+

Quoted 9 lines

Review

Quoted 7 lines
(quoted diff) +5 -0
+    /* Check the list for matching entries */
+    list_for_each_entry_safe(pte, tmp, list, list_vpte) {
+        /* Jump over the helper entry */
+        if (&pte->list_vpte == list)
+            continue;

list cannot contain list. Or maybe I don't understand the data structure. Isn't it multiple hash tables with lists holding matching ptes?

It is multiple hash tables with list_heads that are one element of a list that contains the matching ptes. Usually you'd have

struct x {
struct list_head;
int foo;
char bar;
};

and you loop through each of those elements. What we have here is

Quoted 10 lines
Quoted 6 lines

struct list_head hash[..];

and some loose struct x's. The hash's "next" element is a struct x.

The "normal" way would be to have "struct x hash[..];" but I figured that eats too much space.

No, what you describe is quite normal. In fact, x86 kvm mmu is exactly like that, except we only have a single hash:

Quoted 1 line
   struct hlist_head mmu_page_hash[KVM_NUM_MMU_PAGES];

I see.

Quoted 2 lines

(another difference is using struct hlist_head instead of list_head, which I recommend since it saves space)

Hrm. I thought about this quite a bit before too, but that makes invalidation more complicated, no? We always need to remember the previous entry in a list.

Quoted 30 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
(quoted diff) +5 -0
+
+            if ((pte->pte.raddr>= pa_start)&&
+                (pte->pte.raddr<   pa_end)) {
+                invalidate_pte(vcpu, pte);
+            }
Quoted 4 lines
Quoted 2 lines

Extra braces.

Yeah, for two-lined if's I find it more readable that way. Is it forbidden?

It's not forbidden, but it tends to attract "cleanup" patches, which are annoying. Best to conform to the coding style if there isn't a good reason not to.

Personally I prefer braces for one-liners (yes they're ugly, but they're safer and easier to patch).

Quoted 13 lines
Quoted 13 lines
Quoted 11 lines
(quoted diff) +8 -0
+int kvmppc_mmu_hpte_init(struct kvm_vcpu *vcpu)
+{
+    char kmem_name[128];
+
+    /* init hpte slab cache */
+    snprintf(kmem_name, 128, "kvm-spt-%p", vcpu);
+    vcpu->arch.hpte_cache = kmem_cache_create(kmem_name,
+        sizeof(struct hpte_cache), sizeof(struct hpte_cache), 0,  

NULL);

Why not one global cache?

Quoted 3 lines
Quoted 1 line

You mean over all vcpus? Or over all VMs?

Totally global. As in 'static struct kmem_cache *kvm_hpte_cache;'.

What would be the benefit?

Quoted 7 lines
Quoted 3 lines

Because this way they don't interfere. An operation on one vCPU doesn't inflict anything on another. There's also no locking necessary this way.

The slab writers have solved this for everyone, not just us. kmem_cache_alloc() will usually allocate from a per-cpu cache, so no interference and/or locking. See ____cache_alloc().

If there's a problem in kmem_cache_alloc(), solve it there, don't introduce workarounds.

So you would still keep different hash arrays and everything, just allocate the objects from a global pool? I still fail to see how that benefits anyone.

Alex

#11 lore raw

Re: [PATCH] KVM: PPC: Add generic hpte management functions

Quoted 4 lines · Alexander Graf
Quoted 1 line

Am I looking at old code?

Apparently. Check book3s_mmu_*.c

I don't have that pattern.

Quoted 8 lines
Quoted 4 lines

(another difference is using struct hlist_head instead of list_head, which I recommend since it saves space)

Hrm. I thought about this quite a bit before too, but that makes invalidation more complicated, no? We always need to remember the previous entry in a list.

hlist_for_each_entry_safe() does that.

Quoted 14 lines
Quoted 14 lines
Quoted 14 lines
Quoted 14 lines
Quoted 9 lines
(quoted diff) +8 -0
+int kvmppc_mmu_hpte_init(struct kvm_vcpu *vcpu)
+{
+    char kmem_name[128];
+
+    /* init hpte slab cache */
+    snprintf(kmem_name, 128, "kvm-spt-%p", vcpu);
+    vcpu->arch.hpte_cache = kmem_cache_create(kmem_name,
+        sizeof(struct hpte_cache), sizeof(struct hpte_cache), 0, 
Quoted 3 lines

NULL);

Why not one global cache?

Quoted 5 lines
Quoted 3 lines
Quoted 1 line

You mean over all vcpus? Or over all VMs?

Totally global. As in 'static struct kmem_cache *kvm_hpte_cache;'.

What would be the benefit?

Less and simpler code, better reporting through slabtop, less wastage of partially allocated slab pages.

Quoted 9 lines
Quoted 9 lines
Quoted 4 lines

Because this way they don't interfere. An operation on one vCPU doesn't inflict anything on another. There's also no locking necessary this way.

The slab writers have solved this for everyone, not just us. kmem_cache_alloc() will usually allocate from a per-cpu cache, so no interference and/or locking. See ____cache_alloc().

Quoted 5 lines
Quoted 2 lines

If there's a problem in kmem_cache_alloc(), solve it there, don't introduce workarounds.

So you would still keep different hash arrays and everything, just allocate the objects from a global pool?

Yes.

Quoted 1 line

I still fail to see how that benefits anyone.

See above.

Signature · 1 line
error compiling committee.c: too many arguments to function
#13 lore raw

Re: [PATCH] KVM: PPC: Add generic hpte management functions

In reply to a message not in the archive
Quoted 7 lines · Avi Kivity
Quoted 4 lines · Alexander Graf
Quoted 1 line

Am I looking at old code?

Apparently. Check book3s_mmu_*.c

I don't have that pattern.

It's in this patch.

Quoted 25 lines
(quoted diff) +21 -0
+static void invalidate_pte(struct kvm_vcpu *vcpu, struct hpte_cache *pte)
+{
+	dprintk_mmu("KVM: Flushing SPT: 0x%lx (0x%llx) -> 0x%llx\n",
+		    pte->pte.eaddr, pte->pte.vpage, pte->host_va);
+
+	/* Different for 32 and 64 bit */
+	kvmppc_mmu_invalidate_pte(vcpu, pte);
+
+	if (pte->pte.may_write)
+		kvm_release_pfn_dirty(pte->pfn);
+	else
+		kvm_release_pfn_clean(pte->pfn);
+
+	list_del(&pte->list_pte);
+	list_del(&pte->list_vpte);
+	list_del(&pte->list_vpte_long);
+	list_del(&pte->list_all);
+
+	kmem_cache_free(vcpu->arch.hpte_cache, pte);
+}
+
Quoted 4 lines
Quoted 4 lines
Quoted 8 lines
Quoted 6 lines
Quoted 2 lines

(another difference is using struct hlist_head instead of list_head, which I recommend since it saves space)

Hrm. I thought about this quite a bit before too, but that makes invalidation more complicated, no? We always need to remember the previous entry in a list.

hlist_for_each_entry_safe() does that.

Oh - very nice. So all I need to do is pass the previous list entry to invalide_pte too and I'm good. I guess I'll give it a shot.

Quoted 15 lines
Quoted 15 lines
Quoted 15 lines
Quoted 15 lines
Quoted 15 lines
Quoted 10 lines
(quoted diff) +8 -0
+int kvmppc_mmu_hpte_init(struct kvm_vcpu *vcpu)
+{
+    char kmem_name[128];
+
+    /* init hpte slab cache */
+    snprintf(kmem_name, 128, "kvm-spt-%p", vcpu);
+    vcpu->arch.hpte_cache = kmem_cache_create(kmem_name,
+        sizeof(struct hpte_cache), sizeof(struct hpte_cache), 0,
Quoted 3 lines

NULL);

Why not one global cache?

Quoted 8 lines
Quoted 5 lines
Quoted 3 lines
Quoted 1 line

You mean over all vcpus? Or over all VMs?

Totally global. As in 'static struct kmem_cache *kvm_hpte_cache;'.

What would be the benefit?

Less and simpler code, better reporting through slabtop, less wastage of partially allocated slab pages.

But it also means that one VM can spill the global slab cache and kill another VM's mm performance, no?

Alex

#15 lore raw

Re: [PATCH] KVM: PPC: Add generic hpte management functions

In reply to a message not in the archive
Quoted 4 lines · Alexander Graf
Quoted 2 lines · Avi Kivity

On 06/28/2010 12:27 PM, Alexander Graf wrote:

Quoted 9 lines
Quoted 7 lines
Quoted 5 lines
Quoted 2 lines

Am I looking at old code?

Apparently. Check book3s_mmu_*.c

I don't have that pattern.

It's in this patch.

Yes. Silly me.

Quoted 14 lines
Quoted 14 lines
(quoted diff) +14 -0
+static void invalidate_pte(struct kvm_vcpu *vcpu, struct hpte_cache *pte)
+{
+	dprintk_mmu("KVM: Flushing SPT: 0x%lx (0x%llx) ->  0x%llx\n",
+		    pte->pte.eaddr, pte->pte.vpage, pte->host_va);
+
+	/* Different for 32 and 64 bit */
+	kvmppc_mmu_invalidate_pte(vcpu, pte);
+
+	if (pte->pte.may_write)
+		kvm_release_pfn_dirty(pte->pfn);
+	else
+		kvm_release_pfn_clean(pte->pfn);
+
+	list_del(&pte->list_pte);
Quoted 8 lines

Review

Quoted 8 lines
(quoted diff) +7 -0
+	list_del(&pte->list_vpte);
+	list_del(&pte->list_vpte_long);
+	list_del(&pte->list_all);
+
+	kmem_cache_free(vcpu->arch.hpte_cache, pte);
+}
+

(that's the old one with list_all - better check what's going on here)

Quoted 12 lines
Quoted 9 lines
Quoted 7 lines
Quoted 3 lines

(another difference is using struct hlist_head instead of list_head, which I recommend since it saves space)

Hrm. I thought about this quite a bit before too, but that makes invalidation more complicated, no? We always need to remember the previous entry in a list.

hlist_for_each_entry_safe() does that.

Oh - very nice. So all I need to do is pass the previous list entry to invalide_pte too and I'm good. I guess I'll give it a shot.

No, just the for_each cursor.

Quoted 6 lines
Quoted 3 lines

Less and simpler code, better reporting through slabtop, less wastage of partially allocated slab pages.

But it also means that one VM can spill the global slab cache and kill another VM's mm performance, no?

What do you mean by spill?

btw, in the midst of the nit-picking frenzy I forgot to ask how the individual hash chain lengths as well as the per-vm allocation were limited.

On x86 we have a per-vm limit and we allow the mm shrinker to reduce shadow mmu data structures dynamically.

Signature · 1 line
error compiling committee.c: too many arguments to function
#17 lore raw

Re: [PATCH] KVM: PPC: Add generic hpte management functions

In reply to a message not in the archive
Quoted 34 lines · Avi Kivity
Quoted 4 lines · Alexander Graf
Quoted 2 lines · Avi Kivity

On 06/28/2010 12:27 PM, Alexander Graf wrote:

Quoted 9 lines
Quoted 7 lines
Quoted 5 lines
Quoted 2 lines

Am I looking at old code?

Apparently. Check book3s_mmu_*.c

I don't have that pattern.

It's in this patch.

Yes. Silly me.

Quoted 17 lines
Quoted 17 lines

+static void invalidate_pte(struct kvm_vcpu *vcpu, struct hpte_cache *pte)

(quoted diff) +15 -0
+{
+    dprintk_mmu("KVM: Flushing SPT: 0x%lx (0x%llx) ->  0x%llx\n",
+            pte->pte.eaddr, pte->pte.vpage, pte->host_va);
+
+    /* Different for 32 and 64 bit */
+    kvmppc_mmu_invalidate_pte(vcpu, pte);
+
+    if (pte->pte.may_write)
+        kvm_release_pfn_dirty(pte->pfn);
+    else
+        kvm_release_pfn_clean(pte->pfn);
+
+    list_del(&pte->list_pte);
+    list_del(&pte->list_vpte);
+    list_del(&pte->list_vpte_long);
Quoted 8 lines
Quoted 6 lines

Review

Quoted 6 lines
(quoted diff) +5 -0
+    list_del(&pte->list_all);
+
+    kmem_cache_free(vcpu->arch.hpte_cache, pte);
+}
+

(that's the old one with list_all - better check what's going on here)

Yeah, I just searched my inbox for the first patch. Obviously it was the old version :(.

Quoted 24 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

(another difference is using struct hlist_head instead of list_head, which I recommend since it saves space)

Quoted 9 lines
Quoted 6 lines
Quoted 4 lines

Hrm. I thought about this quite a bit before too, but that makes invalidation more complicated, no? We always need to remember the previous entry in a list.

hlist_for_each_entry_safe() does that.

Oh - very nice. So all I need to do is pass the previous list entry to invalide_pte too and I'm good. I guess I'll give it a shot.

No, just the for_each cursor.

Quoted 6 lines
Quoted 3 lines

Less and simpler code, better reporting through slabtop, less wastage of partially allocated slab pages.

But it also means that one VM can spill the global slab cache and kill another VM's mm performance, no?

Quoted 9 lines

What do you mean by spill?

btw, in the midst of the nit-picking frenzy I forgot to ask how the individual hash chain lengths as well as the per-vm allocation were limited.

On x86 we have a per-vm limit and we allow the mm shrinker to reduce shadow mmu data structures dynamically.

Very simple. I keep an int with the number of allocated entries around and if that hits a define'd threshold, I flush all shadow pages.

Alex

#18 lore raw

Re: [PATCH] KVM: PPC: Add generic hpte management functions

Quoted 11 lines · Alexander Graf
Quoted 11 lines
Quoted 9 lines
Quoted 5 lines

Less and simpler code, better reporting through slabtop, less wastage of partially allocated slab pages.

But it also means that one VM can spill the global slab cache and kill another VM's mm performance, no?

What do you mean by spill?

Well?

Quoted 11 lines
Quoted 8 lines

btw, in the midst of the nit-picking frenzy I forgot to ask how the individual hash chain lengths as well as the per-vm allocation were limited.

On x86 we have a per-vm limit and we allow the mm shrinker to reduce shadow mmu data structures dynamically.

Very simple. I keep an int with the number of allocated entries around and if that hits a define'd threshold, I flush all shadow pages.

A truly nefarious guest will make all ptes hash to the same chain, making some operations very long (O(n^2) in the x86 mmu, don't know about ppc) under a spinlock. So we had to limit hash chains, not just the number of entries.

But your mmu is per-cpu, no? In that case, no spinlock, and any damage the guest does is limited to itself.

Signature · 1 line
error compiling committee.c: too many arguments to function
#20 lore raw

Re: [PATCH] KVM: PPC: Add generic hpte management functions

In reply to a message not in the archive
Quoted 10 lines · Avi Kivity
Quoted 9 lines · Alexander Graf
Quoted 9 lines
Quoted 9 lines
Quoted 5 lines

Less and simpler code, better reporting through slabtop, less wastage of partially allocated slab pages.

But it also means that one VM can spill the global slab cache and kill another VM's mm performance, no?

Quoted 5 lines
Quoted 2 lines
Quoted 2 lines

What do you mean by spill?

Well?

I was thinking of a global capping, but I guess I could still do it per-vcpu. So yes, doing a global slab doesn't hurt.

Quoted 13 lines
Quoted 5 lines
Quoted 5 lines

btw, in the midst of the nit-picking frenzy I forgot to ask how the individual hash chain lengths as well as the per-vm allocation were limited.

Quoted 7 lines
Quoted 4 lines

On x86 we have a per-vm limit and we allow the mm shrinker to reduce shadow mmu data structures dynamically.

Very simple. I keep an int with the number of allocated entries around and if that hits a define'd threshold, I flush all shadow pages.

Quoted 7 lines

A truly nefarious guest will make all ptes hash to the same chain, making some operations very long (O(n^2) in the x86 mmu, don't know about ppc) under a spinlock. So we had to limit hash chains, not just the number of entries.

But your mmu is per-cpu, no? In that case, no spinlock, and any damage the guest does is limited to itself.

Yes, it is. No locking. The vcpu can kill its own performance, but I don't care about that.

Alex

#21 lore raw

Re: [PATCH] KVM: PPC: Add generic hpte management functions

In reply to a message not in the archive
Quoted 19 lines · Avi Kivity
Quoted 18 lines · Alexander Graf
Quoted 18 lines
Quoted 6 lines
(quoted diff) +3 -0
+
+static inline u64 kvmppc_mmu_hash_pte(u64 eaddr) {
+    return hash_64(eaddr>>   PTE_SIZE, HPTEG_HASH_BITS_PTE);

Review

Quoted 10 lines
(quoted diff) +10 -0
+}
+
+static inline u64 kvmppc_mmu_hash_vpte(u64 vpage) {
+    return hash_64(vpage&   0xfffffffffULL, HPTEG_HASH_BITS_VPTE);
+}
+
+static inline u64 kvmppc_mmu_hash_vpte_long(u64 vpage) {
+    return hash_64((vpage&   0xffffff000ULL)>>   12,
+               HPTEG_HASH_BITS_VPTE_LONG);
+}

Still with the wierd coding style?

Quoted 4 lines
Quoted 2 lines

Not sure what's going on there. My editor displays it normally. Weird.

Try hitting 'save'.

hexdump -C on the respective section in the exact patch file I submitted above shows:

00000a80 75 72 6e 20 68 61 73 68 5f 36 34 28 65 61 64 64 |urn hash_64(eadd|
00000a90 72 20 3e 3e 20 50 54 45 5f 53 49 5a 45 2c 20 48 |r >> PTE_SIZE, H|

Maybe your mail client breaks it?

Alex

#23 lore raw

Re: [PATCH] KVM: PPC: Add generic hpte management functions

In reply to a message not in the archive
Quoted 32 lines · Alexander Graf
Quoted 22 lines · Avi Kivity
Quoted 20 lines · Alexander Graf
Quoted 20 lines
Quoted 7 lines
(quoted diff) +3 -0
+
+static inline u64 kvmppc_mmu_hash_pte(u64 eaddr) {
+    return hash_64(eaddr>>    PTE_SIZE, HPTEG_HASH_BITS_PTE);

Review

Quoted 10 lines
(quoted diff) +10 -0
+}
+
+static inline u64 kvmppc_mmu_hash_vpte(u64 vpage) {
+    return hash_64(vpage&    0xfffffffffULL, HPTEG_HASH_BITS_VPTE);
+}
+
+static inline u64 kvmppc_mmu_hash_vpte_long(u64 vpage) {
+    return hash_64((vpage&    0xffffff000ULL)>>    12,
+               HPTEG_HASH_BITS_VPTE_LONG);
+}

Still with the wierd coding style?

Quoted 5 lines
Quoted 3 lines

Not sure what's going on there. My editor displays it normally. Weird.

Try hitting 'save'.

hexdump -C on the respective section in the exact patch file I submitted above shows:

Quoted 8 lines

00000a80 75 72 6e 20 68 61 73 68 5f 36 34 28 65 61 64 64 |urn hash_64(eadd|
00000a90 72 20 3e 3e 20 50 54 45 5f 53 49 5a 45 2c 20 48 |r>> PTE_SIZE, H|

Maybe your mail client breaks it?

The list archives too:

http://www.mail-archive.com/[email protected]/msg37093.html

Looks like a cache coherency bug. What processor are you using?

Signature · 1 line
error compiling committee.c: too many arguments to function
#24 lore raw

Re: [PATCH] KVM: PPC: Add generic hpte management functions

Quoted 39 lines · Avi Kivity
Quoted 38 lines · Alexander Graf
Quoted 22 lines · Avi Kivity
Quoted 20 lines · Alexander Graf
Quoted 20 lines
Quoted 7 lines
(quoted diff) +3 -0
+
+static inline u64 kvmppc_mmu_hash_pte(u64 eaddr) {
+    return hash_64(eaddr>>    PTE_SIZE, HPTEG_HASH_BITS_PTE);

Review

Quoted 10 lines
(quoted diff) +10 -0
+}
+
+static inline u64 kvmppc_mmu_hash_vpte(u64 vpage) {
+    return hash_64(vpage&    0xfffffffffULL, HPTEG_HASH_BITS_VPTE);
+}
+
+static inline u64 kvmppc_mmu_hash_vpte_long(u64 vpage) {
+    return hash_64((vpage&    0xffffff000ULL)>>    12,
+               HPTEG_HASH_BITS_VPTE_LONG);
+}

Still with the wierd coding style?

Quoted 5 lines
Quoted 3 lines

Not sure what's going on there. My editor displays it normally. Weird.

Try hitting 'save'.

hexdump -C on the respective section in the exact patch file I submitted above shows:

00000a80 75 72 6e 20 68 61 73 68 5f 36 34 28 65 61 64 64 |urn hash_64(eadd|
00000a90 72 20 3e 3e 20 50 54 45 5f 53 49 5a 45 2c 20 48 |r>> PTE_SIZE, H|

Quoted 8 lines
Quoted 2 lines

Maybe your mail client breaks it?

The list archives too:

http://www.mail-archive.com/[email protected]/msg37093.html

Looks like a cache coherency bug. What processor are you using?

Are we looking at the same link? Looks good to me there.

Alex

#25 lore raw

Re: [PATCH] KVM: PPC: Add generic hpte management functions

Quoted 4 lines · Alexander Graf

Are we looking at the same link? Looks good to me there.

We're probably looking at the same link but looking at different things. I'm whining about

     static u64 f() {
         ...
     }

as opposed to the more sober

     static u64 f()
     {
         ...
     }

for f in [kvmppc_mmu_hash_pte, kvmppc_mmu_hash_vpte, kvmppc_mmu_hash_vpte_long].

Signature · 1 line
error compiling committee.c: too many arguments to function