Skip to content

Commit 003659f

Browse files
Peter ZijlstraIngo Molnar
authored andcommitted
perf/core: Fix perf_pmu_register() vs. perf_init_event()
There is a fairly obvious race between perf_init_event() doing idr_find() and perf_pmu_register() doing idr_alloc() with an incompletely initialized PMU pointer. Avoid by doing idr_alloc() on a NULL pointer to register the id, and swizzling the real struct pmu pointer at the end using idr_replace(). Also making sure to not set struct pmu members after publishing the struct pmu, duh. [ introduce idr_cmpxchg() in order to better handle the idr_replace() error case -- if it were to return an unexpected pointer, it will already have replaced the value and there is no going back. ] Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org> Signed-off-by: Ingo Molnar <mingo@kernel.org> Link: https://lore.kernel.org/r/20241104135517.858805880@infradead.org
1 parent 2565e42 commit 003659f

File tree

1 file changed

+26
-2
lines changed

1 file changed

+26
-2
lines changed

kernel/events/core.c

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11830,6 +11830,21 @@ static int pmu_dev_alloc(struct pmu *pmu)
1183011830
static struct lock_class_key cpuctx_mutex;
1183111831
static struct lock_class_key cpuctx_lock;
1183211832

11833+
static bool idr_cmpxchg(struct idr *idr, unsigned long id, void *old, void *new)
11834+
{
11835+
void *tmp, *val = idr_find(idr, id);
11836+
11837+
if (val != old)
11838+
return false;
11839+
11840+
tmp = idr_replace(idr, new, id);
11841+
if (IS_ERR(tmp))
11842+
return false;
11843+
11844+
WARN_ON_ONCE(tmp != val);
11845+
return true;
11846+
}
11847+
1183311848
int perf_pmu_register(struct pmu *pmu, const char *name, int type)
1183411849
{
1183511850
int cpu, ret, max = PERF_TYPE_MAX;
@@ -11856,14 +11871,15 @@ int perf_pmu_register(struct pmu *pmu, const char *name, int type)
1185611871
if (type >= 0)
1185711872
max = type;
1185811873

11859-
ret = idr_alloc(&pmu_idr, pmu, max, 0, GFP_KERNEL);
11874+
ret = idr_alloc(&pmu_idr, NULL, max, 0, GFP_KERNEL);
1186011875
if (ret < 0)
1186111876
goto free_pdc;
1186211877

1186311878
WARN_ON(type >= 0 && ret != type);
1186411879

1186511880
type = ret;
1186611881
pmu->type = type;
11882+
atomic_set(&pmu->exclusive_cnt, 0);
1186711883

1186811884
if (pmu_bus_running && !pmu->dev) {
1186911885
ret = pmu_dev_alloc(pmu);
@@ -11912,14 +11928,22 @@ int perf_pmu_register(struct pmu *pmu, const char *name, int type)
1191211928
if (!pmu->event_idx)
1191311929
pmu->event_idx = perf_event_idx_default;
1191411930

11931+
/*
11932+
* Now that the PMU is complete, make it visible to perf_try_init_event().
11933+
*/
11934+
if (!idr_cmpxchg(&pmu_idr, pmu->type, NULL, pmu))
11935+
goto free_context;
1191511936
list_add_rcu(&pmu->entry, &pmus);
11916-
atomic_set(&pmu->exclusive_cnt, 0);
11937+
1191711938
ret = 0;
1191811939
unlock:
1191911940
mutex_unlock(&pmus_lock);
1192011941

1192111942
return ret;
1192211943

11944+
free_context:
11945+
free_percpu(pmu->cpu_pmu_context);
11946+
1192311947
free_dev:
1192411948
if (pmu->dev && pmu->dev != PMU_NULL_DEV) {
1192511949
device_del(pmu->dev);

0 commit comments

Comments
 (0)