Skip to content

Commit e18bccc

Browse files
jpoimboeingomolnar
authored andcommitted
x86/dumpstack: Convert show_trace_log_lvl() to use the new unwinder
Convert show_trace_log_lvl() to use the new unwinder. dump_trace() has been deprecated. show_trace_log_lvl() is special compared to other users of the unwinder. It's the only place where both reliable *and* unreliable addresses are needed. With frame pointers enabled, most callers of the unwinder don't want to know about unreliable addresses. But in this case, when we're dumping the stack to the console because something presumably went wrong, the unreliable addresses are useful: - They show stale data on the stack which can provide useful clues. - If something goes wrong with the unwinder, or if frame pointers are corrupt or missing, all the stack addresses still get shown. So in order to show all addresses on the stack, and at the same time figure out which addresses are reliable, we have to do the scanning and the unwinding in parallel. The scanning is done with the help of get_stack_info() to traverse the stacks. The unwinding is done separately by the new unwinder. In theory we could simplify show_trace_log_lvl() by instead pushing some of this logic into the unwind code. But then we would need some kind of "fake" frame logic in the unwinder which would add a lot of complexity and wouldn't be worth it in order to support only one user. Another benefit of this approach is that once we have a DWARF unwinder, we should be able to just plug it in with minimal impact to this code. Another change here is that callers of show_trace_log_lvl() don't need to provide the 'bp' argument. The unwinder already finds the relevant frame pointer by unwinding until it reaches the first frame after the provided stack pointer. Signed-off-by: Josh Poimboeuf <jpoimboe@redhat.com> Cc: Andy Lutomirski <luto@amacapital.net> Cc: Andy Lutomirski <luto@kernel.org> Cc: Borislav Petkov <bp@alien8.de> Cc: Brian Gerst <brgerst@gmail.com> Cc: Byungchul Park <byungchul.park@lge.com> Cc: Denys Vlasenko <dvlasenk@redhat.com> Cc: Frederic Weisbecker <fweisbec@gmail.com> Cc: H. Peter Anvin <hpa@zytor.com> Cc: Kees Cook <keescook@chromium.org> Cc: Linus Torvalds <torvalds@linux-foundation.org> Cc: Nilay Vaish <nilayvaish@gmail.com> Cc: Peter Zijlstra <peterz@infradead.org> Cc: Steven Rostedt <rostedt@goodmis.org> Cc: Thomas Gleixner <tglx@linutronix.de> Link: http://lkml.kernel.org/r/703b5998604c712a1f801874b43f35d6dac52ede.1474045023.git.jpoimboe@redhat.com Signed-off-by: Ingo Molnar <mingo@kernel.org>
1 parent ec2ad9c commit e18bccc

4 files changed

Lines changed: 107 additions & 47 deletions

File tree

‎arch/x86/include/asm/stacktrace.h‎

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -119,13 +119,11 @@ get_stack_pointer(struct task_struct *task, struct pt_regs *regs)
119119
return (unsigned long *)task->thread.sp;
120120
}
121121

122-
extern void
123-
show_trace_log_lvl(struct task_struct *task, struct pt_regs *regs,
124-
unsigned long *stack, unsigned long bp, char *log_lvl);
122+
void show_trace_log_lvl(struct task_struct *task, struct pt_regs *regs,
123+
unsigned long *stack, char *log_lvl);
125124

126-
extern void
127-
show_stack_log_lvl(struct task_struct *task, struct pt_regs *regs,
128-
unsigned long *sp, unsigned long bp, char *log_lvl);
125+
void show_stack_log_lvl(struct task_struct *task, struct pt_regs *regs,
126+
unsigned long *sp, char *log_lvl);
129127

130128
extern unsigned int code_bytes;
131129

‎arch/x86/kernel/dumpstack.c‎

Lines changed: 95 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
#include <linux/sysfs.h>
1818

1919
#include <asm/stacktrace.h>
20-
20+
#include <asm/unwind.h>
2121

2222
int panic_on_unrecovered_nmi;
2323
int panic_on_io_nmi;
@@ -142,56 +142,120 @@ print_context_stack_bp(struct task_struct *task,
142142
}
143143
EXPORT_SYMBOL_GPL(print_context_stack_bp);
144144

