mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Linus Torvalds <torvalds@linux-foundation.org>
To: Bart Van Assche <bart.vanassche@gmail.com>
Cc: Linux Kernel Mailing List <linux-kernel@vger.kernel.org>,
	Andrew Haley <aph@redhat.com>
Subject: Re: Is gcc thread-unsafe?
Date: Fri, 26 Oct 2007 09:28:47 -0700 (PDT)	[thread overview]
Message-ID: <alpine.LFD.0.999.0710260917500.30120@woody.linux-foundation.org> (raw)
In-Reply-To: <alpine.LFD.0.999.0710260814570.30120@woody.linux-foundation.org>



On Fri, 26 Oct 2007, Linus Torvalds wrote:

> 
> 
> On Fri, 26 Oct 2007, Bart Van Assche wrote:
> > 
> > You can find my proposal to improve gcc here:
> > http://gcc.gnu.org/ml/gcc/2007-10/msg00465.html
> 
> Btw, I think this is fine per se, but putting "__attribute__((acquire))" 
> on the functions that acquire a lock does seem to be problematic, in that 
> quite often you might well want to inline those things. How would you 
> handle that?

Thinking some more about this, you really have two cases:

 - full functions taking/releasing locks (possibly conditionally, ie 
   with something lik etrylock and/or based on argument values). 

   You simply *cannot* require these to be marked, because the locking may 
   have been done indirectly. Yes, you can mark things like 
   "pthread_mutex_trylock()" as being an acquire-function, but the fact 
   is, users will then wrap these things in *other* functions, and return 
   their return values.

   Ergo: a compiler *must* assume that a function call that it 
   didn't inline involves locking. There's no point in adding some 
   gcc-specific attributes to system header files, because it's not going 
   to fix anything in any portable program.

 - inline assembly (together with, potentially, compiler primitives). 
   That's the only other way to reliably do locking from C. 

   This one gcc could certainly extend on. But would there really be any 
   upside? It would be easier/better to say that inline assembly (at least 
   if it clobbers memory or is volatile) has the same serialization issues 
   as a function call.

So the fact is, any compiler that turns

	if (conditional)
		x++;

into an unconditional write to x (where 'x' is potentially visible to the 
outside - global visibility or has had its address taken) is just broken. 

No ifs, buts or maybes about it. You simply cannot do that optimization, 
because there is no way for the compiler to know whether the conditional 
implies that you hold a lock or not.

				Linus

  reply	other threads:[~2007-10-26 16:29 UTC|newest]

Thread overview: 61+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [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 [this message]
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
2007-10-28 18:19 linux
     [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
  -- strict thread matches above, loose matches on Subject: below --
2007-10-25  3:24 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
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 11:59       ` Andrew Haley
2007-10-26 17:39         ` Chris Friesen
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

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=alpine.LFD.0.999.0710260917500.30120@woody.linux-foundation.org \
    --to=torvalds@linux-foundation.org \
    --cc=aph@redhat.com \
    --cc=bart.vanassche@gmail.com \
    --cc=linux-kernel@vger.kernel.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

all inboxes | Powered by JetHome®