Re: [PATCH v2 09/15] KVM: x86/tdp_mmu: Support mirror root for TDP MMU

From: Edgecombe, Rick P
Date: Fri Jun 07 2024 - 16:27:29 EST


On Fri, 2024-06-07 at 10:46 +0200, Paolo Bonzini wrote:
> On Thu, May 30, 2024 at 11:07 PM Rick Edgecombe
> <rick.p.edgecombe@xxxxxxxxx> wrote:
> > +static inline bool kvm_on_mirror(const struct kvm *kvm, enum kvm_process
> > process)
> > +{
> > +       if (!kvm_has_mirrored_tdp(kvm))
> > +               return false;
> > +
> > +       return process & KVM_PROCESS_PRIVATE;
> > +}
> > +
> > +static inline bool kvm_on_direct(const struct kvm *kvm, enum kvm_process
> > process)
> > +{
> > +       if (!kvm_has_mirrored_tdp(kvm))
> > +               return true;
> > +
> > +       return process & KVM_PROCESS_SHARED;
> > +}
>
> These helpers don't add much, it's easier to just write
> kvm_process_to_root_types() as
>
> if (process & KVM_PROCESS_SHARED)
>   ret |= KVM_DIRECT_ROOTS;
> if (process & KVM_PROCESS_PRIVATE)
>   ret |= kvm_has_mirror_tdp(kvm) ? KVM_MIRROR_ROOTS : KVM_DIRECT_ROOTS;
>
> WARN_ON_ONCE(!ret);
> return ret;

The point of kvm_on_mirror() and kvm_on_direct() was to try to centralize TDX
specific checks in mmu.h. Whether that is a good idea aside, looking at it now
I'm not sure if it even does a good job.

I'll try it the way you suggest.

>
> (Here I chose to rename kvm_has_mirrored_tdp to kvm_has_mirror_tdp;
> but if you prefer to call it kvm_has_external_tdp, it's just as
> readable. Whatever floats your boat).
>
> >          struct kvm *kvm = vcpu->kvm;
> > -       struct kvm_mmu_page *root = root_to_sp(vcpu->arch.mmu->root.hpa);
> > +       enum kvm_tdp_mmu_root_types root_type =
> > tdp_mmu_get_fault_root_type(kvm, fault);
> > +       struct kvm_mmu_page *root = tdp_mmu_get_root(vcpu, root_type);
>
> Please make this
>
>   struct kvm_mmu_page *root = tdp_mmu_get_root_for_fault(vcpu, fault);

Yes, that seems clearer.


[snip]

>
> > +static inline enum kvm_tdp_mmu_root_types
> > tdp_mmu_get_fault_root_type(struct kvm *kvm,
> > +                                                                     struct
> > kvm_page_fault *fault)
> > +{
> > +       if (fault->is_private)
> > +               return kvm_process_to_root_types(kvm, KVM_PROCESS_PRIVATE);
> > +       return KVM_DIRECT_ROOTS;
> > +}
> > +
> > +static inline struct kvm_mmu_page *tdp_mmu_get_root(struct kvm_vcpu *vcpu,
> > enum kvm_tdp_mmu_root_types type)
> > +{
> > +       if (type == KVM_MIRROR_ROOTS)
> > +               return root_to_sp(vcpu->arch.mmu->mirror_root_hpa);
> > +       return root_to_sp(vcpu->arch.mmu->root.hpa);
> > +}
>
> static inline struct kvm_mmu_page *
> tdp_mmu_get_root_for_fault(struct kvm_vcpu *vcpu, struct kvm_page_fault
> *fault)
> {
>   hpa_t root_hpa;
>   if (unlikely(fault->is_private && kvm_has_mirror_tdp(kvm)))
>     root_hpa = vcpu->arch.mmu->mirror_root_hpa;
>   else
>     root_hpa = vcpu->arch.mmu->root.hpa;
>   return root_to_sp(root_hpa);
> }

I was not loving the amount of indirection here in the patch, but thought it
centralized the logic a bit better. This way seems good, given that the actual
logic is not that complex.