All of lore.kernel.org
 help / color / mirror / Atom feed
* Subject: [RFC PATCH] kvm/x86: Fix 'lpages' kvm stat for TDM MMU
@ 2021-04-29 12:46 Shahin, Md Shahadat Hossain
  2021-04-29 15:39 ` Paolo Bonzini
  2021-04-29 16:49 ` Sean Christopherson
  0 siblings, 2 replies; 5+ messages in thread
From: Shahin, Md Shahadat Hossain @ 2021-04-29 12:46 UTC (permalink / raw)
  To: kvm; +Cc: Szczepanek, Bartosz, pbonzini, seanjc, bgardon

Large pages not being created properly may result in increased memory
access time. The 'lpages' kvm stat used to keep track of the current
number of large pages in the system, but with TDP MMU enabled the stat
is not showing the correct number.

This patch extends the lpages counter to cover the TDP case.

Signed-off-by: Md Shahadat Hossain Shahin <shahinmd@amazon.de>
Cc: Bartosz Szczepanek <bsz@amazon.de>
---
 arch/x86/kvm/mmu/tdp_mmu.c | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/arch/x86/kvm/mmu/tdp_mmu.c b/arch/x86/kvm/mmu/tdp_mmu.c
index 34207b874886..1e2a3cb33568 100644
--- a/arch/x86/kvm/mmu/tdp_mmu.c
+++ b/arch/x86/kvm/mmu/tdp_mmu.c
@@ -425,6 +425,12 @@ static void __handle_changed_spte(struct kvm *kvm, int as_id, gfn_t gfn,
 
 	if (old_spte == new_spte)
 		return;
+	
+	if (is_large_pte(old_spte))
+		--kvm->stat.lpages;
+	
+	if (is_large_pte(new_spte))
+		++kvm->stat.lpages;
 
 	trace_kvm_tdp_mmu_spte_changed(as_id, gfn, level, old_spte, new_spte);
 
-- 
2.17.1



Amazon Development Center Germany GmbH
Krausenstr. 38
10117 Berlin
Geschaeftsfuehrung: Christian Schlaeger, Jonathan Weiss
Eingetragen am Amtsgericht Charlottenburg unter HRB 149173 B
Sitz: Berlin
Ust-ID: DE 289 237 879




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

* Re: Subject: [RFC PATCH] kvm/x86: Fix 'lpages' kvm stat for TDM MMU
  2021-04-29 12:46 Subject: [RFC PATCH] kvm/x86: Fix 'lpages' kvm stat for TDM MMU Shahin, Md Shahadat Hossain
@ 2021-04-29 15:39 ` Paolo Bonzini
  2021-04-29 16:49 ` Sean Christopherson
  1 sibling, 0 replies; 5+ messages in thread
From: Paolo Bonzini @ 2021-04-29 15:39 UTC (permalink / raw)
  To: Shahin, Md Shahadat Hossain, kvm; +Cc: Szczepanek, Bartosz, seanjc, bgardon

On 29/04/21 14:46, Shahin, Md Shahadat Hossain wrote:
> Large pages not being created properly may result in increased memory
> access time. The 'lpages' kvm stat used to keep track of the current
> number of large pages in the system, but with TDP MMU enabled the stat
> is not showing the correct number.
> 
> This patch extends the lpages counter to cover the TDP case.
> 
> Signed-off-by: Md Shahadat Hossain Shahin <shahinmd@amazon.de>
> Cc: Bartosz Szczepanek <bsz@amazon.de>
> ---
>   arch/x86/kvm/mmu/tdp_mmu.c | 6 ++++++
>   1 file changed, 6 insertions(+)
> 
> diff --git a/arch/x86/kvm/mmu/tdp_mmu.c b/arch/x86/kvm/mmu/tdp_mmu.c
> index 34207b874886..1e2a3cb33568 100644
> --- a/arch/x86/kvm/mmu/tdp_mmu.c
> +++ b/arch/x86/kvm/mmu/tdp_mmu.c
> @@ -425,6 +425,12 @@ static void __handle_changed_spte(struct kvm *kvm, int as_id, gfn_t gfn,
>   
>   	if (old_spte == new_spte)
>   		return;
> +	
> +	if (is_large_pte(old_spte))
> +		--kvm->stat.lpages;
> +	
> +	if (is_large_pte(new_spte))
> +		++kvm->stat.lpages;
>   
>   	trace_kvm_tdp_mmu_spte_changed(as_id, gfn, level, old_spte, new_spte);
>   
> 

Queued, thanks.

Paolo


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

* Re: Subject: [RFC PATCH] kvm/x86: Fix 'lpages' kvm stat for TDM MMU
  2021-04-29 12:46 Subject: [RFC PATCH] kvm/x86: Fix 'lpages' kvm stat for TDM MMU Shahin, Md Shahadat Hossain
  2021-04-29 15:39 ` Paolo Bonzini
@ 2021-04-29 16:49 ` Sean Christopherson
  2021-04-29 17:04   ` Paolo Bonzini
  1 sibling, 1 reply; 5+ messages in thread
From: Sean Christopherson @ 2021-04-29 16:49 UTC (permalink / raw)
  To: Shahin, Md Shahadat Hossain; +Cc: kvm, Szczepanek, Bartosz, pbonzini, bgardon

On Thu, Apr 29, 2021, Shahin, Md Shahadat Hossain wrote:
> Large pages not being created properly may result in increased memory
> access time. The 'lpages' kvm stat used to keep track of the current
> number of large pages in the system, but with TDP MMU enabled the stat
> is not showing the correct number.
> 
> This patch extends the lpages counter to cover the TDP case.
> 
> Signed-off-by: Md Shahadat Hossain Shahin <shahinmd@amazon.de>
> Cc: Bartosz Szczepanek <bsz@amazon.de>
> ---
>  arch/x86/kvm/mmu/tdp_mmu.c | 6 ++++++
>  1 file changed, 6 insertions(+)
> 
> diff --git a/arch/x86/kvm/mmu/tdp_mmu.c b/arch/x86/kvm/mmu/tdp_mmu.c
> index 34207b874886..1e2a3cb33568 100644
> --- a/arch/x86/kvm/mmu/tdp_mmu.c
> +++ b/arch/x86/kvm/mmu/tdp_mmu.c
> @@ -425,6 +425,12 @@ static void __handle_changed_spte(struct kvm *kvm, int as_id, gfn_t gfn,
>  
>  	if (old_spte == new_spte)
>  		return;
> +	
> +	if (is_large_pte(old_spte))
> +		--kvm->stat.lpages;
> +	
> +	if (is_large_pte(new_spte))
> +		++kvm->stat.lpages;

Hrm, kvm->stat.lpages could get corrupted when __handle_changed_spte() is called
under read lock, e.g. if multiple vCPUs are faulting in memory.

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

* Re: Subject: [RFC PATCH] kvm/x86: Fix 'lpages' kvm stat for TDM MMU
  2021-04-29 16:49 ` Sean Christopherson
@ 2021-04-29 17:04   ` Paolo Bonzini
  2021-04-29 17:25     ` Ben Gardon
  0 siblings, 1 reply; 5+ messages in thread
From: Paolo Bonzini @ 2021-04-29 17:04 UTC (permalink / raw)
  To: Sean Christopherson, Shahin, Md Shahadat Hossain
  Cc: kvm, Szczepanek, Bartosz, bgardon

On 29/04/21 18:49, Sean Christopherson wrote:
> On Thu, Apr 29, 2021, Shahin, Md Shahadat Hossain wrote:
>> Large pages not being created properly may result in increased memory
>> access time. The 'lpages' kvm stat used to keep track of the current
>> number of large pages in the system, but with TDP MMU enabled the stat
>> is not showing the correct number.
>>
>> This patch extends the lpages counter to cover the TDP case.
>>
>> Signed-off-by: Md Shahadat Hossain Shahin <shahinmd@amazon.de>
>> Cc: Bartosz Szczepanek <bsz@amazon.de>
>> ---
>>   arch/x86/kvm/mmu/tdp_mmu.c | 6 ++++++
>>   1 file changed, 6 insertions(+)
>>
>> diff --git a/arch/x86/kvm/mmu/tdp_mmu.c b/arch/x86/kvm/mmu/tdp_mmu.c
>> index 34207b874886..1e2a3cb33568 100644
>> --- a/arch/x86/kvm/mmu/tdp_mmu.c
>> +++ b/arch/x86/kvm/mmu/tdp_mmu.c
>> @@ -425,6 +425,12 @@ static void __handle_changed_spte(struct kvm *kvm, int as_id, gfn_t gfn,
>>   
>>   	if (old_spte == new_spte)
>>   		return;
>> +	
>> +	if (is_large_pte(old_spte))
>> +		--kvm->stat.lpages;
>> +	
>> +	if (is_large_pte(new_spte))
>> +		++kvm->stat.lpages;
> 
> Hrm, kvm->stat.lpages could get corrupted when __handle_changed_spte() is called
> under read lock, e.g. if multiple vCPUs are faulting in memory.

Ouch, indeed!

One way to fix it without needing an atomic operation is to make it a 
per-vcpu stat.  It would be a bit weird for the binary stats because we 
would have to hide this one from the vCPU statistics file descriptor 
(and only aggregate it in the VM statistics).

Alternatively, you can do the atomic_add only if is_large_pte(old_spte) 
!= is_large_pte(new_spte), casting &kvm->stat.lpages to an atomic64_t*.

Paolo


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

* Re: Subject: [RFC PATCH] kvm/x86: Fix 'lpages' kvm stat for TDM MMU
  2021-04-29 17:04   ` Paolo Bonzini
@ 2021-04-29 17:25     ` Ben Gardon
  0 siblings, 0 replies; 5+ messages in thread
From: Ben Gardon @ 2021-04-29 17:25 UTC (permalink / raw)
  To: Paolo Bonzini
  Cc: Sean Christopherson, Shahin, Md Shahadat Hossain, kvm,
	Szczepanek, Bartosz

On Thu, Apr 29, 2021 at 10:04 AM Paolo Bonzini <pbonzini@redhat.com> wrote:
>
> On 29/04/21 18:49, Sean Christopherson wrote:
> > On Thu, Apr 29, 2021, Shahin, Md Shahadat Hossain wrote:
> >> Large pages not being created properly may result in increased memory
> >> access time. The 'lpages' kvm stat used to keep track of the current
> >> number of large pages in the system, but with TDP MMU enabled the stat
> >> is not showing the correct number.
> >>
> >> This patch extends the lpages counter to cover the TDP case.
> >>
> >> Signed-off-by: Md Shahadat Hossain Shahin <shahinmd@amazon.de>
> >> Cc: Bartosz Szczepanek <bsz@amazon.de>
> >> ---
> >>   arch/x86/kvm/mmu/tdp_mmu.c | 6 ++++++
> >>   1 file changed, 6 insertions(+)
> >>
> >> diff --git a/arch/x86/kvm/mmu/tdp_mmu.c b/arch/x86/kvm/mmu/tdp_mmu.c
> >> index 34207b874886..1e2a3cb33568 100644
> >> --- a/arch/x86/kvm/mmu/tdp_mmu.c
> >> +++ b/arch/x86/kvm/mmu/tdp_mmu.c
> >> @@ -425,6 +425,12 @@ static void __handle_changed_spte(struct kvm *kvm, int as_id, gfn_t gfn,
> >>
> >>      if (old_spte == new_spte)
> >>              return;
> >> +
> >> +    if (is_large_pte(old_spte))
> >> +            --kvm->stat.lpages;
> >> +
> >> +    if (is_large_pte(new_spte))
> >> +            ++kvm->stat.lpages;
> >
> > Hrm, kvm->stat.lpages could get corrupted when __handle_changed_spte() is called
> > under read lock, e.g. if multiple vCPUs are faulting in memory.
>
> Ouch, indeed!
>
> One way to fix it without needing an atomic operation is to make it a
> per-vcpu stat.  It would be a bit weird for the binary stats because we
> would have to hide this one from the vCPU statistics file descriptor
> (and only aggregate it in the VM statistics).
>
> Alternatively, you can do the atomic_add only if is_large_pte(old_spte)
> != is_large_pte(new_spte), casting &kvm->stat.lpages to an atomic64_t*.

I forgot that the lpages stat existed upstream. Internally at Google
we also maintain a count for each mapping level and just update it
atomically. The per-vCPU stat approach would work too, but I doubt
it's worth the complexity unless we have other use cases for a
similarly aggregated stat.

>
> Paolo
>

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

end of thread, other threads:[~2021-04-29 17:25 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-04-29 12:46 Subject: [RFC PATCH] kvm/x86: Fix 'lpages' kvm stat for TDM MMU Shahin, Md Shahadat Hossain
2021-04-29 15:39 ` Paolo Bonzini
2021-04-29 16:49 ` Sean Christopherson
2021-04-29 17:04   ` Paolo Bonzini
2021-04-29 17:25     ` Ben Gardon

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.