The VNCR area is 4K aligned. On a host with 16K or 64K pages, KVM maps the host page containing the VNCR area at a fixmap address. __compute_hcr() currently takes the offset within that page from the guest's VNCR_EL2, which holds a VA. The guest hypervisor can use a smaller EL2 stage-1 granule than the host page size. This is possible with nested stage-2 disabled (HCR_EL2.VM clear), or with a nested stage-2 granule at least as large as the host page. The VA and PA offsets can then differ, and VNCR accesses land in the wrong 4K area. Keep the translated offset in the cached HPA. Align the HPA when installing the fixmap and use its offset when programming VNCR_EL2. Reported-by: Sashiko Closes: https://lore.kernel.org/all/sashiko-outbox-232440@kernel.org/ Fixes: 73e1b621b25d ("KVM: arm64: nv: Program host's VNCR_EL2 to the fixmap address") Signed-off-by: Jia He --- arch/arm64/include/asm/kvm_nested.h | 20 ++++++++++++++++++++ arch/arm64/kvm/hyp/vhe/switch.c | 7 +++++-- arch/arm64/kvm/nested.c | 24 +++--------------------- 3 files changed, 28 insertions(+), 23 deletions(-) diff --git a/arch/arm64/include/asm/kvm_nested.h b/arch/arm64/include/asm/kvm_nested.h index 0f899addafa8..4b0057413229 100644 --- a/arch/arm64/include/asm/kvm_nested.h +++ b/arch/arm64/include/asm/kvm_nested.h @@ -430,6 +430,26 @@ int __kvm_find_s1_desc_level(struct kvm_vcpu *vcpu, u64 va, u64 ipa, int *level); /* VNCR management */ +struct vncr_tlb { + /* The guest's VNCR_EL2 */ + u64 gva; + struct s1_walk_info wi; + struct s1_walk_result wr; + + /* Host physical address of the 4K VNCR page. */ + u64 hpa; + bool hpa_writable; + + /* -1 when not mapped on a CPU */ + atomic_t cpu; + + /* + * true if the TLB is valid. Can only be changed with the + * mmu_lock held. + */ + bool valid; +}; + int kvm_vcpu_allocate_vncr_tlb(struct kvm_vcpu *vcpu); int kvm_handle_vncr_abort(struct kvm_vcpu *vcpu); void kvm_handle_s1e2_tlbi(struct kvm_vcpu *vcpu, u32 inst, u64 val); diff --git a/arch/arm64/kvm/hyp/vhe/switch.c b/arch/arm64/kvm/hyp/vhe/switch.c index 3f2390a13173..c6aab5940376 100644 --- a/arch/arm64/kvm/hyp/vhe/switch.c +++ b/arch/arm64/kvm/hyp/vhe/switch.c @@ -94,8 +94,11 @@ 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); + /* + * Inherit the offset within the fixmap page from the PA + * obtained after translating the guest's VA. + */ + va |= offset_in_page(vcpu->arch.vncr_tlb->hpa); write_sysreg_s(va, SYS_VNCR_EL2); /* Force NV2 in case the guest is forgetful... */ diff --git a/arch/arm64/kvm/nested.c b/arch/arm64/kvm/nested.c index 4d83281a7a28..ff78d2784db5 100644 --- a/arch/arm64/kvm/nested.c +++ b/arch/arm64/kvm/nested.c @@ -19,25 +19,6 @@ #include "sys_regs.h" #include "vgic/vgic.h" -struct vncr_tlb { - /* The guest's VNCR_EL2 */ - u64 gva; - struct s1_walk_info wi; - struct s1_walk_result wr; - - u64 hpa; - bool hpa_writable; - - /* -1 when not mapped on a CPU */ - atomic_t cpu; - - /* - * true if the TLB is valid. Can only be changed with the - * mmu_lock held. - */ - bool valid; -}; - /* * Ratio of live shadow S2 MMU per vcpu. This is a trade-off between * memory usage and potential number of different sets of S2 PTs in @@ -1643,7 +1624,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); @@ -1805,7 +1786,8 @@ static void kvm_map_l1_vncr(struct kvm_vcpu *vcpu) */ if (pgprot_val(prot) != pgprot_val(PAGE_NONE)) { atomic_set(&vt->cpu, smp_processor_id()); - __set_fixmap(vncr_fixmap(atomic_read(&vt->cpu)), vt->hpa, prot); + __set_fixmap(vncr_fixmap(atomic_read(&vt->cpu)), + vt->hpa & PAGE_MASK, prot); host_data_set_flag(L1_VNCR_MAPPED); } } -- 2.34.1