From owner-freebsd-threads@FreeBSD.ORG Sun Aug 31 18:27:14 2008 Return-Path: Delivered-To: freebsd-threads@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 00BFD106567D; Sun, 31 Aug 2008 18:27:14 +0000 (UTC) (envelope-from kostikbel@gmail.com) Received: from mail.terabit.net.ua (mail.terabit.net.ua [195.137.202.147]) by mx1.freebsd.org (Postfix) with ESMTP id 86E0D8FC0C; Sun, 31 Aug 2008 18:27:13 +0000 (UTC) (envelope-from kostikbel@gmail.com) Received: from skuns.zoral.com.ua ([91.193.166.194] helo=mail.zoral.com.ua) by mail.terabit.net.ua with esmtp (Exim 4.63 (FreeBSD)) (envelope-from ) id 1KZrdn-0009de-Mb; Sun, 31 Aug 2008 21:27:11 +0300 Received: from deviant.kiev.zoral.com.ua (root@deviant.kiev.zoral.com.ua [10.1.1.148]) by mail.zoral.com.ua (8.14.2/8.14.2) with ESMTP id m7VIR8dk034978 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO); Sun, 31 Aug 2008 21:27:08 +0300 (EEST) (envelope-from kostikbel@gmail.com) Received: from deviant.kiev.zoral.com.ua (kostik@localhost [127.0.0.1]) by deviant.kiev.zoral.com.ua (8.14.2/8.14.2) with ESMTP id m7VIR8h3096669; Sun, 31 Aug 2008 21:27:08 +0300 (EEST) (envelope-from kostikbel@gmail.com) Received: (from kostik@localhost) by deviant.kiev.zoral.com.ua (8.14.2/8.14.2/Submit) id m7VIR8uW096667; Sun, 31 Aug 2008 21:27:08 +0300 (EEST) (envelope-from kostikbel@gmail.com) X-Authentication-Warning: deviant.kiev.zoral.com.ua: kostik set sender to kostikbel@gmail.com using -f Date: Sun, 31 Aug 2008 21:27:08 +0300 From: Kostik Belousov To: Daniel Eischen Message-ID: <20080831182708.GN2038@deviant.kiev.zoral.com.ua> References: <20080829141043.GX2038@deviant.kiev.zoral.com.ua> <48B8052A.6070908@icyb.net.ua> <20080829143645.GY2038@deviant.kiev.zoral.com.ua> <20080829190506.GA2038@deviant.kiev.zoral.com.ua> <20080830155622.GF2038@deviant.kiev.zoral.com.ua> <20080830184512.GH2038@deviant.kiev.zoral.com.ua> Mime-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha1; protocol="application/pgp-signature"; boundary="yY9LxKOJMX9E+noF" Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.4.2.3i X-Virus-Scanned: ClamAV version 0.93.3, clamav-milter version 0.93.3 on skuns.kiev.zoral.com.ua X-Virus-Status: Clean X-Spam-Status: No, score=-4.4 required=5.0 tests=ALL_TRUSTED,AWL,BAYES_00 autolearn=ham version=3.2.5 X-Spam-Checker-Version: SpamAssassin 3.2.5 (2008-06-10) on skuns.kiev.zoral.com.ua X-Virus-Scanned: mail.terabit.net.ua 1KZrdn-0009de-Mb fa61bc0ad60d36ea49b1bc329fe8d6ab X-Terabit: YES Cc: davidxu@freebsd.org, Andriy Gapon , freebsd-threads@freebsd.org Subject: Re: mysterious hang in pthread_create X-BeenThere: freebsd-threads@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Threading on FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sun, 31 Aug 2008 18:27:14 -0000 --yY9LxKOJMX9E+noF Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Sun, Aug 31, 2008 at 11:39:52AM -0400, Daniel Eischen wrote: > On Sat, 30 Aug 2008, Kostik Belousov wrote: >=20 > >On Sat, Aug 30, 2008 at 12:15:31PM -0400, Daniel Eischen wrote: > >>On Sat, 30 Aug 2008, Kostik Belousov wrote: > >> > >>>On Sat, Aug 30, 2008 at 11:32:35AM -0400, Daniel Eischen wrote: > >>>>On Fri, 29 Aug 2008, Kostik Belousov wrote: > >>>>> > >>>>>As demonstrated by Andriy' example, we need _thr_rtld_init() be call= ed > >>>>>before any rtld locks are given chance to be acquired. _thr_rtld_ini= t() > >>>>>shall be protected from repeated invocation, and _thr_setthreaded() > >>>>>implements exactly this. > >>>>> > >>>>>If calling _thr_setthreaded(1) has not quite right intent, could you, > >>>>>please, suggest satisfying solution ? > >>>> > >>>>I'm not sure I _quite_ understand the problem, but why > >>>>wouldn't you have the same potential problem with some > >>>>other library (without libthread)? I'll have to go back > >>>>and read the beginning of the thread - I just kinda came > >>>>in at the end. > >>> > >>>Sure, for appropriate value of any. If you mean whether the same probl= em > >>>would arise for any threading library that supplies locking=20 > >>>implementation > >>>for rtld, then certainly yes. I looked over and patched only libthr > >>>since this is the only survived library for now. > >> > >>What I mean is, is fixing libthr a solution that will work > >>for cases? Or, is libthr doing something wrong? I can't > >>really see that it is. > >> > >>libthr assumes that everything is single-threaded (or > >>serialized, I guess) before a thread is created. I > >>am looking at this thread: > >> > >> http://docs.freebsd.org/cgi/getmsg.cgi?fetch=3D5235+0+current/freebsd= -threads > >> > >>Where is the corresponding unlock for the wlock_acquire()? > >>I guess this is the problem. When would this normally > >>be released (without libthr being linked in)? > >> > >>Also, the __isthreaded flag is used in libc to avoid taking > >>locks unless necessary. So if you have a single threaded > >>application that is also linked with libthr, you don't > >>pay the penalty of locking overhead. Lots of 3rd-party > >>libraries link with a threads library, so an application > >>may not even know it is "threaded". > >> > >>> > >>>Anyway, I do not insist on the proposed solution, and definitely > >>>prefer the change that is well aligned with libthr architecture. > >> > >>I'm not arguing anything, I just don't know that the problem > >>lies within lib. Of course, the > >>rtld init stuff could be pulled out and done in thread > >>initialization instead of thr_setthreaded(). That doesn't > >>leave much in thr_setthreaded, and it also adds locking > >>overhead into rtld for single-threaded programs that are > >>linked with libthr... > > > >Ok, let me to tell the whole story. I am sure that in fact you know > >it better then me. > > > >Assuming libthr is the only threading library, there are two locking > >implementations for the rtld: 'default' and the one supplied by libthr. > >On the first call to pthread_create(), libthr calls _rtld_thread_init() > >to substitute the default by the implementation from libthr. > > > >In fact, default implementation is broken from my point of view. For > >instance, thread_flag update is not atomic. Moreover, it does not > >correctly handles sequential acquision of several locks, due > >to thread_flag. > > > >The dl_iterate_phdr() function, called by gcc exception handling support > >code, does exactly this. It acquires rtld_phdr_lock, then rtld_bind_lock. > >[I shall admit it does this after my change]. In particular, this would > >leave the bit for the bind lock set in the thread_flag. > > > >Andriy' example throw the exception and calls dl_iterate_phdr() before > >first thread is created. On thread creation, _rtld_thread_init() is > >called, that tries to move the locks according to thread_flag. This is > >the cause for the reported wlock acquisition. > > > >I do not want to change anything in the default rtld locking. It is > >disfunctional from the time libc_r is gone, and I think it would be > >better to make it nop. My change makes the image that is linked with > >libthr, to consistently use libthr locks. >=20 > What happens if you remove the thread_flag() stuff (support > for libc_r?) from rtld? It seems that libc_r should be providing > its own rtld locking hooks - just like libthr does. No, libc_r uses default rtld locking, as absence of the references to _rtld_thread_init in libc_r code indicates. This is one of the reasons why I decline to change the code. We still support compat-4.x on all supported systems, but I have no way to properly test it, If changing the default implementation, I think we should just record lock attempts to be able to transfer them when _rtld_thread_init is called. But this would break libc_r. I saw the use of the libthr locking from the start as the cleanest and least intrusive. As I said, I would gladly accept any better idea. --yY9LxKOJMX9E+noF Content-Type: application/pgp-signature Content-Disposition: inline -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.9 (FreeBSD) iEYEARECAAYFAki64nsACgkQC3+MBN1Mb4gLwACgufpcBFyw9Brld30rcXBeHalX qIIAoJxtTMjT39lGFKj4dVXf3Bz+yMbm =BvKg -----END PGP SIGNATURE----- --yY9LxKOJMX9E+noF--