Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

tmpfs/ramfs: fix VM_MAYSHARE mappings for NOMMU

The nommu do_mmap expects f_op->get_unmapped_area to either succeed or
return -ENOSYS for VM_MAYSHARE (e.g. private read-only) mappings.
Returning addr in the non-MAP_SHARED case was completely wrong, and only
happened to work because addr was 0. However, it prevented VM_MAYSHARE
mappings from sharing backing with the fs cache, and forced such
mappings (including shareable program text) to be copied whenever the
number of mappings transitioned from 0 to 1, impacting performance and
memory usage. Subsequent mappings beyond the first still correctly
shared memory with the first.

Instead, treat VM_MAYSHARE identically to VM_SHARED at the file ops level;
do_mmap already handles the semantic differences between them.

Signed-off-by: Rich Felker <dalias@libc.org>
Cc: Michal Hocko <mhocko@suse.com>
Cc: Greg Ungerer <gerg@uclinux.org>
Cc: Geert Uytterhoeven <geert@linux-m68k.org>
Cc: Yoshinori Sato <ysato@users.sourceforge.jp>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>

authored by

Rich Felker and committed by
Linus Torvalds
63678c32 f4fcd558

+2 -6
+2 -6
fs/ramfs/file-nommu.c
··· 211 211 struct page **pages = NULL, **ptr, *page; 212 212 loff_t isize; 213 213 214 - if (!(flags & MAP_SHARED)) 215 - return addr; 216 - 217 214 /* the mapping mustn't extend beyond the EOF */ 218 215 lpages = (len + PAGE_SIZE - 1) >> PAGE_SHIFT; 219 216 isize = i_size_read(inode); 220 217 221 - ret = -EINVAL; 218 + ret = -ENOSYS; 222 219 maxpages = (isize + PAGE_SIZE - 1) >> PAGE_SHIFT; 223 220 if (pgoff >= maxpages) 224 221 goto out; ··· 224 227 goto out; 225 228 226 229 /* gang-find the pages */ 227 - ret = -ENOMEM; 228 230 pages = kcalloc(lpages, sizeof(struct page *), GFP_KERNEL); 229 231 if (!pages) 230 232 goto out_free; ··· 259 263 */ 260 264 static int ramfs_nommu_mmap(struct file *file, struct vm_area_struct *vma) 261 265 { 262 - if (!(vma->vm_flags & VM_SHARED)) 266 + if (!(vma->vm_flags & (VM_SHARED | VM_MAYSHARE))) 263 267 return -ENOSYS; 264 268 265 269 file_accessed(file);