Skip to contentSkip to conversation

x86: avoid unnecessary tlb flush

PATCH In review Shaohua Li 5 participants 9 messages Aug 6, 2010 – Aug 23, 2010

Subsystems (4)
lore
Share & tools
lore

Shaohua Li

author
#0 lore raw

PATCH 1/1 x86: avoid unnecessary tlb flush

In x86, access and dirty bits are set automatically by CPU when CPU accesses memory. When we go into the code path of below flush_tlb_nonprotect_page(), we already set dirty bit for pte and don't need flush tlb. This might mean tlb entry in some CPUs hasn't dirty bit set, but this doesn't matter. When the CPUs do page write, they will automatically check the bit and no software involved.

On the other hand, flush tlb in below position is harmful. Test creates CPU number of threads, each thread writes to a same but random address in same vma range and we measure the total time. Under a 4 socket system, original time is 1.96s, while with the patch, the time is 0.8s. Under a 2 socket system, there is 20% time cut too. perf shows a lot of time are taking to send ipi/handle ipi for tlb flush.

---
 arch/x86/include/asm/pgtable.h |    3 +++
 include/asm-generic/pgtable.h  |    4 ++++
 mm/memory.c                    |    2 +-
 3 files changed, 8 insertions(+), 1 deletion(-)
arch/x86/include/asm/pgtable.h +3 -0
Index: linux/arch/x86/include/asm/pgtable.h
===================================================================
@@ -603,6 +603,9 @@ static inline void ptep_set_wrprotect(st
 	pte_update(mm, addr, ptep);
 }
 
+#define __HAVE_ARCH_FLUSH_TLB_NONPROTECT_PAGE
+#define flush_tlb_nonprotect_page(vma, address)
+
 /*
  * clone_pgd_range(pgd_t *dst, pgd_t *src, int count);
  *
include/asm-generic/pgtable.h +4 -0
Index: linux/include/asm-generic/pgtable.h
===================================================================
@@ -129,6 +129,10 @@ static inline void ptep_set_wrprotect(st
 #define move_pte(pte, prot, old_addr, new_addr)	(pte)
 #endif
 
+#ifndef __HAVE_ARCH_FLUSH_TLB_NONPROTECT_PAGE
+#define flush_tlb_nonprotect_page(vma, address) flush_tlb_page(vma, address)
+#endif
+
 #ifndef pgprot_noncached
 #define pgprot_noncached(prot)	(prot)
 #endif
mm/memory.c +1 -1
Index: linux/mm/memory.c
===================================================================
@@ -3116,7 +3116,7 @@ static inline int handle_pte_fault(struc
 		 * with threads.
 		 */
 		if (flags & FAULT_FLAG_WRITE)
-			flush_tlb_page(vma, address);
+			flush_tlb_nonprotect_page(vma, address);
 	}
 unlock:
 	pte_unmap_unlock(pte, ptl);
maintainer
#1 lore raw
Quoted 41 lines · On Fri, 06 Aug 2010 11:28:28 +0800 Shaohua Li <[email protected]> wrote:

In x86, access and dirty bits are set automatically by CPU when CPU accesses memory. When we go into the code path of below flush_tlb_nonprotect_page(), we already set dirty bit for pte and don't need flush tlb. This might mean tlb entry in some CPUs hasn't dirty bit set, but this doesn't matter. When the CPUs do page write, they will automatically check the bit and no software involved.

On the other hand, flush tlb in below position is harmful. Test creates CPU number of threads, each thread writes to a same but random address in same vma range and we measure the total time. Under a 4 socket system, original time is 1.96s, while with the patch, the time is 0.8s. Under a 2 socket system, there is 20% time cut too. perf shows a lot of time are taking to send ipi/handle ipi for tlb flush.

---
 arch/x86/include/asm/pgtable.h |    3 +++
 include/asm-generic/pgtable.h  |    4 ++++
 mm/memory.c                    |    2 +-
 3 files changed, 8 insertions(+), 1 deletion(-)

Index: linux/arch/x86/include/asm/pgtable.h
===================================================================

