Re: [PATCH v4 2/3] mm: gup: allow long-term pins of CMA-allocated HugeTLB folios
From: David Hildenbrand (Arm)
Date: Thu Nov 12 2026 - 04:08:15 EST
On 11/12/26 07:13, Qi Zheng wrote:
>
>
> On 11/11/26 3:42 PM, Muchun Song wrote:
>>
>>
>>>
>>> From: Qi Zheng <zhengqi.arch@xxxxxxxxxxxxx>
>>>
>>> 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.
>
>>
>>> 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")
>>> Cc: stable@xxxxxxxxxxxxxxx
>>> Suggested-by: Muchun Song <muchun.song@xxxxxxxxx>
>>> Signed-off-by: Qi Zheng <zhengqi.arch@xxxxxxxxxxxxx>
>>> Acked-by: Muchun Song <muchun.song@xxxxxxxxx>
>>> Reviewed-by: Oscar Salvador <osalvador@xxxxxxx>
>>> Reviewed-by: Jose A. Perez de Azpillaga <azpijr@xxxxxxxxx>
>>> ---
>>> include/linux/mm.h | 29 +----------------------------
>>> mm/gup.c | 31 +++++++++++++++++++++++++++++++
>>> 2 files changed, 32 insertions(+), 28 deletions(-)
>>>
>>> diff --git a/include/linux/mm.h b/include/linux/mm.h
>>> index b0da9be413098..47d1d96bfc7fd 100644
>>> --- a/include/linux/mm.h
>>> +++ b/include/linux/mm.h
>>> @@ -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?
>
> 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.
diff --git a/include/linux/cma.h b/include/linux/cma.h
index 8555d38a97b1a..d829fd0355bae 100644
--- a/include/linux/cma.h
+++ b/include/linux/cma.h
@@ -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);
diff --git a/include/linux/mm.h b/include/linux/mm.h
index 6af3b7008f1ad..5392e225cab9b 100644
--- a/include/linux/mm.h
+++ b/include/linux/mm.h
@@ -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))
diff --git a/mm/cma.c b/mm/cma.c
index 9ec1d1fd5dff5..b3bf0014a90f5 100644
--- a/mm/cma.c
+++ b/mm/cma.c
@@ -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);
--
2.43.0
In a follow-up cleanup folio_is_longterm_pinnable() can be moved to gup.c.
--
Cheers,
David