From patchwork Tue Jul 4 18:24:02 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Siddhesh Poyarekar X-Patchwork-Id: 1803346 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=sourceware.org (client-ip=8.43.85.97; helo=server2.sourceware.org; envelope-from=libc-alpha-bounces+incoming=patchwork.ozlabs.org@sourceware.org; receiver=) Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; secure) header.d=sourceware.org header.i=@sourceware.org header.a=rsa-sha256 header.s=default header.b=AhaYWzWz; dkim-atps=neutral Received: from server2.sourceware.org (ip-8-43-85-97.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4QwWRP3mjnz20ZF for ; Wed, 5 Jul 2023 04:24:45 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 52CF63858C31 for ; Tue, 4 Jul 2023 18:24:43 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 52CF63858C31 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1688495083; bh=afhZWQ0px5WUtCLp+KeqXmuESF5RZO5qYxgSJLey2rs=; h=To:Cc:Subject:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=AhaYWzWz5+39xUd5wpTg0evr1k39HQkWyUNCj8c//D9JUBtd/HCFM3ouB43J0QxJg sMzjYnjIOh0fdqAk9eAQbItloCdw5zgJC4y6dn/9tIg7jEw3HizJuILsAFpi+FWQXh 94rsTwT6dMI+z38Ac4GyFRVeiHN5TI31KH9JGtWA= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from eastern.birch.relay.mailchannels.net (eastern.birch.relay.mailchannels.net [23.83.209.55]) by sourceware.org (Postfix) with ESMTPS id 806F33858D32 for ; Tue, 4 Jul 2023 18:24:27 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 806F33858D32 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 4A86454191E; Tue, 4 Jul 2023 18:24:26 +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 83A3A5420B4; Tue, 4 Jul 2023 18:24:25 +0000 (UTC) ARC-Seal: i=1; s=arc-2022; d=mailchannels.net; t=1688495065; a=rsa-sha256; cv=none; b=4Js9+nET40VbuGfjy8zKFksub1uHJ6MVFqM7gfIoXvrTlgvVpFQIWxA+ULlW2J0ZVUWX+o C4T6r6SwU/cE696VcOuvHeuSYAPR+4FjI1Q5JPIxD0i39N5Iq78A/98yCddlkR0x/l3vSt yweI8zVKm5af4Jyg+zMXk2Nr7kdTtQEeglSGyBnrCkUfAdLmCGWAdZfg6jMB2RgKiCTAMW fZlN0n0Aa/WiNu73L961iSdg53zo7qO4D6UeAkTKD1SYrn9Gy6GwZRleD/IxnnxV95U5ae hCa0+EeptvL1Y1ZshklnB7yujfMKQKJJN+SU6t0aV559l9WRZ2qhgK3mklpUgg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=mailchannels.net; s=arc-2022; t=1688495065; 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=afhZWQ0px5WUtCLp+KeqXmuESF5RZO5qYxgSJLey2rs=; b=ysP1cPWIaztXIOJT6v7Km81elkRXFY6Jx1cnomKZFAXDFRmd7WTu17yILJgWspyQh1rkI+ aD4t7TqnkmXu/AbsN1qSgo55k5J6EDLtIp8wOpD9gYGAxJ3Eu9OX8EXVRtZC96ri0wS4Ps bq6Dccpvlqe/E5oT1CB3JGZ8DZrH+RXNIDizrCD1c3kq/BECq328deSw9fMuRqO+UPnpjW 1qcNP6NcWyB9KtsxuZ5Jl++i7zW0kJI6ekPexh94IyY661WEB9w5G4biWd1XTqiU1/9Q0e EcwRDojcoJ7Kyy9JPxRAoRRULTxxx4TL8wpHaGP8x+tw/wQwsKGYoBYrf3DEsw== ARC-Authentication-Results: i=1; rspamd-9fcc56855-59fcg; 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-Chemical-Whimsical: 061d76d2562a8dc9_1688495065942_2633939879 X-MC-Loop-Signature: 1688495065942:4204058333 X-MC-Ingress-Time: 1688495065942 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.230 (trex/6.9.1); Tue, 04 Jul 2023 18:24:25 +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 4QwWQx0LvYzBB; Tue, 4 Jul 2023 11:24:24 -0700 (PDT) To: libc-alpha@sourceware.org Cc: Nicolas Dusart , Aurelien Jarno Subject: [PATCH] realloc: Limit chunk reuse to only growing requests [BZ #30579] Date: Tue, 4 Jul 2023 14:24:02 -0400 Message-ID: <20230704182402.1040962-1-siddhesh@sourceware.org> X-Mailer: git-send-email 2.41.0 MIME-Version: 1.0 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, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, 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 X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Siddhesh Poyarekar via Libc-alpha From: Siddhesh Poyarekar Reply-To: Siddhesh Poyarekar Errors-To: libc-alpha-bounces+incoming=patchwork.ozlabs.org@sourceware.org Sender: "Libc-alpha" 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 --- The test case in the bz seems fixed with this, bringing VSZ and RSS back to ~40M from ~1G. Aurelien, can you please test with plasma desktop? Thanks, Sid malloc/malloc.c | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/malloc/malloc.c b/malloc/malloc.c index b8c0f4f580..e2f1a615a4 100644 --- a/malloc/malloc.c +++ b/malloc/malloc.c @@ -3417,16 +3417,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);