Received: by 2002:ab2:6203:0:b0:1f5:f2ab:c469 with SMTP id o3csp773309lqt; Fri, 19 Apr 2024 09:57:51 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCU1COluoorcVoM5akuOqZrhRb7OtxwYe05PYgTh2fhlaVlt6phXsOVqSMcet01EjMBAgoE1XgeAx5ZaGeOTQ1s8vL7ZQ/Bdy0fBGu+ZNw== X-Google-Smtp-Source: AGHT+IGhPgfSIe71GdK2Zd4m26Lee2Xs/GFaLqmevd44IljbqTJyCu/Pik4Kc80LevpbDAZKbgOg X-Received: by 2002:a17:903:41cf:b0:1e4:fd4:48d0 with SMTP id u15-20020a17090341cf00b001e40fd448d0mr3754068ple.62.1713545871393; Fri, 19 Apr 2024 09:57:51 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1713545871; cv=pass; d=google.com; s=arc-20160816; b=bxCIHEyzXcpqawpTbs/5AiwoWqnoQNWw8pZ6I3WOrd7YOnFnyaolA5Bpv7RQwESbdj lBbivf9iwRyPqiCO3IoGKRYVrlARADlViN++9in6rzjFh7mn3ajanjvxQUheBDFMoiwK fV3UfDiL5EAmyj1SybBv9nE9QQnpdjibbsgN2ACcpkwygESzHB07lsBLfP3OsrZOOOTm /35JUTzL4zB0ZudyVhngRxZsHEy4mqA3qIuvG8QQPnU7ez9BZ/rgy+HYPRBsxHz8pJ3r 0XZ7EgzM44Fh9MMPKRA4961LVeUjHldAV68l90wb1hGi328u3yH/N3uFCrnTHPU7eEIL fMCw== 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=WmpVLE5sl14IhWTPr4HYavORg6HWxvvP6WeGClJSPgA=; fh=DTaH2EVASKNnUTo+khl4jHZ5aV5048FEathUcD0bxLo=; b=bIA6F9xZRbs2y25KKFDDDArnjc2gXhNbMzr0/DPsE+Mt4ipxm3hc63OwNnC9Enf9hM 4tN8gFGgjuairx9S6kzX+LY4K54N/I+JriKIgH+qEaOIjdUlU5WlqVvdQVqWj1h/6ziS KzM3qzXZTWBqWCi7K0yIx+sLHVzU2BWax8Ow/RbYKafSDyBAPowoy8sldAoG6+cPgSgY /tBp8h7uw6/YpL10ukcpstJwvaLalIXGp03vJ9FW7HkqSh4wIBA72Rq3RMg/gRxyZitl gBHKyLzgfY0wWtSI90YXxFXx7wZCsO3AghAMiNoaO+SXY4A6HfywbCGg4omg1eVyAJZw mgPQ==; 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-151789-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-151789-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [147.75.48.161]) by mx.google.com with ESMTPS id i7-20020a170902c94700b001e7b7c93013si3483382pla.627.2024.04.19.09.57.50 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 19 Apr 2024 09:57:51 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-151789-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) client-ip=147.75.48.161; 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-151789-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-151789-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 sy.mirrors.kernel.org (Postfix) with ESMTPS id D43D2B21214 for ; Fri, 19 Apr 2024 16:56:20 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 3701E13C813; Fri, 19 Apr 2024 16:55:31 +0000 (UTC) Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 422D1139CE8; Fri, 19 Apr 2024 16:55:29 +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=1713545730; cv=none; b=TKHdxmjDht7PyEpUZ0kXfZt9QcjOeP+wNUEMpy9mE7UalkrqEkAkObLNbmYbUJTjsXpp6Ycn8x+pCoxtgODQMJDnbw9DxSPNfAq4ptjcxE5nBEQ0RoaQlVREbVRKNdED2p2WtM38Ug0jRijw2+fYWQeJAXLG33iTIVCybcgZkaQ= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713545730; c=relaxed/simple; bh=7FEGpDG9haiMZCIZH2smWlWn8fRlvHbcsm22cfLb438=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=u/GWV4E9ghiMp6AbcJSzrEJ2rxlaAiRI8ht1WmT1DIbi4aWN1pRbT/YIa7ayQb1HsNT8TGW91c3tUMcePF6mvPvtwJF48K1JN5s7fZJ8lqvpCHhhLMHwClhA4GbUW4zVjSbsFustwmljMnVDL73hSqVP8zR3I9ArkgsKF80tzCA= 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 321911576; Fri, 19 Apr 2024 09:55:57 -0700 (PDT) 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 1D2393F792; Fri, 19 Apr 2024 09:55:23 -0700 (PDT) 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 v4 5/7] iommu/dma: Make limit checks self-contained Date: Fri, 19 Apr 2024 17:54:44 +0100 Message-Id: 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 Tested-by: Hanjun Guo 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 a3039005b696..f542eabaefa4 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 */ @@ -1760,7 +1757,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