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=-12.5 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH,MAILING_LIST_MULTI, SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham 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 41044C43463 for ; Fri, 18 Sep 2020 10:20:16 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id BFC682075E for ; Fri, 18 Sep 2020 10:20:15 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (2048-bit key) header.d=linaro.org header.i=@linaro.org header.b="CBxa45ll" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org BFC682075E Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=linaro.org Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 3680E6B0071; Fri, 18 Sep 2020 06:20:06 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 2A6786B0072; Fri, 18 Sep 2020 06:20:06 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 087268E0001; Fri, 18 Sep 2020 06:20:06 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0070.hostedemail.com [216.40.44.70]) by kanga.kvack.org (Postfix) with ESMTP id E85B56B0072 for ; Fri, 18 Sep 2020 06:20:05 -0400 (EDT) Received: from smtpin09.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay02.hostedemail.com (Postfix) with ESMTP id AAE613622 for ; Fri, 18 Sep 2020 10:20:05 +0000 (UTC) X-FDA: 77275786770.09.snake29_3f069952712a Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin09.hostedemail.com (Postfix) with ESMTP id 8975C180AD81D for ; Fri, 18 Sep 2020 10:20:05 +0000 (UTC) X-HE-Tag: snake29_3f069952712a X-Filterd-Recvd-Size: 12967 Received: from mail-ej1-f67.google.com (mail-ej1-f67.google.com [209.85.218.67]) by imf43.hostedemail.com (Postfix) with ESMTP for ; Fri, 18 Sep 2020 10:20:05 +0000 (UTC) Received: by mail-ej1-f67.google.com with SMTP id z22so7354227ejl.7 for ; Fri, 18 Sep 2020 03:20:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=N4DgoZbv9M84LHIoPp24tcG/3zGmTW+r6nCgwOX6Bv8=; b=CBxa45llh5OZfBmW/bSVVbfSxVaVc2lCs9+BXqUfvaNeCPBpTQN4FhBxFWhVV2C1n/ J4kvdQeFs3Z86rbPUxm1V6q8lXoAo6Uqh2BNp5+wuF9Q15rw9P+Ae6k+OVIuqMu4sqPa PLKYY08Q/39/P8DcUwzChBhrvzLS38UczwWn+xUJTgtlHelDtPWY2AUnzc3KqOAZq5p+ RbJHJBPid/ZWb6nyP92zX/tGj4lNoCvD4Tbf/lnFQzd0yY3cSJztUfiCf2bjPu+w8nyp apnX8RTNax6UO1j6oZ0L8FzSHoPCMxyn0b+1RcQeik7kdZkqMOd4O+CxlOd42clRII18 StMA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=N4DgoZbv9M84LHIoPp24tcG/3zGmTW+r6nCgwOX6Bv8=; b=NAl9EnF6vx14I4Ub/BxQcj9X9gJb6TVTU30to/DkfCHEVlWRcBpDgFZIMwmUL2Y1jJ qbFtUIic1/x156Ak+ux++l0IzRVv9hS/Q+ZP4SE7pi8fA+/tnqugyhxnV84kTbTAMUVY x2CufCmRi+nvUQRGtrT6yhptiSAdcAtO3NDoGj4GD4g6nLc0Xa/BxSiIRPYBfZYN3Tqf y3IxE6HtGgZq1Rt8DSgm+WaQwzXj5+6v55o5YO0inmpGCfDq65W8Ba3v4nTnQpKgudmx Ynm+MgW0cgKLqN8D9Hr8yv2NMHO7E7ORfZ0oh9H3C/7/n4+ERmpLIl/ps2Rls50W9r50 HYMA== X-Gm-Message-State: AOAM530XzqVir+ctEgsP3gjpSe4te8WjHD+oEXk+9t/js2n+iAxojP+I 8sQ9ekqLT9R+yUYRaPtwyN5dng== X-Google-Smtp-Source: ABdhPJw0y11rLC3V4HUBzuqKM0ykw3IE9QWgwHhJWilDvaIzUwRNg5fTpd0io672bC3r56rduJPllw== X-Received: by 2002:a17:906:9718:: with SMTP id k24mr32946631ejx.365.1600424403919; Fri, 18 Sep 2020 03:20:03 -0700 (PDT) Received: from localhost.localdomain ([2001:1715:4e26:a7e0:116c:c27a:3e7f:5eaf]) by smtp.gmail.com with ESMTPSA id r16sm1940674edc.57.2020.09.18.03.20.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 18 Sep 2020 03:20:03 -0700 (PDT) From: Jean-Philippe Brucker To: iommu@lists.linux-foundation.org, linux-arm-kernel@lists.infradead.org, linux-mm@kvack.org Cc: joro@8bytes.org, catalin.marinas@arm.com, will@kernel.org, robin.murphy@arm.com, baolu.lu@linux.intel.com, Jonathan.Cameron@huawei.com, jacob.jun.pan@linux.intel.com, zhangfei.gao@linaro.org, xuzaibo@huawei.com, fenghua.yu@intel.com, eric.auger@redhat.com, Jean-Philippe Brucker Subject: [PATCH v10 08/13] iommu/arm-smmu-v3: Share process page tables Date: Fri, 18 Sep 2020 12:18:48 +0200 Message-Id: <20200918101852.582559-9-jean-philippe@linaro.org> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20200918101852.582559-1-jean-philippe@linaro.org> References: <20200918101852.582559-1-jean-philippe@linaro.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable 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: 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. Introduce a new arm-smmu-v3-sva.c file and the CONFIG_ARM_SMMU_V3_SVA option to let users opt in SVA support. Signed-off-by: Jean-Philippe Brucker --- v10: use vabits_actual instead of VA_BITS --- drivers/iommu/Kconfig | 10 ++ drivers/iommu/arm/arm-smmu-v3/Makefile | 5 +- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h | 8 ++ .../iommu/arm/arm-smmu-v3/arm-smmu-v3-sva.c | 123 ++++++++++++++++++ drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 34 ++++- 5 files changed, 172 insertions(+), 8 deletions(-) create mode 100644 drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-sva.c diff --git a/drivers/iommu/Kconfig b/drivers/iommu/Kconfig index fb1787377eb6..b1d592cd9984 100644 --- a/drivers/iommu/Kconfig +++ b/drivers/iommu/Kconfig @@ -313,6 +313,16 @@ config ARM_SMMU_V3 Say Y here if your system includes an IOMMU device implementing the ARM SMMUv3 architecture. =20 +config ARM_SMMU_V3_SVA + bool "Shared Virtual Addressing support for the ARM SMMUv3" + depends on ARM_SMMU_V3 + help + Support for sharing process address spaces with devices using the + SMMUv3. + + Say Y here if your system supports SVA extensions such as PCIe PASID + and PRI. + config S390_IOMMU def_bool y if S390 && PCI depends on S390 && PCI diff --git a/drivers/iommu/arm/arm-smmu-v3/Makefile b/drivers/iommu/arm/a= rm-smmu-v3/Makefile index 569e24e9f162..54feb1ecccad 100644 --- a/drivers/iommu/arm/arm-smmu-v3/Makefile +++ b/drivers/iommu/arm/arm-smmu-v3/Makefile @@ -1,2 +1,5 @@ # SPDX-License-Identifier: GPL-2.0 -obj-$(CONFIG_ARM_SMMU_V3) +=3D arm-smmu-v3.o +obj-$(CONFIG_ARM_SMMU_V3) +=3D arm_smmu_v3.o +arm_smmu_v3-objs-y +=3D arm-smmu-v3.o +arm_smmu_v3-objs-$(CONFIG_ARM_SMMU_V3_SVA) +=3D arm-smmu-v3-sva.o +arm_smmu_v3-objs :=3D $(arm_smmu_v3-objs-y) diff --git a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h b/drivers/iommu/= arm/arm-smmu-v3/arm-smmu-v3.h index 51a9ce07b2d6..6b06a6f19604 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h @@ -540,6 +540,9 @@ struct arm_smmu_ctx_desc { u64 ttbr; u64 tcr; u64 mair; + + refcount_t refs; + struct mm_struct *mm; }; =20 struct arm_smmu_l1_ctx_desc { @@ -672,4 +675,9 @@ struct arm_smmu_domain { spinlock_t devices_lock; }; =20 +extern struct xarray arm_smmu_asid_xa; +extern struct mutex arm_smmu_asid_lock; + +bool arm_smmu_free_asid(struct arm_smmu_ctx_desc *cd); + #endif /* _ARM_SMMU_V3_H */ diff --git a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-sva.c b/drivers/io= mmu/arm/arm-smmu-v3/arm-smmu-v3-sva.c new file mode 100644 index 000000000000..6c1113059632 --- /dev/null +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-sva.c @@ -0,0 +1,123 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Implementation of the IOMMU SVA API for the ARM SMMUv3 + */ + +#include +#include +#include + +#include "arm-smmu-v3.h" +#include "../../io-pgtable-arm.h" + +static struct arm_smmu_ctx_desc * +arm_smmu_share_asid(struct mm_struct *mm, u16 asid) +{ + struct arm_smmu_ctx_desc *cd; + + cd =3D xa_load(&arm_smmu_asid_xa, asid); + if (!cd) + return NULL; + + if (cd->mm) { + if (WARN_ON(cd->mm !=3D mm)) + return ERR_PTR(-EINVAL); + /* All devices bound to this mm use the same cd struct. */ + refcount_inc(&cd->refs); + return cd; + } + + /* Ouch, ASID is already in use for a private cd. */ + return ERR_PTR(-EBUSY); +} + +__maybe_unused +static struct arm_smmu_ctx_desc *arm_smmu_alloc_shared_cd(struct mm_stru= ct *mm) +{ + u16 asid; + int err =3D 0; + u64 tcr, par, reg; + struct arm_smmu_ctx_desc *cd; + struct arm_smmu_ctx_desc *ret =3D NULL; + + asid =3D arm64_mm_context_get(mm); + if (!asid) + return ERR_PTR(-ESRCH); + + cd =3D kzalloc(sizeof(*cd), GFP_KERNEL); + if (!cd) { + err =3D -ENOMEM; + goto out_put_context; + } + + refcount_set(&cd->refs, 1); + + mutex_lock(&arm_smmu_asid_lock); + ret =3D arm_smmu_share_asid(mm, asid); + if (ret) { + mutex_unlock(&arm_smmu_asid_lock); + goto out_free_cd; + } + + err =3D xa_insert(&arm_smmu_asid_xa, asid, cd, GFP_KERNEL); + mutex_unlock(&arm_smmu_asid_lock); + + if (err) + goto out_free_asid; + + tcr =3D FIELD_PREP(CTXDESC_CD_0_TCR_T0SZ, 64ULL - vabits_actual) | + FIELD_PREP(CTXDESC_CD_0_TCR_IRGN0, ARM_LPAE_TCR_RGN_WBWA) | + FIELD_PREP(CTXDESC_CD_0_TCR_ORGN0, ARM_LPAE_TCR_RGN_WBWA) | + FIELD_PREP(CTXDESC_CD_0_TCR_SH0, ARM_LPAE_TCR_SH_IS) | + CTXDESC_CD_0_TCR_EPD1 | CTXDESC_CD_0_AA64; + + switch (PAGE_SIZE) { + case SZ_4K: + tcr |=3D FIELD_PREP(CTXDESC_CD_0_TCR_TG0, ARM_LPAE_TCR_TG0_4K); + break; + case SZ_16K: + tcr |=3D FIELD_PREP(CTXDESC_CD_0_TCR_TG0, ARM_LPAE_TCR_TG0_16K); + break; + case SZ_64K: + tcr |=3D FIELD_PREP(CTXDESC_CD_0_TCR_TG0, ARM_LPAE_TCR_TG0_64K); + break; + default: + WARN_ON(1); + err =3D -EINVAL; + goto out_free_asid; + } + + reg =3D read_sanitised_ftr_reg(SYS_ID_AA64MMFR0_EL1); + par =3D cpuid_feature_extract_unsigned_field(reg, ID_AA64MMFR0_PARANGE_= SHIFT); + tcr |=3D FIELD_PREP(CTXDESC_CD_0_TCR_IPS, par); + + cd->ttbr =3D virt_to_phys(mm->pgd); + cd->tcr =3D tcr; + /* + * MAIR value is pretty much constant and global, so we can just get it + * from the current CPU register + */ + cd->mair =3D read_sysreg(mair_el1); + cd->asid =3D asid; + cd->mm =3D mm; + + return cd; + +out_free_asid: + arm_smmu_free_asid(cd); +out_free_cd: + kfree(cd); +out_put_context: + arm64_mm_context_put(mm); + return err < 0 ? ERR_PTR(err) : ret; +} + +__maybe_unused +static void arm_smmu_free_shared_cd(struct arm_smmu_ctx_desc *cd) +{ + if (arm_smmu_free_asid(cd)) { + /* Unpin ASID */ + arm64_mm_context_put(cd->mm); + kfree(cd); + } +} diff --git a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c b/drivers/iommu/= arm/arm-smmu-v3/arm-smmu-v3.c index ce604abad384..19af27fd183b 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c @@ -68,7 +68,8 @@ struct arm_smmu_option_prop { const char *prop; }; =20 -static DEFINE_XARRAY_ALLOC1(asid_xa); +DEFINE_XARRAY_ALLOC1(arm_smmu_asid_xa); +DEFINE_MUTEX(arm_smmu_asid_lock); =20 static struct arm_smmu_option_prop arm_smmu_options[] =3D { { ARM_SMMU_OPT_SKIP_PREFETCH, "hisilicon,broken-prefetch-cmd" }, @@ -1003,7 +1004,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; @@ -1107,12 +1109,20 @@ static void arm_smmu_free_cd_tables(struct arm_sm= mu_domain *smmu_domain) cdcfg->cdtab =3D NULL; } =20 -static void arm_smmu_free_asid(struct arm_smmu_ctx_desc *cd) +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; =20 - xa_erase(&asid_xa, cd->asid); + free =3D refcount_dec_and_test(&cd->refs); + if (free) { + old_cd =3D xa_erase(&arm_smmu_asid_xa, cd->asid); + WARN_ON(old_cd !=3D cd); + } + return free; } =20 /* Stream table manipulation functions */ @@ -1800,9 +1810,12 @@ static void arm_smmu_domain_free(struct iommu_doma= in *domain) if (smmu_domain->stage =3D=3D ARM_SMMU_DOMAIN_S1) { struct arm_smmu_s1_cfg *cfg =3D &smmu_domain->s1_cfg; =20 + /* Prevent SVA from touching the CD while we're freeing it */ + mutex_lock(&arm_smmu_asid_lock); if (cfg->cdcfg.cdtab) arm_smmu_free_cd_tables(smmu_domain); arm_smmu_free_asid(&cfg->cd); + mutex_unlock(&arm_smmu_asid_lock); } else { struct arm_smmu_s2_cfg *cfg =3D &smmu_domain->s2_cfg; if (cfg->vmid) @@ -1822,10 +1835,14 @@ static int arm_smmu_domain_finalise_s1(struct arm= _smmu_domain *smmu_domain, struct arm_smmu_s1_cfg *cfg =3D &smmu_domain->s1_cfg; typeof(&pgtbl_cfg->arm_lpae_s1_cfg.tcr) tcr =3D &pgtbl_cfg->arm_lpae_s1= _cfg.tcr; =20 - ret =3D xa_alloc(&asid_xa, &asid, &cfg->cd, + refcount_set(&cfg->cd.refs, 1); + + /* Prevent SVA from modifying the ASID until it is written to the CD */ + mutex_lock(&arm_smmu_asid_lock); + ret =3D xa_alloc(&arm_smmu_asid_xa, &asid, &cfg->cd, XA_LIMIT(1, (1 << smmu->asid_bits) - 1), GFP_KERNEL); if (ret) - return ret; + goto out_unlock; =20 cfg->s1cdmax =3D master->ssid_bits; =20 @@ -1853,12 +1870,15 @@ static int arm_smmu_domain_finalise_s1(struct arm= _smmu_domain *smmu_domain, if (ret) goto out_free_cd_tables; =20 + mutex_unlock(&arm_smmu_asid_lock); return 0; =20 out_free_cd_tables: arm_smmu_free_cd_tables(smmu_domain); out_free_asid: arm_smmu_free_asid(&cfg->cd); +out_unlock: + mutex_unlock(&arm_smmu_asid_lock); return ret; } =20 --=20 2.28.0