From: "Lorenzo Stoakes (ARM)" <ljs@kernel.org> mainline inclusion from mainline-v7.4-rc1 commit f1fbc7ef12caf0785fdb3ea24193dbd9c425d7b7 category: feature bugzilla: https://atomgit.com/openeuler/kernel/issues/10374 CVE: NA Reference: https://github.com/torvalds/linux/commit/f1fbc7ef12caf0785fdb3ea24193dbd9c42... -------------------------------- commit f1fbc7ef12caf0785fdb3ea24193dbd9c425d7b7 upstream 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 the mapping size via 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(). A non-NULL result also tells the abort handlers that the caller is pre-faulting rather than a vcpu, in which case -EAGAIN is propagated to the caller to allow the pre-fault to be retried. Likewise, only prepare a KVM_EXIT_MEMORY_FAULT in gmem_abort() when a real fault is being handled, as this updates vcpu state. No functional change intended. [Backport note] Adapted for OLK-6.6: - OLK-6.6 has no gmem_abort() or kvm_s2_fault_map(), so the result is threaded through user_mem_abort() only. - The new parameter is named fault_result because user_mem_abort() already has a struct kvm_s1_trans *result argument. - mapping_size is recorded from vma_pagesize, which already reflects any transparent hugepage upgrade performed in this function. Suggested-by: Vincent Donnefort <vdonnefort@google.com> 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-6-d4b0b4dfa8c3@kernel.... Signed-off-by: Marc Zyngier <maz@kernel.org> Signed-off-by: Yanan Wang <wangyanan55@huawei.com> --- arch/arm64/kvm/mmu.c | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/arch/arm64/kvm/mmu.c b/arch/arm64/kvm/mmu.c index 5279399d3120..67e76e2096bb 100644 --- a/arch/arm64/kvm/mmu.c +++ b/arch/arm64/kvm/mmu.c @@ -1606,10 +1606,15 @@ struct kvm_s2_fault_desc { struct kvm_s2_mmu *mmu; }; +struct kvm_s2_fault_result { + unsigned long mapping_size; +}; + static int user_mem_abort(const struct kvm_s2_fault_desc *s2fd, struct kvm_s1_trans *result, unsigned long fault_status, unsigned long shadow_seq, - unsigned long walk_seq) + unsigned long walk_seq, + struct kvm_s2_fault_result *fault_result) { struct kvm_vcpu *vcpu = s2fd->vcpu; phys_addr_t fault_ipa = s2fd->fault_ipa; @@ -1913,7 +1918,14 @@ static int user_mem_abort(const struct kvm_s2_fault_desc *s2fd, out_unlock: read_unlock(&kvm->mmu_lock); kvm_release_pfn_clean(pfn); - return ret != -EAGAIN ? ret : 0; + + if (ret == -EAGAIN) + return fault_result ? ret : 0; + + if (fault_result && !ret) + fault_result->mapping_size = vma_pagesize; + + return ret; } /* Resolve the access fault by making the page young again. */ @@ -2150,7 +2162,7 @@ int kvm_handle_guest_abort(struct kvm_vcpu *vcpu) .mmu = mmu, }; - ret = user_mem_abort(&s2fd, result, fault_status, shadow_seq, walk_seq); + ret = user_mem_abort(&s2fd, result, fault_status, shadow_seq, walk_seq, NULL); if (ret == 0) ret = 1; out: -- 2.43.0