blob: 06ea3eae886e3b0c0343c3a3ab02795e39504cce [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{
107 if (atomic_dec_and_test(&ctx->refcount))
108 kfree(ctx);
109}
110
Ingo Molnar04289bb2008-12-11 08:38:42 +0100111static void
112list_add_counter(struct perf_counter *counter, struct perf_counter_context *ctx)
113{
114 struct perf_counter *group_leader = counter->group_leader;
115
116 /*
117 * Depending on whether it is a standalone or sibling counter,
118 * add it straight to the context's counter list, or to the group
119 * leader's sibling list:
120 */
Peter Zijlstra3df5eda2009-05-08 18:52:22 +0200121 if (group_leader == counter)
Ingo Molnar04289bb2008-12-11 08:38:42 +0100122 list_add_tail(&counter->list_entry, &ctx->counter_list);
Peter Zijlstra5c148192009-03-25 12:30:23 +0100123 else {
Ingo Molnar04289bb2008-12-11 08:38:42 +0100124 list_add_tail(&counter->list_entry, &group_leader->sibling_list);
Peter Zijlstra5c148192009-03-25 12:30:23 +0100125 group_leader->nr_siblings++;
126 }
Peter Zijlstra592903c2009-03-13 12:21:36 +0100127
128 list_add_rcu(&counter->event_entry, &ctx->event_list);
Peter Zijlstra8bc20952009-05-15 20:45:59 +0200129 ctx->nr_counters++;
Ingo Molnar04289bb2008-12-11 08:38:42 +0100130}
131
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000132/*
133 * Remove a counter from the lists for its context.
134 * Must be called with counter->mutex and ctx->mutex held.
135 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100136static void
137list_del_counter(struct perf_counter *counter, struct perf_counter_context *ctx)
138{
139 struct perf_counter *sibling, *tmp;
140
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000141 if (list_empty(&counter->list_entry))
142 return;
Peter Zijlstra8bc20952009-05-15 20:45:59 +0200143 ctx->nr_counters--;
144
Ingo Molnar04289bb2008-12-11 08:38:42 +0100145 list_del_init(&counter->list_entry);
Peter Zijlstra592903c2009-03-13 12:21:36 +0100146 list_del_rcu(&counter->event_entry);
Ingo Molnar04289bb2008-12-11 08:38:42 +0100147
Peter Zijlstra5c148192009-03-25 12:30:23 +0100148 if (counter->group_leader != counter)
149 counter->group_leader->nr_siblings--;
150
Ingo Molnar04289bb2008-12-11 08:38:42 +0100151 /*
152 * If this was a group counter with sibling counters then
153 * upgrade the siblings to singleton counters by adding them
154 * to the context list directly:
155 */
156 list_for_each_entry_safe(sibling, tmp,
157 &counter->sibling_list, list_entry) {
158
Peter Zijlstra75564232009-03-13 12:21:29 +0100159 list_move_tail(&sibling->list_entry, &ctx->counter_list);
Ingo Molnar04289bb2008-12-11 08:38:42 +0100160 sibling->group_leader = sibling;
161 }
162}
163
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100164static void
165counter_sched_out(struct perf_counter *counter,
166 struct perf_cpu_context *cpuctx,
167 struct perf_counter_context *ctx)
168{
169 if (counter->state != PERF_COUNTER_STATE_ACTIVE)
170 return;
171
172 counter->state = PERF_COUNTER_STATE_INACTIVE;
Peter Zijlstra4af49982009-04-06 11:45:10 +0200173 counter->tstamp_stopped = ctx->time;
Robert Richter4aeb0b42009-04-29 12:47:03 +0200174 counter->pmu->disable(counter);
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100175 counter->oncpu = -1;
176
177 if (!is_software_counter(counter))
178 cpuctx->active_oncpu--;
179 ctx->nr_active--;
180 if (counter->hw_event.exclusive || !cpuctx->active_oncpu)
181 cpuctx->exclusive = 0;
182}
183
Paul Mackerrasd859e292009-01-17 18:10:22 +1100184static void
185group_sched_out(struct perf_counter *group_counter,
186 struct perf_cpu_context *cpuctx,
187 struct perf_counter_context *ctx)
188{
189 struct perf_counter *counter;
190
191 if (group_counter->state != PERF_COUNTER_STATE_ACTIVE)
192 return;
193
194 counter_sched_out(group_counter, cpuctx, ctx);
195
196 /*
197 * Schedule out siblings (if any):
198 */
199 list_for_each_entry(counter, &group_counter->sibling_list, list_entry)
200 counter_sched_out(counter, cpuctx, ctx);
201
202 if (group_counter->hw_event.exclusive)
203 cpuctx->exclusive = 0;
204}
205
Thomas Gleixner0793a612008-12-04 20:12:29 +0100206/*
207 * Cross CPU call to remove a performance counter
208 *
209 * We disable the counter on the hardware level first. After that we
210 * remove it from the context list.
211 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100212static void __perf_counter_remove_from_context(void *info)
Thomas Gleixner0793a612008-12-04 20:12:29 +0100213{
214 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
215 struct perf_counter *counter = info;
216 struct perf_counter_context *ctx = counter->ctx;
Ingo Molnar9b51f662008-12-12 13:49:45 +0100217 unsigned long flags;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100218
219 /*
220 * If this is a task context, we need to check whether it is
221 * the current task context of this cpu. If not it has been
222 * scheduled out before the smp call arrived.
223 */
224 if (ctx->task && cpuctx->task_ctx != ctx)
225 return;
226
Peter Zijlstra849691a2009-04-06 11:45:12 +0200227 spin_lock_irqsave(&ctx->lock, flags);
Ingo Molnar34adc802009-05-20 20:13:28 +0200228 /*
229 * Protect the list operation against NMI by disabling the
230 * counters on a global level.
231 */
232 perf_disable();
Thomas Gleixner0793a612008-12-04 20:12:29 +0100233
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100234 counter_sched_out(counter, cpuctx, ctx);
235
Ingo Molnar04289bb2008-12-11 08:38:42 +0100236 list_del_counter(counter, ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100237
238 if (!ctx->task) {
239 /*
240 * Allow more per task counters with respect to the
241 * reservation:
242 */
243 cpuctx->max_pertask =
244 min(perf_max_counters - ctx->nr_counters,
245 perf_max_counters - perf_reserved_percpu);
246 }
247
Ingo Molnar34adc802009-05-20 20:13:28 +0200248 perf_enable();
Peter Zijlstra849691a2009-04-06 11:45:12 +0200249 spin_unlock_irqrestore(&ctx->lock, flags);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100250}
251
252
253/*
254 * Remove the counter from a task's (or a CPU's) list of counters.
255 *
Paul Mackerrasd859e292009-01-17 18:10:22 +1100256 * Must be called with counter->mutex and ctx->mutex held.
Thomas Gleixner0793a612008-12-04 20:12:29 +0100257 *
258 * CPU counters are removed with a smp call. For task counters we only
259 * call when the task is on a CPU.
260 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100261static void perf_counter_remove_from_context(struct perf_counter *counter)
Thomas Gleixner0793a612008-12-04 20:12:29 +0100262{
263 struct perf_counter_context *ctx = counter->ctx;
264 struct task_struct *task = ctx->task;
265
266 if (!task) {
267 /*
268 * Per cpu counters are removed via an smp call and
269 * the removal is always sucessful.
270 */
271 smp_call_function_single(counter->cpu,
Ingo Molnar04289bb2008-12-11 08:38:42 +0100272 __perf_counter_remove_from_context,
Thomas Gleixner0793a612008-12-04 20:12:29 +0100273 counter, 1);
274 return;
275 }
276
277retry:
Ingo Molnar04289bb2008-12-11 08:38:42 +0100278 task_oncpu_function_call(task, __perf_counter_remove_from_context,
Thomas Gleixner0793a612008-12-04 20:12:29 +0100279 counter);
280
281 spin_lock_irq(&ctx->lock);
282 /*
283 * If the context is active we need to retry the smp call.
284 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100285 if (ctx->nr_active && !list_empty(&counter->list_entry)) {
Thomas Gleixner0793a612008-12-04 20:12:29 +0100286 spin_unlock_irq(&ctx->lock);
287 goto retry;
288 }
289
290 /*
291 * The lock prevents that this context is scheduled in so we
Ingo Molnar04289bb2008-12-11 08:38:42 +0100292 * can remove the counter safely, if the call above did not
Thomas Gleixner0793a612008-12-04 20:12:29 +0100293 * succeed.
294 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100295 if (!list_empty(&counter->list_entry)) {
Ingo Molnar04289bb2008-12-11 08:38:42 +0100296 list_del_counter(counter, ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100297 }
298 spin_unlock_irq(&ctx->lock);
299}
300
Peter Zijlstra4af49982009-04-06 11:45:10 +0200301static inline u64 perf_clock(void)
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100302{
Peter Zijlstra4af49982009-04-06 11:45:10 +0200303 return cpu_clock(smp_processor_id());
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100304}
305
306/*
307 * Update the record of the current time in a context.
308 */
Peter Zijlstra4af49982009-04-06 11:45:10 +0200309static void update_context_time(struct perf_counter_context *ctx)
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100310{
Peter Zijlstra4af49982009-04-06 11:45:10 +0200311 u64 now = perf_clock();
312
313 ctx->time += now - ctx->timestamp;
314 ctx->timestamp = now;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100315}
316
317/*
318 * Update the total_time_enabled and total_time_running fields for a counter.
319 */
320static void update_counter_times(struct perf_counter *counter)
321{
322 struct perf_counter_context *ctx = counter->ctx;
323 u64 run_end;
324
Peter Zijlstra4af49982009-04-06 11:45:10 +0200325 if (counter->state < PERF_COUNTER_STATE_INACTIVE)
326 return;
327
328 counter->total_time_enabled = ctx->time - counter->tstamp_enabled;
329
330 if (counter->state == PERF_COUNTER_STATE_INACTIVE)
331 run_end = counter->tstamp_stopped;
332 else
333 run_end = ctx->time;
334
335 counter->total_time_running = run_end - counter->tstamp_running;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100336}
337
338/*
339 * Update total_time_enabled and total_time_running for all counters in a group.
340 */
341static void update_group_times(struct perf_counter *leader)
342{
343 struct perf_counter *counter;
344
345 update_counter_times(leader);
346 list_for_each_entry(counter, &leader->sibling_list, list_entry)
347 update_counter_times(counter);
348}
349
350/*
Paul Mackerrasd859e292009-01-17 18:10:22 +1100351 * Cross CPU call to disable a performance counter
352 */
353static void __perf_counter_disable(void *info)
354{
355 struct perf_counter *counter = info;
356 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
357 struct perf_counter_context *ctx = counter->ctx;
358 unsigned long flags;
359
360 /*
361 * If this is a per-task counter, need to check whether this
362 * counter's task is the current task on this cpu.
363 */
364 if (ctx->task && cpuctx->task_ctx != ctx)
365 return;
366
Peter Zijlstra849691a2009-04-06 11:45:12 +0200367 spin_lock_irqsave(&ctx->lock, flags);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100368
369 /*
370 * If the counter is on, turn it off.
371 * If it is in error state, leave it in error state.
372 */
373 if (counter->state >= PERF_COUNTER_STATE_INACTIVE) {
Peter Zijlstra4af49982009-04-06 11:45:10 +0200374 update_context_time(ctx);
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100375 update_counter_times(counter);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100376 if (counter == counter->group_leader)
377 group_sched_out(counter, cpuctx, ctx);
378 else
379 counter_sched_out(counter, cpuctx, ctx);
380 counter->state = PERF_COUNTER_STATE_OFF;
381 }
382
Peter Zijlstra849691a2009-04-06 11:45:12 +0200383 spin_unlock_irqrestore(&ctx->lock, flags);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100384}
385
386/*
387 * Disable a counter.
388 */
389static void perf_counter_disable(struct perf_counter *counter)
390{
391 struct perf_counter_context *ctx = counter->ctx;
392 struct task_struct *task = ctx->task;
393
394 if (!task) {
395 /*
396 * Disable the counter on the cpu that it's on
397 */
398 smp_call_function_single(counter->cpu, __perf_counter_disable,
399 counter, 1);
400 return;
401 }
402
403 retry:
404 task_oncpu_function_call(task, __perf_counter_disable, counter);
405
406 spin_lock_irq(&ctx->lock);
407 /*
408 * If the counter is still active, we need to retry the cross-call.
409 */
410 if (counter->state == PERF_COUNTER_STATE_ACTIVE) {
411 spin_unlock_irq(&ctx->lock);
412 goto retry;
413 }
414
415 /*
416 * Since we have the lock this context can't be scheduled
417 * in, so we can change the state safely.
418 */
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100419 if (counter->state == PERF_COUNTER_STATE_INACTIVE) {
420 update_counter_times(counter);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100421 counter->state = PERF_COUNTER_STATE_OFF;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100422 }
Paul Mackerrasd859e292009-01-17 18:10:22 +1100423
424 spin_unlock_irq(&ctx->lock);
425}
426
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100427static int
428counter_sched_in(struct perf_counter *counter,
429 struct perf_cpu_context *cpuctx,
430 struct perf_counter_context *ctx,
431 int cpu)
432{
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100433 if (counter->state <= PERF_COUNTER_STATE_OFF)
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100434 return 0;
435
436 counter->state = PERF_COUNTER_STATE_ACTIVE;
437 counter->oncpu = cpu; /* TODO: put 'cpu' into cpuctx->cpu */
438 /*
439 * The new state must be visible before we turn it on in the hardware:
440 */
441 smp_wmb();
442
Robert Richter4aeb0b42009-04-29 12:47:03 +0200443 if (counter->pmu->enable(counter)) {
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100444 counter->state = PERF_COUNTER_STATE_INACTIVE;
445 counter->oncpu = -1;
446 return -EAGAIN;
447 }
448
Peter Zijlstra4af49982009-04-06 11:45:10 +0200449 counter->tstamp_running += ctx->time - counter->tstamp_stopped;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100450
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100451 if (!is_software_counter(counter))
452 cpuctx->active_oncpu++;
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100453 ctx->nr_active++;
454
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100455 if (counter->hw_event.exclusive)
456 cpuctx->exclusive = 1;
457
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100458 return 0;
459}
460
Paul Mackerras6751b712009-05-11 12:08:02 +1000461static int
462group_sched_in(struct perf_counter *group_counter,
463 struct perf_cpu_context *cpuctx,
464 struct perf_counter_context *ctx,
465 int cpu)
466{
467 struct perf_counter *counter, *partial_group;
468 int ret;
469
470 if (group_counter->state == PERF_COUNTER_STATE_OFF)
471 return 0;
472
473 ret = hw_perf_group_sched_in(group_counter, cpuctx, ctx, cpu);
474 if (ret)
475 return ret < 0 ? ret : 0;
476
477 group_counter->prev_state = group_counter->state;
478 if (counter_sched_in(group_counter, cpuctx, ctx, cpu))
479 return -EAGAIN;
480
481 /*
482 * Schedule in siblings as one group (if any):
483 */
484 list_for_each_entry(counter, &group_counter->sibling_list, list_entry) {
485 counter->prev_state = counter->state;
486 if (counter_sched_in(counter, cpuctx, ctx, cpu)) {
487 partial_group = counter;
488 goto group_error;
489 }
490 }
491
492 return 0;
493
494group_error:
495 /*
496 * Groups can be scheduled in as one unit only, so undo any
497 * partial group before returning:
498 */
499 list_for_each_entry(counter, &group_counter->sibling_list, list_entry) {
500 if (counter == partial_group)
501 break;
502 counter_sched_out(counter, cpuctx, ctx);
503 }
504 counter_sched_out(group_counter, cpuctx, ctx);
505
506 return -EAGAIN;
507}
508
Thomas Gleixner0793a612008-12-04 20:12:29 +0100509/*
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100510 * Return 1 for a group consisting entirely of software counters,
511 * 0 if the group contains any hardware counters.
512 */
513static int is_software_only_group(struct perf_counter *leader)
514{
515 struct perf_counter *counter;
516
517 if (!is_software_counter(leader))
518 return 0;
Peter Zijlstra5c148192009-03-25 12:30:23 +0100519
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100520 list_for_each_entry(counter, &leader->sibling_list, list_entry)
521 if (!is_software_counter(counter))
522 return 0;
Peter Zijlstra5c148192009-03-25 12:30:23 +0100523
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100524 return 1;
525}
526
527/*
528 * Work out whether we can put this counter group on the CPU now.
529 */
530static int group_can_go_on(struct perf_counter *counter,
531 struct perf_cpu_context *cpuctx,
532 int can_add_hw)
533{
534 /*
535 * Groups consisting entirely of software counters can always go on.
536 */
537 if (is_software_only_group(counter))
538 return 1;
539 /*
540 * If an exclusive group is already on, no other hardware
541 * counters can go on.
542 */
543 if (cpuctx->exclusive)
544 return 0;
545 /*
546 * If this group is exclusive and there are already
547 * counters on the CPU, it can't go on.
548 */
549 if (counter->hw_event.exclusive && cpuctx->active_oncpu)
550 return 0;
551 /*
552 * Otherwise, try to add it if all previous groups were able
553 * to go on.
554 */
555 return can_add_hw;
556}
557
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100558static void add_counter_to_ctx(struct perf_counter *counter,
559 struct perf_counter_context *ctx)
560{
561 list_add_counter(counter, ctx);
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100562 counter->prev_state = PERF_COUNTER_STATE_OFF;
Peter Zijlstra4af49982009-04-06 11:45:10 +0200563 counter->tstamp_enabled = ctx->time;
564 counter->tstamp_running = ctx->time;
565 counter->tstamp_stopped = ctx->time;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100566}
567
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100568/*
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100569 * Cross CPU call to install and enable a performance counter
Thomas Gleixner0793a612008-12-04 20:12:29 +0100570 */
571static void __perf_install_in_context(void *info)
572{
573 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
574 struct perf_counter *counter = info;
575 struct perf_counter_context *ctx = counter->ctx;
Paul Mackerrasd859e292009-01-17 18:10:22 +1100576 struct perf_counter *leader = counter->group_leader;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100577 int cpu = smp_processor_id();
Ingo Molnar9b51f662008-12-12 13:49:45 +0100578 unsigned long flags;
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100579 int err;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100580
581 /*
582 * If this is a task context, we need to check whether it is
583 * the current task context of this cpu. If not it has been
584 * scheduled out before the smp call arrived.
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000585 * Or possibly this is the right context but it isn't
586 * on this cpu because it had no counters.
Thomas Gleixner0793a612008-12-04 20:12:29 +0100587 */
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000588 if (ctx->task && cpuctx->task_ctx != ctx) {
589 if (cpuctx->task_ctx || ctx->task != current)
590 return;
591 cpuctx->task_ctx = ctx;
592 }
Thomas Gleixner0793a612008-12-04 20:12:29 +0100593
Peter Zijlstra849691a2009-04-06 11:45:12 +0200594 spin_lock_irqsave(&ctx->lock, flags);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000595 ctx->is_active = 1;
Peter Zijlstra4af49982009-04-06 11:45:10 +0200596 update_context_time(ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100597
598 /*
599 * Protect the list operation against NMI by disabling the
600 * counters on a global level. NOP for non NMI based counters.
601 */
Peter Zijlstra9e35ad32009-05-13 16:21:38 +0200602 perf_disable();
Thomas Gleixner0793a612008-12-04 20:12:29 +0100603
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100604 add_counter_to_ctx(counter, ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100605
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100606 /*
Paul Mackerrasd859e292009-01-17 18:10:22 +1100607 * Don't put the counter on if it is disabled or if
608 * it is in a group and the group isn't on.
609 */
610 if (counter->state != PERF_COUNTER_STATE_INACTIVE ||
611 (leader != counter && leader->state != PERF_COUNTER_STATE_ACTIVE))
612 goto unlock;
613
614 /*
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100615 * An exclusive counter can't go on if there are already active
616 * hardware counters, and no hardware counter can go on if there
617 * is already an exclusive counter on.
618 */
Paul Mackerrasd859e292009-01-17 18:10:22 +1100619 if (!group_can_go_on(counter, cpuctx, 1))
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100620 err = -EEXIST;
621 else
622 err = counter_sched_in(counter, cpuctx, ctx, cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100623
Paul Mackerrasd859e292009-01-17 18:10:22 +1100624 if (err) {
625 /*
626 * This counter couldn't go on. If it is in a group
627 * then we have to pull the whole group off.
628 * If the counter group is pinned then put it in error state.
629 */
630 if (leader != counter)
631 group_sched_out(leader, cpuctx, ctx);
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100632 if (leader->hw_event.pinned) {
633 update_group_times(leader);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100634 leader->state = PERF_COUNTER_STATE_ERROR;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100635 }
Paul Mackerrasd859e292009-01-17 18:10:22 +1100636 }
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100637
638 if (!err && !ctx->task && cpuctx->max_pertask)
Thomas Gleixner0793a612008-12-04 20:12:29 +0100639 cpuctx->max_pertask--;
640
Paul Mackerrasd859e292009-01-17 18:10:22 +1100641 unlock:
Peter Zijlstra9e35ad32009-05-13 16:21:38 +0200642 perf_enable();
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100643
Peter Zijlstra849691a2009-04-06 11:45:12 +0200644 spin_unlock_irqrestore(&ctx->lock, flags);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100645}
646
647/*
648 * Attach a performance counter to a context
649 *
650 * First we add the counter to the list with the hardware enable bit
651 * in counter->hw_config cleared.
652 *
653 * If the counter is attached to a task which is on a CPU we use a smp
654 * call to enable it in the task context. The task might have been
655 * scheduled away, but we check this in the smp call again.
Paul Mackerrasd859e292009-01-17 18:10:22 +1100656 *
657 * Must be called with ctx->mutex held.
Thomas Gleixner0793a612008-12-04 20:12:29 +0100658 */
659static void
660perf_install_in_context(struct perf_counter_context *ctx,
661 struct perf_counter *counter,
662 int cpu)
663{
664 struct task_struct *task = ctx->task;
665
Thomas Gleixner0793a612008-12-04 20:12:29 +0100666 if (!task) {
667 /*
668 * Per cpu counters are installed via an smp call and
669 * the install is always sucessful.
670 */
671 smp_call_function_single(cpu, __perf_install_in_context,
672 counter, 1);
673 return;
674 }
675
Thomas Gleixner0793a612008-12-04 20:12:29 +0100676retry:
677 task_oncpu_function_call(task, __perf_install_in_context,
678 counter);
679
680 spin_lock_irq(&ctx->lock);
681 /*
Thomas Gleixner0793a612008-12-04 20:12:29 +0100682 * we need to retry the smp call.
683 */
Paul Mackerrasd859e292009-01-17 18:10:22 +1100684 if (ctx->is_active && list_empty(&counter->list_entry)) {
Thomas Gleixner0793a612008-12-04 20:12:29 +0100685 spin_unlock_irq(&ctx->lock);
686 goto retry;
687 }
688
689 /*
690 * The lock prevents that this context is scheduled in so we
691 * can add the counter safely, if it the call above did not
692 * succeed.
693 */
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100694 if (list_empty(&counter->list_entry))
695 add_counter_to_ctx(counter, ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100696 spin_unlock_irq(&ctx->lock);
697}
698
Paul Mackerrasd859e292009-01-17 18:10:22 +1100699/*
700 * Cross CPU call to enable a performance counter
701 */
702static void __perf_counter_enable(void *info)
Ingo Molnar04289bb2008-12-11 08:38:42 +0100703{
Paul Mackerrasd859e292009-01-17 18:10:22 +1100704 struct perf_counter *counter = info;
705 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
706 struct perf_counter_context *ctx = counter->ctx;
707 struct perf_counter *leader = counter->group_leader;
708 unsigned long flags;
709 int err;
Ingo Molnar04289bb2008-12-11 08:38:42 +0100710
711 /*
Paul Mackerrasd859e292009-01-17 18:10:22 +1100712 * If this is a per-task counter, need to check whether this
713 * counter's task is the current task on this cpu.
Ingo Molnar04289bb2008-12-11 08:38:42 +0100714 */
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000715 if (ctx->task && cpuctx->task_ctx != ctx) {
716 if (cpuctx->task_ctx || ctx->task != current)
717 return;
718 cpuctx->task_ctx = ctx;
719 }
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100720
Peter Zijlstra849691a2009-04-06 11:45:12 +0200721 spin_lock_irqsave(&ctx->lock, flags);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000722 ctx->is_active = 1;
Peter Zijlstra4af49982009-04-06 11:45:10 +0200723 update_context_time(ctx);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100724
Paul Mackerrasc07c99b2009-02-13 22:10:34 +1100725 counter->prev_state = counter->state;
Paul Mackerrasd859e292009-01-17 18:10:22 +1100726 if (counter->state >= PERF_COUNTER_STATE_INACTIVE)
727 goto unlock;
728 counter->state = PERF_COUNTER_STATE_INACTIVE;
Peter Zijlstra4af49982009-04-06 11:45:10 +0200729 counter->tstamp_enabled = ctx->time - counter->total_time_enabled;
Paul Mackerrasd859e292009-01-17 18:10:22 +1100730
731 /*
732 * If the counter is in a group and isn't the group leader,
733 * then don't put it on unless the group is on.
734 */
735 if (leader != counter && leader->state != PERF_COUNTER_STATE_ACTIVE)
736 goto unlock;
737
Paul Mackerrase758a332009-05-12 21:59:01 +1000738 if (!group_can_go_on(counter, cpuctx, 1)) {
Paul Mackerrasd859e292009-01-17 18:10:22 +1100739 err = -EEXIST;
Paul Mackerrase758a332009-05-12 21:59:01 +1000740 } else {
Peter Zijlstra9e35ad32009-05-13 16:21:38 +0200741 perf_disable();
Paul Mackerrase758a332009-05-12 21:59:01 +1000742 if (counter == leader)
743 err = group_sched_in(counter, cpuctx, ctx,
744 smp_processor_id());
745 else
746 err = counter_sched_in(counter, cpuctx, ctx,
747 smp_processor_id());
Peter Zijlstra9e35ad32009-05-13 16:21:38 +0200748 perf_enable();
Paul Mackerrase758a332009-05-12 21:59:01 +1000749 }
Paul Mackerrasd859e292009-01-17 18:10:22 +1100750
751 if (err) {
752 /*
753 * If this counter can't go on and it's part of a
754 * group, then the whole group has to come off.
755 */
756 if (leader != counter)
757 group_sched_out(leader, cpuctx, ctx);
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100758 if (leader->hw_event.pinned) {
759 update_group_times(leader);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100760 leader->state = PERF_COUNTER_STATE_ERROR;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100761 }
Paul Mackerrasd859e292009-01-17 18:10:22 +1100762 }
763
764 unlock:
Peter Zijlstra849691a2009-04-06 11:45:12 +0200765 spin_unlock_irqrestore(&ctx->lock, flags);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100766}
767
768/*
769 * Enable a counter.
770 */
771static void perf_counter_enable(struct perf_counter *counter)
772{
773 struct perf_counter_context *ctx = counter->ctx;
774 struct task_struct *task = ctx->task;
775
776 if (!task) {
777 /*
778 * Enable the counter on the cpu that it's on
779 */
780 smp_call_function_single(counter->cpu, __perf_counter_enable,
781 counter, 1);
782 return;
783 }
784
785 spin_lock_irq(&ctx->lock);
786 if (counter->state >= PERF_COUNTER_STATE_INACTIVE)
787 goto out;
788
789 /*
790 * If the counter is in error state, clear that first.
791 * That way, if we see the counter in error state below, we
792 * know that it has gone back into error state, as distinct
793 * from the task having been scheduled away before the
794 * cross-call arrived.
795 */
796 if (counter->state == PERF_COUNTER_STATE_ERROR)
797 counter->state = PERF_COUNTER_STATE_OFF;
798
799 retry:
800 spin_unlock_irq(&ctx->lock);
801 task_oncpu_function_call(task, __perf_counter_enable, counter);
802
803 spin_lock_irq(&ctx->lock);
804
805 /*
806 * If the context is active and the counter is still off,
807 * we need to retry the cross-call.
808 */
809 if (ctx->is_active && counter->state == PERF_COUNTER_STATE_OFF)
810 goto retry;
811
812 /*
813 * Since we have the lock this context can't be scheduled
814 * in, so we can change the state safely.
815 */
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100816 if (counter->state == PERF_COUNTER_STATE_OFF) {
Paul Mackerrasd859e292009-01-17 18:10:22 +1100817 counter->state = PERF_COUNTER_STATE_INACTIVE;
Peter Zijlstra4af49982009-04-06 11:45:10 +0200818 counter->tstamp_enabled =
819 ctx->time - counter->total_time_enabled;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100820 }
Paul Mackerrasd859e292009-01-17 18:10:22 +1100821 out:
822 spin_unlock_irq(&ctx->lock);
823}
824
Peter Zijlstra2023b352009-05-05 17:50:26 +0200825static int perf_counter_refresh(struct perf_counter *counter, int refresh)
Peter Zijlstra79f14642009-04-06 11:45:07 +0200826{
Peter Zijlstra2023b352009-05-05 17:50:26 +0200827 /*
828 * not supported on inherited counters
829 */
830 if (counter->hw_event.inherit)
831 return -EINVAL;
832
Peter Zijlstra79f14642009-04-06 11:45:07 +0200833 atomic_add(refresh, &counter->event_limit);
834 perf_counter_enable(counter);
Peter Zijlstra2023b352009-05-05 17:50:26 +0200835
836 return 0;
Peter Zijlstra79f14642009-04-06 11:45:07 +0200837}
838
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100839void __perf_counter_sched_out(struct perf_counter_context *ctx,
840 struct perf_cpu_context *cpuctx)
841{
842 struct perf_counter *counter;
843
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100844 spin_lock(&ctx->lock);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100845 ctx->is_active = 0;
846 if (likely(!ctx->nr_counters))
847 goto out;
Peter Zijlstra4af49982009-04-06 11:45:10 +0200848 update_context_time(ctx);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100849
Peter Zijlstra9e35ad32009-05-13 16:21:38 +0200850 perf_disable();
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100851 if (ctx->nr_active) {
Peter Zijlstraafedadf2009-05-20 12:21:22 +0200852 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
853 if (counter != counter->group_leader)
854 counter_sched_out(counter, cpuctx, ctx);
855 else
856 group_sched_out(counter, cpuctx, ctx);
857 }
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100858 }
Peter Zijlstra9e35ad32009-05-13 16:21:38 +0200859 perf_enable();
Paul Mackerrasd859e292009-01-17 18:10:22 +1100860 out:
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100861 spin_unlock(&ctx->lock);
862}
863
Thomas Gleixner0793a612008-12-04 20:12:29 +0100864/*
865 * Called from scheduler to remove the counters of the current task,
866 * with interrupts disabled.
867 *
868 * We stop each counter and update the counter value in counter->count.
869 *
Ingo Molnar76715812008-12-17 14:20:28 +0100870 * This does not protect us against NMI, but disable()
Thomas Gleixner0793a612008-12-04 20:12:29 +0100871 * sets the disabled bit in the control field of counter _before_
872 * accessing the counter control register. If a NMI hits, then it will
873 * not restart the counter.
874 */
875void perf_counter_task_sched_out(struct task_struct *task, int cpu)
876{
877 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000878 struct perf_counter_context *ctx = task->perf_counter_ctxp;
Peter Zijlstra4a0deca2009-03-19 20:26:12 +0100879 struct pt_regs *regs;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100880
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000881 if (likely(!ctx || !cpuctx->task_ctx))
Thomas Gleixner0793a612008-12-04 20:12:29 +0100882 return;
883
Peter Zijlstrabce379b2009-04-06 11:45:13 +0200884 update_context_time(ctx);
885
Peter Zijlstra4a0deca2009-03-19 20:26:12 +0100886 regs = task_pt_regs(task);
Peter Zijlstra78f13e92009-04-08 15:01:33 +0200887 perf_swcounter_event(PERF_COUNT_CONTEXT_SWITCHES, 1, 1, regs, 0);
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100888 __perf_counter_sched_out(ctx, cpuctx);
889
Thomas Gleixner0793a612008-12-04 20:12:29 +0100890 cpuctx->task_ctx = NULL;
891}
892
Paul Mackerrasa08b1592009-05-11 15:46:10 +1000893static void __perf_counter_task_sched_out(struct perf_counter_context *ctx)
894{
895 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
896
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000897 if (!cpuctx->task_ctx)
898 return;
Paul Mackerrasa08b1592009-05-11 15:46:10 +1000899 __perf_counter_sched_out(ctx, cpuctx);
900 cpuctx->task_ctx = NULL;
901}
902
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100903static void perf_counter_cpu_sched_out(struct perf_cpu_context *cpuctx)
Ingo Molnar04289bb2008-12-11 08:38:42 +0100904{
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100905 __perf_counter_sched_out(&cpuctx->ctx, cpuctx);
Ingo Molnar04289bb2008-12-11 08:38:42 +0100906}
907
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100908static void
909__perf_counter_sched_in(struct perf_counter_context *ctx,
910 struct perf_cpu_context *cpuctx, int cpu)
Thomas Gleixner0793a612008-12-04 20:12:29 +0100911{
Thomas Gleixner0793a612008-12-04 20:12:29 +0100912 struct perf_counter *counter;
Paul Mackerrasdd0e6ba2009-01-12 15:11:00 +1100913 int can_add_hw = 1;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100914
Thomas Gleixner0793a612008-12-04 20:12:29 +0100915 spin_lock(&ctx->lock);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100916 ctx->is_active = 1;
917 if (likely(!ctx->nr_counters))
918 goto out;
919
Peter Zijlstra4af49982009-04-06 11:45:10 +0200920 ctx->timestamp = perf_clock();
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100921
Peter Zijlstra9e35ad32009-05-13 16:21:38 +0200922 perf_disable();
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100923
924 /*
925 * First go through the list and put on any pinned groups
926 * in order to give them the best chance of going on.
927 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100928 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100929 if (counter->state <= PERF_COUNTER_STATE_OFF ||
930 !counter->hw_event.pinned)
931 continue;
932 if (counter->cpu != -1 && counter->cpu != cpu)
933 continue;
934
Peter Zijlstraafedadf2009-05-20 12:21:22 +0200935 if (counter != counter->group_leader)
936 counter_sched_in(counter, cpuctx, ctx, cpu);
937 else {
938 if (group_can_go_on(counter, cpuctx, 1))
939 group_sched_in(counter, cpuctx, ctx, cpu);
940 }
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100941
942 /*
943 * If this pinned group hasn't been scheduled,
944 * put it in error state.
945 */
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100946 if (counter->state == PERF_COUNTER_STATE_INACTIVE) {
947 update_group_times(counter);
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100948 counter->state = PERF_COUNTER_STATE_ERROR;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100949 }
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100950 }
951
952 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
953 /*
954 * Ignore counters in OFF or ERROR state, and
955 * ignore pinned counters since we did them already.
956 */
957 if (counter->state <= PERF_COUNTER_STATE_OFF ||
958 counter->hw_event.pinned)
959 continue;
960
Ingo Molnar04289bb2008-12-11 08:38:42 +0100961 /*
962 * Listen to the 'cpu' scheduling filter constraint
963 * of counters:
964 */
Thomas Gleixner0793a612008-12-04 20:12:29 +0100965 if (counter->cpu != -1 && counter->cpu != cpu)
966 continue;
967
Peter Zijlstraafedadf2009-05-20 12:21:22 +0200968 if (counter != counter->group_leader) {
969 if (counter_sched_in(counter, cpuctx, ctx, cpu))
Paul Mackerrasdd0e6ba2009-01-12 15:11:00 +1100970 can_add_hw = 0;
Peter Zijlstraafedadf2009-05-20 12:21:22 +0200971 } else {
972 if (group_can_go_on(counter, cpuctx, can_add_hw)) {
973 if (group_sched_in(counter, cpuctx, ctx, cpu))
974 can_add_hw = 0;
975 }
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100976 }
Thomas Gleixner0793a612008-12-04 20:12:29 +0100977 }
Peter Zijlstra9e35ad32009-05-13 16:21:38 +0200978 perf_enable();
Paul Mackerrasd859e292009-01-17 18:10:22 +1100979 out:
Thomas Gleixner0793a612008-12-04 20:12:29 +0100980 spin_unlock(&ctx->lock);
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100981}
Ingo Molnar04289bb2008-12-11 08:38:42 +0100982
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100983/*
984 * Called from scheduler to add the counters of the current task
985 * with interrupts disabled.
986 *
987 * We restore the counter value and then enable it.
988 *
989 * This does not protect us against NMI, but enable()
990 * sets the enabled bit in the control field of counter _before_
991 * accessing the counter control register. If a NMI hits, then it will
992 * keep the counter running.
993 */
994void perf_counter_task_sched_in(struct task_struct *task, int cpu)
995{
996 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000997 struct perf_counter_context *ctx = task->perf_counter_ctxp;
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100998
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000999 if (likely(!ctx))
1000 return;
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001001 __perf_counter_sched_in(ctx, cpuctx, cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001002 cpuctx->task_ctx = ctx;
1003}
1004
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001005static void perf_counter_cpu_sched_in(struct perf_cpu_context *cpuctx, int cpu)
1006{
1007 struct perf_counter_context *ctx = &cpuctx->ctx;
1008
1009 __perf_counter_sched_in(ctx, cpuctx, cpu);
1010}
1011
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +01001012int perf_counter_task_disable(void)
1013{
1014 struct task_struct *curr = current;
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001015 struct perf_counter_context *ctx = curr->perf_counter_ctxp;
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +01001016 struct perf_counter *counter;
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01001017 unsigned long flags;
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +01001018
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001019 if (!ctx || !ctx->nr_counters)
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +01001020 return 0;
1021
Peter Zijlstra849691a2009-04-06 11:45:12 +02001022 local_irq_save(flags);
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +01001023
Paul Mackerrasa08b1592009-05-11 15:46:10 +10001024 __perf_counter_task_sched_out(ctx);
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +01001025
1026 spin_lock(&ctx->lock);
1027
1028 /*
1029 * Disable all the counters:
1030 */
Peter Zijlstra9e35ad32009-05-13 16:21:38 +02001031 perf_disable();
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +01001032
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001033 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001034 if (counter->state != PERF_COUNTER_STATE_ERROR) {
1035 update_group_times(counter);
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001036 counter->state = PERF_COUNTER_STATE_OFF;
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001037 }
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001038 }
Ingo Molnar9b51f662008-12-12 13:49:45 +01001039
Peter Zijlstra9e35ad32009-05-13 16:21:38 +02001040 perf_enable();
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +01001041
Peter Zijlstra849691a2009-04-06 11:45:12 +02001042 spin_unlock_irqrestore(&ctx->lock, flags);
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +01001043
1044 return 0;
1045}
1046
1047int perf_counter_task_enable(void)
1048{
1049 struct task_struct *curr = current;
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001050 struct perf_counter_context *ctx = curr->perf_counter_ctxp;
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +01001051 struct perf_counter *counter;
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01001052 unsigned long flags;
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +01001053 int cpu;
1054
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001055 if (!ctx || !ctx->nr_counters)
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +01001056 return 0;
1057
Peter Zijlstra849691a2009-04-06 11:45:12 +02001058 local_irq_save(flags);
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +01001059 cpu = smp_processor_id();
1060
Paul Mackerrasa08b1592009-05-11 15:46:10 +10001061 __perf_counter_task_sched_out(ctx);
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001062
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +01001063 spin_lock(&ctx->lock);
1064
1065 /*
1066 * Disable all the counters:
1067 */
Peter Zijlstra9e35ad32009-05-13 16:21:38 +02001068 perf_disable();
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +01001069
1070 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001071 if (counter->state > PERF_COUNTER_STATE_OFF)
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +01001072 continue;
Ingo Molnar6a930702008-12-11 15:17:03 +01001073 counter->state = PERF_COUNTER_STATE_INACTIVE;
Peter Zijlstra4af49982009-04-06 11:45:10 +02001074 counter->tstamp_enabled =
1075 ctx->time - counter->total_time_enabled;
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01001076 counter->hw_event.disabled = 0;
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +01001077 }
Peter Zijlstra9e35ad32009-05-13 16:21:38 +02001078 perf_enable();
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +01001079
1080 spin_unlock(&ctx->lock);
1081
1082 perf_counter_task_sched_in(curr, cpu);
1083
Peter Zijlstra849691a2009-04-06 11:45:12 +02001084 local_irq_restore(flags);
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +01001085
1086 return 0;
1087}
1088
Peter Zijlstra26b119b2009-05-20 12:21:20 +02001089static void perf_log_period(struct perf_counter *counter, u64 period);
1090
1091static void perf_adjust_freq(struct perf_counter_context *ctx)
Peter Zijlstra60db5e02009-05-15 15:19:28 +02001092{
1093 struct perf_counter *counter;
1094 u64 irq_period;
1095 u64 events, period;
1096 s64 delta;
1097
1098 spin_lock(&ctx->lock);
1099 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
1100 if (counter->state != PERF_COUNTER_STATE_ACTIVE)
1101 continue;
1102
1103 if (!counter->hw_event.freq || !counter->hw_event.irq_freq)
1104 continue;
1105
1106 events = HZ * counter->hw.interrupts * counter->hw.irq_period;
1107 period = div64_u64(events, counter->hw_event.irq_freq);
1108
1109 delta = (s64)(1 + period - counter->hw.irq_period);
1110 delta >>= 1;
1111
1112 irq_period = counter->hw.irq_period + delta;
1113
1114 if (!irq_period)
1115 irq_period = 1;
1116
Peter Zijlstra26b119b2009-05-20 12:21:20 +02001117 perf_log_period(counter, irq_period);
1118
Peter Zijlstra60db5e02009-05-15 15:19:28 +02001119 counter->hw.irq_period = irq_period;
1120 counter->hw.interrupts = 0;
1121 }
1122 spin_unlock(&ctx->lock);
1123}
1124
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001125/*
1126 * Round-robin a context's counters:
1127 */
1128static void rotate_ctx(struct perf_counter_context *ctx)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001129{
Thomas Gleixner0793a612008-12-04 20:12:29 +01001130 struct perf_counter *counter;
1131
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001132 if (!ctx->nr_counters)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001133 return;
1134
Thomas Gleixner0793a612008-12-04 20:12:29 +01001135 spin_lock(&ctx->lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001136 /*
Ingo Molnar04289bb2008-12-11 08:38:42 +01001137 * Rotate the first entry last (works just fine for group counters too):
Thomas Gleixner0793a612008-12-04 20:12:29 +01001138 */
Peter Zijlstra9e35ad32009-05-13 16:21:38 +02001139 perf_disable();
Ingo Molnar04289bb2008-12-11 08:38:42 +01001140 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
Peter Zijlstra75564232009-03-13 12:21:29 +01001141 list_move_tail(&counter->list_entry, &ctx->counter_list);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001142 break;
1143 }
Peter Zijlstra9e35ad32009-05-13 16:21:38 +02001144 perf_enable();
Thomas Gleixner0793a612008-12-04 20:12:29 +01001145
1146 spin_unlock(&ctx->lock);
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001147}
Thomas Gleixner0793a612008-12-04 20:12:29 +01001148
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001149void perf_counter_task_tick(struct task_struct *curr, int cpu)
1150{
Peter Zijlstra7fc23a52009-05-08 18:52:21 +02001151 struct perf_cpu_context *cpuctx;
1152 struct perf_counter_context *ctx;
1153
1154 if (!atomic_read(&nr_counters))
1155 return;
1156
1157 cpuctx = &per_cpu(perf_cpu_context, cpu);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001158 ctx = curr->perf_counter_ctxp;
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001159
Peter Zijlstra60db5e02009-05-15 15:19:28 +02001160 perf_adjust_freq(&cpuctx->ctx);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001161 if (ctx)
1162 perf_adjust_freq(ctx);
Peter Zijlstra60db5e02009-05-15 15:19:28 +02001163
Ingo Molnarb82914c2009-05-04 18:54:32 +02001164 perf_counter_cpu_sched_out(cpuctx);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001165 if (ctx)
1166 __perf_counter_task_sched_out(ctx);
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001167
Ingo Molnarb82914c2009-05-04 18:54:32 +02001168 rotate_ctx(&cpuctx->ctx);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001169 if (ctx)
1170 rotate_ctx(ctx);
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001171
Ingo Molnarb82914c2009-05-04 18:54:32 +02001172 perf_counter_cpu_sched_in(cpuctx, cpu);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001173 if (ctx)
1174 perf_counter_task_sched_in(curr, cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001175}
1176
1177/*
Thomas Gleixner0793a612008-12-04 20:12:29 +01001178 * Cross CPU call to read the hardware counter
1179 */
Ingo Molnar76715812008-12-17 14:20:28 +01001180static void __read(void *info)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001181{
Ingo Molnar621a01e2008-12-11 12:46:46 +01001182 struct perf_counter *counter = info;
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001183 struct perf_counter_context *ctx = counter->ctx;
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01001184 unsigned long flags;
Ingo Molnar621a01e2008-12-11 12:46:46 +01001185
Peter Zijlstra849691a2009-04-06 11:45:12 +02001186 local_irq_save(flags);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001187 if (ctx->is_active)
Peter Zijlstra4af49982009-04-06 11:45:10 +02001188 update_context_time(ctx);
Robert Richter4aeb0b42009-04-29 12:47:03 +02001189 counter->pmu->read(counter);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001190 update_counter_times(counter);
Peter Zijlstra849691a2009-04-06 11:45:12 +02001191 local_irq_restore(flags);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001192}
1193
Ingo Molnar04289bb2008-12-11 08:38:42 +01001194static u64 perf_counter_read(struct perf_counter *counter)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001195{
1196 /*
1197 * If counter is enabled and currently active on a CPU, update the
1198 * value in the counter structure:
1199 */
Ingo Molnar6a930702008-12-11 15:17:03 +01001200 if (counter->state == PERF_COUNTER_STATE_ACTIVE) {
Thomas Gleixner0793a612008-12-04 20:12:29 +01001201 smp_call_function_single(counter->oncpu,
Ingo Molnar76715812008-12-17 14:20:28 +01001202 __read, counter, 1);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001203 } else if (counter->state == PERF_COUNTER_STATE_INACTIVE) {
1204 update_counter_times(counter);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001205 }
1206
Ingo Molnaree060942008-12-13 09:00:03 +01001207 return atomic64_read(&counter->count);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001208}
1209
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001210/*
1211 * Initialize the perf_counter context in a task_struct:
1212 */
1213static void
1214__perf_counter_init_context(struct perf_counter_context *ctx,
1215 struct task_struct *task)
1216{
1217 memset(ctx, 0, sizeof(*ctx));
1218 spin_lock_init(&ctx->lock);
1219 mutex_init(&ctx->mutex);
1220 INIT_LIST_HEAD(&ctx->counter_list);
1221 INIT_LIST_HEAD(&ctx->event_list);
1222 atomic_set(&ctx->refcount, 1);
1223 ctx->task = task;
1224}
1225
Thomas Gleixner0793a612008-12-04 20:12:29 +01001226static void put_context(struct perf_counter_context *ctx)
1227{
1228 if (ctx->task)
1229 put_task_struct(ctx->task);
1230}
1231
1232static struct perf_counter_context *find_get_context(pid_t pid, int cpu)
1233{
1234 struct perf_cpu_context *cpuctx;
1235 struct perf_counter_context *ctx;
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001236 struct perf_counter_context *tctx;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001237 struct task_struct *task;
1238
1239 /*
1240 * If cpu is not a wildcard then this is a percpu counter:
1241 */
1242 if (cpu != -1) {
1243 /* Must be root to operate on a CPU counter: */
Peter Zijlstra1ccd1542009-04-09 10:53:45 +02001244 if (sysctl_perf_counter_priv && !capable(CAP_SYS_ADMIN))
Thomas Gleixner0793a612008-12-04 20:12:29 +01001245 return ERR_PTR(-EACCES);
1246
1247 if (cpu < 0 || cpu > num_possible_cpus())
1248 return ERR_PTR(-EINVAL);
1249
1250 /*
1251 * We could be clever and allow to attach a counter to an
1252 * offline CPU and activate it when the CPU comes up, but
1253 * that's for later.
1254 */
1255 if (!cpu_isset(cpu, cpu_online_map))
1256 return ERR_PTR(-ENODEV);
1257
1258 cpuctx = &per_cpu(perf_cpu_context, cpu);
1259 ctx = &cpuctx->ctx;
1260
Thomas Gleixner0793a612008-12-04 20:12:29 +01001261 return ctx;
1262 }
1263
1264 rcu_read_lock();
1265 if (!pid)
1266 task = current;
1267 else
1268 task = find_task_by_vpid(pid);
1269 if (task)
1270 get_task_struct(task);
1271 rcu_read_unlock();
1272
1273 if (!task)
1274 return ERR_PTR(-ESRCH);
1275
Thomas Gleixner0793a612008-12-04 20:12:29 +01001276 /* Reuse ptrace permission checks for now. */
1277 if (!ptrace_may_access(task, PTRACE_MODE_READ)) {
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001278 put_task_struct(task);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001279 return ERR_PTR(-EACCES);
1280 }
1281
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001282 ctx = task->perf_counter_ctxp;
1283 if (!ctx) {
1284 ctx = kmalloc(sizeof(struct perf_counter_context), GFP_KERNEL);
1285 if (!ctx) {
1286 put_task_struct(task);
1287 return ERR_PTR(-ENOMEM);
1288 }
1289 __perf_counter_init_context(ctx, task);
1290 /*
1291 * Make sure other cpus see correct values for *ctx
1292 * once task->perf_counter_ctxp is visible to them.
1293 */
1294 smp_wmb();
1295 tctx = cmpxchg(&task->perf_counter_ctxp, NULL, ctx);
1296 if (tctx) {
1297 /*
1298 * We raced with some other task; use
1299 * the context they set.
1300 */
1301 kfree(ctx);
1302 ctx = tctx;
1303 }
1304 }
1305
Thomas Gleixner0793a612008-12-04 20:12:29 +01001306 return ctx;
1307}
1308
Peter Zijlstra592903c2009-03-13 12:21:36 +01001309static void free_counter_rcu(struct rcu_head *head)
1310{
1311 struct perf_counter *counter;
1312
1313 counter = container_of(head, struct perf_counter, rcu_head);
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10001314 put_ctx(counter->ctx);
Peter Zijlstra592903c2009-03-13 12:21:36 +01001315 kfree(counter);
1316}
1317
Peter Zijlstra925d5192009-03-30 19:07:02 +02001318static void perf_pending_sync(struct perf_counter *counter);
1319
Peter Zijlstraf1600952009-03-19 20:26:16 +01001320static void free_counter(struct perf_counter *counter)
1321{
Peter Zijlstra925d5192009-03-30 19:07:02 +02001322 perf_pending_sync(counter);
1323
Peter Zijlstra7fc23a52009-05-08 18:52:21 +02001324 atomic_dec(&nr_counters);
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02001325 if (counter->hw_event.mmap)
1326 atomic_dec(&nr_mmap_tracking);
1327 if (counter->hw_event.munmap)
1328 atomic_dec(&nr_munmap_tracking);
1329 if (counter->hw_event.comm)
1330 atomic_dec(&nr_comm_tracking);
1331
Peter Zijlstrae077df42009-03-19 20:26:17 +01001332 if (counter->destroy)
1333 counter->destroy(counter);
1334
Peter Zijlstraf1600952009-03-19 20:26:16 +01001335 call_rcu(&counter->rcu_head, free_counter_rcu);
1336}
1337
Thomas Gleixner0793a612008-12-04 20:12:29 +01001338/*
1339 * Called when the last reference to the file is gone.
1340 */
1341static int perf_release(struct inode *inode, struct file *file)
1342{
1343 struct perf_counter *counter = file->private_data;
1344 struct perf_counter_context *ctx = counter->ctx;
1345
1346 file->private_data = NULL;
1347
Paul Mackerrasd859e292009-01-17 18:10:22 +11001348 mutex_lock(&ctx->mutex);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001349 mutex_lock(&counter->mutex);
1350
Ingo Molnar04289bb2008-12-11 08:38:42 +01001351 perf_counter_remove_from_context(counter);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001352
1353 mutex_unlock(&counter->mutex);
Paul Mackerrasd859e292009-01-17 18:10:22 +11001354 mutex_unlock(&ctx->mutex);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001355
Peter Zijlstraf1600952009-03-19 20:26:16 +01001356 free_counter(counter);
Mike Galbraith5af75912009-02-11 10:53:37 +01001357 put_context(ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001358
1359 return 0;
1360}
1361
1362/*
1363 * Read the performance counter - simple non blocking version for now
1364 */
1365static ssize_t
1366perf_read_hw(struct perf_counter *counter, char __user *buf, size_t count)
1367{
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001368 u64 values[3];
1369 int n;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001370
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001371 /*
1372 * Return end-of-file for a read on a counter that is in
1373 * error state (i.e. because it was pinned but it couldn't be
1374 * scheduled on to the CPU at some point).
1375 */
1376 if (counter->state == PERF_COUNTER_STATE_ERROR)
1377 return 0;
1378
Thomas Gleixner0793a612008-12-04 20:12:29 +01001379 mutex_lock(&counter->mutex);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001380 values[0] = perf_counter_read(counter);
1381 n = 1;
1382 if (counter->hw_event.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
1383 values[n++] = counter->total_time_enabled +
1384 atomic64_read(&counter->child_total_time_enabled);
1385 if (counter->hw_event.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
1386 values[n++] = counter->total_time_running +
1387 atomic64_read(&counter->child_total_time_running);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001388 mutex_unlock(&counter->mutex);
1389
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001390 if (count < n * sizeof(u64))
1391 return -EINVAL;
1392 count = n * sizeof(u64);
1393
1394 if (copy_to_user(buf, values, count))
1395 return -EFAULT;
1396
1397 return count;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001398}
1399
1400static ssize_t
Thomas Gleixner0793a612008-12-04 20:12:29 +01001401perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
1402{
1403 struct perf_counter *counter = file->private_data;
1404
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001405 return perf_read_hw(counter, buf, count);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001406}
1407
1408static unsigned int perf_poll(struct file *file, poll_table *wait)
1409{
1410 struct perf_counter *counter = file->private_data;
Peter Zijlstrac7138f32009-03-24 13:18:16 +01001411 struct perf_mmap_data *data;
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001412 unsigned int events = POLL_HUP;
Peter Zijlstrac7138f32009-03-24 13:18:16 +01001413
1414 rcu_read_lock();
1415 data = rcu_dereference(counter->data);
1416 if (data)
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001417 events = atomic_xchg(&data->poll, 0);
Peter Zijlstrac7138f32009-03-24 13:18:16 +01001418 rcu_read_unlock();
Thomas Gleixner0793a612008-12-04 20:12:29 +01001419
1420 poll_wait(file, &counter->waitq, wait);
1421
Thomas Gleixner0793a612008-12-04 20:12:29 +01001422 return events;
1423}
1424
Peter Zijlstra6de6a7b2009-05-05 17:50:23 +02001425static void perf_counter_reset(struct perf_counter *counter)
1426{
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001427 (void)perf_counter_read(counter);
Paul Mackerras615a3f12009-05-11 15:50:21 +10001428 atomic64_set(&counter->count, 0);
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001429 perf_counter_update_userpage(counter);
1430}
1431
1432static void perf_counter_for_each_sibling(struct perf_counter *counter,
1433 void (*func)(struct perf_counter *))
1434{
1435 struct perf_counter_context *ctx = counter->ctx;
1436 struct perf_counter *sibling;
1437
1438 spin_lock_irq(&ctx->lock);
1439 counter = counter->group_leader;
1440
1441 func(counter);
1442 list_for_each_entry(sibling, &counter->sibling_list, list_entry)
1443 func(sibling);
1444 spin_unlock_irq(&ctx->lock);
1445}
1446
1447static void perf_counter_for_each_child(struct perf_counter *counter,
1448 void (*func)(struct perf_counter *))
1449{
1450 struct perf_counter *child;
1451
1452 mutex_lock(&counter->mutex);
1453 func(counter);
1454 list_for_each_entry(child, &counter->child_list, child_list)
1455 func(child);
1456 mutex_unlock(&counter->mutex);
1457}
1458
1459static void perf_counter_for_each(struct perf_counter *counter,
1460 void (*func)(struct perf_counter *))
1461{
1462 struct perf_counter *child;
1463
1464 mutex_lock(&counter->mutex);
1465 perf_counter_for_each_sibling(counter, func);
1466 list_for_each_entry(child, &counter->child_list, child_list)
1467 perf_counter_for_each_sibling(child, func);
1468 mutex_unlock(&counter->mutex);
Peter Zijlstra6de6a7b2009-05-05 17:50:23 +02001469}
1470
Paul Mackerrasd859e292009-01-17 18:10:22 +11001471static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1472{
1473 struct perf_counter *counter = file->private_data;
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001474 void (*func)(struct perf_counter *);
1475 u32 flags = arg;
Paul Mackerrasd859e292009-01-17 18:10:22 +11001476
1477 switch (cmd) {
1478 case PERF_COUNTER_IOC_ENABLE:
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001479 func = perf_counter_enable;
Paul Mackerrasd859e292009-01-17 18:10:22 +11001480 break;
1481 case PERF_COUNTER_IOC_DISABLE:
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001482 func = perf_counter_disable;
Peter Zijlstra79f14642009-04-06 11:45:07 +02001483 break;
Peter Zijlstra6de6a7b2009-05-05 17:50:23 +02001484 case PERF_COUNTER_IOC_RESET:
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001485 func = perf_counter_reset;
Peter Zijlstra6de6a7b2009-05-05 17:50:23 +02001486 break;
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001487
1488 case PERF_COUNTER_IOC_REFRESH:
1489 return perf_counter_refresh(counter, arg);
Paul Mackerrasd859e292009-01-17 18:10:22 +11001490 default:
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001491 return -ENOTTY;
Paul Mackerrasd859e292009-01-17 18:10:22 +11001492 }
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001493
1494 if (flags & PERF_IOC_FLAG_GROUP)
1495 perf_counter_for_each(counter, func);
1496 else
1497 perf_counter_for_each_child(counter, func);
1498
1499 return 0;
Paul Mackerrasd859e292009-01-17 18:10:22 +11001500}
1501
Peter Zijlstra38ff6672009-03-30 19:07:03 +02001502/*
1503 * Callers need to ensure there can be no nesting of this function, otherwise
1504 * the seqlock logic goes bad. We can not serialize this because the arch
1505 * code calls this from NMI context.
1506 */
1507void perf_counter_update_userpage(struct perf_counter *counter)
Paul Mackerras37d81822009-03-23 18:22:08 +01001508{
Peter Zijlstra38ff6672009-03-30 19:07:03 +02001509 struct perf_mmap_data *data;
1510 struct perf_counter_mmap_page *userpg;
1511
1512 rcu_read_lock();
1513 data = rcu_dereference(counter->data);
1514 if (!data)
1515 goto unlock;
1516
1517 userpg = data->user_page;
Paul Mackerras37d81822009-03-23 18:22:08 +01001518
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001519 /*
1520 * Disable preemption so as to not let the corresponding user-space
1521 * spin too long if we get preempted.
1522 */
1523 preempt_disable();
Paul Mackerras37d81822009-03-23 18:22:08 +01001524 ++userpg->lock;
Peter Zijlstra92f22a32009-04-02 11:12:04 +02001525 barrier();
Paul Mackerras37d81822009-03-23 18:22:08 +01001526 userpg->index = counter->hw.idx;
1527 userpg->offset = atomic64_read(&counter->count);
1528 if (counter->state == PERF_COUNTER_STATE_ACTIVE)
1529 userpg->offset -= atomic64_read(&counter->hw.prev_count);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001530
Peter Zijlstra92f22a32009-04-02 11:12:04 +02001531 barrier();
Paul Mackerras37d81822009-03-23 18:22:08 +01001532 ++userpg->lock;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001533 preempt_enable();
Peter Zijlstra38ff6672009-03-30 19:07:03 +02001534unlock:
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001535 rcu_read_unlock();
Paul Mackerras37d81822009-03-23 18:22:08 +01001536}
1537
1538static int perf_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1539{
1540 struct perf_counter *counter = vma->vm_file->private_data;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001541 struct perf_mmap_data *data;
1542 int ret = VM_FAULT_SIGBUS;
Paul Mackerras37d81822009-03-23 18:22:08 +01001543
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001544 rcu_read_lock();
1545 data = rcu_dereference(counter->data);
1546 if (!data)
1547 goto unlock;
Paul Mackerras37d81822009-03-23 18:22:08 +01001548
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001549 if (vmf->pgoff == 0) {
1550 vmf->page = virt_to_page(data->user_page);
1551 } else {
1552 int nr = vmf->pgoff - 1;
1553
1554 if ((unsigned)nr > data->nr_pages)
1555 goto unlock;
1556
1557 vmf->page = virt_to_page(data->data_pages[nr]);
1558 }
Paul Mackerras37d81822009-03-23 18:22:08 +01001559 get_page(vmf->page);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001560 ret = 0;
1561unlock:
1562 rcu_read_unlock();
1563
1564 return ret;
1565}
1566
1567static int perf_mmap_data_alloc(struct perf_counter *counter, int nr_pages)
1568{
1569 struct perf_mmap_data *data;
1570 unsigned long size;
1571 int i;
1572
1573 WARN_ON(atomic_read(&counter->mmap_count));
1574
1575 size = sizeof(struct perf_mmap_data);
1576 size += nr_pages * sizeof(void *);
1577
1578 data = kzalloc(size, GFP_KERNEL);
1579 if (!data)
1580 goto fail;
1581
1582 data->user_page = (void *)get_zeroed_page(GFP_KERNEL);
1583 if (!data->user_page)
1584 goto fail_user_page;
1585
1586 for (i = 0; i < nr_pages; i++) {
1587 data->data_pages[i] = (void *)get_zeroed_page(GFP_KERNEL);
1588 if (!data->data_pages[i])
1589 goto fail_data_pages;
1590 }
1591
1592 data->nr_pages = nr_pages;
Peter Zijlstra22c15582009-05-05 17:50:25 +02001593 atomic_set(&data->lock, -1);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001594
1595 rcu_assign_pointer(counter->data, data);
1596
Paul Mackerras37d81822009-03-23 18:22:08 +01001597 return 0;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001598
1599fail_data_pages:
1600 for (i--; i >= 0; i--)
1601 free_page((unsigned long)data->data_pages[i]);
1602
1603 free_page((unsigned long)data->user_page);
1604
1605fail_user_page:
1606 kfree(data);
1607
1608fail:
1609 return -ENOMEM;
1610}
1611
1612static void __perf_mmap_data_free(struct rcu_head *rcu_head)
1613{
1614 struct perf_mmap_data *data = container_of(rcu_head,
1615 struct perf_mmap_data, rcu_head);
1616 int i;
1617
1618 free_page((unsigned long)data->user_page);
1619 for (i = 0; i < data->nr_pages; i++)
1620 free_page((unsigned long)data->data_pages[i]);
1621 kfree(data);
1622}
1623
1624static void perf_mmap_data_free(struct perf_counter *counter)
1625{
1626 struct perf_mmap_data *data = counter->data;
1627
1628 WARN_ON(atomic_read(&counter->mmap_count));
1629
1630 rcu_assign_pointer(counter->data, NULL);
1631 call_rcu(&data->rcu_head, __perf_mmap_data_free);
1632}
1633
1634static void perf_mmap_open(struct vm_area_struct *vma)
1635{
1636 struct perf_counter *counter = vma->vm_file->private_data;
1637
1638 atomic_inc(&counter->mmap_count);
1639}
1640
1641static void perf_mmap_close(struct vm_area_struct *vma)
1642{
1643 struct perf_counter *counter = vma->vm_file->private_data;
1644
1645 if (atomic_dec_and_mutex_lock(&counter->mmap_count,
1646 &counter->mmap_mutex)) {
Peter Zijlstra789f90f2009-05-15 15:19:27 +02001647 struct user_struct *user = current_user();
1648
1649 atomic_long_sub(counter->data->nr_pages + 1, &user->locked_vm);
Peter Zijlstrac5078f72009-05-05 17:50:24 +02001650 vma->vm_mm->locked_vm -= counter->data->nr_locked;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001651 perf_mmap_data_free(counter);
1652 mutex_unlock(&counter->mmap_mutex);
1653 }
Paul Mackerras37d81822009-03-23 18:22:08 +01001654}
1655
1656static struct vm_operations_struct perf_mmap_vmops = {
Peter Zijlstraebb3c4c2009-04-06 11:45:05 +02001657 .open = perf_mmap_open,
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001658 .close = perf_mmap_close,
Paul Mackerras37d81822009-03-23 18:22:08 +01001659 .fault = perf_mmap_fault,
1660};
1661
1662static int perf_mmap(struct file *file, struct vm_area_struct *vma)
1663{
1664 struct perf_counter *counter = file->private_data;
Peter Zijlstra789f90f2009-05-15 15:19:27 +02001665 struct user_struct *user = current_user();
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001666 unsigned long vma_size;
1667 unsigned long nr_pages;
Peter Zijlstra789f90f2009-05-15 15:19:27 +02001668 unsigned long user_locked, user_lock_limit;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001669 unsigned long locked, lock_limit;
Peter Zijlstra789f90f2009-05-15 15:19:27 +02001670 long user_extra, extra;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001671 int ret = 0;
Paul Mackerras37d81822009-03-23 18:22:08 +01001672
1673 if (!(vma->vm_flags & VM_SHARED) || (vma->vm_flags & VM_WRITE))
1674 return -EINVAL;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001675
1676 vma_size = vma->vm_end - vma->vm_start;
1677 nr_pages = (vma_size / PAGE_SIZE) - 1;
1678
Peter Zijlstra7730d862009-03-25 12:48:31 +01001679 /*
1680 * If we have data pages ensure they're a power-of-two number, so we
1681 * can do bitmasks instead of modulo.
1682 */
1683 if (nr_pages != 0 && !is_power_of_2(nr_pages))
Paul Mackerras37d81822009-03-23 18:22:08 +01001684 return -EINVAL;
1685
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001686 if (vma_size != PAGE_SIZE * (1 + nr_pages))
Paul Mackerras37d81822009-03-23 18:22:08 +01001687 return -EINVAL;
1688
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001689 if (vma->vm_pgoff != 0)
1690 return -EINVAL;
Paul Mackerras37d81822009-03-23 18:22:08 +01001691
Peter Zijlstraebb3c4c2009-04-06 11:45:05 +02001692 mutex_lock(&counter->mmap_mutex);
1693 if (atomic_inc_not_zero(&counter->mmap_count)) {
1694 if (nr_pages != counter->data->nr_pages)
1695 ret = -EINVAL;
1696 goto unlock;
1697 }
1698
Peter Zijlstra789f90f2009-05-15 15:19:27 +02001699 user_extra = nr_pages + 1;
1700 user_lock_limit = sysctl_perf_counter_mlock >> (PAGE_SHIFT - 10);
1701 user_locked = atomic_long_read(&user->locked_vm) + user_extra;
Peter Zijlstrac5078f72009-05-05 17:50:24 +02001702
Peter Zijlstra789f90f2009-05-15 15:19:27 +02001703 extra = 0;
1704 if (user_locked > user_lock_limit)
1705 extra = user_locked - user_lock_limit;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001706
1707 lock_limit = current->signal->rlim[RLIMIT_MEMLOCK].rlim_cur;
1708 lock_limit >>= PAGE_SHIFT;
Peter Zijlstra789f90f2009-05-15 15:19:27 +02001709 locked = vma->vm_mm->locked_vm + extra;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001710
Peter Zijlstraebb3c4c2009-04-06 11:45:05 +02001711 if ((locked > lock_limit) && !capable(CAP_IPC_LOCK)) {
1712 ret = -EPERM;
1713 goto unlock;
1714 }
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001715
1716 WARN_ON(counter->data);
1717 ret = perf_mmap_data_alloc(counter, nr_pages);
Peter Zijlstraebb3c4c2009-04-06 11:45:05 +02001718 if (ret)
1719 goto unlock;
1720
1721 atomic_set(&counter->mmap_count, 1);
Peter Zijlstra789f90f2009-05-15 15:19:27 +02001722 atomic_long_add(user_extra, &user->locked_vm);
Peter Zijlstrac5078f72009-05-05 17:50:24 +02001723 vma->vm_mm->locked_vm += extra;
1724 counter->data->nr_locked = extra;
Peter Zijlstraebb3c4c2009-04-06 11:45:05 +02001725unlock:
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001726 mutex_unlock(&counter->mmap_mutex);
Paul Mackerras37d81822009-03-23 18:22:08 +01001727
1728 vma->vm_flags &= ~VM_MAYWRITE;
1729 vma->vm_flags |= VM_RESERVED;
1730 vma->vm_ops = &perf_mmap_vmops;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001731
1732 return ret;
Paul Mackerras37d81822009-03-23 18:22:08 +01001733}
1734
Peter Zijlstra3c446b32009-04-06 11:45:01 +02001735static int perf_fasync(int fd, struct file *filp, int on)
1736{
1737 struct perf_counter *counter = filp->private_data;
1738 struct inode *inode = filp->f_path.dentry->d_inode;
1739 int retval;
1740
1741 mutex_lock(&inode->i_mutex);
1742 retval = fasync_helper(fd, filp, on, &counter->fasync);
1743 mutex_unlock(&inode->i_mutex);
1744
1745 if (retval < 0)
1746 return retval;
1747
1748 return 0;
1749}
1750
Thomas Gleixner0793a612008-12-04 20:12:29 +01001751static const struct file_operations perf_fops = {
1752 .release = perf_release,
1753 .read = perf_read,
1754 .poll = perf_poll,
Paul Mackerrasd859e292009-01-17 18:10:22 +11001755 .unlocked_ioctl = perf_ioctl,
1756 .compat_ioctl = perf_ioctl,
Paul Mackerras37d81822009-03-23 18:22:08 +01001757 .mmap = perf_mmap,
Peter Zijlstra3c446b32009-04-06 11:45:01 +02001758 .fasync = perf_fasync,
Thomas Gleixner0793a612008-12-04 20:12:29 +01001759};
1760
Peter Zijlstra15dbf272009-03-13 12:21:32 +01001761/*
Peter Zijlstra925d5192009-03-30 19:07:02 +02001762 * Perf counter wakeup
1763 *
1764 * If there's data, ensure we set the poll() state and publish everything
1765 * to user-space before waking everybody up.
1766 */
1767
1768void perf_counter_wakeup(struct perf_counter *counter)
1769{
Peter Zijlstra925d5192009-03-30 19:07:02 +02001770 wake_up_all(&counter->waitq);
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02001771
1772 if (counter->pending_kill) {
1773 kill_fasync(&counter->fasync, SIGIO, counter->pending_kill);
1774 counter->pending_kill = 0;
1775 }
Peter Zijlstra925d5192009-03-30 19:07:02 +02001776}
1777
1778/*
1779 * Pending wakeups
1780 *
1781 * Handle the case where we need to wakeup up from NMI (or rq->lock) context.
1782 *
1783 * The NMI bit means we cannot possibly take locks. Therefore, maintain a
1784 * single linked list and use cmpxchg() to add entries lockless.
1785 */
1786
Peter Zijlstra79f14642009-04-06 11:45:07 +02001787static void perf_pending_counter(struct perf_pending_entry *entry)
1788{
1789 struct perf_counter *counter = container_of(entry,
1790 struct perf_counter, pending);
1791
1792 if (counter->pending_disable) {
1793 counter->pending_disable = 0;
1794 perf_counter_disable(counter);
1795 }
1796
1797 if (counter->pending_wakeup) {
1798 counter->pending_wakeup = 0;
1799 perf_counter_wakeup(counter);
1800 }
1801}
1802
Peter Zijlstra671dec52009-04-06 11:45:02 +02001803#define PENDING_TAIL ((struct perf_pending_entry *)-1UL)
Peter Zijlstra925d5192009-03-30 19:07:02 +02001804
Peter Zijlstra671dec52009-04-06 11:45:02 +02001805static DEFINE_PER_CPU(struct perf_pending_entry *, perf_pending_head) = {
Peter Zijlstra925d5192009-03-30 19:07:02 +02001806 PENDING_TAIL,
1807};
1808
Peter Zijlstra671dec52009-04-06 11:45:02 +02001809static void perf_pending_queue(struct perf_pending_entry *entry,
1810 void (*func)(struct perf_pending_entry *))
Peter Zijlstra925d5192009-03-30 19:07:02 +02001811{
Peter Zijlstra671dec52009-04-06 11:45:02 +02001812 struct perf_pending_entry **head;
Peter Zijlstra925d5192009-03-30 19:07:02 +02001813
Peter Zijlstra671dec52009-04-06 11:45:02 +02001814 if (cmpxchg(&entry->next, NULL, PENDING_TAIL) != NULL)
Peter Zijlstra925d5192009-03-30 19:07:02 +02001815 return;
1816
Peter Zijlstra671dec52009-04-06 11:45:02 +02001817 entry->func = func;
1818
1819 head = &get_cpu_var(perf_pending_head);
Peter Zijlstra925d5192009-03-30 19:07:02 +02001820
1821 do {
Peter Zijlstra671dec52009-04-06 11:45:02 +02001822 entry->next = *head;
1823 } while (cmpxchg(head, entry->next, entry) != entry->next);
Peter Zijlstra925d5192009-03-30 19:07:02 +02001824
1825 set_perf_counter_pending();
1826
Peter Zijlstra671dec52009-04-06 11:45:02 +02001827 put_cpu_var(perf_pending_head);
Peter Zijlstra925d5192009-03-30 19:07:02 +02001828}
1829
1830static int __perf_pending_run(void)
1831{
Peter Zijlstra671dec52009-04-06 11:45:02 +02001832 struct perf_pending_entry *list;
Peter Zijlstra925d5192009-03-30 19:07:02 +02001833 int nr = 0;
1834
Peter Zijlstra671dec52009-04-06 11:45:02 +02001835 list = xchg(&__get_cpu_var(perf_pending_head), PENDING_TAIL);
Peter Zijlstra925d5192009-03-30 19:07:02 +02001836 while (list != PENDING_TAIL) {
Peter Zijlstra671dec52009-04-06 11:45:02 +02001837 void (*func)(struct perf_pending_entry *);
1838 struct perf_pending_entry *entry = list;
Peter Zijlstra925d5192009-03-30 19:07:02 +02001839
1840 list = list->next;
1841
Peter Zijlstra671dec52009-04-06 11:45:02 +02001842 func = entry->func;
1843 entry->next = NULL;
Peter Zijlstra925d5192009-03-30 19:07:02 +02001844 /*
1845 * Ensure we observe the unqueue before we issue the wakeup,
1846 * so that we won't be waiting forever.
1847 * -- see perf_not_pending().
1848 */
1849 smp_wmb();
1850
Peter Zijlstra671dec52009-04-06 11:45:02 +02001851 func(entry);
Peter Zijlstra925d5192009-03-30 19:07:02 +02001852 nr++;
1853 }
1854
1855 return nr;
1856}
1857
1858static inline int perf_not_pending(struct perf_counter *counter)
1859{
1860 /*
1861 * If we flush on whatever cpu we run, there is a chance we don't
1862 * need to wait.
1863 */
1864 get_cpu();
1865 __perf_pending_run();
1866 put_cpu();
1867
1868 /*
1869 * Ensure we see the proper queue state before going to sleep
1870 * so that we do not miss the wakeup. -- see perf_pending_handle()
1871 */
1872 smp_rmb();
Peter Zijlstra671dec52009-04-06 11:45:02 +02001873 return counter->pending.next == NULL;
Peter Zijlstra925d5192009-03-30 19:07:02 +02001874}
1875
1876static void perf_pending_sync(struct perf_counter *counter)
1877{
1878 wait_event(counter->waitq, perf_not_pending(counter));
1879}
1880
1881void perf_counter_do_pending(void)
1882{
1883 __perf_pending_run();
1884}
1885
1886/*
Peter Zijlstra394ee072009-03-30 19:07:14 +02001887 * Callchain support -- arch specific
1888 */
1889
Peter Zijlstra9c03d882009-04-06 11:45:00 +02001890__weak struct perf_callchain_entry *perf_callchain(struct pt_regs *regs)
Peter Zijlstra394ee072009-03-30 19:07:14 +02001891{
1892 return NULL;
1893}
1894
1895/*
Peter Zijlstra0322cd62009-03-19 20:26:19 +01001896 * Output
1897 */
1898
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01001899struct perf_output_handle {
1900 struct perf_counter *counter;
1901 struct perf_mmap_data *data;
1902 unsigned int offset;
Peter Zijlstra63e35b22009-03-25 12:30:24 +01001903 unsigned int head;
Peter Zijlstra78d613e2009-03-30 19:07:11 +02001904 int nmi;
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02001905 int overflow;
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001906 int locked;
1907 unsigned long flags;
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01001908};
1909
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001910static void perf_output_wakeup(struct perf_output_handle *handle)
Peter Zijlstra78d613e2009-03-30 19:07:11 +02001911{
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001912 atomic_set(&handle->data->poll, POLL_IN);
1913
Peter Zijlstra671dec52009-04-06 11:45:02 +02001914 if (handle->nmi) {
Peter Zijlstra79f14642009-04-06 11:45:07 +02001915 handle->counter->pending_wakeup = 1;
Peter Zijlstra671dec52009-04-06 11:45:02 +02001916 perf_pending_queue(&handle->counter->pending,
Peter Zijlstra79f14642009-04-06 11:45:07 +02001917 perf_pending_counter);
Peter Zijlstra671dec52009-04-06 11:45:02 +02001918 } else
Peter Zijlstra78d613e2009-03-30 19:07:11 +02001919 perf_counter_wakeup(handle->counter);
1920}
1921
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001922/*
1923 * Curious locking construct.
1924 *
1925 * We need to ensure a later event doesn't publish a head when a former
1926 * event isn't done writing. However since we need to deal with NMIs we
1927 * cannot fully serialize things.
1928 *
1929 * What we do is serialize between CPUs so we only have to deal with NMI
1930 * nesting on a single CPU.
1931 *
1932 * We only publish the head (and generate a wakeup) when the outer-most
1933 * event completes.
1934 */
1935static void perf_output_lock(struct perf_output_handle *handle)
1936{
1937 struct perf_mmap_data *data = handle->data;
1938 int cpu;
1939
1940 handle->locked = 0;
1941
1942 local_irq_save(handle->flags);
1943 cpu = smp_processor_id();
1944
1945 if (in_nmi() && atomic_read(&data->lock) == cpu)
1946 return;
1947
Peter Zijlstra22c15582009-05-05 17:50:25 +02001948 while (atomic_cmpxchg(&data->lock, -1, cpu) != -1)
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001949 cpu_relax();
1950
1951 handle->locked = 1;
1952}
1953
1954static void perf_output_unlock(struct perf_output_handle *handle)
1955{
1956 struct perf_mmap_data *data = handle->data;
1957 int head, cpu;
1958
Peter Zijlstrac66de4a2009-05-05 17:50:22 +02001959 data->done_head = data->head;
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001960
1961 if (!handle->locked)
1962 goto out;
1963
1964again:
1965 /*
1966 * The xchg implies a full barrier that ensures all writes are done
1967 * before we publish the new head, matched by a rmb() in userspace when
1968 * reading this position.
1969 */
Peter Zijlstrac66de4a2009-05-05 17:50:22 +02001970 while ((head = atomic_xchg(&data->done_head, 0)))
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001971 data->user_page->data_head = head;
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001972
1973 /*
Peter Zijlstrac66de4a2009-05-05 17:50:22 +02001974 * NMI can happen here, which means we can miss a done_head update.
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001975 */
1976
Peter Zijlstra22c15582009-05-05 17:50:25 +02001977 cpu = atomic_xchg(&data->lock, -1);
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001978 WARN_ON_ONCE(cpu != smp_processor_id());
1979
1980 /*
1981 * Therefore we have to validate we did not indeed do so.
1982 */
Peter Zijlstrac66de4a2009-05-05 17:50:22 +02001983 if (unlikely(atomic_read(&data->done_head))) {
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001984 /*
1985 * Since we had it locked, we can lock it again.
1986 */
Peter Zijlstra22c15582009-05-05 17:50:25 +02001987 while (atomic_cmpxchg(&data->lock, -1, cpu) != -1)
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001988 cpu_relax();
1989
1990 goto again;
1991 }
1992
Peter Zijlstrac66de4a2009-05-05 17:50:22 +02001993 if (atomic_xchg(&data->wakeup, 0))
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001994 perf_output_wakeup(handle);
1995out:
1996 local_irq_restore(handle->flags);
1997}
1998
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01001999static int perf_output_begin(struct perf_output_handle *handle,
Peter Zijlstra78d613e2009-03-30 19:07:11 +02002000 struct perf_counter *counter, unsigned int size,
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02002001 int nmi, int overflow)
Peter Zijlstra0322cd62009-03-19 20:26:19 +01002002{
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002003 struct perf_mmap_data *data;
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002004 unsigned int offset, head;
Peter Zijlstra0322cd62009-03-19 20:26:19 +01002005
Peter Zijlstra2023b352009-05-05 17:50:26 +02002006 /*
2007 * For inherited counters we send all the output towards the parent.
2008 */
2009 if (counter->parent)
2010 counter = counter->parent;
2011
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002012 rcu_read_lock();
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002013 data = rcu_dereference(counter->data);
2014 if (!data)
2015 goto out;
Peter Zijlstra0322cd62009-03-19 20:26:19 +01002016
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002017 handle->data = data;
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02002018 handle->counter = counter;
2019 handle->nmi = nmi;
2020 handle->overflow = overflow;
Peter Zijlstra78d613e2009-03-30 19:07:11 +02002021
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002022 if (!data->nr_pages)
Peter Zijlstra78d613e2009-03-30 19:07:11 +02002023 goto fail;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002024
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002025 perf_output_lock(handle);
2026
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002027 do {
2028 offset = head = atomic_read(&data->head);
Peter Zijlstrac7138f32009-03-24 13:18:16 +01002029 head += size;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002030 } while (atomic_cmpxchg(&data->head, offset, head) != offset);
2031
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002032 handle->offset = offset;
Peter Zijlstra63e35b22009-03-25 12:30:24 +01002033 handle->head = head;
Peter Zijlstrac66de4a2009-05-05 17:50:22 +02002034
2035 if ((offset >> PAGE_SHIFT) != (head >> PAGE_SHIFT))
2036 atomic_set(&data->wakeup, 1);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002037
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002038 return 0;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002039
Peter Zijlstra78d613e2009-03-30 19:07:11 +02002040fail:
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002041 perf_output_wakeup(handle);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002042out:
2043 rcu_read_unlock();
2044
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002045 return -ENOSPC;
2046}
2047
2048static void perf_output_copy(struct perf_output_handle *handle,
2049 void *buf, unsigned int len)
2050{
2051 unsigned int pages_mask;
2052 unsigned int offset;
2053 unsigned int size;
2054 void **pages;
2055
2056 offset = handle->offset;
2057 pages_mask = handle->data->nr_pages - 1;
2058 pages = handle->data->data_pages;
2059
2060 do {
2061 unsigned int page_offset;
2062 int nr;
2063
2064 nr = (offset >> PAGE_SHIFT) & pages_mask;
2065 page_offset = offset & (PAGE_SIZE - 1);
2066 size = min_t(unsigned int, PAGE_SIZE - page_offset, len);
2067
2068 memcpy(pages[nr] + page_offset, buf, size);
2069
2070 len -= size;
2071 buf += size;
2072 offset += size;
2073 } while (len);
2074
2075 handle->offset = offset;
Peter Zijlstra63e35b22009-03-25 12:30:24 +01002076
Peter Zijlstra53020fe2009-05-13 21:26:19 +02002077 /*
2078 * Check we didn't copy past our reservation window, taking the
2079 * possible unsigned int wrap into account.
2080 */
2081 WARN_ON_ONCE(((int)(handle->head - handle->offset)) < 0);
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002082}
2083
Peter Zijlstra5c148192009-03-25 12:30:23 +01002084#define perf_output_put(handle, x) \
2085 perf_output_copy((handle), &(x), sizeof(x))
2086
Peter Zijlstra78d613e2009-03-30 19:07:11 +02002087static void perf_output_end(struct perf_output_handle *handle)
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002088{
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002089 struct perf_counter *counter = handle->counter;
2090 struct perf_mmap_data *data = handle->data;
2091
2092 int wakeup_events = counter->hw_event.wakeup_events;
Peter Zijlstrac4578102009-04-02 11:12:01 +02002093
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02002094 if (handle->overflow && wakeup_events) {
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002095 int events = atomic_inc_return(&data->events);
Peter Zijlstrac4578102009-04-02 11:12:01 +02002096 if (events >= wakeup_events) {
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002097 atomic_sub(wakeup_events, &data->events);
Peter Zijlstrac66de4a2009-05-05 17:50:22 +02002098 atomic_set(&data->wakeup, 1);
Peter Zijlstrac4578102009-04-02 11:12:01 +02002099 }
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002100 }
2101
2102 perf_output_unlock(handle);
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002103 rcu_read_unlock();
2104}
2105
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002106static void perf_counter_output(struct perf_counter *counter,
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002107 int nmi, struct pt_regs *regs, u64 addr)
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002108{
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002109 int ret;
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002110 u64 record_type = counter->hw_event.record_type;
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002111 struct perf_output_handle handle;
2112 struct perf_event_header header;
2113 u64 ip;
Peter Zijlstra5c148192009-03-25 12:30:23 +01002114 struct {
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01002115 u32 pid, tid;
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002116 } tid_entry;
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002117 struct {
2118 u64 event;
2119 u64 counter;
2120 } group_entry;
Peter Zijlstra394ee072009-03-30 19:07:14 +02002121 struct perf_callchain_entry *callchain = NULL;
2122 int callchain_size = 0;
Peter Zijlstra339f7c92009-04-06 11:45:06 +02002123 u64 time;
Peter Zijlstraf370e1e2009-05-08 18:52:24 +02002124 struct {
2125 u32 cpu, reserved;
2126 } cpu_entry;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002127
Peter Zijlstra6b6e5482009-04-08 15:01:27 +02002128 header.type = 0;
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002129 header.size = sizeof(header);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002130
Peter Zijlstra6b6e5482009-04-08 15:01:27 +02002131 header.misc = PERF_EVENT_MISC_OVERFLOW;
Paul Mackerras9d23a902009-05-14 21:48:08 +10002132 header.misc |= perf_misc_flags(regs);
Peter Zijlstra6fab0192009-04-08 15:01:26 +02002133
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002134 if (record_type & PERF_RECORD_IP) {
Paul Mackerras9d23a902009-05-14 21:48:08 +10002135 ip = perf_instruction_pointer(regs);
Peter Zijlstra6b6e5482009-04-08 15:01:27 +02002136 header.type |= PERF_RECORD_IP;
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002137 header.size += sizeof(ip);
2138 }
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01002139
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002140 if (record_type & PERF_RECORD_TID) {
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01002141 /* namespace issues */
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002142 tid_entry.pid = current->group_leader->pid;
2143 tid_entry.tid = current->pid;
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01002144
Peter Zijlstra6b6e5482009-04-08 15:01:27 +02002145 header.type |= PERF_RECORD_TID;
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002146 header.size += sizeof(tid_entry);
2147 }
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01002148
Peter Zijlstra4d855452009-04-08 15:01:32 +02002149 if (record_type & PERF_RECORD_TIME) {
2150 /*
2151 * Maybe do better on x86 and provide cpu_clock_nmi()
2152 */
2153 time = sched_clock();
2154
2155 header.type |= PERF_RECORD_TIME;
2156 header.size += sizeof(u64);
2157 }
2158
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002159 if (record_type & PERF_RECORD_ADDR) {
2160 header.type |= PERF_RECORD_ADDR;
2161 header.size += sizeof(u64);
2162 }
2163
Peter Zijlstraa85f61a2009-05-08 18:52:23 +02002164 if (record_type & PERF_RECORD_CONFIG) {
2165 header.type |= PERF_RECORD_CONFIG;
2166 header.size += sizeof(u64);
2167 }
2168
Peter Zijlstraf370e1e2009-05-08 18:52:24 +02002169 if (record_type & PERF_RECORD_CPU) {
2170 header.type |= PERF_RECORD_CPU;
2171 header.size += sizeof(cpu_entry);
2172
2173 cpu_entry.cpu = raw_smp_processor_id();
2174 }
2175
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002176 if (record_type & PERF_RECORD_GROUP) {
Peter Zijlstra6b6e5482009-04-08 15:01:27 +02002177 header.type |= PERF_RECORD_GROUP;
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002178 header.size += sizeof(u64) +
2179 counter->nr_siblings * sizeof(group_entry);
2180 }
2181
2182 if (record_type & PERF_RECORD_CALLCHAIN) {
Peter Zijlstra394ee072009-03-30 19:07:14 +02002183 callchain = perf_callchain(regs);
2184
2185 if (callchain) {
Peter Zijlstra9c03d882009-04-06 11:45:00 +02002186 callchain_size = (1 + callchain->nr) * sizeof(u64);
Peter Zijlstra394ee072009-03-30 19:07:14 +02002187
Peter Zijlstra6b6e5482009-04-08 15:01:27 +02002188 header.type |= PERF_RECORD_CALLCHAIN;
Peter Zijlstra394ee072009-03-30 19:07:14 +02002189 header.size += callchain_size;
2190 }
2191 }
2192
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02002193 ret = perf_output_begin(&handle, counter, header.size, nmi, 1);
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002194 if (ret)
2195 return;
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01002196
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002197 perf_output_put(&handle, header);
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002198
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002199 if (record_type & PERF_RECORD_IP)
2200 perf_output_put(&handle, ip);
2201
2202 if (record_type & PERF_RECORD_TID)
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002203 perf_output_put(&handle, tid_entry);
2204
Peter Zijlstra4d855452009-04-08 15:01:32 +02002205 if (record_type & PERF_RECORD_TIME)
2206 perf_output_put(&handle, time);
2207
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002208 if (record_type & PERF_RECORD_ADDR)
2209 perf_output_put(&handle, addr);
2210
Peter Zijlstraa85f61a2009-05-08 18:52:23 +02002211 if (record_type & PERF_RECORD_CONFIG)
2212 perf_output_put(&handle, counter->hw_event.config);
2213
Peter Zijlstraf370e1e2009-05-08 18:52:24 +02002214 if (record_type & PERF_RECORD_CPU)
2215 perf_output_put(&handle, cpu_entry);
2216
Peter Zijlstra2023b352009-05-05 17:50:26 +02002217 /*
2218 * XXX PERF_RECORD_GROUP vs inherited counters seems difficult.
2219 */
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002220 if (record_type & PERF_RECORD_GROUP) {
2221 struct perf_counter *leader, *sub;
2222 u64 nr = counter->nr_siblings;
2223
2224 perf_output_put(&handle, nr);
2225
2226 leader = counter->group_leader;
2227 list_for_each_entry(sub, &leader->sibling_list, list_entry) {
2228 if (sub != counter)
Robert Richter4aeb0b42009-04-29 12:47:03 +02002229 sub->pmu->read(sub);
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002230
2231 group_entry.event = sub->hw_event.config;
2232 group_entry.counter = atomic64_read(&sub->count);
2233
2234 perf_output_put(&handle, group_entry);
2235 }
2236 }
2237
Peter Zijlstra394ee072009-03-30 19:07:14 +02002238 if (callchain)
2239 perf_output_copy(&handle, callchain, callchain_size);
2240
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002241 perf_output_end(&handle);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002242}
2243
Peter Zijlstra0322cd62009-03-19 20:26:19 +01002244/*
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02002245 * comm tracking
2246 */
2247
2248struct perf_comm_event {
2249 struct task_struct *task;
2250 char *comm;
2251 int comm_size;
2252
2253 struct {
2254 struct perf_event_header header;
2255
2256 u32 pid;
2257 u32 tid;
2258 } event;
2259};
2260
2261static void perf_counter_comm_output(struct perf_counter *counter,
2262 struct perf_comm_event *comm_event)
2263{
2264 struct perf_output_handle handle;
2265 int size = comm_event->event.header.size;
2266 int ret = perf_output_begin(&handle, counter, size, 0, 0);
2267
2268 if (ret)
2269 return;
2270
2271 perf_output_put(&handle, comm_event->event);
2272 perf_output_copy(&handle, comm_event->comm,
2273 comm_event->comm_size);
2274 perf_output_end(&handle);
2275}
2276
2277static int perf_counter_comm_match(struct perf_counter *counter,
2278 struct perf_comm_event *comm_event)
2279{
2280 if (counter->hw_event.comm &&
2281 comm_event->event.header.type == PERF_EVENT_COMM)
2282 return 1;
2283
2284 return 0;
2285}
2286
2287static void perf_counter_comm_ctx(struct perf_counter_context *ctx,
2288 struct perf_comm_event *comm_event)
2289{
2290 struct perf_counter *counter;
2291
2292 if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
2293 return;
2294
2295 rcu_read_lock();
2296 list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
2297 if (perf_counter_comm_match(counter, comm_event))
2298 perf_counter_comm_output(counter, comm_event);
2299 }
2300 rcu_read_unlock();
2301}
2302
2303static void perf_counter_comm_event(struct perf_comm_event *comm_event)
2304{
2305 struct perf_cpu_context *cpuctx;
2306 unsigned int size;
2307 char *comm = comm_event->task->comm;
2308
Ingo Molnar888fcee2009-04-09 09:48:22 +02002309 size = ALIGN(strlen(comm)+1, sizeof(u64));
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02002310
2311 comm_event->comm = comm;
2312 comm_event->comm_size = size;
2313
2314 comm_event->event.header.size = sizeof(comm_event->event) + size;
2315
2316 cpuctx = &get_cpu_var(perf_cpu_context);
2317 perf_counter_comm_ctx(&cpuctx->ctx, comm_event);
2318 put_cpu_var(perf_cpu_context);
2319
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10002320 perf_counter_comm_ctx(current->perf_counter_ctxp, comm_event);
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02002321}
2322
2323void perf_counter_comm(struct task_struct *task)
2324{
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02002325 struct perf_comm_event comm_event;
2326
2327 if (!atomic_read(&nr_comm_tracking))
2328 return;
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10002329 if (!current->perf_counter_ctxp)
2330 return;
2331
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02002332 comm_event = (struct perf_comm_event){
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02002333 .task = task,
2334 .event = {
2335 .header = { .type = PERF_EVENT_COMM, },
2336 .pid = task->group_leader->pid,
2337 .tid = task->pid,
2338 },
2339 };
2340
2341 perf_counter_comm_event(&comm_event);
2342}
2343
2344/*
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002345 * mmap tracking
2346 */
2347
2348struct perf_mmap_event {
2349 struct file *file;
2350 char *file_name;
2351 int file_size;
2352
2353 struct {
2354 struct perf_event_header header;
2355
2356 u32 pid;
2357 u32 tid;
2358 u64 start;
2359 u64 len;
2360 u64 pgoff;
2361 } event;
2362};
2363
2364static void perf_counter_mmap_output(struct perf_counter *counter,
2365 struct perf_mmap_event *mmap_event)
2366{
2367 struct perf_output_handle handle;
2368 int size = mmap_event->event.header.size;
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02002369 int ret = perf_output_begin(&handle, counter, size, 0, 0);
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002370
2371 if (ret)
2372 return;
2373
2374 perf_output_put(&handle, mmap_event->event);
2375 perf_output_copy(&handle, mmap_event->file_name,
2376 mmap_event->file_size);
Peter Zijlstra78d613e2009-03-30 19:07:11 +02002377 perf_output_end(&handle);
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002378}
2379
2380static int perf_counter_mmap_match(struct perf_counter *counter,
2381 struct perf_mmap_event *mmap_event)
2382{
2383 if (counter->hw_event.mmap &&
2384 mmap_event->event.header.type == PERF_EVENT_MMAP)
2385 return 1;
2386
2387 if (counter->hw_event.munmap &&
2388 mmap_event->event.header.type == PERF_EVENT_MUNMAP)
2389 return 1;
2390
2391 return 0;
2392}
2393
2394static void perf_counter_mmap_ctx(struct perf_counter_context *ctx,
2395 struct perf_mmap_event *mmap_event)
2396{
2397 struct perf_counter *counter;
2398
2399 if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
2400 return;
2401
2402 rcu_read_lock();
2403 list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
2404 if (perf_counter_mmap_match(counter, mmap_event))
2405 perf_counter_mmap_output(counter, mmap_event);
2406 }
2407 rcu_read_unlock();
2408}
2409
2410static void perf_counter_mmap_event(struct perf_mmap_event *mmap_event)
2411{
2412 struct perf_cpu_context *cpuctx;
2413 struct file *file = mmap_event->file;
2414 unsigned int size;
2415 char tmp[16];
2416 char *buf = NULL;
2417 char *name;
2418
2419 if (file) {
2420 buf = kzalloc(PATH_MAX, GFP_KERNEL);
2421 if (!buf) {
2422 name = strncpy(tmp, "//enomem", sizeof(tmp));
2423 goto got_name;
2424 }
Peter Zijlstrad3d21c42009-04-09 10:53:46 +02002425 name = d_path(&file->f_path, buf, PATH_MAX);
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002426 if (IS_ERR(name)) {
2427 name = strncpy(tmp, "//toolong", sizeof(tmp));
2428 goto got_name;
2429 }
2430 } else {
2431 name = strncpy(tmp, "//anon", sizeof(tmp));
2432 goto got_name;
2433 }
2434
2435got_name:
Ingo Molnar888fcee2009-04-09 09:48:22 +02002436 size = ALIGN(strlen(name)+1, sizeof(u64));
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002437
2438 mmap_event->file_name = name;
2439 mmap_event->file_size = size;
2440
2441 mmap_event->event.header.size = sizeof(mmap_event->event) + size;
2442
2443 cpuctx = &get_cpu_var(perf_cpu_context);
2444 perf_counter_mmap_ctx(&cpuctx->ctx, mmap_event);
2445 put_cpu_var(perf_cpu_context);
2446
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10002447 perf_counter_mmap_ctx(current->perf_counter_ctxp, mmap_event);
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002448
2449 kfree(buf);
2450}
2451
2452void perf_counter_mmap(unsigned long addr, unsigned long len,
2453 unsigned long pgoff, struct file *file)
2454{
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02002455 struct perf_mmap_event mmap_event;
2456
2457 if (!atomic_read(&nr_mmap_tracking))
2458 return;
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10002459 if (!current->perf_counter_ctxp)
2460 return;
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02002461
2462 mmap_event = (struct perf_mmap_event){
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002463 .file = file,
2464 .event = {
2465 .header = { .type = PERF_EVENT_MMAP, },
2466 .pid = current->group_leader->pid,
2467 .tid = current->pid,
2468 .start = addr,
2469 .len = len,
2470 .pgoff = pgoff,
2471 },
2472 };
2473
2474 perf_counter_mmap_event(&mmap_event);
2475}
2476
2477void perf_counter_munmap(unsigned long addr, unsigned long len,
2478 unsigned long pgoff, struct file *file)
2479{
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02002480 struct perf_mmap_event mmap_event;
2481
2482 if (!atomic_read(&nr_munmap_tracking))
2483 return;
2484
2485 mmap_event = (struct perf_mmap_event){
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002486 .file = file,
2487 .event = {
2488 .header = { .type = PERF_EVENT_MUNMAP, },
2489 .pid = current->group_leader->pid,
2490 .tid = current->pid,
2491 .start = addr,
2492 .len = len,
2493 .pgoff = pgoff,
2494 },
2495 };
2496
2497 perf_counter_mmap_event(&mmap_event);
2498}
2499
2500/*
Peter Zijlstra26b119b2009-05-20 12:21:20 +02002501 *
2502 */
2503
2504static void perf_log_period(struct perf_counter *counter, u64 period)
2505{
2506 struct perf_output_handle handle;
2507 int ret;
2508
2509 struct {
2510 struct perf_event_header header;
2511 u64 time;
2512 u64 period;
2513 } freq_event = {
2514 .header = {
2515 .type = PERF_EVENT_PERIOD,
2516 .misc = 0,
2517 .size = sizeof(freq_event),
2518 },
2519 .time = sched_clock(),
2520 .period = period,
2521 };
2522
2523 if (counter->hw.irq_period == period)
2524 return;
2525
2526 ret = perf_output_begin(&handle, counter, sizeof(freq_event), 0, 0);
2527 if (ret)
2528 return;
2529
2530 perf_output_put(&handle, freq_event);
2531 perf_output_end(&handle);
2532}
2533
2534/*
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002535 * Generic counter overflow handling.
2536 */
2537
2538int perf_counter_overflow(struct perf_counter *counter,
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002539 int nmi, struct pt_regs *regs, u64 addr)
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002540{
Peter Zijlstra79f14642009-04-06 11:45:07 +02002541 int events = atomic_read(&counter->event_limit);
2542 int ret = 0;
2543
Peter Zijlstra60db5e02009-05-15 15:19:28 +02002544 counter->hw.interrupts++;
2545
Peter Zijlstra2023b352009-05-05 17:50:26 +02002546 /*
2547 * XXX event_limit might not quite work as expected on inherited
2548 * counters
2549 */
2550
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02002551 counter->pending_kill = POLL_IN;
Peter Zijlstra79f14642009-04-06 11:45:07 +02002552 if (events && atomic_dec_and_test(&counter->event_limit)) {
2553 ret = 1;
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02002554 counter->pending_kill = POLL_HUP;
Peter Zijlstra79f14642009-04-06 11:45:07 +02002555 if (nmi) {
2556 counter->pending_disable = 1;
2557 perf_pending_queue(&counter->pending,
2558 perf_pending_counter);
2559 } else
2560 perf_counter_disable(counter);
2561 }
2562
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002563 perf_counter_output(counter, nmi, regs, addr);
Peter Zijlstra79f14642009-04-06 11:45:07 +02002564 return ret;
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002565}
2566
2567/*
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002568 * Generic software counter infrastructure
2569 */
2570
2571static void perf_swcounter_update(struct perf_counter *counter)
2572{
2573 struct hw_perf_counter *hwc = &counter->hw;
2574 u64 prev, now;
2575 s64 delta;
2576
2577again:
2578 prev = atomic64_read(&hwc->prev_count);
2579 now = atomic64_read(&hwc->count);
2580 if (atomic64_cmpxchg(&hwc->prev_count, prev, now) != prev)
2581 goto again;
2582
2583 delta = now - prev;
2584
2585 atomic64_add(delta, &counter->count);
2586 atomic64_sub(delta, &hwc->period_left);
2587}
2588
2589static void perf_swcounter_set_period(struct perf_counter *counter)
2590{
2591 struct hw_perf_counter *hwc = &counter->hw;
2592 s64 left = atomic64_read(&hwc->period_left);
2593 s64 period = hwc->irq_period;
2594
2595 if (unlikely(left <= -period)) {
2596 left = period;
2597 atomic64_set(&hwc->period_left, left);
2598 }
2599
2600 if (unlikely(left <= 0)) {
2601 left += period;
2602 atomic64_add(period, &hwc->period_left);
2603 }
2604
2605 atomic64_set(&hwc->prev_count, -left);
2606 atomic64_set(&hwc->count, -left);
2607}
2608
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002609static enum hrtimer_restart perf_swcounter_hrtimer(struct hrtimer *hrtimer)
2610{
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002611 enum hrtimer_restart ret = HRTIMER_RESTART;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002612 struct perf_counter *counter;
2613 struct pt_regs *regs;
Peter Zijlstra60db5e02009-05-15 15:19:28 +02002614 u64 period;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002615
2616 counter = container_of(hrtimer, struct perf_counter, hw.hrtimer);
Robert Richter4aeb0b42009-04-29 12:47:03 +02002617 counter->pmu->read(counter);
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002618
2619 regs = get_irq_regs();
2620 /*
2621 * In case we exclude kernel IPs or are somehow not in interrupt
2622 * context, provide the next best thing, the user IP.
2623 */
2624 if ((counter->hw_event.exclude_kernel || !regs) &&
2625 !counter->hw_event.exclude_user)
2626 regs = task_pt_regs(current);
2627
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002628 if (regs) {
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002629 if (perf_counter_overflow(counter, 0, regs, 0))
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002630 ret = HRTIMER_NORESTART;
2631 }
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002632
Peter Zijlstra60db5e02009-05-15 15:19:28 +02002633 period = max_t(u64, 10000, counter->hw.irq_period);
2634 hrtimer_forward_now(hrtimer, ns_to_ktime(period));
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002635
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002636 return ret;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002637}
2638
2639static void perf_swcounter_overflow(struct perf_counter *counter,
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002640 int nmi, struct pt_regs *regs, u64 addr)
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002641{
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002642 perf_swcounter_update(counter);
2643 perf_swcounter_set_period(counter);
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002644 if (perf_counter_overflow(counter, nmi, regs, addr))
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002645 /* soft-disable the counter */
2646 ;
2647
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002648}
2649
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002650static int perf_swcounter_match(struct perf_counter *counter,
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002651 enum perf_event_types type,
2652 u32 event, struct pt_regs *regs)
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002653{
2654 if (counter->state != PERF_COUNTER_STATE_ACTIVE)
2655 return 0;
2656
Peter Zijlstraf4a2deb2009-03-23 18:22:06 +01002657 if (perf_event_raw(&counter->hw_event))
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002658 return 0;
2659
Peter Zijlstraf4a2deb2009-03-23 18:22:06 +01002660 if (perf_event_type(&counter->hw_event) != type)
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002661 return 0;
2662
Peter Zijlstraf4a2deb2009-03-23 18:22:06 +01002663 if (perf_event_id(&counter->hw_event) != event)
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002664 return 0;
2665
2666 if (counter->hw_event.exclude_user && user_mode(regs))
2667 return 0;
2668
2669 if (counter->hw_event.exclude_kernel && !user_mode(regs))
2670 return 0;
2671
2672 return 1;
2673}
2674
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002675static void perf_swcounter_add(struct perf_counter *counter, u64 nr,
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002676 int nmi, struct pt_regs *regs, u64 addr)
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002677{
2678 int neg = atomic64_add_negative(nr, &counter->hw.count);
2679 if (counter->hw.irq_period && !neg)
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002680 perf_swcounter_overflow(counter, nmi, regs, addr);
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002681}
2682
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002683static void perf_swcounter_ctx_event(struct perf_counter_context *ctx,
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002684 enum perf_event_types type, u32 event,
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002685 u64 nr, int nmi, struct pt_regs *regs,
2686 u64 addr)
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002687{
2688 struct perf_counter *counter;
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002689
Peter Zijlstra01ef09d2009-03-19 20:26:11 +01002690 if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002691 return;
2692
Peter Zijlstra592903c2009-03-13 12:21:36 +01002693 rcu_read_lock();
2694 list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002695 if (perf_swcounter_match(counter, type, event, regs))
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002696 perf_swcounter_add(counter, nr, nmi, regs, addr);
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002697 }
Peter Zijlstra592903c2009-03-13 12:21:36 +01002698 rcu_read_unlock();
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002699}
2700
Peter Zijlstra96f6d442009-03-23 18:22:07 +01002701static int *perf_swcounter_recursion_context(struct perf_cpu_context *cpuctx)
2702{
2703 if (in_nmi())
2704 return &cpuctx->recursion[3];
2705
2706 if (in_irq())
2707 return &cpuctx->recursion[2];
2708
2709 if (in_softirq())
2710 return &cpuctx->recursion[1];
2711
2712 return &cpuctx->recursion[0];
2713}
2714
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002715static void __perf_swcounter_event(enum perf_event_types type, u32 event,
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002716 u64 nr, int nmi, struct pt_regs *regs,
2717 u64 addr)
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002718{
2719 struct perf_cpu_context *cpuctx = &get_cpu_var(perf_cpu_context);
Peter Zijlstra96f6d442009-03-23 18:22:07 +01002720 int *recursion = perf_swcounter_recursion_context(cpuctx);
2721
2722 if (*recursion)
2723 goto out;
2724
2725 (*recursion)++;
2726 barrier();
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002727
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002728 perf_swcounter_ctx_event(&cpuctx->ctx, type, event,
2729 nr, nmi, regs, addr);
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002730 if (cpuctx->task_ctx) {
2731 perf_swcounter_ctx_event(cpuctx->task_ctx, type, event,
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002732 nr, nmi, regs, addr);
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002733 }
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002734
Peter Zijlstra96f6d442009-03-23 18:22:07 +01002735 barrier();
2736 (*recursion)--;
2737
2738out:
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002739 put_cpu_var(perf_cpu_context);
2740}
2741
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002742void
2743perf_swcounter_event(u32 event, u64 nr, int nmi, struct pt_regs *regs, u64 addr)
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002744{
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002745 __perf_swcounter_event(PERF_TYPE_SOFTWARE, event, nr, nmi, regs, addr);
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002746}
2747
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002748static void perf_swcounter_read(struct perf_counter *counter)
2749{
2750 perf_swcounter_update(counter);
2751}
2752
2753static int perf_swcounter_enable(struct perf_counter *counter)
2754{
2755 perf_swcounter_set_period(counter);
2756 return 0;
2757}
2758
2759static void perf_swcounter_disable(struct perf_counter *counter)
2760{
2761 perf_swcounter_update(counter);
2762}
2763
Robert Richter4aeb0b42009-04-29 12:47:03 +02002764static const struct pmu perf_ops_generic = {
Peter Zijlstraac17dc82009-03-13 12:21:34 +01002765 .enable = perf_swcounter_enable,
2766 .disable = perf_swcounter_disable,
2767 .read = perf_swcounter_read,
2768};
2769
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002770/*
2771 * Software counter: cpu wall time clock
2772 */
2773
Paul Mackerras9abf8a02009-01-09 16:26:43 +11002774static void cpu_clock_perf_counter_update(struct perf_counter *counter)
2775{
2776 int cpu = raw_smp_processor_id();
2777 s64 prev;
2778 u64 now;
2779
2780 now = cpu_clock(cpu);
2781 prev = atomic64_read(&counter->hw.prev_count);
2782 atomic64_set(&counter->hw.prev_count, now);
2783 atomic64_add(now - prev, &counter->count);
2784}
2785
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002786static int cpu_clock_perf_counter_enable(struct perf_counter *counter)
2787{
2788 struct hw_perf_counter *hwc = &counter->hw;
2789 int cpu = raw_smp_processor_id();
2790
2791 atomic64_set(&hwc->prev_count, cpu_clock(cpu));
Peter Zijlstra039fc912009-03-13 16:43:47 +01002792 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
2793 hwc->hrtimer.function = perf_swcounter_hrtimer;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002794 if (hwc->irq_period) {
Peter Zijlstra60db5e02009-05-15 15:19:28 +02002795 u64 period = max_t(u64, 10000, hwc->irq_period);
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002796 __hrtimer_start_range_ns(&hwc->hrtimer,
Peter Zijlstra60db5e02009-05-15 15:19:28 +02002797 ns_to_ktime(period), 0,
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002798 HRTIMER_MODE_REL, 0);
2799 }
2800
2801 return 0;
2802}
2803
Ingo Molnar5c92d122008-12-11 13:21:10 +01002804static void cpu_clock_perf_counter_disable(struct perf_counter *counter)
2805{
Peter Zijlstrab986d7e2009-05-20 12:21:21 +02002806 if (counter->hw.irq_period)
2807 hrtimer_cancel(&counter->hw.hrtimer);
Paul Mackerras9abf8a02009-01-09 16:26:43 +11002808 cpu_clock_perf_counter_update(counter);
Ingo Molnar5c92d122008-12-11 13:21:10 +01002809}
2810
2811static void cpu_clock_perf_counter_read(struct perf_counter *counter)
2812{
Paul Mackerras9abf8a02009-01-09 16:26:43 +11002813 cpu_clock_perf_counter_update(counter);
Ingo Molnar5c92d122008-12-11 13:21:10 +01002814}
2815
Robert Richter4aeb0b42009-04-29 12:47:03 +02002816static const struct pmu perf_ops_cpu_clock = {
Ingo Molnar76715812008-12-17 14:20:28 +01002817 .enable = cpu_clock_perf_counter_enable,
2818 .disable = cpu_clock_perf_counter_disable,
2819 .read = cpu_clock_perf_counter_read,
Ingo Molnar5c92d122008-12-11 13:21:10 +01002820};
2821
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01002822/*
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002823 * Software counter: task time clock
2824 */
2825
Peter Zijlstrae30e08f2009-04-08 15:01:25 +02002826static void task_clock_perf_counter_update(struct perf_counter *counter, u64 now)
Ingo Molnarbae43c92008-12-11 14:03:20 +01002827{
Peter Zijlstrae30e08f2009-04-08 15:01:25 +02002828 u64 prev;
Ingo Molnar8cb391e2008-12-14 12:22:31 +01002829 s64 delta;
Ingo Molnarbae43c92008-12-11 14:03:20 +01002830
Peter Zijlstraa39d6f22009-04-06 11:45:11 +02002831 prev = atomic64_xchg(&counter->hw.prev_count, now);
Ingo Molnar8cb391e2008-12-14 12:22:31 +01002832 delta = now - prev;
Ingo Molnar8cb391e2008-12-14 12:22:31 +01002833 atomic64_add(delta, &counter->count);
Ingo Molnarbae43c92008-12-11 14:03:20 +01002834}
2835
Ingo Molnar95cdd2e2008-12-21 13:50:42 +01002836static int task_clock_perf_counter_enable(struct perf_counter *counter)
Ingo Molnar8cb391e2008-12-14 12:22:31 +01002837{
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002838 struct hw_perf_counter *hwc = &counter->hw;
Peter Zijlstraa39d6f22009-04-06 11:45:11 +02002839 u64 now;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002840
Peter Zijlstraa39d6f22009-04-06 11:45:11 +02002841 now = counter->ctx->time;
2842
2843 atomic64_set(&hwc->prev_count, now);
Peter Zijlstra039fc912009-03-13 16:43:47 +01002844 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
2845 hwc->hrtimer.function = perf_swcounter_hrtimer;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002846 if (hwc->irq_period) {
Peter Zijlstra60db5e02009-05-15 15:19:28 +02002847 u64 period = max_t(u64, 10000, hwc->irq_period);
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002848 __hrtimer_start_range_ns(&hwc->hrtimer,
Peter Zijlstra60db5e02009-05-15 15:19:28 +02002849 ns_to_ktime(period), 0,
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002850 HRTIMER_MODE_REL, 0);
2851 }
Ingo Molnar95cdd2e2008-12-21 13:50:42 +01002852
2853 return 0;
Ingo Molnar8cb391e2008-12-14 12:22:31 +01002854}
2855
2856static void task_clock_perf_counter_disable(struct perf_counter *counter)
2857{
Peter Zijlstrab986d7e2009-05-20 12:21:21 +02002858 if (counter->hw.irq_period)
2859 hrtimer_cancel(&counter->hw.hrtimer);
Peter Zijlstrae30e08f2009-04-08 15:01:25 +02002860 task_clock_perf_counter_update(counter, counter->ctx->time);
2861
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002862}
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01002863
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002864static void task_clock_perf_counter_read(struct perf_counter *counter)
2865{
Peter Zijlstrae30e08f2009-04-08 15:01:25 +02002866 u64 time;
2867
2868 if (!in_nmi()) {
2869 update_context_time(counter->ctx);
2870 time = counter->ctx->time;
2871 } else {
2872 u64 now = perf_clock();
2873 u64 delta = now - counter->ctx->timestamp;
2874 time = counter->ctx->time + delta;
2875 }
2876
2877 task_clock_perf_counter_update(counter, time);
Ingo Molnarbae43c92008-12-11 14:03:20 +01002878}
2879
Robert Richter4aeb0b42009-04-29 12:47:03 +02002880static const struct pmu perf_ops_task_clock = {
Ingo Molnar76715812008-12-17 14:20:28 +01002881 .enable = task_clock_perf_counter_enable,
2882 .disable = task_clock_perf_counter_disable,
2883 .read = task_clock_perf_counter_read,
Ingo Molnarbae43c92008-12-11 14:03:20 +01002884};
2885
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002886/*
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002887 * Software counter: cpu migrations
2888 */
2889
Paul Mackerras23a185c2009-02-09 22:42:47 +11002890static inline u64 get_cpu_migrations(struct perf_counter *counter)
Ingo Molnar6c594c22008-12-14 12:34:15 +01002891{
Paul Mackerras23a185c2009-02-09 22:42:47 +11002892 struct task_struct *curr = counter->ctx->task;
2893
2894 if (curr)
2895 return curr->se.nr_migrations;
2896 return cpu_nr_migrations(smp_processor_id());
Ingo Molnar6c594c22008-12-14 12:34:15 +01002897}
2898
2899static void cpu_migrations_perf_counter_update(struct perf_counter *counter)
2900{
2901 u64 prev, now;
2902 s64 delta;
2903
2904 prev = atomic64_read(&counter->hw.prev_count);
Paul Mackerras23a185c2009-02-09 22:42:47 +11002905 now = get_cpu_migrations(counter);
Ingo Molnar6c594c22008-12-14 12:34:15 +01002906
2907 atomic64_set(&counter->hw.prev_count, now);
2908
2909 delta = now - prev;
Ingo Molnar6c594c22008-12-14 12:34:15 +01002910
2911 atomic64_add(delta, &counter->count);
2912}
2913
2914static void cpu_migrations_perf_counter_read(struct perf_counter *counter)
2915{
2916 cpu_migrations_perf_counter_update(counter);
2917}
2918
Ingo Molnar95cdd2e2008-12-21 13:50:42 +01002919static int cpu_migrations_perf_counter_enable(struct perf_counter *counter)
Ingo Molnar6c594c22008-12-14 12:34:15 +01002920{
Paul Mackerrasc07c99b2009-02-13 22:10:34 +11002921 if (counter->prev_state <= PERF_COUNTER_STATE_OFF)
2922 atomic64_set(&counter->hw.prev_count,
2923 get_cpu_migrations(counter));
Ingo Molnar95cdd2e2008-12-21 13:50:42 +01002924 return 0;
Ingo Molnar6c594c22008-12-14 12:34:15 +01002925}
2926
2927static void cpu_migrations_perf_counter_disable(struct perf_counter *counter)
2928{
2929 cpu_migrations_perf_counter_update(counter);
2930}
2931
Robert Richter4aeb0b42009-04-29 12:47:03 +02002932static const struct pmu perf_ops_cpu_migrations = {
Ingo Molnar76715812008-12-17 14:20:28 +01002933 .enable = cpu_migrations_perf_counter_enable,
2934 .disable = cpu_migrations_perf_counter_disable,
2935 .read = cpu_migrations_perf_counter_read,
Ingo Molnar6c594c22008-12-14 12:34:15 +01002936};
2937
Peter Zijlstrae077df42009-03-19 20:26:17 +01002938#ifdef CONFIG_EVENT_PROFILE
2939void perf_tpcounter_event(int event_id)
2940{
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002941 struct pt_regs *regs = get_irq_regs();
2942
2943 if (!regs)
2944 regs = task_pt_regs(current);
2945
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002946 __perf_swcounter_event(PERF_TYPE_TRACEPOINT, event_id, 1, 1, regs, 0);
Peter Zijlstrae077df42009-03-19 20:26:17 +01002947}
Steven Whitehouseff7b1b42009-04-15 16:55:05 +01002948EXPORT_SYMBOL_GPL(perf_tpcounter_event);
Peter Zijlstrae077df42009-03-19 20:26:17 +01002949
2950extern int ftrace_profile_enable(int);
2951extern void ftrace_profile_disable(int);
2952
2953static void tp_perf_counter_destroy(struct perf_counter *counter)
2954{
Peter Zijlstraf4a2deb2009-03-23 18:22:06 +01002955 ftrace_profile_disable(perf_event_id(&counter->hw_event));
Peter Zijlstrae077df42009-03-19 20:26:17 +01002956}
2957
Robert Richter4aeb0b42009-04-29 12:47:03 +02002958static const struct pmu *tp_perf_counter_init(struct perf_counter *counter)
Peter Zijlstrae077df42009-03-19 20:26:17 +01002959{
Peter Zijlstraf4a2deb2009-03-23 18:22:06 +01002960 int event_id = perf_event_id(&counter->hw_event);
Peter Zijlstrae077df42009-03-19 20:26:17 +01002961 int ret;
2962
2963 ret = ftrace_profile_enable(event_id);
2964 if (ret)
2965 return NULL;
2966
2967 counter->destroy = tp_perf_counter_destroy;
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002968 counter->hw.irq_period = counter->hw_event.irq_period;
Peter Zijlstrae077df42009-03-19 20:26:17 +01002969
2970 return &perf_ops_generic;
2971}
2972#else
Robert Richter4aeb0b42009-04-29 12:47:03 +02002973static const struct pmu *tp_perf_counter_init(struct perf_counter *counter)
Peter Zijlstrae077df42009-03-19 20:26:17 +01002974{
2975 return NULL;
2976}
2977#endif
2978
Robert Richter4aeb0b42009-04-29 12:47:03 +02002979static const struct pmu *sw_perf_counter_init(struct perf_counter *counter)
Ingo Molnar5c92d122008-12-11 13:21:10 +01002980{
Robert Richter4aeb0b42009-04-29 12:47:03 +02002981 const struct pmu *pmu = NULL;
Ingo Molnar5c92d122008-12-11 13:21:10 +01002982
Paul Mackerras0475f9e2009-02-11 14:35:35 +11002983 /*
2984 * Software counters (currently) can't in general distinguish
2985 * between user, kernel and hypervisor events.
2986 * However, context switches and cpu migrations are considered
2987 * to be kernel events, and page faults are never hypervisor
2988 * events.
2989 */
Peter Zijlstraf4a2deb2009-03-23 18:22:06 +01002990 switch (perf_event_id(&counter->hw_event)) {
Ingo Molnar5c92d122008-12-11 13:21:10 +01002991 case PERF_COUNT_CPU_CLOCK:
Robert Richter4aeb0b42009-04-29 12:47:03 +02002992 pmu = &perf_ops_cpu_clock;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002993
Ingo Molnar5c92d122008-12-11 13:21:10 +01002994 break;
Ingo Molnarbae43c92008-12-11 14:03:20 +01002995 case PERF_COUNT_TASK_CLOCK:
Paul Mackerras23a185c2009-02-09 22:42:47 +11002996 /*
2997 * If the user instantiates this as a per-cpu counter,
2998 * use the cpu_clock counter instead.
2999 */
3000 if (counter->ctx->task)
Robert Richter4aeb0b42009-04-29 12:47:03 +02003001 pmu = &perf_ops_task_clock;
Paul Mackerras23a185c2009-02-09 22:42:47 +11003002 else
Robert Richter4aeb0b42009-04-29 12:47:03 +02003003 pmu = &perf_ops_cpu_clock;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01003004
Ingo Molnarbae43c92008-12-11 14:03:20 +01003005 break;
Ingo Molnare06c61a2008-12-14 14:44:31 +01003006 case PERF_COUNT_PAGE_FAULTS:
Peter Zijlstraac17dc82009-03-13 12:21:34 +01003007 case PERF_COUNT_PAGE_FAULTS_MIN:
3008 case PERF_COUNT_PAGE_FAULTS_MAJ:
Ingo Molnar5d6a27d2008-12-14 12:28:33 +01003009 case PERF_COUNT_CONTEXT_SWITCHES:
Robert Richter4aeb0b42009-04-29 12:47:03 +02003010 pmu = &perf_ops_generic;
Ingo Molnar5d6a27d2008-12-14 12:28:33 +01003011 break;
Ingo Molnar6c594c22008-12-14 12:34:15 +01003012 case PERF_COUNT_CPU_MIGRATIONS:
Paul Mackerras0475f9e2009-02-11 14:35:35 +11003013 if (!counter->hw_event.exclude_kernel)
Robert Richter4aeb0b42009-04-29 12:47:03 +02003014 pmu = &perf_ops_cpu_migrations;
Ingo Molnar6c594c22008-12-14 12:34:15 +01003015 break;
Ingo Molnar5c92d122008-12-11 13:21:10 +01003016 }
Peter Zijlstra15dbf272009-03-13 12:21:32 +01003017
Robert Richter4aeb0b42009-04-29 12:47:03 +02003018 return pmu;
Ingo Molnar5c92d122008-12-11 13:21:10 +01003019}
3020
Thomas Gleixner0793a612008-12-04 20:12:29 +01003021/*
3022 * Allocate and initialize a counter structure
3023 */
3024static struct perf_counter *
Ingo Molnar04289bb2008-12-11 08:38:42 +01003025perf_counter_alloc(struct perf_counter_hw_event *hw_event,
3026 int cpu,
Paul Mackerras23a185c2009-02-09 22:42:47 +11003027 struct perf_counter_context *ctx,
Ingo Molnar9b51f662008-12-12 13:49:45 +01003028 struct perf_counter *group_leader,
3029 gfp_t gfpflags)
Thomas Gleixner0793a612008-12-04 20:12:29 +01003030{
Robert Richter4aeb0b42009-04-29 12:47:03 +02003031 const struct pmu *pmu;
Ingo Molnar621a01e2008-12-11 12:46:46 +01003032 struct perf_counter *counter;
Peter Zijlstra60db5e02009-05-15 15:19:28 +02003033 struct hw_perf_counter *hwc;
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003034 long err;
Thomas Gleixner0793a612008-12-04 20:12:29 +01003035
Ingo Molnar9b51f662008-12-12 13:49:45 +01003036 counter = kzalloc(sizeof(*counter), gfpflags);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003037 if (!counter)
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003038 return ERR_PTR(-ENOMEM);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003039
Ingo Molnar04289bb2008-12-11 08:38:42 +01003040 /*
3041 * Single counters are their own group leaders, with an
3042 * empty sibling list:
3043 */
3044 if (!group_leader)
3045 group_leader = counter;
3046
Thomas Gleixner0793a612008-12-04 20:12:29 +01003047 mutex_init(&counter->mutex);
Ingo Molnar04289bb2008-12-11 08:38:42 +01003048 INIT_LIST_HEAD(&counter->list_entry);
Peter Zijlstra592903c2009-03-13 12:21:36 +01003049 INIT_LIST_HEAD(&counter->event_entry);
Ingo Molnar04289bb2008-12-11 08:38:42 +01003050 INIT_LIST_HEAD(&counter->sibling_list);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003051 init_waitqueue_head(&counter->waitq);
3052
Peter Zijlstra7b732a72009-03-23 18:22:10 +01003053 mutex_init(&counter->mmap_mutex);
3054
Paul Mackerrasd859e292009-01-17 18:10:22 +11003055 INIT_LIST_HEAD(&counter->child_list);
3056
Ingo Molnar9f66a382008-12-10 12:33:23 +01003057 counter->cpu = cpu;
3058 counter->hw_event = *hw_event;
Ingo Molnar04289bb2008-12-11 08:38:42 +01003059 counter->group_leader = group_leader;
Robert Richter4aeb0b42009-04-29 12:47:03 +02003060 counter->pmu = NULL;
Paul Mackerras23a185c2009-02-09 22:42:47 +11003061 counter->ctx = ctx;
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10003062 get_ctx(ctx);
Ingo Molnar621a01e2008-12-11 12:46:46 +01003063
Ingo Molnar235c7fc2008-12-21 14:43:25 +01003064 counter->state = PERF_COUNTER_STATE_INACTIVE;
Ingo Molnara86ed502008-12-17 00:43:10 +01003065 if (hw_event->disabled)
3066 counter->state = PERF_COUNTER_STATE_OFF;
3067
Robert Richter4aeb0b42009-04-29 12:47:03 +02003068 pmu = NULL;
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003069
Peter Zijlstra60db5e02009-05-15 15:19:28 +02003070 hwc = &counter->hw;
3071 if (hw_event->freq && hw_event->irq_freq)
Peter Zijlstra2e569d32009-05-15 15:37:47 +02003072 hwc->irq_period = div64_u64(TICK_NSEC, hw_event->irq_freq);
Peter Zijlstra60db5e02009-05-15 15:19:28 +02003073 else
3074 hwc->irq_period = hw_event->irq_period;
3075
Peter Zijlstra2023b352009-05-05 17:50:26 +02003076 /*
3077 * we currently do not support PERF_RECORD_GROUP on inherited counters
3078 */
3079 if (hw_event->inherit && (hw_event->record_type & PERF_RECORD_GROUP))
3080 goto done;
3081
Peter Zijlstraf4a2deb2009-03-23 18:22:06 +01003082 if (perf_event_raw(hw_event)) {
Robert Richter4aeb0b42009-04-29 12:47:03 +02003083 pmu = hw_perf_counter_init(counter);
Peter Zijlstraf4a2deb2009-03-23 18:22:06 +01003084 goto done;
3085 }
3086
3087 switch (perf_event_type(hw_event)) {
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003088 case PERF_TYPE_HARDWARE:
Robert Richter4aeb0b42009-04-29 12:47:03 +02003089 pmu = hw_perf_counter_init(counter);
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003090 break;
3091
3092 case PERF_TYPE_SOFTWARE:
Robert Richter4aeb0b42009-04-29 12:47:03 +02003093 pmu = sw_perf_counter_init(counter);
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003094 break;
3095
3096 case PERF_TYPE_TRACEPOINT:
Robert Richter4aeb0b42009-04-29 12:47:03 +02003097 pmu = tp_perf_counter_init(counter);
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003098 break;
3099 }
Peter Zijlstraf4a2deb2009-03-23 18:22:06 +01003100done:
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003101 err = 0;
Robert Richter4aeb0b42009-04-29 12:47:03 +02003102 if (!pmu)
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003103 err = -EINVAL;
Robert Richter4aeb0b42009-04-29 12:47:03 +02003104 else if (IS_ERR(pmu))
3105 err = PTR_ERR(pmu);
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003106
3107 if (err) {
3108 kfree(counter);
3109 return ERR_PTR(err);
3110 }
3111
Robert Richter4aeb0b42009-04-29 12:47:03 +02003112 counter->pmu = pmu;
Thomas Gleixner0793a612008-12-04 20:12:29 +01003113
Peter Zijlstra7fc23a52009-05-08 18:52:21 +02003114 atomic_inc(&nr_counters);
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02003115 if (counter->hw_event.mmap)
3116 atomic_inc(&nr_mmap_tracking);
3117 if (counter->hw_event.munmap)
3118 atomic_inc(&nr_munmap_tracking);
3119 if (counter->hw_event.comm)
3120 atomic_inc(&nr_comm_tracking);
3121
Thomas Gleixner0793a612008-12-04 20:12:29 +01003122 return counter;
3123}
3124
3125/**
Paul Mackerras2743a5b2009-03-04 20:36:51 +11003126 * sys_perf_counter_open - open a performance counter, associate it to a task/cpu
Ingo Molnar9f66a382008-12-10 12:33:23 +01003127 *
3128 * @hw_event_uptr: event type attributes for monitoring/sampling
Thomas Gleixner0793a612008-12-04 20:12:29 +01003129 * @pid: target pid
Ingo Molnar9f66a382008-12-10 12:33:23 +01003130 * @cpu: target cpu
3131 * @group_fd: group leader counter fd
Thomas Gleixner0793a612008-12-04 20:12:29 +01003132 */
Paul Mackerras2743a5b2009-03-04 20:36:51 +11003133SYSCALL_DEFINE5(perf_counter_open,
Paul Mackerrasf3dfd262009-02-26 22:43:46 +11003134 const struct perf_counter_hw_event __user *, hw_event_uptr,
Paul Mackerras2743a5b2009-03-04 20:36:51 +11003135 pid_t, pid, int, cpu, int, group_fd, unsigned long, flags)
Thomas Gleixner0793a612008-12-04 20:12:29 +01003136{
Ingo Molnar04289bb2008-12-11 08:38:42 +01003137 struct perf_counter *counter, *group_leader;
Ingo Molnar9f66a382008-12-10 12:33:23 +01003138 struct perf_counter_hw_event hw_event;
Ingo Molnar04289bb2008-12-11 08:38:42 +01003139 struct perf_counter_context *ctx;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003140 struct file *counter_file = NULL;
Ingo Molnar04289bb2008-12-11 08:38:42 +01003141 struct file *group_file = NULL;
3142 int fput_needed = 0;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003143 int fput_needed2 = 0;
Thomas Gleixner0793a612008-12-04 20:12:29 +01003144 int ret;
3145
Paul Mackerras2743a5b2009-03-04 20:36:51 +11003146 /* for future expandability... */
3147 if (flags)
3148 return -EINVAL;
3149
Ingo Molnar9f66a382008-12-10 12:33:23 +01003150 if (copy_from_user(&hw_event, hw_event_uptr, sizeof(hw_event)) != 0)
Thomas Gleixnereab656a2008-12-08 19:26:59 +01003151 return -EFAULT;
3152
Ingo Molnar04289bb2008-12-11 08:38:42 +01003153 /*
Ingo Molnarccff2862008-12-11 11:26:29 +01003154 * Get the target context (task or percpu):
3155 */
3156 ctx = find_get_context(pid, cpu);
3157 if (IS_ERR(ctx))
3158 return PTR_ERR(ctx);
3159
3160 /*
3161 * Look up the group leader (we will attach this counter to it):
Ingo Molnar04289bb2008-12-11 08:38:42 +01003162 */
3163 group_leader = NULL;
3164 if (group_fd != -1) {
3165 ret = -EINVAL;
3166 group_file = fget_light(group_fd, &fput_needed);
3167 if (!group_file)
Ingo Molnarccff2862008-12-11 11:26:29 +01003168 goto err_put_context;
Ingo Molnar04289bb2008-12-11 08:38:42 +01003169 if (group_file->f_op != &perf_fops)
Ingo Molnarccff2862008-12-11 11:26:29 +01003170 goto err_put_context;
Ingo Molnar04289bb2008-12-11 08:38:42 +01003171
3172 group_leader = group_file->private_data;
3173 /*
Ingo Molnarccff2862008-12-11 11:26:29 +01003174 * Do not allow a recursive hierarchy (this new sibling
3175 * becoming part of another group-sibling):
Ingo Molnar04289bb2008-12-11 08:38:42 +01003176 */
Ingo Molnarccff2862008-12-11 11:26:29 +01003177 if (group_leader->group_leader != group_leader)
3178 goto err_put_context;
3179 /*
3180 * Do not allow to attach to a group in a different
3181 * task or CPU context:
3182 */
3183 if (group_leader->ctx != ctx)
3184 goto err_put_context;
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11003185 /*
3186 * Only a group leader can be exclusive or pinned
3187 */
3188 if (hw_event.exclusive || hw_event.pinned)
3189 goto err_put_context;
Ingo Molnar04289bb2008-12-11 08:38:42 +01003190 }
3191
Paul Mackerras23a185c2009-02-09 22:42:47 +11003192 counter = perf_counter_alloc(&hw_event, cpu, ctx, group_leader,
3193 GFP_KERNEL);
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003194 ret = PTR_ERR(counter);
3195 if (IS_ERR(counter))
Thomas Gleixner0793a612008-12-04 20:12:29 +01003196 goto err_put_context;
3197
Thomas Gleixner0793a612008-12-04 20:12:29 +01003198 ret = anon_inode_getfd("[perf_counter]", &perf_fops, counter, 0);
3199 if (ret < 0)
Ingo Molnar9b51f662008-12-12 13:49:45 +01003200 goto err_free_put_context;
3201
3202 counter_file = fget_light(ret, &fput_needed2);
3203 if (!counter_file)
3204 goto err_free_put_context;
3205
3206 counter->filp = counter_file;
Paul Mackerrasd859e292009-01-17 18:10:22 +11003207 mutex_lock(&ctx->mutex);
Ingo Molnar9b51f662008-12-12 13:49:45 +01003208 perf_install_in_context(ctx, counter, cpu);
Paul Mackerrasd859e292009-01-17 18:10:22 +11003209 mutex_unlock(&ctx->mutex);
Ingo Molnar9b51f662008-12-12 13:49:45 +01003210
3211 fput_light(counter_file, fput_needed2);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003212
Ingo Molnar04289bb2008-12-11 08:38:42 +01003213out_fput:
3214 fput_light(group_file, fput_needed);
3215
Thomas Gleixner0793a612008-12-04 20:12:29 +01003216 return ret;
3217
Ingo Molnar9b51f662008-12-12 13:49:45 +01003218err_free_put_context:
Thomas Gleixner0793a612008-12-04 20:12:29 +01003219 kfree(counter);
3220
3221err_put_context:
3222 put_context(ctx);
3223
Ingo Molnar04289bb2008-12-11 08:38:42 +01003224 goto out_fput;
Thomas Gleixner0793a612008-12-04 20:12:29 +01003225}
3226
Ingo Molnar9b51f662008-12-12 13:49:45 +01003227/*
Ingo Molnar9b51f662008-12-12 13:49:45 +01003228 * inherit a counter from parent task to child task:
3229 */
Paul Mackerrasd859e292009-01-17 18:10:22 +11003230static struct perf_counter *
Ingo Molnar9b51f662008-12-12 13:49:45 +01003231inherit_counter(struct perf_counter *parent_counter,
3232 struct task_struct *parent,
3233 struct perf_counter_context *parent_ctx,
3234 struct task_struct *child,
Paul Mackerrasd859e292009-01-17 18:10:22 +11003235 struct perf_counter *group_leader,
Ingo Molnar9b51f662008-12-12 13:49:45 +01003236 struct perf_counter_context *child_ctx)
3237{
3238 struct perf_counter *child_counter;
3239
Paul Mackerrasd859e292009-01-17 18:10:22 +11003240 /*
3241 * Instead of creating recursive hierarchies of counters,
3242 * we link inherited counters back to the original parent,
3243 * which has a filp for sure, which we use as the reference
3244 * count:
3245 */
3246 if (parent_counter->parent)
3247 parent_counter = parent_counter->parent;
3248
Ingo Molnar9b51f662008-12-12 13:49:45 +01003249 child_counter = perf_counter_alloc(&parent_counter->hw_event,
Paul Mackerras23a185c2009-02-09 22:42:47 +11003250 parent_counter->cpu, child_ctx,
3251 group_leader, GFP_KERNEL);
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003252 if (IS_ERR(child_counter))
3253 return child_counter;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003254
3255 /*
3256 * Link it up in the child's context:
3257 */
Paul Mackerras53cfbf52009-03-25 22:46:58 +11003258 add_counter_to_ctx(child_counter, child_ctx);
Ingo Molnar9b51f662008-12-12 13:49:45 +01003259
3260 child_counter->parent = parent_counter;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003261 /*
3262 * inherit into child's child as well:
3263 */
3264 child_counter->hw_event.inherit = 1;
3265
3266 /*
3267 * Get a reference to the parent filp - we will fput it
3268 * when the child counter exits. This is safe to do because
3269 * we are in the parent and we know that the filp still
3270 * exists and has a nonzero count:
3271 */
3272 atomic_long_inc(&parent_counter->filp->f_count);
3273
Paul Mackerrasd859e292009-01-17 18:10:22 +11003274 /*
3275 * Link this into the parent counter's child list
3276 */
3277 mutex_lock(&parent_counter->mutex);
3278 list_add_tail(&child_counter->child_list, &parent_counter->child_list);
3279
3280 /*
3281 * Make the child state follow the state of the parent counter,
3282 * not its hw_event.disabled bit. We hold the parent's mutex,
3283 * so we won't race with perf_counter_{en,dis}able_family.
3284 */
3285 if (parent_counter->state >= PERF_COUNTER_STATE_INACTIVE)
3286 child_counter->state = PERF_COUNTER_STATE_INACTIVE;
3287 else
3288 child_counter->state = PERF_COUNTER_STATE_OFF;
3289
3290 mutex_unlock(&parent_counter->mutex);
3291
3292 return child_counter;
3293}
3294
3295static int inherit_group(struct perf_counter *parent_counter,
3296 struct task_struct *parent,
3297 struct perf_counter_context *parent_ctx,
3298 struct task_struct *child,
3299 struct perf_counter_context *child_ctx)
3300{
3301 struct perf_counter *leader;
3302 struct perf_counter *sub;
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003303 struct perf_counter *child_ctr;
Paul Mackerrasd859e292009-01-17 18:10:22 +11003304
3305 leader = inherit_counter(parent_counter, parent, parent_ctx,
3306 child, NULL, child_ctx);
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003307 if (IS_ERR(leader))
3308 return PTR_ERR(leader);
Paul Mackerrasd859e292009-01-17 18:10:22 +11003309 list_for_each_entry(sub, &parent_counter->sibling_list, list_entry) {
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003310 child_ctr = inherit_counter(sub, parent, parent_ctx,
3311 child, leader, child_ctx);
3312 if (IS_ERR(child_ctr))
3313 return PTR_ERR(child_ctr);
Paul Mackerrasd859e292009-01-17 18:10:22 +11003314 }
Ingo Molnar9b51f662008-12-12 13:49:45 +01003315 return 0;
3316}
3317
Paul Mackerrasd859e292009-01-17 18:10:22 +11003318static void sync_child_counter(struct perf_counter *child_counter,
3319 struct perf_counter *parent_counter)
3320{
Peter Zijlstra8bc20952009-05-15 20:45:59 +02003321 u64 child_val;
Paul Mackerrasd859e292009-01-17 18:10:22 +11003322
Paul Mackerrasd859e292009-01-17 18:10:22 +11003323 child_val = atomic64_read(&child_counter->count);
3324
3325 /*
3326 * Add back the child's count to the parent's count:
3327 */
3328 atomic64_add(child_val, &parent_counter->count);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11003329 atomic64_add(child_counter->total_time_enabled,
3330 &parent_counter->child_total_time_enabled);
3331 atomic64_add(child_counter->total_time_running,
3332 &parent_counter->child_total_time_running);
Paul Mackerrasd859e292009-01-17 18:10:22 +11003333
3334 /*
3335 * Remove this counter from the parent's list
3336 */
3337 mutex_lock(&parent_counter->mutex);
3338 list_del_init(&child_counter->child_list);
3339 mutex_unlock(&parent_counter->mutex);
3340
3341 /*
3342 * Release the parent counter, if this was the last
3343 * reference to it.
3344 */
3345 fput(parent_counter->filp);
3346}
3347
Ingo Molnar9b51f662008-12-12 13:49:45 +01003348static void
3349__perf_counter_exit_task(struct task_struct *child,
3350 struct perf_counter *child_counter,
3351 struct perf_counter_context *child_ctx)
3352{
3353 struct perf_counter *parent_counter;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003354
3355 /*
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10003356 * Protect against concurrent operations on child_counter
3357 * due its fd getting closed, etc.
Ingo Molnar9b51f662008-12-12 13:49:45 +01003358 */
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10003359 mutex_lock(&child_counter->mutex);
Ingo Molnar235c7fc2008-12-21 14:43:25 +01003360
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10003361 update_counter_times(child_counter);
3362 list_del_counter(child_counter, child_ctx);
Ingo Molnar0cc0c022008-12-14 23:20:36 +01003363
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10003364 mutex_unlock(&child_counter->mutex);
Ingo Molnar0cc0c022008-12-14 23:20:36 +01003365
Ingo Molnar9b51f662008-12-12 13:49:45 +01003366 parent_counter = child_counter->parent;
3367 /*
3368 * It can happen that parent exits first, and has counters
3369 * that are still around due to the child reference. These
3370 * counters need to be zapped - but otherwise linger.
3371 */
Paul Mackerrasd859e292009-01-17 18:10:22 +11003372 if (parent_counter) {
3373 sync_child_counter(child_counter, parent_counter);
Peter Zijlstraf1600952009-03-19 20:26:16 +01003374 free_counter(child_counter);
Paul Mackerras4bcf3492009-02-11 13:53:19 +01003375 }
Ingo Molnar9b51f662008-12-12 13:49:45 +01003376}
3377
3378/*
Paul Mackerrasd859e292009-01-17 18:10:22 +11003379 * When a child task exits, feed back counter values to parent counters.
Ingo Molnar9b51f662008-12-12 13:49:45 +01003380 *
Paul Mackerrasd859e292009-01-17 18:10:22 +11003381 * Note: we may be running in child context, but the PID is not hashed
Ingo Molnar9b51f662008-12-12 13:49:45 +01003382 * anymore so new counters will not be added.
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10003383 * (XXX not sure that is true when we get called from flush_old_exec.
3384 * -- paulus)
Ingo Molnar9b51f662008-12-12 13:49:45 +01003385 */
3386void perf_counter_exit_task(struct task_struct *child)
3387{
3388 struct perf_counter *child_counter, *tmp;
3389 struct perf_counter_context *child_ctx;
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10003390 unsigned long flags;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003391
Ingo Molnar33b2fb32009-05-17 11:08:41 +02003392 WARN_ON_ONCE(child != current);
3393
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10003394 child_ctx = child->perf_counter_ctxp;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003395
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10003396 if (likely(!child_ctx))
Ingo Molnar9b51f662008-12-12 13:49:45 +01003397 return;
3398
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10003399 local_irq_save(flags);
3400 __perf_counter_task_sched_out(child_ctx);
3401 child->perf_counter_ctxp = NULL;
3402 local_irq_restore(flags);
3403
3404 mutex_lock(&child_ctx->mutex);
3405
Peter Zijlstra8bc20952009-05-15 20:45:59 +02003406again:
Ingo Molnar9b51f662008-12-12 13:49:45 +01003407 list_for_each_entry_safe(child_counter, tmp, &child_ctx->counter_list,
3408 list_entry)
3409 __perf_counter_exit_task(child, child_counter, child_ctx);
Peter Zijlstra8bc20952009-05-15 20:45:59 +02003410
3411 /*
3412 * If the last counter was a group counter, it will have appended all
3413 * its siblings to the list, but we obtained 'tmp' before that which
3414 * will still point to the list head terminating the iteration.
3415 */
3416 if (!list_empty(&child_ctx->counter_list))
3417 goto again;
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10003418
3419 mutex_unlock(&child_ctx->mutex);
3420
3421 put_ctx(child_ctx);
Ingo Molnar9b51f662008-12-12 13:49:45 +01003422}
3423
3424/*
3425 * Initialize the perf_counter context in task_struct
3426 */
3427void perf_counter_init_task(struct task_struct *child)
3428{
3429 struct perf_counter_context *child_ctx, *parent_ctx;
Paul Mackerrasd859e292009-01-17 18:10:22 +11003430 struct perf_counter *counter;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003431 struct task_struct *parent = current;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003432
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10003433 child->perf_counter_ctxp = NULL;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003434
3435 /*
3436 * This is executed from the parent task context, so inherit
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10003437 * counters that have been marked for cloning.
3438 * First allocate and initialize a context for the child.
Ingo Molnar9b51f662008-12-12 13:49:45 +01003439 */
3440
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10003441 child_ctx = kmalloc(sizeof(struct perf_counter_context), GFP_KERNEL);
3442 if (!child_ctx)
Ingo Molnar9b51f662008-12-12 13:49:45 +01003443 return;
3444
Paul Mackerrasa63eaf32009-05-22 14:17:31 +10003445 parent_ctx = parent->perf_counter_ctxp;
3446 if (likely(!parent_ctx || !parent_ctx->nr_counters))
3447 return;
3448
3449 __perf_counter_init_context(child_ctx, child);
3450 child->perf_counter_ctxp = child_ctx;
3451
Ingo Molnar9b51f662008-12-12 13:49:45 +01003452 /*
3453 * Lock the parent list. No need to lock the child - not PID
3454 * hashed yet and not running, so nobody can access it.
3455 */
Paul Mackerrasd859e292009-01-17 18:10:22 +11003456 mutex_lock(&parent_ctx->mutex);
Ingo Molnar9b51f662008-12-12 13:49:45 +01003457
3458 /*
3459 * We dont have to disable NMIs - we are only looking at
3460 * the list, not manipulating it:
3461 */
Peter Zijlstrad7b629a2009-05-20 12:21:19 +02003462 list_for_each_entry_rcu(counter, &parent_ctx->event_list, event_entry) {
3463 if (counter != counter->group_leader)
3464 continue;
3465
Paul Mackerrasd859e292009-01-17 18:10:22 +11003466 if (!counter->hw_event.inherit)
Ingo Molnar9b51f662008-12-12 13:49:45 +01003467 continue;
3468
Paul Mackerrasd859e292009-01-17 18:10:22 +11003469 if (inherit_group(counter, parent,
Ingo Molnar9b51f662008-12-12 13:49:45 +01003470 parent_ctx, child, child_ctx))
3471 break;
3472 }
3473
Paul Mackerrasd859e292009-01-17 18:10:22 +11003474 mutex_unlock(&parent_ctx->mutex);
Ingo Molnar9b51f662008-12-12 13:49:45 +01003475}
3476
Ingo Molnar04289bb2008-12-11 08:38:42 +01003477static void __cpuinit perf_counter_init_cpu(int cpu)
Thomas Gleixner0793a612008-12-04 20:12:29 +01003478{
Ingo Molnar04289bb2008-12-11 08:38:42 +01003479 struct perf_cpu_context *cpuctx;
Thomas Gleixner0793a612008-12-04 20:12:29 +01003480
Ingo Molnar04289bb2008-12-11 08:38:42 +01003481 cpuctx = &per_cpu(perf_cpu_context, cpu);
3482 __perf_counter_init_context(&cpuctx->ctx, NULL);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003483
Ingo Molnar1dce8d92009-05-04 19:23:18 +02003484 spin_lock(&perf_resource_lock);
Ingo Molnar04289bb2008-12-11 08:38:42 +01003485 cpuctx->max_pertask = perf_max_counters - perf_reserved_percpu;
Ingo Molnar1dce8d92009-05-04 19:23:18 +02003486 spin_unlock(&perf_resource_lock);
Ingo Molnar04289bb2008-12-11 08:38:42 +01003487
Paul Mackerras01d02872009-01-14 13:44:19 +11003488 hw_perf_counter_setup(cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003489}
3490
3491#ifdef CONFIG_HOTPLUG_CPU
Ingo Molnar04289bb2008-12-11 08:38:42 +01003492static void __perf_counter_exit_cpu(void *info)
Thomas Gleixner0793a612008-12-04 20:12:29 +01003493{
3494 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
3495 struct perf_counter_context *ctx = &cpuctx->ctx;
3496 struct perf_counter *counter, *tmp;
3497
Ingo Molnar04289bb2008-12-11 08:38:42 +01003498 list_for_each_entry_safe(counter, tmp, &ctx->counter_list, list_entry)
3499 __perf_counter_remove_from_context(counter);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003500}
Ingo Molnar04289bb2008-12-11 08:38:42 +01003501static void perf_counter_exit_cpu(int cpu)
Thomas Gleixner0793a612008-12-04 20:12:29 +01003502{
Paul Mackerrasd859e292009-01-17 18:10:22 +11003503 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
3504 struct perf_counter_context *ctx = &cpuctx->ctx;
3505
3506 mutex_lock(&ctx->mutex);
Ingo Molnar04289bb2008-12-11 08:38:42 +01003507 smp_call_function_single(cpu, __perf_counter_exit_cpu, NULL, 1);
Paul Mackerrasd859e292009-01-17 18:10:22 +11003508 mutex_unlock(&ctx->mutex);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003509}
3510#else
Ingo Molnar04289bb2008-12-11 08:38:42 +01003511static inline void perf_counter_exit_cpu(int cpu) { }
Thomas Gleixner0793a612008-12-04 20:12:29 +01003512#endif
3513
3514static int __cpuinit
3515perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
3516{
3517 unsigned int cpu = (long)hcpu;
3518
3519 switch (action) {
3520
3521 case CPU_UP_PREPARE:
3522 case CPU_UP_PREPARE_FROZEN:
Ingo Molnar04289bb2008-12-11 08:38:42 +01003523 perf_counter_init_cpu(cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003524 break;
3525
3526 case CPU_DOWN_PREPARE:
3527 case CPU_DOWN_PREPARE_FROZEN:
Ingo Molnar04289bb2008-12-11 08:38:42 +01003528 perf_counter_exit_cpu(cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003529 break;
3530
3531 default:
3532 break;
3533 }
3534
3535 return NOTIFY_OK;
3536}
3537
3538static struct notifier_block __cpuinitdata perf_cpu_nb = {
3539 .notifier_call = perf_cpu_notify,
3540};
3541
Ingo Molnar0d905bc2009-05-04 19:13:30 +02003542void __init perf_counter_init(void)
Thomas Gleixner0793a612008-12-04 20:12:29 +01003543{
3544 perf_cpu_notify(&perf_cpu_nb, (unsigned long)CPU_UP_PREPARE,
3545 (void *)(long)smp_processor_id());
3546 register_cpu_notifier(&perf_cpu_nb);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003547}
Thomas Gleixner0793a612008-12-04 20:12:29 +01003548
3549static ssize_t perf_show_reserve_percpu(struct sysdev_class *class, char *buf)
3550{
3551 return sprintf(buf, "%d\n", perf_reserved_percpu);
3552}
3553
3554static ssize_t
3555perf_set_reserve_percpu(struct sysdev_class *class,
3556 const char *buf,
3557 size_t count)
3558{
3559 struct perf_cpu_context *cpuctx;
3560 unsigned long val;
3561 int err, cpu, mpt;
3562
3563 err = strict_strtoul(buf, 10, &val);
3564 if (err)
3565 return err;
3566 if (val > perf_max_counters)
3567 return -EINVAL;
3568
Ingo Molnar1dce8d92009-05-04 19:23:18 +02003569 spin_lock(&perf_resource_lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003570 perf_reserved_percpu = val;
3571 for_each_online_cpu(cpu) {
3572 cpuctx = &per_cpu(perf_cpu_context, cpu);
3573 spin_lock_irq(&cpuctx->ctx.lock);
3574 mpt = min(perf_max_counters - cpuctx->ctx.nr_counters,
3575 perf_max_counters - perf_reserved_percpu);
3576 cpuctx->max_pertask = mpt;
3577 spin_unlock_irq(&cpuctx->ctx.lock);
3578 }
Ingo Molnar1dce8d92009-05-04 19:23:18 +02003579 spin_unlock(&perf_resource_lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003580
3581 return count;
3582}
3583
3584static ssize_t perf_show_overcommit(struct sysdev_class *class, char *buf)
3585{
3586 return sprintf(buf, "%d\n", perf_overcommit);
3587}
3588
3589static ssize_t
3590perf_set_overcommit(struct sysdev_class *class, const char *buf, size_t count)
3591{
3592 unsigned long val;
3593 int err;
3594
3595 err = strict_strtoul(buf, 10, &val);
3596 if (err)
3597 return err;
3598 if (val > 1)
3599 return -EINVAL;
3600
Ingo Molnar1dce8d92009-05-04 19:23:18 +02003601 spin_lock(&perf_resource_lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003602 perf_overcommit = val;
Ingo Molnar1dce8d92009-05-04 19:23:18 +02003603 spin_unlock(&perf_resource_lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003604
3605 return count;
3606}
3607
3608static SYSDEV_CLASS_ATTR(
3609 reserve_percpu,
3610 0644,
3611 perf_show_reserve_percpu,
3612 perf_set_reserve_percpu
3613 );
3614
3615static SYSDEV_CLASS_ATTR(
3616 overcommit,
3617 0644,
3618 perf_show_overcommit,
3619 perf_set_overcommit
3620 );
3621
3622static struct attribute *perfclass_attrs[] = {
3623 &attr_reserve_percpu.attr,
3624 &attr_overcommit.attr,
3625 NULL
3626};
3627
3628static struct attribute_group perfclass_attr_group = {
3629 .attrs = perfclass_attrs,
3630 .name = "perf_counters",
3631};
3632
3633static int __init perf_counter_sysfs_init(void)
3634{
3635 return sysfs_create_group(&cpu_sysdev_class.kset.kobj,
3636 &perfclass_attr_group);
3637}
3638device_initcall(perf_counter_sysfs_init);