Commit b2c56e4f authored by Oleg Nesterov's avatar Oleg Nesterov Committed by Linus Torvalds

mm: shift VM_GROWS* check from mmap_region() to do_mmap_pgoff()

mmap() doesn't allow the non-anonymous mappings with VM_GROWS* bit set.
In particular this means that mmap_region()->vma_merge(file, vm_flags)
must always fail if "vm_flags & VM_GROWS" is set incorrectly.

So it does not make sense to check VM_GROWS* after we already allocated
the new vma, the only caller, do_mmap_pgoff(), which can pass this flag
can do the check itself.

And this looks a bit more correct, mmap_region() already unmapped the
old mapping at this stage. But if mmap() is going to fail, it should
avoid do_munmap() if possible.

Note: we check VM_GROWS at the end to ensure that do_mmap_pgoff() won't
return EINVAL in the case when it currently returns another error code.

Many thanks to Hugh who nacked the buggy v1.
Signed-off-by: default avatarOleg Nesterov <oleg@redhat.com>
Acked-by: default avatarHugh Dickins <hughd@google.com>
Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
Signed-off-by: default avatarLinus Torvalds <torvalds@linux-foundation.org>
parent 465c47fd
...@@ -1302,6 +1302,8 @@ unsigned long do_mmap_pgoff(struct file *file, unsigned long addr, ...@@ -1302,6 +1302,8 @@ unsigned long do_mmap_pgoff(struct file *file, unsigned long addr,
if (!file->f_op || !file->f_op->mmap) if (!file->f_op || !file->f_op->mmap)
return -ENODEV; return -ENODEV;
if (vm_flags & (VM_GROWSDOWN|VM_GROWSUP))
return -EINVAL;
break; break;
default: default:
...@@ -1310,6 +1312,8 @@ unsigned long do_mmap_pgoff(struct file *file, unsigned long addr, ...@@ -1310,6 +1312,8 @@ unsigned long do_mmap_pgoff(struct file *file, unsigned long addr,
} else { } else {
switch (flags & MAP_TYPE) { switch (flags & MAP_TYPE) {
case MAP_SHARED: case MAP_SHARED:
if (vm_flags & (VM_GROWSDOWN|VM_GROWSUP))
return -EINVAL;
/* /*
* Ignore pgoff. * Ignore pgoff.
*/ */
...@@ -1544,11 +1548,7 @@ unsigned long mmap_region(struct file *file, unsigned long addr, ...@@ -1544,11 +1548,7 @@ unsigned long mmap_region(struct file *file, unsigned long addr,
vma->vm_pgoff = pgoff; vma->vm_pgoff = pgoff;
INIT_LIST_HEAD(&vma->anon_vma_chain); INIT_LIST_HEAD(&vma->anon_vma_chain);
error = -EINVAL; /* when rejecting VM_GROWSDOWN|VM_GROWSUP */
if (file) { if (file) {
if (vm_flags & (VM_GROWSDOWN|VM_GROWSUP))
goto free_vma;
if (vm_flags & VM_DENYWRITE) { if (vm_flags & VM_DENYWRITE) {
error = deny_write_access(file); error = deny_write_access(file);
if (error) if (error)
...@@ -1573,8 +1573,6 @@ unsigned long mmap_region(struct file *file, unsigned long addr, ...@@ -1573,8 +1573,6 @@ unsigned long mmap_region(struct file *file, unsigned long addr,
pgoff = vma->vm_pgoff; pgoff = vma->vm_pgoff;
vm_flags = vma->vm_flags; vm_flags = vma->vm_flags;
} else if (vm_flags & VM_SHARED) { } else if (vm_flags & VM_SHARED) {
if (unlikely(vm_flags & (VM_GROWSDOWN|VM_GROWSUP)))
goto free_vma;
error = shmem_zero_setup(vma); error = shmem_zero_setup(vma);
if (error) if (error)
goto free_vma; goto free_vma;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment