From mboxrd@z Thu Jan 1 00:00:00 1970 X-Msuck: nntp://news.gmane.org/gmane.linux.lib.musl.general/11114 Path: news.gmane.org!.POSTED!not-for-mail From: Rich Felker Newsgroups: gmane.linux.lib.musl.general Subject: Re: Reviving planned ldso changes Date: Tue, 7 Mar 2017 17:02:09 -0500 Message-ID: <20170307220209.GV1520@brightrain.aerifal.cx> References: <20170115174438.GD1533@brightrain.aerifal.cx> <20170226010429.GQ12395@port70.net> <20170226013926.GY1520@brightrain.aerifal.cx> <20170226102830.GR12395@port70.net> <20170226152016.GZ1520@brightrain.aerifal.cx> <20170226153436.GA2082@port70.net> <20170226213925.GB1520@brightrain.aerifal.cx> <20170303013026.GJ1520@brightrain.aerifal.cx> <20170304105817.GF2082@port70.net> <20170306011159.GM1520@brightrain.aerifal.cx> Reply-To: musl@lists.openwall.com NNTP-Posting-Host: blaine.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: blaine.gmane.org 1488924147 10291 195.159.176.226 (7 Mar 2017 22:02:27 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Tue, 7 Mar 2017 22:02:27 +0000 (UTC) User-Agent: Mutt/1.5.21 (2010-09-15) To: musl@lists.openwall.com Original-X-From: musl-return-11129-gllmg-musl=m.gmane.org@lists.openwall.com Tue Mar 07 23:02:23 2017 Return-path: Envelope-to: gllmg-musl@m.gmane.org Original-Received: from mother.openwall.net ([195.42.179.200]) by blaine.gmane.org with smtp (Exim 4.84_2) (envelope-from ) id 1clNBS-0001p4-78 for gllmg-musl@m.gmane.org; Tue, 07 Mar 2017 23:02:18 +0100 Original-Received: (qmail 32262 invoked by uid 550); 7 Mar 2017 22:02:22 -0000 Mailing-List: contact musl-help@lists.openwall.com; run by ezmlm Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-ID: Original-Received: (qmail 32233 invoked from network); 7 Mar 2017 22:02:21 -0000 Content-Disposition: inline In-Reply-To: <20170306011159.GM1520@brightrain.aerifal.cx> Original-Sender: Rich Felker Xref: news.gmane.org gmane.linux.lib.musl.general:11114 Archived-At: On Sun, Mar 05, 2017 at 08:11:59PM -0500, Rich Felker wrote: > On Sat, Mar 04, 2017 at 11:58:18AM +0100, Szabolcs Nagy wrote: > > * Rich Felker [2017-03-02 20:30:26 -0500]: > > > Here's a v4 of the patch that saves the "init parent" we descended > > > from so that it can return where it left off. There are a couple > > > gratuitous hunks left over adding setting of "needed_by" where it made > > > sense to be set, but it's not actually used anymore. They could be > > > dropped if desired but are probably nice to keep for the sake of > > > consistency of data, even thoough it's data we don't use. > > > > > > I believe this can be extended to allow concurrent dlopen by amending > > > the case in the tree-walk where a dependency isn't constructed yet but > > > already has an "init parent" to check whether it's > > > pending-construction in the calling thread (recursive dlopen from a > > > ctor) or another thread; in the former case (as now) treat it as > > > already-constructed; in the latter, wait on a condvar that gets > > > signaled at the end of each construction, then continue the loop > > > without advancing p. There are probably some subtleties I'm missing, > > > though. > > .... > > > static void do_init_fini(struct dso *p) > > > { > > > size_t dyn[DYN_CNT]; > > > - int need_locking = libc.threads_minus_1; > > > - /* Allow recursive calls that arise when a library calls > > > - * dlopen from one of its constructors, but block any > > > - * other threads until all ctors have finished. */ > > > - if (need_locking) pthread_mutex_lock(&init_fini_lock); > > > - for (; p; p=p->prev) { > > > - if (p->constructed) continue; > > > + pthread_mutex_lock(&init_fini_lock); > > > + /* Construct in dependency order without any recursive state. */ > > > + while (p && !p->constructed) { > > > + /* The following loop descends into the first dependency > > > + * that is neither alredy constructed nor pending > > > + * construction due to circular deps, stopping only > > > + * when it reaches a dso with no remaining dependencies > > > + * to descend into. */ > > > + while (p->deps && p->deps[p->next_dep]) { > > > + if (!p->deps[p->next_dep]->constructed && > > > + !p->deps[p->next_dep]->init_parent) { > > > + p->deps[p->next_dep]->init_parent = p; > > > + p = p->deps[p->next_dep++]; > > > > i think the root may be visited twice because it > > has no init_parent, which may be problematic with > > the concurrent dlopen (and can cause unexpected > > ctor order: the root node is not constructed last > > if there is a cycle through it) > > Ah, the case where the root is an indirect dependency for itself? Yes, > I think you're right in that case. We should be able to avoid it by > setting the initial p->init_parent to head (the application), I think. > > > i think only checking init_parent of a dep is > > enough and the root node can have a dummy parent > > that is guaranteed to be not a dependency (ldso?) > > and constructed so it stops the loop. > > I think ldso would work too, but in principle it need not be a > dependency of anything if you have a dynamic-linked program that > doesn't use libc (-nostdlib), so it's better to use head, I think. > > Also I agree we don't need to check p->constructed now, but once we > unlock during ctor execution, the !init_parent and !constructed cases > need to be treated separately. If it's constructed or pending > construction in the same thread, we can just do p->next_dep++, but if > it has an init_parent but isn't constructed or pending construction in > same thread (recursive) we need to condvar wait and re-check instead, > right? Arg, deep problems I missed. Quoting from IRC: nsz, uhg, the dep-order draft so far has a big bug p->deps is not actually deps for p rather, it's all indirect deps, but only set for a lib that was explicitly dlopen'd so the new code doesn't actually do dep-order it just walks a flat list of all (breadth-first, not depth-first) direct and indirect dependencies of p and descends into each then immediately backs out because after descending, p->deps is null i think we should get rid of the old use of p->deps which is just undoing temp-globalization of libs during load for reloc purposes If I first do the work of having a separate global-namespace dso list (which is a pending change that will speed up relocations anyway), then the old use of p->deps is no longer needed and we can simply repurpose it to be direct-deps only. An alternate idea I like a lot, but don't see how to make work with concurrency (making it so ctors still running in one thread don't unnecessarily preclude a new dlopen in another thread), is to build a queue for ctor execution at the time dependencies are loaded. The logic for this seems simple: when processing the DT_NEEDED entry, check if the needed library is already constructed or already in the ctor queue. If so, do nothing. If not, insert it in the queue just before the library it's a dependency for. Rich