diff mbox series

[v6,05/22] KVM: x86/mmu: Always pass 0 for @quadrant when gptes are 8 bytes

Message ID 20220516232138.1783324-6-dmatlack@google.com (mailing list archive)
State Handled Elsewhere
Headers show
Series KVM: Extend Eager Page Splitting to the shadow MMU | expand

Commit Message

David Matlack May 16, 2022, 11:21 p.m. UTC
The quadrant is only used when gptes are 4 bytes, but
mmu_alloc_{direct,shadow}_roots() pass in a non-zero quadrant for PAE
page directories regardless. Make this less confusing by only passing in
a non-zero quadrant when it is actually necessary.

Signed-off-by: David Matlack <dmatlack@google.com>
---
 arch/x86/kvm/mmu/mmu.c | 18 ++++++++++++++----
 1 file changed, 14 insertions(+), 4 deletions(-)

Comments

Sean Christopherson June 17, 2022, 3:20 p.m. UTC | #1
On Mon, May 16, 2022, David Matlack wrote:
> The quadrant is only used when gptes are 4 bytes, but
> mmu_alloc_{direct,shadow}_roots() pass in a non-zero quadrant for PAE
> page directories regardless. Make this less confusing by only passing in
> a non-zero quadrant when it is actually necessary.
> 
> Signed-off-by: David Matlack <dmatlack@google.com>
> ---

One nit, otherwise

Reviewed-by: Sean Christopherson <seanjc@google.com>

