Re: [PATCH 03/23] KVM: MMU: remove valid from extended role

Sean Christopherson
2022-02-09T22:55:21Z

On Fri, Feb 04, 2022, Paolo Bonzini wrote:
> The level field of the MMU role can act as a marker for validity
> instead: it is guaranteed to be nonzero so a zero value means the role
> is invalid and the MMU properties will be computed again.

Nope, it's not guaranteed to be non-zero:

static int role_regs_to_root_level(struct kvm_mmu_role_regs *regs)
{
if (!____is_cr0_pg(regs))
return 0; <=============================================
else if (____is_efer_lma(regs))
return ____is_cr4_la57(regs) ? PT64_ROOT_5LEVEL :
PT64_ROOT_4LEVEL;
else if (____is_cr4_pae(regs))
return PT32E_ROOT_LEVEL;
else
return PT32_ROOT_LEVEL;
}

static union kvm_mmu_role
kvm_calc_nested_mmu_role(struct kvm_vcpu *vcpu, struct kvm_mmu_role_regs *regs)
{
union kvm_mmu_role role;

role = kvm_calc_shadow_root_page_role_common(vcpu, regs, false);

/*
* Nested MMUs are used only for walking L2's gva->gpa, they never have
* shadow pages of their own and so "direct" has no meaning. Set it
* to "true" to try to detect bogus usage of the nested MMU.
*/
role.base.direct = true;
role.base.level = role_regs_to_root_level(regs);
return role;
}

static void init_kvm_nested_mmu(struct kvm_vcpu *vcpu)
{
struct kvm_mmu_role_regs regs = vcpu_to_role_regs(vcpu);
union kvm_mmu_role new_role = kvm_calc_nested_mmu_role(vcpu, &regs);
struct kvm_mmu *g_context = &vcpu->arch.nested_mmu;

if (new_role.as_u64 == g_context->mmu_role.as_u64) <== theoretically can get a false positive
return;