Skip to content

Commit ce47479

Browse files
committed
Merge branch 'x86-mm-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip
Pull x86 mm changes from Ingo Molnar: "The main change in this cycle is the rework of the TLB range flushing code, to simplify, fix and consolidate the code. By Dave Hansen" * 'x86-mm-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip: x86/mm: Set TLB flush tunable to sane value (33) x86/mm: New tunable for single vs full TLB flush x86/mm: Add tracepoints for TLB flushes x86/mm: Unify remote INVLPG code x86/mm: Fix missed global TLB flush stat x86/mm: Rip out complicated, out-of-date, buggy TLB flushing x86/mm: Clean up the TLB flushing code x86/smep: Be more informative when signalling an SMEP fault
2 parents 76f09aa + a510247 commit ce47479

11 files changed

Lines changed: 193 additions & 99 deletions

File tree

‎Documentation/x86/tlb.txt‎

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
When the kernel unmaps or modified the attributes of a range of
2+
memory, it has two choices:
3+
1. Flush the entire TLB with a two-instruction sequence. This is
4+
a quick operation, but it causes collateral damage: TLB entries
5+
from areas other than the one we are trying to flush will be
6+
destroyed and must be refilled later, at some cost.
7+
2. Use the invlpg instruction to invalidate a single page at a
8+
time. This could potentialy cost many more instructions, but
9+
it is a much more precise operation, causing no collateral
10+
damage to other TLB entries.
11+
12+
Which method to do depends on a few things:
13+
1. The size of the flush being performed. A flush of the entire
14+
address space is obviously better performed by flushing the
15+
entire TLB than doing 2^48/PAGE_SIZE individual flushes.
16+
2. The contents of the TLB. If the TLB is empty, then there will
17+
be no collateral damage caused by doing the global flush, and
18+
all of the individual flush will have ended up being wasted
19+
work.
20+
3. The size of the TLB. The larger the TLB, the more collateral
21+
damage we do with a full flush. So, the larger the TLB, the
22+
more attrative an individual flush looks. Data and
23+
instructions have separate TLBs, as do different page sizes.
24+
4. The microarchitecture. The TLB has become a multi-level
25+
cache on modern CPUs, and the global flushes have become more
26+
expensive relative to single-page flushes.
27+
28+
There is obviously no way the kernel can know all these things,
29+
especially the contents of the TLB during a given flush. The
30+
sizes of the flush will vary greatly depending on the workload as
31+
well. There is essentially no "right" point to choose.
32+
33+
You may be doing too many individual invalidations if you see the
34+
invlpg instruction (or instructions _near_ it) show up high in
35+
profiles. If you believe that individual invalidations being
36+
called too often, you can lower the tunable:
37+
38+
/sys/debug/kernel/x86/tlb_single_page_flush_ceiling
39+
40+
This will cause us to do the global flush for more cases.
41+
Lowering it to 0 will disable the use of the individual flushes.
42+
Setting it to 1 is a very conservative setting and it should
43+
never need to be 0 under normal circumstances.
44+
45+
Despite the fact that a single individual flush on x86 is
46+
guaranteed to flush a full 2MB [1], hugetlbfs always uses the full
47+
flushes. THP is treated exactly the same as normal memory.
48+
49+
You might see invlpg inside of flush_tlb_mm_range() show up in
50+
profiles, or you can use the trace_tlb_flush() tracepoints. to
51+
determine how long the flush operations are taking.
52+
53+
Essentially, you are balancing the cycles you spend doing invlpg
54+
with the cycles that you spend refilling the TLB later.
55+
56+
You can measure how expensive TLB refills are by using
57+
performance counters and 'perf stat', like this:
58+
59+
perf stat -e
60+
cpu/event=0x8,umask=0x84,name=dtlb_load_misses_walk_duration/,
61+
cpu/event=0x8,umask=0x82,name=dtlb_load_misses_walk_completed/,
62+
cpu/event=0x49,umask=0x4,name=dtlb_store_misses_walk_duration/,
63+
cpu/event=0x49,umask=0x2,name=dtlb_store_misses_walk_completed/,
64+
cpu/event=0x85,umask=0x4,name=itlb_misses_walk_duration/,
65+
cpu/event=0x85,umask=0x2,name=itlb_misses_walk_completed/
66+
67+
That works on an IvyBridge-era CPU (i5-3320M). Different CPUs
68+
may have differently-named counters, but they should at least
69+
be there in some form. You can use pmu-tools 'ocperf list'
70+
(https://github.com/andikleen/pmu-tools) to find the right
71+
counters for a given CPU.
72+
73+
1. A footnote in Intel's SDM "4.10.4.2 Recommended Invalidation"
74+
says: "One execution of INVLPG is sufficient even for a page
75+
with size greater than 4 KBytes."

‎arch/x86/include/asm/mmu_context.h‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33

44
#include <asm/desc.h>
55
#include <linux/atomic.h>
6+
#include <linux/mm_types.h>
7+
8+
#include <trace/events/tlb.h>
9+
610
#include <asm/pgalloc.h>
711
#include <asm/tlbflush.h>
812
#include <asm/paravirt.h>
@@ -44,6 +48,7 @@ static inline void switch_mm(struct mm_struct *prev, struct mm_struct *next,
4448

4549
/* Re-load page tables */
4650
load_cr3(next->pgd);
51+
trace_tlb_flush(TLB_FLUSH_ON_TASK_SWITCH, TLB_FLUSH_ALL);
4752

4853
/* Stop flush ipis for the previous mm */
4954
cpumask_clear_cpu(cpu, mm_cpumask(prev));
@@ -71,6 +76,7 @@ static inline void switch_mm(struct mm_struct *prev, struct mm_struct *next,
7176
* to make sure to use no freed page tables.
7277
*/
7378
load_cr3(next->pgd);
79+
trace_tlb_flush(TLB_FLUSH_ON_TASK_SWITCH, TLB_FLUSH_ALL);
7480
load_LDT_nolock(&next->context);
7581
}
7682
}

‎arch/x86/include/asm/processor.h‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,6 @@ extern u16 __read_mostly tlb_lld_4k[NR_INFO];
7272
extern u16 __read_mostly tlb_lld_2m[NR_INFO];
7373
extern u16 __read_mostly tlb_lld_4m[NR_INFO];
7474
extern u16 __read_mostly tlb_lld_1g[NR_INFO];
75-
extern s8 __read_mostly tlb_flushall_shift;
7675

7776
/*
7877
* CPU type and hardware bug flags. Kept separately for each CPU.

‎arch/x86/kernel/cpu/amd.c‎

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -724,11 +724,6 @@ static unsigned int amd_size_cache(struct cpuinfo_x86 *c, unsigned int size)
724724
}
725725
#endif
726726

727-
static void cpu_set_tlb_flushall_shift(struct cpuinfo_x86 *c)
728-
{
729-
tlb_flushall_shift = 6;
730-
}
731-
732727
static void cpu_detect_tlb_amd(struct cpuinfo_x86 *c)
733728
{
734729
u32 ebx, eax, ecx, edx;
@@ -776,8 +771,6 @@ static void cpu_detect_tlb_amd(struct cpuinfo_x86 *c)
776771
tlb_lli_2m[ENTRIES] = eax & mask;
777772

778773
tlb_lli_4m[ENTRIES] = tlb_lli_2m[ENTRIES] >> 1;
779-
780-
cpu_set_tlb_flushall_shift(c);
781774
}
782775

783776
static const struct cpu_dev amd_cpu_dev = {

‎arch/x86/kernel/cpu/common.c‎

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -481,26 +481,17 @@ u16 __read_mostly tlb_lld_2m[NR_INFO];
481481
u16 __read_mostly tlb_lld_4m[NR_INFO];
482482
u16 __read_mostly tlb_lld_1g[NR_INFO];
483483

484-
/*
485-
* tlb_flushall_shift shows the balance point in replacing cr3 write
486-
* with multiple 'invlpg'. It will do this replacement when
487-
* flush_tlb_lines <= active_lines/2^tlb_flushall_shift.
488-
* If tlb_flushall_shift is -1, means the replacement will be disabled.
489-
*/
490-
s8 __read_mostly tlb_flushall_shift = -1;
491-
492484
void cpu_detect_tlb(struct cpuinfo_x86 *c)
493485
{
494486
if (this_cpu->c_detect_tlb)
495487
this_cpu->c_detect_tlb(c);
496488

497489
printk(KERN_INFO "Last level iTLB entries: 4KB %d, 2MB %d, 4MB %d\n"
498-
"Last level dTLB entries: 4KB %d, 2MB %d, 4MB %d, 1GB %d\n"
499-
"tlb_flushall_shift: %d\n",
490+
"Last level dTLB entries: 4KB %d, 2MB %d, 4MB %d, 1GB %d\n",
500491
tlb_lli_4k[ENTRIES], tlb_lli_2m[ENTRIES],
501492
tlb_lli_4m[ENTRIES], tlb_lld_4k[ENTRIES],
502493
tlb_lld_2m[ENTRIES], tlb_lld_4m[ENTRIES],
503-
tlb_lld_1g[ENTRIES], tlb_flushall_shift);
494+
tlb_lld_1g[ENTRIES]);
504495
}
505496

