perf: sched: Fix perf crash with new is_user_task() helper

In order to do a user space stacktrace the current task needs to be a user
task that has executed in user space. It use to be possible to test if a
task is a user task or not by simply checking the task_struct mm field. If
it was non NULL, it was a user task and if not it was a kernel task.

But things have changed over time, and some kernel tasks now have their
own mm field.

An idea was made to instead test PF_KTHREAD and two functions were used to
wrap this check in case it became more complex to test if a task was a
user task or not[1]. But this was rejected and the C code simply checked
the PF_KTHREAD directly.

It was later found that not all kernel threads set PF_KTHREAD. The io-uring
helpers instead set PF_USER_WORKER and this needed to be added as well.

But checking the flags is still not enough. There's a very small window
when a task exits that it frees its mm field and it is set back to NULL.
If perf were to trigger at this moment, the flags test would say its a
user space task but when perf would read the mm field it would crash with
at NULL pointer dereference.

Now there are flags that can be used to test if a task is exiting, but
they are set in areas that perf may still want to profile the user space
task (to see where it exited). The only real test is to check both the
flags and the mm field.

Instead of making this modification in every location, create a new
is_user_task() helper function that does all the tests needed to know if
it is safe to read the user space memory or not.

[1] https://lore.kernel.org/all/20250425204120.639530125@goodmis.org/

Fixes: 90942f9fac05 ("perf: Use current->flags & PF_KTHREAD|PF_USER_WORKER instead of current->mm == NULL")
Closes: https://lore.kernel.org/all/0d877e6f-41a7-4724-875d-0b0a27b8a545@roeck-us.net/
Reported-by: Guenter Roeck <linux@roeck-us.net>
Signed-off-by: Steven Rostedt (Google) <rostedt@goodmis.org>
Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Tested-by: Guenter Roeck <linux@roeck-us.net>
Cc: stable@vger.kernel.org
Link: https://patch.msgid.link/20260129102821.46484722@gandalf.local.home

authored by Steven Rostedt and committed by Peter Zijlstra 76ed2760 63804fed

+9 -4
+5
include/linux/sched.h
··· 1776 1776 (current->nr_cpus_allowed == 1); 1777 1777 } 1778 1778 1779 + static __always_inline bool is_user_task(struct task_struct *task) 1780 + { 1781 + return task->mm && !(task->flags & (PF_KTHREAD | PF_USER_WORKER)); 1782 + } 1783 + 1779 1784 /* Per-process atomic flags. */ 1780 1785 #define PFA_NO_NEW_PRIVS 0 /* May not gain new privileges. */ 1781 1786 #define PFA_SPREAD_PAGE 1 /* Spread page cache over cpuset */
+1 -1
kernel/events/callchain.c
··· 246 246 247 247 if (user && !crosstask) { 248 248 if (!user_mode(regs)) { 249 - if (current->flags & (PF_KTHREAD | PF_USER_WORKER)) 249 + if (!is_user_task(current)) 250 250 goto exit_put; 251 251 regs = task_pt_regs(current); 252 252 }
+3 -3
kernel/events/core.c
··· 7460 7460 if (user_mode(regs)) { 7461 7461 regs_user->abi = perf_reg_abi(current); 7462 7462 regs_user->regs = regs; 7463 - } else if (!(current->flags & (PF_KTHREAD | PF_USER_WORKER))) { 7463 + } else if (is_user_task(current)) { 7464 7464 perf_get_regs_user(regs_user, regs); 7465 7465 } else { 7466 7466 regs_user->abi = PERF_SAMPLE_REGS_ABI_NONE; ··· 8100 8100 * Try IRQ-safe get_user_page_fast_only first. 8101 8101 * If failed, leave phys_addr as 0. 8102 8102 */ 8103 - if (!(current->flags & (PF_KTHREAD | PF_USER_WORKER))) { 8103 + if (is_user_task(current)) { 8104 8104 struct page *p; 8105 8105 8106 8106 pagefault_disable(); ··· 8215 8215 { 8216 8216 bool kernel = !event->attr.exclude_callchain_kernel; 8217 8217 bool user = !event->attr.exclude_callchain_user && 8218 - !(current->flags & (PF_KTHREAD | PF_USER_WORKER)); 8218 + is_user_task(current); 8219 8219 /* Disallow cross-task user callchains. */ 8220 8220 bool crosstask = event->ctx->task && event->ctx->task != current; 8221 8221 bool defer_user = IS_ENABLED(CONFIG_UNWIND_USER) && user &&