Skip to contentSkip to conversation

mm/hugetlb: copy the CMA flag when demoting

PATCH Applied Frank van der Linden 4 participants 5 messages May 1, 2025 – May 5, 2025

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 mm/hugetlb: copy the CMA flag when demoting

Since commit d2d786714080 ("mm/hugetlb: enable bootmem allocation from CMA areas"), a flag is used to mark hugetlb folios as allocated from CMA. This flag is also used to decide if it should be freed to CMA.

However, the flag isn't copied to the smaller folios when a hugetlb folio is broken up for demotion, which would cause it to be freed incorrectly.

Fix this by copying the flag to the smaller order hugetlb pages created from the original one.

Fixes d2d786714080 ("mm/hugetlb: enable bootmem allocation from CMA areas")
---
 mm/hugetlb.c | 6 ++++++
 1 file changed, 6 insertions(+)
mm/hugetlb.c +6 -0
index e3e6ac991b9c..6ea1be71aa42 100644
@@ -4034,10 +4034,13 @@ static long demote_free_hugetlb_folios(struct hstate *src, struct hstate *dst,
 
 	list_for_each_entry_safe(folio, next, src_list, lru) {
 		int i;
+		bool cma;
 
 		if (folio_test_hugetlb_vmemmap_optimized(folio))
 			continue;
 
+		cma = folio_test_hugetlb_cma(folio);
+
 		list_del(&folio->lru);
 
 		split_page_owner(&folio->page, huge_page_order(src), huge_page_order(dst));
@@ -4053,6 +4056,9 @@ static long demote_free_hugetlb_folios(struct hstate *src, struct hstate *dst,
 
 			new_folio->mapping = NULL;
 			init_new_hugetlb_folio(dst, new_folio);
+			/* Copy the CMA flag so that it is freed correctly */
+			if (cma)
+				folio_set_hugetlb_cma(new_folio);
 			list_add(&new_folio->lru, &dst_list);
 		}
 	}
Signature · 1 line
2.49.0.967.g6a0df3ecc3-goog
maintainer Reviewed-by on v1 1/1
#1 lore raw
Quoted 9 lines · Frank van der Linden

Since commit d2d786714080 ("mm/hugetlb: enable bootmem allocation from CMA areas"), a flag is used to mark hugetlb folios as allocated from CMA. This flag is also used to decide if it should be freed to CMA.

However, the flag isn't copied to the smaller folios when a hugetlb folio is broken up for demotion, which would cause it to be freed incorrectly.

Quoted 5 lines

Fix this by copying the flag to the smaller order hugetlb pages created from the original one.

Fixes d2d786714080 ("mm/hugetlb: enable bootmem allocation from CMA areas")

Hi Frank,

By the way, AFAICS we are missing some docs for HPG_cma flag in include/linux/hugetlb.h. Could you please add it :-P?

Thanks!

Signature · 2 lines
Oscar Salvador
SUSE Labs
Reviewed-by on v1 1/1
#2 lore raw
In reply to Frank van der Linden
Quoted 37 lines · Frank van der Linden

Since commit d2d786714080 ("mm/hugetlb: enable bootmem allocation from CMA areas"), a flag is used to mark hugetlb folios as allocated from CMA. This flag is also used to decide if it should be freed to CMA.

However, the flag isn't copied to the smaller folios when a hugetlb folio is broken up for demotion, which would cause it to be freed incorrectly.

Fix this by copying the flag to the smaller order hugetlb pages created from the original one.

Fixes d2d786714080 ("mm/hugetlb: enable bootmem allocation from CMA areas")
---
 mm/hugetlb.c | 6 ++++++
 1 file changed, 6 insertions(+)
mm/hugetlb.c +3 -0
index e3e6ac991b9c..6ea1be71aa42 100644
@@ -4034,10 +4034,13 @@ static long demote_free_hugetlb_folios(struct hstate *src, struct hstate *dst,
 
 	list_for_each_entry_safe(folio, next, src_list, lru) {
 		int i;
+		bool cma;
 
 		if (folio_test_hugetlb_vmemmap_optimized(folio))
 			continue;
 
+		cma = folio_test_hugetlb_cma(folio);
+
 		list_del(&folio->lru);
 
 		split_page_owner(&folio->page, huge_page_order(src), huge_page_order(dst));

mm/hugetlb.c static long demote_free_hugetlb_folios(struct hstate *src, struct hstate *dst, · line 4064

Quoted 9 lines
mm/hugetlb.c +3 -0
@@ -4053,6 +4056,9 @@ static long demote_free_hugetlb_folios(struct hstate *src, struct hstate *dst,
 
 			new_folio->mapping = NULL;
 			init_new_hugetlb_folio(dst, new_folio);
+			/* Copy the CMA flag so that it is freed correctly */
+			if (cma)
+				folio_set_hugetlb_cma(new_folio);
 			list_add(&new_folio->lru, &dst_list);
 		}
 	}

Agree with Oscar that HPG_cma need documentation like other flags.

#3 lore raw
Quoted 53 lines · Anshuman Khandual
Quoted 37 lines · Frank van der Linden

Since commit d2d786714080 ("mm/hugetlb: enable bootmem allocation from CMA areas"), a flag is used to mark hugetlb folios as allocated from CMA. This flag is also used to decide if it should be freed to CMA.

However, the flag isn't copied to the smaller folios when a hugetlb folio is broken up for demotion, which would cause it to be freed incorrectly.

Fix this by copying the flag to the smaller order hugetlb pages created from the original one.

Fixes d2d786714080 ("mm/hugetlb: enable bootmem allocation from CMA areas")
---
 mm/hugetlb.c | 6 ++++++
 1 file changed, 6 insertions(+)
mm/hugetlb.c +3 -0
index e3e6ac991b9c..6ea1be71aa42 100644
@@ -4034,10 +4034,13 @@ static long demote_free_hugetlb_folios(struct hstate *src, struct hstate *dst,
 
      list_for_each_entry_safe(folio, next, src_list, lru) {
              int i;
+             bool cma;
 
              if (folio_test_hugetlb_vmemmap_optimized(folio))
                      continue;
 
+             cma = folio_test_hugetlb_cma(folio);
+
              list_del(&folio->lru);
 
              split_page_owner(&folio->page, huge_page_order(src), huge_page_order(dst));

mm/hugetlb.c static long demote_free_hugetlb_folios(struct hstate *src, struct hstate *dst, · line 4064

Quoted 9 lines
mm/hugetlb.c +3 -0
@@ -4053,6 +4056,9 @@ static long demote_free_hugetlb_folios(struct hstate *src, struct hstate *dst,
 
                      new_folio->mapping = NULL;
                      init_new_hugetlb_folio(dst, new_folio);
+                     /* Copy the CMA flag so that it is freed correctly */
+                     if (cma)
+                             folio_set_hugetlb_cma(new_folio);
                      list_add(&new_folio->lru, &dst_list);
              }
      }

Agree with Oscar that HPG_cma need documentation like other flags.

Quoted 1 line

Thanks for the reviews! I'll send a patch to document the flag in the near future.

- Frank

Reviewed-by on v1 1/1
#4 lore raw
In reply to Frank van der Linden
Quoted 37 lines · Frank van der Linden

Since commit d2d786714080 ("mm/hugetlb: enable bootmem allocation from CMA areas"), a flag is used to mark hugetlb folios as allocated from CMA. This flag is also used to decide if it should be freed to CMA.

However, the flag isn't copied to the smaller folios when a hugetlb folio is broken up for demotion, which would cause it to be freed incorrectly.

Fix this by copying the flag to the smaller order hugetlb pages created from the original one.

Fixes d2d786714080 ("mm/hugetlb: enable bootmem allocation from CMA areas")
---
  mm/hugetlb.c | 6 ++++++
  1 file changed, 6 insertions(+)
mm/hugetlb.c +3 -0
index e3e6ac991b9c..6ea1be71aa42 100644
@@ -4034,10 +4034,13 @@ static long demote_free_hugetlb_folios(struct hstate *src, struct hstate *dst,
 
  	list_for_each_entry_safe(folio, next, src_list, lru) {
  		int i;
+		bool cma;
 
  		if (folio_test_hugetlb_vmemmap_optimized(folio))
  			continue;
 
+		cma = folio_test_hugetlb_cma(folio);
+
  		list_del(&folio->lru);
 
  		split_page_owner(&folio->page, huge_page_order(src), huge_page_order(dst));

mm/hugetlb.c static long demote_free_hugetlb_folios(struct hstate *src, struct hstate *dst, · line 4064

Quoted 9 lines
mm/hugetlb.c +3 -0
@@ -4053,6 +4056,9 @@ static long demote_free_hugetlb_folios(struct hstate *src, struct hstate *dst,
 
  			new_folio->mapping = NULL;
  			init_new_hugetlb_folio(dst, new_folio);
+			/* Copy the CMA flag so that it is freed correctly */
+			if (cma)
+				folio_set_hugetlb_cma(new_folio);
  			list_add(&new_folio->lru, &dst_list);
  		}
  	}

Looks good.

thanks,
-jane