From patchwork Wed Feb 21 22:59:13 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jordan Crouse X-Patchwork-Id: 10234249 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 97B40602A0 for ; Wed, 21 Feb 2018 22:59:49 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 8A78D27D4A for ; Wed, 21 Feb 2018 22:59:49 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 7E7352855D; Wed, 21 Feb 2018 22:59:49 +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.1 required=2.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_MED,T_DKIM_INVALID autolearn=ham version=3.3.1 Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher DHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id F40D027D4A for ; Wed, 21 Feb 2018 22:59:48 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 02ECD6EABE; Wed, 21 Feb 2018 22:59:43 +0000 (UTC) X-Original-To: dri-devel@lists.freedesktop.org Delivered-To: dri-devel@lists.freedesktop.org Received: from smtp.codeaurora.org (smtp.codeaurora.org [198.145.29.96]) by gabe.freedesktop.org (Postfix) with ESMTPS id 45A916EABC; Wed, 21 Feb 2018 22:59:42 +0000 (UTC) Received: by smtp.codeaurora.org (Postfix, from userid 1000) id D99AF60A05; Wed, 21 Feb 2018 22:59:40 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=codeaurora.org; s=default; t=1519253982; bh=DJsu/KvmPk1T6YD2Fqn2EtJ+SRlGvr3n83UT15p+mkI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=A4EgfmjexuCSY/HvBG2s6lC1cFXYyGHCEmHv5zH5IJCMiwFLVQZl5//iYXyJlAOBt pVcuoi7e0pNBjHUbcf6itUEw3UqS49ge+iRSBLQK8XO23dzhb+hxXWSg2aQbw1gLvF fxEngAB5SwoUVKJjZLB74UpxKAPOEjdAixqLM8pE= Received: from jcrouse-lnx.qualcomm.com (i-global254.qualcomm.com [199.106.103.254]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-SHA256 (128/128 bits)) (No client certificate requested) (Authenticated sender: jcrouse@smtp.codeaurora.org) by smtp.codeaurora.org (Postfix) with ESMTPSA id 9D6A160BEB; Wed, 21 Feb 2018 22:59:37 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=codeaurora.org; s=default; t=1519253978; bh=DJsu/KvmPk1T6YD2Fqn2EtJ+SRlGvr3n83UT15p+mkI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=FnPbWvikgUmKmIJPnw20ikiRICXhOSmgZ9uFcd24mfWGIUJhY7cFP0b9LC3foFj7U a5ZXLcRmdYAkWmo6nmo3ziCOmPWQTyGBsDJ2SmFUNYyaUIJY9P2f308UjWKC9Y28RT Q+YjojSSxFOdAm1So+2NBTHXO03QiA+WsaeWED0o= DMARC-Filter: OpenDMARC Filter v1.3.2 smtp.codeaurora.org 9D6A160BEB Authentication-Results: pdx-caf-mail.web.codeaurora.org; dmarc=none (p=none dis=none) header.from=codeaurora.org Authentication-Results: pdx-caf-mail.web.codeaurora.org; spf=none smtp.mailfrom=jcrouse@codeaurora.org From: Jordan Crouse To: freedreno@lists.freedesktop.org Subject: [PATCH 03/14] iommu: Create a base struct for io_mm Date: Wed, 21 Feb 2018 15:59:13 -0700 Message-Id: <20180221225924.30737-4-jcrouse@codeaurora.org> X-Mailer: git-send-email 2.16.1 In-Reply-To: <20180221225924.30737-1-jcrouse@codeaurora.org> References: <20180221225924.30737-1-jcrouse@codeaurora.org> X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: jean-philippe.brucker@arm.com, linux-arm-msm@vger.kernel.org, dri-devel@lists.freedesktop.org, tfiga@chromium.org, iommu@lists.linux-foundation.org, vivek.gautam@codeaurora.org, linux-arm-kernel@lists.infradead.org MIME-Version: 1.0 Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" X-Virus-Scanned: ClamAV using ClamSMTP In order to support both shared mm sva pagetables as well as io-pgtable backed tables add a base structure to io_mm so that the two styles can share the same idr. Signed-off-by: Jordan Crouse --- drivers/iommu/arm-smmu-v3.c | 8 ++++---- drivers/iommu/iommu-sva.c | 50 ++++++++++++++++++++++++++++++--------------- include/linux/iommu.h | 11 +++++++++- 3 files changed, 47 insertions(+), 22 deletions(-) diff --git a/drivers/iommu/arm-smmu-v3.c b/drivers/iommu/arm-smmu-v3.c index 26935a9a5a97..4736a2bf39cf 100644 --- a/drivers/iommu/arm-smmu-v3.c +++ b/drivers/iommu/arm-smmu-v3.c @@ -2454,7 +2454,7 @@ static int arm_smmu_mm_attach(struct iommu_domain *domain, struct device *dev, if (!attach_domain) return 0; - return ops->set_entry(ops, io_mm->pasid, smmu_mm->cd); + return ops->set_entry(ops, io_mm->base.pasid, smmu_mm->cd); } static void arm_smmu_mm_detach(struct iommu_domain *domain, struct device *dev, @@ -2466,9 +2466,9 @@ static void arm_smmu_mm_detach(struct iommu_domain *domain, struct device *dev, struct arm_smmu_master_data *master = dev->iommu_fwspec->iommu_priv; if (detach_domain) - ops->clear_entry(ops, io_mm->pasid, smmu_mm->cd); + ops->clear_entry(ops, io_mm->base.pasid, smmu_mm->cd); - arm_smmu_atc_inv_master_all(master, io_mm->pasid); + arm_smmu_atc_inv_master_all(master, io_mm->base.pasid); /* TODO: Invalidate all mappings if last and not DVM. */ } @@ -2478,7 +2478,7 @@ static void arm_smmu_mm_invalidate(struct iommu_domain *domain, { struct arm_smmu_master_data *master = dev->iommu_fwspec->iommu_priv; - arm_smmu_atc_inv_master_range(master, io_mm->pasid, iova, size); + arm_smmu_atc_inv_master_range(master, io_mm->base.pasid, iova, size); /* * TODO: Invalidate mapping if not DVM */ diff --git a/drivers/iommu/iommu-sva.c b/drivers/iommu/iommu-sva.c index d7b231cd7355..5fc689b1ef72 100644 --- a/drivers/iommu/iommu-sva.c +++ b/drivers/iommu/iommu-sva.c @@ -161,13 +161,15 @@ io_mm_alloc(struct iommu_domain *domain, struct device *dev, io_mm->mm = mm; io_mm->notifier.ops = &iommu_mmu_notifier; io_mm->release = domain->ops->mm_free; + io_mm->base.type = IO_TYPE_MM; + INIT_LIST_HEAD(&io_mm->devices); idr_preload(GFP_KERNEL); spin_lock(&iommu_sva_lock); - pasid = idr_alloc_cyclic(&iommu_pasid_idr, io_mm, dev_param->min_pasid, - dev_param->max_pasid + 1, GFP_ATOMIC); - io_mm->pasid = pasid; + pasid = idr_alloc_cyclic(&iommu_pasid_idr, &io_mm->base, + dev_param->min_pasid, dev_param->max_pasid + 1, GFP_ATOMIC); + io_mm->base.pasid = pasid; spin_unlock(&iommu_sva_lock); idr_preload_end(); @@ -200,7 +202,7 @@ io_mm_alloc(struct iommu_domain *domain, struct device *dev, * 0 so no user could get a reference to it. Free it manually. */ spin_lock(&iommu_sva_lock); - idr_remove(&iommu_pasid_idr, io_mm->pasid); + idr_remove(&iommu_pasid_idr, io_mm->base.pasid); spin_unlock(&iommu_sva_lock); err_free_mm: @@ -231,7 +233,7 @@ static void io_mm_release(struct kref *kref) io_mm = container_of(kref, struct io_mm, kref); WARN_ON(!list_empty(&io_mm->devices)); - idr_remove(&iommu_pasid_idr, io_mm->pasid); + idr_remove(&iommu_pasid_idr, io_mm->base.pasid); /* * If we're being released from mm exit, the notifier callback ->release @@ -286,7 +288,7 @@ static int io_mm_attach(struct iommu_domain *domain, struct device *dev, { int ret; bool attach_domain = true; - int pasid = io_mm->pasid; + int pasid = io_mm->base.pasid; struct iommu_bond *bond, *tmp; struct iommu_param *dev_param = dev->iommu_param; @@ -378,7 +380,7 @@ static int iommu_signal_mm_exit(struct iommu_bond *bond) if (!dev->iommu_param || !dev->iommu_param->mm_exit) return 0; - return dev->iommu_param->mm_exit(dev, io_mm->pasid, bond->drvdata); + return dev->iommu_param->mm_exit(dev, io_mm->base.pasid, bond->drvdata); } /* @@ -410,7 +412,7 @@ static void iommu_notifier_release(struct mmu_notifier *mn, struct mm_struct *mm list_for_each_entry_safe(bond, next, &io_mm->devices, mm_head) { if (iommu_signal_mm_exit(bond)) dev_WARN(bond->dev, "possible leak of PASID %u", - io_mm->pasid); + io_mm->base.pasid); io_mm_detach_all_locked(bond); } @@ -585,6 +587,7 @@ int iommu_sva_bind_device(struct device *dev, struct mm_struct *mm, int *pasid, unsigned long flags, void *drvdata) { int i, ret; + struct io_base *io_base = NULL; struct io_mm *io_mm = NULL; struct iommu_domain *domain; struct iommu_bond *bond = NULL, *tmp; @@ -605,7 +608,12 @@ int iommu_sva_bind_device(struct device *dev, struct mm_struct *mm, int *pasid, /* If an io_mm already exists, use it */ spin_lock(&iommu_sva_lock); - idr_for_each_entry(&iommu_pasid_idr, io_mm, i) { + idr_for_each_entry(&iommu_pasid_idr, io_base, i) { + if (io_base->type != IO_TYPE_MM) + continue; + + io_mm = container_of(io_base, struct io_mm, base); + if (io_mm->mm != mm || !io_mm_get_locked(io_mm)) continue; @@ -636,7 +644,7 @@ int iommu_sva_bind_device(struct device *dev, struct mm_struct *mm, int *pasid, if (ret) io_mm_put(io_mm); else - *pasid = io_mm->pasid; + *pasid = io_mm->base.pasid; return ret; } @@ -659,6 +667,7 @@ EXPORT_SYMBOL_GPL(iommu_sva_bind_device); int iommu_sva_unbind_device(struct device *dev, int pasid) { int ret = -ESRCH; + struct io_base *io_base; struct io_mm *io_mm; struct iommu_domain *domain; struct iommu_bond *bond = NULL; @@ -674,12 +683,14 @@ int iommu_sva_unbind_device(struct device *dev, int pasid) iommu_fault_queue_flush(dev); spin_lock(&iommu_sva_lock); - io_mm = idr_find(&iommu_pasid_idr, pasid); - if (!io_mm) { + io_base = idr_find(&iommu_pasid_idr, pasid); + if (!io_base || io_base->type != IO_TYPE_MM) { spin_unlock(&iommu_sva_lock); return -ESRCH; } + io_mm = container_of(io_base, struct io_mm, base); + list_for_each_entry(bond, &io_mm->devices, mm_head) { if (bond->dev == dev) { io_mm_detach_locked(bond); @@ -777,16 +788,21 @@ EXPORT_SYMBOL_GPL(iommu_unregister_mm_exit_handler); */ struct mm_struct *iommu_sva_find(int pasid) { + struct io_base *io_base; struct io_mm *io_mm; struct mm_struct *mm = NULL; spin_lock(&iommu_sva_lock); - io_mm = idr_find(&iommu_pasid_idr, pasid); - if (io_mm && io_mm_get_locked(io_mm)) { - if (mmget_not_zero(io_mm->mm)) - mm = io_mm->mm; + io_base = idr_find(&iommu_pasid_idr, pasid); + if (io_base && io_base->type == IO_TYPE_MM) { + io_mm = container_of(io_base, struct io_mm, base); + + if (io_mm_get_locked(io_mm)) { + if (mmget_not_zero(io_mm->mm)) + mm = io_mm->mm; - io_mm_put_locked(io_mm); + io_mm_put_locked(io_mm); + } } spin_unlock(&iommu_sva_lock); diff --git a/include/linux/iommu.h b/include/linux/iommu.h index e2c49e583d8d..e998389cf195 100644 --- a/include/linux/iommu.h +++ b/include/linux/iommu.h @@ -110,8 +110,17 @@ struct iommu_domain { struct list_head mm_list; }; +enum iommu_io_type { + IO_TYPE_MM, +}; + +struct io_base { + int type; + int pasid; +}; + struct io_mm { - int pasid; + struct io_base base; struct list_head devices; struct kref kref; #if defined(CONFIG_MMU_NOTIFIER)