From 54ef51121b1d7d3c3be9807a88827fb98ba48b67 Mon Sep 17 00:00:00 2001 From: Slobodan Predolac Date: Fri, 8 May 2026 10:01:00 -0700 Subject: [PATCH] Extract postfork-child tcache list relink into tcache_arena_postfork_child --- include/jemalloc/internal/tcache_externs.h | 1 + src/arena.c | 16 +-------- src/tcache.c | 39 +++++++++++++++++----- 3 files changed, 32 insertions(+), 24 deletions(-) diff --git a/include/jemalloc/internal/tcache_externs.h b/include/jemalloc/internal/tcache_externs.h index b7fdb5a4..97024b2b 100644 --- a/include/jemalloc/internal/tcache_externs.h +++ b/include/jemalloc/internal/tcache_externs.h @@ -73,6 +73,7 @@ void tcaches_destroy(tsd_t *tsd, unsigned ind); bool tcache_boot(tsdn_t *tsdn, base_t *base); void tcache_arena_associate( tsdn_t *tsdn, tcache_slow_t *tcache_slow, tcache_t *tcache, arena_t *arena); +void tcache_arena_postfork_child(tsdn_t *tsdn, arena_t *arena); void tcache_prefork(tsdn_t *tsdn); void tcache_postfork_parent(tsdn_t *tsdn); void tcache_postfork_child(tsdn_t *tsdn); diff --git a/src/arena.c b/src/arena.c index 516791c8..a76b26e8 100644 --- a/src/arena.c +++ b/src/arena.c @@ -2089,21 +2089,7 @@ arena_postfork_child(tsdn_t *tsdn, arena_t *arena) { if (tsd_iarena_get(tsdn_tsd(tsdn)) == arena) { arena_nthreads_inc(arena, true); } - if (config_stats) { - ql_new(&arena->tcache_ql); - ql_new(&arena->cache_bin_array_descriptor_ql); - tcache_slow_t *tcache_slow = tcache_slow_get(tsdn_tsd(tsdn)); - if (tcache_slow != NULL && tcache_slow->arena == arena) { - tcache_t *tcache = tcache_slow->tcache; - ql_elm_new(tcache_slow, link); - ql_tail_insert(&arena->tcache_ql, tcache_slow, link); - cache_bin_array_descriptor_init( - &tcache_slow->cache_bin_array_descriptor, - tcache->bins); - ql_tail_insert(&arena->cache_bin_array_descriptor_ql, - &tcache_slow->cache_bin_array_descriptor, link); - } - } + tcache_arena_postfork_child(tsdn, arena); for (unsigned i = 0; i < nbins_total; i++) { JEMALLOC_SUPPRESS_WARN_ON_USAGE( diff --git a/src/tcache.c b/src/tcache.c index d898e0d5..c843dee7 100644 --- a/src/tcache.c +++ b/src/tcache.c @@ -720,6 +720,22 @@ tcache_bin_ncached_max_read( return false; } +/* + * Insert tcache_slow into arena's tcache list. Caller must hold + * arena->tcache_ql_mtx, OR be in the postfork-child path (single-threaded, + * mutex re-init pending). config_stats must be true. + */ +static void +tcache_arena_link(arena_t *arena, tcache_slow_t *tcache_slow, + tcache_t *tcache) { + ql_elm_new(tcache_slow, link); + ql_tail_insert(&arena->tcache_ql, tcache_slow, link); + cache_bin_array_descriptor_init( + &tcache_slow->cache_bin_array_descriptor, tcache->bins); + ql_tail_insert(&arena->cache_bin_array_descriptor_ql, + &tcache_slow->cache_bin_array_descriptor, link); +} + void tcache_arena_associate(tsdn_t *tsdn, tcache_slow_t *tcache_slow, tcache_t *tcache, arena_t *arena) { @@ -727,16 +743,8 @@ tcache_arena_associate(tsdn_t *tsdn, tcache_slow_t *tcache_slow, tcache_slow->arena = arena; if (config_stats) { - /* Link into list of extant tcaches. */ malloc_mutex_lock(tsdn, &arena->tcache_ql_mtx); - - ql_elm_new(tcache_slow, link); - ql_tail_insert(&arena->tcache_ql, tcache_slow, link); - cache_bin_array_descriptor_init( - &tcache_slow->cache_bin_array_descriptor, tcache->bins); - ql_tail_insert(&arena->cache_bin_array_descriptor_ql, - &tcache_slow->cache_bin_array_descriptor, link); - + tcache_arena_link(arena, tcache_slow, tcache); malloc_mutex_unlock(tsdn, &arena->tcache_ql_mtx); } } @@ -776,6 +784,19 @@ tcache_arena_reassociate(tsdn_t *tsdn, tcache_slow_t *tcache_slow, tcache_arena_associate(tsdn, tcache_slow, tcache, arena); } +void +tcache_arena_postfork_child(tsdn_t *tsdn, arena_t *arena) { + if (!config_stats) { + return; + } + ql_new(&arena->tcache_ql); + ql_new(&arena->cache_bin_array_descriptor_ql); + tcache_slow_t *tcache_slow = tcache_slow_get(tsdn_tsd(tsdn)); + if (tcache_slow != NULL && tcache_slow->arena == arena) { + tcache_arena_link(arena, tcache_slow, tcache_slow->tcache); + } +} + static void tcache_default_settings_init(tcache_slow_t *tcache_slow) { assert(tcache_slow != NULL);