From patchwork Wed Apr 2 10:41:14 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: 109669 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 539A0384A879 for ; Wed, 2 Apr 2025 10:42:01 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 539A0384A879 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=BGeXElxm X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-pl1-x62f.google.com (mail-pl1-x62f.google.com [IPv6:2607:f8b0:4864:20::62f]) by sourceware.org (Postfix) with ESMTPS id 4DBD2384A861 for ; Wed, 2 Apr 2025 10:41:19 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 4DBD2384A861 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 4DBD2384A861 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::62f ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1743590479; cv=none; b=Q6+N/C47sKO3OMHcjt2HPHGoEcOdKHgiLnHPXyTI8TiCH+Hf6s4WM2leiXbWEe1uHK5bHh6fx4AeuLfFPTmqfVFjh/ca32nkIvLOfdmBMz5h31YlttXyEs5tWbhI4cAUo1XOUEuy4Jn3xjkFQb/EduA2ZtUvJY0TImVh3uTViGA= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1743590479; c=relaxed/simple; bh=uvew0G3R42fThn+fel6uPqj5LCjphx1cB1VFlEN4Zac=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=ZTvYwC5VQBYqrnTA4L7gtVlKAnXwjP01jQvaKuccSLexv0cY4Tdvua6h8eLK8L1sYMtQGsHKvDa6HCEEFw04i2xuofIvzDrNr2dXP/3fL2KG/O0bf6dxxmwO6TlSuIlDOSnGW6aXOKlKvfMWwSS10rZIgRPOuSAIGmFumRhMIqk= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 4DBD2384A861 Received: by mail-pl1-x62f.google.com with SMTP id d9443c01a7336-22622ddcc35so57283715ad.2 for ; Wed, 02 Apr 2025 03:41:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1743590478; x=1744195278; 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=FFV3R7p4vTOYPyCpAXcpGIcuJBVgDPA1njKqTqVyKxA=; b=BGeXElxmsh//N5Hagf9i/PY7XiNAtP/KFtKqUaD5Pjnt3dcrEWaM5BidUIQUn3SgbB NkBnyKBXB2W01bQB2zL0auQiGJcdGhqHFA9VjH98HaN/HE8RgwhVb1MM+pSEk7UtrHbs xP99lkBnxja1i4jt/6pqiA6T5ce88boywF2JadXzXKaw+J+Vn2aJ8cJIdwtIRg7wchXp svHstw/KzEvZPNIjHsixVpFxoihWsbzm2Y4C1eBeUMmjeeftxAEU9U1tYn0hcbGkktud n/7N4MYdh0uRj4cb0KbpUBUE2SZ4LXXnNSHmMW63/wAn563cvn8Z1O6gRsVG22ASec0I 0oEA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1743590478; x=1744195278; 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=FFV3R7p4vTOYPyCpAXcpGIcuJBVgDPA1njKqTqVyKxA=; b=VeXh20F++L+YhJOAT5vD1ufeOthRyqxtQfTaBkADa8t+ty3boKtSKI8BIIDdfYT511 ADaxRzJetiy2jfNRSNk5LVgeNSPn/9uOgP5FKJAPh7B2ItJayCPobxGpTSUBhkACeIsz pMja+MdY9YFX49rcmfrscyhZJ7HvYBfvlUkGzLXIekZNFKCmFyraKTrwuVlqYpFqlnVJ sbmubwgK+lcVs2xfotKzb5QQ7TRPXRVWtDwOEzb7lDbxY0rDARG1UM5i5ul+osi2hSpP q+LkL0VOZCObzJkdVfialzdB+JYJLf44rrsv6BegiHQBdykD+KF5DKF9EOkzBdwdBdkS aYWg== X-Gm-Message-State: AOJu0Yw2FtMuFBX0XNeaNKm1x1PPJDvgbV3wFkjpoO1nR1Emug6F/MP+ zhctSOLUV4iOh+joYfdxRiUkxQ0TAYBTzioiwZVtobNh/MV6XttYiNQwYqFvG+o= X-Gm-Gg: ASbGncvF2QZoOrkF1sLx0CvvSl/Jgm0YySTiTSl/u1NQMNplQJ9nu/+zIzb8w93X1pD xACe+1N9ssP6SN4J8N39t95pLBGjRE3DsfZoi01uJu2oRWAJ5GbtswYsOiT86tviSF+9z0YWi+z ez+OCSTCSRDyM8rRZqW3mfF6xACEBgRAt1WmSXHSY4lrGq6Oe8BdrDBMvbRa56EUjSUL28dYnjI l1pj7eRTnIkMgiGkNUoQCLtamw+/y+rwe/lb8U7VyqWBKX8uModZxnfVul4BwdNEHYGNzgt7G+l 9PDGMJh5fwEx8WKDoBcKLm7VcLW+5uxR3O0Uv1+xF3yPZ1jB4X6in1j0X0JNdErDI3qdcXespRf GfEAd47M= X-Google-Smtp-Source: AGHT+IEroWLE/0R4XzTcz7L+O7LfvIl7aS974Ihi49AdMP1YjWc6/6VP6/54u+s2J6vJ8OpphWq5uw== X-Received: by 2002:a05:6a21:1643:b0:1ee:dded:e5b with SMTP id adf61e73a8af0-200e4c5d854mr2490516637.24.1743590478326; Wed, 02 Apr 2025 03:41:18 -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-7397106ae4asm10597849b3a.110.2025.04.02.03.41.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 02 Apr 2025 03:41:17 -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:41:14 +0800 Message-ID: <20250402104114.2538-1-dudududuMaxVer@gmail.com> X-Mailer: git-send-email 2.49.0 MIME-Version: 1.0 X-Spam-Status: No, score=-11.0 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 plz ignore the earlier two email.my bad. 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. Signed-off-by: dbgbgtf $ 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