Skip to contentSkip to conversation

mm/hugetlb: wait for hugetlb folios to be freed

PATCH Superseded [email protected] 2 participants 5 messages Feb 18, 2025 – Feb 19, 2025

Subsystems (4)

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 mm/hugetlb: wait for hugetlb folios to be freed

From: Ge Yang <[email protected]>

Since the introduction of commit c77c0a8ac4c52 ("mm/hugetlb: defer freeing of huge pages if in non-task context"), which supports deferring the freeing of hugetlb pages, the allocation of contiguous memory through cma_alloc() may fail probabilistically.

In the CMA allocation process, if it is found that the CMA area is occupied by in-use hugetlb folios, these in-use hugetlb folios need to be migrated to another location. When there are no available hugetlb folios in the free hugetlb pool during the migration of in-use hugetlb folios, new folios are allocated from the buddy system. A temporary state is set on the newly allocated folio. Upon completion of the hugetlb folio migration, the temporary state is transferred from the new folios to the old folios. Normally, when the old folios with the temporary state are freed, it is directly released back to the buddy system. However, due to the deferred freeing of hugetlb pages, the PageBuddy() check fails, ultimately leading to the failure of cma_alloc().

Here is a simplified call trace illustrating the process:
cma_alloc()

    ->__alloc_contig_migrate_range() // Migrate in-use hugetlb folios
        ->unmap_and_move_huge_page()
            ->folio_putback_hugetlb() // Free old folios
    ->test_pages_isolated()
        ->__test_page_isolated_in_pageblock()
             ->PageBuddy(page) // Check if the page is in buddy

To resolve this issue, we have implemented a function named wait_for_freed_hugetlb_folios(). This function ensures that the hugetlb folios are properly released back to the buddy system after their migration is completed. By invoking wait_for_freed_hugetlb_folios() before calling PageBuddy(), we ensure that PageBuddy() will succeed.

Fixes c77c0a8ac4c52 ("mm/hugetlb: defer freeing of huge pages if in non-task context")

---

V3:
- adjust code and message suggested by Muchun and David

V2:
- flush all folios at once suggested by David

 include/linux/hugetlb.h |  5 +++++
 mm/hugetlb.c            |  5 +++++
 mm/page_isolation.c     | 10 ++++++++++
 3 files changed, 20 insertions(+)
include/linux/hugetlb.h +5 -0
index 6c6546b..0c54b3a 100644
@@ -697,6 +697,7 @@ bool hugetlb_bootmem_page_zones_valid(int nid, struct huge_bootmem_page *m);
 
 int isolate_or_dissolve_huge_page(struct page *page, struct list_head *list);
 int replace_free_hugepage_folios(unsigned long start_pfn, unsigned long end_pfn);
+void wait_for_freed_hugetlb_folios(void);
 struct folio *alloc_hugetlb_folio(struct vm_area_struct *vma,
 				unsigned long addr, bool cow_from_owner);
 struct folio *alloc_hugetlb_folio_nodemask(struct hstate *h, int preferred_nid,
@@ -1092,6 +1093,10 @@ static inline int replace_free_hugepage_folios(unsigned long start_pfn,
 	return 0;
 }
 
+static inline void wait_for_freed_hugetlb_folios(void)
+{
+}
+
 static inline struct folio *alloc_hugetlb_folio(struct vm_area_struct *vma,
 					   unsigned long addr,
 					   bool cow_from_owner)
mm/hugetlb.c +5 -0
index 30bc34d..b4630b3 100644
@@ -2955,6 +2955,11 @@ int replace_free_hugepage_folios(unsigned long start_pfn, unsigned long end_pfn)
 	return ret;
 }
 
