Skip to content

Commit 99f7b02

Browse files
committed
Merge branch 'x86-threadinfo-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip
Pull x86 threadinfo changes from Ingo Molnar: "The main change here is the consolidation/unification of 32 and 64 bit thread_info handling methods, from Steve Rostedt" * 'x86-threadinfo-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip: x86, threadinfo: Redo "x86: Use inline assembler to get sp" x86: Clean up dumpstack_64.c code x86: Keep thread_info on thread stack in x86_32 x86: Prepare removal of previous_esp from i386 thread_info structure x86: Nuke GET_THREAD_INFO_WITH_ESP() macro for i386 x86: Nuke the supervisor_stack field in i386 thread_info
2 parents a21e408 + 6cce16f commit 99f7b02

9 files changed

Lines changed: 191 additions & 137 deletions

File tree

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -449,6 +449,15 @@ struct stack_canary {
449449
};
450450
DECLARE_PER_CPU_ALIGNED(struct stack_canary, stack_canary);
451451
#endif
452+
/*
453+
* per-CPU IRQ handling stacks
454+
*/
455+
struct irq_stack {
456+
u32 stack[THREAD_SIZE/sizeof(u32)];
457+
} __aligned(THREAD_SIZE);
458+
459+
DECLARE_PER_CPU(struct irq_stack *, hardirq_stack);
460+
DECLARE_PER_CPU(struct irq_stack *, softirq_stack);
452461
#endif /* X86_64 */
453462

454463
extern unsigned int xstate_size;

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

Lines changed: 5 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
#include <linux/compiler.h>
1111
#include <asm/page.h>
12+
#include <asm/percpu.h>
1213
#include <asm/types.h>
1314

1415
/*
@@ -32,12 +33,6 @@ struct thread_info {
3233
mm_segment_t addr_limit;
3334
struct restart_block restart_block;
3435
void __user *sysenter_return;
35-
#ifdef CONFIG_X86_32
36-
unsigned long previous_esp; /* ESP of the previous stack in
37-
case of nested (IRQ) stacks
38-
*/
39-
__u8 supervisor_stack[0];
40-
#endif
4136
unsigned int sig_on_uaccess_error:1;
4237
unsigned int uaccess_err:1; /* uaccess failed */
4338
};
@@ -153,52 +148,16 @@ struct thread_info {
153148
#define _TIF_WORK_CTXSW_PREV (_TIF_WORK_CTXSW|_TIF_USER_RETURN_NOTIFY)
154149
#define _TIF_WORK_CTXSW_NEXT (_TIF_WORK_CTXSW)
155150

156-
#ifdef CONFIG_X86_32
151+
#define STACK_WARN (THREAD_SIZE/8)
152+
#define KERNEL_STACK_OFFSET (5*(BITS_PER_LONG/8))
157153

158-
#define STACK_WARN (THREAD_SIZE/8)
159154
/*
160155
* macros/functions for gaining access to the thread information structure
161156
*
162157
* preempt_count needs to be 1 initially, until the scheduler is functional.
163158
*/
164159
#ifndef __ASSEMBLY__
165160

166-
#define current_stack_pointer ({ \
167-
unsigned long sp; \
168-
asm("mov %%esp,%0" : "=g" (sp)); \
169-
sp; \
170-
})
171-
172-
/* how to get the thread information struct from C */
173-
static inline struct thread_info *current_thread_info(void)
174-
{
175-
return (struct thread_info *)
176-
(current_stack_pointer & ~(THREAD_SIZE - 1));
177-
}
178-
179-
#else /* !__ASSEMBLY__ */
180-
181-
/* how to get the thread information struct from ASM */
182-
#define GET_THREAD_INFO(reg) \
183-
movl $-THREAD_SIZE, reg; \
184-
andl %esp, reg
185-
186-
/* use this one if reg already contains %esp */
187-
#define GET_THREAD_INFO_WITH_ESP(reg) \
188-
andl $-THREAD_SIZE, reg
189-
190-
#endif
191-
192-
#else /* X86_32 */
193-
194-
#include <asm/percpu.h>
195-
#define KERNEL_STACK_OFFSET (5*8)
196-
197-
/*
198-
* macros/functions for gaining access to the thread information structure
199-
* preempt_count needs to be 1 initially, until the scheduler is functional.
200-
*/
201-
#ifndef __ASSEMBLY__
202161
DECLARE_PER_CPU(unsigned long, kernel_stack);
203162

204163
static inline struct thread_info *current_thread_info(void)
@@ -213,8 +172,8 @@ static inline struct thread_info *current_thread_info(void)
213172

214173
/* how to get the thread information struct from ASM */
215174
#define GET_THREAD_INFO(reg) \
216-
movq PER_CPU_VAR(kernel_stack),reg ; \
217-
subq $(THREAD_SIZE-KERNEL_STACK_OFFSET),reg
175+
_ASM_MOV PER_CPU_VAR(kernel_stack),reg ; \
176+
_ASM_SUB $(THREAD_SIZE-KERNEL_STACK_OFFSET),reg ;
218177

