Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.210.199 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796170807; b=oDhUKs/UTPUVWCkDSkQkKx8RAhkkPTOdNwv1XRSAtd3IwVEwrf5dkBAksAz29y1zfAEP OAofUL1VZaEVfDAvu+V5X8H2Z8PaF11Mu2LTK/K683k7K74o/EJ3ZZ7DnkvKkz/dmICVZ wSnkq+VTJHEMs5MFVRyS268Z+0PLJccvwQEF07NvRn2+0wac+5JPt6dD05g78UfNwoQlh oqFiw4xQ4Akirdo87lJ04j5lWaFPt6zoPG2K8SjoegL9RHx1NiM0YEm0UPxW2GgflHPxC cvrl3B5yn61h+a4dz2Y21UTfLm3BtNkyV5LzPzZhyd+m5nibkl1rg+FjV/PVm7ox/aQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796170807; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Received:Date:In-Reply-To: Mime-Version:References:X-Mailer:Message-ID:Subject:From:To:Cc: Content-Type; bh=iZwV53+L9JDTiuOakhACIJJsJ9dnjbcJqyJlnfSfiu8=; b=cCRgPehdarW/+pC88dOAZxWwvibl8dflBiXMpgAuQjbr4jzeEMec/PkFP1Qs40ZAweTa YTFoUP/tV0ECBwyRUEwXEOpVthfyTrPX4ITVTYRqdeYua9SBAmYDclwy02DEcC8P8oN/L enpILuYoU6+gsXsZ/Y2XmU1bZPgWgz1KIyrsSwFHLFADP5niXOiBSfEk3RkH42IOsWDo7 rq0qrxRmiGTJ+JGDIN+MLBsbEvYX8pwq/2myPyJ1XI1wyd/2dIwF8fg6GqM8VpgtG1WfG LXinAIiA36xGjCP7zHhfzSQjF6oWUCpUu2m2jjqn7slz7JPz+9n5KcoUU+hBy8kxgGA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=google.com; spf=pass smtp.mailfrom=flex--suseendranb.bounces.google.com; dkim=pass header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=W6CgGBhK; arc=none smtp.remote-ip=209.85.210.199 Received: from mail-pf1-f199.google.com (mail-pf1-f199.google.com [209.85.210.199]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 27F83606A322 for ; Wed, 2 Dec 2026 00:20:05 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 27F83606A322 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--suseendranb.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=W6CgGBhK Received: by mail-pf1-f199.google.com with SMTP id d2e1a72fcca58-8b60129a46eso4150802b3a.1 for ; Tue, 01 Dec 2026 16:20:05 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1796170805; x=1796775605; darn=lists.linux.dev; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:from:to:cc:subject:date:message-id:reply-to :content-type; bh=iZwV53+L9JDTiuOakhACIJJsJ9dnjbcJqyJlnfSfiu8=; b=W6CgGBhK6q2yKitMf4IeZqI9mMYuI3nViyWBsMGQVd219q3w4ABH5GB4hoP913JPKo f0lzw5oer77ZksZLKXfw2giBoW+6qNClhVx0C9LnlLWEO/KZTR+iObh7r7Xu513fOpMM 47S5+TQUBh0wxGYJcdJr2gBYM1vJccRUYLTkqXH8J2AKl+HJzG40BZswDPX/knfv7J+m rM625Ou76rq7ATSeeA0VBE973YsHvM6NMh4X86VWs0UKaTeJATFatMbL+tDfNHgDAKFG wwV1kxnPwiIb2KL+uxgDRfchUzlm/0aWWsZTeCeU91Gn0za0etXAlmqXvTse6YTzjaOe dabw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796170805; x=1796775605; h=content-type: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:content-type; bh=iZwV53+L9JDTiuOakhACIJJsJ9dnjbcJqyJlnfSfiu8=; b=NeSdlIfbtWlzD2dcmBH0+3ksm1qqmPn44jBQll/nP1PYHk6ooaOW9wO9ED6e6c+jXs BT5HExilxR86ANloyT8DPaxF/zGuMVijj69SSKJ+5m7Jnq7gQcIRWaJuLnVtmG1ERgPy s9e9Z3PEb5i9zg8jagjedah8Jg8KM7VSOlm8VbbAKBoY1iDheadNxAzwTOdbF1Z9Nsmy dDDyzauJDR7Mw4DeV+sxqn12iGD3n17Pro54ZU1tdmCYH5+CNb3I73h/gsi4Pcsw12+T p02/fL1Urz2a6IvHdkpvqWlQH9C1iwilBH8SmPCHkWj4AKq7FKp8ggK1jV+VslK5YI1X qp0A== X-Forwarded-Encrypted: i=1; AL+hErEr0L88CTFXU9OoJ5rDyiZmTha91QbJTnWL+IPsvDl49odoA3t/AzXkmWI/l9X+PH19ezPySzM=@lists.linux.dev X-Gm-Message-State: APuU4Z+6nSZs/h/YMSEUxh/kJa5QR37YiQxshyHKv3V3w6RNolI1pKWO VWEnk9s2Hx3kN3kH1osp757Eu3fXkPqj7mD15Wmn3By3Qx1hI2Jjdj1rFwe/wdzpIWXT9Lk6EY+ 0z/lofYc39oblxySsbeVNWQ456g== X-Received: from pfbim8.prod.google.com ([2002:a05:6a00:8d88:b0:89d:e040:c63e]) (user=suseendranb job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:2d06:b0:882:e3a6:27c9 with SMTP id d2e1a72fcca58-8c87c28e012mr38636361b3a.30.1796170804915; Tue, 01 Dec 2026 16:20:04 -0800 (PST) Date: Wed, 2 Dec 2026 00:19:36 +0000 In-Reply-To: <20261202001946.1226273-1-suseendranb@google.com> X-Mailing-List: nvdimm@lists.linux.dev List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list Mime-Version: 1.0 References: <20261202001946.1226273-1-suseendranb@google.com> X-Mailer: git-send-email 2.56.1.911.g883d8bcb20-goog Message-ID: <20261202001946.1226273-2-suseendranb@google.com> Subject: [RFC PATCH 01/11] mm/cma: clamp multi-range search to 4G and honor memblock flags From: Suseendran Bhaskaran To: Andrew Morton , Mike Rapoport , Dan Williams , David Hildenbrand Cc: Alexander Graf , Alison Schofield , Andy Lutomirski , Borislav Petkov , Catalin Marinas , Changyuan Lyu , Dave Hansen , Dave Jiang , David Rientjes , Frank van der Linden , "H. Peter Anvin" , Ingo Molnar , Ira Weiny , Jonathan Corbet , "Liam R. Howlett" , Mark Rutland , Michal Hocko , Oscar Salvador , Pasha Tatashin , Peter Zijlstra , Pratyush Yadav , Randy Dunlap , Shuah Khan , Thomas Gleixner , Vishal Verma , Vlastimil Babka , Will Deacon , kexec@lists.infradead.org, linux-arm-kernel@lists.infradead.org, linux-cxl@vger.kernel.org, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, nvdimm@lists.linux.dev, x86@kernel.org, Suseendran Bhaskaran Content-Type: text/plain; charset="UTF-8" When single-range CMA allocation fails with -ENOMEM (for example, across a memory hole) and falls back to cma_find_free_range(), any free memblock span starting below 4 GiB is skipped outright (!upper_32_bits(start)). On platforms without a hole just below 4 GiB (such as arm64 where DRAM can cross 4 GiB contiguously), that discards the usable >= 4 GiB portion of the first span and can fail with -ENOMEM. It also passes MEMBLOCK_NONE to for_each_free_mem_range() instead of choose_memblock_flags(). On KHO boots, hugetlb_cma_reserve() runs in mm_core_init_early() while kho_noprsrv_only is active and before kho_memory_init() reserves preserved pages in memblock.reserved, so ignoring choose_memblock_flags() can place multi-range CMA areas directly over KHO preserved memory. To fix this: - Clamp each candidate range's lower bound to 4 GiB before alignment while guarding against address wraparound near the top of physical memory. - Honor the active memblock allocation flags so KHO preserved memory is skipped, excluding mirror-only filtering since free-range iteration does not fall back to non-mirrored RAM. - Replace the runtime IS_ENABLED(CONFIG_PHYS_ADDR_T_64BIT) check with #ifdef CONFIG_PHYS_ADDR_T_64BIT so clamping to SZ_4G does not truncate on 32-bit phys_addr_t builds. Found by code inspection. Fixes: c009da4258f9 ("mm, cma: support multiple contiguous ranges, if requested") Fixes: d59f43b57480 ("memblock: add support for scratch memory") Signed-off-by: Suseendran Bhaskaran --- mm/cma.c | 21 ++++++++++----------- mm/memblock.c | 2 +- mm/mm_init.h | 2 ++ 3 files changed, 13 insertions(+), 12 deletions(-) diff --git a/mm/cma.c b/mm/cma.c index 0c97aadcfea4..c0db52b4ae29 100644 --- a/mm/cma.c +++ b/mm/cma.c @@ -507,20 +507,16 @@ static int __init __cma_declare_contiguous_nid(phys_addr_t *basep, static phys_addr_t __init cma_find_free_range(phys_addr_t align, phys_addr_t size_align, int nid, phys_addr_t *basep) { - phys_addr_t start, end, size; +#ifdef CONFIG_PHYS_ADDR_T_64BIT + enum memblock_flags flags = choose_memblock_flags() & ~MEMBLOCK_MIRROR; + phys_addr_t start, end, size, orig_start; phys_addr_t max = 0; u64 i; - /* no point searching if the system can't address above 4G */ - if (!IS_ENABLED(CONFIG_PHYS_ADDR_T_64BIT)) - return 0; - - for_each_free_mem_range(i, nid, MEMBLOCK_NONE, &start, &end, NULL) { - if (!upper_32_bits(start)) - continue; - - start = ALIGN(start, align); - if (start >= end) + for_each_free_mem_range(i, nid, flags, &start, &end, NULL) { + orig_start = max_t(phys_addr_t, start, SZ_4G); + start = ALIGN(orig_start, align); + if (start < orig_start || start >= end) continue; size = ALIGN_DOWN(end - start, size_align); @@ -531,6 +527,9 @@ static phys_addr_t __init cma_find_free_range(phys_addr_t align, } return max; +#else + return 0; +#endif } static int __init cma_range_cmp(const void *a, const void *b) diff --git a/mm/memblock.c b/mm/memblock.c index a450092a80fb..d81f2074e16e 100644 --- a/mm/memblock.c +++ b/mm/memblock.c @@ -172,7 +172,7 @@ bool __init_memblock memblock_has_mirror(void) return system_has_some_mirror; } -static enum memblock_flags __init_memblock choose_memblock_flags(void) +enum memblock_flags __init_memblock choose_memblock_flags(void) { /* only use KHO_NOPRSRV memory for kho early boot allocations */ if (kho_noprsrv_only) diff --git a/mm/mm_init.h b/mm/mm_init.h index 9ee9bcb8e672..245baa769add 100644 --- a/mm/mm_init.h +++ b/mm/mm_init.h @@ -10,6 +10,7 @@ #include #include +#include #include #include #include @@ -99,6 +100,7 @@ static inline void mminit_verify_zonelist(void) extern bool mirrored_kernelcore; bool memblock_has_mirror(void); +enum memblock_flags choose_memblock_flags(void); void memblock_free_all(void); void memmap_init_page(struct page *page, enum zone_type zone, int nid);