From patchwork Mon Dec 1 17:04:54 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Simon Glass X-Patchwork-Id: 790 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=1764608928; bh=wF/bpcc5ZeRJtVrZspsZtNd/KSoN1TKJDDf5FPcsmcs=; 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=K+vLQ7l3eAlLrTaRcQOdiBZ4UQi9Z+Jhp/e1i66x8wF6YLjtLU0aPqm3bBeZbRvwW hcBFXL5f6EluSf1TQko2cjG2VUjDHVe8UbrUnAIi4K32AUo2rF3alosaAqi5M6mxlD WrfLv6L1HzXG6ELtScOJgsFYk4V1H4TNvH8Def7Tgc326fHA4vffFC0PP5ixkLakZ1 Nq6sYa3cU/9M3dMfacAsjSyHNnPhPoJZMR0/KoKWpvMJVxjGPc5t0dqWU13MzCaNO8 ahZoj33P869f2GDbCUbYk2Gw6KT2D8PpLdjUOVs+9GuF4ESfgFVSbCMZspnlgIRM0d Xp/GCl6E8wEmw== Received: from localhost (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id D210668821 for ; Mon, 1 Dec 2025 10:08:48 -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 tBgIUyf7Zoo4 for ; Mon, 1 Dec 2025 10:08:48 -0700 (MST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1764608928; bh=wF/bpcc5ZeRJtVrZspsZtNd/KSoN1TKJDDf5FPcsmcs=; 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=K+vLQ7l3eAlLrTaRcQOdiBZ4UQi9Z+Jhp/e1i66x8wF6YLjtLU0aPqm3bBeZbRvwW hcBFXL5f6EluSf1TQko2cjG2VUjDHVe8UbrUnAIi4K32AUo2rF3alosaAqi5M6mxlD WrfLv6L1HzXG6ELtScOJgsFYk4V1H4TNvH8Def7Tgc326fHA4vffFC0PP5ixkLakZ1 Nq6sYa3cU/9M3dMfacAsjSyHNnPhPoJZMR0/KoKWpvMJVxjGPc5t0dqWU13MzCaNO8 ahZoj33P869f2GDbCUbYk2Gw6KT2D8PpLdjUOVs+9GuF4ESfgFVSbCMZspnlgIRM0d Xp/GCl6E8wEmw== Received: from mail.u-boot.org (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id C0C95688AE for ; Mon, 1 Dec 2025 10:08:48 -0700 (MST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1764608926; bh=bF28b1KaQY7EmuDEf/nx84X8UlmQuXrjv1BPETC3MtU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=qsU02jj6gESTqRRnd2DWXgtHX2avVi897h6mR9h2UPyPgIid5o36mQp114VTPFhCN 7Rh+z9qt+6gRs9o3oRlZ29Q5tRxKvD2bF29dCQFPz++LmGtV7eK5CDz269snLk0xAj xWkB5eJilH+AwCRqzLU6dQAD9buja6tQRzPWg8WG7PlQC6/nM5Drm3nCrLWyAFG/Xx yiHjWp3Hs3iCYmfdab8ExWXckppWldhY3s/m+K45qq5ccgTIYcBnDhPDFZ9yQUwhdk 6bRCnMWw38RiDZd2bi3odGIczWJ2FyNJNJhYmUnFzYoNMhKxMlU6ZOyGOtzM2mD1ZO fV+IRRgU47btg== Received: from localhost (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id A7A44686B9; Mon, 1 Dec 2025 10:08:46 -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 q0IuYLAPrbF6; Mon, 1 Dec 2025 10:08:46 -0700 (MST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1764608921; bh=rAmFs8IBf5jFCoVNxtIfkaO3oe0Jge4ysixPryiGxmw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Un6zp4jo4k6iaYw/lCX1DuZT6RlyUUaIqPupovtFXNM/iztKEWQSyfEBTmwFNuTac QyD82lOulT5GdU+vkI5nsM6s1/+rSd/zgcT9iasCBN043GuE4oRani65dkXNzq84l/ 2qFH03dY0e69FE50hWf4tm8PIdBw3tEVbhHpGdv0pUmBuBI+wV5Wi2DpNFbasCOM29 cEAmLEvEve0zZUbKFjFPS6JS1b+5iWiPl/tbDy3hdlTaN3P4ouqEnveJXOTwu7Kud9 0UGd1JHZEJFnLwaXtBrMTdWYvsu/bqMXpYZ2SJ7xUHkl2u2iE2a/rUMIIRJgYzXky+ QqbiRV3hnZPGA== Received: from u-boot.org (unknown [73.34.74.121]) by mail.u-boot.org (Postfix) with ESMTPSA id E8301688A4; Mon, 1 Dec 2025 10:08:40 -0700 (MST) From: Simon Glass To: U-Boot Concept Date: Mon, 1 Dec 2025 10:04:54 -0700 Message-ID: <20251201170529.3237986-16-sjg@u-boot.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20251201170529.3237986-1-sjg@u-boot.org> References: <20251201170529.3237986-1-sjg@u-boot.org> MIME-Version: 1.0 Message-ID-Hash: ZKGKS2ROJ4MUUFL2XNGA3VTSRJV5QSOS X-Message-ID-Hash: ZKGKS2ROJ4MUUFL2XNGA3VTSRJV5QSOS 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 , Stephen Warren , Tom Rini , Lukasz Majewski , Claude X-Mailman-Version: 3.3.10 Precedence: list Subject: [Concept] [PATCH 15/37] malloc: work around some memalign fragmentation issues 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 Use of memalign can trigger fragmentation issues where the over-sized allocation needed to guarantee alignment fails, even though the exact user-requested size would succeed and be properly aligned. If the padded allocation fails, try allocating exactly the user's requested size. If that happens to be aligned, return it. Otherwise, try a third allocation with just enough extra space to achieve alignment. Changes from original commits: - Port to dlmalloc 2.8.6 internal_memalign() instead of mEMALIGn() - Use internal_malloc/internal_free instead of mALLOc/fREe Signed-off-by: Stephen Warren Reviewed-by: Tom Rini Acked-by: Lukasz Majewski Co-developed-by: Claude Signed-off-by: Simon Glass (cherry picked from 4f144a416469c6a29127b0656523ae628ea7cbaf) --- common/dlmalloc.c | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/common/dlmalloc.c b/common/dlmalloc.c index 268d3fea52a..5a8e463671c 100644 --- a/common/dlmalloc.c +++ b/common/dlmalloc.c @@ -4975,6 +4975,46 @@ static void* internal_memalign(mstate m, size_t alignment, size_t bytes) { size_t nb = request2size(bytes); size_t req = nb + alignment + MIN_CHUNK_SIZE - CHUNK_OVERHEAD; mem = internal_malloc(m, req); +#ifdef __UBOOT__ + /* + * The attempt to over-allocate (with a size large enough to guarantee the + * ability to find an aligned region within allocated memory) failed. + * + * Try again, this time only allocating exactly the size the user wants. + * If the allocation now succeeds and just happens to be aligned, we can + * still fulfill the user's request. + */ + if (mem == 0) { + size_t extra, extra2; + + mem = internal_malloc(m, bytes); + /* Aligned -> use it */ + if (mem != 0 && (((size_t)(mem)) & (alignment - 1)) == 0) + return mem; + /* + * Otherwise, try again, requesting enough extra space to be able to + * acquire alignment. + */ + if (mem != 0) { + internal_free(m, mem); + /* Add in extra bytes to match misalignment of unexpanded alloc */ + extra = alignment - (((size_t)(mem)) % alignment); + mem = internal_malloc(m, bytes + extra); + /* + * mem might not be the same as before. Validate that the previous + * value of extra still works for the current value of mem. + */ + if (mem != 0) { + extra2 = alignment - (((size_t)(mem)) % alignment); + if (extra2 > extra) { + internal_free(m, mem); + mem = 0; + } + } + } + /* Fall through to original NULL check and chunk splitting logic */ + } +#endif if (mem != 0) { mchunkptr p = mem2chunk(mem); if (PREACTION(m))