From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796393643; b=nWhl7PmIT+OWYYPTHH59Gtjiku85/pDOrvJpJOYXowUbI61qr8eOVRYce9gsXhHJP7Ky dp8eY2dGfW8qiPbFN4n/bmTQEV/v2IqNVzKJsxC7PbBcgUPGwFbicvxtq6+Hw/WxE/tu7 AeSK8VVWDSsVJO50e3rghrSzcYf6qqaPyrNJEQBIPSoSqhSzKuUvJ4JHQKlZ9S+qvpg+Q FqvlnCXwtt+fTEdfcmJ7p92w+kAuPqmWzt467QxPTELSJBLeooh+ryKBY8WQm1hH4nnXe WrxIUuDMn40QxCKyOqj+2tXeEShX+RMNNmrpByKjBbUAXHnGBCgjN5pjCZyLrdX3u7A== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796393643; h=Received:DKIM-Signature:Date:From:To:Cc:Subject:Message-ID: References:MIME-Version:Content-Type:Content-Disposition:In-Reply-To; bh=JzRcq2psAI9IXGSiEn+YV8ggnHBRFufHTi+Kx9DCtwY=; b=gU9heWj043Ws7JeUDE75R3xki6H/VWgsa5ARrgmdJtv/AMkrPVpXCTHUJqc8rx0bAMsF 45saGZ8jbVoldS+djq1J9vNrGjN78C/YwTZ4s1Tq5BRqWc0EHV71Q8U6vDfulfmpO+giK buyM/auvWEG1uVQOuogrX27/cqLb1hJTqFy/5Rd+gcQS2mPd25esW74QURegJH8t8brBo 5B+860ZClIByCk+SbdqBeWjLPus9IXfogXFtXxhSMhp49sl2P8M+g3+YKNvEa2+VkuvHi QpL8NhyUSDT3lBwqHnynHZTXYQSwCDrzUQA5QbGT+w95bCvnl0TJe8sgFVhRlOBpjoA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=aGQdqoMn; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D31CE606A322; Fri, 4 Dec 2026 14:14:03 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=aGQdqoMn Received: by smtp.kernel.org (Postfix) with ESMTPSA id 05AC31F000CE; Fri, 4 Dec 2026 14:13:57 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796393643; bh=JzRcq2psAI9IXGSiEn+YV8ggnHBRFufHTi+Kx9DCtwY=; h=Date:From:To:Cc:Subject:References:In-Reply-To; b=aGQdqoMnneN3OHBpUL8G14UryZdv8DQjniS+sqgfd5X0OYv0c6LyVlBLqujoE1AiD 36AOufr+RT0EyHuMNeGajD80DZXEnuan8zmIqaHWopCthIQHvv88X3tz6CGz3HTO3Z 4yfZSNB2B1ECHvsBU21Iyu+u5M5l82/zRSDLK8Ue65RsRWYoWMNERMOxEjlTeUJIAu miRs6iPsFDxE9gyirakpU45qup0X0Jr9ryBmKeiXmKBveTgp6hwhbLJJLeC+PquGTN K8v6blFGtNehAkb4/EodXhfzdjsr3gxLkbq6sL6hKxMY+lN0z6wZc/py/YokyZXR02 pSBf7brdONMjQ== Date: Fri, 4 Dec 2026 14:13:53 +0000 From: Will Deacon To: Vikash Garodia Cc: "Joerg Roedel (AMD)" , Vasant Hegde , Suravee Suthikulpanit , Robin Murphy , David Woodhouse , Lu Baolu , Jean-Philippe Brucker , Rob Herring , Saravana Kannan , Tomasz Nowicki , Eric Auger , Bryan O'Donoghue , Joerg Roedel , Srinath Mannam , Oza Pawandeep , Lorenzo Pieralisi , Bjorn Helgaas , Alex Williamson , iommu@lists.linux.dev, linux-kernel@vger.kernel.org, virtualization@lists.linux.dev, devicetree@vger.kernel.org, kvm@vger.kernel.org, linux-arm-msm@vger.kernel.org, linux-media@vger.kernel.org, Sashiko , stable@vger.kernel.org, Krzysztof Kozlowski , Dmitry Baryshkov , Vishnu Reddy Subject: Re: [PATCH v8 1/4] iommu/dma: Keep reserved regions below the IOVA rbtree anchor Message-ID: References: <20261120-add_iommu_ranges-v8-0-d90486973b73@oss.qualcomm.com> <20261120-add_iommu_ranges-v8-1-d90486973b73@oss.qualcomm.com> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20261120-add_iommu_ranges-v8-1-d90486973b73@oss.qualcomm.com> On Fri, Nov 20, 2026 at 04:05:13PM +0530, Vikash Garodia wrote: > The IOVA allocator tracks address ranges by page frame number (PFN). > The highest node in its rbtree is a fixed anchor at PFN ~0UL, which is > embedded in the DMA cookie. dma-iommu converts reserved regions to PFNs > with iova_pfn(), which returns unsigned long. On x86-32 with PAE, > addresses are 64-bit but unsigned long is 32-bit, so the upper bits of > the PFN are lost. > A reserved region that ends at the top of the address space then gets > the anchor's PFN. reserve_iova() merges it into the anchor and lowers > the anchor's pfn_lo. At teardown, free_iova_mem() no longer recognises > the anchor and frees it to iova_cache, which corrupts the slab. PCI host > bridge windows above 16TiB and the reservation after the last PCI DMA > window hit the same problem. > Compute the PFNs in u64. Skip ranges that start above the PFN space and > clamp the end one PFN below the anchor. The allocator never hands out > those IOVAs, so nothing is lost. On 64-bit, PFNs never reach ~0UL, so > nothing changes there. Do you have a crash log you could include here? There is some driver code doing similar things, does that need fixing too? (see e.g. ipu7_get_fw_code_region()). > Fixes: fade1ec055dc ("iommu/dma: Avoid PCI host bridge windows") > Fixes: 7c1b058c8b5a ("iommu/dma: Handle IOMMU API reserved regions") > Fixes: aadad097cd46 ("iommu/dma: Reserve IOVA for PCIe inaccessible DMA address") > Reported-by: Sashiko > Closes: https://lore.kernel.org/all/sashiko-outbox-195658@kernel.org/ > Cc: stable@vger.kernel.org > Reviewed-by: Krzysztof Kozlowski > Reviewed-by: Dmitry Baryshkov > Co-developed-by: Vishnu Reddy > Signed-off-by: Vishnu Reddy > Signed-off-by: Vikash Garodia > --- > drivers/iommu/dma-iommu.c | 32 ++++++++++++++++++++------------ > 1 file changed, 20 insertions(+), 12 deletions(-) > > diff --git a/drivers/iommu/dma-iommu.c b/drivers/iommu/dma-iommu.c > index b66e83cfbc5f195a46d286d376068fcc13148611..2ee30df470730fd03b5dc46b46e3eaba2df130d6 100644 > --- a/drivers/iommu/dma-iommu.c > +++ b/drivers/iommu/dma-iommu.c > @@ -505,21 +505,34 @@ static int iommu_dma_ranges_sort(void *priv, const struct list_head *a, > return res_a->res->start > res_b->res->start; > } > > +/* > + * IOVA PFNs are unsigned long, and ~0UL is the rbtree anchor. On 32-bit > + * with a 64-bit dma_addr_t, iova_pfn() would truncate the range. Compute > + * the PFNs in u64 instead, skip a range that starts beyond the PFN space > + * and clamp its end below the anchor. Such IOVAs cannot be allocated. > + */ > +static void iova_reserve_range(struct iova_domain *iovad, u64 start, u64 end) > +{ > + u64 lo = start >> iova_shift(iovad); > + u64 hi = end >> iova_shift(iovad); > + > + if (lo < ULONG_MAX) > + reserve_iova(iovad, lo, min(hi, ULONG_MAX - 1)); > +} I appreciate that you're trying to fix an existing issue here, but the terminology used in the IOVA code is really confusing to me. iova_pfn() shifts a virtual address (the "va" in "iova" to get a pfn, which is completely different to how we think about this on the CPU side where the pfn is the shifted _physical_ address (e.g. compare with virt_to_pfn()). It would be good to remove iova_pfn() altogether, if we can. Is there a good reason why IOVAs aren't dma_addr_t instead of unsigned long? I suppose it doesn't help that iommu_map() takes an unsigned long, so poor old dma-iommu.c gets caught in the middle a bit here. Anyway, I think what you have here is fine, I just think that we could probably do more. Will