x86: Is 'volatile' necessary for readb/writeb and friends?

From: Ahmed S. Darwish
Date: Fri Dec 04 2009 - 04:21:40 EST


Hi all,

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) {
unsigned char ret;
asm volatile("movb %1, %0"
:"=q" (ret)
:"m" (*(volatile unsigned char __force *)addr)
:"memory");
return ret;
}

I wonder if the volatile qualifiers in the parameter above and at the asm
statement operand were strictly necessary, or just added for extra safety.

AFAIK, the asm statement already functions as a compiler barrier, and the
compiler won't 'optimize' the statement away due to the 'asm volatile' part,
so shouldn't things be safe without those volatile qualifiers?

The only red-herring I found in the gcc manual was the fact that the
"volatile asm instruction can be moved relative to other code, including
across jump instructions."

I wonder if this was the reason a volatile-type data dependency was added
to the mov{b,w,l,q} asm statements; not to reorder the asm instruction
around non-memory-accessing instructions (we already have a barrier).

Thank you!

--
Darwish
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/