Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp3160490imu; Fri, 23 Nov 2018 23:01:24 -0800 (PST) X-Google-Smtp-Source: AFSGD/VBrcASe4+FFKSJSo4mgMzwOzQp9/q0g1OfnVgeMaz2/VwDGfLyOCQVgxEVgzUL628Mo7yt X-Received: by 2002:a17:902:1e9:: with SMTP id b96mr19004901plb.150.1543042884756; Fri, 23 Nov 2018 23:01:24 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1543042884; cv=none; d=google.com; s=arc-20160816; b=zcizY6FGkrWhepU/pHnwCz9bjQNDOnzB4KyyRLCxU2f8iWU3+e6ekToIil5AVNQPlq 5dpKlXHZszQCVoP7dGSKBASLa019jlFVXePEEreTjTDumybFKIbheDluox/KF8jMwLYa 2r7/nyF1JzLTrx+TExvcNCqXGBV6fl4U732WfNBjOHFbHyC2PZNevg4MNKv5zcxS2KXJ wUoCwBtXuPQteq7ZqIG5HDhxSp1urmt/ZXf8Rw1aNaoWu7qLoaZRD1vHcAWyjZEWv2TY pUEnNuMjgplXLaF05IkiwhWKGZ7oz57es1KubOAxnfhMpQ+FpfRbQOLebBudi/g18Bxr coTQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date; bh=pOQLsarv2+tLNPLRiRI62BDuS7lB1HKss1az9ptXtLY=; b=EFGTS3kDZcp5gsPUuOyL3czhdOgofUZyEZypGL1xiBN/FQ1Os+3q0ZZ+xiyxBUYGm9 HUiZzHnEWs+hUUAc0V9XNvs0he0tzpZmPq3OgNEeopo01rR/KhC7Gv1yYqMFg35umDww PNqGEaN66b/9ohIg0K3MKMkSk9oKdAjqwKuMoiMa4OjMj0m+NkwjnzXIFl87kTve2tpP sMtdGwTaH+uRIjMe8ydats/o24UM8t+FORJWYFsz4vGX7dZ+Y+asoKA+uY/9ZKPwkXN9 RvSXOc/60t7QoB/RT5t2ihqUA+Br6DjiBymYxpADt+3UIzdjvb1HSY7QDkpPw/Z/e06G jJ1Q== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id e4si29974656plk.260.2018.11.23.23.01.10; Fri, 23 Nov 2018 23:01:24 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2406240AbeKWDvP (ORCPT + 99 others); Thu, 22 Nov 2018 22:51:15 -0500 Received: from 216-12-86-13.cv.mvl.ntelos.net ([216.12.86.13]:58442 "EHLO brightrain.aerifal.cx" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1731310AbeKWDvP (ORCPT ); Thu, 22 Nov 2018 22:51:15 -0500 Received: from dalias by brightrain.aerifal.cx with local (Exim 3.15 #2) id 1gPsUU-0005Hs-00; Thu, 22 Nov 2018 17:10:10 +0000 Date: Thu, 22 Nov 2018 12:10:10 -0500 From: Rich Felker To: Florian Weimer Cc: Mathieu Desnoyers , carlos , Joseph Myers , Szabolcs Nagy , libc-alpha , Thomas Gleixner , Ben Maurer , Peter Zijlstra , "Paul E. McKenney" , Boqun Feng , Will Deacon , Dave Watson , Paul Turner , linux-kernel , linux-api Subject: Re: [RFC PATCH v4 1/5] glibc: Perform rseq(2) registration at nptl init and thread creation Message-ID: <20181122171010.GH23599@brightrain.aerifal.cx> References: <20181121183936.8176-1-mathieu.desnoyers@efficios.com> <20181122143603.GD23599@brightrain.aerifal.cx> <782067422.9852.1542899056778.JavaMail.zimbra@efficios.com> <20181122151444.GE23599@brightrain.aerifal.cx> <686626451.10113.1542901620250.JavaMail.zimbra@efficios.com> <87wop5xeit.fsf@oldenburg.str.redhat.com> <1045257294.10291.1542905262086.JavaMail.zimbra@efficios.com> <87k1l5xd33.fsf@oldenburg.str.redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <87k1l5xd33.fsf@oldenburg.str.redhat.com> User-Agent: Mutt/1.5.21 (2010-09-15) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, Nov 22, 2018 at 05:59:44PM +0100, Florian Weimer wrote: > * Mathieu Desnoyers: > > > ----- On Nov 22, 2018, at 11:28 AM, Florian Weimer fweimer@redhat.com wrote: > > > >> * Mathieu Desnoyers: > >> > >>> Here is one scenario: we have 2 early adopter libraries using rseq which > >>> are deployed in an environment with an older glibc (which does not > >>> support rseq). > >>> > >>> Of course, none of those libraries can be dlclose'd unless they somehow > >>> track all registered threads. > >> > >> Well, you can always make them NODELETE so that dlclose is not an issue. > >> If the library is small enough, that shouldn't be a problem. > > > > That's indeed what I do with lttng-ust, mainly due to use of pthread_key. > > > >> > >>> But let's focus on how exactly those libraries can handle lazily > >>> registering rseq. They can use pthread_key, and pthread_setspecific on > >>> first use by the thread to setup a destructor function to be invoked > >>> at thread exit. But each early adopter library is unaware of the > >>> other, so if we just use a "is_initialized" flag, the first destructor > >>> to run will unregister rseq while the second library may still be > >>> using it. > >> > >> I don't think you need unregistering if the memory is initial-exec TLS > >> memory. Initial-exec TLS memory is tied directly to the TCB and cannot > >> be freed while the thread is running, so it should be safe to put the > >> rseq area there even if glibc knows nothing about it. > > > > Is it true for user-supplied stacks as well ? > > I'm not entirely sure because the glibc terminology is confusing, but I > think it places intial-exec TLS into the static TLS area (so that it has > a fixed offset from the TCB). The static TLS area is placed on the > user-supplied stack. This is an implementation detail that should not leak to applications, and I believe it's still considered a bug, in that, with large static TLS, it could overflow or leave unusably little space left on an otherwise-plenty-large application-provided stack. > > One issue here is that early adopter libraries cannot always use > > the IE model. I tried using it for other TLS variables in lttng-ust, and > > it ended up hanging our CI tests when tracing a sample application with > > lttng-ust under a Java virtual machine: being dlopen'd in a process that > > possibly already exhausts the number of available backup TLS IE entries > > seems to have odd effects. This is why I'm worried about using the IE model > > within lttng-ust. > > You can work around this by preloading the library. I'm not sure if > this is a compelling reason not to use initial-exec TLS memory. Use of IE model from a .so file (except possibly libc.so or something else that inherently needs to be present at program startup for other reasons) should be a considered a bug and unsupported usage. Encouraging libraries to perpetuate this behavior is going backwards on progress that's being made to end it. > >>> The same problem arises if we have an application early adopter which > >>> explicitly deal with rseq, with a library early adopter. The issue is > >>> similar, except that the application will explicitly want to unregister > >>> rseq before exiting the thread, which leaves a race window where rseq > >>> is unregistered, but the library may still need to use it. > >>> > >>> The reference counter solves this: only the last rseq user for a thread > >>> performs unregistration. > >> > >> If you do explicit unregistration, you will run into issues related to > >> destructor ordering. You should really find a way to avoid that. > > > > The per-thread reference counter is a way to avoid issues that arise from > > lack of destructor ordering. Is it an acceptable approach for you, or > > you have something else in mind ? > > Only for the involved libraries. It will not help if other TLS > destructors run and use these libraries. Presumably they should have registered their need for rseq too, thereby incrementing the reference count. I'm not sure this is a good idea, but I think I understand it now. Rich