Skip to content

Commit 2f4305b

Browse files
anadavIngo Molnar
authored andcommitted
x86/mm/tlb: Privatize cpu_tlbstate
cpu_tlbstate is mostly private and only the variable is_lazy is shared. This causes some false-sharing when TLB flushes are performed. Break cpu_tlbstate intro cpu_tlbstate and cpu_tlbstate_shared, and mark each one accordingly. Signed-off-by: Nadav Amit <[email protected]> Signed-off-by: Ingo Molnar <[email protected]> Reviewed-by: Dave Hansen <[email protected]> Link: https://lore.kernel.org/r/[email protected]
1 parent 4ce94ea commit 2f4305b

File tree

4 files changed

+33
-27
lines changed

4 files changed

+33
-27
lines changed

arch/x86/include/asm/tlbflush.h

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -89,23 +89,6 @@ struct tlb_state {
8989
u16 loaded_mm_asid;
9090
u16 next_asid;
9191

92-
/*
93-
* We can be in one of several states:
94-
*
95-
* - Actively using an mm. Our CPU's bit will be set in
96-
* mm_cpumask(loaded_mm) and is_lazy == false;
97-
*
98-
* - Not using a real mm. loaded_mm == &init_mm. Our CPU's bit
99-
* will not be set in mm_cpumask(&init_mm) and is_lazy == false.
100-
*
101-
* - Lazily using a real mm. loaded_mm != &init_mm, our bit
102-
* is set in mm_cpumask(loaded_mm), but is_lazy == true.
103-
* We're heuristically guessing that the CR3 load we
104-
* skipped more than makes up for the overhead added by
105-
* lazy mode.
106-
*/
107-
bool is_lazy;
108-
10992
/*
11093
* If set we changed the page tables in such a way that we
11194
* needed an invalidation of all contexts (aka. PCIDs / ASIDs).
@@ -151,7 +134,27 @@ struct tlb_state {
151134
*/
152135
struct tlb_context ctxs[TLB_NR_DYN_ASIDS];
153136
};
154-
DECLARE_PER_CPU_SHARED_ALIGNED(struct tlb_state, cpu_tlbstate);
137+
DECLARE_PER_CPU_ALIGNED(struct tlb_state, cpu_tlbstate);
138+
139+
struct tlb_state_shared {
140+
/*
141+
* We can be in one of several states:
142+
*
143+
* - Actively using an mm. Our CPU's bit will be set in
144+
* mm_cpumask(loaded_mm) and is_lazy == false;
145+
*
146+
* - Not using a real mm. loaded_mm == &init_mm. Our CPU's bit
147+
* will not be set in mm_cpumask(&init_mm) and is_lazy == false.
148+
*
149+
* - Lazily using a real mm. loaded_mm != &init_mm, our bit
150+
* is set in mm_cpumask(loaded_mm), but is_lazy == true.
151+
* We're heuristically guessing that the CR3 load we
152+
* skipped more than makes up for the overhead added by
153+
* lazy mode.
154+
*/
155+
bool is_lazy;
156+
};
157+
DECLARE_PER_CPU_SHARED_ALIGNED(struct tlb_state_shared, cpu_tlbstate_shared);
155158

156159
bool nmi_uaccess_okay(void);
157160
#define nmi_uaccess_okay nmi_uaccess_okay

arch/x86/kernel/alternative.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -813,7 +813,7 @@ static inline temp_mm_state_t use_temporary_mm(struct mm_struct *mm)
813813
* with a stale address space WITHOUT being in lazy mode after
814814
* restoring the previous mm.
815815
*/
816-
if (this_cpu_read(cpu_tlbstate.is_lazy))
816+
if (this_cpu_read(cpu_tlbstate_shared.is_lazy))
817817
leave_mm(smp_processor_id());
818818

819819
temp_state.mm = this_cpu_read(cpu_tlbstate.loaded_mm);

arch/x86/mm/init.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1017,7 +1017,7 @@ void __init zone_sizes_init(void)
10171017
free_area_init(max_zone_pfns);
10181018
}
10191019

