kvm.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Gleb Natapov <gleb@redhat.com>
To: kvm@vger.kernel.org
Cc: pbonzini@redhat.com, Jan Kiszka <jan.kiszka@siemens.com>
Subject: [PATCH RFC] KVM: Fix race in apic->pending_events processing
Date: Sun, 26 May 2013 16:00:31 +0300	[thread overview]
Message-ID: <20130526130031.GS4725@redhat.com> (raw)

apic->pending_events processing has a race that may cause INIT and SIPI
processing to be reordered:

vpu0:                            vcpu1:
set INIT
                               test_and_clear_bit(KVM_APIC_INIT)
                                  process INIT
set INIT
set SIPI
                               test_and_clear_bit(KVM_APIC_SIPI)
                                  process SIPI

At the and INIT is left pending in pending_events. The following patch
tries to fix this using the fact that if INIT comes after SIPI it drops
SIPI from the pending_events, so if pending_events is different after
SIPI is processed it means that INIT was issued after SIPI otherwise
all pending event are processed and pending_events can be reset to zero.
 
Signed-off-by: Gleb Natapov <gleb@redhat.com>
diff --git a/arch/x86/kvm/lapic.c b/arch/x86/kvm/lapic.c
index 9d75193..67686b8 100644
--- a/arch/x86/kvm/lapic.c
+++ b/arch/x86/kvm/lapic.c
@@ -1850,6 +1850,7 @@ void kvm_apic_accept_events(struct kvm_vcpu *vcpu)
 {
 	struct kvm_lapic *apic = vcpu->arch.apic;
 	unsigned int sipi_vector;
+	unsigned long pe;
 
 	if (!kvm_vcpu_has_lapic(vcpu))
 		return;
@@ -1862,7 +1863,8 @@ void kvm_apic_accept_events(struct kvm_vcpu *vcpu)
 		else
 			vcpu->arch.mp_state = KVM_MP_STATE_INIT_RECEIVED;
 	}
-	if (test_and_clear_bit(KVM_APIC_SIPI, &apic->pending_events) &&
+	pe = apic->pending_events;
+	if (test_bit(KVM_APIC_SIPI, &pe) &&
 	    vcpu->arch.mp_state == KVM_MP_STATE_INIT_RECEIVED) {
 		/* evaluate pending_events before reading the vector */
 		smp_rmb();
@@ -1871,6 +1873,7 @@ void kvm_apic_accept_events(struct kvm_vcpu *vcpu)
 			 vcpu->vcpu_id, sipi_vector);
 		kvm_vcpu_deliver_sipi_vector(vcpu, sipi_vector);
 		vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
+		cmpxchg(&apic->pending_events, pe, 0);
 	}
 }
 
--
			Gleb.

             reply	other threads:[~2013-05-26 13:00 UTC|newest]

Thread overview: 25+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-05-26 13:00 Gleb Natapov [this message]
2013-05-28 10:56 ` [PATCH RFC] KVM: Fix race in apic->pending_events processing Paolo Bonzini
2013-05-28 12:56   ` Gleb Natapov
2013-05-28 13:48     ` Paolo Bonzini
2013-05-28 15:00       ` Gleb Natapov
2013-05-28 16:33         ` Paolo Bonzini
2013-05-30  1:20           ` Gleb Natapov
2013-05-30  5:41             ` Paolo Bonzini
2013-05-30  6:01               ` Gleb Natapov
2013-05-30  6:31                 ` Paolo Bonzini
2013-05-30  7:09                   ` Gleb Natapov
2013-05-30  7:30                     ` Paolo Bonzini
2013-05-30 12:34                       ` Gleb Natapov
2013-05-30 12:58                         ` Paolo Bonzini
2013-05-30 13:10                           ` Gleb Natapov
2013-05-30 13:23                             ` Paolo Bonzini
2013-05-30 13:35                               ` Gleb Natapov
2013-05-30 14:15                                 ` Paolo Bonzini
2013-05-31  4:36                                   ` Gleb Natapov
2013-05-31  8:48                                     ` Paolo Bonzini
2013-05-31  9:18                                       ` Gleb Natapov
2013-05-31  9:48                                         ` Paolo Bonzini
2013-06-02 13:14                                           ` Gleb Natapov
2013-06-02 14:32                                             ` Paolo Bonzini
2013-06-02 17:33                                               ` Gleb Natapov

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20130526130031.GS4725@redhat.com \
    --to=gleb@redhat.com \
    --cc=jan.kiszka@siemens.com \
    --cc=kvm@vger.kernel.org \
    --cc=pbonzini@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).