From: Avi Kivity Subject: [PATCH 1/3] smp: fix race in async on_cpu() Date: Thu, 6 Jan 2011 12:48:42 +0200 Message-ID: <1294310924-14174-2-git-send-email-avi@redhat.com> References: <1294310924-14174-1-git-send-email-avi@redhat.com> Cc: Rik van Riel To: Marcelo Tosatti , kvm@vger.kernel.org Return-path: Received: from mx1.redhat.com ([209.132.183.28]:19542 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752950Ab1AFKtA (ORCPT ); Thu, 6 Jan 2011 05:49:00 -0500 Received: from int-mx10.intmail.prod.int.phx2.redhat.com (int-mx10.intmail.prod.int.phx2.redhat.com [10.5.11.23]) by mx1.redhat.com (8.13.8/8.13.8) with ESMTP id p06AmxhE023787 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK) for ; Thu, 6 Jan 2011 05:49:00 -0500 Received: from cleopatra.tlv.redhat.com (cleopatra.tlv.redhat.com [10.35.255.11]) by int-mx10.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id p06AmxI7031055 for ; Thu, 6 Jan 2011 05:48:59 -0500 In-Reply-To: <1294310924-14174-1-git-send-email-avi@redhat.com> Sender: kvm-owner@vger.kernel.org List-ID: We fire off the IPI, but don't wait for the other cpu to pickk up the function and data before returning. Fix by making the other cpu ACK the receipt of the IPI (but still execute the result asynchrously). Signed-off-by: Avi Kivity --- lib/x86/smp.c | 29 ++++++++++++++++++++--------- 1 files changed, 20 insertions(+), 9 deletions(-) diff --git a/lib/x86/smp.c b/lib/x86/smp.c index 241f755..8da614a 100644 --- a/lib/x86/smp.c +++ b/lib/x86/smp.c @@ -7,15 +7,27 @@ #define IPI_VECTOR 0x20 static struct spinlock ipi_lock; -static void (*ipi_function)(void *data); -static void *ipi_data; +static volatile void (*ipi_function)(void *data); +static volatile void *ipi_data; static volatile int ipi_done; +static volatile bool ipi_wait; +static int _cpu_count; static __attribute__((used)) void ipi() { - ipi_function(ipi_data); - apic_write(APIC_EOI, 0); - ipi_done = 1; + void (*function)(void *data) = ipi_function; + void *data = ipi_data; + bool wait = ipi_wait; + + if (!wait) { + ipi_done = 1; + apic_write(APIC_EOI, 0); + } + function(data); + if (wait) { + ipi_done = 1; + apic_write(APIC_EOI, 0); + } } asm ( @@ -92,13 +104,12 @@ static void __on_cpu(int cpu, void (*function)(void *data), void *data, ipi_done = 0; ipi_function = function; ipi_data = data; + ipi_wait = wait; apic_icr_write(APIC_INT_ASSERT | APIC_DEST_PHYSICAL | APIC_DM_FIXED | IPI_VECTOR, cpu); - if (wait) { - while (!ipi_done) - ; - } + while (!ipi_done) + ; } spin_unlock(&ipi_lock); } -- 1.7.1