From patchwork Thu Jun 10 07:51:31 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jean-Philippe Brucker X-Patchwork-Id: 457829 Delivered-To: patch@linaro.org Received: by 2002:a02:735a:0:0:0:0:0 with SMTP id a26csp165802jae; Thu, 10 Jun 2021 01:04:21 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxo5clDWi5nswZZ7Q1EJrqc9+92dbT/NH+0YvWxiR/sf5e32VT5Wp86Bm2wOGqgNqrlrsPo X-Received: by 2002:a05:6402:685:: with SMTP id f5mr3400163edy.178.1623312260926; Thu, 10 Jun 2021 01:04:20 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1623312260; cv=none; d=google.com; s=arc-20160816; b=eKvO1/vPTK44FbVK2koNFWMdK3M5V4P2XH46dTb/P35u6C8LHd4OEsGgbalffZ+yAR XO4eBLezYGVdiq3mik3sygR7eaM065na5vTCdvqafs7pqLeGgPkzjMJSEHijnnav3b+1 +1EEUm6kId/Zok8H3Nf7Cx5NI1r8wnjEmDngIZYOkJePAyyQjqlx/APCl/XqIWZGmTUb Urn9aA0taLhk57Y3X5CRewn4UCca58XHheoWpSfOp3CdgRpPEx4oImBuq5Vl15gAOEEI VB46aAVpsVQwRley7JKVhM+TQRdilE4C60zHSmd4QGZZDXMjomG8/p8YTwOWcJjR4pgs ZKCA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=T0y2iCeYYHmrE1fv4Hgj0FTcnFeMz1q5JiDftJFl4po=; b=X8ks8b+IwTDTk3To28hlILZH38qjMSduCT5ddzwZaPYELfsWwmPFAk9JpkXKk5CEw7 xxmw25B1/CYiskY4RI/4jrlDPU02M9OxN22V5tHhH2OJBTaTPsq/Dhj1HrwBj7+kAEqH dUO0ZF8jmBIvHJ3EPJikOZPBcDFzd2MA5FwBIK9Zs22tAg7gJW6A2DyO2KQ2zVpIBOw5 LtKv8HXE4vTmTLW1GoYHGkHITC9Ywdz8KGkbwweI8Z3wCXFLcBJ1AD3wTh5GuFsB/73T OwRnbOXe2BbN4rXpvZhVuSFXmKU9Md1IEbraUg6onQBmrGK3C7ULaiKHeGb5RA7M1PAx /RjQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=piloNKZ3; spf=pass (google.com: domain of linux-acpi-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-acpi-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id u11si1794275edx.42.2021.06.10.01.04.20; Thu, 10 Jun 2021 01:04:20 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-acpi-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=piloNKZ3; spf=pass (google.com: domain of linux-acpi-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-acpi-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230186AbhFJIFs (ORCPT + 4 others); Thu, 10 Jun 2021 04:05:48 -0400 Received: from mail-wm1-f47.google.com ([209.85.128.47]:41820 "EHLO mail-wm1-f47.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229941AbhFJIFs (ORCPT ); Thu, 10 Jun 2021 04:05:48 -0400 Received: by mail-wm1-f47.google.com with SMTP id l11-20020a05600c4f0bb029017a7cd488f5so5847019wmq.0 for ; Thu, 10 Jun 2021 01:03:52 -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=T0y2iCeYYHmrE1fv4Hgj0FTcnFeMz1q5JiDftJFl4po=; b=piloNKZ3ohr7pJlYL+8Vi7WuxS6ZzeMjvv2uKYon90XrjsxqgLFIbwbfB4kJsdA0v7 0ozBa16idjurywVrDqinefJkY/MNbqYRRsY+mXLdox7/NHHMSpXwsHUTtUdD6mqF5MeE 1j0v2DZOWc6tt+q5oa9zHrZx7DL4vKbfMqbvK7ZI4Sc5wHTFmZsz37xjxNu7N7O6u2DM V2PGp+w/RUs9vhhztPS7IEFj5dwI8ACn3a9ZurIh+axORWmDDJPjXHpN+itozM2lY/we gh+d/rlXU14QiQJZvmQLqOVhYAKcDR7N+3oELLHtsKCQczCwG0bP+72HbuGcXv3q3fRO 5hNw== 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=T0y2iCeYYHmrE1fv4Hgj0FTcnFeMz1q5JiDftJFl4po=; b=nPwKXHGDMcyZaaVEoiuA7+zMfsnXSWkYL9T7UHvyy/kqG3CFHQ9DH2nm9kcJRwF8ql Xm/WHB5/76b9lHHFF4XlUIfE1eA2VT2TqxU1Oovjmzt9KwSoAxPzDKw0v41yOyqDK/CY kqei4Qrrnol9DaZoGP1xx4HxgoO5hIjcvbrS9JVlmVy/kvhiwn0as+uCf+CHA21G/hK+ nJAZRNmYNFHyotrbrYwjA8Xq2ccAcvdPjbmfZxDay4YwEK/imIrEAZ9vY8RMdQmxCa39 dmxwqiEz66YGFFttsNUMOUU686wB1b51jMOrTW/XvCaSFpZpNCtMdnEbXll5X1GnnA/C k8JQ== X-Gm-Message-State: AOAM530wV4T5Q8MVWgLPTPtOLdGvVSgnVcoqgKseOzUtKu8xrEdDPObS Ff3mDYP6RoiZgt4NQjs7pXRMig== X-Received: by 2002:a1c:a54b:: with SMTP id o72mr3678130wme.124.1623312171684; Thu, 10 Jun 2021 01:02:51 -0700 (PDT) Received: from localhost.localdomain (adsl-84-226-111-173.adslplus.ch. [84.226.111.173]) by smtp.gmail.com with ESMTPSA id o3sm2715059wrm.78.2021.06.10.01.02.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 10 Jun 2021 01:02:51 -0700 (PDT) From: Jean-Philippe Brucker To: rjw@rjwysocki.net, lenb@kernel.org, joro@8bytes.org, mst@redhat.com Cc: will@kernel.org, catalin.marinas@arm.com, baolu.lu@linux.intel.com, dwmw2@infradead.org, linux-acpi@vger.kernel.org, iommu@lists.linux-foundation.org, virtualization@lists.linux-foundation.org, linux-arm-kernel@lists.infradead.org, eric.auger@redhat.com, sebastien.boeuf@intel.com, robin.murphy@arm.com, kevin.tian@intel.com, lorenzo.pieralisi@arm.com, guohanjun@huawei.com, sudeep.holla@arm.com, jean-philippe@linaro.org Subject: [PATCH v4 4/6] iommu/dma: Pass address limit rather than size to iommu_setup_dma_ops() Date: Thu, 10 Jun 2021 09:51:31 +0200 Message-Id: <20210610075130.67517-5-jean-philippe@linaro.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210610075130.67517-1-jean-philippe@linaro.org> References: <20210610075130.67517-1-jean-philippe@linaro.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-acpi@vger.kernel.org Passing a 64-bit address width to iommu_setup_dma_ops() is valid on virtual platforms, but isn't currently possible. The overflow check in iommu_dma_init_domain() prevents this even when @dma_base isn't 0. Pass a limit address instead of a size, so callers don't have to fake a size to work around the check. Signed-off-by: Jean-Philippe Brucker --- include/linux/dma-iommu.h | 4 ++-- arch/arm64/mm/dma-mapping.c | 2 +- drivers/iommu/amd/iommu.c | 2 +- drivers/iommu/dma-iommu.c | 12 ++++++------ drivers/iommu/intel/iommu.c | 2 +- 5 files changed, 11 insertions(+), 11 deletions(-) -- 2.31.1 diff --git a/include/linux/dma-iommu.h b/include/linux/dma-iommu.h index 6e75a2d689b4..758ca4694257 100644 --- a/include/linux/dma-iommu.h +++ b/include/linux/dma-iommu.h @@ -19,7 +19,7 @@ int iommu_get_msi_cookie(struct iommu_domain *domain, dma_addr_t base); void iommu_put_dma_cookie(struct iommu_domain *domain); /* Setup call for arch DMA mapping code */ -void iommu_setup_dma_ops(struct device *dev, u64 dma_base, u64 size); +void iommu_setup_dma_ops(struct device *dev, u64 dma_base, u64 dma_limit); /* The DMA API isn't _quite_ the whole story, though... */ /* @@ -50,7 +50,7 @@ struct msi_msg; struct device; static inline void iommu_setup_dma_ops(struct device *dev, u64 dma_base, - u64 size) + u64 dma_limit) { } diff --git a/arch/arm64/mm/dma-mapping.c b/arch/arm64/mm/dma-mapping.c index 4bf1dd3eb041..7bd1d2199141 100644 --- a/arch/arm64/mm/dma-mapping.c +++ b/arch/arm64/mm/dma-mapping.c @@ -50,7 +50,7 @@ void arch_setup_dma_ops(struct device *dev, u64 dma_base, u64 size, dev->dma_coherent = coherent; if (iommu) - iommu_setup_dma_ops(dev, dma_base, size); + iommu_setup_dma_ops(dev, dma_base, size - dma_base - 1); #ifdef CONFIG_XEN if (xen_swiotlb_detect()) diff --git a/drivers/iommu/amd/iommu.c b/drivers/iommu/amd/iommu.c index 3ac42bbdefc6..94b96d81fcfd 100644 --- a/drivers/iommu/amd/iommu.c +++ b/drivers/iommu/amd/iommu.c @@ -1713,7 +1713,7 @@ static void amd_iommu_probe_finalize(struct device *dev) /* Domains are initialized for this device - have a look what we ended up with */ domain = iommu_get_domain_for_dev(dev); if (domain->type == IOMMU_DOMAIN_DMA) - iommu_setup_dma_ops(dev, IOVA_START_PFN << PAGE_SHIFT, 0); + iommu_setup_dma_ops(dev, IOVA_START_PFN << PAGE_SHIFT, U64_MAX); else set_dma_ops(dev, NULL); } diff --git a/drivers/iommu/dma-iommu.c b/drivers/iommu/dma-iommu.c index 7bcdd1205535..c62e19bed302 100644 --- a/drivers/iommu/dma-iommu.c +++ b/drivers/iommu/dma-iommu.c @@ -319,16 +319,16 @@ static bool dev_is_untrusted(struct device *dev) * iommu_dma_init_domain - Initialise a DMA mapping domain * @domain: IOMMU domain previously prepared by iommu_get_dma_cookie() * @base: IOVA at which the mappable address space starts - * @size: Size of IOVA space + * @limit: Last address of the IOVA space * @dev: Device the domain is being initialised for * - * @base and @size should be exact multiples of IOMMU page granularity to + * @base and @limit + 1 should be exact multiples of IOMMU page granularity to * avoid rounding surprises. If necessary, we reserve the page at address 0 * to ensure it is an invalid IOVA. It is safe to reinitialise a domain, but * any change which could make prior IOVAs invalid will fail. */ static int iommu_dma_init_domain(struct iommu_domain *domain, dma_addr_t base, - u64 size, struct device *dev) + dma_addr_t limit, struct device *dev) { struct iommu_dma_cookie *cookie = domain->iova_cookie; unsigned long order, base_pfn; @@ -346,7 +346,7 @@ static int iommu_dma_init_domain(struct iommu_domain *domain, dma_addr_t base, /* Check the domain allows at least some access to the device... */ if (domain->geometry.force_aperture) { if (base > domain->geometry.aperture_end || - base + size <= domain->geometry.aperture_start) { + limit < domain->geometry.aperture_start) { pr_warn("specified DMA range outside IOMMU capability\n"); return -EFAULT; } @@ -1308,7 +1308,7 @@ static const struct dma_map_ops iommu_dma_ops = { * The IOMMU core code allocates the default DMA domain, which the underlying * IOMMU driver needs to support via the dma-iommu layer. */ -void iommu_setup_dma_ops(struct device *dev, u64 dma_base, u64 size) +void iommu_setup_dma_ops(struct device *dev, u64 dma_base, u64 dma_limit) { struct iommu_domain *domain = iommu_get_domain_for_dev(dev); @@ -1320,7 +1320,7 @@ void iommu_setup_dma_ops(struct device *dev, u64 dma_base, u64 size) * underlying IOMMU driver needs to support via the dma-iommu layer. */ if (domain->type == IOMMU_DOMAIN_DMA) { - if (iommu_dma_init_domain(domain, dma_base, size, dev)) + if (iommu_dma_init_domain(domain, dma_base, dma_limit, dev)) goto out_err; dev->dma_ops = &iommu_dma_ops; } diff --git a/drivers/iommu/intel/iommu.c b/drivers/iommu/intel/iommu.c index be35284a2016..85f18342603c 100644 --- a/drivers/iommu/intel/iommu.c +++ b/drivers/iommu/intel/iommu.c @@ -5171,7 +5171,7 @@ static void intel_iommu_probe_finalize(struct device *dev) if (domain && domain->type == IOMMU_DOMAIN_DMA) iommu_setup_dma_ops(dev, base, - __DOMAIN_MAX_ADDR(dmar_domain->gaw) - base); + __DOMAIN_MAX_ADDR(dmar_domain->gaw)); else set_dma_ops(dev, NULL); }