Skip to contentSkip to conversation

KVM: PPC: Book3s: HV: Add HPT preallocation

PATCH In review Alexander Graf 2 participants 10 messages Jan 18, 2012 – Jan 19, 2012

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

cover KVM: PPC: Book3s: HV: Add HPT preallocation

While using the book3s hv code on a 970 system, we quickly ran into situations where we didn't have enough contiguous memory available to allocate a 16MB region for the page table we need to manage the guest's memory.

So I went ahead, cleaned up the code we currently use to preallocate RMAs and made it usable for HPT allocation. This patch set is the result of it. As a nice side effect, it also solves a potential security issue on 970 where we reused the RMA of previous guests without clearing them.

These patches are in use in our internal build service testbed and work smoothly, giving us a working setup based on 970.

Alexander Graf (3):
KVM: PPC: Convert RMA allocation into generic code
KVM: PPC: Initialize linears with zeros
KVM: PPC: Add HPT preallocator

 arch/powerpc/include/asm/kvm_host.h  |    8 +-
 arch/powerpc/include/asm/kvm_ppc.h   |   10 +-
 arch/powerpc/kernel/setup_64.c       |    2 +-
 arch/powerpc/kvm/book3s_64_mmu_hv.c  |   18 +++-
 arch/powerpc/kvm/book3s_hv.c         |    8 +-
 arch/powerpc/kvm/book3s_hv_builtin.c |  209 +++++++++++++++++++++++-----------
 6 files changed, 174 insertions(+), 81 deletions(-)
#1 lore raw

PATCH 1/3 KVM: PPC: Convert RMA allocation into generic code

We have code to allocate big chunks of linear memory on bootup for later use. This code is currently used for RMA allocation, but can be useful beyond that extent.

Make it generic so we can reuse it for other stuff later.

---
 arch/powerpc/include/asm/kvm_host.h  |    7 +-
 arch/powerpc/include/asm/kvm_ppc.h   |    8 +-
 arch/powerpc/kernel/setup_64.c       |    2 +-
 arch/powerpc/kvm/book3s_hv.c         |    8 +-
 arch/powerpc/kvm/book3s_hv_builtin.c |  175 ++++++++++++++++++++--------------
 5 files changed, 118 insertions(+), 82 deletions(-)
arch/powerpc/include/asm/kvm_host.h +4 -3
index af438b1..8221e71 100644
@@ -173,12 +173,13 @@ struct kvmppc_spapr_tce_table {
 	struct page *pages[0];
 };
 
-struct kvmppc_rma_info {
+struct kvmppc_linear_info {
 	void		*base_virt;
 	unsigned long	 base_pfn;
 	unsigned long	 npages;
 	struct list_head list;
-	atomic_t 	 use_count;
+	atomic_t	 use_count;
+	int		 type;
 };
 
 /*
@@ -224,7 +225,7 @@ struct kvm_arch {
 	int tlbie_lock;
 	unsigned long lpcr;
 	unsigned long rmor;
-	struct kvmppc_rma_info *rma;
+	struct kvmppc_linear_info *rma;
 	unsigned long vrma_slb_v;
 	int rma_setup_done;
 	int using_mmu_notifiers;
arch/powerpc/include/asm/kvm_ppc.h +4 -4
index a61b5b5..1c37a2f 100644
@@ -128,8 +128,8 @@ extern long kvm_vm_ioctl_create_spapr_tce(struct kvm *kvm,
 				struct kvm_create_spapr_tce *args);
 extern long kvm_vm_ioctl_allocate_rma(struct kvm *kvm,
 				struct kvm_allocate_rma *rma);
-extern struct kvmppc_rma_info *kvm_alloc_rma(void);
-extern void kvm_release_rma(struct kvmppc_rma_info *ri);
+extern struct kvmppc_linear_info *kvm_alloc_rma(void);
+extern void kvm_release_rma(struct kvmppc_linear_info *ri);
 extern int kvmppc_core_init_vm(struct kvm *kvm);
 extern void kvmppc_core_destroy_vm(struct kvm *kvm);
 extern int kvmppc_core_prepare_memory_region(struct kvm *kvm,
@@ -187,13 +187,13 @@ static inline void kvmppc_set_xics_phys(int cpu, unsigned long addr)
 	paca[cpu].kvm_hstate.xics_phys = addr;
 }
 
-extern void kvm_rma_init(void);
+extern void kvm_linear_init(void);
 
 #else
 static inline void kvmppc_set_xics_phys(int cpu, unsigned long addr)
 {}
 
-static inline void kvm_rma_init(void)
+static inline void kvm_linear_init(void)
 {}
 #endif
 
arch/powerpc/kernel/setup_64.c +1 -1
index 4cb8f1e..4721b0c 100644
@@ -598,7 +598,7 @@ void __init setup_arch(char **cmdline_p)
 	/* Initialize the MMU context management stuff */
 	mmu_context_init();
 
-	kvm_rma_init();
+	kvm_linear_init();
 
 	ppc64_boot_msg(0x15, "Setup Done");
 }
