Received: by 2002:ab2:6816:0:b0:1f9:5764:f03e with SMTP id t22csp2659732lqo; Mon, 20 May 2024 12:15:22 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCWf+AvdZQnhuB1mIkuhifCJAsnUrUNaK8GJ+APZckQuiAQMlqw81vgfomyw14hxtbCNe7f1mVslQzo2OZOUYt0OrOGApEXKQ1fwYhtIVA== X-Google-Smtp-Source: AGHT+IGrAb19fd9O2GpzRQMCUIsgG8AY3WyTchHjh4Kl3djztl6/poAzr3FiTYj09bosjLOKOEgd X-Received: by 2002:a19:6406:0:b0:521:6c38:6949 with SMTP id 2adb3069b0e04-522100749b0mr16878073e87.45.1716232521889; Mon, 20 May 2024 12:15:21 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1716232521; cv=pass; d=google.com; s=arc-20160816; b=H/CoynaQXuNACV/me4xKCHyz1rm6w0dqSc0ufFuZBuWAwcVYDOZijfdc0afzq3MpGq GIAD5gySon9Ld8rozes99G3L4TBbaf6SEnUS1r3WuMVoPUcENbRiOoBxvNT7epHXNPqq XM6hgtggFb9pY0mrecadb2V9k+tXcG0AlHMP/iOD3B/2J1MsMaRgnHOJ11jKpFor475l f4LNQZgvIYLpFxsQOOgqj4P0SfkidSybzFeb9vAcUBXYL5dJPxqBvFj5Z03Q3O609LOu Tmzo9qMMjGFIURGIJRodPMMd9bPgrlRhUp3xNSAgPjDsOyvdm13XmBJeuBmpabkzMot/ 7DUQ== 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:message-id:date:subject:cc:to :from; bh=4MsAYneKMm7/wk16trYuMyYnGFYUT1AQZ5MlARi9Wss=; fh=2m8QWeY+P3ZQU7D5LZYAaaJB2BK/0ELliGGo0b+8YZE=; b=Z/HuEJafFj61Vj2+ClQrhQ9MuNcB2Mowu6HdbzKFw/Cahzvs1kNvxrIj+B245E34dS 9TQL4lHxWT0Tpzw2V0xcFoPM9+lmcY4L1boZ8Q2cm78fDp7/YNwhv5Mh0Fo1Hb6r6HLb ZPzvKjLPQVbIoT0EBYDCk29exizJX9PZ6FvNGRaHkE1TZrNTGmV0xXfdn0fX3aQg6iBR FL9ujONTcGBsKBFsONCLzTWwMIg5a/7QUdUrllOR8BgVyDHQwYAU+Ie+m2n6PPbNCn+M MBguRlY7wqARWu1goLr8bUVvwNr/yRMVbdD4iJOG9VEhxWjLLA3BU8FJyCrgT1gBnxbD 4edQ==; 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-184102-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-184102-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id a640c23a62f3a-a5a17ba3cb6si1457979466b.619.2024.05.20.12.15.21 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 20 May 2024 12:15:21 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-184102-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; 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-184102-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-184102-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 9691D1F21E04 for ; Mon, 20 May 2024 19:15:21 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id BB55C13956F; Mon, 20 May 2024 19:14:59 +0000 (UTC) Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 227CE13956D for ; Mon, 20 May 2024 19:14:55 +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=1716232499; cv=none; b=uwlcbSjOJ+ipEJCEH9r7j1+Z+qaxiZgZkBve3tZEdwabO1waXlHGyTUvGCqtijAr0tFM92iM6w+QO3bb6BBh75SbeyqqD5C0/6e0YDuoZFjuqLGOJz5ifPEEB94Ar4yLd8SG3bjwk+uavcYsnykZJ0jplX38rljHVZoSBQM+lWQ= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716232499; c=relaxed/simple; bh=aKWSKFKGF6zwX7P7XN89k4m4AaRFffhHWHjbFPxtRpk=; h=From:To:Cc:Subject:Date:Message-Id:MIME-Version; b=BlxeLt1+ATh6Ub07AB3msM8Z5r/aqyLNvM7jKZIaSL6zw4psH9PGRWFa+KFL2bqOMaISuYO0NuGvdl9Dj5LxkwIZyY88sKTqR1LvPMk1ncNDlRhtdRfak4dxRAG/oQcmKC3Gi/R4LMD5MAEmbjuRT8yTBdt6pws672hzv1Ey9/c= 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 361BEDA7; Mon, 20 May 2024 12:15:19 -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 57CC43F641; Mon, 20 May 2024 12:14:54 -0700 (PDT) From: Robin Murphy To: joro@8bytes.org Cc: iommu@lists.linux.dev, will@kernel.org, linux-kernel@vger.kernel.org, Jon Hunter , Jerry Snitselaar Subject: [PATCH] iommu/dma: Fix domain init Date: Mon, 20 May 2024 20:14:44 +0100 Message-Id: <721fa6baebb0924aa40db0b8fb86bcb4538434af.1716232484.git.robin.murphy@arm.com> X-Mailer: git-send-email 2.39.2.101.g768bb238c484.dirty Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Despite carefully rewording the kerneldoc to describe the new direct interaction with dma_range_map, it seems I managed to confuse myself in removing the redundant force_aperture check and ended up making the code not do that at all. This led to dma_range_maps inadvertently being able to set iovad->start_pfn = 0, and all the nonsensical chaos which ensues from there. Restore the correct behaviour of constraining base_pfn to the domain aperture regardless of dma_range_map, and not trying to apply dma_range_map constraints to the basic IOVA domain since they will be properly handled with reserved regions later. Reported-by: Jon Hunter Reported-by: Jerry Snitselaar Fixes: e28a114243d1 ("iommu/dma: Make limit checks self-contained") Signed-off-by: Robin Murphy --- drivers/iommu/dma-iommu.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/drivers/iommu/dma-iommu.c b/drivers/iommu/dma-iommu.c index 89a53c2f2cf9..1ec89ca75c59 100644 --- a/drivers/iommu/dma-iommu.c +++ b/drivers/iommu/dma-iommu.c @@ -685,15 +685,15 @@ static int iommu_dma_init_domain(struct iommu_domain *domain, struct device *dev /* Check the domain allows at least some access to the device... */ if (map) { - dma_addr_t base = dma_range_map_min(map); - if (base > domain->geometry.aperture_end || + if (dma_range_map_min(map) > domain->geometry.aperture_end || 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(base, domain->geometry.aperture_start) >> order; } + /* ...then finally give it a kicking to make sure it fits */ + base_pfn = max_t(unsigned long, base_pfn, + domain->geometry.aperture_start >> order); /* start_pfn is always nonzero for an already-initialised domain */ mutex_lock(&cookie->mutex); -- 2.39.2.101.g768bb238c484.dirty