Commit ac09b3a1 authored by Adam Litke's avatar Adam Litke Committed by Linus Torvalds

hugetlb: close a difficult to trigger reservation race

A hugetlb reservation may be inadequately backed in the event of racing
allocations and frees when utilizing surplus huge pages.  Consider the
following series of events in processes A and B:

 A) Allocates some surplus pages to satisfy a reservation
 B) Frees some huge pages
 A) A notices the extra free pages and drops hugetlb_lock to free some of
    its surplus pages back to the buddy allocator.
 B) Allocates some huge pages
 A) Reacquires hugetlb_lock and returns from gather_surplus_huge_pages()

Avoid this by commiting the reservation after pages have been allocated but
before dropping the lock to free excess pages.  For parity, release the
reservation in return_unused_surplus_pages().

This patch also corrects the cpuset_mems_nr() error path in
hugetlb_acct_memory().  If the cpuset check fails, uncommit the
reservation, but also be sure to return any surplus huge pages that may
have been allocated to back the failed reservation.

Thanks to Andy Whitcroft for discovering this.
Signed-off-by: default avatarAdam Litke <agl@us.ibm.com>
Cc: Mel Gorman <mel@csn.ul.ie>
Cc: Andy Whitcroft <apw@shadowen.org>
Cc: Dave Hansen <haveblue@us.ibm.com>
Cc: William Lee Irwin III <wli@holomorphy.com>
Cc: Andy Whitcroft <apw@shadowen.org>
Cc: Mel Gorman <mel@csn.ul.ie>
Cc: David Gibson <david@gibson.dropbear.id.au>
Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
Signed-off-by: default avatarLinus Torvalds <torvalds@linux-foundation.org>
parent a07e6ab4
...@@ -296,8 +296,10 @@ static int gather_surplus_pages(int delta) ...@@ -296,8 +296,10 @@ static int gather_surplus_pages(int delta)
int needed, allocated; int needed, allocated;
needed = (resv_huge_pages + delta) - free_huge_pages; needed = (resv_huge_pages + delta) - free_huge_pages;
if (needed <= 0) if (needed <= 0) {
resv_huge_pages += delta;
return 0; return 0;
}
allocated = 0; allocated = 0;
INIT_LIST_HEAD(&surplus_list); INIT_LIST_HEAD(&surplus_list);
...@@ -335,9 +337,12 @@ static int gather_surplus_pages(int delta) ...@@ -335,9 +337,12 @@ static int gather_surplus_pages(int delta)
* The surplus_list now contains _at_least_ the number of extra pages * The surplus_list now contains _at_least_ the number of extra pages
* needed to accomodate the reservation. Add the appropriate number * needed to accomodate the reservation. Add the appropriate number
* of pages to the hugetlb pool and free the extras back to the buddy * of pages to the hugetlb pool and free the extras back to the buddy
* allocator. * allocator. Commit the entire reservation here to prevent another
* process from stealing the pages as they are added to the pool but
* before they are reserved.
*/ */
needed += allocated; needed += allocated;
resv_huge_pages += delta;
ret = 0; ret = 0;
free: free:
list_for_each_entry_safe(page, tmp, &surplus_list, lru) { list_for_each_entry_safe(page, tmp, &surplus_list, lru) {
...@@ -371,6 +376,9 @@ static void return_unused_surplus_pages(unsigned long unused_resv_pages) ...@@ -371,6 +376,9 @@ static void return_unused_surplus_pages(unsigned long unused_resv_pages)
struct page *page; struct page *page;
unsigned long nr_pages; unsigned long nr_pages;
/* Uncommit the reservation */
resv_huge_pages -= unused_resv_pages;
nr_pages = min(unused_resv_pages, surplus_huge_pages); nr_pages = min(unused_resv_pages, surplus_huge_pages);
while (nr_pages) { while (nr_pages) {
...@@ -1205,12 +1213,13 @@ static int hugetlb_acct_memory(long delta) ...@@ -1205,12 +1213,13 @@ static int hugetlb_acct_memory(long delta)
if (gather_surplus_pages(delta) < 0) if (gather_surplus_pages(delta) < 0)
goto out; goto out;
if (delta > cpuset_mems_nr(free_huge_pages_node)) if (delta > cpuset_mems_nr(free_huge_pages_node)) {
return_unused_surplus_pages(delta);
goto out; goto out;
} }
}
ret = 0; ret = 0;
resv_huge_pages += delta;
if (delta < 0) if (delta < 0)
return_unused_surplus_pages((unsigned long) -delta); return_unused_surplus_pages((unsigned long) -delta);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment