Received: by 2002:a05:6a10:7420:0:0:0:0 with SMTP id hk32csp657268pxb; Thu, 17 Feb 2022 11:47:29 -0800 (PST) X-Google-Smtp-Source: ABdhPJwgAmdhZunBzkTPeS/ZMvveNS9KEp+ZF3z2k5YAhsAKinHW235bc556p1N8NS3NJ++Dwv6W X-Received: by 2002:a17:90b:1b51:b0:1b9:b61a:aaf4 with SMTP id nv17-20020a17090b1b5100b001b9b61aaaf4mr8855566pjb.79.1645127248825; Thu, 17 Feb 2022 11:47:28 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1645127248; cv=none; d=google.com; s=arc-20160816; b=oSbaORTzNES0VbSt5Oys2e+5I+P9IiYlCxfjCoKWPEOEAlECDYS1r+mWHGa3o8THwa a7fx3JQ3J78b67tVIdHLghFE7ptkNPdxxE/HDurTUEEWo4ScveKm/+ZALC6G7bSsXl0o NU1tZqa/KmPWQR7C15aWs4TVylQU3vLVSW6UKe7oHjJv4X9qBQGDOjwquWdBJz6bl3J3 Khh7jb6CzTAc0T1Qwqg+fFAkqsG5WmWgZ6e73v4nT1rDlSE88gB7zOz8yyVEvIkLR5tq N+LKWilae9ja/dT39Vkfj/VHFm9Qypyy6bUaEt8lFwb7n0WE5fyZzpW/dPg1Xt1gzyMM XhWg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=/nqpxuKQfkVRRZMI1eQzAvlMUmMPIQh98Y37rBXs1N4=; b=NVsCQn4Wk7Z3hwxfXtuZEbXkpdXPEMEL6IxbTcmjTWnfg1AUg9OYP1r3yWt7TgexFV 1ji5VDTsxASX5PwFxYKT/GYkEeq1nivPVY7w1heb+uZSWVfZlkiXjlAY5dRwRsMyvGEC dfAymeWAQNMHAI+mXu0uqZLqQ5So/JdLvmhgJ5tVbBvK0+dM1SPY52gmsESrRylqe4Fj GmYGrSX8kW/C9nib3eAP1W05jdq1V+fUM0BWIY4kYjk0riXWYKAEK7it3bXwkC4QKkmy ud05grpPL4MJneftjNefl48VVT4czhYvlDsK80bU5ivoHF5oIzBK1kvjWh7n41WoBtUh MZ2w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@alien8.de header.s=dkim header.b=V9f9Gm5+; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=alien8.de Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id p19si1508520pjo.120.2022.02.17.11.47.12; Thu, 17 Feb 2022 11:47:28 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@alien8.de header.s=dkim header.b=V9f9Gm5+; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=alien8.de Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229893AbiBQQcu (ORCPT + 99 others); Thu, 17 Feb 2022 11:32:50 -0500 Received: from mxb-00190b01.gslb.pphosted.com ([23.128.96.19]:54870 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S242494AbiBQQcp (ORCPT ); Thu, 17 Feb 2022 11:32:45 -0500 Received: from mail.skyhub.de (mail.skyhub.de [5.9.137.197]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F1BD527FB8F for ; Thu, 17 Feb 2022 08:32:29 -0800 (PST) Received: from zn.tnic (dslb-088-067-221-104.088.067.pools.vodafone-ip.de [88.67.221.104]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.skyhub.de (SuperMail on ZX Spectrum 128k) with ESMTPSA id 746691EC05B0; Thu, 17 Feb 2022 17:32:24 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=alien8.de; s=dkim; t=1645115544; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:in-reply-to:in-reply-to: references:references; bh=/nqpxuKQfkVRRZMI1eQzAvlMUmMPIQh98Y37rBXs1N4=; b=V9f9Gm5+7Z8IlfaI2QMYqf/flC3ioiNAe47MZUZ6N1b8UC9zWot1N8Sf8afGYUDMM28C0L 7tq9MtpCGrbCm5zOo5trrYZb8vRb0B0sHvk0quboehColjPpIjpytfRkR3pJALKdCAwVd+ FVDk7pCAU/waSsic4Xl0mYuKxxY+Oes= Date: Thu, 17 Feb 2022 17:32:30 +0100 From: Borislav Petkov To: Jue Wang Cc: Tony Luck , x86@kernel.org, linux-kernel@vger.kernel.org, patches@lists.linux.dev Subject: Re: [PATCH] x86/mce: work around an erratum on fast string copy instructions. Message-ID: References: <20220216215313.1707663-1-juew@google.com> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Also, when sending a new one, pls use this fixed up version where I've cleaned a bunch of minor things: --- From: Jue Wang Date: Wed, 16 Feb 2022 13:53:13 -0800 Subject: [PATCH] x86/mce: Work around an erratum with fast string copy instructions A rare kernel panic scenario can happen when the following conditions are met due to an erratum on fast string copy instructions: 1) An uncorrected error. 2) That error must be in first cache line of a page. 3) Kernel must execute page_copy from the page immediately before that page. The fast string copy instructions ("REP; MOVS*") could consume an uncorrectable memory error in the cache line _right after_ the desired region to copy and raise an MCE. Bit 0 of MSR_IA32_MISC_ENABLE can be cleared to disable fast string copy and will avoid such spurious machine checks. However, that is less preferable due to the permanent performance impact. Considering memory poison is rare, it's desirable to keep fast string copy enabled until an MCE is seen. Intel has confirmed the following: 1. The CPU erratum of fast string copy only applies to Skylake, Cascade Lake and Cooper Lake generations. Directly return from the MCE handler: 2. Will result in complete execution of the "REP; MOVS*" with no data loss or corruption. 3. Will not result in another MCE firing on the next poisoned cache line due to "REP; MOVS*". 4. Will resume execution from a correct point in code. 5. Will result in the same instruction that triggered the MCE firing a second MCE immediately for any other software recoverable data fetch errors. 6. Is not safe without disabling the fast string copy, as the next fast string copy of the same buffer on the same CPU would result in a PANIC MCE. This should mitigate the erratum completely with the only caveat that the fast string copy is disabled on the affected hyper thread thus performance degradation. This is still better than the OS crashing on MCEs raised on an irrelevant process due to "REP; MOVS*' accesses in a kernel context, e.g., copy_page. Tested: Injected errors on 1st cache line of 8 anonymous pages of process 'proc1' and observed MCE consumption from 'proc2' with no panic (directly returned). Without the fix, the host panicked within a few minutes on a random 'proc2' process due to kernel access from copy_page. [ bp: Fix comment style + touch ups. ] Signed-off-by: Jue Wang Signed-off-by: Borislav Petkov Reviewed-by: Tony Luck --- arch/x86/kernel/cpu/mce/core.c | 57 ++++++++++++++++++++++++++++++ arch/x86/kernel/cpu/mce/internal.h | 5 ++- 2 files changed, 61 insertions(+), 1 deletion(-) diff --git a/arch/x86/kernel/cpu/mce/core.c b/arch/x86/kernel/cpu/mce/core.c index 0e7147430ec0..f7179a103d30 100644 --- a/arch/x86/kernel/cpu/mce/core.c +++ b/arch/x86/kernel/cpu/mce/core.c @@ -814,6 +814,53 @@ quirk_sandybridge_ifu(int bank, struct mce *m, struct pt_regs *regs) m->cs = regs->cs; } +/* + * Disable fast string copy and return from the MCE handler upon the first SRAR + * MCE on bank 1 due to a CPU erratum on Intel Skylake/Cascade Lake/Cooper Lake + * CPUs. + * The fast string copy instructions ("REP; MOVS*") could consume an + * uncorrectable memory error in the cache line _right after_ the desired region + * to copy and raise an MCE with RIP pointing to the instruction _after_ the + * "REP; MOVS*". + * This mitigation addresses the issue completely with the caveat of performance + * degradation on the CPU affected. This is still better than the OS crashing on + * MCEs raised on an irrelevant process due to "REP; MOVS*" accesses from a + * kernel context (e.g., copy_page). + * + * Returns true when fast string copy on CPU has been disabled. + */ +static noinstr bool quirk_skylake_repmov(void) +{ + u64 mcgstatus = mce_rdmsrl(MSR_IA32_MCG_STATUS); + u64 misc_enable = __rdmsr(MSR_IA32_MISC_ENABLE); + + /* + * Apply the quirk only to local machine checks, i.e., no broadcast + * sync is needed. + */ + if ((mcgstatus & MCG_STATUS_LMCES) && + unlikely(misc_enable & MSR_IA32_MISC_ENABLE_FAST_STRING)) { + u64 mc1_status = mce_rdmsrl(MSR_IA32_MCx_STATUS(1)); + + /* Check for a software-recoverable data fetch error. */ + if ((mc1_status & + (MCI_STATUS_VAL | MCI_STATUS_OVER | MCI_STATUS_UC | MCI_STATUS_EN | + MCI_STATUS_ADDRV | MCI_STATUS_MISCV | MCI_STATUS_PCC | + MCI_STATUS_AR | MCI_STATUS_S)) == + (MCI_STATUS_VAL | MCI_STATUS_UC | MCI_STATUS_EN | + MCI_STATUS_ADDRV | MCI_STATUS_MISCV | + MCI_STATUS_AR | MCI_STATUS_S)) { + msr_clear_bit(MSR_IA32_MISC_ENABLE, + MSR_IA32_MISC_ENABLE_FAST_STRING_BIT); + mce_wrmsrl(MSR_IA32_MCG_STATUS, 0); + mce_wrmsrl(MSR_IA32_MCx_STATUS(1), 0); + pr_err_once("Errata detected, disable fast string copy instructions.\n"); + return true; + } + } + return false; +} + /* * Do a quick check if any of the events requires a panic. * This decides if we keep the events around or clear them. @@ -1383,6 +1430,9 @@ noinstr void do_machine_check(struct pt_regs *regs) else if (unlikely(!mca_cfg.initialized)) return unexpected_machine_check(regs); + if (mce_flags.skx_repmov_quirk && quirk_skylake_repmov()) + return; + /* * Establish sequential order between the CPUs entering the machine * check handler. @@ -1838,6 +1888,13 @@ static int __mcheck_cpu_apply_quirks(struct cpuinfo_x86 *c) if (c->x86 == 6 && c->x86_model == 45) mce_flags.snb_ifu_quirk = 1; + + /* + * Skylake, Cascacde Lake and Cooper Lake require a quirk on + * rep movs. + */ + if (c->x86 == 6 && c->x86_model == INTEL_FAM6_SKYLAKE_X) + mce_flags.skx_repmov_quirk = 1; } if (c->x86_vendor == X86_VENDOR_ZHAOXIN) { diff --git a/arch/x86/kernel/cpu/mce/internal.h b/arch/x86/kernel/cpu/mce/internal.h index a04b61e27827..a80b8fed3489 100644 --- a/arch/x86/kernel/cpu/mce/internal.h +++ b/arch/x86/kernel/cpu/mce/internal.h @@ -170,7 +170,10 @@ struct mce_vendor_flags { /* SandyBridge IFU quirk */ snb_ifu_quirk : 1, - __reserved_0 : 57; + /* Skylake, Cascade Lake, Cooper Lake REP; MOVS* quirk */ + skx_repmov_quirk : 1, + + __reserved_0 : 56; }; extern struct mce_vendor_flags mce_flags; -- 2.29.2 -- Regards/Gruss, Boris. https://people.kernel.org/tglx/notes-about-netiquette