From patchwork Thu Apr 13 20:19:50 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?UmFkaW0gS3LEjW3DocWZ?= X-Patchwork-Id: 9679963 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id C560F60387 for ; Thu, 13 Apr 2017 20:20:31 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id B6639286AE for ; Thu, 13 Apr 2017 20:20:27 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id AA592286B6; Thu, 13 Apr 2017 20:20:27 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.9 required=2.0 tests=BAYES_00,HK_RANDOM_FROM, RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id ED3DC286B4 for ; Thu, 13 Apr 2017 20:20:23 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1756224AbdDMUUX (ORCPT ); Thu, 13 Apr 2017 16:20:23 -0400 Received: from mx1.redhat.com ([209.132.183.28]:57000 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1756209AbdDMUUV (ORCPT ); Thu, 13 Apr 2017 16:20:21 -0400 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id C750BC04B317; Thu, 13 Apr 2017 20:20:20 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com C750BC04B317 Authentication-Results: ext-mx07.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx07.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=rkrcmar@redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com C750BC04B317 Received: from potion (dhcp-1-208.brq.redhat.com [10.34.1.208]) by smtp.corp.redhat.com (Postfix) with SMTP id 07DC877EC2; Thu, 13 Apr 2017 20:20:17 +0000 (UTC) Received: by potion (sSMTP sendmail emulation); Thu, 13 Apr 2017 22:20:17 +0200 From: =?UTF-8?q?Radim=20Kr=C4=8Dm=C3=A1=C5=99?= To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org Cc: Christoffer Dall , Marc Zyngier , Paolo Bonzini , Christian Borntraeger , Cornelia Huck , James Hogan , Paul Mackerras , Alexander Graf Subject: [PATCH 3/4] KVM: add KVM_CREATE_VM2 system ioctl Date: Thu, 13 Apr 2017 22:19:50 +0200 Message-Id: <20170413201951.11939-4-rkrcmar@redhat.com> In-Reply-To: <20170413201951.11939-1-rkrcmar@redhat.com> References: <20170413201951.11939-1-rkrcmar@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.31]); Thu, 13 Apr 2017 20:20:21 +0000 (UTC) Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP This patch allows userspace to tell how many VCPUs it is going to use, which can save memory when allocating the kvm->vcpus array. This will be done with a new KVM_CREATE_VM2 IOCTL. An alternative would be to redo kvm->vcpus as a list or protect the array with RCU. RCU is slower and a list is not even practical as kvm->vcpus are being used for index-based accesses. We could have an IOCTL that is called in between KVM_CREATE_VM and first KVM_CREATE_VCPU and sets the size of the vcpus array, but we'd be making one useless allocation. Knowing the desired number of VCPUs from the beginning is seems best for now. This patch also prepares generic code for architectures that will set KVM_CONFIGURABLE_MAX_VCPUS to a non-zero value. A disputable decision is that KVM_CREATE_VM2 actually works even if KVM_CAP_CONFIGURABLE_MAX_VCPUS is 0, but uses that capability for its detection. Signed-off-by: Radim Krčmář --- Documentation/virtual/kvm/api.txt | 28 ++++++++++++++++++++++++ include/linux/kvm_host.h | 3 +++ include/uapi/linux/kvm.h | 8 +++++++ virt/kvm/kvm_main.c | 45 +++++++++++++++++++++++++++++++++------ 4 files changed, 77 insertions(+), 7 deletions(-) diff --git a/Documentation/virtual/kvm/api.txt b/Documentation/virtual/kvm/api.txt index e60be91d8036..461130adbdc7 100644 --- a/Documentation/virtual/kvm/api.txt +++ b/Documentation/virtual/kvm/api.txt @@ -3259,6 +3259,34 @@ Otherwise, if the MCE is a corrected error, KVM will just store it in the corresponding bank (provided this bank is not holding a previously reported uncorrected error). + +4.107 KVM_CREATE_VM2 + +Capability: KVM_CAP_CONFIGURABLE_MAX_VCPUS +Architectures: all +Type: system ioctl +Parameters: struct kvm_vm_config +Returns: a VM fd that can be used to control the new virtual machine, + -E2BIG if the value of max_vcpus is not supported + +This is an extension of KVM_CREATE_VM that allows the user to pass more +information through + +struct kvm_vm_config { + __u64 type; + __u32 max_vcpus; + __u8 reserved[52]; +}; + +type is the argument to KVM_CREATE_VM + +max_vcpus is the desired maximal number of VCPUs, it must not exceed the value +returned by KVM_CAP_CONFIGURABLE_MAX_VCPUS. Value of 0 treated as if userspace +passed the value returned by KVM_CAP_MAX_VCPU instead. + +reserved is must be 0. + + 5. The kvm_run structure ------------------------ diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index 6ba7bc831094..b875c0997328 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -39,6 +39,9 @@ #ifndef KVM_MAX_VCPU_ID #define KVM_MAX_VCPU_ID KVM_MAX_VCPUS #endif +#ifndef KVM_CONFIGURABLE_MAX_VCPUS +#define KVM_CONFIGURABLE_MAX_VCPUS 0U +#endif /* * The bit 16 ~ bit 31 of kvm_memory_region::flags are internally used diff --git a/include/uapi/linux/kvm.h b/include/uapi/linux/kvm.h index 6180ea50e9ef..8349c73b3517 100644 --- a/include/uapi/linux/kvm.h +++ b/include/uapi/linux/kvm.h @@ -83,6 +83,12 @@ struct kvm_debug_guest { /* *** End of deprecated interfaces *** */ +/* for KVM_CREATE_VM2 */ +struct kvm_vm_config { + __u64 type; + __u32 max_vcpus; + __u8 reserved[52]; +}; /* for KVM_CREATE_MEMORY_REGION */ struct kvm_memory_region { @@ -713,6 +719,7 @@ struct kvm_ppc_resize_hpt { */ #define KVM_GET_API_VERSION _IO(KVMIO, 0x00) #define KVM_CREATE_VM _IO(KVMIO, 0x01) /* returns a VM fd */ +#define KVM_CREATE_VM2 _IOR(KVMIO, 0x01, struct kvm_vm_config) #define KVM_GET_MSR_INDEX_LIST _IOWR(KVMIO, 0x02, struct kvm_msr_list) #define KVM_S390_ENABLE_SIE _IO(KVMIO, 0x06) @@ -892,6 +899,7 @@ struct kvm_ppc_resize_hpt { #define KVM_CAP_MIPS_64BIT 139 #define KVM_CAP_S390_GS 140 #define KVM_CAP_S390_AIS 141 +#define KVM_CAP_CONFIGURABLE_MAX_VCPUS 142 #ifdef KVM_CAP_IRQ_ROUTING diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index 0f1579f118b4..9ef52fa006ec 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -629,11 +629,20 @@ static inline void kvm_free_vm(struct kvm *kvm) kfree(kvm); } -static struct kvm *kvm_create_vm(unsigned long type) +static struct kvm *kvm_create_vm(struct kvm_vm_config *vm_config) { int r, i; - struct kvm *kvm = kvm_alloc_vm(KVM_MAX_VCPUS); + struct kvm *kvm; + if (!KVM_CONFIGURABLE_MAX_VCPUS && vm_config->max_vcpus) + return ERR_PTR(-EINVAL); + if (vm_config->max_vcpus > KVM_CONFIGURABLE_MAX_VCPUS) + return ERR_PTR(-E2BIG); + + if (!vm_config->max_vcpus) + vm_config->max_vcpus = KVM_MAX_VCPUS; + + kvm = kvm_alloc_vm(vm_config->max_vcpus); if (!kvm) return ERR_PTR(-ENOMEM); @@ -647,7 +656,7 @@ static struct kvm *kvm_create_vm(unsigned long type) refcount_set(&kvm->users_count, 1); INIT_LIST_HEAD(&kvm->devices); - r = kvm_arch_init_vm(kvm, type); + r = kvm_arch_init_vm(kvm, vm_config->type); if (r) goto out_err_no_disable; @@ -2957,6 +2966,8 @@ static long kvm_vm_ioctl_check_extension_generic(struct kvm *kvm, long arg) #endif case KVM_CAP_MAX_VCPU_ID: return KVM_MAX_VCPU_ID; + case KVM_CAP_CONFIGURABLE_MAX_VCPUS: + return KVM_CONFIGURABLE_MAX_VCPUS; default: break; } @@ -3182,13 +3193,13 @@ static struct file_operations kvm_vm_fops = { .llseek = noop_llseek, }; -static int kvm_dev_ioctl_create_vm(unsigned long type) +static int kvm_dev_ioctl_create_vm(struct kvm_vm_config *vm_config) { int r; struct kvm *kvm; struct file *file; - kvm = kvm_create_vm(type); + kvm = kvm_create_vm(vm_config); if (IS_ERR(kvm)) return PTR_ERR(kvm); #ifdef CONFIG_KVM_MMIO @@ -3223,6 +3234,7 @@ static int kvm_dev_ioctl_create_vm(unsigned long type) static long kvm_dev_ioctl(struct file *filp, unsigned int ioctl, unsigned long arg) { + void __user *argp = (void __user *)arg; long r = -EINVAL; switch (ioctl) { @@ -3231,9 +3243,28 @@ static long kvm_dev_ioctl(struct file *filp, goto out; r = KVM_API_VERSION; break; - case KVM_CREATE_VM: - r = kvm_dev_ioctl_create_vm(arg); + case KVM_CREATE_VM: { + struct kvm_vm_config vm_config = {.type = arg}; + + r = kvm_dev_ioctl_create_vm(&vm_config); break; + } + case KVM_CREATE_VM2: { + struct kvm_vm_config vm_config, check_reserved = {}; + + r = -EFAULT; + if (copy_from_user(&vm_config, argp, sizeof vm_config)) + goto out; + + r = -EINVAL; + check_reserved.type = vm_config.type; + check_reserved.max_vcpus = vm_config.max_vcpus; + if (memcmp(&vm_config, &check_reserved, sizeof check_reserved)) + goto out; + + r = kvm_dev_ioctl_create_vm(&vm_config); + break; + } case KVM_CHECK_EXTENSION: r = kvm_vm_ioctl_check_extension_generic(NULL, arg); break;