506497
void detect_ht(struct cpuinfo_x86 *c)

‎arch/x86/kernel/cpu/intel.c‎

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -634,31 +634,6 @@ static void intel_tlb_lookup(const unsigned char desc)
634634
}
635635
}
636636

637-
static void intel_tlb_flushall_shift_set(struct cpuinfo_x86 *c)
638-
{
639-
switch ((c->x86 << 8) + c->x86_model) {
640-
case 0x60f: /* original 65 nm celeron/pentium/core2/xeon, "Merom"/"Conroe" */
641-
case 0x616: /* single-core 65 nm celeron/core2solo "Merom-L"/"Conroe-L" */
642-
case 0x617: /* current 45 nm celeron/core2/xeon "Penryn"/"Wolfdale" */
643-
case 0x61d: /* six-core 45 nm xeon "Dunnington" */
644-
tlb_flushall_shift = -1;
645-
break;
646-
case 0x63a: /* Ivybridge */
647-
tlb_flushall_shift = 2;
648-
break;
649-
case 0x61a: /* 45 nm nehalem, "Bloomfield" */
650-
case 0x61e: /* 45 nm nehalem, "Lynnfield" */
651-
case 0x625: /* 32 nm nehalem, "Clarkdale" */
652-
case 0x62c: /* 32 nm nehalem, "Gulftown" */
653-
case 0x62e: /* 45 nm nehalem-ex, "Beckton" */
654-
case 0x62f: /* 32 nm Xeon E7 */
655-
case 0x62a: /* SandyBridge */
656-
case 0x62d: /* SandyBridge, "Romely-EP" */
657-
default:
658-
tlb_flushall_shift = 6;
659-
}
660-
}
661-
662637
static void intel_detect_tlb(struct cpuinfo_x86 *c)
663638
{
664639
int i, j, n;
@@ -683,7 +658,6 @@ static void intel_detect_tlb(struct cpuinfo_x86 *c)
683658
for (j = 1 ; j < 16 ; j++)
684659
intel_tlb_lookup(desc[j]);
685660
}
686-
intel_tlb_flushall_shift_set(c);
687661
}
688662

