Re: [RFC PATCH 00/10] KVM: x86/mmu: simplify argument to kvm page fault handler

From: Sean Christopherson
Date: Wed May 26 2021 - 17:11:08 EST


On Tue, Apr 20, 2021, Isaku Yamahata wrote:
> This is a preliminary clean up for TDX which complicates KVM page fault
> execution path.

Ooh, a series to complicate the page fault path! ;-)

Grammatical snarkiness aside, I'm all in favor of adding a struct to collect the
page fault collateral. Overarching feedback:

- Have kvm_mmu_do_page_fault() handle initialization of the struct. That
will allow making most of the fields const, and will avoid the rather painful
kvm_page_fault_init().

- Pass @vcpu separately. Yes, it's associated with the fault, but literally
the first line in every consumer is "struct kvm_vcpu *vcpu = kpf->vcpu;".

- Use "fault" instead of "kpf", mostly because it reads better for people that
aren't intimately familiar with the code, but also to avoid having to refactor
a huge amount of code if we decide to rename kvm_page_fault, e.g. if we decide
to use that name to return fault information to userspace.

- Snapshot anything that is computed in multiple places, even if it is
derivative of existing info. E.g. it probably makes sense to grab
write/fetch (or exec).


E.g. I'm thinking something like

struct kvm_page_fault {
const gpa_t cr2_or_gpa;
const u32 error_code;
const bool write;
const bool read;
const bool fetch;
const bool prefault;
const bool is_tdp;

gfn_t gfn;
hva_t hva;
int max_level;

kvm_pfn_t pfn;
bool map_writable;
};

int kvm_tdp_page_fault(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault);

static inline int kvm_mmu_do_page_fault(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa,
u32 err, bool prefault)
{
struct kvm_page_fault fault = {
.cr2_or_gpa = cr2_or_gpa,
.error_code = err,
.write = err & PFERR_WRITE_MASK,
.fetch = err & PFERR_FETCH_MASK,
.perm = ...
.rsvd = err & PFERR_RSVD_MASK,

.is_tdp = vcpu->arch.mmu->page_fault == kvm_tdp_page_fault,

...
};

#ifdef CONFIG_RETPOLINE
if (likely(fault.is_tdp))
return kvm_tdp_page_fault(vcpu, &fault);
#endif
return vcpu->arch.mmu->page_fault(vcpu, &fault);
}