Skip to contentSkip to conversation

x86/mce, KVM: X86: KVM memory poison and MCE injector support

PATCH In review [email protected] 3 participants 15 messages Oct 10, 2023 – Oct 20, 2023

Subsystems (8)
Memory management x86 VFS KVM +4 more subsystems
lore
Share & tools
lore
#0 lore raw

cover x86/mce, KVM: X86: KVM memory poison and MCE injector support

From: Isaku Yamahata <[email protected]>

Background
==========
The TDX is a VM-based confidential computing technology. It encrypts guest memory to protect it from software (host OS, VMM, firmware, etc. ...) outside of the TCB. Software in the host can write to the protected guest memory to corrupt, and the protected guest can consume corrupted memory. TDX uses machine checks to notify the TDX vcpu consumption of corrupted memory.

For VM-based confidential computing (AMD SEV-SNP and Intel TDX), the KVM guest memfd effort [1] is ongoing. It allows guests to use file descriptors as the protected guest memory without user-space virtual address mapping. KVM handles machine checks for guest vcpu specially. It sets up the guest so that vcpu exits from running guests on machine check, checks the exit reason, and manually raises the machine check by calling do_machine_check().

Although Linux supports hwpoison and MCE injection framework, there are gaps in testing hwpoison or MCE for TDX KVM [2] with KVM guest memfd [1]. a) hwpoison framework (debugfs /sys/kernel/debug/hwpoison/{corrupt-pfn, unpison-pfn}) uses physical address. MADV_{HWPISON, UNPOISON} uses the virtual address of the user process. However, KVM guest memfd requires file descriptor and offset. b) The x86 MCE injection framework, /dev/mce-log (legacy deprecated device driver interface) or debug fs /sys/kernel/debug/mce-inject/addr, also uses a physical address. c) The x86 MCE injection framework injects machine checks in the context of the injector process. KVM wants to inject machine check on behalf of running vcpu.

Proposed solution
=================
This patch series fills those gaps and to test KVM with injecting machine checks. The proposed solution is

a) Introduce new flags FADVISE_{HWPOISON, UNPOISON} to hwpoison memory to posix_fadvise():
Possible options are a1) add new flags for posix_fadvise() because hwpoison with file descriptor and offset is a generic operation, not specific to KVM. (This patch series) a2) Add KVM guest memfd specific ioctl to inject hwpoison/trigger MCE. We can use same value to MADV_{HWPOISON, UNPOISON}. a3) Add KVM-specific debugfs entry for guest memfd. Say, /sys/kernel/debug/kvm/<pid>-<vm-fd>/guest-memfd<fd>/hwpoison/{corrupt-offset, unoison-offset}.

- fadvise(FADVISE_{HWPOISON, UNPOISON}): This patch series. Generic interface. Not specific to KVM.
- KVM ioctl
KVM specific. The KVM debugfs is better.
- KVM debugfs
Debugfs is natural fit because this feature is for debug/test. Specific to KVM guest_memfd.

b) Enhancement to x86 MCE injector:
Add debug fs entries to x86 MCE injector under /sys/kernel/debug/mce-inject/ to allow necessary parameters. mcgstatus and notrigger. mcgstatus is for LMCE_S. notrigger is to suppress triggering machine check handler so that KVM can trigger it.

c) Add a debugfs entry for KVM vcpu to trigger MCE injection:
Because setting parameters for MCE is not specific to KVM, reuse the existing debugfs mce-inject. The debugfs entry is only to cause KVM to trigger MCE. An alternative is to add an interface to set parameters without b) similar to /dev/mce-log.

- KVM debugfs: this patch series.
Debugfs is natural fit because this feature is for debug/test.
- New KVM ioctl
KVM debugfs seems better.
- Enhance /dev/mce-log
Because this is legacy, debugfs mce injector is better.
- Enhance /sys/kernel/debug/mce-inject/
Because the feature is KVM specific, adding the feature to KVM interface is better than to the x86 MCE injector.

[1] https://lore.kernel.org/all/[email protected]/
KVM guest_memfd() and per-page attributes
https://lore.kernel.org/all/[email protected]/
[PATCH 00/13] KVM: guest_memfd fixes
[2] https://lore.kernel.org/all/[email protected]/
v15 KVM TDX basic feature support

Isaku Yamahata (12):
x86/mce: Fix hw MCE injection feature detection X86/mce/inject: Add mcgstatus for mce-inject debugfs x86/mce/inject: Add notrigger entry to suppress MCE injection x86/mce: Move and export inject_mce() from inject.c to core.c mm/fadvise: Add flags to inject hwpoison for posix_fadvise() mm/fadvise: Add FADV_MCE_INJECT flag for posix_fadvise() x86/mce/inject: Wire up the x86 MCE injector to FADV_MCE_INJECT x86/mce: Define a notifier chain for mce injector
KVM: X86: Add debugfs to inject machine check on VM exit
KVM: selftests: Allow mapping guest memory without host alias
KVM: selftests: lib: Add src memory type for hwpoison test
KVM: selftests: hwpoison/mce failure injection

 arch/x86/include/asm/kvm_host.h               |   1 +
 arch/x86/include/asm/mce.h                    |  16 +
 arch/x86/kernel/cpu/mce/core.c                |  56 ++
 arch/x86/kernel/cpu/mce/inject.c              |  91 ++-
 arch/x86/kvm/debugfs.c                        |  22 +
 arch/x86/kvm/x86.c                            |  14 +
 include/linux/fs.h                            |   8 +
 include/uapi/linux/fadvise.h                  |   5 +
 mm/fadvise.c                                  | 120 ++-
 tools/testing/selftests/kvm/Makefile          |   1 +
 .../selftests/kvm/include/kvm_util_base.h     |   4 +
 .../testing/selftests/kvm/include/test_util.h |   2 +
 tools/testing/selftests/kvm/lib/kvm_util.c    |  30 +-
 tools/testing/selftests/kvm/lib/test_util.c   |   8 +
 .../testing/selftests/kvm/mem_hwpoison_test.c | 721 ++++++++++++++++++
 15 files changed, 1066 insertions(+), 33 deletions(-)
 create mode 100644 tools/testing/selftests/kvm/mem_hwpoison_test.c

base-commit: 6465e260f48790807eef06b583b38ca9789b6072

Signature · 1 line
2.25.1
#4 lore raw

PATCH 3/12 x86/mce/inject: Add notrigger entry to suppress MCE injection

From: Isaku Yamahata <[email protected]>

The current x86 MCE injection framework injects MCE when writing to /sys/kernel/debug/mce-inject/bank. KVM wants to inject machine check on behalf of vcpu context instead of the context of writing to the bank file.

