From patchwork Thu May 19 12:28:30 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sasha Levin X-Patchwork-Id: 797382 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by demeter2.kernel.org (8.14.4/8.14.3) with ESMTP id p4JCSoFT015935 for ; Thu, 19 May 2011 12:28:51 GMT Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1756152Ab1ESM2r (ORCPT ); Thu, 19 May 2011 08:28:47 -0400 Received: from mail-ww0-f44.google.com ([74.125.82.44]:36353 "EHLO mail-ww0-f44.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1754801Ab1ESM2r (ORCPT ); Thu, 19 May 2011 08:28:47 -0400 Received: by wwa36 with SMTP id 36so2825246wwa.1 for ; Thu, 19 May 2011 05:28:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:from:to:cc:subject:date:message-id:x-mailer; bh=+1tFauA/0N8h1nuG1jwrJHfqaWogVSLAakmpUvPFZzI=; b=GHBDs/inEetiHlE2ceGKqfmD6PMsbe5zq+WUJSVI7rfq94T/OBOWNG/3izspkVTXqn XdkoqN5ULlPCUkpFSJsnKzsfNsXq4uK2cdutUGibosJDN3kYbeHwd7g9a228G2OUxPu9 Esj5nzEI8FT5tIH8adbMbwhNwSnxom+MTqqkA= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=from:to:cc:subject:date:message-id:x-mailer; b=cpUfQSD0RegauLwEVyqAQd9UQKwCQ6PGW50k1CatZqusGksxoX2suhx2+DVdjhtUgl njDI2wWN/+2UVAjHcifuIsOtbTkilUQa9GZQmZCo98waRCev3Wb+MSJ98TcLodf9x0Rx a1h0MJG01iGxW8BLjA5T44F/gn5fT+2YPAGdo= Received: by 10.227.59.210 with SMTP id m18mr3052179wbh.112.1305808126135; Thu, 19 May 2011 05:28:46 -0700 (PDT) Received: from localhost.localdomain (bzq-79-179-206-38.red.bezeqint.net [79.179.206.38]) by mx.google.com with ESMTPS id l24sm1585088wbc.47.2011.05.19.05.28.44 (version=TLSv1/SSLv3 cipher=OTHER); Thu, 19 May 2011 05:28:45 -0700 (PDT) From: Sasha Levin To: penberg@kernel.org Cc: mingo@elte.hu, asias.hejun@gmail.com, gorcunov@gmail.com, prasadjoshi124@gmail.com, kvm@vger.kernel.org, Sasha Levin Subject: [PATCH] kvm tools: Exit properly on SMP guests Date: Thu, 19 May 2011 15:28:30 +0300 Message-Id: <1305808110-20294-1-git-send-email-levinsasha928@gmail.com> X-Mailer: git-send-email 1.7.5.rc3 Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org X-Greylist: IP, sender and recipient auto-whitelisted, not delayed by milter-greylist-4.2.6 (demeter2.kernel.org [140.211.167.43]); Thu, 19 May 2011 12:28:51 +0000 (UTC) When shutting down SMP guests only VCPU #0 will receive a KVM_EXIT_SHUTDOWN. Waiting for all VCPU threads to exit causes them to hang. Instead, notify all VCPU threads once VCPU #0 thread is terminated so they could also stop properly. Signed-off-by: Sasha Levin --- tools/kvm/include/kvm/kvm.h | 2 ++ tools/kvm/kvm-cpu.c | 13 ++++++++++++- tools/kvm/kvm-run.c | 10 +++++++--- 3 files changed, 21 insertions(+), 4 deletions(-) diff --git a/tools/kvm/include/kvm/kvm.h b/tools/kvm/include/kvm/kvm.h index d9943bf..aafba87 100644 --- a/tools/kvm/include/kvm/kvm.h +++ b/tools/kvm/include/kvm/kvm.h @@ -11,6 +11,8 @@ #define KVM_32BIT_GAP_SIZE (512 << 20) #define KVM_32BIT_GAP_START ((1ULL << 32) - KVM_32BIT_GAP_SIZE) +#define SIGKVMEXIT (SIGUSR1 + 2) + struct kvm { int sys_fd; /* For system ioctls(), i.e. /dev/kvm */ int vm_fd; /* For VM ioctls() */ diff --git a/tools/kvm/kvm-cpu.c b/tools/kvm/kvm-cpu.c index cdfeb85..331e025 100644 --- a/tools/kvm/kvm-cpu.c +++ b/tools/kvm/kvm-cpu.c @@ -379,6 +379,11 @@ void kvm_cpu__run(struct kvm_cpu *vcpu) die_perror("KVM_RUN failed"); } +static void kvm_cpu_exit_handler(int signum) +{ + /* Don't do anything here */ +} + int kvm_cpu__start(struct kvm_cpu *cpu) { sigset_t sigset; @@ -388,6 +393,8 @@ int kvm_cpu__start(struct kvm_cpu *cpu) pthread_sigmask(SIG_BLOCK, &sigset, NULL); + signal(SIGKVMEXIT, kvm_cpu_exit_handler); + kvm_cpu__setup_cpuid(cpu); kvm_cpu__reset_vcpu(cpu); @@ -430,7 +437,11 @@ int kvm_cpu__start(struct kvm_cpu *cpu) break; } case KVM_EXIT_INTR: - break; + /* + * Currently we only handle exit signal, which means + * we just exit if KVM_RUN exited due to a signal. + */ + goto exit_kvm; case KVM_EXIT_SHUTDOWN: goto exit_kvm; default: diff --git a/tools/kvm/kvm-run.c b/tools/kvm/kvm-run.c index 63181bf..8379a52 100644 --- a/tools/kvm/kvm-run.c +++ b/tools/kvm/kvm-run.c @@ -404,6 +404,7 @@ int kvm_cmd_run(int argc, const char **argv, const char *prefix) int max_cpus; char *hi; int i; + void *ret; signal(SIGALRM, handle_sigalrm); signal(SIGQUIT, handle_sigquit); @@ -583,14 +584,17 @@ int kvm_cmd_run(int argc, const char **argv, const char *prefix) die("unable to create KVM VCPU thread"); } - for (i = 0; i < nrcpus; i++) { - void *ret; + /* Only VCPU #0 is going to exit by itself when shutting down */ + if (pthread_join(kvm_cpus[0]->thread, &ret) != 0) + exit_code = 1; + for (i = 1; i < nrcpus; i++) { + pthread_kill(kvm_cpus[i]->thread, SIGKVMEXIT); if (pthread_join(kvm_cpus[i]->thread, &ret) != 0) die("pthread_join"); if (ret != NULL) - exit_code = 1; + exit_code = 1; } kvm__delete(kvm);