KVM: arm64: Add KVM_PRE_FAULT_MEMORY support
Subsystems (5)
cover KVM: arm64: Add KVM_PRE_FAULT_MEMORY support
This series implements the KVM stage 2 page table pre-faulting feature for arm64.
== Foundations ==
The series begins by establishing required foundations:
1. Updating kvm_s2_fault_desc to independently store the exception syndrome register (ESR) value, and updating all code paths to use this value exclusively.
This is needed so we can later generate a synthetic fault to perform the pre-faulting - we need to be sure the code doesn't grab an incorrect ESR from elsewhere.
2. Updating kvm_s2_fault_desc to independently store the kvm_s2_mmu and updating all code paths to use this value exclusively.
Similarly this is needed so we can generate a synthetic fault against the canonical stage 2 MMU (it would make no sense for it to touch nested shadow page tables) - we need to be sure that the code doesn't grab an incorrect MMU from elsewhere.
3. Updating the abort paths which consume kvm_s2_fault_desc to also return a kvm_s2_fault_result data structure.
To perform pre-faulting the code must know the granule size of what was just walked. So the abort paths have to tell us what that was.
4. Pass walk flags to kvm_pgtable_get_leaf() to permit walking page tables under the MMU read lock.
This is Jack's patch, verbatim, which allows the use of the KVM_PGTABLE_WALK_SHARED flag to walk page tables under the MMU read lock.
Pre-faulting requires it to be able to work in parallel as specified by the API. The read lock precludes page tables being torn down behind our back.
== Implementation ==
Pre-faulting is implemented in kvm_arch_vcpu_pre_fault_memory() whose job is to pre-fault the stage 2 page tables which map a specific GPA (which, for arm64, is the guest's IPA).
This function is called by kvm_vcpu_pre_fault_memory() for each GPA in the range, which itself is ultimately invoked by userland via the KVM_PRE_FAULT_MEMORY ioctl.
The implementation is simple - try to walk to the stage 2 page table mapping the GPA - if unmapped, fault it in through a synthetic page fault.
pKVM is not supported regardless of whether the VM is protected or not.
This is because pKVM instantiates vCPUs upon run, but pre-faulting is typically performed before a vCPU is run. It would be confusing and inconsistent to error out on non-running vCPUs but to pre-fault running ones.
Jack's original test suite is also included in the series.
== Credits ==
This series is based, with gratitude, on Jack Thomson's series and their respins (links provided below) as well as the feedback he received.
The series includes Jack's v5 "KVM: arm64: Pass walk flags to kvm_pgtable_get_leaf()" patch verbatim, and all three of his test patches, two of which required minor fixups.
== Reviewer Notes ==
I synced with maintainers on this who asked me to take a look, as there hadn't been progress on the series for some time.
Am happy to rebase again after -rc1.
---
Jack Thomson (4):
KVM: arm64: Pass walk flags to kvm_pgtable_get_leaf()
KVM: selftests: Enable pre_fault_memory_test for arm64
KVM: selftests: Add option for different backing in pre-fault tests
KVM: selftests: Add nested pre-fault test for arm64
Lorenzo Stoakes (ARM) (4):
KVM: arm64: Propagate and use esr in s2fd when handling guest aborts
KVM: arm64: Propagate and use mmu in s2fd when handling guest aborts
KVM: arm64: Propagate and use kvm_s2_fault_result on S2 fault
KVM: arm64: Implement KVM_PRE_FAULT_MEMORY
Documentation/virt/kvm/api.rst | 17 +- arch/arm64/include/asm/esr.h | 129 +++++--- arch/arm64/include/asm/kvm_emulate.h | 52 +--- arch/arm64/include/asm/kvm_pgtable.h | 5 +- arch/arm64/include/asm/kvm_pkvm.h | 2 +- arch/arm64/kvm/Kconfig | 1 + arch/arm64/kvm/arm.c | 1 + arch/arm64/kvm/hyp/nvhe/mem_protect.c | 10 +- arch/arm64/kvm/hyp/pgtable.c | 5 +- arch/arm64/kvm/mmu.c | 324 +++++++++++++++++---- arch/arm64/kvm/nested.c | 2 +- tools/testing/selftests/kvm/Makefile.kvm | 2 + .../selftests/kvm/arm64/nv_pre_fault_memory_test.c | 206 +++++++++++++ .../testing/selftests/kvm/pre_fault_memory_test.c | 152 ++++++++-- 14 files changed, 736 insertions(+), 172 deletions(-)
---
base-commit: aa8e5dc6a7a2a1141ab40706a51010adcd0e57d2
change-id: 20260815-kvm-arm-prefault-9bb411b6897d
Best regards,
Signature · 1 line
Lorenzo Stoakes (ARM) <[email protected]>
PATCH 1/8 KVM: arm64: Propagate and use esr in s2fd when handling guest aborts
kvm_handle_guest_abort() establishes a kvm_s2_fault_desc data structure, s2fd, to store and propagate state to either pkvm_mem_abort(), gmem_abort() or user_mem_abort() handlers.
Each of these, however, examines the Exception Syndrome Register (ESR) via s2fd->vcpu.
Introduce an s2fd->esr field to abstract this and propagate it to callers.
The value of this (beyond refactoring) is to be able to later generate faults with a synthetic esr, specifically to implement stage 2 page table pre-faulting.
Abstract esr-specific predicates and helpers to the esr.h header and either have vcpu wrappers call these, or eliminate them if they are not used elsewhere.
Provide kvm_s2_fault_is_[write,exec,perm]() helpers for convenience.
Since kvm_s2_fault_map() either sets perm_fault_granule to the permission fault granule or 0 if not a permission fault, implement kvm_s2_perm_fault_granule() to do this directly.
Abort handlers which use kvm_s2_fault_desc - gmem_abort() and user_mem_abort() - now only reference s2fd->esr and do not look it up in any other way, which makes it safe to pass a synthetic s2fd->esr value to these functions.
No functional change intended.
--- arch/arm64/include/asm/esr.h | 129 +++++++++++++++++++++++++---------- arch/arm64/include/asm/kvm_emulate.h | 52 ++++---------- arch/arm64/kvm/mmu.c | 80 +++++++++++++--------- 3 files changed, 156 insertions(+), 105 deletions(-)
arch/arm64/include/asm/esr.h +92 -37
@@ -437,6 +437,32 @@#ifndef __ASSEMBLER__#include <asm/types.h> +static inline u8 esr_trap_get_class (unsigned long esr) +{ +return ESR_ELx_EC (esr); +} + +static inline bool esr_trap_is_iabt (unsigned long esr) +{ +return esr_trap_get_class (esr) == ESR_ELx_EC_IABT_LOW; +} + +static inline bool esr_abt_is_s1ptw (unsigned long esr) +{ +return esr & ESR_ELx_S1PTW; +} + +/* Always check for S1PTW *before* using this. */ +static inline bool esr_dabt_is_write (unsigned long esr) +{ +return esr & ESR_ELx_WNR; +} + +static inline bool esr_dabt_is_cm (unsigned long esr) +{ +return esr & ESR_ELx_CM; +} +static inline unsigned long esr_brk_comment (unsigned long esr) {return esr & ESR_ELx_BRK64_ISS_COMMENT_MASK;
@@ -460,75 +486,104 @@ static inline bool esr_is_ubsan_brk(unsigned long esr)return (esr_brk_comment (esr) & ~UBSAN_BRK_MASK ) ==UBSAN_BRK_IMM ; } +static inline u8 esr_fsc_get_fault (unsigned long esr) +{ +return esr & ESR_ELx_FSC; +} +static inline bool esr_fsc_is_translation_fault (unsigned long esr) { - esr = esr & ESR_ELx_FSC; +const u8 fault =esr_fsc_get_fault (esr); -return (esr ==ESR_ELx_FSC_FAULT_L (3 )) || - (esr ==ESR_ELx_FSC_FAULT_L (2 )) || - (esr ==ESR_ELx_FSC_FAULT_L (1 )) || - (esr ==ESR_ELx_FSC_FAULT_L (0 )) || - (esr ==ESR_ELx_FSC_FAULT_L (-1 )); +return (fault ==ESR_ELx_FSC_FAULT_L (3 )) || + (fault ==ESR_ELx_FSC_FAULT_L (2 )) || + (fault ==ESR_ELx_FSC_FAULT_L (1 )) || + (fault ==ESR_ELx_FSC_FAULT_L (0 )) || + (fault ==ESR_ELx_FSC_FAULT_L (-1 )); }static inline bool esr_fsc_is_permission_fault (unsigned long esr) { - esr = esr & ESR_ELx_FSC; +const u8 fault =esr_fsc_get_fault (esr); -return (esr ==ESR_ELx_FSC_PERM_L (3 )) || - (esr ==ESR_ELx_FSC_PERM_L (2 )) || - (esr ==ESR_ELx_FSC_PERM_L (1 )) || - (esr ==ESR_ELx_FSC_PERM_L (0 )); +return (fault ==ESR_ELx_FSC_PERM_L (3 )) || + (fault ==ESR_ELx_FSC_PERM_L (2 )) || + (fault ==ESR_ELx_FSC_PERM_L (1 )) || + (fault ==ESR_ELx_FSC_PERM_L (0 )); }static inline bool esr_fsc_is_access_flag_fault (unsigned long esr) { - esr = esr & ESR_ELx_FSC; +const u8 fault =esr_fsc_get_fault (esr); -return (esr ==ESR_ELx_FSC_ACCESS_L (3 )) || - (esr ==ESR_ELx_FSC_ACCESS_L (2 )) || - (esr ==ESR_ELx_FSC_ACCESS_L (1 )) || - (esr ==ESR_ELx_FSC_ACCESS_L (0 )); +return (fault ==ESR_ELx_FSC_ACCESS_L (3 )) || + (fault ==ESR_ELx_FSC_ACCESS_L (2 )) || + (fault ==ESR_ELx_FSC_ACCESS_L (1 )) || + (fault ==ESR_ELx_FSC_ACCESS_L (0 )); }static inline bool esr_fsc_is_excl_atomic_fault (unsigned long esr) { - esr = esr & ESR_ELx_FSC; - -return esr == ESR_ELx_FSC_EXCL_ATOMIC; +return esr_fsc_get_fault (esr) == ESR_ELx_FSC_EXCL_ATOMIC; }static inline bool esr_fsc_is_addr_sz_fault (unsigned long esr) { - esr &= ESR_ELx_FSC; +const u8 fault =esr_fsc_get_fault (esr); + +return (fault ==ESR_ELx_FSC_ADDRSZ_L (3 )) || + (fault ==ESR_ELx_FSC_ADDRSZ_L (2 )) || + (fault ==ESR_ELx_FSC_ADDRSZ_L (1 )) || + (fault ==ESR_ELx_FSC_ADDRSZ_L (0 )) || + (fault ==ESR_ELx_FSC_ADDRSZ_L (-1 )); +} + +static inline bool esr_abt_is_exec_fault (unsigned long esr) +{ +return esr_trap_is_iabt (esr) && !esr_abt_is_s1ptw (esr); +} -return (esr ==ESR_ELx_FSC_ADDRSZ_L (3 )) || - (esr ==ESR_ELx_FSC_ADDRSZ_L (2 )) || - (esr ==ESR_ELx_FSC_ADDRSZ_L (1 )) || - (esr ==ESR_ELx_FSC_ADDRSZ_L (0 )) || - (esr ==ESR_ELx_FSC_ADDRSZ_L (-1 )); +static inline bool esr_abt_is_sea (unsigned long esr) +{ +const u8 fault =esr_fsc_get_fault (esr); + +switch (fault) { +case ESR_ELx_FSC_EXTABT: + case ESR_ELx_FSC_SEA_TTW(-1) ... ESR_ELx_FSC_SEA_TTW(3): +case ESR_ELx_FSC_SECC: + case ESR_ELx_FSC_SECC_TTW(-1) ... ESR_ELx_FSC_SECC_TTW(3): +return true; +default : +return false; + } +} + +/* Not valid for negative levels. */ +static inline u64 esr_fsc_get_level (unsigned long esr) +{ +return esr & ESR_ELx_FSC_LEVEL; }static inline bool esr_fsc_is_sea_ttw (unsigned long esr) { - esr = esr & ESR_ELx_FSC; +const u8 fault =esr_fsc_get_fault (esr); -return (esr ==ESR_ELx_FSC_SEA_TTW (3 )) || - (esr ==ESR_ELx_FSC_SEA_TTW (2 )) || - (esr ==ESR_ELx_FSC_SEA_TTW (1 )) || - (esr ==ESR_ELx_FSC_SEA_TTW (0 )) || - (esr ==ESR_ELx_FSC_SEA_TTW (-1 )); +return (fault ==ESR_ELx_FSC_SEA_TTW (3 )) || + (fault ==ESR_ELx_FSC_SEA_TTW (2 )) || + (fault ==ESR_ELx_FSC_SEA_TTW (1 )) || + (fault ==ESR_ELx_FSC_SEA_TTW (0 )) || + (fault ==ESR_ELx_FSC_SEA_TTW (-1 )); }static inline bool esr_fsc_is_secc_ttw (unsigned long esr) { - esr = esr & ESR_ELx_FSC; +const u8 fault =esr_fsc_get_fault (esr); -return (esr ==ESR_ELx_FSC_SECC_TTW (3 )) || - (esr ==ESR_ELx_FSC_SECC_TTW (2 )) || - (esr ==ESR_ELx_FSC_SECC_TTW (1 )) || - (esr ==ESR_ELx_FSC_SECC_TTW (0 )) || - (esr ==ESR_ELx_FSC_SECC_TTW (-1 )); +return (fault ==ESR_ELx_FSC_SECC_TTW (3 )) || + (fault ==ESR_ELx_FSC_SECC_TTW (2 )) || + (fault ==ESR_ELx_FSC_SECC_TTW (1 )) || + (fault ==ESR_ELx_FSC_SECC_TTW (0 )) || + (fault ==ESR_ELx_FSC_SECC_TTW (-1 )); }/* Indicate whether ESR.EC==0x1A is for an ERETAx instruction */
arch/arm64/include/asm/kvm_emulate.h +15 -37
98 lines not on this page · raw message
arch/arm64/kvm/mmu.c +49 -31
205 lines not on this page · raw message
Signature · 1 line
2.55.0
PATCH 2/8 KVM: arm64: Propagate and use mmu in s2fd when handling guest aborts
In reply to Lorenzo Stoakes (ARM)kvm_handle_guest_abort() establishes a kvm_s2_fault_desc data structure, s2fd, to store and propagate state to either pkvm_mem_abort(), gmem_abort() or user_mem_abort() handlers.
Each of these, however, examines the state of the stage 2 MMU via vcpu->arch.hw_mmu.
Introduce an s2fd->mmu field to abstract this and propagate it to callers.
Similar to adding the esr field, this allows injection of synthetic faults with the ultimate intention of implementing stage 2 page table pre-faulting.
No functional change intended.
--- arch/arm64/kvm/mmu.c | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-)
arch/arm64/kvm/mmu.c +10 -7
@@ -1604,6 +1604,7 @@ struct kvm_s2_fault_desc {struct kvm_memory_slot *memslot;unsigned long hva;unsigned long esr; +struct kvm_s2_mmu *mmu; };static bool kvm_s2_fault_is_perm (const struct kvm_s2_fault_desc *s2fd)
@@ -1637,7 +1638,7 @@ static int gmem_abort(const struct kvm_s2_fault_desc *s2fd)const bool perm_fault =kvm_s2_fault_is_perm (s2fd);enum kvm_pgtable_walk_flags flags =KVM_PGTABLE_WALK_SHARED ;enum kvm_pgtable_prot prot =KVM_PGTABLE_PROT_R ; -struct kvm_pgtable *pgt = s2fd->vcpu->arch.hw_mmu->pgt; +struct kvm_pgtable *pgt = s2fd->mmu->pgt;unsigned long mmu_seq;struct page *page;struct kvm *kvm = s2fd->vcpu->kvm;
@@ -1735,7 +1736,7 @@ static int pkvm_mem_abort(const struct kvm_s2_fault_desc *s2fd) {unsigned int flags =FOLL_HWPOISON |FOLL_LONGTERM |FOLL_WRITE ;struct kvm_vcpu *vcpu = s2fd->vcpu; -struct kvm_pgtable *pgt = vcpu->arch.hw_mmu->pgt; +struct kvm_pgtable *pgt = s2fd->mmu->pgt;struct mm_struct *mm = current->mm;struct kvm *kvm = vcpu->kvm;void *hyp_memcache;
@@ -2050,7 +2051,7 @@ static int kvm_s2_fault_map(const struct kvm_s2_fault_desc *s2fd,int ret;kvm_fault_lock (kvm); - pgt = s2fd->vcpu->arch.hw_mmu->pgt; + pgt = s2fd->mmu->pgt; ret = -EAGAIN ;if (mmu_invalidate_retry (kvm, s2vi->mmu_seq)) goto out_unlock;
@@ -2271,6 +2272,7 @@ int kvm_handle_guest_abort(struct kvm_vcpu *vcpu) {struct kvm_s2_trans nested_trans, *nested =NULL ;const unsigned long esr =kvm_vcpu_get_esr (vcpu); +struct kvm_s2_mmu *mmu = vcpu->arch.hw_mmu;phys_addr_t fault_ipa;/* The address we faulted on */ phys_addr_t ipa;/* Always the IPA in the L1 guest phys space */ struct kvm_memory_slot *memslot;
@@ -2300,7 +2302,7 @@ int kvm_handle_guest_abort(struct kvm_vcpu *vcpu) }/* Falls between the IPA range and the PARange? */ -if (fault_ipa >=BIT_ULL (VTCR_EL2_IPA (vcpu->arch.hw_mmu->vtcr))) { +if (fault_ipa >=BIT_ULL (VTCR_EL2_IPA (mmu->vtcr))) { fault_ipa |=FAR_TO_FIPA_OFFSET (kvm_vcpu_get_hfar (vcpu));return kvm_inject_sea (vcpu, is_iabt, fault_ipa);
@@ -2337,8 +2339,8 @@ int kvm_handle_guest_abort(struct kvm_vcpu *vcpu)* nothing to walk and we treat it as a 1:1 before going through the * canonical translation. */ -if (kvm_is_nested_s2_mmu (vcpu->kvm,vcpu->arch.hw_mmu) && - vcpu->arch.hw_mmu->nested_stage2_enabled) { +if (kvm_is_nested_s2_mmu (vcpu->kvm, mmu) && + mmu->nested_stage2_enabled) {u32 esr; ret =kvm_walk_nested_s2 (vcpu, fault_ipa, &nested_trans);
@@ -2413,7 +2415,7 @@ int kvm_handle_guest_abort(struct kvm_vcpu *vcpu) }/* Userspace should not be able to register out-of-bounds IPAs */ -VM_BUG_ON (ipa >=kvm_phys_size (vcpu->arch.hw_mmu)); +VM_BUG_ON (ipa >=kvm_phys_size (mmu));if (esr_fsc_is_access_flag_fault (esr)) {handle_access_fault (vcpu, fault_ipa);
@@ -2428,6 +2430,7 @@ int kvm_handle_guest_abort(struct kvm_vcpu *vcpu) .memslot = memslot, .hva = hva, .esr = esr, + .mmu = mmu, };if (kvm_vm_is_protected (vcpu->kvm)) {
Signature · 1 line
2.55.0
PATCH 3/8 KVM: arm64: Propagate and use kvm_s2_fault_result on S2 fault
In reply to Lorenzo Stoakes (ARM)When stage 2 page tables fault the net result may either be that a page is mapped, an error occurred or the fault should be retried (-EAGAIN).
When a fault succeeds it may be upgraded to a PMD size via transparent_hugepage_adjust().
In order to support KVM pre-faulting the outcome of the fault and the mapping size must be recorded.
Track this in the new kvm_s2_fault_result struct, which is threaded through gmem_abort(), user_mem_abort() and kvm_s2_fault_map().
PKVM and SEA aren't relevant to synthetic pre-faulting so neither kvm_inject_sea() nor pkvm_mem_abort() are altered.
Actual hardware faulting doesn't require this information, so kvm_handle_guest_abort() simply passes NULL kvm_s2_fault_result to gmem_abort() and user_mem_abort().
Faults are necessarily ephemeral and pre-faulting can't guarantee what may happen in parallel, so do not store the GFN or PFN in kvm_s2_fault_result. Pre-faulting only needs to know what was mapped in at the point of the fault.
This struct could be replaced with a pointer to an unsigned long, however it's clearer to separate out the mapped flag and having a struct allows us to easily add additional fields in future as needed.
No functional change intended.
--- arch/arm64/kvm/mmu.c | 33 +++++++++++++++++++++++++++------ 1 file changed, 27 insertions(+), 6 deletions(-)
arch/arm64/kvm/mmu.c +27 -6
@@ -1607,6 +1607,11 @@ struct kvm_s2_fault_desc {struct kvm_s2_mmu *mmu; }; +struct kvm_s2_fault_result { +unsigned long mapping_size; +bool mapped; +}; +static bool kvm_s2_fault_is_perm (const struct kvm_s2_fault_desc *s2fd) {return esr_fsc_is_permission_fault (s2fd->esr);
@@ -1632,7 +1637,17 @@ static u64 kvm_s2_perm_fault_granule(const struct kvm_s2_fault_desc *s2fd)return BIT (ARM64_HW_PGTABLE_LEVEL_SHIFT (level)); } -static int gmem_abort (const struct kvm_s2_fault_desc *s2fd) +static void populate_fault_result (struct kvm_s2_fault_result *result, +unsigned long mapping_size) +{ +/* A THP upgrade may have altered mapping size. */ + result->mapping_size = mapping_size; +/* -EAGAIN is swallowed so be explicit when we actually map. */ + result->mapped = true; +} + +static int gmem_abort (const struct kvm_s2_fault_desc *s2fd, +struct kvm_s2_fault_result *result) {bool write_fault, exec_fault;const bool perm_fault =kvm_s2_fault_is_perm (s2fd);
@@ -1714,6 +1729,8 @@ static int gmem_abort(const struct kvm_s2_fault_desc *s2fd)if ((prot &KVM_PGTABLE_PROT_W ) && !ret)mark_page_dirty_in_slot (kvm, s2fd->memslot, gfn); +if (result && !ret) +populate_fault_result (result,PAGE_SIZE );return ret != -EAGAIN ? ret :0 ; }
@@ -2038,7 +2055,8 @@ static int kvm_s2_fault_compute_prot(const struct kvm_s2_fault_desc *s2fd,static int kvm_s2_fault_map (const struct kvm_s2_fault_desc *s2fd,const struct kvm_s2_fault_vma_info *s2vi,enum kvm_pgtable_prot prot, -void *memcache) +void *memcache, +struct kvm_s2_fault_result *result) {enum kvm_pgtable_walk_flags flags =KVM_PGTABLE_WALK_SHARED ;bool writable = prot &KVM_PGTABLE_PROT_W ;
@@ -2117,12 +2135,15 @@ static int kvm_s2_fault_map(const struct kvm_s2_fault_desc *s2fd,mark_page_dirty_in_slot (kvm, s2fd->memslot,gpa_to_gfn (ipa)); } +if (result && !ret) +populate_fault_result (result, mapping_size);if (ret != -EAGAIN )return ret;return 0 ; } -static int user_mem_abort (const struct kvm_s2_fault_desc *s2fd) +static int user_mem_abort (const struct kvm_s2_fault_desc *s2fd, +struct kvm_s2_fault_result *result) {const bool perm_fault =kvm_s2_fault_is_perm (s2fd);struct kvm_s2_fault_vma_info s2vi = {};
@@ -2161,7 +2182,7 @@ static int user_mem_abort(const struct kvm_s2_fault_desc *s2fd)return ret; } -return kvm_s2_fault_map (s2fd, &s2vi, prot, memcache); +return kvm_s2_fault_map (s2fd, &s2vi, prot, memcache, result); }/* Resolve the access fault by making the page young again. */
@@ -2440,9 +2461,9 @@ int kvm_handle_guest_abort(struct kvm_vcpu *vcpu) !kvm_s2_fault_is_exec(&s2fd));if (kvm_slot_has_gmem (memslot)) - ret =gmem_abort (&s2fd); + ret =gmem_abort (&s2fd,NULL );else - ret =user_mem_abort (&s2fd); + ret =user_mem_abort (&s2fd,NULL ); }if (ret ==0 )
Signature · 1 line
2.55.0
PATCH 4/8 KVM: arm64: Pass walk flags to kvm_pgtable_get_leaf()
In reply to Lorenzo Stoakes (ARM)From: Jack Thomson <[email protected]>
Allow callers of kvm_pgtable_get_leaf() to specify the page-table walk flags, in preparation for performing walks under the MMU read lock.
Reading a stage-2 leaf while only holding the read lock requiresKVM_PGTABLE_WALK_SHARED: parallel faults (which also only hold the read lock) can unlink table pages and free them via RCU, so the walker must be inside an RCU read-side critical section, which the shared walk flag provides via kvm_pgtable_walk_begin().
All existing callers either hold the write lock, walk with interrupts disabled, or run at hyp where shared walks are rejected; they keep the current behaviour by passing no flags.
No functional change intended.
--- arch/arm64/include/asm/kvm_pgtable.h | 5 ++++- arch/arm64/kvm/hyp/nvhe/mem_protect.c | 10 +++++----- arch/arm64/kvm/hyp/pgtable.c | 5 +++-- arch/arm64/kvm/mmu.c | 2 +- arch/arm64/kvm/nested.c | 2 +- 5 files changed, 14 insertions(+), 10 deletions(-)
arch/arm64/include/asm/kvm_pgtable.h +4 -1
@@ -859,6 +859,8 @@ int kvm_pgtable_walk(struct kvm_pgtable *pgt, u64 addr, u64 size,* @addr: Input address for the start of the walk. * @ptep: Pointer to storage for the retrieved PTE. * @level: Pointer to storage for the level of the retrieved PTE. +* @flags: Flags to control the page-table walk +* (see struct kvm_pgtable_visit_ctx). * * The offset of @addr within a page is ignored. *
@@ -869,7 +871,8 @@ int kvm_pgtable_walk(struct kvm_pgtable *pgt, u64 addr, u64 size,* Return: 0 on success, negative error code on failure. */ int kvm_pgtable_get_leaf (struct kvm_pgtable *pgt,u64 addr, -kvm_pte_t *ptep,s8 *level); +kvm_pte_t *ptep,s8 *level, +enum kvm_pgtable_walk_flags flags);/** * kvm_pgtable_stage2_pte_prot() - Retrieve the protection attributes of a
arch/arm64/kvm/hyp/nvhe/mem_protect.c +5 -5
@@ -532,7 +532,7 @@ static int host_stage2_adjust_range(u64 addr, struct kvm_mem_range *range)int ret;hyp_assert_lock_held (&host_mmu.lock); - ret =kvm_pgtable_get_leaf (&host_mmu.pgt, addr, &pte, &level); + ret =kvm_pgtable_get_leaf (&host_mmu.pgt, addr, &pte, &level,0 );if (ret)return ret;
@@ -910,7 +910,7 @@ static int get_valid_guest_pte(struct pkvm_hyp_vm *vm, u64 ipa, kvm_pte_t *ptep,s8 level;int ret; - ret =kvm_pgtable_get_leaf (&vm->pgt, ipa, &pte, &level); + ret =kvm_pgtable_get_leaf (&vm->pgt, ipa, &pte, &level,0 );if (ret)return ret;if (guest_pte_is_poisoned (pte))
@@ -959,7 +959,7 @@ int __pkvm_vcpu_in_poison_fault(struct pkvm_hyp_vcpu *hyp_vcpu) ipa |=FAR_TO_FIPA_OFFSET (kvm_vcpu_get_hfar (&hyp_vcpu->vcpu));guest_lock_component (vm); - ret =kvm_pgtable_get_leaf (&vm->pgt, ipa, &pte, &level); + ret =kvm_pgtable_get_leaf (&vm->pgt, ipa, &pte, &level,0 );if (ret) goto unlock;
@@ -1315,7 +1315,7 @@ static int host_stage2_get_guest_info(phys_addr_t phys, struct pkvm_hyp_vm **vm,return -EPERM ; } - ret =kvm_pgtable_get_leaf (&host_mmu.pgt, phys, &pte, &level); + ret =kvm_pgtable_get_leaf (&host_mmu.pgt, phys, &pte, &level,0 );if (ret)return ret;
@@ -1544,7 +1544,7 @@ static int __check_host_shared_guest(struct pkvm_hyp_vm *vm, u64 *__phys, u64 ips8 level;int ret; - ret =kvm_pgtable_get_leaf (&vm->pgt, ipa, &pte, &level); + ret =kvm_pgtable_get_leaf (&vm->pgt, ipa, &pte, &level,0 );if (ret)return ret;if (!kvm_pte_valid (pte))
arch/arm64/kvm/hyp/pgtable.c +3 -2
@@ -298,12 +298,13 @@ static int leaf_walker(const struct kvm_pgtable_visit_ctx *ctx, }int kvm_pgtable_get_leaf (struct kvm_pgtable *pgt,u64 addr, -kvm_pte_t *ptep,s8 *level) +kvm_pte_t *ptep,s8 *level, +enum kvm_pgtable_walk_flags flags) {struct leaf_walk_data data;struct kvm_pgtable_walker walker = { .cb = leaf_walker, - .flags =KVM_PGTABLE_WALK_LEAF , + .flags = flags |KVM_PGTABLE_WALK_LEAF , .arg = &data, };int ret;
arch/arm64/kvm/mmu.c +1 -1
@@ -866,7 +866,7 @@ static int get_user_mapping_size(struct kvm *kvm, u64 addr)* IPI-ing threads). */ local_irq_save (flags); - ret =kvm_pgtable_get_leaf (&pgt, addr, &pte, &level); + ret =kvm_pgtable_get_leaf (&pgt, addr, &pte, &level,0 );local_irq_restore (flags);if (ret)
arch/arm64/kvm/nested.c +1 -1
@@ -654,7 +654,7 @@ static u8 get_guest_mapping_ttl(struct kvm_s2_mmu *mmu, u64 addr)return 0 ; tmp &= ~(sz -1 ); -if (kvm_pgtable_get_leaf (mmu->pgt, tmp, &pte,NULL )) +if (kvm_pgtable_get_leaf (mmu->pgt, tmp, &pte,NULL ,0 )) goto again;if (!(pte &PTE_VALID )) goto again;
Signature · 1 line
2.55.0
PATCH 5/8 KVM: arm64: Implement KVM_PRE_FAULT_MEMORY
In reply to Lorenzo Stoakes (ARM)Implement KVM stage 2 page table pre-faulting for the arm64 architecture.
Do the trivial plumbing by selecting CONFIG_KVM_GENERIC_PRE_FAULT_MEMORY in Kconfig, returning 1 in kvm_vm_ioctl_check_extension() for KVM_CAP_PRE_FAULT_MEMORY but false in kvm_pkvm_ext_allowed() to disallow its use for pKVM.
Add kvm_arch_vcpu_pre_fault_memory() to actually implement the feature by pre-faulting stage 2 page tables for a specific GPA.
Previous commits added the required foundations - gmem_abort() and user_mem_abort() determine esr and mmu from s2fd->esr and s2fd->mmu respectively, the granule size is returned by them in kvm_s2_fault_result and kvm_pgtable_get_leaf() accepts a walk flags parameter.
Additionally, kvm_pgtable_get_leaf() now accepts a walk flag, meaning its page table walk can be performed with KVM_PGTABLE_WALK_SHARED set and thus can be performed under a read mmu_lock.
With these changes in place, implement pre-faulting by first trying a page table walk under the MMU read lock then, if it fails, injecting a synthetic data abort at the page table level at which the page table walk failed.
This is necessarily racey as reclaim might happen at any time. Successfully pre-faulting can therefore only guarantee that each GPA was observed to be mapped at least once.
Protected KVM (pKVM) is not supported at all because pKVM creates VMs and vCPUs when first run, meaning any attempt to pre-fault prior to this cannot succeed.
Since the sensible use case for pre-faulting is doing so prior to vCPU run, and supporting only online pKVM vCPUs is confusing and inconsistent, simply don't support this at all.
There is a subtlety when retrieving the hva: it seems natural to use gfn_to_hva_memslot(), but this errors out for read-only memslots.
Since pre-faulting should fault in both read-only and read/write hvas, this isn't the correct API to use here.
gfn_to_hva_memslot_prot() allows retrieval of read-only hvas, but has unclear semantics, so introduce gfn_to_hva_memslot_read() to wrap it.
A retry mechanic is implemented when user_mem_abort() or gmem_abort() fail to map memory due to a benign failure where a hardware abort would not result in an error.
These occur when the abort handler was raced by either an invalidation MMU notifier or a racing abort path.
Since these faults are highly likely to succeed on immediate retry, retry up to MAX_PRE_FAULT_RETRIES times, after which -EAGAIN is ultimately returned to the caller.
An invalid memslot (i.e. a memslot with the KVM_MEMSLOT_INVALID flag set) also results in the operation returning -EAGAIN without a retry mechanic.
This is because an invalid memslot means the pre-fault operation raced with memslot reclaim, and since the SRCU lock is held, progress cannot be made.
Therefore, when this happens, -EAGAIN indicates that userland should retry the ioctl, which will bounce the SRCU lock and permit forward progress.
Finally, update the KVM API documentation to describe the changes, providing arm64-specific implementation details.
This work is based with gratitude on Jack Thomson's original series, its previous revisions and the feedback they received.
--- Documentation/virt/kvm/api.rst | 17 +++- arch/arm64/include/asm/kvm_pkvm.h | 2 +- arch/arm64/kvm/Kconfig | 1 + arch/arm64/kvm/arm.c | 1 + arch/arm64/kvm/mmu.c | 192 +++++++++++++++++++++++++++++++++++--- 5 files changed, 198 insertions(+), 15 deletions(-)
Documentation/virt/kvm/api.rst +14 -3
@@ -6485,7 +6485,7 @@ See KVM_SET_USER_MEMORY_REGION2 for additional details. --------------------------- :Capability: KVM_CAP_PRE_FAULT_MEMORY -:Architectures: none +:Architectures: x86, s390, arm64 :Type: vcpu ioctl :Parameters: struct kvm_pre_fault_memory (in/out) :Returns: 0 if at least one page is processed, < 0 on error
@@ -6493,12 +6493,14 @@ See KVM_SET_USER_MEMORY_REGION2 for additional details. Errors: ========== =============================================================== + EAGAIN A race occurred before progress was made, but a retry may succeed. EINVAL The specified `gpa` and `size` were invalid (e.g. not page aligned, causes an overflow, or size is zero), or the VM is UCONTROL (s390). ENOENT The specified `gpa` is outside defined memslots. EINTR An unmasked signal is pending and no page was processed. EFAULT The parameter address was invalid. + EHWPOISON A poisoned host page was encountered. EOPNOTSUPP Mapping memory for a GPA is unsupported by the hypervisor, and/or for the current vCPU state/mode. EIO unexpected error conditions (also causes a WARN)
@@ -6518,7 +6520,16 @@ Errors: KVM_PRE_FAULT_MEMORY populates KVM's stage-2 page tables used to map memory for the current vCPU state. KVM maps memory as if the vCPU generated a stage-2 read page fault, e.g. faults in memory as needed, but doesn't break -CoW. On x86, KVM does not mark any newly created stage-2 PTE as Accessed. +CoW. On arm64, KVM marks both existing and newly created stage-2 PTEs as +Accessed, on x86 it does not and for s390 it is not applicable. + +On arm64, a GPA is interpreted as an IPA, and never interpreted as the IPA +of a nested guest. Pre-faulting only populates canonical stage 2 page +tables. + +The feature is not supported on arm64 if the protected KVM (pKVM) feature +is enabled, as that results in vCPUs being instantiated on first run, which +renders pre-faulting useless. In the case of confidential VM types where there is an initial set up of private guest memory before the guest is 'finalized'/measured, this ioctl
@@ -6533,7 +6544,7 @@ When the ioctl returns, the input values are updated to point to the remaining range. If `size` > 0 on return, the caller can just issue the ioctl again with the same `struct kvm_map_memory` argument. -Shadow page tables cannot support this ioctl because they +On x86, Shadow page tables cannot support this ioctl because they are indexed by virtual address or nested guest physical address. Calling this ioctl when the guest is using shadow page tables (for example because it is running a nested guest with nested page tables)
arch/arm64/include/asm/kvm_pkvm.h +1 -1
@@ -44,9 +44,9 @@ static inline bool kvm_pkvm_ext_allowed(struct kvm *kvm, long ext)case KVM_CAP_ARM_PTRAUTH_GENERIC :return true;case KVM_CAP_ARM_MTE : -return false;case KVM_CAP_ARM_EAGER_SPLIT_CHUNK_SIZE :case KVM_CAP_ARM_SUPPORTED_BLOCK_SIZES : +case KVM_CAP_PRE_FAULT_MEMORY :return false;default :return !kvm || !kvm_vm_is_protected (kvm);
arch/arm64/kvm/Kconfig +1 -0
@@ -37,6 +37,7 @@ menuconfig KVM select SCHED_INFO select GUEST_PERF_EVENTS if PERF_EVENTS select KVM_GUEST_MEMFD + select KVM_GENERIC_PRE_FAULT_MEMORY help Support hosting virtualized guest machines.
arch/arm64/kvm/arm.c +1 -0
@@ -393,6 +393,7 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)case KVM_CAP_COUNTER_OFFSET :case KVM_CAP_ARM_WRITABLE_IMP_ID_REGS :case KVM_CAP_ARM_SEA_TO_USER : +case KVM_CAP_PRE_FAULT_MEMORY : r =1 ;break ;case KVM_CAP_SET_GUEST_DEBUG2 :
arch/arm64/kvm/mmu.c +181 -11
250 lines not on this page · raw message
Signature · 1 line
2.55.0
PATCH 6/8 KVM: selftests: Enable pre_fault_memory_test for arm64
In reply to Lorenzo Stoakes (ARM)From: Jack Thomson <[email protected]>
Enable the pre_fault_memory_test to run on arm64 by making it work with different guest page sizes and testing multiple guest configurations.
Update the test_assert to compare against the UCALL_EXIT_REASON, for portability, as arm64 exits with KVM_EXIT_MMIO while x86 uses KVM_EXIT_IO.
--- tools/testing/selftests/kvm/Makefile.kvm | 1 + .../testing/selftests/kvm/pre_fault_memory_test.c | 117 +++++++++++++++++---- 2 files changed, 96 insertions(+), 22 deletions(-)
tools/testing/selftests/kvm/Makefile.kvm +1 -0
@@ -199,6 +199,7 @@ TEST_GEN_PROGS_arm64 += guest_memfd_test TEST_GEN_PROGS_arm64 += mmu_stress_test TEST_GEN_PROGS_arm64 += rseq_test TEST_GEN_PROGS_arm64 += steal_time +TEST_GEN_PROGS_arm64 += pre_fault_memory_test TEST_GEN_PROGS_s390 = $(TEST_GEN_PROGS_COMMON) TEST_GEN_PROGS_s390 += s390/memop
tools/testing/selftests/kvm/pre_fault_memory_test.c +95 -22
184 lines not on this page · raw message
Signature · 1 line
2.55.0
PATCH 7/8 KVM: selftests: Add option for different backing in pre-fault tests
In reply to Lorenzo Stoakes (ARM)From: Jack Thomson <[email protected]>
Add a -s option to specify different memory backing types for the pre-fault tests (e.g. anonymous, hugetlb), allowing testing of the pre-fault functionality across different memory configurations.
--- .../testing/selftests/kvm/pre_fault_memory_test.c | 51 +++++++++++++++------- 1 file changed, 36 insertions(+), 15 deletions(-)
tools/testing/selftests/kvm/pre_fault_memory_test.c +36 -15
@@ -46,6 +46,7 @@ struct slot_worker_data {struct kvm_vm *vm;gpa_t gpa;u32 flags; +enum vm_mem_backing_src_type mem_backing_src;bool worker_ready;bool prefault_ready;bool recreate_slot;
@@ -66,14 +67,16 @@ static void *delete_slot_worker(void *__data)while (!READ_ONCE (data->recreate_slot))cpu_relax (); -vm_userspace_mem_region_add (vm,VM_MEM_SRC_ANONYMOUS , data->gpa, +vm_userspace_mem_region_add (vm, data->mem_backing_src, data->gpa,TEST_SLOT , test_config.test_num_pages, data->flags);return NULL ; }static void pre_fault_memory (struct kvm_vcpu *vcpu,u64 base_gpa,u64 offset, -u64 size,u64 expected_left,bool private) +u64 size,u64 expected_left, +enum vm_mem_backing_src_type mem_backing_src, +bool private) {struct kvm_pre_fault_memory range = { .gpa = base_gpa + offset,
@@ -84,6 +87,7 @@ static void pre_fault_memory(struct kvm_vcpu *vcpu, u64 base_gpa, u64 offset, .vm = vcpu->vm, .gpa = base_gpa, .flags = private ? KVM_MEM_GUEST_MEMFD : 0, + .mem_backing_src = mem_backing_src, };bool slot_recreated = false;pthread_t slot_worker;
@@ -173,11 +177,13 @@ static void pre_fault_memory(struct kvm_vcpu *vcpu, u64 base_gpa, u64 offset,struct test_params {unsigned long vm_type;bool private; +enum vm_mem_backing_src_type mem_backing_src; };static void __test_pre_fault_memory (enum vm_guest_mode guest_mode,void *arg) {gpa_t gpa, gva, alignment, guest_page_size, host_page_size; +gpa_t backing_src_pagesz, mem_page_size;struct test_params *p = arg;const struct vm_shape shape = { .mode = guest_mode,
@@ -189,24 +195,28 @@ static void __test_pre_fault_memory(enum vm_guest_mode guest_mode, void *arg)struct ucall uc;pr_info ("Testing guest mode: %s\n" ,vm_guest_mode_string (guest_mode)); +pr_info ("Testing memory backing src type: %s\n" , +vm_mem_backing_src_alias (p->mem_backing_src)->name); vm =vm_create_shape_with_one_vcpu (shape, &vcpu, guest_code); guest_page_size = vm_guest_mode_params[guest_mode].page_size; host_page_size =getpagesize (); + backing_src_pagesz =get_backing_src_pagesz (p->mem_backing_src); + mem_page_size =max (host_page_size, backing_src_pagesz); test_config.page_size = guest_page_size; test_config.test_size =align_up (TEST_BASE_SIZE + test_config.page_size, - host_page_size); + mem_page_size); test_config.test_num_pages =vm_calc_num_guest_pages (vm->mode, test_config.test_size); gpa = (vm->max_gfn - test_config.test_num_pages) * test_config.page_size; alignment =SZ_2M ; - alignment =max (alignment, host_page_size); + alignment =max (alignment, mem_page_size); gpa =align_down (gpa, alignment); gva = gpa & ((1ULL << (vm->va_bits -1 )) -1 ); -vm_userspace_mem_region_add (vm,VM_MEM_SRC_ANONYMOUS , +vm_userspace_mem_region_add (vm, p->mem_backing_src, gpa,TEST_SLOT , test_config.test_num_pages, p->private ?KVM_MEM_GUEST_MEMFD :0 );virt_map (vm, gva, gpa, test_config.test_num_pages);
@@ -214,14 +224,18 @@ static void __test_pre_fault_memory(enum vm_guest_mode guest_mode, void *arg)if (p->private)vm_mem_set_private (vm, gpa, test_config.test_size); -pre_fault_memory (vcpu, gpa,0 , test_config.test_size,0 , p->private); +pre_fault_memory (vcpu, gpa,0 , test_config.test_size,0 , + p->mem_backing_src, p->private);/* Retry the same range after the first prefault attempt. */ -pre_fault_memory (vcpu, gpa,0 , test_config.test_size,0 , p->private); +pre_fault_memory (vcpu, gpa,0 , test_config.test_size,0 , + p->mem_backing_src, p->private);pre_fault_memory (vcpu, gpa, test_config.test_size - host_page_size, - host_page_size *2 , host_page_size, p->private); + host_page_size *2 , host_page_size, + p->mem_backing_src, p->private);pre_fault_memory (vcpu, gpa, test_config.test_size, - host_page_size, host_page_size, p->private); + host_page_size, host_page_size, + p->mem_backing_src, p->private);vcpu_args_set (vcpu,1 , gva);
@@ -250,11 +264,13 @@ static void __test_pre_fault_memory(enum vm_guest_mode guest_mode, void *arg)kvm_vm_free (vm); } -static void test_pre_fault_memory (unsigned long vm_type,bool private) +static void test_pre_fault_memory (unsigned long vm_type,enum vm_mem_backing_src_type backing_src, +bool private) {struct test_params p = { .vm_type = vm_type, .private = private, + .mem_backing_src = backing_src, };if (vm_type && !(kvm_check_cap (KVM_CAP_VM_TYPES ) &BIT (vm_type))) {
@@ -268,23 +284,28 @@ static void test_pre_fault_memory(unsigned long vm_type, bool private)static void help (char *name) {puts ("" ); -printf ("usage: %s [-h] [-m mode]\n" , name); +printf ("usage: %s [-h] [-m mode] [-s mem-type]\n" , name);puts ("" );guest_modes_help (); +backing_src_help ("-s" );puts ("" ); }int main (int argc,char *argv[]) { +enum vm_mem_backing_src_type backing =DEFAULT_VM_MEM_SRC ;int opt;guest_modes_append_default (); -while ((opt =getopt (argc, argv,"hm:" )) !=-1 ) { +while ((opt =getopt (argc, argv,"hm:s:" )) !=-1 ) {switch (opt) {case 'm' :guest_modes_cmdline (optarg);break ; +case 's' : + backing =parse_backing_src_type (optarg); +break ;case 'h' :default :help (argv[0 ]);
@@ -294,10 +315,10 @@ int main(int argc, char *argv[])TEST_REQUIRE (kvm_check_cap (KVM_CAP_PRE_FAULT_MEMORY )); -test_pre_fault_memory (0 , false); +test_pre_fault_memory (0 , backing, false);#ifdef __x86_64__ -test_pre_fault_memory (KVM_X86_SW_PROTECTED_VM , false); -test_pre_fault_memory (KVM_X86_SW_PROTECTED_VM , true); +test_pre_fault_memory (KVM_X86_SW_PROTECTED_VM , backing, false); +test_pre_fault_memory (KVM_X86_SW_PROTECTED_VM , backing, true);#endif return 0 ; }
Signature · 1 line
2.55.0
PATCH 8/8 KVM: selftests: Add nested pre-fault test for arm64
In reply to Lorenzo Stoakes (ARM)From: Jack Thomson <[email protected]>
Add an arm64 nested-virt selftest for KVM_PRE_FAULT_MEMORY. The guest enters vEL1 and exits to userspace with a nested/shadow stage-2 MMU as the vCPU's last-run context.
Before prefaulting, userspace enables HCR_EL2.VM and points VTTBR_EL2 at an empty nested stage-2 root. A prefault implementation that incorrectly treats the userspace GPA as an L2 IPA will fail the ioctl; the correct path targets the canonical stage-2 and succeeds.
Restore the original nested state before resuming the guest, then touch the prefaulted range to check that vEL1 still runs correctly.
--- tools/testing/selftests/kvm/Makefile.kvm | 1 + .../selftests/kvm/arm64/nv_pre_fault_memory_test.c | 206 +++++++++++++++++++++ 2 files changed, 207 insertions(+)
tools/testing/selftests/kvm/Makefile.kvm +1 -0
@@ -173,6 +173,7 @@ TEST_GEN_PROGS_arm64 += arm64/debug-exceptions TEST_GEN_PROGS_arm64 += arm64/hello_el2 TEST_GEN_PROGS_arm64 += arm64/host_sve TEST_GEN_PROGS_arm64 += arm64/hypercalls +TEST_GEN_PROGS_arm64 += arm64/nv_pre_fault_memory_test TEST_GEN_PROGS_arm64 += arm64/external_aborts TEST_GEN_PROGS_arm64 += arm64/mmio_sign_ext TEST_GEN_PROGS_arm64 += arm64/page_fault_test
tools/testing/selftests/kvm/arm64/nv_pre_fault_memory_test.c new file +206 -0
206 lines not on this page · raw message
Signature · 1 line
2.55.0
Marc Zyngier
maintainerRe: [PATCH 1/8] KVM: arm64: Propagate and use esr in s2fd when handling guest aborts
In reply to Lorenzo Stoakes (ARM)Quoted 20 lines · Lorenzo Stoakes (ARM)
kvm_handle_guest_abort()establishes akvm_s2_fault_descdata structure, s2fd, to store and propagate state to eitherpkvm_mem_abort(),gmem_abort()oruser_mem_abort()handlers.Each of these, however, examines the Exception Syndrome Register (ESR) via
s2fd->vcpu.Introduce an
s2fd->esrfield to abstract this and propagate it to callers.The value of this (beyond refactoring) is to be able to later generate faults with a synthetic esr, specifically to implement stage 2 page table pre-faulting.
Abstract esr-specific predicates and helpers to the
esr.hheader and either have vcpu wrappers call these, or eliminate them if they are not used elsewhere.Provide kvm_s2_fault_is_[write,exec,perm]() helpers for convenience.
Quoted 8 lines
Since
kvm_s2_fault_map()either setsperm_fault_granuleto the permission fault granule or 0 if not a permission fault, implementkvm_s2_perm_fault_granule()to do this directly.Abort handlers which use
kvm_s2_fault_desc-gmem_abort()anduser_mem_abort()- now only references2fd->esrand do not look it up in any other way, which makes it safe to pass a synthetics2fd->esrvalue to these functions.
Please split this. ESR helpers in one patch, hacking the MMU code to use it in another, s2fd->esr stuff last.
Quoted 15 lines
No functional change intended.
Signed-off-by Lorenzo Stoakes (ARM) <[email protected]>--- arch/arm64/include/asm/esr.h | 129 +++++++++++++++++++++++++---------- arch/arm64/include/asm/kvm_emulate.h | 52 ++++---------- arch/arm64/kvm/mmu.c | 80 +++++++++++++--------- 3 files changed, 156 insertions(+), 105 deletions(-)arch/arm64/include/asm/esr.h +0
-0
arch/arm64/include/asm/esr.h line 440
Quoted 4 lines
arch/arm64/include/asm/esr.h +1
-0@@ -437,6 +437,32 @@#ifndef __ASSEMBLER__#include <asm/types.h> +static inline u8 esr_trap_get_class (unsigned long esr)
nit: there is no notion of trap here. This is simply extracting the EC from the ESR. My personal (and wholly unreliable) taste would be to go for something like esr_get_ec().
I appreciate that you are simply propagating the names used in KVM, but they were pretty poor the first place, and have only been kept to avoid churn.
Also, 'inline' is a bit of a problem given that the callers are __always_inline for good reasons (see 5c37f1ae1c3358).
Quoted 42 lines
arch/arm64/include/asm/esr.h +31
-1+{ +return ESR_ELx_EC (esr); +} + +static inline bool esr_trap_is_iabt (unsigned long esr) +{ +return esr_trap_get_class (esr) == ESR_ELx_EC_IABT_LOW; +} + +static inline bool esr_abt_is_s1ptw (unsigned long esr) +{ +return esr & ESR_ELx_S1PTW; +} + +/* Always check for S1PTW *before* using this. */ +static inline bool esr_dabt_is_write (unsigned long esr) +{ +return esr & ESR_ELx_WNR; +} + +static inline bool esr_dabt_is_cm (unsigned long esr) +{ +return esr & ESR_ELx_CM; +} +static inline unsigned long esr_brk_comment (unsigned long esr) {return esr & ESR_ELx_BRK64_ISS_COMMENT_MASK;@@ -460,75 +486,104 @@ static inline bool esr_is_ubsan_brk(unsigned long esr)return (esr_brk_comment (esr) & ~UBSAN_BRK_MASK ) ==UBSAN_BRK_IMM ; } +static inline u8 esr_fsc_get_fault (unsigned long esr) +{ +return esr & ESR_ELx_FSC; +} +static inline bool esr_fsc_is_translation_fault (unsigned long esr) { - esr = esr & ESR_ELx_FSC; +const u8 fault =esr_fsc_get_fault (esr);
arch/arm64/include/asm/esr.h static inline bool esr_is_ubsan_brk(unsigned long esr) · line 502
Quoted 10 lines
arch/arm64/include/asm/esr.h +5
-5-return (esr ==ESR_ELx_FSC_FAULT_L (3 )) || - (esr ==ESR_ELx_FSC_FAULT_L (2 )) || - (esr ==ESR_ELx_FSC_FAULT_L (1 )) || - (esr ==ESR_ELx_FSC_FAULT_L (0 )) || - (esr ==ESR_ELx_FSC_FAULT_L (-1 )); +return (fault ==ESR_ELx_FSC_FAULT_L (3 )) || + (fault ==ESR_ELx_FSC_FAULT_L (2 )) || + (fault ==ESR_ELx_FSC_FAULT_L (1 )) || + (fault ==ESR_ELx_FSC_FAULT_L (0 )) || + (fault ==ESR_ELx_FSC_FAULT_L (-1 ));
I really think we could do without this sort of churn.
Quoted 72 lines
arch/arm64/include/asm/esr.h +34
-19}static inline bool esr_fsc_is_permission_fault (unsigned long esr) { - esr = esr & ESR_ELx_FSC; +const u8 fault =esr_fsc_get_fault (esr); -return (esr ==ESR_ELx_FSC_PERM_L (3 )) || - (esr ==ESR_ELx_FSC_PERM_L (2 )) || - (esr ==ESR_ELx_FSC_PERM_L (1 )) || - (esr ==ESR_ELx_FSC_PERM_L (0 )); +return (fault ==ESR_ELx_FSC_PERM_L (3 )) || + (fault ==ESR_ELx_FSC_PERM_L (2 )) || + (fault ==ESR_ELx_FSC_PERM_L (1 )) || + (fault ==ESR_ELx_FSC_PERM_L (0 )); }static inline bool esr_fsc_is_access_flag_fault (unsigned long esr) { - esr = esr & ESR_ELx_FSC; +const u8 fault =esr_fsc_get_fault (esr); -return (esr ==ESR_ELx_FSC_ACCESS_L (3 )) || - (esr ==ESR_ELx_FSC_ACCESS_L (2 )) || - (esr ==ESR_ELx_FSC_ACCESS_L (1 )) || - (esr ==ESR_ELx_FSC_ACCESS_L (0 )); +return (fault ==ESR_ELx_FSC_ACCESS_L (3 )) || + (fault ==ESR_ELx_FSC_ACCESS_L (2 )) || + (fault ==ESR_ELx_FSC_ACCESS_L (1 )) || + (fault ==ESR_ELx_FSC_ACCESS_L (0 )); }static inline bool esr_fsc_is_excl_atomic_fault (unsigned long esr) { - esr = esr & ESR_ELx_FSC; - -return esr == ESR_ELx_FSC_EXCL_ATOMIC; +return esr_fsc_get_fault (esr) == ESR_ELx_FSC_EXCL_ATOMIC; }static inline bool esr_fsc_is_addr_sz_fault (unsigned long esr) { - esr &= ESR_ELx_FSC; +const u8 fault =esr_fsc_get_fault (esr); + +return (fault ==ESR_ELx_FSC_ADDRSZ_L (3 )) || + (fault ==ESR_ELx_FSC_ADDRSZ_L (2 )) || + (fault ==ESR_ELx_FSC_ADDRSZ_L (1 )) || + (fault ==ESR_ELx_FSC_ADDRSZ_L (0 )) || + (fault ==ESR_ELx_FSC_ADDRSZ_L (-1 )); +} + +static inline bool esr_abt_is_exec_fault (unsigned long esr) +{ +return esr_trap_is_iabt (esr) && !esr_abt_is_s1ptw (esr); +} -return (esr ==ESR_ELx_FSC_ADDRSZ_L (3 )) || - (esr ==ESR_ELx_FSC_ADDRSZ_L (2 )) || - (esr ==ESR_ELx_FSC_ADDRSZ_L (1 )) || - (esr ==ESR_ELx_FSC_ADDRSZ_L (0 )) || - (esr ==ESR_ELx_FSC_ADDRSZ_L (-1 )); +static inline bool esr_abt_is_sea (unsigned long esr) +{ +const u8 fault =esr_fsc_get_fault (esr); + +switch (fault) { +case ESR_ELx_FSC_EXTABT: + case ESR_ELx_FSC_SEA_TTW(-1) ... ESR_ELx_FSC_SEA_TTW(3): +case ESR_ELx_FSC_SECC: + case ESR_ELx_FSC_SECC_TTW(-1) ... ESR_ELx_FSC_SECC_TTW(3): +return true;
arch/arm64/include/asm/esr.h static inline bool esr_is_ubsan_brk(unsigned long esr) · line 565
Quoted 10 lines
arch/arm64/include/asm/esr.h +9
-0+default : +return false; + } +} + +/* Not valid for negative levels. */ +static inline u64 esr_fsc_get_level (unsigned long esr) +{ +return esr & ESR_ELx_FSC_LEVEL; }
If that's such an unreliable helper, why is it exposed to everyone instead of being kept local to the single caller?
Quoted 174 lines
arch/arm64/include/asm/esr.h +12
-12static inline bool esr_fsc_is_sea_ttw (unsigned long esr) { - esr = esr & ESR_ELx_FSC; +const u8 fault =esr_fsc_get_fault (esr); -return (esr ==ESR_ELx_FSC_SEA_TTW (3 )) || - (esr ==ESR_ELx_FSC_SEA_TTW (2 )) || - (esr ==ESR_ELx_FSC_SEA_TTW (1 )) || - (esr ==ESR_ELx_FSC_SEA_TTW (0 )) || - (esr ==ESR_ELx_FSC_SEA_TTW (-1 )); +return (fault ==ESR_ELx_FSC_SEA_TTW (3 )) || + (fault ==ESR_ELx_FSC_SEA_TTW (2 )) || + (fault ==ESR_ELx_FSC_SEA_TTW (1 )) || + (fault ==ESR_ELx_FSC_SEA_TTW (0 )) || + (fault ==ESR_ELx_FSC_SEA_TTW (-1 )); }static inline bool esr_fsc_is_secc_ttw (unsigned long esr) { - esr = esr & ESR_ELx_FSC; +const u8 fault =esr_fsc_get_fault (esr); -return (esr ==ESR_ELx_FSC_SECC_TTW (3 )) || - (esr ==ESR_ELx_FSC_SECC_TTW (2 )) || - (esr ==ESR_ELx_FSC_SECC_TTW (1 )) || - (esr ==ESR_ELx_FSC_SECC_TTW (0 )) || - (esr ==ESR_ELx_FSC_SECC_TTW (-1 )); +return (fault ==ESR_ELx_FSC_SECC_TTW (3 )) || + (fault ==ESR_ELx_FSC_SECC_TTW (2 )) || + (fault ==ESR_ELx_FSC_SECC_TTW (1 )) || + (fault ==ESR_ELx_FSC_SECC_TTW (0 )) || + (fault ==ESR_ELx_FSC_SECC_TTW (-1 )); }/* Indicate whether ESR.EC==0x1A is for an ERETAx instruction */ arch/arm64/include/asm/kvm_emulate.h +15
-37@@ -411,18 +411,13 @@ static __always_inline int kvm_vcpu_dabt_get_rd(const struct kvm_vcpu *vcpu) static __always_inline bool kvm_vcpu_abt_iss1tw(const struct kvm_vcpu *vcpu) { -return !!(kvm_vcpu_get_esr (vcpu) & ESR_ELx_S1PTW); +return esr_abt_is_s1ptw (kvm_vcpu_get_esr (vcpu)); }/* Always check for S1PTW *before* using this. */ static __always_inline bool kvm_vcpu_dabt_iswrite(const struct kvm_vcpu *vcpu) { -return kvm_vcpu_get_esr (vcpu) & ESR_ELx_WNR; -} - -static inline bool kvm_vcpu_dabt_is_cm (const struct kvm_vcpu *vcpu) -{ -return !!(kvm_vcpu_get_esr (vcpu) & ESR_ELx_CM); +return esr_dabt_is_write (kvm_vcpu_get_esr (vcpu)); } static __always_inline unsigned int kvm_vcpu_dabt_get_as(const struct kvm_vcpu *vcpu)@@ -438,17 +433,12 @@ static __always_inline bool kvm_vcpu_trap_il_is32bit(const struct kvm_vcpu *vcpu static __always_inline u8 kvm_vcpu_trap_get_class(const struct kvm_vcpu *vcpu) { -return ESR_ELx_EC (kvm_vcpu_get_esr (vcpu)); +return esr_trap_get_class (kvm_vcpu_get_esr (vcpu)); }static inline bool kvm_vcpu_trap_is_iabt (const struct kvm_vcpu *vcpu) { -return kvm_vcpu_trap_get_class (vcpu) == ESR_ELx_EC_IABT_LOW; -} - -static inline bool kvm_vcpu_trap_is_exec_fault (const struct kvm_vcpu *vcpu) -{ -return kvm_vcpu_trap_is_iabt (vcpu) && !kvm_vcpu_abt_iss1tw (vcpu); +return esr_trap_is_iabt (kvm_vcpu_get_esr (vcpu)); } static __always_inline u8 kvm_vcpu_trap_get_fault(const struct kvm_vcpu *vcpu)@@ -468,26 +458,9 @@ bool kvm_vcpu_trap_is_translation_fault(const struct kvm_vcpu *vcpu)return esr_fsc_is_translation_fault (kvm_vcpu_get_esr (vcpu)); } -static inline -u64 kvm_vcpu_trap_get_perm_fault_granule (const struct kvm_vcpu *vcpu) -{ -unsigned long esr =kvm_vcpu_get_esr (vcpu); - -BUG_ON (!esr_fsc_is_permission_fault (esr)); -return BIT (ARM64_HW_PGTABLE_LEVEL_SHIFT (esr & ESR_ELx_FSC_LEVEL)); -} - static __always_inline bool kvm_vcpu_abt_issea(const struct kvm_vcpu *vcpu) { -switch (kvm_vcpu_trap_get_fault (vcpu)) { -case ESR_ELx_FSC_EXTABT: - case ESR_ELx_FSC_SEA_TTW(-1) ... ESR_ELx_FSC_SEA_TTW(3): -case ESR_ELx_FSC_SECC: - case ESR_ELx_FSC_SECC_TTW(-1) ... ESR_ELx_FSC_SECC_TTW(3): -return true; -default : -return false; - } +return esr_abt_is_sea (kvm_vcpu_get_esr (vcpu)); } static __always_inline int kvm_vcpu_sys_get_rt(struct kvm_vcpu *vcpu)@@ -496,9 +469,9 @@ static __always_inline int kvm_vcpu_sys_get_rt(struct kvm_vcpu *vcpu)return ESR_ELx_SYS64_ISS_RT (esr); } -static inline bool kvm_is_write_fault (struct kvm_vcpu *vcpu) +static inline bool esr_abt_is_write_fault (unsigned long esr) { -if (kvm_vcpu_abt_iss1tw (vcpu)) { +if (esr_abt_is_s1ptw (esr)) {/* * Only a permission fault on a S1PTW should be * considered as a write. Otherwise, page tables baked @@ -511,13 +484,18 @@ static inline bool kvm_is_write_fault(struct kvm_vcpu *vcpu)* first), then a permission fault to allow the flags * to be set. */ -return kvm_vcpu_trap_is_permission_fault (vcpu); +return esr_fsc_is_permission_fault (esr); } -if (kvm_vcpu_trap_is_iabt (vcpu)) +if (esr_trap_is_iabt (esr))return false; -return kvm_vcpu_dabt_iswrite (vcpu); +return esr_dabt_is_write (esr); +} + +static inline bool kvm_is_write_fault (struct kvm_vcpu *vcpu) +{ +return esr_abt_is_write_fault (kvm_vcpu_get_esr (vcpu)); }static inline unsigned long kvm_vcpu_get_mpidr_aff (struct kvm_vcpu *vcpu)arch/arm64/kvm/mmu.c +21
-0@@ -1603,12 +1603,38 @@ struct kvm_s2_fault_desc {struct kvm_s2_trans *nested;struct kvm_memory_slot *memslot;unsigned long hva; +unsigned long esr; }; +static bool kvm_s2_fault_is_perm (const struct kvm_s2_fault_desc *s2fd) +{ +return esr_fsc_is_permission_fault (s2fd->esr); +} + +static bool kvm_s2_fault_is_exec (const struct kvm_s2_fault_desc *s2fd) +{ +return esr_abt_is_exec_fault (s2fd->esr); +} + +static bool kvm_s2_fault_is_write (const struct kvm_s2_fault_desc *s2fd) +{ +return esr_abt_is_write_fault (s2fd->esr); +} + +static u64 kvm_s2_perm_fault_granule (const struct kvm_s2_fault_desc *s2fd) +{ +u64 level; + +if (!kvm_s2_fault_is_perm (s2fd))
arch/arm64/kvm/mmu.c struct kvm_s2_fault_desc { · line 1637
Quoted 10 lines
arch/arm64/kvm/mmu.c +6
-1+return 0 ; + level =esr_fsc_get_level (s2fd->esr); +return BIT (ARM64_HW_PGTABLE_LEVEL_SHIFT (level)); +} +static int gmem_abort (const struct kvm_s2_fault_desc *s2fd) {bool write_fault, exec_fault; -bool perm_fault =kvm_vcpu_trap_is_permission_fault (s2fd->vcpu); +const bool perm_fault =kvm_s2_fault_is_perm (s2fd);
Please don't randomly introduce const local variables. I understand the benefit, but *if* we want to go down that road, then we do it for all the predicates, as a separate series, because this obviously applies to {exec,write}_fault as well.
Thanks,
M.
Signature · 1 line
Without deviation from the norm, progress is not possible.
Marc Zyngier
maintainerRe: [PATCH 3/8] KVM: arm64: Propagate and use kvm_s2_fault_result on S2 fault
In reply to Lorenzo Stoakes (ARM)Quoted 55 lines · Lorenzo Stoakes (ARM)
When stage 2 page tables fault the net result may either be that a page is mapped, an error occurred or the fault should be retried (
-EAGAIN).When a fault succeeds it may be upgraded to a PMD size via
transparent_hugepage_adjust().In order to support KVM pre-faulting the outcome of the fault and the mapping size must be recorded.
Track this in the new
kvm_s2_fault_resultstruct, which is threaded throughgmem_abort(),user_mem_abort()andkvm_s2_fault_map().PKVM and SEA aren't relevant to synthetic pre-faulting so neither
kvm_inject_sea()norpkvm_mem_abort()are altered.Actual hardware faulting doesn't require this information, so
kvm_handle_guest_abort()simply passes NULLkvm_s2_fault_resulttogmem_abort()anduser_mem_abort().Faults are necessarily ephemeral and pre-faulting can't guarantee what may happen in parallel, so do not store the GFN or PFN in
kvm_s2_fault_result. Pre-faulting only needs to know what was mapped in at the point of the fault.This struct could be replaced with a pointer to an unsigned long, however it's clearer to separate out the mapped flag and having a struct allows us to easily add additional fields in future as needed.
No functional change intended.
Suggested-by Vincent Donnefort <[email protected]>Signed-off-by Lorenzo Stoakes (ARM) <[email protected]>--- arch/arm64/kvm/mmu.c | 33 +++++++++++++++++++++++++++------ 1 file changed, 27 insertions(+), 6 deletions(-)arch/arm64/kvm/mmu.c +5
-0@@ -1607,6 +1607,11 @@ struct kvm_s2_fault_desc {struct kvm_s2_mmu *mmu; }; +struct kvm_s2_fault_result { +unsigned long mapping_size; +bool mapped; +}; +static bool kvm_s2_fault_is_perm (const struct kvm_s2_fault_desc *s2fd) {return esr_fsc_is_permission_fault (s2fd->esr);@@ -1632,7 +1637,17 @@ static u64 kvm_s2_perm_fault_granule(const struct kvm_s2_fault_desc *s2fd)return BIT (ARM64_HW_PGTABLE_LEVEL_SHIFT (level));
arch/arm64/kvm/mmu.c static u64 kvm_s2_perm_fault_granule(const struct kvm_s2_fault_desc *s2fd) · line 1646
Quoted 10 lines
arch/arm64/kvm/mmu.c +7
-1} -static int gmem_abort (const struct kvm_s2_fault_desc *s2fd) +static void populate_fault_result (struct kvm_s2_fault_result *result, +unsigned long mapping_size) +{ +/* A THP upgrade may have altered mapping size. */ + result->mapping_size = mapping_size; +/* -EAGAIN is swallowed so be explicit when we actually map. */ + result->mapped = true;
I'm not sold on this boolean. I'd rather we use the fact that the fault handler has passed a result pointer to return -EAGAIN rather than turning into a 0, because that's a clear sign that the fault hasn't been generated by a vcpu.
M.
Signature · 1 line
Without deviation from the norm, progress is not possible.
Re: [PATCH 3/8] KVM: arm64: Propagate and use kvm_s2_fault_result on S2 fault
Quoted 25 lines · Marc Zyngier
Quoted 23 lines · Lorenzo Stoakes (ARM)
arch/arm64/kvm/mmu.c | 33 +++++++++++++++++++++++++++------ 1 file changed, 27 insertions(+), 6 deletions(-)arch/arm64/kvm/mmu.c +5
-0@@ -1607,6 +1607,11 @@ struct kvm_s2_fault_desc {struct kvm_s2_mmu *mmu; }; +struct kvm_s2_fault_result { +unsigned long mapping_size; +bool mapped; +}; +static bool kvm_s2_fault_is_perm (const struct kvm_s2_fault_desc *s2fd) {return esr_fsc_is_permission_fault (s2fd->esr);@@ -1632,7 +1637,17 @@ static u64 kvm_s2_perm_fault_granule(const struct kvm_s2_fault_desc *s2fd)return BIT (ARM64_HW_PGTABLE_LEVEL_SHIFT (level)); }
Quoted 13 lines
arch/arm64/kvm/mmu.c static u64 kvm_s2_perm_fault_granule(const struct kvm_s2_fault_desc *s2fd) · line 1646
Quoted 8 lines
arch/arm64/kvm/mmu.c +7
-1-static int gmem_abort (const struct kvm_s2_fault_desc *s2fd) +static void populate_fault_result (struct kvm_s2_fault_result *result, +unsigned long mapping_size) +{ +/* A THP upgrade may have altered mapping size. */ + result->mapping_size = mapping_size; +/* -EAGAIN is swallowed so be explicit when we actually map. */ + result->mapped = true;I'm not sold on this boolean. I'd rather we use the fact that the fault handler has passed a result pointer to return
-EAGAINrather than turning into a 0, because that's a clear sign that the fault hasn't been generated by a vcpu.
Yeah I did actually think that myself when writing it :) I was a bit in two minds about how to do this, but yeah that's just a better way, will fix that!
Quoted 6 lines
M.--
Without deviation from the norm, progress is not possible.
--
Cheers, Lorenzo
Re: [PATCH 1/8] KVM: arm64: Propagate and use esr in s2fd when handling guest aborts
In reply to Marc ZyngierQuoted 26 lines · Marc Zyngier
Quoted 24 lines · Lorenzo Stoakes (ARM)
kvm_handle_guest_abort()establishes akvm_s2_fault_descdata structure, s2fd, to store and propagate state to eitherpkvm_mem_abort(),gmem_abort()oruser_mem_abort()handlers.Each of these, however, examines the Exception Syndrome Register (ESR) via
s2fd->vcpu.Introduce an
s2fd->esrfield to abstract this and propagate it to callers.The value of this (beyond refactoring) is to be able to later generate faults with a synthetic esr, specifically to implement stage 2 page table pre-faulting.
Abstract esr-specific predicates and helpers to the
esr.hheader and either have vcpu wrappers call these, or eliminate them if they are not used elsewhere.Provide kvm_s2_fault_is_[write,exec,perm]() helpers for convenience.
Since
kvm_s2_fault_map()either setsperm_fault_granuleto the permission fault granule or 0 if not a permission fault, implementkvm_s2_perm_fault_granule()to do this directly.
Quoted 7 lines
Quoted 4 lines
Abort handlers which use
kvm_s2_fault_desc-gmem_abort()anduser_mem_abort()- now only references2fd->esrand do not look it up in any other way, which makes it safe to pass a synthetics2fd->esrvalue to these functions.Please split this. ESR helpers in one patch, hacking the MMU code to use it in another,
s2fd->esrstuff last.
Ack will do!
Quoted 19 lines
Quoted 19 lines
No functional change intended.
Signed-off-by Lorenzo Stoakes (ARM) <[email protected]>--- arch/arm64/include/asm/esr.h | 129 +++++++++++++++++++++++++---------- arch/arm64/include/asm/kvm_emulate.h | 52 ++++---------- arch/arm64/kvm/mmu.c | 80 +++++++++++++--------- 3 files changed, 156 insertions(+), 105 deletions(-)arch/arm64/include/asm/esr.h +0
-0@@ -437,6 +437,32 @@#ifndef __ASSEMBLER__#include <asm/types.h>
Quoted 5 lines
arch/arm64/include/asm/esr.h line 440
Quoted 1 line
arch/arm64/include/asm/esr.h +1
-0+static inline u8 esr_trap_get_class (unsigned long esr)nit: there is no notion of trap here. This is simply extracting the EC from the ESR. My personal (and wholly unreliable) taste would be to go for something like
esr_get_ec().
Ack will change!
Quoted 4 lines
I appreciate that you are simply propagating the names used in KVM, but they were pretty poor the first place, and have only been kept to avoid churn.
Yeah, when in Rome etc. :)
Quoted 3 lines
Also, 'inline' is a bit of a problem given that the callers are
__always_inlinefor good reasons (see5c37f1ae1c3358).
Ack will fix up.
Quoted 43 lines
Quoted 43 lines
arch/arm64/include/asm/esr.h +31
-1+{ +return ESR_ELx_EC (esr); +} + +static inline bool esr_trap_is_iabt (unsigned long esr) +{ +return esr_trap_get_class (esr) == ESR_ELx_EC_IABT_LOW; +} + +static inline bool esr_abt_is_s1ptw (unsigned long esr) +{ +return esr & ESR_ELx_S1PTW; +} + +/* Always check for S1PTW *before* using this. */ +static inline bool esr_dabt_is_write (unsigned long esr) +{ +return esr & ESR_ELx_WNR; +} + +static inline bool esr_dabt_is_cm (unsigned long esr) +{ +return esr & ESR_ELx_CM; +} +static inline unsigned long esr_brk_comment (unsigned long esr) {return esr & ESR_ELx_BRK64_ISS_COMMENT_MASK;@@ -460,75 +486,104 @@ static inline bool esr_is_ubsan_brk(unsigned long esr)return (esr_brk_comment (esr) & ~UBSAN_BRK_MASK ) ==UBSAN_BRK_IMM ; } +static inline u8 esr_fsc_get_fault (unsigned long esr) +{ +return esr & ESR_ELx_FSC; +} +static inline bool esr_fsc_is_translation_fault (unsigned long esr) { - esr = esr & ESR_ELx_FSC; +const u8 fault =esr_fsc_get_fault (esr);
Quoted 12 lines
arch/arm64/include/asm/esr.h static inline bool esr_is_ubsan_brk(unsigned long esr) · line 502
Quoted 10 lines
arch/arm64/include/asm/esr.h +5
-5-return (esr ==ESR_ELx_FSC_FAULT_L (3 )) || - (esr ==ESR_ELx_FSC_FAULT_L (2 )) || - (esr ==ESR_ELx_FSC_FAULT_L (1 )) || - (esr ==ESR_ELx_FSC_FAULT_L (0 )) || - (esr ==ESR_ELx_FSC_FAULT_L (-1 )); +return (fault ==ESR_ELx_FSC_FAULT_L (3 )) || + (fault ==ESR_ELx_FSC_FAULT_L (2 )) || + (fault ==ESR_ELx_FSC_FAULT_L (1 )) || + (fault ==ESR_ELx_FSC_FAULT_L (0 )) || + (fault ==ESR_ELx_FSC_FAULT_L (-1 ));I really think we could do without this sort of churn.
Sure, will avoid the var name changes etc. on respin.
Quoted 78 lines
Quoted 78 lines
arch/arm64/include/asm/esr.h +39
-19}static inline bool esr_fsc_is_permission_fault (unsigned long esr) { - esr = esr & ESR_ELx_FSC; +const u8 fault =esr_fsc_get_fault (esr); -return (esr ==ESR_ELx_FSC_PERM_L (3 )) || - (esr ==ESR_ELx_FSC_PERM_L (2 )) || - (esr ==ESR_ELx_FSC_PERM_L (1 )) || - (esr ==ESR_ELx_FSC_PERM_L (0 )); +return (fault ==ESR_ELx_FSC_PERM_L (3 )) || + (fault ==ESR_ELx_FSC_PERM_L (2 )) || + (fault ==ESR_ELx_FSC_PERM_L (1 )) || + (fault ==ESR_ELx_FSC_PERM_L (0 )); }static inline bool esr_fsc_is_access_flag_fault (unsigned long esr) { - esr = esr & ESR_ELx_FSC; +const u8 fault =esr_fsc_get_fault (esr); -return (esr ==ESR_ELx_FSC_ACCESS_L (3 )) || - (esr ==ESR_ELx_FSC_ACCESS_L (2 )) || - (esr ==ESR_ELx_FSC_ACCESS_L (1 )) || - (esr ==ESR_ELx_FSC_ACCESS_L (0 )); +return (fault ==ESR_ELx_FSC_ACCESS_L (3 )) || + (fault ==ESR_ELx_FSC_ACCESS_L (2 )) || + (fault ==ESR_ELx_FSC_ACCESS_L (1 )) || + (fault ==ESR_ELx_FSC_ACCESS_L (0 )); }static inline bool esr_fsc_is_excl_atomic_fault (unsigned long esr) { - esr = esr & ESR_ELx_FSC; - -return esr == ESR_ELx_FSC_EXCL_ATOMIC; +return esr_fsc_get_fault (esr) == ESR_ELx_FSC_EXCL_ATOMIC; }static inline bool esr_fsc_is_addr_sz_fault (unsigned long esr) { - esr &= ESR_ELx_FSC; +const u8 fault =esr_fsc_get_fault (esr); + +return (fault ==ESR_ELx_FSC_ADDRSZ_L (3 )) || + (fault ==ESR_ELx_FSC_ADDRSZ_L (2 )) || + (fault ==ESR_ELx_FSC_ADDRSZ_L (1 )) || + (fault ==ESR_ELx_FSC_ADDRSZ_L (0 )) || + (fault ==ESR_ELx_FSC_ADDRSZ_L (-1 )); +} + +static inline bool esr_abt_is_exec_fault (unsigned long esr) +{ +return esr_trap_is_iabt (esr) && !esr_abt_is_s1ptw (esr); +} -return (esr ==ESR_ELx_FSC_ADDRSZ_L (3 )) || - (esr ==ESR_ELx_FSC_ADDRSZ_L (2 )) || - (esr ==ESR_ELx_FSC_ADDRSZ_L (1 )) || - (esr ==ESR_ELx_FSC_ADDRSZ_L (0 )) || - (esr ==ESR_ELx_FSC_ADDRSZ_L (-1 )); +static inline bool esr_abt_is_sea (unsigned long esr) +{ +const u8 fault =esr_fsc_get_fault (esr); + +switch (fault) { +case ESR_ELx_FSC_EXTABT: + case ESR_ELx_FSC_SEA_TTW(-1) ... ESR_ELx_FSC_SEA_TTW(3): +case ESR_ELx_FSC_SECC: + case ESR_ELx_FSC_SECC_TTW(-1) ... ESR_ELx_FSC_SECC_TTW(3): +return true; +default : +return false; + } +} +
Quoted 8 lines
arch/arm64/include/asm/esr.h static inline bool esr_is_ubsan_brk(unsigned long esr) · line 565
Quoted 5 lines
arch/arm64/include/asm/esr.h +4
-0+/* Not valid for negative levels. */ +static inline u64 esr_fsc_get_level (unsigned long esr) +{ +return esr & ESR_ELx_FSC_LEVEL; }If that's such an unreliable helper, why is it exposed to everyone instead of being kept local to the single caller?
That's a fair point :) will open code instead.
Quoted 182 lines
Quoted 182 lines
arch/arm64/include/asm/esr.h +12
-12static inline bool esr_fsc_is_sea_ttw (unsigned long esr) { - esr = esr & ESR_ELx_FSC; +const u8 fault =esr_fsc_get_fault (esr); -return (esr ==ESR_ELx_FSC_SEA_TTW (3 )) || - (esr ==ESR_ELx_FSC_SEA_TTW (2 )) || - (esr ==ESR_ELx_FSC_SEA_TTW (1 )) || - (esr ==ESR_ELx_FSC_SEA_TTW (0 )) || - (esr ==ESR_ELx_FSC_SEA_TTW (-1 )); +return (fault ==ESR_ELx_FSC_SEA_TTW (3 )) || + (fault ==ESR_ELx_FSC_SEA_TTW (2 )) || + (fault ==ESR_ELx_FSC_SEA_TTW (1 )) || + (fault ==ESR_ELx_FSC_SEA_TTW (0 )) || + (fault ==ESR_ELx_FSC_SEA_TTW (-1 )); }static inline bool esr_fsc_is_secc_ttw (unsigned long esr) { - esr = esr & ESR_ELx_FSC; +const u8 fault =esr_fsc_get_fault (esr); -return (esr ==ESR_ELx_FSC_SECC_TTW (3 )) || - (esr ==ESR_ELx_FSC_SECC_TTW (2 )) || - (esr ==ESR_ELx_FSC_SECC_TTW (1 )) || - (esr ==ESR_ELx_FSC_SECC_TTW (0 )) || - (esr ==ESR_ELx_FSC_SECC_TTW (-1 )); +return (fault ==ESR_ELx_FSC_SECC_TTW (3 )) || + (fault ==ESR_ELx_FSC_SECC_TTW (2 )) || + (fault ==ESR_ELx_FSC_SECC_TTW (1 )) || + (fault ==ESR_ELx_FSC_SECC_TTW (0 )) || + (fault ==ESR_ELx_FSC_SECC_TTW (-1 )); }/* Indicate whether ESR.EC==0x1A is for an ERETAx instruction */ arch/arm64/include/asm/kvm_emulate.h +15
-37@@ -411,18 +411,13 @@ static __always_inline int kvm_vcpu_dabt_get_rd(const struct kvm_vcpu *vcpu) static __always_inline bool kvm_vcpu_abt_iss1tw(const struct kvm_vcpu *vcpu) { -return !!(kvm_vcpu_get_esr (vcpu) & ESR_ELx_S1PTW); +return esr_abt_is_s1ptw (kvm_vcpu_get_esr (vcpu)); }/* Always check for S1PTW *before* using this. */ static __always_inline bool kvm_vcpu_dabt_iswrite(const struct kvm_vcpu *vcpu) { -return kvm_vcpu_get_esr (vcpu) & ESR_ELx_WNR; -} - -static inline bool kvm_vcpu_dabt_is_cm (const struct kvm_vcpu *vcpu) -{ -return !!(kvm_vcpu_get_esr (vcpu) & ESR_ELx_CM); +return esr_dabt_is_write (kvm_vcpu_get_esr (vcpu)); } static __always_inline unsigned int kvm_vcpu_dabt_get_as(const struct kvm_vcpu *vcpu)@@ -438,17 +433,12 @@ static __always_inline bool kvm_vcpu_trap_il_is32bit(const struct kvm_vcpu *vcpu static __always_inline u8 kvm_vcpu_trap_get_class(const struct kvm_vcpu *vcpu) { -return ESR_ELx_EC (kvm_vcpu_get_esr (vcpu)); +return esr_trap_get_class (kvm_vcpu_get_esr (vcpu)); }static inline bool kvm_vcpu_trap_is_iabt (const struct kvm_vcpu *vcpu) { -return kvm_vcpu_trap_get_class (vcpu) == ESR_ELx_EC_IABT_LOW; -} - -static inline bool kvm_vcpu_trap_is_exec_fault (const struct kvm_vcpu *vcpu) -{ -return kvm_vcpu_trap_is_iabt (vcpu) && !kvm_vcpu_abt_iss1tw (vcpu); +return esr_trap_is_iabt (kvm_vcpu_get_esr (vcpu)); } static __always_inline u8 kvm_vcpu_trap_get_fault(const struct kvm_vcpu *vcpu)@@ -468,26 +458,9 @@ bool kvm_vcpu_trap_is_translation_fault(const struct kvm_vcpu *vcpu)return esr_fsc_is_translation_fault (kvm_vcpu_get_esr (vcpu)); } -static inline -u64 kvm_vcpu_trap_get_perm_fault_granule (const struct kvm_vcpu *vcpu) -{ -unsigned long esr =kvm_vcpu_get_esr (vcpu); - -BUG_ON (!esr_fsc_is_permission_fault (esr)); -return BIT (ARM64_HW_PGTABLE_LEVEL_SHIFT (esr & ESR_ELx_FSC_LEVEL)); -} - static __always_inline bool kvm_vcpu_abt_issea(const struct kvm_vcpu *vcpu) { -switch (kvm_vcpu_trap_get_fault (vcpu)) { -case ESR_ELx_FSC_EXTABT: - case ESR_ELx_FSC_SEA_TTW(-1) ... ESR_ELx_FSC_SEA_TTW(3): -case ESR_ELx_FSC_SECC: - case ESR_ELx_FSC_SECC_TTW(-1) ... ESR_ELx_FSC_SECC_TTW(3): -return true; -default : -return false; - } +return esr_abt_is_sea (kvm_vcpu_get_esr (vcpu)); } static __always_inline int kvm_vcpu_sys_get_rt(struct kvm_vcpu *vcpu)@@ -496,9 +469,9 @@ static __always_inline int kvm_vcpu_sys_get_rt(struct kvm_vcpu *vcpu)return ESR_ELx_SYS64_ISS_RT (esr); } -static inline bool kvm_is_write_fault (struct kvm_vcpu *vcpu) +static inline bool esr_abt_is_write_fault (unsigned long esr) { -if (kvm_vcpu_abt_iss1tw (vcpu)) { +if (esr_abt_is_s1ptw (esr)) {/* * Only a permission fault on a S1PTW should be * considered as a write. Otherwise, page tables baked @@ -511,13 +484,18 @@ static inline bool kvm_is_write_fault(struct kvm_vcpu *vcpu)* first), then a permission fault to allow the flags * to be set. */ -return kvm_vcpu_trap_is_permission_fault (vcpu); +return esr_fsc_is_permission_fault (esr); } -if (kvm_vcpu_trap_is_iabt (vcpu)) +if (esr_trap_is_iabt (esr))return false; -return kvm_vcpu_dabt_iswrite (vcpu); +return esr_dabt_is_write (esr); +} + +static inline bool kvm_is_write_fault (struct kvm_vcpu *vcpu) +{ +return esr_abt_is_write_fault (kvm_vcpu_get_esr (vcpu)); }static inline unsigned long kvm_vcpu_get_mpidr_aff (struct kvm_vcpu *vcpu)arch/arm64/kvm/mmu.c +26
-0@@ -1603,12 +1603,38 @@ struct kvm_s2_fault_desc {struct kvm_s2_trans *nested;struct kvm_memory_slot *memslot;unsigned long hva; +unsigned long esr; }; +static bool kvm_s2_fault_is_perm (const struct kvm_s2_fault_desc *s2fd) +{ +return esr_fsc_is_permission_fault (s2fd->esr); +} + +static bool kvm_s2_fault_is_exec (const struct kvm_s2_fault_desc *s2fd) +{ +return esr_abt_is_exec_fault (s2fd->esr); +} + +static bool kvm_s2_fault_is_write (const struct kvm_s2_fault_desc *s2fd) +{ +return esr_abt_is_write_fault (s2fd->esr); +} + +static u64 kvm_s2_perm_fault_granule (const struct kvm_s2_fault_desc *s2fd) +{ +u64 level; + +if (!kvm_s2_fault_is_perm (s2fd)) +return 0 ; + level =esr_fsc_get_level (s2fd->esr); +return BIT (ARM64_HW_PGTABLE_LEVEL_SHIFT (level)); +} +static int gmem_abort (const struct kvm_s2_fault_desc *s2fd) {
Quoted 8 lines
arch/arm64/kvm/mmu.c struct kvm_s2_fault_desc { · line 1637
Quoted 3 lines
arch/arm64/kvm/mmu.c +1
-1bool write_fault, exec_fault; -bool perm_fault =kvm_vcpu_trap_is_permission_fault (s2fd->vcpu); +const bool perm_fault =kvm_s2_fault_is_perm (s2fd);Please don't randomly introduce const local variables. I understand the benefit, but *if* we want to go down that road, then we do it for all the predicates, as a separate series, because this obviously applies to {exec,write}_fault as well.
Ack, force of habit :) will avoid on respin.
Quoted 7 lines
Thanks,
M.--
Without deviation from the norm, progress is not possible.
--
Cheers, Lorenzo
Marc Zyngier
maintainerRe: [PATCH 5/8] KVM: arm64: Implement KVM_PRE_FAULT_MEMORY
In reply to Lorenzo Stoakes (ARM)Quoted 11 lines · Lorenzo Stoakes (ARM)
Implement KVM stage 2 page table pre-faulting for the arm64 architecture.
Do the trivial plumbing by selecting
CONFIG_KVM_GENERIC_PRE_FAULT_MEMORYinKconfig, returning 1 inkvm_vm_ioctl_check_extension()forKVM_CAP_PRE_FAULT_MEMORYbut false inkvm_pkvm_ext_allowed()to disallow its use for pKVM.Add
kvm_arch_vcpu_pre_fault_memory()to actually implement the feature by pre-faulting stage 2 page tables for a specific GPA.
Quoted 8 lines
Previous commits added the required foundations -
gmem_abort()anduser_mem_abort()determine esr and mmu froms2fd->esrands2fd->mmurespectively, the granule size is returned by them inkvm_s2_fault_resultandkvm_pgtable_get_leaf()accepts a walk flags parameter.Additionally,
kvm_pgtable_get_leaf()now accepts a walk flag, meaning its page table walk can be performed withKVM_PGTABLE_WALK_SHAREDset and thus can be performed under a readmmu_lock.
We really don't need a recap of the previous episodes... ;-)
Quoted 44 lines
With these changes in place, implement pre-faulting by first trying a page table walk under the MMU read lock then, if it fails, injecting a synthetic data abort at the page table level at which the page table walk failed.
This is necessarily racey as reclaim might happen at any time. Successfully pre-faulting can therefore only guarantee that each GPA was observed to be mapped at least once.
Protected KVM (pKVM) is not supported at all because pKVM creates VMs and vCPUs when first run, meaning any attempt to pre-fault prior to this cannot succeed.
Since the sensible use case for pre-faulting is doing so prior to vCPU run, and supporting only online pKVM vCPUs is confusing and inconsistent, simply don't support this at all.
There is a subtlety when retrieving the hva: it seems natural to use
gfn_to_hva_memslot(), but this errors out for read-only memslots.Since pre-faulting should fault in both read-only and read/write hvas, this isn't the correct API to use here.
gfn_to_hva_memslot_prot()allows retrieval of read-only hvas, but has unclear semantics, so introducegfn_to_hva_memslot_read()to wrap it.A retry mechanic is implemented when
user_mem_abort()orgmem_abort()fail to map memory due to a benign failure where a hardware abort would not result in an error.These occur when the abort handler was raced by either an invalidation MMU notifier or a racing abort path.
Since these faults are highly likely to succeed on immediate retry, retry up to
MAX_PRE_FAULT_RETRIEStimes, after which-EAGAINis ultimately returned to the caller.An invalid memslot (i.e. a memslot with the
KVM_MEMSLOT_INVALIDflag set) also results in the operation returning-EAGAINwithout a retry mechanic.This is because an invalid memslot means the pre-fault operation raced with memslot reclaim, and since the SRCU lock is held, progress cannot be made.
Quoted 8 lines
Therefore, when this happens,
-EAGAINindicates that userland should retry the ioctl, which will bounce the SRCU lock and permit forward progress.Finally, update the KVM API documentation to describe the changes, providing arm64-specific implementation details.
This work is based with gratitude on Jack Thomson's original series, its previous revisions and the feedback they received.
Honestly, this whole commit message reads like a cover letter. Consider writing something more succinct, which would be helped by splitting the patch: if you have so much to say for a single patch, that's probably that the patch is doing too many things.
Quoted 40 lines
Signed-off-by Lorenzo Stoakes (ARM) <[email protected]>--- Documentation/virt/kvm/api.rst | 17 +++- arch/arm64/include/asm/kvm_pkvm.h | 2 +- arch/arm64/kvm/Kconfig | 1 + arch/arm64/kvm/arm.c | 1 + arch/arm64/kvm/mmu.c | 192 +++++++++++++++++++++++++++++++++++--- 5 files changed, 198 insertions(+), 15 deletions(-)Documentation/virt/kvm/api.rst +3
-1@@ -6485,7 +6485,7 @@ See KVM_SET_USER_MEMORY_REGION2 for additional details. --------------------------- :Capability: KVM_CAP_PRE_FAULT_MEMORY -:Architectures: none +:Architectures: x86, s390, arm64 :Type: vcpu ioctl :Parameters: struct kvm_pre_fault_memory (in/out) :Returns: 0 if at least one page is processed, < 0 on error@@ -6493,12 +6493,14 @@ See KVM_SET_USER_MEMORY_REGION2 for additional details. Errors: ========== =============================================================== + EAGAIN A race occurred before progress was made, but a retry may succeed. EINVAL The specified `gpa` and `size` were invalid (e.g. not page aligned, causes an overflow, or size is zero), or the VM is UCONTROL (s390). ENOENT The specified `gpa` is outside defined memslots. EINTR An unmasked signal is pending and no page was processed. EFAULT The parameter address was invalid. + EHWPOISON A poisoned host page was encountered. EOPNOTSUPP Mapping memory for a GPA is unsupported by the hypervisor, and/or for the current vCPU state/mode. EIO unexpected error conditions (also causes a WARN)
Documentation/virt/kvm/api.rst Errors: · line 6524
Quoted 6 lines
Documentation/virt/kvm/api.rst +2
-1@@ -6518,7 +6520,16 @@ Errors: KVM_PRE_FAULT_MEMORY populates KVM's stage-2 page tables used to map memory for the current vCPU state. KVM maps memory as if the vCPU generated a stage-2 read page fault, e.g. faults in memory as needed, but doesn't break -CoW. On x86, KVM does not mark any newly created stage-2 PTE as Accessed. +CoW. On arm64, KVM marks both existing and newly created stage-2 PTEs as +Accessed, on x86 it does not and for s390 it is not applicable.
Meh. See below.
Documentation/virt/kvm/api.rst Errors: · line 6532
Quoted 8 lines
Documentation/virt/kvm/api.rst +8
-0+ +On arm64, a GPA is interpreted as an IPA, and never interpreted as the IPA +of a nested guest. Pre-faulting only populates canonical stage 2 page +tables. + +The feature is not supported on arm64 if the protected KVM (pKVM) feature +is enabled, as that results in vCPUs being instantiated on first run, which +renders pre-faulting useless.
I don't think we need to justify why this is not available with pKVM. I expect pKVM to eventually support this through a slightly different mechanism anyway.
Documentation/virt/kvm/api.rst When the ioctl returns, the input values are updated to point to the · line 6547
Quoted 9 lines
Documentation/virt/kvm/api.rst +1
-1In the case of confidential VM types where there is an initial set up of private guest memory before the guest is 'finalized'/measured, this ioctl@@ -6533,7 +6544,7 @@ When the ioctl returns, the input values are updated to point to the remaining range. If `size` > 0 on return, the caller can just issue the ioctl again with the same `struct kvm_map_memory` argument. -Shadow page tables cannot support this ioctl because they +On x86, Shadow page tables cannot support this ioctl because they
shadow
Documentation/virt/kvm/api.rst When the ioctl returns, the input values are updated to point to the · line 6550
Quoted 3 lines
Documentation/virt/kvm/api.rst +0
-0are indexed by virtual address or nested guest physical address. Calling this ioctl when the guest is using shadow page tables (for example because it is running a nested guest with nested page tables)
Documentation updates in a separate patch, please.
Quoted 52 lines
arch/arm64/include/asm/kvm_pkvm.h +1
-1@@ -44,9 +44,9 @@ static inline bool kvm_pkvm_ext_allowed(struct kvm *kvm, long ext)case KVM_CAP_ARM_PTRAUTH_GENERIC :return true;case KVM_CAP_ARM_MTE : -return false;case KVM_CAP_ARM_EAGER_SPLIT_CHUNK_SIZE :case KVM_CAP_ARM_SUPPORTED_BLOCK_SIZES : +case KVM_CAP_PRE_FAULT_MEMORY :return false;default :return !kvm || !kvm_vm_is_protected (kvm);arch/arm64/kvm/Kconfig +1
-0@@ -37,6 +37,7 @@ menuconfig KVM select SCHED_INFO select GUEST_PERF_EVENTS if PERF_EVENTS select KVM_GUEST_MEMFD + select KVM_GENERIC_PRE_FAULT_MEMORY help Support hosting virtualized guest machines.arch/arm64/kvm/arm.c +1
-0@@ -393,6 +393,7 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)case KVM_CAP_COUNTER_OFFSET :case KVM_CAP_ARM_WRITABLE_IMP_ID_REGS :case KVM_CAP_ARM_SEA_TO_USER : +case KVM_CAP_PRE_FAULT_MEMORY : r =1 ;break ;case KVM_CAP_SET_GUEST_DEBUG2 :arch/arm64/kvm/mmu.c +1
-0@@ -5,6 +5,7 @@*/ #include <linux/acpi.h> +#include <linux/cleanup.h> #include <linux/mman.h> #include <linux/kvm_host.h> #include <linux/io.h>
arch/arm64/kvm/mmu.c static void *get_mmu_memcache(struct kvm_vcpu *vcpu) · line 1560
Quoted 8 lines
arch/arm64/kvm/mmu.c +2
-2@@ -1554,9 +1555,9 @@ static void *get_mmu_memcache(struct kvm_vcpu *vcpu)return &vcpu->arch.pkvm_memcache; } -static int topup_mmu_memcache (struct kvm_vcpu *vcpu,void *memcache) +static int topup_mmu_memcache (struct kvm_s2_mmu *mmu,void *memcache) { -int min_pages =kvm_mmu_cache_min_pages (vcpu->arch.hw_mmu); +const int min_pages =kvm_mmu_cache_min_pages (mmu);
Changes to the memcache interface in a separate patch, please.
arch/arm64/kvm/mmu.c struct kvm_s2_fault_desc { · line 1609
Quoted 8 lines
arch/arm64/kvm/mmu.c +1
-0if (!is_protected_kvm_enabled ())return kvm_mmu_topup_memory_cache (memcache, min_pages);@@ -1605,6 +1606,7 @@ struct kvm_s2_fault_desc {unsigned long hva;unsigned long esr;struct kvm_s2_mmu *mmu; +bool pre_fault;
No, please, that's horrible. See below.
Quoted 22 lines
arch/arm64/kvm/mmu.c +2
-2};struct kvm_s2_fault_result {@@ -1664,7 +1666,7 @@ static int gmem_abort(const struct kvm_s2_fault_desc *s2fd,if (!perm_fault) { memcache =get_mmu_memcache (s2fd->vcpu); - ret =topup_mmu_memcache (s2fd->vcpu, memcache); + ret =topup_mmu_memcache (s2fd->mmu, memcache);if (ret)return ret; }@@ -1761,7 +1763,7 @@ static int pkvm_mem_abort(const struct kvm_s2_fault_desc *s2fd)int ret; hyp_memcache =get_mmu_memcache (vcpu); - ret =topup_mmu_memcache (vcpu, hyp_memcache); + ret =topup_mmu_memcache (s2fd->mmu, hyp_memcache);if (ret)return -ENOMEM ;
arch/arm64/kvm/mmu.c static int kvm_s2_fault_pin_pfn(const struct kvm_s2_fault_desc *s2fd, · line 1959
Quoted 5 lines
arch/arm64/kvm/mmu.c +2
-0@@ -1953,6 +1955,8 @@ static int kvm_s2_fault_pin_pfn(const struct kvm_s2_fault_desc *s2fd, &s2vi->map_writable, &s2vi->page);if (unlikely (is_error_noslot_pfn (s2vi->pfn))) {if (s2vi->pfn ==KVM_PFN_ERR_HWPOISON ) { +if (s2fd->pre_fault) +return -EHWPOISON ;
I'd rather *always* return -EHWPOISON here, and move the signal injection into the callers. That would much more consistent.
Quoted 73 lines
arch/arm64/kvm/mmu.c +39
-7kvm_send_hwpoison_signal (s2fd->hva,__ffs (s2vi->vma_pagesize));return 0 ; }@@ -2163,7 +2167,7 @@ static int user_mem_abort(const struct kvm_s2_fault_desc *s2fd, memcache =get_mmu_memcache (s2fd->vcpu);if (!perm_fault ||memslot_is_logging (s2fd->memslot) ||is_protected_kvm_enabled ()) { - ret =topup_mmu_memcache (s2fd->vcpu, memcache); + ret =topup_mmu_memcache (s2fd->mmu, memcache);if (ret)return ret; }@@ -2185,18 +2189,22 @@ static int user_mem_abort(const struct kvm_s2_fault_desc *s2fd,return kvm_s2_fault_map (s2fd, &s2vi, prot, memcache, result); } -/* Resolve the access fault by making the page young again. */ -static void handle_access_fault (struct kvm_vcpu *vcpu,phys_addr_t fault_ipa) +static void __handle_access_fault (struct kvm_pgtable *pgt,phys_addr_t fault_ipa) {enum kvm_pgtable_walk_flags flags =KVM_PGTABLE_WALK_SHARED ; -struct kvm_s2_mmu *mmu;trace_kvm_access_fault (fault_ipa); +KVM_PGT_FN (kvm_pgtable_stage2_mkyoung)(pgt, fault_ipa, flags); +} + +/* Resolve the access fault by making the page young again. */ +static void handle_access_fault (struct kvm_vcpu *vcpu,phys_addr_t fault_ipa) +{ +struct kvm_s2_mmu *mmu; -read_lock (&vcpu->kvm->mmu_lock); +guard (read_lock)(&vcpu->kvm->mmu_lock); mmu = vcpu->arch.hw_mmu; -KVM_PGT_FN (kvm_pgtable_stage2_mkyoung)(mmu->pgt, fault_ipa, flags); -read_unlock (&vcpu->kvm->mmu_lock); +__handle_access_fault (mmu->pgt, fault_ipa); }/* @@ -2834,3 +2842,165 @@ void kvm_toggle_cache(struct kvm_vcpu *vcpu, bool was_enabled)trace_kvm_toggle_cache (*vcpu_pc (vcpu), was_enabled, now_enabled); } + +static bool kvm_pte_young_s2 (kvm_pte_t pte) +{ +return pte &KVM_PTE_LEAF_ATTR_LO_S2_AF ; +} + +static void kvm_pte_mkyoung_s2 (struct kvm_pgtable *pgt,gpa_t gpa) +{ +struct kvm *kvm =kvm_s2_mmu_to_kvm (pgt->mmu); + +lockdep_assert_held (&kvm->mmu_lock); +/* Despite its name, doesn't fault here. */ +__handle_access_fault (pgt, gpa); +} + +/* +* Try to walk to the specified GPA in canonical mmu - if unmapped returns 0, if +* mapped returns the granule size, otherwise returns an error. +*/ +static long kvm_walk_s2 (struct kvm_pgtable *pgt, +gpa_t gpa,s8 *level) +{ +struct kvm *kvm =kvm_s2_mmu_to_kvm (pgt->mmu); +kvm_pte_t pte; +long ret; + +guard (read_lock)(&kvm->mmu_lock); +
arch/arm64/kvm/mmu.c void kvm_toggle_cache(struct kvm_vcpu *vcpu, bool was_enabled) · line 2882
Quoted 10 lines
arch/arm64/kvm/mmu.c +10
-0+ ret =kvm_pgtable_get_leaf (pgt, gpa, &pte, level, +KVM_PGTABLE_WALK_SHARED ); +if (ret) +return ret; +/* Unpopulated, must fault. */ +if (!kvm_pte_valid (pte)) +return 0 ; +/* Walked the entry so mark young. */ +if (!kvm_pte_young_s2 (pte)) +kvm_pte_mkyoung_s2 (pgt, gpa);
I'm not sold on this either. The contract with userspace is to map the page at S2, not to pretend it has been accessed by the vcpu. I don't see why we should do that, as this is a departure from an established behaviour.
Quoted 41 lines
arch/arm64/kvm/mmu.c +41
-0+return kvm_granule_size (*level); +} + +/* Synthesised data abort at specified page table level. */ +#define PRE_FAULT_ESR (level) \ + ((ESR_ELx_EC_DABT_LOW << ESR_ELx_EC_SHIFT) | \ + ESR_ELx_IL | ESR_ELx_FSC_FAULT_L(level)) + +/* Retrieve either a read-only or a read/write hva. */ +static hva_t gfn_to_hva_memslot_read (struct kvm_memory_slot *slot,gfn_t gfn) +{ +return gfn_to_hva_memslot_prot (slot, gfn,/*writable=*/ NULL ); +} + +static long __pre_fault_s2 (struct kvm_s2_mmu *mmu,struct kvm_vcpu *vcpu, +gpa_t gpa,struct kvm_memory_slot *memslot,s8 level) +{ +const bool is_gmem =kvm_slot_has_gmem (memslot); +const gfn_t gfn =gpa_to_gfn (gpa); +const hva_t hva = is_gmem ?0 :gfn_to_hva_memslot_read (memslot, gfn); +const struct kvm_s2_fault_desc s2fd = { + .vcpu = vcpu, + .fault_ipa = gpa, + .nested =NULL , + .memslot = memslot, + .hva = hva, + .esr =PRE_FAULT_ESR (level), + .mmu = mmu, + .pre_fault = true, + }; +struct kvm_s2_fault_result result = {}; +long ret; + +if (kvm_is_error_hva (hva)) +return -EFAULT ; + +if (is_gmem) + ret =gmem_abort (&s2fd, &result); +else + ret =user_mem_abort (&s2fd, &result); +if (IS_ERR_VALUE (ret))
arch/arm64/kvm/mmu.c void kvm_toggle_cache(struct kvm_vcpu *vcpu, bool was_enabled) · line 2933
Quoted 10 lines
arch/arm64/kvm/mmu.c +10
-0+return ret; +if (!result.mapped) +return -EAGAIN ; +return result.mapping_size; +} + +static long pre_fault_s2 (struct kvm_s2_mmu *mmu,struct kvm_vcpu *vcpu, +gpa_t gpa,struct kvm_memory_slot *memslot) +{ +s8 level =KVM_PGTABLE_LAST_LEVEL ;
level is the result of a successful walk. Why do we need to initialise this to anything here? We don't seem to do that anywhere else.
Quoted 64 lines
arch/arm64/kvm/mmu.c +63
-0+long ret; + +/* Try a walk first. */ + ret =kvm_walk_s2 (mmu->pgt, gpa, &level); +if (ret) +return ret; +/* OK, have to fault page in. */ +return __pre_fault_s2 (mmu, vcpu, gpa, memslot, level); +} + +static unsigned long +pre_fault_bytes_consumed (gpa_t gpa,unsigned long granule_size, +unsigned long bytes_remaining) +{ +/* Granules are always a power-of-2. */ +const unsigned long granule_bytes_remaining = + granule_size - (gpa % granule_size); + +return min (granule_bytes_remaining, bytes_remaining); +} + +/* If you lose the race this many times, time to give up. */ +#define MAX_PRE_FAULT_RETRIES 3 + +/** +* kvm_arch_vcpu_pre_fault_memory - pre-fault stage-2 page tables for the +* specified GPA. +* @vcpu: The VCPU pointer +* @range: {gpa, size, flags} tuple +* +* The mapping performed is always best-effort - faulting in is necessarily +* racey. The ranges faulted in are canonical, nested page tables are ignored. +* +* If the GPA is already mapped, the page table entry is marked young. +* +* @range->gpa specifies the GPA to pre-fault, @range->size specifies how many +* bytes remain to be pre-faulted and @range->flags is reserved and must be 0. +* +* Returns: the number of bytes the pre-fault consumed, or an error. +*/ +long kvm_arch_vcpu_pre_fault_memory (struct kvm_vcpu *vcpu, +struct kvm_pre_fault_memory *range) +{ +struct kvm *kvm = vcpu->kvm; +const u64 bytes_remaining = range->size; +struct kvm_s2_mmu *mmu = &kvm->arch.mmu;/* Canonical. */ +struct kvm_memory_slot *memslot; +const gpa_t gpa = range->gpa; +int num_retries =0 ; +long ret; + +/* +* pKVM is unsupported as their vCPUs are instantiated on first run and +* pre-faulting only running vCPUs would be inconsistent and confusing. +*/ +if (is_protected_kvm_enabled ()) +return -EOPNOTSUPP ; + + memslot =gfn_to_memslot (kvm,gpa_to_gfn (gpa)); +if (!memslot) +return -ENOENT ; +/* SRCU must be released for progress and only userland can do that. */ +if (memslot->flags &KVM_MEMSLOT_INVALID )
arch/arm64/kvm/mmu.c void kvm_toggle_cache(struct kvm_vcpu *vcpu, bool was_enabled) · line 3006
Quoted 10 lines
arch/arm64/kvm/mmu.c +10
-0+return -EAGAIN ; + +do { + ret =pre_fault_s2 (mmu, vcpu, gpa, memslot); + }while (ret == -EAGAIN && num_retries++ <MAX_PRE_FAULT_RETRIES ); + +if (IS_ERR_VALUE (ret)) +return ret; +return pre_fault_bytes_consumed (gpa, ret, bytes_remaining); +}
Thanks,
M.
Signature · 1 line
Without deviation from the norm, progress is not possible.
Re: [PATCH 5/8] KVM: arm64: Implement KVM_PRE_FAULT_MEMORY
Quoted 18 lines · Marc Zyngier
Quoted 16 lines · Lorenzo Stoakes (ARM)
Implement KVM stage 2 page table pre-faulting for the arm64 architecture.
Do the trivial plumbing by selecting
CONFIG_KVM_GENERIC_PRE_FAULT_MEMORYinKconfig, returning 1 inkvm_vm_ioctl_check_extension()forKVM_CAP_PRE_FAULT_MEMORYbut false inkvm_pkvm_ext_allowed()to disallow its use for pKVM.Add
kvm_arch_vcpu_pre_fault_memory()to actually implement the feature by pre-faulting stage 2 page tables for a specific GPA.Previous commits added the required foundations -
gmem_abort()anduser_mem_abort()determine esr and mmu froms2fd->esrands2fd->mmurespectively, the granule size is returned by them inkvm_s2_fault_resultandkvm_pgtable_get_leaf()accepts a walk flags parameter.
Quoted 5 lines
Quoted 3 lines
Additionally,
kvm_pgtable_get_leaf()now accepts a walk flag, meaning its page table walk can be performed withKVM_PGTABLE_WALK_SHAREDset and thus can be performed under a readmmu_lock.We really don't need a recap of the previous episodes... ;-)
Haha fair enough ;) will drop.
Quoted 51 lines
Quoted 51 lines
With these changes in place, implement pre-faulting by first trying a page table walk under the MMU read lock then, if it fails, injecting a synthetic data abort at the page table level at which the page table walk failed.
This is necessarily racey as reclaim might happen at any time. Successfully pre-faulting can therefore only guarantee that each GPA was observed to be mapped at least once.
Protected KVM (pKVM) is not supported at all because pKVM creates VMs and vCPUs when first run, meaning any attempt to pre-fault prior to this cannot succeed.
Since the sensible use case for pre-faulting is doing so prior to vCPU run, and supporting only online pKVM vCPUs is confusing and inconsistent, simply don't support this at all.
There is a subtlety when retrieving the hva: it seems natural to use
gfn_to_hva_memslot(), but this errors out for read-only memslots.Since pre-faulting should fault in both read-only and read/write hvas, this isn't the correct API to use here.
gfn_to_hva_memslot_prot()allows retrieval of read-only hvas, but has unclear semantics, so introducegfn_to_hva_memslot_read()to wrap it.A retry mechanic is implemented when
user_mem_abort()orgmem_abort()fail to map memory due to a benign failure where a hardware abort would not result in an error.These occur when the abort handler was raced by either an invalidation MMU notifier or a racing abort path.
Since these faults are highly likely to succeed on immediate retry, retry up to
MAX_PRE_FAULT_RETRIEStimes, after which-EAGAINis ultimately returned to the caller.An invalid memslot (i.e. a memslot with the
KVM_MEMSLOT_INVALIDflag set) also results in the operation returning-EAGAINwithout a retry mechanic.This is because an invalid memslot means the pre-fault operation raced with memslot reclaim, and since the SRCU lock is held, progress cannot be made.
Therefore, when this happens,
-EAGAINindicates that userland should retry the ioctl, which will bounce the SRCU lock and permit forward progress.Finally, update the KVM API documentation to describe the changes, providing arm64-specific implementation details.
Quoted 7 lines
Quoted 2 lines
This work is based with gratitude on Jack Thomson's original series, its previous revisions and the feedback they received.
Honestly, this whole commit message reads like a cover letter. Consider writing something more succinct, which would be helped by splitting the patch: if you have so much to say for a single patch, that's probably that the patch is doing too many things.
Ack,
Quoted 50 lines
Quoted 41 lines
Signed-off-by Lorenzo Stoakes (ARM) <[email protected]>--- Documentation/virt/kvm/api.rst | 17 +++- arch/arm64/include/asm/kvm_pkvm.h | 2 +- arch/arm64/kvm/Kconfig | 1 + arch/arm64/kvm/arm.c | 1 + arch/arm64/kvm/mmu.c | 192 +++++++++++++++++++++++++++++++++++--- 5 files changed, 198 insertions(+), 15 deletions(-)Documentation/virt/kvm/api.rst +3
-1@@ -6485,7 +6485,7 @@ See KVM_SET_USER_MEMORY_REGION2 for additional details. --------------------------- :Capability: KVM_CAP_PRE_FAULT_MEMORY -:Architectures: none +:Architectures: x86, s390, arm64 :Type: vcpu ioctl :Parameters: struct kvm_pre_fault_memory (in/out) :Returns: 0 if at least one page is processed, < 0 on error@@ -6493,12 +6493,14 @@ See KVM_SET_USER_MEMORY_REGION2 for additional details. Errors: ========== =============================================================== + EAGAIN A race occurred before progress was made, but a retry may succeed. EINVAL The specified `gpa` and `size` were invalid (e.g. not page aligned, causes an overflow, or size is zero), or the VM is UCONTROL (s390). ENOENT The specified `gpa` is outside defined memslots. EINTR An unmasked signal is pending and no page was processed. EFAULT The parameter address was invalid. + EHWPOISON A poisoned host page was encountered. EOPNOTSUPP Mapping memory for a GPA is unsupported by the hypervisor, and/or for the current vCPU state/mode. EIO unexpected error conditions (also causes a WARN)Documentation/virt/kvm/api.rst Errors: · line 6524
Quoted 6 lines
Documentation/virt/kvm/api.rst +2
-1@@ -6518,7 +6520,16 @@ Errors: KVM_PRE_FAULT_MEMORY populates KVM's stage-2 page tables used to map memory for the current vCPU state. KVM maps memory as if the vCPU generated a stage-2 read page fault, e.g. faults in memory as needed, but doesn't break -CoW. On x86, KVM does not mark any newly created stage-2 PTE as Accessed. +CoW. On arm64, KVM marks both existing and newly created stage-2 PTEs as +Accessed, on x86 it does not and for s390 it is not applicable.Meh. See below.
Quoted 12 lines
Documentation/virt/kvm/api.rst Errors: · line 6532
Quoted 8 lines
Documentation/virt/kvm/api.rst +8
-0+ +On arm64, a GPA is interpreted as an IPA, and never interpreted as the IPA +of a nested guest. Pre-faulting only populates canonical stage 2 page +tables. + +The feature is not supported on arm64 if the protected KVM (pKVM) feature +is enabled, as that results in vCPUs being instantiated on first run, which +renders pre-faulting useless.I don't think we need to justify why this is not available with pKVM. I expect pKVM to eventually support this through a slightly different mechanism anyway.
OK, can just end the sentence at 'is enabled'.
Quoted 8 lines
Quoted 8 lines
Documentation/virt/kvm/api.rst +0
-0In the case of confidential VM types where there is an initial set up of private guest memory before the guest is 'finalized'/measured, this ioctl@@ -6533,7 +6544,7 @@ When the ioctl returns, the input values are updated to point to the remaining range. If `size` > 0 on return, the caller can just issue the ioctl again with the same `struct kvm_map_memory` argument.
Quoted 4 lines
Documentation/virt/kvm/api.rst When the ioctl returns, the input values are updated to point to the · line 6547
Quoted 2 lines
Documentation/virt/kvm/api.rst +1
-1-Shadow page tables cannot support this ioctl because they +On x86, Shadow page tables cannot support this ioctl because theyshadow
Oops yup!
Quoted 6 lines
Documentation/virt/kvm/api.rst When the ioctl returns, the input values are updated to point to the · line 6550
Quoted 4 lines
Documentation/virt/kvm/api.rst +0
-0are indexed by virtual address or nested guest physical address. Calling this ioctl when the guest is using shadow page tables (for example because it is running a nested guest with nested page tables)Documentation updates in a separate patch, please.
Ack!
Quoted 56 lines
Quoted 56 lines
arch/arm64/include/asm/kvm_pkvm.h +1
-1@@ -44,9 +44,9 @@ static inline bool kvm_pkvm_ext_allowed(struct kvm *kvm, long ext)case KVM_CAP_ARM_PTRAUTH_GENERIC :return true;case KVM_CAP_ARM_MTE : -return false;case KVM_CAP_ARM_EAGER_SPLIT_CHUNK_SIZE :case KVM_CAP_ARM_SUPPORTED_BLOCK_SIZES : +case KVM_CAP_PRE_FAULT_MEMORY :return false;default :return !kvm || !kvm_vm_is_protected (kvm);arch/arm64/kvm/Kconfig +1
-0@@ -37,6 +37,7 @@ menuconfig KVM select SCHED_INFO select GUEST_PERF_EVENTS if PERF_EVENTS select KVM_GUEST_MEMFD + select KVM_GENERIC_PRE_FAULT_MEMORY help Support hosting virtualized guest machines.arch/arm64/kvm/arm.c +1
-0@@ -393,6 +393,7 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)case KVM_CAP_COUNTER_OFFSET :case KVM_CAP_ARM_WRITABLE_IMP_ID_REGS :case KVM_CAP_ARM_SEA_TO_USER : +case KVM_CAP_PRE_FAULT_MEMORY : r =1 ;break ;case KVM_CAP_SET_GUEST_DEBUG2 :arch/arm64/kvm/mmu.c +1
-0@@ -5,6 +5,7 @@*/ #include <linux/acpi.h> +#include <linux/cleanup.h> #include <linux/mman.h> #include <linux/kvm_host.h> #include <linux/io.h> @@ -1554,9 +1555,9 @@ static void *get_mmu_memcache(struct kvm_vcpu *vcpu)return &vcpu->arch.pkvm_memcache; }
Quoted 7 lines
arch/arm64/kvm/mmu.c static void *get_mmu_memcache(struct kvm_vcpu *vcpu) · line 1560
Quoted 5 lines
arch/arm64/kvm/mmu.c +2
-2-static int topup_mmu_memcache (struct kvm_vcpu *vcpu,void *memcache) +static int topup_mmu_memcache (struct kvm_s2_mmu *mmu,void *memcache) { -int min_pages =kvm_mmu_cache_min_pages (vcpu->arch.hw_mmu); +const int min_pages =kvm_mmu_cache_min_pages (mmu);Changes to the memcache interface in a separate patch, please.
Ack!
Quoted 11 lines
arch/arm64/kvm/mmu.c struct kvm_s2_fault_desc { · line 1609
Quoted 9 lines
arch/arm64/kvm/mmu.c +1
-0if (!is_protected_kvm_enabled ())return kvm_mmu_topup_memory_cache (memcache, min_pages);@@ -1605,6 +1606,7 @@ struct kvm_s2_fault_desc {unsigned long hva;unsigned long esr;struct kvm_s2_mmu *mmu; +bool pre_fault;No, please, that's horrible. See below.
Yeah, I think this was a carry-through from the original series. Will drop!
Quoted 22 lines
Quoted 22 lines
arch/arm64/kvm/mmu.c +2
-2};struct kvm_s2_fault_result {@@ -1664,7 +1666,7 @@ static int gmem_abort(const struct kvm_s2_fault_desc *s2fd,if (!perm_fault) { memcache =get_mmu_memcache (s2fd->vcpu); - ret =topup_mmu_memcache (s2fd->vcpu, memcache); + ret =topup_mmu_memcache (s2fd->mmu, memcache);if (ret)return ret; }@@ -1761,7 +1763,7 @@ static int pkvm_mem_abort(const struct kvm_s2_fault_desc *s2fd)int ret; hyp_memcache =get_mmu_memcache (vcpu); - ret =topup_mmu_memcache (vcpu, hyp_memcache); + ret =topup_mmu_memcache (s2fd->mmu, hyp_memcache);if (ret)return -ENOMEM ;
Quoted 9 lines
arch/arm64/kvm/mmu.c static int kvm_s2_fault_pin_pfn(const struct kvm_s2_fault_desc *s2fd, · line 1959
Quoted 6 lines
arch/arm64/kvm/mmu.c +2
-0@@ -1953,6 +1955,8 @@ static int kvm_s2_fault_pin_pfn(const struct kvm_s2_fault_desc *s2fd, &s2vi->map_writable, &s2vi->page);if (unlikely (is_error_noslot_pfn (s2vi->pfn))) {if (s2vi->pfn ==KVM_PFN_ERR_HWPOISON ) { +if (s2fd->pre_fault) +return -EHWPOISON ;I'd rather *always* return
-EHWPOISONhere, and move the signal injection into the callers. That would much more consistent.
OK that is nicer :) Will do that instead.
Quoted 81 lines
Quoted 81 lines
arch/arm64/kvm/mmu.c +46
-7kvm_send_hwpoison_signal (s2fd->hva,__ffs (s2vi->vma_pagesize));return 0 ; }@@ -2163,7 +2167,7 @@ static int user_mem_abort(const struct kvm_s2_fault_desc *s2fd, memcache =get_mmu_memcache (s2fd->vcpu);if (!perm_fault ||memslot_is_logging (s2fd->memslot) ||is_protected_kvm_enabled ()) { - ret =topup_mmu_memcache (s2fd->vcpu, memcache); + ret =topup_mmu_memcache (s2fd->mmu, memcache);if (ret)return ret; }@@ -2185,18 +2189,22 @@ static int user_mem_abort(const struct kvm_s2_fault_desc *s2fd,return kvm_s2_fault_map (s2fd, &s2vi, prot, memcache, result); } -/* Resolve the access fault by making the page young again. */ -static void handle_access_fault (struct kvm_vcpu *vcpu,phys_addr_t fault_ipa) +static void __handle_access_fault (struct kvm_pgtable *pgt,phys_addr_t fault_ipa) {enum kvm_pgtable_walk_flags flags =KVM_PGTABLE_WALK_SHARED ; -struct kvm_s2_mmu *mmu;trace_kvm_access_fault (fault_ipa); +KVM_PGT_FN (kvm_pgtable_stage2_mkyoung)(pgt, fault_ipa, flags); +} + +/* Resolve the access fault by making the page young again. */ +static void handle_access_fault (struct kvm_vcpu *vcpu,phys_addr_t fault_ipa) +{ +struct kvm_s2_mmu *mmu; -read_lock (&vcpu->kvm->mmu_lock); +guard (read_lock)(&vcpu->kvm->mmu_lock); mmu = vcpu->arch.hw_mmu; -KVM_PGT_FN (kvm_pgtable_stage2_mkyoung)(mmu->pgt, fault_ipa, flags); -read_unlock (&vcpu->kvm->mmu_lock); +__handle_access_fault (mmu->pgt, fault_ipa); }/* @@ -2834,3 +2842,165 @@ void kvm_toggle_cache(struct kvm_vcpu *vcpu, bool was_enabled)trace_kvm_toggle_cache (*vcpu_pc (vcpu), was_enabled, now_enabled); } + +static bool kvm_pte_young_s2 (kvm_pte_t pte) +{ +return pte &KVM_PTE_LEAF_ATTR_LO_S2_AF ; +} + +static void kvm_pte_mkyoung_s2 (struct kvm_pgtable *pgt,gpa_t gpa) +{ +struct kvm *kvm =kvm_s2_mmu_to_kvm (pgt->mmu); + +lockdep_assert_held (&kvm->mmu_lock); +/* Despite its name, doesn't fault here. */ +__handle_access_fault (pgt, gpa); +} + +/* +* Try to walk to the specified GPA in canonical mmu - if unmapped returns 0, if +* mapped returns the granule size, otherwise returns an error. +*/ +static long kvm_walk_s2 (struct kvm_pgtable *pgt, +gpa_t gpa,s8 *level) +{ +struct kvm *kvm =kvm_s2_mmu_to_kvm (pgt->mmu); +kvm_pte_t pte; +long ret; + +guard (read_lock)(&kvm->mmu_lock); + + ret =kvm_pgtable_get_leaf (pgt, gpa, &pte, level, +KVM_PGTABLE_WALK_SHARED ); +if (ret) +return ret; +/* Unpopulated, must fault. */ +if (!kvm_pte_valid (pte)) +return 0 ;
Quoted 8 lines
arch/arm64/kvm/mmu.c void kvm_toggle_cache(struct kvm_vcpu *vcpu, bool was_enabled) · line 2882
Quoted 3 lines
arch/arm64/kvm/mmu.c +3
-0+/* Walked the entry so mark young. */ +if (!kvm_pte_young_s2 (pte)) +kvm_pte_mkyoung_s2 (pgt, gpa);I'm not sold on this either. The contract with userspace is to map the page at S2, not to pretend it has been accessed by the vcpu. I don't see why we should do that, as this is a departure from an established behaviour.
Ack, yeah, that's fair enough.
Thinking was it's the 'equivalent of touching the pages', but for existing pages that's not the case at all.
New pages will be born young, but that makes sense since you did actually do something (fault them in).
Quoted 47 lines
Quoted 47 lines
arch/arm64/kvm/mmu.c +46
-0+return kvm_granule_size (*level); +} + +/* Synthesised data abort at specified page table level. */ +#define PRE_FAULT_ESR (level) \ + ((ESR_ELx_EC_DABT_LOW << ESR_ELx_EC_SHIFT) | \ + ESR_ELx_IL | ESR_ELx_FSC_FAULT_L(level)) + +/* Retrieve either a read-only or a read/write hva. */ +static hva_t gfn_to_hva_memslot_read (struct kvm_memory_slot *slot,gfn_t gfn) +{ +return gfn_to_hva_memslot_prot (slot, gfn,/*writable=*/ NULL ); +} + +static long __pre_fault_s2 (struct kvm_s2_mmu *mmu,struct kvm_vcpu *vcpu, +gpa_t gpa,struct kvm_memory_slot *memslot,s8 level) +{ +const bool is_gmem =kvm_slot_has_gmem (memslot); +const gfn_t gfn =gpa_to_gfn (gpa); +const hva_t hva = is_gmem ?0 :gfn_to_hva_memslot_read (memslot, gfn); +const struct kvm_s2_fault_desc s2fd = { + .vcpu = vcpu, + .fault_ipa = gpa, + .nested =NULL , + .memslot = memslot, + .hva = hva, + .esr =PRE_FAULT_ESR (level), + .mmu = mmu, + .pre_fault = true, + }; +struct kvm_s2_fault_result result = {}; +long ret; + +if (kvm_is_error_hva (hva)) +return -EFAULT ; + +if (is_gmem) + ret =gmem_abort (&s2fd, &result); +else + ret =user_mem_abort (&s2fd, &result); +if (IS_ERR_VALUE (ret)) +return ret; +if (!result.mapped) +return -EAGAIN ; +return result.mapping_size; +}
Quoted 8 lines
arch/arm64/kvm/mmu.c void kvm_toggle_cache(struct kvm_vcpu *vcpu, bool was_enabled) · line 2933
Quoted 5 lines
arch/arm64/kvm/mmu.c +5
-0+ +static long pre_fault_s2 (struct kvm_s2_mmu *mmu,struct kvm_vcpu *vcpu, +gpa_t gpa,struct kvm_memory_slot *memslot) +{ +s8 level =KVM_PGTABLE_LAST_LEVEL ;level is the result of a successful walk. Why do we need to initialise this to anything here? We don't seem to do that anywhere else.
I feared that level wouldn't get assigned, but looking through the code, if kvm_walk_s2() returns 0 then it's always assigned so will update -> s8 level;
Quoted 78 lines
Quoted 65 lines
arch/arm64/kvm/mmu.c +63
-0+long ret; + +/* Try a walk first. */ + ret =kvm_walk_s2 (mmu->pgt, gpa, &level); +if (ret) +return ret; +/* OK, have to fault page in. */ +return __pre_fault_s2 (mmu, vcpu, gpa, memslot, level); +} + +static unsigned long +pre_fault_bytes_consumed (gpa_t gpa,unsigned long granule_size, +unsigned long bytes_remaining) +{ +/* Granules are always a power-of-2. */ +const unsigned long granule_bytes_remaining = + granule_size - (gpa % granule_size); + +return min (granule_bytes_remaining, bytes_remaining); +} + +/* If you lose the race this many times, time to give up. */ +#define MAX_PRE_FAULT_RETRIES 3 + +/** +* kvm_arch_vcpu_pre_fault_memory - pre-fault stage-2 page tables for the +* specified GPA. +* @vcpu: The VCPU pointer +* @range: {gpa, size, flags} tuple +* +* The mapping performed is always best-effort - faulting in is necessarily +* racey. The ranges faulted in are canonical, nested page tables are ignored. +* +* If the GPA is already mapped, the page table entry is marked young. +* +* @range->gpa specifies the GPA to pre-fault, @range->size specifies how many +* bytes remain to be pre-faulted and @range->flags is reserved and must be 0. +* +* Returns: the number of bytes the pre-fault consumed, or an error. +*/ +long kvm_arch_vcpu_pre_fault_memory (struct kvm_vcpu *vcpu, +struct kvm_pre_fault_memory *range) +{ +struct kvm *kvm = vcpu->kvm; +const u64 bytes_remaining = range->size; +struct kvm_s2_mmu *mmu = &kvm->arch.mmu;/* Canonical. */ +struct kvm_memory_slot *memslot; +const gpa_t gpa = range->gpa; +int num_retries =0 ; +long ret; + +/* +* pKVM is unsupported as their vCPUs are instantiated on first run and +* pre-faulting only running vCPUs would be inconsistent and confusing. +*/ +if (is_protected_kvm_enabled ()) +return -EOPNOTSUPP ; + + memslot =gfn_to_memslot (kvm,gpa_to_gfn (gpa)); +if (!memslot) +return -ENOENT ; +/* SRCU must be released for progress and only userland can do that. */ +if (memslot->flags &KVM_MEMSLOT_INVALID )arch/arm64/kvm/mmu.c void kvm_toggle_cache(struct kvm_vcpu *vcpu, bool was_enabled) · line 3006
Quoted 10 lines
arch/arm64/kvm/mmu.c +10
-0+return -EAGAIN ; + +do { + ret =pre_fault_s2 (mmu, vcpu, gpa, memslot); + }while (ret == -EAGAIN && num_retries++ <MAX_PRE_FAULT_RETRIES ); + +if (IS_ERR_VALUE (ret)) +return ret; +return pre_fault_bytes_consumed (gpa, ret, bytes_remaining); +}Thanks,
Quoted 4 lines
M.--
Without deviation from the norm, progress is not possible.
--
Cheers, Lorenzo
Hi Lorenzo,
Quoted 3 lines · Lorenzo Stoakes (ARM)
This series implements the KVM stage 2 page table pre-faulting feature for arm64.
I know there's likely going to be a V2, but FWIW, I tested this series as follows (finally managed to get protected mode running on my Mac):
QEMU (arm64, -cpu max,pauth=off):
- Protected-mode host, boots non-protected and protected guests
- pre_fault_memory_test on a non-protected host, VHE and nVHE, all 13 guest modes
- nv_pre_fault_memory_test with kvm-arm.mode=nested
FVP (AEM, FEAT_NV2):
- nv_pre_fault_memory_test with kvm-arm.mode=nested
Apple M4 hardware, running as a KVM host at EL2 under Apple's nested virtualization (nVHE only):
- pre_fault_memory_test, anonymous and anonymous_thp backing, over the five guest modes available there (the IPA is capped at 40 bits and the 64K granule isn't implemented)
- with kvm-arm.mode=protected, pre_fault_memory_test skips on KVM_CAP_PRE_FAULT_MEMORY, so the pKVM exclusion holds on a live pKVM host
(happy to retest again when you respin)
Cheers,
/fuad
Quoted 118 lines
== Foundations ==
The series begins by establishing required foundations:
1. Updating
kvm_s2_fault_descto independently store the exception syndrome register (ESR) value, and updating all code paths to use this value exclusively.This is needed so we can later generate a synthetic fault to perform the pre-faulting - we need to be sure the code doesn't grab an incorrect ESR from elsewhere.
2. Updating
kvm_s2_fault_descto independently store thekvm_s2_mmuand updating all code paths to use this value exclusively.Similarly this is needed so we can generate a synthetic fault against the canonical stage 2 MMU (it would make no sense for it to touch nested shadow page tables) - we need to be sure that the code doesn't grab an incorrect MMU from elsewhere.
3. Updating the abort paths which consume
kvm_s2_fault_descto also return akvm_s2_fault_resultdata structure.To perform pre-faulting the code must know the granule size of what was just walked. So the abort paths have to tell us what that was.
4. Pass walk flags to
kvm_pgtable_get_leaf()to permit walking page tables under the MMU read lock.This is Jack's patch, verbatim, which allows the use of the
KVM_PGTABLE_WALK_SHAREDflag to walk page tables under the MMU read lock.Pre-faulting requires it to be able to work in parallel as specified by the API. The read lock precludes page tables being torn down behind our back.
== Implementation ==
Pre-faulting is implemented in
kvm_arch_vcpu_pre_fault_memory()whose job is to pre-fault the stage 2 page tables which map a specific GPA (which, for arm64, is the guest's IPA).This function is called by
kvm_vcpu_pre_fault_memory()for each GPA in the range, which itself is ultimately invoked by userland via theKVM_PRE_FAULT_MEMORYioctl.The implementation is simple - try to walk to the stage 2 page table mapping the GPA - if unmapped, fault it in through a synthetic page fault.
pKVM is not supported regardless of whether the VM is protected or not.
This is because pKVM instantiates vCPUs upon run, but pre-faulting is typically performed before a vCPU is run. It would be confusing and inconsistent to error out on non-running vCPUs but to pre-fault running ones.
Jack's original test suite is also included in the series.
== Credits ==
This series is based, with gratitude, on Jack Thomson's series and their respins (links provided below) as well as the feedback he received.
The series includes Jack's v5 "KVM: arm64: Pass walk flags to
kvm_pgtable_get_leaf()" patch verbatim, and all three of his test patches, two of which required minor fixups.== Reviewer Notes ==
I synced with maintainers on this who asked me to take a look, as there hadn't been progress on the series for some time.
Am happy to rebase again after -rc1.
Signed-off-by Lorenzo Stoakes (ARM) <[email protected]>---
Jack Thomson (4):
KVM: arm64: Pass walk flags tokvm_pgtable_get_leaf()
KVM: selftests: Enablepre_fault_memory_testfor arm64
KVM: selftests: Add option for different backing in pre-fault tests
KVM: selftests: Add nested pre-fault test for arm64Lorenzo Stoakes (ARM) (4):
KVM: arm64: Propagate and use esr in s2fd when handling guest aborts
KVM: arm64: Propagate and use mmu in s2fd when handling guest aborts
KVM: arm64: Propagate and usekvm_s2_fault_resulton S2 fault
KVM: arm64: ImplementKVM_PRE_FAULT_MEMORYDocumentation/virt/kvm/api.rst | 17 +- arch/arm64/include/asm/esr.h | 129 +++++--- arch/arm64/include/asm/kvm_emulate.h | 52 +--- arch/arm64/include/asm/kvm_pgtable.h | 5 +- arch/arm64/include/asm/kvm_pkvm.h | 2 +- arch/arm64/kvm/Kconfig | 1 + arch/arm64/kvm/arm.c | 1 + arch/arm64/kvm/hyp/nvhe/mem_protect.c | 10 +- arch/arm64/kvm/hyp/pgtable.c | 5 +- arch/arm64/kvm/mmu.c | 324 +++++++++++++++++---- arch/arm64/kvm/nested.c | 2 +- tools/testing/selftests/kvm/Makefile.kvm | 2 + .../selftests/kvm/arm64/nv_pre_fault_memory_test.c | 206 +++++++++++++ .../testing/selftests/kvm/pre_fault_memory_test.c | 152 ++++++++-- 14 files changed, 736 insertions(+), 172 deletions(-)---
base-commit:aa8e5dc6a7a2a1141ab40706a51010adcd0e57d2
change-id: 20260815-kvm-arm-prefault-9bb411b6897dBest regards,
--
Lorenzo Stoakes (ARM) <[email protected]>
Re: [PATCH 6/8] KVM: selftests: Enable pre_fault_memory_test for arm64
In reply to Lorenzo Stoakes (ARM)Quoted 9 lines · Lorenzo Stoakes (ARM)
From: Jack Thomson <[email protected]>
Enable the
pre_fault_memory_testto run on arm64 by making it work with different guest page sizes and testing multiple guest configurations.Update the
test_assertto compare against theUCALL_EXIT_REASON, for portability, as arm64 exits withKVM_EXIT_MMIOwhile x86 usesKVM_EXIT_IO.
The message says this updates the test_assert for portability, but the parent has no exit-reason assert and no struct kvm_run; this patch adds both, so there was no hardcoded KVM_EXIT_IO to make portable.
...
Quoted 2 lines
int
main(int argc, char *argv[])
{
...
Quoted 4 lines
+
TEST_REQUIRE(kvm_check_cap(KVM_CAP_PRE_FAULT_MEMORY)); test_pre_fault_memory(0, false);
Would it be worth adding an arm64 case for gmem_abort()'s pre-fault path?
Cheers,
/fuad
Quoted 4 lines
--
2.55.0
Re: [PATCH 8/8] KVM: selftests: Add nested pre-fault test for arm64
In reply to Lorenzo Stoakes (ARM)Hi,
On Tue, 25 Aug 2026 at 17:02, Lorenzo Stoakes (ARM) <[email protected]> wrote:
..
tools/testing/selftests/kvm/arm64/nv_pre_fault_memory_test.c line 143
Quoted 10 lines
tools/testing/selftests/kvm/arm64/nv_pre_fault_memory_test.c new file +10
-0+int main (void ) +{ +struct nested_s2_state s2; +struct kvm_vcpu_init init; +struct kvm_vcpu *vcpu; +struct kvm_vm *vm; +struct ucall uc; +u64 npages; + +TEST_REQUIRE (kvm_check_cap (KVM_CAP_ARM_EL2 ));
Would test_supports_el2() work better here? It honours the NV=0 opt-out, and since HAS_EL2 is set unconditionally below, NV=0 doesn't disable this test as things stand.
Cheers,
/fuad
Quoted 67 lines
tools/testing/selftests/kvm/arm64/nv_pre_fault_memory_test.c new file +63
-1+TEST_REQUIRE (kvm_check_cap (KVM_CAP_PRE_FAULT_MEMORY )); + + vm =vm_create (1 ); + +kvm_get_default_vcpu_target (vm, &init); + init.features[0 ] |=BIT (KVM_ARM_VCPU_HAS_EL2 ); + vcpu =aarch64_vcpu_add (vm,0 , &init, guest_code); +kvm_arch_vm_finalize_vcpus (vm); + + npages =TEST_MEM_SIZE / vm->page_size; +vm_userspace_mem_region_add (vm,VM_MEM_SRC_ANONYMOUS ,TEST_MEM_GPA , +TEST_MEM_SLOT , npages,0 ); +virt_map (vm,TEST_MEM_GPA ,TEST_MEM_GPA , npages); + +vm_userspace_mem_region_add (vm,VM_MEM_SRC_ANONYMOUS , +NESTED_S2_ROOT_GPA ,NESTED_S2_ROOT_SLOT , +vm_adjust_num_guest_pages (vm->mode,1 ),0 ); + +/* Run the guest until it has ERET'd from vEL2 to vEL1. */ +vcpu_run (vcpu); +switch (get_ucall (vcpu, &uc)) { +case UCALL_SYNC : +TEST_ASSERT_EQ (uc.args[1 ],1 ); +break ; +case UCALL_ABORT : +REPORT_GUEST_ASSERT (uc); +break ; +default : +TEST_FAIL ("Unhandled ucall: %ld" , uc.cmd); + } + +/* +* The vCPU's last-run context is vEL1, backed by a shadow stage-2 +* MMU. Enable nested stage-2 with an empty root so that the ioctl +* fails if it tries to interpret the userspace GPA as an L2 IPA. +* +* Prefault in two halves so that the second ioctl exercises a +* repeated shadow-MMU attach and canonical stage-2 swap. +* +* (Note that an implementation that wrongly populates shadow +* stage-2 page tables would not be caught as userland can't +* inspect these.) +*/ + s2 =enable_empty_nested_s2 (vcpu); +pre_fault (vcpu,TEST_MEM_GPA ,TEST_MEM_SIZE /2 ); +pre_fault (vcpu,TEST_MEM_GPA +TEST_MEM_SIZE /2 ,TEST_MEM_SIZE /2 ); +restore_nested_s2 (vcpu, &s2); + +/* Resume at vEL1 and touch the prefaulted range. */ +vcpu_run (vcpu); +switch (get_ucall (vcpu, &uc)) { +case UCALL_DONE : +break ; +case UCALL_ABORT : +REPORT_GUEST_ASSERT (uc); +break ; +default : +TEST_FAIL ("Unhandled ucall: %ld" , uc.cmd); + } + +kvm_vm_free (vm); +return 0 ; +} --2.55.0
Re: [PATCH 8/8] KVM: selftests: Add nested pre-fault test for arm64
In reply to Lorenzo Stoakes (ARM)Hi Lorenzo,
Quoted 14 lines · Lorenzo Stoakes (ARM)
From: Jack Thomson <[email protected]>
Add an arm64 nested-virt selftest for
KVM_PRE_FAULT_MEMORY. The guest enters vEL1 and exits to userspace with a nested/shadow stage-2 MMU as the vCPU's last-run context.Before prefaulting, userspace enables HCR_EL2.VM and points
VTTBR_EL2at an empty nested stage-2 root. A prefault implementation that incorrectly treats the userspace GPA as an L2 IPA will fail the ioctl; the correct path targets the canonical stage-2 and succeeds.Restore the original nested state before resuming the guest, then touch the prefaulted range to check that vEL1 still runs correctly.
Quoted 7 lines
Signed-off-by Jack Thomson <[email protected]>[ljs: partial progress, >4 KiB pgsize, commit msg, comment fixups]Signed-off-by Lorenzo Stoakes (ARM) <[email protected]>--- tools/testing/selftests/kvm/Makefile.kvm | 1 + .../selftests/kvm/arm64/nv_pre_fault_memory_test.c | 206 +++++++++++++++++++++ 2 files changed, 207 insertions(+)
I wonder whether this selftest can use functions Wei-Lin proposed some time ago [1]: [email protected] or would you prefer this test as propose, since it is clear as to what needs to be done to enter L2.
Thanks,
Itaru.
Quoted 229 lines
tools/testing/selftests/kvm/Makefile.kvm +1
-0@@ -173,6 +173,7 @@ TEST_GEN_PROGS_arm64 += arm64/debug-exceptions TEST_GEN_PROGS_arm64 += arm64/hello_el2 TEST_GEN_PROGS_arm64 += arm64/host_sve TEST_GEN_PROGS_arm64 += arm64/hypercalls +TEST_GEN_PROGS_arm64 += arm64/nv_pre_fault_memory_test TEST_GEN_PROGS_arm64 += arm64/external_aborts TEST_GEN_PROGS_arm64 += arm64/mmio_sign_ext TEST_GEN_PROGS_arm64 += arm64/page_fault_testtools/testing/selftests/kvm/arm64/nv_pre_fault_memory_test.c new file +206
-0@@ -0,0 +1,206 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* +* nv_pre_fault_memory_test - Test KVM_PRE_FAULT_MEMORY on a vCPU whose +* last-run context is nested. +* +* The guest starts at vEL2, mirrors its EL2 translation regime into the +* real EL1 registers, drops HCR_EL2.TGE and ERETs to vEL1, then exits to +* userspace from vEL1 so that the vCPU's last-run context selects a +* shadow stage-2 MMU. Userspace then enables an empty nested stage-2 +* before prefaulting. Prefaulting must target the canonical stage-2, +* regardless of the vCPU's nested state. +*/ +#include "kvm_util.h" +#include "processor.h" +#include "test_util.h" +#include "ucall.h" + +#include <asm/sysreg.h> +#include <linux/sizes.h> + +#define TEST_MEM_SLOT 10 +#define NESTED_S2_ROOT_SLOT 11 +#define TEST_MEM_SIZE SZ_2M +#define TEST_MEM_GPA SZ_1G +#define NESTED_S2_ROOT_GPA (TEST_MEM_GPA + TEST_MEM_SIZE) + +struct nested_s2_state { +u64 hcr_el2; +u64 vttbr_el2; +}; + +static void guest_el1_code (void ) +{ +u64 offset; + +GUEST_ASSERT_EQ (get_current_el (),1 ); + +/* Exit to userspace with the vEL1 (nested) context live. */ +GUEST_SYNC (1 ); + +/* +* Touch the prefaulted range. vstage-2 is disabled, so the shadow +* stage-2 is a 1:1 view of the canonical IPA space. +*/ +for (offset =0 ; offset <TEST_MEM_SIZE ; offset +=SZ_4K ) +READ_ONCE (*(u64 *)(TEST_MEM_GPA + offset)); + +GUEST_DONE (); +} + +static void guest_code (void ) +{ +u64 sp; + +GUEST_ASSERT_EQ (get_current_el (),2 ); + +/* +* Mirror the EL2 translation regime into the real EL1 registers so +* that vEL1 runs on the test's stage-1 page tables. With E2H=1, the +* _EL1 accessors read the EL2 registers, and the _EL12 accessors +* write the real EL1 registers. +*/ +write_sysreg_s (read_sysreg (sctlr_el1),SYS_SCTLR_EL12 ); +write_sysreg_s (read_sysreg (tcr_el1),SYS_TCR_EL12 ); +write_sysreg_s (read_sysreg (ttbr0_el1),SYS_TTBR0_EL12 ); +write_sysreg_s (read_sysreg (mair_el1),SYS_MAIR_EL12 ); +write_sysreg_s (read_sysreg (cpacr_el1),SYS_CPACR_EL12 ); + +/* Run vEL1 on the same stack. */ + asmvolatile ("mov %0, sp" :"=r" (sp)); +write_sysreg (sp, sp_el1); + +/* +* Drop TGE so that vEL1 is a nested context rather than host EL0. +* KVM backs it with a shadow stage-2 MMU even though vstage-2 is +* disabled (HCR_EL2.VM=0). +*/ +write_sysreg (read_sysreg (hcr_el2) & ~HCR_EL2_TGE , hcr_el2); +isb (); + +write_sysreg (PSR_MODE_EL1h |PSR_F_BIT |PSR_I_BIT |PSR_A_BIT | +PSR_D_BIT , spsr_el2); +write_sysreg ((u64 )guest_el1_code, elr_el2); + asmvolatile ("eret" ); + +GUEST_ASSERT (false); +} + +static void pre_fault (struct kvm_vcpu *vcpu,u64 gpa,u64 size) +{ +struct kvm_pre_fault_memory range = { + .gpa = gpa, + .size = size, + }; +int ret; + +do { + ret =__vcpu_ioctl (vcpu,KVM_PRE_FAULT_MEMORY , &range); + }while ((!ret && range.size) || + (ret <0 && (errno ==EINTR || errno ==EAGAIN ))); + +TEST_ASSERT (!ret,"KVM_PRE_FAULT_MEMORY failed, ret: %d errno: %d" , + ret, errno); +TEST_ASSERT_EQ (range.size,0 ); +} + +static struct nested_s2_state enable_empty_nested_s2 (struct kvm_vcpu *vcpu) +{ +struct nested_s2_state state = { + .hcr_el2 =vcpu_get_reg (vcpu,KVM_ARM64_SYS_REG (SYS_HCR_EL2 )), + .vttbr_el2 =vcpu_get_reg (vcpu, +KVM_ARM64_SYS_REG (SYS_VTTBR_EL2 )), + }; + +TEST_ASSERT (!(state.hcr_el2 &HCR_EL2_TGE ), +"vCPU should be in nested/vEL1 context" ); + +vcpu_set_reg (vcpu,KVM_ARM64_SYS_REG (SYS_VTTBR_EL2 ), +NESTED_S2_ROOT_GPA ); +vcpu_set_reg (vcpu,KVM_ARM64_SYS_REG (SYS_HCR_EL2 ), + state.hcr_el2 |HCR_EL2_VM ); + +return state; +} + +static void restore_nested_s2 (struct kvm_vcpu *vcpu, +struct nested_s2_state *state) +{ +vcpu_set_reg (vcpu,KVM_ARM64_SYS_REG (SYS_HCR_EL2 ), state->hcr_el2); +vcpu_set_reg (vcpu,KVM_ARM64_SYS_REG (SYS_VTTBR_EL2 ), + state->vttbr_el2); +} + +int main (void ) +{ +struct nested_s2_state s2; +struct kvm_vcpu_init init; +struct kvm_vcpu *vcpu; +struct kvm_vm *vm; +struct ucall uc; +u64 npages; + +TEST_REQUIRE (kvm_check_cap (KVM_CAP_ARM_EL2 )); +TEST_REQUIRE (kvm_check_cap (KVM_CAP_PRE_FAULT_MEMORY )); + + vm =vm_create (1 ); + +kvm_get_default_vcpu_target (vm, &init); + init.features[0 ] |=BIT (KVM_ARM_VCPU_HAS_EL2 ); + vcpu =aarch64_vcpu_add (vm,0 , &init, guest_code); +kvm_arch_vm_finalize_vcpus (vm); + + npages =TEST_MEM_SIZE / vm->page_size; +vm_userspace_mem_region_add (vm,VM_MEM_SRC_ANONYMOUS ,TEST_MEM_GPA , +TEST_MEM_SLOT , npages,0 ); +virt_map (vm,TEST_MEM_GPA ,TEST_MEM_GPA , npages); + +vm_userspace_mem_region_add (vm,VM_MEM_SRC_ANONYMOUS , +NESTED_S2_ROOT_GPA ,NESTED_S2_ROOT_SLOT , +vm_adjust_num_guest_pages (vm->mode,1 ),0 ); + +/* Run the guest until it has ERET'd from vEL2 to vEL1. */ +vcpu_run (vcpu); +switch (get_ucall (vcpu, &uc)) { +case UCALL_SYNC : +TEST_ASSERT_EQ (uc.args[1 ],1 ); +break ; +case UCALL_ABORT : +REPORT_GUEST_ASSERT (uc); +break ; +default : +TEST_FAIL ("Unhandled ucall: %ld" , uc.cmd); + } + +/* +* The vCPU's last-run context is vEL1, backed by a shadow stage-2 +* MMU. Enable nested stage-2 with an empty root so that the ioctl +* fails if it tries to interpret the userspace GPA as an L2 IPA. +* +* Prefault in two halves so that the second ioctl exercises a +* repeated shadow-MMU attach and canonical stage-2 swap. +* +* (Note that an implementation that wrongly populates shadow +* stage-2 page tables would not be caught as userland can't +* inspect these.) +*/ + s2 =enable_empty_nested_s2 (vcpu); +pre_fault (vcpu,TEST_MEM_GPA ,TEST_MEM_SIZE /2 ); +pre_fault (vcpu,TEST_MEM_GPA +TEST_MEM_SIZE /2 ,TEST_MEM_SIZE /2 ); +restore_nested_s2 (vcpu, &s2); + +/* Resume at vEL1 and touch the prefaulted range. */ +vcpu_run (vcpu); +switch (get_ucall (vcpu, &uc)) { +case UCALL_DONE : +break ; +case UCALL_ABORT : +REPORT_GUEST_ASSERT (uc); +break ; +default : +TEST_FAIL ("Unhandled ucall: %ld" , uc.cmd); + } + +kvm_vm_free (vm); +return 0 ; +}Signature · 2 lines
-- 2.55.0
Quoted 27 lines · Fuad Tabba
Hi Lorenzo,
Quoted 3 lines · Lorenzo Stoakes (ARM)
This series implements the KVM stage 2 page table pre-faulting feature for arm64.
I know there's likely going to be a V2, but FWIW, I tested this series as follows (finally managed to get protected mode running on my Mac):
QEMU (arm64, -cpu max,pauth=off):
- Protected-mode host, boots non-protected and protected guests
-pre_fault_memory_teston a non-protected host, VHE and nVHE, all 13 guest modes
-nv_pre_fault_memory_testwith kvm-arm.mode=nestedFVP (AEM,
FEAT_NV2):
-nv_pre_fault_memory_testwith kvm-arm.mode=nestedApple M4 hardware, running as a KVM host at EL2 under Apple's nested virtualization (nVHE only):
-pre_fault_memory_test, anonymous andanonymous_thpbacking, over the five guest modes available there (the IPA is capped at 40 bits and the 64K granule isn't implemented)
- with kvm-arm.mode=protected,pre_fault_memory_testskips onKVM_CAP_PRE_FAULT_MEMORY, so the pKVM exclusion holds on a live pKVM host
Quoted 2 lines
Tested-by Fuad Tabba < [email protected]>(happy to retest again when you respin)
Perfect, much appreciated thanks! :)
I won't carry the tags forward to v2 just in case anything breaks there, so appreciate you taking another run at the v2 also thanks! :)
Quoted 3 lines
Cheers,
/fuad
--
Cheers, Lorenzo
Re: [PATCH 8/8] KVM: selftests: Add nested pre-fault test for arm64
In reply to Fuad TabbaQuoted 4 lines · Fuad Tabba
Hi,
On Tue, 25 Aug 2026 at 17:02, Lorenzo Stoakes (ARM) <[email protected]> wrote:
..
Quoted 14 lines
tools/testing/selftests/kvm/arm64/nv_pre_fault_memory_test.c line 143
Quoted 10 lines
tools/testing/selftests/kvm/arm64/nv_pre_fault_memory_test.c new file +10
-0+int main (void ) +{ +struct nested_s2_state s2; +struct kvm_vcpu_init init; +struct kvm_vcpu *vcpu; +struct kvm_vm *vm; +struct ucall uc; +u64 npages; + +TEST_REQUIRE (kvm_check_cap (KVM_CAP_ARM_EL2 ));Would
test_supports_el2()work better here? It honours the NV=0 opt-out, and sinceHAS_EL2is set unconditionally below, NV=0 doesn't disable this test as things stand.
That sounds sensible :) Will fix on respin.
Quoted 68 lines
Cheers,
/fuadQuoted 64 lines
tools/testing/selftests/kvm/arm64/nv_pre_fault_memory_test.c new file +63
-0+TEST_REQUIRE (kvm_check_cap (KVM_CAP_PRE_FAULT_MEMORY )); + + vm =vm_create (1 ); + +kvm_get_default_vcpu_target (vm, &init); + init.features[0 ] |=BIT (KVM_ARM_VCPU_HAS_EL2 ); + vcpu =aarch64_vcpu_add (vm,0 , &init, guest_code); +kvm_arch_vm_finalize_vcpus (vm); + + npages =TEST_MEM_SIZE / vm->page_size; +vm_userspace_mem_region_add (vm,VM_MEM_SRC_ANONYMOUS ,TEST_MEM_GPA , +TEST_MEM_SLOT , npages,0 ); +virt_map (vm,TEST_MEM_GPA ,TEST_MEM_GPA , npages); + +vm_userspace_mem_region_add (vm,VM_MEM_SRC_ANONYMOUS , +NESTED_S2_ROOT_GPA ,NESTED_S2_ROOT_SLOT , +vm_adjust_num_guest_pages (vm->mode,1 ),0 ); + +/* Run the guest until it has ERET'd from vEL2 to vEL1. */ +vcpu_run (vcpu); +switch (get_ucall (vcpu, &uc)) { +case UCALL_SYNC : +TEST_ASSERT_EQ (uc.args[1 ],1 ); +break ; +case UCALL_ABORT : +REPORT_GUEST_ASSERT (uc); +break ; +default : +TEST_FAIL ("Unhandled ucall: %ld" , uc.cmd); + } + +/* +* The vCPU's last-run context is vEL1, backed by a shadow stage-2 +* MMU. Enable nested stage-2 with an empty root so that the ioctl +* fails if it tries to interpret the userspace GPA as an L2 IPA. +* +* Prefault in two halves so that the second ioctl exercises a +* repeated shadow-MMU attach and canonical stage-2 swap. +* +* (Note that an implementation that wrongly populates shadow +* stage-2 page tables would not be caught as userland can't +* inspect these.) +*/ + s2 =enable_empty_nested_s2 (vcpu); +pre_fault (vcpu,TEST_MEM_GPA ,TEST_MEM_SIZE /2 ); +pre_fault (vcpu,TEST_MEM_GPA +TEST_MEM_SIZE /2 ,TEST_MEM_SIZE /2 ); +restore_nested_s2 (vcpu, &s2); + +/* Resume at vEL1 and touch the prefaulted range. */ +vcpu_run (vcpu); +switch (get_ucall (vcpu, &uc)) { +case UCALL_DONE : +break ; +case UCALL_ABORT : +REPORT_GUEST_ASSERT (uc); +break ; +default : +TEST_FAIL ("Unhandled ucall: %ld" , uc.cmd); + } + +kvm_vm_free (vm); +return 0 ; +}
Quoted 3 lines
Quoted 3 lines
--
2.55.0
--
Cheers, Lorenzo
Re: [PATCH 8/8] KVM: selftests: Add nested pre-fault test for arm64
In reply to Itaru Kitayama+cc Wei-Lin
Quoted 25 lines · Itaru Kitayama
Hi Lorenzo,
Quoted 14 lines · Lorenzo Stoakes (ARM)
From: Jack Thomson <[email protected]>
Add an arm64 nested-virt selftest for
KVM_PRE_FAULT_MEMORY. The guest enters vEL1 and exits to userspace with a nested/shadow stage-2 MMU as the vCPU's last-run context.Before prefaulting, userspace enables HCR_EL2.VM and points
VTTBR_EL2at an empty nested stage-2 root. A prefault implementation that incorrectly treats the userspace GPA as an L2 IPA will fail the ioctl; the correct path targets the canonical stage-2 and succeeds.Restore the original nested state before resuming the guest, then touch the prefaulted range to check that vEL1 still runs correctly.
Quoted 7 lines
Signed-off-by Jack Thomson <[email protected]>[ljs: partial progress, >4 KiB pgsize, commit msg, comment fixups]Signed-off-by Lorenzo Stoakes (ARM) <[email protected]>--- tools/testing/selftests/kvm/Makefile.kvm | 1 + .../selftests/kvm/arm64/nv_pre_fault_memory_test.c | 206 +++++++++++++++++++++ 2 files changed, 207 insertions(+)
Quoted 4 lines
I wonder whether this selftest can use functions Wei-Lin proposed some time ago [1]: [email protected] or would you prefer this test as propose, since it is clear as to what needs to be done to enter L2.
I think if these aren't merged yet then that could be a follow up?
Quoted 230 lines
Thanks,
Itaru.Quoted 226 lines
tools/testing/selftests/kvm/Makefile.kvm +1
-0@@ -173,6 +173,7 @@ TEST_GEN_PROGS_arm64 += arm64/debug-exceptions TEST_GEN_PROGS_arm64 += arm64/hello_el2 TEST_GEN_PROGS_arm64 += arm64/host_sve TEST_GEN_PROGS_arm64 += arm64/hypercalls +TEST_GEN_PROGS_arm64 += arm64/nv_pre_fault_memory_test TEST_GEN_PROGS_arm64 += arm64/external_aborts TEST_GEN_PROGS_arm64 += arm64/mmio_sign_ext TEST_GEN_PROGS_arm64 += arm64/page_fault_testtools/testing/selftests/kvm/arm64/nv_pre_fault_memory_test.c new file +206
-0@@ -0,0 +1,206 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* +* nv_pre_fault_memory_test - Test KVM_PRE_FAULT_MEMORY on a vCPU whose +* last-run context is nested. +* +* The guest starts at vEL2, mirrors its EL2 translation regime into the +* real EL1 registers, drops HCR_EL2.TGE and ERETs to vEL1, then exits to +* userspace from vEL1 so that the vCPU's last-run context selects a +* shadow stage-2 MMU. Userspace then enables an empty nested stage-2 +* before prefaulting. Prefaulting must target the canonical stage-2, +* regardless of the vCPU's nested state. +*/ +#include "kvm_util.h" +#include "processor.h" +#include "test_util.h" +#include "ucall.h" + +#include <asm/sysreg.h> +#include <linux/sizes.h> + +#define TEST_MEM_SLOT 10 +#define NESTED_S2_ROOT_SLOT 11 +#define TEST_MEM_SIZE SZ_2M +#define TEST_MEM_GPA SZ_1G +#define NESTED_S2_ROOT_GPA (TEST_MEM_GPA + TEST_MEM_SIZE) + +struct nested_s2_state { +u64 hcr_el2; +u64 vttbr_el2; +}; + +static void guest_el1_code (void ) +{ +u64 offset; + +GUEST_ASSERT_EQ (get_current_el (),1 ); + +/* Exit to userspace with the vEL1 (nested) context live. */ +GUEST_SYNC (1 ); + +/* +* Touch the prefaulted range. vstage-2 is disabled, so the shadow +* stage-2 is a 1:1 view of the canonical IPA space. +*/ +for (offset =0 ; offset <TEST_MEM_SIZE ; offset +=SZ_4K ) +READ_ONCE (*(u64 *)(TEST_MEM_GPA + offset)); + +GUEST_DONE (); +} + +static void guest_code (void ) +{ +u64 sp; + +GUEST_ASSERT_EQ (get_current_el (),2 ); + +/* +* Mirror the EL2 translation regime into the real EL1 registers so +* that vEL1 runs on the test's stage-1 page tables. With E2H=1, the +* _EL1 accessors read the EL2 registers, and the _EL12 accessors +* write the real EL1 registers. +*/ +write_sysreg_s (read_sysreg (sctlr_el1),SYS_SCTLR_EL12 ); +write_sysreg_s (read_sysreg (tcr_el1),SYS_TCR_EL12 ); +write_sysreg_s (read_sysreg (ttbr0_el1),SYS_TTBR0_EL12 ); +write_sysreg_s (read_sysreg (mair_el1),SYS_MAIR_EL12 ); +write_sysreg_s (read_sysreg (cpacr_el1),SYS_CPACR_EL12 ); + +/* Run vEL1 on the same stack. */ + asmvolatile ("mov %0, sp" :"=r" (sp)); +write_sysreg (sp, sp_el1); + +/* +* Drop TGE so that vEL1 is a nested context rather than host EL0. +* KVM backs it with a shadow stage-2 MMU even though vstage-2 is +* disabled (HCR_EL2.VM=0). +*/ +write_sysreg (read_sysreg (hcr_el2) & ~HCR_EL2_TGE , hcr_el2); +isb (); + +write_sysreg (PSR_MODE_EL1h |PSR_F_BIT |PSR_I_BIT |PSR_A_BIT | +PSR_D_BIT , spsr_el2); +write_sysreg ((u64 )guest_el1_code, elr_el2); + asmvolatile ("eret" ); + +GUEST_ASSERT (false); +} + +static void pre_fault (struct kvm_vcpu *vcpu,u64 gpa,u64 size) +{ +struct kvm_pre_fault_memory range = { + .gpa = gpa, + .size = size, + }; +int ret; + +do { + ret =__vcpu_ioctl (vcpu,KVM_PRE_FAULT_MEMORY , &range); + }while ((!ret && range.size) || + (ret <0 && (errno ==EINTR || errno ==EAGAIN ))); + +TEST_ASSERT (!ret,"KVM_PRE_FAULT_MEMORY failed, ret: %d errno: %d" , + ret, errno); +TEST_ASSERT_EQ (range.size,0 ); +} + +static struct nested_s2_state enable_empty_nested_s2 (struct kvm_vcpu *vcpu) +{ +struct nested_s2_state state = { + .hcr_el2 =vcpu_get_reg (vcpu,KVM_ARM64_SYS_REG (SYS_HCR_EL2 )), + .vttbr_el2 =vcpu_get_reg (vcpu, +KVM_ARM64_SYS_REG (SYS_VTTBR_EL2 )), + }; + +TEST_ASSERT (!(state.hcr_el2 &HCR_EL2_TGE ), +"vCPU should be in nested/vEL1 context" ); + +vcpu_set_reg (vcpu,KVM_ARM64_SYS_REG (SYS_VTTBR_EL2 ), +NESTED_S2_ROOT_GPA ); +vcpu_set_reg (vcpu,KVM_ARM64_SYS_REG (SYS_HCR_EL2 ), + state.hcr_el2 |HCR_EL2_VM ); + +return state; +} + +static void restore_nested_s2 (struct kvm_vcpu *vcpu, +struct nested_s2_state *state) +{ +vcpu_set_reg (vcpu,KVM_ARM64_SYS_REG (SYS_HCR_EL2 ), state->hcr_el2); +vcpu_set_reg (vcpu,KVM_ARM64_SYS_REG (SYS_VTTBR_EL2 ), + state->vttbr_el2); +} + +int main (void ) +{ +struct nested_s2_state s2; +struct kvm_vcpu_init init; +struct kvm_vcpu *vcpu; +struct kvm_vm *vm; +struct ucall uc; +u64 npages; + +TEST_REQUIRE (kvm_check_cap (KVM_CAP_ARM_EL2 )); +TEST_REQUIRE (kvm_check_cap (KVM_CAP_PRE_FAULT_MEMORY )); + + vm =vm_create (1 ); + +kvm_get_default_vcpu_target (vm, &init); + init.features[0 ] |=BIT (KVM_ARM_VCPU_HAS_EL2 ); + vcpu =aarch64_vcpu_add (vm,0 , &init, guest_code); +kvm_arch_vm_finalize_vcpus (vm); + + npages =TEST_MEM_SIZE / vm->page_size; +vm_userspace_mem_region_add (vm,VM_MEM_SRC_ANONYMOUS ,TEST_MEM_GPA , +TEST_MEM_SLOT , npages,0 ); +virt_map (vm,TEST_MEM_GPA ,TEST_MEM_GPA , npages); + +vm_userspace_mem_region_add (vm,VM_MEM_SRC_ANONYMOUS , +NESTED_S2_ROOT_GPA ,NESTED_S2_ROOT_SLOT , +vm_adjust_num_guest_pages (vm->mode,1 ),0 ); + +/* Run the guest until it has ERET'd from vEL2 to vEL1. */ +vcpu_run (vcpu); +switch (get_ucall (vcpu, &uc)) { +case UCALL_SYNC : +TEST_ASSERT_EQ (uc.args[1 ],1 ); +break ; +case UCALL_ABORT : +REPORT_GUEST_ASSERT (uc); +break ; +default : +TEST_FAIL ("Unhandled ucall: %ld" , uc.cmd); + } + +/* +* The vCPU's last-run context is vEL1, backed by a shadow stage-2 +* MMU. Enable nested stage-2 with an empty root so that the ioctl +* fails if it tries to interpret the userspace GPA as an L2 IPA. +* +* Prefault in two halves so that the second ioctl exercises a +* repeated shadow-MMU attach and canonical stage-2 swap. +* +* (Note that an implementation that wrongly populates shadow +* stage-2 page tables would not be caught as userland can't +* inspect these.) +*/ + s2 =enable_empty_nested_s2 (vcpu); +pre_fault (vcpu,TEST_MEM_GPA ,TEST_MEM_SIZE /2 ); +pre_fault (vcpu,TEST_MEM_GPA +TEST_MEM_SIZE /2 ,TEST_MEM_SIZE /2 ); +restore_nested_s2 (vcpu, &s2); + +/* Resume at vEL1 and touch the prefaulted range. */ +vcpu_run (vcpu); +switch (get_ucall (vcpu, &uc)) { +case UCALL_DONE : +break ; +case UCALL_ABORT : +REPORT_GUEST_ASSERT (uc); +break ; +default : +TEST_FAIL ("Unhandled ucall: %ld" , uc.cmd); + } + +kvm_vm_free (vm); +return 0 ; +}
Quoted 3 lines
Quoted 3 lines
--
2.55.0
--
Cheers, Lorenzo
Re: [PATCH 8/8] KVM: selftests: Add nested pre-fault test for arm64
Quoted 28 lines · Lorenzo Stoakes (ARM)
+cc Wei-Lin
Quoted 25 lines · Itaru Kitayama
Hi Lorenzo,
Quoted 21 lines · Lorenzo Stoakes (ARM)
From: Jack Thomson <[email protected]>
Add an arm64 nested-virt selftest for
KVM_PRE_FAULT_MEMORY. The guest enters vEL1 and exits to userspace with a nested/shadow stage-2 MMU as the vCPU's last-run context.Before prefaulting, userspace enables HCR_EL2.VM and points
VTTBR_EL2at an empty nested stage-2 root. A prefault implementation that incorrectly treats the userspace GPA as an L2 IPA will fail the ioctl; the correct path targets the canonical stage-2 and succeeds.Restore the original nested state before resuming the guest, then touch the prefaulted range to check that vEL1 still runs correctly.
Signed-off-by Jack Thomson <[email protected]>[ljs: partial progress, >4 KiB pgsize, commit msg, comment fixups]Signed-off-by Lorenzo Stoakes (ARM) <[email protected]>--- tools/testing/selftests/kvm/Makefile.kvm | 1 + .../selftests/kvm/arm64/nv_pre_fault_memory_test.c | 206 +++++++++++++++++++++ 2 files changed, 207 insertions(+)
Quoted 6 lines
Quoted 4 lines
I wonder whether this selftest can use functions Wei-Lin proposed some time ago [1]: [email protected] or would you prefer this test as propose, since it is clear as to what needs to be done to enter L2.
I think if these aren't merged yet then that could be a follow up?
This selftest doesn't require setting up L1's stage-2 for L2, so to me the current approach is okay.
[...]
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
tools/testing/selftests/kvm/arm64/nv_pre_fault_memory_test.c new file +9
-0+static void guest_code (void ) +{ +u64 sp; + +GUEST_ASSERT_EQ (get_current_el (),2 ); + +/* +* Mirror the EL2 translation regime into the real EL1 registers so +* that vEL1 runs on the test's stage-1 page tables. With E2H=1, the
Quoted 8 lines
Quoted 8 lines
tools/testing/selftests/kvm/arm64/nv_pre_fault_memory_test.c line 67
Quoted 8 lines
tools/testing/selftests/kvm/arm64/nv_pre_fault_memory_test.c new file +8
-0+* _EL1 accessors read the EL2 registers, and the _EL12 accessors +* write the real EL1 registers. +*/ +write_sysreg_s (read_sysreg (sctlr_el1),SYS_SCTLR_EL12 ); +write_sysreg_s (read_sysreg (tcr_el1),SYS_TCR_EL12 ); +write_sysreg_s (read_sysreg (ttbr0_el1),SYS_TTBR0_EL12 ); +write_sysreg_s (read_sysreg (mair_el1),SYS_MAIR_EL12 ); +write_sysreg_s (read_sysreg (cpacr_el1),SYS_CPACR_EL12 );
Just FYI, the at.c selftest uses a macro for these.
Quoted 21 lines
Quoted 21 lines
Quoted 21 lines
tools/testing/selftests/kvm/arm64/nv_pre_fault_memory_test.c new file +21
-0+ +/* Run vEL1 on the same stack. */ + asmvolatile ("mov %0, sp" :"=r" (sp)); +write_sysreg (sp, sp_el1); + +/* +* Drop TGE so that vEL1 is a nested context rather than host EL0. +* KVM backs it with a shadow stage-2 MMU even though vstage-2 is +* disabled (HCR_EL2.VM=0). +*/ +write_sysreg (read_sysreg (hcr_el2) & ~HCR_EL2_TGE , hcr_el2); +isb (); + +write_sysreg (PSR_MODE_EL1h |PSR_F_BIT |PSR_I_BIT |PSR_A_BIT | +PSR_D_BIT , spsr_el2); +write_sysreg ((u64 )guest_el1_code, elr_el2); + asmvolatile ("eret" ); + +GUEST_ASSERT (false); +} +
[...]
Quoted 37 lines
Quoted 37 lines
Quoted 37 lines
tools/testing/selftests/kvm/arm64/nv_pre_fault_memory_test.c new file +37
-0+ +static struct nested_s2_state enable_empty_nested_s2 (struct kvm_vcpu *vcpu) +{ +struct nested_s2_state state = { + .hcr_el2 =vcpu_get_reg (vcpu,KVM_ARM64_SYS_REG (SYS_HCR_EL2 )), + .vttbr_el2 =vcpu_get_reg (vcpu, +KVM_ARM64_SYS_REG (SYS_VTTBR_EL2 )), + }; + +TEST_ASSERT (!(state.hcr_el2 &HCR_EL2_TGE ), +"vCPU should be in nested/vEL1 context" ); + +vcpu_set_reg (vcpu,KVM_ARM64_SYS_REG (SYS_VTTBR_EL2 ), +NESTED_S2_ROOT_GPA ); +vcpu_set_reg (vcpu,KVM_ARM64_SYS_REG (SYS_HCR_EL2 ), + state.hcr_el2 |HCR_EL2_VM ); + +return state; +} + +static void restore_nested_s2 (struct kvm_vcpu *vcpu, +struct nested_s2_state *state) +{ +vcpu_set_reg (vcpu,KVM_ARM64_SYS_REG (SYS_HCR_EL2 ), state->hcr_el2); +vcpu_set_reg (vcpu,KVM_ARM64_SYS_REG (SYS_VTTBR_EL2 ), + state->vttbr_el2); +} + +int main (void ) +{ +struct nested_s2_state s2; +struct kvm_vcpu_init init; +struct kvm_vcpu *vcpu; +struct kvm_vm *vm; +struct ucall uc; +u64 npages; +
[...]
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
tools/testing/selftests/kvm/arm64/nv_pre_fault_memory_test.c new file +9
-0+ +/* +* The vCPU's last-run context is vEL1, backed by a shadow stage-2 +* MMU. Enable nested stage-2 with an empty root so that the ioctl +* fails if it tries to interpret the userspace GPA as an L2 IPA. +* +* Prefault in two halves so that the second ioctl exercises a +* repeated shadow-MMU attach and canonical stage-2 swap. +*
Quoted 8 lines
Quoted 8 lines
tools/testing/selftests/kvm/arm64/nv_pre_fault_memory_test.c line 190
Quoted 8 lines
tools/testing/selftests/kvm/arm64/nv_pre_fault_memory_test.c new file +8
-0+* (Note that an implementation that wrongly populates shadow +* stage-2 page tables would not be caught as userland can't +* inspect these.) +*/ + s2 =enable_empty_nested_s2 (vcpu); +pre_fault (vcpu,TEST_MEM_GPA ,TEST_MEM_SIZE /2 ); +pre_fault (vcpu,TEST_MEM_GPA +TEST_MEM_SIZE /2 ,TEST_MEM_SIZE /2 ); +restore_nested_s2 (vcpu, &s2);
Sorry I don't get what enable_empty_nested_s2(), restore_nested_s2() brings to the test?
After quickly going through the prefault implementation there isn't anything about it that is nested related, how would giving L2 an empty stage-2 translation affect the outcome if it misinterprets the GPA as an L2 IPA?
In other words would there be potential false positives if we don't call these functions?
Thanks,
Wei-Lin Chang
Quoted 23 lines
Quoted 17 lines
Quoted 17 lines
tools/testing/selftests/kvm/arm64/nv_pre_fault_memory_test.c new file +16
-0+ +/* Resume at vEL1 and touch the prefaulted range. */ +vcpu_run (vcpu); +switch (get_ucall (vcpu, &uc)) { +case UCALL_DONE : +break ; +case UCALL_ABORT : +REPORT_GUEST_ASSERT (uc); +break ; +default : +TEST_FAIL ("Unhandled ucall: %ld" , uc.cmd); + } + +kvm_vm_free (vm); +return 0 ; +}Quoted 3 lines
Quoted 3 lines
--
2.55.0--
Cheers, Lorenzo
Re: [PATCH 8/8] KVM: selftests: Add nested pre-fault test for arm64
Quoted 55 lines · Wei-Lin Chang
Quoted 28 lines · Lorenzo Stoakes (ARM)
+cc Wei-Lin
Quoted 25 lines · Itaru Kitayama
Hi Lorenzo,
Quoted 21 lines · Lorenzo Stoakes (ARM)
From: Jack Thomson <[email protected]>
Add an arm64 nested-virt selftest for
KVM_PRE_FAULT_MEMORY. The guest enters vEL1 and exits to userspace with a nested/shadow stage-2 MMU as the vCPU's last-run context.Before prefaulting, userspace enables HCR_EL2.VM and points
VTTBR_EL2at an empty nested stage-2 root. A prefault implementation that incorrectly treats the userspace GPA as an L2 IPA will fail the ioctl; the correct path targets the canonical stage-2 and succeeds.Restore the original nested state before resuming the guest, then touch the prefaulted range to check that vEL1 still runs correctly.
Signed-off-by Jack Thomson <[email protected]>[ljs: partial progress, >4 KiB pgsize, commit msg, comment fixups]Signed-off-by Lorenzo Stoakes (ARM) <[email protected]>--- tools/testing/selftests/kvm/Makefile.kvm | 1 + .../selftests/kvm/arm64/nv_pre_fault_memory_test.c | 206 +++++++++++++++++++++ 2 files changed, 207 insertions(+)Quoted 6 lines
Quoted 4 lines
I wonder whether this selftest can use functions Wei-Lin proposed some time ago [1]: [email protected] or would you prefer this test as propose, since it is clear as to what needs to be done to enter L2.
I think if these aren't merged yet then that could be a follow up?
This selftest doesn't require setting up L1's stage-2 for L2, so to me the current approach is okay.
[...]
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
tools/testing/selftests/kvm/arm64/nv_pre_fault_memory_test.c new file +11
-0+static void guest_code (void ) +{ +u64 sp; + +GUEST_ASSERT_EQ (get_current_el (),2 ); + +/* +* Mirror the EL2 translation regime into the real EL1 registers so +* that vEL1 runs on the test's stage-1 page tables. With E2H=1, the +* _EL1 accessors read the EL2 registers, and the _EL12 accessors +* write the real EL1 registers.
Quoted 8 lines
Quoted 6 lines
Quoted 6 lines
tools/testing/selftests/kvm/arm64/nv_pre_fault_memory_test.c line 67
Quoted 6 lines
tools/testing/selftests/kvm/arm64/nv_pre_fault_memory_test.c new file +6
-0+*/ +write_sysreg_s (read_sysreg (sctlr_el1),SYS_SCTLR_EL12 ); +write_sysreg_s (read_sysreg (tcr_el1),SYS_TCR_EL12 ); +write_sysreg_s (read_sysreg (ttbr0_el1),SYS_TTBR0_EL12 ); +write_sysreg_s (read_sysreg (mair_el1),SYS_MAIR_EL12 ); +write_sysreg_s (read_sysreg (cpacr_el1),SYS_CPACR_EL12 );Just FYI, the
at.cselftest uses a macro for these.
Ack will check and see if I can use same approach here.
Quoted 86 lines
Quoted 22 lines
Quoted 22 lines
Quoted 22 lines
tools/testing/selftests/kvm/arm64/nv_pre_fault_memory_test.c new file +21
-0+ +/* Run vEL1 on the same stack. */ + asmvolatile ("mov %0, sp" :"=r" (sp)); +write_sysreg (sp, sp_el1); + +/* +* Drop TGE so that vEL1 is a nested context rather than host EL0. +* KVM backs it with a shadow stage-2 MMU even though vstage-2 is +* disabled (HCR_EL2.VM=0). +*/ +write_sysreg (read_sysreg (hcr_el2) & ~HCR_EL2_TGE , hcr_el2); +isb (); + +write_sysreg (PSR_MODE_EL1h |PSR_F_BIT |PSR_I_BIT |PSR_A_BIT | +PSR_D_BIT , spsr_el2); +write_sysreg ((u64 )guest_el1_code, elr_el2); + asmvolatile ("eret" ); + +GUEST_ASSERT (false); +} +[...]
Quoted 37 lines
Quoted 37 lines
Quoted 37 lines
tools/testing/selftests/kvm/arm64/nv_pre_fault_memory_test.c new file +37
-0+ +static struct nested_s2_state enable_empty_nested_s2 (struct kvm_vcpu *vcpu) +{ +struct nested_s2_state state = { + .hcr_el2 =vcpu_get_reg (vcpu,KVM_ARM64_SYS_REG (SYS_HCR_EL2 )), + .vttbr_el2 =vcpu_get_reg (vcpu, +KVM_ARM64_SYS_REG (SYS_VTTBR_EL2 )), + }; + +TEST_ASSERT (!(state.hcr_el2 &HCR_EL2_TGE ), +"vCPU should be in nested/vEL1 context" ); + +vcpu_set_reg (vcpu,KVM_ARM64_SYS_REG (SYS_VTTBR_EL2 ), +NESTED_S2_ROOT_GPA ); +vcpu_set_reg (vcpu,KVM_ARM64_SYS_REG (SYS_HCR_EL2 ), + state.hcr_el2 |HCR_EL2_VM ); + +return state; +} + +static void restore_nested_s2 (struct kvm_vcpu *vcpu, +struct nested_s2_state *state) +{ +vcpu_set_reg (vcpu,KVM_ARM64_SYS_REG (SYS_HCR_EL2 ), state->hcr_el2); +vcpu_set_reg (vcpu,KVM_ARM64_SYS_REG (SYS_VTTBR_EL2 ), + state->vttbr_el2); +} + +int main (void ) +{ +struct nested_s2_state s2; +struct kvm_vcpu_init init; +struct kvm_vcpu *vcpu; +struct kvm_vm *vm; +struct ucall uc; +u64 npages; +[...]
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
tools/testing/selftests/kvm/arm64/nv_pre_fault_memory_test.c new file +9
-0+ +/* +* The vCPU's last-run context is vEL1, backed by a shadow stage-2 +* MMU. Enable nested stage-2 with an empty root so that the ioctl +* fails if it tries to interpret the userspace GPA as an L2 IPA. +* +* Prefault in two halves so that the second ioctl exercises a +* repeated shadow-MMU attach and canonical stage-2 swap. +* Quoted 8 lines
Quoted 8 lines
tools/testing/selftests/kvm/arm64/nv_pre_fault_memory_test.c line 190
Quoted 8 lines
tools/testing/selftests/kvm/arm64/nv_pre_fault_memory_test.c new file +8
-0+* (Note that an implementation that wrongly populates shadow +* stage-2 page tables would not be caught as userland can't +* inspect these.) +*/ + s2 =enable_empty_nested_s2 (vcpu); +pre_fault (vcpu,TEST_MEM_GPA ,TEST_MEM_SIZE /2 ); +pre_fault (vcpu,TEST_MEM_GPA +TEST_MEM_SIZE /2 ,TEST_MEM_SIZE /2 ); +restore_nested_s2 (vcpu, &s2);Sorry I don't get what
enable_empty_nested_s2(),restore_nested_s2()brings to the test?
Quoted 4 lines
After quickly going through the prefault implementation there isn't anything about it that is nested related, how would giving L2 an empty stage-2 translation affect the outcome if it misinterprets the GPA as an L2 IPA?
Well the prefaulting intentionally does not interact with nested and only prefaults for the canonical s2 page tables.
I guess the intention here was to make sure that's the case.
But I think you're right in that it's a bit silly - the code doesn't try to interpret anything in terms of a nested guest, and nobody's going to change it to do so.
Quoted 3 lines
In other words would there be potential false positives if we don't call these functions?
I think really think this is actually asserting anything meaningful.
I'll drop it and simplify - have it just assert that the last-run vCPU was a stage2 vCPU and prefault doesn't do anything weird with that.
The only way it could be broken is if it set a non-canonical mmu in __pre_fault_s2() or set s2fd->nested, neither of which, of course, it does.
Quoted 27 lines
Thanks,
Wei-Lin ChangQuoted 23 lines
Quoted 17 lines
Quoted 17 lines
tools/testing/selftests/kvm/arm64/nv_pre_fault_memory_test.c new file +16
-0+ +/* Resume at vEL1 and touch the prefaulted range. */ +vcpu_run (vcpu); +switch (get_ucall (vcpu, &uc)) { +case UCALL_DONE : +break ; +case UCALL_ABORT : +REPORT_GUEST_ASSERT (uc); +break ; +default : +TEST_FAIL ("Unhandled ucall: %ld" , uc.cmd); + } + +kvm_vm_free (vm); +return 0 ; +}Quoted 3 lines
Quoted 3 lines
--
2.55.0--
Cheers, Lorenzo
--
Cheers, Lorenzo
Re: [PATCH 6/8] KVM: selftests: Enable pre_fault_memory_test for arm64
In reply to Fuad TabbaQuoted 7 lines · Fuad Tabba
Quoted 6 lines · Lorenzo Stoakes (ARM)
From: Jack Thomson <[email protected]>
Enable the
pre_fault_memory_testto run on arm64 by making it work with different guest page sizes and testing multiple guest configurations.
Quoted 7 lines
Quoted 3 lines
Update the
test_assertto compare against theUCALL_EXIT_REASON, for portability, as arm64 exits withKVM_EXIT_MMIOwhile x86 usesKVM_EXIT_IO.The message says this updates the
test_assertfor portability, but the parent has no exit-reason assert and no structkvm_run; this patch adds both, so there was no hardcodedKVM_EXIT_IOto make portable.
Ack will fix up for v2 thanks!
Quoted 5 lines
...
Quoted 2 lines
int
main(int argc, char *argv[])
{
Quoted 7 lines
...
Quoted 4 lines
+
TEST_REQUIRE(kvm_check_cap(KVM_CAP_PRE_FAULT_MEMORY)); test_pre_fault_memory(0, false);Would it be worth adding an arm64 case for gmem_abort()'s pre-fault path?
I think doing this would be quite a bit of work, I think perhaps one for a follow-up?
Quoted 8 lines
Cheers,
/fuadQuoted 4 lines
--
2.55.0
--
Cheers, Lorenzo