[RFC PATCH 4/8] KVM: x86/mmu: Factor out kvm_mmu_do_page_fault()

From: isaku . yamahata
Date: Fri Mar 01 2024 - 12:31:01 EST


From: Isaku Yamahata <isaku.yamahata@xxxxxxxxx>

For ioctl to pre-populate guest memory, factor out kvm_mmu_do_page_fault()
into initialization function of struct kvm_page_fault, calling fault hander,
and the surrounding logic of error check and stats update part. This
enables to implement a wrapper to call fault handler.

No functional change intended.

Signed-off-by: Isaku Yamahata <isaku.yamahata@xxxxxxxxx>
---
arch/x86/kvm/mmu/mmu_internal.h | 28 +++++++++++++++++++---------
1 file changed, 19 insertions(+), 9 deletions(-)

diff --git a/arch/x86/kvm/mmu/mmu_internal.h b/arch/x86/kvm/mmu/mmu_internal.h
index 72ef09fc9322..aac52f0fdf54 100644
--- a/arch/x86/kvm/mmu/mmu_internal.h
+++ b/arch/x86/kvm/mmu/mmu_internal.h
@@ -302,6 +302,24 @@ enum {
.pfn = KVM_PFN_ERR_FAULT, \
.hva = KVM_HVA_ERR_BAD, }

+static inline int __kvm_mmu_do_page_fault(struct kvm_vcpu *vcpu,
+ struct kvm_page_fault *fault)
+{
+ int r;
+
+ if (vcpu->arch.mmu->root_role.direct) {
+ fault->gfn = fault->addr >> PAGE_SHIFT;
+ fault->slot = kvm_vcpu_gfn_to_memslot(vcpu, fault->gfn);
+ }
+
+ if (IS_ENABLED(CONFIG_RETPOLINE) && fault->is_tdp)
+ r = kvm_tdp_page_fault(vcpu, fault);
+ else
+ r = vcpu->arch.mmu->page_fault(vcpu, fault);
+
+ return r;
+}
+
static inline int kvm_mmu_do_page_fault(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa,
u32 err, bool prefetch, int *emulation_type)
{
@@ -310,11 +328,6 @@ static inline int kvm_mmu_do_page_fault(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa,
KVM_MAX_HUGEPAGE_LEVEL);
int r;

- if (vcpu->arch.mmu->root_role.direct) {
- fault.gfn = fault.addr >> PAGE_SHIFT;
- fault.slot = kvm_vcpu_gfn_to_memslot(vcpu, fault.gfn);
- }
-
/*
* Async #PF "faults", a.k.a. prefetch faults, are not faults from the
* guest perspective and have already been counted at the time of the
@@ -323,10 +336,7 @@ static inline int kvm_mmu_do_page_fault(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa,
if (!prefetch)
vcpu->stat.pf_taken++;

- if (IS_ENABLED(CONFIG_RETPOLINE) && fault.is_tdp)
- r = kvm_tdp_page_fault(vcpu, &fault);
- else
- r = vcpu->arch.mmu->page_fault(vcpu, &fault);
+ r = __kvm_mmu_do_page_fault(vcpu, &fault);

if (fault.write_fault_to_shadow_pgtable && emulation_type)
*emulation_type |= EMULTYPE_WRITE_PF_TO_SP;
--
2.25.1