aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTim Chen <tim.c.chen@linux.intel.com>2018-01-29 17:04:47 -0500
committerThomas Gleixner <tglx@linutronix.de>2018-01-30 17:09:21 -0500
commit18bf3c3ea8ece8f03b6fc58508f2dfd23c7711c7 (patch)
tree4245516482bb8e394254f3f23f347cf996d59af5
parent7fcae1118f5fd44a862aa5c3525248e35ee67c3b (diff)
x86/speculation: Use Indirect Branch Prediction Barrier in context switch
Flush indirect branches when switching into a process that marked itself non dumpable. This protects high value processes like gpg better, without having too high performance overhead. If done naïvely, we could switch to a kernel idle thread and then back to the original process, such as: process A -> idle -> process A In such scenario, we do not have to do IBPB here even though the process is non-dumpable, as we are switching back to the same process after a hiatus. To avoid the redundant IBPB, which is expensive, we track the last mm user context ID. The cost is to have an extra u64 mm context id to track the last mm we were using before switching to the init_mm used by idle. Avoiding the extra IBPB is probably worth the extra memory for this common scenario. For those cases where tlb_defer_switch_to_init_mm() returns true (non PCID), lazy tlb will defer switch to init_mm, so we will not be changing the mm for the process A -> idle -> process A switch. So IBPB will be skipped for this case. Thanks to the reviewers and Andy Lutomirski for the suggestion of using ctx_id which got rid of the problem of mm pointer recycling. Signed-off-by: Tim Chen <tim.c.chen@linux.intel.com> Signed-off-by: David Woodhouse <dwmw@amazon.co.uk> Signed-off-by: Thomas Gleixner <tglx@linutronix.de> Cc: ak@linux.intel.com Cc: karahmed@amazon.de Cc: arjan@linux.intel.com Cc: torvalds@linux-foundation.org Cc: linux@dominikbrodowski.net Cc: peterz@infradead.org Cc: bp@alien8.de Cc: luto@kernel.org Cc: pbonzini@redhat.com Cc: gregkh@linux-foundation.org Link: https://lkml.kernel.org/r/1517263487-3708-1-git-send-email-dwmw@amazon.co.uk
-rw-r--r--arch/x86/include/asm/tlbflush.h2
-rw-r--r--arch/x86/mm/tlb.c33
2 files changed, 34 insertions, 1 deletions
diff --git a/arch/x86/include/asm/tlbflush.h b/arch/x86/include/asm/tlbflush.h
index d33e4a26dc7e..2b8f18ca5874 100644
--- a/arch/x86/include/asm/tlbflush.h
+++ b/arch/x86/include/asm/tlbflush.h
@@ -174,6 +174,8 @@ struct tlb_state {
174 struct mm_struct *loaded_mm; 174 struct mm_struct *loaded_mm;
175 u16 loaded_mm_asid; 175 u16 loaded_mm_asid;
176 u16 next_asid; 176 u16 next_asid;
177 /* last user mm's ctx id */
178 u64 last_ctx_id;
177 179
178 /* 180 /*
179 * We can be in one of several states: 181 * We can be in one of several states:
diff --git a/arch/x86/mm/tlb.c b/arch/x86/mm/tlb.c
index 5bfe61a5e8e3..012d02624848 100644
--- a/arch/x86/mm/tlb.c
+++ b/arch/x86/mm/tlb.c
@@ -6,13 +6,14 @@
6#include <linux/interrupt.h> 6#include <linux/interrupt.h>
7#include <linux/export.h> 7#include <linux/export.h>
8#include <linux/cpu.h> 8#include <linux/cpu.h>
9#include <linux/debugfs.h>
9 10
10#include <asm/tlbflush.h> 11#include <asm/tlbflush.h>
11#include <asm/mmu_context.h> 12#include <asm/mmu_context.h>
13#include <asm/nospec-branch.h>
12#include <asm/cache.h> 14#include <asm/cache.h>
13#include <asm/apic.h> 15#include <asm/apic.h>
14#include <asm/uv/uv.h> 16#include <asm/uv/uv.h>
15#include <linux/debugfs.h>
16 17
17/* 18/*
18 * TLB flushing, formerly SMP-only 19 * TLB flushing, formerly SMP-only
@@ -247,6 +248,27 @@ void switch_mm_irqs_off(struct mm_struct *prev, struct mm_struct *next,
247 } else { 248 } else {
248 u16 new_asid; 249 u16 new_asid;
249 bool need_flush; 250 bool need_flush;
251 u64 last_ctx_id = this_cpu_read(cpu_tlbstate.last_ctx_id);
252
253 /*
254 * Avoid user/user BTB poisoning by flushing the branch
255 * predictor when switching between processes. This stops
256 * one process from doing Spectre-v2 attacks on another.
257 *
258 * As an optimization, flush indirect branches only when
259 * switching into processes that disable dumping. This
260 * protects high value processes like gpg, without having
261 * too high performance overhead. IBPB is *expensive*!
262 *
263 * This will not flush branches when switching into kernel
264 * threads. It will also not flush if we switch to idle
265 * thread and back to the same process. It will flush if we
266 * switch to a different non-dumpable process.
267 */
268 if (tsk && tsk->mm &&
269 tsk->mm->context.ctx_id != last_ctx_id &&
270 get_dumpable(tsk->mm) != SUID_DUMP_USER)
271 indirect_branch_prediction_barrier();
250 272
251 if (IS_ENABLED(CONFIG_VMAP_STACK)) { 273 if (IS_ENABLED(CONFIG_VMAP_STACK)) {
252 /* 274 /*
@@ -292,6 +314,14 @@ void switch_mm_irqs_off(struct mm_struct *prev, struct mm_struct *next,
292 trace_tlb_flush_rcuidle(TLB_FLUSH_ON_TASK_SWITCH, 0); 314 trace_tlb_flush_rcuidle(TLB_FLUSH_ON_TASK_SWITCH, 0);
293 } 315 }
294 316
317 /*
318 * Record last user mm's context id, so we can avoid
319 * flushing branch buffer with IBPB if we switch back
320 * to the same user.
321 */
322 if (next != &init_mm)
323 this_cpu_write(cpu_tlbstate.last_ctx_id, next->context.ctx_id);
324
295 this_cpu_write(cpu_tlbstate.loaded_mm, next); 325 this_cpu_write(cpu_tlbstate.loaded_mm, next);
296 this_cpu_write(cpu_tlbstate.loaded_mm_asid, new_asid); 326 this_cpu_write(cpu_tlbstate.loaded_mm_asid, new_asid);
297 } 327 }
@@ -369,6 +399,7 @@ void initialize_tlbstate_and_flush(void)
369 write_cr3(build_cr3(mm->pgd, 0)); 399 write_cr3(build_cr3(mm->pgd, 0));
370 400
371 /* Reinitialize tlbstate. */ 401 /* Reinitialize tlbstate. */
402 this_cpu_write(cpu_tlbstate.last_ctx_id, mm->context.ctx_id);
372 this_cpu_write(cpu_tlbstate.loaded_mm_asid, 0); 403 this_cpu_write(cpu_tlbstate.loaded_mm_asid, 0);
373 this_cpu_write(cpu_tlbstate.next_asid, 1); 404 this_cpu_write(cpu_tlbstate.next_asid, 1);
374 this_cpu_write(cpu_tlbstate.ctxs[0].ctx_id, mm->context.ctx_id); 405 this_cpu_write(cpu_tlbstate.ctxs[0].ctx_id, mm->context.ctx_id);