From patchwork Sat Oct 29 20:05:13 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jike Song X-Patchwork-Id: 9404091 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 E3A2160588 for ; Sat, 29 Oct 2016 20:11:20 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id D6E20291BB for ; Sat, 29 Oct 2016 20:11:20 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id CBDEF291C0; Sat, 29 Oct 2016 20:11:20 +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,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 7C854291BB for ; Sat, 29 Oct 2016 20:11:20 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754495AbcJ2ULQ (ORCPT ); Sat, 29 Oct 2016 16:11:16 -0400 Received: from mga02.intel.com ([134.134.136.20]:40163 "EHLO mga02.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1754424AbcJ2ULP (ORCPT ); Sat, 29 Oct 2016 16:11:15 -0400 Received: from orsmga004.jf.intel.com ([10.7.209.38]) by orsmga101.jf.intel.com with ESMTP; 29 Oct 2016 13:11:15 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.31,565,1473145200"; d="scan'208";a="25569161" Received: from kvmgt1.bj.intel.com ([10.238.154.158]) by orsmga004.jf.intel.com with ESMTP; 29 Oct 2016 13:11:13 -0700 From: Jike Song To: pbonzini@redhat.com, alex.williamson@redhat.com, guangrong.xiao@linux.intel.com Cc: kwankhede@nvidia.com, cjia@nvidia.com, kevin.tian@intel.com, jike.song@intel.com, kvm@vger.kernel.org Subject: [v2 3/5] KVM: move kvm_{get|put}_kvm to kvm_host.h Date: Sun, 30 Oct 2016 04:05:13 +0800 Message-Id: <1477771515-18015-4-git-send-email-jike.song@intel.com> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1477771515-18015-1-git-send-email-jike.song@intel.com> References: <1477771515-18015-1-git-send-email-jike.song@intel.com> Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP So that external users like vfio can call them without introducing symbol-level dependency. A destroy() method is introduced to the kvm structure so that we don't have to also expose kvm_destroy_vm. Signed-off-by: Jike Song --- include/linux/kvm_host.h | 13 +++++++++++-- virt/kvm/kvm_main.c | 16 ++-------------- 2 files changed, 13 insertions(+), 16 deletions(-) diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index 01c0b9c..583e9e2 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -405,6 +405,7 @@ struct kvm { struct kvm_vm_stat stat; struct kvm_arch arch; atomic_t users_count; + void (*destroy)(struct kvm *kvm); #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET struct kvm_coalesced_mmio_ring *coalesced_mmio_ring; spinlock_t ring_lock; @@ -526,8 +527,16 @@ int kvm_init(void *opaque, unsigned vcpu_size, unsigned vcpu_align, struct module *module); void kvm_exit(void); -void kvm_get_kvm(struct kvm *kvm); -void kvm_put_kvm(struct kvm *kvm); +static inline void kvm_get_kvm(struct kvm *kvm) +{ + atomic_inc(&kvm->users_count); +} + +static inline void kvm_put_kvm(struct kvm *kvm) +{ + if (atomic_dec_and_test(&kvm->users_count)) + kvm->destroy(kvm); +} static inline struct kvm_memslots *__kvm_memslots(struct kvm *kvm, int as_id) { diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index 81dfc73..996a88d 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -106,6 +106,7 @@ static int kvm_debugfs_num_entries; static const struct file_operations *stat_fops_per_vm[]; +static void kvm_destroy_vm(struct kvm *kvm); static long kvm_vcpu_ioctl(struct file *file, unsigned int ioctl, unsigned long arg); #ifdef CONFIG_KVM_COMPAT @@ -620,6 +621,7 @@ static struct kvm *kvm_create_vm(unsigned long type) mutex_init(&kvm->irq_lock); mutex_init(&kvm->slots_lock); atomic_set(&kvm->users_count, 1); + kvm->destroy = kvm_destroy_vm; INIT_LIST_HEAD(&kvm->devices); r = kvm_arch_init_vm(kvm, type); @@ -740,20 +742,6 @@ static void kvm_destroy_vm(struct kvm *kvm) mmdrop(mm); } -void kvm_get_kvm(struct kvm *kvm) -{ - atomic_inc(&kvm->users_count); -} -EXPORT_SYMBOL_GPL(kvm_get_kvm); - -void kvm_put_kvm(struct kvm *kvm) -{ - if (atomic_dec_and_test(&kvm->users_count)) - kvm_destroy_vm(kvm); -} -EXPORT_SYMBOL_GPL(kvm_put_kvm); - - static int kvm_vm_release(struct inode *inode, struct file *filp) { struct kvm *kvm = filp->private_data;