Skip to content

Commit da02f54

Browse files
Peter ZijlstraIngo Molnar
authored andcommitted
perf/core: Clean up perf_try_init_event()
Make sure that perf_try_init_event() doesn't leave event->pmu nor event->destroy set on failure. Signed-off-by: Peter Zijlstra (Intel) <[email protected]> Signed-off-by: Ingo Molnar <[email protected]> Acked-by: Ravi Bangoria <[email protected]> Link: https://lore.kernel.org/r/[email protected]
1 parent 66477c7 commit da02f54

File tree

1 file changed

+38
-27
lines changed

1 file changed

+38
-27
lines changed

kernel/events/core.c

Lines changed: 38 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -12109,40 +12109,51 @@ static int perf_try_init_event(struct pmu *pmu, struct perf_event *event)
1210912109
if (ctx)
1211012110
perf_event_ctx_unlock(event->group_leader, ctx);
1211112111

12112-
if (!ret) {
12113-
if (!(pmu->capabilities & PERF_PMU_CAP_EXTENDED_REGS) &&
12114-
has_extended_regs(event))
12115-
ret = -EOPNOTSUPP;
12112+
if (ret)
12113+
goto err_pmu;
1211612114

12117-
if (pmu->capabilities & PERF_PMU_CAP_NO_EXCLUDE &&
12118-
event_has_any_exclude_flag(event))
12119-
ret = -EINVAL;
12115+
if (!(pmu->capabilities & PERF_PMU_CAP_EXTENDED_REGS) &&
12116+
has_extended_regs(event)) {
12117+
ret = -EOPNOTSUPP;
12118+
goto err_destroy;
12119+
}
1212012120

12121-
if (pmu->scope != PERF_PMU_SCOPE_NONE && event->cpu >= 0) {
12122-
const struct cpumask *cpumask = perf_scope_cpu_topology_cpumask(pmu->scope, event->cpu);
12123-
struct cpumask *pmu_cpumask = perf_scope_cpumask(pmu->scope);
12124-
int cpu;
12125-
12126-
if (pmu_cpumask && cpumask) {
12127-
cpu = cpumask_any_and(pmu_cpumask, cpumask);
12128-
if (cpu >= nr_cpu_ids)
12129-
ret = -ENODEV;
12130-
else
12131-
event->event_caps |= PERF_EV_CAP_READ_SCOPE;
12132-
} else {
12133-
ret = -ENODEV;
12134-
}
12135-
}
12121+
if (pmu->capabilities & PERF_PMU_CAP_NO_EXCLUDE &&
12122+
event_has_any_exclude_flag(event)) {
12123+
ret = -EINVAL;
12124+
goto err_destroy;
12125+
}
1213612126

12137-
if (ret && event->destroy)
12138-
event->destroy(event);
12127+
if (pmu->scope != PERF_PMU_SCOPE_NONE && event->cpu >= 0) {
12128+
const struct cpumask *cpumask;
12129+
struct cpumask *pmu_cpumask;
12130+
int cpu;
12131+
12132+
cpumask = perf_scope_cpu_topology_cpumask(pmu->scope, event->cpu);
12133+
pmu_cpumask = perf_scope_cpumask(pmu->scope);
12134+
12135+
ret = -ENODEV;
12136+
if (!pmu_cpumask || !cpumask)
12137+
goto err_destroy;
12138+
12139+
cpu = cpumask_any_and(pmu_cpumask, cpumask);
12140+
if (cpu >= nr_cpu_ids)
12141+
goto err_destroy;
12142+
12143+
event->event_caps |= PERF_EV_CAP_READ_SCOPE;
1213912144
}
1214012145

12141-
if (ret) {
12142-
event->pmu = NULL;
12143-
module_put(pmu->module);
12146+
return 0;
12147+
12148+
err_destroy:
12149+
if (event->destroy) {
12150+
event->destroy(event);
12151+
event->destroy = NULL;
1214412152
}
1214512153

12154+
err_pmu:
12155+
event->pmu = NULL;
12156+
module_put(pmu->module);
1214612157
return ret;
1214712158
}
1214812159

0 commit comments

Comments
 (0)