689663
static const struct cpu_dev intel_cpu_dev = {

‎arch/x86/mm/fault.c‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -577,6 +577,8 @@ static int is_f00f_bug(struct pt_regs *regs, unsigned long address)
577577

578578
static const char nx_warning[] = KERN_CRIT
579579
"kernel tried to execute NX-protected page - exploit attempt? (uid: %d)\n";
580+
static const char smep_warning[] = KERN_CRIT
581+
"unable to execute userspace code (SMEP?) (uid: %d)\n";
580582

581583
static void
582584
show_fault_oops(struct pt_regs *regs, unsigned long error_code,
@@ -597,6 +599,10 @@ show_fault_oops(struct pt_regs *regs, unsigned long error_code,
597599

598600
if (pte && pte_present(*pte) && !pte_exec(*pte))
599601
printk(nx_warning, from_kuid(&init_user_ns, current_uid()));
602+
if (pte && pte_present(*pte) && pte_exec(*pte) &&
603+
(pgd_flags(*pgd) & _PAGE_USER) &&
604+
(read_cr4() & X86_CR4_SMEP))
605+
printk(smep_warning, from_kuid(&init_user_ns, current_uid()));
600606
}
601607

602608
printk(KERN_ALERT "BUG: unable to handle kernel ");

‎arch/x86/mm/init.c‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,13 @@
1818
#include <asm/dma.h> /* for MAX_DMA_PFN */
1919
#include <asm/microcode.h>
2020

21+
/*
22+
* We need to define the tracepoints somewhere, and tlb.c
23+
* is only compied when SMP=y.
24+
*/
25+
#define CREATE_TRACE_POINTS
26+
#include <trace/events/tlb.h>
27+
2128
#include "mm_internal.h"
2229

2330
static unsigned long __initdata pgt_buf_start;

0 commit comments

Comments
 (0)