From patchwork Fri Jan 2 00:50:39 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Simon Glass X-Patchwork-Id: 1173 Return-Path: X-Original-To: u-boot-concept@u-boot.org Delivered-To: u-boot-concept@u-boot.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1767315118; bh=Ed+og2rfPz9ks2cw4yTeuKzxmeKQ0OlS12883G80fAI=; h=From:To:Date:In-Reply-To:References:CC:Subject:List-Id: List-Archive:List-Help:List-Owner:List-Post:List-Subscribe: List-Unsubscribe:From; b=eGbUsj/2/Hk4r7mpkRjrUO0IIZRmHqNsQS8iTjNBEfieZ0Nut5/l7oRcqWK0h1gW1 Blic9pc3AErF3Q2Kp5NK9gIUqR/6gm0UQvPnQW+SeZZqsU22Gu2M9LMj9tkXZ4joyc W6PLj+JYlKJxmTfEOwhdmba3sqDUwFyOINj1PUh+58BIri/mFdWeD0WT+0LLOvnvNt mHIvYEYYZAUbFggeqrKH/LuHj5lkF09qY87HDCk+o3alKgJdMaoqZzMYZ4lgPwu5Vc xPSkhs2JcGlDlZ33P3C7QnuSTXFbf4Jw3JCyEep+Ma5kDkNAYZ9Y7XxO0wJxbEc1vl JpnBeDQgIeSwg== Received: from localhost (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id 97F8769007 for ; Thu, 1 Jan 2026 17:51:58 -0700 (MST) X-Virus-Scanned: Debian amavis at Received: from mail.u-boot.org ([127.0.0.1]) by localhost (mail.u-boot.org [127.0.0.1]) (amavis, port 10024) with ESMTP id CECE7_YwgFO2 for ; Thu, 1 Jan 2026 17:51:58 -0700 (MST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1767315118; bh=Ed+og2rfPz9ks2cw4yTeuKzxmeKQ0OlS12883G80fAI=; h=From:To:Date:In-Reply-To:References:CC:Subject:List-Id: List-Archive:List-Help:List-Owner:List-Post:List-Subscribe: List-Unsubscribe:From; b=eGbUsj/2/Hk4r7mpkRjrUO0IIZRmHqNsQS8iTjNBEfieZ0Nut5/l7oRcqWK0h1gW1 Blic9pc3AErF3Q2Kp5NK9gIUqR/6gm0UQvPnQW+SeZZqsU22Gu2M9LMj9tkXZ4joyc W6PLj+JYlKJxmTfEOwhdmba3sqDUwFyOINj1PUh+58BIri/mFdWeD0WT+0LLOvnvNt mHIvYEYYZAUbFggeqrKH/LuHj5lkF09qY87HDCk+o3alKgJdMaoqZzMYZ4lgPwu5Vc xPSkhs2JcGlDlZ33P3C7QnuSTXFbf4Jw3JCyEep+Ma5kDkNAYZ9Y7XxO0wJxbEc1vl JpnBeDQgIeSwg== Received: from mail.u-boot.org (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id 87CFC68FFD for ; Thu, 1 Jan 2026 17:51:58 -0700 (MST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1767315116; bh=cfyEcokymVFNC3aZA2grV9/K/wzKT5MpHez4bawXZmM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=KYZ7bEDkCKmnO74gbZ+crdsfk+cyp2QoZ+WEScC76oOBpXIsKmLaV+GChVeaFh/P2 Lc/p5mLK5Dg8tWJibKXu62c44VvOyuJBOggoNaBPyIonpV9NwciKwl03bdpgoDawJ8 KgvryTz77ACkgasEfU0jJ0tuxxNzTW4CfE1BypPcSpVKMow8bclu/y+HvLJaXELY7l B2omw3QjXmpUpgOwsKGiaQ7lJ9UStEiJpMkIzxMtaPsqK45swNCCJMpUPNWBXgcNEp 5yf0iujAN9en89NhhbMoHGqBVsxu7ZRR2qqrk8fWmh+76SprXEmQOs5lALfl2Nf/a8 h6MeXlvndLiBQ== Received: from localhost (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id 2E75568FFD; Thu, 1 Jan 2026 17:51:56 -0700 (MST) X-Virus-Scanned: Debian amavis at Received: from mail.u-boot.org ([127.0.0.1]) by localhost (mail.u-boot.org [127.0.0.1]) (amavis, port 10026) with ESMTP id 5xuQ7Yybi0cL; Thu, 1 Jan 2026 17:51:56 -0700 (MST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1767315110; bh=67rFhNp76sS2CJpWozdDjhkWWDZ2HNJ7V/YhEmlqss0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Zj6e0xWzplxf/CkuctYkZOvvFHlTdU3opVmvM0aj6Hmwq2op+iIhLBaSD2wdvcXAD yj34XlllZLibZBe+p2B+NAUSJbX9LMYklVXg7zCqwWzwZHbDntTCN8sgYCwEjyWXPa KVxVwLeUDabQYFg72h5M6Cc56wAI+ssJHftOTULlG5KIeg7YRWVYN4DmetpgOiXccy Rapww7FBQvWhhjpKB9LeSwvFZtWsohqI00FsQKZlbJ5DbjF13GHk5QMwNJ/P+M6NUE b5v4NWJ00qxutB4uSmB6hWWWGmgNBofPlbWXKnfOMgU5aztzBrM3F9IkrONdBOLw0d YtJSrxHoXhBPQ== Received: from u-boot.org (unknown [73.34.74.121]) by mail.u-boot.org (Postfix) with ESMTPSA id 3C2F368F58; Thu, 1 Jan 2026 17:51:50 -0700 (MST) From: Simon Glass To: U-Boot Concept Date: Thu, 1 Jan 2026 17:50:39 -0700 Message-ID: <20260102005112.552256-10-sjg@u-boot.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20260102005112.552256-1-sjg@u-boot.org> References: <20260102005112.552256-1-sjg@u-boot.org> MIME-Version: 1.0 Message-ID-Hash: CPKKQRP3WJG3KIO2QUK32GHNBNZBDVKQ X-Message-ID-Hash: CPKKQRP3WJG3KIO2QUK32GHNBNZBDVKQ X-MailFrom: sjg@u-boot.org X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; loop; banned-address; emergency; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header CC: Heinrich Schuchardt , Simon Glass , "Claude Opus 4 . 5" X-Mailman-Version: 3.3.10 Precedence: list Subject: [Concept] [PATCH v2 09/30] mcheck: Skip mcheck overhead for pre-relocation allocations List-Id: Discussion and patches related to U-Boot Concept Archived-At: List-Archive: List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: From: Simon Glass When mcheck heap-protection is enabled, each allocation adds quite a bit of overhead for headers and canaries. While this is needed for the full allocator, it serves no purpose for pre-relocation allocations, since: 1. Simple malloc is a bump allocator that cannot free memory 3. Mcheck's corruption-detection provides no benefit for non-freeable memory Since the pre-relocation heap space is limited (typically <16KB), this overhead can exhaust the heap, causing boot failures. Fix this by bypassing mcheck hooks in dlmalloc(), dlfree(), dlmemalign() and dlcalloc() when called before relocation, directly calling the simple malloc functions instead. Co-developed-by: Claude Opus 4.5 Signed-off-by: Simon Glass --- (no changes since v1) common/dlmalloc.c | 33 ++++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/common/dlmalloc.c b/common/dlmalloc.c index 62e1e0a77da..5069a95d9b3 100644 --- a/common/dlmalloc.c +++ b/common/dlmalloc.c @@ -5984,6 +5984,15 @@ static const char *mcheck_caller(void) void *dlmalloc(size_t bytes) { + /* + * Skip mcheck for simple malloc (pre-relocation). Simple malloc is a + * bump allocator that can't free, so mcheck overhead is useless and + * wastes the limited pre-relocation heap space. + */ + if (CONFIG_IS_ENABLED(SYS_MALLOC_F) && + !(gd->flags & GD_FLG_FULL_MALLOC_INIT)) + return malloc_simple(bytes); + mcheck_pedantic_prehook(); size_t fullsz = mcheck_alloc_prehook(bytes); void *p = dlmalloc_impl(fullsz CALLER_NULL); @@ -5993,7 +6002,15 @@ void *dlmalloc(size_t bytes) return mcheck_alloc_posthook(p, bytes, mcheck_caller()); } -void dlfree(void *mem) { dlfree_impl(mcheck_free_prehook(mem)); } +void dlfree(void *mem) +{ + if (CONFIG_IS_ENABLED(SYS_MALLOC_F) && + !(gd->flags & GD_FLG_FULL_MALLOC_INIT)) { + dlfree_impl(mem); + return; + } + dlfree_impl(mcheck_free_prehook(mem)); +} void *dlrealloc(void *oldmem, size_t bytes) { @@ -6020,6 +6037,10 @@ void *dlrealloc(void *oldmem, size_t bytes) void *dlmemalign(size_t alignment, size_t bytes) { + if (CONFIG_IS_ENABLED(SYS_MALLOC_F) && + !(gd->flags & GD_FLG_FULL_MALLOC_INIT)) + return memalign_simple(alignment, bytes); + mcheck_pedantic_prehook(); size_t fullsz = mcheck_memalign_prehook(alignment, bytes); void *p = dlmemalign_impl(alignment, fullsz); @@ -6033,6 +6054,16 @@ void *dlmemalign(size_t alignment, size_t bytes) void *dlcalloc(size_t n, size_t elem_size) { + if (CONFIG_IS_ENABLED(SYS_MALLOC_F) && + !(gd->flags & GD_FLG_FULL_MALLOC_INIT)) { + size_t sz = n * elem_size; + void *p = malloc_simple(sz); + + if (p) + memset(p, '\0', sz); + return p; + } + mcheck_pedantic_prehook(); /* NB: no overflow check here */ size_t fullsz = mcheck_alloc_prehook(n * elem_size);