Skip to content

Commit aaec913

Browse files
Peter Zijlstrasmb49
authored andcommitted
perf: Fix sample vs do_exit()
BugLink: https://bugs.launchpad.net/bugs/2120812 [ Upstream commit 4f6fc782128355931527cefe3eb45338abd8ab39 ] Baisheng Gao reported an ARM64 crash, which Mark decoded as being a synchronous external abort -- most likely due to trying to access MMIO in bad ways. The crash further shows perf trying to do a user stack sample while in exit_mmap()'s tlb_finish_mmu() -- i.e. while tearing down the address space it is trying to access. It turns out that we stop perf after we tear down the userspace mm; a receipie for disaster, since perf likes to access userspace for various reasons. Flip this order by moving up where we stop perf in do_exit(). Additionally, harden PERF_SAMPLE_CALLCHAIN and PERF_SAMPLE_STACK_USER to abort when the current task does not have an mm (exit_mm() makes sure to set current->mm = NULL; before commencing with the actual teardown). Such that CPU wide events don't trip on this same problem. Fixes: c5ebced ("perf: Add ability to attach user stack dump to sample") Reported-by: Baisheng Gao <baisheng.gao@unisoc.com> Suggested-by: Mark Rutland <mark.rutland@arm.com> Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org> Link: https://lkml.kernel.org/r/20250605110815.GQ39944@noisy.programming.kicks-ass.net Signed-off-by: Sasha Levin <sashal@kernel.org> Signed-off-by: Noah Wager <noah.wager@canonical.com> Signed-off-by: Stefan Bader <stefan.bader@canonical.com>
1 parent 6f9504f commit aaec913

File tree

2 files changed

+16
-8
lines changed

2 files changed

+16
-8
lines changed

kernel/events/core.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7145,6 +7145,10 @@ perf_sample_ustack_size(u16 stack_size, u16 header_size,
71457145
if (!regs)
71467146
return 0;
71477147

7148+
/* No mm, no stack, no dump. */
7149+
if (!current->mm)
7150+
return 0;
7151+
71487152
/*
71497153
* Check if we fit in with the requested stack size into the:
71507154
* - TASK_SIZE
@@ -7856,6 +7860,9 @@ perf_callchain(struct perf_event *event, struct pt_regs *regs)
78567860
const u32 max_stack = event->attr.sample_max_stack;
78577861
struct perf_callchain_entry *callchain;
78587862

7863+
if (!current->mm)
7864+
user = false;
7865+
78597866
if (!kernel && !user)
78607867
return &__empty_callchain;
78617868

kernel/exit.c

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -922,6 +922,15 @@ void __noreturn do_exit(long code)
922922
tsk->exit_code = code;
923923
taskstats_exit(tsk, group_dead);
924924

925+
/*
926+
* Since sampling can touch ->mm, make sure to stop everything before we
927+
* tear it down.
928+
*
929+
* Also flushes inherited counters to the parent - before the parent
930+
* gets woken up by child-exit notifications.
931+
*/
932+
perf_event_exit_task(tsk);
933+
925934
exit_mm();
926935

927936
if (group_dead)
@@ -938,14 +947,6 @@ void __noreturn do_exit(long code)
938947
exit_task_work(tsk);
939948
exit_thread(tsk);
940949

941-
/*
942-
* Flush inherited counters to the parent - before the parent
943-
* gets woken up by child-exit notifications.
944-
*
945-
* because of cgroup mode, must be called before cgroup_exit()
946-
*/
947-
perf_event_exit_task(tsk);
948-
949950
sched_autogroup_exit_task(tsk);
950951
cgroup_exit(tsk);
951952

0 commit comments

Comments
 (0)