Skip to contentSkip to conversation

ARM: vfp: use asm volatile for FP control register accesses

PATCH In review Ard Biesheuvel 3 participants 5 messages Mar 18, 2024 – Mar 27, 2024

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ARM: vfp: use asm volatile for FP control register accesses

From: Ard Biesheuvel <[email protected]>

Clang may reorder FP control register reads and writes, due to the fact that the inline asm() blocks in the read/write wrappers are not volatile qualified, and the compiler has no idea that these reads and writes may have side effects.

In particular, reads of FPSCR may generate an UNDEF exception if a floating point exception is pending, and the FP emulation code in VFP_bounce() explicitly clears FP exceptions temporarily in order to be able to perform the emulation on behalf of user space. This requires that the writes to FPEXC are never reordered with respect to accesses to other FP control registers, such as FPSCR.

So use asm volatile for both the read and the write helpers.

---
 arch/arm/vfp/vfpinstr.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
arch/arm/vfp/vfpinstr.h +2 -2
index 3c7938fd40aa..c4ac778e6fc9 100644
@@ -66,14 +66,14 @@
 
 #define fmrx(_vfp_) ({			\
 	u32 __v;			\
-	asm(".fpu	vfpv2\n"	\
+	asm volatile(".fpu vfpv2\n"	\
 	    "vmrs	%0, " #_vfp_	\
 	    : "=r" (__v) : : "cc");	\
 	__v;				\
  })
 
 #define fmxr(_vfp_,_var_)		\
-	asm(".fpu	vfpv2\n"	\
+	asm volatile(".fpu vfpv2\n"	\
 	    "vmsr	" #_vfp_ ", %0"	\
 	   : : "r" (_var_) : "cc")
 
Signature · 7 lines
2.39.2


_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
Acked-by on v1 1/1
#1 lore raw
Quoted 14 lines · Ard Biesheuvel

From: Ard Biesheuvel <[email protected]>

Clang may reorder FP control register reads and writes, due to the fact that the inline asm() blocks in the read/write wrappers are not volatile qualified, and the compiler has no idea that these reads and writes may have side effects.

In particular, reads of FPSCR may generate an UNDEF exception if a floating point exception is pending, and the FP emulation code in VFP_bounce() explicitly clears FP exceptions temporarily in order to be able to perform the emulation on behalf of user space. This requires that the writes to FPEXC are never reordered with respect to accesses to other FP control registers, such as FPSCR.

Quoted 4 lines

So use asm volatile for both the read and the write helpers.

This seems reasonable to me based on my understanding of GCC's documentation. However, their documentation states "the compiler can move even volatile asm instructions relative to other code, including across jump instructions" and I feel like there was some discussion around this sentence in the past but I can't remember what the conclusion was, although I want to say Clang did not have the same behavior. Regardless:

I am just curious, how was this discovered or noticed? Was there a report I missed?

Quoted 28 lines
---
 arch/arm/vfp/vfpinstr.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
arch/arm/vfp/vfpinstr.h +2 -2
index 3c7938fd40aa..c4ac778e6fc9 100644
@@ -66,14 +66,14 @@
 
 #define fmrx(_vfp_) ({			\
 	u32 __v;			\
-	asm(".fpu	vfpv2\n"	\
+	asm volatile(".fpu vfpv2\n"	\
 	    "vmrs	%0, " #_vfp_	\
 	    : "=r" (__v) : : "cc");	\
 	__v;				\
  })
 
 #define fmxr(_vfp_,_var_)		\
