From patchwork Thu Dec 1 05:20: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: 9455425 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 A1F4160235 for ; Thu, 1 Dec 2016 05:27:58 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 94F0C2846D for ; Thu, 1 Dec 2016 05:27:58 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 89EBB28497; Thu, 1 Dec 2016 05:27:58 +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 92DE72846D for ; Thu, 1 Dec 2016 05:27:56 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1758711AbcLAF1w (ORCPT ); Thu, 1 Dec 2016 00:27:52 -0500 Received: from mga07.intel.com ([134.134.136.100]:35445 "EHLO mga07.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750848AbcLAF1w (ORCPT ); Thu, 1 Dec 2016 00:27:52 -0500 Received: from orsmga001.jf.intel.com ([10.7.209.18]) by orsmga105.jf.intel.com with ESMTP; 30 Nov 2016 21:27:51 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.33,723,1477983600"; d="scan'208";a="1066482970" Received: from kvmgt1.bj.intel.com ([10.238.154.158]) by orsmga001.jf.intel.com with ESMTP; 30 Nov 2016 21:27:49 -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 Subject: [v8 1/3] vfio: vfio_register_notifier: classify iommu notifier Date: Thu, 1 Dec 2016 13:20:05 +0800 Message-Id: <1480569607-31190-2-git-send-email-jike.song@intel.com> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1480569607-31190-1-git-send-email-jike.song@intel.com> References: <1480569607-31190-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 Currently vfio_register_notifier assumes that there is only one notifier chain, which is in vfio_iommu. However, the user might also be interested in events other than vfio_iommu, for example, vfio_group. Refactor vfio_{un}register_notifier implementation to make it feasible. Cc: Alex Williamson Cc: Paolo Bonzini Cc: Xiao Guangrong Reviewed-by: Kirti Wankhede Signed-off-by: Jike Song --- drivers/vfio/vfio.c | 85 +++++++++++++++++++++++++++++------------ drivers/vfio/vfio_iommu_type1.c | 8 ++++ include/linux/vfio.h | 17 +++++++-- 3 files changed, 82 insertions(+), 28 deletions(-) diff --git a/drivers/vfio/vfio.c b/drivers/vfio/vfio.c index 0aac3ca..cb2ce2d 100644 --- a/drivers/vfio/vfio.c +++ b/drivers/vfio/vfio.c @@ -2008,59 +2008,44 @@ int vfio_unpin_pages(struct device *dev, unsigned long *user_pfn, int npage) } EXPORT_SYMBOL(vfio_unpin_pages); -int vfio_register_notifier(struct device *dev, struct notifier_block *nb) +static int vfio_register_iommu_notifier(struct vfio_group *group, + unsigned long *events, + struct notifier_block *nb) { struct vfio_container *container; - struct vfio_group *group; struct vfio_iommu_driver *driver; int ret; - if (!dev || !nb) - return -EINVAL; - - group = vfio_group_get_from_dev(dev); - if (IS_ERR(group)) - return PTR_ERR(group); - ret = vfio_group_add_container_user(group); if (ret) - goto err_register_nb; + return -EINVAL; container = group->container; down_read(&container->group_lock); driver = container->iommu_driver; if (likely(driver && driver->ops->register_notifier)) - ret = driver->ops->register_notifier(container->iommu_data, nb); + ret = driver->ops->register_notifier(container->iommu_data, + events, nb); else ret = -ENOTTY; up_read(&container->group_lock); vfio_group_try_dissolve_container(group); -err_register_nb: - vfio_group_put(group); return ret; } -EXPORT_SYMBOL(vfio_register_notifier); -int vfio_unregister_notifier(struct device *dev, struct notifier_block *nb) +static int vfio_unregister_iommu_notifier(struct vfio_group *group, + struct notifier_block *nb) { struct vfio_container *container; - struct vfio_group *group; struct vfio_iommu_driver *driver; int ret; - if (!dev || !nb) - return -EINVAL; - - group = vfio_group_get_from_dev(dev); - if (IS_ERR(group)) - return PTR_ERR(group); - ret = vfio_group_add_container_user(group); if (ret) - goto err_unregister_nb; + return -EINVAL; container = group->container; down_read(&container->group_lock); @@ -2075,7 +2060,57 @@ int vfio_unregister_notifier(struct device *dev, struct notifier_block *nb) up_read(&container->group_lock); vfio_group_try_dissolve_container(group); -err_unregister_nb: + return ret; +} + +int vfio_register_notifier(struct device *dev, vfio_notify_type_t type, + unsigned long *events, + struct notifier_block *nb) +{ + struct vfio_group *group; + int ret; + + if (!dev || !nb || !events || (*events == 0)) + return -EINVAL; + + group = vfio_group_get_from_dev(dev); + if (IS_ERR(group)) + return PTR_ERR(group); + + switch (type) { + case VFIO_IOMMU_NOTIFY: + ret = vfio_register_iommu_notifier(group, events, nb); + break; + default: + ret = -EINVAL; + } + + vfio_group_put(group); + return ret; +} +EXPORT_SYMBOL(vfio_register_notifier); + +int vfio_unregister_notifier(struct device *dev, vfio_notify_type_t type, + struct notifier_block *nb) +{ + struct vfio_group *group; + int ret; + + if (!dev || !nb) + return -EINVAL; + + group = vfio_group_get_from_dev(dev); + if (IS_ERR(group)) + return PTR_ERR(group); + + switch (type) { + case VFIO_IOMMU_NOTIFY: + ret = vfio_unregister_iommu_notifier(group, nb); + break; + default: + ret = -EINVAL; + } + vfio_group_put(group); return ret; } diff --git a/drivers/vfio/vfio_iommu_type1.c b/drivers/vfio/vfio_iommu_type1.c index 51810a9..b88ad1e 100644 --- a/drivers/vfio/vfio_iommu_type1.c +++ b/drivers/vfio/vfio_iommu_type1.c @@ -1584,10 +1584,18 @@ static long vfio_iommu_type1_ioctl(void *iommu_data, } static int vfio_iommu_type1_register_notifier(void *iommu_data, + unsigned long *events, struct notifier_block *nb) { struct vfio_iommu *iommu = iommu_data; + /* clear known events */ + *events &= ~VFIO_IOMMU_NOTIFY_DMA_UNMAP; + + /* refuse to register if still events remaining */ + if (*events) + return -EINVAL; + return blocking_notifier_chain_register(&iommu->notifier, nb); } diff --git a/include/linux/vfio.h b/include/linux/vfio.h index 15ff042..fdb8460 100644 --- a/include/linux/vfio.h +++ b/include/linux/vfio.h @@ -81,6 +81,7 @@ struct vfio_iommu_driver_ops { int (*unpin_pages)(void *iommu_data, unsigned long *user_pfn, int npage); int (*register_notifier)(void *iommu_data, + unsigned long *events, struct notifier_block *nb); int (*unregister_notifier)(void *iommu_data, struct notifier_block *nb); @@ -107,14 +108,24 @@ extern int vfio_pin_pages(struct device *dev, unsigned long *user_pfn, extern int vfio_unpin_pages(struct device *dev, unsigned long *user_pfn, int npage); -#define VFIO_IOMMU_NOTIFY_DMA_UNMAP (1) - +typedef unsigned short vfio_notify_type_t; extern int vfio_register_notifier(struct device *dev, + vfio_notify_type_t type, + unsigned long *required_events, struct notifier_block *nb); - extern int vfio_unregister_notifier(struct device *dev, + vfio_notify_type_t type, struct notifier_block *nb); +/* each type has independent events */ +enum vfio_notify_type { + VFIO_IOMMU_NOTIFY = (__force vfio_notify_type_t)0, +}; + +/* events for VFIO_IOMMU_NOTIFY */ +#define VFIO_IOMMU_NOTIFY_DMA_UNMAP BIT(0) + + /* * Sub-module helpers */