Received: by 10.223.176.46 with SMTP id f43csp1125840wra; Sat, 20 Jan 2018 11:26:10 -0800 (PST) X-Google-Smtp-Source: AH8x224eXlnwwYFMAdPNV3IUoNEV/Yy54waYeY+3p9T55KYoJIDChguRUPJRJzE0eR9x7xC8Ky8B X-Received: by 10.99.125.74 with SMTP id m10mr2848567pgn.354.1516476370788; Sat, 20 Jan 2018 11:26:10 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1516476370; cv=none; d=google.com; s=arc-20160816; b=0SuwCmu8XUqdwmmnM3Hnxh48IKA7G5F7gQXek9UODOzvEeQJSyexeufL3adUg/lbw1 ng3HfaGilg3GYW4oPdRBJCeqhKHcq5FMmhCtkqLv1FGocQ1lKnjNdyDqc5cPWq7tpqdn IcZ0mnJ+JPfT7ijKbrm0i8HsJ3T907YZWJcbPiR1Au6qNa4SN6zoYXYOV1D5zApCl6aO wGhMR4q1b+5R3GTVX6DSqXjEg7I8Jtw3YRoCCCtjSrUlE+/8wgUnPHLpGdFBQAwsd/jd wMw2tlD6hdL/AKx4fnPwlrsLmbl5GDXXkkFieEGMScPtALPgntGAMCHyNVdU/+hsURsN zYhg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:dkim-signature:arc-authentication-results; bh=ZUBs/+GJjylx+r7QkS+h3/vptWiEKpiLTyKIpNbgITM=; b=N2FyDgAhlHT8EX8iOEY4H2T4Bsf0eR02ZWddHywhmm7Ss+tBdU24W/iNQQTywdGH0X KLMQu7VubhD/tr3whQaraFtt0CS9sEZvoqQK7B/MkwQHQC0KyrTwgRl2KDsSu5NWVUjG Ebrgvp59p+REsHBl2c3XkD3bDZ0BTLaQ23rbYPruiendkFn+AHReXoeTQ5UVBu4R/4av KRtLh12e1cfNTjAFH/uiUxMV+qmN3XjM7xq+kff5YDUdWRF/lKWbG944nqhh19j2gCKb faYUBGdd9/64YcrGi5p+H0UZkFbtgIsSKiGqF1AOxUZp1Bw3mIMH5M3szxb2X6qcN2HL m2Cw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@amazon.de header.s=amazon201209 header.b=qv01TzNN; 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=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amazon.de Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id e22si7255369pfl.178.2018.01.20.11.25.57; Sat, 20 Jan 2018 11:26:10 -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=@amazon.de header.s=amazon201209 header.b=qv01TzNN; 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=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amazon.de Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S932894AbeATTYm (ORCPT + 99 others); Sat, 20 Jan 2018 14:24:42 -0500 Received: from smtp-fw-33001.amazon.com ([207.171.190.10]:49196 "EHLO smtp-fw-33001.amazon.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S932546AbeATTXq (ORCPT ); Sat, 20 Jan 2018 14:23:46 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.de; i=@amazon.de; q=dns/txt; s=amazon201209; t=1516476226; x=1548012226; h=from:to:cc:subject:date:message-id:in-reply-to: references; bh=ZUBs/+GJjylx+r7QkS+h3/vptWiEKpiLTyKIpNbgITM=; b=qv01TzNNHUp5nXq62sjWYYDsWjsuy/Z3abcP7N4B8omuqUr3CM6q5dAF OalH2080TD9tDnN7+S1yEW1j30TOXpElLVCi8ppOsjEAo1yh+By6rPRMi qEIqpW1T/a9iOuaqzHK/ZTki5rH+/U9XUfHulL+SfLq52oICpvQU7gqxL o=; X-IronPort-AV: E=Sophos;i="5.46,387,1511827200"; d="scan'208";a="716423186" Received: from sea3-co-svc-lb6-vlan2.sea.amazon.com (HELO email-inbound-relay-2b-55156cd4.us-west-2.amazon.com) ([10.47.22.34]) by smtp-border-fw-out-33001.sea14.amazon.com with ESMTP/TLS/DHE-RSA-AES256-SHA; 20 Jan 2018 19:23:45 +0000 Received: from u54e1ad5160425a4b64ea.ant.amazon.com (pdx2-ws-svc-lb17-vlan3.amazon.com [10.247.140.70]) by email-inbound-relay-2b-55156cd4.us-west-2.amazon.com (8.14.7/8.14.7) with ESMTP id w0KJNe20013016 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO); Sat, 20 Jan 2018 19:23:41 GMT Received: from u54e1ad5160425a4b64ea.ant.amazon.com (localhost [127.0.0.1]) by u54e1ad5160425a4b64ea.ant.amazon.com (8.15.2/8.15.2/Debian-3) with ESMTP id w0KJNbGt005283; Sat, 20 Jan 2018 20:23:38 +0100 Received: (from karahmed@localhost) by u54e1ad5160425a4b64ea.ant.amazon.com (8.15.2/8.15.2/Submit) id w0KJNasW005280; Sat, 20 Jan 2018 20:23:36 +0100 From: KarimAllah Ahmed To: linux-kernel@vger.kernel.org Cc: KarimAllah Ahmed , 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 , =?UTF-8?q?Radim=20Kr=C4=8Dm=C3=A1=C5=99?= , Thomas Gleixner , Tim Chen , Tom Lendacky , kvm@vger.kernel.org, x86@kernel.org Subject: [RFC 07/10] x86: Simplify spectre_v2 command line parsing Date: Sat, 20 Jan 2018 20:22:58 +0100 Message-Id: <1516476182-5153-8-git-send-email-karahmed@amazon.de> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1516476182-5153-1-git-send-email-karahmed@amazon.de> References: <1516476182-5153-1-git-send-email-karahmed@amazon.de> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Signed-off-by: KarimAllah Ahmed --- arch/x86/kernel/cpu/bugs.c | 106 +++++++++++++++++++++++++-------------------- 1 file changed, 58 insertions(+), 48 deletions(-) diff --git a/arch/x86/kernel/cpu/bugs.c b/arch/x86/kernel/cpu/bugs.c index 1d5e12f..349c7f4 100644 --- a/arch/x86/kernel/cpu/bugs.c +++ b/arch/x86/kernel/cpu/bugs.c @@ -99,13 +99,13 @@ static enum spectre_v2_mitigation spectre_v2_enabled = SPECTRE_V2_NONE; static void __init spec2_print_if_insecure(const char *reason) { if (boot_cpu_has_bug(X86_BUG_SPECTRE_V2)) - pr_info("%s\n", reason); + pr_info("%s selected on command line.\n", reason); } static void __init spec2_print_if_secure(const char *reason) { if (!boot_cpu_has_bug(X86_BUG_SPECTRE_V2)) - pr_info("%s\n", reason); + pr_info("%s selected on command line.\n", reason); } static inline bool retp_compiler(void) @@ -120,61 +120,71 @@ static inline bool match_option(const char *arg, int arglen, const char *opt) return len == arglen && !strncmp(arg, opt, len); } +static struct { + char *option; + enum spectre_v2_mitigation_cmd cmd; + bool secure; +} mitigation_options[] = { + { "off", SPECTRE_V2_CMD_NONE, false }, + { "on", SPECTRE_V2_CMD_FORCE, true }, + { "retpoline", SPECTRE_V2_CMD_RETPOLINE, false }, + { "retpoline,amd", SPECTRE_V2_CMD_RETPOLINE_AMD, false }, + { "retpoline,generic", SPECTRE_V2_CMD_RETPOLINE_GENERIC, false }, + { "ibrs", SPECTRE_V2_CMD_IBRS, false }, + { "auto", SPECTRE_V2_CMD_AUTO, false }, +}; + +static const int mitigation_options_count = sizeof(mitigation_options) / + sizeof(mitigation_options[0]); + static enum spectre_v2_mitigation_cmd __init spectre_v2_parse_cmdline(void) { char arg[20]; - int ret; + int ret, i; + enum spectre_v2_mitigation_cmd cmd = SPECTRE_V2_CMD_AUTO; + + if (cmdline_find_option_bool(boot_command_line, "nospectre_v2")) + return SPECTRE_V2_CMD_NONE; ret = cmdline_find_option(boot_command_line, "spectre_v2", arg, sizeof(arg)); - if (ret > 0) { - if (match_option(arg, ret, "off")) { - goto disable; - } else if (match_option(arg, ret, "on")) { - 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; - } - 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; - } + if (ret < 0) + return SPECTRE_V2_CMD_AUTO; + + for (i = 0; i < mitigation_options_count; i++) { + if (!match_option(arg, ret, mitigation_options[i].option)) + continue; + cmd = mitigation_options[i].cmd; + break; } - if (!cmdline_find_option_bool(boot_command_line, "nospectre_v2")) + if (i >= mitigation_options_count) { + pr_err("unknown option (%s). Switching to AUTO select\n", + mitigation_options[i].option); return SPECTRE_V2_CMD_AUTO; -disable: - spec2_print_if_insecure("disabled on command line."); - return SPECTRE_V2_CMD_NONE; + } + + if ((cmd == SPECTRE_V2_CMD_RETPOLINE || + cmd == SPECTRE_V2_CMD_RETPOLINE_AMD || + cmd == SPECTRE_V2_CMD_RETPOLINE_GENERIC) && + !IS_ENABLED(CONFIG_RETPOLINE)) { + pr_err("%s selected but not compiled in. Switching to AUTO select\n", + mitigation_options[i].option); + return SPECTRE_V2_CMD_AUTO; + } + + if (cmd == SPECTRE_V2_CMD_RETPOLINE_AMD && + 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; + } + + if (mitigation_options[i].secure) + spec2_print_if_secure(mitigation_options[i].option); + else + spec2_print_if_insecure(mitigation_options[i].option); + + return cmd; } /* Check for Skylake-like CPUs (for RSB and IBRS handling) */ -- 2.7.4