Inter-revision diff: patch 5

Comparing v7 (message) to v15 (message)

--- v7
+++ v15
@@ -1,297 +1,146 @@
-thread_info is not anymore in the stack, so the entire stack
-can now be used.
+[text copied from commit 9bbd4c56b0b6
+("arm64: prep stack walkers for THREAD_INFO_IN_TASK")]
 
-There is also no risk anymore of corrupting task_cpu(p) with a
-stack overflow so the patch removes the test.
+When CONFIG_THREAD_INFO_IN_TASK is selected, task stacks may be freed
+before a task is destroyed. To account for this, the stacks are
+refcounted, and when manipulating the stack of another task, it is
+necessary to get/put the stack to ensure it isn't freed and/or re-used
+while we do so.
 
-When doing this, an explicit test for NULL stack pointer is
-needed in validate_sp() as it is not anymore implicitely covered
-by the sizeof(thread_info) gap.
+This patch reworks the powerpc stack walking code to account for this.
+When CONFIG_THREAD_INFO_IN_TASK is not selected these perform no
+refcounting, and this should only be a structural change that does not
+affect behaviour.
 
-In the meantime, with the previous patch all pointers to the stacks
-are not anymore pointers to thread_info so this patch changes them
-to void*
-
+Acked-by: Mark Rutland <mark.rutland@arm.com>
 Signed-off-by: Christophe Leroy <christophe.leroy@c-s.fr>
 ---
- arch/powerpc/include/asm/irq.h       | 10 +++++-----
- arch/powerpc/include/asm/processor.h |  3 +--
- arch/powerpc/kernel/asm-offsets.c    |  1 -
- arch/powerpc/kernel/entry_32.S       | 14 ++++----------
- arch/powerpc/kernel/irq.c            | 19 +++++++++----------
- arch/powerpc/kernel/misc_32.S        |  6 ++----
- arch/powerpc/kernel/process.c        | 32 +++++++++++++-------------------
- arch/powerpc/kernel/setup_64.c       |  8 ++++----
- 8 files changed, 38 insertions(+), 55 deletions(-)
+ arch/powerpc/kernel/process.c    | 23 +++++++++++++++++++++--
+ arch/powerpc/kernel/stacktrace.c | 29 ++++++++++++++++++++++++++---
+ 2 files changed, 47 insertions(+), 5 deletions(-)
 
