Skip to contentSkip to conversation

KVM: PPC: E500: Support hugetlbfs

PATCH In review Alexander Graf 3 participants 7 messages Sep 19, 2011 – Sep 26, 2011

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

PATCH 1/1 KVM: PPC: E500: Support hugetlbfs

With hugetlbfs support emerging on e500, we should also support KVM backing its guest memory by it.

This patch adds support for hugetlbfs into the e500 shadow mmu code.

---
 arch/powerpc/kvm/e500_tlb.c |   22 ++++++++++++++++++++++
 1 files changed, 22 insertions(+), 0 deletions(-)
arch/powerpc/kvm/e500_tlb.c +22 -0
index ec17148..64f75eb 100644
@@ -24,6 +24,7 @@
 #include <linux/sched.h>
 #include <linux/rwsem.h>
 #include <linux/vmalloc.h>
+#include <linux/hugetlb.h>
 #include <asm/kvm_ppc.h>
 #include <asm/kvm_e500.h>
 
@@ -673,13 +674,34 @@ static inline void kvmppc_e500_shadow_map(struct kvmppc_vcpu_e500 *vcpu_e500,
 				pfn &= ~(tsize_pages - 1);
 				break;
 			}
+		} else if (vma && hva >= vma->vm_start &&
+                           (vma->vm_flags & VM_HUGETLB)) {
+			unsigned long psize = vma_kernel_pagesize(vma);
+			int lz;
+
+			tsize = (gtlbe->mas1 & MAS1_TSIZE_MASK) >>
+				MAS1_TSIZE_SHIFT;
+
+			/*
+			 * e500 doesn't implement the lowest tsize bit,
+			 * or 1K pages.
+			 */
+			tsize = max(BOOK3E_PAGESZ_4K, tsize & ~1);
+
+			/* take the smallest page size that satisfies host and
+			   guest mapping */
+			asm (PPC_CNTLZL "%0,%1" : "=r" (lz) : "r" (psize));
+			tsize = min(21 - lz, tsize);
 		}
 
 		up_read(&current->mm->mmap_sem);
 	}
 
 	if (likely(!pfnmap)) {
+		unsigned long tsize_pages = 1 << (tsize - 2);
 		pfn = gfn_to_pfn_memslot(vcpu_e500->vcpu.kvm, slot, gfn);
+		pfn &= ~(tsize_pages - 1);
+		gvaddr &= ~((tsize_pages << PAGE_SHIFT) - 1);
 		if (is_error_pfn(pfn)) {
 			printk(KERN_ERR "Couldn't get real page for gfn %lx!\n",
 					(long)gfn);
Signature · 1 line
1.6.0.2
#1 lore raw

arch/powerpc/kvm/e500_tlb.c static inline void kvmppc_e500_shadow_map(struct kvmppc_vcpu_e500 *vcpu_e500, · line 692

Quoted 8 lines · Alexander Graf
arch/powerpc/kvm/e500_tlb.c +8 -0
+			/*
+			 * e500 doesn't implement the lowest tsize bit,
+			 * or 1K pages.
+			 */
+			tsize = max(BOOK3E_PAGESZ_4K, tsize & ~1);
+
+			/* take the smallest page size that satisfies host and
+			   guest mapping */

kernel comment style...

I don't personally care much, but this leaves a checkpatch complaint for the next person to modify the comment. Such as to make it say "take the largest page size that...". :-)

arch/powerpc/kvm/e500_tlb.c static inline void kvmppc_e500_shadow_map(struct kvmppc_vcpu_e500 *vcpu_e500, · line 694

Quoted 2 lines
arch/powerpc/kvm/e500_tlb.c +2 -0
+			asm (PPC_CNTLZL "%0,%1" : "=r" (lz) : "r" (psize));
+			tsize = min(21 - lz, tsize);

No need to open-code the cntlz and subtract-from-21:

	tsize = min(ilog2(psize) - 10, tsize);

	/*
	 * e500 doesn't implement the lowest tsize bit,
	 * or 1K pages.
	 */
	tsize = max(BOOK3E_PAGESZ_4K, tsize & ~1);

There's still an open-coded subtraction of 10, but that relates more straightforwardly to the definition of tsize (and could be factored out into a size-to-tsize function).

arch/powerpc/kvm/e500_tlb.c static inline void kvmppc_e500_shadow_map(struct kvmppc_vcpu_e500 *vcpu_e500, · line 701

Quoted 7 lines
arch/powerpc/kvm/e500_tlb.c +1 -0
 		}
 
 		up_read(&current->mm->mmap_sem);
 	}
 
 	if (likely(!pfnmap)) {
+		unsigned long tsize_pages = 1 << (tsize - 2);

1 << (tsize + 10 - PAGE_SHIFT);

arch/powerpc/kvm/e500_tlb.c static inline void kvmppc_e500_shadow_map(struct kvmppc_vcpu_e500 *vcpu_e500, · line 705

Quoted 4 lines
arch/powerpc/kvm/e500_tlb.c +2 -0
 		pfn = gfn_to_pfn_memslot(vcpu_e500->vcpu.kvm, slot, gfn);
+		pfn &= ~(tsize_pages - 1);
+		gvaddr &= ~((tsize_pages << PAGE_SHIFT) - 1);
 		if (is_error_pfn(pfn)) {

Won't the masking of pfn affect is_error_pfn()?

-Scott

#4 lore raw
In reply to Alexander Graf
Quoted 33 lines

-----Original Message-----
From: [email protected] [mailto:[email protected]] On Behalf Of Alexander Graf
Sent: Tuesday, September 20, 2011 7:36 AM
To: [email protected]
Cc: [email protected]
Subject: [PATCH] KVM: PPC: E500: Support hugetlbfs

With hugetlbfs support emerging on e500, we should also support KVM backing its guest memory by it.

This patch adds support for hugetlbfs into the e500 shadow mmu code.

---
 arch/powerpc/kvm/e500_tlb.c |   22 ++++++++++++++++++++++
 1 files changed, 22 insertions(+), 0 deletions(-)
arch/powerpc/kvm/e500_tlb.c +1 -0
index ec17148..64f75eb 100644
@@ -24,6 +24,7 @@
 #include <linux/sched.h>
 #include <linux/rwsem.h>
 #include <linux/vmalloc.h>
+#include <linux/hugetlb.h>
 #include <asm/kvm_ppc.h>
 #include <asm/kvm_e500.h>
 

arch/powerpc/kvm/e500_tlb.c static inline void · line 679

Quoted 6 lines
arch/powerpc/kvm/e500_tlb.c +2 -0
@@ -673,13 +674,34 @@ static inline void 
 kvmppc_e500_shadow_map(struct kvmppc_vcpu_e500 *vcpu_e500,
 				pfn &= ~(tsize_pages - 1);
 				break;
 			}
+		} else if (vma && hva >= vma->vm_start &&
+                           (vma->vm_flags & VM_HUGETLB)) {

Why check (vma && hva >= vma->vm_start) twice?

Thanks,
Yu

#2 lore raw
In reply to Scott Wood
Quoted 12 lines · Scott Wood

arch/powerpc/kvm/e500_tlb.c static inline void kvmppc_e500_shadow_map(struct kvmppc_vcpu_e500 *vcpu_e500, · line 692

Quoted 8 lines · Alexander Graf
arch/powerpc/kvm/e500_tlb.c +8 -0
+			/*
+			 * e500 doesn't implement the lowest tsize bit,
+			 * or 1K pages.
+			 */
+			tsize = max(BOOK3E_PAGESZ_4K, tsize & ~1);
+
+			/* take the smallest page size that satisfies host and
+			   guest mapping */

kernel comment style...

Quoted 3 lines

I don't personally care much, but this leaves a checkpatch complaint for the next person to modify the comment. Such as to make it say "take the largest page size that...". :-)

Ahem :). Changed.

Quoted 14 lines

arch/powerpc/kvm/e500_tlb.c static inline void kvmppc_e500_shadow_map(struct kvmppc_vcpu_e500 *vcpu_e500, · line 694

Quoted 3 lines
arch/powerpc/kvm/e500_tlb.c +2 -0
+			asm (PPC_CNTLZL "%0,%1" : "=r" (lz) : "r" (psize));
+			tsize = min(21 - lz, tsize);

No need to open-code the cntlz and subtract-from-21:

	tsize = min(ilog2(psize) - 10, tsize);

	/*
	 * e500 doesn't implement the lowest tsize bit,
	 * or 1K pages.
	 */
	tsize = max(BOOK3E_PAGESZ_4K, tsize & ~1);
Quoted 3 lines

There's still an open-coded subtraction of 10, but that relates more straightforwardly to the definition of tsize (and could be factored out into a size-to-tsize function).

Yeah, no need to micro-optimized those few bits. The reason I used the asm statement was that I copied the hugetlbfs code which does it that way :).

Quoted 10 lines

arch/powerpc/kvm/e500_tlb.c static inline void kvmppc_e500_shadow_map(struct kvmppc_vcpu_e500 *vcpu_e500, · line 701

Quoted 8 lines
		}

		up_read(&current->mm->mmap_sem);
	}

	if (likely(!pfnmap)) {
arch/powerpc/kvm/e500_tlb.c +1 -0
+		unsigned long tsize_pages = 1 << (tsize - 2);

1 << (tsize + 10 - PAGE_SHIFT);

Are we getting variable page sizes anytime soon? Will change it nevertheless, just curious :).

Quoted 7 lines
Quoted 5 lines
		pfn = gfn_to_pfn_memslot(vcpu_e500->vcpu.kvm, slot, gfn);
arch/powerpc/kvm/e500_tlb.c +2 -0
+		pfn &= ~(tsize_pages - 1);
+		gvaddr &= ~((tsize_pages << PAGE_SHIFT) - 1);
		if (is_error_pfn(pfn)) {

Won't the masking of pfn affect is_error_pfn()?

Yes, it would. Good catch!

Alex

#5 lore raw
In reply to Liu Yu-B13201
Quoted 32 lines · On 22.09.2011, at 08:50, Liu Yu-B13201 wrote:
Quoted 32 lines

-----Original Message-----
From: [email protected] [mailto:[email protected]] On Behalf Of Alexander Graf
Sent: Tuesday, September 20, 2011 7:36 AM
To: [email protected]
Cc: [email protected]
Subject: [PATCH] KVM: PPC: E500: Support hugetlbfs

With hugetlbfs support emerging on e500, we should also support KVM backing its guest memory by it.

This patch adds support for hugetlbfs into the e500 shadow mmu code.

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

1 files changed, 22 insertions(+), 0 deletions(-)
arch/powerpc/kvm/e500_tlb.c +1 -0
index ec17148..64f75eb 100644
@@ -24,6 +24,7 @@
 #include <linux/sched.h>
 #include <linux/rwsem.h>
 #include <linux/vmalloc.h>
+#include <linux/hugetlb.h>
 #include <asm/kvm_ppc.h>
 #include <asm/kvm_e500.h>
 
Quoted 9 lines

arch/powerpc/kvm/e500_tlb.c static inline void · line 679

Quoted 7 lines
arch/powerpc/kvm/e500_tlb.c +2 -0
@@ -673,13 +674,34 @@ static inline void 
 kvmppc_e500_shadow_map(struct kvmppc_vcpu_e500 *vcpu_e500,
 				pfn &= ~(tsize_pages - 1);
 				break;
 			}
+		} else if (vma && hva >= vma->vm_start &&
+                           (vma->vm_flags & VM_HUGETLB)) {

Why check (vma && hva >= vma->vm_start) twice?

What would you do? :)

In fact, I only copied the vm_start condition from the pfn code. Scott, why do we have to check this in the first place? We're calling find_vma. Can that return a vma that does not cover the hva we're passing in?

Alex

#6 lore raw
Quoted 45 lines · Alexander Graf
Quoted 32 lines · On 22.09.2011, at 08:50, Liu Yu-B13201 wrote:
Quoted 32 lines

-----Original Message-----
From: [email protected] [mailto:[email protected]] On Behalf Of Alexander Graf
Sent: Tuesday, September 20, 2011 7:36 AM
To: [email protected]
Cc: [email protected]
Subject: [PATCH] KVM: PPC: E500: Support hugetlbfs

With hugetlbfs support emerging on e500, we should also support KVM backing its guest memory by it.

This patch adds support for hugetlbfs into the e500 shadow mmu code.

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

1 files changed, 22 insertions(+), 0 deletions(-)
arch/powerpc/kvm/e500_tlb.c +1 -0
index ec17148..64f75eb 100644
@@ -24,6 +24,7 @@
 #include <linux/sched.h>
 #include <linux/rwsem.h>
 #include <linux/vmalloc.h>
+#include <linux/hugetlb.h>
 #include <asm/kvm_ppc.h>
 #include <asm/kvm_e500.h>
 
Quoted 9 lines

arch/powerpc/kvm/e500_tlb.c static inline void · line 679

Quoted 7 lines
arch/powerpc/kvm/e500_tlb.c +2 -0
@@ -673,13 +674,34 @@ static inline void 
 kvmppc_e500_shadow_map(struct kvmppc_vcpu_e500 *vcpu_e500,
 				pfn &= ~(tsize_pages - 1);
 				break;
 			}
+		} else if (vma && hva >= vma->vm_start &&
+                           (vma->vm_flags & VM_HUGETLB)) {

Why check (vma && hva >= vma->vm_start) twice?

Quoted 1 line

What would you do? :)

I think he's just complaining about doing the check twice, in which case the answer could be "it avoids extra indentation and the compiler should be able to factor out the common subexpression".

Quoted 4 lines

In fact, I only copied the vm_start condition from the pfn code. Scott, why do we have to check this in the first place? We're calling find_vma. Can that return a vma that does not cover the hva we're passing in?

Yes, it can. From find_vma():

/* Look up the first VMA which satisfies addr < vm_end, NULL if none. */

You'll find similar checks in a lot of other places where find_vma() is used.

-Scott

#3 lore raw
In reply to Alexander Graf
Quoted 15 lines · Alexander Graf
Quoted 14 lines · Scott Wood

arch/powerpc/kvm/e500_tlb.c static inline void kvmppc_e500_shadow_map(struct kvmppc_vcpu_e500 *vcpu_e500, · line 694

Quoted 2 lines · Alexander Graf
arch/powerpc/kvm/e500_tlb.c +2 -0
+			asm (PPC_CNTLZL "%0,%1" : "=r" (lz) : "r" (psize));
+			tsize = min(21 - lz, tsize);

No need to open-code the cntlz and subtract-from-21:

	tsize = min(ilog2(psize) - 10, tsize);

	/*
	 * e500 doesn't implement the lowest tsize bit,
	 * or 1K pages.
	 */
	tsize = max(BOOK3E_PAGESZ_4K, tsize & ~1);
Quoted 5 lines
Quoted 3 lines

There's still an open-coded subtraction of 10, but that relates more straightforwardly to the definition of tsize (and could be factored out into a size-to-tsize function).

Yeah, no need to micro-optimized those few bits. The reason I used the asm statement was that I copied the hugetlbfs code which does it that way :).

The "21 - lz" thing is broken on 64-bit, FWIW. It works by chance in the hugetlbfs code (and in settlbcam) because it results in tsize being too low by 32, which only affects bits that subsequently get masked off.

Quoted 11 lines
Quoted 9 lines

arch/powerpc/kvm/e500_tlb.c static inline void kvmppc_e500_shadow_map(struct kvmppc_vcpu_e500 *vcpu_e500, · line 701

Quoted 7 lines
		}

		up_read(&current->mm->mmap_sem);
	}

	if (likely(!pfnmap)) {
arch/powerpc/kvm/e500_tlb.c +1 -0
+		unsigned long tsize_pages = 1 << (tsize - 2);

1 << (tsize + 10 - PAGE_SHIFT);

Are we getting variable page sizes anytime soon? Will change it nevertheless, just curious :).

Nothing imminent on our chips AFAIK, but there's some 64K page support in Linux for IBM's book3e chips, and it's not nice to hardcode (especially in a hidden way) regardless.

-Scott