Received: by 10.223.176.5 with SMTP id f5csp641285wra; Fri, 2 Feb 2018 03:49:46 -0800 (PST) X-Google-Smtp-Source: AH8x224O2EytR0tYrsQ6YMoNEpg7pL8LxgiCrS62Kk766rr2iysEj4Iu5MD0eG2ZRae7SGUBuC24 X-Received: by 10.99.96.216 with SMTP id u207mr4148208pgb.167.1517572185849; Fri, 02 Feb 2018 03:49:45 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1517572185; cv=none; d=google.com; s=arc-20160816; b=C55UVljR3Vi1dx0TPIAMZ7Kw3B+dpoRr62uOkJ3funYyqThAJbgCUav6n4hRvyRS+K 5LMj9N2P7Vr+Bo7FaGKZZ58lJHORhHcF8hT+ND5C4f6KdOvZOD+W59LAeoehiP88WmmX V7rLHcUW3v4ysA+4B3YhYiIbDn5TyiaCf13ze//CMkkZLEnPpANR2HPH8+w9w8QYvJSl 1ddWSaprNTCjKbHTKL9pxMbxLBC3XPgaTyKfc5FNbCpPBT43eobVZ0m3fBFOOYQqqHuG /HaRYkYBJkEqCnZFHYaVRoE1QnP1dMLW8Iq4tpoID3bF6JIs1kE47Tie8pu4Hwo9sJIf 0I3g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-disposition :content-transfer-encoding:mime-version:robot-unsubscribe:robot-id :git-commit-id:subject:to:references:in-reply-to:reply-to:cc :message-id:from:date:arc-authentication-results; bh=dCSQ3ew5zbG8ac82KIE4d4qMlkBjrlP4daNWc3vSaS8=; b=CnceDZ9nWaOSnfIa/pQz8BezXMV/Ozgx0yoZz0ZJBeWUj8mCtRuipFrZcsdauswquS yGVTiY5DTps0V1MibBfVKDhH28zkCr+NjC9flsU0q5DzXV+gcEEdFoIxJ4uvM8avLJ2K e/R52aCS4PC3Nx9DZSnqIBwvERACJI8N9iR/Srm7281Yxesd582gW+AqY8vFFOkQ6z02 JlcxZ1Eqvbp+7Fk9fedb6gbrWAX/GglSHqCPJEyQ/B3delpIoBONhY1bquWEGiCcluG4 OpPPDAAESKO2YvA8Fb1bOTvZqisLDNsB/OjH6QZbku6/i830D2VeQ0YKtMPIXIzGYPbq teqA== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id d15-v6si89740pli.317.2018.02.02.03.49.31; Fri, 02 Feb 2018 03:49:45 -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; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751728AbeBBLsX (ORCPT + 99 others); Fri, 2 Feb 2018 06:48:23 -0500 Received: from terminus.zytor.com ([65.50.211.136]:41715 "EHLO terminus.zytor.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751656AbeBBLsQ (ORCPT ); Fri, 2 Feb 2018 06:48:16 -0500 Received: from terminus.zytor.com (localhost [127.0.0.1]) by terminus.zytor.com (8.15.2/8.15.2) with ESMTP id w12Bl9MR005444; Fri, 2 Feb 2018 03:47:09 -0800 Received: (from tipbot@localhost) by terminus.zytor.com (8.15.2/8.15.2/Submit) id w12Bl842005441; Fri, 2 Feb 2018 03:47:08 -0800 Date: Fri, 2 Feb 2018 03:47:08 -0800 X-Authentication-Warning: terminus.zytor.com: tipbot set sender to tipbot@zytor.com using -f From: tip-bot for KarimAllah Ahmed Message-ID: Cc: linux-kernel@vger.kernel.org, mingo@kernel.org, dwmw@amazon.co.uk, karahmed@amazon.de, hpa@zytor.com, tglx@linutronix.de Reply-To: mingo@kernel.org, linux-kernel@vger.kernel.org, karahmed@amazon.de, tglx@linutronix.de, hpa@zytor.com, dwmw@amazon.co.uk In-Reply-To: <1517484441-1420-3-git-send-email-dwmw@amazon.co.uk> References: <1517484441-1420-3-git-send-email-dwmw@amazon.co.uk> To: linux-tip-commits@vger.kernel.org Subject: [tip:x86/pti] x86/spectre: Simplify spectre_v2 command line parsing Git-Commit-ID: 9005c6834c0ffdfe46afa76656bd9276cca864f6 X-Mailer: tip-git-log-daemon Robot-ID: Robot-Unsubscribe: Contact to get blacklisted from these emails MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset=UTF-8 Content-Disposition: inline X-Spam-Status: No, score=-2.9 required=5.0 tests=ALL_TRUSTED,BAYES_00 autolearn=ham autolearn_force=no version=3.4.1 X-Spam-Checker-Version: SpamAssassin 3.4.1 (2015-04-28) on terminus.zytor.com Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Commit-ID: 9005c6834c0ffdfe46afa76656bd9276cca864f6 Gitweb: https://git.kernel.org/tip/9005c6834c0ffdfe46afa76656bd9276cca864f6 Author: KarimAllah Ahmed AuthorDate: Thu, 1 Feb 2018 11:27:21 +0000 Committer: Thomas Gleixner CommitDate: Fri, 2 Feb 2018 12:28:27 +0100 x86/spectre: Simplify spectre_v2 command line parsing [dwmw2: Use ARRAY_SIZE] Signed-off-by: KarimAllah Ahmed Signed-off-by: David Woodhouse Signed-off-by: Thomas Gleixner Cc: peterz@infradead.org Cc: bp@alien8.de Link: https://lkml.kernel.org/r/1517484441-1420-3-git-send-email-dwmw@amazon.co.uk --- arch/x86/kernel/cpu/bugs.c | 86 ++++++++++++++++++++++++++++++---------------- 1 file changed, 56 insertions(+), 30 deletions(-) diff --git a/arch/x86/kernel/cpu/bugs.c b/arch/x86/kernel/cpu/bugs.c index dd3a3cc..71949bf 100644 --- a/arch/x86/kernel/cpu/bugs.c +++ b/arch/x86/kernel/cpu/bugs.c @@ -119,13 +119,13 @@ static inline const char *spectre_v2_module_string(void) { return ""; } 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) @@ -140,42 +140,68 @@ static inline bool match_option(const char *arg, int arglen, const char *opt) return len == arglen && !strncmp(arg, opt, len); } +static const struct { + const 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 }, + { "auto", SPECTRE_V2_CMD_AUTO, false }, +}; + static enum spectre_v2_mitigation_cmd __init spectre_v2_parse_cmdline(void) { char arg[20]; - int ret; - - 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")) { - spec2_print_if_insecure("retpoline selected on command line."); - return SPECTRE_V2_CMD_RETPOLINE; - } else if (match_option(arg, ret, "retpoline,amd")) { - 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")) { - spec2_print_if_insecure("generic retpoline selected on command line."); - return SPECTRE_V2_CMD_RETPOLINE_GENERIC; - } else if (match_option(arg, ret, "auto")) { + 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; + else { + ret = cmdline_find_option(boot_command_line, "spectre_v2", arg, + sizeof(arg)); + if (ret < 0) + return SPECTRE_V2_CMD_AUTO; + + for (i = 0; i < ARRAY_SIZE(mitigation_options); i++) { + if (!match_option(arg, ret, mitigation_options[i].option)) + continue; + cmd = mitigation_options[i].cmd; + break; + } + + if (i >= ARRAY_SIZE(mitigation_options)) { + pr_err("unknown option (%s). Switching to AUTO select\n", + mitigation_options[i].option); return SPECTRE_V2_CMD_AUTO; } } - if (!cmdline_find_option_bool(boot_command_line, "nospectre_v2")) + 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; -disable: - spec2_print_if_insecure("disabled on command line."); - return SPECTRE_V2_CMD_NONE; + } + + 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 handling) */