Skip to content

Commit 63678c3

Browse files
Rich Felkertorvalds
authored andcommitted
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>
1 parent f4fcd55 commit 63678c3

1 file changed

Lines changed: 2 additions & 6 deletions

File tree

‎fs/ramfs/file-nommu.c‎

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -211,14 +211,11 @@ static unsigned long ramfs_nommu_get_unmapped_area(struct file *file,
211211
struct page **pages = NULL, **ptr, *page;
212212
loff_t isize;
213213

214-
if (!(flags & MAP_SHARED))
215-
return addr;
216-
217214
/* the mapping mustn't extend beyond the EOF */
218215
lpages = (len + PAGE_SIZE - 1) >> PAGE_SHIFT;
219216
isize = i_size_read(inode);
220217

221-
ret = -EINVAL;
218+
ret = -ENOSYS;
222219
maxpages = (isize + PAGE_SIZE - 1) >> PAGE_SHIFT;
223220
if (pgoff >= maxpages)
224221
goto out;
@@ -227,7 +224,6 @@ static unsigned long ramfs_nommu_get_unmapped_area(struct file *file,
227224
goto out;
228225

229226
/* gang-find the pages */
230-
ret = -ENOMEM;
231227
pages = kcalloc(lpages, sizeof(struct page *), GFP_KERNEL);
232228
if (!pages)
233229
goto out_free;
@@ -263,7 +259,7 @@ static unsigned long ramfs_nommu_get_unmapped_area(struct file *file,
263259
*/
264260
static int ramfs_nommu_mmap(struct file *file, struct vm_area_struct *vma)
265261
{
266-
if (!(vma->vm_flags & VM_SHARED))
262+
if (!(vma->vm_flags & (VM_SHARED | VM_MAYSHARE)))
267263
return -ENOSYS;
268264

269265
file_accessed(file);

0 commit comments

Comments
 (0)