nommu: fix race between ramfs truncation and shared mmap

Fix the race between the truncation of a ramfs file and an attempt to make
a shared mmap of region of that file.

The problem is that do_mmap_pgoff() calls f_op->get_unmapped_area() to
verify that the file region is made of contiguous pages and to find its
base address - but there isn't any locking to guarantee this region until
vma_prio_tree_insert() is called by add_vma_to_mm().

Note that moving the functionality into f_op->mmap() doesn't help as that
is also called before vma_prio_tree_insert().

Instead make ramfs_nommu_check_mappings() grab nommu_region_sem whilst it
does its checks. This means that this function will wait whilst mmaps
take place.

Signed-off-by: David Howells <dhowells@redhat.com>
Acked-by: Al Viro <viro@zeniv.linux.org.uk>
Cc: Greg Ungerer <gerg@snapgear.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>

authored by David Howells and committed by Linus Torvalds 81759b5b efc1a3b1

+6 -1
+6 -1
fs/ramfs/file-nommu.c
··· 131 struct vm_area_struct *vma; 132 struct prio_tree_iter iter; 133 134 /* search for VMAs that fall within the dead zone */ 135 vma_prio_tree_foreach(vma, &iter, &inode->i_mapping->i_mmap, 136 newsize >> PAGE_SHIFT, ··· 140 ) { 141 /* found one - only interested if it's shared out of the page 142 * cache */ 143 - if (vma->vm_flags & VM_SHARED) 144 return -ETXTBSY; /* not quite true, but near enough */ 145 } 146 147 return 0; 148 } 149
··· 131 struct vm_area_struct *vma; 132 struct prio_tree_iter iter; 133 134 + down_write(&nommu_region_sem); 135 + 136 /* search for VMAs that fall within the dead zone */ 137 vma_prio_tree_foreach(vma, &iter, &inode->i_mapping->i_mmap, 138 newsize >> PAGE_SHIFT, ··· 138 ) { 139 /* found one - only interested if it's shared out of the page 140 * cache */ 141 + if (vma->vm_flags & VM_SHARED) { 142 + up_write(&nommu_region_sem); 143 return -ETXTBSY; /* not quite true, but near enough */ 144 + } 145 } 146 147 + up_write(&nommu_region_sem); 148 return 0; 149 } 150