From nobody Sat Sep 13 20:27:35 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 70FFAC54EAA for ; Mon, 30 Jan 2023 16:17:23 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235784AbjA3QRW (ORCPT ); Mon, 30 Jan 2023 11:17:22 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52252 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230202AbjA3QRT (ORCPT ); Mon, 30 Jan 2023 11:17:19 -0500 Received: from mail.skyhub.de (mail.skyhub.de [5.9.137.197]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6CC772726 for ; Mon, 30 Jan 2023 08:17:17 -0800 (PST) Received: from zn.tnic (p5de8e9fe.dip0.t-ipconnect.de [93.232.233.254]) (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 18EBB1EC0645; Mon, 30 Jan 2023 17:17:16 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=alien8.de; s=dkim; t=1675095436; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=1SYkUvMceHmQv4CWJzaHm7dllYvbghnGU+k2S37WGWE=; b=PQabSffDgDJpaBVkhERnOz7WPcn3aTjUShARnx32aKil7Q30xd3/zFlTIULwGVFVRWo1RN qqgZ+nvPKKszrV0tU9tmMK2RBb7kFz2p1PlfAJy7DNDeQKVZ9frANGhVcV1DcthLo0YHz5 IFdorUK/5e1zoYiMmJMAtOeyYvrEWOE= From: Borislav Petkov To: X86 ML Cc: LKML Subject: [PATCH 1/4] x86/microcode/amd: Remove load_microcode_amd()'s bsp parameter Date: Mon, 30 Jan 2023 17:17:06 +0100 Message-Id: <20230130161709.11615-2-bp@alien8.de> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20230130161709.11615-1-bp@alien8.de> References: <20230130161709.11615-1-bp@alien8.de> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" From: "Borislav Petkov (AMD)" It is always the BSP. No functional changes. Signed-off-by: Borislav Petkov (AMD) --- arch/x86/kernel/cpu/microcode/amd.c | 19 ++++--------------- 1 file changed, 4 insertions(+), 15 deletions(-) diff --git a/arch/x86/kernel/cpu/microcode/amd.c b/arch/x86/kernel/cpu/micr= ocode/amd.c index 56471f750762..f41ea46475ac 100644 --- a/arch/x86/kernel/cpu/microcode/amd.c +++ b/arch/x86/kernel/cpu/microcode/amd.c @@ -553,8 +553,7 @@ void load_ucode_amd_ap(unsigned int cpuid_1_eax) apply_microcode_early_amd(cpuid_1_eax, cp.data, cp.size, false); } =20 -static enum ucode_state -load_microcode_amd(bool save, u8 family, const u8 *data, size_t size); +static enum ucode_state load_microcode_amd(u8 family, const u8 *data, size= _t size); =20 int __init save_microcode_in_initrd_amd(unsigned int cpuid_1_eax) { @@ -572,7 +571,7 @@ int __init save_microcode_in_initrd_amd(unsigned int cp= uid_1_eax) if (!desc.mc) return -EINVAL; =20 - ret =3D load_microcode_amd(true, x86_family(cpuid_1_eax), desc.data, desc= .size); + ret =3D load_microcode_amd(x86_family(cpuid_1_eax), desc.data, desc.size); if (ret > UCODE_UPDATED) return -EINVAL; =20 @@ -850,8 +849,7 @@ static enum ucode_state __load_microcode_amd(u8 family,= const u8 *data, return UCODE_OK; } =20 -static enum ucode_state -load_microcode_amd(bool save, u8 family, const u8 *data, size_t size) +static enum ucode_state load_microcode_amd(u8 family, const u8 *data, size= _t size) { struct ucode_patch *p; enum ucode_state ret; @@ -875,10 +873,6 @@ load_microcode_amd(bool save, u8 family, const u8 *dat= a, size_t size) ret =3D UCODE_NEW; } =20 - /* save BSP's matching patch for early load */ - if (!save) - return ret; - memset(amd_ucode_patch, 0, PATCH_MAX_SIZE); memcpy(amd_ucode_patch, p->data, min_t(u32, p->size, PATCH_MAX_SIZE)); =20 @@ -905,14 +899,9 @@ static enum ucode_state request_microcode_amd(int cpu,= struct device *device) { char fw_name[36] =3D "amd-ucode/microcode_amd.bin"; struct cpuinfo_x86 *c =3D &cpu_data(cpu); - bool bsp =3D c->cpu_index =3D=3D boot_cpu_data.cpu_index; enum ucode_state ret =3D UCODE_NFOUND; const struct firmware *fw; =20 - /* reload ucode container only on the boot cpu */ - if (!bsp) - return UCODE_OK; - if (c->x86 >=3D 0x15) snprintf(fw_name, sizeof(fw_name), "amd-ucode/microcode_amd_fam%.2xh.bin= ", c->x86); =20 @@ -925,7 +914,7 @@ static enum ucode_state request_microcode_amd(int cpu, = struct device *device) if (!verify_container(fw->data, fw->size, false)) goto fw_release; =20 - ret =3D load_microcode_amd(bsp, c->x86, fw->data, fw->size); + ret =3D load_microcode_amd(c->x86, fw->data, fw->size); =20 fw_release: release_firmware(fw); --=20 2.35.1 From nobody Sat Sep 13 20:27:35 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 1A9E3C61DA4 for ; Mon, 30 Jan 2023 16:17:28 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236089AbjA3QR0 (ORCPT ); Mon, 30 Jan 2023 11:17:26 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52260 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234005AbjA3QRT (ORCPT ); Mon, 30 Jan 2023 11:17:19 -0500 Received: from mail.skyhub.de (mail.skyhub.de [IPv6:2a01:4f8:190:11c2::b:1457]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 67DF24C17 for ; Mon, 30 Jan 2023 08:17:18 -0800 (PST) Received: from zn.tnic (p5de8e9fe.dip0.t-ipconnect.de [93.232.233.254]) (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 906651EC064A; Mon, 30 Jan 2023 17:17:16 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=alien8.de; s=dkim; t=1675095436; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=GTNh4MA8iB7YYMspAsV81mmzA+02A3dlbsqeFZ/PphI=; b=C0Rlbcu1mp6q50Y5EQjaTx2JRSggp9mP+rwyf/kOKPbf0b5vZWPgVgolKFtiv4aR1mh6cC MaDFJPO/npagtaXUCupghd8Tdbg1YsY9FMgTB+6eLtsY+0XJjP0qHhEqF2HozlU80y6XK3 a93S+ezKBfvC4d69AXJ4yBLnIFNOjBg= From: Borislav Petkov To: X86 ML Cc: LKML Subject: [PATCH 2/4] x86/microcode/AMD: Add a @cpu parameter to the reloading functions Date: Mon, 30 Jan 2023 17:17:07 +0100 Message-Id: <20230130161709.11615-3-bp@alien8.de> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20230130161709.11615-1-bp@alien8.de> References: <20230130161709.11615-1-bp@alien8.de> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" From: "Borislav Petkov (AMD)" Will be used in a subsequent change. Signed-off-by: Borislav Petkov (AMD) --- arch/x86/include/asm/microcode.h | 4 ++-- arch/x86/include/asm/microcode_amd.h | 4 ++-- arch/x86/kernel/cpu/microcode/amd.c | 2 +- arch/x86/kernel/cpu/microcode/core.c | 6 +++--- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/arch/x86/include/asm/microcode.h b/arch/x86/include/asm/microc= ode.h index d5a58bde091c..320566a0443d 100644 --- a/arch/x86/include/asm/microcode.h +++ b/arch/x86/include/asm/microcode.h @@ -125,13 +125,13 @@ static inline unsigned int x86_cpuid_family(void) #ifdef CONFIG_MICROCODE extern void __init load_ucode_bsp(void); extern void load_ucode_ap(void); -void reload_early_microcode(void); +void reload_early_microcode(unsigned int cpu); extern bool initrd_gone; void microcode_bsp_resume(void); #else static inline void __init load_ucode_bsp(void) { } static inline void load_ucode_ap(void) { } -static inline void reload_early_microcode(void) { } +static inline void reload_early_microcode(unsigned int cpu) { } static inline void microcode_bsp_resume(void) { } #endif =20 diff --git a/arch/x86/include/asm/microcode_amd.h b/arch/x86/include/asm/mi= crocode_amd.h index ac31f9140d07..e6662adf3af4 100644 --- a/arch/x86/include/asm/microcode_amd.h +++ b/arch/x86/include/asm/microcode_amd.h @@ -47,12 +47,12 @@ struct microcode_amd { extern void __init load_ucode_amd_bsp(unsigned int family); extern void load_ucode_amd_ap(unsigned int family); extern int __init save_microcode_in_initrd_amd(unsigned int family); -void reload_ucode_amd(void); +void reload_ucode_amd(unsigned int cpu); #else static inline void __init load_ucode_amd_bsp(unsigned int family) {} static inline void load_ucode_amd_ap(unsigned int family) {} static inline int __init save_microcode_in_initrd_amd(unsigned int family) { return -EINVAL; } -static inline void reload_ucode_amd(void) {} +static inline void reload_ucode_amd(unsigned int cpu) {} #endif #endif /* _ASM_X86_MICROCODE_AMD_H */ diff --git a/arch/x86/kernel/cpu/microcode/amd.c b/arch/x86/kernel/cpu/micr= ocode/amd.c index f41ea46475ac..b8d73f133193 100644 --- a/arch/x86/kernel/cpu/microcode/amd.c +++ b/arch/x86/kernel/cpu/microcode/amd.c @@ -578,7 +578,7 @@ int __init save_microcode_in_initrd_amd(unsigned int cp= uid_1_eax) return 0; } =20 -void reload_ucode_amd(void) +void reload_ucode_amd(unsigned int cpu) { struct microcode_amd *mc; u32 rev, dummy __always_unused; diff --git a/arch/x86/kernel/cpu/microcode/core.c b/arch/x86/kernel/cpu/mic= rocode/core.c index 712aafff96e0..931d3c3262ec 100644 --- a/arch/x86/kernel/cpu/microcode/core.c +++ b/arch/x86/kernel/cpu/microcode/core.c @@ -298,7 +298,7 @@ struct cpio_data find_microcode_in_initrd(const char *p= ath, bool use_pa) #endif } =20 -void reload_early_microcode(void) +void reload_early_microcode(unsigned int cpu) { int vendor, family; =20 @@ -312,7 +312,7 @@ void reload_early_microcode(void) break; case X86_VENDOR_AMD: if (family >=3D 0x10) - reload_ucode_amd(); + reload_ucode_amd(cpu); break; default: break; @@ -557,7 +557,7 @@ void microcode_bsp_resume(void) if (uci->mc) microcode_ops->apply_microcode(cpu); else - reload_early_microcode(); + reload_early_microcode(cpu); } =20 static struct syscore_ops mc_syscore_ops =3D { --=20 2.35.1 From nobody Sat Sep 13 20:27:35 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 0BF86C54EAA for ; Mon, 30 Jan 2023 16:17:26 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235938AbjA3QRY (ORCPT ); Mon, 30 Jan 2023 11:17:24 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52262 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234818AbjA3QRT (ORCPT ); Mon, 30 Jan 2023 11:17:19 -0500 Received: from mail.skyhub.de (mail.skyhub.de [IPv6:2a01:4f8:190:11c2::b:1457]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6F76176B2 for ; Mon, 30 Jan 2023 08:17:18 -0800 (PST) Received: from zn.tnic (p5de8e9fe.dip0.t-ipconnect.de [93.232.233.254]) (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 0AB6D1EC0662; Mon, 30 Jan 2023 17:17:17 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=alien8.de; s=dkim; t=1675095437; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=8di1DcnjPBJs+6sR+q46scxgOm64hQ6Nc0v1gDxBPOQ=; b=kbOYdYxLjuSUHcotOBWNPGUcLBj0XhcknRBh57PVD4lt4cuJ1o+eXgSuU+xjCRrRt+5rRz c12g2PGtCToU8+7JVwFv9ON0bMyvxJOq1JaZuBbVeO8dNLUQkDfAIcm8rmIM7sMuVk0+/L hPcev2GG1vnTDjiqHAvlO3F/H3Md9BY= From: Borislav Petkov To: X86 ML Cc: LKML , stable@kernel.org Subject: [PATCH 3/4] x86/microcode/AMD: Fix mixed steppings support Date: Mon, 30 Jan 2023 17:17:08 +0100 Message-Id: <20230130161709.11615-4-bp@alien8.de> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20230130161709.11615-1-bp@alien8.de> References: <20230130161709.11615-1-bp@alien8.de> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" From: "Borislav Petkov (AMD)" The AMD side of the loader has always claimed to support mixed steppings. But somewhere along the way, it broke that by assuming that the cached patch blob is a single one instead of it being one per *node*. So turn it into a per-node one so that each node can stash the blob relevant for it. [ NB: Fixes tag is not really the exactly correct one but it is good enough. ] Fixes: fe055896c040 ("x86/microcode: Merge the early microcode loader") Signed-off-by: Borislav Petkov (AMD) Cc: --- arch/x86/kernel/cpu/microcode/amd.c | 34 ++++++++++++++++++----------- 1 file changed, 21 insertions(+), 13 deletions(-) diff --git a/arch/x86/kernel/cpu/microcode/amd.c b/arch/x86/kernel/cpu/micr= ocode/amd.c index b8d73f133193..ac59783e6e9f 100644 --- a/arch/x86/kernel/cpu/microcode/amd.c +++ b/arch/x86/kernel/cpu/microcode/amd.c @@ -55,7 +55,9 @@ struct cont_desc { }; =20 static u32 ucode_new_rev; -static u8 amd_ucode_patch[PATCH_MAX_SIZE]; + +/* One blob per node. */ +static u8 amd_ucode_patch[MAX_NUMNODES][PATCH_MAX_SIZE]; =20 /* * Microcode patch container file is prepended to the initrd in cpio @@ -428,7 +430,7 @@ apply_microcode_early_amd(u32 cpuid_1_eax, void *ucode,= size_t size, bool save_p patch =3D (u8 (*)[PATCH_MAX_SIZE])__pa_nodebug(&amd_ucode_patch); #else new_rev =3D &ucode_new_rev; - patch =3D &amd_ucode_patch; + patch =3D &amd_ucode_patch[0]; #endif =20 desc.cpuid_1_eax =3D cpuid_1_eax; @@ -580,10 +582,10 @@ int __init save_microcode_in_initrd_amd(unsigned int = cpuid_1_eax) =20 void reload_ucode_amd(unsigned int cpu) { - struct microcode_amd *mc; u32 rev, dummy __always_unused; + struct microcode_amd *mc; =20 - mc =3D (struct microcode_amd *)amd_ucode_patch; + mc =3D (struct microcode_amd *)amd_ucode_patch[cpu_to_node(cpu)]; =20 rdmsr(MSR_AMD64_PATCH_LEVEL, rev, dummy); =20 @@ -851,6 +853,8 @@ static enum ucode_state __load_microcode_amd(u8 family,= const u8 *data, =20 static enum ucode_state load_microcode_amd(u8 family, const u8 *data, size= _t size) { + struct cpuinfo_x86 *c; + unsigned int nid, cpu; struct ucode_patch *p; enum ucode_state ret; =20 @@ -863,18 +867,22 @@ static enum ucode_state load_microcode_amd(u8 family,= const u8 *data, size_t siz return ret; } =20 - p =3D find_patch(0); - if (!p) { - return ret; - } else { - if (boot_cpu_data.microcode >=3D p->patch_id) - return ret; + for_each_node(nid) { + cpu =3D cpumask_first(cpumask_of_node(nid)); + c =3D &cpu_data(cpu); + + p =3D find_patch(cpu); + if (!p) + continue; + + if (c->microcode >=3D p->patch_id) + continue; =20 ret =3D UCODE_NEW; - } =20 - memset(amd_ucode_patch, 0, PATCH_MAX_SIZE); - memcpy(amd_ucode_patch, p->data, min_t(u32, p->size, PATCH_MAX_SIZE)); + memset(&amd_ucode_patch[nid], 0, PATCH_MAX_SIZE); + memcpy(&amd_ucode_patch[nid], p->data, min_t(u32, p->size, PATCH_MAX_SIZ= E)); + } =20 return ret; } --=20 2.35.1 From nobody Sat Sep 13 20:27:35 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 86E54C54EAA for ; Mon, 30 Jan 2023 16:17:31 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236218AbjA3QR3 (ORCPT ); Mon, 30 Jan 2023 11:17:29 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52264 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235264AbjA3QRT (ORCPT ); Mon, 30 Jan 2023 11:17:19 -0500 Received: from mail.skyhub.de (mail.skyhub.de [5.9.137.197]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D16732137 for ; Mon, 30 Jan 2023 08:17:18 -0800 (PST) Received: from zn.tnic (p5de8e9fe.dip0.t-ipconnect.de [93.232.233.254]) (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 836021EC066E; Mon, 30 Jan 2023 17:17:17 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=alien8.de; s=dkim; t=1675095437; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=d83KcD8OBTU0320YA3XbPbLjBYW6teajmhjAa074RMo=; b=iI0mWQpjGGsIaO6s913P6m725ex127gGkwG0rhWVn0i4NN94QLBfTLtxlfFIG3M4T+eYUa unForHvyJmRf/yUM9bwIUxp9avfPIO67eYSUnzvvcdbtFYAwdm6jaububuVmcfhcGRJr1b MrL2M8Bu6C3lfV13XdcOljUq+MO5e4g= From: Borislav Petkov To: X86 ML Cc: LKML Subject: [PATCH 4/4] x86/microcode/core: Return an error only when necessary Date: Mon, 30 Jan 2023 17:17:09 +0100 Message-Id: <20230130161709.11615-5-bp@alien8.de> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20230130161709.11615-1-bp@alien8.de> References: <20230130161709.11615-1-bp@alien8.de> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" From: "Borislav Petkov (AMD)" Return an error from the late loading function which is run on each CPU only when an error has actually been encountered during the update. Signed-off-by: Borislav Petkov (AMD) --- arch/x86/kernel/cpu/microcode/core.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/arch/x86/kernel/cpu/microcode/core.c b/arch/x86/kernel/cpu/mic= rocode/core.c index 931d3c3262ec..d52042d5faae 100644 --- a/arch/x86/kernel/cpu/microcode/core.c +++ b/arch/x86/kernel/cpu/microcode/core.c @@ -409,10 +409,10 @@ static int __reload_late(void *info) goto wait_for_siblings; =20 if (err >=3D UCODE_NFOUND) { - if (err =3D=3D UCODE_ERROR) + if (err =3D=3D UCODE_ERROR) { pr_warn("Error reloading microcode on CPU %d\n", cpu); - - ret =3D -1; + ret =3D -1; + } } =20 wait_for_siblings: --=20 2.35.1