From patchwork Fri May 11 19:06:03 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: 10394835 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 D77FB60348 for ; Fri, 11 May 2018 19:07:46 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id C0E3628F91 for ; Fri, 11 May 2018 19:07:46 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id B58FB28F95; Fri, 11 May 2018 19:07:46 +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=-2.9 required=2.0 tests=BAYES_00, MAILING_LIST_MULTI, RCVD_IN_DNSWL_NONE autolearn=ham version=3.3.1 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id F332F28D17 for ; Fri, 11 May 2018 19:07:45 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id A3FCB6B0681; Fri, 11 May 2018 15:07:44 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 9EE236B0682; Fri, 11 May 2018 15:07:44 -0400 (EDT) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 8B5776B0683; Fri, 11 May 2018 15:07:44 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-ot0-f198.google.com (mail-ot0-f198.google.com [74.125.82.198]) by kanga.kvack.org (Postfix) with ESMTP id 6308C6B0681 for ; Fri, 11 May 2018 15:07:44 -0400 (EDT) Received: by mail-ot0-f198.google.com with SMTP id b5-v6so4292223otf.8 for ; Fri, 11 May 2018 12:07:44 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-original-authentication-results:x-gm-message-state:from:to:cc :subject:date:message-id:in-reply-to:references; bh=C1gp8Nsv5bhJgImTBEKds8VBLd2pAKrow0zAByCRPAQ=; b=TzdCwR6NoD6Mb0t8bPQ1hexY8Pg7PdoVT8MW8j1F2Yh86i6KNZZSusrXBTb+3/xI4r zcNnR3m978f88qrMTUxXfeB5jxy49dgKSxyZS+UOtHTornSd8LV082D/CXGrTqhZkCpe mHPu7XW2R79gfVsExrPQMIDNFBcF1Hhp7HSy0Hq4KVsAXZ2lwCKHWroiluakKxnw9SHd gDnw5ZIyd97eQhUaYM/6ht/wXzVO4lL7fXEpOw6DUTdKuqzq/qV/sdK5AWMwXdIJoOiX grU8iXqOkWTwKdVHvwVw9hd4Dg55hrgUY/TUOyrSUuA51uH/9wo/ZHxF06+EVan1Dgem xRKw== X-Original-Authentication-Results: mx.google.com; spf=pass (google.com: domain of jean-philippe.brucker@arm.com designates 217.140.101.70 as permitted sender) smtp.mailfrom=jean-philippe.brucker@arm.com X-Gm-Message-State: ALKqPwd2bLFZUZEsZTXYTmtPt6i5neEc250qaFbmEaqiE/6bWko1C7tP ZEDjX1g/eFg2489Kd5gNJGBKoZx+GJLmTfwghFBqTiTRLFg7UfubX8SFMVPhKZElID9UwykKkYk YVw2SNe+fz6zJmRXyvdyb880WgoFAPyPPfdb4WFW5e6r46jSy2tNRg/5XmuwK5syEUw== X-Received: by 2002:a9d:ac4:: with SMTP id 62-v6mr4956345otq.257.1526065664061; Fri, 11 May 2018 12:07:44 -0700 (PDT) X-Google-Smtp-Source: AB8JxZpSb2IaqW12+NlHDjVRQfgqr1MQL5JuhWj1zAZOlNh8o4ALdemxG6EYPwy2X6I20jLSY0no X-Received: by 2002:a9d:ac4:: with SMTP id 62-v6mr4956291otq.257.1526065663147; Fri, 11 May 2018 12:07:43 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1526065663; cv=none; d=google.com; s=arc-20160816; b=Lq9UVwWiw2FOxgk96Skf0rvXlpJuGBcJI+JWanzvv4YhOaz7vNg+zXoC8utp9CCgMy Kj02ikhzkK4K17N3nYUj26X+Wd52XZBVVFgDKfpaTFqXLzZJBeoSSYNofFhmmE7+VXgX vNZlWYz0wZQcrrIQlJ54uzNdGQTIKsyYQVOleI+J9odORmiZZvMkpjCK+/UC90nIdIZw 7skugbOrjrZhPSF2FiVfne9Y8j7hvXoyiiFOttt9lGmYV0F1Gzbl4pi4NzNWGM7rqFVV 3m7ozWXUzjfgEdJRz0/OC+GSG0Dul0SyqKTWwg/lfF5vKGZFdRh3xiGxPcFaEcHB2Pe8 vMPQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=references:in-reply-to:message-id:date:subject:cc:to:from :arc-authentication-results; bh=C1gp8Nsv5bhJgImTBEKds8VBLd2pAKrow0zAByCRPAQ=; b=q5GbZzfclaUCk9J6HcpqzsSVrf0OeFCg54y+HscFUxR/oz4i1/f6PvauZgEDIfAw8P j69qC/zwML0cGlbqml1+njUh4bGUq2z9go5RAGz/SSuNNlNpbGNPjDgjp1sHDNWx+z/C KrP4Lu4jjV2cztUCvBL49X6Jueg94gchWQes6GbCCTr+kPvT0gW5AfQtDTtq69Ga+u3H txRyHL4T/494ZrciLUoWprjNJcyPDbgKPl1jKb/VFhvah0wgy0dYx9d8+owgXT/eSFfd Ioq6ggrU1776VjIO27Qv/oNbvFw1cYffvvj6ZPOjTSeiwFqBZhRkeljXaCHmyAi4WPDf Wkag== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of jean-philippe.brucker@arm.com designates 217.140.101.70 as permitted sender) smtp.mailfrom=jean-philippe.brucker@arm.com Received: from foss.arm.com (foss.arm.com. [217.140.101.70]) by mx.google.com with ESMTP id l62-v6si1167415oia.55.2018.05.11.12.07.42 for ; Fri, 11 May 2018 12:07:43 -0700 (PDT) Received-SPF: pass (google.com: domain of jean-philippe.brucker@arm.com designates 217.140.101.70 as permitted sender) client-ip=217.140.101.70; Authentication-Results: mx.google.com; spf=pass (google.com: domain of jean-philippe.brucker@arm.com designates 217.140.101.70 as permitted sender) smtp.mailfrom=jean-philippe.brucker@arm.com 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 B167E1993; Fri, 11 May 2018 12:07:42 -0700 (PDT) Received: from ostrya.cambridge.arm.com (ostrya.cambridge.arm.com [10.1.210.33]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id 731B93F23C; Fri, 11 May 2018 12:07:37 -0700 (PDT) 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, linux-mm@kvack.org Cc: joro@8bytes.org, will.deacon@arm.com, robin.murphy@arm.com, alex.williamson@redhat.com, tn@semihalf.com, liubo95@huawei.com, thunder.leizhen@huawei.com, xieyisheng1@huawei.com, xuzaibo@huawei.com, ilias.apalodimas@linaro.org, jonathan.cameron@huawei.com, liudongdong3@huawei.com, shunyong.yang@hxt-semitech.com, nwatters@codeaurora.org, okaya@codeaurora.org, jcrouse@codeaurora.org, rfranz@cavium.com, dwmw2@infradead.org, jacob.jun.pan@linux.intel.com, yi.l.liu@intel.com, ashok.raj@intel.com, kevin.tian@intel.com, baolu.lu@linux.intel.com, robdclark@gmail.com, christian.koenig@amd.com, bharatku@xilinx.com, rgummal@xilinx.com Subject: [PATCH v2 02/40] iommu/sva: Bind process address spaces to devices Date: Fri, 11 May 2018 20:06:03 +0100 Message-Id: <20180511190641.23008-3-jean-philippe.brucker@arm.com> X-Mailer: git-send-email 2.17.0 In-Reply-To: <20180511190641.23008-1-jean-philippe.brucker@arm.com> References: <20180511190641.23008-1-jean-philippe.brucker@arm.com> X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: X-Virus-Scanned: ClamAV using ClamSMTP Add bind() and unbind() operations to the IOMMU API. Bind() returns a PASID that drivers can program in hardware, to let their devices access an mm. This patch only adds skeletons for the device driver API, most of the implementation is still missing. IOMMU groups with more than one device aren't supported for SVA at the moment. There may be P2P traffic between devices within a group, which cannot be seen by an IOMMU (note that supporting PASID doesn't add any form of isolation with regard to P2P). Supporting groups would require calling bind() for all bound processes every time a device is added to a group, to perform sanity checks (e.g. ensure that new devices support PASIDs at least as big as those already allocated in the group). It also means making sure that reserved ranges (IOMMU_RESV_*) of all devices are carved out of processes. This is already tricky with single devices, but becomes very difficult with groups. Since SVA-capable devices are expected to be cleanly isolated, and since we don't have any way to test groups or hot-plug, we only allow singular groups for now. Signed-off-by: Jean-Philippe Brucker --- v1->v2: remove iommu_sva_bind/unbind_group --- drivers/iommu/iommu-sva.c | 27 +++++++++++++ drivers/iommu/iommu.c | 83 +++++++++++++++++++++++++++++++++++++++ include/linux/iommu.h | 37 +++++++++++++++++ 3 files changed, 147 insertions(+) diff --git a/drivers/iommu/iommu-sva.c b/drivers/iommu/iommu-sva.c index 8b4afb7c63ae..8d98f9c09864 100644 --- a/drivers/iommu/iommu-sva.c +++ b/drivers/iommu/iommu-sva.c @@ -93,6 +93,8 @@ int iommu_sva_device_shutdown(struct device *dev) if (!domain) return -ENODEV; + __iommu_sva_unbind_dev_all(dev); + mutex_lock(&dev->iommu_param->lock); param = dev->iommu_param->sva_param; dev->iommu_param->sva_param = NULL; @@ -108,3 +110,28 @@ int iommu_sva_device_shutdown(struct device *dev) return 0; } EXPORT_SYMBOL_GPL(iommu_sva_device_shutdown); + +int __iommu_sva_bind_device(struct device *dev, struct mm_struct *mm, + int *pasid, unsigned long flags, void *drvdata) +{ + return -ENOSYS; /* TODO */ +} +EXPORT_SYMBOL_GPL(__iommu_sva_bind_device); + +int __iommu_sva_unbind_device(struct device *dev, int pasid) +{ + return -ENOSYS; /* TODO */ +} +EXPORT_SYMBOL_GPL(__iommu_sva_unbind_device); + +/** + * __iommu_sva_unbind_dev_all() - Detach all address spaces from this device + * @dev: the device + * + * When detaching @device from a domain, IOMMU drivers should use this helper. + */ +void __iommu_sva_unbind_dev_all(struct device *dev) +{ + /* TODO */ +} +EXPORT_SYMBOL_GPL(__iommu_sva_unbind_dev_all); diff --git a/drivers/iommu/iommu.c b/drivers/iommu/iommu.c index 9e28d88c8074..bd2819deae5b 100644 --- a/drivers/iommu/iommu.c +++ b/drivers/iommu/iommu.c @@ -2261,3 +2261,86 @@ int iommu_fwspec_add_ids(struct device *dev, u32 *ids, int num_ids) return 0; } EXPORT_SYMBOL_GPL(iommu_fwspec_add_ids); + +/** + * 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 + * @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. If unbind() isn't called first, a subsequent bind() + * for the same device and mm fails with -EEXIST. + * + * iommu_sva_device_init() must be called first, to initialize the required SVA + * features. @flags is a subset of these features. + * + * The caller must pin down using get_user_pages*() all mappings shared with the + * device. mlock() isn't sufficient, as it doesn't prevent minor page faults + * (e.g. copy-on-write). + * + * 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) +{ + int ret = -EINVAL; + struct iommu_group *group; + + if (!pasid) + return -EINVAL; + + group = iommu_group_get(dev); + if (!group) + return -ENODEV; + + /* Ensure device count and domain don't change while we're binding */ + mutex_lock(&group->mutex); + if (iommu_group_device_count(group) != 1) + goto out_unlock; + + ret = __iommu_sva_bind_device(dev, mm, pasid, flags, drvdata); + +out_unlock: + mutex_unlock(&group->mutex); + iommu_group_put(group); + + return ret; +} +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 for 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) +{ + int ret = -EINVAL; + struct iommu_group *group; + + group = iommu_group_get(dev); + if (!group) + return -ENODEV; + + mutex_lock(&group->mutex); + ret = __iommu_sva_unbind_device(dev, pasid); + mutex_unlock(&group->mutex); + + iommu_group_put(group); + + return ret; +} +EXPORT_SYMBOL_GPL(iommu_sva_unbind_device); diff --git a/include/linux/iommu.h b/include/linux/iommu.h index 2efe7738bedb..da59c20c4f12 100644 --- a/include/linux/iommu.h +++ b/include/linux/iommu.h @@ -613,6 +613,10 @@ 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_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); + #else /* CONFIG_IOMMU_API */ struct iommu_ops {}; @@ -932,12 +936,29 @@ static inline int iommu_sva_invalidate(struct iommu_domain *domain, return -EINVAL; } +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; +} + #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, @@ -950,6 +971,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 */