+void wait_for_freed_hugetlb_folios(void)
+{
+	flush_work(&free_hpage_work);
+}
+
 typedef enum {
 	/*
 	 * For either 0/1: we checked the per-vma resv map, and one resv
mm/page_isolation.c +10 -0
index 8ed53ee0..b2fc526 100644
@@ -615,6 +615,16 @@ int test_pages_isolated(unsigned long start_pfn, unsigned long end_pfn,
 	int ret;
 
 	/*
+	 * Due to the deferred freeing of hugetlb folios, the hugepage folios may
+	 * not immediately release to the buddy system. This can cause PageBuddy()
+	 * to fail in __test_page_isolated_in_pageblock(). To ensure that the
+	 * hugetlb folios are properly released back to the buddy system, we
+	 * invoke the wait_for_freed_hugetlb_folios() function to wait for the
+	 * release to complete.
+	 */
+	wait_for_freed_hugetlb_folios();
+
+	/*
 	 * Note: pageblock_nr_pages != MAX_PAGE_ORDER. Then, chunks of free
 	 * pages are not aligned to pageblock_nr_pages.
 	 * Then we just check migratetype first.
Signature · 1 line
2.7.4
Acked-by on v3 1/1
#1 lore raw
Quoted 34 lines · On 18.02.25 12:40, [email protected] wrote:

From: Ge Yang <[email protected]>

Since the introduction of commit c77c0a8ac4c52 ("mm/hugetlb: defer freeing of huge pages if in non-task context"), which supports deferring the freeing of hugetlb pages, the allocation of contiguous memory through cma_alloc() may fail probabilistically.

In the CMA allocation process, if it is found that the CMA area is occupied by in-use hugetlb folios, these in-use hugetlb folios need to be migrated to another location. When there are no available hugetlb folios in the free hugetlb pool during the migration of in-use hugetlb folios, new folios are allocated from the buddy system. A temporary state is set on the newly allocated folio. Upon completion of the hugetlb folio migration, the temporary state is transferred from the new folios to the old folios. Normally, when the old folios with the temporary state are freed, it is directly released back to the buddy system. However, due to the deferred freeing of hugetlb pages, the PageBuddy() check fails, ultimately leading to the failure of cma_alloc().

Here is a simplified call trace illustrating the process:
cma_alloc()

     ->__alloc_contig_migrate_range() // Migrate in-use hugetlb folios
         ->unmap_and_move_huge_page()
             ->folio_putback_hugetlb() // Free old folios
     ->test_pages_isolated()
         ->__test_page_isolated_in_pageblock()
              ->PageBuddy(page) // Check if the page is in buddy

To resolve this issue, we have implemented a function named wait_for_freed_hugetlb_folios(). This function ensures that the hugetlb folios are properly released back to the buddy system after their migration is completed. By invoking wait_for_freed_hugetlb_folios() before calling PageBuddy(), we ensure that PageBuddy() will succeed.

Quoted 3 lines
Fixes c77c0a8ac4c52 ("mm/hugetlb: defer freeing of huge pages if in non-task context")

mm/hugetlb.c int replace_free_hugepage_folios(unsigned long start_pfn, unsigned long end_pfn) · line 2960

Quoted 4 lines
mm/hugetlb.c +3 -0
+void wait_for_freed_hugetlb_folios(void)
+{
+	flush_work(&free_hpage_work);

BTW, I was wondering if we could optimize out some calls here by sensing if there is actually work.

In most cases, we'll never ever have to actually wait here, especially on systems without any configured hugetlb pages etc ...

It's rather a corner case that we have to wait here on most systems.

Signature · 3 lines
Cheers,

David / dhildenb
author
#2 lore raw
Quoted 41 lines · David Hildenbrand
Quoted 34 lines · On 18.02.25 12:40, [email protected] wrote:

From: Ge Yang <[email protected]>

Since the introduction of commit c77c0a8ac4c52 ("mm/hugetlb: defer freeing of huge pages if in non-task context"), which supports deferring the freeing of hugetlb pages, the allocation of contiguous memory through cma_alloc() may fail probabilistically.

In the CMA allocation process, if it is found that the CMA area is occupied by in-use hugetlb folios, these in-use hugetlb folios need to be migrated to another location. When there are no available hugetlb folios in the free hugetlb pool during the migration of in-use hugetlb folios, new folios are allocated from the buddy system. A temporary state is set on the newly allocated folio. Upon completion of the hugetlb folio migration, the temporary state is transferred from the new folios to the old folios. Normally, when the old folios with the temporary state are freed, it is directly released back to the buddy system. However, due to the deferred freeing of hugetlb pages, the PageBuddy() check fails, ultimately leading to the failure of cma_alloc().

Here is a simplified call trace illustrating the process:
cma_alloc()
     ->__alloc_contig_migrate_range() // Migrate in-use hugetlb folios ->unmap_and_move_huge_page() ->folio_putback_hugetlb() // Free old folios ->test_pages_isolated() ->__test_page_isolated_in_pageblock() ->PageBuddy(page) // Check if the page is in buddy

To resolve this issue, we have implemented a function named wait_for_freed_hugetlb_folios(). This function ensures that the hugetlb folios are properly released back to the buddy system after their migration is completed. By invoking wait_for_freed_hugetlb_folios() before calling PageBuddy(), we ensure that PageBuddy() will succeed.

Quoted 3 lines
Fixes c77c0a8ac4c52 ("mm/hugetlb: defer freeing of huge pages if in non-task context")
Quoted 7 lines

mm/hugetlb.c int replace_free_hugepage_folios(unsigned long start_pfn, unsigned long end_pfn) · line 2960

Quoted 3 lines
mm/hugetlb.c +3 -0
+void wait_for_freed_hugetlb_folios(void)
+{
+    flush_work(&free_hpage_work);

BTW, I was wondering if we could optimize out some calls here by sensing if there is actually work.

for_each_hstate(h) {
	if (hugetlb_vmemmap_optimizable(h)) {
		flush_work(&free_hpage_work);
		break;
	}
}

Is this adjustment okay?

Quoted 4 lines

In most cases, we'll never ever have to actually wait here, especially on systems without any configured hugetlb pages etc ...

It's rather a corner case that we have to wait here on most systems.

#3 lore raw
Quoted 58 lines · Ge Yang
Quoted 47 lines · David Hildenbrand
Quoted 39 lines · On 18.02.25 12:40, [email protected] wrote:

From: Ge Yang <[email protected]>

Since the introduction of commit c77c0a8ac4c52 ("mm/hugetlb: defer freeing
of huge pages if in non-task context"), which supports deferring the freeing of hugetlb pages, the allocation of contiguous memory through cma_alloc() may fail probabilistically.

In the CMA allocation process, if it is found that the CMA area is occupied
by in-use hugetlb folios, these in-use hugetlb folios need to be migrated to another location. When there are no available hugetlb folios in the free hugetlb pool during the migration of in-use hugetlb folios, new folios
are allocated from the buddy system. A temporary state is set on the newly
allocated folio. Upon completion of the hugetlb folio migration, the temporary state is transferred from the new folios to the old folios. Normally, when the old folios with the temporary state are freed, it is directly released back to the buddy system. However, due to the deferred freeing of hugetlb pages, the PageBuddy() check fails, ultimately leading to the failure of cma_alloc().

Here is a simplified call trace illustrating the process:
cma_alloc()
     ->__alloc_contig_migrate_range() // Migrate in-use hugetlb folios ->unmap_and_move_huge_page() ->folio_putback_hugetlb() // Free old folios ->test_pages_isolated() ->__test_page_isolated_in_pageblock() ->PageBuddy(page) // Check if the page is in buddy

To resolve this issue, we have implemented a function named wait_for_freed_hugetlb_folios(). This function ensures that the hugetlb folios are properly released back to the buddy system after their migration
is completed. By invoking wait_for_freed_hugetlb_folios() before calling PageBuddy(), we ensure that PageBuddy() will succeed.

Quoted 4 lines
Fixes c77c0a8ac4c52 ("mm/hugetlb: defer freeing of huge pages if in

non-task context")

Quoted 8 lines

mm/hugetlb.c int replace_free_hugepage_folios(unsigned long start_pfn, unsigned long end_pfn) · line 2960

Quoted 3 lines
mm/hugetlb.c +3 -0
+void wait_for_freed_hugetlb_folios(void)
+{
+    flush_work(&free_hpage_work);

BTW, I was wondering if we could optimize out some calls here by sensing if there is actually work.

Quoted 6 lines
for_each_hstate(h) {
	if (hugetlb_vmemmap_optimizable(h)) {
		flush_work(&free_hpage_work);
		break;> 	}
}

Is this adjustment okay?

I think that's better, except that it would still trigger in scenarios where hugetlb is completely unused if CONFIG_HUGETLB_PAGE_OPTIMIZE_VMEMMAP is around.

Can't we check hpage_freelist?

if (llist_empty(&hpage_freelist))

	return;

flush_work(&free_hpage_work);

It should be able to deal with races (we don't care if something is getting added concurrently, only if there is something right now).

Signature · 3 lines
Cheers,

David / dhildenb
author
#4 lore raw
Quoted 66 lines · David Hildenbrand
Quoted 58 lines · Ge Yang
Quoted 47 lines · David Hildenbrand
Quoted 39 lines · On 18.02.25 12:40, [email protected] wrote:

From: Ge Yang <[email protected]>

Since the introduction of commit c77c0a8ac4c52 ("mm/hugetlb: defer freeing
of huge pages if in non-task context"), which supports deferring the freeing of hugetlb pages, the allocation of contiguous memory through cma_alloc() may fail probabilistically.

In the CMA allocation process, if it is found that the CMA area is occupied
by in-use hugetlb folios, these in-use hugetlb folios need to be migrated to another location. When there are no available hugetlb folios in the free hugetlb pool during the migration of in-use hugetlb folios, new folios
are allocated from the buddy system. A temporary state is set on the newly
allocated folio. Upon completion of the hugetlb folio migration, the temporary state is transferred from the new folios to the old folios. Normally, when the old folios with the temporary state are freed, it is directly released back to the buddy system. However, due to the deferred freeing of hugetlb pages, the PageBuddy() check fails, ultimately leading to the failure of cma_alloc().

Here is a simplified call trace illustrating the process:
cma_alloc()
      ->__alloc_contig_migrate_range() // Migrate in-use hugetlb folios ->unmap_and_move_huge_page() ->folio_putback_hugetlb() // Free old folios ->test_pages_isolated() ->__test_page_isolated_in_pageblock() ->PageBuddy(page) // Check if the page is in buddy

To resolve this issue, we have implemented a function named wait_for_freed_hugetlb_folios(). This function ensures that the hugetlb folios are properly released back to the buddy system after their migration
is completed. By invoking wait_for_freed_hugetlb_folios() before calling PageBuddy(), we ensure that PageBuddy() will succeed.

Quoted 4 lines
Fixes c77c0a8ac4c52 ("mm/hugetlb: defer freeing of huge pages if in

non-task context")

Quoted 8 lines

mm/hugetlb.c int replace_free_hugepage_folios(unsigned long start_pfn, unsigned long end_pfn) · line 2960

Quoted 3 lines
mm/hugetlb.c +3 -0
+void wait_for_freed_hugetlb_folios(void)
+{
+    flush_work(&free_hpage_work);

BTW, I was wondering if we could optimize out some calls here by sensing if there is actually work.

Quoted 6 lines

for_each_hstate(h) {
    if (hugetlb_vmemmap_optimizable(h)) {
        flush_work(&free_hpage_work);
         break;>     }
}
Is this adjustment okay?

Quoted 8 lines

I think that's better, except that it would still trigger in scenarios where hugetlb is completely unused if CONFIG_HUGETLB_PAGE_OPTIMIZE_VMEMMAP is around.

Can't we check hpage_freelist?

if (llist_empty(&hpage_freelist))
    return;
flush_work(&free_hpage_work);

Ok, thanks.

Quoted 2 lines

It should be able to deal with races (we don't care if something is getting added concurrently, only if there is something right now).