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 3E9453837599 for ; Fri, 16 Dec 2022 17:25:50 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 3E9453837599 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=1671211549; 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=MrTc3KnMYlXZbNRYTII9RIsfAJGsx/hY4memAKE+S/w=; b=dg6FkHER8pKPY28OcikwhTi2nny5J86550V95uYPd2nvALhjLs71jVBjz3y/9ergyMbZcB +85uF+V13PugTCe4CEPoId189LcE9sE2uXOxd9l5YDVuXHxoaHQZCjgld5D/u0V6XiZK/N EPhykYJhsNviNUBjqq3rXhvHSENwbgM= Received: from mail-qt1-f197.google.com (mail-qt1-f197.google.com [209.85.160.197]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-327-7Rn1tzLxNryKZPxlZTyldA-1; Fri, 16 Dec 2022 12:25:48 -0500 X-MC-Unique: 7Rn1tzLxNryKZPxlZTyldA-1 Received: by mail-qt1-f197.google.com with SMTP id l11-20020ac8148b000000b003a81fbc3b3bso1910634qtj.12 for ; Fri, 16 Dec 2022 09:25:48 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=MrTc3KnMYlXZbNRYTII9RIsfAJGsx/hY4memAKE+S/w=; b=mcmnWBdsNzxGDJKGeSc3xzudibV3B7thm/XRrnejVmB3Qpepy8Nn1r4yV+FE0snCs/ 3EnOlAbWpj9wRPi1z5ToidmhuBU4/cCC5ViX3Ll/IJeBiRouxi5oa//3IcbY3U5RdPid 8axwMZnv6ZTRROH+I3fIlmodp8dhceRHlu+KAZzWWP3SKYNRbPH4fipChVNdK3OFXu8G NcNmHwybSD54tQKHcMLoc/DT+xyu9iLz/V4n7nmlrob3VsvgzsxCjCa6OlXtN3Qx8OMP kXpBTnw1tIhmIQuSs5yFUZBY5yWS7RrmQ9vGeqVQ3/qDlDDw/IhXO00ls3m10Yp4lV8Q R/zQ== X-Gm-Message-State: ANoB5pmvvrES7GRBmY3sSNteRwFTqo7PcWmcmvqB7QXbRmgqTRMpr4hz bGP3xg96UkBt3cnKHkfh1Nwt3QGIOr4rZ0KDNxgrzJzNacttkTMlco11TOoapVeAm8TfbjLIc1S HhkbR0fgKUIJM3aucKg== X-Received: by 2002:ac8:74d:0:b0:3a8:11f3:b81c with SMTP id k13-20020ac8074d000000b003a811f3b81cmr32843747qth.53.1671211548272; Fri, 16 Dec 2022 09:25:48 -0800 (PST) X-Google-Smtp-Source: AA0mqf7KjthnBJ8AJeh0Q9CSefz/z5K8izW4j8u8KvDT/mq5QLSZWbKlVFZgL2HQCdTh8XZ1MCu5Ew== X-Received: by 2002:ac8:74d:0:b0:3a8:11f3:b81c with SMTP id k13-20020ac8074d000000b003a811f3b81cmr32843722qth.53.1671211547951; Fri, 16 Dec 2022 09:25:47 -0800 (PST) Received: from [192.168.1.108] (130-44-159-43.s15913.c3-0.arl-cbr1.sbo-arl.ma.cable.rcncustomer.com. [130.44.159.43]) by smtp.gmail.com with ESMTPSA id fg18-20020a05622a581200b003a7e2aea23esm1580513qtb.86.2022.12.16.09.25.46 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 16 Dec 2022 09:25:47 -0800 (PST) Message-ID: <2f2a22a0-18ba-c4e1-e464-fae17fb2223f@redhat.com> Date: Fri, 16 Dec 2022 12:25:45 -0500 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.5.1 Subject: Re: [PATCH] initialize fde objects lazily To: Thomas Neumann , "gcc-patches@gcc.gnu.org" Cc: "H.J. Lu" , Jakub Jelinek , Tamar Christina , Jonathan Wakely , Florian Weimer References: <2a4776b9-9271-bb3c-a626-d5ec22dae6f3@in.tum.de> <91045a34-a534-4436-bb06-cac32d797a36@in.tum.de> <87sfibqu1s.fsf@oldenburg.str.redhat.com> <7d18f085-ae46-138d-4f04-df5857b7b014@in.tum.de> From: Jason Merrill In-Reply-To: <7d18f085-ae46-138d-4f04-df5857b7b014@in.tum.de> X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Language: en-US Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-10.9 required=5.0 tests=BAYES_00,BODY_8BITS,DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,NICE_REPLY_A,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_NONE,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 List-Id: On 12/9/22 12:34, Thomas Neumann wrote: > When registering an unwind frame with __register_frame_info_bases > we currently initialize that fde object eagerly. This has the > advantage that it is immutable afterwards and we can safely > access it from multiple threads, but it has the disadvantage > that we pay the initialization cost even if the application > never throws an exception. > > This commit changes the logic to initialize the objects lazily. > The objects themselves are inserted into the b-tree when > registering the frame, but the sorted fde_vector is > not constructed yet. Only on the first time that an > exception tries to pass through the registered code the > object is initialized. We notice that with a double checking, > first doing a relaxed load of the sorted bit and then re-checking > under a mutex when the object was not initialized yet. > > Note that the check must implicitly be safe concering a concurrent > frame deregistration, as trying the deregister a frame that is > on the unwinding path of a concurrent exception is inherently racy. OK, thanks. > libgcc/ChangeLog: >         * unwind-dw2-fde.c: Initialize fde object lazily when >         the first exception tries to pass through. > --- >  libgcc/unwind-dw2-fde.c | 52 ++++++++++++++++++++++++++++++++--------- >  1 file changed, 41 insertions(+), 11 deletions(-) > > diff --git a/libgcc/unwind-dw2-fde.c b/libgcc/unwind-dw2-fde.c > index 3c0cc654ec0..6f69c20ff4b 100644 > --- a/libgcc/unwind-dw2-fde.c > +++ b/libgcc/unwind-dw2-fde.c > @@ -63,8 +63,6 @@ release_registered_frames (void) > >  static void >  get_pc_range (const struct object *ob, uintptr_type *range); > -static void > -init_object (struct object *ob); > >  #else >  /* Without fast path frame deregistration must always succeed.  */ > @@ -76,6 +74,7 @@ static const int in_shutdown = 0; >     by decreasing value of pc_begin.  */ >  static struct object *unseen_objects; >  static struct object *seen_objects; > +#endif > >  #ifdef __GTHREAD_MUTEX_INIT >  static __gthread_mutex_t object_mutex = __GTHREAD_MUTEX_INIT; > @@ -103,7 +102,6 @@ init_object_mutex_once (void) >  static __gthread_mutex_t object_mutex; >  #endif >  #endif > -#endif > >  /* Called from crtbegin.o to register the unwind info for an object.  */ > > @@ -126,10 +124,7 @@ __register_frame_info_bases (const void *begin, > struct object *ob, >  #endif > >  #ifdef ATOMIC_FDE_FAST_PATH > -  // Initialize eagerly to avoid locking later > -  init_object (ob); > - > -  // And register the frame > +  // Register the frame in the b-tree >    uintptr_type range[2]; >    get_pc_range (ob, range); >    btree_insert (®istered_frames, range[0], range[1] - range[0], ob); > @@ -180,10 +175,7 @@ __register_frame_info_table_bases (void *begin, > struct object *ob, >    ob->s.b.encoding = DW_EH_PE_omit; > >  #ifdef ATOMIC_FDE_FAST_PATH > -  // Initialize eagerly to avoid locking later > -  init_object (ob); > - > -  // And register the frame > +  // Register the frame in the b-tree >    uintptr_type range[2]; >    get_pc_range (ob, range); >    btree_insert (®istered_frames, range[0], range[1] - range[0], ob); > @@ -892,7 +884,15 @@ init_object (struct object* ob) >    accu.linear->orig_data = ob->u.single; >    ob->u.sort = accu.linear; > > +#ifdef ATOMIC_FDE_FAST_PATH > +  // We must update the sorted bit with an atomic operation > +  struct object tmp; > +  tmp.s.b = ob->s.b; > +  tmp.s.b.sorted = 1; > +  __atomic_store (&(ob->s.b), &(tmp.s.b), __ATOMIC_SEQ_CST); > +#else >    ob->s.b.sorted = 1; > +#endif >  } > >  #ifdef ATOMIC_FDE_FAST_PATH > @@ -1130,6 +1130,21 @@ search_object (struct object* ob, void *pc) >      } >  } > > +#ifdef ATOMIC_FDE_FAST_PATH > + > +// Check if the object was already initialized > +static inline bool > +is_object_initialized (struct object *ob) > +{ > +  // We have to use relaxed atomics for the read, which > +  // is a bit involved as we read from a bitfield > +  struct object tmp; > +  __atomic_load (&(ob->s.b), &(tmp.s.b), __ATOMIC_RELAXED); > +  return tmp.s.b.sorted; > +} > + > +#endif > + >  const fde * >  _Unwind_Find_FDE (void *pc, struct dwarf_eh_bases *bases) >  { > @@ -1141,6 +1156,21 @@ _Unwind_Find_FDE (void *pc, struct dwarf_eh_bases > *bases) >    if (!ob) >      return NULL; > > +  // Initialize the object lazily > +  if (!is_object_initialized (ob)) > +    { > +      // Check again under mutex > +      init_object_mutex_once (); > +      __gthread_mutex_lock (&object_mutex); > + > +      if (!ob->s.b.sorted) > +    { > +      init_object (ob); > +    } > + > +      __gthread_mutex_unlock (&object_mutex); > +    } > + >    f = search_object (ob, pc); >  #else >