xref: /linux-6.15/arch/x86/kernel/stacktrace.c (revision c5c27a0a)
1 /*
2  * Stack trace management functions
3  *
4  *  Copyright (C) 2006-2009 Red Hat, Inc., Ingo Molnar <[email protected]>
5  */
6 #include <linux/sched.h>
7 #include <linux/sched/debug.h>
8 #include <linux/sched/task_stack.h>
9 #include <linux/stacktrace.h>
10 #include <linux/export.h>
11 #include <linux/uaccess.h>
12 #include <asm/stacktrace.h>
13 #include <asm/unwind.h>
14 
15 static int save_stack_address(struct stack_trace *trace, unsigned long addr,
16 			      bool nosched)
17 {
18 	if (nosched && in_sched_functions(addr))
19 		return 0;
20 
21 	if (trace->skip > 0) {
22 		trace->skip--;
23 		return 0;
24 	}
25 
26 	if (trace->nr_entries >= trace->max_entries)
27 		return -1;
28 
29 	trace->entries[trace->nr_entries++] = addr;
30 	return 0;
31 }
32 
33 static void noinline __save_stack_trace(struct stack_trace *trace,
34 			       struct task_struct *task, struct pt_regs *regs,
35 			       bool nosched)
36 {
37 	struct unwind_state state;
38 	unsigned long addr;
39 
40 	if (regs)
41 		save_stack_address(trace, regs->ip, nosched);
42 
43 	for (unwind_start(&state, task, regs, NULL); !unwind_done(&state);
44 	     unwind_next_frame(&state)) {
45 		addr = unwind_get_return_address(&state);
46 		if (!addr || save_stack_address(trace, addr, nosched))
47 			break;
48 	}
49 }
50 
51 /*
52  * Save stack-backtrace addresses into a stack_trace buffer.
53  */
54 void save_stack_trace(struct stack_trace *trace)
55 {
56 	trace->skip++;
57 	__save_stack_trace(trace, current, NULL, false);
58 }
59 EXPORT_SYMBOL_GPL(save_stack_trace);
60 
61 void save_stack_trace_regs(struct pt_regs *regs, struct stack_trace *trace)
62 {
63 	__save_stack_trace(trace, current, regs, false);
64 }
65 
66 void save_stack_trace_tsk(struct task_struct *tsk, struct stack_trace *trace)
67 {
68 	if (!try_get_task_stack(tsk))
69 		return;
70 
71 	if (tsk == current)
72 		trace->skip++;
73 	__save_stack_trace(trace, tsk, NULL, true);
74 
75 	put_task_stack(tsk);
76 }
77 EXPORT_SYMBOL_GPL(save_stack_trace_tsk);
78 
79 #ifdef CONFIG_HAVE_RELIABLE_STACKTRACE
80 
81 static int __always_inline
82 __save_stack_trace_reliable(struct stack_trace *trace,
83 			    struct task_struct *task)
84 {
85 	struct unwind_state state;
86 	struct pt_regs *regs;
87 	unsigned long addr;
88 
89 	for (unwind_start(&state, task, NULL, NULL);
90 	     !unwind_done(&state) && !unwind_error(&state);
91 	     unwind_next_frame(&state)) {
92 
93 		regs = unwind_get_entry_regs(&state, NULL);
94 		if (regs) {
95 			/* Success path for user tasks */
96 			if (user_mode(regs))
97 				return 0;
98 
99 			/*
100 			 * Kernel mode registers on the stack indicate an
101 			 * in-kernel interrupt or exception (e.g., preemption
102 			 * or a page fault), which can make frame pointers
103 			 * unreliable.
104 			 */
105 
106 			if (IS_ENABLED(CONFIG_FRAME_POINTER))
107 				return -EINVAL;
108 		}
109 
110 		addr = unwind_get_return_address(&state);
111 
112 		/*
113 		 * A NULL or invalid return address probably means there's some
114 		 * generated code which __kernel_text_address() doesn't know
115 		 * about.
116 		 */
117 		if (!addr)
118 			return -EINVAL;
119 
120 		if (save_stack_address(trace, addr, false))
121 			return -EINVAL;
122 	}
123 
124 	/* Check for stack corruption */
125 	if (unwind_error(&state))
126 		return -EINVAL;
127 
128 	/* Success path for non-user tasks, i.e. kthreads and idle tasks */
129 	if (!(task->flags & (PF_KTHREAD | PF_IDLE)))
130 		return -EINVAL;
131 
132 	return 0;
133 }
134 
135 /*
136  * This function returns an error if it detects any unreliable features of the
137  * stack.  Otherwise it guarantees that the stack trace is reliable.
138  *
139  * If the task is not 'current', the caller *must* ensure the task is inactive.
140  */
141 int save_stack_trace_tsk_reliable(struct task_struct *tsk,
142 				  struct stack_trace *trace)
143 {
144 	int ret;
145 
146 	/*
147 	 * If the task doesn't have a stack (e.g., a zombie), the stack is
148 	 * "reliably" empty.
149 	 */
150 	if (!try_get_task_stack(tsk))
151 		return 0;
152 
153 	ret = __save_stack_trace_reliable(trace, tsk);
154 
155 	put_task_stack(tsk);
156 
157 	return ret;
158 }
159 #endif /* CONFIG_HAVE_RELIABLE_STACKTRACE */
160 
161 /* Userspace stacktrace - based on kernel/trace/trace_sysprof.c */
162 
163 struct stack_frame_user {
164 	const void __user	*next_fp;
165 	unsigned long		ret_addr;
166 };
167 
168 static int
169 copy_stack_frame(const void __user *fp, struct stack_frame_user *frame)
170 {
171 	int ret;
172 
173 	if (!access_ok(fp, sizeof(*frame)))
174 		return 0;
175 
176 	ret = 1;
177 	pagefault_disable();
178 	if (__copy_from_user_inatomic(frame, fp, sizeof(*frame)))
179 		ret = 0;
180 	pagefault_enable();
181 
182 	return ret;
183 }
184 
185 static inline void __save_stack_trace_user(struct stack_trace *trace)
186 {
187 	const struct pt_regs *regs = task_pt_regs(current);
188 	const void __user *fp = (const void __user *)regs->bp;
189 
190 	if (trace->nr_entries < trace->max_entries)
191 		trace->entries[trace->nr_entries++] = regs->ip;
192 
193 	while (trace->nr_entries < trace->max_entries) {
194 		struct stack_frame_user frame;
195 
196 		frame.next_fp = NULL;
197 		frame.ret_addr = 0;
198 		if (!copy_stack_frame(fp, &frame))
199 			break;
200 		if ((unsigned long)fp < regs->sp)
201 			break;
202 		if (frame.ret_addr) {
203 			trace->entries[trace->nr_entries++] =
204 				frame.ret_addr;
205 		}
206 		if (fp == frame.next_fp)
207 			break;
208 		fp = frame.next_fp;
209 	}
210 }
211 
212 void save_stack_trace_user(struct stack_trace *trace)
213 {
214 	/*
215 	 * Trace user stack if we are not a kernel thread
216 	 */
217 	if (current->mm)
218 		__save_stack_trace_user(trace);
219 }
220