From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-11.5 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,INCLUDES_PATCH,MAILING_LIST_MULTI,SIGNED_OFF_BY, SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_SANE_1 autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 8C0B5C433E3 for ; Mon, 13 Jul 2020 20:24:05 +0000 (UTC) Received: from merlin.infradead.org (merlin.infradead.org [205.233.59.134]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 5A9D620720 for ; Mon, 13 Jul 2020 20:24:05 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=lists.infradead.org header.i=@lists.infradead.org header.b="YVQXG9dy"; dkim=fail reason="signature verification failed" (1024-bit key) header.d=kernel.org header.i=@kernel.org header.b="oiFwI26T" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 5A9D620720 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=kernel.org Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=merlin.20170209; h=Sender:Content-Transfer-Encoding: Content-Type:Cc:List-Subscribe:List-Help:List-Post:List-Archive: List-Unsubscribe:List-Id:In-Reply-To:MIME-Version:References:Message-ID: Subject:To:From:Date:Reply-To:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=sECfAyZWA73jyVHNGh7Lt6VnFQbJvdbLOtAU2B0LW6g=; b=YVQXG9dyJDz88qrfRILZJ6UdQ WQic6RnaroC/b595e+QqtkuFi0MOwGlzA27MeVrsoR/keJOAo3+F3dNUXDmnUE19yySAAr+GBmRGQ d/plSqAvNJotTJxWINhwaIwmuElGfP3gQtuQ2GHxoE/EbRIkfbNq7KKV0Uyq0eCpRkt/+7TNlIlBD 0bQmSIMny+RsdnmUk0BQhFqir4eDzUlgc9xcFlUZQZQG68whMIqQ+ScpUaWT3DfFpzKSsNCTE2qMp Y8sxBmlGJvZJphUdtWrOuQ16qW+cFuQc18kZGVoiJDY4bQIiO0ns5Z1CITizOkFLs0yur5r9U9uOI cSxt4cLXA==; Received: from localhost ([::1] helo=merlin.infradead.org) by merlin.infradead.org with esmtp (Exim 4.92.3 #3 (Red Hat Linux)) id 1jv4yO-0001lJ-Mu; Mon, 13 Jul 2020 20:22:48 +0000 Received: from mail.kernel.org ([198.145.29.99]) by merlin.infradead.org with esmtps (Exim 4.92.3 #3 (Red Hat Linux)) id 1jv4yK-0001jl-Lh for linux-arm-kernel@lists.infradead.org; Mon, 13 Jul 2020 20:22:45 +0000 Received: from willie-the-truck (236.31.169.217.in-addr.arpa [217.169.31.236]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 628A52075B; Mon, 13 Jul 2020 20:22:40 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1594671762; bh=tM1FG+INMniDO2N5XmTmfJsq0UrVrFhjB9hPE3JBaRI=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=oiFwI26TXEaKYwiXAkSUdtnF6xjknVkYaPt5FiaBfoFvuqmyN4pT7MpgNbZGtVlTf GNimjfAn28yXttkqcq6BhRH+mWq/kz5JNKQYSZyPpswpDRY2/1nb11twItEbbpn7dJ sKniyzq9REWXMHQ55HYbTFFb9Nb/cSJHS3EBb3k0= Date: Mon, 13 Jul 2020 21:22:37 +0100 From: Will Deacon To: Jean-Philippe Brucker Subject: Re: [PATCH v8 07/12] iommu/arm-smmu-v3: Share process page tables Message-ID: <20200713202236.GA3575@willie-the-truck> References: <20200618155125.1548969-1-jean-philippe@linaro.org> <20200618155125.1548969-8-jean-philippe@linaro.org> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <20200618155125.1548969-8-jean-philippe@linaro.org> User-Agent: Mutt/1.10.1 (2018-07-13) X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20200713_162244_985060_282576CF X-CRM114-Status: GOOD ( 26.94 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: fenghua.yu@intel.com, jacob.jun.pan@linux.intel.com, Suzuki K Poulose , catalin.marinas@arm.com, joro@8bytes.org, zhengxiang9@huawei.com, hch@infradead.org, linux-mm@kvack.org, iommu@lists.linux-foundation.org, Jonathan.Cameron@huawei.com, zhangfei.gao@linaro.org, robin.murphy@arm.com, xuzaibo@huawei.com, linux-arm-kernel@lists.infradead.org, baolu.lu@linux.intel.com Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org On Thu, Jun 18, 2020 at 05:51:20PM +0200, Jean-Philippe Brucker wrote: > With Shared Virtual Addressing (SVA), we need to mirror CPU TTBR, TCR, > MAIR and ASIDs in SMMU contexts. Each SMMU has a single ASID space split > into two sets, shared and private. Shared ASIDs correspond to those > obtained from the arch ASID allocator, and private ASIDs are used for > "classic" map/unmap DMA. > > A possible conflict happens when trying to use a shared ASID that has > already been allocated for private use by the SMMU driver. This will be > addressed in a later patch by replacing the private ASID. At the > moment we return -EBUSY. > > Each mm_struct shared with the SMMU will have a single context > descriptor. Add a refcount to keep track of this. It will be protected > by the global SVA lock. > > Acked-by: Suzuki K Poulose > Signed-off-by: Jean-Philippe Brucker > --- > drivers/iommu/arm-smmu-v3.c | 150 +++++++++++++++++++++++++++++++++++- > 1 file changed, 146 insertions(+), 4 deletions(-) > > diff --git a/drivers/iommu/arm-smmu-v3.c b/drivers/iommu/arm-smmu-v3.c > index 937aa1af428d5..cabd942e4cbf3 100644 > --- a/drivers/iommu/arm-smmu-v3.c > +++ b/drivers/iommu/arm-smmu-v3.c > @@ -22,6 +22,7 @@ > #include > #include > #include > +#include > #include > #include > #include > @@ -33,6 +34,8 @@ > > #include > > +#include "io-pgtable-arm.h" > + > /* MMIO registers */ > #define ARM_SMMU_IDR0 0x0 > #define IDR0_ST_LVL GENMASK(28, 27) > @@ -589,6 +592,9 @@ struct arm_smmu_ctx_desc { > u64 ttbr; > u64 tcr; > u64 mair; > + > + refcount_t refs; > + struct mm_struct *mm; > }; > > struct arm_smmu_l1_ctx_desc { > @@ -727,6 +733,7 @@ struct arm_smmu_option_prop { > }; > > static DEFINE_XARRAY_ALLOC1(asid_xa); > +static DEFINE_MUTEX(sva_lock); > > static struct arm_smmu_option_prop arm_smmu_options[] = { > { ARM_SMMU_OPT_SKIP_PREFETCH, "hisilicon,broken-prefetch-cmd" }, > @@ -1662,7 +1669,8 @@ static int arm_smmu_write_ctx_desc(struct arm_smmu_domain *smmu_domain, > #ifdef __BIG_ENDIAN > CTXDESC_CD_0_ENDI | > #endif > - CTXDESC_CD_0_R | CTXDESC_CD_0_A | CTXDESC_CD_0_ASET | > + CTXDESC_CD_0_R | CTXDESC_CD_0_A | > + (cd->mm ? 0 : CTXDESC_CD_0_ASET) | > CTXDESC_CD_0_AA64 | > FIELD_PREP(CTXDESC_CD_0_ASID, cd->asid) | > CTXDESC_CD_0_V; > @@ -1766,12 +1774,144 @@ static void arm_smmu_free_cd_tables(struct arm_smmu_domain *smmu_domain) > cdcfg->cdtab = NULL; > } > > -static void arm_smmu_free_asid(struct arm_smmu_ctx_desc *cd) > +static void arm_smmu_init_cd(struct arm_smmu_ctx_desc *cd) > { > + refcount_set(&cd->refs, 1); > +} > + > +static bool arm_smmu_free_asid(struct arm_smmu_ctx_desc *cd) > +{ > + bool free; > + struct arm_smmu_ctx_desc *old_cd; > + > if (!cd->asid) > - return; > + return false; > + > + free = refcount_dec_and_test(&cd->refs); > + if (free) { > + old_cd = xa_erase(&asid_xa, cd->asid); > + WARN_ON(old_cd != cd); > + } > + return free; > +} > + > +static struct arm_smmu_ctx_desc *arm_smmu_share_asid(u16 asid) > +{ > + struct arm_smmu_ctx_desc *cd; > > - xa_erase(&asid_xa, cd->asid); > + cd = xa_load(&asid_xa, asid); > + if (!cd) > + return NULL; > + > + if (cd->mm) { > + /* All devices bound to this mm use the same cd struct. */ > + refcount_inc(&cd->refs); > + return cd; > + } How do you handle racing against a concurrent arm_smmu_free_asid() here? > +__maybe_unused > +static struct arm_smmu_ctx_desc *arm_smmu_alloc_shared_cd(struct mm_struct *mm) > +{ > + u16 asid; > + int ret = 0; > + u64 tcr, par, reg; > + struct arm_smmu_ctx_desc *cd; > + struct arm_smmu_ctx_desc *old_cd = NULL; > + > + lockdep_assert_held(&sva_lock); Please don't bother with these for static functions (but I can see the value in having them for functions with external callers). > + > + asid = mm_context_get(mm); > + if (!asid) > + return ERR_PTR(-ESRCH); > + > + cd = kzalloc(sizeof(*cd), GFP_KERNEL); > + if (!cd) { > + ret = -ENOMEM; > + goto err_put_context; > + } > + > + arm_smmu_init_cd(cd); > + > + old_cd = arm_smmu_share_asid(asid); > + if (IS_ERR(old_cd)) { > + ret = PTR_ERR(old_cd); > + goto err_free_cd; > + } else if (old_cd) { Don't need the 'else' > + if (WARN_ON(old_cd->mm != mm)) { > + ret = -EINVAL; > + goto err_free_cd; > + } > + kfree(cd); > + mm_context_put(mm); > + return old_cd; This is a bit messy. Can you consolidate the return path so that ret is a pointer and you have an 'int err', e.g.: return err < 0 ? ERR_PTR(err) : ret; Will _______________________________________________ linux-arm-kernel mailing list linux-arm-kernel@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-arm-kernel