Skip to contentSkip to conversation

ARM: mtd-xip: work around clang/llvm bug

PATCH Superseded Arnd Bergmann 4 participants 13 messages Jul 8, 2019 – Jul 10, 2019 Arm (32-bit)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ARM: mtd-xip: work around clang/llvm bug

llvm gets confused by inline asm with .rep directives, which can lead to miscalculating the number of instructions inside it, and in turn lead to an overflow for relative address calculation:

/tmp/cfi_cmdset_0002-539a47.s: Assembler messages:
/tmp/cfi_cmdset_0002-539a47.s:11288: Error: bad immediate value for offset (4100) /tmp/cfi_cmdset_0002-539a47.s:11289: Error: bad immediate value for offset (4100)

This might be fixed in future clang versions, but is not hard to work around by just replacing the .rep with a series of eight unrolled nop instructions.

https://godbolt.org/z/DSM2Jy

---
 arch/arm/include/asm/mtd-xip.h | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
arch/arm/include/asm/mtd-xip.h +3 -1
index dfcef0152e3d..5ad0325604e4 100644
@@ -15,6 +15,8 @@
 #include <mach/mtd-xip.h>
 
 /* fill instruction prefetch */
-#define xip_iprefetch() 	do { asm volatile (".rep 8; nop; .endr"); } while (0)
+#define xip_iprefetch()	do {						\
+	 asm volatile ("nop; nop; nop; nop; nop; nop; nop; nop;");	\
+} while (0)								\
 
 #endif /* __ARM_MTD_XIP_H__ */
Signature · 1 line
2.20.0
Acked-by on v1 1/1
#1 lore raw
Quoted 5 lines · Arnd Bergmann

llvm gets confused by inline asm with .rep directives, which can lead to miscalculating the number of instructions inside it, and in turn lead to an overflow for relative address calculation:

Quoted 7 lines

/tmp/cfi_cmdset_0002-539a47.s: Assembler messages:
/tmp/cfi_cmdset_0002-539a47.s:11288: Error: bad immediate value for offset (4100) /tmp/cfi_cmdset_0002-539a47.s:11289: Error: bad immediate value for offset (4100)

This might be fixed in future clang versions, but is not hard to work around by just replacing the .rep with a series of eight unrolled nop instructions.

Seems like this is fixable on the Clang side as well. For now, thanks for the patch.

Quoted 3 lines

https://godbolt.org/z/DSM2Jy

^ prefix with Link: on both lines?
also, linking to clang trunk will become stale once the issue is fixed. When possible, link to a release version of clang that's not prone to change over time.

Quoted 17 lines
---
 arch/arm/include/asm/mtd-xip.h | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
arch/arm/include/asm/mtd-xip.h +3 -1
index dfcef0152e3d..5ad0325604e4 100644
@@ -15,6 +15,8 @@
 #include <mach/mtd-xip.h>
 
 /* fill instruction prefetch */
-#define xip_iprefetch()        do { asm volatile (".rep 8; nop; .endr"); } while (0)
+#define xip_iprefetch()        do {                                            \
+        asm volatile ("nop; nop; nop; nop; nop; nop; nop; nop;");      \
+} while (0)                                                            \
Signature · 2 lines
Thanks,
~Nick Desaulniers
Reviewed-by on v1 1/1
#2 lore raw
In reply to Arnd Bergmann
Quoted 1 line · Arnd Bergmann

llvm gets confused by inline asm with .rep directives,

Are the LLVM developers aware of the bug?
It seems like something we can work around but should eventually be fixed properly in LLVM, right?

Quoted 8 lines

which
can lead to miscalculating the number of instructions inside it, and in turn lead to an overflow for relative address calculation:

/tmp/cfi_cmdset_0002-539a47.s: Assembler messages:
/tmp/cfi_cmdset_0002-539a47.s:11288: Error: bad immediate value for offset (4100) /tmp/cfi_cmdset_0002-539a47.s:11289: Error: bad immediate value for offset (4100)

Quoted 7 lines

This might be fixed in future clang versions, but is not hard to work around by just replacing the .rep with a series of eight unrolled nop instructions.

https://godbolt.org/z/DSM2Jy

I guess this brings up the old question whether the compiler should be worked around or just considered immature, but as it happens this other day I was grep:ing around to find "the 8 NOP" that is so compulsively inserted in ARM executables (like at the very start of the kernel execution) and I couldn't find them and now I see why. Spelling them out makes it easier to find so:

