From patchwork Sat Jan 3 20:04:53 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Simon Glass X-Patchwork-Id: 1240 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=1767470819; bh=skxCC+wdwTZEn02U/drCi/yfcrKJzseOk7maRjIevk4=; 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=v6Yb7D/SUTjL4rx6J3f5cp9wLWfJpkU4pa5eeBd3Xh5SVe28gNusNnMeMFovBEI4M uef6dkJ48zM2jUEZo2Bn15nKkXWFnFuczwAkyG0Te6uBxHgFYIcW6dLiBr8WilpToc fhhZO8ZME3q2wsOFrdcBD2d8GrbdRoj+znp0r2B0WziyCj7tw9NVadAyviZF59Z4PO vnSAMP7MtNqKAWxQU9jDvyV3kiD8HGM+SDvN/GQwEHSQ1KRfjRILtyRNQVfsDK8Isy dYXku+i0y7hJfKjaAtDb3WqOjmmExn18m3Uxn/Il9DcQAV1kEb4dDr6eGSiARABDh8 f0wIsgMCDnd+Q== Received: from localhost (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id 01DD6690A1 for ; Sat, 3 Jan 2026 13:06:59 -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 BcCRgi_nRGY9 for ; Sat, 3 Jan 2026 13:06:58 -0700 (MST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1767470818; bh=skxCC+wdwTZEn02U/drCi/yfcrKJzseOk7maRjIevk4=; 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=GoCytdKTooUUzHxHTroKu2hBvL4dhdqTEJCV0OxohG0+P+9UX9rWOGywDJwSirr2A dnXPFVJoZaakvw7MU9WsqHve0B+76RmN6EdqEVEugKm7oZKsdiNd/v4C+5mpaHHLwm 2L3xQ48Ico2zclvSoS2E/hG1JOmRC+YJOZ4mhRb3v60PMJ0qULoWaeJ9vxWJJHjxiZ uiPv59qkYFtxh9nz88/eX+U0FyqShQO5icswvHdcr0k+1K51lYATpupYcbYpob7eRY /1U/mUWi/ljpxyDw8Z0tt4MAbP6OjJN6nqOsmpeBXJueLQKN7h6gS9z28madQ1skCR nqujp76zMPLig== Received: from mail.u-boot.org (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id E58EA69097 for ; Sat, 3 Jan 2026 13:06:58 -0700 (MST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1767470817; bh=ifGlxxXIOUqPWuaD+Dwyl6XMaBN0ZOzw9hkYA9MTr60=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ODjEVY81bRM8A67D414c8xL+dQWkO5z5CjFlOL4sEXqH9MbG9yRq7+ZhO7L3BdUxh z/oMGCNZLWnm7gZ17SQWpE1rRhpsZEbgbXwjeTvyzLh9VO5WlacA65aAym/Ew9lQlS vl/fwyLeSDmpkPfqsADBPg76pkNaEmcoeknI6Mrqm221EjvR++p0ffF1oq8HDRGETK 9hvePl5Ho0KTmOBl9h/TSQc4sDpCXA6qX2i5eWkELioSKVGF6OyNyhNxru77Hctz5G ynX8Atvm+Q03A8MAgDGzKG6hWzNwja8FK/OL8ianlXLq3/EI7hkd2jF4rXBGrDGyX5 twzQNdccDtO3Q== Received: from localhost (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id AA3EC68FCC; Sat, 3 Jan 2026 13:06:57 -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 ARepfVZCLchf; Sat, 3 Jan 2026 13:06:57 -0700 (MST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1767470813; bh=/v0OaphbWu+qsHZGjMqpOtWNJ2FW96aYgV1zhIMYTrI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=WtxBFH7Wpqf5ELE3b8BNhJX/Q3HdQEBx5scmxzEzzHPXoTIgdN2O0Ka76xcZgENpk K/nWkOCMEuD7zwEdHazTHzWDtwDU3BFGsSWMIv29HGlym/nY9cfi9f0SBszj01mmGv f34w3pkpyiKoXPMHo/Vuocatt/ab/bNsADLioYC3i+t54OeegpuWKp4S0Bh3FsKu/U l9N9deSmL752R5cJF19Z5UeoUXDmI9ZoHftgL5lrNUnc6WuFC9d4hAvlPeC9q6eBLQ n0KavGH1FPkTf7cvCuYMDBBDM7HTsm+j/S1p9qzanWGnxu+4BIOMdceXZ3Q9bzJDeE cLp+xTJYqzaDg== Received: from u-boot.org (unknown [73.34.74.121]) by mail.u-boot.org (Postfix) with ESMTPSA id 4E18F68FD2; Sat, 3 Jan 2026 13:06:53 -0700 (MST) From: Simon Glass To: U-Boot Concept Date: Sat, 3 Jan 2026 13:04:53 -0700 Message-ID: <20260103200510.3605009-23-sjg@u-boot.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20260103200510.3605009-1-sjg@u-boot.org> References: <20260103200510.3605009-1-sjg@u-boot.org> MIME-Version: 1.0 Message-ID-Hash: FWPH4Y6LUJ33O37KBAAUXOTEDKEQ5WOV X-Message-ID-Hash: FWPH4Y6LUJ33O37KBAAUXOTEDKEQ5WOV 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 X-Mailman-Version: 3.3.10 Precedence: list Subject: [Concept] [PATCH v2 22/29] malloc: Add an option to disable mcheck-backtrace collection 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 Backtrace collection is relatively expensive and can significantly slow down malloc()-heavy code when mcheck is enabled. Add a new CONFIG_MCHECK_BACKTRACE option (default y) to allow disabling backtrace collection while keeping the other mcheck features (canaries, double-free detection, etc.) enabled. This allows using mcheck with less overhead when caller information is not needed. Co-developed-by: Claude Signed-off-by: Simon Glass --- (no changes since v1) Kconfig | 10 ++++++++++ common/dlmalloc.c | 8 ++++++++ doc/develop/malloc.rst | 7 +++++++ 3 files changed, 25 insertions(+) diff --git a/Kconfig b/Kconfig index d47077d43bc..a058bd1a2aa 100644 --- a/Kconfig +++ b/Kconfig @@ -355,6 +355,16 @@ config MCHECK_HEAP_PROTECTION significantly increases memory overhead and should only be used for debugging. +config MCHECK_BACKTRACE + bool "Enable backtrace for mcheck caller info" + depends on MCHECK_HEAP_PROTECTION + default y + help + Enable collection of stack backtrace for each allocation. This + records the function/file/line that allocated the memory, useful + for debugging memory leaks. Disable this to reduce the overhead + of mcheck, since backtrace collection is relatively expensive. + config MCHECK_CALLER_LEN int "Length of caller string in mcheck header" depends on MCHECK_HEAP_PROTECTION diff --git a/common/dlmalloc.c b/common/dlmalloc.c index 0e363e78e26..ebcf8439ff7 100644 --- a/common/dlmalloc.c +++ b/common/dlmalloc.c @@ -5960,6 +5960,7 @@ STATIC_IF_MCHECK size_t dlmalloc_usable_size_impl(const void *mem) #include #include "mcheck_core.inc.h" +#if CONFIG_IS_ENABLED(MCHECK_BACKTRACE) /* Guard against recursive backtrace calls during malloc */ static bool in_backtrace __section(".data"); @@ -5968,6 +5969,7 @@ static bool in_backtrace __section(".data"); * Set via malloc_backtrace_skip() before calling panic(). */ static bool mcheck_skip_backtrace __section(".data"); +#endif /* Runtime flag to disable mcheck - allows bypassing heap protection */ static bool mcheck_disabled __section(".data"); @@ -5979,11 +5981,14 @@ void mcheck_set_disabled(bool disabled) void malloc_backtrace_skip(bool skip) { +#if CONFIG_IS_ENABLED(MCHECK_BACKTRACE) mcheck_skip_backtrace = skip; +#endif } static const char *mcheck_caller(void) { +#if CONFIG_IS_ENABLED(MCHECK_BACKTRACE) const char *caller = NULL; if (!in_backtrace && !mcheck_skip_backtrace) { @@ -5992,6 +5997,9 @@ static const char *mcheck_caller(void) in_backtrace = false; } return caller; +#else + return NULL; +#endif } #if CONFIG_IS_ENABLED(MCHECK_LOG) diff --git a/doc/develop/malloc.rst b/doc/develop/malloc.rst index b9ab884d419..776294809b8 100644 --- a/doc/develop/malloc.rst +++ b/doc/develop/malloc.rst @@ -130,6 +130,13 @@ Main U-Boot (post-relocation) ``malloc dump``. This significantly increases memory overhead and should only be used for debugging. Default: n +``CONFIG_MCHECK_BACKTRACE`` + Bool to enable backtrace collection for mcheck caller information. When + enabled (the default), each allocation records a stack backtrace showing + where it was made. This is useful for debugging memory leaks but adds + overhead to every malloc call. Disable this to reduce mcheck overhead + while keeping the canary checks and double-free detection. Default: y + xPL Boot Phases ~~~~~~~~~~~~~~~