LKML Archive on lore.kernel.org
help / color / mirror / Atom feed
* [PATCH] KVM: x86/mmu: Don't freak out if pml5_root is NULL on 4-level host
@ 2021-08-24  0:58 Sean Christopherson
  2021-09-06  9:59 ` Paolo Bonzini
  0 siblings, 1 reply; 2+ messages in thread
From: Sean Christopherson @ 2021-08-24  0:58 UTC (permalink / raw)
  To: Paolo Bonzini
  Cc: Sean Christopherson, Vitaly Kuznetsov, Wanpeng Li, Jim Mattson,
	Joerg Roedel, kvm, linux-kernel, Maxim Levitsky

Include pml5_root in the set of special roots if and only if the host,
and thus NPT, is using 5-level paging.  mmu_alloc_special_roots() expects
special roots to be allocated as a bundle, i.e. they're either all valid
or all NULL.  But for pml5_root, that expectation only holds true if the
host uses 5-level paging, which causes KVM to WARN about pml5_root being
NULL when the other special roots are valid.

The silver lining of 4-level vs. 5-level NPT being tied to the host
kernel's paging level is that KVM's shadow root level is constant; unlike
VMX's EPT, KVM can't choose 4-level NPT based on guest.MAXPHYADDR.  That
means KVM can still expect pml5_root to be bundled with the other special
roots, it just needs to be conditioned on the shadow root level.

Fixes: cb0f722aff6e ("KVM: x86/mmu: Support shadowing NPT when 5-level paging is enabled in host")
Reported-by: Maxim Levitsky <mlevitsk@redhat.com>
Reviewed-by: Maxim Levitsky <mlevitsk@redhat.com>
Signed-off-by: Sean Christopherson <seanjc@google.com>
---
 arch/x86/kvm/mmu/mmu.c | 14 +++++++++++---
 1 file changed, 11 insertions(+), 3 deletions(-)

diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c
index 4853c033e6ce..39c7b5a587df 100644
--- a/arch/x86/kvm/mmu/mmu.c
+++ b/arch/x86/kvm/mmu/mmu.c
@@ -3548,6 +3548,7 @@ static int mmu_alloc_shadow_roots(struct kvm_vcpu *vcpu)
 static int mmu_alloc_special_roots(struct kvm_vcpu *vcpu)
 {
 	struct kvm_mmu *mmu = vcpu->arch.mmu;
+	bool need_pml5 = mmu->shadow_root_level > PT64_ROOT_4LEVEL;
 	u64 *pml5_root = NULL;
 	u64 *pml4_root = NULL;
 	u64 *pae_root;
@@ -3562,7 +3563,14 @@ static int mmu_alloc_special_roots(struct kvm_vcpu *vcpu)
 	    mmu->shadow_root_level < PT64_ROOT_4LEVEL)
 		return 0;
 
-	if (mmu->pae_root && mmu->pml4_root && mmu->pml5_root)
+	/*
+	 * NPT, the only paging mode that uses this horror, uses a fixed number
+	 * of levels for the shadow page tables, e.g. all MMUs are 4-level or
+	 * all MMus are 5-level.  Thus, this can safely require that pml5_root
+	 * is allocated if the other roots are valid and pml5 is needed, as any
+	 * prior MMU would also have required pml5.
+	 */
+	if (mmu->pae_root && mmu->pml4_root && (!need_pml5 || mmu->pml5_root))
 		return 0;
 
 	/*
@@ -3570,7 +3578,7 @@ static int mmu_alloc_special_roots(struct kvm_vcpu *vcpu)
 	 * bail if KVM ends up in a state where only one of the roots is valid.
 	 */
 	if (WARN_ON_ONCE(!tdp_enabled || mmu->pae_root || mmu->pml4_root ||
-			 mmu->pml5_root))
+			 (need_pml5 && mmu->pml5_root)))
 		return -EIO;
 
 	/*
@@ -3586,7 +3594,7 @@ static int mmu_alloc_special_roots(struct kvm_vcpu *vcpu)
 	if (!pml4_root)
 		goto err_pml4;
 
-	if (mmu->shadow_root_level > PT64_ROOT_4LEVEL) {
+	if (need_pml5) {
 		pml5_root = (void *)get_zeroed_page(GFP_KERNEL_ACCOUNT);
 		if (!pml5_root)
 			goto err_pml5;
-- 
2.33.0.rc2.250.ged5fa647cd-goog


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH] KVM: x86/mmu: Don't freak out if pml5_root is NULL on 4-level host
  2021-08-24  0:58 [PATCH] KVM: x86/mmu: Don't freak out if pml5_root is NULL on 4-level host Sean Christopherson
@ 2021-09-06  9:59 ` Paolo Bonzini
  0 siblings, 0 replies; 2+ messages in thread
