aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAaron Lu <aaron.lu@intel.com>2016-11-29 00:27:31 -0500
committerLinus Torvalds <torvalds@linux-foundation.org>2016-11-29 11:20:24 -0500
commita2ce2666aa3509ac31fac0f540a3502372b7b630 (patch)
tree10b193706622c09b1b23c540359de7d75557a5a7
parent88abd8249ee8bcebb98c90e890ea5e342db832af (diff)
mremap: move_ptes: check pte dirty after its removal
Linus found there still is a race in mremap after commit 5d1904204c99 ("mremap: fix race between mremap() and page cleanning"). As described by Linus: "the issue is that another thread might make the pte be dirty (in the hardware walker, so no locking of ours will make any difference) *after* we checked whether it was dirty, but *before* we removed it from the page tables" Fix it by moving the check after we removed it from the page table. Suggested-by: Linus Torvalds <torvalds@linux-foundation.org> Signed-off-by: Aaron Lu <aaron.lu@intel.com> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
-rw-r--r--mm/huge_memory.c4
-rw-r--r--mm/mremap.c12
2 files changed, 10 insertions, 6 deletions
diff --git a/mm/huge_memory.c b/mm/huge_memory.c
index eff3de359d50..d4a6e4001512 100644
--- a/mm/huge_memory.c
+++ b/mm/huge_memory.c
@@ -1456,9 +1456,9 @@ bool move_huge_pmd(struct vm_area_struct *vma, unsigned long old_addr,
1456 new_ptl = pmd_lockptr(mm, new_pmd); 1456 new_ptl = pmd_lockptr(mm, new_pmd);
1457 if (new_ptl != old_ptl) 1457 if (new_ptl != old_ptl)
1458 spin_lock_nested(new_ptl, SINGLE_DEPTH_NESTING); 1458 spin_lock_nested(new_ptl, SINGLE_DEPTH_NESTING);
1459 if (pmd_present(*old_pmd) && pmd_dirty(*old_pmd))
1460 force_flush = true;
1461 pmd = pmdp_huge_get_and_clear(mm, old_addr, old_pmd); 1459 pmd = pmdp_huge_get_and_clear(mm, old_addr, old_pmd);
1460 if (pmd_present(pmd) && pmd_dirty(pmd))
1461 force_flush = true;
1462 VM_BUG_ON(!pmd_none(*new_pmd)); 1462 VM_BUG_ON(!pmd_none(*new_pmd));
1463 1463
1464 if (pmd_move_must_withdraw(new_ptl, old_ptl) && 1464 if (pmd_move_must_withdraw(new_ptl, old_ptl) &&
diff --git a/mm/mremap.c b/mm/mremap.c
index 6ccecc03f56a..30d7d2482eea 100644
--- a/mm/mremap.c
+++ b/mm/mremap.c
@@ -149,14 +149,18 @@ static void move_ptes(struct vm_area_struct *vma, pmd_t *old_pmd,
149 if (pte_none(*old_pte)) 149 if (pte_none(*old_pte))
150 continue; 150 continue;
151 151
152 pte = ptep_get_and_clear(mm, old_addr, old_pte);
152 /* 153 /*
153 * We are remapping a dirty PTE, make sure to 154 * If we are remapping a dirty PTE, make sure
154 * flush TLB before we drop the PTL for the 155 * to flush TLB before we drop the PTL for the
155 * old PTE or we may race with page_mkclean(). 156 * old PTE or we may race with page_mkclean().
157 *
158 * This check has to be done after we removed the
159 * old PTE from page tables or another thread may
160 * dirty it after the check and before the removal.
156 */ 161 */
157 if (pte_present(*old_pte) && pte_dirty(*old_pte)) 162 if (pte_present(pte) && pte_dirty(pte))
158 force_flush = true; 163 force_flush = true;
159 pte = ptep_get_and_clear(mm, old_addr, old_pte);
160 pte = move_pte(pte, new_vma->vm_page_prot, old_addr, new_addr); 164 pte = move_pte(pte, new_vma->vm_page_prot, old_addr, new_addr);
161 pte = move_soft_dirty_pte(pte); 165 pte = move_soft_dirty_pte(pte);
162 set_pte_at(mm, new_addr, new_pte, pte); 166 set_pte_at(mm, new_addr, new_pte, pte);