From: Nick Piggin <nickpiggin@yahoo.com.au>
To: Andi Kleen <ak@novell.com>
Cc: Linux Kernel Mailing List <linux-kernel@vger.kernel.org>,
Linus Torvalds <torvalds@linux-foundation.org>
Subject: Re: Is gcc thread-unsafe?
Date: Fri, 26 Oct 2007 09:43:44 +1000 [thread overview]
Message-ID: <200710260943.44719.nickpiggin@yahoo.com.au> (raw)
In-Reply-To: <200710260109.50092.ak@novell.com>
On Friday 26 October 2007 09:09, Andi Kleen wrote:
> On Friday 26 October 2007 00:49:42 Nick Piggin wrote:
> > Marking volatile I think is out of the question. To start with,
> > volatile creates really poor code (and most of the time we actually
> > do want the code in critical sections to be as tight as possible).
>
> Poor code is better than broken code I would say. Besides
> the cross CPU synchronization paths are likely dominated by
> cache misses anyways; it's unlikely they're actually limited
> by the core CPU. So it'll probably not matter all that much
> if the code is poor or not.
But we have to mark all memory access inside the critical section
as volatile, even after the CPU synchronisation, and often the
common case where there is no contention or cacheline bouncing.
Sure, real code is dominated by cache misses anyway, etc etc.
However volatile prevents a lot of real optimisations that we'd
actually want to do, and increases icache size etc.
> > But also because I don't think these bugs are just going to be
> > found easily.
> >
> > > It might be useful to come up with some kind of assembler pattern
> > > matcher to check if any such code is generated for the kernel
> > > and try it with different compiler versions.
> >
> > Hard to know how to do it. If you can, then it would be interesting.
>
> I checked my kernel for "adc" at least (for the trylock/++ pattern)
> and couldn't find any (in fact all of them were in
> data the compiler thought to be code). That was not a allyesconfig build,
> so i missed some code.
sbb as well.
> For cmov it's at first harder because they're much more frequent
> and cmov to memory is a multiple instruction pattern (the instruction
> does only support memory source operands). Luckily gcc
> doesn't know the if (x) mem = a; => ptr = cmov(x, &a, &dummy); *ptr = a;
> transformation trick so I don't think there are actually any conditional
> stores on current x86.
>
> It might be a problem on other architectures which support true
> conditional stores though (like IA64 or ARM)
It might just depend on the instruction costs that gcc knows about.
For example, if ld/st is expensive, they might hoist them out of
loops etc. You don't even need to have one of these predicate or
pseudo predicate instructions.
> Also I'm not sure if gcc doesn't know any other tricks like the
> conditional add using carry, although I cannot think of any related
> to stores from the top of my hat.
>
> Anyways, if it's only conditional add if we ever catch such a case
> it could be also handled with inline assembly similar to local_inc()
But we don't actually know what it is, and it could change with
different architectures or versions of gcc. I think the sanest thing
is for gcc to help us out here, seeing as there is this very well
defined requirement that we want.
If you really still want the optimisation to occur, I don't think it
is too much to use a local variable for the accumulator (eg. in the
simple example case).
next prev parent reply other threads:[~2007-10-25 23:50 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
2007-10-25 23:43 ` Nick Piggin [this message]
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=200710260943.44719.nickpiggin@yahoo.com.au \
--to=nickpiggin@yahoo.com.au \
--cc=ak@novell.com \
--cc=linux-kernel@vger.kernel.org \
--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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.