Skip to content

Commit 541500a

Browse files
torvaldsgregkh
authored andcommitted
mremap: properly flush TLB before releasing the page
commit eb66ae0 upstream. Jann Horn points out that our TLB flushing was subtly wrong for the mremap() case. What makes mremap() special is that we don't follow the usual "add page to list of pages to be freed, then flush tlb, and then free pages". No, mremap() obviously just _moves_ the page from one page table location to another. That matters, because mremap() thus doesn't directly control the lifetime of the moved page with a freelist: instead, the lifetime of the page is controlled by the page table locking, that serializes access to the entry. As a result, we need to flush the TLB not just before releasing the lock for the source location (to avoid any concurrent accesses to the entry), but also before we release the destination page table lock (to avoid the TLB being flushed after somebody else has already done something to that page). This also makes the whole "need_flush" logic unnecessary, since we now always end up flushing the TLB for every valid entry. Reported-and-tested-by: Jann Horn <jannh@google.com> Acked-by: Will Deacon <will.deacon@arm.com> Tested-by: Ingo Molnar <mingo@kernel.org> Acked-by: Peter Zijlstra (Intel) <peterz@infradead.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent f473bf9 commit 541500a

3 files changed

Lines changed: 18 additions & 24 deletions

File tree

include/linux/huge_mm.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ extern int mincore_huge_pmd(struct vm_area_struct *vma, pmd_t *pmd,
4242
unsigned char *vec);
4343
extern bool move_huge_pmd(struct vm_area_struct *vma, unsigned long old_addr,
4444
unsigned long new_addr, unsigned long old_end,
45-
pmd_t *old_pmd, pmd_t *new_pmd, bool *need_flush);
45+
pmd_t *old_pmd, pmd_t *new_pmd);
4646
extern int change_huge_pmd(struct vm_area_struct *vma, pmd_t *pmd,
4747
unsigned long addr, pgprot_t newprot,
4848
int prot_numa);

mm/huge_memory.c

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1765,7 +1765,7 @@ static pmd_t move_soft_dirty_pmd(pmd_t pmd)
17651765

