kvm.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] KVM: X86: Force ASYNC_PF_PER_VCPU to be power of two
@ 2020-04-16 15:58 Peter Xu
  2020-05-04 16:57 ` Paolo Bonzini
  0 siblings, 1 reply; 2+ messages in thread
From: Peter Xu @ 2020-04-16 15:58 UTC (permalink / raw)
  To: kvm; +Cc: Paolo Bonzini, Sean Christopherson, Vitaly Kuznetsov, peterx

Forcing the ASYNC_PF_PER_VCPU to be power of two is much easier to be
used rather than calling roundup_pow_of_two() from time to time.  Do
this by adding a BUILD_BUG_ON() inside the hash function.

Another point is that generally async pf does not allow concurrency
over ASYNC_PF_PER_VCPU after all (see kvm_setup_async_pf()), so it
does not make much sense either to have it not a power of two or some
of the entries will definitely be wasted.

Signed-off-by: Peter Xu <peterx@redhat.com>
---
 arch/x86/include/asm/kvm_host.h | 2 +-
 arch/x86/kvm/x86.c              | 8 +++++---
 2 files changed, 6 insertions(+), 4 deletions(-)

diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h
index 42a2d0d3984a..9f0fdaacdfa5 100644
--- a/arch/x86/include/asm/kvm_host.h
+++ b/arch/x86/include/asm/kvm_host.h
@@ -761,7 +761,7 @@ struct kvm_vcpu_arch {
 
 	struct {
 		bool halted;
-		gfn_t gfns[roundup_pow_of_two(ASYNC_PF_PER_VCPU)];
+		gfn_t gfns[ASYNC_PF_PER_VCPU];
 		struct gfn_to_hva_cache data;
 		u64 msr_val;
 		u32 id;
diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
index b8124b562dea..fc74dafa72ff 100644
--- a/arch/x86/kvm/x86.c
+++ b/arch/x86/kvm/x86.c
@@ -261,7 +261,7 @@ static int emulator_fix_hypercall(struct x86_emulate_ctxt *ctxt);
 static inline void kvm_async_pf_hash_reset(struct kvm_vcpu *vcpu)
 {
 	int i;
-	for (i = 0; i < roundup_pow_of_two(ASYNC_PF_PER_VCPU); i++)
+	for (i = 0; i < ASYNC_PF_PER_VCPU; i++)
 		vcpu->arch.apf.gfns[i] = ~0;
 }
 
@@ -10265,12 +10265,14 @@ void kvm_arch_async_page_ready(struct kvm_vcpu *vcpu, struct kvm_async_pf *work)
 
 static inline u32 kvm_async_pf_hash_fn(gfn_t gfn)
 {
+	BUILD_BUG_ON(!is_power_of_2(ASYNC_PF_PER_VCPU));
+
 	return hash_32(gfn & 0xffffffff, order_base_2(ASYNC_PF_PER_VCPU));
 }
 
 static inline u32 kvm_async_pf_next_probe(u32 key)
 {
-	return (key + 1) & (roundup_pow_of_two(ASYNC_PF_PER_VCPU) - 1);
+	return (key + 1) & (ASYNC_PF_PER_VCPU - 1);
 }
 
 static void kvm_add_async_pf_gfn(struct kvm_vcpu *vcpu, gfn_t gfn)
@@ -10288,7 +10290,7 @@ static u32 kvm_async_pf_gfn_slot(struct kvm_vcpu *vcpu, gfn_t gfn)
 	int i;
 	u32 key = kvm_async_pf_hash_fn(gfn);
 
-	for (i = 0; i < roundup_pow_of_two(ASYNC_PF_PER_VCPU) &&
+	for (i = 0; i < ASYNC_PF_PER_VCPU &&
 		     (vcpu->arch.apf.gfns[key] != gfn &&
 		      vcpu->arch.apf.gfns[key] != ~0); i++)
 		key = kvm_async_pf_next_probe(key);
-- 
2.24.1


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

* Re: [PATCH] KVM: X86: Force ASYNC_PF_PER_VCPU to be power of two
  2020-04-16 15:58 [PATCH] KVM: X86: Force ASYNC_PF_PER_VCPU to be power of two Peter Xu
@ 2020-05-04 16:57 ` Paolo Bonzini
  0 siblings, 0 replies; 2+ messages in thread
