Skip to contentSkip to conversation

KVM: arm64: Selftest for pKVM transitions

PATCH In review Quentin Perret 2 participants 3 messages Nov 29, 2024 – Dec 20, 2024

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 KVM: arm64: Selftest for pKVM transitions

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         | 110 ++++++++++++++++++
 arch/arm64/kvm/hyp/nvhe/setup.c               |   2 +
 4 files changed, 128 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 0972faccc2af..a9b2677227cc 100644
@@ -90,4 +90,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 +110 -0

113 lines not on this page · raw message

arch/arm64/kvm/hyp/nvhe/setup.c +2 -0
index cbdd18cd3f98..d154e80fe6b9 100644
@@ -306,6 +306,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.47.0.338.g60cca15819-goog
maintainer
#1 lore raw
Quoted 6 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.

Thanks for doing this!

Quoted 4 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).

Hmm. What does a test failure look like without CONFIG_EL2_NVHE_DEBUG enabled? Do we still get file:line information?

[...]

Quoted 18 lines
arch/arm64/kvm/hyp/nvhe/mem_protect.c +18 -0
+void pkvm_ownership_selftest(void)
+{
+	void *virt = hyp_alloc_pages(&host_s2_pool, 0);
+	u64 phys, size, pfn;
+
+	WARN_ON(!virt);
+	selftest_page = hyp_virt_to_page(virt);
+	selftest_page->refcount = 0;
+
+	size = PAGE_SIZE << selftest_page->order;
+	phys = hyp_virt_to_phys(virt);
+	pfn = hyp_phys_to_pfn(phys);
+
+	selftest_state.host = PKVM_NOPAGE;
+	selftest_state.hyp = PKVM_PAGE_OWNED;
+	assert_page_state();
+	assert_transition_res(-EPERM,	__pkvm_host_donate_hyp, pfn, 1);
+	assert_transition_res(-EPERM,	__pkvm_host_share_hyp, pfn);

arch/arm64/kvm/hyp/nvhe/mem_protect.c int __pkvm_host_unshare_ffa(u64 pfn, u64 nr_pages) · line 1367

Quoted 10 lines
arch/arm64/kvm/hyp/nvhe/mem_protect.c +10 -0
+	assert_transition_res(-EPERM,	__pkvm_host_unshare_hyp, pfn);
+	assert_transition_res(-EPERM,	__pkvm_host_share_ffa, pfn, 1);
+	assert_transition_res(-EPERM,	__pkvm_host_unshare_ffa, pfn, 1);
+	assert_transition_res(-EPERM,	hyp_pin_shared_mem, virt, virt + size);
+
+	selftest_state.host = PKVM_PAGE_OWNED;
+	selftest_state.hyp = PKVM_NOPAGE;
+	assert_transition_res(0,	__pkvm_hyp_donate_host, pfn, 1);
+	assert_transition_res(-EPERM,	__pkvm_hyp_donate_host, pfn, 1);
+	assert_transition_res(-EPERM,	hyp_pin_shared_mem, virt, virt + size);

Should we recheck the unshare transitions here?

arch/arm64/kvm/hyp/nvhe/mem_protect.c int __pkvm_host_unshare_ffa(u64 pfn, u64 nr_pages) · line 1374

Quoted 6 lines
arch/arm64/kvm/hyp/nvhe/mem_protect.c +6 -0
+	selftest_state.host = PKVM_PAGE_SHARED_OWNED;
+	selftest_state.hyp = PKVM_PAGE_SHARED_BORROWED;
+	assert_transition_res(0,	__pkvm_host_share_hyp, pfn);
+	assert_transition_res(-EPERM,	__pkvm_host_share_hyp, pfn);
+	assert_transition_res(-EPERM,	__pkvm_host_donate_hyp, pfn, 1);
+	assert_transition_res(-EPERM,	__pkvm_host_share_ffa, pfn, 1);

We could check unshare_ffa here.

arch/arm64/kvm/hyp/nvhe/mem_protect.c int __pkvm_host_unshare_ffa(u64 pfn, u64 nr_pages) · line 1377

Quoted 3 lines
arch/arm64/kvm/hyp/nvhe/mem_protect.c +3 -0
+	assert_transition_res(-EPERM,	__pkvm_hyp_donate_host, pfn, 1);
+
+	assert_transition_res(0,	hyp_pin_shared_mem, virt, virt + size);

Is it worth trying an extra pin + unpin?

arch/arm64/kvm/hyp/nvhe/mem_protect.c int __pkvm_host_unshare_ffa(u64 pfn, u64 nr_pages) · line 1378

Quoted 1 line
arch/arm64/kvm/hyp/nvhe/mem_protect.c +1 -0
+	WARN_ON(!hyp_page_count(virt));

Can we assert an exact value (e.g. count == 1)?

arch/arm64/kvm/hyp/nvhe/mem_protect.c int __pkvm_host_unshare_ffa(u64 pfn, u64 nr_pages) · line 1382

Quoted 4 lines
arch/arm64/kvm/hyp/nvhe/mem_protect.c +4 -0
+	assert_transition_res(-EBUSY,	__pkvm_host_unshare_hyp, pfn);
+	assert_transition_res(-EPERM,	__pkvm_host_share_hyp, pfn);
+	assert_transition_res(-EPERM,	__pkvm_host_donate_hyp, pfn, 1);
+	assert_transition_res(-EPERM,	__pkvm_host_share_ffa, pfn, 1);

unshare_ffa again here.

arch/arm64/kvm/hyp/nvhe/mem_protect.c int __pkvm_host_unshare_ffa(u64 pfn, u64 nr_pages) · line 1391

Quoted 9 lines
arch/arm64/kvm/hyp/nvhe/mem_protect.c +9 -0
+	assert_transition_res(-EPERM,	__pkvm_hyp_donate_host, pfn, 1);
+
+	hyp_unpin_shared_mem(virt, virt + size);
+	assert_page_state();
+	WARN_ON(hyp_page_count(virt));
+	assert_transition_res(-EPERM,	__pkvm_host_share_hyp, pfn);
+	assert_transition_res(-EPERM,	__pkvm_host_donate_hyp, pfn, 1);
+	assert_transition_res(-EPERM,	__pkvm_host_share_ffa, pfn, 1);
+	assert_transition_res(-EPERM,	__pkvm_hyp_donate_host, pfn, 1);

Is this block actually testing anything new? Once we've asserted the page state and checked the refcount, the transitions seem redundant to me.

Quoted 7 lines
arch/arm64/kvm/hyp/nvhe/mem_protect.c +7 -0
+	selftest_state.host = PKVM_PAGE_OWNED;
+	selftest_state.hyp = PKVM_NOPAGE;
+	assert_transition_res(0,	__pkvm_host_unshare_hyp, pfn);
+
+	selftest_state.host = PKVM_PAGE_SHARED_OWNED;
+	selftest_state.hyp = PKVM_NOPAGE;
+	assert_transition_res(0,	__pkvm_host_share_ffa, pfn, 1);

arch/arm64/kvm/hyp/nvhe/mem_protect.c int __pkvm_host_unshare_ffa(u64 pfn, u64 nr_pages) · line 1409

Quoted 10 lines
arch/arm64/kvm/hyp/nvhe/mem_protect.c +10 -0
+	assert_transition_res(-EPERM,	__pkvm_host_share_ffa, pfn, 1);
+	assert_transition_res(-EPERM,	__pkvm_host_donate_hyp, pfn, 1);
+	assert_transition_res(-EPERM,	__pkvm_host_share_hyp, pfn);
+	assert_transition_res(-EPERM,	__pkvm_host_unshare_hyp, pfn);
+	assert_transition_res(-EPERM,	__pkvm_hyp_donate_host, pfn, 1);
+	assert_transition_res(-EPERM,	hyp_pin_shared_mem, virt, virt + size);
+
+	selftest_state.host = PKVM_PAGE_OWNED;
+	selftest_state.hyp = PKVM_NOPAGE;
+	assert_transition_res(0,	__pkvm_host_unshare_ffa, pfn, 1);

Try it twice?

Will

#2 lore raw
Quoted 11 lines · Will Deacon
Quoted 6 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.

Thanks for doing this!

Quoted 1 line
Quoted 6 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).

