Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp3009408imu; Fri, 23 Nov 2018 19:16:17 -0800 (PST) X-Google-Smtp-Source: AFSGD/W8jr8S8UUfrp+zpbBGQM4KGJvjYhZVV1M1W9vgkyImqpsr0jBX2Re9MmhySzUbe3VxcICN X-Received: by 2002:a63:1904:: with SMTP id z4mr16023948pgl.135.1543029377048; Fri, 23 Nov 2018 19:16:17 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1543029377; cv=none; d=google.com; s=arc-20160816; b=AyIvmUW08AVtWKLUq04wr0dqaEo/z63mAyDZI2sQZfwJv5WyCStoAM5jZcHr1ykP6d mB6CEEBHRfPrwW1QeNic9+r81SyXzCPSAH/uCULq8Zg08XibcGiTge+smSkeBnP734Q7 RIPEI4m4amEIODVDqWULyQGi2NQxjzbNlk+M0HaOtwiVF/1wWlS+PFzRPgLUfSSJgV2g ZY8fYeUOEZ5szegS4mPEjIGcym+GYV1V3qKFcInDn9uf5i7mLuudUwfMKs5a8N0YBUWC ZAcB/LmjocIz2xJ0q1Cv8XZapLG0bdK4TOVfsU0be8lGoKqfpGjZN8c9Cr9zBD6T/qeE w1mQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:thread-index:thread-topic :content-transfer-encoding:mime-version:subject:references :in-reply-to:message-id:cc:to:from:date:dkim-signature:dkim-filter; bh=zEUD2cv3m9QoBxqFrRcnLu6dRfjO+JYa50ASikZlxrY=; b=wjILeczoMknwVHMfy4XUPwfCcuotAgrKy2u6EQK96SB7GzO+5/4ZDMm3VrRQP5hSAQ 5i/xGL4vBc0oVOrYfehmzxfLLiEH6WjqEU5SWZ8/Q2OQ4HBetG6bPrIGB83VVwCC+Iej PiaxuJ90Spy12l7Pa7QJHmGk3ZKOsjPVKaoh7PqKGUQczImSolsdZpjRF6WfF79Urhif RTUV0fPXRrrVlRQ6pEVP579yKOcMnt202fvrPM30qj4p5ppdn46eXcwp/cVEc+sDK95p SjwZJo20AtOuqPBnyD1bZQDWZLlWU/t/jfHXXMufwudGAopuiqAXlOhcb0BBELkIc/t5 n10A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@efficios.com header.s=default header.b=jHTiVEEb; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=efficios.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id g15si11190670pgl.141.2018.11.23.19.16.03; Fri, 23 Nov 2018 19:16:16 -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; dkim=pass header.i=@efficios.com header.s=default header.b=jHTiVEEb; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=efficios.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2391788AbeKWD1y (ORCPT + 99 others); Thu, 22 Nov 2018 22:27:54 -0500 Received: from mail.efficios.com ([167.114.142.138]:35804 "EHLO mail.efficios.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1732442AbeKWD1x (ORCPT ); Thu, 22 Nov 2018 22:27:53 -0500 Received: from localhost (ip6-localhost [IPv6:::1]) by mail.efficios.com (Postfix) with ESMTP id D95CE7B342; Thu, 22 Nov 2018 11:47:42 -0500 (EST) Received: from mail.efficios.com ([IPv6:::1]) by localhost (mail02.efficios.com [IPv6:::1]) (amavisd-new, port 10032) with ESMTP id WWjduBERekzg; Thu, 22 Nov 2018 11:47:42 -0500 (EST) Received: from localhost (ip6-localhost [IPv6:::1]) by mail.efficios.com (Postfix) with ESMTP id 6CEEE7B33D; Thu, 22 Nov 2018 11:47:42 -0500 (EST) DKIM-Filter: OpenDKIM Filter v2.10.3 mail.efficios.com 6CEEE7B33D DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=efficios.com; s=default; t=1542905262; bh=zEUD2cv3m9QoBxqFrRcnLu6dRfjO+JYa50ASikZlxrY=; h=Date:From:To:Message-ID:MIME-Version; b=jHTiVEEbmNQBBlZPisA4639XmLuEEFGzX2ziBILLaPdDpqLwIUR6fhtMABv6CiNeV MHy2Of2dJqc2KvJ9JdABYjhyVhrXmAJLtRt1mGa1cRGrRdR07s7qf7jmk55GLPuZis EUjPVdLUJJzP1hsQSN1UjU3UDkU96WNWa5sZJT8/89rpjoZDnG2X7m5TDFwRPX17Vh QD7X9WcSrKEtZZqlLvcdxZA7BrbDEpNM0Be1X1ViJlGcpVAPsq/vKpMElNHaOajB5l yYtSuqajiJYjRTpwfPM8RUeWl0UnV0aKTqEBUKgLxdtx0w3nwM/18Cnh3Nd30R8gq8 8KjPAVqQzrDrA== X-Virus-Scanned: amavisd-new at efficios.com Received: from mail.efficios.com ([IPv6:::1]) by localhost (mail02.efficios.com [IPv6:::1]) (amavisd-new, port 10026) with ESMTP id 2zCcVB0wB0Oc; Thu, 22 Nov 2018 11:47:42 -0500 (EST) Received: from mail02.efficios.com (mail02.efficios.com [167.114.142.138]) by mail.efficios.com (Postfix) with ESMTP id 4EB217B331; Thu, 22 Nov 2018 11:47:42 -0500 (EST) Date: Thu, 22 Nov 2018 11:47:42 -0500 (EST) From: Mathieu Desnoyers To: Florian Weimer Cc: Rich Felker , 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 Message-ID: <1045257294.10291.1542905262086.JavaMail.zimbra@efficios.com> In-Reply-To: <87wop5xeit.fsf@oldenburg.str.redhat.com> 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> Subject: Re: [RFC PATCH v4 1/5] glibc: Perform rseq(2) registration at nptl init and thread creation MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit X-Originating-IP: [167.114.142.138] X-Mailer: Zimbra 8.8.10_GA_3047 (ZimbraWebClient - FF52 (Linux)/8.8.10_GA_3041) Thread-Topic: glibc: Perform rseq(2) registration at nptl init and thread creation Thread-Index: daNrSODWQpbUGqf0EEqpOD2sZf2NSQ== Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org ----- 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 ? > Then you'll only > need a mechanism to find the address of the actually active rseq area > (which you probably have to store in a TLS variable for performance > reasons). And that part you need whether you have reference counter or > not. I'm not sure I follow your thoughts here. Currently, the __rseq_abi TLS symbol identifies a structure registered to the kernel. The "currently active" rseq critical section is identified by the field "rseq_cs" within the __rseq_abi structure. So here when you say "actually active rseq area", do you mean the currently registered struct rseq (__rseq_abi) or the currently running rseq critical section ? (pointed to by __rseq_abi.rseq_cs) 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. So using the IE model for glibc makes sense, because nobody dlopen glibc AFAIK. But it's not so simple for early adopter libraries which can be dlopen'd. > >> 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 ? Thanks, Mathieu -- Mathieu Desnoyers EfficiOS Inc. http://www.efficios.com