From patchwork Thu Dec 18 10:17:32 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Florian Weimer X-Patchwork-Id: 126770 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from vm01.sourceware.org (localhost [127.0.0.1]) by sourceware.org (Postfix) with ESMTP id C7BB14BA2E30 for ; Thu, 18 Dec 2025 10:19:43 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org C7BB14BA2E30 Authentication-Results: sourceware.org; dkim=pass (1024-bit key, unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=FkKrjixv X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTP id D11464BA2E2D for ; Thu, 18 Dec 2025 10:17:37 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org D11464BA2E2D Authentication-Results: sourceware.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org D11464BA2E2D Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1766053057; cv=none; b=Z0AN/SCeHze8CeXL8DdiXD7V9MEnCZI3VMXLfPrtkuK+q9RHs5mnaqyw0Epx+s5sINFFPIDLIfIr81kGIb4xQw+2JBaIz9Zxmaze6BRAalhakoLoJFYJlDfiHpsp+3Kwdnhw2gcFWxq897fbTQxKxMhZ4Tp3D48ZF1O+84djUDk= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1766053057; c=relaxed/simple; bh=3ZOvKUAeNF90nBV4tQXEYAOpga8xsKQ64O+2Ez/g5c0=; h=DKIM-Signature:From:To:Subject:Message-ID:Date:MIME-Version; b=AgiDa/W6qB2uDaR/JNrjq4EFp3F3NBmj3iZkFl+AEoDWrgydG279iqXmw/DbcjemzoJzLJ/wp3qQsPFCypGCiWL/UAtkGK4PBb9aucis4g6NLZB8kkukqpP5Jqf5R1YmSUabACa1P1Lux5RQYCZV0poNOKEkunPW2Ccaj95C8AA= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org D11464BA2E2D DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1766053057; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=LfmZYP7SafI2RqRi1tkXlT4BaLhshImMCQqqV2cZ6to=; b=FkKrjixvCBroWAK1hhN70FNxOVZpa/wuUnzYUrUJrhZpf7KFFZUSZN1pDp/O7xt40Y+8SL m/d+1GJOqSnM+rAzwudeTsrX6wjgeFF9N9M4FHCL4vN3jJ6jLXGMRMbXlcM2Ey0u8RNXH7 F918nhF8mOTc5X0LNDlG5eVs7U6rASA= Received: from mx-prod-mc-03.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-179-zPYs86piNsO8klYUvcoiaQ-1; Thu, 18 Dec 2025 05:17:36 -0500 X-MC-Unique: zPYs86piNsO8klYUvcoiaQ-1 X-Mimecast-MFC-AGG-ID: zPYs86piNsO8klYUvcoiaQ_1766053055 Received: from mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.111]) (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) by mx-prod-mc-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 3FEAA1955DC3 for ; Thu, 18 Dec 2025 10:17:35 +0000 (UTC) Received: from fweimer-oldenburg.csb.redhat.com (unknown [10.44.33.94]) by mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 8A93E18004D4 for ; Thu, 18 Dec 2025 10:17:34 +0000 (UTC) From: Florian Weimer To: libc-alpha@sourceware.org Subject: [PATCH 1/3] malloc: Perform batched frees if tcache is full In-Reply-To: Message-ID: <2625bdca771c84d837407762eb0d5fd0d0ccd983.1766051058.git.fweimer@redhat.com> References: X-From-Line: 2625bdca771c84d837407762eb0d5fd0d0ccd983 Mon Sep 17 00:00:00 2001 Date: Thu, 18 Dec 2025 11:17:32 +0100 User-Agent: Gnus/5.13 (Gnus v5.13) MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.111 X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: IFwqsolVf1sSQJ7Uxi6jiS8Qt4MjOQf9Gdr7DEx4B8o_1766053055 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-9.2 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_BLOCKED, RCVD_IN_MSPIKE_H2, RCVD_IN_SBL_CSS, RCVD_IN_VALIDITY_RPBL_BLOCKED, RCVD_IN_VALIDITY_SAFE_BLOCKED, SPF_HELO_PASS, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces~patchwork=sourceware.org@sourceware.org Batched frees amortize the locking overhead once the tcache is full. Previously, once the tcache slot was full, each free acquired the arena lock. With this change, neighboring tcache allocations in the same arena re-use an arena lock that already exists. Pass the tcache pointer to __libc_free_batch so that it is not necessary to reload the tcache pointer after the _int_free_chunk calls. --- malloc/malloc.c | 66 +++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 58 insertions(+), 8 deletions(-) diff --git a/malloc/malloc.c b/malloc/malloc.c index be29929993..d2184c8d7a 100644 --- a/malloc/malloc.c +++ b/malloc/malloc.c @@ -3037,7 +3037,8 @@ tcache_put_n (mchunkptr chunk, size_t tc_idx, tcache_entry **ep, bool mangled) available chunks to remove. Removes chunk from the middle of the list. */ static __always_inline void * -tcache_get_n (size_t tc_idx, tcache_entry **ep, bool mangled) +tcache_get_n (tcache_perthread_struct *tc, size_t tc_idx, tcache_entry **ep, + bool mangled) { tcache_entry *e; if (!mangled) @@ -3053,7 +3054,7 @@ tcache_get_n (size_t tc_idx, tcache_entry **ep, bool mangled) else *ep = PROTECT_PTR (ep, REVEAL_PTR (e->next)); - ++(tcache->num_slots[tc_idx]); + ++(tc->num_slots[tc_idx]); e->key = 0; return (void *) e; } @@ -3068,7 +3069,7 @@ tcache_put (mchunkptr chunk, size_t tc_idx) static __always_inline void * tcache_get (size_t tc_idx) { - return tcache_get_n (tc_idx, &tcache->entries[tc_idx], false); + return tcache_get_n (tcache, tc_idx, &tcache->entries[tc_idx], false); } static __always_inline tcache_entry ** @@ -3111,7 +3112,7 @@ tcache_get_large (size_t tc_idx, size_t nb) if (te == NULL || nb != chunksize (mem2chunk (te))) return NULL; - return tcache_get_n (tc_idx, entry, mangled); + return tcache_get_n (tcache, tc_idx, entry, mangled); } static void tcache_init (mstate av); @@ -3149,7 +3150,7 @@ tcache_get_align (size_t nb, size_t alignment) if (te != NULL && csize == nb && PTR_IS_ALIGNED (te, alignment)) - return tag_new_usable (tcache_get_n (tc_idx, tep, mangled)); + return tag_new_usable (tcache_get_n (tcache, tc_idx, tep, mangled)); DIAG_POP_NEEDS_COMMENT; } return NULL; @@ -3332,6 +3333,49 @@ tcache_free_init (void *mem) __libc_free (mem); } +/* Deallocate half of the tcache entries into arenas, to amortize the + locking overhead. */ +static __attribute_noinline__ void +__libc_free_batched (mchunkptr p, INTERNAL_SIZE_T size, + tcache_perthread_struct *tc, size_t tc_idx) +{ + /* Check size >= MINSIZE and p + size does not overflow. */ + if (__glibc_unlikely (INT_ADD_OVERFLOW ((uintptr_t) p, + size - MINSIZE))) + return malloc_printerr_tail ("free(): invalid size (batch)"); + + /* Empty half of the tcache, for a hysteresis effect. */ + unsigned int to_free = mp_.tcache_count / 2; + + /* If the arena does not change between chunks, keep the lock. */ + mstate av = arena_for_chunk (p); + __libc_lock_lock (av->mutex); + _int_free_chunk (av, p, size, true); + + while (tc->entries[tc_idx] != NULL && to_free > 0) + { + void *mem = tcache_get_n (tc, tc_idx, &tc->entries[tc_idx], false); + p = mem2chunk (mem); + size = chunksize (p); + + /* Lock a different arena if necessary. */ + { + mstate chunk_av = arena_for_chunk (p); + if (chunk_av != av) + { + __libc_lock_unlock (av->mutex); + av = chunk_av; + __libc_lock_lock (av->mutex); + } + } + + _int_free_chunk (av, p, size, true); + to_free--; + } + + __libc_lock_unlock (av->mutex); +} + void __libc_free (void *mem) { @@ -3370,6 +3414,13 @@ __libc_free (void *mem) { if (__glibc_likely (tcache->num_slots[tc_idx] != 0)) return tcache_put (p, tc_idx); + else + { + /* Perform batched freeing of tcache entries. */ + if (__glibc_unlikely (tcache_inactive ())) + return tcache_free_init (mem); + return __libc_free_batched (p, size, tcache, tc_idx); + } } else { @@ -3377,10 +3428,9 @@ __libc_free (void *mem) if (size >= MINSIZE && __glibc_likely (tcache->num_slots[tc_idx] != 0)) return tcache_put_large (p, tc_idx); + if (__glibc_unlikely (tcache_inactive ())) + return tcache_free_init (mem); } - - if (__glibc_unlikely (tcache_inactive ())) - return tcache_free_init (mem); } #endif