mm: hugetlb: fix CMA-related issues
Subsystems (8)
v3 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 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 | 5 +++-- mm/gup.c | 31 +++++++++++++++++++++++++++++++ mm/hugetlb_cma.c | 4 ++-- 6 files changed, 39 insertions(+), 34 deletions(-)
base-commit: 8b7179210b22f8e48a82c5ceacb9d56a9c4cfec4
Signature · 1 line
2.54.0
PATCH v3 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.
--- arch/x86/mm/hugetlbpage.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/mm/hugetlbpage.c +1 -1
@@ -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
PATCH v3 2/3 mm: gup: allow long-term pins of CMA-allocated HugeTLB folios
In reply to Qi ZhengFrom: 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.
--- include/linux/mm.h | 29 +---------------------------- mm/gup.c | 31 +++++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 28 deletions(-)
include/linux/mm.h +1 -28
@@ -2975,35 +2975,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
@@ -2216,6 +2216,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
PATCH v3 3/3 mm: hugetlb: fix GFP mask propagation to CMA
In reply to Qi ZhengFrom: 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.
--- include/linux/cma.h | 2 +- mm/cma.c | 5 +++-- mm/hugetlb_cma.c | 4 ++-- 3 files changed, 6 insertions(+), 5 deletions(-)
include/linux/cma.h +1 -1
@@ -53,7 +53,7 @@ extern bool cma_release(struct cma *cma, const struct page *pages, unsigned longstruct 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 +3 -2
@@ -933,9 +933,10 @@ 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; +if (WARN_ON_ONCE (!(gfp & __GFP_COMP))) +return NULL ;return __cma_alloc_frozen (cma,1 << order, order, gfp); }
mm/hugetlb_cma.c +2 -2
@@ -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
Re: [PATCH v3 3/3] mm: hugetlb: fix GFP mask propagation to CMA
Quoted 6 lines · Qi Zheng
From: Qi Zheng <[email protected]>
cma_alloc_frozen_compound()usesGFP_KERNEL, allowing direct reclaim during the firstMPOL_PREFERRED_MANYallocation attempt even though the caller clears__GFP_DIRECT_RECLAIM.
Quoted 8 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}()")Signed-off-by Qi Zheng <[email protected]>Acked-by Muchun Song <[email protected]>Reviewed-by Jose A. Perez de Azpillaga <[email protected]>
A comment below:
mm/cma.c struct page *cma_alloc_frozen(struct cma *cma, unsigned long count, · line 939
Quoted 12 lines
mm/cma.c +3
-2@@ -933,9 +933,10 @@ 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; +if (WARN_ON_ONCE (!(gfp & __GFP_COMP))) +return NULL ;
I think this needs a comment explaining why we are checking for __GFP_COMP, because it might not be obvious to the casual reader.
Something like:
"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()."
Maybe in the future we do have some other users of this function which are outside of the hugetlb realm, but if we are propagating the gfp mask down the chain to this point, we do have to check that those users are also setting __GFP_COMP.
Signature · 2 lines
Oscar Salvador SUSE Labs
David Hildenbrand (Arm)
maintainerRe: [PATCH v3 2/3] mm: gup: allow long-term pins of CMA-allocated HugeTLB folios
In reply to Qi ZhengQuoted 48 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_cmato be pinned directly, including inZONE_MOVABLE.Fixes cf11e85fc08c ("mm: hugetlb: optionally allocate gigantic hugepages using cma")Suggested-by Muchun Song <[email protected]>Signed-off-by Qi Zheng <[email protected]>Acked-by Muchun Song <[email protected]>Reviewed-by Oscar Salvador <[email protected]>Reviewed-by Jose A. Perez de Azpillaga <[email protected]>--- include/linux/mm.h | 29 +---------------------------- mm/gup.c | 31 +++++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 28 deletions(-)include/linux/mm.h +0
-18@@ -2975,35 +2975,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; - -/*
include/linux/mm.h static inline bool is_zero_folio(const struct folio *folio) · line 3006
Quoted 10 lines
include/linux/mm.h +0
-10-* 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); - -}
Moving code while adding new stuff to it, please don't.
Move code first, then modify it.
I don't want to play "find the difference between two pictures" on a Saturday evening.
Quoted 10 lines
include/linux/mm.h +1
-0+bool folio_is_longterm_pinnable (struct folio *folio);#else static inline bool folio_is_longterm_pinnable (struct folio *folio) {mm/gup.c +0
-0@@ -2216,6 +2216,37 @@ struct page *get_dump_page(unsigned long addr, int *locked)
mm/gup.c struct page *get_dump_page(unsigned long addr, int *locked) · line 2226
Quoted 10 lines
mm/gup.c +8
-0#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;
More hugetlb hackery ... don't like that, sorry.
I think this just goes down to a problem we discussed a while ago that is independent of hugetlb: we'd need some way to identify that the the page was actually allocated by the CMA owner, and it's not just some other movable allcoation that ended up there.
For something generic non-hugetlb thing, can't we call into CMA to ask whether the current chunk is marked allocated?
If that's too hard, we could add a temporary hugetlb hack.
But we all hate hugetlb hacks. :)
Signature · 3 lines
Cheers, David
Re: [PATCH v3 2/3] mm: gup: allow long-term pins of CMA-allocated HugeTLB folios
Quoted 62 lines · David Hildenbrand (Arm)
Quoted 48 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_cmato be pinned directly, including inZONE_MOVABLE.Fixes cf11e85fc08c ("mm: hugetlb: optionally allocate gigantic hugepages using cma")Suggested-by Muchun Song <[email protected]>Signed-off-by Qi Zheng <[email protected]>Acked-by Muchun Song <[email protected]>Reviewed-by Oscar Salvador <[email protected]>Reviewed-by Jose A. Perez de Azpillaga <[email protected]>--- include/linux/mm.h | 29 +---------------------------- mm/gup.c | 31 +++++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 28 deletions(-)include/linux/mm.h +0
-18@@ -2975,35 +2975,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; - -/* include/linux/mm.h static inline bool is_zero_folio(const struct folio *folio) · line 3006
Quoted 10 lines
include/linux/mm.h +0
-10-* 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); - -}Moving code while adding new stuff to it, please don't.
Quoted 4 lines
Move code first, then modify it.
I don't want to play "find the difference between two pictures" on a Saturday evening.
;), got it.
I moved the code to resolve a build dependency.
Quoted 13 lines
Quoted 13 lines
include/linux/mm.h +1
-0+bool folio_is_longterm_pinnable (struct folio *folio);#else static inline bool folio_is_longterm_pinnable (struct folio *folio) {mm/gup.c +0
-0@@ -2216,6 +2216,37 @@ struct page *get_dump_page(unsigned long addr, int *locked)#ifdef CONFIG_MIGRATION
Quoted 9 lines
mm/gup.c struct page *get_dump_page(unsigned long addr, int *locked) · line 2226
Quoted 8 lines
mm/gup.c +8
-0+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;More hugetlb hackery ... don't like that, sorry.
Agree.
Quoted 8 lines
I think this just goes down to a problem we discussed a while ago that is independent of hugetlb: we'd need some way to identify that the the page was actually allocated by the CMA owner, and it's not just some other movable allcoation that ended up there.
For something generic non-hugetlb thing, can't we call into CMA to ask whether the current chunk is marked allocated?
The existing bitmap is set before allocation completes, so the range may still contain borrowed movable pages. A set bit alone cannot establish ownership.
A generic query may need separate ownership tracking, such as an owned_bitmap recording successfully allocated pages. Its updates and lookups would also need to handle concurrency with fast GUP.
Quoted 2 lines
If that's too hard, we could add a temporary hugetlb hack.
For this fix, I'd prefer to keep the current check.
Quoted 2 lines
But we all hate hugetlb hacks. :)
+1.
Quoted 1 line
Re: [PATCH v3 3/3] mm: hugetlb: fix GFP mask propagation to CMA
In reply to Oscar Salvador (SUSE)Quoted 37 lines · Oscar Salvador (SUSE)
Quoted 6 lines · Qi Zheng
From: Qi Zheng <[email protected]>
cma_alloc_frozen_compound()usesGFP_KERNEL, allowing direct reclaim during the firstMPOL_PREFERRED_MANYallocation attempt even though the caller clears__GFP_DIRECT_RECLAIM.Quoted 8 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}()")Signed-off-by Qi Zheng <[email protected]>Acked-by Muchun Song <[email protected]>Reviewed-by Jose A. Perez de Azpillaga <[email protected]>Reviewed-by Oscar Salvador (SUSE) <[email protected]>A comment below:
mm/cma.c struct page *cma_alloc_frozen(struct cma *cma, unsigned long count, · line 939
Quoted 12 lines
mm/cma.c +3
-2@@ -933,9 +933,10 @@ 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; +if (WARN_ON_ONCE (!(gfp & __GFP_COMP))) +return NULL ;I think this needs a comment explaining why we are checking for
__GFP_COMP, because it might not be obvious to the casual reader.
Something like:
Quoted 3 lines
"The only caller of
cma_alloc_frozen_compound()is hugetlb, and hugetlb
constructs its gfp mask viahtlb_alloc_mask(), which already sets__GFP_COMP. Check here for usecases bypassinghtlb_alloc_mask()."
OK, will add this comment.
Thanks,
Qi
Quoted 8 lines
Maybe in the future we do have some other users of this function which are outside of the hugetlb realm, but if we are propagating the gfp mask down the chain to this point, we do have to check that those users are also setting
__GFP_COMP.
David Hildenbrand (Arm)
maintainerRe: [PATCH v3 2/3] mm: gup: allow long-term pins of CMA-allocated HugeTLB folios
In reply to Qi ZhengQuoted 65 lines · Qi Zheng
Quoted 62 lines · David Hildenbrand (Arm)
Quoted 48 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_cmato be pinned directly, including inZONE_MOVABLE.Fixes cf11e85fc08c ("mm: hugetlb: optionally allocate gigantic hugepages using cma")Suggested-by Muchun Song <[email protected]>Signed-off-by Qi Zheng <[email protected]>Acked-by Muchun Song <[email protected]>Reviewed-by Oscar Salvador <[email protected]>Reviewed-by Jose A. Perez de Azpillaga <[email protected]>--- include/linux/mm.h | 29 +---------------------------- mm/gup.c | 31 +++++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 28 deletions(-)include/linux/mm.h +0
-18@@ -2975,35 +2975,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; - -/* include/linux/mm.h static inline bool is_zero_folio(const struct folio *folio) · line 3006
Quoted 10 lines
include/linux/mm.h +0
-10-* 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); - -}Moving code while adding new stuff to it, please don't.
Quoted 5 lines
Quoted 4 lines
Move code first, then modify it.
I don't want to play "find the difference between two pictures" on a Saturday evening.
You resent your series way too fast.
Now we have discussions in to threads, congratulations ;)
Quoted 4 lines
;), got it.
I moved the code to resolve a build dependency.
Why can't that be resolved differently? What is the dependency?
Quoted 34 lines
Quoted 13 lines
Quoted 13 lines
include/linux/mm.h +1
-0+bool folio_is_longterm_pinnable (struct folio *folio);#else static inline bool folio_is_longterm_pinnable (struct folio *folio) {mm/gup.c +0
-0@@ -2216,6 +2216,37 @@ struct page *get_dump_page(unsigned long addr, int *locked)#ifdef CONFIG_MIGRATION Quoted 9 lines
mm/gup.c struct page *get_dump_page(unsigned long addr, int *locked) · line 2226
Quoted 8 lines
mm/gup.c +8
-0+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;More hugetlb hackery ... don't like that, sorry.
Agree.
Quoted 8 lines
I think this just goes down to a problem we discussed a while ago that is independent of hugetlb: we'd need some way to identify that the the page was actually allocated by the CMA owner, and it's not just some other movable allcoation that ended up there.
For something generic non-hugetlb thing, can't we call into CMA to ask whether the current chunk is marked allocated?
Quoted 7 lines
The existing bitmap is set before allocation completes, so the range may still contain borrowed movable pages. A set bit alone cannot establish ownership.
A generic query may need separate ownership tracking, such as an
owned_bitmaprecording successfully allocated pages. Its updates and lookups would also need to handle concurrency with fast GUP.
Or simply two bitmaps? "trying allocation - allocated"
Signature · 3 lines
Cheers, David
Re: [PATCH v3 2/3] mm: gup: allow long-term pins of CMA-allocated HugeTLB folios
Hi David,
Quoted 66 lines · David Hildenbrand (Arm)
Quoted 65 lines · Qi Zheng
Quoted 62 lines · David Hildenbrand (Arm)
Quoted 48 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_cmato be pinned directly, including inZONE_MOVABLE.Fixes cf11e85fc08c ("mm: hugetlb: optionally allocate gigantic hugepages using cma")Suggested-by Muchun Song <[email protected]>Signed-off-by Qi Zheng <[email protected]>Acked-by Muchun Song <[email protected]>Reviewed-by Oscar Salvador <[email protected]>Reviewed-by Jose A. Perez de Azpillaga <[email protected]>--- include/linux/mm.h | 29 +---------------------------- mm/gup.c | 31 +++++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 28 deletions(-)include/linux/mm.h +0
-18@@ -2975,35 +2975,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; - -/* include/linux/mm.h static inline bool is_zero_folio(const struct folio *folio) · line 3006
Quoted 10 lines
include/linux/mm.h +0
-10-* 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); - -}Moving code while adding new stuff to it, please don't.
Quoted 7 lines
Quoted 5 lines
Quoted 4 lines
Move code first, then modify it.
I don't want to play "find the difference between two pictures" on a Saturday evening.
You resent your series way too fast.
Got it.
Quoted 9 lines
Now we have discussions in to threads, congratulations ;)
Quoted 4 lines
;), got it.
I moved the code to resolve a build dependency.
Why can't that be resolved differently? What is the dependency?
The dependency is that the new check wants to use folio_test_hugetlb_cma(), which is a HugeTLB-specific inline helper from include/linux/hugetlb.h.
folio_is_longterm_pinnable() currently lives in include/linux/mm.h, while include/linux/hugetlb.h already includes linux/mm.h. So I can't simply use folio_test_hugetlb_cma() from mm.h
Quoted 32 lines
Quoted 32 lines
Quoted 14 lines
Quoted 14 lines
include/linux/mm.h +1
-0+bool folio_is_longterm_pinnable (struct folio *folio);#else static inline bool folio_is_longterm_pinnable (struct folio *folio) {mm/gup.c +0
-0@@ -2216,6 +2216,37 @@ struct page *get_dump_page(unsigned long addr, int *locked)#ifdef CONFIG_MIGRATION Quoted 9 lines
mm/gup.c struct page *get_dump_page(unsigned long addr, int *locked) · line 2226
Quoted 8 lines
mm/gup.c +8
-0+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;More hugetlb hackery ... don't like that, sorry.
Agree.
Quoted 6 lines
I think this just goes down to a problem we discussed a while ago that is independent of hugetlb: we'd need some way to identify that the the page was actually allocated by the CMA owner, and it's not just some other movable allcoation that ended up there.
Quoted 6 lines
Quoted 6 lines
Quoted 2 lines
For something generic non-hugetlb thing, can't we call into CMA to ask whether the current chunk is marked allocated?
The existing bitmap is set before allocation completes, so the range may still contain borrowed movable pages. A set bit alone cannot establish ownership.
The more accurate code path is as follows.
Assuming the 2M buddy allocation is successful, page P is borrowed from the 1G CMA area.
Now, proceed to request a new 1G huge page:
only_alloc_fresh_hugetlb_folio
--> alloc_gigantic_frozen_folio
--> hugetlb_cma_alloc_frozen_folio
--> cma_range_alloc
/* set bitmap!! */
--> bitmap_set(cmr->bitmap, bitmap_no, bitmap_count);
<time A>
/* may fail */
alloc_contig_frozen_range
/* set HPG_cma!! */
folio_set_hugetlb_cma(folio);At time A, for page P, a bitmap was set, but HPG_cma was not set.
If a concurrent FOLL_LONGTERM pin uses the bitmap alone to determine ownership, it could incorrectly allow P to remain pinned. If alloc_contig_frozen_range() fails, cma_range_alloc() clears the bitmap bits, while the long-term pin on P remains.
HPG_cma is set only after explicit CMA allocation succeeds. It records the folio's own CMA ownership, whereas a set bitmap bit can represent another allocation's in-flight reservation. Therefore, the bitmap alone cannot replace HPG_cma in this check.
Quoted 6 lines
Quoted 4 lines
A generic query may need separate ownership tracking, such as an
owned_bitmaprecording successfully allocated pages. Its updates and lookups would also need to handle concurrency with fast GUP.Or simply two bitmaps? "trying allocation - allocated"
As a fix, perhaps this is a bit too complex? :(
Alternatively, we could adjust the timing for setting the bitmap, though this might introduce new concurrency issues.
Thanks,
Qi
David Hildenbrand (Arm)
maintainerQuoted 7 lines · Qi Zheng
From: Qi Zheng <[email protected]>
Hi all,
This series fixes three CMA-related issues in HugeTLB reservation, long-term pinning and allocation:
Can I hand you over another CMA-related hugetlb problem?
On arm64, we can end up allocating 1GiB and 32MiB hugetlb folios from the cma region.
Doing that dynamically is not a problem. Doing it during boot doesn't quite work.
cma_reserve_early() does not handle any kind of alignment.
So if you allocate a 32M folio followed by a 1G folio, the 1G folio will suddenly start at offset 32M, not being naturally aligned in physical address space.
Likely cma_reserve_early() must be taught to align (and maybe consume an order instead of a size), similar to how the dynamic cma allocations work.
Signature · 3 lines
Cheers, David
Frank van der Linden
Reviewed-byRe: [PATCH v3 2/3] mm: gup: allow long-term pins of CMA-allocated HugeTLB folios
In reply to Qi ZhengQuoted 178 lines · Qi Zheng
Hi David,
Quoted 66 lines · David Hildenbrand (Arm)
Quoted 65 lines · Qi Zheng
Quoted 62 lines · David Hildenbrand (Arm)
Quoted 48 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_cmato be pinned directly, including inZONE_MOVABLE.Fixes cf11e85fc08c ("mm: hugetlb: optionally allocate gigantic hugepages using cma")Suggested-by Muchun Song <[email protected]>Signed-off-by Qi Zheng <[email protected]>Acked-by Muchun Song <[email protected]>Reviewed-by Oscar Salvador <[email protected]>Reviewed-by Jose A. Perez de Azpillaga <[email protected]>--- include/linux/mm.h | 29 +---------------------------- mm/gup.c | 31 +++++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 28 deletions(-)include/linux/mm.h +0
-18@@ -2975,35 +2975,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; - -/* include/linux/mm.h static inline bool is_zero_folio(const struct folio *folio) · line 3006
Quoted 10 lines
include/linux/mm.h +0
-10-* 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); - -}Moving code while adding new stuff to it, please don't.
Quoted 7 lines
Quoted 5 lines
Quoted 4 lines
Move code first, then modify it.
I don't want to play "find the difference between two pictures" on a Saturday evening.
You resent your series way too fast.
Got it.
Quoted 9 lines
Now we have discussions in to threads, congratulations ;)
Quoted 4 lines
;), got it.
I moved the code to resolve a build dependency.
Why can't that be resolved differently? What is the dependency?
The dependency is that the new check wants to use
folio_test_hugetlb_cma(), which is a HugeTLB-specific inline helper frominclude/linux/hugetlb.h.
folio_is_longterm_pinnable()currently lives ininclude/linux/mm.h, whileinclude/linux/hugetlb.halready includeslinux/mm.h. So I can't simply usefolio_test_hugetlb_cma()frommm.hQuoted 32 lines
Quoted 32 lines
Quoted 14 lines
Quoted 14 lines
include/linux/mm.h +1
-0+bool folio_is_longterm_pinnable (struct folio *folio);#else static inline bool folio_is_longterm_pinnable (struct folio *folio) {mm/gup.c +0
-0@@ -2216,6 +2216,37 @@ struct page *get_dump_page(unsigned long addr, int *locked)#ifdef CONFIG_MIGRATION Quoted 9 lines
mm/gup.c struct page *get_dump_page(unsigned long addr, int *locked) · line 2226
Quoted 8 lines
mm/gup.c +8
-0+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;More hugetlb hackery ... don't like that, sorry.
Agree.
Quoted 6 lines
I think this just goes down to a problem we discussed a while ago that is independent of hugetlb: we'd need some way to identify that the the page was actually allocated by the CMA owner, and it's not just some other movable allcoation that ended up there.
Quoted 6 lines
Quoted 6 lines
Quoted 2 lines
For something generic non-hugetlb thing, can't we call into CMA to ask whether the current chunk is marked allocated?
The existing bitmap is set before allocation completes, so the range may still contain borrowed movable pages. A set bit alone cannot establish ownership.
The more accurate code path is as follows.
Assuming the 2M buddy allocation is successful, page P is borrowed from the 1G CMA area.
Now, proceed to request a new 1G huge page:
only_alloc_fresh_hugetlb_folio
-->alloc_gigantic_frozen_folio--> hugetlb_cma_alloc_frozen_folio --> cma_range_alloc /* set bitmap!! */ --> bitmap_set(cmr->bitmap, bitmap_no, bitmap_count); <time A> /* may fail */ alloc_contig_frozen_range /* set HPG_cma!! */ folio_set_hugetlb_cma(folio);At time A, for page P, a bitmap was set, but
HPG_cmawas not set.If a concurrent
FOLL_LONGTERMpin uses the bitmap alone to determine ownership, it could incorrectly allow P to remain pinned. Ifalloc_contig_frozen_range()fails,cma_range_alloc()clears the bitmap bits, while the long-term pin on P remains.
HPG_cmais set only after explicit CMA allocation succeeds. It records the folio's own CMA ownership, whereas a set bitmap bit can represent another allocation's in-flight reservation. Therefore, the bitmap alone cannot replaceHPG_cmain this check.Quoted 6 lines
Quoted 4 lines
A generic query may need separate ownership tracking, such as an
owned_bitmaprecording successfully allocated pages. Its updates and lookups would also need to handle concurrency with fast GUP.Or simply two bitmaps? "trying allocation - allocated"
Quoted 7 lines
As a fix, perhaps this is a bit too complex? :(
Alternatively, we could adjust the timing for setting the bitmap, though this might introduce new concurrency issues.
Thanks,
Qi
Yeah, ideally there would be a way to identify folios allocated through cma_alloc. But that would probably require.. dare I say.. a page flag. Trying to check bitmaps does seem too complex.
And, since we've had a local change that does exactly the same thing for a few years (which I apparently forgot to send along with other hugetlb_cma changes), I can confirm that this looks good to me and works. So,
Hi David,
Quoted 14 lines · David Hildenbrand (Arm)
Quoted 7 lines · Qi Zheng
From: Qi Zheng <[email protected]>
Hi all,
This series fixes three CMA-related issues in HugeTLB reservation, long-term pinning and allocation:
Can I hand you over another CMA-related hugetlb problem?
On arm64, we can end up allocating 1GiB and 32MiB hugetlb folios from the cma region.
Quoted 6 lines
Doing that dynamically is not a problem. Doing it during boot doesn't quite work.
cma_reserve_early()does not handle any kind of alignment.So if you allocate a 32M folio followed by a 1G folio, the 1G folio will suddenly start at offset 32M, not being naturally aligned in physical address space.
Yes.
Quoted 3 lines
Likely
cma_reserve_early()must be taught to align (and maybe consume an order instead of a size), similar to how the dynamic cma allocations work.
However, simply adding alignment would leave gaps that the current activation code cannot distinguish from actual reservations.
The early allocation path would become:
hugetlb_cma_alloc_bootmem()
-> cma_reserve_early()
pfn = ALIGN(cmr->early_pfn, nr_pages);
cmr->early_pfn = pfn + nr_pages;
cma->available_count -= nr_pages;For a CMA range starting at 4 GiB, reserving 32 MiB followed by 1 GiB would produce:
4 GiB 4 GiB + 32 MiB 5 GiB 6 GiB
| 32 MiB used |------ 992 MiB gap ------| 1 GiB used |
^
early_pfnLater, cma_activate_area() marks the entire prefix below early_pfn as allocated, including the gap:
count = early_pfn[r] - cmr->base_pfn; bitmap_count = cma_bitmap_pages_to_bits(cma, count); bitmap_set(cmr->bitmap, 0, bitmap_count);
It only initializes and frees pageblocks after that cursor:
for (pfn = early_pfn[r]; pfn < cmr->base_pfn + cmr->count;
pfn += pageblock_nr_pages)
init_cma_reserved_pageblock(pfn_to_page(pfn));The gap therefore remains reserved and marked allocated. Releasing the two HugeTLB folios only clears their own ranges through cma_release_frozen(); neither release covers the gap. Those 992 MiB would be permanently lost from usable CMA capacity.
So we would also need to track the gaps, clear their bitmap bits during activation, and initialize them as free CMA pageblocks.
Quoted 1 line
Another possibility would be to give 32 MiB pages their own pool, similar to the separate PMD-sized pool proposed in the other series. On arm64 with 4 KiB base pages, 2 MiB, 32 MiB and 1 GiB allocations would then use separate pools.
This could work during early boot too: hugetlb_cma_reserve() establishes the CMA areas and their ranges before hugetlb_bootmem_alloc() runs. Although the bitmaps are allocated later, each range already has its own early_pfn. With naturally aligned ranges and one allocation size per pool, the cursors would stay aligned without creating gaps.
What do you think?
Thanks,
Qi
Re: [PATCH v3 2/3] mm: gup: allow long-term pins of CMA-allocated HugeTLB folios
In reply to Frank van der LindenQuoted 184 lines · Frank van der Linden
Quoted 183 lines · Qi Zheng
Hi David,
Quoted 66 lines · David Hildenbrand (Arm)
Quoted 65 lines · Qi Zheng
Quoted 62 lines · David Hildenbrand (Arm)
Quoted 48 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_cmato be pinned directly, including inZONE_MOVABLE.Fixes cf11e85fc08c ("mm: hugetlb: optionally allocate gigantic hugepages using cma")Suggested-by Muchun Song <[email protected]>Signed-off-by Qi Zheng <[email protected]>Acked-by Muchun Song <[email protected]>Reviewed-by Oscar Salvador <[email protected]>Reviewed-by Jose A. Perez de Azpillaga <[email protected]>--- include/linux/mm.h | 29 +---------------------------- mm/gup.c | 31 +++++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 28 deletions(-)include/linux/mm.h +0
-18@@ -2975,35 +2975,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; - -/* include/linux/mm.h static inline bool is_zero_folio(const struct folio *folio) · line 3006
Quoted 10 lines
include/linux/mm.h +0
-10-* 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); - -}Moving code while adding new stuff to it, please don't.
Quoted 7 lines
Quoted 5 lines
Quoted 4 lines
Move code first, then modify it.
I don't want to play "find the difference between two pictures" on a Saturday evening.
You resent your series way too fast.
Got it.
Quoted 9 lines
Now we have discussions in to threads, congratulations ;)
Quoted 4 lines
;), got it.
I moved the code to resolve a build dependency.
Why can't that be resolved differently? What is the dependency?
The dependency is that the new check wants to use
folio_test_hugetlb_cma(), which is a HugeTLB-specific inline helper frominclude/linux/hugetlb.h.
folio_is_longterm_pinnable()currently lives ininclude/linux/mm.h, whileinclude/linux/hugetlb.halready includeslinux/mm.h. So I can't simply usefolio_test_hugetlb_cma()frommm.hQuoted 32 lines
Quoted 32 lines
Quoted 14 lines
Quoted 14 lines
include/linux/mm.h +1
-0+bool folio_is_longterm_pinnable (struct folio *folio);#else static inline bool folio_is_longterm_pinnable (struct folio *folio) {mm/gup.c +0
-0@@ -2216,6 +2216,37 @@ struct page *get_dump_page(unsigned long addr, int *locked)#ifdef CONFIG_MIGRATION Quoted 9 lines
mm/gup.c struct page *get_dump_page(unsigned long addr, int *locked) · line 2226
Quoted 8 lines
mm/gup.c +8
-0+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;More hugetlb hackery ... don't like that, sorry.
Agree.
Quoted 6 lines
I think this just goes down to a problem we discussed a while ago that is independent of hugetlb: we'd need some way to identify that the the page was actually allocated by the CMA owner, and it's not just some other movable allcoation that ended up there.
Quoted 6 lines
Quoted 6 lines
Quoted 2 lines
For something generic non-hugetlb thing, can't we call into CMA to ask whether the current chunk is marked allocated?
The existing bitmap is set before allocation completes, so the range may still contain borrowed movable pages. A set bit alone cannot establish ownership.
The more accurate code path is as follows.
Assuming the 2M buddy allocation is successful, page P is borrowed from the 1G CMA area.
Now, proceed to request a new 1G huge page:
only_alloc_fresh_hugetlb_folio
-->alloc_gigantic_frozen_folio--> hugetlb_cma_alloc_frozen_folio --> cma_range_alloc /* set bitmap!! */ --> bitmap_set(cmr->bitmap, bitmap_no, bitmap_count); <time A> /* may fail */ alloc_contig_frozen_range /* set HPG_cma!! */ folio_set_hugetlb_cma(folio);At time A, for page P, a bitmap was set, but
HPG_cmawas not set.If a concurrent
FOLL_LONGTERMpin uses the bitmap alone to determine ownership, it could incorrectly allow P to remain pinned. Ifalloc_contig_frozen_range()fails,cma_range_alloc()clears the bitmap bits, while the long-term pin on P remains.
HPG_cmais set only after explicit CMA allocation succeeds. It records the folio's own CMA ownership, whereas a set bitmap bit can represent another allocation's in-flight reservation. Therefore, the bitmap alone cannot replaceHPG_cmain this check.Quoted 6 lines
Quoted 4 lines
A generic query may need separate ownership tracking, such as an
owned_bitmaprecording successfully allocated pages. Its updates and lookups would also need to handle concurrency with fast GUP.Or simply two bitmaps? "trying allocation - allocated"
As a fix, perhaps this is a bit too complex? :(
Alternatively, we could adjust the timing for setting the bitmap, though this might introduce new concurrency issues.
Quoted 6 lines
Quoted 2 lines
Thanks,
QiYeah, ideally there would be a way to identify folios allocated through
cma_alloc. But that would probably require.. dare I say.. a page flag. Trying to check bitmaps does seem too complex.
Agree.
Quoted 7 lines
And, since we've had a local change that does exactly the same thing for a few years (which I apparently forgot to send along with other
hugetlb_cmachanges), I can confirm that this looks good to me and works. So,Reviewed-by Frank van der Linden <[email protected]>
Thanks for your review and confirmation!
David Hildenbrand (Arm)
maintainerRe: [PATCH v3 2/3] mm: gup: allow long-term pins of CMA-allocated HugeTLB folios
In reply to Frank van der LindenQuoted 68 lines · Frank van der Linden
Quoted 67 lines · Qi Zheng
Hi David,
Quoted 2 lines · David Hildenbrand (Arm)
You resent your series way too fast.
Got it.
Quoted 5 lines
Now we have discussions in to threads, congratulations ;)
Why can't that be resolved differently? What is the dependency?
The dependency is that the new check wants to use
folio_test_hugetlb_cma(), which is a HugeTLB-specific inline helper frominclude/linux/hugetlb.h.
folio_is_longterm_pinnable()currently lives ininclude/linux/mm.h, whileinclude/linux/hugetlb.halready includeslinux/mm.h. So I can't simply usefolio_test_hugetlb_cma()frommm.hQuoted 1 line
The more accurate code path is as follows.
Assuming the 2M buddy allocation is successful, page P is borrowed from the 1G CMA area.
Now, proceed to request a new 1G huge page:
only_alloc_fresh_hugetlb_folio
-->alloc_gigantic_frozen_folio--> hugetlb_cma_alloc_frozen_folio --> cma_range_alloc /* set bitmap!! */ --> bitmap_set(cmr->bitmap, bitmap_no, bitmap_count); <time A> /* may fail */ alloc_contig_frozen_range /* set HPG_cma!! */ folio_set_hugetlb_cma(folio);At time A, for page P, a bitmap was set, but
HPG_cmawas not set.If a concurrent
FOLL_LONGTERMpin uses the bitmap alone to determine ownership, it could incorrectly allow P to remain pinned. Ifalloc_contig_frozen_range()fails,cma_range_alloc()clears the bitmap bits, while the long-term pin on P remains.
HPG_cmais set only after explicit CMA allocation succeeds. It records the folio's own CMA ownership, whereas a set bitmap bit can represent another allocation's in-flight reservation. Therefore, the bitmap alone cannot replaceHPG_cmain this check.Quoted 2 lines
Or simply two bitmaps? "trying allocation - allocated"
As a fix, perhaps this is a bit too complex? :(
Alternatively, we could adjust the timing for setting the bitmap, though this might introduce new concurrency issues.
Quoted 6 lines
Quoted 2 lines
Thanks,
QiYeah, ideally there would be a way to identify folios allocated through
cma_alloc. But that would probably require.. dare I say.. a page flag. Trying to check bitmaps does seem too complex.
Why?
You know it's a CMA region. And CMA regions are limited and static. Bitmap check is cheap.
Pageflag likely won't happen ...
In general, please, it's ok to send some hugetlb hacks (that make me angry) as a quick fix, but please do think about a proper way of handling that.
Signature · 3 lines
Cheers, David
David Hildenbrand (Arm)
maintainerRe: [PATCH v3 2/3] mm: gup: allow long-term pins of CMA-allocated HugeTLB folios
In reply to Qi ZhengQuoted 22 lines · Qi Zheng
Hi David,
Quoted 6 lines · David Hildenbrand (Arm)
Quoted 3 lines · Qi Zheng
You resent your series way too fast.
Got it.
Quoted 9 lines
Now we have discussions in to threads, congratulations ;)
Quoted 4 lines
;), got it.
I moved the code to resolve a build dependency.
Why can't that be resolved differently? What is the dependency?
Quoted 7 lines
The dependency is that the new check wants to use
folio_test_hugetlb_cma(), which is a HugeTLB-specific inline helper frominclude/linux/hugetlb.h.
folio_is_longterm_pinnable()currently lives ininclude/linux/mm.h, whileinclude/linux/hugetlb.halready includeslinux/mm.h. So I can't simply usefolio_test_hugetlb_cma()frommm.h
It's all so hacky. :)
core-mm shouldn't have to worry about hugetlb internals like folio_test_hugetlb_cma().
Quoted 11 lines
Quoted 6 lines
Quoted 4 lines
A generic query may need separate ownership tracking, such as an
owned_bitmaprecording successfully allocated pages. Its updates and lookups would also need to handle concurrency with fast GUP.Or simply two bitmaps? "trying allocation - allocated"
As a fix, perhaps this is a bit too complex? :(
Alternatively, we could adjust the timing for setting the bitmap, though this might introduce new concurrency issues.
I think we had that order reversed in the past. Likely a second bitmap would allow us to fix it fairly easily.
Signature · 3 lines
Cheers, David
Quoted 77 lines · Qi Zheng
Hi David,
Quoted 14 lines · David Hildenbrand (Arm)
Quoted 7 lines · Qi Zheng
From: Qi Zheng <[email protected]>
Hi all,
This series fixes three CMA-related issues in HugeTLB reservation, long-term pinning and allocation:
Can I hand you over another CMA-related hugetlb problem?
On arm64, we can end up allocating 1GiB and 32MiB hugetlb folios from the cma region.
Quoted 6 lines
Doing that dynamically is not a problem. Doing it during boot doesn't quite work.
cma_reserve_early()does not handle any kind of alignment.So if you allocate a 32M folio followed by a 1G folio, the 1G folio will suddenly start at offset 32M, not being naturally aligned in physical address space.
Yes.
Quoted 3 lines
Likely
cma_reserve_early()must be taught to align (and maybe consume an order instead of a size), similar to how the dynamic cma allocations work.However, simply adding alignment would leave gaps that the current activation code cannot distinguish from actual reservations.
The early allocation path would become:
hugetlb_cma_alloc_bootmem()-> cma_reserve_early() pfn = ALIGN(cmr->early_pfn, nr_pages); cmr->early_pfn = pfn + nr_pages; cma->available_count -= nr_pages;For a CMA range starting at 4 GiB, reserving 32 MiB followed by 1 GiB would produce:
4 GiB 4 GiB + 32 MiB 5 GiB 6 GiB
| 32 MiB used |------ 992 MiB gap ------| 1 GiB used |^ early_pfnLater,
cma_activate_area()marks the entire prefix belowearly_pfnas allocated, including the gap:count = early_pfn[r] -
cmr->base_pfn;bitmap_count=cma_bitmap_pages_to_bits(cma, count);bitmap_set(cmr->bitmap, 0, bitmap_count);It only initializes and frees pageblocks after that cursor:
for (pfn = early_pfn[r]; pfn < cmr->base_pfn + cmr->count; pfn += pageblock_nr_pages) init_cma_reserved_pageblock(pfn_to_page(pfn));The gap therefore remains reserved and marked allocated. Releasing the two HugeTLB folios only clears their own ranges through
cma_release_frozen(); neither release covers the gap. Those 992 MiB would be permanently lost from usable CMA capacity.So we would also need to track the gaps, clear their bitmap bits during activation, and initialize them as free CMA pageblocks.
Quoted 1 line
Another possibility would be to give 32 MiB pages their own pool, similar to the separate PMD-sized pool proposed in the other series. On arm64 with 4 KiB base pages, 2 MiB, 32 MiB and 1 GiB allocations would then use separate pools.
Quoted 7 lines
This could work during early boot too:
hugetlb_cma_reserve()establishes the CMA areas and their ranges beforehugetlb_bootmem_alloc()runs. Although the bitmaps are allocated later, each range already has its ownearly_pfn. With naturally aligned ranges and one allocation size per pool, the cursors would stay aligned without creating gaps.What do you think?
Hi David, gentle ping.
I’d like to update to v5 so that I can continue advancing the support for 2M hugetlb CMA. Regarding this issue, would you like me to include a fix in v5? If you agree with the "split pool" approach, we can handle it as part of the patch set for 2M hugetlb CMA support.
Thanks,
Qi
Quoted 3 lines
Thanks,
Qi
David Hildenbrand (Arm)
maintainerHi,
sorry somehow I missed your mail.
Quoted 37 lines
Quoted 37 lines
Quoted 4 lines
Likely
cma_reserve_early()must be taught to align (and maybe consume an order instead of a size), similar to how the dynamic cma allocations work.However, simply adding alignment would leave gaps that the current activation code cannot distinguish from actual reservations.
The early allocation path would become:
hugetlb_cma_alloc_bootmem()-> cma_reserve_early() pfn = ALIGN(cmr->early_pfn, nr_pages); cmr->early_pfn = pfn + nr_pages; cma->available_count -= nr_pages;For a CMA range starting at 4 GiB, reserving 32 MiB followed by 1 GiB would produce:
4 GiB 4 GiB + 32 MiB 5 GiB 6 GiB
| 32 MiB used |------ 992 MiB gap ------| 1 GiB used |^ early_pfnLater,
cma_activate_area()marks the entire prefix belowearly_pfnas allocated, including the gap:count = early_pfn[r] -
cmr->base_pfn;bitmap_count=cma_bitmap_pages_to_bits(cma, count);bitmap_set(cmr->bitmap, 0, bitmap_count);It only initializes and frees pageblocks after that cursor:
for (pfn = early_pfn[r]; pfn < cmr->base_pfn + cmr->count; pfn += pageblock_nr_pages) init_cma_reserved_pageblock(pfn_to_page(pfn));
Quoted 7 lines
Quoted 7 lines
The gap therefore remains reserved and marked allocated. Releasing the two HugeTLB folios only clears their own ranges through
cma_release_frozen(); neither release covers the gap. Those 992 MiB would be permanently lost from usable CMA capacity.So we would also need to track the gaps, clear their bitmap bits during activation, and initialize them as free CMA pageblocks.
Ugh, what a hacky mess people created.
Quoted 7 lines
Quoted 7 lines
Quoted 2 lines
Another possibility would be to give 32 MiB pages their own pool, similar to the separate PMD-sized pool proposed in the other series. On arm64 with 4 KiB base pages, 2 MiB, 32 MiB and 1 GiB allocations would then use separate pools.
That would be cleanest. Would it break existing setups?
Another quick-fix idea could be to always allocate larger folios first.
That way we should avoid any holes, right>
Quoted 7 lines
Quoted 7 lines
This could work during early boot too:
hugetlb_cma_reserve()establishes the CMA areas and their ranges beforehugetlb_bootmem_alloc()runs. Although the bitmaps are allocated later, each range already has its ownearly_pfn. With naturally aligned ranges and one allocation size per pool, the cursors would stay aligned without creating gaps.
Quoted 5 lines
Quoted 1 line
What do you think?
Hi David, gentle ping
I’d like to update to v5 so that I can continue advancing the support
Feel free to fix this issue here independently from the current series.
Quoted 3 lines
for 2M hugetlb CMA. Regarding this issue, would you like me to include a fix in v5? If you agree with the "split pool" approach, we can handle it as part of the patch set for 2M hugetlb CMA support.
I think we want a stable-first fix for that.
Signature · 3 lines
Cheers, David
Hi David,
Quoted 56 lines · David Hildenbrand (Arm)
Hi,
sorry somehow I missed your mail.
Quoted 37 lines
Quoted 37 lines
Quoted 4 lines
Likely
cma_reserve_early()must be taught to align (and maybe consume an order instead of a size), similar to how the dynamic cma allocations work.However, simply adding alignment would leave gaps that the current activation code cannot distinguish from actual reservations.
The early allocation path would become:
hugetlb_cma_alloc_bootmem()-> cma_reserve_early() pfn = ALIGN(cmr->early_pfn, nr_pages); cmr->early_pfn = pfn + nr_pages; cma->available_count -= nr_pages;For a CMA range starting at 4 GiB, reserving 32 MiB followed by 1 GiB would produce:
4 GiB 4 GiB + 32 MiB 5 GiB 6 GiB
| 32 MiB used |------ 992 MiB gap ------| 1 GiB used |^ early_pfnLater,
cma_activate_area()marks the entire prefix belowearly_pfnas allocated, including the gap:count = early_pfn[r] -
cmr->base_pfn;bitmap_count=cma_bitmap_pages_to_bits(cma, count);bitmap_set(cmr->bitmap, 0, bitmap_count);It only initializes and frees pageblocks after that cursor:
for (pfn = early_pfn[r]; pfn < cmr->base_pfn + cmr->count; pfn += pageblock_nr_pages) init_cma_reserved_pageblock(pfn_to_page(pfn));Quoted 7 lines
Quoted 7 lines
The gap therefore remains reserved and marked allocated. Releasing the two HugeTLB folios only clears their own ranges through
cma_release_frozen(); neither release covers the gap. Those 992 MiB would be permanently lost from usable CMA capacity.So we would also need to track the gaps, clear their bitmap bits during activation, and initialize them as free CMA pageblocks.
Ugh, what a hacky mess people created.
Quoted 3 lines
Quoted 3 lines
Quoted 2 lines
Quoted 6 lines
Quoted 4 lines
Quoted 4 lines
Another possibility would be to give 32 MiB pages their own pool, similar to the separate PMD-sized pool proposed in the other series. On arm64 with 4 KiB base pages, 2 MiB, 32 MiB and 1 GiB allocations would then use separate pools.
That would be cleanest. Would it break existing setups?
Yes, this requires an existing user to explicitly configure a 32M memory pool.
But in practice there shouldn't be such a user today, otherwise the misalignment issue would have surfaced long ago, wouldn't it?
Quoted 5 lines
Another quick-fix idea could be to always allocate larger folios first.
That way we should avoid any holes, right>
Ah, this looks feasible. How about the following?
(If that looks good, this could go as a stable-first fix.)
mm/hugetlb.c +27 -1
@@ -4556,6 +4556,22 @@ void __init hugetlb_bootmem_set_nodes(void)node_set (nid, hugetlb_bootmem_nodes); } +/* Return the largest early-CMA hstate below the given order. */ +static struct hstate *__init +hugetlb_next_early_cma_hstate (unsigned int order) +{ +struct hstate *h, *next =NULL ; + +for_each_hstate (h) { +if (!hugetlb_early_cma (h) ||huge_page_order (h) >= order) +continue ; +if (!next ||huge_page_order (h) >huge_page_order (next)) + next = h; + } + +return next; +} +void __init hugetlb_bootmem_alloc (void ) {struct hstate *h;
@@ -4571,9 +4587,19 @@ void __init hugetlb_bootmem_alloc(void)for_each_hstate (h) { h->next_nid_to_alloc = first_online_node; -if (hstate_is_gigantic (h)) +if (hstate_is_gigantic (h) && !hugetlb_early_cma (h))hugetlb_hstate_alloc_pages (h); } + +/* +* CMA ranges are aligned to the largest supported size. Allocate larger +* sizes first to keep the early reservation cursor aligned without gaps. +*/ + h =hugetlb_next_early_cma_hstate (UINT_MAX ); +while (h) { +hugetlb_hstate_alloc_pages (h); + h =hugetlb_next_early_cma_hstate (huge_page_order (h)); + } }/*
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
This could work during early boot too:
hugetlb_cma_reserve()establishes the CMA areas and their ranges beforehugetlb_bootmem_alloc()runs. Although the bitmaps are allocated later, each range already has its ownearly_pfn. With naturally aligned ranges and one allocation size per pool, the cursors would stay aligned without creating gaps.
Quoted 7 lines
Quoted 5 lines
Quoted 1 line
What do you think?
Hi David, gentle ping
I’d like to update to v5 so that I can continue advancing the support
Feel free to fix this issue here independently from the current series.
OK. I'll go ahead and post the v5.
Quoted 7 lines
Quoted 4 lines
for 2M hugetlb CMA. Regarding this issue, would you like me to include a fix in v5? If you agree with the "split pool" approach, we can handle it as part of the patch set for 2M hugetlb CMA support.
I think we want a stable-first fix for that.
Got it.
Thanks,
Qi
Quoted 2 lines
David Hildenbrand (Arm)
maintainerQuoted 69 lines · Qi Zheng
Hi David,
Quoted 11 lines · David Hildenbrand (Arm)
Hi,
sorry somehow I missed your mail.
Ugh, what a hacky mess people created.
That would be cleanest. Would it break existing setups?
Yes, this requires an existing user to explicitly configure a 32M memory pool.
But in practice there shouldn't be such a user today, otherwise the misalignment issue would have surfaced long ago, wouldn't it?
Quoted 5 lines
Another quick-fix idea could be to always allocate larger folios first.
That way we should avoid any holes, right>
Ah, this looks feasible. How about the following?
(If that looks good, this could go as a stable-first fix.)
mm/hugetlb.c +20
-1@@ -4556,6 +4556,22 @@ void __init hugetlb_bootmem_set_nodes(void)node_set (nid, hugetlb_bootmem_nodes); } +/* Return the largest early-CMA hstate below the given order. */ +static struct hstate *__init +hugetlb_next_early_cma_hstate (unsigned int order) +{ +struct hstate *h, *next =NULL ; + +for_each_hstate (h) { +if (!hugetlb_early_cma (h) ||huge_page_order (h) >= order) +continue ; +if (!next ||huge_page_order (h) >huge_page_order (next)) + next = h; + } + +return next; +} +void __init hugetlb_bootmem_alloc (void ) {struct hstate *h;@@ -4571,9 +4587,19 @@ void __init hugetlb_bootmem_alloc(void)for_each_hstate (h) { h->next_nid_to_alloc = first_online_node; -if (hstate_is_gigantic (h)) +if (hstate_is_gigantic (h) && !hugetlb_early_cma (h))hugetlb_hstate_alloc_pages (h); } + +/* +* CMA ranges are aligned to the largest supported size. Allocate larger
mm/hugetlb.c void __init hugetlb_bootmem_alloc(void) · line 4605
Quoted 10 lines
mm/hugetlb.c +7
-0+* sizes first to keep the early reservation cursor aligned without gaps. +*/ + h =hugetlb_next_early_cma_hstate (UINT_MAX ); +while (h) { +hugetlb_hstate_alloc_pages (h); + h =hugetlb_next_early_cma_hstate (huge_page_order (h)); + } }/*
I think this should do. Best to send out a proper patch.
Signature · 3 lines
Cheers, David
Quoted 79 lines · David Hildenbrand (Arm)
Quoted 78 lines · Qi Zheng
Hi David,
Quoted 11 lines · David Hildenbrand (Arm)
Hi,
sorry somehow I missed your mail.
Ugh, what a hacky mess people created.
That would be cleanest. Would it break existing setups?
Yes, this requires an existing user to explicitly configure a 32M memory pool.
But in practice there shouldn't be such a user today, otherwise the misalignment issue would have surfaced long ago, wouldn't it?
Quoted 5 lines
Another quick-fix idea could be to always allocate larger folios first.
That way we should avoid any holes, right>
Ah, this looks feasible. How about the following?
(If that looks good, this could go as a stable-first fix.)
mm/hugetlb.c +27
-1@@ -4556,6 +4556,22 @@ void __init hugetlb_bootmem_set_nodes(void)node_set (nid, hugetlb_bootmem_nodes); } +/* Return the largest early-CMA hstate below the given order. */ +static struct hstate *__init +hugetlb_next_early_cma_hstate (unsigned int order) +{ +struct hstate *h, *next =NULL ; + +for_each_hstate (h) { +if (!hugetlb_early_cma (h) ||huge_page_order (h) >= order) +continue ; +if (!next ||huge_page_order (h) >huge_page_order (next)) + next = h; + } + +return next; +} +void __init hugetlb_bootmem_alloc (void ) {struct hstate *h;@@ -4571,9 +4587,19 @@ void __init hugetlb_bootmem_alloc(void)for_each_hstate (h) { h->next_nid_to_alloc = first_online_node; -if (hstate_is_gigantic (h)) +if (hstate_is_gigantic (h) && !hugetlb_early_cma (h))hugetlb_hstate_alloc_pages (h); } + +/* +* CMA ranges are aligned to the largest supported size. Allocate larger +* sizes first to keep the early reservation cursor aligned without gaps. +*/ + h =hugetlb_next_early_cma_hstate (UINT_MAX ); +while (h) { +hugetlb_hstate_alloc_pages (h); + h =hugetlb_next_early_cma_hstate (huge_page_order (h)); + } }
Quoted 3 lines
mm/hugetlb.c void __init hugetlb_bootmem_alloc(void) · line 4605
Quoted 1 line
mm/hugetlb.c +0
-0/* I think this should do. Best to send out a proper patch.
Okay, will post a proper patch.
Thanks,
Qi