From mboxrd@z Thu Jan 1 00:00:00 1970 X-Msuck: nntp://news.gmane.org/gmane.linux.lib.musl.general/561 Path: news.gmane.org!not-for-mail From: Rich Felker Newsgroups: gmane.linux.lib.musl.general Subject: Re: tough choice on thread pointer initialization issue Date: Fri, 10 Feb 2012 12:00:15 -0500 Message-ID: <20120210170015.GH146@brightrain.aerifal.cx> References: <20120210025824.GA25414@brightrain.aerifal.cx> <20120210074002.GA13559@openwall.com> <20120210075818.GG146@brightrain.aerifal.cx> <20120210104252.GA14485@openwall.com> Reply-To: musl@lists.openwall.com NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: dough.gmane.org 1328893250 10336 80.91.229.3 (10 Feb 2012 17:00:50 GMT) X-Complaints-To: usenet@dough.gmane.org NNTP-Posting-Date: Fri, 10 Feb 2012 17:00:50 +0000 (UTC) To: musl@lists.openwall.com Original-X-From: musl-return-562-gllmg-musl=m.gmane.org@lists.openwall.com Fri Feb 10 18:00:49 2012 Return-path: Envelope-to: gllmg-musl@plane.gmane.org Original-Received: from mother.openwall.net ([195.42.179.200]) by plane.gmane.org with smtp (Exim 4.69) (envelope-from ) id 1Rvtq3-0005IA-Pu for gllmg-musl@plane.gmane.org; Fri, 10 Feb 2012 18:00:47 +0100 Original-Received: (qmail 12110 invoked by uid 550); 10 Feb 2012 17:00:47 -0000 Mailing-List: contact musl-help@lists.openwall.com; run by ezmlm Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: Original-Received: (qmail 12102 invoked from network); 10 Feb 2012 17:00:47 -0000 Content-Disposition: inline In-Reply-To: <20120210104252.GA14485@openwall.com> User-Agent: Mutt/1.5.21 (2010-09-15) Xref: news.gmane.org gmane.linux.lib.musl.general:561 Archived-At: On Fri, Feb 10, 2012 at 02:42:52PM +0400, Solar Designer wrote: > On Fri, Feb 10, 2012 at 02:58:18AM -0500, Rich Felker wrote: > > On Fri, Feb 10, 2012 at 11:40:02AM +0400, Solar Designer wrote: > > > approach 4: initialize the thread pointer register to zero at program > > > startup. Then before its use, just check it for non-zero instead of > > > checking a global flag. (I presume that you're doing the latter now, > > > based on your description of the problem.) > > > > Well this is definitely feasible but it makes more arch-specific code, > > since examining the thread register needs asm. > > But accessing it needs asm anyway, no? Well it's a little bit more complicated, but yes. The code that would need fixing to use this approach is the "lazy init already completed" version (__pthread_self() macro/inline) which intentionally has no references to the init code to avoid pulling it into static bins that don't want the extra code and data. This code would have to check that the register is valid, and if not, backup registers (since the asm is specified not to clobber any in the normal case) on the stack and make a function call to a function that would get the proper value for the thread register and fix it. This could be done without too much bloat using some nonstandard calling convention hacks (putting the local register save/restore code in the callee rather than caller) I suppose. I don't claim any of this is impossible or even necessarily too difficult to make approach 4 feasible, but it does at least require some thinking about it. > "Potentially" is the keyword here, but anyway I just did some testing on > a Core 2'ish CPU (E5420), and the instruction is very fast. I put 1000 > instances of: > > movl %ss,%eax > testl %eax,%eax > jz 0 > > one after another, and this runs in just 1 cycle per the three > instructions above (so 1000 cycles for the 1000 instances total). > Of course, there are data dependencies here, so there's probably a > bypass involved and the instructions are grouped differently, like: OK, looks like performance is not an issue, at least on your hardware. > In actual code (where you won't have repeats like this), you might want > to insert some instructions between the mov and the test (if you can). Unfortunately the only useful thing would be dereferencing %gs:0, which is not a good idea if the test/jz hasn't completed... In pure asm more could be done, but this code is only used in inline asm in C, not in pure asm functions. After all the ugly arch-specific bugs in glibc's asm thread primitives, I really don't want to go there.. :-) > All of these execute in 1000 cycles total as well. With "w" forms of > the instructions there are extra prefixes, so I think these should > better be avoided, even though there's no slowdown from them on this CPU. I had no idea it was even valid to use the non-w-prefix forms with segment registers. Learned something new. Are the high bits just discarded (when writing) and zeroed (when reading)? Rich