All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] perf: Cleanup user's child events
@ 2016-01-15 11:22 Alexander Shishkin
  2016-01-15 12:54 ` Peter Zijlstra
  0 siblings, 1 reply; 33+ messages in thread
From: Alexander Shishkin @ 2016-01-15 11:22 UTC (permalink / raw)
  To: Peter Zijlstra
  Cc: Ingo Molnar, linux-kernel, vince, eranian,
	Arnaldo Carvalho de Melo, Alexander Shishkin

Events are leaking in the following scenario: user creates an event for
task A, task A forks into B (producing a child event), user closes the
original event. Both original user's event and its child will remain for
as long as task B is around. In other words, we don't clean up children
when we try to release the parent.

This patch cleans up user event's children when its file descriptor is
closed.

Signed-off-by: Alexander Shishkin <alexander.shishkin@linux.intel.com>
---
 kernel/events/core.c | 38 +++++++++++++++++++++++++++++++++++++-
 1 file changed, 37 insertions(+), 1 deletion(-)

diff --git a/kernel/events/core.c b/kernel/events/core.c
index 630f53acce..867c4347ea 100644
--- a/kernel/events/core.c
+++ b/kernel/events/core.c
@@ -3828,7 +3828,43 @@ EXPORT_SYMBOL_GPL(perf_event_release_kernel);
  */
 static int perf_release(struct inode *inode, struct file *file)
 {
-	put_event(file->private_data);
+	struct perf_event *event = file->private_data, *child, *tmp;
+	LIST_HEAD(child_list);
+
+	/*
+	 * event::child_mutex nests inside ctx::lock, so move children
+	 * to a safe place first and avoid inversion
+	 */
+	mutex_lock(&event->child_mutex);
+	list_splice_init(&event->child_list, &child_list);
+	mutex_unlock(&event->child_mutex);
+
+	list_for_each_entry_safe(child, tmp, &child_list, child_list) {
+		struct perf_event_context *ctx;
+
+		/*
+		 * This is somewhat similar to perf_free_event(),
+		 * except for these events are alive and need
+		 * proper perf_remove_from_context().
+		 */
+		ctx = perf_event_ctx_lock(child);
+		perf_remove_from_context(child, true);
+		perf_event_ctx_unlock(child, ctx);
+
+		list_del(&child->child_list);
+
+		/* Children will have exactly one reference */
+		free_event(child);
+
+		/*
+		 * This matches the refcount bump in inherit_event();
+		 * this can't be the last reference.
+		 */
+		put_event(event);
+	}
+
+	/* Must be the last reference */
+	put_event(event);
 	return 0;
 }
 
-- 
2.7.0.rc3

^ permalink raw reply related	[flat|nested] 33+ messages in thread

end of thread, other threads:[~2016-01-29 20:01 UTC | newest]

Thread overview: 33+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-01-15 11:22 [PATCH] perf: Cleanup user's child events Alexander Shishkin
2016-01-15 12:54 ` Peter Zijlstra
2016-01-15 13:05   ` Alexander Shishkin
2016-01-15 13:09     ` Peter Zijlstra
2016-01-15 14:07       ` [PATCH] perf: Synchronously cleanup " Alexander Shishkin
2016-01-15 17:57         ` Peter Zijlstra
2016-01-18 12:07           ` Alexander Shishkin
2016-01-18 12:37           ` Alexander Shishkin
2016-01-18 14:44             ` Peter Zijlstra
2016-01-19 15:12               ` [PATCH v2] " Alexander Shishkin
2016-01-19 20:05                 ` Peter Zijlstra
2016-01-19 21:58                   ` Alexei Starovoitov
2016-01-20  8:32                     ` Peter Zijlstra
2016-01-21  4:55                       ` Alexei Starovoitov
2016-01-20  7:04                   ` Alexander Shishkin
2016-01-20  8:03                     ` Peter Zijlstra
2016-01-22 11:35                   ` Alexander Shishkin
2016-01-22 12:12                     ` Peter Zijlstra
2016-01-22 12:38                     ` Peter Zijlstra
2016-01-22 19:44                       ` Alexei Starovoitov
2016-01-25 11:48                         ` Peter Zijlstra
2016-01-25 14:54                           ` Peter Zijlstra
2016-01-25 21:04                             ` Peter Zijlstra
2016-01-26  4:59                               ` Alexei Starovoitov
2016-01-26 16:16                                 ` Peter Zijlstra
2016-01-26 17:24                                   ` Peter Zijlstra
2016-01-26 23:31                                     ` Alexei Starovoitov
2016-01-27  9:58                                       ` Peter Zijlstra
2016-01-27 17:52                                         ` Alexei Starovoitov
2016-01-29 11:28                                 ` [tip:perf/urgent] perf/bpf: Convert perf_event_array to use struct file tip-bot for Alexei Starovoitov
2016-01-29 20:01                                   ` Alexei Starovoitov
2016-01-19 20:07                 ` [PATCH v2] perf: Synchronously cleanup child events Peter Zijlstra
2016-01-19  7:45         ` [PATCH] " Ingo Molnar

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.