blob: 97f891ffeb40611f0f966dab3087646d76a78a71 [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>
Thomas Gleixner0793a612008-12-04 20:12:29 +010026
Tim Blechmann4e193bd2009-03-14 14:29:25 +010027#include <asm/irq_regs.h>
28
Thomas Gleixner0793a612008-12-04 20:12:29 +010029/*
30 * Each CPU has a list of per CPU counters:
31 */
32DEFINE_PER_CPU(struct perf_cpu_context, perf_cpu_context);
33
Ingo Molnar088e2852008-12-14 20:21:00 +010034int perf_max_counters __read_mostly = 1;
Thomas Gleixner0793a612008-12-04 20:12:29 +010035static int perf_reserved_percpu __read_mostly;
36static int perf_overcommit __read_mostly = 1;
37
38/*
39 * Mutex for (sysadmin-configurable) counter reservations:
40 */
41static DEFINE_MUTEX(perf_resource_mutex);
42
43/*
44 * Architecture provided APIs - weak aliases:
45 */
Ingo Molnar5c92d122008-12-11 13:21:10 +010046extern __weak const struct hw_perf_counter_ops *
Ingo Molnar621a01e2008-12-11 12:46:46 +010047hw_perf_counter_init(struct perf_counter *counter)
Thomas Gleixner0793a612008-12-04 20:12:29 +010048{
Paul Mackerrasff6f0542009-01-09 16:19:25 +110049 return NULL;
Thomas Gleixner0793a612008-12-04 20:12:29 +010050}
51
Ingo Molnar01b28382008-12-11 13:45:51 +010052u64 __weak hw_perf_save_disable(void) { return 0; }
Yinghai Lu01ea1cc2008-12-26 21:05:06 -080053void __weak hw_perf_restore(u64 ctrl) { barrier(); }
Paul Mackerras01d02872009-01-14 13:44:19 +110054void __weak hw_perf_counter_setup(int cpu) { barrier(); }
Paul Mackerras3cbed422009-01-09 16:43:42 +110055int __weak hw_perf_group_sched_in(struct perf_counter *group_leader,
56 struct perf_cpu_context *cpuctx,
57 struct perf_counter_context *ctx, int cpu)
58{
59 return 0;
60}
Thomas Gleixner0793a612008-12-04 20:12:29 +010061
Paul Mackerras4eb96fc2009-01-09 17:24:34 +110062void __weak perf_counter_print_debug(void) { }
63
Ingo Molnar04289bb2008-12-11 08:38:42 +010064static void
65list_add_counter(struct perf_counter *counter, struct perf_counter_context *ctx)
66{
67 struct perf_counter *group_leader = counter->group_leader;
68
69 /*
70 * Depending on whether it is a standalone or sibling counter,
71 * add it straight to the context's counter list, or to the group
72 * leader's sibling list:
73 */
74 if (counter->group_leader == counter)
75 list_add_tail(&counter->list_entry, &ctx->counter_list);
76 else
77 list_add_tail(&counter->list_entry, &group_leader->sibling_list);
Peter Zijlstra592903c2009-03-13 12:21:36 +010078
79 list_add_rcu(&counter->event_entry, &ctx->event_list);
Ingo Molnar04289bb2008-12-11 08:38:42 +010080}
81
82static void
83list_del_counter(struct perf_counter *counter, struct perf_counter_context *ctx)
84{
85 struct perf_counter *sibling, *tmp;
86
87 list_del_init(&counter->list_entry);
Peter Zijlstra592903c2009-03-13 12:21:36 +010088 list_del_rcu(&counter->event_entry);
Ingo Molnar04289bb2008-12-11 08:38:42 +010089
Ingo Molnar04289bb2008-12-11 08:38:42 +010090 /*
91 * If this was a group counter with sibling counters then
92 * upgrade the siblings to singleton counters by adding them
93 * to the context list directly:
94 */
95 list_for_each_entry_safe(sibling, tmp,
96 &counter->sibling_list, list_entry) {
97
Peter Zijlstra75564232009-03-13 12:21:29 +010098 list_move_tail(&sibling->list_entry, &ctx->counter_list);
Ingo Molnar04289bb2008-12-11 08:38:42 +010099 sibling->group_leader = sibling;
100 }
101}
102
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100103static void
104counter_sched_out(struct perf_counter *counter,
105 struct perf_cpu_context *cpuctx,
106 struct perf_counter_context *ctx)
107{
108 if (counter->state != PERF_COUNTER_STATE_ACTIVE)
109 return;
110
111 counter->state = PERF_COUNTER_STATE_INACTIVE;
112 counter->hw_ops->disable(counter);
113 counter->oncpu = -1;
114
115 if (!is_software_counter(counter))
116 cpuctx->active_oncpu--;
117 ctx->nr_active--;
118 if (counter->hw_event.exclusive || !cpuctx->active_oncpu)
119 cpuctx->exclusive = 0;
120}
121
Paul Mackerrasd859e292009-01-17 18:10:22 +1100122static void
123group_sched_out(struct perf_counter *group_counter,
124 struct perf_cpu_context *cpuctx,
125 struct perf_counter_context *ctx)
126{
127 struct perf_counter *counter;
128
129 if (group_counter->state != PERF_COUNTER_STATE_ACTIVE)
130 return;
131
132 counter_sched_out(group_counter, cpuctx, ctx);
133
134 /*
135 * Schedule out siblings (if any):
136 */
137 list_for_each_entry(counter, &group_counter->sibling_list, list_entry)
138 counter_sched_out(counter, cpuctx, ctx);
139
140 if (group_counter->hw_event.exclusive)
141 cpuctx->exclusive = 0;
142}
143
Thomas Gleixner0793a612008-12-04 20:12:29 +0100144/*
145 * Cross CPU call to remove a performance counter
146 *
147 * We disable the counter on the hardware level first. After that we
148 * remove it from the context list.
149 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100150static void __perf_counter_remove_from_context(void *info)
Thomas Gleixner0793a612008-12-04 20:12:29 +0100151{
152 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
153 struct perf_counter *counter = info;
154 struct perf_counter_context *ctx = counter->ctx;
Ingo Molnar9b51f662008-12-12 13:49:45 +0100155 unsigned long flags;
Ingo Molnar5c92d122008-12-11 13:21:10 +0100156 u64 perf_flags;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100157
158 /*
159 * If this is a task context, we need to check whether it is
160 * the current task context of this cpu. If not it has been
161 * scheduled out before the smp call arrived.
162 */
163 if (ctx->task && cpuctx->task_ctx != ctx)
164 return;
165
Ingo Molnaraa9c4c02008-12-17 14:10:57 +0100166 curr_rq_lock_irq_save(&flags);
167 spin_lock(&ctx->lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100168
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100169 counter_sched_out(counter, cpuctx, ctx);
170
171 counter->task = NULL;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100172 ctx->nr_counters--;
173
174 /*
175 * Protect the list operation against NMI by disabling the
176 * counters on a global level. NOP for non NMI based counters.
177 */
Ingo Molnar01b28382008-12-11 13:45:51 +0100178 perf_flags = hw_perf_save_disable();
Ingo Molnar04289bb2008-12-11 08:38:42 +0100179 list_del_counter(counter, ctx);
Ingo Molnar01b28382008-12-11 13:45:51 +0100180 hw_perf_restore(perf_flags);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100181
182 if (!ctx->task) {
183 /*
184 * Allow more per task counters with respect to the
185 * reservation:
186 */
187 cpuctx->max_pertask =
188 min(perf_max_counters - ctx->nr_counters,
189 perf_max_counters - perf_reserved_percpu);
190 }
191
Ingo Molnaraa9c4c02008-12-17 14:10:57 +0100192 spin_unlock(&ctx->lock);
193 curr_rq_unlock_irq_restore(&flags);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100194}
195
196
197/*
198 * Remove the counter from a task's (or a CPU's) list of counters.
199 *
Paul Mackerrasd859e292009-01-17 18:10:22 +1100200 * Must be called with counter->mutex and ctx->mutex held.
Thomas Gleixner0793a612008-12-04 20:12:29 +0100201 *
202 * CPU counters are removed with a smp call. For task counters we only
203 * call when the task is on a CPU.
204 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100205static void perf_counter_remove_from_context(struct perf_counter *counter)
Thomas Gleixner0793a612008-12-04 20:12:29 +0100206{
207 struct perf_counter_context *ctx = counter->ctx;
208 struct task_struct *task = ctx->task;
209
210 if (!task) {
211 /*
212 * Per cpu counters are removed via an smp call and
213 * the removal is always sucessful.
214 */
215 smp_call_function_single(counter->cpu,
Ingo Molnar04289bb2008-12-11 08:38:42 +0100216 __perf_counter_remove_from_context,
Thomas Gleixner0793a612008-12-04 20:12:29 +0100217 counter, 1);
218 return;
219 }
220
221retry:
Ingo Molnar04289bb2008-12-11 08:38:42 +0100222 task_oncpu_function_call(task, __perf_counter_remove_from_context,
Thomas Gleixner0793a612008-12-04 20:12:29 +0100223 counter);
224
225 spin_lock_irq(&ctx->lock);
226 /*
227 * If the context is active we need to retry the smp call.
228 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100229 if (ctx->nr_active && !list_empty(&counter->list_entry)) {
Thomas Gleixner0793a612008-12-04 20:12:29 +0100230 spin_unlock_irq(&ctx->lock);
231 goto retry;
232 }
233
234 /*
235 * The lock prevents that this context is scheduled in so we
Ingo Molnar04289bb2008-12-11 08:38:42 +0100236 * can remove the counter safely, if the call above did not
Thomas Gleixner0793a612008-12-04 20:12:29 +0100237 * succeed.
238 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100239 if (!list_empty(&counter->list_entry)) {
Thomas Gleixner0793a612008-12-04 20:12:29 +0100240 ctx->nr_counters--;
Ingo Molnar04289bb2008-12-11 08:38:42 +0100241 list_del_counter(counter, ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100242 counter->task = NULL;
243 }
244 spin_unlock_irq(&ctx->lock);
245}
246
Paul Mackerrasd859e292009-01-17 18:10:22 +1100247/*
248 * Cross CPU call to disable a performance counter
249 */
250static void __perf_counter_disable(void *info)
251{
252 struct perf_counter *counter = info;
253 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
254 struct perf_counter_context *ctx = counter->ctx;
255 unsigned long flags;
256
257 /*
258 * If this is a per-task counter, need to check whether this
259 * counter's task is the current task on this cpu.
260 */
261 if (ctx->task && cpuctx->task_ctx != ctx)
262 return;
263
264 curr_rq_lock_irq_save(&flags);
265 spin_lock(&ctx->lock);
266
267 /*
268 * If the counter is on, turn it off.
269 * If it is in error state, leave it in error state.
270 */
271 if (counter->state >= PERF_COUNTER_STATE_INACTIVE) {
272 if (counter == counter->group_leader)
273 group_sched_out(counter, cpuctx, ctx);
274 else
275 counter_sched_out(counter, cpuctx, ctx);
276 counter->state = PERF_COUNTER_STATE_OFF;
277 }
278
279 spin_unlock(&ctx->lock);
280 curr_rq_unlock_irq_restore(&flags);
281}
282
283/*
284 * Disable a counter.
285 */
286static void perf_counter_disable(struct perf_counter *counter)
287{
288 struct perf_counter_context *ctx = counter->ctx;
289 struct task_struct *task = ctx->task;
290
291 if (!task) {
292 /*
293 * Disable the counter on the cpu that it's on
294 */
295 smp_call_function_single(counter->cpu, __perf_counter_disable,
296 counter, 1);
297 return;
298 }
299
300 retry:
301 task_oncpu_function_call(task, __perf_counter_disable, counter);
302
303 spin_lock_irq(&ctx->lock);
304 /*
305 * If the counter is still active, we need to retry the cross-call.
306 */
307 if (counter->state == PERF_COUNTER_STATE_ACTIVE) {
308 spin_unlock_irq(&ctx->lock);
309 goto retry;
310 }
311
312 /*
313 * Since we have the lock this context can't be scheduled
314 * in, so we can change the state safely.
315 */
316 if (counter->state == PERF_COUNTER_STATE_INACTIVE)
317 counter->state = PERF_COUNTER_STATE_OFF;
318
319 spin_unlock_irq(&ctx->lock);
320}
321
322/*
323 * Disable a counter and all its children.
324 */
325static void perf_counter_disable_family(struct perf_counter *counter)
326{
327 struct perf_counter *child;
328
329 perf_counter_disable(counter);
330
331 /*
332 * Lock the mutex to protect the list of children
333 */
334 mutex_lock(&counter->mutex);
335 list_for_each_entry(child, &counter->child_list, child_list)
336 perf_counter_disable(child);
337 mutex_unlock(&counter->mutex);
338}
339
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100340static int
341counter_sched_in(struct perf_counter *counter,
342 struct perf_cpu_context *cpuctx,
343 struct perf_counter_context *ctx,
344 int cpu)
345{
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100346 if (counter->state <= PERF_COUNTER_STATE_OFF)
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100347 return 0;
348
349 counter->state = PERF_COUNTER_STATE_ACTIVE;
350 counter->oncpu = cpu; /* TODO: put 'cpu' into cpuctx->cpu */
351 /*
352 * The new state must be visible before we turn it on in the hardware:
353 */
354 smp_wmb();
355
356 if (counter->hw_ops->enable(counter)) {
357 counter->state = PERF_COUNTER_STATE_INACTIVE;
358 counter->oncpu = -1;
359 return -EAGAIN;
360 }
361
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100362 if (!is_software_counter(counter))
363 cpuctx->active_oncpu++;
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100364 ctx->nr_active++;
365
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100366 if (counter->hw_event.exclusive)
367 cpuctx->exclusive = 1;
368
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100369 return 0;
370}
371
Thomas Gleixner0793a612008-12-04 20:12:29 +0100372/*
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100373 * Return 1 for a group consisting entirely of software counters,
374 * 0 if the group contains any hardware counters.
375 */
376static int is_software_only_group(struct perf_counter *leader)
377{
378 struct perf_counter *counter;
379
380 if (!is_software_counter(leader))
381 return 0;
382 list_for_each_entry(counter, &leader->sibling_list, list_entry)
383 if (!is_software_counter(counter))
384 return 0;
385 return 1;
386}
387
388/*
389 * Work out whether we can put this counter group on the CPU now.
390 */
391static int group_can_go_on(struct perf_counter *counter,
392 struct perf_cpu_context *cpuctx,
393 int can_add_hw)
394{
395 /*
396 * Groups consisting entirely of software counters can always go on.
397 */
398 if (is_software_only_group(counter))
399 return 1;
400 /*
401 * If an exclusive group is already on, no other hardware
402 * counters can go on.
403 */
404 if (cpuctx->exclusive)
405 return 0;
406 /*
407 * If this group is exclusive and there are already
408 * counters on the CPU, it can't go on.
409 */
410 if (counter->hw_event.exclusive && cpuctx->active_oncpu)
411 return 0;
412 /*
413 * Otherwise, try to add it if all previous groups were able
414 * to go on.
415 */
416 return can_add_hw;
417}
418
419/*
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100420 * Cross CPU call to install and enable a performance counter
Thomas Gleixner0793a612008-12-04 20:12:29 +0100421 */
422static void __perf_install_in_context(void *info)
423{
424 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
425 struct perf_counter *counter = info;
426 struct perf_counter_context *ctx = counter->ctx;
Paul Mackerrasd859e292009-01-17 18:10:22 +1100427 struct perf_counter *leader = counter->group_leader;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100428 int cpu = smp_processor_id();
Ingo Molnar9b51f662008-12-12 13:49:45 +0100429 unsigned long flags;
Ingo Molnar5c92d122008-12-11 13:21:10 +0100430 u64 perf_flags;
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100431 int err;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100432
433 /*
434 * If this is a task context, we need to check whether it is
435 * the current task context of this cpu. If not it has been
436 * scheduled out before the smp call arrived.
437 */
438 if (ctx->task && cpuctx->task_ctx != ctx)
439 return;
440
Ingo Molnaraa9c4c02008-12-17 14:10:57 +0100441 curr_rq_lock_irq_save(&flags);
442 spin_lock(&ctx->lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100443
444 /*
445 * Protect the list operation against NMI by disabling the
446 * counters on a global level. NOP for non NMI based counters.
447 */
Ingo Molnar01b28382008-12-11 13:45:51 +0100448 perf_flags = hw_perf_save_disable();
Thomas Gleixner0793a612008-12-04 20:12:29 +0100449
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100450 list_add_counter(counter, ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100451 ctx->nr_counters++;
Paul Mackerrasc07c99b2009-02-13 22:10:34 +1100452 counter->prev_state = PERF_COUNTER_STATE_OFF;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100453
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100454 /*
Paul Mackerrasd859e292009-01-17 18:10:22 +1100455 * Don't put the counter on if it is disabled or if
456 * it is in a group and the group isn't on.
457 */
458 if (counter->state != PERF_COUNTER_STATE_INACTIVE ||
459 (leader != counter && leader->state != PERF_COUNTER_STATE_ACTIVE))
460 goto unlock;
461
462 /*
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100463 * An exclusive counter can't go on if there are already active
464 * hardware counters, and no hardware counter can go on if there
465 * is already an exclusive counter on.
466 */
Paul Mackerrasd859e292009-01-17 18:10:22 +1100467 if (!group_can_go_on(counter, cpuctx, 1))
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100468 err = -EEXIST;
469 else
470 err = counter_sched_in(counter, cpuctx, ctx, cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100471
Paul Mackerrasd859e292009-01-17 18:10:22 +1100472 if (err) {
473 /*
474 * This counter couldn't go on. If it is in a group
475 * then we have to pull the whole group off.
476 * If the counter group is pinned then put it in error state.
477 */
478 if (leader != counter)
479 group_sched_out(leader, cpuctx, ctx);
480 if (leader->hw_event.pinned)
481 leader->state = PERF_COUNTER_STATE_ERROR;
482 }
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100483
484 if (!err && !ctx->task && cpuctx->max_pertask)
Thomas Gleixner0793a612008-12-04 20:12:29 +0100485 cpuctx->max_pertask--;
486
Paul Mackerrasd859e292009-01-17 18:10:22 +1100487 unlock:
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100488 hw_perf_restore(perf_flags);
489
Ingo Molnaraa9c4c02008-12-17 14:10:57 +0100490 spin_unlock(&ctx->lock);
491 curr_rq_unlock_irq_restore(&flags);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100492}
493
494/*
495 * Attach a performance counter to a context
496 *
497 * First we add the counter to the list with the hardware enable bit
498 * in counter->hw_config cleared.
499 *
500 * If the counter is attached to a task which is on a CPU we use a smp
501 * call to enable it in the task context. The task might have been
502 * scheduled away, but we check this in the smp call again.
Paul Mackerrasd859e292009-01-17 18:10:22 +1100503 *
504 * Must be called with ctx->mutex held.
Thomas Gleixner0793a612008-12-04 20:12:29 +0100505 */
506static void
507perf_install_in_context(struct perf_counter_context *ctx,
508 struct perf_counter *counter,
509 int cpu)
510{
511 struct task_struct *task = ctx->task;
512
Thomas Gleixner0793a612008-12-04 20:12:29 +0100513 if (!task) {
514 /*
515 * Per cpu counters are installed via an smp call and
516 * the install is always sucessful.
517 */
518 smp_call_function_single(cpu, __perf_install_in_context,
519 counter, 1);
520 return;
521 }
522
523 counter->task = task;
524retry:
525 task_oncpu_function_call(task, __perf_install_in_context,
526 counter);
527
528 spin_lock_irq(&ctx->lock);
529 /*
Thomas Gleixner0793a612008-12-04 20:12:29 +0100530 * we need to retry the smp call.
531 */
Paul Mackerrasd859e292009-01-17 18:10:22 +1100532 if (ctx->is_active && list_empty(&counter->list_entry)) {
Thomas Gleixner0793a612008-12-04 20:12:29 +0100533 spin_unlock_irq(&ctx->lock);
534 goto retry;
535 }
536
537 /*
538 * The lock prevents that this context is scheduled in so we
539 * can add the counter safely, if it the call above did not
540 * succeed.
541 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100542 if (list_empty(&counter->list_entry)) {
543 list_add_counter(counter, ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100544 ctx->nr_counters++;
545 }
546 spin_unlock_irq(&ctx->lock);
547}
548
Paul Mackerrasd859e292009-01-17 18:10:22 +1100549/*
550 * Cross CPU call to enable a performance counter
551 */
552static void __perf_counter_enable(void *info)
Ingo Molnar04289bb2008-12-11 08:38:42 +0100553{
Paul Mackerrasd859e292009-01-17 18:10:22 +1100554 struct perf_counter *counter = info;
555 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
556 struct perf_counter_context *ctx = counter->ctx;
557 struct perf_counter *leader = counter->group_leader;
558 unsigned long flags;
559 int err;
Ingo Molnar04289bb2008-12-11 08:38:42 +0100560
561 /*
Paul Mackerrasd859e292009-01-17 18:10:22 +1100562 * If this is a per-task counter, need to check whether this
563 * counter's task is the current task on this cpu.
Ingo Molnar04289bb2008-12-11 08:38:42 +0100564 */
Paul Mackerrasd859e292009-01-17 18:10:22 +1100565 if (ctx->task && cpuctx->task_ctx != ctx)
566 return;
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100567
Paul Mackerrasd859e292009-01-17 18:10:22 +1100568 curr_rq_lock_irq_save(&flags);
569 spin_lock(&ctx->lock);
570
Paul Mackerrasc07c99b2009-02-13 22:10:34 +1100571 counter->prev_state = counter->state;
Paul Mackerrasd859e292009-01-17 18:10:22 +1100572 if (counter->state >= PERF_COUNTER_STATE_INACTIVE)
573 goto unlock;
574 counter->state = PERF_COUNTER_STATE_INACTIVE;
575
576 /*
577 * If the counter is in a group and isn't the group leader,
578 * then don't put it on unless the group is on.
579 */
580 if (leader != counter && leader->state != PERF_COUNTER_STATE_ACTIVE)
581 goto unlock;
582
583 if (!group_can_go_on(counter, cpuctx, 1))
584 err = -EEXIST;
585 else
586 err = counter_sched_in(counter, cpuctx, ctx,
587 smp_processor_id());
588
589 if (err) {
590 /*
591 * If this counter can't go on and it's part of a
592 * group, then the whole group has to come off.
593 */
594 if (leader != counter)
595 group_sched_out(leader, cpuctx, ctx);
596 if (leader->hw_event.pinned)
597 leader->state = PERF_COUNTER_STATE_ERROR;
598 }
599
600 unlock:
601 spin_unlock(&ctx->lock);
602 curr_rq_unlock_irq_restore(&flags);
603}
604
605/*
606 * Enable a counter.
607 */
608static void perf_counter_enable(struct perf_counter *counter)
609{
610 struct perf_counter_context *ctx = counter->ctx;
611 struct task_struct *task = ctx->task;
612
613 if (!task) {
614 /*
615 * Enable the counter on the cpu that it's on
616 */
617 smp_call_function_single(counter->cpu, __perf_counter_enable,
618 counter, 1);
619 return;
620 }
621
622 spin_lock_irq(&ctx->lock);
623 if (counter->state >= PERF_COUNTER_STATE_INACTIVE)
624 goto out;
625
626 /*
627 * If the counter is in error state, clear that first.
628 * That way, if we see the counter in error state below, we
629 * know that it has gone back into error state, as distinct
630 * from the task having been scheduled away before the
631 * cross-call arrived.
632 */
633 if (counter->state == PERF_COUNTER_STATE_ERROR)
634 counter->state = PERF_COUNTER_STATE_OFF;
635
636 retry:
637 spin_unlock_irq(&ctx->lock);
638 task_oncpu_function_call(task, __perf_counter_enable, counter);
639
640 spin_lock_irq(&ctx->lock);
641
642 /*
643 * If the context is active and the counter is still off,
644 * we need to retry the cross-call.
645 */
646 if (ctx->is_active && counter->state == PERF_COUNTER_STATE_OFF)
647 goto retry;
648
649 /*
650 * Since we have the lock this context can't be scheduled
651 * in, so we can change the state safely.
652 */
653 if (counter->state == PERF_COUNTER_STATE_OFF)
654 counter->state = PERF_COUNTER_STATE_INACTIVE;
655 out:
656 spin_unlock_irq(&ctx->lock);
657}
658
659/*
660 * Enable a counter and all its children.
661 */
662static void perf_counter_enable_family(struct perf_counter *counter)
663{
664 struct perf_counter *child;
665
666 perf_counter_enable(counter);
667
668 /*
669 * Lock the mutex to protect the list of children
670 */
671 mutex_lock(&counter->mutex);
672 list_for_each_entry(child, &counter->child_list, child_list)
673 perf_counter_enable(child);
674 mutex_unlock(&counter->mutex);
Ingo Molnar04289bb2008-12-11 08:38:42 +0100675}
676
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100677void __perf_counter_sched_out(struct perf_counter_context *ctx,
678 struct perf_cpu_context *cpuctx)
679{
680 struct perf_counter *counter;
Paul Mackerras3cbed422009-01-09 16:43:42 +1100681 u64 flags;
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100682
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100683 spin_lock(&ctx->lock);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100684 ctx->is_active = 0;
685 if (likely(!ctx->nr_counters))
686 goto out;
687
Paul Mackerras3cbed422009-01-09 16:43:42 +1100688 flags = hw_perf_save_disable();
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100689 if (ctx->nr_active) {
690 list_for_each_entry(counter, &ctx->counter_list, list_entry)
691 group_sched_out(counter, cpuctx, ctx);
692 }
Paul Mackerras3cbed422009-01-09 16:43:42 +1100693 hw_perf_restore(flags);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100694 out:
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100695 spin_unlock(&ctx->lock);
696}
697
Thomas Gleixner0793a612008-12-04 20:12:29 +0100698/*
699 * Called from scheduler to remove the counters of the current task,
700 * with interrupts disabled.
701 *
702 * We stop each counter and update the counter value in counter->count.
703 *
Ingo Molnar76715812008-12-17 14:20:28 +0100704 * This does not protect us against NMI, but disable()
Thomas Gleixner0793a612008-12-04 20:12:29 +0100705 * sets the disabled bit in the control field of counter _before_
706 * accessing the counter control register. If a NMI hits, then it will
707 * not restart the counter.
708 */
709void perf_counter_task_sched_out(struct task_struct *task, int cpu)
710{
711 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
712 struct perf_counter_context *ctx = &task->perf_counter_ctx;
Peter Zijlstra4a0deca2009-03-19 20:26:12 +0100713 struct pt_regs *regs;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100714
715 if (likely(!cpuctx->task_ctx))
716 return;
717
Peter Zijlstra4a0deca2009-03-19 20:26:12 +0100718 regs = task_pt_regs(task);
719 perf_swcounter_event(PERF_COUNT_CONTEXT_SWITCHES, 1, 1, regs);
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100720 __perf_counter_sched_out(ctx, cpuctx);
721
Thomas Gleixner0793a612008-12-04 20:12:29 +0100722 cpuctx->task_ctx = NULL;
723}
724
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100725static void perf_counter_cpu_sched_out(struct perf_cpu_context *cpuctx)
Ingo Molnar04289bb2008-12-11 08:38:42 +0100726{
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100727 __perf_counter_sched_out(&cpuctx->ctx, cpuctx);
Ingo Molnar04289bb2008-12-11 08:38:42 +0100728}
729
Ingo Molnar79958882008-12-17 08:54:56 +0100730static int
Ingo Molnar04289bb2008-12-11 08:38:42 +0100731group_sched_in(struct perf_counter *group_counter,
732 struct perf_cpu_context *cpuctx,
733 struct perf_counter_context *ctx,
734 int cpu)
735{
Ingo Molnar95cdd2e2008-12-21 13:50:42 +0100736 struct perf_counter *counter, *partial_group;
Paul Mackerras3cbed422009-01-09 16:43:42 +1100737 int ret;
738
739 if (group_counter->state == PERF_COUNTER_STATE_OFF)
740 return 0;
741
742 ret = hw_perf_group_sched_in(group_counter, cpuctx, ctx, cpu);
743 if (ret)
744 return ret < 0 ? ret : 0;
Ingo Molnar04289bb2008-12-11 08:38:42 +0100745
Paul Mackerrasc07c99b2009-02-13 22:10:34 +1100746 group_counter->prev_state = group_counter->state;
Ingo Molnar95cdd2e2008-12-21 13:50:42 +0100747 if (counter_sched_in(group_counter, cpuctx, ctx, cpu))
748 return -EAGAIN;
Ingo Molnar04289bb2008-12-11 08:38:42 +0100749
750 /*
751 * Schedule in siblings as one group (if any):
752 */
Ingo Molnar79958882008-12-17 08:54:56 +0100753 list_for_each_entry(counter, &group_counter->sibling_list, list_entry) {
Paul Mackerrasc07c99b2009-02-13 22:10:34 +1100754 counter->prev_state = counter->state;
Ingo Molnar95cdd2e2008-12-21 13:50:42 +0100755 if (counter_sched_in(counter, cpuctx, ctx, cpu)) {
756 partial_group = counter;
757 goto group_error;
758 }
Ingo Molnar79958882008-12-17 08:54:56 +0100759 }
760
Paul Mackerras3cbed422009-01-09 16:43:42 +1100761 return 0;
Ingo Molnar95cdd2e2008-12-21 13:50:42 +0100762
763group_error:
764 /*
765 * Groups can be scheduled in as one unit only, so undo any
766 * partial group before returning:
767 */
768 list_for_each_entry(counter, &group_counter->sibling_list, list_entry) {
769 if (counter == partial_group)
770 break;
771 counter_sched_out(counter, cpuctx, ctx);
772 }
773 counter_sched_out(group_counter, cpuctx, ctx);
774
775 return -EAGAIN;
Ingo Molnar04289bb2008-12-11 08:38:42 +0100776}
777
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100778static void
779__perf_counter_sched_in(struct perf_counter_context *ctx,
780 struct perf_cpu_context *cpuctx, int cpu)
Thomas Gleixner0793a612008-12-04 20:12:29 +0100781{
Thomas Gleixner0793a612008-12-04 20:12:29 +0100782 struct perf_counter *counter;
Paul Mackerras3cbed422009-01-09 16:43:42 +1100783 u64 flags;
Paul Mackerrasdd0e6ba2009-01-12 15:11:00 +1100784 int can_add_hw = 1;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100785
Thomas Gleixner0793a612008-12-04 20:12:29 +0100786 spin_lock(&ctx->lock);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100787 ctx->is_active = 1;
788 if (likely(!ctx->nr_counters))
789 goto out;
790
Paul Mackerras3cbed422009-01-09 16:43:42 +1100791 flags = hw_perf_save_disable();
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100792
793 /*
794 * First go through the list and put on any pinned groups
795 * in order to give them the best chance of going on.
796 */
Ingo Molnar04289bb2008-12-11 08:38:42 +0100797 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100798 if (counter->state <= PERF_COUNTER_STATE_OFF ||
799 !counter->hw_event.pinned)
800 continue;
801 if (counter->cpu != -1 && counter->cpu != cpu)
802 continue;
803
804 if (group_can_go_on(counter, cpuctx, 1))
805 group_sched_in(counter, cpuctx, ctx, cpu);
806
807 /*
808 * If this pinned group hasn't been scheduled,
809 * put it in error state.
810 */
811 if (counter->state == PERF_COUNTER_STATE_INACTIVE)
812 counter->state = PERF_COUNTER_STATE_ERROR;
813 }
814
815 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
816 /*
817 * Ignore counters in OFF or ERROR state, and
818 * ignore pinned counters since we did them already.
819 */
820 if (counter->state <= PERF_COUNTER_STATE_OFF ||
821 counter->hw_event.pinned)
822 continue;
823
Ingo Molnar04289bb2008-12-11 08:38:42 +0100824 /*
825 * Listen to the 'cpu' scheduling filter constraint
826 * of counters:
827 */
Thomas Gleixner0793a612008-12-04 20:12:29 +0100828 if (counter->cpu != -1 && counter->cpu != cpu)
829 continue;
830
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100831 if (group_can_go_on(counter, cpuctx, can_add_hw)) {
Paul Mackerrasdd0e6ba2009-01-12 15:11:00 +1100832 if (group_sched_in(counter, cpuctx, ctx, cpu))
833 can_add_hw = 0;
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100834 }
Thomas Gleixner0793a612008-12-04 20:12:29 +0100835 }
Paul Mackerras3cbed422009-01-09 16:43:42 +1100836 hw_perf_restore(flags);
Paul Mackerrasd859e292009-01-17 18:10:22 +1100837 out:
Thomas Gleixner0793a612008-12-04 20:12:29 +0100838 spin_unlock(&ctx->lock);
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100839}
Ingo Molnar04289bb2008-12-11 08:38:42 +0100840
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100841/*
842 * Called from scheduler to add the counters of the current task
843 * with interrupts disabled.
844 *
845 * We restore the counter value and then enable it.
846 *
847 * This does not protect us against NMI, but enable()
848 * sets the enabled bit in the control field of counter _before_
849 * accessing the counter control register. If a NMI hits, then it will
850 * keep the counter running.
851 */
852void perf_counter_task_sched_in(struct task_struct *task, int cpu)
853{
854 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
855 struct perf_counter_context *ctx = &task->perf_counter_ctx;
856
857 __perf_counter_sched_in(ctx, cpuctx, cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100858 cpuctx->task_ctx = ctx;
859}
860
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100861static void perf_counter_cpu_sched_in(struct perf_cpu_context *cpuctx, int cpu)
862{
863 struct perf_counter_context *ctx = &cpuctx->ctx;
864
865 __perf_counter_sched_in(ctx, cpuctx, cpu);
866}
867
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +0100868int perf_counter_task_disable(void)
869{
870 struct task_struct *curr = current;
871 struct perf_counter_context *ctx = &curr->perf_counter_ctx;
872 struct perf_counter *counter;
Ingo Molnaraa9c4c02008-12-17 14:10:57 +0100873 unsigned long flags;
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +0100874 u64 perf_flags;
875 int cpu;
876
877 if (likely(!ctx->nr_counters))
878 return 0;
879
Ingo Molnaraa9c4c02008-12-17 14:10:57 +0100880 curr_rq_lock_irq_save(&flags);
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +0100881 cpu = smp_processor_id();
882
Ingo Molnaraa9c4c02008-12-17 14:10:57 +0100883 /* force the update of the task clock: */
884 __task_delta_exec(curr, 1);
885
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +0100886 perf_counter_task_sched_out(curr, cpu);
887
888 spin_lock(&ctx->lock);
889
890 /*
891 * Disable all the counters:
892 */
893 perf_flags = hw_perf_save_disable();
894
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100895 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
896 if (counter->state != PERF_COUNTER_STATE_ERROR)
897 counter->state = PERF_COUNTER_STATE_OFF;
898 }
Ingo Molnar9b51f662008-12-12 13:49:45 +0100899
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +0100900 hw_perf_restore(perf_flags);
901
902 spin_unlock(&ctx->lock);
903
Ingo Molnaraa9c4c02008-12-17 14:10:57 +0100904 curr_rq_unlock_irq_restore(&flags);
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +0100905
906 return 0;
907}
908
909int perf_counter_task_enable(void)
910{
911 struct task_struct *curr = current;
912 struct perf_counter_context *ctx = &curr->perf_counter_ctx;
913 struct perf_counter *counter;
Ingo Molnaraa9c4c02008-12-17 14:10:57 +0100914 unsigned long flags;
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +0100915 u64 perf_flags;
916 int cpu;
917
918 if (likely(!ctx->nr_counters))
919 return 0;
920
Ingo Molnaraa9c4c02008-12-17 14:10:57 +0100921 curr_rq_lock_irq_save(&flags);
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +0100922 cpu = smp_processor_id();
923
Ingo Molnaraa9c4c02008-12-17 14:10:57 +0100924 /* force the update of the task clock: */
925 __task_delta_exec(curr, 1);
926
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100927 perf_counter_task_sched_out(curr, cpu);
928
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +0100929 spin_lock(&ctx->lock);
930
931 /*
932 * Disable all the counters:
933 */
934 perf_flags = hw_perf_save_disable();
935
936 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100937 if (counter->state > PERF_COUNTER_STATE_OFF)
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +0100938 continue;
Ingo Molnar6a930702008-12-11 15:17:03 +0100939 counter->state = PERF_COUNTER_STATE_INACTIVE;
Ingo Molnaraa9c4c02008-12-17 14:10:57 +0100940 counter->hw_event.disabled = 0;
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +0100941 }
942 hw_perf_restore(perf_flags);
943
944 spin_unlock(&ctx->lock);
945
946 perf_counter_task_sched_in(curr, cpu);
947
Ingo Molnaraa9c4c02008-12-17 14:10:57 +0100948 curr_rq_unlock_irq_restore(&flags);
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +0100949
950 return 0;
951}
952
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100953/*
954 * Round-robin a context's counters:
955 */
956static void rotate_ctx(struct perf_counter_context *ctx)
Thomas Gleixner0793a612008-12-04 20:12:29 +0100957{
Thomas Gleixner0793a612008-12-04 20:12:29 +0100958 struct perf_counter *counter;
Ingo Molnar5c92d122008-12-11 13:21:10 +0100959 u64 perf_flags;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100960
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100961 if (!ctx->nr_counters)
Thomas Gleixner0793a612008-12-04 20:12:29 +0100962 return;
963
Thomas Gleixner0793a612008-12-04 20:12:29 +0100964 spin_lock(&ctx->lock);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100965 /*
Ingo Molnar04289bb2008-12-11 08:38:42 +0100966 * Rotate the first entry last (works just fine for group counters too):
Thomas Gleixner0793a612008-12-04 20:12:29 +0100967 */
Ingo Molnar01b28382008-12-11 13:45:51 +0100968 perf_flags = hw_perf_save_disable();
Ingo Molnar04289bb2008-12-11 08:38:42 +0100969 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
Peter Zijlstra75564232009-03-13 12:21:29 +0100970 list_move_tail(&counter->list_entry, &ctx->counter_list);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100971 break;
972 }
Ingo Molnar01b28382008-12-11 13:45:51 +0100973 hw_perf_restore(perf_flags);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100974
975 spin_unlock(&ctx->lock);
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100976}
Thomas Gleixner0793a612008-12-04 20:12:29 +0100977
Ingo Molnar235c7fc2008-12-21 14:43:25 +0100978void perf_counter_task_tick(struct task_struct *curr, int cpu)
979{
980 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
981 struct perf_counter_context *ctx = &curr->perf_counter_ctx;
982 const int rotate_percpu = 0;
983
984 if (rotate_percpu)
985 perf_counter_cpu_sched_out(cpuctx);
986 perf_counter_task_sched_out(curr, cpu);
987
988 if (rotate_percpu)
989 rotate_ctx(&cpuctx->ctx);
990 rotate_ctx(ctx);
991
992 if (rotate_percpu)
993 perf_counter_cpu_sched_in(cpuctx, cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100994 perf_counter_task_sched_in(curr, cpu);
995}
996
997/*
Thomas Gleixner0793a612008-12-04 20:12:29 +0100998 * Cross CPU call to read the hardware counter
999 */
Ingo Molnar76715812008-12-17 14:20:28 +01001000static void __read(void *info)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001001{
Ingo Molnar621a01e2008-12-11 12:46:46 +01001002 struct perf_counter *counter = info;
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01001003 unsigned long flags;
Ingo Molnar621a01e2008-12-11 12:46:46 +01001004
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01001005 curr_rq_lock_irq_save(&flags);
Ingo Molnar76715812008-12-17 14:20:28 +01001006 counter->hw_ops->read(counter);
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01001007 curr_rq_unlock_irq_restore(&flags);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001008}
1009
Ingo Molnar04289bb2008-12-11 08:38:42 +01001010static u64 perf_counter_read(struct perf_counter *counter)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001011{
1012 /*
1013 * If counter is enabled and currently active on a CPU, update the
1014 * value in the counter structure:
1015 */
Ingo Molnar6a930702008-12-11 15:17:03 +01001016 if (counter->state == PERF_COUNTER_STATE_ACTIVE) {
Thomas Gleixner0793a612008-12-04 20:12:29 +01001017 smp_call_function_single(counter->oncpu,
Ingo Molnar76715812008-12-17 14:20:28 +01001018 __read, counter, 1);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001019 }
1020
Ingo Molnaree060942008-12-13 09:00:03 +01001021 return atomic64_read(&counter->count);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001022}
1023
1024/*
1025 * Cross CPU call to switch performance data pointers
1026 */
1027static void __perf_switch_irq_data(void *info)
1028{
1029 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
1030 struct perf_counter *counter = info;
1031 struct perf_counter_context *ctx = counter->ctx;
1032 struct perf_data *oldirqdata = counter->irqdata;
1033
1034 /*
1035 * If this is a task context, we need to check whether it is
1036 * the current task context of this cpu. If not it has been
1037 * scheduled out before the smp call arrived.
1038 */
1039 if (ctx->task) {
1040 if (cpuctx->task_ctx != ctx)
1041 return;
1042 spin_lock(&ctx->lock);
1043 }
1044
1045 /* Change the pointer NMI safe */
1046 atomic_long_set((atomic_long_t *)&counter->irqdata,
1047 (unsigned long) counter->usrdata);
1048 counter->usrdata = oldirqdata;
1049
1050 if (ctx->task)
1051 spin_unlock(&ctx->lock);
1052}
1053
1054static struct perf_data *perf_switch_irq_data(struct perf_counter *counter)
1055{
1056 struct perf_counter_context *ctx = counter->ctx;
1057 struct perf_data *oldirqdata = counter->irqdata;
1058 struct task_struct *task = ctx->task;
1059
1060 if (!task) {
1061 smp_call_function_single(counter->cpu,
1062 __perf_switch_irq_data,
1063 counter, 1);
1064 return counter->usrdata;
1065 }
1066
1067retry:
1068 spin_lock_irq(&ctx->lock);
Ingo Molnar6a930702008-12-11 15:17:03 +01001069 if (counter->state != PERF_COUNTER_STATE_ACTIVE) {
Thomas Gleixner0793a612008-12-04 20:12:29 +01001070 counter->irqdata = counter->usrdata;
1071 counter->usrdata = oldirqdata;
1072 spin_unlock_irq(&ctx->lock);
1073 return oldirqdata;
1074 }
1075 spin_unlock_irq(&ctx->lock);
1076 task_oncpu_function_call(task, __perf_switch_irq_data, counter);
1077 /* Might have failed, because task was scheduled out */
1078 if (counter->irqdata == oldirqdata)
1079 goto retry;
1080
1081 return counter->usrdata;
1082}
1083
1084static void put_context(struct perf_counter_context *ctx)
1085{
1086 if (ctx->task)
1087 put_task_struct(ctx->task);
1088}
1089
1090static struct perf_counter_context *find_get_context(pid_t pid, int cpu)
1091{
1092 struct perf_cpu_context *cpuctx;
1093 struct perf_counter_context *ctx;
1094 struct task_struct *task;
1095
1096 /*
1097 * If cpu is not a wildcard then this is a percpu counter:
1098 */
1099 if (cpu != -1) {
1100 /* Must be root to operate on a CPU counter: */
1101 if (!capable(CAP_SYS_ADMIN))
1102 return ERR_PTR(-EACCES);
1103
1104 if (cpu < 0 || cpu > num_possible_cpus())
1105 return ERR_PTR(-EINVAL);
1106
1107 /*
1108 * We could be clever and allow to attach a counter to an
1109 * offline CPU and activate it when the CPU comes up, but
1110 * that's for later.
1111 */
1112 if (!cpu_isset(cpu, cpu_online_map))
1113 return ERR_PTR(-ENODEV);
1114
1115 cpuctx = &per_cpu(perf_cpu_context, cpu);
1116 ctx = &cpuctx->ctx;
1117
Thomas Gleixner0793a612008-12-04 20:12:29 +01001118 return ctx;
1119 }
1120
1121 rcu_read_lock();
1122 if (!pid)
1123 task = current;
1124 else
1125 task = find_task_by_vpid(pid);
1126 if (task)
1127 get_task_struct(task);
1128 rcu_read_unlock();
1129
1130 if (!task)
1131 return ERR_PTR(-ESRCH);
1132
1133 ctx = &task->perf_counter_ctx;
1134 ctx->task = task;
1135
1136 /* Reuse ptrace permission checks for now. */
1137 if (!ptrace_may_access(task, PTRACE_MODE_READ)) {
1138 put_context(ctx);
1139 return ERR_PTR(-EACCES);
1140 }
1141
1142 return ctx;
1143}
1144
Peter Zijlstra592903c2009-03-13 12:21:36 +01001145static void free_counter_rcu(struct rcu_head *head)
1146{
1147 struct perf_counter *counter;
1148
1149 counter = container_of(head, struct perf_counter, rcu_head);
1150 kfree(counter);
1151}
1152
Peter Zijlstraf1600952009-03-19 20:26:16 +01001153static void free_counter(struct perf_counter *counter)
1154{
1155 call_rcu(&counter->rcu_head, free_counter_rcu);
1156}
1157
Thomas Gleixner0793a612008-12-04 20:12:29 +01001158/*
1159 * Called when the last reference to the file is gone.
1160 */
1161static int perf_release(struct inode *inode, struct file *file)
1162{
1163 struct perf_counter *counter = file->private_data;
1164 struct perf_counter_context *ctx = counter->ctx;
1165
1166 file->private_data = NULL;
1167
Paul Mackerrasd859e292009-01-17 18:10:22 +11001168 mutex_lock(&ctx->mutex);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001169 mutex_lock(&counter->mutex);
1170
Ingo Molnar04289bb2008-12-11 08:38:42 +01001171 perf_counter_remove_from_context(counter);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001172
1173 mutex_unlock(&counter->mutex);
Paul Mackerrasd859e292009-01-17 18:10:22 +11001174 mutex_unlock(&ctx->mutex);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001175
Peter Zijlstraf1600952009-03-19 20:26:16 +01001176 free_counter(counter);
Mike Galbraith5af75912009-02-11 10:53:37 +01001177 put_context(ctx);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001178
1179 return 0;
1180}
1181
1182/*
1183 * Read the performance counter - simple non blocking version for now
1184 */
1185static ssize_t
1186perf_read_hw(struct perf_counter *counter, char __user *buf, size_t count)
1187{
1188 u64 cntval;
1189
1190 if (count != sizeof(cntval))
1191 return -EINVAL;
1192
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001193 /*
1194 * Return end-of-file for a read on a counter that is in
1195 * error state (i.e. because it was pinned but it couldn't be
1196 * scheduled on to the CPU at some point).
1197 */
1198 if (counter->state == PERF_COUNTER_STATE_ERROR)
1199 return 0;
1200
Thomas Gleixner0793a612008-12-04 20:12:29 +01001201 mutex_lock(&counter->mutex);
Ingo Molnar04289bb2008-12-11 08:38:42 +01001202 cntval = perf_counter_read(counter);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001203 mutex_unlock(&counter->mutex);
1204
1205 return put_user(cntval, (u64 __user *) buf) ? -EFAULT : sizeof(cntval);
1206}
1207
1208static ssize_t
1209perf_copy_usrdata(struct perf_data *usrdata, char __user *buf, size_t count)
1210{
1211 if (!usrdata->len)
1212 return 0;
1213
1214 count = min(count, (size_t)usrdata->len);
1215 if (copy_to_user(buf, usrdata->data + usrdata->rd_idx, count))
1216 return -EFAULT;
1217
1218 /* Adjust the counters */
1219 usrdata->len -= count;
1220 if (!usrdata->len)
1221 usrdata->rd_idx = 0;
1222 else
1223 usrdata->rd_idx += count;
1224
1225 return count;
1226}
1227
1228static ssize_t
1229perf_read_irq_data(struct perf_counter *counter,
1230 char __user *buf,
1231 size_t count,
1232 int nonblocking)
1233{
1234 struct perf_data *irqdata, *usrdata;
1235 DECLARE_WAITQUEUE(wait, current);
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001236 ssize_t res, res2;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001237
1238 irqdata = counter->irqdata;
1239 usrdata = counter->usrdata;
1240
1241 if (usrdata->len + irqdata->len >= count)
1242 goto read_pending;
1243
1244 if (nonblocking)
1245 return -EAGAIN;
1246
1247 spin_lock_irq(&counter->waitq.lock);
1248 __add_wait_queue(&counter->waitq, &wait);
1249 for (;;) {
1250 set_current_state(TASK_INTERRUPTIBLE);
1251 if (usrdata->len + irqdata->len >= count)
1252 break;
1253
1254 if (signal_pending(current))
1255 break;
1256
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001257 if (counter->state == PERF_COUNTER_STATE_ERROR)
1258 break;
1259
Thomas Gleixner0793a612008-12-04 20:12:29 +01001260 spin_unlock_irq(&counter->waitq.lock);
1261 schedule();
1262 spin_lock_irq(&counter->waitq.lock);
1263 }
1264 __remove_wait_queue(&counter->waitq, &wait);
1265 __set_current_state(TASK_RUNNING);
1266 spin_unlock_irq(&counter->waitq.lock);
1267
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001268 if (usrdata->len + irqdata->len < count &&
1269 counter->state != PERF_COUNTER_STATE_ERROR)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001270 return -ERESTARTSYS;
1271read_pending:
1272 mutex_lock(&counter->mutex);
1273
1274 /* Drain pending data first: */
1275 res = perf_copy_usrdata(usrdata, buf, count);
1276 if (res < 0 || res == count)
1277 goto out;
1278
1279 /* Switch irq buffer: */
1280 usrdata = perf_switch_irq_data(counter);
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001281 res2 = perf_copy_usrdata(usrdata, buf + res, count - res);
1282 if (res2 < 0) {
Thomas Gleixner0793a612008-12-04 20:12:29 +01001283 if (!res)
1284 res = -EFAULT;
1285 } else {
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001286 res += res2;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001287 }
1288out:
1289 mutex_unlock(&counter->mutex);
1290
1291 return res;
1292}
1293
1294static ssize_t
1295perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
1296{
1297 struct perf_counter *counter = file->private_data;
1298
Ingo Molnar9f66a382008-12-10 12:33:23 +01001299 switch (counter->hw_event.record_type) {
Thomas Gleixner0793a612008-12-04 20:12:29 +01001300 case PERF_RECORD_SIMPLE:
1301 return perf_read_hw(counter, buf, count);
1302
1303 case PERF_RECORD_IRQ:
1304 case PERF_RECORD_GROUP:
1305 return perf_read_irq_data(counter, buf, count,
1306 file->f_flags & O_NONBLOCK);
1307 }
1308 return -EINVAL;
1309}
1310
1311static unsigned int perf_poll(struct file *file, poll_table *wait)
1312{
1313 struct perf_counter *counter = file->private_data;
1314 unsigned int events = 0;
1315 unsigned long flags;
1316
1317 poll_wait(file, &counter->waitq, wait);
1318
1319 spin_lock_irqsave(&counter->waitq.lock, flags);
1320 if (counter->usrdata->len || counter->irqdata->len)
1321 events |= POLLIN;
1322 spin_unlock_irqrestore(&counter->waitq.lock, flags);
1323
1324 return events;
1325}
1326
Paul Mackerrasd859e292009-01-17 18:10:22 +11001327static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1328{
1329 struct perf_counter *counter = file->private_data;
1330 int err = 0;
1331
1332 switch (cmd) {
1333 case PERF_COUNTER_IOC_ENABLE:
1334 perf_counter_enable_family(counter);
1335 break;
1336 case PERF_COUNTER_IOC_DISABLE:
1337 perf_counter_disable_family(counter);
1338 break;
1339 default:
1340 err = -ENOTTY;
1341 }
1342 return err;
1343}
1344
Thomas Gleixner0793a612008-12-04 20:12:29 +01001345static const struct file_operations perf_fops = {
1346 .release = perf_release,
1347 .read = perf_read,
1348 .poll = perf_poll,
Paul Mackerrasd859e292009-01-17 18:10:22 +11001349 .unlocked_ioctl = perf_ioctl,
1350 .compat_ioctl = perf_ioctl,
Thomas Gleixner0793a612008-12-04 20:12:29 +01001351};
1352
Peter Zijlstra15dbf272009-03-13 12:21:32 +01001353/*
1354 * Generic software counter infrastructure
1355 */
1356
1357static void perf_swcounter_update(struct perf_counter *counter)
1358{
1359 struct hw_perf_counter *hwc = &counter->hw;
1360 u64 prev, now;
1361 s64 delta;
1362
1363again:
1364 prev = atomic64_read(&hwc->prev_count);
1365 now = atomic64_read(&hwc->count);
1366 if (atomic64_cmpxchg(&hwc->prev_count, prev, now) != prev)
1367 goto again;
1368
1369 delta = now - prev;
1370
1371 atomic64_add(delta, &counter->count);
1372 atomic64_sub(delta, &hwc->period_left);
1373}
1374
1375static void perf_swcounter_set_period(struct perf_counter *counter)
1376{
1377 struct hw_perf_counter *hwc = &counter->hw;
1378 s64 left = atomic64_read(&hwc->period_left);
1379 s64 period = hwc->irq_period;
1380
1381 if (unlikely(left <= -period)) {
1382 left = period;
1383 atomic64_set(&hwc->period_left, left);
1384 }
1385
1386 if (unlikely(left <= 0)) {
1387 left += period;
1388 atomic64_add(period, &hwc->period_left);
1389 }
1390
1391 atomic64_set(&hwc->prev_count, -left);
1392 atomic64_set(&hwc->count, -left);
1393}
1394
1395static void perf_swcounter_save_and_restart(struct perf_counter *counter)
1396{
1397 perf_swcounter_update(counter);
1398 perf_swcounter_set_period(counter);
1399}
1400
1401static void perf_swcounter_store_irq(struct perf_counter *counter, u64 data)
1402{
1403 struct perf_data *irqdata = counter->irqdata;
1404
1405 if (irqdata->len > PERF_DATA_BUFLEN - sizeof(u64)) {
1406 irqdata->overrun++;
1407 } else {
1408 u64 *p = (u64 *) &irqdata->data[irqdata->len];
1409
1410 *p = data;
1411 irqdata->len += sizeof(u64);
1412 }
1413}
1414
1415static void perf_swcounter_handle_group(struct perf_counter *sibling)
1416{
1417 struct perf_counter *counter, *group_leader = sibling->group_leader;
1418
1419 list_for_each_entry(counter, &group_leader->sibling_list, list_entry) {
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01001420 counter->hw_ops->read(counter);
Peter Zijlstra15dbf272009-03-13 12:21:32 +01001421 perf_swcounter_store_irq(sibling, counter->hw_event.type);
1422 perf_swcounter_store_irq(sibling, atomic64_read(&counter->count));
1423 }
1424}
1425
1426static void perf_swcounter_interrupt(struct perf_counter *counter,
1427 int nmi, struct pt_regs *regs)
1428{
Peter Zijlstra15dbf272009-03-13 12:21:32 +01001429 switch (counter->hw_event.record_type) {
1430 case PERF_RECORD_SIMPLE:
1431 break;
1432
1433 case PERF_RECORD_IRQ:
1434 perf_swcounter_store_irq(counter, instruction_pointer(regs));
1435 break;
1436
1437 case PERF_RECORD_GROUP:
1438 perf_swcounter_handle_group(counter);
1439 break;
1440 }
1441
1442 if (nmi) {
1443 counter->wakeup_pending = 1;
Paul Mackerrasb6c5a71d2009-03-16 21:00:00 +11001444 set_perf_counter_pending();
Peter Zijlstra15dbf272009-03-13 12:21:32 +01001445 } else
1446 wake_up(&counter->waitq);
1447}
1448
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01001449static enum hrtimer_restart perf_swcounter_hrtimer(struct hrtimer *hrtimer)
1450{
1451 struct perf_counter *counter;
1452 struct pt_regs *regs;
1453
1454 counter = container_of(hrtimer, struct perf_counter, hw.hrtimer);
1455 counter->hw_ops->read(counter);
1456
1457 regs = get_irq_regs();
1458 /*
1459 * In case we exclude kernel IPs or are somehow not in interrupt
1460 * context, provide the next best thing, the user IP.
1461 */
1462 if ((counter->hw_event.exclude_kernel || !regs) &&
1463 !counter->hw_event.exclude_user)
1464 regs = task_pt_regs(current);
1465
1466 if (regs)
1467 perf_swcounter_interrupt(counter, 0, regs);
1468
1469 hrtimer_forward_now(hrtimer, ns_to_ktime(counter->hw.irq_period));
1470
1471 return HRTIMER_RESTART;
1472}
1473
1474static void perf_swcounter_overflow(struct perf_counter *counter,
1475 int nmi, struct pt_regs *regs)
1476{
1477 perf_swcounter_save_and_restart(counter);
1478 perf_swcounter_interrupt(counter, nmi, regs);
1479}
1480
Peter Zijlstra15dbf272009-03-13 12:21:32 +01001481static int perf_swcounter_match(struct perf_counter *counter,
1482 enum hw_event_types event,
1483 struct pt_regs *regs)
1484{
1485 if (counter->state != PERF_COUNTER_STATE_ACTIVE)
1486 return 0;
1487
1488 if (counter->hw_event.raw)
1489 return 0;
1490
1491 if (counter->hw_event.type != event)
1492 return 0;
1493
1494 if (counter->hw_event.exclude_user && user_mode(regs))
1495 return 0;
1496
1497 if (counter->hw_event.exclude_kernel && !user_mode(regs))
1498 return 0;
1499
1500 return 1;
1501}
1502
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01001503static void perf_swcounter_add(struct perf_counter *counter, u64 nr,
1504 int nmi, struct pt_regs *regs)
1505{
1506 int neg = atomic64_add_negative(nr, &counter->hw.count);
1507 if (counter->hw.irq_period && !neg)
1508 perf_swcounter_overflow(counter, nmi, regs);
1509}
1510
Peter Zijlstra15dbf272009-03-13 12:21:32 +01001511static void perf_swcounter_ctx_event(struct perf_counter_context *ctx,
1512 enum hw_event_types event, u64 nr,
1513 int nmi, struct pt_regs *regs)
1514{
1515 struct perf_counter *counter;
Peter Zijlstra15dbf272009-03-13 12:21:32 +01001516
Peter Zijlstra01ef09d2009-03-19 20:26:11 +01001517 if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
Peter Zijlstra15dbf272009-03-13 12:21:32 +01001518 return;
1519
Peter Zijlstra592903c2009-03-13 12:21:36 +01001520 rcu_read_lock();
1521 list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01001522 if (perf_swcounter_match(counter, event, regs))
1523 perf_swcounter_add(counter, nr, nmi, regs);
Peter Zijlstra15dbf272009-03-13 12:21:32 +01001524 }
Peter Zijlstra592903c2009-03-13 12:21:36 +01001525 rcu_read_unlock();
Peter Zijlstra15dbf272009-03-13 12:21:32 +01001526}
1527
1528void perf_swcounter_event(enum hw_event_types event, u64 nr,
1529 int nmi, struct pt_regs *regs)
1530{
1531 struct perf_cpu_context *cpuctx = &get_cpu_var(perf_cpu_context);
1532
1533 perf_swcounter_ctx_event(&cpuctx->ctx, event, nr, nmi, regs);
1534 if (cpuctx->task_ctx)
1535 perf_swcounter_ctx_event(cpuctx->task_ctx, event, nr, nmi, regs);
1536
1537 put_cpu_var(perf_cpu_context);
1538}
1539
1540static void perf_swcounter_read(struct perf_counter *counter)
1541{
1542 perf_swcounter_update(counter);
1543}
1544
1545static int perf_swcounter_enable(struct perf_counter *counter)
1546{
1547 perf_swcounter_set_period(counter);
1548 return 0;
1549}
1550
1551static void perf_swcounter_disable(struct perf_counter *counter)
1552{
1553 perf_swcounter_update(counter);
1554}
1555
Peter Zijlstraac17dc82009-03-13 12:21:34 +01001556static const struct hw_perf_counter_ops perf_ops_generic = {
1557 .enable = perf_swcounter_enable,
1558 .disable = perf_swcounter_disable,
1559 .read = perf_swcounter_read,
1560};
1561
Peter Zijlstra15dbf272009-03-13 12:21:32 +01001562/*
1563 * Software counter: cpu wall time clock
1564 */
1565
Paul Mackerras9abf8a02009-01-09 16:26:43 +11001566static void cpu_clock_perf_counter_update(struct perf_counter *counter)
1567{
1568 int cpu = raw_smp_processor_id();
1569 s64 prev;
1570 u64 now;
1571
1572 now = cpu_clock(cpu);
1573 prev = atomic64_read(&counter->hw.prev_count);
1574 atomic64_set(&counter->hw.prev_count, now);
1575 atomic64_add(now - prev, &counter->count);
1576}
1577
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01001578static int cpu_clock_perf_counter_enable(struct perf_counter *counter)
1579{
1580 struct hw_perf_counter *hwc = &counter->hw;
1581 int cpu = raw_smp_processor_id();
1582
1583 atomic64_set(&hwc->prev_count, cpu_clock(cpu));
Peter Zijlstra039fc912009-03-13 16:43:47 +01001584 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1585 hwc->hrtimer.function = perf_swcounter_hrtimer;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01001586 if (hwc->irq_period) {
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01001587 __hrtimer_start_range_ns(&hwc->hrtimer,
1588 ns_to_ktime(hwc->irq_period), 0,
1589 HRTIMER_MODE_REL, 0);
1590 }
1591
1592 return 0;
1593}
1594
Ingo Molnar5c92d122008-12-11 13:21:10 +01001595static void cpu_clock_perf_counter_disable(struct perf_counter *counter)
1596{
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01001597 hrtimer_cancel(&counter->hw.hrtimer);
Paul Mackerras9abf8a02009-01-09 16:26:43 +11001598 cpu_clock_perf_counter_update(counter);
Ingo Molnar5c92d122008-12-11 13:21:10 +01001599}
1600
1601static void cpu_clock_perf_counter_read(struct perf_counter *counter)
1602{
Paul Mackerras9abf8a02009-01-09 16:26:43 +11001603 cpu_clock_perf_counter_update(counter);
Ingo Molnar5c92d122008-12-11 13:21:10 +01001604}
1605
1606static const struct hw_perf_counter_ops perf_ops_cpu_clock = {
Ingo Molnar76715812008-12-17 14:20:28 +01001607 .enable = cpu_clock_perf_counter_enable,
1608 .disable = cpu_clock_perf_counter_disable,
1609 .read = cpu_clock_perf_counter_read,
Ingo Molnar5c92d122008-12-11 13:21:10 +01001610};
1611
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01001612/*
Peter Zijlstra15dbf272009-03-13 12:21:32 +01001613 * Software counter: task time clock
1614 */
1615
1616/*
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01001617 * Called from within the scheduler:
1618 */
1619static u64 task_clock_perf_counter_val(struct perf_counter *counter, int update)
Ingo Molnarbae43c92008-12-11 14:03:20 +01001620{
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01001621 struct task_struct *curr = counter->task;
1622 u64 delta;
1623
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01001624 delta = __task_delta_exec(curr, update);
1625
1626 return curr->se.sum_exec_runtime + delta;
1627}
1628
1629static void task_clock_perf_counter_update(struct perf_counter *counter, u64 now)
1630{
1631 u64 prev;
Ingo Molnar8cb391e2008-12-14 12:22:31 +01001632 s64 delta;
Ingo Molnarbae43c92008-12-11 14:03:20 +01001633
Ingo Molnar8cb391e2008-12-14 12:22:31 +01001634 prev = atomic64_read(&counter->hw.prev_count);
Ingo Molnar8cb391e2008-12-14 12:22:31 +01001635
1636 atomic64_set(&counter->hw.prev_count, now);
1637
1638 delta = now - prev;
Ingo Molnar8cb391e2008-12-14 12:22:31 +01001639
1640 atomic64_add(delta, &counter->count);
Ingo Molnarbae43c92008-12-11 14:03:20 +01001641}
1642
Ingo Molnar95cdd2e2008-12-21 13:50:42 +01001643static int task_clock_perf_counter_enable(struct perf_counter *counter)
Ingo Molnar8cb391e2008-12-14 12:22:31 +01001644{
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01001645 struct hw_perf_counter *hwc = &counter->hw;
1646
1647 atomic64_set(&hwc->prev_count, task_clock_perf_counter_val(counter, 0));
Peter Zijlstra039fc912009-03-13 16:43:47 +01001648 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1649 hwc->hrtimer.function = perf_swcounter_hrtimer;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01001650 if (hwc->irq_period) {
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01001651 __hrtimer_start_range_ns(&hwc->hrtimer,
1652 ns_to_ktime(hwc->irq_period), 0,
1653 HRTIMER_MODE_REL, 0);
1654 }
Ingo Molnar95cdd2e2008-12-21 13:50:42 +01001655
1656 return 0;
Ingo Molnar8cb391e2008-12-14 12:22:31 +01001657}
1658
1659static void task_clock_perf_counter_disable(struct perf_counter *counter)
1660{
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01001661 hrtimer_cancel(&counter->hw.hrtimer);
1662 task_clock_perf_counter_update(counter,
1663 task_clock_perf_counter_val(counter, 0));
1664}
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01001665
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01001666static void task_clock_perf_counter_read(struct perf_counter *counter)
1667{
1668 task_clock_perf_counter_update(counter,
1669 task_clock_perf_counter_val(counter, 1));
Ingo Molnarbae43c92008-12-11 14:03:20 +01001670}
1671
1672static const struct hw_perf_counter_ops perf_ops_task_clock = {
Ingo Molnar76715812008-12-17 14:20:28 +01001673 .enable = task_clock_perf_counter_enable,
1674 .disable = task_clock_perf_counter_disable,
1675 .read = task_clock_perf_counter_read,
Ingo Molnarbae43c92008-12-11 14:03:20 +01001676};
1677
Peter Zijlstra15dbf272009-03-13 12:21:32 +01001678/*
Peter Zijlstra15dbf272009-03-13 12:21:32 +01001679 * Software counter: cpu migrations
1680 */
1681
Paul Mackerras23a185c2009-02-09 22:42:47 +11001682static inline u64 get_cpu_migrations(struct perf_counter *counter)
Ingo Molnar6c594c22008-12-14 12:34:15 +01001683{
Paul Mackerras23a185c2009-02-09 22:42:47 +11001684 struct task_struct *curr = counter->ctx->task;
1685
1686 if (curr)
1687 return curr->se.nr_migrations;
1688 return cpu_nr_migrations(smp_processor_id());
Ingo Molnar6c594c22008-12-14 12:34:15 +01001689}
1690
1691static void cpu_migrations_perf_counter_update(struct perf_counter *counter)
1692{
1693 u64 prev, now;
1694 s64 delta;
1695
1696 prev = atomic64_read(&counter->hw.prev_count);
Paul Mackerras23a185c2009-02-09 22:42:47 +11001697 now = get_cpu_migrations(counter);
Ingo Molnar6c594c22008-12-14 12:34:15 +01001698
1699 atomic64_set(&counter->hw.prev_count, now);
1700
1701 delta = now - prev;
Ingo Molnar6c594c22008-12-14 12:34:15 +01001702
1703 atomic64_add(delta, &counter->count);
1704}
1705
1706static void cpu_migrations_perf_counter_read(struct perf_counter *counter)
1707{
1708 cpu_migrations_perf_counter_update(counter);
1709}
1710
Ingo Molnar95cdd2e2008-12-21 13:50:42 +01001711static int cpu_migrations_perf_counter_enable(struct perf_counter *counter)
Ingo Molnar6c594c22008-12-14 12:34:15 +01001712{
Paul Mackerrasc07c99b2009-02-13 22:10:34 +11001713 if (counter->prev_state <= PERF_COUNTER_STATE_OFF)
1714 atomic64_set(&counter->hw.prev_count,
1715 get_cpu_migrations(counter));
Ingo Molnar95cdd2e2008-12-21 13:50:42 +01001716 return 0;
Ingo Molnar6c594c22008-12-14 12:34:15 +01001717}
1718
1719static void cpu_migrations_perf_counter_disable(struct perf_counter *counter)
1720{
1721 cpu_migrations_perf_counter_update(counter);
1722}
1723
1724static const struct hw_perf_counter_ops perf_ops_cpu_migrations = {
Ingo Molnar76715812008-12-17 14:20:28 +01001725 .enable = cpu_migrations_perf_counter_enable,
1726 .disable = cpu_migrations_perf_counter_disable,
1727 .read = cpu_migrations_perf_counter_read,
Ingo Molnar6c594c22008-12-14 12:34:15 +01001728};
1729
Ingo Molnar5c92d122008-12-11 13:21:10 +01001730static const struct hw_perf_counter_ops *
1731sw_perf_counter_init(struct perf_counter *counter)
1732{
Peter Zijlstra15dbf272009-03-13 12:21:32 +01001733 struct perf_counter_hw_event *hw_event = &counter->hw_event;
Ingo Molnar5c92d122008-12-11 13:21:10 +01001734 const struct hw_perf_counter_ops *hw_ops = NULL;
Peter Zijlstra15dbf272009-03-13 12:21:32 +01001735 struct hw_perf_counter *hwc = &counter->hw;
Ingo Molnar5c92d122008-12-11 13:21:10 +01001736
Paul Mackerras0475f9e2009-02-11 14:35:35 +11001737 /*
1738 * Software counters (currently) can't in general distinguish
1739 * between user, kernel and hypervisor events.
1740 * However, context switches and cpu migrations are considered
1741 * to be kernel events, and page faults are never hypervisor
1742 * events.
1743 */
Ingo Molnar5c92d122008-12-11 13:21:10 +01001744 switch (counter->hw_event.type) {
1745 case PERF_COUNT_CPU_CLOCK:
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01001746 hw_ops = &perf_ops_cpu_clock;
1747
1748 if (hw_event->irq_period && hw_event->irq_period < 10000)
1749 hw_event->irq_period = 10000;
Ingo Molnar5c92d122008-12-11 13:21:10 +01001750 break;
Ingo Molnarbae43c92008-12-11 14:03:20 +01001751 case PERF_COUNT_TASK_CLOCK:
Paul Mackerras23a185c2009-02-09 22:42:47 +11001752 /*
1753 * If the user instantiates this as a per-cpu counter,
1754 * use the cpu_clock counter instead.
1755 */
1756 if (counter->ctx->task)
1757 hw_ops = &perf_ops_task_clock;
1758 else
1759 hw_ops = &perf_ops_cpu_clock;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +01001760
1761 if (hw_event->irq_period && hw_event->irq_period < 10000)
1762 hw_event->irq_period = 10000;
Ingo Molnarbae43c92008-12-11 14:03:20 +01001763 break;
Ingo Molnare06c61a2008-12-14 14:44:31 +01001764 case PERF_COUNT_PAGE_FAULTS:
Peter Zijlstraac17dc82009-03-13 12:21:34 +01001765 case PERF_COUNT_PAGE_FAULTS_MIN:
1766 case PERF_COUNT_PAGE_FAULTS_MAJ:
Ingo Molnar5d6a27d2008-12-14 12:28:33 +01001767 case PERF_COUNT_CONTEXT_SWITCHES:
Peter Zijlstra4a0deca2009-03-19 20:26:12 +01001768 hw_ops = &perf_ops_generic;
Ingo Molnar5d6a27d2008-12-14 12:28:33 +01001769 break;
Ingo Molnar6c594c22008-12-14 12:34:15 +01001770 case PERF_COUNT_CPU_MIGRATIONS:
Paul Mackerras0475f9e2009-02-11 14:35:35 +11001771 if (!counter->hw_event.exclude_kernel)
1772 hw_ops = &perf_ops_cpu_migrations;
Ingo Molnar6c594c22008-12-14 12:34:15 +01001773 break;
Ingo Molnar5c92d122008-12-11 13:21:10 +01001774 default:
1775 break;
1776 }
Peter Zijlstra15dbf272009-03-13 12:21:32 +01001777
1778 if (hw_ops)
1779 hwc->irq_period = hw_event->irq_period;
1780
Ingo Molnar5c92d122008-12-11 13:21:10 +01001781 return hw_ops;
1782}
1783
Thomas Gleixner0793a612008-12-04 20:12:29 +01001784/*
1785 * Allocate and initialize a counter structure
1786 */
1787static struct perf_counter *
Ingo Molnar04289bb2008-12-11 08:38:42 +01001788perf_counter_alloc(struct perf_counter_hw_event *hw_event,
1789 int cpu,
Paul Mackerras23a185c2009-02-09 22:42:47 +11001790 struct perf_counter_context *ctx,
Ingo Molnar9b51f662008-12-12 13:49:45 +01001791 struct perf_counter *group_leader,
1792 gfp_t gfpflags)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001793{
Ingo Molnar5c92d122008-12-11 13:21:10 +01001794 const struct hw_perf_counter_ops *hw_ops;
Ingo Molnar621a01e2008-12-11 12:46:46 +01001795 struct perf_counter *counter;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001796
Ingo Molnar9b51f662008-12-12 13:49:45 +01001797 counter = kzalloc(sizeof(*counter), gfpflags);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001798 if (!counter)
1799 return NULL;
1800
Ingo Molnar04289bb2008-12-11 08:38:42 +01001801 /*
1802 * Single counters are their own group leaders, with an
1803 * empty sibling list:
1804 */
1805 if (!group_leader)
1806 group_leader = counter;
1807
Thomas Gleixner0793a612008-12-04 20:12:29 +01001808 mutex_init(&counter->mutex);
Ingo Molnar04289bb2008-12-11 08:38:42 +01001809 INIT_LIST_HEAD(&counter->list_entry);
Peter Zijlstra592903c2009-03-13 12:21:36 +01001810 INIT_LIST_HEAD(&counter->event_entry);
Ingo Molnar04289bb2008-12-11 08:38:42 +01001811 INIT_LIST_HEAD(&counter->sibling_list);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001812 init_waitqueue_head(&counter->waitq);
1813
Paul Mackerrasd859e292009-01-17 18:10:22 +11001814 INIT_LIST_HEAD(&counter->child_list);
1815
Ingo Molnar9f66a382008-12-10 12:33:23 +01001816 counter->irqdata = &counter->data[0];
1817 counter->usrdata = &counter->data[1];
1818 counter->cpu = cpu;
1819 counter->hw_event = *hw_event;
1820 counter->wakeup_pending = 0;
Ingo Molnar04289bb2008-12-11 08:38:42 +01001821 counter->group_leader = group_leader;
Ingo Molnar621a01e2008-12-11 12:46:46 +01001822 counter->hw_ops = NULL;
Paul Mackerras23a185c2009-02-09 22:42:47 +11001823 counter->ctx = ctx;
Ingo Molnar621a01e2008-12-11 12:46:46 +01001824
Ingo Molnar235c7fc2008-12-21 14:43:25 +01001825 counter->state = PERF_COUNTER_STATE_INACTIVE;
Ingo Molnara86ed502008-12-17 00:43:10 +01001826 if (hw_event->disabled)
1827 counter->state = PERF_COUNTER_STATE_OFF;
1828
Ingo Molnar5c92d122008-12-11 13:21:10 +01001829 hw_ops = NULL;
1830 if (!hw_event->raw && hw_event->type < 0)
1831 hw_ops = sw_perf_counter_init(counter);
Paul Mackerras23a185c2009-02-09 22:42:47 +11001832 else
Ingo Molnar5c92d122008-12-11 13:21:10 +01001833 hw_ops = hw_perf_counter_init(counter);
Ingo Molnar5c92d122008-12-11 13:21:10 +01001834
Ingo Molnar621a01e2008-12-11 12:46:46 +01001835 if (!hw_ops) {
1836 kfree(counter);
1837 return NULL;
1838 }
1839 counter->hw_ops = hw_ops;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001840
1841 return counter;
1842}
1843
1844/**
Paul Mackerras2743a5b2009-03-04 20:36:51 +11001845 * sys_perf_counter_open - open a performance counter, associate it to a task/cpu
Ingo Molnar9f66a382008-12-10 12:33:23 +01001846 *
1847 * @hw_event_uptr: event type attributes for monitoring/sampling
Thomas Gleixner0793a612008-12-04 20:12:29 +01001848 * @pid: target pid
Ingo Molnar9f66a382008-12-10 12:33:23 +01001849 * @cpu: target cpu
1850 * @group_fd: group leader counter fd
Thomas Gleixner0793a612008-12-04 20:12:29 +01001851 */
Paul Mackerras2743a5b2009-03-04 20:36:51 +11001852SYSCALL_DEFINE5(perf_counter_open,
Paul Mackerrasf3dfd262009-02-26 22:43:46 +11001853 const struct perf_counter_hw_event __user *, hw_event_uptr,
Paul Mackerras2743a5b2009-03-04 20:36:51 +11001854 pid_t, pid, int, cpu, int, group_fd, unsigned long, flags)
Thomas Gleixner0793a612008-12-04 20:12:29 +01001855{
Ingo Molnar04289bb2008-12-11 08:38:42 +01001856 struct perf_counter *counter, *group_leader;
Ingo Molnar9f66a382008-12-10 12:33:23 +01001857 struct perf_counter_hw_event hw_event;
Ingo Molnar04289bb2008-12-11 08:38:42 +01001858 struct perf_counter_context *ctx;
Ingo Molnar9b51f662008-12-12 13:49:45 +01001859 struct file *counter_file = NULL;
Ingo Molnar04289bb2008-12-11 08:38:42 +01001860 struct file *group_file = NULL;
1861 int fput_needed = 0;
Ingo Molnar9b51f662008-12-12 13:49:45 +01001862 int fput_needed2 = 0;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001863 int ret;
1864
Paul Mackerras2743a5b2009-03-04 20:36:51 +11001865 /* for future expandability... */
1866 if (flags)
1867 return -EINVAL;
1868
Ingo Molnar9f66a382008-12-10 12:33:23 +01001869 if (copy_from_user(&hw_event, hw_event_uptr, sizeof(hw_event)) != 0)
Thomas Gleixnereab656a2008-12-08 19:26:59 +01001870 return -EFAULT;
1871
Ingo Molnar04289bb2008-12-11 08:38:42 +01001872 /*
Ingo Molnarccff2862008-12-11 11:26:29 +01001873 * Get the target context (task or percpu):
1874 */
1875 ctx = find_get_context(pid, cpu);
1876 if (IS_ERR(ctx))
1877 return PTR_ERR(ctx);
1878
1879 /*
1880 * Look up the group leader (we will attach this counter to it):
Ingo Molnar04289bb2008-12-11 08:38:42 +01001881 */
1882 group_leader = NULL;
1883 if (group_fd != -1) {
1884 ret = -EINVAL;
1885 group_file = fget_light(group_fd, &fput_needed);
1886 if (!group_file)
Ingo Molnarccff2862008-12-11 11:26:29 +01001887 goto err_put_context;
Ingo Molnar04289bb2008-12-11 08:38:42 +01001888 if (group_file->f_op != &perf_fops)
Ingo Molnarccff2862008-12-11 11:26:29 +01001889 goto err_put_context;
Ingo Molnar04289bb2008-12-11 08:38:42 +01001890
1891 group_leader = group_file->private_data;
1892 /*
Ingo Molnarccff2862008-12-11 11:26:29 +01001893 * Do not allow a recursive hierarchy (this new sibling
1894 * becoming part of another group-sibling):
Ingo Molnar04289bb2008-12-11 08:38:42 +01001895 */
Ingo Molnarccff2862008-12-11 11:26:29 +01001896 if (group_leader->group_leader != group_leader)
1897 goto err_put_context;
1898 /*
1899 * Do not allow to attach to a group in a different
1900 * task or CPU context:
1901 */
1902 if (group_leader->ctx != ctx)
1903 goto err_put_context;
Paul Mackerras3b6f9e52009-01-14 21:00:30 +11001904 /*
1905 * Only a group leader can be exclusive or pinned
1906 */
1907 if (hw_event.exclusive || hw_event.pinned)
1908 goto err_put_context;
Ingo Molnar04289bb2008-12-11 08:38:42 +01001909 }
1910
Ingo Molnar5c92d122008-12-11 13:21:10 +01001911 ret = -EINVAL;
Paul Mackerras23a185c2009-02-09 22:42:47 +11001912 counter = perf_counter_alloc(&hw_event, cpu, ctx, group_leader,
1913 GFP_KERNEL);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001914 if (!counter)
1915 goto err_put_context;
1916
Thomas Gleixner0793a612008-12-04 20:12:29 +01001917 ret = anon_inode_getfd("[perf_counter]", &perf_fops, counter, 0);
1918 if (ret < 0)
Ingo Molnar9b51f662008-12-12 13:49:45 +01001919 goto err_free_put_context;
1920
1921 counter_file = fget_light(ret, &fput_needed2);
1922 if (!counter_file)
1923 goto err_free_put_context;
1924
1925 counter->filp = counter_file;
Paul Mackerrasd859e292009-01-17 18:10:22 +11001926 mutex_lock(&ctx->mutex);
Ingo Molnar9b51f662008-12-12 13:49:45 +01001927 perf_install_in_context(ctx, counter, cpu);
Paul Mackerrasd859e292009-01-17 18:10:22 +11001928 mutex_unlock(&ctx->mutex);
Ingo Molnar9b51f662008-12-12 13:49:45 +01001929
1930 fput_light(counter_file, fput_needed2);
Thomas Gleixner0793a612008-12-04 20:12:29 +01001931
Ingo Molnar04289bb2008-12-11 08:38:42 +01001932out_fput:
1933 fput_light(group_file, fput_needed);
1934
Thomas Gleixner0793a612008-12-04 20:12:29 +01001935 return ret;
1936
Ingo Molnar9b51f662008-12-12 13:49:45 +01001937err_free_put_context:
Thomas Gleixner0793a612008-12-04 20:12:29 +01001938 kfree(counter);
1939
1940err_put_context:
1941 put_context(ctx);
1942
Ingo Molnar04289bb2008-12-11 08:38:42 +01001943 goto out_fput;
Thomas Gleixner0793a612008-12-04 20:12:29 +01001944}
1945
Ingo Molnar9b51f662008-12-12 13:49:45 +01001946/*
1947 * Initialize the perf_counter context in a task_struct:
1948 */
1949static void
1950__perf_counter_init_context(struct perf_counter_context *ctx,
1951 struct task_struct *task)
1952{
1953 memset(ctx, 0, sizeof(*ctx));
1954 spin_lock_init(&ctx->lock);
Paul Mackerrasd859e292009-01-17 18:10:22 +11001955 mutex_init(&ctx->mutex);
Ingo Molnar9b51f662008-12-12 13:49:45 +01001956 INIT_LIST_HEAD(&ctx->counter_list);
Peter Zijlstra592903c2009-03-13 12:21:36 +01001957 INIT_LIST_HEAD(&ctx->event_list);
Ingo Molnar9b51f662008-12-12 13:49:45 +01001958 ctx->task = task;
1959}
1960
1961/*
1962 * inherit a counter from parent task to child task:
1963 */
Paul Mackerrasd859e292009-01-17 18:10:22 +11001964static struct perf_counter *
Ingo Molnar9b51f662008-12-12 13:49:45 +01001965inherit_counter(struct perf_counter *parent_counter,
1966 struct task_struct *parent,
1967 struct perf_counter_context *parent_ctx,
1968 struct task_struct *child,
Paul Mackerrasd859e292009-01-17 18:10:22 +11001969 struct perf_counter *group_leader,
Ingo Molnar9b51f662008-12-12 13:49:45 +01001970 struct perf_counter_context *child_ctx)
1971{
1972 struct perf_counter *child_counter;
1973
Paul Mackerrasd859e292009-01-17 18:10:22 +11001974 /*
1975 * Instead of creating recursive hierarchies of counters,
1976 * we link inherited counters back to the original parent,
1977 * which has a filp for sure, which we use as the reference
1978 * count:
1979 */
1980 if (parent_counter->parent)
1981 parent_counter = parent_counter->parent;
1982
Ingo Molnar9b51f662008-12-12 13:49:45 +01001983 child_counter = perf_counter_alloc(&parent_counter->hw_event,
Paul Mackerras23a185c2009-02-09 22:42:47 +11001984 parent_counter->cpu, child_ctx,
1985 group_leader, GFP_KERNEL);
Ingo Molnar9b51f662008-12-12 13:49:45 +01001986 if (!child_counter)
Paul Mackerrasd859e292009-01-17 18:10:22 +11001987 return NULL;
Ingo Molnar9b51f662008-12-12 13:49:45 +01001988
1989 /*
1990 * Link it up in the child's context:
1991 */
Ingo Molnar9b51f662008-12-12 13:49:45 +01001992 child_counter->task = child;
1993 list_add_counter(child_counter, child_ctx);
1994 child_ctx->nr_counters++;
1995
1996 child_counter->parent = parent_counter;
Ingo Molnar9b51f662008-12-12 13:49:45 +01001997 /*
1998 * inherit into child's child as well:
1999 */
2000 child_counter->hw_event.inherit = 1;
2001
2002 /*
2003 * Get a reference to the parent filp - we will fput it
2004 * when the child counter exits. This is safe to do because
2005 * we are in the parent and we know that the filp still
2006 * exists and has a nonzero count:
2007 */
2008 atomic_long_inc(&parent_counter->filp->f_count);
2009
Paul Mackerrasd859e292009-01-17 18:10:22 +11002010 /*
2011 * Link this into the parent counter's child list
2012 */
2013 mutex_lock(&parent_counter->mutex);
2014 list_add_tail(&child_counter->child_list, &parent_counter->child_list);
2015
2016 /*
2017 * Make the child state follow the state of the parent counter,
2018 * not its hw_event.disabled bit. We hold the parent's mutex,
2019 * so we won't race with perf_counter_{en,dis}able_family.
2020 */
2021 if (parent_counter->state >= PERF_COUNTER_STATE_INACTIVE)
2022 child_counter->state = PERF_COUNTER_STATE_INACTIVE;
2023 else
2024 child_counter->state = PERF_COUNTER_STATE_OFF;
2025
2026 mutex_unlock(&parent_counter->mutex);
2027
2028 return child_counter;
2029}
2030
2031static int inherit_group(struct perf_counter *parent_counter,
2032 struct task_struct *parent,
2033 struct perf_counter_context *parent_ctx,
2034 struct task_struct *child,
2035 struct perf_counter_context *child_ctx)
2036{
2037 struct perf_counter *leader;
2038 struct perf_counter *sub;
2039
2040 leader = inherit_counter(parent_counter, parent, parent_ctx,
2041 child, NULL, child_ctx);
2042 if (!leader)
2043 return -ENOMEM;
2044 list_for_each_entry(sub, &parent_counter->sibling_list, list_entry) {
2045 if (!inherit_counter(sub, parent, parent_ctx,
2046 child, leader, child_ctx))
2047 return -ENOMEM;
2048 }
Ingo Molnar9b51f662008-12-12 13:49:45 +01002049 return 0;
2050}
2051
Paul Mackerrasd859e292009-01-17 18:10:22 +11002052static void sync_child_counter(struct perf_counter *child_counter,
2053 struct perf_counter *parent_counter)
2054{
2055 u64 parent_val, child_val;
2056
2057 parent_val = atomic64_read(&parent_counter->count);
2058 child_val = atomic64_read(&child_counter->count);
2059
2060 /*
2061 * Add back the child's count to the parent's count:
2062 */
2063 atomic64_add(child_val, &parent_counter->count);
2064
2065 /*
2066 * Remove this counter from the parent's list
2067 */
2068 mutex_lock(&parent_counter->mutex);
2069 list_del_init(&child_counter->child_list);
2070 mutex_unlock(&parent_counter->mutex);
2071
2072 /*
2073 * Release the parent counter, if this was the last
2074 * reference to it.
2075 */
2076 fput(parent_counter->filp);
2077}
2078
Ingo Molnar9b51f662008-12-12 13:49:45 +01002079static void
2080__perf_counter_exit_task(struct task_struct *child,
2081 struct perf_counter *child_counter,
2082 struct perf_counter_context *child_ctx)
2083{
2084 struct perf_counter *parent_counter;
Paul Mackerrasd859e292009-01-17 18:10:22 +11002085 struct perf_counter *sub, *tmp;
Ingo Molnar9b51f662008-12-12 13:49:45 +01002086
2087 /*
Ingo Molnar235c7fc2008-12-21 14:43:25 +01002088 * If we do not self-reap then we have to wait for the
2089 * child task to unschedule (it will happen for sure),
2090 * so that its counter is at its final count. (This
2091 * condition triggers rarely - child tasks usually get
2092 * off their CPU before the parent has a chance to
2093 * get this far into the reaping action)
Ingo Molnar9b51f662008-12-12 13:49:45 +01002094 */
Ingo Molnar235c7fc2008-12-21 14:43:25 +01002095 if (child != current) {
2096 wait_task_inactive(child, 0);
2097 list_del_init(&child_counter->list_entry);
2098 } else {
Ingo Molnar0cc0c022008-12-14 23:20:36 +01002099 struct perf_cpu_context *cpuctx;
Ingo Molnar235c7fc2008-12-21 14:43:25 +01002100 unsigned long flags;
2101 u64 perf_flags;
2102
2103 /*
2104 * Disable and unlink this counter.
2105 *
2106 * Be careful about zapping the list - IRQ/NMI context
2107 * could still be processing it:
2108 */
2109 curr_rq_lock_irq_save(&flags);
2110 perf_flags = hw_perf_save_disable();
Ingo Molnar0cc0c022008-12-14 23:20:36 +01002111
2112 cpuctx = &__get_cpu_var(perf_cpu_context);
2113
Paul Mackerrasd859e292009-01-17 18:10:22 +11002114 group_sched_out(child_counter, cpuctx, child_ctx);
Ingo Molnar0cc0c022008-12-14 23:20:36 +01002115
Ingo Molnar235c7fc2008-12-21 14:43:25 +01002116 list_del_init(&child_counter->list_entry);
2117
2118 child_ctx->nr_counters--;
2119
2120 hw_perf_restore(perf_flags);
2121 curr_rq_unlock_irq_restore(&flags);
Ingo Molnar0cc0c022008-12-14 23:20:36 +01002122 }
2123
Ingo Molnar9b51f662008-12-12 13:49:45 +01002124 parent_counter = child_counter->parent;
2125 /*
2126 * It can happen that parent exits first, and has counters
2127 * that are still around due to the child reference. These
2128 * counters need to be zapped - but otherwise linger.
2129 */
Paul Mackerrasd859e292009-01-17 18:10:22 +11002130 if (parent_counter) {
2131 sync_child_counter(child_counter, parent_counter);
2132 list_for_each_entry_safe(sub, tmp, &child_counter->sibling_list,
2133 list_entry) {
Paul Mackerras4bcf3492009-02-11 13:53:19 +01002134 if (sub->parent) {
Paul Mackerrasd859e292009-01-17 18:10:22 +11002135 sync_child_counter(sub, sub->parent);
Peter Zijlstraf1600952009-03-19 20:26:16 +01002136 free_counter(sub);
Paul Mackerras4bcf3492009-02-11 13:53:19 +01002137 }
Paul Mackerrasd859e292009-01-17 18:10:22 +11002138 }
Peter Zijlstraf1600952009-03-19 20:26:16 +01002139 free_counter(child_counter);
Paul Mackerras4bcf3492009-02-11 13:53:19 +01002140 }
Ingo Molnar9b51f662008-12-12 13:49:45 +01002141}
2142
2143/*
Paul Mackerrasd859e292009-01-17 18:10:22 +11002144 * When a child task exits, feed back counter values to parent counters.
Ingo Molnar9b51f662008-12-12 13:49:45 +01002145 *
Paul Mackerrasd859e292009-01-17 18:10:22 +11002146 * Note: we may be running in child context, but the PID is not hashed
Ingo Molnar9b51f662008-12-12 13:49:45 +01002147 * anymore so new counters will not be added.
2148 */
2149void perf_counter_exit_task(struct task_struct *child)
2150{
2151 struct perf_counter *child_counter, *tmp;
2152 struct perf_counter_context *child_ctx;
2153
2154 child_ctx = &child->perf_counter_ctx;
2155
2156 if (likely(!child_ctx->nr_counters))
2157 return;
2158
2159 list_for_each_entry_safe(child_counter, tmp, &child_ctx->counter_list,
2160 list_entry)
2161 __perf_counter_exit_task(child, child_counter, child_ctx);
2162}
2163
2164/*
2165 * Initialize the perf_counter context in task_struct
2166 */
2167void perf_counter_init_task(struct task_struct *child)
2168{
2169 struct perf_counter_context *child_ctx, *parent_ctx;
Paul Mackerrasd859e292009-01-17 18:10:22 +11002170 struct perf_counter *counter;
Ingo Molnar9b51f662008-12-12 13:49:45 +01002171 struct task_struct *parent = current;
Ingo Molnar9b51f662008-12-12 13:49:45 +01002172
2173 child_ctx = &child->perf_counter_ctx;
2174 parent_ctx = &parent->perf_counter_ctx;
2175
2176 __perf_counter_init_context(child_ctx, child);
2177
2178 /*
2179 * This is executed from the parent task context, so inherit
2180 * counters that have been marked for cloning:
2181 */
2182
2183 if (likely(!parent_ctx->nr_counters))
2184 return;
2185
2186 /*
2187 * Lock the parent list. No need to lock the child - not PID
2188 * hashed yet and not running, so nobody can access it.
2189 */
Paul Mackerrasd859e292009-01-17 18:10:22 +11002190 mutex_lock(&parent_ctx->mutex);
Ingo Molnar9b51f662008-12-12 13:49:45 +01002191
2192 /*
2193 * We dont have to disable NMIs - we are only looking at
2194 * the list, not manipulating it:
2195 */
2196 list_for_each_entry(counter, &parent_ctx->counter_list, list_entry) {
Paul Mackerrasd859e292009-01-17 18:10:22 +11002197 if (!counter->hw_event.inherit)
Ingo Molnar9b51f662008-12-12 13:49:45 +01002198 continue;
2199
Paul Mackerrasd859e292009-01-17 18:10:22 +11002200 if (inherit_group(counter, parent,
Ingo Molnar9b51f662008-12-12 13:49:45 +01002201 parent_ctx, child, child_ctx))
2202 break;
2203 }
2204
Paul Mackerrasd859e292009-01-17 18:10:22 +11002205 mutex_unlock(&parent_ctx->mutex);
Ingo Molnar9b51f662008-12-12 13:49:45 +01002206}
2207
Ingo Molnar04289bb2008-12-11 08:38:42 +01002208static void __cpuinit perf_counter_init_cpu(int cpu)
Thomas Gleixner0793a612008-12-04 20:12:29 +01002209{
Ingo Molnar04289bb2008-12-11 08:38:42 +01002210 struct perf_cpu_context *cpuctx;
Thomas Gleixner0793a612008-12-04 20:12:29 +01002211
Ingo Molnar04289bb2008-12-11 08:38:42 +01002212 cpuctx = &per_cpu(perf_cpu_context, cpu);
2213 __perf_counter_init_context(&cpuctx->ctx, NULL);
Thomas Gleixner0793a612008-12-04 20:12:29 +01002214
2215 mutex_lock(&perf_resource_mutex);
Ingo Molnar04289bb2008-12-11 08:38:42 +01002216 cpuctx->max_pertask = perf_max_counters - perf_reserved_percpu;
Thomas Gleixner0793a612008-12-04 20:12:29 +01002217 mutex_unlock(&perf_resource_mutex);
Ingo Molnar04289bb2008-12-11 08:38:42 +01002218
Paul Mackerras01d02872009-01-14 13:44:19 +11002219 hw_perf_counter_setup(cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +01002220}
2221
2222#ifdef CONFIG_HOTPLUG_CPU
Ingo Molnar04289bb2008-12-11 08:38:42 +01002223static void __perf_counter_exit_cpu(void *info)
Thomas Gleixner0793a612008-12-04 20:12:29 +01002224{
2225 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
2226 struct perf_counter_context *ctx = &cpuctx->ctx;
2227 struct perf_counter *counter, *tmp;
2228
Ingo Molnar04289bb2008-12-11 08:38:42 +01002229 list_for_each_entry_safe(counter, tmp, &ctx->counter_list, list_entry)
2230 __perf_counter_remove_from_context(counter);
Thomas Gleixner0793a612008-12-04 20:12:29 +01002231}
Ingo Molnar04289bb2008-12-11 08:38:42 +01002232static void perf_counter_exit_cpu(int cpu)
Thomas Gleixner0793a612008-12-04 20:12:29 +01002233{
Paul Mackerrasd859e292009-01-17 18:10:22 +11002234 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
2235 struct perf_counter_context *ctx = &cpuctx->ctx;
2236
2237 mutex_lock(&ctx->mutex);
Ingo Molnar04289bb2008-12-11 08:38:42 +01002238 smp_call_function_single(cpu, __perf_counter_exit_cpu, NULL, 1);
Paul Mackerrasd859e292009-01-17 18:10:22 +11002239 mutex_unlock(&ctx->mutex);
Thomas Gleixner0793a612008-12-04 20:12:29 +01002240}
2241#else
Ingo Molnar04289bb2008-12-11 08:38:42 +01002242static inline void perf_counter_exit_cpu(int cpu) { }
Thomas Gleixner0793a612008-12-04 20:12:29 +01002243#endif
2244
2245static int __cpuinit
2246perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
2247{
2248 unsigned int cpu = (long)hcpu;
2249
2250 switch (action) {
2251
2252 case CPU_UP_PREPARE:
2253 case CPU_UP_PREPARE_FROZEN:
Ingo Molnar04289bb2008-12-11 08:38:42 +01002254 perf_counter_init_cpu(cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +01002255 break;
2256
2257 case CPU_DOWN_PREPARE:
2258 case CPU_DOWN_PREPARE_FROZEN:
Ingo Molnar04289bb2008-12-11 08:38:42 +01002259 perf_counter_exit_cpu(cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +01002260 break;
2261
2262 default:
2263 break;
2264 }
2265
2266 return NOTIFY_OK;
2267}
2268
2269static struct notifier_block __cpuinitdata perf_cpu_nb = {
2270 .notifier_call = perf_cpu_notify,
2271};
2272
2273static int __init perf_counter_init(void)
2274{
2275 perf_cpu_notify(&perf_cpu_nb, (unsigned long)CPU_UP_PREPARE,
2276 (void *)(long)smp_processor_id());
2277 register_cpu_notifier(&perf_cpu_nb);
2278
2279 return 0;
2280}
2281early_initcall(perf_counter_init);
2282
2283static ssize_t perf_show_reserve_percpu(struct sysdev_class *class, char *buf)
2284{
2285 return sprintf(buf, "%d\n", perf_reserved_percpu);
2286}
2287
2288static ssize_t
2289perf_set_reserve_percpu(struct sysdev_class *class,
2290 const char *buf,
2291 size_t count)
2292{
2293 struct perf_cpu_context *cpuctx;
2294 unsigned long val;
2295 int err, cpu, mpt;
2296
2297 err = strict_strtoul(buf, 10, &val);
2298 if (err)
2299 return err;
2300 if (val > perf_max_counters)
2301 return -EINVAL;
2302
2303 mutex_lock(&perf_resource_mutex);
2304 perf_reserved_percpu = val;
2305 for_each_online_cpu(cpu) {
2306 cpuctx = &per_cpu(perf_cpu_context, cpu);
2307 spin_lock_irq(&cpuctx->ctx.lock);
2308 mpt = min(perf_max_counters - cpuctx->ctx.nr_counters,
2309 perf_max_counters - perf_reserved_percpu);
2310 cpuctx->max_pertask = mpt;
2311 spin_unlock_irq(&cpuctx->ctx.lock);
2312 }
2313 mutex_unlock(&perf_resource_mutex);
2314
2315 return count;
2316}
2317
2318static ssize_t perf_show_overcommit(struct sysdev_class *class, char *buf)
2319{
2320 return sprintf(buf, "%d\n", perf_overcommit);
2321}
2322
2323static ssize_t
2324perf_set_overcommit(struct sysdev_class *class, const char *buf, size_t count)
2325{
2326 unsigned long val;
2327 int err;
2328
2329 err = strict_strtoul(buf, 10, &val);
2330 if (err)
2331 return err;
2332 if (val > 1)
2333 return -EINVAL;
2334
2335 mutex_lock(&perf_resource_mutex);
2336 perf_overcommit = val;
2337 mutex_unlock(&perf_resource_mutex);
2338
2339 return count;
2340}
2341
2342static SYSDEV_CLASS_ATTR(
2343 reserve_percpu,
2344 0644,
2345 perf_show_reserve_percpu,
2346 perf_set_reserve_percpu
2347 );
2348
2349static SYSDEV_CLASS_ATTR(
2350 overcommit,
2351 0644,
2352 perf_show_overcommit,
2353 perf_set_overcommit
2354 );
2355
2356static struct attribute *perfclass_attrs[] = {
2357 &attr_reserve_percpu.attr,
2358 &attr_overcommit.attr,
2359 NULL
2360};
2361
2362static struct attribute_group perfclass_attr_group = {
2363 .attrs = perfclass_attrs,
2364 .name = "perf_counters",
2365};
2366
2367static int __init perf_counter_sysfs_init(void)
2368{
2369 return sysfs_create_group(&cpu_sysdev_class.kset.kobj,
2370 &perfclass_attr_group);
2371}
2372device_initcall(perf_counter_sysfs_init);