blob: 616c52426b32ec207945243a63a55cd4aab8aa92 [file] [log] [blame]
Thomas Gleixner0793a612008-12-04 20:12:29 +01001/*
2 * Performance counter core code
3 *
Ingo Molnar98144512009-04-29 14:52:50 +02004 * Copyright (C) 2008 Thomas Gleixner <tglx@linutronix.de>
5 * Copyright (C) 2008-2009 Red Hat, Inc., Ingo Molnar
6 * Copyright (C) 2008-2009 Red Hat, Inc., Peter Zijlstra <pzijlstr@redhat.com>
Paul Mackerrasc5dd0162009-04-30 09:48:16 +10007 * Copyright © 2009 Paul Mackerras, IBM Corp. <paulus@au1.ibm.com>
Peter Zijlstra7b732a72009-03-23 18:22:10 +01008 *
9 * For licensing details see kernel-base/COPYING
Thomas Gleixner0793a612008-12-04 20:12:29 +010010 */
11
12#include <linux/fs.h>
Peter Zijlstrab9cacc72009-03-25 12:30:22 +010013#include <linux/mm.h>
Thomas Gleixner0793a612008-12-04 20:12:29 +010014#include <linux/cpu.h>
15#include <linux/smp.h>
Ingo Molnar04289bb2008-12-11 08:38:42 +010016#include <linux/file.h>
Thomas Gleixner0793a612008-12-04 20:12:29 +010017#include <linux/poll.h>
18#include <linux/sysfs.h>
19#include <linux/ptrace.h>
20#include <linux/percpu.h>
Peter Zijlstrab9cacc72009-03-25 12:30:22 +010021#include <linux/vmstat.h>
22#include <linux/hardirq.h>
23#include <linux/rculist.h>
Thomas Gleixner0793a612008-12-04 20:12:29 +010024#include <linux/uaccess.h>
25#include <linux/syscalls.h>
26#include <linux/anon_inodes.h>
Ingo Molnaraa9c4c02008-12-17 14:10:57 +010027#include <linux/kernel_stat.h>
Thomas Gleixner0793a612008-12-04 20:12:29 +010028#include <linux/perf_counter.h>
Peter Zijlstra0a4a9392009-03-30 19:07:05 +020029#include <linux/dcache.h>
Thomas Gleixner0793a612008-12-04 20:12:29 +010030
Tim Blechmann4e193bd2009-03-14 14:29:25 +010031#include <asm/irq_regs.h>
32
Thomas Gleixner0793a612008-12-04 20:12:29 +010033/*
34 * Each CPU has a list of per CPU counters:
35 */
36DEFINE_PER_CPU(struct perf_cpu_context, perf_cpu_context);
37
Ingo Molnar088e2852008-12-14 20:21:00 +010038int perf_max_counters __read_mostly = 1;
Thomas Gleixner0793a612008-12-04 20:12:29 +010039static int perf_reserved_percpu __read_mostly;
40static int perf_overcommit __read_mostly = 1;
41
Peter Zijlstra7fc23a52009-05-08 18:52:21 +020042static atomic_t nr_counters __read_mostly;
Peter Zijlstra9ee318a2009-04-09 10:53:44 +020043static atomic_t nr_mmap_tracking __read_mostly;
44static atomic_t nr_munmap_tracking __read_mostly;
45static atomic_t nr_comm_tracking __read_mostly;
46
Peter Zijlstra1ccd1542009-04-09 10:53:45 +020047int sysctl_perf_counter_priv __read_mostly; /* do we need to be privileged */
Peter Zijlstra789f90f2009-05-15 15:19:27 +020048int sysctl_perf_counter_mlock __read_mostly = 512; /* 'free' kb per user */
Peter Zijlstraa78ac322009-05-25 17:39:05 +020049int sysctl_perf_counter_limit __read_mostly = 100000; /* max NMIs per second */
Peter Zijlstra1ccd1542009-04-09 10:53:45 +020050
Thomas Gleixner0793a612008-12-04 20:12:29 +010051/*
Ingo Molnar1dce8d92009-05-04 19:23:18 +020052 * Lock for (sysadmin-configurable) counter reservations:
Thomas Gleixner0793a612008-12-04 20:12:29 +010053 */
Ingo Molnar1dce8d92009-05-04 19:23:18 +020054static DEFINE_SPINLOCK(perf_resource_lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +010055
56/*
57 * Architecture provided APIs - weak aliases:
58 */
Robert Richter4aeb0b42009-04-29 12:47:03 +020059extern __weak const struct pmu *hw_perf_counter_init(struct perf_counter *counter)
Thomas Gleixner0793a612008-12-04 20:12:29 +010060{
Paul Mackerrasff6f0542009-01-09 16:19:25 +110061 return NULL;
Thomas Gleixner0793a612008-12-04 20:12:29 +010062}
63
Peter Zijlstra9e35ad32009-05-13 16:21:38 +020064void __weak hw_perf_disable(void) { barrier(); }
65void __weak hw_perf_enable(void) { barrier(); }
66
Paul Mackerras01d02872009-01-14 13:44:19 +110067void __weak hw_perf_counter_setup(int cpu) { barrier(); }
Paul Mackerras3cbed422009-01-09 16:43:42 +110068int __weak hw_perf_group_sched_in(struct perf_counter *group_leader,
69 struct perf_cpu_context *cpuctx,
70 struct perf_counter_context *ctx, int cpu)
71{
72 return 0;
73}
Thomas Gleixner0793a612008-12-04 20:12:29 +010074
Paul Mackerras4eb96fc2009-01-09 17:24:34 +110075void __weak perf_counter_print_debug(void) { }
76
Peter Zijlstra9e35ad32009-05-13 16:21:38 +020077static DEFINE_PER_CPU(int, disable_count);
78
79void __perf_disable(void)
80{
81 __get_cpu_var(disable_count)++;
82}
83
84bool __perf_enable(void)
85{
86 return !--__get_cpu_var(disable_count);
87}
88
89void perf_disable(void)
90{
91 __perf_disable();
92 hw_perf_disable();
93}
Peter Zijlstra9e35ad32009-05-13 16:21:38 +020094
95void perf_enable(void)
96{
97 if (__perf_enable())
98 hw_perf_enable();
99}
Peter Zijlstra9e35ad32009-05-13 16:21:38 +0200100
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000101static void get_ctx(struct perf_counter_context *ctx)
102{
103 atomic_inc(&ctx->refcount);
104}
105
Paul Mackerrasc93f7662009-05-28 22:18:17 +1000106static void free_ctx(struct rcu_head *head)
107{
108 struct perf_counter_context *ctx;
109
110 ctx = container_of(head, struct perf_counter_context, rcu_head);
111 kfree(ctx);
112}
113
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000114static void put_ctx(struct perf_counter_context *ctx)
115{
Paul Mackerras564c2b22009-05-22 14:27:22 +1000116 if (atomic_dec_and_test(&ctx->refcount)) {
117 if (ctx->parent_ctx)
118 put_ctx(ctx->parent_ctx);
Paul Mackerrasc93f7662009-05-28 22:18:17 +1000119 if (ctx->task)
120 put_task_struct(ctx->task);
121 call_rcu(&ctx->rcu_head, free_ctx);
Paul Mackerras564c2b22009-05-22 14:27:22 +1000122 }
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000123}
124
Peter Zijlstrafccc7142009-05-23 18:28:56 +0200125/*
126 * Add a counter from the lists for its context.
127 * Must be called with ctx->mutex and ctx->lock held.
128 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100129static void
130list_add_counter(struct perf_counter *counter, struct perf_counter_context *ctx)
131{
132 struct perf_counter *group_leader = counter->group_leader;
133
134 /*
135 * Depending on whether it is a standalone or sibling counter,
136 * add it straight to the context's counter list, or to the group
137 * leader's sibling list:
138 */
Peter Zijlstra3df5eda2009-05-08 18:52:22 +0200139 if (group_leader == counter)
Ingo Molnar04289bb2008-12-11 08:38:42 +0100140 list_add_tail(&counter->list_entry, &ctx->counter_list);
Peter Zijlstra5c148192009-03-25 12:30:23 +0100141 else {
Ingo Molnar04289bb2008-12-11 08:38:42 +0100142 list_add_tail(&counter->list_entry, &group_leader->sibling_list);
Peter Zijlstra5c148192009-03-25 12:30:23 +0100143 group_leader->nr_siblings++;
144 }
Peter Zijlstra592903c2009-03-13 12:21:36 +0100145
146 list_add_rcu(&counter->event_entry, &ctx->event_list);
Peter Zijlstra8bc20952009-05-15 20:45:59 +0200147 ctx->nr_counters++;
Ingo Molnar04289bb2008-12-11 08:38:42 +0100148}
149
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000150/*
151 * Remove a counter from the lists for its context.
Peter Zijlstrafccc7142009-05-23 18:28:56 +0200152 * Must be called with ctx->mutex and ctx->lock held.
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000153 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100154static void
155list_del_counter(struct perf_counter *counter, struct perf_counter_context *ctx)
156{
157 struct perf_counter *sibling, *tmp;
158
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000159 if (list_empty(&counter->list_entry))
160 return;
Peter Zijlstra8bc20952009-05-15 20:45:59 +0200161 ctx->nr_counters--;
162
Ingo Molnar04289bb2008-12-11 08:38:42 +0100163 list_del_init(&counter->list_entry);
Peter Zijlstra592903c2009-03-13 12:21:36 +0100164 list_del_rcu(&counter->event_entry);
Ingo Molnar04289bb2008-12-11 08:38:42 +0100165
Peter Zijlstra5c148192009-03-25 12:30:23 +0100166 if (counter->group_leader != counter)
167 counter->group_leader->nr_siblings--;
168
Ingo Molnar04289bb2008-12-11 08:38:42 +0100169 /*
170 * If this was a group counter with sibling counters then
171 * upgrade the siblings to singleton counters by adding them
172 * to the context list directly:
173 */
174 list_for_each_entry_safe(sibling, tmp,
175 &counter->sibling_list, list_entry) {
176
Peter Zijlstra75564232009-03-13 12:21:29 +0100177 list_move_tail(&sibling->list_entry, &ctx->counter_list);
Ingo Molnar04289bb2008-12-11 08:38:42 +0100178 sibling->group_leader = sibling;
179 }
180}
181
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100182static void
183counter_sched_out(struct perf_counter *counter,
184 struct perf_cpu_context *cpuctx,
185 struct perf_counter_context *ctx)
186{
187 if (counter->state != PERF_COUNTER_STATE_ACTIVE)
188 return;
189
190 counter->state = PERF_COUNTER_STATE_INACTIVE;
Peter Zijlstra4af49982009-04-06 11:45:10 +0200191 counter->tstamp_stopped = ctx->time;
Robert Richter4aeb0b42009-04-29 12:47:03 +0200192 counter->pmu->disable(counter);
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100193 counter->oncpu = -1;
194
195 if (!is_software_counter(counter))
196 cpuctx->active_oncpu--;
197 ctx->nr_active--;
198 if (counter->hw_event.exclusive || !cpuctx->active_oncpu)
199 cpuctx->exclusive = 0;
200}
201
Paul Mackerrasd859e292009-01-17 18:10:22 +1100202static void
203group_sched_out(struct perf_counter *group_counter,
204 struct perf_cpu_context *cpuctx,
205 struct perf_counter_context *ctx)
206{
207 struct perf_counter *counter;
208
209 if (group_counter->state != PERF_COUNTER_STATE_ACTIVE)
210 return;
211
212 counter_sched_out(group_counter, cpuctx, ctx);
213
214 /*
215 * Schedule out siblings (if any):
216 */
217 list_for_each_entry(counter, &group_counter->sibling_list, list_entry)
218 counter_sched_out(counter, cpuctx, ctx);
219
220 if (group_counter->hw_event.exclusive)
221 cpuctx->exclusive = 0;
222}
223
Thomas Gleixner0793a612008-12-04 20:12:29 +0100224/*
225 * Cross CPU call to remove a performance counter
226 *
227 * We disable the counter on the hardware level first. After that we
228 * remove it from the context list.
229 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100230static void __perf_counter_remove_from_context(void *info)
Thomas Gleixner0793a612008-12-04 20:12:29 +0100231{
232 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
233 struct perf_counter *counter = info;
234 struct perf_counter_context *ctx = counter->ctx;
Ingo Molnar9b51f662008-12-12 13:49:45 +0100235 unsigned long flags;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100236
Ingo Molnar3f4dee22009-05-29 11:25:09 +0200237 local_irq_save(flags);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100238 /*
239 * If this is a task context, we need to check whether it is
240 * the current task context of this cpu. If not it has been
241 * scheduled out before the smp call arrived.
242 */
Ingo Molnar3f4dee22009-05-29 11:25:09 +0200243 if (ctx->task && cpuctx->task_ctx != ctx) {
244 local_irq_restore(flags);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100245 return;
Ingo Molnar3f4dee22009-05-29 11:25:09 +0200246 }
Thomas Gleixner0793a612008-12-04 20:12:29 +0100247
Ingo Molnar3f4dee22009-05-29 11:25:09 +0200248 spin_lock(&ctx->lock);
Ingo Molnar34adc802009-05-20 20:13:28 +0200249 /*
250 * Protect the list operation against NMI by disabling the
251 * counters on a global level.
252 */
253 perf_disable();
Thomas Gleixner0793a612008-12-04 20:12:29 +0100254
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100255 counter_sched_out(counter, cpuctx, ctx);
256
Ingo Molnar04289bb2008-12-11 08:38:42 +0100257 list_del_counter(counter, ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100258
259 if (!ctx->task) {
260 /*
261 * Allow more per task counters with respect to the
262 * reservation:
263 */
264 cpuctx->max_pertask =
265 min(perf_max_counters - ctx->nr_counters,
266 perf_max_counters - perf_reserved_percpu);
267 }
268
Ingo Molnar34adc802009-05-20 20:13:28 +0200269 perf_enable();
Peter Zijlstra849691a2009-04-06 11:45:12 +0200270 spin_unlock_irqrestore(&ctx->lock, flags);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100271}
272
273
274/*
275 * Remove the counter from a task's (or a CPU's) list of counters.
276 *
Peter Zijlstrafccc7142009-05-23 18:28:56 +0200277 * Must be called with ctx->mutex held.
Thomas Gleixner0793a612008-12-04 20:12:29 +0100278 *
279 * CPU counters are removed with a smp call. For task counters we only
280 * call when the task is on a CPU.
Paul Mackerrasc93f7662009-05-28 22:18:17 +1000281 *
282 * If counter->ctx is a cloned context, callers must make sure that
283 * every task struct that counter->ctx->task could possibly point to
284 * remains valid. This is OK when called from perf_release since
285 * that only calls us on the top-level context, which can't be a clone.
286 * When called from perf_counter_exit_task, it's OK because the
287 * context has been detached from its task.
Thomas Gleixner0793a612008-12-04 20:12:29 +0100288 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100289static void perf_counter_remove_from_context(struct perf_counter *counter)
Thomas Gleixner0793a612008-12-04 20:12:29 +0100290{
291 struct perf_counter_context *ctx = counter->ctx;
292 struct task_struct *task = ctx->task;
293
294 if (!task) {
295 /*
296 * Per cpu counters are removed via an smp call and
297 * the removal is always sucessful.
298 */
299 smp_call_function_single(counter->cpu,
Ingo Molnar04289bb2008-12-11 08:38:42 +0100300 __perf_counter_remove_from_context,
Thomas Gleixner0793a612008-12-04 20:12:29 +0100301 counter, 1);
302 return;
303 }
304
305retry:
Ingo Molnar04289bb2008-12-11 08:38:42 +0100306 task_oncpu_function_call(task, __perf_counter_remove_from_context,
Thomas Gleixner0793a612008-12-04 20:12:29 +0100307 counter);
308
309 spin_lock_irq(&ctx->lock);
310 /*
311 * If the context is active we need to retry the smp call.
312 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100313 if (ctx->nr_active && !list_empty(&counter->list_entry)) {
Thomas Gleixner0793a612008-12-04 20:12:29 +0100314 spin_unlock_irq(&ctx->lock);
315 goto retry;
316 }
317
318 /*
319 * The lock prevents that this context is scheduled in so we
Ingo Molnar04289bb2008-12-11 08:38:42 +0100320 * can remove the counter safely, if the call above did not
Thomas Gleixner0793a612008-12-04 20:12:29 +0100321 * succeed.
322 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100323 if (!list_empty(&counter->list_entry)) {
Ingo Molnar04289bb2008-12-11 08:38:42 +0100324 list_del_counter(counter, ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100325 }
326 spin_unlock_irq(&ctx->lock);
327}
328
Peter Zijlstra4af49982009-04-06 11:45:10 +0200329static inline u64 perf_clock(void)
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100330{
Peter Zijlstra4af49982009-04-06 11:45:10 +0200331 return cpu_clock(smp_processor_id());
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100332}
333
334/*
335 * Update the record of the current time in a context.
336 */
Peter Zijlstra4af49982009-04-06 11:45:10 +0200337static void update_context_time(struct perf_counter_context *ctx)
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100338{
Peter Zijlstra4af49982009-04-06 11:45:10 +0200339 u64 now = perf_clock();
340
341 ctx->time += now - ctx->timestamp;
342 ctx->timestamp = now;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100343}
344
345/*
346 * Update the total_time_enabled and total_time_running fields for a counter.
347 */
348static void update_counter_times(struct perf_counter *counter)
349{
350 struct perf_counter_context *ctx = counter->ctx;
351 u64 run_end;
352
Peter Zijlstra4af49982009-04-06 11:45:10 +0200353 if (counter->state < PERF_COUNTER_STATE_INACTIVE)
354 return;
355
356 counter->total_time_enabled = ctx->time - counter->tstamp_enabled;
357
358 if (counter->state == PERF_COUNTER_STATE_INACTIVE)
359 run_end = counter->tstamp_stopped;
360 else
361 run_end = ctx->time;
362
363 counter->total_time_running = run_end - counter->tstamp_running;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100364}
365
366/*
367 * Update total_time_enabled and total_time_running for all counters in a group.
368 */
369static void update_group_times(struct perf_counter *leader)
370{
371 struct perf_counter *counter;
372
373 update_counter_times(leader);
374 list_for_each_entry(counter, &leader->sibling_list, list_entry)
375 update_counter_times(counter);
376}
377
378/*
Paul Mackerrasd859e292009-01-17 18:10:22 +1100379 * Cross CPU call to disable a performance counter
380 */
381static void __perf_counter_disable(void *info)
382{
383 struct perf_counter *counter = info;
384 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
385 struct perf_counter_context *ctx = counter->ctx;
386 unsigned long flags;
387
Ingo Molnar3f4dee22009-05-29 11:25:09 +0200388 local_irq_save(flags);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100389 /*
390 * If this is a per-task counter, need to check whether this
391 * counter's task is the current task on this cpu.
392 */
Ingo Molnar3f4dee22009-05-29 11:25:09 +0200393 if (ctx->task && cpuctx->task_ctx != ctx) {
394 local_irq_restore(flags);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100395 return;
Ingo Molnar3f4dee22009-05-29 11:25:09 +0200396 }
Paul Mackerrasd859e292009-01-17 18:10:22 +1100397
Ingo Molnar3f4dee22009-05-29 11:25:09 +0200398 spin_lock(&ctx->lock);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100399
400 /*
401 * If the counter is on, turn it off.
402 * If it is in error state, leave it in error state.
403 */
404 if (counter->state >= PERF_COUNTER_STATE_INACTIVE) {
Peter Zijlstra4af49982009-04-06 11:45:10 +0200405 update_context_time(ctx);
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100406 update_counter_times(counter);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100407 if (counter == counter->group_leader)
408 group_sched_out(counter, cpuctx, ctx);
409 else
410 counter_sched_out(counter, cpuctx, ctx);
411 counter->state = PERF_COUNTER_STATE_OFF;
412 }
413
Peter Zijlstra849691a2009-04-06 11:45:12 +0200414 spin_unlock_irqrestore(&ctx->lock, flags);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100415}
416
417/*
418 * Disable a counter.
Paul Mackerrasc93f7662009-05-28 22:18:17 +1000419 *
420 * If counter->ctx is a cloned context, callers must make sure that
421 * every task struct that counter->ctx->task could possibly point to
422 * remains valid. This condition is satisifed when called through
423 * perf_counter_for_each_child or perf_counter_for_each because they
424 * hold the top-level counter's child_mutex, so any descendant that
425 * goes to exit will block in sync_child_counter.
426 * When called from perf_pending_counter it's OK because counter->ctx
427 * is the current context on this CPU and preemption is disabled,
428 * hence we can't get into perf_counter_task_sched_out for this context.
Paul Mackerrasd859e292009-01-17 18:10:22 +1100429 */
430static void perf_counter_disable(struct perf_counter *counter)
431{
432 struct perf_counter_context *ctx = counter->ctx;
433 struct task_struct *task = ctx->task;
434
435 if (!task) {
436 /*
437 * Disable the counter on the cpu that it's on
438 */
439 smp_call_function_single(counter->cpu, __perf_counter_disable,
440 counter, 1);
441 return;
442 }
443
444 retry:
445 task_oncpu_function_call(task, __perf_counter_disable, counter);
446
447 spin_lock_irq(&ctx->lock);
448 /*
449 * If the counter is still active, we need to retry the cross-call.
450 */
451 if (counter->state == PERF_COUNTER_STATE_ACTIVE) {
452 spin_unlock_irq(&ctx->lock);
453 goto retry;
454 }
455
456 /*
457 * Since we have the lock this context can't be scheduled
458 * in, so we can change the state safely.
459 */
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100460 if (counter->state == PERF_COUNTER_STATE_INACTIVE) {
461 update_counter_times(counter);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100462 counter->state = PERF_COUNTER_STATE_OFF;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100463 }
Paul Mackerrasd859e292009-01-17 18:10:22 +1100464
465 spin_unlock_irq(&ctx->lock);
466}
467
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100468static int
469counter_sched_in(struct perf_counter *counter,
470 struct perf_cpu_context *cpuctx,
471 struct perf_counter_context *ctx,
472 int cpu)
473{
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100474 if (counter->state <= PERF_COUNTER_STATE_OFF)
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100475 return 0;
476
477 counter->state = PERF_COUNTER_STATE_ACTIVE;
478 counter->oncpu = cpu; /* TODO: put 'cpu' into cpuctx->cpu */
479 /*
480 * The new state must be visible before we turn it on in the hardware:
481 */
482 smp_wmb();
483
Robert Richter4aeb0b42009-04-29 12:47:03 +0200484 if (counter->pmu->enable(counter)) {
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100485 counter->state = PERF_COUNTER_STATE_INACTIVE;
486 counter->oncpu = -1;
487 return -EAGAIN;
488 }
489
Peter Zijlstra4af49982009-04-06 11:45:10 +0200490 counter->tstamp_running += ctx->time - counter->tstamp_stopped;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100491
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100492 if (!is_software_counter(counter))
493 cpuctx->active_oncpu++;
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100494 ctx->nr_active++;
495
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100496 if (counter->hw_event.exclusive)
497 cpuctx->exclusive = 1;
498
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100499 return 0;
500}
501
Paul Mackerras6751b712009-05-11 12:08:02 +1000502static int
503group_sched_in(struct perf_counter *group_counter,
504 struct perf_cpu_context *cpuctx,
505 struct perf_counter_context *ctx,
506 int cpu)
507{
508 struct perf_counter *counter, *partial_group;
509 int ret;
510
511 if (group_counter->state == PERF_COUNTER_STATE_OFF)
512 return 0;
513
514 ret = hw_perf_group_sched_in(group_counter, cpuctx, ctx, cpu);
515 if (ret)
516 return ret < 0 ? ret : 0;
517
518 group_counter->prev_state = group_counter->state;
519 if (counter_sched_in(group_counter, cpuctx, ctx, cpu))
520 return -EAGAIN;
521
522 /*
523 * Schedule in siblings as one group (if any):
524 */
525 list_for_each_entry(counter, &group_counter->sibling_list, list_entry) {
526 counter->prev_state = counter->state;
527 if (counter_sched_in(counter, cpuctx, ctx, cpu)) {
528 partial_group = counter;
529 goto group_error;
530 }
531 }
532
533 return 0;
534
535group_error:
536 /*
537 * Groups can be scheduled in as one unit only, so undo any
538 * partial group before returning:
539 */
540 list_for_each_entry(counter, &group_counter->sibling_list, list_entry) {
541 if (counter == partial_group)
542 break;
543 counter_sched_out(counter, cpuctx, ctx);
544 }
545 counter_sched_out(group_counter, cpuctx, ctx);
546
547 return -EAGAIN;
548}
549
Thomas Gleixner0793a612008-12-04 20:12:29 +0100550/*
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100551 * Return 1 for a group consisting entirely of software counters,
552 * 0 if the group contains any hardware counters.
553 */
554static int is_software_only_group(struct perf_counter *leader)
555{
556 struct perf_counter *counter;
557
558 if (!is_software_counter(leader))
559 return 0;
Peter Zijlstra5c148192009-03-25 12:30:23 +0100560
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100561 list_for_each_entry(counter, &leader->sibling_list, list_entry)
562 if (!is_software_counter(counter))
563 return 0;
Peter Zijlstra5c148192009-03-25 12:30:23 +0100564
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100565 return 1;
566}
567
568/*
569 * Work out whether we can put this counter group on the CPU now.
570 */
571static int group_can_go_on(struct perf_counter *counter,
572 struct perf_cpu_context *cpuctx,
573 int can_add_hw)
574{
575 /*
576 * Groups consisting entirely of software counters can always go on.
577 */
578 if (is_software_only_group(counter))
579 return 1;
580 /*
581 * If an exclusive group is already on, no other hardware
582 * counters can go on.
583 */
584 if (cpuctx->exclusive)
585 return 0;
586 /*
587 * If this group is exclusive and there are already
588 * counters on the CPU, it can't go on.
589 */
590 if (counter->hw_event.exclusive && cpuctx->active_oncpu)
591 return 0;
592 /*
593 * Otherwise, try to add it if all previous groups were able
594 * to go on.
595 */
596 return can_add_hw;
597}
598
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100599static void add_counter_to_ctx(struct perf_counter *counter,
600 struct perf_counter_context *ctx)
601{
602 list_add_counter(counter, ctx);
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100603 counter->prev_state = PERF_COUNTER_STATE_OFF;
Peter Zijlstra4af49982009-04-06 11:45:10 +0200604 counter->tstamp_enabled = ctx->time;
605 counter->tstamp_running = ctx->time;
606 counter->tstamp_stopped = ctx->time;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100607}
608
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100609/*
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100610 * Cross CPU call to install and enable a performance counter
Peter Zijlstra682076a2009-05-23 18:28:57 +0200611 *
612 * Must be called with ctx->mutex held
Thomas Gleixner0793a612008-12-04 20:12:29 +0100613 */
614static void __perf_install_in_context(void *info)
615{
616 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
617 struct perf_counter *counter = info;
618 struct perf_counter_context *ctx = counter->ctx;
Paul Mackerrasd859e292009-01-17 18:10:22 +1100619 struct perf_counter *leader = counter->group_leader;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100620 int cpu = smp_processor_id();
Ingo Molnar9b51f662008-12-12 13:49:45 +0100621 unsigned long flags;
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100622 int err;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100623
Ingo Molnar3f4dee22009-05-29 11:25:09 +0200624 local_irq_save(flags);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100625 /*
626 * If this is a task context, we need to check whether it is
627 * the current task context of this cpu. If not it has been
628 * scheduled out before the smp call arrived.
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000629 * Or possibly this is the right context but it isn't
630 * on this cpu because it had no counters.
Thomas Gleixner0793a612008-12-04 20:12:29 +0100631 */
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000632 if (ctx->task && cpuctx->task_ctx != ctx) {
Ingo Molnar3f4dee22009-05-29 11:25:09 +0200633 if (cpuctx->task_ctx || ctx->task != current) {
634 local_irq_restore(flags);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000635 return;
Ingo Molnar3f4dee22009-05-29 11:25:09 +0200636 }
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000637 cpuctx->task_ctx = ctx;
638 }
Thomas Gleixner0793a612008-12-04 20:12:29 +0100639
Ingo Molnar3f4dee22009-05-29 11:25:09 +0200640 spin_lock(&ctx->lock);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000641 ctx->is_active = 1;
Peter Zijlstra4af49982009-04-06 11:45:10 +0200642 update_context_time(ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100643
644 /*
645 * Protect the list operation against NMI by disabling the
646 * counters on a global level. NOP for non NMI based counters.
647 */
Peter Zijlstra9e35ad32009-05-13 16:21:38 +0200648 perf_disable();
Thomas Gleixner0793a612008-12-04 20:12:29 +0100649
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100650 add_counter_to_ctx(counter, ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100651
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100652 /*
Paul Mackerrasd859e292009-01-17 18:10:22 +1100653 * Don't put the counter on if it is disabled or if
654 * it is in a group and the group isn't on.
655 */
656 if (counter->state != PERF_COUNTER_STATE_INACTIVE ||
657 (leader != counter && leader->state != PERF_COUNTER_STATE_ACTIVE))
658 goto unlock;
659
660 /*
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100661 * An exclusive counter can't go on if there are already active
662 * hardware counters, and no hardware counter can go on if there
663 * is already an exclusive counter on.
664 */
Paul Mackerrasd859e292009-01-17 18:10:22 +1100665 if (!group_can_go_on(counter, cpuctx, 1))
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100666 err = -EEXIST;
667 else
668 err = counter_sched_in(counter, cpuctx, ctx, cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100669
Paul Mackerrasd859e292009-01-17 18:10:22 +1100670 if (err) {
671 /*
672 * This counter couldn't go on. If it is in a group
673 * then we have to pull the whole group off.
674 * If the counter group is pinned then put it in error state.
675 */
676 if (leader != counter)
677 group_sched_out(leader, cpuctx, ctx);
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100678 if (leader->hw_event.pinned) {
679 update_group_times(leader);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100680 leader->state = PERF_COUNTER_STATE_ERROR;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100681 }
Paul Mackerrasd859e292009-01-17 18:10:22 +1100682 }
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100683
684 if (!err && !ctx->task && cpuctx->max_pertask)
Thomas Gleixner0793a612008-12-04 20:12:29 +0100685 cpuctx->max_pertask--;
686
Paul Mackerrasd859e292009-01-17 18:10:22 +1100687 unlock:
Peter Zijlstra9e35ad32009-05-13 16:21:38 +0200688 perf_enable();
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100689
Peter Zijlstra849691a2009-04-06 11:45:12 +0200690 spin_unlock_irqrestore(&ctx->lock, flags);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100691}
692
693/*
694 * Attach a performance counter to a context
695 *
696 * First we add the counter to the list with the hardware enable bit
697 * in counter->hw_config cleared.
698 *
699 * If the counter is attached to a task which is on a CPU we use a smp
700 * call to enable it in the task context. The task might have been
701 * scheduled away, but we check this in the smp call again.
Paul Mackerrasd859e292009-01-17 18:10:22 +1100702 *
703 * Must be called with ctx->mutex held.
Thomas Gleixner0793a612008-12-04 20:12:29 +0100704 */
705static void
706perf_install_in_context(struct perf_counter_context *ctx,
707 struct perf_counter *counter,
708 int cpu)
709{
710 struct task_struct *task = ctx->task;
711
Thomas Gleixner0793a612008-12-04 20:12:29 +0100712 if (!task) {
713 /*
714 * Per cpu counters are installed via an smp call and
715 * the install is always sucessful.
716 */
717 smp_call_function_single(cpu, __perf_install_in_context,
718 counter, 1);
719 return;
720 }
721
Thomas Gleixner0793a612008-12-04 20:12:29 +0100722retry:
723 task_oncpu_function_call(task, __perf_install_in_context,
724 counter);
725
726 spin_lock_irq(&ctx->lock);
727 /*
Thomas Gleixner0793a612008-12-04 20:12:29 +0100728 * we need to retry the smp call.
729 */
Paul Mackerrasd859e292009-01-17 18:10:22 +1100730 if (ctx->is_active && list_empty(&counter->list_entry)) {
Thomas Gleixner0793a612008-12-04 20:12:29 +0100731 spin_unlock_irq(&ctx->lock);
732 goto retry;
733 }
734
735 /*
736 * The lock prevents that this context is scheduled in so we
737 * can add the counter safely, if it the call above did not
738 * succeed.
739 */
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100740 if (list_empty(&counter->list_entry))
741 add_counter_to_ctx(counter, ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100742 spin_unlock_irq(&ctx->lock);
743}
744
Paul Mackerrasd859e292009-01-17 18:10:22 +1100745/*
746 * Cross CPU call to enable a performance counter
747 */
748static void __perf_counter_enable(void *info)
Ingo Molnar04289bb2008-12-11 08:38:42 +0100749{
Paul Mackerrasd859e292009-01-17 18:10:22 +1100750 struct perf_counter *counter = info;
751 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
752 struct perf_counter_context *ctx = counter->ctx;
753 struct perf_counter *leader = counter->group_leader;
754 unsigned long flags;
755 int err;
Ingo Molnar04289bb2008-12-11 08:38:42 +0100756
Ingo Molnar3f4dee22009-05-29 11:25:09 +0200757 local_irq_save(flags);
Ingo Molnar04289bb2008-12-11 08:38:42 +0100758 /*
Paul Mackerrasd859e292009-01-17 18:10:22 +1100759 * If this is a per-task counter, need to check whether this
760 * counter's task is the current task on this cpu.
Ingo Molnar04289bb2008-12-11 08:38:42 +0100761 */
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000762 if (ctx->task && cpuctx->task_ctx != ctx) {
Ingo Molnar3f4dee22009-05-29 11:25:09 +0200763 if (cpuctx->task_ctx || ctx->task != current) {
764 local_irq_restore(flags);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000765 return;
Ingo Molnar3f4dee22009-05-29 11:25:09 +0200766 }
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000767 cpuctx->task_ctx = ctx;
768 }
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100769
Ingo Molnar3f4dee22009-05-29 11:25:09 +0200770 spin_lock(&ctx->lock);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000771 ctx->is_active = 1;
Peter Zijlstra4af49982009-04-06 11:45:10 +0200772 update_context_time(ctx);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100773
Paul Mackerrasc07c99b2009-02-13 22:10:34 +1100774 counter->prev_state = counter->state;
Paul Mackerrasd859e292009-01-17 18:10:22 +1100775 if (counter->state >= PERF_COUNTER_STATE_INACTIVE)
776 goto unlock;
777 counter->state = PERF_COUNTER_STATE_INACTIVE;
Peter Zijlstra4af49982009-04-06 11:45:10 +0200778 counter->tstamp_enabled = ctx->time - counter->total_time_enabled;
Paul Mackerrasd859e292009-01-17 18:10:22 +1100779
780 /*
781 * If the counter is in a group and isn't the group leader,
782 * then don't put it on unless the group is on.
783 */
784 if (leader != counter && leader->state != PERF_COUNTER_STATE_ACTIVE)
785 goto unlock;
786
Paul Mackerrase758a332009-05-12 21:59:01 +1000787 if (!group_can_go_on(counter, cpuctx, 1)) {
Paul Mackerrasd859e292009-01-17 18:10:22 +1100788 err = -EEXIST;
Paul Mackerrase758a332009-05-12 21:59:01 +1000789 } else {
Peter Zijlstra9e35ad32009-05-13 16:21:38 +0200790 perf_disable();
Paul Mackerrase758a332009-05-12 21:59:01 +1000791 if (counter == leader)
792 err = group_sched_in(counter, cpuctx, ctx,
793 smp_processor_id());
794 else
795 err = counter_sched_in(counter, cpuctx, ctx,
796 smp_processor_id());
Peter Zijlstra9e35ad32009-05-13 16:21:38 +0200797 perf_enable();
Paul Mackerrase758a332009-05-12 21:59:01 +1000798 }
Paul Mackerrasd859e292009-01-17 18:10:22 +1100799
800 if (err) {
801 /*
802 * If this counter can't go on and it's part of a
803 * group, then the whole group has to come off.
804 */
805 if (leader != counter)
806 group_sched_out(leader, cpuctx, ctx);
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100807 if (leader->hw_event.pinned) {
808 update_group_times(leader);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100809 leader->state = PERF_COUNTER_STATE_ERROR;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100810 }
Paul Mackerrasd859e292009-01-17 18:10:22 +1100811 }
812
813 unlock:
Peter Zijlstra849691a2009-04-06 11:45:12 +0200814 spin_unlock_irqrestore(&ctx->lock, flags);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100815}
816
817/*
818 * Enable a counter.
Paul Mackerrasc93f7662009-05-28 22:18:17 +1000819 *
820 * If counter->ctx is a cloned context, callers must make sure that
821 * every task struct that counter->ctx->task could possibly point to
822 * remains valid. This condition is satisfied when called through
823 * perf_counter_for_each_child or perf_counter_for_each as described
824 * for perf_counter_disable.
Paul Mackerrasd859e292009-01-17 18:10:22 +1100825 */
826static void perf_counter_enable(struct perf_counter *counter)
827{
828 struct perf_counter_context *ctx = counter->ctx;
829 struct task_struct *task = ctx->task;
830
831 if (!task) {
832 /*
833 * Enable the counter on the cpu that it's on
834 */
835 smp_call_function_single(counter->cpu, __perf_counter_enable,
836 counter, 1);
837 return;
838 }
839
840 spin_lock_irq(&ctx->lock);
841 if (counter->state >= PERF_COUNTER_STATE_INACTIVE)
842 goto out;
843
844 /*
845 * If the counter is in error state, clear that first.
846 * That way, if we see the counter in error state below, we
847 * know that it has gone back into error state, as distinct
848 * from the task having been scheduled away before the
849 * cross-call arrived.
850 */
851 if (counter->state == PERF_COUNTER_STATE_ERROR)
852 counter->state = PERF_COUNTER_STATE_OFF;
853
854 retry:
855 spin_unlock_irq(&ctx->lock);
856 task_oncpu_function_call(task, __perf_counter_enable, counter);
857
858 spin_lock_irq(&ctx->lock);
859
860 /*
861 * If the context is active and the counter is still off,
862 * we need to retry the cross-call.
863 */
864 if (ctx->is_active && counter->state == PERF_COUNTER_STATE_OFF)
865 goto retry;
866
867 /*
868 * Since we have the lock this context can't be scheduled
869 * in, so we can change the state safely.
870 */
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100871 if (counter->state == PERF_COUNTER_STATE_OFF) {
Paul Mackerrasd859e292009-01-17 18:10:22 +1100872 counter->state = PERF_COUNTER_STATE_INACTIVE;
Peter Zijlstra4af49982009-04-06 11:45:10 +0200873 counter->tstamp_enabled =
874 ctx->time - counter->total_time_enabled;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100875 }
Paul Mackerrasd859e292009-01-17 18:10:22 +1100876 out:
877 spin_unlock_irq(&ctx->lock);
878}
879
Peter Zijlstra2023b352009-05-05 17:50:26 +0200880static int perf_counter_refresh(struct perf_counter *counter, int refresh)
Peter Zijlstra79f14642009-04-06 11:45:07 +0200881{
Peter Zijlstra2023b352009-05-05 17:50:26 +0200882 /*
883 * not supported on inherited counters
884 */
885 if (counter->hw_event.inherit)
886 return -EINVAL;
887
Peter Zijlstra79f14642009-04-06 11:45:07 +0200888 atomic_add(refresh, &counter->event_limit);
889 perf_counter_enable(counter);
Peter Zijlstra2023b352009-05-05 17:50:26 +0200890
891 return 0;
Peter Zijlstra79f14642009-04-06 11:45:07 +0200892}
893
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100894void __perf_counter_sched_out(struct perf_counter_context *ctx,
895 struct perf_cpu_context *cpuctx)
896{
897 struct perf_counter *counter;
898
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100899 spin_lock(&ctx->lock);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100900 ctx->is_active = 0;
901 if (likely(!ctx->nr_counters))
902 goto out;
Peter Zijlstra4af49982009-04-06 11:45:10 +0200903 update_context_time(ctx);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100904
Peter Zijlstra9e35ad32009-05-13 16:21:38 +0200905 perf_disable();
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100906 if (ctx->nr_active) {
Peter Zijlstraafedadf2009-05-20 12:21:22 +0200907 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
908 if (counter != counter->group_leader)
909 counter_sched_out(counter, cpuctx, ctx);
910 else
911 group_sched_out(counter, cpuctx, ctx);
912 }
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100913 }
Peter Zijlstra9e35ad32009-05-13 16:21:38 +0200914 perf_enable();
Paul Mackerrasd859e292009-01-17 18:10:22 +1100915 out:
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100916 spin_unlock(&ctx->lock);
917}
918
Thomas Gleixner0793a612008-12-04 20:12:29 +0100919/*
Paul Mackerras564c2b22009-05-22 14:27:22 +1000920 * Test whether two contexts are equivalent, i.e. whether they
921 * have both been cloned from the same version of the same context
922 * and they both have the same number of enabled counters.
923 * If the number of enabled counters is the same, then the set
924 * of enabled counters should be the same, because these are both
925 * inherited contexts, therefore we can't access individual counters
926 * in them directly with an fd; we can only enable/disable all
927 * counters via prctl, or enable/disable all counters in a family
928 * via ioctl, which will have the same effect on both contexts.
929 */
930static int context_equiv(struct perf_counter_context *ctx1,
931 struct perf_counter_context *ctx2)
932{
933 return ctx1->parent_ctx && ctx1->parent_ctx == ctx2->parent_ctx
Paul Mackerrasad3a37d2009-05-29 16:06:20 +1000934 && ctx1->parent_gen == ctx2->parent_gen
935 && ctx1->parent_gen != ~0ull;
Paul Mackerras564c2b22009-05-22 14:27:22 +1000936}
937
938/*
Thomas Gleixner0793a612008-12-04 20:12:29 +0100939 * Called from scheduler to remove the counters of the current task,
940 * with interrupts disabled.
941 *
942 * We stop each counter and update the counter value in counter->count.
943 *
Ingo Molnar76715812008-12-17 14:20:28 +0100944 * This does not protect us against NMI, but disable()
Thomas Gleixner0793a612008-12-04 20:12:29 +0100945 * sets the disabled bit in the control field of counter _before_
946 * accessing the counter control register. If a NMI hits, then it will
947 * not restart the counter.
948 */
Paul Mackerras564c2b22009-05-22 14:27:22 +1000949void perf_counter_task_sched_out(struct task_struct *task,
950 struct task_struct *next, int cpu)
Thomas Gleixner0793a612008-12-04 20:12:29 +0100951{
952 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000953 struct perf_counter_context *ctx = task->perf_counter_ctxp;
Paul Mackerras564c2b22009-05-22 14:27:22 +1000954 struct perf_counter_context *next_ctx;
Paul Mackerrasc93f7662009-05-28 22:18:17 +1000955 struct perf_counter_context *parent;
Peter Zijlstra4a0deca2009-03-19 20:26:12 +0100956 struct pt_regs *regs;
Paul Mackerrasc93f7662009-05-28 22:18:17 +1000957 int do_switch = 1;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100958
Peter Zijlstra10989fb2009-05-25 14:45:28 +0200959 regs = task_pt_regs(task);
960 perf_swcounter_event(PERF_COUNT_CONTEXT_SWITCHES, 1, 1, regs, 0);
961
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000962 if (likely(!ctx || !cpuctx->task_ctx))
Thomas Gleixner0793a612008-12-04 20:12:29 +0100963 return;
964
Peter Zijlstrabce379b2009-04-06 11:45:13 +0200965 update_context_time(ctx);
Paul Mackerrasc93f7662009-05-28 22:18:17 +1000966
967 rcu_read_lock();
968 parent = rcu_dereference(ctx->parent_ctx);
Paul Mackerras564c2b22009-05-22 14:27:22 +1000969 next_ctx = next->perf_counter_ctxp;
Paul Mackerrasc93f7662009-05-28 22:18:17 +1000970 if (parent && next_ctx &&
971 rcu_dereference(next_ctx->parent_ctx) == parent) {
972 /*
973 * Looks like the two contexts are clones, so we might be
974 * able to optimize the context switch. We lock both
975 * contexts and check that they are clones under the
976 * lock (including re-checking that neither has been
977 * uncloned in the meantime). It doesn't matter which
978 * order we take the locks because no other cpu could
979 * be trying to lock both of these tasks.
980 */
981 spin_lock(&ctx->lock);
982 spin_lock_nested(&next_ctx->lock, SINGLE_DEPTH_NESTING);
983 if (context_equiv(ctx, next_ctx)) {
984 task->perf_counter_ctxp = next_ctx;
985 next->perf_counter_ctxp = ctx;
986 ctx->task = next;
987 next_ctx->task = task;
988 do_switch = 0;
989 }
990 spin_unlock(&next_ctx->lock);
991 spin_unlock(&ctx->lock);
Paul Mackerras564c2b22009-05-22 14:27:22 +1000992 }
Paul Mackerrasc93f7662009-05-28 22:18:17 +1000993 rcu_read_unlock();
Paul Mackerras564c2b22009-05-22 14:27:22 +1000994
Paul Mackerrasc93f7662009-05-28 22:18:17 +1000995 if (do_switch) {
996 __perf_counter_sched_out(ctx, cpuctx);
997 cpuctx->task_ctx = NULL;
998 }
Thomas Gleixner0793a612008-12-04 20:12:29 +0100999}
1000
Paul Mackerrasa08b1592009-05-11 15:46:10 +10001001static void __perf_counter_task_sched_out(struct perf_counter_context *ctx)
1002{
1003 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
1004
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001005 if (!cpuctx->task_ctx)
1006 return;
Ingo Molnar012b84d2009-05-17 11:08:41 +02001007
1008 if (WARN_ON_ONCE(ctx != cpuctx->task_ctx))
1009 return;
1010
Paul Mackerrasa08b1592009-05-11 15:46:10 +10001011 __perf_counter_sched_out(ctx, cpuctx);
1012 cpuctx->task_ctx = NULL;
1013}
1014
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001015static void perf_counter_cpu_sched_out(struct perf_cpu_context *cpuctx)
Ingo Molnar04289bb2008-12-11 08:38:42 +01001016{
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001017 __perf_counter_sched_out(&cpuctx->ctx, cpuctx);
Ingo Molnar04289bb2008-12-11 08:38:42 +01001018}
1019
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001020static void
1021__perf_counter_sched_in(struct perf_counter_context *ctx,
1022 struct perf_cpu_context *cpuctx, int cpu)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001023{
Thomas Gleixner0793a612008-12-04 20:12:29 +01001024 struct perf_counter *counter;
Paul Mackerrasdd0e6ba2009-01-12 15:11:00 +11001025 int can_add_hw = 1;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001026
Thomas Gleixner0793a612008-12-04 20:12:29 +01001027 spin_lock(&ctx->lock);
Paul Mackerrasd859e292009-01-17 18:10:22 +11001028 ctx->is_active = 1;
1029 if (likely(!ctx->nr_counters))
1030 goto out;
1031
Peter Zijlstra4af49982009-04-06 11:45:10 +02001032 ctx->timestamp = perf_clock();
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001033
Peter Zijlstra9e35ad32009-05-13 16:21:38 +02001034 perf_disable();
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001035
1036 /*
1037 * First go through the list and put on any pinned groups
1038 * in order to give them the best chance of going on.
1039 */
Ingo Molnar04289bb2008-12-11 08:38:42 +01001040 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001041 if (counter->state <= PERF_COUNTER_STATE_OFF ||
1042 !counter->hw_event.pinned)
1043 continue;
1044 if (counter->cpu != -1 && counter->cpu != cpu)
1045 continue;
1046
Peter Zijlstraafedadf2009-05-20 12:21:22 +02001047 if (counter != counter->group_leader)
1048 counter_sched_in(counter, cpuctx, ctx, cpu);
1049 else {
1050 if (group_can_go_on(counter, cpuctx, 1))
1051 group_sched_in(counter, cpuctx, ctx, cpu);
1052 }
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001053
1054 /*
1055 * If this pinned group hasn't been scheduled,
1056 * put it in error state.
1057 */
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001058 if (counter->state == PERF_COUNTER_STATE_INACTIVE) {
1059 update_group_times(counter);
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001060 counter->state = PERF_COUNTER_STATE_ERROR;
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001061 }
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001062 }
1063
1064 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
1065 /*
1066 * Ignore counters in OFF or ERROR state, and
1067 * ignore pinned counters since we did them already.
1068 */
1069 if (counter->state <= PERF_COUNTER_STATE_OFF ||
1070 counter->hw_event.pinned)
1071 continue;
1072
Ingo Molnar04289bb2008-12-11 08:38:42 +01001073 /*
1074 * Listen to the 'cpu' scheduling filter constraint
1075 * of counters:
1076 */
Thomas Gleixner0793a612008-12-04 20:12:29 +01001077 if (counter->cpu != -1 && counter->cpu != cpu)
1078 continue;
1079
Peter Zijlstraafedadf2009-05-20 12:21:22 +02001080 if (counter != counter->group_leader) {
1081 if (counter_sched_in(counter, cpuctx, ctx, cpu))
Paul Mackerrasdd0e6ba2009-01-12 15:11:00 +11001082 can_add_hw = 0;
Peter Zijlstraafedadf2009-05-20 12:21:22 +02001083 } else {
1084 if (group_can_go_on(counter, cpuctx, can_add_hw)) {
1085 if (group_sched_in(counter, cpuctx, ctx, cpu))
1086 can_add_hw = 0;
1087 }
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001088 }
Thomas Gleixner0793a612008-12-04 20:12:29 +01001089 }
Peter Zijlstra9e35ad32009-05-13 16:21:38 +02001090 perf_enable();
Paul Mackerrasd859e292009-01-17 18:10:22 +11001091 out:
Thomas Gleixner0793a612008-12-04 20:12:29 +01001092 spin_unlock(&ctx->lock);
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001093}
Ingo Molnar04289bb2008-12-11 08:38:42 +01001094
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001095/*
1096 * Called from scheduler to add the counters of the current task
1097 * with interrupts disabled.
1098 *
1099 * We restore the counter value and then enable it.
1100 *
1101 * This does not protect us against NMI, but enable()
1102 * sets the enabled bit in the control field of counter _before_
1103 * accessing the counter control register. If a NMI hits, then it will
1104 * keep the counter running.
1105 */
1106void perf_counter_task_sched_in(struct task_struct *task, int cpu)
1107{
1108 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001109 struct perf_counter_context *ctx = task->perf_counter_ctxp;
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001110
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001111 if (likely(!ctx))
1112 return;
Paul Mackerras564c2b22009-05-22 14:27:22 +10001113 if (cpuctx->task_ctx == ctx)
1114 return;
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001115 __perf_counter_sched_in(ctx, cpuctx, cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001116 cpuctx->task_ctx = ctx;
1117}
1118
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001119static void perf_counter_cpu_sched_in(struct perf_cpu_context *cpuctx, int cpu)
1120{
1121 struct perf_counter_context *ctx = &cpuctx->ctx;
1122
1123 __perf_counter_sched_in(ctx, cpuctx, cpu);
1124}
1125
Peter Zijlstraa78ac322009-05-25 17:39:05 +02001126#define MAX_INTERRUPTS (~0ULL)
1127
1128static void perf_log_throttle(struct perf_counter *counter, int enable);
Peter Zijlstra26b119b2009-05-20 12:21:20 +02001129static void perf_log_period(struct perf_counter *counter, u64 period);
1130
1131static void perf_adjust_freq(struct perf_counter_context *ctx)
Peter Zijlstra60db5e02009-05-15 15:19:28 +02001132{
1133 struct perf_counter *counter;
Peter Zijlstraa78ac322009-05-25 17:39:05 +02001134 u64 interrupts, irq_period;
Peter Zijlstra60db5e02009-05-15 15:19:28 +02001135 u64 events, period;
1136 s64 delta;
1137
1138 spin_lock(&ctx->lock);
1139 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
1140 if (counter->state != PERF_COUNTER_STATE_ACTIVE)
1141 continue;
1142
Peter Zijlstraa78ac322009-05-25 17:39:05 +02001143 interrupts = counter->hw.interrupts;
1144 counter->hw.interrupts = 0;
1145
1146 if (interrupts == MAX_INTERRUPTS) {
1147 perf_log_throttle(counter, 1);
1148 counter->pmu->unthrottle(counter);
1149 interrupts = 2*sysctl_perf_counter_limit/HZ;
1150 }
1151
Peter Zijlstra60db5e02009-05-15 15:19:28 +02001152 if (!counter->hw_event.freq || !counter->hw_event.irq_freq)
1153 continue;
1154
Peter Zijlstraa78ac322009-05-25 17:39:05 +02001155 events = HZ * interrupts * counter->hw.irq_period;
Peter Zijlstra60db5e02009-05-15 15:19:28 +02001156 period = div64_u64(events, counter->hw_event.irq_freq);
1157
1158 delta = (s64)(1 + period - counter->hw.irq_period);
1159 delta >>= 1;
1160
1161 irq_period = counter->hw.irq_period + delta;
1162
1163 if (!irq_period)
1164 irq_period = 1;
1165
Peter Zijlstra26b119b2009-05-20 12:21:20 +02001166 perf_log_period(counter, irq_period);
1167
Peter Zijlstra60db5e02009-05-15 15:19:28 +02001168 counter->hw.irq_period = irq_period;
Peter Zijlstra60db5e02009-05-15 15:19:28 +02001169 }
1170 spin_unlock(&ctx->lock);
1171}
1172
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001173/*
1174 * Round-robin a context's counters:
1175 */
1176static void rotate_ctx(struct perf_counter_context *ctx)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001177{
Thomas Gleixner0793a612008-12-04 20:12:29 +01001178 struct perf_counter *counter;
1179
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001180 if (!ctx->nr_counters)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001181 return;
1182
Thomas Gleixner0793a612008-12-04 20:12:29 +01001183 spin_lock(&ctx->lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001184 /*
Ingo Molnar04289bb2008-12-11 08:38:42 +01001185 * Rotate the first entry last (works just fine for group counters too):
Thomas Gleixner0793a612008-12-04 20:12:29 +01001186 */
Peter Zijlstra9e35ad32009-05-13 16:21:38 +02001187 perf_disable();
Ingo Molnar04289bb2008-12-11 08:38:42 +01001188 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
Peter Zijlstra75564232009-03-13 12:21:29 +01001189 list_move_tail(&counter->list_entry, &ctx->counter_list);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001190 break;
1191 }
Peter Zijlstra9e35ad32009-05-13 16:21:38 +02001192 perf_enable();
Thomas Gleixner0793a612008-12-04 20:12:29 +01001193
1194 spin_unlock(&ctx->lock);
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001195}
Thomas Gleixner0793a612008-12-04 20:12:29 +01001196
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001197void perf_counter_task_tick(struct task_struct *curr, int cpu)
1198{
Peter Zijlstra7fc23a52009-05-08 18:52:21 +02001199 struct perf_cpu_context *cpuctx;
1200 struct perf_counter_context *ctx;
1201
1202 if (!atomic_read(&nr_counters))
1203 return;
1204
1205 cpuctx = &per_cpu(perf_cpu_context, cpu);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001206 ctx = curr->perf_counter_ctxp;
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001207
Peter Zijlstra60db5e02009-05-15 15:19:28 +02001208 perf_adjust_freq(&cpuctx->ctx);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001209 if (ctx)
1210 perf_adjust_freq(ctx);
Peter Zijlstra60db5e02009-05-15 15:19:28 +02001211
Ingo Molnarb82914c2009-05-04 18:54:32 +02001212 perf_counter_cpu_sched_out(cpuctx);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001213 if (ctx)
1214 __perf_counter_task_sched_out(ctx);
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001215
Ingo Molnarb82914c2009-05-04 18:54:32 +02001216 rotate_ctx(&cpuctx->ctx);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001217 if (ctx)
1218 rotate_ctx(ctx);
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001219
Ingo Molnarb82914c2009-05-04 18:54:32 +02001220 perf_counter_cpu_sched_in(cpuctx, cpu);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001221 if (ctx)
1222 perf_counter_task_sched_in(curr, cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001223}
1224
1225/*
Thomas Gleixner0793a612008-12-04 20:12:29 +01001226 * Cross CPU call to read the hardware counter
1227 */
Ingo Molnar76715812008-12-17 14:20:28 +01001228static void __read(void *info)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001229{
Ingo Molnar621a01e2008-12-11 12:46:46 +01001230 struct perf_counter *counter = info;
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001231 struct perf_counter_context *ctx = counter->ctx;
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01001232 unsigned long flags;
Ingo Molnar621a01e2008-12-11 12:46:46 +01001233
Peter Zijlstra849691a2009-04-06 11:45:12 +02001234 local_irq_save(flags);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001235 if (ctx->is_active)
Peter Zijlstra4af49982009-04-06 11:45:10 +02001236 update_context_time(ctx);
Robert Richter4aeb0b42009-04-29 12:47:03 +02001237 counter->pmu->read(counter);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001238 update_counter_times(counter);
Peter Zijlstra849691a2009-04-06 11:45:12 +02001239 local_irq_restore(flags);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001240}
1241
Ingo Molnar04289bb2008-12-11 08:38:42 +01001242static u64 perf_counter_read(struct perf_counter *counter)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001243{
1244 /*
1245 * If counter is enabled and currently active on a CPU, update the
1246 * value in the counter structure:
1247 */
Ingo Molnar6a930702008-12-11 15:17:03 +01001248 if (counter->state == PERF_COUNTER_STATE_ACTIVE) {
Thomas Gleixner0793a612008-12-04 20:12:29 +01001249 smp_call_function_single(counter->oncpu,
Ingo Molnar76715812008-12-17 14:20:28 +01001250 __read, counter, 1);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001251 } else if (counter->state == PERF_COUNTER_STATE_INACTIVE) {
1252 update_counter_times(counter);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001253 }
1254
Ingo Molnaree060942008-12-13 09:00:03 +01001255 return atomic64_read(&counter->count);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001256}
1257
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001258/*
1259 * Initialize the perf_counter context in a task_struct:
1260 */
1261static void
1262__perf_counter_init_context(struct perf_counter_context *ctx,
1263 struct task_struct *task)
1264{
1265 memset(ctx, 0, sizeof(*ctx));
1266 spin_lock_init(&ctx->lock);
1267 mutex_init(&ctx->mutex);
1268 INIT_LIST_HEAD(&ctx->counter_list);
1269 INIT_LIST_HEAD(&ctx->event_list);
1270 atomic_set(&ctx->refcount, 1);
1271 ctx->task = task;
1272}
1273
Thomas Gleixner0793a612008-12-04 20:12:29 +01001274static struct perf_counter_context *find_get_context(pid_t pid, int cpu)
1275{
1276 struct perf_cpu_context *cpuctx;
1277 struct perf_counter_context *ctx;
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001278 struct perf_counter_context *parent_ctx;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001279 struct task_struct *task;
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001280 int err;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001281
1282 /*
1283 * If cpu is not a wildcard then this is a percpu counter:
1284 */
1285 if (cpu != -1) {
1286 /* Must be root to operate on a CPU counter: */
Peter Zijlstra1ccd1542009-04-09 10:53:45 +02001287 if (sysctl_perf_counter_priv && !capable(CAP_SYS_ADMIN))
Thomas Gleixner0793a612008-12-04 20:12:29 +01001288 return ERR_PTR(-EACCES);
1289
1290 if (cpu < 0 || cpu > num_possible_cpus())
1291 return ERR_PTR(-EINVAL);
1292
1293 /*
1294 * We could be clever and allow to attach a counter to an
1295 * offline CPU and activate it when the CPU comes up, but
1296 * that's for later.
1297 */
1298 if (!cpu_isset(cpu, cpu_online_map))
1299 return ERR_PTR(-ENODEV);
1300
1301 cpuctx = &per_cpu(perf_cpu_context, cpu);
1302 ctx = &cpuctx->ctx;
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001303 get_ctx(ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001304
Thomas Gleixner0793a612008-12-04 20:12:29 +01001305 return ctx;
1306 }
1307
1308 rcu_read_lock();
1309 if (!pid)
1310 task = current;
1311 else
1312 task = find_task_by_vpid(pid);
1313 if (task)
1314 get_task_struct(task);
1315 rcu_read_unlock();
1316
1317 if (!task)
1318 return ERR_PTR(-ESRCH);
1319
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001320 /*
1321 * Can't attach counters to a dying task.
1322 */
1323 err = -ESRCH;
1324 if (task->flags & PF_EXITING)
1325 goto errout;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001326
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001327 /* Reuse ptrace permission checks for now. */
1328 err = -EACCES;
1329 if (!ptrace_may_access(task, PTRACE_MODE_READ))
1330 goto errout;
1331
1332 retry_lock:
1333 rcu_read_lock();
1334 retry:
1335 ctx = rcu_dereference(task->perf_counter_ctxp);
1336 if (ctx) {
1337 /*
1338 * If this context is a clone of another, it might
1339 * get swapped for another underneath us by
1340 * perf_counter_task_sched_out, though the
1341 * rcu_read_lock() protects us from any context
1342 * getting freed. Lock the context and check if it
1343 * got swapped before we could get the lock, and retry
1344 * if so. If we locked the right context, then it
1345 * can't get swapped on us any more and we can
1346 * unclone it if necessary.
1347 * Once it's not a clone things will be stable.
1348 */
1349 spin_lock_irq(&ctx->lock);
1350 if (ctx != rcu_dereference(task->perf_counter_ctxp)) {
1351 spin_unlock_irq(&ctx->lock);
1352 goto retry;
1353 }
1354 parent_ctx = ctx->parent_ctx;
1355 if (parent_ctx) {
1356 put_ctx(parent_ctx);
1357 ctx->parent_ctx = NULL; /* no longer a clone */
1358 }
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001359 /*
1360 * Get an extra reference before dropping the lock so that
1361 * this context won't get freed if the task exits.
1362 */
1363 get_ctx(ctx);
1364 spin_unlock_irq(&ctx->lock);
1365 }
1366 rcu_read_unlock();
1367
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001368 if (!ctx) {
1369 ctx = kmalloc(sizeof(struct perf_counter_context), GFP_KERNEL);
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001370 err = -ENOMEM;
1371 if (!ctx)
1372 goto errout;
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001373 __perf_counter_init_context(ctx, task);
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001374 get_ctx(ctx);
1375 if (cmpxchg(&task->perf_counter_ctxp, NULL, ctx)) {
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001376 /*
1377 * We raced with some other task; use
1378 * the context they set.
1379 */
1380 kfree(ctx);
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001381 goto retry_lock;
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001382 }
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001383 get_task_struct(task);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001384 }
1385
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001386 put_task_struct(task);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001387 return ctx;
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001388
1389 errout:
1390 put_task_struct(task);
1391 return ERR_PTR(err);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001392}
1393
Peter Zijlstra592903c2009-03-13 12:21:36 +01001394static void free_counter_rcu(struct rcu_head *head)
1395{
1396 struct perf_counter *counter;
1397
1398 counter = container_of(head, struct perf_counter, rcu_head);
1399 kfree(counter);
1400}
1401
Peter Zijlstra925d5192009-03-30 19:07:02 +02001402static void perf_pending_sync(struct perf_counter *counter);
1403
Peter Zijlstraf1600952009-03-19 20:26:16 +01001404static void free_counter(struct perf_counter *counter)
1405{
Peter Zijlstra925d5192009-03-30 19:07:02 +02001406 perf_pending_sync(counter);
1407
Peter Zijlstra7fc23a52009-05-08 18:52:21 +02001408 atomic_dec(&nr_counters);
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02001409 if (counter->hw_event.mmap)
1410 atomic_dec(&nr_mmap_tracking);
1411 if (counter->hw_event.munmap)
1412 atomic_dec(&nr_munmap_tracking);
1413 if (counter->hw_event.comm)
1414 atomic_dec(&nr_comm_tracking);
1415
Peter Zijlstrae077df42009-03-19 20:26:17 +01001416 if (counter->destroy)
1417 counter->destroy(counter);
1418
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001419 put_ctx(counter->ctx);
Peter Zijlstraf1600952009-03-19 20:26:16 +01001420 call_rcu(&counter->rcu_head, free_counter_rcu);
1421}
1422
Thomas Gleixner0793a612008-12-04 20:12:29 +01001423/*
1424 * Called when the last reference to the file is gone.
1425 */
1426static int perf_release(struct inode *inode, struct file *file)
1427{
1428 struct perf_counter *counter = file->private_data;
1429 struct perf_counter_context *ctx = counter->ctx;
1430
1431 file->private_data = NULL;
1432
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10001433 WARN_ON_ONCE(ctx->parent_ctx);
Paul Mackerrasd859e292009-01-17 18:10:22 +11001434 mutex_lock(&ctx->mutex);
Ingo Molnar04289bb2008-12-11 08:38:42 +01001435 perf_counter_remove_from_context(counter);
Paul Mackerrasd859e292009-01-17 18:10:22 +11001436 mutex_unlock(&ctx->mutex);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001437
Peter Zijlstra082ff5a2009-05-23 18:29:00 +02001438 mutex_lock(&counter->owner->perf_counter_mutex);
1439 list_del_init(&counter->owner_entry);
1440 mutex_unlock(&counter->owner->perf_counter_mutex);
1441 put_task_struct(counter->owner);
1442
Peter Zijlstraf1600952009-03-19 20:26:16 +01001443 free_counter(counter);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001444
1445 return 0;
1446}
1447
1448/*
1449 * Read the performance counter - simple non blocking version for now
1450 */
1451static ssize_t
1452perf_read_hw(struct perf_counter *counter, char __user *buf, size_t count)
1453{
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001454 u64 values[3];
1455 int n;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001456
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001457 /*
1458 * Return end-of-file for a read on a counter that is in
1459 * error state (i.e. because it was pinned but it couldn't be
1460 * scheduled on to the CPU at some point).
1461 */
1462 if (counter->state == PERF_COUNTER_STATE_ERROR)
1463 return 0;
1464
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10001465 WARN_ON_ONCE(counter->ctx->parent_ctx);
Peter Zijlstrafccc7142009-05-23 18:28:56 +02001466 mutex_lock(&counter->child_mutex);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001467 values[0] = perf_counter_read(counter);
1468 n = 1;
1469 if (counter->hw_event.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
1470 values[n++] = counter->total_time_enabled +
1471 atomic64_read(&counter->child_total_time_enabled);
1472 if (counter->hw_event.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
1473 values[n++] = counter->total_time_running +
1474 atomic64_read(&counter->child_total_time_running);
Peter Zijlstrafccc7142009-05-23 18:28:56 +02001475 mutex_unlock(&counter->child_mutex);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001476
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001477 if (count < n * sizeof(u64))
1478 return -EINVAL;
1479 count = n * sizeof(u64);
1480
1481 if (copy_to_user(buf, values, count))
1482 return -EFAULT;
1483
1484 return count;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001485}
1486
1487static ssize_t
Thomas Gleixner0793a612008-12-04 20:12:29 +01001488perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
1489{
1490 struct perf_counter *counter = file->private_data;
1491
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001492 return perf_read_hw(counter, buf, count);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001493}
1494
1495static unsigned int perf_poll(struct file *file, poll_table *wait)
1496{
1497 struct perf_counter *counter = file->private_data;
Peter Zijlstrac7138f32009-03-24 13:18:16 +01001498 struct perf_mmap_data *data;
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001499 unsigned int events = POLL_HUP;
Peter Zijlstrac7138f32009-03-24 13:18:16 +01001500
1501 rcu_read_lock();
1502 data = rcu_dereference(counter->data);
1503 if (data)
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001504 events = atomic_xchg(&data->poll, 0);
Peter Zijlstrac7138f32009-03-24 13:18:16 +01001505 rcu_read_unlock();
Thomas Gleixner0793a612008-12-04 20:12:29 +01001506
1507 poll_wait(file, &counter->waitq, wait);
1508
Thomas Gleixner0793a612008-12-04 20:12:29 +01001509 return events;
1510}
1511
Peter Zijlstra6de6a7b2009-05-05 17:50:23 +02001512static void perf_counter_reset(struct perf_counter *counter)
1513{
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001514 (void)perf_counter_read(counter);
Paul Mackerras615a3f12009-05-11 15:50:21 +10001515 atomic64_set(&counter->count, 0);
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001516 perf_counter_update_userpage(counter);
1517}
1518
1519static void perf_counter_for_each_sibling(struct perf_counter *counter,
1520 void (*func)(struct perf_counter *))
1521{
1522 struct perf_counter_context *ctx = counter->ctx;
1523 struct perf_counter *sibling;
1524
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10001525 WARN_ON_ONCE(ctx->parent_ctx);
Peter Zijlstra682076a2009-05-23 18:28:57 +02001526 mutex_lock(&ctx->mutex);
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001527 counter = counter->group_leader;
1528
1529 func(counter);
1530 list_for_each_entry(sibling, &counter->sibling_list, list_entry)
1531 func(sibling);
Peter Zijlstra682076a2009-05-23 18:28:57 +02001532 mutex_unlock(&ctx->mutex);
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001533}
1534
Paul Mackerrasc93f7662009-05-28 22:18:17 +10001535/*
1536 * Holding the top-level counter's child_mutex means that any
1537 * descendant process that has inherited this counter will block
1538 * in sync_child_counter if it goes to exit, thus satisfying the
1539 * task existence requirements of perf_counter_enable/disable.
1540 */
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001541static void perf_counter_for_each_child(struct perf_counter *counter,
1542 void (*func)(struct perf_counter *))
1543{
1544 struct perf_counter *child;
1545
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10001546 WARN_ON_ONCE(counter->ctx->parent_ctx);
Peter Zijlstrafccc7142009-05-23 18:28:56 +02001547 mutex_lock(&counter->child_mutex);
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001548 func(counter);
1549 list_for_each_entry(child, &counter->child_list, child_list)
1550 func(child);
Peter Zijlstrafccc7142009-05-23 18:28:56 +02001551 mutex_unlock(&counter->child_mutex);
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001552}
1553
1554static void perf_counter_for_each(struct perf_counter *counter,
1555 void (*func)(struct perf_counter *))
1556{
1557 struct perf_counter *child;
1558
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10001559 WARN_ON_ONCE(counter->ctx->parent_ctx);
Peter Zijlstrafccc7142009-05-23 18:28:56 +02001560 mutex_lock(&counter->child_mutex);
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001561 perf_counter_for_each_sibling(counter, func);
1562 list_for_each_entry(child, &counter->child_list, child_list)
1563 perf_counter_for_each_sibling(child, func);
Peter Zijlstrafccc7142009-05-23 18:28:56 +02001564 mutex_unlock(&counter->child_mutex);
Peter Zijlstra6de6a7b2009-05-05 17:50:23 +02001565}
1566
Paul Mackerrasd859e292009-01-17 18:10:22 +11001567static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1568{
1569 struct perf_counter *counter = file->private_data;
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001570 void (*func)(struct perf_counter *);
1571 u32 flags = arg;
Paul Mackerrasd859e292009-01-17 18:10:22 +11001572
1573 switch (cmd) {
1574 case PERF_COUNTER_IOC_ENABLE:
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001575 func = perf_counter_enable;
Paul Mackerrasd859e292009-01-17 18:10:22 +11001576 break;
1577 case PERF_COUNTER_IOC_DISABLE:
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001578 func = perf_counter_disable;
Peter Zijlstra79f14642009-04-06 11:45:07 +02001579 break;
Peter Zijlstra6de6a7b2009-05-05 17:50:23 +02001580 case PERF_COUNTER_IOC_RESET:
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001581 func = perf_counter_reset;
Peter Zijlstra6de6a7b2009-05-05 17:50:23 +02001582 break;
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001583
1584 case PERF_COUNTER_IOC_REFRESH:
1585 return perf_counter_refresh(counter, arg);
Paul Mackerrasd859e292009-01-17 18:10:22 +11001586 default:
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001587 return -ENOTTY;
Paul Mackerrasd859e292009-01-17 18:10:22 +11001588 }
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001589
1590 if (flags & PERF_IOC_FLAG_GROUP)
1591 perf_counter_for_each(counter, func);
1592 else
1593 perf_counter_for_each_child(counter, func);
1594
1595 return 0;
Paul Mackerrasd859e292009-01-17 18:10:22 +11001596}
1597
Peter Zijlstra771d7cd2009-05-25 14:45:26 +02001598int perf_counter_task_enable(void)
1599{
1600 struct perf_counter *counter;
1601
1602 mutex_lock(&current->perf_counter_mutex);
1603 list_for_each_entry(counter, &current->perf_counter_list, owner_entry)
1604 perf_counter_for_each_child(counter, perf_counter_enable);
1605 mutex_unlock(&current->perf_counter_mutex);
1606
1607 return 0;
1608}
1609
1610int perf_counter_task_disable(void)
1611{
1612 struct perf_counter *counter;
1613
1614 mutex_lock(&current->perf_counter_mutex);
1615 list_for_each_entry(counter, &current->perf_counter_list, owner_entry)
1616 perf_counter_for_each_child(counter, perf_counter_disable);
1617 mutex_unlock(&current->perf_counter_mutex);
1618
1619 return 0;
1620}
1621
Peter Zijlstra38ff6672009-03-30 19:07:03 +02001622/*
1623 * Callers need to ensure there can be no nesting of this function, otherwise
1624 * the seqlock logic goes bad. We can not serialize this because the arch
1625 * code calls this from NMI context.
1626 */
1627void perf_counter_update_userpage(struct perf_counter *counter)
Paul Mackerras37d81822009-03-23 18:22:08 +01001628{
Peter Zijlstra38ff6672009-03-30 19:07:03 +02001629 struct perf_mmap_data *data;
1630 struct perf_counter_mmap_page *userpg;
1631
1632 rcu_read_lock();
1633 data = rcu_dereference(counter->data);
1634 if (!data)
1635 goto unlock;
1636
1637 userpg = data->user_page;
Paul Mackerras37d81822009-03-23 18:22:08 +01001638
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001639 /*
1640 * Disable preemption so as to not let the corresponding user-space
1641 * spin too long if we get preempted.
1642 */
1643 preempt_disable();
Paul Mackerras37d81822009-03-23 18:22:08 +01001644 ++userpg->lock;
Peter Zijlstra92f22a32009-04-02 11:12:04 +02001645 barrier();
Paul Mackerras37d81822009-03-23 18:22:08 +01001646 userpg->index = counter->hw.idx;
1647 userpg->offset = atomic64_read(&counter->count);
1648 if (counter->state == PERF_COUNTER_STATE_ACTIVE)
1649 userpg->offset -= atomic64_read(&counter->hw.prev_count);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001650
Peter Zijlstra92f22a32009-04-02 11:12:04 +02001651 barrier();
Paul Mackerras37d81822009-03-23 18:22:08 +01001652 ++userpg->lock;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001653 preempt_enable();
Peter Zijlstra38ff6672009-03-30 19:07:03 +02001654unlock:
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001655 rcu_read_unlock();
Paul Mackerras37d81822009-03-23 18:22:08 +01001656}
1657
1658static int perf_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1659{
1660 struct perf_counter *counter = vma->vm_file->private_data;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001661 struct perf_mmap_data *data;
1662 int ret = VM_FAULT_SIGBUS;
Paul Mackerras37d81822009-03-23 18:22:08 +01001663
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001664 rcu_read_lock();
1665 data = rcu_dereference(counter->data);
1666 if (!data)
1667 goto unlock;
Paul Mackerras37d81822009-03-23 18:22:08 +01001668
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001669 if (vmf->pgoff == 0) {
1670 vmf->page = virt_to_page(data->user_page);
1671 } else {
1672 int nr = vmf->pgoff - 1;
1673
1674 if ((unsigned)nr > data->nr_pages)
1675 goto unlock;
1676
1677 vmf->page = virt_to_page(data->data_pages[nr]);
1678 }
Paul Mackerras37d81822009-03-23 18:22:08 +01001679 get_page(vmf->page);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001680 ret = 0;
1681unlock:
1682 rcu_read_unlock();
1683
1684 return ret;
1685}
1686
1687static int perf_mmap_data_alloc(struct perf_counter *counter, int nr_pages)
1688{
1689 struct perf_mmap_data *data;
1690 unsigned long size;
1691 int i;
1692
1693 WARN_ON(atomic_read(&counter->mmap_count));
1694
1695 size = sizeof(struct perf_mmap_data);
1696 size += nr_pages * sizeof(void *);
1697
1698 data = kzalloc(size, GFP_KERNEL);
1699 if (!data)
1700 goto fail;
1701
1702 data->user_page = (void *)get_zeroed_page(GFP_KERNEL);
1703 if (!data->user_page)
1704 goto fail_user_page;
1705
1706 for (i = 0; i < nr_pages; i++) {
1707 data->data_pages[i] = (void *)get_zeroed_page(GFP_KERNEL);
1708 if (!data->data_pages[i])
1709 goto fail_data_pages;
1710 }
1711
1712 data->nr_pages = nr_pages;
Peter Zijlstra22c15582009-05-05 17:50:25 +02001713 atomic_set(&data->lock, -1);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001714
1715 rcu_assign_pointer(counter->data, data);
1716
Paul Mackerras37d81822009-03-23 18:22:08 +01001717 return 0;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001718
1719fail_data_pages:
1720 for (i--; i >= 0; i--)
1721 free_page((unsigned long)data->data_pages[i]);
1722
1723 free_page((unsigned long)data->user_page);
1724
1725fail_user_page:
1726 kfree(data);
1727
1728fail:
1729 return -ENOMEM;
1730}
1731
1732static void __perf_mmap_data_free(struct rcu_head *rcu_head)
1733{
1734 struct perf_mmap_data *data = container_of(rcu_head,
1735 struct perf_mmap_data, rcu_head);
1736 int i;
1737
1738 free_page((unsigned long)data->user_page);
1739 for (i = 0; i < data->nr_pages; i++)
1740 free_page((unsigned long)data->data_pages[i]);
1741 kfree(data);
1742}
1743
1744static void perf_mmap_data_free(struct perf_counter *counter)
1745{
1746 struct perf_mmap_data *data = counter->data;
1747
1748 WARN_ON(atomic_read(&counter->mmap_count));
1749
1750 rcu_assign_pointer(counter->data, NULL);
1751 call_rcu(&data->rcu_head, __perf_mmap_data_free);
1752}
1753
1754static void perf_mmap_open(struct vm_area_struct *vma)
1755{
1756 struct perf_counter *counter = vma->vm_file->private_data;
1757
1758 atomic_inc(&counter->mmap_count);
1759}
1760
1761static void perf_mmap_close(struct vm_area_struct *vma)
1762{
1763 struct perf_counter *counter = vma->vm_file->private_data;
1764
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10001765 WARN_ON_ONCE(counter->ctx->parent_ctx);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001766 if (atomic_dec_and_mutex_lock(&counter->mmap_count,
1767 &counter->mmap_mutex)) {
Peter Zijlstra789f90f2009-05-15 15:19:27 +02001768 struct user_struct *user = current_user();
1769
1770 atomic_long_sub(counter->data->nr_pages + 1, &user->locked_vm);
Peter Zijlstrac5078f72009-05-05 17:50:24 +02001771 vma->vm_mm->locked_vm -= counter->data->nr_locked;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001772 perf_mmap_data_free(counter);
1773 mutex_unlock(&counter->mmap_mutex);
1774 }
Paul Mackerras37d81822009-03-23 18:22:08 +01001775}
1776
1777static struct vm_operations_struct perf_mmap_vmops = {
Peter Zijlstraebb3c4c2009-04-06 11:45:05 +02001778 .open = perf_mmap_open,
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001779 .close = perf_mmap_close,
Paul Mackerras37d81822009-03-23 18:22:08 +01001780 .fault = perf_mmap_fault,
1781};
1782
1783static int perf_mmap(struct file *file, struct vm_area_struct *vma)
1784{
1785 struct perf_counter *counter = file->private_data;
Peter Zijlstra789f90f2009-05-15 15:19:27 +02001786 struct user_struct *user = current_user();
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001787 unsigned long vma_size;
1788 unsigned long nr_pages;
Peter Zijlstra789f90f2009-05-15 15:19:27 +02001789 unsigned long user_locked, user_lock_limit;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001790 unsigned long locked, lock_limit;
Peter Zijlstra789f90f2009-05-15 15:19:27 +02001791 long user_extra, extra;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001792 int ret = 0;
Paul Mackerras37d81822009-03-23 18:22:08 +01001793
1794 if (!(vma->vm_flags & VM_SHARED) || (vma->vm_flags & VM_WRITE))
1795 return -EINVAL;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001796
1797 vma_size = vma->vm_end - vma->vm_start;
1798 nr_pages = (vma_size / PAGE_SIZE) - 1;
1799
Peter Zijlstra7730d862009-03-25 12:48:31 +01001800 /*
1801 * If we have data pages ensure they're a power-of-two number, so we
1802 * can do bitmasks instead of modulo.
1803 */
1804 if (nr_pages != 0 && !is_power_of_2(nr_pages))
Paul Mackerras37d81822009-03-23 18:22:08 +01001805 return -EINVAL;
1806
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001807 if (vma_size != PAGE_SIZE * (1 + nr_pages))
Paul Mackerras37d81822009-03-23 18:22:08 +01001808 return -EINVAL;
1809
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001810 if (vma->vm_pgoff != 0)
1811 return -EINVAL;
Paul Mackerras37d81822009-03-23 18:22:08 +01001812
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10001813 WARN_ON_ONCE(counter->ctx->parent_ctx);
Peter Zijlstraebb3c4c2009-04-06 11:45:05 +02001814 mutex_lock(&counter->mmap_mutex);
1815 if (atomic_inc_not_zero(&counter->mmap_count)) {
1816 if (nr_pages != counter->data->nr_pages)
1817 ret = -EINVAL;
1818 goto unlock;
1819 }
1820
Peter Zijlstra789f90f2009-05-15 15:19:27 +02001821 user_extra = nr_pages + 1;
1822 user_lock_limit = sysctl_perf_counter_mlock >> (PAGE_SHIFT - 10);
Ingo Molnara3862d32009-05-24 09:02:37 +02001823
1824 /*
1825 * Increase the limit linearly with more CPUs:
1826 */
1827 user_lock_limit *= num_online_cpus();
1828
Peter Zijlstra789f90f2009-05-15 15:19:27 +02001829 user_locked = atomic_long_read(&user->locked_vm) + user_extra;
Peter Zijlstrac5078f72009-05-05 17:50:24 +02001830
Peter Zijlstra789f90f2009-05-15 15:19:27 +02001831 extra = 0;
1832 if (user_locked > user_lock_limit)
1833 extra = user_locked - user_lock_limit;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001834
1835 lock_limit = current->signal->rlim[RLIMIT_MEMLOCK].rlim_cur;
1836 lock_limit >>= PAGE_SHIFT;
Peter Zijlstra789f90f2009-05-15 15:19:27 +02001837 locked = vma->vm_mm->locked_vm + extra;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001838
Peter Zijlstraebb3c4c2009-04-06 11:45:05 +02001839 if ((locked > lock_limit) && !capable(CAP_IPC_LOCK)) {
1840 ret = -EPERM;
1841 goto unlock;
1842 }
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001843
1844 WARN_ON(counter->data);
1845 ret = perf_mmap_data_alloc(counter, nr_pages);
Peter Zijlstraebb3c4c2009-04-06 11:45:05 +02001846 if (ret)
1847 goto unlock;
1848
1849 atomic_set(&counter->mmap_count, 1);
Peter Zijlstra789f90f2009-05-15 15:19:27 +02001850 atomic_long_add(user_extra, &user->locked_vm);
Peter Zijlstrac5078f72009-05-05 17:50:24 +02001851 vma->vm_mm->locked_vm += extra;
1852 counter->data->nr_locked = extra;
Peter Zijlstraebb3c4c2009-04-06 11:45:05 +02001853unlock:
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001854 mutex_unlock(&counter->mmap_mutex);
Paul Mackerras37d81822009-03-23 18:22:08 +01001855
1856 vma->vm_flags &= ~VM_MAYWRITE;
1857 vma->vm_flags |= VM_RESERVED;
1858 vma->vm_ops = &perf_mmap_vmops;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001859
1860 return ret;
Paul Mackerras37d81822009-03-23 18:22:08 +01001861}
1862
Peter Zijlstra3c446b32009-04-06 11:45:01 +02001863static int perf_fasync(int fd, struct file *filp, int on)
1864{
1865 struct perf_counter *counter = filp->private_data;
1866 struct inode *inode = filp->f_path.dentry->d_inode;
1867 int retval;
1868
1869 mutex_lock(&inode->i_mutex);
1870 retval = fasync_helper(fd, filp, on, &counter->fasync);
1871 mutex_unlock(&inode->i_mutex);
1872
1873 if (retval < 0)
1874 return retval;
1875
1876 return 0;
1877}
1878
Thomas Gleixner0793a612008-12-04 20:12:29 +01001879static const struct file_operations perf_fops = {
1880 .release = perf_release,
1881 .read = perf_read,
1882 .poll = perf_poll,
Paul Mackerrasd859e292009-01-17 18:10:22 +11001883 .unlocked_ioctl = perf_ioctl,
1884 .compat_ioctl = perf_ioctl,
Paul Mackerras37d81822009-03-23 18:22:08 +01001885 .mmap = perf_mmap,
Peter Zijlstra3c446b32009-04-06 11:45:01 +02001886 .fasync = perf_fasync,
Thomas Gleixner0793a612008-12-04 20:12:29 +01001887};
1888
Peter Zijlstra15dbf272009-03-13 12:21:32 +01001889/*
Peter Zijlstra925d5192009-03-30 19:07:02 +02001890 * Perf counter wakeup
1891 *
1892 * If there's data, ensure we set the poll() state and publish everything
1893 * to user-space before waking everybody up.
1894 */
1895
1896void perf_counter_wakeup(struct perf_counter *counter)
1897{
Peter Zijlstra925d5192009-03-30 19:07:02 +02001898 wake_up_all(&counter->waitq);
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02001899
1900 if (counter->pending_kill) {
1901 kill_fasync(&counter->fasync, SIGIO, counter->pending_kill);
1902 counter->pending_kill = 0;
1903 }
Peter Zijlstra925d5192009-03-30 19:07:02 +02001904}
1905
1906/*
1907 * Pending wakeups
1908 *
1909 * Handle the case where we need to wakeup up from NMI (or rq->lock) context.
1910 *
1911 * The NMI bit means we cannot possibly take locks. Therefore, maintain a
1912 * single linked list and use cmpxchg() to add entries lockless.
1913 */
1914
Peter Zijlstra79f14642009-04-06 11:45:07 +02001915static void perf_pending_counter(struct perf_pending_entry *entry)
1916{
1917 struct perf_counter *counter = container_of(entry,
1918 struct perf_counter, pending);
1919
1920 if (counter->pending_disable) {
1921 counter->pending_disable = 0;
1922 perf_counter_disable(counter);
1923 }
1924
1925 if (counter->pending_wakeup) {
1926 counter->pending_wakeup = 0;
1927 perf_counter_wakeup(counter);
1928 }
1929}
1930
Peter Zijlstra671dec52009-04-06 11:45:02 +02001931#define PENDING_TAIL ((struct perf_pending_entry *)-1UL)
Peter Zijlstra925d5192009-03-30 19:07:02 +02001932
Peter Zijlstra671dec52009-04-06 11:45:02 +02001933static DEFINE_PER_CPU(struct perf_pending_entry *, perf_pending_head) = {
Peter Zijlstra925d5192009-03-30 19:07:02 +02001934 PENDING_TAIL,
1935};
1936
Peter Zijlstra671dec52009-04-06 11:45:02 +02001937static void perf_pending_queue(struct perf_pending_entry *entry,
1938 void (*func)(struct perf_pending_entry *))
Peter Zijlstra925d5192009-03-30 19:07:02 +02001939{
Peter Zijlstra671dec52009-04-06 11:45:02 +02001940 struct perf_pending_entry **head;
Peter Zijlstra925d5192009-03-30 19:07:02 +02001941
Peter Zijlstra671dec52009-04-06 11:45:02 +02001942 if (cmpxchg(&entry->next, NULL, PENDING_TAIL) != NULL)
Peter Zijlstra925d5192009-03-30 19:07:02 +02001943 return;
1944
Peter Zijlstra671dec52009-04-06 11:45:02 +02001945 entry->func = func;
1946
1947 head = &get_cpu_var(perf_pending_head);
Peter Zijlstra925d5192009-03-30 19:07:02 +02001948
1949 do {
Peter Zijlstra671dec52009-04-06 11:45:02 +02001950 entry->next = *head;
1951 } while (cmpxchg(head, entry->next, entry) != entry->next);
Peter Zijlstra925d5192009-03-30 19:07:02 +02001952
1953 set_perf_counter_pending();
1954
Peter Zijlstra671dec52009-04-06 11:45:02 +02001955 put_cpu_var(perf_pending_head);
Peter Zijlstra925d5192009-03-30 19:07:02 +02001956}
1957
1958static int __perf_pending_run(void)
1959{
Peter Zijlstra671dec52009-04-06 11:45:02 +02001960 struct perf_pending_entry *list;
Peter Zijlstra925d5192009-03-30 19:07:02 +02001961 int nr = 0;
1962
Peter Zijlstra671dec52009-04-06 11:45:02 +02001963 list = xchg(&__get_cpu_var(perf_pending_head), PENDING_TAIL);
Peter Zijlstra925d5192009-03-30 19:07:02 +02001964 while (list != PENDING_TAIL) {
Peter Zijlstra671dec52009-04-06 11:45:02 +02001965 void (*func)(struct perf_pending_entry *);
1966 struct perf_pending_entry *entry = list;
Peter Zijlstra925d5192009-03-30 19:07:02 +02001967
1968 list = list->next;
1969
Peter Zijlstra671dec52009-04-06 11:45:02 +02001970 func = entry->func;
1971 entry->next = NULL;
Peter Zijlstra925d5192009-03-30 19:07:02 +02001972 /*
1973 * Ensure we observe the unqueue before we issue the wakeup,
1974 * so that we won't be waiting forever.
1975 * -- see perf_not_pending().
1976 */
1977 smp_wmb();
1978
Peter Zijlstra671dec52009-04-06 11:45:02 +02001979 func(entry);
Peter Zijlstra925d5192009-03-30 19:07:02 +02001980 nr++;
1981 }
1982
1983 return nr;
1984}
1985
1986static inline int perf_not_pending(struct perf_counter *counter)
1987{
1988 /*
1989 * If we flush on whatever cpu we run, there is a chance we don't
1990 * need to wait.
1991 */
1992 get_cpu();
1993 __perf_pending_run();
1994 put_cpu();
1995
1996 /*
1997 * Ensure we see the proper queue state before going to sleep
1998 * so that we do not miss the wakeup. -- see perf_pending_handle()
1999 */
2000 smp_rmb();
Peter Zijlstra671dec52009-04-06 11:45:02 +02002001 return counter->pending.next == NULL;
Peter Zijlstra925d5192009-03-30 19:07:02 +02002002}
2003
2004static void perf_pending_sync(struct perf_counter *counter)
2005{
2006 wait_event(counter->waitq, perf_not_pending(counter));
2007}
2008
2009void perf_counter_do_pending(void)
2010{
2011 __perf_pending_run();
2012}
2013
2014/*
Peter Zijlstra394ee072009-03-30 19:07:14 +02002015 * Callchain support -- arch specific
2016 */
2017
Peter Zijlstra9c03d882009-04-06 11:45:00 +02002018__weak struct perf_callchain_entry *perf_callchain(struct pt_regs *regs)
Peter Zijlstra394ee072009-03-30 19:07:14 +02002019{
2020 return NULL;
2021}
2022
2023/*
Peter Zijlstra0322cd62009-03-19 20:26:19 +01002024 * Output
2025 */
2026
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002027struct perf_output_handle {
2028 struct perf_counter *counter;
2029 struct perf_mmap_data *data;
2030 unsigned int offset;
Peter Zijlstra63e35b22009-03-25 12:30:24 +01002031 unsigned int head;
Peter Zijlstra78d613e2009-03-30 19:07:11 +02002032 int nmi;
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02002033 int overflow;
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002034 int locked;
2035 unsigned long flags;
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002036};
2037
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002038static void perf_output_wakeup(struct perf_output_handle *handle)
Peter Zijlstra78d613e2009-03-30 19:07:11 +02002039{
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002040 atomic_set(&handle->data->poll, POLL_IN);
2041
Peter Zijlstra671dec52009-04-06 11:45:02 +02002042 if (handle->nmi) {
Peter Zijlstra79f14642009-04-06 11:45:07 +02002043 handle->counter->pending_wakeup = 1;
Peter Zijlstra671dec52009-04-06 11:45:02 +02002044 perf_pending_queue(&handle->counter->pending,
Peter Zijlstra79f14642009-04-06 11:45:07 +02002045 perf_pending_counter);
Peter Zijlstra671dec52009-04-06 11:45:02 +02002046 } else
Peter Zijlstra78d613e2009-03-30 19:07:11 +02002047 perf_counter_wakeup(handle->counter);
2048}
2049
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002050/*
2051 * Curious locking construct.
2052 *
2053 * We need to ensure a later event doesn't publish a head when a former
2054 * event isn't done writing. However since we need to deal with NMIs we
2055 * cannot fully serialize things.
2056 *
2057 * What we do is serialize between CPUs so we only have to deal with NMI
2058 * nesting on a single CPU.
2059 *
2060 * We only publish the head (and generate a wakeup) when the outer-most
2061 * event completes.
2062 */
2063static void perf_output_lock(struct perf_output_handle *handle)
2064{
2065 struct perf_mmap_data *data = handle->data;
2066 int cpu;
2067
2068 handle->locked = 0;
2069
2070 local_irq_save(handle->flags);
2071 cpu = smp_processor_id();
2072
2073 if (in_nmi() && atomic_read(&data->lock) == cpu)
2074 return;
2075
Peter Zijlstra22c15582009-05-05 17:50:25 +02002076 while (atomic_cmpxchg(&data->lock, -1, cpu) != -1)
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002077 cpu_relax();
2078
2079 handle->locked = 1;
2080}
2081
2082static void perf_output_unlock(struct perf_output_handle *handle)
2083{
2084 struct perf_mmap_data *data = handle->data;
2085 int head, cpu;
2086
Peter Zijlstrac66de4a2009-05-05 17:50:22 +02002087 data->done_head = data->head;
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002088
2089 if (!handle->locked)
2090 goto out;
2091
2092again:
2093 /*
2094 * The xchg implies a full barrier that ensures all writes are done
2095 * before we publish the new head, matched by a rmb() in userspace when
2096 * reading this position.
2097 */
Peter Zijlstrac66de4a2009-05-05 17:50:22 +02002098 while ((head = atomic_xchg(&data->done_head, 0)))
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002099 data->user_page->data_head = head;
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002100
2101 /*
Peter Zijlstrac66de4a2009-05-05 17:50:22 +02002102 * NMI can happen here, which means we can miss a done_head update.
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002103 */
2104
Peter Zijlstra22c15582009-05-05 17:50:25 +02002105 cpu = atomic_xchg(&data->lock, -1);
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002106 WARN_ON_ONCE(cpu != smp_processor_id());
2107
2108 /*
2109 * Therefore we have to validate we did not indeed do so.
2110 */
Peter Zijlstrac66de4a2009-05-05 17:50:22 +02002111 if (unlikely(atomic_read(&data->done_head))) {
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002112 /*
2113 * Since we had it locked, we can lock it again.
2114 */
Peter Zijlstra22c15582009-05-05 17:50:25 +02002115 while (atomic_cmpxchg(&data->lock, -1, cpu) != -1)
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002116 cpu_relax();
2117
2118 goto again;
2119 }
2120
Peter Zijlstrac66de4a2009-05-05 17:50:22 +02002121 if (atomic_xchg(&data->wakeup, 0))
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002122 perf_output_wakeup(handle);
2123out:
2124 local_irq_restore(handle->flags);
2125}
2126
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002127static int perf_output_begin(struct perf_output_handle *handle,
Peter Zijlstra78d613e2009-03-30 19:07:11 +02002128 struct perf_counter *counter, unsigned int size,
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02002129 int nmi, int overflow)
Peter Zijlstra0322cd62009-03-19 20:26:19 +01002130{
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002131 struct perf_mmap_data *data;
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002132 unsigned int offset, head;
Peter Zijlstra0322cd62009-03-19 20:26:19 +01002133
Peter Zijlstra2023b352009-05-05 17:50:26 +02002134 /*
2135 * For inherited counters we send all the output towards the parent.
2136 */
2137 if (counter->parent)
2138 counter = counter->parent;
2139
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002140 rcu_read_lock();
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002141 data = rcu_dereference(counter->data);
2142 if (!data)
2143 goto out;
Peter Zijlstra0322cd62009-03-19 20:26:19 +01002144
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002145 handle->data = data;
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02002146 handle->counter = counter;
2147 handle->nmi = nmi;
2148 handle->overflow = overflow;
Peter Zijlstra78d613e2009-03-30 19:07:11 +02002149
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002150 if (!data->nr_pages)
Peter Zijlstra78d613e2009-03-30 19:07:11 +02002151 goto fail;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002152
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002153 perf_output_lock(handle);
2154
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002155 do {
2156 offset = head = atomic_read(&data->head);
Peter Zijlstrac7138f32009-03-24 13:18:16 +01002157 head += size;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002158 } while (atomic_cmpxchg(&data->head, offset, head) != offset);
2159
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002160 handle->offset = offset;
Peter Zijlstra63e35b22009-03-25 12:30:24 +01002161 handle->head = head;
Peter Zijlstrac66de4a2009-05-05 17:50:22 +02002162
2163 if ((offset >> PAGE_SHIFT) != (head >> PAGE_SHIFT))
2164 atomic_set(&data->wakeup, 1);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002165
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002166 return 0;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002167
Peter Zijlstra78d613e2009-03-30 19:07:11 +02002168fail:
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002169 perf_output_wakeup(handle);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002170out:
2171 rcu_read_unlock();
2172
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002173 return -ENOSPC;
2174}
2175
2176static void perf_output_copy(struct perf_output_handle *handle,
2177 void *buf, unsigned int len)
2178{
2179 unsigned int pages_mask;
2180 unsigned int offset;
2181 unsigned int size;
2182 void **pages;
2183
2184 offset = handle->offset;
2185 pages_mask = handle->data->nr_pages - 1;
2186 pages = handle->data->data_pages;
2187
2188 do {
2189 unsigned int page_offset;
2190 int nr;
2191
2192 nr = (offset >> PAGE_SHIFT) & pages_mask;
2193 page_offset = offset & (PAGE_SIZE - 1);
2194 size = min_t(unsigned int, PAGE_SIZE - page_offset, len);
2195
2196 memcpy(pages[nr] + page_offset, buf, size);
2197
2198 len -= size;
2199 buf += size;
2200 offset += size;
2201 } while (len);
2202
2203 handle->offset = offset;
Peter Zijlstra63e35b22009-03-25 12:30:24 +01002204
Peter Zijlstra53020fe2009-05-13 21:26:19 +02002205 /*
2206 * Check we didn't copy past our reservation window, taking the
2207 * possible unsigned int wrap into account.
2208 */
2209 WARN_ON_ONCE(((int)(handle->head - handle->offset)) < 0);
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002210}
2211
Peter Zijlstra5c148192009-03-25 12:30:23 +01002212#define perf_output_put(handle, x) \
2213 perf_output_copy((handle), &(x), sizeof(x))
2214
Peter Zijlstra78d613e2009-03-30 19:07:11 +02002215static void perf_output_end(struct perf_output_handle *handle)
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002216{
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002217 struct perf_counter *counter = handle->counter;
2218 struct perf_mmap_data *data = handle->data;
2219
2220 int wakeup_events = counter->hw_event.wakeup_events;
Peter Zijlstrac4578102009-04-02 11:12:01 +02002221
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02002222 if (handle->overflow && wakeup_events) {
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002223 int events = atomic_inc_return(&data->events);
Peter Zijlstrac4578102009-04-02 11:12:01 +02002224 if (events >= wakeup_events) {
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002225 atomic_sub(wakeup_events, &data->events);
Peter Zijlstrac66de4a2009-05-05 17:50:22 +02002226 atomic_set(&data->wakeup, 1);
Peter Zijlstrac4578102009-04-02 11:12:01 +02002227 }
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002228 }
2229
2230 perf_output_unlock(handle);
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002231 rcu_read_unlock();
2232}
2233
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002234static void perf_counter_output(struct perf_counter *counter,
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002235 int nmi, struct pt_regs *regs, u64 addr)
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002236{
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002237 int ret;
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002238 u64 record_type = counter->hw_event.record_type;
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002239 struct perf_output_handle handle;
2240 struct perf_event_header header;
2241 u64 ip;
Peter Zijlstra5c148192009-03-25 12:30:23 +01002242 struct {
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01002243 u32 pid, tid;
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002244 } tid_entry;
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002245 struct {
2246 u64 event;
2247 u64 counter;
2248 } group_entry;
Peter Zijlstra394ee072009-03-30 19:07:14 +02002249 struct perf_callchain_entry *callchain = NULL;
2250 int callchain_size = 0;
Peter Zijlstra339f7c92009-04-06 11:45:06 +02002251 u64 time;
Peter Zijlstraf370e1e2009-05-08 18:52:24 +02002252 struct {
2253 u32 cpu, reserved;
2254 } cpu_entry;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002255
Peter Zijlstra6b6e5482009-04-08 15:01:27 +02002256 header.type = 0;
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002257 header.size = sizeof(header);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002258
Peter Zijlstra6b6e5482009-04-08 15:01:27 +02002259 header.misc = PERF_EVENT_MISC_OVERFLOW;
Paul Mackerras9d23a902009-05-14 21:48:08 +10002260 header.misc |= perf_misc_flags(regs);
Peter Zijlstra6fab0192009-04-08 15:01:26 +02002261
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002262 if (record_type & PERF_RECORD_IP) {
Paul Mackerras9d23a902009-05-14 21:48:08 +10002263 ip = perf_instruction_pointer(regs);
Peter Zijlstra6b6e5482009-04-08 15:01:27 +02002264 header.type |= PERF_RECORD_IP;
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002265 header.size += sizeof(ip);
2266 }
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01002267
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002268 if (record_type & PERF_RECORD_TID) {
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01002269 /* namespace issues */
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002270 tid_entry.pid = current->group_leader->pid;
2271 tid_entry.tid = current->pid;
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01002272
Peter Zijlstra6b6e5482009-04-08 15:01:27 +02002273 header.type |= PERF_RECORD_TID;
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002274 header.size += sizeof(tid_entry);
2275 }
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01002276
Peter Zijlstra4d855452009-04-08 15:01:32 +02002277 if (record_type & PERF_RECORD_TIME) {
2278 /*
2279 * Maybe do better on x86 and provide cpu_clock_nmi()
2280 */
2281 time = sched_clock();
2282
2283 header.type |= PERF_RECORD_TIME;
2284 header.size += sizeof(u64);
2285 }
2286
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002287 if (record_type & PERF_RECORD_ADDR) {
2288 header.type |= PERF_RECORD_ADDR;
2289 header.size += sizeof(u64);
2290 }
2291
Peter Zijlstraa85f61a2009-05-08 18:52:23 +02002292 if (record_type & PERF_RECORD_CONFIG) {
2293 header.type |= PERF_RECORD_CONFIG;
2294 header.size += sizeof(u64);
2295 }
2296
Peter Zijlstraf370e1e2009-05-08 18:52:24 +02002297 if (record_type & PERF_RECORD_CPU) {
2298 header.type |= PERF_RECORD_CPU;
2299 header.size += sizeof(cpu_entry);
2300
2301 cpu_entry.cpu = raw_smp_processor_id();
2302 }
2303
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002304 if (record_type & PERF_RECORD_GROUP) {
Peter Zijlstra6b6e5482009-04-08 15:01:27 +02002305 header.type |= PERF_RECORD_GROUP;
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002306 header.size += sizeof(u64) +
2307 counter->nr_siblings * sizeof(group_entry);
2308 }
2309
2310 if (record_type & PERF_RECORD_CALLCHAIN) {
Peter Zijlstra394ee072009-03-30 19:07:14 +02002311 callchain = perf_callchain(regs);
2312
2313 if (callchain) {
Peter Zijlstra9c03d882009-04-06 11:45:00 +02002314 callchain_size = (1 + callchain->nr) * sizeof(u64);
Peter Zijlstra394ee072009-03-30 19:07:14 +02002315
Peter Zijlstra6b6e5482009-04-08 15:01:27 +02002316 header.type |= PERF_RECORD_CALLCHAIN;
Peter Zijlstra394ee072009-03-30 19:07:14 +02002317 header.size += callchain_size;
2318 }
2319 }
2320
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02002321 ret = perf_output_begin(&handle, counter, header.size, nmi, 1);
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002322 if (ret)
2323 return;
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01002324
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002325 perf_output_put(&handle, header);
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002326
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002327 if (record_type & PERF_RECORD_IP)
2328 perf_output_put(&handle, ip);
2329
2330 if (record_type & PERF_RECORD_TID)
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002331 perf_output_put(&handle, tid_entry);
2332
Peter Zijlstra4d855452009-04-08 15:01:32 +02002333 if (record_type & PERF_RECORD_TIME)
2334 perf_output_put(&handle, time);
2335
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002336 if (record_type & PERF_RECORD_ADDR)
2337 perf_output_put(&handle, addr);
2338
Peter Zijlstraa85f61a2009-05-08 18:52:23 +02002339 if (record_type & PERF_RECORD_CONFIG)
2340 perf_output_put(&handle, counter->hw_event.config);
2341
Peter Zijlstraf370e1e2009-05-08 18:52:24 +02002342 if (record_type & PERF_RECORD_CPU)
2343 perf_output_put(&handle, cpu_entry);
2344
Peter Zijlstra2023b352009-05-05 17:50:26 +02002345 /*
2346 * XXX PERF_RECORD_GROUP vs inherited counters seems difficult.
2347 */
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002348 if (record_type & PERF_RECORD_GROUP) {
2349 struct perf_counter *leader, *sub;
2350 u64 nr = counter->nr_siblings;
2351
2352 perf_output_put(&handle, nr);
2353
2354 leader = counter->group_leader;
2355 list_for_each_entry(sub, &leader->sibling_list, list_entry) {
2356 if (sub != counter)
Robert Richter4aeb0b42009-04-29 12:47:03 +02002357 sub->pmu->read(sub);
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002358
2359 group_entry.event = sub->hw_event.config;
2360 group_entry.counter = atomic64_read(&sub->count);
2361
2362 perf_output_put(&handle, group_entry);
2363 }
2364 }
2365
Peter Zijlstra394ee072009-03-30 19:07:14 +02002366 if (callchain)
2367 perf_output_copy(&handle, callchain, callchain_size);
2368
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002369 perf_output_end(&handle);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002370}
2371
Peter Zijlstra0322cd62009-03-19 20:26:19 +01002372/*
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02002373 * comm tracking
2374 */
2375
2376struct perf_comm_event {
2377 struct task_struct *task;
2378 char *comm;
2379 int comm_size;
2380
2381 struct {
2382 struct perf_event_header header;
2383
2384 u32 pid;
2385 u32 tid;
2386 } event;
2387};
2388
2389static void perf_counter_comm_output(struct perf_counter *counter,
2390 struct perf_comm_event *comm_event)
2391{
2392 struct perf_output_handle handle;
2393 int size = comm_event->event.header.size;
2394 int ret = perf_output_begin(&handle, counter, size, 0, 0);
2395
2396 if (ret)
2397 return;
2398
2399 perf_output_put(&handle, comm_event->event);
2400 perf_output_copy(&handle, comm_event->comm,
2401 comm_event->comm_size);
2402 perf_output_end(&handle);
2403}
2404
2405static int perf_counter_comm_match(struct perf_counter *counter,
2406 struct perf_comm_event *comm_event)
2407{
2408 if (counter->hw_event.comm &&
2409 comm_event->event.header.type == PERF_EVENT_COMM)
2410 return 1;
2411
2412 return 0;
2413}
2414
2415static void perf_counter_comm_ctx(struct perf_counter_context *ctx,
2416 struct perf_comm_event *comm_event)
2417{
2418 struct perf_counter *counter;
2419
2420 if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
2421 return;
2422
2423 rcu_read_lock();
2424 list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
2425 if (perf_counter_comm_match(counter, comm_event))
2426 perf_counter_comm_output(counter, comm_event);
2427 }
2428 rcu_read_unlock();
2429}
2430
2431static void perf_counter_comm_event(struct perf_comm_event *comm_event)
2432{
2433 struct perf_cpu_context *cpuctx;
2434 unsigned int size;
2435 char *comm = comm_event->task->comm;
2436
Ingo Molnar888fcee2009-04-09 09:48:22 +02002437 size = ALIGN(strlen(comm)+1, sizeof(u64));
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02002438
2439 comm_event->comm = comm;
2440 comm_event->comm_size = size;
2441
2442 comm_event->event.header.size = sizeof(comm_event->event) + size;
2443
2444 cpuctx = &get_cpu_var(perf_cpu_context);
2445 perf_counter_comm_ctx(&cpuctx->ctx, comm_event);
2446 put_cpu_var(perf_cpu_context);
2447
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10002448 perf_counter_comm_ctx(current->perf_counter_ctxp, comm_event);
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02002449}
2450
2451void perf_counter_comm(struct task_struct *task)
2452{
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02002453 struct perf_comm_event comm_event;
2454
2455 if (!atomic_read(&nr_comm_tracking))
2456 return;
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10002457 if (!current->perf_counter_ctxp)
2458 return;
2459
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02002460 comm_event = (struct perf_comm_event){
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02002461 .task = task,
2462 .event = {
2463 .header = { .type = PERF_EVENT_COMM, },
2464 .pid = task->group_leader->pid,
2465 .tid = task->pid,
2466 },
2467 };
2468
2469 perf_counter_comm_event(&comm_event);
2470}
2471
2472/*
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002473 * mmap tracking
2474 */
2475
2476struct perf_mmap_event {
2477 struct file *file;
2478 char *file_name;
2479 int file_size;
2480
2481 struct {
2482 struct perf_event_header header;
2483
2484 u32 pid;
2485 u32 tid;
2486 u64 start;
2487 u64 len;
2488 u64 pgoff;
2489 } event;
2490};
2491
2492static void perf_counter_mmap_output(struct perf_counter *counter,
2493 struct perf_mmap_event *mmap_event)
2494{
2495 struct perf_output_handle handle;
2496 int size = mmap_event->event.header.size;
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02002497 int ret = perf_output_begin(&handle, counter, size, 0, 0);
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002498
2499 if (ret)
2500 return;
2501
2502 perf_output_put(&handle, mmap_event->event);
2503 perf_output_copy(&handle, mmap_event->file_name,
2504 mmap_event->file_size);
Peter Zijlstra78d613e2009-03-30 19:07:11 +02002505 perf_output_end(&handle);
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002506}
2507
2508static int perf_counter_mmap_match(struct perf_counter *counter,
2509 struct perf_mmap_event *mmap_event)
2510{
2511 if (counter->hw_event.mmap &&
2512 mmap_event->event.header.type == PERF_EVENT_MMAP)
2513 return 1;
2514
2515 if (counter->hw_event.munmap &&
2516 mmap_event->event.header.type == PERF_EVENT_MUNMAP)
2517 return 1;
2518
2519 return 0;
2520}
2521
2522static void perf_counter_mmap_ctx(struct perf_counter_context *ctx,
2523 struct perf_mmap_event *mmap_event)
2524{
2525 struct perf_counter *counter;
2526
2527 if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
2528 return;
2529
2530 rcu_read_lock();
2531 list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
2532 if (perf_counter_mmap_match(counter, mmap_event))
2533 perf_counter_mmap_output(counter, mmap_event);
2534 }
2535 rcu_read_unlock();
2536}
2537
2538static void perf_counter_mmap_event(struct perf_mmap_event *mmap_event)
2539{
2540 struct perf_cpu_context *cpuctx;
2541 struct file *file = mmap_event->file;
2542 unsigned int size;
2543 char tmp[16];
2544 char *buf = NULL;
2545 char *name;
2546
2547 if (file) {
2548 buf = kzalloc(PATH_MAX, GFP_KERNEL);
2549 if (!buf) {
2550 name = strncpy(tmp, "//enomem", sizeof(tmp));
2551 goto got_name;
2552 }
Peter Zijlstrad3d21c42009-04-09 10:53:46 +02002553 name = d_path(&file->f_path, buf, PATH_MAX);
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002554 if (IS_ERR(name)) {
2555 name = strncpy(tmp, "//toolong", sizeof(tmp));
2556 goto got_name;
2557 }
2558 } else {
2559 name = strncpy(tmp, "//anon", sizeof(tmp));
2560 goto got_name;
2561 }
2562
2563got_name:
Ingo Molnar888fcee2009-04-09 09:48:22 +02002564 size = ALIGN(strlen(name)+1, sizeof(u64));
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002565
2566 mmap_event->file_name = name;
2567 mmap_event->file_size = size;
2568
2569 mmap_event->event.header.size = sizeof(mmap_event->event) + size;
2570
2571 cpuctx = &get_cpu_var(perf_cpu_context);
2572 perf_counter_mmap_ctx(&cpuctx->ctx, mmap_event);
2573 put_cpu_var(perf_cpu_context);
2574
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10002575 perf_counter_mmap_ctx(current->perf_counter_ctxp, mmap_event);
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002576
2577 kfree(buf);
2578}
2579
2580void perf_counter_mmap(unsigned long addr, unsigned long len,
2581 unsigned long pgoff, struct file *file)
2582{
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02002583 struct perf_mmap_event mmap_event;
2584
2585 if (!atomic_read(&nr_mmap_tracking))
2586 return;
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10002587 if (!current->perf_counter_ctxp)
2588 return;
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02002589
2590 mmap_event = (struct perf_mmap_event){
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002591 .file = file,
2592 .event = {
2593 .header = { .type = PERF_EVENT_MMAP, },
2594 .pid = current->group_leader->pid,
2595 .tid = current->pid,
2596 .start = addr,
2597 .len = len,
2598 .pgoff = pgoff,
2599 },
2600 };
2601
2602 perf_counter_mmap_event(&mmap_event);
2603}
2604
2605void perf_counter_munmap(unsigned long addr, unsigned long len,
2606 unsigned long pgoff, struct file *file)
2607{
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02002608 struct perf_mmap_event mmap_event;
2609
2610 if (!atomic_read(&nr_munmap_tracking))
2611 return;
2612
2613 mmap_event = (struct perf_mmap_event){
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002614 .file = file,
2615 .event = {
2616 .header = { .type = PERF_EVENT_MUNMAP, },
2617 .pid = current->group_leader->pid,
2618 .tid = current->pid,
2619 .start = addr,
2620 .len = len,
2621 .pgoff = pgoff,
2622 },
2623 };
2624
2625 perf_counter_mmap_event(&mmap_event);
2626}
2627
2628/*
Peter Zijlstrae220d2d2009-05-23 18:28:55 +02002629 * Log irq_period changes so that analyzing tools can re-normalize the
2630 * event flow.
Peter Zijlstra26b119b2009-05-20 12:21:20 +02002631 */
2632
2633static void perf_log_period(struct perf_counter *counter, u64 period)
2634{
2635 struct perf_output_handle handle;
2636 int ret;
2637
2638 struct {
2639 struct perf_event_header header;
2640 u64 time;
2641 u64 period;
2642 } freq_event = {
2643 .header = {
2644 .type = PERF_EVENT_PERIOD,
2645 .misc = 0,
2646 .size = sizeof(freq_event),
2647 },
2648 .time = sched_clock(),
2649 .period = period,
2650 };
2651
2652 if (counter->hw.irq_period == period)
2653 return;
2654
2655 ret = perf_output_begin(&handle, counter, sizeof(freq_event), 0, 0);
2656 if (ret)
2657 return;
2658
2659 perf_output_put(&handle, freq_event);
2660 perf_output_end(&handle);
2661}
2662
2663/*
Peter Zijlstraa78ac322009-05-25 17:39:05 +02002664 * IRQ throttle logging
2665 */
2666
2667static void perf_log_throttle(struct perf_counter *counter, int enable)
2668{
2669 struct perf_output_handle handle;
2670 int ret;
2671
2672 struct {
2673 struct perf_event_header header;
2674 u64 time;
2675 } throttle_event = {
2676 .header = {
2677 .type = PERF_EVENT_THROTTLE + 1,
2678 .misc = 0,
2679 .size = sizeof(throttle_event),
2680 },
2681 .time = sched_clock(),
2682 };
2683
Ingo Molnar0127c3e2009-05-25 22:03:26 +02002684 ret = perf_output_begin(&handle, counter, sizeof(throttle_event), 1, 0);
Peter Zijlstraa78ac322009-05-25 17:39:05 +02002685 if (ret)
2686 return;
2687
2688 perf_output_put(&handle, throttle_event);
2689 perf_output_end(&handle);
2690}
2691
2692/*
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002693 * Generic counter overflow handling.
2694 */
2695
2696int perf_counter_overflow(struct perf_counter *counter,
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002697 int nmi, struct pt_regs *regs, u64 addr)
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002698{
Peter Zijlstra79f14642009-04-06 11:45:07 +02002699 int events = atomic_read(&counter->event_limit);
Peter Zijlstraa78ac322009-05-25 17:39:05 +02002700 int throttle = counter->pmu->unthrottle != NULL;
Peter Zijlstra79f14642009-04-06 11:45:07 +02002701 int ret = 0;
2702
Peter Zijlstraa78ac322009-05-25 17:39:05 +02002703 if (!throttle) {
2704 counter->hw.interrupts++;
2705 } else if (counter->hw.interrupts != MAX_INTERRUPTS) {
2706 counter->hw.interrupts++;
2707 if (HZ*counter->hw.interrupts > (u64)sysctl_perf_counter_limit) {
2708 counter->hw.interrupts = MAX_INTERRUPTS;
2709 perf_log_throttle(counter, 0);
2710 ret = 1;
2711 }
2712 }
Peter Zijlstra60db5e02009-05-15 15:19:28 +02002713
Peter Zijlstra2023b352009-05-05 17:50:26 +02002714 /*
2715 * XXX event_limit might not quite work as expected on inherited
2716 * counters
2717 */
2718
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02002719 counter->pending_kill = POLL_IN;
Peter Zijlstra79f14642009-04-06 11:45:07 +02002720 if (events && atomic_dec_and_test(&counter->event_limit)) {
2721 ret = 1;
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02002722 counter->pending_kill = POLL_HUP;
Peter Zijlstra79f14642009-04-06 11:45:07 +02002723 if (nmi) {
2724 counter->pending_disable = 1;
2725 perf_pending_queue(&counter->pending,
2726 perf_pending_counter);
2727 } else
2728 perf_counter_disable(counter);
2729 }
2730
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002731 perf_counter_output(counter, nmi, regs, addr);
Peter Zijlstra79f14642009-04-06 11:45:07 +02002732 return ret;
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002733}
2734
2735/*
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002736 * Generic software counter infrastructure
2737 */
2738
2739static void perf_swcounter_update(struct perf_counter *counter)
2740{
2741 struct hw_perf_counter *hwc = &counter->hw;
2742 u64 prev, now;
2743 s64 delta;
2744
2745again:
2746 prev = atomic64_read(&hwc->prev_count);
2747 now = atomic64_read(&hwc->count);
2748 if (atomic64_cmpxchg(&hwc->prev_count, prev, now) != prev)
2749 goto again;
2750
2751 delta = now - prev;
2752
2753 atomic64_add(delta, &counter->count);
2754 atomic64_sub(delta, &hwc->period_left);
2755}
2756
2757static void perf_swcounter_set_period(struct perf_counter *counter)
2758{
2759 struct hw_perf_counter *hwc = &counter->hw;
2760 s64 left = atomic64_read(&hwc->period_left);
2761 s64 period = hwc->irq_period;
2762
2763 if (unlikely(left <= -period)) {
2764 left = period;
2765 atomic64_set(&hwc->period_left, left);
2766 }
2767
2768 if (unlikely(left <= 0)) {
2769 left += period;
2770 atomic64_add(period, &hwc->period_left);
2771 }
2772
2773 atomic64_set(&hwc->prev_count, -left);
2774 atomic64_set(&hwc->count, -left);
2775}
2776
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002777static enum hrtimer_restart perf_swcounter_hrtimer(struct hrtimer *hrtimer)
2778{
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002779 enum hrtimer_restart ret = HRTIMER_RESTART;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002780 struct perf_counter *counter;
2781 struct pt_regs *regs;
Peter Zijlstra60db5e02009-05-15 15:19:28 +02002782 u64 period;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002783
2784 counter = container_of(hrtimer, struct perf_counter, hw.hrtimer);
Robert Richter4aeb0b42009-04-29 12:47:03 +02002785 counter->pmu->read(counter);
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002786
2787 regs = get_irq_regs();
2788 /*
2789 * In case we exclude kernel IPs or are somehow not in interrupt
2790 * context, provide the next best thing, the user IP.
2791 */
2792 if ((counter->hw_event.exclude_kernel || !regs) &&
2793 !counter->hw_event.exclude_user)
2794 regs = task_pt_regs(current);
2795
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002796 if (regs) {
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002797 if (perf_counter_overflow(counter, 0, regs, 0))
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002798 ret = HRTIMER_NORESTART;
2799 }
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002800
Peter Zijlstra60db5e02009-05-15 15:19:28 +02002801 period = max_t(u64, 10000, counter->hw.irq_period);
2802 hrtimer_forward_now(hrtimer, ns_to_ktime(period));
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002803
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002804 return ret;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002805}
2806
2807static void perf_swcounter_overflow(struct perf_counter *counter,
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002808 int nmi, struct pt_regs *regs, u64 addr)
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002809{
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002810 perf_swcounter_update(counter);
2811 perf_swcounter_set_period(counter);
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002812 if (perf_counter_overflow(counter, nmi, regs, addr))
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002813 /* soft-disable the counter */
2814 ;
2815
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002816}
2817
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002818static int perf_swcounter_match(struct perf_counter *counter,
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002819 enum perf_event_types type,
2820 u32 event, struct pt_regs *regs)
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002821{
2822 if (counter->state != PERF_COUNTER_STATE_ACTIVE)
2823 return 0;
2824
Peter Zijlstraf4a2deb2009-03-23 18:22:06 +01002825 if (perf_event_raw(&counter->hw_event))
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002826 return 0;
2827
Peter Zijlstraf4a2deb2009-03-23 18:22:06 +01002828 if (perf_event_type(&counter->hw_event) != type)
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002829 return 0;
2830
Peter Zijlstraf4a2deb2009-03-23 18:22:06 +01002831 if (perf_event_id(&counter->hw_event) != event)
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002832 return 0;
2833
2834 if (counter->hw_event.exclude_user && user_mode(regs))
2835 return 0;
2836
2837 if (counter->hw_event.exclude_kernel && !user_mode(regs))
2838 return 0;
2839
2840 return 1;
2841}
2842
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002843static void perf_swcounter_add(struct perf_counter *counter, u64 nr,
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002844 int nmi, struct pt_regs *regs, u64 addr)
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002845{
2846 int neg = atomic64_add_negative(nr, &counter->hw.count);
2847 if (counter->hw.irq_period && !neg)
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002848 perf_swcounter_overflow(counter, nmi, regs, addr);
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002849}
2850
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002851static void perf_swcounter_ctx_event(struct perf_counter_context *ctx,
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002852 enum perf_event_types type, u32 event,
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002853 u64 nr, int nmi, struct pt_regs *regs,
2854 u64 addr)
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002855{
2856 struct perf_counter *counter;
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002857
Peter Zijlstra01ef09d2009-03-19 20:26:11 +01002858 if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002859 return;
2860
Peter Zijlstra592903c2009-03-13 12:21:36 +01002861 rcu_read_lock();
2862 list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002863 if (perf_swcounter_match(counter, type, event, regs))
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002864 perf_swcounter_add(counter, nr, nmi, regs, addr);
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002865 }
Peter Zijlstra592903c2009-03-13 12:21:36 +01002866 rcu_read_unlock();
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002867}
2868
Peter Zijlstra96f6d442009-03-23 18:22:07 +01002869static int *perf_swcounter_recursion_context(struct perf_cpu_context *cpuctx)
2870{
2871 if (in_nmi())
2872 return &cpuctx->recursion[3];
2873
2874 if (in_irq())
2875 return &cpuctx->recursion[2];
2876
2877 if (in_softirq())
2878 return &cpuctx->recursion[1];
2879
2880 return &cpuctx->recursion[0];
2881}
2882
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002883static void __perf_swcounter_event(enum perf_event_types type, u32 event,
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002884 u64 nr, int nmi, struct pt_regs *regs,
2885 u64 addr)
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002886{
2887 struct perf_cpu_context *cpuctx = &get_cpu_var(perf_cpu_context);
Peter Zijlstra96f6d442009-03-23 18:22:07 +01002888 int *recursion = perf_swcounter_recursion_context(cpuctx);
2889
2890 if (*recursion)
2891 goto out;
2892
2893 (*recursion)++;
2894 barrier();
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002895
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002896 perf_swcounter_ctx_event(&cpuctx->ctx, type, event,
2897 nr, nmi, regs, addr);
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002898 if (cpuctx->task_ctx) {
2899 perf_swcounter_ctx_event(cpuctx->task_ctx, type, event,
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002900 nr, nmi, regs, addr);
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002901 }
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002902
Peter Zijlstra96f6d442009-03-23 18:22:07 +01002903 barrier();
2904 (*recursion)--;
2905
2906out:
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002907 put_cpu_var(perf_cpu_context);
2908}
2909
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002910void
2911perf_swcounter_event(u32 event, u64 nr, int nmi, struct pt_regs *regs, u64 addr)
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002912{
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002913 __perf_swcounter_event(PERF_TYPE_SOFTWARE, event, nr, nmi, regs, addr);
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002914}
2915
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002916static void perf_swcounter_read(struct perf_counter *counter)
2917{
2918 perf_swcounter_update(counter);
2919}
2920
2921static int perf_swcounter_enable(struct perf_counter *counter)
2922{
2923 perf_swcounter_set_period(counter);
2924 return 0;
2925}
2926
2927static void perf_swcounter_disable(struct perf_counter *counter)
2928{
2929 perf_swcounter_update(counter);
2930}
2931
Robert Richter4aeb0b42009-04-29 12:47:03 +02002932static const struct pmu perf_ops_generic = {
Peter Zijlstraac17dc82009-03-13 12:21:34 +01002933 .enable = perf_swcounter_enable,
2934 .disable = perf_swcounter_disable,
2935 .read = perf_swcounter_read,
2936};
2937
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002938/*
2939 * Software counter: cpu wall time clock
2940 */
2941
Paul Mackerras9abf8a02009-01-09 16:26:43 +11002942static void cpu_clock_perf_counter_update(struct perf_counter *counter)
2943{
2944 int cpu = raw_smp_processor_id();
2945 s64 prev;
2946 u64 now;
2947
2948 now = cpu_clock(cpu);
2949 prev = atomic64_read(&counter->hw.prev_count);
2950 atomic64_set(&counter->hw.prev_count, now);
2951 atomic64_add(now - prev, &counter->count);
2952}
2953
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002954static int cpu_clock_perf_counter_enable(struct perf_counter *counter)
2955{
2956 struct hw_perf_counter *hwc = &counter->hw;
2957 int cpu = raw_smp_processor_id();
2958
2959 atomic64_set(&hwc->prev_count, cpu_clock(cpu));
Peter Zijlstra039fc912009-03-13 16:43:47 +01002960 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
2961 hwc->hrtimer.function = perf_swcounter_hrtimer;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002962 if (hwc->irq_period) {
Peter Zijlstra60db5e02009-05-15 15:19:28 +02002963 u64 period = max_t(u64, 10000, hwc->irq_period);
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002964 __hrtimer_start_range_ns(&hwc->hrtimer,
Peter Zijlstra60db5e02009-05-15 15:19:28 +02002965 ns_to_ktime(period), 0,
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002966 HRTIMER_MODE_REL, 0);
2967 }
2968
2969 return 0;
2970}
2971
Ingo Molnar5c92d122008-12-11 13:21:10 +01002972static void cpu_clock_perf_counter_disable(struct perf_counter *counter)
2973{
Peter Zijlstrab986d7e2009-05-20 12:21:21 +02002974 if (counter->hw.irq_period)
2975 hrtimer_cancel(&counter->hw.hrtimer);
Paul Mackerras9abf8a02009-01-09 16:26:43 +11002976 cpu_clock_perf_counter_update(counter);
Ingo Molnar5c92d122008-12-11 13:21:10 +01002977}
2978
2979static void cpu_clock_perf_counter_read(struct perf_counter *counter)
2980{
Paul Mackerras9abf8a02009-01-09 16:26:43 +11002981 cpu_clock_perf_counter_update(counter);
Ingo Molnar5c92d122008-12-11 13:21:10 +01002982}
2983
Robert Richter4aeb0b42009-04-29 12:47:03 +02002984static const struct pmu perf_ops_cpu_clock = {
Ingo Molnar76715812008-12-17 14:20:28 +01002985 .enable = cpu_clock_perf_counter_enable,
2986 .disable = cpu_clock_perf_counter_disable,
2987 .read = cpu_clock_perf_counter_read,
Ingo Molnar5c92d122008-12-11 13:21:10 +01002988};
2989
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01002990/*
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002991 * Software counter: task time clock
2992 */
2993
Peter Zijlstrae30e08f2009-04-08 15:01:25 +02002994static void task_clock_perf_counter_update(struct perf_counter *counter, u64 now)
Ingo Molnarbae43c92008-12-11 14:03:20 +01002995{
Peter Zijlstrae30e08f2009-04-08 15:01:25 +02002996 u64 prev;
Ingo Molnar8cb391e2008-12-14 12:22:31 +01002997 s64 delta;
Ingo Molnarbae43c92008-12-11 14:03:20 +01002998
Peter Zijlstraa39d6f22009-04-06 11:45:11 +02002999 prev = atomic64_xchg(&counter->hw.prev_count, now);
Ingo Molnar8cb391e2008-12-14 12:22:31 +01003000 delta = now - prev;
Ingo Molnar8cb391e2008-12-14 12:22:31 +01003001 atomic64_add(delta, &counter->count);
Ingo Molnarbae43c92008-12-11 14:03:20 +01003002}
3003
Ingo Molnar95cdd2e2008-12-21 13:50:42 +01003004static int task_clock_perf_counter_enable(struct perf_counter *counter)
Ingo Molnar8cb391e2008-12-14 12:22:31 +01003005{
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003006 struct hw_perf_counter *hwc = &counter->hw;
Peter Zijlstraa39d6f22009-04-06 11:45:11 +02003007 u64 now;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003008
Peter Zijlstraa39d6f22009-04-06 11:45:11 +02003009 now = counter->ctx->time;
3010
3011 atomic64_set(&hwc->prev_count, now);
Peter Zijlstra039fc912009-03-13 16:43:47 +01003012 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
3013 hwc->hrtimer.function = perf_swcounter_hrtimer;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003014 if (hwc->irq_period) {
Peter Zijlstra60db5e02009-05-15 15:19:28 +02003015 u64 period = max_t(u64, 10000, hwc->irq_period);
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003016 __hrtimer_start_range_ns(&hwc->hrtimer,
Peter Zijlstra60db5e02009-05-15 15:19:28 +02003017 ns_to_ktime(period), 0,
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003018 HRTIMER_MODE_REL, 0);
3019 }
Ingo Molnar95cdd2e2008-12-21 13:50:42 +01003020
3021 return 0;
Ingo Molnar8cb391e2008-12-14 12:22:31 +01003022}
3023
3024static void task_clock_perf_counter_disable(struct perf_counter *counter)
3025{
Peter Zijlstrab986d7e2009-05-20 12:21:21 +02003026 if (counter->hw.irq_period)
3027 hrtimer_cancel(&counter->hw.hrtimer);
Peter Zijlstrae30e08f2009-04-08 15:01:25 +02003028 task_clock_perf_counter_update(counter, counter->ctx->time);
3029
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003030}
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01003031
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003032static void task_clock_perf_counter_read(struct perf_counter *counter)
3033{
Peter Zijlstrae30e08f2009-04-08 15:01:25 +02003034 u64 time;
3035
3036 if (!in_nmi()) {
3037 update_context_time(counter->ctx);
3038 time = counter->ctx->time;
3039 } else {
3040 u64 now = perf_clock();
3041 u64 delta = now - counter->ctx->timestamp;
3042 time = counter->ctx->time + delta;
3043 }
3044
3045 task_clock_perf_counter_update(counter, time);
Ingo Molnarbae43c92008-12-11 14:03:20 +01003046}
3047
Robert Richter4aeb0b42009-04-29 12:47:03 +02003048static const struct pmu perf_ops_task_clock = {
Ingo Molnar76715812008-12-17 14:20:28 +01003049 .enable = task_clock_perf_counter_enable,
3050 .disable = task_clock_perf_counter_disable,
3051 .read = task_clock_perf_counter_read,
Ingo Molnarbae43c92008-12-11 14:03:20 +01003052};
3053
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003054/*
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003055 * Software counter: cpu migrations
3056 */
3057
Paul Mackerras23a185c2009-02-09 22:42:47 +11003058static inline u64 get_cpu_migrations(struct perf_counter *counter)
Ingo Molnar6c594c22008-12-14 12:34:15 +01003059{
Paul Mackerras23a185c2009-02-09 22:42:47 +11003060 struct task_struct *curr = counter->ctx->task;
3061
3062 if (curr)
3063 return curr->se.nr_migrations;
3064 return cpu_nr_migrations(smp_processor_id());
Ingo Molnar6c594c22008-12-14 12:34:15 +01003065}
3066
3067static void cpu_migrations_perf_counter_update(struct perf_counter *counter)
3068{
3069 u64 prev, now;
3070 s64 delta;
3071
3072 prev = atomic64_read(&counter->hw.prev_count);
Paul Mackerras23a185c2009-02-09 22:42:47 +11003073 now = get_cpu_migrations(counter);
Ingo Molnar6c594c22008-12-14 12:34:15 +01003074
3075 atomic64_set(&counter->hw.prev_count, now);
3076
3077 delta = now - prev;
Ingo Molnar6c594c22008-12-14 12:34:15 +01003078
3079 atomic64_add(delta, &counter->count);
3080}
3081
3082static void cpu_migrations_perf_counter_read(struct perf_counter *counter)
3083{
3084 cpu_migrations_perf_counter_update(counter);
3085}
3086
Ingo Molnar95cdd2e2008-12-21 13:50:42 +01003087static int cpu_migrations_perf_counter_enable(struct perf_counter *counter)
Ingo Molnar6c594c22008-12-14 12:34:15 +01003088{
Paul Mackerrasc07c99b2009-02-13 22:10:34 +11003089 if (counter->prev_state <= PERF_COUNTER_STATE_OFF)
3090 atomic64_set(&counter->hw.prev_count,
3091 get_cpu_migrations(counter));
Ingo Molnar95cdd2e2008-12-21 13:50:42 +01003092 return 0;
Ingo Molnar6c594c22008-12-14 12:34:15 +01003093}
3094
3095static void cpu_migrations_perf_counter_disable(struct perf_counter *counter)
3096{
3097 cpu_migrations_perf_counter_update(counter);
3098}
3099
Robert Richter4aeb0b42009-04-29 12:47:03 +02003100static const struct pmu perf_ops_cpu_migrations = {
Ingo Molnar76715812008-12-17 14:20:28 +01003101 .enable = cpu_migrations_perf_counter_enable,
3102 .disable = cpu_migrations_perf_counter_disable,
3103 .read = cpu_migrations_perf_counter_read,
Ingo Molnar6c594c22008-12-14 12:34:15 +01003104};
3105
Peter Zijlstrae077df42009-03-19 20:26:17 +01003106#ifdef CONFIG_EVENT_PROFILE
3107void perf_tpcounter_event(int event_id)
3108{
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003109 struct pt_regs *regs = get_irq_regs();
3110
3111 if (!regs)
3112 regs = task_pt_regs(current);
3113
Peter Zijlstra78f13e92009-04-08 15:01:33 +02003114 __perf_swcounter_event(PERF_TYPE_TRACEPOINT, event_id, 1, 1, regs, 0);
Peter Zijlstrae077df42009-03-19 20:26:17 +01003115}
Steven Whitehouseff7b1b42009-04-15 16:55:05 +01003116EXPORT_SYMBOL_GPL(perf_tpcounter_event);
Peter Zijlstrae077df42009-03-19 20:26:17 +01003117
3118extern int ftrace_profile_enable(int);
3119extern void ftrace_profile_disable(int);
3120
3121static void tp_perf_counter_destroy(struct perf_counter *counter)
3122{
Peter Zijlstraf4a2deb2009-03-23 18:22:06 +01003123 ftrace_profile_disable(perf_event_id(&counter->hw_event));
Peter Zijlstrae077df42009-03-19 20:26:17 +01003124}
3125
Robert Richter4aeb0b42009-04-29 12:47:03 +02003126static const struct pmu *tp_perf_counter_init(struct perf_counter *counter)
Peter Zijlstrae077df42009-03-19 20:26:17 +01003127{
Peter Zijlstraf4a2deb2009-03-23 18:22:06 +01003128 int event_id = perf_event_id(&counter->hw_event);
Peter Zijlstrae077df42009-03-19 20:26:17 +01003129 int ret;
3130
3131 ret = ftrace_profile_enable(event_id);
3132 if (ret)
3133 return NULL;
3134
3135 counter->destroy = tp_perf_counter_destroy;
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003136 counter->hw.irq_period = counter->hw_event.irq_period;
Peter Zijlstrae077df42009-03-19 20:26:17 +01003137
3138 return &perf_ops_generic;
3139}
3140#else
Robert Richter4aeb0b42009-04-29 12:47:03 +02003141static const struct pmu *tp_perf_counter_init(struct perf_counter *counter)
Peter Zijlstrae077df42009-03-19 20:26:17 +01003142{
3143 return NULL;
3144}
3145#endif
3146
Robert Richter4aeb0b42009-04-29 12:47:03 +02003147static const struct pmu *sw_perf_counter_init(struct perf_counter *counter)
Ingo Molnar5c92d122008-12-11 13:21:10 +01003148{
Robert Richter4aeb0b42009-04-29 12:47:03 +02003149 const struct pmu *pmu = NULL;
Ingo Molnar5c92d122008-12-11 13:21:10 +01003150
Paul Mackerras0475f9e2009-02-11 14:35:35 +11003151 /*
3152 * Software counters (currently) can't in general distinguish
3153 * between user, kernel and hypervisor events.
3154 * However, context switches and cpu migrations are considered
3155 * to be kernel events, and page faults are never hypervisor
3156 * events.
3157 */
Peter Zijlstraf4a2deb2009-03-23 18:22:06 +01003158 switch (perf_event_id(&counter->hw_event)) {
Ingo Molnar5c92d122008-12-11 13:21:10 +01003159 case PERF_COUNT_CPU_CLOCK:
Robert Richter4aeb0b42009-04-29 12:47:03 +02003160 pmu = &perf_ops_cpu_clock;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003161
Ingo Molnar5c92d122008-12-11 13:21:10 +01003162 break;
Ingo Molnarbae43c92008-12-11 14:03:20 +01003163 case PERF_COUNT_TASK_CLOCK:
Paul Mackerras23a185c2009-02-09 22:42:47 +11003164 /*
3165 * If the user instantiates this as a per-cpu counter,
3166 * use the cpu_clock counter instead.
3167 */
3168 if (counter->ctx->task)
Robert Richter4aeb0b42009-04-29 12:47:03 +02003169 pmu = &perf_ops_task_clock;
Paul Mackerras23a185c2009-02-09 22:42:47 +11003170 else
Robert Richter4aeb0b42009-04-29 12:47:03 +02003171 pmu = &perf_ops_cpu_clock;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003172
Ingo Molnarbae43c92008-12-11 14:03:20 +01003173 break;
Ingo Molnare06c61a2008-12-14 14:44:31 +01003174 case PERF_COUNT_PAGE_FAULTS:
Peter Zijlstraac17dc82009-03-13 12:21:34 +01003175 case PERF_COUNT_PAGE_FAULTS_MIN:
3176 case PERF_COUNT_PAGE_FAULTS_MAJ:
Ingo Molnar5d6a27d2008-12-14 12:28:33 +01003177 case PERF_COUNT_CONTEXT_SWITCHES:
Robert Richter4aeb0b42009-04-29 12:47:03 +02003178 pmu = &perf_ops_generic;
Ingo Molnar5d6a27d2008-12-14 12:28:33 +01003179 break;
Ingo Molnar6c594c22008-12-14 12:34:15 +01003180 case PERF_COUNT_CPU_MIGRATIONS:
Paul Mackerras0475f9e2009-02-11 14:35:35 +11003181 if (!counter->hw_event.exclude_kernel)
Robert Richter4aeb0b42009-04-29 12:47:03 +02003182 pmu = &perf_ops_cpu_migrations;
Ingo Molnar6c594c22008-12-14 12:34:15 +01003183 break;
Ingo Molnar5c92d122008-12-11 13:21:10 +01003184 }
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003185
Robert Richter4aeb0b42009-04-29 12:47:03 +02003186 return pmu;
Ingo Molnar5c92d122008-12-11 13:21:10 +01003187}
3188
Thomas Gleixner0793a612008-12-04 20:12:29 +01003189/*
3190 * Allocate and initialize a counter structure
3191 */
3192static struct perf_counter *
Ingo Molnar04289bb2008-12-11 08:38:42 +01003193perf_counter_alloc(struct perf_counter_hw_event *hw_event,
3194 int cpu,
Paul Mackerras23a185c2009-02-09 22:42:47 +11003195 struct perf_counter_context *ctx,
Ingo Molnar9b51f662008-12-12 13:49:45 +01003196 struct perf_counter *group_leader,
3197 gfp_t gfpflags)
Thomas Gleixner0793a612008-12-04 20:12:29 +01003198{
Robert Richter4aeb0b42009-04-29 12:47:03 +02003199 const struct pmu *pmu;
Ingo Molnar621a01e2008-12-11 12:46:46 +01003200 struct perf_counter *counter;
Peter Zijlstra60db5e02009-05-15 15:19:28 +02003201 struct hw_perf_counter *hwc;
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003202 long err;
Thomas Gleixner0793a612008-12-04 20:12:29 +01003203
Ingo Molnar9b51f662008-12-12 13:49:45 +01003204 counter = kzalloc(sizeof(*counter), gfpflags);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003205 if (!counter)
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003206 return ERR_PTR(-ENOMEM);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003207
Ingo Molnar04289bb2008-12-11 08:38:42 +01003208 /*
3209 * Single counters are their own group leaders, with an
3210 * empty sibling list:
3211 */
3212 if (!group_leader)
3213 group_leader = counter;
3214
Peter Zijlstrafccc7142009-05-23 18:28:56 +02003215 mutex_init(&counter->child_mutex);
3216 INIT_LIST_HEAD(&counter->child_list);
3217
Ingo Molnar04289bb2008-12-11 08:38:42 +01003218 INIT_LIST_HEAD(&counter->list_entry);
Peter Zijlstra592903c2009-03-13 12:21:36 +01003219 INIT_LIST_HEAD(&counter->event_entry);
Ingo Molnar04289bb2008-12-11 08:38:42 +01003220 INIT_LIST_HEAD(&counter->sibling_list);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003221 init_waitqueue_head(&counter->waitq);
3222
Peter Zijlstra7b732a72009-03-23 18:22:10 +01003223 mutex_init(&counter->mmap_mutex);
3224
Ingo Molnar9f66a382008-12-10 12:33:23 +01003225 counter->cpu = cpu;
3226 counter->hw_event = *hw_event;
Ingo Molnar04289bb2008-12-11 08:38:42 +01003227 counter->group_leader = group_leader;
Robert Richter4aeb0b42009-04-29 12:47:03 +02003228 counter->pmu = NULL;
Paul Mackerras23a185c2009-02-09 22:42:47 +11003229 counter->ctx = ctx;
Ingo Molnar329d8762009-05-26 08:10:00 +02003230 counter->oncpu = -1;
3231
Ingo Molnar235c7fc2008-12-21 14:43:25 +01003232 counter->state = PERF_COUNTER_STATE_INACTIVE;
Ingo Molnara86ed502008-12-17 00:43:10 +01003233 if (hw_event->disabled)
3234 counter->state = PERF_COUNTER_STATE_OFF;
3235
Robert Richter4aeb0b42009-04-29 12:47:03 +02003236 pmu = NULL;
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003237
Peter Zijlstra60db5e02009-05-15 15:19:28 +02003238 hwc = &counter->hw;
3239 if (hw_event->freq && hw_event->irq_freq)
Peter Zijlstra2e569d32009-05-15 15:37:47 +02003240 hwc->irq_period = div64_u64(TICK_NSEC, hw_event->irq_freq);
Peter Zijlstra60db5e02009-05-15 15:19:28 +02003241 else
3242 hwc->irq_period = hw_event->irq_period;
3243
Peter Zijlstra2023b352009-05-05 17:50:26 +02003244 /*
3245 * we currently do not support PERF_RECORD_GROUP on inherited counters
3246 */
3247 if (hw_event->inherit && (hw_event->record_type & PERF_RECORD_GROUP))
3248 goto done;
3249
Peter Zijlstraf4a2deb2009-03-23 18:22:06 +01003250 if (perf_event_raw(hw_event)) {
Robert Richter4aeb0b42009-04-29 12:47:03 +02003251 pmu = hw_perf_counter_init(counter);
Peter Zijlstraf4a2deb2009-03-23 18:22:06 +01003252 goto done;
3253 }
3254
3255 switch (perf_event_type(hw_event)) {
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003256 case PERF_TYPE_HARDWARE:
Robert Richter4aeb0b42009-04-29 12:47:03 +02003257 pmu = hw_perf_counter_init(counter);
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003258 break;
3259
3260 case PERF_TYPE_SOFTWARE:
Robert Richter4aeb0b42009-04-29 12:47:03 +02003261 pmu = sw_perf_counter_init(counter);
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003262 break;
3263
3264 case PERF_TYPE_TRACEPOINT:
Robert Richter4aeb0b42009-04-29 12:47:03 +02003265 pmu = tp_perf_counter_init(counter);
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003266 break;
3267 }
Peter Zijlstraf4a2deb2009-03-23 18:22:06 +01003268done:
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003269 err = 0;
Robert Richter4aeb0b42009-04-29 12:47:03 +02003270 if (!pmu)
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003271 err = -EINVAL;
Robert Richter4aeb0b42009-04-29 12:47:03 +02003272 else if (IS_ERR(pmu))
3273 err = PTR_ERR(pmu);
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003274
3275 if (err) {
3276 kfree(counter);
3277 return ERR_PTR(err);
3278 }
3279
Robert Richter4aeb0b42009-04-29 12:47:03 +02003280 counter->pmu = pmu;
Thomas Gleixner0793a612008-12-04 20:12:29 +01003281
Peter Zijlstra7fc23a52009-05-08 18:52:21 +02003282 atomic_inc(&nr_counters);
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02003283 if (counter->hw_event.mmap)
3284 atomic_inc(&nr_mmap_tracking);
3285 if (counter->hw_event.munmap)
3286 atomic_inc(&nr_munmap_tracking);
3287 if (counter->hw_event.comm)
3288 atomic_inc(&nr_comm_tracking);
3289
Thomas Gleixner0793a612008-12-04 20:12:29 +01003290 return counter;
3291}
3292
3293/**
Paul Mackerras2743a5b2009-03-04 20:36:51 +11003294 * sys_perf_counter_open - open a performance counter, associate it to a task/cpu
Ingo Molnar9f66a382008-12-10 12:33:23 +01003295 *
3296 * @hw_event_uptr: event type attributes for monitoring/sampling
Thomas Gleixner0793a612008-12-04 20:12:29 +01003297 * @pid: target pid
Ingo Molnar9f66a382008-12-10 12:33:23 +01003298 * @cpu: target cpu
3299 * @group_fd: group leader counter fd
Thomas Gleixner0793a612008-12-04 20:12:29 +01003300 */
Paul Mackerras2743a5b2009-03-04 20:36:51 +11003301SYSCALL_DEFINE5(perf_counter_open,
Paul Mackerrasf3dfd262009-02-26 22:43:46 +11003302 const struct perf_counter_hw_event __user *, hw_event_uptr,
Paul Mackerras2743a5b2009-03-04 20:36:51 +11003303 pid_t, pid, int, cpu, int, group_fd, unsigned long, flags)
Thomas Gleixner0793a612008-12-04 20:12:29 +01003304{
Ingo Molnar04289bb2008-12-11 08:38:42 +01003305 struct perf_counter *counter, *group_leader;
Ingo Molnar9f66a382008-12-10 12:33:23 +01003306 struct perf_counter_hw_event hw_event;
Ingo Molnar04289bb2008-12-11 08:38:42 +01003307 struct perf_counter_context *ctx;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003308 struct file *counter_file = NULL;
Ingo Molnar04289bb2008-12-11 08:38:42 +01003309 struct file *group_file = NULL;
3310 int fput_needed = 0;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003311 int fput_needed2 = 0;
Thomas Gleixner0793a612008-12-04 20:12:29 +01003312 int ret;
3313
Paul Mackerras2743a5b2009-03-04 20:36:51 +11003314 /* for future expandability... */
3315 if (flags)
3316 return -EINVAL;
3317
Ingo Molnar9f66a382008-12-10 12:33:23 +01003318 if (copy_from_user(&hw_event, hw_event_uptr, sizeof(hw_event)) != 0)
Thomas Gleixnereab656a2008-12-08 19:26:59 +01003319 return -EFAULT;
3320
Ingo Molnar04289bb2008-12-11 08:38:42 +01003321 /*
Ingo Molnarccff2862008-12-11 11:26:29 +01003322 * Get the target context (task or percpu):
3323 */
3324 ctx = find_get_context(pid, cpu);
3325 if (IS_ERR(ctx))
3326 return PTR_ERR(ctx);
3327
3328 /*
3329 * Look up the group leader (we will attach this counter to it):
Ingo Molnar04289bb2008-12-11 08:38:42 +01003330 */
3331 group_leader = NULL;
3332 if (group_fd != -1) {
3333 ret = -EINVAL;
3334 group_file = fget_light(group_fd, &fput_needed);
3335 if (!group_file)
Ingo Molnarccff2862008-12-11 11:26:29 +01003336 goto err_put_context;
Ingo Molnar04289bb2008-12-11 08:38:42 +01003337 if (group_file->f_op != &perf_fops)
Ingo Molnarccff2862008-12-11 11:26:29 +01003338 goto err_put_context;
Ingo Molnar04289bb2008-12-11 08:38:42 +01003339
3340 group_leader = group_file->private_data;
3341 /*
Ingo Molnarccff2862008-12-11 11:26:29 +01003342 * Do not allow a recursive hierarchy (this new sibling
3343 * becoming part of another group-sibling):
Ingo Molnar04289bb2008-12-11 08:38:42 +01003344 */
Ingo Molnarccff2862008-12-11 11:26:29 +01003345 if (group_leader->group_leader != group_leader)
3346 goto err_put_context;
3347 /*
3348 * Do not allow to attach to a group in a different
3349 * task or CPU context:
3350 */
3351 if (group_leader->ctx != ctx)
3352 goto err_put_context;
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11003353 /*
3354 * Only a group leader can be exclusive or pinned
3355 */
3356 if (hw_event.exclusive || hw_event.pinned)
3357 goto err_put_context;
Ingo Molnar04289bb2008-12-11 08:38:42 +01003358 }
3359
Paul Mackerras23a185c2009-02-09 22:42:47 +11003360 counter = perf_counter_alloc(&hw_event, cpu, ctx, group_leader,
3361 GFP_KERNEL);
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003362 ret = PTR_ERR(counter);
3363 if (IS_ERR(counter))
Thomas Gleixner0793a612008-12-04 20:12:29 +01003364 goto err_put_context;
3365
Thomas Gleixner0793a612008-12-04 20:12:29 +01003366 ret = anon_inode_getfd("[perf_counter]", &perf_fops, counter, 0);
3367 if (ret < 0)
Ingo Molnar9b51f662008-12-12 13:49:45 +01003368 goto err_free_put_context;
3369
3370 counter_file = fget_light(ret, &fput_needed2);
3371 if (!counter_file)
3372 goto err_free_put_context;
3373
3374 counter->filp = counter_file;
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10003375 WARN_ON_ONCE(ctx->parent_ctx);
Paul Mackerrasd859e292009-01-17 18:10:22 +11003376 mutex_lock(&ctx->mutex);
Ingo Molnar9b51f662008-12-12 13:49:45 +01003377 perf_install_in_context(ctx, counter, cpu);
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10003378 ++ctx->generation;
Paul Mackerrasd859e292009-01-17 18:10:22 +11003379 mutex_unlock(&ctx->mutex);
Ingo Molnar9b51f662008-12-12 13:49:45 +01003380
Peter Zijlstra082ff5a2009-05-23 18:29:00 +02003381 counter->owner = current;
3382 get_task_struct(current);
3383 mutex_lock(&current->perf_counter_mutex);
3384 list_add_tail(&counter->owner_entry, &current->perf_counter_list);
3385 mutex_unlock(&current->perf_counter_mutex);
3386
Ingo Molnar9b51f662008-12-12 13:49:45 +01003387 fput_light(counter_file, fput_needed2);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003388
Ingo Molnar04289bb2008-12-11 08:38:42 +01003389out_fput:
3390 fput_light(group_file, fput_needed);
3391
Thomas Gleixner0793a612008-12-04 20:12:29 +01003392 return ret;
3393
Ingo Molnar9b51f662008-12-12 13:49:45 +01003394err_free_put_context:
Thomas Gleixner0793a612008-12-04 20:12:29 +01003395 kfree(counter);
3396
3397err_put_context:
Paul Mackerrasc93f7662009-05-28 22:18:17 +10003398 put_ctx(ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003399
Ingo Molnar04289bb2008-12-11 08:38:42 +01003400 goto out_fput;
Thomas Gleixner0793a612008-12-04 20:12:29 +01003401}
3402
Ingo Molnar9b51f662008-12-12 13:49:45 +01003403/*
Ingo Molnar9b51f662008-12-12 13:49:45 +01003404 * inherit a counter from parent task to child task:
3405 */
Paul Mackerrasd859e292009-01-17 18:10:22 +11003406static struct perf_counter *
Ingo Molnar9b51f662008-12-12 13:49:45 +01003407inherit_counter(struct perf_counter *parent_counter,
3408 struct task_struct *parent,
3409 struct perf_counter_context *parent_ctx,
3410 struct task_struct *child,
Paul Mackerrasd859e292009-01-17 18:10:22 +11003411 struct perf_counter *group_leader,
Ingo Molnar9b51f662008-12-12 13:49:45 +01003412 struct perf_counter_context *child_ctx)
3413{
3414 struct perf_counter *child_counter;
3415
Paul Mackerrasd859e292009-01-17 18:10:22 +11003416 /*
3417 * Instead of creating recursive hierarchies of counters,
3418 * we link inherited counters back to the original parent,
3419 * which has a filp for sure, which we use as the reference
3420 * count:
3421 */
3422 if (parent_counter->parent)
3423 parent_counter = parent_counter->parent;
3424
Ingo Molnar9b51f662008-12-12 13:49:45 +01003425 child_counter = perf_counter_alloc(&parent_counter->hw_event,
Paul Mackerras23a185c2009-02-09 22:42:47 +11003426 parent_counter->cpu, child_ctx,
3427 group_leader, GFP_KERNEL);
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003428 if (IS_ERR(child_counter))
3429 return child_counter;
Paul Mackerrasc93f7662009-05-28 22:18:17 +10003430 get_ctx(child_ctx);
Ingo Molnar9b51f662008-12-12 13:49:45 +01003431
3432 /*
Paul Mackerras564c2b22009-05-22 14:27:22 +10003433 * Make the child state follow the state of the parent counter,
3434 * not its hw_event.disabled bit. We hold the parent's mutex,
3435 * so we won't race with perf_counter_{en,dis}able_family.
3436 */
3437 if (parent_counter->state >= PERF_COUNTER_STATE_INACTIVE)
3438 child_counter->state = PERF_COUNTER_STATE_INACTIVE;
3439 else
3440 child_counter->state = PERF_COUNTER_STATE_OFF;
3441
3442 /*
Ingo Molnar9b51f662008-12-12 13:49:45 +01003443 * Link it up in the child's context:
3444 */
Paul Mackerras53cfbf52009-03-25 22:46:58 +11003445 add_counter_to_ctx(child_counter, child_ctx);
Ingo Molnar9b51f662008-12-12 13:49:45 +01003446
3447 child_counter->parent = parent_counter;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003448 /*
3449 * inherit into child's child as well:
3450 */
3451 child_counter->hw_event.inherit = 1;
3452
3453 /*
3454 * Get a reference to the parent filp - we will fput it
3455 * when the child counter exits. This is safe to do because
3456 * we are in the parent and we know that the filp still
3457 * exists and has a nonzero count:
3458 */
3459 atomic_long_inc(&parent_counter->filp->f_count);
3460
Paul Mackerrasd859e292009-01-17 18:10:22 +11003461 /*
3462 * Link this into the parent counter's child list
3463 */
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10003464 WARN_ON_ONCE(parent_counter->ctx->parent_ctx);
Peter Zijlstrafccc7142009-05-23 18:28:56 +02003465 mutex_lock(&parent_counter->child_mutex);
Paul Mackerrasd859e292009-01-17 18:10:22 +11003466 list_add_tail(&child_counter->child_list, &parent_counter->child_list);
Peter Zijlstrafccc7142009-05-23 18:28:56 +02003467 mutex_unlock(&parent_counter->child_mutex);
Paul Mackerrasd859e292009-01-17 18:10:22 +11003468
3469 return child_counter;
3470}
3471
3472static int inherit_group(struct perf_counter *parent_counter,
3473 struct task_struct *parent,
3474 struct perf_counter_context *parent_ctx,
3475 struct task_struct *child,
3476 struct perf_counter_context *child_ctx)
3477{
3478 struct perf_counter *leader;
3479 struct perf_counter *sub;
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003480 struct perf_counter *child_ctr;
Paul Mackerrasd859e292009-01-17 18:10:22 +11003481
3482 leader = inherit_counter(parent_counter, parent, parent_ctx,
3483 child, NULL, child_ctx);
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003484 if (IS_ERR(leader))
3485 return PTR_ERR(leader);
Paul Mackerrasd859e292009-01-17 18:10:22 +11003486 list_for_each_entry(sub, &parent_counter->sibling_list, list_entry) {
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003487 child_ctr = inherit_counter(sub, parent, parent_ctx,
3488 child, leader, child_ctx);
3489 if (IS_ERR(child_ctr))
3490 return PTR_ERR(child_ctr);
Paul Mackerrasd859e292009-01-17 18:10:22 +11003491 }
Ingo Molnar9b51f662008-12-12 13:49:45 +01003492 return 0;
3493}
3494
Paul Mackerrasd859e292009-01-17 18:10:22 +11003495static void sync_child_counter(struct perf_counter *child_counter,
3496 struct perf_counter *parent_counter)
3497{
Peter Zijlstra8bc20952009-05-15 20:45:59 +02003498 u64 child_val;
Paul Mackerrasd859e292009-01-17 18:10:22 +11003499
Paul Mackerrasd859e292009-01-17 18:10:22 +11003500 child_val = atomic64_read(&child_counter->count);
3501
3502 /*
3503 * Add back the child's count to the parent's count:
3504 */
3505 atomic64_add(child_val, &parent_counter->count);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11003506 atomic64_add(child_counter->total_time_enabled,
3507 &parent_counter->child_total_time_enabled);
3508 atomic64_add(child_counter->total_time_running,
3509 &parent_counter->child_total_time_running);
Paul Mackerrasd859e292009-01-17 18:10:22 +11003510
3511 /*
3512 * Remove this counter from the parent's list
3513 */
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10003514 WARN_ON_ONCE(parent_counter->ctx->parent_ctx);
Peter Zijlstrafccc7142009-05-23 18:28:56 +02003515 mutex_lock(&parent_counter->child_mutex);
Paul Mackerrasd859e292009-01-17 18:10:22 +11003516 list_del_init(&child_counter->child_list);
Peter Zijlstrafccc7142009-05-23 18:28:56 +02003517 mutex_unlock(&parent_counter->child_mutex);
Paul Mackerrasd859e292009-01-17 18:10:22 +11003518
3519 /*
3520 * Release the parent counter, if this was the last
3521 * reference to it.
3522 */
3523 fput(parent_counter->filp);
3524}
3525
Ingo Molnar9b51f662008-12-12 13:49:45 +01003526static void
3527__perf_counter_exit_task(struct task_struct *child,
3528 struct perf_counter *child_counter,
3529 struct perf_counter_context *child_ctx)
3530{
3531 struct perf_counter *parent_counter;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003532
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10003533 update_counter_times(child_counter);
Peter Zijlstraaa9c67f2009-05-23 18:28:59 +02003534 perf_counter_remove_from_context(child_counter);
Ingo Molnar0cc0c022008-12-14 23:20:36 +01003535
Ingo Molnar9b51f662008-12-12 13:49:45 +01003536 parent_counter = child_counter->parent;
3537 /*
3538 * It can happen that parent exits first, and has counters
3539 * that are still around due to the child reference. These
3540 * counters need to be zapped - but otherwise linger.
3541 */
Paul Mackerrasd859e292009-01-17 18:10:22 +11003542 if (parent_counter) {
3543 sync_child_counter(child_counter, parent_counter);
Peter Zijlstraf1600952009-03-19 20:26:16 +01003544 free_counter(child_counter);
Paul Mackerras4bcf3492009-02-11 13:53:19 +01003545 }
Ingo Molnar9b51f662008-12-12 13:49:45 +01003546}
3547
3548/*
Paul Mackerrasd859e292009-01-17 18:10:22 +11003549 * When a child task exits, feed back counter values to parent counters.
Ingo Molnar9b51f662008-12-12 13:49:45 +01003550 */
3551void perf_counter_exit_task(struct task_struct *child)
3552{
3553 struct perf_counter *child_counter, *tmp;
3554 struct perf_counter_context *child_ctx;
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10003555 unsigned long flags;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003556
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10003557 if (likely(!child->perf_counter_ctxp))
Ingo Molnar9b51f662008-12-12 13:49:45 +01003558 return;
3559
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10003560 local_irq_save(flags);
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10003561 /*
3562 * We can't reschedule here because interrupts are disabled,
3563 * and either child is current or it is a task that can't be
3564 * scheduled, so we are now safe from rescheduling changing
3565 * our context.
3566 */
3567 child_ctx = child->perf_counter_ctxp;
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10003568 __perf_counter_task_sched_out(child_ctx);
Paul Mackerrasc93f7662009-05-28 22:18:17 +10003569
3570 /*
3571 * Take the context lock here so that if find_get_context is
3572 * reading child->perf_counter_ctxp, we wait until it has
3573 * incremented the context's refcount before we do put_ctx below.
3574 */
3575 spin_lock(&child_ctx->lock);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10003576 child->perf_counter_ctxp = NULL;
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10003577 if (child_ctx->parent_ctx) {
3578 /*
3579 * This context is a clone; unclone it so it can't get
3580 * swapped to another process while we're removing all
3581 * the counters from it.
3582 */
3583 put_ctx(child_ctx->parent_ctx);
3584 child_ctx->parent_ctx = NULL;
3585 }
Paul Mackerrasc93f7662009-05-28 22:18:17 +10003586 spin_unlock(&child_ctx->lock);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10003587 local_irq_restore(flags);
3588
3589 mutex_lock(&child_ctx->mutex);
3590
Peter Zijlstra8bc20952009-05-15 20:45:59 +02003591again:
Ingo Molnar9b51f662008-12-12 13:49:45 +01003592 list_for_each_entry_safe(child_counter, tmp, &child_ctx->counter_list,
3593 list_entry)
3594 __perf_counter_exit_task(child, child_counter, child_ctx);
Peter Zijlstra8bc20952009-05-15 20:45:59 +02003595
3596 /*
3597 * If the last counter was a group counter, it will have appended all
3598 * its siblings to the list, but we obtained 'tmp' before that which
3599 * will still point to the list head terminating the iteration.
3600 */
3601 if (!list_empty(&child_ctx->counter_list))
3602 goto again;
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10003603
3604 mutex_unlock(&child_ctx->mutex);
3605
3606 put_ctx(child_ctx);
Ingo Molnar9b51f662008-12-12 13:49:45 +01003607}
3608
3609/*
3610 * Initialize the perf_counter context in task_struct
3611 */
Peter Zijlstra6ab423e2009-05-25 14:45:27 +02003612int perf_counter_init_task(struct task_struct *child)
Ingo Molnar9b51f662008-12-12 13:49:45 +01003613{
3614 struct perf_counter_context *child_ctx, *parent_ctx;
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10003615 struct perf_counter_context *cloned_ctx;
Paul Mackerrasd859e292009-01-17 18:10:22 +11003616 struct perf_counter *counter;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003617 struct task_struct *parent = current;
Paul Mackerras564c2b22009-05-22 14:27:22 +10003618 int inherited_all = 1;
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10003619 u64 cloned_gen;
Peter Zijlstra6ab423e2009-05-25 14:45:27 +02003620 int ret = 0;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003621
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10003622 child->perf_counter_ctxp = NULL;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003623
Peter Zijlstra082ff5a2009-05-23 18:29:00 +02003624 mutex_init(&child->perf_counter_mutex);
3625 INIT_LIST_HEAD(&child->perf_counter_list);
3626
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10003627 if (likely(!parent->perf_counter_ctxp))
Peter Zijlstra6ab423e2009-05-25 14:45:27 +02003628 return 0;
3629
Ingo Molnar9b51f662008-12-12 13:49:45 +01003630 /*
3631 * This is executed from the parent task context, so inherit
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10003632 * counters that have been marked for cloning.
3633 * First allocate and initialize a context for the child.
Ingo Molnar9b51f662008-12-12 13:49:45 +01003634 */
3635
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10003636 child_ctx = kmalloc(sizeof(struct perf_counter_context), GFP_KERNEL);
3637 if (!child_ctx)
Peter Zijlstra6ab423e2009-05-25 14:45:27 +02003638 return -ENOMEM;
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10003639
3640 __perf_counter_init_context(child_ctx, child);
3641 child->perf_counter_ctxp = child_ctx;
Paul Mackerrasc93f7662009-05-28 22:18:17 +10003642 get_task_struct(child);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10003643
Ingo Molnar9b51f662008-12-12 13:49:45 +01003644 /*
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10003645 * If the parent's context is a clone, temporarily set its
3646 * parent_gen to an impossible value (all 1s) so it won't get
3647 * swapped under us. The rcu_read_lock makes sure that
3648 * parent_ctx continues to exist even if it gets swapped to
3649 * another process and then freed while we are trying to get
3650 * its lock.
3651 */
3652 rcu_read_lock();
3653 retry:
3654 parent_ctx = rcu_dereference(parent->perf_counter_ctxp);
3655 /*
3656 * No need to check if parent_ctx != NULL here; since we saw
3657 * it non-NULL earlier, the only reason for it to become NULL
3658 * is if we exit, and since we're currently in the middle of
3659 * a fork we can't be exiting at the same time.
3660 */
3661 spin_lock_irq(&parent_ctx->lock);
3662 if (parent_ctx != rcu_dereference(parent->perf_counter_ctxp)) {
3663 spin_unlock_irq(&parent_ctx->lock);
3664 goto retry;
3665 }
3666 cloned_gen = parent_ctx->parent_gen;
3667 if (parent_ctx->parent_ctx)
3668 parent_ctx->parent_gen = ~0ull;
3669 spin_unlock_irq(&parent_ctx->lock);
3670 rcu_read_unlock();
3671
3672 /*
Ingo Molnar9b51f662008-12-12 13:49:45 +01003673 * Lock the parent list. No need to lock the child - not PID
3674 * hashed yet and not running, so nobody can access it.
3675 */
Paul Mackerrasd859e292009-01-17 18:10:22 +11003676 mutex_lock(&parent_ctx->mutex);
Ingo Molnar9b51f662008-12-12 13:49:45 +01003677
3678 /*
3679 * We dont have to disable NMIs - we are only looking at
3680 * the list, not manipulating it:
3681 */
Peter Zijlstrad7b629a2009-05-20 12:21:19 +02003682 list_for_each_entry_rcu(counter, &parent_ctx->event_list, event_entry) {
3683 if (counter != counter->group_leader)
3684 continue;
3685
Paul Mackerras564c2b22009-05-22 14:27:22 +10003686 if (!counter->hw_event.inherit) {
3687 inherited_all = 0;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003688 continue;
Paul Mackerras564c2b22009-05-22 14:27:22 +10003689 }
Ingo Molnar9b51f662008-12-12 13:49:45 +01003690
Peter Zijlstra6ab423e2009-05-25 14:45:27 +02003691 ret = inherit_group(counter, parent, parent_ctx,
3692 child, child_ctx);
3693 if (ret) {
Paul Mackerras564c2b22009-05-22 14:27:22 +10003694 inherited_all = 0;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003695 break;
Paul Mackerras564c2b22009-05-22 14:27:22 +10003696 }
3697 }
3698
3699 if (inherited_all) {
3700 /*
3701 * Mark the child context as a clone of the parent
3702 * context, or of whatever the parent is a clone of.
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10003703 * Note that if the parent is a clone, it could get
3704 * uncloned at any point, but that doesn't matter
3705 * because the list of counters and the generation
3706 * count can't have changed since we took the mutex.
Paul Mackerras564c2b22009-05-22 14:27:22 +10003707 */
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10003708 cloned_ctx = rcu_dereference(parent_ctx->parent_ctx);
3709 if (cloned_ctx) {
3710 child_ctx->parent_ctx = cloned_ctx;
3711 child_ctx->parent_gen = cloned_gen;
Paul Mackerras564c2b22009-05-22 14:27:22 +10003712 } else {
3713 child_ctx->parent_ctx = parent_ctx;
3714 child_ctx->parent_gen = parent_ctx->generation;
3715 }
3716 get_ctx(child_ctx->parent_ctx);
Ingo Molnar9b51f662008-12-12 13:49:45 +01003717 }
3718
Paul Mackerrasd859e292009-01-17 18:10:22 +11003719 mutex_unlock(&parent_ctx->mutex);
Peter Zijlstra6ab423e2009-05-25 14:45:27 +02003720
Paul Mackerrasad3a37d2009-05-29 16:06:20 +10003721 /*
3722 * Restore the clone status of the parent.
3723 */
3724 if (parent_ctx->parent_ctx) {
3725 spin_lock_irq(&parent_ctx->lock);
3726 if (parent_ctx->parent_ctx)
3727 parent_ctx->parent_gen = cloned_gen;
3728 spin_unlock_irq(&parent_ctx->lock);
3729 }
3730
Peter Zijlstra6ab423e2009-05-25 14:45:27 +02003731 return ret;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003732}
3733
Ingo Molnar04289bb2008-12-11 08:38:42 +01003734static void __cpuinit perf_counter_init_cpu(int cpu)
Thomas Gleixner0793a612008-12-04 20:12:29 +01003735{
Ingo Molnar04289bb2008-12-11 08:38:42 +01003736 struct perf_cpu_context *cpuctx;
Thomas Gleixner0793a612008-12-04 20:12:29 +01003737
Ingo Molnar04289bb2008-12-11 08:38:42 +01003738 cpuctx = &per_cpu(perf_cpu_context, cpu);
3739 __perf_counter_init_context(&cpuctx->ctx, NULL);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003740
Ingo Molnar1dce8d92009-05-04 19:23:18 +02003741 spin_lock(&perf_resource_lock);
Ingo Molnar04289bb2008-12-11 08:38:42 +01003742 cpuctx->max_pertask = perf_max_counters - perf_reserved_percpu;
Ingo Molnar1dce8d92009-05-04 19:23:18 +02003743 spin_unlock(&perf_resource_lock);
Ingo Molnar04289bb2008-12-11 08:38:42 +01003744
Paul Mackerras01d02872009-01-14 13:44:19 +11003745 hw_perf_counter_setup(cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003746}
3747
3748#ifdef CONFIG_HOTPLUG_CPU
Ingo Molnar04289bb2008-12-11 08:38:42 +01003749static void __perf_counter_exit_cpu(void *info)
Thomas Gleixner0793a612008-12-04 20:12:29 +01003750{
3751 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
3752 struct perf_counter_context *ctx = &cpuctx->ctx;
3753 struct perf_counter *counter, *tmp;
3754
Ingo Molnar04289bb2008-12-11 08:38:42 +01003755 list_for_each_entry_safe(counter, tmp, &ctx->counter_list, list_entry)
3756 __perf_counter_remove_from_context(counter);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003757}
Ingo Molnar04289bb2008-12-11 08:38:42 +01003758static void perf_counter_exit_cpu(int cpu)
Thomas Gleixner0793a612008-12-04 20:12:29 +01003759{
Paul Mackerrasd859e292009-01-17 18:10:22 +11003760 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
3761 struct perf_counter_context *ctx = &cpuctx->ctx;
3762
3763 mutex_lock(&ctx->mutex);
Ingo Molnar04289bb2008-12-11 08:38:42 +01003764 smp_call_function_single(cpu, __perf_counter_exit_cpu, NULL, 1);
Paul Mackerrasd859e292009-01-17 18:10:22 +11003765 mutex_unlock(&ctx->mutex);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003766}
3767#else
Ingo Molnar04289bb2008-12-11 08:38:42 +01003768static inline void perf_counter_exit_cpu(int cpu) { }
Thomas Gleixner0793a612008-12-04 20:12:29 +01003769#endif
3770
3771static int __cpuinit
3772perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
3773{
3774 unsigned int cpu = (long)hcpu;
3775
3776 switch (action) {
3777
3778 case CPU_UP_PREPARE:
3779 case CPU_UP_PREPARE_FROZEN:
Ingo Molnar04289bb2008-12-11 08:38:42 +01003780 perf_counter_init_cpu(cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003781 break;
3782
3783 case CPU_DOWN_PREPARE:
3784 case CPU_DOWN_PREPARE_FROZEN:
Ingo Molnar04289bb2008-12-11 08:38:42 +01003785 perf_counter_exit_cpu(cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003786 break;
3787
3788 default:
3789 break;
3790 }
3791
3792 return NOTIFY_OK;
3793}
3794
3795static struct notifier_block __cpuinitdata perf_cpu_nb = {
3796 .notifier_call = perf_cpu_notify,
3797};
3798
Ingo Molnar0d905bc2009-05-04 19:13:30 +02003799void __init perf_counter_init(void)
Thomas Gleixner0793a612008-12-04 20:12:29 +01003800{
3801 perf_cpu_notify(&perf_cpu_nb, (unsigned long)CPU_UP_PREPARE,
3802 (void *)(long)smp_processor_id());
3803 register_cpu_notifier(&perf_cpu_nb);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003804}
Thomas Gleixner0793a612008-12-04 20:12:29 +01003805
3806static ssize_t perf_show_reserve_percpu(struct sysdev_class *class, char *buf)
3807{
3808 return sprintf(buf, "%d\n", perf_reserved_percpu);
3809}
3810
3811static ssize_t
3812perf_set_reserve_percpu(struct sysdev_class *class,
3813 const char *buf,
3814 size_t count)
3815{
3816 struct perf_cpu_context *cpuctx;
3817 unsigned long val;
3818 int err, cpu, mpt;
3819
3820 err = strict_strtoul(buf, 10, &val);
3821 if (err)
3822 return err;
3823 if (val > perf_max_counters)
3824 return -EINVAL;
3825
Ingo Molnar1dce8d92009-05-04 19:23:18 +02003826 spin_lock(&perf_resource_lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003827 perf_reserved_percpu = val;
3828 for_each_online_cpu(cpu) {
3829 cpuctx = &per_cpu(perf_cpu_context, cpu);
3830 spin_lock_irq(&cpuctx->ctx.lock);
3831 mpt = min(perf_max_counters - cpuctx->ctx.nr_counters,
3832 perf_max_counters - perf_reserved_percpu);
3833 cpuctx->max_pertask = mpt;
3834 spin_unlock_irq(&cpuctx->ctx.lock);
3835 }
Ingo Molnar1dce8d92009-05-04 19:23:18 +02003836 spin_unlock(&perf_resource_lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003837
3838 return count;
3839}
3840
3841static ssize_t perf_show_overcommit(struct sysdev_class *class, char *buf)
3842{
3843 return sprintf(buf, "%d\n", perf_overcommit);
3844}
3845
3846static ssize_t
3847perf_set_overcommit(struct sysdev_class *class, const char *buf, size_t count)
3848{
3849 unsigned long val;
3850 int err;
3851
3852 err = strict_strtoul(buf, 10, &val);
3853 if (err)
3854 return err;
3855 if (val > 1)
3856 return -EINVAL;
3857
Ingo Molnar1dce8d92009-05-04 19:23:18 +02003858 spin_lock(&perf_resource_lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003859 perf_overcommit = val;
Ingo Molnar1dce8d92009-05-04 19:23:18 +02003860 spin_unlock(&perf_resource_lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003861
3862 return count;
3863}
3864
3865static SYSDEV_CLASS_ATTR(
3866 reserve_percpu,
3867 0644,
3868 perf_show_reserve_percpu,
3869 perf_set_reserve_percpu
3870 );
3871
3872static SYSDEV_CLASS_ATTR(
3873 overcommit,
3874 0644,
3875 perf_show_overcommit,
3876 perf_set_overcommit
3877 );
3878
3879static struct attribute *perfclass_attrs[] = {
3880 &attr_reserve_percpu.attr,
3881 &attr_overcommit.attr,
3882 NULL
3883};
3884
3885static struct attribute_group perfclass_attr_group = {
3886 .attrs = perfclass_attrs,
3887 .name = "perf_counters",
3888};
3889
3890static int __init perf_counter_sysfs_init(void)
3891{
3892 return sysfs_create_group(&cpu_sysdev_class.kset.kobj,
3893 &perfclass_attr_group);
3894}
3895device_initcall(perf_counter_sysfs_init);