Skip to contentSkip to conversation

RISC-V: Break load reservations during switch_to

PATCH In review Palmer Dabbelt 2 participants 3 messages Jun 17, 2019 – Jun 18, 2019 RISC-V

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 RISC-V: Break load reservations during switch_to

The comment describes why in detail. This was found because QEMU never gives up load reservations, the issue is unlikely to manifest on real hardware.

Thanks to Carlos Eduardo for finding the bug!

---
Changes since v2 <[email protected]>:

* REG_SC has the arguments the right way around.

Changes since v1 <[email protected]>:

* REG_SC is now defined as a helper macro, for any code that wants to SC a register-sized value.
* The explicit #ifdef to check that TASK_THREAD_RA_RA is 0 has been removed. Instead we rely on the assembler to catch non-zero SC offsets. I've tested this does actually work.

 arch/riscv/include/asm/asm.h |  1 +
 arch/riscv/kernel/entry.S    | 11 +++++++++++
 2 files changed, 12 insertions(+)
arch/riscv/include/asm/asm.h +1 -0
index 5ad4cb622bed..946b671f996c 100644
@@ -30,6 +30,7 @@
 
 #define REG_L		__REG_SEL(ld, lw)
 #define REG_S		__REG_SEL(sd, sw)
+#define REG_SC		__REG_SEL(sc.w, sc.d)
 #define SZREG		__REG_SEL(8, 4)
 #define LGREG		__REG_SEL(3, 2)
 
arch/riscv/kernel/entry.S +11 -0
index fd9b57c8b4ce..7e5d6e035b51 100644
@@ -330,6 +330,17 @@ ENTRY(__switch_to)
 	add   a3, a0, a4
 	add   a4, a1, a4
 	REG_S ra,  TASK_THREAD_RA_RA(a3)
+	/*
+	 * The Linux ABI allows programs to depend on load reservations being
+	 * broken on context switches, but the ISA doesn't require that the
+	 * hardware ever breaks a load reservation.  The only way to break a
+	 * load reservation is with a store conditional, so we emit one here.
+	 * Since nothing ever takes a load reservation on TASK_THREAD_RA_RA we
+	 * know this will always fail, but just to be on the safe side this
+	 * writes the same value that was unconditionally written by the
+	 * previous instruction.
+	 */
+	REG_SC x0, ra, TASK_THREAD_RA_RA(a3)
 	REG_S sp,  TASK_THREAD_SP_RA(a3)
 	REG_S s0,  TASK_THREAD_S0_RA(a3)
 	REG_S s1,  TASK_THREAD_S1_RA(a3)
Signature · 7 lines
2.21.0


_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv
#1 lore raw

Hi Palmer,

Quoted 8 lines · Palmer Dabbelt

The comment describes why in detail. This was found because QEMU never gives up load reservations, the issue is unlikely to manifest on real hardware.

Thanks to Carlos Eduardo for finding the bug!

This breaks the rv32_defconfig build:

AS arch/riscv/kernel/entry.o
arch/riscv/kernel/entry.S: Assembler messages:
arch/riscv/kernel/entry.S:343: Error: unrecognized opcode `sc.d x0,ra,0(a3)' make[1]: *** [scripts/Makefile.build:369: arch/riscv/kernel/entry.o] Error 1

Quoted 22 lines

---
Changes since v2 <[email protected]>:

* REG_SC has the arguments the right way around.

Changes since v1 <[email protected]>:

* REG_SC is now defined as a helper macro, for any code that wants to SC a register-sized value.
* The explicit #ifdef to check that TASK_THREAD_RA_RA is 0 has been removed. Instead we rely on the assembler to catch non-zero SC offsets. I've tested this does actually work.

 arch/riscv/include/asm/asm.h |  1 +
 arch/riscv/kernel/entry.S    | 11 +++++++++++
 2 files changed, 12 insertions(+)
arch/riscv/include/asm/asm.h +0 -0
index 5ad4cb622bed..946b671f996c 100644

arch/riscv/include/asm/asm.h line 33

Quoted 4 lines
arch/riscv/include/asm/asm.h +1 -0
@@ -30,6 +30,7 @@
 
 #define REG_L		__REG_SEL(ld, lw)
 #define REG_S		__REG_SEL(sd, sw)
+#define REG_SC		__REG_SEL(sc.w, sc.d)

I guess this should be __REG_SEL(sc.d, sc.w) ?

- Paul

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

#2 lore raw
Quoted 43 lines · Paul Walmsley

Hi Palmer,

Quoted 8 lines · Palmer Dabbelt

The comment describes why in detail. This was found because QEMU never gives up load reservations, the issue is unlikely to manifest on real hardware.

Thanks to Carlos Eduardo for finding the bug!

This breaks the rv32_defconfig build:

AS arch/riscv/kernel/entry.o
arch/riscv/kernel/entry.S: Assembler messages:
arch/riscv/kernel/entry.S:343: Error: unrecognized opcode `sc.d x0,ra,0(a3)' make[1]: *** [scripts/Makefile.build:369: arch/riscv/kernel/entry.o] Error 1

Quoted 23 lines

---
Changes since v2 <[email protected]>:

* REG_SC has the arguments the right way around.

Changes since v1 <[email protected]>:

* REG_SC is now defined as a helper macro, for any code that wants to SC a register-sized value.
* The explicit #ifdef to check that TASK_THREAD_RA_RA is 0 has been removed. Instead we rely on the assembler to catch non-zero SC offsets. I've tested this does actually work.

 arch/riscv/include/asm/asm.h |  1 +
 arch/riscv/kernel/entry.S    | 11 +++++++++++
 2 files changed, 12 insertions(+)
arch/riscv/include/asm/asm.h +0 -0
index 5ad4cb622bed..946b671f996c 100644
@@ -30,6 +30,7 @@
 
Quoted 5 lines

arch/riscv/include/asm/asm.h line 33

Quoted 3 lines
arch/riscv/include/asm/asm.h +1 -0
 #define REG_L		__REG_SEL(ld, lw)
 #define REG_S		__REG_SEL(sd, sw)
+#define REG_SC		__REG_SEL(sc.w, sc.d)

I guess this should be __REG_SEL(sc.d, sc.w) ?

Sorry about that, I forget to actually include the fixup in the patch. There's also some debate about the comment's correctness, so I'll submit a v4.

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