[PATCH 03/10] perf: Ensure IOC_OUTPUT isnt used to create multi-writer buffers

Peter Zijlstra
2010-05-21T09:11:32Z

[-- Attachment #1: perf-fix-output-ioctl.patch --]
[-- Type: text/plain, Size: 1167 bytes --]

Since we want to ensure buffers only have a single writer, we must
avoid creating one with multiple.

Signed-off-by: Peter Zijlstra <a.p.zijlstra@chello.nl>
---
kernel/perf_event.c | 19 +++++++++++++++++++
1 file changed, 19 insertions(+)

Index: linux-2.6/kernel/perf_event.c
===================================================================
--- linux-2.6.orig/kernel/perf_event.c
+++ linux-2.6/kernel/perf_event.c
@@ -4885,6 +4885,13 @@ static int perf_event_set_output(struct
int fput_needed = 0;
int ret = -EINVAL;

+ /*
+ * Don't allow output of inherited per-task events. This would
+ * create performance issues due to cross cpu access.
+ */
+ if (event->cpu == -1 && event->attr.inherit)
+ return -EINVAL;
+
if (!output_fd)
goto set;

@@ -4905,6 +4912,18 @@ static int perf_event_set_output(struct
if (event->data)
goto out;

+ /*
+ * Don't allow cross-cpu buffers
+ */
+ if (output_event->cpu != event->cpu)
+ goto out;
+
+ /*
+ * If its not a per-cpu buffer, it must be the same task.
+ */
+ if (output_event->cpu == -1 && output_event->ctx != event->ctx)
+ goto out;
+
atomic_long_inc(&output_file->f_count);