Skip to contentSkip to conversation

xtensa: Implement "current_stack_pointer"

PATCH In review Kees Cook 2 participants 5 messages Feb 24, 2022 – Feb 25, 2022 Tensilica Xtensa port (xtensa)

lore
Share & tools
lore

Kees Cook

author
#0 lore raw

PATCH 1/1 xtensa: Implement "current_stack_pointer"

To follow the existing per-arch conventions replace open-coded uses of asm "sp" as "current_stack_pointer". This will let it be used in non-arch places (like HARDENED_USERCOPY).

Cc Chris Zankel <[email protected]>
Cc Max Filippov <[email protected]>
Cc Marc Zyngier <[email protected]>
---
 arch/xtensa/Kconfig                  | 1 +
 arch/xtensa/include/asm/current.h    | 2 ++
 arch/xtensa/include/asm/stacktrace.h | 2 +-
 arch/xtensa/kernel/irq.c             | 3 +--
 4 files changed, 5 insertions(+), 3 deletions(-)
arch/xtensa/Kconfig +1 -0
index 8ac599aa6d99..887432327613 100644
@@ -3,6 +3,7 @@ config XTENSA
 	def_bool y
 	select ARCH_32BIT_OFF_T
 	select ARCH_HAS_BINFMT_FLAT if !MMU
+	select ARCH_HAS_CURRENT_STACK_POINTER
 	select ARCH_HAS_DMA_PREP_COHERENT if MMU
 	select ARCH_HAS_SYNC_DMA_FOR_CPU if MMU
 	select ARCH_HAS_SYNC_DMA_FOR_DEVICE if MMU
arch/xtensa/include/asm/current.h +2 -0
index 5d98a7ad4251..08010dbf5e09 100644
@@ -26,6 +26,8 @@ static inline struct task_struct *get_current(void)
 
 #define current get_current()
 
+register unsigned long current_stack_pointer __asm__("a1");
+
 #else
 
 #define GET_CURRENT(reg,sp)		\
arch/xtensa/include/asm/stacktrace.h +1 -1
index fe06e8ed162b..4d84fd6bd43c 100644
@@ -22,7 +22,7 @@ static __always_inline unsigned long *stack_pointer(struct task_struct *task)
 	unsigned long *sp;
 
 	if (!task || task == current)
-		__asm__ __volatile__ ("mov %0, a1\n" : "=a"(sp));
+		sp = current_stack_pointer;
 	else
 		sp = (unsigned long *)task->thread.sp;
 
