Skip to content

Commit db8466c

Browse files
mpredfearnralfbaechle
authored andcommitted
MIPS: IRQ Stack: Unwind IRQ stack onto task stack
When the separate IRQ stack was introduced, stack unwinding only proceeded as far as the top of the IRQ stack, leading to kernel backtraces being less useful, lacking the trace of what was interrupted. Fix this by providing a means for the kernel to unwind the IRQ stack onto the interrupted task stack. The processor state is saved to the kernel task stack on interrupt. The IRQ_STACK_START macro reserves an unsigned long at the top of the IRQ stack where the interrupted task stack pointer can be saved. After the active stack is switched to the IRQ stack, save the interrupted tasks stack pointer to the reserved location. Fix the stack unwinding code to look for the frame being the top of the IRQ stack and if so get the next frame from the saved location. The existing test does not work with the separate stack since the ra is no longer pointed at ret_from_{irq,exception}. The test to stop unwinding the stack 32 bytes from the top of a stack must be modified to allow unwinding to continue up to the location of the saved task stack pointer when on the IRQ stack. The low / high marks of the stack are set depending on whether the sp is on an irq stack or not. Signed-off-by: Matt Redfearn <matt.redfearn@imgtec.com> Cc: Paolo Bonzini <pbonzini@redhat.com> Cc: Marcin Nowakowski <marcin.nowakowski@imgtec.com> Cc: Masanari Iida <standby24x7@gmail.com> Cc: Chris Metcalf <cmetcalf@mellanox.com> Cc: James Hogan <james.hogan@imgtec.com> Cc: Paul Burton <paul.burton@imgtec.com> Cc: Ingo Molnar <mingo@kernel.org> Cc: Jason A. Donenfeld <jason@zx2c4.com> Cc: Andrew Morton <akpm@linux-foundation.org> Cc: linux-mips@linux-mips.org Cc: linux-kernel@vger.kernel.org Patchwork: https://patchwork.linux-mips.org/patch/15788/ Signed-off-by: Ralf Baechle <ralf@linux-mips.org>
1 parent 0be032c commit db8466c

File tree

4 files changed

+60
-20
lines changed

4 files changed

+60
-20
lines changed

arch/mips/include/asm/irq.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,24 @@
1818
#include <irq.h>
1919

2020
#define IRQ_STACK_SIZE THREAD_SIZE
21+
#define IRQ_STACK_START (IRQ_STACK_SIZE - sizeof(unsigned long))
2122

2223
extern void *irq_stack[NR_CPUS];
2324

25+
/*
26+
* The highest address on the IRQ stack contains a dummy frame put down in
27+
* genex.S (handle_int & except_vec_vi_handler) which is structured as follows:
28+
*
29+
* top ------------
30+
* | task sp | <- irq_stack[cpu] + IRQ_STACK_START
31+
* ------------
32+
* | | <- First frame of IRQ context
33+
* ------------
34+
*
35+
* task sp holds a copy of the task stack pointer where the struct pt_regs
36+
* from exception entry can be found.
37+
*/
38+
2439
static inline bool on_irq_stack(int cpu, unsigned long sp)
2540
{
2641
unsigned long low = (unsigned long)irq_stack[cpu];

arch/mips/kernel/asm-offsets.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ void output_thread_info_defines(void)
102102
DEFINE(_THREAD_SIZE, THREAD_SIZE);
103103
DEFINE(_THREAD_MASK, THREAD_MASK);
104104
DEFINE(_IRQ_STACK_SIZE, IRQ_STACK_SIZE);
105+
DEFINE(_IRQ_STACK_START, IRQ_STACK_START);
105106
BLANK();
106107
}
107108

arch/mips/kernel/genex.S

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -215,9 +215,11 @@ NESTED(handle_int, PT_SIZE, sp)
215215
beq t0, t1, 2f
216216

