[PATCH] mm/hugetlb: avoid hardcoding while checking if cma is reserved

From: Barry Song
Date: Mon Jul 06 2020 - 04:46:09 EST


hugetlb_cma[0] can be NULL due to various reasons, for example, node0 has
no memory. Thus, NULL hugetlb_cma[0] doesn't necessarily mean cma is not
enabled. gigantic pages might have been reserved on other nodes.

Fixes: cf11e85fc08c ("mm: hugetlb: optionally allocate gigantic hugepages using cma")
Cc: Roman Gushchin <guro@xxxxxx>
Cc: Mike Kravetz <mike.kravetz@xxxxxxxxxx>
Cc: Jonathan Cameron <jonathan.cameron@xxxxxxxxxx>
Signed-off-by: Barry Song <song.bao.hua@xxxxxxxxxxxxx>
---
mm/hugetlb.c | 18 +++++++++++++++---
1 file changed, 15 insertions(+), 3 deletions(-)

diff --git a/mm/hugetlb.c b/mm/hugetlb.c
index 57ece74e3aae..603aa854aa89 100644
--- a/mm/hugetlb.c
+++ b/mm/hugetlb.c
@@ -2571,9 +2571,21 @@ static void __init hugetlb_hstate_alloc_pages(struct hstate *h)

for (i = 0; i < h->max_huge_pages; ++i) {
if (hstate_is_gigantic(h)) {
- if (IS_ENABLED(CONFIG_CMA) && hugetlb_cma[0]) {
- pr_warn_once("HugeTLB: hugetlb_cma is enabled, skip boot time allocation\n");
- break;
+ if (IS_ENABLED(CONFIG_CMA)) {
+ int nid;
+ bool cma_reserved = false;
+
+ for_each_node_state(nid, N_ONLINE) {
+ if (hugetlb_cma[nid]) {
+ pr_warn_once("HugeTLB: hugetlb_cma is reserved,"
+ "skip boot time allocation\n");
+ cma_reserved = true;
+ break;
+ }
+ }
+
+ if (cma_reserved)
+ break;
}
if (!alloc_bootmem_huge_page(h))
break;
--
2.27.0