Received: by 2002:a05:6500:1b8f:b0:1fa:5c73:8e2d with SMTP id df15csp744472lqb; Wed, 29 May 2024 09:15:39 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCWUMSyeKUTOLUof3ZXHcph3q9HFyN+G5lm6Z5h++526WOVWIJeywtKe0/IHkYnYgc1rFk4WBKaHhLtf/oXGq6yALXW14M5KWFvumAc6oA== X-Google-Smtp-Source: AGHT+IEZipRPYAWGNJL9RTQDybbvK+6IAu5G6hZaborkYS7lFm/ECtUhB7DBtJDWT3zdoxdVSBeE X-Received: by 2002:a05:6a00:4c12:b0:6f0:c79f:cd7e with SMTP id d2e1a72fcca58-6f8f1960325mr18350133b3a.0.1716999338645; Wed, 29 May 2024 09:15:38 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1716999338; cv=pass; d=google.com; s=arc-20160816; b=FgNPzeZl34ucXsRFcOrW7XMk6pHRmKKEefipu3PGc5pKvjQtHwVqNY3+rR4rP4bQ5N srRU7HA6k9bF0qvjvSivHXgBDQRCRKn1YuTNj8Zna4HrybA1iogS2Xire466yKxdcaUw sziLPqmGdmBPnpc+wZ3HlqKNrk0brllNUk/xyPB4XOs/oeyKqW4zudpG6GFqzEwOOz9l rvl4x5UeZge4Piz1v1yTSM+W0vZUN6MaAEO2GZhp12OwGBKihUWLdlS8mFQIF/+vm5r4 663YuDi8XwOqoq/VDZCyEwTTl4WNBkx4S2+k+KbwcoZgsg8/iepKPIJ+XA7msk/ehDl5 4SNQ== 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=3fjY7Wq8AdIJOl5nKI18ATBzfUXJ3v7CFxvO91IuMrM=; fh=2m8QWeY+P3ZQU7D5LZYAaaJB2BK/0ELliGGo0b+8YZE=; b=ac4y2JeUaVZXcyNvng3lifSDNEnT47p0fpoBgYF9Pt2attrsmqoXkwz3auQbXm2Pr0 MfUyxazl5AGi1fc8LxIGKlfx4YULqh8qf29AgBliJYSeLLFxcasOO7jA6Syb83Qfxx26 OfzEwuDGlhoGaNy22GQwPQvEUxaRZZ7Y3WMAKkjSZCya6TR21E34H9kcFnovGRW+hb1+ rhme4HK6TCMexRjCcc6SALZiL5WrXabHf1m1Rfsqv102UL0eRlFu4motgDb2/FiRoCK/ obKWfUD8EE5D3U3wVhqBxpGnCmgGaD6x1NuzTOrZwp9DCCd6/TcYIG55LfjDkh9Kso89 mhAQ==; 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-194391-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-194391-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 41be03b00d2f7-682288e66a6si10288513a12.439.2024.05.29.09.15.37 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 29 May 2024 09:15:38 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-194391-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-194391-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-194391-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 2B96BB21EB6 for ; Wed, 29 May 2024 15:54:30 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id E641F181D02; Wed, 29 May 2024 15:54:22 +0000 (UTC) Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 1FB93181315 for ; Wed, 29 May 2024 15:54:19 +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=1716998062; cv=none; b=dz9ZTqeX8QXtLg2FMyKsnTl1EAZmNCF0jK/mnuJEUNmQb/aOJZue4RHkWr9zI/nmeMw/u2PiwPlXyvVqqJjiMg7n50hb1qjGD3hG7t8Z9k/ReMExz8Omlu+mRhfacD34hOdeSgcDdzKO3SoGrlYknyi+dV10YK50dbFiYm8bCgk= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716998062; c=relaxed/simple; bh=c6dqKVeQyxgbzfBnYTmLHRwbpXQUgnveDT5DlgxVB50=; h=From:To:Cc:Subject:Date:Message-Id:MIME-Version; b=FNywXnuw8C3UO6KZfOrajP2loCmTMd7WyMuPt/saq3pJH9TYkTM+RDtKxQhI0RSoyxKLg94Cc13RYJmkYLMqJewZ/9AXBQubJH1/p9GnqkBesSNljZ3V05lir5TkSfZARFx5TD2KcHeQgCGTE/Kv2kA/pkMXmQbyamc9VWp+yHM= 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 AB0FA339; Wed, 29 May 2024 08:54:43 -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 99F1A3F792; Wed, 29 May 2024 08:54:18 -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 v2] iommu/dma: Fix domain init Date: Wed, 29 May 2024 16:54:09 +0100 Message-Id: <159193e80b6a7701c61b32d6119ac68989d457bd.1716997607.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: ad4750b07d34 ("iommu/dma: Make limit checks self-contained") Tested-by: Jerry Snitselaar Reviewed-by: Jerry Snitselaar Signed-off-by: Robin Murphy --- Since I've now got round to rebasing on -rc1, I figured I may as well send a v2 with updated tags to avoid any confusion. 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 f731e4b2a417..43520e7275cc 100644 --- a/drivers/iommu/dma-iommu.c +++ b/drivers/iommu/dma-iommu.c @@ -686,15 +686,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