Skip to contentSkip to conversation

MIPS: Fix ejtag handler on SMP

PATCH Superseded [email protected] 2 participants 2 messages Apr 3, 2018 – Jun 1, 2018 MIPS

v2 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 MIPS: Fix ejtag handler on SMP

From: Heiher <[email protected]>

On SMP systems, the shared ejtag debug buffer may be overwritten by other cores, because every cores can generate ejtag exception at same time.

Unfortunately, in that context, it's difficult to relax more registers to access per cpu buffers. so use ll/sc to serialize the access.

---
 arch/mips/kernel/genex.S | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)
arch/mips/kernel/genex.S +18 -0
index 37b9383eacd3..1af8c83835ef 100644
@@ -354,6 +354,16 @@ NESTED(ejtag_debug_handler, PT_SIZE, sp)
 	sll	k0, k0, 30	# Check for SDBBP.
 	bgez	k0, ejtag_return
 
+#ifdef CONFIG_SMP
+	PTR_LA	k0, ejtag_debug_buffer
+1:	ll	k0, LONGSIZE(k0)
+	bnez	k0, 1b
+	PTR_LA	k0, ejtag_debug_buffer
+	sc	k0, LONGSIZE(k0)
+	beqz	k0, 1b
+	sync
+#endif
+
 	PTR_LA	k0, ejtag_debug_buffer
 	LONG_S	k1, 0(k0)
 	SAVE_ALL
@@ -363,7 +373,12 @@ NESTED(ejtag_debug_handler, PT_SIZE, sp)
 	PTR_LA	k0, ejtag_debug_buffer
 	LONG_L	k1, 0(k0)
 
+#ifdef CONFIG_SMP
+	sw	zero, LONGSIZE(k0)
+#endif
+
 ejtag_return:
+	back_to_back_c0_hazard
 	MFC0	k0, CP0_DESAVE
 	.set	mips32
 	deret
@@ -377,6 +392,9 @@ ejtag_return:
 	.data
 EXPORT(ejtag_debug_buffer)
 	.fill	LONGSIZE
+#ifdef CONFIG_SMP
+	.fill	LONGSIZE
+#endif
 	.previous
 
 	__INIT
Signature · 1 line
2.16.3
#1 lore raw

Hi Heiher,

Quoted 19 lines · On Tue, Apr 03, 2018 at 07:41:02PM +0800, [email protected] wrote:

From: Heiher <[email protected]>

On SMP systems, the shared ejtag debug buffer may be overwritten by other cores, because every cores can generate ejtag exception at same time.

Unfortunately, in that context, it's difficult to relax more registers to access per cpu buffers. so use ll/sc to serialize the access.

---
 arch/mips/kernel/genex.S | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)
arch/mips/kernel/genex.S +0 -0
index 37b9383eacd3..1af8c83835ef 100644

arch/mips/kernel/genex.S NESTED(ejtag_debug_handler, PT_SIZE, sp) · line 363

Quoted 10 lines
arch/mips/kernel/genex.S +7 -0
@@ -354,6 +354,16 @@ NESTED(ejtag_debug_handler, PT_SIZE, sp)
 	sll	k0, k0, 30	# Check for SDBBP.
 	bgez	k0, ejtag_return
 
+#ifdef CONFIG_SMP
+	PTR_LA	k0, ejtag_debug_buffer
+1:	ll	k0, LONGSIZE(k0)
+	bnez	k0, 1b
+	PTR_LA	k0, ejtag_debug_buffer
+	sc	k0, LONGSIZE(k0)
+	beqz	k0, 1b

If this branch is taken then we're in trouble k0 is now 0x0 so the ll will try to load from address 0+LONGSIZE. So we'll take a TLBL exception & panic.

The easiest solution would be to move the 1 label a line earlier to the PTR_LA, so that we repeat the PTR_LA. I like the clever trick that the data returned from the ll should either be 0 or the address of ejtag_debug_buffer, which meant the bnez a little earlier wouldn't have this problem, and I see that trivially moving the 1 label would make that case redundantly repeat the PTR_LA too but in practice this should be rarely executed so the extra couple of instructions ought not to matter.

I think if we're being really correct this should take into account R10000_LLSC_WAR too, and use beqzl when that's set (see asm/cmpxchg.h).

arch/mips/kernel/genex.S NESTED(ejtag_debug_handler, PT_SIZE, sp) · line 364

Quoted 1 line
arch/mips/kernel/genex.S +1 -0
+	sync

This is being used as the equivalent of an smp_llsc_mb(), so it would be good to comment to that effect & clarify that we need an ordering barrier to ensure that writes to ejtag_debug_buffer can't overtake the acquisition of this lock.

The sync could also be omitted if either of CONFIG_WEAK_REORDERING_BEYOND_LLSC or CONFIG_SMP aren't enabled, but that might just clutter up the rarely executed code for little gain so I won't insist.

Quoted 19 lines
arch/mips/kernel/genex.S +7 -0
+#endif
+
 	PTR_LA	k0, ejtag_debug_buffer
 	LONG_S	k1, 0(k0)
 	SAVE_ALL
@@ -363,7 +373,12 @@ NESTED(ejtag_debug_handler, PT_SIZE, sp)
 	PTR_LA	k0, ejtag_debug_buffer
 	LONG_L	k1, 0(k0)
 
+#ifdef CONFIG_SMP
+	sw	zero, LONGSIZE(k0)
+#endif
+
 ejtag_return:
+	back_to_back_c0_hazard
 	MFC0	k0, CP0_DESAVE
 	.set	mips32
 	deret

arch/mips/kernel/genex.S ejtag_return: · line 395

Quoted 4 lines
arch/mips/kernel/genex.S +1 -0
@@ -377,6 +392,9 @@ ejtag_return:
 	.data
 EXPORT(ejtag_debug_buffer)
 	.fill	LONGSIZE
+#ifdef CONFIG_SMP

I think it would make sense to EXPORT(ejtag_debug_buffer_spinlock) or something to that effect here, and use that symbol when accessing the lock above. That would make it easier to see at a glance that this is an open-coded spinlock.

Having said all of that, I'm a little concerned about the approach in general because it relies on CPUs always returning from ejtag_exception_handler() to release the lock. If a CPU doesn't return from ejtag_exception_handler() then any other CPUs that take an EJTAG exception will hang silently. In practice ejtag_exception_handler() is currently quite simple so we probably will always return, so I'd consider this patch an improvement on what we have now once the comments above are addressed.

One obvious alternative would be to make ejtag_debug_buffer per-CPU, or probably more easily making it an array of NR_CPUS length. It would be tricky to access though until MIPSr6, where AUI would help, or nanoMIPS where an ADDIU[48] could encode the whole address of ejtag_debug_buffer to add to the CPU offset.

Another option would be to use a KScratch register where they're present, but pre-r6 we usually can't guarantee they exist so that would complicate things again.

Thanks,
Paul

Quoted 9 lines
arch/mips/kernel/genex.S +2 -0
+	.fill	LONGSIZE
+#endif
 	.previous
 
 	__INIT
Signature · 1 line
2.16.3