From patchwork Tue Nov 15 11:35:45 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jike Song X-Patchwork-Id: 9429509 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 BA72F60471 for ; Tue, 15 Nov 2016 11:42:36 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id A60A7286EC for ; Tue, 15 Nov 2016 11:42:36 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 9ABBE28B9A; Tue, 15 Nov 2016 11:42:36 +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 35A04286EC for ; Tue, 15 Nov 2016 11:42:36 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753056AbcKOLmb (ORCPT ); Tue, 15 Nov 2016 06:42:31 -0500 Received: from mga09.intel.com ([134.134.136.24]:6706 "EHLO mga09.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752603AbcKOLma (ORCPT ); Tue, 15 Nov 2016 06:42:30 -0500 Received: from orsmga002.jf.intel.com ([10.7.209.21]) by orsmga102.jf.intel.com with ESMTP; 15 Nov 2016 03:42:30 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.31,494,1473145200"; d="scan'208";a="4436488" Received: from kvmgt1.bj.intel.com ([10.238.154.158]) by orsmga002.jf.intel.com with ESMTP; 15 Nov 2016 03:42:27 -0800 From: Jike Song To: alex.williamson@redhat.com, pbonzini@redhat.com, guangrong.xiao@linux.intel.com Cc: kwankhede@nvidia.com, cjia@nvidia.com, kevin.tian@intel.com, kvm@vger.kernel.org, jike.song@intel.com Subject: [v4 1/3] vfio: add vfio_group_notify support Date: Tue, 15 Nov 2016 19:35:45 +0800 Message-Id: <1479209747-5564-2-git-send-email-jike.song@intel.com> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1479209747-5564-1-git-send-email-jike.song@intel.com> References: <1479209747-5564-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, and when it will detach. In VFIO there are already external APIs for KVM to get/put vfio_group, by providing a similar vfio_group_notify, KVM can notify the vfio_group about attaching events. Cc: Xiao Guangrong Cc: Paolo Bonzini Cc: Alex Williamson 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 0afb58e..b149ced 100644 --- a/drivers/vfio/vfio.c +++ b/drivers/vfio/vfio.c @@ -86,6 +86,8 @@ struct vfio_group { struct mutex unbound_lock; atomic_t opened; bool noiommu; + struct kvm *kvm; + struct blocking_notifier_head notifier; }; struct vfio_device { @@ -1015,6 +1017,34 @@ static long vfio_ioctl_check_extension(struct vfio_container *container, return ret; } +void vfio_group_notify(struct vfio_group *group, struct kvm *kvm) +{ + group->kvm = kvm; + blocking_notifier_call_chain(&group->notifier, + VFIO_GROUP_NOTIFY_SET_KVM, kvm); +} +EXPORT_SYMBOL_GPL(vfio_group_notify); + +static void vfio_group_register_notifier(struct vfio_group *group, + struct notifier_block *nb) +{ + blocking_notifier_chain_register(&group->notifier, nb); + + /* + * The attaching of kvm and vfio_group might already happen, so + * here we replay once on registration. + */ + if (group->kvm) + blocking_notifier_call_chain(&group->notifier, + VFIO_GROUP_NOTIFY_SET_KVM, group->kvm); +} + +static void vfio_group_unregister_notifier(struct vfio_group *group, + struct notifier_block *nb) +{ + blocking_notifier_chain_unregister(&group->notifier, nb); +} + /* hold write lock on container->group_lock */ static int __vfio_container_attach_groups(struct vfio_container *container, struct vfio_iommu_driver *driver, diff --git a/include/linux/vfio.h b/include/linux/vfio.h index d4ce14d..ec9f74f 100644 --- a/include/linux/vfio.h +++ b/include/linux/vfio.h @@ -100,6 +100,9 @@ extern void vfio_unregister_iommu_driver( extern long vfio_external_check_extension(struct vfio_group *group, unsigned long arg); +struct kvm; +extern void vfio_group_notify(struct vfio_group *group, struct kvm *kvm); + /* * Sub-module helpers */ @@ -149,6 +152,7 @@ extern int vfio_unpin_pages(struct device *dev, unsigned long *user_pfn, int npage); #define VFIO_IOMMU_NOTIFY_DMA_UNMAP 1 +#define VFIO_GROUP_NOTIFY_SET_KVM 2 extern int vfio_register_notifier(struct device *dev, struct notifier_block *nb);