Skip to content

Commit 8f1d56f

Browse files
anadavhansendc
authored andcommitted
x86/mm/tlb: Ignore f->new_tlb_gen when zero
Commit aa44284 ("x86/mm/tlb: Avoid reading mm_tlb_gen when possible") introduced an optimization to skip superfluous TLB flushes based on the generation provided in flush_tlb_info. However, arch_tlbbatch_flush() does not provide any generation in flush_tlb_info and populates the flush_tlb_info generation with 0. This 0 is causes the flush_tlb_info to be interpreted as a superfluous, old flush. As a result, try_to_unmap_one() would not perform any TLB flushes. Fix it by checking whether f->new_tlb_gen is nonzero. Zero value is anyhow is an invalid generation value. To avoid future confusion, introduce TLB_GENERATION_INVALID constant and use it properly. Add warnings to ensure no partial flushes are done with TLB_GENERATION_INVALID or when f->mm is NULL, since this does not make any sense. In addition, add the missing unlikely(). [ dhansen: change VM_BUG_ON() -> VM_WARN_ON(), clarify changelog ] Fixes: aa44284 ("x86/mm/tlb: Avoid reading mm_tlb_gen when possible") Reported-by: Hugh Dickins <hughd@google.com> Signed-off-by: Nadav Amit <namit@vmware.com> Signed-off-by: Dave Hansen <dave.hansen@linux.intel.com> Tested-by: Hugh Dickins <hughd@google.com> Link: https://lkml.kernel.org/r/20220710232837.3618-1-namit@vmware.com
1 parent 54ee184 commit 8f1d56f

2 files changed

Lines changed: 13 additions & 3 deletions

File tree

arch/x86/include/asm/tlbflush.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
void __flush_tlb_all(void);
1717

1818
#define TLB_FLUSH_ALL -1UL
19+
#define TLB_GENERATION_INVALID 0
1920

2021
void cr4_update_irqsoff(unsigned long set, unsigned long clear);
2122
unsigned long cr4_read_shadow(void);

arch/x86/mm/tlb.c

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -771,7 +771,8 @@ static void flush_tlb_func(void *info)
771771
return;
772772
}
773773

774-
if (f->new_tlb_gen <= local_tlb_gen) {
774+
if (unlikely(f->new_tlb_gen != TLB_GENERATION_INVALID &&
775+
f->new_tlb_gen <= local_tlb_gen)) {
775776
/*
776777
* The TLB is already up to date in respect to f->new_tlb_gen.
777778
* While the core might be still behind mm_tlb_gen, checking
@@ -843,6 +844,12 @@ static void flush_tlb_func(void *info)
843844
/* Partial flush */
844845
unsigned long addr = f->start;
845846

847+
/* Partial flush cannot have invalid generations */
848+
VM_WARN_ON(f->new_tlb_gen == TLB_GENERATION_INVALID);
849+
850+
/* Partial flush must have valid mm */
851+
VM_WARN_ON(f->mm == NULL);
852+
846853
nr_invalidate = (f->end - f->start) >> f->stride_shift;
847854

848855
while (addr < f->end) {
@@ -1045,7 +1052,8 @@ void flush_tlb_kernel_range(unsigned long start, unsigned long end)
10451052
struct flush_tlb_info *info;
10461053

10471054
preempt_disable();
1048-
info = get_flush_tlb_info(NULL, start, end, 0, false, 0);
1055+
info = get_flush_tlb_info(NULL, start, end, 0, false,
1056+
TLB_GENERATION_INVALID);
10491057

10501058
on_each_cpu(do_kernel_range_flush, info, 1);
10511059

@@ -1214,7 +1222,8 @@ void arch_tlbbatch_flush(struct arch_tlbflush_unmap_batch *batch)
12141222

12151223
int cpu = get_cpu();
12161224

1217-
info = get_flush_tlb_info(NULL, 0, TLB_FLUSH_ALL, 0, false, 0);
1225+
info = get_flush_tlb_info(NULL, 0, TLB_FLUSH_ALL, 0, false,
1226+
TLB_GENERATION_INVALID);
12181227
/*
12191228
* flush_tlb_multi() is not optimized for the common case in which only
12201229
* a local TLB flush is needed. Optimize this use-case by calling

0 commit comments

Comments
 (0)