Skip to contentSkip to conversation

powerpc/kvm: Handle transparent hugepage in KVM

PATCH In review Aneesh Kumar K.V 2 participants 4 messages Jun 19, 2013

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

PATCH 1/1 powerpc/kvm: Handle transparent hugepage in KVM

From: "Aneesh Kumar K.V" <[email protected]>

We can find pte that are splitting while walking page tables. Return None pte in that case.

---
 arch/powerpc/include/asm/kvm_book3s_64.h | 51 ++++++++++++++++++--------------
 arch/powerpc/kvm/book3s_64_mmu_hv.c      |  7 +++--
 arch/powerpc/kvm/book3s_hv_rm_mmu.c      |  4 +--
 3 files changed, 34 insertions(+), 28 deletions(-)
arch/powerpc/include/asm/kvm_book3s_64.h +29 -22
index 9c1ff33..ce20f7e 100644
@@ -162,33 +162,40 @@ static inline int hpte_cache_flags_ok(unsigned long ptel, unsigned long io_type)
  * Lock and read a linux PTE.  If it's present and writable, atomically
  * set dirty and referenced bits and return the PTE, otherwise return 0.
  */
-static inline pte_t kvmppc_read_update_linux_pte(pte_t *p, int writing)
+static inline pte_t kvmppc_read_update_linux_pte(pte_t *ptep, int writing,
+						 unsigned int hugepage)
 {
-	pte_t pte, tmp;
-
-	/* wait until _PAGE_BUSY is clear then set it atomically */
-	__asm__ __volatile__ (
-		"1:	ldarx	%0,0,%3\n"
-		"	andi.	%1,%0,%4\n"
-		"	bne-	1b\n"
-		"	ori	%1,%0,%4\n"
-		"	stdcx.	%1,0,%3\n"
-		"	bne-	1b"
-		: "=&r" (pte), "=&r" (tmp), "=m" (*p)
-		: "r" (p), "i" (_PAGE_BUSY)
-		: "cc");
-
-	if (pte_present(pte)) {
-		pte = pte_mkyoung(pte);
-		if (writing && pte_write(pte))
-			pte = pte_mkdirty(pte);
-	}
+	pte_t old_pte, new_pte = __pte(0);
+repeat:
+	do {
+		old_pte = pte_val(*ptep);
+		/*
+		 * wait until _PAGE_BUSY is clear then set it atomically
+		 */
+		if (unlikely(old_pte & _PAGE_BUSY))
+			goto repeat;
+
+#ifdef CONFIG_TRANSPARENT_HUGEPAGE
+		/* If hugepage and is trans splitting return None */
+		if (unlikely(hugepage &&
+			     pmd_trans_splitting(pte_pmd(old_pte))))
+			return __pte(0);
+#endif
 
-	*p = pte;	/* clears _PAGE_BUSY */
+		/* If pte is not present return None */
+		if (unlikely(!(old_pte & _PAGE_PRESENT)))
+			return __pte(0);
 
-	return pte;
+		new_pte = pte_mkyoung(old_pte);
+		if (writing && pte_write(old_pte))
+			new_pte = pte_mkdirty(new_pte);
+
+	} while (old_pte != __cmpxchg_u64((unsigned long *)ptep,
+					  old_pte, new_pte));
+	return new_pte;
 }
 
