Received: from smtp.kernel.org (aws-us-west-2-korg-mail-alma10-1.taild15c8.ts.net [100.103.45.18]) (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 812D547ECF2; Thu, 20 Aug 2026 17:30:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=100.103.45.18 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1787247019; cv=none; b=HX9IMmAHB+Eu73/AfLS8vRn+dmtXoqIcaLEAObd06r6MnzJnupoMOG/Itw6rSP1hp/TMs9QLTarjTiM+jCsWzoJBjzu2IcsqJ9D0a1e4rSWhWPjAvVIwLeWGu6Kx9xQUo6fwDlq0arpjUoTMJ7qSZ972viHotukeSSl4dkmAcgA= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1787247019; c=relaxed/simple; bh=uFuZy0i+65i/URcpYdydBdhcju13KTX/504k2ByydBw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=cv6f4xuA+g1QdO7p2IhgwU4ISuoE9/6NoosWv9j6Xr4qZeZp5qicLHX/+DRxfZe5Dpw4NhoZF8TybY08GzGcfc7FeGNPANl6QBSXAd1oPheMWLR0V6/mvdjt34wRYGC/wAn5F0jkDKiqEgnQW/CLkdShTHR44XrM9lq23E7z8Ro= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.b=h1rphhIq; arc=none smtp.client-ip=100.103.45.18 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.b="h1rphhIq" Received: by smtp.kernel.org (Postfix) with ESMTPSA id B356C1F000E9; Thu, 20 Aug 2026 17:30:17 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1787247018; bh=QLvGMfUpiW6KkSPKC8d9L3cWY1FnSEtfi8nC9jbKTNA=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=h1rphhIqJ8NIcYnGBIoG2pBC33pbL6QMWzEbMnskzJgKVlyV9u736iO3XoYef6V3/ 4VkpNGzjwP8OEDOaDgf8Hi9wo7eexP8eDpmUfXSXsiv9olwxDTZYiOtDcp0RO6buuK pj9tmObpOl0xIkVleO9z3fxOY8mi9jZP2VZ6mQHM= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, Lance Yang , Lorenzo Stoakes , Nico Pache , Dev Jain , Baolin Wang , Wei Yang , David Hildenbrand , Liam Howlett , Ryan Roberts , Zi Yan , Andrew Morton , Sasha Levin Subject: [PATCH 6.12 120/220] mm/khugepaged: guard is_zero_pfn() calls with pte_present() Date: Thu, 20 Aug 2026 16:55:10 +0200 Message-ID: <20260820145227.082686941@linuxfoundation.org> X-Mailer: git-send-email 2.55.0 In-Reply-To: <20260820145223.480031205@linuxfoundation.org> References: <20260820145223.480031205@linuxfoundation.org> User-Agent: quilt/0.69 X-stable: review X-Patchwork-Hint: ignore Precedence: bulk X-Mailing-List: patches@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 6.12-stable review patch. If anyone has any objections, please let me know. ------------------ From: Lance Yang [ Upstream commit 074f027d15c10cb376b3ad88405b8e512fa5b3a8 ] A non-present entry, like a swap PTE, contains completely different data (swap type and offset). pte_pfn() doesn't know this, so if we feed it a non-present entry, it will spit out a junk PFN. What if that junk PFN happens to match the zeropage's PFN by sheer chance? While really unlikely, this would be really bad if it did. So, let's fix this potential bug by ensuring all calls to is_zero_pfn() in khugepaged.c are properly guarded by a pte_present() check. Link: https://lkml.kernel.org/r/20251020151111.53561-1-lance.yang@linux.dev Signed-off-by: Lance Yang Suggested-by: Lorenzo Stoakes Reviewed-by: Nico Pache Reviewed-by: Dev Jain Reviewed-by: Baolin Wang Reviewed-by: Wei Yang Acked-by: David Hildenbrand Reviewed-by: Lorenzo Stoakes Cc: Liam Howlett Cc: Ryan Roberts Cc: Wei Yang Cc: Zi Yan Signed-off-by: Andrew Morton Stable-dep-of: 3c58f641e813 ("userfaultfd: prevent registration of special VMAs") Signed-off-by: Sasha Levin Signed-off-by: Greg Kroah-Hartman --- mm/khugepaged.c | 35 +++++++++++++++++++++-------------- 1 file changed, 21 insertions(+), 14 deletions(-) --- a/mm/khugepaged.c +++ b/mm/khugepaged.c @@ -346,6 +346,13 @@ struct attribute_group khugepaged_attr_g }; #endif /* CONFIG_SYSFS */ +static bool pte_none_or_zero(pte_t pte) +{ + if (pte_none(pte)) + return true; + return pte_present(pte) && is_zero_pfn(pte_pfn(pte)); +} + int hugepage_madvise(struct vm_area_struct *vma, unsigned long *vm_flags, int advice) { @@ -529,6 +536,7 @@ static void release_pte_pages(pte_t *pte if (pte_none(pteval)) continue; + VM_WARN_ON_ONCE(!pte_present(pteval)); pfn = pte_pfn(pteval); if (is_zero_pfn(pfn)) continue; @@ -572,8 +580,7 @@ static int __collapse_huge_page_isolate( for (_pte = pte; _pte < pte + HPAGE_PMD_NR; _pte++, address += PAGE_SIZE) { pte_t pteval = ptep_get(_pte); - if (pte_none(pteval) || (pte_present(pteval) && - is_zero_pfn(pte_pfn(pteval)))) { + if (pte_none_or_zero(pteval)) { ++none_or_zero; if (!userfaultfd_armed(vma) && (!cc->is_khugepaged || @@ -716,17 +723,17 @@ static void __collapse_huge_page_copy_su for (_pte = pte; _pte < pte + HPAGE_PMD_NR; _pte++, address += PAGE_SIZE) { pteval = ptep_get(_pte); - if (pte_none(pteval) || is_zero_pfn(pte_pfn(pteval))) { + if (pte_none_or_zero(pteval)) { add_mm_counter(vma->vm_mm, MM_ANONPAGES, 1); - if (is_zero_pfn(pte_pfn(pteval))) { - /* - * ptl mostly unnecessary. - */ - spin_lock(ptl); - ptep_clear(vma->vm_mm, address, _pte); - spin_unlock(ptl); - ksm_might_unmap_zero_page(vma->vm_mm, pteval); - } + if (pte_none(pteval)) + continue; + /* + * ptl mostly unnecessary. + */ + spin_lock(ptl); + ptep_clear(vma->vm_mm, address, _pte); + spin_unlock(ptl); + ksm_might_unmap_zero_page(vma->vm_mm, pteval); } else { struct page *src_page = pte_page(pteval); @@ -812,7 +819,7 @@ static int __collapse_huge_page_copy(pte unsigned long src_addr = address + i * PAGE_SIZE; struct page *src_page; - if (pte_none(pteval) || is_zero_pfn(pte_pfn(pteval))) { + if (pte_none_or_zero(pteval)) { clear_user_highpage(page, src_addr); continue; } @@ -1305,7 +1312,7 @@ static int hpage_collapse_scan_pmd(struc goto out_unmap; } } - if (pte_none(pteval) || is_zero_pfn(pte_pfn(pteval))) { + if (pte_none_or_zero(pteval)) { ++none_or_zero; if (!userfaultfd_armed(vma) && (!cc->is_khugepaged ||