All of lore.kernel.org
 help / color / mirror / Atom feed
From: Suseendran Bhaskaran <suseendranb@google.com>
To: Andrew Morton <akpm@linux-foundation.org>,
	Mike Rapoport <rppt@kernel.org>,  Dan Williams <djbw@kernel.org>,
	David Hildenbrand <david@kernel.org>
Cc: Alexander Graf <graf@amazon.com>,
	Alison Schofield <alison.schofield@intel.com>,
	 Andy Lutomirski <luto@kernel.org>,
	Borislav Petkov <bp@alien8.de>,
	 Catalin Marinas <catalin.marinas@arm.com>,
	Changyuan Lyu <changyuanl@google.com>,
	 Dave Hansen <dave.hansen@linux.intel.com>,
	Dave Jiang <dave.jiang@intel.com>,
	 David Rientjes <rientjes@google.com>,
	Frank van der Linden <fvdl@google.com>,
	"H. Peter Anvin" <hpa@zytor.com>,  Ingo Molnar <mingo@redhat.com>,
	Ira Weiny <iweiny@kernel.org>, Jonathan Corbet <corbet@lwn.net>,
	 "Liam R. Howlett" <liam@infradead.org>,
	Mark Rutland <mark.rutland@arm.com>,
	 Michal Hocko <mhocko@suse.com>,
	Oscar Salvador <osalvador@suse.de>,
	 Pasha Tatashin <pasha.tatashin@soleen.com>,
	Peter Zijlstra <peterz@infradead.org>,
	 Pratyush Yadav <pratyush@kernel.org>,
	Randy Dunlap <rdunlap@infradead.org>,
	 Shuah Khan <skhan@linuxfoundation.org>,
	Thomas Gleixner <tglx@kernel.org>,
	 Vishal Verma <vishal.l.verma@intel.com>,
	Vlastimil Babka <vbabka@kernel.org>,
	 Will Deacon <will@kernel.org>,
	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 <suseendranb@google.com>
Subject: [RFC PATCH 01/11] mm/cma: clamp multi-range search to 4G and honor memblock flags
Date: Wed,  2 Dec 2026 00:19:36 +0000	[thread overview]
Message-ID: <20261202001946.1226273-2-suseendranb@google.com> (raw)
In-Reply-To: <20261202001946.1226273-1-suseendranb@google.com>

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 <suseendranb@google.com>
---
 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 <linux/types.h>
 #include <linux/init.h>
+#include <linux/memblock.h>
 #include <linux/mmzone.h>
 #include <linux/jump_label.h>
 #include <linux/printk.h>
@@ -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);

  reply	other threads:[~2026-12-02  0:20 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-02  0:19 [RFC PATCH 00/11] mm: NUMA-aware boot memory carving (extmem) Suseendran Bhaskaran
2026-12-02  0:19 ` Suseendran Bhaskaran [this message]
2026-12-02  0:19 ` [RFC PATCH 02/11] mm/memblock: factor out multi-range contiguous reservation helper from CMA Suseendran Bhaskaran
2026-12-02  0:19 ` [RFC PATCH 03/11] x86/mm: add early direct-map unmap helpers for boot carveouts Suseendran Bhaskaran
2026-12-02  0:19 ` [RFC PATCH 04/11] arm64/mm: support unmapping boot carveouts from the linear map Suseendran Bhaskaran
2026-12-02  0:19 ` [RFC PATCH 05/11] mm/extmem: add architecture-neutral external memory subsystem Suseendran Bhaskaran
2026-12-02  0:19 ` [RFC PATCH 06/11] dax/hmem: bind extmem platform devices with per-region alignment Suseendran Bhaskaran
2026-12-02  0:19 ` [RFC PATCH 07/11] nvdimm/ramdax: manage bus and DIMM allocations with devres Suseendran Bhaskaran
2026-12-02  0:19 ` [RFC PATCH 08/11] nvdimm/ramdax: harden probe checks and select NUMA_KEEP_MEMINFO Suseendran Bhaskaran
2026-12-02  0:19 ` [RFC PATCH 09/11] nvdimm/ramdax: support binding extmem platform devices Suseendran Bhaskaran
2026-12-02  0:19 ` [RFC PATCH 10/11] x86/kaslr: avoid placing kernel in carvable high memory when extmem= is set Suseendran Bhaskaran
2026-12-02  0:19 ` [RFC PATCH 11/11] mm/extmem: preserve carved regions across KHO kexec Suseendran Bhaskaran
2026-12-02 14:45 ` [RFC PATCH 00/11] mm: NUMA-aware boot memory carving (extmem) Mike Rapoport
2026-12-02 15:10   ` David Hildenbrand (Arm)

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261202001946.1226273-2-suseendranb@google.com \
    --to=suseendranb@google.com \
    --cc=akpm@linux-foundation.org \
    --cc=alison.schofield@intel.com \
    --cc=bp@alien8.de \
    --cc=catalin.marinas@arm.com \
    --cc=changyuanl@google.com \
    --cc=corbet@lwn.net \
    --cc=dave.hansen@linux.intel.com \
    --cc=dave.jiang@intel.com \
    --cc=david@kernel.org \
    --cc=djbw@kernel.org \
    --cc=fvdl@google.com \
    --cc=graf@amazon.com \
    --cc=hpa@zytor.com \
    --cc=iweiny@kernel.org \
    --cc=kexec@lists.infradead.org \
    --cc=liam@infradead.org \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-cxl@vger.kernel.org \
    --cc=linux-doc@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=luto@kernel.org \
    --cc=mark.rutland@arm.com \
    --cc=mhocko@suse.com \
    --cc=mingo@redhat.com \
    --cc=nvdimm@lists.linux.dev \
    --cc=osalvador@suse.de \
    --cc=pasha.tatashin@soleen.com \
    --cc=peterz@infradead.org \
    --cc=pratyush@kernel.org \
    --cc=rdunlap@infradead.org \
    --cc=rientjes@google.com \
    --cc=rppt@kernel.org \
    --cc=skhan@linuxfoundation.org \
    --cc=tglx@kernel.org \
    --cc=vbabka@kernel.org \
    --cc=vishal.l.verma@intel.com \
    --cc=will@kernel.org \
    --cc=x86@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.