arch/x86/include/asm/pgtable.h +3 -0
@@ -603,6 +603,9 @@ static inline void ptep_set_wrprotect(st
 	pte_update(mm, addr, ptep);
 }
 
+#define __HAVE_ARCH_FLUSH_TLB_NONPROTECT_PAGE
+#define flush_tlb_nonprotect_page(vma, address)
+
 /*
  * clone_pgd_range(pgd_t *dst, pgd_t *src, int count);
  *

Index: linux/include/asm-generic/pgtable.h
===================================================================

include/asm-generic/pgtable.h static inline void ptep_set_wrprotect(st · line 134

Quoted 6 lines
include/asm-generic/pgtable.h +3 -0
@@ -129,6 +129,10 @@ static inline void ptep_set_wrprotect(st
 #define move_pte(pte, prot, old_addr, new_addr)	(pte)
 #endif
 
+#ifndef __HAVE_ARCH_FLUSH_TLB_NONPROTECT_PAGE
+#define flush_tlb_nonprotect_page(vma, address) flush_tlb_page(vma, address)
+#endif

The preferred technique here is

#ifndef flush_tlb_nonprotect_page
#define flush_tlb_nonprotect_page(vma, address) flush_tlb_page(vma, address) #endif

so no need for __HAVE_ARCH_FLUSH_TLB_NONPROTECT_PAGE. include/asm-generic/pgtable.h uses a mix of the two techniques.

author
#2 lore raw
Quoted 52 lines · Andrew Morton
Quoted 41 lines · On Fri, 06 Aug 2010 11:28:28 +0800 Shaohua Li <[email protected]> wrote:

In x86, access and dirty bits are set automatically by CPU when CPU accesses memory. When we go into the code path of below flush_tlb_nonprotect_page(), we already set dirty bit for pte and don't need flush tlb. This might mean tlb entry in some CPUs hasn't dirty bit set, but this doesn't matter. When the CPUs do page write, they will automatically check the bit and no software involved.

On the other hand, flush tlb in below position is harmful. Test creates CPU number of threads, each thread writes to a same but random address in same vma range and we measure the total time. Under a 4 socket system, original time is 1.96s, while with the patch, the time is 0.8s. Under a 2 socket system, there is 20% time cut too. perf shows a lot of time are taking to send ipi/handle ipi for tlb flush.

---
 arch/x86/include/asm/pgtable.h |    3 +++
 include/asm-generic/pgtable.h  |    4 ++++
 mm/memory.c                    |    2 +-
 3 files changed, 8 insertions(+), 1 deletion(-)

Index: linux/arch/x86/include/asm/pgtable.h
===================================================================

arch/x86/include/asm/pgtable.h +3 -0
@@ -603,6 +603,9 @@ static inline void ptep_set_wrprotect(st
 	pte_update(mm, addr, ptep);
 }
 
+#define __HAVE_ARCH_FLUSH_TLB_NONPROTECT_PAGE
+#define flush_tlb_nonprotect_page(vma, address)
+
 /*
  * clone_pgd_range(pgd_t *dst, pgd_t *src, int count);
  *

Index: linux/include/asm-generic/pgtable.h
===================================================================

include/asm-generic/pgtable.h static inline void ptep_set_wrprotect(st · line 134

Quoted 6 lines
include/asm-generic/pgtable.h +3 -0
@@ -129,6 +129,10 @@ static inline void ptep_set_wrprotect(st
 #define move_pte(pte, prot, old_addr, new_addr)	(pte)
 #endif
 
+#ifndef __HAVE_ARCH_FLUSH_TLB_NONPROTECT_PAGE
+#define flush_tlb_nonprotect_page(vma, address) flush_tlb_page(vma, address)
+#endif

The preferred technique here is

Quoted 6 lines

#ifndef flush_tlb_nonprotect_page
#define flush_tlb_nonprotect_page(vma, address) flush_tlb_page(vma, address) #endif

so no need for __HAVE_ARCH_FLUSH_TLB_NONPROTECT_PAGE. include/asm-generic/pgtable.h uses a mix of the two techniques.

ok, updated the patch.

In x86, access and dirty bits are set automatically by CPU when CPU accesses memory. When we go into the code path of below flush_tlb_nonprotect_page(), we already set dirty bit for pte and don't need flush tlb. This might mean tlb entry in some CPUs hasn't dirty bit set, but this doesn't matter. When the CPUs do page write, they will automatically check the bit and no software involved.

On the other hand, flush tlb in below position is harmful. Test creates CPU number of threads, each thread writes to a same but random address in same vma range and we measure the total time. Under a 4 socket system, original time is 1.96s, while with the patch, the time is 0.8s. Under a 2 socket system, there is 20% time cut too. perf shows a lot of time are taking to send ipi/handle ipi for tlb flush.

---
 arch/x86/include/asm/pgtable.h |    2 ++
 include/asm-generic/pgtable.h  |    4 ++++
 mm/memory.c                    |    2 +-
 3 files changed, 7 insertions(+), 1 deletion(-)
arch/x86/include/asm/pgtable.h +2 -0
Index: linux/arch/x86/include/asm/pgtable.h
===================================================================
@@ -603,6 +603,8 @@ static inline void ptep_set_wrprotect(st
 	pte_update(mm, addr, ptep);
 }
 
+#define flush_tlb_nonprotect_page(vma, address)
+
 /*
  * clone_pgd_range(pgd_t *dst, pgd_t *src, int count);
  *
include/asm-generic/pgtable.h +4 -0
Index: linux/include/asm-generic/pgtable.h
===================================================================
@@ -129,6 +129,10 @@ static inline void ptep_set_wrprotect(st
 #define move_pte(pte, prot, old_addr, new_addr)	(pte)
 #endif
 
+#ifndef flush_tlb_nonprotect_page
+#define flush_tlb_nonprotect_page(vma, address) flush_tlb_page(vma, address)
+#endif
+
 #ifndef pgprot_noncached
 #define pgprot_noncached(prot)	(prot)
 #endif
mm/memory.c +1 -1
Index: linux/mm/memory.c
===================================================================
@@ -3116,7 +3116,7 @@ static inline int handle_pte_fault(struc
 		 * with threads.
 		 */
 		if (flags & FAULT_FLAG_WRITE)
