Received: by 10.223.176.5 with SMTP id f5csp2624770wra; Thu, 1 Feb 2018 03:30:35 -0800 (PST) X-Google-Smtp-Source: AH8x224rpCA3L+pwyJBjIHqyAmiNTU7/mNO9qe4Nj4r6Td5tmHTMFDHaSgUauwUdr+3JKD7i23ed X-Received: by 10.99.165.28 with SMTP id n28mr1646970pgf.103.1517484635872; Thu, 01 Feb 2018 03:30:35 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1517484635; cv=none; d=google.com; s=arc-20160816; b=CWGOJVYG/iKrBUQ49yOcR+Kh/9nlbsLgN0s4WxDT6/zyy4eMIDkY+WvRWdBfDOkpqi XvNIN76vaZWmCgcUKP3mePt+DIqth13V22ABA6gdIrjQV4PRJbdqpvcZoZ64WP8EyLDs vXB5fI+QBmLBKyXjoHzAC+J72p1v4NHuVLRUdcYo9Uo0TJNPA7Mkbjf2NRgKKcmRCp2+ 9Piv6mNRhkUBFxFPPUtdW0LNliJ3g7gezA3OvL8rJOLnxNa3ZDTT9075VAdHqWeJYduV /GMCOJZ6Zf7sIKjHr2JlzCAlnhLcChUTa775F8UbI52f8Z5/F3jWAyi2/yCZ+Nu7Fxf5 H7uQ== 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:to:from:dkim-signature:arc-authentication-results; bh=rvAniTn1Dz6oVMtiHEziwB58i57cyopwtcbUMr3IKL4=; b=lTsbjZEATBZMpGq9Sp6OUrweRW6tPJ2/ese1rGI0MjqJKr/f1UiQE+VyQJoKaTrBXy wSjDEjg6vYbaVeYu3+0i4d5SYg8sEBcaCW8xpw2FSvRj29KZOxWDOeVZ+7AitJmT6NN/ Ycd5TALC4MM87YfviCxXibCRI9TaIgqpqXt/XozuHESpNipP5aMvhMxafuCY+KeTPgWe LVwj4qHC9gZpf5KKOaLqeAl3C78ixC7uO+CwmqeVHt1otk3ooY2hyn0hRw0ZVtpnYgYB yHKOB7+H3NKgQN23Zomaso28zvwcbi49jsFxB/kK3Ol1MkQ/IX14CnyHsrWCLnc3ORU0 zK/Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@amazon.co.uk header.s=amazon201209 header.b=qCykNOba; 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.co.uk Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id t10si1493073pgv.34.2018.02.01.03.30.20; Thu, 01 Feb 2018 03:30:35 -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.co.uk header.s=amazon201209 header.b=qCykNOba; 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.co.uk Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752299AbeBAL31 (ORCPT + 99 others); Thu, 1 Feb 2018 06:29:27 -0500 Received: from smtp-fw-2101.amazon.com ([72.21.196.25]:11686 "EHLO smtp-fw-2101.amazon.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751772AbeBAL3Z (ORCPT ); Thu, 1 Feb 2018 06:29:25 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.co.uk; i=@amazon.co.uk; q=dns/txt; s=amazon201209; t=1517484566; x=1549020566; h=from:to:subject:date:message-id:in-reply-to:references; bh=rvAniTn1Dz6oVMtiHEziwB58i57cyopwtcbUMr3IKL4=; b=qCykNObayBey6odqxSbkxRUiRNs9OMsf4Fk+ipAyRxVgLX4kgRMYLax4 eC+n3sLi+tb5XEwMSMdDB2WBv5i0/IEyrWzQVeCqxrfEPlSf/0T51whIL 13A52/OqXklbjX+G3DN61AyIFOFYt+10Hi+qmPeipvY8Ekp0vSeBNp/jj c=; X-IronPort-AV: E=Sophos;i="5.46,443,1511827200"; d="scan'208";a="666033793" Received: from iad6-co-svc-p1-lb1-vlan2.amazon.com (HELO email-inbound-relay-2a-e7be2041.us-west-2.amazon.com) ([10.124.125.2]) by smtp-border-fw-out-2101.iad2.amazon.com with ESMTP/TLS/DHE-RSA-AES256-SHA; 01 Feb 2018 11:27:55 +0000 Received: from uc8d3ff76b9bc5848a9cc.ant.amazon.com (pdx2-ws-svc-lb17-vlan3.amazon.com [10.247.140.70]) by email-inbound-relay-2a-e7be2041.us-west-2.amazon.com (8.14.7/8.14.7) with ESMTP id w11BRVJs063373 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO); Thu, 1 Feb 2018 11:27:32 GMT Received: from uc8d3ff76b9bc5848a9cc.ant.amazon.com (localhost [127.0.0.1]) by uc8d3ff76b9bc5848a9cc.ant.amazon.com (8.15.2/8.15.2/Debian-3) with ESMTP id w11BRUU9001541; Thu, 1 Feb 2018 11:27:30 GMT Received: (from dwmw@localhost) by uc8d3ff76b9bc5848a9cc.ant.amazon.com (8.15.2/8.15.2/Submit) id w11BRTgP001540; Thu, 1 Feb 2018 11:27:29 GMT From: David Woodhouse To: tglx@linutronix.de, karahmed@amazon.de, x86@kernel.org, linux-kernel@vger.kernel.org, bp@alien8.de, peterz@infradead.org Subject: [PATCH 2/2] x86: Simplify spectre_v2 command line parsing Date: Thu, 1 Feb 2018 11:27:21 +0000 Message-Id: <1517484441-1420-3-git-send-email-dwmw@amazon.co.uk> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1517484441-1420-1-git-send-email-dwmw@amazon.co.uk> References: <1517484441-1420-1-git-send-email-dwmw@amazon.co.uk> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: KarimAllah Ahmed [dwmw2: Use ARRAY_SIZE] Signed-off-by: KarimAllah Ahmed Signed-off-by: David Woodhouse --- 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) */ -- 2.7.4