linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Davide Libenzi <davidel@xmailserver.org>
To: Linus Torvalds <torvalds@transmeta.com>
Cc: Andrea Arcangeli <andrea@suse.de>, Jeff Dike <jdike@karaya.com>,
	user-mode-linux-user@lists.sourceforge.net,
	linux-kernel@vger.kernel.org, Jan Hubicka <jh@suse.cz>,
	Jonathan Lundell <jlundell@pobox.com>,
	Alexander Viro <viro@math.psu.edu>
Subject: Re: user-mode port 0.44-2.4.7
Date: Tue, 24 Jul 2001 10:31:57 -0700 (PDT)	[thread overview]
Message-ID: <XFMail.20010724103157.davidel@xmailserver.org> (raw)
In-Reply-To: <Pine.LNX.4.33.0107240949460.29448-100000@penguin.transmeta.com>


On 24-Jul-2001 Linus Torvalds wrote:
> But it shouldn't optimize it that way _every_ time. You only want the
> specific optimizations in specific places. Which is why you use
> "barrier()" or volatile in the _code_, not the data declaration.
> 
> For example, if you're holding a lock that protects it or you otherwise
> know that nothing is touching it at the same time, you do NOT want to have
> the compiler generate bad code.
> 
> And trust me, "volatile" generates _bad_ code a lot more often than it
> generates correct code.
> 
> Look at this:
> 
>       volatile int i;
>       int j;
> 
>       int main()
>       {
>               i++;
>               j++;
>       }
> 
> turning into this:
> 
>       main:
>               movl i,%eax
>               incl %eax
>               movl %eax,i
>               incl j
>               ret
> 
> Now, ask yourself why? The two _should_ be the same. Both do a
> read-modify-write cycle. But the fact is, that when you add "volatile" to
> the register, it really tells gcc "Be afraid.  Be very afraid. This user
> expects some random behaviour that is not actually covered by any
> standard, so just don't ever use this variable for any optimizations, even
> if they are obviously correct. That way he can't complain".

This is a too simple case, this is maybe better :

        mov homer, %edx
        ...
        ...
        ...
        ... ( 101 asm ins )
loop:
        cmp %edx, ...
        ja out
        ...
        inc %edx
        ...
        jmp loop

You're right, it might be optimized with a barrier() but it's all kind of how
much times you're going to need one behaviour or the other.
When I'll need most of my access to be "strict" I'd like to have a way that avoid
me to spread the code with barries()s.


> Also note how the "incl j" instruction is actually _better_ from a
> "atomicity" standpoint than the "load+inc+store" instruction. In this
> case, adding a "volatile" actually made the accesses to "i" be _less_
> likely to be correct - you could have had an interrupt happen in between
> that also updated "i", and got lost when we wrote the value back.

Not that much if you look at how incl is "decomposed" internally ( w/o LOCK )
by the CPU. If you really care about  j  you need an atomic op here, in any case.





- Davide


  reply	other threads:[~2001-07-24 17:28 UTC|newest]

Thread overview: 49+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2001-07-23  5:08 user-mode port 0.44-2.4.7 Jeff Dike
2001-07-23 15:56 ` Andrea Arcangeli
2001-07-23 15:59   ` Andrea Arcangeli
2001-07-23 16:17     ` Andrea Arcangeli
2001-07-23 16:51       ` Andrea Arcangeli
2001-07-23 16:33   ` Linus Torvalds
2001-07-23 16:45     ` Andrea Arcangeli
2001-07-23 17:32       ` Linus Torvalds
2001-07-23 17:50         ` Andrea Arcangeli
2001-07-23 18:11           ` Linus Torvalds
2001-07-23 18:27             ` Andrea Arcangeli
2001-07-23 20:00               ` Linus Torvalds
2001-07-23 20:15                 ` Jonathan Lundell
2001-07-23 22:51                   ` Linus Torvalds
2001-07-24  3:45                     ` Jonathan Lundell
2001-07-24 15:41                       ` Davide Libenzi
2001-07-24 15:46                         ` Alexander Viro
2001-07-24 16:01                           ` Davide Libenzi
2001-07-24 16:08                             ` Alexander Viro
2001-07-24 16:52                               ` Davide Libenzi
2001-07-24 16:59                                 ` Linus Torvalds
2001-07-24 17:31                                   ` Davide Libenzi [this message]
2001-07-24 17:38                                     ` Linus Torvalds
2001-07-24 18:07                               ` Anton Altaparmakov
2001-07-23 20:44                 ` Chris Friesen
2001-07-23 21:11                   ` Andrea Arcangeli
2001-07-23 21:50                   ` Richard Gooch
2001-07-23 22:09                     ` Andrea Arcangeli
2001-07-23 13:20                       ` Rob Landley
2001-07-23 22:27                         ` Andrea Arcangeli
2001-07-23 17:50                           ` Rob Landley
2001-07-23 23:47                     ` Richard Gooch
2001-07-24  0:04                       ` Andrea Arcangeli
2001-07-24  9:02                         ` Jan Hubicka
2001-07-24 15:35                           ` Linus Torvalds
2001-07-24 16:04                         ` Linus Torvalds
2001-07-25 22:49                           ` Andrea Arcangeli
2001-07-25 23:16                             ` Linus Torvalds
2001-07-25 23:37                               ` Chris Friesen
2001-07-26 18:28                             ` Jan Hubicka
2001-07-26 18:35                               ` Alan Cox
2001-07-23 22:53                   ` Linus Torvalds
2001-07-23 23:13                     ` Alan Cox
2001-07-23 20:25   ` Jeff Dike
     [not found] <no.id>
2001-07-23 20:57 ` Alan Cox
2001-07-23 21:14   ` Chris Friesen
2001-07-25 19:12 ` Alan Cox
2001-07-25 23:49 ` Alan Cox
2001-07-25 19:03 James W. Lake

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=XFMail.20010724103157.davidel@xmailserver.org \
    --to=davidel@xmailserver.org \
    --cc=andrea@suse.de \
    --cc=jdike@karaya.com \
    --cc=jh@suse.cz \
    --cc=jlundell@pobox.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=torvalds@transmeta.com \
    --cc=user-mode-linux-user@lists.sourceforge.net \
    --cc=viro@math.psu.edu \
    /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;
as well as URLs for NNTP newsgroup(s).