arch/powerpc/kvm/book3s_hv.c +4 -4
index 3580db8..ce1cac7 100644
@@ -1055,7 +1055,7 @@ static inline int lpcr_rmls(unsigned long rma_size)
 
 static int kvm_rma_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
 {
-	struct kvmppc_rma_info *ri = vma->vm_file->private_data;
+	struct kvmppc_linear_info *ri = vma->vm_file->private_data;
 	struct page *page;
 
 	if (vmf->pgoff >= ri->npages)
@@ -1080,7 +1080,7 @@ static int kvm_rma_mmap(struct file *file, struct vm_area_struct *vma)
 
 static int kvm_rma_release(struct inode *inode, struct file *filp)
 {
-	struct kvmppc_rma_info *ri = filp->private_data;
+	struct kvmppc_linear_info *ri = filp->private_data;
 
 	kvm_release_rma(ri);
 	return 0;
@@ -1093,7 +1093,7 @@ static struct file_operations kvm_rma_fops = {
 
 long kvm_vm_ioctl_allocate_rma(struct kvm *kvm, struct kvm_allocate_rma *ret)
 {
-	struct kvmppc_rma_info *ri;
+	struct kvmppc_linear_info *ri;
 	long fd;
 
 	ri = kvm_alloc_rma();
@@ -1212,7 +1212,7 @@ static int kvmppc_hv_setup_rma(struct kvm_vcpu *vcpu)
 {
 	int err = 0;
 	struct kvm *kvm = vcpu->kvm;
-	struct kvmppc_rma_info *ri = NULL;
+	struct kvmppc_linear_info *ri = NULL;
 	unsigned long hva;
 	struct kvm_memory_slot *memslot;
 	struct vm_area_struct *vma;
arch/powerpc/kvm/book3s_hv_builtin.c +105 -70

218 lines not on this page · raw message

Signature · 1 line
1.6.0.2
Acked-by on v1 1/3
#2 lore raw

Re: [PATCH 1/3] KVM: PPC: Convert RMA allocation into generic code

Quoted 7 lines · Alexander Graf

We have code to allocate big chunks of linear memory on bootup for later use. This code is currently used for RMA allocation, but can be useful beyond that extent.

Make it generic so we can reuse it for other stuff later.

#3 lore raw

PATCH 3/3 KVM: PPC: Add HPT preallocator

In reply to Alexander Graf

We're currently allocating 16MB of linear memory on demand when creating a guest. That does work some times, but finding 16MB of linear memory available in the system at runtime is definitely not a given.

So let's add another command line option similar to the RMA preallocator, that we can use to keep a pool of page tables around. Now, when a guest gets created it has a pretty low chance of receiving an OOM.

---

v1 -> v2: use else instead of goto

---
 arch/powerpc/include/asm/kvm_host.h  |    1 +
 arch/powerpc/include/asm/kvm_ppc.h   |    2 +
 arch/powerpc/kvm/book3s_64_mmu_hv.c  |   19 ++++++++++++++--
 arch/powerpc/kvm/book3s_hv_builtin.c |   39 +++++++++++++++++++++++++++++++++-
 4 files changed, 57 insertions(+), 4 deletions(-)
arch/powerpc/include/asm/kvm_host.h +1 -0
index 8221e71..1843d5d 100644
@@ -235,6 +235,7 @@ struct kvm_arch {
 	int slot_npages[KVM_MEM_SLOTS_NUM];
 	unsigned short last_vcpu[NR_CPUS];
 	struct kvmppc_vcore *vcores[KVM_MAX_VCORES];
+	struct kvmppc_linear_info *hpt_li;
 #endif /* CONFIG_KVM_BOOK3S_64_HV */
 };
 
arch/powerpc/include/asm/kvm_ppc.h +2 -0
index 1c37a2f..9d6dee0 100644
@@ -130,6 +130,8 @@ extern long kvm_vm_ioctl_allocate_rma(struct kvm *kvm,
 				struct kvm_allocate_rma *rma);
 extern struct kvmppc_linear_info *kvm_alloc_rma(void);
 extern void kvm_release_rma(struct kvmppc_linear_info *ri);
+extern struct kvmppc_linear_info *kvm_alloc_hpt(void);
+extern void kvm_release_hpt(struct kvmppc_linear_info *li);
 extern int kvmppc_core_init_vm(struct kvm *kvm);
 extern void kvmppc_core_destroy_vm(struct kvm *kvm);
 extern int kvmppc_core_prepare_memory_region(struct kvm *kvm,
arch/powerpc/kvm/book3s_64_mmu_hv.c +16 -3
index 783cd35..ddc485a 100644
@@ -44,10 +44,20 @@ long kvmppc_alloc_hpt(struct kvm *kvm)
 	unsigned long hpt;
 	unsigned long lpid;
 	struct revmap_entry *rev;
+	struct kvmppc_linear_info *li;
 
 	/* Allocate guest's hashed page table */
-	hpt = __get_free_pages(GFP_KERNEL|__GFP_ZERO|__GFP_REPEAT|__GFP_NOWARN,
-			       HPT_ORDER - PAGE_SHIFT);
+	li = kvm_alloc_hpt();
+	if (li) {
+		/* using preallocated memory */
+		hpt = (ulong)li->base_virt;
+		kvm->arch.hpt_li = li;
+	} else {
+		/* using dynamic memory */
+		hpt = __get_free_pages(GFP_KERNEL|__GFP_ZERO|__GFP_REPEAT|
+				       __GFP_NOWARN, HPT_ORDER - PAGE_SHIFT);
+	}
+
 	if (!hpt) {
 		pr_err("kvm_alloc_hpt: Couldn't alloc HPT\n");
 		return -ENOMEM;
@@ -88,7 +98,10 @@ void kvmppc_free_hpt(struct kvm *kvm)
 {
 	clear_bit(kvm->arch.lpid, lpid_inuse);
 	vfree(kvm->arch.revmap);
-	free_pages(kvm->arch.hpt_virt, HPT_ORDER - PAGE_SHIFT);
+	if (kvm->arch.hpt_li)
+		kvm_release_hpt(kvm->arch.hpt_li);
+	else
+		free_pages(kvm->arch.hpt_virt, HPT_ORDER - PAGE_SHIFT);
 }
 
 /* Bits in first HPTE dword for pagesize 4k, 64k or 16M */
arch/powerpc/kvm/book3s_hv_builtin.c +38 -1
index 7caed1d..bed1279 100644
@@ -19,6 +19,7 @@
 #include <asm/kvm_book3s.h>
 
 #define KVM_LINEAR_RMA		0
+#define KVM_LINEAR_HPT		1
 
 static void __init kvm_linear_init_one(ulong size, int count, int type);
 static struct kvmppc_linear_info *kvm_alloc_linear(int type);
@@ -97,6 +98,39 @@ void kvm_release_rma(struct kvmppc_linear_info *ri)
 }
 EXPORT_SYMBOL_GPL(kvm_release_rma);
 
+/*************** HPT *************/
+
+/*
+ * This maintains a list of big linear HPT tables that contain the GVA->HPA
+ * memory mappings. If we don't reserve those early on, we might not be able
+ * to get a big (usually 16MB) linear memory region from the kernel anymore.
+ */
+
+static unsigned long kvm_hpt_count;
+
+static int __init early_parse_hpt_count(char *p)
+{
+	if (!p)
+		return 1;
+
+	kvm_hpt_count = simple_strtoul(p, NULL, 0);
+
+	return 0;
+}
+early_param("kvm_hpt_count", early_parse_hpt_count);
+
+struct kvmppc_linear_info *kvm_alloc_hpt(void)
+{
+	return kvm_alloc_linear(KVM_LINEAR_HPT);
+}
+EXPORT_SYMBOL_GPL(kvm_alloc_hpt);
+
+void kvm_release_hpt(struct kvmppc_linear_info *li)
+{
+	kvm_release_linear(li);
+}
+EXPORT_SYMBOL_GPL(kvm_release_hpt);
+
 /*************** generic *************/
 
 static LIST_HEAD(free_linears);
@@ -114,7 +148,7 @@ static void __init kvm_linear_init_one(ulong size, int count, int type)
 	if (!count)
 		return;
 
-	typestr = (type == KVM_LINEAR_RMA) ? "RMA" : "";
+	typestr = (type == KVM_LINEAR_RMA) ? "RMA" : "HPT";
 
 	npages = size >> PAGE_SHIFT;
 	linear_info = alloc_bootmem(count * sizeof(struct kvmppc_linear_info));
@@ -173,6 +207,9 @@ static void kvm_release_linear(struct kvmppc_linear_info *ri)
  */
 void __init kvm_linear_init(void)
 {
+	/* HPT */
+	kvm_linear_init_one(1 << HPT_ORDER, kvm_hpt_count, KVM_LINEAR_HPT);
+
 	/* RMA */
 	/* Only do this on PPC970 in HV mode */
 	if (!cpu_has_feature(CPU_FTR_HVMODE) ||
Signature · 1 line
1.6.0.2
#4 lore raw

PATCH 2/3 KVM: PPC: Initialize linears with zeros

In reply to Alexander Graf

RMAs and HPT preallocated spaces should be zeroed, so we don't accidently leak information from previous VM executions.

---
 arch/powerpc/kvm/book3s_hv_builtin.c |    1 +
 1 files changed, 1 insertions(+), 0 deletions(-)
arch/powerpc/kvm/book3s_hv_builtin.c +1 -0
index 1c7e6ab..7caed1d 100644
@@ -152,6 +152,7 @@ static struct kvmppc_linear_info *kvm_alloc_linear(int type)
 		break;
 	}
 	spin_unlock(&linear_lock);
+	memset(ri->base_virt, 0, ri->npages << PAGE_SHIFT);
 	return ri;
 }
 
Signature · 1 line
1.6.0.2
#6 lore raw

PATCH 3/3 KVM: PPC: Add HPT preallocator

In reply to Alexander Graf

We're currently allocating 16MB of linear memory on demand when creating a guest. That does work some times, but finding 16MB of linear memory available in the system at runtime is definitely not a given.

So let's add another command line option similar to the RMA preallocator, that we can use to keep a pool of page tables around. Now, when a guest gets created it has a pretty low chance of receiving an OOM.

---
 arch/powerpc/include/asm/kvm_host.h  |    1 +
 arch/powerpc/include/asm/kvm_ppc.h   |    2 +
 arch/powerpc/kvm/book3s_64_mmu_hv.c  |   18 ++++++++++++++-
 arch/powerpc/kvm/book3s_hv_builtin.c |   39 +++++++++++++++++++++++++++++++++-
 4 files changed, 58 insertions(+), 2 deletions(-)
arch/powerpc/include/asm/kvm_host.h +1 -0
index 8221e71..1843d5d 100644
@@ -235,6 +235,7 @@ struct kvm_arch {
 	int slot_npages[KVM_MEM_SLOTS_NUM];
 	unsigned short last_vcpu[NR_CPUS];
 	struct kvmppc_vcore *vcores[KVM_MAX_VCORES];
+	struct kvmppc_linear_info *hpt_li;
 #endif /* CONFIG_KVM_BOOK3S_64_HV */
 };
 
arch/powerpc/include/asm/kvm_ppc.h +2 -0
index 1c37a2f..9d6dee0 100644
@@ -130,6 +130,8 @@ extern long kvm_vm_ioctl_allocate_rma(struct kvm *kvm,
 				struct kvm_allocate_rma *rma);
 extern struct kvmppc_linear_info *kvm_alloc_rma(void);
 extern void kvm_release_rma(struct kvmppc_linear_info *ri);
+extern struct kvmppc_linear_info *kvm_alloc_hpt(void);
+extern void kvm_release_hpt(struct kvmppc_linear_info *li);
 extern int kvmppc_core_init_vm(struct kvm *kvm);
 extern void kvmppc_core_destroy_vm(struct kvm *kvm);
 extern int kvmppc_core_prepare_memory_region(struct kvm *kvm,
arch/powerpc/kvm/book3s_64_mmu_hv.c +17 -1
index 783cd35..b3207c7 100644
@@ -44,10 +44,23 @@ long kvmppc_alloc_hpt(struct kvm *kvm)
 	unsigned long hpt;
 	unsigned long lpid;
 	struct revmap_entry *rev;
+	struct kvmppc_linear_info *li;
 
 	/* Allocate guest's hashed page table */
+
+	/* using preallocated memory */
+	li = kvm_alloc_hpt();
+	if (li) {
+		hpt = (ulong)li->base_virt;
+		kvm->arch.hpt_li = li;
+		goto has_hpt;
+	}
+
+	/* using dynamic memory */
 	hpt = __get_free_pages(GFP_KERNEL|__GFP_ZERO|__GFP_REPEAT|__GFP_NOWARN,
 			       HPT_ORDER - PAGE_SHIFT);
+
+has_hpt:
 	if (!hpt) {
 		pr_err("kvm_alloc_hpt: Couldn't alloc HPT\n");
 		return -ENOMEM;
@@ -88,7 +101,10 @@ void kvmppc_free_hpt(struct kvm *kvm)
 {
 	clear_bit(kvm->arch.lpid, lpid_inuse);
 	vfree(kvm->arch.revmap);
-	free_pages(kvm->arch.hpt_virt, HPT_ORDER - PAGE_SHIFT);
+	if (kvm->arch.hpt_li)
+		kvm_release_hpt(kvm->arch.hpt_li);
+	else
+		free_pages(kvm->arch.hpt_virt, HPT_ORDER - PAGE_SHIFT);
 }
 
 /* Bits in first HPTE dword for pagesize 4k, 64k or 16M */
arch/powerpc/kvm/book3s_hv_builtin.c +38 -1
index 7caed1d..bed1279 100644
@@ -19,6 +19,7 @@
 #include <asm/kvm_book3s.h>
 
 #define KVM_LINEAR_RMA		0
+#define KVM_LINEAR_HPT		1
 
 static void __init kvm_linear_init_one(ulong size, int count, int type);
 static struct kvmppc_linear_info *kvm_alloc_linear(int type);
@@ -97,6 +98,39 @@ void kvm_release_rma(struct kvmppc_linear_info *ri)
 }
 EXPORT_SYMBOL_GPL(kvm_release_rma);
 
+/*************** HPT *************/
+
+/*
+ * This maintains a list of big linear HPT tables that contain the GVA->HPA
+ * memory mappings. If we don't reserve those early on, we might not be able
+ * to get a big (usually 16MB) linear memory region from the kernel anymore.
+ */
+
+static unsigned long kvm_hpt_count;
+
+static int __init early_parse_hpt_count(char *p)
+{
+	if (!p)
+		return 1;
+
+	kvm_hpt_count = simple_strtoul(p, NULL, 0);
+
+	return 0;
+}
+early_param("kvm_hpt_count", early_parse_hpt_count);
+
+struct kvmppc_linear_info *kvm_alloc_hpt(void)
+{
+	return kvm_alloc_linear(KVM_LINEAR_HPT);
+}
+EXPORT_SYMBOL_GPL(kvm_alloc_hpt);
+
+void kvm_release_hpt(struct kvmppc_linear_info *li)
+{
+	kvm_release_linear(li);
+}
+EXPORT_SYMBOL_GPL(kvm_release_hpt);
+
 /*************** generic *************/
 
 static LIST_HEAD(free_linears);
@@ -114,7 +148,7 @@ static void __init kvm_linear_init_one(ulong size, int count, int type)
 	if (!count)
 		return;
 
-	typestr = (type == KVM_LINEAR_RMA) ? "RMA" : "";
+	typestr = (type == KVM_LINEAR_RMA) ? "RMA" : "HPT";
 
 	npages = size >> PAGE_SHIFT;
 	linear_info = alloc_bootmem(count * sizeof(struct kvmppc_linear_info));
@@ -173,6 +207,9 @@ static void kvm_release_linear(struct kvmppc_linear_info *ri)
  */
 void __init kvm_linear_init(void)
 {
+	/* HPT */
+	kvm_linear_init_one(1 << HPT_ORDER, kvm_hpt_count, KVM_LINEAR_HPT);
+
 	/* RMA */
 	/* Only do this on PPC970 in HV mode */
 	if (!cpu_has_feature(CPU_FTR_HVMODE) ||
Signature · 1 line
1.6.0.2
#7 lore raw

Re: [PATCH 3/3] KVM: PPC: Add HPT preallocator

Quoted 5 lines · Alexander Graf
arch/powerpc/kvm/book3s_64_mmu_hv.c +4 -0
 	/* Allocate guest's hashed page table */
+
+	/* using preallocated memory */
+	li = kvm_alloc_hpt();
+	if (li) {

arch/powerpc/kvm/book3s_64_mmu_hv.c long kvmppc_alloc_hpt(struct kvm *kvm) · line 63

Quoted 10 lines
arch/powerpc/kvm/book3s_64_mmu_hv.c +8 -0
+		hpt = (ulong)li->base_virt;
+		kvm->arch.hpt_li = li;
+		goto has_hpt;
+	}
+
+	/* using dynamic memory */
 	hpt = __get_free_pages(GFP_KERNEL|__GFP_ZERO|__GFP_REPEAT|__GFP_NOWARN,
 			       HPT_ORDER - PAGE_SHIFT);
+
+has_hpt:

This would look cleaner and be easier to follow if you used the "else" keyword. :)

Paul.

#8 lore raw

Re: [PATCH 3/3] KVM: PPC: Add HPT preallocator

Quoted 12 lines · Paul Mackerras
Quoted 10 lines · Alexander Graf
	/* Allocate guest's hashed page table */
arch/powerpc/kvm/book3s_64_mmu_hv.c +9 -0
+
+	/* using preallocated memory */
+	li = kvm_alloc_hpt();
+	if (li) {
+		hpt = (ulong)li->base_virt;
+		kvm->arch.hpt_li = li;
+		goto has_hpt;
+	}
+
Quoted 8 lines

Review

Quoted 5 lines
arch/powerpc/kvm/book3s_64_mmu_hv.c +1 -0
+	/* using dynamic memory */
	hpt = __get_free_pages(GFP_KERNEL|__GFP_ZERO|__GFP_REPEAT|__GFP_NOWARN,
			       HPT_ORDER - PAGE_SHIFT);
(quoted diff) +2 -0
+
+has_hpt:

This would look cleaner and be easier to follow if you used the "else" keyword. :)

Yeah, I just figured we might want to extend the "else" path by quite a bit. But I'll change it for now :). It certainly does look better when done that way in its current form.

Alex

#9 lore raw
In reply to Alexander Graf
Quoted 8 lines · Alexander Graf

While using the book3s hv code on a 970 system, we quickly ran into situations where we didn't have enough contiguous memory available to allocate a 16MB region for the page table we need to manage the guest's memory.

So I went ahead, cleaned up the code we currently use to preallocate RMAs and made it usable for HPT allocation. This patch set is the result of it. As a nice side effect, it also solves a potential security issue on 970 where we reused the RMA of previous guests without clearing them.

Nice patches. I just have one minor comment about the 3rd patch.

Now I should get in and implement support for other HPT sizes. 16MB is larger than really necessary for small guests (1GB of RAM or less), and really large guests could use a larger HPT.

Paul.