-diff --git a/arch/powerpc/include/asm/irq.h b/arch/powerpc/include/asm/irq.h
-index 2efbae8d93be..966ddd4d2414 100644
---- a/arch/powerpc/include/asm/irq.h
-+++ b/arch/powerpc/include/asm/irq.h
-@@ -48,9 +48,9 @@ struct pt_regs;
-  * Per-cpu stacks for handling critical, debug and machine check
-  * level interrupts.
-  */
--extern struct thread_info *critirq_ctx[NR_CPUS];
--extern struct thread_info *dbgirq_ctx[NR_CPUS];
--extern struct thread_info *mcheckirq_ctx[NR_CPUS];
-+extern void *critirq_ctx[NR_CPUS];
-+extern void *dbgirq_ctx[NR_CPUS];
-+extern void *mcheckirq_ctx[NR_CPUS];
- extern void exc_lvl_ctx_init(void);
- #else
- #define exc_lvl_ctx_init()
-@@ -59,8 +59,8 @@ extern void exc_lvl_ctx_init(void);
- /*
-  * Per-cpu stacks for handling hard and soft interrupts.
-  */
--extern struct thread_info *hardirq_ctx[NR_CPUS];
--extern struct thread_info *softirq_ctx[NR_CPUS];
-+extern void *hardirq_ctx[NR_CPUS];
-+extern void *softirq_ctx[NR_CPUS];
- 
- extern void irq_ctx_init(void);
- void call_do_softirq(void *sp);
-diff --git a/arch/powerpc/include/asm/processor.h b/arch/powerpc/include/asm/processor.h
-index b225c7f7c5a4..e763342265a2 100644
---- a/arch/powerpc/include/asm/processor.h
-+++ b/arch/powerpc/include/asm/processor.h
-@@ -331,8 +331,7 @@ struct thread_struct {
- #define ARCH_MIN_TASKALIGN 16
- 
- #define INIT_SP		(sizeof(init_stack) + (unsigned long) &init_stack)
--#define INIT_SP_LIMIT \
--	(_ALIGN_UP(sizeof(struct thread_info), 16) + (unsigned long)&init_stack)
-+#define INIT_SP_LIMIT	((unsigned long)&init_stack)
- 
- #ifdef CONFIG_SPE
- #define SPEFSCR_INIT \
-diff --git a/arch/powerpc/kernel/asm-offsets.c b/arch/powerpc/kernel/asm-offsets.c
-index 833d189df04c..768ce602d624 100644
---- a/arch/powerpc/kernel/asm-offsets.c
-+++ b/arch/powerpc/kernel/asm-offsets.c
-@@ -93,7 +93,6 @@ int main(void)
- 	DEFINE(NMI_MASK, NMI_MASK);
- 	OFFSET(TASKTHREADPPR, task_struct, thread.ppr);
- #else
--	DEFINE(THREAD_INFO_GAP, _ALIGN_UP(sizeof(struct thread_info), 16));
- 	OFFSET(KSP_LIMIT, thread_struct, ksp_limit);
- #endif /* CONFIG_PPC64 */
- 	OFFSET(TASK_STACK, task_struct, stack);
-diff --git a/arch/powerpc/kernel/entry_32.S b/arch/powerpc/kernel/entry_32.S
-index fa7a69ffb37a..bd3b146e18a3 100644
---- a/arch/powerpc/kernel/entry_32.S
-+++ b/arch/powerpc/kernel/entry_32.S
-@@ -97,14 +97,11 @@ crit_transfer_to_handler:
- 	mfspr	r0,SPRN_SRR1
- 	stw	r0,_SRR1(r11)
- 
--	/* set the stack limit to the current stack
--	 * and set the limit to protect the thread_info
--	 * struct
--	 */
-+	/* set the stack limit to the current stack */
- 	mfspr	r8,SPRN_SPRG_THREAD
- 	lwz	r0,KSP_LIMIT(r8)
- 	stw	r0,SAVED_KSP_LIMIT(r11)
--	rlwimi	r0,r1,0,0,(31-THREAD_SHIFT)
-+	rlwinm	r0,r1,0,0,(31 - THREAD_SHIFT)
- 	stw	r0,KSP_LIMIT(r8)
- 	/* fall through */
- #endif
-@@ -121,14 +118,11 @@ crit_transfer_to_handler:
- 	mfspr	r0,SPRN_SRR1
- 	stw	r0,crit_srr1@l(0)
- 
--	/* set the stack limit to the current stack
--	 * and set the limit to protect the thread_info
--	 * struct
--	 */
-+	/* set the stack limit to the current stack */
- 	mfspr	r8,SPRN_SPRG_THREAD
- 	lwz	r0,KSP_LIMIT(r8)
- 	stw	r0,saved_ksp_limit@l(0)
--	rlwimi	r0,r1,0,0,(31-THREAD_SHIFT)
-+	rlwinm	r0,r1,0,0,(31 - THREAD_SHIFT)
- 	stw	r0,KSP_LIMIT(r8)
- 	/* fall through */
- #endif
-diff --git a/arch/powerpc/kernel/irq.c b/arch/powerpc/kernel/irq.c
-index 3fdb6b6973cf..62cfccf4af89 100644
---- a/arch/powerpc/kernel/irq.c
-+++ b/arch/powerpc/kernel/irq.c
-@@ -618,9 +618,8 @@ static inline void check_stack_overflow(void)
- 	sp = current_stack_pointer() & (THREAD_SIZE-1);
- 
- 	/* check for stack overflow: is there less than 2KB free? */
--	if (unlikely(sp < (sizeof(struct thread_info) + 2048))) {
--		pr_err("do_IRQ: stack overflow: %ld\n",
--			sp - sizeof(struct thread_info));
-+	if (unlikely(sp < 2048)) {
-+		pr_err("do_IRQ: stack overflow: %ld\n", sp);
- 		dump_stack();
- 	}
- #endif
-@@ -660,7 +659,7 @@ void __do_irq(struct pt_regs *regs)
- void do_IRQ(struct pt_regs *regs)
- {
- 	struct pt_regs *old_regs = set_irq_regs(regs);
--	struct thread_info *curtp, *irqtp, *sirqtp;
-+	void *curtp, *irqtp, *sirqtp;
- 
- 	/* Switch to the irq stack to handle this */
- 	curtp = (void *)(current_stack_pointer() & ~(THREAD_SIZE - 1));
-@@ -690,9 +689,9 @@ void __init init_IRQ(void)
- }
- 
- #if defined(CONFIG_BOOKE) || defined(CONFIG_40x)
--struct thread_info   *critirq_ctx[NR_CPUS] __read_mostly;
--struct thread_info    *dbgirq_ctx[NR_CPUS] __read_mostly;
--struct thread_info *mcheckirq_ctx[NR_CPUS] __read_mostly;
-+void   *critirq_ctx[NR_CPUS] __read_mostly;
-+void    *dbgirq_ctx[NR_CPUS] __read_mostly;
-+void *mcheckirq_ctx[NR_CPUS] __read_mostly;
- 
- void exc_lvl_ctx_init(void)
- {
-@@ -718,8 +717,8 @@ void exc_lvl_ctx_init(void)
- }
- #endif
- 
--struct thread_info *softirq_ctx[NR_CPUS] __read_mostly;
--struct thread_info *hardirq_ctx[NR_CPUS] __read_mostly;
-+void *softirq_ctx[NR_CPUS] __read_mostly;
-+void *hardirq_ctx[NR_CPUS] __read_mostly;
- 
- void irq_ctx_init(void)
- {
-@@ -733,7 +732,7 @@ void irq_ctx_init(void)
- 
- void do_softirq_own_stack(void)
- {
--	struct thread_info *irqtp;
-+	void *irqtp;
- 
- 	irqtp = softirq_ctx[smp_processor_id()];
- 	call_do_softirq(irqtp);
-diff --git a/arch/powerpc/kernel/misc_32.S b/arch/powerpc/kernel/misc_32.S
-index 32762f4c3458..a3663ad62f16 100644
---- a/arch/powerpc/kernel/misc_32.S
-+++ b/arch/powerpc/kernel/misc_32.S
-@@ -46,11 +46,10 @@ _GLOBAL(call_do_softirq)
- 	mflr	r0
- 	stw	r0,4(r1)
- 	lwz	r10,THREAD+KSP_LIMIT(r2)
--	addi	r11,r3,THREAD_INFO_GAP
-+	stw	r3, THREAD+KSP_LIMIT(r2)
- 	stwu	r1,THREAD_SIZE-STACK_FRAME_OVERHEAD(r3)
- 	mr	r1,r3
- 	stw	r10,8(r1)
--	stw	r11,THREAD+KSP_LIMIT(r2)
- 	bl	__do_softirq
- 	lwz	r10,8(r1)
- 	lwz	r1,0(r1)
-@@ -66,11 +65,10 @@ _GLOBAL(call_do_irq)
- 	mflr	r0
- 	stw	r0,4(r1)
- 	lwz	r10,THREAD+KSP_LIMIT(r2)
--	addi	r11,r4,THREAD_INFO_GAP
-+	stw	r4, THREAD+KSP_LIMIT(r2)
- 	stwu	r1,THREAD_SIZE-STACK_FRAME_OVERHEAD(r4)
- 	mr	r1,r4
- 	stw	r10,8(r1)
--	stw	r11,THREAD+KSP_LIMIT(r2)
- 	bl	__do_irq
- 	lwz	r10,8(r1)
- 	lwz	r1,0(r1)
 diff --git a/arch/powerpc/kernel/process.c b/arch/powerpc/kernel/process.c
-index a12307ebb7ef..23657b00e161 100644
+index ce393df243aa..4ffbb677c9f5 100644
 --- a/arch/powerpc/kernel/process.c
 +++ b/arch/powerpc/kernel/process.c
-@@ -1689,8 +1689,7 @@ int copy_thread(unsigned long clone_flags, unsigned long usp,
- 	sp -= STACK_FRAME_OVERHEAD;
- 	p->thread.ksp = sp;
- #ifdef CONFIG_PPC32
--	p->thread.ksp_limit = (unsigned long)task_stack_page(p) +
--				_ALIGN_UP(sizeof(struct thread_info), 16);
-+	p->thread.ksp_limit = (unsigned long)end_of_stack(p);
- #endif
- #ifdef CONFIG_HAVE_HW_BREAKPOINT
- 	p->thread.ptrace_bps[0] = NULL;
-@@ -1986,21 +1985,14 @@ static inline int valid_irq_stack(unsigned long sp, struct task_struct *p,
- 	unsigned long stack_page;
- 	unsigned long cpu = task_cpu(p);
+@@ -2027,7 +2027,7 @@ int validate_sp(unsigned long sp, struct task_struct *p,
  
--	/*
--	 * Avoid crashing if the stack has overflowed and corrupted
--	 * task_cpu(p), which is in the thread_info struct.
--	 */
--	if (cpu < NR_CPUS && cpu_possible(cpu)) {
--		stack_page = (unsigned long) hardirq_ctx[cpu];
--		if (sp >= stack_page + sizeof(struct thread_struct)
--		    && sp <= stack_page + THREAD_SIZE - nbytes)
--			return 1;
--
--		stack_page = (unsigned long) softirq_ctx[cpu];
--		if (sp >= stack_page + sizeof(struct thread_struct)
--		    && sp <= stack_page + THREAD_SIZE - nbytes)
--			return 1;
--	}
-+	stack_page = (unsigned long)hardirq_ctx[cpu];
-+	if (sp >= stack_page && sp <= stack_page + THREAD_SIZE - nbytes)
-+		return 1;
-+
-+	stack_page = (unsigned long)softirq_ctx[cpu];
-+	if (sp >= stack_page && sp <= stack_page + THREAD_SIZE - nbytes)
-+		return 1;
-+
+ EXPORT_SYMBOL(validate_sp);
+ 
+-unsigned long get_wchan(struct task_struct *p)
++static unsigned long __get_wchan(struct task_struct *p)
+ {
+ 	unsigned long ip, sp;
+ 	int count = 0;
+@@ -2053,6 +2053,20 @@ unsigned long get_wchan(struct task_struct *p)
  	return 0;
  }
  
-@@ -2009,8 +2001,10 @@ int validate_sp(unsigned long sp, struct task_struct *p,
- {
- 	unsigned long stack_page = (unsigned long)task_stack_page(p);
- 
--	if (sp >= stack_page + sizeof(struct thread_struct)
--	    && sp <= stack_page + THREAD_SIZE - nbytes)
-+	if (sp < THREAD_SIZE)
++unsigned long get_wchan(struct task_struct *p)
++{
++	unsigned long ret;
++
++	if (!try_get_task_stack(p))
 +		return 0;
 +
-+	if (sp >= stack_page && sp <= stack_page + THREAD_SIZE - nbytes)
- 		return 1;
++	ret = __get_wchan(p);
++
++	put_task_stack(p);
++
++	return ret;
++}
++
+ static int kstack_depth_to_print = CONFIG_PRINT_STACK_DEPTH;
  
- 	return valid_irq_stack(sp, p, nbytes);
-diff --git a/arch/powerpc/kernel/setup_64.c b/arch/powerpc/kernel/setup_64.c
-index 2d682f3e31c6..6792e9c90689 100644
---- a/arch/powerpc/kernel/setup_64.c
-+++ b/arch/powerpc/kernel/setup_64.c
-@@ -717,22 +717,22 @@ void __init emergency_stack_init(void)
- 	limit = min(ppc64_bolted_size(), ppc64_rma_size);
+ void show_stack(struct task_struct *tsk, unsigned long *stack)
+@@ -2067,6 +2081,9 @@ void show_stack(struct task_struct *tsk, unsigned long *stack)
+ 	int curr_frame = 0;
+ #endif
  
- 	for_each_possible_cpu(i) {
--		struct thread_info *ti;
-+		void *ti;
++	if (!try_get_task_stack(tsk))
++		return;
++
+ 	sp = (unsigned long) stack;
+ 	if (tsk == NULL)
+ 		tsk = current;
+@@ -2081,7 +2098,7 @@ void show_stack(struct task_struct *tsk, unsigned long *stack)
+ 	printk("Call Trace:\n");
+ 	do {
+ 		if (!validate_sp(sp, tsk, STACK_FRAME_OVERHEAD))
+-			return;
++			break;
  
- 		ti = alloc_stack(limit, i);
- 		memset(ti, 0, THREAD_SIZE);
--		paca_ptrs[i]->emergency_sp = (void *)ti + THREAD_SIZE;
-+		paca_ptrs[i]->emergency_sp = ti + THREAD_SIZE;
+ 		stack = (unsigned long *) sp;
+ 		newsp = stack[0];
+@@ -2121,6 +2138,8 @@ void show_stack(struct task_struct *tsk, unsigned long *stack)
  
- #ifdef CONFIG_PPC_BOOK3S_64
- 		/* emergency stack for NMI exception handling. */
- 		ti = alloc_stack(limit, i);
- 		memset(ti, 0, THREAD_SIZE);
--		paca_ptrs[i]->nmi_emergency_sp = (void *)ti + THREAD_SIZE;
-+		paca_ptrs[i]->nmi_emergency_sp = ti + THREAD_SIZE;
+ 		sp = newsp;
+ 	} while (count++ < kstack_depth_to_print);
++
++	put_task_stack(tsk);
+ }
  
- 		/* emergency stack for machine check exception handling. */
- 		ti = alloc_stack(limit, i);
- 		memset(ti, 0, THREAD_SIZE);
--		paca_ptrs[i]->mc_emergency_sp = (void *)ti + THREAD_SIZE;
-+		paca_ptrs[i]->mc_emergency_sp = ti + THREAD_SIZE;
- #endif
+ #ifdef CONFIG_PPC64
+diff --git a/arch/powerpc/kernel/stacktrace.c b/arch/powerpc/kernel/stacktrace.c
+index e2c50b55138f..f80e1129c0f2 100644
+--- a/arch/powerpc/kernel/stacktrace.c
++++ b/arch/powerpc/kernel/stacktrace.c
+@@ -67,12 +67,17 @@ void save_stack_trace_tsk(struct task_struct *tsk, struct stack_trace *trace)
+ {
+ 	unsigned long sp;
+ 
++	if (!try_get_task_stack(tsk))
++		return;
++
+ 	if (tsk == current)
+ 		sp = current_stack_pointer();
+ 	else
+ 		sp = tsk->thread.ksp;
+ 
+ 	save_context_stack(trace, sp, tsk, 0);
++
++	put_task_stack(tsk);
+ }
+ EXPORT_SYMBOL_GPL(save_stack_trace_tsk);
+ 
+@@ -84,9 +89,8 @@ save_stack_trace_regs(struct pt_regs *regs, struct stack_trace *trace)
+ EXPORT_SYMBOL_GPL(save_stack_trace_regs);
+ 
+ #ifdef CONFIG_HAVE_RELIABLE_STACKTRACE
+-int
+-save_stack_trace_tsk_reliable(struct task_struct *tsk,
+-				struct stack_trace *trace)
++static int __save_stack_trace_tsk_reliable(struct task_struct *tsk,
++					   struct stack_trace *trace)
+ {
+ 	unsigned long sp;
+ 	unsigned long stack_page = (unsigned long)task_stack_page(tsk);
+@@ -193,6 +197,25 @@ save_stack_trace_tsk_reliable(struct task_struct *tsk,
  	}
+ 	return 0;
  }
++
++int save_stack_trace_tsk_reliable(struct task_struct *tsk,
++				  struct stack_trace *trace)
++{
++	int ret;
++
++	/*
++	 * If the task doesn't have a stack (e.g., a zombie), the stack is
++	 * "reliably" empty.
++	 */
++	if (!try_get_task_stack(tsk))
++		return 0;
++
++	ret = __save_stack_trace_tsk_reliable(tsk, trace);
++
++	put_task_stack(tsk);
++
++	return ret;
++}
+ EXPORT_SYMBOL_GPL(save_stack_trace_tsk_reliable);
+ #endif /* CONFIG_HAVE_RELIABLE_STACKTRACE */
+ 
 -- 
 2.13.3
 
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help