>  arch/x86/kvm/mmu/mmu.c | 18 ++++++++++++++----
>  1 file changed, 14 insertions(+), 4 deletions(-)
> 
> diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c
> index 515e0b33144a..8508c4bfddb5 100644
> --- a/arch/x86/kvm/mmu/mmu.c
> +++ b/arch/x86/kvm/mmu/mmu.c
> @@ -3406,9 +3406,10 @@ static hpa_t mmu_alloc_root(struct kvm_vcpu *vcpu, gfn_t gfn, int quadrant,
>  	struct kvm_mmu_page *sp;
>  
>  	role.level = level;
> +	role.quadrant = quadrant;
>  
> -	if (role.has_4_byte_gpte)
> -		role.quadrant = quadrant;
> +	WARN_ON_ONCE(quadrant && !role.has_4_byte_gpte);
> +	WARN_ON_ONCE(role.direct && role.has_4_byte_gpte);
>  
>  	sp = kvm_mmu_get_page(vcpu, gfn, role);
>  	++sp->root_count;
> @@ -3444,7 +3445,7 @@ static int mmu_alloc_direct_roots(struct kvm_vcpu *vcpu)
>  		for (i = 0; i < 4; ++i) {
>  			WARN_ON_ONCE(IS_VALID_PAE_ROOT(mmu->pae_root[i]));
>  
> -			root = mmu_alloc_root(vcpu, i << (30 - PAGE_SHIFT), i,
> +			root = mmu_alloc_root(vcpu, i << (30 - PAGE_SHIFT), 0,
>  					      PT32_ROOT_LEVEL);
>  			mmu->pae_root[i] = root | PT_PRESENT_MASK |
>  					   shadow_me_mask;
> @@ -3529,6 +3530,7 @@ static int mmu_alloc_shadow_roots(struct kvm_vcpu *vcpu)
>  	struct kvm_mmu *mmu = vcpu->arch.mmu;
>  	u64 pdptrs[4], pm_mask;
>  	gfn_t root_gfn, root_pgd;
> +	unsigned int quadrant;
>  	hpa_t root;
>  	unsigned i;

Not really your fault, but this manages to use three different type declarations
for quadrant.  i is a bare "unsigned", quadrant an "unsigned int" here, and then
@quadrant in mmu_alloc_root() is an "int".

I suspect the "unsigned i" is originated with the "i << (30 - PAGE_SHIFT)" in
mmu_alloc_direct_roots(), though even that can't create a negative value.

Given that quadrant is tiny and "int i" is a de facto standard for iterator values,
my preference would be to opportunisticaly consolidate this to

	int quadrant, i, r;

>  	int r;
> @@ -3614,7 +3616,15 @@ static int mmu_alloc_shadow_roots(struct kvm_vcpu *vcpu)
>  			root_gfn = pdptrs[i] >> PAGE_SHIFT;
>  		}
>  
> -		root = mmu_alloc_root(vcpu, root_gfn, i, PT32_ROOT_LEVEL);
> +		/*
> +		 * If shadowing 32-bit non-PAE page tables, each PAE page
> +		 * directory maps one quarter of the guest's non-PAE page
> +		 * directory. Othwerise each PAE page direct shadows one guest
> +		 * PAE page directory so that quadrant should be 0.
> +		 */
> +		quadrant = (mmu->cpu_role.base.level == PT32_ROOT_LEVEL) ? i : 0;
> +
> +		root = mmu_alloc_root(vcpu, root_gfn, quadrant, PT32_ROOT_LEVEL);
>  		mmu->pae_root[i] = root | pm_mask;
>  	}
>  
> -- 
> 2.36.0.550.gb090851708-goog
>
diff mbox series

Patch

diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c
index 515e0b33144a..8508c4bfddb5 100644
--- a/arch/x86/kvm/mmu/mmu.c
+++ b/arch/x86/kvm/mmu/mmu.c
@@ -3406,9 +3406,10 @@  static hpa_t mmu_alloc_root(struct kvm_vcpu *vcpu, gfn_t gfn, int quadrant,
 	struct kvm_mmu_page *sp;
 
 	role.level = level;
+	role.quadrant = quadrant;
 
-	if (role.has_4_byte_gpte)
-		role.quadrant = quadrant;
+	WARN_ON_ONCE(quadrant && !role.has_4_byte_gpte);
+	WARN_ON_ONCE(role.direct && role.has_4_byte_gpte);
 
 	sp = kvm_mmu_get_page(vcpu, gfn, role);
 	++sp->root_count;
@@ -3444,7 +3445,7 @@  static int mmu_alloc_direct_roots(struct kvm_vcpu *vcpu)
 		for (i = 0; i < 4; ++i) {
 			WARN_ON_ONCE(IS_VALID_PAE_ROOT(mmu->pae_root[i]));
 
-			root = mmu_alloc_root(vcpu, i << (30 - PAGE_SHIFT), i,
+			root = mmu_alloc_root(vcpu, i << (30 - PAGE_SHIFT), 0,
 					      PT32_ROOT_LEVEL);
 			mmu->pae_root[i] = root | PT_PRESENT_MASK |
 					   shadow_me_mask;
@@ -3529,6 +3530,7 @@  static int mmu_alloc_shadow_roots(struct kvm_vcpu *vcpu)
 	struct kvm_mmu *mmu = vcpu->arch.mmu;
 	u64 pdptrs[4], pm_mask;
 	gfn_t root_gfn, root_pgd;
+	unsigned int quadrant;
 	hpa_t root;
 	unsigned i;
 	int r;
@@ -3614,7 +3616,15 @@  static int mmu_alloc_shadow_roots(struct kvm_vcpu *vcpu)
 			root_gfn = pdptrs[i] >> PAGE_SHIFT;
 		}
 
-		root = mmu_alloc_root(vcpu, root_gfn, i, PT32_ROOT_LEVEL);
+		/*
+		 * If shadowing 32-bit non-PAE page tables, each PAE page
+		 * directory maps one quarter of the guest's non-PAE page
+		 * directory. Othwerise each PAE page direct shadows one guest
+		 * PAE page directory so that quadrant should be 0.
+		 */
+		quadrant = (mmu->cpu_role.base.level == PT32_ROOT_LEVEL) ? i : 0;
+
+		root = mmu_alloc_root(vcpu, root_gfn, quadrant, PT32_ROOT_LEVEL);
 		mmu->pae_root[i] = root | pm_mask;
 	}