Received: by 2002:a05:7412:3b8b:b0:fc:a2b0:25d7 with SMTP id nd11csp1052768rdb; Fri, 9 Feb 2024 08:56:11 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCUJ5CTIrFIdpAAzzOOHoe3dDI4XgLDOdOz2HMMk8VXZ492MzGG4wtUYuD7aidUti7J/9SAefMzIntW8zPkj2PYD8NplYPJJ5Dsdm3O3AQ== X-Google-Smtp-Source: AGHT+IEwSBI6K/SugdW4+mr+koZGI+uWzrPkDJ2pmAeHcugk0RSzj/70xndkmYk0IOERVKpNz72r X-Received: by 2002:a17:906:bc47:b0:a3b:ec24:a7f8 with SMTP id s7-20020a170906bc4700b00a3bec24a7f8mr1756404ejv.34.1707497770993; Fri, 09 Feb 2024 08:56:10 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707497770; cv=pass; d=google.com; s=arc-20160816; b=YAwq+1gCkZuJtRcCCaxMaGJp98/11J8n4JQJ/+nuOHvVYmp4ZGu2O+kYTuY3D/sIUh rTT7hzXGigC50Ar1+TPaEsuM4nAfsDubdwlqCdec05MWxdYMwOPYfXI+RMyj14a7byf3 p1moJ/Kr6gCMxBGpgdCFYSzaDgIutUcjGERjJBJ25Mrb3Yyqvb9mhXreG6v9n1C8Jvl3 Or+YLtWHS/b3H5UB2m7XYQr/XNWoYgvGUFSXp+VenxXhge5z8gvQaZ5E0n1TqQ5sxLtG ycp7JgdxNgS+/Hta0fPwv0VMcDtGuhuyrK7VXQL+RbVHdZC3xoSH7Csu6DoICF5PwzqI v7+Q== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from; bh=cjFmI2ANS7b2tzm6bYo6wlDk3GhB/mMNddCV3ifPOS8=; fh=iioAR7EgzZOz6mWqXsESrtNnyk1p9vmNCth29imyYlE=; b=dH9q4nVCfkKlE9f+TMj407jTLwIw1+Sy8t9vxFLsDmfJf6fduPvdocTs0J3Tuxcp6h PmNvwXPujGIxrnQBo/3VnQHH9mkBkcMAh5qP5iNFpSd7vZHpCY/S8pBuYK/em0ksGxMa l8qUlOXVVnws6azj5X+7czuq//rH5dF6sjsHDISSSFDbcvDUjjW12x6/eNBvy0+rORIw bCGKmxYrHiD1jUbxQ7mu/gD4ixTjWgE6SLm6JXC7sgi9AcGA74WTbxD8BKiXOu0+TiE6 alayuBNfDiupZk5Z4P3yFUvXoddEOM6IxGzyAhI7rt8hJpDZXIx78BeozSgBqDr/N6ou GcTQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1 spf=pass spfdomain=arm.com dmarc=pass fromdomain=arm.com); spf=pass (google.com: domain of linux-kernel+bounces-59677-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-59677-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com X-Forwarded-Encrypted: i=2; AJvYcCUBDJ3W1b2HL/oz6jkC6QQUSa9exUdcKRaJWmWwBG5PHPKE3EQ55OP5b2FKhWIeEuqiqIyJJtrlyb27A5/OE8SJlH2wgxxeaLJlqoM+Ag== Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id lz8-20020a170906fb0800b00a3856fdb6aesi1038996ejb.132.2024.02.09.08.56.10 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 09 Feb 2024 08:56:10 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-59677-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; arc=pass (i=1 spf=pass spfdomain=arm.com dmarc=pass fromdomain=arm.com); spf=pass (google.com: domain of linux-kernel+bounces-59677-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-59677-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id 775A21F22017 for ; Fri, 9 Feb 2024 16:56:10 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id ECBD612BEBD; Fri, 9 Feb 2024 16:50:45 +0000 (UTC) Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 6C22012BE9C; Fri, 9 Feb 2024 16:50:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=217.140.110.172 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707497445; cv=none; b=KAYtTCVNW1/6Y3iJAllZT70nWK3YmX54Z637vHF9hvabH0McVDSRQme1xDEcienHJ66FgQjihLw1W1xwWPMLr9KdJisIsxoVfrjaVJd7nRKoFJ6hGQseQ1jYHCrDXyg+nP4gkGY7y9rwAw4RUjD8IafeClDW1xAV2jz42yl1z3k= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707497445; c=relaxed/simple; bh=dj4HiXbIQbXIq5aOa8YaHipGKB16kgEAFi56Lehan3Q=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=OBsw6z3/52m54NhyPuqYkLt9bT/qAdXhCdaJBUoflLOoJLIa5+ep120IDf4OSwkYwbRmCVbbhM3tuRAz6ijuT9g6ZacaAuE5jIo+K40xC3Fh3q9dOrwXmgaKFLZt80/rixdzhL7VAOVEXH9+La71yQvSNSP8q4/Dlo5uduTWEIM= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com; spf=pass smtp.mailfrom=arm.com; arc=none smtp.client-ip=217.140.110.172 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=arm.com 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 1847EFEC; Fri, 9 Feb 2024 08:51:25 -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 42FEA3F64C; Fri, 9 Feb 2024 08:50:39 -0800 (PST) From: Robin Murphy To: 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 v3 5/7] iommu/dma: Make limit checks self-contained Date: Fri, 9 Feb 2024 16:50:02 +0000 Message-Id: <2f9fe1c4fd29043db3d61d265bdf9908bb7fa85c.1707493264.git.robin.murphy@arm.com> X-Mailer: git-send-email 2.39.2.101.g768bb238c484.dirty In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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 dbe3c225e0d5..52126f73f690 100644 --- a/drivers/iommu/dma-iommu.c +++ b/drivers/iommu/dma-iommu.c @@ -660,19 +660,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; @@ -684,18 +681,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 */ @@ -1746,7 +1743,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