-			flush_tlb_page(vma, address);
+			flush_tlb_nonprotect_page(vma, address);
 	}
 unlock:
 	pte_unmap_unlock(pte, ptl);
#3 lore raw
Quoted 56 lines · Shaohua Li

In x86, access and dirty bits are set automatically by CPU when CPU accesses memory. When we go into the code path of below flush_tlb_nonprotect_page(), we already set dirty bit for pte and don't need flush tlb. This might mean tlb entry in some CPUs hasn't dirty bit set, but this doesn't matter. When the CPUs do page write, they will automatically check the bit and no software involved.

On the other hand, flush tlb in below position is harmful. Test creates CPU number of threads, each thread writes to a same but random address in same vma range and we measure the total time. Under a 4 socket system, original time is 1.96s, while with the patch, the time is 0.8s. Under a 2 socket system, there is 20% time cut too. perf shows a lot of time are taking to send ipi/handle ipi for tlb flush.

---
 arch/x86/include/asm/pgtable.h |    2 ++
 include/asm-generic/pgtable.h  |    4 ++++
 mm/memory.c                    |    2 +-
 3 files changed, 7 insertions(+), 1 deletion(-)

Index: linux/arch/x86/include/asm/pgtable.h
===================================================================

arch/x86/include/asm/pgtable.h +2 -0
@@ -603,6 +603,8 @@ static inline void ptep_set_wrprotect(st
 	pte_update(mm, addr, ptep);
 }
 
