Skip to content

Commit

Permalink
mm/hugetlb: check for reserved hugepages during memory offline
Browse files Browse the repository at this point in the history
In dissolve_free_huge_pages(), free hugepages will be dissolved without
making sure that there are enough of them left to satisfy hugepage
reservations.

Fix this by adding a return value to dissolve_free_huge_pages() and
checking h->free_huge_pages vs.  h->resv_huge_pages.  Note that this may
lead to the situation where dissolve_free_huge_page() returns an error
and all free hugepages that were dissolved before that error are lost,
while the memory block still cannot be set offline.

Fixes: c8721bb ("mm: memory-hotplug: enable memory hotplug to handle hugepage")
Link: http://lkml.kernel.org/r/[email protected]
Signed-off-by: Gerald Schaefer <[email protected]>
Acked-by: Michal Hocko <[email protected]>
Acked-by: Naoya Horiguchi <[email protected]>
Cc: "Kirill A . Shutemov" <[email protected]>
Cc: Vlastimil Babka <[email protected]>
Cc: Mike Kravetz <[email protected]>
Cc: "Aneesh Kumar K . V" <[email protected]>
Cc: Martin Schwidefsky <[email protected]>
Cc: Heiko Carstens <[email protected]>
Cc: Rui Teng <[email protected]>
Cc: Dave Hansen <[email protected]>
Signed-off-by: Andrew Morton <[email protected]>
Signed-off-by: Linus Torvalds <[email protected]>
  • Loading branch information
gerald-schaefer authored and torvalds committed Oct 8, 2016
1 parent 2247bb3 commit 082d5b6
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 9 deletions.
6 changes: 3 additions & 3 deletions include/linux/hugetlb.h
Original file line number Diff line number Diff line change
Expand Up @@ -450,8 +450,8 @@ static inline pgoff_t basepage_index(struct page *page)
return __basepage_index(page);
}

extern void dissolve_free_huge_pages(unsigned long start_pfn,
unsigned long end_pfn);
extern int dissolve_free_huge_pages(unsigned long start_pfn,
unsigned long end_pfn);
static inline bool hugepage_migration_supported(struct hstate *h)
{
#ifdef CONFIG_ARCH_ENABLE_HUGEPAGE_MIGRATION
Expand Down Expand Up @@ -518,7 +518,7 @@ static inline pgoff_t basepage_index(struct page *page)
{
return page->index;
}
#define dissolve_free_huge_pages(s, e) do {} while (0)
#define dissolve_free_huge_pages(s, e) 0
#define hugepage_migration_supported(h) false

static inline spinlock_t *huge_pte_lockptr(struct hstate *h,
Expand Down
26 changes: 21 additions & 5 deletions mm/hugetlb.c
Original file line number Diff line number Diff line change
Expand Up @@ -1437,39 +1437,55 @@ static int free_pool_huge_page(struct hstate *h, nodemask_t *nodes_allowed,

/*
* Dissolve a given free hugepage into free buddy pages. This function does
* nothing for in-use (including surplus) hugepages.
* nothing for in-use (including surplus) hugepages. Returns -EBUSY if the
* number of free hugepages would be reduced below the number of reserved
* hugepages.
*/
static void dissolve_free_huge_page(struct page *page)
static int dissolve_free_huge_page(struct page *page)
{
int rc = 0;

spin_lock(&hugetlb_lock);
if (PageHuge(page) && !page_count(page)) {
struct page *head = compound_head(page);
struct hstate *h = page_hstate(head);
int nid = page_to_nid(head);
if (h->free_huge_pages - h->resv_huge_pages == 0) {
rc = -EBUSY;
goto out;
}
list_del(&head->lru);
h->free_huge_pages--;
h->free_huge_pages_node[nid]--;
h->max_huge_pages--;
update_and_free_page(h, head);
}
out:
spin_unlock(&hugetlb_lock);
return rc;
}

/*
* Dissolve free hugepages in a given pfn range. Used by memory hotplug to
* make specified memory blocks removable from the system.
* Note that this will dissolve a free gigantic hugepage completely, if any
* part of it lies within the given range.
* Also note that if dissolve_free_huge_page() returns with an error, all
* free hugepages that were dissolved before that error are lost.
*/
void dissolve_free_huge_pages(unsigned long start_pfn, unsigned long end_pfn)
int dissolve_free_huge_pages(unsigned long start_pfn, unsigned long end_pfn)
{
unsigned long pfn;
int rc = 0;

if (!hugepages_supported())
return;
return rc;

for (pfn = start_pfn; pfn < end_pfn; pfn += 1 << minimum_order)
dissolve_free_huge_page(pfn_to_page(pfn));
if (rc = dissolve_free_huge_page(pfn_to_page(pfn)))
break;

return rc;
}

/*
Expand Down
4 changes: 3 additions & 1 deletion mm/memory_hotplug.c
Original file line number Diff line number Diff line change
Expand Up @@ -1945,7 +1945,9 @@ static int __ref __offline_pages(unsigned long start_pfn,
* dissolve free hugepages in the memory block before doing offlining
* actually in order to make hugetlbfs's object counting consistent.
*/
dissolve_free_huge_pages(start_pfn, end_pfn);
ret = dissolve_free_huge_pages(start_pfn, end_pfn);
if (ret)
goto failed_removal;
/* check again */
offlined_pages = check_pages_isolated(start_pfn, end_pfn);
if (offlined_pages < 0) {
Expand Down

0 comments on commit 082d5b6

Please sign in to comment.