Skip to contentSkip to conversation

MIPS: Alchemy: fix wait function

PATCH Superseded Manuel Lauss 3 participants 5 messages May 23, 2013 – Jun 10, 2013 MIPS

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 MIPS: Alchemy: fix wait function

Only an interrupt can wake the core from 'wait', enable interrupts locally before executing 'wait'.

---
Ralf made me aware of the race in between enabling interrupts and entering wait. While this patch does not eliminate it, it shrinks it to 1 instruction. It's not perfect, but lets Alchemy boot until a more sophisticated solution (like __r4k_wait) can be implemented without having to duplicate the interrupt exception handler.

 arch/mips/kernel/idle.c | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)
arch/mips/kernel/idle.c +6 -5
index 3b09b88..1d37b4b 100644
@@ -93,9 +93,9 @@ static void rm7k_wait_irqoff(void)
 }
 
 /*
- * The Au1xxx wait is available only if using 32khz counter or
- * external timer source, but specifically not CP0 Counter.
- * alchemy/common/time.c may override cpu_wait!
+ * Au1 'wait' is only useful when the 32kHz counter is used as timer,
+ * since coreclock (and the cp0 counter) stops upon executing it. Only an
+ * interrupt can wake it, so they must be enabled before entering idle modes.
  */
 static void au1k_wait(void)
 {
@@ -103,8 +103,10 @@ static void au1k_wait(void)
 	"	.set	mips3			\n"
 	"	cache	0x14, 0(%0)		\n"
 	"	cache	0x14, 32(%0)		\n"
+	"	mfc0	$8, $12			\n"
+	"	ori	$8, $8, 1		\n"
 	"	sync				\n"
-	"	nop				\n"
+	"	mtc0	$8, $12			\n"	/* enable irqs */
 	"	wait				\n"
 	"	nop				\n"
 	"	nop				\n"
@@ -112,7 +114,6 @@ static void au1k_wait(void)
 	"	nop				\n"
 	"	.set	mips0			\n"
 	: : "r" (au1k_wait));
-	local_irq_enable();
 }
 
 static int __initdata nowait;
Signature · 1 line
1.8.2.1
#1 lore raw
Quoted 10 lines · Manuel Lauss

Only an interrupt can wake the core from 'wait', enable interrupts locally before executing 'wait'.

---
Ralf made me aware of the race in between enabling interrupts and entering wait. While this patch does not eliminate it, it shrinks it to 1 instruction. It's not perfect, but lets Alchemy boot until a more sophisticated solution (like __r4k_wait) can be implemented without having to duplicate the interrupt exception handler.

I suggest double-checking with Alchemy documentation, but I doubt there
is a race here, the write-back pipeline stage of MTC0 should overlap with the execution stage of WAIT, so assuming interrupts were previously disabled there should be no window between setting CP0.Status.IE and executing WAIT that would permit an interrrupt exception to be taken.

There is a bug in your change however.

Quoted 33 lines
 arch/mips/kernel/idle.c | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)
arch/mips/kernel/idle.c +6 -4
index 3b09b88..1d37b4b 100644
@@ -93,9 +93,9 @@ static void rm7k_wait_irqoff(void)
 }
 
 /*
- * The Au1xxx wait is available only if using 32khz counter or
- * external timer source, but specifically not CP0 Counter.
- * alchemy/common/time.c may override cpu_wait!
+ * Au1 'wait' is only useful when the 32kHz counter is used as timer,
+ * since coreclock (and the cp0 counter) stops upon executing it. Only an
+ * interrupt can wake it, so they must be enabled before entering idle modes.
  */
 static void au1k_wait(void)
 {
@@ -103,8 +103,10 @@ static void au1k_wait(void)
 	"	.set	mips3			\n"
 	"	cache	0x14, 0(%0)		\n"
 	"	cache	0x14, 32(%0)		\n"
+	"	mfc0	$8, $12			\n"
+	"	ori	$8, $8, 1		\n"
 	"	sync				\n"
-	"	nop				\n"
+	"	mtc0	$8, $12			\n"	/* enable irqs */
 	"	wait				\n"
 	"	nop				\n"
 	"	nop				\n"

arch/mips/kernel/idle.c static void au1k_wait(void) · line 119

Quoted 7 lines
arch/mips/kernel/idle.c +0 -1
@@ -112,7 +114,6 @@ static void au1k_wait(void)
 	"	nop				\n"
 	"	.set	mips0			\n"
 	: : "r" (au1k_wait));