217217
/* Switch to IRQ stack */
218-
li t1, _IRQ_STACK_SIZE
218+
li t1, _IRQ_STACK_START
219219
PTR_ADD sp, t0, t1
220220

221+
/* Save task's sp on IRQ stack so that unwinding can follow it */
222+
LONG_S s1, 0(sp)
221223
2:
222224
jal plat_irq_dispatch
223225

@@ -325,9 +327,11 @@ NESTED(except_vec_vi_handler, 0, sp)
325327
beq t0, t1, 2f
326328

327329
/* Switch to IRQ stack */
328-
li t1, _IRQ_STACK_SIZE
330+
li t1, _IRQ_STACK_START
329331
PTR_ADD sp, t0, t1
330332

333+
/* Save task's sp on IRQ stack so that unwinding can follow it */
334+
LONG_S s1, 0(sp)
331335
2:
332336
jalr v0
333337

arch/mips/kernel/process.c

Lines changed: 38 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -488,31 +488,52 @@ unsigned long notrace unwind_stack_by_address(unsigned long stack_page,
488488
unsigned long pc,
489489
unsigned long *ra)
490490
{
491+
unsigned long low, high, irq_stack_high;
491492
struct mips_frame_info info;
492493
unsigned long size, ofs;
494+
struct pt_regs *regs;
493495
int leaf;
494-
extern void ret_from_irq(void);
495-
extern void ret_from_exception(void);
496496

497497
if (!stack_page)
498498
return 0;
499499

500500
/*
501-
* If we reached the bottom of interrupt context,
502-
* return saved pc in pt_regs.
501+
* IRQ stacks start at IRQ_STACK_START
502+
* task stacks at THREAD_SIZE - 32
503503
*/
504-
if (pc == (unsigned long)ret_from_irq ||
505-
pc == (unsigned long)ret_from_exception) {
506-
struct pt_regs *regs;
507-
if (*sp >= stack_page &&
508-
*sp + sizeof(*regs) <= stack_page + THREAD_SIZE - 32) {
509-
regs = (struct pt_regs *)*sp;
510-
pc = regs->cp0_epc;
511-
if (!user_mode(regs) && __kernel_text_address(pc)) {
512-
*sp = regs->regs[29];
513-
*ra = regs->regs[31];
514-
return pc;
515-
}
504+
low = stack_page;
505+
if (!preemptible() && on_irq_stack(raw_smp_processor_id(), *sp)) {
506+
high = stack_page + IRQ_STACK_START;
507+
irq_stack_high = high;
508+
} else {
509+
high = stack_page + THREAD_SIZE - 32;
510+
irq_stack_high = 0;
511+
}
512+
513+
/*
514+
* If we reached the top of the interrupt stack, start unwinding
515+
* the interrupted task stack.
516+
*/
517+
if (unlikely(*sp == irq_stack_high)) {
518+
unsigned long task_sp = *(unsigned long *)*sp;
519+
520+
/*
521+
* Check that the pointer saved in the IRQ stack head points to
522+
* something within the stack of the current task
523+
*/
524+
if (!object_is_on_stack((void *)task_sp))
525+
return 0;
526+
527+
/*
528+
* Follow pointer to tasks kernel stack frame where interrupted
529+
* state was saved.
530+
*/
531+
regs = (struct pt_regs *)task_sp;
532+
pc = regs->cp0_epc;
533+
if (!user_mode(regs) && __kernel_text_address(pc)) {
534+
*sp = regs->regs[29];
535+
*ra = regs->regs[31];
536+
return pc;
516537
}
517538
return 0;
518539
}
@@ -533,8 +554,7 @@ unsigned long notrace unwind_stack_by_address(unsigned long stack_page,
533554
if (leaf < 0)
534555
return 0;
535556

536-
if (*sp < stack_page ||
537-
*sp + info.frame_size > stack_page + THREAD_SIZE - 32)
557+
if (*sp < low || *sp + info.frame_size > high)
538558
return 0;
539559

540560
if (leaf)

0 commit comments

Comments
 (0)