Skip to content

Commit cb76c93

Browse files
jpoimboeIngo Molnar
authored andcommitted
x86/dumpstack: Add get_stack_info() interface
valid_stack_ptr() is buggy: it assumes that all stacks are of size THREAD_SIZE, which is not true for exception stacks. So the walk_stack() callbacks will need to know the location of the beginning of the stack as well as the end. Another issue is that in general the various features of a stack (type, size, next stack pointer, description string) are scattered around in various places throughout the stack dump code. Encapsulate all that information in a single place with a new stack_info struct and a get_stack_info() interface. 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/8164dd0db96b7e6a279fa17ae5e6dc375eecb4a9.1473905218.git.jpoimboe@redhat.com Signed-off-by: Ingo Molnar <mingo@kernel.org>
1 parent 9c00390 commit cb76c93

File tree

7 files changed

+234
-128
lines changed

7 files changed

+234
-128
lines changed

arch/x86/events/core.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2251,7 +2251,7 @@ void arch_perf_update_userpage(struct perf_event *event,
22512251
* callchain support
22522252
*/
22532253

2254-
static int backtrace_stack(void *data, char *name)
2254+
static int backtrace_stack(void *data, const char *name)
22552255
{
22562256
return 0;
22572257
}

arch/x86/include/asm/stacktrace.h

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,39 @@
1010
#include <linux/ptrace.h>
1111
#include <asm/switch_to.h>
1212

13+
enum stack_type {
14+
STACK_TYPE_UNKNOWN,
15+
STACK_TYPE_TASK,
16+
STACK_TYPE_IRQ,
17+
STACK_TYPE_SOFTIRQ,
18+
STACK_TYPE_EXCEPTION,
19+
STACK_TYPE_EXCEPTION_LAST = STACK_TYPE_EXCEPTION + N_EXCEPTION_STACKS-1,
20+
};
21+
22+
struct stack_info {
23+
enum stack_type type;
24+
unsigned long *begin, *end, *next_sp;
25+
};
26+
27+
bool in_task_stack(unsigned long *stack, struct task_struct *task,
28+
struct stack_info *info);
29+
30+
int get_stack_info(unsigned long *stack, struct task_struct *task,
31+
struct stack_info *info, unsigned long *visit_mask);
32+
33+
void stack_type_str(enum stack_type type, const char **begin,
34+
const char **end);
35+
36+
static inline bool on_stack(struct stack_info *info, void *addr, size_t len)
37+
{
38+
void *begin = info->begin;
39+
void *end = info->end;
40+
41+
return (info->type != STACK_TYPE_UNKNOWN &&
42+
addr >= begin && addr < end &&
43+
addr + len > begin && addr + len <= end);
44+
}
45+
1346
extern int kstack_depth_to_print;
1447

1548
struct thread_info;
@@ -20,27 +53,27 @@ typedef unsigned long (*walk_stack_t)(struct task_struct *task,
2053
unsigned long bp,
2154
const struct stacktrace_ops *ops,
2255
void *data,
23-
unsigned long *end,
56+
struct stack_info *info,
2457
int *graph);
2558

2659
extern unsigned long
2760
print_context_stack(struct task_struct *task,
2861
unsigned long *stack, unsigned long bp,
2962
const struct stacktrace_ops *ops, void *data,
30-
unsigned long *end, int *graph);
63+
struct stack_info *info, int *graph);
3164

3265
extern unsigned long
3366
print_context_stack_bp(struct task_struct *task,
3467
unsigned long *stack, unsigned long bp,
3568
const struct stacktrace_ops *ops, void *data,
36-
unsigned long *end, int *graph);
69+
struct stack_info *info, int *graph);
3770

3871
/* Generic stack tracer with callbacks */
3972

4073
struct stacktrace_ops {
4174
int (*address)(void *data, unsigned long address, int reliable);
4275
/* On negative return stop dumping */
43-
int (*stack)(void *data, char *name);
76+
int (*stack)(void *data, const char *name);
4477
walk_stack_t walk_stack;
4578
};
4679