145-
static int print_trace_stack(void *data, const char *name)
145+
void show_trace_log_lvl(struct task_struct *task, struct pt_regs *regs,
146+
unsigned long *stack, char *log_lvl)
146147
{
147-
printk("%s <%s> ", (char *)data, name);
148-
return 0;
149-
}
148+
struct unwind_state state;
149+
struct stack_info stack_info = {0};
150+
unsigned long visit_mask = 0;
151+
int graph_idx = 0;
150152

151-
/*
152-
* Print one address/symbol entries per line.
153-
*/
154-
static int print_trace_address(void *data, unsigned long addr, int reliable)
155-
{
156-
printk_stack_address(addr, reliable, data);
157-
return 0;
158-
}
153+
printk("%sCall Trace:\n", log_lvl);
159154

160-
static const struct stacktrace_ops print_trace_ops = {
161-
.stack = print_trace_stack,
162-
.address = print_trace_address,
163-
.walk_stack = print_context_stack,
164-
};
155+
unwind_start(&state, task, regs, stack);
165156

166-
void
167-
show_trace_log_lvl(struct task_struct *task, struct pt_regs *regs,
168-
unsigned long *stack, unsigned long bp, char *log_lvl)
169-
{
170-
printk("%sCall Trace:\n", log_lvl);
171-
dump_trace(task, regs, stack, bp, &print_trace_ops, log_lvl);
157+
/*
158+
* Iterate through the stacks, starting with the current stack pointer.
159+
* Each stack has a pointer to the next one.
160+
*
161+
* x86-64 can have several stacks:
162+
* - task stack
163+
* - interrupt stack
164+
* - HW exception stacks (double fault, nmi, debug, mce)
165+
*
166+
* x86-32 can have up to three stacks:
167+
* - task stack
168+
* - softirq stack
169+
* - hardirq stack
170+
*/
171+
for (; stack; stack = stack_info.next_sp) {
172+
const char *str_begin, *str_end;
173+
174+
/*
175+
* If we overflowed the task stack into a guard page, jump back
176+
* to the bottom of the usable stack.
177+
*/
178+
if (task_stack_page(task) - (void *)stack < PAGE_SIZE)
179+
stack = task_stack_page(task);
180+
181+
if (get_stack_info(stack, task, &stack_info, &visit_mask))
182+
break;
183+
184+
stack_type_str(stack_info.type, &str_begin, &str_end);
185+
if (str_begin)
186+
printk("%s <%s> ", log_lvl, str_begin);
187+
188+
/*
189+
* Scan the stack, printing any text addresses we find. At the
190+
* same time, follow proper stack frames with the unwinder.
191+
*
192+
* Addresses found during the scan which are not reported by
193+
* the unwinder are considered to be additional clues which are
194+
* sometimes useful for debugging and are prefixed with '?'.
195+
* This also serves as a failsafe option in case the unwinder
196+
* goes off in the weeds.
197+
*/
198+
for (; stack < stack_info.end; stack++) {
199+
unsigned long real_addr;
200+
int reliable = 0;
201+
unsigned long addr = *stack;
202+
unsigned long *ret_addr_p =
203+
unwind_get_return_address_ptr(&state);
204+
205+
if (!__kernel_text_address(addr))
206+
continue;
207+
208+
if (stack == ret_addr_p)
209+
reliable = 1;
210+
211+
/*
212+
* When function graph tracing is enabled for a
213+
* function, its return address on the stack is
214+
* replaced with the address of an ftrace handler
215+
* (return_to_handler). In that case, before printing
216+
* the "real" address, we want to print the handler
217+
* address as an "unreliable" hint that function graph
218+
* tracing was involved.
219+
*/
220+
real_addr = ftrace_graph_ret_addr(task, &graph_idx,
221+
addr, stack);
222+
if (real_addr != addr)
223+
printk_stack_address(addr, 0, log_lvl);
224+
printk_stack_address(real_addr, reliable, log_lvl);
225+
226+
if (!reliable)
227+
continue;
228+
229+
/*
230+
* Get the next frame from the unwinder. No need to
231+
* check for an error: if anything goes wrong, the rest
232+
* of the addresses will just be printed as unreliable.
233+
*/
234+
unwind_next_frame(&state);
235+
}
236+
237+
if (str_end)
238+
printk("%s <%s> ", log_lvl, str_end);
239+
}
172240
}
173241

