Skip to content

Commit 22d38ba

Browse files
Frederic WeisbeckerPeter Zijlstra
authored andcommitted
perf: Fix failing inherit_event() doing extra refcount decrement on parent
When inherit_event() fails after the child allocation but before the parent refcount has been incremented, calling put_event() wrongly decrements the reference to the parent, risking to free it too early. Also pmu_get_event() can't be holding a reference to the child concurrently at this point since it is under pmus_srcu critical section. Fix it with restoring the deleted free_event() function and call it on the failing child in order to free it directly under the verified assumption that its refcount is only 1. The refcount to the parent is then voluntarily omitted. Fixes: da916e9 ("perf: Make perf_pmu_unregister() useable") Signed-off-by: Frederic Weisbecker <[email protected]> Signed-off-by: Peter Zijlstra (Intel) <[email protected]> Link: https://lkml.kernel.org/r/[email protected]
1 parent 3e830f6 commit 22d38ba

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

kernel/events/core.c

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5627,6 +5627,22 @@ static void _free_event(struct perf_event *event)
56275627
__free_event(event);
56285628
}
56295629

5630+
/*
5631+
* Used to free events which have a known refcount of 1, such as in error paths
5632+
* of inherited events.
5633+
*/
5634+
static void free_event(struct perf_event *event)
5635+
{
5636+
if (WARN(atomic_long_cmpxchg(&event->refcount, 1, 0) != 1,
5637+
"unexpected event refcount: %ld; ptr=%p\n",
5638+
atomic_long_read(&event->refcount), event)) {
5639+
/* leak to avoid use-after-free */
5640+
return;
5641+
}
5642+
5643+
_free_event(event);
5644+
}
5645+
56305646
/*
56315647
* Remove user event from the owner task.
56325648
*/
@@ -14184,7 +14200,7 @@ inherit_event(struct perf_event *parent_event,
1418414200

1418514201
pmu_ctx = find_get_pmu_context(child_event->pmu, child_ctx, child_event);
1418614202
if (IS_ERR(pmu_ctx)) {
14187-
put_event(child_event);
14203+
free_event(child_event);
1418814204
return ERR_CAST(pmu_ctx);
1418914205
}
1419014206
child_event->pmu_ctx = pmu_ctx;
@@ -14199,7 +14215,7 @@ inherit_event(struct perf_event *parent_event,
1419914215
if (is_orphaned_event(parent_event) ||
1420014216
!atomic_long_inc_not_zero(&parent_event->refcount)) {
1420114217
mutex_unlock(&parent_event->child_mutex);
14202-
put_event(child_event);
14218+
free_event(child_event);
1420314219
return NULL;
1420414220
}
1420514221

0 commit comments

Comments
 (0)