Don't broadcast TLBI if mm was only active on local CPU
cover Don't broadcast TLBI if mm was only active on local CPU
Hi All,
This is an RFC for my implementation of an idea from James Morse to avoid broadcasting TBLIs to remote CPUs if it can be proven that no remote CPU could have ever observed the pgtable entry for the TLB entry that is being invalidated. It turns out that x86 does something similar in principle.
The primary feedback I'm looking for is; is this actually correct and safe? James and I both believe it to be, but it would be useful to get further validation.
Beyond that, the next question is; does it actually improve performance? stress-ng's --tlb-shootdown stressor suggests yes; as concurrency increases, we do a much better job of sustaining the overall number of "tlb shootdowns per second" after the change:
+------------+--------------------------+--------------------------+--------------------------+ | | Baseline (v6.15) | tlbi local | Improvement | +------------+-------------+------------+-------------+------------+-------------+------------+ | nr_threads | ops/sec | ops/sec | ops/sec | ops/sec | ops/sec | ops/sec | | | (real time) | (cpu time) | (real time) | (cpu time) | (real time) | (cpu time) | +------------+-------------+------------+-------------+------------+-------------+------------+ | 1 | 9109 | 2573 | 8903 | 3653 | -2% | 42% | | 4 | 8115 | 1299 | 9892 | 1059 | 22% | -18% | | 8 | 5119 | 477 | 11854 | 1265 | 132% | 165% | | 16 | 4796 | 286 | 14176 | 821 | 196% | 187% | | 32 | 1593 | 38 | 15328 | 474 | 862% | 1147% | | 64 | 1486 | 19 | 8096 | 131 | 445% | 589% | | 128 | 1315 | 16 | 8257 | 145 | 528% | 806% | +------------+-------------+------------+-------------+------------+-------------+------------+
But looking at real-world benchmarks, I haven't yet found anything where it makes a huge difference; When compiling the kernel, it reduces kernel time by ~2.2%, but overall wall time remains the same. I'd be interested in any suggestions for workloads where this might prove valuable.
All mm selftests have been run and no regressions are observed. Applies on v6.17-rc3.
Thanks,
Ryan
Ryan Roberts (2):
arm64: tlbflush: Move invocation of __flush_tlb_range_op() to a macro
arm64: tlbflush: Don't broadcast if mm was only active on local cpu
arch/arm64/include/asm/mmu.h | 12 +++ arch/arm64/include/asm/mmu_context.h | 2 + arch/arm64/include/asm/tlbflush.h | 116 ++++++++++++++++++++++++--- arch/arm64/mm/context.c | 30 ++++++- 4 files changed, 145 insertions(+), 15 deletions(-)
--
2.43.0
RFC PATCH 1/2 arm64: tlbflush: Move invocation of __flush_tlb_range_op() to a macro
__flush_tlb_range_op() is a pre-processor macro that takes the TLBI operation as a string, and builds the instruction from it. This prevents passing the TLBI operation around as a variable. __flush_tlb_range_op() also takes 7 other arguments.
Adding extra invocations for different TLB operations means duplicating the whole thing, but those 7 extra arguments are the same each time.
Add an enum for the TLBI operations that __flush_tlb_range() uses, and a macro to pass the operation name as a string to __flush_tlb_range_op(), and the rest of the arguments using __VA_ARGS_.
The result is easier to add new TLBI operations to, and to modify any of the other arguments as they only appear once.
--- arch/arm64/include/asm/tlbflush.h | 30 ++++++++++++++++++++++++------ 1 file changed, 24 insertions(+), 6 deletions(-)
arch/arm64/include/asm/tlbflush.h +24 -6
@@ -11,6 +11,7 @@#ifndef __ASSEMBLY__#include <linux/bitfield.h> +#include <linux/build_bug.h> #include <linux/mm_types.h> #include <linux/sched.h> #include <linux/mmu_notifier.h>
@@ -433,12 +434,32 @@ static inline bool __flush_tlb_range_limit_excess(unsigned long start,return false; } +enum tlbi_op { +TLBI_VALE1IS , +TLBI_VAE1IS , +}; + +#define flush_tlb_range_op (op, ...) \ +do { \ + switch (op) { \ + case TLBI_VALE1IS: \ + __flush_tlb_range_op(vale1is, __VA_ARGS__); \ + break; \ + case TLBI_VAE1IS: \ + __flush_tlb_range_op(vae1is, __VA_ARGS__); \ + break; \ + default: \ + BUILD_BUG_ON_MSG(1, "Unknown TLBI op"); \ + } \ +} while (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 asid, pages; +enum tlbi_op tlbi_op; start =round_down (start, stride); end =round_up (end, stride);
@@ -452,12 +473,9 @@ static inline void __flush_tlb_range_nosync(struct mm_struct *mm,dsb (ishst); asid =ASID (mm); -if (last_level) -__flush_tlb_range_op (vale1is, start, pages, stride, asid, - tlb_level, true,lpa2_is_enabled ()); -else -__flush_tlb_range_op (vae1is, start, pages, stride, asid, - tlb_level, true,lpa2_is_enabled ()); + tlbi_op = last_level ?TLBI_VALE1IS :TLBI_VAE1IS ; +flush_tlb_range_op (tlbi_op, start, pages, stride, asid, tlb_level, + true,lpa2_is_enabled ());mmu_notifier_arch_invalidate_secondary_tlbs (mm, start, end); }
Signature · 1 line
2.43.0
Re: [RFC PATCH v1 1/2] arm64: tlbflush: Move invocation of __flush_tlb_range_op() to a macro
Quoted 12 lines · Ryan Roberts
__flush_tlb_range_op()is a pre-processor macro that takes the TLBI operation as a string, and builds the instruction from it. This prevents passing the TLBI operation around as a variable.__flush_tlb_range_op()also takes 7 other arguments.Adding extra invocations for different TLB operations means duplicating the whole thing, but those 7 extra arguments are the same each time.
Add an enum for the TLBI operations that
__flush_tlb_range()uses, and a macro to pass the operation name as a string to__flush_tlb_range_op(), and the rest of the arguments using__VA_ARGS_.
Quoted 5 lines
The result is easier to add new TLBI operations to, and to modify any of the other arguments as they only appear once.
Suggested-by James Morse <[email protected]>Signed-off-by Ryan Roberts <[email protected]>
Re: [RFC PATCH v1 1/2] arm64: tlbflush: Move invocation of __flush_tlb_range_op() to a macro
In reply to Ryan RobertsQuoted 72 lines · Ryan Roberts
__flush_tlb_range_op()is a pre-processor macro that takes the TLBI operation as a string, and builds the instruction from it. This prevents passing the TLBI operation around as a variable.__flush_tlb_range_op()also takes 7 other arguments.Adding extra invocations for different TLB operations means duplicating the whole thing, but those 7 extra arguments are the same each time.
Add an enum for the TLBI operations that
__flush_tlb_range()uses, and a macro to pass the operation name as a string to__flush_tlb_range_op(), and the rest of the arguments using__VA_ARGS_.The result is easier to add new TLBI operations to, and to modify any of the other arguments as they only appear once.
Suggested-by James Morse <[email protected]>Signed-off-by Ryan Roberts <[email protected]>--- arch/arm64/include/asm/tlbflush.h | 30 ++++++++++++++++++++++++------ 1 file changed, 24 insertions(+), 6 deletions(-)arch/arm64/include/asm/tlbflush.h +21
-2@@ -11,6 +11,7 @@#ifndef __ASSEMBLY__#include <linux/bitfield.h> +#include <linux/build_bug.h> #include <linux/mm_types.h> #include <linux/sched.h> #include <linux/mmu_notifier.h> @@ -433,12 +434,32 @@ static inline bool __flush_tlb_range_limit_excess(unsigned long start,return false; } +enum tlbi_op { +TLBI_VALE1IS , +TLBI_VAE1IS , +}; + +#define flush_tlb_range_op (op, ...) \ +do { \ + switch (op) { \ + case TLBI_VALE1IS: \ + __flush_tlb_range_op(vale1is, __VA_ARGS__); \ + break; \ + case TLBI_VAE1IS: \ + __flush_tlb_range_op(vae1is, __VA_ARGS__); \ + break; \ + default: \ + BUILD_BUG_ON_MSG(1, "Unknown TLBI op"); \ + } \ +} while (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 asid, pages; +enum tlbi_op tlbi_op; start =round_down (start, stride); end =round_up (end, stride);@@ -452,12 +473,9 @@ static inline void __flush_tlb_range_nosync(struct mm_struct *mm,dsb (ishst); asid =ASID (mm); -if (last_level) - __flush_tlb_range_op(vale1is, start, pages, stride, asid,
arch/arm64/include/asm/tlbflush.h static inline void __flush_tlb_range_nosync(struct mm_struct *mm, · line 481
Quoted 10 lines
arch/arm64/include/asm/tlbflush.h +3
-4- tlb_level, true, lpa2_is_enabled()); - else -__flush_tlb_range_op (vae1is ,start ,pages ,stride ,asid , -tlb_level ,true ,lpa2_is_enabled ()); + tlbi_op = last_level ?TLBI_VALE1IS :TLBI_VAE1IS ; +flush_tlb_range_op (tlbi_op, start, pages, stride, asid, tlb_level, + true,lpa2_is_enabled ());mmu_notifier_arch_invalidate_secondary_tlbs (mm, start, end); }
Should the remaining __flush_tlb_range_op() in flush_tlb_kernel_range() converted into flush_tlb_range_op() adding another similar enum variable i.e TLBI_VAALE1IS ? Because this will ensure that there is one variant helper i.e flush_tlb_range_op() that gets called.
arch/arm64/include/asm/tlbflush.h +5 -1
@@ -437,6 +437,7 @@ static inline bool __flush_tlb_range_limit_excess(unsigned long start,enum tlbi_op {TLBI_VALE1IS ,TLBI_VAE1IS , +TLBI_VAALE1IS , };#define flush_tlb_range_op (op, ...) \
@@ -448,6 +449,9 @@ do { \case TLBI_VAE1IS : \__flush_tlb_range_op (vae1is, __VA_ARGS__); \break ; \ +case TLBI_VAALE1IS : \ +__flush_tlb_range_op (vaale1is, __VA_ARGS__); \ +break ; \default : \BUILD_BUG_ON_MSG (1 ,"Unknown TLBI op" ); \ } \
@@ -517,7 +521,7 @@ static inline void flush_tlb_kernel_range(unsigned long start, unsigned long end }dsb (ishst); -__flush_tlb_range_op (vaale1is, start, pages, stride,0 , +flush_tlb_range_op (TLBI_VAALE1IS , start, pages, stride,0 ,TLBI_TTL_UNKNOWN , false,lpa2_is_enabled ());dsb (ish);isb ();
Re: [RFC PATCH v1 1/2] arm64: tlbflush: Move invocation of __flush_tlb_range_op() to a macro
Quoted 83 lines · Anshuman Khandual
Quoted 80 lines · Ryan Roberts
__flush_tlb_range_op()is a pre-processor macro that takes the TLBI operation as a string, and builds the instruction from it. This prevents passing the TLBI operation around as a variable.__flush_tlb_range_op()also takes 7 other arguments.Adding extra invocations for different TLB operations means duplicating the whole thing, but those 7 extra arguments are the same each time.
Add an enum for the TLBI operations that
__flush_tlb_range()uses, and a macro to pass the operation name as a string to__flush_tlb_range_op(), and the rest of the arguments using__VA_ARGS_.The result is easier to add new TLBI operations to, and to modify any of the other arguments as they only appear once.
Suggested-by James Morse <[email protected]>Signed-off-by Ryan Roberts <[email protected]>--- arch/arm64/include/asm/tlbflush.h | 30 ++++++++++++++++++++++++------ 1 file changed, 24 insertions(+), 6 deletions(-)arch/arm64/include/asm/tlbflush.h +24
-6@@ -11,6 +11,7 @@#ifndef __ASSEMBLY__#include <linux/bitfield.h> +#include <linux/build_bug.h> #include <linux/mm_types.h> #include <linux/sched.h> #include <linux/mmu_notifier.h> @@ -433,12 +434,32 @@ static inline bool __flush_tlb_range_limit_excess(unsigned long start,return false; } +enum tlbi_op { +TLBI_VALE1IS , +TLBI_VAE1IS , +}; + +#define flush_tlb_range_op (op, ...) \ +do { \ + switch (op) { \ + case TLBI_VALE1IS: \ + __flush_tlb_range_op(vale1is, __VA_ARGS__); \ + break; \ + case TLBI_VAE1IS: \ + __flush_tlb_range_op(vae1is, __VA_ARGS__); \ + break; \ + default: \ + BUILD_BUG_ON_MSG(1, "Unknown TLBI op"); \ + } \ +} while (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 asid, pages; +enum tlbi_op tlbi_op; start =round_down (start, stride); end =round_up (end, stride);@@ -452,12 +473,9 @@ static inline void __flush_tlb_range_nosync(struct mm_struct *mm,dsb (ishst); asid =ASID (mm); -if (last_level) -__flush_tlb_range_op (vale1is, start, pages, stride, asid, - tlb_level, true,lpa2_is_enabled ()); -else -__flush_tlb_range_op (vae1is, start, pages, stride, asid, - tlb_level, true,lpa2_is_enabled ()); + tlbi_op = last_level ?TLBI_VALE1IS :TLBI_VAE1IS ; +flush_tlb_range_op (tlbi_op, start, pages, stride, asid, tlb_level, + true,lpa2_is_enabled ());
Quoted 7 lines
arch/arm64/include/asm/tlbflush.h static inline void __flush_tlb_range_nosync(struct mm_struct *mm, · line 481
Quoted 2 lines
arch/arm64/include/asm/tlbflush.h +0
-0mmu_notifier_arch_invalidate_secondary_tlbs (mm, start, end); }Should the remaining
__flush_tlb_range_op()inflush_tlb_kernel_range()converted intoflush_tlb_range_op()adding another similar enum variable i.eTLBI_VAALE1IS? Because this will ensure that there is one variant helper i.eflush_tlb_range_op()that gets called.
Yeah maybe. I don't really have a strong opinion.
Will posted an RFC for converting all this to functions a while ago. That's the better baseline to build this all on I think.
Quoted 32 lines
arch/arm64/include/asm/tlbflush.h +5
-1@@ -437,6 +437,7 @@ static inline bool __flush_tlb_range_limit_excess(unsigned long start,enum tlbi_op {TLBI_VALE1IS ,TLBI_VAE1IS , +TLBI_VAALE1IS , };#define flush_tlb_range_op (op, ...) \@@ -448,6 +449,9 @@ do { \case TLBI_VAE1IS : \__flush_tlb_range_op (vae1is, __VA_ARGS__); \break ; \ +case TLBI_VAALE1IS : \ +__flush_tlb_range_op (vaale1is, __VA_ARGS__); \ +break ; \default : \BUILD_BUG_ON_MSG (1 ,"Unknown TLBI op" ); \ } \@@ -517,7 +521,7 @@ static inline void flush_tlb_kernel_range(unsigned long start, unsigned long end }dsb (ishst); -__flush_tlb_range_op (vaale1is, start, pages, stride,0 , +flush_tlb_range_op (TLBI_VAALE1IS , start, pages, stride,0 ,TLBI_TTL_UNKNOWN , false,lpa2_is_enabled ());dsb (ish);isb ();
RFC PATCH 2/2 arm64: tlbflush: Don't broadcast if mm was only active on local cpu
In reply to Ryan RobertsThere are 3 variants of tlb flush that invalidate user mappings:flush_tlb_mm(), flush_tlb_page() and __flush_tlb_range(). All of these would previously unconditionally broadcast their tlbis to all cpus in the inner shareable domain.
But this is a waste of effort if we can prove that the mm for which we are flushing the mappings has only ever been active on the local cpu. In that case, it is safe to avoid the broadcast and simply invalidate the current cpu.
So let's track in mm_context_t::active_cpu either the mm has never been active on any cpu, has been active on more than 1 cpu, or has been active on precisely 1 cpu - and in that case, which one. We update this when switching context, being careful to ensure that it gets updated *before* installing the mm's pgtables. On the reader side, we ensure we read *after* the previous write(s) to the pgtable(s) that necessitated the tlb flush have completed. This guarrantees that if a cpu that is doing a tlb flush sees it's own id in active_cpu, then the old pgtable entry cannot have been seen by any other cpu and we can flush only the local cpu.
--- arch/arm64/include/asm/mmu.h | 12 ++++ arch/arm64/include/asm/mmu_context.h | 2 + arch/arm64/include/asm/tlbflush.h | 90 +++++++++++++++++++++++++--- arch/arm64/mm/context.c | 30 +++++++++- 4 files changed, 123 insertions(+), 11 deletions(-)
arch/arm64/include/asm/mmu.h +12 -0
@@ -17,6 +17,17 @@#include <linux/refcount.h> #include <asm/cpufeature.h> +/* +* Sentinal values for mm_context_t::active_cpu. ACTIVE_CPU_NONE indicates the +* mm has never been active on any CPU. ACTIVE_CPU_MULTIPLE indicates the mm +* has been active on multiple CPUs. Any other value is the ID of the single +* CPU that the mm has been active on. +*/ +enum active_cpu { +ACTIVE_CPU_NONE =UINT_MAX , +ACTIVE_CPU_MULTIPLE =UINT_MAX -1 , +}; + typedef struct { atomic64_t id; #ifdef CONFIG_COMPAT
@@ -26,6 +37,7 @@ typedef struct {void *vdso;unsigned long flags;u8 pkey_allocation_map; +unsigned int active_cpu; } mm_context_t;/*
arch/arm64/include/asm/mmu_context.h +2 -0
@@ -180,6 +180,8 @@ init_new_context(struct task_struct *tsk, struct mm_struct *mm)/* pkey 0 is the default, so always reserve it. */ mm->context.pkey_allocation_map =BIT (0 ); +WRITE_ONCE (mm->context.active_cpu,ACTIVE_CPU_NONE ); +return 0 ; }
arch/arm64/include/asm/tlbflush.h +82 -8
141 lines not on this page · raw message
arch/arm64/mm/context.c +27 -3
@@ -214,9 +214,10 @@ static u64 new_context(struct mm_struct *mm)void check_and_switch_context (struct mm_struct *mm) { -unsigned long flags; -unsigned int cpu; +unsigned int cpu =smp_processor_id ();u64 asid, old_active_asid; +unsigned int active; +unsigned long flags;if (system_supports_cnp ())cpu_set_reserved_ttbr0 ();
@@ -251,7 +252,6 @@ void check_and_switch_context(struct mm_struct *mm)atomic64_set (&mm->context.id, asid); } - cpu =smp_processor_id ();if (cpumask_test_and_clear_cpu (cpu, &tlb_flush_pending))local_flush_tlb_all ();
@@ -262,6 +262,30 @@ void check_and_switch_context(struct mm_struct *mm)arm64_apply_bp_hardening (); +/* +* Update mm->context.active_cpu in such a manner that we avoid cmpxchg +* and dsb unless we definitely need it. If initially ACTIVE_CPU_NONE +* then we are the first cpu to run so set it to our id. If initially +* any id other than ours, we are the second cpu to run so set it to +* ACTIVE_CPU_MULTIPLE. If we update the value then we must issue +* dsb(ishst) to ensure stores to mm->context.active_cpu are ordered +* against the TTBR0 write in cpu_switch_mm()/uaccess_enable(); the +* store must be visible to another cpu before this cpu could have +* populated any TLB entries based on the pgtables that will be +* installed. +*/ + active =READ_ONCE (mm->context.active_cpu); +if (active != cpu && active !=ACTIVE_CPU_MULTIPLE ) { +if (active ==ACTIVE_CPU_NONE ) + active =cmpxchg_relaxed (&mm->context.active_cpu, +ACTIVE_CPU_NONE , cpu); + +if (active !=ACTIVE_CPU_NONE ) +WRITE_ONCE (mm->context.active_cpu,ACTIVE_CPU_MULTIPLE ); + +dsb (ishst); + } +/* * Defer TTBR0_EL1 setting for user threads to uaccess_enable() when * emulating PAN.
Signature · 1 line
2.43.0
Re: [RFC PATCH v1 2/2] arm64: tlbflush: Don't broadcast if mm was only active on local cpu
Hi Ryan,
Quoted 10 lines · Ryan Roberts
There are 3 variants of tlb flush that invalidate user mappings:
flush_tlb_mm(),flush_tlb_page()and__flush_tlb_range(). All of these would previously unconditionally broadcast their tlbis to all cpus in the inner shareable domain.But this is a waste of effort if we can prove that the mm for which we are flushing the mappings has only ever been active on the local cpu. In that case, it is safe to avoid the broadcast and simply invalidate the current cpu.
Quoted 10 lines
So let's track in
mm_context_t::active_cpueither the mm has never been active on any cpu, has been active on more than 1 cpu, or has been active on precisely 1 cpu - and in that case, which one. We update this when switching context, being careful to ensure that it gets updated *before* installing the mm's pgtables. On the reader side, we ensure we read *after* the previous write(s) to the pgtable(s) that necessitated the tlb flush have completed. This guarrantees that if a cpu that is doing a tlb flush sees it's own id inactive_cpu, then the old pgtable entry cannot have been seen by any other cpu and we can flush only the local cpu.
Just curious, how does this work under virtualization, when the VCPU can get moved around on different physical CPUs?
Thanks,
Alex
Quoted 264 lines
Signed-off-by Ryan Roberts <[email protected]>--- arch/arm64/include/asm/mmu.h | 12 ++++ arch/arm64/include/asm/mmu_context.h | 2 + arch/arm64/include/asm/tlbflush.h | 90 +++++++++++++++++++++++++--- arch/arm64/mm/context.c | 30 +++++++++- 4 files changed, 123 insertions(+), 11 deletions(-)arch/arm64/include/asm/mmu.h +12
-0@@ -17,6 +17,17 @@#include <linux/refcount.h> #include <asm/cpufeature.h> +/* +* Sentinal values for mm_context_t::active_cpu. ACTIVE_CPU_NONE indicates the +* mm has never been active on any CPU. ACTIVE_CPU_MULTIPLE indicates the mm +* has been active on multiple CPUs. Any other value is the ID of the single +* CPU that the mm has been active on. +*/ +enum active_cpu { +ACTIVE_CPU_NONE =UINT_MAX , +ACTIVE_CPU_MULTIPLE =UINT_MAX -1 , +}; + typedef struct { atomic64_t id; #ifdef CONFIG_COMPAT@@ -26,6 +37,7 @@ typedef struct {void *vdso;unsigned long flags;u8 pkey_allocation_map; +unsigned int active_cpu; } mm_context_t;/* arch/arm64/include/asm/mmu_context.h +2
-0@@ -180,6 +180,8 @@ init_new_context(struct task_struct *tsk, struct mm_struct *mm)/* pkey 0 is the default, so always reserve it. */ mm->context.pkey_allocation_map =BIT (0 ); +WRITE_ONCE (mm->context.active_cpu,ACTIVE_CPU_NONE ); +return 0 ; }arch/arm64/include/asm/tlbflush.h +82
-8@@ -182,6 +182,52 @@ static inline unsigned long get_trans_granule(void) (__pages >> (5 * (scale) +1 )) -1 ; \ }) +/* +* Determines whether the user tlbi invalidation can be performed only on the +* local CPU or whether it needs to be broadcast. (Returns true for local). +* Additionally issues appropriate barrier to ensure prior pgtable updates are +* visible to the table walker. Must be paired with flush_tlb_user_post(). +*/ +static inline bool flush_tlb_user_pre (struct mm_struct *mm) +{ +unsigned int self, active; +bool local; + +migrate_disable (); + + self =smp_processor_id (); + +/* +* The load of mm->context.active_cpu must not be reordered before the +* store to the pgtable that necessitated this flush. This ensures that +* if the value read is our cpu id, then no other cpu can have seen the +* old pgtable value and therefore does not need this old value to be +* flushed from its tlb. But we don't want to upgrade the dsb(ishst), +* needed to make the pgtable updates visible to the walker, to a +* dsb(ish) by default. So speculatively load without a barrier and if +* it indicates our cpu id, then upgrade the barrier and re-load. +*/ + active =READ_ONCE (mm->context.active_cpu); +if (active == self) { +dsb (ish); + active =READ_ONCE (mm->context.active_cpu); + }else { +dsb (ishst); + } + + local = active == self; +if (!local) +migrate_enable (); + +return local; +} + +static inline void flush_tlb_user_post (bool local) +{ +if (local) +migrate_enable (); +} +/* * TLB Invalidation * ================ @@ -274,11 +320,18 @@ static inline void flush_tlb_all(void)static inline void flush_tlb_mm (struct mm_struct *mm) {unsigned long asid; +bool local; -dsb (ishst); + local =flush_tlb_user_pre (mm); asid =__TLBI_VADDR (0 ,ASID (mm)); -__tlbi (aside1is, asid); -__tlbi_user (aside1is, asid); +if (local) { +__tlbi (aside1, asid); +__tlbi_user (aside1, asid); + }else { +__tlbi (aside1is, asid); +__tlbi_user (aside1is, asid); + } +flush_tlb_user_post (local);dsb (ish);mmu_notifier_arch_invalidate_secondary_tlbs (mm,0 ,-1UL ); }@@ -287,11 +340,18 @@ static inline void __flush_tlb_page_nosync(struct mm_struct *mm, unsigned long uaddr) {unsigned long addr; +bool local; -dsb (ishst); + local =flush_tlb_user_pre (mm); addr =__TLBI_VADDR (uaddr,ASID (mm)); -__tlbi (vale1is, addr); -__tlbi_user (vale1is, addr); +if (local) { +__tlbi (vale1, addr); +__tlbi_user (vale1, addr); + }else { +__tlbi (vale1is, addr); +__tlbi_user (vale1is, addr); + } +flush_tlb_user_post (local);mmu_notifier_arch_invalidate_secondary_tlbs (mm, uaddr &PAGE_MASK , (uaddr &PAGE_MASK ) +PAGE_SIZE ); }@@ -437,6 +497,8 @@ static inline bool __flush_tlb_range_limit_excess(unsigned long start,enum tlbi_op {TLBI_VALE1IS ,TLBI_VAE1IS , +TLBI_VALE1 , +TLBI_VAE1 , };#define flush_tlb_range_op (op, ...) \@@ -448,6 +510,12 @@ do { \case TLBI_VAE1IS : \__flush_tlb_range_op (vae1is, __VA_ARGS__); \break ; \ +case TLBI_VALE1 : \ +__flush_tlb_range_op (vale1, __VA_ARGS__); \ +break ; \ +case TLBI_VAE1 : \ +__flush_tlb_range_op (vae1, __VA_ARGS__); \ +break ; \default : \BUILD_BUG_ON_MSG (1 ,"Unknown TLBI op" ); \ } \@@ -460,6 +528,7 @@ static inline void __flush_tlb_range_nosync(struct mm_struct *mm, {unsigned long asid, pages;enum tlbi_op tlbi_op; +bool local; start =round_down (start, stride); end =round_up (end, stride);@@ -470,13 +539,18 @@ static inline void __flush_tlb_range_nosync(struct mm_struct *mm,return ; } -dsb (ishst); + local =flush_tlb_user_pre (mm); asid =ASID (mm); - tlbi_op = last_level ?TLBI_VALE1IS :TLBI_VAE1IS ; +if (local) + tlbi_op = last_level ?TLBI_VALE1 :TLBI_VAE1 ; +else + tlbi_op = last_level ?TLBI_VALE1IS :TLBI_VAE1IS ; +flush_tlb_range_op (tlbi_op, start, pages, stride, asid, tlb_level, true,lpa2_is_enabled ()); +flush_tlb_user_post (local);mmu_notifier_arch_invalidate_secondary_tlbs (mm, start, end); }arch/arm64/mm/context.c +27
-3@@ -214,9 +214,10 @@ static u64 new_context(struct mm_struct *mm)void check_and_switch_context (struct mm_struct *mm) { -unsigned long flags; -unsigned int cpu; +unsigned int cpu =smp_processor_id ();u64 asid, old_active_asid; +unsigned int active; +unsigned long flags;if (system_supports_cnp ())cpu_set_reserved_ttbr0 ();@@ -251,7 +252,6 @@ void check_and_switch_context(struct mm_struct *mm)atomic64_set (&mm->context.id, asid); } - cpu =smp_processor_id ();if (cpumask_test_and_clear_cpu (cpu, &tlb_flush_pending))local_flush_tlb_all ();@@ -262,6 +262,30 @@ void check_and_switch_context(struct mm_struct *mm)arm64_apply_bp_hardening (); +/* +* Update mm->context.active_cpu in such a manner that we avoid cmpxchg +* and dsb unless we definitely need it. If initially ACTIVE_CPU_NONE +* then we are the first cpu to run so set it to our id. If initially +* any id other than ours, we are the second cpu to run so set it to +* ACTIVE_CPU_MULTIPLE. If we update the value then we must issue +* dsb(ishst) to ensure stores to mm->context.active_cpu are ordered +* against the TTBR0 write in cpu_switch_mm()/uaccess_enable(); the +* store must be visible to another cpu before this cpu could have +* populated any TLB entries based on the pgtables that will be +* installed. +*/ + active =READ_ONCE (mm->context.active_cpu); +if (active != cpu && active !=ACTIVE_CPU_MULTIPLE ) { +if (active ==ACTIVE_CPU_NONE ) + active =cmpxchg_relaxed (&mm->context.active_cpu, +ACTIVE_CPU_NONE , cpu); + +if (active !=ACTIVE_CPU_NONE ) +WRITE_ONCE (mm->context.active_cpu,ACTIVE_CPU_MULTIPLE ); + +dsb (ishst); + } +/* * Defer TTBR0_EL1 setting for user threads to uaccess_enable() when * emulating PAN. Signature · 1 line
2.43.0
Re: [RFC PATCH v1 2/2] arm64: tlbflush: Don't broadcast if mm was only active on local cpu
Quoted 24 lines · Alexandru Elisei
Hi Ryan,
Quoted 10 lines · Ryan Roberts
There are 3 variants of tlb flush that invalidate user mappings:
flush_tlb_mm(),flush_tlb_page()and__flush_tlb_range(). All of these would previously unconditionally broadcast their tlbis to all cpus in the inner shareable domain.But this is a waste of effort if we can prove that the mm for which we are flushing the mappings has only ever been active on the local cpu. In that case, it is safe to avoid the broadcast and simply invalidate the current cpu.
Quoted 10 lines
So let's track in
mm_context_t::active_cpueither the mm has never been active on any cpu, has been active on more than 1 cpu, or has been active on precisely 1 cpu - and in that case, which one. We update this when switching context, being careful to ensure that it gets updated *before* installing the mm's pgtables. On the reader side, we ensure we read *after* the previous write(s) to the pgtable(s) that necessitated the tlb flush have completed. This guarrantees that if a cpu that is doing a tlb flush sees it's own id inactive_cpu, then the old pgtable entry cannot have been seen by any other cpu and we can flush only the local cpu.
Quoted 2 lines
Just curious, how does this work under virtualization, when the VCPU can get moved around on different physical CPUs?
I believe the HW is configured to upgrade local TLBIs to broadcast for this case? I'll have to check the details of that though...
Quoted 268 lines
Thanks,
AlexQuoted 264 lines
Signed-off-by Ryan Roberts <[email protected]>--- arch/arm64/include/asm/mmu.h | 12 ++++ arch/arm64/include/asm/mmu_context.h | 2 + arch/arm64/include/asm/tlbflush.h | 90 +++++++++++++++++++++++++--- arch/arm64/mm/context.c | 30 +++++++++- 4 files changed, 123 insertions(+), 11 deletions(-)arch/arm64/include/asm/mmu.h +12
-0@@ -17,6 +17,17 @@#include <linux/refcount.h> #include <asm/cpufeature.h> +/* +* Sentinal values for mm_context_t::active_cpu. ACTIVE_CPU_NONE indicates the +* mm has never been active on any CPU. ACTIVE_CPU_MULTIPLE indicates the mm +* has been active on multiple CPUs. Any other value is the ID of the single +* CPU that the mm has been active on. +*/ +enum active_cpu { +ACTIVE_CPU_NONE =UINT_MAX , +ACTIVE_CPU_MULTIPLE =UINT_MAX -1 , +}; + typedef struct { atomic64_t id; #ifdef CONFIG_COMPAT@@ -26,6 +37,7 @@ typedef struct {void *vdso;unsigned long flags;u8 pkey_allocation_map; +unsigned int active_cpu; } mm_context_t;/* arch/arm64/include/asm/mmu_context.h +2
-0@@ -180,6 +180,8 @@ init_new_context(struct task_struct *tsk, struct mm_struct *mm)/* pkey 0 is the default, so always reserve it. */ mm->context.pkey_allocation_map =BIT (0 ); +WRITE_ONCE (mm->context.active_cpu,ACTIVE_CPU_NONE ); +return 0 ; }arch/arm64/include/asm/tlbflush.h +82
-8@@ -182,6 +182,52 @@ static inline unsigned long get_trans_granule(void) (__pages >> (5 * (scale) +1 )) -1 ; \ }) +/* +* Determines whether the user tlbi invalidation can be performed only on the +* local CPU or whether it needs to be broadcast. (Returns true for local). +* Additionally issues appropriate barrier to ensure prior pgtable updates are +* visible to the table walker. Must be paired with flush_tlb_user_post(). +*/ +static inline bool flush_tlb_user_pre (struct mm_struct *mm) +{ +unsigned int self, active; +bool local; + +migrate_disable (); + + self =smp_processor_id (); + +/* +* The load of mm->context.active_cpu must not be reordered before the +* store to the pgtable that necessitated this flush. This ensures that +* if the value read is our cpu id, then no other cpu can have seen the +* old pgtable value and therefore does not need this old value to be +* flushed from its tlb. But we don't want to upgrade the dsb(ishst), +* needed to make the pgtable updates visible to the walker, to a +* dsb(ish) by default. So speculatively load without a barrier and if +* it indicates our cpu id, then upgrade the barrier and re-load. +*/ + active =READ_ONCE (mm->context.active_cpu); +if (active == self) { +dsb (ish); + active =READ_ONCE (mm->context.active_cpu); + }else { +dsb (ishst); + } + + local = active == self; +if (!local) +migrate_enable (); + +return local; +} + +static inline void flush_tlb_user_post (bool local) +{ +if (local) +migrate_enable (); +} +/* * TLB Invalidation * ================ @@ -274,11 +320,18 @@ static inline void flush_tlb_all(void)static inline void flush_tlb_mm (struct mm_struct *mm) {unsigned long asid; +bool local; -dsb (ishst); + local =flush_tlb_user_pre (mm); asid =__TLBI_VADDR (0 ,ASID (mm)); -__tlbi (aside1is, asid); -__tlbi_user (aside1is, asid); +if (local) { +__tlbi (aside1, asid); +__tlbi_user (aside1, asid); + }else { +__tlbi (aside1is, asid); +__tlbi_user (aside1is, asid); + } +flush_tlb_user_post (local);dsb (ish);mmu_notifier_arch_invalidate_secondary_tlbs (mm,0 ,-1UL ); }@@ -287,11 +340,18 @@ static inline void __flush_tlb_page_nosync(struct mm_struct *mm, unsigned long uaddr) {unsigned long addr; +bool local; -dsb (ishst); + local =flush_tlb_user_pre (mm); addr =__TLBI_VADDR (uaddr,ASID (mm)); -__tlbi (vale1is, addr); -__tlbi_user (vale1is, addr); +if (local) { +__tlbi (vale1, addr); +__tlbi_user (vale1, addr); + }else { +__tlbi (vale1is, addr); +__tlbi_user (vale1is, addr); + } +flush_tlb_user_post (local);mmu_notifier_arch_invalidate_secondary_tlbs (mm, uaddr &PAGE_MASK , (uaddr &PAGE_MASK ) +PAGE_SIZE ); }@@ -437,6 +497,8 @@ static inline bool __flush_tlb_range_limit_excess(unsigned long start,enum tlbi_op {TLBI_VALE1IS ,TLBI_VAE1IS , +TLBI_VALE1 , +TLBI_VAE1 , };#define flush_tlb_range_op (op, ...) \@@ -448,6 +510,12 @@ do { \case TLBI_VAE1IS : \__flush_tlb_range_op (vae1is, __VA_ARGS__); \break ; \ +case TLBI_VALE1 : \ +__flush_tlb_range_op (vale1, __VA_ARGS__); \ +break ; \ +case TLBI_VAE1 : \ +__flush_tlb_range_op (vae1, __VA_ARGS__); \ +break ; \default : \BUILD_BUG_ON_MSG (1 ,"Unknown TLBI op" ); \ } \@@ -460,6 +528,7 @@ static inline void __flush_tlb_range_nosync(struct mm_struct *mm, {unsigned long asid, pages;enum tlbi_op tlbi_op; +bool local; start =round_down (start, stride); end =round_up (end, stride);@@ -470,13 +539,18 @@ static inline void __flush_tlb_range_nosync(struct mm_struct *mm,return ; } -dsb (ishst); + local =flush_tlb_user_pre (mm); asid =ASID (mm); - tlbi_op = last_level ?TLBI_VALE1IS :TLBI_VAE1IS ; +if (local) + tlbi_op = last_level ?TLBI_VALE1 :TLBI_VAE1 ; +else + tlbi_op = last_level ?TLBI_VALE1IS :TLBI_VAE1IS ; +flush_tlb_range_op (tlbi_op, start, pages, stride, asid, tlb_level, true,lpa2_is_enabled ()); +flush_tlb_user_post (local);mmu_notifier_arch_invalidate_secondary_tlbs (mm, start, end); }arch/arm64/mm/context.c +27
-3@@ -214,9 +214,10 @@ static u64 new_context(struct mm_struct *mm)void check_and_switch_context (struct mm_struct *mm) { -unsigned long flags; -unsigned int cpu; +unsigned int cpu =smp_processor_id ();u64 asid, old_active_asid; +unsigned int active; +unsigned long flags;if (system_supports_cnp ())cpu_set_reserved_ttbr0 ();@@ -251,7 +252,6 @@ void check_and_switch_context(struct mm_struct *mm)atomic64_set (&mm->context.id, asid); } - cpu =smp_processor_id ();if (cpumask_test_and_clear_cpu (cpu, &tlb_flush_pending))local_flush_tlb_all ();@@ -262,6 +262,30 @@ void check_and_switch_context(struct mm_struct *mm)arm64_apply_bp_hardening (); +/* +* Update mm->context.active_cpu in such a manner that we avoid cmpxchg +* and dsb unless we definitely need it. If initially ACTIVE_CPU_NONE +* then we are the first cpu to run so set it to our id. If initially +* any id other than ours, we are the second cpu to run so set it to +* ACTIVE_CPU_MULTIPLE. If we update the value then we must issue +* dsb(ishst) to ensure stores to mm->context.active_cpu are ordered +* against the TTBR0 write in cpu_switch_mm()/uaccess_enable(); the +* store must be visible to another cpu before this cpu could have +* populated any TLB entries based on the pgtables that will be +* installed. +*/ + active =READ_ONCE (mm->context.active_cpu); +if (active != cpu && active !=ACTIVE_CPU_MULTIPLE ) { +if (active ==ACTIVE_CPU_NONE ) + active =cmpxchg_relaxed (&mm->context.active_cpu, +ACTIVE_CPU_NONE , cpu); + +if (active !=ACTIVE_CPU_NONE ) +WRITE_ONCE (mm->context.active_cpu,ACTIVE_CPU_MULTIPLE ); + +dsb (ishst); + } +/* * Defer TTBR0_EL1 setting for user threads to uaccess_enable() when * emulating PAN. Signature · 1 line
2.43.0
Re: [RFC PATCH v1 2/2] arm64: tlbflush: Don't broadcast if mm was only active on local cpu
In reply to Ryan RobertsQuoted 30 lines · Ryan Roberts
arch/arm64/include/asm/tlbflush.h +22
-0@@ -182,6 +182,52 @@ static inline unsigned long get_trans_granule(void) (__pages >> (5 * (scale) +1 )) -1 ; \ }) +/* +* Determines whether the user tlbi invalidation can be performed only on the +* local CPU or whether it needs to be broadcast. (Returns true for local). +* Additionally issues appropriate barrier to ensure prior pgtable updates are +* visible to the table walker. Must be paired with flush_tlb_user_post(). +*/ +static inline bool flush_tlb_user_pre (struct mm_struct *mm) +{ +unsigned int self, active; +bool local; + +migrate_disable (); + + self =smp_processor_id (); + +/* +* The load of mm->context.active_cpu must not be reordered before the +* store to the pgtable that necessitated this flush. This ensures that +* if the value read is our cpu id, then no other cpu can have seen the +* old pgtable value and therefore does not need this old value to be +* flushed from its tlb. But we don't want to upgrade the dsb(ishst), +* needed to make the pgtable updates visible to the walker, to a
arch/arm64/include/asm/tlbflush.h static inline unsigned long get_trans_granule(void) · line 216
Quoted 10 lines
arch/arm64/include/asm/tlbflush.h +10
-0+* dsb(ish) by default. So speculatively load without a barrier and if +* it indicates our cpu id, then upgrade the barrier and re-load. +*/ + active =READ_ONCE (mm->context.active_cpu); +if (active == self) { +dsb (ish); + active =READ_ONCE (mm->context.active_cpu); + }else { +dsb (ishst); + }
Does the ISH vs ISHST make much difference in practice? I wonder whether we could keep this simple.
Quoted 42 lines
arch/arm64/mm/context.c +16
-3@@ -214,9 +214,10 @@ static u64 new_context(struct mm_struct *mm)void check_and_switch_context (struct mm_struct *mm) { -unsigned long flags; -unsigned int cpu; +unsigned int cpu =smp_processor_id ();u64 asid, old_active_asid; +unsigned int active; +unsigned long flags;if (system_supports_cnp ())cpu_set_reserved_ttbr0 ();@@ -251,7 +252,6 @@ void check_and_switch_context(struct mm_struct *mm)atomic64_set (&mm->context.id, asid); } - cpu =smp_processor_id ();if (cpumask_test_and_clear_cpu (cpu, &tlb_flush_pending))local_flush_tlb_all ();@@ -262,6 +262,30 @@ void check_and_switch_context(struct mm_struct *mm)arm64_apply_bp_hardening (); +/* +* Update mm->context.active_cpu in such a manner that we avoid cmpxchg +* and dsb unless we definitely need it. If initially ACTIVE_CPU_NONE +* then we are the first cpu to run so set it to our id. If initially +* any id other than ours, we are the second cpu to run so set it to +* ACTIVE_CPU_MULTIPLE. If we update the value then we must issue +* dsb(ishst) to ensure stores to mm->context.active_cpu are ordered +* against the TTBR0 write in cpu_switch_mm()/uaccess_enable(); the +* store must be visible to another cpu before this cpu could have +* populated any TLB entries based on the pgtables that will be +* installed. +*/ + active =READ_ONCE (mm->context.active_cpu);
arch/arm64/mm/context.c void check_and_switch_context(struct mm_struct *mm) · line 287
Quoted 10 lines
arch/arm64/mm/context.c +10
-0+if (active != cpu && active !=ACTIVE_CPU_MULTIPLE ) { +if (active ==ACTIVE_CPU_NONE ) + active =cmpxchg_relaxed (&mm->context.active_cpu, +ACTIVE_CPU_NONE , cpu); + +if (active !=ACTIVE_CPU_NONE ) +WRITE_ONCE (mm->context.active_cpu,ACTIVE_CPU_MULTIPLE ); + +dsb (ishst); + }
I think this works. I recall James had a litmus test for the model checker confirming this.
In a cut-down version, we'd have:
P0: P1:
set_pte(); WRITE_ONCE(active_cpu);dsb(); dsb();READ_ONCE(active_cpu); READ_ONCE(pte);
The pte read on P1 is implied following the TTBR0 write. As you described, if P0 did not see the active_cpu update, P1 would have seen the updated pte.
So far I couldn't fail this, so:
Re: [RFC PATCH v1 2/2] arm64: tlbflush: Don't broadcast if mm was only active on local cpu
Quoted 36 lines · Catalin Marinas
Quoted 35 lines · Ryan Roberts
arch/arm64/include/asm/tlbflush.h +27
-0@@ -182,6 +182,52 @@ static inline unsigned long get_trans_granule(void) (__pages >> (5 * (scale) +1 )) -1 ; \ }) +/* +* Determines whether the user tlbi invalidation can be performed only on the +* local CPU or whether it needs to be broadcast. (Returns true for local). +* Additionally issues appropriate barrier to ensure prior pgtable updates are +* visible to the table walker. Must be paired with flush_tlb_user_post(). +*/ +static inline bool flush_tlb_user_pre (struct mm_struct *mm) +{ +unsigned int self, active; +bool local; + +migrate_disable (); + + self =smp_processor_id (); + +/* +* The load of mm->context.active_cpu must not be reordered before the +* store to the pgtable that necessitated this flush. This ensures that +* if the value read is our cpu id, then no other cpu can have seen the +* old pgtable value and therefore does not need this old value to be +* flushed from its tlb. But we don't want to upgrade the dsb(ishst), +* needed to make the pgtable updates visible to the walker, to a +* dsb(ish) by default. So speculatively load without a barrier and if +* it indicates our cpu id, then upgrade the barrier and re-load. +*/ + active =READ_ONCE (mm->context.active_cpu); +if (active == self) {
Quoted 8 lines
arch/arm64/include/asm/tlbflush.h static inline unsigned long get_trans_granule(void) · line 216
Quoted 5 lines
arch/arm64/include/asm/tlbflush.h +5
-0+dsb (ish); + active =READ_ONCE (mm->context.active_cpu); + }else { +dsb (ishst); + }Does the ISH vs ISHST make much difference in practice? I wonder whether we could keep this simple.
I don't know. I was being conservative - I'm a bit nervous about upgrading a barrier unconditionally. I'll run some benchmarks with the simpler version and compare.
Quoted 65 lines
Quoted 43 lines
arch/arm64/mm/context.c +16
-3@@ -214,9 +214,10 @@ static u64 new_context(struct mm_struct *mm)void check_and_switch_context (struct mm_struct *mm) { -unsigned long flags; -unsigned int cpu; +unsigned int cpu =smp_processor_id ();u64 asid, old_active_asid; +unsigned int active; +unsigned long flags;if (system_supports_cnp ())cpu_set_reserved_ttbr0 ();@@ -251,7 +252,6 @@ void check_and_switch_context(struct mm_struct *mm)atomic64_set (&mm->context.id, asid); } - cpu =smp_processor_id ();if (cpumask_test_and_clear_cpu (cpu, &tlb_flush_pending))local_flush_tlb_all ();@@ -262,6 +262,30 @@ void check_and_switch_context(struct mm_struct *mm)arm64_apply_bp_hardening (); +/* +* Update mm->context.active_cpu in such a manner that we avoid cmpxchg +* and dsb unless we definitely need it. If initially ACTIVE_CPU_NONE +* then we are the first cpu to run so set it to our id. If initially +* any id other than ours, we are the second cpu to run so set it to +* ACTIVE_CPU_MULTIPLE. If we update the value then we must issue +* dsb(ishst) to ensure stores to mm->context.active_cpu are ordered +* against the TTBR0 write in cpu_switch_mm()/uaccess_enable(); the +* store must be visible to another cpu before this cpu could have +* populated any TLB entries based on the pgtables that will be +* installed. +*/ + active =READ_ONCE (mm->context.active_cpu);arch/arm64/mm/context.c void check_and_switch_context(struct mm_struct *mm) · line 287
Quoted 10 lines
arch/arm64/mm/context.c +10
-0+if (active != cpu && active !=ACTIVE_CPU_MULTIPLE ) { +if (active ==ACTIVE_CPU_NONE ) + active =cmpxchg_relaxed (&mm->context.active_cpu, +ACTIVE_CPU_NONE , cpu); + +if (active !=ACTIVE_CPU_NONE ) +WRITE_ONCE (mm->context.active_cpu,ACTIVE_CPU_MULTIPLE ); + +dsb (ishst); + }I think this works. I recall James had a litmus test for the model checker confirming this.
In a cut-down version, we'd have:
P0: P1:
set_pte();WRITE_ONCE(active_cpu);dsb();dsb();READ_ONCE(active_cpu);READ_ONCE(pte);
Quoted 7 lines
The pte read on P1 is implied following the TTBR0 write. As you described, if P0 did not see the
active_cpuupdate, P1 would have seen the updated pte.So far I couldn't fail this, so:
Reviewed-by Catalin Marinas <[email protected]>
Hmm... Rb at v1.. are you feeling ok? :)
Re: [RFC PATCH v1 2/2] arm64: tlbflush: Don't broadcast if mm was only active on local cpu
In reply to Ryan RobertsHi Ryan,
Quoted 52 lines · Ryan Roberts
There are 3 variants of tlb flush that invalidate user mappings:
flush_tlb_mm(),flush_tlb_page()and__flush_tlb_range(). All of these would previously unconditionally broadcast their tlbis to all cpus in the inner shareable domain.But this is a waste of effort if we can prove that the mm for which we are flushing the mappings has only ever been active on the local cpu. In that case, it is safe to avoid the broadcast and simply invalidate the current cpu.
So let's track in
mm_context_t::active_cpueither the mm has never been active on any cpu, has been active on more than 1 cpu, or has been active on precisely 1 cpu - and in that case, which one. We update this when switching context, being careful to ensure that it gets updated *before* installing the mm's pgtables. On the reader side, we ensure we read *after* the previous write(s) to the pgtable(s) that necessitated the tlb flush have completed. This guarrantees that if a cpu that is doing a tlb flush sees it's own id inactive_cpu, then the old pgtable entry cannot have been seen by any other cpu and we can flush only the local cpu.Signed-off-by Ryan Roberts <[email protected]>--- arch/arm64/include/asm/mmu.h | 12 ++++ arch/arm64/include/asm/mmu_context.h | 2 + arch/arm64/include/asm/tlbflush.h | 90 +++++++++++++++++++++++++--- arch/arm64/mm/context.c | 30 +++++++++- 4 files changed, 123 insertions(+), 11 deletions(-)arch/arm64/include/asm/mmu.h +11
-0@@ -17,6 +17,17 @@#include <linux/refcount.h> #include <asm/cpufeature.h> +/* +* Sentinal values for mm_context_t::active_cpu. ACTIVE_CPU_NONE indicates the +* mm has never been active on any CPU. ACTIVE_CPU_MULTIPLE indicates the mm +* has been active on multiple CPUs. Any other value is the ID of the single +* CPU that the mm has been active on. +*/ +enum active_cpu { +ACTIVE_CPU_NONE =UINT_MAX , +ACTIVE_CPU_MULTIPLE =UINT_MAX -1 , +}; + typedef struct { atomic64_t id; #ifdef CONFIG_COMPAT
arch/arm64/include/asm/mmu.h typedef struct { · line 40
Quoted 4 lines
arch/arm64/include/asm/mmu.h +1
-0@@ -26,6 +37,7 @@ typedef struct {void *vdso;unsigned long flags;u8 pkey_allocation_map; +unsigned int active_cpu;
Any reason why you don't use bit mask to mark the active CPUs? And mm_struct also has cpu_bitmap to record the active CPUs which the process has run on. Why not just use it? x86 uses it to determine which CPUs kernel should send TLB flush IPI to. I understand this series just check whether local cpu is the active cpu or not, but bit mask should not make things more complicated. And it also can provide more flexibility. We can extend this, for example, use IPI to send local TLB flush if the number of active cpus is quite low. AFAIK, x86 added TLBI broadcast support too, and fallback to IPI if the number of active cpus is <= 3. IIRC, Christohper's series did the similar thing. He should be interested in this series too, cc'ed him.
Thanks,
Yang
Quoted 224 lines
arch/arm64/include/asm/mmu.h +0
-0} mm_context_t;/* arch/arm64/include/asm/mmu_context.h +2
-0@@ -180,6 +180,8 @@ init_new_context(struct task_struct *tsk, struct mm_struct *mm)/* pkey 0 is the default, so always reserve it. */ mm->context.pkey_allocation_map =BIT (0 ); +WRITE_ONCE (mm->context.active_cpu,ACTIVE_CPU_NONE ); +return 0 ; }arch/arm64/include/asm/tlbflush.h +82
-8@@ -182,6 +182,52 @@ static inline unsigned long get_trans_granule(void) (__pages >> (5 * (scale) +1 )) -1 ; \ }) +/* +* Determines whether the user tlbi invalidation can be performed only on the +* local CPU or whether it needs to be broadcast. (Returns true for local). +* Additionally issues appropriate barrier to ensure prior pgtable updates are +* visible to the table walker. Must be paired with flush_tlb_user_post(). +*/ +static inline bool flush_tlb_user_pre (struct mm_struct *mm) +{ +unsigned int self, active; +bool local; + +migrate_disable (); + + self =smp_processor_id (); + +/* +* The load of mm->context.active_cpu must not be reordered before the +* store to the pgtable that necessitated this flush. This ensures that +* if the value read is our cpu id, then no other cpu can have seen the +* old pgtable value and therefore does not need this old value to be +* flushed from its tlb. But we don't want to upgrade the dsb(ishst), +* needed to make the pgtable updates visible to the walker, to a +* dsb(ish) by default. So speculatively load without a barrier and if +* it indicates our cpu id, then upgrade the barrier and re-load. +*/ + active =READ_ONCE (mm->context.active_cpu); +if (active == self) { +dsb (ish); + active =READ_ONCE (mm->context.active_cpu); + }else { +dsb (ishst); + } + + local = active == self; +if (!local) +migrate_enable (); + +return local; +} + +static inline void flush_tlb_user_post (bool local) +{ +if (local) +migrate_enable (); +} +/* * TLB Invalidation * ================ @@ -274,11 +320,18 @@ static inline void flush_tlb_all(void)static inline void flush_tlb_mm (struct mm_struct *mm) {unsigned long asid; +bool local; -dsb (ishst); + local =flush_tlb_user_pre (mm); asid =__TLBI_VADDR (0 ,ASID (mm)); -__tlbi (aside1is, asid); -__tlbi_user (aside1is, asid); +if (local) { +__tlbi (aside1, asid); +__tlbi_user (aside1, asid); + }else { +__tlbi (aside1is, asid); +__tlbi_user (aside1is, asid); + } +flush_tlb_user_post (local);dsb (ish);mmu_notifier_arch_invalidate_secondary_tlbs (mm,0 ,-1UL ); }@@ -287,11 +340,18 @@ static inline void __flush_tlb_page_nosync(struct mm_struct *mm, unsigned long uaddr) {unsigned long addr; +bool local; -dsb (ishst); + local =flush_tlb_user_pre (mm); addr =__TLBI_VADDR (uaddr,ASID (mm)); -__tlbi (vale1is, addr); -__tlbi_user (vale1is, addr); +if (local) { +__tlbi (vale1, addr); +__tlbi_user (vale1, addr); + }else { +__tlbi (vale1is, addr); +__tlbi_user (vale1is, addr); + } +flush_tlb_user_post (local);mmu_notifier_arch_invalidate_secondary_tlbs (mm, uaddr &PAGE_MASK , (uaddr &PAGE_MASK ) +PAGE_SIZE ); }@@ -437,6 +497,8 @@ static inline bool __flush_tlb_range_limit_excess(unsigned long start,enum tlbi_op {TLBI_VALE1IS ,TLBI_VAE1IS , +TLBI_VALE1 , +TLBI_VAE1 , };#define flush_tlb_range_op (op, ...) \@@ -448,6 +510,12 @@ do { \case TLBI_VAE1IS : \__flush_tlb_range_op (vae1is, __VA_ARGS__); \break ; \ +case TLBI_VALE1 : \ +__flush_tlb_range_op (vale1, __VA_ARGS__); \ +break ; \ +case TLBI_VAE1 : \ +__flush_tlb_range_op (vae1, __VA_ARGS__); \ +break ; \default : \BUILD_BUG_ON_MSG (1 ,"Unknown TLBI op" ); \ } \@@ -460,6 +528,7 @@ static inline void __flush_tlb_range_nosync(struct mm_struct *mm, {unsigned long asid, pages;enum tlbi_op tlbi_op; +bool local; start =round_down (start, stride); end =round_up (end, stride);@@ -470,13 +539,18 @@ static inline void __flush_tlb_range_nosync(struct mm_struct *mm,return ; } -dsb (ishst); + local =flush_tlb_user_pre (mm); asid =ASID (mm); - tlbi_op = last_level ?TLBI_VALE1IS :TLBI_VAE1IS ; +if (local) + tlbi_op = last_level ?TLBI_VALE1 :TLBI_VAE1 ; +else + tlbi_op = last_level ?TLBI_VALE1IS :TLBI_VAE1IS ; +flush_tlb_range_op (tlbi_op, start, pages, stride, asid, tlb_level, true,lpa2_is_enabled ()); +flush_tlb_user_post (local);mmu_notifier_arch_invalidate_secondary_tlbs (mm, start, end); }arch/arm64/mm/context.c +27
-3@@ -214,9 +214,10 @@ static u64 new_context(struct mm_struct *mm)void check_and_switch_context (struct mm_struct *mm) { -unsigned long flags; -unsigned int cpu; +unsigned int cpu =smp_processor_id ();u64 asid, old_active_asid; +unsigned int active; +unsigned long flags;if (system_supports_cnp ())cpu_set_reserved_ttbr0 ();@@ -251,7 +252,6 @@ void check_and_switch_context(struct mm_struct *mm)atomic64_set (&mm->context.id, asid); } - cpu =smp_processor_id ();if (cpumask_test_and_clear_cpu (cpu, &tlb_flush_pending))local_flush_tlb_all ();@@ -262,6 +262,30 @@ void check_and_switch_context(struct mm_struct *mm)arm64_apply_bp_hardening (); +/* +* Update mm->context.active_cpu in such a manner that we avoid cmpxchg +* and dsb unless we definitely need it. If initially ACTIVE_CPU_NONE +* then we are the first cpu to run so set it to our id. If initially +* any id other than ours, we are the second cpu to run so set it to +* ACTIVE_CPU_MULTIPLE. If we update the value then we must issue +* dsb(ishst) to ensure stores to mm->context.active_cpu are ordered +* against the TTBR0 write in cpu_switch_mm()/uaccess_enable(); the +* store must be visible to another cpu before this cpu could have +* populated any TLB entries based on the pgtables that will be +* installed. +*/ + active =READ_ONCE (mm->context.active_cpu); +if (active != cpu && active !=ACTIVE_CPU_MULTIPLE ) { +if (active ==ACTIVE_CPU_NONE ) + active =cmpxchg_relaxed (&mm->context.active_cpu, +ACTIVE_CPU_NONE , cpu); + +if (active !=ACTIVE_CPU_NONE ) +WRITE_ONCE (mm->context.active_cpu,ACTIVE_CPU_MULTIPLE ); + +dsb (ishst); + } +/* * Defer TTBR0_EL1 setting for user threads to uaccess_enable() when * emulating PAN.
Re: [RFC PATCH v1 2/2] arm64: tlbflush: Don't broadcast if mm was only active on local cpu
Quoted 63 lines · Yang Shi
Hi Ryan,
Quoted 51 lines · Ryan Roberts
There are 3 variants of tlb flush that invalidate user mappings:
flush_tlb_mm(),flush_tlb_page()and__flush_tlb_range(). All of these would previously unconditionally broadcast their tlbis to all cpus in the inner shareable domain.But this is a waste of effort if we can prove that the mm for which we are flushing the mappings has only ever been active on the local cpu. In that case, it is safe to avoid the broadcast and simply invalidate the current cpu.
So let's track in
mm_context_t::active_cpueither the mm has never been active on any cpu, has been active on more than 1 cpu, or has been active on precisely 1 cpu - and in that case, which one. We update this when switching context, being careful to ensure that it gets updated *before* installing the mm's pgtables. On the reader side, we ensure we read *after* the previous write(s) to the pgtable(s) that necessitated the tlb flush have completed. This guarrantees that if a cpu that is doing a tlb flush sees it's own id inactive_cpu, then the old pgtable entry cannot have been seen by any other cpu and we can flush only the local cpu.Signed-off-by Ryan Roberts <[email protected]>--- arch/arm64/include/asm/mmu.h | 12 ++++ arch/arm64/include/asm/mmu_context.h | 2 + arch/arm64/include/asm/tlbflush.h | 90 +++++++++++++++++++++++++--- arch/arm64/mm/context.c | 30 +++++++++- 4 files changed, 123 insertions(+), 11 deletions(-)arch/arm64/include/asm/mmu.h +10
-0@@ -17,6 +17,17 @@#include <linux/refcount.h> #include <asm/cpufeature.h> +/* +* Sentinal values for mm_context_t::active_cpu. ACTIVE_CPU_NONE indicates the +* mm has never been active on any CPU. ACTIVE_CPU_MULTIPLE indicates the mm +* has been active on multiple CPUs. Any other value is the ID of the single +* CPU that the mm has been active on. +*/ +enum active_cpu { + ACTIVE_CPU_NONE = UINT_MAX, + ACTIVE_CPU_MULTIPLE = UINT_MAX - 1, +}; + typedef struct { atomic64_t id; #ifdef CONFIG_COMPATarch/arm64/include/asm/mmu.h typedef struct { · line 40
Quoted 4 lines
arch/arm64/include/asm/mmu.h +1
-0@@ -26,6 +37,7 @@ typedef struct {void *vdso;unsigned long flags;u8 pkey_allocation_map; +unsigned int active_cpu;Any reason why you don't use bit mask to mark the active CPUs? And
mm_structalso hascpu_bitmapto record the active CPUs which the process has run on. Why not just use it? x86 uses it to determine
Quoted 8 lines
which CPUs kernel should send TLB flush IPI to. I understand this series just check whether local cpu is the active cpu or not, but bit mask should not make things more complicated. And it also can provide more flexibility. We can extend this, for example, use IPI to send local TLB flush if the number of active cpus is quite low. AFAIK, x86 added TLBI broadcast support too, and fallback to IPI if the number of active cpus is <= 3. IIRC, Christohper's series did the similar thing. He should be interested in this series too, cc'ed him.
Agree! One possible disadvantage of this series is that the benefit will gone after the process is migrated to another CPU. This is quite common if the process isn't bound to a CPU on a system without many CPUs. A cpumask will be helpful for this situation.
Quoted 3 lines
arch/arm64/include/asm/mmu.h typedef struct { · line 43
Quoted 3 lines
arch/arm64/include/asm/mmu.h +0
-0} mm_context_t;/*
[snip]
---
Best Regards,
Huang, Ying
Re: [RFC PATCH v1 2/2] arm64: tlbflush: Don't broadcast if mm was only active on local cpu
In reply to Yang ShiQuoted 55 lines · Yang Shi
Hi Ryan,
Quoted 51 lines · Ryan Roberts
There are 3 variants of tlb flush that invalidate user mappings:
flush_tlb_mm(),flush_tlb_page()and__flush_tlb_range(). All of these would previously unconditionally broadcast their tlbis to all cpus in the inner shareable domain.But this is a waste of effort if we can prove that the mm for which we are flushing the mappings has only ever been active on the local cpu. In that case, it is safe to avoid the broadcast and simply invalidate the current cpu.
So let's track in
mm_context_t::active_cpueither the mm has never been active on any cpu, has been active on more than 1 cpu, or has been active on precisely 1 cpu - and in that case, which one. We update this when switching context, being careful to ensure that it gets updated *before* installing the mm's pgtables. On the reader side, we ensure we read *after* the previous write(s) to the pgtable(s) that necessitated the tlb flush have completed. This guarrantees that if a cpu that is doing a tlb flush sees it's own id inactive_cpu, then the old pgtable entry cannot have been seen by any other cpu and we can flush only the local cpu.Signed-off-by Ryan Roberts <[email protected]>---
arch/arm64/include/asm/mmu.h | 12 ++++arch/arm64/include/asm/mmu_context.h| 2 + arch/arm64/include/asm/tlbflush.h | 90 +++++++++++++++++++++++++--- arch/arm64/mm/context.c | 30 +++++++++-4 files changed, 123 insertions(+), 11 deletions(-)arch/arm64/include/asm/mmu.h +0
-0@@ -17,6 +17,17 @@ #include <linux/refcount.h> #include <asm/cpufeature.h>+/*
+ * Sentinal values formm_context_t::active_cpu.ACTIVE_CPU_NONEindicates the
+ * mm has never been active on any CPU.ACTIVE_CPU_MULTIPLEindicates the mm
+ * has been active on multiple CPUs. Any other value is the ID of the single
+ * CPU that the mm has been active on.
+ */arch/arm64/include/asm/mmu.h +5
-0+enum active_cpu { + ACTIVE_CPU_NONE = UINT_MAX, + ACTIVE_CPU_MULTIPLE = UINT_MAX - 1, +}; +typedef struct {
atomic64_t id;
#ifdefCONFIG_COMPATarch/arm64/include/asm/mmu.h +0
-0@@ -26,6 +37,7 @@ typedef struct {
Quoted 8 lines
arch/arm64/include/asm/mmu.h typedef struct { · line 40
Quoted 4 lines
arch/arm64/include/asm/mmu.h +1
-0void *vdso; unsigned long flags; u8 pkey_allocation_map; + unsigned int active_cpu;Any reason why you don't use bit mask to mark the active CPUs? And
mm_structalso hascpu_bitmapto record the active CPUs which the process has run on. Why not just use it?
An earlier draft of this patch used that approach. But that version returned some performance regressions. I then re-wrote the whole thing as tight as I could, including getting rid of the use of the cpu mask; I figured that there are (usually?) 512 bits in a mask, so that's 8 loads to figure out if the thread has run on more than 1 CPU. Whereas my approach is 1 load.
Anyway, I don't have hard evidence that it is actually the cpu mask that was causing the slow downs. I made other changes too. I'll revisit this and do more testing. I agree it would be good to use the cpu mask if we can. (or don't bother allocating it if we can't!).
Quoted 8 lines
x86 uses it to determine which CPUs kernel should send TLB
flush IPI to. I understand this series just check whether local cpu is the active cpu or not, but bit mask should not make things more complicated. And it also can provide more flexibility. We can extend this, for example, use IPI to send local TLB flush if the number of active cpus is quite low. AFAIK, x86 added TLBI broadcast support too, and fallback to IPI if the number of active cpus is <= 3. IIRC, Christohper's series did the similar thing. He should be interested in this series too, cc'ed him.
That definitely feels like a separate step, if we even want to go in that direction.
Quoted 218 lines
Thanks,
YangQuoted 213 lines
arch/arm64/include/asm/mmu.h +0
-0} mm_context_t; /*arch/arm64/include/asm/ renamed from arch/arm64/include/asm/mmu_context.h +0
-0
mmu_context.h
index0dbe3b29049b..db2505a8eba8100644arch/arm64/include/asm/mmu_context.h +1
-0@@ -180,6 +180,8 @@ init_new_context(struct task_struct *tsk, struct mm_struct *mm) /* pkey 0 is the default, so always reserve it. */ mm->context.pkey_allocation_map = BIT(0); + WRITE_ONCE(mm->context.active_cpu, ACTIVE_CPU_NONE); + return 0; }diff --git
a/arch/arm64/include/asm/tlbflush.hb/arch/arm64/include/asm/tlbflush.h
indexf66b8c4696d0..651440e0aff9100644arch/arm64/include/asm/tlbflush.h +0
-0@@ -182,6 +182,52 @@ static inline unsigned long get_trans_granule(void) (__pages >> (5 * (scale) + 1)) - 1; \ })+/*
+ * Determines whether the user tlbi invalidation can be performed only on the
+ * local CPU or whether it needs to be broadcast. (Returns true for local).
+ * Additionally issues appropriate barrier to ensure prior pgtable updates are
+ * visible to the table walker. Must be paired withflush_tlb_user_post().
+ */arch/arm64/include/asm/tlbflush.h +40
-0+static inline bool flush_tlb_user_pre (struct mm_struct *mm) +{ + unsigned int self, active; + bool local; + + migrate_disable(); + + self = smp_processor_id(); + + /* + * The load of mm->context.active_cpu must not be reordered before the + * store to the pgtable that necessitated this flush. This ensures that + * if the value read is our cpu id, then no other cpu can have seen the + * old pgtable value and therefore does not need this old value to be + * flushed from its tlb. But we don't want to upgrade the dsb(ishst), + * needed to make the pgtable updates visible to the walker, to a + * dsb(ish) by default. So speculatively load without a barrier and if + * it indicates our cpu id, then upgrade the barrier and re-load. + */ + active = READ_ONCE(mm->context.active_cpu); + if (active == self) { + dsb(ish); + active = READ_ONCE(mm->context.active_cpu); + } else { + dsb(ishst); + } + + local = active == self; + if (!local) + migrate_enable(); + + return local; +} + +static inline void flush_tlb_user_post (bool local) +{ + if (local) + migrate_enable(); +} +/*
* TLB Invalidation
* ================arch/arm64/include/asm/tlbflush.h +20
-4@@ -274,11 +320,18 @@ static inline void flush_tlb_all(void) static inline void flush_tlb_mm(struct mm_struct *mm) { unsigned long asid; + bool local; - dsb(ishst); + local = flush_tlb_user_pre(mm); asid = __TLBI_VADDR(0, ASID(mm)); - __tlbi(aside1is, asid); - __tlbi_user(aside1is, asid); + if (local) { + __tlbi(aside1, asid); + __tlbi_user(aside1, asid); + } else { + __tlbi(aside1is, asid); + __tlbi_user(aside1is, asid); + } + flush_tlb_user_post(local); dsb(ish); mmu_notifier_arch_invalidate_secondary_tlbs(mm, 0, -1UL); }@@ -287,11 +340,18 @@ static inline void __flush_tlb_page_nosync(structmm_struct *mm, unsigned long uaddr) { unsigned long addr; + bool local; - dsb(ishst); + local = flush_tlb_user_pre(mm); addr = __TLBI_VADDR(uaddr, ASID(mm)); - __tlbi(vale1is, addr); - __tlbi_user(vale1is, addr); + if (local) { + __tlbi(vale1, addr); + __tlbi_user(vale1, addr); + } else { + __tlbi(vale1is, addr); + __tlbi_user(vale1is, addr); + } + flush_tlb_user_post(local); mmu_notifier_arch_invalidate_secondary_tlbs(mm, uaddr & PAGE_MASK, (uaddr & PAGE_MASK) + PAGE_SIZE);}
arch/arm64/include/asm/tlbflush.h +15
-0@@ -437,6 +497,8 @@ static inline bool __flush_tlb_range_limit_excess(unsigned long start, enum tlbi_op { TLBI_VALE1IS, TLBI_VAE1IS, + TLBI_VALE1, + TLBI_VAE1, }; #define flush_tlb_range_op(op, ...) \@@ -448,6 +510,12 @@ do { \ case TLBI_VAE1IS: \ __flush_tlb_range_op(vae1is, __VA_ARGS__); \ break; \ + case TLBI_VALE1: \ + __flush_tlb_range_op(vale1, __VA_ARGS__); \ + break; \ + case TLBI_VAE1: \ + __flush_tlb_range_op(vae1, __VA_ARGS__); \ + break; \ default: \ BUILD_BUG_ON_MSG(1, "Unknown TLBI op"); \ } \@@ -460,6 +528,7 @@ static inline void __flush_tlb_range_nosync(structmm_struct *mm, { unsigned long asid, pages; enum tlbi_op tlbi_op; + bool local; start = round_down(start, stride); end = round_up(end, stride);@@ -470,13 +539,18 @@ static inline void __flush_tlb_range_nosync(structmm_struct *mm, return; } - dsb(ishst); + local = flush_tlb_user_pre(mm); asid = ASID(mm); - tlbi_op = last_level ? TLBI_VALE1IS : TLBI_VAE1IS; + if (local) + tlbi_op = last_level ? TLBI_VALE1 : TLBI_VAE1; + else + tlbi_op = last_level ? TLBI_VALE1IS : TLBI_VAE1IS; + flush_tlb_range_op(tlbi_op, start, pages, stride, asid, tlb_level, true, lpa2_is_enabled()); + flush_tlb_user_post(local); mmu_notifier_arch_invalidate_secondary_tlbs(mm, start, end); }diff --git
a/arch/arm64/mm/context.cb/arch/arm64/mm/context.cindexb2ac06246327..adf4fc26ddb6100644arch/arm64/mm/context.c +3
-2@@ -214,9 +214,10 @@ static u64 new_context(struct mm_struct *mm) void check_and_switch_context(struct mm_struct *mm) { - unsigned long flags; - unsigned int cpu; + unsigned int cpu = smp_processor_id(); u64 asid, old_active_asid; + unsigned int active; + unsigned long flags; if (system_supports_cnp()) cpu_set_reserved_ttbr0();@@ -251,7 +252,6 @@ void check_and_switch_context(struct mm_struct *mm) atomic64_set(&mm->context.id, asid); } - cpu = smp_processor_id(); if (cpumask_test_and_clear_cpu(cpu, &tlb_flush_pending)) local_flush_tlb_all(); @@ -262,6 +262,30 @@ void check_and_switch_context(struct mm_struct *mm)
arm64_apply_bp_hardening();
+ /*
+ * Updatemm->context.active_cpuin such a manner that we avoid cmpxchg + * and dsb unless we definitely need it. If initiallyACTIVE_CPU_NONE+ * then we are the first cpu to run so set it to our id. If initially + * any id other than ours, we are the second cpu to run so set it to + *ACTIVE_CPU_MULTIPLE. If we update the value then we must issue + * dsb(ishst) to ensure stores tomm->context.active_cpuare ordered + * against the TTBR0 write in cpu_switch_mm()/uaccess_enable(); the + * store must be visible to another cpu before this cpu could have + * populated any TLB entries based on the pgtables that will be + * installed.
+ */
+ active =READ_ONCE(mm->context.active_cpu);
+ if (active != cpu && active !=ACTIVE_CPU_MULTIPLE) {
+ if (active ==ACTIVE_CPU_NONE)
+ active = cmpxchg_relaxed(&mm->context.active_cpu,
+ACTIVE_CPU_NONE, cpu);arch/arm64/mm/context.c +7
-0+ + if (active != ACTIVE_CPU_NONE) + WRITE_ONCE(mm->context.active_cpu, ACTIVE_CPU_MULTIPLE); + + dsb(ishst); + } +/*
* DeferTTBR0_EL1setting for user threads touaccess_enable()when
* emulating PAN.
Re: [RFC PATCH v1 2/2] arm64: tlbflush: Don't broadcast if mm was only active on local cpu
Quoted 63 lines · Ryan Roberts
Quoted 54 lines · Yang Shi
Hi Ryan,
Quoted 50 lines · Ryan Roberts
There are 3 variants of tlb flush that invalidate user mappings:
flush_tlb_mm(),flush_tlb_page()and__flush_tlb_range(). All of these would previously unconditionally broadcast their tlbis to all cpus in the inner shareable domain.But this is a waste of effort if we can prove that the mm for which we are flushing the mappings has only ever been active on the local cpu. In that case, it is safe to avoid the broadcast and simply invalidate the current cpu.
So let's track in
mm_context_t::active_cpueither the mm has never been active on any cpu, has been active on more than 1 cpu, or has been active on precisely 1 cpu - and in that case, which one. We update this when switching context, being careful to ensure that it gets updated *before* installing the mm's pgtables. On the reader side, we ensure we read *after* the previous write(s) to the pgtable(s) that necessitated the tlb flush have completed. This guarrantees that if a cpu that is doing a tlb flush sees it's own id inactive_cpu, then the old pgtable entry cannot have been seen by any other cpu and we can flush only the local cpu.Signed-off-by Ryan Roberts <[email protected]>--- arch/arm64/include/asm/mmu.h | 12 ++++ arch/arm64/include/asm/mmu_context.h | 2 + arch/arm64/include/asm/tlbflush.h | 90 +++++++++++++++++++++++++--- arch/arm64/mm/context.c | 30 +++++++++- 4 files changed, 123 insertions(+), 11 deletions(-)arch/arm64/include/asm/mmu.h +10
-0@@ -17,6 +17,17 @@ #include <linux/refcount.h> #include <asm/cpufeature.h> +/* + * Sentinal values for mm_context_t::active_cpu. ACTIVE_CPU_NONE indicates the + * mm has never been active on any CPU. ACTIVE_CPU_MULTIPLE indicates the mm + * has been active on multiple CPUs. Any other value is the ID of the single + * CPU that the mm has been active on. + */ +enum active_cpu { + ACTIVE_CPU_NONE = UINT_MAX, + ACTIVE_CPU_MULTIPLE = UINT_MAX - 1, +}; + typedef struct { atomic64_t id; #ifdef CONFIG_COMPATQuoted 8 lines
arch/arm64/include/asm/mmu.h typedef struct { · line 40
Quoted 5 lines
arch/arm64/include/asm/mmu.h +1
-0@@ -26,6 +37,7 @@ typedef struct { void *vdso; unsigned long flags; u8 pkey_allocation_map; + unsigned int active_cpu;Any reason why you don't use bit mask to mark the active CPUs? And
mm_structalso hascpu_bitmapto record the active CPUs which the process has run on. Why not just use it?
Quoted 5 lines
An earlier draft of this patch used that approach. But that version returned some performance regressions. I then re-wrote the whole thing as tight as I could, including getting rid of the use of the cpu mask; I figured that there are (usually?) 512 bits in a mask, so that's 8 loads to figure out if the thread has run on more than 1 CPU. Whereas my approach is 1 load.
Yes, mm_cpumask() loads 64 bits each time. But they should fit into the same cache line (64 bytes). Currently mm_struct doesn't force to place cpu_bitmap into a single cache line, it may be in 2 cache lines. But this can be optimized if it is a problem.
Quoted 14 lines
Anyway, I don't have hard evidence that it is actually the cpu mask that was causing the slow downs. I made other changes too. I'll revisit this and do more testing. I agree it would be good to use the cpu mask if we can. (or don't bother allocating it if we can't!).
Quoted 8 lines
x86 uses it to determine which CPUs kernel should send TLB
flush IPI to. I understand this series just check whether local cpu is the active cpu or not, but bit mask should not make things more complicated. And it also can provide more flexibility. We can extend this, for example, use IPI to send local TLB flush if the number of active cpus is quite low. AFAIK, x86 added TLBI broadcast support too, and fallback to IPI if the number of active cpus is <= 3. IIRC, Christohper's series did the similar thing. He should be interested in this series too, cc'ed him.
Quoted 1 line
That definitely feels like a separate step, if we even want to go in that direction.
Yeah, potential future optimization candidate. It may be not worth for small system, but large system with hundreds of cores is a different story.
Thanks,
Yang
Quoted 217 lines
Quoted 217 lines
Thanks,
YangQuoted 213 lines
arch/arm64/include/asm/mmu.h +0
-0} mm_context_t; /*arch/arm64/include/asm/ renamed from arch/arm64/include/asm/mmu_context.h +0
-0
mmu_context.h
index0dbe3b29049b..db2505a8eba8100644arch/arm64/include/asm/mmu_context.h +1
-0@@ -180,6 +180,8 @@ init_new_context(struct task_struct *tsk, struct mm_struct *mm) /* pkey 0 is the default, so always reserve it. */ mm->context.pkey_allocation_map = BIT(0); + WRITE_ONCE(mm->context.active_cpu, ACTIVE_CPU_NONE); + return 0; } diff --git a/arch/arm64/include/asm/tlbflush.h b/arch/arm64/include/asm/
tlbflush.h
indexf66b8c4696d0..651440e0aff9100644arch/arm64/include/asm/tlbflush.h +80
-4@@ -182,6 +182,52 @@ static inline unsigned long get_trans_granule(void) (__pages >> (5 * (scale) + 1)) - 1; \ }) +/* +* Determines whether the user tlbi invalidation can be performed only on the +* local CPU or whether it needs to be broadcast. (Returns true for local). +* Additionally issues appropriate barrier to ensure prior pgtable updates are +* visible to the table walker. Must be paired with flush_tlb_user_post(). +*/ +static inline bool flush_tlb_user_pre(struct mm_struct *mm) +{ + unsigned int self, active; + bool local; + + migrate_disable(); + + self = smp_processor_id(); + + /* + * The load of mm->context.active_cpu must not be reordered before the + * store to the pgtable that necessitated this flush. This ensures that + * if the value read is our cpu id, then no other cpu can have seen the + * old pgtable value and therefore does not need this old value to be + * flushed from its tlb. But we don't want to upgrade the dsb(ishst), + * needed to make the pgtable updates visible to the walker, to a + * dsb(ish) by default. So speculatively load without a barrier and if + * it indicates our cpu id, then upgrade the barrier and re-load. + */ + active = READ_ONCE(mm->context.active_cpu); + if (active == self) { + dsb(ish); + active = READ_ONCE(mm->context.active_cpu); + } else { + dsb(ishst); + } + + local = active == self; + if (!local) + migrate_enable(); + + return local; +} + +static inline void flush_tlb_user_post (bool local) +{ + if (local) + migrate_enable(); +} + /** TLB Invalidation * ================ @@ -274,11 +320,18 @@ static inline void flush_tlb_all(void) static inline void flush_tlb_mm(struct mm_struct *mm) { unsigned long asid; + bool local; - dsb(ishst); + local = flush_tlb_user_pre(mm); asid = __TLBI_VADDR(0, ASID(mm)); - __tlbi(aside1is, asid); - __tlbi_user(aside1is, asid); + if (local) { + __tlbi(aside1, asid); + __tlbi_user(aside1, asid); + } else { + __tlbi(aside1is, asid); + __tlbi_user(aside1is, asid); + } + flush_tlb_user_post(local); dsb(ish); mmu_notifier_arch_invalidate_secondary_tlbs(mm, 0, -1UL); }@@ -287,11 +340,18 @@ static inline void __flush_tlb_page_nosync(structmm_struct *mm, unsigned long uaddr) { unsigned long addr; + bool local; - dsb(ishst); + local = flush_tlb_user_pre(mm); addr = __TLBI_VADDR(uaddr, ASID(mm)); - __tlbi(vale1is, addr); - __tlbi_user(vale1is, addr); + if (local) { + __tlbi(vale1, addr); + __tlbi_user(vale1, addr); + } else { + __tlbi(vale1is, addr); + __tlbi_user(vale1is, addr); + } + flush_tlb_user_post(local); mmu_notifier_arch_invalidate_secondary_tlbs(mm, uaddr & PAGE_MASK, (uaddr & PAGE_MASK) + PAGE_SIZE); }@@ -437,6 +497,8 @@ static inline bool __flush_tlb_range_limit_excess(unsigned long start, enum tlbi_op { TLBI_VALE1IS, TLBI_VAE1IS, + TLBI_VALE1, + TLBI_VAE1, }; #define flush_tlb_range_op(op, ...) \@@ -448,6 +510,12 @@ do { \ case TLBI_VAE1IS: \ __flush_tlb_range_op(vae1is, __VA_ARGS__); \ break; \ + case TLBI_VALE1: \ + __flush_tlb_range_op(vale1, __VA_ARGS__); \ + break; \ + case TLBI_VAE1: \ + __flush_tlb_range_op(vae1, __VA_ARGS__); \ + break; \ default: \ BUILD_BUG_ON_MSG(1, "Unknown TLBI op"); \ } \@@ -460,6 +528,7 @@ static inline void __flush_tlb_range_nosync(structmm_struct *mm, { unsigned long asid, pages; enum tlbi_op tlbi_op; + bool local; start = round_down(start, stride); end = round_up(end, stride);@@ -470,13 +539,18 @@ static inline void __flush_tlb_range_nosync(structmm_struct *mm, return; } - dsb(ishst); + local = flush_tlb_user_pre(mm); asid = ASID(mm); - tlbi_op = last_level ? TLBI_VALE1IS : TLBI_VAE1IS; + if (local) + tlbi_op = last_level ? TLBI_VALE1 : TLBI_VAE1; + else + tlbi_op = last_level ? TLBI_VALE1IS : TLBI_VAE1IS; + flush_tlb_range_op(tlbi_op, start, pages, stride, asid, tlb_level, true, lpa2_is_enabled()); + flush_tlb_user_post(local); mmu_notifier_arch_invalidate_secondary_tlbs(mm, start, end); } diff --git a/arch/arm64/mm/context.c b/arch/arm64/mm/context.cindex
b2ac06246327..adf4fc26ddb6100644arch/arm64/mm/context.c +26
-2@@ -214,9 +214,10 @@ static u64 new_context(struct mm_struct *mm) void check_and_switch_context(struct mm_struct *mm) { - unsigned long flags; - unsigned int cpu; + unsigned int cpu = smp_processor_id(); u64 asid, old_active_asid; + unsigned int active; + unsigned long flags; if (system_supports_cnp()) cpu_set_reserved_ttbr0();@@ -251,7 +252,6 @@ void check_and_switch_context(struct mm_struct *mm) atomic64_set(&mm->context.id, asid); } - cpu = smp_processor_id(); if (cpumask_test_and_clear_cpu(cpu, &tlb_flush_pending)) local_flush_tlb_all(); @@ -262,6 +262,30 @@ void check_and_switch_context(struct mm_struct *mm) arm64_apply_bp_hardening(); + /* + * Update mm->context.active_cpu in such a manner that we avoid cmpxchg + * and dsb unless we definitely need it. If initially ACTIVE_CPU_NONE + * then we are the first cpu to run so set it to our id. If initially + * any id other than ours, we are the second cpu to run so set it to + * ACTIVE_CPU_MULTIPLE. If we update the value then we must issue + * dsb(ishst) to ensure stores to mm->context.active_cpu are ordered + * against the TTBR0 write in cpu_switch_mm()/uaccess_enable(); the + * store must be visible to another cpu before this cpu could have + * populated any TLB entries based on the pgtables that will be + * installed. + */ + active = READ_ONCE(mm->context.active_cpu); + if (active != cpu && active != ACTIVE_CPU_MULTIPLE) { + if (active == ACTIVE_CPU_NONE) + active = cmpxchg_relaxed(&mm->context.active_cpu, + ACTIVE_CPU_NONE, cpu); + + if (active != ACTIVE_CPU_NONE) + WRITE_ONCE(mm->context.active_cpu, ACTIVE_CPU_MULTIPLE); + + dsb(ishst); + } + /** Defer TTBR0_EL1 setting for user threads to uaccess_enable() when * emulating PAN.
Catalin Marinas
maintainerRe: [RFC PATCH v1 2/2] arm64: tlbflush: Don't broadcast if mm was only active on local cpu
In reply to Ryan RobertsQuoted 42 lines · Ryan Roberts
arch/arm64/mm/context.c +16
-3@@ -214,9 +214,10 @@ static u64 new_context(struct mm_struct *mm)void check_and_switch_context (struct mm_struct *mm) { -unsigned long flags; -unsigned int cpu; +unsigned int cpu =smp_processor_id ();u64 asid, old_active_asid; +unsigned int active; +unsigned long flags;if (system_supports_cnp ())cpu_set_reserved_ttbr0 ();@@ -251,7 +252,6 @@ void check_and_switch_context(struct mm_struct *mm)atomic64_set (&mm->context.id, asid); } - cpu =smp_processor_id ();if (cpumask_test_and_clear_cpu (cpu, &tlb_flush_pending))local_flush_tlb_all ();@@ -262,6 +262,30 @@ void check_and_switch_context(struct mm_struct *mm)arm64_apply_bp_hardening (); +/* +* Update mm->context.active_cpu in such a manner that we avoid cmpxchg +* and dsb unless we definitely need it. If initially ACTIVE_CPU_NONE +* then we are the first cpu to run so set it to our id. If initially +* any id other than ours, we are the second cpu to run so set it to +* ACTIVE_CPU_MULTIPLE. If we update the value then we must issue +* dsb(ishst) to ensure stores to mm->context.active_cpu are ordered +* against the TTBR0 write in cpu_switch_mm()/uaccess_enable(); the +* store must be visible to another cpu before this cpu could have +* populated any TLB entries based on the pgtables that will be +* installed. +*/ + active =READ_ONCE (mm->context.active_cpu);
arch/arm64/mm/context.c void check_and_switch_context(struct mm_struct *mm) · line 287
Quoted 10 lines
arch/arm64/mm/context.c +10
-0+if (active != cpu && active !=ACTIVE_CPU_MULTIPLE ) { +if (active ==ACTIVE_CPU_NONE ) + active =cmpxchg_relaxed (&mm->context.active_cpu, +ACTIVE_CPU_NONE , cpu); + +if (active !=ACTIVE_CPU_NONE ) +WRITE_ONCE (mm->context.active_cpu,ACTIVE_CPU_MULTIPLE ); + +dsb (ishst); + }
I realised when we talked earlier that we can reset active_cpu on the slow path if we got a new ASID. I think it's best to do it in new_context().
The fork() case where the child may unnecessarily inherit the parent's active_cpu is already handled by init_new_context() in this patch.
Signature · 1 line
Catalin
Catalin Marinas
maintainerQuoted 20 lines · Ryan Roberts
Beyond that, the next question is; does it actually improve performance? stress-ng's --tlb-shootdown stressor suggests yes; as concurrency increases, we do a much better job of sustaining the overall number of "tlb shootdowns per second" after the change:
+------------+--------------------------+--------------------------+--------------------------+ | | Baseline (v6.15) | tlbi local | Improvement | +------------+-------------+------------+-------------+------------+-------------+------------+ | nr_threads | ops/sec | ops/sec | ops/sec | ops/sec | ops/sec | ops/sec | | | (real time) | (cpu time) | (real time) | (cpu time) | (real time) | (cpu time) | +------------+-------------+------------+-------------+------------+-------------+------------+ | 1 | 9109 | 2573 | 8903 | 3653 | -2% | 42% | | 4 | 8115 | 1299 | 9892 | 1059 | 22% | -18% | | 8 | 5119 | 477 | 11854 | 1265 | 132% | 165% | | 16 | 4796 | 286 | 14176 | 821 | 196% | 187% | | 32 | 1593 | 38 | 15328 | 474 | 862% | 1147% | | 64 | 1486 | 19 | 8096 | 131 | 445% | 589% | | 128 | 1315 | 16 | 8257 | 145 | 528% | 806% | +------------+-------------+------------+-------------+------------+-------------+------------+
Quoted 4 lines
But looking at real-world benchmarks, I haven't yet found anything where it makes a huge difference; When compiling the kernel, it reduces kernel time by ~2.2%, but overall wall time remains the same. I'd be interested in any suggestions for workloads where this might prove valuable.
I suspect it's highly dependent on hardware and how it handles the DVM messages. There were some old proposals from Fujitsu:
https://lore.kernel.org/linux-arm-kernel/[email protected]/
Christoph Lameter (Ampere) also followed with some refactoring in this area to allow a boot-configurable way to do TLBI via IS ops or IPI:
https://lore.kernel.org/linux-arm-kernel/[email protected]/
(for some reason, the patches did not make it to the list, I have them in my inbox if you are interested)
I don't remember any real-world workload, more like hand-crafted mprotect() loops.
Anyway, I think the approach in your series doesn't have downsides, it's fairly clean and addresses some low-hanging fruits. For multi-threaded workloads where a flush_tlb_mm() is cheaper than a series of per-page TLBIs, I think we can wait for that hardware to be phased out. The TLBI range operations should significantly reduce the DVM messages between CPUs.
Signature · 1 line
Catalin
Quoted 42 lines · Catalin Marinas
Quoted 20 lines · Ryan Roberts
Beyond that, the next question is; does it actually improve performance? stress-ng's --tlb-shootdown stressor suggests yes; as concurrency increases, we do a much better job of sustaining the overall number of "tlb shootdowns per second" after the change:
+------------+--------------------------+--------------------------+--------------------------+ | | Baseline (v6.15) | tlbi local | Improvement | +------------+-------------+------------+-------------+------------+-------------+------------+ | nr_threads | ops/sec | ops/sec | ops/sec | ops/sec | ops/sec | ops/sec | | | (real time) | (cpu time) | (real time) | (cpu time) | (real time) | (cpu time) | +------------+-------------+------------+-------------+------------+-------------+------------+ | 1 | 9109 | 2573 | 8903 | 3653 | -2% | 42% | | 4 | 8115 | 1299 | 9892 | 1059 | 22% | -18% | | 8 | 5119 | 477 | 11854 | 1265 | 132% | 165% | | 16 | 4796 | 286 | 14176 | 821 | 196% | 187% | | 32 | 1593 | 38 | 15328 | 474 | 862% | 1147% | | 64 | 1486 | 19 | 8096 | 131 | 445% | 589% | | 128 | 1315 | 16 | 8257 | 145 | 528% | 806% | +------------+-------------+------------+-------------+------------+-------------+------------+Quoted 4 lines
But looking at real-world benchmarks, I haven't yet found anything where it makes a huge difference; When compiling the kernel, it reduces kernel time by ~2.2%, but overall wall time remains the same. I'd be interested in any suggestions for workloads where this might prove valuable.
I suspect it's highly dependent on hardware and how it handles the DVM messages. There were some old proposals from Fujitsu:
https://lore.kernel.org/linux-arm-kernel/[email protected]/
Christoph Lameter (Ampere) also followed with some refactoring in this area to allow a boot-configurable way to do TLBI via IS ops or IPI:
https://lore.kernel.org/linux-arm-kernel/[email protected]/
(for some reason, the patches did not make it to the list, I have them in my inbox if you are interested)
I don't remember any real-world workload, more like hand-crafted
mprotect()loops.
Quoted 6 lines
Anyway, I think the approach in your series doesn't have downsides, it's fairly clean and addresses some low-hanging fruits. For multi-threaded workloads where a
flush_tlb_mm()is cheaper than a series of per-page TLBIs, I think we can wait for that hardware to be phased out. The TLBI range operations should significantly reduce the DVM messages between CPUs.
I'll gather some more numbers and try to make a case for merging it then. I don't really want to add complexity if there is no clear value.
Thanks for the review.
Thanks,
Ryan
Quoted 7 lines · Catalin Marinas
Christoph Lameter (Ampere) also followed with some refactoring in this area to allow a boot-configurable way to do TLBI via IS ops or IPI:
https://lore.kernel.org/linux-arm-kernel/[email protected]/
(for some reason, the patches did not make it to the list, I have them in my inbox if you are interested)
The patchset is available from kernel.org. Sorry about the mailing list problems at that time.
https://git.kernel.org/pub/scm/linux/kernel/git/christoph/linux.git/log/?h=tlb
Hi, Ryan,
Quoted 31 lines · Ryan Roberts
Hi All,
This is an RFC for my implementation of an idea from James Morse to avoid broadcasting TBLIs to remote CPUs if it can be proven that no remote CPU could have ever observed the pgtable entry for the TLB entry that is being invalidated. It turns out that x86 does something similar in principle.
The primary feedback I'm looking for is; is this actually correct and safe? James and I both believe it to be, but it would be useful to get further validation.
Beyond that, the next question is; does it actually improve performance? stress-ng's --tlb-shootdown stressor suggests yes; as concurrency increases, we do a much better job of sustaining the overall number of "tlb shootdowns per second" after the change:
+------------+--------------------------+--------------------------+--------------------------+ | | Baseline (v6.15) | tlbi local | Improvement | +------------+-------------+------------+-------------+------------+-------------+------------+ | nr_threads | ops/sec | ops/sec | ops/sec | ops/sec | ops/sec | ops/sec | | | (real time) | (cpu time) | (real time) | (cpu time) | (real time) | (cpu time) | +------------+-------------+------------+-------------+------------+-------------+------------+ | 1 | 9109 | 2573 | 8903 | 3653 | -2% | 42% | | 4 | 8115 | 1299 | 9892 | 1059 | 22% | -18% | | 8 | 5119 | 477 | 11854 | 1265 | 132% | 165% | | 16 | 4796 | 286 | 14176 | 821 | 196% | 187% | | 32 | 1593 | 38 | 15328 | 474 | 862% | 1147% | | 64 | 1486 | 19 | 8096 | 131 | 445% | 589% | | 128 | 1315 | 16 | 8257 | 145 | 528% | 806% | +------------+-------------+------------+-------------+------------+-------------+------------+
Quoted 7 lines
But looking at real-world benchmarks, I haven't yet found anything where it makes a huge difference; When compiling the kernel, it reduces kernel time by ~2.2%, but overall wall time remains the same. I'd be interested in any suggestions for workloads where this might prove valuable.
All mm selftests have been run and no regressions are observed. Applies on v6.17-rc3.
Thanks for working on this.
Several previous TLBI broadcast optimization have been tried before, Cced the original authors for discussion. Some workloads show good improvement,
https://lore.kernel.org/lkml/[email protected]/
https://lore.kernel.org/all/[email protected]/
Especially in the following mail,
https://lore.kernel.org/all/[email protected]/
---
Best Regards,
Huang, Ying
Quoted 1 line · Ying
This patch is part of the Redhat 8 ARM kernels.
Quoted 31 lines · Ryan Roberts
Hi All,
This is an RFC for my implementation of an idea from James Morse to avoid broadcasting TBLIs to remote CPUs if it can be proven that no remote CPU could have ever observed the pgtable entry for the TLB entry that is being invalidated. It turns out that x86 does something similar in principle.
The primary feedback I'm looking for is; is this actually correct and safe? James and I both believe it to be, but it would be useful to get further validation.
Beyond that, the next question is; does it actually improve performance? stress-ng's --tlb-shootdown stressor suggests yes; as concurrency increases, we do a much better job of sustaining the overall number of "tlb shootdowns per second" after the change:
+------------+--------------------------+--------------------------+--------------------------+ | | Baseline (v6.15) | tlbi local | Improvement | +------------+-------------+------------+-------------+------------+-------------+------------+ | nr_threads | ops/sec | ops/sec | ops/sec | ops/sec | ops/sec | ops/sec | | | (real time) | (cpu time) | (real time) | (cpu time) | (real time) | (cpu time) | +------------+-------------+------------+-------------+------------+-------------+------------+ | 1 | 9109 | 2573 | 8903 | 3653 | -2% | 42% | | 4 | 8115 | 1299 | 9892 | 1059 | 22% | -18% | | 8 | 5119 | 477 | 11854 | 1265 | 132% | 165% | | 16 | 4796 | 286 | 14176 | 821 | 196% | 187% | | 32 | 1593 | 38 | 15328 | 474 | 862% | 1147% | | 64 | 1486 | 19 | 8096 | 131 | 445% | 589% | | 128 | 1315 | 16 | 8257 | 145 | 528% | 806% | +------------+-------------+------------+-------------+------------+-------------+------------+
Quoted 7 lines
But looking at real-world benchmarks, I haven't yet found anything where it makes a huge difference; When compiling the kernel, it reduces kernel time by ~2.2%, but overall wall time remains the same. I'd be interested in any suggestions for workloads where this might prove valuable.
All mm selftests have been run and no regressions are observed. Applies on v6.17-rc3.
I have used redis (a single threaded in-memory database) to test the patchset on an ARM server. 32 redis-server processes are run on the NUMA node 1 to enlarge the overhead of TLBI broadcast. 32 memtier-benchmark processes are run on the NUMA node 0 accordingly. Snapshot is triggered constantly in redis-server, which fork(), saves memory database to disk, exit(), so that COW in the redis-server will trigger a large amount of TLBI. Basically, this tests the performance of redis-server during snapshot. The test time is about 300s. Test results show that the benchmark score can improve ~4.5% with the patchset.
Feel free to add my
in the future versions.
---
Best Regards,
Huang, Ying
Quoted 52 lines · Ying
Quoted 31 lines · Ryan Roberts
Hi All,
This is an RFC for my implementation of an idea from James Morse to avoid broadcasting TBLIs to remote CPUs if it can be proven that no remote CPU could have ever observed the pgtable entry for the TLB entry that is being invalidated. It turns out that x86 does something similar in principle.
The primary feedback I'm looking for is; is this actually correct and safe? James and I both believe it to be, but it would be useful to get further validation.
Beyond that, the next question is; does it actually improve performance? stress-ng's --tlb-shootdown stressor suggests yes; as concurrency increases, we do a much better job of sustaining the overall number of "tlb shootdowns per second" after the change:
+------------+--------------------------+--------------------------+--------------------------+ | | Baseline (v6.15) | tlbi local | Improvement | +------------+-------------+------------+-------------+------------+-------------+------------+ | nr_threads | ops/sec | ops/sec | ops/sec | ops/sec | ops/sec | ops/sec | | | (real time) | (cpu time) | (real time) | (cpu time) | (real time) | (cpu time) | +------------+-------------+------------+-------------+------------+-------------+------------+ | 1 | 9109 | 2573 | 8903 | 3653 | -2% | 42% | | 4 | 8115 | 1299 | 9892 | 1059 | 22% | -18% | | 8 | 5119 | 477 | 11854 | 1265 | 132% | 165% | | 16 | 4796 | 286 | 14176 | 821 | 196% | 187% | | 32 | 1593 | 38 | 15328 | 474 | 862% | 1147% | | 64 | 1486 | 19 | 8096 | 131 | 445% | 589% | | 128 | 1315 | 16 | 8257 | 145 | 528% | 806% | +------------+-------------+------------+-------------+------------+-------------+------------+Quoted 7 lines
But looking at real-world benchmarks, I haven't yet found anything where it makes a huge difference; When compiling the kernel, it reduces kernel time by ~2.2%, but overall wall time remains the same. I'd be interested in any suggestions for workloads where this might prove valuable.
All mm selftests have been run and no regressions are observed. Applies on v6.17-rc3.
I have used redis (a single threaded in-memory database) to test the patchset on an ARM server. 32 redis-server processes are run on the NUMA node 1 to enlarge the overhead of TLBI broadcast. 32 memtier-benchmark processes are run on the NUMA node 0 accordingly. Snapshot is triggered constantly in redis-server, which
fork(), saves memory database to disk,exit(), so that COW in the redis-server will trigger a large amount of TLBI. Basically, this tests the performance of redis-server during snapshot. The test time is about 300s. Test results show that the benchmark score can improve ~4.5% with the patchset.
Quoted 5 lines
Feel free to add my
Tested-by Huang Ying <[email protected]>in the future versions.
Thanks for this - very useful!
Quoted 4 lines
---
Best Regards,
Huang, Ying