From nobody Tue Feb 10 00:22:24 2026 Received: from mail-pj1-f73.google.com (mail-pj1-f73.google.com [209.85.216.73]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 326AF1F63D8 for ; Mon, 27 Jan 2025 23:23:03 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738020185; cv=none; b=lYuz6pUPzVxwSFs5rZoz0YX5tMQKpNzSMLH9JcLJoyinmUia8wPicXVLPxDbrcooO9OIGAnjNPqbYeqJffGEZQZT6EFjPN6T+18GFXukVbWjH1jb+JKlSgXdFZ9s3/jFCs6bF3F2c7g2dhvsbu3r6BfXK1sfyLA6dpFwDAuBWYw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738020185; c=relaxed/simple; bh=9Q+eCfOW4Mn7oZPdRdVi+Z90DvzYvaT8AIBn/ND7VH0=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=diYxlmgwnoSVMf5sjYfjzqfM6jRLtR1qrqSqvEGM+vr4vqPXaI1AVkRNtZUhEMUlaxilzvcRn8h0Lb3sbjISJv54+Dblz02g9aeHIaGHlBb7Yp6wbnBY0RZDZddMwtpdgErzplcv3TtV8oVv/tnKx5ebSvPY6JTTDZ9yEg9heYw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--fvdl.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=Y4evcNYu; arc=none smtp.client-ip=209.85.216.73 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--fvdl.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="Y4evcNYu" Received: by mail-pj1-f73.google.com with SMTP id 98e67ed59e1d1-2ef9dbeb848so9258527a91.0 for ; Mon, 27 Jan 2025 15:23:03 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1738020183; x=1738624983; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=KwINzBek+TuboeY4k9bxwxfZ/O8zkR2bm85x9QvP/04=; b=Y4evcNYuRUkYEOImTC8xBGHunn1UirhETdHxnAfwrbBusqWCn7noNzqBov7c+/Jbmz RsBsmnP7+groAKMReDXzyViH65KgCTGYkkGM1QtZWc5xfPsIXjGA7RKuEM0LLeWsiMOq s0YqDn4w7lksdMXLVSp/j4kY9oVeNsRiJWNW+8nL90Kr8bLpVTJSslU2k4Mzt0QE1qTm W4FvCVyrNXs8kV4mP0Eet3kFohcuBnypMPPnyRhoeFLApgLevPw9YuVM0FO93LXzKc5n xhKNx2pMcW1+nAYXluEZuO7kcsdvTT5M6zlMhLBGzXxxCL2tICjaRZpZJMhk0zZMPVeX Y0vg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1738020183; x=1738624983; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=KwINzBek+TuboeY4k9bxwxfZ/O8zkR2bm85x9QvP/04=; b=RnqQjhA+B7iFEYGTeFUGL2ltN9su0WclXJSJKSX9+cx+jW8/Hv8Je3+FSd7ash65cL jECq95h5wNEgWa3yFlz0sYZG6ofQ0h1k0ya0Qutqnk0UDohCZFeWZHelJ4dKBJrvK2Zh 5+pBBG6NUaITdx4gpm2XTNirFgHPk5ljxFc2rJ2zP+So5UmVThQRiUaLsm6OtAenp0i2 j3OBNlczIIe8UnNw7s8EYA29K5pQT9KtG9m22B3sNDLHj1zpNEEIvuAuCkv/JJox/iHH LFbmRBg4k0Sa3KMAOj1ufVAQ5AqikObs1zuexipgpZ8K0FrcCXoLN/ZWUVZ2lpVo2nds QHsQ== X-Forwarded-Encrypted: i=1; AJvYcCWQjcoruo9vTENuyyVsVxNQ21ElVbwYlLcQ3ecYDLJIGjiFkvtPGRSumfI9/LYHqI8MwhJXXKXiom6ppmA=@vger.kernel.org X-Gm-Message-State: AOJu0Yzsw5q6i+ChTMt1JaJQmpoW+zSAjVDQvsR/MS97UYXugfZbNVCQ ixiG188glt4krZK/7ZDWvA8yfhigaWhkyDATtRTT6V4ISqE4JqAmTB/+U/Q9nmpzZ1WtPA== X-Google-Smtp-Source: AGHT+IF6R6P25DhcwrUq5f/uWfBdxK6gbsckW/gvsb31sSGfRC3rYruHziIqFE9lTFPRi2qCdE0ZlH17 X-Received: from pfat15.prod.google.com ([2002:a05:6a00:aa0f:b0:727:3c81:f42a]) (user=fvdl job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:10d1:b0:72d:a208:d366 with SMTP id d2e1a72fcca58-72dafaa5962mr67617715b3a.20.1738020183373; Mon, 27 Jan 2025 15:23:03 -0800 (PST) Date: Mon, 27 Jan 2025 23:22:05 +0000 In-Reply-To: <20250127232207.3888640-1-fvdl@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250127232207.3888640-1-fvdl@google.com> X-Mailer: git-send-email 2.48.1.262.g85cc9f2d1e-goog Message-ID: <20250127232207.3888640-26-fvdl@google.com> Subject: [PATCH 25/27] mm/cma: introduce interface for early reservations From: Frank van der Linden To: akpm@linux-foundation.org, muchun.song@linux.dev, linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: yuzhao@google.com, usama.arif@bytedance.com, joao.m.martins@oracle.com, roman.gushchin@linux.dev, Frank van der Linden Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" It can be desirable to reserve memory in a CMA area before it is activated, early in boot. Such reservations would effectively be memblock allocations, but they can be returned to the CMA area later. This functionality can be used to allow hugetlb bootmem allocations from a hugetlb CMA area. A new interface, cma_reserve_early is introduced. This allows for pageblock-aligned reservations. These reservations are skipped during the initial handoff of pages in a CMA area to the buddy allocator. The caller is responsible for making sure that the page structures are set up, and that the migrate type is set correctly, as with other memblock allocations that stick around. If the CMA area fails to activate (because it intersects with multiple zones), the reserved memory is not given to the buddy allocator, the caller needs to take care of that. Signed-off-by: Frank van der Linden --- mm/cma.c | 83 ++++++++++++++++++++++++++++++++++++++++++++++----- mm/cma.h | 8 +++++ mm/internal.h | 16 ++++++++++ mm/mm_init.c | 9 ++++++ 4 files changed, 109 insertions(+), 7 deletions(-) diff --git a/mm/cma.c b/mm/cma.c index 41248dee7197..1c0a01d02a28 100644 --- a/mm/cma.c +++ b/mm/cma.c @@ -144,9 +144,10 @@ bool cma_validate_zones(struct cma *cma) =20 static void __init cma_activate_area(struct cma *cma) { - unsigned long pfn, base_pfn; + unsigned long pfn, end_pfn; int allocrange, r; struct cma_memrange *cmr; + unsigned long bitmap_count, count; =20 for (allocrange =3D 0; allocrange < cma->nranges; allocrange++) { cmr =3D &cma->ranges[allocrange]; @@ -161,8 +162,13 @@ static void __init cma_activate_area(struct cma *cma) =20 for (r =3D 0; r < cma->nranges; r++) { cmr =3D &cma->ranges[r]; - base_pfn =3D cmr->base_pfn; - for (pfn =3D base_pfn; pfn < base_pfn + cmr->count; + if (cmr->early_pfn !=3D cmr->base_pfn) { + count =3D cmr->early_pfn - cmr->base_pfn; + bitmap_count =3D cma_bitmap_pages_to_bits(cma, count); + bitmap_set(cmr->bitmap, 0, bitmap_count); + } + + for (pfn =3D cmr->early_pfn; pfn < cmr->base_pfn + cmr->count; pfn +=3D pageblock_nr_pages) init_cma_reserved_pageblock(pfn_to_page(pfn)); } @@ -173,6 +179,7 @@ static void __init cma_activate_area(struct cma *cma) INIT_HLIST_HEAD(&cma->mem_head); spin_lock_init(&cma->mem_head_lock); #endif + set_bit(CMA_ACTIVATED, &cma->flags); =20 return; =20 @@ -184,9 +191,8 @@ static void __init cma_activate_area(struct cma *cma) if (!test_bit(CMA_RESERVE_PAGES_ON_ERROR, &cma->flags)) { for (r =3D 0; r < allocrange; r++) { cmr =3D &cma->ranges[r]; - for (pfn =3D cmr->base_pfn; - pfn < cmr->base_pfn + cmr->count; - pfn++) + end_pfn =3D cmr->base_pfn + cmr->count; + for (pfn =3D cmr->early_pfn; pfn < end_pfn; pfn++) free_reserved_page(pfn_to_page(pfn)); } } @@ -290,6 +296,7 @@ int __init cma_init_reserved_mem(phys_addr_t base, phys= _addr_t size, return ret; =20 cma->ranges[0].base_pfn =3D PFN_DOWN(base); + cma->ranges[0].early_pfn =3D PFN_DOWN(base); cma->ranges[0].count =3D cma->count; cma->nranges =3D 1; cma->nid =3D NUMA_NO_NODE; @@ -509,6 +516,7 @@ int __init cma_declare_contiguous_multi(phys_addr_t tot= al_size, nr, (u64)mlp->base, (u64)mlp->base + size); cmrp =3D &cma->ranges[nr++]; cmrp->base_pfn =3D PHYS_PFN(mlp->base); + cmrp->early_pfn =3D cmrp->base_pfn; cmrp->count =3D size >> PAGE_SHIFT; =20 sizeleft -=3D size; @@ -540,7 +548,6 @@ int __init cma_declare_contiguous_multi(phys_addr_t tot= al_size, pr_info("Reserved %lu MiB in %d range%s\n", (unsigned long)total_size / SZ_1M, nr, nr > 1 ? "s" : ""); - return ret; } =20 @@ -1044,3 +1051,65 @@ bool cma_intersects(struct cma *cma, unsigned long s= tart, unsigned long end) =20 return false; } + +/* + * Very basic function to reserve memory from a CMA area that has not + * yet been activated. This is expected to be called early, when the + * system is single-threaded, so there is no locking. The alignment + * checking is restrictive - only pageblock-aligned areas + * (CMA_MIN_ALIGNMENT_BYTES) may be reserved through this function. + * This keeps things simple, and is enough for the current use case. + * + * The CMA bitmaps have not yet been allocated, so just start + * reserving from the bottom up, using a PFN to keep track + * of what has been reserved. Unreserving is not possible. + * + * The caller is responsible for initializing the page structures + * in the area properly, since this just points to memblock-allocated + * memory. The caller should subsequently use init_cma_pageblock to + * set the migrate type and CMA stats the pageblocks that were reserved. + * + * If the CMA area fails to activate later, memory obtained through + * this interface is not handed to the page allocator, this is + * the responsibility of the caller (e.g. like normal memblock-allocated + * memory). + */ +void __init *cma_reserve_early(struct cma *cma, unsigned long size) +{ + int r; + struct cma_memrange *cmr; + unsigned long available; + void *ret =3D NULL; + + if (!cma->count) + return NULL; + /* + * Can only be called early in init. + */ + if (test_bit(CMA_ACTIVATED, &cma->flags)) + return NULL; + + if (!IS_ALIGNED(size, CMA_MIN_ALIGNMENT_BYTES)) + return NULL; + + if (!IS_ALIGNED(size, (PAGE_SIZE << cma->order_per_bit))) + return NULL; + + size >>=3D PAGE_SHIFT; + + if (size > cma->available_count) + return NULL; + + for (r =3D 0; r < cma->nranges; r++) { + cmr =3D &cma->ranges[r]; + available =3D cmr->count - (cmr->early_pfn - cmr->base_pfn); + if (size <=3D available) { + ret =3D phys_to_virt(PFN_PHYS(cmr->early_pfn)); + cmr->early_pfn +=3D size; + cma->available_count -=3D size; + return ret; + } + } + + return ret; +} diff --git a/mm/cma.h b/mm/cma.h index 0a1f8f8abe08..93fc76cc6068 100644 --- a/mm/cma.h +++ b/mm/cma.h @@ -16,9 +16,16 @@ struct cma_kobject { * and the total amount of memory requested, while smaller than the total * amount of memory available, is large enough that it doesn't fit in a * single physical memory range because of memory holes. + * + * Fields: + * @base_pfn: physical address of range + * @early_pfn: first PFN not reserved through cma_reserve_early + * @count: size of range + * @bitmap: bitmap of allocated (1 << order_per_bit)-sized chunks. */ struct cma_memrange { unsigned long base_pfn; + unsigned long early_pfn; unsigned long count; unsigned long *bitmap; }; @@ -56,6 +63,7 @@ enum cma_flags { CMA_RESERVE_PAGES_ON_ERROR, CMA_ZONES_VALID, CMA_ZONES_INVALID, + CMA_ACTIVATED, }; =20 extern struct cma cma_areas[MAX_CMA_AREAS]; diff --git a/mm/internal.h b/mm/internal.h index 63fda9bb9426..8318c8e6e589 100644 --- a/mm/internal.h +++ b/mm/internal.h @@ -848,6 +848,22 @@ void init_cma_reserved_pageblock(struct page *page); =20 #endif /* CONFIG_COMPACTION || CONFIG_CMA */ =20 +struct cma; + +#ifdef CONFIG_CMA +void *cma_reserve_early(struct cma *cma, unsigned long size); +void init_cma_pageblock(struct page *page); +#else +static inline void *cma_reserve_early(struct cma *cma, unsigned long size) +{ + return NULL; +} +static inline void init_cma_pageblock(struct page *page) +{ +} +#endif + + int find_suitable_fallback(struct free_area *area, unsigned int order, int migratetype, bool only_stealable, bool *can_steal); =20 diff --git a/mm/mm_init.c b/mm/mm_init.c index f7d5b4fe1ae9..f31260fd393e 100644 --- a/mm/mm_init.c +++ b/mm/mm_init.c @@ -2263,6 +2263,15 @@ void __init init_cma_reserved_pageblock(struct page = *page) adjust_managed_page_count(page, pageblock_nr_pages); page_zone(page)->cma_pages +=3D pageblock_nr_pages; } +/* + * Similar to above, but only set the migrate type and stats. + */ +void __init init_cma_pageblock(struct page *page) +{ + set_pageblock_migratetype(page, MIGRATE_CMA); + adjust_managed_page_count(page, pageblock_nr_pages); + page_zone(page)->cma_pages +=3D pageblock_nr_pages; +} #endif =20 void set_zone_contiguous(struct zone *zone) --=20 2.48.1.262.g85cc9f2d1e-goog