Skip to content

Commit 5aac392

Browse files
rostedtgregkh
authored andcommitted
perf: sched: Fix perf crash with new is_user_task() helper
[ Upstream commit 76ed276 ] 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: 90942f9 ("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 Signed-off-by: Sasha Levin <sashal@kernel.org> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent b45d52c commit 5aac392

3 files changed

Lines changed: 9 additions & 4 deletions

File tree

include/linux/sched.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1739,6 +1739,11 @@ static __always_inline bool is_percpu_thread(void)
17391739
#endif
17401740
}
17411741

1742+
static __always_inline bool is_user_task(struct task_struct *task)
1743+
{
1744+
return task->mm && !(task->flags & (PF_KTHREAD | PF_USER_WORKER));
1745+
}
1746+
17421747
/* Per-process atomic flags. */
17431748
#define PFA_NO_NEW_PRIVS 0 /* May not gain new privileges. */
17441749
#define PFA_SPREAD_PAGE 1 /* Spread page cache over cpuset */

kernel/events/callchain.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ get_perf_callchain(struct pt_regs *regs, bool kernel, bool user,
245245

246246
if (user && !crosstask) {
247247
if (!user_mode(regs)) {
248-
if (current->flags & (PF_KTHREAD | PF_USER_WORKER))
248+
if (!is_user_task(current))
249249
goto exit_put;
250250
regs = task_pt_regs(current);
251251
}

kernel/events/core.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7095,7 +7095,7 @@ static void perf_sample_regs_user(struct perf_regs *regs_user,
70957095
if (user_mode(regs)) {
70967096
regs_user->abi = perf_reg_abi(current);
70977097
regs_user->regs = regs;
7098-
} else if (!(current->flags & (PF_KTHREAD | PF_USER_WORKER))) {
7098+
} else if (is_user_task(current)) {
70997099
perf_get_regs_user(regs_user, regs);
71007100
} else {
71017101
regs_user->abi = PERF_SAMPLE_REGS_ABI_NONE;
@@ -7735,7 +7735,7 @@ static u64 perf_virt_to_phys(u64 virt)
77357735
* Try IRQ-safe get_user_page_fast_only first.
77367736
* If failed, leave phys_addr as 0.
77377737
*/
7738-
if (!(current->flags & (PF_KTHREAD | PF_USER_WORKER))) {
7738+
if (is_user_task(current)) {
77397739
struct page *p;
77407740

77417741
pagefault_disable();
@@ -7848,7 +7848,7 @@ perf_callchain(struct perf_event *event, struct pt_regs *regs)
78487848
{
78497849
bool kernel = !event->attr.exclude_callchain_kernel;
78507850
bool user = !event->attr.exclude_callchain_user &&
7851-
!(current->flags & (PF_KTHREAD | PF_USER_WORKER));
7851+
is_user_task(current);
78527852
/* Disallow cross-task user callchains. */
78537853
bool crosstask = event->ctx->task && event->ctx->task != current;
78547854
const u32 max_stack = event->attr.sample_max_stack;

0 commit comments

Comments
 (0)