Re: [PATCH v2 4/5] KVM: x86: Consolidate nVMX vs. nSVM logic for loading CR3 on nested xfer

From: Yosry Ahmed

Date: Wed Nov 25 2026 - 16:41:06 EST


On Tue, Nov 17, 2026 at 1:09 PM Sean Christopherson <seanjc@xxxxxxxxxx> wrote:
>
> Dedup nested_{svm,vmx}_load_cr3() into kvm_nested_load_cr3(), and drop
> several MMU exports that existed purely to support the related vendor code.
> Except for slight differences around PDPTRs, the logic and reasoning for
> handling CR3 and PDPTRs on nested transitions is identical between Intel
> and AMD.
>
> Add a KVM_BUG_ON() in nested_vmx_load_cr3() to ensure that the new helper
> doesn't unexpectedly fail when PDPTRs aren't being reloaded.
>
> No functional change intended (the new KVM_BUG_ON() is unreachable).
>
> Cc: stable@xxxxxxxxxxxxxxx
> Signed-off-by: Sean Christopherson <seanjc@xxxxxxxxxx>

Code looks good, a couple of nits below:

Reviewed-by: Yosry Ahmed <yosry@xxxxxxxxxx>

[..]
> -/*
> - * Load guest's/host's cr3 on nested vmentry or vmexit. @nested_npt is true
> - * if we are emulating VM-Entry into a guest with NPT enabled.
> - */
> static int nested_svm_load_cr3(struct kvm_vcpu *vcpu, unsigned long cr3,
> bool nested_npt, bool reload_pdptrs)
> {
> - if (reload_pdptrs && is_pae_paging(vcpu)) {
> - if (nested_npt)
> - kvm_register_mark_for_reload(vcpu, VCPU_REG_PDPTR);
> - else if (CC(!load_pdptrs(vcpu, cr3)))
> - return -EINVAL;
> - }
> + if (reload_pdptrs && is_pae_paging(vcpu) && nested_npt)

Check nested_npt first like kvm_nested_load_cr3()?

> + kvm_register_mark_for_reload(vcpu, VCPU_REG_PDPTR);
>
> - vcpu->arch.cr3 = cr3;
> - kvm_register_mark_dirty(vcpu, VCPU_REG_CR3);
> -
> - /* Re-initialize the MMU, e.g. to pick up CR4 MMU role changes. */
> - kvm_init_mmu(vcpu);
> -
> - if (!nested_npt)
> - kvm_mmu_new_pgd(vcpu, cr3);
> -
> - return 0;
> + return kvm_nested_load_cr3(vcpu, cr3, nested_npt, reload_pdptrs);

We dropped CC() here. Do we also want KVM_BUG_ON(!reload_pdptrs) here?