From: Willy Tarreau <w@1wt.eu>
To: Andi Kleen <ak@suse.de>
Cc: Linus Torvalds <torvalds@linux-foundation.org>,
Nick Piggin <nickpiggin@yahoo.com.au>,
Linux Kernel Mailing List <linux-kernel@vger.kernel.org>
Subject: Re: Is gcc thread-unsafe?
Date: Fri, 26 Oct 2007 06:57:55 +0200 [thread overview]
Message-ID: <20071026045754.GX10199@1wt.eu> (raw)
In-Reply-To: <200710260142.37902.ak@suse.de>
On Fri, Oct 26, 2007 at 01:42:37AM +0200, Andi Kleen wrote:
> On Friday 26 October 2007 01:32:53 Linus Torvalds wrote:
> >
> > On Fri, 26 Oct 2007, Andi Kleen wrote:
> > >
> > > No it can't (at least not on x86) as I have explained in the rest of the mail
> > > you conveniently snipped.
> >
> > I "conveniently snipped it" because it was pointless.
> >
> > "adc" or "cmov" has nothing what-so-ever to do with it. If some routine
> > returns 0-vs-1 and gcc then turns "if (routine()) x++" into
> > "x+=routine()", what does that have to do with adc or cmov?
>
> That is not what gcc did in that case. I don't think it tracks sets of values
> over function calls (or even inside functions) at all.
>
> The generated code was
>
> cmpl $1, %eax ; test res
> movl acquires_count, %edx ; load
> adcl $0, %edx ; maybe add 1
> movl %edx, acquires_count ; store
>
> So it just added the result of a comparison into a variable
> by (ab)using carry for this.
While this is OK in mono-threaded code, it introduces a race condition in
multi-threaded code. The code above tried to acquire a lock, and eax was
set to 1 if it succeeded. And whatever the result, all threads still
happily modify the shared memory area (acquires_count). So the classical
case where two threads perform the same operation at the same time ends
up with a random value in acquires_count.
> In theory such things can be done with CMOV too by redirecting
> a store into a dummy variable to cancel it, but gcc doesn't
> do that on its own.
Even with a CMOV, it's the memory write which should not be performed
if the lock was not acquired.
(...)
> But for registers it's a fine optimization.
100% agree.
What would really be needed is an attribute around conditions to
indicate whether they *may* be optimized or not. Something similar
to the likely/unlikely we currently use, we could have something
like __attribute__((unsafe_cond(cond))). I think that it could still
optimize by default but let the user explicitly state that he is
playing with thread-unsafe code. As you pointed out, you did not
find any such mis-optimization in the kernel, which means that it
does not hit too often. That's the reason why I'd let the user be
careful.
Willy
next prev parent reply other threads:[~2007-10-26 5:04 UTC|newest]
Thread overview: 61+ messages / expand[flat|nested] mbox.gz Atom feed top
2007-10-25 3:24 Is gcc thread-unsafe? Nick Piggin
2007-10-25 3:46 ` Arjan van de Ven
2007-10-25 3:58 ` Nick Piggin
2007-10-25 4:29 ` David Schwartz
2007-10-25 4:35 ` Arjan van de Ven
2007-10-25 18:45 ` Måns Rullgård
2007-10-25 4:47 ` Nick Piggin
2007-10-25 9:40 ` Samuel Tardieu
2007-10-25 9:44 ` Samuel Tardieu
2007-10-25 9:54 ` Samuel Tardieu
2007-10-25 9:55 ` Andi Kleen
2007-10-25 7:15 ` Andi Kleen
2007-10-25 11:58 ` linux-os (Dick Johnson)
2007-10-25 12:16 ` Andi Kleen
2007-10-25 22:49 ` Nick Piggin
2007-10-25 23:09 ` Andi Kleen
2007-10-25 23:14 ` Linus Torvalds
2007-10-25 23:16 ` Andi Kleen
2007-10-25 23:32 ` Linus Torvalds
2007-10-25 23:42 ` Andi Kleen
2007-10-25 23:57 ` Linus Torvalds
2007-10-26 1:15 ` Zachary Amsden
2007-10-26 4:57 ` Willy Tarreau [this message]
2007-10-25 23:43 ` Nick Piggin
2007-10-25 23:55 ` Andi Kleen
2007-10-25 23:57 ` Nick Piggin
2007-10-25 14:55 ` Linus Torvalds
2007-10-25 15:12 ` Pekka Enberg
2007-10-25 21:42 ` David Schwartz
2007-10-25 23:22 ` Nick Piggin
2007-10-26 11:59 ` Andrew Haley
2007-10-26 17:39 ` Chris Friesen
2007-10-26 11:59 ` Andrew Haley
2007-10-25 22:26 ` Ismail Dönmez
2007-10-25 22:56 ` Jeff Garzik
2007-10-25 23:04 ` Jeff Garzik
2007-10-31 22:10 ` Phillip Susi
[not found] <fa.JbRGo0cQWncrcfKHmiNdvchsA50@ifi.uio.no>
[not found] ` <fa.8qDECVaPIo7DWbjhQbyw6N5Infg@ifi.uio.no>
[not found] ` <fa.M4DOMggyrQmdTqekWSuw4xCxiTc@ifi.uio.no>
2007-10-25 23:27 ` Robert Hancock
[not found] <e2e108260710260729x4603211cgb68d7434ce1e54e9@mail.gmail.com>
2007-10-26 14:40 ` Bart Van Assche
2007-10-26 15:09 ` Linus Torvalds
2007-10-26 15:34 ` Andrew Haley
2007-10-26 18:06 ` David Schwartz
2007-10-30 10:20 ` Andrew Haley
2007-11-02 15:29 ` Bart Van Assche
2007-11-02 15:38 ` Andrew Haley
2007-11-04 15:13 ` Bart Van Assche
2007-11-04 17:45 ` Linus Torvalds
2007-11-04 17:58 ` Andrew Haley
2007-11-04 18:06 ` Bart Van Assche
2007-11-02 17:18 ` David Schwartz
2007-10-26 21:45 ` Giacomo Catenazzi
2007-10-26 22:24 ` Linus Torvalds
2007-10-26 15:27 ` Linus Torvalds
2007-10-26 16:28 ` Linus Torvalds
2007-10-26 17:07 ` Bart Van Assche
2007-10-26 17:12 ` Andrew Haley
2007-10-26 17:25 ` Linus Torvalds
2007-10-26 18:08 ` Alan Cox
2007-10-26 18:14 ` Linus Torvalds
2007-10-26 20:39 ` Andi Kleen
-- strict thread matches above, loose matches on Subject: below --
2007-10-28 18:19 linux
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20071026045754.GX10199@1wt.eu \
--to=w@1wt.eu \
--cc=ak@suse.de \
--cc=linux-kernel@vger.kernel.org \
--cc=nickpiggin@yahoo.com.au \
--cc=torvalds@linux-foundation.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox