From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 881B6C8E6; Mon, 22 Sep 2025 19:32:26 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758569546; cv=none; b=IZKo8MqCDbPJoZpb2cjbC7T3XGQn2Cp3iWRk/2Nsh8T/QFCnFQpxf7DLaLIiw39/c0302cgXiGoZdGYVpOh+qcUD+GmxiSemb6ezMnOxUHcWio4J21tO+k9sMcQI8vgcByPuXAQhlHvQBMuyzxYrCFeuShlMqxAqMkKYALHyuy4= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758569546; c=relaxed/simple; bh=ev6BSBGf0virjsvm1ZGrkCmvlpN7UgwTOKEatiSzjAE=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=b5pAItZEoA/OXVGdqyRcUjoVeb/PXZ9aaudsCXv1xdIT5NO3uPyoWl3C1iXuF43kX9AXOPJal6vok3aaScJWg+GaKXYoly/ZFAbjQ6NTmNG396xvEjMl3djbNFdsDrlsaLJtYwnt9MG5vC9/O7W0eSlbGB995Q/mWOp7G+K8phI= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.b=FUqMTLiI; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.b="FUqMTLiI" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 20130C4CEF0; Mon, 22 Sep 2025 19:32:25 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1758569546; bh=ev6BSBGf0virjsvm1ZGrkCmvlpN7UgwTOKEatiSzjAE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=FUqMTLiIvhgRHS7Ftj4Fgk8jNfQXRvb9MhUxkomUI2zn/7A417YEwZO3ljYKQ90rl +8ms2Fmxk59aPBdpUNBuHlWzw70wdXIW2r6LgCcSVZ6LHDpcqGB8CFmU8rrMXRnmEJ TXIXSnuNF4npnct/WMxj7W2whdzAHzBoxWglFYig= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, Eugene Koira , Nicolas Saenz Julienne , David Woodhouse , Lu Baolu , Joerg Roedel Subject: [PATCH 6.1 27/61] iommu/vt-d: Fix __domain_mapping()s usage of switch_to_super_page() Date: Mon, 22 Sep 2025 21:29:20 +0200 Message-ID: <20250922192404.299527245@linuxfoundation.org> X-Mailer: git-send-email 2.51.0 In-Reply-To: <20250922192403.524848428@linuxfoundation.org> References: <20250922192403.524848428@linuxfoundation.org> User-Agent: quilt/0.68 X-stable: review X-Patchwork-Hint: ignore Precedence: bulk X-Mailing-List: stable@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 6.1-stable review patch. If anyone has any objections, please let me know. ------------------ From: Eugene Koira commit dce043c07ca1ac19cfbe2844a6dc71e35c322353 upstream. switch_to_super_page() assumes the memory range it's working on is aligned to the target large page level. Unfortunately, __domain_mapping() doesn't take this into account when using it, and will pass unaligned ranges ultimately freeing a PTE range larger than expected. Take for example a mapping with the following iov_pfn range [0x3fe400, 0x4c0600), which should be backed by the following mappings: iov_pfn [0x3fe400, 0x3fffff] covered by 2MiB pages iov_pfn [0x400000, 0x4bffff] covered by 1GiB pages iov_pfn [0x4c0000, 0x4c05ff] covered by 2MiB pages Under this circumstance, __domain_mapping() will pass [0x400000, 0x4c05ff] to switch_to_super_page() at a 1 GiB granularity, which will in turn free PTEs all the way to iov_pfn 0x4fffff. Mitigate this by rounding down the iov_pfn range passed to switch_to_super_page() in __domain_mapping() to the target large page level. Additionally add range alignment checks to switch_to_super_page. Fixes: 9906b9352a35 ("iommu/vt-d: Avoid duplicate removing in __domain_mapping()") Signed-off-by: Eugene Koira Cc: stable@vger.kernel.org Reviewed-by: Nicolas Saenz Julienne Reviewed-by: David Woodhouse Link: https://lore.kernel.org/r/20250826143816.38686-1-eugkoira@amazon.com Signed-off-by: Lu Baolu Signed-off-by: Joerg Roedel Signed-off-by: Greg Kroah-Hartman --- drivers/iommu/intel/iommu.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) --- a/drivers/iommu/intel/iommu.c +++ b/drivers/iommu/intel/iommu.c @@ -2205,6 +2205,10 @@ static void switch_to_super_page(struct struct dma_pte *pte = NULL; unsigned long i; + if (WARN_ON(!IS_ALIGNED(start_pfn, lvl_pages) || + !IS_ALIGNED(end_pfn + 1, lvl_pages))) + return; + while (start_pfn <= end_pfn) { if (!pte) pte = pfn_to_dma_pte(domain, start_pfn, &level); @@ -2272,7 +2276,8 @@ __domain_mapping(struct dmar_domain *dom unsigned long pages_to_remove; pteval |= DMA_PTE_LARGE_PAGE; - pages_to_remove = min_t(unsigned long, nr_pages, + pages_to_remove = min_t(unsigned long, + round_down(nr_pages, lvl_pages), nr_pte_to_next_page(pte) * lvl_pages); end_pfn = iov_pfn + pages_to_remove - 1; switch_to_super_page(domain, iov_pfn, end_pfn, largepage_lvl);