[RFC PATCH 2/3] hugetlb: add HPageCma flag and code to free non-gigantic pages in CMA

From: Mike Kravetz
Date: Mon Mar 08 2021 - 19:20:10 EST


When huge page demotion is fully implemented, gigantic pages can be
demoted to a smaller huge page size. For example, on x86 a 1G page
can be demoted to 512 2M pages. However, gigantic pages can be allocated
from CMA. If a gigantic page which was allocated from CMA is demoted,
the corresponding 2M pages need to be returned to CMA.

In order to track hugetlb pages that need to be returned to CMA, add the
hugetlb specific flag HPageCma. Flag is set when a huge page is
allocated from CMA and transferred to any demoted pages. Non-gigantic
huge page freeing code checks for the flag and takes appropriate action.

Signed-off-by: Mike Kravetz <mike.kravetz@xxxxxxxxxx>
---
include/linux/hugetlb.h | 7 +++++++
mm/hugetlb.c | 27 +++++++++++++++++++++++++--
2 files changed, 32 insertions(+), 2 deletions(-)

diff --git a/include/linux/hugetlb.h b/include/linux/hugetlb.h
index 5e9d6c8ab411..b4ec2daea5aa 100644
--- a/include/linux/hugetlb.h
+++ b/include/linux/hugetlb.h
@@ -498,12 +498,18 @@ unsigned long hugetlb_get_unmapped_area(struct file *file, unsigned long addr,
* modifications require hugetlb_lock.
* HPG_freed - Set when page is on the free lists.
* Synchronization: hugetlb_lock held for examination and modification.
+ * HPG_cma - Set if huge page was directly allocated from CMA area via
+ * cma_alloc. Initially set for gigantic page cma allocations, but can
+ * be set in non-gigantic pages if gigantic pages are demoted.
+ * Synchronization: Only accessed or modified when there is only one
+ * reference to the page at allocation, free or demote time.
*/
enum hugetlb_page_flags {
HPG_restore_reserve = 0,
HPG_migratable,
HPG_temporary,
HPG_freed,
+ HPG_cma,
__NR_HPAGEFLAGS,
};

@@ -549,6 +555,7 @@ HPAGEFLAG(RestoreReserve, restore_reserve)
HPAGEFLAG(Migratable, migratable)
HPAGEFLAG(Temporary, temporary)
HPAGEFLAG(Freed, freed)
+HPAGEFLAG(Cma, cma)

#ifdef CONFIG_HUGETLB_PAGE

diff --git a/mm/hugetlb.c b/mm/hugetlb.c
index 161732ba7aaf..0c29b7d0d469 100644
--- a/mm/hugetlb.c
+++ b/mm/hugetlb.c
@@ -1261,8 +1261,10 @@ static struct page *alloc_gigantic_page(struct hstate *h, gfp_t gfp_mask,
if (hugetlb_cma[nid]) {
page = cma_alloc(hugetlb_cma[nid], nr_pages,
huge_page_order(h), true);
- if (page)
+ if (page) {
+ SetHPageCma(page);
return page;
+ }
}

if (!(gfp_mask & __GFP_THISNODE)) {
@@ -1272,8 +1274,10 @@ static struct page *alloc_gigantic_page(struct hstate *h, gfp_t gfp_mask,

page = cma_alloc(hugetlb_cma[node], nr_pages,
huge_page_order(h), true);
- if (page)
+ if (page) {
+ SetHPageCma(page);
return page;
+ }
}
}
}
@@ -1334,6 +1338,25 @@ static void update_and_free_page(struct hstate *h, struct page *page)
free_gigantic_page(page, huge_page_order(h));
spin_lock(&hugetlb_lock);
} else {
+#ifdef CONFIG_CMA
+ /*
+ * Could be a page that was demoted from a gigantic page
+ * which was allocated in a CMA area.
+ */
+ if (HPageCma(page)) {
+ bool cma_ret;
+
+ spin_unlock(&hugetlb_lock);
+ destroy_compound_gigantic_page(page,
+ huge_page_order(h));
+ cma_ret = cma_release(hugetlb_cma[page_to_nid(page)],
+ page, 1 << huge_page_order(h));
+ spin_lock(&hugetlb_lock);
+ if (cma_ret)
+ return;
+ VM_BUG_ON_PAGE(1, page);
+ }
+#endif
__free_pages(page, huge_page_order(h));
}
}
--
2.29.2