Because ACPI APEI has a similar requirement and it adds /sys/kernel/debug/apei/notrigger to suppress immediate injection. By Following it, add /sys/kernel/debug/mce-inject/notrigger to suppress MCE injection.

The alternative is add new value "notrigger" to /sys/kernel/debug/mce-inject/flags in addition to "sw", "hw", "df", and "th". Because it may break user space ABI, this option follow ACPI APEI error injection.

Supposed usage flow:

$ echo 1 > notrigger
  ... setup MCE values
$ echo 0 > bank
  The last step to setup mce value to inject with MC bank 0.
  Originally this step injects mce.  With noijnect=1, don't inject.
$ echo 1 > /sys/kernel/debug/kvm/<pid>-<fd>/vcpu<N>/mce-inject
  tell KVM to inject MCE in the context of vcpu.
---
 arch/x86/kernel/cpu/mce/inject.c | 23 +++++++++++++++++++++++
 1 file changed, 23 insertions(+)
arch/x86/kernel/cpu/mce/inject.c +23 -0
index 461858ae18f9..88603a6c0afe 100644
@@ -34,6 +34,7 @@
 #include "internal.h"
 
 static bool hw_injection_possible;
+static u64 notrigger;
 
 /*
  * Collect all the MCi_XXX settings
@@ -598,6 +599,8 @@ static int inj_bank_set(void *data, u64 val)
 	}
 
 	m->bank = val;
+	if (notrigger)
+		return 0;
 
 	/*
 	 * sw-only injection allows to write arbitrary values into the MCA
@@ -637,6 +640,21 @@ MCE_INJECT_GET(bank);
 
 DEFINE_SIMPLE_ATTRIBUTE(bank_fops, inj_bank_get, inj_bank_set, "%llu\n");
 
+static int inj_notrigger_get(void *data, u64 *val)
+{
+	*val = notrigger;
+	return 0;
+}
+
+static int inj_notrigger_set(void *data, u64 val)
+{
+	notrigger = val;
+	return 0;
+}
+
+DEFINE_SIMPLE_ATTRIBUTE(notrigger_fops, inj_notrigger_get, inj_notrigger_set,
+			"%llx\n");
+
 static const char readme_msg[] =
 "Description of the files and their usages:\n"
 "\n"
@@ -685,6 +703,9 @@ static const char readme_msg[] =
 "\n"
 "mcgstatus:\t Set MCG_STATUS: the bits in that MSR describes the current state\n"
 "\t of the processor after the MCE.\n"
+"\n"
+"notrigger:\t Suppress triggering the injection when set to non-zero\n"
+"\t The injection is triggered by other way.\n"
 "\n";
 
 static ssize_t
@@ -714,6 +735,8 @@ static struct dfs_node {
 	{ .name = "cpu",	.fops = &extcpu_fops, .perm = S_IRUSR | S_IWUSR },
 	{ .name = "mcgstatus",	.fops = &mcgstatus_fops,
 						      .perm = S_IRUSR | S_IWUSR },
+	{ .name = "notrigger",	.fops = &notrigger_fops,
+						      .perm = S_IRUSR | S_IWUSR },
 	{ .name = "README",	.fops = &readme_fops, .perm = S_IRUSR | S_IRGRP | S_IROTH },
 };
 
Signature · 1 line
2.25.1
#5 lore raw

PATCH 4/12 x86/mce: Move and export inject_mce() from inject.c to core.c

In reply to [email protected]

From: Isaku Yamahata <[email protected]>

X86 MCE support is kernel builtin. CONFIG_X86_MCE=y or n. The x86 MCE injection framework can be kernel builtin or kernel module. CONFIG_X86_MCE_INJECT=y, m, or n. We don't want KVM depend on CONFIG_X86_MCE_INJECT=y, allow CONFIG_X86_MCE_INJECT=m for KVM mce injection.

Move the necessary symbols with rename from arch/x86/kernel/cpu/mce/inject.c to arch/x86/kernel/cpu/mce/core.c and export symbols for KVM to inject MCE.

Oppertunistically add lockdep_assert_held(&mce_inject_mutex) to show that injectm is protected by mce_inject_mutex.

---
 arch/x86/include/asm/mce.h       | 10 +++++++++
 arch/x86/kernel/cpu/mce/core.c   | 36 ++++++++++++++++++++++++++++++++
 arch/x86/kernel/cpu/mce/inject.c | 26 +++--------------------
 3 files changed, 49 insertions(+), 23 deletions(-)
arch/x86/include/asm/mce.h +10 -0
index 180b1cbfcc4e..459066ecd922 100644
@@ -265,6 +265,16 @@ int mce_notify_irq(void);
 
 DECLARE_PER_CPU(struct mce, injectm);
 
+#ifdef CONFIG_X86_MCE
+void mce_inject_lock(void);
+void mce_inject_unlock(void);
+void mce_inject(struct mce *m);
+#else
+static inline void mce_inject_lock(void) {}
+static inline void mce_inject_unlock(void) {}
+static inline void mce_inject(struct mce *m) {}
+#endif
+
 /* Disable CMCI/polling for MCA bank claimed by firmware */
 extern void mce_disable_bank(int bank);
 
arch/x86/kernel/cpu/mce/core.c +36 -0
index 6f35f724cc14..6929c3cad278 100644
@@ -129,9 +129,45 @@ void mce_setup(struct mce *m)
 	m->ppin = cpu_data(m->extcpu).ppin;
 	m->microcode = boot_cpu_data.microcode;
 }
+EXPORT_SYMBOL_GPL(mce_setup);
 
 DEFINE_PER_CPU(struct mce, injectm);
 EXPORT_PER_CPU_SYMBOL_GPL(injectm);
+static DEFINE_MUTEX(mce_inject_mutex);
+
+void mce_inject_lock(void)
+{
+	mutex_lock(&mce_inject_mutex);
+}
+EXPORT_SYMBOL_GPL(mce_inject_lock);
+
+void mce_inject_unlock(void)
+{
+	mutex_unlock(&mce_inject_mutex);
+}
+EXPORT_SYMBOL_GPL(mce_inject_unlock);
+
+/* Update fake mce registers on current CPU. */
+void mce_inject(struct mce *m)
+{
+	struct mce *i = &per_cpu(injectm, m->extcpu);
+
+	lockdep_assert_held(&mce_inject_mutex);
+
+	/* Make sure no one reads partially written injectm */
+	i->finished = 0;
+	mb();
+	m->finished = 0;
+	/* First set the fields after finished */
+	i->extcpu = m->extcpu;
+	mb();
+	/* Now write record in order, finished last (except above) */
+	memcpy(i, m, sizeof(struct mce));
+	/* Finally activate it */
+	mb();
+	i->finished = 1;
+}
+EXPORT_SYMBOL_GPL(mce_inject);
 
 void mce_log(struct mce *m)
 {
arch/x86/kernel/cpu/mce/inject.c +3 -23
index 88603a6c0afe..ae3efbeb78bd 100644
@@ -126,25 +126,6 @@ static void setup_inj_struct(struct mce *m)
 	m->microcode = boot_cpu_data.microcode;
 }
 
