Commit c418d8b
perf/core: Fix missing read event generation on task exit
For events with inherit_stat enabled, a "read" event will be generated
to collect per task event counts on task exit.
The call chain is as follows:
do_exit
-> perf_event_exit_task
-> perf_event_exit_task_context
-> perf_event_exit_event
-> perf_remove_from_context
-> perf_child_detach
-> sync_child_event
-> perf_event_read_event
However, the child event context detaches the task too early in
perf_event_exit_task_context, which causes sync_child_event to never
generate the read event in this case, since child_event->ctx->task is
always set to TASK_TOMBSTONE. Fix that by moving context lock section
backward to ensure ctx->task is not set to TASK_TOMBSTONE before
generating the read event.
Because perf_event_free_task calls perf_event_exit_task_context with
exit = false to tear down all child events from the context, and the
task never lived, accessing the task PID can lead to a use-after-free.
To fix that, let sync_child_event read task from argument and move the
call to the only place it should be triggered to avoid the effect of
setting ctx->task to TASK_TOMESTONE, and add a task parameter to
perf_event_exit_event to trigger the sync_child_event properly when
needed.
This bug can be reproduced by running "perf record -s" and attaching to
any program that generates perf events in its child tasks. If we check
the result with "perf report -T", the last line of the report will leave
an empty table like "# PID TID", which is expected to contain the
per-task event counts by design.
Fixes: ef54c1a ("perf: Rework perf_event_exit_event()")
Signed-off-by: Thaumy Cheng <thaumy.love@gmail.com>
Signed-off-by: Ingo Molnar <mingo@kernel.org>
Acked-by: Peter Zijlstra <peterz@infradead.org>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com>
Cc: Arnaldo Carvalho de Melo <acme@kernel.org>
Cc: Ian Rogers <irogers@google.com>
Cc: James Clark <james.clark@linaro.org>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Mark Rutland <mark.rutland@arm.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: linux-perf-users@vger.kernel.org
Link: https://patch.msgid.link/20251209041600.963586-1-thaumy.love@gmail.com1 parent 0143928 commit c418d8b
1 file changed
Lines changed: 12 additions & 10 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2317 | 2317 | | |
2318 | 2318 | | |
2319 | 2319 | | |
2320 | | - | |
2321 | | - | |
2322 | 2320 | | |
2323 | 2321 | | |
2324 | 2322 | | |
| |||
2337 | 2335 | | |
2338 | 2336 | | |
2339 | 2337 | | |
2340 | | - | |
2341 | 2338 | | |
2342 | 2339 | | |
2343 | 2340 | | |
| |||
4588 | 4585 | | |
4589 | 4586 | | |
4590 | 4587 | | |
| 4588 | + | |
4591 | 4589 | | |
4592 | 4590 | | |
4593 | 4591 | | |
| |||
4615 | 4613 | | |
4616 | 4614 | | |
4617 | 4615 | | |
4618 | | - | |
| 4616 | + | |
4619 | 4617 | | |
4620 | 4618 | | |
4621 | 4619 | | |
| |||
12518 | 12516 | | |
12519 | 12517 | | |
12520 | 12518 | | |
12521 | | - | |
| 12519 | + | |
12522 | 12520 | | |
12523 | 12521 | | |
12524 | 12522 | | |
| |||
14075 | 14073 | | |
14076 | 14074 | | |
14077 | 14075 | | |
14078 | | - | |
| 14076 | + | |
| 14077 | + | |
14079 | 14078 | | |
14080 | 14079 | | |
14081 | 14080 | | |
14082 | 14081 | | |
14083 | 14082 | | |
14084 | | - | |
14085 | | - | |
14086 | 14083 | | |
14087 | 14084 | | |
14088 | 14085 | | |
| |||
14101 | 14098 | | |
14102 | 14099 | | |
14103 | 14100 | | |
14104 | | - | |
| 14101 | + | |
| 14102 | + | |
| 14103 | + | |
14105 | 14104 | | |
14106 | 14105 | | |
14107 | 14106 | | |
| |||
14124 | 14123 | | |
14125 | 14124 | | |
14126 | 14125 | | |
| 14126 | + | |
| 14127 | + | |
| 14128 | + | |
14127 | 14129 | | |
14128 | 14130 | | |
14129 | 14131 | | |
| |||
14215 | 14217 | | |
14216 | 14218 | | |
14217 | 14219 | | |
14218 | | - | |
| 14220 | + | |
14219 | 14221 | | |
14220 | 14222 | | |
14221 | 14223 | | |
| |||
0 commit comments