-	asm(".fpu	vfpv2\n"	\
+	asm volatile(".fpu vfpv2\n"	\
 	    "vmsr	" #_vfp_ ", %0"	\
 	   : : "r" (_var_) : "cc")
 
Signature · 1 line
2.39.2

_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

#2 lore raw
Quoted 21 lines · Nathan Chancellor
Quoted 14 lines · Ard Biesheuvel

From: Ard Biesheuvel <[email protected]>

Clang may reorder FP control register reads and writes, due to the fact that the inline asm() blocks in the read/write wrappers are not volatile qualified, and the compiler has no idea that these reads and writes may have side effects.

In particular, reads of FPSCR may generate an UNDEF exception if a floating point exception is pending, and the FP emulation code in VFP_bounce() explicitly clears FP exceptions temporarily in order to be able to perform the emulation on behalf of user space. This requires that the writes to FPEXC are never reordered with respect to accesses to other FP control registers, such as FPSCR.

Quoted 4 lines

So use asm volatile for both the read and the write helpers.

Quoted 7 lines

This seems reasonable to me based on my understanding of GCC's documentation. However, their documentation states "the compiler can move even volatile asm instructions relative to other code, including across jump instructions" and I feel like there was some discussion around this sentence in the past but I can't remember what the conclusion was, although I want to say Clang did not have the same behavior.

The only thing that matters here is whether two asm blocks are emitted in a different order than they appear in the program, and I would be very surprised if volatile permits that. Otherwise, we might introduce a fake input dependency or a memory clobber instead.

Quoted 4 lines

Regardless:

Thanks.

Quoted 3 lines

I am just curious, how was this discovered or noticed? Was there a report I missed?

I noticed this when building a recent kernel for the original Raspberry Pi, which is ARMv6 not ARMv7, and has a VFP which partially relies on emulation. On more recent cores, we never hit the issue because emulation is never needed. On older cores, there is no VFP so we never reach this code path either.

_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

#3 lore raw
Quoted 47 lines · Ard Biesheuvel
Quoted 21 lines · Nathan Chancellor
Quoted 14 lines · Ard Biesheuvel

From: Ard Biesheuvel <[email protected]>

Clang may reorder FP control register reads and writes, due to the fact that the inline asm() blocks in the read/write wrappers are not volatile qualified, and the compiler has no idea that these reads and writes may have side effects.

In particular, reads of FPSCR may generate an UNDEF exception if a floating point exception is pending, and the FP emulation code in VFP_bounce() explicitly clears FP exceptions temporarily in order to be able to perform the emulation on behalf of user space. This requires that the writes to FPEXC are never reordered with respect to accesses to other FP control registers, such as FPSCR.

Quoted 4 lines

So use asm volatile for both the read and the write helpers.

Quoted 7 lines

This seems reasonable to me based on my understanding of GCC's documentation. However, their documentation states "the compiler can move even volatile asm instructions relative to other code, including across jump instructions" and I feel like there was some discussion around this sentence in the past but I can't remember what the conclusion was, although I want to say Clang did not have the same behavior.

The only thing that matters here is whether two asm blocks are emitted in a different order than they appear in the program, and I would be very surprised if volatile permits that. Otherwise, we might introduce a fake input dependency or a memory clobber instead.

Quoted 4 lines

Regardless:

Thanks.

Quoted 3 lines

I am just curious, how was this discovered or noticed? Was there a report I missed?

Quoted 5 lines

I noticed this when building a recent kernel for the original Raspberry Pi, which is ARMv6 not ARMv7, and has a VFP which partially relies on emulation. On more recent cores, we never hit the issue because emulation is never needed. On older cores, there is no VFP so we never reach this code path either.

An alternative approach might be to do the following, which tricks the compiler into thinking fmxr might update *current, and fmrx might access it. Given that current == current_thread_info(), which is used all the time in the VFP code, it will already be available in a register, and so it doesn't require the compiler to generate any additional code.

arch/arm/vfp/vfpinstr.h +3 -3
@@ -68,14 +68,14 @@
        u32 __v;                        \
        asm(".fpu       vfpv2\n"        \
            "vmrs       %0, " #_vfp_    \
-           : "=r" (__v) : : "cc");     \
+           : "=r" (__v) : "Q" (*current)); \
        __v;                            \
  })
 
 #define fmxr(_vfp_,_var_)              \
        asm(".fpu       vfpv2\n"        \
-           "vmsr       " #_vfp_ ", %0" \
-          : : "r" (_var_) : "cc")
+           "vmsr       " #_vfp_ ", %1" \
+          : "=Q" (*current) : "r" (_var_))
 
 #else
 

_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

#4 lore raw
In reply to Ard Biesheuvel
Quoted 30 lines · Ard Biesheuvel
Quoted 21 lines · Nathan Chancellor
Quoted 14 lines · Ard Biesheuvel

From: Ard Biesheuvel <[email protected]>

Clang may reorder FP control register reads and writes, due to the fact that the inline asm() blocks in the read/write wrappers are not volatile qualified, and the compiler has no idea that these reads and writes may have side effects.

In particular, reads of FPSCR may generate an UNDEF exception if a floating point exception is pending, and the FP emulation code in VFP_bounce() explicitly clears FP exceptions temporarily in order to be able to perform the emulation on behalf of user space. This requires that the writes to FPEXC are never reordered with respect to accesses to other FP control registers, such as FPSCR.

Quoted 4 lines

So use asm volatile for both the read and the write helpers.

Quoted 7 lines

This seems reasonable to me based on my understanding of GCC's documentation. However, their documentation states "the compiler can move even volatile asm instructions relative to other code, including across jump instructions" and I feel like there was some discussion around this sentence in the past but I can't remember what the conclusion was, although I want to say Clang did not have the same behavior.

Quoted 4 lines

The only thing that matters here is whether two asm blocks are emitted in a different order than they appear in the program, and I would be very surprised if volatile permits that. Otherwise, we might introduce a fake input dependency or a memory clobber instead.

Yeah, I think it is reasonable to go with this approach and fall back to your follow up suggestion if this proves not to be robust enough.

Quoted 11 lines
Quoted 4 lines

Regardless:

Thanks.

Quoted 3 lines

I am just curious, how was this discovered or noticed? Was there a report I missed?

Quoted 5 lines

I noticed this when building a recent kernel for the original Raspberry Pi, which is ARMv6 not ARMv7, and has a VFP which partially relies on emulation. On more recent cores, we never hit the issue because emulation is never needed. On older cores, there is no VFP so we never reach this code path either.

Ah, good to know. I boot test -next on a Pi 3 with a 32-bit kernel, so that explains why I have not seen any issues. Just wanted to make sure I did not miss something :)

Cheers,
Nathan

_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel