From patchwork Tue May 15 11:37:37 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marc Orr X-Patchwork-Id: 10400833 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 714CF601D2 for ; Tue, 15 May 2018 11:38:31 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 46A5A2787C for ; Tue, 15 May 2018 11:38:31 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 38B34285A5; Tue, 15 May 2018 11:38:31 +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=-15.5 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI, USER_IN_DEF_DKIM_WL 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 A4EA52787C for ; Tue, 15 May 2018 11:38:30 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752833AbeEOLi2 (ORCPT ); Tue, 15 May 2018 07:38:28 -0400 Received: from mail-pl0-f68.google.com ([209.85.160.68]:38031 "EHLO mail-pl0-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752740AbeEOLi1 (ORCPT ); Tue, 15 May 2018 07:38:27 -0400 Received: by mail-pl0-f68.google.com with SMTP id c11-v6so9274088plr.5 for ; Tue, 15 May 2018 04:38:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=from:to:cc:subject:date:message-id; bh=6df0gAhj84o4FvC3lSFvdQG+lGDpZxRcDh8Zrchz7fU=; b=iF1zgY1/aYOWtwYyZSqGIsHMfnGuN+r72BA4XApEXDLPOI820XIjCC7abpQ5ZzR9bS 4gLYsVogDnWb5iPIGOTA95PTBhHioX6lmJvO9Aj7Ev1PXu06vSK29RS0BBpbyYe3uRcO C4FzXnPZnmhLCzNoJxAxm7ljbfkU5jzpjrOK07VFvQIXwDoBpIdOb8OuEiMoE4KywjSO g4T3IxWZpcsgNkyrRQLiiGII3uCN5PHcNi18Wla6KVooMQjYyKi3fWaiJ4QlIqnR6JnG dM0FVa3/dhKaKseFmAjS08F7cTc30GghKAlaXJWVT+70GW/28cG26t8tjDEak1ZXG+6B M1VA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=6df0gAhj84o4FvC3lSFvdQG+lGDpZxRcDh8Zrchz7fU=; b=PyKjc6dnJXFFAO5EXpivHfixo9rpCiMUzyRlZ/oSHbP014Caxi91DFv2fHJRBWdHbn j7UQzcaqwCp+kGeKw5S+3CCMPqmRWJp5Lz2Sj15PKxB6eS2nFn6ZmTH27uMPFtdYeat4 OnAa9BfvSBMNpog3ZrHrw8rIxd2PFCEXhhGb+Fc/DCR1q8ocPIzZ8Lc1PKR+ZkRPJKES XRrF8f+BFej3YK1VI0FZROMtpmhcx6TPCJLOIgReukp/TS9Rya6Ke9dq4H0k/fTVamc3 pQHk7UtN1sUU2OsH4/Wsa5bmHAI9p660I4q7iKz/JbwCzgsOJRJ3J89STyZIvKzXCbtQ BXdQ== X-Gm-Message-State: ALKqPwc7qIZifhLDsNKi4KDormRtiZwe3ysaPgKdGAHBHJXvop079MHr 0YT2uwk1yhftdGesIXPdOcPZws6YRog= X-Google-Smtp-Source: AB8JxZp41s8sHTNfYnmklOadl3L+Uh7naEljN1RgqXh7/ee9XOICq/+x3Exk48+NbM4KAVrDg/fDFw== X-Received: by 2002:a17:902:6113:: with SMTP id t19-v6mr13839682plj.372.1526384306633; Tue, 15 May 2018 04:38:26 -0700 (PDT) Received: from marcorr-linuxworkstation.sea.corp.google.com ([2620:0:1009:11:58fe:9c9a:2c6c:55d0]) by smtp.gmail.com with ESMTPSA id o88-v6sm22765112pfa.29.2018.05.15.04.38.25 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 15 May 2018 04:38:25 -0700 (PDT) From: Marc Orr To: kvm@vger.kernel.org Cc: marc.zyngier@arm.com, pbonzini@redhat.com, borntraeger@de.ibm.com, paulus@ozlabs.org, frankja@linux.ibm.com, cohuck@redhat.com, David Rientjes , rkrcmar@redhat.com, David Matlack , Jim Mattson , frankja@linux.vnet.ibm.com, jhogan@kernel.org, christoffer.dall@arm.com, Marc Orr Subject: [kvm PATCH v5 1/1] kvm: Make VM ioctl do valloc for some archs Date: Tue, 15 May 2018 04:37:37 -0700 Message-Id: <20180515113737.170480-1-marcorr@google.com> X-Mailer: git-send-email 2.17.0.441.gb46fe60e1d-goog Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP The kvm struct has been bloating. For example, it's tens of kilo-bytes for x86, which turns out to be a large amount of memory to allocate contiguously via kzalloc. Thus, this patch does the following: 1. Uses architecture-specific routines to allocat the kvm struct via kzalloc for x86 and also for arm when has_tbe() is true. 2. Introduces a baseline allocator for the kvm struct that uses valloc. This allocator can be used by an architecture by defining the __KVM_VALLOC_ARCH_VM macro before including include/linux/kvm_host.h. 3. Finally, continue to default to kalloc for all other situations, as many architectures have taken a dependence on kalloc. Signed-off-by: Marc Orr Change-Id: Ide392e51991142d1325e06351231ac685e11d820 Reviewed-by: Marc Zyngier --- arch/arm/include/asm/kvm_host.h | 4 ++++ arch/arm64/include/asm/kvm_host.h | 4 ++++ arch/x86/kvm/svm.c | 4 ++-- arch/x86/kvm/vmx.c | 4 ++-- include/linux/kvm_host.h | 13 +++++++++++++ virt/kvm/arm/arm.c | 15 +++++++++++++++ 6 files changed, 40 insertions(+), 4 deletions(-) diff --git a/arch/arm/include/asm/kvm_host.h b/arch/arm/include/asm/kvm_host.h index c7c28c885a19..f38fd60b4d4d 100644 --- a/arch/arm/include/asm/kvm_host.h +++ b/arch/arm/include/asm/kvm_host.h @@ -318,4 +318,8 @@ static inline bool kvm_arm_harden_branch_predictor(void) static inline void kvm_vcpu_load_sysregs(struct kvm_vcpu *vcpu) {} static inline void kvm_vcpu_put_sysregs(struct kvm_vcpu *vcpu) {} +#define __KVM_HAVE_ARCH_VM_ALLOC +struct kvm *kvm_arch_alloc_vm(void); +void kvm_arch_free_vm(struct kvm *kvm); + #endif /* __ARM_KVM_HOST_H__ */ diff --git a/arch/arm64/include/asm/kvm_host.h b/arch/arm64/include/asm/kvm_host.h index 469de8acd06f..358b1b4dcd92 100644 --- a/arch/arm64/include/asm/kvm_host.h +++ b/arch/arm64/include/asm/kvm_host.h @@ -455,4 +455,8 @@ static inline bool kvm_arm_harden_branch_predictor(void) void kvm_vcpu_load_sysregs(struct kvm_vcpu *vcpu); void kvm_vcpu_put_sysregs(struct kvm_vcpu *vcpu); +#define __KVM_HAVE_ARCH_VM_ALLOC +struct kvm *kvm_arch_alloc_vm(void); +void kvm_arch_free_vm(struct kvm *kvm); + #endif /* __ARM64_KVM_HOST_H__ */ diff --git a/arch/x86/kvm/svm.c b/arch/x86/kvm/svm.c index 1fc05e428aba..45b903ed3e5d 100644 --- a/arch/x86/kvm/svm.c +++ b/arch/x86/kvm/svm.c @@ -1849,13 +1849,13 @@ static void __unregister_enc_region_locked(struct kvm *kvm, static struct kvm *svm_vm_alloc(void) { - struct kvm_svm *kvm_svm = kzalloc(sizeof(struct kvm_svm), GFP_KERNEL); + struct kvm_svm *kvm_svm = vzalloc(sizeof(struct kvm_svm)); return &kvm_svm->kvm; } static void svm_vm_free(struct kvm *kvm) { - kfree(to_kvm_svm(kvm)); + vfree(to_kvm_svm(kvm)); } static void sev_vm_destroy(struct kvm *kvm) diff --git a/arch/x86/kvm/vmx.c b/arch/x86/kvm/vmx.c index 3f1696570b41..ee85ba887db6 100644 --- a/arch/x86/kvm/vmx.c +++ b/arch/x86/kvm/vmx.c @@ -9936,13 +9936,13 @@ STACK_FRAME_NON_STANDARD(vmx_vcpu_run); static struct kvm *vmx_vm_alloc(void) { - struct kvm_vmx *kvm_vmx = kzalloc(sizeof(struct kvm_vmx), GFP_KERNEL); + struct kvm_vmx *kvm_vmx = vzalloc(sizeof(struct kvm_vmx)); return &kvm_vmx->kvm; } static void vmx_vm_free(struct kvm *kvm) { - kfree(to_kvm_vmx(kvm)); + vfree(to_kvm_vmx(kvm)); } static void vmx_switch_vmcs(struct kvm_vcpu *vcpu, struct loaded_vmcs *vmcs) diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index 6d6e79c59e68..e3134283321a 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -19,6 +19,7 @@ #include #include #include +#include #include #include #include @@ -808,6 +809,17 @@ bool kvm_arch_vcpu_in_kernel(struct kvm_vcpu *vcpu); int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu); #ifndef __KVM_HAVE_ARCH_VM_ALLOC +#ifdef __KVM_VALLOC_ARCH_VM +static inline struct kvm *kvm_arch_alloc_vm(void) +{ + return vzalloc(sizeof(struct kvm)); +} + +static inline void kvm_arch_free_vm(struct kvm *kvm) +{ + vfree(kvm); +} +#else static inline struct kvm *kvm_arch_alloc_vm(void) { return kzalloc(sizeof(struct kvm), GFP_KERNEL); @@ -818,6 +830,7 @@ static inline void kvm_arch_free_vm(struct kvm *kvm) kfree(kvm); } #endif +#endif #ifdef __KVM_HAVE_ARCH_NONCOHERENT_DMA void kvm_arch_register_noncoherent_dma(struct kvm *kvm); diff --git a/virt/kvm/arm/arm.c b/virt/kvm/arm/arm.c index a4c1b76240df..ee96f18f852a 100644 --- a/virt/kvm/arm/arm.c +++ b/virt/kvm/arm/arm.c @@ -249,6 +249,21 @@ long kvm_arch_dev_ioctl(struct file *filp, return -EINVAL; } +struct kvm *kvm_arch_alloc_vm(void) +{ + if (!has_vhe()) + return kzalloc(sizeof(struct kvm), GFP_KERNEL); + + return vzalloc(sizeof(struct kvm)); +} + +void kvm_arch_free_vm(struct kvm *kvm) +{ + if (!has_vhe()) + kfree(kvm); + else + vfree(kvm); +} struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm, unsigned int id) {