Re: [PATCH] perf: Synchronously cleanup child events

From: Alexander Shishkin
Date: Mon Jan 18 2016 - 07:37:36 EST


Peter Zijlstra <peterz@xxxxxxxxxxxxx> writes:

> I suspect this races against inherit_event(), like:
>
> inherit_event() perf_event_release_kernel()
>
> if (is_orphaned_event(parent_event) /* false */
>
> event->owner = NULL
>
> mutex_lock(child_mutex);
> list_splice
> mutex_unlock(child_mutex);
>
> mutex_lock(child_mutex);
> list_add_tail
> mutex_unlock(child_mutex);

Or how about this instead:

diff --git a/kernel/events/core.c b/kernel/events/core.c
index 8eb3fee429..cd9f1ac537 100644
--- a/kernel/events/core.c
+++ b/kernel/events/core.c
@@ -3786,8 +3786,9 @@ int perf_event_release_kernel(struct perf_event *event)

event->owner = NULL;

+retry:
/*
- * event::child_mutex nests inside ctx::lock, so move children
+ * event::child_mutex nests inside ctx::mutex, so move children
* to a safe place first and avoid inversion
*/
mutex_lock(&event->child_mutex);
@@ -3818,8 +3819,13 @@ int perf_event_release_kernel(struct perf_event *event)
put_event(event);
}

- /* Must be the last reference */
+ /* Must be the last reference, .. */
put_event(event);
+
+ /* .. unless we raced with inherit_event(), in which case, repeat */
+ if (atomic_long_inc_not_zero(&event->refcount))
+ goto retry;
+
return 0;
}
EXPORT_SYMBOL_GPL(perf_event_release_kernel);