* [PATCH 0/5] x86/microcode/AMD: Some small cleanups
@ 2025-02-11 16:36 Borislav Petkov
2025-02-11 16:36 ` [PATCH 1/5] x86/microcode/AMD: Remove ugly linebreak in __verify_patch_section() signature Borislav Petkov
` (5 more replies)
0 siblings, 6 replies; 12+ messages in thread
From: Borislav Petkov @ 2025-02-11 16:36 UTC (permalink / raw)
To: X86 ML; +Cc: LKML, Borislav Petkov (AMD)
From: "Borislav Petkov (AMD)" <bp@alien8.de>
A bunch of small and trivial cleanups which sprang up from recent staring at
the loader.
No functional changes.
Thx.
Borislav Petkov (AMD) (5):
x86/microcode/AMD: Remove ugly linebreak in __verify_patch_section()
signature
x86/microcode/AMD: Remove unused save_microcode_in_initrd_amd()
declarations
x86/microcode/AMD: Merge early_apply_microcode() into its single
callsite
x86/microcode/AMD: Get rid of the _load_microcode_amd() forward
declaration
x86/microcode/AMD: Add get_patch_level()
arch/x86/kernel/cpu/microcode/amd.c | 155 +++++++++++------------
arch/x86/kernel/cpu/microcode/internal.h | 2 -
2 files changed, 73 insertions(+), 84 deletions(-)
--
2.43.0
^ permalink raw reply [flat|nested] 12+ messages in thread
* [PATCH 1/5] x86/microcode/AMD: Remove ugly linebreak in __verify_patch_section() signature
2025-02-11 16:36 [PATCH 0/5] x86/microcode/AMD: Some small cleanups Borislav Petkov
@ 2025-02-11 16:36 ` Borislav Petkov
2025-02-17 8:51 ` [tip: x86/microcode] " tip-bot2 for Borislav Petkov (AMD)
2025-02-11 16:36 ` [PATCH 2/5] x86/microcode/AMD: Remove unused save_microcode_in_initrd_amd() declarations Borislav Petkov
` (4 subsequent siblings)
5 siblings, 1 reply; 12+ messages in thread
From: Borislav Petkov @ 2025-02-11 16:36 UTC (permalink / raw)
To: X86 ML; +Cc: LKML, Borislav Petkov (AMD)
From: "Borislav Petkov (AMD)" <bp@alien8.de>
No functional changes.
Signed-off-by: Borislav Petkov (AMD) <bp@alien8.de>
---
arch/x86/kernel/cpu/microcode/amd.c | 3 +--
1 file changed, 1 insertion(+), 2 deletions(-)
diff --git a/arch/x86/kernel/cpu/microcode/amd.c b/arch/x86/kernel/cpu/microcode/amd.c
index a5dac7f3c0a0..4a62625c311a 100644
--- a/arch/x86/kernel/cpu/microcode/amd.c
+++ b/arch/x86/kernel/cpu/microcode/amd.c
@@ -246,8 +246,7 @@ static bool verify_equivalence_table(const u8 *buf, size_t buf_size)
* On success, @sh_psize returns the patch size according to the section header,
* to the caller.
*/
-static bool
-__verify_patch_section(const u8 *buf, size_t buf_size, u32 *sh_psize)
+static bool __verify_patch_section(const u8 *buf, size_t buf_size, u32 *sh_psize)
{
u32 p_type, p_size;
const u32 *hdr;
--
2.43.0
^ permalink raw reply related [flat|nested] 12+ messages in thread
* [PATCH 2/5] x86/microcode/AMD: Remove unused save_microcode_in_initrd_amd() declarations
2025-02-11 16:36 [PATCH 0/5] x86/microcode/AMD: Some small cleanups Borislav Petkov
2025-02-11 16:36 ` [PATCH 1/5] x86/microcode/AMD: Remove ugly linebreak in __verify_patch_section() signature Borislav Petkov
@ 2025-02-11 16:36 ` Borislav Petkov
2025-02-17 8:51 ` [tip: x86/microcode] " tip-bot2 for Borislav Petkov (AMD)
2025-02-11 16:36 ` [PATCH 3/5] x86/microcode/AMD: Merge early_apply_microcode() into its single callsite Borislav Petkov
` (3 subsequent siblings)
5 siblings, 1 reply; 12+ messages in thread
From: Borislav Petkov @ 2025-02-11 16:36 UTC (permalink / raw)
To: X86 ML; +Cc: LKML, Borislav Petkov (AMD)
From: "Borislav Petkov (AMD)" <bp@alien8.de>
Commit
a7939f016720 ("x86/microcode/amd: Cache builtin/initrd microcode early")
renamed it to save_microcode_in_initrd() and made it static. Zap the
forgotten declarations.
No functional changes.
Signed-off-by: Borislav Petkov (AMD) <bp@alien8.de>
---
arch/x86/kernel/cpu/microcode/amd.c | 2 +-
arch/x86/kernel/cpu/microcode/internal.h | 2 --
2 files changed, 1 insertion(+), 3 deletions(-)
diff --git a/arch/x86/kernel/cpu/microcode/amd.c b/arch/x86/kernel/cpu/microcode/amd.c
index 4a62625c311a..f831c0602994 100644
--- a/arch/x86/kernel/cpu/microcode/amd.c
+++ b/arch/x86/kernel/cpu/microcode/amd.c
@@ -517,7 +517,7 @@ static bool __apply_microcode_amd(struct microcode_amd *mc, unsigned int psize)
* patch container file in initrd, traverse equivalent cpu table, look for a
* matching microcode patch, and update, all in initrd memory in place.
* When vmalloc() is available for use later -- on 64-bit during first AP load,
- * and on 32-bit during save_microcode_in_initrd_amd() -- we can call
+ * and on 32-bit during save_microcode_in_initrd() -- we can call
* load_microcode_amd() to save equivalent cpu table and microcode patches in
* kernel heap memory.
*
diff --git a/arch/x86/kernel/cpu/microcode/internal.h b/arch/x86/kernel/cpu/microcode/internal.h
index 21776c529fa9..5df621752fef 100644
--- a/arch/x86/kernel/cpu/microcode/internal.h
+++ b/arch/x86/kernel/cpu/microcode/internal.h
@@ -100,14 +100,12 @@ extern bool force_minrev;
#ifdef CONFIG_CPU_SUP_AMD
void load_ucode_amd_bsp(struct early_load_data *ed, unsigned int family);
void load_ucode_amd_ap(unsigned int family);
-int save_microcode_in_initrd_amd(unsigned int family);
void reload_ucode_amd(unsigned int cpu);
struct microcode_ops *init_amd_microcode(void);
void exit_amd_microcode(void);
#else /* CONFIG_CPU_SUP_AMD */
static inline void load_ucode_amd_bsp(struct early_load_data *ed, unsigned int family) { }
static inline void load_ucode_amd_ap(unsigned int family) { }
-static inline int save_microcode_in_initrd_amd(unsigned int family) { return -EINVAL; }
static inline void reload_ucode_amd(unsigned int cpu) { }
static inline struct microcode_ops *init_amd_microcode(void) { return NULL; }
static inline void exit_amd_microcode(void) { }
--
2.43.0
^ permalink raw reply related [flat|nested] 12+ messages in thread
* [PATCH 3/5] x86/microcode/AMD: Merge early_apply_microcode() into its single callsite
2025-02-11 16:36 [PATCH 0/5] x86/microcode/AMD: Some small cleanups Borislav Petkov
2025-02-11 16:36 ` [PATCH 1/5] x86/microcode/AMD: Remove ugly linebreak in __verify_patch_section() signature Borislav Petkov
2025-02-11 16:36 ` [PATCH 2/5] x86/microcode/AMD: Remove unused save_microcode_in_initrd_amd() declarations Borislav Petkov
@ 2025-02-11 16:36 ` Borislav Petkov
2025-02-17 8:51 ` [tip: x86/microcode] " tip-bot2 for Borislav Petkov (AMD)
2025-02-11 16:36 ` [PATCH 4/5] x86/microcode/AMD: Get rid of the _load_microcode_amd() forward declaration Borislav Petkov
` (2 subsequent siblings)
5 siblings, 1 reply; 12+ messages in thread
From: Borislav Petkov @ 2025-02-11 16:36 UTC (permalink / raw)
To: X86 ML; +Cc: LKML, Borislav Petkov (AMD)
From: "Borislav Petkov (AMD)" <bp@alien8.de>
No functional changes.
Signed-off-by: Borislav Petkov (AMD) <bp@alien8.de>
---
arch/x86/kernel/cpu/microcode/amd.c | 60 +++++++++++++----------------
1 file changed, 26 insertions(+), 34 deletions(-)
diff --git a/arch/x86/kernel/cpu/microcode/amd.c b/arch/x86/kernel/cpu/microcode/amd.c
index f831c0602994..90f93b3ca9db 100644
--- a/arch/x86/kernel/cpu/microcode/amd.c
+++ b/arch/x86/kernel/cpu/microcode/amd.c
@@ -512,39 +512,6 @@ static bool __apply_microcode_amd(struct microcode_amd *mc, unsigned int psize)
return true;
}
-/*
- * Early load occurs before we can vmalloc(). So we look for the microcode
- * patch container file in initrd, traverse equivalent cpu table, look for a
- * matching microcode patch, and update, all in initrd memory in place.
- * When vmalloc() is available for use later -- on 64-bit during first AP load,
- * and on 32-bit during save_microcode_in_initrd() -- we can call
- * load_microcode_amd() to save equivalent cpu table and microcode patches in
- * kernel heap memory.
- *
- * Returns true if container found (sets @desc), false otherwise.
- */
-static bool early_apply_microcode(u32 old_rev, void *ucode, size_t size)
-{
- struct cont_desc desc = { 0 };
- struct microcode_amd *mc;
-
- scan_containers(ucode, size, &desc);
-
- mc = desc.mc;
- if (!mc)
- return false;
-
- /*
- * Allow application of the same revision to pick up SMT-specific
- * changes even if the revision of the other SMT thread is already
- * up-to-date.
- */
- if (old_rev > mc->hdr.patch_id)
- return false;
-
- return __apply_microcode_amd(mc, desc.psize);
-}
-
static bool get_builtin_microcode(struct cpio_data *cp)
{
char fw_name[36] = "amd-ucode/microcode_amd.bin";
@@ -582,8 +549,19 @@ static bool __init find_blobs_in_containers(struct cpio_data *ret)
return found;
}
+/*
+ * Early load occurs before we can vmalloc(). So we look for the microcode
+ * patch container file in initrd, traverse equivalent cpu table, look for a
+ * matching microcode patch, and update, all in initrd memory in place.
+ * When vmalloc() is available for use later -- on 64-bit during first AP load,
+ * and on 32-bit during save_microcode_in_initrd() -- we can call
+ * load_microcode_amd() to save equivalent cpu table and microcode patches in
+ * kernel heap memory.
+ */
void __init load_ucode_amd_bsp(struct early_load_data *ed, unsigned int cpuid_1_eax)
{
+ struct cont_desc desc = { };
+ struct microcode_amd *mc;
struct cpio_data cp = { };
u32 dummy;
@@ -597,7 +575,21 @@ void __init load_ucode_amd_bsp(struct early_load_data *ed, unsigned int cpuid_1_
if (!find_blobs_in_containers(&cp))
return;
- if (early_apply_microcode(ed->old_rev, cp.data, cp.size))
+ scan_containers(cp.data, cp.size, &desc);
+
+ mc = desc.mc;
+ if (!mc)
+ return;
+
+ /*
+ * Allow application of the same revision to pick up SMT-specific
+ * changes even if the revision of the other SMT thread is already
+ * up-to-date.
+ */
+ if (ed->old_rev > mc->hdr.patch_id)
+ return;
+
+ if (__apply_microcode_amd(mc, desc.psize))
native_rdmsr(MSR_AMD64_PATCH_LEVEL, ed->new_rev, dummy);
}
--
2.43.0
^ permalink raw reply related [flat|nested] 12+ messages in thread
* [PATCH 4/5] x86/microcode/AMD: Get rid of the _load_microcode_amd() forward declaration
2025-02-11 16:36 [PATCH 0/5] x86/microcode/AMD: Some small cleanups Borislav Petkov
` (2 preceding siblings ...)
2025-02-11 16:36 ` [PATCH 3/5] x86/microcode/AMD: Merge early_apply_microcode() into its single callsite Borislav Petkov
@ 2025-02-11 16:36 ` Borislav Petkov
2025-02-17 8:51 ` [tip: x86/microcode] " tip-bot2 for Borislav Petkov (AMD)
2025-02-11 16:36 ` [PATCH 5/5] x86/microcode/AMD: Add get_patch_level() Borislav Petkov
2025-02-13 11:10 ` [PATCH 0/5] x86/microcode/AMD: Some small cleanups Thomas Gleixner
5 siblings, 1 reply; 12+ messages in thread
From: Borislav Petkov @ 2025-02-11 16:36 UTC (permalink / raw)
To: X86 ML; +Cc: LKML, Borislav Petkov (AMD)
From: "Borislav Petkov (AMD)" <bp@alien8.de>
Simply move save_microcode_in_initrd() down.
No functional changes.
Signed-off-by: Borislav Petkov (AMD) <bp@alien8.de>
---
arch/x86/kernel/cpu/microcode/amd.c | 54 ++++++++++++++---------------
1 file changed, 26 insertions(+), 28 deletions(-)
diff --git a/arch/x86/kernel/cpu/microcode/amd.c b/arch/x86/kernel/cpu/microcode/amd.c
index 90f93b3ca9db..adfea4d0d129 100644
--- a/arch/x86/kernel/cpu/microcode/amd.c
+++ b/arch/x86/kernel/cpu/microcode/amd.c
@@ -593,34 +593,6 @@ void __init load_ucode_amd_bsp(struct early_load_data *ed, unsigned int cpuid_1_
native_rdmsr(MSR_AMD64_PATCH_LEVEL, ed->new_rev, dummy);
}
-static enum ucode_state _load_microcode_amd(u8 family, const u8 *data, size_t size);
-
-static int __init save_microcode_in_initrd(void)
-{
- unsigned int cpuid_1_eax = native_cpuid_eax(1);
- struct cpuinfo_x86 *c = &boot_cpu_data;
- struct cont_desc desc = { 0 };
- enum ucode_state ret;
- struct cpio_data cp;
-
- if (dis_ucode_ldr || c->x86_vendor != X86_VENDOR_AMD || c->x86 < 0x10)
- return 0;
-
- if (!find_blobs_in_containers(&cp))
- return -EINVAL;
-
- scan_containers(cp.data, cp.size, &desc);
- if (!desc.mc)
- return -EINVAL;
-
- ret = _load_microcode_amd(x86_family(cpuid_1_eax), desc.data, desc.size);
- if (ret > UCODE_UPDATED)
- return -EINVAL;
-
- return 0;
-}
-early_initcall(save_microcode_in_initrd);
-
static inline bool patch_cpus_equivalent(struct ucode_patch *p,
struct ucode_patch *n,
bool ignore_stepping)
@@ -1004,6 +976,32 @@ static enum ucode_state load_microcode_amd(u8 family, const u8 *data, size_t siz
return ret;
}
+static int __init save_microcode_in_initrd(void)
+{
+ unsigned int cpuid_1_eax = native_cpuid_eax(1);
+ struct cpuinfo_x86 *c = &boot_cpu_data;
+ struct cont_desc desc = { 0 };
+ enum ucode_state ret;
+ struct cpio_data cp;
+
+ if (dis_ucode_ldr || c->x86_vendor != X86_VENDOR_AMD || c->x86 < 0x10)
+ return 0;
+
+ if (!find_blobs_in_containers(&cp))
+ return -EINVAL;
+
+ scan_containers(cp.data, cp.size, &desc);
+ if (!desc.mc)
+ return -EINVAL;
+
+ ret = _load_microcode_amd(x86_family(cpuid_1_eax), desc.data, desc.size);
+ if (ret > UCODE_UPDATED)
+ return -EINVAL;
+
+ return 0;
+}
+early_initcall(save_microcode_in_initrd);
+
/*
* AMD microcode firmware naming convention, up to family 15h they are in
* the legacy file:
--
2.43.0
^ permalink raw reply related [flat|nested] 12+ messages in thread
* [PATCH 5/5] x86/microcode/AMD: Add get_patch_level()
2025-02-11 16:36 [PATCH 0/5] x86/microcode/AMD: Some small cleanups Borislav Petkov
` (3 preceding siblings ...)
2025-02-11 16:36 ` [PATCH 4/5] x86/microcode/AMD: Get rid of the _load_microcode_amd() forward declaration Borislav Petkov
@ 2025-02-11 16:36 ` Borislav Petkov
2025-02-17 8:51 ` [tip: x86/microcode] " tip-bot2 for Borislav Petkov (AMD)
2025-02-13 11:10 ` [PATCH 0/5] x86/microcode/AMD: Some small cleanups Thomas Gleixner
5 siblings, 1 reply; 12+ messages in thread
From: Borislav Petkov @ 2025-02-11 16:36 UTC (permalink / raw)
To: X86 ML; +Cc: LKML, Borislav Petkov (AMD)
From: "Borislav Petkov (AMD)" <bp@alien8.de>
Put the MSR_AMD64_PATCH_LEVEL reading of the current microcode revision
the hw has, into a separate function.
Signed-off-by: Borislav Petkov (AMD) <bp@alien8.de>
---
arch/x86/kernel/cpu/microcode/amd.c | 46 +++++++++++++++--------------
1 file changed, 24 insertions(+), 22 deletions(-)
diff --git a/arch/x86/kernel/cpu/microcode/amd.c b/arch/x86/kernel/cpu/microcode/amd.c
index adfea4d0d129..31f90e129b08 100644
--- a/arch/x86/kernel/cpu/microcode/amd.c
+++ b/arch/x86/kernel/cpu/microcode/amd.c
@@ -145,6 +145,15 @@ ucode_path[] __maybe_unused = "kernel/x86/microcode/AuthenticAMD.bin";
*/
static u32 bsp_cpuid_1_eax __ro_after_init;
+static u32 get_patch_level(void)
+{
+ u32 rev, dummy __always_unused;
+
+ native_rdmsr(MSR_AMD64_PATCH_LEVEL, rev, dummy);
+
+ return rev;
+}
+
static union cpuid_1_eax ucode_rev_to_cpuid(unsigned int val)
{
union zen_patch_rev p;
@@ -483,10 +492,10 @@ static void scan_containers(u8 *ucode, size_t size, struct cont_desc *desc)
}
}
-static bool __apply_microcode_amd(struct microcode_amd *mc, unsigned int psize)
+static bool __apply_microcode_amd(struct microcode_amd *mc, u32 *cur_rev,
+ unsigned int psize)
{
unsigned long p_addr = (unsigned long)&mc->hdr.data_code;
- u32 rev, dummy;
native_wrmsrl(MSR_AMD64_PATCH_LOADER, p_addr);
@@ -504,9 +513,8 @@ static bool __apply_microcode_amd(struct microcode_amd *mc, unsigned int psize)
}
/* verify patch application was successful */
- native_rdmsr(MSR_AMD64_PATCH_LEVEL, rev, dummy);
-
- if (rev != mc->hdr.patch_id)
+ *cur_rev = get_patch_level();
+ if (*cur_rev != mc->hdr.patch_id)
return false;
return true;
@@ -563,11 +571,12 @@ void __init load_ucode_amd_bsp(struct early_load_data *ed, unsigned int cpuid_1_
struct cont_desc desc = { };
struct microcode_amd *mc;
struct cpio_data cp = { };
- u32 dummy;
+ u32 rev;
bsp_cpuid_1_eax = cpuid_1_eax;
- native_rdmsr(MSR_AMD64_PATCH_LEVEL, ed->old_rev, dummy);
+ rev = get_patch_level();
+ ed->old_rev = rev;
/* Needed in load_microcode_amd() */
ucode_cpu_info[0].cpu_sig.sig = cpuid_1_eax;
@@ -589,8 +598,8 @@ void __init load_ucode_amd_bsp(struct early_load_data *ed, unsigned int cpuid_1_
if (ed->old_rev > mc->hdr.patch_id)
return;
- if (__apply_microcode_amd(mc, desc.psize))
- native_rdmsr(MSR_AMD64_PATCH_LEVEL, ed->new_rev, dummy);
+ if (__apply_microcode_amd(mc, &rev, desc.psize))
+ ed->new_rev = rev;
}
static inline bool patch_cpus_equivalent(struct ucode_patch *p,
@@ -692,14 +701,9 @@ static void free_cache(void)
static struct ucode_patch *find_patch(unsigned int cpu)
{
struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
- u32 rev, dummy __always_unused;
u16 equiv_id = 0;
- /* fetch rev if not populated yet: */
- if (!uci->cpu_sig.rev) {
- rdmsr(MSR_AMD64_PATCH_LEVEL, rev, dummy);
- uci->cpu_sig.rev = rev;
- }
+ uci->cpu_sig.rev = get_patch_level();
if (x86_family(bsp_cpuid_1_eax) < 0x17) {
equiv_id = find_equiv_id(&equiv_table, uci->cpu_sig.sig);
@@ -722,22 +726,20 @@ void reload_ucode_amd(unsigned int cpu)
mc = p->data;
- rdmsr(MSR_AMD64_PATCH_LEVEL, rev, dummy);
-
+ rev = get_patch_level();
if (rev < mc->hdr.patch_id) {
- if (__apply_microcode_amd(mc, p->size))
- pr_info_once("reload revision: 0x%08x\n", mc->hdr.patch_id);
+ if (__apply_microcode_amd(mc, &rev, p->size))
+ pr_info_once("reload revision: 0x%08x\n", rev);
}
}
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;
struct ucode_patch *p;
csig->sig = cpuid_eax(0x00000001);
- csig->rev = c->microcode;
+ csig->rev = get_patch_level();
/*
* a patch could have been loaded early, set uci->mc so that
@@ -778,7 +780,7 @@ static enum ucode_state apply_microcode_amd(int cpu)
goto out;
}
- if (!__apply_microcode_amd(mc_amd, p->size)) {
+ if (!__apply_microcode_amd(mc_amd, &rev, p->size)) {
pr_err("CPU%d: update failed for patch_level=0x%08x\n",
cpu, mc_amd->hdr.patch_id);
return UCODE_ERROR;
--
2.43.0
^ permalink raw reply related [flat|nested] 12+ messages in thread
* Re: [PATCH 0/5] x86/microcode/AMD: Some small cleanups
2025-02-11 16:36 [PATCH 0/5] x86/microcode/AMD: Some small cleanups Borislav Petkov
` (4 preceding siblings ...)
2025-02-11 16:36 ` [PATCH 5/5] x86/microcode/AMD: Add get_patch_level() Borislav Petkov
@ 2025-02-13 11:10 ` Thomas Gleixner
5 siblings, 0 replies; 12+ messages in thread
From: Thomas Gleixner @ 2025-02-13 11:10 UTC (permalink / raw)
To: Borislav Petkov, X86 ML; +Cc: LKML, Borislav Petkov (AMD)
On Tue, Feb 11 2025 at 17:36, Borislav Petkov wrote:
> From: "Borislav Petkov (AMD)" <bp@alien8.de>
>
> A bunch of small and trivial cleanups which sprang up from recent staring at
> the loader.
>
> No functional changes.
Reviewed-by: Thomas Gleixner <tglx@linutronix.de>
^ permalink raw reply [flat|nested] 12+ messages in thread
* [tip: x86/microcode] x86/microcode/AMD: Add get_patch_level()
2025-02-11 16:36 ` [PATCH 5/5] x86/microcode/AMD: Add get_patch_level() Borislav Petkov
@ 2025-02-17 8:51 ` tip-bot2 for Borislav Petkov (AMD)
0 siblings, 0 replies; 12+ messages in thread
From: tip-bot2 for Borislav Petkov (AMD) @ 2025-02-17 8:51 UTC (permalink / raw)
To: linux-tip-commits
Cc: Borislav Petkov (AMD), Thomas Gleixner, x86, linux-kernel
The following commit has been merged into the x86/microcode branch of tip:
Commit-ID: 037e81fb9d2dfe7b31fd97e5f578854e38f09887
Gitweb: https://git.kernel.org/tip/037e81fb9d2dfe7b31fd97e5f578854e38f09887
Author: Borislav Petkov (AMD) <bp@alien8.de>
AuthorDate: Thu, 23 Jan 2025 13:02:32 +01:00
Committer: Borislav Petkov (AMD) <bp@alien8.de>
CommitterDate: Mon, 17 Feb 2025 09:42:40 +01:00
x86/microcode/AMD: Add get_patch_level()
Put the MSR_AMD64_PATCH_LEVEL reading of the current microcode revision
the hw has, into a separate function.
Signed-off-by: Borislav Petkov (AMD) <bp@alien8.de>
Reviewed-by: Thomas Gleixner <tglx@linutronix.de>
Link: https://lore.kernel.org/r/20250211163648.30531-6-bp@kernel.org
---
arch/x86/kernel/cpu/microcode/amd.c | 46 ++++++++++++++--------------
1 file changed, 24 insertions(+), 22 deletions(-)
diff --git a/arch/x86/kernel/cpu/microcode/amd.c b/arch/x86/kernel/cpu/microcode/amd.c
index adfea4d..31f90e1 100644
--- a/arch/x86/kernel/cpu/microcode/amd.c
+++ b/arch/x86/kernel/cpu/microcode/amd.c
@@ -145,6 +145,15 @@ ucode_path[] __maybe_unused = "kernel/x86/microcode/AuthenticAMD.bin";
*/
static u32 bsp_cpuid_1_eax __ro_after_init;
+static u32 get_patch_level(void)
+{
+ u32 rev, dummy __always_unused;
+
+ native_rdmsr(MSR_AMD64_PATCH_LEVEL, rev, dummy);
+
+ return rev;
+}
+
static union cpuid_1_eax ucode_rev_to_cpuid(unsigned int val)
{
union zen_patch_rev p;
@@ -483,10 +492,10 @@ static void scan_containers(u8 *ucode, size_t size, struct cont_desc *desc)
}
}
-static bool __apply_microcode_amd(struct microcode_amd *mc, unsigned int psize)
+static bool __apply_microcode_amd(struct microcode_amd *mc, u32 *cur_rev,
+ unsigned int psize)
{
unsigned long p_addr = (unsigned long)&mc->hdr.data_code;
- u32 rev, dummy;
native_wrmsrl(MSR_AMD64_PATCH_LOADER, p_addr);
@@ -504,9 +513,8 @@ static bool __apply_microcode_amd(struct microcode_amd *mc, unsigned int psize)
}
/* verify patch application was successful */
- native_rdmsr(MSR_AMD64_PATCH_LEVEL, rev, dummy);
-
- if (rev != mc->hdr.patch_id)
+ *cur_rev = get_patch_level();
+ if (*cur_rev != mc->hdr.patch_id)
return false;
return true;
@@ -563,11 +571,12 @@ void __init load_ucode_amd_bsp(struct early_load_data *ed, unsigned int cpuid_1_
struct cont_desc desc = { };
struct microcode_amd *mc;
struct cpio_data cp = { };
- u32 dummy;
+ u32 rev;
bsp_cpuid_1_eax = cpuid_1_eax;
- native_rdmsr(MSR_AMD64_PATCH_LEVEL, ed->old_rev, dummy);
+ rev = get_patch_level();
+ ed->old_rev = rev;
/* Needed in load_microcode_amd() */
ucode_cpu_info[0].cpu_sig.sig = cpuid_1_eax;
@@ -589,8 +598,8 @@ void __init load_ucode_amd_bsp(struct early_load_data *ed, unsigned int cpuid_1_
if (ed->old_rev > mc->hdr.patch_id)
return;
- if (__apply_microcode_amd(mc, desc.psize))
- native_rdmsr(MSR_AMD64_PATCH_LEVEL, ed->new_rev, dummy);
+ if (__apply_microcode_amd(mc, &rev, desc.psize))
+ ed->new_rev = rev;
}
static inline bool patch_cpus_equivalent(struct ucode_patch *p,
@@ -692,14 +701,9 @@ static void free_cache(void)
static struct ucode_patch *find_patch(unsigned int cpu)
{
struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
- u32 rev, dummy __always_unused;
u16 equiv_id = 0;
- /* fetch rev if not populated yet: */
- if (!uci->cpu_sig.rev) {
- rdmsr(MSR_AMD64_PATCH_LEVEL, rev, dummy);
- uci->cpu_sig.rev = rev;
- }
+ uci->cpu_sig.rev = get_patch_level();
if (x86_family(bsp_cpuid_1_eax) < 0x17) {
equiv_id = find_equiv_id(&equiv_table, uci->cpu_sig.sig);
@@ -722,22 +726,20 @@ void reload_ucode_amd(unsigned int cpu)
mc = p->data;
- rdmsr(MSR_AMD64_PATCH_LEVEL, rev, dummy);
-
+ rev = get_patch_level();
if (rev < mc->hdr.patch_id) {
- if (__apply_microcode_amd(mc, p->size))
- pr_info_once("reload revision: 0x%08x\n", mc->hdr.patch_id);
+ if (__apply_microcode_amd(mc, &rev, p->size))
+ pr_info_once("reload revision: 0x%08x\n", rev);
}
}
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;
struct ucode_patch *p;
csig->sig = cpuid_eax(0x00000001);
- csig->rev = c->microcode;
+ csig->rev = get_patch_level();
/*
* a patch could have been loaded early, set uci->mc so that
@@ -778,7 +780,7 @@ static enum ucode_state apply_microcode_amd(int cpu)
goto out;
}
- if (!__apply_microcode_amd(mc_amd, p->size)) {
+ if (!__apply_microcode_amd(mc_amd, &rev, p->size)) {
pr_err("CPU%d: update failed for patch_level=0x%08x\n",
cpu, mc_amd->hdr.patch_id);
return UCODE_ERROR;
^ permalink raw reply related [flat|nested] 12+ messages in thread
* [tip: x86/microcode] x86/microcode/AMD: Get rid of the _load_microcode_amd() forward declaration
2025-02-11 16:36 ` [PATCH 4/5] x86/microcode/AMD: Get rid of the _load_microcode_amd() forward declaration Borislav Petkov
@ 2025-02-17 8:51 ` tip-bot2 for Borislav Petkov (AMD)
0 siblings, 0 replies; 12+ messages in thread
From: tip-bot2 for Borislav Petkov (AMD) @ 2025-02-17 8:51 UTC (permalink / raw)
To: linux-tip-commits
Cc: Borislav Petkov (AMD), Thomas Gleixner, x86, linux-kernel
The following commit has been merged into the x86/microcode branch of tip:
Commit-ID: b39c387164879eef71886fc93cee5ca7dd7bf500
Gitweb: https://git.kernel.org/tip/b39c387164879eef71886fc93cee5ca7dd7bf500
Author: Borislav Petkov (AMD) <bp@alien8.de>
AuthorDate: Thu, 23 Jan 2025 12:51:37 +01:00
Committer: Borislav Petkov (AMD) <bp@alien8.de>
CommitterDate: Mon, 17 Feb 2025 09:42:37 +01:00
x86/microcode/AMD: Get rid of the _load_microcode_amd() forward declaration
Simply move save_microcode_in_initrd() down.
No functional changes.
Signed-off-by: Borislav Petkov (AMD) <bp@alien8.de>
Reviewed-by: Thomas Gleixner <tglx@linutronix.de>
Link: https://lore.kernel.org/r/20250211163648.30531-5-bp@kernel.org
---
arch/x86/kernel/cpu/microcode/amd.c | 54 +++++++++++++---------------
1 file changed, 26 insertions(+), 28 deletions(-)
diff --git a/arch/x86/kernel/cpu/microcode/amd.c b/arch/x86/kernel/cpu/microcode/amd.c
index 90f93b3..adfea4d 100644
--- a/arch/x86/kernel/cpu/microcode/amd.c
+++ b/arch/x86/kernel/cpu/microcode/amd.c
@@ -593,34 +593,6 @@ void __init load_ucode_amd_bsp(struct early_load_data *ed, unsigned int cpuid_1_
native_rdmsr(MSR_AMD64_PATCH_LEVEL, ed->new_rev, dummy);
}
-static enum ucode_state _load_microcode_amd(u8 family, const u8 *data, size_t size);
-
-static int __init save_microcode_in_initrd(void)
-{
- unsigned int cpuid_1_eax = native_cpuid_eax(1);
- struct cpuinfo_x86 *c = &boot_cpu_data;
- struct cont_desc desc = { 0 };
- enum ucode_state ret;
- struct cpio_data cp;
-
- if (dis_ucode_ldr || c->x86_vendor != X86_VENDOR_AMD || c->x86 < 0x10)
- return 0;
-
- if (!find_blobs_in_containers(&cp))
- return -EINVAL;
-
- scan_containers(cp.data, cp.size, &desc);
- if (!desc.mc)
- return -EINVAL;
-
- ret = _load_microcode_amd(x86_family(cpuid_1_eax), desc.data, desc.size);
- if (ret > UCODE_UPDATED)
- return -EINVAL;
-
- return 0;
-}
-early_initcall(save_microcode_in_initrd);
-
static inline bool patch_cpus_equivalent(struct ucode_patch *p,
struct ucode_patch *n,
bool ignore_stepping)
@@ -1004,6 +976,32 @@ static enum ucode_state load_microcode_amd(u8 family, const u8 *data, size_t siz
return ret;
}
+static int __init save_microcode_in_initrd(void)
+{
+ unsigned int cpuid_1_eax = native_cpuid_eax(1);
+ struct cpuinfo_x86 *c = &boot_cpu_data;
+ struct cont_desc desc = { 0 };
+ enum ucode_state ret;
+ struct cpio_data cp;
+
+ if (dis_ucode_ldr || c->x86_vendor != X86_VENDOR_AMD || c->x86 < 0x10)
+ return 0;
+
+ if (!find_blobs_in_containers(&cp))
+ return -EINVAL;
+
+ scan_containers(cp.data, cp.size, &desc);
+ if (!desc.mc)
+ return -EINVAL;
+
+ ret = _load_microcode_amd(x86_family(cpuid_1_eax), desc.data, desc.size);
+ if (ret > UCODE_UPDATED)
+ return -EINVAL;
+
+ return 0;
+}
+early_initcall(save_microcode_in_initrd);
+
/*
* AMD microcode firmware naming convention, up to family 15h they are in
* the legacy file:
^ permalink raw reply related [flat|nested] 12+ messages in thread
* [tip: x86/microcode] x86/microcode/AMD: Merge early_apply_microcode() into its single callsite
2025-02-11 16:36 ` [PATCH 3/5] x86/microcode/AMD: Merge early_apply_microcode() into its single callsite Borislav Petkov
@ 2025-02-17 8:51 ` tip-bot2 for Borislav Petkov (AMD)
0 siblings, 0 replies; 12+ messages in thread
From: tip-bot2 for Borislav Petkov (AMD) @ 2025-02-17 8:51 UTC (permalink / raw)
To: linux-tip-commits
Cc: Borislav Petkov (AMD), Thomas Gleixner, x86, linux-kernel
The following commit has been merged into the x86/microcode branch of tip:
Commit-ID: dc15675074dcfd79a2f10a6e39f96b0244961a01
Gitweb: https://git.kernel.org/tip/dc15675074dcfd79a2f10a6e39f96b0244961a01
Author: Borislav Petkov (AMD) <bp@alien8.de>
AuthorDate: Thu, 23 Jan 2025 12:46:45 +01:00
Committer: Borislav Petkov (AMD) <bp@alien8.de>
CommitterDate: Mon, 17 Feb 2025 09:42:34 +01:00
x86/microcode/AMD: Merge early_apply_microcode() into its single callsite
No functional changes.
Signed-off-by: Borislav Petkov (AMD) <bp@alien8.de>
Reviewed-by: Thomas Gleixner <tglx@linutronix.de>
Link: https://lore.kernel.org/r/20250211163648.30531-4-bp@kernel.org
---
arch/x86/kernel/cpu/microcode/amd.c | 60 ++++++++++++----------------
1 file changed, 26 insertions(+), 34 deletions(-)
diff --git a/arch/x86/kernel/cpu/microcode/amd.c b/arch/x86/kernel/cpu/microcode/amd.c
index f831c06..90f93b3 100644
--- a/arch/x86/kernel/cpu/microcode/amd.c
+++ b/arch/x86/kernel/cpu/microcode/amd.c
@@ -512,39 +512,6 @@ static bool __apply_microcode_amd(struct microcode_amd *mc, unsigned int psize)
return true;
}
-/*
- * Early load occurs before we can vmalloc(). So we look for the microcode
- * patch container file in initrd, traverse equivalent cpu table, look for a
- * matching microcode patch, and update, all in initrd memory in place.
- * When vmalloc() is available for use later -- on 64-bit during first AP load,
- * and on 32-bit during save_microcode_in_initrd() -- we can call
- * load_microcode_amd() to save equivalent cpu table and microcode patches in
- * kernel heap memory.
- *
- * Returns true if container found (sets @desc), false otherwise.
- */
-static bool early_apply_microcode(u32 old_rev, void *ucode, size_t size)
-{
- struct cont_desc desc = { 0 };
- struct microcode_amd *mc;
-
- scan_containers(ucode, size, &desc);
-
- mc = desc.mc;
- if (!mc)
- return false;
-
- /*
- * Allow application of the same revision to pick up SMT-specific
- * changes even if the revision of the other SMT thread is already
- * up-to-date.
- */
- if (old_rev > mc->hdr.patch_id)
- return false;
-
- return __apply_microcode_amd(mc, desc.psize);
-}
-
static bool get_builtin_microcode(struct cpio_data *cp)
{
char fw_name[36] = "amd-ucode/microcode_amd.bin";
@@ -582,8 +549,19 @@ static bool __init find_blobs_in_containers(struct cpio_data *ret)
return found;
}
+/*
+ * Early load occurs before we can vmalloc(). So we look for the microcode
+ * patch container file in initrd, traverse equivalent cpu table, look for a
+ * matching microcode patch, and update, all in initrd memory in place.
+ * When vmalloc() is available for use later -- on 64-bit during first AP load,
+ * and on 32-bit during save_microcode_in_initrd() -- we can call
+ * load_microcode_amd() to save equivalent cpu table and microcode patches in
+ * kernel heap memory.
+ */
void __init load_ucode_amd_bsp(struct early_load_data *ed, unsigned int cpuid_1_eax)
{
+ struct cont_desc desc = { };
+ struct microcode_amd *mc;
struct cpio_data cp = { };
u32 dummy;
@@ -597,7 +575,21 @@ void __init load_ucode_amd_bsp(struct early_load_data *ed, unsigned int cpuid_1_
if (!find_blobs_in_containers(&cp))
return;
- if (early_apply_microcode(ed->old_rev, cp.data, cp.size))
+ scan_containers(cp.data, cp.size, &desc);
+
+ mc = desc.mc;
+ if (!mc)
+ return;
+
+ /*
+ * Allow application of the same revision to pick up SMT-specific
+ * changes even if the revision of the other SMT thread is already
+ * up-to-date.
+ */
+ if (ed->old_rev > mc->hdr.patch_id)
+ return;
+
+ if (__apply_microcode_amd(mc, desc.psize))
native_rdmsr(MSR_AMD64_PATCH_LEVEL, ed->new_rev, dummy);
}
^ permalink raw reply related [flat|nested] 12+ messages in thread
* [tip: x86/microcode] x86/microcode/AMD: Remove ugly linebreak in __verify_patch_section() signature
2025-02-11 16:36 ` [PATCH 1/5] x86/microcode/AMD: Remove ugly linebreak in __verify_patch_section() signature Borislav Petkov
@ 2025-02-17 8:51 ` tip-bot2 for Borislav Petkov (AMD)
0 siblings, 0 replies; 12+ messages in thread
From: tip-bot2 for Borislav Petkov (AMD) @ 2025-02-17 8:51 UTC (permalink / raw)
To: linux-tip-commits
Cc: Borislav Petkov (AMD), Thomas Gleixner, x86, linux-kernel
The following commit has been merged into the x86/microcode branch of tip:
Commit-ID: 7103f0589ac220eac3d2b1e8411494b31b883d06
Gitweb: https://git.kernel.org/tip/7103f0589ac220eac3d2b1e8411494b31b883d06
Author: Borislav Petkov (AMD) <bp@alien8.de>
AuthorDate: Thu, 23 Jan 2025 13:14:34 +01:00
Committer: Borislav Petkov (AMD) <bp@alien8.de>
CommitterDate: Mon, 17 Feb 2025 09:42:13 +01:00
x86/microcode/AMD: Remove ugly linebreak in __verify_patch_section() signature
No functional changes.
Signed-off-by: Borislav Petkov (AMD) <bp@alien8.de>
Reviewed-by: Thomas Gleixner <tglx@linutronix.de>
Link: https://lore.kernel.org/r/20250211163648.30531-2-bp@kernel.org
---
arch/x86/kernel/cpu/microcode/amd.c | 3 +--
1 file changed, 1 insertion(+), 2 deletions(-)
diff --git a/arch/x86/kernel/cpu/microcode/amd.c b/arch/x86/kernel/cpu/microcode/amd.c
index a5dac7f..4a62625 100644
--- a/arch/x86/kernel/cpu/microcode/amd.c
+++ b/arch/x86/kernel/cpu/microcode/amd.c
@@ -246,8 +246,7 @@ static bool verify_equivalence_table(const u8 *buf, size_t buf_size)
* On success, @sh_psize returns the patch size according to the section header,
* to the caller.
*/
-static bool
-__verify_patch_section(const u8 *buf, size_t buf_size, u32 *sh_psize)
+static bool __verify_patch_section(const u8 *buf, size_t buf_size, u32 *sh_psize)
{
u32 p_type, p_size;
const u32 *hdr;
^ permalink raw reply related [flat|nested] 12+ messages in thread
* [tip: x86/microcode] x86/microcode/AMD: Remove unused save_microcode_in_initrd_amd() declarations
2025-02-11 16:36 ` [PATCH 2/5] x86/microcode/AMD: Remove unused save_microcode_in_initrd_amd() declarations Borislav Petkov
@ 2025-02-17 8:51 ` tip-bot2 for Borislav Petkov (AMD)
0 siblings, 0 replies; 12+ messages in thread
From: tip-bot2 for Borislav Petkov (AMD) @ 2025-02-17 8:51 UTC (permalink / raw)
To: linux-tip-commits
Cc: Borislav Petkov (AMD), Thomas Gleixner, x86, linux-kernel
The following commit has been merged into the x86/microcode branch of tip:
Commit-ID: 3ef0740d10b005a45e8ae5b4b7b5d37bfddf63c0
Gitweb: https://git.kernel.org/tip/3ef0740d10b005a45e8ae5b4b7b5d37bfddf63c0
Author: Borislav Petkov (AMD) <bp@alien8.de>
AuthorDate: Thu, 23 Jan 2025 12:23:47 +01:00
Committer: Borislav Petkov (AMD) <bp@alien8.de>
CommitterDate: Mon, 17 Feb 2025 09:42:31 +01:00
x86/microcode/AMD: Remove unused save_microcode_in_initrd_amd() declarations
Commit
a7939f016720 ("x86/microcode/amd: Cache builtin/initrd microcode early")
renamed it to save_microcode_in_initrd() and made it static. Zap the
forgotten declarations.
No functional changes.
Signed-off-by: Borislav Petkov (AMD) <bp@alien8.de>
Reviewed-by: Thomas Gleixner <tglx@linutronix.de>
Link: https://lore.kernel.org/r/20250211163648.30531-3-bp@kernel.org
---
arch/x86/kernel/cpu/microcode/amd.c | 2 +-
arch/x86/kernel/cpu/microcode/internal.h | 2 --
2 files changed, 1 insertion(+), 3 deletions(-)
diff --git a/arch/x86/kernel/cpu/microcode/amd.c b/arch/x86/kernel/cpu/microcode/amd.c
index 4a62625..f831c06 100644
--- a/arch/x86/kernel/cpu/microcode/amd.c
+++ b/arch/x86/kernel/cpu/microcode/amd.c
@@ -517,7 +517,7 @@ static bool __apply_microcode_amd(struct microcode_amd *mc, unsigned int psize)
* patch container file in initrd, traverse equivalent cpu table, look for a
* matching microcode patch, and update, all in initrd memory in place.
* When vmalloc() is available for use later -- on 64-bit during first AP load,
- * and on 32-bit during save_microcode_in_initrd_amd() -- we can call
+ * and on 32-bit during save_microcode_in_initrd() -- we can call
* load_microcode_amd() to save equivalent cpu table and microcode patches in
* kernel heap memory.
*
diff --git a/arch/x86/kernel/cpu/microcode/internal.h b/arch/x86/kernel/cpu/microcode/internal.h
index 21776c5..5df6217 100644
--- a/arch/x86/kernel/cpu/microcode/internal.h
+++ b/arch/x86/kernel/cpu/microcode/internal.h
@@ -100,14 +100,12 @@ extern bool force_minrev;
#ifdef CONFIG_CPU_SUP_AMD
void load_ucode_amd_bsp(struct early_load_data *ed, unsigned int family);
void load_ucode_amd_ap(unsigned int family);
-int save_microcode_in_initrd_amd(unsigned int family);
void reload_ucode_amd(unsigned int cpu);
struct microcode_ops *init_amd_microcode(void);
void exit_amd_microcode(void);
#else /* CONFIG_CPU_SUP_AMD */
static inline void load_ucode_amd_bsp(struct early_load_data *ed, unsigned int family) { }
static inline void load_ucode_amd_ap(unsigned int family) { }
-static inline int save_microcode_in_initrd_amd(unsigned int family) { return -EINVAL; }
static inline void reload_ucode_amd(unsigned int cpu) { }
static inline struct microcode_ops *init_amd_microcode(void) { return NULL; }
static inline void exit_amd_microcode(void) { }
^ permalink raw reply related [flat|nested] 12+ messages in thread
end of thread, other threads:[~2025-02-17 8:51 UTC | newest]
Thread overview: 12+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-02-11 16:36 [PATCH 0/5] x86/microcode/AMD: Some small cleanups Borislav Petkov
2025-02-11 16:36 ` [PATCH 1/5] x86/microcode/AMD: Remove ugly linebreak in __verify_patch_section() signature Borislav Petkov
2025-02-17 8:51 ` [tip: x86/microcode] " tip-bot2 for Borislav Petkov (AMD)
2025-02-11 16:36 ` [PATCH 2/5] x86/microcode/AMD: Remove unused save_microcode_in_initrd_amd() declarations Borislav Petkov
2025-02-17 8:51 ` [tip: x86/microcode] " tip-bot2 for Borislav Petkov (AMD)
2025-02-11 16:36 ` [PATCH 3/5] x86/microcode/AMD: Merge early_apply_microcode() into its single callsite Borislav Petkov
2025-02-17 8:51 ` [tip: x86/microcode] " tip-bot2 for Borislav Petkov (AMD)
2025-02-11 16:36 ` [PATCH 4/5] x86/microcode/AMD: Get rid of the _load_microcode_amd() forward declaration Borislav Petkov
2025-02-17 8:51 ` [tip: x86/microcode] " tip-bot2 for Borislav Petkov (AMD)
2025-02-11 16:36 ` [PATCH 5/5] x86/microcode/AMD: Add get_patch_level() Borislav Petkov
2025-02-17 8:51 ` [tip: x86/microcode] " tip-bot2 for Borislav Petkov (AMD)
2025-02-13 11:10 ` [PATCH 0/5] x86/microcode/AMD: Some small cleanups Thomas Gleixner
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).