From patchwork Wed Apr 2 10:31:56 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: dudududumaxver@gmail.com X-Patchwork-Id: 109668 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id C7542384A80D for ; Wed, 2 Apr 2025 10:32:41 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org C7542384A80D Authentication-Results: sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20230601 header.b=fDOtysM+ X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-pl1-x636.google.com (mail-pl1-x636.google.com [IPv6:2607:f8b0:4864:20::636]) by sourceware.org (Postfix) with ESMTPS id 69FB6384A85B for ; Wed, 2 Apr 2025 10:32:02 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 69FB6384A85B Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 69FB6384A85B Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::636 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1743589922; cv=none; b=nJXh2iPGfIzqL4OwfEuqqGwFu5OlYYzTEB5syIi9BHfaIhS9rPVR2cQ64duwkTsxFzu8vMhprfeK/FKwhGev8VncKa9/XBBP7nxqB3567NZEfyeG3UOZOC7lWmreK/BT3tspGd9ZgHHLivOvFAHS4NvUvNUhrq5hTLq6g96QNj8= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1743589922; c=relaxed/simple; bh=Am+AyJjputu4DDhwhQzz75BvXizjW1RG2hRVhqrugBs=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=AX8LceGHSBPGYunU3oTJWGDhr0L0tIXjjTutv8mbHE/gzgIB7Nq0Pb823G9POiL+97b8UAR3rWWk2GNj3ylQu5TC2BtLM5jVwlwDqsPHgUqdrCXEFEJ3RpmxhC0T7R7MI51PzijOsEqYaEGtg3lTMPscl2AONQqZuRbbaXiTLG0= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 69FB6384A85B Received: by mail-pl1-x636.google.com with SMTP id d9443c01a7336-22438c356c8so125791415ad.1 for ; Wed, 02 Apr 2025 03:32:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1743589921; x=1744194721; darn=sourceware.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=o416YgEVB46rkZbFasnEz9K94Ykx696LpHOdJWMJuLA=; b=fDOtysM+zx4OESCbEGXlJLbwKFKdsEy4tm2CfMYAX11SxomPJpDgrf5LNs9XxSAsoF aRm3I3npjqoN/NGJ4Rn76abzoUQqdFwmwdbp8hbqUt7y3rTl5kq6aqvIrqNwH0B0iaM/ jpsTWls6SyOgVM8fv6ZleMEYW0nSMWbtKFcrXRcKrU+8+dRAWE2no3gwGUEWNWJxDdbU F23MebQEL7CrzKu0ca2nh6t9J9hJStJKNMtlTsj3QVQ/bnpvFfxsnJiphyd63NVXH8Pe x/CnLfqkeGukX/s68O/eHrfXIFoh3PSM3qujtHHG8ZRBKuibcDaVjwXdN4TD9GZ6LvWl cVCA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1743589921; x=1744194721; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=o416YgEVB46rkZbFasnEz9K94Ykx696LpHOdJWMJuLA=; b=gjjkgtDxZ80ev/ZZGefEAww3hdaymJ9XkFWV+gVloTXI4IRf5KcL249UIp/FFAMPSA z8+GR8LX9lG/c5WPjfzqVhL+XmCNRLbuLC9KDO3IPuBmVciX1+BpxUodJqoZi8Wzs5oR ZDhGP+C8QWoIgSyFyRBuHHsj6UdbOcjBGYkmvKise/qFx+ThnoFDi5sgMVRnpHLfZ6W6 tS1oEQfRJG4rJUX7mM7cMHOQt1i4COHnBPFitUVM8I+mpELrQ4NYA/NVrfyALq4LCCWd qFKYXET8YJCCx5DOzAy1hMdT33BiO9geDMF+8e2E0cIUe7W5Giq+VG8xae2nrggwE02W nNHg== X-Gm-Message-State: AOJu0YyTAVyRTSP3kqrZJmB334IPt3GKSJzWR7AUGtGj1yBhXPP+k0yO L5wDEMHsXMuhgStaR601dJ5sub7+tYA7V8CKnhHgQEQphKj5htPVd3z10u1+Ais= X-Gm-Gg: ASbGncv4dJI5JHnlyvpXnOBtWx0elZTJ8yyEjCoTux9oOfa/4EyZUz4SwI0BTdO2w2E DtTnmmjf16V871BHM79h+V7mdqqWblKmbDy0e0MuNPLuhxt+Quw9qSHeVzE9Ry4WyT8j1QxkA4g iNe05sZXoAmTMZ429p+MrncMZZ//5qLT5tOZ5L9om37poCQDYioepR+pCSDOiESwR9IuEoY+Ijv vHVb+ykiOtVv3gebvrPDKYOt4yx7KevwpkHDWaPBmDxU3C82xapI75cx39RBhcudRkxGqWXfOKK 9186vrtajUoXRpEfslv5dBpj+tv4BD42v6conbtJp3ztt7Y9nYSAR9jCDf4vZmr2gtC1T60gfyK Sg24bnpw= X-Google-Smtp-Source: AGHT+IFmZg8TNPjmbDPLHyZz0ivTzxXtOy/yBFqrKFZuITPXtScm+bRU+lXbpJiQ5dvLcb/9Wa+/sA== X-Received: by 2002:a05:6a20:9f0b:b0:1f5:8da5:ffe9 with SMTP id adf61e73a8af0-2009f6064d1mr23371039637.12.1743589921368; Wed, 02 Apr 2025 03:32:01 -0700 (PDT) Received: from localhost (122-116-43-253.hinet-ip.hinet.net. [122.116.43.253]) by smtp.gmail.com with UTF8SMTPSA id d2e1a72fcca58-73970deef92sm10590162b3a.4.2025.04.02.03.32.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 02 Apr 2025 03:32:00 -0700 (PDT) From: dudududumaxver@gmail.com X-Google-Original-From: dudududuMaxVer@gmail.com To: libc-alpha@sourceware.org Cc: dbgbgtf Subject: [PATCH] checking tcache mem size when allocating it Date: Wed, 2 Apr 2025 18:31:56 +0800 Message-ID: <20250402103156.230046-1-dudududuMaxVer@gmail.com> X-Mailer: git-send-email 2.49.0 MIME-Version: 1.0 X-Spam-Status: No, score=-10.9 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, HK_RANDOM_ENVFROM, HK_RANDOM_FROM, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP 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.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 From: dbgbgtf i just got how to make bench, so just ignore the earlier email pls:) checking if `tcache mem size == request size` to avoid arbitrary mem allocating from tcache bin plus, since fastbin will be slash into tcachebin, i remove the check when fastbin fall into tcachebin, they will be findout anyway(i am not so sure about this change, so be free to ignore this) finally, the bench, i think those data will be enough. is that cost acceptable? i am really not an expert on this. $ cat now/bench-malloc-simple-4096.out { "timing_type": "hp_timing", "functions": { "malloc": { "": { "malloc_block_size": 4096, "max_rss": 7492, "main_arena_st_allocs_0025_time": 50.4246, "main_arena_st_allocs_0100_time": 1788.27, "main_arena_st_allocs_0400_time": 2345.23, "main_arena_st_allocs_1600_time": 2600.93, "main_arena_mt_allocs_0025_time": 102.663, "main_arena_mt_allocs_0100_time": 1812.11, "main_arena_mt_allocs_0400_time": 2394.82, "main_arena_mt_allocs_1600_time": 2684.38, "thread_arena__allocs_0025_time": 100.688, "thread_arena__allocs_0100_time": 1744.47, "thread_arena__allocs_0400_time": 2341.64, "thread_arena__allocs_1600_time": 2633.91 } } } }% $ cat origin/bench-malloc-simple-4096.out { "timing_type": "hp_timing", "functions": { "malloc": { "": { "malloc_block_size": 4096, "max_rss": 7660, "main_arena_st_allocs_0025_time": 52.1138, "main_arena_st_allocs_0100_time": 1743.2, "main_arena_st_allocs_0400_time": 2336.48, "main_arena_st_allocs_1600_time": 2584.16, "main_arena_mt_allocs_0025_time": 98.8657, "main_arena_mt_allocs_0100_time": 1784.57, "main_arena_mt_allocs_0400_time": 2377.64, "main_arena_mt_allocs_1600_time": 2623.67, "thread_arena__allocs_0025_time": 97.7683, "thread_arena__allocs_0100_time": 1732.62, "thread_arena__allocs_0400_time": 2340.24, "thread_arena__allocs_1600_time": 2608.58 } } } }% $ cat now/bench-malloc-thread-32.out { "timing_type": "hp_timing", "functions": { "malloc": { "": { "duration": 9.33754e+11, "iterations": 9.06744e+09, "time_per_iteration": 102.979, "max_rss": 6772, "threads": 32, "min_size": 4, "max_size": 32768, "random_seed": 88 } } } }% $ cat origin/bench-malloc-thread-32.out { "timing_type": "hp_timing", "functions": { "malloc": { "": { "duration": 9.3371e+11, "iterations": 1.08938e+10, "time_per_iteration": 85.7105, "max_rss": 7072, "threads": 32, "min_size": 4, "max_size": 32768, "random_seed": 88 } } } }% --- malloc/malloc.c | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/malloc/malloc.c b/malloc/malloc.c index a0bc733482..c8ecebec2c 100644 --- a/malloc/malloc.c +++ b/malloc/malloc.c @@ -3364,7 +3364,12 @@ tcache_try_malloc (size_t bytes, void **memptr) MAYBE_INIT_TCACHE (); if (tcache_available (tc_idx)) - *memptr = tcache_get (tc_idx); + { + *memptr = tcache_get (tc_idx); + if (__glibc_unlikely(chunksize(mem2chunk(memptr))) != tbytes) + malloc_printerr("calloc(): tcache mem size vs request size"); + return memptr; + } else *memptr = NULL; @@ -3429,10 +3434,16 @@ void * __libc_malloc (size_t bytes) { #if USE_TCACHE - size_t tc_idx = csize2tidx (checked_request2size (bytes)); + size_t tbytes = checked_request2size(bytes); + size_t tc_idx = csize2tidx (tbytes); if (tcache_available (tc_idx)) - return tag_new_usable (tcache_get (tc_idx)); + { + void* memptr = tag_new_usable(tcache_get(tc_idx)); + if (__glibc_unlikely(chunksize(mem2chunk(memptr))) != tbytes) + malloc_printerr("malloc(): tcache mem size vs request size"); + return memptr; + } #endif return __libc_malloc2 (bytes); @@ -4009,11 +4020,6 @@ _int_malloc (mstate av, size_t bytes) while (tcache->counts[tc_idx] < mp_.tcache_count && (tc_victim = *fb) != NULL) { - if (__glibc_unlikely (misaligned_chunk (tc_victim))) - malloc_printerr ("malloc(): unaligned fastbin chunk detected 3"); - size_t victim_tc_idx = csize2tidx (chunksize (tc_victim)); - if (__glibc_unlikely (tc_idx != victim_tc_idx)) - malloc_printerr ("malloc(): chunk size mismatch in fastbin"); if (SINGLE_THREAD_P) *fb = REVEAL_PTR (tc_victim->fd); else