All of lore.kernel.org
 help / color / mirror / Atom feed
From: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
To: linux-kernel@vger.kernel.org
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	stable@vger.kernel.org, Tim Chen <tim.c.chen@linux.intel.com>,
	David Woodhouse <dwmw@amazon.co.uk>,
	Thomas Gleixner <tglx@linutronix.de>,
	ak@linux.intel.com, karahmed@amazon.de, arjan@linux.intel.com,
	torvalds@linux-foundation.org, linux@dominikbrodowski.net,
	peterz@infradead.org, bp@alien8.de, luto@kernel.org,
	pbonzini@redhat.com, gregkh@linux-foundation.org
Subject: [PATCH 4.15 46/60] x86/speculation: Use Indirect Branch Prediction Barrier in context switch
Date: Mon,  5 Feb 2018 10:23:19 -0800	[thread overview]
Message-ID: <20180205182215.874232314@linuxfoundation.org> (raw)
In-Reply-To: <20180205182213.902626065@linuxfoundation.org>

4.15-stable review patch.  If anyone has any objections, please let me know.

------------------

From: Tim Chen tim.c.chen@linux.intel.com

commit 18bf3c3ea8ece8f03b6fc58508f2dfd23c7711c7

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
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>


---
 arch/x86/include/asm/tlbflush.h |    2 ++
 arch/x86/mm/tlb.c               |   33 ++++++++++++++++++++++++++++++++-
 2 files changed, 34 insertions(+), 1 deletion(-)