219178
/*
220179
* Same if PER_CPU_VAR(kernel_stack) is, perhaps with some offset, already in
@@ -224,8 +183,6 @@ static inline struct thread_info *current_thread_info(void)
224183

225184
#endif
226185

227-
#endif /* !X86_32 */
228-
229186
/*
230187
* Thread-synchronous status.
231188
*

‎arch/x86/kernel/cpu/common.c‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1079,6 +1079,10 @@ static __init int setup_disablecpuid(char *arg)
10791079
}
10801080
__setup("clearcpuid=", setup_disablecpuid);
10811081

1082+
DEFINE_PER_CPU(unsigned long, kernel_stack) =
1083+
(unsigned long)&init_thread_union - KERNEL_STACK_OFFSET + THREAD_SIZE;
1084+
EXPORT_PER_CPU_SYMBOL(kernel_stack);
1085+
10821086
#ifdef CONFIG_X86_64
10831087
struct desc_ptr idt_descr = { NR_VECTORS * 16 - 1, (unsigned long) idt_table };
10841088
struct desc_ptr debug_idt_descr = { NR_VECTORS * 16 - 1,
@@ -1095,10 +1099,6 @@ DEFINE_PER_CPU(struct task_struct *, current_task) ____cacheline_aligned =
10951099
&init_task;
10961100
EXPORT_PER_CPU_SYMBOL(current_task);
10971101

1098-
DEFINE_PER_CPU(unsigned long, kernel_stack) =
1099-
(unsigned long)&init_thread_union - KERNEL_STACK_OFFSET + THREAD_SIZE;
1100-
EXPORT_PER_CPU_SYMBOL(kernel_stack);
1101-
11021102
DEFINE_PER_CPU(char *, irq_stack_ptr) =
11031103
init_per_cpu_var(irq_stack_union.irq_stack) + IRQ_STACK_SIZE - 64;
11041104

‎arch/x86/kernel/dumpstack_32.c‎

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,35 @@
1616

1717
#include <asm/stacktrace.h>
1818

19+
static void *is_irq_stack(void *p, void *irq)
20+
{
21+
if (p < irq || p >= (irq + THREAD_SIZE))
22+
return NULL;
23+
return irq + THREAD_SIZE;
24+
}
25+
26+
27+
static void *is_hardirq_stack(unsigned long *stack, int cpu)
28+
{
29+
void *irq = per_cpu(hardirq_stack, cpu);
30+
31+
return is_irq_stack(stack, irq);
32+
}
33+
34+
static void *is_softirq_stack(unsigned long *stack, int cpu)
35+
{
36+
void *irq = per_cpu(softirq_stack, cpu);
37+
38+
return is_irq_stack(stack, irq);
39+
}
1940

2041
void dump_trace(struct task_struct *task, struct pt_regs *regs,
2142
unsigned long *stack, unsigned long bp,
2243
const struct stacktrace_ops *ops, void *data)
2344
{
45+
const unsigned cpu = get_cpu();
2446
int graph = 0;
47+
u32 *prev_esp;
2548

2649
if (!task)
2750
task = current;
@@ -39,18 +62,31 @@ void dump_trace(struct task_struct *task, struct pt_regs *regs,
3962

4063
for (;;) {
4164
struct thread_info *context;
65+
void *end_stack;
66+
67+
end_stack = is_hardirq_stack(stack, cpu);
68+
if (!end_stack)
69+
end_stack = is_softirq_stack(stack, cpu);
4270

43-
context = (struct thread_info *)
44-
((unsigned long)stack & (~(THREAD_SIZE - 1)));
45-
bp = ops->walk_stack(context, stack, bp, ops, data, NULL, &graph);
71+
context = task_thread_info(task);
72+
bp = ops->walk_stack(context, stack, bp, ops, data,
73+
end_stack, &graph);
4674

47-
stack = (unsigned long *)context->previous_esp;
75+
/* Stop if not on irq stack */
76+
if (!end_stack)
77+
break;
78+
79+
/* The previous esp is saved on the bottom of the stack */
80+
prev_esp = (u32 *)(end_stack - THREAD_SIZE);
81+
stack = (unsigned long *)*prev_esp;
4882
if (!stack)
4983
break;
84+
5085
if (ops->stack(data, "IRQ") < 0)
5186
break;
5287
touch_nmi_watchdog();
5388
}
89+
put_cpu();
5490
}
5591
EXPORT_SYMBOL(dump_trace);
5692

‎arch/x86/kernel/dumpstack_64.c‎

Lines changed: 83 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,45 @@ in_irq_stack(unsigned long *stack, unsigned long *irq_stack,
104104
return (stack >= irq_stack && stack < irq_stack_end);
105105
}
106106

