Received: by 2002:a05:7412:8d10:b0:f3:1519:9f41 with SMTP id bj16csp5464799rdb; Wed, 13 Dec 2023 09:18:59 -0800 (PST) X-Google-Smtp-Source: AGHT+IFTIeXqFkUZA21UpgbmFKNHI14Y0drjXY/dCFaiXux+5m5+DWRotGvOacrRKua2w23XgQl+ X-Received: by 2002:a05:6e02:190e:b0:35d:9c00:c348 with SMTP id w14-20020a056e02190e00b0035d9c00c348mr8843766ilu.102.1702487938959; Wed, 13 Dec 2023 09:18:58 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702487938; cv=none; d=google.com; s=arc-20160816; b=xvqYi/H0JjuQVQtfkOAccD9vucRQCdR+Uu1XYtPlbG5AVSteokXYo7vwUg0CRM3jeF HrBYHl/gv4OYSMDFll0vzpOErorUm3ihaXCU6GDkAN4AkCrEiTs3ureQzn/Y7jLHOT6i UuUDLZaD5zjahtbs4XciL9xVRB+TfjMeJuvlW7Np0tT07pAEj2AC3czzXIphojs10pnU aPyneSrIy5hTeFtCK49lJma8wPovGyPbTZMr+t9LMBWRD1vWfc8fW9QC6NU+k9jQ2Icm 1tUMZxd/SNTOhzWpK6h+Bd0LekmjXW+gdUxOIekQZQS4+soYtzTL1C5MQKpF+ZiVSOnn l/mg== 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; bh=TNhhiPUrYAJVca+n/RRbc7DmRTcpEiwslP8yS2Hn92Y=; fh=DTaH2EVASKNnUTo+khl4jHZ5aV5048FEathUcD0bxLo=; b=Qdk/TtFrjddNq1mz9hrlyr35qG9+Ca66WgTlDvBr+FiQCqkcZntQA4LTLYS5pF1ZPc hVsThxlUtwB86Umd6Az6p1kHcENimu1bc0sWs+JKH4ST3ipRZsRVtro6JIh72PFTWaB8 c/wIdI5VNFVIdhXSv5elzA2rZa4xoo+2aQUonTSfITe+yDW4MMLt5kGJa6Scg/Ns0+Sd l1zApJ4gImPOZkY5tzD0VHkGXsxTgS/m9BuQvZy+qEly8dsQRi275LPN+arCW3aEubJQ zHBym4SUHvF91cpnEZ9xxBmNIjHd4iBkl1rjnsXbMzkIkZF3J1mD4mOz2QDJIYU8gwlR 9ZLQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Return-Path: Received: from snail.vger.email (snail.vger.email. [23.128.96.37]) by mx.google.com with ESMTPS id y24-20020a634b18000000b005c6b4e190absi9537293pga.502.2023.12.13.09.18.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 13 Dec 2023 09:18:58 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) client-ip=23.128.96.37; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by snail.vger.email (Postfix) with ESMTP id CADED802FB99; Wed, 13 Dec 2023 09:18:57 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1379000AbjLMRSq (ORCPT + 99 others); Wed, 13 Dec 2023 12:18:46 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39602 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1379271AbjLMRSk (ORCPT ); Wed, 13 Dec 2023 12:18:40 -0500 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 77C6E184; Wed, 13 Dec 2023 09:18:43 -0800 (PST) Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 11FC61516; Wed, 13 Dec 2023 09:19:29 -0800 (PST) Received: from e121345-lin.cambridge.arm.com (e121345-lin.cambridge.arm.com [10.1.196.40]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id 07F9C3F762; Wed, 13 Dec 2023 09:18:38 -0800 (PST) From: Robin Murphy To: Joerg Roedel , Christoph Hellwig Cc: Vineet Gupta , Russell King , Catalin Marinas , Will Deacon , Huacai Chen , WANG Xuerui , Thomas Bogendoerfer , Paul Walmsley , Palmer Dabbelt , Albert Ou , Lorenzo Pieralisi , Hanjun Guo , Sudeep Holla , "K. Y. Srinivasan" , Haiyang Zhang , Wei Liu , Dexuan Cui , Suravee Suthikulpanit , David Woodhouse , Lu Baolu , Niklas Schnelle , Matthew Rosato , Gerald Schaefer , Jean-Philippe Brucker , Rob Herring , Frank Rowand , Marek Szyprowski , Jason Gunthorpe , linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-acpi@vger.kernel.org, iommu@lists.linux.dev, devicetree@vger.kernel.org, Jason Gunthorpe Subject: [PATCH v2 5/7] iommu/dma: Make limit checks self-contained Date: Wed, 13 Dec 2023 17:17:58 +0000 Message-Id: <6d1aac26db65bf439f4625986ca76be520774759.1702486837.git.robin.murphy@arm.com> X-Mailer: git-send-email 2.39.2.101.g768bb238c484.dirty In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-4.2 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_NONE,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (snail.vger.email [0.0.0.0]); Wed, 13 Dec 2023 09:18:57 -0800 (PST) It's now easy to retrieve the device's DMA limits if we want to check them against the domain aperture, so do that ourselves instead of relying on them being passed through the callchain. Reviewed-by: Jason Gunthorpe Signed-off-by: Robin Murphy --- drivers/iommu/dma-iommu.c | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/drivers/iommu/dma-iommu.c b/drivers/iommu/dma-iommu.c index 5dc012220ca9..27a167f4cd3e 100644 --- a/drivers/iommu/dma-iommu.c +++ b/drivers/iommu/dma-iommu.c @@ -659,19 +659,16 @@ static void iommu_dma_init_options(struct iommu_dma_options *options, /** * 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 - * @limit: Last address of the IOVA space * @dev: Device the domain is being initialised for * - * @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 + * If the geometry and dma_range_map include address 0, we reserve that page * 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, - dma_addr_t limit, struct device *dev) +static int iommu_dma_init_domain(struct iommu_domain *domain, struct device *dev) { struct iommu_dma_cookie *cookie = domain->iova_cookie; + const struct bus_dma_region *map = dev->dma_range_map; unsigned long order, base_pfn; struct iova_domain *iovad; int ret; @@ -683,18 +680,18 @@ static int iommu_dma_init_domain(struct iommu_domain *domain, dma_addr_t base, /* Use the smallest supported page size for IOVA granularity */ order = __ffs(domain->pgsize_bitmap); - base_pfn = max_t(unsigned long, 1, base >> order); + base_pfn = 1; /* Check the domain allows at least some access to the device... */ - if (domain->geometry.force_aperture) { + if (map) { + dma_addr_t base = dma_range_map_min(map); if (base > domain->geometry.aperture_end || - limit < domain->geometry.aperture_start) { + dma_range_map_max(map) < domain->geometry.aperture_start) { pr_warn("specified DMA range outside IOMMU capability\n"); return -EFAULT; } /* ...then finally give it a kicking to make sure it fits */ - base_pfn = max_t(unsigned long, base_pfn, - domain->geometry.aperture_start >> order); + base_pfn = max(base, domain->geometry.aperture_start) >> order; } /* start_pfn is always nonzero for an already-initialised domain */ @@ -1743,7 +1740,7 @@ void iommu_setup_dma_ops(struct device *dev, u64 dma_base, u64 dma_limit) * underlying IOMMU driver needs to support via the dma-iommu layer. */ if (iommu_is_dma_domain(domain)) { - if (iommu_dma_init_domain(domain, dma_base, dma_limit, dev)) + if (iommu_dma_init_domain(domain, dev)) goto out_err; dev->dma_ops = &iommu_dma_ops; } -- 2.39.2.101.g768bb238c484.dirty