blob: f225d8d4853d82102d060301efa2388c80685695 [file] [log] [blame]
From 8d06d6d4f20dccfe5cb660274360281cdf0ab1e4 Mon Sep 17 00:00:00 2001
From: Huang Ying <>
Date: Thu, 5 Mar 2020 22:28:29 -0800
Subject: [PATCH] mm: fix possible PMD dirty bit lost in
commit 8a8683ad9ba48b4b52a57f013513d1635c1ca5c4 upstream.
In set_pmd_migration_entry(), pmdp_invalidate() is used to change PMD
atomically. But the PMD is read before that with an ordinary memory
reading. If the THP (transparent huge page) is written between the PMD
reading and pmdp_invalidate(), the PMD dirty bit may be lost, and cause
data corruption. The race window is quite small, but still possible in
theory, so need to be fixed.
The race is fixed via using the return value of pmdp_invalidate() to get
the original content of PMD, which is a read/modify/write atomic
operation. So no THP writing can occur in between.
The race has been introduced when the THP migration support is added in
the commit 616b8371539a ("mm: thp: enable thp migration in generic path").
But this fix depends on the commit d52605d7cb30 ("mm: do not lose dirty
and accessed bits in pmdp_invalidate()"). So it's easy to be backported
after v4.16. But the race window is really small, so it may be fine not
to backport the fix at all.
Signed-off-by: Andrew Morton <>
Signed-off-by: "Huang, Ying" <>
Reviewed-by: Zi Yan <>
Reviewed-by: William Kucharski <>
Acked-by: Kirill A. Shutemov <>
Cc: <>
Cc: Vlastimil Babka <>
Cc: Michal Hocko <>
Cc: Andrea Arcangeli <>
Signed-off-by: Linus Torvalds <>
Signed-off-by: Paul Gortmaker <>
diff --git a/mm/huge_memory.c b/mm/huge_memory.c
index 6597095d54c0..84ef8add7bc9 100644
--- a/mm/huge_memory.c
+++ b/mm/huge_memory.c
@@ -2955,8 +2955,7 @@ void set_pmd_migration_entry(struct page_vma_mapped_walk *pvmw,
flush_cache_range(vma, address, address + HPAGE_PMD_SIZE);
- pmdval = *pvmw->pmd;
- pmdp_invalidate(vma, address, pvmw->pmd);
+ pmdval = pmdp_invalidate(vma, address, pvmw->pmd);
if (pmd_dirty(pmdval))
entry = make_migration_entry(page, pmd_write(pmdval));