From: Hillf Danton <dhillf@gmail.com>
To: Chris Metcalf <cmetcalf@tilera.com>
Cc: linux-kernel@vger.kernel.org, linux-mm@kvack.org,
Andrew Morton <akpm@linux-foundation.org>,
Michal Hocko <mhocko@suse.cz>,
KAMEZAWA Hiroyuki <kamezawa.hiroyu@jp.fujitsu.com>,
Hugh Dickins <hughd@google.com>
Subject: Re: [PATCH v2] hugetlb: fix race condition in hugetlb_fault()
Date: Sun, 1 Apr 2012 20:10:31 +0800 [thread overview]
Message-ID: <CAJd=RBBWx7uZcw=_oA06RVunPAGeFcJ7LY=RwFCyB_BreJb_kg@mail.gmail.com> (raw)
In-Reply-To: <201203311339.q2VDdJMD006254@farm-0012.internal.tilera.com>
On Sat, Mar 31, 2012 at 4:07 AM, Chris Metcalf <cmetcalf@tilera.com> wrote:
> The race is as follows. Suppose a multi-threaded task forks a new
> process, 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 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.
>
> The solution is to take an extra reference to the page while we are
> holding the lock on it.
>
If the following chart matches the above description,
===
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);
===
would you please spin with description refreshed?
> Cc: stable@kernel.org
Let Andrew do the stable work, ok?
Best Regards
-hd
> Signed-off-by: Chris Metcalf <cmetcalf@tilera.com>
> ---
> This change incorporates Hillf Danton's suggestion to just unconditionally
> get and put the page around the region of code in question.
>
> mm/hugetlb.c | 2 ++
> 1 files changed, 2 insertions(+), 0 deletions(-)
>
> diff --git a/mm/hugetlb.c b/mm/hugetlb.c
> index 1871753..5f53d6b 100644
> --- a/mm/hugetlb.c
> +++ b/mm/hugetlb.c
> @@ -2701,6 +2701,7 @@ int hugetlb_fault(struct mm_struct *mm, struct vm_area_struct *vma,
> * so no worry about deadlock.
> */
> page = pte_page(entry);
> + get_page(page);
> if (page != pagecache_page)
> lock_page(page);
>
> @@ -2732,6 +2733,7 @@ out_page_table_lock:
> }
> if (page != pagecache_page)
> unlock_page(page);
> + put_page(page);
>
> out_mutex:
> mutex_unlock(&hugetlb_instantiation_mutex);
> --
> 1.6.5.2
>
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to majordomo@kvack.org. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Fight unfair telecom internet charges in Canada: sign http://stopthemeter.ca/
Don't email: <a href=mailto:"dont@kvack.org"> email@kvack.org </a>
next prev parent reply other threads:[~2012-04-01 12:10 UTC|newest]
Thread overview: 17+ messages / expand[flat|nested] mbox.gz Atom feed top
2012-04-01 16:51 Chris Metcalf
2012-03-30 20:07 ` [PATCH] " Chris Metcalf
2012-03-30 20:07 ` [PATCH v2] " Chris Metcalf
2012-04-01 12:10 ` Hillf Danton [this message]
2012-03-30 20:07 ` [PATCH v3] " Chris Metcalf
2012-03-31 12:27 ` [PATCH] " Hillf Danton
2012-03-30 19:37 ` [PATCH v2] arch/tile: support multiple huge page sizes dynamically Chris Metcalf
2012-03-31 14:03 ` Hillf Danton
2012-03-30 19:37 ` [PATCH v3] " Chris Metcalf
2012-04-01 12:33 ` Hillf Danton
2012-04-01 16:46 ` Chris Metcalf
2012-04-02 2:21 ` Hillf Danton
2012-04-06 22:23 ` [PATCH v2] hugetlb: fix race condition in hugetlb_fault() Andrew Morton
2012-04-06 23:10 ` Hugh Dickins
2012-04-06 23:26 ` Andrew Morton
2012-04-06 23:35 ` Hugh Dickins
2012-04-07 0:25 ` Chris Metcalf
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to='CAJd=RBBWx7uZcw=_oA06RVunPAGeFcJ7LY=RwFCyB_BreJb_kg@mail.gmail.com' \
--to=dhillf@gmail.com \
--cc=akpm@linux-foundation.org \
--cc=cmetcalf@tilera.com \
--cc=hughd@google.com \
--cc=kamezawa.hiroyu@jp.fujitsu.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=mhocko@suse.cz \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox