[PATCH v2] riscv: panic if IRQ handler stacks cannot be allocated

Osama Abdelkader posted 1 patch 2 months, 1 week ago
arch/riscv/kernel/irq.c | 22 ++++++++++++++--------
1 file changed, 14 insertions(+), 8 deletions(-)
[PATCH v2] riscv: panic if IRQ handler stacks cannot be allocated
Posted by Osama Abdelkader 2 months, 1 week ago
init_irq_stacks() and init_irq_scs() may fail when arch_alloc_vmap_stack
or scs_alloc return NULL, call panic() in this case.

Signed-off-by: Osama Abdelkader <osama.abdelkader@gmail.com>
---
v2:
- call panic() in place instead of return -ENOMEM
---
 arch/riscv/kernel/irq.c | 22 ++++++++++++++--------
 1 file changed, 14 insertions(+), 8 deletions(-)

diff --git a/arch/riscv/kernel/irq.c b/arch/riscv/kernel/irq.c
index b6af20bc300f..017d42e778be 100644
--- a/arch/riscv/kernel/irq.c
+++ b/arch/riscv/kernel/irq.c
@@ -75,28 +75,34 @@ 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 void __init init_irq_scs(void)
 {
 	int cpu;
+	void *s;
 
 	if (!scs_is_enabled())
 		return;
 
-	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)
+			panic("Failed to allocate IRQ shadow call stack resources\n");
+		per_cpu(irq_shadow_call_stack_ptr, cpu) = s;
+	}
 }
 
 DEFINE_PER_CPU(ulong *, irq_stack_ptr);
 
 #ifdef CONFIG_VMAP_STACK
-static void init_irq_stacks(void)
+static void __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)
+			panic("Failed to allocate IRQ stack resources\n");
 		per_cpu(irq_stack_ptr, cpu) = p;
 	}
 }
@@ -104,7 +110,7 @@ static void init_irq_stacks(void)
 /* 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 void __init init_irq_stacks(void)
 {
 	int cpu;
 
@@ -129,8 +135,8 @@ 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 void __init init_irq_scs(void) {}
+static void __init init_irq_stacks(void) {}
 #endif /* CONFIG_IRQ_STACKS */
 
 int arch_show_interrupts(struct seq_file *p, int prec)
-- 
2.43.0
Re: [PATCH v2] riscv: panic if IRQ handler stacks cannot be allocated
Posted by Paul Walmsley 1 week ago
On Sat, 4 Apr 2026, Osama Abdelkader wrote:

> init_irq_stacks() and init_irq_scs() may fail when arch_alloc_vmap_stack
> or scs_alloc return NULL, call panic() in this case.
> 
> Signed-off-by: Osama Abdelkader <osama.abdelkader@gmail.com>

Thanks, queued for v7.2.


- Paul
Re: [PATCH v2] riscv: panic if IRQ handler stacks cannot be allocated
Posted by Osama Abdelkader 1 month, 1 week ago
On Sat, Apr 04, 2026 at 08:55:20PM +0200, Osama Abdelkader wrote:
> init_irq_stacks() and init_irq_scs() may fail when arch_alloc_vmap_stack
> or scs_alloc return NULL, call panic() in this case.
> 
> Signed-off-by: Osama Abdelkader <osama.abdelkader@gmail.com>
> ---
> v2:
> - call panic() in place instead of return -ENOMEM
> ---
>  arch/riscv/kernel/irq.c | 22 ++++++++++++++--------
>  1 file changed, 14 insertions(+), 8 deletions(-)
> 
> diff --git a/arch/riscv/kernel/irq.c b/arch/riscv/kernel/irq.c
> index b6af20bc300f..017d42e778be 100644
> --- a/arch/riscv/kernel/irq.c
> +++ b/arch/riscv/kernel/irq.c
> @@ -75,28 +75,34 @@ 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 void __init init_irq_scs(void)
>  {
>  	int cpu;
> +	void *s;
>  
>  	if (!scs_is_enabled())
>  		return;
>  
> -	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)
> +			panic("Failed to allocate IRQ shadow call stack resources\n");
> +		per_cpu(irq_shadow_call_stack_ptr, cpu) = s;
> +	}
>  }
>  
>  DEFINE_PER_CPU(ulong *, irq_stack_ptr);
>  
>  #ifdef CONFIG_VMAP_STACK
> -static void init_irq_stacks(void)
> +static void __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)
> +			panic("Failed to allocate IRQ stack resources\n");
>  		per_cpu(irq_stack_ptr, cpu) = p;
>  	}
>  }
> @@ -104,7 +110,7 @@ static void init_irq_stacks(void)
>  /* 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 void __init init_irq_stacks(void)
>  {
>  	int cpu;
>  
> @@ -129,8 +135,8 @@ 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 void __init init_irq_scs(void) {}
> +static void __init init_irq_stacks(void) {}
>  #endif /* CONFIG_IRQ_STACKS */
>  
>  int arch_show_interrupts(struct seq_file *p, int prec)
> -- 
> 2.43.0
> 
ping.
Re: [PATCH v2] riscv: panic if IRQ handler stacks cannot be allocated
Posted by Osama Abdelkader 2 months ago
On Sat, Apr 04, 2026 at 08:55:20PM +0200, Osama Abdelkader wrote:
> init_irq_stacks() and init_irq_scs() may fail when arch_alloc_vmap_stack
> or scs_alloc return NULL, call panic() in this case.
> 
> Signed-off-by: Osama Abdelkader <osama.abdelkader@gmail.com>
> ---
> v2:
> - call panic() in place instead of return -ENOMEM
> ---
>  arch/riscv/kernel/irq.c | 22 ++++++++++++++--------
>  1 file changed, 14 insertions(+), 8 deletions(-)
> 
> diff --git a/arch/riscv/kernel/irq.c b/arch/riscv/kernel/irq.c
> index b6af20bc300f..017d42e778be 100644
> --- a/arch/riscv/kernel/irq.c
> +++ b/arch/riscv/kernel/irq.c
> @@ -75,28 +75,34 @@ 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 void __init init_irq_scs(void)
>  {
>  	int cpu;
> +	void *s;
>  
>  	if (!scs_is_enabled())
>  		return;
>  
> -	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)
> +			panic("Failed to allocate IRQ shadow call stack resources\n");
> +		per_cpu(irq_shadow_call_stack_ptr, cpu) = s;
> +	}
>  }
>  
>  DEFINE_PER_CPU(ulong *, irq_stack_ptr);
>  
>  #ifdef CONFIG_VMAP_STACK
> -static void init_irq_stacks(void)
> +static void __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)
> +			panic("Failed to allocate IRQ stack resources\n");
>  		per_cpu(irq_stack_ptr, cpu) = p;
>  	}
>  }
> @@ -104,7 +110,7 @@ static void init_irq_stacks(void)
>  /* 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 void __init init_irq_stacks(void)
>  {
>  	int cpu;
>  
> @@ -129,8 +135,8 @@ 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 void __init init_irq_scs(void) {}
> +static void __init init_irq_stacks(void) {}
>  #endif /* CONFIG_IRQ_STACKS */
>  
>  int arch_show_interrupts(struct seq_file *p, int prec)
> -- 
> 2.43.0
> 

Hi All,

Can you please review?

Best regards,
Osama