Skip to content

Commit a7492fa

Browse files
Ryan Robertsjfvogel
authored andcommitted
mm: close theoretical race where stale TLB entries could linger
commit 383c4613c67c26e90e8eebb72e3083457d02033f upstream. Commit 3ea2771 ("mm, mprotect: flush TLB if potentially racing with a parallel reclaim leaving stale TLB entries") described a theoretical race as such: """ Nadav Amit identified a theoretical race between page reclaim and mprotect due to TLB flushes being batched outside of the PTL being held. He described the race as follows: CPU0 CPU1 ---- ---- user accesses memory using RW PTE [PTE now cached in TLB] try_to_unmap_one() ==> ptep_get_and_clear() ==> set_tlb_ubc_flush_pending() mprotect(addr, PROT_READ) ==> change_pte_range() ==> [ PTE non-present - no flush ] user writes using cached RW PTE ... try_to_unmap_flush() The same type of race exists for reads when protecting for PROT_NONE and also exists for operations that can leave an old TLB entry behind such as munmap, mremap and madvise. """ The solution was to introduce flush_tlb_batched_pending() and call it under the PTL from mprotect/madvise/munmap/mremap to complete any pending tlb flushes. However, while madvise_free_pte_range() and madvise_cold_or_pageout_pte_range() were both retro-fitted to call flush_tlb_batched_pending() immediately after initially acquiring the PTL, they both temporarily release the PTL to split a large folio if they stumble upon one. In this case, where re-acquiring the PTL flush_tlb_batched_pending() must be called again, but it previously was not. Let's fix that. There are 2 Fixes: tags here: the first is the commit that fixed madvise_free_pte_range(). The second is the commit that added madvise_cold_or_pageout_pte_range(), which looks like it copy/pasted the faulty pattern from madvise_free_pte_range(). This is a theoretical bug discovered during code review. Link: https://lkml.kernel.org/r/[email protected] Fixes: 3ea2771 ("mm, mprotect: flush TLB if potentially racing with a parallel reclaim leaving stale TLB entries") Fixes: 9c276cc ("mm: introduce MADV_COLD") Signed-off-by: Ryan Roberts <[email protected]> Reviewed-by: Jann Horn <[email protected]> Acked-by: David Hildenbrand <[email protected]> Cc: Liam Howlett <[email protected]> Cc: Lorenzo Stoakes <[email protected]> Cc: Mel Gorman <mgorman <[email protected]> Cc: Vlastimil Babka <[email protected]> Cc: <[email protected]> Signed-off-by: Andrew Morton <[email protected]> Signed-off-by: Greg Kroah-Hartman <[email protected]> (cherry picked from commit 510a29d7769907434bc26476043a36c51e034a0b) Signed-off-by: Jack Vogel <[email protected]>
1 parent aa45e3d commit a7492fa

File tree

1 file changed

+2
-0
lines changed

1 file changed

+2
-0
lines changed

mm/madvise.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -497,6 +497,7 @@ static int madvise_cold_or_pageout_pte_range(pmd_t *pmd,
497497
pte_offset_map_lock(mm, pmd, addr, &ptl);
498498
if (!start_pte)
499499
break;
500+
flush_tlb_batched_pending(mm);
500501
arch_enter_lazy_mmu_mode();
501502
if (!err)
502503
nr = 0;
@@ -730,6 +731,7 @@ static int madvise_free_pte_range(pmd_t *pmd, unsigned long addr,
730731
start_pte = pte;
731732
if (!start_pte)
732733
break;
734+
flush_tlb_batched_pending(mm);
733735
arch_enter_lazy_mmu_mode();
734736
if (!err)
735737
nr = 0;

0 commit comments

Comments
 (0)