From: Paolo Bonzini @ 2020-05-04 16:57 UTC (permalink / raw)
  To: Peter Xu, kvm; +Cc: Sean Christopherson, Vitaly Kuznetsov

On 16/04/20 17:58, Peter Xu wrote:
> Forcing the ASYNC_PF_PER_VCPU to be power of two is much easier to be
> used rather than calling roundup_pow_of_two() from time to time.  Do
> this by adding a BUILD_BUG_ON() inside the hash function.
> 
> Another point is that generally async pf does not allow concurrency
> over ASYNC_PF_PER_VCPU after all (see kvm_setup_async_pf()), so it
> does not make much sense either to have it not a power of two or some
> of the entries will definitely be wasted.
> 
> Signed-off-by: Peter Xu <peterx@redhat.com>
> ---
>  arch/x86/include/asm/kvm_host.h | 2 +-
>  arch/x86/kvm/x86.c              | 8 +++++---
>  2 files changed, 6 insertions(+), 4 deletions(-)
> 
> diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h
> index 42a2d0d3984a..9f0fdaacdfa5 100644
> --- a/arch/x86/include/asm/kvm_host.h
> +++ b/arch/x86/include/asm/kvm_host.h
> @@ -761,7 +761,7 @@ struct kvm_vcpu_arch {
>  
>  	struct {
>  		bool halted;
> -		gfn_t gfns[roundup_pow_of_two(ASYNC_PF_PER_VCPU)];
> +		gfn_t gfns[ASYNC_PF_PER_VCPU];
>  		struct gfn_to_hva_cache data;
>  		u64 msr_val;
>  		u32 id;
> diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
> index b8124b562dea..fc74dafa72ff 100644
> --- a/arch/x86/kvm/x86.c
> +++ b/arch/x86/kvm/x86.c
> @@ -261,7 +261,7 @@ static int emulator_fix_hypercall(struct x86_emulate_ctxt *ctxt);
>  static inline void kvm_async_pf_hash_reset(struct kvm_vcpu *vcpu)
>  {
>  	int i;
> -	for (i = 0; i < roundup_pow_of_two(ASYNC_PF_PER_VCPU); i++)
> +	for (i = 0; i < ASYNC_PF_PER_VCPU; i++)
>  		vcpu->arch.apf.gfns[i] = ~0;
>  }
>  
> @@ -10265,12 +10265,14 @@ void kvm_arch_async_page_ready(struct kvm_vcpu *vcpu, struct kvm_async_pf *work)
>  
>  static inline u32 kvm_async_pf_hash_fn(gfn_t gfn)
>  {
> +	BUILD_BUG_ON(!is_power_of_2(ASYNC_PF_PER_VCPU));
> +
>  	return hash_32(gfn & 0xffffffff, order_base_2(ASYNC_PF_PER_VCPU));
>  }
>  
>  static inline u32 kvm_async_pf_next_probe(u32 key)
>  {
> -	return (key + 1) & (roundup_pow_of_two(ASYNC_PF_PER_VCPU) - 1);
> +	return (key + 1) & (ASYNC_PF_PER_VCPU - 1);
>  }
>  
>  static void kvm_add_async_pf_gfn(struct kvm_vcpu *vcpu, gfn_t gfn)
> @@ -10288,7 +10290,7 @@ static u32 kvm_async_pf_gfn_slot(struct kvm_vcpu *vcpu, gfn_t gfn)
>  	int i;
>  	u32 key = kvm_async_pf_hash_fn(gfn);
>  
> -	for (i = 0; i < roundup_pow_of_two(ASYNC_PF_PER_VCPU) &&
> +	for (i = 0; i < ASYNC_PF_PER_VCPU &&
>  		     (vcpu->arch.apf.gfns[key] != gfn &&
>  		      vcpu->arch.apf.gfns[key] != ~0); i++)
>  		key = kvm_async_pf_next_probe(key);
> 

Queued, thanks.

Paolo


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

end of thread, other threads:[~2020-05-04 16:57 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-04-16 15:58 [PATCH] KVM: X86: Force ASYNC_PF_PER_VCPU to be power of two Peter Xu
2020-05-04 16:57 ` 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).