Skip to content

Commit

Permalink
mm/mmap.c:insert_vm_struct(): check for failure before setting values
Browse files Browse the repository at this point in the history
There's no point in initializing vma->vm_pgoff if the insertion attempt
will be failing anyway.  Run the checks before performing the
initialization.

Signed-off-by: Chen Gang <[email protected]>
Cc: Michal Hocko <[email protected]>
Signed-off-by: Andrew Morton <[email protected]>
Signed-off-by: Linus Torvalds <[email protected]>
  • Loading branch information
Chen-Gang authored and torvalds committed Sep 8, 2015
1 parent bde43c6 commit c9d13f5
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions mm/mmap.c
Original file line number Diff line number Diff line change
Expand Up @@ -2871,6 +2871,13 @@ int insert_vm_struct(struct mm_struct *mm, struct vm_area_struct *vma)
struct vm_area_struct *prev;
struct rb_node **rb_link, *rb_parent;

if (find_vma_links(mm, vma->vm_start, vma->vm_end,
&prev, &rb_link, &rb_parent))
return -ENOMEM;
if ((vma->vm_flags & VM_ACCOUNT) &&
security_vm_enough_memory_mm(mm, vma_pages(vma)))
return -ENOMEM;

/*
* The vm_pgoff of a purely anonymous vma should be irrelevant
* until its first write fault, when page's anon_vma and index
Expand All @@ -2887,12 +2894,6 @@ int insert_vm_struct(struct mm_struct *mm, struct vm_area_struct *vma)
BUG_ON(vma->anon_vma);
vma->vm_pgoff = vma->vm_start >> PAGE_SHIFT;
}
if (find_vma_links(mm, vma->vm_start, vma->vm_end,
&prev, &rb_link, &rb_parent))
return -ENOMEM;
if ((vma->vm_flags & VM_ACCOUNT) &&
security_vm_enough_memory_mm(mm, vma_pages(vma)))
return -ENOMEM;

vma_link(mm, vma, prev, rb_link, rb_parent);
return 0;
Expand Down

0 comments on commit c9d13f5

Please sign in to comment.