Re: [PATCH v13 057/113] KVM: TDX: MTRR: implement get_mt_mask() for TDX

From: Huang, Kai
Date: Wed Mar 29 2023 - 21:36:14 EST


On Wed, 2023-03-29 at 18:15 -0700, Isaku Yamahata wrote:
> On Mon, Mar 27, 2023 at 09:54:40AM +0000,
> "Huang, Kai" <kai.huang@xxxxxxxxx> wrote:
>
> > > diff -u b/arch/x86/kvm/vmx/tdx.c b/arch/x86/kvm/vmx/tdx.c
> > > --- b/arch/x86/kvm/vmx/tdx.c
> > > +++ b/arch/x86/kvm/vmx/tdx.c
> > > @@ -347,6 +347,25 @@
> > > return 0;
> > > }
> > >
> > > +u8 tdx_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
> > > +{
> > > + /* TDX private GPA is always WB. */
> > > + if (!(gfn & kvm_gfn_shared_mask(vcpu->kvm))) {
> >
> > Are you still passing a "raw" GFN? Could you explain why you choose this way?
> >
> > > + /* MMIO is only for shared GPA. */
> > > + WARN_ON_ONCE(is_mmio);
> > > + return MTRR_TYPE_WRBACK << VMX_EPT_MT_EPTE_SHIFT;
> >
> > I guess it's better to include VMX_EPT_IPAT_BIT bit.
>
> On second thought, there is no need to check it. We can simply drop this check.
>
> u8 tdx_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
> {
> if (is_mmio)
> return MTRR_TYPE_UNCACHABLE << VMX_EPT_MT_EPTE_SHIFT;
>
> if (!kvm_arch_has_noncoherent_dma(vcpu->kvm))
> return (MTRR_TYPE_WRBACK << VMX_EPT_MT_EPTE_SHIFT) | VMX_EPT_IPAT_BIT;
>
> /* TDX enforces CR0.CD = 0 and KVM MTRR emulation enforces writeback. */
> return MTRR_TYPE_WRBACK << VMX_EPT_MT_EPTE_SHIFT;
> }
>
>

I think for private page, _theoretically_, you should include VMX_EPT_IPAT_BIT
(because the TDX module spec says the "access semantics for private is WB", but
not "private is _mapped_ as WB"). But in practice this doesn't matter because
the mirrored-EPT is never used by hardware.

While for shared page, when guest has non-coherent DMA, IIUC your intention is
you still want to horner guest's PAT, so you omitted the VMX_EPT_IPAT_BIT.