From patchwork Wed Jan 26 09:39:44 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Hajnoczi X-Patchwork-Id: 80494 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [199.232.76.165]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 1362EB70DA for ; Wed, 26 Jan 2011 21:55:02 +1100 (EST) Received: from localhost ([127.0.0.1]:53134 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1Pi31d-0004JB-79 for incoming@patchwork.ozlabs.org; Wed, 26 Jan 2011 05:54:57 -0500 Received: from [140.186.70.92] (port=50449 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1Pi1vs-0003xa-6O for qemu-devel@nongnu.org; Wed, 26 Jan 2011 04:45:03 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Pi1rD-0005Ge-2C for qemu-devel@nongnu.org; Wed, 26 Jan 2011 04:40:09 -0500 Received: from mtagate1.uk.ibm.com ([194.196.100.161]:53361) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Pi1rC-0005GA-NP for qemu-devel@nongnu.org; Wed, 26 Jan 2011 04:40:07 -0500 Received: from d06nrmr1707.portsmouth.uk.ibm.com (d06nrmr1707.portsmouth.uk.ibm.com [9.149.39.225]) by mtagate1.uk.ibm.com (8.13.1/8.13.1) with ESMTP id p0Q9e3KV030209 for ; Wed, 26 Jan 2011 09:40:03 GMT Received: from d06av01.portsmouth.uk.ibm.com (d06av01.portsmouth.uk.ibm.com [9.149.37.212]) by d06nrmr1707.portsmouth.uk.ibm.com (8.13.8/8.13.8/NCO v10.0) with ESMTP id p0Q9e5Bq1450128 for ; Wed, 26 Jan 2011 09:40:05 GMT Received: from d06av01.portsmouth.uk.ibm.com (loopback [127.0.0.1]) by d06av01.portsmouth.uk.ibm.com (8.14.4/8.13.1/NCO v10.0 AVout) with ESMTP id p0Q9e2AP030932 for ; Wed, 26 Jan 2011 02:40:02 -0700 Received: from stefanha-thinkpad.ibm.com (zav01467.de.ibm.com [9.145.152.170]) by d06av01.portsmouth.uk.ibm.com (8.14.4/8.13.1/NCO v10.0 AVin) with ESMTP id p0Q9e1Dh030910; Wed, 26 Jan 2011 02:40:01 -0700 From: Stefan Hajnoczi To: Date: Wed, 26 Jan 2011 09:39:44 +0000 Message-Id: <1296034784-6513-1-git-send-email-stefanha@linux.vnet.ibm.com> X-Mailer: git-send-email 1.7.2.3 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6, seldom 2.4 (older, 4) Cc: Marcelo Tosatti , Avi Kivity , Stefan Hajnoczi Subject: [Qemu-devel] [PATCH] kvm: Prevent dynticks race condition for !CONFIG_IOTHREAD X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: qemu-devel.nongnu.org List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org The dynticks timer arranges for SIGALRM to be raised when the next pending timer expires. When building with !CONFIG_IOTHREAD, we need to check whether a request to exit the vcpu is pending before re-entering the guest. Unfortunately there is a race condition here because SIGALRM may be raised after we check for an exit request but before re-entering the guest. In that case the guest is re-entered without the dynticks timer being rearmed. This results in temporary loss of timers until some other event forces a vmexit. In the case of a CPU-bound guest it can cause softlockups. This patch blocks SIGALRM before checking for an exit request and uses KVM's sigmask support to atomically unblock it when entering the guest, thereby making the exit request check safe. Signed-off-by: Stefan Hajnoczi --- cpus.c | 17 ++++++++++++++++- kvm-all.c | 16 ++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletions(-) Does not affect qemu-kvm.git. Still worth having in qemu.git so we don't get odd behavior when building without --enable-io-thread. diff --git a/cpus.c b/cpus.c index 0309189..59dbfab 100644 --- a/cpus.c +++ b/cpus.c @@ -262,14 +262,29 @@ void qemu_main_loop_start(void) { } +static void kvm_init_sigmask(CPUState *env) +{ + int r; + sigset_t set; + + pthread_sigmask(SIG_SETMASK, NULL, &set); + r = kvm_set_signal_mask(env, &set); + if (r) { + fprintf(stderr, "kvm_set_signal_mask: %s\n", strerror(r)); + exit(1); + } +} + void qemu_init_vcpu(void *_env) { CPUState *env = _env; env->nr_cores = smp_cores; env->nr_threads = smp_threads; - if (kvm_enabled()) + if (kvm_enabled()) { kvm_init_vcpu(env); + kvm_init_sigmask(env); + } return; } diff --git a/kvm-all.c b/kvm-all.c index 255b6fa..9cc2553 100644 --- a/kvm-all.c +++ b/kvm-all.c @@ -890,19 +890,31 @@ int kvm_cpu_exec(CPUState *env) { struct kvm_run *run = env->kvm_run; int ret; +#ifndef CONFIG_IOTHREAD + sigset_t set, old_set; + + sigemptyset(&set); + sigaddset(&set, SIGALRM); +#endif DPRINTF("kvm_cpu_exec()\n"); do { #ifndef CONFIG_IOTHREAD + pthread_sigmask(SIG_BLOCK, &set, &old_set); + if (env->exit_request) { DPRINTF("interrupt exit requested\n"); + pthread_sigmask(SIG_SETMASK, &old_set, NULL); ret = 0; break; } #endif if (kvm_arch_process_irqchip_events(env)) { +#ifndef CONFIG_IOTHREAD + pthread_sigmask(SIG_SETMASK, &old_set, NULL); +#endif ret = 0; break; } @@ -920,6 +932,10 @@ int kvm_cpu_exec(CPUState *env) cpu_single_env = env; kvm_arch_post_run(env, run); +#ifndef CONFIG_IOTHREAD + pthread_sigmask(SIG_SETMASK, &old_set, NULL); +#endif + if (ret == -EINTR || ret == -EAGAIN) { cpu_exit(env); DPRINTF("io window exit\n");