public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: "H. Peter Anvin" <hpa@zytor.com>
To: Segher Boessenkool <segher@kernel.crashing.org>
Cc: "Ahmed S. Darwish" <darwish.07@gmail.com>,
	x86@kernel.org, Rusty Russell <rusty@rustcorp.com.au>,
	Ingo Molnar <mingo@redhat.com>,
	linux-kernel@vger.kernel.org
Subject: Re: x86: Is 'volatile' necessary for readb/writeb and friends?
Date: Fri, 04 Dec 2009 09:30:10 -0800	[thread overview]
Message-ID: <4B194722.9020705@zytor.com> (raw)
In-Reply-To: <8B1FEF0C-5D71-4D64-ADC3-1EE60F50779F@kernel.crashing.org>

On 12/04/2009 06:39 AM, Segher Boessenkool wrote:
>> x86 memory-mapped IO register accessors cast the memory mapped address
>> parameter to a one with the 'volatile' type qualifier. For example, here
>> is readb() after cpp processing
>>
>> --> arch/x86/include/asm/io.h:
>>
>> static inline unsigned char readb(const volatile void __iomem *addr) {
> 
> This "volatile" is meaningless.

Wrong.  "volatile" here is an assertion that it is safe to pass pointer
to a volatile object to this function.

>>     unsigned char ret;
>>     asm volatile("movb %1, %0"
> 
> This "volatile" is required; without it, if "ret" isn't used (or can
> be optimised away), the asm() could be optimised away.
> 
>>              :"=q" (ret)
>>              :"m" (*(volatile unsigned char __force *)addr)
> 
> This "volatile" has no effect, since the asm has a "memory" clobber.
> Without that clobber, this "volatile" would prevent moving the asm
> over other memory accesses.
> 
> If you want to get all language-lawyery, if the object pointed to by
> "addr" is volatile, the volatile here _is_ needed: accessing volatile
> objects via a not volatile-qualified lvalue is undefined.  But since
> this is GCC-specific code anyway, do you care?  :-)

Again, this comes from the prototype being volatile.

Either way, it works, it is guaranteed to be safe, and removing it can
only introduce bugs, not remove them.

	-hpa

-- 
H. Peter Anvin, Intel Open Source Technology Center
I work for Intel.  I don't speak on their behalf.


  parent reply	other threads:[~2009-12-04 17:34 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-12-04  9:21 x86: Is 'volatile' necessary for readb/writeb and friends? Ahmed S. Darwish
2009-12-04 14:39 ` Segher Boessenkool
2009-12-04 16:00   ` Arnd Bergmann
2009-12-04 17:30   ` H. Peter Anvin [this message]
2009-12-04 19:54     ` Segher Boessenkool

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=4B194722.9020705@zytor.com \
    --to=hpa@zytor.com \
    --cc=darwish.07@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@redhat.com \
    --cc=rusty@rustcorp.com.au \
    --cc=segher@kernel.crashing.org \
    --cc=x86@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