public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Oliver Neukum <oliver@neukum.org>
To: "Michael S. Tsirkin" <mst@redhat.com>
Cc: Rusty Russell <rusty@rustcorp.com.au>,
	torvalds@linux-foundation.org,
	LKML <linux-kernel@vger.kernel.org>
Subject: Re: Why does test_bit() take a volatile addr?
Date: Mon, 16 Sep 2013 10:49:40 +0200	[thread overview]
Message-ID: <1379321380.15916.23.camel@linux-fkkt.site> (raw)
In-Reply-To: <20130916084453.GA1222@redhat.com>

On Mon, 2013-09-16 at 11:44 +0300, Michael S. Tsirkin wrote:
> On Mon, Sep 16, 2013 at 10:40:00AM +0200, Oliver Neukum wrote:
> > On Mon, 2013-09-16 at 13:38 +0930, Rusty Russell wrote:
> > > Predates git, does anyone remember the rationale?
> > > 
> > > ie:
> > >         int test_bit(int nr, const volatile unsigned long *addr)
> > > 
> > > I noticed because gcc failed to elimiate some code in a patch I was
> > > playing with.
> > > 
> > > I'm nervous about subtle bugs involved in ripping it out, even if noone
> > > knows why.  Should I add __test_bit()?
> > 
> > It seems to me that if you do
> > 
> > b = *ptr & 0xf;
> > c = b << 2;
> > if (test_bit(1, ptr))
> > 
> > the compiler could optimize away the memory access on ptr without
> > the volatile. We'd have to add a lot of mb().
> > 
> > 	Regards
> > 		Oliver
> 
> What is this code supposed to do?
> Any specific examples?
> 

Often you see

while (test_bit(...) && condition) ... ;

If the compiler can show that you don't change the memory you
do the test_bit on, it can change this to:

if (test_bit(...))
	while (condition) ...;

That must not happen.

	Regards
		Oliver



  reply	other threads:[~2013-09-16  8:49 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-09-16  4:08 Why does test_bit() take a volatile addr? Rusty Russell
2013-09-16  6:53 ` Stephen Rothwell
2013-09-16  7:26   ` Michael S. Tsirkin
2013-09-16  8:02     ` Stephen Rothwell
2013-09-16  8:47       ` Michael S. Tsirkin
2013-09-16  8:20 ` Geert Uytterhoeven
2013-09-16  8:37 ` Michael S. Tsirkin
2013-09-16  8:40 ` Oliver Neukum
2013-09-16  8:44   ` Michael S. Tsirkin
2013-09-16  8:49     ` Oliver Neukum [this message]
2013-09-16 11:59 ` Linus Torvalds
2013-09-22 21:37 ` Rob Landley
2013-09-23  1:13   ` Rusty Russell

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=1379321380.15916.23.camel@linux-fkkt.site \
    --to=oliver@neukum.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mst@redhat.com \
    --cc=rusty@rustcorp.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