From patchwork Mon Dec 28 16:06:31 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Krzysztof Mazur X-Patchwork-Id: 354417 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.8 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI, SPF_HELO_NONE, SPF_PASS, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id BFCBDC4332B for ; Mon, 28 Dec 2020 16:09:29 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 84BE6207C9 for ; Mon, 28 Dec 2020 16:09:29 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2393539AbgL1QJ0 (ORCPT ); Mon, 28 Dec 2020 11:09:26 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49860 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2393535AbgL1QJY (ORCPT ); Mon, 28 Dec 2020 11:09:24 -0500 X-Greylist: delayed 121 seconds by postgrey-1.37 at lindbergh.monkeyblade.net; Mon, 28 Dec 2020 08:08:43 PST Received: from shrek.podlesie.net (shrek-3s.podlesie.net [IPv6:2a00:13a0:3010::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 9F08FC061794; Mon, 28 Dec 2020 08:08:43 -0800 (PST) Received: from geronimo.kostuchna.emnet (unknown [127.0.0.1]) by shrek.podlesie.net (Postfix) with ESMTP id 9119E1636; Mon, 28 Dec 2020 16:06:40 +0000 (UTC) From: Krzysztof Mazur To: Thomas Gleixner , Ingo Molnar , Borislav Petkov Cc: x86@kernel.org, linux-kernel@vger.kernel.org, Krzysztof Mazur , stable@vger.kernel.org Subject: [PATCH] x86/lib: don't use MMX before FPU initialization Date: Mon, 28 Dec 2020 17:06:31 +0100 Message-Id: <20201228160631.32732-1-krzysiek@podlesie.net> X-Mailer: git-send-email 2.27.0.rc1.207.gb85828341f MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: stable@vger.kernel.org When enabled, the MMX 3DNow! optimized memcpy() is used very early, even before FPU is initialized. It worked fine, but commit 7ad816762f9bf89e940e618ea40c43138b479e10 ("x86/fpu: Reset MXCSR to default in kernel_fpu_begin()") broke that. After that commit the kernel crashes just after "Booting the kernel." message. It affects all kernels with CONFIG_X86_USE_3DNOW=y (enabled when some AMD/Cyrix processors are selected). So, enable MMX 3DNow! optimized memcpy() later. Cc: # 5.8+ Signed-off-by: Krzysztof Mazur Signed-off-by: Krzysztof Mazur Signed-off-by: Krzysztof Mazur --- Hi, this patch fixes a kernel crash during boot observed on AMD Athlon XP (with CONFIG_MK7=y). The static key adds 5 byte NOP in the "fast" path. The 3DNow! usage can be enabled earlier, but arch_initcall should be ok. The static_cpu_has() does not work because the kernel with CONFIG_X86_USE_3DNOW=y assumes that 3DNOW! is available, so a static key is used instead. "Alternatives" should also work, as long they not assume that required features (REQUIRED_MASK*) are available early. Similar bugs are possible. For easier debugging, maybe kernel_fpu_begin() should catch such cases and print something? Thanks, Krzysiek arch/x86/lib/mmx_32.c | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/arch/x86/lib/mmx_32.c b/arch/x86/lib/mmx_32.c index 4321fa02e18d..375bf0798993 100644 --- a/arch/x86/lib/mmx_32.c +++ b/arch/x86/lib/mmx_32.c @@ -26,12 +26,14 @@ #include #include +static __ro_after_init DEFINE_STATIC_KEY_FALSE(use_mmx); + void *_mmx_memcpy(void *to, const void *from, size_t len) { void *p; int i; - if (unlikely(in_interrupt())) + if (unlikely(in_interrupt()) || !static_branch_likely(&use_mmx)) return __memcpy(to, from, len); p = to; @@ -376,3 +378,11 @@ void mmx_copy_page(void *to, void *from) fast_copy_page(to, from); } EXPORT_SYMBOL(mmx_copy_page); + +static int __init mmx_32_init(void) +{ + static_branch_enable(&use_mmx); + return 0; +} + +arch_initcall(mmx_32_init);