arm64: panic if IRQ shadow call stack allocation fails
PATCH 1/1 arm64: panic if IRQ shadow call stack allocation fails
scs_alloc() can return NULL when vmalloc fails. init_irq_scs() previously stored that NULL in per-cpu irq_shadow_call_stack_ptr, which IRQ entry would then use under CONFIG_SHADOW_CALL_STACK. Match other SCS setup paths (e.g. SDEI) by failing explicitly instead of continuing with a NULL pointer.
Mark init_irq_scs() __init since it is only called from init_IRQ().
--- arch/arm64/kernel/irq.c | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-)
arch/arm64/kernel/irq.c +9 -5
@@ -14,6 +14,7 @@#include <linux/init.h> #include <linux/irq.h> #include <linux/irqchip.h> +#include <linux/kernel.h> #include <linux/kprobes.h> #include <linux/memory.h> #include <linux/scs.h>
@@ -32,23 +33,26 @@ DEFINE_PER_CPU(struct nmi_ctx, nmi_contexts); DEFINE_PER_CPU(unsigned long *, irq_stack_ptr); - DECLARE_PER_CPU(unsigned long *, irq_shadow_call_stack_ptr);#ifdef CONFIG_SHADOW_CALL_STACK DEFINE_PER_CPU(unsigned long *, 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 (early_cpu_to_node (cpu)); +for_each_possible_cpu (cpu) { + s =scs_alloc (early_cpu_to_node (cpu)); +if (!s) +panic ("irq: Failed to allocate shadow call stack\n" ); +per_cpu (irq_shadow_call_stack_ptr, cpu) = s; + } }static void __init init_irq_stacks (void )
Signature · 1 line
2.43.0
Quoted 5 lines · Osama Abdelkader
scs_alloc()can return NULL when vmalloc fails.init_irq_scs()previously stored that NULL in per-cpuirq_shadow_call_stack_ptr, which IRQ entry would then use underCONFIG_SHADOW_CALL_STACK. Match other SCS setup paths (e.g. SDEI) by failing explicitly instead of continuing with a NULL pointer.
Right, _init_sdei_scs() doesn't not assign the per cpu pointer with NULL, but, at the same time it doesn't panic. SDEI propagates -ENOMEM back up the call chain and even frees already allocated stacks via free_sdei_scs(). Should it panic as well?
Quoted 12 lines
Mark
init_irq_scs()__initsince it is only called frominit_IRQ().Signed-off-by Osama Abdelkader <[email protected]>--- arch/arm64/kernel/irq.c | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-)arch/arm64/kernel/irq.c +0
-0
arch/arm64/kernel/irq.c line 17
Quoted 4 lines
arch/arm64/kernel/irq.c +1
-0@@ -14,6 +14,7 @@#include <linux/init.h> #include <linux/irq.h> #include <linux/irqchip.h> +#include <linux/kernel.h>
Why do you need kernel.h in here? I initially thought it was for panic(), but, later I found panic() is already in use in this file.
Isn't kernel.h being included transitively?
Quoted 23 lines
arch/arm64/kernel/irq.c +2
-2#include <linux/kprobes.h> #include <linux/memory.h> #include <linux/scs.h> @@ -32,23 +33,26 @@ DEFINE_PER_CPU(struct nmi_ctx, nmi_contexts); DEFINE_PER_CPU(unsigned long *, irq_stack_ptr); - DECLARE_PER_CPU(unsigned long *, irq_shadow_call_stack_ptr);#ifdef CONFIG_SHADOW_CALL_STACK DEFINE_PER_CPU(unsigned long *, 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 ;
arch/arm64/kernel/irq.c DEFINE_PER_CPU(struct nmi_ctx, nmi_contexts); · line 56
Quoted 10 lines
arch/arm64/kernel/irq.c +6
-3-for_each_possible_cpu (cpu) -per_cpu (irq_shadow_call_stack_ptr, cpu) = -scs_alloc (early_cpu_to_node (cpu)); +for_each_possible_cpu (cpu) { + s =scs_alloc (early_cpu_to_node (cpu)); +if (!s) +panic ("irq: Failed to allocate shadow call stack\n" ); +per_cpu (irq_shadow_call_stack_ptr, cpu) = s; + } }
Reading RISC-V code, it seems it has the same problem. Is it worth fixing also?
static void init_irq_scs(void)
{
int cpu;
if (!scs_is_enabled())
return;
for_each_possible_cpu(cpu)
per_cpu(irq_shadow_call_stack_ptr, cpu) =
scs_alloc(cpu_to_node(cpu));}
Other than these nits, feel free to add:
Quoted 7 lines · Breno Leitao
Quoted 5 lines · Osama Abdelkader
scs_alloc()can return NULL when vmalloc fails.init_irq_scs()previously stored that NULL in per-cpuirq_shadow_call_stack_ptr, which IRQ entry would then use underCONFIG_SHADOW_CALL_STACK. Match other SCS setup paths (e.g. SDEI) by failing explicitly instead of continuing with a NULL pointer.
Quoted 5 lines
Right,
_init_sdei_scs()doesn't not assign the per cpu pointer with NULL, but, at the same time it doesn't panic. SDEI propagates-ENOMEMback up the call chain and even frees already allocated stacks viafree_sdei_scs(). Should it panic as well?
Thanks, I changed it to return -ENOMEM in v2 to address will's review.
Quoted 20 lines
Quoted 12 lines
Mark
init_irq_scs()__initsince it is only called frominit_IRQ().Signed-off-by Osama Abdelkader <[email protected]>--- arch/arm64/kernel/irq.c | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-)arch/arm64/kernel/irq.c +0
-0arch/arm64/kernel/irq.c line 17
Quoted 4 lines
arch/arm64/kernel/irq.c +1
-0@@ -14,6 +14,7 @@#include <linux/init.h> #include <linux/irq.h> #include <linux/irqchip.h> +#include <linux/kernel.h> Why do you need
kernel.hin here? I initially thought it was forpanic(), but, later I foundpanic()is already in use in this file.
Quoted 1 line
Isn't
kernel.hbeing included transitively?
Right, I removed it in v2, thanks.
Quoted 46 lines
Quoted 23 lines
arch/arm64/kernel/irq.c +2
-2#include <linux/kprobes.h> #include <linux/memory.h> #include <linux/scs.h> @@ -32,23 +33,26 @@ DEFINE_PER_CPU(struct nmi_ctx, nmi_contexts); DEFINE_PER_CPU(unsigned long *, irq_stack_ptr); - DECLARE_PER_CPU(unsigned long *, irq_shadow_call_stack_ptr);#ifdef CONFIG_SHADOW_CALL_STACK DEFINE_PER_CPU(unsigned long *, 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 ;arch/arm64/kernel/irq.c DEFINE_PER_CPU(struct nmi_ctx, nmi_contexts); · line 56
Quoted 10 lines
arch/arm64/kernel/irq.c +6
-3-for_each_possible_cpu (cpu) -per_cpu (irq_shadow_call_stack_ptr, cpu) = -scs_alloc (early_cpu_to_node (cpu)); +for_each_possible_cpu (cpu) { + s =scs_alloc (early_cpu_to_node (cpu)); +if (!s) +panic ("irq: Failed to allocate shadow call stack\n" ); +per_cpu (irq_shadow_call_stack_ptr, cpu) = s; + } }Reading RISC-V code, it seems it has the same problem. Is it worth fixing also?
static void
init_irq_scs(void){ int cpu; if (!scs_is_enabled()) return; for_each_possible_cpu(cpu) per_cpu(irq_shadow_call_stack_ptr, cpu) = scs_alloc(cpu_to_node(cpu));
Quoted 1 line
}
Yes, thanks for the check.
Quoted 4 lines
Other than these nits, feel free to add:
Reviewed-by Breno Leitao <[email protected]>
Thank you. I sent v2:
[PATCH v2] arm64: panic from init_IRQ if IRQ handler stacks cannot be
allocated
To cover init_irq_stacks as well.
Best regards,
Osama
Will Deacon
maintainerQuoted 31 lines · Osama Abdelkader
arch/arm64/kernel/irq.c +3
-2@@ -14,6 +14,7 @@#include <linux/init.h> #include <linux/irq.h> #include <linux/irqchip.h> +#include <linux/kernel.h> #include <linux/kprobes.h> #include <linux/memory.h> #include <linux/scs.h> @@ -32,23 +33,26 @@ DEFINE_PER_CPU(struct nmi_ctx, nmi_contexts); DEFINE_PER_CPU(unsigned long *, irq_stack_ptr); - DECLARE_PER_CPU(unsigned long *, irq_shadow_call_stack_ptr);#ifdef CONFIG_SHADOW_CALL_STACK DEFINE_PER_CPU(unsigned long *, 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 ;
arch/arm64/kernel/irq.c DEFINE_PER_CPU(struct nmi_ctx, nmi_contexts); · line 55
Quoted 10 lines
arch/arm64/kernel/irq.c +6
-3-for_each_possible_cpu (cpu) -per_cpu (irq_shadow_call_stack_ptr, cpu) = -scs_alloc (early_cpu_to_node (cpu)); +for_each_possible_cpu (cpu) { + s =scs_alloc (early_cpu_to_node (cpu)); +if (!s) +panic ("irq: Failed to allocate shadow call stack\n" ); +per_cpu (irq_shadow_call_stack_ptr, cpu) = s; + }
I don't especially see the point in these panic() messages given that presumably all sorts of other things will go wrong if we fail simple allocations this early during boot.
If you really want to check this, then we should at least do the same for the IRQ stack itself, otherwise it's all a bit academic. So maybe have init_irq_scs() and init_irq_stacks() return -ENOMEM so that init_IRQ() can panic?
Will
Quoted 47 lines · Will Deacon
Quoted 31 lines · Osama Abdelkader
arch/arm64/kernel/irq.c +3
-2@@ -14,6 +14,7 @@#include <linux/init.h> #include <linux/irq.h> #include <linux/irqchip.h> +#include <linux/kernel.h> #include <linux/kprobes.h> #include <linux/memory.h> #include <linux/scs.h> @@ -32,23 +33,26 @@ DEFINE_PER_CPU(struct nmi_ctx, nmi_contexts); DEFINE_PER_CPU(unsigned long *, irq_stack_ptr); - DECLARE_PER_CPU(unsigned long *, irq_shadow_call_stack_ptr);#ifdef CONFIG_SHADOW_CALL_STACK DEFINE_PER_CPU(unsigned long *, 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 ;arch/arm64/kernel/irq.c DEFINE_PER_CPU(struct nmi_ctx, nmi_contexts); · line 55
Quoted 10 lines
arch/arm64/kernel/irq.c +6
-3-for_each_possible_cpu (cpu) -per_cpu (irq_shadow_call_stack_ptr, cpu) = -scs_alloc (early_cpu_to_node (cpu)); +for_each_possible_cpu (cpu) { + s =scs_alloc (early_cpu_to_node (cpu)); +if (!s) +panic ("irq: Failed to allocate shadow call stack\n" ); +per_cpu (irq_shadow_call_stack_ptr, cpu) = s; + }I don't especially see the point in these
panic()messages given that presumably all sorts of other things will go wrong if we fail simple allocations this early during boot.
Quoted 6 lines
If you really want to check this, then we should at least do the same for the IRQ stack itself, otherwise it's all a bit academic. So maybe have
init_irq_scs()andinit_irq_stacks()return-ENOMEMso thatinit_IRQ()can panic?Will
Thanks for the review, I just did that in v2:
[PATCH v2] arm64: panic from init_IRQ if IRQ handler stacks cannot be
allocated
Best regards,
Osama