Yours,
Linus Walleij

#3 lore raw
Quoted 5 lines · Linus Walleij

I guess this brings up the old question whether the compiler should be worked around or just considered immature, but as it happens this other day I was grep:ing around to find "the 8 NOP" that is so compulsively inserted in ARM executables (like at the very start of the kernel execution)

The NOPs at the start of the kernel executable have nothing what so ever to do with this. They are there to align the kernel entry with the old a.out format that was used (which had a 32 byte header). Consequently, there are boot loaders around that jump to 32 bytes into the kernel header.

There are other places that we insert 10 NOPs (at cpu_relax()) due to a CPU errata (otherwise a tight loop basically stalls other CPUs.)

Signature · 3 lines
RMK's Patch system: https://www.armlinux.org.uk/developer/patches/
FTTC broadband for 0.8mile line in suburbia: sync at 12.1Mbps down 622kbps up
According to speedtest.net: 11.9Mbps down 500kbps up
#4 lore raw
Quoted 7 lines · Russell King - ARM Linux admin
Quoted 5 lines · Linus Walleij

I guess this brings up the old question whether the compiler should be worked around or just considered immature, but as it happens this other day I was grep:ing around to find "the 8 NOP" that is so compulsively inserted in ARM executables (like at the very start of the kernel execution)

Quoted 5 lines

The NOPs at the start of the kernel executable have nothing what so ever to do with this. They are there to align the kernel entry with the old a.out format that was used (which had a 32 byte header). Consequently, there are boot loaders around that jump to 32 bytes into the kernel header.

Wow! Finally the puzzle pieces come together. And it makes a lot of sense.

Quoted 2 lines

There are other places that we insert 10 NOPs (at cpu_relax()) due to a CPU errata (otherwise a tight loop basically stalls other CPUs.)

Pretty interesting too!

I try to learn a bit more intrinsics of the Arm architecture (been doing assembly experiments recent days) so getting to know things like this is very valuable.

Yours,
Linus Walleij

#10 lore raw
In reply to Arnd Bergmann

arch/arm/include/asm/mtd-xip.h line 20

Quoted 4 lines · Arnd Bergmann
arch/arm/include/asm/mtd-xip.h +3 -1
-#define xip_iprefetch()        do { asm volatile (".rep 8; nop; .endr"); } while (0)
+#define xip_iprefetch()        do {                                            \
+        asm volatile ("nop; nop; nop; nop; nop; nop; nop; nop;");      \
+} while (0)                                                            \

This is certainly an OK fix since we use a row of inline nop at other places.

However after Russell explained the other nops I didn't understand I located these in boot/compressed/head.S as this in __start:

                .rept   7
                __nop
                .endr

#ifndef CONFIG_THUMB2_KERNEL
mov r0, r0
#else

And certainly this gets compiled, right?

So does .rept/.endr work better than .rep/.endr, is it simply mis-spelled?

I.e. s/.rep/.rept/g
?

In that case we should explain in the commit that .rep doesn't work but .rept does.

Yours,
Linus Walleij

#11 lore raw
Quoted 22 lines · Linus Walleij

arch/arm/include/asm/mtd-xip.h line 20

Quoted 4 lines · Arnd Bergmann
arch/arm/include/asm/mtd-xip.h +3 -1
-#define xip_iprefetch()        do { asm volatile (".rep 8; nop; .endr"); } while (0)
+#define xip_iprefetch()        do {                                            \
+        asm volatile ("nop; nop; nop; nop; nop; nop; nop; nop;");      \
+} while (0)                                                            \

This is certainly an OK fix since we use a row of inline nop at other places.

However after Russell explained the other nops I didn't understand I located these in boot/compressed/head.S as this in __start:

                .rept   7
                __nop
                .endr

#ifndef CONFIG_THUMB2_KERNEL
mov r0, r0
#else

And certainly this gets compiled, right?

Quoted 7 lines

So does .rept/.endr work better than .rep/.endr, is it simply mis-spelled?

I.e. s/.rep/.rept/g
?

In that case we should explain in the commit that .rep doesn't work but .rept does.

According to the info pages for gas:

7.96 `.rept COUNT'
==================

Repeat the sequence of lines between the .rept' directive and the next .endr' directive COUNT times.

So yes, ".rep" is mis-spelled, and it brings up the obvious question:
why isn't gas issuing an error for ".rep"? There is no mention of ".rep" in the manual.

Signature · 3 lines
RMK's Patch system: https://www.armlinux.org.uk/developer/patches/
FTTC broadband for 0.8mile line in suburbia: sync at 12.1Mbps down 622kbps up
According to speedtest.net: 11.9Mbps down 500kbps up
#5 lore raw
In reply to Linus Walleij
Quoted 8 lines · Linus Walleij
Quoted 1 line · Arnd Bergmann

llvm gets confused by inline asm with .rep directives,

Are the LLVM developers aware of the bug?
It seems like something we can work around but should eventually be fixed properly in LLVM, right?

Arnd filed the bug yesterday. I looked at it; so someone working on LLVM is aware of it. The kernel is definitely exercising weak points in our inline assembly support.

Quoted 4 lines
Quoted 1 line

I guess this brings up the old question whether the compiler should be worked around or just considered immature, but as it happens this

Definitely a balancing act; we prioritize work based on what's feasible to work around vs must be implemented. A lot of my time is going into validation of asm goto right now, but others are ramping up on the integrated assembler (clang itself can be invoked as a substitute for GNU AS; but there's not enough support to do make AS=clang for the kernel just yet).

Signature · 2 lines
Thanks,
~Nick Desaulniers
#12 lore raw
In reply to Russell King - ARM Linux admin
Quoted 37 lines · Russell King - ARM Linux admin
Quoted 22 lines · Linus Walleij

arch/arm/include/asm/mtd-xip.h line 20

Quoted 4 lines · Arnd Bergmann
arch/arm/include/asm/mtd-xip.h +3 -1
-#define xip_iprefetch()        do { asm volatile (".rep 8; nop; .endr"); } while (0)
+#define xip_iprefetch()        do {                                            \
+        asm volatile ("nop; nop; nop; nop; nop; nop; nop; nop;");      \
+} while (0)                                                            \

This is certainly an OK fix since we use a row of inline nop at other places.

However after Russell explained the other nops I didn't understand I located these in boot/compressed/head.S as this in __start:

                .rept   7
                __nop
                .endr

#ifndef CONFIG_THUMB2_KERNEL
mov r0, r0
#else

And certainly this gets compiled, right?

Quoted 7 lines

So does .rept/.endr work better than .rep/.endr, is it simply mis-spelled?

I.e. s/.rep/.rept/g
?

In that case we should explain in the commit that .rep doesn't work but .rept does.

According to the info pages for gas:

7.96 `.rept COUNT'
==================

Quoted 6 lines

Repeat the sequence of lines between the .rept' directive and the next .endr' directive COUNT times.

So yes, ".rep" is mis-spelled, and it brings up the obvious question:
why isn't gas issuing an error for ".rep"? There is no mention of ".rep" in the manual.

I swear I had looked this up somewhere and found that GNU as and clang's integrated assembler supported alternative spellings for assembly directives. Just checked the manual
https://ftp.gnu.org/old-gnu/Manuals/gas-2.9.1/html_chapter/as_7.html#SEC116
and indeed no mention of the alternatives...must have been looking at the source...

Signature · 2 lines
Thanks,
~Nick Desaulniers
#6 lore raw
In reply to Nick Desaulniers
Quoted 11 lines · Nick Desaulniers' via Clang Built Linux
Quoted 2 lines · Linus Walleij

I guess this brings up the old question whether the compiler should be worked around or just considered immature, but as it happens this

Definitely a balancing act; we prioritize work based on what's feasible to work around vs must be implemented. A lot of my time is going into validation of asm goto right now, but others are ramping up on the integrated assembler (clang itself can be invoked as a substitute for GNU AS; but there's not enough support to do make AS=clang for the kernel just yet).

Note that this bug is the same with both gas and AS=clang, which also indicates that clang implemented the undocumented .rep directive for compatibility.

Overall I think we are at the point where building the kernel with clang-8 is mature enough that we should work around bugs like this where it is easy enough rather than waiting for clang-9.

Arnd

#7 lore raw
Quoted 14 lines · Arnd Bergmann
Quoted 11 lines · Nick Desaulniers' via Clang Built Linux
Quoted 2 lines · Linus Walleij

I guess this brings up the old question whether the compiler should be worked around or just considered immature, but as it happens this

