1250c2277SThomas Gleixner /* 2250c2277SThomas Gleixner * Stack trace management functions 3250c2277SThomas Gleixner * 48f47e163SIngo Molnar * Copyright (C) 2006-2009 Red Hat, Inc., Ingo Molnar <mingo@redhat.com> 5250c2277SThomas Gleixner */ 6250c2277SThomas Gleixner #include <linux/sched.h> 7b17b0153SIngo Molnar #include <linux/sched/debug.h> 868db0cf1SIngo Molnar #include <linux/sched/task_stack.h> 9250c2277SThomas Gleixner #include <linux/stacktrace.h> 10186f4360SPaul Gortmaker #include <linux/export.h> 1102b67518STörök Edwin #include <linux/uaccess.h> 12250c2277SThomas Gleixner #include <asm/stacktrace.h> 1349a612c6SJosh Poimboeuf #include <asm/unwind.h> 14250c2277SThomas Gleixner 1549a612c6SJosh Poimboeuf static int save_stack_address(struct stack_trace *trace, unsigned long addr, 1649a612c6SJosh Poimboeuf bool nosched) 17250c2277SThomas Gleixner { 18018378c5SOleg Nesterov if (nosched && in_sched_functions(addr)) 19568b329aSAlexei Starovoitov return 0; 2049a612c6SJosh Poimboeuf 21250c2277SThomas Gleixner if (trace->skip > 0) { 22250c2277SThomas Gleixner trace->skip--; 23568b329aSAlexei Starovoitov return 0; 24250c2277SThomas Gleixner } 2549a612c6SJosh Poimboeuf 2649a612c6SJosh Poimboeuf if (trace->nr_entries >= trace->max_entries) 2749a612c6SJosh Poimboeuf return -1; 2849a612c6SJosh Poimboeuf 29250c2277SThomas Gleixner trace->entries[trace->nr_entries++] = addr; 30568b329aSAlexei Starovoitov return 0; 31250c2277SThomas Gleixner } 32250c2277SThomas Gleixner 3377072f09SVlastimil Babka static void noinline __save_stack_trace(struct stack_trace *trace, 3449a612c6SJosh Poimboeuf struct task_struct *task, struct pt_regs *regs, 3549a612c6SJosh Poimboeuf bool nosched) 36018378c5SOleg Nesterov { 3749a612c6SJosh Poimboeuf struct unwind_state state; 3849a612c6SJosh Poimboeuf unsigned long addr; 3949a612c6SJosh Poimboeuf 4049a612c6SJosh Poimboeuf if (regs) 4149a612c6SJosh Poimboeuf save_stack_address(trace, regs->ip, nosched); 4249a612c6SJosh Poimboeuf 4349a612c6SJosh Poimboeuf for (unwind_start(&state, task, regs, NULL); !unwind_done(&state); 4449a612c6SJosh Poimboeuf unwind_next_frame(&state)) { 4549a612c6SJosh Poimboeuf addr = unwind_get_return_address(&state); 4649a612c6SJosh Poimboeuf if (!addr || save_stack_address(trace, addr, nosched)) 4749a612c6SJosh Poimboeuf break; 48018378c5SOleg Nesterov } 49018378c5SOleg Nesterov 5049a612c6SJosh Poimboeuf if (trace->nr_entries < trace->max_entries) 5149a612c6SJosh Poimboeuf trace->entries[trace->nr_entries++] = ULONG_MAX; 529745512cSArjan van de Ven } 539745512cSArjan van de Ven 54250c2277SThomas Gleixner /* 55250c2277SThomas Gleixner * Save stack-backtrace addresses into a stack_trace buffer. 56250c2277SThomas Gleixner */ 57250c2277SThomas Gleixner void save_stack_trace(struct stack_trace *trace) 58250c2277SThomas Gleixner { 5977072f09SVlastimil Babka trace->skip++; 6049a612c6SJosh Poimboeuf __save_stack_trace(trace, current, NULL, false); 61250c2277SThomas Gleixner } 628594698eSIngo Molnar EXPORT_SYMBOL_GPL(save_stack_trace); 639745512cSArjan van de Ven 6439581062SMasami Hiramatsu void save_stack_trace_regs(struct pt_regs *regs, struct stack_trace *trace) 65acc6be54SVegard Nossum { 6649a612c6SJosh Poimboeuf __save_stack_trace(trace, current, regs, false); 67acc6be54SVegard Nossum } 68acc6be54SVegard Nossum 699745512cSArjan van de Ven void save_stack_trace_tsk(struct task_struct *tsk, struct stack_trace *trace) 709745512cSArjan van de Ven { 711959a601SAndy Lutomirski if (!try_get_task_stack(tsk)) 721959a601SAndy Lutomirski return; 731959a601SAndy Lutomirski 7477072f09SVlastimil Babka if (tsk == current) 7577072f09SVlastimil Babka trace->skip++; 7649a612c6SJosh Poimboeuf __save_stack_trace(trace, tsk, NULL, true); 771959a601SAndy Lutomirski 781959a601SAndy Lutomirski put_task_stack(tsk); 799745512cSArjan van de Ven } 808594698eSIngo Molnar EXPORT_SYMBOL_GPL(save_stack_trace_tsk); 8102b67518STörök Edwin 82af085d90SJosh Poimboeuf #ifdef CONFIG_HAVE_RELIABLE_STACKTRACE 83af085d90SJosh Poimboeuf 8477072f09SVlastimil Babka static int __always_inline 8577072f09SVlastimil Babka __save_stack_trace_reliable(struct stack_trace *trace, 86af085d90SJosh Poimboeuf struct task_struct *task) 87af085d90SJosh Poimboeuf { 88af085d90SJosh Poimboeuf struct unwind_state state; 89af085d90SJosh Poimboeuf struct pt_regs *regs; 90af085d90SJosh Poimboeuf unsigned long addr; 91af085d90SJosh Poimboeuf 92*441ccc35SJiri Slaby for (unwind_start(&state, task, NULL, NULL); 93*441ccc35SJiri Slaby !unwind_done(&state) && !unwind_error(&state); 94af085d90SJosh Poimboeuf unwind_next_frame(&state)) { 95af085d90SJosh Poimboeuf 96a9cdbe72SJosh Poimboeuf regs = unwind_get_entry_regs(&state, NULL); 97af085d90SJosh Poimboeuf if (regs) { 98*441ccc35SJiri Slaby /* Success path for user tasks */ 99*441ccc35SJiri Slaby if (user_mode(regs)) 100*441ccc35SJiri Slaby goto success; 101*441ccc35SJiri Slaby 102af085d90SJosh Poimboeuf /* 103af085d90SJosh Poimboeuf * Kernel mode registers on the stack indicate an 104af085d90SJosh Poimboeuf * in-kernel interrupt or exception (e.g., preemption 105af085d90SJosh Poimboeuf * or a page fault), which can make frame pointers 106af085d90SJosh Poimboeuf * unreliable. 107af085d90SJosh Poimboeuf */ 108af085d90SJosh Poimboeuf 109*441ccc35SJiri Slaby return -EINVAL; 110af085d90SJosh Poimboeuf } 111af085d90SJosh Poimboeuf 112af085d90SJosh Poimboeuf addr = unwind_get_return_address(&state); 113af085d90SJosh Poimboeuf 114af085d90SJosh Poimboeuf /* 115af085d90SJosh Poimboeuf * A NULL or invalid return address probably means there's some 116af085d90SJosh Poimboeuf * generated code which __kernel_text_address() doesn't know 117af085d90SJosh Poimboeuf * about. 118af085d90SJosh Poimboeuf */ 11917426923SJiri Slaby if (!addr) 120af085d90SJosh Poimboeuf return -EINVAL; 121af085d90SJosh Poimboeuf 122af085d90SJosh Poimboeuf if (save_stack_address(trace, addr, false)) 123af085d90SJosh Poimboeuf return -EINVAL; 124af085d90SJosh Poimboeuf } 125af085d90SJosh Poimboeuf 126af085d90SJosh Poimboeuf /* Check for stack corruption */ 12717426923SJiri Slaby if (unwind_error(&state)) 128af085d90SJosh Poimboeuf return -EINVAL; 129af085d90SJosh Poimboeuf 130*441ccc35SJiri Slaby /* Success path for non-user tasks, i.e. kthreads and idle tasks */ 131*441ccc35SJiri Slaby if (!(task->flags & (PF_KTHREAD | PF_IDLE))) 132*441ccc35SJiri Slaby return -EINVAL; 133*441ccc35SJiri Slaby 134*441ccc35SJiri Slaby success: 135af085d90SJosh Poimboeuf if (trace->nr_entries < trace->max_entries) 136af085d90SJosh Poimboeuf trace->entries[trace->nr_entries++] = ULONG_MAX; 137af085d90SJosh Poimboeuf 138af085d90SJosh Poimboeuf return 0; 139af085d90SJosh Poimboeuf } 140af085d90SJosh Poimboeuf 141af085d90SJosh Poimboeuf /* 142af085d90SJosh Poimboeuf * This function returns an error if it detects any unreliable features of the 143af085d90SJosh Poimboeuf * stack. Otherwise it guarantees that the stack trace is reliable. 144af085d90SJosh Poimboeuf * 145af085d90SJosh Poimboeuf * If the task is not 'current', the caller *must* ensure the task is inactive. 146af085d90SJosh Poimboeuf */ 147af085d90SJosh Poimboeuf int save_stack_trace_tsk_reliable(struct task_struct *tsk, 148af085d90SJosh Poimboeuf struct stack_trace *trace) 149af085d90SJosh Poimboeuf { 150af085d90SJosh Poimboeuf int ret; 151af085d90SJosh Poimboeuf 1526454b3bdSJosh Poimboeuf /* 1536454b3bdSJosh Poimboeuf * If the task doesn't have a stack (e.g., a zombie), the stack is 1546454b3bdSJosh Poimboeuf * "reliably" empty. 1556454b3bdSJosh Poimboeuf */ 156af085d90SJosh Poimboeuf if (!try_get_task_stack(tsk)) 1576454b3bdSJosh Poimboeuf return 0; 158af085d90SJosh Poimboeuf 159af085d90SJosh Poimboeuf ret = __save_stack_trace_reliable(trace, tsk); 160af085d90SJosh Poimboeuf 161af085d90SJosh Poimboeuf put_task_stack(tsk); 162af085d90SJosh Poimboeuf 163af085d90SJosh Poimboeuf return ret; 164af085d90SJosh Poimboeuf } 165af085d90SJosh Poimboeuf #endif /* CONFIG_HAVE_RELIABLE_STACKTRACE */ 166af085d90SJosh Poimboeuf 16702b67518STörök Edwin /* Userspace stacktrace - based on kernel/trace/trace_sysprof.c */ 16802b67518STörök Edwin 169c9cf4dbbSFrederic Weisbecker struct stack_frame_user { 17002b67518STörök Edwin const void __user *next_fp; 1718d7c6a96STörök Edwin unsigned long ret_addr; 17202b67518STörök Edwin }; 17302b67518STörök Edwin 174c9cf4dbbSFrederic Weisbecker static int 175c9cf4dbbSFrederic Weisbecker copy_stack_frame(const void __user *fp, struct stack_frame_user *frame) 17602b67518STörök Edwin { 17702b67518STörök Edwin int ret; 17802b67518STörök Edwin 17902b67518STörök Edwin if (!access_ok(VERIFY_READ, fp, sizeof(*frame))) 18002b67518STörök Edwin return 0; 18102b67518STörök Edwin 18202b67518STörök Edwin ret = 1; 18302b67518STörök Edwin pagefault_disable(); 18402b67518STörök Edwin if (__copy_from_user_inatomic(frame, fp, sizeof(*frame))) 18502b67518STörök Edwin ret = 0; 18602b67518STörök Edwin pagefault_enable(); 18702b67518STörök Edwin 18802b67518STörök Edwin return ret; 18902b67518STörök Edwin } 19002b67518STörök Edwin 1918d7c6a96STörök Edwin static inline void __save_stack_trace_user(struct stack_trace *trace) 19202b67518STörök Edwin { 19302b67518STörök Edwin const struct pt_regs *regs = task_pt_regs(current); 19402b67518STörök Edwin const void __user *fp = (const void __user *)regs->bp; 19502b67518STörök Edwin 19602b67518STörök Edwin if (trace->nr_entries < trace->max_entries) 19702b67518STörök Edwin trace->entries[trace->nr_entries++] = regs->ip; 19802b67518STörök Edwin 19902b67518STörök Edwin while (trace->nr_entries < trace->max_entries) { 200c9cf4dbbSFrederic Weisbecker struct stack_frame_user frame; 2018d7c6a96STörök Edwin 20202b67518STörök Edwin frame.next_fp = NULL; 2038d7c6a96STörök Edwin frame.ret_addr = 0; 20402b67518STörök Edwin if (!copy_stack_frame(fp, &frame)) 20502b67518STörök Edwin break; 20602b67518STörök Edwin if ((unsigned long)fp < regs->sp) 20702b67518STörök Edwin break; 2088d7c6a96STörök Edwin if (frame.ret_addr) { 20902b67518STörök Edwin trace->entries[trace->nr_entries++] = 2108d7c6a96STörök Edwin frame.ret_addr; 2118d7c6a96STörök Edwin } 21202b67518STörök Edwin if (fp == frame.next_fp) 21302b67518STörök Edwin break; 21402b67518STörök Edwin fp = frame.next_fp; 21502b67518STörök Edwin } 21602b67518STörök Edwin } 2178d7c6a96STörök Edwin 2188d7c6a96STörök Edwin void save_stack_trace_user(struct stack_trace *trace) 2198d7c6a96STörök Edwin { 2208d7c6a96STörök Edwin /* 2218d7c6a96STörök Edwin * Trace user stack if we are not a kernel thread 2228d7c6a96STörök Edwin */ 2238d7c6a96STörök Edwin if (current->mm) { 2248d7c6a96STörök Edwin __save_stack_trace_user(trace); 2258d7c6a96STörök Edwin } 22602b67518STörök Edwin if (trace->nr_entries < trace->max_entries) 22702b67518STörök Edwin trace->entries[trace->nr_entries++] = ULONG_MAX; 22802b67518STörök Edwin } 229