1020-
__visible DEFINE_PER_CPU_SHARED_ALIGNED(struct tlb_state, cpu_tlbstate) = {
1020+
__visible DEFINE_PER_CPU_ALIGNED(struct tlb_state, cpu_tlbstate) = {
10211021
.loaded_mm = &init_mm,
10221022
.next_asid = 1,
10231023
.cr4 = ~0UL, /* fail hard if we screw up cr4 shadow initialization */

arch/x86/mm/tlb.c

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ void leave_mm(int cpu)
300300
return;
301301

302302
/* Warn if we're not lazy. */
303-
WARN_ON(!this_cpu_read(cpu_tlbstate.is_lazy));
303+
WARN_ON(!this_cpu_read(cpu_tlbstate_shared.is_lazy));
304304

305305
switch_mm(NULL, &init_mm, NULL);
306306
}
@@ -424,7 +424,7 @@ void switch_mm_irqs_off(struct mm_struct *prev, struct mm_struct *next,
424424
{
425425
struct mm_struct *real_prev = this_cpu_read(cpu_tlbstate.loaded_mm);
426426
u16 prev_asid = this_cpu_read(cpu_tlbstate.loaded_mm_asid);
427-
bool was_lazy = this_cpu_read(cpu_tlbstate.is_lazy);
427+
bool was_lazy = this_cpu_read(cpu_tlbstate_shared.is_lazy);
428428
unsigned cpu = smp_processor_id();
429429
u64 next_tlb_gen;
430430
bool need_flush;
@@ -469,7 +469,7 @@ void switch_mm_irqs_off(struct mm_struct *prev, struct mm_struct *next,
469469
__flush_tlb_all();
470470
}
471471
#endif
472-
this_cpu_write(cpu_tlbstate.is_lazy, false);
472+
this_cpu_write(cpu_tlbstate_shared.is_lazy, false);
473473

474474
/*
475475
* The membarrier system call requires a full memory barrier and
@@ -490,7 +490,7 @@ void switch_mm_irqs_off(struct mm_struct *prev, struct mm_struct *next,
490490
/*
491491
* Even in lazy TLB mode, the CPU should stay set in the
492492
* mm_cpumask. The TLB shootdown code can figure out from
493-
* cpu_tlbstate.is_lazy whether or not to send an IPI.
493+
* cpu_tlbstate_shared.is_lazy whether or not to send an IPI.
494494
*/
495495
if (WARN_ON_ONCE(real_prev != &init_mm &&
496496
!cpumask_test_cpu(cpu, mm_cpumask(next))))
@@ -598,7 +598,7 @@ void enter_lazy_tlb(struct mm_struct *mm, struct task_struct *tsk)
598598
if (this_cpu_read(cpu_tlbstate.loaded_mm) == &init_mm)
599599
return;
600600

601-
this_cpu_write(cpu_tlbstate.is_lazy, true);
601+
this_cpu_write(cpu_tlbstate_shared.is_lazy, true);
602602
}
603603

604604
/*
@@ -690,7 +690,7 @@ static void flush_tlb_func(void *info)
690690
VM_WARN_ON(this_cpu_read(cpu_tlbstate.ctxs[loaded_mm_asid].ctx_id) !=
691691
loaded_mm->context.ctx_id);
692692

693-
if (this_cpu_read(cpu_tlbstate.is_lazy)) {
693+
if (this_cpu_read(cpu_tlbstate_shared.is_lazy)) {
694694
/*
695695
* We're in lazy mode. We need to at least flush our
696696
* paging-structure cache to avoid speculatively reading
@@ -790,11 +790,14 @@ static void flush_tlb_func(void *info)
790790

791791
static bool tlb_is_not_lazy(int cpu)
792792
{
793-
return !per_cpu(cpu_tlbstate.is_lazy, cpu);
793+
return !per_cpu(cpu_tlbstate_shared.is_lazy, cpu);
794794
}
795795

796796
static DEFINE_PER_CPU(cpumask_t, flush_tlb_mask);
797797

798+
DEFINE_PER_CPU_SHARED_ALIGNED(struct tlb_state_shared, cpu_tlbstate_shared);
799+
EXPORT_PER_CPU_SYMBOL(cpu_tlbstate_shared);
800+
798801
STATIC_NOPV void native_flush_tlb_multi(const struct cpumask *cpumask,
799802
const struct flush_tlb_info *info)
800803
{

0 commit comments

Comments
 (0)