arch/x86/kernel/dumpstack.c

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,23 @@ unsigned int code_bytes = 64;
2525
int kstack_depth_to_print = 3 * STACKSLOTS_PER_LINE;
2626
static int die_counter;
2727

28+
bool in_task_stack(unsigned long *stack, struct task_struct *task,
29+
struct stack_info *info)
30+
{
31+
unsigned long *begin = task_stack_page(task);
32+
unsigned long *end = task_stack_page(task) + THREAD_SIZE;
33+
34+
if (stack < begin || stack >= end)
35+
return false;
36+
37+
info->type = STACK_TYPE_TASK;
38+
info->begin = begin;
39+
info->end = end;
40+
info->next_sp = NULL;
41+
42+
return true;
43+
}
44+
2845
static void printk_stack_address(unsigned long address, int reliable,
2946
char *log_lvl)
3047
{
@@ -46,24 +63,11 @@ void printk_address(unsigned long address)
4663
* severe exception (double fault, nmi, stack fault, debug, mce) hardware stack
4764
*/
4865

49-
static inline int valid_stack_ptr(struct task_struct *task,
50-
void *p, unsigned int size, void *end)
51-
{
52-
void *t = task_stack_page(task);
53-
if (end) {
54-
if (p < end && p >= (end-THREAD_SIZE))
55-
return 1;
56-
else
57-
return 0;
58-
}
59-
return p >= t && p < t + THREAD_SIZE - size;
60-
}
61-
6266
unsigned long
6367
print_context_stack(struct task_struct *task,
6468
unsigned long *stack, unsigned long bp,
6569
const struct stacktrace_ops *ops, void *data,
66-
unsigned long *end, int *graph)
70+
struct stack_info *info, int *graph)
6771
{
6872
struct stack_frame *frame = (struct stack_frame *)bp;
6973

@@ -75,7 +79,7 @@ print_context_stack(struct task_struct *task,
7579
PAGE_SIZE)
7680
stack = (unsigned long *)task_stack_page(task);
7781

78-
while (valid_stack_ptr(task, stack, sizeof(*stack), end)) {
82+
while (on_stack(info, stack, sizeof(*stack))) {
7983
unsigned long addr = *stack;
8084

8185
if (__kernel_text_address(addr)) {
@@ -114,12 +118,12 @@ unsigned long
114118
print_context_stack_bp(struct task_struct *task,
115119
unsigned long *stack, unsigned long bp,
116120
const struct stacktrace_ops *ops, void *data,
117-
unsigned long *end, int *graph)
121+
struct stack_info *info, int *graph)
118122
{
119123
struct stack_frame *frame = (struct stack_frame *)bp;
120124
unsigned long *retp = &frame->return_address;
121125

122-
while (valid_stack_ptr(task, retp, sizeof(*retp), end)) {
126+
while (on_stack(info, stack, sizeof(*stack) * 2)) {
123127
unsigned long addr = *retp;
124128
unsigned long real_addr;
125129

@@ -138,7 +142,7 @@ print_context_stack_bp(struct task_struct *task,
138142
}
139143
EXPORT_SYMBOL_GPL(print_context_stack_bp);
140144

141-
static int print_trace_stack(void *data, char *name)
145+
static int print_trace_stack(void *data, const char *name)
142146
{
143147
printk("%s <%s> ", (char *)data, name);
144148
return 0;

arch/x86/kernel/dumpstack_32.c

Lines changed: 81 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -16,61 +16,117 @@
1616

1717
#include <asm/stacktrace.h>
1818

19-
static void *is_irq_stack(void *p, void *irq)
19+
void stack_type_str(enum stack_type type, const char **begin, const char **end)
2020
{
21-
if (p < irq || p >= (irq + THREAD_SIZE))
22-
return NULL;
23-
return irq + THREAD_SIZE;
21+
switch (type) {
22+
case STACK_TYPE_IRQ:
23+
case STACK_TYPE_SOFTIRQ:
24+
*begin = "IRQ";
25+
*end = "EOI";
26+
break;
27+
default:
28+
*begin = NULL;
29+
*end = NULL;
30+
}
2431
}
2532

33+
static bool in_hardirq_stack(unsigned long *stack, struct stack_info *info)
34+
{
35+
unsigned long *begin = (unsigned long *)this_cpu_read(hardirq_stack);
36+
unsigned long *end = begin + (THREAD_SIZE / sizeof(long));
37+
38+
if (stack < begin || stack >= end)
39+
return false;
40+
41+
info->type = STACK_TYPE_IRQ;
42+
info->begin = begin;
43+
info->end = end;
44+
45+
/*
46+
* See irq_32.c -- the next stack pointer is stored at the beginning of
47+
* the stack.
48+
*/
49+
info->next_sp = (unsigned long *)*begin;
50+
51+
return true;
52+
}
2653

27-
static void *is_hardirq_stack(unsigned long *stack)
54+
static bool in_softirq_stack(unsigned long *stack, struct stack_info *info)
2855
{
29-
void *irq = this_cpu_read(hardirq_stack);
56+
unsigned long *begin = (unsigned long *)this_cpu_read(softirq_stack);
57+
unsigned long *end = begin + (THREAD_SIZE / sizeof(long));
58+
59+
if (stack < begin || stack >= end)
60+
return false;
61+
62+
info->type = STACK_TYPE_SOFTIRQ;
63+
info->begin = begin;
64+
info->end = end;
65+
66+
/*
67+
* The next stack pointer is stored at the beginning of the stack.
68+
* See irq_32.c.
69+
*/
70+
info->next_sp = (unsigned long *)*begin;
3071

31-
return is_irq_stack(stack, irq);
72+
return true;
3273
}
3374

34-
static void *is_softirq_stack(unsigned long *stack)
75+
int get_stack_info(unsigned long *stack, struct task_struct *task,
76+
struct stack_info *info, unsigned long *visit_mask)
3577
{
36-
void *irq = this_cpu_read(softirq_stack);
78+
if (!stack)
79+
goto unknown;
3780

38-
return is_irq_stack(stack, irq);
81+
task = task ? : current;
82+
83+
if (in_task_stack(stack, task, info))
84+
return 0;
85+
86+
if (task != current)
87+
goto unknown;
88+
89+
if (in_hardirq_stack(stack, info))
90+
return 0;
91+
92+
if (in_softirq_stack(stack, info))
93+
return 0;
94+
95+
unknown:
96+
info->type = STACK_TYPE_UNKNOWN;
97+
return -EINVAL;
3998
}
4099

41100
void dump_trace(struct task_struct *task, struct pt_regs *regs,
42101
unsigned long *stack, unsigned long bp,
43102
const struct stacktrace_ops *ops, void *data)
44103
{
104+
unsigned long visit_mask = 0;
45105
int graph = 0;
46-
u32 *prev_esp;
47106

48107
task = task ? : current;
49108
stack = stack ? : get_stack_pointer(task, regs);
50109
bp = bp ? : (unsigned long)get_frame_pointer(task, regs);
51110

52111
for (;;) {
53-
void *end_stack;
112+
const char *begin_str, *end_str;
113+
struct stack_info info;
54114

55-
end_stack = is_hardirq_stack(stack);
56-
if (!end_stack)
57-
end_stack = is_softirq_stack(stack);
115+
if (get_stack_info(stack, task, &info, &visit_mask))
116+
break;
58117

59-
bp = ops->walk_stack(task, stack, bp, ops, data,
60-
end_stack, &graph);
118+
stack_type_str(info.type, &begin_str, &end_str);
61119

62-
/* Stop if not on irq stack */
63-
if (!end_stack)
120+
if (begin_str && ops->stack(data, begin_str) < 0)
64121
break;
65122

66-
/* The previous esp is saved on the bottom of the stack */
67-
prev_esp = (u32 *)(end_stack - THREAD_SIZE);
68-
stack = (unsigned long *)*prev_esp;
69-
if (!stack)
70-
break;
123+
bp = ops->walk_stack(task, stack, bp, ops, data, &info, &graph);
71124

72-
if (ops->stack(data, "IRQ") < 0)
125+
if (end_str && ops->stack(data, end_str) < 0)
73126
break;
127+
128+
stack = info.next_sp;
129+
74130
touch_nmi_watchdog();
75131
}
76132
}

0 commit comments

Comments
 (0)