From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([208.118.235.92]:59055) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1SwAV4-0006cx-9B for qemu-devel@nongnu.org; Tue, 31 Jul 2012 07:20:34 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1SwAUy-000355-AC for qemu-devel@nongnu.org; Tue, 31 Jul 2012 07:20:30 -0400 Received: from mail-pb0-f45.google.com ([209.85.160.45]:62899) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1SwAUy-00034z-2I for qemu-devel@nongnu.org; Tue, 31 Jul 2012 07:20:24 -0400 Received: by pbbro12 with SMTP id ro12so10973965pbb.4 for ; Tue, 31 Jul 2012 04:20:23 -0700 (PDT) From: riegamaths@gmail.com Date: Tue, 31 Jul 2012 19:18:17 +0800 Message-Id: <1343733497-26977-1-git-send-email-riegamaths@gmail.com> Subject: [Qemu-devel] [PATCH v3] kvm: Check if smp_cpus exceeds max cpus supported by kvm List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel Cc: Peter Maydell , Stefan Hajnoczi , kvm@vger.kernel.org, Marcelo Tosatti , Dunrong Huang , Avi Kivity From: Dunrong Huang Add a helper function for fetching max cpus supported by kvm. Make QEMU exit with an error message if smp_cpus exceeds limit of VCPU count retrieved by invoking this helper function. Signed-off-by: Dunrong Huang --- v1 -> v2: * Fix indentation(thanks to Stefan Hajnoczi for his review) v2 -> v3(Thanks to Peter Maydell for this hint) * Add a comment for helper function * Use "goto err" instead of "exit(1)" kvm-all.c | 29 +++++++++++++++++++++++++++++ 1 files changed, 29 insertions(+), 0 deletions(-) diff --git a/kvm-all.c b/kvm-all.c index 2148b20..bf64761 100644 --- a/kvm-all.c +++ b/kvm-all.c @@ -1207,6 +1207,26 @@ static int kvm_irqchip_create(KVMState *s) return 0; } +static int kvm_max_vcpus(KVMState *s) +{ + int ret; + + /* Find number of supported CPUs using the recommended + * procedure from the kernel API documentation to cope with + * older kernels that may be missing capabilities. + */ + ret = kvm_check_extension(s, KVM_CAP_MAX_VCPUS); + if (ret) { + return ret; + } + ret = kvm_check_extension(s, KVM_CAP_NR_VCPUS); + if (ret) { + return ret; + } + + return 4; +} + int kvm_init(void) { static const char upgrade_note[] = @@ -1216,6 +1236,7 @@ int kvm_init(void) const KVMCapabilityInfo *missing_cap; int ret; int i; + int max_vcpus; s = g_malloc0(sizeof(KVMState)); @@ -1256,6 +1277,14 @@ int kvm_init(void) goto err; } + max_vcpus = kvm_max_vcpus(s); + if (smp_cpus > max_vcpus) { + ret = -EINVAL; + fprintf(stderr, "Number of SMP cpus requested (%d) exceeds max cpus " + "supported by KVM (%d)\n", smp_cpus, max_vcpus); + goto err; + } + s->vmfd = kvm_ioctl(s, KVM_CREATE_VM, 0); if (s->vmfd < 0) { #ifdef TARGET_S390X -- 1.7.8.6