[v2,2/2] arm64: scs: use vmapped IRQ and SDEI shadow stacks
diff mbox series

Message ID 20201124195940.27061-3-samitolvanen@google.com
State New, archived
Headers show
Series
  • scs: switch to vmapped shadow stacks
Related show

Commit Message

Sami Tolvanen Nov. 24, 2020, 7:59 p.m. UTC
Use scs_alloc() to allocate also IRQ and SDEI shadow stacks instead of
using statically allocated stacks.

Signed-off-by: Sami Tolvanen <samitolvanen@google.com>
---
 arch/arm64/kernel/Makefile |  1 -
 arch/arm64/kernel/entry.S  |  6 ++--
 arch/arm64/kernel/irq.c    | 19 ++++++++++
 arch/arm64/kernel/scs.c    | 16 ---------
 arch/arm64/kernel/sdei.c   | 71 +++++++++++++++++++++++++++++++-------
 include/linux/scs.h        |  4 ---
 6 files changed, 81 insertions(+), 36 deletions(-)
 delete mode 100644 arch/arm64/kernel/scs.c

Comments

Will Deacon Nov. 30, 2020, 11:49 a.m. UTC | #1
On Tue, Nov 24, 2020 at 11:59:40AM -0800, Sami Tolvanen wrote:
> Use scs_alloc() to allocate also IRQ and SDEI shadow stacks instead of
> using statically allocated stacks.
> 
> Signed-off-by: Sami Tolvanen <samitolvanen@google.com>
> ---
>  arch/arm64/kernel/Makefile |  1 -
>  arch/arm64/kernel/entry.S  |  6 ++--
>  arch/arm64/kernel/irq.c    | 19 ++++++++++
>  arch/arm64/kernel/scs.c    | 16 ---------
>  arch/arm64/kernel/sdei.c   | 71 +++++++++++++++++++++++++++++++-------
>  include/linux/scs.h        |  4 ---
>  6 files changed, 81 insertions(+), 36 deletions(-)
>  delete mode 100644 arch/arm64/kernel/scs.c

[...]

