Skip to content

Commit 163e76c

Browse files
guoren83palmer-dabbelt
authored andcommitted
riscv: stack: Support HAVE_IRQ_EXIT_ON_IRQ_STACK
Add independent irq stacks for percpu to prevent kernel stack overflows. It is also compatible with VMAP_STACK by arch_alloc_vmap_stack. Tested-by: Jisheng Zhang <[email protected]> Signed-off-by: Guo Ren <[email protected]> Signed-off-by: Guo Ren <[email protected]> Cc: Clément Léger <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Palmer Dabbelt <[email protected]>
1 parent d5e45e8 commit 163e76c

File tree

5 files changed

+105
-2
lines changed

5 files changed

+105
-2
lines changed

arch/riscv/Kconfig

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -590,6 +590,13 @@ config FPU
590590

591591
If you don't know what to do here, say Y.
592592

593+
config IRQ_STACKS
594+
bool "Independent irq stacks" if EXPERT
595+
default y
596+
select HAVE_IRQ_EXIT_ON_IRQ_STACK
597+
help
598+
Add independent irq stacks for percpu to prevent kernel stack overflows.
599+
593600
endmenu # "Platform type"
594601

595602
menu "Kernel features"

arch/riscv/include/asm/irq_stack.h

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/* SPDX-License-Identifier: GPL-2.0 */
2+
3+
#ifndef _ASM_RISCV_IRQ_STACK_H
4+
#define _ASM_RISCV_IRQ_STACK_H
5+
6+
#include <linux/bug.h>
7+
#include <linux/gfp.h>
8+
#include <linux/kconfig.h>
9+
#include <linux/vmalloc.h>
10+
#include <linux/pgtable.h>
11+
#include <asm/thread_info.h>
12+
13+
DECLARE_PER_CPU(ulong *, irq_stack_ptr);
14+
15+
#ifdef CONFIG_VMAP_STACK
16+
/*
17+
* To ensure that VMAP'd stack overflow detection works correctly, all VMAP'd
18+
* stacks need to have the same alignment.
19+
*/
20+
static inline unsigned long *arch_alloc_vmap_stack(size_t stack_size, int node)
21+
{
22+
void *p;
23+
24+
p = __vmalloc_node(stack_size, THREAD_ALIGN, THREADINFO_GFP, node,
25+
__builtin_return_address(0));
26+
return kasan_reset_tag(p);
27+
}
28+
#endif /* CONFIG_VMAP_STACK */
29+
30+
#endif /* _ASM_RISCV_IRQ_STACK_H */

arch/riscv/include/asm/thread_info.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@
4040
#define OVERFLOW_STACK_SIZE SZ_4K
4141
#define SHADOW_OVERFLOW_STACK_SIZE (1024)
4242

43+
#define IRQ_STACK_SIZE THREAD_SIZE
44+
4345
#ifndef __ASSEMBLY__
4446

4547
extern long shadow_stack[SHADOW_OVERFLOW_STACK_SIZE / sizeof(long)];

arch/riscv/kernel/irq.c

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,38 @@ struct fwnode_handle *riscv_get_intc_hwnode(void)
2828
}
2929
EXPORT_SYMBOL_GPL(riscv_get_intc_hwnode);
3030

31+
#ifdef CONFIG_IRQ_STACKS
32+
#include <asm/irq_stack.h>
33+
34+
DEFINE_PER_CPU(ulong *, irq_stack_ptr);
35+
36+
#ifdef CONFIG_VMAP_STACK
37+
static void init_irq_stacks(void)
38+
{
39+
int cpu;
40+
ulong *p;
41+
42+
for_each_possible_cpu(cpu) {
43+
p = arch_alloc_vmap_stack(IRQ_STACK_SIZE, cpu_to_node(cpu));
44+
per_cpu(irq_stack_ptr, cpu) = p;
45+
}
46+
}
47+
#else
48+
/* irq stack only needs to be 16 byte aligned - not IRQ_STACK_SIZE aligned. */
49+
DEFINE_PER_CPU_ALIGNED(ulong [IRQ_STACK_SIZE/sizeof(ulong)], irq_stack);
50+
51+
static void init_irq_stacks(void)
52+
{
53+
int cpu;
54+
55+
for_each_possible_cpu(cpu)
56+
per_cpu(irq_stack_ptr, cpu) = per_cpu(irq_stack, cpu);
57+
}
58+
#endif /* CONFIG_VMAP_STACK */
59+
#else
60+
static void init_irq_stacks(void) {}
61+
#endif /* CONFIG_IRQ_STACKS */
62+
3163
int arch_show_interrupts(struct seq_file *p, int prec)
3264
{
3365
show_ipi_stats(p, prec);
@@ -36,6 +68,7 @@ int arch_show_interrupts(struct seq_file *p, int prec)
3668

3769
void __init init_IRQ(void)
3870
{
71+
init_irq_stacks();
3972
irqchip_init();
4073
if (!handle_arch_irq)
4174
panic("No interrupt controller found.");

arch/riscv/kernel/traps.c

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include <asm/syscall.h>
2828
#include <asm/thread_info.h>
2929
#include <asm/vector.h>
30+
#include <asm/irq_stack.h>
3031

3132
int show_unhandled_signals = 1;
3233

@@ -327,16 +328,46 @@ asmlinkage __visible noinstr void do_page_fault(struct pt_regs *regs)
327328
}
328329
#endif
329330

330-
asmlinkage __visible noinstr void do_irq(struct pt_regs *regs)
331+
static void noinstr handle_riscv_irq(struct pt_regs *regs)
331332
{
332333
struct pt_regs *old_regs;
333-
irqentry_state_t state = irqentry_enter(regs);
334334

335335
irq_enter_rcu();
336336
old_regs = set_irq_regs(regs);
337337
handle_arch_irq(regs);
338338
set_irq_regs(old_regs);
339339
irq_exit_rcu();
340+
}
341+
342+
asmlinkage void noinstr do_irq(struct pt_regs *regs)
343+
{
344+
irqentry_state_t state = irqentry_enter(regs);
345+
#ifdef CONFIG_IRQ_STACKS
346+
if (on_thread_stack()) {
347+
ulong *sp = per_cpu(irq_stack_ptr, smp_processor_id())
348+
+ IRQ_STACK_SIZE/sizeof(ulong);
349+
__asm__ __volatile(
350+
"addi sp, sp, -"RISCV_SZPTR "\n"
351+
REG_S" ra, (sp) \n"
352+
"addi sp, sp, -"RISCV_SZPTR "\n"
353+
REG_S" s0, (sp) \n"
354+
"addi s0, sp, 2*"RISCV_SZPTR "\n"
355+
"move sp, %[sp] \n"
356+
"move a0, %[regs] \n"
357+
"call handle_riscv_irq \n"
358+
"addi sp, s0, -2*"RISCV_SZPTR"\n"
359+
REG_L" s0, (sp) \n"
360+
"addi sp, sp, "RISCV_SZPTR "\n"
361+
REG_L" ra, (sp) \n"
362+
"addi sp, sp, "RISCV_SZPTR "\n"
363+
:
364+
: [sp] "r" (sp), [regs] "r" (regs)
365+
: "a0", "a1", "a2", "a3", "a4", "a5", "a6", "a7",
366+
"t0", "t1", "t2", "t3", "t4", "t5", "t6",
367+
"memory");
368+
} else
369+
#endif
370+
handle_riscv_irq(regs);
340371

341372
irqentry_exit(regs, state);
342373
}

0 commit comments

Comments
 (0)