Skip to content

Commit 484889f

Browse files
David Daneyralfbaechle
authored andcommitted
MIPS: Avoid clobbering struct pt_regs in kthreads
The resume() implementation octeon_switch.S examines the saved cp0_status register. We were clobbering the entire pt_regs structure in kernel threads leading to random crashes. When switching away from a kernel thread, the saved cp0_status is examined and if bit 30 is set it is cleared and the CP2 state saved into the pt_regs structure. Since the kernel thread stack overlaid the pt_regs structure this resulted in a corrupt stack. When the kthread with the corrupt stack was resumed, it could crash if it used any of the data in the stack that was clobbered. We fix it by moving the kernel thread stack down so it doesn't overlay pt_regs. Signed-off-by: David Daney <ddaney@caviumnetworks.com> Signed-off-by: Ralf Baechle <ralf@linux-mips.org>
1 parent 1de010a commit 484889f

3 files changed

Lines changed: 8 additions & 4 deletions

File tree

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -311,8 +311,9 @@ extern void start_thread(struct pt_regs * regs, unsigned long pc, unsigned long
311311

312312
unsigned long get_wchan(struct task_struct *p);
313313

314-
#define __KSTK_TOS(tsk) ((unsigned long)task_stack_page(tsk) + THREAD_SIZE - 32)
315-
#define task_pt_regs(tsk) ((struct pt_regs *)__KSTK_TOS(tsk) - 1)
314+
#define __KSTK_TOS(tsk) ((unsigned long)task_stack_page(tsk) + \
315+
THREAD_SIZE - 32 - sizeof(struct pt_regs))
316+
#define task_pt_regs(tsk) ((struct pt_regs *)__KSTK_TOS(tsk))
316317
#define KSTK_EIP(tsk) (task_pt_regs(tsk)->cp0_epc)
317318
#define KSTK_ESP(tsk) (task_pt_regs(tsk)->regs[29])
318319
#define KSTK_STATUS(tsk) (task_pt_regs(tsk)->cp0_status)

‎arch/mips/kernel/head.S‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,8 @@ NESTED(kernel_entry, 16, sp) # kernel entry point
188188

189189
MTC0 zero, CP0_CONTEXT # clear context register
190190
PTR_LA $28, init_thread_union
191-
PTR_LI sp, _THREAD_SIZE - 32
191+
/* Set the SP after an empty pt_regs. */
192+
PTR_LI sp, _THREAD_SIZE - 32 - PT_SIZE
192193
PTR_ADDU sp, $28
193194
set_saved_sp sp, t0, t1
194195
PTR_SUBU sp, 4 * SZREG # init stack pointer

‎arch/mips/kernel/process.c‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ int copy_thread(unsigned long clone_flags, unsigned long usp,
115115
{
116116
struct thread_info *ti = task_thread_info(p);
117117
struct pt_regs *childregs;
118-
long childksp;
118+
unsigned long childksp;
119119
p->set_child_tid = p->clear_child_tid = NULL;
120120

121121
childksp = (unsigned long)task_stack_page(p) + THREAD_SIZE - 32;
@@ -132,6 +132,8 @@ int copy_thread(unsigned long clone_flags, unsigned long usp,
132132

133133
/* set up new TSS. */
134134
childregs = (struct pt_regs *) childksp - 1;
135+
/* Put the stack after the struct pt_regs. */
136+
childksp = (unsigned long) childregs;
135137
*childregs = *regs;
136138
childregs->regs[7] = 0; /* Clear error flag */
137139

0 commit comments

Comments
 (0)