kvm.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] KVM: Get rid of KVM_REQ_KICK
@ 2010-05-03 14:19 Avi Kivity
  2010-05-04 16:31 ` Marcelo Tosatti
  2010-05-06 19:56 ` Marcelo Tosatti
  0 siblings, 2 replies; 5+ messages in thread
From: Avi Kivity @ 2010-05-03 14:19 UTC (permalink / raw)
  To: Marcelo Tosatti, kvm

KVM_REQ_KICK poisons vcpu->requests by having a bit set during normal
operation.  This causes the fast path check for a clear vcpu->requests
to fail all the time, triggering tons of atomic operations.

Fix by replacing KVM_REQ_KICK with a vcpu->guest_mode atomic.

Signed-off-by: Avi Kivity <avi@redhat.com>
---
 arch/x86/kvm/x86.c       |   17 ++++++++++-------
 include/linux/kvm_host.h |    1 +
 2 files changed, 11 insertions(+), 7 deletions(-)

diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
index 6b2ce1d..307094a 100644
--- a/arch/x86/kvm/x86.c
+++ b/arch/x86/kvm/x86.c
@@ -4499,13 +4499,15 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu)
 	if (vcpu->fpu_active)
 		kvm_load_guest_fpu(vcpu);
 
-	local_irq_disable();
+	atomic_set(&vcpu->guest_mode, 1);
+	smp_wmb();
 
-	clear_bit(KVM_REQ_KICK, &vcpu->requests);
-	smp_mb__after_clear_bit();
+	local_irq_disable();
 
-	if (vcpu->requests || need_resched() || signal_pending(current)) {
-		set_bit(KVM_REQ_KICK, &vcpu->requests);
+	if (!atomic_read(&vcpu->guest_mode) || vcpu->requests
+	    || need_resched() || signal_pending(current)) {
+		atomic_set(&vcpu->guest_mode, 0);
+		smp_wmb();
 		local_irq_enable();
 		preempt_enable();
 		r = 1;
@@ -4550,7 +4552,8 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu)
 	if (hw_breakpoint_active())
 		hw_breakpoint_restore();
 
-	set_bit(KVM_REQ_KICK, &vcpu->requests);
+	atomic_set(&vcpu->guest_mode, 0);
+	smp_wmb();
 	local_irq_enable();
 
 	++vcpu->stat.exits;
@@ -5470,7 +5473,7 @@ void kvm_vcpu_kick(struct kvm_vcpu *vcpu)
 
 	me = get_cpu();
 	if (cpu != me && (unsigned)cpu < nr_cpu_ids && cpu_online(cpu))
-		if (!test_and_set_bit(KVM_REQ_KICK, &vcpu->requests))
+		if (atomic_xchg(&vcpu->guest_mode, 0))
 			smp_send_reschedule(cpu);
 	put_cpu();
 }
diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h
index ce027d5..a020fa2 100644
--- a/include/linux/kvm_host.h
+++ b/include/linux/kvm_host.h
@@ -81,6 +81,7 @@ struct kvm_vcpu {
 	int vcpu_id;
 	struct mutex mutex;
 	int   cpu;
+	atomic_t guest_mode;
 	struct kvm_run *run;
 	unsigned long requests;
 	unsigned long guest_debug;
-- 
1.7.0.4


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

* Re: [PATCH] KVM: Get rid of KVM_REQ_KICK
  2010-05-03 14:19 [PATCH] KVM: Get rid of KVM_REQ_KICK Avi Kivity
@ 2010-05-04 16:31 ` Marcelo Tosatti
  2010-05-04 16:42   ` Avi Kivity
  2010-05-06 19:56 ` Marcelo Tosatti
  1 sibling, 1 reply; 5+ messages in thread
From: Marcelo Tosatti @ 2010-05-04 16:31 UTC (permalink / raw)
  To: Avi Kivity; +Cc: kvm

On Mon, May 03, 2010 at 05:19:08PM +0300, Avi Kivity wrote:
> KVM_REQ_KICK poisons vcpu->requests by having a bit set during normal
> operation.  This causes the fast path check for a clear vcpu->requests
> to fail all the time, triggering tons of atomic operations.

Avi,

Do you have numbers? 

> Fix by replacing KVM_REQ_KICK with a vcpu->guest_mode atomic.
> 
> Signed-off-by: Avi Kivity <avi@redhat.com>
> ---
>  arch/x86/kvm/x86.c       |   17 ++++++++++-------
>  include/linux/kvm_host.h |    1 +
>  2 files changed, 11 insertions(+), 7 deletions(-)
> 
> diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
> index 6b2ce1d..307094a 100644
> --- a/arch/x86/kvm/x86.c
> +++ b/arch/x86/kvm/x86.c
> @@ -4499,13 +4499,15 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu)
>  	if (vcpu->fpu_active)
>  		kvm_load_guest_fpu(vcpu);
>  
> -	local_irq_disable();
> +	atomic_set(&vcpu->guest_mode, 1);
> +	smp_wmb();

IPI can trigger here?

> -	clear_bit(KVM_REQ_KICK, &vcpu->requests);
> -	smp_mb__after_clear_bit();
> +	local_irq_disable();
>  
> -	if (vcpu->requests || need_resched() || signal_pending(current)) {
> -		set_bit(KVM_REQ_KICK, &vcpu->requests);
> +	if (!atomic_read(&vcpu->guest_mode) || vcpu->requests
> +	    || need_resched() || signal_pending(current)) {
> +		atomic_set(&vcpu->guest_mode, 0);
> +		smp_wmb();
>  		local_irq_enable();
>  		preempt_enable();
>  		r = 1;
> @@ -4550,7 +4552,8 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu)
>  	if (hw_breakpoint_active())
>  		hw_breakpoint_restore();
>  
> -	set_bit(KVM_REQ_KICK, &vcpu->requests);
> +	atomic_set(&vcpu->guest_mode, 0);
> +	smp_wmb();
>  	local_irq_enable();
>  
>  	++vcpu->stat.exits;
> @@ -5470,7 +5473,7 @@ void kvm_vcpu_kick(struct kvm_vcpu *vcpu)
>  
>  	me = get_cpu();
>  	if (cpu != me && (unsigned)cpu < nr_cpu_ids && cpu_online(cpu))
> -		if (!test_and_set_bit(KVM_REQ_KICK, &vcpu->requests))
> +		if (atomic_xchg(&vcpu->guest_mode, 0))
>  			smp_send_reschedule(cpu);
>  	put_cpu();
>  }
> diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h
> index ce027d5..a020fa2 100644
> --- a/include/linux/kvm_host.h
> +++ b/include/linux/kvm_host.h
> @@ -81,6 +81,7 @@ struct kvm_vcpu {
>  	int vcpu_id;
>  	struct mutex mutex;
>  	int   cpu;
> +	atomic_t guest_mode;
>  	struct kvm_run *run;
>  	unsigned long requests;
>  	unsigned long guest_debug;
> -- 
> 1.7.0.4
> 
> --
> To unsubscribe from this list: send the line "unsubscribe kvm" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html

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

* Re: [PATCH] KVM: Get rid of KVM_REQ_KICK
  2010-05-04 16:31 ` Marcelo Tosatti
@ 2010-05-04 16:42   ` Avi Kivity
  2010-05-04 16:49     ` Avi Kivity
  0 siblings, 1 reply; 5+ messages in thread
From: Avi Kivity @ 2010-05-04 16:42 UTC (permalink / raw)
  To: Marcelo Tosatti; +Cc: kvm

On 05/04/2010 07:31 PM, Marcelo Tosatti wrote:
> On Mon, May 03, 2010 at 05:19:08PM +0300, Avi Kivity wrote:
>    
>> KVM_REQ_KICK poisons vcpu->requests by having a bit set during normal
>> operation.  This causes the fast path check for a clear vcpu->requests
>> to fail all the time, triggering tons of atomic operations.
>>      
> Avi,
>
> Do you have numbers?
>    

Forgot to post, was about 100 cycles (I expected more, all those atomics 
really show up in the profile).

>> Fix by replacing KVM_REQ_KICK with a vcpu->guest_mode atomic.
>>
>> Signed-off-by: Avi Kivity<avi@redhat.com>
>> ---
>>   arch/x86/kvm/x86.c       |   17 ++++++++++-------
>>   include/linux/kvm_host.h |    1 +
>>   2 files changed, 11 insertions(+), 7 deletions(-)
>>
>> diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
>> index 6b2ce1d..307094a 100644
>> --- a/arch/x86/kvm/x86.c
>> +++ b/arch/x86/kvm/x86.c
>> @@ -4499,13 +4499,15 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu)
>>   	if (vcpu->fpu_active)
>>   		kvm_load_guest_fpu(vcpu);
>>
>> -	local_irq_disable();
>> +	atomic_set(&vcpu->guest_mode, 1);
>> +	smp_wmb();
>>      
> IPI can trigger here?
>    

It can...

