Received: by 10.223.176.5 with SMTP id f5csp1154882wra; Wed, 31 Jan 2018 02:04:29 -0800 (PST) X-Google-Smtp-Source: AH8x224LTEFF+gL4gig7sddEnuB9+++AuBzF8/F2uPONn03BSZNkqo24Ji9yLXug1W27VGklp74M X-Received: by 10.98.110.202 with SMTP id j193mr33137547pfc.19.1517393069534; Wed, 31 Jan 2018 02:04:29 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1517393069; cv=none; d=google.com; s=arc-20160816; b=lEjp9o5lnPJM3Dn1IsnQOSXutp4T2eP4kWi5b4zF/XvA6h92eh6Xoa7Uk2QExeYyke mC/Sgs20ay5CJkvv8Sf2X5l+vWuLJVdbiiLqU2g9brLoT7IZlbouVHuw0Rxpo58p6TFU T1axB2Ce+Wmgcr7fdDj3N5Vs+8XNwMhf8AlPmIOztkUBAeBPUocgliiJ9KhobPuOGLyr sdncnLcUwmjX00V3ypGxTVSwiJl+cswAaUHcPgkveyChJNCyY7aF6LhIEIsZhfayWieq Mnq80Lr0dHtHMkSRoY+o5UZFgpZG3MiiAriR5rMItF1pJANhA7TURSfYCIBU6j5K4qkK 0CgQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:date:in-reply-to:message-id :subject:cc:to:user-agent:references:from:dkim-signature :arc-authentication-results; bh=5QR25vhJARn+GEdVKuMsLbV4zV7XeokyqFf6z/5XmG4=; b=b3QCzFV9PBuh6pEAL/aehOu5hIKP7nk6cg8cnfWhH5oi4KNR1ACjE+DscDv3zxnVv7 +HtYG+nTeue5efuNyurJ0qN3axqLpCAjS8vAOwzd33gDnObYz6WQqKuyJezF70ddCdqq Y9f0bdUTPjeBBQdb3oQCKeiPvdm92gP0dwY7Jq4Md3CkXozUJKCjNvDcpC8U1SlwtOmn 2b1JaAsFZPPDNtbwhuHFH28rSih41LYZ+O26bID0jyWShTuVOJB5NhG5aTB7RllwR5zH VvEtiEw48GBzlPjYiLDAiqEbOyNU54mA2/e3wH4URMUD4Iq+9KC4hLdgFEQK1ZyPwZn/ /4uQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=uYgqWbNR; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id x61-v6si3826385plb.584.2018.01.31.02.04.14; Wed, 31 Jan 2018 02:04:29 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=uYgqWbNR; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753331AbeAaKDk (ORCPT + 99 others); Wed, 31 Jan 2018 05:03:40 -0500 Received: from mail-wm0-f65.google.com ([74.125.82.65]:36444 "EHLO mail-wm0-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752328AbeAaKDg (ORCPT ); Wed, 31 Jan 2018 05:03:36 -0500 Received: by mail-wm0-f65.google.com with SMTP id f3so6791270wmc.1; Wed, 31 Jan 2018 02:03:35 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:references:user-agent:to:cc:subject:message-id:in-reply-to :date:mime-version; bh=5QR25vhJARn+GEdVKuMsLbV4zV7XeokyqFf6z/5XmG4=; b=uYgqWbNRVbzw/dAWmkpqMwJ1zvHnFhiFHwxvP36koXgcfRtn8sWkKznRpkMtxxnDBX rfDpue8rydUhf/DiZ+WNSlfvzZluifdOC17r45Vj/KKbzgo674BTsLYwVDt5Lp+HLVfq K4X2MlGS4rsYV6uccHvzQxlVwCNiqolaVBT/6NuHlMZH1RLeWZnnpGdEWq5cVT7wFs3+ 9K5WrsIzVQz1kWTFitDU0qRST/fV4z6tKOjpYikFdflU+EY4xMczBmHSSat0sMsIOmT/ 3TcZMRCOwW/qJgKufDY5rfBx66kETd3xZzL2eyquUIExS6Mel475jB4SJHvkkgGA2M+b 45vg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:references:user-agent:to:cc:subject :message-id:in-reply-to:date:mime-version; bh=5QR25vhJARn+GEdVKuMsLbV4zV7XeokyqFf6z/5XmG4=; b=EPSq9742E7lkG5yDQWr+GBqfGhRgR4tC6ZeyKu4kUfaCsbHApu9DZT7mLOHrCHlrVr Y7bjgHb4cr1cKgbPf9srSNuO9uhBA8nIsKWklvFRMMMJeRqdNJhlTeN0sMM2P5LkUVXE YTTYXRMSjgoKfpobT67aZtTadVBNezA9Nr0mQA1ElgeiGLqt7Yb1xJhD93lqXbWGAUr7 FswT7FSs1Jy2MqJkd3gzAnVi+81sdbk7WKHcnTvRepYaApJpPmds/SNjTL4vz6mznXIJ R/AaffbTCQpqCbQGAkwquQuCMpCItPKcR3FqNQApCmeaXIpA0svQMVciWfub2ktIQEPa O0rg== X-Gm-Message-State: AKwxytft/k1+H38cd92NkRvDr8XIfUBsfmwVXI+4vFzCjbI46c7cEflp HdWCX2p+dhv4NIJFJjQRRvM= X-Received: by 10.28.178.147 with SMTP id b141mr21306027wmf.87.1517393014978; Wed, 31 Jan 2018 02:03:34 -0800 (PST) Received: from ptitpuce.local (val06-1-88-182-161-34.fbx.proxad.net. [88.182.161.34]) by smtp.gmail.com with ESMTPSA id x26sm10659301wmc.27.2018.01.31.02.03.32 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Wed, 31 Jan 2018 02:03:34 -0800 (PST) From: Christophe de Dinechin X-Google-Original-From: Christophe de Dinechin References: <1516476182-5153-1-git-send-email-karahmed@amazon.de> <1516476182-5153-6-git-send-email-karahmed@amazon.de> User-agent: mu4e 0.9.18; emacs 25.2.1 To: KarimAllah Ahmed Cc: linux-kernel@vger.kernel.org, Andi Kleen , Andrea Arcangeli , Andy Lutomirski , Arjan van de Ven , Ashok Raj , Asit Mallick , Borislav Petkov , Dan Williams , Dave Hansen , David Woodhouse , Greg Kroah-Hartman , "H . Peter Anvin" , Ingo Molnar , Janakarajan Natarajan , Joerg Roedel , Jun Nakajima , Laura Abbott , Linus Torvalds , Masami Hiramatsu , Paolo Bonzini , Peter Zijlstra , Radim =?utf-8?B?S3LEjW3DocWZ?= , Thomas Gleixner , Tim Chen , Tom Lendacky , kvm@vger.kernel.org, x86@kernel.org Subject: Re: [RFC 05/10] x86/speculation: Add basic IBRS support infrastructure Message-ID: In-reply-to: <1516476182-5153-6-git-send-email-karahmed@amazon.de> Date: Wed, 31 Jan 2018 11:03:21 +0100 MIME-Version: 1.0 Content-Type: text/plain Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org KarimAllah Ahmed writes: > From: David Woodhouse > > Not functional yet; just add the handling for it in the Spectre v2 > mitigation selection, and the X86_FEATURE_IBRS flag which will control > the code to be added in later patches. > > Also take the #ifdef CONFIG_RETPOLINE from around the RSB-stuffing; IBRS > mode will want that too. > > For now we are auto-selecting IBRS on Skylake. We will probably end up > changing that but for now let's default to the safest option. > > XX: Do we want a microcode blacklist? > > [karahmed: simplify the switch block and get rid of all the magic] > > Signed-off-by: David Woodhouse > Signed-off-by: KarimAllah Ahmed > --- > Documentation/admin-guide/kernel-parameters.txt | 1 + > arch/x86/include/asm/cpufeatures.h | 1 + > arch/x86/include/asm/nospec-branch.h | 2 - > arch/x86/kernel/cpu/bugs.c | 108 +++++++++++++++--------- > 4 files changed, 68 insertions(+), 44 deletions(-) > > diff --git a/Documentation/admin-guide/kernel-parameters.txt b/Documentation/admin-guide/kernel-parameters.txt > index 8122b5f..e597650 100644 > --- a/Documentation/admin-guide/kernel-parameters.txt > +++ b/Documentation/admin-guide/kernel-parameters.txt > @@ -3932,6 +3932,7 @@ > retpoline - replace indirect branches > retpoline,generic - google's original retpoline > retpoline,amd - AMD-specific minimal thunk > + ibrs - Intel: Indirect Branch Restricted Speculation > > Not specifying this option is equivalent to > spectre_v2=auto. > diff --git a/arch/x86/include/asm/cpufeatures.h b/arch/x86/include/asm/cpufeatures.h > index 8ec9588..ae86ad9 100644 > --- a/arch/x86/include/asm/cpufeatures.h > +++ b/arch/x86/include/asm/cpufeatures.h > @@ -211,6 +211,7 @@ > #define X86_FEATURE_AMD_PRED_CMD ( 7*32+17) /* Prediction Command MSR (AMD) */ > #define X86_FEATURE_MBA ( 7*32+18) /* Memory Bandwidth Allocation */ > #define X86_FEATURE_RSB_CTXSW ( 7*32+19) /* Fill RSB on context switches */ > +#define X86_FEATURE_IBRS ( 7*32+21) /* Use IBRS for Spectre v2 safety */ > > /* Virtualization flags: Linux defined, word 8 */ > #define X86_FEATURE_TPR_SHADOW ( 8*32+ 0) /* Intel TPR Shadow */ > diff --git a/arch/x86/include/asm/nospec-branch.h b/arch/x86/include/asm/nospec-branch.h > index c333c95..8759449 100644 > --- a/arch/x86/include/asm/nospec-branch.h > +++ b/arch/x86/include/asm/nospec-branch.h > @@ -205,7 +205,6 @@ extern char __indirect_thunk_end[]; > */ > static inline void vmexit_fill_RSB(void) > { > -#ifdef CONFIG_RETPOLINE > unsigned long loops; > > asm volatile (ANNOTATE_NOSPEC_ALTERNATIVE > @@ -215,7 +214,6 @@ static inline void vmexit_fill_RSB(void) > "910:" > : "=r" (loops), ASM_CALL_CONSTRAINT > : : "memory" ); > -#endif > } > > static inline void indirect_branch_prediction_barrier(void) > diff --git a/arch/x86/kernel/cpu/bugs.c b/arch/x86/kernel/cpu/bugs.c > index 96548ff..1d5e12f 100644 > --- a/arch/x86/kernel/cpu/bugs.c > +++ b/arch/x86/kernel/cpu/bugs.c > @@ -79,6 +79,7 @@ enum spectre_v2_mitigation_cmd { > SPECTRE_V2_CMD_RETPOLINE, > SPECTRE_V2_CMD_RETPOLINE_GENERIC, > SPECTRE_V2_CMD_RETPOLINE_AMD, > + SPECTRE_V2_CMD_IBRS, > }; > > static const char *spectre_v2_strings[] = { > @@ -87,6 +88,7 @@ static const char *spectre_v2_strings[] = { > [SPECTRE_V2_RETPOLINE_MINIMAL_AMD] = "Vulnerable: Minimal AMD ASM retpoline", > [SPECTRE_V2_RETPOLINE_GENERIC] = "Mitigation: Full generic retpoline", > [SPECTRE_V2_RETPOLINE_AMD] = "Mitigation: Full AMD retpoline", > + [SPECTRE_V2_IBRS] = "Mitigation: Indirect Branch Restricted Speculation", > }; > > #undef pr_fmt > @@ -132,9 +134,17 @@ static enum spectre_v2_mitigation_cmd __init spectre_v2_parse_cmdline(void) > spec2_print_if_secure("force enabled on command line."); > return SPECTRE_V2_CMD_FORCE; > } else if (match_option(arg, ret, "retpoline")) { > + if (!IS_ENABLED(CONFIG_RETPOLINE)) { > + pr_err("retpoline selected but not compiled in. Switching to AUTO select\n"); > + return SPECTRE_V2_CMD_AUTO; > + } > spec2_print_if_insecure("retpoline selected on command line."); > return SPECTRE_V2_CMD_RETPOLINE; > } else if (match_option(arg, ret, "retpoline,amd")) { > + if (!IS_ENABLED(CONFIG_RETPOLINE)) { > + pr_err("retpoline,amd selected but not compiled in. Switching to AUTO select\n"); > + return SPECTRE_V2_CMD_AUTO; > + } > if (boot_cpu_data.x86_vendor != X86_VENDOR_AMD) { > pr_err("retpoline,amd selected but CPU is not AMD. Switching to AUTO select\n"); > return SPECTRE_V2_CMD_AUTO; > @@ -142,8 +152,19 @@ static enum spectre_v2_mitigation_cmd __init spectre_v2_parse_cmdline(void) > spec2_print_if_insecure("AMD retpoline selected on command line."); > return SPECTRE_V2_CMD_RETPOLINE_AMD; > } else if (match_option(arg, ret, "retpoline,generic")) { > + if (!IS_ENABLED(CONFIG_RETPOLINE)) { > + pr_err("retpoline,generic selected but not compiled in. Switching to AUTO select\n"); > + return SPECTRE_V2_CMD_AUTO; > + } > spec2_print_if_insecure("generic retpoline selected on command line."); > return SPECTRE_V2_CMD_RETPOLINE_GENERIC; > + } else if (match_option(arg, ret, "ibrs")) { > + if (!boot_cpu_has(X86_FEATURE_SPEC_CTRL)) { > + pr_err("IBRS selected but no CPU support. Switching to AUTO select\n"); > + return SPECTRE_V2_CMD_AUTO; > + } > + spec2_print_if_insecure("IBRS seleted on command line."); > + return SPECTRE_V2_CMD_IBRS; > } else if (match_option(arg, ret, "auto")) { > return SPECTRE_V2_CMD_AUTO; > } > @@ -156,7 +177,7 @@ static enum spectre_v2_mitigation_cmd __init spectre_v2_parse_cmdline(void) > return SPECTRE_V2_CMD_NONE; > } > > -/* Check for Skylake-like CPUs (for RSB handling) */ > +/* Check for Skylake-like CPUs (for RSB and IBRS handling) */ > static bool __init is_skylake_era(void) > { > if (boot_cpu_data.x86_vendor == X86_VENDOR_INTEL && > @@ -178,55 +199,58 @@ static void __init spectre_v2_select_mitigation(void) > enum spectre_v2_mitigation_cmd cmd = spectre_v2_parse_cmdline(); > enum spectre_v2_mitigation mode = SPECTRE_V2_NONE; > > - /* > - * If the CPU is not affected and the command line mode is NONE or AUTO > - * then nothing to do. > - */ > - if (!boot_cpu_has_bug(X86_BUG_SPECTRE_V2) && > - (cmd == SPECTRE_V2_CMD_NONE || cmd == SPECTRE_V2_CMD_AUTO)) > - return; > - > switch (cmd) { > case SPECTRE_V2_CMD_NONE: > + if (boot_cpu_has_bug(X86_BUG_SPECTRE_V2)) > + pr_err("kernel not compiled with retpoline; no mitigation available!"); > return; > - > - case SPECTRE_V2_CMD_FORCE: > - /* FALLTRHU */ > - case SPECTRE_V2_CMD_AUTO: > - goto retpoline_auto; > - > - case SPECTRE_V2_CMD_RETPOLINE_AMD: > - if (IS_ENABLED(CONFIG_RETPOLINE)) > - goto retpoline_amd; > - break; > - case SPECTRE_V2_CMD_RETPOLINE_GENERIC: > - if (IS_ENABLED(CONFIG_RETPOLINE)) > - goto retpoline_generic; > + case SPECTRE_V2_CMD_IBRS: > + mode = SPECTRE_V2_IBRS; > + setup_force_cpu_cap(X86_FEATURE_IBRS); > break; > + case SPECTRE_V2_CMD_AUTO: > + if (!boot_cpu_has_bug(X86_BUG_SPECTRE_V2)) > + return; > + /* Fall through */ > + case SPECTRE_V2_CMD_FORCE: > + /* > + * If we have IBRS support, and either Skylake or !RETPOLINE, > + * then that's what we do. > + */ > + if (boot_cpu_has(X86_FEATURE_SPEC_CTRL) && > + (is_skylake_era() || !retp_compiler())) { As per Eduardo's comments and followups, it's unclear this will play well under virtualization. Putting this under a separate function with a name making it clear that what we care about is the host, not guest CPU. Under virtualization, you may want to force is_skylake() to return true (unless there is a way to get a more precise answer about the host CPU at that stage?) > + mode = SPECTRE_V2_IBRS; > + setup_force_cpu_cap(X86_FEATURE_IBRS); > + break; > + } > + /* Fall through */ Given the complexity of the decision and the number of fall-through cases, it's probably a good idea to add some printouts for system mgmt or debugging. > case SPECTRE_V2_CMD_RETPOLINE: > - if (IS_ENABLED(CONFIG_RETPOLINE)) > - goto retpoline_auto; > - break; > - } > - pr_err("kernel not compiled with retpoline; no mitigation available!"); > - return; > + case SPECTRE_V2_CMD_RETPOLINE_AMD: > + if (IS_ENABLED(CONFIG_RETPOLINE) && > + boot_cpu_data.x86_vendor == X86_VENDOR_AMD) { > + if (boot_cpu_has(X86_FEATURE_LFENCE_RDTSC)) { > + mode = retp_compiler() ? SPECTRE_V2_RETPOLINE_AMD : > + SPECTRE_V2_RETPOLINE_MINIMAL_AMD; > + setup_force_cpu_cap(X86_FEATURE_RETPOLINE_AMD); > + setup_force_cpu_cap(X86_FEATURE_RETPOLINE); > + break; > + } > > -retpoline_auto: > - if (boot_cpu_data.x86_vendor == X86_VENDOR_AMD) { > - retpoline_amd: > - if (!boot_cpu_has(X86_FEATURE_LFENCE_RDTSC)) { > pr_err("LFENCE not serializing. Switching to generic retpoline\n"); > - goto retpoline_generic; > } > - mode = retp_compiler() ? SPECTRE_V2_RETPOLINE_AMD : > - SPECTRE_V2_RETPOLINE_MINIMAL_AMD; > - setup_force_cpu_cap(X86_FEATURE_RETPOLINE_AMD); > - setup_force_cpu_cap(X86_FEATURE_RETPOLINE); > - } else { > - retpoline_generic: > - mode = retp_compiler() ? SPECTRE_V2_RETPOLINE_GENERIC : > - SPECTRE_V2_RETPOLINE_MINIMAL; > - setup_force_cpu_cap(X86_FEATURE_RETPOLINE); > + /* Fall through */ > + case SPECTRE_V2_CMD_RETPOLINE_GENERIC: > + if (IS_ENABLED(CONFIG_RETPOLINE)) { > + mode = retp_compiler() ? SPECTRE_V2_RETPOLINE_GENERIC : > + SPECTRE_V2_RETPOLINE_MINIMAL; > + setup_force_cpu_cap(X86_FEATURE_RETPOLINE); > + break; > + } > + /* Fall through */ > + default: > + if (boot_cpu_has_bug(X86_BUG_SPECTRE_V2)) > + pr_err("kernel not compiled with retpoline; no mitigation available!"); > + return; > } > > spectre_v2_enabled = mode; -- Cheers, Christophe de Dinechin (IRC c3d)