x86/microcode: Cleanup and late loading enhancements
Subsystems (7)
v2 cover x86/microcode: Cleanup and late loading enhancements
This is a follow up on:
https://lore.kernel.org/lkml/[email protected]
Late microcode loading is desired by enterprise users. Late loading is problematic as it requires detailed knowledge about the change and an analysis whether this change modifies something which is already in use by the kernel. Large enterprise customers have engineering teams and access to deep technical vendor support. The regular admin does not have such resources, so the kernel has always tainted the kernel after late loading.
Intel recently added a new previously reserved field to the microcode header which contains the minimal microcode revision which must be running on the CPU to make the load safe. This field is 0 in all older microcode revisions, which the kernel assumes to be unsafe. Minimal revision checking can be enforced via Kconfig or kernel command line. It then refuses to load an unsafe revision. The default loads unsafe revisions like before and taints the kernel. If a safe revision is loaded the kernel is not tainted.
But that does not solve all other known problems with late loading:
- Late loading on current Intel CPUs is unsafe vs. NMI when hyperthreading is enabled. If a NMI hits the secondary sibling while the primary loads the microcode, the machine can crash.
- Soft offline SMT siblings which are playing dead with MWAIT can cause damage too when the microcode update modifies MWAIT. That's a realistic scenario in the context of 'nosmt' mitigations. :(
Neither the core code nor the Intel specific code handles any of this at all.
While trying to implement this, I stumbled over disfunctional, horribly complex and redundant code, which I decided to clean up first so the new functionality can be added on a clean slate.
So the series has several sections:
1) Cleanup core code, header files and Kconfig
2) Cleanup of the Intel specific code
3) Implementation of proper core control logic to handle the NMI safe requirements
4) Support for minimal revision check in the core and the Intel specific parts.
Thanks to Borislav for discussing this with me and helping out with testing. Thanks also to Ashok who contributed a few patches and helped with testing on the Intel side especially with the new minimal revision mechanism.
Changes vs. V1:
- Reordered the queue - Nikolay
- Fixed the bogus condition - Borislav
- Moved more structs and defines into the source - Arjan
- Consolidated the exported functions
- Cleaned up the core initialization
The series applies on:
git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip.git x86/microcode
and is also available from git:
git://git.kernel.org/pub/scm/linux/kernel/git/tglx/devel.git ucode-v2
Thanks,
tglx
--- arch/x86/include/asm/microcode_amd.h | 56 - arch/x86/include/asm/microcode_intel.h | 88 -- b/Documentation/admin-guide/kernel-parameters.txt | 5 b/arch/x86/Kconfig | 63 - b/arch/x86/include/asm/apic.h | 5 b/arch/x86/include/asm/cpu.h | 20 b/arch/x86/include/asm/microcode.h | 160 +--- b/arch/x86/kernel/apic/apic_flat_64.c | 2 b/arch/x86/kernel/apic/ipi.c | 9 b/arch/x86/kernel/apic/x2apic_cluster.c | 1 b/arch/x86/kernel/apic/x2apic_phys.c | 1 b/arch/x86/kernel/cpu/common.c | 1 b/arch/x86/kernel/cpu/intel.c | 176 ---- b/arch/x86/kernel/cpu/microcode/Makefile | 4 b/arch/x86/kernel/cpu/microcode/amd.c | 68 + b/arch/x86/kernel/cpu/microcode/core.c | 576 ++++++++++---- b/arch/x86/kernel/cpu/microcode/intel.c | 847 ++++++++++------------ b/arch/x86/kernel/cpu/microcode/internal.h | 125 +++ b/arch/x86/kernel/nmi.c | 9 b/arch/x86/mm/init.c | 1 b/drivers/platform/x86/intel/ifs/load.c | 15 b/include/linux/cpuhotplug.h | 1 22 files changed, 1124 insertions(+), 1109 deletions(-)
PATCH v2 1/37 x86/mm: Remove unused microcode.h include
From: Thomas Gleixner <[email protected]>
No usage for anything in that header.
--- arch/x86/mm/init.c | 1 - 1 file changed, 1 deletion(-)
---
arch/x86/mm/init.c +0 -1
@@ -20,7 +20,6 @@#include <asm/tlb.h> #include <asm/proto.h> #include <asm/dma.h> /* for MAX_DMA_PFN */ -#include <asm/microcode.h> #include <asm/kaslr.h> #include <asm/hypervisor.h> #include <asm/cpufeature.h>
[tip: x86/microcode] x86/mm: Remove unused microcode.h include
The following commit has been merged into the x86/microcode branch of tip:
The following commit has been merged into the x86/microcode branch of tip:
Commit-ID: e5a8192942da0026f28a1713684bd46eded04c92 Gitweb: https://git.kernel.org/tip/e5a8192942da0026f28a1713684bd46eded04c92 Author: Thomas Gleixner <[email protected]> AuthorDate: Sat, 12 Aug 2023 21:58:38 +02:00 Committer: Borislav Petkov (AMD) <[email protected]> CommitterDate: Sun, 13 Aug 2023 09:45:35 +02:00
x86/mm: Remove unused microcode.h include
No usage for anything in that header.
--- arch/x86/mm/init.c | 1 - 1 file changed, 1 deletion(-)
arch/x86/mm/init.c +0 -1
@@ -20,7 +20,6 @@#include <asm/tlb.h> #include <asm/proto.h> #include <asm/dma.h> /* for MAX_DMA_PFN */ -#include <asm/microcode.h> #include <asm/kaslr.h> #include <asm/hypervisor.h> #include <asm/cpufeature.h>
PATCH v2 2/37 x86/microcode: Hide the config knob
In reply to Thomas GleixnerIn reality CONFIG_MICROCODE is enabled in any reasonable configuration when Intel or AMD support is enabled. Accomodate to reality.
--- arch/x86/Kconfig | 38 --------------------------------- arch/x86/include/asm/microcode.h | 6 ++--- arch/x86/include/asm/microcode_amd.h | 2 - arch/x86/include/asm/microcode_intel.h | 2 - arch/x86/kernel/cpu/microcode/Makefile | 4 +-- 5 files changed, 8 insertions(+), 44 deletions(-)
arch/x86/Kconfig +1 -37
@@ -1308,44 +1308,8 @@ config X86_REBOOTFIXUPS Say N otherwise. config MICROCODE - bool "CPU microcode loading support" - default y + def_bool y depends on CPU_SUP_AMD || CPU_SUP_INTEL - help - If you say Y here, you will be able to update the microcode on - Intel and AMD processors. The Intel support is for the IA32 family, - e.g. Pentium Pro, Pentium II, Pentium III, Pentium 4, Xeon etc. The - AMD support is for families 0x10 and later. You will obviously need - the actual microcode binary data itself which is not shipped with - the Linux kernel. - - The preferred method to load microcode from a detached initrd is described - in Documentation/arch/x86/microcode.rst. For that you need to enable - CONFIG_BLK_DEV_INITRD in order for the loader to be able to scan the - initrd for microcode blobs. - - In addition, you can build the microcode into the kernel. For that you - need to add the vendor-supplied microcode to the CONFIG_EXTRA_FIRMWARE - config option. - -config MICROCODE_INTEL - bool "Intel microcode loading support" - depends on CPU_SUP_INTEL && MICROCODE - default MICROCODE - help - This options enables microcode patch loading support for Intel - processors. - - For the current Intel microcode data package go to - <https://downloadcenter.intel.com> and search for - 'Linux Processor Microcode Data File'. - -config MICROCODE_AMD - bool "AMD microcode loading support" - depends on CPU_SUP_AMD && MICROCODE - help - If you select this option, microcode patch loading support for AMD - processors will be enabled. config MICROCODE_LATE_LOADING bool "Late microcode loading (DANGEROUS)"
arch/x86/include/asm/microcode.h +3 -3
@@ -54,16 +54,16 @@ struct ucode_cpu_info {extern struct ucode_cpu_info ucode_cpu_info[];struct cpio_data find_microcode_in_initrd (const char *path,bool use_pa); -#ifdef CONFIG_MICROCODE_INTEL +#ifdef CONFIG_CPU_SUP_INTEL extern struct microcode_ops *__init init_intel_microcode (void );#else static inline struct microcode_ops *__init init_intel_microcode (void ) {return NULL ; } -#endif /* CONFIG_MICROCODE_INTEL */ +#endif /* CONFIG_CPU_SUP_INTEL */ -#ifdef CONFIG_MICROCODE_AMD +#ifdef CONFIG_CPU_SUP_AMD extern struct microcode_ops *__init init_amd_microcode (void ); extern void __exit exit_amd_microcode(void);#else
arch/x86/include/asm/microcode_amd.h +1 -1
@@ -43,7 +43,7 @@ struct microcode_amd {#define PATCH_MAX_SIZE (3 * PAGE_SIZE) -#ifdef CONFIG_MICROCODE_AMD +#ifdef CONFIG_CPU_SUP_AMD extern void load_ucode_amd_early (unsigned int cpuid_1_eax);extern int __init save_microcode_in_initrd_amd (unsigned int family);void reload_ucode_amd (unsigned int cpu);
arch/x86/include/asm/microcode_intel.h +1 -1
@@ -71,7 +71,7 @@ static inline u32 intel_get_microcode_rereturn rev; } -#ifdef CONFIG_MICROCODE_INTEL +#ifdef CONFIG_CPU_SUP_INTEL extern void __init load_ucode_intel_bsp (void );extern void load_ucode_intel_ap (void );extern void show_ucode_info_early (void );
arch/x86/kernel/cpu/microcode/Makefile +2 -2
@@ -1,5 +1,5 @@ # SPDX-License-Identifier: GPL-2.0-only microcode-y := core.o obj-$(CONFIG_MICROCODE) += microcode.o -microcode-$(CONFIG_MICROCODE_INTEL) += intel.o -microcode-$(CONFIG_MICROCODE_AMD) += amd.o +microcode-$(CONFIG_CPU_SUP_INTEL) += intel.o +microcode-$(CONFIG_CPU_SUP_AMD) += amd.o
[tip: x86/microcode] x86/microcode: Hide the config knob
The following commit has been merged into the x86/microcode branch of tip:
The following commit has been merged into the x86/microcode branch of tip:
Commit-ID: e6bcfdd75d53390a67f67237f4eafc77d9772056 Gitweb: https://git.kernel.org/tip/e6bcfdd75d53390a67f67237f4eafc77d9772056 Author: Thomas Gleixner <[email protected]> AuthorDate: Thu, 10 Aug 2023 20:37:29 +02:00 Committer: Borislav Petkov (AMD) <[email protected]> CommitterDate: Sun, 13 Aug 2023 10:26:39 +02:00
x86/microcode: Hide the config knob
In reality CONFIG_MICROCODE is enabled in any reasonable configuration when Intel or AMD support is enabled. Accommodate to reality.
--- arch/x86/Kconfig | 38 +------------------------- arch/x86/include/asm/microcode.h | 6 ++-- arch/x86/include/asm/microcode_amd.h | 2 +- arch/x86/include/asm/microcode_intel.h | 2 +- arch/x86/kernel/cpu/microcode/Makefile | 4 +-- 5 files changed, 8 insertions(+), 44 deletions(-)
arch/x86/Kconfig +1 -37
@@ -1308,44 +1308,8 @@ config X86_REBOOTFIXUPS Say N otherwise. config MICROCODE - bool "CPU microcode loading support" - default y + def_bool y depends on CPU_SUP_AMD || CPU_SUP_INTEL - help - If you say Y here, you will be able to update the microcode on - Intel and AMD processors. The Intel support is for the IA32 family, - e.g. Pentium Pro, Pentium II, Pentium III, Pentium 4, Xeon etc. The - AMD support is for families 0x10 and later. You will obviously need - the actual microcode binary data itself which is not shipped with - the Linux kernel. - - The preferred method to load microcode from a detached initrd is described - in Documentation/arch/x86/microcode.rst. For that you need to enable - CONFIG_BLK_DEV_INITRD in order for the loader to be able to scan the - initrd for microcode blobs. - - In addition, you can build the microcode into the kernel. For that you - need to add the vendor-supplied microcode to the CONFIG_EXTRA_FIRMWARE - config option. - -config MICROCODE_INTEL - bool "Intel microcode loading support" - depends on CPU_SUP_INTEL && MICROCODE - default MICROCODE - help - This options enables microcode patch loading support for Intel - processors. - - For the current Intel microcode data package go to - <https://downloadcenter.intel.com> and search for - 'Linux Processor Microcode Data File'. - -config MICROCODE_AMD - bool "AMD microcode loading support" - depends on CPU_SUP_AMD && MICROCODE - help - If you select this option, microcode patch loading support for AMD - processors will be enabled. config MICROCODE_LATE_LOADING bool "Late microcode loading (DANGEROUS)"
arch/x86/include/asm/microcode.h +3 -3
@@ -54,16 +54,16 @@ struct ucode_cpu_info {extern struct ucode_cpu_info ucode_cpu_info[];struct cpio_data find_microcode_in_initrd (const char *path,bool use_pa); -#ifdef CONFIG_MICROCODE_INTEL +#ifdef CONFIG_CPU_SUP_INTEL extern struct microcode_ops *__init init_intel_microcode (void );#else static inline struct microcode_ops *__init init_intel_microcode (void ) {return NULL ; } -#endif /* CONFIG_MICROCODE_INTEL */ +#endif /* CONFIG_CPU_SUP_INTEL */ -#ifdef CONFIG_MICROCODE_AMD +#ifdef CONFIG_CPU_SUP_AMD extern struct microcode_ops *__init init_amd_microcode (void ); extern void __exit exit_amd_microcode(void);#else
arch/x86/include/asm/microcode_amd.h +1 -1
@@ -43,7 +43,7 @@ struct microcode_amd {#define PATCH_MAX_SIZE (3 * PAGE_SIZE) -#ifdef CONFIG_MICROCODE_AMD +#ifdef CONFIG_CPU_SUP_AMD extern void load_ucode_amd_early (unsigned int cpuid_1_eax);extern int __init save_microcode_in_initrd_amd (unsigned int family);void reload_ucode_amd (unsigned int cpu);
arch/x86/include/asm/microcode_intel.h +1 -1
@@ -71,7 +71,7 @@ static inline u32 intel_get_microcode_revision(void)return rev; } -#ifdef CONFIG_MICROCODE_INTEL +#ifdef CONFIG_CPU_SUP_INTEL extern void __init load_ucode_intel_bsp (void );extern void load_ucode_intel_ap (void );extern void show_ucode_info_early (void );
arch/x86/kernel/cpu/microcode/Makefile +2 -2
@@ -1,5 +1,5 @@ # SPDX-License-Identifier: GPL-2.0-only microcode-y := core.o obj-$(CONFIG_MICROCODE) += microcode.o -microcode-$(CONFIG_MICROCODE_INTEL) += intel.o -microcode-$(CONFIG_MICROCODE_AMD) += amd.o +microcode-$(CONFIG_CPU_SUP_INTEL) += intel.o +microcode-$(CONFIG_CPU_SUP_AMD) += amd.o
Re: [patch V2 02/37] x86/microcode: Hide the config knob
In reply to Thomas Gleixnerarch/x86/Kconfig config X86_REBOOTFIXUPS · line 1311
Quoted 12 lines · Thomas Gleixner
In reality
CONFIG_MICROCODEis enabled in any reasonable configuration when Intel or AMD support is enabled. Accomodate to reality.arch/x86/Kconfig +1
-2@@ -1308,44 +1308,8 @@ config X86_REBOOTFIXUPS Say N otherwise. config MICROCODE - bool "CPU microcode loading support" - default y + def_bool y
There is one completely reasonable configuration that does not need or want CONFIG_MICROCODE: a kernel configured to run *exclusively* in a virtual machine, with no support for booting on physical hardware.
Would it retain an excessive amount of complexity to support omitting this code in that use case? (Doesn't have to eliminate *every last bit* of support code; just hoping to eliminate as much as easily possible without adding non-trivial complexity.)
I'd be happy to help keep this case building. Would you consider accepting a patch atop this series re-introducing minimal support for disabling this?
(I am all for eliminating the added complexity of the separately configurable knobs for CONFIG_MICROCODE_INTEL and CONFIG_MICROCODE_AMD, and just having those be unconditionally enabled if CONFIG_MICROCODE is.)
Borislav Petkov
maintainerRe: [patch V2 02/37] x86/microcode: Hide the config knob
Quoted 3 lines · Josh Triplett
I'd be happy to help keep this case building. Would you consider accepting a patch atop this series re-introducing minimal support for disabling this?
And a couple more KBs of builtin code is a problem because?
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteRe: [patch V2 02/37] x86/microcode: Hide the config knob
Quoted 6 lines · Borislav Petkov
Quoted 3 lines · Josh Triplett
I'd be happy to help keep this case building. Would you consider accepting a patch atop this series re-introducing minimal support for disabling this?
And a couple more KBs of builtin code is a problem because?
Multiple reasons. First, because "a couple more KBs" (concretely, 4637 bytes in this case) add up across the kernel. Second, because disabling something is the easiest way to guarantee spending *zero* boot time on it. And third, because I just confirmed that this requires a *trivial* patch to support (apart from re-enabling the config option):
arch/x86/include/asm/microcode.h +4 -3
@@ -68,11 +68,12 @@ static inline u32 intel_get_microcode_revision(void)return rev; } +#endif /* CONFIG_CPU_SUP_INTEL */ +#if defined(CONFIG_MICROCODE ) && defined(CONFIG_CPU_SUP_INTEL )void show_ucode_info_early (void ); - -#else /* CONFIG_CPU_SUP_INTEL */ +#else static inline void show_ucode_info_early (void ) { } -#endif /* !CONFIG_CPU_SUP_INTEL */ +#endif #endif /* _ASM_X86_MICROCODE_H */
Borislav Petkov
maintainerRe: [patch V2 02/37] x86/microcode: Hide the config knob
Quoted 2 lines · Josh Triplett
Multiple reasons. First, because "a couple more KBs" (concretely, 4637 bytes in this case) add up across the kernel.
Sure, if it were not really core. But this is core code. This is used on practically every baremetal x86 machine out there. You have other core code which is not used on virt either but that doesn't mean we'll axe it off on virt with a Kconfig option just to save a couple of KBs.
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquettePATCH v2 3/37 x86/microcode/intel: Move microcode functions out of cpu/intel.c
In reply to Thomas GleixnerThere is really no point to have that in the CPUID evaluation code. Move it into the intel specific microcode handling along with the datastructures, defines and helpers required by it. The exports need to stay for IFS.
---
V2: Move the structs, defines and helpers into intel.c
--- arch/x86/include/asm/microcode_intel.h | 28 ---- arch/x86/kernel/cpu/intel.c | 174 ---------------------------- arch/x86/kernel/cpu/microcode/intel.c | 202 +++++++++++++++++++++++++++++++++ 3 files changed, 204 insertions(+), 200 deletions(-)
arch/x86/include/asm/microcode_intel.h +2 -26
@@ -23,39 +23,15 @@ struct microcode_intel {unsigned int bits[]; }; -/* microcode format is extended from prescott processors */ -struct extended_signature { -unsigned int sig; -unsigned int pf; -unsigned int cksum; -}; - -struct extended_sigtable { -unsigned int count; -unsigned int cksum; -unsigned int reserved[3 ]; -struct extended_signature sigs[]; -}; - -#define DEFAULT_UCODE_DATASIZE (2000) -#define MC_HEADER_SIZE (sizeof(struct microcode_header_intel)) -#define DEFAULT_UCODE_TOTALSIZE (DEFAULT_UCODE_DATASIZE + MC_HEADER_SIZE) -#define EXT_HEADER_SIZE (sizeof(struct extended_sigtable)) -#define EXT_SIGNATURE_SIZE (sizeof(struct extended_signature)) +#define MC_HEADER_SIZE (sizeof(struct microcode_header_intel))#define MC_HEADER_TYPE_MICROCODE 1#define MC_HEADER_TYPE_IFS 2 - -#define get_totalsize (mc) \ - (((struct microcode_intel *)mc)->hdr.datasize ? \ - ((struct microcode_intel *)mc)->hdr.totalsize : \ - DEFAULT_UCODE_TOTALSIZE) +#define DEFAULT_UCODE_DATASIZE (2000)#define get_datasize (mc) \ (((struct microcode_intel *)mc)->hdr.datasize ? \ ((struct microcode_intel *)mc)->hdr.datasize : DEFAULT_UCODE_DATASIZE) -#define exttable_size (et) ((et)->count * EXT_SIGNATURE_SIZE + EXT_HEADER_SIZE) -static inline u32 intel_get_microcode_revision (void ) {u32 rev, dummy;
arch/x86/kernel/cpu/intel.c +0 -174
180 lines not on this page · raw message
arch/x86/kernel/cpu/microcode/intel.c +202 -0
208 lines not on this page · raw message
[tip: x86/microcode] x86/microcode/intel: Move microcode functions out of cpu/intel.c
The following commit has been merged into the x86/microcode branch of tip:
The following commit has been merged into the x86/microcode branch of tip:
Commit-ID: 4da2131fac10b6928da0b2e39b65189203810f44 Gitweb: https://git.kernel.org/tip/4da2131fac10b6928da0b2e39b65189203810f44 Author: Thomas Gleixner <[email protected]> AuthorDate: Sat, 12 Aug 2023 21:58:41 +02:00 Committer: Borislav Petkov (AMD) <[email protected]> CommitterDate: Sun, 13 Aug 2023 18:42:48 +02:00
x86/microcode/intel: Move microcode functions out of cpu/intel.c
There is really no point to have that in the CPUID evaluation code. Move it into the Intel-specific microcode handling along with the data structures, defines and helpers required by it. The exports need to stay for IFS.
--- arch/x86/include/asm/microcode_intel.h | 28 +--- arch/x86/kernel/cpu/intel.c | 174 +--------------------- arch/x86/kernel/cpu/microcode/intel.c | 202 ++++++++++++++++++++++++- 3 files changed, 204 insertions(+), 200 deletions(-)
arch/x86/include/asm/microcode_intel.h +2 -26
@@ -23,39 +23,15 @@ struct microcode_intel {unsigned int bits[]; }; -/* microcode format is extended from prescott processors */ -struct extended_signature { -unsigned int sig; -unsigned int pf; -unsigned int cksum; -}; - -struct extended_sigtable { -unsigned int count; -unsigned int cksum; -unsigned int reserved[3 ]; -struct extended_signature sigs[]; -}; - -#define DEFAULT_UCODE_DATASIZE (2000) -#define MC_HEADER_SIZE (sizeof(struct microcode_header_intel)) -#define DEFAULT_UCODE_TOTALSIZE (DEFAULT_UCODE_DATASIZE + MC_HEADER_SIZE) -#define EXT_HEADER_SIZE (sizeof(struct extended_sigtable)) -#define EXT_SIGNATURE_SIZE (sizeof(struct extended_signature)) +#define MC_HEADER_SIZE (sizeof(struct microcode_header_intel))#define MC_HEADER_TYPE_MICROCODE 1#define MC_HEADER_TYPE_IFS 2 - -#define get_totalsize (mc) \ - (((struct microcode_intel *)mc)->hdr.datasize ? \ - ((struct microcode_intel *)mc)->hdr.totalsize : \ - DEFAULT_UCODE_TOTALSIZE) +#define DEFAULT_UCODE_DATASIZE (2000)#define get_datasize (mc) \ (((struct microcode_intel *)mc)->hdr.datasize ? \ ((struct microcode_intel *)mc)->hdr.datasize : DEFAULT_UCODE_DATASIZE) -#define exttable_size (et) ((et)->count * EXT_SIGNATURE_SIZE + EXT_HEADER_SIZE) -static inline u32 intel_get_microcode_revision (void ) {u32 rev, dummy;
arch/x86/kernel/cpu/intel.c +0 -174
180 lines not on this page · raw message
arch/x86/kernel/cpu/microcode/intel.c +202 -0
208 lines not on this page · raw message
Re: [patch V2 03/37] x86/microcode/intel: Move microcode functions out of cpu/intel.c
In reply to Thomas GleixnerHi Thomas,
arch/x86/include/asm/microcode_intel.h struct microcode_intel {
Quoted 12 lines
...
arch/x86/include/asm/microcode_intel.h +5
-4-#define get_totalsize (mc) \ - (((struct microcode_intel *)mc)->hdr.datasize ? \ - ((struct microcode_intel *)mc)->hdr.totalsize : \ - DEFAULT_UCODE_TOTALSIZE) +#define DEFAULT_UCODE_DATASIZE (2000) + +static inline unsigned int get_totalsize (struct microcode_header_intel *hdr) +{ +return hdr->datasize ? :DEFAULT_UCODE_TOTALSIZE ;
I tested this patch series and observed that the inline code above failed the late microcode loading with the error message:
[ 117.100299] microcode: Error: bad microcode data file size.
According to the original get_totalsize() macro, seems like it should be:
return hdr->datasize ? hdr->totalsize : DEFAULT_UCODE_TOTALSIZE;
With this minor change, the late microcode loading was successful, and the expected message was displayed:
...
[ 150.198654] microcode: Microcode revision: 0x80000400 -> 0x80000401-Qiuxu
Quoted 2 lines
+}
...
Re: [patch V2 03/37] x86/microcode/intel: Move microcode functions out of cpu/intel.c
Quoted 15 lines · Qiuxu Zhuo
arch/x86/include/asm/microcode_intel.h struct microcode_intel {
Quoted 11 lines
arch/x86/include/asm/microcode_intel.h +5
-4-#define get_totalsize (mc) \ - (((struct microcode_intel *)mc)->hdr.datasize ? \ - ((struct microcode_intel *)mc)->hdr.totalsize : \ - DEFAULT_UCODE_TOTALSIZE) +#define DEFAULT_UCODE_DATASIZE (2000) + +static inline unsigned int get_totalsize (struct microcode_header_intel *hdr) +{ +return hdr->datasize ? :DEFAULT_UCODE_TOTALSIZE ;I tested this patch series and observed that the inline code above failed the late microcode loading with the error message:
Quoted 8 lines
[ 117.100299] microcode: Error: bad microcode data file size.According to the original
get_totalsize()macro, seems like it should be:return hdr->datasize ? hdr->totalsize : DEFAULT_UCODE_TOTALSIZE;With this minor change, the late microcode loading was successful, and the expected message was displayed:
Yes. I messed that up. Borislav already fixed it up when applying the first 9 patches.
PATCH v2 4/37 x86/microcode: Include vendor headers into microcode.h
In reply to Thomas GleixnerFrom: Ashok Raj <[email protected]>
Currently vendor specific headers are included explicitly when used in common code. Instead, include the vendor specific headers in microcode.h, and include that in all usages. No functional change.
--- arch/x86/include/asm/microcode.h | 5 ++++- arch/x86/include/asm/microcode_amd.h | 2 -- arch/x86/include/asm/microcode_intel.h | 2 -- arch/x86/kernel/cpu/common.c | 1 - arch/x86/kernel/cpu/intel.c | 2 +- arch/x86/kernel/cpu/microcode/amd.c | 1 - arch/x86/kernel/cpu/microcode/core.c | 2 -- arch/x86/kernel/cpu/microcode/intel.c | 2 +- drivers/platform/x86/intel/ifs/load.c | 2 +- 9 files changed, 7 insertions(+), 12 deletions(-)
---
arch/x86/include/asm/microcode.h +4 -1
@@ -2,10 +2,13 @@ #ifndef _ASM_X86_MICROCODE_H #define _ASM_X86_MICROCODE_H -#include <asm/cpu.h> #include <linux/earlycpio.h> #include <linux/initrd.h> +#include <asm/cpu.h> +#include <asm/microcode_amd.h> +#include <asm/microcode_intel.h> + struct ucode_patch { struct list_head plist; void *data; /* Intel uses only this one */
arch/x86/include/asm/microcode_amd.h +0 -2
@@ -2,8 +2,6 @@#ifndef _ASM_X86_MICROCODE_AMD_H#define _ASM_X86_MICROCODE_AMD_H -#include <asm/microcode.h> -#define UCODE_MAGIC 0x00414d44#define UCODE_EQUIV_CPU_TABLE_TYPE 0x00000000#define UCODE_UCODE_TYPE 0x00000001
arch/x86/include/asm/microcode_intel.h +0 -2
@@ -2,8 +2,6 @@ #ifndef _ASM_X86_MICROCODE_INTEL_H #define _ASM_X86_MICROCODE_INTEL_H -#include <asm/microcode.h> - struct microcode_header_intel { unsigned int hdrver; unsigned int rev;
arch/x86/kernel/cpu/common.c +0 -1
@@ -59,7 +59,6 @@#include <asm/cacheinfo.h> #include <asm/memtype.h> #include <asm/microcode.h> -#include <asm/microcode_intel.h> #include <asm/intel-family.h> #include <asm/cpu_device_id.h> #include <asm/uv/uv.h>
arch/x86/kernel/cpu/intel.c +1 -1
@@ -20,7 +20,7 @@#include <asm/bugs.h> #include <asm/cpu.h> #include <asm/intel-family.h> -#include <asm/microcode_intel.h> +#include <asm/microcode.h> #include <asm/hwcap2.h> #include <asm/elf.h> #include <asm/cpu_device_id.h>
arch/x86/kernel/cpu/microcode/amd.c +0 -1
@@ -29,7 +29,6 @@#include <linux/kernel.h> #include <linux/pci.h> -#include <asm/microcode_amd.h> #include <asm/microcode.h> #include <asm/processor.h> #include <asm/setup.h>
arch/x86/kernel/cpu/microcode/core.c +0 -2
@@ -31,9 +31,7 @@#include <linux/fs.h> #include <linux/mm.h> -#include <asm/microcode_intel.h> #include <asm/cpu_device_id.h> -#include <asm/microcode_amd.h> #include <asm/perf_event.h> #include <asm/microcode.h> #include <asm/processor.h>
arch/x86/kernel/cpu/microcode/intel.c +1 -1
@@ -30,9 +30,9 @@#include <linux/uio.h> #include <linux/mm.h> -#include <asm/microcode_intel.h> #include <asm/intel-family.h> #include <asm/processor.h> +#include <asm/microcode.h> #include <asm/tlbflush.h> #include <asm/setup.h> #include <asm/msr.h>
drivers/platform/x86/intel/ifs/load.c +1 -1
@@ -3,7 +3,7 @@#include <linux/firmware.h> #include <asm/cpu.h> -#include <asm/microcode_intel.h> +#include <asm/microcode.h> #include "ifs.h"
[tip: x86/microcode] x86/microcode: Include vendor headers into microcode.h
The following commit has been merged into the x86/microcode branch of tip:
The following commit has been merged into the x86/microcode branch of tip:
Commit-ID: 82ad097b02b6ab7985375b925efd06ac042caf1d Gitweb: https://git.kernel.org/tip/82ad097b02b6ab7985375b925efd06ac042caf1d Author: Ashok Raj <[email protected]> AuthorDate: Sat, 12 Aug 2023 21:58:42 +02:00 Committer: Borislav Petkov (AMD) <[email protected]> CommitterDate: Sun, 13 Aug 2023 18:42:55 +02:00
x86/microcode: Include vendor headers into microcode.h
Currently vendor specific headers are included explicitly when used in common code. Instead, include the vendor specific headers in microcode.h, and include that in all usages.
No functional change.
--- arch/x86/include/asm/microcode.h | 5 ++++- arch/x86/include/asm/microcode_amd.h | 2 -- arch/x86/include/asm/microcode_intel.h | 2 -- arch/x86/kernel/cpu/common.c | 1 - arch/x86/kernel/cpu/intel.c | 2 +- arch/x86/kernel/cpu/microcode/amd.c | 1 - arch/x86/kernel/cpu/microcode/core.c | 2 -- arch/x86/kernel/cpu/microcode/intel.c | 2 +- drivers/platform/x86/intel/ifs/load.c | 2 +- 9 files changed, 7 insertions(+), 12 deletions(-)
arch/x86/include/asm/microcode.h +4 -1
@@ -2,10 +2,13 @@ #ifndef _ASM_X86_MICROCODE_H #define _ASM_X86_MICROCODE_H -#include <asm/cpu.h> #include <linux/earlycpio.h> #include <linux/initrd.h> +#include <asm/cpu.h> +#include <asm/microcode_amd.h> +#include <asm/microcode_intel.h> + struct ucode_patch { struct list_head plist; void *data; /* Intel uses only this one */
arch/x86/include/asm/microcode_amd.h +0 -2
@@ -2,8 +2,6 @@#ifndef _ASM_X86_MICROCODE_AMD_H#define _ASM_X86_MICROCODE_AMD_H -#include <asm/microcode.h> -#define UCODE_MAGIC 0x00414d44#define UCODE_EQUIV_CPU_TABLE_TYPE 0x00000000#define UCODE_UCODE_TYPE 0x00000001
arch/x86/include/asm/microcode_intel.h +0 -2
@@ -2,8 +2,6 @@ #ifndef _ASM_X86_MICROCODE_INTEL_H #define _ASM_X86_MICROCODE_INTEL_H -#include <asm/microcode.h> - struct microcode_header_intel { unsigned int hdrver; unsigned int rev;
arch/x86/kernel/cpu/common.c +0 -1
@@ -59,7 +59,6 @@#include <asm/cacheinfo.h> #include <asm/memtype.h> #include <asm/microcode.h> -#include <asm/microcode_intel.h> #include <asm/intel-family.h> #include <asm/cpu_device_id.h> #include <asm/uv/uv.h>
arch/x86/kernel/cpu/intel.c +1 -1
@@ -20,7 +20,7 @@#include <asm/bugs.h> #include <asm/cpu.h> #include <asm/intel-family.h> -#include <asm/microcode_intel.h> +#include <asm/microcode.h> #include <asm/hwcap2.h> #include <asm/elf.h> #include <asm/cpu_device_id.h>
arch/x86/kernel/cpu/microcode/amd.c +0 -1
@@ -29,7 +29,6 @@#include <linux/kernel.h> #include <linux/pci.h> -#include <asm/microcode_amd.h> #include <asm/microcode.h> #include <asm/processor.h> #include <asm/setup.h>
arch/x86/kernel/cpu/microcode/core.c +0 -2
@@ -31,9 +31,7 @@#include <linux/fs.h> #include <linux/mm.h> -#include <asm/microcode_intel.h> #include <asm/cpu_device_id.h> -#include <asm/microcode_amd.h> #include <asm/perf_event.h> #include <asm/microcode.h> #include <asm/processor.h>
arch/x86/kernel/cpu/microcode/intel.c +1 -1
@@ -30,9 +30,9 @@#include <linux/uio.h> #include <linux/mm.h> -#include <asm/microcode_intel.h> #include <asm/intel-family.h> #include <asm/processor.h> +#include <asm/microcode.h> #include <asm/tlbflush.h> #include <asm/setup.h> #include <asm/msr.h>
drivers/platform/x86/intel/ifs/load.c +1 -1
@@ -3,7 +3,7 @@#include <linux/firmware.h> #include <asm/cpu.h> -#include <asm/microcode_intel.h> +#include <asm/microcode.h> #include "ifs.h"
PATCH v2 5/37 x86/microcode: Make reload_early_microcode() static
In reply to Thomas GleixnerFrom: Thomas Gleixner <[email protected]>
fe055896c040 ("x86/microcode: Merge the early microcode loader") left this needlessly public. Git archaeology provided by Borislav.
--- arch/x86/include/asm/microcode.h | 2 -- arch/x86/kernel/cpu/microcode/core.c | 2 +- 2 files changed, 1 insertion(+), 3 deletions(-)
---
arch/x86/include/asm/microcode.h +0 -2
@@ -128,13 +128,11 @@ static inline unsigned int x86_cpuid_fam#ifdef CONFIG_MICROCODE extern void __init load_ucode_bsp (void );extern void load_ucode_ap (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 (unsigned int cpu) { }static inline void microcode_bsp_resume (void ) { }#endif
arch/x86/kernel/cpu/microcode/core.c +1 -1
@@ -293,7 +293,7 @@ struct cpio_data find_microcode_in_initr#endif } -void reload_early_microcode (unsigned int cpu) +static void reload_early_microcode (unsigned int cpu) {int vendor, family;
[tip: x86/microcode] x86/microcode: Make reload_early_microcode() static
The following commit has been merged into the x86/microcode branch of tip:
The following commit has been merged into the x86/microcode branch of tip:
Commit-ID: 18648dbd3338b75e7b2895d725ee3a7187d6e3a3 Gitweb: https://git.kernel.org/tip/18648dbd3338b75e7b2895d725ee3a7187d6e3a3 Author: Thomas Gleixner <[email protected]> AuthorDate: Sat, 12 Aug 2023 21:58:44 +02:00 Committer: Borislav Petkov (AMD) <[email protected]> CommitterDate: Sun, 13 Aug 2023 18:42:55 +02:00
x86/microcode: Make reload_early_microcode() static
fe055896c040 ("x86/microcode: Merge the early microcode loader") left this needlessly public. Git archaeology provided by Borislav.
--- arch/x86/include/asm/microcode.h | 2 -- arch/x86/kernel/cpu/microcode/core.c | 2 +- 2 files changed, 1 insertion(+), 3 deletions(-)
arch/x86/include/asm/microcode.h +0 -2
@@ -128,13 +128,11 @@ 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 (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 (unsigned int cpu) { }static inline void microcode_bsp_resume (void ) { }#endif
arch/x86/kernel/cpu/microcode/core.c +1 -1
@@ -293,7 +293,7 @@ struct cpio_data find_microcode_in_initrd(const char *path, bool use_pa)#endif } -void reload_early_microcode (unsigned int cpu) +static void reload_early_microcode (unsigned int cpu) {int vendor, family;
PATCH v2 6/37 x86/microcode/intel: Rename get_datasize() since its used externally
In reply to Thomas GleixnerFrom: Ashok Raj <[email protected]>
Rename get_datasize() to intel_microcode_get_datasize() and make it an inline.
[ tglx: Make the argument typed and fix up the IFS code ]
---
V2: Make the argument typed
--- arch/x86/include/asm/microcode_intel.h | 7 ++++--- arch/x86/kernel/cpu/microcode/intel.c | 8 ++++---- drivers/platform/x86/intel/ifs/load.c | 5 +++-- 3 files changed, 11 insertions(+), 9 deletions(-)
---
arch/x86/include/asm/microcode_intel.h +4 -3
@@ -26,9 +26,10 @@ struct microcode_intel {#define MC_HEADER_TYPE_IFS 2#define DEFAULT_UCODE_DATASIZE (2000) -#define get_datasize (mc) \ - (((struct microcode_intel *)mc)->hdr.datasize ? \ - ((struct microcode_intel *)mc)->hdr.datasize : DEFAULT_UCODE_DATASIZE) +static inline int intel_microcode_get_datasize (struct microcode_header_intel *hdr) +{ +return hdr->datasize ? :DEFAULT_UCODE_DATASIZE ; +}static inline u32 intel_get_microcode_revision (void ) {
arch/x86/kernel/cpu/microcode/intel.c +4 -4
@@ -118,10 +118,10 @@ int intel_find_matching_signature(void *return 1 ;/* Look for ext. headers: */ -if (get_totalsize (mc_hdr) <=get_datasize (mc_hdr) +MC_HEADER_SIZE ) +if (get_totalsize (mc_hdr) <=intel_microcode_get_datasize (mc_hdr) +MC_HEADER_SIZE )return 0 ; - ext_hdr = mc +get_datasize (mc_hdr) +MC_HEADER_SIZE ; + ext_hdr = mc +intel_microcode_get_datasize (mc_hdr) +MC_HEADER_SIZE ; ext_sig = (void *)ext_hdr +EXT_HEADER_SIZE ;for (i =0 ; i < ext_hdr->count; i++) {
@@ -156,7 +156,7 @@ int intel_microcode_sanity_check(void *mstruct extended_signature *ext_sig; total_size =get_totalsize (mc_header); - data_size =get_datasize (mc_header); + data_size =intel_microcode_get_datasize (mc_header);if (data_size +MC_HEADER_SIZE > total_size) {if (print_err)
@@ -438,7 +438,7 @@ static void show_saved_mc(void) date = mc_saved_header->date; total_size =get_totalsize (mc_saved_header); - data_size =get_datasize (mc_saved_header); + data_size =intel_microcode_get_datasize (mc_saved_header); pr_debug("mc_saved[%d]: sig=0x%x, pf=0x%x, rev=0x%x, total size=0x%x, date = %04x-%02x-%02x\n", i++, sig, pf, rev, total_size,
drivers/platform/x86/intel/ifs/load.c +3 -2
@@ -56,12 +56,13 @@ struct metadata_header {static struct metadata_header *find_meta_data (void *ucode,unsigned int meta_type) { +struct microcode_header_intel *hdr = &((struct microcode_intel *)ucode)->hdr;struct metadata_header *meta_header;unsigned long data_size, total_meta;unsigned long meta_size =0 ; - data_size =get_datasize (ucode); - total_meta = ((struct microcode_intel *)ucode)->hdr.metasize; + data_size =intel_microcode_get_datasize (hdr); + total_meta = hdr->metasize;if (!total_meta)return NULL ;
[tip: x86/microcode] x86/microcode/intel: Rename get_datasize() since its used externally
The following commit has been merged into the x86/microcode branch of tip:
The following commit has been merged into the x86/microcode branch of tip:
Commit-ID: b0e67db12d769cc308a50c1c0ac3721c4f6aead7 Gitweb: https://git.kernel.org/tip/b0e67db12d769cc308a50c1c0ac3721c4f6aead7 Author: Ashok Raj <[email protected]> AuthorDate: Sat, 12 Aug 2023 21:58:45 +02:00 Committer: Borislav Petkov (AMD) <[email protected]> CommitterDate: Sun, 13 Aug 2023 18:42:55 +02:00
x86/microcode/intel: Rename get_datasize() since its used externally
Rename get_datasize() to intel_microcode_get_datasize() and make it an inline.
[ tglx: Make the argument typed and fix up the IFS code ]
--- arch/x86/include/asm/microcode_intel.h | 7 ++++--- arch/x86/kernel/cpu/microcode/intel.c | 8 ++++---- drivers/platform/x86/intel/ifs/load.c | 5 +++-- 3 files changed, 11 insertions(+), 9 deletions(-)
arch/x86/include/asm/microcode_intel.h +4 -3
@@ -26,9 +26,10 @@ struct microcode_intel {#define MC_HEADER_TYPE_IFS 2#define DEFAULT_UCODE_DATASIZE (2000) -#define get_datasize (mc) \ - (((struct microcode_intel *)mc)->hdr.datasize ? \ - ((struct microcode_intel *)mc)->hdr.datasize : DEFAULT_UCODE_DATASIZE) +static inline int intel_microcode_get_datasize (struct microcode_header_intel *hdr) +{ +return hdr->datasize ? :DEFAULT_UCODE_DATASIZE ; +}static inline u32 intel_get_microcode_revision (void ) {
arch/x86/kernel/cpu/microcode/intel.c +4 -4
@@ -118,10 +118,10 @@ int intel_find_matching_signature(void *mc, unsigned int csig, int cpf)return 1 ;/* Look for ext. headers: */ -if (get_totalsize (mc_hdr) <=get_datasize (mc_hdr) +MC_HEADER_SIZE ) +if (get_totalsize (mc_hdr) <=intel_microcode_get_datasize (mc_hdr) +MC_HEADER_SIZE )return 0 ; - ext_hdr = mc +get_datasize (mc_hdr) +MC_HEADER_SIZE ; + ext_hdr = mc +intel_microcode_get_datasize (mc_hdr) +MC_HEADER_SIZE ; ext_sig = (void *)ext_hdr +EXT_HEADER_SIZE ;for (i =0 ; i < ext_hdr->count; i++) {
@@ -156,7 +156,7 @@ int intel_microcode_sanity_check(void *mc, bool print_err, int hdr_type)struct extended_signature *ext_sig; total_size =get_totalsize (mc_header); - data_size =get_datasize (mc_header); + data_size =intel_microcode_get_datasize (mc_header);if (data_size +MC_HEADER_SIZE > total_size) {if (print_err)
@@ -438,7 +438,7 @@ static void show_saved_mc(void) date = mc_saved_header->date; total_size =get_totalsize (mc_saved_header); - data_size =get_datasize (mc_saved_header); + data_size =intel_microcode_get_datasize (mc_saved_header); pr_debug("mc_saved[%d]: sig=0x%x, pf=0x%x, rev=0x%x, total size=0x%x, date = %04x-%02x-%02x\n", i++, sig, pf, rev, total_size,
drivers/platform/x86/intel/ifs/load.c +3 -2
@@ -56,12 +56,13 @@ struct metadata_header {static struct metadata_header *find_meta_data (void *ucode,unsigned int meta_type) { +struct microcode_header_intel *hdr = &((struct microcode_intel *)ucode)->hdr;struct metadata_header *meta_header;unsigned long data_size, total_meta;unsigned long meta_size =0 ; - data_size =get_datasize (ucode); - total_meta = ((struct microcode_intel *)ucode)->hdr.metasize; + data_size =intel_microcode_get_datasize (hdr); + total_meta = hdr->metasize;if (!total_meta)return NULL ;
PATCH v2 7/37 x86/microcode: Move core specific defines to local header
In reply to Thomas GleixnerFrom: Thomas Gleixner <[email protected]>
There is no reason to expose all of this globally. Move everything which is not required outside of the microcode specific code to local header files and into the respective source files.
No functional change.
---
V2: Move AMD specific structs and defines into the AMD code.
--- arch/x86/include/asm/microcode.h | 155 +++++++++---------------------- arch/x86/include/asm/microcode_amd.h | 54 ---------- arch/x86/include/asm/microcode_intel.h | 63 ------------ arch/x86/kernel/cpu/microcode/amd.c | 41 ++++++++ arch/x86/kernel/cpu/microcode/core.c | 3 arch/x86/kernel/cpu/microcode/intel.c | 3 arch/x86/kernel/cpu/microcode/internal.h | 131 ++++++++++++++++++++++++++ 7 files changed, 223 insertions(+), 227 deletions(-)
arch/x86/include/asm/microcode.h +47 -108
@@ -2,138 +2,77 @@#ifndef _ASM_X86_MICROCODE_H#define _ASM_X86_MICROCODE_H -#include <linux/earlycpio.h> -#include <linux/initrd.h> - -#include <asm/cpu.h> -#include <asm/microcode_amd.h> -#include <asm/microcode_intel.h> - -struct ucode_patch { -struct list_head plist; -void *data;/* Intel uses only this one */ -unsigned int size; -u32 patch_id; -u16 equiv_cpu; -}; - -extern struct list_head microcode_cache; -struct cpu_signature {unsigned int sig;unsigned int pf;unsigned int rev; }; -struct device ; - -enum ucode_state { -UCODE_OK =0 , -UCODE_NEW , -UCODE_UPDATED , -UCODE_NFOUND , -UCODE_ERROR , +struct ucode_cpu_info { +struct cpu_signature cpu_sig; +void *mc; }; -struct microcode_ops { -enum ucode_state (*request_microcode_fw) (int cpu,struct device *); - -void (*microcode_fini_cpu) (int cpu); +#ifdef CONFIG_MICROCODE +void load_ucode_bsp (void ); +void load_ucode_ap (void ); +void microcode_bsp_resume (void ); +#else +static inline void load_ucode_bsp (void ) { } +static inline void load_ucode_ap (void ) { } +static inline void microcode_bsp_resume (void ) { } +#endif -/* -* The generic 'microcode_core' part guarantees that -* the callbacks below run on a target cpu when they -* are being called. -* See also the "Synchronization" section in microcode_core.c. -*/ -enum ucode_state (*apply_microcode) (int cpu); -int (*collect_cpu_info) (int cpu,struct cpu_signature *csig); +#ifdef CONFIG_CPU_SUP_INTEL +/* Intel specific microcode defines. Public for IFS */ +struct microcode_header_intel { +unsigned int hdrver; +unsigned int rev; +unsigned int date; +unsigned int sig; +unsigned int cksum; +unsigned int ldrver; +unsigned int pf; +unsigned int datasize; +unsigned int totalsize; +unsigned int metasize; +unsigned int reserved[2 ]; }; -struct ucode_cpu_info { -struct cpu_signature cpu_sig; -void *mc; +struct microcode_intel { +struct microcode_header_intel hdr; +unsigned int bits[]; }; -extern struct ucode_cpu_info ucode_cpu_info[]; -struct cpio_data find_microcode_in_initrd (const char *path,bool use_pa); -#ifdef CONFIG_CPU_SUP_INTEL -extern struct microcode_ops *__init init_intel_microcode (void ); -#else -static inline struct microcode_ops *__init init_intel_microcode (void ) -{ -return NULL ; -} -#endif /* CONFIG_CPU_SUP_INTEL */ +#define DEFAULT_UCODE_DATASIZE (2000) +#define MC_HEADER_SIZE (sizeof(struct microcode_header_intel)) +#define MC_HEADER_TYPE_MICROCODE 1 +#define MC_HEADER_TYPE_IFS 2 -#ifdef CONFIG_CPU_SUP_AMD -extern struct microcode_ops *__init init_amd_microcode (void ); -extern void __exit exit_amd_microcode(void); -#else -static inline struct microcode_ops *__init init_amd_microcode (void ) +static inline int intel_microcode_get_datasize (struct microcode_header_intel *hdr) { -return NULL ; +return hdr->datasize ? :DEFAULT_UCODE_DATASIZE ; } -static inline void __exit exit_amd_microcode(void) {} -#endif - -#define MAX_UCODE_COUNT 128 -#define QCHAR (a, b, c, d) ((a) + ((b) << 8) + ((c) << 16) + ((d) << 24)) -#define CPUID_INTEL1 QCHAR('G', 'e', 'n', 'u') -#define CPUID_INTEL2 QCHAR('i', 'n', 'e', 'I') -#define CPUID_INTEL3 QCHAR('n', 't', 'e', 'l') -#define CPUID_AMD1 QCHAR('A', 'u', 't', 'h') -#define CPUID_AMD2 QCHAR('e', 'n', 't', 'i') -#define CPUID_AMD3 QCHAR('c', 'A', 'M', 'D') - -#define CPUID_IS (a, b, c, ebx, ecx, edx) \ - (!((ebx ^ (a))|(edx ^ (b))|(ecx ^ (c)))) - -/* -* In early loading microcode phase on BSP, boot_cpu_data is not set up yet. -* x86_cpuid_vendor() gets vendor id for BSP. -* -* In 32 bit AP case, accessing boot_cpu_data needs linear address. To simplify -* coding, we still use x86_cpuid_vendor() to get vendor id for AP. -* -* x86_cpuid_vendor() gets vendor information directly from CPUID. -*/ -static inline int x86_cpuid_vendor (void ) +static inline u32 intel_get_microcode_revision (void ) { -u32 eax =0x00000000 ; -u32 ebx, ecx =0 , edx; +u32 rev, dummy; -native_cpuid (&eax, &ebx, &ecx, &edx); +native_wrmsrl (MSR_IA32_UCODE_REV ,0 ); -if (CPUID_IS (CPUID_INTEL1 ,CPUID_INTEL2 ,CPUID_INTEL3 , ebx, ecx, edx)) -return X86_VENDOR_INTEL ; +/* As documented in the SDM: Do a CPUID 1 here */ +native_cpuid_eax (1 ); -if (CPUID_IS (CPUID_AMD1 ,CPUID_AMD2 ,CPUID_AMD3 , ebx, ecx, edx)) -return X86_VENDOR_AMD ; +/* get the current revision from MSR 0x8B */ +native_rdmsr (MSR_IA32_UCODE_REV , dummy, rev); -return X86_VENDOR_UNKNOWN ; +return rev; } -static inline unsigned int x86_cpuid_family (void ) -{ -u32 eax =0x00000001 ; -u32 ebx, ecx =0 , edx; - -native_cpuid (&eax, &ebx, &ecx, &edx); - -return x86_family (eax); -} +void show_ucode_info_early (void ); -#ifdef CONFIG_MICROCODE -extern void __init load_ucode_bsp (void ); -extern void load_ucode_ap (void ); -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 microcode_bsp_resume (void ) { } -#endif +#else /* CONFIG_CPU_SUP_INTEL */ +static inline void show_ucode_info_early (void ) { } +#endif /* !CONFIG_CPU_SUP_INTEL */ #endif /* _ASM_X86_MICROCODE_H */
arch/x86/include/asm/microcode_amd.h deleted +0 -54
54 lines not on this page · raw message
arch/x86/include/asm/microcode_intel.h deleted +0 -63
63 lines not on this page · raw message
arch/x86/kernel/cpu/microcode/amd.c +41 -0
47 lines not on this page · raw message
arch/x86/kernel/cpu/microcode/core.c +2 -1
@@ -33,11 +33,12 @@#include <asm/cpu_device_id.h> #include <asm/perf_event.h> -#include <asm/microcode.h> #include <asm/processor.h> #include <asm/cmdline.h> #include <asm/setup.h> +#include "internal.h" +#define DRIVER_VERSION "2.2"static struct microcode_ops *microcode_ops;
arch/x86/kernel/cpu/microcode/intel.c +2 -1
@@ -32,11 +32,12 @@#include <asm/intel-family.h> #include <asm/processor.h> -#include <asm/microcode.h> #include <asm/tlbflush.h> #include <asm/setup.h> #include <asm/msr.h> +#include "internal.h" +static const char ucode_path[] ="kernel/x86/microcode/GenuineIntel.bin" ;/* Current microcode patch used in early patching on the APs. */
arch/x86/kernel/cpu/microcode/internal.h new file +131 -0
131 lines not on this page · raw message
[tip: x86/microcode] x86/microcode: Move core specific defines to local header
The following commit has been merged into the x86/microcode branch of tip:
The following commit has been merged into the x86/microcode branch of tip:
Commit-ID: d02a0efd0f471a775b9e28ce6593fc8b3cc46e8c Gitweb: https://git.kernel.org/tip/d02a0efd0f471a775b9e28ce6593fc8b3cc46e8c Author: Thomas Gleixner <[email protected]> AuthorDate: Sat, 12 Aug 2023 21:58:47 +02:00 Committer: Borislav Petkov (AMD) <[email protected]> CommitterDate: Sun, 13 Aug 2023 18:42:55 +02:00
x86/microcode: Move core specific defines to local header
There is no reason to expose all of this globally. Move everything which is not required outside of the microcode specific code to local header files and into the respective source files.
No functional change.
--- arch/x86/include/asm/microcode.h | 155 ++++++---------------- arch/x86/include/asm/microcode_amd.h | 54 +-------- arch/x86/include/asm/microcode_intel.h | 63 +--------- arch/x86/kernel/cpu/microcode/amd.c | 41 ++++++- arch/x86/kernel/cpu/microcode/core.c | 3 +- arch/x86/kernel/cpu/microcode/intel.c | 3 +- arch/x86/kernel/cpu/microcode/internal.h | 131 +++++++++++++++++++- 7 files changed, 223 insertions(+), 227 deletions(-) delete mode 100644 arch/x86/include/asm/microcode_amd.h delete mode 100644 arch/x86/include/asm/microcode_intel.h create mode 100644 arch/x86/kernel/cpu/microcode/internal.h
arch/x86/include/asm/microcode.h +47 -108
@@ -2,138 +2,77 @@#ifndef _ASM_X86_MICROCODE_H#define _ASM_X86_MICROCODE_H -#include <linux/earlycpio.h> -#include <linux/initrd.h> - -#include <asm/cpu.h> -#include <asm/microcode_amd.h> -#include <asm/microcode_intel.h> - -struct ucode_patch { -struct list_head plist; -void *data;/* Intel uses only this one */ -unsigned int size; -u32 patch_id; -u16 equiv_cpu; -}; - -extern struct list_head microcode_cache; -struct cpu_signature {unsigned int sig;unsigned int pf;unsigned int rev; }; -struct device ; - -enum ucode_state { -UCODE_OK =0 , -UCODE_NEW , -UCODE_UPDATED , -UCODE_NFOUND , -UCODE_ERROR , -}; - -struct microcode_ops { -enum ucode_state (*request_microcode_fw) (int cpu,struct device *); - -void (*microcode_fini_cpu) (int cpu); - -/* -* The generic 'microcode_core' part guarantees that -* the callbacks below run on a target cpu when they -* are being called. -* See also the "Synchronization" section in microcode_core.c. -*/ -enum ucode_state (*apply_microcode) (int cpu); -int (*collect_cpu_info) (int cpu,struct cpu_signature *csig); -}; -struct ucode_cpu_info {struct cpu_signature cpu_sig;void *mc; }; -extern struct ucode_cpu_info ucode_cpu_info[]; -struct cpio_data find_microcode_in_initrd (const char *path,bool use_pa); -#ifdef CONFIG_CPU_SUP_INTEL -extern struct microcode_ops *__init init_intel_microcode (void ); -#else -static inline struct microcode_ops *__init init_intel_microcode (void ) -{ -return NULL ; -} -#endif /* CONFIG_CPU_SUP_INTEL */ - -#ifdef CONFIG_CPU_SUP_AMD -extern struct microcode_ops *__init init_amd_microcode (void ); -extern void __exit exit_amd_microcode(void); +#ifdef CONFIG_MICROCODE +void load_ucode_bsp (void ); +void load_ucode_ap (void ); +void microcode_bsp_resume (void );#else -static inline struct microcode_ops *__init init_amd_microcode (void ) -{ -return NULL ; -} -static inline void __exit exit_amd_microcode(void) {} +static inline void load_ucode_bsp (void ) { } +static inline void load_ucode_ap (void ) { } +static inline void microcode_bsp_resume (void ) { }#endif -#define MAX_UCODE_COUNT 128 +#ifdef CONFIG_CPU_SUP_INTEL +/* Intel specific microcode defines. Public for IFS */ +struct microcode_header_intel { +unsigned int hdrver; +unsigned int rev; +unsigned int date; +unsigned int sig; +unsigned int cksum; +unsigned int ldrver; +unsigned int pf; +unsigned int datasize; +unsigned int totalsize; +unsigned int metasize; +unsigned int reserved[2 ]; +}; -#define QCHAR (a, b, c, d) ((a) + ((b) << 8) + ((c) << 16) + ((d) << 24)) -#define CPUID_INTEL1 QCHAR('G', 'e', 'n', 'u') -#define CPUID_INTEL2 QCHAR('i', 'n', 'e', 'I') -#define CPUID_INTEL3 QCHAR('n', 't', 'e', 'l') -#define CPUID_AMD1 QCHAR('A', 'u', 't', 'h') -#define CPUID_AMD2 QCHAR('e', 'n', 't', 'i') -#define CPUID_AMD3 QCHAR('c', 'A', 'M', 'D') +struct microcode_intel { +struct microcode_header_intel hdr; +unsigned int bits[]; +}; -#define CPUID_IS (a, b, c, ebx, ecx, edx) \ - (!((ebx ^ (a))|(edx ^ (b))|(ecx ^ (c)))) +#define DEFAULT_UCODE_DATASIZE (2000) +#define MC_HEADER_SIZE (sizeof(struct microcode_header_intel)) +#define MC_HEADER_TYPE_MICROCODE 1 +#define MC_HEADER_TYPE_IFS 2 -/* -* In early loading microcode phase on BSP, boot_cpu_data is not set up yet. -* x86_cpuid_vendor() gets vendor id for BSP. -* -* In 32 bit AP case, accessing boot_cpu_data needs linear address. To simplify -* coding, we still use x86_cpuid_vendor() to get vendor id for AP. -* -* x86_cpuid_vendor() gets vendor information directly from CPUID. -*/ -static inline int x86_cpuid_vendor (void ) +static inline int intel_microcode_get_datasize (struct microcode_header_intel *hdr) { -u32 eax =0x00000000 ; -u32 ebx, ecx =0 , edx; - -native_cpuid (&eax, &ebx, &ecx, &edx); - -if (CPUID_IS (CPUID_INTEL1 ,CPUID_INTEL2 ,CPUID_INTEL3 , ebx, ecx, edx)) -return X86_VENDOR_INTEL ; - -if (CPUID_IS (CPUID_AMD1 ,CPUID_AMD2 ,CPUID_AMD3 , ebx, ecx, edx)) -return X86_VENDOR_AMD ; - -return X86_VENDOR_UNKNOWN ; +return hdr->datasize ? :DEFAULT_UCODE_DATASIZE ; } -static inline unsigned int x86_cpuid_family (void ) +static inline u32 intel_get_microcode_revision (void ) { -u32 eax =0x00000001 ; -u32 ebx, ecx =0 , edx; +u32 rev, dummy; + +native_wrmsrl (MSR_IA32_UCODE_REV ,0 ); -native_cpuid (&eax, &ebx, &ecx, &edx); +/* As documented in the SDM: Do a CPUID 1 here */ +native_cpuid_eax (1 ); -return x86_family (eax); +/* get the current revision from MSR 0x8B */ +native_rdmsr (MSR_IA32_UCODE_REV , dummy, rev); + +return rev; } -#ifdef CONFIG_MICROCODE -extern void __init load_ucode_bsp (void ); -extern void load_ucode_ap (void ); -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 microcode_bsp_resume (void ) { } -#endif +void show_ucode_info_early (void ); + +#else /* CONFIG_CPU_SUP_INTEL */ +static inline void show_ucode_info_early (void ) { } +#endif /* !CONFIG_CPU_SUP_INTEL */ #endif /* _ASM_X86_MICROCODE_H */
arch/x86/include/asm/microcode_amd.h deleted +0 -54
54 lines not on this page · raw message
arch/x86/include/asm/microcode_intel.h deleted +0 -63
63 lines not on this page · raw message
arch/x86/kernel/cpu/microcode/amd.c +41 -0
47 lines not on this page · raw message
arch/x86/kernel/cpu/microcode/core.c +2 -1
@@ -33,11 +33,12 @@#include <asm/cpu_device_id.h> #include <asm/perf_event.h> -#include <asm/microcode.h> #include <asm/processor.h> #include <asm/cmdline.h> #include <asm/setup.h> +#include "internal.h" +#define DRIVER_VERSION "2.2"static struct microcode_ops *microcode_ops;
arch/x86/kernel/cpu/microcode/intel.c +2 -1
@@ -32,11 +32,12 @@#include <asm/intel-family.h> #include <asm/processor.h> -#include <asm/microcode.h> #include <asm/tlbflush.h> #include <asm/setup.h> #include <asm/msr.h> +#include "internal.h" +static const char ucode_path[] ="kernel/x86/microcode/GenuineIntel.bin" ;/* Current microcode patch used in early patching on the APs. */
arch/x86/kernel/cpu/microcode/internal.h new file +131 -0
131 lines not on this page · raw message
PATCH v2 8/37 x86/microcode/intel: Remove debug code
In reply to Thomas GleixnerFrom: Thomas Gleixner <[email protected]>
This is really of dubious value.
---
V2: Reordered in the series - Nikolay
--- arch/x86/kernel/cpu/microcode/intel.c | 75 ---------------------------------- 1 file changed, 75 deletions(-)
---
arch/x86/kernel/cpu/microcode/intel.c +0 -75
@@ -10,15 +10,7 @@* Copyright (C) 2012 Fenghua Yu <[email protected]> * H Peter Anvin" <[email protected]> */ - -/* -* This needs to be before all headers so that pr_debug in printk.h doesn't turn -* printk calls into no_printk(). -* -*#define DEBUG -*/ #define pr_fmt (fmt) "microcode: " fmt -#include <linux/earlycpio.h> #include <linux/firmware.h> #include <linux/uaccess.h>
@@ -405,69 +397,6 @@ scan_microcode(void *data, size_t size,return patch; } -static void show_saved_mc (void ) -{ -#ifdef DEBUG -int i =0 , j; -unsigned int sig, pf, rev, total_size, data_size, date; -struct ucode_cpu_info uci; -struct ucode_patch *p; - -if (list_empty (µcode_cache)) { -pr_debug ("no microcode data saved.\n" ); -return ; - } - -intel_cpu_collect_info (&uci); - - sig = uci.cpu_sig.sig; - pf = uci.cpu_sig.pf; - rev = uci.cpu_sig.rev; -pr_debug ("CPU: sig=0x%x, pf=0x%x, rev=0x%x\n" , sig, pf, rev); - -list_for_each_entry (p, µcode_cache, plist) { -struct microcode_header_intel *mc_saved_header; -struct extended_sigtable *ext_header; -struct extended_signature *ext_sig; -int ext_sigcount; - - mc_saved_header = (struct microcode_header_intel *)p->data; - - sig = mc_saved_header->sig; - pf = mc_saved_header->pf; - rev = mc_saved_header->rev; - date = mc_saved_header->date; - - total_size =get_totalsize (mc_saved_header); - data_size =intel_microcode_get_datasize (mc_saved_header); - -pr_debug ("mc_saved[%d]: sig=0x%x, pf=0x%x, rev=0x%x, total size=0x%x, date = %04x-%02x-%02x\n" , - i++, sig, pf, rev, total_size, - date &0xffff , - date >>24 , - (date >>16 ) &0xff ); - -/* Look for ext. headers: */ -if (total_size <= data_size +MC_HEADER_SIZE ) -continue ; - - ext_header = (void *)mc_saved_header + data_size +MC_HEADER_SIZE ; - ext_sigcount = ext_header->count; - ext_sig = (void *)ext_header +EXT_HEADER_SIZE ; - -for (j =0 ; j < ext_sigcount; j++) { - sig = ext_sig->sig; - pf = ext_sig->pf; - -pr_debug ("\tExtended[%d]: sig=0x%x, pf=0x%x\n" , - j, sig, pf); - - ext_sig++; - } - } -#endif -} -/* * Save this microcode patch. It will be loaded early when a CPU is * hot-added or resumes.
@@ -480,7 +409,6 @@ static void save_mc_for_early(struct ucomutex_lock (&x86_cpu_microcode_mutex);save_microcode_patch (uci, mc, size); -show_saved_mc ();mutex_unlock (&x86_cpu_microcode_mutex); }
@@ -631,9 +559,6 @@ int __init save_microcode_in_initrd_inteintel_cpu_collect_info (&uci);scan_microcode (cp.data, cp.size, &uci, true); - -show_saved_mc (); -return 0 ; }
[tip: x86/microcode] x86/microcode/intel: Remove debug code
The following commit has been merged into the x86/microcode branch of tip:
The following commit has been merged into the x86/microcode branch of tip:
Commit-ID: d44450c5939949fe37e4e94db69429de1ab1aad6 Gitweb: https://git.kernel.org/tip/d44450c5939949fe37e4e94db69429de1ab1aad6 Author: Thomas Gleixner <[email protected]> AuthorDate: Sat, 12 Aug 2023 21:58:49 +02:00 Committer: Borislav Petkov (AMD) <[email protected]> CommitterDate: Sun, 13 Aug 2023 18:42:55 +02:00
x86/microcode/intel: Remove debug code
This is really of dubious value.
--- arch/x86/kernel/cpu/microcode/intel.c | 75 +-------------------------- 1 file changed, 75 deletions(-)
arch/x86/kernel/cpu/microcode/intel.c +0 -75
@@ -10,15 +10,7 @@* Copyright (C) 2012 Fenghua Yu <[email protected]> * H Peter Anvin" <[email protected]> */ - -/* -* This needs to be before all headers so that pr_debug in printk.h doesn't turn -* printk calls into no_printk(). -* -*#define DEBUG -*/ #define pr_fmt (fmt) "microcode: " fmt -#include <linux/earlycpio.h> #include <linux/firmware.h> #include <linux/uaccess.h>
@@ -405,69 +397,6 @@ next:return patch; } -static void show_saved_mc (void ) -{ -#ifdef DEBUG -int i =0 , j; -unsigned int sig, pf, rev, total_size, data_size, date; -struct ucode_cpu_info uci; -struct ucode_patch *p; - -if (list_empty (µcode_cache)) { -pr_debug ("no microcode data saved.\n" ); -return ; - } - -intel_cpu_collect_info (&uci); - - sig = uci.cpu_sig.sig; - pf = uci.cpu_sig.pf; - rev = uci.cpu_sig.rev; -pr_debug ("CPU: sig=0x%x, pf=0x%x, rev=0x%x\n" , sig, pf, rev); - -list_for_each_entry (p, µcode_cache, plist) { -struct microcode_header_intel *mc_saved_header; -struct extended_sigtable *ext_header; -struct extended_signature *ext_sig; -int ext_sigcount; - - mc_saved_header = (struct microcode_header_intel *)p->data; - - sig = mc_saved_header->sig; - pf = mc_saved_header->pf; - rev = mc_saved_header->rev; - date = mc_saved_header->date; - - total_size =get_totalsize (mc_saved_header); - data_size =intel_microcode_get_datasize (mc_saved_header); - -pr_debug ("mc_saved[%d]: sig=0x%x, pf=0x%x, rev=0x%x, total size=0x%x, date = %04x-%02x-%02x\n" , - i++, sig, pf, rev, total_size, - date &0xffff , - date >>24 , - (date >>16 ) &0xff ); - -/* Look for ext. headers: */ -if (total_size <= data_size +MC_HEADER_SIZE ) -continue ; - - ext_header = (void *)mc_saved_header + data_size +MC_HEADER_SIZE ; - ext_sigcount = ext_header->count; - ext_sig = (void *)ext_header +EXT_HEADER_SIZE ; - -for (j =0 ; j < ext_sigcount; j++) { - sig = ext_sig->sig; - pf = ext_sig->pf; - -pr_debug ("\tExtended[%d]: sig=0x%x, pf=0x%x\n" , - j, sig, pf); - - ext_sig++; - } - } -#endif -} -/* * Save this microcode patch. It will be loaded early when a CPU is * hot-added or resumes.
@@ -480,7 +409,6 @@ static void save_mc_for_early(struct ucode_cpu_info *uci, u8 *mc, unsigned int smutex_lock (&x86_cpu_microcode_mutex);save_microcode_patch (uci, mc, size); -show_saved_mc ();mutex_unlock (&x86_cpu_microcode_mutex); }
@@ -631,9 +559,6 @@ int __init save_microcode_in_initrd_intel(void)intel_cpu_collect_info (&uci);scan_microcode (cp.data, cp.size, &uci, true); - -show_saved_mc (); -return 0 ; }
PATCH v2 9/37 x86/microcode/intel: Remove pointless mutex
In reply to Thomas GleixnerFrom: Thomas Gleixner <[email protected]>
There is no concurreny.
--- arch/x86/kernel/cpu/microcode/intel.c | 24 ++---------------------- 1 file changed, 2 insertions(+), 22 deletions(-)
---
arch/x86/kernel/cpu/microcode/intel.c +2 -22
@@ -397,22 +397,6 @@ scan_microcode(void *data, size_t size,return patch; } -/* -* Save this microcode patch. It will be loaded early when a CPU is -* hot-added or resumes. -*/ -static void save_mc_for_early (struct ucode_cpu_info *uci,u8 *mc,unsigned int size) -{ -/* Synchronization during CPU hotplug. */ -static DEFINE_MUTEX (x86_cpu_microcode_mutex); - -mutex_lock (&x86_cpu_microcode_mutex); - -save_microcode_patch (uci, mc, size); - -mutex_unlock (&x86_cpu_microcode_mutex); -} -static bool load_builtin_intel_microcode (struct cpio_data *cp) {unsigned int eax =1 , ebx, ecx =0 , edx;
@@ -829,12 +813,8 @@ static enum ucode_state generic_load_micvfree (uci->mc); uci->mc = (struct microcode_intel *)new_mc; -/* -* If early loading microcode is supported, save this mc into -* permanent memory. So it will be loaded early when a CPU is hot added -* or resumes. -*/ -save_mc_for_early (uci, new_mc, new_mc_size); +/* Save for CPU hotplug */ +save_microcode_patch (uci, new_mc, new_mc_size);pr_debug ("CPU%d found a matching microcode update with version 0x%x (current=0x%x)\n" , cpu, new_rev, uci->cpu_sig.rev);
Re: [patch V2 09/37] x86/microcode/intel: Remove pointless mutex
Quoted 22 lines · Thomas Gleixner
From: Thomas Gleixner <[email protected]>
There is no concurreny.
Signed-off-by Thomas Gleixner <[email protected]>--- arch/x86/kernel/cpu/microcode/intel.c | 24 ++---------------------- 1 file changed, 2 insertions(+), 22 deletions(-)---
arch/x86/kernel/cpu/microcode/intel.c +0
-6@@ -397,22 +397,6 @@ scan_microcode(void *data, size_t size,return patch; } -/* -* Save this microcode patch. It will be loaded early when a CPU is -* hot-added or resumes. -*/ -static void save_mc_for_early (struct ucode_cpu_info *uci,u8 *mc,unsigned int size) -{
arch/x86/kernel/cpu/microcode/intel.c scan_microcode(void *data, size_t size, · line 415
Quoted 10 lines
arch/x86/kernel/cpu/microcode/intel.c +0
-10-/* Synchronization during CPU hotplug. */ -static DEFINE_MUTEX (x86_cpu_microcode_mutex); - -mutex_lock (&x86_cpu_microcode_mutex); - -save_microcode_patch (uci, mc, size); - -mutex_unlock (&x86_cpu_microcode_mutex); -} -
So this function is ultimately invoked from a write to the 'reload' sysfs file, what about 2 racing writes to this file ? Though if it's about cpu hotplug then in reload_store() this is handled via cpus_read_lock I guess so indeed this is correct.
Re: [patch V2 09/37] x86/microcode/intel: Remove pointless mutex
Quoted 6 lines · Nikolay Borisov
On 12.08.23 г. 22:58 ч., Thomas Gleixner wrote:
So this function is ultimately invoked from a write to the 'reload' sysfs file, what about 2 racing writes to this file ? Though if it's about cpu hotplug then in
reload_store()this is handled viacpus_read_lockI guess so indeed this is correct.
80347cd515ca ("x86/microcode: Remove microcode_mutex")
[tip: x86/microcode] x86/microcode/intel: Remove pointless mutex
In reply to Thomas GleixnerThe following commit has been merged into the x86/microcode branch of tip:
The following commit has been merged into the x86/microcode branch of tip:
Commit-ID: d2700f406713d04573803b9fe8a0fb02c015df39 Gitweb: https://git.kernel.org/tip/d2700f406713d04573803b9fe8a0fb02c015df39 Author: Thomas Gleixner <[email protected]> AuthorDate: Sat, 12 Aug 2023 21:58:50 +02:00 Committer: Borislav Petkov (AMD) <[email protected]> CommitterDate: Sun, 13 Aug 2023 18:42:55 +02:00
x86/microcode/intel: Remove pointless mutex
There is no concurrency.
--- arch/x86/kernel/cpu/microcode/intel.c | 24 ++---------------------- 1 file changed, 2 insertions(+), 22 deletions(-)
arch/x86/kernel/cpu/microcode/intel.c +2 -22
@@ -397,22 +397,6 @@ next:return patch; } -/* -* Save this microcode patch. It will be loaded early when a CPU is -* hot-added or resumes. -*/ -static void save_mc_for_early (struct ucode_cpu_info *uci,u8 *mc,unsigned int size) -{ -/* Synchronization during CPU hotplug. */ -static DEFINE_MUTEX (x86_cpu_microcode_mutex); - -mutex_lock (&x86_cpu_microcode_mutex); - -save_microcode_patch (uci, mc, size); - -mutex_unlock (&x86_cpu_microcode_mutex); -} -static bool load_builtin_intel_microcode (struct cpio_data *cp) {unsigned int eax =1 , ebx, ecx =0 , edx;
@@ -829,12 +813,8 @@ static enum ucode_state generic_load_microcode(int cpu, struct iov_iter *iter)vfree (uci->mc); uci->mc = (struct microcode_intel *)new_mc; -/* -* If early loading microcode is supported, save this mc into -* permanent memory. So it will be loaded early when a CPU is hot added -* or resumes. -*/ -save_mc_for_early (uci, new_mc, new_mc_size); +/* Save for CPU hotplug */ +save_microcode_patch (uci, new_mc, new_mc_size);pr_debug ("CPU%d found a matching microcode update with version 0x%x (current=0x%x)\n" , cpu, new_rev, uci->cpu_sig.rev);
PATCH v2 10/37 x86/microcode/intel: Rip out mixed stepping support for Intel CPUs
In reply to Thomas GleixnerFrom: Ashok Raj <[email protected]>
Mixed steppings aren't supported on Intel CPUs. Only one patch is required for the entire system. The caching of micro code blobs which match the family and model is therefore pointless and in fact it is disfunctional as CPU hotplug updates use only a single microcode blob, i.e. the one where *intel_ucode_patch points to.
Remove the microcode cache and make it an AMD local feature.
[ tglx: Save only at the end. Otherwise random microcode ends up in the
pointer for early loading ]
---
V2: Fix the bogus condition - Borislav
--- arch/x86/kernel/cpu/microcode/amd.c | 10 ++ arch/x86/kernel/cpu/microcode/core.c | 2 arch/x86/kernel/cpu/microcode/intel.c | 135 +++++-------------------------- arch/x86/kernel/cpu/microcode/internal.h | 10 -- 4 files changed, 36 insertions(+), 121 deletions(-)
---
arch/x86/kernel/cpu/microcode/amd.c +10 -0
@@ -37,6 +37,16 @@#include "internal.h" +struct ucode_patch { +struct list_head plist; +void *data; +unsigned int size; +u32 patch_id; +u16 equiv_cpu; +}; + +static LIST_HEAD (microcode_cache); +#define UCODE_MAGIC 0x00414d44#define UCODE_EQUIV_CPU_TABLE_TYPE 0x00000000#define UCODE_UCODE_TYPE 0x00000001
arch/x86/kernel/cpu/microcode/core.c +0 -2
@@ -46,8 +46,6 @@ static bool dis_ucode_ldr = true;bool initrd_gone; -LIST_HEAD (microcode_cache); -/* * Synchronization. *
arch/x86/kernel/cpu/microcode/intel.c +26 -109
215 lines not on this page · raw message
arch/x86/kernel/cpu/microcode/internal.h +0 -10
@@ -8,16 +8,6 @@#include <asm/cpu.h> #include <asm/microcode.h> -struct ucode_patch { -struct list_head plist; -void *data;/* Intel uses only this one */ -unsigned int size; -u32 patch_id; -u16 equiv_cpu; -}; - -extern struct list_head microcode_cache; -struct device ;enum ucode_state {
PATCH v2 11/37 x86/microcode/intel: Simplify scan_microcode()
In reply to Thomas GleixnerMake it readable and comprehensible.
--- arch/x86/kernel/cpu/microcode/intel.c | 28 +++++++--------------------- 1 file changed, 7 insertions(+), 21 deletions(-)
arch/x86/kernel/cpu/microcode/intel.c +7 -21
@@ -275,22 +275,16 @@ static void save_microcode_patch(void *d intel_ucode_patch = (struct microcode_intel *)p; } -/* -* Get microcode matching with BSP's model. Only CPUs with the same model as -* BSP can stay in the platform. -*/ -static struct microcode_intel * -scan_microcode (void *data,size_t size,struct ucode_cpu_info *uci,bool save) +/* Scan CPIO for microcode matching the boot CPUs family, model, stepping */ +static struct microcode_intel *scan_microcode (void *data,size_t size, +struct ucode_cpu_info *uci,bool save) {struct microcode_header_intel *mc_header;struct microcode_intel *patch =NULL ;u32 cur_rev = uci->cpu_sig.rev;unsigned int mc_size; -while (size) { -if (size <sizeof (struct microcode_header_intel )) -break ; - +for (; size >=sizeof (struct microcode_header_intel ); size -= mc_size, data += mc_size) { mc_header = (struct microcode_header_intel *)data; mc_size =get_totalsize (mc_header);
@@ -298,27 +292,19 @@ scan_microcode(void *data, size_t size, intel_microcode_sanity_check(data, false, MC_HEADER_TYPE_MICROCODE) < 0) break; - size -= mc_size; - -if (!intel_find_matching_signature (data, uci->cpu_sig.sig, - uci->cpu_sig.pf)) { - data += mc_size; +if (!intel_find_matching_signature (data, uci->cpu_sig.sig, uci->cpu_sig.pf))continue ; - }/* BSP scan: Check whether there is newer microcode */ if (!save && cur_rev >= mc_header->rev) - goto next; +continue ;/* Save scan: Check whether there is newer or matching microcode */ if (save && cur_rev != mc_header->rev) - goto next; +continue ; patch = data; cur_rev = mc_header->rev; - -next: - data += mc_size; }if (size)
PATCH v2 12/37 x86/microcode/intel: Simplify and rename generic_load_microcode()
In reply to Thomas Gleixnerso it becomes less obfuscated and rename it because there is nothing generic about it.
--- arch/x86/kernel/cpu/microcode/intel.c | 47 ++++++++++++---------------------- 1 file changed, 17 insertions(+), 30 deletions(-)
arch/x86/kernel/cpu/microcode/intel.c +17 -30
@@ -240,19 +240,6 @@ int intel_microcode_sanity_check(void *m }EXPORT_SYMBOL_GPL (intel_microcode_sanity_check); -/* -* Returns 1 if update has been found, 0 otherwise. -*/ -static int has_newer_microcode (void *mc,unsigned int csig,int cpf,int new_rev) -{ -struct microcode_header_intel *mc_hdr = mc; - -if (mc_hdr->rev <= new_rev) -return 0 ; - -return intel_find_matching_signature (mc, csig, cpf); -} -static void save_microcode_patch (void *data,unsigned int size) {struct microcode_header_intel *p;
@@ -645,14 +632,12 @@ static enum ucode_state apply_microcode_return ret; } -static enum ucode_state generic_load_microcode (int cpu,struct iov_iter *iter) +static enum ucode_state read_ucode_intel (int cpu,struct iov_iter *iter) {struct ucode_cpu_info *uci = ucode_cpu_info + cpu;unsigned int curr_mc_size =0 , new_mc_size =0 ; -enum ucode_state ret =UCODE_OK ; -int new_rev = uci->cpu_sig.rev; +int cur_rev = uci->cpu_sig.rev;u8 *new_mc =NULL , *mc =NULL ; -unsigned int csig, cpf;while (iov_iter_count (iter)) {struct microcode_header_intel mc_header;
@@ -669,6 +654,7 @@ static enum ucode_state generic_load_micpr_err ("error! Bad data in microcode data file (totalsize too small)\n" );break ; } + data_size = mc_size -sizeof (mc_header);if (data_size >iov_iter_count (iter)) {pr_err ("error! Bad data in microcode data file (truncated file?)\n" );
@@ -691,16 +677,17 @@ static enum ucode_state generic_load_micbreak ; } - csig = uci->cpu_sig.sig; - cpf = uci->cpu_sig.pf; -if (has_newer_microcode (mc, csig, cpf, new_rev)) { -vfree (new_mc); - new_rev = mc_header.rev; - new_mc = mc; - new_mc_size = mc_size; - mc =NULL ;/* trigger new vmalloc */ - ret =UCODE_NEW ; - } +if (cur_rev >= mc_header.rev) +continue ; + +if (!intel_find_matching_signature (mc, uci->cpu_sig.sig, uci->cpu_sig.pf)) +continue ; + +vfree (new_mc); + cur_rev = mc_header.rev; + new_mc = mc; + new_mc_size = mc_size; + mc =NULL ; }vfree (mc);
@@ -720,9 +707,9 @@ static enum ucode_state generic_load_micsave_microcode_patch (new_mc, new_mc_size);pr_debug ("CPU%d found a matching microcode update with version 0x%x (current=0x%x)\n" , - cpu, new_rev, uci->cpu_sig.rev); + cpu, cur_rev, uci->cpu_sig.rev); -return ret; +return UCODE_NEW ; }static bool is_blacklisted (unsigned int cpu)
@@ -771,7 +758,7 @@ static enum ucode_state request_microcod kvec.iov_base = (void *)firmware->data; kvec.iov_len = firmware->size;iov_iter_kvec (&iter,ITER_SOURCE , &kvec,1 , firmware->size); - ret =generic_load_microcode (cpu, &iter); + ret =read_ucode_intel (cpu, &iter);release_firmware (firmware);
Borislav Petkov
maintainerRe: [patch V2 12/37] x86/microcode/intel: Simplify and rename generic_load_microcode()
Review static enum ucode_state apply_microcode_ · line 635
Quoted 6 lines · Thomas Gleixner
(quoted diff) +1
-1@@ -645,14 +632,12 @@ static enum ucode_state apply_microcode_ return ret; } -static enum ucode_state generic_load_microcode(int cpu, struct iov_iter *iter) +static enum ucode_state read_ucode_intel(int cpu, struct iov_iter *iter)
I'd do this ontop:
---
arch/x86/kernel/cpu/microcode/intel.c +2 -2
@@ -632,7 +632,7 @@ static enum ucode_state apply_microcode_intel(int cpu)return ret; } -static enum ucode_state read_ucode_intel (int cpu,struct iov_iter *iter) +static enum ucode_state parse_microcode_blobs (int cpu,struct iov_iter *iter) {struct ucode_cpu_info *uci = ucode_cpu_info + cpu;unsigned int curr_mc_size =0 , new_mc_size =0 ;
@@ -758,7 +758,7 @@ static enum ucode_state request_microcode_fw(int cpu, struct device *device) kvec.iov_base = (void *)firmware->data; kvec.iov_len = firmware->size;iov_iter_kvec (&iter,ITER_SOURCE , &kvec,1 , firmware->size); - ret =read_ucode_intel (cpu, &iter); + ret =parse_microcode_blobs (cpu, &iter);release_firmware (firmware);
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteRe: [patch V2 12/37] x86/microcode/intel: Simplify and rename generic_load_microcode()
Quoted 9 lines · Borislav Petkov
arch/x86/kernel/cpu/microcode/intel.c static enum ucode_state apply_microcode_ · line 635
Quoted 6 lines · Thomas Gleixner
arch/x86/kernel/cpu/microcode/intel.c +1
-1@@ -645,14 +632,12 @@ static enum ucode_state apply_microcode_return ret; } -static enum ucode_state generic_load_microcode (int cpu,struct iov_iter *iter) +static enum ucode_state read_ucode_intel (int cpu,struct iov_iter *iter)I'd do this ontop:
Sure.
PATCH v2 13/37 x86/microcode/intel: Cleanup code further
In reply to Thomas GleixnerFrom: Thomas Gleixner <[email protected]>
Sanitize the microcode scan loop, fixup printks and move the initrd loading function next to the place where it is used and mark it __init.
---
V2: Fix changelog - Nikolay
--- arch/x86/kernel/cpu/microcode/intel.c | 82 +++++++++++++--------------------- 1 file changed, 33 insertions(+), 49 deletions(-)
---
arch/x86/kernel/cpu/microcode/intel.c +33 -49
@@ -36,7 +36,7 @@ static const char ucode_path[] = "kernelstatic struct microcode_intel *intel_ucode_patch__read_mostly ;/* last level cache size per core */ -static int llc_size_per_core__ro_after_init ; +static unsigned int llc_size_per_core__ro_after_init ;/* microcode format is extended from prescott processors */ struct extended_signature {
@@ -303,37 +303,10 @@ static struct microcode_intel *scan_micrreturn patch; } -static bool load_builtin_intel_microcode (struct cpio_data *cp) -{ -unsigned int eax =1 , ebx, ecx =0 , edx; -struct firmware fw; -char name[30 ]; - -if (IS_ENABLED (CONFIG_X86_32 )) -return false; - -native_cpuid (&eax, &ebx, &ecx, &edx); - -sprintf (name,"intel-ucode/%02x-%02x-%02x" , -x86_family (eax),x86_model (eax),x86_stepping (eax)); - -if (firmware_request_builtin (&fw, name)) { - cp->size = fw.size; - cp->data = (void *)fw.data; -return true; - } - -return false; -} -static void print_ucode_info (int old_rev,int new_rev,unsigned int date) {pr_info_once ("updated early: 0x%x -> 0x%x, date = %04x-%02x-%02x\n" , - old_rev, - new_rev, - date &0xffff , - date >>24 , - (date >>16 ) &0xff ); + old_rev, new_rev, date &0xffff , date >>24 , (date >>16 ) &0xff ); }#ifdef CONFIG_X86_32
@@ -427,6 +400,28 @@ static int apply_microcode_early(structreturn 0 ; } +static bool load_builtin_intel_microcode (struct cpio_data *cp) +{ +unsigned int eax =1 , ebx, ecx =0 , edx; +struct firmware fw; +char name[30 ]; + +if (IS_ENABLED (CONFIG_X86_32 )) +return false; + +native_cpuid (&eax, &ebx, &ecx, &edx); + +sprintf (name,"intel-ucode/%02x-%02x-%02x" , +x86_family (eax),x86_model (eax),x86_stepping (eax)); + +if (firmware_request_builtin (&fw, name)) { + cp->size = fw.size; + cp->data = (void *)fw.data; +return true; + } +return false; +} +int __init save_microcode_in_initrd_intel (void ) {struct ucode_cpu_info uci;
@@ -518,25 +513,16 @@ void load_ucode_intel_ap(void)apply_microcode_early (&uci, true); } -/* Accessor for microcode pointer */ -static struct microcode_intel *ucode_get_patch (void ) -{ -return intel_ucode_patch; -} -void reload_ucode_intel (void ) { -struct microcode_intel *p;struct ucode_cpu_info uci;intel_cpu_collect_info (&uci); - p =ucode_get_patch (); -if (!p) + uci.mc = intel_ucode_patch; +if (!uci.mc)return ; - uci.mc = p; -apply_microcode_early (&uci, false); }
@@ -574,8 +560,7 @@ static enum ucode_state apply_microcode_if (WARN_ON (raw_smp_processor_id () != cpu))return UCODE_ERROR ; -/* Look for a newer patch in our cache: */ - mc =ucode_get_patch (); + mc = intel_ucode_patch;if (!mc) { mc = uci->mc;if (!mc)
@@ -766,18 +751,17 @@ static enum ucode_state request_microcod }static struct microcode_ops microcode_intel_ops = { - .request_microcode_fw = request_microcode_fw, - .collect_cpu_info = collect_cpu_info, - .apply_microcode = apply_microcode_intel, + .request_microcode_fw = request_microcode_fw, + .collect_cpu_info = collect_cpu_info, + .apply_microcode = apply_microcode_intel, }; -static int __init calc_llc_size_per_core (struct cpuinfo_x86 *c) +static __init void calc_llc_size_per_core (struct cpuinfo_x86 *c) {u64 llc_size = c->x86_cache_size *1024ULL ;do_div (llc_size, c->x86_max_cores); - -return (int )llc_size; + llc_size_per_core = (unsigned int )llc_size; }struct microcode_ops *__init init_intel_microcode (void )
@@ -790,7 +774,7 @@ struct microcode_ops * __init init_intelreturn NULL ; } - llc_size_per_core =calc_llc_size_per_core (c); +calc_llc_size_per_core (c);return µcode_intel_ops; }
PATCH v2 14/37 x86/microcode/intel: Simplify early loading
In reply to Thomas GleixnerFrom: Thomas Gleixner <[email protected]>
The early loading code is overly complicated:
- It scans the builtin/initrd for microcode not only on the BSP, but also on all APs during early boot and then later in the boot process it scans again to duplicate and save the microcode before initrd goes away.
That's a pointless exercise because this can be simply done before bringing up the APs when the memory allocator is up and running.
- Saving the microcode from within the scan loop is completely non-obvious and a left over of the microcode cache.
This can be done at the call site now which makes it obvious.
Rework the code so that only the BSP scans the builtin/initrd microcode once during early boot and save it away in an early initcall for later use.
--- arch/x86/kernel/cpu/microcode/core.c | 4 arch/x86/kernel/cpu/microcode/intel.c | 191 +++++++++++++------------------ arch/x86/kernel/cpu/microcode/internal.h | 2 3 files changed, 86 insertions(+), 111 deletions(-)
---
arch/x86/kernel/cpu/microcode/core.c +0 -4
@@ -207,10 +207,6 @@ static int __init save_microcode_in_initint ret = -EINVAL ;switch (c->x86_vendor) { -case X86_VENDOR_INTEL : -if (c->x86 >=6 ) - ret =save_microcode_in_initrd_intel (); -break ;case X86_VENDOR_AMD :if (c->x86 >=0x10 ) ret =save_microcode_in_initrd_amd (cpuid_eax (1 ));
arch/x86/kernel/cpu/microcode/intel.c +86 -105
306 lines not on this page · raw message
arch/x86/kernel/cpu/microcode/internal.h +0 -2
@@ -107,13 +107,11 @@ static inline void exit_amd_microcode(vo#ifdef CONFIG_CPU_SUP_INTEL void load_ucode_intel_bsp (void );void load_ucode_intel_ap (void ); -int save_microcode_in_initrd_intel (void );void reload_ucode_intel (void );struct microcode_ops *init_intel_microcode (void );#else /* CONFIG_CPU_SUP_INTEL */ static inline void load_ucode_intel_bsp (void ) { }static inline void load_ucode_intel_ap (void ) { } -static inline int save_microcode_in_initrd_intel (void ) {return -EINVAL ; }static inline void reload_ucode_intel (void ) { }static inline struct microcode_ops *init_intel_microcode (void ) {return NULL ; }#endif /* !CONFIG_CPU_SUP_INTEL */
PATCH v2 15/37 x86/microcode/intel: Save the microcode only after a successful late-load
In reply to Thomas GleixnerFrom: Thomas Gleixner <[email protected]>
There are situations where the late microcode is loaded into memory, but is not applied:
1) The rendevouz fails
2) The microcode is rejected by the CPUs
If any of this happens then the pointer which was updated at firmware load time is stale and subsequent CPU hotplug operations either fail to update or create inconsistent microcode state.
Save the loaded microcode in a separate pointer from with the late load is attempted and when successful, update the hotplug pointer accordingly via a new micrcode_ops callback.
Remove the pointless fallback in the loader to a microcode pointer which is never populated.
--- arch/x86/kernel/cpu/microcode/core.c | 4 ++++ arch/x86/kernel/cpu/microcode/intel.c | 30 +++++++++++++++--------------- arch/x86/kernel/cpu/microcode/internal.h | 1 + 3 files changed, 20 insertions(+), 15 deletions(-)
---
arch/x86/kernel/cpu/microcode/core.c +4 -0
@@ -443,6 +443,10 @@ static int microcode_reload_late(void)store_cpu_caps (&prev_info); ret =stop_machine_cpuslocked (__reload_late,NULL , cpu_online_mask); + +if (microcode_ops->finalize_late_load) + microcode_ops->finalize_late_load (ret); +if (!ret) {pr_info ("Reload succeeded, microcode revision: 0x%x -> 0x%x\n" , old, boot_cpu_data.microcode);
arch/x86/kernel/cpu/microcode/intel.c +15 -15
@@ -34,6 +34,7 @@ static const char ucode_path[] = "kernel/* Current microcode patch used in early patching on the APs. */ static struct microcode_intel *ucode_patch_va__read_mostly ; +static struct microcode_intel *ucode_patch_late__read_mostly ;/* last level cache size per core */ static unsigned int llc_size_per_core__ro_after_init ;
@@ -541,12 +542,9 @@ static enum ucode_state apply_microcode_if (WARN_ON (raw_smp_processor_id () != cpu))return UCODE_ERROR ; - mc = ucode_patch_va; -if (!mc) { - mc = uci->mc; -if (!mc) -return UCODE_NFOUND ; - } + mc = ucode_patch_late; +if (!mc) +return UCODE_NFOUND ;/* * Save us the MSR write below - which is a particular expensive
@@ -666,15 +664,7 @@ static enum ucode_state read_ucode_intelif (!new_mc)return UCODE_NFOUND ; -/* Save for CPU hotplug */ -save_microcode_patch ((struct microcode_intel *)new_mc); - uci->mc = ucode_patch_va; - -vfree (new_mc); - -pr_debug ("CPU%d found a matching microcode update with version 0x%x (current=0x%x)\n" , - cpu, cur_rev, uci->cpu_sig.rev); - + ucode_patch_late = (struct microcode_intel *)new_mc;return UCODE_NEW ; }
@@ -731,10 +721,20 @@ static enum ucode_state request_microcodreturn ret; } +static void finalize_late_load (int result) +{ +if (!result) +save_microcode_patch (ucode_patch_late); + +vfree (ucode_patch_late); + ucode_patch_late =NULL ; +} +static struct microcode_ops microcode_intel_ops = { .request_microcode_fw = request_microcode_fw, .collect_cpu_info = collect_cpu_info, .apply_microcode = apply_microcode_intel, + .finalize_late_load = finalize_late_load, };static __init void calc_llc_size_per_core (struct cpuinfo_x86 *c)
arch/x86/kernel/cpu/microcode/internal.h +1 -0
@@ -31,6 +31,7 @@ struct microcode_ops {*/ enum ucode_state (*apply_microcode)(int cpu);int (*collect_cpu_info)(int cpu,struct cpu_signature *csig); +void (*finalize_late_load)(int result); };extern struct ucode_cpu_info ucode_cpu_info[];
PATCH v2 16/37 x86/microcode/intel: Switch to kvmalloc()
In reply to Thomas GleixnerFrom: Thomas Gleixner <[email protected]>
Microcode blobs are getting larger and might soon reach the kmalloc() limit. Switch over kvmalloc().
32-bit has to stay with kmalloc() as it needs physically contiguous memory because the early loading runs before paging is enabled, so there is a sanity check added to ensure that.
--- arch/x86/kernel/cpu/microcode/intel.c | 55 +++++++++++++++++++--------------- 1 file changed, 32 insertions(+), 23 deletions(-)
---
arch/x86/kernel/cpu/microcode/intel.c +32 -23
@@ -14,7 +14,6 @@#include <linux/earlycpio.h> #include <linux/firmware.h> #include <linux/uaccess.h> -#include <linux/vmalloc.h> #include <linux/initrd.h> #include <linux/kernel.h> #include <linux/slab.h>
@@ -243,7 +242,7 @@ EXPORT_SYMBOL_GPL(intel_microcode_sanitystatic void update_ucode_pointer (struct microcode_intel *mc) { -kfree (ucode_patch_va); +kvfree (ucode_patch_va);/* * Save the virtual address for early loading on 64bit
@@ -257,13 +256,18 @@ static void update_ucode_pointer(structstatic void save_microcode_patch (struct microcode_intel *patch) { -struct microcode_intel *mc; +unsigned int size =get_totalsize (&patch->hdr); +struct microcode_intel *mc =NULL ; + +if (IS_ENABLED (CONFIG_X86_64 )) + mc =kvmemdup (patch, size,GFP_KERNEL ); +else + mc =kmemdup (patch, size,GFP_KERNEL ); - mc =kmemdup (patch,get_totalsize (&patch->hdr),GFP_KERNEL );if (mc)update_ucode_pointer (mc);else -pr_err ("Unable to allocate microcode memory\n" ); +pr_err ("Unable to allocate microcode memory size: %u\n" , size); }/* Scan CPIO for microcode matching the boot CPUs family, model, stepping */
@@ -610,36 +614,34 @@ static enum ucode_state read_ucode_intelif (!copy_from_iter_full (&mc_header,sizeof (mc_header), iter)) {pr_err ("error! Truncated or inaccessible header in microcode data file\n" ); -break ; + goto fail; } mc_size =get_totalsize (&mc_header);if (mc_size <sizeof (mc_header)) {pr_err ("error! Bad data in microcode data file (totalsize too small)\n" ); -break ; + goto fail; } - data_size = mc_size -sizeof (mc_header);if (data_size >iov_iter_count (iter)) {pr_err ("error! Bad data in microcode data file (truncated file?)\n" ); -break ; + goto fail; }/* For performance reasons, reuse mc area when possible */ if (!mc || mc_size > curr_mc_size) { -vfree (mc); - mc =vmalloc (mc_size); +kvfree (mc); + mc =kvmalloc (mc_size,GFP_KERNEL );if (!mc) -break ; + goto fail; curr_mc_size = mc_size; }memcpy (mc, &mc_header,sizeof (mc_header)); data = mc +sizeof (mc_header);if (!copy_from_iter_full (data, data_size, iter) || -intel_microcode_sanity_check (mc, true,MC_HEADER_TYPE_MICROCODE ) <0 ) { -break ; - } +intel_microcode_sanity_check (mc, true,MC_HEADER_TYPE_MICROCODE ) <0 ) + goto fail;if (cur_rev >= mc_header.rev)continue ;
@@ -647,25 +649,32 @@ static enum ucode_state read_ucode_intelif (!intel_find_matching_signature (mc, uci->cpu_sig.sig, uci->cpu_sig.pf))continue ; -vfree (new_mc); +kvfree (new_mc); cur_rev = mc_header.rev; new_mc = mc; new_mc_size = mc_size; mc =NULL ; } -vfree (mc); +if (iov_iter_count (iter)) + goto fail; -if (iov_iter_count (iter)) { -vfree (new_mc); -return UCODE_ERROR ; +if (IS_ENABLED (CONFIG_X86_32 ) && new_mc &&is_vmalloc_addr (new_mc)) { +pr_err ("Microcode too large for 32-bit mode\n" ); + goto fail; } +kvfree (mc);if (!new_mc)return UCODE_NFOUND ; ucode_patch_late = (struct microcode_intel *)new_mc;return UCODE_NEW ; + +fail: +kvfree (mc); +kvfree (new_mc); +return UCODE_ERROR ; }static bool is_blacklisted (unsigned int cpu)
@@ -724,9 +733,9 @@ static enum ucode_state request_microcodstatic void finalize_late_load (int result) {if (!result) -save_microcode_patch (ucode_patch_late); - -vfree (ucode_patch_late); +update_ucode_pointer (ucode_patch_late); +else +kvfree (ucode_patch_late); ucode_patch_late =NULL ; }
PATCH v2 17/37 x86/microcode/intel: Unify microcode apply() functions
In reply to Thomas GleixnerDeduplicate the early and late apply() functions.
--- arch/x86/kernel/cpu/microcode/intel.c | 106 +++++++++++----------------------- 1 file changed, 36 insertions(+), 70 deletions(-)
arch/x86/kernel/cpu/microcode/intel.c +36 -70
@@ -353,12 +353,11 @@ static inline void print_ucode(int old_r }#endif -static enum ucode_state apply_microcode_early (struct ucode_cpu_info *uci,bool early) +static enum ucode_state apply_microcode (struct ucode_cpu_info *uci,struct microcode_intel *mc, +u32 *cur_rev) { -struct microcode_intel *mc; -u32 rev, old_rev; +u32 rev; - mc = uci->mc;if (!mc)return UCODE_NFOUND ;
@@ -367,14 +366,12 @@ static enum ucode_state apply_microcode_* operation - when the other hyperthread has updated the microcode * already. */ - rev =intel_get_microcode_revision (); -if (rev >= mc->hdr.rev) { - uci->cpu_sig.rev = rev; + *cur_rev =intel_get_microcode_revision (); +if (*cur_rev >= mc->hdr.rev) { + uci->cpu_sig.rev = *cur_rev;return UCODE_OK ; } - old_rev = rev; -/* * Writeback and invalidate caches before updating microcode to avoid * internal issues depending on what the microcode is updating.
@@ -389,13 +386,23 @@ static enum ucode_state apply_microcode_return UCODE_ERROR ; uci->cpu_sig.rev = rev; +return UCODE_UPDATED ; +} -if (early) -print_ucode (old_rev, uci->cpu_sig.rev, mc->hdr.date); -else -print_ucode_info (old_rev, uci->cpu_sig.rev, mc->hdr.date); +static enum ucode_state apply_microcode_early (struct ucode_cpu_info *uci,bool early) +{ +struct microcode_intel *mc = uci->mc; +enum ucode_state ret; +u32 cur_rev; -return UCODE_UPDATED ; + ret =apply_microcode (uci, mc, &cur_rev); +if (ret ==UCODE_UPDATED ) { +if (early) +print_ucode (cur_rev, uci->cpu_sig.rev, mc->hdr.date); +else +print_ucode_info (cur_rev, uci->cpu_sig.rev, mc->hdr.date); + } +return ret; }static __init bool load_builtin_intel_microcode (struct cpio_data *cp)
@@ -532,70 +539,29 @@ static int collect_cpu_info(int cpu_num,return 0 ; } -static enum ucode_state apply_microcode_intel (int cpu) +static enum ucode_state apply_microcode_late (int cpu) {struct ucode_cpu_info *uci = ucode_cpu_info + cpu; -struct cpuinfo_x86 *c = &cpu_data (cpu); -bool bsp = c->cpu_index == boot_cpu_data.cpu_index; -struct microcode_intel *mc; +struct microcode_intel *mc = ucode_patch_late;enum ucode_state ret; -static int prev_rev; -u32 rev; - -/* We should bind the task to the CPU */ -if (WARN_ON (raw_smp_processor_id () != cpu)) -return UCODE_ERROR ; - - mc = ucode_patch_late; -if (!mc) -return UCODE_NFOUND ; +u32 cur_rev; -/* -* Save us the MSR write below - which is a particular expensive -* operation - when the other hyperthread has updated the microcode -* already. -*/ - rev =intel_get_microcode_revision (); -if (rev >= mc->hdr.rev) { - ret =UCODE_OK ; - goto out; - } - -/* -* Writeback and invalidate caches before updating microcode to avoid -* internal issues depending on what the microcode is updating. -*/ -native_wbinvd (); - -/* write microcode via MSR 0x79 */ -wrmsrl (MSR_IA32_UCODE_WRITE , (unsigned long )mc->bits); - - rev =intel_get_microcode_revision (); - -if (rev != mc->hdr.rev) { -pr_err ("CPU%d update to revision 0x%x failed\n" , - cpu, mc->hdr.rev); +if (WARN_ON_ONCE (smp_processor_id () != cpu))return UCODE_ERROR ; - } -if (bsp && rev != prev_rev) { -pr_info ("updated to revision 0x%x, date = %04x-%02x-%02x\n" , - rev, - mc->hdr.date &0xffff , - mc->hdr.date >>24 , + ret =apply_microcode (uci, mc, &cur_rev); +if (ret !=UCODE_UPDATED && ret !=UCODE_OK ) +return ret; + +if (!cpu && uci->cpu_sig.rev != cur_rev) { +pr_info ("Updated to revision 0x%x, date = %04x-%02x-%02x\n" , + uci->cpu_sig.rev, mc->hdr.date &0xffff , mc->hdr.date >>24 , (mc->hdr.date >>16 ) &0xff ); - prev_rev = rev; } - ret =UCODE_UPDATED ; - -out: - uci->cpu_sig.rev = rev; - c->microcode = rev; - -/* Update boot_cpu_data's revision too, if we're on the BSP: */ -if (bsp) - boot_cpu_data.microcode = rev; +cpu_data (cpu).microcode = uci->cpu_sig.rev; +if (!cpu) + boot_cpu_data.microcode = uci->cpu_sig.rev;return ret; }
@@ -742,7 +708,7 @@ static void finalize_late_load(int resulstatic struct microcode_ops microcode_intel_ops = { .request_microcode_fw = request_microcode_fw, .collect_cpu_info = collect_cpu_info, - .apply_microcode = apply_microcode_intel, + .apply_microcode = apply_microcode_late, .finalize_late_load = finalize_late_load, };
PATCH v2 18/37 x86/microcode/intel: Rework intel_cpu_collect_info()
In reply to Thomas GleixnerNothing needs struct ucode_cpu_info. Make it take struct cpu_signature, let it return a boolean and simplify the implementation. Rename it now that the silly name clash with collect_cpu_info() is gone.
---
V2: New patch
--- arch/x86/include/asm/cpu.h | 4 +-- arch/x86/kernel/cpu/microcode/intel.c | 39 ++++++++++------------------------ drivers/platform/x86/intel/ifs/load.c | 8 ++---- 3 files changed, 17 insertions(+), 34 deletions(-)
arch/x86/include/asm/cpu.h +2 -2
@@ -73,9 +73,9 @@ static inline void init_ia32_feat_ctl(st extern __noendbr void cet_disable(void); -struct ucode_cpu_info ; +struct cpu_signature ; -int intel_cpu_collect_info (struct ucode_cpu_info *uci); +void intel_collect_cpu_info (struct cpu_signature *sig);static inline bool intel_cpu_signatures_match (unsigned int s1,unsigned int p1,unsigned int s2,unsigned int p2)
arch/x86/kernel/cpu/microcode/intel.c +12 -27
@@ -66,36 +66,21 @@ static inline unsigned int exttable_sizereturn et->count *EXT_SIGNATURE_SIZE +EXT_HEADER_SIZE ; } -int intel_cpu_collect_info (struct ucode_cpu_info *uci) +void intel_collect_cpu_info (struct cpu_signature *sig) { -unsigned int val[2 ]; -unsigned int family, model; -struct cpu_signature csig = {0 }; -unsigned int eax, ebx, ecx, edx; + sig->sig =cpuid_eax (1 ); + sig->pf =0 ; + sig->rev =intel_get_microcode_revision (); -memset (uci,0 ,sizeof (*uci)); +if (x86_model (sig->sig) >=5 ||x86_family (sig->sig) >6 ) { +unsigned int val[2 ]; - eax =0x00000001 ; - ecx =0 ; -native_cpuid (&eax, &ebx, &ecx, &edx); - csig.sig = eax; - - family =x86_family (eax); - model =x86_model (eax); - -if (model >=5 || family >6 ) {/* get processor flags from MSR 0x17 */ native_rdmsr (MSR_IA32_PLATFORM_ID , val[0 ], val[1 ]); - csig.pf =1 << ((val[1 ] >>18 ) &7 ); + sig->pf =1 << ((val[1 ] >>18 ) &7 ); } - - csig.rev =intel_get_microcode_revision (); - - uci->cpu_sig = csig; - -return 0 ; } -EXPORT_SYMBOL_GPL (intel_cpu_collect_info); +EXPORT_SYMBOL_GPL (intel_collect_cpu_info);/* * Returns 1 if update has been found, 0 otherwise.
@@ -318,11 +303,11 @@ static int early_old_rev;*/ void show_ucode_info_early (void ) { -struct ucode_cpu_info uci; +struct cpu_signature sig;if (delay_ucode_info) { -intel_cpu_collect_info (&uci); -print_ucode_info (early_old_rev, uci.cpu_sig.rev, current_mc_date); +intel_collect_cpu_info (&sig); +print_ucode_info (early_old_rev, sig.rev, current_mc_date); delay_ucode_info =0 ; } }
@@ -444,7 +429,7 @@ static __init struct microcode_intel *geif (!(cp.data && cp.size))return NULL ; -intel_cpu_collect_info (uci); +intel_collect_cpu_info (&uci->cpu_sig);return scan_microcode (cp.data, cp.size, uci); }
drivers/platform/x86/intel/ifs/load.c +3 -5
@@ -227,7 +227,7 @@ static int scan_chunks_sanity_check(strustatic int image_sanity_check (struct device *dev,const struct microcode_header_intel *data) { -struct ucode_cpu_info uci; +struct cpu_signature sig;/* Provide a specific error message when loading an older/unsupported image */ if (data->hdrver !=MC_HEADER_TYPE_IFS ) {
@@ -240,11 +240,9 @@ static int image_sanity_check(struct devreturn -EINVAL ; } -intel_cpu_collect_info (&uci); +intel_collect_cpu_info (&sig); -if (!intel_find_matching_signature ((void *)data, - uci.cpu_sig.sig, - uci.cpu_sig.pf)) { +if (!intel_find_matching_signature ((void *)data, sig.sig, sig.pf)) {dev_err (dev,"cpu signature, processor flags not matching\n" );return -EINVAL ; }
PATCH v2 19/37 x86/microcode/intel: Reuse intel_cpu_collect_info()
In reply to Thomas GleixnerNo point for an almost duplicate function.
---
V2: New patch
--- arch/x86/kernel/cpu/microcode/intel.c | 16 +--------------- 1 file changed, 1 insertion(+), 15 deletions(-)
arch/x86/kernel/cpu/microcode/intel.c +1 -15
@@ -506,21 +506,7 @@ void reload_ucode_intel(void)static int collect_cpu_info (int cpu_num,struct cpu_signature *csig) { -struct cpuinfo_x86 *c = &cpu_data (cpu_num); -unsigned int val[2 ]; - -memset (csig,0 ,sizeof (*csig)); - - csig->sig =cpuid_eax (0x00000001 ); - -if ((c->x86_model >=5 ) || (c->x86 >6 )) { -/* get processor flags from MSR 0x17 */ -rdmsr (MSR_IA32_PLATFORM_ID , val[0 ], val[1 ]); - csig->pf =1 << ((val[1 ] >>18 ) &7 ); - } - - csig->rev = c->microcode; - +intel_collect_cpu_info (csig);return 0 ; }
PATCH v2 20/37 x86/microcode/intel: Rework intel_find_matching_signature()
In reply to Thomas GleixnerTake a cpu_signature argument and work from there. Move the match() helper next to the callsite as there is no point for having it in a header.
---
V2: New patch
--- arch/x86/include/asm/cpu.h | 16 +--------------- arch/x86/kernel/cpu/microcode/intel.c | 31 +++++++++++++++++++------------ drivers/platform/x86/intel/ifs/load.c | 2 +- 3 files changed, 21 insertions(+), 28 deletions(-)
arch/x86/include/asm/cpu.h +1 -15
@@ -77,22 +77,8 @@ struct cpu_signature;void intel_collect_cpu_info (struct cpu_signature *sig); -static inline bool intel_cpu_signatures_match (unsigned int s1,unsigned int p1, -unsigned int s2,unsigned int p2) -{ -if (s1 != s2) -return false; - -/* Processor flags are either both 0 ... */ -if (!p1 && !p2) -return true; - -/* ... or they intersect. */ -return p1 & p2; -} -extern u64 x86_read_arch_cap_msr (void ); -int intel_find_matching_signature (void *mc,unsigned int csig,int cpf); +bool intel_find_matching_signature (void *mc,struct cpu_signature *sig);int intel_microcode_sanity_check (void *mc,bool print_err,int hdr_type);extern struct cpumask cpus_stop_mask;
arch/x86/kernel/cpu/microcode/intel.c +19 -12
@@ -82,29 +82,36 @@ void intel_collect_cpu_info(struct cpu_s }EXPORT_SYMBOL_GPL (intel_collect_cpu_info); -/* -* Returns 1 if update has been found, 0 otherwise. -*/ -int intel_find_matching_signature (void *mc,unsigned int csig,int cpf) +static inline bool cpu_signatures_match (struct cpu_signature *s1,unsigned int sig2, +unsigned int pf2) +{ +if (s1->sig != sig2) +return false; + +/* Processor flags are either both 0 or they intersect. */ +return ((!s1->pf && !pf2) || (s1->pf & pf2)); +} + +bool intel_find_matching_signature (void *mc,struct cpu_signature *sig) {struct microcode_header_intel *mc_hdr = mc; -struct extended_sigtable *ext_hdr;struct extended_signature *ext_sig; +struct extended_sigtable *ext_hdr;int i; -if (intel_cpu_signatures_match (csig, cpf, mc_hdr->sig, mc_hdr->pf)) -return 1 ; +if (cpu_signatures_match (sig, mc_hdr->sig, mc_hdr->pf)) +return true;/* Look for ext. headers: */ if (get_totalsize (mc_hdr) <=intel_microcode_get_datasize (mc_hdr) +MC_HEADER_SIZE ) -return 0 ; +return false; ext_hdr = mc +intel_microcode_get_datasize (mc_hdr) +MC_HEADER_SIZE ; ext_sig = (void *)ext_hdr +EXT_HEADER_SIZE ;for (i =0 ; i < ext_hdr->count; i++) { -if (intel_cpu_signatures_match (csig, cpf, ext_sig->sig, ext_sig->pf)) -return 1 ; +if (cpu_signatures_match (sig, ext_sig->sig, ext_sig->pf)) +return true; ext_sig++; }return 0 ;
@@ -272,7 +279,7 @@ static __init struct microcode_intel *sc intel_microcode_sanity_check(data, false, MC_HEADER_TYPE_MICROCODE) < 0)break ; -if (!intel_find_matching_signature (data, uci->cpu_sig.sig, uci->cpu_sig.pf)) +if (!intel_find_matching_signature (data, &uci->cpu_sig))continue ;/* Check whether there is newer microcode */
@@ -583,7 +590,7 @@ static enum ucode_state read_ucode_intelif (cur_rev >= mc_header.rev)continue ; -if (!intel_find_matching_signature (mc, uci->cpu_sig.sig, uci->cpu_sig.pf)) +if (!intel_find_matching_signature (mc, &uci->cpu_sig))continue ;kvfree (new_mc);
drivers/platform/x86/intel/ifs/load.c +1 -1
@@ -242,7 +242,7 @@ static int image_sanity_check(struct devintel_collect_cpu_info (&sig); -if (!intel_find_matching_signature ((void *)data, sig.sig, sig.pf)) { +if (!intel_find_matching_signature ((void *)data, &sig)) {dev_err (dev,"cpu signature, processor flags not matching\n" );return -EINVAL ; }
PATCH v2 21/37 x86/microcode/amd: Read revision from hardware in collect_cpu_info_amd()
In reply to Thomas GleixnerPrepare to decrapify the core initialization logic which invokes microcode_ops::apply_microcode() several times just to set cpu_data::microcode.
---
V2: New patch
--- arch/x86/kernel/cpu/microcode/amd.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
arch/x86/kernel/cpu/microcode/amd.c +2 -2
@@ -673,12 +673,12 @@ void reload_ucode_amd(unsigned int cpu)static int collect_cpu_info_amd (int cpu,struct cpu_signature *csig) { -struct cpuinfo_x86 *c = &cpu_data (cpu);struct ucode_cpu_info *uci = ucode_cpu_info + cpu; +u32 dummy__always_unused ;struct ucode_patch *p; csig->sig =cpuid_eax (0x00000001 ); - csig->rev = c->microcode; +rdmsr (MSR_AMD64_PATCH_LEVEL , csig->rev, dummy);/* * a patch could have been loaded early, set uci->mc so that
PATCH v2 22/37 x86/microcode: Remove pointless apply() invocation
In reply to Thomas GleixnerMicrocode is applied on the APs during early bringup. There is no point in trying to apply the microcode again during the hotplug operations and neither at the point where the microcode device is initialized.
Collect CPU info and microcode revision in setup_online_cpu() for now. This will move to the CPU hotplug callback in the next step.
---
V2: New patch
--- arch/x86/kernel/cpu/microcode/core.c | 34 ++++++---------------------------- include/linux/cpuhotplug.h | 1 - 2 files changed, 6 insertions(+), 29 deletions(-)
arch/x86/kernel/cpu/microcode/core.c +6 -28
@@ -533,17 +533,6 @@ static void microcode_fini_cpu(int cpu) microcode_ops->microcode_fini_cpu (cpu); } -static enum ucode_state microcode_init_cpu (int cpu) -{ -struct ucode_cpu_info *uci = ucode_cpu_info + cpu; - -memset (uci,0 ,sizeof (*uci)); - - microcode_ops->collect_cpu_info (cpu, &uci->cpu_sig); - -return microcode_ops->apply_microcode (cpu); -} -/** * microcode_bsp_resume - Update boot CPU microcode during resume. */
@@ -562,15 +551,6 @@ static struct syscore_ops mc_syscore_ops .resume = microcode_bsp_resume, }; -static int mc_cpu_starting (unsigned int cpu) -{ -enum ucode_state err = microcode_ops->apply_microcode (cpu); - -pr_debug ("%s: CPU%d, err: %d\n" , __func__, cpu, err); - -return err ==UCODE_ERROR ; -} -static int mc_cpu_online (unsigned int cpu) {struct device *dev =get_cpu_device (cpu);
@@ -598,14 +578,14 @@ static int mc_cpu_down_prep(unsigned intstatic void setup_online_cpu (struct work_struct *work) {int cpu =smp_processor_id (); -enum ucode_state err; +struct ucode_cpu_info *uci = ucode_cpu_info + cpu; - err =microcode_init_cpu (cpu); -if (err ==UCODE_ERROR ) { -pr_err ("Error applying microcode on CPU%d\n" , cpu); -return ; - } +memset (uci,0 ,sizeof (*uci)); + microcode_ops->collect_cpu_info (cpu, &uci->cpu_sig); +this_cpu_write (cpu_info.microcode, uci->cpu_sig.rev); +if (!cpu) + boot_cpu_data.microcode = uci->cpu_sig.rev;mc_cpu_online (cpu); }
@@ -658,8 +638,6 @@ static int __init microcode_init(void)schedule_on_each_cpu (setup_online_cpu);register_syscore_ops (&mc_syscore_ops); -cpuhp_setup_state_nocalls (CPUHP_AP_MICROCODE_LOADER ,"x86/microcode:starting" , - mc_cpu_starting,NULL );cpuhp_setup_state_nocalls (CPUHP_AP_ONLINE_DYN ,"x86/microcode:online" , mc_cpu_online, mc_cpu_down_prep);
include/linux/cpuhotplug.h +0 -1
@@ -156,7 +156,6 @@ enum cpuhp_state {CPUHP_AP_IRQ_LOONGARCH_STARTING ,CPUHP_AP_IRQ_SIFIVE_PLIC_STARTING ,CPUHP_AP_ARM_MVEBU_COHERENCY , -CPUHP_AP_MICROCODE_LOADER ,CPUHP_AP_PERF_X86_AMD_UNCORE_STARTING ,CPUHP_AP_PERF_X86_STARTING , CPUHP_AP_PERF_X86_AMD_IBS_STARTING,
PATCH v2 23/37 x86/microcode: Get rid of the schedule work indirection
In reply to Thomas GleixnerScheduling work on all CPUs to collect the microcode information is just another extra step for no value. Let the CPU hotplug callback registration do it.
---
V2: New patch
--- arch/x86/kernel/cpu/microcode/core.c | 28 +++++++++------------------- 1 file changed, 9 insertions(+), 19 deletions(-)
arch/x86/kernel/cpu/microcode/core.c +9 -19
@@ -553,8 +553,15 @@ static struct syscore_ops mc_syscore_opsstatic int mc_cpu_online (unsigned int cpu) { +struct ucode_cpu_info *uci = ucode_cpu_info + cpu;struct device *dev =get_cpu_device (cpu); +memset (uci,0 ,sizeof (*uci)); + microcode_ops->collect_cpu_info (cpu, &uci->cpu_sig); +this_cpu_write (cpu_info.microcode, uci->cpu_sig.rev); +if (!cpu) + boot_cpu_data.microcode = uci->cpu_sig.rev; +if (sysfs_create_group (&dev->kobj, &mc_attr_group))pr_err ("Failed to create group for CPU%d\n" , cpu);return 0 ;
@@ -575,20 +582,6 @@ static int mc_cpu_down_prep(unsigned intreturn 0 ; } -static void setup_online_cpu (struct work_struct *work) -{ -int cpu =smp_processor_id (); -struct ucode_cpu_info *uci = ucode_cpu_info + cpu; - -memset (uci,0 ,sizeof (*uci)); - - microcode_ops->collect_cpu_info (cpu, &uci->cpu_sig); -this_cpu_write (cpu_info.microcode, uci->cpu_sig.rev); -if (!cpu) - boot_cpu_data.microcode = uci->cpu_sig.rev; -mc_cpu_online (cpu); -} -static struct attribute *cpu_root_microcode_attrs[] = { #ifdef CONFIG_MICROCODE_LATE_LOADING &dev_attr_reload.attr,
@@ -634,12 +627,9 @@ static int __init microcode_init(void) } } -/* Do per-CPU setup */ -schedule_on_each_cpu (setup_online_cpu); -register_syscore_ops (&mc_syscore_ops); -cpuhp_setup_state_nocalls (CPUHP_AP_ONLINE_DYN ,"x86/microcode:online" , - mc_cpu_online, mc_cpu_down_prep); +cpuhp_setup_state (CPUHP_AP_ONLINE_DYN ,"x86/microcode:online" , + mc_cpu_online, mc_cpu_down_prep);pr_info ("Microcode Update Driver: v%s." ,DRIVER_VERSION );
PATCH v2 24/37 x86/microcode: Clean up mc_cpu_down_prep()
In reply to Thomas GleixnerThis function has nothing to do with suspend. It's a hotplug callback. Remove the bogus comment.
Drop the pointless debug printk. The hotplug core provides tracepoints which track the invocation of those callbacks.
---
V2: New patch
--- arch/x86/kernel/cpu/microcode/core.c | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-)
arch/x86/kernel/cpu/microcode/core.c +1 -7
@@ -569,16 +569,10 @@ static int mc_cpu_online(unsigned int cpstatic int mc_cpu_down_prep (unsigned int cpu) { -struct device *dev; - - dev =get_cpu_device (cpu); +struct device *dev =get_cpu_device (cpu);microcode_fini_cpu (cpu); - -/* Suspend is in progress, only remove the interface */ sysfs_remove_group (&dev->kobj, &mc_attr_group); -pr_debug ("%s: CPU%d\n" , __func__, cpu); -return 0 ; }
PATCH v2 25/37 x86/microcode: Handle "nosmt" correctly
In reply to Thomas GleixnerFrom: Thomas Gleixner <[email protected]>
On CPUs where microcode loading is not NMI safe the SMT sibling which is parked in one of the play_dead() variants, these parked CPUs still react on NMIs. So if a NMI hits while the primary thread updates the microcode the resulting behaviour is undefined. The default play_dead() implementation on modern CPUs is using MWAIT, which is not guaranteed to be safe against an microcode update which affects MWAIT.
Take the cpus_booted_once_mask into account to detect this case and refuse to load late if the vendor specific driver does not advertise that late loading is NMI safe.
AMD stated that this is safe, so mark the AMD driver accordingly.
This requirement will be partially lifted in later changes.
--- arch/x86/Kconfig | 2 - arch/x86/kernel/cpu/microcode/amd.c | 9 +++-- arch/x86/kernel/cpu/microcode/core.c | 51 +++++++++++++++++++------------ arch/x86/kernel/cpu/microcode/internal.h | 13 +++---- 4 files changed, 44 insertions(+), 31 deletions(-)
---
arch/x86/Kconfig +1 -1
@@ -1314,7 +1314,7 @@ config MICROCODE config MICROCODE_LATE_LOADING bool "Late microcode loading (DANGEROUS)" default n - depends on MICROCODE + depends on MICROCODE && SMP help Loading microcode late, when the system is up and executing instructions is a tricky business and should be avoided if possible. Just the sequence
arch/x86/kernel/cpu/microcode/amd.c +5 -4
@@ -948,10 +948,11 @@ static void microcode_fini_cpu_amd(int c }static struct microcode_ops microcode_amd_ops = { - .request_microcode_fw = request_microcode_amd, - .collect_cpu_info = collect_cpu_info_amd, - .apply_microcode = apply_microcode_amd, - .microcode_fini_cpu = microcode_fini_cpu_amd, + .request_microcode_fw = request_microcode_amd, + .collect_cpu_info = collect_cpu_info_amd, + .apply_microcode = apply_microcode_amd, + .microcode_fini_cpu = microcode_fini_cpu_amd, + .nmi_safe = true, };struct microcode_ops *__init init_amd_microcode (void )
arch/x86/kernel/cpu/microcode/core.c +32 -19
70 lines not on this page · raw message
arch/x86/kernel/cpu/microcode/internal.h +6 -7
@@ -20,18 +20,17 @@ enum ucode_state {struct microcode_ops {enum ucode_state (*request_microcode_fw)(int cpu,struct device *dev); -void (*microcode_fini_cpu)(int cpu);/* -* The generic 'microcode_core' part guarantees that -* the callbacks below run on a target cpu when they -* are being called. +* The generic 'microcode_core' part guarantees that the callbacks +* below run on a target cpu when they are being called. * See also the "Synchronization" section in microcode_core.c. */ -enum ucode_state (*apply_microcode)(int cpu); -int (*collect_cpu_info)(int cpu,struct cpu_signature *csig); -void (*finalize_late_load)(int result); +enum ucode_state (*apply_microcode)(int cpu); +int (*collect_cpu_info)(int cpu,struct cpu_signature *csig); +void (*finalize_late_load)(int result); +unsigned int nmi_safe :1 ; };extern struct ucode_cpu_info ucode_cpu_info[];
PATCH v2 26/37 x86/microcode: Clarify the late load logic
In reply to Thomas GleixnerFrom: Thomas Gleixner <[email protected]>
reload_store() is way too complicated. Split the inner workings out and make the following enhancements:
- Taint the kernel only when the microcode was actually updated. If. e.g. the rendevouz fails, then nothing happened and there is no reason for tainting.
- Return useful error codes
--- arch/x86/kernel/cpu/microcode/core.c | 39 +++++++++++++++-------------------- 1 file changed, 17 insertions(+), 22 deletions(-)
---
arch/x86/kernel/cpu/microcode/core.c +17 -22
@@ -434,11 +434,11 @@ static int microcode_reload_late(void)pr_info ("Reload succeeded, microcode revision: 0x%x -> 0x%x\n" , old, boot_cpu_data.microcode);microcode_check (&prev_info); +add_taint (TAINT_CPU_OUT_OF_SPEC ,LOCKDEP_STILL_OK ); }else {pr_info ("Reload failed, current microcode revision: 0x%x\n" , boot_cpu_data.microcode); } -return ret; }
@@ -471,40 +471,35 @@ static bool ensure_cpus_are_online(void)return true; } +static int ucode_load_late_locked (void ) +{ +int ret; + +if (!ensure_cpus_are_online ()) +return -EBUSY ; + + ret = microcode_ops->request_microcode_fw (0 , µcode_pdev->dev); +if (ret !=UCODE_NEW ) +return ret ==UCODE_NFOUND ? -ENOENT : -EBADFD ; +return microcode_reload_late (); +} +static ssize_t reload_store (struct device *dev,struct device_attribute *attr,const char *buf,size_t size) { -enum ucode_state tmp_ret =UCODE_OK ; -int bsp = boot_cpu_data.cpu_index;unsigned long val; -ssize_t ret =0 ; +ssize_t ret; ret =kstrtoul (buf,0 , &val);if (ret || val !=1 )return -EINVAL ;cpus_read_lock (); - -if (!ensure_cpus_are_online ()) { - ret = -EBUSY ; - goto put; - } - - tmp_ret = microcode_ops->request_microcode_fw (bsp, µcode_pdev->dev); -if (tmp_ret !=UCODE_NEW ) - goto put; - - ret =microcode_reload_late (); -put: + ret =ucode_load_late_locked ();cpus_read_unlock (); -if (ret ==0 ) - ret = size; - -add_taint (TAINT_CPU_OUT_OF_SPEC ,LOCKDEP_STILL_OK ); - -return ret; +return ret ? : size; }static DEVICE_ATTR_WO (reload);
Re: [patch V2 26/37] x86/microcode: Clarify the late load logic
Quoted 5 lines · Thomas Gleixner
From: Thomas Gleixner <[email protected]>
reload_store()is way too complicated. Split the inner workings out and make the following enhancements:
Quoted 7 lines
- Taint the kernel only when the microcode was actually updated. If. e.g. the rendevouz fails, then nothing happened and there is no reason for tainting.
- Return useful error codes
Signed-off-by Thomas Gleixner <[email protected]>
PATCH v2 27/37 x86/microcode: Sanitize __wait_for_cpus()
In reply to Thomas GleixnerFrom: Thomas Gleixner <[email protected]>
The code is too complicated for no reason:
- The return value is pointless as this is a strict boolean.
- It's way simpler to count down from num_online_cpus() and check for zero.
- The timeout argument is pointless as this is always one second.
- Touching the NMI watchdog every 100ns does not make any sense, neither does checking every 100ns. This is really not a hotpath operation.
Preload the atomic counter with the number of online CPUs and simplify the whole timeout logic. Delay for one microsecond and touch the NMI watchdog once per millisecond.
--- arch/x86/kernel/cpu/microcode/core.c | 41 ++++++++++++++--------------------- 1 file changed, 17 insertions(+), 24 deletions(-)
---
arch/x86/kernel/cpu/microcode/core.c +17 -24
@@ -324,31 +324,24 @@ static struct platform_device *microcode* requirement can be relaxed in the future. Right now, this is conservative * and good. */ -#define SPINUNIT 100/* 100 nsec */ +static atomic_t late_cpus_in, late_cpus_out; - -static atomic_t late_cpus_in; -static atomic_t late_cpus_out; - -static int __wait_for_cpus (atomic_t *t,long long timeout) +static bool wait_for_cpus (atomic_t *cnt) { -int all_cpus =num_online_cpus (); - -atomic_inc (t); - -while (atomic_read (t) < all_cpus) { -if (timeout <SPINUNIT ) { -pr_err ("Timeout while waiting for CPUs rendezvous, remaining: %d\n" , - all_cpus -atomic_read (t)); -return 1 ; - } +unsigned int timeout; -ndelay (SPINUNIT ); - timeout -=SPINUNIT ; +WARN_ON_ONCE (atomic_dec_return (cnt) <0 ); -touch_nmi_watchdog (); +for (timeout =0 ; timeout <USEC_PER_SEC ; timeout++) { +if (!atomic_read (cnt)) +return true; +udelay (1 ); +if (!(timeout %1000 )) +touch_nmi_watchdog (); } -return 0 ; +/* Prevent the late comers to make progress and let them time out */ +atomic_inc (cnt); +return false; }/*
@@ -366,7 +359,7 @@ static int __reload_late(void *info)* Wait for all CPUs to arrive. A load will not be attempted unless all * CPUs show up. * */ -if (__wait_for_cpus (&late_cpus_in,NSEC_PER_SEC )) +if (!wait_for_cpus (&late_cpus_in))return -1 ;/*
@@ -389,7 +382,7 @@ static int __reload_late(void *info) } wait_for_siblings: -if (__wait_for_cpus (&late_cpus_out,NSEC_PER_SEC )) +if (!wait_for_cpus (&late_cpus_out))panic ("Timeout during microcode update!\n" );/*
@@ -416,8 +409,8 @@ static int microcode_reload_late(void)pr_err ("Attempting late microcode loading - it is dangerous and taints the kernel.\n" );pr_err ("You should switch to early loading, if possible.\n" ); -atomic_set (&late_cpus_in,0 ); -atomic_set (&late_cpus_out,0 ); +atomic_set (&late_cpus_in,num_online_cpus ()); +atomic_set (&late_cpus_out,num_online_cpus ());/* * Take a snapshot before the microcode update in order to compare and
PATCH v2 28/37 x86/microcode: Add per CPU result state
In reply to Thomas GleixnerFrom: Thomas Gleixner <[email protected]>
The microcode rendevouz is purely acting on global state, which does not allow to analyze fails in a coherent way.
Introduce per CPU state where the results are written into, which allows to analyze the return codes of the individual CPUs.
Initialize the state when walking the cpu_present_mask in the online check to avoid another for_each_cpu() loop.
Enhance the result print out with that.
The structure is intentionally named ucode_ctrl as it will gain control fields in subsequent changes.
--- arch/x86/kernel/cpu/microcode/core.c | 108 ++++++++++++++++++------------- arch/x86/kernel/cpu/microcode/internal.h | 1 2 files changed, 65 insertions(+), 44 deletions(-)
---
arch/x86/kernel/cpu/microcode/core.c +64 -44
@@ -324,6 +324,11 @@ static struct platform_device *microcode* requirement can be relaxed in the future. Right now, this is conservative * and good. */ +struct ucode_ctrl { +enum ucode_state result; +}; + +static DEFINE_PER_CPU(struct ucode_ctrl, ucode_ctrl);static atomic_t late_cpus_in, late_cpus_out;static bool wait_for_cpus (atomic_t *cnt)
@@ -344,23 +349,19 @@ static bool wait_for_cpus(atomic_t *cnt)return false; } -/* -* Returns: -* < 0 - on error -* 0 - success (no update done or microcode was updated) -*/ -static int __reload_late (void *info) +static int ucode_load_cpus_stopped (void *unused) {int cpu =smp_processor_id (); -enum ucode_state err; -int ret =0 ; +enum ucode_state ret;/* * Wait for all CPUs to arrive. A load will not be attempted unless all * CPUs show up. * */ -if (!wait_for_cpus (&late_cpus_in)) -return -1 ; +if (!wait_for_cpus (&late_cpus_in)) { +this_cpu_write (ucode_ctrl.result,UCODE_TIMEOUT ); +return 0 ; + }/* * On an SMT system, it suffices to load the microcode on one sibling of
@@ -369,17 +370,11 @@ static int __reload_late(void *info)* loading attempts happen on multiple threads of an SMT core. See * below. */ -if (cpumask_first (topology_sibling_cpumask (cpu)) == cpu) - err = microcode_ops->apply_microcode (cpu); -else +if (cpumask_first (topology_sibling_cpumask (cpu)) != cpu) goto wait_for_siblings; -if (err >=UCODE_NFOUND ) { -if (err ==UCODE_ERROR ) { -pr_warn ("Error reloading microcode on CPU %d\n" , cpu); - ret =-1 ; - } - } + ret = microcode_ops->apply_microcode (cpu); +this_cpu_write (ucode_ctrl.result, ret); wait_for_siblings:if (!wait_for_cpus (&late_cpus_out))
@@ -391,19 +386,18 @@ static int __reload_late(void *info)* per-cpu cpuinfo can be updated with right microcode * revision. */ -if (cpumask_first (topology_sibling_cpumask (cpu)) != cpu) - err = microcode_ops->apply_microcode (cpu); +if (cpumask_first (topology_sibling_cpumask (cpu)) == cpu) +return 0 ; -return ret; + ret = microcode_ops->apply_microcode (cpu); +this_cpu_write (ucode_ctrl.result, ret); +return 0 ; } -/* -* Reload microcode late on all CPUs. Wait for a sec until they -* all gather together. -*/ -static int microcode_reload_late (void ) +static int ucode_load_late_stop_cpus (void ) { -int old = boot_cpu_data.microcode, ret; +unsigned int cpu, updated =0 , failed =0 , timedout =0 , siblings =0 ; +int old_rev = boot_cpu_data.microcode;struct cpuinfo_x86 prev_info;pr_err ("Attempting late microcode loading - it is dangerous and taints the kernel.\n" );
@@ -418,26 +412,47 @@ static int microcode_reload_late(void)*/ store_cpu_caps (&prev_info); - ret =stop_machine_cpuslocked (__reload_late,NULL , cpu_online_mask); +stop_machine_cpuslocked (ucode_load_cpus_stopped,NULL , cpu_online_mask); + +/* Analyze the results */ +for_each_cpu_and (cpu, cpu_present_mask, &cpus_booted_once_mask) { +switch (per_cpu (ucode_ctrl.result, cpu)) { +case UCODE_UPDATED : updated++;break ; +case UCODE_TIMEOUT : timedout++;break ; +case UCODE_OK : siblings++;break ; +default : failed++;break ; + } + }if (microcode_ops->finalize_late_load) - microcode_ops->finalize_late_load (ret); + microcode_ops->finalize_late_load (!updated); -if (!ret) { -pr_info ("Reload succeeded, microcode revision: 0x%x -> 0x%x\n" , - old, boot_cpu_data.microcode); -microcode_check (&prev_info); -add_taint (TAINT_CPU_OUT_OF_SPEC ,LOCKDEP_STILL_OK ); - }else { -pr_info ("Reload failed, current microcode revision: 0x%x\n" , - boot_cpu_data.microcode); +if (!updated) { +/* Nothing changed. */ +if (!failed && !timedout) +return 0 ; +pr_err ("Microcode update failed: %u CPUs failed %u CPUs timed out\n" , + failed, timedout); +return -EIO ; } -return ret; + +add_taint (TAINT_CPU_OUT_OF_SPEC ,LOCKDEP_STILL_OK ); +pr_info ("Microcode load: updated on %u primary CPUs with %u siblings\n" , updated, siblings); +if (failed || timedout) { +pr_err ("Microcode load incomplete. %u CPUs timed out or failed\n" , +num_online_cpus () - (updated + siblings)); + } +pr_info ("Microcode revision: 0x%x -> 0x%x\n" , old_rev, boot_cpu_data.microcode); +microcode_check (&prev_info); + +return updated + siblings ==num_online_cpus () ?0 : -EIO ; }/* -* Ensure that all required CPUs which are present and have been booted -* once are online. +* This function does two things: +* +* 1) Ensure that all required CPUs which are present and have been booted +* once are online. * * To pass this check, all primary threads must be online. *
@@ -448,9 +463,12 @@ static int microcode_reload_late(void)* behaviour is undefined. The default play_dead() implementation on * modern CPUs is using MWAIT, which is also not guaranteed to be safe * against a microcode update which affects MWAIT. +* +* 2) Initialize the per CPU control structure */ -static bool ensure_cpus_are_online (void ) +static bool ucode_setup_cpus (void ) { +struct ucode_ctrl ctrl = { .result =-1 , };unsigned int cpu;for_each_cpu_and (cpu, cpu_present_mask, &cpus_booted_once_mask) {
@@ -460,6 +478,8 @@ static bool ensure_cpus_are_online(void)return false; } } +/* Initialize the per CPU state */ +per_cpu (ucode_ctrl, cpu) = ctrl; }return true; }
@@ -468,13 +488,13 @@ static int ucode_load_late_locked(void) {int ret; -if (!ensure_cpus_are_online ()) +if (!ucode_setup_cpus ())return -EBUSY ; ret = microcode_ops->request_microcode_fw (0 , µcode_pdev->dev);if (ret !=UCODE_NEW )return ret ==UCODE_NFOUND ? -ENOENT : -EBADFD ; -return microcode_reload_late (); +return ucode_load_late_stop_cpus (); } static ssize_t reload_store(struct device *dev,
arch/x86/kernel/cpu/microcode/internal.h +1 -0
@@ -16,6 +16,7 @@ enum ucode_state {UCODE_UPDATED ,UCODE_NFOUND ,UCODE_ERROR , + UCODE_TIMEOUT, }; struct microcode_ops {
PATCH v2 29/37 x86/microcode: Add per CPU control field
In reply to Thomas GleixnerFrom: Thomas Gleixner <[email protected]>
Add a per CPU control field to ucode_ctrl and define constants for it:
SCTRL_WAIT indicates that the CPU needs to spinwait with timeoutSCTRL_APPLY indicates that the CPU needs to invoke the microcode_apply()
callbackSCTRL_DONE indicates that the CPU can proceed without invoking the
microcode_apply() callback.
In theory this could be a global control field, but a global control does not cover the following case:
15 primary CPUs load microcode successfully
1 primary CPU fails and returns with an error code
With global control the sibling of the failed CPU would either try again or the whole operation would be aborted with the consequence that the 15 siblings do not invoke the apply path and end up with inconsistent software state. The result in dmesg would be inconsistent too.
There are two additional fields added and initialized:
ctrl_cpu and secondaries. ctrl_cpu is the CPU number of the primary thread for now, but with the upcoming uniform loading at package or system scope this will be one CPU per package or just one CPU. Secondaries hands the control CPU a CPU mask which will be required to release the secondary CPUs out of the wait loop.
Preparatory change for implementing a properly split control flow for primary and secondary CPUs.
--- arch/x86/kernel/cpu/microcode/core.c | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-)
---
arch/x86/kernel/cpu/microcode/core.c +18 -2
@@ -324,8 +324,16 @@ static struct platform_device *microcode* requirement can be relaxed in the future. Right now, this is conservative * and good. */ +enum sibling_ctrl { +SCTRL_WAIT , +SCTRL_APPLY , +SCTRL_DONE , +}; +struct ucode_ctrl { +enum sibling_ctrl ctrl;enum ucode_state result; +unsigned int ctrl_cpu; }; static DEFINE_PER_CPU(struct ucode_ctrl, ucode_ctrl);
@@ -468,7 +476,7 @@ static int ucode_load_late_stop_cpus(voi*/ static bool ucode_setup_cpus (void ) { -struct ucode_ctrl ctrl = { .result =-1 , }; +struct ucode_ctrl ctrl = { .ctrl =SCTRL_WAIT , .result =-1 , };unsigned int cpu;for_each_cpu_and (cpu, cpu_present_mask, &cpus_booted_once_mask) {
@@ -478,7 +486,15 @@ static bool ucode_setup_cpus(void)return false; } } -/* Initialize the per CPU state */ + +/* +* Initialize the per CPU state. This is core scope for now, +* but prepared to take package or system scope into account. +*/ +if (topology_is_primary_thread (cpu)) + ctrl.ctrl_cpu = cpu; +else + ctrl.ctrl_cpu =cpumask_first (topology_sibling_cpumask (cpu));per_cpu (ucode_ctrl, cpu) = ctrl; }return true;
PATCH v2 30/37 x86/microcode: Provide new control functions
In reply to Thomas GleixnerFrom: Thomas Gleixner <[email protected]>
The current all in one code is unreadable and really not suited for adding future features like uniform loading with package or system scope.
Provide a set of new control functions which split the handling of the primary and secondary CPUs. These will replace the current rendevouz all in one function in the next step. This is intentionally a separate change because diff makes an complete unreadable mess otherwise.
So the flow separates the primary and the secondary CPUs into their own functions, which use the control field in the per CPU ucode_ctrl struct.
primary() secondary()
wait_for_all() wait_for_all()
apply_ucode() wait_for_release()
release() apply_ucode()--- arch/x86/kernel/cpu/microcode/core.c | 86 +++++++++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+)
---
arch/x86/kernel/cpu/microcode/core.c +86 -0
@@ -357,6 +357,92 @@ static bool wait_for_cpus(atomic_t *cnt)return false; } +static bool wait_for_ctrl (void ) +{ +unsigned int timeout; + +for (timeout =0 ; timeout <USEC_PER_SEC ; timeout++) { +if (this_cpu_read (ucode_ctrl.ctrl) !=SCTRL_WAIT ) +return true; +udelay (1 ); +if (!(timeout %1000 )) +touch_nmi_watchdog (); + } +return false; +} + +static __maybe_unused void ucode_load_secondary (unsigned int cpu) +{ +unsigned int ctrl_cpu =this_cpu_read (ucode_ctrl.ctrl_cpu); +enum ucode_state ret; + +/* Initial rendevouz to ensure that all CPUs have arrived */ +if (!wait_for_cpus (&late_cpus_in)) { +pr_err_once ("Microcode load: %d CPUs timed out\n" , +atomic_read (&late_cpus_in) -1 ); +this_cpu_write (ucode_ctrl.result,UCODE_TIMEOUT ); +return ; + } + +/* +* Wait for primary threads to complete. If one of them hangs due +* to the update, there is no way out. This is non-recoverable +* because the CPU might hold locks or resources and confuse the +* scheduler, watchdogs etc. There is no way to safely evacuate the +* machine. +*/ +if (!wait_for_ctrl ()) +panic ("Microcode load: Primary CPU %d timed out\n" , ctrl_cpu); + +/* +* If the primary succeeded then invoke the apply() callback, +* otherwise copy the state from the primary thread. +*/ +if (this_cpu_read (ucode_ctrl.ctrl) ==SCTRL_APPLY ) + ret = microcode_ops->apply_microcode (cpu); +else + ret =per_cpu (ucode_ctrl.result, ctrl_cpu); + +this_cpu_write (ucode_ctrl.result, ret); +this_cpu_write (ucode_ctrl.ctrl,SCTRL_DONE ); +} + +static __maybe_unused void ucode_load_primary (unsigned int cpu) +{ +struct cpumask *secondaries =topology_sibling_cpumask (cpu); +enum sibling_ctrl ctrl; +enum ucode_state ret; +unsigned int sibling; + +/* Initial rendevouz to ensure that all CPUs have arrived */ +if (!wait_for_cpus (&late_cpus_in)) { +this_cpu_write (ucode_ctrl.result,UCODE_TIMEOUT ); +pr_err_once ("Microcode load: %d CPUs timed out\n" , +atomic_read (&late_cpus_in) -1 ); +return ; + } + + ret = microcode_ops->apply_microcode (cpu); +this_cpu_write (ucode_ctrl.result, ret); +this_cpu_write (ucode_ctrl.ctrl,SCTRL_DONE ); + +/* +* If the update was successful, let the siblings run the apply() +* callback. If not, tell them it's done. This also covers the +* case where the CPU has uniform loading at package or system +* scope implemented but does not advertise it. +*/ +if (ret ==UCODE_UPDATED || ret ==UCODE_OK ) + ctrl =SCTRL_APPLY ; +else + ctrl =SCTRL_DONE ; + +for_each_cpu (sibling, secondaries) { +if (sibling != cpu) +per_cpu (ucode_ctrl.ctrl, sibling) = ctrl; + } +} +static int ucode_load_cpus_stopped (void *unused) {int cpu =smp_processor_id ();
PATCH v2 31/37 x86/microcode: Replace the all in one rendevouz handler
In reply to Thomas GleixnerFrom: Thomas Gleixner <[email protected]>
with a new handler which just separates the control flow of primary and secondary CPUs.
--- arch/x86/kernel/cpu/microcode/core.c | 51 ++++++----------------------------- 1 file changed, 9 insertions(+), 42 deletions(-)
---
arch/x86/kernel/cpu/microcode/core.c +9 -42
@@ -337,7 +337,7 @@ struct ucode_ctrl { }; static DEFINE_PER_CPU(struct ucode_ctrl, ucode_ctrl); -static atomic_t late_cpus_in, late_cpus_out; +static atomic_t late_cpus_in;static bool wait_for_cpus (atomic_t *cnt) {
@@ -371,7 +371,7 @@ static bool wait_for_ctrl(void)return false; } -static __maybe_unused void ucode_load_secondary (unsigned int cpu) +static void ucode_load_secondary (unsigned int cpu) {unsigned int ctrl_cpu =this_cpu_read (ucode_ctrl.ctrl_cpu);enum ucode_state ret;
@@ -407,7 +407,7 @@ static __maybe_unused void ucode_load_sethis_cpu_write (ucode_ctrl.ctrl,SCTRL_DONE ); } -static __maybe_unused void ucode_load_primary (unsigned int cpu) +static void ucode_load_primary (unsigned int cpu) {struct cpumask *secondaries =topology_sibling_cpumask (cpu);enum sibling_ctrl ctrl;
@@ -445,46 +445,14 @@ static __maybe_unused void ucode_load_prstatic int ucode_load_cpus_stopped (void *unused) { -int cpu =smp_processor_id (); -enum ucode_state ret; - -/* -* Wait for all CPUs to arrive. A load will not be attempted unless all -* CPUs show up. -* */ -if (!wait_for_cpus (&late_cpus_in)) { -this_cpu_write (ucode_ctrl.result,UCODE_TIMEOUT ); -return 0 ; - } - -/* -* On an SMT system, it suffices to load the microcode on one sibling of -* the core because the microcode engine is shared between the threads. -* Synchronization still needs to take place so that no concurrent -* loading attempts happen on multiple threads of an SMT core. See -* below. -*/ -if (cpumask_first (topology_sibling_cpumask (cpu)) != cpu) - goto wait_for_siblings; +unsigned int cpu =smp_processor_id (); - ret = microcode_ops->apply_microcode (cpu); -this_cpu_write (ucode_ctrl.result, ret); - -wait_for_siblings: -if (!wait_for_cpus (&late_cpus_out)) -panic ("Timeout during microcode update!\n" ); - -/* -* At least one thread has completed update on each core. -* For others, simply call the update to make sure the -* per-cpu cpuinfo can be updated with right microcode -* revision. -*/ -if (cpumask_first (topology_sibling_cpumask (cpu)) == cpu) -return 0 ; +if (this_cpu_read (ucode_ctrl.ctrl_cpu) == cpu) +ucode_load_primary (cpu); +else +ucode_load_secondary (cpu); - ret = microcode_ops->apply_microcode (cpu); -this_cpu_write (ucode_ctrl.result, ret); +/* No point to wait here. The CPUs will all wait in stop_machine(). */ return 0 ; }
@@ -498,7 +466,6 @@ static int ucode_load_late_stop_cpus(voipr_err ("You should switch to early loading, if possible.\n" );atomic_set (&late_cpus_in,num_online_cpus ()); -atomic_set (&late_cpus_out,num_online_cpus ());/* * Take a snapshot before the microcode update in order to compare and
PATCH v2 32/37 x86/microcode: Rendezvous and load in NMI
In reply to Thomas GleixnerFrom: Thomas Gleixner <[email protected]>
stop_machine() does not prevent the spin-waiting sibling from handling an NMI, which is obviously violating the whole concept of rendezvous.
Implement a static branch right in the beginning of the NMI handler which is NOOPed except when enabled by the late loading mechanism.
The later loader enables the static branch before stop_machine() is invoked. Each CPU has an nmi_enable in its control structure which indicates whether the CPU should go into the update routine.
This is required to bridge the gap between enabling the branch and actually being at the point where it makes sense.
Each CPU which arrives in the stopper thread function sets that flag and issues a self NMI right after that. If the NMI function sees the flag clear, it returns. If it's set it clears the flag and enters the rendezvous.
This is safe against a real NMI which hits in between setting the flag and sending the NMI to itself. The real NMI will be swallowed by the microcode update and the self NMI will then let stuff continue. Otherwise this would end up with a spurious NMI.
--- arch/x86/include/asm/microcode.h | 12 ++++++++ arch/x86/kernel/cpu/microcode/core.c | 42 ++++++++++++++++++++++++++++--- arch/x86/kernel/cpu/microcode/intel.c | 1 arch/x86/kernel/cpu/microcode/internal.h | 3 +- arch/x86/kernel/nmi.c | 4 ++ 5 files changed, 57 insertions(+), 5 deletions(-)
---
arch/x86/include/asm/microcode.h +12 -0
@@ -75,4 +75,16 @@ void show_ucode_info_early(void);static inline void show_ucode_info_early (void ) { }#endif /* !CONFIG_CPU_SUP_INTEL */ +bool microcode_nmi_handler (void ); + +#ifdef CONFIG_MICROCODE_LATE_LOADING +DECLARE_STATIC_KEY_FALSE (microcode_nmi_handler_enable); +static __always_inline bool microcode_nmi_handler_enabled(void) +{ +return static_branch_unlikely (µcode_nmi_handler_enable); +} +#else +static __always_inline bool microcode_nmi_handler_enabled(void) { return false; } +#endif +#endif /* _ASM_X86_MICROCODE_H */
arch/x86/kernel/cpu/microcode/core.c +38 -4
94 lines not on this page · raw message
arch/x86/kernel/cpu/microcode/intel.c +1 -0
@@ -688,6 +688,7 @@ static struct microcode_ops microcode_in .collect_cpu_info = collect_cpu_info, .apply_microcode = apply_microcode_late, .finalize_late_load = finalize_late_load, + .use_nmi = IS_ENABLED(CONFIG_X86_64), };static __init void calc_llc_size_per_core (struct cpuinfo_x86 *c)
arch/x86/kernel/cpu/microcode/internal.h +2 -1
@@ -31,7 +31,8 @@ struct microcode_ops {enum ucode_state (*apply_microcode)(int cpu);int (*collect_cpu_info)(int cpu,struct cpu_signature *csig);void (*finalize_late_load)(int result); - unsigned int nmi_safe : 1; + unsigned int nmi_safe : 1, + use_nmi : 1; };extern struct ucode_cpu_info ucode_cpu_info[];
arch/x86/kernel/nmi.c +4 -0
@@ -33,6 +33,7 @@#include <asm/reboot.h> #include <asm/cache.h> #include <asm/nospec-branch.h> +#include <asm/microcode.h> #include <asm/sev.h> #define CREATE_TRACE_POINTS
@@ -343,6 +344,9 @@ static noinstr void default_do_nmi(strucinstrumentation_begin (); +if (microcode_nmi_handler_enabled () &µcode_nmi_handler ()) + goto out; + handled =nmi_handle (NMI_LOCAL , regs);__this_cpu_add (nmi_stats.normal, handled);if (handled) {
PATCH v2 33/37 x86/microcode: Protect against instrumentation
In reply to Thomas GleixnerFrom: Thomas Gleixner <[email protected]>
The wait for control loop in which the siblings are waiting for the microcode update on the primary thread must be protected against instrumentation as instrumentation can end up in #INT3, #DB or #PF, which then returns with IRET. That IRET reenables NMI which is the opposite of what the NMI rendezvouz is trying to achieve.
--- arch/x86/kernel/cpu/microcode/core.c | 112 ++++++++++++++++++++++++++--------- 1 file changed, 84 insertions(+), 28 deletions(-)
---
arch/x86/kernel/cpu/microcode/core.c +84 -28
@@ -341,53 +341,65 @@ struct ucode_ctrl {DEFINE_STATIC_KEY_FALSE (microcode_nmi_handler_enable); static DEFINE_PER_CPU(struct ucode_ctrl, ucode_ctrl); +static unsigned int loops_per_usec;static atomic_t late_cpus_in; -static bool wait_for_cpus (atomic_t *cnt) +static noinstr bool wait_for_cpus(atomic_t *cnt) { -unsigned int timeout; +unsigned int timeout, loops; -WARN_ON_ONCE (atomic_dec_return (cnt) <0 ); +WARN_ON_ONCE (raw_atomic_dec_return (cnt) <0 );for (timeout =0 ; timeout <USEC_PER_SEC ; timeout++) { -if (!atomic_read (cnt)) +if (!raw_atomic_read (cnt))return true; -udelay (1 ); + +for (loops =0 ; loops < loops_per_usec; loops++) +cpu_relax (); +/* If invoked directly, tickle the NMI watchdog */ -if (!microcode_ops->use_nmi && !(timeout %1000 )) +if (!microcode_ops->use_nmi && !(timeout %1000 )) { +instrumentation_begin ();touch_nmi_watchdog (); +instrumentation_end (); + } }/* Prevent the late comers to make progress and let them time out */ -atomic_inc (cnt); +raw_atomic_inc (cnt);return false; } -static bool wait_for_ctrl (void ) +static noinstr bool wait_for_ctrl(void) { -unsigned int timeout; +unsigned int timeout, loops;for (timeout =0 ; timeout <USEC_PER_SEC ; timeout++) { -if (this_cpu_read (ucode_ctrl.ctrl) !=SCTRL_WAIT ) +if (raw_cpu_read (ucode_ctrl.ctrl) !=SCTRL_WAIT )return true; -udelay (1 ); + +for (loops =0 ; loops < loops_per_usec; loops++) +cpu_relax (); +/* If invoked directly, tickle the NMI watchdog */ -if (!microcode_ops->use_nmi && !(timeout %1000 )) +if (!microcode_ops->use_nmi && !(timeout %1000 )) { +instrumentation_begin ();touch_nmi_watchdog (); +instrumentation_end (); + } }return false; } -static void ucode_load_secondary (unsigned int cpu) +/* +* Protected against instrumentation up to the point where the primary +* thread completed the update. See microcode_nmi_handler() for details. +*/ +static noinstr bool ucode_load_secondary_wait(unsigned int ctrl_cpu) { -unsigned int ctrl_cpu =this_cpu_read (ucode_ctrl.ctrl_cpu); -enum ucode_state ret; -/* Initial rendevouz to ensure that all CPUs have arrived */ if (!wait_for_cpus (&late_cpus_in)) { -pr_err_once ("Microcode load: %d CPUs timed out\n" , -atomic_read (&late_cpus_in) -1 );this_cpu_write (ucode_ctrl.result,UCODE_TIMEOUT ); -return ; +return false; }/*
@@ -397,9 +409,33 @@ static void ucode_load_secondary(unsigne* scheduler, watchdogs etc. There is no way to safely evacuate the * machine. */ -if (!wait_for_ctrl ()) -panic ("Microcode load: Primary CPU %d timed out\n" , ctrl_cpu); +if (wait_for_ctrl ()) +return true; + +instrumentation_begin (); +panic ("Microcode load: Primary CPU %d timed out\n" , ctrl_cpu); +instrumentation_end (); +} +/* +* Protected against instrumentation up to the point where the primary +* thread completed the update. See microcode_nmi_handler() for details. +*/ +static noinstr void ucode_load_secondary(unsigned int cpu) +{ +unsigned int ctrl_cpu =raw_cpu_read (ucode_ctrl.ctrl_cpu); +enum ucode_state ret; + +if (!ucode_load_secondary_wait (ctrl_cpu)) { +instrumentation_begin (); +pr_err_once ("Microcode load: %d CPUs timed out\n" , +atomic_read (&late_cpus_in) -1 ); +instrumentation_end (); +return ; + } + +/* Primary thread completed. Allow to invoke instrumentable code */ +instrumentation_begin ();/* * If the primary succeeded then invoke the apply() callback, * otherwise copy the state from the primary thread.
@@ -411,6 +447,7 @@ static void ucode_load_secondary(unsignethis_cpu_write (ucode_ctrl.result, ret);this_cpu_write (ucode_ctrl.ctrl,SCTRL_DONE ); +instrumentation_end (); }static void ucode_load_primary (unsigned int cpu)
@@ -449,25 +486,43 @@ static void ucode_load_primary(unsigned } } -static bool microcode_update_handler (void ) +static noinstr bool microcode_update_handler(void) { -unsigned int cpu =smp_processor_id (); +unsigned int cpu =raw_smp_processor_id (); -if (this_cpu_read (ucode_ctrl.ctrl_cpu) == cpu) +if (raw_cpu_read (ucode_ctrl.ctrl_cpu) == cpu) { +instrumentation_begin ();ucode_load_primary (cpu); -else +instrumentation_end (); + }else {ucode_load_secondary (cpu); + } +instrumentation_begin ();touch_nmi_watchdog (); +instrumentation_end (); +return true; } -bool microcode_nmi_handler (void ) +/* +* Protection against instrumentation is required for CPUs which are not +* safe against an NMI which is delivered to the secondary SMT sibling +* while the primary thread updates the microcode. Instrumentation can end +* up in #INT3, #DB and #PF. The IRET from those exceptions reenables NMI +* which is the opposite of what the NMI rendevouz is trying to achieve. +* +* The primary thread is safe versus instrumentation as the actual +* microcode update handles this correctly. It's only the sibling code +* path which must be NMI safe until the primary thread completed the +* update. +*/ +bool noinstr microcode_nmi_handler(void) { -if (!this_cpu_read (ucode_ctrl.nmi_enabled)) +if (!raw_cpu_read (ucode_ctrl.nmi_enabled))return false; -this_cpu_write (ucode_ctrl.nmi_enabled, false); +raw_cpu_write (ucode_ctrl.nmi_enabled, false);return microcode_update_handler (); }
@@ -494,6 +549,7 @@ static int ucode_load_late_stop_cpus(voipr_err ("You should switch to early loading, if possible.\n" );atomic_set (&late_cpus_in,num_online_cpus ()); + loops_per_usec = loops_per_jiffy / (TICK_NSEC /1000 );/* * Take a snapshot before the microcode update in order to compare and
PATCH v2 34/37 x86/apic: Provide apic_force_nmi_on_cpu()
In reply to Thomas GleixnerFrom: Thomas Gleixner <[email protected]>
When SMT siblings are soft-offlined and parked in one of the play_dead() variants they still react on NMI, which is problematic on affected Intel CPUs. The default play_dead() variant uses MWAIT on modern CPUs, which is not guaranteed to be safe when updated concurrently.
Right now late loading is prevented when not all SMT siblings are online, but as they still react on NMI, it is possible to bring them out of their park position into a trivial rendevouz handler.
Provide a function which allows to do that. I does sanity checks whether the target is in the cpus_booted_once_mask and whether the APIC driver supports it.
Mark X2APIC and XAPIC as capable, but exclude 32bit and the UV and NUMACHIP variants as that needs feedback from the relevant experts.
--- arch/x86/include/asm/apic.h | 5 +++++ arch/x86/kernel/apic/apic_flat_64.c | 2 ++ arch/x86/kernel/apic/ipi.c | 9 ++++++++- arch/x86/kernel/apic/x2apic_cluster.c | 1 + arch/x86/kernel/apic/x2apic_phys.c | 1 + 5 files changed, 17 insertions(+), 1 deletion(-)
---
arch/x86/include/asm/apic.h +5 -0
@@ -301,6 +301,9 @@ struct apic {enum apic_delivery_modes delivery_mode;bool dest_mode_logical; +/* Allows to send an NMI to an "offline" CPU which hangs in *play_dead() */ +bool nmi_to_offline_cpu; +u32 (*calc_dest_apicid)(unsigned int cpu);/* ICR related functions */
@@ -505,6 +508,8 @@ extern void default_ioapic_phys_id_map(pextern int default_cpu_present_to_apicid (int mps_cpu);extern int default_check_phys_apicid_present (int phys_apicid); +void apic_send_nmi_to_offline_cpu (unsigned int cpu); +#endif /* CONFIG_X86_LOCAL_APIC */ #ifdef CONFIG_SMP
arch/x86/kernel/apic/apic_flat_64.c +2 -0
@@ -138,6 +138,7 @@ static struct apic apic_flat __ro_after_ .send_IPI_allbutself = default_send_IPI_allbutself, .send_IPI_all = default_send_IPI_all, .send_IPI_self = default_send_IPI_self, + .nmi_to_offline_cpu = true, .inquire_remote_apic = default_inquire_remote_apic,
@@ -229,6 +230,7 @@ static struct apic apic_physflat __ro_af .send_IPI_allbutself = default_send_IPI_allbutself, .send_IPI_all = default_send_IPI_all, .send_IPI_self = default_send_IPI_self, + .nmi_to_offline_cpu = true, .inquire_remote_apic = default_inquire_remote_apic,
arch/x86/kernel/apic/ipi.c +8 -1
@@ -95,8 +95,15 @@ void native_send_call_func_ipi(const str apic->send_IPI_mask(mask, CALL_FUNCTION_VECTOR); } +void apic_send_nmi_to_offline_cpu (unsigned int cpu) +{ +if (WARN_ON_ONCE (!apic->nmi_to_offline_cpu)) +return ; +if (WARN_ON_ONCE (!cpumask_test_cpu (cpu, &cpus_booted_once_mask))) +return ; + apic->send_IPI (cpu,NMI_VECTOR ); +}#endif /* CONFIG_SMP */ -static inline int __prepare_ICR2 (unsigned int mask) {return SET_XAPIC_DEST_FIELD (mask);
arch/x86/kernel/apic/x2apic_cluster.c +1 -0
@@ -264,6 +264,7 @@ static struct apic apic_x2apic_cluster _ .send_IPI_allbutself = x2apic_send_IPI_allbutself, .send_IPI_all = x2apic_send_IPI_all, .send_IPI_self = x2apic_send_IPI_self, + .nmi_to_offline_cpu = true, .inquire_remote_apic = NULL,
arch/x86/kernel/apic/x2apic_phys.c +1 -0
@@ -188,6 +188,7 @@ static struct apic apic_x2apic_phys __ro .send_IPI_allbutself = x2apic_send_IPI_allbutself, .send_IPI_all = x2apic_send_IPI_all, .send_IPI_self = x2apic_send_IPI_self, + .nmi_to_offline_cpu = true, .inquire_remote_apic = NULL,
PATCH v2 35/37 x86/microcode: Handle "offline" CPUs correctly
In reply to Thomas GleixnerFrom: Thomas Gleixner <[email protected]>
Offline CPUs need to be parked in a safe loop when microcode update is in progress on the primary CPU. Currently offline CPUs are parked in 'mwait_play_dead()', and for Intel CPUs, its not a safe instruction, because 'mwait' instruction can be patched in the new microcode update that can cause instability.
- Adds a new microcode state 'UCODE_OFFLINE' to report status on per-cpu basis.
- Force NMI on the offline CPUs.
Wakeup offline CPUs while the update is in progress and then return them back to 'mwait_play_dead()' after microcode update is complete.
--- arch/x86/include/asm/microcode.h | 1 arch/x86/kernel/cpu/microcode/core.c | 112 +++++++++++++++++++++++++++++-- arch/x86/kernel/cpu/microcode/internal.h | 1 arch/x86/kernel/nmi.c | 5 + 4 files changed, 113 insertions(+), 6 deletions(-)
---
arch/x86/include/asm/microcode.h +1 -0
@@ -76,6 +76,7 @@ static inline void show_ucode_info_early#endif /* !CONFIG_CPU_SUP_INTEL */ bool microcode_nmi_handler (void ); +void microcode_offline_nmi_handler (void );#ifdef CONFIG_MICROCODE_LATE_LOADING DECLARE_STATIC_KEY_FALSE (microcode_nmi_handler_enable);
arch/x86/kernel/cpu/microcode/core.c +107 -5
171 lines not on this page · raw message
arch/x86/kernel/cpu/microcode/internal.h +1 -0
@@ -17,6 +17,7 @@ enum ucode_state {UCODE_NFOUND ,UCODE_ERROR ,UCODE_TIMEOUT , + UCODE_OFFLINE, }; struct microcode_ops {
arch/x86/kernel/nmi.c +4 -1
@@ -502,8 +502,11 @@ DEFINE_IDTENTRY_RAW(exc_nmi)if (IS_ENABLED (CONFIG_NMI_CHECK_CPU ))raw_atomic_long_inc (&nsp->idt_calls); -if (IS_ENABLED (CONFIG_SMP ) &&arch_cpu_is_offline (smp_processor_id ())) +if (IS_ENABLED (CONFIG_SMP ) &&arch_cpu_is_offline (smp_processor_id ())) { +if (microcode_nmi_handler_enabled ()) +microcode_offline_nmi_handler ();return ; + }if (this_cpu_read (nmi_state) !=NMI_NOT_RUNNING ) {this_cpu_write (nmi_state,NMI_LATCHED );
PATCH v2 36/37 x86/microcode: Prepare for minimal revision check
In reply to Thomas GleixnerFrom: Thomas Gleixner <[email protected]>
Applying microcode late can be fatal for the running kernel when the update changes functionality which is in use already in a non-compatible way, e.g. by removing a CPUID bit.
There is no way for admins which do not have access to the vendors deep technical support to decide whether late loading of such a microcode is safe or not.
Intel has added a new field to the microcode header which tells the minimal microcode revision which is required to be active in the CPU in order to be safe.
Provide infrastructure for handling this in the core code and a command line switch which allows to enforce it.
If the update is considered safe the kernel is not tainted and the annoying warning message not emitted. If it's enforced and the currently loaded microcode revision is not safe for late loading then the load is aborted.
--- Documentation/admin-guide/kernel-parameters.txt | 5 ++++ arch/x86/Kconfig | 23 ++++++++++++++++++- arch/x86/kernel/cpu/microcode/amd.c | 3 ++ arch/x86/kernel/cpu/microcode/core.c | 29 ++++++++++++++++++------ arch/x86/kernel/cpu/microcode/intel.c | 3 ++ arch/x86/kernel/cpu/microcode/internal.h | 3 ++ 6 files changed, 58 insertions(+), 8 deletions(-)
---
Documentation/admin-guide/kernel-parameters.txt +5 -0
@@ -3239,6 +3239,11 @@ mga= [HW,DRM] + microcode.force_minrev= [X86] + Format: <bool> + Enable or disable the microcode minimal revision + enforcement for the runtime microcode loader. + min_addr=nn[KMG] [KNL,BOOT,IA-64] All physical memory below this physical address is ignored.
arch/x86/Kconfig +22 -1
@@ -1320,7 +1320,28 @@ config MICROCODE_LATE_LOADING is a tricky business and should be avoided if possible. Just the sequence of synchronizing all cores and SMT threads is one fragile dance which does not guarantee that cores might not softlock after the loading. Therefore, - use this at your own risk. Late loading taints the kernel too. + use this at your own risk. Late loading taints the kernel unless the + microcode header indicates that it is safe for late loading via the + minimal revision check. This minimal revision check can be enforced on + the kernel command line with "microcode.minrev=Y". + +config MICROCODE_LATE_FORCE_MINREV + bool "Enforce late microcode loading minimal revision check" + default n + depends on MICROCODE_LATE_LOADING + help + To prevent that users load microcode late which modifies already + in use features, newer microcodes have a minimum revision field + in the microcode header, which tells the kernel which minimum + revision must be active in the CPU to safely load that new microcode + late into the running system. If disabled the check will not + be enforced but the kernel will be tainted when the minimal + revision check fails. + + This minimal revision check can also be controlled via the + "microcode.minrev" parameter on the kernel command line. + + If unsure say Y. config X86_MSR tristate "/dev/cpu/*/msr - Model-specific register support"
arch/x86/kernel/cpu/microcode/amd.c +3 -0
@@ -919,6 +919,9 @@ static enum ucode_state request_microcodenum ucode_state ret =UCODE_NFOUND ;const struct firmware *fw; +if (force_minrev) +return UCODE_NFOUND ; +if (c->x86 >=0x15 )snprintf (fw_name,sizeof (fw_name),"amd-ucode/microcode_amd_fam%.2xh.bin" , c->x86);
arch/x86/kernel/cpu/microcode/core.c +22 -7
@@ -46,6 +46,9 @@static struct microcode_ops *microcode_ops;static bool dis_ucode_ldr = true; +bool force_minrev =IS_ENABLED (CONFIG_MICROCODE_LATE_FORCE_MINREV ); +module_param (force_minrev, bool,S_IRUSR |S_IWUSR ); +bool initrd_gone;/*
@@ -601,15 +604,17 @@ static int ucode_load_cpus_stopped(voidreturn 0 ; } -static int ucode_load_late_stop_cpus (void ) +static int ucode_load_late_stop_cpus (bool is_safe) {unsigned int cpu, updated =0 , failed =0 , timedout =0 , siblings =0 ;unsigned int nr_offl, offline =0 ;int old_rev = boot_cpu_data.microcode;struct cpuinfo_x86 prev_info; -pr_err ("Attempting late microcode loading - it is dangerous and taints the kernel.\n" ); -pr_err ("You should switch to early loading, if possible.\n" ); +if (!is_safe) { +pr_err ("Late microcode loading without minimal revision check.\n" ); +pr_err ("You should switch to early loading, if possible.\n" ); + }atomic_set (&late_cpus_in,num_online_cpus ());atomic_set (&offline_in_nmi,0 );
@@ -659,7 +664,9 @@ static int ucode_load_late_stop_cpus(voireturn -EIO ; } -add_taint (TAINT_CPU_OUT_OF_SPEC ,LOCKDEP_STILL_OK ); +if (!is_safe || failed || timedout) +add_taint (TAINT_CPU_OUT_OF_SPEC ,LOCKDEP_STILL_OK ); +pr_info ("Microcode load: updated on %u primary CPUs with %u siblings\n" , updated, siblings);if (failed || timedout) { pr_err("Microcode load incomplete. %u CPUs timed out or failed\n",
@@ -753,9 +760,17 @@ static int ucode_load_late_locked(void)return -EBUSY ; ret = microcode_ops->request_microcode_fw (0 , µcode_pdev->dev); -if (ret !=UCODE_NEW ) -return ret ==UCODE_NFOUND ? -ENOENT : -EBADFD ; -return ucode_load_late_stop_cpus (); + +switch (ret) { +case UCODE_NEW : +case UCODE_NEW_SAFE : +break ; +case UCODE_NFOUND : +return -ENOENT ; +default : +return -EBADFD ; + } +return ucode_load_late_stop_cpus (ret ==UCODE_NEW_SAFE ); } static ssize_t reload_store(struct device *dev,
arch/x86/kernel/cpu/microcode/intel.c +3 -0
@@ -551,6 +551,9 @@ static enum ucode_state read_ucode_intelint cur_rev = uci->cpu_sig.rev;u8 *new_mc =NULL , *mc =NULL ; +if (force_minrev) +return UCODE_NFOUND ; +while (iov_iter_count (iter)) {struct microcode_header_intel mc_header;unsigned int mc_size, data_size;
arch/x86/kernel/cpu/microcode/internal.h +3 -0
@@ -13,6 +13,7 @@ struct device;enum ucode_state {UCODE_OK =0 ,UCODE_NEW , +UCODE_NEW_SAFE ,UCODE_UPDATED ,UCODE_NFOUND ,UCODE_ERROR ,
@@ -36,6 +37,8 @@ struct microcode_ops { use_nmi :1 ; }; +extern bool force_minrev; +extern struct ucode_cpu_info ucode_cpu_info[];struct cpio_data find_microcode_in_initrd (const char *path,bool use_pa);
PATCH v2 37/37 x86/microcode/intel: Add a minimum required revision for late-loads
In reply to Thomas GleixnerFrom: Ashok Raj <[email protected]>
In general users don't have the necessary information to determine whether late loading of a new microcode version is safe and does not modify anything which the currently running kernel uses already, e.g. removal of CPUID bits or behavioural changes of MSRs.
To address this issue, Intel has added a "minimum required version" field to a previously reserved field in the microcode header. Microcode updates should only be applied if the current microcode version is equal to, or greater than this minimum required version.
Thomas made some suggestions on how meta-data in the microcode file could provide Linux with information to decide if the new microcode is suitable candidate for late loading. But even the "simpler" option requires a lot of metadata and corresponding kernel code to parse it, so the final suggestion was to add the 'minimum required version' field in the header.
When microcode changes visible features, microcode will set the minimum required version to its own revision which prevents late loading.
Old microcode blobs have the minimum revision field always set to 0, which indicates that there is no information and the kernel considers it as unsafe.
This is a pure OS software mechanism. The hardware/firmware ignores this header field.
For early loading there is no restriction because OS visible features are enumerated after the early load and therefor a change has no effect.
The check is always enabled, but by default not enforced. It can be enforced via Kconfig or kernel command line.
If enforced, the kernel refuses to late load microcode with a minium required version field which is zero or when the currently loaded microcode revision is smaller than the minimum required revision.
If not enforced the load happens independent of the revision check to stay compatible with the existing behaviour, but it influences the decision whether the kernel is tainted or not. If the check signals that the late load is safe, then the kernel is not tainted.
Early loading is not affected by this.
[ tglx: Massaged changelog and fixed up the implementation ]
--- arch/x86/include/asm/microcode.h | 3 +- arch/x86/kernel/cpu/microcode/intel.c | 37 ++++++++++++++++++++++++++++++---- 2 files changed, 35 insertions(+), 5 deletions(-)
---
arch/x86/include/asm/microcode.h +2 -1
@@ -36,7 +36,8 @@ struct microcode_header_intel {unsigned int datasize;unsigned int totalsize;unsigned int metasize; -unsigned int reserved[2 ]; +unsigned int min_req_ver; +unsigned int reserved; };struct microcode_intel {
arch/x86/kernel/cpu/microcode/intel.c +33 -4
@@ -544,16 +544,40 @@ static enum ucode_state apply_microcode_return ret; } +static bool ucode_validate_minrev (struct microcode_header_intel *mc_header) +{ +int cur_rev = boot_cpu_data.microcode; + +/* +* When late-loading, ensure the header declares a minimum revision +* required to perform a late-load. The previously reserved field +* is 0 in older microcode blobs. +*/ +if (!mc_header->min_req_ver) { +pr_info ("Unsafe microcode update: Microcode header does not specify a required min version\n" ); +return false; + } + +/* +* Check whether the minimum revision specified in the header is either +* greater or equal to the current revision. +*/ +if (cur_rev < mc_header->min_req_ver) { +pr_info ("Unsafe microcode update: Current revision 0x%x too old\n" , cur_rev); +pr_info ("Current should be at 0x%x or higher. Use early loading instead\n" , mc_header->min_req_ver); +return false; + } +return true; +} +static enum ucode_state read_ucode_intel (int cpu,struct iov_iter *iter) {struct ucode_cpu_info *uci = ucode_cpu_info + cpu;unsigned int curr_mc_size =0 , new_mc_size =0 ; +bool is_safe, new_is_safe = false;int cur_rev = uci->cpu_sig.rev;u8 *new_mc =NULL , *mc =NULL ; -if (force_minrev) -return UCODE_NFOUND ; -while (iov_iter_count (iter)) {struct microcode_header_intel mc_header;unsigned int mc_size, data_size;
@@ -596,10 +620,15 @@ static enum ucode_state read_ucode_intelif (!intel_find_matching_signature (mc, &uci->cpu_sig))continue ; + is_safe =ucode_validate_minrev (&mc_header); +if (force_minrev && !is_safe) +continue ; +kvfree (new_mc); cur_rev = mc_header.rev; new_mc = mc; new_mc_size = mc_size; + new_is_safe = is_safe; mc =NULL ; }
@@ -616,7 +645,7 @@ static enum ucode_state read_ucode_intelreturn UCODE_NFOUND ; ucode_patch_late = (struct microcode_intel *)new_mc; -return UCODE_NEW ; +return new_is_safe ?UCODE_NEW_SAFE :UCODE_NEW ; fail:kvfree (mc);