Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp3238488imu; Sat, 24 Nov 2018 00:49:10 -0800 (PST) X-Google-Smtp-Source: AJdET5dcWCl3JhcO/eZLkXU5Sbv5vdl9veRF1ZXjm85v7/Tp+31FPe40PrzHKdZA9pRxbf5EQgTo X-Received: by 2002:a62:75d1:: with SMTP id q200mr19396484pfc.254.1543049350847; Sat, 24 Nov 2018 00:49:10 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1543049350; cv=none; d=google.com; s=arc-20160816; b=D9czyB/je7K71Zt1EkywpKCgO3nrNLXHrBeJ7T/wui7qAW7jZrIxDJyHIbkUlXgZ+w H47ZLqk5Wse983D9cAZa2E2DF4AG3w8NsZuIy35GyWQvX9/NJ2hzf2QLL7BkFbpjmTBY e3QvM8bTOCbfREs6NvD6oiIKG7/Bmouo+n7drro5EYSrw5jm787HxNhpiRt0ig7G6K0e vSm5Rz/J+3tau/or/0VWcTDU+IdU09B/TCgaDVlFJroUIsL1GIBT7jyqjyglZqls/wY+ zPbABQ28nYJoKalXFoj3+pU7axnRPO9mBK6FNG9YTIRHS+f3wZ7F35yCi/CoDHv5kJIK IXqw== 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=6gPtAO/NBpqZGQ6L1Dmmei18O64oER0p2NNQefP3apM=; b=BvnHN2KmGW6V1Qw77K+l7G20YvRB4Ux7ITSKEd5FDPuqpYpvjgWisHOeXEm3+yOa4b uYIlv22T3Vk0b2iMK43cNN9iAKzpeF1TbY+BeJxHzPvSgAVNxMY28ieJcFwtPc5rrfUJ iZ/51ieYdKub/rK9bf6P/IrYDdze1Q9ByGBYOsxE0vmddBgx8A/me2NvefMT16s4pz5b +3d3OALxBQxoa7qwSqky/pGUpnu1/r717FqZVTff/o5VzEV7KlBc9KZbgN1R6d0j5dK5 ViveBoFX5z+jxzNOWkV9U8AkncbbJ+1QxppWyAXL4mFWlWOr5Doa2jieq7fYDG+YihcG /Eig== 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 u4si5373242pls.34.2018.11.24.00.48.56; Sat, 24 Nov 2018 00:49:10 -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 S2437178AbeKXFWN (ORCPT + 99 others); Sat, 24 Nov 2018 00:22:13 -0500 Received: from 216-12-86-13.cv.mvl.ntelos.net ([216.12.86.13]:58562 "EHLO brightrain.aerifal.cx" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730291AbeKXFWN (ORCPT ); Sat, 24 Nov 2018 00:22:13 -0500 Received: from dalias by brightrain.aerifal.cx with local (Exim 3.15 #2) id 1gQGJ4-00013C-00; Fri, 23 Nov 2018 18:35:58 +0000 Date: Fri, 23 Nov 2018 13:35:58 -0500 From: Rich Felker To: Mathieu Desnoyers Cc: Florian Weimer , 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: <20181123183558.GM23599@brightrain.aerifal.cx> References: <20181121183936.8176-1-mathieu.desnoyers@efficios.com> <1045257294.10291.1542905262086.JavaMail.zimbra@efficios.com> <87k1l5xd33.fsf@oldenburg.str.redhat.com> <20181122171010.GH23599@brightrain.aerifal.cx> <871s7cvt1l.fsf@oldenburg.str.redhat.com> <20181123142843.GJ23599@brightrain.aerifal.cx> <1150466925.11664.1542992720871.JavaMail.zimbra@efficios.com> <20181123173019.GK23599@brightrain.aerifal.cx> <865273158.11687.1542995541389.JavaMail.zimbra@efficios.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <865273158.11687.1542995541389.JavaMail.zimbra@efficios.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 Fri, Nov 23, 2018 at 12:52:21PM -0500, Mathieu Desnoyers wrote: > ----- On Nov 23, 2018, at 12:30 PM, Rich Felker dalias@libc.org wrote: > > > On Fri, Nov 23, 2018 at 12:05:20PM -0500, Mathieu Desnoyers wrote: > >> ----- On Nov 23, 2018, at 9:28 AM, Rich Felker dalias@libc.org wrote: > >> [...] > >> > > >> > Absolutely. As long as it's in libc, implicit destruction will happen. > >> > Actually I think the glibc code shound unconditionally unregister the > >> > rseq address at exit (after blocking signals, so no application code > >> > can run) in case a third-party rseq library was linked and failed to > >> > do so before thread exit (e.g. due to mismatched ref counts) rather > >> > than respecting the reference count, since it knows it's the last > >> > user. This would make potentially-buggy code safer. > >> > >> OK, let me go ahead with a few ideas/questions along that path. > > ^^^^^^^^^^^^^^^ > >> > >> Let's say our stated goal is to let the "exit" system call from the > >> glibc thread exit path perform rseq unregistration (without explicit > >> unregistration beforehand). Let's look at what we need. > > > > This is not "along that path". The above-quoted text is not about > > assuming it's safe to make SYS_exit without unregistering the rseq > > object, but rather about glibc being able to perform the > > rseq-unregister syscall without caring about reference counts, since > > it knows no other code that might depend on rseq can run after it. > > When saying "along that path", what I mean is: if we go in that direction, > then we should look into going all the way there, and rely on thread > exit to implicitly unregister the TLS area. > > Do you see any reason for doing an explicit unregistration at thread > exit rather than simply rely on the exit system call ? Whether this is needed is an implementation detail of glibc that should be permitted to vary between versions. Unless glibc wants to promise that it would become a public guarantee, it's not part of the discussion around the API/ABI. Only part of the discussion around implementation internals of the glibc rseq stuff. Of course I may be biased thinking application code should not assume this since it's not true on musl -- for detached threads, the thread frees its own stack before exiting (and thus has to unregister set_tid_address and set_robustlist before exiting). > >> First, we need the TLS area to be valid until the exit system call > >> is invoked by the thread. If glibc defines __rseq_abi as a weak symbol, > >> I'm not entirely sure we can guarantee the IE model if another library > >> gets its own global-dynamic weak symbol elected at execution time. Would > >> it be better to switch to a "strong" symbol for the glibc __rseq_abi > >> rather than weak ? > > > > This doesn't help; still whichever comes first in link order would > > override. Either way __rseq_abi would be in static TLS, though, > > because any dynamically-loaded library is necessarily loaded after > > libc, which is loaded at initial exec time. > > OK, AFAIU so you argue for leaving the __rseq_abi symbol "weak". Just making > sure I correctly understand your position. I don't think it matters, and I don't think making it weak is meaningful or useful (weak in a shared library is largely meaningless) but maybe I'm missing something here. > Something can be technically correct based on the current implementation, > but fragile with respect to future changes. We need to carefully distinguish > between the two when exposing ABIs. Yes. > >> There has been presumptions about signals being blocked when the thread > >> exits throughout this email thread. Out of curiosity, what code is > >> responsible for disabling signals in this situation ? > > This question is still open. I can't find it -- maybe it's not done in glibc. It is in musl, and I assumed glibc would also do it, because otherwise it's possible to see some inconsistent states from signal handlers. Maybe these are all undefined due to AS-unsafety of pthread_exit, but I think you can construct examples where something could be observably wrong without breaking any rules. > > Related to this, > >> is it valid to access a IE model TLS variable from a signal handler at > >> _any_ point where the signal handler nests over thread's execution ? > >> This includes early start and just before invoking the exit system call. > > > > It should be valid to access *any* TLS object like this, but the > > standards don't cover it well. Right now access to dynamic TLS from > > signal handlers is unsafe in glibc, but static is safe. > > Which is a shame for the lttng-ust tracer, which needs global-dynamic > TLS variables so it can be dlopen'd, but aims at allowing tracing from > signal handlers. It looks like due to limitations of global-dynamic > TLS, tracing from instrumented signal handlers with lttng-ust tracepoints > could crash the process if the signal handler nests early at thread start > or late before thread exit. One way out of this would be to ensure signals > are blocked at thread start/exit, but I can't find the code responsible for > doing this within glibc. Just blocking at start/exit won't solve the problem because global-dynamic TLS in glibc involves dynamic allocation, which is hard to make AS-safe and of course can fail, leaving no way to make forward progress. Rich