From: "Lorenzo Stoakes (ARM)" <ljs@kernel.org> mainline inclusion from mainline-v7.4-rc1 commit 2b8033a99631b744a67a6fa5df46d0d7b3b9ab7c category: feature bugzilla: https://atomgit.com/openeuler/kernel/issues/10374 CVE: NA Reference: https://github.com/torvalds/linux/commit/2b8033a99631b744a67a6fa5df46d0d7b3b... -------------------------------- commit 2b8033a99631b744a67a6fa5df46d0d7b3b9ab7c upstream 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. 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. [Backport note] Adapted for OLK-6.6: - The ESR is propagated through the s2fd->esr field added by this patch. - user_mem_abort() now derives fault_level, write_fault and exec_fault from the passed-in esr rather than re-reading the vCPU ESR, which is what makes a synthetic ESR usable by the later pre-fault patch. - fault_level and write_fault are now derived unconditionally from the passed-in esr; esr_abt_is_write_fault() retains the PVM-specific cache-maintenance check. - exec_fault keeps the original PVM distinction: the PVM path uses esr_trap_is_iabt() (equivalent to the old kvm_vcpu_trap_is_iabt()), while the ordinary path uses esr_abt_is_exec_fault(). - The now-unused kvm_vcpu_trap_get_fault_level() and kvm_vcpu_trap_is_exec_fault() wrappers are removed. Reviewed-by: Oliver Upton <oupton@kernel.org> Signed-off-by: Lorenzo Stoakes (ARM) <ljs@kernel.org> Reviewed-by: Fuad Tabba <fuad.tabba@linux.dev> Tested-by: Fuad Tabba <fuad.tabba@linux.dev> Link: https://patch.msgid.link/20260923-kvm-arm-prefault-v4-4-d4b0b4dfa8c3@kernel.... Signed-off-by: Marc Zyngier <maz@kernel.org> Signed-off-by: Yanan Wang <wangyanan55@huawei.com> --- arch/arm64/include/asm/kvm_emulate.h | 10 ---------- arch/arm64/kvm/mmu.c | 11 +++++++---- 2 files changed, 7 insertions(+), 14 deletions(-) diff --git a/arch/arm64/include/asm/kvm_emulate.h b/arch/arm64/include/asm/kvm_emulate.h index 373c20b998b3..d8f91faeaf9a 100644 --- a/arch/arm64/include/asm/kvm_emulate.h +++ b/arch/arm64/include/asm/kvm_emulate.h @@ -545,11 +545,6 @@ static inline bool kvm_vcpu_trap_is_iabt(const struct kvm_vcpu *vcpu) return esr_trap_is_iabt(kvm_vcpu_get_esr(vcpu)); } -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); -} - static __always_inline u8 kvm_vcpu_trap_get_fault(const struct kvm_vcpu *vcpu) { return kvm_vcpu_get_esr(vcpu) & ESR_ELx_FSC; @@ -560,11 +555,6 @@ static __always_inline u8 kvm_vcpu_trap_get_fault_type(const struct kvm_vcpu *vc return kvm_vcpu_get_esr(vcpu) & ESR_ELx_FSC_TYPE; } -static __always_inline u8 kvm_vcpu_trap_get_fault_level(const struct kvm_vcpu *vcpu) -{ - return esr_abt_get_fault_level(kvm_vcpu_get_esr(vcpu)); -} - static __always_inline bool kvm_vcpu_abt_issea(const struct kvm_vcpu *vcpu) { return esr_abt_is_sea(kvm_vcpu_get_esr(vcpu)); diff --git a/arch/arm64/kvm/mmu.c b/arch/arm64/kvm/mmu.c index 3ae04d3d104c..eab7ebd0a1d2 100644 --- a/arch/arm64/kvm/mmu.c +++ b/arch/arm64/kvm/mmu.c @@ -1602,6 +1602,7 @@ struct kvm_s2_fault_desc { phys_addr_t fault_ipa; struct kvm_memory_slot *memslot; unsigned long hva; + unsigned long esr; }; static int user_mem_abort(const struct kvm_s2_fault_desc *s2fd, @@ -1613,6 +1614,7 @@ static int user_mem_abort(const struct kvm_s2_fault_desc *s2fd, phys_addr_t fault_ipa = s2fd->fault_ipa; struct kvm_memory_slot *memslot = s2fd->memslot; unsigned long hva = s2fd->hva; + unsigned long esr = s2fd->esr; int ret = 0; bool write_fault, writable, force_pte = false; bool exec_fault, mte_allowed; @@ -1629,7 +1631,7 @@ static int user_mem_abort(const struct kvm_s2_fault_desc *s2fd, gfn_t gfn; kvm_pfn_t pfn; bool logging_active = memslot_is_logging(memslot); - unsigned long fault_level = kvm_vcpu_trap_get_fault_level(vcpu); + unsigned long fault_level = esr_abt_get_fault_level(esr); long vma_pagesize, fault_granule; enum kvm_pgtable_prot prot = KVM_PGTABLE_PROT_R; struct kvm_pgtable *pgt; @@ -1638,7 +1640,7 @@ static int user_mem_abort(const struct kvm_s2_fault_desc *s2fd, fault_granule = result ? PAGE_SIZE : 1UL << ARM64_HW_PGTABLE_LEVEL_SHIFT(fault_level); - write_fault = kvm_is_write_fault(vcpu); + write_fault = esr_abt_is_write_fault(esr); /* * Realms cannot map protected pages read-only @@ -1649,8 +1651,8 @@ static int user_mem_abort(const struct kvm_s2_fault_desc *s2fd, if (vcpu_is_tec(vcpu)) prot = KVM_PGTABLE_PROT_R | KVM_PGTABLE_PROT_W; - exec_fault = result ? kvm_vcpu_trap_is_iabt(vcpu) : - kvm_vcpu_trap_is_exec_fault(vcpu); + exec_fault = result ? esr_trap_is_iabt(esr) : + esr_abt_is_exec_fault(esr); VM_BUG_ON(write_fault && exec_fault); vcpu->stat.mabt_exit_stat++; @@ -2141,6 +2143,7 @@ int kvm_handle_guest_abort(struct kvm_vcpu *vcpu) .fault_ipa = fault_ipa, .memslot = memslot, .hva = hva, + .esr = esr, }; ret = user_mem_abort(&s2fd, result, fault_status, shadow_seq, walk_seq); -- 2.43.0