Skip to contentSkip to conversation

Pte xchg optimization.patch

PATCH In review Zachary Amsden 2 participants 3 messages Apr 12, 2007 virtio core

lore
Share & tools
lore
#0 lore raw

PATCH 3/4 Pte xchg optimization.patch

In situations where page table updates need only be made locally, and there is no cross-processor A/D bit races involved, we need not use the heavyweight xchg instruction to atomically fetch and clear page table entries. Instead, we can just read and clear them directly.

This introduces a neat optimization for non-SMP kernels; drop the atomic xchg operations from page table updates.

Thanks to Michel Lespinasse for noting this potential optimization.

include/asm-i386/pgtable-2level.h +14 -0
diff -r 47495b2532b3 include/asm-i386/pgtable-2level.h
@@ -41,10 +41,24 @@ static inline void native_pte_clear(stru
 	*xp = __pte(0);
 }
 
+/* local pte updates need not use xchg for locking */
+static inline pte_t native_local_ptep_get_and_clear(pte_t *ptep)
+{
+	pte_t res;
+
+	res = *ptep;
+	native_pte_clear(NULL, 0, ptep);
+	return res;
+}
+
+#ifdef CONFIG_SMP
 static inline pte_t native_ptep_get_and_clear(pte_t *xp)
 {
 	return __pte(xchg(&xp->pte_low, 0));
 }
+#else
+#define native_ptep_get_and_clear(xp) native_local_ptep_get_and_clear(xp)
+#endif
 
 #define pte_page(x)		pfn_to_page(pte_pfn(x))
 #define pte_none(x)		(!(x).pte_low)
include/asm-i386/pgtable-3level.h +14 -0
diff -r 47495b2532b3 include/asm-i386/pgtable-3level.h
@@ -139,6 +139,17 @@ static inline void pud_clear (pud_t * pu
 #define pmd_offset(pud, address) ((pmd_t *) pud_page(*(pud)) + \
 			pmd_index(address))
 
+/* local pte updates need not use xchg for locking */
+static inline pte_t native_local_ptep_get_and_clear(pte_t *ptep)
+{
+	pte_t res;
+	
+	res = *ptep;
+	native_pte_clear(NULL, 0, ptep);
+	return res;
+}
+
+#ifdef CONFIG_SMP
 static inline pte_t native_ptep_get_and_clear(pte_t *ptep)
 {
 	pte_t res;
@@ -150,6 +161,9 @@ static inline pte_t native_ptep_get_and_
 
 	return res;
 }
+#else
+#define native_ptep_get_and_clear(xp) native_local_ptep_get_and_clear(xp)
+#endif
 
 #define __HAVE_ARCH_PTE_SAME
 static inline int pte_same(pte_t a, pte_t b)
#1 lore raw
Quoted 7 lines · Zachary Amsden

In situations where page table updates need only be made locally, and there is no cross-processor A/D bit races involved, we need not use the heavyweight xchg instruction to atomically fetch and clear page table entries. Instead, we can just read and clear them directly.

This introduces a neat optimization for non-SMP kernels; drop the atomic xchg operations from page table updates.

Would it be feasible to do this using the smp_alternatives stuff? Given many distros are now shipping SMP-only kernels, it'd be nice to get that win without using having to rebuild their kernels.

Dave

Signature · 1 line
http://www.codemonkey.org.uk
#2 lore raw
Quoted 6 lines · Dave Jones
Quoted 5 lines · Zachary Amsden

In situations where page table updates need only be made locally, and there is no cross-processor A/D bit races involved, we need not use the heavyweight xchg instruction to atomically fetch and clear page table entries. Instead, we can just read and clear them directly.

Quoted 7 lines
Quoted 2 lines

This introduces a neat optimization for non-SMP kernels; drop the atomic xchg operations from page table updates.

Would it be feasible to do this using the smp_alternatives stuff? Given many distros are now shipping SMP-only kernels, it'd be nice to get that win without using having to rebuild their kernels.

That's a good point. It is a fairly straightforward substitution for the static case:

xchg reg, mem

to

mov mem, reg
mov imm, mem

I'll give it a spin and see how it drives. It is, though, less obvious how easy it will be for PAE kernels.

Zach