diff mbox series

[2/2] ppc64le save_stack_trace_tsk_reliable (Was: HAVE_RELIABLE_STACKTRACE)

Message ID 20180227160924.GA19111@lst.de (mailing list archive)
State Superseded
Headers show
Series On ppc64le we HAVE_RELIABLE_STACKTRACE | expand

Commit Message

Torsten Duwe Feb. 27, 2018, 4:09 p.m. UTC
On Tue, Dec 12, 2017 at 01:12:37PM +0100, Miroslav Benes wrote:
> 
> I think that this is not enough. You need to also implement 
> save_stack_trace_tsk_reliable() for powerpc defined as __weak in 
> kernel/stacktrace.c.

So here is my initial proposal. I'd really like to get the successful
exit stricter, i.e. hit the initial stack value exactly instead of just
a window. Also, the check for kernel code looks clumsy IMHO. IOW:
Comments more than welcome!

Most of it is Copy&Waste, nonetheless:

Signed-off-by: Torsten Duwe <duwe@suse.de>

Comments

Balbir Singh March 8, 2018, 9:43 p.m. UTC | #1
On Tue, 27 Feb 2018 17:09:24 +0100
Torsten Duwe <duwe@lst.de> wrote:

> On Tue, Dec 12, 2017 at 01:12:37PM +0100, Miroslav Benes wrote:
> > 
> > I think that this is not enough. You need to also implement 
> > save_stack_trace_tsk_reliable() for powerpc defined as __weak in 
> > kernel/stacktrace.c.  
> 
> So here is my initial proposal. I'd really like to get the successful
> exit stricter, i.e. hit the initial stack value exactly instead of just
> a window. Also, the check for kernel code looks clumsy IMHO. IOW:
> Comments more than welcome!
> 
> Most of it is Copy&Waste, nonetheless:

:)

> 
> Signed-off-by: Torsten Duwe <duwe@suse.de>
> 
> 
> diff --git a/arch/powerpc/kernel/stacktrace.c b/arch/powerpc/kernel/stacktrace.c
> index d534ed901538..e08af49e71d0 100644
> --- a/arch/powerpc/kernel/stacktrace.c
> +++ b/arch/powerpc/kernel/stacktrace.c
> @@ -13,6 +13,7 @@
>  #include <linux/export.h>
>  #include <linux/sched.h>
>  #include <linux/sched/debug.h>
> +#include <linux/sched/task_stack.h>
>  #include <linux/stacktrace.h>
>  #include <asm/ptrace.h>
>  #include <asm/processor.h>
> @@ -76,3 +77,58 @@ save_stack_trace_regs(struct pt_regs *regs, struct stack_trace *trace)
>  	save_context_stack(trace, regs->gpr[1], current, 0);
>  }
>  EXPORT_SYMBOL_GPL(save_stack_trace_regs);
> +
> +#ifdef CONFIG_HAVE_RELIABLE_STACKTRACE
> +int
> +save_stack_trace_tsk_reliable(struct task_struct *tsk,
> +                              struct stack_trace *trace)

Just double checking this is called under the task_rq_lock, so its safe
to call task_stack_page() as opposed to try_get_task_stack()

> +{
> +	unsigned long sp;
> +	unsigned long stack_page = (unsigned long)task_stack_page(tsk);
> +	/* the last frame (unwinding first) may not yet have saved its LR onto the stack. */
> +	int firstframe = 1;
> +
> +	if (tsk == current)
> +		sp = current_stack_pointer();
> +	else
> +		sp = tsk->thread.ksp;
> +
> +	if (sp < stack_page + sizeof(struct thread_struct)
> +	    || sp > stack_page + THREAD_SIZE - STACK_FRAME_OVERHEAD)
> +		return 1;

Some of this is already present in validate_sp(), it also validates
irq stacks, should we just reuse that?

> +	
> +	for (;;) {
> +		unsigned long *stack = (unsigned long *) sp;
> +		unsigned long newsp, ip;
> +
> +		newsp = stack[0];
> +		/* Stack grows downwards; unwinder may only go up */
> +		if (newsp <= sp)
> +			return 1;
> +
> +		if (newsp >= stack_page + THREAD_SIZE)
> +			return 1; /* invalid backlink, too far up! */
> +
> +		/* Examine the saved LR: it must point into kernel code. */
> +		ip = stack[STACK_FRAME_LR_SAVE];
> +		if ( (ip & 0xEFFF000000000000) != CONFIG_KERNEL_START
> +		     && !firstframe)
> +			return 1;
> +		firstframe = 0;
> +
> +		if (!trace->skip)
> +			trace->entries[trace->nr_entries++] = ip;
> +		else
> +			trace->skip--;
> +
> +		if (newsp > stack_page + THREAD_SIZE - STACK_FRAME_OVERHEAD)
> +			break; /* hit the window for last frame */
> +
> +		if (trace->nr_entries >= trace->max_entries)
> +			return -E2BIG;
> +
> +		sp = newsp;
> +	}
> +	return 0;
> +}
> +#endif /* CONFIG_HAVE_RELIABLE_STACKTRACE */
> 

