Skip to content

Commit 1885660

Browse files
RISC-V: Clear load reservations while restoring hart contexts
This is almost entirely a comment. The bug is unlikely to manifest on existing hardware because there is a timeout on load reservations, but manifests on QEMU because there is no timeout. Signed-off-by: Palmer Dabbelt <[email protected]> Reviewed-by: Christoph Hellwig <[email protected]> Signed-off-by: Paul Walmsley <[email protected]>
1 parent 54ecb8f commit 1885660

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

arch/riscv/include/asm/asm.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
#define REG_L __REG_SEL(ld, lw)
2424
#define REG_S __REG_SEL(sd, sw)
25+
#define REG_SC __REG_SEL(sc.d, sc.w)
2526
#define SZREG __REG_SEL(8, 4)
2627
#define LGREG __REG_SEL(3, 2)
2728

arch/riscv/kernel/entry.S

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,26 @@ _save_context:
9898
*/
9999
.macro RESTORE_ALL
100100
REG_L a0, PT_SSTATUS(sp)
101-
REG_L a2, PT_SEPC(sp)
101+
/*
102+
* The current load reservation is effectively part of the processor's
103+
* state, in the sense that load reservations cannot be shared between
104+
* different hart contexts. We can't actually save and restore a load
105+
* reservation, so instead here we clear any existing reservation --
106+
* it's always legal for implementations to clear load reservations at
107+
* any point (as long as the forward progress guarantee is kept, but
108+
* we'll ignore that here).
109+
*
110+
* Dangling load reservations can be the result of taking a trap in the
111+
* middle of an LR/SC sequence, but can also be the result of a taken
112+
* forward branch around an SC -- which is how we implement CAS. As a
113+
* result we need to clear reservations between the last CAS and the
114+
* jump back to the new context. While it is unlikely the store
115+
* completes, implementations are allowed to expand reservations to be
116+
* arbitrarily large.
117+
*/
118+
REG_L a2, PT_SEPC(sp)
119+
REG_SC x0, a2, PT_SEPC(sp)
120+
102121
csrw CSR_SSTATUS, a0
103122
csrw CSR_SEPC, a2
104123

0 commit comments

Comments
 (0)