blob: 217dbcce2ebdb5d9026fcb5f32d48b414a3b8d90 [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 Zijlstra1ccd1542009-04-09 10:53:45 +020049
Thomas Gleixner0793a612008-12-04 20:12:29 +010050/*
Ingo Molnar1dce8d92009-05-04 19:23:18 +020051 * Lock for (sysadmin-configurable) counter reservations:
Thomas Gleixner0793a612008-12-04 20:12:29 +010052 */
Ingo Molnar1dce8d92009-05-04 19:23:18 +020053static DEFINE_SPINLOCK(perf_resource_lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +010054
55/*
56 * Architecture provided APIs - weak aliases:
57 */
Robert Richter4aeb0b42009-04-29 12:47:03 +020058extern __weak const struct pmu *hw_perf_counter_init(struct perf_counter *counter)
Thomas Gleixner0793a612008-12-04 20:12:29 +010059{
Paul Mackerrasff6f0542009-01-09 16:19:25 +110060 return NULL;
Thomas Gleixner0793a612008-12-04 20:12:29 +010061}
62
Peter Zijlstra9e35ad32009-05-13 16:21:38 +020063void __weak hw_perf_disable(void) { barrier(); }
64void __weak hw_perf_enable(void) { barrier(); }
65
Paul Mackerras01d02872009-01-14 13:44:19 +110066void __weak hw_perf_counter_setup(int cpu) { barrier(); }
Paul Mackerras3cbed422009-01-09 16:43:42 +110067int __weak hw_perf_group_sched_in(struct perf_counter *group_leader,
68 struct perf_cpu_context *cpuctx,
69 struct perf_counter_context *ctx, int cpu)
70{
71 return 0;
72}
Thomas Gleixner0793a612008-12-04 20:12:29 +010073
Paul Mackerras4eb96fc2009-01-09 17:24:34 +110074void __weak perf_counter_print_debug(void) { }
75
Peter Zijlstra9e35ad32009-05-13 16:21:38 +020076static DEFINE_PER_CPU(int, disable_count);
77
78void __perf_disable(void)
79{
80 __get_cpu_var(disable_count)++;
81}
82
83bool __perf_enable(void)
84{
85 return !--__get_cpu_var(disable_count);
86}
87
88void perf_disable(void)
89{
90 __perf_disable();
91 hw_perf_disable();
92}
Peter Zijlstra9e35ad32009-05-13 16:21:38 +020093
94void perf_enable(void)
95{
96 if (__perf_enable())
97 hw_perf_enable();
98}
Peter Zijlstra9e35ad32009-05-13 16:21:38 +020099
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000100static void get_ctx(struct perf_counter_context *ctx)
101{
102 atomic_inc(&ctx->refcount);
103}
104
105static void put_ctx(struct perf_counter_context *ctx)
106{
Paul Mackerras564c2b22009-05-22 14:27:22 +1000107 if (atomic_dec_and_test(&ctx->refcount)) {
108 if (ctx->parent_ctx)
109 put_ctx(ctx->parent_ctx);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000110 kfree(ctx);
Paul Mackerras564c2b22009-05-22 14:27:22 +1000111 }
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000112}
113
Peter Zijlstrafccc7142009-05-23 18:28:56 +0200114/*
115 * Add a counter from the lists for its context.
116 * Must be called with ctx->mutex and ctx->lock held.
117 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100118static void
119list_add_counter(struct perf_counter *counter, struct perf_counter_context *ctx)
120{
121 struct perf_counter *group_leader = counter->group_leader;
122
123 /*
124 * Depending on whether it is a standalone or sibling counter,
125 * add it straight to the context's counter list, or to the group
126 * leader's sibling list:
127 */
Peter Zijlstra3df5eda2009-05-08 18:52:22 +0200128 if (group_leader == counter)
Ingo Molnar04289bb2008-12-11 08:38:42 +0100129 list_add_tail(&counter->list_entry, &ctx->counter_list);
Peter Zijlstra5c148192009-03-25 12:30:23 +0100130 else {
Ingo Molnar04289bb2008-12-11 08:38:42 +0100131 list_add_tail(&counter->list_entry, &group_leader->sibling_list);
Peter Zijlstra5c148192009-03-25 12:30:23 +0100132 group_leader->nr_siblings++;
133 }
Peter Zijlstra592903c2009-03-13 12:21:36 +0100134
135 list_add_rcu(&counter->event_entry, &ctx->event_list);
Peter Zijlstra8bc20952009-05-15 20:45:59 +0200136 ctx->nr_counters++;
Ingo Molnar04289bb2008-12-11 08:38:42 +0100137}
138
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000139/*
140 * Remove a counter from the lists for its context.
Peter Zijlstrafccc7142009-05-23 18:28:56 +0200141 * Must be called with ctx->mutex and ctx->lock held.
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000142 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100143static void
144list_del_counter(struct perf_counter *counter, struct perf_counter_context *ctx)
145{
146 struct perf_counter *sibling, *tmp;
147
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000148 if (list_empty(&counter->list_entry))
149 return;
Peter Zijlstra8bc20952009-05-15 20:45:59 +0200150 ctx->nr_counters--;
151
Ingo Molnar04289bb2008-12-11 08:38:42 +0100152 list_del_init(&counter->list_entry);
Peter Zijlstra592903c2009-03-13 12:21:36 +0100153 list_del_rcu(&counter->event_entry);
Ingo Molnar04289bb2008-12-11 08:38:42 +0100154
Peter Zijlstra5c148192009-03-25 12:30:23 +0100155 if (counter->group_leader != counter)
156 counter->group_leader->nr_siblings--;
157
Ingo Molnar04289bb2008-12-11 08:38:42 +0100158 /*
159 * If this was a group counter with sibling counters then
160 * upgrade the siblings to singleton counters by adding them
161 * to the context list directly:
162 */
163 list_for_each_entry_safe(sibling, tmp,
164 &counter->sibling_list, list_entry) {
165
Peter Zijlstra75564232009-03-13 12:21:29 +0100166 list_move_tail(&sibling->list_entry, &ctx->counter_list);
Ingo Molnar04289bb2008-12-11 08:38:42 +0100167 sibling->group_leader = sibling;
168 }
169}
170
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100171static void
172counter_sched_out(struct perf_counter *counter,
173 struct perf_cpu_context *cpuctx,
174 struct perf_counter_context *ctx)
175{
176 if (counter->state != PERF_COUNTER_STATE_ACTIVE)
177 return;
178
179 counter->state = PERF_COUNTER_STATE_INACTIVE;
Peter Zijlstra4af49982009-04-06 11:45:10 +0200180 counter->tstamp_stopped = ctx->time;
Robert Richter4aeb0b42009-04-29 12:47:03 +0200181 counter->pmu->disable(counter);
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100182 counter->oncpu = -1;
183
184 if (!is_software_counter(counter))
185 cpuctx->active_oncpu--;
186 ctx->nr_active--;
187 if (counter->hw_event.exclusive || !cpuctx->active_oncpu)
188 cpuctx->exclusive = 0;
189}
190
Paul Mackerrasd859e292009-01-17 18:10:22 +1100191static void
192group_sched_out(struct perf_counter *group_counter,
193 struct perf_cpu_context *cpuctx,
194 struct perf_counter_context *ctx)
195{
196 struct perf_counter *counter;
197
198 if (group_counter->state != PERF_COUNTER_STATE_ACTIVE)
199 return;
200
201 counter_sched_out(group_counter, cpuctx, ctx);
202
203 /*
204 * Schedule out siblings (if any):
205 */
206 list_for_each_entry(counter, &group_counter->sibling_list, list_entry)
207 counter_sched_out(counter, cpuctx, ctx);
208
209 if (group_counter->hw_event.exclusive)
210 cpuctx->exclusive = 0;
211}
212
Thomas Gleixner0793a612008-12-04 20:12:29 +0100213/*
Paul Mackerras564c2b22009-05-22 14:27:22 +1000214 * Mark this context as not being a clone of another.
215 * Called when counters are added to or removed from this context.
216 * We also increment our generation number so that anything that
217 * was cloned from this context before this will not match anything
218 * cloned from this context after this.
219 */
220static void unclone_ctx(struct perf_counter_context *ctx)
221{
222 ++ctx->generation;
223 if (!ctx->parent_ctx)
224 return;
225 put_ctx(ctx->parent_ctx);
226 ctx->parent_ctx = NULL;
227}
228
229/*
Thomas Gleixner0793a612008-12-04 20:12:29 +0100230 * Cross CPU call to remove a performance counter
231 *
232 * We disable the counter on the hardware level first. After that we
233 * remove it from the context list.
234 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100235static void __perf_counter_remove_from_context(void *info)
Thomas Gleixner0793a612008-12-04 20:12:29 +0100236{
237 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
238 struct perf_counter *counter = info;
239 struct perf_counter_context *ctx = counter->ctx;
Ingo Molnar9b51f662008-12-12 13:49:45 +0100240 unsigned long flags;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100241
242 /*
243 * If this is a task context, we need to check whether it is
244 * the current task context of this cpu. If not it has been
245 * scheduled out before the smp call arrived.
246 */
247 if (ctx->task && cpuctx->task_ctx != ctx)
248 return;
249
Peter Zijlstra849691a2009-04-06 11:45:12 +0200250 spin_lock_irqsave(&ctx->lock, flags);
Ingo Molnar34adc802009-05-20 20:13:28 +0200251 /*
252 * Protect the list operation against NMI by disabling the
253 * counters on a global level.
254 */
255 perf_disable();
Thomas Gleixner0793a612008-12-04 20:12:29 +0100256
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100257 counter_sched_out(counter, cpuctx, ctx);
258
Ingo Molnar04289bb2008-12-11 08:38:42 +0100259 list_del_counter(counter, ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100260
261 if (!ctx->task) {
262 /*
263 * Allow more per task counters with respect to the
264 * reservation:
265 */
266 cpuctx->max_pertask =
267 min(perf_max_counters - ctx->nr_counters,
268 perf_max_counters - perf_reserved_percpu);
269 }
270
Ingo Molnar34adc802009-05-20 20:13:28 +0200271 perf_enable();
Peter Zijlstra849691a2009-04-06 11:45:12 +0200272 spin_unlock_irqrestore(&ctx->lock, flags);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100273}
274
275
276/*
277 * Remove the counter from a task's (or a CPU's) list of counters.
278 *
Peter Zijlstrafccc7142009-05-23 18:28:56 +0200279 * Must be called with ctx->mutex held.
Thomas Gleixner0793a612008-12-04 20:12:29 +0100280 *
281 * CPU counters are removed with a smp call. For task counters we only
282 * call when the task is on a CPU.
283 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100284static void perf_counter_remove_from_context(struct perf_counter *counter)
Thomas Gleixner0793a612008-12-04 20:12:29 +0100285{
286 struct perf_counter_context *ctx = counter->ctx;
287 struct task_struct *task = ctx->task;
288
Paul Mackerras564c2b22009-05-22 14:27:22 +1000289 unclone_ctx(ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100290 if (!task) {
291 /*
292 * Per cpu counters are removed via an smp call and
293 * the removal is always sucessful.
294 */
295 smp_call_function_single(counter->cpu,
Ingo Molnar04289bb2008-12-11 08:38:42 +0100296 __perf_counter_remove_from_context,
Thomas Gleixner0793a612008-12-04 20:12:29 +0100297 counter, 1);
298 return;
299 }
300
301retry:
Ingo Molnar04289bb2008-12-11 08:38:42 +0100302 task_oncpu_function_call(task, __perf_counter_remove_from_context,
Thomas Gleixner0793a612008-12-04 20:12:29 +0100303 counter);
304
305 spin_lock_irq(&ctx->lock);
306 /*
307 * If the context is active we need to retry the smp call.
308 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100309 if (ctx->nr_active && !list_empty(&counter->list_entry)) {
Thomas Gleixner0793a612008-12-04 20:12:29 +0100310 spin_unlock_irq(&ctx->lock);
311 goto retry;
312 }
313
314 /*
315 * The lock prevents that this context is scheduled in so we
Ingo Molnar04289bb2008-12-11 08:38:42 +0100316 * can remove the counter safely, if the call above did not
Thomas Gleixner0793a612008-12-04 20:12:29 +0100317 * succeed.
318 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100319 if (!list_empty(&counter->list_entry)) {
Ingo Molnar04289bb2008-12-11 08:38:42 +0100320 list_del_counter(counter, ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100321 }
322 spin_unlock_irq(&ctx->lock);
323}
324
Peter Zijlstra4af49982009-04-06 11:45:10 +0200325static inline u64 perf_clock(void)
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100326{
Peter Zijlstra4af49982009-04-06 11:45:10 +0200327 return cpu_clock(smp_processor_id());
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100328}
329
330/*
331 * Update the record of the current time in a context.
332 */
Peter Zijlstra4af49982009-04-06 11:45:10 +0200333static void update_context_time(struct perf_counter_context *ctx)
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100334{
Peter Zijlstra4af49982009-04-06 11:45:10 +0200335 u64 now = perf_clock();
336
337 ctx->time += now - ctx->timestamp;
338 ctx->timestamp = now;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100339}
340
341/*
342 * Update the total_time_enabled and total_time_running fields for a counter.
343 */
344static void update_counter_times(struct perf_counter *counter)
345{
346 struct perf_counter_context *ctx = counter->ctx;
347 u64 run_end;
348
Peter Zijlstra4af49982009-04-06 11:45:10 +0200349 if (counter->state < PERF_COUNTER_STATE_INACTIVE)
350 return;
351
352 counter->total_time_enabled = ctx->time - counter->tstamp_enabled;
353
354 if (counter->state == PERF_COUNTER_STATE_INACTIVE)
355 run_end = counter->tstamp_stopped;
356 else
357 run_end = ctx->time;
358
359 counter->total_time_running = run_end - counter->tstamp_running;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100360}
361
362/*
363 * Update total_time_enabled and total_time_running for all counters in a group.
364 */
365static void update_group_times(struct perf_counter *leader)
366{
367 struct perf_counter *counter;
368
369 update_counter_times(leader);
370 list_for_each_entry(counter, &leader->sibling_list, list_entry)
371 update_counter_times(counter);
372}
373
374/*
Paul Mackerrasd859e292009-01-17 18:10:22 +1100375 * Cross CPU call to disable a performance counter
376 */
377static void __perf_counter_disable(void *info)
378{
379 struct perf_counter *counter = info;
380 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
381 struct perf_counter_context *ctx = counter->ctx;
382 unsigned long flags;
383
384 /*
385 * If this is a per-task counter, need to check whether this
386 * counter's task is the current task on this cpu.
387 */
388 if (ctx->task && cpuctx->task_ctx != ctx)
389 return;
390
Peter Zijlstra849691a2009-04-06 11:45:12 +0200391 spin_lock_irqsave(&ctx->lock, flags);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100392
393 /*
394 * If the counter is on, turn it off.
395 * If it is in error state, leave it in error state.
396 */
397 if (counter->state >= PERF_COUNTER_STATE_INACTIVE) {
Peter Zijlstra4af49982009-04-06 11:45:10 +0200398 update_context_time(ctx);
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100399 update_counter_times(counter);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100400 if (counter == counter->group_leader)
401 group_sched_out(counter, cpuctx, ctx);
402 else
403 counter_sched_out(counter, cpuctx, ctx);
404 counter->state = PERF_COUNTER_STATE_OFF;
405 }
406
Peter Zijlstra849691a2009-04-06 11:45:12 +0200407 spin_unlock_irqrestore(&ctx->lock, flags);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100408}
409
410/*
411 * Disable a counter.
412 */
413static void perf_counter_disable(struct perf_counter *counter)
414{
415 struct perf_counter_context *ctx = counter->ctx;
416 struct task_struct *task = ctx->task;
417
418 if (!task) {
419 /*
420 * Disable the counter on the cpu that it's on
421 */
422 smp_call_function_single(counter->cpu, __perf_counter_disable,
423 counter, 1);
424 return;
425 }
426
427 retry:
428 task_oncpu_function_call(task, __perf_counter_disable, counter);
429
430 spin_lock_irq(&ctx->lock);
431 /*
432 * If the counter is still active, we need to retry the cross-call.
433 */
434 if (counter->state == PERF_COUNTER_STATE_ACTIVE) {
435 spin_unlock_irq(&ctx->lock);
436 goto retry;
437 }
438
439 /*
440 * Since we have the lock this context can't be scheduled
441 * in, so we can change the state safely.
442 */
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100443 if (counter->state == PERF_COUNTER_STATE_INACTIVE) {
444 update_counter_times(counter);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100445 counter->state = PERF_COUNTER_STATE_OFF;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100446 }
Paul Mackerrasd859e292009-01-17 18:10:22 +1100447
448 spin_unlock_irq(&ctx->lock);
449}
450
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100451static int
452counter_sched_in(struct perf_counter *counter,
453 struct perf_cpu_context *cpuctx,
454 struct perf_counter_context *ctx,
455 int cpu)
456{
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100457 if (counter->state <= PERF_COUNTER_STATE_OFF)
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100458 return 0;
459
460 counter->state = PERF_COUNTER_STATE_ACTIVE;
461 counter->oncpu = cpu; /* TODO: put 'cpu' into cpuctx->cpu */
462 /*
463 * The new state must be visible before we turn it on in the hardware:
464 */
465 smp_wmb();
466
Robert Richter4aeb0b42009-04-29 12:47:03 +0200467 if (counter->pmu->enable(counter)) {
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100468 counter->state = PERF_COUNTER_STATE_INACTIVE;
469 counter->oncpu = -1;
470 return -EAGAIN;
471 }
472
Peter Zijlstra4af49982009-04-06 11:45:10 +0200473 counter->tstamp_running += ctx->time - counter->tstamp_stopped;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100474
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100475 if (!is_software_counter(counter))
476 cpuctx->active_oncpu++;
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100477 ctx->nr_active++;
478
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100479 if (counter->hw_event.exclusive)
480 cpuctx->exclusive = 1;
481
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100482 return 0;
483}
484
Paul Mackerras6751b712009-05-11 12:08:02 +1000485static int
486group_sched_in(struct perf_counter *group_counter,
487 struct perf_cpu_context *cpuctx,
488 struct perf_counter_context *ctx,
489 int cpu)
490{
491 struct perf_counter *counter, *partial_group;
492 int ret;
493
494 if (group_counter->state == PERF_COUNTER_STATE_OFF)
495 return 0;
496
497 ret = hw_perf_group_sched_in(group_counter, cpuctx, ctx, cpu);
498 if (ret)
499 return ret < 0 ? ret : 0;
500
501 group_counter->prev_state = group_counter->state;
502 if (counter_sched_in(group_counter, cpuctx, ctx, cpu))
503 return -EAGAIN;
504
505 /*
506 * Schedule in siblings as one group (if any):
507 */
508 list_for_each_entry(counter, &group_counter->sibling_list, list_entry) {
509 counter->prev_state = counter->state;
510 if (counter_sched_in(counter, cpuctx, ctx, cpu)) {
511 partial_group = counter;
512 goto group_error;
513 }
514 }
515
516 return 0;
517
518group_error:
519 /*
520 * Groups can be scheduled in as one unit only, so undo any
521 * partial group before returning:
522 */
523 list_for_each_entry(counter, &group_counter->sibling_list, list_entry) {
524 if (counter == partial_group)
525 break;
526 counter_sched_out(counter, cpuctx, ctx);
527 }
528 counter_sched_out(group_counter, cpuctx, ctx);
529
530 return -EAGAIN;
531}
532
Thomas Gleixner0793a612008-12-04 20:12:29 +0100533/*
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100534 * Return 1 for a group consisting entirely of software counters,
535 * 0 if the group contains any hardware counters.
536 */
537static int is_software_only_group(struct perf_counter *leader)
538{
539 struct perf_counter *counter;
540
541 if (!is_software_counter(leader))
542 return 0;
Peter Zijlstra5c148192009-03-25 12:30:23 +0100543
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100544 list_for_each_entry(counter, &leader->sibling_list, list_entry)
545 if (!is_software_counter(counter))
546 return 0;
Peter Zijlstra5c148192009-03-25 12:30:23 +0100547
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100548 return 1;
549}
550
551/*
552 * Work out whether we can put this counter group on the CPU now.
553 */
554static int group_can_go_on(struct perf_counter *counter,
555 struct perf_cpu_context *cpuctx,
556 int can_add_hw)
557{
558 /*
559 * Groups consisting entirely of software counters can always go on.
560 */
561 if (is_software_only_group(counter))
562 return 1;
563 /*
564 * If an exclusive group is already on, no other hardware
565 * counters can go on.
566 */
567 if (cpuctx->exclusive)
568 return 0;
569 /*
570 * If this group is exclusive and there are already
571 * counters on the CPU, it can't go on.
572 */
573 if (counter->hw_event.exclusive && cpuctx->active_oncpu)
574 return 0;
575 /*
576 * Otherwise, try to add it if all previous groups were able
577 * to go on.
578 */
579 return can_add_hw;
580}
581
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100582static void add_counter_to_ctx(struct perf_counter *counter,
583 struct perf_counter_context *ctx)
584{
585 list_add_counter(counter, ctx);
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100586 counter->prev_state = PERF_COUNTER_STATE_OFF;
Peter Zijlstra4af49982009-04-06 11:45:10 +0200587 counter->tstamp_enabled = ctx->time;
588 counter->tstamp_running = ctx->time;
589 counter->tstamp_stopped = ctx->time;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100590}
591
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100592/*
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100593 * Cross CPU call to install and enable a performance counter
Peter Zijlstra682076a2009-05-23 18:28:57 +0200594 *
595 * Must be called with ctx->mutex held
Thomas Gleixner0793a612008-12-04 20:12:29 +0100596 */
597static void __perf_install_in_context(void *info)
598{
599 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
600 struct perf_counter *counter = info;
601 struct perf_counter_context *ctx = counter->ctx;
Paul Mackerrasd859e292009-01-17 18:10:22 +1100602 struct perf_counter *leader = counter->group_leader;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100603 int cpu = smp_processor_id();
Ingo Molnar9b51f662008-12-12 13:49:45 +0100604 unsigned long flags;
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100605 int err;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100606
607 /*
608 * If this is a task context, we need to check whether it is
609 * the current task context of this cpu. If not it has been
610 * scheduled out before the smp call arrived.
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000611 * Or possibly this is the right context but it isn't
612 * on this cpu because it had no counters.
Thomas Gleixner0793a612008-12-04 20:12:29 +0100613 */
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000614 if (ctx->task && cpuctx->task_ctx != ctx) {
615 if (cpuctx->task_ctx || ctx->task != current)
616 return;
617 cpuctx->task_ctx = ctx;
618 }
Thomas Gleixner0793a612008-12-04 20:12:29 +0100619
Peter Zijlstra849691a2009-04-06 11:45:12 +0200620 spin_lock_irqsave(&ctx->lock, flags);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000621 ctx->is_active = 1;
Peter Zijlstra4af49982009-04-06 11:45:10 +0200622 update_context_time(ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100623
624 /*
625 * Protect the list operation against NMI by disabling the
626 * counters on a global level. NOP for non NMI based counters.
627 */
Peter Zijlstra9e35ad32009-05-13 16:21:38 +0200628 perf_disable();
Thomas Gleixner0793a612008-12-04 20:12:29 +0100629
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100630 add_counter_to_ctx(counter, ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100631
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100632 /*
Paul Mackerrasd859e292009-01-17 18:10:22 +1100633 * Don't put the counter on if it is disabled or if
634 * it is in a group and the group isn't on.
635 */
636 if (counter->state != PERF_COUNTER_STATE_INACTIVE ||
637 (leader != counter && leader->state != PERF_COUNTER_STATE_ACTIVE))
638 goto unlock;
639
640 /*
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100641 * An exclusive counter can't go on if there are already active
642 * hardware counters, and no hardware counter can go on if there
643 * is already an exclusive counter on.
644 */
Paul Mackerrasd859e292009-01-17 18:10:22 +1100645 if (!group_can_go_on(counter, cpuctx, 1))
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100646 err = -EEXIST;
647 else
648 err = counter_sched_in(counter, cpuctx, ctx, cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100649
Paul Mackerrasd859e292009-01-17 18:10:22 +1100650 if (err) {
651 /*
652 * This counter couldn't go on. If it is in a group
653 * then we have to pull the whole group off.
654 * If the counter group is pinned then put it in error state.
655 */
656 if (leader != counter)
657 group_sched_out(leader, cpuctx, ctx);
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100658 if (leader->hw_event.pinned) {
659 update_group_times(leader);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100660 leader->state = PERF_COUNTER_STATE_ERROR;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100661 }
Paul Mackerrasd859e292009-01-17 18:10:22 +1100662 }
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100663
664 if (!err && !ctx->task && cpuctx->max_pertask)
Thomas Gleixner0793a612008-12-04 20:12:29 +0100665 cpuctx->max_pertask--;
666
Paul Mackerrasd859e292009-01-17 18:10:22 +1100667 unlock:
Peter Zijlstra9e35ad32009-05-13 16:21:38 +0200668 perf_enable();
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100669
Peter Zijlstra849691a2009-04-06 11:45:12 +0200670 spin_unlock_irqrestore(&ctx->lock, flags);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100671}
672
673/*
674 * Attach a performance counter to a context
675 *
676 * First we add the counter to the list with the hardware enable bit
677 * in counter->hw_config cleared.
678 *
679 * If the counter is attached to a task which is on a CPU we use a smp
680 * call to enable it in the task context. The task might have been
681 * scheduled away, but we check this in the smp call again.
Paul Mackerrasd859e292009-01-17 18:10:22 +1100682 *
683 * Must be called with ctx->mutex held.
Thomas Gleixner0793a612008-12-04 20:12:29 +0100684 */
685static void
686perf_install_in_context(struct perf_counter_context *ctx,
687 struct perf_counter *counter,
688 int cpu)
689{
690 struct task_struct *task = ctx->task;
691
Thomas Gleixner0793a612008-12-04 20:12:29 +0100692 if (!task) {
693 /*
694 * Per cpu counters are installed via an smp call and
695 * the install is always sucessful.
696 */
697 smp_call_function_single(cpu, __perf_install_in_context,
698 counter, 1);
699 return;
700 }
701
Thomas Gleixner0793a612008-12-04 20:12:29 +0100702retry:
703 task_oncpu_function_call(task, __perf_install_in_context,
704 counter);
705
706 spin_lock_irq(&ctx->lock);
707 /*
Thomas Gleixner0793a612008-12-04 20:12:29 +0100708 * we need to retry the smp call.
709 */
Paul Mackerrasd859e292009-01-17 18:10:22 +1100710 if (ctx->is_active && list_empty(&counter->list_entry)) {
Thomas Gleixner0793a612008-12-04 20:12:29 +0100711 spin_unlock_irq(&ctx->lock);
712 goto retry;
713 }
714
715 /*
716 * The lock prevents that this context is scheduled in so we
717 * can add the counter safely, if it the call above did not
718 * succeed.
719 */
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100720 if (list_empty(&counter->list_entry))
721 add_counter_to_ctx(counter, ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100722 spin_unlock_irq(&ctx->lock);
723}
724
Paul Mackerrasd859e292009-01-17 18:10:22 +1100725/*
726 * Cross CPU call to enable a performance counter
727 */
728static void __perf_counter_enable(void *info)
Ingo Molnar04289bb2008-12-11 08:38:42 +0100729{
Paul Mackerrasd859e292009-01-17 18:10:22 +1100730 struct perf_counter *counter = info;
731 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
732 struct perf_counter_context *ctx = counter->ctx;
733 struct perf_counter *leader = counter->group_leader;
734 unsigned long flags;
735 int err;
Ingo Molnar04289bb2008-12-11 08:38:42 +0100736
737 /*
Paul Mackerrasd859e292009-01-17 18:10:22 +1100738 * If this is a per-task counter, need to check whether this
739 * counter's task is the current task on this cpu.
Ingo Molnar04289bb2008-12-11 08:38:42 +0100740 */
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000741 if (ctx->task && cpuctx->task_ctx != ctx) {
742 if (cpuctx->task_ctx || ctx->task != current)
743 return;
744 cpuctx->task_ctx = ctx;
745 }
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100746
Peter Zijlstra849691a2009-04-06 11:45:12 +0200747 spin_lock_irqsave(&ctx->lock, flags);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000748 ctx->is_active = 1;
Peter Zijlstra4af49982009-04-06 11:45:10 +0200749 update_context_time(ctx);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100750
Paul Mackerrasc07c99b2009-02-13 22:10:34 +1100751 counter->prev_state = counter->state;
Paul Mackerrasd859e292009-01-17 18:10:22 +1100752 if (counter->state >= PERF_COUNTER_STATE_INACTIVE)
753 goto unlock;
754 counter->state = PERF_COUNTER_STATE_INACTIVE;
Peter Zijlstra4af49982009-04-06 11:45:10 +0200755 counter->tstamp_enabled = ctx->time - counter->total_time_enabled;
Paul Mackerrasd859e292009-01-17 18:10:22 +1100756
757 /*
758 * If the counter is in a group and isn't the group leader,
759 * then don't put it on unless the group is on.
760 */
761 if (leader != counter && leader->state != PERF_COUNTER_STATE_ACTIVE)
762 goto unlock;
763
Paul Mackerrase758a332009-05-12 21:59:01 +1000764 if (!group_can_go_on(counter, cpuctx, 1)) {
Paul Mackerrasd859e292009-01-17 18:10:22 +1100765 err = -EEXIST;
Paul Mackerrase758a332009-05-12 21:59:01 +1000766 } else {
Peter Zijlstra9e35ad32009-05-13 16:21:38 +0200767 perf_disable();
Paul Mackerrase758a332009-05-12 21:59:01 +1000768 if (counter == leader)
769 err = group_sched_in(counter, cpuctx, ctx,
770 smp_processor_id());
771 else
772 err = counter_sched_in(counter, cpuctx, ctx,
773 smp_processor_id());
Peter Zijlstra9e35ad32009-05-13 16:21:38 +0200774 perf_enable();
Paul Mackerrase758a332009-05-12 21:59:01 +1000775 }
Paul Mackerrasd859e292009-01-17 18:10:22 +1100776
777 if (err) {
778 /*
779 * If this counter can't go on and it's part of a
780 * group, then the whole group has to come off.
781 */
782 if (leader != counter)
783 group_sched_out(leader, cpuctx, ctx);
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100784 if (leader->hw_event.pinned) {
785 update_group_times(leader);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100786 leader->state = PERF_COUNTER_STATE_ERROR;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100787 }
Paul Mackerrasd859e292009-01-17 18:10:22 +1100788 }
789
790 unlock:
Peter Zijlstra849691a2009-04-06 11:45:12 +0200791 spin_unlock_irqrestore(&ctx->lock, flags);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100792}
793
794/*
795 * Enable a counter.
796 */
797static void perf_counter_enable(struct perf_counter *counter)
798{
799 struct perf_counter_context *ctx = counter->ctx;
800 struct task_struct *task = ctx->task;
801
802 if (!task) {
803 /*
804 * Enable the counter on the cpu that it's on
805 */
806 smp_call_function_single(counter->cpu, __perf_counter_enable,
807 counter, 1);
808 return;
809 }
810
811 spin_lock_irq(&ctx->lock);
812 if (counter->state >= PERF_COUNTER_STATE_INACTIVE)
813 goto out;
814
815 /*
816 * If the counter is in error state, clear that first.
817 * That way, if we see the counter in error state below, we
818 * know that it has gone back into error state, as distinct
819 * from the task having been scheduled away before the
820 * cross-call arrived.
821 */
822 if (counter->state == PERF_COUNTER_STATE_ERROR)
823 counter->state = PERF_COUNTER_STATE_OFF;
824
825 retry:
826 spin_unlock_irq(&ctx->lock);
827 task_oncpu_function_call(task, __perf_counter_enable, counter);
828
829 spin_lock_irq(&ctx->lock);
830
831 /*
832 * If the context is active and the counter is still off,
833 * we need to retry the cross-call.
834 */
835 if (ctx->is_active && counter->state == PERF_COUNTER_STATE_OFF)
836 goto retry;
837
838 /*
839 * Since we have the lock this context can't be scheduled
840 * in, so we can change the state safely.
841 */
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100842 if (counter->state == PERF_COUNTER_STATE_OFF) {
Paul Mackerrasd859e292009-01-17 18:10:22 +1100843 counter->state = PERF_COUNTER_STATE_INACTIVE;
Peter Zijlstra4af49982009-04-06 11:45:10 +0200844 counter->tstamp_enabled =
845 ctx->time - counter->total_time_enabled;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100846 }
Paul Mackerrasd859e292009-01-17 18:10:22 +1100847 out:
848 spin_unlock_irq(&ctx->lock);
849}
850
Peter Zijlstra2023b352009-05-05 17:50:26 +0200851static int perf_counter_refresh(struct perf_counter *counter, int refresh)
Peter Zijlstra79f14642009-04-06 11:45:07 +0200852{
Peter Zijlstra2023b352009-05-05 17:50:26 +0200853 /*
854 * not supported on inherited counters
855 */
856 if (counter->hw_event.inherit)
857 return -EINVAL;
858
Peter Zijlstra79f14642009-04-06 11:45:07 +0200859 atomic_add(refresh, &counter->event_limit);
860 perf_counter_enable(counter);
Peter Zijlstra2023b352009-05-05 17:50:26 +0200861
862 return 0;
Peter Zijlstra79f14642009-04-06 11:45:07 +0200863}
864
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100865void __perf_counter_sched_out(struct perf_counter_context *ctx,
866 struct perf_cpu_context *cpuctx)
867{
868 struct perf_counter *counter;
869
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100870 spin_lock(&ctx->lock);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100871 ctx->is_active = 0;
872 if (likely(!ctx->nr_counters))
873 goto out;
Peter Zijlstra4af49982009-04-06 11:45:10 +0200874 update_context_time(ctx);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100875
Peter Zijlstra9e35ad32009-05-13 16:21:38 +0200876 perf_disable();
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100877 if (ctx->nr_active) {
Peter Zijlstraafedadf2009-05-20 12:21:22 +0200878 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
879 if (counter != counter->group_leader)
880 counter_sched_out(counter, cpuctx, ctx);
881 else
882 group_sched_out(counter, cpuctx, ctx);
883 }
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100884 }
Peter Zijlstra9e35ad32009-05-13 16:21:38 +0200885 perf_enable();
Paul Mackerrasd859e292009-01-17 18:10:22 +1100886 out:
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100887 spin_unlock(&ctx->lock);
888}
889
Thomas Gleixner0793a612008-12-04 20:12:29 +0100890/*
Paul Mackerras564c2b22009-05-22 14:27:22 +1000891 * Test whether two contexts are equivalent, i.e. whether they
892 * have both been cloned from the same version of the same context
893 * and they both have the same number of enabled counters.
894 * If the number of enabled counters is the same, then the set
895 * of enabled counters should be the same, because these are both
896 * inherited contexts, therefore we can't access individual counters
897 * in them directly with an fd; we can only enable/disable all
898 * counters via prctl, or enable/disable all counters in a family
899 * via ioctl, which will have the same effect on both contexts.
900 */
901static int context_equiv(struct perf_counter_context *ctx1,
902 struct perf_counter_context *ctx2)
903{
904 return ctx1->parent_ctx && ctx1->parent_ctx == ctx2->parent_ctx
Peter Zijlstra475c5572009-05-23 18:29:01 +0200905 && ctx1->parent_gen == ctx2->parent_gen;
Paul Mackerras564c2b22009-05-22 14:27:22 +1000906}
907
908/*
Thomas Gleixner0793a612008-12-04 20:12:29 +0100909 * Called from scheduler to remove the counters of the current task,
910 * with interrupts disabled.
911 *
912 * We stop each counter and update the counter value in counter->count.
913 *
Ingo Molnar76715812008-12-17 14:20:28 +0100914 * This does not protect us against NMI, but disable()
Thomas Gleixner0793a612008-12-04 20:12:29 +0100915 * sets the disabled bit in the control field of counter _before_
916 * accessing the counter control register. If a NMI hits, then it will
917 * not restart the counter.
918 */
Paul Mackerras564c2b22009-05-22 14:27:22 +1000919void perf_counter_task_sched_out(struct task_struct *task,
920 struct task_struct *next, int cpu)
Thomas Gleixner0793a612008-12-04 20:12:29 +0100921{
922 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000923 struct perf_counter_context *ctx = task->perf_counter_ctxp;
Paul Mackerras564c2b22009-05-22 14:27:22 +1000924 struct perf_counter_context *next_ctx;
Peter Zijlstra4a0deca2009-03-19 20:26:12 +0100925 struct pt_regs *regs;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100926
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000927 if (likely(!ctx || !cpuctx->task_ctx))
Thomas Gleixner0793a612008-12-04 20:12:29 +0100928 return;
929
Peter Zijlstrabce379b2009-04-06 11:45:13 +0200930 update_context_time(ctx);
931
Peter Zijlstra4a0deca2009-03-19 20:26:12 +0100932 regs = task_pt_regs(task);
Peter Zijlstra78f13e92009-04-08 15:01:33 +0200933 perf_swcounter_event(PERF_COUNT_CONTEXT_SWITCHES, 1, 1, regs, 0);
Paul Mackerras564c2b22009-05-22 14:27:22 +1000934
935 next_ctx = next->perf_counter_ctxp;
936 if (next_ctx && context_equiv(ctx, next_ctx)) {
937 task->perf_counter_ctxp = next_ctx;
938 next->perf_counter_ctxp = ctx;
939 ctx->task = next;
940 next_ctx->task = task;
941 return;
942 }
943
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100944 __perf_counter_sched_out(ctx, cpuctx);
945
Thomas Gleixner0793a612008-12-04 20:12:29 +0100946 cpuctx->task_ctx = NULL;
947}
948
Paul Mackerrasa08b1592009-05-11 15:46:10 +1000949static void __perf_counter_task_sched_out(struct perf_counter_context *ctx)
950{
951 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
952
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000953 if (!cpuctx->task_ctx)
954 return;
Paul Mackerrasa08b1592009-05-11 15:46:10 +1000955 __perf_counter_sched_out(ctx, cpuctx);
956 cpuctx->task_ctx = NULL;
957}
958
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100959static void perf_counter_cpu_sched_out(struct perf_cpu_context *cpuctx)
Ingo Molnar04289bb2008-12-11 08:38:42 +0100960{
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100961 __perf_counter_sched_out(&cpuctx->ctx, cpuctx);
Ingo Molnar04289bb2008-12-11 08:38:42 +0100962}
963
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100964static void
965__perf_counter_sched_in(struct perf_counter_context *ctx,
966 struct perf_cpu_context *cpuctx, int cpu)
Thomas Gleixner0793a612008-12-04 20:12:29 +0100967{
Thomas Gleixner0793a612008-12-04 20:12:29 +0100968 struct perf_counter *counter;
Paul Mackerrasdd0e6ba2009-01-12 15:11:00 +1100969 int can_add_hw = 1;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100970
Thomas Gleixner0793a612008-12-04 20:12:29 +0100971 spin_lock(&ctx->lock);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100972 ctx->is_active = 1;
973 if (likely(!ctx->nr_counters))
974 goto out;
975
Peter Zijlstra4af49982009-04-06 11:45:10 +0200976 ctx->timestamp = perf_clock();
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100977
Peter Zijlstra9e35ad32009-05-13 16:21:38 +0200978 perf_disable();
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100979
980 /*
981 * First go through the list and put on any pinned groups
982 * in order to give them the best chance of going on.
983 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100984 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100985 if (counter->state <= PERF_COUNTER_STATE_OFF ||
986 !counter->hw_event.pinned)
987 continue;
988 if (counter->cpu != -1 && counter->cpu != cpu)
989 continue;
990
Peter Zijlstraafedadf2009-05-20 12:21:22 +0200991 if (counter != counter->group_leader)
992 counter_sched_in(counter, cpuctx, ctx, cpu);
993 else {
994 if (group_can_go_on(counter, cpuctx, 1))
995 group_sched_in(counter, cpuctx, ctx, cpu);
996 }
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100997
998 /*
999 * If this pinned group hasn't been scheduled,
1000 * put it in error state.
1001 */
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001002 if (counter->state == PERF_COUNTER_STATE_INACTIVE) {
1003 update_group_times(counter);
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001004 counter->state = PERF_COUNTER_STATE_ERROR;
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001005 }
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001006 }
1007
1008 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
1009 /*
1010 * Ignore counters in OFF or ERROR state, and
1011 * ignore pinned counters since we did them already.
1012 */
1013 if (counter->state <= PERF_COUNTER_STATE_OFF ||
1014 counter->hw_event.pinned)
1015 continue;
1016
Ingo Molnar04289bb2008-12-11 08:38:42 +01001017 /*
1018 * Listen to the 'cpu' scheduling filter constraint
1019 * of counters:
1020 */
Thomas Gleixner0793a612008-12-04 20:12:29 +01001021 if (counter->cpu != -1 && counter->cpu != cpu)
1022 continue;
1023
Peter Zijlstraafedadf2009-05-20 12:21:22 +02001024 if (counter != counter->group_leader) {
1025 if (counter_sched_in(counter, cpuctx, ctx, cpu))
Paul Mackerrasdd0e6ba2009-01-12 15:11:00 +11001026 can_add_hw = 0;
Peter Zijlstraafedadf2009-05-20 12:21:22 +02001027 } else {
1028 if (group_can_go_on(counter, cpuctx, can_add_hw)) {
1029 if (group_sched_in(counter, cpuctx, ctx, cpu))
1030 can_add_hw = 0;
1031 }
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001032 }
Thomas Gleixner0793a612008-12-04 20:12:29 +01001033 }
Peter Zijlstra9e35ad32009-05-13 16:21:38 +02001034 perf_enable();
Paul Mackerrasd859e292009-01-17 18:10:22 +11001035 out:
Thomas Gleixner0793a612008-12-04 20:12:29 +01001036 spin_unlock(&ctx->lock);
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001037}
Ingo Molnar04289bb2008-12-11 08:38:42 +01001038
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001039/*
1040 * Called from scheduler to add the counters of the current task
1041 * with interrupts disabled.
1042 *
1043 * We restore the counter value and then enable it.
1044 *
1045 * This does not protect us against NMI, but enable()
1046 * sets the enabled bit in the control field of counter _before_
1047 * accessing the counter control register. If a NMI hits, then it will
1048 * keep the counter running.
1049 */
1050void perf_counter_task_sched_in(struct task_struct *task, int cpu)
1051{
1052 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001053 struct perf_counter_context *ctx = task->perf_counter_ctxp;
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001054
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001055 if (likely(!ctx))
1056 return;
Paul Mackerras564c2b22009-05-22 14:27:22 +10001057 if (cpuctx->task_ctx == ctx)
1058 return;
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001059 __perf_counter_sched_in(ctx, cpuctx, cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001060 cpuctx->task_ctx = ctx;
1061}
1062
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001063static void perf_counter_cpu_sched_in(struct perf_cpu_context *cpuctx, int cpu)
1064{
1065 struct perf_counter_context *ctx = &cpuctx->ctx;
1066
1067 __perf_counter_sched_in(ctx, cpuctx, cpu);
1068}
1069
Peter Zijlstra26b119b2009-05-20 12:21:20 +02001070static void perf_log_period(struct perf_counter *counter, u64 period);
1071
1072static void perf_adjust_freq(struct perf_counter_context *ctx)
Peter Zijlstra60db5e02009-05-15 15:19:28 +02001073{
1074 struct perf_counter *counter;
1075 u64 irq_period;
1076 u64 events, period;
1077 s64 delta;
1078
1079 spin_lock(&ctx->lock);
1080 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
1081 if (counter->state != PERF_COUNTER_STATE_ACTIVE)
1082 continue;
1083
1084 if (!counter->hw_event.freq || !counter->hw_event.irq_freq)
1085 continue;
1086
1087 events = HZ * counter->hw.interrupts * counter->hw.irq_period;
1088 period = div64_u64(events, counter->hw_event.irq_freq);
1089
1090 delta = (s64)(1 + period - counter->hw.irq_period);
1091 delta >>= 1;
1092
1093 irq_period = counter->hw.irq_period + delta;
1094
1095 if (!irq_period)
1096 irq_period = 1;
1097
Peter Zijlstra26b119b2009-05-20 12:21:20 +02001098 perf_log_period(counter, irq_period);
1099
Peter Zijlstra60db5e02009-05-15 15:19:28 +02001100 counter->hw.irq_period = irq_period;
1101 counter->hw.interrupts = 0;
1102 }
1103 spin_unlock(&ctx->lock);
1104}
1105
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001106/*
1107 * Round-robin a context's counters:
1108 */
1109static void rotate_ctx(struct perf_counter_context *ctx)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001110{
Thomas Gleixner0793a612008-12-04 20:12:29 +01001111 struct perf_counter *counter;
1112
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001113 if (!ctx->nr_counters)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001114 return;
1115
Thomas Gleixner0793a612008-12-04 20:12:29 +01001116 spin_lock(&ctx->lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001117 /*
Ingo Molnar04289bb2008-12-11 08:38:42 +01001118 * Rotate the first entry last (works just fine for group counters too):
Thomas Gleixner0793a612008-12-04 20:12:29 +01001119 */
Peter Zijlstra9e35ad32009-05-13 16:21:38 +02001120 perf_disable();
Ingo Molnar04289bb2008-12-11 08:38:42 +01001121 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
Peter Zijlstra75564232009-03-13 12:21:29 +01001122 list_move_tail(&counter->list_entry, &ctx->counter_list);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001123 break;
1124 }
Peter Zijlstra9e35ad32009-05-13 16:21:38 +02001125 perf_enable();
Thomas Gleixner0793a612008-12-04 20:12:29 +01001126
1127 spin_unlock(&ctx->lock);
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001128}
Thomas Gleixner0793a612008-12-04 20:12:29 +01001129
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001130void perf_counter_task_tick(struct task_struct *curr, int cpu)
1131{
Peter Zijlstra7fc23a52009-05-08 18:52:21 +02001132 struct perf_cpu_context *cpuctx;
1133 struct perf_counter_context *ctx;
1134
1135 if (!atomic_read(&nr_counters))
1136 return;
1137
1138 cpuctx = &per_cpu(perf_cpu_context, cpu);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001139 ctx = curr->perf_counter_ctxp;
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001140
Peter Zijlstra60db5e02009-05-15 15:19:28 +02001141 perf_adjust_freq(&cpuctx->ctx);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001142 if (ctx)
1143 perf_adjust_freq(ctx);
Peter Zijlstra60db5e02009-05-15 15:19:28 +02001144
Ingo Molnarb82914c2009-05-04 18:54:32 +02001145 perf_counter_cpu_sched_out(cpuctx);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001146 if (ctx)
1147 __perf_counter_task_sched_out(ctx);
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001148
Ingo Molnarb82914c2009-05-04 18:54:32 +02001149 rotate_ctx(&cpuctx->ctx);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001150 if (ctx)
1151 rotate_ctx(ctx);
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001152
Ingo Molnarb82914c2009-05-04 18:54:32 +02001153 perf_counter_cpu_sched_in(cpuctx, cpu);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001154 if (ctx)
1155 perf_counter_task_sched_in(curr, cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001156}
1157
1158/*
Thomas Gleixner0793a612008-12-04 20:12:29 +01001159 * Cross CPU call to read the hardware counter
1160 */
Ingo Molnar76715812008-12-17 14:20:28 +01001161static void __read(void *info)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001162{
Ingo Molnar621a01e2008-12-11 12:46:46 +01001163 struct perf_counter *counter = info;
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001164 struct perf_counter_context *ctx = counter->ctx;
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01001165 unsigned long flags;
Ingo Molnar621a01e2008-12-11 12:46:46 +01001166
Peter Zijlstra849691a2009-04-06 11:45:12 +02001167 local_irq_save(flags);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001168 if (ctx->is_active)
Peter Zijlstra4af49982009-04-06 11:45:10 +02001169 update_context_time(ctx);
Robert Richter4aeb0b42009-04-29 12:47:03 +02001170 counter->pmu->read(counter);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001171 update_counter_times(counter);
Peter Zijlstra849691a2009-04-06 11:45:12 +02001172 local_irq_restore(flags);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001173}
1174
Ingo Molnar04289bb2008-12-11 08:38:42 +01001175static u64 perf_counter_read(struct perf_counter *counter)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001176{
1177 /*
1178 * If counter is enabled and currently active on a CPU, update the
1179 * value in the counter structure:
1180 */
Ingo Molnar6a930702008-12-11 15:17:03 +01001181 if (counter->state == PERF_COUNTER_STATE_ACTIVE) {
Thomas Gleixner0793a612008-12-04 20:12:29 +01001182 smp_call_function_single(counter->oncpu,
Ingo Molnar76715812008-12-17 14:20:28 +01001183 __read, counter, 1);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001184 } else if (counter->state == PERF_COUNTER_STATE_INACTIVE) {
1185 update_counter_times(counter);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001186 }
1187
Ingo Molnaree060942008-12-13 09:00:03 +01001188 return atomic64_read(&counter->count);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001189}
1190
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001191/*
1192 * Initialize the perf_counter context in a task_struct:
1193 */
1194static void
1195__perf_counter_init_context(struct perf_counter_context *ctx,
1196 struct task_struct *task)
1197{
1198 memset(ctx, 0, sizeof(*ctx));
1199 spin_lock_init(&ctx->lock);
1200 mutex_init(&ctx->mutex);
1201 INIT_LIST_HEAD(&ctx->counter_list);
1202 INIT_LIST_HEAD(&ctx->event_list);
1203 atomic_set(&ctx->refcount, 1);
1204 ctx->task = task;
1205}
1206
Thomas Gleixner0793a612008-12-04 20:12:29 +01001207static void put_context(struct perf_counter_context *ctx)
1208{
1209 if (ctx->task)
1210 put_task_struct(ctx->task);
1211}
1212
1213static struct perf_counter_context *find_get_context(pid_t pid, int cpu)
1214{
1215 struct perf_cpu_context *cpuctx;
1216 struct perf_counter_context *ctx;
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001217 struct perf_counter_context *tctx;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001218 struct task_struct *task;
1219
1220 /*
1221 * If cpu is not a wildcard then this is a percpu counter:
1222 */
1223 if (cpu != -1) {
1224 /* Must be root to operate on a CPU counter: */
Peter Zijlstra1ccd1542009-04-09 10:53:45 +02001225 if (sysctl_perf_counter_priv && !capable(CAP_SYS_ADMIN))
Thomas Gleixner0793a612008-12-04 20:12:29 +01001226 return ERR_PTR(-EACCES);
1227
1228 if (cpu < 0 || cpu > num_possible_cpus())
1229 return ERR_PTR(-EINVAL);
1230
1231 /*
1232 * We could be clever and allow to attach a counter to an
1233 * offline CPU and activate it when the CPU comes up, but
1234 * that's for later.
1235 */
1236 if (!cpu_isset(cpu, cpu_online_map))
1237 return ERR_PTR(-ENODEV);
1238
1239 cpuctx = &per_cpu(perf_cpu_context, cpu);
1240 ctx = &cpuctx->ctx;
1241
Thomas Gleixner0793a612008-12-04 20:12:29 +01001242 return ctx;
1243 }
1244
1245 rcu_read_lock();
1246 if (!pid)
1247 task = current;
1248 else
1249 task = find_task_by_vpid(pid);
1250 if (task)
1251 get_task_struct(task);
1252 rcu_read_unlock();
1253
1254 if (!task)
1255 return ERR_PTR(-ESRCH);
1256
Thomas Gleixner0793a612008-12-04 20:12:29 +01001257 /* Reuse ptrace permission checks for now. */
1258 if (!ptrace_may_access(task, PTRACE_MODE_READ)) {
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001259 put_task_struct(task);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001260 return ERR_PTR(-EACCES);
1261 }
1262
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001263 ctx = task->perf_counter_ctxp;
1264 if (!ctx) {
1265 ctx = kmalloc(sizeof(struct perf_counter_context), GFP_KERNEL);
1266 if (!ctx) {
1267 put_task_struct(task);
1268 return ERR_PTR(-ENOMEM);
1269 }
1270 __perf_counter_init_context(ctx, task);
1271 /*
1272 * Make sure other cpus see correct values for *ctx
1273 * once task->perf_counter_ctxp is visible to them.
1274 */
1275 smp_wmb();
1276 tctx = cmpxchg(&task->perf_counter_ctxp, NULL, ctx);
1277 if (tctx) {
1278 /*
1279 * We raced with some other task; use
1280 * the context they set.
1281 */
1282 kfree(ctx);
1283 ctx = tctx;
1284 }
1285 }
1286
Thomas Gleixner0793a612008-12-04 20:12:29 +01001287 return ctx;
1288}
1289
Peter Zijlstra592903c2009-03-13 12:21:36 +01001290static void free_counter_rcu(struct rcu_head *head)
1291{
1292 struct perf_counter *counter;
1293
1294 counter = container_of(head, struct perf_counter, rcu_head);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001295 put_ctx(counter->ctx);
Peter Zijlstra592903c2009-03-13 12:21:36 +01001296 kfree(counter);
1297}
1298
Peter Zijlstra925d5192009-03-30 19:07:02 +02001299static void perf_pending_sync(struct perf_counter *counter);
1300
Peter Zijlstraf1600952009-03-19 20:26:16 +01001301static void free_counter(struct perf_counter *counter)
1302{
Peter Zijlstra925d5192009-03-30 19:07:02 +02001303 perf_pending_sync(counter);
1304
Peter Zijlstra7fc23a52009-05-08 18:52:21 +02001305 atomic_dec(&nr_counters);
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02001306 if (counter->hw_event.mmap)
1307 atomic_dec(&nr_mmap_tracking);
1308 if (counter->hw_event.munmap)
1309 atomic_dec(&nr_munmap_tracking);
1310 if (counter->hw_event.comm)
1311 atomic_dec(&nr_comm_tracking);
1312
Peter Zijlstrae077df42009-03-19 20:26:17 +01001313 if (counter->destroy)
1314 counter->destroy(counter);
1315
Peter Zijlstraf1600952009-03-19 20:26:16 +01001316 call_rcu(&counter->rcu_head, free_counter_rcu);
1317}
1318
Thomas Gleixner0793a612008-12-04 20:12:29 +01001319/*
1320 * Called when the last reference to the file is gone.
1321 */
1322static int perf_release(struct inode *inode, struct file *file)
1323{
1324 struct perf_counter *counter = file->private_data;
1325 struct perf_counter_context *ctx = counter->ctx;
1326
1327 file->private_data = NULL;
1328
Paul Mackerrasd859e292009-01-17 18:10:22 +11001329 mutex_lock(&ctx->mutex);
Ingo Molnar04289bb2008-12-11 08:38:42 +01001330 perf_counter_remove_from_context(counter);
Paul Mackerrasd859e292009-01-17 18:10:22 +11001331 mutex_unlock(&ctx->mutex);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001332
Peter Zijlstra082ff5a2009-05-23 18:29:00 +02001333 mutex_lock(&counter->owner->perf_counter_mutex);
1334 list_del_init(&counter->owner_entry);
1335 mutex_unlock(&counter->owner->perf_counter_mutex);
1336 put_task_struct(counter->owner);
1337
Peter Zijlstraf1600952009-03-19 20:26:16 +01001338 free_counter(counter);
Mike Galbraith5af75912009-02-11 10:53:37 +01001339 put_context(ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001340
1341 return 0;
1342}
1343
1344/*
1345 * Read the performance counter - simple non blocking version for now
1346 */
1347static ssize_t
1348perf_read_hw(struct perf_counter *counter, char __user *buf, size_t count)
1349{
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001350 u64 values[3];
1351 int n;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001352
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001353 /*
1354 * Return end-of-file for a read on a counter that is in
1355 * error state (i.e. because it was pinned but it couldn't be
1356 * scheduled on to the CPU at some point).
1357 */
1358 if (counter->state == PERF_COUNTER_STATE_ERROR)
1359 return 0;
1360
Peter Zijlstrafccc7142009-05-23 18:28:56 +02001361 mutex_lock(&counter->child_mutex);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001362 values[0] = perf_counter_read(counter);
1363 n = 1;
1364 if (counter->hw_event.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
1365 values[n++] = counter->total_time_enabled +
1366 atomic64_read(&counter->child_total_time_enabled);
1367 if (counter->hw_event.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
1368 values[n++] = counter->total_time_running +
1369 atomic64_read(&counter->child_total_time_running);
Peter Zijlstrafccc7142009-05-23 18:28:56 +02001370 mutex_unlock(&counter->child_mutex);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001371
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001372 if (count < n * sizeof(u64))
1373 return -EINVAL;
1374 count = n * sizeof(u64);
1375
1376 if (copy_to_user(buf, values, count))
1377 return -EFAULT;
1378
1379 return count;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001380}
1381
1382static ssize_t
Thomas Gleixner0793a612008-12-04 20:12:29 +01001383perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
1384{
1385 struct perf_counter *counter = file->private_data;
1386
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001387 return perf_read_hw(counter, buf, count);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001388}
1389
1390static unsigned int perf_poll(struct file *file, poll_table *wait)
1391{
1392 struct perf_counter *counter = file->private_data;
Peter Zijlstrac7138f32009-03-24 13:18:16 +01001393 struct perf_mmap_data *data;
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001394 unsigned int events = POLL_HUP;
Peter Zijlstrac7138f32009-03-24 13:18:16 +01001395
1396 rcu_read_lock();
1397 data = rcu_dereference(counter->data);
1398 if (data)
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001399 events = atomic_xchg(&data->poll, 0);
Peter Zijlstrac7138f32009-03-24 13:18:16 +01001400 rcu_read_unlock();
Thomas Gleixner0793a612008-12-04 20:12:29 +01001401
1402 poll_wait(file, &counter->waitq, wait);
1403
Thomas Gleixner0793a612008-12-04 20:12:29 +01001404 return events;
1405}
1406
Peter Zijlstra6de6a7b2009-05-05 17:50:23 +02001407static void perf_counter_reset(struct perf_counter *counter)
1408{
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001409 (void)perf_counter_read(counter);
Paul Mackerras615a3f12009-05-11 15:50:21 +10001410 atomic64_set(&counter->count, 0);
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001411 perf_counter_update_userpage(counter);
1412}
1413
1414static void perf_counter_for_each_sibling(struct perf_counter *counter,
1415 void (*func)(struct perf_counter *))
1416{
1417 struct perf_counter_context *ctx = counter->ctx;
1418 struct perf_counter *sibling;
1419
Peter Zijlstra682076a2009-05-23 18:28:57 +02001420 mutex_lock(&ctx->mutex);
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001421 counter = counter->group_leader;
1422
1423 func(counter);
1424 list_for_each_entry(sibling, &counter->sibling_list, list_entry)
1425 func(sibling);
Peter Zijlstra682076a2009-05-23 18:28:57 +02001426 mutex_unlock(&ctx->mutex);
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001427}
1428
1429static void perf_counter_for_each_child(struct perf_counter *counter,
1430 void (*func)(struct perf_counter *))
1431{
1432 struct perf_counter *child;
1433
Peter Zijlstrafccc7142009-05-23 18:28:56 +02001434 mutex_lock(&counter->child_mutex);
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001435 func(counter);
1436 list_for_each_entry(child, &counter->child_list, child_list)
1437 func(child);
Peter Zijlstrafccc7142009-05-23 18:28:56 +02001438 mutex_unlock(&counter->child_mutex);
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001439}
1440
1441static void perf_counter_for_each(struct perf_counter *counter,
1442 void (*func)(struct perf_counter *))
1443{
1444 struct perf_counter *child;
1445
Peter Zijlstrafccc7142009-05-23 18:28:56 +02001446 mutex_lock(&counter->child_mutex);
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001447 perf_counter_for_each_sibling(counter, func);
1448 list_for_each_entry(child, &counter->child_list, child_list)
1449 perf_counter_for_each_sibling(child, func);
Peter Zijlstrafccc7142009-05-23 18:28:56 +02001450 mutex_unlock(&counter->child_mutex);
Peter Zijlstra6de6a7b2009-05-05 17:50:23 +02001451}
1452
Paul Mackerrasd859e292009-01-17 18:10:22 +11001453static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1454{
1455 struct perf_counter *counter = file->private_data;
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001456 void (*func)(struct perf_counter *);
1457 u32 flags = arg;
Paul Mackerrasd859e292009-01-17 18:10:22 +11001458
1459 switch (cmd) {
1460 case PERF_COUNTER_IOC_ENABLE:
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001461 func = perf_counter_enable;
Paul Mackerrasd859e292009-01-17 18:10:22 +11001462 break;
1463 case PERF_COUNTER_IOC_DISABLE:
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001464 func = perf_counter_disable;
Peter Zijlstra79f14642009-04-06 11:45:07 +02001465 break;
Peter Zijlstra6de6a7b2009-05-05 17:50:23 +02001466 case PERF_COUNTER_IOC_RESET:
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001467 func = perf_counter_reset;
Peter Zijlstra6de6a7b2009-05-05 17:50:23 +02001468 break;
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001469
1470 case PERF_COUNTER_IOC_REFRESH:
1471 return perf_counter_refresh(counter, arg);
Paul Mackerrasd859e292009-01-17 18:10:22 +11001472 default:
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001473 return -ENOTTY;
Paul Mackerrasd859e292009-01-17 18:10:22 +11001474 }
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001475
1476 if (flags & PERF_IOC_FLAG_GROUP)
1477 perf_counter_for_each(counter, func);
1478 else
1479 perf_counter_for_each_child(counter, func);
1480
1481 return 0;
Paul Mackerrasd859e292009-01-17 18:10:22 +11001482}
1483
Peter Zijlstra771d7cd2009-05-25 14:45:26 +02001484int perf_counter_task_enable(void)
1485{
1486 struct perf_counter *counter;
1487
1488 mutex_lock(&current->perf_counter_mutex);
1489 list_for_each_entry(counter, &current->perf_counter_list, owner_entry)
1490 perf_counter_for_each_child(counter, perf_counter_enable);
1491 mutex_unlock(&current->perf_counter_mutex);
1492
1493 return 0;
1494}
1495
1496int perf_counter_task_disable(void)
1497{
1498 struct perf_counter *counter;
1499
1500 mutex_lock(&current->perf_counter_mutex);
1501 list_for_each_entry(counter, &current->perf_counter_list, owner_entry)
1502 perf_counter_for_each_child(counter, perf_counter_disable);
1503 mutex_unlock(&current->perf_counter_mutex);
1504
1505 return 0;
1506}
1507
Peter Zijlstra38ff6672009-03-30 19:07:03 +02001508/*
1509 * Callers need to ensure there can be no nesting of this function, otherwise
1510 * the seqlock logic goes bad. We can not serialize this because the arch
1511 * code calls this from NMI context.
1512 */
1513void perf_counter_update_userpage(struct perf_counter *counter)
Paul Mackerras37d81822009-03-23 18:22:08 +01001514{
Peter Zijlstra38ff6672009-03-30 19:07:03 +02001515 struct perf_mmap_data *data;
1516 struct perf_counter_mmap_page *userpg;
1517
1518 rcu_read_lock();
1519 data = rcu_dereference(counter->data);
1520 if (!data)
1521 goto unlock;
1522
1523 userpg = data->user_page;
Paul Mackerras37d81822009-03-23 18:22:08 +01001524
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001525 /*
1526 * Disable preemption so as to not let the corresponding user-space
1527 * spin too long if we get preempted.
1528 */
1529 preempt_disable();
Paul Mackerras37d81822009-03-23 18:22:08 +01001530 ++userpg->lock;
Peter Zijlstra92f22a32009-04-02 11:12:04 +02001531 barrier();
Paul Mackerras37d81822009-03-23 18:22:08 +01001532 userpg->index = counter->hw.idx;
1533 userpg->offset = atomic64_read(&counter->count);
1534 if (counter->state == PERF_COUNTER_STATE_ACTIVE)
1535 userpg->offset -= atomic64_read(&counter->hw.prev_count);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001536
Peter Zijlstra92f22a32009-04-02 11:12:04 +02001537 barrier();
Paul Mackerras37d81822009-03-23 18:22:08 +01001538 ++userpg->lock;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001539 preempt_enable();
Peter Zijlstra38ff6672009-03-30 19:07:03 +02001540unlock:
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001541 rcu_read_unlock();
Paul Mackerras37d81822009-03-23 18:22:08 +01001542}
1543
1544static int perf_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1545{
1546 struct perf_counter *counter = vma->vm_file->private_data;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001547 struct perf_mmap_data *data;
1548 int ret = VM_FAULT_SIGBUS;
Paul Mackerras37d81822009-03-23 18:22:08 +01001549
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001550 rcu_read_lock();
1551 data = rcu_dereference(counter->data);
1552 if (!data)
1553 goto unlock;
Paul Mackerras37d81822009-03-23 18:22:08 +01001554
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001555 if (vmf->pgoff == 0) {
1556 vmf->page = virt_to_page(data->user_page);
1557 } else {
1558 int nr = vmf->pgoff - 1;
1559
1560 if ((unsigned)nr > data->nr_pages)
1561 goto unlock;
1562
1563 vmf->page = virt_to_page(data->data_pages[nr]);
1564 }
Paul Mackerras37d81822009-03-23 18:22:08 +01001565 get_page(vmf->page);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001566 ret = 0;
1567unlock:
1568 rcu_read_unlock();
1569
1570 return ret;
1571}
1572
1573static int perf_mmap_data_alloc(struct perf_counter *counter, int nr_pages)
1574{
1575 struct perf_mmap_data *data;
1576 unsigned long size;
1577 int i;
1578
1579 WARN_ON(atomic_read(&counter->mmap_count));
1580
1581 size = sizeof(struct perf_mmap_data);
1582 size += nr_pages * sizeof(void *);
1583
1584 data = kzalloc(size, GFP_KERNEL);
1585 if (!data)
1586 goto fail;
1587
1588 data->user_page = (void *)get_zeroed_page(GFP_KERNEL);
1589 if (!data->user_page)
1590 goto fail_user_page;
1591
1592 for (i = 0; i < nr_pages; i++) {
1593 data->data_pages[i] = (void *)get_zeroed_page(GFP_KERNEL);
1594 if (!data->data_pages[i])
1595 goto fail_data_pages;
1596 }
1597
1598 data->nr_pages = nr_pages;
Peter Zijlstra22c15582009-05-05 17:50:25 +02001599 atomic_set(&data->lock, -1);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001600
1601 rcu_assign_pointer(counter->data, data);
1602
Paul Mackerras37d81822009-03-23 18:22:08 +01001603 return 0;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001604
1605fail_data_pages:
1606 for (i--; i >= 0; i--)
1607 free_page((unsigned long)data->data_pages[i]);
1608
1609 free_page((unsigned long)data->user_page);
1610
1611fail_user_page:
1612 kfree(data);
1613
1614fail:
1615 return -ENOMEM;
1616}
1617
1618static void __perf_mmap_data_free(struct rcu_head *rcu_head)
1619{
1620 struct perf_mmap_data *data = container_of(rcu_head,
1621 struct perf_mmap_data, rcu_head);
1622 int i;
1623
1624 free_page((unsigned long)data->user_page);
1625 for (i = 0; i < data->nr_pages; i++)
1626 free_page((unsigned long)data->data_pages[i]);
1627 kfree(data);
1628}
1629
1630static void perf_mmap_data_free(struct perf_counter *counter)
1631{
1632 struct perf_mmap_data *data = counter->data;
1633
1634 WARN_ON(atomic_read(&counter->mmap_count));
1635
1636 rcu_assign_pointer(counter->data, NULL);
1637 call_rcu(&data->rcu_head, __perf_mmap_data_free);
1638}
1639
1640static void perf_mmap_open(struct vm_area_struct *vma)
1641{
1642 struct perf_counter *counter = vma->vm_file->private_data;
1643
1644 atomic_inc(&counter->mmap_count);
1645}
1646
1647static void perf_mmap_close(struct vm_area_struct *vma)
1648{
1649 struct perf_counter *counter = vma->vm_file->private_data;
1650
1651 if (atomic_dec_and_mutex_lock(&counter->mmap_count,
1652 &counter->mmap_mutex)) {
Peter Zijlstra789f90f2009-05-15 15:19:27 +02001653 struct user_struct *user = current_user();
1654
1655 atomic_long_sub(counter->data->nr_pages + 1, &user->locked_vm);
Peter Zijlstrac5078f72009-05-05 17:50:24 +02001656 vma->vm_mm->locked_vm -= counter->data->nr_locked;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001657 perf_mmap_data_free(counter);
1658 mutex_unlock(&counter->mmap_mutex);
1659 }
Paul Mackerras37d81822009-03-23 18:22:08 +01001660}
1661
1662static struct vm_operations_struct perf_mmap_vmops = {
Peter Zijlstraebb3c4c2009-04-06 11:45:05 +02001663 .open = perf_mmap_open,
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001664 .close = perf_mmap_close,
Paul Mackerras37d81822009-03-23 18:22:08 +01001665 .fault = perf_mmap_fault,
1666};
1667
1668static int perf_mmap(struct file *file, struct vm_area_struct *vma)
1669{
1670 struct perf_counter *counter = file->private_data;
Peter Zijlstra789f90f2009-05-15 15:19:27 +02001671 struct user_struct *user = current_user();
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001672 unsigned long vma_size;
1673 unsigned long nr_pages;
Peter Zijlstra789f90f2009-05-15 15:19:27 +02001674 unsigned long user_locked, user_lock_limit;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001675 unsigned long locked, lock_limit;
Peter Zijlstra789f90f2009-05-15 15:19:27 +02001676 long user_extra, extra;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001677 int ret = 0;
Paul Mackerras37d81822009-03-23 18:22:08 +01001678
1679 if (!(vma->vm_flags & VM_SHARED) || (vma->vm_flags & VM_WRITE))
1680 return -EINVAL;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001681
1682 vma_size = vma->vm_end - vma->vm_start;
1683 nr_pages = (vma_size / PAGE_SIZE) - 1;
1684
Peter Zijlstra7730d862009-03-25 12:48:31 +01001685 /*
1686 * If we have data pages ensure they're a power-of-two number, so we
1687 * can do bitmasks instead of modulo.
1688 */
1689 if (nr_pages != 0 && !is_power_of_2(nr_pages))
Paul Mackerras37d81822009-03-23 18:22:08 +01001690 return -EINVAL;
1691
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001692 if (vma_size != PAGE_SIZE * (1 + nr_pages))
Paul Mackerras37d81822009-03-23 18:22:08 +01001693 return -EINVAL;
1694
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001695 if (vma->vm_pgoff != 0)
1696 return -EINVAL;
Paul Mackerras37d81822009-03-23 18:22:08 +01001697
Peter Zijlstraebb3c4c2009-04-06 11:45:05 +02001698 mutex_lock(&counter->mmap_mutex);
1699 if (atomic_inc_not_zero(&counter->mmap_count)) {
1700 if (nr_pages != counter->data->nr_pages)
1701 ret = -EINVAL;
1702 goto unlock;
1703 }
1704
Peter Zijlstra789f90f2009-05-15 15:19:27 +02001705 user_extra = nr_pages + 1;
1706 user_lock_limit = sysctl_perf_counter_mlock >> (PAGE_SHIFT - 10);
Ingo Molnara3862d32009-05-24 09:02:37 +02001707
1708 /*
1709 * Increase the limit linearly with more CPUs:
1710 */
1711 user_lock_limit *= num_online_cpus();
1712
Peter Zijlstra789f90f2009-05-15 15:19:27 +02001713 user_locked = atomic_long_read(&user->locked_vm) + user_extra;
Peter Zijlstrac5078f72009-05-05 17:50:24 +02001714
Peter Zijlstra789f90f2009-05-15 15:19:27 +02001715 extra = 0;
1716 if (user_locked > user_lock_limit)
1717 extra = user_locked - user_lock_limit;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001718
1719 lock_limit = current->signal->rlim[RLIMIT_MEMLOCK].rlim_cur;
1720 lock_limit >>= PAGE_SHIFT;
Peter Zijlstra789f90f2009-05-15 15:19:27 +02001721 locked = vma->vm_mm->locked_vm + extra;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001722
Peter Zijlstraebb3c4c2009-04-06 11:45:05 +02001723 if ((locked > lock_limit) && !capable(CAP_IPC_LOCK)) {
1724 ret = -EPERM;
1725 goto unlock;
1726 }
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001727
1728 WARN_ON(counter->data);
1729 ret = perf_mmap_data_alloc(counter, nr_pages);
Peter Zijlstraebb3c4c2009-04-06 11:45:05 +02001730 if (ret)
1731 goto unlock;
1732
1733 atomic_set(&counter->mmap_count, 1);
Peter Zijlstra789f90f2009-05-15 15:19:27 +02001734 atomic_long_add(user_extra, &user->locked_vm);
Peter Zijlstrac5078f72009-05-05 17:50:24 +02001735 vma->vm_mm->locked_vm += extra;
1736 counter->data->nr_locked = extra;
Peter Zijlstraebb3c4c2009-04-06 11:45:05 +02001737unlock:
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001738 mutex_unlock(&counter->mmap_mutex);
Paul Mackerras37d81822009-03-23 18:22:08 +01001739
1740 vma->vm_flags &= ~VM_MAYWRITE;
1741 vma->vm_flags |= VM_RESERVED;
1742 vma->vm_ops = &perf_mmap_vmops;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001743
1744 return ret;
Paul Mackerras37d81822009-03-23 18:22:08 +01001745}
1746
Peter Zijlstra3c446b3d2009-04-06 11:45:01 +02001747static int perf_fasync(int fd, struct file *filp, int on)
1748{
1749 struct perf_counter *counter = filp->private_data;
1750 struct inode *inode = filp->f_path.dentry->d_inode;
1751 int retval;
1752
1753 mutex_lock(&inode->i_mutex);
1754 retval = fasync_helper(fd, filp, on, &counter->fasync);
1755 mutex_unlock(&inode->i_mutex);
1756
1757 if (retval < 0)
1758 return retval;
1759
1760 return 0;
1761}
1762
Thomas Gleixner0793a612008-12-04 20:12:29 +01001763static const struct file_operations perf_fops = {
1764 .release = perf_release,
1765 .read = perf_read,
1766 .poll = perf_poll,
Paul Mackerrasd859e292009-01-17 18:10:22 +11001767 .unlocked_ioctl = perf_ioctl,
1768 .compat_ioctl = perf_ioctl,
Paul Mackerras37d81822009-03-23 18:22:08 +01001769 .mmap = perf_mmap,
Peter Zijlstra3c446b3d2009-04-06 11:45:01 +02001770 .fasync = perf_fasync,
Thomas Gleixner0793a612008-12-04 20:12:29 +01001771};
1772
Peter Zijlstra15dbf272009-03-13 12:21:32 +01001773/*
Peter Zijlstra925d5192009-03-30 19:07:02 +02001774 * Perf counter wakeup
1775 *
1776 * If there's data, ensure we set the poll() state and publish everything
1777 * to user-space before waking everybody up.
1778 */
1779
1780void perf_counter_wakeup(struct perf_counter *counter)
1781{
Peter Zijlstra925d5192009-03-30 19:07:02 +02001782 wake_up_all(&counter->waitq);
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02001783
1784 if (counter->pending_kill) {
1785 kill_fasync(&counter->fasync, SIGIO, counter->pending_kill);
1786 counter->pending_kill = 0;
1787 }
Peter Zijlstra925d5192009-03-30 19:07:02 +02001788}
1789
1790/*
1791 * Pending wakeups
1792 *
1793 * Handle the case where we need to wakeup up from NMI (or rq->lock) context.
1794 *
1795 * The NMI bit means we cannot possibly take locks. Therefore, maintain a
1796 * single linked list and use cmpxchg() to add entries lockless.
1797 */
1798
Peter Zijlstra79f14642009-04-06 11:45:07 +02001799static void perf_pending_counter(struct perf_pending_entry *entry)
1800{
1801 struct perf_counter *counter = container_of(entry,
1802 struct perf_counter, pending);
1803
1804 if (counter->pending_disable) {
1805 counter->pending_disable = 0;
1806 perf_counter_disable(counter);
1807 }
1808
1809 if (counter->pending_wakeup) {
1810 counter->pending_wakeup = 0;
1811 perf_counter_wakeup(counter);
1812 }
1813}
1814
Peter Zijlstra671dec52009-04-06 11:45:02 +02001815#define PENDING_TAIL ((struct perf_pending_entry *)-1UL)
Peter Zijlstra925d5192009-03-30 19:07:02 +02001816
Peter Zijlstra671dec52009-04-06 11:45:02 +02001817static DEFINE_PER_CPU(struct perf_pending_entry *, perf_pending_head) = {
Peter Zijlstra925d5192009-03-30 19:07:02 +02001818 PENDING_TAIL,
1819};
1820
Peter Zijlstra671dec52009-04-06 11:45:02 +02001821static void perf_pending_queue(struct perf_pending_entry *entry,
1822 void (*func)(struct perf_pending_entry *))
Peter Zijlstra925d5192009-03-30 19:07:02 +02001823{
Peter Zijlstra671dec52009-04-06 11:45:02 +02001824 struct perf_pending_entry **head;
Peter Zijlstra925d5192009-03-30 19:07:02 +02001825
Peter Zijlstra671dec52009-04-06 11:45:02 +02001826 if (cmpxchg(&entry->next, NULL, PENDING_TAIL) != NULL)
Peter Zijlstra925d5192009-03-30 19:07:02 +02001827 return;
1828
Peter Zijlstra671dec52009-04-06 11:45:02 +02001829 entry->func = func;
1830
1831 head = &get_cpu_var(perf_pending_head);
Peter Zijlstra925d5192009-03-30 19:07:02 +02001832
1833 do {
Peter Zijlstra671dec52009-04-06 11:45:02 +02001834 entry->next = *head;
1835 } while (cmpxchg(head, entry->next, entry) != entry->next);
Peter Zijlstra925d5192009-03-30 19:07:02 +02001836
1837 set_perf_counter_pending();
1838
Peter Zijlstra671dec52009-04-06 11:45:02 +02001839 put_cpu_var(perf_pending_head);
Peter Zijlstra925d5192009-03-30 19:07:02 +02001840}
1841
1842static int __perf_pending_run(void)
1843{
Peter Zijlstra671dec52009-04-06 11:45:02 +02001844 struct perf_pending_entry *list;
Peter Zijlstra925d5192009-03-30 19:07:02 +02001845 int nr = 0;
1846
Peter Zijlstra671dec52009-04-06 11:45:02 +02001847 list = xchg(&__get_cpu_var(perf_pending_head), PENDING_TAIL);
Peter Zijlstra925d5192009-03-30 19:07:02 +02001848 while (list != PENDING_TAIL) {
Peter Zijlstra671dec52009-04-06 11:45:02 +02001849 void (*func)(struct perf_pending_entry *);
1850 struct perf_pending_entry *entry = list;
Peter Zijlstra925d5192009-03-30 19:07:02 +02001851
1852 list = list->next;
1853
Peter Zijlstra671dec52009-04-06 11:45:02 +02001854 func = entry->func;
1855 entry->next = NULL;
Peter Zijlstra925d5192009-03-30 19:07:02 +02001856 /*
1857 * Ensure we observe the unqueue before we issue the wakeup,
1858 * so that we won't be waiting forever.
1859 * -- see perf_not_pending().
1860 */
1861 smp_wmb();
1862
Peter Zijlstra671dec52009-04-06 11:45:02 +02001863 func(entry);
Peter Zijlstra925d5192009-03-30 19:07:02 +02001864 nr++;
1865 }
1866
1867 return nr;
1868}
1869
1870static inline int perf_not_pending(struct perf_counter *counter)
1871{
1872 /*
1873 * If we flush on whatever cpu we run, there is a chance we don't
1874 * need to wait.
1875 */
1876 get_cpu();
1877 __perf_pending_run();
1878 put_cpu();
1879
1880 /*
1881 * Ensure we see the proper queue state before going to sleep
1882 * so that we do not miss the wakeup. -- see perf_pending_handle()
1883 */
1884 smp_rmb();
Peter Zijlstra671dec52009-04-06 11:45:02 +02001885 return counter->pending.next == NULL;
Peter Zijlstra925d5192009-03-30 19:07:02 +02001886}
1887
1888static void perf_pending_sync(struct perf_counter *counter)
1889{
1890 wait_event(counter->waitq, perf_not_pending(counter));
1891}
1892
1893void perf_counter_do_pending(void)
1894{
1895 __perf_pending_run();
1896}
1897
1898/*
Peter Zijlstra394ee072009-03-30 19:07:14 +02001899 * Callchain support -- arch specific
1900 */
1901
Peter Zijlstra9c03d882009-04-06 11:45:00 +02001902__weak struct perf_callchain_entry *perf_callchain(struct pt_regs *regs)
Peter Zijlstra394ee072009-03-30 19:07:14 +02001903{
1904 return NULL;
1905}
1906
1907/*
Peter Zijlstra0322cd62009-03-19 20:26:19 +01001908 * Output
1909 */
1910
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01001911struct perf_output_handle {
1912 struct perf_counter *counter;
1913 struct perf_mmap_data *data;
1914 unsigned int offset;
Peter Zijlstra63e35b22009-03-25 12:30:24 +01001915 unsigned int head;
Peter Zijlstra78d613e2009-03-30 19:07:11 +02001916 int nmi;
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02001917 int overflow;
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001918 int locked;
1919 unsigned long flags;
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01001920};
1921
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001922static void perf_output_wakeup(struct perf_output_handle *handle)
Peter Zijlstra78d613e2009-03-30 19:07:11 +02001923{
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001924 atomic_set(&handle->data->poll, POLL_IN);
1925
Peter Zijlstra671dec52009-04-06 11:45:02 +02001926 if (handle->nmi) {
Peter Zijlstra79f14642009-04-06 11:45:07 +02001927 handle->counter->pending_wakeup = 1;
Peter Zijlstra671dec52009-04-06 11:45:02 +02001928 perf_pending_queue(&handle->counter->pending,
Peter Zijlstra79f14642009-04-06 11:45:07 +02001929 perf_pending_counter);
Peter Zijlstra671dec52009-04-06 11:45:02 +02001930 } else
Peter Zijlstra78d613e2009-03-30 19:07:11 +02001931 perf_counter_wakeup(handle->counter);
1932}
1933
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001934/*
1935 * Curious locking construct.
1936 *
1937 * We need to ensure a later event doesn't publish a head when a former
1938 * event isn't done writing. However since we need to deal with NMIs we
1939 * cannot fully serialize things.
1940 *
1941 * What we do is serialize between CPUs so we only have to deal with NMI
1942 * nesting on a single CPU.
1943 *
1944 * We only publish the head (and generate a wakeup) when the outer-most
1945 * event completes.
1946 */
1947static void perf_output_lock(struct perf_output_handle *handle)
1948{
1949 struct perf_mmap_data *data = handle->data;
1950 int cpu;
1951
1952 handle->locked = 0;
1953
1954 local_irq_save(handle->flags);
1955 cpu = smp_processor_id();
1956
1957 if (in_nmi() && atomic_read(&data->lock) == cpu)
1958 return;
1959
Peter Zijlstra22c15582009-05-05 17:50:25 +02001960 while (atomic_cmpxchg(&data->lock, -1, cpu) != -1)
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001961 cpu_relax();
1962
1963 handle->locked = 1;
1964}
1965
1966static void perf_output_unlock(struct perf_output_handle *handle)
1967{
1968 struct perf_mmap_data *data = handle->data;
1969 int head, cpu;
1970
Peter Zijlstrac66de4a2009-05-05 17:50:22 +02001971 data->done_head = data->head;
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001972
1973 if (!handle->locked)
1974 goto out;
1975
1976again:
1977 /*
1978 * The xchg implies a full barrier that ensures all writes are done
1979 * before we publish the new head, matched by a rmb() in userspace when
1980 * reading this position.
1981 */
Peter Zijlstrac66de4a2009-05-05 17:50:22 +02001982 while ((head = atomic_xchg(&data->done_head, 0)))
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001983 data->user_page->data_head = head;
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001984
1985 /*
Peter Zijlstrac66de4a2009-05-05 17:50:22 +02001986 * NMI can happen here, which means we can miss a done_head update.
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001987 */
1988
Peter Zijlstra22c15582009-05-05 17:50:25 +02001989 cpu = atomic_xchg(&data->lock, -1);
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001990 WARN_ON_ONCE(cpu != smp_processor_id());
1991
1992 /*
1993 * Therefore we have to validate we did not indeed do so.
1994 */
Peter Zijlstrac66de4a2009-05-05 17:50:22 +02001995 if (unlikely(atomic_read(&data->done_head))) {
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001996 /*
1997 * Since we had it locked, we can lock it again.
1998 */
Peter Zijlstra22c15582009-05-05 17:50:25 +02001999 while (atomic_cmpxchg(&data->lock, -1, cpu) != -1)
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002000 cpu_relax();
2001
2002 goto again;
2003 }
2004
Peter Zijlstrac66de4a2009-05-05 17:50:22 +02002005 if (atomic_xchg(&data->wakeup, 0))
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002006 perf_output_wakeup(handle);
2007out:
2008 local_irq_restore(handle->flags);
2009}
2010
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002011static int perf_output_begin(struct perf_output_handle *handle,
Peter Zijlstra78d613e2009-03-30 19:07:11 +02002012 struct perf_counter *counter, unsigned int size,
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02002013 int nmi, int overflow)
Peter Zijlstra0322cd62009-03-19 20:26:19 +01002014{
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002015 struct perf_mmap_data *data;
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002016 unsigned int offset, head;
Peter Zijlstra0322cd62009-03-19 20:26:19 +01002017
Peter Zijlstra2023b352009-05-05 17:50:26 +02002018 /*
2019 * For inherited counters we send all the output towards the parent.
2020 */
2021 if (counter->parent)
2022 counter = counter->parent;
2023
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002024 rcu_read_lock();
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002025 data = rcu_dereference(counter->data);
2026 if (!data)
2027 goto out;
Peter Zijlstra0322cd62009-03-19 20:26:19 +01002028
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002029 handle->data = data;
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02002030 handle->counter = counter;
2031 handle->nmi = nmi;
2032 handle->overflow = overflow;
Peter Zijlstra78d613e2009-03-30 19:07:11 +02002033
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002034 if (!data->nr_pages)
Peter Zijlstra78d613e2009-03-30 19:07:11 +02002035 goto fail;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002036
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002037 perf_output_lock(handle);
2038
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002039 do {
2040 offset = head = atomic_read(&data->head);
Peter Zijlstrac7138f32009-03-24 13:18:16 +01002041 head += size;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002042 } while (atomic_cmpxchg(&data->head, offset, head) != offset);
2043
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002044 handle->offset = offset;
Peter Zijlstra63e35b22009-03-25 12:30:24 +01002045 handle->head = head;
Peter Zijlstrac66de4a2009-05-05 17:50:22 +02002046
2047 if ((offset >> PAGE_SHIFT) != (head >> PAGE_SHIFT))
2048 atomic_set(&data->wakeup, 1);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002049
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002050 return 0;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002051
Peter Zijlstra78d613e2009-03-30 19:07:11 +02002052fail:
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002053 perf_output_wakeup(handle);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002054out:
2055 rcu_read_unlock();
2056
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002057 return -ENOSPC;
2058}
2059
2060static void perf_output_copy(struct perf_output_handle *handle,
2061 void *buf, unsigned int len)
2062{
2063 unsigned int pages_mask;
2064 unsigned int offset;
2065 unsigned int size;
2066 void **pages;
2067
2068 offset = handle->offset;
2069 pages_mask = handle->data->nr_pages - 1;
2070 pages = handle->data->data_pages;
2071
2072 do {
2073 unsigned int page_offset;
2074 int nr;
2075
2076 nr = (offset >> PAGE_SHIFT) & pages_mask;
2077 page_offset = offset & (PAGE_SIZE - 1);
2078 size = min_t(unsigned int, PAGE_SIZE - page_offset, len);
2079
2080 memcpy(pages[nr] + page_offset, buf, size);
2081
2082 len -= size;
2083 buf += size;
2084 offset += size;
2085 } while (len);
2086
2087 handle->offset = offset;
Peter Zijlstra63e35b22009-03-25 12:30:24 +01002088
Peter Zijlstra53020fe2009-05-13 21:26:19 +02002089 /*
2090 * Check we didn't copy past our reservation window, taking the
2091 * possible unsigned int wrap into account.
2092 */
2093 WARN_ON_ONCE(((int)(handle->head - handle->offset)) < 0);
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002094}
2095
Peter Zijlstra5c148192009-03-25 12:30:23 +01002096#define perf_output_put(handle, x) \
2097 perf_output_copy((handle), &(x), sizeof(x))
2098
Peter Zijlstra78d613e2009-03-30 19:07:11 +02002099static void perf_output_end(struct perf_output_handle *handle)
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002100{
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002101 struct perf_counter *counter = handle->counter;
2102 struct perf_mmap_data *data = handle->data;
2103
2104 int wakeup_events = counter->hw_event.wakeup_events;
Peter Zijlstrac4578102009-04-02 11:12:01 +02002105
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02002106 if (handle->overflow && wakeup_events) {
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002107 int events = atomic_inc_return(&data->events);
Peter Zijlstrac4578102009-04-02 11:12:01 +02002108 if (events >= wakeup_events) {
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002109 atomic_sub(wakeup_events, &data->events);
Peter Zijlstrac66de4a2009-05-05 17:50:22 +02002110 atomic_set(&data->wakeup, 1);
Peter Zijlstrac4578102009-04-02 11:12:01 +02002111 }
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002112 }
2113
2114 perf_output_unlock(handle);
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002115 rcu_read_unlock();
2116}
2117
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002118static void perf_counter_output(struct perf_counter *counter,
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002119 int nmi, struct pt_regs *regs, u64 addr)
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002120{
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002121 int ret;
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002122 u64 record_type = counter->hw_event.record_type;
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002123 struct perf_output_handle handle;
2124 struct perf_event_header header;
2125 u64 ip;
Peter Zijlstra5c148192009-03-25 12:30:23 +01002126 struct {
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01002127 u32 pid, tid;
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002128 } tid_entry;
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002129 struct {
2130 u64 event;
2131 u64 counter;
2132 } group_entry;
Peter Zijlstra394ee072009-03-30 19:07:14 +02002133 struct perf_callchain_entry *callchain = NULL;
2134 int callchain_size = 0;
Peter Zijlstra339f7c92009-04-06 11:45:06 +02002135 u64 time;
Peter Zijlstraf370e1e2009-05-08 18:52:24 +02002136 struct {
2137 u32 cpu, reserved;
2138 } cpu_entry;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002139
Peter Zijlstra6b6e54862009-04-08 15:01:27 +02002140 header.type = 0;
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002141 header.size = sizeof(header);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002142
Peter Zijlstra6b6e54862009-04-08 15:01:27 +02002143 header.misc = PERF_EVENT_MISC_OVERFLOW;
Paul Mackerras9d23a902009-05-14 21:48:08 +10002144 header.misc |= perf_misc_flags(regs);
Peter Zijlstra6fab0192009-04-08 15:01:26 +02002145
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002146 if (record_type & PERF_RECORD_IP) {
Paul Mackerras9d23a902009-05-14 21:48:08 +10002147 ip = perf_instruction_pointer(regs);
Peter Zijlstra6b6e54862009-04-08 15:01:27 +02002148 header.type |= PERF_RECORD_IP;
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002149 header.size += sizeof(ip);
2150 }
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01002151
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002152 if (record_type & PERF_RECORD_TID) {
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01002153 /* namespace issues */
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002154 tid_entry.pid = current->group_leader->pid;
2155 tid_entry.tid = current->pid;
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01002156
Peter Zijlstra6b6e54862009-04-08 15:01:27 +02002157 header.type |= PERF_RECORD_TID;
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002158 header.size += sizeof(tid_entry);
2159 }
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01002160
Peter Zijlstra4d855452009-04-08 15:01:32 +02002161 if (record_type & PERF_RECORD_TIME) {
2162 /*
2163 * Maybe do better on x86 and provide cpu_clock_nmi()
2164 */
2165 time = sched_clock();
2166
2167 header.type |= PERF_RECORD_TIME;
2168 header.size += sizeof(u64);
2169 }
2170
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002171 if (record_type & PERF_RECORD_ADDR) {
2172 header.type |= PERF_RECORD_ADDR;
2173 header.size += sizeof(u64);
2174 }
2175
Peter Zijlstraa85f61a2009-05-08 18:52:23 +02002176 if (record_type & PERF_RECORD_CONFIG) {
2177 header.type |= PERF_RECORD_CONFIG;
2178 header.size += sizeof(u64);
2179 }
2180
Peter Zijlstraf370e1e2009-05-08 18:52:24 +02002181 if (record_type & PERF_RECORD_CPU) {
2182 header.type |= PERF_RECORD_CPU;
2183 header.size += sizeof(cpu_entry);
2184
2185 cpu_entry.cpu = raw_smp_processor_id();
2186 }
2187
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002188 if (record_type & PERF_RECORD_GROUP) {
Peter Zijlstra6b6e54862009-04-08 15:01:27 +02002189 header.type |= PERF_RECORD_GROUP;
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002190 header.size += sizeof(u64) +
2191 counter->nr_siblings * sizeof(group_entry);
2192 }
2193
2194 if (record_type & PERF_RECORD_CALLCHAIN) {
Peter Zijlstra394ee072009-03-30 19:07:14 +02002195 callchain = perf_callchain(regs);
2196
2197 if (callchain) {
Peter Zijlstra9c03d882009-04-06 11:45:00 +02002198 callchain_size = (1 + callchain->nr) * sizeof(u64);
Peter Zijlstra394ee072009-03-30 19:07:14 +02002199
Peter Zijlstra6b6e54862009-04-08 15:01:27 +02002200 header.type |= PERF_RECORD_CALLCHAIN;
Peter Zijlstra394ee072009-03-30 19:07:14 +02002201 header.size += callchain_size;
2202 }
2203 }
2204
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02002205 ret = perf_output_begin(&handle, counter, header.size, nmi, 1);
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002206 if (ret)
2207 return;
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01002208
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002209 perf_output_put(&handle, header);
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002210
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002211 if (record_type & PERF_RECORD_IP)
2212 perf_output_put(&handle, ip);
2213
2214 if (record_type & PERF_RECORD_TID)
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002215 perf_output_put(&handle, tid_entry);
2216
Peter Zijlstra4d855452009-04-08 15:01:32 +02002217 if (record_type & PERF_RECORD_TIME)
2218 perf_output_put(&handle, time);
2219
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002220 if (record_type & PERF_RECORD_ADDR)
2221 perf_output_put(&handle, addr);
2222
Peter Zijlstraa85f61a2009-05-08 18:52:23 +02002223 if (record_type & PERF_RECORD_CONFIG)
2224 perf_output_put(&handle, counter->hw_event.config);
2225
Peter Zijlstraf370e1e2009-05-08 18:52:24 +02002226 if (record_type & PERF_RECORD_CPU)
2227 perf_output_put(&handle, cpu_entry);
2228
Peter Zijlstra2023b352009-05-05 17:50:26 +02002229 /*
2230 * XXX PERF_RECORD_GROUP vs inherited counters seems difficult.
2231 */
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002232 if (record_type & PERF_RECORD_GROUP) {
2233 struct perf_counter *leader, *sub;
2234 u64 nr = counter->nr_siblings;
2235
2236 perf_output_put(&handle, nr);
2237
2238 leader = counter->group_leader;
2239 list_for_each_entry(sub, &leader->sibling_list, list_entry) {
2240 if (sub != counter)
Robert Richter4aeb0b42009-04-29 12:47:03 +02002241 sub->pmu->read(sub);
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002242
2243 group_entry.event = sub->hw_event.config;
2244 group_entry.counter = atomic64_read(&sub->count);
2245
2246 perf_output_put(&handle, group_entry);
2247 }
2248 }
2249
Peter Zijlstra394ee072009-03-30 19:07:14 +02002250 if (callchain)
2251 perf_output_copy(&handle, callchain, callchain_size);
2252
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002253 perf_output_end(&handle);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002254}
2255
Peter Zijlstra0322cd62009-03-19 20:26:19 +01002256/*
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02002257 * comm tracking
2258 */
2259
2260struct perf_comm_event {
2261 struct task_struct *task;
2262 char *comm;
2263 int comm_size;
2264
2265 struct {
2266 struct perf_event_header header;
2267
2268 u32 pid;
2269 u32 tid;
2270 } event;
2271};
2272
2273static void perf_counter_comm_output(struct perf_counter *counter,
2274 struct perf_comm_event *comm_event)
2275{
2276 struct perf_output_handle handle;
2277 int size = comm_event->event.header.size;
2278 int ret = perf_output_begin(&handle, counter, size, 0, 0);
2279
2280 if (ret)
2281 return;
2282
2283 perf_output_put(&handle, comm_event->event);
2284 perf_output_copy(&handle, comm_event->comm,
2285 comm_event->comm_size);
2286 perf_output_end(&handle);
2287}
2288
2289static int perf_counter_comm_match(struct perf_counter *counter,
2290 struct perf_comm_event *comm_event)
2291{
2292 if (counter->hw_event.comm &&
2293 comm_event->event.header.type == PERF_EVENT_COMM)
2294 return 1;
2295
2296 return 0;
2297}
2298
2299static void perf_counter_comm_ctx(struct perf_counter_context *ctx,
2300 struct perf_comm_event *comm_event)
2301{
2302 struct perf_counter *counter;
2303
2304 if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
2305 return;
2306
2307 rcu_read_lock();
2308 list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
2309 if (perf_counter_comm_match(counter, comm_event))
2310 perf_counter_comm_output(counter, comm_event);
2311 }
2312 rcu_read_unlock();
2313}
2314
2315static void perf_counter_comm_event(struct perf_comm_event *comm_event)
2316{
2317 struct perf_cpu_context *cpuctx;
2318 unsigned int size;
2319 char *comm = comm_event->task->comm;
2320
Ingo Molnar888fcee2009-04-09 09:48:22 +02002321 size = ALIGN(strlen(comm)+1, sizeof(u64));
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02002322
2323 comm_event->comm = comm;
2324 comm_event->comm_size = size;
2325
2326 comm_event->event.header.size = sizeof(comm_event->event) + size;
2327
2328 cpuctx = &get_cpu_var(perf_cpu_context);
2329 perf_counter_comm_ctx(&cpuctx->ctx, comm_event);
2330 put_cpu_var(perf_cpu_context);
2331
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10002332 perf_counter_comm_ctx(current->perf_counter_ctxp, comm_event);
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02002333}
2334
2335void perf_counter_comm(struct task_struct *task)
2336{
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02002337 struct perf_comm_event comm_event;
2338
2339 if (!atomic_read(&nr_comm_tracking))
2340 return;
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10002341 if (!current->perf_counter_ctxp)
2342 return;
2343
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02002344 comm_event = (struct perf_comm_event){
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02002345 .task = task,
2346 .event = {
2347 .header = { .type = PERF_EVENT_COMM, },
2348 .pid = task->group_leader->pid,
2349 .tid = task->pid,
2350 },
2351 };
2352
2353 perf_counter_comm_event(&comm_event);
2354}
2355
2356/*
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002357 * mmap tracking
2358 */
2359
2360struct perf_mmap_event {
2361 struct file *file;
2362 char *file_name;
2363 int file_size;
2364
2365 struct {
2366 struct perf_event_header header;
2367
2368 u32 pid;
2369 u32 tid;
2370 u64 start;
2371 u64 len;
2372 u64 pgoff;
2373 } event;
2374};
2375
2376static void perf_counter_mmap_output(struct perf_counter *counter,
2377 struct perf_mmap_event *mmap_event)
2378{
2379 struct perf_output_handle handle;
2380 int size = mmap_event->event.header.size;
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02002381 int ret = perf_output_begin(&handle, counter, size, 0, 0);
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002382
2383 if (ret)
2384 return;
2385
2386 perf_output_put(&handle, mmap_event->event);
2387 perf_output_copy(&handle, mmap_event->file_name,
2388 mmap_event->file_size);
Peter Zijlstra78d613e2009-03-30 19:07:11 +02002389 perf_output_end(&handle);
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002390}
2391
2392static int perf_counter_mmap_match(struct perf_counter *counter,
2393 struct perf_mmap_event *mmap_event)
2394{
2395 if (counter->hw_event.mmap &&
2396 mmap_event->event.header.type == PERF_EVENT_MMAP)
2397 return 1;
2398
2399 if (counter->hw_event.munmap &&
2400 mmap_event->event.header.type == PERF_EVENT_MUNMAP)
2401 return 1;
2402
2403 return 0;
2404}
2405
2406static void perf_counter_mmap_ctx(struct perf_counter_context *ctx,
2407 struct perf_mmap_event *mmap_event)
2408{
2409 struct perf_counter *counter;
2410
2411 if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
2412 return;
2413
2414 rcu_read_lock();
2415 list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
2416 if (perf_counter_mmap_match(counter, mmap_event))
2417 perf_counter_mmap_output(counter, mmap_event);
2418 }
2419 rcu_read_unlock();
2420}
2421
2422static void perf_counter_mmap_event(struct perf_mmap_event *mmap_event)
2423{
2424 struct perf_cpu_context *cpuctx;
2425 struct file *file = mmap_event->file;
2426 unsigned int size;
2427 char tmp[16];
2428 char *buf = NULL;
2429 char *name;
2430
2431 if (file) {
2432 buf = kzalloc(PATH_MAX, GFP_KERNEL);
2433 if (!buf) {
2434 name = strncpy(tmp, "//enomem", sizeof(tmp));
2435 goto got_name;
2436 }
Peter Zijlstrad3d21c42009-04-09 10:53:46 +02002437 name = d_path(&file->f_path, buf, PATH_MAX);
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002438 if (IS_ERR(name)) {
2439 name = strncpy(tmp, "//toolong", sizeof(tmp));
2440 goto got_name;
2441 }
2442 } else {
2443 name = strncpy(tmp, "//anon", sizeof(tmp));
2444 goto got_name;
2445 }
2446
2447got_name:
Ingo Molnar888fcee2009-04-09 09:48:22 +02002448 size = ALIGN(strlen(name)+1, sizeof(u64));
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002449
2450 mmap_event->file_name = name;
2451 mmap_event->file_size = size;
2452
2453 mmap_event->event.header.size = sizeof(mmap_event->event) + size;
2454
2455 cpuctx = &get_cpu_var(perf_cpu_context);
2456 perf_counter_mmap_ctx(&cpuctx->ctx, mmap_event);
2457 put_cpu_var(perf_cpu_context);
2458
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10002459 perf_counter_mmap_ctx(current->perf_counter_ctxp, mmap_event);
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002460
2461 kfree(buf);
2462}
2463
2464void perf_counter_mmap(unsigned long addr, unsigned long len,
2465 unsigned long pgoff, struct file *file)
2466{
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02002467 struct perf_mmap_event mmap_event;
2468
2469 if (!atomic_read(&nr_mmap_tracking))
2470 return;
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10002471 if (!current->perf_counter_ctxp)
2472 return;
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02002473
2474 mmap_event = (struct perf_mmap_event){
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002475 .file = file,
2476 .event = {
2477 .header = { .type = PERF_EVENT_MMAP, },
2478 .pid = current->group_leader->pid,
2479 .tid = current->pid,
2480 .start = addr,
2481 .len = len,
2482 .pgoff = pgoff,
2483 },
2484 };
2485
2486 perf_counter_mmap_event(&mmap_event);
2487}
2488
2489void perf_counter_munmap(unsigned long addr, unsigned long len,
2490 unsigned long pgoff, struct file *file)
2491{
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02002492 struct perf_mmap_event mmap_event;
2493
2494 if (!atomic_read(&nr_munmap_tracking))
2495 return;
2496
2497 mmap_event = (struct perf_mmap_event){
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002498 .file = file,
2499 .event = {
2500 .header = { .type = PERF_EVENT_MUNMAP, },
2501 .pid = current->group_leader->pid,
2502 .tid = current->pid,
2503 .start = addr,
2504 .len = len,
2505 .pgoff = pgoff,
2506 },
2507 };
2508
2509 perf_counter_mmap_event(&mmap_event);
2510}
2511
2512/*
Peter Zijlstrae220d2d2009-05-23 18:28:55 +02002513 * Log irq_period changes so that analyzing tools can re-normalize the
2514 * event flow.
Peter Zijlstra26b119b2009-05-20 12:21:20 +02002515 */
2516
2517static void perf_log_period(struct perf_counter *counter, u64 period)
2518{
2519 struct perf_output_handle handle;
2520 int ret;
2521
2522 struct {
2523 struct perf_event_header header;
2524 u64 time;
2525 u64 period;
2526 } freq_event = {
2527 .header = {
2528 .type = PERF_EVENT_PERIOD,
2529 .misc = 0,
2530 .size = sizeof(freq_event),
2531 },
2532 .time = sched_clock(),
2533 .period = period,
2534 };
2535
2536 if (counter->hw.irq_period == period)
2537 return;
2538
2539 ret = perf_output_begin(&handle, counter, sizeof(freq_event), 0, 0);
2540 if (ret)
2541 return;
2542
2543 perf_output_put(&handle, freq_event);
2544 perf_output_end(&handle);
2545}
2546
2547/*
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002548 * Generic counter overflow handling.
2549 */
2550
2551int perf_counter_overflow(struct perf_counter *counter,
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002552 int nmi, struct pt_regs *regs, u64 addr)
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002553{
Peter Zijlstra79f14642009-04-06 11:45:07 +02002554 int events = atomic_read(&counter->event_limit);
2555 int ret = 0;
2556
Peter Zijlstra60db5e02009-05-15 15:19:28 +02002557 counter->hw.interrupts++;
2558
Peter Zijlstra2023b352009-05-05 17:50:26 +02002559 /*
2560 * XXX event_limit might not quite work as expected on inherited
2561 * counters
2562 */
2563
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02002564 counter->pending_kill = POLL_IN;
Peter Zijlstra79f14642009-04-06 11:45:07 +02002565 if (events && atomic_dec_and_test(&counter->event_limit)) {
2566 ret = 1;
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02002567 counter->pending_kill = POLL_HUP;
Peter Zijlstra79f14642009-04-06 11:45:07 +02002568 if (nmi) {
2569 counter->pending_disable = 1;
2570 perf_pending_queue(&counter->pending,
2571 perf_pending_counter);
2572 } else
2573 perf_counter_disable(counter);
2574 }
2575
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002576 perf_counter_output(counter, nmi, regs, addr);
Peter Zijlstra79f14642009-04-06 11:45:07 +02002577 return ret;
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002578}
2579
2580/*
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002581 * Generic software counter infrastructure
2582 */
2583
2584static void perf_swcounter_update(struct perf_counter *counter)
2585{
2586 struct hw_perf_counter *hwc = &counter->hw;
2587 u64 prev, now;
2588 s64 delta;
2589
2590again:
2591 prev = atomic64_read(&hwc->prev_count);
2592 now = atomic64_read(&hwc->count);
2593 if (atomic64_cmpxchg(&hwc->prev_count, prev, now) != prev)
2594 goto again;
2595
2596 delta = now - prev;
2597
2598 atomic64_add(delta, &counter->count);
2599 atomic64_sub(delta, &hwc->period_left);
2600}
2601
2602static void perf_swcounter_set_period(struct perf_counter *counter)
2603{
2604 struct hw_perf_counter *hwc = &counter->hw;
2605 s64 left = atomic64_read(&hwc->period_left);
2606 s64 period = hwc->irq_period;
2607
2608 if (unlikely(left <= -period)) {
2609 left = period;
2610 atomic64_set(&hwc->period_left, left);
2611 }
2612
2613 if (unlikely(left <= 0)) {
2614 left += period;
2615 atomic64_add(period, &hwc->period_left);
2616 }
2617
2618 atomic64_set(&hwc->prev_count, -left);
2619 atomic64_set(&hwc->count, -left);
2620}
2621
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002622static enum hrtimer_restart perf_swcounter_hrtimer(struct hrtimer *hrtimer)
2623{
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002624 enum hrtimer_restart ret = HRTIMER_RESTART;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002625 struct perf_counter *counter;
2626 struct pt_regs *regs;
Peter Zijlstra60db5e02009-05-15 15:19:28 +02002627 u64 period;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002628
2629 counter = container_of(hrtimer, struct perf_counter, hw.hrtimer);
Robert Richter4aeb0b42009-04-29 12:47:03 +02002630 counter->pmu->read(counter);
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002631
2632 regs = get_irq_regs();
2633 /*
2634 * In case we exclude kernel IPs or are somehow not in interrupt
2635 * context, provide the next best thing, the user IP.
2636 */
2637 if ((counter->hw_event.exclude_kernel || !regs) &&
2638 !counter->hw_event.exclude_user)
2639 regs = task_pt_regs(current);
2640
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002641 if (regs) {
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002642 if (perf_counter_overflow(counter, 0, regs, 0))
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002643 ret = HRTIMER_NORESTART;
2644 }
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002645
Peter Zijlstra60db5e02009-05-15 15:19:28 +02002646 period = max_t(u64, 10000, counter->hw.irq_period);
2647 hrtimer_forward_now(hrtimer, ns_to_ktime(period));
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002648
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002649 return ret;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002650}
2651
2652static void perf_swcounter_overflow(struct perf_counter *counter,
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002653 int nmi, struct pt_regs *regs, u64 addr)
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002654{
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002655 perf_swcounter_update(counter);
2656 perf_swcounter_set_period(counter);
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002657 if (perf_counter_overflow(counter, nmi, regs, addr))
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002658 /* soft-disable the counter */
2659 ;
2660
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002661}
2662
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002663static int perf_swcounter_match(struct perf_counter *counter,
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002664 enum perf_event_types type,
2665 u32 event, struct pt_regs *regs)
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002666{
2667 if (counter->state != PERF_COUNTER_STATE_ACTIVE)
2668 return 0;
2669
Peter Zijlstraf4a2deb42009-03-23 18:22:06 +01002670 if (perf_event_raw(&counter->hw_event))
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002671 return 0;
2672
Peter Zijlstraf4a2deb42009-03-23 18:22:06 +01002673 if (perf_event_type(&counter->hw_event) != type)
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002674 return 0;
2675
Peter Zijlstraf4a2deb42009-03-23 18:22:06 +01002676 if (perf_event_id(&counter->hw_event) != event)
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002677 return 0;
2678
2679 if (counter->hw_event.exclude_user && user_mode(regs))
2680 return 0;
2681
2682 if (counter->hw_event.exclude_kernel && !user_mode(regs))
2683 return 0;
2684
2685 return 1;
2686}
2687
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002688static void perf_swcounter_add(struct perf_counter *counter, u64 nr,
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002689 int nmi, struct pt_regs *regs, u64 addr)
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002690{
2691 int neg = atomic64_add_negative(nr, &counter->hw.count);
2692 if (counter->hw.irq_period && !neg)
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002693 perf_swcounter_overflow(counter, nmi, regs, addr);
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002694}
2695
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002696static void perf_swcounter_ctx_event(struct perf_counter_context *ctx,
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002697 enum perf_event_types type, u32 event,
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002698 u64 nr, int nmi, struct pt_regs *regs,
2699 u64 addr)
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002700{
2701 struct perf_counter *counter;
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002702
Peter Zijlstra01ef09d2009-03-19 20:26:11 +01002703 if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002704 return;
2705
Peter Zijlstra592903c2009-03-13 12:21:36 +01002706 rcu_read_lock();
2707 list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002708 if (perf_swcounter_match(counter, type, event, regs))
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002709 perf_swcounter_add(counter, nr, nmi, regs, addr);
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002710 }
Peter Zijlstra592903c2009-03-13 12:21:36 +01002711 rcu_read_unlock();
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002712}
2713
Peter Zijlstra96f6d442009-03-23 18:22:07 +01002714static int *perf_swcounter_recursion_context(struct perf_cpu_context *cpuctx)
2715{
2716 if (in_nmi())
2717 return &cpuctx->recursion[3];
2718
2719 if (in_irq())
2720 return &cpuctx->recursion[2];
2721
2722 if (in_softirq())
2723 return &cpuctx->recursion[1];
2724
2725 return &cpuctx->recursion[0];
2726}
2727
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002728static void __perf_swcounter_event(enum perf_event_types type, u32 event,
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002729 u64 nr, int nmi, struct pt_regs *regs,
2730 u64 addr)
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002731{
2732 struct perf_cpu_context *cpuctx = &get_cpu_var(perf_cpu_context);
Peter Zijlstra96f6d442009-03-23 18:22:07 +01002733 int *recursion = perf_swcounter_recursion_context(cpuctx);
2734
2735 if (*recursion)
2736 goto out;
2737
2738 (*recursion)++;
2739 barrier();
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002740
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002741 perf_swcounter_ctx_event(&cpuctx->ctx, type, event,
2742 nr, nmi, regs, addr);
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002743 if (cpuctx->task_ctx) {
2744 perf_swcounter_ctx_event(cpuctx->task_ctx, type, event,
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002745 nr, nmi, regs, addr);
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002746 }
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002747
Peter Zijlstra96f6d442009-03-23 18:22:07 +01002748 barrier();
2749 (*recursion)--;
2750
2751out:
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002752 put_cpu_var(perf_cpu_context);
2753}
2754
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002755void
2756perf_swcounter_event(u32 event, u64 nr, int nmi, struct pt_regs *regs, u64 addr)
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002757{
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002758 __perf_swcounter_event(PERF_TYPE_SOFTWARE, event, nr, nmi, regs, addr);
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002759}
2760
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002761static void perf_swcounter_read(struct perf_counter *counter)
2762{
2763 perf_swcounter_update(counter);
2764}
2765
2766static int perf_swcounter_enable(struct perf_counter *counter)
2767{
2768 perf_swcounter_set_period(counter);
2769 return 0;
2770}
2771
2772static void perf_swcounter_disable(struct perf_counter *counter)
2773{
2774 perf_swcounter_update(counter);
2775}
2776
Robert Richter4aeb0b42009-04-29 12:47:03 +02002777static const struct pmu perf_ops_generic = {
Peter Zijlstraac17dc82009-03-13 12:21:34 +01002778 .enable = perf_swcounter_enable,
2779 .disable = perf_swcounter_disable,
2780 .read = perf_swcounter_read,
2781};
2782
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002783/*
2784 * Software counter: cpu wall time clock
2785 */
2786
Paul Mackerras9abf8a02009-01-09 16:26:43 +11002787static void cpu_clock_perf_counter_update(struct perf_counter *counter)
2788{
2789 int cpu = raw_smp_processor_id();
2790 s64 prev;
2791 u64 now;
2792
2793 now = cpu_clock(cpu);
2794 prev = atomic64_read(&counter->hw.prev_count);
2795 atomic64_set(&counter->hw.prev_count, now);
2796 atomic64_add(now - prev, &counter->count);
2797}
2798
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002799static int cpu_clock_perf_counter_enable(struct perf_counter *counter)
2800{
2801 struct hw_perf_counter *hwc = &counter->hw;
2802 int cpu = raw_smp_processor_id();
2803
2804 atomic64_set(&hwc->prev_count, cpu_clock(cpu));
Peter Zijlstra039fc912009-03-13 16:43:47 +01002805 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
2806 hwc->hrtimer.function = perf_swcounter_hrtimer;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002807 if (hwc->irq_period) {
Peter Zijlstra60db5e02009-05-15 15:19:28 +02002808 u64 period = max_t(u64, 10000, hwc->irq_period);
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002809 __hrtimer_start_range_ns(&hwc->hrtimer,
Peter Zijlstra60db5e02009-05-15 15:19:28 +02002810 ns_to_ktime(period), 0,
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002811 HRTIMER_MODE_REL, 0);
2812 }
2813
2814 return 0;
2815}
2816
Ingo Molnar5c92d122008-12-11 13:21:10 +01002817static void cpu_clock_perf_counter_disable(struct perf_counter *counter)
2818{
Peter Zijlstrab986d7e2009-05-20 12:21:21 +02002819 if (counter->hw.irq_period)
2820 hrtimer_cancel(&counter->hw.hrtimer);
Paul Mackerras9abf8a02009-01-09 16:26:43 +11002821 cpu_clock_perf_counter_update(counter);
Ingo Molnar5c92d122008-12-11 13:21:10 +01002822}
2823
2824static void cpu_clock_perf_counter_read(struct perf_counter *counter)
2825{
Paul Mackerras9abf8a02009-01-09 16:26:43 +11002826 cpu_clock_perf_counter_update(counter);
Ingo Molnar5c92d122008-12-11 13:21:10 +01002827}
2828
Robert Richter4aeb0b42009-04-29 12:47:03 +02002829static const struct pmu perf_ops_cpu_clock = {
Ingo Molnar76715812008-12-17 14:20:28 +01002830 .enable = cpu_clock_perf_counter_enable,
2831 .disable = cpu_clock_perf_counter_disable,
2832 .read = cpu_clock_perf_counter_read,
Ingo Molnar5c92d122008-12-11 13:21:10 +01002833};
2834
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01002835/*
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002836 * Software counter: task time clock
2837 */
2838
Peter Zijlstrae30e08f2009-04-08 15:01:25 +02002839static void task_clock_perf_counter_update(struct perf_counter *counter, u64 now)
Ingo Molnarbae43c92008-12-11 14:03:20 +01002840{
Peter Zijlstrae30e08f2009-04-08 15:01:25 +02002841 u64 prev;
Ingo Molnar8cb391e2008-12-14 12:22:31 +01002842 s64 delta;
Ingo Molnarbae43c92008-12-11 14:03:20 +01002843
Peter Zijlstraa39d6f22009-04-06 11:45:11 +02002844 prev = atomic64_xchg(&counter->hw.prev_count, now);
Ingo Molnar8cb391e2008-12-14 12:22:31 +01002845 delta = now - prev;
Ingo Molnar8cb391e2008-12-14 12:22:31 +01002846 atomic64_add(delta, &counter->count);
Ingo Molnarbae43c92008-12-11 14:03:20 +01002847}
2848
Ingo Molnar95cdd2e2008-12-21 13:50:42 +01002849static int task_clock_perf_counter_enable(struct perf_counter *counter)
Ingo Molnar8cb391e2008-12-14 12:22:31 +01002850{
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002851 struct hw_perf_counter *hwc = &counter->hw;
Peter Zijlstraa39d6f22009-04-06 11:45:11 +02002852 u64 now;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002853
Peter Zijlstraa39d6f22009-04-06 11:45:11 +02002854 now = counter->ctx->time;
2855
2856 atomic64_set(&hwc->prev_count, now);
Peter Zijlstra039fc912009-03-13 16:43:47 +01002857 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
2858 hwc->hrtimer.function = perf_swcounter_hrtimer;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002859 if (hwc->irq_period) {
Peter Zijlstra60db5e02009-05-15 15:19:28 +02002860 u64 period = max_t(u64, 10000, hwc->irq_period);
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002861 __hrtimer_start_range_ns(&hwc->hrtimer,
Peter Zijlstra60db5e02009-05-15 15:19:28 +02002862 ns_to_ktime(period), 0,
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002863 HRTIMER_MODE_REL, 0);
2864 }
Ingo Molnar95cdd2e2008-12-21 13:50:42 +01002865
2866 return 0;
Ingo Molnar8cb391e2008-12-14 12:22:31 +01002867}
2868
2869static void task_clock_perf_counter_disable(struct perf_counter *counter)
2870{
Peter Zijlstrab986d7e2009-05-20 12:21:21 +02002871 if (counter->hw.irq_period)
2872 hrtimer_cancel(&counter->hw.hrtimer);
Peter Zijlstrae30e08f2009-04-08 15:01:25 +02002873 task_clock_perf_counter_update(counter, counter->ctx->time);
2874
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002875}
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01002876
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002877static void task_clock_perf_counter_read(struct perf_counter *counter)
2878{
Peter Zijlstrae30e08f2009-04-08 15:01:25 +02002879 u64 time;
2880
2881 if (!in_nmi()) {
2882 update_context_time(counter->ctx);
2883 time = counter->ctx->time;
2884 } else {
2885 u64 now = perf_clock();
2886 u64 delta = now - counter->ctx->timestamp;
2887 time = counter->ctx->time + delta;
2888 }
2889
2890 task_clock_perf_counter_update(counter, time);
Ingo Molnarbae43c92008-12-11 14:03:20 +01002891}
2892
Robert Richter4aeb0b42009-04-29 12:47:03 +02002893static const struct pmu perf_ops_task_clock = {
Ingo Molnar76715812008-12-17 14:20:28 +01002894 .enable = task_clock_perf_counter_enable,
2895 .disable = task_clock_perf_counter_disable,
2896 .read = task_clock_perf_counter_read,
Ingo Molnarbae43c92008-12-11 14:03:20 +01002897};
2898
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002899/*
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002900 * Software counter: cpu migrations
2901 */
2902
Paul Mackerras23a185c2009-02-09 22:42:47 +11002903static inline u64 get_cpu_migrations(struct perf_counter *counter)
Ingo Molnar6c594c22008-12-14 12:34:15 +01002904{
Paul Mackerras23a185c2009-02-09 22:42:47 +11002905 struct task_struct *curr = counter->ctx->task;
2906
2907 if (curr)
2908 return curr->se.nr_migrations;
2909 return cpu_nr_migrations(smp_processor_id());
Ingo Molnar6c594c22008-12-14 12:34:15 +01002910}
2911
2912static void cpu_migrations_perf_counter_update(struct perf_counter *counter)
2913{
2914 u64 prev, now;
2915 s64 delta;
2916
2917 prev = atomic64_read(&counter->hw.prev_count);
Paul Mackerras23a185c2009-02-09 22:42:47 +11002918 now = get_cpu_migrations(counter);
Ingo Molnar6c594c22008-12-14 12:34:15 +01002919
2920 atomic64_set(&counter->hw.prev_count, now);
2921
2922 delta = now - prev;
Ingo Molnar6c594c22008-12-14 12:34:15 +01002923
2924 atomic64_add(delta, &counter->count);
2925}
2926
2927static void cpu_migrations_perf_counter_read(struct perf_counter *counter)
2928{
2929 cpu_migrations_perf_counter_update(counter);
2930}
2931
Ingo Molnar95cdd2e2008-12-21 13:50:42 +01002932static int cpu_migrations_perf_counter_enable(struct perf_counter *counter)
Ingo Molnar6c594c22008-12-14 12:34:15 +01002933{
Paul Mackerrasc07c99b2009-02-13 22:10:34 +11002934 if (counter->prev_state <= PERF_COUNTER_STATE_OFF)
2935 atomic64_set(&counter->hw.prev_count,
2936 get_cpu_migrations(counter));
Ingo Molnar95cdd2e2008-12-21 13:50:42 +01002937 return 0;
Ingo Molnar6c594c22008-12-14 12:34:15 +01002938}
2939
2940static void cpu_migrations_perf_counter_disable(struct perf_counter *counter)
2941{
2942 cpu_migrations_perf_counter_update(counter);
2943}
2944
Robert Richter4aeb0b42009-04-29 12:47:03 +02002945static const struct pmu perf_ops_cpu_migrations = {
Ingo Molnar76715812008-12-17 14:20:28 +01002946 .enable = cpu_migrations_perf_counter_enable,
2947 .disable = cpu_migrations_perf_counter_disable,
2948 .read = cpu_migrations_perf_counter_read,
Ingo Molnar6c594c22008-12-14 12:34:15 +01002949};
2950
Peter Zijlstrae077df42009-03-19 20:26:17 +01002951#ifdef CONFIG_EVENT_PROFILE
2952void perf_tpcounter_event(int event_id)
2953{
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002954 struct pt_regs *regs = get_irq_regs();
2955
2956 if (!regs)
2957 regs = task_pt_regs(current);
2958
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002959 __perf_swcounter_event(PERF_TYPE_TRACEPOINT, event_id, 1, 1, regs, 0);
Peter Zijlstrae077df42009-03-19 20:26:17 +01002960}
Steven Whitehouseff7b1b42009-04-15 16:55:05 +01002961EXPORT_SYMBOL_GPL(perf_tpcounter_event);
Peter Zijlstrae077df42009-03-19 20:26:17 +01002962
2963extern int ftrace_profile_enable(int);
2964extern void ftrace_profile_disable(int);
2965
2966static void tp_perf_counter_destroy(struct perf_counter *counter)
2967{
Peter Zijlstraf4a2deb42009-03-23 18:22:06 +01002968 ftrace_profile_disable(perf_event_id(&counter->hw_event));
Peter Zijlstrae077df42009-03-19 20:26:17 +01002969}
2970
Robert Richter4aeb0b42009-04-29 12:47:03 +02002971static const struct pmu *tp_perf_counter_init(struct perf_counter *counter)
Peter Zijlstrae077df42009-03-19 20:26:17 +01002972{
Peter Zijlstraf4a2deb42009-03-23 18:22:06 +01002973 int event_id = perf_event_id(&counter->hw_event);
Peter Zijlstrae077df42009-03-19 20:26:17 +01002974 int ret;
2975
2976 ret = ftrace_profile_enable(event_id);
2977 if (ret)
2978 return NULL;
2979
2980 counter->destroy = tp_perf_counter_destroy;
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002981 counter->hw.irq_period = counter->hw_event.irq_period;
Peter Zijlstrae077df42009-03-19 20:26:17 +01002982
2983 return &perf_ops_generic;
2984}
2985#else
Robert Richter4aeb0b42009-04-29 12:47:03 +02002986static const struct pmu *tp_perf_counter_init(struct perf_counter *counter)
Peter Zijlstrae077df42009-03-19 20:26:17 +01002987{
2988 return NULL;
2989}
2990#endif
2991
Robert Richter4aeb0b42009-04-29 12:47:03 +02002992static const struct pmu *sw_perf_counter_init(struct perf_counter *counter)
Ingo Molnar5c92d122008-12-11 13:21:10 +01002993{
Robert Richter4aeb0b42009-04-29 12:47:03 +02002994 const struct pmu *pmu = NULL;
Ingo Molnar5c92d122008-12-11 13:21:10 +01002995
Paul Mackerras0475f9e2009-02-11 14:35:35 +11002996 /*
2997 * Software counters (currently) can't in general distinguish
2998 * between user, kernel and hypervisor events.
2999 * However, context switches and cpu migrations are considered
3000 * to be kernel events, and page faults are never hypervisor
3001 * events.
3002 */
Peter Zijlstraf4a2deb42009-03-23 18:22:06 +01003003 switch (perf_event_id(&counter->hw_event)) {
Ingo Molnar5c92d122008-12-11 13:21:10 +01003004 case PERF_COUNT_CPU_CLOCK:
Robert Richter4aeb0b42009-04-29 12:47:03 +02003005 pmu = &perf_ops_cpu_clock;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003006
Ingo Molnar5c92d122008-12-11 13:21:10 +01003007 break;
Ingo Molnarbae43c92008-12-11 14:03:20 +01003008 case PERF_COUNT_TASK_CLOCK:
Paul Mackerras23a185c2009-02-09 22:42:47 +11003009 /*
3010 * If the user instantiates this as a per-cpu counter,
3011 * use the cpu_clock counter instead.
3012 */
3013 if (counter->ctx->task)
Robert Richter4aeb0b42009-04-29 12:47:03 +02003014 pmu = &perf_ops_task_clock;
Paul Mackerras23a185c2009-02-09 22:42:47 +11003015 else
Robert Richter4aeb0b42009-04-29 12:47:03 +02003016 pmu = &perf_ops_cpu_clock;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003017
Ingo Molnarbae43c92008-12-11 14:03:20 +01003018 break;
Ingo Molnare06c61a2008-12-14 14:44:31 +01003019 case PERF_COUNT_PAGE_FAULTS:
Peter Zijlstraac17dc82009-03-13 12:21:34 +01003020 case PERF_COUNT_PAGE_FAULTS_MIN:
3021 case PERF_COUNT_PAGE_FAULTS_MAJ:
Ingo Molnar5d6a27d2008-12-14 12:28:33 +01003022 case PERF_COUNT_CONTEXT_SWITCHES:
Robert Richter4aeb0b42009-04-29 12:47:03 +02003023 pmu = &perf_ops_generic;
Ingo Molnar5d6a27d2008-12-14 12:28:33 +01003024 break;
Ingo Molnar6c594c22008-12-14 12:34:15 +01003025 case PERF_COUNT_CPU_MIGRATIONS:
Paul Mackerras0475f9e2009-02-11 14:35:35 +11003026 if (!counter->hw_event.exclude_kernel)
Robert Richter4aeb0b42009-04-29 12:47:03 +02003027 pmu = &perf_ops_cpu_migrations;
Ingo Molnar6c594c22008-12-14 12:34:15 +01003028 break;
Ingo Molnar5c92d122008-12-11 13:21:10 +01003029 }
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003030
Robert Richter4aeb0b42009-04-29 12:47:03 +02003031 return pmu;
Ingo Molnar5c92d122008-12-11 13:21:10 +01003032}
3033
Thomas Gleixner0793a612008-12-04 20:12:29 +01003034/*
3035 * Allocate and initialize a counter structure
3036 */
3037static struct perf_counter *
Ingo Molnar04289bb2008-12-11 08:38:42 +01003038perf_counter_alloc(struct perf_counter_hw_event *hw_event,
3039 int cpu,
Paul Mackerras23a185c2009-02-09 22:42:47 +11003040 struct perf_counter_context *ctx,
Ingo Molnar9b51f662008-12-12 13:49:45 +01003041 struct perf_counter *group_leader,
3042 gfp_t gfpflags)
Thomas Gleixner0793a612008-12-04 20:12:29 +01003043{
Robert Richter4aeb0b42009-04-29 12:47:03 +02003044 const struct pmu *pmu;
Ingo Molnar621a01e2008-12-11 12:46:46 +01003045 struct perf_counter *counter;
Peter Zijlstra60db5e02009-05-15 15:19:28 +02003046 struct hw_perf_counter *hwc;
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003047 long err;
Thomas Gleixner0793a612008-12-04 20:12:29 +01003048
Ingo Molnar9b51f662008-12-12 13:49:45 +01003049 counter = kzalloc(sizeof(*counter), gfpflags);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003050 if (!counter)
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003051 return ERR_PTR(-ENOMEM);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003052
Ingo Molnar04289bb2008-12-11 08:38:42 +01003053 /*
3054 * Single counters are their own group leaders, with an
3055 * empty sibling list:
3056 */
3057 if (!group_leader)
3058 group_leader = counter;
3059
Peter Zijlstrafccc7142009-05-23 18:28:56 +02003060 mutex_init(&counter->child_mutex);
3061 INIT_LIST_HEAD(&counter->child_list);
3062
Ingo Molnar04289bb2008-12-11 08:38:42 +01003063 INIT_LIST_HEAD(&counter->list_entry);
Peter Zijlstra592903c2009-03-13 12:21:36 +01003064 INIT_LIST_HEAD(&counter->event_entry);
Ingo Molnar04289bb2008-12-11 08:38:42 +01003065 INIT_LIST_HEAD(&counter->sibling_list);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003066 init_waitqueue_head(&counter->waitq);
3067
Peter Zijlstra7b732a72009-03-23 18:22:10 +01003068 mutex_init(&counter->mmap_mutex);
3069
Ingo Molnar9f66a382008-12-10 12:33:23 +01003070 counter->cpu = cpu;
3071 counter->hw_event = *hw_event;
Ingo Molnar04289bb2008-12-11 08:38:42 +01003072 counter->group_leader = group_leader;
Robert Richter4aeb0b42009-04-29 12:47:03 +02003073 counter->pmu = NULL;
Paul Mackerras23a185c2009-02-09 22:42:47 +11003074 counter->ctx = ctx;
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10003075 get_ctx(ctx);
Ingo Molnar621a01e2008-12-11 12:46:46 +01003076
Ingo Molnar235c7fc2008-12-21 14:43:25 +01003077 counter->state = PERF_COUNTER_STATE_INACTIVE;
Ingo Molnara86ed502008-12-17 00:43:10 +01003078 if (hw_event->disabled)
3079 counter->state = PERF_COUNTER_STATE_OFF;
3080
Robert Richter4aeb0b42009-04-29 12:47:03 +02003081 pmu = NULL;
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003082
Peter Zijlstra60db5e02009-05-15 15:19:28 +02003083 hwc = &counter->hw;
3084 if (hw_event->freq && hw_event->irq_freq)
Peter Zijlstra2e569d32009-05-15 15:37:47 +02003085 hwc->irq_period = div64_u64(TICK_NSEC, hw_event->irq_freq);
Peter Zijlstra60db5e02009-05-15 15:19:28 +02003086 else
3087 hwc->irq_period = hw_event->irq_period;
3088
Peter Zijlstra2023b352009-05-05 17:50:26 +02003089 /*
3090 * we currently do not support PERF_RECORD_GROUP on inherited counters
3091 */
3092 if (hw_event->inherit && (hw_event->record_type & PERF_RECORD_GROUP))
3093 goto done;
3094
Peter Zijlstraf4a2deb42009-03-23 18:22:06 +01003095 if (perf_event_raw(hw_event)) {
Robert Richter4aeb0b42009-04-29 12:47:03 +02003096 pmu = hw_perf_counter_init(counter);
Peter Zijlstraf4a2deb42009-03-23 18:22:06 +01003097 goto done;
3098 }
3099
3100 switch (perf_event_type(hw_event)) {
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003101 case PERF_TYPE_HARDWARE:
Robert Richter4aeb0b42009-04-29 12:47:03 +02003102 pmu = hw_perf_counter_init(counter);
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003103 break;
3104
3105 case PERF_TYPE_SOFTWARE:
Robert Richter4aeb0b42009-04-29 12:47:03 +02003106 pmu = sw_perf_counter_init(counter);
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003107 break;
3108
3109 case PERF_TYPE_TRACEPOINT:
Robert Richter4aeb0b42009-04-29 12:47:03 +02003110 pmu = tp_perf_counter_init(counter);
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003111 break;
3112 }
Peter Zijlstraf4a2deb42009-03-23 18:22:06 +01003113done:
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003114 err = 0;
Robert Richter4aeb0b42009-04-29 12:47:03 +02003115 if (!pmu)
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003116 err = -EINVAL;
Robert Richter4aeb0b42009-04-29 12:47:03 +02003117 else if (IS_ERR(pmu))
3118 err = PTR_ERR(pmu);
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003119
3120 if (err) {
3121 kfree(counter);
3122 return ERR_PTR(err);
3123 }
3124
Robert Richter4aeb0b42009-04-29 12:47:03 +02003125 counter->pmu = pmu;
Thomas Gleixner0793a612008-12-04 20:12:29 +01003126
Peter Zijlstra7fc23a52009-05-08 18:52:21 +02003127 atomic_inc(&nr_counters);
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02003128 if (counter->hw_event.mmap)
3129 atomic_inc(&nr_mmap_tracking);
3130 if (counter->hw_event.munmap)
3131 atomic_inc(&nr_munmap_tracking);
3132 if (counter->hw_event.comm)
3133 atomic_inc(&nr_comm_tracking);
3134
Thomas Gleixner0793a612008-12-04 20:12:29 +01003135 return counter;
3136}
3137
3138/**
Paul Mackerras2743a5b2009-03-04 20:36:51 +11003139 * sys_perf_counter_open - open a performance counter, associate it to a task/cpu
Ingo Molnar9f66a382008-12-10 12:33:23 +01003140 *
3141 * @hw_event_uptr: event type attributes for monitoring/sampling
Thomas Gleixner0793a612008-12-04 20:12:29 +01003142 * @pid: target pid
Ingo Molnar9f66a382008-12-10 12:33:23 +01003143 * @cpu: target cpu
3144 * @group_fd: group leader counter fd
Thomas Gleixner0793a612008-12-04 20:12:29 +01003145 */
Paul Mackerras2743a5b2009-03-04 20:36:51 +11003146SYSCALL_DEFINE5(perf_counter_open,
Paul Mackerrasf3dfd262009-02-26 22:43:46 +11003147 const struct perf_counter_hw_event __user *, hw_event_uptr,
Paul Mackerras2743a5b2009-03-04 20:36:51 +11003148 pid_t, pid, int, cpu, int, group_fd, unsigned long, flags)
Thomas Gleixner0793a612008-12-04 20:12:29 +01003149{
Ingo Molnar04289bb2008-12-11 08:38:42 +01003150 struct perf_counter *counter, *group_leader;
Ingo Molnar9f66a382008-12-10 12:33:23 +01003151 struct perf_counter_hw_event hw_event;
Ingo Molnar04289bb2008-12-11 08:38:42 +01003152 struct perf_counter_context *ctx;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003153 struct file *counter_file = NULL;
Ingo Molnar04289bb2008-12-11 08:38:42 +01003154 struct file *group_file = NULL;
3155 int fput_needed = 0;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003156 int fput_needed2 = 0;
Thomas Gleixner0793a612008-12-04 20:12:29 +01003157 int ret;
3158
Paul Mackerras2743a5b2009-03-04 20:36:51 +11003159 /* for future expandability... */
3160 if (flags)
3161 return -EINVAL;
3162
Ingo Molnar9f66a382008-12-10 12:33:23 +01003163 if (copy_from_user(&hw_event, hw_event_uptr, sizeof(hw_event)) != 0)
Thomas Gleixnereab656a2008-12-08 19:26:59 +01003164 return -EFAULT;
3165
Ingo Molnar04289bb2008-12-11 08:38:42 +01003166 /*
Ingo Molnarccff2862008-12-11 11:26:29 +01003167 * Get the target context (task or percpu):
3168 */
3169 ctx = find_get_context(pid, cpu);
3170 if (IS_ERR(ctx))
3171 return PTR_ERR(ctx);
3172
3173 /*
3174 * Look up the group leader (we will attach this counter to it):
Ingo Molnar04289bb2008-12-11 08:38:42 +01003175 */
3176 group_leader = NULL;
3177 if (group_fd != -1) {
3178 ret = -EINVAL;
3179 group_file = fget_light(group_fd, &fput_needed);
3180 if (!group_file)
Ingo Molnarccff2862008-12-11 11:26:29 +01003181 goto err_put_context;
Ingo Molnar04289bb2008-12-11 08:38:42 +01003182 if (group_file->f_op != &perf_fops)
Ingo Molnarccff2862008-12-11 11:26:29 +01003183 goto err_put_context;
Ingo Molnar04289bb2008-12-11 08:38:42 +01003184
3185 group_leader = group_file->private_data;
3186 /*
Ingo Molnarccff2862008-12-11 11:26:29 +01003187 * Do not allow a recursive hierarchy (this new sibling
3188 * becoming part of another group-sibling):
Ingo Molnar04289bb2008-12-11 08:38:42 +01003189 */
Ingo Molnarccff2862008-12-11 11:26:29 +01003190 if (group_leader->group_leader != group_leader)
3191 goto err_put_context;
3192 /*
3193 * Do not allow to attach to a group in a different
3194 * task or CPU context:
3195 */
3196 if (group_leader->ctx != ctx)
3197 goto err_put_context;
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11003198 /*
3199 * Only a group leader can be exclusive or pinned
3200 */
3201 if (hw_event.exclusive || hw_event.pinned)
3202 goto err_put_context;
Ingo Molnar04289bb2008-12-11 08:38:42 +01003203 }
3204
Paul Mackerras23a185c2009-02-09 22:42:47 +11003205 counter = perf_counter_alloc(&hw_event, cpu, ctx, group_leader,
3206 GFP_KERNEL);
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003207 ret = PTR_ERR(counter);
3208 if (IS_ERR(counter))
Thomas Gleixner0793a612008-12-04 20:12:29 +01003209 goto err_put_context;
3210
Thomas Gleixner0793a612008-12-04 20:12:29 +01003211 ret = anon_inode_getfd("[perf_counter]", &perf_fops, counter, 0);
3212 if (ret < 0)
Ingo Molnar9b51f662008-12-12 13:49:45 +01003213 goto err_free_put_context;
3214
3215 counter_file = fget_light(ret, &fput_needed2);
3216 if (!counter_file)
3217 goto err_free_put_context;
3218
3219 counter->filp = counter_file;
Paul Mackerrasd859e292009-01-17 18:10:22 +11003220 mutex_lock(&ctx->mutex);
Ingo Molnar9b51f662008-12-12 13:49:45 +01003221 perf_install_in_context(ctx, counter, cpu);
Paul Mackerrasd859e292009-01-17 18:10:22 +11003222 mutex_unlock(&ctx->mutex);
Ingo Molnar9b51f662008-12-12 13:49:45 +01003223
Peter Zijlstra082ff5a2009-05-23 18:29:00 +02003224 counter->owner = current;
3225 get_task_struct(current);
3226 mutex_lock(&current->perf_counter_mutex);
3227 list_add_tail(&counter->owner_entry, &current->perf_counter_list);
3228 mutex_unlock(&current->perf_counter_mutex);
3229
Ingo Molnar9b51f662008-12-12 13:49:45 +01003230 fput_light(counter_file, fput_needed2);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003231
Ingo Molnar04289bb2008-12-11 08:38:42 +01003232out_fput:
3233 fput_light(group_file, fput_needed);
3234
Thomas Gleixner0793a612008-12-04 20:12:29 +01003235 return ret;
3236
Ingo Molnar9b51f662008-12-12 13:49:45 +01003237err_free_put_context:
Thomas Gleixner0793a612008-12-04 20:12:29 +01003238 kfree(counter);
3239
3240err_put_context:
3241 put_context(ctx);
3242
Ingo Molnar04289bb2008-12-11 08:38:42 +01003243 goto out_fput;
Thomas Gleixner0793a612008-12-04 20:12:29 +01003244}
3245
Ingo Molnar9b51f662008-12-12 13:49:45 +01003246/*
Ingo Molnar9b51f662008-12-12 13:49:45 +01003247 * inherit a counter from parent task to child task:
3248 */
Paul Mackerrasd859e292009-01-17 18:10:22 +11003249static struct perf_counter *
Ingo Molnar9b51f662008-12-12 13:49:45 +01003250inherit_counter(struct perf_counter *parent_counter,
3251 struct task_struct *parent,
3252 struct perf_counter_context *parent_ctx,
3253 struct task_struct *child,
Paul Mackerrasd859e292009-01-17 18:10:22 +11003254 struct perf_counter *group_leader,
Ingo Molnar9b51f662008-12-12 13:49:45 +01003255 struct perf_counter_context *child_ctx)
3256{
3257 struct perf_counter *child_counter;
3258
Paul Mackerrasd859e292009-01-17 18:10:22 +11003259 /*
3260 * Instead of creating recursive hierarchies of counters,
3261 * we link inherited counters back to the original parent,
3262 * which has a filp for sure, which we use as the reference
3263 * count:
3264 */
3265 if (parent_counter->parent)
3266 parent_counter = parent_counter->parent;
3267
Ingo Molnar9b51f662008-12-12 13:49:45 +01003268 child_counter = perf_counter_alloc(&parent_counter->hw_event,
Paul Mackerras23a185c2009-02-09 22:42:47 +11003269 parent_counter->cpu, child_ctx,
3270 group_leader, GFP_KERNEL);
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003271 if (IS_ERR(child_counter))
3272 return child_counter;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003273
3274 /*
Paul Mackerras564c2b22009-05-22 14:27:22 +10003275 * Make the child state follow the state of the parent counter,
3276 * not its hw_event.disabled bit. We hold the parent's mutex,
3277 * so we won't race with perf_counter_{en,dis}able_family.
3278 */
3279 if (parent_counter->state >= PERF_COUNTER_STATE_INACTIVE)
3280 child_counter->state = PERF_COUNTER_STATE_INACTIVE;
3281 else
3282 child_counter->state = PERF_COUNTER_STATE_OFF;
3283
3284 /*
Ingo Molnar9b51f662008-12-12 13:49:45 +01003285 * Link it up in the child's context:
3286 */
Paul Mackerras53cfbf52009-03-25 22:46:58 +11003287 add_counter_to_ctx(child_counter, child_ctx);
Ingo Molnar9b51f662008-12-12 13:49:45 +01003288
3289 child_counter->parent = parent_counter;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003290 /*
3291 * inherit into child's child as well:
3292 */
3293 child_counter->hw_event.inherit = 1;
3294
3295 /*
3296 * Get a reference to the parent filp - we will fput it
3297 * when the child counter exits. This is safe to do because
3298 * we are in the parent and we know that the filp still
3299 * exists and has a nonzero count:
3300 */
3301 atomic_long_inc(&parent_counter->filp->f_count);
3302
Paul Mackerrasd859e292009-01-17 18:10:22 +11003303 /*
3304 * Link this into the parent counter's child list
3305 */
Peter Zijlstrafccc7142009-05-23 18:28:56 +02003306 mutex_lock(&parent_counter->child_mutex);
Paul Mackerrasd859e292009-01-17 18:10:22 +11003307 list_add_tail(&child_counter->child_list, &parent_counter->child_list);
Peter Zijlstrafccc7142009-05-23 18:28:56 +02003308 mutex_unlock(&parent_counter->child_mutex);
Paul Mackerrasd859e292009-01-17 18:10:22 +11003309
3310 return child_counter;
3311}
3312
3313static int inherit_group(struct perf_counter *parent_counter,
3314 struct task_struct *parent,
3315 struct perf_counter_context *parent_ctx,
3316 struct task_struct *child,
3317 struct perf_counter_context *child_ctx)
3318{
3319 struct perf_counter *leader;
3320 struct perf_counter *sub;
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003321 struct perf_counter *child_ctr;
Paul Mackerrasd859e292009-01-17 18:10:22 +11003322
3323 leader = inherit_counter(parent_counter, parent, parent_ctx,
3324 child, NULL, child_ctx);
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003325 if (IS_ERR(leader))
3326 return PTR_ERR(leader);
Paul Mackerrasd859e292009-01-17 18:10:22 +11003327 list_for_each_entry(sub, &parent_counter->sibling_list, list_entry) {
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003328 child_ctr = inherit_counter(sub, parent, parent_ctx,
3329 child, leader, child_ctx);
3330 if (IS_ERR(child_ctr))
3331 return PTR_ERR(child_ctr);
Paul Mackerrasd859e292009-01-17 18:10:22 +11003332 }
Ingo Molnar9b51f662008-12-12 13:49:45 +01003333 return 0;
3334}
3335
Paul Mackerrasd859e292009-01-17 18:10:22 +11003336static void sync_child_counter(struct perf_counter *child_counter,
3337 struct perf_counter *parent_counter)
3338{
Peter Zijlstra8bc20952009-05-15 20:45:59 +02003339 u64 child_val;
Paul Mackerrasd859e292009-01-17 18:10:22 +11003340
Paul Mackerrasd859e292009-01-17 18:10:22 +11003341 child_val = atomic64_read(&child_counter->count);
3342
3343 /*
3344 * Add back the child's count to the parent's count:
3345 */
3346 atomic64_add(child_val, &parent_counter->count);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11003347 atomic64_add(child_counter->total_time_enabled,
3348 &parent_counter->child_total_time_enabled);
3349 atomic64_add(child_counter->total_time_running,
3350 &parent_counter->child_total_time_running);
Paul Mackerrasd859e292009-01-17 18:10:22 +11003351
3352 /*
3353 * Remove this counter from the parent's list
3354 */
Peter Zijlstrafccc7142009-05-23 18:28:56 +02003355 mutex_lock(&parent_counter->child_mutex);
Paul Mackerrasd859e292009-01-17 18:10:22 +11003356 list_del_init(&child_counter->child_list);
Peter Zijlstrafccc7142009-05-23 18:28:56 +02003357 mutex_unlock(&parent_counter->child_mutex);
Paul Mackerrasd859e292009-01-17 18:10:22 +11003358
3359 /*
3360 * Release the parent counter, if this was the last
3361 * reference to it.
3362 */
3363 fput(parent_counter->filp);
3364}
3365
Ingo Molnar9b51f662008-12-12 13:49:45 +01003366static void
3367__perf_counter_exit_task(struct task_struct *child,
3368 struct perf_counter *child_counter,
3369 struct perf_counter_context *child_ctx)
3370{
3371 struct perf_counter *parent_counter;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003372
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10003373 update_counter_times(child_counter);
Peter Zijlstraaa9c67f2009-05-23 18:28:59 +02003374 perf_counter_remove_from_context(child_counter);
Ingo Molnar0cc0c022008-12-14 23:20:36 +01003375
Ingo Molnar9b51f662008-12-12 13:49:45 +01003376 parent_counter = child_counter->parent;
3377 /*
3378 * It can happen that parent exits first, and has counters
3379 * that are still around due to the child reference. These
3380 * counters need to be zapped - but otherwise linger.
3381 */
Paul Mackerrasd859e292009-01-17 18:10:22 +11003382 if (parent_counter) {
3383 sync_child_counter(child_counter, parent_counter);
Peter Zijlstraf1600952009-03-19 20:26:16 +01003384 free_counter(child_counter);
Paul Mackerras4bcf3492009-02-11 13:53:19 +01003385 }
Ingo Molnar9b51f662008-12-12 13:49:45 +01003386}
3387
3388/*
Paul Mackerrasd859e292009-01-17 18:10:22 +11003389 * When a child task exits, feed back counter values to parent counters.
Ingo Molnar9b51f662008-12-12 13:49:45 +01003390 *
Paul Mackerrasd859e292009-01-17 18:10:22 +11003391 * Note: we may be running in child context, but the PID is not hashed
Ingo Molnar9b51f662008-12-12 13:49:45 +01003392 * anymore so new counters will not be added.
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10003393 * (XXX not sure that is true when we get called from flush_old_exec.
3394 * -- paulus)
Ingo Molnar9b51f662008-12-12 13:49:45 +01003395 */
3396void perf_counter_exit_task(struct task_struct *child)
3397{
3398 struct perf_counter *child_counter, *tmp;
3399 struct perf_counter_context *child_ctx;
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10003400 unsigned long flags;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003401
Ingo Molnar33b2fb32009-05-17 11:08:41 +02003402 WARN_ON_ONCE(child != current);
3403
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10003404 child_ctx = child->perf_counter_ctxp;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003405
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10003406 if (likely(!child_ctx))
Ingo Molnar9b51f662008-12-12 13:49:45 +01003407 return;
3408
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10003409 local_irq_save(flags);
3410 __perf_counter_task_sched_out(child_ctx);
3411 child->perf_counter_ctxp = NULL;
3412 local_irq_restore(flags);
3413
3414 mutex_lock(&child_ctx->mutex);
3415
Peter Zijlstra8bc20952009-05-15 20:45:59 +02003416again:
Ingo Molnar9b51f662008-12-12 13:49:45 +01003417 list_for_each_entry_safe(child_counter, tmp, &child_ctx->counter_list,
3418 list_entry)
3419 __perf_counter_exit_task(child, child_counter, child_ctx);
Peter Zijlstra8bc20952009-05-15 20:45:59 +02003420
3421 /*
3422 * If the last counter was a group counter, it will have appended all
3423 * its siblings to the list, but we obtained 'tmp' before that which
3424 * will still point to the list head terminating the iteration.
3425 */
3426 if (!list_empty(&child_ctx->counter_list))
3427 goto again;
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10003428
3429 mutex_unlock(&child_ctx->mutex);
3430
3431 put_ctx(child_ctx);
Ingo Molnar9b51f662008-12-12 13:49:45 +01003432}
3433
3434/*
3435 * Initialize the perf_counter context in task_struct
3436 */
3437void perf_counter_init_task(struct task_struct *child)
3438{
3439 struct perf_counter_context *child_ctx, *parent_ctx;
Paul Mackerrasd859e292009-01-17 18:10:22 +11003440 struct perf_counter *counter;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003441 struct task_struct *parent = current;
Paul Mackerras564c2b22009-05-22 14:27:22 +10003442 int inherited_all = 1;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003443
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10003444 child->perf_counter_ctxp = NULL;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003445
Peter Zijlstra082ff5a2009-05-23 18:29:00 +02003446 mutex_init(&child->perf_counter_mutex);
3447 INIT_LIST_HEAD(&child->perf_counter_list);
3448
Ingo Molnar9b51f662008-12-12 13:49:45 +01003449 /*
3450 * This is executed from the parent task context, so inherit
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10003451 * counters that have been marked for cloning.
3452 * First allocate and initialize a context for the child.
Ingo Molnar9b51f662008-12-12 13:49:45 +01003453 */
3454
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10003455 child_ctx = kmalloc(sizeof(struct perf_counter_context), GFP_KERNEL);
3456 if (!child_ctx)
Ingo Molnar9b51f662008-12-12 13:49:45 +01003457 return;
3458
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10003459 parent_ctx = parent->perf_counter_ctxp;
3460 if (likely(!parent_ctx || !parent_ctx->nr_counters))
3461 return;
3462
3463 __perf_counter_init_context(child_ctx, child);
3464 child->perf_counter_ctxp = child_ctx;
3465
Ingo Molnar9b51f662008-12-12 13:49:45 +01003466 /*
3467 * Lock the parent list. No need to lock the child - not PID
3468 * hashed yet and not running, so nobody can access it.
3469 */
Paul Mackerrasd859e292009-01-17 18:10:22 +11003470 mutex_lock(&parent_ctx->mutex);
Ingo Molnar9b51f662008-12-12 13:49:45 +01003471
3472 /*
3473 * We dont have to disable NMIs - we are only looking at
3474 * the list, not manipulating it:
3475 */
Peter Zijlstrad7b629a2009-05-20 12:21:19 +02003476 list_for_each_entry_rcu(counter, &parent_ctx->event_list, event_entry) {
3477 if (counter != counter->group_leader)
3478 continue;
3479
Paul Mackerras564c2b22009-05-22 14:27:22 +10003480 if (!counter->hw_event.inherit) {
3481 inherited_all = 0;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003482 continue;
Paul Mackerras564c2b22009-05-22 14:27:22 +10003483 }
Ingo Molnar9b51f662008-12-12 13:49:45 +01003484
Paul Mackerrasd859e292009-01-17 18:10:22 +11003485 if (inherit_group(counter, parent,
Paul Mackerras564c2b22009-05-22 14:27:22 +10003486 parent_ctx, child, child_ctx)) {
3487 inherited_all = 0;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003488 break;
Paul Mackerras564c2b22009-05-22 14:27:22 +10003489 }
3490 }
3491
3492 if (inherited_all) {
3493 /*
3494 * Mark the child context as a clone of the parent
3495 * context, or of whatever the parent is a clone of.
3496 */
3497 if (parent_ctx->parent_ctx) {
3498 child_ctx->parent_ctx = parent_ctx->parent_ctx;
3499 child_ctx->parent_gen = parent_ctx->parent_gen;
3500 } else {
3501 child_ctx->parent_ctx = parent_ctx;
3502 child_ctx->parent_gen = parent_ctx->generation;
3503 }
3504 get_ctx(child_ctx->parent_ctx);
Ingo Molnar9b51f662008-12-12 13:49:45 +01003505 }
3506
Paul Mackerrasd859e292009-01-17 18:10:22 +11003507 mutex_unlock(&parent_ctx->mutex);
Ingo Molnar9b51f662008-12-12 13:49:45 +01003508}
3509
Ingo Molnar04289bb2008-12-11 08:38:42 +01003510static void __cpuinit perf_counter_init_cpu(int cpu)
Thomas Gleixner0793a612008-12-04 20:12:29 +01003511{
Ingo Molnar04289bb2008-12-11 08:38:42 +01003512 struct perf_cpu_context *cpuctx;
Thomas Gleixner0793a612008-12-04 20:12:29 +01003513
Ingo Molnar04289bb2008-12-11 08:38:42 +01003514 cpuctx = &per_cpu(perf_cpu_context, cpu);
3515 __perf_counter_init_context(&cpuctx->ctx, NULL);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003516
Ingo Molnar1dce8d92009-05-04 19:23:18 +02003517 spin_lock(&perf_resource_lock);
Ingo Molnar04289bb2008-12-11 08:38:42 +01003518 cpuctx->max_pertask = perf_max_counters - perf_reserved_percpu;
Ingo Molnar1dce8d92009-05-04 19:23:18 +02003519 spin_unlock(&perf_resource_lock);
Ingo Molnar04289bb2008-12-11 08:38:42 +01003520
Paul Mackerras01d02872009-01-14 13:44:19 +11003521 hw_perf_counter_setup(cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003522}
3523
3524#ifdef CONFIG_HOTPLUG_CPU
Ingo Molnar04289bb2008-12-11 08:38:42 +01003525static void __perf_counter_exit_cpu(void *info)
Thomas Gleixner0793a612008-12-04 20:12:29 +01003526{
3527 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
3528 struct perf_counter_context *ctx = &cpuctx->ctx;
3529 struct perf_counter *counter, *tmp;
3530
Ingo Molnar04289bb2008-12-11 08:38:42 +01003531 list_for_each_entry_safe(counter, tmp, &ctx->counter_list, list_entry)
3532 __perf_counter_remove_from_context(counter);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003533}
Ingo Molnar04289bb2008-12-11 08:38:42 +01003534static void perf_counter_exit_cpu(int cpu)
Thomas Gleixner0793a612008-12-04 20:12:29 +01003535{
Paul Mackerrasd859e292009-01-17 18:10:22 +11003536 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
3537 struct perf_counter_context *ctx = &cpuctx->ctx;
3538
3539 mutex_lock(&ctx->mutex);
Ingo Molnar04289bb2008-12-11 08:38:42 +01003540 smp_call_function_single(cpu, __perf_counter_exit_cpu, NULL, 1);
Paul Mackerrasd859e292009-01-17 18:10:22 +11003541 mutex_unlock(&ctx->mutex);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003542}
3543#else
Ingo Molnar04289bb2008-12-11 08:38:42 +01003544static inline void perf_counter_exit_cpu(int cpu) { }
Thomas Gleixner0793a612008-12-04 20:12:29 +01003545#endif
3546
3547static int __cpuinit
3548perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
3549{
3550 unsigned int cpu = (long)hcpu;
3551
3552 switch (action) {
3553
3554 case CPU_UP_PREPARE:
3555 case CPU_UP_PREPARE_FROZEN:
Ingo Molnar04289bb2008-12-11 08:38:42 +01003556 perf_counter_init_cpu(cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003557 break;
3558
3559 case CPU_DOWN_PREPARE:
3560 case CPU_DOWN_PREPARE_FROZEN:
Ingo Molnar04289bb2008-12-11 08:38:42 +01003561 perf_counter_exit_cpu(cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003562 break;
3563
3564 default:
3565 break;
3566 }
3567
3568 return NOTIFY_OK;
3569}
3570
3571static struct notifier_block __cpuinitdata perf_cpu_nb = {
3572 .notifier_call = perf_cpu_notify,
3573};
3574
Ingo Molnar0d905bc2009-05-04 19:13:30 +02003575void __init perf_counter_init(void)
Thomas Gleixner0793a612008-12-04 20:12:29 +01003576{
3577 perf_cpu_notify(&perf_cpu_nb, (unsigned long)CPU_UP_PREPARE,
3578 (void *)(long)smp_processor_id());
3579 register_cpu_notifier(&perf_cpu_nb);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003580}
Thomas Gleixner0793a612008-12-04 20:12:29 +01003581
3582static ssize_t perf_show_reserve_percpu(struct sysdev_class *class, char *buf)
3583{
3584 return sprintf(buf, "%d\n", perf_reserved_percpu);
3585}
3586
3587static ssize_t
3588perf_set_reserve_percpu(struct sysdev_class *class,
3589 const char *buf,
3590 size_t count)
3591{
3592 struct perf_cpu_context *cpuctx;
3593 unsigned long val;
3594 int err, cpu, mpt;
3595
3596 err = strict_strtoul(buf, 10, &val);
3597 if (err)
3598 return err;
3599 if (val > perf_max_counters)
3600 return -EINVAL;
3601
Ingo Molnar1dce8d92009-05-04 19:23:18 +02003602 spin_lock(&perf_resource_lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003603 perf_reserved_percpu = val;
3604 for_each_online_cpu(cpu) {
3605 cpuctx = &per_cpu(perf_cpu_context, cpu);
3606 spin_lock_irq(&cpuctx->ctx.lock);
3607 mpt = min(perf_max_counters - cpuctx->ctx.nr_counters,
3608 perf_max_counters - perf_reserved_percpu);
3609 cpuctx->max_pertask = mpt;
3610 spin_unlock_irq(&cpuctx->ctx.lock);
3611 }
Ingo Molnar1dce8d92009-05-04 19:23:18 +02003612 spin_unlock(&perf_resource_lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003613
3614 return count;
3615}
3616
3617static ssize_t perf_show_overcommit(struct sysdev_class *class, char *buf)
3618{
3619 return sprintf(buf, "%d\n", perf_overcommit);
3620}
3621
3622static ssize_t
3623perf_set_overcommit(struct sysdev_class *class, const char *buf, size_t count)
3624{
3625 unsigned long val;
3626 int err;
3627
3628 err = strict_strtoul(buf, 10, &val);
3629 if (err)
3630 return err;
3631 if (val > 1)
3632 return -EINVAL;
3633
Ingo Molnar1dce8d92009-05-04 19:23:18 +02003634 spin_lock(&perf_resource_lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003635 perf_overcommit = val;
Ingo Molnar1dce8d92009-05-04 19:23:18 +02003636 spin_unlock(&perf_resource_lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003637
3638 return count;
3639}
3640
3641static SYSDEV_CLASS_ATTR(
3642 reserve_percpu,
3643 0644,
3644 perf_show_reserve_percpu,
3645 perf_set_reserve_percpu
3646 );
3647
3648static SYSDEV_CLASS_ATTR(
3649 overcommit,
3650 0644,
3651 perf_show_overcommit,
3652 perf_set_overcommit
3653 );
3654
3655static struct attribute *perfclass_attrs[] = {
3656 &attr_reserve_percpu.attr,
3657 &attr_overcommit.attr,
3658 NULL
3659};
3660
3661static struct attribute_group perfclass_attr_group = {
3662 .attrs = perfclass_attrs,
3663 .name = "perf_counters",
3664};
3665
3666static int __init perf_counter_sysfs_init(void)
3667{
3668 return sysfs_create_group(&cpu_sysdev_class.kset.kobj,
3669 &perfclass_attr_group);
3670}
3671device_initcall(perf_counter_sysfs_init);