From patchwork Wed Jan 29 22:41:54 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Frank van der Linden X-Patchwork-Id: 13954222 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id A3132C02190 for ; Wed, 29 Jan 2025 22:43:37 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 51BAB440009; Wed, 29 Jan 2025 17:42:59 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 4CD7828026C; Wed, 29 Jan 2025 17:42:59 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 31DF3440009; Wed, 29 Jan 2025 17:42:59 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 0FB2028026C for ; Wed, 29 Jan 2025 17:42:59 -0500 (EST) Received: from smtpin28.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id B849F1404A2 for ; Wed, 29 Jan 2025 22:42:58 +0000 (UTC) X-FDA: 83061966036.28.B200D5F Received: from mail-pl1-f201.google.com (mail-pl1-f201.google.com [209.85.214.201]) by imf24.hostedemail.com (Postfix) with ESMTP id D68E218000D for ; Wed, 29 Jan 2025 22:42:56 +0000 (UTC) Authentication-Results: imf24.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=O903qEnI; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf24.hostedemail.com: domain of 3766aZwQKCPohxfniqqing.eqonkpwz-oomxcem.qti@flex--fvdl.bounces.google.com designates 209.85.214.201 as permitted sender) smtp.mailfrom=3766aZwQKCPohxfniqqing.eqonkpwz-oomxcem.qti@flex--fvdl.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1738190576; a=rsa-sha256; cv=none; b=VvBUhkrN8jGy5QuoxVyJ8uS78IRfJCAISwSoZS1lJ/6xRGcRnsMsshw7zt4hRMjutUJXVh nkplJ92CkHOmmO1JLjKDEm5QRdsDwYhgcIoKwZzcei84pl5nSQPbrxm4jJDHj0GHMAy6Kx NP7qw6TfbWc5s3QfK0Le2e8yuYl8Z2I= ARC-Authentication-Results: i=1; imf24.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=O903qEnI; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf24.hostedemail.com: domain of 3766aZwQKCPohxfniqqing.eqonkpwz-oomxcem.qti@flex--fvdl.bounces.google.com designates 209.85.214.201 as permitted sender) smtp.mailfrom=3766aZwQKCPohxfniqqing.eqonkpwz-oomxcem.qti@flex--fvdl.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1738190576; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=St+q1DoY+42s4g4QcYXYYqTt/AeFuMkTKVQ73Ddb3Ys=; b=gXtPoSuqRPoxQrNhUfSlw33G5OQqRk58atTmmzQAS95LiIa8RJw1sZep0nVqlTyF3OB1w1 NJXTazU/hxM6VFKamQ+FC7/kd/FZdHPSBvC9pOOBIwfA6psE54U9wgK4li8zN2JrNuB2TZ KLbUa5XC7/EXeps3nIgekheVLKPRK2g= Received: by mail-pl1-f201.google.com with SMTP id d9443c01a7336-21648c8601cso2332145ad.2 for ; Wed, 29 Jan 2025 14:42:56 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1738190576; x=1738795376; darn=kvack.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=St+q1DoY+42s4g4QcYXYYqTt/AeFuMkTKVQ73Ddb3Ys=; b=O903qEnIj9hHZz+P4VNhwjLqMp5ApBochKHg9WE4txrJLviTHxw2KGMSzdvqT5J9dn OEenFjeo7b1QZlE0E+PE+XJlkDFH/emcmDiFmejseET60sbotiGsT0jViW3V8EqF4M4i PTAriKBc4B8TR277W7Rkk9YRy3tLEVZilsMpkzA2XyJhxLjyf+o0fg3N+oWpAOniz3Q5 9ymY1Sc+42Gd5wjy/7OLqibD0vqCr5vMobiUGbMOt7TIYJK0XS6RIyTs0Ayb3TEEIlxx wDR/Y0EdWaAKGfi+eiRnqlUXC1u8Ww5Z98n5KB8d6MAjRnHbwk2SN8ue6rejt+uVqWgU JqNA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1738190576; x=1738795376; 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=St+q1DoY+42s4g4QcYXYYqTt/AeFuMkTKVQ73Ddb3Ys=; b=Ws1+P1hcSkaAIS2r+LSQd44i3oU3/gWexOy8nOfHJ35CJdARUR16mSmjapHhWsBOtP NXT4rUxq0AWSglX5f+Ognaa4qjcb/fNWkPVJaSjJ/FpdtaNRgQrr6BcmLHYYHjalxJHy lfYc8EMJTi6r1VPGI0jYX1sGwsd7ZCLKNqoHfiFNi6Bip1+a15Bhwy876b8dpNMmpoFB awz+55JLs8wZwGHDVgDR9CUAFa8FPPbRPcmYPpiY1z/AuF1Bf2b4WxAZ+JyncT8Ma6hi mRpFY7jxZNj404qSLhIiDKFtFHJNvUCl1k9GuY1qWjJQWJQXRFP/w1EJv8r8+ntj/6RH aGKA== X-Forwarded-Encrypted: i=1; AJvYcCUQqLByYx7ov3aX+1NrjiC8fP21NK8N2tKlcef//8sSJfQoxG/IU7U6Y6Zs42p/1K/vVee85vTS0w==@kvack.org X-Gm-Message-State: AOJu0YzIbKr/QbVyekqg++ODbUaWD5AoET5EMAPNElUlMdkV1BCTENAf RfUIwDvBv8CBYwPC/OoUX2X6eHyuM+FUGSOCAj5DtVeMtpNXMpGmiFrBn/4q/D08jH1jCA== X-Google-Smtp-Source: AGHT+IHKa9OKErOBIP4kt8mPwsrgnK1mz1JVZAjaYOuo9N5ZF7fBntLgqqGp6sxXhVo/+8TW/rd/4Vec X-Received: from plbq8.prod.google.com ([2002:a17:903:1788:b0:216:2234:bf3e]) (user=fvdl job=prod-delivery.src-stubby-dispatcher) by 2002:a17:903:298d:b0:216:3436:b87e with SMTP id d9443c01a7336-21dd7dff854mr91313615ad.44.1738190575742; Wed, 29 Jan 2025 14:42:55 -0800 (PST) Date: Wed, 29 Jan 2025 22:41:54 +0000 In-Reply-To: <20250129224157.2046079-1-fvdl@google.com> Mime-Version: 1.0 References: <20250129224157.2046079-1-fvdl@google.com> X-Mailer: git-send-email 2.48.1.262.g85cc9f2d1e-goog Message-ID: <20250129224157.2046079-26-fvdl@google.com> Subject: [PATCH v2 25/28] 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, usamaarif642@gmail.com, joao.m.martins@oracle.com, roman.gushchin@linux.dev, Frank van der Linden X-Rspam-User: X-Rspamd-Queue-Id: D68E218000D X-Rspamd-Server: rspam10 X-Stat-Signature: xfc7x9rhb64emhs3eodsfqpq54mt7ip5 X-HE-Tag: 1738190576-879226 X-HE-Meta: U2FsdGVkX1+ArtQ0CNZ0CcZd2YOf3mTZs7XRP5Q4tB/n+rDuO6qtNAvVWlt9jRyPOaD0qUbu1WhBzHRI1nXC4l/eWwxt9hvwj+ltLPzvptemWacBL8xe9Oej6BfLWiJLElxUOknWxhPr/F/SfMq/+GLl2JW6SfLK7txKmwFD6OMsUu02fAi5fHrtPkDjlvg8uQLzvKIABSvXDlUQS7Knl5fNN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3aCnziqcfEbnbjIWTn3Gt5VOTHNZY2wtp7nC6v31APYQsjqMYcEs9IUzcHqsslU0l3zoKf7K+mGgNcfZTBwaZwPSbFSmpIUvP7F0nTdbrVYJu0k3bb6iAIYyI4Ya8kTRZMpAwkUZhK21dgz6euLFvv600XGMkR+zc8FiyLyoL7TqNVihqD8VYHxVV0X44CWrUrY/tfkkFBwJtBHrElufLNwmuWhQ== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: 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..2b1e264e4e99 100644 --- a/mm/cma.c +++ b/mm/cma.c @@ -144,9 +144,10 @@ bool cma_validate_zones(struct cma *cma) 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; for (allocrange = 0; allocrange < cma->nranges; allocrange++) { cmr = &cma->ranges[allocrange]; @@ -161,8 +162,13 @@ static void __init cma_activate_area(struct cma *cma) for (r = 0; r < cma->nranges; r++) { cmr = &cma->ranges[r]; - base_pfn = cmr->base_pfn; - for (pfn = base_pfn; pfn < base_pfn + cmr->count; + if (cmr->early_pfn != cmr->base_pfn) { + count = cmr->early_pfn - cmr->base_pfn; + bitmap_count = cma_bitmap_pages_to_bits(cma, count); + bitmap_set(cmr->bitmap, 0, bitmap_count); + } + + for (pfn = cmr->early_pfn; pfn < cmr->base_pfn + cmr->count; pfn += 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); return; @@ -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 = 0; r < allocrange; r++) { cmr = &cma->ranges[r]; - for (pfn = cmr->base_pfn; - pfn < cmr->base_pfn + cmr->count; - pfn++) + end_pfn = cmr->base_pfn + cmr->count; + for (pfn = 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; cma->ranges[0].base_pfn = PFN_DOWN(base); + cma->ranges[0].early_pfn = PFN_DOWN(base); cma->ranges[0].count = cma->count; cma->nranges = 1; cma->nid = NUMA_NO_NODE; @@ -509,6 +516,7 @@ int __init cma_declare_contiguous_multi(phys_addr_t total_size, nr, (u64)mlp->base, (u64)mlp->base + size); cmrp = &cma->ranges[nr++]; cmrp->base_pfn = PHYS_PFN(mlp->base); + cmrp->early_pfn = cmrp->base_pfn; cmrp->count = size >> PAGE_SHIFT; sizeleft -= size; @@ -540,7 +548,6 @@ int __init cma_declare_contiguous_multi(phys_addr_t total_size, pr_info("Reserved %lu MiB in %d range%s\n", (unsigned long)total_size / SZ_1M, nr, nr > 1 ? "s" : ""); - return ret; } @@ -1044,3 +1051,65 @@ bool cma_intersects(struct cma *cma, unsigned long start, unsigned long end) 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 = NULL; + + if (!cma || !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 >>= PAGE_SHIFT; + + if (size > cma->available_count) + return NULL; + + for (r = 0; r < cma->nranges; r++) { + cmr = &cma->ranges[r]; + available = cmr->count - (cmr->early_pfn - cmr->base_pfn); + if (size <= available) { + ret = phys_to_virt(PFN_PHYS(cmr->early_pfn)); + cmr->early_pfn += size; + cma->available_count -= size; + return ret; + } + } + + return ret; +} diff --git a/mm/cma.h b/mm/cma.h index bddc84b3cd96..df7fc623b7a6 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; #ifdef CONFIG_CMA_DEBUGFS @@ -58,6 +65,7 @@ enum cma_flags { CMA_RESERVE_PAGES_ON_ERROR, CMA_ZONES_VALID, CMA_ZONES_INVALID, + CMA_ACTIVATED, }; 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); #endif /* CONFIG_COMPACTION || CONFIG_CMA */ +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); 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 += 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 += pageblock_nr_pages; +} #endif void set_zone_contiguous(struct zone *zone)