From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1F78360A260B for ; Wed, 2 Dec 2026 00:20:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=lists.infradead.org header.i=@lists.infradead.org header.a=rsa-sha256 header.s=bombadil.20210309 header.b=yMQS6R4e; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=l0Un5j2l DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Type:Cc:To:From: Subject:Message-ID:References:Mime-Version:In-Reply-To:Date:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Unsubscribe-Post:List-Owner; bh=iZwV53+L9JDTiuOakhACIJJsJ9dnjbcJqyJlnfSfiu8=; b=yMQS6R4edW7xDZ+JM+DGf0ezCw xbg+AC0WhZMttP3XJQyvDddLRYcu01BmsLLIHB7lEnhExujmg+wJ3PcHi9D41o5dSozBo2XXHIR/y AejfOuLQBhmngotGlr6T5WApVxcoEB07LT3jxrtzedjwL42Fz8ituOzFczVWo7vNlLmZnLQNgpjmJ rSqibCMsuu9nn8b4zxPoKDQ5u8vrQbFnJoSjfh1CKaRekFY1rteqob5ZuqDTO/GHBpxbthcwQhGiA y41wlx0aF4LRZ0OgBROTUc95ZmlAVh9nw0kSBFxJkMJljd1/5mu5eETYccM2e4TWLZch8PJzOrvRk rmsbfJQA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.100.1 #2 (Red Hat Linux)) id 1xYY4w-00000009j9N-1ZNp; Wed, 02 Dec 2026 00:20:10 +0000 Received: from mail-pf1-x448.google.com ([2607:f8b0:4864:20::448]) by bombadil.infradead.org with esmtps (Exim 4.100.1 #2 (Red Hat Linux)) id 1xYY4s-00000009j6n-2bs7 for linux-arm-kernel@lists.infradead.org; Wed, 02 Dec 2026 00:20:08 +0000 Received: by mail-pf1-x448.google.com with SMTP id d2e1a72fcca58-8b852fdd55aso4766360b3a.0 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.infradead.org; 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=l0Un5j2lP/RYx7zFI4XiDc2aaF8CGhYjCAK3+C0C10w5DLbQIfrby9fiHAq3+6GUMA +Zj7v3MjQeZYuJjQLdJYIWWzzCc1aLTc9g0WHmSKtxIqzDcoF9S3NpUfQS+GMRoq3X1b fgSWydXIH6+VzzGR0Gy0jSFzs5jtMNzflfCzPjaEL4XEoI6FHs5n3uA0zws7kmIdJbDi RDzw+MVUJK/qUBl2xI08Z96EmU7U9f06KO/HTrRWiEpSXkktB0JoQ9FMOSATajdPQ54d SjpSxef1Z12f+hadw8YBIyI2DZ51sftsg37gKKnleyBaB3Bsuz7XE4gTAWarw2/qGxEO /tfA== 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=X3/QGODEziMCqJ0gpDdif+x04Lj5oSE8tRJHsettVKONMnb99k3P0Y7w+aAwlI3P/Y u0wVpQsEPBXxKQ2dzzkXH3Xg07uNXbkqq6t2Yi0PXZVKJqJadRAzHWubSIghfCCZZTUI o3XVKE1HPbLwfKhUoV0XI4P4IsAHyvHUfTK6og69BTKmpfb3TBtEvBybeRPhf+GBVCk5 xtlUt7LhYcsWaMZEcudc3RZksSRG9G+1KDCVxOgf3bfuLVe1jJR22dVHEpBLVG9zzG2n 8bUY+DS+iTVEfR5t4LgNwISueUEH4p4yMMlj0oUwDDPPc2xlGCATvekRjghfqVpLqJkf UEsg== X-Forwarded-Encrypted: i=1; AL+hErETe/2nDbKukNf8yEekNZ8PqbJuwvEm7vmYAj9ajjjSu6+6gc4Mc6UcC79CSzevrpKHdR2+tHi0GUR6DxQUwhv/@lists.infradead.org X-Gm-Message-State: APuU4Z83PUYjfFXXPDe4TfKWkisY/cCP3oH7cjQnVwZvucC7p8t5lyBf BjS9VCxZE68Y19sAXY5a0mdPEwxZfMk+L9M212uUBDB8NCwmG/YcWstp/SYHxkzC1jfm0YeD/db gCCUrgDS7DdFIJAE3Qomxyc+E0Q== 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-Archived-By: 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" X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.9.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20261201_162006_661258_888C8538 X-CRM114-Status: GOOD ( 19.29 ) X-Spam-Score: -9.5 (---------) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" X-Spam-Status: No, score=-26.4 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIMWL_WL_MED,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT, USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=4.0.1 X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-26) on aws-us-west-2-korg-listserv-alma10-1 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);