Hmm. What does a test failure look like without CONFIG_EL2_NVHE_DEBUG enabled? Do we still get file:line information?

Nope, sadly we don't, but if you get a failure after turning on that config then you can at least be confident there is a problem with your code :-)

As discussed in the other thread, once we get the hyp state into the vmemmap (I've got patches that do exactly that, happy to send them out) we could just plain remove the 'skip_pgtable_check()' logic, and then I'm not opposed to merging CONFIG_PKVM_SELFTESTS and CONFIG_EL2_NVHE_DEBUG together as the latter won't have the same side effects.

Quoted 22 lines
Quoted 22 lines
arch/arm64/kvm/hyp/nvhe/mem_protect.c +22 -0
+void pkvm_ownership_selftest(void)
+{
+	void *virt = hyp_alloc_pages(&host_s2_pool, 0);
+	u64 phys, size, pfn;
+
+	WARN_ON(!virt);
+	selftest_page = hyp_virt_to_page(virt);
+	selftest_page->refcount = 0;
+
+	size = PAGE_SIZE << selftest_page->order;
+	phys = hyp_virt_to_phys(virt);
+	pfn = hyp_phys_to_pfn(phys);
+
+	selftest_state.host = PKVM_NOPAGE;
+	selftest_state.hyp = PKVM_PAGE_OWNED;
+	assert_page_state();
+	assert_transition_res(-EPERM,	__pkvm_host_donate_hyp, pfn, 1);
+	assert_transition_res(-EPERM,	__pkvm_host_share_hyp, pfn);
+	assert_transition_res(-EPERM,	__pkvm_host_unshare_hyp, pfn);
+	assert_transition_res(-EPERM,	__pkvm_host_share_ffa, pfn, 1);
+	assert_transition_res(-EPERM,	__pkvm_host_unshare_ffa, pfn, 1);
+	assert_transition_res(-EPERM,	hyp_pin_shared_mem, virt, virt + size);
Quoted 8 lines

arch/arm64/kvm/hyp/nvhe/mem_protect.c int __pkvm_host_unshare_ffa(u64 pfn, u64 nr_pages) · line 1367

Quoted 6 lines
arch/arm64/kvm/hyp/nvhe/mem_protect.c +6 -0
+
+	selftest_state.host = PKVM_PAGE_OWNED;
+	selftest_state.hyp = PKVM_NOPAGE;
+	assert_transition_res(0,	__pkvm_hyp_donate_host, pfn, 1);
+	assert_transition_res(-EPERM,	__pkvm_hyp_donate_host, pfn, 1);
+	assert_transition_res(-EPERM,	hyp_pin_shared_mem, virt, virt + size);

Should we recheck the unshare transitions here?

Yep, sounds like a good idea.

Quoted 8 lines

arch/arm64/kvm/hyp/nvhe/mem_protect.c int __pkvm_host_unshare_ffa(u64 pfn, u64 nr_pages) · line 1374

Quoted 6 lines
arch/arm64/kvm/hyp/nvhe/mem_protect.c +6 -0
+	selftest_state.host = PKVM_PAGE_SHARED_OWNED;
+	selftest_state.hyp = PKVM_PAGE_SHARED_BORROWED;
+	assert_transition_res(0,	__pkvm_host_share_hyp, pfn);
+	assert_transition_res(-EPERM,	__pkvm_host_share_hyp, pfn);
+	assert_transition_res(-EPERM,	__pkvm_host_donate_hyp, pfn, 1);
+	assert_transition_res(-EPERM,	__pkvm_host_share_ffa, pfn, 1);

We could check unshare_ffa here.

Sadly no, unshare_ffa() isn't super smart, it'll only check that a page is SHARED_OWNED from the host's PoV and turn it to PAGE_OWNED. So we'd break the state in this case :/

Quoted 5 lines

arch/arm64/kvm/hyp/nvhe/mem_protect.c int __pkvm_host_unshare_ffa(u64 pfn, u64 nr_pages) · line 1377

Quoted 3 lines
arch/arm64/kvm/hyp/nvhe/mem_protect.c +3 -0
+	assert_transition_res(-EPERM,	__pkvm_hyp_donate_host, pfn, 1);
+
+	assert_transition_res(0,	hyp_pin_shared_mem, virt, virt + size);

Is it worth trying an extra pin + unpin?

You mean doing pin > pin > unpin > sanity checks > unpin ?

I guess that would test that we can indeed stack the pins, so sounds like a good idea too.

Quoted 3 lines

arch/arm64/kvm/hyp/nvhe/mem_protect.c int __pkvm_host_unshare_ffa(u64 pfn, u64 nr_pages) · line 1378

Quoted 1 line
arch/arm64/kvm/hyp/nvhe/mem_protect.c +1 -0
+	WARN_ON(!hyp_page_count(virt));

Can we assert an exact value (e.g. count == 1)?

Sure.

Quoted 6 lines

arch/arm64/kvm/hyp/nvhe/mem_protect.c int __pkvm_host_unshare_ffa(u64 pfn, u64 nr_pages) · line 1382

Quoted 4 lines
arch/arm64/kvm/hyp/nvhe/mem_protect.c +4 -0
+	assert_transition_res(-EBUSY,	__pkvm_host_unshare_hyp, pfn);
+	assert_transition_res(-EPERM,	__pkvm_host_share_hyp, pfn);
+	assert_transition_res(-EPERM,	__pkvm_host_donate_hyp, pfn, 1);
+	assert_transition_res(-EPERM,	__pkvm_host_share_ffa, pfn, 1);

unshare_ffa again here.

Same as above.

Quoted 4 lines
Quoted 4 lines
arch/arm64/kvm/hyp/nvhe/mem_protect.c +4 -0
+	assert_transition_res(-EPERM,	__pkvm_hyp_donate_host, pfn, 1);
+
+	hyp_unpin_shared_mem(virt, virt + size);
+	assert_page_state();
Quoted 8 lines

arch/arm64/kvm/hyp/nvhe/mem_protect.c int __pkvm_host_unshare_ffa(u64 pfn, u64 nr_pages) · line 1391

Quoted 5 lines
arch/arm64/kvm/hyp/nvhe/mem_protect.c +5 -0
+	WARN_ON(hyp_page_count(virt));
+	assert_transition_res(-EPERM,	__pkvm_host_share_hyp, pfn);
+	assert_transition_res(-EPERM,	__pkvm_host_donate_hyp, pfn, 1);
+	assert_transition_res(-EPERM,	__pkvm_host_share_ffa, pfn, 1);
+	assert_transition_res(-EPERM,	__pkvm_hyp_donate_host, pfn, 1);

Is this block actually testing anything new? Once we've asserted the page state and checked the refcount, the transitions seem redundant to me.

The idea was to check if the previous transitions had other sad effects not visible through the <state, refcount> pair that would cause problems. But I guess that is a bit far fetched so happy to remove that part.

Quoted 11 lines
Quoted 11 lines
arch/arm64/kvm/hyp/nvhe/mem_protect.c +11 -0
+	selftest_state.host = PKVM_PAGE_OWNED;
+	selftest_state.hyp = PKVM_NOPAGE;
+	assert_transition_res(0,	__pkvm_host_unshare_hyp, pfn);
+
+	selftest_state.host = PKVM_PAGE_SHARED_OWNED;
+	selftest_state.hyp = PKVM_NOPAGE;
+	assert_transition_res(0,	__pkvm_host_share_ffa, pfn, 1);
+	assert_transition_res(-EPERM,	__pkvm_host_share_ffa, pfn, 1);
+	assert_transition_res(-EPERM,	__pkvm_host_donate_hyp, pfn, 1);
+	assert_transition_res(-EPERM,	__pkvm_host_share_hyp, pfn);
+	assert_transition_res(-EPERM,	__pkvm_host_unshare_hyp, pfn);
Quoted 8 lines

arch/arm64/kvm/hyp/nvhe/mem_protect.c int __pkvm_host_unshare_ffa(u64 pfn, u64 nr_pages) · line 1409

Quoted 6 lines
arch/arm64/kvm/hyp/nvhe/mem_protect.c +6 -0
+	assert_transition_res(-EPERM,	__pkvm_hyp_donate_host, pfn, 1);
+	assert_transition_res(-EPERM,	hyp_pin_shared_mem, virt, virt + size);
+
+	selftest_state.host = PKVM_PAGE_OWNED;
+	selftest_state.hyp = PKVM_NOPAGE;
+	assert_transition_res(0,	__pkvm_host_unshare_ffa, pfn, 1);

Try it twice?

Sg.

Thanks!
Quentin