perf_counter: hook up the tracepoint events
[linux-2.6-block.git] / kernel / perf_counter.c
CommitLineData
0793a61d
TG
1/*
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>
04289bb9 13#include <linux/file.h>
0793a61d
TG
14#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>
aa9c4c0f 21#include <linux/kernel_stat.h>
0793a61d 22#include <linux/perf_counter.h>
23a185ca
PM
23#include <linux/mm.h>
24#include <linux/vmstat.h>
592903cd 25#include <linux/rculist.h>
0793a61d 26
4e193bd4
TB
27#include <asm/irq_regs.h>
28
0793a61d
TG
29/*
30 * Each CPU has a list of per CPU counters:
31 */
32DEFINE_PER_CPU(struct perf_cpu_context, perf_cpu_context);
33
088e2852 34int perf_max_counters __read_mostly = 1;
0793a61d
TG
35static 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 */
5c92d124 46extern __weak const struct hw_perf_counter_ops *
621a01ea 47hw_perf_counter_init(struct perf_counter *counter)
0793a61d 48{
ff6f0541 49 return NULL;
0793a61d
TG
50}
51
01b2838c 52u64 __weak hw_perf_save_disable(void) { return 0; }
01ea1cca 53void __weak hw_perf_restore(u64 ctrl) { barrier(); }
01d0287f 54void __weak hw_perf_counter_setup(int cpu) { barrier(); }
3cbed429
PM
55int __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}
0793a61d 61
4eb96fcf
PM
62void __weak perf_counter_print_debug(void) { }
63
04289bb9
IM
64static 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);
592903cd
PZ
78
79 list_add_rcu(&counter->event_entry, &ctx->event_list);
04289bb9
IM
80}
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);
592903cd 88 list_del_rcu(&counter->event_entry);
04289bb9 89
04289bb9
IM
90 /*
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
75564232 98 list_move_tail(&sibling->list_entry, &ctx->counter_list);
04289bb9
IM
99 sibling->group_leader = sibling;
100 }
101}
102
3b6f9e5c
PM
103static 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
d859e29f
PM
122static 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
0793a61d
TG
144/*
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 */
04289bb9 150static void __perf_counter_remove_from_context(void *info)
0793a61d
TG
151{
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;
9b51f66d 155 unsigned long flags;
5c92d124 156 u64 perf_flags;
0793a61d
TG
157
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
aa9c4c0f
IM
166 curr_rq_lock_irq_save(&flags);
167 spin_lock(&ctx->lock);
0793a61d 168
3b6f9e5c
PM
169 counter_sched_out(counter, cpuctx, ctx);
170
171 counter->task = NULL;
0793a61d
TG
172 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 */
01b2838c 178 perf_flags = hw_perf_save_disable();
04289bb9 179 list_del_counter(counter, ctx);
01b2838c 180 hw_perf_restore(perf_flags);
0793a61d
TG
181
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
aa9c4c0f
IM
192 spin_unlock(&ctx->lock);
193 curr_rq_unlock_irq_restore(&flags);
0793a61d
TG
194}
195
196
197/*
198 * Remove the counter from a task's (or a CPU's) list of counters.
199 *
d859e29f 200 * Must be called with counter->mutex and ctx->mutex held.
0793a61d
TG
201 *
202 * CPU counters are removed with a smp call. For task counters we only
203 * call when the task is on a CPU.
204 */
04289bb9 205static void perf_counter_remove_from_context(struct perf_counter *counter)
0793a61d
TG
206{
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,
04289bb9 216 __perf_counter_remove_from_context,
0793a61d
TG
217 counter, 1);
218 return;
219 }
220
221retry:
04289bb9 222 task_oncpu_function_call(task, __perf_counter_remove_from_context,
0793a61d
TG
223 counter);
224
225 spin_lock_irq(&ctx->lock);
226 /*
227 * If the context is active we need to retry the smp call.
228 */
04289bb9 229 if (ctx->nr_active && !list_empty(&counter->list_entry)) {
0793a61d
TG
230 spin_unlock_irq(&ctx->lock);
231 goto retry;
232 }
233
234 /*
235 * The lock prevents that this context is scheduled in so we
04289bb9 236 * can remove the counter safely, if the call above did not
0793a61d
TG
237 * succeed.
238 */
04289bb9 239 if (!list_empty(&counter->list_entry)) {
0793a61d 240 ctx->nr_counters--;
04289bb9 241 list_del_counter(counter, ctx);
0793a61d
TG
242 counter->task = NULL;
243 }
244 spin_unlock_irq(&ctx->lock);
245}
246
d859e29f
PM
247/*
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
235c7fc7
IM
340static int
341counter_sched_in(struct perf_counter *counter,
342 struct perf_cpu_context *cpuctx,
343 struct perf_counter_context *ctx,
344 int cpu)
345{
3b6f9e5c 346 if (counter->state <= PERF_COUNTER_STATE_OFF)
235c7fc7
IM
347 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
3b6f9e5c
PM
362 if (!is_software_counter(counter))
363 cpuctx->active_oncpu++;
235c7fc7
IM
364 ctx->nr_active++;
365
3b6f9e5c
PM
366 if (counter->hw_event.exclusive)
367 cpuctx->exclusive = 1;
368
235c7fc7
IM
369 return 0;
370}
371
3b6f9e5c
PM
372/*
373 * 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
0793a61d 419/*
235c7fc7 420 * Cross CPU call to install and enable a performance counter
0793a61d
TG
421 */
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;
d859e29f 427 struct perf_counter *leader = counter->group_leader;
0793a61d 428 int cpu = smp_processor_id();
9b51f66d 429 unsigned long flags;
5c92d124 430 u64 perf_flags;
3b6f9e5c 431 int err;
0793a61d
TG
432
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
aa9c4c0f
IM
441 curr_rq_lock_irq_save(&flags);
442 spin_lock(&ctx->lock);
0793a61d
TG
443
444 /*
445 * Protect the list operation against NMI by disabling the
446 * counters on a global level. NOP for non NMI based counters.
447 */
01b2838c 448 perf_flags = hw_perf_save_disable();
0793a61d 449
235c7fc7 450 list_add_counter(counter, ctx);
0793a61d 451 ctx->nr_counters++;
c07c99b6 452 counter->prev_state = PERF_COUNTER_STATE_OFF;
0793a61d 453
d859e29f
PM
454 /*
455 * 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
3b6f9e5c
PM
462 /*
463 * 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 */
d859e29f 467 if (!group_can_go_on(counter, cpuctx, 1))
3b6f9e5c
PM
468 err = -EEXIST;
469 else
470 err = counter_sched_in(counter, cpuctx, ctx, cpu);
471
d859e29f
PM
472 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 }
0793a61d 483
3b6f9e5c 484 if (!err && !ctx->task && cpuctx->max_pertask)
0793a61d
TG
485 cpuctx->max_pertask--;
486
d859e29f 487 unlock:
235c7fc7
IM
488 hw_perf_restore(perf_flags);
489
aa9c4c0f
IM
490 spin_unlock(&ctx->lock);
491 curr_rq_unlock_irq_restore(&flags);
0793a61d
TG
492}
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.
d859e29f
PM
503 *
504 * Must be called with ctx->mutex held.
0793a61d
TG
505 */
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
0793a61d
TG
513 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 /*
0793a61d
TG
530 * we need to retry the smp call.
531 */
d859e29f 532 if (ctx->is_active && list_empty(&counter->list_entry)) {
0793a61d
TG
533 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 */
04289bb9
IM
542 if (list_empty(&counter->list_entry)) {
543 list_add_counter(counter, ctx);
0793a61d
TG
544 ctx->nr_counters++;
545 }
546 spin_unlock_irq(&ctx->lock);
547}
548
d859e29f
PM
549/*
550 * Cross CPU call to enable a performance counter
551 */
552static void __perf_counter_enable(void *info)
04289bb9 553{
d859e29f
PM
554 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;
04289bb9 560
d859e29f
PM
561 /*
562 * If this is a per-task counter, need to check whether this
563 * counter's task is the current task on this cpu.
564 */
565 if (ctx->task && cpuctx->task_ctx != ctx)
3cbed429
PM
566 return;
567
d859e29f
PM
568 curr_rq_lock_irq_save(&flags);
569 spin_lock(&ctx->lock);
570
c07c99b6 571 counter->prev_state = counter->state;
d859e29f
PM
572 if (counter->state >= PERF_COUNTER_STATE_INACTIVE)
573 goto unlock;
574 counter->state = PERF_COUNTER_STATE_INACTIVE;
04289bb9
IM
575
576 /*
d859e29f
PM
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.
04289bb9 579 */
d859e29f
PM
580 if (leader != counter && leader->state != PERF_COUNTER_STATE_ACTIVE)
581 goto unlock;
3b6f9e5c 582
d859e29f
PM
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);
04289bb9
IM
675}
676
235c7fc7
IM
677void __perf_counter_sched_out(struct perf_counter_context *ctx,
678 struct perf_cpu_context *cpuctx)
679{
680 struct perf_counter *counter;
3cbed429 681 u64 flags;
235c7fc7 682
d859e29f
PM
683 spin_lock(&ctx->lock);
684 ctx->is_active = 0;
235c7fc7 685 if (likely(!ctx->nr_counters))
d859e29f 686 goto out;
235c7fc7 687
3cbed429 688 flags = hw_perf_save_disable();
235c7fc7
IM
689 if (ctx->nr_active) {
690 list_for_each_entry(counter, &ctx->counter_list, list_entry)
691 group_sched_out(counter, cpuctx, ctx);
692 }
3cbed429 693 hw_perf_restore(flags);
d859e29f 694 out:
235c7fc7
IM
695 spin_unlock(&ctx->lock);
696}
697
0793a61d
TG
698/*
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 *
7671581f 704 * This does not protect us against NMI, but disable()
0793a61d
TG
705 * 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;
4a0deca6 713 struct pt_regs *regs;
0793a61d
TG
714
715 if (likely(!cpuctx->task_ctx))
716 return;
717
4a0deca6
PZ
718 regs = task_pt_regs(task);
719 perf_swcounter_event(PERF_COUNT_CONTEXT_SWITCHES, 1, 1, regs);
235c7fc7
IM
720 __perf_counter_sched_out(ctx, cpuctx);
721
0793a61d
TG
722 cpuctx->task_ctx = NULL;
723}
724
235c7fc7 725static void perf_counter_cpu_sched_out(struct perf_cpu_context *cpuctx)
04289bb9 726{
235c7fc7 727 __perf_counter_sched_out(&cpuctx->ctx, cpuctx);
04289bb9
IM
728}
729
7995888f 730static int
04289bb9
IM
731group_sched_in(struct perf_counter *group_counter,
732 struct perf_cpu_context *cpuctx,
733 struct perf_counter_context *ctx,
734 int cpu)
735{
95cdd2e7 736 struct perf_counter *counter, *partial_group;
3cbed429
PM
737 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;
04289bb9 745
c07c99b6 746 group_counter->prev_state = group_counter->state;
95cdd2e7
IM
747 if (counter_sched_in(group_counter, cpuctx, ctx, cpu))
748 return -EAGAIN;
04289bb9
IM
749
750 /*
751 * Schedule in siblings as one group (if any):
752 */
7995888f 753 list_for_each_entry(counter, &group_counter->sibling_list, list_entry) {
c07c99b6 754 counter->prev_state = counter->state;
95cdd2e7
IM
755 if (counter_sched_in(counter, cpuctx, ctx, cpu)) {
756 partial_group = counter;
757 goto group_error;
758 }
95cdd2e7
IM
759 }
760
3cbed429 761 return 0;
95cdd2e7
IM
762
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);
7995888f 772 }
95cdd2e7 773 counter_sched_out(group_counter, cpuctx, ctx);
7995888f 774
95cdd2e7 775 return -EAGAIN;
04289bb9
IM
776}
777
235c7fc7
IM
778static void
779__perf_counter_sched_in(struct perf_counter_context *ctx,
780 struct perf_cpu_context *cpuctx, int cpu)
0793a61d 781{
0793a61d 782 struct perf_counter *counter;
3cbed429 783 u64 flags;
dd0e6ba2 784 int can_add_hw = 1;
0793a61d 785
d859e29f
PM
786 spin_lock(&ctx->lock);
787 ctx->is_active = 1;
0793a61d 788 if (likely(!ctx->nr_counters))
d859e29f 789 goto out;
0793a61d 790
3cbed429 791 flags = hw_perf_save_disable();
3b6f9e5c
PM
792
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 */
797 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
798 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
04289bb9 815 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
3b6f9e5c
PM
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
04289bb9
IM
824 /*
825 * Listen to the 'cpu' scheduling filter constraint
826 * of counters:
827 */
0793a61d
TG
828 if (counter->cpu != -1 && counter->cpu != cpu)
829 continue;
830
3b6f9e5c 831 if (group_can_go_on(counter, cpuctx, can_add_hw)) {
dd0e6ba2
PM
832 if (group_sched_in(counter, cpuctx, ctx, cpu))
833 can_add_hw = 0;
3b6f9e5c 834 }
0793a61d 835 }
3cbed429 836 hw_perf_restore(flags);
d859e29f 837 out:
0793a61d 838 spin_unlock(&ctx->lock);
235c7fc7
IM
839}
840
841/*
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;
04289bb9 856
235c7fc7 857 __perf_counter_sched_in(ctx, cpuctx, cpu);
0793a61d
TG
858 cpuctx->task_ctx = ctx;
859}
860
235c7fc7
IM
861static 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
1d1c7ddb
IM
868int 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;
aa9c4c0f 873 unsigned long flags;
1d1c7ddb
IM
874 u64 perf_flags;
875 int cpu;
876
877 if (likely(!ctx->nr_counters))
878 return 0;
879
aa9c4c0f 880 curr_rq_lock_irq_save(&flags);
1d1c7ddb
IM
881 cpu = smp_processor_id();
882
aa9c4c0f
IM
883 /* force the update of the task clock: */
884 __task_delta_exec(curr, 1);
885
1d1c7ddb
IM
886 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
3b6f9e5c
PM
895 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 }
9b51f66d 899
1d1c7ddb
IM
900 hw_perf_restore(perf_flags);
901
902 spin_unlock(&ctx->lock);
903
aa9c4c0f 904 curr_rq_unlock_irq_restore(&flags);
1d1c7ddb
IM
905
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;
aa9c4c0f 914 unsigned long flags;
1d1c7ddb
IM
915 u64 perf_flags;
916 int cpu;
917
918 if (likely(!ctx->nr_counters))
919 return 0;
920
aa9c4c0f 921 curr_rq_lock_irq_save(&flags);
1d1c7ddb
IM
922 cpu = smp_processor_id();
923
aa9c4c0f
IM
924 /* force the update of the task clock: */
925 __task_delta_exec(curr, 1);
926
235c7fc7
IM
927 perf_counter_task_sched_out(curr, cpu);
928
1d1c7ddb
IM
929 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) {
3b6f9e5c 937 if (counter->state > PERF_COUNTER_STATE_OFF)
1d1c7ddb 938 continue;
6a930700 939 counter->state = PERF_COUNTER_STATE_INACTIVE;
aa9c4c0f 940 counter->hw_event.disabled = 0;
1d1c7ddb
IM
941 }
942 hw_perf_restore(perf_flags);
943
944 spin_unlock(&ctx->lock);
945
946 perf_counter_task_sched_in(curr, cpu);
947
aa9c4c0f 948 curr_rq_unlock_irq_restore(&flags);
1d1c7ddb
IM
949
950 return 0;
951}
952
235c7fc7
IM
953/*
954 * Round-robin a context's counters:
955 */
956static void rotate_ctx(struct perf_counter_context *ctx)
0793a61d 957{
0793a61d 958 struct perf_counter *counter;
5c92d124 959 u64 perf_flags;
0793a61d 960
235c7fc7 961 if (!ctx->nr_counters)
0793a61d
TG
962 return;
963
0793a61d 964 spin_lock(&ctx->lock);
0793a61d 965 /*
04289bb9 966 * Rotate the first entry last (works just fine for group counters too):
0793a61d 967 */
01b2838c 968 perf_flags = hw_perf_save_disable();
04289bb9 969 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
75564232 970 list_move_tail(&counter->list_entry, &ctx->counter_list);
0793a61d
TG
971 break;
972 }
01b2838c 973 hw_perf_restore(perf_flags);
0793a61d
TG
974
975 spin_unlock(&ctx->lock);
235c7fc7
IM
976}
977
978void 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);
0793a61d 987
235c7fc7
IM
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);
0793a61d
TG
994 perf_counter_task_sched_in(curr, cpu);
995}
996
0793a61d
TG
997/*
998 * Cross CPU call to read the hardware counter
999 */
7671581f 1000static void __read(void *info)
0793a61d 1001{
621a01ea 1002 struct perf_counter *counter = info;
aa9c4c0f 1003 unsigned long flags;
621a01ea 1004
aa9c4c0f 1005 curr_rq_lock_irq_save(&flags);
7671581f 1006 counter->hw_ops->read(counter);
aa9c4c0f 1007 curr_rq_unlock_irq_restore(&flags);
0793a61d
TG
1008}
1009
04289bb9 1010static u64 perf_counter_read(struct perf_counter *counter)
0793a61d
TG
1011{
1012 /*
1013 * If counter is enabled and currently active on a CPU, update the
1014 * value in the counter structure:
1015 */
6a930700 1016 if (counter->state == PERF_COUNTER_STATE_ACTIVE) {
0793a61d 1017 smp_call_function_single(counter->oncpu,
7671581f 1018 __read, counter, 1);
0793a61d
TG
1019 }
1020
ee06094f 1021 return atomic64_read(&counter->count);
0793a61d
TG
1022}
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);
6a930700 1069 if (counter->state != PERF_COUNTER_STATE_ACTIVE) {
0793a61d
TG
1070 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
0793a61d
TG
1118 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
592903cd
PZ
1145static 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
f1600952
PZ
1153static void free_counter(struct perf_counter *counter)
1154{
e077df4f
PZ
1155 if (counter->destroy)
1156 counter->destroy(counter);
1157
f1600952
PZ
1158 call_rcu(&counter->rcu_head, free_counter_rcu);
1159}
1160
0793a61d
TG
1161/*
1162 * Called when the last reference to the file is gone.
1163 */
1164static int perf_release(struct inode *inode, struct file *file)
1165{
1166 struct perf_counter *counter = file->private_data;
1167 struct perf_counter_context *ctx = counter->ctx;
1168
1169 file->private_data = NULL;
1170
d859e29f 1171 mutex_lock(&ctx->mutex);
0793a61d
TG
1172 mutex_lock(&counter->mutex);
1173
04289bb9 1174 perf_counter_remove_from_context(counter);
0793a61d
TG
1175
1176 mutex_unlock(&counter->mutex);
d859e29f 1177 mutex_unlock(&ctx->mutex);
0793a61d 1178
f1600952 1179 free_counter(counter);
5af75917 1180 put_context(ctx);
0793a61d
TG
1181
1182 return 0;
1183}
1184
1185/*
1186 * Read the performance counter - simple non blocking version for now
1187 */
1188static ssize_t
1189perf_read_hw(struct perf_counter *counter, char __user *buf, size_t count)
1190{
1191 u64 cntval;
1192
1193 if (count != sizeof(cntval))
1194 return -EINVAL;
1195
3b6f9e5c
PM
1196 /*
1197 * Return end-of-file for a read on a counter that is in
1198 * error state (i.e. because it was pinned but it couldn't be
1199 * scheduled on to the CPU at some point).
1200 */
1201 if (counter->state == PERF_COUNTER_STATE_ERROR)
1202 return 0;
1203
0793a61d 1204 mutex_lock(&counter->mutex);
04289bb9 1205 cntval = perf_counter_read(counter);
0793a61d
TG
1206 mutex_unlock(&counter->mutex);
1207
1208 return put_user(cntval, (u64 __user *) buf) ? -EFAULT : sizeof(cntval);
1209}
1210
1211static ssize_t
1212perf_copy_usrdata(struct perf_data *usrdata, char __user *buf, size_t count)
1213{
1214 if (!usrdata->len)
1215 return 0;
1216
1217 count = min(count, (size_t)usrdata->len);
1218 if (copy_to_user(buf, usrdata->data + usrdata->rd_idx, count))
1219 return -EFAULT;
1220
1221 /* Adjust the counters */
1222 usrdata->len -= count;
1223 if (!usrdata->len)
1224 usrdata->rd_idx = 0;
1225 else
1226 usrdata->rd_idx += count;
1227
1228 return count;
1229}
1230
1231static ssize_t
1232perf_read_irq_data(struct perf_counter *counter,
1233 char __user *buf,
1234 size_t count,
1235 int nonblocking)
1236{
1237 struct perf_data *irqdata, *usrdata;
1238 DECLARE_WAITQUEUE(wait, current);
3b6f9e5c 1239 ssize_t res, res2;
0793a61d
TG
1240
1241 irqdata = counter->irqdata;
1242 usrdata = counter->usrdata;
1243
1244 if (usrdata->len + irqdata->len >= count)
1245 goto read_pending;
1246
1247 if (nonblocking)
1248 return -EAGAIN;
1249
1250 spin_lock_irq(&counter->waitq.lock);
1251 __add_wait_queue(&counter->waitq, &wait);
1252 for (;;) {
1253 set_current_state(TASK_INTERRUPTIBLE);
1254 if (usrdata->len + irqdata->len >= count)
1255 break;
1256
1257 if (signal_pending(current))
1258 break;
1259
3b6f9e5c
PM
1260 if (counter->state == PERF_COUNTER_STATE_ERROR)
1261 break;
1262
0793a61d
TG
1263 spin_unlock_irq(&counter->waitq.lock);
1264 schedule();
1265 spin_lock_irq(&counter->waitq.lock);
1266 }
1267 __remove_wait_queue(&counter->waitq, &wait);
1268 __set_current_state(TASK_RUNNING);
1269 spin_unlock_irq(&counter->waitq.lock);
1270
3b6f9e5c
PM
1271 if (usrdata->len + irqdata->len < count &&
1272 counter->state != PERF_COUNTER_STATE_ERROR)
0793a61d
TG
1273 return -ERESTARTSYS;
1274read_pending:
1275 mutex_lock(&counter->mutex);
1276
1277 /* Drain pending data first: */
1278 res = perf_copy_usrdata(usrdata, buf, count);
1279 if (res < 0 || res == count)
1280 goto out;
1281
1282 /* Switch irq buffer: */
1283 usrdata = perf_switch_irq_data(counter);
3b6f9e5c
PM
1284 res2 = perf_copy_usrdata(usrdata, buf + res, count - res);
1285 if (res2 < 0) {
0793a61d
TG
1286 if (!res)
1287 res = -EFAULT;
1288 } else {
3b6f9e5c 1289 res += res2;
0793a61d
TG
1290 }
1291out:
1292 mutex_unlock(&counter->mutex);
1293
1294 return res;
1295}
1296
1297static ssize_t
1298perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
1299{
1300 struct perf_counter *counter = file->private_data;
1301
9f66a381 1302 switch (counter->hw_event.record_type) {
0793a61d
TG
1303 case PERF_RECORD_SIMPLE:
1304 return perf_read_hw(counter, buf, count);
1305
1306 case PERF_RECORD_IRQ:
1307 case PERF_RECORD_GROUP:
1308 return perf_read_irq_data(counter, buf, count,
1309 file->f_flags & O_NONBLOCK);
1310 }
1311 return -EINVAL;
1312}
1313
1314static unsigned int perf_poll(struct file *file, poll_table *wait)
1315{
1316 struct perf_counter *counter = file->private_data;
1317 unsigned int events = 0;
1318 unsigned long flags;
1319
1320 poll_wait(file, &counter->waitq, wait);
1321
1322 spin_lock_irqsave(&counter->waitq.lock, flags);
1323 if (counter->usrdata->len || counter->irqdata->len)
1324 events |= POLLIN;
1325 spin_unlock_irqrestore(&counter->waitq.lock, flags);
1326
1327 return events;
1328}
1329
d859e29f
PM
1330static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1331{
1332 struct perf_counter *counter = file->private_data;
1333 int err = 0;
1334
1335 switch (cmd) {
1336 case PERF_COUNTER_IOC_ENABLE:
1337 perf_counter_enable_family(counter);
1338 break;
1339 case PERF_COUNTER_IOC_DISABLE:
1340 perf_counter_disable_family(counter);
1341 break;
1342 default:
1343 err = -ENOTTY;
1344 }
1345 return err;
1346}
1347
0793a61d
TG
1348static const struct file_operations perf_fops = {
1349 .release = perf_release,
1350 .read = perf_read,
1351 .poll = perf_poll,
d859e29f
PM
1352 .unlocked_ioctl = perf_ioctl,
1353 .compat_ioctl = perf_ioctl,
0793a61d
TG
1354};
1355
15dbf27c
PZ
1356/*
1357 * Generic software counter infrastructure
1358 */
1359
1360static void perf_swcounter_update(struct perf_counter *counter)
1361{
1362 struct hw_perf_counter *hwc = &counter->hw;
1363 u64 prev, now;
1364 s64 delta;
1365
1366again:
1367 prev = atomic64_read(&hwc->prev_count);
1368 now = atomic64_read(&hwc->count);
1369 if (atomic64_cmpxchg(&hwc->prev_count, prev, now) != prev)
1370 goto again;
1371
1372 delta = now - prev;
1373
1374 atomic64_add(delta, &counter->count);
1375 atomic64_sub(delta, &hwc->period_left);
1376}
1377
1378static void perf_swcounter_set_period(struct perf_counter *counter)
1379{
1380 struct hw_perf_counter *hwc = &counter->hw;
1381 s64 left = atomic64_read(&hwc->period_left);
1382 s64 period = hwc->irq_period;
1383
1384 if (unlikely(left <= -period)) {
1385 left = period;
1386 atomic64_set(&hwc->period_left, left);
1387 }
1388
1389 if (unlikely(left <= 0)) {
1390 left += period;
1391 atomic64_add(period, &hwc->period_left);
1392 }
1393
1394 atomic64_set(&hwc->prev_count, -left);
1395 atomic64_set(&hwc->count, -left);
1396}
1397
1398static void perf_swcounter_save_and_restart(struct perf_counter *counter)
1399{
1400 perf_swcounter_update(counter);
1401 perf_swcounter_set_period(counter);
1402}
1403
1404static void perf_swcounter_store_irq(struct perf_counter *counter, u64 data)
1405{
1406 struct perf_data *irqdata = counter->irqdata;
1407
1408 if (irqdata->len > PERF_DATA_BUFLEN - sizeof(u64)) {
1409 irqdata->overrun++;
1410 } else {
1411 u64 *p = (u64 *) &irqdata->data[irqdata->len];
1412
1413 *p = data;
1414 irqdata->len += sizeof(u64);
1415 }
1416}
1417
1418static void perf_swcounter_handle_group(struct perf_counter *sibling)
1419{
1420 struct perf_counter *counter, *group_leader = sibling->group_leader;
1421
1422 list_for_each_entry(counter, &group_leader->sibling_list, list_entry) {
d6d020e9 1423 counter->hw_ops->read(counter);
15dbf27c
PZ
1424 perf_swcounter_store_irq(sibling, counter->hw_event.type);
1425 perf_swcounter_store_irq(sibling, atomic64_read(&counter->count));
1426 }
1427}
1428
1429static void perf_swcounter_interrupt(struct perf_counter *counter,
1430 int nmi, struct pt_regs *regs)
1431{
15dbf27c
PZ
1432 switch (counter->hw_event.record_type) {
1433 case PERF_RECORD_SIMPLE:
1434 break;
1435
1436 case PERF_RECORD_IRQ:
1437 perf_swcounter_store_irq(counter, instruction_pointer(regs));
1438 break;
1439
1440 case PERF_RECORD_GROUP:
1441 perf_swcounter_handle_group(counter);
1442 break;
1443 }
1444
1445 if (nmi) {
1446 counter->wakeup_pending = 1;
b6c5a71d 1447 set_perf_counter_pending();
15dbf27c
PZ
1448 } else
1449 wake_up(&counter->waitq);
1450}
1451
d6d020e9
PZ
1452static enum hrtimer_restart perf_swcounter_hrtimer(struct hrtimer *hrtimer)
1453{
1454 struct perf_counter *counter;
1455 struct pt_regs *regs;
1456
1457 counter = container_of(hrtimer, struct perf_counter, hw.hrtimer);
1458 counter->hw_ops->read(counter);
1459
1460 regs = get_irq_regs();
1461 /*
1462 * In case we exclude kernel IPs or are somehow not in interrupt
1463 * context, provide the next best thing, the user IP.
1464 */
1465 if ((counter->hw_event.exclude_kernel || !regs) &&
1466 !counter->hw_event.exclude_user)
1467 regs = task_pt_regs(current);
1468
1469 if (regs)
1470 perf_swcounter_interrupt(counter, 0, regs);
1471
1472 hrtimer_forward_now(hrtimer, ns_to_ktime(counter->hw.irq_period));
1473
1474 return HRTIMER_RESTART;
1475}
1476
1477static void perf_swcounter_overflow(struct perf_counter *counter,
1478 int nmi, struct pt_regs *regs)
1479{
1480 perf_swcounter_save_and_restart(counter);
1481 perf_swcounter_interrupt(counter, nmi, regs);
1482}
1483
15dbf27c
PZ
1484static int perf_swcounter_match(struct perf_counter *counter,
1485 enum hw_event_types event,
1486 struct pt_regs *regs)
1487{
1488 if (counter->state != PERF_COUNTER_STATE_ACTIVE)
1489 return 0;
1490
1491 if (counter->hw_event.raw)
1492 return 0;
1493
1494 if (counter->hw_event.type != event)
1495 return 0;
1496
1497 if (counter->hw_event.exclude_user && user_mode(regs))
1498 return 0;
1499
1500 if (counter->hw_event.exclude_kernel && !user_mode(regs))
1501 return 0;
1502
1503 return 1;
1504}
1505
d6d020e9
PZ
1506static void perf_swcounter_add(struct perf_counter *counter, u64 nr,
1507 int nmi, struct pt_regs *regs)
1508{
1509 int neg = atomic64_add_negative(nr, &counter->hw.count);
1510 if (counter->hw.irq_period && !neg)
1511 perf_swcounter_overflow(counter, nmi, regs);
1512}
1513
15dbf27c
PZ
1514static void perf_swcounter_ctx_event(struct perf_counter_context *ctx,
1515 enum hw_event_types event, u64 nr,
1516 int nmi, struct pt_regs *regs)
1517{
1518 struct perf_counter *counter;
15dbf27c 1519
01ef09d9 1520 if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
15dbf27c
PZ
1521 return;
1522
592903cd
PZ
1523 rcu_read_lock();
1524 list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
d6d020e9
PZ
1525 if (perf_swcounter_match(counter, event, regs))
1526 perf_swcounter_add(counter, nr, nmi, regs);
15dbf27c 1527 }
592903cd 1528 rcu_read_unlock();
15dbf27c
PZ
1529}
1530
1531void perf_swcounter_event(enum hw_event_types event, u64 nr,
1532 int nmi, struct pt_regs *regs)
1533{
1534 struct perf_cpu_context *cpuctx = &get_cpu_var(perf_cpu_context);
1535
1536 perf_swcounter_ctx_event(&cpuctx->ctx, event, nr, nmi, regs);
1537 if (cpuctx->task_ctx)
1538 perf_swcounter_ctx_event(cpuctx->task_ctx, event, nr, nmi, regs);
1539
1540 put_cpu_var(perf_cpu_context);
1541}
1542
1543static void perf_swcounter_read(struct perf_counter *counter)
1544{
1545 perf_swcounter_update(counter);
1546}
1547
1548static int perf_swcounter_enable(struct perf_counter *counter)
1549{
1550 perf_swcounter_set_period(counter);
1551 return 0;
1552}
1553
1554static void perf_swcounter_disable(struct perf_counter *counter)
1555{
1556 perf_swcounter_update(counter);
1557}
1558
ac17dc8e
PZ
1559static const struct hw_perf_counter_ops perf_ops_generic = {
1560 .enable = perf_swcounter_enable,
1561 .disable = perf_swcounter_disable,
1562 .read = perf_swcounter_read,
1563};
1564
15dbf27c
PZ
1565/*
1566 * Software counter: cpu wall time clock
1567 */
1568
9abf8a08
PM
1569static void cpu_clock_perf_counter_update(struct perf_counter *counter)
1570{
1571 int cpu = raw_smp_processor_id();
1572 s64 prev;
1573 u64 now;
1574
1575 now = cpu_clock(cpu);
1576 prev = atomic64_read(&counter->hw.prev_count);
1577 atomic64_set(&counter->hw.prev_count, now);
1578 atomic64_add(now - prev, &counter->count);
1579}
1580
d6d020e9
PZ
1581static int cpu_clock_perf_counter_enable(struct perf_counter *counter)
1582{
1583 struct hw_perf_counter *hwc = &counter->hw;
1584 int cpu = raw_smp_processor_id();
1585
1586 atomic64_set(&hwc->prev_count, cpu_clock(cpu));
039fc91e
PZ
1587 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1588 hwc->hrtimer.function = perf_swcounter_hrtimer;
d6d020e9 1589 if (hwc->irq_period) {
d6d020e9
PZ
1590 __hrtimer_start_range_ns(&hwc->hrtimer,
1591 ns_to_ktime(hwc->irq_period), 0,
1592 HRTIMER_MODE_REL, 0);
1593 }
1594
1595 return 0;
1596}
1597
5c92d124
IM
1598static void cpu_clock_perf_counter_disable(struct perf_counter *counter)
1599{
d6d020e9 1600 hrtimer_cancel(&counter->hw.hrtimer);
9abf8a08 1601 cpu_clock_perf_counter_update(counter);
5c92d124
IM
1602}
1603
1604static void cpu_clock_perf_counter_read(struct perf_counter *counter)
1605{
9abf8a08 1606 cpu_clock_perf_counter_update(counter);
5c92d124
IM
1607}
1608
1609static const struct hw_perf_counter_ops perf_ops_cpu_clock = {
7671581f
IM
1610 .enable = cpu_clock_perf_counter_enable,
1611 .disable = cpu_clock_perf_counter_disable,
1612 .read = cpu_clock_perf_counter_read,
5c92d124
IM
1613};
1614
15dbf27c
PZ
1615/*
1616 * Software counter: task time clock
1617 */
1618
aa9c4c0f
IM
1619/*
1620 * Called from within the scheduler:
1621 */
1622static u64 task_clock_perf_counter_val(struct perf_counter *counter, int update)
bae43c99 1623{
aa9c4c0f
IM
1624 struct task_struct *curr = counter->task;
1625 u64 delta;
1626
aa9c4c0f
IM
1627 delta = __task_delta_exec(curr, update);
1628
1629 return curr->se.sum_exec_runtime + delta;
1630}
1631
1632static void task_clock_perf_counter_update(struct perf_counter *counter, u64 now)
1633{
1634 u64 prev;
8cb391e8
IM
1635 s64 delta;
1636
1637 prev = atomic64_read(&counter->hw.prev_count);
8cb391e8
IM
1638
1639 atomic64_set(&counter->hw.prev_count, now);
1640
1641 delta = now - prev;
8cb391e8
IM
1642
1643 atomic64_add(delta, &counter->count);
bae43c99
IM
1644}
1645
95cdd2e7 1646static int task_clock_perf_counter_enable(struct perf_counter *counter)
8cb391e8 1647{
d6d020e9
PZ
1648 struct hw_perf_counter *hwc = &counter->hw;
1649
1650 atomic64_set(&hwc->prev_count, task_clock_perf_counter_val(counter, 0));
039fc91e
PZ
1651 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1652 hwc->hrtimer.function = perf_swcounter_hrtimer;
d6d020e9 1653 if (hwc->irq_period) {
d6d020e9
PZ
1654 __hrtimer_start_range_ns(&hwc->hrtimer,
1655 ns_to_ktime(hwc->irq_period), 0,
1656 HRTIMER_MODE_REL, 0);
1657 }
95cdd2e7
IM
1658
1659 return 0;
8cb391e8
IM
1660}
1661
1662static void task_clock_perf_counter_disable(struct perf_counter *counter)
bae43c99 1663{
d6d020e9
PZ
1664 hrtimer_cancel(&counter->hw.hrtimer);
1665 task_clock_perf_counter_update(counter,
1666 task_clock_perf_counter_val(counter, 0));
1667}
aa9c4c0f 1668
d6d020e9
PZ
1669static void task_clock_perf_counter_read(struct perf_counter *counter)
1670{
1671 task_clock_perf_counter_update(counter,
1672 task_clock_perf_counter_val(counter, 1));
bae43c99
IM
1673}
1674
1675static const struct hw_perf_counter_ops perf_ops_task_clock = {
7671581f
IM
1676 .enable = task_clock_perf_counter_enable,
1677 .disable = task_clock_perf_counter_disable,
1678 .read = task_clock_perf_counter_read,
bae43c99
IM
1679};
1680
15dbf27c
PZ
1681/*
1682 * Software counter: cpu migrations
1683 */
1684
23a185ca 1685static inline u64 get_cpu_migrations(struct perf_counter *counter)
6c594c21 1686{
23a185ca
PM
1687 struct task_struct *curr = counter->ctx->task;
1688
1689 if (curr)
1690 return curr->se.nr_migrations;
1691 return cpu_nr_migrations(smp_processor_id());
6c594c21
IM
1692}
1693
1694static void cpu_migrations_perf_counter_update(struct perf_counter *counter)
1695{
1696 u64 prev, now;
1697 s64 delta;
1698
1699 prev = atomic64_read(&counter->hw.prev_count);
23a185ca 1700 now = get_cpu_migrations(counter);
6c594c21
IM
1701
1702 atomic64_set(&counter->hw.prev_count, now);
1703
1704 delta = now - prev;
6c594c21
IM
1705
1706 atomic64_add(delta, &counter->count);
1707}
1708
1709static void cpu_migrations_perf_counter_read(struct perf_counter *counter)
1710{
1711 cpu_migrations_perf_counter_update(counter);
1712}
1713
95cdd2e7 1714static int cpu_migrations_perf_counter_enable(struct perf_counter *counter)
6c594c21 1715{
c07c99b6
PM
1716 if (counter->prev_state <= PERF_COUNTER_STATE_OFF)
1717 atomic64_set(&counter->hw.prev_count,
1718 get_cpu_migrations(counter));
95cdd2e7 1719 return 0;
6c594c21
IM
1720}
1721
1722static void cpu_migrations_perf_counter_disable(struct perf_counter *counter)
1723{
1724 cpu_migrations_perf_counter_update(counter);
1725}
1726
1727static const struct hw_perf_counter_ops perf_ops_cpu_migrations = {
7671581f
IM
1728 .enable = cpu_migrations_perf_counter_enable,
1729 .disable = cpu_migrations_perf_counter_disable,
1730 .read = cpu_migrations_perf_counter_read,
6c594c21
IM
1731};
1732
e077df4f
PZ
1733#ifdef CONFIG_EVENT_PROFILE
1734void perf_tpcounter_event(int event_id)
1735{
1736 perf_swcounter_event(PERF_TP_EVENTS_MIN + event_id, 1, 1,
1737 task_pt_regs(current));
1738}
1739
1740extern int ftrace_profile_enable(int);
1741extern void ftrace_profile_disable(int);
1742
1743static void tp_perf_counter_destroy(struct perf_counter *counter)
1744{
1745 int event_id = counter->hw_event.type - PERF_TP_EVENTS_MIN;
1746
1747 ftrace_profile_disable(event_id);
1748}
1749
1750static const struct hw_perf_counter_ops *
1751tp_perf_counter_init(struct perf_counter *counter)
1752{
1753 int event_id = counter->hw_event.type - PERF_TP_EVENTS_MIN;
1754 int ret;
1755
1756 ret = ftrace_profile_enable(event_id);
1757 if (ret)
1758 return NULL;
1759
1760 counter->destroy = tp_perf_counter_destroy;
1761
1762 return &perf_ops_generic;
1763}
1764#else
1765static const struct hw_perf_counter_ops *
1766tp_perf_counter_init(struct perf_counter *counter)
1767{
1768 return NULL;
1769}
1770#endif
1771
5c92d124
IM
1772static const struct hw_perf_counter_ops *
1773sw_perf_counter_init(struct perf_counter *counter)
1774{
15dbf27c 1775 struct perf_counter_hw_event *hw_event = &counter->hw_event;
5c92d124 1776 const struct hw_perf_counter_ops *hw_ops = NULL;
15dbf27c 1777 struct hw_perf_counter *hwc = &counter->hw;
5c92d124 1778
0475f9ea
PM
1779 /*
1780 * Software counters (currently) can't in general distinguish
1781 * between user, kernel and hypervisor events.
1782 * However, context switches and cpu migrations are considered
1783 * to be kernel events, and page faults are never hypervisor
1784 * events.
1785 */
5c92d124
IM
1786 switch (counter->hw_event.type) {
1787 case PERF_COUNT_CPU_CLOCK:
d6d020e9
PZ
1788 hw_ops = &perf_ops_cpu_clock;
1789
1790 if (hw_event->irq_period && hw_event->irq_period < 10000)
1791 hw_event->irq_period = 10000;
5c92d124 1792 break;
bae43c99 1793 case PERF_COUNT_TASK_CLOCK:
23a185ca
PM
1794 /*
1795 * If the user instantiates this as a per-cpu counter,
1796 * use the cpu_clock counter instead.
1797 */
1798 if (counter->ctx->task)
1799 hw_ops = &perf_ops_task_clock;
1800 else
1801 hw_ops = &perf_ops_cpu_clock;
d6d020e9
PZ
1802
1803 if (hw_event->irq_period && hw_event->irq_period < 10000)
1804 hw_event->irq_period = 10000;
bae43c99 1805 break;
e06c61a8 1806 case PERF_COUNT_PAGE_FAULTS:
ac17dc8e
PZ
1807 case PERF_COUNT_PAGE_FAULTS_MIN:
1808 case PERF_COUNT_PAGE_FAULTS_MAJ:
5d6a27d8 1809 case PERF_COUNT_CONTEXT_SWITCHES:
4a0deca6 1810 hw_ops = &perf_ops_generic;
5d6a27d8 1811 break;
6c594c21 1812 case PERF_COUNT_CPU_MIGRATIONS:
0475f9ea
PM
1813 if (!counter->hw_event.exclude_kernel)
1814 hw_ops = &perf_ops_cpu_migrations;
6c594c21 1815 break;
5c92d124 1816 default:
e077df4f 1817 hw_ops = tp_perf_counter_init(counter);
5c92d124
IM
1818 break;
1819 }
15dbf27c
PZ
1820
1821 if (hw_ops)
1822 hwc->irq_period = hw_event->irq_period;
1823
5c92d124
IM
1824 return hw_ops;
1825}
1826
0793a61d
TG
1827/*
1828 * Allocate and initialize a counter structure
1829 */
1830static struct perf_counter *
04289bb9
IM
1831perf_counter_alloc(struct perf_counter_hw_event *hw_event,
1832 int cpu,
23a185ca 1833 struct perf_counter_context *ctx,
9b51f66d
IM
1834 struct perf_counter *group_leader,
1835 gfp_t gfpflags)
0793a61d 1836{
5c92d124 1837 const struct hw_perf_counter_ops *hw_ops;
621a01ea 1838 struct perf_counter *counter;
0793a61d 1839
9b51f66d 1840 counter = kzalloc(sizeof(*counter), gfpflags);
0793a61d
TG
1841 if (!counter)
1842 return NULL;
1843
04289bb9
IM
1844 /*
1845 * Single counters are their own group leaders, with an
1846 * empty sibling list:
1847 */
1848 if (!group_leader)
1849 group_leader = counter;
1850
0793a61d 1851 mutex_init(&counter->mutex);
04289bb9 1852 INIT_LIST_HEAD(&counter->list_entry);
592903cd 1853 INIT_LIST_HEAD(&counter->event_entry);
04289bb9 1854 INIT_LIST_HEAD(&counter->sibling_list);
0793a61d
TG
1855 init_waitqueue_head(&counter->waitq);
1856
d859e29f
PM
1857 INIT_LIST_HEAD(&counter->child_list);
1858
9f66a381
IM
1859 counter->irqdata = &counter->data[0];
1860 counter->usrdata = &counter->data[1];
1861 counter->cpu = cpu;
1862 counter->hw_event = *hw_event;
1863 counter->wakeup_pending = 0;
04289bb9 1864 counter->group_leader = group_leader;
621a01ea 1865 counter->hw_ops = NULL;
23a185ca 1866 counter->ctx = ctx;
621a01ea 1867
235c7fc7 1868 counter->state = PERF_COUNTER_STATE_INACTIVE;
a86ed508
IM
1869 if (hw_event->disabled)
1870 counter->state = PERF_COUNTER_STATE_OFF;
1871
5c92d124
IM
1872 hw_ops = NULL;
1873 if (!hw_event->raw && hw_event->type < 0)
1874 hw_ops = sw_perf_counter_init(counter);
23a185ca 1875 else
5c92d124 1876 hw_ops = hw_perf_counter_init(counter);
5c92d124 1877
621a01ea
IM
1878 if (!hw_ops) {
1879 kfree(counter);
1880 return NULL;
1881 }
1882 counter->hw_ops = hw_ops;
0793a61d
TG
1883
1884 return counter;
1885}
1886
1887/**
2743a5b0 1888 * sys_perf_counter_open - open a performance counter, associate it to a task/cpu
9f66a381
IM
1889 *
1890 * @hw_event_uptr: event type attributes for monitoring/sampling
0793a61d 1891 * @pid: target pid
9f66a381
IM
1892 * @cpu: target cpu
1893 * @group_fd: group leader counter fd
0793a61d 1894 */
2743a5b0 1895SYSCALL_DEFINE5(perf_counter_open,
f3dfd265 1896 const struct perf_counter_hw_event __user *, hw_event_uptr,
2743a5b0 1897 pid_t, pid, int, cpu, int, group_fd, unsigned long, flags)
0793a61d 1898{
04289bb9 1899 struct perf_counter *counter, *group_leader;
9f66a381 1900 struct perf_counter_hw_event hw_event;
04289bb9 1901 struct perf_counter_context *ctx;
9b51f66d 1902 struct file *counter_file = NULL;
04289bb9
IM
1903 struct file *group_file = NULL;
1904 int fput_needed = 0;
9b51f66d 1905 int fput_needed2 = 0;
0793a61d
TG
1906 int ret;
1907
2743a5b0
PM
1908 /* for future expandability... */
1909 if (flags)
1910 return -EINVAL;
1911
9f66a381 1912 if (copy_from_user(&hw_event, hw_event_uptr, sizeof(hw_event)) != 0)
eab656ae
TG
1913 return -EFAULT;
1914
04289bb9 1915 /*
ccff286d
IM
1916 * Get the target context (task or percpu):
1917 */
1918 ctx = find_get_context(pid, cpu);
1919 if (IS_ERR(ctx))
1920 return PTR_ERR(ctx);
1921
1922 /*
1923 * Look up the group leader (we will attach this counter to it):
04289bb9
IM
1924 */
1925 group_leader = NULL;
1926 if (group_fd != -1) {
1927 ret = -EINVAL;
1928 group_file = fget_light(group_fd, &fput_needed);
1929 if (!group_file)
ccff286d 1930 goto err_put_context;
04289bb9 1931 if (group_file->f_op != &perf_fops)
ccff286d 1932 goto err_put_context;
04289bb9
IM
1933
1934 group_leader = group_file->private_data;
1935 /*
ccff286d
IM
1936 * Do not allow a recursive hierarchy (this new sibling
1937 * becoming part of another group-sibling):
1938 */
1939 if (group_leader->group_leader != group_leader)
1940 goto err_put_context;
1941 /*
1942 * Do not allow to attach to a group in a different
1943 * task or CPU context:
04289bb9 1944 */
ccff286d
IM
1945 if (group_leader->ctx != ctx)
1946 goto err_put_context;
3b6f9e5c
PM
1947 /*
1948 * Only a group leader can be exclusive or pinned
1949 */
1950 if (hw_event.exclusive || hw_event.pinned)
1951 goto err_put_context;
04289bb9
IM
1952 }
1953
5c92d124 1954 ret = -EINVAL;
23a185ca
PM
1955 counter = perf_counter_alloc(&hw_event, cpu, ctx, group_leader,
1956 GFP_KERNEL);
0793a61d
TG
1957 if (!counter)
1958 goto err_put_context;
1959
0793a61d
TG
1960 ret = anon_inode_getfd("[perf_counter]", &perf_fops, counter, 0);
1961 if (ret < 0)
9b51f66d
IM
1962 goto err_free_put_context;
1963
1964 counter_file = fget_light(ret, &fput_needed2);
1965 if (!counter_file)
1966 goto err_free_put_context;
1967
1968 counter->filp = counter_file;
d859e29f 1969 mutex_lock(&ctx->mutex);
9b51f66d 1970 perf_install_in_context(ctx, counter, cpu);
d859e29f 1971 mutex_unlock(&ctx->mutex);
9b51f66d
IM
1972
1973 fput_light(counter_file, fput_needed2);
0793a61d 1974
04289bb9
IM
1975out_fput:
1976 fput_light(group_file, fput_needed);
1977
0793a61d
TG
1978 return ret;
1979
9b51f66d 1980err_free_put_context:
0793a61d
TG
1981 kfree(counter);
1982
1983err_put_context:
1984 put_context(ctx);
1985
04289bb9 1986 goto out_fput;
0793a61d
TG
1987}
1988
9b51f66d
IM
1989/*
1990 * Initialize the perf_counter context in a task_struct:
1991 */
1992static void
1993__perf_counter_init_context(struct perf_counter_context *ctx,
1994 struct task_struct *task)
1995{
1996 memset(ctx, 0, sizeof(*ctx));
1997 spin_lock_init(&ctx->lock);
d859e29f 1998 mutex_init(&ctx->mutex);
9b51f66d 1999 INIT_LIST_HEAD(&ctx->counter_list);
592903cd 2000 INIT_LIST_HEAD(&ctx->event_list);
9b51f66d
IM
2001 ctx->task = task;
2002}
2003
2004/*
2005 * inherit a counter from parent task to child task:
2006 */
d859e29f 2007static struct perf_counter *
9b51f66d
IM
2008inherit_counter(struct perf_counter *parent_counter,
2009 struct task_struct *parent,
2010 struct perf_counter_context *parent_ctx,
2011 struct task_struct *child,
d859e29f 2012 struct perf_counter *group_leader,
9b51f66d
IM
2013 struct perf_counter_context *child_ctx)
2014{
2015 struct perf_counter *child_counter;
2016
d859e29f
PM
2017 /*
2018 * Instead of creating recursive hierarchies of counters,
2019 * we link inherited counters back to the original parent,
2020 * which has a filp for sure, which we use as the reference
2021 * count:
2022 */
2023 if (parent_counter->parent)
2024 parent_counter = parent_counter->parent;
2025
9b51f66d 2026 child_counter = perf_counter_alloc(&parent_counter->hw_event,
23a185ca
PM
2027 parent_counter->cpu, child_ctx,
2028 group_leader, GFP_KERNEL);
9b51f66d 2029 if (!child_counter)
d859e29f 2030 return NULL;
9b51f66d
IM
2031
2032 /*
2033 * Link it up in the child's context:
2034 */
9b51f66d
IM
2035 child_counter->task = child;
2036 list_add_counter(child_counter, child_ctx);
2037 child_ctx->nr_counters++;
2038
2039 child_counter->parent = parent_counter;
9b51f66d
IM
2040 /*
2041 * inherit into child's child as well:
2042 */
2043 child_counter->hw_event.inherit = 1;
2044
2045 /*
2046 * Get a reference to the parent filp - we will fput it
2047 * when the child counter exits. This is safe to do because
2048 * we are in the parent and we know that the filp still
2049 * exists and has a nonzero count:
2050 */
2051 atomic_long_inc(&parent_counter->filp->f_count);
2052
d859e29f
PM
2053 /*
2054 * Link this into the parent counter's child list
2055 */
2056 mutex_lock(&parent_counter->mutex);
2057 list_add_tail(&child_counter->child_list, &parent_counter->child_list);
2058
2059 /*
2060 * Make the child state follow the state of the parent counter,
2061 * not its hw_event.disabled bit. We hold the parent's mutex,
2062 * so we won't race with perf_counter_{en,dis}able_family.
2063 */
2064 if (parent_counter->state >= PERF_COUNTER_STATE_INACTIVE)
2065 child_counter->state = PERF_COUNTER_STATE_INACTIVE;
2066 else
2067 child_counter->state = PERF_COUNTER_STATE_OFF;
2068
2069 mutex_unlock(&parent_counter->mutex);
2070
2071 return child_counter;
2072}
2073
2074static int inherit_group(struct perf_counter *parent_counter,
2075 struct task_struct *parent,
2076 struct perf_counter_context *parent_ctx,
2077 struct task_struct *child,
2078 struct perf_counter_context *child_ctx)
2079{
2080 struct perf_counter *leader;
2081 struct perf_counter *sub;
2082
2083 leader = inherit_counter(parent_counter, parent, parent_ctx,
2084 child, NULL, child_ctx);
2085 if (!leader)
2086 return -ENOMEM;
2087 list_for_each_entry(sub, &parent_counter->sibling_list, list_entry) {
2088 if (!inherit_counter(sub, parent, parent_ctx,
2089 child, leader, child_ctx))
2090 return -ENOMEM;
2091 }
9b51f66d
IM
2092 return 0;
2093}
2094
d859e29f
PM
2095static void sync_child_counter(struct perf_counter *child_counter,
2096 struct perf_counter *parent_counter)
2097{
2098 u64 parent_val, child_val;
2099
2100 parent_val = atomic64_read(&parent_counter->count);
2101 child_val = atomic64_read(&child_counter->count);
2102
2103 /*
2104 * Add back the child's count to the parent's count:
2105 */
2106 atomic64_add(child_val, &parent_counter->count);
2107
2108 /*
2109 * Remove this counter from the parent's list
2110 */
2111 mutex_lock(&parent_counter->mutex);
2112 list_del_init(&child_counter->child_list);
2113 mutex_unlock(&parent_counter->mutex);
2114
2115 /*
2116 * Release the parent counter, if this was the last
2117 * reference to it.
2118 */
2119 fput(parent_counter->filp);
2120}
2121
9b51f66d
IM
2122static void
2123__perf_counter_exit_task(struct task_struct *child,
2124 struct perf_counter *child_counter,
2125 struct perf_counter_context *child_ctx)
2126{
2127 struct perf_counter *parent_counter;
d859e29f 2128 struct perf_counter *sub, *tmp;
9b51f66d
IM
2129
2130 /*
235c7fc7
IM
2131 * If we do not self-reap then we have to wait for the
2132 * child task to unschedule (it will happen for sure),
2133 * so that its counter is at its final count. (This
2134 * condition triggers rarely - child tasks usually get
2135 * off their CPU before the parent has a chance to
2136 * get this far into the reaping action)
9b51f66d 2137 */
235c7fc7
IM
2138 if (child != current) {
2139 wait_task_inactive(child, 0);
2140 list_del_init(&child_counter->list_entry);
2141 } else {
0cc0c027 2142 struct perf_cpu_context *cpuctx;
235c7fc7
IM
2143 unsigned long flags;
2144 u64 perf_flags;
2145
2146 /*
2147 * Disable and unlink this counter.
2148 *
2149 * Be careful about zapping the list - IRQ/NMI context
2150 * could still be processing it:
2151 */
2152 curr_rq_lock_irq_save(&flags);
2153 perf_flags = hw_perf_save_disable();
0cc0c027
IM
2154
2155 cpuctx = &__get_cpu_var(perf_cpu_context);
2156
d859e29f 2157 group_sched_out(child_counter, cpuctx, child_ctx);
0cc0c027 2158
235c7fc7 2159 list_del_init(&child_counter->list_entry);
0cc0c027 2160
235c7fc7 2161 child_ctx->nr_counters--;
9b51f66d 2162
235c7fc7
IM
2163 hw_perf_restore(perf_flags);
2164 curr_rq_unlock_irq_restore(&flags);
2165 }
9b51f66d
IM
2166
2167 parent_counter = child_counter->parent;
2168 /*
2169 * It can happen that parent exits first, and has counters
2170 * that are still around due to the child reference. These
2171 * counters need to be zapped - but otherwise linger.
2172 */
d859e29f
PM
2173 if (parent_counter) {
2174 sync_child_counter(child_counter, parent_counter);
2175 list_for_each_entry_safe(sub, tmp, &child_counter->sibling_list,
2176 list_entry) {
4bcf349a 2177 if (sub->parent) {
d859e29f 2178 sync_child_counter(sub, sub->parent);
f1600952 2179 free_counter(sub);
4bcf349a 2180 }
d859e29f 2181 }
f1600952 2182 free_counter(child_counter);
4bcf349a 2183 }
9b51f66d
IM
2184}
2185
2186/*
d859e29f 2187 * When a child task exits, feed back counter values to parent counters.
9b51f66d 2188 *
d859e29f 2189 * Note: we may be running in child context, but the PID is not hashed
9b51f66d
IM
2190 * anymore so new counters will not be added.
2191 */
2192void perf_counter_exit_task(struct task_struct *child)
2193{
2194 struct perf_counter *child_counter, *tmp;
2195 struct perf_counter_context *child_ctx;
2196
2197 child_ctx = &child->perf_counter_ctx;
2198
2199 if (likely(!child_ctx->nr_counters))
2200 return;
2201
2202 list_for_each_entry_safe(child_counter, tmp, &child_ctx->counter_list,
2203 list_entry)
2204 __perf_counter_exit_task(child, child_counter, child_ctx);
2205}
2206
2207/*
2208 * Initialize the perf_counter context in task_struct
2209 */
2210void perf_counter_init_task(struct task_struct *child)
2211{
2212 struct perf_counter_context *child_ctx, *parent_ctx;
d859e29f 2213 struct perf_counter *counter;
9b51f66d 2214 struct task_struct *parent = current;
9b51f66d
IM
2215
2216 child_ctx = &child->perf_counter_ctx;
2217 parent_ctx = &parent->perf_counter_ctx;
2218
2219 __perf_counter_init_context(child_ctx, child);
2220
2221 /*
2222 * This is executed from the parent task context, so inherit
2223 * counters that have been marked for cloning:
2224 */
2225
2226 if (likely(!parent_ctx->nr_counters))
2227 return;
2228
2229 /*
2230 * Lock the parent list. No need to lock the child - not PID
2231 * hashed yet and not running, so nobody can access it.
2232 */
d859e29f 2233 mutex_lock(&parent_ctx->mutex);
9b51f66d
IM
2234
2235 /*
2236 * We dont have to disable NMIs - we are only looking at
2237 * the list, not manipulating it:
2238 */
2239 list_for_each_entry(counter, &parent_ctx->counter_list, list_entry) {
d859e29f 2240 if (!counter->hw_event.inherit)
9b51f66d
IM
2241 continue;
2242
d859e29f 2243 if (inherit_group(counter, parent,
9b51f66d
IM
2244 parent_ctx, child, child_ctx))
2245 break;
2246 }
2247
d859e29f 2248 mutex_unlock(&parent_ctx->mutex);
9b51f66d
IM
2249}
2250
04289bb9 2251static void __cpuinit perf_counter_init_cpu(int cpu)
0793a61d 2252{
04289bb9 2253 struct perf_cpu_context *cpuctx;
0793a61d 2254
04289bb9
IM
2255 cpuctx = &per_cpu(perf_cpu_context, cpu);
2256 __perf_counter_init_context(&cpuctx->ctx, NULL);
0793a61d
TG
2257
2258 mutex_lock(&perf_resource_mutex);
04289bb9 2259 cpuctx->max_pertask = perf_max_counters - perf_reserved_percpu;
0793a61d 2260 mutex_unlock(&perf_resource_mutex);
04289bb9 2261
01d0287f 2262 hw_perf_counter_setup(cpu);
0793a61d
TG
2263}
2264
2265#ifdef CONFIG_HOTPLUG_CPU
04289bb9 2266static void __perf_counter_exit_cpu(void *info)
0793a61d
TG
2267{
2268 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
2269 struct perf_counter_context *ctx = &cpuctx->ctx;
2270 struct perf_counter *counter, *tmp;
2271
04289bb9
IM
2272 list_for_each_entry_safe(counter, tmp, &ctx->counter_list, list_entry)
2273 __perf_counter_remove_from_context(counter);
0793a61d 2274}
04289bb9 2275static void perf_counter_exit_cpu(int cpu)
0793a61d 2276{
d859e29f
PM
2277 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
2278 struct perf_counter_context *ctx = &cpuctx->ctx;
2279
2280 mutex_lock(&ctx->mutex);
04289bb9 2281 smp_call_function_single(cpu, __perf_counter_exit_cpu, NULL, 1);
d859e29f 2282 mutex_unlock(&ctx->mutex);
0793a61d
TG
2283}
2284#else
04289bb9 2285static inline void perf_counter_exit_cpu(int cpu) { }
0793a61d
TG
2286#endif
2287
2288static int __cpuinit
2289perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
2290{
2291 unsigned int cpu = (long)hcpu;
2292
2293 switch (action) {
2294
2295 case CPU_UP_PREPARE:
2296 case CPU_UP_PREPARE_FROZEN:
04289bb9 2297 perf_counter_init_cpu(cpu);
0793a61d
TG
2298 break;
2299
2300 case CPU_DOWN_PREPARE:
2301 case CPU_DOWN_PREPARE_FROZEN:
04289bb9 2302 perf_counter_exit_cpu(cpu);
0793a61d
TG
2303 break;
2304
2305 default:
2306 break;
2307 }
2308
2309 return NOTIFY_OK;
2310}
2311
2312static struct notifier_block __cpuinitdata perf_cpu_nb = {
2313 .notifier_call = perf_cpu_notify,
2314};
2315
2316static int __init perf_counter_init(void)
2317{
2318 perf_cpu_notify(&perf_cpu_nb, (unsigned long)CPU_UP_PREPARE,
2319 (void *)(long)smp_processor_id());
2320 register_cpu_notifier(&perf_cpu_nb);
2321
2322 return 0;
2323}
2324early_initcall(perf_counter_init);
2325
2326static ssize_t perf_show_reserve_percpu(struct sysdev_class *class, char *buf)
2327{
2328 return sprintf(buf, "%d\n", perf_reserved_percpu);
2329}
2330
2331static ssize_t
2332perf_set_reserve_percpu(struct sysdev_class *class,
2333 const char *buf,
2334 size_t count)
2335{
2336 struct perf_cpu_context *cpuctx;
2337 unsigned long val;
2338 int err, cpu, mpt;
2339
2340 err = strict_strtoul(buf, 10, &val);
2341 if (err)
2342 return err;
2343 if (val > perf_max_counters)
2344 return -EINVAL;
2345
2346 mutex_lock(&perf_resource_mutex);
2347 perf_reserved_percpu = val;
2348 for_each_online_cpu(cpu) {
2349 cpuctx = &per_cpu(perf_cpu_context, cpu);
2350 spin_lock_irq(&cpuctx->ctx.lock);
2351 mpt = min(perf_max_counters - cpuctx->ctx.nr_counters,
2352 perf_max_counters - perf_reserved_percpu);
2353 cpuctx->max_pertask = mpt;
2354 spin_unlock_irq(&cpuctx->ctx.lock);
2355 }
2356 mutex_unlock(&perf_resource_mutex);
2357
2358 return count;
2359}
2360
2361static ssize_t perf_show_overcommit(struct sysdev_class *class, char *buf)
2362{
2363 return sprintf(buf, "%d\n", perf_overcommit);
2364}
2365
2366static ssize_t
2367perf_set_overcommit(struct sysdev_class *class, const char *buf, size_t count)
2368{
2369 unsigned long val;
2370 int err;
2371
2372 err = strict_strtoul(buf, 10, &val);
2373 if (err)
2374 return err;
2375 if (val > 1)
2376 return -EINVAL;
2377
2378 mutex_lock(&perf_resource_mutex);
2379 perf_overcommit = val;
2380 mutex_unlock(&perf_resource_mutex);
2381
2382 return count;
2383}
2384
2385static SYSDEV_CLASS_ATTR(
2386 reserve_percpu,
2387 0644,
2388 perf_show_reserve_percpu,
2389 perf_set_reserve_percpu
2390 );
2391
2392static SYSDEV_CLASS_ATTR(
2393 overcommit,
2394 0644,
2395 perf_show_overcommit,
2396 perf_set_overcommit
2397 );
2398
2399static struct attribute *perfclass_attrs[] = {
2400 &attr_reserve_percpu.attr,
2401 &attr_overcommit.attr,
2402 NULL
2403};
2404
2405static struct attribute_group perfclass_attr_group = {
2406 .attrs = perfclass_attrs,
2407 .name = "perf_counters",
2408};
2409
2410static int __init perf_counter_sysfs_init(void)
2411{
2412 return sysfs_create_group(&cpu_sysdev_class.kset.kobj,
2413 &perfclass_attr_group);
2414}
2415device_initcall(perf_counter_sysfs_init);