Skip to content

Commit 9bbd4c5

Browse files
mrutland-armctmarinas
authored andcommitted
arm64: prep stack walkers for THREAD_INFO_IN_TASK
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. This patch reworks the arm64 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. Signed-off-by: Mark Rutland <mark.rutland@arm.com> Tested-by: Laura Abbott <labbott@redhat.com> Cc: AKASHI Takahiro <takahiro.akashi@linaro.org> Cc: Andy Lutomirski <luto@kernel.org> Cc: James Morse <james.morse@arm.com> Cc: Will Deacon <will.deacon@arm.com> Signed-off-by: Catalin Marinas <catalin.marinas@arm.com>
1 parent 2020a5a commit 9bbd4c5

3 files changed

Lines changed: 24 additions & 6 deletions

File tree

‎arch/arm64/kernel/process.c‎

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -350,27 +350,35 @@ struct task_struct *__switch_to(struct task_struct *prev,
350350
unsigned long get_wchan(struct task_struct *p)
351351
{
352352
struct stackframe frame;
353-
unsigned long stack_page;
353+
unsigned long stack_page, ret = 0;
354354
int count = 0;
355355
if (!p || p == current || p->state == TASK_RUNNING)
356356
return 0;
357357

358+
stack_page = (unsigned long)try_get_task_stack(p);
359+
if (!stack_page)
360+
return 0;
361+
358362
frame.fp = thread_saved_fp(p);
359363
frame.sp = thread_saved_sp(p);
360364
frame.pc = thread_saved_pc(p);
361365
#ifdef CONFIG_FUNCTION_GRAPH_TRACER
362366
frame.graph = p->curr_ret_stack;
363367
#endif
364-
stack_page = (unsigned long)task_stack_page(p);
365368
do {
366369
if (frame.sp < stack_page ||
367370
frame.sp >= stack_page + THREAD_SIZE ||
368371
unwind_frame(p, &frame))
369-
return 0;
370-
if (!in_sched_functions(frame.pc))
371-
return frame.pc;
372+
goto out;
373+
if (!in_sched_functions(frame.pc)) {
374+
ret = frame.pc;
375+
goto out;
376+
}
372377
} while (count ++ < 16);
373-
return 0;
378+
379+
out:
380+
put_task_stack(p);
381+
return ret;
374382
}
375383

376384
unsigned long arch_align_stack(unsigned long sp)

‎arch/arm64/kernel/stacktrace.c‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,9 @@ void save_stack_trace_tsk(struct task_struct *tsk, struct stack_trace *trace)
181181
struct stack_trace_data data;
182182
struct stackframe frame;
183183

184+
if (!try_get_task_stack(tsk))
185+
return;
186+
184187
data.trace = trace;
185188
data.skip = trace->skip;
186189

@@ -202,6 +205,8 @@ void save_stack_trace_tsk(struct task_struct *tsk, struct stack_trace *trace)
202205
walk_stackframe(tsk, &frame, save_trace, &data);
203206
if (trace->nr_entries < trace->max_entries)
204207
trace->entries[trace->nr_entries++] = ULONG_MAX;
208+
209+
put_task_stack(tsk);
205210
}
206211

207212
void save_stack_trace(struct stack_trace *trace)

‎arch/arm64/kernel/traps.c‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,9 @@ static void dump_backtrace(struct pt_regs *regs, struct task_struct *tsk)
148148
if (!tsk)
149149
tsk = current;
150150

151+
if (!try_get_task_stack(tsk))
152+
return;
153+
151154
/*
152155
* Switching between stacks is valid when tracing current and in
153156
* non-preemptible context.
@@ -213,6 +216,8 @@ static void dump_backtrace(struct pt_regs *regs, struct task_struct *tsk)
213216
stack + sizeof(struct pt_regs));
214217
}
215218
}
219+
220+
put_task_stack(tsk);
216221
}
217222

218223
void show_stack(struct task_struct *tsk, unsigned long *sp)

0 commit comments

Comments
 (0)