--- a/arch/x86/include/asm/tlbflush.h
+++ b/arch/x86/include/asm/tlbflush.h
@@ -174,6 +174,8 @@ struct tlb_state {
 	struct mm_struct *loaded_mm;
 	u16 loaded_mm_asid;
 	u16 next_asid;
+	/* last user mm's ctx id */
+	u64 last_ctx_id;
 
 	/*
 	 * We can be in one of several states:
--- a/arch/x86/mm/tlb.c
+++ b/arch/x86/mm/tlb.c
@@ -6,13 +6,14 @@
 #include <linux/interrupt.h>
 #include <linux/export.h>
 #include <linux/cpu.h>
+#include <linux/debugfs.h>
 
 #include <asm/tlbflush.h>
 #include <asm/mmu_context.h>
+#include <asm/nospec-branch.h>
 #include <asm/cache.h>
 #include <asm/apic.h>
 #include <asm/uv/uv.h>
-#include <linux/debugfs.h>
 
 /*
  *	TLB flushing, formerly SMP-only
@@ -247,6 +248,27 @@ void switch_mm_irqs_off(struct mm_struct
 	} else {
 		u16 new_asid;
 		bool need_flush;
+		u64 last_ctx_id = this_cpu_read(cpu_tlbstate.last_ctx_id);
+
+		/*
+		 * Avoid user/user BTB poisoning by flushing the branch
+		 * predictor when switching between processes. This stops
+		 * one process from doing Spectre-v2 attacks on another.
+		 *
+		 * As an optimization, flush indirect branches only when
+		 * switching into processes that disable dumping. This
+		 * protects high value processes like gpg, without having
+		 * too high performance overhead. IBPB is *expensive*!
+		 *
+		 * This will not flush branches when switching into kernel
+		 * threads. It will also not flush if we switch to idle
+		 * thread and back to the same process. It will flush if we
+		 * switch to a different non-dumpable process.
+		 */
+		if (tsk && tsk->mm &&
+		    tsk->mm->context.ctx_id != last_ctx_id &&
+		    get_dumpable(tsk->mm) != SUID_DUMP_USER)
+			indirect_branch_prediction_barrier();
 
 		if (IS_ENABLED(CONFIG_VMAP_STACK)) {
 			/*
@@ -292,6 +314,14 @@ void switch_mm_irqs_off(struct mm_struct
 			trace_tlb_flush_rcuidle(TLB_FLUSH_ON_TASK_SWITCH, 0);
 		}
 
+		/*
+		 * Record last user mm's context id, so we can avoid
+		 * flushing branch buffer with IBPB if we switch back
+		 * to the same user.
+		 */
+		if (next != &init_mm)
+			this_cpu_write(cpu_tlbstate.last_ctx_id, next->context.ctx_id);
+
 		this_cpu_write(cpu_tlbstate.loaded_mm, next);
 		this_cpu_write(cpu_tlbstate.loaded_mm_asid, new_asid);
 	}
@@ -369,6 +399,7 @@ void initialize_tlbstate_and_flush(void)
 	write_cr3(build_cr3(mm->pgd, 0));
 
 	/* Reinitialize tlbstate. */
+	this_cpu_write(cpu_tlbstate.last_ctx_id, mm->context.ctx_id);
 	this_cpu_write(cpu_tlbstate.loaded_mm_asid, 0);
 	this_cpu_write(cpu_tlbstate.next_asid, 1);
 	this_cpu_write(cpu_tlbstate.ctxs[0].ctx_id, mm->context.ctx_id);

  parent reply	other threads:[~2018-02-05 18:23 UTC|newest]

Thread overview: 74+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-02-05 18:22 [PATCH 4.15 00/60] 4.15.2-stable review Greg Kroah-Hartman
2018-02-05 18:22 ` [PATCH 4.15 01/60] KVM: x86: Make indirect calls in emulator speculation safe Greg Kroah-Hartman
2018-02-05 18:22 ` [PATCH 4.15 02/60] KVM: VMX: Make indirect call " Greg Kroah-Hartman
2018-02-05 18:22 ` [PATCH 4.15 03/60] module/retpoline: Warn about missing retpoline in module Greg Kroah-Hartman
2018-02-05 18:22 ` [PATCH 4.15 04/60] x86/cpufeatures: Add CPUID_7_EDX CPUID leaf Greg Kroah-Hartman
2018-02-05 18:22 ` [PATCH 4.15 05/60] x86/cpufeatures: Add Intel feature bits for Speculation Control Greg Kroah-Hartman
2018-02-05 18:22 ` [PATCH 4.15 06/60] x86/cpufeatures: Add AMD " Greg Kroah-Hartman
2018-02-05 18:22 ` [PATCH 4.15 07/60] x86/msr: Add definitions for new speculation control MSRs Greg Kroah-Hartman
2018-02-05 18:22 ` [PATCH 4.15 08/60] x86/pti: Do not enable PTI on CPUs which are not vulnerable to Meltdown Greg Kroah-Hartman
2018-02-05 18:22 ` [PATCH 4.15 09/60] x86/cpufeature: Blacklist SPEC_CTRL/PRED_CMD on early Spectre v2 microcodes Greg Kroah-Hartman
2018-02-05 18:22 ` [PATCH 4.15 10/60] x86/speculation: Add basic IBPB (Indirect Branch Prediction Barrier) support Greg Kroah-Hartman
2018-02-05 18:22 ` [PATCH 4.15 11/60] x86/alternative: Print unadorned pointers Greg Kroah-Hartman
2018-02-05 18:22 ` [PATCH 4.15 12/60] x86/nospec: Fix header guards names Greg Kroah-Hartman
2018-02-05 18:22 ` [PATCH 4.15 13/60] x86/bugs: Drop one "mitigation" from dmesg Greg Kroah-Hartman
2018-02-05 18:22 ` [PATCH 4.15 14/60] x86/cpu/bugs: Make retpoline module warning conditional Greg Kroah-Hartman
2018-02-05 18:22 ` [PATCH 4.15 15/60] x86/cpufeatures: Clean up Spectre v2 related CPUID flags Greg Kroah-Hartman
2018-02-05 18:22 ` [PATCH 4.15 16/60] x86/retpoline: Simplify vmexit_fill_RSB() Greg Kroah-Hartman
2018-02-05 18:22 ` [PATCH 4.15 17/60] x86/speculation: Simplify indirect_branch_prediction_barrier() Greg Kroah-Hartman
2018-02-05 18:22 ` [PATCH 4.15 18/60] auxdisplay: img-ascii-lcd: add missing MODULE_DESCRIPTION/AUTHOR/LICENSE Greg Kroah-Hartman
2018-02-05 18:22 ` [PATCH 4.15 19/60] iio: adc/accel: Fix up module licenses Greg Kroah-Hartman
2018-02-05 18:22 ` [PATCH 4.15 20/60] pinctrl: pxa: pxa2xx: add missing MODULE_DESCRIPTION/AUTHOR/LICENSE Greg Kroah-Hartman
2018-02-05 18:22 ` [PATCH 4.15 21/60] ASoC: pcm512x: " Greg Kroah-Hartman
2018-02-05 18:22 ` [PATCH 4.15 22/60] KVM: nVMX: Eliminate vmcs02 pool Greg Kroah-Hartman
2018-02-05 18:22 ` [PATCH 4.15 23/60] KVM: VMX: introduce alloc_loaded_vmcs Greg Kroah-Hartman
2018-02-05 18:22 ` [PATCH 4.15 24/60] objtool: Improve retpoline alternative handling Greg Kroah-Hartman
2018-02-05 18:22 ` [PATCH 4.15 25/60] objtool: Add support for alternatives at the end of a section Greg Kroah-Hartman
2018-02-05 18:22 ` [PATCH 4.15 26/60] objtool: Warn on stripped section symbol Greg Kroah-Hartman
2018-02-05 18:23 ` [PATCH 4.15 27/60] x86/mm: Fix overlap of i386 CPU_ENTRY_AREA with FIX_BTMAP Greg Kroah-Hartman
2018-02-05 18:23 ` [PATCH 4.15 28/60] x86/spectre: Check CONFIG_RETPOLINE in command line parser Greg Kroah-Hartman
2018-02-05 18:23 ` [PATCH 4.15 29/60] x86/entry/64: Remove the SYSCALL64 fast path Greg Kroah-Hartman
2018-02-05 18:23 ` [PATCH 4.15 30/60] x86/entry/64: Push extra regs right away Greg Kroah-Hartman
2018-02-05 18:23 ` [PATCH 4.15 31/60] x86/asm: Move status from thread_struct to thread_info Greg Kroah-Hartman
2018-02-05 18:23 ` [PATCH 4.15 32/60] Documentation: Document array_index_nospec Greg Kroah-Hartman
2018-02-05 18:23 ` [PATCH 4.15 33/60] array_index_nospec: Sanitize speculative array de-references Greg Kroah-Hartman
2018-02-05 18:23 ` [PATCH 4.15 34/60] x86: Implement array_index_mask_nospec Greg Kroah-Hartman
2018-02-05 18:23 ` [PATCH 4.15 35/60] x86: Introduce barrier_nospec Greg Kroah-Hartman
2018-02-05 18:23 ` [PATCH 4.15 36/60] x86: Introduce __uaccess_begin_nospec() and uaccess_try_nospec Greg Kroah-Hartman
2018-02-05 18:23 ` [PATCH 4.15 37/60] x86/usercopy: Replace open coded stac/clac with __uaccess_{begin, end} Greg Kroah-Hartman
2018-02-05 18:23 ` [PATCH 4.15 38/60] x86/uaccess: Use __uaccess_begin_nospec() and uaccess_try_nospec Greg Kroah-Hartman
2018-02-05 18:23 ` [PATCH 4.15 39/60] x86/get_user: Use pointer masking to limit speculation Greg Kroah-Hartman
2018-02-05 18:23 ` [PATCH 4.15 40/60] x86/syscall: Sanitize syscall table de-references under speculation Greg Kroah-Hartman
2018-02-05 18:23 ` [PATCH 4.15 41/60] vfs, fdtable: Prevent bounds-check bypass via speculative execution Greg Kroah-Hartman
2018-02-05 18:23 ` [PATCH 4.15 42/60] nl80211: Sanitize array index in parse_txq_params Greg Kroah-Hartman
2018-02-05 18:23 ` [PATCH 4.15 43/60] x86/spectre: Report get_user mitigation for spectre_v1 Greg Kroah-Hartman
2018-02-05 18:23 ` [PATCH 4.15 44/60] x86/spectre: Fix spelling mistake: "vunerable"-> "vulnerable" Greg Kroah-Hartman
2018-02-05 18:23   ` Greg Kroah-Hartman
2018-02-05 18:23 ` [PATCH 4.15 45/60] x86/cpuid: Fix up "virtual" IBRS/IBPB/STIBP feature bits on Intel Greg Kroah-Hartman
2018-02-05 18:23 ` Greg Kroah-Hartman [this message]
2018-02-05 18:23 ` [PATCH 4.15 47/60] x86/paravirt: Remove noreplace-paravirt cmdline option Greg Kroah-Hartman
2018-02-05 18:23 ` [PATCH 4.15 48/60] KVM: VMX: make MSR bitmaps per-VCPU Greg Kroah-Hartman
2018-02-05 18:23 ` [PATCH 4.15 49/60] x86/kvm: Update spectre-v1 mitigation Greg Kroah-Hartman
2018-02-05 18:23 ` [PATCH 4.15 50/60] x86/retpoline: Avoid retpolines for built-in __init functions Greg Kroah-Hartman
2018-02-05 18:23 ` [PATCH 4.15 51/60] x86/spectre: Simplify spectre_v2 command line parsing Greg Kroah-Hartman
2018-02-05 18:23 ` [PATCH 4.15 52/60] x86/pti: Mark constant arrays as __initconst Greg Kroah-Hartman
2018-02-05 18:23 ` [PATCH 4.15 53/60] x86/speculation: Fix typo IBRS_ATT, which should be IBRS_ALL Greg Kroah-Hartman
2018-02-05 18:23 ` [PATCH 4.15 54/60] KVM/x86: Update the reverse_cpuid list to include CPUID_7_EDX Greg Kroah-Hartman
2018-02-05 18:23 ` [PATCH 4.15 55/60] KVM/x86: Add IBPB support Greg Kroah-Hartman
2018-02-05 18:23   ` Greg Kroah-Hartman
2018-02-05 18:23 ` [PATCH 4.15 56/60] KVM/VMX: Emulate MSR_IA32_ARCH_CAPABILITIES Greg Kroah-Hartman
2018-02-05 18:23   ` Greg Kroah-Hartman
2018-02-05 18:23 ` [PATCH 4.15 57/60] KVM/VMX: Allow direct access to MSR_IA32_SPEC_CTRL Greg Kroah-Hartman
2018-02-05 18:23   ` Greg Kroah-Hartman
2018-02-05 18:23 ` [PATCH 4.15 58/60] KVM/SVM: " Greg Kroah-Hartman
2018-02-05 18:23   ` Greg Kroah-Hartman
2018-02-05 18:23 ` [PATCH 4.15 59/60] serial: core: mark port as initialized after successful IRQ change Greg Kroah-Hartman
2018-02-05 18:23 ` [PATCH 4.15 60/60] fpga: region: release of_parse_phandle nodes after use Greg Kroah-Hartman
2018-02-05 22:14 ` [PATCH 4.15 00/60] 4.15.2-stable review Shuah Khan
2018-02-06 10:33   ` Greg Kroah-Hartman
2018-02-05 23:58 ` Dan Rue
2018-02-06 10:33   ` Greg Kroah-Hartman
2018-02-06  0:36 ` kernelci.org bot
2018-02-06 14:30 ` Guenter Roeck
2018-02-06 14:32 ` Guenter Roeck
2018-02-06 17:00   ` Greg Kroah-Hartman

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20180205182215.874232314@linuxfoundation.org \
    --to=gregkh@linuxfoundation.org \
    --cc=ak@linux.intel.com \
    --cc=arjan@linux.intel.com \
    --cc=bp@alien8.de \
    --cc=dwmw@amazon.co.uk \
    --cc=gregkh@linux-foundation.org \
    --cc=karahmed@amazon.de \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux@dominikbrodowski.net \
    --cc=luto@kernel.org \
    --cc=pbonzini@redhat.com \
    --cc=peterz@infradead.org \
    --cc=stable@vger.kernel.org \
    --cc=tglx@linutronix.de \
    --cc=tim.c.chen@linux.intel.com \
    --cc=torvalds@linux-foundation.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.