Selftest for pKVM ownership transitions
v3 cover Selftest for pKVM ownership transitions
Hi all,
This is the v3 of the pKVM selftest series, v2 was here [1]. This series depends on [2] which moves the hyp tracking to the vmemmap. A full stack on top of v6.15-rc2 with everything applied is available here:
https://android-kvm.googlesource.com/linux qperret/pkvm-selftest-v3
Changes since v2:
- rebased on top of v2 of the hyp-state-in-vmemmap series [2];
- thanks to above rebase, dropped the PKVM_SELFTEST kconfig option and folded the selftest under the existing NVHE_EL2_DEBUG;
- fixed commit message of patch 02
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.
[1] https://lore.kernel.org/kvmarm/[email protected]/
[2] https://lore.kernel.org/kvmarm/[email protected]/
Thanks,
Quentin
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/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 + 10 files changed, 250 insertions(+), 4 deletions(-)
Signature · 1 line
2.49.0.604.gff1f9ca942-goog
PATCH v3 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
@@ -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
@@ -131,6 +131,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
@@ -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
@@ -2604,6 +2604,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
@@ -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
@@ -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
@@ -262,6 +262,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.49.0.604.gff1f9ca942-goog
PATCH v3 2/4 KVM: arm64: Don't WARN from __pkvm_host_share_guest()
In reply to Quentin PerretWe 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 make sure to propage the error code instead.
--- arch/arm64/kvm/hyp/nvhe/mem_protect.c | 1 - 1 file changed, 1 deletion(-)
arch/arm64/kvm/hyp/nvhe/mem_protect.c +0 -1
@@ -921,7 +921,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.49.0.604.gff1f9ca942-goog
PATCH v3 3/4 KVM: arm64: Selftest for pKVM transitions
In reply to Quentin PerretWe 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/hyp/include/nvhe/mem_protect.h | 6 + arch/arm64/kvm/hyp/nvhe/mem_protect.c | 111 ++++++++++++++++++ arch/arm64/kvm/hyp/nvhe/setup.c | 2 + 3 files changed, 119 insertions(+)
arch/arm64/kvm/hyp/include/nvhe/mem_protect.h +6 -0
@@ -67,4 +67,10 @@ static __always_inline void __load_host_stage2(void) else write_sysreg(0, vttbr_el2); } + +#ifdef CONFIG_NVHE_EL2_DEBUG +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
@@ -312,6 +312,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.49.0.604.gff1f9ca942-goog
PATCH v3 4/4 KVM: arm64: Extend pKVM selftest for np-guests
In reply to Quentin PerretThe 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
@@ -135,6 +135,12 @@ static inline unsigned long host_s2_pgtable_pages(void)return res; } +#ifdef CONFIG_NVHE_EL2_DEBUG +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 1static inline unsigned long hyp_ffa_proxy_pages (void )
arch/arm64/kvm/hyp/include/nvhe/mem_protect.h +2 -2
@@ -69,8 +69,8 @@ static __always_inline void __load_host_stage2(void) }#ifdef CONFIG_NVHE_EL2_DEBUG -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
@@ -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)
@@ -313,7 +319,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
@@ -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.49.0.604.gff1f9ca942-goog
Marc Zyngier
maintainerQuoted 6 lines · Quentin Perret
This is the v3 of the pKVM selftest series, v2 was here [1]. This series depends on [2] which moves the hyp tracking to the vmemmap. A full stack on top of v6.15-rc2 with everything applied is available here:
https://android-kvm.googlesource.com/linux qperret/pkvm-selftest-v3
Quoted 7 lines
Changes since v2:
- rebased on top of v2 of the hyp-state-in-vmemmap series [2];
- thanks to above rebase, dropped thePKVM_SELFTESTkconfig option and folded the selftest under the existingNVHE_EL2_DEBUG;
- fixed commit message of patch 02[...]
Applied to kvm-arm64/pkvm-selftest-6.16, thanks!
[1/4] KVM: arm64: Add .hyp.data section
commit: 74b13d5816c9389381e937b218f49f03921f87fe
[2/4] KVM: arm64: Don't WARN from __pkvm_host_share_guest()
commit: 845f126732a8126dee3f15855487121f4836131a
[3/4] KVM: arm64: Selftest for pKVM transitions
commit: 6c2d4c319c7db7be883428ae1161502f3f690e60
[4/4] KVM: arm64: Extend pKVM selftest for np-guests
commit: 48d564507293082c370a5fc1eb3d73ad3de30561Cheers,
M.
Signature · 1 line
Without deviation from the norm, progress is not possible.