>    
>> -	clear_bit(KVM_REQ_KICK,&vcpu->requests);
>> -	smp_mb__after_clear_bit();
>> +	local_irq_disable();
>>
>> -	if (vcpu->requests || need_resched() || signal_pending(current)) {
>> -		set_bit(KVM_REQ_KICK,&vcpu->requests);
>> +	if (!atomic_read(&vcpu->guest_mode) || vcpu->requests
>> +	    || need_resched() || signal_pending(current)) {
>>      

... and we'll detect that guest_mode was cleared and go back.

>> +		atomic_set(&vcpu->guest_mode, 0);
>> +		smp_wmb();
>>   		local_irq_enable();
>>   		preempt_enable();
>>   		r = 1;
>> @@ -4550,7 +4552,8 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu)
>>   	if (hw_breakpoint_active())
>>   		hw_breakpoint_restore();
>>
>> -	set_bit(KVM_REQ_KICK,&vcpu->requests);
>> +	atomic_set(&vcpu->guest_mode, 0);
>> +	smp_wmb();
>>   	local_irq_enable();
>>
>>   	++vcpu->stat.exits;
>> @@ -5470,7 +5473,7 @@ void kvm_vcpu_kick(struct kvm_vcpu *vcpu)
>>
>>   	me = get_cpu();
>>   	if (cpu != me&&  (unsigned)cpu<  nr_cpu_ids&&  cpu_online(cpu))
>> -		if (!test_and_set_bit(KVM_REQ_KICK,&vcpu->requests))
>> +		if (atomic_xchg(&vcpu->guest_mode, 0))
>>   			smp_send_reschedule(cpu);
>>   	put_cpu();
>>      

The atomic_xchg() does the trick.

-- 
error compiling committee.c: too many arguments to function


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

* Re: [PATCH] KVM: Get rid of KVM_REQ_KICK
  2010-05-04 16:42   ` Avi Kivity
@ 2010-05-04 16:49     ` Avi Kivity
  0 siblings, 0 replies; 5+ messages in thread
From: Avi Kivity @ 2010-05-04 16:49 UTC (permalink / raw)
  To: Marcelo Tosatti; +Cc: kvm

On 05/04/2010 07:42 PM, Avi Kivity wrote:
>>> @@ -5470,7 +5473,7 @@ void kvm_vcpu_kick(struct kvm_vcpu *vcpu)
>>>
>>>       me = get_cpu();
>>>       if (cpu != me&&  (unsigned)cpu<  nr_cpu_ids&&  cpu_online(cpu))
>>> -        if (!test_and_set_bit(KVM_REQ_KICK,&vcpu->requests))
>>> +        if (atomic_xchg(&vcpu->guest_mode, 0))
>>>               smp_send_reschedule(cpu);
>>>       put_cpu();
>
>
> The atomic_xchg() does the trick.
>

btw, things like this should go into locking.txt.  It's definitely tricky.

-- 
error compiling committee.c: too many arguments to function


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

* Re: [PATCH] KVM: Get rid of KVM_REQ_KICK
  2010-05-03 14:19 [PATCH] KVM: Get rid of KVM_REQ_KICK Avi Kivity
  2010-05-04 16:31 ` Marcelo Tosatti
@ 2010-05-06 19:56 ` Marcelo Tosatti
  1 sibling, 0 replies; 5+ messages in thread
From: Marcelo Tosatti @ 2010-05-06 19:56 UTC (permalink / raw)
  To: Avi Kivity; +Cc: kvm

On Mon, May 03, 2010 at 05:19:08PM +0300, Avi Kivity wrote:
> KVM_REQ_KICK poisons vcpu->requests by having a bit set during normal
> operation.  This causes the fast path check for a clear vcpu->requests
> to fail all the time, triggering tons of atomic operations.
> 
> Fix by replacing KVM_REQ_KICK with a vcpu->guest_mode atomic.
> 
> Signed-off-by: Avi Kivity <avi@redhat.com>
> ---
>  arch/x86/kvm/x86.c       |   17 ++++++++++-------
>  include/linux/kvm_host.h |    1 +
>  2 files changed, 11 insertions(+), 7 deletions(-)

Reviewed-by: Marcelo Tosatti <mtosatti@redhat.com>

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

end of thread, other threads:[~2010-05-06 20:11 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2010-05-03 14:19 [PATCH] KVM: Get rid of KVM_REQ_KICK Avi Kivity
2010-05-04 16:31 ` Marcelo Tosatti
2010-05-04 16:42   ` Avi Kivity
2010-05-04 16:49     ` Avi Kivity
2010-05-06 19:56 ` Marcelo Tosatti

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).