arm64: Refactor TLB invalidation API and implementation
Subsystems (3)
v3 cover arm64: Refactor TLB invalidation API and implementation
Hi All,
This series refactors the TLB invalidation API to make it more general and flexible, and refactors the implementation, aiming to make it more robust, easier to understand and easier to add new features in future.
It is heavily based on the series posted by Will back in July at [1]; I've attempted to maintain correct authorship and tags - apologies if I got any of the etiquette wrong.
The first 8 patches reimplement the full scope of Will's series, but fixed up to use function pointers instead of the enum, as per Linus's suggestion. Patches 9-12 then reformulate the API for the range- and page-based functions to remove all the "nosync", "nonotify" and "local" function variants and replace with a set of flags to modify the behaviour instead. This allows having a single implementation that can rely on constant folding. IMO It's much cleaner and more flexible. Finally, patch 13 provides a minor theoretical performance improvement by hinting the TTL for page-based invalidations (the preceeding API improvements made that pretty simple).
We have a couple of other things in the queue to put on top of this series, which these changes make simpler:
- Optimization to only do local TLBI when an mm is single-threaded
- Introduce TLBIP for use with D128 pgtables
The series applies on top of v7.0-rc2, I've compile tested each patch and run mm selftests for the end result in a VM on Apple M2; all tests pass. I've run an earlier version of this code through our performance benchmarking system and no regressions were found. I've looked at the generated instructions and all the expected constant folding seems to be happening, and I've checked code size before and after; there is no significant change.
Changes since v1 [2]
====================
- patch 1: Converted mask+FIELD_PREP() to FIELD_MODIFY() (per Jonathan C)
- patch 1,2,3: Fixed botched rebase (per Linu C)
- patch 3: Fixed whitespace error (per Jonathan C)
- patch 8: Modified __flush_tlb_range_limit_excess() logic (per Dev J)
- patch 10: Fixed documentation bug (per Linu C)
- patch 10,11: Added documentation for each TLBF_ flag (per Linu C)
- Collected R-bs from Linu - thanks!
Changes since v2 [3]
====================
- patch 4: Fixed bug: __kvm_tlb_flush_vmid_ipa[_nsh]() now passes IPA not PFN since conversion is done in __tlbi_level() (found during testing by Linu C)
- patch 11: Renamed ___flush_tlb_range -> __do_flush_tlb_range (per Jonathan C)
- patch 11: Removed flags=TLBF_NOBROADCAST case and replaced with BUG(). Nobody is using combination so avoid dead code (per Jonathan C)
- Collected R-bs from Jonathan C - thanks!
[1] https://lore.kernel.org/linux-arm-kernel/[email protected]/
[2] https://lore.kernel.org/all/[email protected]/
[3] https://lore.kernel.org/linux-arm-kernel/[email protected]/
Thanks,
Ryan
Ryan Roberts (9):
arm64: mm: Re-implement the __tlbi_level macro as a C function
arm64: mm: Introduce a C wrapper for by-range TLB invalidation
arm64: mm: Implicitly invalidate user ASID based on TLBI operation
arm64: mm: Re-implement the __flush_tlb_range_op macro in C
arm64: mm: Refactor flush_tlb_page() to use __tlbi_level_asid()
arm64: mm: Refactor __flush_tlb_range() to take flags
arm64: mm: More flags for __flush_tlb_range()
arm64: mm: Wrap flush_tlb_page() around __do_flush_tlb_range()
arm64: mm: Provide level hint for flush_tlb_page()
Will Deacon (4):
arm64: mm: Push __TLBI_VADDR() into __tlbi_level()
arm64: mm: Inline __TLBI_VADDR_RANGE() into __tlbi_range()
arm64: mm: Simplify __TLBI_RANGE_NUM() macro
arm64: mm: Simplify __flush_tlb_range_limit_excess()
arch/arm64/include/asm/hugetlb.h | 12 +- arch/arm64/include/asm/pgtable.h | 13 +- arch/arm64/include/asm/tlb.h | 6 +- arch/arm64/include/asm/tlbflush.h | 461 +++++++++++++++++------------- arch/arm64/kernel/sys_compat.c | 2 +- arch/arm64/kvm/hyp/nvhe/mm.c | 2 +- arch/arm64/kvm/hyp/nvhe/tlb.c | 2 - arch/arm64/kvm/hyp/pgtable.c | 4 +- arch/arm64/kvm/hyp/vhe/tlb.c | 2 - arch/arm64/mm/contpte.c | 12 +- arch/arm64/mm/fault.c | 2 +- arch/arm64/mm/hugetlbpage.c | 4 +- arch/arm64/mm/mmu.c | 2 +- 13 files changed, 290 insertions(+), 234 deletions(-)
--
2.43.0
PATCH v3 1/13 arm64: mm: Re-implement the __tlbi_level macro as a C function
As part of efforts to reduce our reliance on complex preprocessor macros for TLB invalidation routines, convert the __tlbi_level macro to a C function for by-level TLB invalidation.
Each specific tlbi level op is implemented as a C function and the appropriate function pointer is passed to __tlbi_level(). Since everything is declared inline and is statically resolvable, the compiler will convert the indirect function call to a direct inline execution.
--- arch/arm64/include/asm/tlbflush.h | 67 +++++++++++++++++++++++++------ 1 file changed, 54 insertions(+), 13 deletions(-)
arch/arm64/include/asm/tlbflush.h +54 -13
@@ -97,19 +97,60 @@ static inline unsigned long get_trans_granule(void)#define TLBI_TTL_UNKNOWN INT_MAX -#define __tlbi_level (op, addr, level) do { \ - u64 arg = addr; \ - \ - if (alternative_has_cap_unlikely(ARM64_HAS_ARMv8_4_TTL) && \ - level >= 0 && level <= 3) { \ - u64 ttl = level & 3; \ - ttl |= get_trans_granule() << 2; \ - arg &= ~TLBI_TTL_MASK; \ - arg |= FIELD_PREP(TLBI_TTL_MASK, ttl); \ - } \ - \ - __tlbi(op, arg); \ -} while(0) +typedef void (*tlbi_op )(u64 arg); + +static __always_inline void vae1is(u64 arg) +{ +__tlbi (vae1is, arg); +} + +static __always_inline void vae2is(u64 arg) +{ +__tlbi (vae2is, arg); +} + +static __always_inline void vale1(u64 arg) +{ +__tlbi (vale1, arg); +} + +static __always_inline void vale1is(u64 arg) +{ +__tlbi (vale1is, arg); +} + +static __always_inline void vale2is(u64 arg) +{ +__tlbi (vale2is, arg); +} + +static __always_inline void vaale1is(u64 arg) +{ +__tlbi (vaale1is, arg); +} + +static __always_inline void ipas2e1(u64 arg) +{ +__tlbi (ipas2e1, arg); +} + +static __always_inline void ipas2e1is(u64 arg) +{ +__tlbi (ipas2e1is, arg); +} + +static __always_inline void __tlbi_level(tlbi_op op, u64 addr, u32 level) +{ +u64 arg = addr; + +if (alternative_has_cap_unlikely (ARM64_HAS_ARMv8_4_TTL) && level <=3 ) { +u64 ttl = level | (get_trans_granule () <<2 ); + +FIELD_MODIFY (TLBI_TTL_MASK , &arg, ttl); + } + +op (arg); +}#define __tlbi_user_level (op, arg, level) do { \ if (arm64_kernel_unmapped_at_el0()) \
Signature · 1 line
2.43.0
PATCH v3 2/13 arm64: mm: Introduce a C wrapper for by-range TLB invalidation
In reply to Ryan RobertsAs part of efforts to reduce our reliance on complex preprocessor macros for TLB invalidation routines, introduce a new C wrapper for by-range TLB invalidation which can be used instead of the __tlbi() macro and can additionally be called from C code.
Each specific tlbi range op is implemented as a C function and the appropriate function pointer is passed to __tlbi_range(). Since everything is declared inline and is statically resolvable, the compiler will convert the indirect function call to a direct inline execution.
--- arch/arm64/include/asm/tlbflush.h | 32 ++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-)
arch/arm64/include/asm/tlbflush.h +31 -1
@@ -468,6 +468,36 @@ static inline void arch_tlbbatch_flush(struct arch_tlbflush_unmap_batch *batch)* operations can only span an even number of pages. We save this for last to * ensure 64KB start alignment is maintained for the LPA2 case. */ +static __always_inline void rvae1is(u64 arg) +{ +__tlbi (rvae1is, arg); +} + +static __always_inline void rvale1(u64 arg) +{ +__tlbi (rvale1, arg); +} + +static __always_inline void rvale1is(u64 arg) +{ +__tlbi (rvale1is, arg); +} + +static __always_inline void rvaale1is(u64 arg) +{ +__tlbi (rvaale1is, arg); +} + +static __always_inline void ripas2e1is(u64 arg) +{ +__tlbi (ripas2e1is, arg); +} + +static __always_inline void __tlbi_range(tlbi_op op, u64 arg) +{ +op (arg); +} +#define __flush_tlb_range_op (op, start, pages, stride, \ asid, tlb_level, tlbi_user, lpa2) \ do { \
@@ -495,7 +525,7 @@ do { \if (num >=0 ) { \ addr =__TLBI_VADDR_RANGE (__flush_start >> shift, asid, \ scale, num, tlb_level); \ - __tlbi(r##op, addr); \ + __tlbi_range(r##op, addr); \ if (tlbi_user) \ __tlbi_user(r##op, addr); \ __flush_start += __TLBI_RANGE_PAGES(num, scale) << PAGE_SHIFT; \
Signature · 1 line
2.43.0
PATCH v3 3/13 arm64: mm: Implicitly invalidate user ASID based on TLBI operation
In reply to Ryan RobertsWhen kpti is enabled, separate ASIDs are used for userspace and kernelspace, requiring ASID-qualified TLB invalidation by virtual address to invalidate both of them.
Push the logic for invalidating the two ASIDs down into the low-level tlbi-op-specific functions and remove the burden from the caller to handle the kpti-specific behaviour.
--- arch/arm64/include/asm/tlbflush.h | 30 +++++++++++++----------------- 1 file changed, 13 insertions(+), 17 deletions(-)
arch/arm64/include/asm/tlbflush.h +13 -17
@@ -102,6 +102,7 @@ typedef void (*tlbi_op)(u64 arg); static __always_inline void vae1is(u64 arg) {__tlbi (vae1is, arg); +__tlbi_user (vae1is, arg); } static __always_inline void vae2is(u64 arg)
@@ -112,11 +113,13 @@ static __always_inline void vae2is(u64 arg) static __always_inline void vale1(u64 arg) {__tlbi (vale1, arg); +__tlbi_user (vale1, arg); } static __always_inline void vale1is(u64 arg) {__tlbi (vale1is, arg); +__tlbi_user (vale1is, arg); } static __always_inline void vale2is(u64 arg)
@@ -152,11 +155,6 @@ static __always_inline void __tlbi_level(tlbi_op op, u64 addr, u32 level)op (arg); } -#define __tlbi_user_level (op, arg, level) do { \ - if (arm64_kernel_unmapped_at_el0()) \ - __tlbi_level(op, (arg | USER_ASID_FLAG), level); \ -} while (0) -/* * This macro creates a properly formatted VA operand for the TLB RANGE. The * value bit assignments are:
@@ -444,8 +442,6 @@ static inline void arch_tlbbatch_flush(struct arch_tlbflush_unmap_batch *batch)* @stride: Flush granularity * @asid: The ASID of the task (0 for IPA instructions) * @tlb_level: Translation Table level hint, if known -* @tlbi_user: If 'true', call an additional __tlbi_user() -* (typically for user ASIDs). 'flase' for IPA instructions * @lpa2: If 'true', the lpa2 scheme is used as set out below * * When the CPU does not support TLB range operations, flush the TLB
@@ -471,16 +467,19 @@ static inline void arch_tlbbatch_flush(struct arch_tlbflush_unmap_batch *batch) static __always_inline void rvae1is(u64 arg) {__tlbi (rvae1is, arg); +__tlbi_user (rvae1is, arg); } static __always_inline void rvale1(u64 arg) {__tlbi (rvale1, arg); +__tlbi_user (rvale1, arg); } static __always_inline void rvale1is(u64 arg) {__tlbi (rvale1is, arg); +__tlbi_user (rvale1is, arg); } static __always_inline void rvaale1is(u64 arg)
@@ -499,7 +498,7 @@ static __always_inline void __tlbi_range(tlbi_op op, u64 arg) }#define __flush_tlb_range_op (op, start, pages, stride, \ - asid, tlb_level, tlbi_user, lpa2) \ + asid, tlb_level, lpa2) \ do { \ typeof(start) __flush_start = start; \ typeof(pages) __flush_pages = pages; \
@@ -514,8 +513,6 @@ do { \ (lpa2 && __flush_start != ALIGN(__flush_start, SZ_64K))) { \ addr =__TLBI_VADDR (__flush_start, asid); \__tlbi_level (op, addr, tlb_level); \ -if (tlbi_user) \ -__tlbi_user_level (op, addr, tlb_level); \ __flush_start += stride; \ __flush_pages -= stride >>PAGE_SHIFT ; \continue ; \
@@ -526,8 +523,6 @@ do { \ addr =__TLBI_VADDR_RANGE (__flush_start >> shift, asid, \ scale, num, tlb_level); \ __tlbi_range(r##op, addr); \ - if (tlbi_user) \ - __tlbi_user(r##op, addr); \ __flush_start +=__TLBI_RANGE_PAGES (num, scale) <<PAGE_SHIFT ; \ __flush_pages -=__TLBI_RANGE_PAGES (num, scale);\ } \
@@ -536,7 +531,7 @@ do { \ } while (0)#define __flush_s2_tlb_range_op (op, start, pages, stride, tlb_level) \ - __flush_tlb_range_op(op, start, pages, stride, 0, tlb_level, false, kvm_lpa2_is_enabled()); + __flush_tlb_range_op(op, start, pages, stride, 0, tlb_level, kvm_lpa2_is_enabled());static inline bool __flush_tlb_range_limit_excess (unsigned long start,unsigned long end,unsigned long pages,unsigned long stride)
@@ -576,10 +571,10 @@ static inline void __flush_tlb_range_nosync(struct mm_struct *mm,if (last_level)__flush_tlb_range_op (vale1is, start, pages, stride, asid, - tlb_level, true,lpa2_is_enabled ()); + tlb_level,lpa2_is_enabled ());else __flush_tlb_range_op (vae1is, start, pages, stride, asid, - tlb_level, true,lpa2_is_enabled ()); + tlb_level,lpa2_is_enabled ());mmu_notifier_arch_invalidate_secondary_tlbs (mm, start, end); }
@@ -604,7 +599,7 @@ static inline void local_flush_tlb_contpte(struct vm_area_struct *vma,dsb (nshst); asid =ASID (vma->vm_mm);__flush_tlb_range_op (vale1, addr,CONT_PTES ,PAGE_SIZE , asid, -3 , true,lpa2_is_enabled ()); +3 ,lpa2_is_enabled ());mmu_notifier_arch_invalidate_secondary_tlbs (vma->vm_mm, addr, addr +CONT_PTE_SIZE );dsb (nsh);
@@ -638,7 +633,7 @@ static inline void flush_tlb_kernel_range(unsigned long start, unsigned long enddsb (ishst);__flush_tlb_range_op (vaale1is, start, pages, stride,0 , -TLBI_TTL_UNKNOWN , false,lpa2_is_enabled ()); +TLBI_TTL_UNKNOWN ,lpa2_is_enabled ());__tlbi_sync_s1ish ();isb (); }
@@ -689,6 +684,7 @@ static inline bool huge_pmd_needs_flush(pmd_t oldpmd, pmd_t newpmd) }#define huge_pmd_needs_flush huge_pmd_needs_flush +#undef __tlbi_user#endif #endif
Signature · 1 line
2.43.0
PATCH v3 4/13 arm64: mm: Push __TLBI_VADDR() into __tlbi_level()
In reply to Ryan RobertsFrom: Will Deacon <[email protected]>
The __TLBI_VADDR() macro takes an ASID and an address and converts them into a single argument formatted correctly for a TLB invalidation instruction.
Rather than have callers worry about this (especially in the case where the ASID is zero), push the macro down into __tlbi_level() via a new __tlbi_level_asid() helper.
--- arch/arm64/include/asm/tlbflush.h | 14 ++++++++++---- arch/arm64/kernel/sys_compat.c | 2 +- arch/arm64/kvm/hyp/nvhe/mm.c | 2 +- arch/arm64/kvm/hyp/nvhe/tlb.c | 2 -- arch/arm64/kvm/hyp/pgtable.c | 4 ++-- arch/arm64/kvm/hyp/vhe/tlb.c | 2 -- 6 files changed, 14 insertions(+), 12 deletions(-)
arch/arm64/include/asm/tlbflush.h +10 -4
@@ -142,9 +142,10 @@ static __always_inline void ipas2e1is(u64 arg)__tlbi (ipas2e1is, arg); } -static __always_inline void __tlbi_level(tlbi_op op, u64 addr, u32 level) +static __always_inline void __tlbi_level_asid(tlbi_op op, u64 addr, u32 level, +u16 asid) { -u64 arg = addr; +u64 arg =__TLBI_VADDR (addr, asid);if (alternative_has_cap_unlikely (ARM64_HAS_ARMv8_4_TTL) && level <=3 ) {u64 ttl = level | (get_trans_granule () <<2 );
@@ -155,6 +156,11 @@ static __always_inline void __tlbi_level(tlbi_op op, u64 addr, u32 level)op (arg); } +static inline void __tlbi_level (tlbi_op op,u64 addr,u32 level) +{ +__tlbi_level_asid (op, addr, level,0 ); +} +/* * This macro creates a properly formatted VA operand for the TLB RANGE. The * value bit assignments are:
@@ -511,8 +517,7 @@ do { \if (!system_supports_tlb_range () || \ __flush_pages ==1 || \ (lpa2 && __flush_start !=ALIGN (__flush_start,SZ_64K ))) { \ - addr =__TLBI_VADDR (__flush_start, asid); \ -__tlbi_level (op, addr, tlb_level); \ +__tlbi_level_asid (op, __flush_start, tlb_level, asid); \ __flush_start += stride; \ __flush_pages -= stride >>PAGE_SHIFT ; \continue ; \
@@ -685,6 +690,7 @@ static inline bool huge_pmd_needs_flush(pmd_t oldpmd, pmd_t newpmd)#define huge_pmd_needs_flush huge_pmd_needs_flush#undef __tlbi_user +#undef __TLBI_VADDR#endif #endif
arch/arm64/kernel/sys_compat.c +1 -1
@@ -36,7 +36,7 @@ __do_compat_cache_op(unsigned long start, unsigned long end)* The workaround requires an inner-shareable tlbi. * We pick the reserved-ASID to minimise the impact. */ -__tlbi (aside1is,__TLBI_VADDR (0 ,0 )); +__tlbi (aside1is,0UL );__tlbi_sync_s1ish (); }
arch/arm64/kvm/hyp/nvhe/mm.c +1 -1
@@ -270,7 +270,7 @@ static void fixmap_clear_slot(struct hyp_fixmap_slot *slot)* https://lore.kernel.org/kvm/[email protected]/T/#mf10dfbaf1eaef9274c581b81c53758918c1d0f03 */ dsb (ishst); -__tlbi_level (vale2is,__TLBI_VADDR (addr,0 ), level); +__tlbi_level (vale2is, addr, level);__tlbi_sync_s1ish_hyp ();isb (); }
arch/arm64/kvm/hyp/nvhe/tlb.c +0 -2
@@ -158,7 +158,6 @@ void __kvm_tlb_flush_vmid_ipa(struct kvm_s2_mmu *mmu,* Instead, we invalidate Stage-2 for this IPA, and the * whole of Stage-1. Weep... */ - ipa >>=12 ;__tlbi_level (ipas2e1is, ipa, level);/*
@@ -188,7 +187,6 @@ void __kvm_tlb_flush_vmid_ipa_nsh(struct kvm_s2_mmu *mmu,* Instead, we invalidate Stage-2 for this IPA, and the * whole of Stage-1. Weep... */ - ipa >>=12 ;__tlbi_level (ipas2e1, ipa, level);/*
arch/arm64/kvm/hyp/pgtable.c +2 -2
@@ -490,14 +490,14 @@ static int hyp_unmap_walker(const struct kvm_pgtable_visit_ctx *ctx,kvm_clear_pte (ctx->ptep);dsb (ishst); -__tlbi_level (vae2is,__TLBI_VADDR (ctx->addr,0 ),TLBI_TTL_UNKNOWN ); +__tlbi_level (vae2is, ctx->addr,TLBI_TTL_UNKNOWN ); }else {if (ctx->end - ctx->addr < granule)return -EINVAL ;kvm_clear_pte (ctx->ptep);dsb (ishst); -__tlbi_level (vale2is,__TLBI_VADDR (ctx->addr,0 ), ctx->level); +__tlbi_level (vale2is, ctx->addr, ctx->level); *unmapped += granule; }
arch/arm64/kvm/hyp/vhe/tlb.c +0 -2
@@ -104,7 +104,6 @@ void __kvm_tlb_flush_vmid_ipa(struct kvm_s2_mmu *mmu,* Instead, we invalidate Stage-2 for this IPA, and the * whole of Stage-1. Weep... */ - ipa >>=12 ;__tlbi_level (ipas2e1is, ipa, level);/*
@@ -136,7 +135,6 @@ void __kvm_tlb_flush_vmid_ipa_nsh(struct kvm_s2_mmu *mmu,* Instead, we invalidate Stage-2 for this IPA, and the * whole of Stage-1. Weep... */ - ipa >>=12 ;__tlbi_level (ipas2e1, ipa, level);/*
Signature · 1 line
2.43.0
PATCH v3 5/13 arm64: mm: Inline __TLBI_VADDR_RANGE() into __tlbi_range()
In reply to Ryan RobertsFrom: Will Deacon <[email protected]>
The __TLBI_VADDR_RANGE() macro is only used in one place and isn't something that's generally useful outside of the low-level range invalidation gubbins.
Inline __TLBI_VADDR_RANGE() into the __tlbi_range() function so that the macro can be removed entirely.
--- arch/arm64/include/asm/tlbflush.h | 32 +++++++++++++------------------ 1 file changed, 13 insertions(+), 19 deletions(-)
arch/arm64/include/asm/tlbflush.h +13 -19
@@ -186,19 +186,6 @@ static inline void __tlbi_level(tlbi_op op, u64 addr, u32 level)#define TLBIR_TTL_MASK GENMASK_ULL(38, 37)#define TLBIR_BADDR_MASK GENMASK_ULL(36, 0) -#define __TLBI_VADDR_RANGE (baddr, asid, scale, num, ttl) \ - ({ \ - unsigned long __ta = 0; \ - unsigned long __ttl = (ttl >= 1 && ttl <= 3) ? ttl : 0; \ - __ta |= FIELD_PREP(TLBIR_BADDR_MASK, baddr); \ - __ta |= FIELD_PREP(TLBIR_TTL_MASK, __ttl); \ - __ta |= FIELD_PREP(TLBIR_NUM_MASK, num); \ - __ta |= FIELD_PREP(TLBIR_SCALE_MASK, scale); \ - __ta |= FIELD_PREP(TLBIR_TG_MASK, get_trans_granule()); \ - __ta |= FIELD_PREP(TLBIR_ASID_MASK, asid); \ - __ta; \ - }) -/* These macros are used by the TLBI RANGE feature. */ #define __TLBI_RANGE_PAGES (num, scale) \ ((unsigned long)((num) + 1) << (5 * (scale) + 1))
@@ -498,8 +485,19 @@ static __always_inline void ripas2e1is(u64 arg)__tlbi (ripas2e1is, arg); } -static __always_inline void __tlbi_range(tlbi_op op, u64 arg) +static __always_inline void __tlbi_range(tlbi_op op, u64 addr, +u16 asid,int scale,int num, +u32 level,bool lpa2) { +u64 arg =0 ; + + arg |=FIELD_PREP (TLBIR_BADDR_MASK , addr >> (lpa2 ?16 :PAGE_SHIFT )); + arg |=FIELD_PREP (TLBIR_TTL_MASK , level >3 ?0 : level); + arg |=FIELD_PREP (TLBIR_NUM_MASK , num); + arg |=FIELD_PREP (TLBIR_SCALE_MASK , scale); + arg |=FIELD_PREP (TLBIR_TG_MASK ,get_trans_granule ()); + arg |=FIELD_PREP (TLBIR_ASID_MASK , asid); +op (arg); }
@@ -510,8 +508,6 @@ do { \typeof (pages) __flush_pages = pages; \int num =0 ; \int scale =3 ; \ -int shift = lpa2 ?16 :PAGE_SHIFT ; \ -unsigned long addr; \ \while (__flush_pages >0 ) { \ if (!system_supports_tlb_range() || \
@@ -525,9 +521,7 @@ do { \ \ num =__TLBI_RANGE_NUM (__flush_pages, scale); \if (num >=0 ) { \ - addr =__TLBI_VADDR_RANGE (__flush_start >> shift, asid, \ - scale, num, tlb_level); \ - __tlbi_range(r##op, addr); \ + __tlbi_range(r##op, __flush_start, asid, scale, num, tlb_level, lpa2); \ __flush_start += __TLBI_RANGE_PAGES(num, scale) << PAGE_SHIFT; \ __flush_pages -= __TLBI_RANGE_PAGES(num, scale);\ } \
Signature · 1 line
2.43.0
PATCH v3 6/13 arm64: mm: Re-implement the __flush_tlb_range_op macro in C
In reply to Ryan RobertsThe __flush_tlb_range_op() macro is horrible and has been a previous source of bugs thanks to multiple expansions of its arguments (see commit f7edb07ad7c6 ("Fix mmu notifiers for range-based invalidates")).
Rewrite the thing in C.
--- arch/arm64/include/asm/tlbflush.h | 84 +++++++++++++++++-------------- 1 file changed, 46 insertions(+), 38 deletions(-)
arch/arm64/include/asm/tlbflush.h +46 -38
@@ -429,12 +429,13 @@ static inline void arch_tlbbatch_flush(struct arch_tlbflush_unmap_batch *batch)/* * __flush_tlb_range_op - Perform TLBI operation upon a range * -* @op: TLBI instruction that operates on a range (has 'r' prefix) +* @lop: TLBI level operation to perform +* @rop: TLBI range operation to perform * @start: The start address of the range * @pages: Range as the number of pages from 'start' * @stride: Flush granularity * @asid: The ASID of the task (0 for IPA instructions) -* @tlb_level: Translation Table level hint, if known +* @level: Translation Table level hint, if known * @lpa2: If 'true', the lpa2 scheme is used as set out below * * When the CPU does not support TLB range operations, flush the TLB
@@ -501,36 +502,44 @@ static __always_inline void __tlbi_range(tlbi_op op, u64 addr,op (arg); } -#define __flush_tlb_range_op (op, start, pages, stride, \ - asid, tlb_level, lpa2) \ -do { \ - typeof(start) __flush_start = start; \ - typeof(pages) __flush_pages = pages; \ - int num = 0; \ - int scale = 3; \ - \ - while (__flush_pages > 0) { \ - if (!system_supports_tlb_range() || \ - __flush_pages == 1 || \ - (lpa2 && __flush_start != ALIGN(__flush_start, SZ_64K))) { \ - __tlbi_level_asid(op, __flush_start, tlb_level, asid); \ - __flush_start += stride; \ - __flush_pages -= stride >> PAGE_SHIFT; \ - continue; \ - } \ - \ - num = __TLBI_RANGE_NUM(__flush_pages, scale); \ - if (num >= 0) { \ - __tlbi_range(r##op, __flush_start, asid, scale, num, tlb_level, lpa2); \ - __flush_start += __TLBI_RANGE_PAGES(num, scale) << PAGE_SHIFT; \ - __flush_pages -= __TLBI_RANGE_PAGES(num, scale);\ - } \ - scale--; \ - } \ -} while (0) +static __always_inline void __flush_tlb_range_op(tlbi_op lop, tlbi_op rop, +u64 start,size_t pages, +u64 stride,u16 asid, +u32 level,bool lpa2) +{ +u64 addr = start, end = start + pages *PAGE_SIZE ; +int scale =3 ; + +while (addr != end) { +int num; + + pages = (end - addr) >>PAGE_SHIFT ; + +if (!system_supports_tlb_range () || pages ==1 ) + goto invalidate_one; + +if (lpa2 && !IS_ALIGNED (addr,SZ_64K )) + goto invalidate_one; + + num =__TLBI_RANGE_NUM (pages, scale); +if (num >=0 ) { +__tlbi_range (rop, addr, asid, scale, num, level, lpa2); + addr +=__TLBI_RANGE_PAGES (num, scale) <<PAGE_SHIFT ; + } + + scale--; +continue ; +invalidate_one: +__tlbi_level_asid (lop, addr, level, asid); + addr += stride; + } +} + +#define __flush_s1_tlb_range_op (op, start, pages, stride, asid, tlb_level) \ + __flush_tlb_range_op(op, r##op, start, pages, stride, asid, tlb_level, lpa2_is_enabled())#define __flush_s2_tlb_range_op (op, start, pages, stride, tlb_level) \ - __flush_tlb_range_op(op, start, pages, stride, 0, tlb_level, kvm_lpa2_is_enabled()); + __flush_tlb_range_op(op, r##op, start, pages, stride, 0, tlb_level, kvm_lpa2_is_enabled())static inline bool __flush_tlb_range_limit_excess (unsigned long start,unsigned long end,unsigned long pages,unsigned long stride)
@@ -569,11 +578,11 @@ static inline void __flush_tlb_range_nosync(struct mm_struct *mm, asid =ASID (mm);if (last_level) -__flush_tlb_range_op (vale1is, start, pages, stride, asid, - tlb_level,lpa2_is_enabled ()); +__flush_s1_tlb_range_op (vale1is, start, pages, stride, + asid, tlb_level);else -__flush_tlb_range_op (vae1is, start, pages, stride, asid, - tlb_level,lpa2_is_enabled ()); +__flush_s1_tlb_range_op (vae1is, start, pages, stride, + asid, tlb_level);mmu_notifier_arch_invalidate_secondary_tlbs (mm, start, end); }
@@ -597,8 +606,7 @@ static inline void local_flush_tlb_contpte(struct vm_area_struct *vma,dsb (nshst); asid =ASID (vma->vm_mm); -__flush_tlb_range_op (vale1, addr,CONT_PTES ,PAGE_SIZE , asid, -3 ,lpa2_is_enabled ()); +__flush_s1_tlb_range_op (vale1, addr,CONT_PTES ,PAGE_SIZE , asid,3 );mmu_notifier_arch_invalidate_secondary_tlbs (vma->vm_mm, addr, addr +CONT_PTE_SIZE );dsb (nsh);
@@ -631,8 +639,8 @@ static inline void flush_tlb_kernel_range(unsigned long start, unsigned long end }dsb (ishst); -__flush_tlb_range_op (vaale1is, start, pages, stride,0 , -TLBI_TTL_UNKNOWN ,lpa2_is_enabled ()); +__flush_s1_tlb_range_op (vaale1is, start, pages, stride,0 , +TLBI_TTL_UNKNOWN );__tlbi_sync_s1ish ();isb (); }
Signature · 1 line
2.43.0
PATCH v3 7/13 arm64: mm: Simplify __TLBI_RANGE_NUM() macro
In reply to Ryan RobertsFrom: Will Deacon <[email protected]>
Since commit e2768b798a19 ("arm64/mm: Modify range-based tlbi to decrement scale"), we don't need to clamp the 'pages' argument to fit the range for the specified 'scale' as we know that the upper bits will have been processed in a prior iteration.
Drop the clamping and simplify the __TLBI_RANGE_NUM() macro.
--- arch/arm64/include/asm/tlbflush.h | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-)
arch/arm64/include/asm/tlbflush.h +1 -5
@@ -199,11 +199,7 @@ static inline void __tlbi_level(tlbi_op op, u64 addr, u32 level)* range. */ #define __TLBI_RANGE_NUM (pages, scale) \ - ({ \ - int __pages = min((pages), \ - __TLBI_RANGE_PAGES(31, (scale))); \ - (__pages >> (5 * (scale) + 1)) - 1; \ - }) + (((pages) >> (5 * (scale) + 1)) - 1) #define __repeat_tlbi_sync(op, arg...) \ do { \
Signature · 1 line
2.43.0
PATCH v3 8/13 arm64: mm: Simplify __flush_tlb_range_limit_excess()
In reply to Ryan RobertsFrom: Will Deacon <[email protected]>
__flush_tlb_range_limit_excess() is unnecessarily complicated:
- It takes a 'start', 'end' and 'pages' argument, whereas it only needs 'pages' (which the caller has computed from the other two arguments!).
- It erroneously compares 'pages' with MAX_TLBI_RANGE_PAGES when the system doesn't support range-based invalidation but the range to be invalidated would result in fewer than MAX_DVM_OPS invalidations.
Simplify the function so that it no longer takes the 'start' and 'end' arguments and only considers the MAX_TLBI_RANGE_PAGES threshold on systems that implement range-based invalidation.
--- arch/arm64/include/asm/tlbflush.h | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-)
arch/arm64/include/asm/tlbflush.h +11 -13
@@ -537,21 +537,19 @@ static __always_inline void __flush_tlb_range_op(tlbi_op lop, tlbi_op rop,#define __flush_s2_tlb_range_op (op, start, pages, stride, tlb_level) \ __flush_tlb_range_op(op, r##op, start, pages, stride, 0, tlb_level, kvm_lpa2_is_enabled()) -static inline bool __flush_tlb_range_limit_excess (unsigned long start, -unsigned long end,unsigned long pages,unsigned long stride) +static inline bool __flush_tlb_range_limit_excess (unsigned long pages, +unsigned long stride) {/* -* When the system does not support TLB range based flush -* operation, (MAX_DVM_OPS - 1) pages can be handled. But -* with TLB range based operation, MAX_TLBI_RANGE_PAGES -* pages can be handled. +* Assume that the worst case number of DVM ops required to flush a +* given range on a system that supports tlb-range is 20 (4 scales, 1 +* final page, 15 for alignment on LPA2 systems), which is much smaller +* than MAX_DVM_OPS. */ -if ((!system_supports_tlb_range () && - (end - start) >= (MAX_DVM_OPS * stride)) || - pages >MAX_TLBI_RANGE_PAGES ) -return true; +if (system_supports_tlb_range ()) +return pages >MAX_TLBI_RANGE_PAGES ; -return false; +return pages >= (MAX_DVM_OPS * stride) >>PAGE_SHIFT ; } static inline void __flush_tlb_range_nosync(struct mm_struct *mm,
@@ -565,7 +563,7 @@ static inline void __flush_tlb_range_nosync(struct mm_struct *mm, end =round_up (end, stride); pages = (end - start) >>PAGE_SHIFT ; -if (__flush_tlb_range_limit_excess (start, end, pages, stride)) { +if (__flush_tlb_range_limit_excess (pages, stride)) {flush_tlb_mm (mm);return ; }
@@ -629,7 +627,7 @@ static inline void flush_tlb_kernel_range(unsigned long start, unsigned long end end =round_up (end, stride); pages = (end - start) >>PAGE_SHIFT ; -if (__flush_tlb_range_limit_excess (start, end, pages, stride)) { +if (__flush_tlb_range_limit_excess (pages, stride)) {flush_tlb_all ();return ; }
Signature · 1 line
2.43.0
PATCH v3 9/13 arm64: mm: Refactor flush_tlb_page() to use __tlbi_level_asid()
In reply to Ryan RobertsNow that we have __tlbi_level_asid(), let's refactor the *flush_tlb_page*() variants to use it rather than open coding.
The emitted tlbi(s) is/are intended to be exactly the same as before; no TTL hint is provided. Although the spec for flush_tlb_page() allows for setting the TTL hint to 3, it turns out that flush_tlb_fix_spurious_fault_pmd() depends on local_flush_tlb_page_nonotify() to invalidate the level 2 entry. This will be fixed separately.
--- arch/arm64/include/asm/tlbflush.h | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-)
arch/arm64/include/asm/tlbflush.h +2 -10
@@ -346,12 +346,8 @@ static inline void flush_tlb_mm(struct mm_struct *mm)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); +__tlbi_level_asid (vale1, uaddr,TLBI_TTL_UNKNOWN ,ASID (mm)); } static inline void local_flush_tlb_page_nonotify(struct vm_area_struct *vma,
@@ -373,12 +369,8 @@ static inline void local_flush_tlb_page(struct vm_area_struct *vma,static inline void __flush_tlb_page_nosync (struct mm_struct *mm,unsigned long uaddr) { -unsigned long addr; -dsb (ishst); - addr =__TLBI_VADDR (uaddr,ASID (mm)); -__tlbi (vale1is, addr); -__tlbi_user (vale1is, addr); +__tlbi_level_asid (vale1is, uaddr,TLBI_TTL_UNKNOWN ,ASID (mm));mmu_notifier_arch_invalidate_secondary_tlbs (mm, uaddr &PAGE_MASK , (uaddr &PAGE_MASK ) +PAGE_SIZE ); }
Signature · 1 line
2.43.0
PATCH v3 10/13 arm64: mm: Refactor __flush_tlb_range() to take flags
In reply to Ryan RobertsWe have function variants with "_nosync", "_local", "_nonotify" as well as the "last_level" parameter. Let's generalize and simplify by using a flags parameter to encode all these variants.
As a first step, convert the "last_level" boolean parameter to a flags parameter and create the first flag, TLBF_NOWALKCACHE. When present, walk cache entries are not evicted, which is the same as the old last_level=true.
--- arch/arm64/include/asm/hugetlb.h | 12 ++++++------ arch/arm64/include/asm/pgtable.h | 4 ++-- arch/arm64/include/asm/tlb.h | 6 +++--- arch/arm64/include/asm/tlbflush.h | 32 +++++++++++++++++++------------ arch/arm64/mm/contpte.c | 5 +++-- arch/arm64/mm/hugetlbpage.c | 4 ++-- arch/arm64/mm/mmu.c | 2 +- 7 files changed, 37 insertions(+), 28 deletions(-)
arch/arm64/include/asm/hugetlb.h +6 -6
@@ -71,23 +71,23 @@ static inline void __flush_hugetlb_tlb_range(struct vm_area_struct *vma,unsigned long start, unsigned long end, unsigned long stride, - bool last_level) + tlbf_t flags) { switch (stride) { #ifndef __PAGETABLE_PMD_FOLDED case PUD_SIZE: - __flush_tlb_range(vma, start, end, PUD_SIZE, last_level, 1); + __flush_tlb_range(vma, start, end, PUD_SIZE, 1, flags);break ;#endif case CONT_PMD_SIZE :case PMD_SIZE : -__flush_tlb_range (vma, start, end,PMD_SIZE , last_level,2 ); +__flush_tlb_range (vma, start, end,PMD_SIZE ,2 , flags);break ;case CONT_PTE_SIZE : -__flush_tlb_range (vma, start, end,PAGE_SIZE , last_level,3 ); +__flush_tlb_range (vma, start, end,PAGE_SIZE ,3 , flags);break ;default : -__flush_tlb_range (vma, start, end,PAGE_SIZE , last_level,TLBI_TTL_UNKNOWN ); +__flush_tlb_range (vma, start, end,PAGE_SIZE ,TLBI_TTL_UNKNOWN , flags); } }
@@ -98,7 +98,7 @@ static inline void flush_hugetlb_tlb_range(struct vm_area_struct *vma, {unsigned long stride =huge_page_size (hstate_vma (vma)); -__flush_hugetlb_tlb_range (vma, start, end, stride, false); +__flush_hugetlb_tlb_range (vma, start, end, stride,TLBF_NONE ); }#endif /* __ASM_HUGETLB_H */
arch/arm64/include/asm/pgtable.h +2 -2
@@ -89,9 +89,9 @@ static inline void arch_leave_lazy_mmu_mode(void)/* Set stride and tlb_level in flush_*_tlb_range */ #define flush_pmd_tlb_range (vma, addr, end) \ - __flush_tlb_range(vma, addr, end, PMD_SIZE, false, 2) + __flush_tlb_range(vma, addr, end, PMD_SIZE, 2, TLBF_NONE)#define flush_pud_tlb_range (vma, addr, end) \ - __flush_tlb_range(vma, addr, end, PUD_SIZE, false, 1) + __flush_tlb_range(vma, addr, end, PUD_SIZE, 1, TLBF_NONE)#endif /* CONFIG_TRANSPARENT_HUGEPAGE */ /*
arch/arm64/include/asm/tlb.h +3 -3
@@ -53,7 +53,7 @@ static inline int tlb_get_level(struct mmu_gather *tlb)static inline void tlb_flush (struct mmu_gather *tlb) {struct vm_area_struct vma =TLB_FLUSH_VMA (tlb->mm,0 ); -bool last_level = !tlb->freed_tables; +tlbf_t flags = tlb->freed_tables ?TLBF_NONE :TLBF_NOWALKCACHE ;unsigned long stride =tlb_get_unmap_size (tlb);int tlb_level =tlb_get_level (tlb);
@@ -63,13 +63,13 @@ static inline void tlb_flush(struct mmu_gather *tlb)* reallocate our ASID without invalidating the entire TLB. */ if (tlb->fullmm) { -if (!last_level) +if (tlb->freed_tables)flush_tlb_mm (tlb->mm);return ; }__flush_tlb_range (&vma, tlb->start, tlb->end, stride, - last_level, tlb_level); + tlb_level, flags); } static inline void __pte_free_tlb(struct mmu_gather *tlb, pgtable_t pte,
arch/arm64/include/asm/tlbflush.h +20 -12
@@ -286,16 +286,16 @@ static inline void __tlbi_sync_s1ish_hyp(void)* CPUs, ensuring that any walk-cache entries associated with the * translation are also invalidated. * -* __flush_tlb_range(vma, start, end, stride, last_level, tlb_level) +* __flush_tlb_range(vma, start, end, stride, tlb_level, flags) * Invalidate the virtual-address range '[start, end)' on all * CPUs for the user address space corresponding to 'vma->mm'. * The invalidation operations are issued at a granularity -* determined by 'stride' and only affect any walk-cache entries -* if 'last_level' is equal to false. tlb_level is the level at +* determined by 'stride'. tlb_level is the level at * which the invalidation must take place. If the level is wrong, * no invalidation may take place. In the case where the level * cannot be easily determined, the value TLBI_TTL_UNKNOWN will -* perform a non-hinted invalidation. +* perform a non-hinted invalidation. flags may be TLBF_NONE (0) or +* TLBF_NOWALKCACHE (elide eviction of walk cache entries). * * local_flush_tlb_page(vma, addr) * Local variant of flush_tlb_page(). Stale TLB entries may
@@ -544,10 +544,18 @@ static inline bool __flush_tlb_range_limit_excess(unsigned long pages,return pages >= (MAX_DVM_OPS * stride) >>PAGE_SHIFT ; } +typedef unsigned __bitwise tlbf_t ; + +/* No special behaviour. */ +#define TLBF_NONE ((__force tlbf_t)0) + +/* Invalidate tlb entries only, leaving the page table walk cache intact. */ +#define TLBF_NOWALKCACHE ((__force tlbf_t)BIT(0)) +static inline void __flush_tlb_range_nosync (struct mm_struct *mm,unsigned long start,unsigned long end, -unsigned long stride,bool last_level, -int tlb_level) +unsigned long stride,int tlb_level, +tlbf_t flags) {unsigned long asid, pages;
@@ -563,7 +571,7 @@ static inline void __flush_tlb_range_nosync(struct mm_struct *mm,dsb (ishst); asid =ASID (mm); -if (last_level) +if (flags &TLBF_NOWALKCACHE )__flush_s1_tlb_range_op (vale1is, start, pages, stride, asid, tlb_level);else
@@ -575,11 +583,11 @@ static inline void __flush_tlb_range_nosync(struct mm_struct *mm,static inline void __flush_tlb_range (struct vm_area_struct *vma,unsigned long start,unsigned long end, -unsigned long stride,bool last_level, -int tlb_level) +unsigned long stride,int tlb_level, +tlbf_t flags) {__flush_tlb_range_nosync (vma->vm_mm, start, end, stride, - last_level, tlb_level); + tlb_level, flags);__tlbi_sync_s1ish (); }
@@ -607,7 +615,7 @@ static inline void flush_tlb_range(struct vm_area_struct *vma,* Set the tlb_level to TLBI_TTL_UNKNOWN because we can not get enough * information here. */ -__flush_tlb_range (vma, start, end,PAGE_SIZE , false,TLBI_TTL_UNKNOWN ); +__flush_tlb_range (vma, start, end,PAGE_SIZE ,TLBI_TTL_UNKNOWN ,TLBF_NONE ); }static inline void flush_tlb_kernel_range (unsigned long start,unsigned long end)
@@ -648,7 +656,7 @@ static inline void __flush_tlb_kernel_pgtable(unsigned long kaddr)static inline void arch_tlbbatch_add_pending (struct arch_tlbflush_unmap_batch *batch,struct mm_struct *mm,unsigned long start,unsigned long end) { -__flush_tlb_range_nosync (mm, start, end,PAGE_SIZE , true,3 ); +__flush_tlb_range_nosync (mm, start, end,PAGE_SIZE ,3 ,TLBF_NOWALKCACHE ); }static inline bool __pte_flags_need_flush (ptdesc_t oldval,ptdesc_t newval)
arch/arm64/mm/contpte.c +3 -2
@@ -225,7 +225,8 @@ static void contpte_convert(struct mm_struct *mm, unsigned long addr,*/ if (!system_supports_bbml2_noabort ()) -__flush_tlb_range (&vma, start_addr, addr,PAGE_SIZE , true,3 ); +__flush_tlb_range (&vma, start_addr, addr,PAGE_SIZE ,3 , +TLBF_NOWALKCACHE );__set_ptes (mm, start_addr, start_ptep, pte,CONT_PTES ); }
@@ -552,7 +553,7 @@ int contpte_clear_flush_young_ptes(struct vm_area_struct *vma,* eliding the trailing DSB applies here. */ __flush_tlb_range_nosync (vma->vm_mm, addr, end, -PAGE_SIZE , true,3 ); +PAGE_SIZE ,3 ,TLBF_NOWALKCACHE ); }return young;
arch/arm64/mm/hugetlbpage.c +2 -2
@@ -181,7 +181,7 @@ static pte_t get_clear_contig_flush(struct mm_struct *mm,struct vm_area_struct vma =TLB_FLUSH_VMA (mm,0 );unsigned long end = addr + (pgsize * ncontig); -__flush_hugetlb_tlb_range (&vma, addr, end, pgsize, true); +__flush_hugetlb_tlb_range (&vma, addr, end, pgsize,TLBF_NOWALKCACHE );return orig_pte; }
@@ -209,7 +209,7 @@ static void clear_flush(struct mm_struct *mm,if (mm == &init_mm)flush_tlb_kernel_range (saddr, addr);else -__flush_hugetlb_tlb_range (&vma, saddr, addr, pgsize, true); +__flush_hugetlb_tlb_range (&vma, saddr, addr, pgsize,TLBF_NOWALKCACHE ); } void set_huge_pte_at(struct mm_struct *mm, unsigned long addr,
arch/arm64/mm/mmu.c +1 -1
@@ -2149,7 +2149,7 @@ pte_t modify_prot_start_ptes(struct vm_area_struct *vma, unsigned long addr,*/ if (pte_accessible (vma->vm_mm, pte) &&pte_user_exec (pte))__flush_tlb_range (vma, addr, nr *PAGE_SIZE , -PAGE_SIZE , true,3 ); +PAGE_SIZE ,3 ,TLBF_NOWALKCACHE ); }return pte;
Signature · 1 line
2.43.0
PATCH v3 11/13 arm64: mm: More flags for __flush_tlb_range()
In reply to Ryan RobertsRefactor function variants with "_nosync", "_local" and "_nonotify" into a single __always_inline implementation that takes flags and rely on constant folding to select the parts that are actually needed at any given callsite, based on the provided flags.
Flags all live in the tlbf_t (TLB flags) type; TLBF_NONE (0) continues to provide the strongest semantics (i.e. evict from walk cache, broadcast, synchronise and notify). Each flag reduces the strength in some way; TLBF_NONOTIFY, TLBF_NOSYNC and TLBF_NOBROADCAST are added to complement the existing TLBF_NOWALKCACHE.
There are no users that require TLBF_NOBROADCAST without TLBF_NOWALKCACHE so implement that as BUILD_BUG() to avoid needing to introduce dead code for vae1 invalidations.
The result is a clearer, simpler, more powerful API.
--- arch/arm64/include/asm/tlbflush.h | 95 ++++++++++++++++++------------- arch/arm64/mm/contpte.c | 9 ++- 2 files changed, 62 insertions(+), 42 deletions(-)
arch/arm64/include/asm/tlbflush.h +56 -39
@@ -295,7 +295,10 @@ static inline void __tlbi_sync_s1ish_hyp(void)* no invalidation may take place. In the case where the level * cannot be easily determined, the value TLBI_TTL_UNKNOWN will * perform a non-hinted invalidation. flags may be TLBF_NONE (0) or -* TLBF_NOWALKCACHE (elide eviction of walk cache entries). +* any combination of TLBF_NOWALKCACHE (elide eviction of walk +* cache entries), TLBF_NONOTIFY (don't call mmu notifiers), +* TLBF_NOSYNC (don't issue trailing dsb) and TLBF_NOBROADCAST +* (only perform the invalidation for the local cpu). * * local_flush_tlb_page(vma, addr) * Local variant of flush_tlb_page(). Stale TLB entries may
@@ -305,12 +308,6 @@ static inline void __tlbi_sync_s1ish_hyp(void)* Same as local_flush_tlb_page() except MMU notifier will not be * called. * -* local_flush_tlb_contpte(vma, addr) -* Invalidate the virtual-address range -* '[addr, addr+CONT_PTE_SIZE)' mapped with contpte on local CPU -* for the user address space corresponding to 'vma->mm'. Stale -* TLB entries may remain in remote CPUs. -* * Finally, take a look at asm/tlb.h to see how tlb_flush() is implemented * on top of these routines, since that is our interface to the mmu_gather * API as used by munmap() and friends.
@@ -552,15 +549,23 @@ typedef unsigned __bitwise tlbf_t;/* Invalidate tlb entries only, leaving the page table walk cache intact. */ #define TLBF_NOWALKCACHE ((__force tlbf_t)BIT(0)) -static inline void __flush_tlb_range_nosync (struct mm_struct *mm, -unsigned long start,unsigned long end, -unsigned long stride,int tlb_level, -tlbf_t flags) +/* Skip the trailing dsb after issuing tlbi. */ +#define TLBF_NOSYNC ((__force tlbf_t)BIT(1)) + +/* Suppress tlb notifier callbacks for this flush operation. */ +#define TLBF_NONOTIFY ((__force tlbf_t)BIT(2)) + +/* Perform the tlbi locally without broadcasting to other CPUs. */ +#define TLBF_NOBROADCAST ((__force tlbf_t)BIT(3)) + +static __always_inline void __do_flush_tlb_range(struct vm_area_struct *vma, +unsigned long start,unsigned long end, +unsigned long stride,int tlb_level, +tlbf_t flags) { +struct mm_struct *mm = vma->vm_mm;unsigned long asid, pages; - start =round_down (start, stride); - end =round_up (end, stride); pages = (end - start) >>PAGE_SHIFT ;if (__flush_tlb_range_limit_excess (pages, stride)) {
@@ -568,17 +573,41 @@ static inline void __flush_tlb_range_nosync(struct mm_struct *mm,return ; } -dsb (ishst); +if (!(flags &TLBF_NOBROADCAST )) +dsb (ishst); +else +dsb (nshst); + asid =ASID (mm); -if (flags &TLBF_NOWALKCACHE ) -__flush_s1_tlb_range_op (vale1is, start, pages, stride, - asid, tlb_level); -else +switch (flags & (TLBF_NOWALKCACHE |TLBF_NOBROADCAST )) { +case TLBF_NONE :__flush_s1_tlb_range_op (vae1is, start, pages, stride, - asid, tlb_level); + asid, tlb_level); +break ; +case TLBF_NOWALKCACHE : +__flush_s1_tlb_range_op (vale1is, start, pages, stride, + asid, tlb_level); +break ; +case TLBF_NOBROADCAST : +/* Combination unused */ +BUG (); +break ; +case TLBF_NOWALKCACHE |TLBF_NOBROADCAST : +__flush_s1_tlb_range_op (vale1, start, pages, stride, + asid, tlb_level); +break ; + } + +if (!(flags &TLBF_NONOTIFY )) +mmu_notifier_arch_invalidate_secondary_tlbs (mm, start, end); -mmu_notifier_arch_invalidate_secondary_tlbs (mm, start, end); +if (!(flags &TLBF_NOSYNC )) { +if (!(flags &TLBF_NOBROADCAST )) +__tlbi_sync_s1ish (); +else +dsb (nsh); + } } static inline void __flush_tlb_range(struct vm_area_struct *vma,
@@ -586,24 +615,9 @@ static inline void __flush_tlb_range(struct vm_area_struct *vma, unsigned long stride, int tlb_level, tlbf_t flags) { - __flush_tlb_range_nosync(vma->vm_mm, start, end, stride, -tlb_level ,flags ); -__tlbi_sync_s1ish (); -} - -static inline void local_flush_tlb_contpte (struct vm_area_struct *vma, -unsigned long addr) -{ -unsigned long asid; - - addr =round_down (addr,CONT_PTE_SIZE ); - -dsb (nshst); - asid =ASID (vma->vm_mm); -__flush_s1_tlb_range_op (vale1, addr,CONT_PTES ,PAGE_SIZE , asid,3 ); -mmu_notifier_arch_invalidate_secondary_tlbs (vma->vm_mm, addr, - addr +CONT_PTE_SIZE ); -dsb (nsh); + start =round_down (start, stride); + end =round_up (end, stride); +__do_flush_tlb_range (vma, start, end, stride, tlb_level, flags); } static inline void flush_tlb_range(struct vm_area_struct *vma,
@@ -656,7 +670,10 @@ static inline void __flush_tlb_kernel_pgtable(unsigned long kaddr)static inline void arch_tlbbatch_add_pending (struct arch_tlbflush_unmap_batch *batch,struct mm_struct *mm,unsigned long start,unsigned long end) { -__flush_tlb_range_nosync (mm, start, end,PAGE_SIZE ,3 ,TLBF_NOWALKCACHE ); +struct vm_area_struct vma = { .vm_mm = mm, .vm_flags =0 }; + +__flush_tlb_range (&vma, start, end,PAGE_SIZE ,3 , +TLBF_NOWALKCACHE |TLBF_NOSYNC ); }static inline bool __pte_flags_need_flush (ptdesc_t oldval,ptdesc_t newval)
arch/arm64/mm/contpte.c +6 -3
@@ -552,8 +552,8 @@ int contpte_clear_flush_young_ptes(struct vm_area_struct *vma,* See comment in __ptep_clear_flush_young(); same rationale for * eliding the trailing DSB applies here. */ -__flush_tlb_range_nosync (vma->vm_mm, addr, end, -PAGE_SIZE ,3 ,TLBF_NOWALKCACHE ); +__flush_tlb_range (vma, addr, end,PAGE_SIZE ,3 , +TLBF_NOWALKCACHE |TLBF_NOSYNC ); }return young;
@@ -641,7 +641,10 @@ int contpte_ptep_set_access_flags(struct vm_area_struct *vma,__ptep_set_access_flags (vma, addr, ptep, entry,0 );if (dirty) -local_flush_tlb_contpte (vma, start_addr); +__flush_tlb_range (vma, start_addr, + start_addr +CONT_PTE_SIZE , +PAGE_SIZE ,3 , +TLBF_NOWALKCACHE |TLBF_NOBROADCAST ); }else {__contpte_try_unfold (vma->vm_mm, addr, ptep, orig_pte);__ptep_set_access_flags (vma, addr, ptep, entry, dirty);
Signature · 1 line
2.43.0
Re: [PATCH v3 11/13] arm64: mm: More flags for __flush_tlb_range()
Quoted 11 lines · On Mon, 2 Mar 2026 13:55:58 +0000 Ryan Roberts <[email protected]> wrote:
Refactor function variants with "
_nosync", "_local" and "_nonotify" into a single__always_inlineimplementation that takes flags and rely on constant folding to select the parts that are actually needed at any given callsite, based on the provided flags.Flags all live in the
tlbf_t(TLB flags) type;TLBF_NONE(0) continues to provide the strongest semantics (i.e. evict from walk cache, broadcast, synchronise and notify). Each flag reduces the strength in some way;TLBF_NONOTIFY,TLBF_NOSYNCandTLBF_NOBROADCASTare added to complement the existingTLBF_NOWALKCACHE.
Quoted 5 lines
There are no users that require
TLBF_NOBROADCASTwithoutTLBF_NOWALKCACHEso implement that asBUILD_BUG()to avoid needing to introduce dead code for vae1 invalidations.The result is a clearer, simpler, more powerful API.
Hi Ryan,
There is one subtle change to rounding that should be called out at least.
Might even be worth pulling it to a precursor patch where you can add an explanation of why original code was rounding to a larger value than was ever needed.
Jonathan
Quoted 10 lines
Signed-off-by Ryan Roberts <[email protected]>static inline void __flush_tlb_range(struct
vm_area_struct*vma,arch/arm64/include/asm/tlbflush.h +0
-1@@ -586,24 +615,9 @@ static inline void __flush_tlb_range(struct vm_area_struct *vma, unsigned long stride, int tlb_level, tlbf_t flags) { - __flush_tlb_range_nosync(vma->vm_mm, start, end, stride,
arch/arm64/include/asm/tlbflush.h static inline void __flush_tlb_range(struct vm_area_struct *vma, · line 599
Quoted 10 lines
arch/arm64/include/asm/tlbflush.h +0
-10- tlb_level, flags); -__tlbi_sync_s1ish (); -} - -static inline void local_flush_tlb_contpte (struct vm_area_struct *vma, -unsigned long addr) -{ -unsigned long asid; - - addr =round_down (addr,CONT_PTE_SIZE );
See below.
arch/arm64/include/asm/tlbflush.h static inline void __flush_tlb_range(struct vm_area_struct *vma, · line 618
Quoted 8 lines
arch/arm64/include/asm/tlbflush.h +1
-7- -dsb (nshst); - asid =ASID (vma->vm_mm); -__flush_s1_tlb_range_op (vale1, addr,CONT_PTES ,PAGE_SIZE , asid,3 ); -mmu_notifier_arch_invalidate_secondary_tlbs (vma->vm_mm, addr, - addr +CONT_PTE_SIZE ); -dsb (nsh); + start =round_down (start, stride);
See below.
Quoted 10 lines
arch/arm64/include/asm/tlbflush.h +2
-0+ end =round_up (end, stride); +__do_flush_tlb_range (vma, start, end, stride, tlb_level, flags); }static inline bool
__pte_flags_need_flush(ptdesc_t oldval, ptdesc_t newval)arch/arm64/mm/contpte.c +0
-0
...
arch/arm64/mm/contpte.c int contpte_ptep_set_access_flags(struct vm_area_struct *vma, · line 646
Quoted 8 lines
arch/arm64/mm/contpte.c +3
-1@@ -641,7 +641,10 @@ int contpte_ptep_set_access_flags(struct vm_area_struct *vma,__ptep_set_access_flags (vma, addr, ptep, entry,0 );if (dirty) -local_flush_tlb_contpte (vma, start_addr); + __flush_tlb_range(vma, start_addr, + start_addr + CONT_PTE_SIZE, + PAGE_SIZE, 3,
This results in a different stride to round down.local_flush_tlb_contpte() did
addr = round_down(addr, CONT_PTE_SIZE);
With this call we have
start = round_down(start, stride); where stride is PAGE_SIZE.
I'm too lazy to figure out if that matters.
Quoted 4 lines
arch/arm64/mm/contpte.c +1
-0+ TLBF_NOWALKCACHE | TLBF_NOBROADCAST); }else {__contpte_try_unfold (vma->vm_mm, addr, ptep, orig_pte);__ptep_set_access_flags (vma, addr, ptep, entry, dirty);
Re: [PATCH v3 11/13] arm64: mm: More flags for __flush_tlb_range()
Quoted 14 lines · Jonathan Cameron
Quoted 11 lines · On Mon, 2 Mar 2026 13:55:58 +0000 Ryan Roberts <[email protected]> wrote:
Refactor function variants with "
_nosync", "_local" and "_nonotify" into a single__always_inlineimplementation that takes flags and rely on constant folding to select the parts that are actually needed at any given callsite, based on the provided flags.Flags all live in the
tlbf_t(TLB flags) type;TLBF_NONE(0) continues to provide the strongest semantics (i.e. evict from walk cache, broadcast, synchronise and notify). Each flag reduces the strength in some way;TLBF_NONOTIFY,TLBF_NOSYNCandTLBF_NOBROADCASTare added to complement the existingTLBF_NOWALKCACHE.
Quoted 8 lines
Quoted 5 lines
There are no users that require
TLBF_NOBROADCASTwithoutTLBF_NOWALKCACHEso implement that asBUILD_BUG()to avoid needing to introduce dead code for vae1 invalidations.The result is a clearer, simpler, more powerful API.
Hi Ryan,
There is one subtle change to rounding that should be called out at least.
Thanks for the review. I'm confident that there isn't actually a change to the rounding here, but the responsibility has moved to the caller. See below...
Quoted 63 lines
Might even be worth pulling it to a precursor patch where you can add an explanation of why original code was rounding to a larger value than was ever needed.
Jonathan
Quoted 10 lines
Signed-off-by Ryan Roberts <[email protected]>static inline void __flush_tlb_range(struct
vm_area_struct*vma,arch/arm64/include/asm/tlbflush.h +0
-1@@ -586,24 +615,9 @@ static inline void __flush_tlb_range(struct vm_area_struct *vma, unsigned long stride, int tlb_level, tlbf_t flags) { - __flush_tlb_range_nosync(vma->vm_mm, start, end, stride,arch/arm64/include/asm/tlbflush.h static inline void __flush_tlb_range(struct vm_area_struct *vma, · line 599
Quoted 10 lines
arch/arm64/include/asm/tlbflush.h +0
-10- tlb_level, flags); -__tlbi_sync_s1ish (); -} - -static inline void local_flush_tlb_contpte (struct vm_area_struct *vma, -unsigned long addr) -{ -unsigned long asid; - - addr =round_down (addr,CONT_PTE_SIZE );See below.
arch/arm64/include/asm/tlbflush.h static inline void __flush_tlb_range(struct vm_area_struct *vma, · line 618
Quoted 8 lines
arch/arm64/include/asm/tlbflush.h +1
-7- -dsb (nshst); - asid =ASID (vma->vm_mm); -__flush_s1_tlb_range_op (vale1, addr,CONT_PTES ,PAGE_SIZE , asid,3 ); -mmu_notifier_arch_invalidate_secondary_tlbs (vma->vm_mm, addr, - addr +CONT_PTE_SIZE ); -dsb (nsh); + start =round_down (start, stride);See below.
Quoted 10 lines
arch/arm64/include/asm/tlbflush.h +2
-0+ end =round_up (end, stride); +__do_flush_tlb_range (vma, start, end, stride, tlb_level, flags); }static inline bool
__pte_flags_need_flush(ptdesc_t oldval, ptdesc_t newval)arch/arm64/mm/contpte.c +0
-0...
arch/arm64/mm/contpte.c int contpte_ptep_set_access_flags(struct vm_area_struct *vma, · line 646
Quoted 8 lines
arch/arm64/mm/contpte.c +3
-1@@ -641,7 +641,10 @@ int contpte_ptep_set_access_flags(struct vm_area_struct *vma,__ptep_set_access_flags (vma, addr, ptep, entry,0 );if (dirty) -local_flush_tlb_contpte (vma, start_addr); + __flush_tlb_range(vma, start_addr, + start_addr + CONT_PTE_SIZE, + PAGE_SIZE, 3,This results in a different stride to round down.
local_flush_tlb_contpte()did
addr =round_down(addr, CONT_PTE_SIZE);
Quoted 4 lines
With this call we have
start =round_down(start, stride); where stride isPAGE_SIZE.I'm too lazy to figure out if that matters.
contpte_ptep_set_access_flags() is operating on a contpte block of ptes, and as such, start_addr has already been rounded down to the start of the block, which is always bigger than (and perfectly divisible by) PAGE_SIZE.
Previously, local_flush_tlb_contpte() allowed passing any VA in within the contpte block and the function would automatically round it down to the start of the block and invalidate the full block.
After the change, we are explicitly passing the already aligned block; start_addr is already guaranteed to be at the start of the block and "start_addr
+ CONT_PTE_SIZE" is the end.
So in both cases, the rounding down that is done by local_flush_tlb_contpte() / __flush_tlb_range() doesn't actually change the value.
Thanks,
Ryan
Quoted 7 lines
Quoted 6 lines
arch/arm64/mm/contpte.c +1
-0+ TLBF_NOWALKCACHE | TLBF_NOBROADCAST); }else {__contpte_try_unfold (vma->vm_mm, addr, ptep, orig_pte);__ptep_set_access_flags (vma, addr, ptep, entry, dirty);
Jonathan Cameron
Reviewed-byRe: [PATCH v3 11/13] arm64: mm: More flags for __flush_tlb_range()
Quoted 103 lines · On Tue, 3 Mar 2026 13:54:33 +0000 Ryan Roberts <[email protected]> wrote:
Quoted 14 lines · Jonathan Cameron
Quoted 11 lines · On Mon, 2 Mar 2026 13:55:58 +0000 Ryan Roberts <[email protected]> wrote:
Refactor function variants with "
_nosync", "_local" and "_nonotify" into a single__always_inlineimplementation that takes flags and rely on constant folding to select the parts that are actually needed at any given callsite, based on the provided flags.Flags all live in the
tlbf_t(TLB flags) type;TLBF_NONE(0) continues to provide the strongest semantics (i.e. evict from walk cache, broadcast, synchronise and notify). Each flag reduces the strength in some way;TLBF_NONOTIFY,TLBF_NOSYNCandTLBF_NOBROADCASTare added to complement the existingTLBF_NOWALKCACHE.Quoted 8 lines
Quoted 5 lines
There are no users that require
TLBF_NOBROADCASTwithoutTLBF_NOWALKCACHEso implement that asBUILD_BUG()to avoid needing to introduce dead code for vae1 invalidations.The result is a clearer, simpler, more powerful API.
Hi Ryan,
There is one subtle change to rounding that should be called out at least.
Thanks for the review. I'm confident that there isn't actually a change to the rounding here, but the responsibility has moved to the caller. See below...
Quoted 63 lines
Might even be worth pulling it to a precursor patch where you can add an explanation of why original code was rounding to a larger value than was ever needed.
Jonathan
Quoted 10 lines
Signed-off-by Ryan Roberts <[email protected]>static inline void __flush_tlb_range(struct
vm_area_struct*vma,arch/arm64/include/asm/tlbflush.h +0
-1@@ -586,24 +615,9 @@ static inline void __flush_tlb_range(struct vm_area_struct *vma, unsigned long stride, int tlb_level, tlbf_t flags) { - __flush_tlb_range_nosync(vma->vm_mm, start, end, stride,arch/arm64/include/asm/tlbflush.h static inline void __flush_tlb_range(struct vm_area_struct *vma, · line 599
Quoted 10 lines
arch/arm64/include/asm/tlbflush.h +0
-10- tlb_level, flags); -__tlbi_sync_s1ish (); -} - -static inline void local_flush_tlb_contpte (struct vm_area_struct *vma, -unsigned long addr) -{ -unsigned long asid; - - addr =round_down (addr,CONT_PTE_SIZE );See below.
arch/arm64/include/asm/tlbflush.h static inline void __flush_tlb_range(struct vm_area_struct *vma, · line 618
Quoted 8 lines
arch/arm64/include/asm/tlbflush.h +1
-7- -dsb (nshst); - asid =ASID (vma->vm_mm); -__flush_s1_tlb_range_op (vale1, addr,CONT_PTES ,PAGE_SIZE , asid,3 ); -mmu_notifier_arch_invalidate_secondary_tlbs (vma->vm_mm, addr, - addr +CONT_PTE_SIZE ); -dsb (nsh); + start =round_down (start, stride);See below.
Quoted 10 lines
arch/arm64/include/asm/tlbflush.h +2
-0+ end =round_up (end, stride); +__do_flush_tlb_range (vma, start, end, stride, tlb_level, flags); }static inline bool
__pte_flags_need_flush(ptdesc_t oldval, ptdesc_t newval)arch/arm64/mm/contpte.c +0
-0...
arch/arm64/mm/contpte.c int contpte_ptep_set_access_flags(struct vm_area_struct *vma, · line 646
Quoted 8 lines
arch/arm64/mm/contpte.c +3
-1@@ -641,7 +641,10 @@ int contpte_ptep_set_access_flags(struct vm_area_struct *vma,__ptep_set_access_flags (vma, addr, ptep, entry,0 );if (dirty) -local_flush_tlb_contpte (vma, start_addr); + __flush_tlb_range(vma, start_addr, + start_addr + CONT_PTE_SIZE, + PAGE_SIZE, 3,This results in a different stride to round down.
local_flush_tlb_contpte()did
addr =round_down(addr, CONT_PTE_SIZE);Quoted 4 lines
With this call we have
start =round_down(start, stride); where stride isPAGE_SIZE.I'm too lazy to figure out if that matters.
contpte_ptep_set_access_flags()is operating on a contpte block of ptes, and as such,start_addrhas already been rounded down to the start of the block, which is always bigger than (and perfectly divisible by)PAGE_SIZE.Previously,
local_flush_tlb_contpte()allowed passing any VA in within the contpte block and the function would automatically round it down to the start of the block and invalidate the full block.
Quoted 6 lines
After the change, we are explicitly passing the already aligned block;
start_addris already guaranteed to be at the start of the block and "start_addr
+CONT_PTE_SIZE" is the end.So in both cases, the rounding down that is done by
local_flush_tlb_contpte()/__flush_tlb_range()doesn't actually change the value.
Ah ok, so key is that the round down in local_flush_tlb_contpte() never did anything in practice because the only caller is contpte_ptep_set_access_flags() and that does the align down a couple of lines before the call. I should have spent a few seconds looking! :(
Maybe if you are respinning just throw in a one line comment on this in the commit description.
Quoted 14 lines
Thanks,
RyanQuoted 7 lines
Quoted 6 lines
arch/arm64/mm/contpte.c +1
-0+ TLBF_NOWALKCACHE | TLBF_NOBROADCAST); }else {__contpte_try_unfold (vma->vm_mm, addr, ptep, orig_pte);__ptep_set_access_flags (vma, addr, ptep, entry, dirty);
PATCH v3 12/13 arm64: mm: Wrap flush_tlb_page() around __do_flush_tlb_range()
In reply to Ryan RobertsFlushing a page from the tlb is just a special case of flushing a range. So let's rework flush_tlb_page() so that it simply wraps __do_flush_tlb_range(). While at it, let's also update the API to take the same flags that we use when flushing a range. This allows us to delete all the ugly "_nosync", "_local" and "_nonotify" variants.
Thanks to constant folding, all of the complex looping and tlbi-by-range options get eliminated so that the generated code for flush_tlb_page() looks very similar to the previous version.
--- arch/arm64/include/asm/pgtable.h | 6 +-- arch/arm64/include/asm/tlbflush.h | 81 ++++++++++--------------------- arch/arm64/mm/fault.c | 2 +- 3 files changed, 29 insertions(+), 60 deletions(-)
arch/arm64/include/asm/pgtable.h +3 -3
@@ -101,10 +101,10 @@ static inline void arch_leave_lazy_mmu_mode(void)* entries exist. */ #define flush_tlb_fix_spurious_fault (vma, address, ptep) \ - local_flush_tlb_page_nonotify(vma, address) + __flush_tlb_page(vma, address, TLBF_NOBROADCAST | TLBF_NONOTIFY)#define flush_tlb_fix_spurious_fault_pmd (vma, address, pmdp) \ - local_flush_tlb_page_nonotify(vma, address) + __flush_tlb_page(vma, address, TLBF_NOBROADCAST | TLBF_NONOTIFY)/* * ZERO_PAGE is a global shared page that is always zero: used
@@ -1320,7 +1320,7 @@ static inline int __ptep_clear_flush_young(struct vm_area_struct *vma,* context-switch, which provides a DSB to complete the TLB * invalidation. */ -flush_tlb_page_nosync (vma, address); +__flush_tlb_page (vma, address,TLBF_NOSYNC ); }return young;
arch/arm64/include/asm/tlbflush.h +25 -56
105 lines not on this page · raw message
arch/arm64/mm/fault.c +1 -1
@@ -239,7 +239,7 @@ int __ptep_set_access_flags(struct vm_area_struct *vma,* flush_tlb_fix_spurious_fault(). */ if (dirty) -local_flush_tlb_page (vma, address); +__flush_tlb_page (vma, address,TLBF_NOBROADCAST );return 1 ; }
Signature · 1 line
2.43.0
Re: [PATCH v3 12/13] arm64: mm: Wrap flush_tlb_page() around __do_flush_tlb_range()
Quoted 6 lines · On Mon, 2 Mar 2026 13:55:59 +0000 Ryan Roberts <[email protected]> wrote:
Flushing a page from the tlb is just a special case of flushing a range. So let's rework
flush_tlb_page()so that it simply wraps__do_flush_tlb_range(). While at it, let's also update the API to take the same flags that we use when flushing a range. This allows us to delete all the ugly "_nosync", "_local" and "_nonotify" variants.
Quoted 6 lines
Thanks to constant folding, all of the complex looping and tlbi-by-range options get eliminated so that the generated code for
flush_tlb_page()looks very similar to the previous version.Reviewed-by Linu Cherian <[email protected]>Signed-off-by Ryan Roberts <[email protected]>
PATCH v3 13/13 arm64: mm: Provide level hint for flush_tlb_page()
In reply to Ryan RobertsPreviously tlb invalidations issued by __flush_tlb_page() did not contain a level hint. But the function is clearly only ever targeting level 3 tlb entries and its documentation agrees:
| this operation only invalidates a single, last-level page-table | entry and therefore does not affect any walk-caches
However, it turns out that the function was actually being used to invalidate a level 2 mapping via flush_tlb_fix_spurious_fault_pmd(). The bug was benign because the level hint was not set so the HW would still invalidate the PMD mapping, and also because the TLBF_NONOTIFY flag was set, the bounds of the mapping were never used for anything else.
Now that we have the new and improved range-invalidation API, it is trival to fix flush_tlb_fix_spurious_fault_pmd() to explicitly flush the whole range (locally, without notification and last level only). So let's do that, and then update __flush_tlb_page() to hint level 3.
--- arch/arm64/include/asm/pgtable.h | 5 +++-- arch/arm64/include/asm/tlbflush.h | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-)
arch/arm64/include/asm/pgtable.h +3 -2
@@ -103,8 +103,9 @@ static inline void arch_leave_lazy_mmu_mode(void)#define flush_tlb_fix_spurious_fault (vma, address, ptep) \ __flush_tlb_page(vma, address, TLBF_NOBROADCAST | TLBF_NONOTIFY) -#define flush_tlb_fix_spurious_fault_pmd (vma, address, pmdp) \ - __flush_tlb_page(vma, address, TLBF_NOBROADCAST | TLBF_NONOTIFY) +#define flush_tlb_fix_spurious_fault_pmd (vma, address, pmdp) \ + __flush_tlb_range(vma, address, address + PMD_SIZE, PMD_SIZE, 2, \ + TLBF_NOBROADCAST | TLBF_NONOTIFY | TLBF_NOWALKCACHE)/* * ZERO_PAGE is a global shared page that is always zero: used
arch/arm64/include/asm/tlbflush.h +1 -1
@@ -591,7 +591,7 @@ static inline void __flush_tlb_page(struct vm_area_struct *vma,unsigned long start =round_down (uaddr,PAGE_SIZE );unsigned long end = start +PAGE_SIZE ; -__do_flush_tlb_range (vma, start, end,PAGE_SIZE ,TLBI_TTL_UNKNOWN , +__do_flush_tlb_range (vma, start, end,PAGE_SIZE ,3 ,TLBF_NOWALKCACHE | flags); }
Signature · 1 line
2.43.0
Mark Rutland
reviewerRe: [PATCH v3 13/13] arm64: mm: Provide level hint for flush_tlb_page()
Hi Ryan,
Quoted 6 lines · Ryan Roberts
Previously tlb invalidations issued by
__flush_tlb_page()did not contain a level hint. But the function is clearly only ever targeting level 3 tlb entries and its documentation agrees:| this operation only invalidates a single, last-level page-table | entry and therefore does not affect any walk-caches
FWIW, I'd have read "last-level" as synonymous with "leaf" (i.e. a Page or Block entry, which is the last level of walk) rather than level 3 specifically. The architecture uses the term to match the former (e.g. in the description of TLBI VALE1IS).
If we're tightening up __flush_tlb_page(), I think it'd be worth either updating the comment to explicitly note that this only applies to level 3 entries, OR update the comment+name to say it applies to leaf entries, and have it take a level parameter.
Quoted 6 lines
However, it turns out that the function was actually being used to invalidate a level 2 mapping via
flush_tlb_fix_spurious_fault_pmd(). The bug was benign because the level hint was not set so the HW would still invalidate the PMD mapping, and also because theTLBF_NONOTIFYflag was set, the bounds of the mapping were never used for anything else.
I suspect (as above) that the current usage was intentional, legitimate usage, just poorly documented.
Quoted 4 lines
Now that we have the new and improved range-invalidation API, it is trival to fix
flush_tlb_fix_spurious_fault_pmd()to explicitly flush the whole range (locally, without notification and last level only). So let's do that, and then update__flush_tlb_page()to hint level 3.
Do we never use __flush_tlb_page() to manipulate a level 1 block mapping? I'd have expected we did the same lazy invalidation for permission relazation there, but if that's not the case, then this seems fine in principle.
Quoted 12 lines
Reviewed-by Linu Cherian <[email protected]>Signed-off-by Ryan Roberts <[email protected]>--- arch/arm64/include/asm/pgtable.h | 5 +++-- arch/arm64/include/asm/tlbflush.h | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-)arch/arm64/include/asm/pgtable.h +0
-0
arch/arm64/include/asm/pgtable.h static inline void arch_leave_lazy_mmu_mode(void) · line 108
Quoted 8 lines
arch/arm64/include/asm/pgtable.h +3
-2@@ -103,8 +103,9 @@ static inline void arch_leave_lazy_mmu_mode(void)#define flush_tlb_fix_spurious_fault (vma, address, ptep) \ __flush_tlb_page(vma, address, TLBF_NOBROADCAST | TLBF_NONOTIFY) -#define flush_tlb_fix_spurious_fault_pmd (vma, address, pmdp) \ - __flush_tlb_page(vma, address, TLBF_NOBROADCAST | TLBF_NONOTIFY) +#define flush_tlb_fix_spurious_fault_pmd (vma, address, pmdp) \ + __flush_tlb_range(vma, address, address + PMD_SIZE, PMD_SIZE, 2, \ + TLBF_NOBROADCAST | TLBF_NONOTIFY | TLBF_NOWALKCACHE)
Is there a reason to keep __flush_tlb_page(), rather than defining flush_tlb_fix_spurious_fault() in terms of __flush_tlb_range() with all the level 3 constants?
Mark.
Quoted 19 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 +1
-1@@ -591,7 +591,7 @@ static inline void __flush_tlb_page(struct vm_area_struct *vma,unsigned long start =round_down (uaddr,PAGE_SIZE );unsigned long end = start +PAGE_SIZE ; -__do_flush_tlb_range (vma, start, end,PAGE_SIZE ,TLBI_TTL_UNKNOWN , +__do_flush_tlb_range (vma, start, end,PAGE_SIZE ,3 ,TLBF_NOWALKCACHE | flags); }Signature · 1 line
2.43.0
Re: [PATCH v3 13/13] arm64: mm: Provide level hint for flush_tlb_page()
Quoted 7 lines · Mark Rutland
Hi Ryan,
Quoted 4 lines · Ryan Roberts
Previously tlb invalidations issued by
__flush_tlb_page()did not contain a level hint. But the function is clearly only ever targeting level 3 tlb entries and its documentation agrees:
Quoted 7 lines
Quoted 2 lines
| this operation only invalidates a single, last-level page-table | entry and therefore does not affect any walk-caches
FWIW, I'd have read "last-level" as synonymous with "leaf" (i.e. a Page or Block entry, which is the last level of walk) rather than level 3 specifically. The architecture uses the term to match the former (e.g. in the description of TLBI VALE1IS).
Hmm yeah, now that I'm re-reading, I agree that quoted documentation doesn't say anything about it being level 3 specific.
But actually that was arm64-specific documentation for flush_tlb_page(), which is a core-mm function. The generic docs at Documentation/core-api/cachetlb.rst make it clear that it's intended only for PTE invalidations, I think:
| 4) ``void flush_tlb_page(struct vm_area_struct *vma, unsigned long addr)`` | | This time we need to remove the PAGE_SIZE sized translation | from the TLB. ...
Quoted 5 lines
If we're tightening up
__flush_tlb_page(), I think it'd be worth either updating the comment to explicitly note that this only applies to level 3 entries, OR update the comment+name to say it applies to leaf entries, and have it take a level parameter.
I'll fix the arm64-specific docs to align with the generic docs and replace "last-level" with "level 3" if that works for you.
Quoted 10 lines
Quoted 7 lines
However, it turns out that the function was actually being used to invalidate a level 2 mapping via
flush_tlb_fix_spurious_fault_pmd(). The bug was benign because the level hint was not set so the HW would still invalidate the PMD mapping, and also because theTLBF_NONOTIFYflag was set, the bounds of the mapping were never used for anything else.I suspect (as above) that the current usage was intentional, legitimate usage, just poorly documented.
Before this series flush_tlb_fix_spurious_fault_pmd() was implemented using local_flush_tlb_page_nonotify() which never even gives an option to set the TTL hint, so I agree.
But I don't think flush_tlb_fix_spurious_fault_pmd() should be calling any tlb flush api that has "page" in the name since that implies PTE, not PMD.
I think what I have done is an improvement; but I'm happy to soften/correct this description in the next version.
Quoted 10 lines
Quoted 5 lines
Now that we have the new and improved range-invalidation API, it is trival to fix
flush_tlb_fix_spurious_fault_pmd()to explicitly flush the whole range (locally, without notification and last level only). So let's do that, and then update__flush_tlb_page()to hint level 3.Do we never use
__flush_tlb_page()to manipulate a level 1 block mapping? I'd have expected we did the same lazy invalidation for permission relazation there, but if that's not the case, then this seems fine in principle.
No, there is no flush_tlb_fix_spurious_fault_pud(). (flush_tlb_fix_spurious_fault_pmd() was only added last cycle).
Quoted 16 lines
Quoted 16 lines
Reviewed-by Linu Cherian <[email protected]>Signed-off-by Ryan Roberts <[email protected]>--- arch/arm64/include/asm/pgtable.h | 5 +++-- arch/arm64/include/asm/tlbflush.h | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-)arch/arm64/include/asm/pgtable.h +0
-0@@ -103,8 +103,9 @@ static inline void arch_leave_lazy_mmu_mode(void)#define flush_tlb_fix_spurious_fault (vma, address, ptep) \ __flush_tlb_page(vma, address, TLBF_NOBROADCAST | TLBF_NONOTIFY)
Quoted 9 lines
arch/arm64/include/asm/pgtable.h static inline void arch_leave_lazy_mmu_mode(void) · line 108
Quoted 5 lines
arch/arm64/include/asm/pgtable.h +3
-2-#define flush_tlb_fix_spurious_fault_pmd (vma, address, pmdp) \ - __flush_tlb_page(vma, address, TLBF_NOBROADCAST | TLBF_NONOTIFY) +#define flush_tlb_fix_spurious_fault_pmd (vma, address, pmdp) \ + __flush_tlb_range(vma, address, address + PMD_SIZE, PMD_SIZE, 2, \ + TLBF_NOBROADCAST | TLBF_NONOTIFY | TLBF_NOWALKCACHE)Is there a reason to keep
__flush_tlb_page(), rather than definingflush_tlb_fix_spurious_fault()in terms of__flush_tlb_range()with all the level 3 constants?
__flush_tlb_page() is called by __ptep_clear_flush_young() and __ptep_set_access_flags() (as well as by flush_tlb_page()). I could replace them all, but __flush_tlb_page() is a bit less verbose... I have no strong preference.
Thanks,
Ryan
Quoted 22 lines
Mark.
Quoted 19 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 +1
-1@@ -591,7 +591,7 @@ static inline void __flush_tlb_page(struct vm_area_struct *vma,unsigned long start =round_down (uaddr,PAGE_SIZE );unsigned long end = start +PAGE_SIZE ; -__do_flush_tlb_range (vma, start, end,PAGE_SIZE ,TLBI_TTL_UNKNOWN , +__do_flush_tlb_range (vma, start, end,PAGE_SIZE ,3 ,TLBF_NOWALKCACHE | flags); }Signature · 1 line
2.43.0
Mark Rutland
reviewerRe: [PATCH v3 13/13] arm64: mm: Provide level hint for flush_tlb_page()
Quoted 17 lines · Ryan Roberts
Quoted 5 lines · Mark Rutland
Quoted 4 lines · Ryan Roberts
Previously tlb invalidations issued by
__flush_tlb_page()did not contain a level hint. But the function is clearly only ever targeting level 3 tlb entries and its documentation agrees:Quoted 7 lines
Quoted 2 lines
| this operation only invalidates a single, last-level page-table | entry and therefore does not affect any walk-caches
FWIW, I'd have read "last-level" as synonymous with "leaf" (i.e. a Page or Block entry, which is the last level of walk) rather than level 3 specifically. The architecture uses the term to match the former (e.g. in the description of TLBI VALE1IS).
Hmm yeah, now that I'm re-reading, I agree that quoted documentation doesn't say anything about it being level 3 specific.
Quoted 8 lines
But actually that was arm64-specific documentation for
flush_tlb_page(), which is a core-mm function. The generic docs atDocumentation/core-api/cachetlb.rstmake it clear that it's intended only for PTE invalidations, I think:| 4) ``void flush_tlb_page(struct vm_area_struct *vma, unsigned long addr)`` | | This time we need to remove the PAGE_SIZE sized translation | from the TLB. ...
Ah! I agree that's stronger and clearer.
Quoted 7 lines
Quoted 4 lines
If we're tightening up
__flush_tlb_page(), I think it'd be worth either updating the comment to explicitly note that this only applies to level 3 entries, OR update the comment+name to say it applies to leaf entries, and have it take a level parameter.I'll fix the arm64-specific docs to align with the generic docs and replace "last-level" with "level 3" if that works for you.
Yep, saying "level 3" definitely works for me -- I just want this to be explicit either way to minimize risk of confusion.
[...]
Quoted 14 lines
Quoted 9 lines
Quoted 6 lines
However, it turns out that the function was actually being used to invalidate a level 2 mapping via
flush_tlb_fix_spurious_fault_pmd(). The bug was benign because the level hint was not set so the HW would still invalidate the PMD mapping, and also because theTLBF_NONOTIFYflag was set, the bounds of the mapping were never used for anything else.I suspect (as above) that the current usage was intentional, legitimate usage, just poorly documented.
Before this series
flush_tlb_fix_spurious_fault_pmd()was implemented usinglocal_flush_tlb_page_nonotify()which never even gives an option to set the TTL hint, so I agree.
Quoted 5 lines
But I don't think
flush_tlb_fix_spurious_fault_pmd()should be calling any tlb flush api that has "page" in the name since that implies PTE, not PMD.I think what I have done is an improvement; but I'm happy to soften/correct this description in the next version.
I think if you include the quote from the core API documentation above, the rest is is good as is, and doesn't need to be softened.
Quoted 5 lines
Quoted 5 lines
Quoted 4 lines
Now that we have the new and improved range-invalidation API, it is trival to fix
flush_tlb_fix_spurious_fault_pmd()to explicitly flush the whole range (locally, without notification and last level only). So let's do that, and then update__flush_tlb_page()to hint level 3.
Quoted 7 lines
Quoted 4 lines
Do we never use
__flush_tlb_page()to manipulate a level 1 block mapping? I'd have expected we did the same lazy invalidation for permission relazation there, but if that's not the case, then this seems fine in principle.No, there is no
flush_tlb_fix_spurious_fault_pud(). (flush_tlb_fix_spurious_fault_pmd()was only added last cycle).
Thanks for confirming; I just wasn't sure whether that didn't exist or whether it had been possibly missed.
[...]
Quoted 10 lines
Quoted 9 lines
arch/arm64/include/asm/pgtable.h static inline void arch_leave_lazy_mmu_mode(void) · line 108
Quoted 5 lines
arch/arm64/include/asm/pgtable.h +3
-2-#define flush_tlb_fix_spurious_fault_pmd (vma, address, pmdp) \ - __flush_tlb_page(vma, address, TLBF_NOBROADCAST | TLBF_NONOTIFY) +#define flush_tlb_fix_spurious_fault_pmd (vma, address, pmdp) \ + __flush_tlb_range(vma, address, address + PMD_SIZE, PMD_SIZE, 2, \ + TLBF_NOBROADCAST | TLBF_NONOTIFY | TLBF_NOWALKCACHE)Is there a reason to keep
__flush_tlb_page(), rather than definingflush_tlb_fix_spurious_fault()in terms of__flush_tlb_range()with all the level 3 constants?
Quoted 3 lines
__flush_tlb_page()is called by__ptep_clear_flush_young()and__ptep_set_access_flags()(as well as byflush_tlb_page()). I could replace them all, but__flush_tlb_page()is a bit less verbose... I have no strong preference.
That's reason enough to keep it; thanks for confirming, and sorry for the noise!
Mark.
Quoted 9 lines · Ryan Roberts
This series refactors the TLB invalidation API to make it more general and flexible, and refactors the implementation, aiming to make it more robust, easier to understand and easier to add new features in future.
It is heavily based on the series posted by Will back in July at [1]; I've attempted to maintain correct authorship and tags - apologies if I got any of the etiquette wrong.
[...]
Applied to arm64 (for-next/tlbflush), thanks!
Slight tweak to patch 13 around the last-level vs level 3 comment (both source comment and the commit log).
[01/13] arm64: mm: Re-implement the __tlbi_level macro as a C function
https://git.kernel.org/arm64/c/5b3fb8a6b429 [02/13] arm64: mm: Introduce a C wrapper for by-range TLB invalidation https://git.kernel.org/arm64/c/d2bf3226952c [03/13] arm64: mm: Implicitly invalidate user ASID based on TLBI operation https://git.kernel.org/arm64/c/edc55b7abb25 [04/13] arm64: mm: Push __TLBI_VADDR() into __tlbi_level() https://git.kernel.org/arm64/c/a3710035604f [05/13] arm64: mm: Inline __TLBI_VADDR_RANGE() into __tlbi_range() https://git.kernel.org/arm64/c/d4b048ca145f [06/13] arm64: mm: Re-implement the __flush_tlb_range_op macro in C https://git.kernel.org/arm64/c/5e63b73f3deb [07/13] arm64: mm: Simplify __TLBI_RANGE_NUM() macro https://git.kernel.org/arm64/c/057bbd8e0610 [08/13] arm64: mm: Simplify __flush_tlb_range_limit_excess() https://git.kernel.org/arm64/c/c753d667d959 [09/13] arm64: mm: Refactor flush_tlb_page() to use __tlbi_level_asid() https://git.kernel.org/arm64/c/64212d689306 [10/13] arm64: mm: Refactor __flush_tlb_range() to take flags https://git.kernel.org/arm64/c/11f6dd8dd283 [11/13] arm64: mm: More flags for __flush_tlb_range() https://git.kernel.org/arm64/c/0477fc56960d [12/13] arm64: mm: Wrap flush_tlb_page() around __do_flush_tlb_range() https://git.kernel.org/arm64/c/15397e3c3850 [13/13] arm64: mm: Provide level hint for flush_tlb_page() https://git.kernel.org/arm64/c/752a0d1d483e
Signature · 1 line
Catalin