arch/xtensa/kernel/irq.c +1 -2
index 15051a8a1539..529fe9245821 100644
@@ -36,9 +36,8 @@ asmlinkage void do_IRQ(int hwirq, struct pt_regs *regs)
 #ifdef CONFIG_DEBUG_STACKOVERFLOW
 	/* Debugging check for stack overflow: is there less than 1KB free? */
 	{
-		unsigned long sp;
+		unsigned long sp = current_stack_pointer;
 
-		__asm__ __volatile__ ("mov %0, a1\n" : "=a" (sp));
 		sp &= THREAD_SIZE - 1;
 
 		if (unlikely(sp < (sizeof(thread_info) + 1024)))
Signature · 1 line
2.30.2
maintainer Acked-by on v1 1/1
#1 lore raw
Quoted 10 lines · Kees Cook

To follow the existing per-arch conventions replace open-coded uses of asm "sp" as "current_stack_pointer". This will let it be used in non-arch places (like HARDENED_USERCOPY).

Cc Chris Zankel <[email protected]>
Cc Max Filippov <[email protected]>
Cc Marc Zyngier <[email protected]>
Quoted 6 lines
---
 arch/xtensa/Kconfig                  | 1 +
 arch/xtensa/include/asm/current.h    | 2 ++
 arch/xtensa/include/asm/stacktrace.h | 2 +-
 arch/xtensa/kernel/irq.c             | 3 +--
 4 files changed, 5 insertions(+), 3 deletions(-)
Signature · 2 lines
Thanks.
-- Max
author
#2 lore raw
Quoted 18 lines · Max Filippov
Quoted 10 lines · Kees Cook

To follow the existing per-arch conventions replace open-coded uses of asm "sp" as "current_stack_pointer". This will let it be used in non-arch places (like HARDENED_USERCOPY).

Cc Chris Zankel <[email protected]>
Cc Max Filippov <[email protected]>
Cc Marc Zyngier <[email protected]>
Quoted 6 lines
---
 arch/xtensa/Kconfig                  | 1 +
 arch/xtensa/include/asm/current.h    | 2 ++
 arch/xtensa/include/asm/stacktrace.h | 2 +-
 arch/xtensa/kernel/irq.c             | 3 +--
 4 files changed, 5 insertions(+), 3 deletions(-)
Quoted 1 line

Thanks! And apologies, my cross-compiler tricked me into thinking this patch compiled without problems. It did, however. I've change the patch slightly to deal with the needed casts:

arch/xtensa/include/asm/stacktrace.h +4 -4
index fe06e8ed162b..a85e785a6288 100644
@@ -19,14 +19,14 @@ struct stackframe {
 
 static __always_inline unsigned long *stack_pointer(struct task_struct *task)
 {
-	unsigned long *sp;
+	unsigned long sp;
 
 	if (!task || task == current)
-		__asm__ __volatile__ ("mov %0, a1\n" : "=a"(sp));
+		sp = current_stack_pointer;
 	else
-		sp = (unsigned long *)task->thread.sp;
+		sp = task->thread.sp;
 
-	return sp;
+	return (unsigned long *)sp;
 }
 
 void walk_stackframe(unsigned long *sp,

Shall I send a v2, or just carry this fix in my tree?

Sorry for the glitch!

-Kees

Signature · 1 line
Kees Cook
maintainer
#3 lore raw
Quoted 50 lines · Kees Cook
Quoted 18 lines · Max Filippov
Quoted 10 lines · Kees Cook

To follow the existing per-arch conventions replace open-coded uses of asm "sp" as "current_stack_pointer". This will let it be used in non-arch places (like HARDENED_USERCOPY).

Cc Chris Zankel <[email protected]>
Cc Max Filippov <[email protected]>
Cc Marc Zyngier <[email protected]>
Quoted 6 lines
---
 arch/xtensa/Kconfig                  | 1 +
 arch/xtensa/include/asm/current.h    | 2 ++
 arch/xtensa/include/asm/stacktrace.h | 2 +-
 arch/xtensa/kernel/irq.c             | 3 +--
 4 files changed, 5 insertions(+), 3 deletions(-)
Quoted 1 line

Thanks! And apologies, my cross-compiler tricked me into thinking this patch compiled without problems. It did, however. I've change the patch slightly to deal with the needed casts:

arch/xtensa/include/asm/stacktrace.h +4 -4
index fe06e8ed162b..a85e785a6288 100644
@@ -19,14 +19,14 @@ struct stackframe {
 
 static __always_inline unsigned long *stack_pointer(struct task_struct *task)
 {
-       unsigned long *sp;
+       unsigned long sp;
 
        if (!task || task == current)
-               __asm__ __volatile__ ("mov %0, a1\n" : "=a"(sp));
+               sp = current_stack_pointer;
        else
-               sp = (unsigned long *)task->thread.sp;
+               sp = task->thread.sp;
 
-       return sp;
+       return (unsigned long *)sp;
 }
 
 void walk_stackframe(unsigned long *sp,
Quoted 1 line

Shall I send a v2, or just carry this fix in my tree?

This additional change looks good to me, if you could fold it into the original patch that'd be perfect. But separate patch would also work.

Signature · 2 lines
Thanks.
-- Max
author
#4 lore raw
Quoted 49 lines · Max Filippov
Quoted 48 lines · Kees Cook
Quoted 18 lines · Max Filippov
Quoted 10 lines · Kees Cook

To follow the existing per-arch conventions replace open-coded uses of asm "sp" as "current_stack_pointer". This will let it be used in non-arch places (like HARDENED_USERCOPY).

Cc Chris Zankel <[email protected]>
Cc Max Filippov <[email protected]>
Cc Marc Zyngier <[email protected]>
Quoted 6 lines
---
 arch/xtensa/Kconfig                  | 1 +
 arch/xtensa/include/asm/current.h    | 2 ++
 arch/xtensa/include/asm/stacktrace.h | 2 +-
 arch/xtensa/kernel/irq.c             | 3 +--
 4 files changed, 5 insertions(+), 3 deletions(-)
Quoted 1 line

Thanks! And apologies, my cross-compiler tricked me into thinking this patch compiled without problems. It did, however. I've change the patch slightly to deal with the needed casts:

arch/xtensa/include/asm/stacktrace.h +4 -4
index fe06e8ed162b..a85e785a6288 100644
@@ -19,14 +19,14 @@ struct stackframe {
 
 static __always_inline unsigned long *stack_pointer(struct task_struct *task)
 {
-       unsigned long *sp;
+       unsigned long sp;
 
        if (!task || task == current)
-               __asm__ __volatile__ ("mov %0, a1\n" : "=a"(sp));
+               sp = current_stack_pointer;
        else
-               sp = (unsigned long *)task->thread.sp;
+               sp = task->thread.sp;
 
-       return sp;
+       return (unsigned long *)sp;
 }
 
Quoted 7 lines
Quoted 3 lines
arch/xtensa/include/asm/stacktrace.h +0 -0
 void walk_stackframe(unsigned long *sp,

Shall I send a v2, or just carry this fix in my tree?

This additional change looks good to me, if you could fold it into the original patch that'd be perfect. But separate patch would also work.

Thanks!

Signature · 1 line
Kees Cook