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>
This commit is contained in:
Rich Felker 2016-05-20 16:57:47 -07:00 коммит произвёл Linus Torvalds
Родитель f4fcd55841
Коммит 63678c32e2
1 изменённых файлов: 2 добавлений и 6 удалений

Просмотреть файл

@ -211,14 +211,11 @@ static unsigned long ramfs_nommu_get_unmapped_area(struct file *file,
struct page **pages = NULL, **ptr, *page; struct page **pages = NULL, **ptr, *page;
loff_t isize; loff_t isize;
if (!(flags & MAP_SHARED))
return addr;
/* the mapping mustn't extend beyond the EOF */ /* the mapping mustn't extend beyond the EOF */
lpages = (len + PAGE_SIZE - 1) >> PAGE_SHIFT; lpages = (len + PAGE_SIZE - 1) >> PAGE_SHIFT;
isize = i_size_read(inode); isize = i_size_read(inode);
ret = -EINVAL; ret = -ENOSYS;
maxpages = (isize + PAGE_SIZE - 1) >> PAGE_SHIFT; maxpages = (isize + PAGE_SIZE - 1) >> PAGE_SHIFT;
if (pgoff >= maxpages) if (pgoff >= maxpages)
goto out; goto out;
@ -227,7 +224,6 @@ static unsigned long ramfs_nommu_get_unmapped_area(struct file *file,
goto out; goto out;
/* gang-find the pages */ /* gang-find the pages */
ret = -ENOMEM;
pages = kcalloc(lpages, sizeof(struct page *), GFP_KERNEL); pages = kcalloc(lpages, sizeof(struct page *), GFP_KERNEL);
if (!pages) if (!pages)
goto out_free; goto out_free;
@ -263,7 +259,7 @@ out:
*/ */
static int ramfs_nommu_mmap(struct file *file, struct vm_area_struct *vma) static int ramfs_nommu_mmap(struct file *file, struct vm_area_struct *vma)
{ {
if (!(vma->vm_flags & VM_SHARED)) if (!(vma->vm_flags & (VM_SHARED | VM_MAYSHARE)))
return -ENOSYS; return -ENOSYS;
file_accessed(file); file_accessed(file);