Skip to content

Commit 80b51f3

Browse files
fenrus75Ingo Molnar
authored andcommitted
x86: use the stack frames to get exact stack-traces for CONFIG_FRAMEPOINTER on x86-64
x86 32 bit already has this feature: This patch uses the stack frames with frame pointer into an exact stack trace, by following the frame pointer. This only affects kernels built with the CONFIG_FRAME_POINTER config option enabled, and greatly reduces the amount of noise in oopses. This code uses the traditional method of doing backtraces, but if it finds a valid frame pointer chain, will use that to show which parts of the backtrace are reliable and which parts are not Due to the fragility and importance of the backtrace code, this needs to be well reviewed and well tested before merging into mainlne. Signed-off-by: Arjan van de Ven <arjan@linux.intel.com> Signed-off-by: Ingo Molnar <mingo@elte.hu> Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
1 parent e4a9456 commit 80b51f3

1 file changed

Lines changed: 44 additions & 23 deletions

File tree

‎arch/x86/kernel/traps_64.c‎

Lines changed: 44 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -225,31 +225,34 @@ static inline int valid_stack_ptr(struct thread_info *tinfo,
225225
return p > t && p < t + THREAD_SIZE - size;
226226
}
227227

228+
/* The form of the top of the frame on the stack */
229+
struct stack_frame {
230+
struct stack_frame *next_frame;
231+
unsigned long return_address;
232+
};
233+
234+
228235
static inline unsigned long print_context_stack(struct thread_info *tinfo,
229236
unsigned long *stack, unsigned long bp,
230237
const struct stacktrace_ops *ops, void *data,
231238
unsigned long *end)
232239
{
233-
/*
234-
* Print function call entries within a stack. 'cond' is the
235-
* "end of stackframe" condition, that the 'stack++'
236-
* iteration will eventually trigger.
237-
*/
238-
while (valid_stack_ptr(tinfo, stack, 3, end)) {
239-
unsigned long addr = *stack++;
240-
/* Use unlocked access here because except for NMIs
241-
we should be already protected against module unloads */
240+
struct stack_frame *frame = (struct stack_frame *)bp;
241+
242+
while (valid_stack_ptr(tinfo, stack, sizeof(*stack), end)) {
243+
unsigned long addr;
244+
245+
addr = *stack;
242246
if (__kernel_text_address(addr)) {
243-
/*
244-
* If the address is either in the text segment of the
245-
* kernel, or in the region which contains vmalloc'ed
246-
* memory, it *may* be the address of a calling
247-
* routine; if so, print it so that someone tracing
248-
* down the cause of the crash will be able to figure
249-
* out the call path that was taken.
250-
*/
251-
ops->address(data, addr, 1);
247+
if ((unsigned long) stack == bp + 8) {
248+
ops->address(data, addr, 1);
249+
frame = frame->next_frame;
250+
bp = (unsigned long) frame;
251+
} else {
252+
ops->address(data, addr, bp == 0);
253+
}
252254
}
255+
stack++;
253256
}
254257
return bp;
255258
}
@@ -274,6 +277,19 @@ void dump_trace(struct task_struct *tsk, struct pt_regs *regs,
274277
stack = (unsigned long *)tsk->thread.sp;
275278
}
276279

280+
#ifdef CONFIG_FRAME_POINTER
281+
if (!bp) {
282+
if (tsk == current) {
283+
/* Grab bp right from our regs */
284+
asm("movq %%rbp, %0" : "=r" (bp):);
285+
} else {
286+
/* bp is the last reg pushed by switch_to */
287+
bp = *(unsigned long *) tsk->thread.sp;
288+
}
289+
}
290+
#endif
291+
292+
277293

278294
/*
279295
* Print function call entries in all stacks, starting at the
@@ -290,8 +306,8 @@ void dump_trace(struct task_struct *tsk, struct pt_regs *regs,
290306
if (ops->stack(data, id) < 0)
291307
break;
292308

293-
print_context_stack(tinfo, stack, 0, ops,
294-
data, estack_end);
309+
bp = print_context_stack(tinfo, stack, bp, ops,
310+
data, estack_end);
295311
ops->stack(data, "<EOE>");
296312
/*
297313
* We link to the next stack via the
@@ -309,8 +325,8 @@ void dump_trace(struct task_struct *tsk, struct pt_regs *regs,
309325
if (stack >= irqstack && stack < irqstack_end) {
310326
if (ops->stack(data, "IRQ") < 0)
311327
break;
312-
print_context_stack(tinfo, stack, 0, ops,
313-
data, irqstack_end);
328+
bp = print_context_stack(tinfo, stack, bp,
329+
ops, data, irqstack_end);
314330
/*
315331
* We link to the next stack (which would be
316332
* the process stack normally) the last
@@ -328,7 +344,7 @@ void dump_trace(struct task_struct *tsk, struct pt_regs *regs,
328344
/*
329345
* This handles the process stack:
330346
*/
331-
print_context_stack(tinfo, stack, 0, ops, data, NULL);
347+
bp = print_context_stack(tinfo, stack, bp, ops, data, NULL);
332348
put_cpu();
333349
}
334350
EXPORT_SYMBOL(dump_trace);
@@ -425,6 +441,11 @@ void dump_stack(void)
425441
unsigned long dummy;
426442
unsigned long bp = 0;
427443

444+
#ifdef CONFIG_FRAME_POINTER
445+
if (!bp)
446+
asm("movq %%rbp, %0" : "=r" (bp):);
447+
#endif
448+
428449
printk("Pid: %d, comm: %.20s %s %s %.*s\n",
429450
current->pid, current->comm, print_tainted(),
430451
init_utsname()->release,

0 commit comments

Comments
 (0)