Skip to content

Commit 79f3d12

Browse files
howlettakpm00
authored andcommitted
mm/mmap: fix race in mmap_region() with ftruncate()
Avoiding the zeroing of the vma tree in mmap_region() introduced a race with truncate in the page table walk. To avoid any races, create a hole in the rmap during the operation by clearing the pagetable entries earlier under the mmap write lock and (critically) before the new vma is installed into the vma tree. The result is that the old vma(s) are left in the vma tree, but free_pgtables() removes them from the rmap and clears the ptes while holding the necessary locks. This change extends the fix required for hugetblfs and the call_mmap() function by moving the cleanup higher in the function and running it unconditionally. Link: https://lkml.kernel.org/r/[email protected] Fixes: f8d112a ("mm/mmap: avoid zeroing vma tree in mmap_region()") Signed-off-by: Liam R. Howlett <[email protected]> Reported-by: Jann Horn <[email protected]> Closes: https://lore.kernel.org/all/CAG48ez0ZpGzxi=-5O_uGQ0xKXOmbjeQ0LjZsRJ1Qtf2X5eOr1w@mail.gmail.com/ Reviewed-by: Jann Horn <[email protected]> Reviewed-by: Lorenzo Stoakes <[email protected]> Acked-by: Vlastimil Babka <[email protected]> Cc: Matthew Wilcox <[email protected]> Cc: David Hildenbrand <[email protected]> Signed-off-by: Andrew Morton <[email protected]>
1 parent 281dd25 commit 79f3d12

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

mm/mmap.c

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1418,6 +1418,13 @@ unsigned long mmap_region(struct file *file, unsigned long addr,
14181418
vmg.flags = vm_flags;
14191419
}
14201420

1421+
/*
1422+
* clear PTEs while the vma is still in the tree so that rmap
1423+
* cannot race with the freeing later in the truncate scenario.
1424+
* This is also needed for call_mmap(), which is why vm_ops
1425+
* close function is called.
1426+
*/
1427+
vms_clean_up_area(&vms, &mas_detach);
14211428
vma = vma_merge_new_range(&vmg);
14221429
if (vma)
14231430
goto expanded;
@@ -1439,11 +1446,6 @@ unsigned long mmap_region(struct file *file, unsigned long addr,
14391446

14401447
if (file) {
14411448
vma->vm_file = get_file(file);
1442-
/*
1443-
* call_mmap() may map PTE, so ensure there are no existing PTEs
1444-
* and call the vm_ops close function if one exists.
1445-
*/
1446-
vms_clean_up_area(&vms, &mas_detach);
14471449
error = call_mmap(file, vma);
14481450
if (error)
14491451
goto unmap_and_free_vma;

0 commit comments

Comments
 (0)