+#define flush_tlb_nonprotect_page(vma, address)
+
 /*
  * clone_pgd_range(pgd_t *dst, pgd_t *src, int count);
  *

Index: linux/include/asm-generic/pgtable.h
===================================================================

include/asm-generic/pgtable.h +4 -0
@@ -129,6 +129,10 @@ static inline void ptep_set_wrprotect(st
 #define move_pte(pte, prot, old_addr, new_addr)	(pte)
 #endif
 
+#ifndef flush_tlb_nonprotect_page
+#define flush_tlb_nonprotect_page(vma, address) flush_tlb_page(vma, address)
+#endif
+
 #ifndef pgprot_noncached
 #define pgprot_noncached(prot)	(prot)
 #endif

Index: linux/mm/memory.c
===================================================================

mm/memory.c static inline int handle_pte_fault(struc · line 3122

Quoted 8 lines
mm/memory.c +1 -1
@@ -3116,7 +3116,7 @@ static inline int handle_pte_fault(struc
 		 * with threads.
 		 */
 		if (flags & FAULT_FLAG_WRITE)
-			flush_tlb_page(vma, address);
+			flush_tlb_nonprotect_page(vma, address);
 	}
 unlock:
 	pte_unmap_unlock(pte, ptl);

Just added Andrea to the Cc list: he did that TLB flush in 1a44e149, I'd feel more comfortable noop-ing it on x86 if you've convinced him.

Hugh

#4 lore raw
Quoted 5 lines · Hugh Dickins

Just added Andrea to the Cc list: he did that TLB flush in 1a44e149, I'd feel more comfortable noop-ing it on x86 if you've convinced him.

Hugh

Andrea is probably on his way back from LinuxCon, but looking at the original patch it might be something that non-x86 architectures need, but which can be optimized specifically on x86, since x86 has explicit "no flush needed when going to more permissive" semantics.

-hpa

Acked-by
#5 lore raw
Quoted 11 lines · H. Peter Anvin
Quoted 5 lines · Hugh Dickins

Just added Andrea to the Cc list: he did that TLB flush in 1a44e149, I'd feel more comfortable noop-ing it on x86 if you've convinced him.

Hugh

Andrea is probably on his way back from LinuxCon, but looking at the original patch it might be something that non-x86 architectures need, but which can be optimized specifically on x86, since x86 has explicit "no flush needed when going to more permissive" semantics.

Yes. I don't see a problem with the proposed patch. This is the case of parallel thread execution getting spurious write protection faults for the same page for which the pte entry is already up to date and the fault has already flushed the existing spurious TLB entry in the case of x86.

I prefer a better name for the new flush_tlb_nonprotect_page() to reflect the above. something like tlb_fix_spurious_fault() or something?

Also for other architectures, in this case, do we really need a global tlb flush or just the local tlb flush?

author Acked-by Suresh Siddha
#6 lore raw
Quoted 13 lines · Suresh B
Quoted 11 lines · H. Peter Anvin
Quoted 5 lines · Hugh Dickins

Just added Andrea to the Cc list: he did that TLB flush in 1a44e149, I'd feel more comfortable noop-ing it on x86 if you've convinced him.

Hugh

Andrea is probably on his way back from LinuxCon, but looking at the original patch it might be something that non-x86 architectures need, but which can be optimized specifically on x86, since x86 has explicit "no flush needed when going to more permissive" semantics.

Quoted 8 lines

Yes. I don't see a problem with the proposed patch. This is the case of parallel thread execution getting spurious write protection faults for the same page for which the pte entry is already up to date and the fault has already flushed the existing spurious TLB entry in the case of x86.

I prefer a better name for the new flush_tlb_nonprotect_page() to reflect the above. something like tlb_fix_spurious_fault() or something?

this name is better.

In x86, access and dirty bits are set automatically by CPU when CPU accesses memory. When we go into the code path of below flush_tlb_fix_spurious_fault(), we already set dirty bit for pte and don't need flush tlb. This might mean tlb entry in some CPUs hasn't dirty bit set, but this doesn't matter. When the CPUs do page write, they will automatically check the bit and no software involved.

On the other hand, flush tlb in below position is harmful. Test creates CPU number of threads, each thread writes to a same but random address in same vma range and we measure the total time. Under a 4 socket system, original time is 1.96s, while with the patch, the time is 0.8s. Under a 2 socket system, there is 20% time cut too. perf shows a lot of time are taking to send ipi/handle ipi for tlb flush.

