From patchwork Mon Feb 12 18:33:17 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jean-Philippe Brucker X-Patchwork-Id: 10213901 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 81DED60153 for ; Mon, 12 Feb 2018 18:31:41 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 73BE828AB7 for ; Mon, 12 Feb 2018 18:31:41 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 67D8B28AF8; Mon, 12 Feb 2018 18:31:41 +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=-4.2 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,RCVD_IN_DNSWL_MED autolearn=unavailable version=3.3.1 Received: from casper.infradead.org (casper.infradead.org [85.118.1.10]) (using TLSv1.2 with cipher AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id E20F128AB7 for ; Mon, 12 Feb 2018 18:31:38 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=Sender:Content-Transfer-Encoding: Content-Type:MIME-Version:Cc:List-Subscribe:List-Help:List-Post:List-Archive: List-Unsubscribe:List-Id:References:In-Reply-To:Message-Id:Date:Subject:To: From:Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=rCyV1+kjOkzCe9P3MIHsDrOjn77X74U8p9WKHu9DOBk=; b=WvjICQSOPAsNmTeFjGGZ1Nt6pH ckB6bb3qw2hsTWEMQ20+ILXlSNA+/AAe1t74CbUQcSDqcgY5VGUXG+tbCs1kzSFO33xZbZSkdQqsU g5+sYkz4Tphq/zgdeobtI+T68qJsQTLfT2RsJQ+BdiYZiGv2fTznjn1k8euuDQXAQ7sJhkd0KL6ul EHiKXx+Le2ShKojeIPJNy251oafoAS/JpABeC9AaS9MZ6LnMwenoQ+9TJuTKoumpljx9IB9KfSON2 Kr/akyNOCElCchjZp1TRpQOxFRDeo4GX9htKwhFvri+0Bg5GgIIlNMtgz9be6M3Awm/OcHrjvh8h0 mBJDohTg==; Received: from [198.137.202.133] (helo=bombadil.infradead.org) by casper.infradead.org with esmtps (Exim 4.89 #1 (Red Hat Linux)) id 1elIsw-00054o-TH for patchwork-linux-arm@patchwork.kernel.org; Mon, 12 Feb 2018 18:31:27 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20170209; h=Sender: Content-Transfer-Encoding:Content-Type:MIME-Version:Cc:List-Subscribe: List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id:References: In-Reply-To:Message-Id:Date:Subject:To:From:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Owner; bh=rCyV1+kjOkzCe9P3MIHsDrOjn77X74U8p9WKHu9DOBk=; b=aI9Q/U9V5HiF45sBr6GSjq8m0R hgqir8iipWDjPNSQQQZhnn/LS4W1DRwBAa513ppFXB1xaC0CfnnbYCAKHTcBymvBVPN5IdJ3wxG+w Bl6cyoeGP7UTO1yoePiB2+qqONICbydJvCd6csIPcQkq5hoCxu5KUz3pdFkI0rFJzXbZscQ5cTdUS 87yIqNhOyPbPxk9/iPfgZC2VsHcW5etV8/SAKfddW1vhOM8aiTgENK6tbSw4xGVJjR2q0MKc7MLMT 8ck9Vy0t79UMJcV7dP4aZPZPsHFm2JlJ9s4PYhYYoKQ6/ZRPeRVGTV6EYEhvEbZLSNgDL1AAv5DNb rnvwKd+A==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.89 #1 (Red Hat Linux)) id 1elIso-0002dP-Eb; Mon, 12 Feb 2018 18:31:18 +0000 Received: from foss.arm.com ([217.140.101.70]) by bombadil.infradead.org with esmtp (Exim 4.89 #1 (Red Hat Linux)) id 1elIsk-0002Vj-3R for linux-arm-kernel@lists.infradead.org; Mon, 12 Feb 2018 18:31:15 +0000 Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.72.51.249]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id E7F6C165C; Mon, 12 Feb 2018 10:31:03 -0800 (PST) Received: from e106794-lin.cambridge.arm.com (e106794-lin.cambridge.arm.com [10.1.210.24]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id 16AFB3F24D; Mon, 12 Feb 2018 10:30:57 -0800 (PST) From: Jean-Philippe Brucker To: linux-arm-kernel@lists.infradead.org, linux-pci@vger.kernel.org, linux-acpi@vger.kernel.org, devicetree@vger.kernel.org, iommu@lists.linux-foundation.org, kvm@vger.kernel.org Subject: [PATCH 02/37] iommu/sva: Bind process address spaces to devices Date: Mon, 12 Feb 2018 18:33:17 +0000 Message-Id: <20180212183352.22730-3-jean-philippe.brucker@arm.com> X-Mailer: git-send-email 2.15.1 In-Reply-To: <20180212183352.22730-1-jean-philippe.brucker@arm.com> References: <20180212183352.22730-1-jean-philippe.brucker@arm.com> X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: mark.rutland@arm.com, xieyisheng1@huawei.com, ilias.apalodimas@linaro.org, catalin.marinas@arm.com, xuzaibo@huawei.com, jonathan.cameron@huawei.com, will.deacon@arm.com, okaya@codeaurora.org, yi.l.liu@intel.com, lorenzo.pieralisi@arm.com, ashok.raj@intel.com, tn@semihalf.com, joro@8bytes.org, bharatku@xilinx.com, rfranz@cavium.com, lenb@kernel.org, jacob.jun.pan@linux.intel.com, alex.williamson@redhat.com, robh+dt@kernel.org, thunder.leizhen@huawei.com, bhelgaas@google.com, shunyong.yang@hxt-semitech.com, dwmw2@infradead.org, liubo95@huawei.com, rjw@rjwysocki.net, jcrouse@codeaurora.org, robdclark@gmail.com, hanjun.guo@linaro.org, sudeep.holla@arm.com, robin.murphy@arm.com, christian.koenig@amd.com, nwatters@codeaurora.org MIME-Version: 1.0 Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org X-Virus-Scanned: ClamAV using ClamSMTP Add bind() and unbind() operations to the IOMMU API. Device drivers can use them to share process page tables with their devices. bind_group() is provided for VFIO's convenience, as it needs to provide a coherent interface on containers. Other device drivers will most likely want to use bind_device(), which binds a single device in the group. Regardless of the IOMMU group or domain a device is in, device drivers should call bind() for each device that will use the PASID. This patch only adds skeletons for the device driver API, most of the implementation is still missing. Signed-off-by: Jean-Philippe Brucker --- drivers/iommu/iommu-sva.c | 105 ++++++++++++++++++++++++++++++++++++++++++++++ drivers/iommu/iommu.c | 63 ++++++++++++++++++++++++++++ include/linux/iommu.h | 36 ++++++++++++++++ 3 files changed, 204 insertions(+) diff --git a/drivers/iommu/iommu-sva.c b/drivers/iommu/iommu-sva.c index cab5d723520f..593685d891bf 100644 --- a/drivers/iommu/iommu-sva.c +++ b/drivers/iommu/iommu-sva.c @@ -9,6 +9,9 @@ #include +/* TODO: stub for the fault queue. Remove later. */ +#define iommu_fault_queue_flush(...) + /** * iommu_sva_device_init() - Initialize Shared Virtual Addressing for a device * @dev: the device @@ -78,6 +81,8 @@ int iommu_sva_device_shutdown(struct device *dev) if (!domain) return -ENODEV; + __iommu_sva_unbind_dev_all(dev); + if (domain->ops->sva_device_shutdown) domain->ops->sva_device_shutdown(dev); @@ -88,3 +93,103 @@ int iommu_sva_device_shutdown(struct device *dev) return 0; } EXPORT_SYMBOL_GPL(iommu_sva_device_shutdown); + +/** + * iommu_sva_bind_device() - Bind a process address space to a device + * @dev: the device + * @mm: the mm to bind, caller must hold a reference to it + * @pasid: valid address where the PASID will be stored + * @flags: bond properties (IOMMU_SVA_FEAT_*) + * @drvdata: private data passed to the mm exit handler + * + * Create a bond between device and task, allowing the device to access the mm + * using the returned PASID. A subsequent bind() for the same device and mm will + * reuse the bond (and return the same PASID), but users will have to call + * unbind() twice. + * + * Callers should have taken care of setting up SVA for this device with + * iommu_sva_device_init() beforehand. They may also be notified of the bond + * disappearing, for example when the last task that uses the mm dies, by + * registering a notifier with iommu_register_mm_exit_handler(). + * + * If IOMMU_SVA_FEAT_PASID is requested, a PASID is allocated and returned. + * TODO: The alternative, binding the non-PASID context to an mm, isn't + * supported at the moment because existing IOMMU domain types initialize the + * non-PASID context for iommu_map()/unmap() or bypass. This requires a new + * domain type. + * + * If IOMMU_SVA_FEAT_IOPF is not requested, the caller must pin down all + * mappings shared with the device. mlock() isn't sufficient, as it doesn't + * prevent minor page faults (e.g. copy-on-write). TODO: !IOPF isn't allowed at + * the moment. + * + * On success, 0 is returned and @pasid contains a valid ID. Otherwise, an error + * is returned. + */ +int iommu_sva_bind_device(struct device *dev, struct mm_struct *mm, int *pasid, + unsigned long flags, void *drvdata) +{ + struct iommu_domain *domain; + struct iommu_param *dev_param = dev->iommu_param; + + domain = iommu_get_domain_for_dev(dev); + if (!domain) + return -EINVAL; + + if (!pasid) + return -EINVAL; + + if (!dev_param || (flags & ~dev_param->sva_features)) + return -EINVAL; + + if (flags != (IOMMU_SVA_FEAT_PASID | IOMMU_SVA_FEAT_IOPF)) + return -EINVAL; + + return -ENOSYS; /* TODO */ +} +EXPORT_SYMBOL_GPL(iommu_sva_bind_device); + +/** + * iommu_sva_unbind_device() - Remove a bond created with iommu_sva_bind_device + * @dev: the device + * @pasid: the pasid returned by bind() + * + * Remove bond between device and address space identified by @pasid. Users + * should not call unbind() if the corresponding mm exited (as the PASID might + * have been reallocated to another process.) + * + * The device must not be issuing any more transaction for this PASID. All + * outstanding page requests for this PASID must have been flushed to the IOMMU. + * + * Returns 0 on success, or an error value + */ +int iommu_sva_unbind_device(struct device *dev, int pasid) +{ + struct iommu_domain *domain; + + domain = iommu_get_domain_for_dev(dev); + if (WARN_ON(!domain)) + return -EINVAL; + + /* + * Caller stopped the device from issuing PASIDs, now make sure they are + * out of the fault queue. + */ + iommu_fault_queue_flush(dev); + + return -ENOSYS; /* TODO */ +} +EXPORT_SYMBOL_GPL(iommu_sva_unbind_device); + +/** + * __iommu_sva_unbind_dev_all() - Detach all address spaces from this device + * + * When detaching @device from a domain, IOMMU drivers should use this helper. + */ +void __iommu_sva_unbind_dev_all(struct device *dev) +{ + iommu_fault_queue_flush(dev); + + /* TODO */ +} +EXPORT_SYMBOL_GPL(__iommu_sva_unbind_dev_all); diff --git a/drivers/iommu/iommu.c b/drivers/iommu/iommu.c index d4a4edaf2d8c..f977851c522b 100644 --- a/drivers/iommu/iommu.c +++ b/drivers/iommu/iommu.c @@ -1535,6 +1535,69 @@ void iommu_detach_group(struct iommu_domain *domain, struct iommu_group *group) } EXPORT_SYMBOL_GPL(iommu_detach_group); +/* + * iommu_sva_bind_group() - Share address space with all devices in the group. + * @group: the iommu group + * @mm: the mm to bind + * @pasid: valid address where the PASID will be stored + * @flags: bond properties (IOMMU_PROCESS_BIND_*) + * @drvdata: private data passed to the mm exit handler + * + * Create a bond between group and process, allowing devices in the group to + * access the process address space using @pasid. + * + * Refer to iommu_sva_bind_device() for more details. + * + * On success, 0 is returned and @pasid contains a valid ID. Otherwise, an error + * is returned. + */ +int iommu_sva_bind_group(struct iommu_group *group, struct mm_struct *mm, + int *pasid, unsigned long flags, void *drvdata) +{ + struct group_device *device; + int ret = -ENODEV; + + if (!group->domain) + return -EINVAL; + + mutex_lock(&group->mutex); + list_for_each_entry(device, &group->devices, list) { + ret = iommu_sva_bind_device(device->dev, mm, pasid, flags, + drvdata); + if (ret) + break; + } + + if (ret) { + list_for_each_entry_continue_reverse(device, &group->devices, list) + iommu_sva_unbind_device(device->dev, *pasid); + } + mutex_unlock(&group->mutex); + + return ret; +} +EXPORT_SYMBOL_GPL(iommu_sva_bind_group); + +/** + * iommu_sva_unbind_group() - Remove a bond created with iommu_sva_bind_group() + * @group: the group + * @pasid: the pasid returned by bind + * + * Refer to iommu_sva_unbind_device() for more details. + */ +int iommu_sva_unbind_group(struct iommu_group *group, int pasid) +{ + struct group_device *device; + + mutex_lock(&group->mutex); + list_for_each_entry(device, &group->devices, list) + iommu_sva_unbind_device(device->dev, pasid); + mutex_unlock(&group->mutex); + + return 0; +} +EXPORT_SYMBOL_GPL(iommu_sva_unbind_group); + phys_addr_t iommu_iova_to_phys(struct iommu_domain *domain, dma_addr_t iova) { if (unlikely(domain->ops->iova_to_phys == NULL)) diff --git a/include/linux/iommu.h b/include/linux/iommu.h index e9e09eecdece..1fb10d64b9e5 100644 --- a/include/linux/iommu.h +++ b/include/linux/iommu.h @@ -576,6 +576,10 @@ int iommu_fwspec_init(struct device *dev, struct fwnode_handle *iommu_fwnode, void iommu_fwspec_free(struct device *dev); int iommu_fwspec_add_ids(struct device *dev, u32 *ids, int num_ids); const struct iommu_ops *iommu_ops_from_fwnode(struct fwnode_handle *fwnode); +extern int iommu_sva_bind_group(struct iommu_group *group, + struct mm_struct *mm, int *pasid, + unsigned long flags, void *drvdata); +extern int iommu_sva_unbind_group(struct iommu_group *group, int pasid); #else /* CONFIG_IOMMU_API */ @@ -890,12 +894,28 @@ const struct iommu_ops *iommu_ops_from_fwnode(struct fwnode_handle *fwnode) return NULL; } +static inline int iommu_sva_bind_group(struct iommu_group *group, + struct mm_struct *mm, int *pasid, + unsigned long flags, void *drvdata) +{ + return -ENODEV; +} + +static inline int iommu_sva_unbind_group(struct iommu_group *group, int pasid) +{ + return -ENODEV; +} + #endif /* CONFIG_IOMMU_API */ #ifdef CONFIG_IOMMU_SVA extern int iommu_sva_device_init(struct device *dev, unsigned long features, unsigned int max_pasid); extern int iommu_sva_device_shutdown(struct device *dev); +extern int iommu_sva_bind_device(struct device *dev, struct mm_struct *mm, + int *pasid, unsigned long flags, void *drvdata); +extern int iommu_sva_unbind_device(struct device *dev, int pasid); +extern void __iommu_sva_unbind_dev_all(struct device *dev); #else /* CONFIG_IOMMU_SVA */ static inline int iommu_sva_device_init(struct device *dev, unsigned long features, @@ -908,6 +928,22 @@ static inline int iommu_sva_device_shutdown(struct device *dev) { return -ENODEV; } + +static inline int iommu_sva_bind_device(struct device *dev, + struct mm_struct *mm, int *pasid, + unsigned long flags, void *drvdata) +{ + return -ENODEV; +} + +static inline int iommu_sva_unbind_device(struct device *dev, int pasid) +{ + return -ENODEV; +} + +static inline void __iommu_sva_unbind_dev_all(struct device *dev) +{ +} #endif /* CONFIG_IOMMU_SVA */ #endif /* __LINUX_IOMMU_H */