> @@ -70,18 +97,40 @@ static int _init_sdei_stack(unsigned long * __percpu *ptr, int cpu)
>  	return 0;
>  }
>  
> +static int _init_sdei_scs(unsigned long * __percpu *ptr, int cpu)
> +{
> +	void *s;
> +
> +	s = scs_alloc(cpu_to_node(cpu));
> +	if (!s)
> +		return -ENOMEM;
> +	per_cpu(*ptr, cpu) = s;
> +
> +	return 0;
> +}
> +
>  static int init_sdei_stacks(void)
>  {
>  	int cpu;
>  	int err = 0;
>  
>  	for_each_possible_cpu(cpu) {
> -		err = _init_sdei_stack(&sdei_stack_normal_ptr, cpu);
> -		if (err)
> -			break;
> -		err = _init_sdei_stack(&sdei_stack_critical_ptr, cpu);
> -		if (err)
> -			break;
> +		if (IS_ENABLED(CONFIG_VMAP_STACK)) {
> +			err = _init_sdei_stack(&sdei_stack_normal_ptr, cpu);
> +			if (err)
> +				break;
> +			err = _init_sdei_stack(&sdei_stack_critical_ptr, cpu);
> +			if (err)
> +				break;
> +		}
> +		if (IS_ENABLED(CONFIG_SHADOW_CALL_STACK)) {
> +			err = _init_sdei_scs(&sdei_shadow_call_stack_normal_ptr, cpu);
> +			if (err)
> +				break;
> +			err = _init_sdei_scs(&sdei_shadow_call_stack_critical_ptr, cpu);
> +			if (err)
> +				break;

This looks ok to me, but I think it would be better to follow the same
approach as you have for the IRQ stacks and instead have a separate
init_sdei_scs() function (similarly for the free() path), which means
you can simply the IS_ENABLED() checks too.

Either way:

Acked-by: Will Deacon <will@kernel.org>

Will
Sami Tolvanen Nov. 30, 2020, 9:13 p.m. UTC | #2
On Mon, Nov 30, 2020 at 3:49 AM Will Deacon <will@kernel.org> wrote:
>
> On Tue, Nov 24, 2020 at 11:59:40AM -0800, Sami Tolvanen wrote:
> > Use scs_alloc() to allocate also IRQ and SDEI shadow stacks instead of
> > using statically allocated stacks.
> >
> > Signed-off-by: Sami Tolvanen <samitolvanen@google.com>
> > ---
> >  arch/arm64/kernel/Makefile |  1 -
> >  arch/arm64/kernel/entry.S  |  6 ++--
> >  arch/arm64/kernel/irq.c    | 19 ++++++++++
> >  arch/arm64/kernel/scs.c    | 16 ---------
> >  arch/arm64/kernel/sdei.c   | 71 +++++++++++++++++++++++++++++++-------
> >  include/linux/scs.h        |  4 ---
> >  6 files changed, 81 insertions(+), 36 deletions(-)
> >  delete mode 100644 arch/arm64/kernel/scs.c
>
> [...]
>
> > @@ -70,18 +97,40 @@ static int _init_sdei_stack(unsigned long * __percpu *ptr, int cpu)
> >       return 0;
> >  }
> >
> > +static int _init_sdei_scs(unsigned long * __percpu *ptr, int cpu)
> > +{
> > +     void *s;
> > +
> > +     s = scs_alloc(cpu_to_node(cpu));
> > +     if (!s)
> > +             return -ENOMEM;
> > +     per_cpu(*ptr, cpu) = s;
> > +
> > +     return 0;
> > +}
> > +
> >  static int init_sdei_stacks(void)
> >  {
> >       int cpu;
> >       int err = 0;
> >
> >       for_each_possible_cpu(cpu) {
> > -             err = _init_sdei_stack(&sdei_stack_normal_ptr, cpu);
> > -             if (err)
> > -                     break;
> > -             err = _init_sdei_stack(&sdei_stack_critical_ptr, cpu);
> > -             if (err)
> > -                     break;
> > +             if (IS_ENABLED(CONFIG_VMAP_STACK)) {
> > +                     err = _init_sdei_stack(&sdei_stack_normal_ptr, cpu);
> > +                     if (err)
> > +                             break;
> > +                     err = _init_sdei_stack(&sdei_stack_critical_ptr, cpu);
> > +                     if (err)
> > +                             break;
> > +             }
> > +             if (IS_ENABLED(CONFIG_SHADOW_CALL_STACK)) {
> > +                     err = _init_sdei_scs(&sdei_shadow_call_stack_normal_ptr, cpu);
> > +                     if (err)
> > +                             break;
> > +                     err = _init_sdei_scs(&sdei_shadow_call_stack_critical_ptr, cpu);
> > +                     if (err)
> > +                             break;
>
> This looks ok to me, but I think it would be better to follow the same
> approach as you have for the IRQ stacks and instead have a separate
> init_sdei_scs() function (similarly for the free() path), which means
> you can simply the IS_ENABLED() checks too.

OK, I can change this in v3. It makes error handling in
sdei_arch_get_entry_point() a bit more awkward though. We'll need
something like this:

        if (IS_ENABLED(CONFIG_VMAP_STACK)) {
                if (init_sdei_stacks())
                        return 0;
        }

        if (IS_ENABLED(CONFIG_SHADOW_CALL_STACK)) {
                if (init_sdei_scs()) {
                        if (IS_ENABLED(CONFIG_VMAP_STACK))
                                free_sdei_stacks();
                        return 0;
                }
        }

Sami
Will Deacon Dec. 1, 2020, 10:18 a.m. UTC | #3
On Mon, Nov 30, 2020 at 01:13:07PM -0800, Sami Tolvanen wrote:
> On Mon, Nov 30, 2020 at 3:49 AM Will Deacon <will@kernel.org> wrote:
> >
> > On Tue, Nov 24, 2020 at 11:59:40AM -0800, Sami Tolvanen wrote:
> > > Use scs_alloc() to allocate also IRQ and SDEI shadow stacks instead of
> > > using statically allocated stacks.
> > >
> > > Signed-off-by: Sami Tolvanen <samitolvanen@google.com>
> > > ---
> > >  arch/arm64/kernel/Makefile |  1 -
> > >  arch/arm64/kernel/entry.S  |  6 ++--
> > >  arch/arm64/kernel/irq.c    | 19 ++++++++++
> > >  arch/arm64/kernel/scs.c    | 16 ---------
> > >  arch/arm64/kernel/sdei.c   | 71 +++++++++++++++++++++++++++++++-------
> > >  include/linux/scs.h        |  4 ---
> > >  6 files changed, 81 insertions(+), 36 deletions(-)
> > >  delete mode 100644 arch/arm64/kernel/scs.c
> >
> > [...]
> >
> > > @@ -70,18 +97,40 @@ static int _init_sdei_stack(unsigned long * __percpu *ptr, int cpu)
> > >       return 0;
> > >  }
> > >
> > > +static int _init_sdei_scs(unsigned long * __percpu *ptr, int cpu)
> > > +{
> > > +     void *s;
> > > +
> > > +     s = scs_alloc(cpu_to_node(cpu));
> > > +     if (!s)
> > > +             return -ENOMEM;
> > > +     per_cpu(*ptr, cpu) = s;
> > > +
> > > +     return 0;
> > > +}
> > > +
> > >  static int init_sdei_stacks(void)
> > >  {
> > >       int cpu;
> > >       int err = 0;
> > >
> > >       for_each_possible_cpu(cpu) {
> > > -             err = _init_sdei_stack(&sdei_stack_normal_ptr, cpu);
> > > -             if (err)
> > > -                     break;
> > > -             err = _init_sdei_stack(&sdei_stack_critical_ptr, cpu);
> > > -             if (err)
> > > -                     break;
> > > +             if (IS_ENABLED(CONFIG_VMAP_STACK)) {
> > > +                     err = _init_sdei_stack(&sdei_stack_normal_ptr, cpu);
> > > +                     if (err)
> > > +                             break;
> > > +                     err = _init_sdei_stack(&sdei_stack_critical_ptr, cpu);
> > > +                     if (err)
> > > +                             break;
> > > +             }
> > > +             if (IS_ENABLED(CONFIG_SHADOW_CALL_STACK)) {
> > > +                     err = _init_sdei_scs(&sdei_shadow_call_stack_normal_ptr, cpu);
> > > +                     if (err)
> > > +                             break;
> > > +                     err = _init_sdei_scs(&sdei_shadow_call_stack_critical_ptr, cpu);
> > > +                     if (err)
> > > +                             break;
> >
> > This looks ok to me, but I think it would be better to follow the same
> > approach as you have for the IRQ stacks and instead have a separate
> > init_sdei_scs() function (similarly for the free() path), which means
> > you can simply the IS_ENABLED() checks too.
> 
> OK, I can change this in v3. It makes error handling in
> sdei_arch_get_entry_point() a bit more awkward though. We'll need
> something like this:
> 
>         if (IS_ENABLED(CONFIG_VMAP_STACK)) {
>                 if (init_sdei_stacks())
>                         return 0;
>         }
> 
>         if (IS_ENABLED(CONFIG_SHADOW_CALL_STACK)) {
>                 if (init_sdei_scs()) {
>                         if (IS_ENABLED(CONFIG_VMAP_STACK))
>                                 free_sdei_stacks();
>                         return 0;
>                 }

Can you push the IS_ENABLED() checks into their respective functions?
Then you can do something like:

	if (init_sdei_stacks())
		return 0;

	if (init_sdei_scs())
		goto out_free_stacks;

	...

out_free_stacks:
	free_sdei_stacks();
	return 0;


Will
Will Deacon Dec. 1, 2020, 10:26 a.m. UTC | #4
On Tue, Dec 01, 2020 at 10:18:19AM +0000, Will Deacon wrote:
> On Mon, Nov 30, 2020 at 01:13:07PM -0800, Sami Tolvanen wrote:
> > On Mon, Nov 30, 2020 at 3:49 AM Will Deacon <will@kernel.org> wrote:
> > > On Tue, Nov 24, 2020 at 11:59:40AM -0800, Sami Tolvanen wrote:
> > > >       for_each_possible_cpu(cpu) {
> > > > -             err = _init_sdei_stack(&sdei_stack_normal_ptr, cpu);
> > > > -             if (err)
> > > > -                     break;
> > > > -             err = _init_sdei_stack(&sdei_stack_critical_ptr, cpu);
> > > > -             if (err)
> > > > -                     break;
> > > > +             if (IS_ENABLED(CONFIG_VMAP_STACK)) {
> > > > +                     err = _init_sdei_stack(&sdei_stack_normal_ptr, cpu);
> > > > +                     if (err)
> > > > +                             break;
> > > > +                     err = _init_sdei_stack(&sdei_stack_critical_ptr, cpu);
> > > > +                     if (err)
> > > > +                             break;
> > > > +             }
> > > > +             if (IS_ENABLED(CONFIG_SHADOW_CALL_STACK)) {
> > > > +                     err = _init_sdei_scs(&sdei_shadow_call_stack_normal_ptr, cpu);
> > > > +                     if (err)
> > > > +                             break;
> > > > +                     err = _init_sdei_scs(&sdei_shadow_call_stack_critical_ptr, cpu);
> > > > +                     if (err)
> > > > +                             break;
> > >
> > > This looks ok to me, but I think it would be better to follow the same
> > > approach as you have for the IRQ stacks and instead have a separate
> > > init_sdei_scs() function (similarly for the free() path), which means
> > > you can simply the IS_ENABLED() checks too.
> > 
> > OK, I can change this in v3. It makes error handling in
> > sdei_arch_get_entry_point() a bit more awkward though. We'll need
> > something like this:
> > 
> >         if (IS_ENABLED(CONFIG_VMAP_STACK)) {
> >                 if (init_sdei_stacks())
> >                         return 0;
> >         }
> > 
> >         if (IS_ENABLED(CONFIG_SHADOW_CALL_STACK)) {
> >                 if (init_sdei_scs()) {
> >                         if (IS_ENABLED(CONFIG_VMAP_STACK))
> >                                 free_sdei_stacks();
> >                         return 0;
> >                 }
> 
> Can you push the IS_ENABLED() checks into their respective functions?
> Then you can do something like:
> 
> 	if (init_sdei_stacks())
> 		return 0;
> 
> 	if (init_sdei_scs())
> 		goto out_free_stacks;
> 
> 	...
> 
> out_free_stacks:
> 	free_sdei_stacks();
> 	return 0;

Wait, I see you already posted a v3. Maybe I can just hack this up on top...

Will

Patch
diff mbox series

diff --git a/arch/arm64/kernel/Makefile b/arch/arm64/kernel/Makefile
index bbaf0bc4ad60..86364ab6f13f 100644
--- a/arch/arm64/kernel/Makefile
+++ b/arch/arm64/kernel/Makefile
@@ -58,7 +58,6 @@  obj-$(CONFIG_CRASH_DUMP)		+= crash_dump.o
 obj-$(CONFIG_CRASH_CORE)		+= crash_core.o
 obj-$(CONFIG_ARM_SDE_INTERFACE)		+= sdei.o
 obj-$(CONFIG_ARM64_PTR_AUTH)		+= pointer_auth.o
-obj-$(CONFIG_SHADOW_CALL_STACK)		+= scs.o
 obj-$(CONFIG_ARM64_MTE)			+= mte.o
 
 obj-y					+= vdso/ probes/
diff --git a/arch/arm64/kernel/entry.S b/arch/arm64/kernel/entry.S
index b295fb912b12..5c2ac4b5b2da 100644
--- a/arch/arm64/kernel/entry.S
+++ b/arch/arm64/kernel/entry.S
@@ -441,7 +441,7 @@  SYM_CODE_END(__swpan_exit_el0)
 
 #ifdef CONFIG_SHADOW_CALL_STACK
 	/* also switch to the irq shadow stack */
-	adr_this_cpu scs_sp, irq_shadow_call_stack, x26
+	ldr_this_cpu scs_sp, irq_shadow_call_stack_ptr, x26
 #endif
 
 9998:
@@ -1097,9 +1097,9 @@  SYM_CODE_START(__sdei_asm_handler)
 #ifdef CONFIG_SHADOW_CALL_STACK
 	/* Use a separate shadow call stack for normal and critical events */
 	cbnz	w4, 3f
-	adr_this_cpu dst=scs_sp, sym=sdei_shadow_call_stack_normal, tmp=x6
+	ldr_this_cpu dst=scs_sp, sym=sdei_shadow_call_stack_normal_ptr, tmp=x6
 	b	4f
-3:	adr_this_cpu dst=scs_sp, sym=sdei_shadow_call_stack_critical, tmp=x6
+3:	ldr_this_cpu dst=scs_sp, sym=sdei_shadow_call_stack_critical_ptr, tmp=x6
 4:
 #endif
 
diff --git a/arch/arm64/kernel/irq.c b/arch/arm64/kernel/irq.c
index 9cf2fb87584a..5b7ada9d9559 100644
--- a/arch/arm64/kernel/irq.c
+++ b/arch/arm64/kernel/irq.c
@@ -17,6 +17,7 @@ 
 #include <linux/init.h>
 #include <linux/irqchip.h>
 #include <linux/kprobes.h>
+#include <linux/scs.h>
 #include <linux/seq_file.h>
 #include <linux/vmalloc.h>
 #include <asm/daifflags.h>
@@ -27,6 +28,22 @@  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)
+{
+	int cpu;
+
+	for_each_possible_cpu(cpu)
+		per_cpu(irq_shadow_call_stack_ptr, cpu) =
+			scs_alloc(cpu_to_node(cpu));
+}
+
 #ifdef CONFIG_VMAP_STACK
 static void init_irq_stacks(void)
 {
@@ -54,6 +71,8 @@  static void init_irq_stacks(void)
 void __init init_IRQ(void)
 {
 	init_irq_stacks();
+	if (IS_ENABLED(CONFIG_SHADOW_CALL_STACK))
+		init_irq_scs();
 	irqchip_init();
 	if (!handle_arch_irq)
 		panic("No interrupt controller found.");
diff --git a/arch/arm64/kernel/scs.c b/arch/arm64/kernel/scs.c
deleted file mode 100644
index e8f7ff45dd8f..000000000000
--- a/arch/arm64/kernel/scs.c
+++ /dev/null
@@ -1,16 +0,0 @@ 
-// SPDX-License-Identifier: GPL-2.0
-/*
- * Shadow Call Stack support.
- *
- * Copyright (C) 2019 Google LLC
- */
-
-#include <linux/percpu.h>
-#include <linux/scs.h>
-
-DEFINE_SCS(irq_shadow_call_stack);
-
-#ifdef CONFIG_ARM_SDE_INTERFACE
-DEFINE_SCS(sdei_shadow_call_stack_normal);
-DEFINE_SCS(sdei_shadow_call_stack_critical);
-#endif
diff --git a/arch/arm64/kernel/sdei.c b/arch/arm64/kernel/sdei.c
index 7689f2031c0c..cbc370d3bb4f 100644
--- a/arch/arm64/kernel/sdei.c
+++ b/arch/arm64/kernel/sdei.c
@@ -7,6 +7,7 @@ 
 #include <linux/hardirq.h>
 #include <linux/irqflags.h>
 #include <linux/sched/task_stack.h>
+#include <linux/scs.h>
 #include <linux/uaccess.h>
 
 #include <asm/alternative.h>
@@ -37,6 +38,14 @@  DEFINE_PER_CPU(unsigned long *, sdei_stack_normal_ptr);
 DEFINE_PER_CPU(unsigned long *, sdei_stack_critical_ptr);
 #endif
 
+DECLARE_PER_CPU(unsigned long *, sdei_shadow_call_stack_normal_ptr);
+DECLARE_PER_CPU(unsigned long *, sdei_shadow_call_stack_critical_ptr);
+
+#ifdef CONFIG_SHADOW_CALL_STACK
+DEFINE_PER_CPU(unsigned long *, sdei_shadow_call_stack_normal_ptr);
+DEFINE_PER_CPU(unsigned long *, sdei_shadow_call_stack_critical_ptr);
+#endif
+
 static void _free_sdei_stack(unsigned long * __percpu *ptr, int cpu)
 {
 	unsigned long *p;
@@ -48,13 +57,31 @@  static void _free_sdei_stack(unsigned long * __percpu *ptr, int cpu)
 	}
 }
 
+static void _free_sdei_scs(unsigned long * __percpu *ptr, int cpu)
+{
+	void *s;
+
+	s = per_cpu(*ptr, cpu);
+	if (s) {
+		per_cpu(*ptr, cpu) = NULL;
+		scs_free(s);
+	}
+}
+
 static void free_sdei_stacks(void)
 {
 	int cpu;
 
 	for_each_possible_cpu(cpu) {
-		_free_sdei_stack(&sdei_stack_normal_ptr, cpu);
-		_free_sdei_stack(&sdei_stack_critical_ptr, cpu);
+		if (IS_ENABLED(CONFIG_VMAP_STACK)) {
+			_free_sdei_stack(&sdei_stack_normal_ptr, cpu);
+			_free_sdei_stack(&sdei_stack_critical_ptr, cpu);
+		}
+
+		if (IS_ENABLED(CONFIG_SHADOW_CALL_STACK)) {
+			_free_sdei_scs(&sdei_shadow_call_stack_normal_ptr, cpu);
+			_free_sdei_scs(&sdei_shadow_call_stack_critical_ptr, cpu);
+		}
 	}
 }
 
@@ -70,18 +97,40 @@  static int _init_sdei_stack(unsigned long * __percpu *ptr, int cpu)
 	return 0;
 }
 
+static int _init_sdei_scs(unsigned long * __percpu *ptr, int cpu)
+{
+	void *s;
+
+	s = scs_alloc(cpu_to_node(cpu));
+	if (!s)
+		return -ENOMEM;
+	per_cpu(*ptr, cpu) = s;
+
+	return 0;
+}
+
 static int init_sdei_stacks(void)
 {
 	int cpu;
 	int err = 0;
 
 	for_each_possible_cpu(cpu) {
-		err = _init_sdei_stack(&sdei_stack_normal_ptr, cpu);
-		if (err)
-			break;
-		err = _init_sdei_stack(&sdei_stack_critical_ptr, cpu);
-		if (err)
-			break;
+		if (IS_ENABLED(CONFIG_VMAP_STACK)) {
+			err = _init_sdei_stack(&sdei_stack_normal_ptr, cpu);
+			if (err)
+				break;
+			err = _init_sdei_stack(&sdei_stack_critical_ptr, cpu);
+			if (err)
+				break;
+		}
+		if (IS_ENABLED(CONFIG_SHADOW_CALL_STACK)) {
+			err = _init_sdei_scs(&sdei_shadow_call_stack_normal_ptr, cpu);
+			if (err)
+				break;
+			err = _init_sdei_scs(&sdei_shadow_call_stack_critical_ptr, cpu);
+			if (err)
+				break;
+		}
 	}
 
 	if (err)
@@ -133,10 +182,8 @@  unsigned long sdei_arch_get_entry_point(int conduit)
 		return 0;
 	}
 
-	if (IS_ENABLED(CONFIG_VMAP_STACK)) {
-		if (init_sdei_stacks())
-			return 0;
-	}
+	if (init_sdei_stacks())
+		return 0;
 
 	sdei_exit_mode = (conduit == SMCCC_CONDUIT_HVC) ? SDEI_EXIT_HVC : SDEI_EXIT_SMC;
 
diff --git a/include/linux/scs.h b/include/linux/scs.h
index 2a506c2a16f4..18122d9e17ff 100644
--- a/include/linux/scs.h
+++ b/include/linux/scs.h
@@ -22,10 +22,6 @@ 
 /* An illegal pointer value to mark the end of the shadow stack. */
 #define SCS_END_MAGIC		(0x5f6UL + POISON_POINTER_DELTA)
 
-/* Allocate a static per-CPU shadow stack */
-#define DEFINE_SCS(name)						\
-	DEFINE_PER_CPU(unsigned long [SCS_SIZE/sizeof(long)], name)	\
-
 #define task_scs(tsk)		(task_thread_info(tsk)->scs_base)
 #define task_scs_sp(tsk)	(task_thread_info(tsk)->scs_sp)