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

From: Paolo Bonzini
Date: Fri Jun 07 2024 - 04:47:07 EST


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;

(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);

Most other uses of tdp_mmu_get_root() don't really need a root_type,
I'll get to them in the reviews for the rest of the series.

> enum kvm_tdp_mmu_root_types {
> KVM_VALID_ROOTS = BIT(0),
> + KVM_DIRECT_ROOTS = BIT(1),
> + KVM_MIRROR_ROOTS = BIT(2),
>
> - KVM_ANY_ROOTS = 0,
> - KVM_ANY_VALID_ROOTS = KVM_VALID_ROOTS,
> + KVM_ANY_ROOTS = KVM_DIRECT_ROOTS | KVM_MIRROR_ROOTS,
> + KVM_ANY_VALID_ROOTS = KVM_DIRECT_ROOTS | KVM_MIRROR_ROOTS | KVM_VALID_ROOTS,

See previous review.

> +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);
}

Paolo