Re: [PATCH] KVM: x86/mmu: Embed direct bits into gpa for KVM_PRE_FAULT_MEMORY

From: Yan Zhao
Date: Thu Jun 12 2025 - 03:22:23 EST


On Thu, Jun 12, 2025 at 05:16:05AM +0800, Edgecombe, Rick P wrote:
> On Wed, 2025-06-11 at 13:43 -0700, Sean Christopherson wrote:
> > > Functionally, page_fault_can_be_fast() should prevented this with the
> > > check of
> > > kvm->arch.has_private_mem.
> >
> > No?  I see this:
> >
> > if (kvm->arch.has_private_mem &&
> >     fault->is_private != kvm_mem_is_private(kvm, fault->gfn))
> > return false;
> >
> > I.e. a private fault can be fast, so long as the page is already in the
> > correct
> > shared vs. private state.  I can imagine that it's impossible for TDX to
> > generate
> > protection violations, but I think kvm_tdp_mmu_fast_pf_get_last_sptep() could
> > be
> > reached with a mirror root if kvm_ad_enabled=false.
> >
> > if (!fault->present)
> > return !kvm_ad_enabled;
> >
> > /*
> > * Note, instruction fetches and writes are mutually exclusive,
> > ignore
> > * the "exec" flag.
> > */
> > return fault->write;
>
> Oh, how embarrassing. Yes, I misread the code, but the way it's working is, oh
> man...
>
> TDX isn't setting PFERR_WRITE_MASK or PFERR_PRESENT_MASK in the error_code
> passed into the fault handler. So page_fault_can_be_fast() should return false
> for that reason for private/mirror faults.
Hmm, TDX does set PFERR_WRITE_MASK in the error_code when fault->prefetch is
false (since exit_qual is set to EPT_VIOLATION_ACC_WRITE in
tdx_handle_ept_violation()).

PFERR_PRESENT_MASK is always unset.

page_fault_can_be_fast() does always return false for private mirror faults
though, due to the reason in
https://lore.kernel.org/kvm/aEp6pDQgbjsfrg2h@xxxxxxxxxxxxxxxxxxxxxxxxx :)