Skip to contentSkip to conversation

mm: hugetlb: fix CMA-related issues

PATCH Superseded Qi Zheng 4 participants 15 messages Nov 11 – Nov 13

Subsystems (8)

v4 of 5 Superseded Latest is v5 →

lore
Share & tools
lore

Qi Zheng

author
#0 lore raw

v4 cover mm: hugetlb: fix CMA-related issues

From: Qi Zheng <[email protected]>

Hi all,

This series fixes three CMA-related issues in HugeTLB reservation, long-term pinning and allocation:

- Avoid ineffective CMA reservations on 32-bit x86 kernels running on CPUs that advertise 1 GiB page support.
- Allow long-term pins of explicitly CMA-allocated HugeTLB folios, which already own the CMA memory.
- Preserve the caller's GFP mask when allocating from CMA.

Changes in v4:
- add comment to cma_alloc_frozen_compound().
(suggested by Oscar Salvador)
- collect Reviewed-by tag.
- rebase onto next-20261110.

Changes in v3:
- Drop the cpuset fix. A more comprehensive fix across the HugeTLB allocation paths will follow in a separate patchset.
- Check for __GFP_COMP instead of adding it in cma_alloc_frozen_compound(), and leave __GFP_NOWARN to callers. (suggested by Oscar Salvador)
- Clarify the CMA ownership comment in folio_is_longterm_pinnable(). (suggested by Muchun Song)
- Collect Reviewed-by and Acked-by tags.

Changes in v2:
- Replace the PF_MEMALLOC_PIN allocation restriction with a GUP exception for HugeTLB folios marked HPG_cma. (suggested by Muchun Song)
- Pass the caller's GFP mask through cma_alloc_frozen_compound() instead of skipping CMA when direct reclaim is disallowed. (suggested by sashiko)
- Remove unnecessary revalidation of successfully allocated folios after cpuset changes. Retry only failed allocations.
(pointed by Jose A. Perez de Azpillaga)
- collect Reviewed-by and Acked-by.
- Rebase onto next-20261104.

Thanks,
Qi

Qi Zheng (3):
x86: hugetlb: disable default CMA reservations on 32-bit kernel
mm: gup: allow long-term pins of CMA-allocated HugeTLB folios
mm: hugetlb: fix GFP mask propagation to CMA

 arch/x86/mm/hugetlbpage.c |  2 +-
 include/linux/cma.h       |  2 +-
 include/linux/mm.h        | 29 +----------------------------
 mm/cma.c                  | 10 ++++++++--
 mm/gup.c                  | 31 +++++++++++++++++++++++++++++++
 mm/hugetlb_cma.c          |  4 ++--
 6 files changed, 44 insertions(+), 34 deletions(-)

base-commit: 793807ca13bf3072fc6d16ce6d90e9ab9beff2a5

Signature · 1 line
2.54.0
author
#1 lore raw

PATCH v4 1/3 x86: hugetlb: disable default CMA reservations on 32-bit kernel

From: Qi Zheng <[email protected]>

A 32-bit x86 kernel can run on a 64-bit-capable CPU advertising GBPAGES. arch_hugetlb_cma_order() checks only this feature and returns a nonzero order, although gigantic HugeTLB support on x86 requires CONFIG_X86_64. Specifying hugetlb_cma= can therefore reserve a CMA area that cannot serve the intended gigantic HugeTLB allocations.

On non-PAE kernels, the folded PUD_SHIFT yields a 4 MiB order, which is equal to MAX_PAGE_ORDER. With CONFIG_DEBUG_VM enabled, this triggers the VM_WARN_ON(order <= MAX_PAGE_ORDER) check in hugetlb_cma_reserve(), warning that hugetlb_cma is attempting to reserve a non-gigantic order.

Require CONFIG_X86_64 as well as GBPAGES when selecting the default CMA order to avoid these ineffective reservations.

Fixes cf11e85fc08c ("mm: hugetlb: optionally allocate gigantic hugepages using cma")
---
 arch/x86/mm/hugetlbpage.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/mm/hugetlbpage.c +1 -1
index 3b26621c91286..70726a99ede7b 100644
@@ -45,7 +45,7 @@ arch_initcall(gigantic_pages_init);
 
 unsigned int __init arch_hugetlb_cma_order(void)
 {
-	if (boot_cpu_has(X86_FEATURE_GBPAGES))
+	if (IS_ENABLED(CONFIG_X86_64) && boot_cpu_has(X86_FEATURE_GBPAGES))
 		return PUD_SHIFT - PAGE_SHIFT;
 
 	return 0;
Signature · 1 line
2.54.0
author
#2 lore raw

PATCH v4 2/3 mm: gup: allow long-term pins of CMA-allocated HugeTLB folios

In reply to Qi Zheng

From: Qi Zheng <[email protected]>

Long-term pinning treats explicitly CMA-allocated HugeTLB folios as unpinnable, causing unnecessary migration or pinning failures.

These folios already own their CMA bitmap ranges, and memory blocks overlapping CMA areas cannot be offlined. Allow HugeTLB folios with HPG_cma to be pinned directly, including in ZONE_MOVABLE.

Fixes cf11e85fc08c ("mm: hugetlb: optionally allocate gigantic hugepages using cma")
---
 include/linux/mm.h | 29 +----------------------------
 mm/gup.c           | 31 +++++++++++++++++++++++++++++++
 2 files changed, 32 insertions(+), 28 deletions(-)
include/linux/mm.h +1 -28
index b0da9be413098..47d1d96bfc7fd 100644
@@ -2954,35 +2954,8 @@ static inline bool is_zero_folio(const struct folio *folio)
 	return is_zero_page(&folio->page);
 }
 
-/* MIGRATE_CMA and ZONE_MOVABLE do not allow pin folios */
 #ifdef CONFIG_MIGRATION
