Skip to content

Commit 2016066

Browse files
Luo GengkunIngo Molnar
authored andcommitted
perf/core: Order the PMU list to fix warning about unordered pmu_ctx_list
Syskaller triggers a warning due to prev_epc->pmu != next_epc->pmu in perf_event_swap_task_ctx_data(). vmcore shows that two lists have the same perf_event_pmu_context, but not in the same order. The problem is that the order of pmu_ctx_list for the parent is impacted by the time when an event/PMU is added. While the order for a child is impacted by the event order in the pinned_groups and flexible_groups. So the order of pmu_ctx_list in the parent and child may be different. To fix this problem, insert the perf_event_pmu_context to its proper place after iteration of the pmu_ctx_list. The follow testcase can trigger above warning: # perf record -e cycles --call-graph lbr -- taskset -c 3 ./a.out & # perf stat -e cpu-clock,cs -p xxx // xxx is the pid of a.out test.c void main() { int count = 0; pid_t pid; printf("%d running\n", getpid()); sleep(30); printf("running\n"); pid = fork(); if (pid == -1) { printf("fork error\n"); return; } if (pid == 0) { while (1) { count++; } } else { while (1) { count++; } } } The testcase first opens an LBR event, so it will allocate task_ctx_data, and then open tracepoint and software events, so the parent context will have 3 different perf_event_pmu_contexts. On inheritance, child ctx will insert the perf_event_pmu_context in another order and the warning will trigger. [ mingo: Tidied up the changelog. ] Fixes: bd27568 ("perf: Rewrite core context handling") Signed-off-by: Luo Gengkun <luogengkun@huaweicloud.com> Signed-off-by: Ingo Molnar <mingo@kernel.org> Reviewed-by: Kan Liang <kan.liang@linux.intel.com> Link: https://lore.kernel.org/r/20250122073356.1824736-1-luogengkun@huaweicloud.com
1 parent 0fe8813 commit 2016066

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

kernel/events/core.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4950,7 +4950,7 @@ static struct perf_event_pmu_context *
49504950
find_get_pmu_context(struct pmu *pmu, struct perf_event_context *ctx,
49514951
struct perf_event *event)
49524952
{
4953-
struct perf_event_pmu_context *new = NULL, *epc;
4953+
struct perf_event_pmu_context *new = NULL, *pos = NULL, *epc;
49544954
void *task_ctx_data = NULL;
49554955

49564956
if (!ctx->task) {
@@ -5007,12 +5007,19 @@ find_get_pmu_context(struct pmu *pmu, struct perf_event_context *ctx,
50075007
atomic_inc(&epc->refcount);
50085008
goto found_epc;
50095009
}
5010+
/* Make sure the pmu_ctx_list is sorted by PMU type: */
5011+
if (!pos && epc->pmu->type > pmu->type)
5012+
pos = epc;
50105013
}
50115014

50125015
epc = new;
50135016
new = NULL;
50145017

5015-
list_add(&epc->pmu_ctx_entry, &ctx->pmu_ctx_list);
5018+
if (!pos)
5019+
list_add_tail(&epc->pmu_ctx_entry, &ctx->pmu_ctx_list);
5020+
else
5021+
list_add(&epc->pmu_ctx_entry, pos->pmu_ctx_entry.prev);
5022+
50165023
epc->ctx = ctx;
50175024

50185025
found_epc:

0 commit comments

Comments
 (0)