[PATCH] mm: free HIGHATOMIC and CMA frozen pages via buddy

zhaoyang.huang posted 1 patch 2 weeks ago
mm/page_alloc.c | 32 ++++++++++++++++----------------
1 file changed, 16 insertions(+), 16 deletions(-)
[PATCH] mm: free HIGHATOMIC and CMA frozen pages via buddy
Posted by zhaoyang.huang 2 weeks ago
From: Zhaoyang Huang <zhaoyang.huang@unisoc.com>

Pages in pageblocks above MIGRATE_PCPTYPES were remapped to
MIGRATE_MOVABLE on the per-CPU lists, so order-9 GFP_ATOMIC
allocations could consume CMA physical blocks which may lead to
cma_alloc failed.
This commit would like to free them through free_one_page() simply.

Signed-off-by: Zhaoyang Huang <zhaoyang.huang@unisoc.com>
---
 mm/page_alloc.c | 32 ++++++++++++++++----------------
 1 file changed, 16 insertions(+), 16 deletions(-)

diff --git a/mm/page_alloc.c b/mm/page_alloc.c
index 65e205111553..94b79db8da2f 100644
--- a/mm/page_alloc.c
+++ b/mm/page_alloc.c
@@ -2945,19 +2945,15 @@ static void __free_frozen_pages(struct page *page, unsigned int order,
 
 	/*
 	 * We only track unmovable, reclaimable and movable on pcp lists.
-	 * Place ISOLATE pages on the isolated list because they are being
-	 * offlined but treat HIGHATOMIC and CMA as movable pages so we can
-	 * get those areas back if necessary. Otherwise, we may have to free
-	 * excessively into the page allocator
+	 * ISOLATE, HIGHATOMIC and CMA pageblocks are freed directly to the
+	 * buddy allocator so their migratetype is not misrouted on PCP lists
+	 * (e.g. order-9 pages must not share the MIGRATE_MOVABLE THP list).
 	 */
 	zone = page_zone(page);
 	migratetype = get_pfnblock_migratetype(page, pfn);
 	if (unlikely(migratetype >= MIGRATE_PCPTYPES)) {
-		if (unlikely(is_migrate_isolate(migratetype))) {
-			free_one_page(zone, page, pfn, order, fpi_flags);
-			return;
-		}
-		migratetype = MIGRATE_MOVABLE;
+		free_one_page(zone, page, pfn, order, fpi_flags);
+		return;
 	}
 
 	if (unlikely((fpi_flags & FPI_TRYLOCK) && IS_ENABLED(CONFIG_PREEMPT_RT)
@@ -3029,6 +3025,17 @@ void free_unref_folios(struct folio_batch *folios)
 		folio->private = NULL;
 		migratetype = get_pfnblock_migratetype(&folio->page, pfn);
 
+		if (unlikely(!is_migrate_isolate(migratetype) &&
+			     migratetype >= MIGRATE_PCPTYPES)) {
+			if (pcp) {
+				pcp_spin_unlock(pcp);
+				locked_zone = NULL;
+				pcp = NULL;
+			}
+			free_one_page(zone, &folio->page, pfn, order, FPI_NONE);
+			continue;
+		}
+
 		/* Different zone requires a different pcp lock */
 		if (zone != locked_zone ||
 		    is_migrate_isolate(migratetype)) {
@@ -3061,13 +3068,6 @@ void free_unref_folios(struct folio_batch *folios)
 			locked_zone = zone;
 		}
 
-		/*
-		 * Non-isolated types over MIGRATE_PCPTYPES get added
-		 * to the MIGRATE_MOVABLE pcp list.
-		 */
-		if (unlikely(migratetype >= MIGRATE_PCPTYPES))
-			migratetype = MIGRATE_MOVABLE;
-
 		trace_mm_page_free_batched(&folio->page);
 		if (!free_frozen_page_commit(zone, pcp, &folio->page,
 				migratetype, order, FPI_NONE)) {
-- 
2.25.1
Re: [PATCH] mm: free HIGHATOMIC and CMA frozen pages via buddy
Posted by Andrew Morton 2 weeks ago
On Mon, 25 May 2026 15:10:33 +0800 "zhaoyang.huang" <zhaoyang.huang@unisoc.com> wrote:

> Pages in pageblocks above MIGRATE_PCPTYPES were remapped to
> MIGRATE_MOVABLE on the per-CPU lists, so order-9 GFP_ATOMIC
> allocations could consume CMA physical blocks which may lead to
> cma_alloc failed.
> This commit would like to free them through free_one_page() simply.

Can you please describe the userspace-visible effects of this change? 
Before and after.  And include this description within the patch
changelog.

AI review claims to have found a couple of issues, one of which sounds
serious:
	https://sashiko.dev/#/patchset/20260525071033.1104929-1-zhaoyang.huang@unisoc.com

Thanks.
Re: [PATCH] mm: free HIGHATOMIC and CMA frozen pages via buddy
Posted by Zhaoyang Huang 1 week, 6 days ago
On Tue, May 26, 2026 at 4:15 AM Andrew Morton <akpm@linux-foundation.org> wrote:
>
> On Mon, 25 May 2026 15:10:33 +0800 "zhaoyang.huang" <zhaoyang.huang@unisoc.com> wrote:
>
> > Pages in pageblocks above MIGRATE_PCPTYPES were remapped to
> > MIGRATE_MOVABLE on the per-CPU lists, so order-9 GFP_ATOMIC
> > allocations could consume CMA physical blocks which may lead to
> > cma_alloc failed.
> > This commit would like to free them through free_one_page() simply.
>
> Can you please describe the userspace-visible effects of this change?
> Before and after.  And include this description within the patch
> changelog.
ok. KVM start up failed occasionally observed in our system which is
supposed to be caused by page migration fail
>
> AI review claims to have found a couple of issues, one of which sounds
> serious:
>         https://sashiko.dev/#/patchset/20260525071033.1104929-1-zhaoyang.huang@unisoc.com
yes, that is problem, will fix in v2
>
> Thanks.