[PATCH v4 2/6] mm: gup: clarify a reason to check pte/pmd in gup_fast_pte_range()
From: Yeoreum Yun
Date: Tue Nov 10 2026 - 17:55:48 EST
Clarify a reason to check pte/pmd in gup_fast_pte_range().
Reviewed-by: Rik van Riel <riel@xxxxxxxxxxx>
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 2b405cce2ca5..00dac03093a4 100644
--- a/mm/gup.c
+++ b/mm/gup.c
@@ -2885,6 +2885,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 collapse can detach the PTE table
+ * while its entries remain unchanged. Checking the PTE alone
+ * would not detect 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