[PATCH] arm64/io: Remind compiler that there is a memory side effect

2022-04-01 Thread Jeremy Linton via Gcc
The relaxed variants of read/write macros are only declared
as `asm volatile()` which forces the compiler to generate the
instruction in the code path as intended. The only problem
is that it doesn't also tell the compiler that there may
be memory side effects. Meaning that if a function is comprised
entirely of relaxed io operations, the compiler may think that
it only has register side effects and doesn't need to be called.

For an example function look at bcmgenet_enable_dma(), before the
relaxed variants were removed. When built with gcc12 the code
contains the asm blocks as expected, but then the function is
never called.

Signed-off-by: Jeremy Linton 
---
 arch/arm64/include/asm/io.h | 8 
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/arch/arm64/include/asm/io.h b/arch/arm64/include/asm/io.h
index 7fd836bea7eb..3cceda7948a0 100644
--- a/arch/arm64/include/asm/io.h
+++ b/arch/arm64/include/asm/io.h
@@ -24,25 +24,25 @@
 #define __raw_writeb __raw_writeb
 static inline void __raw_writeb(u8 val, volatile void __iomem *addr)
 {
-   asm volatile("strb %w0, [%1]" : : "rZ" (val), "r" (addr));
+   asm volatile("strb %w0, [%1]" : : "rZ" (val), "r" (addr) : "memory");
 }
 
 #define __raw_writew __raw_writew
 static inline void __raw_writew(u16 val, volatile void __iomem *addr)
 {
-   asm volatile("strh %w0, [%1]" : : "rZ" (val), "r" (addr));
+   asm volatile("strh %w0, [%1]" : : "rZ" (val), "r" (addr) : "memory");
 }
 
 #define __raw_writel __raw_writel
 static __always_inline void __raw_writel(u32 val, volatile void __iomem *addr)
 {
-   asm volatile("str %w0, [%1]" : : "rZ" (val), "r" (addr));
+   asm volatile("str %w0, [%1]" : : "rZ" (val), "r" (addr) : "memory");
 }
 
 #define __raw_writeq __raw_writeq
 static inline void __raw_writeq(u64 val, volatile void __iomem *addr)
 {
-   asm volatile("str %x0, [%1]" : : "rZ" (val), "r" (addr));
+   asm volatile("str %x0, [%1]" : : "rZ" (val), "r" (addr) : "memory");
 }
 
 #define __raw_readb __raw_readb
-- 
2.35.1



Re: GCC 12 miscompilation of volatile asm (was: Re: [PATCH] arm64/io: Remind compiler that there is a memory side effect)

2022-04-11 Thread Jeremy Linton via Gcc

Hi,


On 4/11/22 05:31, Mark Rutland wrote:

On Tue, Apr 05, 2022 at 01:51:30PM +0100, Mark Rutland wrote:

Hi all,

[adding kernel folk who work on asm stuff]

As a heads-up, GCC 12 (not yet released) appears to erroneously optimize away
calls to functions with volatile asm. Szabolcs has raised an issue on the GCC
bugzilla:

   https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105160

... which is a P1 release blocker, and is currently being investigated.


Jan Hubicka fixed this in GCC commit:

   aabb9a261ef060cf ("Propagate nondeterministic and side_effects flags in modref 
summary after inlining")

... and all my local tests look good with that applied.

Compiler explorer's trunk build now has that fix, so the examples from before
now look good:

   aarch64: https://godbolt.org/z/vMczqjYvs

   x86_64: https://godbolt.org/z/cveff9hq5

Jeremy, now that the real issue has been identified and fixed, I assume you'll
send a revert for commit:

   8d3ea3d402db94b6 ("net: bcmgenet: Use stronger register read/writes to assure 
ordering")

... ?


Yes, that's the plan.

Thanks,