Re: [PATCH 5/6] KVM: x86: Implement kvm_arch_vcpu_pre_fault_memory()

From: Sean Christopherson
Date: Wed Jun 12 2024 - 17:04:39 EST


On Mon, Apr 22, 2024, Xiaoyao Li wrote:
> On 4/19/2024 4:59 PM, Paolo Bonzini wrote:
> > diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c
> > index 10e90788b263..a045b23964c0 100644
> > --- a/arch/x86/kvm/mmu/mmu.c
> > +++ b/arch/x86/kvm/mmu/mmu.c
> > @@ -4647,6 +4647,78 @@ int kvm_tdp_page_fault(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault)
> > return direct_page_fault(vcpu, fault);
> > }
> > +static int kvm_tdp_map_page(struct kvm_vcpu *vcpu, gpa_t gpa, u64 error_code,
> > + u8 *level)

Align parameters:

static int kvm_tdp_map_page(struct kvm_vcpu *vcpu, gpa_t gpa, u64 error_code,
u8 *level)

> > +{
> > + int r;
> > +
> > + /* Restrict to TDP page fault. */

This is fairly obvious from the code, what might not be obvious is _why_. I'm
also ok dropping the comment entirely, but it's easy enough to provide a hint to
the reader.

> > + if (vcpu->arch.mmu->page_fault != kvm_tdp_page_fault)
> > + return -EOPNOTSUPP;
> > +
> > +retry:
> > + r = __kvm_mmu_do_page_fault(vcpu, gpa, error_code, true, NULL, level);
> > + if (r < 0)
> > + return r;
> > +
> > + switch (r) {
> > + case RET_PF_RETRY:
> > + if (signal_pending(current))
> > + return -EINTR;
> > + cond_resched();
> > + goto retry;

Rather than a goto+retry from inside a switch statement, what about:

int r;

/*
* Pre-faulting a GPA is supported only non-nested TDP, as indirect
* MMUs map either GVAs or L2 GPAs, not L1 GPAs.
*/
if (vcpu->arch.mmu->page_fault != kvm_tdp_page_fault)
return -EOPNOTSUPP;

do {
if (signal_pending(current))
return -EINTR;

cond_resched();

r = kvm_mmu_do_page_fault(vcpu, gpa, error_code, true, NULL, level);
} while (r == RET_PF_RETRY);

switch (r) {
case RET_PF_FIXED:
case RET_PF_SPURIOUS:
break;

case RET_PF_EMULATE:
return -ENOENT;

case RET_PF_CONTINUE:
case RET_PF_INVALID:
case RET_PF_RETRY:
default:
WARN_ON_ONCE(r >= 0);
return -EIO;
}

return 0;