---
 arch/x86/include/asm/pgtable.h |    2 ++
 include/asm-generic/pgtable.h  |    4 ++++
 mm/memory.c                    |    2 +-
 3 files changed, 7 insertions(+), 1 deletion(-)
arch/x86/include/asm/pgtable.h +2 -0
Index: linux/arch/x86/include/asm/pgtable.h
===================================================================
@@ -603,6 +603,8 @@ static inline void ptep_set_wrprotect(st
 	pte_update(mm, addr, ptep);
 }
 
+#define flush_tlb_fix_spurious_fault(vma, address)
+
 /*
  * clone_pgd_range(pgd_t *dst, pgd_t *src, int count);
  *
include/asm-generic/pgtable.h +4 -0
Index: linux/include/asm-generic/pgtable.h
===================================================================
@@ -129,6 +129,10 @@ static inline void ptep_set_wrprotect(st
 #define move_pte(pte, prot, old_addr, new_addr)	(pte)
 #endif
 
+#ifndef flush_tlb_fix_spurious_fault
+#define flush_tlb_fix_spurious_fault(vma, address) flush_tlb_page(vma, address)
+#endif
+
 #ifndef pgprot_noncached
 #define pgprot_noncached(prot)	(prot)
 #endif
mm/memory.c +1 -1
Index: linux/mm/memory.c
===================================================================
@@ -3140,7 +3140,7 @@ static inline int handle_pte_fault(struc
 		 * with threads.
 		 */
 		if (flags & FAULT_FLAG_WRITE)
-			flush_tlb_page(vma, address);
+			flush_tlb_fix_spurious_fault(vma, address);
 	}
 unlock:
 	pte_unmap_unlock(pte, ptl);
author
#7 lore raw
Quoted 20 lines · Shaohua Li
Quoted 19 lines · Suresh B
Quoted 11 lines · H. Peter Anvin
Quoted 5 lines · Hugh Dickins

Just added Andrea to the Cc list: he did that TLB flush in 1a44e149, I'd feel more comfortable noop-ing it on x86 if you've convinced him.

Hugh

Andrea is probably on his way back from LinuxCon, but looking at the original patch it might be something that non-x86 architectures need, but which can be optimized specifically on x86, since x86 has explicit "no flush needed when going to more permissive" semantics.

Yes. I don't see a problem with the proposed patch. This is the case of parallel thread execution getting spurious write protection faults for the same page for which the pte entry is already up to date and the fault has already flushed the existing spurious TLB entry in the case of x86.

Quoted 3 lines
Quoted 2 lines

I prefer a better name for the new flush_tlb_nonprotect_page() to reflect the above. something like tlb_fix_spurious_fault() or something?

this name is better.

Hi Andrea,
can you look at this patch?

Thanks,
Shaohua

Quoted 64 lines

In x86, access and dirty bits are set automatically by CPU when CPU accesses memory. When we go into the code path of below flush_tlb_fix_spurious_fault(), we already set dirty bit for pte and don't need flush tlb. This might mean tlb entry in some CPUs hasn't dirty bit set, but this doesn't matter. When the CPUs do page write, they will automatically check the bit and no software involved.

On the other hand, flush tlb in below position is harmful. Test creates CPU number of threads, each thread writes to a same but random address in same vma range and we measure the total time. Under a 4 socket system, original time is 1.96s, while with the patch, the time is 0.8s. Under a 2 socket system, there is 20% time cut too. perf shows a lot of time are taking to send ipi/handle ipi for tlb flush.

---
 arch/x86/include/asm/pgtable.h |    2 ++
 include/asm-generic/pgtable.h  |    4 ++++
 mm/memory.c                    |    2 +-
 3 files changed, 7 insertions(+), 1 deletion(-)

Index: linux/arch/x86/include/asm/pgtable.h
===================================================================

arch/x86/include/asm/pgtable.h +2 -0
@@ -603,6 +603,8 @@ static inline void ptep_set_wrprotect(st
 	pte_update(mm, addr, ptep);
 }
 