107+
static const unsigned long irq_stack_size =
108+
(IRQ_STACK_SIZE - 64) / sizeof(unsigned long);
109+
110+
enum stack_type {
111+
STACK_IS_UNKNOWN,
112+
STACK_IS_NORMAL,
113+
STACK_IS_EXCEPTION,
114+
STACK_IS_IRQ,
115+
};
116+
117+
static enum stack_type
118+
analyze_stack(int cpu, struct task_struct *task,
119+
unsigned long *stack, unsigned long **stack_end, char **id)
120+
{
121+
unsigned long *irq_stack;
122+
unsigned long addr;
123+
unsigned used = 0;
124+
125+
addr = ((unsigned long)stack & (~(THREAD_SIZE - 1)));
126+
if ((unsigned long)task_stack_page(task) == addr)
127+
return STACK_IS_NORMAL;
128+
129+
*stack_end = in_exception_stack(cpu, (unsigned long)stack,
130+
&used, id);
131+
if (*stack_end)
132+
return STACK_IS_EXCEPTION;
133+
134+
*stack_end = (unsigned long *)per_cpu(irq_stack_ptr, cpu);
135+
if (!*stack_end)
136+
return STACK_IS_UNKNOWN;
137+
138+
irq_stack = *stack_end - irq_stack_size;
139+
140+
if (in_irq_stack(stack, irq_stack, *stack_end))
141+
return STACK_IS_IRQ;
142+
143+
return STACK_IS_UNKNOWN;
144+
}
145+
107146
/*
108147
* x86-64 can have up to three kernel stacks:
109148
* process stack
@@ -116,12 +155,11 @@ void dump_trace(struct task_struct *task, struct pt_regs *regs,
116155
const struct stacktrace_ops *ops, void *data)
117156
{
118157
const unsigned cpu = get_cpu();
119-
unsigned long *irq_stack_end =
120-
(unsigned long *)per_cpu(irq_stack_ptr, cpu);
121-
unsigned used = 0;
122158
struct thread_info *tinfo;
123-
int graph = 0;
159+
unsigned long *irq_stack;
124160
unsigned long dummy;
161+
int graph = 0;
162+
int done = 0;
125163

126164
if (!task)
127165
task = current;
@@ -143,49 +181,60 @@ void dump_trace(struct task_struct *task, struct pt_regs *regs,
143181
* exceptions
144182
*/
145183
tinfo = task_thread_info(task);
146-
for (;;) {
184+
while (!done) {
185+
unsigned long *stack_end;
186+
enum stack_type stype;
147187
char *id;
148-
unsigned long *estack_end;
149-
estack_end = in_exception_stack(cpu, (unsigned long)stack,
150-
&used, &id);
151188

152-
if (estack_end) {
189+
stype = analyze_stack(cpu, task, stack, &stack_end, &id);
190+
191+
/* Default finish unless specified to continue */
192+
done = 1;
193+
194+
switch (stype) {
195+
196+
/* Break out early if we are on the thread stack */
197+
case STACK_IS_NORMAL:
198+
break;
199+
200+
case STACK_IS_EXCEPTION:
201+
153202
if (ops->stack(data, id) < 0)
154203
break;
155204

156205
bp = ops->walk_stack(tinfo, stack, bp, ops,
157-
data, estack_end, &graph);
206+
data, stack_end, &graph);
158207
ops->stack(data, "<EOE>");
159208
/*
160209
* We link to the next stack via the
161210
* second-to-last pointer (index -2 to end) in the
162211
* exception stack:
163212
*/
164-
stack = (unsigned long *) estack_end[-2];
165-
continue;
166-
}
167-
if (irq_stack_end) {
168-
unsigned long *irq_stack;
169-
irq_stack = irq_stack_end -
170-
(IRQ_STACK_SIZE - 64) / sizeof(*irq_stack);
171-
172-
if (in_irq_stack(stack, irq_stack, irq_stack_end)) {
173-
if (ops->stack(data, "IRQ") < 0)
174-
break;
175-
bp = ops->walk_stack(tinfo, stack, bp,
176-
ops, data, irq_stack_end, &graph);
177-
/*
178-
* We link to the next stack (which would be
179-
* the process stack normally) the last
180-
* pointer (index -1 to end) in the IRQ stack:
181-
*/
182-
stack = (unsigned long *) (irq_stack_end[-1]);
183-
irq_stack_end = NULL;
184-
ops->stack(data, "EOI");
185-
continue;
186-
}
213+
stack = (unsigned long *) stack_end[-2];
214+
done = 0;
215+
break;
216+
217+
case STACK_IS_IRQ:
218+
219+
if (ops->stack(data, "IRQ") < 0)
220+
break;
221+
bp = ops->walk_stack(tinfo, stack, bp,
222+
ops, data, stack_end, &graph);
223+
/*
224+
* We link to the next stack (which would be
225+
* the process stack normally) the last
226+
* pointer (index -1 to end) in the IRQ stack:
227+
*/
228+
stack = (unsigned long *) (stack_end[-1]);
229+
irq_stack = stack_end - irq_stack_size;
230+
ops->stack(data, "EOI");
231+
done = 0;
232+
break;
233+
234+
case STACK_IS_UNKNOWN:
235+
ops->stack(data, "UNK");
236+
break;
187237
}
188-
break;
189238
}
190239

191240
/*

0 commit comments

Comments
 (0)