-static inline bool folio_is_longterm_pinnable(struct folio *folio)
-{
-#ifdef CONFIG_CMA
-	int mt = folio_migratetype(folio);
-
-	if (mt == MIGRATE_CMA || mt == MIGRATE_ISOLATE)
-		return false;
-#endif
-	/* The zero page can be "pinned" but gets special handling. */
-	if (is_zero_folio(folio))
-		return true;
-
-	/* Coherent device memory must always allow eviction. */
-	if (folio_is_device_coherent(folio))
-		return false;
-
-	/*
-	 * Filesystems can only tolerate transient delays to truncate and
-	 * hole-punch operations
-	 */
-	if (folio_is_fsdax(folio))
-		return false;
-
-	/* Otherwise, non-movable zone folios can be pinned. */
-	return !folio_is_zone_movable(folio);
-
-}
+bool folio_is_longterm_pinnable(struct folio *folio);
 #else
 static inline bool folio_is_longterm_pinnable(struct folio *folio)
 {
mm/gup.c +31 -0
index ebbfee09b7278..fdafc5a1b78ed 100644
@@ -2219,6 +2219,37 @@ struct page *get_dump_page(unsigned long addr, int *locked)
 
 #ifdef CONFIG_MIGRATION
 
+bool folio_is_longterm_pinnable(struct folio *folio)
+{
+#ifdef CONFIG_CMA
+	int mt = folio_migratetype(folio);
+
+	/* These HugeTLB folios own the CMA memory, so they can be long-term pinned. */
+	if (folio_test_hugetlb(folio) && folio_test_hugetlb_cma(folio))
+		return true;
+
+	if (mt == MIGRATE_CMA || mt == MIGRATE_ISOLATE)
+		return false;
+#endif
+	/* The zero page can be "pinned" but gets special handling. */
+	if (is_zero_folio(folio))
+		return true;
+
+	/* Coherent device memory must always allow eviction. */
+	if (folio_is_device_coherent(folio))
+		return false;
+
+	/*
+	 * Filesystems can only tolerate transient delays to truncate and
+	 * hole-punch operations
+	 */
+	if (folio_is_fsdax(folio))
+		return false;
+
+	/* Otherwise, non-movable zone folios can be pinned. */
+	return !folio_is_zone_movable(folio);
+}
+
 /*
  * An array of either pages or folios ("pofs"). Although it may seem tempting to
  * avoid this complication, by simply interpreting a list of folios as a list of
Signature · 1 line
2.54.0
#3 lore raw

Re: [PATCH v4 2/3] mm: gup: allow long-term pins of CMA-allocated HugeTLB folios

Quoted 8 lines · Qi Zheng

From: Qi Zheng <[email protected]>

Long-term pinning treats explicitly CMA-allocated HugeTLB folios as unpinnable, causing unnecessary migration or pinning failures.

These folios already own their CMA bitmap ranges, and memory blocks overlapping CMA areas cannot be offlined. Allow HugeTLB folios with HPG_cma to be pinned directly, including in ZONE_MOVABLE.

You should really describe here what this patch does (move stuff) and why, and document why you decided to add more hugetlb hacks to core-MM that I absolutely hate.

Signature · 3 lines
Cheers,

David
maintainer
#4 lore raw

Re: [PATCH v4 2/3] mm: gup: allow long-term pins of CMA-allocated HugeTLB folios

In reply to Qi Zheng
Quoted 10 lines · Qi Zheng

From: Qi Zheng <[email protected]>

Long-term pinning treats explicitly CMA-allocated HugeTLB folios as unpinnable, causing unnecessary migration or pinning failures.

These folios already own their CMA bitmap ranges, and memory blocks

Commit message needs updating to bitmap as well.

Quoted 20 lines

overlapping CMA areas cannot be offlined. Allow HugeTLB folios with HPG_cma to be pinned directly, including in ZONE_MOVABLE.

Fixes cf11e85fc08c ("mm: hugetlb: optionally allocate gigantic hugepages using cma")

---
include/linux/mm.h | 29 +----------------------------
mm/gup.c | 31 +++++++++++++++++++++++++++++++

2 files changed, 32 insertions(+), 28 deletions(-)
include/linux/mm.h +0 -0
index b0da9be413098..47d1d96bfc7fd 100644

include/linux/mm.h static inline bool is_zero_folio(const struct folio *folio) · line 2959

Quoted 6 lines
include/linux/mm.h +0 -2
@@ -2954,35 +2954,8 @@ static inline bool is_zero_folio(const struct folio *folio)
 	return is_zero_page(&folio->page);
 }
 
-/* MIGRATE_CMA and ZONE_MOVABLE do not allow pin folios */
 #ifdef CONFIG_MIGRATION
-static inline bool folio_is_longterm_pinnable(struct folio *folio)

I think David prefer to move this function first, then fix it in a separate patch, right?

Thanks,
Muchun

Quoted 75 lines
include/linux/mm.h +1 -26
-{
-#ifdef CONFIG_CMA
-	int mt = folio_migratetype(folio);
-
-	if (mt == MIGRATE_CMA || mt == MIGRATE_ISOLATE)
-		return false;
-#endif
-	/* The zero page can be "pinned" but gets special handling. */
-	if (is_zero_folio(folio))
-		return true;
-
-	/* Coherent device memory must always allow eviction. */
-	if (folio_is_device_coherent(folio))
-		return false;
-
-	/*
-	 * Filesystems can only tolerate transient delays to truncate and
-	 * hole-punch operations
-	 */
-	if (folio_is_fsdax(folio))
-		return false;
-
-	/* Otherwise, non-movable zone folios can be pinned. */
-	return !folio_is_zone_movable(folio);
-
-}
+bool folio_is_longterm_pinnable(struct folio *folio);
 #else
 static inline bool folio_is_longterm_pinnable(struct folio *folio)
 {
mm/gup.c +31 -0
index ebbfee09b7278..fdafc5a1b78ed 100644
@@ -2219,6 +2219,37 @@ struct page *get_dump_page(unsigned long addr, int *locked)
 
 #ifdef CONFIG_MIGRATION
 
+bool folio_is_longterm_pinnable(struct folio *folio)
+{
+#ifdef CONFIG_CMA
+	int mt = folio_migratetype(folio);
+
+	/* These HugeTLB folios own the CMA memory, so they can be long-term pinned. */
+	if (folio_test_hugetlb(folio) && folio_test_hugetlb_cma(folio))
+		return true;
+
+	if (mt == MIGRATE_CMA || mt == MIGRATE_ISOLATE)
+		return false;
+#endif
+	/* The zero page can be "pinned" but gets special handling. */
+	if (is_zero_folio(folio))
+		return true;
+
+	/* Coherent device memory must always allow eviction. */
+	if (folio_is_device_coherent(folio))
+		return false;
+
+	/*
+	 * Filesystems can only tolerate transient delays to truncate and
+	 * hole-punch operations
+	 */
+	if (folio_is_fsdax(folio))
+		return false;
+
+	/* Otherwise, non-movable zone folios can be pinned. */
+	return !folio_is_zone_movable(folio);
+}
+
 /*
 * An array of either pages or folios ("pofs"). Although it may seem tempting to
 * avoid this complication, by simply interpreting a list of folios as a list of
Signature · 1 line
2.54.0
author
#5 lore raw

Re: [PATCH v4 2/3] mm: gup: allow long-term pins of CMA-allocated HugeTLB folios

Quoted 4 lines · Muchun Song
Quoted 4 lines · Qi Zheng
Quoted 8 lines
Quoted 6 lines

From: Qi Zheng <[email protected]>

Long-term pinning treats explicitly CMA-allocated HugeTLB folios as unpinnable, causing unnecessary migration or pinning failures.

These folios already own their CMA bitmap ranges, and memory blocks

Commit message needs updating to bitmap as well.

Will do.

Quoted 24 lines
Quoted 24 lines

overlapping CMA areas cannot be offlined. Allow HugeTLB folios with HPG_cma to be pinned directly, including in ZONE_MOVABLE.

Fixes cf11e85fc08c ("mm: hugetlb: optionally allocate gigantic hugepages using cma")

---
include/linux/mm.h | 29 +----------------------------
mm/gup.c | 31 +++++++++++++++++++++++++++++++

2 files changed, 32 insertions(+), 28 deletions(-)
include/linux/mm.h +0 -0
index b0da9be413098..47d1d96bfc7fd 100644
@@ -2954,35 +2954,8 @@ static inline bool is_zero_folio(const struct folio *folio)
 	return is_zero_page(&folio->page);
 }
 
Quoted 6 lines

include/linux/mm.h static inline bool is_zero_folio(const struct folio *folio) · line 2959

Quoted 3 lines
include/linux/mm.h +0 -2
-/* MIGRATE_CMA and ZONE_MOVABLE do not allow pin folios */
 #ifdef CONFIG_MIGRATION
-static inline bool folio_is_longterm_pinnable(struct folio *folio)

I think David prefer to move this function first, then fix it in a separate patch, right?

Hi David, is this what you mean? If so, I will split it into two patches for the v5.

Thanks,
Qi

#6 lore raw

Re: [PATCH v4 2/3] mm: gup: allow long-term pins of CMA-allocated HugeTLB folios

Quoted 51 lines · Qi Zheng
Quoted 11 lines · Muchun Song
Quoted 9 lines

From: Qi Zheng <[email protected]>

Long-term pinning treats explicitly CMA-allocated HugeTLB folios as unpinnable, causing unnecessary migration or pinning failures.

These folios already own their CMA bitmap ranges, and memory blocks

Commit message needs updating to bitmap as well.

Will do.

Quoted 26 lines
Quoted 26 lines

overlapping CMA areas cannot be offlined. Allow HugeTLB folios with HPG_cma to be pinned directly, including in ZONE_MOVABLE.

Fixes cf11e85fc08c ("mm: hugetlb: optionally allocate gigantic hugepages

using cma")

---
include/linux/mm.h | 29 +---------------------------- mm/gup.c           | 31 +++++++++++++++++++++++++++++++

2 files changed, 32 insertions(+), 28 deletions(-)
include/linux/mm.h +0 -0
index b0da9be413098..47d1d96bfc7fd 100644
@@ -2954,35 +2954,8 @@ static inline bool is_zero_folio(const struct folio
 *folio)
     return is_zero_page(&folio->page);
 }
 
Quoted 7 lines

include/linux/mm.h static inline bool is_zero_folio(const struct folio · line 2960

Quoted 3 lines
include/linux/mm.h +0 -2
-/* MIGRATE_CMA and ZONE_MOVABLE do not allow pin folios */
 #ifdef CONFIG_MIGRATION
-static inline bool folio_is_longterm_pinnable(struct folio *folio)

I think David prefer to move this function first, then fix it in a separate patch,
right?

Quoted 2 lines

Hi David, is this what you mean? If so, I will split it into two patches for the v5.

Let's try something that is more future-directed: cma should deal with cma details. In the future we might implement something reasonable there.

include/linux/cma.h +3 -0
index 8555d38a97b1a..d829fd0355bae 100644
@@ -21,6 +21,9 @@
 #define CMA_MIN_ALIGNMENT_BYTES (PAGE_SIZE * CMA_MIN_ALIGNMENT_PAGES)
 
 struct cma;
+struct folio;
+
+bool cma_folio_is_longterm_pinnable(struct folio *folio);
 
 extern unsigned long totalcma_pages;
 extern phys_addr_t cma_get_base(const struct cma *cma);
include/linux/mm.h +6 -2
index 6af3b7008f1ad..5392e225cab9b 100644
@@ -3,6 +3,7 @@
 #define _LINUX_MM_H
 
 #include <linux/args.h>
+#include <linux/cma.h>
 #include <linux/errno.h>
 #include <linux/mmdebug.h>
 #include <linux/gfp.h>
@@ -2975,7 +2976,10 @@ static inline bool is_zero_folio(const struct folio *folio)
 	return is_zero_page(&folio->page);
 }
 
-/* MIGRATE_CMA and ZONE_MOVABLE do not allow pin folios */
+/*
+ * MIGRATE_CMA and ZONE_MOVABLE folios cannot normally be long-term pinned.
+ * CMA may allow pinning folios explicitly allocated from its areas.
+ */
 #ifdef CONFIG_MIGRATION
 static inline bool folio_is_longterm_pinnable(struct folio *folio)
 {
@@ -2983,7 +2987,7 @@ static inline bool folio_is_longterm_pinnable(struct folio *folio)
 	int mt = folio_migratetype(folio);
 
 	if (mt == MIGRATE_CMA || mt == MIGRATE_ISOLATE)
-		return false;
+		return cma_folio_is_longterm_pinnable(folio);
 #endif
 	/* The zero page can be "pinned" but gets special handling. */
 	if (is_zero_folio(folio))
mm/cma.c +11 -0
index 9ec1d1fd5dff5..b3bf0014a90f5 100644
@@ -27,6 +27,7 @@
 #include <linux/log2.h>
 #include <linux/cma.h>
 #include <linux/highmem.h>
+#include <linux/hugetlb.h>
 #include <linux/io.h>
 #include <linux/kmemleak.h>
 #include <trace/events/cma.h>
@@ -38,6 +39,16 @@
 struct cma cma_areas[MAX_CMA_AREAS];
 unsigned int cma_area_count;
 
+bool cma_folio_is_longterm_pinnable(struct folio *folio)
+{
+	/*
+	 * Explicitly allocated CMA folios own their bitmap ranges and can be
+	 * pinned even in ZONE_MOVABLE, as CMA memory cannot be offlined.
+	 * For now, only HugeTLB tracks these allocations with a folio flag.
+	 */
+	return folio_test_hugetlb(folio) && folio_test_hugetlb_cma(folio);
+}
+
 phys_addr_t cma_get_base(const struct cma *cma)
 {
 	WARN_ON_ONCE(cma->nranges != 1);
Signature · 10 lines
2.43.0



In a follow-up cleanup folio_is_longterm_pinnable() can be moved to gup.c.

-- 
Cheers,

David
author
#7 lore raw

Re: [PATCH v4 2/3] mm: gup: allow long-term pins of CMA-allocated HugeTLB folios

Quoted 52 lines · David Hildenbrand (Arm)
Quoted 51 lines · Qi Zheng
Quoted 11 lines · Muchun Song
Quoted 9 lines

From: Qi Zheng <[email protected]>

Long-term pinning treats explicitly CMA-allocated HugeTLB folios as unpinnable, causing unnecessary migration or pinning failures.

These folios already own their CMA bitmap ranges, and memory blocks

Commit message needs updating to bitmap as well.

Will do.

Quoted 33 lines
Quoted 22 lines

overlapping CMA areas cannot be offlined. Allow HugeTLB folios with HPG_cma to be pinned directly, including in ZONE_MOVABLE.

Fixes cf11e85fc08c ("mm: hugetlb: optionally allocate gigantic hugepages

using cma")

---
include/linux/mm.h | 29 +---------------------------- mm/gup.c           | 31 +++++++++++++++++++++++++++++++

2 files changed, 32 insertions(+), 28 deletions(-)
include/linux/mm.h +0 -0
index b0da9be413098..47d1d96bfc7fd 100644

include/linux/mm.h static inline bool is_zero_folio(const struct folio · line 2960

Quoted 7 lines
include/linux/mm.h +0 -2
@@ -2954,35 +2954,8 @@ static inline bool is_zero_folio(const struct folio
 *folio)
     return is_zero_page(&folio->page);
 }
 
-/* MIGRATE_CMA and ZONE_MOVABLE do not allow pin folios */
 #ifdef CONFIG_MIGRATION
-static inline bool folio_is_longterm_pinnable(struct folio *folio)

I think David prefer to move this function first, then fix it in a separate patch,
right?

Quoted 5 lines
Quoted 2 lines

Hi David, is this what you mean? If so, I will split it into two patches for the v5.

Let's try something that is more future-directed: cma should deal with cma details. In the future we might implement something reasonable there.

Aha, that does make more sense. Will do.

Thanks,
Qi

Quoted 77 lines
include/linux/cma.h +3 -0
index 8555d38a97b1a..d829fd0355bae 100644
@@ -21,6 +21,9 @@
  #define CMA_MIN_ALIGNMENT_BYTES (PAGE_SIZE * CMA_MIN_ALIGNMENT_PAGES)
 
  struct cma;
+struct folio;
+
+bool cma_folio_is_longterm_pinnable(struct folio *folio);
 
  extern unsigned long totalcma_pages;
  extern phys_addr_t cma_get_base(const struct cma *cma);
include/linux/mm.h +6 -2
index 6af3b7008f1ad..5392e225cab9b 100644
@@ -3,6 +3,7 @@
  #define _LINUX_MM_H
 
  #include <linux/args.h>
+#include <linux/cma.h>
  #include <linux/errno.h>
  #include <linux/mmdebug.h>
  #include <linux/gfp.h>
@@ -2975,7 +2976,10 @@ static inline bool is_zero_folio(const struct folio *folio)
  	return is_zero_page(&folio->page);
  }
 
-/* MIGRATE_CMA and ZONE_MOVABLE do not allow pin folios */
+/*
+ * MIGRATE_CMA and ZONE_MOVABLE folios cannot normally be long-term pinned.
+ * CMA may allow pinning folios explicitly allocated from its areas.
+ */
  #ifdef CONFIG_MIGRATION
  static inline bool folio_is_longterm_pinnable(struct folio *folio)
  {
@@ -2983,7 +2987,7 @@ static inline bool folio_is_longterm_pinnable(struct folio *folio)
  	int mt = folio_migratetype(folio);
 
  	if (mt == MIGRATE_CMA || mt == MIGRATE_ISOLATE)
-		return false;
+		return cma_folio_is_longterm_pinnable(folio);
  #endif
  	/* The zero page can be "pinned" but gets special handling. */
  	if (is_zero_folio(folio))
mm/cma.c +11 -0
index 9ec1d1fd5dff5..b3bf0014a90f5 100644
@@ -27,6 +27,7 @@
  #include <linux/log2.h>
  #include <linux/cma.h>
  #include <linux/highmem.h>
+#include <linux/hugetlb.h>
  #include <linux/io.h>
  #include <linux/kmemleak.h>
  #include <trace/events/cma.h>
@@ -38,6 +39,16 @@
  struct cma cma_areas[MAX_CMA_AREAS];
  unsigned int cma_area_count;
 
+bool cma_folio_is_longterm_pinnable(struct folio *folio)
+{
+	/*
+	 * Explicitly allocated CMA folios own their bitmap ranges and can be
+	 * pinned even in ZONE_MOVABLE, as CMA memory cannot be offlined.
+	 * For now, only HugeTLB tracks these allocations with a folio flag.
+	 */
+	return folio_test_hugetlb(folio) && folio_test_hugetlb_cma(folio);
+}
+
  phys_addr_t cma_get_base(const struct cma *cma)
  {
  	WARN_ON_ONCE(cma->nranges != 1);
author
#8 lore raw

PATCH v4 3/3 mm: hugetlb: fix GFP mask propagation to CMA

In reply to Qi Zheng

From: Qi Zheng <[email protected]>

cma_alloc_frozen_compound() uses GFP_KERNEL, allowing direct reclaim during the first MPOL_PREFERRED_MANY allocation attempt even though the caller clears __GFP_DIRECT_RECLAIM.

Pass the caller's GFP mask through the helper so CMA respects its reclaim constraints when trying the preferred nodes.

Fixes 9bda131c6093 ("mm: cma: add cma_alloc_frozen{_compound}()")
---
 include/linux/cma.h |  2 +-
 mm/cma.c            | 10 ++++++++--
 mm/hugetlb_cma.c    |  4 ++--
 3 files changed, 11 insertions(+), 5 deletions(-)
include/linux/cma.h +1 -1
index 8555d38a97b1a..4539ea5f2c4e8 100644
@@ -53,7 +53,7 @@ extern bool cma_release(struct cma *cma, const struct page *pages, unsigned long
 
 struct page *cma_alloc_frozen(struct cma *cma, unsigned long count,
 		unsigned int align, bool no_warn);
-struct page *cma_alloc_frozen_compound(struct cma *cma, unsigned int order);
+struct page *cma_alloc_frozen_compound(struct cma *cma, unsigned int order, gfp_t gfp);
 bool cma_release_frozen(struct cma *cma, const struct page *pages,
 		unsigned long count);
 
mm/cma.c +8 -2
index 9ec1d1fd5dff5..a7c47c0e7d070 100644
@@ -933,9 +933,15 @@ struct page *cma_alloc_frozen(struct cma *cma, unsigned long count,
 	return __cma_alloc_frozen(cma, count, align, gfp);
 }
 
-struct page *cma_alloc_frozen_compound(struct cma *cma, unsigned int order)
+struct page *cma_alloc_frozen_compound(struct cma *cma, unsigned int order, gfp_t gfp)
 {
-	gfp_t gfp = GFP_KERNEL | __GFP_COMP | __GFP_NOWARN;
+	/*
+	 * The only caller of cma_alloc_frozen_compound() is hugetlb, and
+	 * hugetlb constructs its gfp mask via htlb_alloc_mask(), which already
+	 * sets __GFP_COMP. Check here for usecases bypassing htlb_alloc_mask().
+	 */
+	if (WARN_ON_ONCE(!(gfp & __GFP_COMP)))
+		return NULL;
 
 	return __cma_alloc_frozen(cma, 1 << order, order, gfp);
 }
mm/hugetlb_cma.c +2 -2
index cd6a64986278a..3e5a30571524a 100644
@@ -69,14 +69,14 @@ struct folio *hugetlb_cma_alloc_frozen_folio(int order, gfp_t gfp_mask,
 	}
 
 	if (hugetlb_cma[nid] && node_isset(nid, *nmask))
-		page = cma_alloc_frozen_compound(hugetlb_cma[nid], order);
+		page = cma_alloc_frozen_compound(hugetlb_cma[nid], order, gfp_mask);
 
 	if (!page && !(gfp_mask & __GFP_THISNODE)) {
 		for_each_node_mask(node, *nmask) {
 			if (node == nid || !hugetlb_cma[node])
 				continue;
 
-			page = cma_alloc_frozen_compound(hugetlb_cma[node], order);
+			page = cma_alloc_frozen_compound(hugetlb_cma[node], order, gfp_mask);
 			if (page)
 				break;
 		}
Signature · 1 line
2.54.0
#9 lore raw

Re: [PATCH v4 3/3] mm: hugetlb: fix GFP mask propagation to CMA

Quoted 5 lines · Qi Zheng

From: Qi Zheng <[email protected]>

cma_alloc_frozen_compound() uses GFP_KERNEL, allowing direct reclaim during the first MPOL_PREFERRED_MANY allocation attempt even though the caller clears __GFP_DIRECT_RECLAIM.

MPOL_PREFERRED_MANY is only used for the partner allocator. Without your patches "mm: hugetlb: add CMA support for PMD-sized pages," cma_alloc_frozen_compound() is only used for huge page allocations. In this case, I think we don't need the gfp flag, which is why I hardcoded it, so there's no need for the Fixes tag; this patch should be part of your new feature. Maybe I missed something?

Quoted 72 lines

Pass the caller's GFP mask through the helper so CMA respects its reclaim constraints when trying the preferred nodes.

Fixes 9bda131c6093 ("mm: cma: add cma_alloc_frozen{_compound}()")
---
  include/linux/cma.h |  2 +-
  mm/cma.c            | 10 ++++++++--
  mm/hugetlb_cma.c    |  4 ++--
  3 files changed, 11 insertions(+), 5 deletions(-)
include/linux/cma.h +1 -1
index 8555d38a97b1a..4539ea5f2c4e8 100644
@@ -53,7 +53,7 @@ extern bool cma_release(struct cma *cma, const struct page *pages, unsigned long
 
  struct page *cma_alloc_frozen(struct cma *cma, unsigned long count,
  		unsigned int align, bool no_warn);
-struct page *cma_alloc_frozen_compound(struct cma *cma, unsigned int order);
+struct page *cma_alloc_frozen_compound(struct cma *cma, unsigned int order, gfp_t gfp);
  bool cma_release_frozen(struct cma *cma, const struct page *pages,
  		unsigned long count);
 
mm/cma.c +8 -2
index 9ec1d1fd5dff5..a7c47c0e7d070 100644
@@ -933,9 +933,15 @@ struct page *cma_alloc_frozen(struct cma *cma, unsigned long count,
  	return __cma_alloc_frozen(cma, count, align, gfp);
  }
 
-struct page *cma_alloc_frozen_compound(struct cma *cma, unsigned int order)
+struct page *cma_alloc_frozen_compound(struct cma *cma, unsigned int order, gfp_t gfp)
  {
-	gfp_t gfp = GFP_KERNEL | __GFP_COMP | __GFP_NOWARN;
+	/*
+	 * The only caller of cma_alloc_frozen_compound() is hugetlb, and
+	 * hugetlb constructs its gfp mask via htlb_alloc_mask(), which already
+	 * sets __GFP_COMP. Check here for usecases bypassing htlb_alloc_mask().
+	 */
+	if (WARN_ON_ONCE(!(gfp & __GFP_COMP)))
+		return NULL;
 
  	return __cma_alloc_frozen(cma, 1 << order, order, gfp);
  }
mm/hugetlb_cma.c +2 -2
index cd6a64986278a..3e5a30571524a 100644
@@ -69,14 +69,14 @@ struct folio *hugetlb_cma_alloc_frozen_folio(int order, gfp_t gfp_mask,
  	}
 
  	if (hugetlb_cma[nid] && node_isset(nid, *nmask))
-		page = cma_alloc_frozen_compound(hugetlb_cma[nid], order);
+		page = cma_alloc_frozen_compound(hugetlb_cma[nid], order, gfp_mask);
 
  	if (!page && !(gfp_mask & __GFP_THISNODE)) {
  		for_each_node_mask(node, *nmask) {
  			if (node == nid || !hugetlb_cma[node])
  				continue;
 
-			page = cma_alloc_frozen_compound(hugetlb_cma[node], order);
+			page = cma_alloc_frozen_compound(hugetlb_cma[node], order, gfp_mask);
  			if (page)
  				break;
  		}
#10 lore raw

Re: [PATCH v4 3/3] mm: hugetlb: fix GFP mask propagation to CMA

Quoted 10 lines · Kefeng Wang
Quoted 5 lines · Qi Zheng

From: Qi Zheng <[email protected]>

cma_alloc_frozen_compound() uses GFP_KERNEL, allowing direct reclaim during the first MPOL_PREFERRED_MANY allocation attempt even though the caller clears __GFP_DIRECT_RECLAIM.

MPOL_PREFERRED_MANY is only used for the partner allocator. Without your

Sorry, buddy allocation

Quoted 2 lines

patches "mm: hugetlb: add CMA support for PMD-sized pages," cma_alloc_frozen_compound() is only used for huge page allocations. In

gigantic allocation

Quoted 81 lines

this case, I think we don't need the gfp flag, which is why I hardcoded it, so there's no need for the Fixes tag; this patch should be part of your new feature. Maybe I missed something?

Quoted 76 lines

Pass the caller's GFP mask through the helper so CMA respects its reclaim constraints when trying the preferred nodes.

Fixes 9bda131c6093 ("mm: cma: add cma_alloc_frozen{_compound}()")

---
  include/linux/cma.h |  2 +-
  mm/cma.c            | 10 ++++++++--
  mm/hugetlb_cma.c    |  4 ++--

  3 files changed, 11 insertions(+), 5 deletions(-)
include/linux/cma.h +0 -0
index 8555d38a97b1a..4539ea5f2c4e8 100644
@@ -53,7 +53,7 @@ extern bool cma_release(struct cma *cma, const 

struct page *pages, unsigned long
  struct page *cma_alloc_frozen(struct cma *cma, unsigned long count, unsigned int align, bool no_warn);
-struct page *cma_alloc_frozen_compound(struct cma *cma, unsigned int order);
+struct page *cma_alloc_frozen_compound(struct cma *cma, unsigned int order, gfp_t gfp);
  bool cma_release_frozen(struct cma *cma, const struct page *pages, unsigned long count);

mm/cma.c +4 -2
index 9ec1d1fd5dff5..a7c47c0e7d070 100644
@@ -933,9 +933,15 @@ struct page *cma_alloc_frozen(struct cma *cma, 
 unsigned long count,
       return __cma_alloc_frozen(cma, count, align, gfp);
   }
-struct page *cma_alloc_frozen_compound(struct cma *cma, unsigned int 
 order)
+struct page *cma_alloc_frozen_compound(struct cma *cma, unsigned int 
 order, gfp_t gfp)
   {
-    gfp_t gfp = GFP_KERNEL | __GFP_COMP | __GFP_NOWARN;
+    /*
+     * The only caller of cma_alloc_frozen_compound() is hugetlb, and
+     * hugetlb constructs its gfp mask via htlb_alloc_mask(), which 

already
+     * sets __GFP_COMP. Check here for usecases bypassing htlb_alloc_mask().
+     */
+    if (WARN_ON_ONCE(!(gfp & __GFP_COMP)))
+        return NULL;
      return __cma_alloc_frozen(cma, 1 << order, order, gfp); }

mm/hugetlb_cma.c +2 -2
index cd6a64986278a..3e5a30571524a 100644
@@ -69,14 +69,14 @@ struct folio *hugetlb_cma_alloc_frozen_folio(int 
 order, gfp_t gfp_mask,
       }
       if (hugetlb_cma[nid] && node_isset(nid, *nmask))
-        page = cma_alloc_frozen_compound(hugetlb_cma[nid], order);
+        page = cma_alloc_frozen_compound(hugetlb_cma[nid], order, 
 gfp_mask);
       if (!page && !(gfp_mask & __GFP_THISNODE)) {
           for_each_node_mask(node, *nmask) {
               if (node == nid || !hugetlb_cma[node])
                   continue;
-            page = cma_alloc_frozen_compound(hugetlb_cma[node], order);
+            page = cma_alloc_frozen_compound(hugetlb_cma[node], 
 order, gfp_mask);
               if (page)
                   break;
           }
author
#11 lore raw

Re: [PATCH v4 3/3] mm: hugetlb: fix GFP mask propagation to CMA

In reply to Kefeng Wang
Quoted 10 lines · Kefeng Wang
Quoted 5 lines · Qi Zheng

From: Qi Zheng <[email protected]>

cma_alloc_frozen_compound() uses GFP_KERNEL, allowing direct reclaim during the first MPOL_PREFERRED_MANY allocation attempt even though the caller clears __GFP_DIRECT_RECLAIM.

MPOL_PREFERRED_MANY is only used for the partner allocator. Without your

It's doesn't seem so.

Gigantic surplus allocations already use MPOL_PREFERRED_MANY, following eb02f14c4a2b ("mm/hugetlb: allow overcommitting gigantic hugepages"), which predates 9bda131c6093.

alloc_buddy_hugetlb_folio
--> MPOL_PREFERRED_MANY

    --> gfp_t gfp = gfp_mask & ~(__GFP_DIRECT_RECLAIM | __GFP_NOFAIL);
        folio = alloc_surplus_hugetlb_folio(h, gfp, mpoli->nid, nodemask);
        --> alloc_fresh_hugetlb_folio
            --> only_alloc_fresh_hugetlb_folio
                --> alloc_gigantic_frozen_folio
                    --> hugetlb_cma_alloc_frozen_folio
                        --> cma_alloc_frozen_compound

Before 9bda131c6093, cma_alloc_folio() forwarded the caller's GFP mask. The conversion to cma_alloc_frozen_compound() replaced it with GFP_KERNEL, re-enabling direct reclaim during the preferred-node attempt.

Right?

Thanks,
Qi

Quoted 83 lines

patches "mm: hugetlb: add CMA support for PMD-sized pages," cma_alloc_frozen_compound() is only used for huge page allocations. In this case, I think we don't need the gfp flag, which is why I hardcoded it, so there's no need for the Fixes tag; this patch should be part of your new feature. Maybe I missed something?

Quoted 76 lines

Pass the caller's GFP mask through the helper so CMA respects its reclaim constraints when trying the preferred nodes.

Fixes 9bda131c6093 ("mm: cma: add cma_alloc_frozen{_compound}()")

---
  include/linux/cma.h |  2 +-
  mm/cma.c            | 10 ++++++++--
  mm/hugetlb_cma.c    |  4 ++--

  3 files changed, 11 insertions(+), 5 deletions(-)
include/linux/cma.h +0 -0
index 8555d38a97b1a..4539ea5f2c4e8 100644
@@ -53,7 +53,7 @@ extern bool cma_release(struct cma *cma, const 

struct page *pages, unsigned long
  struct page *cma_alloc_frozen(struct cma *cma, unsigned long count, unsigned int align, bool no_warn);
-struct page *cma_alloc_frozen_compound(struct cma *cma, unsigned int order);
+struct page *cma_alloc_frozen_compound(struct cma *cma, unsigned int order, gfp_t gfp);
  bool cma_release_frozen(struct cma *cma, const struct page *pages, unsigned long count);

mm/cma.c +4 -2
index 9ec1d1fd5dff5..a7c47c0e7d070 100644
@@ -933,9 +933,15 @@ struct page *cma_alloc_frozen(struct cma *cma, 
 unsigned long count,
       return __cma_alloc_frozen(cma, count, align, gfp);
   }
-struct page *cma_alloc_frozen_compound(struct cma *cma, unsigned int 
 order)
+struct page *cma_alloc_frozen_compound(struct cma *cma, unsigned int 
 order, gfp_t gfp)
   {
-    gfp_t gfp = GFP_KERNEL | __GFP_COMP | __GFP_NOWARN;
+    /*
+     * The only caller of cma_alloc_frozen_compound() is hugetlb, and
+     * hugetlb constructs its gfp mask via htlb_alloc_mask(), which 

already
+     * sets __GFP_COMP. Check here for usecases bypassing htlb_alloc_mask().
+     */
+    if (WARN_ON_ONCE(!(gfp & __GFP_COMP)))
+        return NULL;
      return __cma_alloc_frozen(cma, 1 << order, order, gfp); }

mm/hugetlb_cma.c +2 -2
index cd6a64986278a..3e5a30571524a 100644
@@ -69,14 +69,14 @@ struct folio *hugetlb_cma_alloc_frozen_folio(int 
 order, gfp_t gfp_mask,
       }
       if (hugetlb_cma[nid] && node_isset(nid, *nmask))
-        page = cma_alloc_frozen_compound(hugetlb_cma[nid], order);
+        page = cma_alloc_frozen_compound(hugetlb_cma[nid], order, 
 gfp_mask);
       if (!page && !(gfp_mask & __GFP_THISNODE)) {
           for_each_node_mask(node, *nmask) {
               if (node == nid || !hugetlb_cma[node])
                   continue;
-            page = cma_alloc_frozen_compound(hugetlb_cma[node], order);
+            page = cma_alloc_frozen_compound(hugetlb_cma[node], 
 order, gfp_mask);
               if (page)
                   break;
           }
#12 lore raw

Re: [PATCH v4 3/3] mm: hugetlb: fix GFP mask propagation to CMA

Quoted 30 lines · Qi Zheng
Quoted 10 lines · Kefeng Wang
Quoted 5 lines · Qi Zheng

From: Qi Zheng <[email protected]>

cma_alloc_frozen_compound() uses GFP_KERNEL, allowing direct reclaim during the first MPOL_PREFERRED_MANY allocation attempt even though the caller clears __GFP_DIRECT_RECLAIM.

MPOL_PREFERRED_MANY is only used for the partner allocator. Without your

It's doesn't seem so.

Gigantic surplus allocations already use MPOL_PREFERRED_MANY, following eb02f14c4a2b ("mm/hugetlb: allow overcommitting gigantic hugepages"), which predates 9bda131c6093.

alloc_buddy_hugetlb_folio
--> MPOL_PREFERRED_MANY
    --> gfp_t gfp = gfp_mask & ~(__GFP_DIRECT_RECLAIM | __GFP_NOFAIL); folio = alloc_surplus_hugetlb_folio(h, gfp, mpoli->nid, nodemask); --> alloc_fresh_hugetlb_folio
            --> only_alloc_fresh_hugetlb_folio
                --> alloc_gigantic_frozen_folio
                    --> hugetlb_cma_alloc_frozen_folio
                        --> cma_alloc_frozen_compound

Quoted 4 lines

Before 9bda131c6093, cma_alloc_folio() forwarded the caller's GFP mask. The conversion to cma_alloc_frozen_compound() replaced it with GFP_KERNEL, re-enabling direct reclaim during the preferred-node attempt.

Oh, I forgot about that commit. The naming and some of the comments are misleading because alloc_buddy_hugetlb_folio/alloc_surplus_hugetlb_folio can allocate both buddy and gigantic folios (which can be allocated from
CMA).

Maybe rename alloc_buddy_hugetlb_folio to hugetlb_alloc_surplus_folio, or other names...

author
#13 lore raw

Re: [PATCH v4 3/3] mm: hugetlb: fix GFP mask propagation to CMA

Quoted 38 lines · Kefeng Wang
Quoted 30 lines · Qi Zheng
Quoted 10 lines · Kefeng Wang
Quoted 5 lines · Qi Zheng

From: Qi Zheng <[email protected]>

cma_alloc_frozen_compound() uses GFP_KERNEL, allowing direct reclaim during the first MPOL_PREFERRED_MANY allocation attempt even though the caller clears __GFP_DIRECT_RECLAIM.

MPOL_PREFERRED_MANY is only used for the partner allocator. Without your

It's doesn't seem so.

Gigantic surplus allocations already use MPOL_PREFERRED_MANY, following eb02f14c4a2b ("mm/hugetlb: allow overcommitting gigantic hugepages"), which predates 9bda131c6093.

alloc_buddy_hugetlb_folio
--> MPOL_PREFERRED_MANY
     --> gfp_t gfp = gfp_mask & ~(__GFP_DIRECT_RECLAIM | __GFP_NOFAIL); folio = alloc_surplus_hugetlb_folio(h, gfp, mpoli->nid, nodemask); --> alloc_fresh_hugetlb_folio
             --> only_alloc_fresh_hugetlb_folio
                 --> alloc_gigantic_frozen_folio
                     --> hugetlb_cma_alloc_frozen_folio
                         --> cma_alloc_frozen_compound

Quoted 4 lines

Before 9bda131c6093, cma_alloc_folio() forwarded the caller's GFP mask. The conversion to cma_alloc_frozen_compound() replaced it with GFP_KERNEL, re-enabling direct reclaim during the preferred-node attempt.

Quoted 6 lines

Oh, I forgot about that commit. The naming and some of the comments are misleading because alloc_buddy_hugetlb_folio/alloc_surplus_hugetlb_folio can allocate both buddy and gigantic folios (which can be allocated from
CMA).

Maybe rename alloc_buddy_hugetlb_folio to hugetlb_alloc_surplus_folio, or other names...

Naming is difficult. ;)

The Hugetlb implementation seems quite messy in many places; let’s focus on fixing the issues first.

#14 lore raw

Re: [PATCH v4 3/3] mm: hugetlb: fix GFP mask propagation to CMA

Quoted 45 lines · Qi Zheng
Quoted 42 lines · Kefeng Wang
Quoted 30 lines · Qi Zheng
Quoted 10 lines · Kefeng Wang
Quoted 5 lines · Qi Zheng

From: Qi Zheng <[email protected]>

cma_alloc_frozen_compound() uses GFP_KERNEL, allowing direct reclaim during the first MPOL_PREFERRED_MANY allocation attempt even though the caller clears __GFP_DIRECT_RECLAIM.

MPOL_PREFERRED_MANY is only used for the partner allocator. Without your

It's doesn't seem so.

Gigantic surplus allocations already use MPOL_PREFERRED_MANY, following eb02f14c4a2b ("mm/hugetlb: allow overcommitting gigantic hugepages"), which predates 9bda131c6093.

alloc_buddy_hugetlb_folio
--> MPOL_PREFERRED_MANY
     --> gfp_t gfp = gfp_mask & ~(__GFP_DIRECT_RECLAIM | __GFP_NOFAIL); folio = alloc_surplus_hugetlb_folio(h, gfp, mpoli->nid, nodemask); --> alloc_fresh_hugetlb_folio
             --> only_alloc_fresh_hugetlb_folio
                 --> alloc_gigantic_frozen_folio
                     --> hugetlb_cma_alloc_frozen_folio
                         --> cma_alloc_frozen_compound

Quoted 4 lines

Before 9bda131c6093, cma_alloc_folio() forwarded the caller's GFP mask. The conversion to cma_alloc_frozen_compound() replaced it with GFP_KERNEL, re-enabling direct reclaim during the preferred-node attempt.

Oh, I forgot about that commit. The naming and some of the comments are misleading because alloc_buddy_hugetlb_folio/ alloc_surplus_hugetlb_folio can allocate both buddy and gigantic folios (which can be allocated from
CMA).

Quoted 8 lines
Quoted 2 lines

Maybe rename alloc_buddy_hugetlb_folio to hugetlb_alloc_surplus_folio, or other names...

Naming is difficult. ;)

The Hugetlb implementation seems quite messy in many places; let’s focus on fixing the issues first.

Indeed. thanks for you confirm.