riscv: panic from init_IRQ if IRQ handler stacks cannot be allocated
PATCH 1/1 riscv: panic from init_IRQ if IRQ handler stacks cannot be allocated
init_irq_stacks() and init_irq_scs() may fail when arch_alloc_vmap_stack or scs_alloc return NULL. Return -ENOMEM from both and call panic() once from init_IRQ(), covering per-CPU IRQ stacks and shadow IRQ stacks consistently.
--- arch/riscv/kernel/irq.c | 43 ++++++++++++++++++++++++++++++----------- 1 file changed, 32 insertions(+), 11 deletions(-)
arch/riscv/kernel/irq.c +32 -11
@@ -5,6 +5,7 @@* Copyright (C) 2018 Christoph Hellwig */ +#include <linux/errno.h> #include <linux/interrupt.h> #include <linux/irqchip.h> #include <linux/irqdomain.h>
@@ -75,41 +76,53 @@ DECLARE_PER_CPU(ulong *, irq_shadow_call_stack_ptr); DEFINE_PER_CPU(ulong *, irq_shadow_call_stack_ptr);#endif -static void init_irq_scs (void ) +static int __init init_irq_scs (void ) {int cpu; +void *s;if (!scs_is_enabled ()) -return ; +return 0 ; -for_each_possible_cpu (cpu) -per_cpu (irq_shadow_call_stack_ptr, cpu) = -scs_alloc (cpu_to_node (cpu)); +for_each_possible_cpu (cpu) { + s =scs_alloc (cpu_to_node (cpu)); +if (!s) +return -ENOMEM ; +per_cpu (irq_shadow_call_stack_ptr, cpu) = s; + } + +return 0 ; } DEFINE_PER_CPU(ulong *, irq_stack_ptr);#ifdef CONFIG_VMAP_STACK -static void init_irq_stacks (void ) +static int __init init_irq_stacks (void ) {int cpu;ulong *p;for_each_possible_cpu (cpu) { p =arch_alloc_vmap_stack (IRQ_STACK_SIZE ,cpu_to_node (cpu)); +if (!p) +return -ENOMEM ;per_cpu (irq_stack_ptr, cpu) = p; } + +return 0 ; }#else /* irq stack only needs to be 16 byte aligned - not IRQ_STACK_SIZE aligned. */ DEFINE_PER_CPU_ALIGNED (ulong [IRQ_STACK_SIZE /sizeof (ulong)], irq_stack); -static void init_irq_stacks (void ) +static int __init init_irq_stacks (void ) {int cpu;for_each_possible_cpu (cpu)per_cpu (irq_stack_ptr, cpu) =per_cpu (irq_stack, cpu); + +return 0 ; }#endif /* CONFIG_VMAP_STACK */
@@ -129,8 +142,15 @@ void do_softirq_own_stack(void)#endif /* CONFIG_SOFTIRQ_ON_OWN_STACK */ #else -static void init_irq_scs (void ) {} -static void init_irq_stacks (void ) {} +static int __init init_irq_scs (void ) +{ +return 0 ; +} + +static int __init init_irq_stacks (void ) +{ +return 0 ; +}#endif /* CONFIG_IRQ_STACKS */ int arch_show_interrupts (struct seq_file *p,int prec)
@@ -141,8 +161,9 @@ int arch_show_interrupts(struct seq_file *p, int prec)void __init init_IRQ (void ) { -init_irq_scs (); -init_irq_stacks (); +if (init_irq_stacks () ||init_irq_scs ()) +panic ("Failed to allocate IRQ stack resources\n" ); +irqchip_init ();if (!handle_arch_irq)panic ("No interrupt controller found." );
Signature · 1 line
2.43.0
Paul Walmsley
maintainerHi,
Quoted 6 lines · Osama Abdelkader
init_irq_stacks()andinit_irq_scs()may fail whenarch_alloc_vmap_stackorscs_allocreturn NULL. Return-ENOMEMfrom both and callpanic()once frominit_IRQ(), covering per-CPU IRQ stacks and shadow IRQ stacks consistently.Signed-off-by Osama Abdelkader <[email protected]>
This patch appears to be written with AI assistance. If so, it should be noted in the description. Please see
Quoted 27 lines
--- arch/riscv/kernel/irq.c | 43 ++++++++++++++++++++++++++++++----------- 1 file changed, 32 insertions(+), 11 deletions(-)arch/riscv/kernel/irq.c +3
-1@@ -5,6 +5,7 @@* Copyright (C) 2018 Christoph Hellwig */ +#include <linux/errno.h> #include <linux/interrupt.h> #include <linux/irqchip.h> #include <linux/irqdomain.h> @@ -75,41 +76,53 @@ DECLARE_PER_CPU(ulong *, irq_shadow_call_stack_ptr); DEFINE_PER_CPU(ulong *, irq_shadow_call_stack_ptr);#endif -static void init_irq_scs (void ) +static int __init init_irq_scs (void ) {int cpu; +void *s;if (!scs_is_enabled ())
arch/riscv/kernel/irq.c DECLARE_PER_CPU(ulong *, irq_shadow_call_stack_ptr); · line 90
Quoted 10 lines
arch/riscv/kernel/irq.c +5
-4-return ; +return 0 ; -for_each_possible_cpu (cpu) -per_cpu (irq_shadow_call_stack_ptr, cpu) = -scs_alloc (cpu_to_node (cpu)); +for_each_possible_cpu (cpu) { + s =scs_alloc (cpu_to_node (cpu)); +if (!s) +return -ENOMEM ;
I don't see why this bothers to return an error value if the sole caller is just going to panic. Best to panic here. Then no one reviewing this patch needs to be concerned about memory leaks.
Quoted 9 lines
arch/riscv/kernel/irq.c +4
-0+per_cpu (irq_shadow_call_stack_ptr, cpu) = s; + } + +return 0 ; } DEFINE_PER_CPU(ulong *, irq_stack_ptr);#ifdef CONFIG_VMAP_STACK
arch/riscv/kernel/irq.c DECLARE_PER_CPU(ulong *, irq_shadow_call_stack_ptr); · line 108
Quoted 10 lines
arch/riscv/kernel/irq.c +3
-1-static void init_irq_stacks (void ) +static int __init init_irq_stacks (void ) {int cpu;ulong *p;for_each_possible_cpu (cpu) { p =arch_alloc_vmap_stack (IRQ_STACK_SIZE ,cpu_to_node (cpu)); +if (!p) +return -ENOMEM ;
Same comment as the above.
Quoted 8 lines
arch/riscv/kernel/irq.c +2
-0per_cpu(irq_stack_ptr, cpu) = p; } + +return 0 ; }#else /* irq stack only needs to be 16 byte aligned - not IRQ_STACK_SIZE aligned. */ DEFINE_PER_CPU_ALIGNED (ulong [IRQ_STACK_SIZE /sizeof (ulong)], irq_stack);
arch/riscv/kernel/irq.c DECLARE_PER_CPU(ulong *, irq_shadow_call_stack_ptr); · line 125
Quoted 10 lines
arch/riscv/kernel/irq.c +3
-1-static void init_irq_stacks (void ) +static int __init init_irq_stacks (void ) {int cpu;for_each_possible_cpu (cpu)per_cpu (irq_stack_ptr, cpu) =per_cpu (irq_stack, cpu); + +return 0 ;
Why?
Quoted 22 lines
arch/riscv/kernel/irq.c +9
-2}#endif /* CONFIG_VMAP_STACK */ @@ -129,8 +142,15 @@ void do_softirq_own_stack(void)#endif /* CONFIG_SOFTIRQ_ON_OWN_STACK */ #else -static void init_irq_scs (void ) {} -static void init_irq_stacks (void ) {} +static int __init init_irq_scs (void ) +{ +return 0 ; +} + +static int __init init_irq_stacks (void ) +{ +return 0 ; +}#endif /* CONFIG_IRQ_STACKS */ int arch_show_interrupts (struct seq_file *p,int prec)
arch/riscv/kernel/irq.c int arch_show_interrupts(struct seq_file *p, int prec) · line 165
Quoted 7 lines
arch/riscv/kernel/irq.c +2
-2@@ -141,8 +161,9 @@ int arch_show_interrupts(struct seq_file *p, int prec)void __init init_IRQ (void ) { -init_irq_scs (); -init_irq_stacks (); +if (init_irq_stacks () ||init_irq_scs ()) +panic ("Failed to allocate IRQ stack resources\n" );
Any reason why the initialization order is reversed?
Quoted 6 lines
arch/riscv/kernel/irq.c +1
-0+irqchip_init ();if (!handle_arch_irq)panic ("No interrupt controller found." );Signature · 1 line
2.43.0
- Paul
Hi Paul,
Quoted 9 lines · Paul Walmsley
Hi,
Quoted 5 lines · Osama Abdelkader
init_irq_stacks()andinit_irq_scs()may fail whenarch_alloc_vmap_stackorscs_allocreturn NULL. Return-ENOMEMfrom both and callpanic()once frominit_IRQ(), covering per-CPU IRQ stacks and shadow IRQ stacks consistently.
Quoted 7 lines
Quoted 1 line
Signed-off-by Osama Abdelkader <[email protected]>This patch appears to be written with AI assistance. If so, it should be noted in the description. Please see
This is actually riscv version of arm64 patch:
[PATCH] arm64: panic if IRQ shadow call stack allocation fails
https://lore.kernel.org/lkml/[email protected]/
where I initially used panic, but in review, returning -ENOMEM was suggested, and this patch also, so I submitted v2 there:
[PATCH v2] arm64: panic from init_IRQ if IRQ handler stacks cannot be allocated
https://lore.kernel.org/lkml/[email protected]/
Quoted 30 lines
Quoted 30 lines
--- arch/riscv/kernel/irq.c | 43 ++++++++++++++++++++++++++++++----------- 1 file changed, 32 insertions(+), 11 deletions(-)arch/riscv/kernel/irq.c +4
-2@@ -5,6 +5,7 @@* Copyright (C) 2018 Christoph Hellwig */ +#include <linux/errno.h> #include <linux/interrupt.h> #include <linux/irqchip.h> #include <linux/irqdomain.h> @@ -75,41 +76,53 @@ DECLARE_PER_CPU(ulong *, irq_shadow_call_stack_ptr); DEFINE_PER_CPU(ulong *, irq_shadow_call_stack_ptr);#endif -static void init_irq_scs (void ) +static int __init init_irq_scs (void ) {int cpu; +void *s;if (!scs_is_enabled ()) -return ; +return 0 ;
Quoted 12 lines
arch/riscv/kernel/irq.c DECLARE_PER_CPU(ulong *, irq_shadow_call_stack_ptr); · line 90
Quoted 7 lines
arch/riscv/kernel/irq.c +4
-3-for_each_possible_cpu (cpu) -per_cpu (irq_shadow_call_stack_ptr, cpu) = -scs_alloc (cpu_to_node (cpu)); +for_each_possible_cpu (cpu) { + s =scs_alloc (cpu_to_node (cpu)); +if (!s) +return -ENOMEM ;I don't see why this bothers to return an error value if the sole caller is just going to panic. Best to panic here. Then no one reviewing this patch needs to be concerned about memory leaks.
Okay, I'm going to use panic in place instead.
Quoted 66 lines
Quoted 9 lines
arch/riscv/kernel/irq.c +4
-0+per_cpu (irq_shadow_call_stack_ptr, cpu) = s; + } + +return 0 ; } DEFINE_PER_CPU(ulong *, irq_stack_ptr);#ifdef CONFIG_VMAP_STACK arch/riscv/kernel/irq.c DECLARE_PER_CPU(ulong *, irq_shadow_call_stack_ptr); · line 108
Quoted 10 lines
arch/riscv/kernel/irq.c +3
-1-static void init_irq_stacks (void ) +static int __init init_irq_stacks (void ) {int cpu;ulong *p;for_each_possible_cpu (cpu) { p =arch_alloc_vmap_stack (IRQ_STACK_SIZE ,cpu_to_node (cpu)); +if (!p) +return -ENOMEM ;Same comment as the above.
Quoted 8 lines
arch/riscv/kernel/irq.c +2
-0per_cpu(irq_stack_ptr, cpu) = p; } + +return 0 ; }#else /* irq stack only needs to be 16 byte aligned - not IRQ_STACK_SIZE aligned. */ DEFINE_PER_CPU_ALIGNED (ulong [IRQ_STACK_SIZE /sizeof (ulong)], irq_stack);arch/riscv/kernel/irq.c DECLARE_PER_CPU(ulong *, irq_shadow_call_stack_ptr); · line 125
Quoted 10 lines
arch/riscv/kernel/irq.c +3
-1-static void init_irq_stacks (void ) +static int __init init_irq_stacks (void ) {int cpu;for_each_possible_cpu (cpu)per_cpu (irq_stack_ptr, cpu) =per_cpu (irq_stack, cpu); + +return 0 ;Why?
Quoted 23 lines
arch/riscv/kernel/irq.c +9
-2}#endif /* CONFIG_VMAP_STACK */ @@ -129,8 +142,15 @@ void do_softirq_own_stack(void)#endif /* CONFIG_SOFTIRQ_ON_OWN_STACK */ #else -static void init_irq_scs (void ) {} -static void init_irq_stacks (void ) {} +static int __init init_irq_scs (void ) +{ +return 0 ; +} + +static int __init init_irq_stacks (void ) +{ +return 0 ; +}#endif /* CONFIG_IRQ_STACKS */ int arch_show_interrupts (struct seq_file *p,int prec)@@ -141,8 +161,9 @@ int arch_show_interrupts(struct seq_file *p, int prec)
Quoted 9 lines
arch/riscv/kernel/irq.c int arch_show_interrupts(struct seq_file *p, int prec) · line 165
Quoted 6 lines
arch/riscv/kernel/irq.c +2
-2void __init init_IRQ (void ) { -init_irq_scs (); -init_irq_stacks (); +if (init_irq_stacks () ||init_irq_scs ()) +panic ("Failed to allocate IRQ stack resources\n" );Any reason why the initialization order is reversed?
It's a mistake, will reverse it back.
Thanks,
Osama
Quoted 9 lines
Quoted 6 lines
arch/riscv/kernel/irq.c +1
-0+irqchip_init ();if (!handle_arch_irq)panic ("No interrupt controller found." );Signature · 1 line
2.43.0- Paul