From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from duck.ash.relay.mailchannels.net (duck.ash.relay.mailchannels.net [23.83.222.52]) by sourceware.org (Postfix) with ESMTPS id BA63F3858C2D for ; Thu, 6 Jul 2023 15:41:12 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org BA63F3858C2D Authentication-Results: sourceware.org; dmarc=fail (p=none dis=none) header.from=sourceware.org Authentication-Results: sourceware.org; spf=fail smtp.mailfrom=sourceware.org X-Sender-Id: dreamhost|x-authsender|siddhesh@gotplt.org Received: from relay.mailchannels.net (localhost [127.0.0.1]) by relay.mailchannels.net (Postfix) with ESMTP id E072A140D6F; Thu, 6 Jul 2023 15:41:07 +0000 (UTC) Received: from pdx1-sub0-mail-a286.dreamhost.com (unknown [127.0.0.6]) (Authenticated sender: dreamhost) by relay.mailchannels.net (Postfix) with ESMTPA id 1B76C140FE3; Thu, 6 Jul 2023 15:41:07 +0000 (UTC) ARC-Seal: i=1; s=arc-2022; d=mailchannels.net; t=1688658067; a=rsa-sha256; cv=none; b=djRbDY8ZNF+zE5IJVqvUgjeyGmLXAv6eQ2Vfe7wRenzIHQ5ddwgDozkChbF+9MT7H5b9vl 4KjKSpbWEJedhGvvgISLsQWji8qyiMSx4iuQlLVnNuLHLJxyvTzJxboWKbZs+HlV/cfihA m64wxhPqt9KQocJsDQQZO8jNKl/RCIFjlB8vvunikWzKiC8nUs1s+wx0BFo86u1mGeDFLl tM/5RgKSTOC83Ty1nNcRAoyiUzYlAxe6jETb+LIdAQL8mJA6VFkpFbD0D6UbweeWkA3Dtj /kAFrffpx8pgBe3eFDfYEl+KftN5XvQjUvIs3enpF97HsVJBVEZo4K7gpw7Grw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=mailchannels.net; s=arc-2022; t=1688658067; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=+J5MMoKs7NJkByHOhOCC+RQUlhQuHOaFQqsPig1Lo30=; b=L/pIUkrAOza6j9wP411FIEJ1IG4qIarvCJKj/CQORbidb3COhDMGJx/y0+l1tthB9JNxC9 ni4Y80XqUCC6i/XXZ2P/CEDipRw3onAeMjYJugln9lq7Xxh0bBTPBWyCzGuXy81kK9tnSU Jg+97gxBy10meLA6vbRxuv/7c8l3+xW2fWldf4wwwvI8LjioTWbVLmz7qIl87YunLBcwhv bYEF243GYJc0FYA32WLTZziRZas8PcC+kk8PvGSzbvjlxs6470eqn3BJur9TmRrWZPDEHV vJPqXQBwJhPRPfMM7JVO4Y93QQhf3H83cVq0zYjngR4tmoJ56etp33PgPk46ug== ARC-Authentication-Results: i=1; rspamd-9fcc56855-g45mn; auth=pass smtp.auth=dreamhost smtp.mailfrom=siddhesh@sourceware.org X-Sender-Id: dreamhost|x-authsender|siddhesh@gotplt.org X-MC-Relay: Neutral X-MC-Copy: stored-urls X-MailChannels-SenderId: dreamhost|x-authsender|siddhesh@gotplt.org X-MailChannels-Auth-Id: dreamhost X-Chief-Grain: 07028afa5b013a95_1688658067666_2618447825 X-MC-Loop-Signature: 1688658067666:2569963028 X-MC-Ingress-Time: 1688658067666 Received: from pdx1-sub0-mail-a286.dreamhost.com (pop.dreamhost.com [64.90.62.162]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384) by 100.116.217.243 (trex/6.9.1); Thu, 06 Jul 2023 15:41:07 +0000 Received: from fedora.redhat.com (bras-vprn-toroon4834w-lp130-09-174-91-45-44.dsl.bell.ca [174.91.45.44]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) (Authenticated sender: siddhesh@gotplt.org) by pdx1-sub0-mail-a286.dreamhost.com (Postfix) with ESMTPSA id 4QxgjZ4P1nz5C; Thu, 6 Jul 2023 08:41:06 -0700 (PDT) From: Siddhesh Poyarekar To: libc-stable@sourceware.org Cc: Nicolas Dusart , Aurelien Jarno Subject: [committed v2.37] realloc: Limit chunk reuse to only growing requests [BZ #30579] Date: Thu, 6 Jul 2023 11:41:04 -0400 Message-ID: <20230706154104.3418043-1-siddhesh@sourceware.org> X-Mailer: git-send-email 2.41.0 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-1172.1 required=5.0 tests=BAYES_00,GIT_PATCH_0,KAM_DMARC_NONE,KAM_DMARC_STATUS,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_SOFTFAIL,TXREP,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: The trim_threshold is too aggressive a heuristic to decide if chunk reuse is OK for reallocated memory; for repeated small, shrinking allocations it leads to internal fragmentation and for repeated larger allocations that fragmentation may blow up even worse due to the dynamic nature of the threshold. Limit reuse only when it is within the alignment padding, which is 2 * size_t for heap allocations and a page size for mmapped allocations. There's the added wrinkle of THP, but this fix ignores it for now, pessimizing that case in favor of keeping fragmentation low. This resolves BZ #30579. Signed-off-by: Siddhesh Poyarekar Reported-by: Nicolas Dusart Reported-by: Aurelien Jarno Reviewed-by: Aurelien Jarno Tested-by: Aurelien Jarno (cherry picked from commit 2fb12bbd092b0c10f1f2083216e723d2406e21c4) --- malloc/malloc.c | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/malloc/malloc.c b/malloc/malloc.c index fd8b52bfac..67df9f8c51 100644 --- a/malloc/malloc.c +++ b/malloc/malloc.c @@ -3398,16 +3398,23 @@ __libc_realloc (void *oldmem, size_t bytes) if (__glibc_unlikely (mtag_enabled)) *(volatile char*) oldmem; - /* Return the chunk as is whenever possible, i.e. there's enough usable space - but not so much that we end up fragmenting the block. We use the trim - threshold as the heuristic to decide the latter. */ - size_t usable = musable (oldmem); - if (bytes <= usable - && (unsigned long) (usable - bytes) <= mp_.trim_threshold) - return oldmem; - /* chunk corresponding to oldmem */ const mchunkptr oldp = mem2chunk (oldmem); + + /* Return the chunk as is if the request grows within usable bytes, typically + into the alignment padding. We want to avoid reusing the block for + shrinkages because it ends up unnecessarily fragmenting the address space. + This is also why the heuristic misses alignment padding for THP for + now. */ + size_t usable = musable (oldmem); + if (bytes <= usable) + { + size_t difference = usable - bytes; + if ((unsigned long) difference < 2 * sizeof (INTERNAL_SIZE_T) + || (chunk_is_mmapped (oldp) && difference <= GLRO (dl_pagesize))) + return oldmem; + } + /* its size */ const INTERNAL_SIZE_T oldsize = chunksize (oldp); -- 2.41.0