From: Paolo Bonzini @ 2021-09-06  9:59 UTC (permalink / raw)
  To: Sean Christopherson
  Cc: Vitaly Kuznetsov, Wanpeng Li, Jim Mattson, Joerg Roedel, kvm,
	linux-kernel, Maxim Levitsky

On 24/08/21 02:58, Sean Christopherson wrote:
> Include pml5_root in the set of special roots if and only if the host,
> and thus NPT, is using 5-level paging.  mmu_alloc_special_roots() expects
> special roots to be allocated as a bundle, i.e. they're either all valid
> or all NULL.  But for pml5_root, that expectation only holds true if the
> host uses 5-level paging, which causes KVM to WARN about pml5_root being
> NULL when the other special roots are valid.
> 
> The silver lining of 4-level vs. 5-level NPT being tied to the host
> kernel's paging level is that KVM's shadow root level is constant; unlike
> VMX's EPT, KVM can't choose 4-level NPT based on guest.MAXPHYADDR.  That
> means KVM can still expect pml5_root to be bundled with the other special
> roots, it just needs to be conditioned on the shadow root level.
> 
> Fixes: cb0f722aff6e ("KVM: x86/mmu: Support shadowing NPT when 5-level paging is enabled in host")
> Reported-by: Maxim Levitsky <mlevitsk@redhat.com>
> Reviewed-by: Maxim Levitsky <mlevitsk@redhat.com>
> Signed-off-by: Sean Christopherson <seanjc@google.com>
> ---
>   arch/x86/kvm/mmu/mmu.c | 14 +++++++++++---
>   1 file changed, 11 insertions(+), 3 deletions(-)
> 
> diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c
> index 4853c033e6ce..39c7b5a587df 100644
> --- a/arch/x86/kvm/mmu/mmu.c
> +++ b/arch/x86/kvm/mmu/mmu.c
> @@ -3548,6 +3548,7 @@ static int mmu_alloc_shadow_roots(struct kvm_vcpu *vcpu)
>   static int mmu_alloc_special_roots(struct kvm_vcpu *vcpu)
>   {
>   	struct kvm_mmu *mmu = vcpu->arch.mmu;
> +	bool need_pml5 = mmu->shadow_root_level > PT64_ROOT_4LEVEL;
>   	u64 *pml5_root = NULL;
>   	u64 *pml4_root = NULL;
>   	u64 *pae_root;
> @@ -3562,7 +3563,14 @@ static int mmu_alloc_special_roots(struct kvm_vcpu *vcpu)
>   	    mmu->shadow_root_level < PT64_ROOT_4LEVEL)
>   		return 0;
>   
> -	if (mmu->pae_root && mmu->pml4_root && mmu->pml5_root)
> +	/*
> +	 * NPT, the only paging mode that uses this horror, uses a fixed number
> +	 * of levels for the shadow page tables, e.g. all MMUs are 4-level or
> +	 * all MMus are 5-level.  Thus, this can safely require that pml5_root
> +	 * is allocated if the other roots are valid and pml5 is needed, as any
> +	 * prior MMU would also have required pml5.
> +	 */
> +	if (mmu->pae_root && mmu->pml4_root && (!need_pml5 || mmu->pml5_root))
>   		return 0;
>   
>   	/*
> @@ -3570,7 +3578,7 @@ static int mmu_alloc_special_roots(struct kvm_vcpu *vcpu)
>   	 * bail if KVM ends up in a state where only one of the roots is valid.
>   	 */
>   	if (WARN_ON_ONCE(!tdp_enabled || mmu->pae_root || mmu->pml4_root ||
> -			 mmu->pml5_root))
> +			 (need_pml5 && mmu->pml5_root)))
>   		return -EIO;
>   
>   	/*
> @@ -3586,7 +3594,7 @@ static int mmu_alloc_special_roots(struct kvm_vcpu *vcpu)
>   	if (!pml4_root)
>   		goto err_pml4;
>   
> -	if (mmu->shadow_root_level > PT64_ROOT_4LEVEL) {
> +	if (need_pml5) {
>   		pml5_root = (void *)get_zeroed_page(GFP_KERNEL_ACCOUNT);
>   		if (!pml5_root)
>   			goto err_pml5;
> 

Queued, thanks.

Paolo


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2021-09-06  9:59 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-08-24  0:58 [PATCH] KVM: x86/mmu: Don't freak out if pml5_root is NULL on 4-level host Sean Christopherson
2021-09-06  9:59 ` Paolo Bonzini

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).