Skip to contentSkip to conversation

mm,hugetlb: Allocate frozen pages in alloc_buddy_hugetlb_folio

PATCH Applied Oscar Salvador 4 participants 6 messages Apr 11, 2025 – Apr 13, 2025

Subsystems (2)
lore
Share & tools
lore

Oscar Salvador

author maintainer
#0 lore raw

PATCH 1/1 mm,hugetlb: Allocate frozen pages in alloc_buddy_hugetlb_folio

alloc_buddy_hugetlb_folio() allocates a rmappable folio, then strips the rmappable part and freezes it.
We can simplify all that by allocating frozen pages directly.

---
 mm/hugetlb.c | 17 +----------------
 1 file changed, 1 insertion(+), 16 deletions(-)
mm/hugetlb.c +1 -16
index e3e6ac991b9c..83fa2b9f6fc4 100644
@@ -1950,7 +1950,6 @@ static struct folio *alloc_buddy_hugetlb_folio(struct hstate *h,
 	int order = huge_page_order(h);
 	struct folio *folio;
 	bool alloc_try_hard = true;
-	bool retry = true;
 
 	/*
 	 * By default we always try hard to allocate the folio with
@@ -1965,22 +1964,8 @@ static struct folio *alloc_buddy_hugetlb_folio(struct hstate *h,
 		gfp_mask |= __GFP_RETRY_MAYFAIL;
 	if (nid == NUMA_NO_NODE)
 		nid = numa_mem_id();
-retry:
-	folio = __folio_alloc(gfp_mask, order, nid, nmask);
-	/* Ensure hugetlb folio won't have large_rmappable flag set. */
-	if (folio)
-		folio_clear_large_rmappable(folio);
 
-	if (folio && !folio_ref_freeze(folio, 1)) {
-		folio_put(folio);
-		if (retry) {	/* retry once */
-			retry = false;
-			goto retry;
-		}
-		/* WOW!  twice in a row. */
-		pr_warn("HugeTLB unexpected inflated folio ref count\n");
-		folio = NULL;
-	}
+	folio = (struct folio *)__alloc_frozen_pages(gfp_mask, order, nid, nmask);
 
 	/*
 	 * If we did not specify __GFP_RETRY_MAYFAIL, but still got a
Signature · 1 line
2.49.0
#2 lore raw
In reply to Oscar Salvador
Quoted 36 lines · Oscar Salvador

alloc_buddy_hugetlb_folio() allocates a rmappable folio, then strips the rmappable part and freezes it.
We can simplify all that by allocating frozen pages directly.

---
  mm/hugetlb.c | 17 +----------------
  1 file changed, 1 insertion(+), 16 deletions(-)
mm/hugetlb.c +0 -10
index e3e6ac991b9c..83fa2b9f6fc4 100644
@@ -1950,7 +1950,6 @@ static struct folio *alloc_buddy_hugetlb_folio(struct hstate *h,
  	int order = huge_page_order(h);
  	struct folio *folio;
  	bool alloc_try_hard = true;
-	bool retry = true;
 
  	/*
  	 * By default we always try hard to allocate the folio with
@@ -1965,22 +1964,8 @@ static struct folio *alloc_buddy_hugetlb_folio(struct hstate *h,
  		gfp_mask |= __GFP_RETRY_MAYFAIL;
  	if (nid == NUMA_NO_NODE)
  		nid = numa_mem_id();
-retry:
-	folio = __folio_alloc(gfp_mask, order, nid, nmask);
-	/* Ensure hugetlb folio won't have large_rmappable flag set. */
-	if (folio)
-		folio_clear_large_rmappable(folio);
 
-	if (folio && !folio_ref_freeze(folio, 1)) {
-		folio_put(folio);
-		if (retry) {	/* retry once */
-			retry = false;

mm/hugetlb.c static struct folio *alloc_buddy_hugetlb_folio(struct hstate *h, · line 1971

Quoted 10 lines
mm/hugetlb.c +1 -6
-			goto retry;
-		}
-		/* WOW!  twice in a row. */
-		pr_warn("HugeTLB unexpected inflated folio ref count\n");
-		folio = NULL;
-	}
+	folio = (struct folio *)__alloc_frozen_pages(gfp_mask, order, nid, nmask);
 
  	/*
  	 * If we did not specify __GFP_RETRY_MAYFAIL, but still got a

I assume htlb_alloc_mask() will always include _GFP_COMP.

But semantically, it might be wrong: __folio_alloc() will in the memdesc world also make sure to allocate the memdesc, __alloc_frozen_pages() not.

Maybe one would want a __alloc_frozen_folio() .... @willy?

Signature · 3 lines
Cheers,

David / dhildenb
Reviewed-by
#3 lore raw
Quoted 1 line · David Hildenbrand

I assume htlb_alloc_mask() will always include _GFP_COMP.

static inline gfp_t htlb_alloc_mask(struct hstate *h)

{
        gfp_t gfp = __GFP_COMP | __GFP_NOWARN;
Quoted 4 lines

But semantically, it might be wrong: __folio_alloc() will in the memdesc world also make sure to allocate the memdesc, __alloc_frozen_pages() not.

Maybe one would want a __alloc_frozen_folio() .... @willy?

This is fine. Yes, it'll need to be modified when we get to the separately allocated memdesc, but there's a number of places that cast the freshly allocated page to a folio, and I'll have to come up with a way to catch them all.

Oscar, if you want to take on the gigantic allocation next ...

- I don't think we need folio_alloc_gigantic() to be wrapped in alloc_hooks
- folio_alloc_gigantic() should return a frozen folio
- as should hugetlb_cma_alloc_folio()

Reviewed-by
#4 lore raw
Quoted 7 lines · Matthew Wilcox
Quoted 1 line · David Hildenbrand

I assume htlb_alloc_mask() will always include _GFP_COMP.

static inline gfp_t htlb_alloc_mask(struct hstate *h)

{
         gfp_t gfp = __GFP_COMP | __GFP_NOWARN;

I checked that as well and actually meant to say the I assume that we will here always have __GFP_COMP because the gfp_mask we get here will always be derived from htlb_alloc_mask().

Quoted 9 lines
Quoted 4 lines

But semantically, it might be wrong: __folio_alloc() will in the memdesc world also make sure to allocate the memdesc, __alloc_frozen_pages() not.

Maybe one would want a __alloc_frozen_folio() .... @willy?

This is fine. Yes, it'll need to be modified when we get to the separately allocated memdesc, but there's a number of places that cast the freshly allocated page to a folio, and I'll have to come up with a way to catch them all.

Okay, thanks for clarifying.

Signature · 3 lines
Cheers,

David / dhildenb
author maintainer
#5 lore raw
In reply to Matthew Wilcox
Quoted 6 lines · Matthew Wilcox

Oscar, if you want to take on the gigantic allocation next ...

- I don't think we need folio_alloc_gigantic() to be wrapped in alloc_hooks
- folio_alloc_gigantic() should return a frozen folio
- as should hugetlb_cma_alloc_folio()

Yeah, I was taking a look at that as well.
I plan to tackle it in a few days.

Signature · 2 lines
Oscar Salvador
SUSE Labs