From patchwork Thu Jun 17 10:32:17 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Siddhesh Poyarekar X-Patchwork-Id: 43860 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 4D8B2388980B for ; Thu, 17 Jun 2021 10:32:59 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 4D8B2388980B DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1623925979; bh=EyI1J5gzBLY/DTE/ozV1QMgLq4SAjxgXaXzllKrG0aE=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=GvncuyKesLqrS4C6QbG7xdoYmRy2ND3/2mNova9n2UReTLlsO72W/sHL8YJFRG91W BQYKGbxp9VmZ9S6qMATIJaAWKsnoCt/8hSyhichyKTATRoNkyY7++HQKmaMP1xEenJ 0PA5AGZRqu1SQ3iJKTqfsM5jZ0BX6BaYPgQHtMPg= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from hamster.birch.relay.mailchannels.net (hamster.birch.relay.mailchannels.net [23.83.209.80]) by sourceware.org (Postfix) with ESMTPS id 8E2633857C73 for ; Thu, 17 Jun 2021 10:32:36 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 8E2633857C73 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 3E7B112282D for ; Thu, 17 Jun 2021 10:32:35 +0000 (UTC) Received: from pdx1-sub0-mail-a82.g.dreamhost.com (100-96-18-89.trex.outbound.svc.cluster.local [100.96.18.89]) (Authenticated sender: dreamhost) by relay.mailchannels.net (Postfix) with ESMTPA id 7BD54122841 for ; Thu, 17 Jun 2021 10:32:34 +0000 (UTC) X-Sender-Id: dreamhost|x-authsender|siddhesh@gotplt.org Received: from pdx1-sub0-mail-a82.g.dreamhost.com (pop.dreamhost.com [64.90.62.162]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384) by 100.96.18.89 (trex/6.3.1); Thu, 17 Jun 2021 10:32:34 +0000 X-MC-Relay: Neutral X-MailChannels-SenderId: dreamhost|x-authsender|siddhesh@gotplt.org X-MailChannels-Auth-Id: dreamhost X-Chemical-Spill: 44ddce243a1e629c_1623925954716_690070307 X-MC-Loop-Signature: 1623925954715:1384286858 X-MC-Ingress-Time: 1623925954715 Received: from pdx1-sub0-mail-a82.g.dreamhost.com (localhost [127.0.0.1]) by pdx1-sub0-mail-a82.g.dreamhost.com (Postfix) with ESMTP id 3B06789D4A for ; Thu, 17 Jun 2021 03:32:34 -0700 (PDT) Received: from rhbox.intra.reserved-bit.com (unknown [1.186.101.110]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) (Authenticated sender: siddhesh@gotplt.org) by pdx1-sub0-mail-a82.g.dreamhost.com (Postfix) with ESMTPSA id 3032D89388 for ; Thu, 17 Jun 2021 03:32:32 -0700 (PDT) X-DH-BACKEND: pdx1-sub0-mail-a82 To: libc-alpha@sourceware.org Subject: [PATCH] malloc: Ensure that ptmalloc_init runs only once Date: Thu, 17 Jun 2021 16:02:17 +0530 Message-Id: <20210617103217.2633690-1-siddhesh@sourceware.org> X-Mailer: git-send-email 2.31.1 MIME-Version: 1.0 X-Spam-Status: No, score=-3494.6 required=5.0 tests=BAYES_00, GIT_PATCH_0, JMQ_SPF_NEUTRAL, KAM_DMARC_NONE, KAM_DMARC_STATUS, RCVD_IN_BARRACUDACENTRAL, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NEUTRAL, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) 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+patchwork=sourceware.org@sourceware.org Sender: "Libc-alpha" It is possible that multiple threads simultaneously enter ptmalloc_init and succeed the < 0 check. Make the comparison and setting of __malloc_initialized atomic so that only one of them goes through. Additionally, if a thread sees that another thread is running the initialization (i.e. __malloc_initialized == 0) then wait till it is done. --- malloc/arena.c | 12 +++++++++--- malloc/malloc.c | 14 +++++++------- 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/malloc/arena.c b/malloc/arena.c index 7eb110445e..3cd4391f25 100644 --- a/malloc/arena.c +++ b/malloc/arena.c @@ -290,10 +290,16 @@ libc_hidden_proto (_dl_open_hook); static void ptmalloc_init (void) { - if (__malloc_initialized >= 0) - return; + int oldval = catomic_compare_and_exchange_val_acq (&__malloc_initialized, + -1, 0); - __malloc_initialized = 0; + if (oldval == 1) + return; + else if (oldval == 0) + { + while (__malloc_initialized != 1); + return; + } #ifdef USE_MTAG if ((TUNABLE_GET_FULL (glibc, mem, tagging, int32_t, NULL) & 1) != 0) diff --git a/malloc/malloc.c b/malloc/malloc.c index 0e2e1747e0..cc3d1f41e8 100644 --- a/malloc/malloc.c +++ b/malloc/malloc.c @@ -3562,7 +3562,7 @@ libc_hidden_def (__libc_memalign) void * __libc_valloc (size_t bytes) { - if (__malloc_initialized < 0) + if (__malloc_initialized <= 0) ptmalloc_init (); void *address = RETURN_ADDRESS (0); @@ -3573,7 +3573,7 @@ __libc_valloc (size_t bytes) void * __libc_pvalloc (size_t bytes) { - if (__malloc_initialized < 0) + if (__malloc_initialized <= 0) ptmalloc_init (); void *address = RETURN_ADDRESS (0); @@ -5077,7 +5077,7 @@ __malloc_trim (size_t s) { int result = 0; - if (__malloc_initialized < 0) + if (__malloc_initialized <= 0) ptmalloc_init (); mstate ar_ptr = &main_arena; @@ -5212,7 +5212,7 @@ __libc_mallinfo2 (void) struct mallinfo2 m; mstate ar_ptr; - if (__malloc_initialized < 0) + if (__malloc_initialized <= 0) ptmalloc_init (); memset (&m, 0, sizeof (m)); @@ -5263,7 +5263,7 @@ __malloc_stats (void) mstate ar_ptr; unsigned int in_use_b = mp_.mmapped_mem, system_b = in_use_b; - if (__malloc_initialized < 0) + if (__malloc_initialized <= 0) ptmalloc_init (); _IO_flockfile (stderr); int old_flags2 = stderr->_flags2; @@ -5432,7 +5432,7 @@ __libc_mallopt (int param_number, int value) mstate av = &main_arena; int res = 1; - if (__malloc_initialized < 0) + if (__malloc_initialized <= 0) ptmalloc_init (); __libc_lock_lock (av->mutex); @@ -5691,7 +5691,7 @@ __malloc_info (int options, FILE *fp) - if (__malloc_initialized < 0) + if (__malloc_initialized <= 0) ptmalloc_init (); fputs ("\n", fp);