Looks good to me otherwise.

Acked-by: Balbir Singh <bsingharora@gmail.com>
Torsten Duwe March 9, 2018, 3:54 p.m. UTC | #2
On Fri, 9 Mar 2018 08:43:33 +1100
Balbir Singh <bsingharora@gmail.com> wrote:

> On Tue, 27 Feb 2018 17:09:24 +0100
> Torsten Duwe <duwe@lst.de> wrote:

> > +save_stack_trace_tsk_reliable(struct task_struct *tsk,
> > +                              struct stack_trace *trace)  
> 
> Just double checking this is called under the task_rq_lock, so its
> safe to call task_stack_page() as opposed to try_get_task_stack()

Yes. IIRC a comment at the call site mentioned it.

[...]
> > +	if (sp < stack_page + sizeof(struct thread_struct)
> > +	    || sp > stack_page + THREAD_SIZE -
> > STACK_FRAME_OVERHEAD)
> > +		return 1;  
> 
> Some of this is already present in validate_sp(), it also validates
> irq stacks, should we just reuse that?

This goes a bit along one of Josh's points; I'll answer there, OK?

[...]

> Looks good to me otherwise.
> 
> Acked-by: Balbir Singh <bsingharora@gmail.com>
Thanks.

	Torsten
diff mbox series

Patch

diff --git a/arch/powerpc/kernel/stacktrace.c b/arch/powerpc/kernel/stacktrace.c
index d534ed901538..e08af49e71d0 100644
--- a/arch/powerpc/kernel/stacktrace.c
+++ b/arch/powerpc/kernel/stacktrace.c
@@ -13,6 +13,7 @@ 
 #include <linux/export.h>
 #include <linux/sched.h>
 #include <linux/sched/debug.h>
+#include <linux/sched/task_stack.h>
 #include <linux/stacktrace.h>
 #include <asm/ptrace.h>
 #include <asm/processor.h>
@@ -76,3 +77,58 @@  save_stack_trace_regs(struct pt_regs *regs, struct stack_trace *trace)
 	save_context_stack(trace, regs->gpr[1], current, 0);
 }
 EXPORT_SYMBOL_GPL(save_stack_trace_regs);
+
+#ifdef CONFIG_HAVE_RELIABLE_STACKTRACE
+int
+save_stack_trace_tsk_reliable(struct task_struct *tsk,
+                              struct stack_trace *trace)
+{
+	unsigned long sp;
+	unsigned long stack_page = (unsigned long)task_stack_page(tsk);
+	/* the last frame (unwinding first) may not yet have saved its LR onto the stack. */
+	int firstframe = 1;
+
+	if (tsk == current)
+		sp = current_stack_pointer();
+	else
+		sp = tsk->thread.ksp;
+
+	if (sp < stack_page + sizeof(struct thread_struct)
+	    || sp > stack_page + THREAD_SIZE - STACK_FRAME_OVERHEAD)
+		return 1;
+	
+	for (;;) {
+		unsigned long *stack = (unsigned long *) sp;
+		unsigned long newsp, ip;
+
+		newsp = stack[0];
+		/* Stack grows downwards; unwinder may only go up */
+		if (newsp <= sp)
+			return 1;
+
+		if (newsp >= stack_page + THREAD_SIZE)
+			return 1; /* invalid backlink, too far up! */
+
+		/* Examine the saved LR: it must point into kernel code. */
+		ip = stack[STACK_FRAME_LR_SAVE];
+		if ( (ip & 0xEFFF000000000000) != CONFIG_KERNEL_START
+		     && !firstframe)
+			return 1;
+		firstframe = 0;
+
+		if (!trace->skip)
+			trace->entries[trace->nr_entries++] = ip;
+		else
+			trace->skip--;
+
+		if (newsp > stack_page + THREAD_SIZE - STACK_FRAME_OVERHEAD)
+			break; /* hit the window for last frame */
+
+		if (trace->nr_entries >= trace->max_entries)
+			return -E2BIG;
+
+		sp = newsp;
+	}
+	return 0;
+}
+#endif /* CONFIG_HAVE_RELIABLE_STACKTRACE */