Skip to content

Commit faf53de

Browse files
Naoya Horiguchitorvalds
authored andcommitted
mm: hugetlb: soft-offline: dissolve_free_huge_page() return zero on !PageHuge
madvise(MADV_SOFT_OFFLINE) often returns -EBUSY when calling soft offline for hugepages with overcommitting enabled. That was caused by the suboptimal code in current soft-offline code. See the following part: ret = migrate_pages(&pagelist, new_page, NULL, MPOL_MF_MOVE_ALL, MIGRATE_SYNC, MR_MEMORY_FAILURE); if (ret) { ... } else { /* * We set PG_hwpoison only when the migration source hugepage * was successfully dissolved, because otherwise hwpoisoned * hugepage remains on free hugepage list, then userspace will * find it as SIGBUS by allocation failure. That's not expected * in soft-offlining. */ ret = dissolve_free_huge_page(page); if (!ret) { if (set_hwpoison_free_buddy_page(page)) num_poisoned_pages_inc(); } } return ret; Here dissolve_free_huge_page() returns -EBUSY if the migration source page was freed into buddy in migrate_pages(), but even in that case we actually has a chance that set_hwpoison_free_buddy_page() succeeds. So that means current code gives up offlining too early now. dissolve_free_huge_page() checks that a given hugepage is suitable for dissolving, where we should return success for !PageHuge() case because the given hugepage is considered as already dissolved. This change also affects other callers of dissolve_free_huge_page(), which are cleaned up together. [[email protected]: v3] Link: http://lkml.kernel.org/r/[email protected]: http://lkml.kernel.org/r/[email protected] Fixes: 6bc9b56 ("mm: fix race on soft-offlining") Signed-off-by: Naoya Horiguchi <[email protected]> Reported-by: Chen, Jerry T <[email protected]> Tested-by: Chen, Jerry T <[email protected]> Reviewed-by: Mike Kravetz <[email protected]> Reviewed-by: Oscar Salvador <[email protected]> Cc: Michal Hocko <[email protected]> Cc: Xishi Qiu <[email protected]> Cc: "Chen, Jerry T" <[email protected]> Cc: "Zhuo, Qiuxu" <[email protected]> Cc: <[email protected]> [4.19+] Signed-off-by: Andrew Morton <[email protected]> Signed-off-by: Linus Torvalds <[email protected]>
1 parent b38e596 commit faf53de

File tree

2 files changed

+21
-13
lines changed

2 files changed

+21
-13
lines changed

mm/hugetlb.c

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1510,16 +1510,29 @@ static int free_pool_huge_page(struct hstate *h, nodemask_t *nodes_allowed,
15101510

15111511
/*
15121512
* Dissolve a given free hugepage into free buddy pages. This function does
1513-
* nothing for in-use (including surplus) hugepages. Returns -EBUSY if the
1514-
* dissolution fails because a give page is not a free hugepage, or because
1515-
* free hugepages are fully reserved.
1513+
* nothing for in-use hugepages and non-hugepages.
1514+
* This function returns values like below:
1515+
*
1516+
* -EBUSY: failed to dissolved free hugepages or the hugepage is in-use
1517+
* (allocated or reserved.)
1518+
* 0: successfully dissolved free hugepages or the page is not a
1519+
* hugepage (considered as already dissolved)
15161520
*/
15171521
int dissolve_free_huge_page(struct page *page)
15181522
{
15191523
int rc = -EBUSY;
15201524

1525+
/* Not to disrupt normal path by vainly holding hugetlb_lock */
1526+
if (!PageHuge(page))
1527+
return 0;
1528+
15211529
spin_lock(&hugetlb_lock);
1522-
if (PageHuge(page) && !page_count(page)) {
1530+
if (!PageHuge(page)) {
1531+
rc = 0;
1532+
goto out;
1533+
}
1534+
1535+
if (!page_count(page)) {
15231536
struct page *head = compound_head(page);
15241537
struct hstate *h = page_hstate(head);
15251538
int nid = page_to_nid(head);
@@ -1564,11 +1577,9 @@ int dissolve_free_huge_pages(unsigned long start_pfn, unsigned long end_pfn)
15641577

15651578
for (pfn = start_pfn; pfn < end_pfn; pfn += 1 << minimum_order) {
15661579
page = pfn_to_page(pfn);
1567-
if (PageHuge(page) && !page_count(page)) {
1568-
rc = dissolve_free_huge_page(page);
1569-
if (rc)
1570-
break;
1571-
}
1580+
rc = dissolve_free_huge_page(page);
1581+
if (rc)
1582+
break;
15721583
}
15731584

15741585
return rc;

mm/memory-failure.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1856,11 +1856,8 @@ static int soft_offline_in_use_page(struct page *page, int flags)
18561856

18571857
static int soft_offline_free_page(struct page *page)
18581858
{
1859-
int rc = 0;
1860-
struct page *head = compound_head(page);
1859+
int rc = dissolve_free_huge_page(page);
18611860

1862-
if (PageHuge(head))
1863-
rc = dissolve_free_huge_page(page);
18641861
if (!rc) {
18651862
if (set_hwpoison_free_buddy_page(page))
18661863
num_poisoned_pages_inc();

0 commit comments

Comments
 (0)