From: Tejun Heo <tj@kernel.org>
To: mingo@elte.hu, peterz@infradead.org, efault@gmx.de,
avi@redhat.com, paulus@samba.org, acme@redhat.com,
linux-kernel@vger.kernel.org
Cc: Tejun Heo <tj@kernel.org>, Peter Zijlstra <a.p.zijlstra@chello.nl>
Subject: [PATCH 10/12] perf: make nr_events an int and add perf_online_mutex to protect it
Date: Tue, 4 May 2010 14:38:42 +0200 [thread overview]
Message-ID: <1272976724-14312-11-git-send-email-tj@kernel.org> (raw)
In-Reply-To: <1272976724-14312-1-git-send-email-tj@kernel.org>
Change nr_events from atomic_t to int and add perf_online_mutex to
protect it. Helpers to manipulate the count, perf_inc_nr_events() and
perf_dec_nr_events(), are added. The former may fail although it
doesn't in the current code.
This will be used to make sched perf functions called via tracepoints.
Signed-off-by: Tejun Heo <tj@kernel.org>
Cc: Peter Zijlstra <a.p.zijlstra@chello.nl>
Cc: Paul Mackerras <paulus@samba.org>
Cc: Ingo Molnar <mingo@elte.hu>
Cc: Arnaldo Carvalho de Melo <acme@redhat.com>
---
kernel/perf_event.c | 40 ++++++++++++++++++++++++++++++++++++----
1 files changed, 36 insertions(+), 4 deletions(-)
diff --git a/kernel/perf_event.c b/kernel/perf_event.c
index 3f3e328..621d1f1 100644
--- a/kernel/perf_event.c
+++ b/kernel/perf_event.c
@@ -43,7 +43,7 @@ int perf_max_events __read_mostly = 1;
static int perf_reserved_percpu __read_mostly;
static int perf_overcommit __read_mostly = 1;
-static atomic_t nr_events __read_mostly;
+static int nr_events __read_mostly;
static atomic_t nr_mmap_events __read_mostly;
static atomic_t nr_comm_events __read_mostly;
static atomic_t nr_task_events __read_mostly;
@@ -72,6 +72,26 @@ static atomic64_t perf_event_id;
static DEFINE_SPINLOCK(perf_resource_lock);
/*
+ * Lock to protect nr_events and master enable:
+ */
+static DEFINE_MUTEX(perf_online_mutex);
+
+static int perf_inc_nr_events(void)
+{
+ mutex_lock(&perf_online_mutex);
+ nr_events++;
+ mutex_unlock(&perf_online_mutex);
+ return 0;
+}
+
+static void perf_dec_nr_events(void)
+{
+ mutex_lock(&perf_online_mutex);
+ nr_events--;
+ mutex_unlock(&perf_online_mutex);
+}
+
+/*
* Architecture provided APIs - weak aliases:
*/
extern __weak const struct pmu *hw_perf_event_init(struct perf_event *event)
@@ -1589,7 +1609,13 @@ void perf_event_task_tick(struct task_struct *curr)
struct perf_event_context *ctx;
int rotate = 0;
- if (!atomic_read(&nr_events))
+ /*
+ * nr_events is incremented before events are enabled and
+ * decremented after they have been pulled out, so it is
+ * guaranteed to read as non-zero with any enabled event on
+ * this cpu.
+ */
+ if (!nr_events)
return;
cpuctx = &__get_cpu_var(perf_cpu_context);
@@ -1857,7 +1883,7 @@ static void free_event(struct perf_event *event)
perf_pending_sync(event);
if (!event->parent) {
- atomic_dec(&nr_events);
+ perf_dec_nr_events();
if (event->attr.mmap)
atomic_dec(&nr_mmap_events);
if (event->attr.comm)
@@ -4569,6 +4595,12 @@ perf_event_alloc(struct perf_event_attr *attr,
if (!event)
return ERR_PTR(-ENOMEM);
+ err = perf_inc_nr_events();
+ if (err) {
+ kfree(event);
+ return ERR_PTR(err);
+ }
+
/*
* Single events are their own group leaders, with an
* empty sibling list:
@@ -4657,6 +4689,7 @@ done:
if (err) {
if (event->ns)
put_pid_ns(event->ns);
+ perf_dec_nr_events();
kfree(event);
return ERR_PTR(err);
}
@@ -4664,7 +4697,6 @@ done:
event->pmu = pmu;
if (!event->parent) {
- atomic_inc(&nr_events);
if (event->attr.mmap)
atomic_inc(&nr_mmap_events);
if (event->attr.comm)
--
1.6.4.2
next prev parent reply other threads:[~2010-05-04 12:39 UTC|newest]
Thread overview: 55+ messages / expand[flat|nested] mbox.gz Atom feed top
2010-05-04 12:38 [RFC PATCHSET] sched,perf: unify tracers in sched and move perf on top of TP Tejun Heo
2010-05-04 12:38 ` [PATCH 01/12] sched: drop @cpu argument from sched_in preempt notifier Tejun Heo
2010-05-04 17:11 ` Peter Zijlstra
2010-05-04 12:38 ` [PATCH 02/12] sched: rename preempt_notifiers to sched_notifiers and refactor implementation Tejun Heo
2010-05-04 12:38 ` [PATCH 03/12] perf: add perf_event_task_migrate() Tejun Heo
2010-05-05 5:08 ` Frederic Weisbecker
2010-05-05 5:16 ` Tejun Heo
2010-05-05 9:11 ` Peter Zijlstra
2010-05-05 9:37 ` Tejun Heo
2010-05-05 9:50 ` Peter Zijlstra
2010-05-05 9:56 ` Tejun Heo
2010-05-04 12:38 ` [PATCH 04/12] perf: add @rq to perf_event_task_sched_out() Tejun Heo
2010-05-04 17:11 ` Peter Zijlstra
2010-05-04 17:22 ` Tejun Heo
2010-05-04 17:42 ` Peter Zijlstra
2010-05-04 18:22 ` Steven Rostedt
2010-05-04 18:26 ` Peter Zijlstra
2010-05-04 18:32 ` Steven Rostedt
2010-05-05 4:48 ` Tejun Heo
2010-05-05 9:58 ` Tejun Heo
2010-05-07 18:41 ` [tip:sched/core] sched: Remove rq argument to the tracepoints tip-bot for Peter Zijlstra
2010-05-04 12:38 ` [PATCH 05/12] perf: move perf_event_task_sched_in() next to fire_sched_notifiers_in() Tejun Heo
2010-05-04 12:38 ` [PATCH 06/12] sched: relocate fire_sched_notifiers_out() and trace_sched_switch() Tejun Heo
2010-05-04 12:38 ` [PATCH 07/12] sched: coalesce event notifiers Tejun Heo
2010-05-04 12:38 ` [PATCH 08/12] sched: add switch_in and tick tracepoints Tejun Heo
2010-05-04 12:38 ` [PATCH 09/12] perf: factor out perf_event_switch_clones() Tejun Heo
2010-05-04 12:38 ` Tejun Heo [this message]
2010-05-04 12:38 ` [PATCH 11/12] perf: prepare to move sched perf functions on top of tracepoints Tejun Heo
2010-05-04 12:38 ` [PATCH 12/12] perf: " Tejun Heo
2010-05-04 17:29 ` [RFC PATCHSET] sched,perf: unify tracers in sched and move perf on top of TP Peter Zijlstra
2010-05-05 5:00 ` Tejun Heo
2010-05-05 9:06 ` Peter Zijlstra
2010-05-05 9:32 ` Tejun Heo
2010-05-05 9:51 ` Peter Zijlstra
2010-05-05 9:54 ` Tejun Heo
2010-05-05 11:38 ` Peter Zijlstra
2010-05-05 12:28 ` Tejun Heo
2010-05-05 16:55 ` Ingo Molnar
2010-05-05 18:12 ` Peter Zijlstra
2010-05-05 18:16 ` Peter Zijlstra
2010-05-05 18:30 ` Frederic Weisbecker
2010-05-06 6:28 ` Ingo Molnar
2010-05-06 7:11 ` Tejun Heo
2010-05-06 8:27 ` Ingo Molnar
2010-05-06 8:41 ` Tejun Heo
2010-05-06 8:18 ` Avi Kivity
2010-05-06 6:31 ` Ingo Molnar
2010-05-06 7:04 ` Peter Zijlstra
2010-05-06 7:11 ` Ingo Molnar
2010-05-06 7:29 ` Tejun Heo
2010-05-06 7:33 ` Tejun Heo
2010-05-05 12:33 ` Avi Kivity
2010-05-05 13:09 ` Tejun Heo
2010-05-10 5:20 ` Paul Mackerras
2010-05-10 5:48 ` Tejun Heo
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=1272976724-14312-11-git-send-email-tj@kernel.org \
--to=tj@kernel.org \
--cc=a.p.zijlstra@chello.nl \
--cc=acme@redhat.com \
--cc=avi@redhat.com \
--cc=efault@gmx.de \
--cc=linux-kernel@vger.kernel.org \
--cc=mingo@elte.hu \
--cc=paulus@samba.org \
--cc=peterz@infradead.org \
/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
Powered by JetHome