blob: 473ed2cafbfcaa904f48a1148e2dcb0716a683ae [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
Ingo Molnar04289bb2008-12-11 08:38:42 +0100100static void
101list_add_counter(struct perf_counter *counter, struct perf_counter_context *ctx)
102{
103 struct perf_counter *group_leader = counter->group_leader;
104
105 /*
106 * Depending on whether it is a standalone or sibling counter,
107 * add it straight to the context's counter list, or to the group
108 * leader's sibling list:
109 */
Peter Zijlstra3df5eda2009-05-08 18:52:22 +0200110 if (group_leader == counter)
Ingo Molnar04289bb2008-12-11 08:38:42 +0100111 list_add_tail(&counter->list_entry, &ctx->counter_list);
Peter Zijlstra5c148192009-03-25 12:30:23 +0100112 else {
Ingo Molnar04289bb2008-12-11 08:38:42 +0100113 list_add_tail(&counter->list_entry, &group_leader->sibling_list);
Peter Zijlstra5c148192009-03-25 12:30:23 +0100114 group_leader->nr_siblings++;
115 }
Peter Zijlstra592903c2009-03-13 12:21:36 +0100116
117 list_add_rcu(&counter->event_entry, &ctx->event_list);
Peter Zijlstra8bc20952009-05-15 20:45:59 +0200118 ctx->nr_counters++;
Ingo Molnar04289bb2008-12-11 08:38:42 +0100119}
120
121static void
122list_del_counter(struct perf_counter *counter, struct perf_counter_context *ctx)
123{
124 struct perf_counter *sibling, *tmp;
125
Peter Zijlstra8bc20952009-05-15 20:45:59 +0200126 ctx->nr_counters--;
127
Ingo Molnar04289bb2008-12-11 08:38:42 +0100128 list_del_init(&counter->list_entry);
Peter Zijlstra592903c2009-03-13 12:21:36 +0100129 list_del_rcu(&counter->event_entry);
Ingo Molnar04289bb2008-12-11 08:38:42 +0100130
Peter Zijlstra5c148192009-03-25 12:30:23 +0100131 if (counter->group_leader != counter)
132 counter->group_leader->nr_siblings--;
133
Ingo Molnar04289bb2008-12-11 08:38:42 +0100134 /*
135 * If this was a group counter with sibling counters then
136 * upgrade the siblings to singleton counters by adding them
137 * to the context list directly:
138 */
139 list_for_each_entry_safe(sibling, tmp,
140 &counter->sibling_list, list_entry) {
141
Peter Zijlstra75564232009-03-13 12:21:29 +0100142 list_move_tail(&sibling->list_entry, &ctx->counter_list);
Ingo Molnar04289bb2008-12-11 08:38:42 +0100143 sibling->group_leader = sibling;
144 }
145}
146
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100147static void
148counter_sched_out(struct perf_counter *counter,
149 struct perf_cpu_context *cpuctx,
150 struct perf_counter_context *ctx)
151{
152 if (counter->state != PERF_COUNTER_STATE_ACTIVE)
153 return;
154
155 counter->state = PERF_COUNTER_STATE_INACTIVE;
Peter Zijlstra4af49982009-04-06 11:45:10 +0200156 counter->tstamp_stopped = ctx->time;
Robert Richter4aeb0b42009-04-29 12:47:03 +0200157 counter->pmu->disable(counter);
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100158 counter->oncpu = -1;
159
160 if (!is_software_counter(counter))
161 cpuctx->active_oncpu--;
162 ctx->nr_active--;
163 if (counter->hw_event.exclusive || !cpuctx->active_oncpu)
164 cpuctx->exclusive = 0;
165}
166
Paul Mackerrasd859e292009-01-17 18:10:22 +1100167static void
168group_sched_out(struct perf_counter *group_counter,
169 struct perf_cpu_context *cpuctx,
170 struct perf_counter_context *ctx)
171{
172 struct perf_counter *counter;
173
174 if (group_counter->state != PERF_COUNTER_STATE_ACTIVE)
175 return;
176
177 counter_sched_out(group_counter, cpuctx, ctx);
178
179 /*
180 * Schedule out siblings (if any):
181 */
182 list_for_each_entry(counter, &group_counter->sibling_list, list_entry)
183 counter_sched_out(counter, cpuctx, ctx);
184
185 if (group_counter->hw_event.exclusive)
186 cpuctx->exclusive = 0;
187}
188
Thomas Gleixner0793a612008-12-04 20:12:29 +0100189/*
190 * Cross CPU call to remove a performance counter
191 *
192 * We disable the counter on the hardware level first. After that we
193 * remove it from the context list.
194 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100195static void __perf_counter_remove_from_context(void *info)
Thomas Gleixner0793a612008-12-04 20:12:29 +0100196{
197 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
198 struct perf_counter *counter = info;
199 struct perf_counter_context *ctx = counter->ctx;
Ingo Molnar9b51f662008-12-12 13:49:45 +0100200 unsigned long flags;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100201
202 /*
203 * If this is a task context, we need to check whether it is
204 * the current task context of this cpu. If not it has been
205 * scheduled out before the smp call arrived.
206 */
207 if (ctx->task && cpuctx->task_ctx != ctx)
208 return;
209
Peter Zijlstra849691a2009-04-06 11:45:12 +0200210 spin_lock_irqsave(&ctx->lock, flags);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100211
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100212 counter_sched_out(counter, cpuctx, ctx);
213
214 counter->task = NULL;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100215
216 /*
217 * Protect the list operation against NMI by disabling the
218 * counters on a global level. NOP for non NMI based counters.
219 */
Peter Zijlstra9e35ad32009-05-13 16:21:38 +0200220 perf_disable();
Ingo Molnar04289bb2008-12-11 08:38:42 +0100221 list_del_counter(counter, ctx);
Peter Zijlstra9e35ad32009-05-13 16:21:38 +0200222 perf_enable();
Thomas Gleixner0793a612008-12-04 20:12:29 +0100223
224 if (!ctx->task) {
225 /*
226 * Allow more per task counters with respect to the
227 * reservation:
228 */
229 cpuctx->max_pertask =
230 min(perf_max_counters - ctx->nr_counters,
231 perf_max_counters - perf_reserved_percpu);
232 }
233
Peter Zijlstra849691a2009-04-06 11:45:12 +0200234 spin_unlock_irqrestore(&ctx->lock, flags);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100235}
236
237
238/*
239 * Remove the counter from a task's (or a CPU's) list of counters.
240 *
Paul Mackerrasd859e292009-01-17 18:10:22 +1100241 * Must be called with counter->mutex and ctx->mutex held.
Thomas Gleixner0793a612008-12-04 20:12:29 +0100242 *
243 * CPU counters are removed with a smp call. For task counters we only
244 * call when the task is on a CPU.
245 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100246static void perf_counter_remove_from_context(struct perf_counter *counter)
Thomas Gleixner0793a612008-12-04 20:12:29 +0100247{
248 struct perf_counter_context *ctx = counter->ctx;
249 struct task_struct *task = ctx->task;
250
251 if (!task) {
252 /*
253 * Per cpu counters are removed via an smp call and
254 * the removal is always sucessful.
255 */
256 smp_call_function_single(counter->cpu,
Ingo Molnar04289bb2008-12-11 08:38:42 +0100257 __perf_counter_remove_from_context,
Thomas Gleixner0793a612008-12-04 20:12:29 +0100258 counter, 1);
259 return;
260 }
261
262retry:
Ingo Molnar04289bb2008-12-11 08:38:42 +0100263 task_oncpu_function_call(task, __perf_counter_remove_from_context,
Thomas Gleixner0793a612008-12-04 20:12:29 +0100264 counter);
265
266 spin_lock_irq(&ctx->lock);
267 /*
268 * If the context is active we need to retry the smp call.
269 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100270 if (ctx->nr_active && !list_empty(&counter->list_entry)) {
Thomas Gleixner0793a612008-12-04 20:12:29 +0100271 spin_unlock_irq(&ctx->lock);
272 goto retry;
273 }
274
275 /*
276 * The lock prevents that this context is scheduled in so we
Ingo Molnar04289bb2008-12-11 08:38:42 +0100277 * can remove the counter safely, if the call above did not
Thomas Gleixner0793a612008-12-04 20:12:29 +0100278 * succeed.
279 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100280 if (!list_empty(&counter->list_entry)) {
Ingo Molnar04289bb2008-12-11 08:38:42 +0100281 list_del_counter(counter, ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100282 counter->task = NULL;
283 }
284 spin_unlock_irq(&ctx->lock);
285}
286
Peter Zijlstra4af49982009-04-06 11:45:10 +0200287static inline u64 perf_clock(void)
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100288{
Peter Zijlstra4af49982009-04-06 11:45:10 +0200289 return cpu_clock(smp_processor_id());
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100290}
291
292/*
293 * Update the record of the current time in a context.
294 */
Peter Zijlstra4af49982009-04-06 11:45:10 +0200295static void update_context_time(struct perf_counter_context *ctx)
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100296{
Peter Zijlstra4af49982009-04-06 11:45:10 +0200297 u64 now = perf_clock();
298
299 ctx->time += now - ctx->timestamp;
300 ctx->timestamp = now;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100301}
302
303/*
304 * Update the total_time_enabled and total_time_running fields for a counter.
305 */
306static void update_counter_times(struct perf_counter *counter)
307{
308 struct perf_counter_context *ctx = counter->ctx;
309 u64 run_end;
310
Peter Zijlstra4af49982009-04-06 11:45:10 +0200311 if (counter->state < PERF_COUNTER_STATE_INACTIVE)
312 return;
313
314 counter->total_time_enabled = ctx->time - counter->tstamp_enabled;
315
316 if (counter->state == PERF_COUNTER_STATE_INACTIVE)
317 run_end = counter->tstamp_stopped;
318 else
319 run_end = ctx->time;
320
321 counter->total_time_running = run_end - counter->tstamp_running;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100322}
323
324/*
325 * Update total_time_enabled and total_time_running for all counters in a group.
326 */
327static void update_group_times(struct perf_counter *leader)
328{
329 struct perf_counter *counter;
330
331 update_counter_times(leader);
332 list_for_each_entry(counter, &leader->sibling_list, list_entry)
333 update_counter_times(counter);
334}
335
336/*
Paul Mackerrasd859e292009-01-17 18:10:22 +1100337 * Cross CPU call to disable a performance counter
338 */
339static void __perf_counter_disable(void *info)
340{
341 struct perf_counter *counter = info;
342 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
343 struct perf_counter_context *ctx = counter->ctx;
344 unsigned long flags;
345
346 /*
347 * If this is a per-task counter, need to check whether this
348 * counter's task is the current task on this cpu.
349 */
350 if (ctx->task && cpuctx->task_ctx != ctx)
351 return;
352
Peter Zijlstra849691a2009-04-06 11:45:12 +0200353 spin_lock_irqsave(&ctx->lock, flags);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100354
355 /*
356 * If the counter is on, turn it off.
357 * If it is in error state, leave it in error state.
358 */
359 if (counter->state >= PERF_COUNTER_STATE_INACTIVE) {
Peter Zijlstra4af49982009-04-06 11:45:10 +0200360 update_context_time(ctx);
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100361 update_counter_times(counter);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100362 if (counter == counter->group_leader)
363 group_sched_out(counter, cpuctx, ctx);
364 else
365 counter_sched_out(counter, cpuctx, ctx);
366 counter->state = PERF_COUNTER_STATE_OFF;
367 }
368
Peter Zijlstra849691a2009-04-06 11:45:12 +0200369 spin_unlock_irqrestore(&ctx->lock, flags);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100370}
371
372/*
373 * Disable a counter.
374 */
375static void perf_counter_disable(struct perf_counter *counter)
376{
377 struct perf_counter_context *ctx = counter->ctx;
378 struct task_struct *task = ctx->task;
379
380 if (!task) {
381 /*
382 * Disable the counter on the cpu that it's on
383 */
384 smp_call_function_single(counter->cpu, __perf_counter_disable,
385 counter, 1);
386 return;
387 }
388
389 retry:
390 task_oncpu_function_call(task, __perf_counter_disable, counter);
391
392 spin_lock_irq(&ctx->lock);
393 /*
394 * If the counter is still active, we need to retry the cross-call.
395 */
396 if (counter->state == PERF_COUNTER_STATE_ACTIVE) {
397 spin_unlock_irq(&ctx->lock);
398 goto retry;
399 }
400
401 /*
402 * Since we have the lock this context can't be scheduled
403 * in, so we can change the state safely.
404 */
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100405 if (counter->state == PERF_COUNTER_STATE_INACTIVE) {
406 update_counter_times(counter);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100407 counter->state = PERF_COUNTER_STATE_OFF;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100408 }
Paul Mackerrasd859e292009-01-17 18:10:22 +1100409
410 spin_unlock_irq(&ctx->lock);
411}
412
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100413static int
414counter_sched_in(struct perf_counter *counter,
415 struct perf_cpu_context *cpuctx,
416 struct perf_counter_context *ctx,
417 int cpu)
418{
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100419 if (counter->state <= PERF_COUNTER_STATE_OFF)
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100420 return 0;
421
422 counter->state = PERF_COUNTER_STATE_ACTIVE;
423 counter->oncpu = cpu; /* TODO: put 'cpu' into cpuctx->cpu */
424 /*
425 * The new state must be visible before we turn it on in the hardware:
426 */
427 smp_wmb();
428
Robert Richter4aeb0b42009-04-29 12:47:03 +0200429 if (counter->pmu->enable(counter)) {
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100430 counter->state = PERF_COUNTER_STATE_INACTIVE;
431 counter->oncpu = -1;
432 return -EAGAIN;
433 }
434
Peter Zijlstra4af49982009-04-06 11:45:10 +0200435 counter->tstamp_running += ctx->time - counter->tstamp_stopped;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100436
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100437 if (!is_software_counter(counter))
438 cpuctx->active_oncpu++;
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100439 ctx->nr_active++;
440
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100441 if (counter->hw_event.exclusive)
442 cpuctx->exclusive = 1;
443
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100444 return 0;
445}
446
Paul Mackerras6751b712009-05-11 12:08:02 +1000447static int
448group_sched_in(struct perf_counter *group_counter,
449 struct perf_cpu_context *cpuctx,
450 struct perf_counter_context *ctx,
451 int cpu)
452{
453 struct perf_counter *counter, *partial_group;
454 int ret;
455
456 if (group_counter->state == PERF_COUNTER_STATE_OFF)
457 return 0;
458
459 ret = hw_perf_group_sched_in(group_counter, cpuctx, ctx, cpu);
460 if (ret)
461 return ret < 0 ? ret : 0;
462
463 group_counter->prev_state = group_counter->state;
464 if (counter_sched_in(group_counter, cpuctx, ctx, cpu))
465 return -EAGAIN;
466
467 /*
468 * Schedule in siblings as one group (if any):
469 */
470 list_for_each_entry(counter, &group_counter->sibling_list, list_entry) {
471 counter->prev_state = counter->state;
472 if (counter_sched_in(counter, cpuctx, ctx, cpu)) {
473 partial_group = counter;
474 goto group_error;
475 }
476 }
477
478 return 0;
479
480group_error:
481 /*
482 * Groups can be scheduled in as one unit only, so undo any
483 * partial group before returning:
484 */
485 list_for_each_entry(counter, &group_counter->sibling_list, list_entry) {
486 if (counter == partial_group)
487 break;
488 counter_sched_out(counter, cpuctx, ctx);
489 }
490 counter_sched_out(group_counter, cpuctx, ctx);
491
492 return -EAGAIN;
493}
494
Thomas Gleixner0793a612008-12-04 20:12:29 +0100495/*
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100496 * Return 1 for a group consisting entirely of software counters,
497 * 0 if the group contains any hardware counters.
498 */
499static int is_software_only_group(struct perf_counter *leader)
500{
501 struct perf_counter *counter;
502
503 if (!is_software_counter(leader))
504 return 0;
Peter Zijlstra5c148192009-03-25 12:30:23 +0100505
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100506 list_for_each_entry(counter, &leader->sibling_list, list_entry)
507 if (!is_software_counter(counter))
508 return 0;
Peter Zijlstra5c148192009-03-25 12:30:23 +0100509
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100510 return 1;
511}
512
513/*
514 * Work out whether we can put this counter group on the CPU now.
515 */
516static int group_can_go_on(struct perf_counter *counter,
517 struct perf_cpu_context *cpuctx,
518 int can_add_hw)
519{
520 /*
521 * Groups consisting entirely of software counters can always go on.
522 */
523 if (is_software_only_group(counter))
524 return 1;
525 /*
526 * If an exclusive group is already on, no other hardware
527 * counters can go on.
528 */
529 if (cpuctx->exclusive)
530 return 0;
531 /*
532 * If this group is exclusive and there are already
533 * counters on the CPU, it can't go on.
534 */
535 if (counter->hw_event.exclusive && cpuctx->active_oncpu)
536 return 0;
537 /*
538 * Otherwise, try to add it if all previous groups were able
539 * to go on.
540 */
541 return can_add_hw;
542}
543
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100544static void add_counter_to_ctx(struct perf_counter *counter,
545 struct perf_counter_context *ctx)
546{
547 list_add_counter(counter, ctx);
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100548 counter->prev_state = PERF_COUNTER_STATE_OFF;
Peter Zijlstra4af49982009-04-06 11:45:10 +0200549 counter->tstamp_enabled = ctx->time;
550 counter->tstamp_running = ctx->time;
551 counter->tstamp_stopped = ctx->time;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100552}
553
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100554/*
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100555 * Cross CPU call to install and enable a performance counter
Thomas Gleixner0793a612008-12-04 20:12:29 +0100556 */
557static void __perf_install_in_context(void *info)
558{
559 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
560 struct perf_counter *counter = info;
561 struct perf_counter_context *ctx = counter->ctx;
Paul Mackerrasd859e292009-01-17 18:10:22 +1100562 struct perf_counter *leader = counter->group_leader;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100563 int cpu = smp_processor_id();
Ingo Molnar9b51f662008-12-12 13:49:45 +0100564 unsigned long flags;
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100565 int err;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100566
567 /*
568 * If this is a task context, we need to check whether it is
569 * the current task context of this cpu. If not it has been
570 * scheduled out before the smp call arrived.
571 */
572 if (ctx->task && cpuctx->task_ctx != ctx)
573 return;
574
Peter Zijlstra849691a2009-04-06 11:45:12 +0200575 spin_lock_irqsave(&ctx->lock, flags);
Peter Zijlstra4af49982009-04-06 11:45:10 +0200576 update_context_time(ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100577
578 /*
579 * Protect the list operation against NMI by disabling the
580 * counters on a global level. NOP for non NMI based counters.
581 */
Peter Zijlstra9e35ad32009-05-13 16:21:38 +0200582 perf_disable();
Thomas Gleixner0793a612008-12-04 20:12:29 +0100583
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100584 add_counter_to_ctx(counter, ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100585
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100586 /*
Paul Mackerrasd859e292009-01-17 18:10:22 +1100587 * Don't put the counter on if it is disabled or if
588 * it is in a group and the group isn't on.
589 */
590 if (counter->state != PERF_COUNTER_STATE_INACTIVE ||
591 (leader != counter && leader->state != PERF_COUNTER_STATE_ACTIVE))
592 goto unlock;
593
594 /*
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100595 * An exclusive counter can't go on if there are already active
596 * hardware counters, and no hardware counter can go on if there
597 * is already an exclusive counter on.
598 */
Paul Mackerrasd859e292009-01-17 18:10:22 +1100599 if (!group_can_go_on(counter, cpuctx, 1))
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100600 err = -EEXIST;
601 else
602 err = counter_sched_in(counter, cpuctx, ctx, cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100603
Paul Mackerrasd859e292009-01-17 18:10:22 +1100604 if (err) {
605 /*
606 * This counter couldn't go on. If it is in a group
607 * then we have to pull the whole group off.
608 * If the counter group is pinned then put it in error state.
609 */
610 if (leader != counter)
611 group_sched_out(leader, cpuctx, ctx);
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100612 if (leader->hw_event.pinned) {
613 update_group_times(leader);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100614 leader->state = PERF_COUNTER_STATE_ERROR;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100615 }
Paul Mackerrasd859e292009-01-17 18:10:22 +1100616 }
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100617
618 if (!err && !ctx->task && cpuctx->max_pertask)
Thomas Gleixner0793a612008-12-04 20:12:29 +0100619 cpuctx->max_pertask--;
620
Paul Mackerrasd859e292009-01-17 18:10:22 +1100621 unlock:
Peter Zijlstra9e35ad32009-05-13 16:21:38 +0200622 perf_enable();
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100623
Peter Zijlstra849691a2009-04-06 11:45:12 +0200624 spin_unlock_irqrestore(&ctx->lock, flags);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100625}
626
627/*
628 * Attach a performance counter to a context
629 *
630 * First we add the counter to the list with the hardware enable bit
631 * in counter->hw_config cleared.
632 *
633 * If the counter is attached to a task which is on a CPU we use a smp
634 * call to enable it in the task context. The task might have been
635 * scheduled away, but we check this in the smp call again.
Paul Mackerrasd859e292009-01-17 18:10:22 +1100636 *
637 * Must be called with ctx->mutex held.
Thomas Gleixner0793a612008-12-04 20:12:29 +0100638 */
639static void
640perf_install_in_context(struct perf_counter_context *ctx,
641 struct perf_counter *counter,
642 int cpu)
643{
644 struct task_struct *task = ctx->task;
645
Thomas Gleixner0793a612008-12-04 20:12:29 +0100646 if (!task) {
647 /*
648 * Per cpu counters are installed via an smp call and
649 * the install is always sucessful.
650 */
651 smp_call_function_single(cpu, __perf_install_in_context,
652 counter, 1);
653 return;
654 }
655
656 counter->task = task;
657retry:
658 task_oncpu_function_call(task, __perf_install_in_context,
659 counter);
660
661 spin_lock_irq(&ctx->lock);
662 /*
Thomas Gleixner0793a612008-12-04 20:12:29 +0100663 * we need to retry the smp call.
664 */
Paul Mackerrasd859e292009-01-17 18:10:22 +1100665 if (ctx->is_active && list_empty(&counter->list_entry)) {
Thomas Gleixner0793a612008-12-04 20:12:29 +0100666 spin_unlock_irq(&ctx->lock);
667 goto retry;
668 }
669
670 /*
671 * The lock prevents that this context is scheduled in so we
672 * can add the counter safely, if it the call above did not
673 * succeed.
674 */
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100675 if (list_empty(&counter->list_entry))
676 add_counter_to_ctx(counter, ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100677 spin_unlock_irq(&ctx->lock);
678}
679
Paul Mackerrasd859e292009-01-17 18:10:22 +1100680/*
681 * Cross CPU call to enable a performance counter
682 */
683static void __perf_counter_enable(void *info)
Ingo Molnar04289bb2008-12-11 08:38:42 +0100684{
Paul Mackerrasd859e292009-01-17 18:10:22 +1100685 struct perf_counter *counter = info;
686 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
687 struct perf_counter_context *ctx = counter->ctx;
688 struct perf_counter *leader = counter->group_leader;
689 unsigned long flags;
690 int err;
Ingo Molnar04289bb2008-12-11 08:38:42 +0100691
692 /*
Paul Mackerrasd859e292009-01-17 18:10:22 +1100693 * If this is a per-task counter, need to check whether this
694 * counter's task is the current task on this cpu.
Ingo Molnar04289bb2008-12-11 08:38:42 +0100695 */
Paul Mackerrasd859e292009-01-17 18:10:22 +1100696 if (ctx->task && cpuctx->task_ctx != ctx)
697 return;
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100698
Peter Zijlstra849691a2009-04-06 11:45:12 +0200699 spin_lock_irqsave(&ctx->lock, flags);
Peter Zijlstra4af49982009-04-06 11:45:10 +0200700 update_context_time(ctx);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100701
Paul Mackerrasc07c99b2009-02-13 22:10:34 +1100702 counter->prev_state = counter->state;
Paul Mackerrasd859e292009-01-17 18:10:22 +1100703 if (counter->state >= PERF_COUNTER_STATE_INACTIVE)
704 goto unlock;
705 counter->state = PERF_COUNTER_STATE_INACTIVE;
Peter Zijlstra4af49982009-04-06 11:45:10 +0200706 counter->tstamp_enabled = ctx->time - counter->total_time_enabled;
Paul Mackerrasd859e292009-01-17 18:10:22 +1100707
708 /*
709 * If the counter is in a group and isn't the group leader,
710 * then don't put it on unless the group is on.
711 */
712 if (leader != counter && leader->state != PERF_COUNTER_STATE_ACTIVE)
713 goto unlock;
714
Paul Mackerrase758a332009-05-12 21:59:01 +1000715 if (!group_can_go_on(counter, cpuctx, 1)) {
Paul Mackerrasd859e292009-01-17 18:10:22 +1100716 err = -EEXIST;
Paul Mackerrase758a332009-05-12 21:59:01 +1000717 } else {
Peter Zijlstra9e35ad32009-05-13 16:21:38 +0200718 perf_disable();
Paul Mackerrase758a332009-05-12 21:59:01 +1000719 if (counter == leader)
720 err = group_sched_in(counter, cpuctx, ctx,
721 smp_processor_id());
722 else
723 err = counter_sched_in(counter, cpuctx, ctx,
724 smp_processor_id());
Peter Zijlstra9e35ad32009-05-13 16:21:38 +0200725 perf_enable();
Paul Mackerrase758a332009-05-12 21:59:01 +1000726 }
Paul Mackerrasd859e292009-01-17 18:10:22 +1100727
728 if (err) {
729 /*
730 * If this counter can't go on and it's part of a
731 * group, then the whole group has to come off.
732 */
733 if (leader != counter)
734 group_sched_out(leader, cpuctx, ctx);
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100735 if (leader->hw_event.pinned) {
736 update_group_times(leader);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100737 leader->state = PERF_COUNTER_STATE_ERROR;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100738 }
Paul Mackerrasd859e292009-01-17 18:10:22 +1100739 }
740
741 unlock:
Peter Zijlstra849691a2009-04-06 11:45:12 +0200742 spin_unlock_irqrestore(&ctx->lock, flags);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100743}
744
745/*
746 * Enable a counter.
747 */
748static void perf_counter_enable(struct perf_counter *counter)
749{
750 struct perf_counter_context *ctx = counter->ctx;
751 struct task_struct *task = ctx->task;
752
753 if (!task) {
754 /*
755 * Enable the counter on the cpu that it's on
756 */
757 smp_call_function_single(counter->cpu, __perf_counter_enable,
758 counter, 1);
759 return;
760 }
761
762 spin_lock_irq(&ctx->lock);
763 if (counter->state >= PERF_COUNTER_STATE_INACTIVE)
764 goto out;
765
766 /*
767 * If the counter is in error state, clear that first.
768 * That way, if we see the counter in error state below, we
769 * know that it has gone back into error state, as distinct
770 * from the task having been scheduled away before the
771 * cross-call arrived.
772 */
773 if (counter->state == PERF_COUNTER_STATE_ERROR)
774 counter->state = PERF_COUNTER_STATE_OFF;
775
776 retry:
777 spin_unlock_irq(&ctx->lock);
778 task_oncpu_function_call(task, __perf_counter_enable, counter);
779
780 spin_lock_irq(&ctx->lock);
781
782 /*
783 * If the context is active and the counter is still off,
784 * we need to retry the cross-call.
785 */
786 if (ctx->is_active && counter->state == PERF_COUNTER_STATE_OFF)
787 goto retry;
788
789 /*
790 * Since we have the lock this context can't be scheduled
791 * in, so we can change the state safely.
792 */
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100793 if (counter->state == PERF_COUNTER_STATE_OFF) {
Paul Mackerrasd859e292009-01-17 18:10:22 +1100794 counter->state = PERF_COUNTER_STATE_INACTIVE;
Peter Zijlstra4af49982009-04-06 11:45:10 +0200795 counter->tstamp_enabled =
796 ctx->time - counter->total_time_enabled;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100797 }
Paul Mackerrasd859e292009-01-17 18:10:22 +1100798 out:
799 spin_unlock_irq(&ctx->lock);
800}
801
Peter Zijlstra2023b352009-05-05 17:50:26 +0200802static int perf_counter_refresh(struct perf_counter *counter, int refresh)
Peter Zijlstra79f14642009-04-06 11:45:07 +0200803{
Peter Zijlstra2023b352009-05-05 17:50:26 +0200804 /*
805 * not supported on inherited counters
806 */
807 if (counter->hw_event.inherit)
808 return -EINVAL;
809
Peter Zijlstra79f14642009-04-06 11:45:07 +0200810 atomic_add(refresh, &counter->event_limit);
811 perf_counter_enable(counter);
Peter Zijlstra2023b352009-05-05 17:50:26 +0200812
813 return 0;
Peter Zijlstra79f14642009-04-06 11:45:07 +0200814}
815
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100816void __perf_counter_sched_out(struct perf_counter_context *ctx,
817 struct perf_cpu_context *cpuctx)
818{
819 struct perf_counter *counter;
820
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100821 spin_lock(&ctx->lock);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100822 ctx->is_active = 0;
823 if (likely(!ctx->nr_counters))
824 goto out;
Peter Zijlstra4af49982009-04-06 11:45:10 +0200825 update_context_time(ctx);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100826
Peter Zijlstra9e35ad32009-05-13 16:21:38 +0200827 perf_disable();
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100828 if (ctx->nr_active) {
829 list_for_each_entry(counter, &ctx->counter_list, list_entry)
830 group_sched_out(counter, cpuctx, ctx);
831 }
Peter Zijlstra9e35ad32009-05-13 16:21:38 +0200832 perf_enable();
Paul Mackerrasd859e292009-01-17 18:10:22 +1100833 out:
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100834 spin_unlock(&ctx->lock);
835}
836
Thomas Gleixner0793a612008-12-04 20:12:29 +0100837/*
838 * Called from scheduler to remove the counters of the current task,
839 * with interrupts disabled.
840 *
841 * We stop each counter and update the counter value in counter->count.
842 *
Ingo Molnar76715812008-12-17 14:20:28 +0100843 * This does not protect us against NMI, but disable()
Thomas Gleixner0793a612008-12-04 20:12:29 +0100844 * sets the disabled bit in the control field of counter _before_
845 * accessing the counter control register. If a NMI hits, then it will
846 * not restart the counter.
847 */
848void perf_counter_task_sched_out(struct task_struct *task, int cpu)
849{
850 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
851 struct perf_counter_context *ctx = &task->perf_counter_ctx;
Peter Zijlstra4a0deca2009-03-19 20:26:12 +0100852 struct pt_regs *regs;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100853
854 if (likely(!cpuctx->task_ctx))
855 return;
856
Peter Zijlstrabce379b2009-04-06 11:45:13 +0200857 update_context_time(ctx);
858
Peter Zijlstra4a0deca2009-03-19 20:26:12 +0100859 regs = task_pt_regs(task);
Peter Zijlstra78f13e92009-04-08 15:01:33 +0200860 perf_swcounter_event(PERF_COUNT_CONTEXT_SWITCHES, 1, 1, regs, 0);
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100861 __perf_counter_sched_out(ctx, cpuctx);
862
Thomas Gleixner0793a612008-12-04 20:12:29 +0100863 cpuctx->task_ctx = NULL;
864}
865
Paul Mackerrasa08b1592009-05-11 15:46:10 +1000866static void __perf_counter_task_sched_out(struct perf_counter_context *ctx)
867{
868 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
869
870 __perf_counter_sched_out(ctx, cpuctx);
871 cpuctx->task_ctx = NULL;
872}
873
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100874static void perf_counter_cpu_sched_out(struct perf_cpu_context *cpuctx)
Ingo Molnar04289bb2008-12-11 08:38:42 +0100875{
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100876 __perf_counter_sched_out(&cpuctx->ctx, cpuctx);
Ingo Molnar04289bb2008-12-11 08:38:42 +0100877}
878
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100879static void
880__perf_counter_sched_in(struct perf_counter_context *ctx,
881 struct perf_cpu_context *cpuctx, int cpu)
Thomas Gleixner0793a612008-12-04 20:12:29 +0100882{
Thomas Gleixner0793a612008-12-04 20:12:29 +0100883 struct perf_counter *counter;
Paul Mackerrasdd0e6ba2009-01-12 15:11:00 +1100884 int can_add_hw = 1;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100885
Thomas Gleixner0793a612008-12-04 20:12:29 +0100886 spin_lock(&ctx->lock);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100887 ctx->is_active = 1;
888 if (likely(!ctx->nr_counters))
889 goto out;
890
Peter Zijlstra4af49982009-04-06 11:45:10 +0200891 ctx->timestamp = perf_clock();
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100892
Peter Zijlstra9e35ad32009-05-13 16:21:38 +0200893 perf_disable();
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100894
895 /*
896 * First go through the list and put on any pinned groups
897 * in order to give them the best chance of going on.
898 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100899 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100900 if (counter->state <= PERF_COUNTER_STATE_OFF ||
901 !counter->hw_event.pinned)
902 continue;
903 if (counter->cpu != -1 && counter->cpu != cpu)
904 continue;
905
906 if (group_can_go_on(counter, cpuctx, 1))
907 group_sched_in(counter, cpuctx, ctx, cpu);
908
909 /*
910 * If this pinned group hasn't been scheduled,
911 * put it in error state.
912 */
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100913 if (counter->state == PERF_COUNTER_STATE_INACTIVE) {
914 update_group_times(counter);
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100915 counter->state = PERF_COUNTER_STATE_ERROR;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100916 }
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100917 }
918
919 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
920 /*
921 * Ignore counters in OFF or ERROR state, and
922 * ignore pinned counters since we did them already.
923 */
924 if (counter->state <= PERF_COUNTER_STATE_OFF ||
925 counter->hw_event.pinned)
926 continue;
927
Ingo Molnar04289bb2008-12-11 08:38:42 +0100928 /*
929 * Listen to the 'cpu' scheduling filter constraint
930 * of counters:
931 */
Thomas Gleixner0793a612008-12-04 20:12:29 +0100932 if (counter->cpu != -1 && counter->cpu != cpu)
933 continue;
934
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100935 if (group_can_go_on(counter, cpuctx, can_add_hw)) {
Paul Mackerrasdd0e6ba2009-01-12 15:11:00 +1100936 if (group_sched_in(counter, cpuctx, ctx, cpu))
937 can_add_hw = 0;
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100938 }
Thomas Gleixner0793a612008-12-04 20:12:29 +0100939 }
Peter Zijlstra9e35ad32009-05-13 16:21:38 +0200940 perf_enable();
Paul Mackerrasd859e292009-01-17 18:10:22 +1100941 out:
Thomas Gleixner0793a612008-12-04 20:12:29 +0100942 spin_unlock(&ctx->lock);
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100943}
Ingo Molnar04289bb2008-12-11 08:38:42 +0100944
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100945/*
946 * Called from scheduler to add the counters of the current task
947 * with interrupts disabled.
948 *
949 * We restore the counter value and then enable it.
950 *
951 * This does not protect us against NMI, but enable()
952 * sets the enabled bit in the control field of counter _before_
953 * accessing the counter control register. If a NMI hits, then it will
954 * keep the counter running.
955 */
956void perf_counter_task_sched_in(struct task_struct *task, int cpu)
957{
958 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
959 struct perf_counter_context *ctx = &task->perf_counter_ctx;
960
961 __perf_counter_sched_in(ctx, cpuctx, cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100962 cpuctx->task_ctx = ctx;
963}
964
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100965static void perf_counter_cpu_sched_in(struct perf_cpu_context *cpuctx, int cpu)
966{
967 struct perf_counter_context *ctx = &cpuctx->ctx;
968
969 __perf_counter_sched_in(ctx, cpuctx, cpu);
970}
971
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +0100972int perf_counter_task_disable(void)
973{
974 struct task_struct *curr = current;
975 struct perf_counter_context *ctx = &curr->perf_counter_ctx;
976 struct perf_counter *counter;
Ingo Molnaraa9c4c02008-12-17 14:10:57 +0100977 unsigned long flags;
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +0100978
979 if (likely(!ctx->nr_counters))
980 return 0;
981
Peter Zijlstra849691a2009-04-06 11:45:12 +0200982 local_irq_save(flags);
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +0100983
Paul Mackerrasa08b1592009-05-11 15:46:10 +1000984 __perf_counter_task_sched_out(ctx);
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +0100985
986 spin_lock(&ctx->lock);
987
988 /*
989 * Disable all the counters:
990 */
Peter Zijlstra9e35ad32009-05-13 16:21:38 +0200991 perf_disable();
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +0100992
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100993 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100994 if (counter->state != PERF_COUNTER_STATE_ERROR) {
995 update_group_times(counter);
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100996 counter->state = PERF_COUNTER_STATE_OFF;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100997 }
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100998 }
Ingo Molnar9b51f662008-12-12 13:49:45 +0100999
Peter Zijlstra9e35ad32009-05-13 16:21:38 +02001000 perf_enable();
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +01001001
Peter Zijlstra849691a2009-04-06 11:45:12 +02001002 spin_unlock_irqrestore(&ctx->lock, flags);
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +01001003
1004 return 0;
1005}
1006
1007int perf_counter_task_enable(void)
1008{
1009 struct task_struct *curr = current;
1010 struct perf_counter_context *ctx = &curr->perf_counter_ctx;
1011 struct perf_counter *counter;
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01001012 unsigned long flags;
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +01001013 int cpu;
1014
1015 if (likely(!ctx->nr_counters))
1016 return 0;
1017
Peter Zijlstra849691a2009-04-06 11:45:12 +02001018 local_irq_save(flags);
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +01001019 cpu = smp_processor_id();
1020
Paul Mackerrasa08b1592009-05-11 15:46:10 +10001021 __perf_counter_task_sched_out(ctx);
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001022
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +01001023 spin_lock(&ctx->lock);
1024
1025 /*
1026 * Disable all the counters:
1027 */
Peter Zijlstra9e35ad32009-05-13 16:21:38 +02001028 perf_disable();
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +01001029
1030 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001031 if (counter->state > PERF_COUNTER_STATE_OFF)
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +01001032 continue;
Ingo Molnar6a930702008-12-11 15:17:03 +01001033 counter->state = PERF_COUNTER_STATE_INACTIVE;
Peter Zijlstra4af49982009-04-06 11:45:10 +02001034 counter->tstamp_enabled =
1035 ctx->time - counter->total_time_enabled;
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01001036 counter->hw_event.disabled = 0;
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +01001037 }
Peter Zijlstra9e35ad32009-05-13 16:21:38 +02001038 perf_enable();
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +01001039
1040 spin_unlock(&ctx->lock);
1041
1042 perf_counter_task_sched_in(curr, cpu);
1043
Peter Zijlstra849691a2009-04-06 11:45:12 +02001044 local_irq_restore(flags);
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +01001045
1046 return 0;
1047}
1048
Peter Zijlstra26b119b2009-05-20 12:21:20 +02001049static void perf_log_period(struct perf_counter *counter, u64 period);
1050
1051static void perf_adjust_freq(struct perf_counter_context *ctx)
Peter Zijlstra60db5e02009-05-15 15:19:28 +02001052{
1053 struct perf_counter *counter;
1054 u64 irq_period;
1055 u64 events, period;
1056 s64 delta;
1057
1058 spin_lock(&ctx->lock);
1059 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
1060 if (counter->state != PERF_COUNTER_STATE_ACTIVE)
1061 continue;
1062
1063 if (!counter->hw_event.freq || !counter->hw_event.irq_freq)
1064 continue;
1065
1066 events = HZ * counter->hw.interrupts * counter->hw.irq_period;
1067 period = div64_u64(events, counter->hw_event.irq_freq);
1068
1069 delta = (s64)(1 + period - counter->hw.irq_period);
1070 delta >>= 1;
1071
1072 irq_period = counter->hw.irq_period + delta;
1073
1074 if (!irq_period)
1075 irq_period = 1;
1076
Peter Zijlstra26b119b2009-05-20 12:21:20 +02001077 perf_log_period(counter, irq_period);
1078
Peter Zijlstra60db5e02009-05-15 15:19:28 +02001079 counter->hw.irq_period = irq_period;
1080 counter->hw.interrupts = 0;
1081 }
1082 spin_unlock(&ctx->lock);
1083}
1084
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001085/*
1086 * Round-robin a context's counters:
1087 */
1088static void rotate_ctx(struct perf_counter_context *ctx)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001089{
Thomas Gleixner0793a612008-12-04 20:12:29 +01001090 struct perf_counter *counter;
1091
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001092 if (!ctx->nr_counters)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001093 return;
1094
Thomas Gleixner0793a612008-12-04 20:12:29 +01001095 spin_lock(&ctx->lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001096 /*
Ingo Molnar04289bb2008-12-11 08:38:42 +01001097 * Rotate the first entry last (works just fine for group counters too):
Thomas Gleixner0793a612008-12-04 20:12:29 +01001098 */
Peter Zijlstra9e35ad32009-05-13 16:21:38 +02001099 perf_disable();
Ingo Molnar04289bb2008-12-11 08:38:42 +01001100 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
Peter Zijlstra75564232009-03-13 12:21:29 +01001101 list_move_tail(&counter->list_entry, &ctx->counter_list);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001102 break;
1103 }
Peter Zijlstra9e35ad32009-05-13 16:21:38 +02001104 perf_enable();
Thomas Gleixner0793a612008-12-04 20:12:29 +01001105
1106 spin_unlock(&ctx->lock);
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001107}
Thomas Gleixner0793a612008-12-04 20:12:29 +01001108
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001109void perf_counter_task_tick(struct task_struct *curr, int cpu)
1110{
Peter Zijlstra7fc23a52009-05-08 18:52:21 +02001111 struct perf_cpu_context *cpuctx;
1112 struct perf_counter_context *ctx;
1113
1114 if (!atomic_read(&nr_counters))
1115 return;
1116
1117 cpuctx = &per_cpu(perf_cpu_context, cpu);
1118 ctx = &curr->perf_counter_ctx;
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001119
Peter Zijlstra60db5e02009-05-15 15:19:28 +02001120 perf_adjust_freq(&cpuctx->ctx);
1121 perf_adjust_freq(ctx);
1122
Ingo Molnarb82914c2009-05-04 18:54:32 +02001123 perf_counter_cpu_sched_out(cpuctx);
Paul Mackerrasa08b1592009-05-11 15:46:10 +10001124 __perf_counter_task_sched_out(ctx);
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001125
Ingo Molnarb82914c2009-05-04 18:54:32 +02001126 rotate_ctx(&cpuctx->ctx);
Peter Zijlstrad7b629a2009-05-20 12:21:19 +02001127 rotate_ctx(ctx);
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001128
Ingo Molnarb82914c2009-05-04 18:54:32 +02001129 perf_counter_cpu_sched_in(cpuctx, cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001130 perf_counter_task_sched_in(curr, cpu);
1131}
1132
1133/*
Thomas Gleixner0793a612008-12-04 20:12:29 +01001134 * Cross CPU call to read the hardware counter
1135 */
Ingo Molnar76715812008-12-17 14:20:28 +01001136static void __read(void *info)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001137{
Ingo Molnar621a01e2008-12-11 12:46:46 +01001138 struct perf_counter *counter = info;
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001139 struct perf_counter_context *ctx = counter->ctx;
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01001140 unsigned long flags;
Ingo Molnar621a01e2008-12-11 12:46:46 +01001141
Peter Zijlstra849691a2009-04-06 11:45:12 +02001142 local_irq_save(flags);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001143 if (ctx->is_active)
Peter Zijlstra4af49982009-04-06 11:45:10 +02001144 update_context_time(ctx);
Robert Richter4aeb0b42009-04-29 12:47:03 +02001145 counter->pmu->read(counter);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001146 update_counter_times(counter);
Peter Zijlstra849691a2009-04-06 11:45:12 +02001147 local_irq_restore(flags);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001148}
1149
Ingo Molnar04289bb2008-12-11 08:38:42 +01001150static u64 perf_counter_read(struct perf_counter *counter)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001151{
1152 /*
1153 * If counter is enabled and currently active on a CPU, update the
1154 * value in the counter structure:
1155 */
Ingo Molnar6a930702008-12-11 15:17:03 +01001156 if (counter->state == PERF_COUNTER_STATE_ACTIVE) {
Thomas Gleixner0793a612008-12-04 20:12:29 +01001157 smp_call_function_single(counter->oncpu,
Ingo Molnar76715812008-12-17 14:20:28 +01001158 __read, counter, 1);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001159 } else if (counter->state == PERF_COUNTER_STATE_INACTIVE) {
1160 update_counter_times(counter);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001161 }
1162
Ingo Molnaree060942008-12-13 09:00:03 +01001163 return atomic64_read(&counter->count);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001164}
1165
Thomas Gleixner0793a612008-12-04 20:12:29 +01001166static void put_context(struct perf_counter_context *ctx)
1167{
1168 if (ctx->task)
1169 put_task_struct(ctx->task);
1170}
1171
1172static struct perf_counter_context *find_get_context(pid_t pid, int cpu)
1173{
1174 struct perf_cpu_context *cpuctx;
1175 struct perf_counter_context *ctx;
1176 struct task_struct *task;
1177
1178 /*
1179 * If cpu is not a wildcard then this is a percpu counter:
1180 */
1181 if (cpu != -1) {
1182 /* Must be root to operate on a CPU counter: */
Peter Zijlstra1ccd1542009-04-09 10:53:45 +02001183 if (sysctl_perf_counter_priv && !capable(CAP_SYS_ADMIN))
Thomas Gleixner0793a612008-12-04 20:12:29 +01001184 return ERR_PTR(-EACCES);
1185
1186 if (cpu < 0 || cpu > num_possible_cpus())
1187 return ERR_PTR(-EINVAL);
1188
1189 /*
1190 * We could be clever and allow to attach a counter to an
1191 * offline CPU and activate it when the CPU comes up, but
1192 * that's for later.
1193 */
1194 if (!cpu_isset(cpu, cpu_online_map))
1195 return ERR_PTR(-ENODEV);
1196
1197 cpuctx = &per_cpu(perf_cpu_context, cpu);
1198 ctx = &cpuctx->ctx;
1199
Thomas Gleixner0793a612008-12-04 20:12:29 +01001200 return ctx;
1201 }
1202
1203 rcu_read_lock();
1204 if (!pid)
1205 task = current;
1206 else
1207 task = find_task_by_vpid(pid);
1208 if (task)
1209 get_task_struct(task);
1210 rcu_read_unlock();
1211
1212 if (!task)
1213 return ERR_PTR(-ESRCH);
1214
1215 ctx = &task->perf_counter_ctx;
1216 ctx->task = task;
1217
1218 /* Reuse ptrace permission checks for now. */
1219 if (!ptrace_may_access(task, PTRACE_MODE_READ)) {
1220 put_context(ctx);
1221 return ERR_PTR(-EACCES);
1222 }
1223
1224 return ctx;
1225}
1226
Peter Zijlstra592903c2009-03-13 12:21:36 +01001227static void free_counter_rcu(struct rcu_head *head)
1228{
1229 struct perf_counter *counter;
1230
1231 counter = container_of(head, struct perf_counter, rcu_head);
1232 kfree(counter);
1233}
1234
Peter Zijlstra925d5192009-03-30 19:07:02 +02001235static void perf_pending_sync(struct perf_counter *counter);
1236
Peter Zijlstraf1600952009-03-19 20:26:16 +01001237static void free_counter(struct perf_counter *counter)
1238{
Peter Zijlstra925d5192009-03-30 19:07:02 +02001239 perf_pending_sync(counter);
1240
Peter Zijlstra7fc23a52009-05-08 18:52:21 +02001241 atomic_dec(&nr_counters);
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02001242 if (counter->hw_event.mmap)
1243 atomic_dec(&nr_mmap_tracking);
1244 if (counter->hw_event.munmap)
1245 atomic_dec(&nr_munmap_tracking);
1246 if (counter->hw_event.comm)
1247 atomic_dec(&nr_comm_tracking);
1248
Peter Zijlstrae077df42009-03-19 20:26:17 +01001249 if (counter->destroy)
1250 counter->destroy(counter);
1251
Peter Zijlstraf1600952009-03-19 20:26:16 +01001252 call_rcu(&counter->rcu_head, free_counter_rcu);
1253}
1254
Thomas Gleixner0793a612008-12-04 20:12:29 +01001255/*
1256 * Called when the last reference to the file is gone.
1257 */
1258static int perf_release(struct inode *inode, struct file *file)
1259{
1260 struct perf_counter *counter = file->private_data;
1261 struct perf_counter_context *ctx = counter->ctx;
1262
1263 file->private_data = NULL;
1264
Paul Mackerrasd859e292009-01-17 18:10:22 +11001265 mutex_lock(&ctx->mutex);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001266 mutex_lock(&counter->mutex);
1267
Ingo Molnar04289bb2008-12-11 08:38:42 +01001268 perf_counter_remove_from_context(counter);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001269
1270 mutex_unlock(&counter->mutex);
Paul Mackerrasd859e292009-01-17 18:10:22 +11001271 mutex_unlock(&ctx->mutex);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001272
Peter Zijlstraf1600952009-03-19 20:26:16 +01001273 free_counter(counter);
Mike Galbraith5af75912009-02-11 10:53:37 +01001274 put_context(ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001275
1276 return 0;
1277}
1278
1279/*
1280 * Read the performance counter - simple non blocking version for now
1281 */
1282static ssize_t
1283perf_read_hw(struct perf_counter *counter, char __user *buf, size_t count)
1284{
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001285 u64 values[3];
1286 int n;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001287
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001288 /*
1289 * Return end-of-file for a read on a counter that is in
1290 * error state (i.e. because it was pinned but it couldn't be
1291 * scheduled on to the CPU at some point).
1292 */
1293 if (counter->state == PERF_COUNTER_STATE_ERROR)
1294 return 0;
1295
Thomas Gleixner0793a612008-12-04 20:12:29 +01001296 mutex_lock(&counter->mutex);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001297 values[0] = perf_counter_read(counter);
1298 n = 1;
1299 if (counter->hw_event.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
1300 values[n++] = counter->total_time_enabled +
1301 atomic64_read(&counter->child_total_time_enabled);
1302 if (counter->hw_event.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
1303 values[n++] = counter->total_time_running +
1304 atomic64_read(&counter->child_total_time_running);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001305 mutex_unlock(&counter->mutex);
1306
Paul Mackerras53cfbf52009-03-25 22:46:58 +11001307 if (count < n * sizeof(u64))
1308 return -EINVAL;
1309 count = n * sizeof(u64);
1310
1311 if (copy_to_user(buf, values, count))
1312 return -EFAULT;
1313
1314 return count;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001315}
1316
1317static ssize_t
Thomas Gleixner0793a612008-12-04 20:12:29 +01001318perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
1319{
1320 struct perf_counter *counter = file->private_data;
1321
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001322 return perf_read_hw(counter, buf, count);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001323}
1324
1325static unsigned int perf_poll(struct file *file, poll_table *wait)
1326{
1327 struct perf_counter *counter = file->private_data;
Peter Zijlstrac7138f32009-03-24 13:18:16 +01001328 struct perf_mmap_data *data;
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001329 unsigned int events = POLL_HUP;
Peter Zijlstrac7138f32009-03-24 13:18:16 +01001330
1331 rcu_read_lock();
1332 data = rcu_dereference(counter->data);
1333 if (data)
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001334 events = atomic_xchg(&data->poll, 0);
Peter Zijlstrac7138f32009-03-24 13:18:16 +01001335 rcu_read_unlock();
Thomas Gleixner0793a612008-12-04 20:12:29 +01001336
1337 poll_wait(file, &counter->waitq, wait);
1338
Thomas Gleixner0793a612008-12-04 20:12:29 +01001339 return events;
1340}
1341
Peter Zijlstra6de6a7b2009-05-05 17:50:23 +02001342static void perf_counter_reset(struct perf_counter *counter)
1343{
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001344 (void)perf_counter_read(counter);
Paul Mackerras615a3f12009-05-11 15:50:21 +10001345 atomic64_set(&counter->count, 0);
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001346 perf_counter_update_userpage(counter);
1347}
1348
1349static void perf_counter_for_each_sibling(struct perf_counter *counter,
1350 void (*func)(struct perf_counter *))
1351{
1352 struct perf_counter_context *ctx = counter->ctx;
1353 struct perf_counter *sibling;
1354
1355 spin_lock_irq(&ctx->lock);
1356 counter = counter->group_leader;
1357
1358 func(counter);
1359 list_for_each_entry(sibling, &counter->sibling_list, list_entry)
1360 func(sibling);
1361 spin_unlock_irq(&ctx->lock);
1362}
1363
1364static void perf_counter_for_each_child(struct perf_counter *counter,
1365 void (*func)(struct perf_counter *))
1366{
1367 struct perf_counter *child;
1368
1369 mutex_lock(&counter->mutex);
1370 func(counter);
1371 list_for_each_entry(child, &counter->child_list, child_list)
1372 func(child);
1373 mutex_unlock(&counter->mutex);
1374}
1375
1376static void perf_counter_for_each(struct perf_counter *counter,
1377 void (*func)(struct perf_counter *))
1378{
1379 struct perf_counter *child;
1380
1381 mutex_lock(&counter->mutex);
1382 perf_counter_for_each_sibling(counter, func);
1383 list_for_each_entry(child, &counter->child_list, child_list)
1384 perf_counter_for_each_sibling(child, func);
1385 mutex_unlock(&counter->mutex);
Peter Zijlstra6de6a7b2009-05-05 17:50:23 +02001386}
1387
Paul Mackerrasd859e292009-01-17 18:10:22 +11001388static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1389{
1390 struct perf_counter *counter = file->private_data;
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001391 void (*func)(struct perf_counter *);
1392 u32 flags = arg;
Paul Mackerrasd859e292009-01-17 18:10:22 +11001393
1394 switch (cmd) {
1395 case PERF_COUNTER_IOC_ENABLE:
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001396 func = perf_counter_enable;
Paul Mackerrasd859e292009-01-17 18:10:22 +11001397 break;
1398 case PERF_COUNTER_IOC_DISABLE:
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001399 func = perf_counter_disable;
Peter Zijlstra79f14642009-04-06 11:45:07 +02001400 break;
Peter Zijlstra6de6a7b2009-05-05 17:50:23 +02001401 case PERF_COUNTER_IOC_RESET:
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001402 func = perf_counter_reset;
Peter Zijlstra6de6a7b2009-05-05 17:50:23 +02001403 break;
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001404
1405 case PERF_COUNTER_IOC_REFRESH:
1406 return perf_counter_refresh(counter, arg);
Paul Mackerrasd859e292009-01-17 18:10:22 +11001407 default:
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001408 return -ENOTTY;
Paul Mackerrasd859e292009-01-17 18:10:22 +11001409 }
Peter Zijlstra3df5eda2009-05-08 18:52:22 +02001410
1411 if (flags & PERF_IOC_FLAG_GROUP)
1412 perf_counter_for_each(counter, func);
1413 else
1414 perf_counter_for_each_child(counter, func);
1415
1416 return 0;
Paul Mackerrasd859e292009-01-17 18:10:22 +11001417}
1418
Peter Zijlstra38ff6672009-03-30 19:07:03 +02001419/*
1420 * Callers need to ensure there can be no nesting of this function, otherwise
1421 * the seqlock logic goes bad. We can not serialize this because the arch
1422 * code calls this from NMI context.
1423 */
1424void perf_counter_update_userpage(struct perf_counter *counter)
Paul Mackerras37d81822009-03-23 18:22:08 +01001425{
Peter Zijlstra38ff6672009-03-30 19:07:03 +02001426 struct perf_mmap_data *data;
1427 struct perf_counter_mmap_page *userpg;
1428
1429 rcu_read_lock();
1430 data = rcu_dereference(counter->data);
1431 if (!data)
1432 goto unlock;
1433
1434 userpg = data->user_page;
Paul Mackerras37d81822009-03-23 18:22:08 +01001435
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001436 /*
1437 * Disable preemption so as to not let the corresponding user-space
1438 * spin too long if we get preempted.
1439 */
1440 preempt_disable();
Paul Mackerras37d81822009-03-23 18:22:08 +01001441 ++userpg->lock;
Peter Zijlstra92f22a32009-04-02 11:12:04 +02001442 barrier();
Paul Mackerras37d81822009-03-23 18:22:08 +01001443 userpg->index = counter->hw.idx;
1444 userpg->offset = atomic64_read(&counter->count);
1445 if (counter->state == PERF_COUNTER_STATE_ACTIVE)
1446 userpg->offset -= atomic64_read(&counter->hw.prev_count);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001447
Peter Zijlstra92f22a32009-04-02 11:12:04 +02001448 barrier();
Paul Mackerras37d81822009-03-23 18:22:08 +01001449 ++userpg->lock;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001450 preempt_enable();
Peter Zijlstra38ff6672009-03-30 19:07:03 +02001451unlock:
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001452 rcu_read_unlock();
Paul Mackerras37d81822009-03-23 18:22:08 +01001453}
1454
1455static int perf_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1456{
1457 struct perf_counter *counter = vma->vm_file->private_data;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001458 struct perf_mmap_data *data;
1459 int ret = VM_FAULT_SIGBUS;
Paul Mackerras37d81822009-03-23 18:22:08 +01001460
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001461 rcu_read_lock();
1462 data = rcu_dereference(counter->data);
1463 if (!data)
1464 goto unlock;
Paul Mackerras37d81822009-03-23 18:22:08 +01001465
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001466 if (vmf->pgoff == 0) {
1467 vmf->page = virt_to_page(data->user_page);
1468 } else {
1469 int nr = vmf->pgoff - 1;
1470
1471 if ((unsigned)nr > data->nr_pages)
1472 goto unlock;
1473
1474 vmf->page = virt_to_page(data->data_pages[nr]);
1475 }
Paul Mackerras37d81822009-03-23 18:22:08 +01001476 get_page(vmf->page);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001477 ret = 0;
1478unlock:
1479 rcu_read_unlock();
1480
1481 return ret;
1482}
1483
1484static int perf_mmap_data_alloc(struct perf_counter *counter, int nr_pages)
1485{
1486 struct perf_mmap_data *data;
1487 unsigned long size;
1488 int i;
1489
1490 WARN_ON(atomic_read(&counter->mmap_count));
1491
1492 size = sizeof(struct perf_mmap_data);
1493 size += nr_pages * sizeof(void *);
1494
1495 data = kzalloc(size, GFP_KERNEL);
1496 if (!data)
1497 goto fail;
1498
1499 data->user_page = (void *)get_zeroed_page(GFP_KERNEL);
1500 if (!data->user_page)
1501 goto fail_user_page;
1502
1503 for (i = 0; i < nr_pages; i++) {
1504 data->data_pages[i] = (void *)get_zeroed_page(GFP_KERNEL);
1505 if (!data->data_pages[i])
1506 goto fail_data_pages;
1507 }
1508
1509 data->nr_pages = nr_pages;
Peter Zijlstra22c15582009-05-05 17:50:25 +02001510 atomic_set(&data->lock, -1);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001511
1512 rcu_assign_pointer(counter->data, data);
1513
Paul Mackerras37d81822009-03-23 18:22:08 +01001514 return 0;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001515
1516fail_data_pages:
1517 for (i--; i >= 0; i--)
1518 free_page((unsigned long)data->data_pages[i]);
1519
1520 free_page((unsigned long)data->user_page);
1521
1522fail_user_page:
1523 kfree(data);
1524
1525fail:
1526 return -ENOMEM;
1527}
1528
1529static void __perf_mmap_data_free(struct rcu_head *rcu_head)
1530{
1531 struct perf_mmap_data *data = container_of(rcu_head,
1532 struct perf_mmap_data, rcu_head);
1533 int i;
1534
1535 free_page((unsigned long)data->user_page);
1536 for (i = 0; i < data->nr_pages; i++)
1537 free_page((unsigned long)data->data_pages[i]);
1538 kfree(data);
1539}
1540
1541static void perf_mmap_data_free(struct perf_counter *counter)
1542{
1543 struct perf_mmap_data *data = counter->data;
1544
1545 WARN_ON(atomic_read(&counter->mmap_count));
1546
1547 rcu_assign_pointer(counter->data, NULL);
1548 call_rcu(&data->rcu_head, __perf_mmap_data_free);
1549}
1550
1551static void perf_mmap_open(struct vm_area_struct *vma)
1552{
1553 struct perf_counter *counter = vma->vm_file->private_data;
1554
1555 atomic_inc(&counter->mmap_count);
1556}
1557
1558static void perf_mmap_close(struct vm_area_struct *vma)
1559{
1560 struct perf_counter *counter = vma->vm_file->private_data;
1561
1562 if (atomic_dec_and_mutex_lock(&counter->mmap_count,
1563 &counter->mmap_mutex)) {
Peter Zijlstra789f90f2009-05-15 15:19:27 +02001564 struct user_struct *user = current_user();
1565
1566 atomic_long_sub(counter->data->nr_pages + 1, &user->locked_vm);
Peter Zijlstrac5078f72009-05-05 17:50:24 +02001567 vma->vm_mm->locked_vm -= counter->data->nr_locked;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001568 perf_mmap_data_free(counter);
1569 mutex_unlock(&counter->mmap_mutex);
1570 }
Paul Mackerras37d81822009-03-23 18:22:08 +01001571}
1572
1573static struct vm_operations_struct perf_mmap_vmops = {
Peter Zijlstraebb3c4c2009-04-06 11:45:05 +02001574 .open = perf_mmap_open,
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001575 .close = perf_mmap_close,
Paul Mackerras37d81822009-03-23 18:22:08 +01001576 .fault = perf_mmap_fault,
1577};
1578
1579static int perf_mmap(struct file *file, struct vm_area_struct *vma)
1580{
1581 struct perf_counter *counter = file->private_data;
Peter Zijlstra789f90f2009-05-15 15:19:27 +02001582 struct user_struct *user = current_user();
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001583 unsigned long vma_size;
1584 unsigned long nr_pages;
Peter Zijlstra789f90f2009-05-15 15:19:27 +02001585 unsigned long user_locked, user_lock_limit;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001586 unsigned long locked, lock_limit;
Peter Zijlstra789f90f2009-05-15 15:19:27 +02001587 long user_extra, extra;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001588 int ret = 0;
Paul Mackerras37d81822009-03-23 18:22:08 +01001589
1590 if (!(vma->vm_flags & VM_SHARED) || (vma->vm_flags & VM_WRITE))
1591 return -EINVAL;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001592
1593 vma_size = vma->vm_end - vma->vm_start;
1594 nr_pages = (vma_size / PAGE_SIZE) - 1;
1595
Peter Zijlstra7730d862009-03-25 12:48:31 +01001596 /*
1597 * If we have data pages ensure they're a power-of-two number, so we
1598 * can do bitmasks instead of modulo.
1599 */
1600 if (nr_pages != 0 && !is_power_of_2(nr_pages))
Paul Mackerras37d81822009-03-23 18:22:08 +01001601 return -EINVAL;
1602
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001603 if (vma_size != PAGE_SIZE * (1 + nr_pages))
Paul Mackerras37d81822009-03-23 18:22:08 +01001604 return -EINVAL;
1605
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001606 if (vma->vm_pgoff != 0)
1607 return -EINVAL;
Paul Mackerras37d81822009-03-23 18:22:08 +01001608
Peter Zijlstraebb3c4c2009-04-06 11:45:05 +02001609 mutex_lock(&counter->mmap_mutex);
1610 if (atomic_inc_not_zero(&counter->mmap_count)) {
1611 if (nr_pages != counter->data->nr_pages)
1612 ret = -EINVAL;
1613 goto unlock;
1614 }
1615
Peter Zijlstra789f90f2009-05-15 15:19:27 +02001616 user_extra = nr_pages + 1;
1617 user_lock_limit = sysctl_perf_counter_mlock >> (PAGE_SHIFT - 10);
1618 user_locked = atomic_long_read(&user->locked_vm) + user_extra;
Peter Zijlstrac5078f72009-05-05 17:50:24 +02001619
Peter Zijlstra789f90f2009-05-15 15:19:27 +02001620 extra = 0;
1621 if (user_locked > user_lock_limit)
1622 extra = user_locked - user_lock_limit;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001623
1624 lock_limit = current->signal->rlim[RLIMIT_MEMLOCK].rlim_cur;
1625 lock_limit >>= PAGE_SHIFT;
Peter Zijlstra789f90f2009-05-15 15:19:27 +02001626 locked = vma->vm_mm->locked_vm + extra;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001627
Peter Zijlstraebb3c4c2009-04-06 11:45:05 +02001628 if ((locked > lock_limit) && !capable(CAP_IPC_LOCK)) {
1629 ret = -EPERM;
1630 goto unlock;
1631 }
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001632
1633 WARN_ON(counter->data);
1634 ret = perf_mmap_data_alloc(counter, nr_pages);
Peter Zijlstraebb3c4c2009-04-06 11:45:05 +02001635 if (ret)
1636 goto unlock;
1637
1638 atomic_set(&counter->mmap_count, 1);
Peter Zijlstra789f90f2009-05-15 15:19:27 +02001639 atomic_long_add(user_extra, &user->locked_vm);
Peter Zijlstrac5078f72009-05-05 17:50:24 +02001640 vma->vm_mm->locked_vm += extra;
1641 counter->data->nr_locked = extra;
Peter Zijlstraebb3c4c2009-04-06 11:45:05 +02001642unlock:
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001643 mutex_unlock(&counter->mmap_mutex);
Paul Mackerras37d81822009-03-23 18:22:08 +01001644
1645 vma->vm_flags &= ~VM_MAYWRITE;
1646 vma->vm_flags |= VM_RESERVED;
1647 vma->vm_ops = &perf_mmap_vmops;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001648
1649 return ret;
Paul Mackerras37d81822009-03-23 18:22:08 +01001650}
1651
Peter Zijlstra3c446b32009-04-06 11:45:01 +02001652static int perf_fasync(int fd, struct file *filp, int on)
1653{
1654 struct perf_counter *counter = filp->private_data;
1655 struct inode *inode = filp->f_path.dentry->d_inode;
1656 int retval;
1657
1658 mutex_lock(&inode->i_mutex);
1659 retval = fasync_helper(fd, filp, on, &counter->fasync);
1660 mutex_unlock(&inode->i_mutex);
1661
1662 if (retval < 0)
1663 return retval;
1664
1665 return 0;
1666}
1667
Thomas Gleixner0793a612008-12-04 20:12:29 +01001668static const struct file_operations perf_fops = {
1669 .release = perf_release,
1670 .read = perf_read,
1671 .poll = perf_poll,
Paul Mackerrasd859e292009-01-17 18:10:22 +11001672 .unlocked_ioctl = perf_ioctl,
1673 .compat_ioctl = perf_ioctl,
Paul Mackerras37d81822009-03-23 18:22:08 +01001674 .mmap = perf_mmap,
Peter Zijlstra3c446b32009-04-06 11:45:01 +02001675 .fasync = perf_fasync,
Thomas Gleixner0793a612008-12-04 20:12:29 +01001676};
1677
Peter Zijlstra15dbf272009-03-13 12:21:32 +01001678/*
Peter Zijlstra925d5192009-03-30 19:07:02 +02001679 * Perf counter wakeup
1680 *
1681 * If there's data, ensure we set the poll() state and publish everything
1682 * to user-space before waking everybody up.
1683 */
1684
1685void perf_counter_wakeup(struct perf_counter *counter)
1686{
Peter Zijlstra925d5192009-03-30 19:07:02 +02001687 wake_up_all(&counter->waitq);
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02001688
1689 if (counter->pending_kill) {
1690 kill_fasync(&counter->fasync, SIGIO, counter->pending_kill);
1691 counter->pending_kill = 0;
1692 }
Peter Zijlstra925d5192009-03-30 19:07:02 +02001693}
1694
1695/*
1696 * Pending wakeups
1697 *
1698 * Handle the case where we need to wakeup up from NMI (or rq->lock) context.
1699 *
1700 * The NMI bit means we cannot possibly take locks. Therefore, maintain a
1701 * single linked list and use cmpxchg() to add entries lockless.
1702 */
1703
Peter Zijlstra79f14642009-04-06 11:45:07 +02001704static void perf_pending_counter(struct perf_pending_entry *entry)
1705{
1706 struct perf_counter *counter = container_of(entry,
1707 struct perf_counter, pending);
1708
1709 if (counter->pending_disable) {
1710 counter->pending_disable = 0;
1711 perf_counter_disable(counter);
1712 }
1713
1714 if (counter->pending_wakeup) {
1715 counter->pending_wakeup = 0;
1716 perf_counter_wakeup(counter);
1717 }
1718}
1719
Peter Zijlstra671dec52009-04-06 11:45:02 +02001720#define PENDING_TAIL ((struct perf_pending_entry *)-1UL)
Peter Zijlstra925d5192009-03-30 19:07:02 +02001721
Peter Zijlstra671dec52009-04-06 11:45:02 +02001722static DEFINE_PER_CPU(struct perf_pending_entry *, perf_pending_head) = {
Peter Zijlstra925d5192009-03-30 19:07:02 +02001723 PENDING_TAIL,
1724};
1725
Peter Zijlstra671dec52009-04-06 11:45:02 +02001726static void perf_pending_queue(struct perf_pending_entry *entry,
1727 void (*func)(struct perf_pending_entry *))
Peter Zijlstra925d5192009-03-30 19:07:02 +02001728{
Peter Zijlstra671dec52009-04-06 11:45:02 +02001729 struct perf_pending_entry **head;
Peter Zijlstra925d5192009-03-30 19:07:02 +02001730
Peter Zijlstra671dec52009-04-06 11:45:02 +02001731 if (cmpxchg(&entry->next, NULL, PENDING_TAIL) != NULL)
Peter Zijlstra925d5192009-03-30 19:07:02 +02001732 return;
1733
Peter Zijlstra671dec52009-04-06 11:45:02 +02001734 entry->func = func;
1735
1736 head = &get_cpu_var(perf_pending_head);
Peter Zijlstra925d5192009-03-30 19:07:02 +02001737
1738 do {
Peter Zijlstra671dec52009-04-06 11:45:02 +02001739 entry->next = *head;
1740 } while (cmpxchg(head, entry->next, entry) != entry->next);
Peter Zijlstra925d5192009-03-30 19:07:02 +02001741
1742 set_perf_counter_pending();
1743
Peter Zijlstra671dec52009-04-06 11:45:02 +02001744 put_cpu_var(perf_pending_head);
Peter Zijlstra925d5192009-03-30 19:07:02 +02001745}
1746
1747static int __perf_pending_run(void)
1748{
Peter Zijlstra671dec52009-04-06 11:45:02 +02001749 struct perf_pending_entry *list;
Peter Zijlstra925d5192009-03-30 19:07:02 +02001750 int nr = 0;
1751
Peter Zijlstra671dec52009-04-06 11:45:02 +02001752 list = xchg(&__get_cpu_var(perf_pending_head), PENDING_TAIL);
Peter Zijlstra925d5192009-03-30 19:07:02 +02001753 while (list != PENDING_TAIL) {
Peter Zijlstra671dec52009-04-06 11:45:02 +02001754 void (*func)(struct perf_pending_entry *);
1755 struct perf_pending_entry *entry = list;
Peter Zijlstra925d5192009-03-30 19:07:02 +02001756
1757 list = list->next;
1758
Peter Zijlstra671dec52009-04-06 11:45:02 +02001759 func = entry->func;
1760 entry->next = NULL;
Peter Zijlstra925d5192009-03-30 19:07:02 +02001761 /*
1762 * Ensure we observe the unqueue before we issue the wakeup,
1763 * so that we won't be waiting forever.
1764 * -- see perf_not_pending().
1765 */
1766 smp_wmb();
1767
Peter Zijlstra671dec52009-04-06 11:45:02 +02001768 func(entry);
Peter Zijlstra925d5192009-03-30 19:07:02 +02001769 nr++;
1770 }
1771
1772 return nr;
1773}
1774
1775static inline int perf_not_pending(struct perf_counter *counter)
1776{
1777 /*
1778 * If we flush on whatever cpu we run, there is a chance we don't
1779 * need to wait.
1780 */
1781 get_cpu();
1782 __perf_pending_run();
1783 put_cpu();
1784
1785 /*
1786 * Ensure we see the proper queue state before going to sleep
1787 * so that we do not miss the wakeup. -- see perf_pending_handle()
1788 */
1789 smp_rmb();
Peter Zijlstra671dec52009-04-06 11:45:02 +02001790 return counter->pending.next == NULL;
Peter Zijlstra925d5192009-03-30 19:07:02 +02001791}
1792
1793static void perf_pending_sync(struct perf_counter *counter)
1794{
1795 wait_event(counter->waitq, perf_not_pending(counter));
1796}
1797
1798void perf_counter_do_pending(void)
1799{
1800 __perf_pending_run();
1801}
1802
1803/*
Peter Zijlstra394ee072009-03-30 19:07:14 +02001804 * Callchain support -- arch specific
1805 */
1806
Peter Zijlstra9c03d882009-04-06 11:45:00 +02001807__weak struct perf_callchain_entry *perf_callchain(struct pt_regs *regs)
Peter Zijlstra394ee072009-03-30 19:07:14 +02001808{
1809 return NULL;
1810}
1811
1812/*
Peter Zijlstra0322cd62009-03-19 20:26:19 +01001813 * Output
1814 */
1815
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01001816struct perf_output_handle {
1817 struct perf_counter *counter;
1818 struct perf_mmap_data *data;
1819 unsigned int offset;
Peter Zijlstra63e35b22009-03-25 12:30:24 +01001820 unsigned int head;
Peter Zijlstra78d613e2009-03-30 19:07:11 +02001821 int nmi;
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02001822 int overflow;
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001823 int locked;
1824 unsigned long flags;
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01001825};
1826
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001827static void perf_output_wakeup(struct perf_output_handle *handle)
Peter Zijlstra78d613e2009-03-30 19:07:11 +02001828{
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001829 atomic_set(&handle->data->poll, POLL_IN);
1830
Peter Zijlstra671dec52009-04-06 11:45:02 +02001831 if (handle->nmi) {
Peter Zijlstra79f14642009-04-06 11:45:07 +02001832 handle->counter->pending_wakeup = 1;
Peter Zijlstra671dec52009-04-06 11:45:02 +02001833 perf_pending_queue(&handle->counter->pending,
Peter Zijlstra79f14642009-04-06 11:45:07 +02001834 perf_pending_counter);
Peter Zijlstra671dec52009-04-06 11:45:02 +02001835 } else
Peter Zijlstra78d613e2009-03-30 19:07:11 +02001836 perf_counter_wakeup(handle->counter);
1837}
1838
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001839/*
1840 * Curious locking construct.
1841 *
1842 * We need to ensure a later event doesn't publish a head when a former
1843 * event isn't done writing. However since we need to deal with NMIs we
1844 * cannot fully serialize things.
1845 *
1846 * What we do is serialize between CPUs so we only have to deal with NMI
1847 * nesting on a single CPU.
1848 *
1849 * We only publish the head (and generate a wakeup) when the outer-most
1850 * event completes.
1851 */
1852static void perf_output_lock(struct perf_output_handle *handle)
1853{
1854 struct perf_mmap_data *data = handle->data;
1855 int cpu;
1856
1857 handle->locked = 0;
1858
1859 local_irq_save(handle->flags);
1860 cpu = smp_processor_id();
1861
1862 if (in_nmi() && atomic_read(&data->lock) == cpu)
1863 return;
1864
Peter Zijlstra22c15582009-05-05 17:50:25 +02001865 while (atomic_cmpxchg(&data->lock, -1, cpu) != -1)
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001866 cpu_relax();
1867
1868 handle->locked = 1;
1869}
1870
1871static void perf_output_unlock(struct perf_output_handle *handle)
1872{
1873 struct perf_mmap_data *data = handle->data;
1874 int head, cpu;
1875
Peter Zijlstrac66de4a2009-05-05 17:50:22 +02001876 data->done_head = data->head;
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001877
1878 if (!handle->locked)
1879 goto out;
1880
1881again:
1882 /*
1883 * The xchg implies a full barrier that ensures all writes are done
1884 * before we publish the new head, matched by a rmb() in userspace when
1885 * reading this position.
1886 */
Peter Zijlstrac66de4a2009-05-05 17:50:22 +02001887 while ((head = atomic_xchg(&data->done_head, 0)))
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001888 data->user_page->data_head = head;
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001889
1890 /*
Peter Zijlstrac66de4a2009-05-05 17:50:22 +02001891 * NMI can happen here, which means we can miss a done_head update.
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001892 */
1893
Peter Zijlstra22c15582009-05-05 17:50:25 +02001894 cpu = atomic_xchg(&data->lock, -1);
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001895 WARN_ON_ONCE(cpu != smp_processor_id());
1896
1897 /*
1898 * Therefore we have to validate we did not indeed do so.
1899 */
Peter Zijlstrac66de4a2009-05-05 17:50:22 +02001900 if (unlikely(atomic_read(&data->done_head))) {
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001901 /*
1902 * Since we had it locked, we can lock it again.
1903 */
Peter Zijlstra22c15582009-05-05 17:50:25 +02001904 while (atomic_cmpxchg(&data->lock, -1, cpu) != -1)
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001905 cpu_relax();
1906
1907 goto again;
1908 }
1909
Peter Zijlstrac66de4a2009-05-05 17:50:22 +02001910 if (atomic_xchg(&data->wakeup, 0))
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001911 perf_output_wakeup(handle);
1912out:
1913 local_irq_restore(handle->flags);
1914}
1915
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01001916static int perf_output_begin(struct perf_output_handle *handle,
Peter Zijlstra78d613e2009-03-30 19:07:11 +02001917 struct perf_counter *counter, unsigned int size,
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02001918 int nmi, int overflow)
Peter Zijlstra0322cd62009-03-19 20:26:19 +01001919{
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001920 struct perf_mmap_data *data;
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01001921 unsigned int offset, head;
Peter Zijlstra0322cd62009-03-19 20:26:19 +01001922
Peter Zijlstra2023b352009-05-05 17:50:26 +02001923 /*
1924 * For inherited counters we send all the output towards the parent.
1925 */
1926 if (counter->parent)
1927 counter = counter->parent;
1928
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001929 rcu_read_lock();
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001930 data = rcu_dereference(counter->data);
1931 if (!data)
1932 goto out;
Peter Zijlstra0322cd62009-03-19 20:26:19 +01001933
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001934 handle->data = data;
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02001935 handle->counter = counter;
1936 handle->nmi = nmi;
1937 handle->overflow = overflow;
Peter Zijlstra78d613e2009-03-30 19:07:11 +02001938
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001939 if (!data->nr_pages)
Peter Zijlstra78d613e2009-03-30 19:07:11 +02001940 goto fail;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001941
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001942 perf_output_lock(handle);
1943
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001944 do {
1945 offset = head = atomic_read(&data->head);
Peter Zijlstrac7138f32009-03-24 13:18:16 +01001946 head += size;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001947 } while (atomic_cmpxchg(&data->head, offset, head) != offset);
1948
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01001949 handle->offset = offset;
Peter Zijlstra63e35b22009-03-25 12:30:24 +01001950 handle->head = head;
Peter Zijlstrac66de4a2009-05-05 17:50:22 +02001951
1952 if ((offset >> PAGE_SHIFT) != (head >> PAGE_SHIFT))
1953 atomic_set(&data->wakeup, 1);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001954
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01001955 return 0;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001956
Peter Zijlstra78d613e2009-03-30 19:07:11 +02001957fail:
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02001958 perf_output_wakeup(handle);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01001959out:
1960 rcu_read_unlock();
1961
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01001962 return -ENOSPC;
1963}
1964
1965static void perf_output_copy(struct perf_output_handle *handle,
1966 void *buf, unsigned int len)
1967{
1968 unsigned int pages_mask;
1969 unsigned int offset;
1970 unsigned int size;
1971 void **pages;
1972
1973 offset = handle->offset;
1974 pages_mask = handle->data->nr_pages - 1;
1975 pages = handle->data->data_pages;
1976
1977 do {
1978 unsigned int page_offset;
1979 int nr;
1980
1981 nr = (offset >> PAGE_SHIFT) & pages_mask;
1982 page_offset = offset & (PAGE_SIZE - 1);
1983 size = min_t(unsigned int, PAGE_SIZE - page_offset, len);
1984
1985 memcpy(pages[nr] + page_offset, buf, size);
1986
1987 len -= size;
1988 buf += size;
1989 offset += size;
1990 } while (len);
1991
1992 handle->offset = offset;
Peter Zijlstra63e35b22009-03-25 12:30:24 +01001993
Peter Zijlstra53020fe2009-05-13 21:26:19 +02001994 /*
1995 * Check we didn't copy past our reservation window, taking the
1996 * possible unsigned int wrap into account.
1997 */
1998 WARN_ON_ONCE(((int)(handle->head - handle->offset)) < 0);
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01001999}
2000
Peter Zijlstra5c148192009-03-25 12:30:23 +01002001#define perf_output_put(handle, x) \
2002 perf_output_copy((handle), &(x), sizeof(x))
2003
Peter Zijlstra78d613e2009-03-30 19:07:11 +02002004static void perf_output_end(struct perf_output_handle *handle)
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002005{
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002006 struct perf_counter *counter = handle->counter;
2007 struct perf_mmap_data *data = handle->data;
2008
2009 int wakeup_events = counter->hw_event.wakeup_events;
Peter Zijlstrac4578102009-04-02 11:12:01 +02002010
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02002011 if (handle->overflow && wakeup_events) {
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002012 int events = atomic_inc_return(&data->events);
Peter Zijlstrac4578102009-04-02 11:12:01 +02002013 if (events >= wakeup_events) {
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002014 atomic_sub(wakeup_events, &data->events);
Peter Zijlstrac66de4a2009-05-05 17:50:22 +02002015 atomic_set(&data->wakeup, 1);
Peter Zijlstrac4578102009-04-02 11:12:01 +02002016 }
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +02002017 }
2018
2019 perf_output_unlock(handle);
Peter Zijlstrab9cacc72009-03-25 12:30:22 +01002020 rcu_read_unlock();
2021}
2022
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002023static void perf_counter_output(struct perf_counter *counter,
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002024 int nmi, struct pt_regs *regs, u64 addr)
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002025{
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002026 int ret;
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002027 u64 record_type = counter->hw_event.record_type;
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002028 struct perf_output_handle handle;
2029 struct perf_event_header header;
2030 u64 ip;
Peter Zijlstra5c148192009-03-25 12:30:23 +01002031 struct {
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01002032 u32 pid, tid;
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002033 } tid_entry;
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002034 struct {
2035 u64 event;
2036 u64 counter;
2037 } group_entry;
Peter Zijlstra394ee072009-03-30 19:07:14 +02002038 struct perf_callchain_entry *callchain = NULL;
2039 int callchain_size = 0;
Peter Zijlstra339f7c92009-04-06 11:45:06 +02002040 u64 time;
Peter Zijlstraf370e1e2009-05-08 18:52:24 +02002041 struct {
2042 u32 cpu, reserved;
2043 } cpu_entry;
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002044
Peter Zijlstra6b6e5482009-04-08 15:01:27 +02002045 header.type = 0;
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002046 header.size = sizeof(header);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002047
Peter Zijlstra6b6e5482009-04-08 15:01:27 +02002048 header.misc = PERF_EVENT_MISC_OVERFLOW;
Paul Mackerras9d23a902009-05-14 21:48:08 +10002049 header.misc |= perf_misc_flags(regs);
Peter Zijlstra6fab0192009-04-08 15:01:26 +02002050
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002051 if (record_type & PERF_RECORD_IP) {
Paul Mackerras9d23a902009-05-14 21:48:08 +10002052 ip = perf_instruction_pointer(regs);
Peter Zijlstra6b6e5482009-04-08 15:01:27 +02002053 header.type |= PERF_RECORD_IP;
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002054 header.size += sizeof(ip);
2055 }
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01002056
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002057 if (record_type & PERF_RECORD_TID) {
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01002058 /* namespace issues */
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002059 tid_entry.pid = current->group_leader->pid;
2060 tid_entry.tid = current->pid;
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01002061
Peter Zijlstra6b6e5482009-04-08 15:01:27 +02002062 header.type |= PERF_RECORD_TID;
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002063 header.size += sizeof(tid_entry);
2064 }
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01002065
Peter Zijlstra4d855452009-04-08 15:01:32 +02002066 if (record_type & PERF_RECORD_TIME) {
2067 /*
2068 * Maybe do better on x86 and provide cpu_clock_nmi()
2069 */
2070 time = sched_clock();
2071
2072 header.type |= PERF_RECORD_TIME;
2073 header.size += sizeof(u64);
2074 }
2075
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002076 if (record_type & PERF_RECORD_ADDR) {
2077 header.type |= PERF_RECORD_ADDR;
2078 header.size += sizeof(u64);
2079 }
2080
Peter Zijlstraa85f61a2009-05-08 18:52:23 +02002081 if (record_type & PERF_RECORD_CONFIG) {
2082 header.type |= PERF_RECORD_CONFIG;
2083 header.size += sizeof(u64);
2084 }
2085
Peter Zijlstraf370e1e2009-05-08 18:52:24 +02002086 if (record_type & PERF_RECORD_CPU) {
2087 header.type |= PERF_RECORD_CPU;
2088 header.size += sizeof(cpu_entry);
2089
2090 cpu_entry.cpu = raw_smp_processor_id();
2091 }
2092
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002093 if (record_type & PERF_RECORD_GROUP) {
Peter Zijlstra6b6e5482009-04-08 15:01:27 +02002094 header.type |= PERF_RECORD_GROUP;
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002095 header.size += sizeof(u64) +
2096 counter->nr_siblings * sizeof(group_entry);
2097 }
2098
2099 if (record_type & PERF_RECORD_CALLCHAIN) {
Peter Zijlstra394ee072009-03-30 19:07:14 +02002100 callchain = perf_callchain(regs);
2101
2102 if (callchain) {
Peter Zijlstra9c03d882009-04-06 11:45:00 +02002103 callchain_size = (1 + callchain->nr) * sizeof(u64);
Peter Zijlstra394ee072009-03-30 19:07:14 +02002104
Peter Zijlstra6b6e5482009-04-08 15:01:27 +02002105 header.type |= PERF_RECORD_CALLCHAIN;
Peter Zijlstra394ee072009-03-30 19:07:14 +02002106 header.size += callchain_size;
2107 }
2108 }
2109
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02002110 ret = perf_output_begin(&handle, counter, header.size, nmi, 1);
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002111 if (ret)
2112 return;
Peter Zijlstraea5d20c2009-03-25 12:30:25 +01002113
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002114 perf_output_put(&handle, header);
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002115
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002116 if (record_type & PERF_RECORD_IP)
2117 perf_output_put(&handle, ip);
2118
2119 if (record_type & PERF_RECORD_TID)
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002120 perf_output_put(&handle, tid_entry);
2121
Peter Zijlstra4d855452009-04-08 15:01:32 +02002122 if (record_type & PERF_RECORD_TIME)
2123 perf_output_put(&handle, time);
2124
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002125 if (record_type & PERF_RECORD_ADDR)
2126 perf_output_put(&handle, addr);
2127
Peter Zijlstraa85f61a2009-05-08 18:52:23 +02002128 if (record_type & PERF_RECORD_CONFIG)
2129 perf_output_put(&handle, counter->hw_event.config);
2130
Peter Zijlstraf370e1e2009-05-08 18:52:24 +02002131 if (record_type & PERF_RECORD_CPU)
2132 perf_output_put(&handle, cpu_entry);
2133
Peter Zijlstra2023b352009-05-05 17:50:26 +02002134 /*
2135 * XXX PERF_RECORD_GROUP vs inherited counters seems difficult.
2136 */
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002137 if (record_type & PERF_RECORD_GROUP) {
2138 struct perf_counter *leader, *sub;
2139 u64 nr = counter->nr_siblings;
2140
2141 perf_output_put(&handle, nr);
2142
2143 leader = counter->group_leader;
2144 list_for_each_entry(sub, &leader->sibling_list, list_entry) {
2145 if (sub != counter)
Robert Richter4aeb0b42009-04-29 12:47:03 +02002146 sub->pmu->read(sub);
Peter Zijlstra8a057d82009-04-02 11:11:59 +02002147
2148 group_entry.event = sub->hw_event.config;
2149 group_entry.counter = atomic64_read(&sub->count);
2150
2151 perf_output_put(&handle, group_entry);
2152 }
2153 }
2154
Peter Zijlstra394ee072009-03-30 19:07:14 +02002155 if (callchain)
2156 perf_output_copy(&handle, callchain, callchain_size);
2157
Peter Zijlstra5ed00412009-03-30 19:07:12 +02002158 perf_output_end(&handle);
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002159}
2160
Peter Zijlstra0322cd62009-03-19 20:26:19 +01002161/*
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02002162 * comm tracking
2163 */
2164
2165struct perf_comm_event {
2166 struct task_struct *task;
2167 char *comm;
2168 int comm_size;
2169
2170 struct {
2171 struct perf_event_header header;
2172
2173 u32 pid;
2174 u32 tid;
2175 } event;
2176};
2177
2178static void perf_counter_comm_output(struct perf_counter *counter,
2179 struct perf_comm_event *comm_event)
2180{
2181 struct perf_output_handle handle;
2182 int size = comm_event->event.header.size;
2183 int ret = perf_output_begin(&handle, counter, size, 0, 0);
2184
2185 if (ret)
2186 return;
2187
2188 perf_output_put(&handle, comm_event->event);
2189 perf_output_copy(&handle, comm_event->comm,
2190 comm_event->comm_size);
2191 perf_output_end(&handle);
2192}
2193
2194static int perf_counter_comm_match(struct perf_counter *counter,
2195 struct perf_comm_event *comm_event)
2196{
2197 if (counter->hw_event.comm &&
2198 comm_event->event.header.type == PERF_EVENT_COMM)
2199 return 1;
2200
2201 return 0;
2202}
2203
2204static void perf_counter_comm_ctx(struct perf_counter_context *ctx,
2205 struct perf_comm_event *comm_event)
2206{
2207 struct perf_counter *counter;
2208
2209 if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
2210 return;
2211
2212 rcu_read_lock();
2213 list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
2214 if (perf_counter_comm_match(counter, comm_event))
2215 perf_counter_comm_output(counter, comm_event);
2216 }
2217 rcu_read_unlock();
2218}
2219
2220static void perf_counter_comm_event(struct perf_comm_event *comm_event)
2221{
2222 struct perf_cpu_context *cpuctx;
2223 unsigned int size;
2224 char *comm = comm_event->task->comm;
2225
Ingo Molnar888fcee2009-04-09 09:48:22 +02002226 size = ALIGN(strlen(comm)+1, sizeof(u64));
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02002227
2228 comm_event->comm = comm;
2229 comm_event->comm_size = size;
2230
2231 comm_event->event.header.size = sizeof(comm_event->event) + size;
2232
2233 cpuctx = &get_cpu_var(perf_cpu_context);
2234 perf_counter_comm_ctx(&cpuctx->ctx, comm_event);
2235 put_cpu_var(perf_cpu_context);
2236
2237 perf_counter_comm_ctx(&current->perf_counter_ctx, comm_event);
2238}
2239
2240void perf_counter_comm(struct task_struct *task)
2241{
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02002242 struct perf_comm_event comm_event;
2243
2244 if (!atomic_read(&nr_comm_tracking))
2245 return;
2246
2247 comm_event = (struct perf_comm_event){
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +02002248 .task = task,
2249 .event = {
2250 .header = { .type = PERF_EVENT_COMM, },
2251 .pid = task->group_leader->pid,
2252 .tid = task->pid,
2253 },
2254 };
2255
2256 perf_counter_comm_event(&comm_event);
2257}
2258
2259/*
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002260 * mmap tracking
2261 */
2262
2263struct perf_mmap_event {
2264 struct file *file;
2265 char *file_name;
2266 int file_size;
2267
2268 struct {
2269 struct perf_event_header header;
2270
2271 u32 pid;
2272 u32 tid;
2273 u64 start;
2274 u64 len;
2275 u64 pgoff;
2276 } event;
2277};
2278
2279static void perf_counter_mmap_output(struct perf_counter *counter,
2280 struct perf_mmap_event *mmap_event)
2281{
2282 struct perf_output_handle handle;
2283 int size = mmap_event->event.header.size;
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02002284 int ret = perf_output_begin(&handle, counter, size, 0, 0);
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002285
2286 if (ret)
2287 return;
2288
2289 perf_output_put(&handle, mmap_event->event);
2290 perf_output_copy(&handle, mmap_event->file_name,
2291 mmap_event->file_size);
Peter Zijlstra78d613e2009-03-30 19:07:11 +02002292 perf_output_end(&handle);
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002293}
2294
2295static int perf_counter_mmap_match(struct perf_counter *counter,
2296 struct perf_mmap_event *mmap_event)
2297{
2298 if (counter->hw_event.mmap &&
2299 mmap_event->event.header.type == PERF_EVENT_MMAP)
2300 return 1;
2301
2302 if (counter->hw_event.munmap &&
2303 mmap_event->event.header.type == PERF_EVENT_MUNMAP)
2304 return 1;
2305
2306 return 0;
2307}
2308
2309static void perf_counter_mmap_ctx(struct perf_counter_context *ctx,
2310 struct perf_mmap_event *mmap_event)
2311{
2312 struct perf_counter *counter;
2313
2314 if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
2315 return;
2316
2317 rcu_read_lock();
2318 list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
2319 if (perf_counter_mmap_match(counter, mmap_event))
2320 perf_counter_mmap_output(counter, mmap_event);
2321 }
2322 rcu_read_unlock();
2323}
2324
2325static void perf_counter_mmap_event(struct perf_mmap_event *mmap_event)
2326{
2327 struct perf_cpu_context *cpuctx;
2328 struct file *file = mmap_event->file;
2329 unsigned int size;
2330 char tmp[16];
2331 char *buf = NULL;
2332 char *name;
2333
2334 if (file) {
2335 buf = kzalloc(PATH_MAX, GFP_KERNEL);
2336 if (!buf) {
2337 name = strncpy(tmp, "//enomem", sizeof(tmp));
2338 goto got_name;
2339 }
Peter Zijlstrad3d21c42009-04-09 10:53:46 +02002340 name = d_path(&file->f_path, buf, PATH_MAX);
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002341 if (IS_ERR(name)) {
2342 name = strncpy(tmp, "//toolong", sizeof(tmp));
2343 goto got_name;
2344 }
2345 } else {
2346 name = strncpy(tmp, "//anon", sizeof(tmp));
2347 goto got_name;
2348 }
2349
2350got_name:
Ingo Molnar888fcee2009-04-09 09:48:22 +02002351 size = ALIGN(strlen(name)+1, sizeof(u64));
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002352
2353 mmap_event->file_name = name;
2354 mmap_event->file_size = size;
2355
2356 mmap_event->event.header.size = sizeof(mmap_event->event) + size;
2357
2358 cpuctx = &get_cpu_var(perf_cpu_context);
2359 perf_counter_mmap_ctx(&cpuctx->ctx, mmap_event);
2360 put_cpu_var(perf_cpu_context);
2361
2362 perf_counter_mmap_ctx(&current->perf_counter_ctx, mmap_event);
2363
2364 kfree(buf);
2365}
2366
2367void perf_counter_mmap(unsigned long addr, unsigned long len,
2368 unsigned long pgoff, struct file *file)
2369{
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02002370 struct perf_mmap_event mmap_event;
2371
2372 if (!atomic_read(&nr_mmap_tracking))
2373 return;
2374
2375 mmap_event = (struct perf_mmap_event){
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002376 .file = file,
2377 .event = {
2378 .header = { .type = PERF_EVENT_MMAP, },
2379 .pid = current->group_leader->pid,
2380 .tid = current->pid,
2381 .start = addr,
2382 .len = len,
2383 .pgoff = pgoff,
2384 },
2385 };
2386
2387 perf_counter_mmap_event(&mmap_event);
2388}
2389
2390void perf_counter_munmap(unsigned long addr, unsigned long len,
2391 unsigned long pgoff, struct file *file)
2392{
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02002393 struct perf_mmap_event mmap_event;
2394
2395 if (!atomic_read(&nr_munmap_tracking))
2396 return;
2397
2398 mmap_event = (struct perf_mmap_event){
Peter Zijlstra0a4a9392009-03-30 19:07:05 +02002399 .file = file,
2400 .event = {
2401 .header = { .type = PERF_EVENT_MUNMAP, },
2402 .pid = current->group_leader->pid,
2403 .tid = current->pid,
2404 .start = addr,
2405 .len = len,
2406 .pgoff = pgoff,
2407 },
2408 };
2409
2410 perf_counter_mmap_event(&mmap_event);
2411}
2412
2413/*
Peter Zijlstra26b119b2009-05-20 12:21:20 +02002414 *
2415 */
2416
2417static void perf_log_period(struct perf_counter *counter, u64 period)
2418{
2419 struct perf_output_handle handle;
2420 int ret;
2421
2422 struct {
2423 struct perf_event_header header;
2424 u64 time;
2425 u64 period;
2426 } freq_event = {
2427 .header = {
2428 .type = PERF_EVENT_PERIOD,
2429 .misc = 0,
2430 .size = sizeof(freq_event),
2431 },
2432 .time = sched_clock(),
2433 .period = period,
2434 };
2435
2436 if (counter->hw.irq_period == period)
2437 return;
2438
2439 ret = perf_output_begin(&handle, counter, sizeof(freq_event), 0, 0);
2440 if (ret)
2441 return;
2442
2443 perf_output_put(&handle, freq_event);
2444 perf_output_end(&handle);
2445}
2446
2447/*
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002448 * Generic counter overflow handling.
2449 */
2450
2451int perf_counter_overflow(struct perf_counter *counter,
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002452 int nmi, struct pt_regs *regs, u64 addr)
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002453{
Peter Zijlstra79f14642009-04-06 11:45:07 +02002454 int events = atomic_read(&counter->event_limit);
2455 int ret = 0;
2456
Peter Zijlstra60db5e02009-05-15 15:19:28 +02002457 counter->hw.interrupts++;
2458
Peter Zijlstra2023b352009-05-05 17:50:26 +02002459 /*
2460 * XXX event_limit might not quite work as expected on inherited
2461 * counters
2462 */
2463
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02002464 counter->pending_kill = POLL_IN;
Peter Zijlstra79f14642009-04-06 11:45:07 +02002465 if (events && atomic_dec_and_test(&counter->event_limit)) {
2466 ret = 1;
Peter Zijlstra4c9e2542009-04-06 11:45:09 +02002467 counter->pending_kill = POLL_HUP;
Peter Zijlstra79f14642009-04-06 11:45:07 +02002468 if (nmi) {
2469 counter->pending_disable = 1;
2470 perf_pending_queue(&counter->pending,
2471 perf_pending_counter);
2472 } else
2473 perf_counter_disable(counter);
2474 }
2475
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002476 perf_counter_output(counter, nmi, regs, addr);
Peter Zijlstra79f14642009-04-06 11:45:07 +02002477 return ret;
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002478}
2479
2480/*
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002481 * Generic software counter infrastructure
2482 */
2483
2484static void perf_swcounter_update(struct perf_counter *counter)
2485{
2486 struct hw_perf_counter *hwc = &counter->hw;
2487 u64 prev, now;
2488 s64 delta;
2489
2490again:
2491 prev = atomic64_read(&hwc->prev_count);
2492 now = atomic64_read(&hwc->count);
2493 if (atomic64_cmpxchg(&hwc->prev_count, prev, now) != prev)
2494 goto again;
2495
2496 delta = now - prev;
2497
2498 atomic64_add(delta, &counter->count);
2499 atomic64_sub(delta, &hwc->period_left);
2500}
2501
2502static void perf_swcounter_set_period(struct perf_counter *counter)
2503{
2504 struct hw_perf_counter *hwc = &counter->hw;
2505 s64 left = atomic64_read(&hwc->period_left);
2506 s64 period = hwc->irq_period;
2507
2508 if (unlikely(left <= -period)) {
2509 left = period;
2510 atomic64_set(&hwc->period_left, left);
2511 }
2512
2513 if (unlikely(left <= 0)) {
2514 left += period;
2515 atomic64_add(period, &hwc->period_left);
2516 }
2517
2518 atomic64_set(&hwc->prev_count, -left);
2519 atomic64_set(&hwc->count, -left);
2520}
2521
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002522static enum hrtimer_restart perf_swcounter_hrtimer(struct hrtimer *hrtimer)
2523{
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002524 enum hrtimer_restart ret = HRTIMER_RESTART;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002525 struct perf_counter *counter;
2526 struct pt_regs *regs;
Peter Zijlstra60db5e02009-05-15 15:19:28 +02002527 u64 period;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002528
2529 counter = container_of(hrtimer, struct perf_counter, hw.hrtimer);
Robert Richter4aeb0b42009-04-29 12:47:03 +02002530 counter->pmu->read(counter);
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002531
2532 regs = get_irq_regs();
2533 /*
2534 * In case we exclude kernel IPs or are somehow not in interrupt
2535 * context, provide the next best thing, the user IP.
2536 */
2537 if ((counter->hw_event.exclude_kernel || !regs) &&
2538 !counter->hw_event.exclude_user)
2539 regs = task_pt_regs(current);
2540
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002541 if (regs) {
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002542 if (perf_counter_overflow(counter, 0, regs, 0))
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002543 ret = HRTIMER_NORESTART;
2544 }
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002545
Peter Zijlstra60db5e02009-05-15 15:19:28 +02002546 period = max_t(u64, 10000, counter->hw.irq_period);
2547 hrtimer_forward_now(hrtimer, ns_to_ktime(period));
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002548
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002549 return ret;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002550}
2551
2552static void perf_swcounter_overflow(struct perf_counter *counter,
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002553 int nmi, struct pt_regs *regs, u64 addr)
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002554{
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002555 perf_swcounter_update(counter);
2556 perf_swcounter_set_period(counter);
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002557 if (perf_counter_overflow(counter, nmi, regs, addr))
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +02002558 /* soft-disable the counter */
2559 ;
2560
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002561}
2562
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002563static int perf_swcounter_match(struct perf_counter *counter,
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002564 enum perf_event_types type,
2565 u32 event, struct pt_regs *regs)
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002566{
2567 if (counter->state != PERF_COUNTER_STATE_ACTIVE)
2568 return 0;
2569
Peter Zijlstraf4a2deb2009-03-23 18:22:06 +01002570 if (perf_event_raw(&counter->hw_event))
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002571 return 0;
2572
Peter Zijlstraf4a2deb2009-03-23 18:22:06 +01002573 if (perf_event_type(&counter->hw_event) != type)
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002574 return 0;
2575
Peter Zijlstraf4a2deb2009-03-23 18:22:06 +01002576 if (perf_event_id(&counter->hw_event) != event)
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002577 return 0;
2578
2579 if (counter->hw_event.exclude_user && user_mode(regs))
2580 return 0;
2581
2582 if (counter->hw_event.exclude_kernel && !user_mode(regs))
2583 return 0;
2584
2585 return 1;
2586}
2587
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002588static void perf_swcounter_add(struct perf_counter *counter, u64 nr,
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002589 int nmi, struct pt_regs *regs, u64 addr)
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002590{
2591 int neg = atomic64_add_negative(nr, &counter->hw.count);
2592 if (counter->hw.irq_period && !neg)
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002593 perf_swcounter_overflow(counter, nmi, regs, addr);
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002594}
2595
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002596static void perf_swcounter_ctx_event(struct perf_counter_context *ctx,
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002597 enum perf_event_types type, u32 event,
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002598 u64 nr, int nmi, struct pt_regs *regs,
2599 u64 addr)
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002600{
2601 struct perf_counter *counter;
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002602
Peter Zijlstra01ef09d2009-03-19 20:26:11 +01002603 if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002604 return;
2605
Peter Zijlstra592903c2009-03-13 12:21:36 +01002606 rcu_read_lock();
2607 list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002608 if (perf_swcounter_match(counter, type, event, regs))
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002609 perf_swcounter_add(counter, nr, nmi, regs, addr);
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002610 }
Peter Zijlstra592903c2009-03-13 12:21:36 +01002611 rcu_read_unlock();
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002612}
2613
Peter Zijlstra96f6d442009-03-23 18:22:07 +01002614static int *perf_swcounter_recursion_context(struct perf_cpu_context *cpuctx)
2615{
2616 if (in_nmi())
2617 return &cpuctx->recursion[3];
2618
2619 if (in_irq())
2620 return &cpuctx->recursion[2];
2621
2622 if (in_softirq())
2623 return &cpuctx->recursion[1];
2624
2625 return &cpuctx->recursion[0];
2626}
2627
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002628static void __perf_swcounter_event(enum perf_event_types type, u32 event,
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002629 u64 nr, int nmi, struct pt_regs *regs,
2630 u64 addr)
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002631{
2632 struct perf_cpu_context *cpuctx = &get_cpu_var(perf_cpu_context);
Peter Zijlstra96f6d442009-03-23 18:22:07 +01002633 int *recursion = perf_swcounter_recursion_context(cpuctx);
2634
2635 if (*recursion)
2636 goto out;
2637
2638 (*recursion)++;
2639 barrier();
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002640
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002641 perf_swcounter_ctx_event(&cpuctx->ctx, type, event,
2642 nr, nmi, regs, addr);
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002643 if (cpuctx->task_ctx) {
2644 perf_swcounter_ctx_event(cpuctx->task_ctx, type, event,
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002645 nr, nmi, regs, addr);
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002646 }
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002647
Peter Zijlstra96f6d442009-03-23 18:22:07 +01002648 barrier();
2649 (*recursion)--;
2650
2651out:
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002652 put_cpu_var(perf_cpu_context);
2653}
2654
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002655void
2656perf_swcounter_event(u32 event, u64 nr, int nmi, struct pt_regs *regs, u64 addr)
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002657{
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002658 __perf_swcounter_event(PERF_TYPE_SOFTWARE, event, nr, nmi, regs, addr);
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002659}
2660
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002661static void perf_swcounter_read(struct perf_counter *counter)
2662{
2663 perf_swcounter_update(counter);
2664}
2665
2666static int perf_swcounter_enable(struct perf_counter *counter)
2667{
2668 perf_swcounter_set_period(counter);
2669 return 0;
2670}
2671
2672static void perf_swcounter_disable(struct perf_counter *counter)
2673{
2674 perf_swcounter_update(counter);
2675}
2676
Robert Richter4aeb0b42009-04-29 12:47:03 +02002677static const struct pmu perf_ops_generic = {
Peter Zijlstraac17dc82009-03-13 12:21:34 +01002678 .enable = perf_swcounter_enable,
2679 .disable = perf_swcounter_disable,
2680 .read = perf_swcounter_read,
2681};
2682
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002683/*
2684 * Software counter: cpu wall time clock
2685 */
2686
Paul Mackerras9abf8a02009-01-09 16:26:43 +11002687static void cpu_clock_perf_counter_update(struct perf_counter *counter)
2688{
2689 int cpu = raw_smp_processor_id();
2690 s64 prev;
2691 u64 now;
2692
2693 now = cpu_clock(cpu);
2694 prev = atomic64_read(&counter->hw.prev_count);
2695 atomic64_set(&counter->hw.prev_count, now);
2696 atomic64_add(now - prev, &counter->count);
2697}
2698
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002699static int cpu_clock_perf_counter_enable(struct perf_counter *counter)
2700{
2701 struct hw_perf_counter *hwc = &counter->hw;
2702 int cpu = raw_smp_processor_id();
2703
2704 atomic64_set(&hwc->prev_count, cpu_clock(cpu));
Peter Zijlstra039fc912009-03-13 16:43:47 +01002705 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
2706 hwc->hrtimer.function = perf_swcounter_hrtimer;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002707 if (hwc->irq_period) {
Peter Zijlstra60db5e02009-05-15 15:19:28 +02002708 u64 period = max_t(u64, 10000, hwc->irq_period);
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002709 __hrtimer_start_range_ns(&hwc->hrtimer,
Peter Zijlstra60db5e02009-05-15 15:19:28 +02002710 ns_to_ktime(period), 0,
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002711 HRTIMER_MODE_REL, 0);
2712 }
2713
2714 return 0;
2715}
2716
Ingo Molnar5c92d122008-12-11 13:21:10 +01002717static void cpu_clock_perf_counter_disable(struct perf_counter *counter)
2718{
Peter Zijlstrab986d7e2009-05-20 12:21:21 +02002719 if (counter->hw.irq_period)
2720 hrtimer_cancel(&counter->hw.hrtimer);
Paul Mackerras9abf8a02009-01-09 16:26:43 +11002721 cpu_clock_perf_counter_update(counter);
Ingo Molnar5c92d122008-12-11 13:21:10 +01002722}
2723
2724static void cpu_clock_perf_counter_read(struct perf_counter *counter)
2725{
Paul Mackerras9abf8a02009-01-09 16:26:43 +11002726 cpu_clock_perf_counter_update(counter);
Ingo Molnar5c92d122008-12-11 13:21:10 +01002727}
2728
Robert Richter4aeb0b42009-04-29 12:47:03 +02002729static const struct pmu perf_ops_cpu_clock = {
Ingo Molnar76715812008-12-17 14:20:28 +01002730 .enable = cpu_clock_perf_counter_enable,
2731 .disable = cpu_clock_perf_counter_disable,
2732 .read = cpu_clock_perf_counter_read,
Ingo Molnar5c92d122008-12-11 13:21:10 +01002733};
2734
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01002735/*
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002736 * Software counter: task time clock
2737 */
2738
Peter Zijlstrae30e08f2009-04-08 15:01:25 +02002739static void task_clock_perf_counter_update(struct perf_counter *counter, u64 now)
Ingo Molnarbae43c92008-12-11 14:03:20 +01002740{
Peter Zijlstrae30e08f2009-04-08 15:01:25 +02002741 u64 prev;
Ingo Molnar8cb391e2008-12-14 12:22:31 +01002742 s64 delta;
Ingo Molnarbae43c92008-12-11 14:03:20 +01002743
Peter Zijlstraa39d6f22009-04-06 11:45:11 +02002744 prev = atomic64_xchg(&counter->hw.prev_count, now);
Ingo Molnar8cb391e2008-12-14 12:22:31 +01002745 delta = now - prev;
Ingo Molnar8cb391e2008-12-14 12:22:31 +01002746 atomic64_add(delta, &counter->count);
Ingo Molnarbae43c92008-12-11 14:03:20 +01002747}
2748
Ingo Molnar95cdd2e2008-12-21 13:50:42 +01002749static int task_clock_perf_counter_enable(struct perf_counter *counter)
Ingo Molnar8cb391e2008-12-14 12:22:31 +01002750{
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002751 struct hw_perf_counter *hwc = &counter->hw;
Peter Zijlstraa39d6f22009-04-06 11:45:11 +02002752 u64 now;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002753
Peter Zijlstraa39d6f22009-04-06 11:45:11 +02002754 now = counter->ctx->time;
2755
2756 atomic64_set(&hwc->prev_count, now);
Peter Zijlstra039fc912009-03-13 16:43:47 +01002757 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
2758 hwc->hrtimer.function = perf_swcounter_hrtimer;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002759 if (hwc->irq_period) {
Peter Zijlstra60db5e02009-05-15 15:19:28 +02002760 u64 period = max_t(u64, 10000, hwc->irq_period);
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002761 __hrtimer_start_range_ns(&hwc->hrtimer,
Peter Zijlstra60db5e02009-05-15 15:19:28 +02002762 ns_to_ktime(period), 0,
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002763 HRTIMER_MODE_REL, 0);
2764 }
Ingo Molnar95cdd2e2008-12-21 13:50:42 +01002765
2766 return 0;
Ingo Molnar8cb391e2008-12-14 12:22:31 +01002767}
2768
2769static void task_clock_perf_counter_disable(struct perf_counter *counter)
2770{
Peter Zijlstrab986d7e2009-05-20 12:21:21 +02002771 if (counter->hw.irq_period)
2772 hrtimer_cancel(&counter->hw.hrtimer);
Peter Zijlstrae30e08f2009-04-08 15:01:25 +02002773 task_clock_perf_counter_update(counter, counter->ctx->time);
2774
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002775}
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01002776
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002777static void task_clock_perf_counter_read(struct perf_counter *counter)
2778{
Peter Zijlstrae30e08f2009-04-08 15:01:25 +02002779 u64 time;
2780
2781 if (!in_nmi()) {
2782 update_context_time(counter->ctx);
2783 time = counter->ctx->time;
2784 } else {
2785 u64 now = perf_clock();
2786 u64 delta = now - counter->ctx->timestamp;
2787 time = counter->ctx->time + delta;
2788 }
2789
2790 task_clock_perf_counter_update(counter, time);
Ingo Molnarbae43c92008-12-11 14:03:20 +01002791}
2792
Robert Richter4aeb0b42009-04-29 12:47:03 +02002793static const struct pmu perf_ops_task_clock = {
Ingo Molnar76715812008-12-17 14:20:28 +01002794 .enable = task_clock_perf_counter_enable,
2795 .disable = task_clock_perf_counter_disable,
2796 .read = task_clock_perf_counter_read,
Ingo Molnarbae43c92008-12-11 14:03:20 +01002797};
2798
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002799/*
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002800 * Software counter: cpu migrations
2801 */
2802
Paul Mackerras23a185c2009-02-09 22:42:47 +11002803static inline u64 get_cpu_migrations(struct perf_counter *counter)
Ingo Molnar6c594c22008-12-14 12:34:15 +01002804{
Paul Mackerras23a185c2009-02-09 22:42:47 +11002805 struct task_struct *curr = counter->ctx->task;
2806
2807 if (curr)
2808 return curr->se.nr_migrations;
2809 return cpu_nr_migrations(smp_processor_id());
Ingo Molnar6c594c22008-12-14 12:34:15 +01002810}
2811
2812static void cpu_migrations_perf_counter_update(struct perf_counter *counter)
2813{
2814 u64 prev, now;
2815 s64 delta;
2816
2817 prev = atomic64_read(&counter->hw.prev_count);
Paul Mackerras23a185c2009-02-09 22:42:47 +11002818 now = get_cpu_migrations(counter);
Ingo Molnar6c594c22008-12-14 12:34:15 +01002819
2820 atomic64_set(&counter->hw.prev_count, now);
2821
2822 delta = now - prev;
Ingo Molnar6c594c22008-12-14 12:34:15 +01002823
2824 atomic64_add(delta, &counter->count);
2825}
2826
2827static void cpu_migrations_perf_counter_read(struct perf_counter *counter)
2828{
2829 cpu_migrations_perf_counter_update(counter);
2830}
2831
Ingo Molnar95cdd2e2008-12-21 13:50:42 +01002832static int cpu_migrations_perf_counter_enable(struct perf_counter *counter)
Ingo Molnar6c594c22008-12-14 12:34:15 +01002833{
Paul Mackerrasc07c99b2009-02-13 22:10:34 +11002834 if (counter->prev_state <= PERF_COUNTER_STATE_OFF)
2835 atomic64_set(&counter->hw.prev_count,
2836 get_cpu_migrations(counter));
Ingo Molnar95cdd2e2008-12-21 13:50:42 +01002837 return 0;
Ingo Molnar6c594c22008-12-14 12:34:15 +01002838}
2839
2840static void cpu_migrations_perf_counter_disable(struct perf_counter *counter)
2841{
2842 cpu_migrations_perf_counter_update(counter);
2843}
2844
Robert Richter4aeb0b42009-04-29 12:47:03 +02002845static const struct pmu perf_ops_cpu_migrations = {
Ingo Molnar76715812008-12-17 14:20:28 +01002846 .enable = cpu_migrations_perf_counter_enable,
2847 .disable = cpu_migrations_perf_counter_disable,
2848 .read = cpu_migrations_perf_counter_read,
Ingo Molnar6c594c22008-12-14 12:34:15 +01002849};
2850
Peter Zijlstrae077df42009-03-19 20:26:17 +01002851#ifdef CONFIG_EVENT_PROFILE
2852void perf_tpcounter_event(int event_id)
2853{
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002854 struct pt_regs *regs = get_irq_regs();
2855
2856 if (!regs)
2857 regs = task_pt_regs(current);
2858
Peter Zijlstra78f13e92009-04-08 15:01:33 +02002859 __perf_swcounter_event(PERF_TYPE_TRACEPOINT, event_id, 1, 1, regs, 0);
Peter Zijlstrae077df42009-03-19 20:26:17 +01002860}
Steven Whitehouseff7b1b42009-04-15 16:55:05 +01002861EXPORT_SYMBOL_GPL(perf_tpcounter_event);
Peter Zijlstrae077df42009-03-19 20:26:17 +01002862
2863extern int ftrace_profile_enable(int);
2864extern void ftrace_profile_disable(int);
2865
2866static void tp_perf_counter_destroy(struct perf_counter *counter)
2867{
Peter Zijlstraf4a2deb2009-03-23 18:22:06 +01002868 ftrace_profile_disable(perf_event_id(&counter->hw_event));
Peter Zijlstrae077df42009-03-19 20:26:17 +01002869}
2870
Robert Richter4aeb0b42009-04-29 12:47:03 +02002871static const struct pmu *tp_perf_counter_init(struct perf_counter *counter)
Peter Zijlstrae077df42009-03-19 20:26:17 +01002872{
Peter Zijlstraf4a2deb2009-03-23 18:22:06 +01002873 int event_id = perf_event_id(&counter->hw_event);
Peter Zijlstrae077df42009-03-19 20:26:17 +01002874 int ret;
2875
2876 ret = ftrace_profile_enable(event_id);
2877 if (ret)
2878 return NULL;
2879
2880 counter->destroy = tp_perf_counter_destroy;
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002881 counter->hw.irq_period = counter->hw_event.irq_period;
Peter Zijlstrae077df42009-03-19 20:26:17 +01002882
2883 return &perf_ops_generic;
2884}
2885#else
Robert Richter4aeb0b42009-04-29 12:47:03 +02002886static const struct pmu *tp_perf_counter_init(struct perf_counter *counter)
Peter Zijlstrae077df42009-03-19 20:26:17 +01002887{
2888 return NULL;
2889}
2890#endif
2891
Robert Richter4aeb0b42009-04-29 12:47:03 +02002892static const struct pmu *sw_perf_counter_init(struct perf_counter *counter)
Ingo Molnar5c92d122008-12-11 13:21:10 +01002893{
Robert Richter4aeb0b42009-04-29 12:47:03 +02002894 const struct pmu *pmu = NULL;
Ingo Molnar5c92d122008-12-11 13:21:10 +01002895
Paul Mackerras0475f9e2009-02-11 14:35:35 +11002896 /*
2897 * Software counters (currently) can't in general distinguish
2898 * between user, kernel and hypervisor events.
2899 * However, context switches and cpu migrations are considered
2900 * to be kernel events, and page faults are never hypervisor
2901 * events.
2902 */
Peter Zijlstraf4a2deb2009-03-23 18:22:06 +01002903 switch (perf_event_id(&counter->hw_event)) {
Ingo Molnar5c92d122008-12-11 13:21:10 +01002904 case PERF_COUNT_CPU_CLOCK:
Robert Richter4aeb0b42009-04-29 12:47:03 +02002905 pmu = &perf_ops_cpu_clock;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002906
Ingo Molnar5c92d122008-12-11 13:21:10 +01002907 break;
Ingo Molnarbae43c92008-12-11 14:03:20 +01002908 case PERF_COUNT_TASK_CLOCK:
Paul Mackerras23a185c2009-02-09 22:42:47 +11002909 /*
2910 * If the user instantiates this as a per-cpu counter,
2911 * use the cpu_clock counter instead.
2912 */
2913 if (counter->ctx->task)
Robert Richter4aeb0b42009-04-29 12:47:03 +02002914 pmu = &perf_ops_task_clock;
Paul Mackerras23a185c2009-02-09 22:42:47 +11002915 else
Robert Richter4aeb0b42009-04-29 12:47:03 +02002916 pmu = &perf_ops_cpu_clock;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01002917
Ingo Molnarbae43c92008-12-11 14:03:20 +01002918 break;
Ingo Molnare06c61a2008-12-14 14:44:31 +01002919 case PERF_COUNT_PAGE_FAULTS:
Peter Zijlstraac17dc82009-03-13 12:21:34 +01002920 case PERF_COUNT_PAGE_FAULTS_MIN:
2921 case PERF_COUNT_PAGE_FAULTS_MAJ:
Ingo Molnar5d6a27d2008-12-14 12:28:33 +01002922 case PERF_COUNT_CONTEXT_SWITCHES:
Robert Richter4aeb0b42009-04-29 12:47:03 +02002923 pmu = &perf_ops_generic;
Ingo Molnar5d6a27d2008-12-14 12:28:33 +01002924 break;
Ingo Molnar6c594c22008-12-14 12:34:15 +01002925 case PERF_COUNT_CPU_MIGRATIONS:
Paul Mackerras0475f9e2009-02-11 14:35:35 +11002926 if (!counter->hw_event.exclude_kernel)
Robert Richter4aeb0b42009-04-29 12:47:03 +02002927 pmu = &perf_ops_cpu_migrations;
Ingo Molnar6c594c22008-12-14 12:34:15 +01002928 break;
Ingo Molnar5c92d122008-12-11 13:21:10 +01002929 }
Peter Zijlstra15dbf272009-03-13 12:21:32 +01002930
Robert Richter4aeb0b42009-04-29 12:47:03 +02002931 return pmu;
Ingo Molnar5c92d122008-12-11 13:21:10 +01002932}
2933
Thomas Gleixner0793a612008-12-04 20:12:29 +01002934/*
2935 * Allocate and initialize a counter structure
2936 */
2937static struct perf_counter *
Ingo Molnar04289bb2008-12-11 08:38:42 +01002938perf_counter_alloc(struct perf_counter_hw_event *hw_event,
2939 int cpu,
Paul Mackerras23a185c2009-02-09 22:42:47 +11002940 struct perf_counter_context *ctx,
Ingo Molnar9b51f662008-12-12 13:49:45 +01002941 struct perf_counter *group_leader,
2942 gfp_t gfpflags)
Thomas Gleixner0793a612008-12-04 20:12:29 +01002943{
Robert Richter4aeb0b42009-04-29 12:47:03 +02002944 const struct pmu *pmu;
Ingo Molnar621a01e2008-12-11 12:46:46 +01002945 struct perf_counter *counter;
Peter Zijlstra60db5e02009-05-15 15:19:28 +02002946 struct hw_perf_counter *hwc;
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02002947 long err;
Thomas Gleixner0793a612008-12-04 20:12:29 +01002948
Ingo Molnar9b51f662008-12-12 13:49:45 +01002949 counter = kzalloc(sizeof(*counter), gfpflags);
Thomas Gleixner0793a612008-12-04 20:12:29 +01002950 if (!counter)
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02002951 return ERR_PTR(-ENOMEM);
Thomas Gleixner0793a612008-12-04 20:12:29 +01002952
Ingo Molnar04289bb2008-12-11 08:38:42 +01002953 /*
2954 * Single counters are their own group leaders, with an
2955 * empty sibling list:
2956 */
2957 if (!group_leader)
2958 group_leader = counter;
2959
Thomas Gleixner0793a612008-12-04 20:12:29 +01002960 mutex_init(&counter->mutex);
Ingo Molnar04289bb2008-12-11 08:38:42 +01002961 INIT_LIST_HEAD(&counter->list_entry);
Peter Zijlstra592903c2009-03-13 12:21:36 +01002962 INIT_LIST_HEAD(&counter->event_entry);
Ingo Molnar04289bb2008-12-11 08:38:42 +01002963 INIT_LIST_HEAD(&counter->sibling_list);
Thomas Gleixner0793a612008-12-04 20:12:29 +01002964 init_waitqueue_head(&counter->waitq);
2965
Peter Zijlstra7b732a72009-03-23 18:22:10 +01002966 mutex_init(&counter->mmap_mutex);
2967
Paul Mackerrasd859e292009-01-17 18:10:22 +11002968 INIT_LIST_HEAD(&counter->child_list);
2969
Ingo Molnar9f66a382008-12-10 12:33:23 +01002970 counter->cpu = cpu;
2971 counter->hw_event = *hw_event;
Ingo Molnar04289bb2008-12-11 08:38:42 +01002972 counter->group_leader = group_leader;
Robert Richter4aeb0b42009-04-29 12:47:03 +02002973 counter->pmu = NULL;
Paul Mackerras23a185c2009-02-09 22:42:47 +11002974 counter->ctx = ctx;
Ingo Molnar621a01e2008-12-11 12:46:46 +01002975
Ingo Molnar235c7fc2008-12-21 14:43:25 +01002976 counter->state = PERF_COUNTER_STATE_INACTIVE;
Ingo Molnara86ed502008-12-17 00:43:10 +01002977 if (hw_event->disabled)
2978 counter->state = PERF_COUNTER_STATE_OFF;
2979
Robert Richter4aeb0b42009-04-29 12:47:03 +02002980 pmu = NULL;
Peter Zijlstrab8e83512009-03-19 20:26:18 +01002981
Peter Zijlstra60db5e02009-05-15 15:19:28 +02002982 hwc = &counter->hw;
2983 if (hw_event->freq && hw_event->irq_freq)
Peter Zijlstra2e569d32009-05-15 15:37:47 +02002984 hwc->irq_period = div64_u64(TICK_NSEC, hw_event->irq_freq);
Peter Zijlstra60db5e02009-05-15 15:19:28 +02002985 else
2986 hwc->irq_period = hw_event->irq_period;
2987
Peter Zijlstra2023b352009-05-05 17:50:26 +02002988 /*
2989 * we currently do not support PERF_RECORD_GROUP on inherited counters
2990 */
2991 if (hw_event->inherit && (hw_event->record_type & PERF_RECORD_GROUP))
2992 goto done;
2993
Peter Zijlstraf4a2deb2009-03-23 18:22:06 +01002994 if (perf_event_raw(hw_event)) {
Robert Richter4aeb0b42009-04-29 12:47:03 +02002995 pmu = hw_perf_counter_init(counter);
Peter Zijlstraf4a2deb2009-03-23 18:22:06 +01002996 goto done;
2997 }
2998
2999 switch (perf_event_type(hw_event)) {
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003000 case PERF_TYPE_HARDWARE:
Robert Richter4aeb0b42009-04-29 12:47:03 +02003001 pmu = hw_perf_counter_init(counter);
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003002 break;
3003
3004 case PERF_TYPE_SOFTWARE:
Robert Richter4aeb0b42009-04-29 12:47:03 +02003005 pmu = sw_perf_counter_init(counter);
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003006 break;
3007
3008 case PERF_TYPE_TRACEPOINT:
Robert Richter4aeb0b42009-04-29 12:47:03 +02003009 pmu = tp_perf_counter_init(counter);
Peter Zijlstrab8e83512009-03-19 20:26:18 +01003010 break;
3011 }
Peter Zijlstraf4a2deb2009-03-23 18:22:06 +01003012done:
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003013 err = 0;
Robert Richter4aeb0b42009-04-29 12:47:03 +02003014 if (!pmu)
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003015 err = -EINVAL;
Robert Richter4aeb0b42009-04-29 12:47:03 +02003016 else if (IS_ERR(pmu))
3017 err = PTR_ERR(pmu);
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003018
3019 if (err) {
3020 kfree(counter);
3021 return ERR_PTR(err);
3022 }
3023
Robert Richter4aeb0b42009-04-29 12:47:03 +02003024 counter->pmu = pmu;
Thomas Gleixner0793a612008-12-04 20:12:29 +01003025
Peter Zijlstra7fc23a52009-05-08 18:52:21 +02003026 atomic_inc(&nr_counters);
Peter Zijlstra9ee318a2009-04-09 10:53:44 +02003027 if (counter->hw_event.mmap)
3028 atomic_inc(&nr_mmap_tracking);
3029 if (counter->hw_event.munmap)
3030 atomic_inc(&nr_munmap_tracking);
3031 if (counter->hw_event.comm)
3032 atomic_inc(&nr_comm_tracking);
3033
Thomas Gleixner0793a612008-12-04 20:12:29 +01003034 return counter;
3035}
3036
3037/**
Paul Mackerras2743a5b2009-03-04 20:36:51 +11003038 * sys_perf_counter_open - open a performance counter, associate it to a task/cpu
Ingo Molnar9f66a382008-12-10 12:33:23 +01003039 *
3040 * @hw_event_uptr: event type attributes for monitoring/sampling
Thomas Gleixner0793a612008-12-04 20:12:29 +01003041 * @pid: target pid
Ingo Molnar9f66a382008-12-10 12:33:23 +01003042 * @cpu: target cpu
3043 * @group_fd: group leader counter fd
Thomas Gleixner0793a612008-12-04 20:12:29 +01003044 */
Paul Mackerras2743a5b2009-03-04 20:36:51 +11003045SYSCALL_DEFINE5(perf_counter_open,
Paul Mackerrasf3dfd262009-02-26 22:43:46 +11003046 const struct perf_counter_hw_event __user *, hw_event_uptr,
Paul Mackerras2743a5b2009-03-04 20:36:51 +11003047 pid_t, pid, int, cpu, int, group_fd, unsigned long, flags)
Thomas Gleixner0793a612008-12-04 20:12:29 +01003048{
Ingo Molnar04289bb2008-12-11 08:38:42 +01003049 struct perf_counter *counter, *group_leader;
Ingo Molnar9f66a382008-12-10 12:33:23 +01003050 struct perf_counter_hw_event hw_event;
Ingo Molnar04289bb2008-12-11 08:38:42 +01003051 struct perf_counter_context *ctx;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003052 struct file *counter_file = NULL;
Ingo Molnar04289bb2008-12-11 08:38:42 +01003053 struct file *group_file = NULL;
3054 int fput_needed = 0;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003055 int fput_needed2 = 0;
Thomas Gleixner0793a612008-12-04 20:12:29 +01003056 int ret;
3057
Paul Mackerras2743a5b2009-03-04 20:36:51 +11003058 /* for future expandability... */
3059 if (flags)
3060 return -EINVAL;
3061
Ingo Molnar9f66a382008-12-10 12:33:23 +01003062 if (copy_from_user(&hw_event, hw_event_uptr, sizeof(hw_event)) != 0)
Thomas Gleixnereab656a2008-12-08 19:26:59 +01003063 return -EFAULT;
3064
Ingo Molnar04289bb2008-12-11 08:38:42 +01003065 /*
Ingo Molnarccff2862008-12-11 11:26:29 +01003066 * Get the target context (task or percpu):
3067 */
3068 ctx = find_get_context(pid, cpu);
3069 if (IS_ERR(ctx))
3070 return PTR_ERR(ctx);
3071
3072 /*
3073 * Look up the group leader (we will attach this counter to it):
Ingo Molnar04289bb2008-12-11 08:38:42 +01003074 */
3075 group_leader = NULL;
3076 if (group_fd != -1) {
3077 ret = -EINVAL;
3078 group_file = fget_light(group_fd, &fput_needed);
3079 if (!group_file)
Ingo Molnarccff2862008-12-11 11:26:29 +01003080 goto err_put_context;
Ingo Molnar04289bb2008-12-11 08:38:42 +01003081 if (group_file->f_op != &perf_fops)
Ingo Molnarccff2862008-12-11 11:26:29 +01003082 goto err_put_context;
Ingo Molnar04289bb2008-12-11 08:38:42 +01003083
3084 group_leader = group_file->private_data;
3085 /*
Ingo Molnarccff2862008-12-11 11:26:29 +01003086 * Do not allow a recursive hierarchy (this new sibling
3087 * becoming part of another group-sibling):
Ingo Molnar04289bb2008-12-11 08:38:42 +01003088 */
Ingo Molnarccff2862008-12-11 11:26:29 +01003089 if (group_leader->group_leader != group_leader)
3090 goto err_put_context;
3091 /*
3092 * Do not allow to attach to a group in a different
3093 * task or CPU context:
3094 */
3095 if (group_leader->ctx != ctx)
3096 goto err_put_context;
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11003097 /*
3098 * Only a group leader can be exclusive or pinned
3099 */
3100 if (hw_event.exclusive || hw_event.pinned)
3101 goto err_put_context;
Ingo Molnar04289bb2008-12-11 08:38:42 +01003102 }
3103
Paul Mackerras23a185c2009-02-09 22:42:47 +11003104 counter = perf_counter_alloc(&hw_event, cpu, ctx, group_leader,
3105 GFP_KERNEL);
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003106 ret = PTR_ERR(counter);
3107 if (IS_ERR(counter))
Thomas Gleixner0793a612008-12-04 20:12:29 +01003108 goto err_put_context;
3109
Thomas Gleixner0793a612008-12-04 20:12:29 +01003110 ret = anon_inode_getfd("[perf_counter]", &perf_fops, counter, 0);
3111 if (ret < 0)
Ingo Molnar9b51f662008-12-12 13:49:45 +01003112 goto err_free_put_context;
3113
3114 counter_file = fget_light(ret, &fput_needed2);
3115 if (!counter_file)
3116 goto err_free_put_context;
3117
3118 counter->filp = counter_file;
Paul Mackerrasd859e292009-01-17 18:10:22 +11003119 mutex_lock(&ctx->mutex);
Ingo Molnar9b51f662008-12-12 13:49:45 +01003120 perf_install_in_context(ctx, counter, cpu);
Paul Mackerrasd859e292009-01-17 18:10:22 +11003121 mutex_unlock(&ctx->mutex);
Ingo Molnar9b51f662008-12-12 13:49:45 +01003122
3123 fput_light(counter_file, fput_needed2);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003124
Ingo Molnar04289bb2008-12-11 08:38:42 +01003125out_fput:
3126 fput_light(group_file, fput_needed);
3127
Thomas Gleixner0793a612008-12-04 20:12:29 +01003128 return ret;
3129
Ingo Molnar9b51f662008-12-12 13:49:45 +01003130err_free_put_context:
Thomas Gleixner0793a612008-12-04 20:12:29 +01003131 kfree(counter);
3132
3133err_put_context:
3134 put_context(ctx);
3135
Ingo Molnar04289bb2008-12-11 08:38:42 +01003136 goto out_fput;
Thomas Gleixner0793a612008-12-04 20:12:29 +01003137}
3138
Ingo Molnar9b51f662008-12-12 13:49:45 +01003139/*
3140 * Initialize the perf_counter context in a task_struct:
3141 */
3142static void
3143__perf_counter_init_context(struct perf_counter_context *ctx,
3144 struct task_struct *task)
3145{
3146 memset(ctx, 0, sizeof(*ctx));
3147 spin_lock_init(&ctx->lock);
Paul Mackerrasd859e292009-01-17 18:10:22 +11003148 mutex_init(&ctx->mutex);
Ingo Molnar9b51f662008-12-12 13:49:45 +01003149 INIT_LIST_HEAD(&ctx->counter_list);
Peter Zijlstra592903c2009-03-13 12:21:36 +01003150 INIT_LIST_HEAD(&ctx->event_list);
Ingo Molnar9b51f662008-12-12 13:49:45 +01003151 ctx->task = task;
3152}
3153
3154/*
3155 * inherit a counter from parent task to child task:
3156 */
Paul Mackerrasd859e292009-01-17 18:10:22 +11003157static struct perf_counter *
Ingo Molnar9b51f662008-12-12 13:49:45 +01003158inherit_counter(struct perf_counter *parent_counter,
3159 struct task_struct *parent,
3160 struct perf_counter_context *parent_ctx,
3161 struct task_struct *child,
Paul Mackerrasd859e292009-01-17 18:10:22 +11003162 struct perf_counter *group_leader,
Ingo Molnar9b51f662008-12-12 13:49:45 +01003163 struct perf_counter_context *child_ctx)
3164{
3165 struct perf_counter *child_counter;
3166
Paul Mackerrasd859e292009-01-17 18:10:22 +11003167 /*
3168 * Instead of creating recursive hierarchies of counters,
3169 * we link inherited counters back to the original parent,
3170 * which has a filp for sure, which we use as the reference
3171 * count:
3172 */
3173 if (parent_counter->parent)
3174 parent_counter = parent_counter->parent;
3175
Ingo Molnar9b51f662008-12-12 13:49:45 +01003176 child_counter = perf_counter_alloc(&parent_counter->hw_event,
Paul Mackerras23a185c2009-02-09 22:42:47 +11003177 parent_counter->cpu, child_ctx,
3178 group_leader, GFP_KERNEL);
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003179 if (IS_ERR(child_counter))
3180 return child_counter;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003181
3182 /*
3183 * Link it up in the child's context:
3184 */
Ingo Molnar9b51f662008-12-12 13:49:45 +01003185 child_counter->task = child;
Paul Mackerras53cfbf52009-03-25 22:46:58 +11003186 add_counter_to_ctx(child_counter, child_ctx);
Ingo Molnar9b51f662008-12-12 13:49:45 +01003187
3188 child_counter->parent = parent_counter;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003189 /*
3190 * inherit into child's child as well:
3191 */
3192 child_counter->hw_event.inherit = 1;
3193
3194 /*
3195 * Get a reference to the parent filp - we will fput it
3196 * when the child counter exits. This is safe to do because
3197 * we are in the parent and we know that the filp still
3198 * exists and has a nonzero count:
3199 */
3200 atomic_long_inc(&parent_counter->filp->f_count);
3201
Paul Mackerrasd859e292009-01-17 18:10:22 +11003202 /*
3203 * Link this into the parent counter's child list
3204 */
3205 mutex_lock(&parent_counter->mutex);
3206 list_add_tail(&child_counter->child_list, &parent_counter->child_list);
3207
3208 /*
3209 * Make the child state follow the state of the parent counter,
3210 * not its hw_event.disabled bit. We hold the parent's mutex,
3211 * so we won't race with perf_counter_{en,dis}able_family.
3212 */
3213 if (parent_counter->state >= PERF_COUNTER_STATE_INACTIVE)
3214 child_counter->state = PERF_COUNTER_STATE_INACTIVE;
3215 else
3216 child_counter->state = PERF_COUNTER_STATE_OFF;
3217
3218 mutex_unlock(&parent_counter->mutex);
3219
3220 return child_counter;
3221}
3222
3223static int inherit_group(struct perf_counter *parent_counter,
3224 struct task_struct *parent,
3225 struct perf_counter_context *parent_ctx,
3226 struct task_struct *child,
3227 struct perf_counter_context *child_ctx)
3228{
3229 struct perf_counter *leader;
3230 struct perf_counter *sub;
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003231 struct perf_counter *child_ctr;
Paul Mackerrasd859e292009-01-17 18:10:22 +11003232
3233 leader = inherit_counter(parent_counter, parent, parent_ctx,
3234 child, NULL, child_ctx);
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003235 if (IS_ERR(leader))
3236 return PTR_ERR(leader);
Paul Mackerrasd859e292009-01-17 18:10:22 +11003237 list_for_each_entry(sub, &parent_counter->sibling_list, list_entry) {
Paul Mackerrasd5d2bc0d2009-03-30 19:07:08 +02003238 child_ctr = inherit_counter(sub, parent, parent_ctx,
3239 child, leader, child_ctx);
3240 if (IS_ERR(child_ctr))
3241 return PTR_ERR(child_ctr);
Paul Mackerrasd859e292009-01-17 18:10:22 +11003242 }
Ingo Molnar9b51f662008-12-12 13:49:45 +01003243 return 0;
3244}
3245
Paul Mackerrasd859e292009-01-17 18:10:22 +11003246static void sync_child_counter(struct perf_counter *child_counter,
3247 struct perf_counter *parent_counter)
3248{
Peter Zijlstra8bc20952009-05-15 20:45:59 +02003249 u64 child_val;
Paul Mackerrasd859e292009-01-17 18:10:22 +11003250
Paul Mackerrasd859e292009-01-17 18:10:22 +11003251 child_val = atomic64_read(&child_counter->count);
3252
3253 /*
3254 * Add back the child's count to the parent's count:
3255 */
3256 atomic64_add(child_val, &parent_counter->count);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11003257 atomic64_add(child_counter->total_time_enabled,
3258 &parent_counter->child_total_time_enabled);
3259 atomic64_add(child_counter->total_time_running,
3260 &parent_counter->child_total_time_running);
Paul Mackerrasd859e292009-01-17 18:10:22 +11003261
3262 /*
3263 * Remove this counter from the parent's list
3264 */
3265 mutex_lock(&parent_counter->mutex);
3266 list_del_init(&child_counter->child_list);
3267 mutex_unlock(&parent_counter->mutex);
3268
3269 /*
3270 * Release the parent counter, if this was the last
3271 * reference to it.
3272 */
3273 fput(parent_counter->filp);
3274}
3275
Ingo Molnar9b51f662008-12-12 13:49:45 +01003276static void
3277__perf_counter_exit_task(struct task_struct *child,
3278 struct perf_counter *child_counter,
3279 struct perf_counter_context *child_ctx)
3280{
3281 struct perf_counter *parent_counter;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003282
3283 /*
Ingo Molnar235c7fc2008-12-21 14:43:25 +01003284 * If we do not self-reap then we have to wait for the
3285 * child task to unschedule (it will happen for sure),
3286 * so that its counter is at its final count. (This
3287 * condition triggers rarely - child tasks usually get
3288 * off their CPU before the parent has a chance to
3289 * get this far into the reaping action)
Ingo Molnar9b51f662008-12-12 13:49:45 +01003290 */
Ingo Molnar235c7fc2008-12-21 14:43:25 +01003291 if (child != current) {
3292 wait_task_inactive(child, 0);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11003293 update_counter_times(child_counter);
Peter Zijlstra8bc20952009-05-15 20:45:59 +02003294 list_del_counter(child_counter, child_ctx);
Ingo Molnar235c7fc2008-12-21 14:43:25 +01003295 } else {
Ingo Molnar0cc0c022008-12-14 23:20:36 +01003296 struct perf_cpu_context *cpuctx;
Ingo Molnar235c7fc2008-12-21 14:43:25 +01003297 unsigned long flags;
Ingo Molnar235c7fc2008-12-21 14:43:25 +01003298
3299 /*
3300 * Disable and unlink this counter.
3301 *
3302 * Be careful about zapping the list - IRQ/NMI context
3303 * could still be processing it:
3304 */
Peter Zijlstra849691a2009-04-06 11:45:12 +02003305 local_irq_save(flags);
Peter Zijlstra9e35ad32009-05-13 16:21:38 +02003306 perf_disable();
Ingo Molnar0cc0c022008-12-14 23:20:36 +01003307
3308 cpuctx = &__get_cpu_var(perf_cpu_context);
3309
Paul Mackerrasd859e292009-01-17 18:10:22 +11003310 group_sched_out(child_counter, cpuctx, child_ctx);
Paul Mackerras53cfbf52009-03-25 22:46:58 +11003311 update_counter_times(child_counter);
Ingo Molnar0cc0c022008-12-14 23:20:36 +01003312
Peter Zijlstra8bc20952009-05-15 20:45:59 +02003313 list_del_counter(child_counter, child_ctx);
Ingo Molnar235c7fc2008-12-21 14:43:25 +01003314
Peter Zijlstra9e35ad32009-05-13 16:21:38 +02003315 perf_enable();
Peter Zijlstra849691a2009-04-06 11:45:12 +02003316 local_irq_restore(flags);
Ingo Molnar0cc0c022008-12-14 23:20:36 +01003317 }
3318
Ingo Molnar9b51f662008-12-12 13:49:45 +01003319 parent_counter = child_counter->parent;
3320 /*
3321 * It can happen that parent exits first, and has counters
3322 * that are still around due to the child reference. These
3323 * counters need to be zapped - but otherwise linger.
3324 */
Paul Mackerrasd859e292009-01-17 18:10:22 +11003325 if (parent_counter) {
3326 sync_child_counter(child_counter, parent_counter);
Peter Zijlstraf1600952009-03-19 20:26:16 +01003327 free_counter(child_counter);
Paul Mackerras4bcf3492009-02-11 13:53:19 +01003328 }
Ingo Molnar9b51f662008-12-12 13:49:45 +01003329}
3330
3331/*
Paul Mackerrasd859e292009-01-17 18:10:22 +11003332 * When a child task exits, feed back counter values to parent counters.
Ingo Molnar9b51f662008-12-12 13:49:45 +01003333 *
Paul Mackerrasd859e292009-01-17 18:10:22 +11003334 * Note: we may be running in child context, but the PID is not hashed
Ingo Molnar9b51f662008-12-12 13:49:45 +01003335 * anymore so new counters will not be added.
3336 */
3337void perf_counter_exit_task(struct task_struct *child)
3338{
3339 struct perf_counter *child_counter, *tmp;
3340 struct perf_counter_context *child_ctx;
3341
Ingo Molnar33b2fb32009-05-17 11:08:41 +02003342 WARN_ON_ONCE(child != current);
3343
Ingo Molnar9b51f662008-12-12 13:49:45 +01003344 child_ctx = &child->perf_counter_ctx;
3345
3346 if (likely(!child_ctx->nr_counters))
3347 return;
3348
Peter Zijlstra8bc20952009-05-15 20:45:59 +02003349again:
Ingo Molnar9b51f662008-12-12 13:49:45 +01003350 list_for_each_entry_safe(child_counter, tmp, &child_ctx->counter_list,
3351 list_entry)
3352 __perf_counter_exit_task(child, child_counter, child_ctx);
Peter Zijlstra8bc20952009-05-15 20:45:59 +02003353
3354 /*
3355 * If the last counter was a group counter, it will have appended all
3356 * its siblings to the list, but we obtained 'tmp' before that which
3357 * will still point to the list head terminating the iteration.
3358 */
3359 if (!list_empty(&child_ctx->counter_list))
3360 goto again;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003361}
3362
3363/*
3364 * Initialize the perf_counter context in task_struct
3365 */
3366void perf_counter_init_task(struct task_struct *child)
3367{
3368 struct perf_counter_context *child_ctx, *parent_ctx;
Paul Mackerrasd859e292009-01-17 18:10:22 +11003369 struct perf_counter *counter;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003370 struct task_struct *parent = current;
Ingo Molnar9b51f662008-12-12 13:49:45 +01003371
3372 child_ctx = &child->perf_counter_ctx;
3373 parent_ctx = &parent->perf_counter_ctx;
3374
3375 __perf_counter_init_context(child_ctx, child);
3376
3377 /*
3378 * This is executed from the parent task context, so inherit
3379 * counters that have been marked for cloning:
3380 */
3381
3382 if (likely(!parent_ctx->nr_counters))
3383 return;
3384
3385 /*
3386 * Lock the parent list. No need to lock the child - not PID
3387 * hashed yet and not running, so nobody can access it.
3388 */
Paul Mackerrasd859e292009-01-17 18:10:22 +11003389 mutex_lock(&parent_ctx->mutex);
Ingo Molnar9b51f662008-12-12 13:49:45 +01003390
3391 /*
3392 * We dont have to disable NMIs - we are only looking at
3393 * the list, not manipulating it:
3394 */
Peter Zijlstrad7b629a2009-05-20 12:21:19 +02003395 list_for_each_entry_rcu(counter, &parent_ctx->event_list, event_entry) {
3396 if (counter != counter->group_leader)
3397 continue;
3398
Paul Mackerrasd859e292009-01-17 18:10:22 +11003399 if (!counter->hw_event.inherit)
Ingo Molnar9b51f662008-12-12 13:49:45 +01003400 continue;
3401
Paul Mackerrasd859e292009-01-17 18:10:22 +11003402 if (inherit_group(counter, parent,
Ingo Molnar9b51f662008-12-12 13:49:45 +01003403 parent_ctx, child, child_ctx))
3404 break;
3405 }
3406
Paul Mackerrasd859e292009-01-17 18:10:22 +11003407 mutex_unlock(&parent_ctx->mutex);
Ingo Molnar9b51f662008-12-12 13:49:45 +01003408}
3409
Ingo Molnar04289bb2008-12-11 08:38:42 +01003410static void __cpuinit perf_counter_init_cpu(int cpu)
Thomas Gleixner0793a612008-12-04 20:12:29 +01003411{
Ingo Molnar04289bb2008-12-11 08:38:42 +01003412 struct perf_cpu_context *cpuctx;
Thomas Gleixner0793a612008-12-04 20:12:29 +01003413
Ingo Molnar04289bb2008-12-11 08:38:42 +01003414 cpuctx = &per_cpu(perf_cpu_context, cpu);
3415 __perf_counter_init_context(&cpuctx->ctx, NULL);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003416
Ingo Molnar1dce8d92009-05-04 19:23:18 +02003417 spin_lock(&perf_resource_lock);
Ingo Molnar04289bb2008-12-11 08:38:42 +01003418 cpuctx->max_pertask = perf_max_counters - perf_reserved_percpu;
Ingo Molnar1dce8d92009-05-04 19:23:18 +02003419 spin_unlock(&perf_resource_lock);
Ingo Molnar04289bb2008-12-11 08:38:42 +01003420
Paul Mackerras01d02872009-01-14 13:44:19 +11003421 hw_perf_counter_setup(cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003422}
3423
3424#ifdef CONFIG_HOTPLUG_CPU
Ingo Molnar04289bb2008-12-11 08:38:42 +01003425static void __perf_counter_exit_cpu(void *info)
Thomas Gleixner0793a612008-12-04 20:12:29 +01003426{
3427 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
3428 struct perf_counter_context *ctx = &cpuctx->ctx;
3429 struct perf_counter *counter, *tmp;
3430
Ingo Molnar04289bb2008-12-11 08:38:42 +01003431 list_for_each_entry_safe(counter, tmp, &ctx->counter_list, list_entry)
3432 __perf_counter_remove_from_context(counter);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003433}
Ingo Molnar04289bb2008-12-11 08:38:42 +01003434static void perf_counter_exit_cpu(int cpu)
Thomas Gleixner0793a612008-12-04 20:12:29 +01003435{
Paul Mackerrasd859e292009-01-17 18:10:22 +11003436 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
3437 struct perf_counter_context *ctx = &cpuctx->ctx;
3438
3439 mutex_lock(&ctx->mutex);
Ingo Molnar04289bb2008-12-11 08:38:42 +01003440 smp_call_function_single(cpu, __perf_counter_exit_cpu, NULL, 1);
Paul Mackerrasd859e292009-01-17 18:10:22 +11003441 mutex_unlock(&ctx->mutex);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003442}
3443#else
Ingo Molnar04289bb2008-12-11 08:38:42 +01003444static inline void perf_counter_exit_cpu(int cpu) { }
Thomas Gleixner0793a612008-12-04 20:12:29 +01003445#endif
3446
3447static int __cpuinit
3448perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
3449{
3450 unsigned int cpu = (long)hcpu;
3451
3452 switch (action) {
3453
3454 case CPU_UP_PREPARE:
3455 case CPU_UP_PREPARE_FROZEN:
Ingo Molnar04289bb2008-12-11 08:38:42 +01003456 perf_counter_init_cpu(cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003457 break;
3458
3459 case CPU_DOWN_PREPARE:
3460 case CPU_DOWN_PREPARE_FROZEN:
Ingo Molnar04289bb2008-12-11 08:38:42 +01003461 perf_counter_exit_cpu(cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003462 break;
3463
3464 default:
3465 break;
3466 }
3467
3468 return NOTIFY_OK;
3469}
3470
3471static struct notifier_block __cpuinitdata perf_cpu_nb = {
3472 .notifier_call = perf_cpu_notify,
3473};
3474
Ingo Molnar0d905bc2009-05-04 19:13:30 +02003475void __init perf_counter_init(void)
Thomas Gleixner0793a612008-12-04 20:12:29 +01003476{
3477 perf_cpu_notify(&perf_cpu_nb, (unsigned long)CPU_UP_PREPARE,
3478 (void *)(long)smp_processor_id());
3479 register_cpu_notifier(&perf_cpu_nb);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003480}
Thomas Gleixner0793a612008-12-04 20:12:29 +01003481
3482static ssize_t perf_show_reserve_percpu(struct sysdev_class *class, char *buf)
3483{
3484 return sprintf(buf, "%d\n", perf_reserved_percpu);
3485}
3486
3487static ssize_t
3488perf_set_reserve_percpu(struct sysdev_class *class,
3489 const char *buf,
3490 size_t count)
3491{
3492 struct perf_cpu_context *cpuctx;
3493 unsigned long val;
3494 int err, cpu, mpt;
3495
3496 err = strict_strtoul(buf, 10, &val);
3497 if (err)
3498 return err;
3499 if (val > perf_max_counters)
3500 return -EINVAL;
3501
Ingo Molnar1dce8d92009-05-04 19:23:18 +02003502 spin_lock(&perf_resource_lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003503 perf_reserved_percpu = val;
3504 for_each_online_cpu(cpu) {
3505 cpuctx = &per_cpu(perf_cpu_context, cpu);
3506 spin_lock_irq(&cpuctx->ctx.lock);
3507 mpt = min(perf_max_counters - cpuctx->ctx.nr_counters,
3508 perf_max_counters - perf_reserved_percpu);
3509 cpuctx->max_pertask = mpt;
3510 spin_unlock_irq(&cpuctx->ctx.lock);
3511 }
Ingo Molnar1dce8d92009-05-04 19:23:18 +02003512 spin_unlock(&perf_resource_lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003513
3514 return count;
3515}
3516
3517static ssize_t perf_show_overcommit(struct sysdev_class *class, char *buf)
3518{
3519 return sprintf(buf, "%d\n", perf_overcommit);
3520}
3521
3522static ssize_t
3523perf_set_overcommit(struct sysdev_class *class, const char *buf, size_t count)
3524{
3525 unsigned long val;
3526 int err;
3527
3528 err = strict_strtoul(buf, 10, &val);
3529 if (err)
3530 return err;
3531 if (val > 1)
3532 return -EINVAL;
3533
Ingo Molnar1dce8d92009-05-04 19:23:18 +02003534 spin_lock(&perf_resource_lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003535 perf_overcommit = val;
Ingo Molnar1dce8d92009-05-04 19:23:18 +02003536 spin_unlock(&perf_resource_lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +01003537
3538 return count;
3539}
3540
3541static SYSDEV_CLASS_ATTR(
3542 reserve_percpu,
3543 0644,
3544 perf_show_reserve_percpu,
3545 perf_set_reserve_percpu
3546 );
3547
3548static SYSDEV_CLASS_ATTR(
3549 overcommit,
3550 0644,
3551 perf_show_overcommit,
3552 perf_set_overcommit
3553 );
3554
3555static struct attribute *perfclass_attrs[] = {
3556 &attr_reserve_percpu.attr,
3557 &attr_overcommit.attr,
3558 NULL
3559};
3560
3561static struct attribute_group perfclass_attr_group = {
3562 .attrs = perfclass_attrs,
3563 .name = "perf_counters",
3564};
3565
3566static int __init perf_counter_sysfs_init(void)
3567{
3568 return sysfs_create_group(&cpu_sysdev_class.kset.kobj,
3569 &perfclass_attr_group);
3570}
3571device_initcall(perf_counter_sysfs_init);