Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754835AbeAJAjn (ORCPT + 1 other); Tue, 9 Jan 2018 19:39:43 -0500 Received: from mail-sn1nam02on0057.outbound.protection.outlook.com ([104.47.36.57]:64144 "EHLO NAM02-SN1-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1752584AbeAJAjk (ORCPT ); Tue, 9 Jan 2018 19:39:40 -0500 Authentication-Results: spf=none (sender IP is ) smtp.mailfrom=Thomas.Lendacky@amd.com; From: Tom Lendacky Subject: [PATCH v2] x86/retpoline: Only set RETPOLINE_AMD if LFENCE is serializing To: x86@kernel.org, linux-kernel@vger.kernel.org Cc: Rik van Riel , Andi Kleen , Peter Zijlstra , Linus Torvalds , Jiri Kosina , Dan Williams , Dave Hansen , Borislav Petkov , Andy Lutomirski , Kees Cook , Thomas Gleixner , Tim Chen , Greg Kroah-Hartman , David Woodhouse , Paul Turner Date: Tue, 09 Jan 2018 18:39:31 -0600 Message-ID: <20180110003931.32411.55646.stgit@tlendack-t1.amdoffice.net> User-Agent: StGit/0.17.1-dirty MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit X-Originating-IP: [165.204.77.1] X-ClientProxiedBy: CY4PR03CA0101.namprd03.prod.outlook.com (10.171.242.170) To CY4PR12MB1141.namprd12.prod.outlook.com (10.168.163.149) X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-HT: Tenant X-MS-Office365-Filtering-Correlation-Id: 72ea6974-b0e8-44f2-e44d-08d557c29f35 X-Microsoft-Antispam: UriScan:;BCL:0;PCL:0;RULEID:(48565401081)(4534020)(4602075)(4627115)(201703031133081)(201702281549075)(5600026)(4604075)(2017052603307)(7153060)(7193020);SRVR:CY4PR12MB1141; X-Microsoft-Exchange-Diagnostics: 1;CY4PR12MB1141;3:lomOItrRGdMqpYONT4bnn3AfqgV0PxUkod3ltXs2MQe4XxsL/PhY3z3/i1V6RchuLMh0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yFX3iwnZ5P78Fz8Q6bId65jrKublEgt8asitf2U5tSLnBe7FiEK6XZpn5aHl/cpid2hvZ2S4t3/F/yAwXKz2LGjqrVmR+6IUczmRtwSMHSePnwLaLhDudP8hSt4pSrbmzsBQwV4qsZpj2oZb4mqexB2+VW3f8E4/MovCDQDN3BjOdEc1Zi+Zs7woLzYuj2nc+WYMiGyboGfK5VmZN8MB+TT5dgIDcg4Cvpt0r5MdGdKlYmp91CQHubj7KwiD5EyYmzHKpLkdIqoPB;4:CQb7z3EUZQQiybR547HjryYbLNrGIXlDn/VYfwxNPd2DplyL8/dpQcLM45nMhSEaafdhJfWZhiYbtyZcKJcCK5VLTkUVZaw8d4jV7i2Ffj6EF4YYkN4/7FBrF3/me/Yl7dF7u4oEkmEovHmzw/wAYoxVGe7VlGjNLAMF72UAZL2Zbq3qhnGWp3GrjE2mnk3sscFtiTWivVnKr4EUNeQl5+Hmqc8oNAqJJfoAnO8lYw0/+DCxsS2saMspHr3Pugw8XyZC7TA3zU/T6OL1JhHoBcxeSZOlpVLxj+E2kEWDwm3tNPQSa5yraiLsIN2u80dX X-Microsoft-Antispam-PRVS: X-Exchange-Antispam-Report-Test: UriScan:(767451399110); X-Exchange-Antispam-Report-CFA-Test: BCL:0;PCL:0;RULEID:(6040470)(2401047)(8121501046)(5005006)(93006095)(93001095)(3002001)(10201501046)(3231023)(944501075)(6055026)(6041268)(20161123560045)(201703131423095)(201702281528075)(20161123555045)(201703061421075)(201703061406153)(20161123562045)(20161123558120)(20161123564045)(6072148)(201708071742011);SRVR:CY4PR12MB1141;BCL:0;PCL:0;RULEID:(100000803101)(100110400095);SRVR:CY4PR12MB1141; X-Forefront-PRVS: 0548586081 X-Forefront-Antispam-Report: SFV:NSPM;SFS:(10009020)(979002)(376002)(396003)(346002)(366004)(39860400002)(39380400002)(199004)(189003)(68736007)(305945005)(6666003)(316002)(3846002)(7416002)(97736004)(9686003)(25786009)(50466002)(86362001)(6116002)(47776003)(69596002)(1076002)(55016002)(58126008)(53936002)(7736002)(54906003)(105586002)(53416004)(8676002)(7696005)(16526018)(52116002)(23676004)(97746001)(478600001)(2486003)(72206003)(230700001)(83506002)(81156014)(2906002)(386003)(81166006)(6506007)(8936002)(4326008)(66066001)(106356001)(103116003)(5660300001)(969003)(989001)(999001)(1009001)(1019001);DIR:OUT;SFP:1101;SCL:1;SRVR:CY4PR12MB1141;H:tlendack-t1.amdoffice.net;FPR:;SPF:None;PTR:InfoNoRecords;A:1;MX:1;LANG:en; X-Microsoft-Exchange-Diagnostics: =?utf-8?B?MTtDWTRQUjEyTUIxMTQxOzIzOkxmaGN5VnhESVhDL2xGeEZDWUtGK0lwS1gx?= =?utf-8?B?NlFsZ25VOG5DaStjOGtmTzNSV2kvcDkxRHA1NDNFNDc5ZVZCcy9seTFxeGNj?= =?utf-8?B?UzNJZnlYTDg1NUJMLzRNRS9FYVdUUytIUWt0MDRQRXpxdmJvYXZXanByWVB5?= =?utf-8?B?TXB4WUI0MW83VHdHY1BQQk0rNnVCaFg0UVJVUmpveUkzaXF1UnZTWVBIM0l4?= =?utf-8?B?b0F6Nml0enpzNWNhOWJ4WTBVY0VOOVBMR0d1YmNrSklPa0x6TTdxVHI5SUI0?= =?utf-8?B?eVQ4SEFqZWpxOUZpdGlJakJjQTliZGcyU1IrQ0o2WDgrWUlwQWx6M00xaXh4?= =?utf-8?B?UTB0VU9pYXdmRVRrT1NXZnVzdHRZU0tqa0crd2ZjczdLek4yQW5WZ0cyMXNa?= =?utf-8?B?ZDVPR0ZIMERrM3k0dEV3UmZYVy8zMFFkSWRiRjJSelVvN2hKdkVkMHpOeXl0?= =?utf-8?B?U2h0Y2Faa1Z0dmFEclF4N3ZsbVFscnpGeXpQdXUvSHRaQi81MUZ6RTBQWXh2?= =?utf-8?B?eER3VnFoMUFRbktEMkNlMkNLdWt1ZlpzU3g0WEoxN1hNbXYyazQ0NFl3d1Rn?= =?utf-8?B?aDQvZkZ1TlA5Wms2Zm1PYi9ydWdwNWVZK3pQ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w3fgWn1wX1qzYu3YzivD7F5AWWCGbqd0blAwad73Pd9juRuBj68vQFtJBilOa83BVjRgWmTqyVAMPcWuwlELwkLEGKh3wOvtJ/sssF4gWIdUB3XNYlKxGju8/fgLWAFi3zWP/VWGvXd8HoVKRWz0RiZxS5ii4CVAAqL6ldTjY29omruNj0Q0HvPb SpamDiagnosticOutput: 1:99 SpamDiagnosticMetadata: NSPM X-Microsoft-Exchange-Diagnostics: 1;CY4PR12MB1141;20:Ib9+u6X/m6tJPck/FL4jwISLjihz9IDi4lGf6NJAGE8eZa0R/BFQk/7S7bp6U9WLV6de56AbhWtoknRgppulJypa1vA/NNAfprXHrf7dumiEW9FSJ52Ktmij63964hZ0FrxLvy/oDQJs1viD2btrHr6kNads+zsyIx1zyl8f2nhPkIJvWp0NUnRaz5oUsYgz5TwcROvsGIyZMKsVnEBKG9euSXJbzQ48KiVD/osI23l0dPpa+CwFORHV+9erjhHt X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 10 Jan 2018 00:39:34.3236 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 72ea6974-b0e8-44f2-e44d-08d557c29f35 X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-Transport-CrossTenantHeadersStamped: CY4PR12MB1141 Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Return-Path: The RETPOLINE_AMD feature relies on a serializing LFENCE for speculation control. For AMD hardware, only set RETPOLINE_AMD if LFENCE is a serializing instruction, which is indicated by the LFENCE_RDTSC feature. The call to spectre_v2_check_boottime_disable() is currently before the boot CPU is identified and, therefore, able to set the LFENCE_RDTSC feature. Move the call to spectre_v2_check_boottime_disable() to after identify_boot_cpu() in check_bugs(). Also, protect against specifying spectre_v2=retpoline,amd for non-AMD hardware and fall-back to the generic retpoline. Signed-off-by: Thomas Gleixner Signed-off-by: Tom Lendacky --- arch/x86/include/asm/nospec-branch.h | 2 -- arch/x86/kernel/cpu/bugs.c | 22 +++++++++++++++++----- arch/x86/kernel/setup.c | 2 -- 3 files changed, 17 insertions(+), 9 deletions(-) diff --git a/arch/x86/include/asm/nospec-branch.h b/arch/x86/include/asm/nospec-branch.h index 8ddf851..6bda2c0 100644 --- a/arch/x86/include/asm/nospec-branch.h +++ b/arch/x86/include/asm/nospec-branch.h @@ -152,7 +152,5 @@ # define THUNK_TARGET(addr) [thunk_target] "rm" (addr) #endif -void spectre_v2_check_boottime_disable(void); - #endif /* __ASSEMBLY__ */ #endif /* __NOSPEC_BRANCH_H__ */ diff --git a/arch/x86/kernel/cpu/bugs.c b/arch/x86/kernel/cpu/bugs.c index b957f77..815dee2 100644 --- a/arch/x86/kernel/cpu/bugs.c +++ b/arch/x86/kernel/cpu/bugs.c @@ -24,6 +24,8 @@ #include #include +static void __init spectre_v2_check_boottime_disable(void); + void __init check_bugs(void) { identify_boot_cpu(); @@ -33,6 +35,9 @@ void __init check_bugs(void) print_cpu_info(&boot_cpu_data); } + /* Select the proper spectre mitigation before patching alternatives */ + spectre_v2_check_boottime_disable(); + #ifdef CONFIG_X86_32 /* * Check whether we are able to run this kernel safely on SMP. @@ -106,7 +111,7 @@ static inline bool match_option(const char *arg, int arglen, const char *opt) return len == arglen && !strncmp(arg, opt, len); } -void __init spectre_v2_check_boottime_disable(void) +static void __init spectre_v2_check_boottime_disable(void) { char arg[20]; int ret; @@ -148,14 +153,21 @@ void __init spectre_v2_check_boottime_disable(void) retpoline: if (boot_cpu_data.x86_vendor == X86_VENDOR_AMD) { retpoline_amd: + if (boot_cpu_data.x86_vendor != X86_VENDOR_AMD || + !boot_cpu_has(X86_FEATURE_LFENCE_RDTSC)) { + pr_info("AMD retpoline not supported, fall back to generic\n"); + goto retpoline_generic; + } + spectre_v2_enabled = retp_compiler() ? SPECTRE_V2_RETPOLINE_AMD : SPECTRE_V2_RETPOLINE_MINIMAL_AMD; setup_force_cpu_cap(X86_FEATURE_RETPOLINE_AMD); - } else { - retpoline_generic: - spectre_v2_enabled = retp_compiler() ? - SPECTRE_V2_RETPOLINE_GENERIC : SPECTRE_V2_RETPOLINE_MINIMAL; + setup_force_cpu_cap(X86_FEATURE_RETPOLINE); + return; } +retpoline_generic: + spectre_v2_enabled = retp_compiler() ? + SPECTRE_V2_RETPOLINE_GENERIC : SPECTRE_V2_RETPOLINE_MINIMAL; setup_force_cpu_cap(X86_FEATURE_RETPOLINE); return; #else diff --git a/arch/x86/kernel/setup.c b/arch/x86/kernel/setup.c index 9fb4f9d..b5a908b 100644 --- a/arch/x86/kernel/setup.c +++ b/arch/x86/kernel/setup.c @@ -1322,8 +1322,6 @@ void __init setup_arch(char **cmdline_p) register_refined_jiffies(CLOCK_TICK_RATE); - spectre_v2_check_boottime_disable(); - #ifdef CONFIG_EFI if (efi_enabled(EFI_BOOT)) efi_apply_memmap_quirks();