-/* Update fake mce registers on current CPU. */
-static void inject_mce(struct mce *m)
-{
-	struct mce *i = &per_cpu(injectm, m->extcpu);
-
-	/* Make sure no one reads partially written injectm */
-	i->finished = 0;
-	mb();
-	m->finished = 0;
-	/* First set the fields after finished */
-	i->extcpu = m->extcpu;
-	mb();
-	/* Now write record in order, finished last (except above) */
-	memcpy(i, m, sizeof(struct mce));
-	/* Finally activate it */
-	mb();
-	i->finished = 1;
-}
-
 static void raise_poll(struct mce *m)
 {
 	unsigned long flags;
@@ -176,7 +157,6 @@ static void raise_exception(struct mce *m, struct pt_regs *pregs)
 }
 
 static cpumask_var_t mce_inject_cpumask;
-static DEFINE_MUTEX(mce_inject_mutex);
 
 static int mce_raise_notify(unsigned int cmd, struct pt_regs *regs)
 {
@@ -245,7 +225,7 @@ static void __maybe_unused raise_mce(struct mce *m)
 {
 	int context = MCJ_CTX(m->inject_flags);
 
-	inject_mce(m);
+	mce_inject(m);
 
 	if (context == MCJ_CTX_RANDOM)
 		return;
@@ -303,9 +283,9 @@ static int mce_inject_raise(struct notifier_block *nb, unsigned long val,
 	if (!m)
 		return NOTIFY_DONE;
 
-	mutex_lock(&mce_inject_mutex);
+	mce_inject_lock();
 	raise_mce(m);
-	mutex_unlock(&mce_inject_mutex);
+	mce_inject_unlock();
 
 	return NOTIFY_DONE;
 }
Signature · 1 line
2.25.1
#1 lore raw

PATCH 1/12 x86/mce: Fix hw MCE injection feature detection

In reply to [email protected]

From: Isaku Yamahata <[email protected]>

When initializing x86 MCE injection framework, it checks if hardware mce injection is available or not. When it's not available on AMD, set the boolean variable to false to not use it. The variable is on by default and the feature is AMD specific based on the code.

Because the variable is default on, it is true on Intel platform (probably on other non-AMD x86 platform). It results in unchecked msr access of MSR_K7_HWCR=0xc0010015 when injecting MCE on Intel platform. (Probably on other x86 platform.)

Make the variable of by default, and set the variable on when the hardware feature is usable.

The procedure to produce on Intel platform.
echo hw > /sys/kernel/debug/mce-inject/flags
This succeeds.
set other MCE parameters if necessary.
echo 0 > /sys/kernel/debug/mce-inject/bank
This triggers mce injection.

The kernel accesses MSR_K7_HWCR=0xc0010015 resulting unchecked MSR access.

Fixes 891e465a1bd8 ("x86/mce: Check whether writes to MCA_STATUS are getting ignored")
---
 arch/x86/kernel/cpu/mce/inject.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
arch/x86/kernel/cpu/mce/inject.c +2 -1
index 4d8d4bcf915d..881898a1d2f4 100644
@@ -33,7 +33,7 @@
 
 #include "internal.h"
 
-static bool hw_injection_possible = true;
+static bool hw_injection_possible;
 
 /*
  * Collect all the MCi_XXX settings
@@ -732,6 +732,7 @@ static void check_hw_inj_possible(void)
 	if (!cpu_feature_enabled(X86_FEATURE_SMCA))
 		return;
 
+	hw_injection_possible = true;
 	cpu = get_cpu();
 
 	for (bank = 0; bank < MAX_NR_BANKS; ++bank) {
Signature · 1 line
2.25.1
#3 lore raw

PATCH 2/12 X86/mce/inject: Add mcgstatus for mce-inject debugfs

In reply to [email protected]

From: Isaku Yamahata <[email protected]>

To test KVM x86 to inject machine check with memory failure, it wants to to set LMCE_S (local machine check exception signaled) for memory failure injection test. The current code sets mcgstatus based on the other values and can't set LMCE_S flag.

Add mcgstatus entry to allow to set mcgstatus value.

---
 arch/x86/kernel/cpu/mce/inject.c | 8 ++++++++
 1 file changed, 8 insertions(+)
arch/x86/kernel/cpu/mce/inject.c +8 -0
index 881898a1d2f4..461858ae18f9 100644
@@ -72,6 +72,7 @@ static int inj_##reg##_set(void *data, u64 val)				\
 	return 0;							\
 }
 
+MCE_INJECT_SET(mcgstatus);
 MCE_INJECT_SET(status);
 MCE_INJECT_SET(misc);
 MCE_INJECT_SET(addr);
@@ -86,12 +87,14 @@ static int inj_##reg##_get(void *data, u64 *val)			\
 	return 0;							\
 }
 
+MCE_INJECT_GET(mcgstatus);
 MCE_INJECT_GET(status);
 MCE_INJECT_GET(misc);
 MCE_INJECT_GET(addr);
 MCE_INJECT_GET(synd);
 MCE_INJECT_GET(ipid);
 
+DEFINE_SIMPLE_ATTRIBUTE(mcgstatus_fops, inj_mcgstatus_get, inj_mcgstatus_set, "%llx\n");
 DEFINE_SIMPLE_ATTRIBUTE(status_fops, inj_status_get, inj_status_set, "%llx\n");
 DEFINE_SIMPLE_ATTRIBUTE(misc_fops, inj_misc_get, inj_misc_set, "%llx\n");
 DEFINE_SIMPLE_ATTRIBUTE(addr_fops, inj_addr_get, inj_addr_set, "%llx\n");
@@ -679,6 +682,9 @@ static const char readme_msg[] =
 "\t    APIC interrupt handler to handle the error. \n"
 "\n"
 "ipid:\t IPID (AMD-specific)\n"
+"\n"
+"mcgstatus:\t Set MCG_STATUS: the bits in that MSR describes the current state\n"
+"\t of the processor after the MCE.\n"
 "\n";
 
 static ssize_t
@@ -706,6 +712,8 @@ static struct dfs_node {
 	{ .name = "bank",	.fops = &bank_fops,   .perm = S_IRUSR | S_IWUSR },
 	{ .name = "flags",	.fops = &flags_fops,  .perm = S_IRUSR | S_IWUSR },
 	{ .name = "cpu",	.fops = &extcpu_fops, .perm = S_IRUSR | S_IWUSR },
+	{ .name = "mcgstatus",	.fops = &mcgstatus_fops,
+						      .perm = S_IRUSR | S_IWUSR },
 	{ .name = "README",	.fops = &readme_fops, .perm = S_IRUSR | S_IRGRP | S_IROTH },
 };
 
Signature · 1 line
2.25.1
#6 lore raw

PATCH 5/12 mm/fadvise: Add flags to inject hwpoison for posix_fadvise()

In reply to [email protected]

From: Isaku Yamahata <[email protected]>

For VM-based confidential computing (AMD SEV-SNP and Intel TDX), the KVM guest memfd effort [1] is ongoing. It allows KVM guests to use file descriptors and their offset as protected guest memory without user-space virtual address mapping.

Intel TDX uses machine checks to notify the host OS/VMM that the TDX guest consumed corrupted memory. The KVM/x86 handles machine checks for guest vcpu specially. It sets up the guest so that vcpu exits from running on machine check, checks the exit reason, and manually raises the machine check by calling do_machine_check(). To test the KVM machine check path, KVM wants to poison memory based on file descriptor and its offset. The current memory poisoning is based on the physical address, /sys/kernel/debug/hwpoison/{corrupt-pfn, unpoison-pfn}, or the virtual address, MADV_HWPOISON and MADV_UNPOISON.

Add new flags FADV_HWPOISON, and FADV_UNPOISON to posix_fadvise() by following MADV_HWPOISON and MADV_UNPOISON. 9893e49d64a4 ("HWPOISON: Add madvise() based injector for hardware poisoned pages v4")

The possible options would be
- Add FADV flags for memory poison. This patch.
- introduce IOCTL specific to KVM guest memfd
- introduce debugfs entries for KVM guest memfd /sys/kernel/debug/kvm/<pid>-<vm-fd>/guest-memfd<fd>/hwpoison/ {corrupt-offset, unoison-offset}.
This options follows /sys/kernel/debug/hwpoison/{corrupt-pfn, unpoison-pfn}

[1] https://lore.kernel.org/all/[email protected]/
KVM guest_memfd() and per-page attributes
https://lore.kernel.org/all/[email protected]/
[PATCH 00/13] KVM: guest_memfd fixes

---
 include/uapi/linux/fadvise.h |  4 +++
 mm/fadvise.c                 | 58 +++++++++++++++++++++++++++++++++++-
 2 files changed, 61 insertions(+), 1 deletion(-)
include/uapi/linux/fadvise.h +4 -0
index 0862b87434c2..3699b4b0adcd 100644
@@ -19,4 +19,8 @@
 #define POSIX_FADV_NOREUSE	5 /* Data will be accessed once.  */
 #endif
 
+/* Same to MADV_HWPOISON and MADV_SOFT_OFFLINE */
+#define FADV_HWPOISON		100	/* poison a page for testing */
+#define FADV_SOFT_OFFLINE	101	/* soft offline page for testing */
+
 #endif	/* FADVISE_H_INCLUDED */
mm/fadvise.c +57 -1

85 lines not on this page · raw message

Signature · 1 line
2.25.1
#7 lore raw

PATCH 6/12 mm/fadvise: Add FADV_MCE_INJECT flag for posix_fadvise()

In reply to [email protected]

From: Isaku Yamahata <[email protected]>

For VM-based confidential computing (AMD SEV-SNP and Intel TDX), the KVM guest memfd effort [1] is ongoing. It allows KVM guests to use file descriptors and their offset as protected guest memory without user-space virtual address mapping.

Intel TDX uses machine checks to notify the host OS/VMM that the TDX guest consumed corrupted memory. The KVM/x86 handles machine checks for guest vcpu specially. It sets up the guest so that vcpu exits from running on machine check, checks the exit reason, and manually raises the machine check by calling do_machine_check().

To test the KVM machine check path, KVM wants to inject an x86 machine check based on the file descriptor and offset. Add a new flag FADV_MCE_INJECT for posix_fadvise() to tell the x86 MCE injector the physical address. The x86 MCE injector can be kernel-builtin or kernel module. CONFIG_X86_MCE_INJECT=y, m, or n. Because we don't want posix_fadvise() to depend on the x86 MCE injector, add notifier for it to register. And the x86 MCE injector will register to the notifier chain.

The possible options would be
- Add FADV flags for memory poison. This patch.
- Introduce a new IOCTL specific to KVM guest memfd
- Introduce debugfs entries for KVM guest memfd like /sys/kernel/debug/kvm/<pid>-<vm-fd>/guest-memfd<fd>/hwpoison/ {corrupt-offset, unoison-offset}.
This options follows /sys/kernel/debug/hwpoison/ {corrupt-pfn, unpoison-pfn}

[1] https://lore.kernel.org/all/[email protected]/
KVM guest_memfd() and per-page attributes
https://lore.kernel.org/all/[email protected]/
[PATCH 00/13] KVM: guest_memfd fixes

---
 include/linux/fs.h           |  8 +++++
 include/uapi/linux/fadvise.h |  1 +
 mm/fadvise.c                 | 62 ++++++++++++++++++++++++++++++++++++
 3 files changed, 71 insertions(+)
include/linux/fs.h +8 -0
index b528f063e8ff..c458f1e86245 100644
@@ -3377,5 +3377,13 @@ extern int vfs_fadvise(struct file *file, loff_t offset, loff_t len,
 		       int advice);
 extern int generic_fadvise(struct file *file, loff_t offset, loff_t len,
 			   int advice);
+#ifdef CONFIG_X86_MCE_INJECT
+void fadvise_register_mce_injector_chain(struct notifier_block *nb);
+void fadvise_unregister_mce_injector_chain(struct notifier_block *nb);
+#else
+static inline void fadvise_register_mce_injector_chain(struct notifier_block *nb) {}
+static inline void fadvise_unregister_mce_injector_chain(struct notifier_block *nb) {}
+#endif
+
 
 #endif /* _LINUX_FS_H */
include/uapi/linux/fadvise.h +1 -0
index 3699b4b0adcd..90e1b5a94f37 100644
@@ -22,5 +22,6 @@
 /* Same to MADV_HWPOISON and MADV_SOFT_OFFLINE */
 #define FADV_HWPOISON		100	/* poison a page for testing */
 #define FADV_SOFT_OFFLINE	101	/* soft offline page for testing */
+#define FADV_MCE_INJECT		102
 
 #endif	/* FADVISE_H_INCLUDED */
mm/fadvise.c +62 -0

80 lines not on this page · raw message

Signature · 1 line
2.25.1
#8 lore raw

PATCH 7/12 x86/mce/inject: Wire up the x86 MCE injector to FADV_MCE_INJECT

In reply to [email protected]

From: Isaku Yamahata <[email protected]>

To inject machine check based on FADV_MCE_INJECT, wire up the x86 MCE injector to register its handler to get notified physical address on FADV_MCE_INJECT.

---
 arch/x86/kernel/cpu/mce/inject.c | 13 +++++++++++++
 1 file changed, 13 insertions(+)
arch/x86/kernel/cpu/mce/inject.c +13 -0
index ae3efbeb78bd..43d896a89648 100644
@@ -294,6 +294,17 @@ static struct notifier_block inject_nb = {
 	.notifier_call  = mce_inject_raise,
 };
 
+static int fadvise_inject_addr(struct notifier_block *nb, unsigned long val,
+				void *data)
+{
+	inj_addr_set(&i_mce, val);
+	return NOTIFY_DONE;
+}
+
+static struct notifier_block fadvise_nb = {
+	.notifier_call	= fadvise_inject_addr,
+}
+
 /*
  * Caller needs to be make sure this cpu doesn't disappear
  * from under us, i.e.: get_cpu/put_cpu.
@@ -784,6 +795,7 @@ static int __init inject_init(void)
 
 	register_nmi_handler(NMI_LOCAL, mce_raise_notify, 0, "mce_notify");
 	mce_register_injector_chain(&inject_nb);
+	fadvise_register_mce_injector_chain(&fadvise_nb);
 
 	setup_inj_struct(&i_mce);
 
@@ -795,6 +807,7 @@ static int __init inject_init(void)
 static void __exit inject_exit(void)
 {
 
+	fadvise_unregister_mce_injector_chain(&fadvise_nb);
 	mce_unregister_injector_chain(&inject_nb);
 	unregister_nmi_handler(NMI_LOCAL, "mce_notify");
 
Signature · 1 line
2.25.1
#9 lore raw

PATCH 8/12 x86/mce: Define a notifier chain for mce injector

In reply to [email protected]

From: Isaku Yamahata <[email protected]>

To test KVM mechine check injection path, KVM wants to trigger machine check on its own. Not on writing to debugfs bank file of x86 mce injector.

Because KVM doesn't want to depend on x86 MCE injector, define notifier chain in the x86 MCE core and make the x86 MCE injector to register the notifier chain to decouple the MCE injector and its user. This follows how dev-mcelog does.

---
 arch/x86/include/asm/mce.h       |  6 ++++++
 arch/x86/kernel/cpu/mce/core.c   | 20 ++++++++++++++++++++
 arch/x86/kernel/cpu/mce/inject.c | 18 ++++++++++++++++++
 3 files changed, 44 insertions(+)
arch/x86/include/asm/mce.h +6 -0
index 459066ecd922..1a832a207b6a 100644
@@ -269,10 +269,16 @@ DECLARE_PER_CPU(struct mce, injectm);
 void mce_inject_lock(void);
 void mce_inject_unlock(void);
 void mce_inject(struct mce *m);
+void mce_register_atomic_injector_chain(struct notifier_block *nb);
+void mce_unregister_atomic_injector_chain(struct notifier_block *nb);
+void mce_call_atomic_injector_chain(unsigned long cpu);
 #else
 static inline void mce_inject_lock(void) {}
 static inline void mce_inject_unlock(void) {}
 static inline void mce_inject(struct mce *m) {}
+static inline void mce_register_atomic_injector_chain(struct notifier_block *nb) {}
+static inline void mce_unregister_atomic_injector_chain(struct notifier_block *nb) {}
+static inline void mce_call_atomic_injector_chain(unsigned long cpu) {}
 #endif
 
 /* Disable CMCI/polling for MCA bank claimed by firmware */
arch/x86/kernel/cpu/mce/core.c +20 -0
index 6929c3cad278..9a6b04be7404 100644
@@ -169,6 +169,26 @@ void mce_inject(struct mce *m)
 }
 EXPORT_SYMBOL_GPL(mce_inject);
 
+static ATOMIC_NOTIFIER_HEAD(mce_atomic_injector_chain);
+
+void mce_register_atomic_injector_chain(struct notifier_block *nb)
+{
+	atomic_notifier_chain_register(&mce_atomic_injector_chain, nb);
+}
+EXPORT_SYMBOL_GPL(mce_register_atomic_injector_chain);
+
+void mce_unregister_atomic_injector_chain(struct notifier_block *nb)
+{
+	atomic_notifier_chain_unregister(&mce_atomic_injector_chain, nb);
+}
+EXPORT_SYMBOL_GPL(mce_unregister_atomic_injector_chain);
+
+void mce_call_atomic_injector_chain(unsigned long cpu)
+{
+	atomic_notifier_call_chain(&mce_atomic_injector_chain, cpu, NULL);
+}
+EXPORT_SYMBOL_GPL(mce_call_atomic_injector_chain);
+
 void mce_log(struct mce *m)
 {
 	if (!mce_gen_pool_add(m))
arch/x86/kernel/cpu/mce/inject.c +18 -0
index 43d896a89648..5dac4dcb25aa 100644
@@ -303,8 +303,24 @@ static int fadvise_inject_addr(struct notifier_block *nb, unsigned long val,
 
 static struct notifier_block fadvise_nb = {
 	.notifier_call	= fadvise_inject_addr,
+};
+
+static int mce_atomic_injector(struct notifier_block *nb, unsigned long val,
+			       void *data)
+{
+	lockdep_assert_preemption_disabled();
+
+	i_mce.extcpu = val;
+	mce_inject(&i_mce);
+	setup_inj_struct(&i_mce);
+
+	return NOTIFY_DONE;
 }
 
+static struct notifier_block atomic_injector_nb = {
+	.notifier_call = mce_atomic_injector,
+};
+
 /*
  * Caller needs to be make sure this cpu doesn't disappear
  * from under us, i.e.: get_cpu/put_cpu.
@@ -796,6 +812,7 @@ static int __init inject_init(void)
 	register_nmi_handler(NMI_LOCAL, mce_raise_notify, 0, "mce_notify");
 	mce_register_injector_chain(&inject_nb);
 	fadvise_register_mce_injector_chain(&fadvise_nb);
+	mce_register_atomic_injector_chain(&atomic_injector_nb);
 
 	setup_inj_struct(&i_mce);
 
@@ -807,6 +824,7 @@ static int __init inject_init(void)
 static void __exit inject_exit(void)
 {
 
+	mce_unregister_atomic_injector_chain(&atomic_injector_nb);
 	fadvise_unregister_mce_injector_chain(&fadvise_nb);
 	mce_unregister_injector_chain(&inject_nb);
 	unregister_nmi_handler(NMI_LOCAL, "mce_notify");
Signature · 1 line
2.25.1
#10 lore raw

PATCH 9/12 KVM: X86: Add debugfs to inject machine check on VM exit

In reply to [email protected]

From: Isaku Yamahata <[email protected]>

The KVM/x86 handles machine-check in the guest specially. It sets up the guest so that vcpu exits from running guests, checks the exit reason and, manually raises the machine check by calling do_machine_check().

To test the KVM machine check execution path, KVM wants to inject the machine check in the context of vcpu instead of the context of the process of MCE injection. Wire up the MCE injection framework for KVM to trigger MCE in the vcpu context. Add a kvm vcpu debugfs entry for an operator to tell KVM to inject MCE.

The operation flow is as follows:
- Set notrigger to 1 to tell the x86 MCE injector to suppress it from injecting machine check.
echo 1 > /sys/kernel/debug/mce-inject/notrigger
- Set MCE parameters via x86 MCE injector debugfs /sys/kernel/debug/mce-inject/{addr, bank, flags, mcgstatus, misc, status}
- Tell KVM to inject MCE
echo 1 > /sys/kernel/debug/kvm/<pid>-<vm-fd>/vcpu<vcpuid>/mce-inject

---
 arch/x86/include/asm/kvm_host.h |  1 +
 arch/x86/kvm/debugfs.c          | 22 ++++++++++++++++++++++
 arch/x86/kvm/x86.c              | 14 ++++++++++++++
 3 files changed, 37 insertions(+)
arch/x86/include/asm/kvm_host.h +1 -0
index 17715cb8731d..9286f3d02f30 100644
@@ -113,6 +113,7 @@
 	KVM_ARCH_REQ_FLAGS(31, KVM_REQUEST_WAIT | KVM_REQUEST_NO_WAKEUP)
 #define KVM_REQ_HV_TLB_FLUSH \
 	KVM_ARCH_REQ_FLAGS(32, KVM_REQUEST_WAIT | KVM_REQUEST_NO_WAKEUP)
+#define KVM_REQ_MCE_INJECT		KVM_ARCH_REQ(33)
 
 #define CR0_RESERVED_BITS                                               \
 	(~(unsigned long)(X86_CR0_PE | X86_CR0_MP | X86_CR0_EM | X86_CR0_TS \
arch/x86/kvm/debugfs.c +22 -0
index ee8c4c3496ed..fee208f30400 100644
@@ -56,6 +56,22 @@ static int vcpu_get_tsc_scaling_frac_bits(void *data, u64 *val)
 
 DEFINE_SIMPLE_ATTRIBUTE(vcpu_tsc_scaling_frac_fops, vcpu_get_tsc_scaling_frac_bits, NULL, "%llu\n");
 
+static int vcpu_mce_inject_set(void *data, u64 val)
+{
+	struct kvm_vcpu *vcpu = (struct kvm_vcpu *) data;
+
+	if (!capable(CAP_SYS_ADMIN))
+		return -EPERM;
+
+	if (val != 1)
+		return -EINVAL;
+	kvm_make_request(KVM_REQ_MCE_INJECT, vcpu);
+	kvm_vcpu_kick(vcpu);
+	return 0;
+}
+
+DEFINE_SIMPLE_ATTRIBUTE(vcpu_mce_inject_fops, NULL, vcpu_mce_inject_set, "%llx\n");
+
 void kvm_arch_create_vcpu_debugfs(struct kvm_vcpu *vcpu, struct dentry *debugfs_dentry)
 {
 	debugfs_create_file("guest_mode", 0444, debugfs_dentry, vcpu,
@@ -76,6 +92,12 @@ void kvm_arch_create_vcpu_debugfs(struct kvm_vcpu *vcpu, struct dentry *debugfs_
 				    debugfs_dentry, vcpu,
 				    &vcpu_tsc_scaling_frac_fops);
 	}
+
+	if (IS_ENABLED(CONFIG_X86_MCE_INJECT) &&
+	    boot_cpu_has(X86_FEATURE_MCE) && boot_cpu_has(X86_FEATURE_MCA))
+		debugfs_create_file("mce-inject", 0200,
+				    debugfs_dentry, vcpu,
+				    &vcpu_mce_inject_fops);
 }
 
 /*
arch/x86/kvm/x86.c +14 -0
index 9f18b06bbda6..e4c63ded4c9a 100644
@@ -10496,6 +10496,7 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu)
 	fastpath_t exit_fastpath;
 
 	bool req_immediate_exit = false;
+	bool req_mce_inject = false;
 
 	if (kvm_request_pending(vcpu)) {
 		if (kvm_check_request(KVM_REQ_VM_DEAD, vcpu)) {
@@ -10642,6 +10643,8 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu)
 
 		if (kvm_check_request(KVM_REQ_UPDATE_CPU_DIRTY_LOGGING, vcpu))
 			static_call(kvm_x86_update_cpu_dirty_logging)(vcpu);
+
+		req_mce_inject = kvm_check_request(KVM_REQ_MCE_INJECT, vcpu);
 	}
 
 	if (kvm_check_request(KVM_REQ_EVENT, vcpu) || req_int_win ||
@@ -10676,6 +10679,8 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu)
 		goto cancel_injection;
 	}
 
+	if (unlikely(req_mce_inject))
+		mce_inject_lock();
 	preempt_disable();
 
 	static_call(kvm_x86_prepare_switch_to_guest)(vcpu);
@@ -10721,6 +10726,10 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu)
 		smp_wmb();
 		local_irq_enable();
 		preempt_enable();
+		if (unlikely(req_mce_inject)) {
+			kvm_make_request(KVM_REQ_MCE_INJECT, vcpu);
+			mce_inject_unlock();
+		}
 		kvm_vcpu_srcu_read_lock(vcpu);
 		r = 1;
 		goto cancel_injection;
@@ -10814,6 +10823,11 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu)
 		fpu_sync_guest_vmexit_xfd_state();
 
 	static_call(kvm_x86_handle_exit_irqoff)(vcpu);
+	if (unlikely(req_mce_inject)) {
+		mce_call_atomic_injector_chain(smp_processor_id());
+		kvm_machine_check();
+		mce_inject_unlock();
+	}
 
 	if (vcpu->arch.guest_fpu.xfd_err)
 		wrmsrl(MSR_IA32_XFD_ERR, 0);
Signature · 1 line
2.25.1
#12 lore raw

PATCH 10/12 KVM: selftests: Allow mapping guest memory without host alias

In reply to [email protected]

From: Isaku Yamahata <[email protected]>

For test the KVM hwpoison of memory, the recovery path unmaps the virtual memory and send SIGBUS with its address. If the poisoned page is mapped at twice or more in the virtual memory, the address in SIGBUS siginfo isn't deterministic.

To make the hwpoison test cases deterministic, allow to avoid guest memory alias.

---
 .../selftests/kvm/include/kvm_util_base.h     |  4 ++++
 tools/testing/selftests/kvm/lib/kvm_util.c    | 23 ++++++++++++++-----
 2 files changed, 21 insertions(+), 6 deletions(-)
tools/testing/selftests/kvm/include/kvm_util_base.h +4 -0
index a18db6a7b3cf..e980776a2c94 100644
@@ -435,6 +435,10 @@ void vm_set_user_memory_region(struct kvm_vm *vm, uint32_t slot, uint32_t flags,
 			       uint64_t gpa, uint64_t size, void *hva);
 int __vm_set_user_memory_region(struct kvm_vm *vm, uint32_t slot, uint32_t flags,
 				uint64_t gpa, uint64_t size, void *hva);
+void __vm_userspace_mem_region_add(struct kvm_vm *vm,
+	enum vm_mem_backing_src_type src_type,
+	uint64_t guest_paddr, uint32_t slot, uint64_t npages,
+	uint32_t flags, bool alias);
 void vm_userspace_mem_region_add(struct kvm_vm *vm,
 	enum vm_mem_backing_src_type src_type,
 	uint64_t guest_paddr, uint32_t slot, uint64_t npages,
tools/testing/selftests/kvm/lib/kvm_util.c +17 -6
index 7a8af1821f5d..310c3a760cb8 100644
@@ -691,12 +691,14 @@ static void __vm_mem_region_delete(struct kvm_vm *vm,
 	sparsebit_free(&region->unused_phy_pages);
 	ret = munmap(region->mmap_start, region->mmap_size);
 	TEST_ASSERT(!ret, __KVM_SYSCALL_ERROR("munmap()", ret));
-	if (region->fd >= 0) {
-		/* There's an extra map when using shared memory. */
+
+	/* There's an extra map when using shared memory. */
+	if (region->mmap_alias) {
 		ret = munmap(region->mmap_alias, region->mmap_size);
 		TEST_ASSERT(!ret, __KVM_SYSCALL_ERROR("munmap()", ret));
-		close(region->fd);
 	}
+	if (region->fd >= 0)
+		close(region->fd);
 
 	free(region);
 }
@@ -920,10 +922,10 @@ void vm_set_user_memory_region(struct kvm_vm *vm, uint32_t slot, uint32_t flags,
  * given by slot, which must be unique and < KVM_MEM_SLOTS_NUM.  The
  * region is created with the flags given by flags.
  */
-void vm_userspace_mem_region_add(struct kvm_vm *vm,
+void __vm_userspace_mem_region_add(struct kvm_vm *vm,
 	enum vm_mem_backing_src_type src_type,
 	uint64_t guest_paddr, uint32_t slot, uint64_t npages,
-	uint32_t flags)
+	uint32_t flags, bool create_alias)
 {
 	int ret;
 	struct userspace_mem_region *region;
@@ -1057,7 +1059,7 @@ void vm_userspace_mem_region_add(struct kvm_vm *vm,
 	hash_add(vm->regions.slot_hash, &region->slot_node, slot);
 
 	/* If shared memory, create an alias. */
-	if (region->fd >= 0) {
+	if (region->fd >= 0 && create_alias) {
 		region->mmap_alias = mmap(NULL, region->mmap_size,
 					  PROT_READ | PROT_WRITE,
 					  vm_mem_backing_src_alias(src_type)->flag,
@@ -1070,6 +1072,15 @@ void vm_userspace_mem_region_add(struct kvm_vm *vm,
 	}
 }
 
+void vm_userspace_mem_region_add(struct kvm_vm *vm,
+	enum vm_mem_backing_src_type src_type,
+	uint64_t guest_paddr, uint32_t slot, uint64_t npages,
+	uint32_t flags)
+{
+	__vm_userspace_mem_region_add(vm, src_type, guest_paddr, slot, npages,
+				      flags, true);
+}
+
 /*
  * Memslot to region
  *
Signature · 1 line
2.25.1
#13 lore raw

PATCH 11/12 KVM: selftests: lib: Add src memory type for hwpoison test

In reply to [email protected]

From: Isaku Yamahata <[email protected]>

For hwpoison test to recover hwpoisoned memory, use madvise(MADV_FREE) that requires private mapping. Add a new src memory type for it.

---
 tools/testing/selftests/kvm/include/test_util.h | 2 ++
 tools/testing/selftests/kvm/lib/kvm_util.c      | 7 +++++--
 tools/testing/selftests/kvm/lib/test_util.c     | 8 ++++++++
 3 files changed, 15 insertions(+), 2 deletions(-)
tools/testing/selftests/kvm/include/test_util.h +2 -0
index 7e614adc6cf4..c97af4ff0699 100644
@@ -111,6 +111,8 @@ enum vm_mem_backing_src_type {
 	VM_MEM_SRC_ANONYMOUS_HUGETLB_16GB,
 	VM_MEM_SRC_SHMEM,
 	VM_MEM_SRC_SHARED_HUGETLB,
+	VM_MEM_SRC_ANONYMOUS_MEMFD,
+	VM_MEM_SRC_ANONYMOUS_MEMFD_HUGETLB,
 	NUM_SRC_TYPES,
 };
 
tools/testing/selftests/kvm/lib/kvm_util.c +5 -2
index 310c3a760cb8..95ffb3b97dcf 100644
@@ -1007,9 +1007,12 @@ void __vm_userspace_mem_region_add(struct kvm_vm *vm,
 		region->mmap_size += alignment;
 
 	region->fd = -1;
-	if (backing_src_is_shared(src_type))
+	if (backing_src_is_shared(src_type) ||
+		src_type == VM_MEM_SRC_ANONYMOUS_MEMFD ||
+		src_type == VM_MEM_SRC_ANONYMOUS_MEMFD_HUGETLB)
 		region->fd = kvm_memfd_alloc(region->mmap_size,
-					     src_type == VM_MEM_SRC_SHARED_HUGETLB);
+					     src_type == VM_MEM_SRC_SHARED_HUGETLB ||
+					     src_type == VM_MEM_SRC_ANONYMOUS_MEMFD_HUGETLB);
 
 	region->mmap_start = mmap(NULL, region->mmap_size,
 				  PROT_READ | PROT_WRITE,
tools/testing/selftests/kvm/lib/test_util.c +8 -0
index 5d7f28b02d73..a09bba5c074e 100644
@@ -281,6 +281,14 @@ const struct vm_mem_backing_src_alias *vm_mem_backing_src_alias(uint32_t i)
 			 */
 			.flag = MAP_SHARED,
 		},
+		[VM_MEM_SRC_ANONYMOUS_MEMFD] = {
+			.name = "anonymous_memfd",
+			.flag = ANON_FLAGS,
+		},
+		[VM_MEM_SRC_ANONYMOUS_MEMFD_HUGETLB] = {
+			.name = "anonymous_memfd_hugetlb",
+			.flag = ANON_HUGE_FLAGS,
+		},
 	};
 	_Static_assert(ARRAY_SIZE(aliases) == NUM_SRC_TYPES,
 		       "Missing new backing src types?");
Signature · 1 line
2.25.1
#14 lore raw

PATCH 12/12 KVM: selftests: hwpoison/mce failure injection

In reply to [email protected]

From: Isaku Yamahata <[email protected]>

Add test cases for KVM to inject hwpoison or x86 MCE. It exercises FADV_HWPOISON and KVM/x86 machine check injection.

---
 tools/testing/selftests/kvm/Makefile          |   1 +
 .../testing/selftests/kvm/mem_hwpoison_test.c | 721 ++++++++++++++++++
 2 files changed, 722 insertions(+)
 create mode 100644 tools/testing/selftests/kvm/mem_hwpoison_test.c
tools/testing/selftests/kvm/Makefile +1 -0
index a3bb36fb3cfc..ec9b4c6f1e75 100644
@@ -128,6 +128,7 @@ TEST_GEN_PROGS_x86_64 += hardware_disable_test
 TEST_GEN_PROGS_x86_64 += kvm_create_max_vcpus
 TEST_GEN_PROGS_x86_64 += kvm_page_table_test
 TEST_GEN_PROGS_x86_64 += max_guest_memory_test
+TEST_GEN_PROGS_x86_64 += mem_hwpoison_test
 TEST_GEN_PROGS_x86_64 += memslot_modification_stress_test
 TEST_GEN_PROGS_x86_64 += memslot_perf_test
 TEST_GEN_PROGS_x86_64 += rseq_test
tools/testing/selftests/kvm/mem_hwpoison_test.c new file +721 -0

721 lines not on this page · raw message

Signature · 1 line
2.25.1
#2 lore raw

Re: [PATCH 01/12] x86/mce: Fix hw MCE injection feature detection

In reply to [email protected]

Isaku, when you report a bug, it would be great to include the folks who authored and worked on the original patch that introduced the bug. I've gone ahead and done that for you here.

Quoted 7 lines · On 10/10/23 01:35, [email protected] wrote:

From: Isaku Yamahata <[email protected]>

When initializing x86 MCE injection framework, it checks if hardware mce injection is available or not. When it's not available on AMD, set the boolean variable to false to not use it. The variable is on by default and the feature is AMD specific based on the code.

Quoted 7 lines

Because the variable is default on, it is true on Intel platform (probably on other non-AMD x86 platform). It results in unchecked msr access of MSR_K7_HWCR=0xc0010015 when injecting MCE on Intel platform. (Probably on other x86 platform.)

Make the variable of by default, and set the variable on when the hardware feature is usable.

Gah, I'm finding that changelog impenetrable. Here's what's missing:

* The entirety of check_hw_inj_possible() is for AMD hardware:
X86_FEATURE_SMCA, the MSRs, hw_injection_possible, everything.
* Only AMD systems with SMCA support hardware error injection (anything other than "echo sw > /sys/kernel/debug/mce-inject/flags")
* That AMD-only restriction is enforced by 'hw_injection_possible'
* 'hw_injection_possible' is true by default and only set to false in check_hw_inj_possible() ... the AMD-only code

The end result is that everyone except SMCA-enabled AMD systems (Intel included) leaves hw_injection_possible=true. They are free to try and inject hardware errors. If they do, they'll get errors when writing to the MSRs.

To fix this, make disable hw_injection_possible by default. Only enable it on SMCA hardware that actually succeeds in ... whatever:

                wrmsrl_safe(mca_msr_reg(bank, MCA_STATUS), status);
                rdmsrl_safe(mca_msr_reg(bank, MCA_STATUS), &status);

is doing.

... and don't do it at the top of the function. Why bother setting it to true only to disable it a moment later?

Do something like the following instead.

arch/x86/kernel/cpu/mce/inject.c +3 -2
index 4d8d4bcf915d..01ee886d8540 100644
@@ -33,7 +33,7 @@
 
 #include "internal.h"
 
-static bool hw_injection_possible = true;
+static bool hw_injection_possible;
 
 /*
  * Collect all the MCi_XXX settings
@@ -748,9 +748,10 @@ static void check_hw_inj_possible(void)
 		rdmsrl_safe(mca_msr_reg(bank, MCA_STATUS), &status);
 
 		if (!status) {
-			hw_injection_possible = false;
 			pr_warn("Platform does not allow *hardware* error injection."
 				"Try using APEI EINJ instead.\n");
+		} else {
+			hw_injection_possible = true;
 		}
 
 		toggle_hw_mce_inject(cpu, false);
#11 lore raw

Re: [PATCH 09/12] KVM: X86: Add debugfs to inject machine check on VM exit

In reply to [email protected]
Quoted 11 lines · On Tue, Oct 10, 2023, [email protected] wrote:

From: Isaku Yamahata <[email protected]>

The KVM/x86 handles machine-check in the guest specially. It sets up the guest so that vcpu exits from running guests, checks the exit reason and, manually raises the machine check by calling do_machine_check().

To test the KVM machine check execution path, KVM wants to inject the machine check in the context of vcpu instead of the context of the process of MCE injection. Wire up the MCE injection framework for KVM to trigger MCE in the vcpu context. Add a kvm vcpu debugfs entry for an operator to tell KVM to inject MCE.

But this isn't "injecting" a #MC, it's just having KVM call do_machine_check() before enabling IRQs after a VM-Exit. I don't see how that is interesting enough to warrant a dedicated knob and code in KVM's run loop.

Quoted 15 lines
arch/x86/kvm/x86.c +5 -0
@@ -10814,6 +10823,11 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu)
 		fpu_sync_guest_vmexit_xfd_state();
 
 	static_call(kvm_x86_handle_exit_irqoff)(vcpu);
+	if (unlikely(req_mce_inject)) {
+		mce_call_atomic_injector_chain(smp_processor_id());
+		kvm_machine_check();
+		mce_inject_unlock();
+	}
 
 	if (vcpu->arch.guest_fpu.xfd_err)
 		wrmsrl(MSR_IA32_XFD_ERR, 0);
Signature · 1 line
2.25.1