Skip to contentSkip to conversation

Selftest for pKVM ownership transitions

PATCH Superseded Quentin Perret 2 participants 10 messages Feb 25, 2025 – Feb 26, 2025

Subsystems (2)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

v2 cover Selftest for pKVM ownership transitions

Hi all,

This is the v2 of the pKVM selftest originally posted here [1].

Changes since v1:
- added support for testing NP guest transitions;
- added .data section in pKVM to allow using non-zero globals (needed for previous point);
- dropped superfluous WARN() from __pkvm_host_share_guest() which is a pain for testing;
- updated assertions for hyp/host transitions according to the discussion on v1.

Patches based on v6.14-rc4, tested on Qemu.

[1] https://lore.kernel.org/kvmarm/[email protected]/

David Brazdil (1):
KVM: arm64: Add .hyp.data section

Quentin Perret (3):
KVM: arm64: Don't WARN from __pkvm_host_share_guest()
KVM: arm64: Selftest for pKVM transitions
KVM: arm64: Extend pKVM selftest for np-guests

 arch/arm64/include/asm/kvm_pkvm.h             |   6 +
 arch/arm64/include/asm/sections.h             |   1 +
 arch/arm64/kernel/image-vars.h                |   2 +
 arch/arm64/kernel/vmlinux.lds.S               |  18 +-
 arch/arm64/kvm/Kconfig                        |  10 +
 arch/arm64/kvm/arm.c                          |   7 +
 arch/arm64/kvm/hyp/include/nvhe/mem_protect.h |   6 +
 arch/arm64/kvm/hyp/nvhe/hyp.lds.S             |   2 +
 arch/arm64/kvm/hyp/nvhe/mem_protect.c         | 198 +++++++++++++++++-
 arch/arm64/kvm/hyp/nvhe/setup.c               |  12 ++
 arch/arm64/kvm/pkvm.c                         |   2 +
 11 files changed, 260 insertions(+), 4 deletions(-)
Signature · 1 line
2.48.1.658.g4767266eb4-goog
#1 lore raw

PATCH v2 1/4 KVM: arm64: Add .hyp.data section

From: David Brazdil <[email protected]>

The hypervisor has not needed its own .data section because all globals were either .rodata or .bss. To avoid having to initialize future data-structures at run-time, let's introduce add a .data section to the hypervisor.

---
 arch/arm64/include/asm/sections.h |  1 +
 arch/arm64/kernel/image-vars.h    |  2 ++
 arch/arm64/kernel/vmlinux.lds.S   | 18 +++++++++++++++---
 arch/arm64/kvm/arm.c              |  7 +++++++
 arch/arm64/kvm/hyp/nvhe/hyp.lds.S |  2 ++
 arch/arm64/kvm/hyp/nvhe/setup.c   |  4 ++++
 arch/arm64/kvm/pkvm.c             |  1 +
 7 files changed, 32 insertions(+), 3 deletions(-)
arch/arm64/include/asm/sections.h +1 -0
index 40971ac1303f..51b0d594239e 100644
@@ -11,6 +11,7 @@ extern char __alt_instructions[], __alt_instructions_end[];
 extern char __hibernate_exit_text_start[], __hibernate_exit_text_end[];
 extern char __hyp_idmap_text_start[], __hyp_idmap_text_end[];
 extern char __hyp_text_start[], __hyp_text_end[];
+extern char __hyp_data_start[], __hyp_data_end[];
 extern char __hyp_rodata_start[], __hyp_rodata_end[];
 extern char __hyp_reloc_begin[], __hyp_reloc_end[];
 extern char __hyp_bss_start[], __hyp_bss_end[];
arch/arm64/kernel/image-vars.h +2 -0
index ef3a69cc398e..7c675e61ae58 100644
@@ -135,6 +135,8 @@ KVM_NVHE_ALIAS(__hyp_text_start);
 KVM_NVHE_ALIAS(__hyp_text_end);
 KVM_NVHE_ALIAS(__hyp_bss_start);
 KVM_NVHE_ALIAS(__hyp_bss_end);
