blob: d9cfd902140e00011c65775dc7e262ec0576d426 [file] [log] [blame]
Thomas Gleixner0793a612008-12-04 20:12:29 +01001/*
2 * Performance counter core code
3 *
4 * Copyright(C) 2008 Thomas Gleixner <tglx@linutronix.de>
5 * Copyright(C) 2008 Red Hat, Inc., Ingo Molnar
6 *
7 * For licencing details see kernel-base/COPYING
8 */
9
10#include <linux/fs.h>
11#include <linux/cpu.h>
12#include <linux/smp.h>
Ingo Molnar04289bb2008-12-11 08:38:42 +010013#include <linux/file.h>
Thomas Gleixner0793a612008-12-04 20:12:29 +010014#include <linux/poll.h>
15#include <linux/sysfs.h>
16#include <linux/ptrace.h>
17#include <linux/percpu.h>
18#include <linux/uaccess.h>
19#include <linux/syscalls.h>
20#include <linux/anon_inodes.h>
Ingo Molnaraa9c4c02008-12-17 14:10:57 +010021#include <linux/kernel_stat.h>
Thomas Gleixner0793a612008-12-04 20:12:29 +010022#include <linux/perf_counter.h>
Paul Mackerras23a185c2009-02-09 22:42:47 +110023#include <linux/mm.h>
24#include <linux/vmstat.h>
Peter Zijlstra592903c2009-03-13 12:21:36 +010025#include <linux/rculist.h>
Peter Zijlstra96f6d442009-03-23 18:22:07 +010026#include <linux/hardirq.h>
Thomas Gleixner0793a612008-12-04 20:12:29 +010027
Tim Blechmann4e193bd2009-03-14 14:29:25 +010028#include <asm/irq_regs.h>
29
Thomas Gleixner0793a612008-12-04 20:12:29 +010030/*
31 * Each CPU has a list of per CPU counters:
32 */
33DEFINE_PER_CPU(struct perf_cpu_context, perf_cpu_context);
34
Ingo Molnar088e2852008-12-14 20:21:00 +010035int perf_max_counters __read_mostly = 1;
Thomas Gleixner0793a612008-12-04 20:12:29 +010036static int perf_reserved_percpu __read_mostly;
37static int perf_overcommit __read_mostly = 1;
38
39/*
40 * Mutex for (sysadmin-configurable) counter reservations:
41 */
42static DEFINE_MUTEX(perf_resource_mutex);
43
44/*
45 * Architecture provided APIs - weak aliases:
46 */
Ingo Molnar5c92d122008-12-11 13:21:10 +010047extern __weak const struct hw_perf_counter_ops *
Ingo Molnar621a01e2008-12-11 12:46:46 +010048hw_perf_counter_init(struct perf_counter *counter)
Thomas Gleixner0793a612008-12-04 20:12:29 +010049{
Paul Mackerrasff6f0542009-01-09 16:19:25 +110050 return NULL;
Thomas Gleixner0793a612008-12-04 20:12:29 +010051}
52
Ingo Molnar01b28382008-12-11 13:45:51 +010053u64 __weak hw_perf_save_disable(void) { return 0; }
Yinghai Lu01ea1cc2008-12-26 21:05:06 -080054void __weak hw_perf_restore(u64 ctrl) { barrier(); }
Paul Mackerras01d02872009-01-14 13:44:19 +110055void __weak hw_perf_counter_setup(int cpu) { barrier(); }
Paul Mackerras3cbed422009-01-09 16:43:42 +110056int __weak hw_perf_group_sched_in(struct perf_counter *group_leader,
57 struct perf_cpu_context *cpuctx,
58 struct perf_counter_context *ctx, int cpu)
59{
60 return 0;
61}
Thomas Gleixner0793a612008-12-04 20:12:29 +010062
Paul Mackerras4eb96fc2009-01-09 17:24:34 +110063void __weak perf_counter_print_debug(void) { }
64
Ingo Molnar04289bb2008-12-11 08:38:42 +010065static void
66list_add_counter(struct perf_counter *counter, struct perf_counter_context *ctx)
67{
68 struct perf_counter *group_leader = counter->group_leader;
69
70 /*
71 * Depending on whether it is a standalone or sibling counter,
72 * add it straight to the context's counter list, or to the group
73 * leader's sibling list:
74 */
75 if (counter->group_leader == counter)
76 list_add_tail(&counter->list_entry, &ctx->counter_list);
77 else
78 list_add_tail(&counter->list_entry, &group_leader->sibling_list);
Peter Zijlstra592903c2009-03-13 12:21:36 +010079
80 list_add_rcu(&counter->event_entry, &ctx->event_list);
Ingo Molnar04289bb2008-12-11 08:38:42 +010081}
82
83static void
84list_del_counter(struct perf_counter *counter, struct perf_counter_context *ctx)
85{
86 struct perf_counter *sibling, *tmp;
87
88 list_del_init(&counter->list_entry);
Peter Zijlstra592903c2009-03-13 12:21:36 +010089 list_del_rcu(&counter->event_entry);
Ingo Molnar04289bb2008-12-11 08:38:42 +010090
Ingo Molnar04289bb2008-12-11 08:38:42 +010091 /*
92 * If this was a group counter with sibling counters then
93 * upgrade the siblings to singleton counters by adding them
94 * to the context list directly:
95 */
96 list_for_each_entry_safe(sibling, tmp,
97 &counter->sibling_list, list_entry) {
98
Peter Zijlstra75564232009-03-13 12:21:29 +010099 list_move_tail(&sibling->list_entry, &ctx->counter_list);
Ingo Molnar04289bb2008-12-11 08:38:42 +0100100 sibling->group_leader = sibling;
101 }
102}
103
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100104static void
105counter_sched_out(struct perf_counter *counter,
106 struct perf_cpu_context *cpuctx,
107 struct perf_counter_context *ctx)
108{
109 if (counter->state != PERF_COUNTER_STATE_ACTIVE)
110 return;
111
112 counter->state = PERF_COUNTER_STATE_INACTIVE;
113 counter->hw_ops->disable(counter);
114 counter->oncpu = -1;
115
116 if (!is_software_counter(counter))
117 cpuctx->active_oncpu--;
118 ctx->nr_active--;
119 if (counter->hw_event.exclusive || !cpuctx->active_oncpu)
120 cpuctx->exclusive = 0;
121}
122
Paul Mackerrasd859e292009-01-17 18:10:22 +1100123static void
124group_sched_out(struct perf_counter *group_counter,
125 struct perf_cpu_context *cpuctx,
126 struct perf_counter_context *ctx)
127{
128 struct perf_counter *counter;
129
130 if (group_counter->state != PERF_COUNTER_STATE_ACTIVE)
131 return;
132
133 counter_sched_out(group_counter, cpuctx, ctx);
134
135 /*
136 * Schedule out siblings (if any):
137 */
138 list_for_each_entry(counter, &group_counter->sibling_list, list_entry)
139 counter_sched_out(counter, cpuctx, ctx);
140
141 if (group_counter->hw_event.exclusive)
142 cpuctx->exclusive = 0;
143}
144
Thomas Gleixner0793a612008-12-04 20:12:29 +0100145/*
146 * Cross CPU call to remove a performance counter
147 *
148 * We disable the counter on the hardware level first. After that we
149 * remove it from the context list.
150 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100151static void __perf_counter_remove_from_context(void *info)
Thomas Gleixner0793a612008-12-04 20:12:29 +0100152{
153 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
154 struct perf_counter *counter = info;
155 struct perf_counter_context *ctx = counter->ctx;
Ingo Molnar9b51f662008-12-12 13:49:45 +0100156 unsigned long flags;
Ingo Molnar5c92d122008-12-11 13:21:10 +0100157 u64 perf_flags;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100158
159 /*
160 * If this is a task context, we need to check whether it is
161 * the current task context of this cpu. If not it has been
162 * scheduled out before the smp call arrived.
163 */
164 if (ctx->task && cpuctx->task_ctx != ctx)
165 return;
166
Ingo Molnaraa9c4c02008-12-17 14:10:57 +0100167 curr_rq_lock_irq_save(&flags);
168 spin_lock(&ctx->lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100169
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100170 counter_sched_out(counter, cpuctx, ctx);
171
172 counter->task = NULL;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100173 ctx->nr_counters--;
174
175 /*
176 * Protect the list operation against NMI by disabling the
177 * counters on a global level. NOP for non NMI based counters.
178 */
Ingo Molnar01b28382008-12-11 13:45:51 +0100179 perf_flags = hw_perf_save_disable();
Ingo Molnar04289bb2008-12-11 08:38:42 +0100180 list_del_counter(counter, ctx);
Ingo Molnar01b28382008-12-11 13:45:51 +0100181 hw_perf_restore(perf_flags);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100182
183 if (!ctx->task) {
184 /*
185 * Allow more per task counters with respect to the
186 * reservation:
187 */
188 cpuctx->max_pertask =
189 min(perf_max_counters - ctx->nr_counters,
190 perf_max_counters - perf_reserved_percpu);
191 }
192
Ingo Molnaraa9c4c02008-12-17 14:10:57 +0100193 spin_unlock(&ctx->lock);
194 curr_rq_unlock_irq_restore(&flags);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100195}
196
197
198/*
199 * Remove the counter from a task's (or a CPU's) list of counters.
200 *
Paul Mackerrasd859e292009-01-17 18:10:22 +1100201 * Must be called with counter->mutex and ctx->mutex held.
Thomas Gleixner0793a612008-12-04 20:12:29 +0100202 *
203 * CPU counters are removed with a smp call. For task counters we only
204 * call when the task is on a CPU.
205 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100206static void perf_counter_remove_from_context(struct perf_counter *counter)
Thomas Gleixner0793a612008-12-04 20:12:29 +0100207{
208 struct perf_counter_context *ctx = counter->ctx;
209 struct task_struct *task = ctx->task;
210
211 if (!task) {
212 /*
213 * Per cpu counters are removed via an smp call and
214 * the removal is always sucessful.
215 */
216 smp_call_function_single(counter->cpu,
Ingo Molnar04289bb2008-12-11 08:38:42 +0100217 __perf_counter_remove_from_context,
Thomas Gleixner0793a612008-12-04 20:12:29 +0100218 counter, 1);
219 return;
220 }
221
222retry:
Ingo Molnar04289bb2008-12-11 08:38:42 +0100223 task_oncpu_function_call(task, __perf_counter_remove_from_context,
Thomas Gleixner0793a612008-12-04 20:12:29 +0100224 counter);
225
226 spin_lock_irq(&ctx->lock);
227 /*
228 * If the context is active we need to retry the smp call.
229 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100230 if (ctx->nr_active && !list_empty(&counter->list_entry)) {
Thomas Gleixner0793a612008-12-04 20:12:29 +0100231 spin_unlock_irq(&ctx->lock);
232 goto retry;
233 }
234
235 /*
236 * The lock prevents that this context is scheduled in so we
Ingo Molnar04289bb2008-12-11 08:38:42 +0100237 * can remove the counter safely, if the call above did not
Thomas Gleixner0793a612008-12-04 20:12:29 +0100238 * succeed.
239 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100240 if (!list_empty(&counter->list_entry)) {
Thomas Gleixner0793a612008-12-04 20:12:29 +0100241 ctx->nr_counters--;
Ingo Molnar04289bb2008-12-11 08:38:42 +0100242 list_del_counter(counter, ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100243 counter->task = NULL;
244 }
245 spin_unlock_irq(&ctx->lock);
246}
247
Paul Mackerrasd859e292009-01-17 18:10:22 +1100248/*
249 * Cross CPU call to disable a performance counter
250 */
251static void __perf_counter_disable(void *info)
252{
253 struct perf_counter *counter = info;
254 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
255 struct perf_counter_context *ctx = counter->ctx;
256 unsigned long flags;
257
258 /*
259 * If this is a per-task counter, need to check whether this
260 * counter's task is the current task on this cpu.
261 */
262 if (ctx->task && cpuctx->task_ctx != ctx)
263 return;
264
265 curr_rq_lock_irq_save(&flags);
266 spin_lock(&ctx->lock);
267
268 /*
269 * If the counter is on, turn it off.
270 * If it is in error state, leave it in error state.
271 */
272 if (counter->state >= PERF_COUNTER_STATE_INACTIVE) {
273 if (counter == counter->group_leader)
274 group_sched_out(counter, cpuctx, ctx);
275 else
276 counter_sched_out(counter, cpuctx, ctx);
277 counter->state = PERF_COUNTER_STATE_OFF;
278 }
279
280 spin_unlock(&ctx->lock);
281 curr_rq_unlock_irq_restore(&flags);
282}
283
284/*
285 * Disable a counter.
286 */
287static void perf_counter_disable(struct perf_counter *counter)
288{
289 struct perf_counter_context *ctx = counter->ctx;
290 struct task_struct *task = ctx->task;
291
292 if (!task) {
293 /*
294 * Disable the counter on the cpu that it's on
295 */
296 smp_call_function_single(counter->cpu, __perf_counter_disable,
297 counter, 1);
298 return;
299 }
300
301 retry:
302 task_oncpu_function_call(task, __perf_counter_disable, counter);
303
304 spin_lock_irq(&ctx->lock);
305 /*
306 * If the counter is still active, we need to retry the cross-call.
307 */
308 if (counter->state == PERF_COUNTER_STATE_ACTIVE) {
309 spin_unlock_irq(&ctx->lock);
310 goto retry;
311 }
312
313 /*
314 * Since we have the lock this context can't be scheduled
315 * in, so we can change the state safely.
316 */
317 if (counter->state == PERF_COUNTER_STATE_INACTIVE)
318 counter->state = PERF_COUNTER_STATE_OFF;
319
320 spin_unlock_irq(&ctx->lock);
321}
322
323/*
324 * Disable a counter and all its children.
325 */
326static void perf_counter_disable_family(struct perf_counter *counter)
327{
328 struct perf_counter *child;
329
330 perf_counter_disable(counter);
331
332 /*
333 * Lock the mutex to protect the list of children
334 */
335 mutex_lock(&counter->mutex);
336 list_for_each_entry(child, &counter->child_list, child_list)
337 perf_counter_disable(child);
338 mutex_unlock(&counter->mutex);
339}
340
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100341static int
342counter_sched_in(struct perf_counter *counter,
343 struct perf_cpu_context *cpuctx,
344 struct perf_counter_context *ctx,
345 int cpu)
346{
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100347 if (counter->state <= PERF_COUNTER_STATE_OFF)
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100348 return 0;
349
350 counter->state = PERF_COUNTER_STATE_ACTIVE;
351 counter->oncpu = cpu; /* TODO: put 'cpu' into cpuctx->cpu */
352 /*
353 * The new state must be visible before we turn it on in the hardware:
354 */
355 smp_wmb();
356
357 if (counter->hw_ops->enable(counter)) {
358 counter->state = PERF_COUNTER_STATE_INACTIVE;
359 counter->oncpu = -1;
360 return -EAGAIN;
361 }
362
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100363 if (!is_software_counter(counter))
364 cpuctx->active_oncpu++;
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100365 ctx->nr_active++;
366
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100367 if (counter->hw_event.exclusive)
368 cpuctx->exclusive = 1;
369
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100370 return 0;
371}
372
Thomas Gleixner0793a612008-12-04 20:12:29 +0100373/*
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100374 * Return 1 for a group consisting entirely of software counters,
375 * 0 if the group contains any hardware counters.
376 */
377static int is_software_only_group(struct perf_counter *leader)
378{
379 struct perf_counter *counter;
380
381 if (!is_software_counter(leader))
382 return 0;
383 list_for_each_entry(counter, &leader->sibling_list, list_entry)
384 if (!is_software_counter(counter))
385 return 0;
386 return 1;
387}
388
389/*
390 * Work out whether we can put this counter group on the CPU now.
391 */
392static int group_can_go_on(struct perf_counter *counter,
393 struct perf_cpu_context *cpuctx,
394 int can_add_hw)
395{
396 /*
397 * Groups consisting entirely of software counters can always go on.
398 */
399 if (is_software_only_group(counter))
400 return 1;
401 /*
402 * If an exclusive group is already on, no other hardware
403 * counters can go on.
404 */
405 if (cpuctx->exclusive)
406 return 0;
407 /*
408 * If this group is exclusive and there are already
409 * counters on the CPU, it can't go on.
410 */
411 if (counter->hw_event.exclusive && cpuctx->active_oncpu)
412 return 0;
413 /*
414 * Otherwise, try to add it if all previous groups were able
415 * to go on.
416 */
417 return can_add_hw;
418}
419
420/*
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100421 * Cross CPU call to install and enable a performance counter
Thomas Gleixner0793a612008-12-04 20:12:29 +0100422 */
423static void __perf_install_in_context(void *info)
424{
425 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
426 struct perf_counter *counter = info;
427 struct perf_counter_context *ctx = counter->ctx;
Paul Mackerrasd859e292009-01-17 18:10:22 +1100428 struct perf_counter *leader = counter->group_leader;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100429 int cpu = smp_processor_id();
Ingo Molnar9b51f662008-12-12 13:49:45 +0100430 unsigned long flags;
Ingo Molnar5c92d122008-12-11 13:21:10 +0100431 u64 perf_flags;
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100432 int err;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100433
434 /*
435 * If this is a task context, we need to check whether it is
436 * the current task context of this cpu. If not it has been
437 * scheduled out before the smp call arrived.
438 */
439 if (ctx->task && cpuctx->task_ctx != ctx)
440 return;
441
Ingo Molnaraa9c4c02008-12-17 14:10:57 +0100442 curr_rq_lock_irq_save(&flags);
443 spin_lock(&ctx->lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100444
445 /*
446 * Protect the list operation against NMI by disabling the
447 * counters on a global level. NOP for non NMI based counters.
448 */
Ingo Molnar01b28382008-12-11 13:45:51 +0100449 perf_flags = hw_perf_save_disable();
Thomas Gleixner0793a612008-12-04 20:12:29 +0100450
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100451 list_add_counter(counter, ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100452 ctx->nr_counters++;
Paul Mackerrasc07c99b2009-02-13 22:10:34 +1100453 counter->prev_state = PERF_COUNTER_STATE_OFF;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100454
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100455 /*
Paul Mackerrasd859e292009-01-17 18:10:22 +1100456 * Don't put the counter on if it is disabled or if
457 * it is in a group and the group isn't on.
458 */
459 if (counter->state != PERF_COUNTER_STATE_INACTIVE ||
460 (leader != counter && leader->state != PERF_COUNTER_STATE_ACTIVE))
461 goto unlock;
462
463 /*
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100464 * An exclusive counter can't go on if there are already active
465 * hardware counters, and no hardware counter can go on if there
466 * is already an exclusive counter on.
467 */
Paul Mackerrasd859e292009-01-17 18:10:22 +1100468 if (!group_can_go_on(counter, cpuctx, 1))
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100469 err = -EEXIST;
470 else
471 err = counter_sched_in(counter, cpuctx, ctx, cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100472
Paul Mackerrasd859e292009-01-17 18:10:22 +1100473 if (err) {
474 /*
475 * This counter couldn't go on. If it is in a group
476 * then we have to pull the whole group off.
477 * If the counter group is pinned then put it in error state.
478 */
479 if (leader != counter)
480 group_sched_out(leader, cpuctx, ctx);
481 if (leader->hw_event.pinned)
482 leader->state = PERF_COUNTER_STATE_ERROR;
483 }
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100484
485 if (!err && !ctx->task && cpuctx->max_pertask)
Thomas Gleixner0793a612008-12-04 20:12:29 +0100486 cpuctx->max_pertask--;
487
Paul Mackerrasd859e292009-01-17 18:10:22 +1100488 unlock:
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100489 hw_perf_restore(perf_flags);
490
Ingo Molnaraa9c4c02008-12-17 14:10:57 +0100491 spin_unlock(&ctx->lock);
492 curr_rq_unlock_irq_restore(&flags);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100493}
494
495/*
496 * Attach a performance counter to a context
497 *
498 * First we add the counter to the list with the hardware enable bit
499 * in counter->hw_config cleared.
500 *
501 * If the counter is attached to a task which is on a CPU we use a smp
502 * call to enable it in the task context. The task might have been
503 * scheduled away, but we check this in the smp call again.
Paul Mackerrasd859e292009-01-17 18:10:22 +1100504 *
505 * Must be called with ctx->mutex held.
Thomas Gleixner0793a612008-12-04 20:12:29 +0100506 */
507static void
508perf_install_in_context(struct perf_counter_context *ctx,
509 struct perf_counter *counter,
510 int cpu)
511{
512 struct task_struct *task = ctx->task;
513
Thomas Gleixner0793a612008-12-04 20:12:29 +0100514 if (!task) {
515 /*
516 * Per cpu counters are installed via an smp call and
517 * the install is always sucessful.
518 */
519 smp_call_function_single(cpu, __perf_install_in_context,
520 counter, 1);
521 return;
522 }
523
524 counter->task = task;
525retry:
526 task_oncpu_function_call(task, __perf_install_in_context,
527 counter);
528
529 spin_lock_irq(&ctx->lock);
530 /*
Thomas Gleixner0793a612008-12-04 20:12:29 +0100531 * we need to retry the smp call.
532 */
Paul Mackerrasd859e292009-01-17 18:10:22 +1100533 if (ctx->is_active && list_empty(&counter->list_entry)) {
Thomas Gleixner0793a612008-12-04 20:12:29 +0100534 spin_unlock_irq(&ctx->lock);
535 goto retry;
536 }
537
538 /*
539 * The lock prevents that this context is scheduled in so we
540 * can add the counter safely, if it the call above did not
541 * succeed.
542 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100543 if (list_empty(&counter->list_entry)) {
544 list_add_counter(counter, ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100545 ctx->nr_counters++;
546 }
547 spin_unlock_irq(&ctx->lock);
548}
549
Paul Mackerrasd859e292009-01-17 18:10:22 +1100550/*
551 * Cross CPU call to enable a performance counter
552 */
553static void __perf_counter_enable(void *info)
Ingo Molnar04289bb2008-12-11 08:38:42 +0100554{
Paul Mackerrasd859e292009-01-17 18:10:22 +1100555 struct perf_counter *counter = info;
556 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
557 struct perf_counter_context *ctx = counter->ctx;
558 struct perf_counter *leader = counter->group_leader;
559 unsigned long flags;
560 int err;
Ingo Molnar04289bb2008-12-11 08:38:42 +0100561
562 /*
Paul Mackerrasd859e292009-01-17 18:10:22 +1100563 * If this is a per-task counter, need to check whether this
564 * counter's task is the current task on this cpu.
Ingo Molnar04289bb2008-12-11 08:38:42 +0100565 */
Paul Mackerrasd859e292009-01-17 18:10:22 +1100566 if (ctx->task && cpuctx->task_ctx != ctx)
567 return;
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100568
Paul Mackerrasd859e292009-01-17 18:10:22 +1100569 curr_rq_lock_irq_save(&flags);
570 spin_lock(&ctx->lock);
571
Paul Mackerrasc07c99b2009-02-13 22:10:34 +1100572 counter->prev_state = counter->state;
Paul Mackerrasd859e292009-01-17 18:10:22 +1100573 if (counter->state >= PERF_COUNTER_STATE_INACTIVE)
574 goto unlock;
575 counter->state = PERF_COUNTER_STATE_INACTIVE;
576
577 /*
578 * If the counter is in a group and isn't the group leader,
579 * then don't put it on unless the group is on.
580 */
581 if (leader != counter && leader->state != PERF_COUNTER_STATE_ACTIVE)
582 goto unlock;
583
584 if (!group_can_go_on(counter, cpuctx, 1))
585 err = -EEXIST;
586 else
587 err = counter_sched_in(counter, cpuctx, ctx,
588 smp_processor_id());
589
590 if (err) {
591 /*
592 * If this counter can't go on and it's part of a
593 * group, then the whole group has to come off.
594 */
595 if (leader != counter)
596 group_sched_out(leader, cpuctx, ctx);
597 if (leader->hw_event.pinned)
598 leader->state = PERF_COUNTER_STATE_ERROR;
599 }
600
601 unlock:
602 spin_unlock(&ctx->lock);
603 curr_rq_unlock_irq_restore(&flags);
604}
605
606/*
607 * Enable a counter.
608 */
609static void perf_counter_enable(struct perf_counter *counter)
610{
611 struct perf_counter_context *ctx = counter->ctx;
612 struct task_struct *task = ctx->task;
613
614 if (!task) {
615 /*
616 * Enable the counter on the cpu that it's on
617 */
618 smp_call_function_single(counter->cpu, __perf_counter_enable,
619 counter, 1);
620 return;
621 }
622
623 spin_lock_irq(&ctx->lock);
624 if (counter->state >= PERF_COUNTER_STATE_INACTIVE)
625 goto out;
626
627 /*
628 * If the counter is in error state, clear that first.
629 * That way, if we see the counter in error state below, we
630 * know that it has gone back into error state, as distinct
631 * from the task having been scheduled away before the
632 * cross-call arrived.
633 */
634 if (counter->state == PERF_COUNTER_STATE_ERROR)
635 counter->state = PERF_COUNTER_STATE_OFF;
636
637 retry:
638 spin_unlock_irq(&ctx->lock);
639 task_oncpu_function_call(task, __perf_counter_enable, counter);
640
641 spin_lock_irq(&ctx->lock);
642
643 /*
644 * If the context is active and the counter is still off,
645 * we need to retry the cross-call.
646 */
647 if (ctx->is_active && counter->state == PERF_COUNTER_STATE_OFF)
648 goto retry;
649
650 /*
651 * Since we have the lock this context can't be scheduled
652 * in, so we can change the state safely.
653 */
654 if (counter->state == PERF_COUNTER_STATE_OFF)
655 counter->state = PERF_COUNTER_STATE_INACTIVE;
656 out:
657 spin_unlock_irq(&ctx->lock);
658}
659
660/*
661 * Enable a counter and all its children.
662 */
663static void perf_counter_enable_family(struct perf_counter *counter)
664{
665 struct perf_counter *child;
666
667 perf_counter_enable(counter);
668
669 /*
670 * Lock the mutex to protect the list of children
671 */
672 mutex_lock(&counter->mutex);
673 list_for_each_entry(child, &counter->child_list, child_list)
674 perf_counter_enable(child);
675 mutex_unlock(&counter->mutex);
Ingo Molnar04289bb2008-12-11 08:38:42 +0100676}
677
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100678void __perf_counter_sched_out(struct perf_counter_context *ctx,
679 struct perf_cpu_context *cpuctx)
680{
681 struct perf_counter *counter;
Paul Mackerras3cbed422009-01-09 16:43:42 +1100682 u64 flags;
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100683
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100684 spin_lock(&ctx->lock);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100685 ctx->is_active = 0;
686 if (likely(!ctx->nr_counters))
687 goto out;
688
Paul Mackerras3cbed422009-01-09 16:43:42 +1100689 flags = hw_perf_save_disable();
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100690 if (ctx->nr_active) {
691 list_for_each_entry(counter, &ctx->counter_list, list_entry)
692 group_sched_out(counter, cpuctx, ctx);
693 }
Paul Mackerras3cbed422009-01-09 16:43:42 +1100694 hw_perf_restore(flags);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100695 out:
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100696 spin_unlock(&ctx->lock);
697}
698
Thomas Gleixner0793a612008-12-04 20:12:29 +0100699/*
700 * Called from scheduler to remove the counters of the current task,
701 * with interrupts disabled.
702 *
703 * We stop each counter and update the counter value in counter->count.
704 *
Ingo Molnar76715812008-12-17 14:20:28 +0100705 * This does not protect us against NMI, but disable()
Thomas Gleixner0793a612008-12-04 20:12:29 +0100706 * sets the disabled bit in the control field of counter _before_
707 * accessing the counter control register. If a NMI hits, then it will
708 * not restart the counter.
709 */
710void perf_counter_task_sched_out(struct task_struct *task, int cpu)
711{
712 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
713 struct perf_counter_context *ctx = &task->perf_counter_ctx;
Peter Zijlstra4a0deca2009-03-19 20:26:12 +0100714 struct pt_regs *regs;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100715
716 if (likely(!cpuctx->task_ctx))
717 return;
718
Peter Zijlstra4a0deca2009-03-19 20:26:12 +0100719 regs = task_pt_regs(task);
720 perf_swcounter_event(PERF_COUNT_CONTEXT_SWITCHES, 1, 1, regs);
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100721 __perf_counter_sched_out(ctx, cpuctx);
722
Thomas Gleixner0793a612008-12-04 20:12:29 +0100723 cpuctx->task_ctx = NULL;
724}
725
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100726static void perf_counter_cpu_sched_out(struct perf_cpu_context *cpuctx)
Ingo Molnar04289bb2008-12-11 08:38:42 +0100727{
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100728 __perf_counter_sched_out(&cpuctx->ctx, cpuctx);
Ingo Molnar04289bb2008-12-11 08:38:42 +0100729}
730
Ingo Molnar79958882008-12-17 08:54:56 +0100731static int
Ingo Molnar04289bb2008-12-11 08:38:42 +0100732group_sched_in(struct perf_counter *group_counter,
733 struct perf_cpu_context *cpuctx,
734 struct perf_counter_context *ctx,
735 int cpu)
736{
Ingo Molnar95cdd2e2008-12-21 13:50:42 +0100737 struct perf_counter *counter, *partial_group;
Paul Mackerras3cbed422009-01-09 16:43:42 +1100738 int ret;
739
740 if (group_counter->state == PERF_COUNTER_STATE_OFF)
741 return 0;
742
743 ret = hw_perf_group_sched_in(group_counter, cpuctx, ctx, cpu);
744 if (ret)
745 return ret < 0 ? ret : 0;
Ingo Molnar04289bb2008-12-11 08:38:42 +0100746
Paul Mackerrasc07c99b2009-02-13 22:10:34 +1100747 group_counter->prev_state = group_counter->state;
Ingo Molnar95cdd2e2008-12-21 13:50:42 +0100748 if (counter_sched_in(group_counter, cpuctx, ctx, cpu))
749 return -EAGAIN;
Ingo Molnar04289bb2008-12-11 08:38:42 +0100750
751 /*
752 * Schedule in siblings as one group (if any):
753 */
Ingo Molnar79958882008-12-17 08:54:56 +0100754 list_for_each_entry(counter, &group_counter->sibling_list, list_entry) {
Paul Mackerrasc07c99b2009-02-13 22:10:34 +1100755 counter->prev_state = counter->state;
Ingo Molnar95cdd2e2008-12-21 13:50:42 +0100756 if (counter_sched_in(counter, cpuctx, ctx, cpu)) {
757 partial_group = counter;
758 goto group_error;
759 }
Ingo Molnar79958882008-12-17 08:54:56 +0100760 }
761
Paul Mackerras3cbed422009-01-09 16:43:42 +1100762 return 0;
Ingo Molnar95cdd2e2008-12-21 13:50:42 +0100763
764group_error:
765 /*
766 * Groups can be scheduled in as one unit only, so undo any
767 * partial group before returning:
768 */
769 list_for_each_entry(counter, &group_counter->sibling_list, list_entry) {
770 if (counter == partial_group)
771 break;
772 counter_sched_out(counter, cpuctx, ctx);
773 }
774 counter_sched_out(group_counter, cpuctx, ctx);
775
776 return -EAGAIN;
Ingo Molnar04289bb2008-12-11 08:38:42 +0100777}
778
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100779static void
780__perf_counter_sched_in(struct perf_counter_context *ctx,
781 struct perf_cpu_context *cpuctx, int cpu)
Thomas Gleixner0793a612008-12-04 20:12:29 +0100782{
Thomas Gleixner0793a612008-12-04 20:12:29 +0100783 struct perf_counter *counter;
Paul Mackerras3cbed422009-01-09 16:43:42 +1100784 u64 flags;
Paul Mackerrasdd0e6ba2009-01-12 15:11:00 +1100785 int can_add_hw = 1;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100786
Thomas Gleixner0793a612008-12-04 20:12:29 +0100787 spin_lock(&ctx->lock);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100788 ctx->is_active = 1;
789 if (likely(!ctx->nr_counters))
790 goto out;
791
Paul Mackerras3cbed422009-01-09 16:43:42 +1100792 flags = hw_perf_save_disable();
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100793
794 /*
795 * First go through the list and put on any pinned groups
796 * in order to give them the best chance of going on.
797 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100798 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100799 if (counter->state <= PERF_COUNTER_STATE_OFF ||
800 !counter->hw_event.pinned)
801 continue;
802 if (counter->cpu != -1 && counter->cpu != cpu)
803 continue;
804
805 if (group_can_go_on(counter, cpuctx, 1))
806 group_sched_in(counter, cpuctx, ctx, cpu);
807
808 /*
809 * If this pinned group hasn't been scheduled,
810 * put it in error state.
811 */
812 if (counter->state == PERF_COUNTER_STATE_INACTIVE)
813 counter->state = PERF_COUNTER_STATE_ERROR;
814 }
815
816 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
817 /*
818 * Ignore counters in OFF or ERROR state, and
819 * ignore pinned counters since we did them already.
820 */
821 if (counter->state <= PERF_COUNTER_STATE_OFF ||
822 counter->hw_event.pinned)
823 continue;
824
Ingo Molnar04289bb2008-12-11 08:38:42 +0100825 /*
826 * Listen to the 'cpu' scheduling filter constraint
827 * of counters:
828 */
Thomas Gleixner0793a612008-12-04 20:12:29 +0100829 if (counter->cpu != -1 && counter->cpu != cpu)
830 continue;
831
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100832 if (group_can_go_on(counter, cpuctx, can_add_hw)) {
Paul Mackerrasdd0e6ba2009-01-12 15:11:00 +1100833 if (group_sched_in(counter, cpuctx, ctx, cpu))
834 can_add_hw = 0;
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100835 }
Thomas Gleixner0793a612008-12-04 20:12:29 +0100836 }
Paul Mackerras3cbed422009-01-09 16:43:42 +1100837 hw_perf_restore(flags);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100838 out:
Thomas Gleixner0793a612008-12-04 20:12:29 +0100839 spin_unlock(&ctx->lock);
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100840}
Ingo Molnar04289bb2008-12-11 08:38:42 +0100841
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100842/*
843 * Called from scheduler to add the counters of the current task
844 * with interrupts disabled.
845 *
846 * We restore the counter value and then enable it.
847 *
848 * This does not protect us against NMI, but enable()
849 * sets the enabled bit in the control field of counter _before_
850 * accessing the counter control register. If a NMI hits, then it will
851 * keep the counter running.
852 */
853void perf_counter_task_sched_in(struct task_struct *task, int cpu)
854{
855 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
856 struct perf_counter_context *ctx = &task->perf_counter_ctx;
857
858 __perf_counter_sched_in(ctx, cpuctx, cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100859 cpuctx->task_ctx = ctx;
860}
861
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100862static void perf_counter_cpu_sched_in(struct perf_cpu_context *cpuctx, int cpu)
863{
864 struct perf_counter_context *ctx = &cpuctx->ctx;
865
866 __perf_counter_sched_in(ctx, cpuctx, cpu);
867}
868
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +0100869int perf_counter_task_disable(void)
870{
871 struct task_struct *curr = current;
872 struct perf_counter_context *ctx = &curr->perf_counter_ctx;
873 struct perf_counter *counter;
Ingo Molnaraa9c4c02008-12-17 14:10:57 +0100874 unsigned long flags;
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +0100875 u64 perf_flags;
876 int cpu;
877
878 if (likely(!ctx->nr_counters))
879 return 0;
880
Ingo Molnaraa9c4c02008-12-17 14:10:57 +0100881 curr_rq_lock_irq_save(&flags);
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +0100882 cpu = smp_processor_id();
883
Ingo Molnaraa9c4c02008-12-17 14:10:57 +0100884 /* force the update of the task clock: */
885 __task_delta_exec(curr, 1);
886
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +0100887 perf_counter_task_sched_out(curr, cpu);
888
889 spin_lock(&ctx->lock);
890
891 /*
892 * Disable all the counters:
893 */
894 perf_flags = hw_perf_save_disable();
895
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100896 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
897 if (counter->state != PERF_COUNTER_STATE_ERROR)
898 counter->state = PERF_COUNTER_STATE_OFF;
899 }
Ingo Molnar9b51f662008-12-12 13:49:45 +0100900
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +0100901 hw_perf_restore(perf_flags);
902
903 spin_unlock(&ctx->lock);
904
Ingo Molnaraa9c4c02008-12-17 14:10:57 +0100905 curr_rq_unlock_irq_restore(&flags);
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +0100906
907 return 0;
908}
909
910int perf_counter_task_enable(void)
911{
912 struct task_struct *curr = current;
913 struct perf_counter_context *ctx = &curr->perf_counter_ctx;
914 struct perf_counter *counter;
Ingo Molnaraa9c4c02008-12-17 14:10:57 +0100915 unsigned long flags;
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +0100916 u64 perf_flags;
917 int cpu;
918
919 if (likely(!ctx->nr_counters))
920 return 0;
921
Ingo Molnaraa9c4c02008-12-17 14:10:57 +0100922 curr_rq_lock_irq_save(&flags);
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +0100923 cpu = smp_processor_id();
924
Ingo Molnaraa9c4c02008-12-17 14:10:57 +0100925 /* force the update of the task clock: */
926 __task_delta_exec(curr, 1);
927
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100928 perf_counter_task_sched_out(curr, cpu);
929
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +0100930 spin_lock(&ctx->lock);
931
932 /*
933 * Disable all the counters:
934 */
935 perf_flags = hw_perf_save_disable();
936
937 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100938 if (counter->state > PERF_COUNTER_STATE_OFF)
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +0100939 continue;
Ingo Molnar6a930702008-12-11 15:17:03 +0100940 counter->state = PERF_COUNTER_STATE_INACTIVE;
Ingo Molnaraa9c4c02008-12-17 14:10:57 +0100941 counter->hw_event.disabled = 0;
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +0100942 }
943 hw_perf_restore(perf_flags);
944
945 spin_unlock(&ctx->lock);
946
947 perf_counter_task_sched_in(curr, cpu);
948
Ingo Molnaraa9c4c02008-12-17 14:10:57 +0100949 curr_rq_unlock_irq_restore(&flags);
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +0100950
951 return 0;
952}
953
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100954/*
955 * Round-robin a context's counters:
956 */
957static void rotate_ctx(struct perf_counter_context *ctx)
Thomas Gleixner0793a612008-12-04 20:12:29 +0100958{
Thomas Gleixner0793a612008-12-04 20:12:29 +0100959 struct perf_counter *counter;
Ingo Molnar5c92d122008-12-11 13:21:10 +0100960 u64 perf_flags;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100961
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100962 if (!ctx->nr_counters)
Thomas Gleixner0793a612008-12-04 20:12:29 +0100963 return;
964
Thomas Gleixner0793a612008-12-04 20:12:29 +0100965 spin_lock(&ctx->lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100966 /*
Ingo Molnar04289bb2008-12-11 08:38:42 +0100967 * Rotate the first entry last (works just fine for group counters too):
Thomas Gleixner0793a612008-12-04 20:12:29 +0100968 */
Ingo Molnar01b28382008-12-11 13:45:51 +0100969 perf_flags = hw_perf_save_disable();
Ingo Molnar04289bb2008-12-11 08:38:42 +0100970 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
Peter Zijlstra75564232009-03-13 12:21:29 +0100971 list_move_tail(&counter->list_entry, &ctx->counter_list);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100972 break;
973 }
Ingo Molnar01b28382008-12-11 13:45:51 +0100974 hw_perf_restore(perf_flags);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100975
976 spin_unlock(&ctx->lock);
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100977}
Thomas Gleixner0793a612008-12-04 20:12:29 +0100978
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100979void perf_counter_task_tick(struct task_struct *curr, int cpu)
980{
981 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
982 struct perf_counter_context *ctx = &curr->perf_counter_ctx;
983 const int rotate_percpu = 0;
984
985 if (rotate_percpu)
986 perf_counter_cpu_sched_out(cpuctx);
987 perf_counter_task_sched_out(curr, cpu);
988
989 if (rotate_percpu)
990 rotate_ctx(&cpuctx->ctx);
991 rotate_ctx(ctx);
992
993 if (rotate_percpu)
994 perf_counter_cpu_sched_in(cpuctx, cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100995 perf_counter_task_sched_in(curr, cpu);
996}
997
998/*
Thomas Gleixner0793a612008-12-04 20:12:29 +0100999 * Cross CPU call to read the hardware counter
1000 */
Ingo Molnar76715812008-12-17 14:20:28 +01001001static void __read(void *info)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001002{
Ingo Molnar621a01e2008-12-11 12:46:46 +01001003 struct perf_counter *counter = info;
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01001004 unsigned long flags;
Ingo Molnar621a01e2008-12-11 12:46:46 +01001005
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01001006 curr_rq_lock_irq_save(&flags);
Ingo Molnar76715812008-12-17 14:20:28 +01001007 counter->hw_ops->read(counter);
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01001008 curr_rq_unlock_irq_restore(&flags);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001009}
1010
Ingo Molnar04289bb2008-12-11 08:38:42 +01001011static u64 perf_counter_read(struct perf_counter *counter)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001012{
1013 /*
1014 * If counter is enabled and currently active on a CPU, update the
1015 * value in the counter structure:
1016 */
Ingo Molnar6a930702008-12-11 15:17:03 +01001017 if (counter->state == PERF_COUNTER_STATE_ACTIVE) {
Thomas Gleixner0793a612008-12-04 20:12:29 +01001018 smp_call_function_single(counter->oncpu,
Ingo Molnar76715812008-12-17 14:20:28 +01001019 __read, counter, 1);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001020 }
1021
Ingo Molnaree060942008-12-13 09:00:03 +01001022 return atomic64_read(&counter->count);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001023}
1024
1025/*
1026 * Cross CPU call to switch performance data pointers
1027 */
1028static void __perf_switch_irq_data(void *info)
1029{
1030 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
1031 struct perf_counter *counter = info;
1032 struct perf_counter_context *ctx = counter->ctx;
1033 struct perf_data *oldirqdata = counter->irqdata;
1034
1035 /*
1036 * If this is a task context, we need to check whether it is
1037 * the current task context of this cpu. If not it has been
1038 * scheduled out before the smp call arrived.
1039 */
1040 if (ctx->task) {
1041 if (cpuctx->task_ctx != ctx)
1042 return;
1043 spin_lock(&ctx->lock);
1044 }
1045
1046 /* Change the pointer NMI safe */
1047 atomic_long_set((atomic_long_t *)&counter->irqdata,
1048 (unsigned long) counter->usrdata);
1049 counter->usrdata = oldirqdata;
1050
1051 if (ctx->task)
1052 spin_unlock(&ctx->lock);
1053}
1054
1055static struct perf_data *perf_switch_irq_data(struct perf_counter *counter)
1056{
1057 struct perf_counter_context *ctx = counter->ctx;
1058 struct perf_data *oldirqdata = counter->irqdata;
1059 struct task_struct *task = ctx->task;
1060
1061 if (!task) {
1062 smp_call_function_single(counter->cpu,
1063 __perf_switch_irq_data,
1064 counter, 1);
1065 return counter->usrdata;
1066 }
1067
1068retry:
1069 spin_lock_irq(&ctx->lock);
Ingo Molnar6a930702008-12-11 15:17:03 +01001070 if (counter->state != PERF_COUNTER_STATE_ACTIVE) {
Thomas Gleixner0793a612008-12-04 20:12:29 +01001071 counter->irqdata = counter->usrdata;
1072 counter->usrdata = oldirqdata;
1073 spin_unlock_irq(&ctx->lock);
1074 return oldirqdata;
1075 }
1076 spin_unlock_irq(&ctx->lock);
1077 task_oncpu_function_call(task, __perf_switch_irq_data, counter);
1078 /* Might have failed, because task was scheduled out */
1079 if (counter->irqdata == oldirqdata)
1080 goto retry;
1081
1082 return counter->usrdata;
1083}
1084
1085static void put_context(struct perf_counter_context *ctx)
1086{
1087 if (ctx->task)
1088 put_task_struct(ctx->task);
1089}
1090
1091static struct perf_counter_context *find_get_context(pid_t pid, int cpu)
1092{
1093 struct perf_cpu_context *cpuctx;
1094 struct perf_counter_context *ctx;
1095 struct task_struct *task;
1096
1097 /*
1098 * If cpu is not a wildcard then this is a percpu counter:
1099 */
1100 if (cpu != -1) {
1101 /* Must be root to operate on a CPU counter: */
1102 if (!capable(CAP_SYS_ADMIN))
1103 return ERR_PTR(-EACCES);
1104
1105 if (cpu < 0 || cpu > num_possible_cpus())
1106 return ERR_PTR(-EINVAL);
1107
1108 /*
1109 * We could be clever and allow to attach a counter to an
1110 * offline CPU and activate it when the CPU comes up, but
1111 * that's for later.
1112 */
1113 if (!cpu_isset(cpu, cpu_online_map))
1114 return ERR_PTR(-ENODEV);
1115
1116 cpuctx = &per_cpu(perf_cpu_context, cpu);
1117 ctx = &cpuctx->ctx;
1118
Thomas Gleixner0793a612008-12-04 20:12:29 +01001119 return ctx;
1120 }
1121
1122 rcu_read_lock();
1123 if (!pid)
1124 task = current;
1125 else
1126 task = find_task_by_vpid(pid);
1127 if (task)
1128 get_task_struct(task);
1129 rcu_read_unlock();
1130
1131 if (!task)
1132 return ERR_PTR(-ESRCH);
1133
1134 ctx = &task->perf_counter_ctx;
1135 ctx->task = task;
1136
1137 /* Reuse ptrace permission checks for now. */
1138 if (!ptrace_may_access(task, PTRACE_MODE_READ)) {
1139 put_context(ctx);
1140 return ERR_PTR(-EACCES);
1141 }
1142
1143 return ctx;
1144}
1145
Peter Zijlstra592903c2009-03-13 12:21:36 +01001146static void free_counter_rcu(struct rcu_head *head)
1147{
1148 struct perf_counter *counter;
1149
1150 counter = container_of(head, struct perf_counter, rcu_head);
1151 kfree(counter);
1152}
1153
Peter Zijlstraf1600952009-03-19 20:26:16 +01001154static void free_counter(struct perf_counter *counter)
1155{
Peter Zijlstrae077df42009-03-19 20:26:17 +01001156 if (counter->destroy)
1157 counter->destroy(counter);
1158
Peter Zijlstraf1600952009-03-19 20:26:16 +01001159 call_rcu(&counter->rcu_head, free_counter_rcu);
1160}
1161
Thomas Gleixner0793a612008-12-04 20:12:29 +01001162/*
1163 * Called when the last reference to the file is gone.
1164 */
1165static int perf_release(struct inode *inode, struct file *file)
1166{
1167 struct perf_counter *counter = file->private_data;
1168 struct perf_counter_context *ctx = counter->ctx;
1169
1170 file->private_data = NULL;
1171
Paul Mackerrasd859e292009-01-17 18:10:22 +11001172 mutex_lock(&ctx->mutex);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001173 mutex_lock(&counter->mutex);
1174
Ingo Molnar04289bb2008-12-11 08:38:42 +01001175 perf_counter_remove_from_context(counter);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001176
1177 mutex_unlock(&counter->mutex);
Paul Mackerrasd859e292009-01-17 18:10:22 +11001178 mutex_unlock(&ctx->mutex);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001179
Paul Mackerras37d81822009-03-23 18:22:08 +01001180 free_page(counter->user_page);
Peter Zijlstraf1600952009-03-19 20:26:16 +01001181 free_counter(counter);
Mike Galbraith5af75912009-02-11 10:53:37 +01001182 put_context(ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001183
1184 return 0;
1185}
1186
1187/*
1188 * Read the performance counter - simple non blocking version for now
1189 */
1190static ssize_t
1191perf_read_hw(struct perf_counter *counter, char __user *buf, size_t count)
1192{
1193 u64 cntval;
1194
1195 if (count != sizeof(cntval))
1196 return -EINVAL;
1197
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001198 /*
1199 * Return end-of-file for a read on a counter that is in
1200 * error state (i.e. because it was pinned but it couldn't be
1201 * scheduled on to the CPU at some point).
1202 */
1203 if (counter->state == PERF_COUNTER_STATE_ERROR)
1204 return 0;
1205
Thomas Gleixner0793a612008-12-04 20:12:29 +01001206 mutex_lock(&counter->mutex);
Ingo Molnar04289bb2008-12-11 08:38:42 +01001207 cntval = perf_counter_read(counter);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001208 mutex_unlock(&counter->mutex);
1209
1210 return put_user(cntval, (u64 __user *) buf) ? -EFAULT : sizeof(cntval);
1211}
1212
1213static ssize_t
1214perf_copy_usrdata(struct perf_data *usrdata, char __user *buf, size_t count)
1215{
1216 if (!usrdata->len)
1217 return 0;
1218
1219 count = min(count, (size_t)usrdata->len);
1220 if (copy_to_user(buf, usrdata->data + usrdata->rd_idx, count))
1221 return -EFAULT;
1222
1223 /* Adjust the counters */
1224 usrdata->len -= count;
1225 if (!usrdata->len)
1226 usrdata->rd_idx = 0;
1227 else
1228 usrdata->rd_idx += count;
1229
1230 return count;
1231}
1232
1233static ssize_t
1234perf_read_irq_data(struct perf_counter *counter,
1235 char __user *buf,
1236 size_t count,
1237 int nonblocking)
1238{
1239 struct perf_data *irqdata, *usrdata;
1240 DECLARE_WAITQUEUE(wait, current);
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001241 ssize_t res, res2;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001242
1243 irqdata = counter->irqdata;
1244 usrdata = counter->usrdata;
1245
1246 if (usrdata->len + irqdata->len >= count)
1247 goto read_pending;
1248
1249 if (nonblocking)
1250 return -EAGAIN;
1251
1252 spin_lock_irq(&counter->waitq.lock);
1253 __add_wait_queue(&counter->waitq, &wait);
1254 for (;;) {
1255 set_current_state(TASK_INTERRUPTIBLE);
1256 if (usrdata->len + irqdata->len >= count)
1257 break;
1258
1259 if (signal_pending(current))
1260 break;
1261
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001262 if (counter->state == PERF_COUNTER_STATE_ERROR)
1263 break;
1264
Thomas Gleixner0793a612008-12-04 20:12:29 +01001265 spin_unlock_irq(&counter->waitq.lock);
1266 schedule();
1267 spin_lock_irq(&counter->waitq.lock);
1268 }
1269 __remove_wait_queue(&counter->waitq, &wait);
1270 __set_current_state(TASK_RUNNING);
1271 spin_unlock_irq(&counter->waitq.lock);
1272
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001273 if (usrdata->len + irqdata->len < count &&
1274 counter->state != PERF_COUNTER_STATE_ERROR)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001275 return -ERESTARTSYS;
1276read_pending:
1277 mutex_lock(&counter->mutex);
1278
1279 /* Drain pending data first: */
1280 res = perf_copy_usrdata(usrdata, buf, count);
1281 if (res < 0 || res == count)
1282 goto out;
1283
1284 /* Switch irq buffer: */
1285 usrdata = perf_switch_irq_data(counter);
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001286 res2 = perf_copy_usrdata(usrdata, buf + res, count - res);
1287 if (res2 < 0) {
Thomas Gleixner0793a612008-12-04 20:12:29 +01001288 if (!res)
1289 res = -EFAULT;
1290 } else {
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001291 res += res2;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001292 }
1293out:
1294 mutex_unlock(&counter->mutex);
1295
1296 return res;
1297}
1298
1299static ssize_t
1300perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
1301{
1302 struct perf_counter *counter = file->private_data;
1303
Ingo Molnar9f66a382008-12-10 12:33:23 +01001304 switch (counter->hw_event.record_type) {
Thomas Gleixner0793a612008-12-04 20:12:29 +01001305 case PERF_RECORD_SIMPLE:
1306 return perf_read_hw(counter, buf, count);
1307
1308 case PERF_RECORD_IRQ:
1309 case PERF_RECORD_GROUP:
1310 return perf_read_irq_data(counter, buf, count,
1311 file->f_flags & O_NONBLOCK);
1312 }
1313 return -EINVAL;
1314}
1315
1316static unsigned int perf_poll(struct file *file, poll_table *wait)
1317{
1318 struct perf_counter *counter = file->private_data;
1319 unsigned int events = 0;
1320 unsigned long flags;
1321
1322 poll_wait(file, &counter->waitq, wait);
1323
1324 spin_lock_irqsave(&counter->waitq.lock, flags);
1325 if (counter->usrdata->len || counter->irqdata->len)
1326 events |= POLLIN;
1327 spin_unlock_irqrestore(&counter->waitq.lock, flags);
1328
1329 return events;
1330}
1331
Paul Mackerrasd859e292009-01-17 18:10:22 +11001332static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1333{
1334 struct perf_counter *counter = file->private_data;
1335 int err = 0;
1336
1337 switch (cmd) {
1338 case PERF_COUNTER_IOC_ENABLE:
1339 perf_counter_enable_family(counter);
1340 break;
1341 case PERF_COUNTER_IOC_DISABLE:
1342 perf_counter_disable_family(counter);
1343 break;
1344 default:
1345 err = -ENOTTY;
1346 }
1347 return err;
1348}
1349
Paul Mackerras37d81822009-03-23 18:22:08 +01001350void perf_counter_update_userpage(struct perf_counter *counter)
1351{
1352 struct perf_counter_mmap_page *userpg;
1353
1354 if (!counter->user_page)
1355 return;
1356 userpg = (struct perf_counter_mmap_page *) counter->user_page;
1357
1358 ++userpg->lock;
1359 smp_wmb();
1360 userpg->index = counter->hw.idx;
1361 userpg->offset = atomic64_read(&counter->count);
1362 if (counter->state == PERF_COUNTER_STATE_ACTIVE)
1363 userpg->offset -= atomic64_read(&counter->hw.prev_count);
1364 smp_wmb();
1365 ++userpg->lock;
1366}
1367
1368static int perf_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1369{
1370 struct perf_counter *counter = vma->vm_file->private_data;
1371
1372 if (!counter->user_page)
1373 return VM_FAULT_SIGBUS;
1374
1375 vmf->page = virt_to_page(counter->user_page);
1376 get_page(vmf->page);
1377 return 0;
1378}
1379
1380static struct vm_operations_struct perf_mmap_vmops = {
1381 .fault = perf_mmap_fault,
1382};
1383
1384static int perf_mmap(struct file *file, struct vm_area_struct *vma)
1385{
1386 struct perf_counter *counter = file->private_data;
1387 unsigned long userpg;
1388
1389 if (!(vma->vm_flags & VM_SHARED) || (vma->vm_flags & VM_WRITE))
1390 return -EINVAL;
1391 if (vma->vm_end - vma->vm_start != PAGE_SIZE)
1392 return -EINVAL;
1393
1394 /*
1395 * For now, restrict to the case of a hardware counter
1396 * on the current task.
1397 */
1398 if (is_software_counter(counter) || counter->task != current)
1399 return -EINVAL;
1400
1401 userpg = counter->user_page;
1402 if (!userpg) {
1403 userpg = get_zeroed_page(GFP_KERNEL);
1404 mutex_lock(&counter->mutex);
1405 if (counter->user_page) {
1406 free_page(userpg);
1407 userpg = counter->user_page;
1408 } else {
1409 counter->user_page = userpg;
1410 }
1411 mutex_unlock(&counter->mutex);
1412 if (!userpg)
1413 return -ENOMEM;
1414 }
1415
1416 perf_counter_update_userpage(counter);
1417
1418 vma->vm_flags &= ~VM_MAYWRITE;
1419 vma->vm_flags |= VM_RESERVED;
1420 vma->vm_ops = &perf_mmap_vmops;
1421 return 0;
1422}
1423
Thomas Gleixner0793a612008-12-04 20:12:29 +01001424static const struct file_operations perf_fops = {
1425 .release = perf_release,
1426 .read = perf_read,
1427 .poll = perf_poll,
Paul Mackerrasd859e292009-01-17 18:10:22 +11001428 .unlocked_ioctl = perf_ioctl,
1429 .compat_ioctl = perf_ioctl,
Paul Mackerras37d81822009-03-23 18:22:08 +01001430 .mmap = perf_mmap,
Thomas Gleixner0793a612008-12-04 20:12:29 +01001431};
1432
Peter Zijlstra15dbf272009-03-13 12:21:32 +01001433/*
Peter Zijlstra0322cd62009-03-19 20:26:19 +01001434 * Output
1435 */
1436
1437static void perf_counter_store_irq(struct perf_counter *counter, u64 data)
1438{
1439 struct perf_data *irqdata = counter->irqdata;
1440
1441 if (irqdata->len > PERF_DATA_BUFLEN - sizeof(u64)) {
1442 irqdata->overrun++;
1443 } else {
1444 u64 *p = (u64 *) &irqdata->data[irqdata->len];
1445
1446 *p = data;
1447 irqdata->len += sizeof(u64);
1448 }
1449}
1450
1451static void perf_counter_handle_group(struct perf_counter *counter)
1452{
1453 struct perf_counter *leader, *sub;
1454
1455 leader = counter->group_leader;
1456 list_for_each_entry(sub, &leader->sibling_list, list_entry) {
1457 if (sub != counter)
1458 sub->hw_ops->read(sub);
Peter Zijlstraf4a2deb2009-03-23 18:22:06 +01001459 perf_counter_store_irq(counter, sub->hw_event.config);
Peter Zijlstra0322cd62009-03-19 20:26:19 +01001460 perf_counter_store_irq(counter, atomic64_read(&sub->count));
1461 }
1462}
1463
1464void perf_counter_output(struct perf_counter *counter,
1465 int nmi, struct pt_regs *regs)
1466{
1467 switch (counter->hw_event.record_type) {
1468 case PERF_RECORD_SIMPLE:
1469 return;
1470
1471 case PERF_RECORD_IRQ:
1472 perf_counter_store_irq(counter, instruction_pointer(regs));
1473 break;
1474
1475 case PERF_RECORD_GROUP:
1476 perf_counter_handle_group(counter);
1477 break;
1478 }
1479
1480 if (nmi) {
1481 counter->wakeup_pending = 1;
1482 set_perf_counter_pending();
1483 } else
1484 wake_up(&counter->waitq);
1485}
1486
1487/*
Peter Zijlstra15dbf272009-03-13 12:21:32 +01001488 * Generic software counter infrastructure
1489 */
1490
1491static void perf_swcounter_update(struct perf_counter *counter)
1492{
1493 struct hw_perf_counter *hwc = &counter->hw;
1494 u64 prev, now;
1495 s64 delta;
1496
1497again:
1498 prev = atomic64_read(&hwc->prev_count);
1499 now = atomic64_read(&hwc->count);
1500 if (atomic64_cmpxchg(&hwc->prev_count, prev, now) != prev)
1501 goto again;
1502
1503 delta = now - prev;
1504
1505 atomic64_add(delta, &counter->count);
1506 atomic64_sub(delta, &hwc->period_left);
1507}
1508
1509static void perf_swcounter_set_period(struct perf_counter *counter)
1510{
1511 struct hw_perf_counter *hwc = &counter->hw;
1512 s64 left = atomic64_read(&hwc->period_left);
1513 s64 period = hwc->irq_period;
1514
1515 if (unlikely(left <= -period)) {
1516 left = period;
1517 atomic64_set(&hwc->period_left, left);
1518 }
1519
1520 if (unlikely(left <= 0)) {
1521 left += period;
1522 atomic64_add(period, &hwc->period_left);
1523 }
1524
1525 atomic64_set(&hwc->prev_count, -left);
1526 atomic64_set(&hwc->count, -left);
1527}
1528
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01001529static enum hrtimer_restart perf_swcounter_hrtimer(struct hrtimer *hrtimer)
1530{
1531 struct perf_counter *counter;
1532 struct pt_regs *regs;
1533
1534 counter = container_of(hrtimer, struct perf_counter, hw.hrtimer);
1535 counter->hw_ops->read(counter);
1536
1537 regs = get_irq_regs();
1538 /*
1539 * In case we exclude kernel IPs or are somehow not in interrupt
1540 * context, provide the next best thing, the user IP.
1541 */
1542 if ((counter->hw_event.exclude_kernel || !regs) &&
1543 !counter->hw_event.exclude_user)
1544 regs = task_pt_regs(current);
1545
1546 if (regs)
Peter Zijlstra0322cd62009-03-19 20:26:19 +01001547 perf_counter_output(counter, 0, regs);
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01001548
1549 hrtimer_forward_now(hrtimer, ns_to_ktime(counter->hw.irq_period));
1550
1551 return HRTIMER_RESTART;
1552}
1553
1554static void perf_swcounter_overflow(struct perf_counter *counter,
1555 int nmi, struct pt_regs *regs)
1556{
Peter Zijlstrab8e83512009-03-19 20:26:18 +01001557 perf_swcounter_update(counter);
1558 perf_swcounter_set_period(counter);
Peter Zijlstra0322cd62009-03-19 20:26:19 +01001559 perf_counter_output(counter, nmi, regs);
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01001560}
1561
Peter Zijlstra15dbf272009-03-13 12:21:32 +01001562static int perf_swcounter_match(struct perf_counter *counter,
Peter Zijlstrab8e83512009-03-19 20:26:18 +01001563 enum perf_event_types type,
1564 u32 event, struct pt_regs *regs)
Peter Zijlstra15dbf272009-03-13 12:21:32 +01001565{
1566 if (counter->state != PERF_COUNTER_STATE_ACTIVE)
1567 return 0;
1568
Peter Zijlstraf4a2deb2009-03-23 18:22:06 +01001569 if (perf_event_raw(&counter->hw_event))
Peter Zijlstra15dbf272009-03-13 12:21:32 +01001570 return 0;
1571
Peter Zijlstraf4a2deb2009-03-23 18:22:06 +01001572 if (perf_event_type(&counter->hw_event) != type)
Peter Zijlstrab8e83512009-03-19 20:26:18 +01001573 return 0;
1574
Peter Zijlstraf4a2deb2009-03-23 18:22:06 +01001575 if (perf_event_id(&counter->hw_event) != event)
Peter Zijlstra15dbf272009-03-13 12:21:32 +01001576 return 0;
1577
1578 if (counter->hw_event.exclude_user && user_mode(regs))
1579 return 0;
1580
1581 if (counter->hw_event.exclude_kernel && !user_mode(regs))
1582 return 0;
1583
1584 return 1;
1585}
1586
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01001587static void perf_swcounter_add(struct perf_counter *counter, u64 nr,
1588 int nmi, struct pt_regs *regs)
1589{
1590 int neg = atomic64_add_negative(nr, &counter->hw.count);
1591 if (counter->hw.irq_period && !neg)
1592 perf_swcounter_overflow(counter, nmi, regs);
1593}
1594
Peter Zijlstra15dbf272009-03-13 12:21:32 +01001595static void perf_swcounter_ctx_event(struct perf_counter_context *ctx,
Peter Zijlstrab8e83512009-03-19 20:26:18 +01001596 enum perf_event_types type, u32 event,
1597 u64 nr, int nmi, struct pt_regs *regs)
Peter Zijlstra15dbf272009-03-13 12:21:32 +01001598{
1599 struct perf_counter *counter;
Peter Zijlstra15dbf272009-03-13 12:21:32 +01001600
Peter Zijlstra01ef09d2009-03-19 20:26:11 +01001601 if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
Peter Zijlstra15dbf272009-03-13 12:21:32 +01001602 return;
1603
Peter Zijlstra592903c2009-03-13 12:21:36 +01001604 rcu_read_lock();
1605 list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
Peter Zijlstrab8e83512009-03-19 20:26:18 +01001606 if (perf_swcounter_match(counter, type, event, regs))
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01001607 perf_swcounter_add(counter, nr, nmi, regs);
Peter Zijlstra15dbf272009-03-13 12:21:32 +01001608 }
Peter Zijlstra592903c2009-03-13 12:21:36 +01001609 rcu_read_unlock();
Peter Zijlstra15dbf272009-03-13 12:21:32 +01001610}
1611
Peter Zijlstra96f6d442009-03-23 18:22:07 +01001612static int *perf_swcounter_recursion_context(struct perf_cpu_context *cpuctx)
1613{
1614 if (in_nmi())
1615 return &cpuctx->recursion[3];
1616
1617 if (in_irq())
1618 return &cpuctx->recursion[2];
1619
1620 if (in_softirq())
1621 return &cpuctx->recursion[1];
1622
1623 return &cpuctx->recursion[0];
1624}
1625
Peter Zijlstrab8e83512009-03-19 20:26:18 +01001626static void __perf_swcounter_event(enum perf_event_types type, u32 event,
1627 u64 nr, int nmi, struct pt_regs *regs)
Peter Zijlstra15dbf272009-03-13 12:21:32 +01001628{
1629 struct perf_cpu_context *cpuctx = &get_cpu_var(perf_cpu_context);
Peter Zijlstra96f6d442009-03-23 18:22:07 +01001630 int *recursion = perf_swcounter_recursion_context(cpuctx);
1631
1632 if (*recursion)
1633 goto out;
1634
1635 (*recursion)++;
1636 barrier();
Peter Zijlstra15dbf272009-03-13 12:21:32 +01001637
Peter Zijlstrab8e83512009-03-19 20:26:18 +01001638 perf_swcounter_ctx_event(&cpuctx->ctx, type, event, nr, nmi, regs);
1639 if (cpuctx->task_ctx) {
1640 perf_swcounter_ctx_event(cpuctx->task_ctx, type, event,
1641 nr, nmi, regs);
1642 }
Peter Zijlstra15dbf272009-03-13 12:21:32 +01001643
Peter Zijlstra96f6d442009-03-23 18:22:07 +01001644 barrier();
1645 (*recursion)--;
1646
1647out:
Peter Zijlstra15dbf272009-03-13 12:21:32 +01001648 put_cpu_var(perf_cpu_context);
1649}
1650
Peter Zijlstrab8e83512009-03-19 20:26:18 +01001651void perf_swcounter_event(u32 event, u64 nr, int nmi, struct pt_regs *regs)
1652{
1653 __perf_swcounter_event(PERF_TYPE_SOFTWARE, event, nr, nmi, regs);
1654}
1655
Peter Zijlstra15dbf272009-03-13 12:21:32 +01001656static void perf_swcounter_read(struct perf_counter *counter)
1657{
1658 perf_swcounter_update(counter);
1659}
1660
1661static int perf_swcounter_enable(struct perf_counter *counter)
1662{
1663 perf_swcounter_set_period(counter);
1664 return 0;
1665}
1666
1667static void perf_swcounter_disable(struct perf_counter *counter)
1668{
1669 perf_swcounter_update(counter);
1670}
1671
Peter Zijlstraac17dc82009-03-13 12:21:34 +01001672static const struct hw_perf_counter_ops perf_ops_generic = {
1673 .enable = perf_swcounter_enable,
1674 .disable = perf_swcounter_disable,
1675 .read = perf_swcounter_read,
1676};
1677
Peter Zijlstra15dbf272009-03-13 12:21:32 +01001678/*
1679 * Software counter: cpu wall time clock
1680 */
1681
Paul Mackerras9abf8a02009-01-09 16:26:43 +11001682static void cpu_clock_perf_counter_update(struct perf_counter *counter)
1683{
1684 int cpu = raw_smp_processor_id();
1685 s64 prev;
1686 u64 now;
1687
1688 now = cpu_clock(cpu);
1689 prev = atomic64_read(&counter->hw.prev_count);
1690 atomic64_set(&counter->hw.prev_count, now);
1691 atomic64_add(now - prev, &counter->count);
1692}
1693
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01001694static int cpu_clock_perf_counter_enable(struct perf_counter *counter)
1695{
1696 struct hw_perf_counter *hwc = &counter->hw;
1697 int cpu = raw_smp_processor_id();
1698
1699 atomic64_set(&hwc->prev_count, cpu_clock(cpu));
Peter Zijlstra039fc912009-03-13 16:43:47 +01001700 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1701 hwc->hrtimer.function = perf_swcounter_hrtimer;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01001702 if (hwc->irq_period) {
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01001703 __hrtimer_start_range_ns(&hwc->hrtimer,
1704 ns_to_ktime(hwc->irq_period), 0,
1705 HRTIMER_MODE_REL, 0);
1706 }
1707
1708 return 0;
1709}
1710
Ingo Molnar5c92d122008-12-11 13:21:10 +01001711static void cpu_clock_perf_counter_disable(struct perf_counter *counter)
1712{
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01001713 hrtimer_cancel(&counter->hw.hrtimer);
Paul Mackerras9abf8a02009-01-09 16:26:43 +11001714 cpu_clock_perf_counter_update(counter);
Ingo Molnar5c92d122008-12-11 13:21:10 +01001715}
1716
1717static void cpu_clock_perf_counter_read(struct perf_counter *counter)
1718{
Paul Mackerras9abf8a02009-01-09 16:26:43 +11001719 cpu_clock_perf_counter_update(counter);
Ingo Molnar5c92d122008-12-11 13:21:10 +01001720}
1721
1722static const struct hw_perf_counter_ops perf_ops_cpu_clock = {
Ingo Molnar76715812008-12-17 14:20:28 +01001723 .enable = cpu_clock_perf_counter_enable,
1724 .disable = cpu_clock_perf_counter_disable,
1725 .read = cpu_clock_perf_counter_read,
Ingo Molnar5c92d122008-12-11 13:21:10 +01001726};
1727
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01001728/*
Peter Zijlstra15dbf272009-03-13 12:21:32 +01001729 * Software counter: task time clock
1730 */
1731
1732/*
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01001733 * Called from within the scheduler:
1734 */
1735static u64 task_clock_perf_counter_val(struct perf_counter *counter, int update)
Ingo Molnarbae43c92008-12-11 14:03:20 +01001736{
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01001737 struct task_struct *curr = counter->task;
1738 u64 delta;
1739
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01001740 delta = __task_delta_exec(curr, update);
1741
1742 return curr->se.sum_exec_runtime + delta;
1743}
1744
1745static void task_clock_perf_counter_update(struct perf_counter *counter, u64 now)
1746{
1747 u64 prev;
Ingo Molnar8cb391e2008-12-14 12:22:31 +01001748 s64 delta;
Ingo Molnarbae43c92008-12-11 14:03:20 +01001749
Ingo Molnar8cb391e2008-12-14 12:22:31 +01001750 prev = atomic64_read(&counter->hw.prev_count);
Ingo Molnar8cb391e2008-12-14 12:22:31 +01001751
1752 atomic64_set(&counter->hw.prev_count, now);
1753
1754 delta = now - prev;
Ingo Molnar8cb391e2008-12-14 12:22:31 +01001755
1756 atomic64_add(delta, &counter->count);
Ingo Molnarbae43c92008-12-11 14:03:20 +01001757}
1758
Ingo Molnar95cdd2e2008-12-21 13:50:42 +01001759static int task_clock_perf_counter_enable(struct perf_counter *counter)
Ingo Molnar8cb391e2008-12-14 12:22:31 +01001760{
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01001761 struct hw_perf_counter *hwc = &counter->hw;
1762
1763 atomic64_set(&hwc->prev_count, task_clock_perf_counter_val(counter, 0));
Peter Zijlstra039fc912009-03-13 16:43:47 +01001764 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1765 hwc->hrtimer.function = perf_swcounter_hrtimer;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01001766 if (hwc->irq_period) {
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01001767 __hrtimer_start_range_ns(&hwc->hrtimer,
1768 ns_to_ktime(hwc->irq_period), 0,
1769 HRTIMER_MODE_REL, 0);
1770 }
Ingo Molnar95cdd2e2008-12-21 13:50:42 +01001771
1772 return 0;
Ingo Molnar8cb391e2008-12-14 12:22:31 +01001773}
1774
1775static void task_clock_perf_counter_disable(struct perf_counter *counter)
1776{
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01001777 hrtimer_cancel(&counter->hw.hrtimer);
1778 task_clock_perf_counter_update(counter,
1779 task_clock_perf_counter_val(counter, 0));
1780}
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01001781
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01001782static void task_clock_perf_counter_read(struct perf_counter *counter)
1783{
1784 task_clock_perf_counter_update(counter,
1785 task_clock_perf_counter_val(counter, 1));
Ingo Molnarbae43c92008-12-11 14:03:20 +01001786}
1787
1788static const struct hw_perf_counter_ops perf_ops_task_clock = {
Ingo Molnar76715812008-12-17 14:20:28 +01001789 .enable = task_clock_perf_counter_enable,
1790 .disable = task_clock_perf_counter_disable,
1791 .read = task_clock_perf_counter_read,
Ingo Molnarbae43c92008-12-11 14:03:20 +01001792};
1793
Peter Zijlstra15dbf272009-03-13 12:21:32 +01001794/*
Peter Zijlstra15dbf272009-03-13 12:21:32 +01001795 * Software counter: cpu migrations
1796 */
1797
Paul Mackerras23a185c2009-02-09 22:42:47 +11001798static inline u64 get_cpu_migrations(struct perf_counter *counter)
Ingo Molnar6c594c22008-12-14 12:34:15 +01001799{
Paul Mackerras23a185c2009-02-09 22:42:47 +11001800 struct task_struct *curr = counter->ctx->task;
1801
1802 if (curr)
1803 return curr->se.nr_migrations;
1804 return cpu_nr_migrations(smp_processor_id());
Ingo Molnar6c594c22008-12-14 12:34:15 +01001805}
1806
1807static void cpu_migrations_perf_counter_update(struct perf_counter *counter)
1808{
1809 u64 prev, now;
1810 s64 delta;
1811
1812 prev = atomic64_read(&counter->hw.prev_count);
Paul Mackerras23a185c2009-02-09 22:42:47 +11001813 now = get_cpu_migrations(counter);
Ingo Molnar6c594c22008-12-14 12:34:15 +01001814
1815 atomic64_set(&counter->hw.prev_count, now);
1816
1817 delta = now - prev;
Ingo Molnar6c594c22008-12-14 12:34:15 +01001818
1819 atomic64_add(delta, &counter->count);
1820}
1821
1822static void cpu_migrations_perf_counter_read(struct perf_counter *counter)
1823{
1824 cpu_migrations_perf_counter_update(counter);
1825}
1826
Ingo Molnar95cdd2e2008-12-21 13:50:42 +01001827static int cpu_migrations_perf_counter_enable(struct perf_counter *counter)
Ingo Molnar6c594c22008-12-14 12:34:15 +01001828{
Paul Mackerrasc07c99b2009-02-13 22:10:34 +11001829 if (counter->prev_state <= PERF_COUNTER_STATE_OFF)
1830 atomic64_set(&counter->hw.prev_count,
1831 get_cpu_migrations(counter));
Ingo Molnar95cdd2e2008-12-21 13:50:42 +01001832 return 0;
Ingo Molnar6c594c22008-12-14 12:34:15 +01001833}
1834
1835static void cpu_migrations_perf_counter_disable(struct perf_counter *counter)
1836{
1837 cpu_migrations_perf_counter_update(counter);
1838}
1839
1840static const struct hw_perf_counter_ops perf_ops_cpu_migrations = {
Ingo Molnar76715812008-12-17 14:20:28 +01001841 .enable = cpu_migrations_perf_counter_enable,
1842 .disable = cpu_migrations_perf_counter_disable,
1843 .read = cpu_migrations_perf_counter_read,
Ingo Molnar6c594c22008-12-14 12:34:15 +01001844};
1845
Peter Zijlstrae077df42009-03-19 20:26:17 +01001846#ifdef CONFIG_EVENT_PROFILE
1847void perf_tpcounter_event(int event_id)
1848{
Peter Zijlstrab8e83512009-03-19 20:26:18 +01001849 struct pt_regs *regs = get_irq_regs();
1850
1851 if (!regs)
1852 regs = task_pt_regs(current);
1853
1854 __perf_swcounter_event(PERF_TYPE_TRACEPOINT, event_id, 1, 1, regs);
Peter Zijlstrae077df42009-03-19 20:26:17 +01001855}
1856
1857extern int ftrace_profile_enable(int);
1858extern void ftrace_profile_disable(int);
1859
1860static void tp_perf_counter_destroy(struct perf_counter *counter)
1861{
Peter Zijlstraf4a2deb2009-03-23 18:22:06 +01001862 ftrace_profile_disable(perf_event_id(&counter->hw_event));
Peter Zijlstrae077df42009-03-19 20:26:17 +01001863}
1864
1865static const struct hw_perf_counter_ops *
1866tp_perf_counter_init(struct perf_counter *counter)
1867{
Peter Zijlstraf4a2deb2009-03-23 18:22:06 +01001868 int event_id = perf_event_id(&counter->hw_event);
Peter Zijlstrae077df42009-03-19 20:26:17 +01001869 int ret;
1870
1871 ret = ftrace_profile_enable(event_id);
1872 if (ret)
1873 return NULL;
1874
1875 counter->destroy = tp_perf_counter_destroy;
Peter Zijlstrab8e83512009-03-19 20:26:18 +01001876 counter->hw.irq_period = counter->hw_event.irq_period;
Peter Zijlstrae077df42009-03-19 20:26:17 +01001877
1878 return &perf_ops_generic;
1879}
1880#else
1881static const struct hw_perf_counter_ops *
1882tp_perf_counter_init(struct perf_counter *counter)
1883{
1884 return NULL;
1885}
1886#endif
1887
Ingo Molnar5c92d122008-12-11 13:21:10 +01001888static const struct hw_perf_counter_ops *
1889sw_perf_counter_init(struct perf_counter *counter)
1890{
Peter Zijlstra15dbf272009-03-13 12:21:32 +01001891 struct perf_counter_hw_event *hw_event = &counter->hw_event;
Ingo Molnar5c92d122008-12-11 13:21:10 +01001892 const struct hw_perf_counter_ops *hw_ops = NULL;
Peter Zijlstra15dbf272009-03-13 12:21:32 +01001893 struct hw_perf_counter *hwc = &counter->hw;
Ingo Molnar5c92d122008-12-11 13:21:10 +01001894
Paul Mackerras0475f9e2009-02-11 14:35:35 +11001895 /*
1896 * Software counters (currently) can't in general distinguish
1897 * between user, kernel and hypervisor events.
1898 * However, context switches and cpu migrations are considered
1899 * to be kernel events, and page faults are never hypervisor
1900 * events.
1901 */
Peter Zijlstraf4a2deb2009-03-23 18:22:06 +01001902 switch (perf_event_id(&counter->hw_event)) {
Ingo Molnar5c92d122008-12-11 13:21:10 +01001903 case PERF_COUNT_CPU_CLOCK:
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01001904 hw_ops = &perf_ops_cpu_clock;
1905
1906 if (hw_event->irq_period && hw_event->irq_period < 10000)
1907 hw_event->irq_period = 10000;
Ingo Molnar5c92d122008-12-11 13:21:10 +01001908 break;
Ingo Molnarbae43c92008-12-11 14:03:20 +01001909 case PERF_COUNT_TASK_CLOCK:
Paul Mackerras23a185c2009-02-09 22:42:47 +11001910 /*
1911 * If the user instantiates this as a per-cpu counter,
1912 * use the cpu_clock counter instead.
1913 */
1914 if (counter->ctx->task)
1915 hw_ops = &perf_ops_task_clock;
1916 else
1917 hw_ops = &perf_ops_cpu_clock;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01001918
1919 if (hw_event->irq_period && hw_event->irq_period < 10000)
1920 hw_event->irq_period = 10000;
Ingo Molnarbae43c92008-12-11 14:03:20 +01001921 break;
Ingo Molnare06c61a2008-12-14 14:44:31 +01001922 case PERF_COUNT_PAGE_FAULTS:
Peter Zijlstraac17dc82009-03-13 12:21:34 +01001923 case PERF_COUNT_PAGE_FAULTS_MIN:
1924 case PERF_COUNT_PAGE_FAULTS_MAJ:
Ingo Molnar5d6a27d2008-12-14 12:28:33 +01001925 case PERF_COUNT_CONTEXT_SWITCHES:
Peter Zijlstra4a0deca2009-03-19 20:26:12 +01001926 hw_ops = &perf_ops_generic;
Ingo Molnar5d6a27d2008-12-14 12:28:33 +01001927 break;
Ingo Molnar6c594c22008-12-14 12:34:15 +01001928 case PERF_COUNT_CPU_MIGRATIONS:
Paul Mackerras0475f9e2009-02-11 14:35:35 +11001929 if (!counter->hw_event.exclude_kernel)
1930 hw_ops = &perf_ops_cpu_migrations;
Ingo Molnar6c594c22008-12-14 12:34:15 +01001931 break;
Ingo Molnar5c92d122008-12-11 13:21:10 +01001932 }
Peter Zijlstra15dbf272009-03-13 12:21:32 +01001933
1934 if (hw_ops)
1935 hwc->irq_period = hw_event->irq_period;
1936
Ingo Molnar5c92d122008-12-11 13:21:10 +01001937 return hw_ops;
1938}
1939
Thomas Gleixner0793a612008-12-04 20:12:29 +01001940/*
1941 * Allocate and initialize a counter structure
1942 */
1943static struct perf_counter *
Ingo Molnar04289bb2008-12-11 08:38:42 +01001944perf_counter_alloc(struct perf_counter_hw_event *hw_event,
1945 int cpu,
Paul Mackerras23a185c2009-02-09 22:42:47 +11001946 struct perf_counter_context *ctx,
Ingo Molnar9b51f662008-12-12 13:49:45 +01001947 struct perf_counter *group_leader,
1948 gfp_t gfpflags)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001949{
Ingo Molnar5c92d122008-12-11 13:21:10 +01001950 const struct hw_perf_counter_ops *hw_ops;
Ingo Molnar621a01e2008-12-11 12:46:46 +01001951 struct perf_counter *counter;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001952
Ingo Molnar9b51f662008-12-12 13:49:45 +01001953 counter = kzalloc(sizeof(*counter), gfpflags);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001954 if (!counter)
1955 return NULL;
1956
Ingo Molnar04289bb2008-12-11 08:38:42 +01001957 /*
1958 * Single counters are their own group leaders, with an
1959 * empty sibling list:
1960 */
1961 if (!group_leader)
1962 group_leader = counter;
1963
Thomas Gleixner0793a612008-12-04 20:12:29 +01001964 mutex_init(&counter->mutex);
Ingo Molnar04289bb2008-12-11 08:38:42 +01001965 INIT_LIST_HEAD(&counter->list_entry);
Peter Zijlstra592903c2009-03-13 12:21:36 +01001966 INIT_LIST_HEAD(&counter->event_entry);
Ingo Molnar04289bb2008-12-11 08:38:42 +01001967 INIT_LIST_HEAD(&counter->sibling_list);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001968 init_waitqueue_head(&counter->waitq);
1969
Paul Mackerrasd859e292009-01-17 18:10:22 +11001970 INIT_LIST_HEAD(&counter->child_list);
1971
Ingo Molnar9f66a382008-12-10 12:33:23 +01001972 counter->irqdata = &counter->data[0];
1973 counter->usrdata = &counter->data[1];
1974 counter->cpu = cpu;
1975 counter->hw_event = *hw_event;
1976 counter->wakeup_pending = 0;
Ingo Molnar04289bb2008-12-11 08:38:42 +01001977 counter->group_leader = group_leader;
Ingo Molnar621a01e2008-12-11 12:46:46 +01001978 counter->hw_ops = NULL;
Paul Mackerras23a185c2009-02-09 22:42:47 +11001979 counter->ctx = ctx;
Ingo Molnar621a01e2008-12-11 12:46:46 +01001980
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001981 counter->state = PERF_COUNTER_STATE_INACTIVE;
Ingo Molnara86ed502008-12-17 00:43:10 +01001982 if (hw_event->disabled)
1983 counter->state = PERF_COUNTER_STATE_OFF;
1984
Ingo Molnar5c92d122008-12-11 13:21:10 +01001985 hw_ops = NULL;
Peter Zijlstrab8e83512009-03-19 20:26:18 +01001986
Peter Zijlstraf4a2deb2009-03-23 18:22:06 +01001987 if (perf_event_raw(hw_event)) {
Ingo Molnar5c92d122008-12-11 13:21:10 +01001988 hw_ops = hw_perf_counter_init(counter);
Peter Zijlstraf4a2deb2009-03-23 18:22:06 +01001989 goto done;
1990 }
1991
1992 switch (perf_event_type(hw_event)) {
Peter Zijlstrab8e83512009-03-19 20:26:18 +01001993 case PERF_TYPE_HARDWARE:
1994 hw_ops = hw_perf_counter_init(counter);
1995 break;
1996
1997 case PERF_TYPE_SOFTWARE:
1998 hw_ops = sw_perf_counter_init(counter);
1999 break;
2000
2001 case PERF_TYPE_TRACEPOINT:
2002 hw_ops = tp_perf_counter_init(counter);
2003 break;
2004 }
Ingo Molnar5c92d122008-12-11 13:21:10 +01002005
Ingo Molnar621a01e2008-12-11 12:46:46 +01002006 if (!hw_ops) {
2007 kfree(counter);
2008 return NULL;
2009 }
Peter Zijlstraf4a2deb2009-03-23 18:22:06 +01002010done:
Ingo Molnar621a01e2008-12-11 12:46:46 +01002011 counter->hw_ops = hw_ops;
Thomas Gleixner0793a612008-12-04 20:12:29 +01002012
2013 return counter;
2014}
2015
2016/**
Paul Mackerras2743a5b2009-03-04 20:36:51 +11002017 * sys_perf_counter_open - open a performance counter, associate it to a task/cpu
Ingo Molnar9f66a382008-12-10 12:33:23 +01002018 *
2019 * @hw_event_uptr: event type attributes for monitoring/sampling
Thomas Gleixner0793a612008-12-04 20:12:29 +01002020 * @pid: target pid
Ingo Molnar9f66a382008-12-10 12:33:23 +01002021 * @cpu: target cpu
2022 * @group_fd: group leader counter fd
Thomas Gleixner0793a612008-12-04 20:12:29 +01002023 */
Paul Mackerras2743a5b2009-03-04 20:36:51 +11002024SYSCALL_DEFINE5(perf_counter_open,
Paul Mackerrasf3dfd262009-02-26 22:43:46 +11002025 const struct perf_counter_hw_event __user *, hw_event_uptr,
Paul Mackerras2743a5b2009-03-04 20:36:51 +11002026 pid_t, pid, int, cpu, int, group_fd, unsigned long, flags)
Thomas Gleixner0793a612008-12-04 20:12:29 +01002027{
Ingo Molnar04289bb2008-12-11 08:38:42 +01002028 struct perf_counter *counter, *group_leader;
Ingo Molnar9f66a382008-12-10 12:33:23 +01002029 struct perf_counter_hw_event hw_event;
Ingo Molnar04289bb2008-12-11 08:38:42 +01002030 struct perf_counter_context *ctx;
Ingo Molnar9b51f662008-12-12 13:49:45 +01002031 struct file *counter_file = NULL;
Ingo Molnar04289bb2008-12-11 08:38:42 +01002032 struct file *group_file = NULL;
2033 int fput_needed = 0;
Ingo Molnar9b51f662008-12-12 13:49:45 +01002034 int fput_needed2 = 0;
Thomas Gleixner0793a612008-12-04 20:12:29 +01002035 int ret;
2036
Paul Mackerras2743a5b2009-03-04 20:36:51 +11002037 /* for future expandability... */
2038 if (flags)
2039 return -EINVAL;
2040
Ingo Molnar9f66a382008-12-10 12:33:23 +01002041 if (copy_from_user(&hw_event, hw_event_uptr, sizeof(hw_event)) != 0)
Thomas Gleixnereab656a2008-12-08 19:26:59 +01002042 return -EFAULT;
2043
Ingo Molnar04289bb2008-12-11 08:38:42 +01002044 /*
Ingo Molnarccff2862008-12-11 11:26:29 +01002045 * Get the target context (task or percpu):
2046 */
2047 ctx = find_get_context(pid, cpu);
2048 if (IS_ERR(ctx))
2049 return PTR_ERR(ctx);
2050
2051 /*
2052 * Look up the group leader (we will attach this counter to it):
Ingo Molnar04289bb2008-12-11 08:38:42 +01002053 */
2054 group_leader = NULL;
2055 if (group_fd != -1) {
2056 ret = -EINVAL;
2057 group_file = fget_light(group_fd, &fput_needed);
2058 if (!group_file)
Ingo Molnarccff2862008-12-11 11:26:29 +01002059 goto err_put_context;
Ingo Molnar04289bb2008-12-11 08:38:42 +01002060 if (group_file->f_op != &perf_fops)
Ingo Molnarccff2862008-12-11 11:26:29 +01002061 goto err_put_context;
Ingo Molnar04289bb2008-12-11 08:38:42 +01002062
2063 group_leader = group_file->private_data;
2064 /*
Ingo Molnarccff2862008-12-11 11:26:29 +01002065 * Do not allow a recursive hierarchy (this new sibling
2066 * becoming part of another group-sibling):
Ingo Molnar04289bb2008-12-11 08:38:42 +01002067 */
Ingo Molnarccff2862008-12-11 11:26:29 +01002068 if (group_leader->group_leader != group_leader)
2069 goto err_put_context;
2070 /*
2071 * Do not allow to attach to a group in a different
2072 * task or CPU context:
2073 */
2074 if (group_leader->ctx != ctx)
2075 goto err_put_context;
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11002076 /*
2077 * Only a group leader can be exclusive or pinned
2078 */
2079 if (hw_event.exclusive || hw_event.pinned)
2080 goto err_put_context;
Ingo Molnar04289bb2008-12-11 08:38:42 +01002081 }
2082
Ingo Molnar5c92d122008-12-11 13:21:10 +01002083 ret = -EINVAL;
Paul Mackerras23a185c2009-02-09 22:42:47 +11002084 counter = perf_counter_alloc(&hw_event, cpu, ctx, group_leader,
2085 GFP_KERNEL);
Thomas Gleixner0793a612008-12-04 20:12:29 +01002086 if (!counter)
2087 goto err_put_context;
2088
Thomas Gleixner0793a612008-12-04 20:12:29 +01002089 ret = anon_inode_getfd("[perf_counter]", &perf_fops, counter, 0);
2090 if (ret < 0)
Ingo Molnar9b51f662008-12-12 13:49:45 +01002091 goto err_free_put_context;
2092
2093 counter_file = fget_light(ret, &fput_needed2);
2094 if (!counter_file)
2095 goto err_free_put_context;
2096
2097 counter->filp = counter_file;
Paul Mackerrasd859e292009-01-17 18:10:22 +11002098 mutex_lock(&ctx->mutex);
Ingo Molnar9b51f662008-12-12 13:49:45 +01002099 perf_install_in_context(ctx, counter, cpu);
Paul Mackerrasd859e292009-01-17 18:10:22 +11002100 mutex_unlock(&ctx->mutex);
Ingo Molnar9b51f662008-12-12 13:49:45 +01002101
2102 fput_light(counter_file, fput_needed2);
Thomas Gleixner0793a612008-12-04 20:12:29 +01002103
Ingo Molnar04289bb2008-12-11 08:38:42 +01002104out_fput:
2105 fput_light(group_file, fput_needed);
2106
Thomas Gleixner0793a612008-12-04 20:12:29 +01002107 return ret;
2108
Ingo Molnar9b51f662008-12-12 13:49:45 +01002109err_free_put_context:
Thomas Gleixner0793a612008-12-04 20:12:29 +01002110 kfree(counter);
2111
2112err_put_context:
2113 put_context(ctx);
2114
Ingo Molnar04289bb2008-12-11 08:38:42 +01002115 goto out_fput;
Thomas Gleixner0793a612008-12-04 20:12:29 +01002116}
2117
Ingo Molnar9b51f662008-12-12 13:49:45 +01002118/*
2119 * Initialize the perf_counter context in a task_struct:
2120 */
2121static void
2122__perf_counter_init_context(struct perf_counter_context *ctx,
2123 struct task_struct *task)
2124{
2125 memset(ctx, 0, sizeof(*ctx));
2126 spin_lock_init(&ctx->lock);
Paul Mackerrasd859e292009-01-17 18:10:22 +11002127 mutex_init(&ctx->mutex);
Ingo Molnar9b51f662008-12-12 13:49:45 +01002128 INIT_LIST_HEAD(&ctx->counter_list);
Peter Zijlstra592903c2009-03-13 12:21:36 +01002129 INIT_LIST_HEAD(&ctx->event_list);
Ingo Molnar9b51f662008-12-12 13:49:45 +01002130 ctx->task = task;
2131}
2132
2133/*
2134 * inherit a counter from parent task to child task:
2135 */
Paul Mackerrasd859e292009-01-17 18:10:22 +11002136static struct perf_counter *
Ingo Molnar9b51f662008-12-12 13:49:45 +01002137inherit_counter(struct perf_counter *parent_counter,
2138 struct task_struct *parent,
2139 struct perf_counter_context *parent_ctx,
2140 struct task_struct *child,
Paul Mackerrasd859e292009-01-17 18:10:22 +11002141 struct perf_counter *group_leader,
Ingo Molnar9b51f662008-12-12 13:49:45 +01002142 struct perf_counter_context *child_ctx)
2143{
2144 struct perf_counter *child_counter;
2145
Paul Mackerrasd859e292009-01-17 18:10:22 +11002146 /*
2147 * Instead of creating recursive hierarchies of counters,
2148 * we link inherited counters back to the original parent,
2149 * which has a filp for sure, which we use as the reference
2150 * count:
2151 */
2152 if (parent_counter->parent)
2153 parent_counter = parent_counter->parent;
2154
Ingo Molnar9b51f662008-12-12 13:49:45 +01002155 child_counter = perf_counter_alloc(&parent_counter->hw_event,
Paul Mackerras23a185c2009-02-09 22:42:47 +11002156 parent_counter->cpu, child_ctx,
2157 group_leader, GFP_KERNEL);
Ingo Molnar9b51f662008-12-12 13:49:45 +01002158 if (!child_counter)
Paul Mackerrasd859e292009-01-17 18:10:22 +11002159 return NULL;
Ingo Molnar9b51f662008-12-12 13:49:45 +01002160
2161 /*
2162 * Link it up in the child's context:
2163 */
Ingo Molnar9b51f662008-12-12 13:49:45 +01002164 child_counter->task = child;
2165 list_add_counter(child_counter, child_ctx);
2166 child_ctx->nr_counters++;
2167
2168 child_counter->parent = parent_counter;
Ingo Molnar9b51f662008-12-12 13:49:45 +01002169 /*
2170 * inherit into child's child as well:
2171 */
2172 child_counter->hw_event.inherit = 1;
2173
2174 /*
2175 * Get a reference to the parent filp - we will fput it
2176 * when the child counter exits. This is safe to do because
2177 * we are in the parent and we know that the filp still
2178 * exists and has a nonzero count:
2179 */
2180 atomic_long_inc(&parent_counter->filp->f_count);
2181
Paul Mackerrasd859e292009-01-17 18:10:22 +11002182 /*
2183 * Link this into the parent counter's child list
2184 */
2185 mutex_lock(&parent_counter->mutex);
2186 list_add_tail(&child_counter->child_list, &parent_counter->child_list);
2187
2188 /*
2189 * Make the child state follow the state of the parent counter,
2190 * not its hw_event.disabled bit. We hold the parent's mutex,
2191 * so we won't race with perf_counter_{en,dis}able_family.
2192 */
2193 if (parent_counter->state >= PERF_COUNTER_STATE_INACTIVE)
2194 child_counter->state = PERF_COUNTER_STATE_INACTIVE;
2195 else
2196 child_counter->state = PERF_COUNTER_STATE_OFF;
2197
2198 mutex_unlock(&parent_counter->mutex);
2199
2200 return child_counter;
2201}
2202
2203static int inherit_group(struct perf_counter *parent_counter,
2204 struct task_struct *parent,
2205 struct perf_counter_context *parent_ctx,
2206 struct task_struct *child,
2207 struct perf_counter_context *child_ctx)
2208{
2209 struct perf_counter *leader;
2210 struct perf_counter *sub;
2211
2212 leader = inherit_counter(parent_counter, parent, parent_ctx,
2213 child, NULL, child_ctx);
2214 if (!leader)
2215 return -ENOMEM;
2216 list_for_each_entry(sub, &parent_counter->sibling_list, list_entry) {
2217 if (!inherit_counter(sub, parent, parent_ctx,
2218 child, leader, child_ctx))
2219 return -ENOMEM;
2220 }
Ingo Molnar9b51f662008-12-12 13:49:45 +01002221 return 0;
2222}
2223
Paul Mackerrasd859e292009-01-17 18:10:22 +11002224static void sync_child_counter(struct perf_counter *child_counter,
2225 struct perf_counter *parent_counter)
2226{
2227 u64 parent_val, child_val;
2228
2229 parent_val = atomic64_read(&parent_counter->count);
2230 child_val = atomic64_read(&child_counter->count);
2231
2232 /*
2233 * Add back the child's count to the parent's count:
2234 */
2235 atomic64_add(child_val, &parent_counter->count);
2236
2237 /*
2238 * Remove this counter from the parent's list
2239 */
2240 mutex_lock(&parent_counter->mutex);
2241 list_del_init(&child_counter->child_list);
2242 mutex_unlock(&parent_counter->mutex);
2243
2244 /*
2245 * Release the parent counter, if this was the last
2246 * reference to it.
2247 */
2248 fput(parent_counter->filp);
2249}
2250
Ingo Molnar9b51f662008-12-12 13:49:45 +01002251static void
2252__perf_counter_exit_task(struct task_struct *child,
2253 struct perf_counter *child_counter,
2254 struct perf_counter_context *child_ctx)
2255{
2256 struct perf_counter *parent_counter;
Paul Mackerrasd859e292009-01-17 18:10:22 +11002257 struct perf_counter *sub, *tmp;
Ingo Molnar9b51f662008-12-12 13:49:45 +01002258
2259 /*
Ingo Molnar235c7fc2008-12-21 14:43:25 +01002260 * If we do not self-reap then we have to wait for the
2261 * child task to unschedule (it will happen for sure),
2262 * so that its counter is at its final count. (This
2263 * condition triggers rarely - child tasks usually get
2264 * off their CPU before the parent has a chance to
2265 * get this far into the reaping action)
Ingo Molnar9b51f662008-12-12 13:49:45 +01002266 */
Ingo Molnar235c7fc2008-12-21 14:43:25 +01002267 if (child != current) {
2268 wait_task_inactive(child, 0);
2269 list_del_init(&child_counter->list_entry);
2270 } else {
Ingo Molnar0cc0c022008-12-14 23:20:36 +01002271 struct perf_cpu_context *cpuctx;
Ingo Molnar235c7fc2008-12-21 14:43:25 +01002272 unsigned long flags;
2273 u64 perf_flags;
2274
2275 /*
2276 * Disable and unlink this counter.
2277 *
2278 * Be careful about zapping the list - IRQ/NMI context
2279 * could still be processing it:
2280 */
2281 curr_rq_lock_irq_save(&flags);
2282 perf_flags = hw_perf_save_disable();
Ingo Molnar0cc0c022008-12-14 23:20:36 +01002283
2284 cpuctx = &__get_cpu_var(perf_cpu_context);
2285
Paul Mackerrasd859e292009-01-17 18:10:22 +11002286 group_sched_out(child_counter, cpuctx, child_ctx);
Ingo Molnar0cc0c022008-12-14 23:20:36 +01002287
Ingo Molnar235c7fc2008-12-21 14:43:25 +01002288 list_del_init(&child_counter->list_entry);
2289
2290 child_ctx->nr_counters--;
2291
2292 hw_perf_restore(perf_flags);
2293 curr_rq_unlock_irq_restore(&flags);
Ingo Molnar0cc0c022008-12-14 23:20:36 +01002294 }
2295
Ingo Molnar9b51f662008-12-12 13:49:45 +01002296 parent_counter = child_counter->parent;
2297 /*
2298 * It can happen that parent exits first, and has counters
2299 * that are still around due to the child reference. These
2300 * counters need to be zapped - but otherwise linger.
2301 */
Paul Mackerrasd859e292009-01-17 18:10:22 +11002302 if (parent_counter) {
2303 sync_child_counter(child_counter, parent_counter);
2304 list_for_each_entry_safe(sub, tmp, &child_counter->sibling_list,
2305 list_entry) {
Paul Mackerras4bcf3492009-02-11 13:53:19 +01002306 if (sub->parent) {
Paul Mackerrasd859e292009-01-17 18:10:22 +11002307 sync_child_counter(sub, sub->parent);
Peter Zijlstraf1600952009-03-19 20:26:16 +01002308 free_counter(sub);
Paul Mackerras4bcf3492009-02-11 13:53:19 +01002309 }
Paul Mackerrasd859e292009-01-17 18:10:22 +11002310 }
Peter Zijlstraf1600952009-03-19 20:26:16 +01002311 free_counter(child_counter);
Paul Mackerras4bcf3492009-02-11 13:53:19 +01002312 }
Ingo Molnar9b51f662008-12-12 13:49:45 +01002313}
2314
2315/*
Paul Mackerrasd859e292009-01-17 18:10:22 +11002316 * When a child task exits, feed back counter values to parent counters.
Ingo Molnar9b51f662008-12-12 13:49:45 +01002317 *
Paul Mackerrasd859e292009-01-17 18:10:22 +11002318 * Note: we may be running in child context, but the PID is not hashed
Ingo Molnar9b51f662008-12-12 13:49:45 +01002319 * anymore so new counters will not be added.
2320 */
2321void perf_counter_exit_task(struct task_struct *child)
2322{
2323 struct perf_counter *child_counter, *tmp;
2324 struct perf_counter_context *child_ctx;
2325
2326 child_ctx = &child->perf_counter_ctx;
2327
2328 if (likely(!child_ctx->nr_counters))
2329 return;
2330
2331 list_for_each_entry_safe(child_counter, tmp, &child_ctx->counter_list,
2332 list_entry)
2333 __perf_counter_exit_task(child, child_counter, child_ctx);
2334}
2335
2336/*
2337 * Initialize the perf_counter context in task_struct
2338 */
2339void perf_counter_init_task(struct task_struct *child)
2340{
2341 struct perf_counter_context *child_ctx, *parent_ctx;
Paul Mackerrasd859e292009-01-17 18:10:22 +11002342 struct perf_counter *counter;
Ingo Molnar9b51f662008-12-12 13:49:45 +01002343 struct task_struct *parent = current;
Ingo Molnar9b51f662008-12-12 13:49:45 +01002344
2345 child_ctx = &child->perf_counter_ctx;
2346 parent_ctx = &parent->perf_counter_ctx;
2347
2348 __perf_counter_init_context(child_ctx, child);
2349
2350 /*
2351 * This is executed from the parent task context, so inherit
2352 * counters that have been marked for cloning:
2353 */
2354
2355 if (likely(!parent_ctx->nr_counters))
2356 return;
2357
2358 /*
2359 * Lock the parent list. No need to lock the child - not PID
2360 * hashed yet and not running, so nobody can access it.
2361 */
Paul Mackerrasd859e292009-01-17 18:10:22 +11002362 mutex_lock(&parent_ctx->mutex);
Ingo Molnar9b51f662008-12-12 13:49:45 +01002363
2364 /*
2365 * We dont have to disable NMIs - we are only looking at
2366 * the list, not manipulating it:
2367 */
2368 list_for_each_entry(counter, &parent_ctx->counter_list, list_entry) {
Paul Mackerrasd859e292009-01-17 18:10:22 +11002369 if (!counter->hw_event.inherit)
Ingo Molnar9b51f662008-12-12 13:49:45 +01002370 continue;
2371
Paul Mackerrasd859e292009-01-17 18:10:22 +11002372 if (inherit_group(counter, parent,
Ingo Molnar9b51f662008-12-12 13:49:45 +01002373 parent_ctx, child, child_ctx))
2374 break;
2375 }
2376
Paul Mackerrasd859e292009-01-17 18:10:22 +11002377 mutex_unlock(&parent_ctx->mutex);
Ingo Molnar9b51f662008-12-12 13:49:45 +01002378}
2379
Ingo Molnar04289bb2008-12-11 08:38:42 +01002380static void __cpuinit perf_counter_init_cpu(int cpu)
Thomas Gleixner0793a612008-12-04 20:12:29 +01002381{
Ingo Molnar04289bb2008-12-11 08:38:42 +01002382 struct perf_cpu_context *cpuctx;
Thomas Gleixner0793a612008-12-04 20:12:29 +01002383
Ingo Molnar04289bb2008-12-11 08:38:42 +01002384 cpuctx = &per_cpu(perf_cpu_context, cpu);
2385 __perf_counter_init_context(&cpuctx->ctx, NULL);
Thomas Gleixner0793a612008-12-04 20:12:29 +01002386
2387 mutex_lock(&perf_resource_mutex);
Ingo Molnar04289bb2008-12-11 08:38:42 +01002388 cpuctx->max_pertask = perf_max_counters - perf_reserved_percpu;
Thomas Gleixner0793a612008-12-04 20:12:29 +01002389 mutex_unlock(&perf_resource_mutex);
Ingo Molnar04289bb2008-12-11 08:38:42 +01002390
Paul Mackerras01d02872009-01-14 13:44:19 +11002391 hw_perf_counter_setup(cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +01002392}
2393
2394#ifdef CONFIG_HOTPLUG_CPU
Ingo Molnar04289bb2008-12-11 08:38:42 +01002395static void __perf_counter_exit_cpu(void *info)
Thomas Gleixner0793a612008-12-04 20:12:29 +01002396{
2397 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
2398 struct perf_counter_context *ctx = &cpuctx->ctx;
2399 struct perf_counter *counter, *tmp;
2400
Ingo Molnar04289bb2008-12-11 08:38:42 +01002401 list_for_each_entry_safe(counter, tmp, &ctx->counter_list, list_entry)
2402 __perf_counter_remove_from_context(counter);
Thomas Gleixner0793a612008-12-04 20:12:29 +01002403}
Ingo Molnar04289bb2008-12-11 08:38:42 +01002404static void perf_counter_exit_cpu(int cpu)
Thomas Gleixner0793a612008-12-04 20:12:29 +01002405{
Paul Mackerrasd859e292009-01-17 18:10:22 +11002406 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
2407 struct perf_counter_context *ctx = &cpuctx->ctx;
2408
2409 mutex_lock(&ctx->mutex);
Ingo Molnar04289bb2008-12-11 08:38:42 +01002410 smp_call_function_single(cpu, __perf_counter_exit_cpu, NULL, 1);
Paul Mackerrasd859e292009-01-17 18:10:22 +11002411 mutex_unlock(&ctx->mutex);
Thomas Gleixner0793a612008-12-04 20:12:29 +01002412}
2413#else
Ingo Molnar04289bb2008-12-11 08:38:42 +01002414static inline void perf_counter_exit_cpu(int cpu) { }
Thomas Gleixner0793a612008-12-04 20:12:29 +01002415#endif
2416
2417static int __cpuinit
2418perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
2419{
2420 unsigned int cpu = (long)hcpu;
2421
2422 switch (action) {
2423
2424 case CPU_UP_PREPARE:
2425 case CPU_UP_PREPARE_FROZEN:
Ingo Molnar04289bb2008-12-11 08:38:42 +01002426 perf_counter_init_cpu(cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +01002427 break;
2428
2429 case CPU_DOWN_PREPARE:
2430 case CPU_DOWN_PREPARE_FROZEN:
Ingo Molnar04289bb2008-12-11 08:38:42 +01002431 perf_counter_exit_cpu(cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +01002432 break;
2433
2434 default:
2435 break;
2436 }
2437
2438 return NOTIFY_OK;
2439}
2440
2441static struct notifier_block __cpuinitdata perf_cpu_nb = {
2442 .notifier_call = perf_cpu_notify,
2443};
2444
2445static int __init perf_counter_init(void)
2446{
2447 perf_cpu_notify(&perf_cpu_nb, (unsigned long)CPU_UP_PREPARE,
2448 (void *)(long)smp_processor_id());
2449 register_cpu_notifier(&perf_cpu_nb);
2450
2451 return 0;
2452}
2453early_initcall(perf_counter_init);
2454
2455static ssize_t perf_show_reserve_percpu(struct sysdev_class *class, char *buf)
2456{
2457 return sprintf(buf, "%d\n", perf_reserved_percpu);
2458}
2459
2460static ssize_t
2461perf_set_reserve_percpu(struct sysdev_class *class,
2462 const char *buf,
2463 size_t count)
2464{
2465 struct perf_cpu_context *cpuctx;
2466 unsigned long val;
2467 int err, cpu, mpt;
2468
2469 err = strict_strtoul(buf, 10, &val);
2470 if (err)
2471 return err;
2472 if (val > perf_max_counters)
2473 return -EINVAL;
2474
2475 mutex_lock(&perf_resource_mutex);
2476 perf_reserved_percpu = val;
2477 for_each_online_cpu(cpu) {
2478 cpuctx = &per_cpu(perf_cpu_context, cpu);
2479 spin_lock_irq(&cpuctx->ctx.lock);
2480 mpt = min(perf_max_counters - cpuctx->ctx.nr_counters,
2481 perf_max_counters - perf_reserved_percpu);
2482 cpuctx->max_pertask = mpt;
2483 spin_unlock_irq(&cpuctx->ctx.lock);
2484 }
2485 mutex_unlock(&perf_resource_mutex);
2486
2487 return count;
2488}
2489
2490static ssize_t perf_show_overcommit(struct sysdev_class *class, char *buf)
2491{
2492 return sprintf(buf, "%d\n", perf_overcommit);
2493}
2494
2495static ssize_t
2496perf_set_overcommit(struct sysdev_class *class, const char *buf, size_t count)
2497{
2498 unsigned long val;
2499 int err;
2500
2501 err = strict_strtoul(buf, 10, &val);
2502 if (err)
2503 return err;
2504 if (val > 1)
2505 return -EINVAL;
2506
2507 mutex_lock(&perf_resource_mutex);
2508 perf_overcommit = val;
2509 mutex_unlock(&perf_resource_mutex);
2510
2511 return count;
2512}
2513
2514static SYSDEV_CLASS_ATTR(
2515 reserve_percpu,
2516 0644,
2517 perf_show_reserve_percpu,
2518 perf_set_reserve_percpu
2519 );
2520
2521static SYSDEV_CLASS_ATTR(
2522 overcommit,
2523 0644,
2524 perf_show_overcommit,
2525 perf_set_overcommit
2526 );
2527
2528static struct attribute *perfclass_attrs[] = {
2529 &attr_reserve_percpu.attr,
2530 &attr_overcommit.attr,
2531 NULL
2532};
2533
2534static struct attribute_group perfclass_attr_group = {
2535 .attrs = perfclass_attrs,
2536 .name = "perf_counters",
2537};
2538
2539static int __init perf_counter_sysfs_init(void)
2540{
2541 return sysfs_create_group(&cpu_sysdev_class.kset.kobj,
2542 &perfclass_attr_group);
2543}
2544device_initcall(perf_counter_sysfs_init);