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=1794409613; b=Ru9ZQXElQrqJCAMrP2YWPYNGzEi3nqsbbnXjaeo94+z2LcKhiBlyRjaV0vwag3tDDJrI lSpwRiLcf68NKaR4amVdSSwhKK7AKd1AvajiyTqqrxRVs0op+7LM/1rfeZ9yGIH16cnzU cy+e/vuNRsionB0xZJiY/ERKMK7SyMtjOsG9qpLKkG4/zSjBBtVENq8swhVdCWjgq+FyI qEkfzovD/f11TZ4sUKo2worcRdmrYRZJ+X+6itENFz7rK9hHqjFYZqoMMzgWl5Fa61UhU fvnGiGxVTTZmnFglb4HNhreppTjhaWUFWmVCSny8NSgGQ/kEpHUaMQZfqwOBJ1SFucQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1794409613; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:MIME-Version:Content-Transfer-Encoding; bh=rYmqzzJLWF1rgvYuvlxPKTNvDSmz0uCZMeJM+Cpf1Hk=; b=d1Pk4504vPu17n0yS51z1kwGSIzzXhX1ZYDwhvT1ZQg/fZbx0ocgkdnz5zAJmGAA28zc 3cfJzb2xsd0FvClef7L5GOgE9tRa2p5oH+qfEL6cc4jArEKSEkhUhdXbtQbFCogJQEyle K+W+0ozaavyrUAgGGd0KT/Hf5VdOorSEfa4gdDhjOzUBLzFCP9VwSulqlMTSbrJZNGkNu R8mtgZ/I+/MhjIU0S2cMpv9+kf4FE9sAnSI9G7Rh/djgYmaXPRW3gmGjo+Rf/TM7+WAec Xsp3sCWClgl3j8ai4ZNmHuLgpHIqne1hPqjdI+oGPWfOjPjaGjDBB9I/60m+GkZLW1A== 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=nhHot1fI; 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 B7DC0606A891 for ; Wed, 11 Nov 2026 15:06:53 +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=nhHot1fI Received: by smtp.kernel.org (Postfix) with ESMTPSA id 5D17C1F00A05; Wed, 11 Nov 2026 15:06:52 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1794409613; bh=rYmqzzJLWF1rgvYuvlxPKTNvDSmz0uCZMeJM+Cpf1Hk=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=nhHot1fI9POETS5Gt1U2S7IfdmrIJvlEun0mNy4K+JJMTVgy6VFYq1/R1TDUdtPSw F4ZSxwW9H7szs5Ah6hOKQJEpQnqqvsbeKWvPKhHy5053+ixJIbY7mR+bM4tl7SsOpN ggRDNBfQcZkgLzQbm/YzbCUPn9rnrTnqAdroESuzGrNghNjQeUda/qkF7gdRD+WC/q Wgm+WKySnwrJWbQLWRS0ooSn+Sk1dfCM46AEsNH7apvxqCtl370v+ul7ye6k7LYUaD GQ545rbGREqDNOM/piIi+bbIcxW5aC9UimBjifSoBjaTjyDQK44qYdhQeymSS73km8 5HYCC3pfAQ7Qg== From: Sasha Levin To: stable@vger.kernel.org Cc: Kyle Zeng , Andrew Morton , "David Hildenbrand (Arm)" , Lorenzo Stoakes , Rik van Riel , "Liam R. Howlett" , Vlastimil Babka , Harry Yoo , Jann Horn , Lance Yang , Sasha Levin Subject: [PATCH 6.18.y] mm/page_vma_mapped: continue past nonmatching huge PMDs Date: Wed, 11 Nov 2026 10:06:51 -0500 Message-ID: <20261111150635.2570625-1-sashal@kernel.org> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20261107051433-failed-6.18-1a2993227e8a@kernel.org> References: <20261107051433-failed-6.18-1a2993227e8a@kernel.org> X-Mailing-List: stable@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit From: Kyle Zeng [ Upstream commit 1a2993227e8a27fc4cdea305bbdae82d290bc885 ] A PTE-mapped large folio can straddle PMD boundaries after an unaligned mremap(). If the first part is discarded and replaced with an unrelated huge PMD, page_vma_mapped_walk() stops instead of looking for the rest of the folio in subsequent PMDs. This also affects migration entries. A walk that stops at an unrelated huge PMD can leave migration PTEs behind when migration or an anonymous folio split restores the mappings. Those entries do not hold mapping references and can outlive the source folio. This issue can lead to memory corruption and I have a reproducer for it. Skip the current PMD rather than ending the walk on a flag or PFN mismatch. Do the same if the PMD became nonpresent before the locked recheck. Release the PMD lock, clear the per-PMD state and advance with step_forward(), so the existing range limit controls termination. This covers present, migration and device-private huge PMDs while preserving the matching-PMD and split-to-PTE paths. Link: https://lore.kernel.org/20261016011549.61146-1-kylebot@openai.com Fixes: ace71a19cec5 ("mm: introduce page_vma_mapped_walk()") Signed-off-by: Kyle Zeng Signed-off-by: Andrew Morton Acked-by: David Hildenbrand (Arm) Assisted-by: LLM Cc: Lorenzo Stoakes Cc: Rik van Riel Cc: Liam R. Howlett Cc: Vlastimil Babka Cc: Harry Yoo Cc: Jann Horn Cc: Lance Yang Cc: [ sashal: Adapted PMD handling to older migration-entry APIs and inline locking without device-private huge-PMD support. ] Signed-off-by: Sasha Levin --- mm/page_vma_mapped.c | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/mm/page_vma_mapped.c b/mm/page_vma_mapped.c index 41cf3192a8f62..dfbbbf5e3f53d 100644 --- a/mm/page_vma_mapped.c +++ b/mm/page_vma_mapped.c @@ -260,18 +260,18 @@ bool page_vma_mapped_walk(struct page_vma_mapped_walk *pvmw) if (!thp_migration_supported() || !(pvmw->flags & PVMW_MIGRATION)) - return not_found(pvmw); + goto next_pmd; entry = pmd_to_swp_entry(pmde); if (!is_migration_entry(entry) || !check_pmd(swp_offset_pfn(entry), pvmw)) - return not_found(pvmw); + goto next_pmd; return true; } if (likely(pmd_trans_huge(pmde))) { if (pvmw->flags & PVMW_MIGRATION) - return not_found(pvmw); + goto next_pmd; if (!check_pmd(pmd_pfn(pmde), pvmw)) - return not_found(pvmw); + goto next_pmd; return true; } /* THP pmd was split under us: handle on pte level */ @@ -291,8 +291,7 @@ bool page_vma_mapped_walk(struct page_vma_mapped_walk *pvmw) spin_unlock(ptl); } - step_forward(pvmw, PMD_SIZE); - continue; + goto next_pmd; } if (!map_pte(pvmw, &pmde, &ptl)) { if (!pvmw->pte) @@ -331,6 +330,17 @@ bool page_vma_mapped_walk(struct page_vma_mapped_walk *pvmw) pvmw->ptl = ptl; } goto this_pte; +next_pmd: + /* + * A large folio can be PTE-mapped across PMDs, so a + * nonmatching PMD does not end the walk. + */ + if (pvmw->ptl) { + spin_unlock(pvmw->ptl); + pvmw->ptl = NULL; + } + pvmw->pmd = NULL; + step_forward(pvmw, PMD_SIZE); } while (pvmw->address < end); return false; -- 2.53.0