From: Wei Hu <[email protected]>
Add an SNP panic-path page-unlock feature so a root partition running
confidential (SEV-SNP) guests can still collect a Linux root vmcore via
kexec after a crash. On panic, guest pages of every encrypted partition
are unmapped and shared back to the host so kexec can read them.
Register a panic notifier that walks mshv_root.pt_htable and, for each
encrypted partition, unmaps each memory region and marks it shared again.
The notifier is only registered when the hypervisor's own crash path is
not active (hv_crash_enabled) and only built when CONFIG_CRASH_DUMP is
enabled. Declare hv_crash_enabled in asm/mshyperv.h alongside the other
MSHV crashdump symbols.
Tested on a SEV-SNP Dom0: the notifier only runs in its intended fallback
path, i.e. when the hypervisor's own kdump support is inactive
(hv_crash_enabled == false); when it is active the hypervisor stops the
CPUs and kexecs before the Linux panic_notifier_list runs. To exercise the
ported code that fallback was reproduced on a test kernel (hypervisor crash
init disabled so hv_crash_enabled == false). With a confidential guest
running, an intentional Dom0 panic showed the notifier fire, find the
encrypted partition, and unlock its regions with no errors ("SNP pages are
unlocked for panic", no "Unlock snp failed"), after which kdump collected a
valid, readable vmcore. The default active-hypervisor path was also
confirmed to boot cleanly with the notifier correctly not registered.
Signed-off-by: Wei Hu <[email protected]>
---
arch/x86/include/asm/mshyperv.h | 1 +
drivers/hv/mshv_root_main.c | 71 +++++++++++++++++++++++++++++++++
2 files changed, 72 insertions(+)
diff --git a/arch/x86/include/asm/mshyperv.h b/arch/x86/include/asm/mshyperv.h
index f64393e853ee..052cc44ae1f0 100644
--- a/arch/x86/include/asm/mshyperv.h
+++ b/arch/x86/include/asm/mshyperv.h
@@ -240,6 +240,7 @@ static __always_inline u64 hv_raw_get_msr(unsigned int reg)
int hv_apicid_to_vp_index(u32 apic_id);
#if IS_ENABLED(CONFIG_MSHV_ROOT) && IS_ENABLED(CONFIG_CRASH_DUMP)
+extern bool hv_crash_enabled;
void hv_root_crash_init(void);
void hv_crash_asm32(void);
void hv_crash_asm64(void);
diff --git a/drivers/hv/mshv_root_main.c b/drivers/hv/mshv_root_main.c
index 0fbd2158968d..4d08d547704e 100644
--- a/drivers/hv/mshv_root_main.c
+++ b/drivers/hv/mshv_root_main.c
@@ -2898,6 +2898,74 @@ static int __init mshv_init_vmm_caps(struct device *dev)
return 0;
}
+#if defined(CONFIG_X86_64) && IS_ENABLED(CONFIG_CRASH_DUMP)
+static void mshv_panic_unlock_snp(struct mshv_partition *vm)
+{
+ struct mshv_mem_region *memreg;
+ int ret;
+
+ hlist_for_each_entry(memreg, &vm->pt_mem_regions, hnode) {
+ mshv_region_unmap(memreg);
+ ret = mshv_region_share(memreg);
+ if (ret)
+ pt_err(vm, "Unlock snp failed. ret:0x%x gfn:%llx
numpfns:%lld\n",
+ ret, memreg->start_gfn, memreg->nr_pages);
+ }
+}
+
+static int mshv_root_panic_cb(struct notifier_block *this, unsigned long event,
+ void *ptr)
+{
+ int i, done = 0;
+ struct mshv_partition *pt;
+ struct device *dev = NULL;
+
+ hash_for_each_rcu(mshv_root.pt_htable, i, pt, pt_hnode) {
+ if (!mshv_partition_encrypted(pt))
+ continue;
+
+ done = 1;
+ mshv_panic_unlock_snp(pt);
+ dev = pt->pt_module_dev;
+ }
+ if (done && dev)
+ dev_info(dev, "SNP pages are unlocked for panic\n");
+
+ return NOTIFY_DONE;
+}
+
+static struct notifier_block mshv_root_panic_blk = {
+ .notifier_call = mshv_root_panic_cb,
+};
+
+/*
+ * If mshv devirt setup failed during boot, or the feature itself is not
+ * available, allow the system to at least collect linux root vmcore. For
+ * that, snp guest pages must be made readable in the panic path so kexec can
+ * collect them.
+ */
+static void mshv_crashdump_init(void)
+{
+ if (hv_crash_enabled)
+ return;
+
+ atomic_notifier_chain_register(&panic_notifier_list,
+ &mshv_root_panic_blk);
+}
+
+static void mshv_crashdump_deinit(void)
+{
+ if (hv_crash_enabled)
+ return;
+
+ atomic_notifier_chain_unregister(&panic_notifier_list,
+ &mshv_root_panic_blk);
+}
+#else
+static void mshv_crashdump_init(void) {}
+static void mshv_crashdump_deinit(void) {}
+#endif
+
static int __init mshv_parent_partition_init(void)
{
int ret;
@@ -2957,6 +3025,8 @@ static int __init mshv_parent_partition_init(void)
hv_setup_mshv_handler(mshv_isr);
+ mshv_crashdump_init();
+
return 0;
exit_debugfs:
@@ -2972,6 +3042,7 @@ static int __init mshv_parent_partition_init(void)
static void __exit mshv_parent_partition_exit(void)
{
+ mshv_crashdump_deinit();
hv_setup_mshv_handler(NULL);
mshv_port_table_fini();
mshv_debugfs_exit();
--
2.43.0