-	local_irq_enable();
 }
 
 static int __initdata nowait;

You can't just take $8 away under the feet of GCC without telling the
compiler, it may be storing some data there across the asm. Rather than picking an arbitrary register as a clobber I suggest using an output register constraint associated with a scratch variable (depending on register usage GCC may possibly be able to reuse the same register both for input and for output).

Maciej

#2 lore raw
Quoted 13 lines · Maciej W. Rozycki
Quoted 10 lines · Manuel Lauss

Only an interrupt can wake the core from 'wait', enable interrupts locally before executing 'wait'.

---
Ralf made me aware of the race in between enabling interrupts and entering wait. While this patch does not eliminate it, it shrinks it to 1 instruction. It's not perfect, but lets Alchemy boot until a more sophisticated solution (like __r4k_wait) can be implemented without having to duplicate the interrupt exception handler.

Quoted 5 lines

I suggest double-checking with Alchemy documentation, but I doubt there
is a race here, the write-back pipeline stage of MTC0 should overlap with the execution stage of WAIT, so assuming interrupts were previously disabled there should be no window between setting CP0.Status.IE and executing WAIT that would permit an interrrupt exception to be taken.

That was my thinking as well. The Alchemy manuals (at least the ones I have) don't specify the stage where c0 writes complete or 'wait' executes, but the wording on how and when exceptions are raised makes me believe the scheme above is race-free.

Quoted 1 line

There is a bug in your change however.

[...]

Quoted 6 lines

You can't just take $8 away under the feet of GCC without telling the
compiler, it may be storing some data there across the asm. Rather than picking an arbitrary register as a clobber I suggest using an output register constraint associated with a scratch variable (depending on register usage GCC may possibly be able to reuse the same register both for input and for output).

I've fixed that, and sent out a new patch.

Thank you very much!
Manuel

#3 lore raw
In reply to Manuel Lauss
Quoted 10 lines · Manuel Lauss

Only an interrupt can wake the core from 'wait', enable interrupts locally before executing 'wait'.

---
Ralf made me aware of the race in between enabling interrupts and entering wait. While this patch does not eliminate it, it shrinks it to 1 instruction. It's not perfect, but lets Alchemy boot until a more sophisticated solution (like __r4k_wait) can be implemented without having to duplicate the interrupt exception handler.

It doesn't shrink it - interrupts will be pending from the time they were disabled to the point where they get re-enabled. That can be quite a few cycles and so the likelyhood for hitting the race is not that low.

Ralf

#4 lore raw
Quoted 11 lines · Ralf Baechle
Quoted 10 lines

Only an interrupt can wake the core from 'wait', enable interrupts locally before executing 'wait'.

---
Ralf made me aware of the race in between enabling interrupts and entering wait. While this patch does not eliminate it, it shrinks it to 1 instruction. It's not perfect, but lets Alchemy boot until a more sophisticated solution (like __r4k_wait) can be implemented without having to duplicate the interrupt exception handler.

Quoted 3 lines

It doesn't shrink it - interrupts will be pending from the time they were disabled to the point where they get re-enabled. That can be quite a few cycles and so the likelyhood for hitting the race is not that low.

My understanding has been the race is that an interrupt can be taken
between the MTC0 instruction used to enable interrupts and the WAIT instruction and the handler can disable interrupts back. That's not going to be a problem if the interrupt is taken "a few cycles" later, because by then WAIT will have already been executed.

Have I been missing something here?

Maciej