Re: [PATCH 2/7] stacktrace,sched: Make stack_trace_save_tsk() more robust

From: Peter Zijlstra
Date: Fri Oct 22 2021 - 13:02:00 EST


On Fri, Oct 22, 2021 at 05:54:31PM +0100, Mark Rutland wrote:

> > Pardon my thin understanding of the scheduler, but I assume this change
> > doesn't mean stack_trace_save_tsk() stops working for "current", right?
> > In trying to answer this for myself, I couldn't convince myself what value
> > current->__state have here. Is it one of TASK_(UN)INTERRUPTIBLE ?
>
> Regardless of that, current->on_rq will be non-zero, so you're right that this
> causes stack_trace_save_tsk() to not work for current, e.g.
>
> | # cat /proc/self/stack
> | # wc /proc/self/stack
> | 0 0 0 /proc/self/stack
>
> TBH, I think that (taking a step back from this issue in particular)
> stack_trace_save_tsk() *shouldn't* work for current, and callers *should* be
> forced to explicitly handle current separately from blocked tasks.

That..

>
> So we could fix this in the stacktrace code with:
>
> | diff --git a/kernel/stacktrace.c b/kernel/stacktrace.c
> | index a1cdbf8c3ef8..327af9ff2c55 100644
> | --- a/kernel/stacktrace.c
> | +++ b/kernel/stacktrace.c
> | @@ -149,7 +149,10 @@ unsigned int stack_trace_save_tsk(struct task_struct *tsk, unsigned long *store,
> | .skip = skipnr + (current == tsk),
> | };
> |
> | - task_try_func(tsk, try_arch_stack_walk_tsk, &c);
> | + if (tsk == current)
> | + try_arch_stack_walk_tsk(tsk, &c);
> | + else
> | + task_try_func(tsk, try_arch_stack_walk_tsk, &c);
> |
> | return c.len;
> | }
>
> ... and we could rename task_try_func() to blocked_task_try_func(), and
> later push the distinction into higher-level callers.

I think I favour this fix if we have to. But that's for next week :-)