From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id F2F5F385354D for ; Tue, 6 Sep 2022 17:42:43 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org F2F5F385354D Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1662486163; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=nu6fylGUIJAnSRj/no81N2446ESwrqd9jg84wH/NOsw=; b=Mq3X1HIXNAWL9VdaX+VARAS2YOkrN2ktubonpY1eDMkrq9ksegsyz+BAjNkX8n865kN6f8 Zooh7iHsea8/DmO+Tr3Pe8PkTSFfsUu3mwEgoROkDKgBoBA4fbkRHmnB0txh4LpB27xt+l U86pVOgykbRzwXIj67ffOSe7kJ78Tlo= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-203-U8o0-wN7Pf2TxxObPE8P7Q-1; Tue, 06 Sep 2022 13:42:37 -0400 X-MC-Unique: U8o0-wN7Pf2TxxObPE8P7Q-1 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 13EBC85A589; Tue, 6 Sep 2022 17:42:37 +0000 (UTC) Received: from oldenburg.str.redhat.com (unknown [10.39.192.109]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 6B9159458A; Tue, 6 Sep 2022 17:42:36 +0000 (UTC) From: Florian Weimer To: Wilco Dijkstra Cc: Wilco Dijkstra via Libc-alpha Subject: Re: [PATCH] Use C11 atomics instead of atomic_bit_set/bit_test_set References: <87mtbc66t4.fsf@oldenburg.str.redhat.com> Date: Tue, 06 Sep 2022 19:42:34 +0200 In-Reply-To: (Wilco Dijkstra's message of "Tue, 6 Sep 2022 16:52:04 +0000") Message-ID: <87o7vs4cxh.fsf@oldenburg.str.redhat.com> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux) MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.11.54.5 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-11.0 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_LOW,SPF_HELO_NONE,SPF_NONE,TXREP,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: * Wilco Dijkstra: > Hi Florian, > >> Why is acquire MO required here?=C2=A0 I don't see any synchronizing sto= re. >> Isn't this mostly a compiler barrier for use-after-free detection? > > You're right, it looks like the only reason for atomic is to ensure memor= y > is only freed once. A few cancelhandling accesses use acquire, and there > are various relaxed loads and even non-atomic loads of it, but not a > single release store, so there is no use for acquire MO here. > > Cheers, > Wilco > > > v2: Use relaxed atomics since there is no MO dependence > > Replace the 3 uses of atomic_bit_set and atomic_bit_test_set with > atomic_fetch_or_relaxed. > > --- > > diff --git a/malloc/malloc.c b/malloc/malloc.c > index 29fa71b3b2a3d0a671149eaf619e4d518c56aef5..ecec901b14f602e3c93da1a84= 7f043ffee41a1f4 100644 > --- a/malloc/malloc.c > +++ b/malloc/malloc.c > @@ -2460,11 +2460,11 @@ sysmalloc_mmap (INTERNAL_SIZE_T nb, size_t pagesi= ze, int extra_flags, mstate av) > } > =20 > /* update statistics */ > - int new =3D atomic_exchange_and_add (&mp_.n_mmaps, 1) + 1; > + int new =3D atomic_fetch_add_relaxed (&mp_.n_mmaps, 1) + 1; > atomic_max (&mp_.max_n_mmaps, new); > =20 > unsigned long sum; > - sum =3D atomic_exchange_and_add (&mp_.mmapped_mem, size) + size; > + sum =3D atomic_fetch_add_relaxed (&mp_.mmapped_mem, size) + size; > atomic_max (&mp_.max_mmapped_mem, sum); > =20 > check_chunk (av, p); > @@ -3084,7 +3084,7 @@ mremap_chunk (mchunkptr p, size_t new_size) > set_head (p, (new_size - offset) | IS_MMAPPED); > =20 > INTERNAL_SIZE_T new; > - new =3D atomic_exchange_and_add (&mp_.mmapped_mem, new_size - size - o= ffset) > + new =3D atomic_fetch_add_relaxed (&mp_.mmapped_mem, new_size - size - = offset) > + new_size - size - offset; > atomic_max (&mp_.max_mmapped_mem, new); > return p; Wrong patch, I think? Thanks, Florian