arm, tlbflush: avoid TLBI broadcast if page reused in write fault
Subsystems (4)
cover arm, tlbflush: avoid TLBI broadcast if page reused in write fault
This series is to optimize the system performance via avoiding TLBI broadcast if page is reused in the write protect fault handler. More details of the background and the test results can be found in [2/2].
Huang Ying (2):
mm: add spurious fault fixing support for huge pmd arm64, tlbflush: don't TLBI broadcast if page reused in write fault
arch/arm64/include/asm/pgtable.h | 14 ++++++++----- arch/arm64/include/asm/tlbflush.h | 33 +++++++++++++++++++++++++++++++ arch/arm64/mm/fault.c | 2 +- include/linux/pgtable.h | 4 ++++ mm/huge_memory.c | 25 +++++++++++++++++++---- mm/internal.h | 4 ++-- 6 files changed, 70 insertions(+), 12 deletions(-)
Best Regards,
Huang, Ying
RFC PATCH 1/2 mm: add spurious fault fixing support for huge pmd
In the current kernel, there is spurious fault fixing support for pte, but not for huge pmd because no architectures need it. But in the next patch in the series, we will change the write protection fault handling logic on arm64, so that some stale huge pmd entries may remain in the TLB. These entries need to be flushed via the huge pmd spurious fault fixing mechanism.
--- include/linux/pgtable.h | 4 ++++ mm/huge_memory.c | 25 +++++++++++++++++++++---- mm/internal.h | 4 ++-- 3 files changed, 27 insertions(+), 6 deletions(-)
include/linux/pgtable.h +4 -0
@@ -1232,6 +1232,10 @@ static inline void arch_swap_restore(swp_entry_t entry, struct folio *folio)#define flush_tlb_fix_spurious_fault (vma, address, ptep) flush_tlb_page(vma, address)#endif +#ifndef flush_tlb_fix_spurious_fault_pmd +#define flush_tlb_fix_spurious_fault_pmd (vma, address, ptep) do { } while (0) +#endif +/* * When walking page tables, get the address of the next boundary, * or the end address of the range if that comes earlier. Although no
mm/huge_memory.c +21 -4
@@ -1650,8 +1650,8 @@ vm_fault_t vmf_insert_folio_pud(struct vm_fault *vmf, struct folio *folio,EXPORT_SYMBOL_GPL (vmf_insert_folio_pud);#endif /* CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD */ -void touch_pmd (struct vm_area_struct *vma,unsigned long addr, -pmd_t *pmd,bool write) +int touch_pmd (struct vm_area_struct *vma,unsigned long addr, +pmd_t *pmd,bool write) {pmd_t _pmd;
@@ -1659,8 +1659,12 @@ void touch_pmd(struct vm_area_struct *vma, unsigned long addr,if (write) _pmd =pmd_mkdirty (_pmd);if (pmdp_set_access_flags (vma, addr &HPAGE_PMD_MASK , - pmd, _pmd, write)) + pmd, _pmd, write)) {update_mmu_cache_pmd (vma, addr, pmd); +return 1 ; + } + +return 0 ; } int copy_huge_pmd(struct mm_struct *dst_mm, struct mm_struct *src_mm,
@@ -1857,7 +1861,20 @@ void huge_pmd_set_accessed(struct vm_fault *vmf)if (unlikely (!pmd_same (*vmf->pmd, vmf->orig_pmd))) goto unlock; -touch_pmd (vmf->vma, vmf->address, vmf->pmd, write); +if (!touch_pmd (vmf->vma, vmf->address, vmf->pmd, write)) { +/* Skip spurious TLB flush for retried page fault */ +if (vmf->flags &FAULT_FLAG_TRIED ) + goto unlock; +/* +* This is needed only for protection faults but the arch code +* is not yet telling us if this is a protection fault or not. +* This still avoids useless tlb flushes for .text page faults +* with threads. +*/ +if (vmf->flags &FAULT_FLAG_WRITE ) +flush_tlb_fix_spurious_fault_pmd (vmf->vma, vmf->address, + vmf->pmd); + } unlock:spin_unlock (vmf->ptl);
mm/internal.h +2 -2
@@ -1406,8 +1406,8 @@ int __must_check try_grab_folio(struct folio *folio, int refs,*/ void touch_pud (struct vm_area_struct *vma,unsigned long addr,pud_t *pud,bool write); -void touch_pmd (struct vm_area_struct *vma,unsigned long addr, -pmd_t *pmd,bool write); +int touch_pmd (struct vm_area_struct *vma,unsigned long addr, +pmd_t *pmd,bool write);/* * Parses a string with mem suffixes into its order. Useful to parse kernel
Signature · 1 line
2.39.5
Re: [RFC PATCH 1/2] mm: add spurious fault fixing support for huge pmd
Quoted 30 lines · Huang Ying
In the current kernel, there is spurious fault fixing support for pte, but not for huge pmd because no architectures need it. But in the next patch in the series, we will change the write protection fault handling logic on arm64, so that some stale huge pmd entries may remain in the TLB. These entries need to be flushed via the huge pmd spurious fault fixing mechanism.
Signed-off-by Huang Ying <[email protected]>Cc Catalin Marinas <[email protected]>Cc Will Deacon <[email protected]>Cc Andrew Morton <[email protected]>Cc David Hildenbrand <[email protected]>Cc Lorenzo Stoakes <[email protected]>Cc Vlastimil Babka <[email protected]>Cc Zi Yan <[email protected]>Cc Baolin Wang <[email protected]>Cc Ryan Roberts <[email protected]>Cc Yang Shi <[email protected]>Cc "Christoph Lameter (Ampere)" <[email protected]>Cc Dev Jain <[email protected]>Cc Barry Song <[email protected]>Cc Anshuman Khandual <[email protected]>Cc Yicong Yang <[email protected]>Cc Kefeng Wang <[email protected]>Cc Kevin Brodsky <[email protected]>Cc Yin Fengwei <[email protected]>---
[...]
Quoted 7 lines
int copy_huge_pmd(struct
mm_struct*dst_mm, structmm_struct*src_mm,mm/huge_memory.c +0
-1@@ -1857,7 +1861,20 @@ void huge_pmd_set_accessed(struct vm_fault *vmf)if (unlikely (!pmd_same (*vmf->pmd, vmf->orig_pmd))) goto unlock; -touch_pmd (vmf->vma, vmf->address, vmf->pmd, write);
mm/huge_memory.c void huge_pmd_set_accessed(struct vm_fault *vmf) · line 1873
Quoted 10 lines
mm/huge_memory.c +10
-0+if (!touch_pmd (vmf->vma, vmf->address, vmf->pmd, write)) { +/* Skip spurious TLB flush for retried page fault */ +if (vmf->flags &FAULT_FLAG_TRIED ) + goto unlock; +/* +* This is needed only for protection faults but the arch code +* is not yet telling us if this is a protection fault or not. +* This still avoids useless tlb flushes for .text page faults +* with threads. +*/
Can we instead just remove these comments and simplly say "see handle_pte_fault()"
mm/huge_memory.c void huge_pmd_set_accessed(struct vm_fault *vmf) · line 1877
Quoted 4 lines
mm/huge_memory.c +4
-0+if (vmf->flags &FAULT_FLAG_WRITE ) +flush_tlb_fix_spurious_fault_pmd (vmf->vma, vmf->address, + vmf->pmd); + }
Okay, In the PTE case, we call flush_tlb_fix_spurious_fault() during write faults if ptep_set_access_flags() returned "0".
You are calling flush_tlb_fix_spurious_fault_pmd() during a write fault when pmdp_set_access_flags() returned "0" as well.
In general, LGTM, but I would just let touch_pmd() return the value of pmdp_set_access_flags() instead and add a quick comment for touch_pmd() what the return value means.
Signature · 3 lines
Cheers David / dhildenb
Re: [RFC PATCH 1/2] mm: add spurious fault fixing support for huge pmd
Hi, David,
Thanks for review!
Quoted 44 lines · David Hildenbrand
Quoted 29 lines · Huang Ying
In the current kernel, there is spurious fault fixing support for pte, but not for huge pmd because no architectures need it. But in the next patch in the series, we will change the write protection fault handling logic on arm64, so that some stale huge pmd entries may remain in the TLB. These entries need to be flushed via the huge pmd spurious fault fixing mechanism.
Signed-off-by Huang Ying <[email protected]>Cc Catalin Marinas <[email protected]>Cc Will Deacon <[email protected]>Cc Andrew Morton <[email protected]>Cc David Hildenbrand <[email protected]>Cc Lorenzo Stoakes <[email protected]>Cc Vlastimil Babka <[email protected]>Cc Zi Yan <[email protected]>Cc Baolin Wang <[email protected]>Cc Ryan Roberts <[email protected]>Cc Yang Shi <[email protected]>Cc "Christoph Lameter (Ampere)" <[email protected]>Cc Dev Jain <[email protected]>Cc Barry Song <[email protected]>Cc Anshuman Khandual <[email protected]>Cc Yicong Yang <[email protected]>Cc Kefeng Wang <[email protected]>Cc Kevin Brodsky <[email protected]>Cc Yin Fengwei <[email protected]>---
[...]
Quoted 11 lines
int copy_huge_pmd(struct
mm_struct*dst_mm, structmm_struct*src_mm,mm/huge_memory.c +5
-0@@ -1857,7 +1861,20 @@ void huge_pmd_set_accessed(struct vm_fault *vmf)if (unlikely (!pmd_same (*vmf->pmd, vmf->orig_pmd))) goto unlock; -touch_pmd (vmf->vma, vmf->address, vmf->pmd, write); +if (!touch_pmd (vmf->vma, vmf->address, vmf->pmd, write)) { +/* Skip spurious TLB flush for retried page fault */ +if (vmf->flags &FAULT_FLAG_TRIED ) + goto unlock; +/*
Quoted 8 lines
mm/huge_memory.c void huge_pmd_set_accessed(struct vm_fault *vmf) · line 1873
Quoted 5 lines
mm/huge_memory.c +5
-0+* This is needed only for protection faults but the arch code +* is not yet telling us if this is a protection fault or not. +* This still avoids useless tlb flushes for .text page faults +* with threads. +*/ Can we instead just remove these comments and simplly say "see
handle_pte_fault()"
Sure.
Quoted 8 lines
mm/huge_memory.c void huge_pmd_set_accessed(struct vm_fault *vmf) · line 1877
Quoted 4 lines
mm/huge_memory.c +4
-0+if (vmf->flags &FAULT_FLAG_WRITE ) +flush_tlb_fix_spurious_fault_pmd (vmf->vma, vmf->address, + vmf->pmd); + }Okay, In the PTE case, we call
flush_tlb_fix_spurious_fault()during write faults ifptep_set_access_flags()returned "0".
Quoted 6 lines
You are calling
flush_tlb_fix_spurious_fault_pmd()during a write fault whenpmdp_set_access_flags()returned "0" as well.In general, LGTM, but I would just let
touch_pmd()return the value ofpmdp_set_access_flags()instead and add a quick comment fortouch_pmd()what the return value means.
Sure.
---
Best Regards,
Huang, Ying
RFC PATCH 2/2 arm64, tlbflush: don't TLBI broadcast if page reused in write fault
In reply to Huang YingA multi-thread customer workload with large memory footprint uses fork()/exec() to run some external programs every tens seconds. When running the workload on an arm64 server machine, it's observed that quite some CPU cycles are spent in the TLB flushing functions. While running the workload on the x86_64 server machine, it's not. This causes the performance on arm64 to be much worse than that on x86_64.
During the workload running, after fork()/exec() write-protects all pages in the parent process, memory writing in the parent process will cause a write protection fault. Then the page fault handler will make the PTE/PDE writable if the page can be reused, which is almost always true in the workload. On arm64, to avoid the write protection fault on other CPUs, the page fault handler flushes the TLB globally with TLBI broadcast after changing the PTE/PDE. However, this isn't always necessary. Firstly, it's safe to leave some stall read-only TLB entries as long as they will be flushed finally. Secondly, it's quite possible that the original read-only PTE/PDEs aren't cached in remote TLB at all if the memory footprint is large. In fact, on x86_64, the page fault handler doesn't flush the remote TLB in this situation, which benefits the performance a lot.
To improve the performance on arm64, make the write protection fault handler flush the TLB locally instead of globally via TLBI broadcast after making the PTE/PDE writable. If there are stall read-only TLB entries in the remote CPUs, the page fault handler on these CPUs will regard the page fault as spurious and flush the stall TLB entries.
To test the patchset, make the usemem.c from
vm-scalability (https://git.kernel.org/pub/scm/linux/kernel/git/wfg/vm-scalability.git).
support calling fork()/exec() periodically. To mimic the behavior of
the customer workload, run usemem with 4 threads, access 100GB memory,
and call fork()/exec() every 40 seconds. Test results show that with
the patchset the score of usemem improves ~40.6%. The cycles% of TLB flush functions reduces from ~50.5% to ~0.3% in perf profile.
--- arch/arm64/include/asm/pgtable.h | 14 ++++++++----- arch/arm64/include/asm/tlbflush.h | 33 +++++++++++++++++++++++++++++++ arch/arm64/mm/fault.c | 2 +- 3 files changed, 43 insertions(+), 6 deletions(-)
arch/arm64/include/asm/pgtable.h +9 -5
@@ -130,12 +130,16 @@ static inline void arch_leave_lazy_mmu_mode(void)#endif /* CONFIG_TRANSPARENT_HUGEPAGE */ /* -* Outside of a few very special situations (e.g. hibernation), we always -* use broadcast TLB invalidation instructions, therefore a spurious page -* fault on one CPU which has been handled concurrently by another CPU -* does not need to perform additional invalidation. +* We use local TLB invalidation instruction when reusing page in +* write protection fault handler to avoid TLBI broadcast in the hot +* path. This will cause spurious page faults if stall read-only TLB +* entries exist. */ -#define flush_tlb_fix_spurious_fault (vma, address, ptep) do { } while (0) +#define flush_tlb_fix_spurious_fault (vma, address, ptep) \ + local_flush_tlb_page_nonotify(vma, address) + +#define flush_tlb_fix_spurious_fault_pmd (vma, address, pmdp) \ + local_flush_tlb_page_nonotify(vma, address)/* * ZERO_PAGE is a global shared page that is always zero: used
arch/arm64/include/asm/tlbflush.h +33 -0
@@ -282,6 +282,39 @@ static inline void flush_tlb_mm(struct mm_struct *mm)mmu_notifier_arch_invalidate_secondary_tlbs (mm,0 ,-1UL ); } +static inline void __local_flush_tlb_page_nonotify_nosync ( +struct mm_struct *mm,unsigned long uaddr) +{ +unsigned long addr; + +dsb (nshst); + addr =__TLBI_VADDR (uaddr,ASID (mm)); +__tlbi (vale1, addr); +__tlbi_user (vale1, addr); +} + +static inline void local_flush_tlb_page_nonotify ( +struct vm_area_struct *vma,unsigned long uaddr) +{ +__local_flush_tlb_page_nonotify_nosync (vma->vm_mm, uaddr); +dsb (nsh); +} + +static inline void __local_flush_tlb_page_nosync ( +struct mm_struct *mm,unsigned long uaddr) +{ +__local_flush_tlb_page_nonotify_nosync (mm, uaddr); +mmu_notifier_arch_invalidate_secondary_tlbs (mm, uaddr &PAGE_MASK , + (uaddr &PAGE_MASK ) +PAGE_SIZE ); +} + +static inline void local_flush_tlb_page (struct vm_area_struct *vma, +unsigned long uaddr) +{ +__local_flush_tlb_page_nosync (vma->vm_mm, uaddr); +dsb (nsh); +} +static inline void __flush_tlb_page_nosync (struct mm_struct *mm,unsigned long uaddr) {
arch/arm64/mm/fault.c +1 -1
@@ -235,7 +235,7 @@ int __ptep_set_access_flags(struct vm_area_struct *vma,/* Invalidate a stale read-only entry */ if (dirty) -flush_tlb_page (vma, address); +local_flush_tlb_page (vma, address);return 1 ; }
Signature · 1 line
2.39.5
Ryan Roberts
reviewerRe: [RFC PATCH 2/2] arm64, tlbflush: don't TLBI broadcast if page reused in write fault
Quoted 27 lines · Huang Ying
A multi-thread customer workload with large memory footprint uses fork()/exec() to run some external programs every tens seconds. When running the workload on an arm64 server machine, it's observed that quite some CPU cycles are spent in the TLB flushing functions. While running the workload on the
x86_64server machine, it's not. This causes the performance on arm64 to be much worse than that onx86_64.During the workload running, after fork()/exec() write-protects all pages in the parent process, memory writing in the parent process will cause a write protection fault. Then the page fault handler will make the PTE/PDE writable if the page can be reused, which is almost always true in the workload. On arm64, to avoid the write protection fault on other CPUs, the page fault handler flushes the TLB globally with TLBI broadcast after changing the PTE/PDE. However, this isn't always necessary. Firstly, it's safe to leave some stall read-only TLB entries as long as they will be flushed finally. Secondly, it's quite possible that the original read-only PTE/PDEs aren't cached in remote TLB at all if the memory footprint is large. In fact, on
x86_64, the page fault handler doesn't flush the remote TLB in this situation, which benefits the performance a lot.To improve the performance on arm64, make the write protection fault handler flush the TLB locally instead of globally via TLBI broadcast after making the PTE/PDE writable. If there are stall read-only TLB entries in the remote CPUs, the page fault handler on these CPUs will regard the page fault as spurious and flush the stall TLB entries.
Quoted 7 lines
To test the patchset, make the
usemem.cfrom
vm-scalability (https://git.kernel.org/pub/scm/linux/kernel/git/wfg/vm-scalability.git).
support calling fork()/exec() periodically. To mimic the behavior of
the customer workload, run usemem with 4 threads, access 100GB memory,
and call fork()/exec() every 40 seconds. Test results show that with
the patchset the score of usemem improves ~40.6%. The cycles% of TLB flush functions reduces from ~50.5% to ~0.3% in perf profile.
Overall, this looks like a simple and useful performance optimization - thanks! I'm running this through our performance regression suite to see if it spots any workloads where the change slows things down and will report once we have the results.
Quoted 42 lines
Signed-off-by Huang Ying <[email protected]>Cc Catalin Marinas <[email protected]>Cc Will Deacon <[email protected]>Cc Andrew Morton <[email protected]>Cc David Hildenbrand <[email protected]>Cc Lorenzo Stoakes <[email protected]>Cc Vlastimil Babka <[email protected]>Cc Zi Yan <[email protected]>Cc Baolin Wang <[email protected]>Cc Ryan Roberts <[email protected]>Cc Yang Shi <[email protected]>Cc "Christoph Lameter (Ampere)" <[email protected]>Cc Dev Jain <[email protected]>Cc Barry Song <[email protected]>Cc Anshuman Khandual <[email protected]>Cc Yicong Yang <[email protected]>Cc Kefeng Wang <[email protected]>Cc Kevin Brodsky <[email protected]>Cc Yin Fengwei <[email protected]>--- arch/arm64/include/asm/pgtable.h | 14 ++++++++----- arch/arm64/include/asm/tlbflush.h | 33 +++++++++++++++++++++++++++++++ arch/arm64/mm/fault.c | 2 +- 3 files changed, 43 insertions(+), 6 deletions(-)arch/arm64/include/asm/pgtable.h +1
-4@@ -130,12 +130,16 @@ static inline void arch_leave_lazy_mmu_mode(void)#endif /* CONFIG_TRANSPARENT_HUGEPAGE */ /* -* Outside of a few very special situations (e.g. hibernation), we always -* use broadcast TLB invalidation instructions, therefore a spurious page -* fault on one CPU which has been handled concurrently by another CPU -* does not need to perform additional invalidation. +* We use local TLB invalidation instruction when reusing page in
arch/arm64/include/asm/pgtable.h static inline void arch_leave_lazy_mmu_mode(void) · line 142
Quoted 10 lines
arch/arm64/include/asm/pgtable.h +8
-1+* write protection fault handler to avoid TLBI broadcast in the hot +* path. This will cause spurious page faults if stall read-only TLB +* entries exist. */ -#define flush_tlb_fix_spurious_fault (vma, address, ptep) do { } while (0) +#define flush_tlb_fix_spurious_fault (vma, address, ptep) \ + local_flush_tlb_page_nonotify(vma, address) + +#define flush_tlb_fix_spurious_fault_pmd (vma, address, pmdp) \ + local_flush_tlb_page_nonotify(vma, address)
It's not really clear to me how important doing local tlb flushes for pmds is for the performance improvement? I'm guessing most of the win comes from the pte level? I suspect you have only added spurious pmd fault handling because the arm64 implementation of __ptep_set_access_flags() actually handles both pte and pmd levels?
Given the core kernel didn't previously have support for pmd spurious faults, I wonder if it would be simpler to drop the first patch and rejig __ptep_set_access_flags() so that it has a pgsize parameter and can differentiate based on that? I'm on the fence...
If you do end up taking this approach, there is a style I introduced for hugetlb, where the function is suffixed with _anysz and it takes a pgsize param:
int __ptep_set_access_flags_anysz(struct vm_area_struct *vma,
unsigned long address, pte_t *ptep, pte_t entry, int dirty, unsigned long pgsize);
Quoted 34 lines
arch/arm64/include/asm/pgtable.h +0
-0/* * ZERO_PAGE is a global shared page that is always zero: used arch/arm64/include/asm/tlbflush.h +23
-0@@ -282,6 +282,39 @@ static inline void flush_tlb_mm(struct mm_struct *mm)mmu_notifier_arch_invalidate_secondary_tlbs (mm,0 ,-1UL ); } +static inline void __local_flush_tlb_page_nonotify_nosync ( +struct mm_struct *mm,unsigned long uaddr) +{ +unsigned long addr; + +dsb (nshst); + addr =__TLBI_VADDR (uaddr,ASID (mm)); +__tlbi (vale1, addr); +__tlbi_user (vale1, addr); +} + +static inline void local_flush_tlb_page_nonotify ( +struct vm_area_struct *vma,unsigned long uaddr) +{ +__local_flush_tlb_page_nonotify_nosync (vma->vm_mm, uaddr); +dsb (nsh); +} + +static inline void __local_flush_tlb_page_nosync ( +struct mm_struct *mm,unsigned long uaddr) +{ +__local_flush_tlb_page_nonotify_nosync (mm, uaddr); + mmu_notifier_arch_invalidate_secondary_tlbs(mm, uaddr & PAGE_MASK,
arch/arm64/include/asm/tlbflush.h static inline void flush_tlb_mm(struct mm_struct *mm) · line 317
Quoted 10 lines
arch/arm64/include/asm/tlbflush.h +10
-0+ (uaddr & PAGE_MASK) + PAGE_SIZE); +} + +static inline void local_flush_tlb_page (struct vm_area_struct *vma, +unsigned long uaddr) +{ +__local_flush_tlb_page_nosync (vma->vm_mm, uaddr); +dsb (nsh); +} +
You're introducing more variants than you're actually using here. I think you just need local_flush_tlb_page() and local_flush_tlb_page_nonotify(); you could keep __local_flush_tlb_page_nonotify_nosync() and drop __local_flush_tlb_page_nosync() since it's not really adding much?
But I'm also wondering if we should tidy up this API in general; we have local vs broadcast, sync vs nosync, notify vs nonotify. And page is really just a special case of a range. So perhaps it is better to rework the range API to take some flags and we can tidy up all of this. I know Will also posted an RFC to convert a lot of this to c functions, which should also be included. Not a blocker for this change, I don't think, but there should definitely be some follow up work to tidy it up. (I'm happy to take it on).
arch/arm64/mm/fault.c int __ptep_set_access_flags(struct vm_area_struct *vma, · line 238
Quoted 13 lines
arch/arm64/include/asm/tlbflush.h +0
-0static inline void __flush_tlb_page_nosync (struct mm_struct *mm,unsigned long uaddr) {arch/arm64/mm/fault.c +1
-1@@ -235,7 +235,7 @@ int __ptep_set_access_flags(struct vm_area_struct *vma,/* Invalidate a stale read-only entry */ if (dirty) -flush_tlb_page (vma, address); +local_flush_tlb_page (vma, address);
Given this is called for both pmds and ptes, it's a bit of an abuse to flush a *page*. Yes it is architecturally safe, but it's not exactly self-documenting. If we pass in the pgsize (as per above) it could be optimized given we know the level and we only want to invalidate the last level. e.g. the local equivalent to:
__flush_tlb_range(vma, address, address + PMD_SIZE, PMD_SIZE, true, 2);
or
__flush_tlb_range(vma, address, address + PAGE_SIZE, PAGE_SIZE, true, 3);
Again though, perhaps that is part of some follow up tidying work?
contpte_ptep_set_access_flags() currently does a (broadcast) __flush_tlb_range() on the (pte_write(orig_pte) == pte_write(entry)) path. I think that should be changed to a local range invalidation to be consistent with this change.
Thanks,
Ryan
Quoted 3 lines
arch/arm64/mm/fault.c +0
-0return 1 ; }
Re: [RFC PATCH 2/2] arm64, tlbflush: don't TLBI broadcast if page reused in write fault
Hi, Ryan,
Thanks for reivew!
Quoted 36 lines · Ryan Roberts
Quoted 27 lines · Huang Ying
A multi-thread customer workload with large memory footprint uses fork()/exec() to run some external programs every tens seconds. When running the workload on an arm64 server machine, it's observed that quite some CPU cycles are spent in the TLB flushing functions. While running the workload on the
x86_64server machine, it's not. This causes the performance on arm64 to be much worse than that onx86_64.During the workload running, after fork()/exec() write-protects all pages in the parent process, memory writing in the parent process will cause a write protection fault. Then the page fault handler will make the PTE/PDE writable if the page can be reused, which is almost always true in the workload. On arm64, to avoid the write protection fault on other CPUs, the page fault handler flushes the TLB globally with TLBI broadcast after changing the PTE/PDE. However, this isn't always necessary. Firstly, it's safe to leave some stall read-only TLB entries as long as they will be flushed finally. Secondly, it's quite possible that the original read-only PTE/PDEs aren't cached in remote TLB at all if the memory footprint is large. In fact, on
x86_64, the page fault handler doesn't flush the remote TLB in this situation, which benefits the performance a lot.To improve the performance on arm64, make the write protection fault handler flush the TLB locally instead of globally via TLBI broadcast after making the PTE/PDE writable. If there are stall read-only TLB entries in the remote CPUs, the page fault handler on these CPUs will regard the page fault as spurious and flush the stall TLB entries.
Quoted 7 lines
To test the patchset, make the
usemem.cfrom
vm-scalability (https://git.kernel.org/pub/scm/linux/kernel/git/wfg/vm-scalability.git).
support calling fork()/exec() periodically. To mimic the behavior of
the customer workload, run usemem with 4 threads, access 100GB memory,
and call fork()/exec() every 40 seconds. Test results show that with
the patchset the score of usemem improves ~40.6%. The cycles% of TLB flush functions reduces from ~50.5% to ~0.3% in perf profile.
Quoted 4 lines
Overall, this looks like a simple and useful performance optimization - thanks! I'm running this through our performance regression suite to see if it spots any workloads where the change slows things down and will report once we have the results.
Thanks!
Quoted 63 lines
Quoted 41 lines
Signed-off-by Huang Ying <[email protected]>Cc Catalin Marinas <[email protected]>Cc Will Deacon <[email protected]>Cc Andrew Morton <[email protected]>Cc David Hildenbrand <[email protected]>Cc Lorenzo Stoakes <[email protected]>Cc Vlastimil Babka <[email protected]>Cc Zi Yan <[email protected]>Cc Baolin Wang <[email protected]>Cc Ryan Roberts <[email protected]>Cc Yang Shi <[email protected]>Cc "Christoph Lameter (Ampere)" <[email protected]>Cc Dev Jain <[email protected]>Cc Barry Song <[email protected]>Cc Anshuman Khandual <[email protected]>Cc Yicong Yang <[email protected]>Cc Kefeng Wang <[email protected]>Cc Kevin Brodsky <[email protected]>Cc Yin Fengwei <[email protected]>--- arch/arm64/include/asm/pgtable.h | 14 ++++++++----- arch/arm64/include/asm/tlbflush.h | 33 +++++++++++++++++++++++++++++++ arch/arm64/mm/fault.c | 2 +- 3 files changed, 43 insertions(+), 6 deletions(-)arch/arm64/include/asm/pgtable.h +1
-4@@ -130,12 +130,16 @@ static inline void arch_leave_lazy_mmu_mode(void)#endif /* CONFIG_TRANSPARENT_HUGEPAGE */ /* -* Outside of a few very special situations (e.g. hibernation), we always -* use broadcast TLB invalidation instructions, therefore a spurious page -* fault on one CPU which has been handled concurrently by another CPU -* does not need to perform additional invalidation. +* We use local TLB invalidation instruction when reusing page in arch/arm64/include/asm/pgtable.h static inline void arch_leave_lazy_mmu_mode(void) · line 142
Quoted 10 lines
arch/arm64/include/asm/pgtable.h +8
-1+* write protection fault handler to avoid TLBI broadcast in the hot +* path. This will cause spurious page faults if stall read-only TLB +* entries exist. */ -#define flush_tlb_fix_spurious_fault (vma, address, ptep) do { } while (0) +#define flush_tlb_fix_spurious_fault (vma, address, ptep) \ + local_flush_tlb_page_nonotify(vma, address) + +#define flush_tlb_fix_spurious_fault_pmd (vma, address, pmdp) \ + local_flush_tlb_page_nonotify(vma, address)It's not really clear to me how important doing local tlb flushes for pmds is for the performance improvement? I'm guessing most of the win comes from the pte level? I suspect you have only added spurious pmd fault handling because the arm64 implementation of
__ptep_set_access_flags()actually handles both pte and pmd levels?Given the core kernel didn't previously have support for pmd spurious faults, I wonder if it would be simpler to drop the first patch and rejig
__ptep_set_access_flags()so that it has a pgsize parameter and can differentiate based on that? I'm on the fence...
Quoted 6 lines
If you do end up taking this approach, there is a style I introduced for hugetlb, where the function is suffixed with
_anyszand it takes a pgsize param:int __ptep_set_access_flags_anysz(struct
vm_area_struct*vma,unsigned long address, pte_t *ptep, pte_t entry, int dirty, unsigned long pgsize);
From the performance point of view, local TLB flushes don't improve performance much. At least in a test similar to the one above, we don't find observable improvement.
From the design point of view, I personally prefer to use similar logic between PMD and PTE unless it hurts performance. IMHO, less different logic means less mental overhead. Do you agree?
Quoted 41 lines
Quoted 41 lines
arch/arm64/include/asm/pgtable.h +0
-0/* * ZERO_PAGE is a global shared page that is always zero: used arch/arm64/include/asm/tlbflush.h +30
-0@@ -282,6 +282,39 @@ static inline void flush_tlb_mm(struct mm_struct *mm)mmu_notifier_arch_invalidate_secondary_tlbs (mm,0 ,-1UL ); } +static inline void __local_flush_tlb_page_nonotify_nosync ( +struct mm_struct *mm,unsigned long uaddr) +{ +unsigned long addr; + +dsb (nshst); + addr =__TLBI_VADDR (uaddr,ASID (mm)); +__tlbi (vale1, addr); +__tlbi_user (vale1, addr); +} + +static inline void local_flush_tlb_page_nonotify ( +struct vm_area_struct *vma,unsigned long uaddr) +{ +__local_flush_tlb_page_nonotify_nosync (vma->vm_mm, uaddr); +dsb (nsh); +} + +static inline void __local_flush_tlb_page_nosync ( +struct mm_struct *mm,unsigned long uaddr) +{ +__local_flush_tlb_page_nonotify_nosync (mm, uaddr); +mmu_notifier_arch_invalidate_secondary_tlbs (mm, uaddr &PAGE_MASK , + (uaddr &PAGE_MASK ) +PAGE_SIZE ); +} + +static inline void local_flush_tlb_page (struct vm_area_struct *vma, +unsigned long uaddr) +{ +__local_flush_tlb_page_nosync (vma->vm_mm, uaddr);
Quoted 8 lines
arch/arm64/include/asm/tlbflush.h static inline void flush_tlb_mm(struct mm_struct *mm) · line 302
Quoted 3 lines
arch/arm64/include/asm/tlbflush.h +3
-0+dsb (nsh); +} +You're introducing more variants than you're actually using here. I think you just need
local_flush_tlb_page()andlocal_flush_tlb_page_nonotify(); you could keep__local_flush_tlb_page_nonotify_nosync()and drop__local_flush_tlb_page_nosync()since it's not really adding much?
Sure.
Quoted 7 lines
But I'm also wondering if we should tidy up this API in general; we have local vs broadcast, sync vs nosync, notify vs nonotify. And page is really just a special case of a range. So perhaps it is better to rework the range API to take some flags and we can tidy up all of this. I know Will also posted an RFC to convert a lot of this to c functions, which should also be included. Not a blocker for this change, I don't think, but there should definitely be some follow up work to tidy it up. (I'm happy to take it on).
This sounds goo to me. Thanks for working on this!
Quoted 19 lines
arch/arm64/mm/fault.c int __ptep_set_access_flags(struct vm_area_struct *vma, · line 238
Quoted 13 lines
arch/arm64/include/asm/tlbflush.h +0
-0static inline void __flush_tlb_page_nosync (struct mm_struct *mm,unsigned long uaddr) {arch/arm64/mm/fault.c +1
-1@@ -235,7 +235,7 @@ int __ptep_set_access_flags(struct vm_area_struct *vma,/* Invalidate a stale read-only entry */ if (dirty) -flush_tlb_page (vma, address); +local_flush_tlb_page (vma, address);Given this is called for both pmds and ptes, it's a bit of an abuse to flush a *page*. Yes it is architecturally safe, but it's not exactly self-documenting. If we pass in the pgsize (as per above) it could be optimized given we know the level and we only want to invalidate the last level. e.g. the local equivalent to:
Quoted 7 lines
__flush_tlb_range(vma, address, address + PMD_SIZE, PMD_SIZE, true, 2);or
__flush_tlb_range(vma, address, address + PAGE_SIZE, PAGE_SIZE, true, 3);Again though, perhaps that is part of some follow up tidying work?
This sounds good to me too. And, Yes, I think this can be a follow up tidying work.
Quoted 3 lines
contpte_ptep_set_access_flags()currently does a (broadcast)__flush_tlb_range()on the (pte_write(orig_pte)==pte_write(entry)) path. I think that should be changed to a local range invalidation to be consistent with this change.
Yes. This should be changed too. However, it means we need a local variant of __flush_tlb_range() and flush_tlb_mm(). Is it OK to introduce them first and tidy up later?
Quoted 3 lines
arch/arm64/mm/fault.c int __ptep_set_access_flags(struct vm_area_struct *vma, · line 241
Quoted 3 lines
arch/arm64/mm/fault.c +0
-0return 1 ; }
---
Best Regards,
Huang, Ying
Catalin Marinas
maintainerRe: [RFC PATCH 2/2] arm64, tlbflush: don't TLBI broadcast if page reused in write fault
Quoted 8 lines · Ying
Quoted 3 lines · Ryan Roberts
contpte_ptep_set_access_flags()currently does a (broadcast)__flush_tlb_range()on the (pte_write(orig_pte)==pte_write(entry)) path. I think that should be changed to a local range invalidation to be consistent with this change.Yes. This should be changed too. However, it means we need a local variant of
__flush_tlb_range()andflush_tlb_mm(). Is it OK to introduce them first and tidy up later?
If it's just for contpte, we'd never take the flush_tlb_mm() path. So we could instead add a specific local_flush_tlb_contpte_range().
Signature · 1 line
Catalin
Re: [RFC PATCH 2/2] arm64, tlbflush: don't TLBI broadcast if page reused in write fault
Quoted 6 lines · Catalin Marinas
Quoted 5 lines · Ying
Quoted 3 lines · Ryan Roberts
contpte_ptep_set_access_flags()currently does a (broadcast)__flush_tlb_range()on the (pte_write(orig_pte)==pte_write(entry)) path. I think that should be changed to a local range invalidation to be consistent with this change.
Quoted 6 lines
Quoted 3 lines
Yes. This should be changed too. However, it means we need a local variant of
__flush_tlb_range()andflush_tlb_mm(). Is it OK to introduce them first and tidy up later?If it's just for contpte, we'd never take the
flush_tlb_mm()path. So we could instead add a specificlocal_flush_tlb_contpte_range().
Sure.
---
Best Regards,
Huang, Ying
Ryan Roberts
reviewerRe: [RFC PATCH 2/2] arm64, tlbflush: don't TLBI broadcast if page reused in write fault
In reply to Huang, YingQuoted 119 lines · Ying
Hi, Ryan,
Thanks for reivew!
Quoted 36 lines · Ryan Roberts
Quoted 27 lines · Huang Ying
A multi-thread customer workload with large memory footprint uses fork()/exec() to run some external programs every tens seconds. When running the workload on an arm64 server machine, it's observed that quite some CPU cycles are spent in the TLB flushing functions. While running the workload on the
x86_64server machine, it's not. This causes the performance on arm64 to be much worse than that onx86_64.During the workload running, after fork()/exec() write-protects all pages in the parent process, memory writing in the parent process will cause a write protection fault. Then the page fault handler will make the PTE/PDE writable if the page can be reused, which is almost always true in the workload. On arm64, to avoid the write protection fault on other CPUs, the page fault handler flushes the TLB globally with TLBI broadcast after changing the PTE/PDE. However, this isn't always necessary. Firstly, it's safe to leave some stall read-only TLB entries as long as they will be flushed finally. Secondly, it's quite possible that the original read-only PTE/PDEs aren't cached in remote TLB at all if the memory footprint is large. In fact, on
x86_64, the page fault handler doesn't flush the remote TLB in this situation, which benefits the performance a lot.To improve the performance on arm64, make the write protection fault handler flush the TLB locally instead of globally via TLBI broadcast after making the PTE/PDE writable. If there are stall read-only TLB entries in the remote CPUs, the page fault handler on these CPUs will regard the page fault as spurious and flush the stall TLB entries.
Quoted 7 lines
To test the patchset, make the
usemem.cfrom
vm-scalability (https://git.kernel.org/pub/scm/linux/kernel/git/wfg/vm-scalability.git).
support calling fork()/exec() periodically. To mimic the behavior of
the customer workload, run usemem with 4 threads, access 100GB memory,
and call fork()/exec() every 40 seconds. Test results show that with
the patchset the score of usemem improves ~40.6%. The cycles% of TLB flush functions reduces from ~50.5% to ~0.3% in perf profile.Quoted 4 lines
Overall, this looks like a simple and useful performance optimization - thanks! I'm running this through our performance regression suite to see if it spots any workloads where the change slows things down and will report once we have the results.
Thanks!
Quoted 63 lines
Quoted 41 lines
Signed-off-by Huang Ying <[email protected]>Cc Catalin Marinas <[email protected]>Cc Will Deacon <[email protected]>Cc Andrew Morton <[email protected]>Cc David Hildenbrand <[email protected]>Cc Lorenzo Stoakes <[email protected]>Cc Vlastimil Babka <[email protected]>Cc Zi Yan <[email protected]>Cc Baolin Wang <[email protected]>Cc Ryan Roberts <[email protected]>Cc Yang Shi <[email protected]>Cc "Christoph Lameter (Ampere)" <[email protected]>Cc Dev Jain <[email protected]>Cc Barry Song <[email protected]>Cc Anshuman Khandual <[email protected]>Cc Yicong Yang <[email protected]>Cc Kefeng Wang <[email protected]>Cc Kevin Brodsky <[email protected]>Cc Yin Fengwei <[email protected]>--- arch/arm64/include/asm/pgtable.h | 14 ++++++++----- arch/arm64/include/asm/tlbflush.h | 33 +++++++++++++++++++++++++++++++ arch/arm64/mm/fault.c | 2 +- 3 files changed, 43 insertions(+), 6 deletions(-)arch/arm64/include/asm/pgtable.h +1
-4@@ -130,12 +130,16 @@ static inline void arch_leave_lazy_mmu_mode(void)#endif /* CONFIG_TRANSPARENT_HUGEPAGE */ /* -* Outside of a few very special situations (e.g. hibernation), we always -* use broadcast TLB invalidation instructions, therefore a spurious page -* fault on one CPU which has been handled concurrently by another CPU -* does not need to perform additional invalidation. +* We use local TLB invalidation instruction when reusing page in arch/arm64/include/asm/pgtable.h static inline void arch_leave_lazy_mmu_mode(void) · line 142
Quoted 10 lines
arch/arm64/include/asm/pgtable.h +8
-1+* write protection fault handler to avoid TLBI broadcast in the hot +* path. This will cause spurious page faults if stall read-only TLB +* entries exist. */ -#define flush_tlb_fix_spurious_fault (vma, address, ptep) do { } while (0) +#define flush_tlb_fix_spurious_fault (vma, address, ptep) \ + local_flush_tlb_page_nonotify(vma, address) + +#define flush_tlb_fix_spurious_fault_pmd (vma, address, pmdp) \ + local_flush_tlb_page_nonotify(vma, address)It's not really clear to me how important doing local tlb flushes for pmds is for the performance improvement? I'm guessing most of the win comes from the pte level? I suspect you have only added spurious pmd fault handling because the arm64 implementation of
__ptep_set_access_flags()actually handles both pte and pmd levels?Given the core kernel didn't previously have support for pmd spurious faults, I wonder if it would be simpler to drop the first patch and rejig
__ptep_set_access_flags()so that it has a pgsize parameter and can differentiate based on that? I'm on the fence...Quoted 6 lines
If you do end up taking this approach, there is a style I introduced for hugetlb, where the function is suffixed with
_anyszand it takes a pgsize param:int __ptep_set_access_flags_anysz(struct
vm_area_struct*vma,unsigned long address, pte_t *ptep, pte_t entry, int dirty, unsigned long pgsize);
Quoted 7 lines
From the performance point of view, local TLB flushes don't improve performance much. At least in a test similar to the one above, we don't find observable improvement.
From the design point of view, I personally prefer to use similar logic between PMD and PTE unless it hurts performance. IMHO, less different logic means less mental overhead. Do you agree?
Yeah fair enough, that's a reasonable argument.
Quoted 61 lines
Quoted 42 lines
Quoted 42 lines
arch/arm64/include/asm/pgtable.h +0
-0/* * ZERO_PAGE is a global shared page that is always zero: used arch/arm64/include/asm/tlbflush.h +30
-0@@ -282,6 +282,39 @@ static inline void flush_tlb_mm(struct mm_struct *mm)mmu_notifier_arch_invalidate_secondary_tlbs (mm,0 ,-1UL ); } +static inline void __local_flush_tlb_page_nonotify_nosync ( +struct mm_struct *mm,unsigned long uaddr) +{ +unsigned long addr; + +dsb (nshst); + addr =__TLBI_VADDR (uaddr,ASID (mm)); +__tlbi (vale1, addr); +__tlbi_user (vale1, addr); +} + +static inline void local_flush_tlb_page_nonotify ( +struct vm_area_struct *vma,unsigned long uaddr) +{ +__local_flush_tlb_page_nonotify_nosync (vma->vm_mm, uaddr); +dsb (nsh); +} + +static inline void __local_flush_tlb_page_nosync ( +struct mm_struct *mm,unsigned long uaddr) +{ +__local_flush_tlb_page_nonotify_nosync (mm, uaddr); +mmu_notifier_arch_invalidate_secondary_tlbs (mm, uaddr &PAGE_MASK , + (uaddr &PAGE_MASK ) +PAGE_SIZE ); +} + +static inline void local_flush_tlb_page (struct vm_area_struct *vma, +unsigned long uaddr) +{ +__local_flush_tlb_page_nosync (vma->vm_mm, uaddr);Quoted 8 lines
arch/arm64/include/asm/tlbflush.h static inline void flush_tlb_mm(struct mm_struct *mm) · line 302
Quoted 3 lines
arch/arm64/include/asm/tlbflush.h +3
-0+dsb (nsh); +} +You're introducing more variants than you're actually using here. I think you just need
local_flush_tlb_page()andlocal_flush_tlb_page_nonotify(); you could keep__local_flush_tlb_page_nonotify_nosync()and drop__local_flush_tlb_page_nosync()since it's not really adding much?Sure.
Quoted 7 lines
But I'm also wondering if we should tidy up this API in general; we have local vs broadcast, sync vs nosync, notify vs nonotify. And page is really just a special case of a range. So perhaps it is better to rework the range API to take some flags and we can tidy up all of this. I know Will also posted an RFC to convert a lot of this to c functions, which should also be included. Not a blocker for this change, I don't think, but there should definitely be some follow up work to tidy it up. (I'm happy to take it on).
Quoted 1 line
This sounds goo to me. Thanks for working on this!
OK I'll put it on my todo list to have a go at this as follow up.
Quoted 22 lines
Quoted 22 lines
Quoted 9 lines
arch/arm64/include/asm/tlbflush.h +0
-0static inline void __flush_tlb_page_nosync (struct mm_struct *mm,unsigned long uaddr) {arch/arm64/mm/fault.c +0
-0arch/arm64/mm/fault.c int __ptep_set_access_flags(struct vm_area_struct *vma, · line 238
Quoted 5 lines
arch/arm64/mm/fault.c +1
-1@@ -235,7 +235,7 @@ int __ptep_set_access_flags(struct vm_area_struct *vma,/* Invalidate a stale read-only entry */ if (dirty) -flush_tlb_page (vma, address); +local_flush_tlb_page (vma, address);Given this is called for both pmds and ptes, it's a bit of an abuse to flush a *page*. Yes it is architecturally safe, but it's not exactly self-documenting. If we pass in the pgsize (as per above) it could be optimized given we know the level and we only want to invalidate the last level. e.g. the local equivalent to:
__flush_tlb_range(vma, address, address + PMD_SIZE, PMD_SIZE, true, 2);
Quoted 8 lines
Quoted 5 lines
or
__flush_tlb_range(vma, address, address + PAGE_SIZE, PAGE_SIZE, true, 3);Again though, perhaps that is part of some follow up tidying work?
This sounds good to me too. And, Yes, I think this can be a follow up tidying work.
I'll include this in my follow up.
Quoted 8 lines
Quoted 4 lines
contpte_ptep_set_access_flags()currently does a (broadcast)__flush_tlb_range()on the (pte_write(orig_pte)==pte_write(entry)) path. I think that should be changed to a local range invalidation to be consistent with this change.Yes. This should be changed too. However, it means we need a local variant of
__flush_tlb_range()andflush_tlb_mm(). Is it OK to introduce them first and tidy up later?
I think do it as Catalin suggested for now. I'll then refactor as part of the tidy up.
Thanks,
Ryan
Quoted 8 lines
Quoted 4 lines
arch/arm64/mm/fault.c int __ptep_set_access_flags(struct vm_area_struct *vma, · line 241
Quoted 4 lines
arch/arm64/mm/fault.c +0
-0return 1 ; }---
Best Regards,
Huang, Ying