17661766
bool move_huge_pmd(struct vm_area_struct *vma, unsigned long old_addr,
17671767
unsigned long new_addr, unsigned long old_end,
1768-
pmd_t *old_pmd, pmd_t *new_pmd, bool *need_flush)
1768+
pmd_t *old_pmd, pmd_t *new_pmd)
17691769
{
17701770
spinlock_t *old_ptl, *new_ptl;
17711771
pmd_t pmd;
@@ -1796,7 +1796,7 @@ bool move_huge_pmd(struct vm_area_struct *vma, unsigned long old_addr,
17961796
if (new_ptl != old_ptl)
17971797
spin_lock_nested(new_ptl, SINGLE_DEPTH_NESTING);
17981798
pmd = pmdp_huge_get_and_clear(mm, old_addr, old_pmd);
1799-
if (pmd_present(pmd) && pmd_dirty(pmd))
1799+
if (pmd_present(pmd))
18001800
force_flush = true;
18011801
VM_BUG_ON(!pmd_none(*new_pmd));
18021802

@@ -1807,12 +1807,10 @@ bool move_huge_pmd(struct vm_area_struct *vma, unsigned long old_addr,
18071807
}
18081808
pmd = move_soft_dirty_pmd(pmd);
18091809
set_pmd_at(mm, new_addr, new_pmd, pmd);
1810-
if (new_ptl != old_ptl)
1811-
spin_unlock(new_ptl);
18121810
if (force_flush)
18131811
flush_tlb_range(vma, old_addr, old_addr + PMD_SIZE);
1814-
else
1815-
*need_flush = true;
1812+
if (new_ptl != old_ptl)
1813+
spin_unlock(new_ptl);
18161814
spin_unlock(old_ptl);
18171815
return true;
18181816
}

mm/mremap.c

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ static pte_t move_soft_dirty_pte(pte_t pte)
115115
static void move_ptes(struct vm_area_struct *vma, pmd_t *old_pmd,
116116
unsigned long old_addr, unsigned long old_end,
117117
struct vm_area_struct *new_vma, pmd_t *new_pmd,
118-
unsigned long new_addr, bool need_rmap_locks, bool *need_flush)
118+
unsigned long new_addr, bool need_rmap_locks)
119119
{
120120
struct mm_struct *mm = vma->vm_mm;
121121
pte_t *old_pte, *new_pte, pte;
@@ -163,29 +163,29 @@ static void move_ptes(struct vm_area_struct *vma, pmd_t *old_pmd,
163163

164164
pte = ptep_get_and_clear(mm, old_addr, old_pte);
165165
/*
166-
* If we are remapping a dirty PTE, make sure
166+
* If we are remapping a valid PTE, make sure
167167
* to flush TLB before we drop the PTL for the
168-
* old PTE or we may race with page_mkclean().
168+
* PTE.
169169
*
170-
* This check has to be done after we removed the
171-
* old PTE from page tables or another thread may
172-
* dirty it after the check and before the removal.
170+
* NOTE! Both old and new PTL matter: the old one
171+
* for racing with page_mkclean(), the new one to
172+
* make sure the physical page stays valid until
173+
* the TLB entry for the old mapping has been
174+
* flushed.
173175
*/
174-
if (pte_present(pte) && pte_dirty(pte))
176+
if (pte_present(pte))
175177
force_flush = true;
176178
pte = move_pte(pte, new_vma->vm_page_prot, old_addr, new_addr);
177179
pte = move_soft_dirty_pte(pte);
178180
set_pte_at(mm, new_addr, new_pte, pte);
179181
}
180182

181183
arch_leave_lazy_mmu_mode();
184+
if (force_flush)
185+
flush_tlb_range(vma, old_end - len, old_end);
182186
if (new_ptl != old_ptl)
183187
spin_unlock(new_ptl);
184188
pte_unmap(new_pte - 1);
185-
if (force_flush)
186-
flush_tlb_range(vma, old_end - len, old_end);
187-
else
188-
*need_flush = true;
189189
pte_unmap_unlock(old_pte - 1, old_ptl);
190190
if (need_rmap_locks)
191191
drop_rmap_locks(vma);
@@ -200,7 +200,6 @@ unsigned long move_page_tables(struct vm_area_struct *vma,
200200
{
201201
unsigned long extent, next, old_end;
202202
pmd_t *old_pmd, *new_pmd;
203-
bool need_flush = false;
204203
unsigned long mmun_start; /* For mmu_notifiers */
205204
unsigned long mmun_end; /* For mmu_notifiers */
206205

@@ -231,8 +230,7 @@ unsigned long move_page_tables(struct vm_area_struct *vma,
231230
if (need_rmap_locks)
232231
take_rmap_locks(vma);
233232
moved = move_huge_pmd(vma, old_addr, new_addr,
234-
old_end, old_pmd, new_pmd,
235-
&need_flush);
233+
old_end, old_pmd, new_pmd);
236234
if (need_rmap_locks)
237235
drop_rmap_locks(vma);
238236
if (moved)
@@ -250,10 +248,8 @@ unsigned long move_page_tables(struct vm_area_struct *vma,
250248
if (extent > LATENCY_LIMIT)
251249
extent = LATENCY_LIMIT;
252250
move_ptes(vma, old_pmd, old_addr, old_addr + extent, new_vma,
253-
new_pmd, new_addr, need_rmap_locks, &need_flush);
251+
new_pmd, new_addr, need_rmap_locks);
254252
}
255-
if (need_flush)
256-
flush_tlb_range(vma, old_end-len, old_addr);
257253

258254
mmu_notifier_invalidate_range_end(vma->vm_mm, mmun_start, mmun_end);
259255

0 commit comments

Comments
 (0)