174242
void show_stack(struct task_struct *task, unsigned long *sp)
175243
{
176-
unsigned long bp = 0;
177-
178244
task = task ? : current;
179245

180246
/*
181247
* Stack frames below this one aren't interesting. Don't show them
182248
* if we're printing for %current.
183249
*/
184-
if (!sp && task == current) {
250+
if (!sp && task == current)
185251
sp = get_stack_pointer(current, NULL);
186-
bp = (unsigned long)get_frame_pointer(current, NULL);
187-
}
188252

189-
show_stack_log_lvl(task, NULL, sp, bp, "");
253+
show_stack_log_lvl(current, NULL, sp, "");
190254
}
191255

192256
void show_stack_regs(struct pt_regs *regs)
193257
{
194-
show_stack_log_lvl(current, regs, NULL, 0, "");
258+
show_stack_log_lvl(current, regs, NULL, "");
195259
}
196260

197261
static arch_spinlock_t die_lock = __ARCH_SPIN_LOCK_UNLOCKED;

‎arch/x86/kernel/dumpstack_32.c‎

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -156,9 +156,8 @@ void dump_trace(struct task_struct *task, struct pt_regs *regs,
156156
}
157157
EXPORT_SYMBOL(dump_trace);
158158

159-
void
160-
show_stack_log_lvl(struct task_struct *task, struct pt_regs *regs,
161-
unsigned long *sp, unsigned long bp, char *log_lvl)
159+
void show_stack_log_lvl(struct task_struct *task, struct pt_regs *regs,
160+
unsigned long *sp, char *log_lvl)
162161
{
163162
unsigned long *stack;
164163
int i;
@@ -181,7 +180,7 @@ show_stack_log_lvl(struct task_struct *task, struct pt_regs *regs,
181180
touch_nmi_watchdog();
182181
}
183182
pr_cont("\n");
184-
show_trace_log_lvl(task, regs, sp, bp, log_lvl);
183+
show_trace_log_lvl(task, regs, sp, log_lvl);
185184

186185
put_task_stack(task);
187186
}
@@ -205,7 +204,7 @@ void show_regs(struct pt_regs *regs)
205204
u8 *ip;
206205

207206
pr_emerg("Stack:\n");
208-
show_stack_log_lvl(current, regs, NULL, 0, KERN_EMERG);
207+
show_stack_log_lvl(current, regs, NULL, KERN_EMERG);
209208

210209
pr_emerg("Code:");
211210

‎arch/x86/kernel/dumpstack_64.c‎

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -209,9 +209,8 @@ void dump_trace(struct task_struct *task, struct pt_regs *regs,
209209
}
210210
EXPORT_SYMBOL(dump_trace);
211211

212-
void
213-
show_stack_log_lvl(struct task_struct *task, struct pt_regs *regs,
214-
unsigned long *sp, unsigned long bp, char *log_lvl)
212+
void show_stack_log_lvl(struct task_struct *task, struct pt_regs *regs,
213+
unsigned long *sp, char *log_lvl)
215214
{
216215
unsigned long *irq_stack_end;
217216
unsigned long *irq_stack;
@@ -255,7 +254,7 @@ show_stack_log_lvl(struct task_struct *task, struct pt_regs *regs,
255254
}
256255

257256
pr_cont("\n");
258-
show_trace_log_lvl(task, regs, sp, bp, log_lvl);
257+
show_trace_log_lvl(task, regs, sp, log_lvl);
259258

260259
put_task_stack(task);
261260
}
@@ -278,7 +277,7 @@ void show_regs(struct pt_regs *regs)
278277
u8 *ip;
279278

280279
printk(KERN_DEFAULT "Stack:\n");
281-
show_stack_log_lvl(current, regs, NULL, 0, KERN_DEFAULT);
280+
show_stack_log_lvl(current, regs, NULL, KERN_DEFAULT);
282281

283282
printk(KERN_DEFAULT "Code: ");
284283

0 commit comments

Comments
 (0)