KVM: arm64: nv: VNCR page mapping fixes
v3 cover KVM: arm64: nv: VNCR page mapping fixes
On a VNCR abort, KVM walks the guest hypervisor's EL2 stage-1 tables, maps the page that VNCR_EL2 points to at a fixmap, and points the hardware VNCR_EL2 at that fixmap. This series fixes three cases where this goes wrong.
Patch 1 fixes the address that KVM programs into VNCR_EL2 on a host with 16K or 64K pages. The fixmap maps the host page that contains the VNCR page, and KVM takes the offset of the VNCR page in it from the VA. If the guest hypervisor uses a smaller stage-1 granule, the VA and the PA can have different offsets, and the nested context accesses another page of the guest. Patch 1 takes the offset from the PA.
Patch 2 fixes a walk that cannot set the AF. If TCR_EL2.HA is set and the descriptor that maps VNCR_EL2 is in a read-only memslot, the walk returns -EPERM. kvm_handle_vncr_abort() has no case for -EPERM, so the vCPU takes the same abort forever. Patch 2 injects the SEA that the walk recorded.
Patch 3 adds a selftest for both. It maps the VNCR page at a VA whose offset in a 16K or 64K page differs from that of its PA, and puts the table that maps it in a read-only memslot.
Patch 4 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.
Patches 1 and 2 fix bugs that predate this series, and each can be applied on its own.
Based on v7.4-rc3.
Patch numbers below are those of v3.
Changes since v2:
- New patch 1 (Sashiko).
- Patch 3: size the read-only memslot to whole host pages, and map the VNCR page at a VA whose bits [15:12] differ from those of its PA (Sashiko).
Changes since v1:
- New patches 2 and 3 (Sashiko).
- Patch 4: get the HVA with gfn_to_hva_memslot_prot() so that a VNCR page in a read-only memslot is not rejected (Sashiko).
- Patch 4: limit the stage-2 comparison in the commit message to VM_PFNMAP and VM_MIXEDMAP VMAs.
v2: https://lore.kernel.org/r/[email protected]
v1: https://lore.kernel.org/r/[email protected]
Jia He (4):
KVM: arm64: nv: Locate the VNCR page in the host page from its PA
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/include/asm/kvm_nested.h | 1 + arch/arm64/kvm/hyp/vhe/switch.c | 9 +- arch/arm64/kvm/mmu.c | 2 +- arch/arm64/kvm/nested.c | 47 +++- tools/testing/selftests/kvm/Makefile.kvm | 1 + .../selftests/kvm/arm64/nv_vncr_abort.c | 221 ++++++++++++++++++ 7 files changed, 273 insertions(+), 10 deletions(-) create mode 100644 tools/testing/selftests/kvm/arm64/nv_vncr_abort.c
Signature · 1 line
2.34.1
PATCH v3 1/4 KVM: arm64: nv: Locate the VNCR page in the host page from its PA
VNCR_EL2 only needs to be 4K aligned. On a host with 16K or 64K pages, kvm_translate_vncr() records the host page that contains the guest hypervisor's VNCR page, and that host page is mapped at the VNCR fixmap. __compute_hcr() then takes the offset of the VNCR page in the host page from bits [PAGE_SHIFT-1:12] of the guest's VNCR_EL2, which is a VA.
If the guest hypervisor uses a stage-1 granule smaller than the host page, these bits of the VA and of the PA can differ. The nested context then accesses another 4K page of the guest instead of the VNCR page.
Take the offset from the PA that the walk returns, and keep it in vt->hpa. Use it only while the VNCR page is mapped. Otherwise the fixmap is clear, the access faults and KVM translates VNCR_EL2 again, so the offset does not matter.
Tested on a Neoverse-N2 host running 16K and 64K page kernels, with the selftest added later in this series. The test maps the VNCR page at a VA whose bits [15:12] differ from those of its PA. With the offset taken from the VA, the nested context reads 0 instead of the value in the VNCR page. With this patch, it reads the expected value.
--- arch/arm64/include/asm/kvm_nested.h | 1 + arch/arm64/kvm/hyp/vhe/switch.c | 9 +++++++-- arch/arm64/kvm/nested.c | 8 +++++++- 3 files changed, 15 insertions(+), 3 deletions(-)
arch/arm64/include/asm/kvm_nested.h +1 -0
@@ -432,6 +432,7 @@ int __kvm_find_s1_desc_level(struct kvm_vcpu *vcpu, u64 va, u64 ipa,/* VNCR management */ int kvm_vcpu_allocate_vncr_tlb (struct kvm_vcpu *vcpu);int kvm_handle_vncr_abort (struct kvm_vcpu *vcpu); +unsigned long kvm_vncr_page_offset (struct kvm_vcpu *vcpu);void kvm_handle_s1e2_tlbi (struct kvm_vcpu *vcpu,u32 inst,u64 val);u16 get_asid_by_regime (struct kvm_vcpu *vcpu,enum trans_regime regime);
arch/arm64/kvm/hyp/vhe/switch.c +7 -2
@@ -94,8 +94,13 @@ static u64 __compute_hcr(struct kvm_vcpu *vcpu)if (guest_hcr &HCR_EL2_NV ) {u64 va =__fix_to_virt (vncr_fixmap (smp_processor_id ())); -/* Inherit the low bits from the actual register */ - va |=__vcpu_sys_reg (vcpu,VNCR_EL2 ) &GENMASK (PAGE_SHIFT -1 ,0 ); +/* +* The fixmap maps the host page that contains the VNCR +* page. Locate the VNCR page in it from its PA: with a +* smaller guest granule, the VA may have another offset. +*/ +if (host_data_test_flag (L1_VNCR_MAPPED )) + va |=kvm_vncr_page_offset (vcpu);write_sysreg_s (va,SYS_VNCR_EL2 );/* Force NV2 in case the guest is forgetful... */
arch/arm64/kvm/nested.c +7 -1
@@ -1643,7 +1643,7 @@ static int kvm_translate_vncr(struct kvm_vcpu *vcpu, bool *is_gmem) } vt->gva = va; - vt->hpa = pfn <<PAGE_SHIFT ; + vt->hpa = (pfn <<PAGE_SHIFT ) |offset_in_page (vt->wr.pa); vt->hpa_writable = writable; vt->valid = true;atomic_set (&vt->cpu,-1 );
@@ -1683,6 +1683,12 @@ static void handle_vncr_perm(struct kvm_vcpu *vcpu)kvm_inject_nested_sync (vcpu, esr); } +/* Offset of the VNCR page in the host page mapped at the VNCR fixmap */ +unsigned long kvm_vncr_page_offset (struct kvm_vcpu *vcpu) +{ +return offset_in_page (vcpu->arch.vncr_tlb->hpa); +} +int kvm_handle_vncr_abort (struct kvm_vcpu *vcpu) {struct vncr_tlb *vt = vcpu->arch.vncr_tlb;
Signature · 1 line
2.34.1
Marc Zyngier
maintainerRe: [PATCH v3 1/4] KVM: arm64: nv: Locate the VNCR page in the host page from its PA
Quoted 11 lines · Jia He
VNCR_EL2only needs to be 4K aligned. On a host with 16K or 64K pages,kvm_translate_vncr()records the host page that contains the guest hypervisor's VNCR page, and that host page is mapped at the VNCR fixmap.__compute_hcr()then takes the offset of the VNCR page in the host page from bits [PAGE_SHIFT-1:12] of the guest'sVNCR_EL2, which is a VA.If the guest hypervisor uses a stage-1 granule smaller than the host page, these bits of the VA and of the PA can differ. The nested context then accesses another 4K page of the guest instead of the VNCR page.
That's a pretty dubious setup, given that a guest hypervisor cannot use a S2 that is smaller than the host page size, and therefore would have to use a S1 size different from its S2. Possible, but unlikely.
Quoted 10 lines
Take the offset from the PA that the walk returns, and keep it in
vt->hpa. Use it only while the VNCR page is mapped. Otherwise the fixmap is clear, the access faults and KVM translatesVNCR_EL2again, so the offset does not matter.Tested on a Neoverse-N2 host running 16K and 64K page kernels, with the selftest added later in this series. The test maps the VNCR page at a VA whose bits [15:12] differ from those of its PA. With the offset taken from the VA, the nested context reads 0 instead of the value in the VNCR page. With this patch, it reads the expected value.
That's for the test commit message, not this.
Quoted 30 lines
Reported-by Sashiko <[email protected]>Fixes 73e1b621b25d ("KVM: arm64: nv: Program host's VNCR_EL2 to the fixmap address")Signed-off-by Jia He <[email protected]>--- arch/arm64/include/asm/kvm_nested.h | 1 + arch/arm64/kvm/hyp/vhe/switch.c | 9 +++++++-- arch/arm64/kvm/nested.c | 8 +++++++- 3 files changed, 15 insertions(+), 3 deletions(-)arch/arm64/include/asm/kvm_nested.h +1
-0@@ -432,6 +432,7 @@ int __kvm_find_s1_desc_level(struct kvm_vcpu *vcpu, u64 va, u64 ipa,/* VNCR management */ int kvm_vcpu_allocate_vncr_tlb (struct kvm_vcpu *vcpu);int kvm_handle_vncr_abort (struct kvm_vcpu *vcpu); +unsigned long kvm_vncr_page_offset (struct kvm_vcpu *vcpu);void kvm_handle_s1e2_tlbi (struct kvm_vcpu *vcpu,u32 inst,u64 val);u16 get_asid_by_regime (struct kvm_vcpu *vcpu,enum trans_regime regime);arch/arm64/kvm/hyp/vhe/switch.c +0
-0@@ -94,8 +94,13 @@ static u64 __compute_hcr(struct kvm_vcpu *vcpu)if (guest_hcr &HCR_EL2_NV ) {u64 va =__fix_to_virt (vncr_fixmap (smp_processor_id ()));
arch/arm64/kvm/hyp/vhe/switch.c static u64 __compute_hcr(struct kvm_vcpu *vcpu) · line 103
Quoted 10 lines
arch/arm64/kvm/hyp/vhe/switch.c +7
-2-/* Inherit the low bits from the actual register */ - va |=__vcpu_sys_reg (vcpu,VNCR_EL2 ) &GENMASK (PAGE_SHIFT -1 ,0 ); +/* +* The fixmap maps the host page that contains the VNCR +* page. Locate the VNCR page in it from its PA: with a +* smaller guest granule, the VA may have another offset. +*/ +if (host_data_test_flag (L1_VNCR_MAPPED )) + va |=kvm_vncr_page_offset (vcpu);
This is a bit gross. If nothing is mapped in the fixmap, then the page offset doesn't matter, does it? The comment is also pretty confusing. It should read something like:
/* * Inherit the offset in the fixmap page from the PA bits * obtained after translating the guest's VA. */ va |= offset_in_page(vcpu->arch.vncr_tlb->hpa);
arch/arm64/kvm/nested.c static int kvm_translate_vncr(struct kvm_vcpu *vcpu, bool *is_gmem) · line 1646
Quoted 13 lines
arch/arm64/kvm/hyp/vhe/switch.c +0
-0write_sysreg_s (va,SYS_VNCR_EL2 );/* Force NV2 in case the guest is forgetful... */ arch/arm64/kvm/nested.c +1
-1@@ -1643,7 +1643,7 @@ static int kvm_translate_vncr(struct kvm_vcpu *vcpu, bool *is_gmem) } vt->gva = va; - vt->hpa = pfn <<PAGE_SHIFT ; + vt->hpa = (pfn <<PAGE_SHIFT ) |offset_in_page (vt->wr.pa);
You should also make sure that when this is fed to set_fixmap(), the sub-page bits are cleared. Yes, it currently does the right thing, but I don't want to rely on this.
arch/arm64/kvm/nested.c static void handle_vncr_perm(struct kvm_vcpu *vcpu) · line 1690
Quoted 12 lines
arch/arm64/kvm/nested.c +5
-0vt->hpa_writable = writable; vt->valid = true;atomic_set (&vt->cpu,-1 );@@ -1683,6 +1683,12 @@ static void handle_vncr_perm(struct kvm_vcpu *vcpu)kvm_inject_nested_sync (vcpu, esr); } +/* Offset of the VNCR page in the host page mapped at the VNCR fixmap */ +unsigned long kvm_vncr_page_offset (struct kvm_vcpu *vcpu) +{ +return offset_in_page (vcpu->arch.vncr_tlb->hpa); +}
And this helper can go.
Thanks,
M.
Signature · 1 line
Without deviation from the norm, progress is not possible.
RE: [PATCH v3 1/4] KVM: arm64: nv: Locate the VNCR page in the host page from its PA
Hi Marc,
Quoted 1 line
-----Original Message-----
[...]
Quoted 7 lines
Quoted 7 lines
VNCR_EL2only needs to be 4K aligned. On a host with 16K or 64K pages,kvm_translate_vncr()records the host page that contains the guest hypervisor's VNCR page, and that host page is mapped at the VNCR fixmap.__compute_hcr()then takes the offset of the VNCR page in the host page from bits [PAGE_SHIFT-1:12] of the guest'sVNCR_EL2, which is a VA.
Quoted 9 lines
Quoted 4 lines
If the guest hypervisor uses a stage-1 granule smaller than the host page, these bits of the VA and of the PA can differ. The nested context then accesses another 4K page of the guest instead of the VNCR page.
That's a pretty dubious setup, given that a guest hypervisor cannot use a S2 that is smaller than the host page size, and therefore would have to use a S1 size different from its S2. Possible, but unlikely.
The selftest uses a 4K EL2 stage-1 with HCR_EL2.VM clear, so no nested stage-2 translation is involved.
Quoted 5 lines
Quoted 5 lines
Take the offset from the PA that the walk returns, and keep it in
vt->hpa. Use it only while the VNCR page is mapped. Otherwise the fixmap is clear, the access faults and KVM translatesVNCR_EL2again, so the offset does not matter.
Quoted 7 lines
Quoted 5 lines
Tested on a Neoverse-N2 host running 16K and 64K page kernels, with the selftest added later in this series. The test maps the VNCR page at a VA whose bits [15:12] differ from those of its PA. With the offset taken from the VA, the nested context reads 0 instead of the value in the VNCR page. With this patch, it reads the expected value.
That's for the test commit message, not this.
Sure, I'll move the test results to the selftest commit message in v4.
Quoted 47 lines
Quoted 5 lines
Reported-by Sashiko <[email protected]>Fixes 73e1b621b25d ("KVM: arm64: nv: Program host's VNCR_EL2 to thefixmap address")
Review
Quoted 8 lines
Signed-off-by Jia He <[email protected]>--- arch/arm64/include/asm/kvm_nested.h | 1 + arch/arm64/kvm/hyp/vhe/switch.c | 9 +++++++-- arch/arm64/kvm/nested.c | 8 +++++++- 3 files changed, 15 insertions(+), 3 deletions(-)(quoted diff) +0
-0
b/arch/arm64/include/asm/kvm_nested.harch/arm64/include/asm/kvm_nested.h int __kvm_find_s1_desc_level(struct kvm_vcpu
Quoted 4 lines
index
0f899addafa8..bc54a4b2e863100644arch/arm64/include/asm/kvm_nested.h +0
-0@@ -432,6 +432,7 @@ int __kvm_find_s1_desc_level(struct kvm_vcpu*vcpu, u64 va, u64 ipa,
arch/arm64/include/asm/kvm_nested.h int __kvm_find_s1_desc_level(struct kvm_vcpu *vcpu, u64 va, u64 ipa,
Quoted 7 lines
arch/arm64/include/asm/kvm_nested.h +1
-0/* VNCR management */ int kvm_vcpu_allocate_vncr_tlb (struct kvm_vcpu *vcpu);int kvm_handle_vncr_abort (struct kvm_vcpu *vcpu); +unsigned long kvm_vncr_page_offset (struct kvm_vcpu *vcpu);void kvm_handle_s1e2_tlbi (struct kvm_vcpu *vcpu,u32 inst,u64 val); u16 get_asid_by_regime(struct kvm_vcpu *vcpu, enum trans_regimeregime);
Review
Quoted 1 line
(quoted diff) +0
-0
b/arch/arm64/kvm/hyp/vhe/switch.carch/arm64/kvm/hyp/vhe/switch.c static u64 __compute_hcr(struct kvm_vcpu *vcpu) · line 95
Quoted 6 lines
index
3f2390a13173..0d15beea418c100644arch/arm64/kvm/hyp/vhe/switch.c +0
-0@@ -94,8 +94,13 @@ static u64 __compute_hcr(struct kvm_vcpu *vcpu)if (guest_hcr &HCR_EL2_NV ) { u64 va =__fix_to_virt(vncr_fixmap(smp_processor_id()));
arch/arm64/kvm/hyp/vhe/switch.c static u64 __compute_hcr(struct kvm_vcpu *vcpu) · line 97
Quoted 3 lines
arch/arm64/kvm/hyp/vhe/switch.c +0
-2-/* Inherit the low bits from the actual register */ - va |= __vcpu_sys_reg(vcpu, VNCR_EL2) &
GENMASK(PAGE_SHIFT - 1, 0);arch/arm64/kvm/hyp/vhe/switch.c static u64 __compute_hcr(struct kvm_vcpu *vcpu) · line 97
Quoted 2 lines
arch/arm64/kvm/hyp/vhe/switch.c +2
-0+/* +* The fixmap maps the host page that contains the VNCR
arch/arm64/kvm/hyp/vhe/switch.c static u64 __compute_hcr(struct kvm_vcpu *vcpu) · line 99
Quoted 2 lines
arch/arm64/kvm/hyp/vhe/switch.c +2
-0+* page. Locate the VNCR page in it from its PA: with a +* smaller guest granule, the VA may have another offset.
Quoted 7 lines
arch/arm64/kvm/hyp/vhe/switch.c static u64 __compute_hcr(struct kvm_vcpu *vcpu) · line 103
Quoted 3 lines
arch/arm64/kvm/hyp/vhe/switch.c +3
-0+*/ +if (host_data_test_flag (L1_VNCR_MAPPED )) + va |=kvm_vncr_page_offset (vcpu);This is a bit gross. If nothing is mapped in the fixmap, then the page offset doesn't matter, does it? The comment is also pretty confusing. It should read something like:
Agreed. I'll drop the mapped check and use the offset directly from the cached HPA, along with your suggested comment.
Quoted 18 lines
/* * Inherit the offset in the fixmap page from the PA bits * obtained after translating the guest's VA. */ va |= offset_in_page(vcpu->arch.vncr_tlb->hpa);arch/arm64/kvm/nested.c static int kvm_translate_vncr(struct kvm_vcpu
Quoted 8 lines
arch/arm64/kvm/hyp/vhe/switch.c +0
-0write_sysreg_s (va,SYS_VNCR_EL2 );/* Force NV2 in case the guest is forgetful... */ arch/arm64/kvm/nested.c +0
-0@@ -1643,7 +1643,7 @@ static int kvm_translate_vncr(struct kvm_vcpu*vcpu, bool
*is_gmem)Quoted 2 lines
arch/arm64/kvm/nested.c +0
-0}
Quoted 7 lines
arch/arm64/kvm/nested.c static int kvm_translate_vncr(struct kvm_vcpu *vcpu, bool *is_gmem) · line 1646
Quoted 3 lines
arch/arm64/kvm/nested.c +1
-1vt->gva = va; - vt->hpa = pfn <<PAGE_SHIFT ; + vt->hpa = (pfn <<PAGE_SHIFT ) |offset_in_page (vt->wr.pa);You should also make sure that when this is fed to
set_fixmap(), the sub-page bits are cleared. Yes, it currently does the right thing, but I don't want to rely on this.
Agreed, I'll explicitly page-align the HPA before passing it to set_fixmap()
Quoted 9 lines
arch/arm64/kvm/nested.c static void handle_vncr_perm(struct kvm_vcpu
Quoted 5 lines
arch/arm64/kvm/nested.c +0
-0vt->hpa_writable = writable; vt->valid = true;atomic_set (&vt->cpu,-1 );@@ -1683,6 +1683,12 @@ static void handle_vncr_perm(struct kvm_vcpu*vcpu)
Quoted 3 lines
arch/arm64/kvm/nested.c +0
-0kvm_inject_nested_sync (vcpu, esr); }
Quoted 8 lines
arch/arm64/kvm/nested.c static void handle_vncr_perm(struct kvm_vcpu *vcpu) · line 1690
Quoted 5 lines
arch/arm64/kvm/nested.c +5
-0+/* Offset of the VNCR page in the host page mapped at the VNCR fixmap */ +unsigned long kvm_vncr_page_offset (struct kvm_vcpu *vcpu) +{ +return offset_in_page (vcpu->arch.vncr_tlb->hpa); +}And this helper can go.
Sure, I'll drop the helper in v4.
Thanks.
___
Best Regards,
Jia He (Justin)
PATCH v3 2/4 KVM: arm64: nv: Inject an SEA when the VNCR walk cannot set the AF
In reply to Jia HeWhen 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.
-EPERM can only come from this AF update. The VNCR walk uses the EL2&0 translation regime, which has no stage-2.
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
@@ -1743,6 +1743,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
Re: [PATCH v3 2/4] KVM: arm64: nv: Inject an SEA when the VNCR walk cannot set the AF
Quoted 6 lines · Jia He
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_EL2with__kvm_at_swap_desc(). If the descriptor is in a read-only memslot, the update fails with-EPERMandwalk_s1()records an SEA on translation table walk in the walk result.
Quoted 6 lines
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.
-EPERMcan only come from this AF update. The VNCR walk uses the EL2&0 translation regime, which has no stage-2.
LGTM.
Quoted 32 lines
Treat
-EPERMlike 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.
Reported-by Sashiko <[email protected]>Fixes bff8aa213dee ("KVM: arm64: Implement HW access flag management in stage-1 SW PTW")Signed-off-by Jia He <[email protected]>--- arch/arm64/kvm/nested.c | 1 + 1 file changed, 1 insertion(+)arch/arm64/kvm/nested.c +1
-1@@ -1743,6 +1743,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. -- 2.34.1
PATCH v3 3/4 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.
The VNCR page is mapped at a VA whose bits [15:12] differ from those of its PA. On a host with 16K or 64K pages, this also checks that KVM locates the VNCR page in the host page from its PA.
--- tools/testing/selftests/kvm/Makefile.kvm | 1 + .../selftests/kvm/arm64/nv_vncr_abort.c | 221 ++++++++++++++++++ 2 files changed, 222 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 +221 -0
221 lines not on this page · raw message
Signature · 1 line
2.34.1
PATCH v3 4/4 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
Marc Zyngier
maintainerRe: [PATCH v3 4/4] KVM: arm64: nv: Allow the L1 VNCR page in cacheable PFNMAP memory
Quoted 34 lines · Jia He
kvm_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()): aVM_PFNMAPorVM_MIXEDMAPVMA 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_PFNMAPandVM_MIXEDMAPVMAs, relax the check to match the stage-2 fault path, and remove the FIXME added by commit4bd7dbe0b224("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 cacheableVM_PFNMAPorVM_MIXEDMAPone 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(), askvm_handle_guest_abort()does.gfn_to_hva_memslot()returnsKVM_HVA_ERR_RO_BADfor 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 commit2684e02bac41("KVM: arm64: nv: Respect read-only PFN when mapping L1 VNCR").
mmu_invalidate_seqis sampled before the PFN is faulted in, so a VMA change after the lookup is caught by themmu_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 withremap_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.
Quoted 5 lines
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 withgfn_to_hva_memslot(), the guest hypervisor takes an SEA. With this patch, the read returns the contents of the VNCR page.
Same as for patch #1: these two paragraphs are better kept either in the cover letter. Keep the commit message on point.
Quoted 39 lines
Signed-off-by Jia He <[email protected]>--- 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 +0
-0
arch/arm64/kvm/nested.c static u64 read_vncr_el2(struct kvm_vcpu *vcpu) · line 1562
Quoted 10 lines
arch/arm64/kvm/nested.c +7
-0@@ -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. +*/
This applies to normal mapping as well, doesn't it? We already have the non-gmem path doing VMA lookups, so this seems rather superfluous. Unless this is covering something else?
Quoted 31 lines
arch/arm64/kvm/nested.c +24
-0+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 ); }
arch/arm64/kvm/nested.c static int kvm_translate_vncr(struct kvm_vcpu *vcpu, bool *is_gmem) · line 1662
Quoted 10 lines
arch/arm64/kvm/nested.c +1
-6-/* -* 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 ;
Otherwise looks OK to me.
Thanks,
M.
Signature · 1 line
Without deviation from the norm, progress is not possible.
RE: [PATCH v3 4/4] KVM: arm64: nv: Allow the L1 VNCR page in cacheable PFNMAP memory
Hi Marc,
Quoted 1 line
-----Original Message-----
[...]
Quoted 4 lines
Same as for patch #1: these two paragraphs are better kept either in the cover letter. Keep the commit message on point.
Agreed, I will move both test descriptions to the cover letter in v4.
Quoted 50 lines
Review
Quoted 9 lines
Signed-off-by Jia He <[email protected]>--- 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(-)(quoted diff) +0
-0
b/arch/arm64/include/asm/kvm_mmu.harch/arm64/kvm/mmu.c static bool kvm_vma_mte_allowed(struct
Quoted 17 lines
index
32392abf2e21..e59c6603b137100644arch/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 +0
-0@@ -1639,7 +1639,7 @@ static bool kvm_vma_mte_allowed(struct
vm_area_struct*vma)Quoted 14 lines
arch/arm64/kvm/mmu.c +1
-1return 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))) {arch/arm64/kvm/mmu.c +0
-0case MT_NORMAL_NC :arch/arm64/kvm/nested.c +0
-0arch/arm64/kvm/nested.c static u64 read_vncr_el2(struct kvm_vcpu *vcpu) · line 1557
Quoted 5 lines
arch/arm64/kvm/nested.c +2
-0@@ -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
arch/arm64/kvm/nested.c static u64 read_vncr_el2(struct kvm_vcpu *vcpu) · line 1558
Quoted 1 line
arch/arm64/kvm/nested.c +1
-0+* VM_MIXEDMAP VMA and the system supports cacheable PFNMAP at stage-2. The
Quoted 9 lines
arch/arm64/kvm/nested.c static u64 read_vncr_el2(struct kvm_vcpu *vcpu) · line 1562
Quoted 4 lines
arch/arm64/kvm/nested.c +4
-0+* 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. +*/ This applies to normal mapping as well, doesn't it? We already have the non-gmem path doing VMA lookups, so this seems rather superfluous. Unless this is covering something else?
Yes, this adds a second VMA lookup for PFNMAP. The existing lookup in hva_to_pfn_remapped() doesn't expose the VMA attributes to kvm_translate_vncr(), so the patch looks up vm_page_prot again. PFNs in the linear map bypass this lookup.
I'd prefer to keep this local to arm64 for v4 to stay aligned with the stage-2 fault path, unless you'd rather extend the generic fault-in interface to reuse the existing lookup.
I also reproduced a spurious SEA when replacing PFNMAP with shmem using MAP_FIXED. In v4, both PFN fault-in errors and cacheability failures are checked after mmu_invalidate_retry() under mmu_lock, before SEA injection. If an invalidation is detected, KVM retries instead.
Quoted 33 lines
arch/arm64/kvm/nested.c static u64 read_vncr_el2(struct kvm_vcpu *vcpu) · line 1563
Quoted 1 line
arch/arm64/kvm/nested.c +1
-0+static bool vncr_pfn_is_cacheable(struct kvm_memory_slot *memslot, gfn_tgfn,
arch/arm64/kvm/nested.c static int kvm_translate_vncr(struct kvm_vcpu
Quoted 27 lines
arch/arm64/kvm/nested.c +23
-0+ 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)Quoted 3 lines
arch/arm64/kvm/nested.c +0
-0writable = !(memslot->flags &KVM_MEM_READONLY ); }
Quoted 13 lines
arch/arm64/kvm/nested.c static int kvm_translate_vncr(struct kvm_vcpu *vcpu, bool *is_gmem) · line 1662
Quoted 10 lines
arch/arm64/kvm/nested.c +1
-6-/* -* 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 ;Otherwise looks OK to me.
Thanks for the review.
Best Regards,
Jia He (Justin)
Marc Zyngier
maintainerRe: [PATCH v3 4/4] KVM: arm64: nv: Allow the L1 VNCR page in cacheable PFNMAP memory
On Thu, 03 Dec 2026 02:32:23 +0000,
Justin He <[email protected]> wrote:
[...]
Quoted 23 lines
Quoted 13 lines
arch/arm64/kvm/nested.c static u64 read_vncr_el2(struct kvm_vcpu *vcpu) · line 1557
Quoted 10 lines
arch/arm64/kvm/nested.c +2
-0@@ -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
arch/arm64/kvm/nested.c static u64 read_vncr_el2(struct kvm_vcpu *vcpu) · line 1558
Quoted 1 line
arch/arm64/kvm/nested.c +1
-0+* VM_MIXEDMAP VMA and the system supports cacheable PFNMAP at stage-2. The
Quoted 9 lines
arch/arm64/kvm/nested.c static u64 read_vncr_el2(struct kvm_vcpu *vcpu) · line 1562
Quoted 4 lines
arch/arm64/kvm/nested.c +4
-0+* 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. +*/ This applies to normal mapping as well, doesn't it? We already have the non-gmem path doing VMA lookups, so this seems rather superfluous. Unless this is covering something else?
Quoted 4 lines
Yes, this adds a second VMA lookup for PFNMAP. The existing lookup in
hva_to_pfn_remapped()doesn't expose the VMA attributes tokvm_translate_vncr(), so the patch looks upvm_page_protagain. PFNs in the linear map bypass this lookup.
I understand what the patch does. My remark is about the comment
itself: it seems to indicate that this does something special, but I don't see how this behaves differently from the code we already have (all code paths release their respective lookup locks and then check for a concurrent invalidation whilst holding the mmu_lock).
Quoted 4 lines
I'd prefer to keep this local to arm64 for v4 to stay aligned with the stage-2 fault path, unless you'd rather extend the generic fault-in interface to reuse the existing lookup.
I think this is the correct place for now.
Quoted 6 lines
I also reproduced a spurious SEA when replacing PFNMAP with shmem using
MAP_FIXED. In v4, both PFN fault-in errors and cacheability failures are checked aftermmu_invalidate_retry()undermmu_lock, before SEA injection. If an invalidation is detected, KVM retries instead.
I don't immediately see it, but I'll find out once you post the patches.
Thanks,
M.
Signature · 1 line
Without deviation from the norm, progress is not possible.