+KVM_NVHE_ALIAS(__hyp_data_start);
+KVM_NVHE_ALIAS(__hyp_data_end);
 KVM_NVHE_ALIAS(__hyp_rodata_start);
 KVM_NVHE_ALIAS(__hyp_rodata_end);
 
arch/arm64/kernel/vmlinux.lds.S +15 -3
index e73326bd3ff7..7c770053f072 100644
@@ -13,7 +13,7 @@
 	*(__kvm_ex_table)					\
 	__stop___kvm_ex_table = .;
 
-#define HYPERVISOR_DATA_SECTIONS				\
+#define HYPERVISOR_RODATA_SECTIONS				\
 	HYP_SECTION_NAME(.rodata) : {				\
 		. = ALIGN(PAGE_SIZE);				\
 		__hyp_rodata_start = .;				\
@@ -23,6 +23,15 @@
 		__hyp_rodata_end = .;				\
 	}
 
+#define HYPERVISOR_DATA_SECTION					\
+	HYP_SECTION_NAME(.data) : {				\
+		. = ALIGN(PAGE_SIZE);				\
+		__hyp_data_start = .;				\
+		*(HYP_SECTION_NAME(.data))			\
+		. = ALIGN(PAGE_SIZE);				\
+		__hyp_data_end = .;				\
+	}
+
 #define HYPERVISOR_PERCPU_SECTION				\
 	. = ALIGN(PAGE_SIZE);					\
 	HYP_SECTION_NAME(.data..percpu) : {			\
@@ -51,7 +60,8 @@
 #define SBSS_ALIGN			PAGE_SIZE
 #else /* CONFIG_KVM */
 #define HYPERVISOR_EXTABLE
-#define HYPERVISOR_DATA_SECTIONS
+#define HYPERVISOR_RODATA_SECTIONS
+#define HYPERVISOR_DATA_SECTION
 #define HYPERVISOR_PERCPU_SECTION
 #define HYPERVISOR_RELOC_SECTION
 #define SBSS_ALIGN			0
@@ -190,7 +200,7 @@ SECTIONS
 	/* everything from this point to __init_begin will be marked RO NX */
 	RO_DATA(PAGE_SIZE)
 
-	HYPERVISOR_DATA_SECTIONS
+	HYPERVISOR_RODATA_SECTIONS
 
 	.got : { *(.got) }
 	/*
@@ -295,6 +305,8 @@ SECTIONS
 	_sdata = .;
 	RW_DATA(L1_CACHE_BYTES, PAGE_SIZE, THREAD_ALIGN)
 
+	HYPERVISOR_DATA_SECTION
+
 	/*
 	 * Data written with the MMU off but read with the MMU on requires
 	 * cache lines to be invalidated, discarding up to a Cache Writeback
arch/arm64/kvm/arm.c +7 -0
index b8e55a441282..94d23b901b66 100644
@@ -2568,6 +2568,13 @@ static int __init init_hyp_mode(void)
 		goto out_err;
 	}
 
+	err = create_hyp_mappings(kvm_ksym_ref(__hyp_data_start),
+				  kvm_ksym_ref(__hyp_data_end), PAGE_HYP);
+	if (err) {
+		kvm_err("Cannot map .hyp.data section\n");
+		goto out_err;
+	}
+
 	err = create_hyp_mappings(kvm_ksym_ref(__hyp_rodata_start),
 				  kvm_ksym_ref(__hyp_rodata_end), PAGE_HYP_RO);
 	if (err) {
arch/arm64/kvm/hyp/nvhe/hyp.lds.S +2 -0
index f4562f417d3f..d724f6d69302 100644
@@ -25,5 +25,7 @@ SECTIONS {
 	BEGIN_HYP_SECTION(.data..percpu)
 		PERCPU_INPUT(L1_CACHE_BYTES)
 	END_HYP_SECTION
+
 	HYP_SECTION(.bss)
+	HYP_SECTION(.data)
 }
arch/arm64/kvm/hyp/nvhe/setup.c +4 -0
index d62bcb5634a2..46d9bd04348f 100644
@@ -119,6 +119,10 @@ static int recreate_hyp_mappings(phys_addr_t phys, unsigned long size,
 	if (ret)
 		return ret;
 
+	ret = pkvm_create_mappings(__hyp_data_start, __hyp_data_end, PAGE_HYP);
+	if (ret)
+		return ret;
+
 	ret = pkvm_create_mappings(__hyp_rodata_start, __hyp_rodata_end, PAGE_HYP_RO);
 	if (ret)
 		return ret;
arch/arm64/kvm/pkvm.c +1 -0
index 930b677eb9b0..5a75f9554e57 100644
@@ -259,6 +259,7 @@ static int __init finalize_pkvm(void)
 	 * at, which would end badly once inaccessible.
 	 */
 	kmemleak_free_part(__hyp_bss_start, __hyp_bss_end - __hyp_bss_start);
+	kmemleak_free_part(__hyp_data_start, __hyp_data_end - __hyp_data_start);
 	kmemleak_free_part(__hyp_rodata_start, __hyp_rodata_end - __hyp_rodata_start);
 	kmemleak_free_part_phys(hyp_mem_base, hyp_mem_size);
 
Signature · 1 line
2.48.1.658.g4767266eb4-goog
#2 lore raw

PATCH v2 2/4 KVM: arm64: Don't WARN from __pkvm_host_share_guest()

In reply to Quentin Perret

We currently WARN() if the host attempts to share a page that is not in an acceptable state with a guest. This isn't strictly necessary and makes testing much harder, so drop the WARN and fix the error code.

---
 arch/arm64/kvm/hyp/nvhe/mem_protect.c | 1 -
 1 file changed, 1 deletion(-)
arch/arm64/kvm/hyp/nvhe/mem_protect.c +0 -1
index 19c3c631708c..ae39abc7e604 100644
@@ -912,7 +912,6 @@ int __pkvm_host_share_guest(u64 pfn, u64 gfn, struct pkvm_hyp_vcpu *vcpu,
 		if (page->host_share_guest_count)
 			break;
 		/* Only host to np-guest multi-sharing is tolerated */
-		WARN_ON(1);
 		fallthrough;
 	default:
 		ret = -EPERM;
Signature · 1 line
2.48.1.658.g4767266eb4-goog
maintainer
#3 lore raw

Re: [PATCH v2 2/4] KVM: arm64: Don't WARN from __pkvm_host_share_guest()

Quoted 4 lines · Quentin Perret

We currently WARN() if the host attempts to share a page that is not in an acceptable state with a guest. This isn't strictly necessary and makes testing much harder, so drop the WARN and fix the error code.

Are you really fixing the error code? You still seem to return a -EPERM. I guess this was never reachable thanks to WARN() being a panic with pKVM?

	M.
Quoted 22 lines
---
 arch/arm64/kvm/hyp/nvhe/mem_protect.c | 1 -
 1 file changed, 1 deletion(-)
arch/arm64/kvm/hyp/nvhe/mem_protect.c +0 -1
index 19c3c631708c..ae39abc7e604 100644
@@ -912,7 +912,6 @@ int __pkvm_host_share_guest(u64 pfn, u64 gfn, struct pkvm_hyp_vcpu *vcpu,
 		if (page->host_share_guest_count)
 			break;
 		/* Only host to np-guest multi-sharing is tolerated */
-		WARN_ON(1);
 		fallthrough;
 	default:
 		ret = -EPERM;
Signature · 1 line
2.48.1.658.g4767266eb4-goog
Signature · 1 line
Without deviation from the norm, progress is not possible.
#4 lore raw

Re: [PATCH v2 2/4] KVM: arm64: Don't WARN from __pkvm_host_share_guest()

Quoted 10 lines · Marc Zyngier
Quoted 4 lines · Quentin Perret

We currently WARN() if the host attempts to share a page that is not in an acceptable state with a guest. This isn't strictly necessary and makes testing much harder, so drop the WARN and fix the error code.

Are you really fixing the error code? You still seem to return a -EPERM. I guess this was never reachable thanks to WARN() being a panic with pKVM?

Exactly, this is really poor wording in the commit message. 'Fix the error code' in this case was intended to mean 'make sure to return the error code properly instead outright crashing the device'.

Happy to send out a v3 with a better commit message.

Thanks!
Quentin

maintainer
#5 lore raw

Re: [PATCH v2 2/4] KVM: arm64: Don't WARN from __pkvm_host_share_guest()

Quoted 13 lines · Quentin Perret
Quoted 10 lines · Marc Zyngier
Quoted 4 lines · Quentin Perret

We currently WARN() if the host attempts to share a page that is not in an acceptable state with a guest. This isn't strictly necessary and makes testing much harder, so drop the WARN and fix the error code.

Are you really fixing the error code? You still seem to return a -EPERM. I guess this was never reachable thanks to WARN() being a panic with pKVM?

Quoted 5 lines

Exactly, this is really poor wording in the commit message. 'Fix the error code' in this case was intended to mean 'make sure to return the error code properly instead outright crashing the device'.

Happy to send out a v3 with a better commit message.

Nah, that's probably something Oliver can fixup when picking up the patch.

Thanks,

	M.
Signature · 1 line
Without deviation from the norm, progress is not possible.
#6 lore raw

PATCH v2 3/4 KVM: arm64: Selftest for pKVM transitions

In reply to Quentin Perret

We have recently found a bug [1] in the pKVM memory ownership transitions by code inspection, but it could have been caught with a test.

Introduce a boot-time selftest exercising all the known pKVM memory transitions and importantly checks the rejection of illegal transitions.

The new test is hidden behind a new Kconfig option separate from CONFIG_EL2_NVHE_DEBUG on purpose as that has side effects on the transition checks ([1] doesn't reproduce with EL2 debug enabled).

[1] https://lore.kernel.org/kvmarm/[email protected]/

---
 arch/arm64/kvm/Kconfig                        |  10 ++
 arch/arm64/kvm/hyp/include/nvhe/mem_protect.h |   6 +
 arch/arm64/kvm/hyp/nvhe/mem_protect.c         | 111 ++++++++++++++++++
 arch/arm64/kvm/hyp/nvhe/setup.c               |   2 +
 4 files changed, 129 insertions(+)
arch/arm64/kvm/Kconfig +10 -0
index ead632ad01b4..038d7f52232c 100644
@@ -46,6 +46,7 @@ menuconfig KVM
 config NVHE_EL2_DEBUG
 	bool "Debug mode for non-VHE EL2 object"
 	depends on KVM
+	select PKVM_SELFTESTS
 	help
 	  Say Y here to enable the debug mode for the non-VHE KVM EL2 object.
 	  Failure reports will BUG() in the hypervisor. This is intended for
@@ -53,6 +54,15 @@ config NVHE_EL2_DEBUG
 
 	  If unsure, say N.
 
+config PKVM_SELFTESTS
+	bool "Protected KVM hypervisor selftests"
+	help
+	  Say Y here to enable Protected KVM (pKVM) hypervisor selftests
+	  during boot. Failure reports will panic the hypervisor. This is
+	  intended for EL2 hypervisor development.
+
+	  If unsure, say N.
+
 config PROTECTED_NVHE_STACKTRACE
 	bool "Protected KVM hypervisor stacktraces"
 	depends on NVHE_EL2_DEBUG
arch/arm64/kvm/hyp/include/nvhe/mem_protect.h +6 -0
index 978f38c386ee..31a3f2cdf242 100644
@@ -67,4 +67,10 @@ static __always_inline void __load_host_stage2(void)
 	else
 		write_sysreg(0, vttbr_el2);
 }
+
+#ifdef CONFIG_PKVM_SELFTESTS
+void pkvm_ownership_selftest(void);
+#else
+static inline void pkvm_ownership_selftest(void) { }
+#endif
 #endif /* __KVM_NVHE_MEM_PROTECT__ */
arch/arm64/kvm/hyp/nvhe/mem_protect.c +111 -0

114 lines not on this page · raw message

arch/arm64/kvm/hyp/nvhe/setup.c +2 -0
index 46d9bd04348f..54006f959e1b 100644
@@ -308,6 +308,8 @@ void __noreturn __pkvm_init_finalise(void)
 		goto out;
 
 	pkvm_hyp_vm_table_init(vm_table_base);
+
+	pkvm_ownership_selftest();
 out:
 	/*
 	 * We tail-called to here from handle___pkvm_init() and will not return,
Signature · 1 line
2.48.1.658.g4767266eb4-goog
maintainer
#7 lore raw

Re: [PATCH v2 3/4] KVM: arm64: Selftest for pKVM transitions

Quoted 11 lines · Quentin Perret

We have recently found a bug [1] in the pKVM memory ownership transitions by code inspection, but it could have been caught with a test.

Introduce a boot-time selftest exercising all the known pKVM memory transitions and importantly checks the rejection of illegal transitions.

The new test is hidden behind a new Kconfig option separate from CONFIG_EL2_NVHE_DEBUG on purpose as that has side effects on the transition checks ([1] doesn't reproduce with EL2 debug enabled).

That's a bit annoying, isn't it? Without EL2_DEBUG selected, you won't get any stacktrace, and the WARN_ON()s are a guaranteed panic. Yes, this is better than nothing, but I'm a bit worried this is going to be hard to use.

Is there a way to reduce the impact the EL2 debug has on the rest of the code? It feels like it is more invasive than it should be...

Thanks,

	M.
Signature · 1 line
Without deviation from the norm, progress is not possible.
#8 lore raw

Re: [PATCH v2 3/4] KVM: arm64: Selftest for pKVM transitions

Quoted 10 lines · Marc Zyngier
Quoted 8 lines · Quentin Perret

We have recently found a bug [1] in the pKVM memory ownership transitions by code inspection, but it could have been caught with a test.

Introduce a boot-time selftest exercising all the known pKVM memory transitions and importantly checks the rejection of illegal transitions.

Quoted 8 lines
Quoted 3 lines

The new test is hidden behind a new Kconfig option separate from CONFIG_EL2_NVHE_DEBUG on purpose as that has side effects on the transition checks ([1] doesn't reproduce with EL2 debug enabled).

That's a bit annoying, isn't it? Without EL2_DEBUG selected, you won't get any stacktrace, and the WARN_ON()s are a guaranteed panic. Yes, this is better than nothing, but I'm a bit worried this is going to be hard to use.

Right, so you _can_ enable EL2_DEBUG on top of the selftest stuff, and if you're not hitting one of those hard-to-find bugs described in the commit message above, then you're golden. In practice I suspect that if enabling the selftest alone leads to a panic, the next logical step is to enable EL2_DEBUG and see what you get. If enabling EL2_DEBUG makes the issue go away, then that'll require digging a bit deeper, but that should be pretty rare I presume.

Quoted 2 lines

Is there a way to reduce the impact the EL2 debug has on the rest of the code? It feels like it is more invasive than it should be...

Turns out I have a WiP series that moves the hypervisor ownership state to the hyp_vmemmap, similar to what we did for the host ownership. A nice property of that is that hyp state lookups become really cheap, no page-table walks required. So we could probably afford to drop the EL2_DEBUG ifdefery in host_share_hyp() and friends, and just unconditionally cross-check the hyp state on all transitions where it is involved. And with that we should probably just fold the pkvm selftest under EL2_DEBUG and call it a day. Would that work?

Thanks,
Quentin

#9 lore raw

PATCH v2 4/4 KVM: arm64: Extend pKVM selftest for np-guests

In reply to Quentin Perret

The pKVM selftest intends to test as many memory 'transitions' as possible, so extend it to cover sharing pages with non-protected guests, including in the case of multi-sharing.

---
 arch/arm64/include/asm/kvm_pkvm.h             |  6 ++
 arch/arm64/kvm/hyp/include/nvhe/mem_protect.h |  4 +-
 arch/arm64/kvm/hyp/nvhe/mem_protect.c         | 90 ++++++++++++++++++-
 arch/arm64/kvm/hyp/nvhe/setup.c               |  8 +-
 arch/arm64/kvm/pkvm.c                         |  1 +
 5 files changed, 104 insertions(+), 5 deletions(-)
arch/arm64/include/asm/kvm_pkvm.h +6 -0
index eb65f12e81d9..104b6b5ab6f5 100644
@@ -134,6 +134,12 @@ static inline unsigned long host_s2_pgtable_pages(void)
 	return res;
 }
 
+#ifdef CONFIG_PKVM_SELFTESTS
+static inline unsigned long pkvm_selftest_pages(void) { return 32; }
+#else
+static inline unsigned long pkvm_selftest_pages(void) { return 0; }
+#endif
+
 #define KVM_FFA_MBOX_NR_PAGES	1
 
 static inline unsigned long hyp_ffa_proxy_pages(void)
arch/arm64/kvm/hyp/include/nvhe/mem_protect.h +2 -2
index 31a3f2cdf242..dd53af947a58 100644
@@ -69,8 +69,8 @@ static __always_inline void __load_host_stage2(void)
 }
 
 #ifdef CONFIG_PKVM_SELFTESTS
-void pkvm_ownership_selftest(void);
+void pkvm_ownership_selftest(void *base);
 #else
-static inline void pkvm_ownership_selftest(void) { }
+static inline void pkvm_ownership_selftest(void *base) { }
 #endif
 #endif /* __KVM_NVHE_MEM_PROTECT__ */
arch/arm64/kvm/hyp/nvhe/mem_protect.c +88 -2

167 lines not on this page · raw message

arch/arm64/kvm/hyp/nvhe/setup.c +7 -1
index 54006f959e1b..814548134a83 100644
@@ -28,6 +28,7 @@ static void *vmemmap_base;
 static void *vm_table_base;
 static void *hyp_pgt_base;
 static void *host_s2_pgt_base;
+static void *selftest_base;
 static void *ffa_proxy_pages;
 static struct kvm_pgtable_mm_ops pkvm_pgtable_mm_ops;
 static struct hyp_pool hpool;
@@ -38,6 +39,11 @@ static int divide_memory_pool(void *virt, unsigned long size)
 
 	hyp_early_alloc_init(virt, size);
 
+	nr_pages = pkvm_selftest_pages();
+	selftest_base = hyp_early_alloc_contig(nr_pages);
+	if (nr_pages && !selftest_base)
+		return -ENOMEM;
+
 	nr_pages = hyp_vmemmap_pages(sizeof(struct hyp_page));
 	vmemmap_base = hyp_early_alloc_contig(nr_pages);
 	if (!vmemmap_base)
@@ -309,7 +315,7 @@ void __noreturn __pkvm_init_finalise(void)
 
 	pkvm_hyp_vm_table_init(vm_table_base);
 
-	pkvm_ownership_selftest();
+	pkvm_ownership_selftest(selftest_base);
 out:
 	/*
 	 * We tail-called to here from handle___pkvm_init() and will not return,
arch/arm64/kvm/pkvm.c +1 -0
index 5a75f9554e57..728ae5f44da3 100644
@@ -79,6 +79,7 @@ void __init kvm_hyp_reserve(void)
 	hyp_mem_pages += host_s2_pgtable_pages();
 	hyp_mem_pages += hyp_vm_table_pages();
 	hyp_mem_pages += hyp_vmemmap_pages(STRUCT_HYP_PAGE_SIZE);
+	hyp_mem_pages += pkvm_selftest_pages();
 	hyp_mem_pages += hyp_ffa_proxy_pages();
 
 	/*
Signature · 1 line
2.48.1.658.g4767266eb4-goog