Skip to content

Commit

Permalink
mm: madvise avoid exclusive mmap_sem
Browse files Browse the repository at this point in the history
Avoid down_write of the mmap_sem in madvise when we can help it.

Acked-by: Hugh Dickins <[email protected]>
Signed-off-by: Nick Piggin <[email protected]>
Cc: Rik van Riel <[email protected]>
Signed-off-by: Andrew Morton <[email protected]>
Signed-off-by: Linus Torvalds <[email protected]>
  • Loading branch information
Nick Piggin authored and Linus Torvalds committed May 7, 2007
1 parent b416952 commit 0a27a14
Showing 1 changed file with 29 additions and 4 deletions.
33 changes: 29 additions & 4 deletions mm/madvise.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,24 @@
#include <linux/mempolicy.h>
#include <linux/hugetlb.h>

/*
* Any behaviour which results in changes to the vma->vm_flags needs to
* take mmap_sem for writing. Others, which simply traverse vmas, need
* to only take it for reading.
*/
static int madvise_need_mmap_write(int behavior)
{
switch (behavior) {
case MADV_REMOVE:
case MADV_WILLNEED:
case MADV_DONTNEED:
return 0;
default:
/* be safe, default to 1. list exceptions explicitly */
return 1;
}
}

/*
* We can potentially split a vm area into separate
* areas, each area with its own behavior.
Expand Down Expand Up @@ -183,9 +201,9 @@ static long madvise_remove(struct vm_area_struct *vma,
+ ((loff_t)vma->vm_pgoff << PAGE_SHIFT);

/* vmtruncate_range needs to take i_mutex and i_alloc_sem */
up_write(&current->mm->mmap_sem);
up_read(&current->mm->mmap_sem);
error = vmtruncate_range(mapping->host, offset, endoff);
down_write(&current->mm->mmap_sem);
down_read(&current->mm->mmap_sem);
return error;
}

Expand Down Expand Up @@ -270,7 +288,10 @@ asmlinkage long sys_madvise(unsigned long start, size_t len_in, int behavior)
int error = -EINVAL;
size_t len;

down_write(&current->mm->mmap_sem);
if (madvise_need_mmap_write(behavior))
down_write(&current->mm->mmap_sem);
else
down_read(&current->mm->mmap_sem);

if (start & ~PAGE_MASK)
goto out;
Expand Down Expand Up @@ -332,6 +353,10 @@ asmlinkage long sys_madvise(unsigned long start, size_t len_in, int behavior)
vma = find_vma(current->mm, start);
}
out:
up_write(&current->mm->mmap_sem);
if (madvise_need_mmap_write(behavior))
up_write(&current->mm->mmap_sem);
else
up_read(&current->mm->mmap_sem);

return error;
}

0 comments on commit 0a27a14

Please sign in to comment.