aboutsummaryrefslogtreecommitdiffstats
path: root/kernel/perf_counter.c
diff options
context:
space:
mode:
Diffstat (limited to 'kernel/perf_counter.c')
-rw-r--r--kernel/perf_counter.c3946
1 files changed, 3946 insertions, 0 deletions
diff --git a/kernel/perf_counter.c b/kernel/perf_counter.c
new file mode 100644
index 000000000000..79c3f26541d3
--- /dev/null
+++ b/kernel/perf_counter.c
@@ -0,0 +1,3946 @@
1/*
2 * Performance counter core code
3 *
4 * Copyright (C) 2008 Thomas Gleixner <tglx@linutronix.de>
5 * Copyright (C) 2008-2009 Red Hat, Inc., Ingo Molnar
6 * Copyright (C) 2008-2009 Red Hat, Inc., Peter Zijlstra <pzijlstr@redhat.com>
7 * Copyright © 2009 Paul Mackerras, IBM Corp. <paulus@au1.ibm.com>
8 *
9 * For licensing details see kernel-base/COPYING
10 */
11
12#include <linux/fs.h>
13#include <linux/mm.h>
14#include <linux/cpu.h>
15#include <linux/smp.h>
16#include <linux/file.h>
17#include <linux/poll.h>
18#include <linux/sysfs.h>
19#include <linux/ptrace.h>
20#include <linux/percpu.h>
21#include <linux/vmstat.h>
22#include <linux/hardirq.h>
23#include <linux/rculist.h>
24#include <linux/uaccess.h>
25#include <linux/syscalls.h>
26#include <linux/anon_inodes.h>
27#include <linux/kernel_stat.h>
28#include <linux/perf_counter.h>
29#include <linux/dcache.h>
30
31#include <asm/irq_regs.h>
32
33/*
34 * Each CPU has a list of per CPU counters:
35 */
36DEFINE_PER_CPU(struct perf_cpu_context, perf_cpu_context);
37
38int perf_max_counters __read_mostly = 1;
39static int perf_reserved_percpu __read_mostly;
40static int perf_overcommit __read_mostly = 1;
41
42static atomic_t nr_counters __read_mostly;
43static atomic_t nr_mmap_tracking __read_mostly;
44static atomic_t nr_munmap_tracking __read_mostly;
45static atomic_t nr_comm_tracking __read_mostly;
46
47int sysctl_perf_counter_priv __read_mostly; /* do we need to be privileged */
48int sysctl_perf_counter_mlock __read_mostly = 512; /* 'free' kb per user */
49int sysctl_perf_counter_limit __read_mostly = 100000; /* max NMIs per second */
50
51/*
52 * Lock for (sysadmin-configurable) counter reservations:
53 */
54static DEFINE_SPINLOCK(perf_resource_lock);
55
56/*
57 * Architecture provided APIs - weak aliases:
58 */
59extern __weak const struct pmu *hw_perf_counter_init(struct perf_counter *counter)
60{
61 return NULL;
62}
63
64void __weak hw_perf_disable(void) { barrier(); }
65void __weak hw_perf_enable(void) { barrier(); }
66
67void __weak hw_perf_counter_setup(int cpu) { barrier(); }
68int __weak hw_perf_group_sched_in(struct perf_counter *group_leader,
69 struct perf_cpu_context *cpuctx,
70 struct perf_counter_context *ctx, int cpu)
71{
72 return 0;
73}
74
75void __weak perf_counter_print_debug(void) { }
76
77static DEFINE_PER_CPU(int, disable_count);
78
79void __perf_disable(void)
80{
81 __get_cpu_var(disable_count)++;
82}
83
84bool __perf_enable(void)
85{
86 return !--__get_cpu_var(disable_count);
87}
88
89void perf_disable(void)
90{
91 __perf_disable();
92 hw_perf_disable();
93}
94
95void perf_enable(void)
96{
97 if (__perf_enable())
98 hw_perf_enable();
99}
100
101static void get_ctx(struct perf_counter_context *ctx)
102{
103 atomic_inc(&ctx->refcount);
104}
105
106static void free_ctx(struct rcu_head *head)
107{
108 struct perf_counter_context *ctx;
109
110 ctx = container_of(head, struct perf_counter_context, rcu_head);
111 kfree(ctx);
112}
113
114static void put_ctx(struct perf_counter_context *ctx)
115{
116 if (atomic_dec_and_test(&ctx->refcount)) {
117 if (ctx->parent_ctx)
118 put_ctx(ctx->parent_ctx);
119 if (ctx->task)
120 put_task_struct(ctx->task);
121 call_rcu(&ctx->rcu_head, free_ctx);
122 }
123}
124
125/*
126 * Add a counter from the lists for its context.
127 * Must be called with ctx->mutex and ctx->lock held.
128 */
129static void
130list_add_counter(struct perf_counter *counter, struct perf_counter_context *ctx)
131{
132 struct perf_counter *group_leader = counter->group_leader;
133
134 /*
135 * Depending on whether it is a standalone or sibling counter,
136 * add it straight to the context's counter list, or to the group
137 * leader's sibling list:
138 */
139 if (group_leader == counter)
140 list_add_tail(&counter->list_entry, &ctx->counter_list);
141 else {
142 list_add_tail(&counter->list_entry, &group_leader->sibling_list);
143 group_leader->nr_siblings++;
144 }
145
146 list_add_rcu(&counter->event_entry, &ctx->event_list);
147 ctx->nr_counters++;
148}
149
150/*
151 * Remove a counter from the lists for its context.
152 * Must be called with ctx->mutex and ctx->lock held.
153 */
154static void
155list_del_counter(struct perf_counter *counter, struct perf_counter_context *ctx)
156{
157 struct perf_counter *sibling, *tmp;
158
159 if (list_empty(&counter->list_entry))
160 return;
161 ctx->nr_counters--;
162
163 list_del_init(&counter->list_entry);
164 list_del_rcu(&counter->event_entry);
165
166 if (counter->group_leader != counter)
167 counter->group_leader->nr_siblings--;
168
169 /*
170 * If this was a group counter with sibling counters then
171 * upgrade the siblings to singleton counters by adding them
172 * to the context list directly:
173 */
174 list_for_each_entry_safe(sibling, tmp,
175 &counter->sibling_list, list_entry) {
176
177 list_move_tail(&sibling->list_entry, &ctx->counter_list);
178 sibling->group_leader = sibling;
179 }
180}
181
182static void
183counter_sched_out(struct perf_counter *counter,
184 struct perf_cpu_context *cpuctx,
185 struct perf_counter_context *ctx)
186{
187 if (counter->state != PERF_COUNTER_STATE_ACTIVE)
188 return;
189
190 counter->state = PERF_COUNTER_STATE_INACTIVE;
191 counter->tstamp_stopped = ctx->time;
192 counter->pmu->disable(counter);
193 counter->oncpu = -1;
194
195 if (!is_software_counter(counter))
196 cpuctx->active_oncpu--;
197 ctx->nr_active--;
198 if (counter->hw_event.exclusive || !cpuctx->active_oncpu)
199 cpuctx->exclusive = 0;
200}
201
202static void
203group_sched_out(struct perf_counter *group_counter,
204 struct perf_cpu_context *cpuctx,
205 struct perf_counter_context *ctx)
206{
207 struct perf_counter *counter;
208
209 if (group_counter->state != PERF_COUNTER_STATE_ACTIVE)
210 return;
211
212 counter_sched_out(group_counter, cpuctx, ctx);
213
214 /*
215 * Schedule out siblings (if any):
216 */
217 list_for_each_entry(counter, &group_counter->sibling_list, list_entry)
218 counter_sched_out(counter, cpuctx, ctx);
219
220 if (group_counter->hw_event.exclusive)
221 cpuctx->exclusive = 0;
222}
223
224/*
225 * Cross CPU call to remove a performance counter
226 *
227 * We disable the counter on the hardware level first. After that we
228 * remove it from the context list.
229 */
230static void __perf_counter_remove_from_context(void *info)
231{
232 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
233 struct perf_counter *counter = info;
234 struct perf_counter_context *ctx = counter->ctx;
235
236 /*
237 * If this is a task context, we need to check whether it is
238 * the current task context of this cpu. If not it has been
239 * scheduled out before the smp call arrived.
240 */
241 if (ctx->task && cpuctx->task_ctx != ctx)
242 return;
243
244 spin_lock(&ctx->lock);
245 /*
246 * Protect the list operation against NMI by disabling the
247 * counters on a global level.
248 */
249 perf_disable();
250
251 counter_sched_out(counter, cpuctx, ctx);
252
253 list_del_counter(counter, ctx);
254
255 if (!ctx->task) {
256 /*
257 * Allow more per task counters with respect to the
258 * reservation:
259 */
260 cpuctx->max_pertask =
261 min(perf_max_counters - ctx->nr_counters,
262 perf_max_counters - perf_reserved_percpu);
263 }
264
265 perf_enable();
266 spin_unlock(&ctx->lock);
267}
268
269
270/*
271 * Remove the counter from a task's (or a CPU's) list of counters.
272 *
273 * Must be called with ctx->mutex held.
274 *
275 * CPU counters are removed with a smp call. For task counters we only
276 * call when the task is on a CPU.
277 *
278 * If counter->ctx is a cloned context, callers must make sure that
279 * every task struct that counter->ctx->task could possibly point to
280 * remains valid. This is OK when called from perf_release since
281 * that only calls us on the top-level context, which can't be a clone.
282 * When called from perf_counter_exit_task, it's OK because the
283 * context has been detached from its task.
284 */
285static void perf_counter_remove_from_context(struct perf_counter *counter)
286{
287 struct perf_counter_context *ctx = counter->ctx;
288 struct task_struct *task = ctx->task;
289
290 if (!task) {
291 /*
292 * Per cpu counters are removed via an smp call and
293 * the removal is always sucessful.
294 */
295 smp_call_function_single(counter->cpu,
296 __perf_counter_remove_from_context,
297 counter, 1);
298 return;
299 }
300
301retry:
302 task_oncpu_function_call(task, __perf_counter_remove_from_context,
303 counter);
304
305 spin_lock_irq(&ctx->lock);
306 /*
307 * If the context is active we need to retry the smp call.
308 */
309 if (ctx->nr_active && !list_empty(&counter->list_entry)) {
310 spin_unlock_irq(&ctx->lock);
311 goto retry;
312 }
313
314 /*
315 * The lock prevents that this context is scheduled in so we
316 * can remove the counter safely, if the call above did not
317 * succeed.
318 */
319 if (!list_empty(&counter->list_entry)) {
320 list_del_counter(counter, ctx);
321 }
322 spin_unlock_irq(&ctx->lock);
323}
324
325static inline u64 perf_clock(void)
326{
327 return cpu_clock(smp_processor_id());
328}
329
330/*
331 * Update the record of the current time in a context.
332 */
333static void update_context_time(struct perf_counter_context *ctx)
334{
335 u64 now = perf_clock();
336
337 ctx->time += now - ctx->timestamp;
338 ctx->timestamp = now;
339}
340
341/*
342 * Update the total_time_enabled and total_time_running fields for a counter.
343 */
344static void update_counter_times(struct perf_counter *counter)
345{
346 struct perf_counter_context *ctx = counter->ctx;
347 u64 run_end;
348
349 if (counter->state < PERF_COUNTER_STATE_INACTIVE)
350 return;
351
352 counter->total_time_enabled = ctx->time - counter->tstamp_enabled;
353
354 if (counter->state == PERF_COUNTER_STATE_INACTIVE)
355 run_end = counter->tstamp_stopped;
356 else
357 run_end = ctx->time;
358
359 counter->total_time_running = run_end - counter->tstamp_running;
360}
361
362/*
363 * Update total_time_enabled and total_time_running for all counters in a group.
364 */
365static void update_group_times(struct perf_counter *leader)
366{
367 struct perf_counter *counter;
368
369 update_counter_times(leader);
370 list_for_each_entry(counter, &leader->sibling_list, list_entry)
371 update_counter_times(counter);
372}
373
374/*
375 * Cross CPU call to disable a performance counter
376 */
377static void __perf_counter_disable(void *info)
378{
379 struct perf_counter *counter = info;
380 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
381 struct perf_counter_context *ctx = counter->ctx;
382
383 /*
384 * If this is a per-task counter, need to check whether this
385 * counter's task is the current task on this cpu.
386 */
387 if (ctx->task && cpuctx->task_ctx != ctx)
388 return;
389
390 spin_lock(&ctx->lock);
391
392 /*
393 * If the counter is on, turn it off.
394 * If it is in error state, leave it in error state.
395 */
396 if (counter->state >= PERF_COUNTER_STATE_INACTIVE) {
397 update_context_time(ctx);
398 update_counter_times(counter);
399 if (counter == counter->group_leader)
400 group_sched_out(counter, cpuctx, ctx);
401 else
402 counter_sched_out(counter, cpuctx, ctx);
403 counter->state = PERF_COUNTER_STATE_OFF;
404 }
405
406 spin_unlock(&ctx->lock);
407}
408
409/*
410 * Disable a counter.
411 *
412 * If counter->ctx is a cloned context, callers must make sure that
413 * every task struct that counter->ctx->task could possibly point to
414 * remains valid. This condition is satisifed when called through
415 * perf_counter_for_each_child or perf_counter_for_each because they
416 * hold the top-level counter's child_mutex, so any descendant that
417 * goes to exit will block in sync_child_counter.
418 * When called from perf_pending_counter it's OK because counter->ctx
419 * is the current context on this CPU and preemption is disabled,
420 * hence we can't get into perf_counter_task_sched_out for this context.
421 */
422static void perf_counter_disable(struct perf_counter *counter)
423{
424 struct perf_counter_context *ctx = counter->ctx;
425 struct task_struct *task = ctx->task;
426
427 if (!task) {
428 /*
429 * Disable the counter on the cpu that it's on
430 */
431 smp_call_function_single(counter->cpu, __perf_counter_disable,
432 counter, 1);
433 return;
434 }
435
436 retry:
437 task_oncpu_function_call(task, __perf_counter_disable, counter);
438
439 spin_lock_irq(&ctx->lock);
440 /*
441 * If the counter is still active, we need to retry the cross-call.
442 */
443 if (counter->state == PERF_COUNTER_STATE_ACTIVE) {
444 spin_unlock_irq(&ctx->lock);
445 goto retry;
446 }
447
448 /*
449 * Since we have the lock this context can't be scheduled
450 * in, so we can change the state safely.
451 */
452 if (counter->state == PERF_COUNTER_STATE_INACTIVE) {
453 update_counter_times(counter);
454 counter->state = PERF_COUNTER_STATE_OFF;
455 }
456
457 spin_unlock_irq(&ctx->lock);
458}
459
460static int
461counter_sched_in(struct perf_counter *counter,
462 struct perf_cpu_context *cpuctx,
463 struct perf_counter_context *ctx,
464 int cpu)
465{
466 if (counter->state <= PERF_COUNTER_STATE_OFF)
467 return 0;
468
469 counter->state = PERF_COUNTER_STATE_ACTIVE;
470 counter->oncpu = cpu; /* TODO: put 'cpu' into cpuctx->cpu */
471 /*
472 * The new state must be visible before we turn it on in the hardware:
473 */
474 smp_wmb();
475
476 if (counter->pmu->enable(counter)) {
477 counter->state = PERF_COUNTER_STATE_INACTIVE;
478 counter->oncpu = -1;
479 return -EAGAIN;
480 }
481
482 counter->tstamp_running += ctx->time - counter->tstamp_stopped;
483
484 if (!is_software_counter(counter))
485 cpuctx->active_oncpu++;
486 ctx->nr_active++;
487
488 if (counter->hw_event.exclusive)
489 cpuctx->exclusive = 1;
490
491 return 0;
492}
493
494static int
495group_sched_in(struct perf_counter *group_counter,
496 struct perf_cpu_context *cpuctx,
497 struct perf_counter_context *ctx,
498 int cpu)
499{
500 struct perf_counter *counter, *partial_group;
501 int ret;
502
503 if (group_counter->state == PERF_COUNTER_STATE_OFF)
504 return 0;
505
506 ret = hw_perf_group_sched_in(group_counter, cpuctx, ctx, cpu);
507 if (ret)
508 return ret < 0 ? ret : 0;
509
510 group_counter->prev_state = group_counter->state;
511 if (counter_sched_in(group_counter, cpuctx, ctx, cpu))
512 return -EAGAIN;
513
514 /*
515 * Schedule in siblings as one group (if any):
516 */
517 list_for_each_entry(counter, &group_counter->sibling_list, list_entry) {
518 counter->prev_state = counter->state;
519 if (counter_sched_in(counter, cpuctx, ctx, cpu)) {
520 partial_group = counter;
521 goto group_error;
522 }
523 }
524
525 return 0;
526
527group_error:
528 /*
529 * Groups can be scheduled in as one unit only, so undo any
530 * partial group before returning:
531 */
532 list_for_each_entry(counter, &group_counter->sibling_list, list_entry) {
533 if (counter == partial_group)
534 break;
535 counter_sched_out(counter, cpuctx, ctx);
536 }
537 counter_sched_out(group_counter, cpuctx, ctx);
538
539 return -EAGAIN;
540}
541
542/*
543 * Return 1 for a group consisting entirely of software counters,
544 * 0 if the group contains any hardware counters.
545 */
546static int is_software_only_group(struct perf_counter *leader)
547{
548 struct perf_counter *counter;
549
550 if (!is_software_counter(leader))
551 return 0;
552
553 list_for_each_entry(counter, &leader->sibling_list, list_entry)
554 if (!is_software_counter(counter))
555 return 0;
556
557 return 1;
558}
559
560/*
561 * Work out whether we can put this counter group on the CPU now.
562 */
563static int group_can_go_on(struct perf_counter *counter,
564 struct perf_cpu_context *cpuctx,
565 int can_add_hw)
566{
567 /*
568 * Groups consisting entirely of software counters can always go on.
569 */
570 if (is_software_only_group(counter))
571 return 1;
572 /*
573 * If an exclusive group is already on, no other hardware
574 * counters can go on.
575 */
576 if (cpuctx->exclusive)
577 return 0;
578 /*
579 * If this group is exclusive and there are already
580 * counters on the CPU, it can't go on.
581 */
582 if (counter->hw_event.exclusive && cpuctx->active_oncpu)
583 return 0;
584 /*
585 * Otherwise, try to add it if all previous groups were able
586 * to go on.
587 */
588 return can_add_hw;
589}
590
591static void add_counter_to_ctx(struct perf_counter *counter,
592 struct perf_counter_context *ctx)
593{
594 list_add_counter(counter, ctx);
595 counter->prev_state = PERF_COUNTER_STATE_OFF;
596 counter->tstamp_enabled = ctx->time;
597 counter->tstamp_running = ctx->time;
598 counter->tstamp_stopped = ctx->time;
599}
600
601/*
602 * Cross CPU call to install and enable a performance counter
603 *
604 * Must be called with ctx->mutex held
605 */
606static void __perf_install_in_context(void *info)
607{
608 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
609 struct perf_counter *counter = info;
610 struct perf_counter_context *ctx = counter->ctx;
611 struct perf_counter *leader = counter->group_leader;
612 int cpu = smp_processor_id();
613 int err;
614
615 /*
616 * If this is a task context, we need to check whether it is
617 * the current task context of this cpu. If not it has been
618 * scheduled out before the smp call arrived.
619 * Or possibly this is the right context but it isn't
620 * on this cpu because it had no counters.
621 */
622 if (ctx->task && cpuctx->task_ctx != ctx) {
623 if (cpuctx->task_ctx || ctx->task != current)
624 return;
625 cpuctx->task_ctx = ctx;
626 }
627
628 spin_lock(&ctx->lock);
629 ctx->is_active = 1;
630 update_context_time(ctx);
631
632 /*
633 * Protect the list operation against NMI by disabling the
634 * counters on a global level. NOP for non NMI based counters.
635 */
636 perf_disable();
637
638 add_counter_to_ctx(counter, ctx);
639
640 /*
641 * Don't put the counter on if it is disabled or if
642 * it is in a group and the group isn't on.
643 */
644 if (counter->state != PERF_COUNTER_STATE_INACTIVE ||
645 (leader != counter && leader->state != PERF_COUNTER_STATE_ACTIVE))
646 goto unlock;
647
648 /*
649 * An exclusive counter can't go on if there are already active
650 * hardware counters, and no hardware counter can go on if there
651 * is already an exclusive counter on.
652 */
653 if (!group_can_go_on(counter, cpuctx, 1))
654 err = -EEXIST;
655 else
656 err = counter_sched_in(counter, cpuctx, ctx, cpu);
657
658 if (err) {
659 /*
660 * This counter couldn't go on. If it is in a group
661 * then we have to pull the whole group off.
662 * If the counter group is pinned then put it in error state.
663 */
664 if (leader != counter)
665 group_sched_out(leader, cpuctx, ctx);
666 if (leader->hw_event.pinned) {
667 update_group_times(leader);
668 leader->state = PERF_COUNTER_STATE_ERROR;
669 }
670 }
671
672 if (!err && !ctx->task && cpuctx->max_pertask)
673 cpuctx->max_pertask--;
674
675 unlock:
676 perf_enable();
677
678 spin_unlock(&ctx->lock);
679}
680
681/*
682 * Attach a performance counter to a context
683 *
684 * First we add the counter to the list with the hardware enable bit
685 * in counter->hw_config cleared.
686 *
687 * If the counter is attached to a task which is on a CPU we use a smp
688 * call to enable it in the task context. The task might have been
689 * scheduled away, but we check this in the smp call again.
690 *
691 * Must be called with ctx->mutex held.
692 */
693static void
694perf_install_in_context(struct perf_counter_context *ctx,
695 struct perf_counter *counter,
696 int cpu)
697{
698 struct task_struct *task = ctx->task;
699
700 if (!task) {
701 /*
702 * Per cpu counters are installed via an smp call and
703 * the install is always sucessful.
704 */
705 smp_call_function_single(cpu, __perf_install_in_context,
706 counter, 1);
707 return;
708 }
709
710retry:
711 task_oncpu_function_call(task, __perf_install_in_context,
712 counter);
713
714 spin_lock_irq(&ctx->lock);
715 /*
716 * we need to retry the smp call.
717 */
718 if (ctx->is_active && list_empty(&counter->list_entry)) {
719 spin_unlock_irq(&ctx->lock);
720 goto retry;
721 }
722
723 /*
724 * The lock prevents that this context is scheduled in so we
725 * can add the counter safely, if it the call above did not
726 * succeed.
727 */
728 if (list_empty(&counter->list_entry))
729 add_counter_to_ctx(counter, ctx);
730 spin_unlock_irq(&ctx->lock);
731}
732
733/*
734 * Cross CPU call to enable a performance counter
735 */
736static void __perf_counter_enable(void *info)
737{
738 struct perf_counter *counter = info;
739 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
740 struct perf_counter_context *ctx = counter->ctx;
741 struct perf_counter *leader = counter->group_leader;
742 int err;
743
744 /*
745 * If this is a per-task counter, need to check whether this
746 * counter's task is the current task on this cpu.
747 */
748 if (ctx->task && cpuctx->task_ctx != ctx) {
749 if (cpuctx->task_ctx || ctx->task != current)
750 return;
751 cpuctx->task_ctx = ctx;
752 }
753
754 spin_lock(&ctx->lock);
755 ctx->is_active = 1;
756 update_context_time(ctx);
757
758 counter->prev_state = counter->state;
759 if (counter->state >= PERF_COUNTER_STATE_INACTIVE)
760 goto unlock;
761 counter->state = PERF_COUNTER_STATE_INACTIVE;
762 counter->tstamp_enabled = ctx->time - counter->total_time_enabled;
763
764 /*
765 * If the counter is in a group and isn't the group leader,
766 * then don't put it on unless the group is on.
767 */
768 if (leader != counter && leader->state != PERF_COUNTER_STATE_ACTIVE)
769 goto unlock;
770
771 if (!group_can_go_on(counter, cpuctx, 1)) {
772 err = -EEXIST;
773 } else {
774 perf_disable();
775 if (counter == leader)
776 err = group_sched_in(counter, cpuctx, ctx,
777 smp_processor_id());
778 else
779 err = counter_sched_in(counter, cpuctx, ctx,
780 smp_processor_id());
781 perf_enable();
782 }
783
784 if (err) {
785 /*
786 * If this counter can't go on and it's part of a
787 * group, then the whole group has to come off.
788 */
789 if (leader != counter)
790 group_sched_out(leader, cpuctx, ctx);
791 if (leader->hw_event.pinned) {
792 update_group_times(leader);
793 leader->state = PERF_COUNTER_STATE_ERROR;
794 }
795 }
796
797 unlock:
798 spin_unlock(&ctx->lock);
799}
800
801/*
802 * Enable a counter.
803 *
804 * If counter->ctx is a cloned context, callers must make sure that
805 * every task struct that counter->ctx->task could possibly point to
806 * remains valid. This condition is satisfied when called through
807 * perf_counter_for_each_child or perf_counter_for_each as described
808 * for perf_counter_disable.
809 */
810static void perf_counter_enable(struct perf_counter *counter)
811{
812 struct perf_counter_context *ctx = counter->ctx;
813 struct task_struct *task = ctx->task;
814
815 if (!task) {
816 /*
817 * Enable the counter on the cpu that it's on
818 */
819 smp_call_function_single(counter->cpu, __perf_counter_enable,
820 counter, 1);
821 return;
822 }
823
824 spin_lock_irq(&ctx->lock);
825 if (counter->state >= PERF_COUNTER_STATE_INACTIVE)
826 goto out;
827
828 /*
829 * If the counter is in error state, clear that first.
830 * That way, if we see the counter in error state below, we
831 * know that it has gone back into error state, as distinct
832 * from the task having been scheduled away before the
833 * cross-call arrived.
834 */
835 if (counter->state == PERF_COUNTER_STATE_ERROR)
836 counter->state = PERF_COUNTER_STATE_OFF;
837
838 retry:
839 spin_unlock_irq(&ctx->lock);
840 task_oncpu_function_call(task, __perf_counter_enable, counter);
841
842 spin_lock_irq(&ctx->lock);
843
844 /*
845 * If the context is active and the counter is still off,
846 * we need to retry the cross-call.
847 */
848 if (ctx->is_active && counter->state == PERF_COUNTER_STATE_OFF)
849 goto retry;
850
851 /*
852 * Since we have the lock this context can't be scheduled
853 * in, so we can change the state safely.
854 */
855 if (counter->state == PERF_COUNTER_STATE_OFF) {
856 counter->state = PERF_COUNTER_STATE_INACTIVE;
857 counter->tstamp_enabled =
858 ctx->time - counter->total_time_enabled;
859 }
860 out:
861 spin_unlock_irq(&ctx->lock);
862}
863
864static int perf_counter_refresh(struct perf_counter *counter, int refresh)
865{
866 /*
867 * not supported on inherited counters
868 */
869 if (counter->hw_event.inherit)
870 return -EINVAL;
871
872 atomic_add(refresh, &counter->event_limit);
873 perf_counter_enable(counter);
874
875 return 0;
876}
877
878void __perf_counter_sched_out(struct perf_counter_context *ctx,
879 struct perf_cpu_context *cpuctx)
880{
881 struct perf_counter *counter;
882
883 spin_lock(&ctx->lock);
884 ctx->is_active = 0;
885 if (likely(!ctx->nr_counters))
886 goto out;
887 update_context_time(ctx);
888
889 perf_disable();
890 if (ctx->nr_active) {
891 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
892 if (counter != counter->group_leader)
893 counter_sched_out(counter, cpuctx, ctx);
894 else
895 group_sched_out(counter, cpuctx, ctx);
896 }
897 }
898 perf_enable();
899 out:
900 spin_unlock(&ctx->lock);
901}
902
903/*
904 * Test whether two contexts are equivalent, i.e. whether they
905 * have both been cloned from the same version of the same context
906 * and they both have the same number of enabled counters.
907 * If the number of enabled counters is the same, then the set
908 * of enabled counters should be the same, because these are both
909 * inherited contexts, therefore we can't access individual counters
910 * in them directly with an fd; we can only enable/disable all
911 * counters via prctl, or enable/disable all counters in a family
912 * via ioctl, which will have the same effect on both contexts.
913 */
914static int context_equiv(struct perf_counter_context *ctx1,
915 struct perf_counter_context *ctx2)
916{
917 return ctx1->parent_ctx && ctx1->parent_ctx == ctx2->parent_ctx
918 && ctx1->parent_gen == ctx2->parent_gen
919 && ctx1->parent_gen != ~0ull;
920}
921
922/*
923 * Called from scheduler to remove the counters of the current task,
924 * with interrupts disabled.
925 *
926 * We stop each counter and update the counter value in counter->count.
927 *
928 * This does not protect us against NMI, but disable()
929 * sets the disabled bit in the control field of counter _before_
930 * accessing the counter control register. If a NMI hits, then it will
931 * not restart the counter.
932 */
933void perf_counter_task_sched_out(struct task_struct *task,
934 struct task_struct *next, int cpu)
935{
936 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
937 struct perf_counter_context *ctx = task->perf_counter_ctxp;
938 struct perf_counter_context *next_ctx;
939 struct perf_counter_context *parent;
940 struct pt_regs *regs;
941 int do_switch = 1;
942
943 regs = task_pt_regs(task);
944 perf_swcounter_event(PERF_COUNT_CONTEXT_SWITCHES, 1, 1, regs, 0);
945
946 if (likely(!ctx || !cpuctx->task_ctx))
947 return;
948
949 update_context_time(ctx);
950
951 rcu_read_lock();
952 parent = rcu_dereference(ctx->parent_ctx);
953 next_ctx = next->perf_counter_ctxp;
954 if (parent && next_ctx &&
955 rcu_dereference(next_ctx->parent_ctx) == parent) {
956 /*
957 * Looks like the two contexts are clones, so we might be
958 * able to optimize the context switch. We lock both
959 * contexts and check that they are clones under the
960 * lock (including re-checking that neither has been
961 * uncloned in the meantime). It doesn't matter which
962 * order we take the locks because no other cpu could
963 * be trying to lock both of these tasks.
964 */
965 spin_lock(&ctx->lock);
966 spin_lock_nested(&next_ctx->lock, SINGLE_DEPTH_NESTING);
967 if (context_equiv(ctx, next_ctx)) {
968 /*
969 * XXX do we need a memory barrier of sorts
970 * wrt to rcu_dereference() of perf_counter_ctxp
971 */
972 task->perf_counter_ctxp = next_ctx;
973 next->perf_counter_ctxp = ctx;
974 ctx->task = next;
975 next_ctx->task = task;
976 do_switch = 0;
977 }
978 spin_unlock(&next_ctx->lock);
979 spin_unlock(&ctx->lock);
980 }
981 rcu_read_unlock();
982
983 if (do_switch) {
984 __perf_counter_sched_out(ctx, cpuctx);
985 cpuctx->task_ctx = NULL;
986 }
987}
988
989/*
990 * Called with IRQs disabled
991 */
992static void __perf_counter_task_sched_out(struct perf_counter_context *ctx)
993{
994 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
995
996 if (!cpuctx->task_ctx)
997 return;
998
999 if (WARN_ON_ONCE(ctx != cpuctx->task_ctx))
1000 return;
1001
1002 __perf_counter_sched_out(ctx, cpuctx);
1003 cpuctx->task_ctx = NULL;
1004}
1005
1006/*
1007 * Called with IRQs disabled
1008 */
1009static void perf_counter_cpu_sched_out(struct perf_cpu_context *cpuctx)
1010{
1011 __perf_counter_sched_out(&cpuctx->ctx, cpuctx);
1012}
1013
1014static void
1015__perf_counter_sched_in(struct perf_counter_context *ctx,
1016 struct perf_cpu_context *cpuctx, int cpu)
1017{
1018 struct perf_counter *counter;
1019 int can_add_hw = 1;
1020
1021 spin_lock(&ctx->lock);
1022 ctx->is_active = 1;
1023 if (likely(!ctx->nr_counters))
1024 goto out;
1025
1026 ctx->timestamp = perf_clock();
1027
1028 perf_disable();
1029
1030 /*
1031 * First go through the list and put on any pinned groups
1032 * in order to give them the best chance of going on.
1033 */
1034 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
1035 if (counter->state <= PERF_COUNTER_STATE_OFF ||
1036 !counter->hw_event.pinned)
1037 continue;
1038 if (counter->cpu != -1 && counter->cpu != cpu)
1039 continue;
1040
1041 if (counter != counter->group_leader)
1042 counter_sched_in(counter, cpuctx, ctx, cpu);
1043 else {
1044 if (group_can_go_on(counter, cpuctx, 1))
1045 group_sched_in(counter, cpuctx, ctx, cpu);
1046 }
1047
1048 /*
1049 * If this pinned group hasn't been scheduled,
1050 * put it in error state.
1051 */
1052 if (counter->state == PERF_COUNTER_STATE_INACTIVE) {
1053 update_group_times(counter);
1054 counter->state = PERF_COUNTER_STATE_ERROR;
1055 }
1056 }
1057
1058 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
1059 /*
1060 * Ignore counters in OFF or ERROR state, and
1061 * ignore pinned counters since we did them already.
1062 */
1063 if (counter->state <= PERF_COUNTER_STATE_OFF ||
1064 counter->hw_event.pinned)
1065 continue;
1066
1067 /*
1068 * Listen to the 'cpu' scheduling filter constraint
1069 * of counters:
1070 */
1071 if (counter->cpu != -1 && counter->cpu != cpu)
1072 continue;
1073
1074 if (counter != counter->group_leader) {
1075 if (counter_sched_in(counter, cpuctx, ctx, cpu))
1076 can_add_hw = 0;
1077 } else {
1078 if (group_can_go_on(counter, cpuctx, can_add_hw)) {
1079 if (group_sched_in(counter, cpuctx, ctx, cpu))
1080 can_add_hw = 0;
1081 }
1082 }
1083 }
1084 perf_enable();
1085 out:
1086 spin_unlock(&ctx->lock);
1087}
1088
1089/*
1090 * Called from scheduler to add the counters of the current task
1091 * with interrupts disabled.
1092 *
1093 * We restore the counter value and then enable it.
1094 *
1095 * This does not protect us against NMI, but enable()
1096 * sets the enabled bit in the control field of counter _before_
1097 * accessing the counter control register. If a NMI hits, then it will
1098 * keep the counter running.
1099 */
1100void perf_counter_task_sched_in(struct task_struct *task, int cpu)
1101{
1102 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
1103 struct perf_counter_context *ctx = task->perf_counter_ctxp;
1104
1105 if (likely(!ctx))
1106 return;
1107 if (cpuctx->task_ctx == ctx)
1108 return;
1109 __perf_counter_sched_in(ctx, cpuctx, cpu);
1110 cpuctx->task_ctx = ctx;
1111}
1112
1113static void perf_counter_cpu_sched_in(struct perf_cpu_context *cpuctx, int cpu)
1114{
1115 struct perf_counter_context *ctx = &cpuctx->ctx;
1116
1117 __perf_counter_sched_in(ctx, cpuctx, cpu);
1118}
1119
1120#define MAX_INTERRUPTS (~0ULL)
1121
1122static void perf_log_throttle(struct perf_counter *counter, int enable);
1123static void perf_log_period(struct perf_counter *counter, u64 period);
1124
1125static void perf_adjust_freq(struct perf_counter_context *ctx)
1126{
1127 struct perf_counter *counter;
1128 u64 interrupts, irq_period;
1129 u64 events, period;
1130 s64 delta;
1131
1132 spin_lock(&ctx->lock);
1133 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
1134 if (counter->state != PERF_COUNTER_STATE_ACTIVE)
1135 continue;
1136
1137 interrupts = counter->hw.interrupts;
1138 counter->hw.interrupts = 0;
1139
1140 if (interrupts == MAX_INTERRUPTS) {
1141 perf_log_throttle(counter, 1);
1142 counter->pmu->unthrottle(counter);
1143 interrupts = 2*sysctl_perf_counter_limit/HZ;
1144 }
1145
1146 if (!counter->hw_event.freq || !counter->hw_event.irq_freq)
1147 continue;
1148
1149 events = HZ * interrupts * counter->hw.irq_period;
1150 period = div64_u64(events, counter->hw_event.irq_freq);
1151
1152 delta = (s64)(1 + period - counter->hw.irq_period);
1153 delta >>= 1;
1154
1155 irq_period = counter->hw.irq_period + delta;
1156
1157 if (!irq_period)
1158 irq_period = 1;
1159
1160 perf_log_period(counter, irq_period);
1161
1162 counter->hw.irq_period = irq_period;
1163 }
1164 spin_unlock(&ctx->lock);
1165}
1166
1167/*
1168 * Round-robin a context's counters:
1169 */
1170static void rotate_ctx(struct perf_counter_context *ctx)
1171{
1172 struct perf_counter *counter;
1173
1174 if (!ctx->nr_counters)
1175 return;
1176
1177 spin_lock(&ctx->lock);
1178 /*
1179 * Rotate the first entry last (works just fine for group counters too):
1180 */
1181 perf_disable();
1182 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
1183 list_move_tail(&counter->list_entry, &ctx->counter_list);
1184 break;
1185 }
1186 perf_enable();
1187
1188 spin_unlock(&ctx->lock);
1189}
1190
1191void perf_counter_task_tick(struct task_struct *curr, int cpu)
1192{
1193 struct perf_cpu_context *cpuctx;
1194 struct perf_counter_context *ctx;
1195
1196 if (!atomic_read(&nr_counters))
1197 return;
1198
1199 cpuctx = &per_cpu(perf_cpu_context, cpu);
1200 ctx = curr->perf_counter_ctxp;
1201
1202 perf_adjust_freq(&cpuctx->ctx);
1203 if (ctx)
1204 perf_adjust_freq(ctx);
1205
1206 perf_counter_cpu_sched_out(cpuctx);
1207 if (ctx)
1208 __perf_counter_task_sched_out(ctx);
1209
1210 rotate_ctx(&cpuctx->ctx);
1211 if (ctx)
1212 rotate_ctx(ctx);
1213
1214 perf_counter_cpu_sched_in(cpuctx, cpu);
1215 if (ctx)
1216 perf_counter_task_sched_in(curr, cpu);
1217}
1218
1219/*
1220 * Cross CPU call to read the hardware counter
1221 */
1222static void __read(void *info)
1223{
1224 struct perf_counter *counter = info;
1225 struct perf_counter_context *ctx = counter->ctx;
1226 unsigned long flags;
1227
1228 local_irq_save(flags);
1229 if (ctx->is_active)
1230 update_context_time(ctx);
1231 counter->pmu->read(counter);
1232 update_counter_times(counter);
1233 local_irq_restore(flags);
1234}
1235
1236static u64 perf_counter_read(struct perf_counter *counter)
1237{
1238 /*
1239 * If counter is enabled and currently active on a CPU, update the
1240 * value in the counter structure:
1241 */
1242 if (counter->state == PERF_COUNTER_STATE_ACTIVE) {
1243 smp_call_function_single(counter->oncpu,
1244 __read, counter, 1);
1245 } else if (counter->state == PERF_COUNTER_STATE_INACTIVE) {
1246 update_counter_times(counter);
1247 }
1248
1249 return atomic64_read(&counter->count);
1250}
1251
1252/*
1253 * Initialize the perf_counter context in a task_struct:
1254 */
1255static void
1256__perf_counter_init_context(struct perf_counter_context *ctx,
1257 struct task_struct *task)
1258{
1259 memset(ctx, 0, sizeof(*ctx));
1260 spin_lock_init(&ctx->lock);
1261 mutex_init(&ctx->mutex);
1262 INIT_LIST_HEAD(&ctx->counter_list);
1263 INIT_LIST_HEAD(&ctx->event_list);
1264 atomic_set(&ctx->refcount, 1);
1265 ctx->task = task;
1266}
1267
1268static struct perf_counter_context *find_get_context(pid_t pid, int cpu)
1269{
1270 struct perf_cpu_context *cpuctx;
1271 struct perf_counter_context *ctx;
1272 struct perf_counter_context *parent_ctx;
1273 struct task_struct *task;
1274 int err;
1275
1276 /*
1277 * If cpu is not a wildcard then this is a percpu counter:
1278 */
1279 if (cpu != -1) {
1280 /* Must be root to operate on a CPU counter: */
1281 if (sysctl_perf_counter_priv && !capable(CAP_SYS_ADMIN))
1282 return ERR_PTR(-EACCES);
1283
1284 if (cpu < 0 || cpu > num_possible_cpus())
1285 return ERR_PTR(-EINVAL);
1286
1287 /*
1288 * We could be clever and allow to attach a counter to an
1289 * offline CPU and activate it when the CPU comes up, but
1290 * that's for later.
1291 */
1292 if (!cpu_isset(cpu, cpu_online_map))
1293 return ERR_PTR(-ENODEV);
1294
1295 cpuctx = &per_cpu(perf_cpu_context, cpu);
1296 ctx = &cpuctx->ctx;
1297 get_ctx(ctx);
1298
1299 return ctx;
1300 }
1301
1302 rcu_read_lock();
1303 if (!pid)
1304 task = current;
1305 else
1306 task = find_task_by_vpid(pid);
1307 if (task)
1308 get_task_struct(task);
1309 rcu_read_unlock();
1310
1311 if (!task)
1312 return ERR_PTR(-ESRCH);
1313
1314 /*
1315 * Can't attach counters to a dying task.
1316 */
1317 err = -ESRCH;
1318 if (task->flags & PF_EXITING)
1319 goto errout;
1320
1321 /* Reuse ptrace permission checks for now. */
1322 err = -EACCES;
1323 if (!ptrace_may_access(task, PTRACE_MODE_READ))
1324 goto errout;
1325
1326 retry_lock:
1327 rcu_read_lock();
1328 retry:
1329 ctx = rcu_dereference(task->perf_counter_ctxp);
1330 if (ctx) {
1331 /*
1332 * If this context is a clone of another, it might
1333 * get swapped for another underneath us by
1334 * perf_counter_task_sched_out, though the
1335 * rcu_read_lock() protects us from any context
1336 * getting freed. Lock the context and check if it
1337 * got swapped before we could get the lock, and retry
1338 * if so. If we locked the right context, then it
1339 * can't get swapped on us any more and we can
1340 * unclone it if necessary.
1341 * Once it's not a clone things will be stable.
1342 */
1343 spin_lock_irq(&ctx->lock);
1344 if (ctx != rcu_dereference(task->perf_counter_ctxp)) {
1345 spin_unlock_irq(&ctx->lock);
1346 goto retry;
1347 }
1348 parent_ctx = ctx->parent_ctx;
1349 if (parent_ctx) {
1350 put_ctx(parent_ctx);
1351 ctx->parent_ctx = NULL; /* no longer a clone */
1352 }
1353 /*
1354 * Get an extra reference before dropping the lock so that
1355 * this context won't get freed if the task exits.
1356 */
1357 get_ctx(ctx);
1358 spin_unlock_irq(&ctx->lock);
1359 }
1360 rcu_read_unlock();
1361
1362 if (!ctx) {
1363 ctx = kmalloc(sizeof(struct perf_counter_context), GFP_KERNEL);
1364 err = -ENOMEM;
1365 if (!ctx)
1366 goto errout;
1367 __perf_counter_init_context(ctx, task);
1368 get_ctx(ctx);
1369 if (cmpxchg(&task->perf_counter_ctxp, NULL, ctx)) {
1370 /*
1371 * We raced with some other task; use
1372 * the context they set.
1373 */
1374 kfree(ctx);
1375 goto retry_lock;
1376 }
1377 get_task_struct(task);
1378 }
1379
1380 put_task_struct(task);
1381 return ctx;
1382
1383 errout:
1384 put_task_struct(task);
1385 return ERR_PTR(err);
1386}
1387
1388static void free_counter_rcu(struct rcu_head *head)
1389{
1390 struct perf_counter *counter;
1391
1392 counter = container_of(head, struct perf_counter, rcu_head);
1393 kfree(counter);
1394}
1395
1396static void perf_pending_sync(struct perf_counter *counter);
1397
1398static void free_counter(struct perf_counter *counter)
1399{
1400 perf_pending_sync(counter);
1401
1402 atomic_dec(&nr_counters);
1403 if (counter->hw_event.mmap)
1404 atomic_dec(&nr_mmap_tracking);
1405 if (counter->hw_event.munmap)
1406 atomic_dec(&nr_munmap_tracking);
1407 if (counter->hw_event.comm)
1408 atomic_dec(&nr_comm_tracking);
1409
1410 if (counter->destroy)
1411 counter->destroy(counter);
1412
1413 put_ctx(counter->ctx);
1414 call_rcu(&counter->rcu_head, free_counter_rcu);
1415}
1416
1417/*
1418 * Called when the last reference to the file is gone.
1419 */
1420static int perf_release(struct inode *inode, struct file *file)
1421{
1422 struct perf_counter *counter = file->private_data;
1423 struct perf_counter_context *ctx = counter->ctx;
1424
1425 file->private_data = NULL;
1426
1427 WARN_ON_ONCE(ctx->parent_ctx);
1428 mutex_lock(&ctx->mutex);
1429 perf_counter_remove_from_context(counter);
1430 mutex_unlock(&ctx->mutex);
1431
1432 mutex_lock(&counter->owner->perf_counter_mutex);
1433 list_del_init(&counter->owner_entry);
1434 mutex_unlock(&counter->owner->perf_counter_mutex);
1435 put_task_struct(counter->owner);
1436
1437 free_counter(counter);
1438
1439 return 0;
1440}
1441
1442/*
1443 * Read the performance counter - simple non blocking version for now
1444 */
1445static ssize_t
1446perf_read_hw(struct perf_counter *counter, char __user *buf, size_t count)
1447{
1448 u64 values[3];
1449 int n;
1450
1451 /*
1452 * Return end-of-file for a read on a counter that is in
1453 * error state (i.e. because it was pinned but it couldn't be
1454 * scheduled on to the CPU at some point).
1455 */
1456 if (counter->state == PERF_COUNTER_STATE_ERROR)
1457 return 0;
1458
1459 WARN_ON_ONCE(counter->ctx->parent_ctx);
1460 mutex_lock(&counter->child_mutex);
1461 values[0] = perf_counter_read(counter);
1462 n = 1;
1463 if (counter->hw_event.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
1464 values[n++] = counter->total_time_enabled +
1465 atomic64_read(&counter->child_total_time_enabled);
1466 if (counter->hw_event.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
1467 values[n++] = counter->total_time_running +
1468 atomic64_read(&counter->child_total_time_running);
1469 mutex_unlock(&counter->child_mutex);
1470
1471 if (count < n * sizeof(u64))
1472 return -EINVAL;
1473 count = n * sizeof(u64);
1474
1475 if (copy_to_user(buf, values, count))
1476 return -EFAULT;
1477
1478 return count;
1479}
1480
1481static ssize_t
1482perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
1483{
1484 struct perf_counter *counter = file->private_data;
1485
1486 return perf_read_hw(counter, buf, count);
1487}
1488
1489static unsigned int perf_poll(struct file *file, poll_table *wait)
1490{
1491 struct perf_counter *counter = file->private_data;
1492 struct perf_mmap_data *data;
1493 unsigned int events = POLL_HUP;
1494
1495 rcu_read_lock();
1496 data = rcu_dereference(counter->data);
1497 if (data)
1498 events = atomic_xchg(&data->poll, 0);
1499 rcu_read_unlock();
1500
1501 poll_wait(file, &counter->waitq, wait);
1502
1503 return events;
1504}
1505
1506static void perf_counter_reset(struct perf_counter *counter)
1507{
1508 (void)perf_counter_read(counter);
1509 atomic64_set(&counter->count, 0);
1510 perf_counter_update_userpage(counter);
1511}
1512
1513static void perf_counter_for_each_sibling(struct perf_counter *counter,
1514 void (*func)(struct perf_counter *))
1515{
1516 struct perf_counter_context *ctx = counter->ctx;
1517 struct perf_counter *sibling;
1518
1519 WARN_ON_ONCE(ctx->parent_ctx);
1520 mutex_lock(&ctx->mutex);
1521 counter = counter->group_leader;
1522
1523 func(counter);
1524 list_for_each_entry(sibling, &counter->sibling_list, list_entry)
1525 func(sibling);
1526 mutex_unlock(&ctx->mutex);
1527}
1528
1529/*
1530 * Holding the top-level counter's child_mutex means that any
1531 * descendant process that has inherited this counter will block
1532 * in sync_child_counter if it goes to exit, thus satisfying the
1533 * task existence requirements of perf_counter_enable/disable.
1534 */
1535static void perf_counter_for_each_child(struct perf_counter *counter,
1536 void (*func)(struct perf_counter *))
1537{
1538 struct perf_counter *child;
1539
1540 WARN_ON_ONCE(counter->ctx->parent_ctx);
1541 mutex_lock(&counter->child_mutex);
1542 func(counter);
1543 list_for_each_entry(child, &counter->child_list, child_list)
1544 func(child);
1545 mutex_unlock(&counter->child_mutex);
1546}
1547
1548static void perf_counter_for_each(struct perf_counter *counter,
1549 void (*func)(struct perf_counter *))
1550{
1551 struct perf_counter *child;
1552
1553 WARN_ON_ONCE(counter->ctx->parent_ctx);
1554 mutex_lock(&counter->child_mutex);
1555 perf_counter_for_each_sibling(counter, func);
1556 list_for_each_entry(child, &counter->child_list, child_list)
1557 perf_counter_for_each_sibling(child, func);
1558 mutex_unlock(&counter->child_mutex);
1559}
1560
1561static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1562{
1563 struct perf_counter *counter = file->private_data;
1564 void (*func)(struct perf_counter *);
1565 u32 flags = arg;
1566
1567 switch (cmd) {
1568 case PERF_COUNTER_IOC_ENABLE:
1569 func = perf_counter_enable;
1570 break;
1571 case PERF_COUNTER_IOC_DISABLE:
1572 func = perf_counter_disable;
1573 break;
1574 case PERF_COUNTER_IOC_RESET:
1575 func = perf_counter_reset;
1576 break;
1577
1578 case PERF_COUNTER_IOC_REFRESH:
1579 return perf_counter_refresh(counter, arg);
1580 default:
1581 return -ENOTTY;
1582 }
1583
1584 if (flags & PERF_IOC_FLAG_GROUP)
1585 perf_counter_for_each(counter, func);
1586 else
1587 perf_counter_for_each_child(counter, func);
1588
1589 return 0;
1590}
1591
1592int perf_counter_task_enable(void)
1593{
1594 struct perf_counter *counter;
1595
1596 mutex_lock(&current->perf_counter_mutex);
1597 list_for_each_entry(counter, &current->perf_counter_list, owner_entry)
1598 perf_counter_for_each_child(counter, perf_counter_enable);
1599 mutex_unlock(&current->perf_counter_mutex);
1600
1601 return 0;
1602}
1603
1604int perf_counter_task_disable(void)
1605{
1606 struct perf_counter *counter;
1607
1608 mutex_lock(&current->perf_counter_mutex);
1609 list_for_each_entry(counter, &current->perf_counter_list, owner_entry)
1610 perf_counter_for_each_child(counter, perf_counter_disable);
1611 mutex_unlock(&current->perf_counter_mutex);
1612
1613 return 0;
1614}
1615
1616/*
1617 * Callers need to ensure there can be no nesting of this function, otherwise
1618 * the seqlock logic goes bad. We can not serialize this because the arch
1619 * code calls this from NMI context.
1620 */
1621void perf_counter_update_userpage(struct perf_counter *counter)
1622{
1623 struct perf_mmap_data *data;
1624 struct perf_counter_mmap_page *userpg;
1625
1626 rcu_read_lock();
1627 data = rcu_dereference(counter->data);
1628 if (!data)
1629 goto unlock;
1630
1631 userpg = data->user_page;
1632
1633 /*
1634 * Disable preemption so as to not let the corresponding user-space
1635 * spin too long if we get preempted.
1636 */
1637 preempt_disable();
1638 ++userpg->lock;
1639 barrier();
1640 userpg->index = counter->hw.idx;
1641 userpg->offset = atomic64_read(&counter->count);
1642 if (counter->state == PERF_COUNTER_STATE_ACTIVE)
1643 userpg->offset -= atomic64_read(&counter->hw.prev_count);
1644
1645 barrier();
1646 ++userpg->lock;
1647 preempt_enable();
1648unlock:
1649 rcu_read_unlock();
1650}
1651
1652static int perf_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1653{
1654 struct perf_counter *counter = vma->vm_file->private_data;
1655 struct perf_mmap_data *data;
1656 int ret = VM_FAULT_SIGBUS;
1657
1658 rcu_read_lock();
1659 data = rcu_dereference(counter->data);
1660 if (!data)
1661 goto unlock;
1662
1663 if (vmf->pgoff == 0) {
1664 vmf->page = virt_to_page(data->user_page);
1665 } else {
1666 int nr = vmf->pgoff - 1;
1667
1668 if ((unsigned)nr > data->nr_pages)
1669 goto unlock;
1670
1671 vmf->page = virt_to_page(data->data_pages[nr]);
1672 }
1673 get_page(vmf->page);
1674 ret = 0;
1675unlock:
1676 rcu_read_unlock();
1677
1678 return ret;
1679}
1680
1681static int perf_mmap_data_alloc(struct perf_counter *counter, int nr_pages)
1682{
1683 struct perf_mmap_data *data;
1684 unsigned long size;
1685 int i;
1686
1687 WARN_ON(atomic_read(&counter->mmap_count));
1688
1689 size = sizeof(struct perf_mmap_data);
1690 size += nr_pages * sizeof(void *);
1691
1692 data = kzalloc(size, GFP_KERNEL);
1693 if (!data)
1694 goto fail;
1695
1696 data->user_page = (void *)get_zeroed_page(GFP_KERNEL);
1697 if (!data->user_page)
1698 goto fail_user_page;
1699
1700 for (i = 0; i < nr_pages; i++) {
1701 data->data_pages[i] = (void *)get_zeroed_page(GFP_KERNEL);
1702 if (!data->data_pages[i])
1703 goto fail_data_pages;
1704 }
1705
1706 data->nr_pages = nr_pages;
1707 atomic_set(&data->lock, -1);
1708
1709 rcu_assign_pointer(counter->data, data);
1710
1711 return 0;
1712
1713fail_data_pages:
1714 for (i--; i >= 0; i--)
1715 free_page((unsigned long)data->data_pages[i]);
1716
1717 free_page((unsigned long)data->user_page);
1718
1719fail_user_page:
1720 kfree(data);
1721
1722fail:
1723 return -ENOMEM;
1724}
1725
1726static void __perf_mmap_data_free(struct rcu_head *rcu_head)
1727{
1728 struct perf_mmap_data *data = container_of(rcu_head,
1729 struct perf_mmap_data, rcu_head);
1730 int i;
1731
1732 free_page((unsigned long)data->user_page);
1733 for (i = 0; i < data->nr_pages; i++)
1734 free_page((unsigned long)data->data_pages[i]);
1735 kfree(data);
1736}
1737
1738static void perf_mmap_data_free(struct perf_counter *counter)
1739{
1740 struct perf_mmap_data *data = counter->data;
1741
1742 WARN_ON(atomic_read(&counter->mmap_count));
1743
1744 rcu_assign_pointer(counter->data, NULL);
1745 call_rcu(&data->rcu_head, __perf_mmap_data_free);
1746}
1747
1748static void perf_mmap_open(struct vm_area_struct *vma)
1749{
1750 struct perf_counter *counter = vma->vm_file->private_data;
1751
1752 atomic_inc(&counter->mmap_count);
1753}
1754
1755static void perf_mmap_close(struct vm_area_struct *vma)
1756{
1757 struct perf_counter *counter = vma->vm_file->private_data;
1758
1759 WARN_ON_ONCE(counter->ctx->parent_ctx);
1760 if (atomic_dec_and_mutex_lock(&counter->mmap_count,
1761 &counter->mmap_mutex)) {
1762 struct user_struct *user = current_user();
1763
1764 atomic_long_sub(counter->data->nr_pages + 1, &user->locked_vm);
1765 vma->vm_mm->locked_vm -= counter->data->nr_locked;
1766 perf_mmap_data_free(counter);
1767 mutex_unlock(&counter->mmap_mutex);
1768 }
1769}
1770
1771static struct vm_operations_struct perf_mmap_vmops = {
1772 .open = perf_mmap_open,
1773 .close = perf_mmap_close,
1774 .fault = perf_mmap_fault,
1775};
1776
1777static int perf_mmap(struct file *file, struct vm_area_struct *vma)
1778{
1779 struct perf_counter *counter = file->private_data;
1780 struct user_struct *user = current_user();
1781 unsigned long vma_size;
1782 unsigned long nr_pages;
1783 unsigned long user_locked, user_lock_limit;
1784 unsigned long locked, lock_limit;
1785 long user_extra, extra;
1786 int ret = 0;
1787
1788 if (!(vma->vm_flags & VM_SHARED) || (vma->vm_flags & VM_WRITE))
1789 return -EINVAL;
1790
1791 vma_size = vma->vm_end - vma->vm_start;
1792 nr_pages = (vma_size / PAGE_SIZE) - 1;
1793
1794 /*
1795 * If we have data pages ensure they're a power-of-two number, so we
1796 * can do bitmasks instead of modulo.
1797 */
1798 if (nr_pages != 0 && !is_power_of_2(nr_pages))
1799 return -EINVAL;
1800
1801 if (vma_size != PAGE_SIZE * (1 + nr_pages))
1802 return -EINVAL;
1803
1804 if (vma->vm_pgoff != 0)
1805 return -EINVAL;
1806
1807 WARN_ON_ONCE(counter->ctx->parent_ctx);
1808 mutex_lock(&counter->mmap_mutex);
1809 if (atomic_inc_not_zero(&counter->mmap_count)) {
1810 if (nr_pages != counter->data->nr_pages)
1811 ret = -EINVAL;
1812 goto unlock;
1813 }
1814
1815 user_extra = nr_pages + 1;
1816 user_lock_limit = sysctl_perf_counter_mlock >> (PAGE_SHIFT - 10);
1817
1818 /*
1819 * Increase the limit linearly with more CPUs:
1820 */
1821 user_lock_limit *= num_online_cpus();
1822
1823 user_locked = atomic_long_read(&user->locked_vm) + user_extra;
1824
1825 extra = 0;
1826 if (user_locked > user_lock_limit)
1827 extra = user_locked - user_lock_limit;
1828
1829 lock_limit = current->signal->rlim[RLIMIT_MEMLOCK].rlim_cur;
1830 lock_limit >>= PAGE_SHIFT;
1831 locked = vma->vm_mm->locked_vm + extra;
1832
1833 if ((locked > lock_limit) && !capable(CAP_IPC_LOCK)) {
1834 ret = -EPERM;
1835 goto unlock;
1836 }
1837
1838 WARN_ON(counter->data);
1839 ret = perf_mmap_data_alloc(counter, nr_pages);
1840 if (ret)
1841 goto unlock;
1842
1843 atomic_set(&counter->mmap_count, 1);
1844 atomic_long_add(user_extra, &user->locked_vm);
1845 vma->vm_mm->locked_vm += extra;
1846 counter->data->nr_locked = extra;
1847unlock:
1848 mutex_unlock(&counter->mmap_mutex);
1849
1850 vma->vm_flags &= ~VM_MAYWRITE;
1851 vma->vm_flags |= VM_RESERVED;
1852 vma->vm_ops = &perf_mmap_vmops;
1853
1854 return ret;
1855}
1856
1857static int perf_fasync(int fd, struct file *filp, int on)
1858{
1859 struct perf_counter *counter = filp->private_data;
1860 struct inode *inode = filp->f_path.dentry->d_inode;
1861 int retval;
1862
1863 mutex_lock(&inode->i_mutex);
1864 retval = fasync_helper(fd, filp, on, &counter->fasync);
1865 mutex_unlock(&inode->i_mutex);
1866
1867 if (retval < 0)
1868 return retval;
1869
1870 return 0;
1871}
1872
1873static const struct file_operations perf_fops = {
1874 .release = perf_release,
1875 .read = perf_read,
1876 .poll = perf_poll,
1877 .unlocked_ioctl = perf_ioctl,
1878 .compat_ioctl = perf_ioctl,
1879 .mmap = perf_mmap,
1880 .fasync = perf_fasync,
1881};
1882
1883/*
1884 * Perf counter wakeup
1885 *
1886 * If there's data, ensure we set the poll() state and publish everything
1887 * to user-space before waking everybody up.
1888 */
1889
1890void perf_counter_wakeup(struct perf_counter *counter)
1891{
1892 wake_up_all(&counter->waitq);
1893
1894 if (counter->pending_kill) {
1895 kill_fasync(&counter->fasync, SIGIO, counter->pending_kill);
1896 counter->pending_kill = 0;
1897 }
1898}
1899
1900/*
1901 * Pending wakeups
1902 *
1903 * Handle the case where we need to wakeup up from NMI (or rq->lock) context.
1904 *
1905 * The NMI bit means we cannot possibly take locks. Therefore, maintain a
1906 * single linked list and use cmpxchg() to add entries lockless.
1907 */
1908
1909static void perf_pending_counter(struct perf_pending_entry *entry)
1910{
1911 struct perf_counter *counter = container_of(entry,
1912 struct perf_counter, pending);
1913
1914 if (counter->pending_disable) {
1915 counter->pending_disable = 0;
1916 perf_counter_disable(counter);
1917 }
1918
1919 if (counter->pending_wakeup) {
1920 counter->pending_wakeup = 0;
1921 perf_counter_wakeup(counter);
1922 }
1923}
1924
1925#define PENDING_TAIL ((struct perf_pending_entry *)-1UL)
1926
1927static DEFINE_PER_CPU(struct perf_pending_entry *, perf_pending_head) = {
1928 PENDING_TAIL,
1929};
1930
1931static void perf_pending_queue(struct perf_pending_entry *entry,
1932 void (*func)(struct perf_pending_entry *))
1933{
1934 struct perf_pending_entry **head;
1935
1936 if (cmpxchg(&entry->next, NULL, PENDING_TAIL) != NULL)
1937 return;
1938
1939 entry->func = func;
1940
1941 head = &get_cpu_var(perf_pending_head);
1942
1943 do {
1944 entry->next = *head;
1945 } while (cmpxchg(head, entry->next, entry) != entry->next);
1946
1947 set_perf_counter_pending();
1948
1949 put_cpu_var(perf_pending_head);
1950}
1951
1952static int __perf_pending_run(void)
1953{
1954 struct perf_pending_entry *list;
1955 int nr = 0;
1956
1957 list = xchg(&__get_cpu_var(perf_pending_head), PENDING_TAIL);
1958 while (list != PENDING_TAIL) {
1959 void (*func)(struct perf_pending_entry *);
1960 struct perf_pending_entry *entry = list;
1961
1962 list = list->next;
1963
1964 func = entry->func;
1965 entry->next = NULL;
1966 /*
1967 * Ensure we observe the unqueue before we issue the wakeup,
1968 * so that we won't be waiting forever.
1969 * -- see perf_not_pending().
1970 */
1971 smp_wmb();
1972
1973 func(entry);
1974 nr++;
1975 }
1976
1977 return nr;
1978}
1979
1980static inline int perf_not_pending(struct perf_counter *counter)
1981{
1982 /*
1983 * If we flush on whatever cpu we run, there is a chance we don't
1984 * need to wait.
1985 */
1986 get_cpu();
1987 __perf_pending_run();
1988 put_cpu();
1989
1990 /*
1991 * Ensure we see the proper queue state before going to sleep
1992 * so that we do not miss the wakeup. -- see perf_pending_handle()
1993 */
1994 smp_rmb();
1995 return counter->pending.next == NULL;
1996}
1997
1998static void perf_pending_sync(struct perf_counter *counter)
1999{
2000 wait_event(counter->waitq, perf_not_pending(counter));
2001}
2002
2003void perf_counter_do_pending(void)
2004{
2005 __perf_pending_run();
2006}
2007
2008/*
2009 * Callchain support -- arch specific
2010 */
2011
2012__weak struct perf_callchain_entry *perf_callchain(struct pt_regs *regs)
2013{
2014 return NULL;
2015}
2016
2017/*
2018 * Output
2019 */
2020
2021struct perf_output_handle {
2022 struct perf_counter *counter;
2023 struct perf_mmap_data *data;
2024 unsigned int offset;
2025 unsigned int head;
2026 int nmi;
2027 int overflow;
2028 int locked;
2029 unsigned long flags;
2030};
2031
2032static void perf_output_wakeup(struct perf_output_handle *handle)
2033{
2034 atomic_set(&handle->data->poll, POLL_IN);
2035
2036 if (handle->nmi) {
2037 handle->counter->pending_wakeup = 1;
2038 perf_pending_queue(&handle->counter->pending,
2039 perf_pending_counter);
2040 } else
2041 perf_counter_wakeup(handle->counter);
2042}
2043
2044/*
2045 * Curious locking construct.
2046 *
2047 * We need to ensure a later event doesn't publish a head when a former
2048 * event isn't done writing. However since we need to deal with NMIs we
2049 * cannot fully serialize things.
2050 *
2051 * What we do is serialize between CPUs so we only have to deal with NMI
2052 * nesting on a single CPU.
2053 *
2054 * We only publish the head (and generate a wakeup) when the outer-most
2055 * event completes.
2056 */
2057static void perf_output_lock(struct perf_output_handle *handle)
2058{
2059 struct perf_mmap_data *data = handle->data;
2060 int cpu;
2061
2062 handle->locked = 0;
2063
2064 local_irq_save(handle->flags);
2065 cpu = smp_processor_id();
2066
2067 if (in_nmi() && atomic_read(&data->lock) == cpu)
2068 return;
2069
2070 while (atomic_cmpxchg(&data->lock, -1, cpu) != -1)
2071 cpu_relax();
2072
2073 handle->locked = 1;
2074}
2075
2076static void perf_output_unlock(struct perf_output_handle *handle)
2077{
2078 struct perf_mmap_data *data = handle->data;
2079 int head, cpu;
2080
2081 data->done_head = data->head;
2082
2083 if (!handle->locked)
2084 goto out;
2085
2086again:
2087 /*
2088 * The xchg implies a full barrier that ensures all writes are done
2089 * before we publish the new head, matched by a rmb() in userspace when
2090 * reading this position.
2091 */
2092 while ((head = atomic_xchg(&data->done_head, 0)))
2093 data->user_page->data_head = head;
2094
2095 /*
2096 * NMI can happen here, which means we can miss a done_head update.
2097 */
2098
2099 cpu = atomic_xchg(&data->lock, -1);
2100 WARN_ON_ONCE(cpu != smp_processor_id());
2101
2102 /*
2103 * Therefore we have to validate we did not indeed do so.
2104 */
2105 if (unlikely(atomic_read(&data->done_head))) {
2106 /*
2107 * Since we had it locked, we can lock it again.
2108 */
2109 while (atomic_cmpxchg(&data->lock, -1, cpu) != -1)
2110 cpu_relax();
2111
2112 goto again;
2113 }
2114
2115 if (atomic_xchg(&data->wakeup, 0))
2116 perf_output_wakeup(handle);
2117out:
2118 local_irq_restore(handle->flags);
2119}
2120
2121static int perf_output_begin(struct perf_output_handle *handle,
2122 struct perf_counter *counter, unsigned int size,
2123 int nmi, int overflow)
2124{
2125 struct perf_mmap_data *data;
2126 unsigned int offset, head;
2127
2128 /*
2129 * For inherited counters we send all the output towards the parent.
2130 */
2131 if (counter->parent)
2132 counter = counter->parent;
2133
2134 rcu_read_lock();
2135 data = rcu_dereference(counter->data);
2136 if (!data)
2137 goto out;
2138
2139 handle->data = data;
2140 handle->counter = counter;
2141 handle->nmi = nmi;
2142 handle->overflow = overflow;
2143
2144 if (!data->nr_pages)
2145 goto fail;
2146
2147 perf_output_lock(handle);
2148
2149 do {
2150 offset = head = atomic_read(&data->head);
2151 head += size;
2152 } while (atomic_cmpxchg(&data->head, offset, head) != offset);
2153
2154 handle->offset = offset;
2155 handle->head = head;
2156
2157 if ((offset >> PAGE_SHIFT) != (head >> PAGE_SHIFT))
2158 atomic_set(&data->wakeup, 1);
2159
2160 return 0;
2161
2162fail:
2163 perf_output_wakeup(handle);
2164out:
2165 rcu_read_unlock();
2166
2167 return -ENOSPC;
2168}
2169
2170static void perf_output_copy(struct perf_output_handle *handle,
2171 void *buf, unsigned int len)
2172{
2173 unsigned int pages_mask;
2174 unsigned int offset;
2175 unsigned int size;
2176 void **pages;
2177
2178 offset = handle->offset;
2179 pages_mask = handle->data->nr_pages - 1;
2180 pages = handle->data->data_pages;
2181
2182 do {
2183 unsigned int page_offset;
2184 int nr;
2185
2186 nr = (offset >> PAGE_SHIFT) & pages_mask;
2187 page_offset = offset & (PAGE_SIZE - 1);
2188 size = min_t(unsigned int, PAGE_SIZE - page_offset, len);
2189
2190 memcpy(pages[nr] + page_offset, buf, size);
2191
2192 len -= size;
2193 buf += size;
2194 offset += size;
2195 } while (len);
2196
2197 handle->offset = offset;
2198
2199 /*
2200 * Check we didn't copy past our reservation window, taking the
2201 * possible unsigned int wrap into account.
2202 */
2203 WARN_ON_ONCE(((int)(handle->head - handle->offset)) < 0);
2204}
2205
2206#define perf_output_put(handle, x) \
2207 perf_output_copy((handle), &(x), sizeof(x))
2208
2209static void perf_output_end(struct perf_output_handle *handle)
2210{
2211 struct perf_counter *counter = handle->counter;
2212 struct perf_mmap_data *data = handle->data;
2213
2214 int wakeup_events = counter->hw_event.wakeup_events;
2215
2216 if (handle->overflow && wakeup_events) {
2217 int events = atomic_inc_return(&data->events);
2218 if (events >= wakeup_events) {
2219 atomic_sub(wakeup_events, &data->events);
2220 atomic_set(&data->wakeup, 1);
2221 }
2222 }
2223
2224 perf_output_unlock(handle);
2225 rcu_read_unlock();
2226}
2227
2228static void perf_counter_output(struct perf_counter *counter,
2229 int nmi, struct pt_regs *regs, u64 addr)
2230{
2231 int ret;
2232 u64 record_type = counter->hw_event.record_type;
2233 struct perf_output_handle handle;
2234 struct perf_event_header header;
2235 u64 ip;
2236 struct {
2237 u32 pid, tid;
2238 } tid_entry;
2239 struct {
2240 u64 event;
2241 u64 counter;
2242 } group_entry;
2243 struct perf_callchain_entry *callchain = NULL;
2244 int callchain_size = 0;
2245 u64 time;
2246 struct {
2247 u32 cpu, reserved;
2248 } cpu_entry;
2249
2250 header.type = 0;
2251 header.size = sizeof(header);
2252
2253 header.misc = PERF_EVENT_MISC_OVERFLOW;
2254 header.misc |= perf_misc_flags(regs);
2255
2256 if (record_type & PERF_RECORD_IP) {
2257 ip = perf_instruction_pointer(regs);
2258 header.type |= PERF_RECORD_IP;
2259 header.size += sizeof(ip);
2260 }
2261
2262 if (record_type & PERF_RECORD_TID) {
2263 /* namespace issues */
2264 tid_entry.pid = current->group_leader->pid;
2265 tid_entry.tid = current->pid;
2266
2267 header.type |= PERF_RECORD_TID;
2268 header.size += sizeof(tid_entry);
2269 }
2270
2271 if (record_type & PERF_RECORD_TIME) {
2272 /*
2273 * Maybe do better on x86 and provide cpu_clock_nmi()
2274 */
2275 time = sched_clock();
2276
2277 header.type |= PERF_RECORD_TIME;
2278 header.size += sizeof(u64);
2279 }
2280
2281 if (record_type & PERF_RECORD_ADDR) {
2282 header.type |= PERF_RECORD_ADDR;
2283 header.size += sizeof(u64);
2284 }
2285
2286 if (record_type & PERF_RECORD_CONFIG) {
2287 header.type |= PERF_RECORD_CONFIG;
2288 header.size += sizeof(u64);
2289 }
2290
2291 if (record_type & PERF_RECORD_CPU) {
2292 header.type |= PERF_RECORD_CPU;
2293 header.size += sizeof(cpu_entry);
2294
2295 cpu_entry.cpu = raw_smp_processor_id();
2296 }
2297
2298 if (record_type & PERF_RECORD_GROUP) {
2299 header.type |= PERF_RECORD_GROUP;
2300 header.size += sizeof(u64) +
2301 counter->nr_siblings * sizeof(group_entry);
2302 }
2303
2304 if (record_type & PERF_RECORD_CALLCHAIN) {
2305 callchain = perf_callchain(regs);
2306
2307 if (callchain) {
2308 callchain_size = (1 + callchain->nr) * sizeof(u64);
2309
2310 header.type |= PERF_RECORD_CALLCHAIN;
2311 header.size += callchain_size;
2312 }
2313 }
2314
2315 ret = perf_output_begin(&handle, counter, header.size, nmi, 1);
2316 if (ret)
2317 return;
2318
2319 perf_output_put(&handle, header);
2320
2321 if (record_type & PERF_RECORD_IP)
2322 perf_output_put(&handle, ip);
2323
2324 if (record_type & PERF_RECORD_TID)
2325 perf_output_put(&handle, tid_entry);
2326
2327 if (record_type & PERF_RECORD_TIME)
2328 perf_output_put(&handle, time);
2329
2330 if (record_type & PERF_RECORD_ADDR)
2331 perf_output_put(&handle, addr);
2332
2333 if (record_type & PERF_RECORD_CONFIG)
2334 perf_output_put(&handle, counter->hw_event.config);
2335
2336 if (record_type & PERF_RECORD_CPU)
2337 perf_output_put(&handle, cpu_entry);
2338
2339 /*
2340 * XXX PERF_RECORD_GROUP vs inherited counters seems difficult.
2341 */
2342 if (record_type & PERF_RECORD_GROUP) {
2343 struct perf_counter *leader, *sub;
2344 u64 nr = counter->nr_siblings;
2345
2346 perf_output_put(&handle, nr);
2347
2348 leader = counter->group_leader;
2349 list_for_each_entry(sub, &leader->sibling_list, list_entry) {
2350 if (sub != counter)
2351 sub->pmu->read(sub);
2352
2353 group_entry.event = sub->hw_event.config;
2354 group_entry.counter = atomic64_read(&sub->count);
2355
2356 perf_output_put(&handle, group_entry);
2357 }
2358 }
2359
2360 if (callchain)
2361 perf_output_copy(&handle, callchain, callchain_size);
2362
2363 perf_output_end(&handle);
2364}
2365
2366/*
2367 * comm tracking
2368 */
2369
2370struct perf_comm_event {
2371 struct task_struct *task;
2372 char *comm;
2373 int comm_size;
2374
2375 struct {
2376 struct perf_event_header header;
2377
2378 u32 pid;
2379 u32 tid;
2380 } event;
2381};
2382
2383static void perf_counter_comm_output(struct perf_counter *counter,
2384 struct perf_comm_event *comm_event)
2385{
2386 struct perf_output_handle handle;
2387 int size = comm_event->event.header.size;
2388 int ret = perf_output_begin(&handle, counter, size, 0, 0);
2389
2390 if (ret)
2391 return;
2392
2393 perf_output_put(&handle, comm_event->event);
2394 perf_output_copy(&handle, comm_event->comm,
2395 comm_event->comm_size);
2396 perf_output_end(&handle);
2397}
2398
2399static int perf_counter_comm_match(struct perf_counter *counter,
2400 struct perf_comm_event *comm_event)
2401{
2402 if (counter->hw_event.comm &&
2403 comm_event->event.header.type == PERF_EVENT_COMM)
2404 return 1;
2405
2406 return 0;
2407}
2408
2409static void perf_counter_comm_ctx(struct perf_counter_context *ctx,
2410 struct perf_comm_event *comm_event)
2411{
2412 struct perf_counter *counter;
2413
2414 if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
2415 return;
2416
2417 rcu_read_lock();
2418 list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
2419 if (perf_counter_comm_match(counter, comm_event))
2420 perf_counter_comm_output(counter, comm_event);
2421 }
2422 rcu_read_unlock();
2423}
2424
2425static void perf_counter_comm_event(struct perf_comm_event *comm_event)
2426{
2427 struct perf_cpu_context *cpuctx;
2428 struct perf_counter_context *ctx;
2429 unsigned int size;
2430 char *comm = comm_event->task->comm;
2431
2432 size = ALIGN(strlen(comm)+1, sizeof(u64));
2433
2434 comm_event->comm = comm;
2435 comm_event->comm_size = size;
2436
2437 comm_event->event.header.size = sizeof(comm_event->event) + size;
2438
2439 cpuctx = &get_cpu_var(perf_cpu_context);
2440 perf_counter_comm_ctx(&cpuctx->ctx, comm_event);
2441 put_cpu_var(perf_cpu_context);
2442
2443 rcu_read_lock();
2444 /*
2445 * doesn't really matter which of the child contexts the
2446 * events ends up in.
2447 */
2448 ctx = rcu_dereference(current->perf_counter_ctxp);
2449 if (ctx)
2450 perf_counter_comm_ctx(ctx, comm_event);
2451 rcu_read_unlock();
2452}
2453
2454void perf_counter_comm(struct task_struct *task)
2455{
2456 struct perf_comm_event comm_event;
2457
2458 if (!atomic_read(&nr_comm_tracking))
2459 return;
2460
2461 comm_event = (struct perf_comm_event){
2462 .task = task,
2463 .event = {
2464 .header = { .type = PERF_EVENT_COMM, },
2465 .pid = task->group_leader->pid,
2466 .tid = task->pid,
2467 },
2468 };
2469
2470 perf_counter_comm_event(&comm_event);
2471}
2472
2473/*
2474 * mmap tracking
2475 */
2476
2477struct perf_mmap_event {
2478 struct file *file;
2479 char *file_name;
2480 int file_size;
2481
2482 struct {
2483 struct perf_event_header header;
2484
2485 u32 pid;
2486 u32 tid;
2487 u64 start;
2488 u64 len;
2489 u64 pgoff;
2490 } event;
2491};
2492
2493static void perf_counter_mmap_output(struct perf_counter *counter,
2494 struct perf_mmap_event *mmap_event)
2495{
2496 struct perf_output_handle handle;
2497 int size = mmap_event->event.header.size;
2498 int ret = perf_output_begin(&handle, counter, size, 0, 0);
2499
2500 if (ret)
2501 return;
2502
2503 perf_output_put(&handle, mmap_event->event);
2504 perf_output_copy(&handle, mmap_event->file_name,
2505 mmap_event->file_size);
2506 perf_output_end(&handle);
2507}
2508
2509static int perf_counter_mmap_match(struct perf_counter *counter,
2510 struct perf_mmap_event *mmap_event)
2511{
2512 if (counter->hw_event.mmap &&
2513 mmap_event->event.header.type == PERF_EVENT_MMAP)
2514 return 1;
2515
2516 if (counter->hw_event.munmap &&
2517 mmap_event->event.header.type == PERF_EVENT_MUNMAP)
2518 return 1;
2519
2520 return 0;
2521}
2522
2523static void perf_counter_mmap_ctx(struct perf_counter_context *ctx,
2524 struct perf_mmap_event *mmap_event)
2525{
2526 struct perf_counter *counter;
2527
2528 if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
2529 return;
2530
2531 rcu_read_lock();
2532 list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
2533 if (perf_counter_mmap_match(counter, mmap_event))
2534 perf_counter_mmap_output(counter, mmap_event);
2535 }
2536 rcu_read_unlock();
2537}
2538
2539static void perf_counter_mmap_event(struct perf_mmap_event *mmap_event)
2540{
2541 struct perf_cpu_context *cpuctx;
2542 struct perf_counter_context *ctx;
2543 struct file *file = mmap_event->file;
2544 unsigned int size;
2545 char tmp[16];
2546 char *buf = NULL;
2547 char *name;
2548
2549 if (file) {
2550 buf = kzalloc(PATH_MAX, GFP_KERNEL);
2551 if (!buf) {
2552 name = strncpy(tmp, "//enomem", sizeof(tmp));
2553 goto got_name;
2554 }
2555 name = d_path(&file->f_path, buf, PATH_MAX);
2556 if (IS_ERR(name)) {
2557 name = strncpy(tmp, "//toolong", sizeof(tmp));
2558 goto got_name;
2559 }
2560 } else {
2561 name = strncpy(tmp, "//anon", sizeof(tmp));
2562 goto got_name;
2563 }
2564
2565got_name:
2566 size = ALIGN(strlen(name)+1, sizeof(u64));
2567
2568 mmap_event->file_name = name;
2569 mmap_event->file_size = size;
2570
2571 mmap_event->event.header.size = sizeof(mmap_event->event) + size;
2572
2573 cpuctx = &get_cpu_var(perf_cpu_context);
2574 perf_counter_mmap_ctx(&cpuctx->ctx, mmap_event);
2575 put_cpu_var(perf_cpu_context);
2576
2577 rcu_read_lock();
2578 /*
2579 * doesn't really matter which of the child contexts the
2580 * events ends up in.
2581 */
2582 ctx = rcu_dereference(current->perf_counter_ctxp);
2583 if (ctx)
2584 perf_counter_mmap_ctx(ctx, mmap_event);
2585 rcu_read_unlock();
2586
2587 kfree(buf);
2588}
2589
2590void perf_counter_mmap(unsigned long addr, unsigned long len,
2591 unsigned long pgoff, struct file *file)
2592{
2593 struct perf_mmap_event mmap_event;
2594
2595 if (!atomic_read(&nr_mmap_tracking))
2596 return;
2597
2598 mmap_event = (struct perf_mmap_event){
2599 .file = file,
2600 .event = {
2601 .header = { .type = PERF_EVENT_MMAP, },
2602 .pid = current->group_leader->pid,
2603 .tid = current->pid,
2604 .start = addr,
2605 .len = len,
2606 .pgoff = pgoff,
2607 },
2608 };
2609
2610 perf_counter_mmap_event(&mmap_event);
2611}
2612
2613void perf_counter_munmap(unsigned long addr, unsigned long len,
2614 unsigned long pgoff, struct file *file)
2615{
2616 struct perf_mmap_event mmap_event;
2617
2618 if (!atomic_read(&nr_munmap_tracking))
2619 return;
2620
2621 mmap_event = (struct perf_mmap_event){
2622 .file = file,
2623 .event = {
2624 .header = { .type = PERF_EVENT_MUNMAP, },
2625 .pid = current->group_leader->pid,
2626 .tid = current->pid,
2627 .start = addr,
2628 .len = len,
2629 .pgoff = pgoff,
2630 },
2631 };
2632
2633 perf_counter_mmap_event(&mmap_event);
2634}
2635
2636/*
2637 * Log irq_period changes so that analyzing tools can re-normalize the
2638 * event flow.
2639 */
2640
2641static void perf_log_period(struct perf_counter *counter, u64 period)
2642{
2643 struct perf_output_handle handle;
2644 int ret;
2645
2646 struct {
2647 struct perf_event_header header;
2648 u64 time;
2649 u64 period;
2650 } freq_event = {
2651 .header = {
2652 .type = PERF_EVENT_PERIOD,
2653 .misc = 0,
2654 .size = sizeof(freq_event),
2655 },
2656 .time = sched_clock(),
2657 .period = period,
2658 };
2659
2660 if (counter->hw.irq_period == period)
2661 return;
2662
2663 ret = perf_output_begin(&handle, counter, sizeof(freq_event), 0, 0);
2664 if (ret)
2665 return;
2666
2667 perf_output_put(&handle, freq_event);
2668 perf_output_end(&handle);
2669}
2670
2671/*
2672 * IRQ throttle logging
2673 */
2674
2675static void perf_log_throttle(struct perf_counter *counter, int enable)
2676{
2677 struct perf_output_handle handle;
2678 int ret;
2679
2680 struct {
2681 struct perf_event_header header;
2682 u64 time;
2683 } throttle_event = {
2684 .header = {
2685 .type = PERF_EVENT_THROTTLE + 1,
2686 .misc = 0,
2687 .size = sizeof(throttle_event),
2688 },
2689 .time = sched_clock(),
2690 };
2691
2692 ret = perf_output_begin(&handle, counter, sizeof(throttle_event), 1, 0);
2693 if (ret)
2694 return;
2695
2696 perf_output_put(&handle, throttle_event);
2697 perf_output_end(&handle);
2698}
2699
2700/*
2701 * Generic counter overflow handling.
2702 */
2703
2704int perf_counter_overflow(struct perf_counter *counter,
2705 int nmi, struct pt_regs *regs, u64 addr)
2706{
2707 int events = atomic_read(&counter->event_limit);
2708 int throttle = counter->pmu->unthrottle != NULL;
2709 int ret = 0;
2710
2711 if (!throttle) {
2712 counter->hw.interrupts++;
2713 } else if (counter->hw.interrupts != MAX_INTERRUPTS) {
2714 counter->hw.interrupts++;
2715 if (HZ*counter->hw.interrupts > (u64)sysctl_perf_counter_limit) {
2716 counter->hw.interrupts = MAX_INTERRUPTS;
2717 perf_log_throttle(counter, 0);
2718 ret = 1;
2719 }
2720 }
2721
2722 /*
2723 * XXX event_limit might not quite work as expected on inherited
2724 * counters
2725 */
2726
2727 counter->pending_kill = POLL_IN;
2728 if (events && atomic_dec_and_test(&counter->event_limit)) {
2729 ret = 1;
2730 counter->pending_kill = POLL_HUP;
2731 if (nmi) {
2732 counter->pending_disable = 1;
2733 perf_pending_queue(&counter->pending,
2734 perf_pending_counter);
2735 } else
2736 perf_counter_disable(counter);
2737 }
2738
2739 perf_counter_output(counter, nmi, regs, addr);
2740 return ret;
2741}
2742
2743/*
2744 * Generic software counter infrastructure
2745 */
2746
2747static void perf_swcounter_update(struct perf_counter *counter)
2748{
2749 struct hw_perf_counter *hwc = &counter->hw;
2750 u64 prev, now;
2751 s64 delta;
2752
2753again:
2754 prev = atomic64_read(&hwc->prev_count);
2755 now = atomic64_read(&hwc->count);
2756 if (atomic64_cmpxchg(&hwc->prev_count, prev, now) != prev)
2757 goto again;
2758
2759 delta = now - prev;
2760
2761 atomic64_add(delta, &counter->count);
2762 atomic64_sub(delta, &hwc->period_left);
2763}
2764
2765static void perf_swcounter_set_period(struct perf_counter *counter)
2766{
2767 struct hw_perf_counter *hwc = &counter->hw;
2768 s64 left = atomic64_read(&hwc->period_left);
2769 s64 period = hwc->irq_period;
2770
2771 if (unlikely(left <= -period)) {
2772 left = period;
2773 atomic64_set(&hwc->period_left, left);
2774 }
2775
2776 if (unlikely(left <= 0)) {
2777 left += period;
2778 atomic64_add(period, &hwc->period_left);
2779 }
2780
2781 atomic64_set(&hwc->prev_count, -left);
2782 atomic64_set(&hwc->count, -left);
2783}
2784
2785static enum hrtimer_restart perf_swcounter_hrtimer(struct hrtimer *hrtimer)
2786{
2787 enum hrtimer_restart ret = HRTIMER_RESTART;
2788 struct perf_counter *counter;
2789 struct pt_regs *regs;
2790 u64 period;
2791
2792 counter = container_of(hrtimer, struct perf_counter, hw.hrtimer);
2793 counter->pmu->read(counter);
2794
2795 regs = get_irq_regs();
2796 /*
2797 * In case we exclude kernel IPs or are somehow not in interrupt
2798 * context, provide the next best thing, the user IP.
2799 */
2800 if ((counter->hw_event.exclude_kernel || !regs) &&
2801 !counter->hw_event.exclude_user)
2802 regs = task_pt_regs(current);
2803
2804 if (regs) {
2805 if (perf_counter_overflow(counter, 0, regs, 0))
2806 ret = HRTIMER_NORESTART;
2807 }
2808
2809 period = max_t(u64, 10000, counter->hw.irq_period);
2810 hrtimer_forward_now(hrtimer, ns_to_ktime(period));
2811
2812 return ret;
2813}
2814
2815static void perf_swcounter_overflow(struct perf_counter *counter,
2816 int nmi, struct pt_regs *regs, u64 addr)
2817{
2818 perf_swcounter_update(counter);
2819 perf_swcounter_set_period(counter);
2820 if (perf_counter_overflow(counter, nmi, regs, addr))
2821 /* soft-disable the counter */
2822 ;
2823
2824}
2825
2826static int perf_swcounter_match(struct perf_counter *counter,
2827 enum perf_event_types type,
2828 u32 event, struct pt_regs *regs)
2829{
2830 if (counter->state != PERF_COUNTER_STATE_ACTIVE)
2831 return 0;
2832
2833 if (perf_event_raw(&counter->hw_event))
2834 return 0;
2835
2836 if (perf_event_type(&counter->hw_event) != type)
2837 return 0;
2838
2839 if (perf_event_id(&counter->hw_event) != event)
2840 return 0;
2841
2842 if (counter->hw_event.exclude_user && user_mode(regs))
2843 return 0;
2844
2845 if (counter->hw_event.exclude_kernel && !user_mode(regs))
2846 return 0;
2847
2848 return 1;
2849}
2850
2851static void perf_swcounter_add(struct perf_counter *counter, u64 nr,
2852 int nmi, struct pt_regs *regs, u64 addr)
2853{
2854 int neg = atomic64_add_negative(nr, &counter->hw.count);
2855 if (counter->hw.irq_period && !neg)
2856 perf_swcounter_overflow(counter, nmi, regs, addr);
2857}
2858
2859static void perf_swcounter_ctx_event(struct perf_counter_context *ctx,
2860 enum perf_event_types type, u32 event,
2861 u64 nr, int nmi, struct pt_regs *regs,
2862 u64 addr)
2863{
2864 struct perf_counter *counter;
2865
2866 if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
2867 return;
2868
2869 rcu_read_lock();
2870 list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
2871 if (perf_swcounter_match(counter, type, event, regs))
2872 perf_swcounter_add(counter, nr, nmi, regs, addr);
2873 }
2874 rcu_read_unlock();
2875}
2876
2877static int *perf_swcounter_recursion_context(struct perf_cpu_context *cpuctx)
2878{
2879 if (in_nmi())
2880 return &cpuctx->recursion[3];
2881
2882 if (in_irq())
2883 return &cpuctx->recursion[2];
2884
2885 if (in_softirq())
2886 return &cpuctx->recursion[1];
2887
2888 return &cpuctx->recursion[0];
2889}
2890
2891static void __perf_swcounter_event(enum perf_event_types type, u32 event,
2892 u64 nr, int nmi, struct pt_regs *regs,
2893 u64 addr)
2894{
2895 struct perf_cpu_context *cpuctx = &get_cpu_var(perf_cpu_context);
2896 int *recursion = perf_swcounter_recursion_context(cpuctx);
2897 struct perf_counter_context *ctx;
2898
2899 if (*recursion)
2900 goto out;
2901
2902 (*recursion)++;
2903 barrier();
2904
2905 perf_swcounter_ctx_event(&cpuctx->ctx, type, event,
2906 nr, nmi, regs, addr);
2907 rcu_read_lock();
2908 /*
2909 * doesn't really matter which of the child contexts the
2910 * events ends up in.
2911 */
2912 ctx = rcu_dereference(current->perf_counter_ctxp);
2913 if (ctx)
2914 perf_swcounter_ctx_event(ctx, type, event, nr, nmi, regs, addr);
2915 rcu_read_unlock();
2916
2917 barrier();
2918 (*recursion)--;
2919
2920out:
2921 put_cpu_var(perf_cpu_context);
2922}
2923
2924void
2925perf_swcounter_event(u32 event, u64 nr, int nmi, struct pt_regs *regs, u64 addr)
2926{
2927 __perf_swcounter_event(PERF_TYPE_SOFTWARE, event, nr, nmi, regs, addr);
2928}
2929
2930static void perf_swcounter_read(struct perf_counter *counter)
2931{
2932 perf_swcounter_update(counter);
2933}
2934
2935static int perf_swcounter_enable(struct perf_counter *counter)
2936{
2937 perf_swcounter_set_period(counter);
2938 return 0;
2939}
2940
2941static void perf_swcounter_disable(struct perf_counter *counter)
2942{
2943 perf_swcounter_update(counter);
2944}
2945
2946static const struct pmu perf_ops_generic = {
2947 .enable = perf_swcounter_enable,
2948 .disable = perf_swcounter_disable,
2949 .read = perf_swcounter_read,
2950};
2951
2952/*
2953 * Software counter: cpu wall time clock
2954 */
2955
2956static void cpu_clock_perf_counter_update(struct perf_counter *counter)
2957{
2958 int cpu = raw_smp_processor_id();
2959 s64 prev;
2960 u64 now;
2961
2962 now = cpu_clock(cpu);
2963 prev = atomic64_read(&counter->hw.prev_count);
2964 atomic64_set(&counter->hw.prev_count, now);
2965 atomic64_add(now - prev, &counter->count);
2966}
2967
2968static int cpu_clock_perf_counter_enable(struct perf_counter *counter)
2969{
2970 struct hw_perf_counter *hwc = &counter->hw;
2971 int cpu = raw_smp_processor_id();
2972
2973 atomic64_set(&hwc->prev_count, cpu_clock(cpu));
2974 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
2975 hwc->hrtimer.function = perf_swcounter_hrtimer;
2976 if (hwc->irq_period) {
2977 u64 period = max_t(u64, 10000, hwc->irq_period);
2978 __hrtimer_start_range_ns(&hwc->hrtimer,
2979 ns_to_ktime(period), 0,
2980 HRTIMER_MODE_REL, 0);
2981 }
2982
2983 return 0;
2984}
2985
2986static void cpu_clock_perf_counter_disable(struct perf_counter *counter)
2987{
2988 if (counter->hw.irq_period)
2989 hrtimer_cancel(&counter->hw.hrtimer);
2990 cpu_clock_perf_counter_update(counter);
2991}
2992
2993static void cpu_clock_perf_counter_read(struct perf_counter *counter)
2994{
2995 cpu_clock_perf_counter_update(counter);
2996}
2997
2998static const struct pmu perf_ops_cpu_clock = {
2999 .enable = cpu_clock_perf_counter_enable,
3000 .disable = cpu_clock_perf_counter_disable,
3001 .read = cpu_clock_perf_counter_read,
3002};
3003
3004/*
3005 * Software counter: task time clock
3006 */
3007
3008static void task_clock_perf_counter_update(struct perf_counter *counter, u64 now)
3009{
3010 u64 prev;
3011 s64 delta;
3012
3013 prev = atomic64_xchg(&counter->hw.prev_count, now);
3014 delta = now - prev;
3015 atomic64_add(delta, &counter->count);
3016}
3017
3018static int task_clock_perf_counter_enable(struct perf_counter *counter)
3019{
3020 struct hw_perf_counter *hwc = &counter->hw;
3021 u64 now;
3022
3023 now = counter->ctx->time;
3024
3025 atomic64_set(&hwc->prev_count, now);
3026 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
3027 hwc->hrtimer.function = perf_swcounter_hrtimer;
3028 if (hwc->irq_period) {
3029 u64 period = max_t(u64, 10000, hwc->irq_period);
3030 __hrtimer_start_range_ns(&hwc->hrtimer,
3031 ns_to_ktime(period), 0,
3032 HRTIMER_MODE_REL, 0);
3033 }
3034
3035 return 0;
3036}
3037
3038static void task_clock_perf_counter_disable(struct perf_counter *counter)
3039{
3040 if (counter->hw.irq_period)
3041 hrtimer_cancel(&counter->hw.hrtimer);
3042 task_clock_perf_counter_update(counter, counter->ctx->time);
3043
3044}
3045
3046static void task_clock_perf_counter_read(struct perf_counter *counter)
3047{
3048 u64 time;
3049
3050 if (!in_nmi()) {
3051 update_context_time(counter->ctx);
3052 time = counter->ctx->time;
3053 } else {
3054 u64 now = perf_clock();
3055 u64 delta = now - counter->ctx->timestamp;
3056 time = counter->ctx->time + delta;
3057 }
3058
3059 task_clock_perf_counter_update(counter, time);
3060}
3061
3062static const struct pmu perf_ops_task_clock = {
3063 .enable = task_clock_perf_counter_enable,
3064 .disable = task_clock_perf_counter_disable,
3065 .read = task_clock_perf_counter_read,
3066};
3067
3068/*
3069 * Software counter: cpu migrations
3070 */
3071
3072static inline u64 get_cpu_migrations(struct perf_counter *counter)
3073{
3074 struct task_struct *curr = counter->ctx->task;
3075
3076 if (curr)
3077 return curr->se.nr_migrations;
3078 return cpu_nr_migrations(smp_processor_id());
3079}
3080
3081static void cpu_migrations_perf_counter_update(struct perf_counter *counter)
3082{
3083 u64 prev, now;
3084 s64 delta;
3085
3086 prev = atomic64_read(&counter->hw.prev_count);
3087 now = get_cpu_migrations(counter);
3088
3089 atomic64_set(&counter->hw.prev_count, now);
3090
3091 delta = now - prev;
3092
3093 atomic64_add(delta, &counter->count);
3094}
3095
3096static void cpu_migrations_perf_counter_read(struct perf_counter *counter)
3097{
3098 cpu_migrations_perf_counter_update(counter);
3099}
3100
3101static int cpu_migrations_perf_counter_enable(struct perf_counter *counter)
3102{
3103 if (counter->prev_state <= PERF_COUNTER_STATE_OFF)
3104 atomic64_set(&counter->hw.prev_count,
3105 get_cpu_migrations(counter));
3106 return 0;
3107}
3108
3109static void cpu_migrations_perf_counter_disable(struct perf_counter *counter)
3110{
3111 cpu_migrations_perf_counter_update(counter);
3112}
3113
3114static const struct pmu perf_ops_cpu_migrations = {
3115 .enable = cpu_migrations_perf_counter_enable,
3116 .disable = cpu_migrations_perf_counter_disable,
3117 .read = cpu_migrations_perf_counter_read,
3118};
3119
3120#ifdef CONFIG_EVENT_PROFILE
3121void perf_tpcounter_event(int event_id)
3122{
3123 struct pt_regs *regs = get_irq_regs();
3124
3125 if (!regs)
3126 regs = task_pt_regs(current);
3127
3128 __perf_swcounter_event(PERF_TYPE_TRACEPOINT, event_id, 1, 1, regs, 0);
3129}
3130EXPORT_SYMBOL_GPL(perf_tpcounter_event);
3131
3132extern int ftrace_profile_enable(int);
3133extern void ftrace_profile_disable(int);
3134
3135static void tp_perf_counter_destroy(struct perf_counter *counter)
3136{
3137 ftrace_profile_disable(perf_event_id(&counter->hw_event));
3138}
3139
3140static const struct pmu *tp_perf_counter_init(struct perf_counter *counter)
3141{
3142 int event_id = perf_event_id(&counter->hw_event);
3143 int ret;
3144
3145 ret = ftrace_profile_enable(event_id);
3146 if (ret)
3147 return NULL;
3148
3149 counter->destroy = tp_perf_counter_destroy;
3150 counter->hw.irq_period = counter->hw_event.irq_period;
3151
3152 return &perf_ops_generic;
3153}
3154#else
3155static const struct pmu *tp_perf_counter_init(struct perf_counter *counter)
3156{
3157 return NULL;
3158}
3159#endif
3160
3161static const struct pmu *sw_perf_counter_init(struct perf_counter *counter)
3162{
3163 const struct pmu *pmu = NULL;
3164
3165 /*
3166 * Software counters (currently) can't in general distinguish
3167 * between user, kernel and hypervisor events.
3168 * However, context switches and cpu migrations are considered
3169 * to be kernel events, and page faults are never hypervisor
3170 * events.
3171 */
3172 switch (perf_event_id(&counter->hw_event)) {
3173 case PERF_COUNT_CPU_CLOCK:
3174 pmu = &perf_ops_cpu_clock;
3175
3176 break;
3177 case PERF_COUNT_TASK_CLOCK:
3178 /*
3179 * If the user instantiates this as a per-cpu counter,
3180 * use the cpu_clock counter instead.
3181 */
3182 if (counter->ctx->task)
3183 pmu = &perf_ops_task_clock;
3184 else
3185 pmu = &perf_ops_cpu_clock;
3186
3187 break;
3188 case PERF_COUNT_PAGE_FAULTS:
3189 case PERF_COUNT_PAGE_FAULTS_MIN:
3190 case PERF_COUNT_PAGE_FAULTS_MAJ:
3191 case PERF_COUNT_CONTEXT_SWITCHES:
3192 pmu = &perf_ops_generic;
3193 break;
3194 case PERF_COUNT_CPU_MIGRATIONS:
3195 if (!counter->hw_event.exclude_kernel)
3196 pmu = &perf_ops_cpu_migrations;
3197 break;
3198 }
3199
3200 return pmu;
3201}
3202
3203/*
3204 * Allocate and initialize a counter structure
3205 */
3206static struct perf_counter *
3207perf_counter_alloc(struct perf_counter_hw_event *hw_event,
3208 int cpu,
3209 struct perf_counter_context *ctx,
3210 struct perf_counter *group_leader,
3211 gfp_t gfpflags)
3212{
3213 const struct pmu *pmu;
3214 struct perf_counter *counter;
3215 struct hw_perf_counter *hwc;
3216 long err;
3217
3218 counter = kzalloc(sizeof(*counter), gfpflags);
3219 if (!counter)
3220 return ERR_PTR(-ENOMEM);
3221
3222 /*
3223 * Single counters are their own group leaders, with an
3224 * empty sibling list:
3225 */
3226 if (!group_leader)
3227 group_leader = counter;
3228
3229 mutex_init(&counter->child_mutex);
3230 INIT_LIST_HEAD(&counter->child_list);
3231
3232 INIT_LIST_HEAD(&counter->list_entry);
3233 INIT_LIST_HEAD(&counter->event_entry);
3234 INIT_LIST_HEAD(&counter->sibling_list);
3235 init_waitqueue_head(&counter->waitq);
3236
3237 mutex_init(&counter->mmap_mutex);
3238
3239 counter->cpu = cpu;
3240 counter->hw_event = *hw_event;
3241 counter->group_leader = group_leader;
3242 counter->pmu = NULL;
3243 counter->ctx = ctx;
3244 counter->oncpu = -1;
3245
3246 counter->state = PERF_COUNTER_STATE_INACTIVE;
3247 if (hw_event->disabled)
3248 counter->state = PERF_COUNTER_STATE_OFF;
3249
3250 pmu = NULL;
3251
3252 hwc = &counter->hw;
3253 if (hw_event->freq && hw_event->irq_freq)
3254 hwc->irq_period = div64_u64(TICK_NSEC, hw_event->irq_freq);
3255 else
3256 hwc->irq_period = hw_event->irq_period;
3257
3258 /*
3259 * we currently do not support PERF_RECORD_GROUP on inherited counters
3260 */
3261 if (hw_event->inherit && (hw_event->record_type & PERF_RECORD_GROUP))
3262 goto done;
3263
3264 if (perf_event_raw(hw_event)) {
3265 pmu = hw_perf_counter_init(counter);
3266 goto done;
3267 }
3268
3269 switch (perf_event_type(hw_event)) {
3270 case PERF_TYPE_HARDWARE:
3271 pmu = hw_perf_counter_init(counter);
3272 break;
3273
3274 case PERF_TYPE_SOFTWARE:
3275 pmu = sw_perf_counter_init(counter);
3276 break;
3277
3278 case PERF_TYPE_TRACEPOINT:
3279 pmu = tp_perf_counter_init(counter);
3280 break;
3281 }
3282done:
3283 err = 0;
3284 if (!pmu)
3285 err = -EINVAL;
3286 else if (IS_ERR(pmu))
3287 err = PTR_ERR(pmu);
3288
3289 if (err) {
3290 kfree(counter);
3291 return ERR_PTR(err);
3292 }
3293
3294 counter->pmu = pmu;
3295
3296 atomic_inc(&nr_counters);
3297 if (counter->hw_event.mmap)
3298 atomic_inc(&nr_mmap_tracking);
3299 if (counter->hw_event.munmap)
3300 atomic_inc(&nr_munmap_tracking);
3301 if (counter->hw_event.comm)
3302 atomic_inc(&nr_comm_tracking);
3303
3304 return counter;
3305}
3306
3307/**
3308 * sys_perf_counter_open - open a performance counter, associate it to a task/cpu
3309 *
3310 * @hw_event_uptr: event type attributes for monitoring/sampling
3311 * @pid: target pid
3312 * @cpu: target cpu
3313 * @group_fd: group leader counter fd
3314 */
3315SYSCALL_DEFINE5(perf_counter_open,
3316 const struct perf_counter_hw_event __user *, hw_event_uptr,
3317 pid_t, pid, int, cpu, int, group_fd, unsigned long, flags)
3318{
3319 struct perf_counter *counter, *group_leader;
3320 struct perf_counter_hw_event hw_event;
3321 struct perf_counter_context *ctx;
3322 struct file *counter_file = NULL;
3323 struct file *group_file = NULL;
3324 int fput_needed = 0;
3325 int fput_needed2 = 0;
3326 int ret;
3327
3328 /* for future expandability... */
3329 if (flags)
3330 return -EINVAL;
3331
3332 if (copy_from_user(&hw_event, hw_event_uptr, sizeof(hw_event)) != 0)
3333 return -EFAULT;
3334
3335 /*
3336 * Get the target context (task or percpu):
3337 */
3338 ctx = find_get_context(pid, cpu);
3339 if (IS_ERR(ctx))
3340 return PTR_ERR(ctx);
3341
3342 /*
3343 * Look up the group leader (we will attach this counter to it):
3344 */
3345 group_leader = NULL;
3346 if (group_fd != -1) {
3347 ret = -EINVAL;
3348 group_file = fget_light(group_fd, &fput_needed);
3349 if (!group_file)
3350 goto err_put_context;
3351 if (group_file->f_op != &perf_fops)
3352 goto err_put_context;
3353
3354 group_leader = group_file->private_data;
3355 /*
3356 * Do not allow a recursive hierarchy (this new sibling
3357 * becoming part of another group-sibling):
3358 */
3359 if (group_leader->group_leader != group_leader)
3360 goto err_put_context;
3361 /*
3362 * Do not allow to attach to a group in a different
3363 * task or CPU context:
3364 */
3365 if (group_leader->ctx != ctx)
3366 goto err_put_context;
3367 /*
3368 * Only a group leader can be exclusive or pinned
3369 */
3370 if (hw_event.exclusive || hw_event.pinned)
3371 goto err_put_context;
3372 }
3373
3374 counter = perf_counter_alloc(&hw_event, cpu, ctx, group_leader,
3375 GFP_KERNEL);
3376 ret = PTR_ERR(counter);
3377 if (IS_ERR(counter))
3378 goto err_put_context;
3379
3380 ret = anon_inode_getfd("[perf_counter]", &perf_fops, counter, 0);
3381 if (ret < 0)
3382 goto err_free_put_context;
3383
3384 counter_file = fget_light(ret, &fput_needed2);
3385 if (!counter_file)
3386 goto err_free_put_context;
3387
3388 counter->filp = counter_file;
3389 WARN_ON_ONCE(ctx->parent_ctx);
3390 mutex_lock(&ctx->mutex);
3391 perf_install_in_context(ctx, counter, cpu);
3392 ++ctx->generation;
3393 mutex_unlock(&ctx->mutex);
3394
3395 counter->owner = current;
3396 get_task_struct(current);
3397 mutex_lock(&current->perf_counter_mutex);
3398 list_add_tail(&counter->owner_entry, &current->perf_counter_list);
3399 mutex_unlock(&current->perf_counter_mutex);
3400
3401 fput_light(counter_file, fput_needed2);
3402
3403out_fput:
3404 fput_light(group_file, fput_needed);
3405
3406 return ret;
3407
3408err_free_put_context:
3409 kfree(counter);
3410
3411err_put_context:
3412 put_ctx(ctx);
3413
3414 goto out_fput;
3415}
3416
3417/*
3418 * inherit a counter from parent task to child task:
3419 */
3420static struct perf_counter *
3421inherit_counter(struct perf_counter *parent_counter,
3422 struct task_struct *parent,
3423 struct perf_counter_context *parent_ctx,
3424 struct task_struct *child,
3425 struct perf_counter *group_leader,
3426 struct perf_counter_context *child_ctx)
3427{
3428 struct perf_counter *child_counter;
3429
3430 /*
3431 * Instead of creating recursive hierarchies of counters,
3432 * we link inherited counters back to the original parent,
3433 * which has a filp for sure, which we use as the reference
3434 * count:
3435 */
3436 if (parent_counter->parent)
3437 parent_counter = parent_counter->parent;
3438
3439 child_counter = perf_counter_alloc(&parent_counter->hw_event,
3440 parent_counter->cpu, child_ctx,
3441 group_leader, GFP_KERNEL);
3442 if (IS_ERR(child_counter))
3443 return child_counter;
3444 get_ctx(child_ctx);
3445
3446 /*
3447 * Make the child state follow the state of the parent counter,
3448 * not its hw_event.disabled bit. We hold the parent's mutex,
3449 * so we won't race with perf_counter_{en,dis}able_family.
3450 */
3451 if (parent_counter->state >= PERF_COUNTER_STATE_INACTIVE)
3452 child_counter->state = PERF_COUNTER_STATE_INACTIVE;
3453 else
3454 child_counter->state = PERF_COUNTER_STATE_OFF;
3455
3456 /*
3457 * Link it up in the child's context:
3458 */
3459 add_counter_to_ctx(child_counter, child_ctx);
3460
3461 child_counter->parent = parent_counter;
3462 /*
3463 * inherit into child's child as well:
3464 */
3465 child_counter->hw_event.inherit = 1;
3466
3467 /*
3468 * Get a reference to the parent filp - we will fput it
3469 * when the child counter exits. This is safe to do because
3470 * we are in the parent and we know that the filp still
3471 * exists and has a nonzero count:
3472 */
3473 atomic_long_inc(&parent_counter->filp->f_count);
3474
3475 /*
3476 * Link this into the parent counter's child list
3477 */
3478 WARN_ON_ONCE(parent_counter->ctx->parent_ctx);
3479 mutex_lock(&parent_counter->child_mutex);
3480 list_add_tail(&child_counter->child_list, &parent_counter->child_list);
3481 mutex_unlock(&parent_counter->child_mutex);
3482
3483 return child_counter;
3484}
3485
3486static int inherit_group(struct perf_counter *parent_counter,
3487 struct task_struct *parent,
3488 struct perf_counter_context *parent_ctx,
3489 struct task_struct *child,
3490 struct perf_counter_context *child_ctx)
3491{
3492 struct perf_counter *leader;
3493 struct perf_counter *sub;
3494 struct perf_counter *child_ctr;
3495
3496 leader = inherit_counter(parent_counter, parent, parent_ctx,
3497 child, NULL, child_ctx);
3498 if (IS_ERR(leader))
3499 return PTR_ERR(leader);
3500 list_for_each_entry(sub, &parent_counter->sibling_list, list_entry) {
3501 child_ctr = inherit_counter(sub, parent, parent_ctx,
3502 child, leader, child_ctx);
3503 if (IS_ERR(child_ctr))
3504 return PTR_ERR(child_ctr);
3505 }
3506 return 0;
3507}
3508
3509static void sync_child_counter(struct perf_counter *child_counter,
3510 struct perf_counter *parent_counter)
3511{
3512 u64 child_val;
3513
3514 child_val = atomic64_read(&child_counter->count);
3515
3516 /*
3517 * Add back the child's count to the parent's count:
3518 */
3519 atomic64_add(child_val, &parent_counter->count);
3520 atomic64_add(child_counter->total_time_enabled,
3521 &parent_counter->child_total_time_enabled);
3522 atomic64_add(child_counter->total_time_running,
3523 &parent_counter->child_total_time_running);
3524
3525 /*
3526 * Remove this counter from the parent's list
3527 */
3528 WARN_ON_ONCE(parent_counter->ctx->parent_ctx);
3529 mutex_lock(&parent_counter->child_mutex);
3530 list_del_init(&child_counter->child_list);
3531 mutex_unlock(&parent_counter->child_mutex);
3532
3533 /*
3534 * Release the parent counter, if this was the last
3535 * reference to it.
3536 */
3537 fput(parent_counter->filp);
3538}
3539
3540static void
3541__perf_counter_exit_task(struct perf_counter *child_counter,
3542 struct perf_counter_context *child_ctx)
3543{
3544 struct perf_counter *parent_counter;
3545
3546 update_counter_times(child_counter);
3547 perf_counter_remove_from_context(child_counter);
3548
3549 parent_counter = child_counter->parent;
3550 /*
3551 * It can happen that parent exits first, and has counters
3552 * that are still around due to the child reference. These
3553 * counters need to be zapped - but otherwise linger.
3554 */
3555 if (parent_counter) {
3556 sync_child_counter(child_counter, parent_counter);
3557 free_counter(child_counter);
3558 }
3559}
3560
3561/*
3562 * When a child task exits, feed back counter values to parent counters.
3563 */
3564void perf_counter_exit_task(struct task_struct *child)
3565{
3566 struct perf_counter *child_counter, *tmp;
3567 struct perf_counter_context *child_ctx;
3568 unsigned long flags;
3569
3570 if (likely(!child->perf_counter_ctxp))
3571 return;
3572
3573 local_irq_save(flags);
3574 /*
3575 * We can't reschedule here because interrupts are disabled,
3576 * and either child is current or it is a task that can't be
3577 * scheduled, so we are now safe from rescheduling changing
3578 * our context.
3579 */
3580 child_ctx = child->perf_counter_ctxp;
3581 __perf_counter_task_sched_out(child_ctx);
3582
3583 /*
3584 * Take the context lock here so that if find_get_context is
3585 * reading child->perf_counter_ctxp, we wait until it has
3586 * incremented the context's refcount before we do put_ctx below.
3587 */
3588 spin_lock(&child_ctx->lock);
3589 child->perf_counter_ctxp = NULL;
3590 if (child_ctx->parent_ctx) {
3591 /*
3592 * This context is a clone; unclone it so it can't get
3593 * swapped to another process while we're removing all
3594 * the counters from it.
3595 */
3596 put_ctx(child_ctx->parent_ctx);
3597 child_ctx->parent_ctx = NULL;
3598 }
3599 spin_unlock(&child_ctx->lock);
3600 local_irq_restore(flags);
3601
3602 mutex_lock(&child_ctx->mutex);
3603
3604again:
3605 list_for_each_entry_safe(child_counter, tmp, &child_ctx->counter_list,
3606 list_entry)
3607 __perf_counter_exit_task(child_counter, child_ctx);
3608
3609 /*
3610 * If the last counter was a group counter, it will have appended all
3611 * its siblings to the list, but we obtained 'tmp' before that which
3612 * will still point to the list head terminating the iteration.
3613 */
3614 if (!list_empty(&child_ctx->counter_list))
3615 goto again;
3616
3617 mutex_unlock(&child_ctx->mutex);
3618
3619 put_ctx(child_ctx);
3620}
3621
3622/*
3623 * free an unexposed, unused context as created by inheritance by
3624 * init_task below, used by fork() in case of fail.
3625 */
3626void perf_counter_free_task(struct task_struct *task)
3627{
3628 struct perf_counter_context *ctx = task->perf_counter_ctxp;
3629 struct perf_counter *counter, *tmp;
3630
3631 if (!ctx)
3632 return;
3633
3634 mutex_lock(&ctx->mutex);
3635again:
3636 list_for_each_entry_safe(counter, tmp, &ctx->counter_list, list_entry) {
3637 struct perf_counter *parent = counter->parent;
3638
3639 if (WARN_ON_ONCE(!parent))
3640 continue;
3641
3642 mutex_lock(&parent->child_mutex);
3643 list_del_init(&counter->child_list);
3644 mutex_unlock(&parent->child_mutex);
3645
3646 fput(parent->filp);
3647
3648 list_del_counter(counter, ctx);
3649 free_counter(counter);
3650 }
3651
3652 if (!list_empty(&ctx->counter_list))
3653 goto again;
3654
3655 mutex_unlock(&ctx->mutex);
3656
3657 put_ctx(ctx);
3658}
3659
3660/*
3661 * Initialize the perf_counter context in task_struct
3662 */
3663int perf_counter_init_task(struct task_struct *child)
3664{
3665 struct perf_counter_context *child_ctx, *parent_ctx;
3666 struct perf_counter_context *cloned_ctx;
3667 struct perf_counter *counter;
3668 struct task_struct *parent = current;
3669 int inherited_all = 1;
3670 u64 cloned_gen;
3671 int ret = 0;
3672
3673 child->perf_counter_ctxp = NULL;
3674
3675 mutex_init(&child->perf_counter_mutex);
3676 INIT_LIST_HEAD(&child->perf_counter_list);
3677
3678 if (likely(!parent->perf_counter_ctxp))
3679 return 0;
3680
3681 /*
3682 * This is executed from the parent task context, so inherit
3683 * counters that have been marked for cloning.
3684 * First allocate and initialize a context for the child.
3685 */
3686
3687 child_ctx = kmalloc(sizeof(struct perf_counter_context), GFP_KERNEL);
3688 if (!child_ctx)
3689 return -ENOMEM;
3690
3691 __perf_counter_init_context(child_ctx, child);
3692 child->perf_counter_ctxp = child_ctx;
3693 get_task_struct(child);
3694
3695 /*
3696 * If the parent's context is a clone, temporarily set its
3697 * parent_gen to an impossible value (all 1s) so it won't get
3698 * swapped under us. The rcu_read_lock makes sure that
3699 * parent_ctx continues to exist even if it gets swapped to
3700 * another process and then freed while we are trying to get
3701 * its lock.
3702 */
3703 rcu_read_lock();
3704 retry:
3705 parent_ctx = rcu_dereference(parent->perf_counter_ctxp);
3706 /*
3707 * No need to check if parent_ctx != NULL here; since we saw
3708 * it non-NULL earlier, the only reason for it to become NULL
3709 * is if we exit, and since we're currently in the middle of
3710 * a fork we can't be exiting at the same time.
3711 */
3712 spin_lock_irq(&parent_ctx->lock);
3713 if (parent_ctx != rcu_dereference(parent->perf_counter_ctxp)) {
3714 spin_unlock_irq(&parent_ctx->lock);
3715 goto retry;
3716 }
3717 cloned_gen = parent_ctx->parent_gen;
3718 if (parent_ctx->parent_ctx)
3719 parent_ctx->parent_gen = ~0ull;
3720 spin_unlock_irq(&parent_ctx->lock);
3721 rcu_read_unlock();
3722
3723 /*
3724 * Lock the parent list. No need to lock the child - not PID
3725 * hashed yet and not running, so nobody can access it.
3726 */
3727 mutex_lock(&parent_ctx->mutex);
3728
3729 /*
3730 * We dont have to disable NMIs - we are only looking at
3731 * the list, not manipulating it:
3732 */
3733 list_for_each_entry_rcu(counter, &parent_ctx->event_list, event_entry) {
3734 if (counter != counter->group_leader)
3735 continue;
3736
3737 if (!counter->hw_event.inherit) {
3738 inherited_all = 0;
3739 continue;
3740 }
3741
3742 ret = inherit_group(counter, parent, parent_ctx,
3743 child, child_ctx);
3744 if (ret) {
3745 inherited_all = 0;
3746 break;
3747 }
3748 }
3749
3750 if (inherited_all) {
3751 /*
3752 * Mark the child context as a clone of the parent
3753 * context, or of whatever the parent is a clone of.
3754 * Note that if the parent is a clone, it could get
3755 * uncloned at any point, but that doesn't matter
3756 * because the list of counters and the generation
3757 * count can't have changed since we took the mutex.
3758 */
3759 cloned_ctx = rcu_dereference(parent_ctx->parent_ctx);
3760 if (cloned_ctx) {
3761 child_ctx->parent_ctx = cloned_ctx;
3762 child_ctx->parent_gen = cloned_gen;
3763 } else {
3764 child_ctx->parent_ctx = parent_ctx;
3765 child_ctx->parent_gen = parent_ctx->generation;
3766 }
3767 get_ctx(child_ctx->parent_ctx);
3768 }
3769
3770 mutex_unlock(&parent_ctx->mutex);
3771
3772 /*
3773 * Restore the clone status of the parent.
3774 */
3775 if (parent_ctx->parent_ctx) {
3776 spin_lock_irq(&parent_ctx->lock);
3777 if (parent_ctx->parent_ctx)
3778 parent_ctx->parent_gen = cloned_gen;
3779 spin_unlock_irq(&parent_ctx->lock);
3780 }
3781
3782 return ret;
3783}
3784
3785static void __cpuinit perf_counter_init_cpu(int cpu)
3786{
3787 struct perf_cpu_context *cpuctx;
3788
3789 cpuctx = &per_cpu(perf_cpu_context, cpu);
3790 __perf_counter_init_context(&cpuctx->ctx, NULL);
3791
3792 spin_lock(&perf_resource_lock);
3793 cpuctx->max_pertask = perf_max_counters - perf_reserved_percpu;
3794 spin_unlock(&perf_resource_lock);
3795
3796 hw_perf_counter_setup(cpu);
3797}
3798
3799#ifdef CONFIG_HOTPLUG_CPU
3800static void __perf_counter_exit_cpu(void *info)
3801{
3802 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
3803 struct perf_counter_context *ctx = &cpuctx->ctx;
3804 struct perf_counter *counter, *tmp;
3805
3806 list_for_each_entry_safe(counter, tmp, &ctx->counter_list, list_entry)
3807 __perf_counter_remove_from_context(counter);
3808}
3809static void perf_counter_exit_cpu(int cpu)
3810{
3811 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
3812 struct perf_counter_context *ctx = &cpuctx->ctx;
3813
3814 mutex_lock(&ctx->mutex);
3815 smp_call_function_single(cpu, __perf_counter_exit_cpu, NULL, 1);
3816 mutex_unlock(&ctx->mutex);
3817}
3818#else
3819static inline void perf_counter_exit_cpu(int cpu) { }
3820#endif
3821
3822static int __cpuinit
3823perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
3824{
3825 unsigned int cpu = (long)hcpu;
3826
3827 switch (action) {
3828
3829 case CPU_UP_PREPARE:
3830 case CPU_UP_PREPARE_FROZEN:
3831 perf_counter_init_cpu(cpu);
3832 break;
3833
3834 case CPU_DOWN_PREPARE:
3835 case CPU_DOWN_PREPARE_FROZEN:
3836 perf_counter_exit_cpu(cpu);
3837 break;
3838
3839 default:
3840 break;
3841 }
3842
3843 return NOTIFY_OK;
3844}
3845
3846static struct notifier_block __cpuinitdata perf_cpu_nb = {
3847 .notifier_call = perf_cpu_notify,
3848};
3849
3850void __init perf_counter_init(void)
3851{
3852 perf_cpu_notify(&perf_cpu_nb, (unsigned long)CPU_UP_PREPARE,
3853 (void *)(long)smp_processor_id());
3854 register_cpu_notifier(&perf_cpu_nb);
3855}
3856
3857static ssize_t perf_show_reserve_percpu(struct sysdev_class *class, char *buf)
3858{
3859 return sprintf(buf, "%d\n", perf_reserved_percpu);
3860}
3861
3862static ssize_t
3863perf_set_reserve_percpu(struct sysdev_class *class,
3864 const char *buf,
3865 size_t count)
3866{
3867 struct perf_cpu_context *cpuctx;
3868 unsigned long val;
3869 int err, cpu, mpt;
3870
3871 err = strict_strtoul(buf, 10, &val);
3872 if (err)
3873 return err;
3874 if (val > perf_max_counters)
3875 return -EINVAL;
3876
3877 spin_lock(&perf_resource_lock);
3878 perf_reserved_percpu = val;
3879 for_each_online_cpu(cpu) {
3880 cpuctx = &per_cpu(perf_cpu_context, cpu);
3881 spin_lock_irq(&cpuctx->ctx.lock);
3882 mpt = min(perf_max_counters - cpuctx->ctx.nr_counters,
3883 perf_max_counters - perf_reserved_percpu);
3884 cpuctx->max_pertask = mpt;
3885 spin_unlock_irq(&cpuctx->ctx.lock);
3886 }
3887 spin_unlock(&perf_resource_lock);
3888
3889 return count;
3890}
3891
3892static ssize_t perf_show_overcommit(struct sysdev_class *class, char *buf)
3893{
3894 return sprintf(buf, "%d\n", perf_overcommit);
3895}
3896
3897static ssize_t
3898perf_set_overcommit(struct sysdev_class *class, const char *buf, size_t count)
3899{
3900 unsigned long val;
3901 int err;
3902
3903 err = strict_strtoul(buf, 10, &val);
3904 if (err)
3905 return err;
3906 if (val > 1)
3907 return -EINVAL;
3908
3909 spin_lock(&perf_resource_lock);
3910 perf_overcommit = val;
3911 spin_unlock(&perf_resource_lock);
3912
3913 return count;
3914}
3915
3916static SYSDEV_CLASS_ATTR(
3917 reserve_percpu,
3918 0644,
3919 perf_show_reserve_percpu,
3920 perf_set_reserve_percpu
3921 );
3922
3923static SYSDEV_CLASS_ATTR(
3924 overcommit,
3925 0644,
3926 perf_show_overcommit,
3927 perf_set_overcommit
3928 );
3929
3930static struct attribute *perfclass_attrs[] = {
3931 &attr_reserve_percpu.attr,
3932 &attr_overcommit.attr,
3933 NULL
3934};
3935
3936static struct attribute_group perfclass_attr_group = {
3937 .attrs = perfclass_attrs,
3938 .name = "perf_counters",
3939};
3940
3941static int __init perf_counter_sysfs_init(void)
3942{
3943 return sysfs_create_group(&cpu_sysdev_class.kset.kobj,
3944 &perfclass_attr_group);
3945}
3946device_initcall(perf_counter_sysfs_init);