From patchwork Thu Apr 16 02:29:29 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Simon Glass X-Patchwork-Id: 2167 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=1776306651; bh=s+cyT0qcb+XizdGy1wSZYHMYdDMuWEK/XBTKYQzZmdo=; 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=nFkQYqG+KWgP+a8JSV030BxI5xs4cJhpycsgPogmfzChJ3gYRr37UmzpbX0fm6bZl p99rw6FmLfpp8h9L9wdAGc20oqtMIGpu3Sub4ieGlq8KBcemx/LU1fDZbN0WAuvfZG AzvHJUdwfXdM/BvTrxHH74xz1We4Dvg71u4kHxfs= Received: from localhost (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id 2216F6A4A7 for ; Wed, 15 Apr 2026 20:30:51 -0600 (MDT) 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 x0CuWtrrLOCa for ; Wed, 15 Apr 2026 20:30:51 -0600 (MDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1776306650; bh=s+cyT0qcb+XizdGy1wSZYHMYdDMuWEK/XBTKYQzZmdo=; 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=QIgkuBLAFaf0SqmGbABr/YQ8JT1cpnbEftFtRJ4KtL5/xP/dG6PMT2LUfeKOiSk59 kAPFuQVDQkapTok9wD3B0xLj5zUUs9x1PB0KCQ9/Ig2ClL8/54tyQtViYGSaSA2+93 OAX/TgtwZZw9L5AyAtn4YD8deNQsO5x+u4wBoFWw= Received: from mail.u-boot.org (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id A375B6A4A8 for ; Wed, 15 Apr 2026 20:30:50 -0600 (MDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1776306648; bh=uOHaC2MGmnaF/cmLeDOmuXVZpyYO+aschl5ZH+i1dBA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Nu+lwgwnWXGWLxIaoWKXlvobFwBX4/PwJAXN8j06HUDVJJFax26XxtKDV7sShtvhr Re1SjXCExYgydsvLFRKmDAx106QbL+ZGVkP3zvqjChjeCmZNmt+1L4d/jU9jj5Xpuw aPuq0047Au6G2uYfS4VQJkwBgXlK1Nf/QrGQnD6c= Received: from localhost (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id 42A186A4A5; Wed, 15 Apr 2026 20:30:48 -0600 (MDT) 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 LsZ6haUzODYy; Wed, 15 Apr 2026 20:30:48 -0600 (MDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1776306646; bh=Wcwu90JGZLvWrZuFJbLSrsRUiXNH0oZPuhH5vg+gvYE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=DRkyJwPn7TtjGjy0DEXFU5aR20I1gKMP2JGmbOx8aXf7sMHvrpGVuBkf7tX4e4mN4 qVKFJxAYQUFw84jUEsqy1wsgSfYwf1CHGehmcqLgluvRJisB8qK/iEfoD7G2SnqHZV A4Umgh+uW2jj6x+QemZCMrQFafk5Z9dyLAgvU6bU= Received: from u-boot.org (unknown [73.34.74.121]) by mail.u-boot.org (Postfix) with ESMTPSA id B61756A4A0; Wed, 15 Apr 2026 20:30:46 -0600 (MDT) From: Simon Glass To: U-Boot Concept Date: Wed, 15 Apr 2026 20:29:29 -0600 Message-ID: <20260416023021.626949-5-sjg@u-boot.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20260416023021.626949-1-sjg@u-boot.org> References: <20260416023021.626949-1-sjg@u-boot.org> MIME-Version: 1.0 Message-ID-Hash: OSQFDQAK5RBFZRK7GSICY4KUFR3TVMH7 X-Message-ID-Hash: OSQFDQAK5RBFZRK7GSICY4KUFR3TVMH7 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: Simon Glass X-Mailman-Version: 3.3.10 Precedence: list Subject: [Concept] [PATCH 04/33] malloc: Expose the largest-contiguous free region 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 Heap-pressure tests like common_test_malloc_very_large() assume almost the whole heap is available as one contiguous free chunk. Even a single, stray, leaked allocation in the middle of the area breaks that assumption and fails the test, which makes the tests flakily depend on every earlier test running with a perfect cleanup. Add malloc_largest_free() which walks all segments (and the top and dv chunks) and returns the biggest free chunk size, minus the per-chunk dlmalloc overhead. Report it from 'malloc info' so the value is easy to see while diagnosing fragmentation, and check for the new line in cmd_test_malloc_info() Signed-off-by: Simon Glass --- cmd/malloc.c | 1 + common/dlmalloc.c | 29 +++++++++++++++++++++++++++++ include/malloc.h | 13 +++++++++++++ test/cmd/malloc.c | 1 + 4 files changed, 44 insertions(+) diff --git a/cmd/malloc.c b/cmd/malloc.c index 7de421c0ae8..94d0f9427d6 100644 --- a/cmd/malloc.c +++ b/cmd/malloc.c @@ -24,6 +24,7 @@ static int do_malloc_info(struct cmd_tbl *cmdtp, int flag, int argc, printf("total bytes = %s\n", format_size(buf, info.total_bytes)); printf("in use bytes = %s\n", format_size(buf, info.in_use_bytes)); + printf("largest free = %s\n", format_size(buf, malloc_largest_free())); printf("malloc count = %lu\n", info.malloc_count); printf("free count = %lu\n", info.free_count); printf("realloc count = %lu\n", info.realloc_count); diff --git a/common/dlmalloc.c b/common/dlmalloc.c index 895aa215228..6d0bdc5b64e 100644 --- a/common/dlmalloc.c +++ b/common/dlmalloc.c @@ -7900,6 +7900,35 @@ void malloc_leak_check_free(struct malloc_leak_snap *snap) snap->count = 0; } +size_t malloc_largest_free(void) +{ + size_t largest = 0; + mstate m = gm; + msegmentptr s; + + if (m->topsize > largest) + largest = m->topsize; + if (m->dvsize > largest) + largest = m->dvsize; + + for (s = &m->seg; s != 0; s = s->next) { + mchunkptr q = align_as_chunk(s->base); + + while (segment_holds(s, q) && q != m->top && + q->head != FENCEPOST_HEAD) { + if (!is_inuse(q)) { + size_t sz = chunksize(q); + + if (sz > largest) + largest = sz; + } + q = next_chunk(q); + } + } + + return largest > CHUNK_OVERHEAD ? largest - CHUNK_OVERHEAD : 0; +} + int initf_malloc(void) { #if CONFIG_IS_ENABLED(SYS_MALLOC_F) diff --git a/include/malloc.h b/include/malloc.h index 787ec999f5e..599440246b4 100644 --- a/include/malloc.h +++ b/include/malloc.h @@ -892,6 +892,19 @@ static inline size_t malloc_mcheck_count(void) { return 0; } */ size_t malloc_chunk_size(void *ptr); +/** + * malloc_largest_free() - Return the size of the largest free chunk + * + * Walks the heap and returns the largest contiguous free region that + * malloc() could currently hand out, minus the per-chunk dlmalloc + * overhead. Any mcheck header/canary overhead still comes off the top + * of the caller's request. Useful for tests that want to allocate "as + * much as possible" without tripping over fragmentation. + * + * Return: approximate largest request bytes malloc() would satisfy + */ +size_t malloc_largest_free(void); + /** * malloc_mcheck_hdr_size() - Return the size of the mcheck header * diff --git a/test/cmd/malloc.c b/test/cmd/malloc.c index 812f7530b79..e84033b3ce9 100644 --- a/test/cmd/malloc.c +++ b/test/cmd/malloc.c @@ -25,6 +25,7 @@ static int cmd_test_malloc_info(struct unit_test_state *uts) ut_assertok(run_command("malloc info", 0)); ut_assert_nextlinen("total bytes = "); ut_assert_nextlinen("in use bytes = "); + ut_assert_nextlinen("largest free = "); ut_assert_nextlinen("malloc count = "); ut_assert_nextlinen("free count = "); ut_assert_nextlinen("realloc count = ");