Definitely a balancing act; we prioritize work based on what's feasible to work around vs must be implemented. A lot of my time is going into validation of asm goto right now, but others are ramping up on the integrated assembler (clang itself can be invoked as a substitute for GNU AS; but there's not enough support to do make AS=clang for the kernel just yet).

Quoted 7 lines

Note that this bug is the same with both gas and AS=clang, which also indicates that clang implemented the undocumented .rep directive for compatibility.

Overall I think we are at the point where building the kernel with clang-8 is mature enough that we should work around bugs like this where it is easy enough rather than waiting for clang-9.

While both assemblers seem to support both .rept and .rep, might it be an idea to check what the clang-8 situation is with .rept ?

Signature · 3 lines
RMK's Patch system: https://www.armlinux.org.uk/developer/patches/
FTTC broadband for 0.8mile line in suburbia: sync at 12.1Mbps down 622kbps up
According to speedtest.net: 11.9Mbps down 500kbps up
#8 lore raw
Quoted 20 lines · Russell King - ARM Linux admin
Quoted 18 lines · Arnd Bergmann
Quoted 11 lines · Nick Desaulniers' via Clang Built Linux
Quoted 2 lines · Linus Walleij

I guess this brings up the old question whether the compiler should be worked around or just considered immature, but as it happens this

Definitely a balancing act; we prioritize work based on what's feasible to work around vs must be implemented. A lot of my time is going into validation of asm goto right now, but others are ramping up on the integrated assembler (clang itself can be invoked as a substitute for GNU AS; but there's not enough support to do make AS=clang for the kernel just yet).

Note that this bug is the same with both gas and AS=clang, which also indicates that clang implemented the undocumented .rep directive for compatibility.

Quoted 6 lines
Quoted 3 lines

Overall I think we are at the point where building the kernel with clang-8 is mature enough that we should work around bugs like this where it is easy enough rather than waiting for clang-9.

While both assemblers seem to support both .rept and .rep, might it be an idea to check what the clang-8 situation is with .rept ?

Good idea. I tried this patch now:

arch/arm/include/asm/mtd-xip.h +0 -1
@@ -15,6 +15,6 @@
 #include <mach/mtd-xip.h>
 
 /* fill instruction prefetch */
-#define xip_iprefetch()        do { asm volatile (".rep 8; nop;

.endr"); } while (0)
+#define xip_iprefetch() do { asm volatile (".rept 8; nop;
.endr"); } while (0)

#endif /* __ARM_MTD_XIP_H__ */

Unfortunately that has no effect, clang treats them both the same way.

Arnd

#9 lore raw
Quoted 42 lines · Arnd Bergmann
Quoted 20 lines · Russell King - ARM Linux admin
Quoted 18 lines · Arnd Bergmann
Quoted 11 lines · Nick Desaulniers' via Clang Built Linux
Quoted 2 lines · Linus Walleij

I guess this brings up the old question whether the compiler should be worked around or just considered immature, but as it happens this

Definitely a balancing act; we prioritize work based on what's feasible to work around vs must be implemented. A lot of my time is going into validation of asm goto right now, but others are ramping up on the integrated assembler (clang itself can be invoked as a substitute for GNU AS; but there's not enough support to do make AS=clang for the kernel just yet).

Note that this bug is the same with both gas and AS=clang, which also indicates that clang implemented the undocumented .rep directive for compatibility.

Quoted 6 lines
Quoted 3 lines

Overall I think we are at the point where building the kernel with clang-8 is mature enough that we should work around bugs like this where it is easy enough rather than waiting for clang-9.

While both assemblers seem to support both .rept and .rep, might it be an idea to check what the clang-8 situation is with .rept ?

Good idea. I tried this patch now:

arch/arm/include/asm/mtd-xip.h +1 -1
@@ -15,6 +15,6 @@
 #include <mach/mtd-xip.h>
 
 /* fill instruction prefetch */
-#define xip_iprefetch()        do { asm volatile (".rep 8; nop;
 .endr"); } while (0)
+#define xip_iprefetch()        do { asm volatile (".rept 8; nop;
 .endr"); } while (0)
Quoted 3 lines

#endif /* __ARM_MTD_XIP_H__ */

Unfortunately that has no effect, clang treats them both the same way.

In any case, good to know.

Signature · 3 lines
RMK's Patch system: https://www.armlinux.org.uk/developer/patches/
FTTC broadband for 0.8mile line in suburbia: sync at 12.1Mbps down 622kbps up
According to speedtest.net: 11.9Mbps down 500kbps up