Re: [PATCH] stacktrace: Use PF_KTHREAD to check for kernel threads

From: Mark Rutland
Date: Tue Jul 02 2019 - 12:17:15 EST


On Tue, Jul 02, 2019 at 05:53:35PM +0200, Thomas Gleixner wrote:
> !current->mm is not a reliable indicator for kernel threads as they might
> temporarily use a user mm. Check for PF_KTHREAD instead.
>
> Signed-off-by: Thomas Gleixner <tglx@xxxxxxxxxxxxx>

FWIW:

Acked-by: Mark Rutland <mark.rutland@xxxxxxx>

As a heads-up, I started looking into cleaning up bogus mm checks
tree-wide, since there are a number that look suspicious (e.g. arm64's
arch_dup_task_struct() and x86's __kernel_fpu_begin()).

I was hoping to add an is_kthread(tsk) helper to push people in the
right direction [1].

Thanks,
Mark.

[1] https://git.kernel.org/pub/scm/linux/kernel/git/mark/linux.git/log/?h=sched/kthread-cleanup

> ---
> --- a/kernel/stacktrace.c
> +++ b/kernel/stacktrace.c
> @@ -228,7 +228,7 @@ unsigned int stack_trace_save_user(unsig
> };
>
> /* Trace user stack if not a kernel thread */
> - if (!current->mm)
> + if (current->flags & PF_KTHREAD)
> return 0;
>
> arch_stack_walk_user(consume_entry, &c, task_pt_regs(current));