[PATCH v3 2/6] mm: gup: clarify a reason to check pte/pmd in gup_fast_pte_range()
From: Yeoreum Yun
Date: Mon Nov 09 2026 - 07:42:08 EST
Clarify a reason to check pte/pmd in gup_fast_pte_range().
Signed-off-by: Yeoreum Yun <yeoreum.yun@xxxxxxx>
---
mm/gup.c | 8 ++++++++
1 file changed, 8 insertions(+)
diff --git a/mm/gup.c b/mm/gup.c
index 7d43ce717b246..7f9348c633d58 100644
--- a/mm/gup.c
+++ b/mm/gup.c
@@ -2881,6 +2881,14 @@ static unsigned long gup_fast_pte_range(pmd_t pmd, pmd_t *pmdp,
if (!folio)
goto pte_unmap;
+ /*
+ * Recheck the PTE after grabbing the folio to detect concurrent
+ * unmapping or protection changes such as by KSM.
+ *
+ * Also, recheck the PMD: THP collpase can detach the PTE table
+ * while its entries remain unchaged. Checking the PTE alone
+ * would not dtect that we are walking a detached page table.
+ */
if (unlikely(pmd_val(pmd) != pmd_val(pmdp_get_lockless(pmdp))) ||
unlikely(pte_val(pte) != pte_val(ptep_get_lockless(ptep)))) {
gup_put_folio(folio, 1, flags);
--
2.43.0