diff options
author | Chris Metcalf <cmetcalf@tilera.com> | 2012-04-12 15:49:15 -0400 |
---|---|---|
committer | Linus Torvalds <torvalds@linux-foundation.org> | 2012-04-12 16:12:12 -0400 |
commit | 66aebce747eaf9bc456bf1f1b217d8db843031d0 (patch) | |
tree | 460c0268105191b2d581f42d5534dc952d565a17 /mm/hugetlb.c | |
parent | f3ec434c69ac7f447ff6e6389c19727c9f002087 (diff) |
hugetlb: fix race condition in hugetlb_fault()
The race is as follows:
Suppose a multi-threaded task forks a new process (on cpu A), thus
bumping up the ref count on all the pages. While the fork is occurring
(and thus we have marked all the PTEs as read-only), another thread in
the original process (on cpu B) tries to write to a huge page, taking an
access violation from the write-protect and calling hugetlb_cow(). Now,
suppose the fork() fails. It will undo the COW and decrement the ref
count on the pages, so the ref count on the huge page drops back to 1.
Meanwhile hugetlb_cow() also decrements the ref count by one on the
original page, since the original address space doesn't need it any
more, having copied a new page to replace the original page. This
leaves the ref count at zero, and when we call unlock_page(), we panic.
fork on CPU A fault on CPU B
============= ==============
...
down_write(&parent->mmap_sem);
down_write_nested(&child->mmap_sem);
...
while duplicating vmas
if error
break;
...
up_write(&child->mmap_sem);
up_write(&parent->mmap_sem); ...
down_read(&parent->mmap_sem);
...
lock_page(page);
handle COW
page_mapcount(old_page) == 2
alloc and prepare new_page
...
handle error
page_remove_rmap(page);
put_page(page);
...
fold new_page into pte
page_remove_rmap(page);
put_page(page);
...
oops ==> unlock_page(page);
up_read(&parent->mmap_sem);
The solution is to take an extra reference to the page while we are
holding the lock on it.
Signed-off-by: Chris Metcalf <cmetcalf@tilera.com>
Cc: Hillf Danton <dhillf@gmail.com>
Cc: Michal Hocko <mhocko@suse.cz>
Cc: KAMEZAWA Hiroyuki <kamezawa.hiroyu@jp.fujitsu.com>
Cc: Hugh Dickins <hughd@google.com>
Cc: <stable@vger.kernel.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Diffstat (limited to 'mm/hugetlb.c')
-rw-r--r-- | mm/hugetlb.c | 2 |
1 files changed, 2 insertions, 0 deletions
diff --git a/mm/hugetlb.c b/mm/hugetlb.c index b8ce6f450956..cd65cb19c941 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c | |||
@@ -2791,6 +2791,7 @@ int hugetlb_fault(struct mm_struct *mm, struct vm_area_struct *vma, | |||
2791 | * so no worry about deadlock. | 2791 | * so no worry about deadlock. |
2792 | */ | 2792 | */ |
2793 | page = pte_page(entry); | 2793 | page = pte_page(entry); |
2794 | get_page(page); | ||
2794 | if (page != pagecache_page) | 2795 | if (page != pagecache_page) |
2795 | lock_page(page); | 2796 | lock_page(page); |
2796 | 2797 | ||
@@ -2822,6 +2823,7 @@ out_page_table_lock: | |||
2822 | } | 2823 | } |
2823 | if (page != pagecache_page) | 2824 | if (page != pagecache_page) |
2824 | unlock_page(page); | 2825 | unlock_page(page); |
2826 | put_page(page); | ||
2825 | 2827 | ||
2826 | out_mutex: | 2828 | out_mutex: |
2827 | mutex_unlock(&hugetlb_instantiation_mutex); | 2829 | mutex_unlock(&hugetlb_instantiation_mutex); |