+#define flush_tlb_fix_spurious_fault(vma, address)
+
 /*
  * clone_pgd_range(pgd_t *dst, pgd_t *src, int count);
  *

Index: linux/include/asm-generic/pgtable.h
===================================================================

include/asm-generic/pgtable.h +4 -0
@@ -129,6 +129,10 @@ static inline void ptep_set_wrprotect(st
 #define move_pte(pte, prot, old_addr, new_addr)	(pte)
 #endif
 
+#ifndef flush_tlb_fix_spurious_fault
+#define flush_tlb_fix_spurious_fault(vma, address) flush_tlb_page(vma, address)
+#endif
+
 #ifndef pgprot_noncached
 #define pgprot_noncached(prot)	(prot)
 #endif

Index: linux/mm/memory.c
===================================================================

mm/memory.c +1 -1
@@ -3140,7 +3140,7 @@ static inline int handle_pte_fault(struc
 		 * with threads.
 		 */
 		if (flags & FAULT_FLAG_WRITE)
-			flush_tlb_page(vma, address);
+			flush_tlb_fix_spurious_fault(vma, address);
 	}
 unlock:
 	pte_unmap_unlock(pte, ptl);
#8 lore raw

[tip:x86/mm] x86, mm: Avoid unnecessary TLB flush

In reply to Shaohua Li
Commit-ID:  61c77326d1df079f202fa79403c3ccd8c5966a81
Gitweb:     http://git.kernel.org/tip/61c77326d1df079f202fa79403c3ccd8c5966a81
Author:     Shaohua Li <[email protected]>
AuthorDate: Mon, 16 Aug 2010 09:16:55 +0800
Committer:  H. Peter Anvin <[email protected]>
CommitDate: Mon, 23 Aug 2010 10:04:57 -0700

x86, mm: Avoid unnecessary TLB flush

In x86, access and dirty bits are set automatically by CPU when CPU accesses memory. When we go into the code path of below flush_tlb_fix_spurious_fault(), we already set dirty bit for pte and don't need flush tlb. This might mean tlb entry in some CPUs hasn't dirty bit set, but this doesn't matter. When the CPUs do page write, they will automatically check the bit and no software involved.

On the other hand, flush tlb in below position is harmful. Test creates CPU number of threads, each thread writes to a same but random address in same vma range and we measure the total time. Under a 4 socket system, original time is 1.96s, while with the patch, the time is 0.8s. Under a 2 socket system, there is 20% time cut too. perf shows a lot of time are taking to send ipi/handle ipi for tlb flush.

LKML-Reference: <[email protected]>

---
 arch/x86/include/asm/pgtable.h |    2 ++
 include/asm-generic/pgtable.h  |    4 ++++
 mm/memory.c                    |    2 +-
 3 files changed, 7 insertions(+), 1 deletions(-)
arch/x86/include/asm/pgtable.h +2 -0
index a34c785..2d0a33b 100644
@@ -603,6 +603,8 @@ static inline void ptep_set_wrprotect(struct mm_struct *mm,
 	pte_update(mm, addr, ptep);
 }
 
+#define flush_tlb_fix_spurious_fault(vma, address)
+
 /*
  * clone_pgd_range(pgd_t *dst, pgd_t *src, int count);
  *
include/asm-generic/pgtable.h +4 -0
index e2bd73e..f4d4120 100644
@@ -129,6 +129,10 @@ static inline void ptep_set_wrprotect(struct mm_struct *mm, unsigned long addres
 #define move_pte(pte, prot, old_addr, new_addr)	(pte)
 #endif
 
+#ifndef flush_tlb_fix_spurious_fault
+#define flush_tlb_fix_spurious_fault(vma, address) flush_tlb_page(vma, address)
+#endif
+
 #ifndef pgprot_noncached
 #define pgprot_noncached(prot)	(prot)
 #endif
mm/memory.c +1 -1
index 2ed2267..a40da69 100644
@@ -3147,7 +3147,7 @@ static inline int handle_pte_fault(struct mm_struct *mm,
 		 * with threads.
 		 */
 		if (flags & FAULT_FLAG_WRITE)
-			flush_tlb_page(vma, address);
+			flush_tlb_fix_spurious_fault(vma, address);
 	}
 unlock:
 	pte_unmap_unlock(pte, ptl);