diff options
author | Hugh Dickins <hugh@veritas.com> | 2007-03-29 04:20:38 -0400 |
---|---|---|
committer | Linus Torvalds <torvalds@woody.linux-foundation.org> | 2007-03-29 11:22:26 -0400 |
commit | 90ed52ebe48181d3c5427b3bd1d24f659e7575ad (patch) | |
tree | bfb735ab12ebe7964714c637bcb64163646cd347 /mm/madvise.c | |
parent | 16a100190d39592d1d56ff5a0b978b20288c3427 (diff) |
[PATCH] holepunch: fix mmap_sem i_mutex deadlock
sys_madvise has down_write of mmap_sem, then madvise_remove calls
vmtruncate_range which takes i_mutex and i_alloc_sem: no, we can easily devise
deadlocks from that ordering.
madvise_remove drop mmap_sem while calling vmtruncate_range: luckily, since
madvise_remove doesn't split or merge vmas, it's easy to handle this case with
a NULL prev, without restructuring sys_madvise. (Though sad to retake
mmap_sem when it's unlikely to be needed, and certainly down_read is
sufficient for MADV_REMOVE, unlike the other madvices.)
Signed-off-by: Hugh Dickins <hugh@veritas.com>
Cc: Miklos Szeredi <mszeredi@suse.cz>
Cc: Badari Pulavarty <pbadari@us.ibm.com>
Cc: Nick Piggin <npiggin@suse.de>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Diffstat (limited to 'mm/madvise.c')
-rw-r--r-- | mm/madvise.c | 19 |
1 files changed, 14 insertions, 5 deletions
diff --git a/mm/madvise.c b/mm/madvise.c index 77916e9fc52b..603c5257ed6e 100644 --- a/mm/madvise.c +++ b/mm/madvise.c | |||
@@ -159,9 +159,10 @@ static long madvise_remove(struct vm_area_struct *vma, | |||
159 | unsigned long start, unsigned long end) | 159 | unsigned long start, unsigned long end) |
160 | { | 160 | { |
161 | struct address_space *mapping; | 161 | struct address_space *mapping; |
162 | loff_t offset, endoff; | 162 | loff_t offset, endoff; |
163 | int error; | ||
163 | 164 | ||
164 | *prev = vma; | 165 | *prev = NULL; /* tell sys_madvise we drop mmap_sem */ |
165 | 166 | ||
166 | if (vma->vm_flags & (VM_LOCKED|VM_NONLINEAR|VM_HUGETLB)) | 167 | if (vma->vm_flags & (VM_LOCKED|VM_NONLINEAR|VM_HUGETLB)) |
167 | return -EINVAL; | 168 | return -EINVAL; |
@@ -180,7 +181,12 @@ static long madvise_remove(struct vm_area_struct *vma, | |||
180 | + ((loff_t)vma->vm_pgoff << PAGE_SHIFT); | 181 | + ((loff_t)vma->vm_pgoff << PAGE_SHIFT); |
181 | endoff = (loff_t)(end - vma->vm_start - 1) | 182 | endoff = (loff_t)(end - vma->vm_start - 1) |
182 | + ((loff_t)vma->vm_pgoff << PAGE_SHIFT); | 183 | + ((loff_t)vma->vm_pgoff << PAGE_SHIFT); |
183 | return vmtruncate_range(mapping->host, offset, endoff); | 184 | |
185 | /* vmtruncate_range needs to take i_mutex and i_alloc_sem */ | ||
186 | up_write(¤t->mm->mmap_sem); | ||
187 | error = vmtruncate_range(mapping->host, offset, endoff); | ||
188 | down_write(¤t->mm->mmap_sem); | ||
189 | return error; | ||
184 | } | 190 | } |
185 | 191 | ||
186 | static long | 192 | static long |
@@ -315,12 +321,15 @@ asmlinkage long sys_madvise(unsigned long start, size_t len_in, int behavior) | |||
315 | if (error) | 321 | if (error) |
316 | goto out; | 322 | goto out; |
317 | start = tmp; | 323 | start = tmp; |
318 | if (start < prev->vm_end) | 324 | if (prev && start < prev->vm_end) |
319 | start = prev->vm_end; | 325 | start = prev->vm_end; |
320 | error = unmapped_error; | 326 | error = unmapped_error; |
321 | if (start >= end) | 327 | if (start >= end) |
322 | goto out; | 328 | goto out; |
323 | vma = prev->vm_next; | 329 | if (prev) |
330 | vma = prev->vm_next; | ||
331 | else /* madvise_remove dropped mmap_sem */ | ||
332 | vma = find_vma(current->mm, start); | ||
324 | } | 333 | } |
325 | out: | 334 | out: |
326 | up_write(¤t->mm->mmap_sem); | 335 | up_write(¤t->mm->mmap_sem); |