From patchwork Mon Oct 31 06:35:05 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jike Song X-Patchwork-Id: 9405073 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 AC0B06022E for ; Mon, 31 Oct 2016 06:40:39 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 9DC3A2912B for ; Mon, 31 Oct 2016 06:40:39 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 92A5C29133; Mon, 31 Oct 2016 06:40:39 +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 331FF2912B for ; Mon, 31 Oct 2016 06:40:39 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1761216AbcJaGkf (ORCPT ); Mon, 31 Oct 2016 02:40:35 -0400 Received: from mga09.intel.com ([134.134.136.24]:46459 "EHLO mga09.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1761103AbcJaGkd (ORCPT ); Mon, 31 Oct 2016 02:40:33 -0400 Received: from orsmga004.jf.intel.com ([10.7.209.38]) by orsmga102.jf.intel.com with ESMTP; 30 Oct 2016 23:40:33 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.31,573,1473145200"; d="scan'208";a="25931448" Received: from kvmgt1.bj.intel.com ([10.238.154.158]) by orsmga004.jf.intel.com with ESMTP; 30 Oct 2016 23:40:30 -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: [v3 4/5] vfio: implement APIs to set/put kvm to/from vfio group Date: Mon, 31 Oct 2016 14:35:05 +0800 Message-Id: <1477895706-22824-5-git-send-email-jike.song@intel.com> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1477895706-22824-1-git-send-email-jike.song@intel.com> References: <1477895706-22824-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 A vfio_group may be or may not be attached to a KVM instance, if it is, the user of vfio_group might also want to know which KVM instance it is attached to, to utilize features provided by KVM. In VFIO there are already external APIs for KVM to get/put the vfio_group, by extending that, KVM can set or clear itself to/from the vfio_group, for external users to use. Signed-off-by: Jike Song --- drivers/vfio/vfio.c | 30 ++++++++++++++++++++++++++++++ include/linux/vfio.h | 4 ++++ 2 files changed, 34 insertions(+) diff --git a/drivers/vfio/vfio.c b/drivers/vfio/vfio.c index e3e58e3..41611cc 100644 --- a/drivers/vfio/vfio.c +++ b/drivers/vfio/vfio.c @@ -34,6 +34,7 @@ #include #include #include +#include #define DRIVER_VERSION "0.3" #define DRIVER_AUTHOR "Alex Williamson " @@ -86,6 +87,10 @@ struct vfio_group { struct mutex unbound_lock; atomic_t opened; bool noiommu; + struct { + struct kvm *kvm; + struct mutex lock; + } udata; }; struct vfio_device { @@ -333,6 +338,7 @@ static struct vfio_group *vfio_create_group(struct iommu_group *iommu_group) mutex_init(&group->device_lock); INIT_LIST_HEAD(&group->unbound_list); mutex_init(&group->unbound_lock); + mutex_init(&group->udata.lock); atomic_set(&group->container_users, 0); atomic_set(&group->opened, 0); group->iommu_group = iommu_group; @@ -1739,6 +1745,30 @@ long vfio_external_check_extension(struct vfio_group *group, unsigned long arg) } EXPORT_SYMBOL_GPL(vfio_external_check_extension); +void vfio_group_set_kvm(struct vfio_group *group, struct kvm *kvm) +{ + mutex_lock(&group->udata.lock); + group->udata.kvm = kvm; + mutex_unlock(&group->udata.lock); +} +EXPORT_SYMBOL_GPL(vfio_group_set_kvm); + +struct kvm *vfio_group_get_kvm(struct vfio_group *group) +{ + struct kvm *kvm = NULL; + + mutex_lock(&group->udata.lock); + + kvm = group->udata.kvm; + if (kvm) + kvm_get_kvm(kvm); + + mutex_unlock(&group->udata.lock); + + return kvm; +} +EXPORT_SYMBOL_GPL(vfio_group_get_kvm); + /** * Sub-module support */ diff --git a/include/linux/vfio.h b/include/linux/vfio.h index ad9b857..3abd690 100644 --- a/include/linux/vfio.h +++ b/include/linux/vfio.h @@ -95,6 +95,10 @@ extern long vfio_external_check_extension(struct vfio_group *group, extern struct vfio_group *vfio_group_get_from_dev(struct device *dev); extern void vfio_group_put(struct vfio_group *group); +struct kvm; +extern void vfio_group_set_kvm(struct vfio_group *group, struct kvm *kvm); +extern struct kvm *vfio_group_get_kvm(struct vfio_group *group); + /* * Sub-module helpers */