KVM: arm64: nv: VNCR page in read-only and cacheable PFNMAP memory
v2 cover KVM: arm64: nv: VNCR page in read-only and cacheable PFNMAP memory
On a VNCR abort, KVM walks the guest hypervisor's EL2 stage-1 tables and maps the page that VNCR_EL2 points to. This series fixes two cases where that fails.
Patch 1 fixes a bug that predates this series. If TCR_EL2.HA is set and the descriptor that maps VNCR_EL2 is in a read-only memslot, KVM cannot set the AF and the walk returns -EPERM. kvm_handle_vncr_abort() has no case for -EPERM, so the vCPU takes the same abort forever. Patch 1 injects the SEA that the walk recorded.
Patch 2 adds a selftest for this case.
Patch 3 allows the VNCR page in a cacheable VM_PFNMAP or VM_MIXEDMAP VMA. The stage-2 fault path already maps such memory as cacheable, but kvm_translate_vncr() rejects it and injects an SEA.
Patch 1 can be applied on its own.
Based on v7.4-rc3.
Changes since v1:
- New patches 1 and 2, for the -EPERM issue found by Sashiko.
- Patch 3: get the HVA with gfn_to_hva_memslot_prot() so that a VNCR page in a read-only memslot is not rejected (Sashiko).
- Patch 3: limit the stage-2 comparison in the commit message to VM_PFNMAP and VM_MIXEDMAP VMAs.
v1: https://lore.kernel.org/r/[email protected]
Jia He (3):
KVM: arm64: nv: Inject an SEA when the VNCR walk cannot set the AF
KVM: arm64: selftests: Add a test for VNCR_EL2 translation faults
KVM: arm64: nv: Allow the L1 VNCR page in cacheable PFNMAP memory
arch/arm64/include/asm/kvm_mmu.h | 2 + arch/arm64/kvm/mmu.c | 2 +- arch/arm64/kvm/nested.c | 39 +++- tools/testing/selftests/kvm/Makefile.kvm | 1 + .../selftests/kvm/arm64/nv_vncr_abort.c | 211 ++++++++++++++++++ 5 files changed, 248 insertions(+), 7 deletions(-) create mode 100644 tools/testing/selftests/kvm/arm64/nv_vncr_abort.c
Signature · 1 line
2.34.1
PATCH v2 1/3 KVM: arm64: nv: Inject an SEA when the VNCR walk cannot set the AF
When the guest hypervisor enables hardware Access flag management (TCR_EL2.HA), KVM's stage-1 walker sets the AF in the descriptor that maps VNCR_EL2 with __kvm_at_swap_desc(). If the descriptor is in a read-only memslot, the update fails with -EPERM and walk_s1() records an SEA on translation table walk in the walk result.
kvm_handle_vncr_abort() does not handle -EPERM. It neither injects the fault nor maps the VNCR page, and returns to the guest. The guest takes the same VNCR abort again and never makes progress.
Treat -EPERM like the other walk failures and inject the recorded fault into the guest hypervisor.
Tested with the selftest added in the next patch on a Neoverse-N2 host. Without this patch, the vCPU exits about 1.3 million times in 5 seconds on the same VNCR abort and makes no progress. With this patch, the guest hypervisor receives an SEA on translation table walk at level 3.
--- arch/arm64/kvm/nested.c | 1 + 1 file changed, 1 insertion(+)
arch/arm64/kvm/nested.c +1 -0
@@ -1737,6 +1737,7 @@ int kvm_handle_vncr_abort(struct kvm_vcpu *vcpu)case -EINVAL :case -ENOENT :case -EACCES : +case -EPERM :/* * Translation failed, inject the corresponding * exception back to EL2.
Signature · 1 line
2.34.1
PATCH v2 2/3 KVM: arm64: selftests: Add a test for VNCR_EL2 translation faults
In reply to Jia HeAdd a test where the guest hypervisor enters a nested context with HCR_EL2.{NV,NV2} set and reads VTTBR_EL2, which FEAT_NV2 turns into a load from the VNCR page. The last-level table that maps the VNCR page is placed in a read-only memslot, and TCR_EL2.HA is set.
With the access flag set, KVM maps the VNCR page and the read returns the value in that page. With the access flag clear, KVM cannot update the descriptor and must inject an SEA on translation table walk into the guest hypervisor. A control case with the table in a writable memslot checks that KVM sets the access flag and maps the page.
--- tools/testing/selftests/kvm/Makefile.kvm | 1 + .../selftests/kvm/arm64/nv_vncr_abort.c | 211 ++++++++++++++++++ 2 files changed, 212 insertions(+) create mode 100644 tools/testing/selftests/kvm/arm64/nv_vncr_abort.c
tools/testing/selftests/kvm/Makefile.kvm +1 -0
@@ -185,6 +185,7 @@ 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/nv_vncr_abort 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_vncr_abort.c new file +211 -0
211 lines not on this page · raw message
Signature · 1 line
2.34.1
[email protected]
AI reviewRe: [PATCH v2 2/3] KVM: arm64: selftests: Add a test for VNCR_EL2 translation faults
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] KVM: arm64: selftests: nv_vncr_abort fails on 16K and 64K host page sizes --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] KVM: arm64: selftests: nv_vncr_abort fails on 16K and 64K host page sizes
--
commit 771173f048bfbff4d1afde9bc6d9374d48302af1
Author: Jia He <[email protected]>
KVM: arm64: selftests: Add a test for VNCR_EL2 translation faults
This commit adds an arm64 KVM selftest for VNCR_EL2 stage-1 translation faults and hardware access flag updates when the last-level page table is in a read-only or writable memslot.
Quoted 5 lines
tools/testing/selftests/kvm/arm64/nv_vncr_abort.c new file +0
-0
[ ... ]
Quoted 43 lines
tools/testing/selftests/kvm/arm64/nv_vncr_abort.c new file +43
-0+static void setup_vncr_page (struct kvm_vm *vm,enum test_mode mode) +{ +u64 oa_mask =GENMASK_ULL (47 ,12 ); +gpa_t data_gpa, table_gpa; +u64 *l2e, *table; +int idx = (VNCR_VA >>12 ) &511 ; + + data_gpa =vm_phy_page_alloc (vm,MEM_REGION_DATA ); +virt_pg_map (vm,VNCR_VA , data_gpa); + *(u64 *)(addr_gpa2hva (vm, data_gpa) +VNCR_VTTBR_EL2 ) =VNCR_MAGIC ; + + l2e =virt_get_pte_hva_at_level (vm,VNCR_VA ,2 ); +TEST_ASSERT ((*l2e &3 ) ==3 ,"level 2 entry is not a table: %lx" , *l2e); + table_gpa = *l2e & oa_mask; + table =addr_gpa2hva (vm, table_gpa); + +if (mode ==RW_PT_AF_CLEAR ) { + table[idx] &= ~PTE_AF ; +return ; + } + +vm_userspace_mem_region_add (vm,VM_MEM_SRC_ANONYMOUS ,RO_PT_GPA , +RO_PT_SLOT ,1 ,KVM_MEM_READONLY ); +memcpy (addr_gpa2hva (vm,RO_PT_GPA ), table, vm->page_size); +if (mode ==RO_PT_AF_CLEAR ) + ((u64 *)addr_gpa2hva (vm,RO_PT_GPA ))[idx] &= ~PTE_AF ; + + *l2e = (*l2e & ~oa_mask) |RO_PT_GPA ; +} + +static void sigalrm_handler (int sig) +{ +} + +static void run_test (enum test_mode mode) +{ +struct kvm_vcpu_init init; +struct kvm_vcpu *vcpu; +struct kvm_vm *vm; +struct ucall uc; +u64 mmfr1; +int ret; +
tools/testing/selftests/kvm/arm64/nv_vncr_abort.c line 162
Quoted 10 lines
tools/testing/selftests/kvm/arm64/nv_vncr_abort.c new file +10
-0+ 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); + + mmfr1 =vcpu_get_reg (vcpu,KVM_ARM64_SYS_REG (SYS_ID_AA64MMFR1_EL1 )); +__TEST_REQUIRE (SYS_FIELD_GET (ID_AA64MMFR1_EL1 ,HAFDBS , mmfr1), +"Hardware AF management not supported" ); +TEST_REQUIRE (vm->page_size ==4096 );
[Severity: Medium]
Will this test fail on 16K and 64K host kernels when the CPU supports a 4K stage-1 translation granule?
guest_modes_append_default() selects VM_MODE_P40V48_4K whenever the CPU supports a 4K granule regardless of the host page size, so vm->page_size is 4096 and TEST_REQUIRE(vm->page_size == 4096) does not skip on 16K or 64K hosts.
Passing npages = 1 to vm_userspace_mem_region_add() in setup_vncr_page() then hits the assertion in vm_mem_add() requiring npages to cover at least one host page:
tools/testing/selftests/kvm/lib/kvm_util.c:vm_mem_add() {
...
TEST_ASSERT(vm_adjust_num_guest_pages(vm->mode, npages) == npages,
"Number of guest pages is not compatible with the host. "
"Try npages=%d", vm_adjust_num_guest_pages(vm->mode, npages));
...
}Also, in RW_PT_AF_CLEAR mode, vm_phy_page_alloc() returns a 4K-aligned data_gpa that is not aligned to a 16K or 64K host page, while KVM maps the containing host pfn at the VNCR fixmap and takes the page offset from VNCR_VA & (PAGE_SIZE - 1) (which is 0), reading from the start of the host page instead of data_gpa.
Should run_test() check TEST_REQUIRE(getpagesize() == 4096), or size RO_PT_SLOT with vm_adjust_num_guest_pages() and align data_gpa to the host page size?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
PATCH v2 3/3 KVM: arm64: nv: Allow the L1 VNCR page in cacheable PFNMAP memory
In reply to Jia Hekvm_translate_vncr() refuses any PFN that isn't memory and injects an SEA into the guest hypervisor. That is stricter than what the stage-2 fault path allows (kvm_s2_fault_pin_pfn()): a VM_PFNMAP or VM_MIXEDMAP VMA whose host stage-1 attributes are cacheable is mapped cacheable at stage-2 when FWB and DIC are present, and the guest can use such memory as RAM (e.g. device memory that it onlines as a NUMA node). If the guest then picks a page of that memory as the VNCR page of one of its own guests, it takes an SEA as soon as it enters that guest.
For VM_PFNMAP and VM_MIXEDMAP VMAs, relax the check to match the stage-2 fault path, and remove the FIXME added by commit 4bd7dbe0b224 ("KVM:
arm64: nv: Inject SEA if guest VNCR isn't normal memory"). Look up the VMA, and allow the PFN if the VMA is a cacheable VM_PFNMAP or VM_MIXEDMAP one and the system supports cacheable PFNMAP at stage-2. No linear mapping is required, because VNCR accesses use the EL2 fixmap and require no cache maintenance.
Get the HVA with gfn_to_hva_memslot_prot(), as kvm_handle_guest_abort() does. gfn_to_hva_memslot() returns KVM_HVA_ERR_RO_BAD for a read-only memslot, and the VMA lookup would then fail. A VNCR page in a read-only memslot is valid and is mapped read-only since commit 2684e02bac41 ("KVM: arm64: nv: Respect read-only PFN when mapping L1 VNCR").
mmu_invalidate_seq is sampled before the PFN is faulted in, so a VMA change after the lookup is caught by the mmu_invalidate_retry() check that follows, as for the PFN itself.
Tested on a host with FEAT_FGT, FWB and DIC, with an L1 whose whole RAM is a cacheable PFNMAP (host RAM withheld with mem= and handed to the VMM by a test driver with remap_pfn_range()) starting an L2 guest hypervisor. Without the patch, L2 repeatedly takes an SEA at the same PC and fails to boot. With the patch, L2 boots successfully.
The read-only case was tested on a Neoverse-N2 host. The guest hypervisor's VNCR page is in a cacheable PFNMAP registered as a read-only memslot, and a nested context reads VTTBR_EL2. When the HVA is looked up with gfn_to_hva_memslot(), the guest hypervisor takes an SEA. With this patch, the read returns the contents of the VNCR page.
--- arch/arm64/include/asm/kvm_mmu.h | 2 ++ arch/arm64/kvm/mmu.c | 2 +- arch/arm64/kvm/nested.c | 38 +++++++++++++++++++++++++++----- 3 files changed, 35 insertions(+), 7 deletions(-)
arch/arm64/include/asm/kvm_mmu.h +2 -0
@@ -385,6 +385,8 @@ static inline void kvm_fault_unlock(struct kvm *kvm)read_unlock (&kvm->mmu_lock); } +bool kvm_vma_is_cacheable (struct vm_area_struct *vma); +/* * ARM64 KVM relies on a simple conversion from physaddr to a kernel * virtual address (KVA) when it does cache maintenance as the CMO
arch/arm64/kvm/mmu.c +1 -1
@@ -1639,7 +1639,7 @@ static bool kvm_vma_mte_allowed(struct vm_area_struct *vma)return vma->vm_flags &VM_MTE_ALLOWED ; } -static bool kvm_vma_is_cacheable (struct vm_area_struct *vma) +bool kvm_vma_is_cacheable (struct vm_area_struct *vma) {switch (FIELD_GET (PTE_ATTRINDX_MASK ,pgprot_val (vma->vm_page_prot))) {case MT_NORMAL_NC :
arch/arm64/kvm/nested.c +32 -6
@@ -1553,6 +1553,37 @@ static u64 read_vncr_el2(struct kvm_vcpu *vcpu)return (u64 )sign_extend64 (__vcpu_sys_reg (vcpu,VNCR_EL2 ),48 ); } +/* +* Allow a non-memory PFN only if it comes from a cacheable VM_PFNMAP or +* VM_MIXEDMAP VMA and the system supports cacheable PFNMAP at stage-2. The +* VMA is looked up after the PFN has been faulted in: the caller must check +* mmu_invalidate_retry() against a sequence count sampled before the +* fault-in. +*/ +static bool vncr_pfn_is_cacheable (struct kvm_memory_slot *memslot,gfn_t gfn, +kvm_pfn_t pfn) +{ +struct vm_area_struct *vma; +unsigned long hva; +bool ret = false; + +if (pfn_is_map_memory (pfn)) +return true; + +if (!kvm_supports_cacheable_pfnmap ()) +return false; + + hva =gfn_to_hva_memslot_prot (memslot, gfn,NULL ); + +mmap_read_lock (current->mm); + vma =vma_lookup (current->mm, hva); +if (vma && (vma->vm_flags & (VM_PFNMAP |VM_MIXEDMAP ))) + ret =kvm_vma_is_cacheable (vma); +mmap_read_unlock (current->mm); + +return ret; +} +static int kvm_translate_vncr (struct kvm_vcpu *vcpu,bool *is_gmem) {struct kvm_memory_slot *memslot;
@@ -1625,12 +1656,7 @@ static int kvm_translate_vncr(struct kvm_vcpu *vcpu, bool *is_gmem) writable = !(memslot->flags &KVM_MEM_READONLY ); } -/* -* FIXME: This check is too restrictive as KVM allows cacheable memory -* attributes for PFNMAP VMAs that have cacheable attributes in host -* stage-1. -*/ -if (!pfn_is_map_memory (pfn)) { +if (!vncr_pfn_is_cacheable (memslot, gfn, pfn)) {kvm_release_faultin_page (vcpu->kvm, page, true, false);fail_s1_walk (&vt->wr, ESR_ELx_FSC_EXTABT, false);return -EINVAL ;
Signature · 1 line
2.34.1