+
 /* Return HPTE cache control bits corresponding to Linux pte bits */
 static inline unsigned long hpte_cache_bits(unsigned long pte_val)
 {
arch/powerpc/kvm/book3s_64_mmu_hv.c +4 -3
index 5880dfb..e1a9415 100644
@@ -675,6 +675,7 @@ int kvmppc_book3s_hv_page_fault(struct kvm_run *run, struct kvm_vcpu *vcpu,
 		}
 		/* if the guest wants write access, see if that is OK */
 		if (!writing && hpte_is_writable(r)) {
+			unsigned int shift;
 			pte_t *ptep, pte;
 
 			/*
@@ -683,9 +684,9 @@ int kvmppc_book3s_hv_page_fault(struct kvm_run *run, struct kvm_vcpu *vcpu,
 			 */
 			rcu_read_lock_sched();
 			ptep = find_linux_pte_or_hugepte(current->mm->pgd,
-							 hva, NULL);
-			if (ptep && pte_present(*ptep)) {
-				pte = kvmppc_read_update_linux_pte(ptep, 1);
+							 hva, &shift);
+			if (ptep) {
+				pte = kvmppc_read_update_linux_pte(ptep, 1, shift);
 				if (pte_write(pte))
 					write_ok = 1;
 			}
arch/powerpc/kvm/book3s_hv_rm_mmu.c +1 -3
index dcf892d..39ae723 100644
@@ -150,9 +150,7 @@ static pte_t lookup_linux_pte(pgd_t *pgdir, unsigned long hva,
 		*pte_sizep = PAGE_SIZE;
 	if (ps > *pte_sizep)
 		return __pte(0);
-	if (!pte_present(*ptep))
-		return __pte(0);
-	return kvmppc_read_update_linux_pte(ptep, writing);
+	return kvmppc_read_update_linux_pte(ptep, writing, shift);
 }
 
 static inline void unlock_hpte(unsigned long *hpte, unsigned long hpte_v)
Signature · 1 line
1.8.1.2
#1 lore raw
Quoted 4 lines · Aneesh Kumar K.V

From: "Aneesh Kumar K.V" <[email protected]>

We can find pte that are splitting while walking page tables. Return None pte in that case.

Can you expand on this more please. There are a lot of details below like removing a ldarx/stdcx loop that should be better described here.

Quoted 13 lines
---
 arch/powerpc/include/asm/kvm_book3s_64.h | 51 ++++++++++++++++++--------------
 arch/powerpc/kvm/book3s_64_mmu_hv.c      |  7 +++--
 arch/powerpc/kvm/book3s_hv_rm_mmu.c      |  4 +--
 3 files changed, 34 insertions(+), 28 deletions(-)
arch/powerpc/include/asm/kvm_book3s_64.h +0 -0
index 9c1ff33..ce20f7e 100644

arch/powerpc/include/asm/kvm_book3s_64.h static inline int hpte_cache_flags_ok(unsigned long ptel, unsigned long io_type) · line 163

Quoted 2 lines
arch/powerpc/include/asm/kvm_book3s_64.h +0 -0
@@ -162,33 +162,40 @@ static inline int hpte_cache_flags_ok(unsigned long ptel, unsigned long io_type)
  * Lock and read a linux PTE.  If it's present and writable, atomically
  * set dirty and referenced bits and return the PTE, otherwise return 0.

This is comment still valid now the ldarx/stdcx is gone?

Quoted 23 lines
arch/powerpc/include/asm/kvm_book3s_64.h +2 -19
  */
-static inline pte_t kvmppc_read_update_linux_pte(pte_t *p, int writing)
+static inline pte_t kvmppc_read_update_linux_pte(pte_t *ptep, int writing,
+						 unsigned int hugepage)
 {
-	pte_t pte, tmp;
-
-	/* wait until _PAGE_BUSY is clear then set it atomically */
-	__asm__ __volatile__ (
-		"1:	ldarx	%0,0,%3\n"
-		"	andi.	%1,%0,%4\n"
-		"	bne-	1b\n"
-		"	ori	%1,%0,%4\n"
-		"	stdcx.	%1,0,%3\n"
-		"	bne-	1b"
-		: "=&r" (pte), "=&r" (tmp), "=m" (*p)
-		: "r" (p), "i" (_PAGE_BUSY)
-		: "cc");
-
-	if (pte_present(pte)) {
-		pte = pte_mkyoung(pte);
-		if (writing && pte_write(pte))
-			pte = pte_mkdirty(pte);

arch/powerpc/include/asm/kvm_book3s_64.h static inline int hpte_cache_flags_ok(unsigned long ptel, unsigned long io_type) · line 176

Quoted 10 lines
arch/powerpc/include/asm/kvm_book3s_64.h +9 -1
-	}
+	pte_t old_pte, new_pte = __pte(0);
+repeat:
+	do {
+		old_pte = pte_val(*ptep);
+		/*
+		 * wait until _PAGE_BUSY is clear then set it atomically
+		 */
+		if (unlikely(old_pte & _PAGE_BUSY))
+			goto repeat;

continue here? Please don't create looping primitives.

arch/powerpc/include/asm/kvm_book3s_64.h static inline int hpte_cache_flags_ok(unsigned long ptel, unsigned long io_type) · line 181

Quoted 5 lines
arch/powerpc/include/asm/kvm_book3s_64.h +5 -0
+
+#ifdef CONFIG_TRANSPARENT_HUGEPAGE
+		/* If hugepage and is trans splitting return None */
+		if (unlikely(hugepage &&
+			     pmd_trans_splitting(pte_pmd(old_pte))))

Comment looks much like the code... seems redundant.

Quoted 9 lines
arch/powerpc/include/asm/kvm_book3s_64.h +5 -2
+			return __pte(0);
+#endif
 
-	*p = pte;	/* clears _PAGE_BUSY */
+		/* If pte is not present return None */
+		if (unlikely(!(old_pte & _PAGE_PRESENT)))
+			return __pte(0);
 
-	return pte;

arch/powerpc/include/asm/kvm_book3s_64.h static inline int hpte_cache_flags_ok(unsigned long ptel, unsigned long io_type) · line 198

Quoted 10 lines
arch/powerpc/include/asm/kvm_book3s_64.h +8 -0
+		new_pte = pte_mkyoung(old_pte);
+		if (writing && pte_write(old_pte))
+			new_pte = pte_mkdirty(new_pte);
+
+	} while (old_pte != __cmpxchg_u64((unsigned long *)ptep,
+					  old_pte, new_pte));
+	return new_pte;
 }
 
+

Whitespace

Quoted 33 lines
arch/powerpc/include/asm/kvm_book3s_64.h +0 -0
 /* Return HPTE cache control bits corresponding to Linux pte bits */
 static inline unsigned long hpte_cache_bits(unsigned long pte_val)
 {
arch/powerpc/kvm/book3s_64_mmu_hv.c +4 -3
index 5880dfb..e1a9415 100644
@@ -675,6 +675,7 @@ int kvmppc_book3s_hv_page_fault(struct kvm_run *run, struct kvm_vcpu *vcpu,
 		}
 		/* if the guest wants write access, see if that is OK */
 		if (!writing && hpte_is_writable(r)) {
+			unsigned int shift;
 			pte_t *ptep, pte;
 
 			/*
@@ -683,9 +684,9 @@ int kvmppc_book3s_hv_page_fault(struct kvm_run *run, struct kvm_vcpu *vcpu,
 			 */
 			rcu_read_lock_sched();
 			ptep = find_linux_pte_or_hugepte(current->mm->pgd,
-							 hva, NULL);
-			if (ptep && pte_present(*ptep)) {
-				pte = kvmppc_read_update_linux_pte(ptep, 1);
+							 hva, &shift);
+			if (ptep) {
+				pte = kvmppc_read_update_linux_pte(ptep, 1, shift);
 				if (pte_write(pte))
 					write_ok = 1;
 			}
arch/powerpc/kvm/book3s_hv_rm_mmu.c +0 -0
index dcf892d..39ae723 100644

arch/powerpc/kvm/book3s_hv_rm_mmu.c static pte_t lookup_linux_pte(pgd_t *pgdir, unsigned long hva, · line 153

Quoted 7 lines
arch/powerpc/kvm/book3s_hv_rm_mmu.c +1 -3
@@ -150,9 +150,7 @@ static pte_t lookup_linux_pte(pgd_t *pgdir, unsigned long hva,
 		*pte_sizep = PAGE_SIZE;
 	if (ps > *pte_sizep)
 		return __pte(0);
-	if (!pte_present(*ptep))
-		return __pte(0);
-	return kvmppc_read_update_linux_pte(ptep, writing);
+	return kvmppc_read_update_linux_pte(ptep, writing, shift);

'shift' goes into the new 'hugepage' parameter? Doesn't seem logical? Can we harmonise the name to make it less confusing?

Mikey

Quoted 11 lines
arch/powerpc/kvm/book3s_hv_rm_mmu.c +0 -0
 }
 
 static inline void unlock_hpte(unsigned long *hpte, unsigned long hpte_v)
Signature · 6 lines
1.8.1.2

_______________________________________________
Linuxppc-dev mailing list
[email protected]
https://lists.ozlabs.org/listinfo/linuxppc-dev
#2 lore raw
Quoted 18 lines · Michael Neuling
Quoted 4 lines · Aneesh Kumar K.V

From: "Aneesh Kumar K.V" <[email protected]>

We can find pte that are splitting while walking page tables. Return None pte in that case.

Can you expand on this more please. There are a lot of details below like removing a ldarx/stdcx loop that should be better described here.

Quoted 8 lines
---
 arch/powerpc/include/asm/kvm_book3s_64.h | 51 ++++++++++++++++++--------------
 arch/powerpc/kvm/book3s_64_mmu_hv.c      |  7 +++--
 arch/powerpc/kvm/book3s_hv_rm_mmu.c      |  4 +--
 3 files changed, 34 insertions(+), 28 deletions(-)
Quoted 9 lines

arch/powerpc/include/asm/kvm_book3s_64.h static inline int hpte_cache_flags_ok(unsigned long ptel, unsigned long io_type) · line 163

Quoted 7 lines
arch/powerpc/include/asm/kvm_book3s_64.h +0 -0
index 9c1ff33..ce20f7e 100644
@@ -162,33 +162,40 @@ static inline int hpte_cache_flags_ok(unsigned long ptel, unsigned long io_type)
  * Lock and read a linux PTE.  If it's present and writable, atomically
  * set dirty and referenced bits and return the PTE, otherwise return 0.

This is comment still valid now the ldarx/stdcx is gone?

In a way yes. Instead of lock and read as it was before, it is now done via cmpxchg which still use ldarx/stdcx

Quoted 28 lines
Quoted 28 lines
arch/powerpc/include/asm/kvm_book3s_64.h +5 -20
  */
-static inline pte_t kvmppc_read_update_linux_pte(pte_t *p, int writing)
+static inline pte_t kvmppc_read_update_linux_pte(pte_t *ptep, int writing,
+						 unsigned int hugepage)
 {
-	pte_t pte, tmp;
-
-	/* wait until _PAGE_BUSY is clear then set it atomically */
-	__asm__ __volatile__ (
-		"1:	ldarx	%0,0,%3\n"
-		"	andi.	%1,%0,%4\n"
-		"	bne-	1b\n"
-		"	ori	%1,%0,%4\n"
-		"	stdcx.	%1,0,%3\n"
-		"	bne-	1b"
-		: "=&r" (pte), "=&r" (tmp), "=m" (*p)
-		: "r" (p), "i" (_PAGE_BUSY)
-		: "cc");
-
-	if (pte_present(pte)) {
-		pte = pte_mkyoung(pte);
-		if (writing && pte_write(pte))
-			pte = pte_mkdirty(pte);
-	}
+	pte_t old_pte, new_pte = __pte(0);
+repeat:
+	do {
Quoted 8 lines

arch/powerpc/include/asm/kvm_book3s_64.h static inline int hpte_cache_flags_ok(unsigned long ptel, unsigned long io_type) · line 176

Quoted 6 lines
arch/powerpc/include/asm/kvm_book3s_64.h +6 -0
+		old_pte = pte_val(*ptep);
+		/*
+		 * wait until _PAGE_BUSY is clear then set it atomically
+		 */
+		if (unlikely(old_pte & _PAGE_BUSY))
+			goto repeat;

continue here? Please don't create looping primitives.

No that would be wrong. (I did that in an earlier version :).We really don't want the below cmpxchg to run if we find _PAGE_BUSY.

Quoted 66 lines

arch/powerpc/include/asm/kvm_book3s_64.h static inline int hpte_cache_flags_ok(unsigned long ptel, unsigned long io_type) · line 181

Quoted 6 lines
arch/powerpc/include/asm/kvm_book3s_64.h +5 -0
+
+#ifdef CONFIG_TRANSPARENT_HUGEPAGE
+		/* If hugepage and is trans splitting return None */
+		if (unlikely(hugepage &&
+			     pmd_trans_splitting(pte_pmd(old_pte))))

Comment looks much like the code... seems redundant.

Quoted 9 lines
arch/powerpc/include/asm/kvm_book3s_64.h +5 -2
+			return __pte(0);
+#endif
 
-	*p = pte;	/* clears _PAGE_BUSY */
+		/* If pte is not present return None */
+		if (unlikely(!(old_pte & _PAGE_PRESENT)))
+			return __pte(0);
 
-	return pte;

arch/powerpc/include/asm/kvm_book3s_64.h static inline int hpte_cache_flags_ok(unsigned long ptel, unsigned long io_type) · line 198

Quoted 10 lines
arch/powerpc/include/asm/kvm_book3s_64.h +8 -0
+		new_pte = pte_mkyoung(old_pte);
+		if (writing && pte_write(old_pte))
+			new_pte = pte_mkdirty(new_pte);
+
+	} while (old_pte != __cmpxchg_u64((unsigned long *)ptep,
+					  old_pte, new_pte));
+	return new_pte;
 }
 
+

Whitespace

Quoted 35 lines
arch/powerpc/include/asm/kvm_book3s_64.h +0 -0
 /* Return HPTE cache control bits corresponding to Linux pte bits */
 static inline unsigned long hpte_cache_bits(unsigned long pte_val)
 {
arch/powerpc/kvm/book3s_64_mmu_hv.c +4 -3
index 5880dfb..e1a9415 100644
@@ -675,6 +675,7 @@ int kvmppc_book3s_hv_page_fault(struct kvm_run *run, struct kvm_vcpu *vcpu,
 		}
 		/* if the guest wants write access, see if that is OK */
 		if (!writing && hpte_is_writable(r)) {
+			unsigned int shift;
 			pte_t *ptep, pte;
 
 			/*
@@ -683,9 +684,9 @@ int kvmppc_book3s_hv_page_fault(struct kvm_run *run, struct kvm_vcpu *vcpu,
 			 */
 			rcu_read_lock_sched();
 			ptep = find_linux_pte_or_hugepte(current->mm->pgd,
-							 hva, NULL);
-			if (ptep && pte_present(*ptep)) {
-				pte = kvmppc_read_update_linux_pte(ptep, 1);
+							 hva, &shift);
+			if (ptep) {
+				pte = kvmppc_read_update_linux_pte(ptep, 1, shift);
 				if (pte_write(pte))
 					write_ok = 1;
 			}
arch/powerpc/kvm/book3s_hv_rm_mmu.c +0 -0
index dcf892d..39ae723 100644
@@ -150,9 +150,7 @@ static pte_t lookup_linux_pte(pgd_t *pgdir, unsigned long hva,
 		*pte_sizep = PAGE_SIZE;
 	if (ps > *pte_sizep)
Quoted 9 lines

arch/powerpc/kvm/book3s_hv_rm_mmu.c static pte_t lookup_linux_pte(pgd_t *pgdir, unsigned long hva, · line 153

Quoted 5 lines
arch/powerpc/kvm/book3s_hv_rm_mmu.c +1 -3
 		return __pte(0);
-	if (!pte_present(*ptep))
-		return __pte(0);
-	return kvmppc_read_update_linux_pte(ptep, writing);
+	return kvmppc_read_update_linux_pte(ptep, writing, shift);

'shift' goes into the new 'hugepage' parameter? Doesn't seem logical? Can we harmonise the name to make it less confusing?

it is actually the shift bits represending hugepage size. We set it to 0 if we don't find hugepage in find_linux_pte_or_hugepte. May be something like hugepage_shift is better ?

-aneesh

#3 lore raw
Quoted 10 lines
Quoted 7 lines

arch/powerpc/include/asm/kvm_book3s_64.h static inline int hpte_cache_flags_ok(unsigned long ptel, unsigned long io_type) · line 163

Quoted 5 lines
arch/powerpc/include/asm/kvm_book3s_64.h +0 -0
@@ -162,33 +162,40 @@ static inline int hpte_cache_flags_ok(unsigned long ptel, unsigned long io_type)
  * Lock and read a linux PTE.  If it's present and writable, atomically
  * set dirty and referenced bits and return the PTE, otherwise return 0.

This is comment still valid now the ldarx/stdcx is gone?

In a way yes. Instead of lock and read as it was before, it is now done via cmpxchg which still use ldarx/stdcx

OK, maybe you can update to reflect that.

Quoted 12 lines
Quoted 11 lines

arch/powerpc/include/asm/kvm_book3s_64.h static inline int hpte_cache_flags_ok(unsigned long ptel, unsigned long io_type) · line 176

Quoted 9 lines
arch/powerpc/include/asm/kvm_book3s_64.h +9 -0
+	pte_t old_pte, new_pte = __pte(0);
+repeat:
+	do {
+		old_pte = pte_val(*ptep);
+		/*
+		 * wait until _PAGE_BUSY is clear then set it atomically
+		 */
+		if (unlikely(old_pte & _PAGE_BUSY))
+			goto repeat;

continue here? Please don't create looping primitives.

Quoted 2 lines

No that would be wrong. (I did that in an earlier version :).We really don't want the below cmpxchg to run if we find _PAGE_BUSY.

How about something like this then?

while (1) {
if (unlikely(old_pte & _PAGE_BUSY))
continue;
.....

      if cmpxchg(foo)
      	 break;
}
Quoted 51 lines
Quoted 34 lines

arch/powerpc/include/asm/kvm_book3s_64.h static inline int hpte_cache_flags_ok(unsigned long ptel, unsigned long io_type) · line 181

Quoted 7 lines
arch/powerpc/include/asm/kvm_book3s_64.h +5 -0
+
+#ifdef CONFIG_TRANSPARENT_HUGEPAGE
+		/* If hugepage and is trans splitting return None */
+		if (unlikely(hugepage &&
+			     pmd_trans_splitting(pte_pmd(old_pte))))

Comment looks much like the code... seems redundant.

Quoted 9 lines
arch/powerpc/include/asm/kvm_book3s_64.h +5 -2
+			return __pte(0);
+#endif
 
-	*p = pte;	/* clears _PAGE_BUSY */
+		/* If pte is not present return None */
+		if (unlikely(!(old_pte & _PAGE_PRESENT)))
+			return __pte(0);
 
-	return pte;

arch/powerpc/include/asm/kvm_book3s_64.h static inline int hpte_cache_flags_ok(unsigned long ptel, unsigned long io_type) · line 198

Quoted 10 lines
arch/powerpc/include/asm/kvm_book3s_64.h +8 -0
+		new_pte = pte_mkyoung(old_pte);
+		if (writing && pte_write(old_pte))
+			new_pte = pte_mkdirty(new_pte);
+
+	} while (old_pte != __cmpxchg_u64((unsigned long *)ptep,
+					  old_pte, new_pte));
+	return new_pte;
 }
 
+

Whitespace

Quoted 16 lines

arch/powerpc/kvm/book3s_hv_rm_mmu.c static pte_t lookup_linux_pte(pgd_t *pgdir, unsigned long hva, · line 153

Quoted 12 lines
arch/powerpc/kvm/book3s_hv_rm_mmu.c +1 -3
index dcf892d..39ae723 100644
@@ -150,9 +150,7 @@ static pte_t lookup_linux_pte(pgd_t *pgdir, unsigned long hva,
 		*pte_sizep = PAGE_SIZE;
 	if (ps > *pte_sizep)
 		return __pte(0);
-	if (!pte_present(*ptep))
-		return __pte(0);
-	return kvmppc_read_update_linux_pte(ptep, writing);
+	return kvmppc_read_update_linux_pte(ptep, writing, shift);

'shift' goes into the new 'hugepage' parameter? Doesn't seem logical? Can we harmonise the name to make it less confusing?

Quoted 3 lines

it is actually the shift bits represending hugepage size. We set it to 0 if we don't find hugepage in find_linux_pte_or_hugepte. May be something like hugepage_shift is better ?

Sure.

Mikey