public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Peter Zijlstra <peterz@infradead.org>
To: mingo@kernel.org
Cc: linux-kernel@vger.kernel.org, vincent.weaver@maine.edu,
	acme@kernel.org, eranian@google.com, jolsa@redhat.com,
	alexander.shishkin@linux.intel.com, peterz@infradead.org
Subject: [RFC][PATCH 3/3] perf: Fix races in computing the header sizes
Date: Thu, 10 Sep 2015 18:16:24 +0200	[thread overview]
Message-ID: <20150910162008.028400851@infradead.org> (raw)
In-Reply-To: 20150910161621.363774906@infradead.org

[-- Attachment #1: peterz-perf-fix-size-compute.patch --]
[-- Type: text/plain, Size: 1615 bytes --]

There are two races with the current code:

 - Another event can join the group and compute a larger header_size
   concurrently, if the smaller store wins we'll have an incorrect
   header_size set.

 - We compute the header_size after the event becomes active,
   therefore its possible to use the size before its computed.

Remedy the first by moving the computation inside the ctx::mutex lock,
and the second by placing it _before_ perf_install_in_context().

Cc: Arnaldo Carvalho de Melo <acme@kernel.org>
Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
---
 kernel/events/core.c |   15 +++++++++------
 1 file changed, 9 insertions(+), 6 deletions(-)

--- a/kernel/events/core.c
+++ b/kernel/events/core.c
@@ -8349,6 +8349,15 @@ SYSCALL_DEFINE5(perf_event_open,
 		put_ctx(gctx);
 	}
 
+	/*
+	 * Precalculate sample_data sizes; do while holding ctx::mutex such
+	 * that we're serialized against further additions and before
+	 * perf_install_in_context() which is the point the event is active and
+	 * can use these values.
+	 */
+	perf_event__header_size(event);
+	perf_event__id_header_size(event);
+
 	perf_install_in_context(ctx, event, event->cpu);
 	perf_unpin_context(ctx);
 
@@ -8365,12 +8374,6 @@ SYSCALL_DEFINE5(perf_event_open,
 	mutex_unlock(&current->perf_event_mutex);
 
 	/*
-	 * Precalculate sample_data sizes
-	 */
-	perf_event__header_size(event);
-	perf_event__id_header_size(event);
-
-	/*
 	 * Drop the reference on the group_event after placing the
 	 * new event on the sibling_list. This ensures destruction
 	 * of the group leader will find the pointer to itself in



  parent reply	other threads:[~2015-09-10 16:21 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-09-10 16:16 [RFC][PATCH 0/3] variuos perf fixes Peter Zijlstra
2015-09-10 16:16 ` [RFC][PATCH 1/3] perf: Restructure perf syscall point of no return Peter Zijlstra
2015-09-10 16:16 ` [RFC][PATCH 2/3] perf: Fix u16 overflows Peter Zijlstra
2015-09-12  8:11   ` Ingo Molnar
2015-09-14  9:06     ` Peter Zijlstra
2015-09-14  9:31       ` Ingo Molnar
2015-09-10 16:16 ` Peter Zijlstra [this message]
2015-09-13 23:10 ` [RFC][PATCH 0/3] variuos perf fixes Vince Weaver
2015-09-14  6:57   ` Peter Zijlstra

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20150910162008.028400851@infradead.org \
    --to=peterz@infradead.org \
    --cc=acme@kernel.org \
    --cc=alexander.shishkin@linux.intel.com \
    --cc=eranian@google.com \
    --cc=jolsa@redhat.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@kernel.org \
    --cc=vincent.weaver@maine.edu \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox