Skip to contentSkip to conversation

mm: hugetlb: improve the handling of hugetlb allocation failure for freed or in-use hugetlb

PATCH Superseded Baolin Wang 2 participants 3 messages Feb 5, 2024 – Feb 6, 2024

Subsystems (2)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 mm: hugetlb: improve the handling of hugetlb allocation failure for freed or in-use hugetlb

When handling the freed hugetlb or in-use hugetlb, we should ignore the failure of alloc_buddy_hugetlb_folio() to dissolve the old hugetlb successfully, since we did not use the new allocated hugetlb in this 2 cases. Moreover, moving the allocation into the free hugetlb handling branch.

---
Changes from v1:
- Update the suject line per Muchun.
- Move the allocation into the free hugetlb handling branch per Michal.

---
 mm/hugetlb.c | 40 ++++++++++++++++++++++++----------------
 1 file changed, 24 insertions(+), 16 deletions(-)
mm/hugetlb.c +24 -16
index 9d996fe4ecd9..4899167d3652 100644
@@ -3031,21 +3031,9 @@ static int alloc_and_dissolve_hugetlb_folio(struct hstate *h,
 {
 	gfp_t gfp_mask = htlb_alloc_mask(h) | __GFP_THISNODE;
 	int nid = folio_nid(old_folio);
-	struct folio *new_folio;
+	struct folio *new_folio = NULL;
 	int ret = 0;
 
-	/*
-	 * Before dissolving the folio, we need to allocate a new one for the
-	 * pool to remain stable.  Here, we allocate the folio and 'prep' it
-	 * by doing everything but actually updating counters and adding to
-	 * the pool.  This simplifies and let us do most of the processing
-	 * under the lock.
-	 */
-	new_folio = alloc_buddy_hugetlb_folio(h, gfp_mask, nid, NULL, NULL);
-	if (!new_folio)
-		return -ENOMEM;
-	__prep_new_hugetlb_folio(h, new_folio);
-
 retry:
 	spin_lock_irq(&hugetlb_lock);
 	if (!folio_test_hugetlb(old_folio)) {
@@ -3075,6 +3063,24 @@ static int alloc_and_dissolve_hugetlb_folio(struct hstate *h,
 		cond_resched();
 		goto retry;
 	} else {
+		if (!new_folio) {
+			spin_unlock_irq(&hugetlb_lock);
+			/*
+			 * Before dissolving the free hugetlb, we need to allocate
+			 * a new one for the pool to remain stable.  Here, we
+			 * allocate the folio and 'prep' it by doing everything
+			 * but actually updating counters and adding to the pool.
+			 * This simplifies and let us do most of the processing
+			 * under the lock.
+			 */
+			new_folio = alloc_buddy_hugetlb_folio(h, gfp_mask, nid,
+							      NULL, NULL);
+			if (!new_folio)
+				return -ENOMEM;
+			__prep_new_hugetlb_folio(h, new_folio);
+			goto retry;
+		}
+
 		/*
 		 * Ok, old_folio is still a genuine free hugepage. Remove it from
 		 * the freelist and decrease the counters. These will be
@@ -3102,9 +3108,11 @@ static int alloc_and_dissolve_hugetlb_folio(struct hstate *h,
 
 free_new:
 	spin_unlock_irq(&hugetlb_lock);
-	/* Folio has a zero ref count, but needs a ref to be freed */
-	folio_ref_unfreeze(new_folio, 1);
-	update_and_free_hugetlb_folio(h, new_folio, false);
+	if (new_folio) {
+		/* Folio has a zero ref count, but needs a ref to be freed */
+		folio_ref_unfreeze(new_folio, 1);
+		update_and_free_hugetlb_folio(h, new_folio, false);
+	}
 
 	return ret;
 }
Signature · 1 line
2.39.3
#1 lore raw
Quoted 4 lines · Baolin Wang

When handling the freed hugetlb or in-use hugetlb, we should ignore the failure of alloc_buddy_hugetlb_folio() to dissolve the old hugetlb successfully, since we did not use the new allocated hugetlb in this 2 cases. Moreover, moving the allocation into the free hugetlb handling branch.

The changelog is a bit hard for me to understand. What about the following instead?
alloc_and_dissolve_hugetlb_folio preallocates a new huge page before it takes hugetlb_lock. In 3 out of 4 cases the page is not really used and therefore the newly allocated page is just freed right away. This is wasteful and it might cause pre-mature failures in those cases.

Address that by moving the allocation down to the only case (hugetlb page is really in the free pages pool). We need to drop hugetlb_lock to do so and therefore need to recheck the page state after regaining it.

The patch is more of a cleanup than an actual fix to an existing problem. There are no known reports about pre-mature failures.

[...]

Quoted 4 lines
mm/hugetlb.c +0 -0
@@ -3075,6 +3063,24 @@ static int alloc_and_dissolve_hugetlb_folio(struct hstate *h,
 		cond_resched();
 		goto retry;
 	} else {

mm/hugetlb.c static int alloc_and_dissolve_hugetlb_folio(struct hstate *h, · line 3075

Quoted 10 lines
mm/hugetlb.c +10 -0
+		if (!new_folio) {
+			spin_unlock_irq(&hugetlb_lock);
+			/*
+			 * Before dissolving the free hugetlb, we need to allocate
+			 * a new one for the pool to remain stable.  Here, we
+			 * allocate the folio and 'prep' it by doing everything
+			 * but actually updating counters and adding to the pool.
+			 * This simplifies and let us do most of the processing
+			 * under the lock.
+			 */

This comment is not really needed anymore IMHO.

Quoted 11 lines
mm/hugetlb.c +8 -0
+			new_folio = alloc_buddy_hugetlb_folio(h, gfp_mask, nid,
+							      NULL, NULL);
+			if (!new_folio)
+				return -ENOMEM;
+			__prep_new_hugetlb_folio(h, new_folio);
+			goto retry;
+		}
+
 		/*
 		 * Ok, old_folio is still a genuine free hugepage. Remove it from
 		 * the freelist and decrease the counters. These will be
Signature · 2 lines
Michal Hocko
SUSE Labs
#2 lore raw
Quoted 13 lines · Michal Hocko
Quoted 4 lines · Baolin Wang

When handling the freed hugetlb or in-use hugetlb, we should ignore the failure of alloc_buddy_hugetlb_folio() to dissolve the old hugetlb successfully, since we did not use the new allocated hugetlb in this 2 cases. Moreover, moving the allocation into the free hugetlb handling branch.

The changelog is a bit hard for me to understand. What about the following instead?
alloc_and_dissolve_hugetlb_folio preallocates a new huge page before it takes hugetlb_lock. In 3 out of 4 cases the page is not really used and therefore the newly allocated page is just freed right away. This is wasteful and it might cause pre-mature failures in those cases.

Quoted 7 lines

Address that by moving the allocation down to the only case (hugetlb page is really in the free pages pool). We need to drop hugetlb_lock to do so and therefore need to recheck the page state after regaining it.

The patch is more of a cleanup than an actual fix to an existing problem. There are no known reports about pre-mature failures.

Looks better. Thanks.

Quoted 8 lines
Quoted 8 lines
mm/hugetlb.c +4 -0
@@ -3075,6 +3063,24 @@ static int alloc_and_dissolve_hugetlb_folio(struct hstate *h,
  		cond_resched();
  		goto retry;
  	} else {
+		if (!new_folio) {
+			spin_unlock_irq(&hugetlb_lock);
+			/*
+			 * Before dissolving the free hugetlb, we need to allocate
Quoted 8 lines

mm/hugetlb.c static int alloc_and_dissolve_hugetlb_folio(struct hstate *h, · line 3075

Quoted 6 lines
mm/hugetlb.c +6 -0
+			 * a new one for the pool to remain stable.  Here, we
+			 * allocate the folio and 'prep' it by doing everything
+			 * but actually updating counters and adding to the pool.
+			 * This simplifies and let us do most of the processing
+			 * under the lock.
+			 */

This comment is not really needed anymore IMHO.

Acked.

Quoted 13 lines
Quoted 12 lines
mm/hugetlb.c +8 -0
+			new_folio = alloc_buddy_hugetlb_folio(h, gfp_mask, nid,
+							      NULL, NULL);
+			if (!new_folio)
+				return -ENOMEM;
+			__prep_new_hugetlb_folio(h, new_folio);
+			goto retry;
+		}
+
  		/*
  		 * Ok, old_folio is still a genuine free hugepage. Remove it from
  		 * the freelist and decrease the counters. These will be