From patchwork Fri Jun 18 15:20:59 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: 463065 Delivered-To: patch@linaro.org Received: by 2002:a02:735a:0:0:0:0:0 with SMTP id a26csp1587218jae; Fri, 18 Jun 2021 08:33:05 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwFEBb48+7zwX12Xn7lOBcBWtDkcmIe7D9/J/Hi2QOYRHnT8TZ2J+qwydZ/lZfbu5VGFF8k X-Received: by 2002:a05:6402:4395:: with SMTP id o21mr5845602edc.163.1624030384928; Fri, 18 Jun 2021 08:33:04 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1624030384; cv=none; d=google.com; s=arc-20160816; b=lXGw9MiTT6PqXvSlxaeUJ0ZGaxyAJpfgtZC47FB0YG5LBlHvAZjCjSAPuHx8sT7YnO k5gCPdNp8bpub0AyFXf5BkJNqrDe3uzY43ToJup4M9f4sXAK1yLXBxF2sjVoYRPJOVhA ZdQP2C31lbqJ80lv3RQqtnV4yCNfi1cXXCHy+8G77RsRUXzYaV7TCeVkqZMBBeQH2nhX YfaqQlOnH8iPpNss+JGJL3efN0tiDVexWdpkOFuF+qcgLXQoLQjglxHbdM0AB4qZ0BVq /F0UJN5PHHiIuP7Mqq2tL3lKklZZyZbfz3hUEV++gcIbU7iaxh7fKEXeLlgXzLBT6vU5 z6Tg== 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=Bu0G1LHNE5nJCUh4BXg8gLzVb7Bp3apdXNqzC8NigtY=; b=HEWkg5c6rwYak3h1R8MN2Ak091/OM/gFS7EpVGOA11yC6JZ9eauviX4CzvE7/LsiJS ScMbRWlcZxG5Tv+bB2eCKQFiZuuloeNyfuT1fxbHUOQ3Kdf85/hCo57MaPmbr7AakBf7 rHjmSSb/vbxQWozfKeRXBUJwQbqDLloWUDMzdhSif64C5GIoOKv1OWJsJo/AmJIRyT03 KkbJmujZ1v0TBVxONxqc13lhSMGQ/TgCtGVil5Vm9xnA4WrHQutA9KDDcxNhn6ODYatx OrxXmz0iDWsP3XDvAwApAy3kAB7jCE8Ix0gVmz1F3U2VBJSyi7LZhXqqsT7vk1RWB3Ee 0j0g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=XEjpawz4; 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 k3si5637868eds.175.2021.06.18.08.33.04; Fri, 18 Jun 2021 08:33:04 -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=XEjpawz4; 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 S235542AbhFRPfL (ORCPT + 4 others); Fri, 18 Jun 2021 11:35:11 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59416 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235661AbhFRPey (ORCPT ); Fri, 18 Jun 2021 11:34:54 -0400 Received: from mail-ed1-x533.google.com (mail-ed1-x533.google.com [IPv6:2a00:1450:4864:20::533]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 42A97C061A2E for ; Fri, 18 Jun 2021 08:30:25 -0700 (PDT) Received: by mail-ed1-x533.google.com with SMTP id b11so9194874edy.4 for ; Fri, 18 Jun 2021 08:30:25 -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=Bu0G1LHNE5nJCUh4BXg8gLzVb7Bp3apdXNqzC8NigtY=; b=XEjpawz4x8k66j8XDfEM2ftRJ1k1+cfbMyEbQJn1YJ62mh4uu9tHSFczuwUtqMw4ky Hg+AzKMzz0qMcFLE+gpFRrA3kUr2n7HF7Vg3Gmgf2bVSJvO9hTmglEJHU3nrjSk2ZF07 0xcjPAlX/5O7NI3j2IjdnnfVrY3rrSVVhYVhWWaMjktp4cIvW39Z3odZkENA73l7xIm+ jeDVLjlQ+8hVISfFkNsJzZLRLLdm2dqLzZUl0uCBhDY2lu9vIt+lW2s7Si4DyReXkrRZ 6ve9wq8kf/y24abGq+N5PhXM6ci6fi49cF8H8AZkuBvXEAEjMLjDpcrlqJYxJFpgw1tw 6dSA== 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=Bu0G1LHNE5nJCUh4BXg8gLzVb7Bp3apdXNqzC8NigtY=; b=BGd4hOl0wfJMFMy1B2mbX4dq+bUI6IV5ziSFoPW8XSm2mM2/Lb7O2HHI69xwLWg79y aLxc/+i7TGodJfw8E3lpioVlilkZIQNsN8BWjNiAvfu6ik4BJRU8LBwkBJQRtaWrm6zb SqO6KV6+AdSPNKY+1i/sGhdMzLVmNb5HHcqkov39zN+8MEmdZ0e0hEczbgv9aXLly5Jr miuw8XNz7TOvnGruwuo8VW9FO0Nb21u36yTqws6xXE1JVkEdYQxOBWm1dXfuhqXWX7DX KrMNeU+/z54gm2HyfN8kHXJk/wUEcgOCHzqhQ7baK1Kd/DhZC+4+eOe3KPMI/QmcNfcN ayvg== X-Gm-Message-State: AOAM5323Id1VjciuHYwDHxA5vb9h44ETtqvJC1ChyIcWVkE7JjH74lDI 1a/vf/XMwyDWk643TKedP+2xhg== X-Received: by 2002:a05:6402:27c9:: with SMTP id c9mr5875203ede.371.1624030224251; Fri, 18 Jun 2021 08:30:24 -0700 (PDT) Received: from localhost.localdomain (adsl-84-226-111-173.adslplus.ch. [84.226.111.173]) by smtp.gmail.com with ESMTPSA id bn7sm1197613ejb.111.2021.06.18.08.30.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 18 Jun 2021 08:30:23 -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 v5 4/5] iommu/dma: Pass address limit rather than size to iommu_setup_dma_ops() Date: Fri, 18 Jun 2021 17:20:59 +0200 Message-Id: <20210618152059.1194210-5-jean-philippe@linaro.org> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20210618152059.1194210-1-jean-philippe@linaro.org> References: <20210618152059.1194210-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. The base and limit parameters are being phased out, because: * they are redundant for x86 callers. dma-iommu already reserves the first page, and the upper limit is already in domain->geometry. * they can now be obtained from dev->dma_range_map on Arm. But removing them on Arm isn't completely straightforward so is left for future work. As an intermediate step, simplify the x86 callers by passing dummy limits. 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 | 5 +---- 5 files changed, 11 insertions(+), 14 deletions(-) -- 2.32.0 Reviewed-by: Robin Murphy Reviewed-by: Eric Auger 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..6719f9efea09 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, dma_base + size - 1); #ifdef CONFIG_XEN if (xen_swiotlb_detect()) diff --git a/drivers/iommu/amd/iommu.c b/drivers/iommu/amd/iommu.c index 3ac42bbdefc6..216323fb27ef 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, 0, 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..2f7213f0e7a1 100644 --- a/drivers/iommu/intel/iommu.c +++ b/drivers/iommu/intel/iommu.c @@ -5165,13 +5165,10 @@ static void intel_iommu_release_device(struct device *dev) static void intel_iommu_probe_finalize(struct device *dev) { - dma_addr_t base = IOVA_START_PFN << VTD_PAGE_SHIFT; struct iommu_domain *domain = iommu_get_domain_for_dev(dev); - struct dmar_domain *dmar_domain = to_dmar_domain(domain); if (domain && domain->type == IOMMU_DOMAIN_DMA) - iommu_setup_dma_ops(dev, base, - __DOMAIN_MAX_ADDR(dmar_domain->gaw) - base); + iommu_setup_dma_ops(dev, 0, U64_MAX); else set_dma_ops(dev, NULL); }