Thomas Gleixner | caab277 | 2019-06-03 07:44:50 +0200 | [diff] [blame^] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Catalin Marinas | 60ffc30 | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Stack tracing support |
| 4 | * |
| 5 | * Copyright (C) 2012 ARM Ltd. |
Catalin Marinas | 60ffc30 | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 6 | */ |
| 7 | #include <linux/kernel.h> |
| 8 | #include <linux/export.h> |
AKASHI Takahiro | 20380bb | 2015-12-15 17:33:41 +0900 | [diff] [blame] | 9 | #include <linux/ftrace.h> |
Catalin Marinas | 60ffc30 | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 10 | #include <linux/sched.h> |
Ingo Molnar | b17b015 | 2017-02-08 18:51:35 +0100 | [diff] [blame] | 11 | #include <linux/sched/debug.h> |
Ingo Molnar | 68db0cf | 2017-02-08 18:51:37 +0100 | [diff] [blame] | 12 | #include <linux/sched/task_stack.h> |
Catalin Marinas | 60ffc30 | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 13 | #include <linux/stacktrace.h> |
| 14 | |
AKASHI Takahiro | 132cd88 | 2015-12-04 11:02:26 +0000 | [diff] [blame] | 15 | #include <asm/irq.h> |
Mark Rutland | a9ea001 | 2016-11-03 20:23:05 +0000 | [diff] [blame] | 16 | #include <asm/stack_pointer.h> |
Catalin Marinas | 60ffc30 | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 17 | #include <asm/stacktrace.h> |
| 18 | |
| 19 | /* |
| 20 | * AArch64 PCS assigns the frame pointer to x29. |
| 21 | * |
| 22 | * A simple function prologue looks like this: |
| 23 | * sub sp, sp, #0x10 |
| 24 | * stp x29, x30, [sp] |
| 25 | * mov x29, sp |
| 26 | * |
| 27 | * A simple function epilogue looks like this: |
| 28 | * mov sp, x29 |
| 29 | * ldp x29, x30, [sp] |
| 30 | * add sp, sp, #0x10 |
| 31 | */ |
AKASHI Takahiro | fe13f95 | 2015-12-15 17:33:40 +0900 | [diff] [blame] | 32 | int notrace unwind_frame(struct task_struct *tsk, struct stackframe *frame) |
Catalin Marinas | 60ffc30 | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 33 | { |
Catalin Marinas | 60ffc30 | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 34 | unsigned long fp = frame->fp; |
Ard Biesheuvel | c736533 | 2017-07-22 12:48:34 +0100 | [diff] [blame] | 35 | |
| 36 | if (fp & 0xf) |
| 37 | return -EINVAL; |
AKASHI Takahiro | 132cd88 | 2015-12-04 11:02:26 +0000 | [diff] [blame] | 38 | |
Mark Rutland | b5e7307 | 2016-09-23 17:55:05 +0100 | [diff] [blame] | 39 | if (!tsk) |
| 40 | tsk = current; |
| 41 | |
Laura Abbott | 8a1ccfb | 2018-07-20 14:41:53 -0700 | [diff] [blame] | 42 | if (!on_accessible_stack(tsk, fp, NULL)) |
Catalin Marinas | 60ffc30 | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 43 | return -EINVAL; |
| 44 | |
Yang Shi | bcaf669 | 2016-02-08 09:13:09 -0800 | [diff] [blame] | 45 | frame->fp = READ_ONCE_NOCHECK(*(unsigned long *)(fp)); |
| 46 | frame->pc = READ_ONCE_NOCHECK(*(unsigned long *)(fp + 8)); |
Catalin Marinas | 60ffc30 | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 47 | |
AKASHI Takahiro | 20380bb | 2015-12-15 17:33:41 +0900 | [diff] [blame] | 48 | #ifdef CONFIG_FUNCTION_GRAPH_TRACER |
Mark Rutland | b5e7307 | 2016-09-23 17:55:05 +0100 | [diff] [blame] | 49 | if (tsk->ret_stack && |
AKASHI Takahiro | 20380bb | 2015-12-15 17:33:41 +0900 | [diff] [blame] | 50 | (frame->pc == (unsigned long)return_to_handler)) { |
Steven Rostedt (VMware) | a448276 | 2018-12-07 13:13:28 -0500 | [diff] [blame] | 51 | struct ftrace_ret_stack *ret_stack; |
AKASHI Takahiro | 20380bb | 2015-12-15 17:33:41 +0900 | [diff] [blame] | 52 | /* |
| 53 | * This is a case where function graph tracer has |
| 54 | * modified a return address (LR) in a stack frame |
| 55 | * to hook a function return. |
| 56 | * So replace it to an original value. |
| 57 | */ |
Steven Rostedt (VMware) | a448276 | 2018-12-07 13:13:28 -0500 | [diff] [blame] | 58 | ret_stack = ftrace_graph_get_ret_stack(tsk, frame->graph++); |
| 59 | if (WARN_ON_ONCE(!ret_stack)) |
| 60 | return -EINVAL; |
| 61 | frame->pc = ret_stack->ret; |
AKASHI Takahiro | 20380bb | 2015-12-15 17:33:41 +0900 | [diff] [blame] | 62 | } |
| 63 | #endif /* CONFIG_FUNCTION_GRAPH_TRACER */ |
| 64 | |
AKASHI Takahiro | 132cd88 | 2015-12-04 11:02:26 +0000 | [diff] [blame] | 65 | /* |
Ard Biesheuvel | 7326749 | 2017-07-22 18:45:33 +0100 | [diff] [blame] | 66 | * Frames created upon entry from EL0 have NULL FP and PC values, so |
| 67 | * don't bother reporting these. Frames created by __noreturn functions |
| 68 | * might have a valid FP even if PC is bogus, so only terminate where |
| 69 | * both are NULL. |
AKASHI Takahiro | 132cd88 | 2015-12-04 11:02:26 +0000 | [diff] [blame] | 70 | */ |
Ard Biesheuvel | 7326749 | 2017-07-22 18:45:33 +0100 | [diff] [blame] | 71 | if (!frame->fp && !frame->pc) |
| 72 | return -EINVAL; |
AKASHI Takahiro | 132cd88 | 2015-12-04 11:02:26 +0000 | [diff] [blame] | 73 | |
Catalin Marinas | 60ffc30 | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 74 | return 0; |
| 75 | } |
| 76 | |
AKASHI Takahiro | fe13f95 | 2015-12-15 17:33:40 +0900 | [diff] [blame] | 77 | void notrace walk_stackframe(struct task_struct *tsk, struct stackframe *frame, |
Catalin Marinas | 60ffc30 | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 78 | int (*fn)(struct stackframe *, void *), void *data) |
| 79 | { |
| 80 | while (1) { |
| 81 | int ret; |
| 82 | |
| 83 | if (fn(frame, data)) |
| 84 | break; |
AKASHI Takahiro | fe13f95 | 2015-12-15 17:33:40 +0900 | [diff] [blame] | 85 | ret = unwind_frame(tsk, frame); |
Catalin Marinas | 60ffc30 | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 86 | if (ret < 0) |
| 87 | break; |
| 88 | } |
| 89 | } |
Catalin Marinas | 60ffc30 | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 90 | |
| 91 | #ifdef CONFIG_STACKTRACE |
| 92 | struct stack_trace_data { |
| 93 | struct stack_trace *trace; |
| 94 | unsigned int no_sched_functions; |
| 95 | unsigned int skip; |
| 96 | }; |
| 97 | |
| 98 | static int save_trace(struct stackframe *frame, void *d) |
| 99 | { |
| 100 | struct stack_trace_data *data = d; |
| 101 | struct stack_trace *trace = data->trace; |
| 102 | unsigned long addr = frame->pc; |
| 103 | |
| 104 | if (data->no_sched_functions && in_sched_functions(addr)) |
| 105 | return 0; |
| 106 | if (data->skip) { |
| 107 | data->skip--; |
| 108 | return 0; |
| 109 | } |
| 110 | |
| 111 | trace->entries[trace->nr_entries++] = addr; |
| 112 | |
| 113 | return trace->nr_entries >= trace->max_entries; |
| 114 | } |
| 115 | |
Pratyush Anand | 98ab10e | 2016-09-05 08:03:16 +0530 | [diff] [blame] | 116 | void save_stack_trace_regs(struct pt_regs *regs, struct stack_trace *trace) |
| 117 | { |
| 118 | struct stack_trace_data data; |
| 119 | struct stackframe frame; |
| 120 | |
| 121 | data.trace = trace; |
| 122 | data.skip = trace->skip; |
| 123 | data.no_sched_functions = 0; |
| 124 | |
| 125 | frame.fp = regs->regs[29]; |
Pratyush Anand | 98ab10e | 2016-09-05 08:03:16 +0530 | [diff] [blame] | 126 | frame.pc = regs->pc; |
| 127 | #ifdef CONFIG_FUNCTION_GRAPH_TRACER |
Steven Rostedt (VMware) | a448276 | 2018-12-07 13:13:28 -0500 | [diff] [blame] | 128 | frame.graph = 0; |
Pratyush Anand | 98ab10e | 2016-09-05 08:03:16 +0530 | [diff] [blame] | 129 | #endif |
| 130 | |
| 131 | walk_stackframe(current, &frame, save_trace, &data); |
Pratyush Anand | 98ab10e | 2016-09-05 08:03:16 +0530 | [diff] [blame] | 132 | } |
William Cohen | c82fd1e | 2019-03-01 15:00:41 -0500 | [diff] [blame] | 133 | EXPORT_SYMBOL_GPL(save_stack_trace_regs); |
Pratyush Anand | 98ab10e | 2016-09-05 08:03:16 +0530 | [diff] [blame] | 134 | |
Prakash Gupta | bb53c82 | 2017-09-13 16:28:32 -0700 | [diff] [blame] | 135 | static noinline void __save_stack_trace(struct task_struct *tsk, |
| 136 | struct stack_trace *trace, unsigned int nosched) |
Catalin Marinas | 60ffc30 | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 137 | { |
| 138 | struct stack_trace_data data; |
| 139 | struct stackframe frame; |
| 140 | |
Mark Rutland | 9bbd4c5 | 2016-11-03 20:23:08 +0000 | [diff] [blame] | 141 | if (!try_get_task_stack(tsk)) |
| 142 | return; |
| 143 | |
Catalin Marinas | 60ffc30 | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 144 | data.trace = trace; |
| 145 | data.skip = trace->skip; |
Prakash Gupta | bb53c82 | 2017-09-13 16:28:32 -0700 | [diff] [blame] | 146 | data.no_sched_functions = nosched; |
Catalin Marinas | 60ffc30 | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 147 | |
| 148 | if (tsk != current) { |
Catalin Marinas | 60ffc30 | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 149 | frame.fp = thread_saved_fp(tsk); |
Catalin Marinas | 60ffc30 | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 150 | frame.pc = thread_saved_pc(tsk); |
| 151 | } else { |
Prakash Gupta | bb53c82 | 2017-09-13 16:28:32 -0700 | [diff] [blame] | 152 | /* We don't want this function nor the caller */ |
| 153 | data.skip += 2; |
Catalin Marinas | 60ffc30 | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 154 | frame.fp = (unsigned long)__builtin_frame_address(0); |
Prakash Gupta | bb53c82 | 2017-09-13 16:28:32 -0700 | [diff] [blame] | 155 | frame.pc = (unsigned long)__save_stack_trace; |
Catalin Marinas | 60ffc30 | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 156 | } |
AKASHI Takahiro | 20380bb | 2015-12-15 17:33:41 +0900 | [diff] [blame] | 157 | #ifdef CONFIG_FUNCTION_GRAPH_TRACER |
Steven Rostedt (VMware) | a448276 | 2018-12-07 13:13:28 -0500 | [diff] [blame] | 158 | frame.graph = 0; |
AKASHI Takahiro | 20380bb | 2015-12-15 17:33:41 +0900 | [diff] [blame] | 159 | #endif |
Catalin Marinas | 60ffc30 | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 160 | |
AKASHI Takahiro | fe13f95 | 2015-12-15 17:33:40 +0900 | [diff] [blame] | 161 | walk_stackframe(tsk, &frame, save_trace, &data); |
Mark Rutland | 9bbd4c5 | 2016-11-03 20:23:08 +0000 | [diff] [blame] | 162 | |
| 163 | put_task_stack(tsk); |
Catalin Marinas | 60ffc30 | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 164 | } |
Dustin Brown | e27c7fa | 2017-06-13 11:40:56 -0700 | [diff] [blame] | 165 | EXPORT_SYMBOL_GPL(save_stack_trace_tsk); |
Catalin Marinas | 60ffc30 | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 166 | |
Prakash Gupta | bb53c82 | 2017-09-13 16:28:32 -0700 | [diff] [blame] | 167 | void save_stack_trace_tsk(struct task_struct *tsk, struct stack_trace *trace) |
| 168 | { |
| 169 | __save_stack_trace(tsk, trace, 1); |
| 170 | } |
| 171 | |
Catalin Marinas | 60ffc30 | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 172 | void save_stack_trace(struct stack_trace *trace) |
| 173 | { |
Prakash Gupta | bb53c82 | 2017-09-13 16:28:32 -0700 | [diff] [blame] | 174 | __save_stack_trace(current, trace, 0); |
Catalin Marinas | 60ffc30 | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 175 | } |
Prakash Gupta | bb53c82 | 2017-09-13 16:28:32 -0700 | [diff] [blame] | 176 | |
Catalin Marinas | 60ffc30 | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 177 | EXPORT_SYMBOL_GPL(save_stack_trace); |
| 178 | #endif |