[PATCH] hugetlb: two-phase hugepage allocation when reservation is high

Wenjie Xu posted 1 patch 7 months, 1 week ago
mm/hugetlb.c | 24 ++++++++++++++++++++----
1 file changed, 20 insertions(+), 4 deletions(-)
[PATCH] hugetlb: two-phase hugepage allocation when reservation is high
Posted by Wenjie Xu 7 months, 1 week ago
When the total reserved hugepages account for 95% or more of system RAM
(common in cloud computing on physical servers), allocating them all in one
go can starve the rest of the kernel and lead to OOM during early boot.

The previous hugetlb vmemmap batching change (91f386bf0772) can worsen
peak memory pressure under these conditions by deferring page frees,
exacerbating allocation failures. To prevent this, split the allocation
into two equal batches whenever
huge_reserved_pages >= total_base_pages * 95ULL / 100UL.

This change does not alter the number of padata worker threads per batch;
it merely introduces a second round of padata_do_multithreaded(). The added
overhead of restarting the worker threads is minimal.

Fixes: 91f386bf0772 ("hugetlb: batch freeing of vmemmap pages")

Co-developed-by: Li RongQing <lirongqing@baidu.com>
Signed-off-by: Li RongQing <lirongqing@baidu.com>
Signed-off-by: Wenjie Xu <xuwenjie04@baidu.com>
---
 mm/hugetlb.c | 24 ++++++++++++++++++++----
 1 file changed, 20 insertions(+), 4 deletions(-)

diff --git a/mm/hugetlb.c b/mm/hugetlb.c
index 6ea1be71aa42..7bdcaab6f7ec 100644
--- a/mm/hugetlb.c
+++ b/mm/hugetlb.c
@@ -3616,12 +3616,21 @@ static unsigned long __init hugetlb_pages_alloc_boot(struct hstate *h)
 		.numa_aware	= true
 	};
 
+	unsigned long huge_pages;
+	int i, hugetlb_page_alloc_iter;
+
+	unsigned long total_base_pages = totalram_pages();
+	unsigned long huge_reserved_pages = h->max_huge_pages << h->order;
+
+	hugetlb_page_alloc_iter = (huge_reserved_pages >= total_base_pages * 95ULL / 100UL)
+				 ? 2 : 1;
+
+	huge_pages = h->max_huge_pages / hugetlb_page_alloc_iter;
+
 	unsigned long jiffies_start;
 	unsigned long jiffies_end;
 
 	job.thread_fn	= hugetlb_pages_alloc_boot_node;
-	job.start	= 0;
-	job.size	= h->max_huge_pages;
 
 	/*
 	 * job.max_threads is 25% of the available cpu threads by default.
@@ -3645,10 +3654,17 @@ static unsigned long __init hugetlb_pages_alloc_boot(struct hstate *h)
 	}
 
 	job.max_threads	= hugepage_allocation_threads;
-	job.min_chunk	= h->max_huge_pages / hugepage_allocation_threads;
+	job.min_chunk	= huge_pages / hugepage_allocation_threads;
 
 	jiffies_start = jiffies;
-	padata_do_multithreaded(&job);
+	for (i = 0; i < hugetlb_page_alloc_iter; i++) {
+		job.start = huge_pages * i;
+		job.size = (i + 1 == hugetlb_page_alloc_iter)
+			 ? h->max_huge_pages - huge_pages * i
+			 : huge_pages;
+		padata_do_multithreaded(&job);
+	}
+
 	jiffies_end = jiffies;
 
 	pr_info("HugeTLB: allocation took %dms with hugepage_allocation_threads=%ld\n",
-- 
2.41.0
Re: [PATCH] hugetlb: two-phase hugepage allocation when reservation is high
Posted by kernel test robot 7 months ago
Hi Wenjie,

kernel test robot noticed the following build errors:

[auto build test ERROR on akpm-mm/mm-everything]
[also build test ERROR on linus/master v6.15-rc6 next-20250516]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Wenjie-Xu/hugetlb-two-phase-hugepage-allocation-when-reservation-is-high/20250516-193732
base:   https://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm.git mm-everything
patch link:    https://lore.kernel.org/r/20250516113552.17648-1-xuwenjie04%40baidu.com
patch subject: [PATCH] hugetlb: two-phase hugepage allocation when reservation is high
config: i386-buildonly-randconfig-006-20250517 (https://download.01.org/0day-ci/archive/20250517/202505171227.2rpT5Nx7-lkp@intel.com/config)
compiler: gcc-12 (Debian 12.2.0-14) 12.2.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20250517/202505171227.2rpT5Nx7-lkp@intel.com/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of
the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <lkp@intel.com>
| Closes: https://lore.kernel.org/oe-kbuild-all/202505171227.2rpT5Nx7-lkp@intel.com/

All errors (new ones prefixed by >>):

   ld: mm/hugetlb.o: in function `hugetlb_hstate_alloc_pages.constprop.0':
>> hugetlb.c:(.init.text+0xac2): undefined reference to `__udivdi3'

-- 
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki