Skip to contentSkip to conversation

hugetlb: Convert alloc_buddy_hugetlb_folio to use a folio

PATCH Applied Matthew Wilcox (Oracle) 4 participants 9 messages Apr 2, 2024 – Apr 7, 2024

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

PATCH 1/1 hugetlb: Convert alloc_buddy_hugetlb_folio to use a folio

While this function returned a folio, it was still using __alloc_pages() and __free_pages(). Use __folio_alloc() and put_folio() instead. This actually removes a call to compound_head(), but more importantly, it prepares us for the move to memdescs.

---
 mm/hugetlb.c | 33 ++++++++++++++++-----------------
 1 file changed, 16 insertions(+), 17 deletions(-)
mm/hugetlb.c +16 -17
index a3bffa8debde..5f1e0b1a0d57 100644
@@ -2177,13 +2177,13 @@ static struct folio *alloc_buddy_hugetlb_folio(struct hstate *h,
 		nodemask_t *node_alloc_noretry)
 {
 	int order = huge_page_order(h);
-	struct page *page;
+	struct folio *folio;
 	bool alloc_try_hard = true;
 	bool retry = true;
 
 	/*
-	 * By default we always try hard to allocate the page with
-	 * __GFP_RETRY_MAYFAIL flag.  However, if we are allocating pages in
+	 * By default we always try hard to allocate the folio with
+	 * __GFP_RETRY_MAYFAIL flag.  However, if we are allocating folios in
 	 * a loop (to adjust global huge page counts) and previous allocation
 	 * failed, do not continue to try hard on the same node.  Use the
 	 * node_alloc_noretry bitmap to manage this state information.
@@ -2196,43 +2196,42 @@ static struct folio *alloc_buddy_hugetlb_folio(struct hstate *h,
 	if (nid == NUMA_NO_NODE)
 		nid = numa_mem_id();
 retry:
-	page = __alloc_pages(gfp_mask, order, nid, nmask);
+	folio = __folio_alloc(gfp_mask, order, nid, nmask);
 
-	/* Freeze head page */
-	if (page && !page_ref_freeze(page, 1)) {
-		__free_pages(page, order);
+	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 head page unexpected inflated ref count\n");
-		page = NULL;
+		pr_warn("HugeTLB unexpected inflated folio ref count\n");
+		folio = NULL;
 	}
 
 	/*
-	 * If we did not specify __GFP_RETRY_MAYFAIL, but still got a page this
-	 * indicates an overall state change.  Clear bit so that we resume
-	 * normal 'try hard' allocations.
+	 * If we did not specify __GFP_RETRY_MAYFAIL, but still got a
+	 * folio this indicates an overall state change.  Clear bit so
+	 * that we resume normal 'try hard' allocations.
 	 */
-	if (node_alloc_noretry && page && !alloc_try_hard)
+	if (node_alloc_noretry && folio && !alloc_try_hard)
 		node_clear(nid, *node_alloc_noretry);
 
 	/*
-	 * If we tried hard to get a page but failed, set bit so that
+	 * If we tried hard to get a folio but failed, set bit so that
 	 * subsequent attempts will not try as hard until there is an
 	 * overall state change.
 	 */
-	if (node_alloc_noretry && !page && alloc_try_hard)
+	if (node_alloc_noretry && !folio && alloc_try_hard)
 		node_set(nid, *node_alloc_noretry);
 
-	if (!page) {
+	if (!folio) {
 		__count_vm_event(HTLB_BUDDY_PGALLOC_FAIL);
 		return NULL;
 	}
 
 	__count_vm_event(HTLB_BUDDY_PGALLOC);
-	return page_folio(page);
+	return folio;
 }
 
 static struct folio *__alloc_fresh_hugetlb_folio(struct hstate *h,
Signature · 1 line
2.43.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 78 lines · Matthew Wilcox (Oracle)

While this function returned a folio, it was still using __alloc_pages() and __free_pages(). Use __folio_alloc() and put_folio() instead. This actually removes a call to compound_head(), but more importantly, it prepares us for the move to memdescs.

---
  mm/hugetlb.c | 33 ++++++++++++++++-----------------
  1 file changed, 16 insertions(+), 17 deletions(-)
mm/hugetlb.c +15 -16
index a3bffa8debde..5f1e0b1a0d57 100644
@@ -2177,13 +2177,13 @@ static struct folio *alloc_buddy_hugetlb_folio(struct hstate *h,
  		nodemask_t *node_alloc_noretry)
  {
  	int order = huge_page_order(h);
-	struct page *page;
+	struct folio *folio;
  	bool alloc_try_hard = true;
  	bool retry = true;
 
  	/*
-	 * By default we always try hard to allocate the page with
-	 * __GFP_RETRY_MAYFAIL flag.  However, if we are allocating pages in
+	 * By default we always try hard to allocate the folio with
+	 * __GFP_RETRY_MAYFAIL flag.  However, if we are allocating folios in
  	 * a loop (to adjust global huge page counts) and previous allocation
  	 * failed, do not continue to try hard on the same node.  Use the
  	 * node_alloc_noretry bitmap to manage this state information.
@@ -2196,43 +2196,42 @@ static struct folio *alloc_buddy_hugetlb_folio(struct hstate *h,
  	if (nid == NUMA_NO_NODE)
  		nid = numa_mem_id();
  retry:
-	page = __alloc_pages(gfp_mask, order, nid, nmask);
+	folio = __folio_alloc(gfp_mask, order, nid, nmask);
 
-	/* Freeze head page */
-	if (page && !page_ref_freeze(page, 1)) {
-		__free_pages(page, order);
+	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 head page unexpected inflated ref count\n");
-		page = NULL;
+		pr_warn("HugeTLB unexpected inflated folio ref count\n");
+		folio = NULL;
  	}
 
  	/*
-	 * If we did not specify __GFP_RETRY_MAYFAIL, but still got a page this
-	 * indicates an overall state change.  Clear bit so that we resume
-	 * normal 'try hard' allocations.
+	 * If we did not specify __GFP_RETRY_MAYFAIL, but still got a
+	 * folio this indicates an overall state change.  Clear bit so
+	 * that we resume normal 'try hard' allocations.
  	 */
-	if (node_alloc_noretry && page && !alloc_try_hard)
+	if (node_alloc_noretry && folio && !alloc_try_hard)
  		node_clear(nid, *node_alloc_noretry);
 
  	/*
-	 * If we tried hard to get a page but failed, set bit so that
+	 * If we tried hard to get a folio but failed, set bit so that
  	 * subsequent attempts will not try as hard until there is an
  	 * overall state change.
  	 */
-	if (node_alloc_noretry && !page && alloc_try_hard)
+	if (node_alloc_noretry && !folio && alloc_try_hard)
  		node_set(nid, *node_alloc_noretry);
 
-	if (!page) {
+	if (!folio) {

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

Quoted 10 lines
mm/hugetlb.c +1 -1
  		__count_vm_event(HTLB_BUDDY_PGALLOC_FAIL);
  		return NULL;
  	}
 
  	__count_vm_event(HTLB_BUDDY_PGALLOC);
-	return page_folio(page);
+	return folio;
  }
 
  static struct folio *__alloc_fresh_hugetlb_folio(struct hstate *h,
maintainer Reviewed-by on v1 1/1
#2 lore raw
In reply to Matthew Wilcox (Oracle)
Quoted 6 lines · Matthew Wilcox (Oracle)

While this function returned a folio, it was still using __alloc_pages() and __free_pages(). Use __folio_alloc() and put_folio() instead. This actually removes a call to compound_head(), but more importantly, it prepares us for the move to memdescs.

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

Quoted 8 lines
mm/hugetlb.c +3 -4
-	page = __alloc_pages(gfp_mask, order, nid, nmask);
+	folio = __folio_alloc(gfp_mask, order, nid, nmask);
 
-	/* Freeze head page */
-	if (page && !page_ref_freeze(page, 1)) {
-		__free_pages(page, order);
+	if (folio && !folio_ref_freeze(folio, 1)) {
+		folio_put(folio);

This made me look again at the problem we had in the past with speculative refcount vs hugetlb pages, and made me think whether there are any more users trying to defeat speculative refcounts this way. It was discussed some time ago that maybe all pages returned from the buddy allocator should have its refcount frozen, to avoid this.

Signature · 2 lines
Oscar Salvador
SUSE Labs
maintainer
#3 lore raw
Quoted 23 lines · Oscar Salvador
Quoted 6 lines · Matthew Wilcox (Oracle)

While this function returned a folio, it was still using __alloc_pages() and __free_pages(). Use __folio_alloc() and put_folio() instead. This actually removes a call to compound_head(), but more importantly, it prepares us for the move to memdescs.

Quoted 8 lines

- page = __alloc_pages(gfp_mask, order, nid, nmask);
+ folio = __folio_alloc(gfp_mask, order, nid, nmask);

- /* Freeze head page */
- if (page && !page_ref_freeze(page, 1)) {
- __free_pages(page, order);
+ if (folio && !folio_ref_freeze(folio, 1)) {
+ folio_put(folio);

Quoted 5 lines

This made me look again at the problem we had in the past with speculative refcount vs hugetlb pages, and made me think whether there are any more users trying to defeat speculative refcounts this way. It was discussed some time ago that maybe all pages returned from the buddy allocator should have its refcount frozen, to avoid this.

I think you mean this patch [1], right? With alloc_frozen_pages() introduced, we could get rid of the trick from HugeTLB code.

[1] https://lore.kernel.org/linux-mm/[email protected]/T/#m20457752e757cdafc99c7f5e6a3d8cbbb65fcd3e

Quoted 5 lines
Signature · 3 lines
-- 
Oscar Salvador
SUSE Labs
maintainer
#4 lore raw
Quoted 2 lines · Muchun Song

I think you mean this patch [1], right? With alloc_frozen_pages() introduced, we could get rid of the trick from HugeTLB code.

Ah yes, that one, thanks.
It would be nice, but having read the discussion I am kind of skeptical.

But maybe some to revisit.

Signature · 2 lines
Oscar Salvador
SUSE Labs
#5 lore raw
Quoted 8 lines · Oscar Salvador
Quoted 2 lines · Muchun Song

I think you mean this patch [1], right? With alloc_frozen_pages() introduced, we could get rid of the trick from HugeTLB code.

Ah yes, that one, thanks.
It would be nice, but having read the discussion I am kind of skeptical.

But maybe some to revisit.

I haven't given up on it. It's just currently parked, awaiting more cleanups, some of which I have scheduled for the next merge window. Part of the memdesc project will involve not having refcounts for some memdescs. Slab, percpu and pagetable don't need them, for example.

I think hugetlb is being unnecessarily paranoid here, tbh. Or maybe this part is just badly structured; if we're allocating a hugetlb folio, it should be fine for its refcount to be temporarily elevated by someone else. Not sure I can figure out what's going on in alloc_and_dissolve_hugetlb_folio() though.

maintainer
#6 lore raw
Quoted 5 lines · Matthew Wilcox

I think hugetlb is being unnecessarily paranoid here, tbh. Or maybe this part is just badly structured; if we're allocating a hugetlb folio, it should be fine for its refcount to be temporarily elevated by someone else. Not sure I can figure out what's going on in alloc_and_dissolve_hugetlb_folio() though.

AFAICR, the problem comes when we need to remap the pages for vmemmap optimization [1].
So, IIUC:

1) if someone comes around and grabs a refcount (say something doing speculative stuff)
2) we do the remapping
3) that someone who took the refcount, now does a put_page()
4) vmemmap no longer points to the old page but the new one, meaning that that 'put_page()' is done on the wrong page.

@Munchun: Did I get this right?

[1] https://lore.kernel.org/linux-mm/YupRjWRiz4lPo+y7@FVFYT0MHHV2J/

Signature · 2 lines
Oscar Salvador
SUSE Labs
maintainer
#7 lore raw
Quoted 15 lines · Oscar Salvador
Quoted 5 lines · Matthew Wilcox

I think hugetlb is being unnecessarily paranoid here, tbh. Or maybe this part is just badly structured; if we're allocating a hugetlb folio, it should be fine for its refcount to be temporarily elevated by someone else. Not sure I can figure out what's going on in alloc_and_dissolve_hugetlb_folio() though.

AFAICR, the problem comes when we need to remap the pages for vmemmap optimization [1].
So, IIUC:

Quoted 8 lines

1) if someone comes around and grabs a refcount (say something doing speculative stuff)
2) we do the remapping
3) that someone who took the refcount, now does a put_page()
4) vmemmap no longer points to the old page but the new one, meaning that that 'put_page()' is done on the wrong page.

@Munchun: Did I get this right?

Right. But let me clarify this again. We need to keep the content of the physical page constant throughout the processing of HVO (i.g. between the copying of head vmemmap page and remapping of it). Zero-referenced page could prevent others from updating the content of the page structs.

Quoted 2 lines

[1] https://lore.kernel.org/linux-mm/YupRjWRiz4lPo+y7@FVFYT0MHHV2J/

Yes, I've also pointed it out here.

Thanks.

Quoted 5 lines
Signature · 3 lines
-- 
Oscar Salvador
SUSE Labs
maintainer Reviewed-by on v1 1/1
#8 lore raw
In reply to Matthew Wilcox (Oracle)
Quoted 10 lines · Matthew Wilcox (Oracle)

While this function returned a folio, it was still using __alloc_pages() and __free_pages(). Use __folio_alloc() and put_folio() instead. This actually removes a call to compound_head(), but more importantly, it prepares us for the move to memdescs.

Thanks.