[PATCH 12/13] x86/mce: Warn of a microcode update is in progress when MCE arrives

From: Ashok Raj
Date: Fri Oct 14 2022 - 16:10:30 EST


Due to the nature of microcode updates to long flow instructions, its
possible if an MCE is taken when microcode update is in progress could be
dangerous. There is nothing the kernel can do to mitigate safely.

Drop some bread crumbs to note that a MCE happened while a microcode update
is also in progress.

Suggested-by: Boris Petkov <bp@xxxxxxxxx>
Signed-off-by: Ashok Raj <ashok.raj@xxxxxxxxx>
---
arch/x86/include/asm/microcode.h | 1 +
arch/x86/kernel/cpu/mce/core.c | 5 +++++
arch/x86/kernel/cpu/microcode/core.c | 4 ++++
3 files changed, 10 insertions(+)

diff --git a/arch/x86/include/asm/microcode.h b/arch/x86/include/asm/microcode.h
index 0c0bbc26560f..38b501d842de 100644
--- a/arch/x86/include/asm/microcode.h
+++ b/arch/x86/include/asm/microcode.h
@@ -41,6 +41,7 @@ struct ucode_patch {
};

extern struct list_head microcode_cache;
+extern int ucode_updating;

struct cpu_signature {
unsigned int sig;
diff --git a/arch/x86/kernel/cpu/mce/core.c b/arch/x86/kernel/cpu/mce/core.c
index 2c8ec5c71712..1f05aec9880f 100644
--- a/arch/x86/kernel/cpu/mce/core.c
+++ b/arch/x86/kernel/cpu/mce/core.c
@@ -46,6 +46,7 @@
#include <linux/hardirq.h>

#include <asm/intel-family.h>
+#include <asm/microcode.h>
#include <asm/processor.h>
#include <asm/traps.h>
#include <asm/tlbflush.h>
@@ -1425,6 +1426,10 @@ noinstr void do_machine_check(struct pt_regs *regs)
else if (unlikely(!mca_cfg.initialized))
return unexpected_machine_check(regs);

+ instrumentation_begin();
+ if (unlikely(ucode_updating))
+ pr_warn("MCE triggered while microcode update is in progress\n");
+ instrumentation_end();
if (mce_flags.skx_repmov_quirk && quirk_skylake_repmov())
goto clear;

diff --git a/arch/x86/kernel/cpu/microcode/core.c b/arch/x86/kernel/cpu/microcode/core.c
index c6cd815190b1..eb2caa74de01 100644
--- a/arch/x86/kernel/cpu/microcode/core.c
+++ b/arch/x86/kernel/cpu/microcode/core.c
@@ -49,6 +49,8 @@ static bool dis_ucode_ldr = true;

bool initrd_gone;

+int ucode_updating;
+
LIST_HEAD(microcode_cache);

/*
@@ -586,7 +588,9 @@ static int microcode_reload_late(void)
goto done;
}

+ ucode_updating = 1;
ret = stop_machine_cpuslocked(__reload_late, NULL, cpu_online_mask);
+ ucode_updating = 0;
if (ret == 0)
microcode_check();

--
2.34.1