Skip to content

Commit 975ffdd

Browse files
Peter Zijlstragregkh
authored andcommitted
perf: Fix sample vs do_exit()
[ 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 <[email protected]> Suggested-by: Mark Rutland <[email protected]> Signed-off-by: Peter Zijlstra (Intel) <[email protected]> Link: https://lkml.kernel.org/r/[email protected] Signed-off-by: Sasha Levin <[email protected]>
1 parent 09ac0e0 commit 975ffdd

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
@@ -6800,6 +6800,10 @@ perf_sample_ustack_size(u16 stack_size, u16 header_size,
68006800
if (!regs)
68016801
return 0;
68026802

6803+
/* No mm, no stack, no dump. */
6804+
if (!current->mm)
6805+
return 0;
6806+
68036807
/*
68046808
* Check if we fit in with the requested stack size into the:
68056809
* - TASK_SIZE
@@ -7485,6 +7489,9 @@ perf_callchain(struct perf_event *event, struct pt_regs *regs)
74857489
const u32 max_stack = event->attr.sample_max_stack;
74867490
struct perf_callchain_entry *callchain;
74877491

7492+
if (!current->mm)
7493+
user = false;
7494+
74887495
if (!kernel && !user)
74897496
return &__empty_callchain;
74907497

kernel/exit.c

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

858+
/*
859+
* Since sampling can touch ->mm, make sure to stop everything before we
860+
* tear it down.
861+
*
862+
* Also flushes inherited counters to the parent - before the parent
863+
* gets woken up by child-exit notifications.
864+
*/
865+
perf_event_exit_task(tsk);
866+
858867
exit_mm();
859868

860869
if (group_dead)
@@ -871,14 +880,6 @@ void __noreturn do_exit(long code)
871880
exit_task_work(tsk);
872881
exit_thread(tsk);
873882

874-
/*
875-
* Flush inherited counters to the parent - before the parent
876-
* gets woken up by child-exit notifications.
877-
*
878-
* because of cgroup mode, must be called before cgroup_exit()
879-
*/
880-
perf_event_exit_task(tsk);
881-
882883
sched_autogroup_exit_task(tsk);
883884
cgroup_exit(tsk);
884885

0 commit comments

Comments
 (0)