Skip to contentSkip to conversation

RISC-V: Align the shadow stack

PATCH Applied Palmer Dabbelt 5 participants 11 messages Nov 30, 2022 – Dec 13, 2022 RISC-V

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 RISC-V: Align the shadow stack

The standard RISC-V ABIs all require 16-byte stack alignment. We're only calling that one function on the shadow stack so I doubt it'd result in a real issue, but might as well keep this lined up.

Fixes 31da94c25aea ("riscv: add VMAP_STACK overflow detection")
---
 arch/riscv/kernel/traps.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/riscv/kernel/traps.c +1 -1
index be54ccea8c47..acdfcacd7e57 100644
@@ -206,7 +206,7 @@ static DEFINE_PER_CPU(unsigned long [OVERFLOW_STACK_SIZE/sizeof(long)],
  * shadow stack, handled_ kernel_ stack_ overflow(in kernel/entry.S) is used
  * to get per-cpu overflow stack(get_overflow_stack).
  */
-long shadow_stack[SHADOW_OVERFLOW_STACK_SIZE/sizeof(long)];
+long shadow_stack[SHADOW_OVERFLOW_STACK_SIZE/sizeof(long)] __aligned(16);
 asmlinkage unsigned long get_overflow_stack(void)
 {
 	return (unsigned long)this_cpu_ptr(overflow_stack) +
Signature · 1 line
2.38.1
#1 lore raw

PATCH 2/2 RISC-V: Add some comments about the shadow and overflow stacks

It took me a while to page all this back in when trying to review the recent spin_shadow_stack, so I figured I'd just write up some comments.

---
 arch/riscv/kernel/traps.c | 20 +++++++++++++-------
 1 file changed, 13 insertions(+), 7 deletions(-)
arch/riscv/kernel/traps.c +13 -7
index acdfcacd7e57..336d4aadadb1 100644
@@ -200,18 +200,18 @@ void __init trap_init(void)
 }
 
 #ifdef CONFIG_VMAP_STACK
+/*
+ * Extra stack space that allows us to provide panic messages when the kernel
+ * has overflowed its stack.
+ */
 static DEFINE_PER_CPU(unsigned long [OVERFLOW_STACK_SIZE/sizeof(long)],
 		overflow_stack)__aligned(16);
 /*
- * shadow stack, handled_ kernel_ stack_ overflow(in kernel/entry.S) is used
- * to get per-cpu overflow stack(get_overflow_stack).
+ * A temporary stack for use by handle_kernel_stack_overflow.  This is used so
+ * we can call into C code to get the per-hart overflow stack.  Usage of this
+ * stack must be protected by spin_shadow_stack.
  */
 long shadow_stack[SHADOW_OVERFLOW_STACK_SIZE/sizeof(long)] __aligned(16);
-asmlinkage unsigned long get_overflow_stack(void)
-{
-	return (unsigned long)this_cpu_ptr(overflow_stack) +
-		OVERFLOW_STACK_SIZE;
-}
 
 /*
  * A pseudo spinlock to protect the shadow stack from being used by multiple
@@ -222,6 +222,12 @@ asmlinkage unsigned long get_overflow_stack(void)
  */
 unsigned long spin_shadow_stack;
 
+asmlinkage unsigned long get_overflow_stack(void)
+{
+	return (unsigned long)this_cpu_ptr(overflow_stack) +
+		OVERFLOW_STACK_SIZE;
+}
+
 asmlinkage void handle_bad_stack(struct pt_regs *regs)
 {
 	unsigned long tsk_stk = (unsigned long)current->stack;
Signature · 1 line
2.38.1
Reviewed-by on v1 2/2
#2 lore raw

Re: [PATCH 2/2] RISC-V: Add some comments about the shadow and overflow stacks

Quoted 19 lines · Palmer Dabbelt

It took me a while to page all this back in when trying to review the recent spin_shadow_stack, so I figured I'd just write up some comments.

---
 arch/riscv/kernel/traps.c | 20 +++++++++++++-------
 1 file changed, 13 insertions(+), 7 deletions(-)
arch/riscv/kernel/traps.c +2 -0
index acdfcacd7e57..336d4aadadb1 100644
@@ -200,18 +200,18 @@ void __init trap_init(void)
 }
 
 #ifdef CONFIG_VMAP_STACK
+/*
+ * Extra stack space that allows us to provide panic messages when the kernel

arch/riscv/kernel/traps.c void __init trap_init(void) · line 212

Quoted 10 lines
arch/riscv/kernel/traps.c +5 -2
+ * has overflowed its stack.
+ */
 static DEFINE_PER_CPU(unsigned long [OVERFLOW_STACK_SIZE/sizeof(long)],
                overflow_stack)__aligned(16);
 /*
- * shadow stack, handled_ kernel_ stack_ overflow(in kernel/entry.S) is used
- * to get per-cpu overflow stack(get_overflow_stack).
+ * A temporary stack for use by handle_kernel_stack_overflow.  This is used so
+ * we can call into C code to get the per-hart overflow stack.  Usage of this
+ * stack must be protected by spin_shadow_stack.
Quoted 26 lines
arch/riscv/kernel/traps.c +6 -6
  */
 long shadow_stack[SHADOW_OVERFLOW_STACK_SIZE/sizeof(long)] __aligned(16);
-asmlinkage unsigned long get_overflow_stack(void)
-{
-       return (unsigned long)this_cpu_ptr(overflow_stack) +
-               OVERFLOW_STACK_SIZE;
-}
 
 /*
  * A pseudo spinlock to protect the shadow stack from being used by multiple
@@ -222,6 +222,12 @@ asmlinkage unsigned long get_overflow_stack(void)
  */
 unsigned long spin_shadow_stack;
 
+asmlinkage unsigned long get_overflow_stack(void)
+{
+       return (unsigned long)this_cpu_ptr(overflow_stack) +
+               OVERFLOW_STACK_SIZE;
+}
+
 asmlinkage void handle_bad_stack(struct pt_regs *regs)
 {
        unsigned long tsk_stk = (unsigned long)current->stack;
--

2.38.1

Signature · 2 lines
Best Regards
 Guo Ren
Reviewed-by on v1 2/2
#3 lore raw

Re: [PATCH 2/2] RISC-V: Add some comments about the shadow and overflow stacks

In reply to Palmer Dabbelt
Quoted 4 lines · Palmer Dabbelt

It took me a while to page all this back in when trying to review the recent spin_shadow_stack, so I figured I'd just write up some comments.

Quoted 50 lines
---
 arch/riscv/kernel/traps.c | 20 +++++++++++++-------
 1 file changed, 13 insertions(+), 7 deletions(-)
arch/riscv/kernel/traps.c +13 -7
index acdfcacd7e57..336d4aadadb1 100644
@@ -200,18 +200,18 @@ void __init trap_init(void)
 }
 
 #ifdef CONFIG_VMAP_STACK
+/*
+ * Extra stack space that allows us to provide panic messages when the kernel
+ * has overflowed its stack.
+ */
 static DEFINE_PER_CPU(unsigned long [OVERFLOW_STACK_SIZE/sizeof(long)],
 		overflow_stack)__aligned(16);
 /*
- * shadow stack, handled_ kernel_ stack_ overflow(in kernel/entry.S) is used
- * to get per-cpu overflow stack(get_overflow_stack).
+ * A temporary stack for use by handle_kernel_stack_overflow.  This is used so
+ * we can call into C code to get the per-hart overflow stack.  Usage of this
+ * stack must be protected by spin_shadow_stack.
  */
 long shadow_stack[SHADOW_OVERFLOW_STACK_SIZE/sizeof(long)] __aligned(16);
-asmlinkage unsigned long get_overflow_stack(void)
-{
-	return (unsigned long)this_cpu_ptr(overflow_stack) +
-		OVERFLOW_STACK_SIZE;
-}
 
 /*
  * A pseudo spinlock to protect the shadow stack from being used by multiple
@@ -222,6 +222,12 @@ asmlinkage unsigned long get_overflow_stack(void)
  */
 unsigned long spin_shadow_stack;
 
+asmlinkage unsigned long get_overflow_stack(void)
+{
+	return (unsigned long)this_cpu_ptr(overflow_stack) +
+		OVERFLOW_STACK_SIZE;
+}
+
 asmlinkage void handle_bad_stack(struct pt_regs *regs)
 {
 	unsigned long tsk_stk = (unsigned long)current->stack;
Signature · 1 line
2.38.1
#4 lore raw
In reply to Palmer Dabbelt

Hi Palmer

Quoted 4 lines · Palmer Dabbelt

The standard RISC-V ABIs all require 16-byte stack alignment. We're only calling that one function on the shadow stack so I doubt it'd result in a real issue, but might as well keep this lined up.

Is 16-byte alignment required on rv32 as well ?

Quoted 28 lines
Fixes 31da94c25aea ("riscv: add VMAP_STACK overflow detection")
---
 arch/riscv/kernel/traps.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/riscv/kernel/traps.c +1 -2
index be54ccea8c47..acdfcacd7e57 100644
@@ -206,7 +206,7 @@ static DEFINE_PER_CPU(unsigned long [OVERFLOW_STACK_SIZE/sizeof(long)],
  * shadow stack, handled_ kernel_ stack_ overflow(in kernel/entry.S) is used
  * to get per-cpu overflow stack(get_overflow_stack).
  */
-long shadow_stack[SHADOW_OVERFLOW_STACK_SIZE/sizeof(long)];
+long shadow_stack[SHADOW_OVERFLOW_STACK_SIZE/sizeof(long)] __aligned(16);
 asmlinkage unsigned long get_overflow_stack(void)
 {
        return (unsigned long)this_cpu_ptr(overflow_stack) +
--

2.38.1

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

#5 lore raw
Quoted 9 lines · Khem Raj

Hi Palmer

Quoted 4 lines · Palmer Dabbelt

The standard RISC-V ABIs all require 16-byte stack alignment. We're only calling that one function on the shadow stack so I doubt it'd result in a real issue, but might as well keep this lined up.

Is 16-byte alignment required on rv32 as well ?

For the standard ABIs that's the case, it's so the Q extension can spill without aligning the stack. There's also at least a proposed embedded ABI that has just XLEN (32-bit on rv32) alignment, as the bigger stack alignment has an impact on some use cases.

Quoted 27 lines
Quoted 27 lines
Fixes 31da94c25aea ("riscv: add VMAP_STACK overflow detection")
---
 arch/riscv/kernel/traps.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/riscv/kernel/traps.c +1 -2
index be54ccea8c47..acdfcacd7e57 100644
@@ -206,7 +206,7 @@ static DEFINE_PER_CPU(unsigned long [OVERFLOW_STACK_SIZE/sizeof(long)],
  * shadow stack, handled_ kernel_ stack_ overflow(in kernel/entry.S) is used
  * to get per-cpu overflow stack(get_overflow_stack).
  */
-long shadow_stack[SHADOW_OVERFLOW_STACK_SIZE/sizeof(long)];
+long shadow_stack[SHADOW_OVERFLOW_STACK_SIZE/sizeof(long)] __aligned(16);
 asmlinkage unsigned long get_overflow_stack(void)
 {
        return (unsigned long)this_cpu_ptr(overflow_stack) +
--

2.38.1

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

#6 lore raw
Quoted 10 lines · Palmer Dabbelt
Quoted 8 lines · Khem Raj

Hi Palmer

Quoted 4 lines · Palmer Dabbelt

The standard RISC-V ABIs all require 16-byte stack alignment. We're only calling that one function on the shadow stack so I doubt it'd result in a real issue, but might as well keep this lined up.

Quoted 6 lines
Quoted 1 line

Is 16-byte alignment required on rv32 as well ?

For the standard ABIs that's the case, it's so the Q extension can spill without aligning the stack. There's also at least a proposed embedded ABI that has just XLEN (32-bit on rv32) alignment, as the bigger stack alignment has an impact on some use cases.

Thanks, so in this case 16byte will be valid for both rv64/rv32 here.

Quoted 28 lines
Quoted 28 lines
Quoted 28 lines
Fixes 31da94c25aea ("riscv: add VMAP_STACK overflow detection")
---
 arch/riscv/kernel/traps.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/riscv/kernel/traps.c +1 -2
index be54ccea8c47..acdfcacd7e57 100644
@@ -206,7 +206,7 @@ static DEFINE_PER_CPU(unsigned long [OVERFLOW_STACK_SIZE/sizeof(long)],
  * shadow stack, handled_ kernel_ stack_ overflow(in kernel/entry.S) is used
  * to get per-cpu overflow stack(get_overflow_stack).
  */
-long shadow_stack[SHADOW_OVERFLOW_STACK_SIZE/sizeof(long)];
+long shadow_stack[SHADOW_OVERFLOW_STACK_SIZE/sizeof(long)] __aligned(16);
 asmlinkage unsigned long get_overflow_stack(void)
 {
        return (unsigned long)this_cpu_ptr(overflow_stack) +
--

2.38.1

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

#7 lore raw
Quoted 11 lines · Khem Raj
Quoted 10 lines · Palmer Dabbelt
Quoted 8 lines · Khem Raj

Hi Palmer

Quoted 4 lines · Palmer Dabbelt

The standard RISC-V ABIs all require 16-byte stack alignment. We're only calling that one function on the shadow stack so I doubt it'd result in a real issue, but might as well keep this lined up.

Quoted 8 lines
Quoted 6 lines
Quoted 1 line

Is 16-byte alignment required on rv32 as well ?

For the standard ABIs that's the case, it's so the Q extension can spill without aligning the stack. There's also at least a proposed embedded ABI that has just XLEN (32-bit on rv32) alignment, as the bigger stack alignment has an impact on some use cases.

Thanks, so in this case 16byte will be valid for both rv64/rv32 here.

Yes, though the long-alignment wouldn't break anything because we don't have Q support and we're just calling that one function -- it's not like the compiler is actively checking for 16-byte alignment or anything, it's just assuming it.

Still best to keep things to the spec where we can, though.

Quoted 23 lines
Quoted 23 lines
Quoted 23 lines
Quoted 23 lines
Fixes 31da94c25aea ("riscv: add VMAP_STACK overflow detection")
---
 arch/riscv/kernel/traps.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/riscv/kernel/traps.c +1 -2
index be54ccea8c47..acdfcacd7e57 100644
@@ -206,7 +206,7 @@ static DEFINE_PER_CPU(unsigned long [OVERFLOW_STACK_SIZE/sizeof(long)],
  * shadow stack, handled_ kernel_ stack_ overflow(in kernel/entry.S) is used
  * to get per-cpu overflow stack(get_overflow_stack).
  */
-long shadow_stack[SHADOW_OVERFLOW_STACK_SIZE/sizeof(long)];
+long shadow_stack[SHADOW_OVERFLOW_STACK_SIZE/sizeof(long)] __aligned(16);
 asmlinkage unsigned long get_overflow_stack(void)
 {
        return (unsigned long)this_cpu_ptr(overflow_stack) +
--

2.38.1

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

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

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

Reviewed-by on v1 1/2
#8 lore raw
In reply to Palmer Dabbelt
Quoted 6 lines · Palmer Dabbelt

The standard RISC-V ABIs all require 16-byte stack alignment. We're only calling that one function on the shadow stack so I doubt it'd result in a real issue, but might as well keep this lined up.

Fixes 31da94c25aea ("riscv: add VMAP_STACK overflow detection")
Quoted 20 lines
---
 arch/riscv/kernel/traps.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/riscv/kernel/traps.c +1 -1
index be54ccea8c47..acdfcacd7e57 100644
@@ -206,7 +206,7 @@ static DEFINE_PER_CPU(unsigned long [OVERFLOW_STACK_SIZE/sizeof(long)],
  * shadow stack, handled_ kernel_ stack_ overflow(in kernel/entry.S) is used
  * to get per-cpu overflow stack(get_overflow_stack).
  */
-long shadow_stack[SHADOW_OVERFLOW_STACK_SIZE/sizeof(long)];
+long shadow_stack[SHADOW_OVERFLOW_STACK_SIZE/sizeof(long)] __aligned(16);
 asmlinkage unsigned long get_overflow_stack(void)
 {
 	return (unsigned long)this_cpu_ptr(overflow_stack) +
Signature · 1 line
2.38.1
author Applied
#9 lore raw
In reply to Palmer Dabbelt
Quoted 5 lines · Palmer Dabbelt

The standard RISC-V ABIs all require 16-byte stack alignment. We're only calling that one function on the shadow stack so I doubt it'd result in a real issue, but might as well keep this lined up.

Applied, thanks!

[1/2] RISC-V: Align the shadow stack

      https://git.kernel.org/palmer/c/c3ec1e8964fb
[2/2] RISC-V: Add some comments about the shadow and overflow stacks
      https://git.kernel.org/palmer/c/de57ecc47610

Best regards,

Signature · 1 line
Palmer Dabbelt <[email protected]>
#10 lore raw
In reply to Palmer Dabbelt
This series was applied to riscv/linux.git (for-next) by Palmer Dabbelt <[email protected]>:

Hello:

This series was applied to riscv/linux.git (for-next) by Palmer Dabbelt <[email protected]>:

Quoted 8 lines · On Tue, 29 Nov 2022 18:35:14 -0800 you wrote:

The standard RISC-V ABIs all require 16-byte stack alignment. We're only calling that one function on the shadow stack so I doubt it'd result in a real issue, but might as well keep this lined up.

Fixes 31da94c25aea ("riscv: add VMAP_STACK overflow detection")

[...]

Here is the summary with links:
- [1/2] RISC-V: Align the shadow stack

    https://git.kernel.org/riscv/c/b003b3b77d65
  - [2/2] RISC-V: Add some comments about the shadow and overflow stacks
    https://git.kernel.org/riscv/c/de57ecc47610

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html