2 * Performance counter core code
4 * Copyright(C) 2008 Thomas Gleixner <tglx@linutronix.de>
5 * Copyright(C) 2008 Red Hat, Inc., Ingo Molnar
7 * For licencing details see kernel-base/COPYING
11 #include <linux/cpu.h>
12 #include <linux/smp.h>
13 #include <linux/file.h>
14 #include <linux/poll.h>
15 #include <linux/sysfs.h>
16 #include <linux/ptrace.h>
17 #include <linux/percpu.h>
18 #include <linux/uaccess.h>
19 #include <linux/syscalls.h>
20 #include <linux/anon_inodes.h>
21 #include <linux/kernel_stat.h>
22 #include <linux/perf_counter.h>
24 #include <linux/vmstat.h>
27 * Each CPU has a list of per CPU counters:
29 DEFINE_PER_CPU(struct perf_cpu_context, perf_cpu_context);
31 int perf_max_counters __read_mostly = 1;
32 static int perf_reserved_percpu __read_mostly;
33 static int perf_overcommit __read_mostly = 1;
36 * Mutex for (sysadmin-configurable) counter reservations:
38 static DEFINE_MUTEX(perf_resource_mutex);
41 * Architecture provided APIs - weak aliases:
43 extern __weak const struct hw_perf_counter_ops *
44 hw_perf_counter_init(struct perf_counter *counter)
49 u64 __weak hw_perf_save_disable(void) { return 0; }
50 void __weak hw_perf_restore(u64 ctrl) { barrier(); }
51 void __weak hw_perf_counter_setup(int cpu) { barrier(); }
52 int __weak hw_perf_group_sched_in(struct perf_counter *group_leader,
53 struct perf_cpu_context *cpuctx,
54 struct perf_counter_context *ctx, int cpu)
59 void __weak perf_counter_print_debug(void) { }
62 list_add_counter(struct perf_counter *counter, struct perf_counter_context *ctx)
64 struct perf_counter *group_leader = counter->group_leader;
67 * Depending on whether it is a standalone or sibling counter,
68 * add it straight to the context's counter list, or to the group
69 * leader's sibling list:
71 if (counter->group_leader == counter)
72 list_add_tail(&counter->list_entry, &ctx->counter_list);
74 list_add_tail(&counter->list_entry, &group_leader->sibling_list);
78 list_del_counter(struct perf_counter *counter, struct perf_counter_context *ctx)
80 struct perf_counter *sibling, *tmp;
82 list_del_init(&counter->list_entry);
85 * If this was a group counter with sibling counters then
86 * upgrade the siblings to singleton counters by adding them
87 * to the context list directly:
89 list_for_each_entry_safe(sibling, tmp,
90 &counter->sibling_list, list_entry) {
92 list_del_init(&sibling->list_entry);
93 list_add_tail(&sibling->list_entry, &ctx->counter_list);
94 sibling->group_leader = sibling;
99 counter_sched_out(struct perf_counter *counter,
100 struct perf_cpu_context *cpuctx,
101 struct perf_counter_context *ctx)
103 if (counter->state != PERF_COUNTER_STATE_ACTIVE)
106 counter->state = PERF_COUNTER_STATE_INACTIVE;
107 counter->hw_ops->disable(counter);
110 if (!is_software_counter(counter))
111 cpuctx->active_oncpu--;
113 if (counter->hw_event.exclusive || !cpuctx->active_oncpu)
114 cpuctx->exclusive = 0;
118 group_sched_out(struct perf_counter *group_counter,
119 struct perf_cpu_context *cpuctx,
120 struct perf_counter_context *ctx)
122 struct perf_counter *counter;
124 if (group_counter->state != PERF_COUNTER_STATE_ACTIVE)
127 counter_sched_out(group_counter, cpuctx, ctx);
130 * Schedule out siblings (if any):
132 list_for_each_entry(counter, &group_counter->sibling_list, list_entry)
133 counter_sched_out(counter, cpuctx, ctx);
135 if (group_counter->hw_event.exclusive)
136 cpuctx->exclusive = 0;
140 * Cross CPU call to remove a performance counter
142 * We disable the counter on the hardware level first. After that we
143 * remove it from the context list.
145 static void __perf_counter_remove_from_context(void *info)
147 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
148 struct perf_counter *counter = info;
149 struct perf_counter_context *ctx = counter->ctx;
154 * If this is a task context, we need to check whether it is
155 * the current task context of this cpu. If not it has been
156 * scheduled out before the smp call arrived.
158 if (ctx->task && cpuctx->task_ctx != ctx)
161 curr_rq_lock_irq_save(&flags);
162 spin_lock(&ctx->lock);
164 counter_sched_out(counter, cpuctx, ctx);
166 counter->task = NULL;
170 * Protect the list operation against NMI by disabling the
171 * counters on a global level. NOP for non NMI based counters.
173 perf_flags = hw_perf_save_disable();
174 list_del_counter(counter, ctx);
175 hw_perf_restore(perf_flags);
179 * Allow more per task counters with respect to the
182 cpuctx->max_pertask =
183 min(perf_max_counters - ctx->nr_counters,
184 perf_max_counters - perf_reserved_percpu);
187 spin_unlock(&ctx->lock);
188 curr_rq_unlock_irq_restore(&flags);
193 * Remove the counter from a task's (or a CPU's) list of counters.
195 * Must be called with counter->mutex and ctx->mutex held.
197 * CPU counters are removed with a smp call. For task counters we only
198 * call when the task is on a CPU.
200 static void perf_counter_remove_from_context(struct perf_counter *counter)
202 struct perf_counter_context *ctx = counter->ctx;
203 struct task_struct *task = ctx->task;
207 * Per cpu counters are removed via an smp call and
208 * the removal is always sucessful.
210 smp_call_function_single(counter->cpu,
211 __perf_counter_remove_from_context,
217 task_oncpu_function_call(task, __perf_counter_remove_from_context,
220 spin_lock_irq(&ctx->lock);
222 * If the context is active we need to retry the smp call.
224 if (ctx->nr_active && !list_empty(&counter->list_entry)) {
225 spin_unlock_irq(&ctx->lock);
230 * The lock prevents that this context is scheduled in so we
231 * can remove the counter safely, if the call above did not
234 if (!list_empty(&counter->list_entry)) {
236 list_del_counter(counter, ctx);
237 counter->task = NULL;
239 spin_unlock_irq(&ctx->lock);
243 * Cross CPU call to disable a performance counter
245 static void __perf_counter_disable(void *info)
247 struct perf_counter *counter = info;
248 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
249 struct perf_counter_context *ctx = counter->ctx;
253 * If this is a per-task counter, need to check whether this
254 * counter's task is the current task on this cpu.
256 if (ctx->task && cpuctx->task_ctx != ctx)
259 curr_rq_lock_irq_save(&flags);
260 spin_lock(&ctx->lock);
263 * If the counter is on, turn it off.
264 * If it is in error state, leave it in error state.
266 if (counter->state >= PERF_COUNTER_STATE_INACTIVE) {
267 if (counter == counter->group_leader)
268 group_sched_out(counter, cpuctx, ctx);
270 counter_sched_out(counter, cpuctx, ctx);
271 counter->state = PERF_COUNTER_STATE_OFF;
274 spin_unlock(&ctx->lock);
275 curr_rq_unlock_irq_restore(&flags);
281 static void perf_counter_disable(struct perf_counter *counter)
283 struct perf_counter_context *ctx = counter->ctx;
284 struct task_struct *task = ctx->task;
288 * Disable the counter on the cpu that it's on
290 smp_call_function_single(counter->cpu, __perf_counter_disable,
296 task_oncpu_function_call(task, __perf_counter_disable, counter);
298 spin_lock_irq(&ctx->lock);
300 * If the counter is still active, we need to retry the cross-call.
302 if (counter->state == PERF_COUNTER_STATE_ACTIVE) {
303 spin_unlock_irq(&ctx->lock);
308 * Since we have the lock this context can't be scheduled
309 * in, so we can change the state safely.
311 if (counter->state == PERF_COUNTER_STATE_INACTIVE)
312 counter->state = PERF_COUNTER_STATE_OFF;
314 spin_unlock_irq(&ctx->lock);
318 * Disable a counter and all its children.
320 static void perf_counter_disable_family(struct perf_counter *counter)
322 struct perf_counter *child;
324 perf_counter_disable(counter);
327 * Lock the mutex to protect the list of children
329 mutex_lock(&counter->mutex);
330 list_for_each_entry(child, &counter->child_list, child_list)
331 perf_counter_disable(child);
332 mutex_unlock(&counter->mutex);
336 counter_sched_in(struct perf_counter *counter,
337 struct perf_cpu_context *cpuctx,
338 struct perf_counter_context *ctx,
341 if (counter->state <= PERF_COUNTER_STATE_OFF)
344 counter->state = PERF_COUNTER_STATE_ACTIVE;
345 counter->oncpu = cpu; /* TODO: put 'cpu' into cpuctx->cpu */
347 * The new state must be visible before we turn it on in the hardware:
351 if (counter->hw_ops->enable(counter)) {
352 counter->state = PERF_COUNTER_STATE_INACTIVE;
357 if (!is_software_counter(counter))
358 cpuctx->active_oncpu++;
361 if (counter->hw_event.exclusive)
362 cpuctx->exclusive = 1;
368 * Return 1 for a group consisting entirely of software counters,
369 * 0 if the group contains any hardware counters.
371 static int is_software_only_group(struct perf_counter *leader)
373 struct perf_counter *counter;
375 if (!is_software_counter(leader))
377 list_for_each_entry(counter, &leader->sibling_list, list_entry)
378 if (!is_software_counter(counter))
384 * Work out whether we can put this counter group on the CPU now.
386 static int group_can_go_on(struct perf_counter *counter,
387 struct perf_cpu_context *cpuctx,
391 * Groups consisting entirely of software counters can always go on.
393 if (is_software_only_group(counter))
396 * If an exclusive group is already on, no other hardware
397 * counters can go on.
399 if (cpuctx->exclusive)
402 * If this group is exclusive and there are already
403 * counters on the CPU, it can't go on.
405 if (counter->hw_event.exclusive && cpuctx->active_oncpu)
408 * Otherwise, try to add it if all previous groups were able
415 * Cross CPU call to install and enable a performance counter
417 static void __perf_install_in_context(void *info)
419 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
420 struct perf_counter *counter = info;
421 struct perf_counter_context *ctx = counter->ctx;
422 struct perf_counter *leader = counter->group_leader;
423 int cpu = smp_processor_id();
429 * If this is a task context, we need to check whether it is
430 * the current task context of this cpu. If not it has been
431 * scheduled out before the smp call arrived.
433 if (ctx->task && cpuctx->task_ctx != ctx)
436 curr_rq_lock_irq_save(&flags);
437 spin_lock(&ctx->lock);
440 * Protect the list operation against NMI by disabling the
441 * counters on a global level. NOP for non NMI based counters.
443 perf_flags = hw_perf_save_disable();
445 list_add_counter(counter, ctx);
447 counter->prev_state = PERF_COUNTER_STATE_OFF;
450 * Don't put the counter on if it is disabled or if
451 * it is in a group and the group isn't on.
453 if (counter->state != PERF_COUNTER_STATE_INACTIVE ||
454 (leader != counter && leader->state != PERF_COUNTER_STATE_ACTIVE))
458 * An exclusive counter can't go on if there are already active
459 * hardware counters, and no hardware counter can go on if there
460 * is already an exclusive counter on.
462 if (!group_can_go_on(counter, cpuctx, 1))
465 err = counter_sched_in(counter, cpuctx, ctx, cpu);
469 * This counter couldn't go on. If it is in a group
470 * then we have to pull the whole group off.
471 * If the counter group is pinned then put it in error state.
473 if (leader != counter)
474 group_sched_out(leader, cpuctx, ctx);
475 if (leader->hw_event.pinned)
476 leader->state = PERF_COUNTER_STATE_ERROR;
479 if (!err && !ctx->task && cpuctx->max_pertask)
480 cpuctx->max_pertask--;
483 hw_perf_restore(perf_flags);
485 spin_unlock(&ctx->lock);
486 curr_rq_unlock_irq_restore(&flags);
490 * Attach a performance counter to a context
492 * First we add the counter to the list with the hardware enable bit
493 * in counter->hw_config cleared.
495 * If the counter is attached to a task which is on a CPU we use a smp
496 * call to enable it in the task context. The task might have been
497 * scheduled away, but we check this in the smp call again.
499 * Must be called with ctx->mutex held.
502 perf_install_in_context(struct perf_counter_context *ctx,
503 struct perf_counter *counter,
506 struct task_struct *task = ctx->task;
510 * Per cpu counters are installed via an smp call and
511 * the install is always sucessful.
513 smp_call_function_single(cpu, __perf_install_in_context,
518 counter->task = task;
520 task_oncpu_function_call(task, __perf_install_in_context,
523 spin_lock_irq(&ctx->lock);
525 * we need to retry the smp call.
527 if (ctx->is_active && list_empty(&counter->list_entry)) {
528 spin_unlock_irq(&ctx->lock);
533 * The lock prevents that this context is scheduled in so we
534 * can add the counter safely, if it the call above did not
537 if (list_empty(&counter->list_entry)) {
538 list_add_counter(counter, ctx);
541 spin_unlock_irq(&ctx->lock);
545 * Cross CPU call to enable a performance counter
547 static void __perf_counter_enable(void *info)
549 struct perf_counter *counter = info;
550 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
551 struct perf_counter_context *ctx = counter->ctx;
552 struct perf_counter *leader = counter->group_leader;
557 * If this is a per-task counter, need to check whether this
558 * counter's task is the current task on this cpu.
560 if (ctx->task && cpuctx->task_ctx != ctx)
563 curr_rq_lock_irq_save(&flags);
564 spin_lock(&ctx->lock);
566 counter->prev_state = counter->state;
567 if (counter->state >= PERF_COUNTER_STATE_INACTIVE)
569 counter->state = PERF_COUNTER_STATE_INACTIVE;
572 * If the counter is in a group and isn't the group leader,
573 * then don't put it on unless the group is on.
575 if (leader != counter && leader->state != PERF_COUNTER_STATE_ACTIVE)
578 if (!group_can_go_on(counter, cpuctx, 1))
581 err = counter_sched_in(counter, cpuctx, ctx,
586 * If this counter can't go on and it's part of a
587 * group, then the whole group has to come off.
589 if (leader != counter)
590 group_sched_out(leader, cpuctx, ctx);
591 if (leader->hw_event.pinned)
592 leader->state = PERF_COUNTER_STATE_ERROR;
596 spin_unlock(&ctx->lock);
597 curr_rq_unlock_irq_restore(&flags);
603 static void perf_counter_enable(struct perf_counter *counter)
605 struct perf_counter_context *ctx = counter->ctx;
606 struct task_struct *task = ctx->task;
610 * Enable the counter on the cpu that it's on
612 smp_call_function_single(counter->cpu, __perf_counter_enable,
617 spin_lock_irq(&ctx->lock);
618 if (counter->state >= PERF_COUNTER_STATE_INACTIVE)
622 * If the counter is in error state, clear that first.
623 * That way, if we see the counter in error state below, we
624 * know that it has gone back into error state, as distinct
625 * from the task having been scheduled away before the
626 * cross-call arrived.
628 if (counter->state == PERF_COUNTER_STATE_ERROR)
629 counter->state = PERF_COUNTER_STATE_OFF;
632 spin_unlock_irq(&ctx->lock);
633 task_oncpu_function_call(task, __perf_counter_enable, counter);
635 spin_lock_irq(&ctx->lock);
638 * If the context is active and the counter is still off,
639 * we need to retry the cross-call.
641 if (ctx->is_active && counter->state == PERF_COUNTER_STATE_OFF)
645 * Since we have the lock this context can't be scheduled
646 * in, so we can change the state safely.
648 if (counter->state == PERF_COUNTER_STATE_OFF)
649 counter->state = PERF_COUNTER_STATE_INACTIVE;
651 spin_unlock_irq(&ctx->lock);
655 * Enable a counter and all its children.
657 static void perf_counter_enable_family(struct perf_counter *counter)
659 struct perf_counter *child;
661 perf_counter_enable(counter);
664 * Lock the mutex to protect the list of children
666 mutex_lock(&counter->mutex);
667 list_for_each_entry(child, &counter->child_list, child_list)
668 perf_counter_enable(child);
669 mutex_unlock(&counter->mutex);
672 void __perf_counter_sched_out(struct perf_counter_context *ctx,
673 struct perf_cpu_context *cpuctx)
675 struct perf_counter *counter;
678 spin_lock(&ctx->lock);
680 if (likely(!ctx->nr_counters))
683 flags = hw_perf_save_disable();
684 if (ctx->nr_active) {
685 list_for_each_entry(counter, &ctx->counter_list, list_entry)
686 group_sched_out(counter, cpuctx, ctx);
688 hw_perf_restore(flags);
690 spin_unlock(&ctx->lock);
694 * Called from scheduler to remove the counters of the current task,
695 * with interrupts disabled.
697 * We stop each counter and update the counter value in counter->count.
699 * This does not protect us against NMI, but disable()
700 * sets the disabled bit in the control field of counter _before_
701 * accessing the counter control register. If a NMI hits, then it will
702 * not restart the counter.
704 void perf_counter_task_sched_out(struct task_struct *task, int cpu)
706 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
707 struct perf_counter_context *ctx = &task->perf_counter_ctx;
709 if (likely(!cpuctx->task_ctx))
712 __perf_counter_sched_out(ctx, cpuctx);
714 cpuctx->task_ctx = NULL;
717 static void perf_counter_cpu_sched_out(struct perf_cpu_context *cpuctx)
719 __perf_counter_sched_out(&cpuctx->ctx, cpuctx);
723 group_sched_in(struct perf_counter *group_counter,
724 struct perf_cpu_context *cpuctx,
725 struct perf_counter_context *ctx,
728 struct perf_counter *counter, *partial_group;
731 if (group_counter->state == PERF_COUNTER_STATE_OFF)
734 ret = hw_perf_group_sched_in(group_counter, cpuctx, ctx, cpu);
736 return ret < 0 ? ret : 0;
738 group_counter->prev_state = group_counter->state;
739 if (counter_sched_in(group_counter, cpuctx, ctx, cpu))
743 * Schedule in siblings as one group (if any):
745 list_for_each_entry(counter, &group_counter->sibling_list, list_entry) {
746 counter->prev_state = counter->state;
747 if (counter_sched_in(counter, cpuctx, ctx, cpu)) {
748 partial_group = counter;
757 * Groups can be scheduled in as one unit only, so undo any
758 * partial group before returning:
760 list_for_each_entry(counter, &group_counter->sibling_list, list_entry) {
761 if (counter == partial_group)
763 counter_sched_out(counter, cpuctx, ctx);
765 counter_sched_out(group_counter, cpuctx, ctx);
771 __perf_counter_sched_in(struct perf_counter_context *ctx,
772 struct perf_cpu_context *cpuctx, int cpu)
774 struct perf_counter *counter;
778 spin_lock(&ctx->lock);
780 if (likely(!ctx->nr_counters))
783 flags = hw_perf_save_disable();
786 * First go through the list and put on any pinned groups
787 * in order to give them the best chance of going on.
789 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
790 if (counter->state <= PERF_COUNTER_STATE_OFF ||
791 !counter->hw_event.pinned)
793 if (counter->cpu != -1 && counter->cpu != cpu)
796 if (group_can_go_on(counter, cpuctx, 1))
797 group_sched_in(counter, cpuctx, ctx, cpu);
800 * If this pinned group hasn't been scheduled,
801 * put it in error state.
803 if (counter->state == PERF_COUNTER_STATE_INACTIVE)
804 counter->state = PERF_COUNTER_STATE_ERROR;
807 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
809 * Ignore counters in OFF or ERROR state, and
810 * ignore pinned counters since we did them already.
812 if (counter->state <= PERF_COUNTER_STATE_OFF ||
813 counter->hw_event.pinned)
817 * Listen to the 'cpu' scheduling filter constraint
820 if (counter->cpu != -1 && counter->cpu != cpu)
823 if (group_can_go_on(counter, cpuctx, can_add_hw)) {
824 if (group_sched_in(counter, cpuctx, ctx, cpu))
828 hw_perf_restore(flags);
830 spin_unlock(&ctx->lock);
834 * Called from scheduler to add the counters of the current task
835 * with interrupts disabled.
837 * We restore the counter value and then enable it.
839 * This does not protect us against NMI, but enable()
840 * sets the enabled bit in the control field of counter _before_
841 * accessing the counter control register. If a NMI hits, then it will
842 * keep the counter running.
844 void perf_counter_task_sched_in(struct task_struct *task, int cpu)
846 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
847 struct perf_counter_context *ctx = &task->perf_counter_ctx;
849 __perf_counter_sched_in(ctx, cpuctx, cpu);
850 cpuctx->task_ctx = ctx;
853 static void perf_counter_cpu_sched_in(struct perf_cpu_context *cpuctx, int cpu)
855 struct perf_counter_context *ctx = &cpuctx->ctx;
857 __perf_counter_sched_in(ctx, cpuctx, cpu);
860 int perf_counter_task_disable(void)
862 struct task_struct *curr = current;
863 struct perf_counter_context *ctx = &curr->perf_counter_ctx;
864 struct perf_counter *counter;
869 if (likely(!ctx->nr_counters))
872 curr_rq_lock_irq_save(&flags);
873 cpu = smp_processor_id();
875 /* force the update of the task clock: */
876 __task_delta_exec(curr, 1);
878 perf_counter_task_sched_out(curr, cpu);
880 spin_lock(&ctx->lock);
883 * Disable all the counters:
885 perf_flags = hw_perf_save_disable();
887 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
888 if (counter->state != PERF_COUNTER_STATE_ERROR)
889 counter->state = PERF_COUNTER_STATE_OFF;
892 hw_perf_restore(perf_flags);
894 spin_unlock(&ctx->lock);
896 curr_rq_unlock_irq_restore(&flags);
901 int perf_counter_task_enable(void)
903 struct task_struct *curr = current;
904 struct perf_counter_context *ctx = &curr->perf_counter_ctx;
905 struct perf_counter *counter;
910 if (likely(!ctx->nr_counters))
913 curr_rq_lock_irq_save(&flags);
914 cpu = smp_processor_id();
916 /* force the update of the task clock: */
917 __task_delta_exec(curr, 1);
919 perf_counter_task_sched_out(curr, cpu);
921 spin_lock(&ctx->lock);
924 * Disable all the counters:
926 perf_flags = hw_perf_save_disable();
928 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
929 if (counter->state > PERF_COUNTER_STATE_OFF)
931 counter->state = PERF_COUNTER_STATE_INACTIVE;
932 counter->hw_event.disabled = 0;
934 hw_perf_restore(perf_flags);
936 spin_unlock(&ctx->lock);
938 perf_counter_task_sched_in(curr, cpu);
940 curr_rq_unlock_irq_restore(&flags);
946 * Round-robin a context's counters:
948 static void rotate_ctx(struct perf_counter_context *ctx)
950 struct perf_counter *counter;
953 if (!ctx->nr_counters)
956 spin_lock(&ctx->lock);
958 * Rotate the first entry last (works just fine for group counters too):
960 perf_flags = hw_perf_save_disable();
961 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
962 list_del(&counter->list_entry);
963 list_add_tail(&counter->list_entry, &ctx->counter_list);
966 hw_perf_restore(perf_flags);
968 spin_unlock(&ctx->lock);
971 void perf_counter_task_tick(struct task_struct *curr, int cpu)
973 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
974 struct perf_counter_context *ctx = &curr->perf_counter_ctx;
975 const int rotate_percpu = 0;
978 perf_counter_cpu_sched_out(cpuctx);
979 perf_counter_task_sched_out(curr, cpu);
982 rotate_ctx(&cpuctx->ctx);
986 perf_counter_cpu_sched_in(cpuctx, cpu);
987 perf_counter_task_sched_in(curr, cpu);
991 * Cross CPU call to read the hardware counter
993 static void __read(void *info)
995 struct perf_counter *counter = info;
998 curr_rq_lock_irq_save(&flags);
999 counter->hw_ops->read(counter);
1000 curr_rq_unlock_irq_restore(&flags);
1003 static u64 perf_counter_read(struct perf_counter *counter)
1006 * If counter is enabled and currently active on a CPU, update the
1007 * value in the counter structure:
1009 if (counter->state == PERF_COUNTER_STATE_ACTIVE) {
1010 smp_call_function_single(counter->oncpu,
1011 __read, counter, 1);
1014 return atomic64_read(&counter->count);
1018 * Cross CPU call to switch performance data pointers
1020 static void __perf_switch_irq_data(void *info)
1022 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
1023 struct perf_counter *counter = info;
1024 struct perf_counter_context *ctx = counter->ctx;
1025 struct perf_data *oldirqdata = counter->irqdata;
1028 * If this is a task context, we need to check whether it is
1029 * the current task context of this cpu. If not it has been
1030 * scheduled out before the smp call arrived.
1033 if (cpuctx->task_ctx != ctx)
1035 spin_lock(&ctx->lock);
1038 /* Change the pointer NMI safe */
1039 atomic_long_set((atomic_long_t *)&counter->irqdata,
1040 (unsigned long) counter->usrdata);
1041 counter->usrdata = oldirqdata;
1044 spin_unlock(&ctx->lock);
1047 static struct perf_data *perf_switch_irq_data(struct perf_counter *counter)
1049 struct perf_counter_context *ctx = counter->ctx;
1050 struct perf_data *oldirqdata = counter->irqdata;
1051 struct task_struct *task = ctx->task;
1054 smp_call_function_single(counter->cpu,
1055 __perf_switch_irq_data,
1057 return counter->usrdata;
1061 spin_lock_irq(&ctx->lock);
1062 if (counter->state != PERF_COUNTER_STATE_ACTIVE) {
1063 counter->irqdata = counter->usrdata;
1064 counter->usrdata = oldirqdata;
1065 spin_unlock_irq(&ctx->lock);
1068 spin_unlock_irq(&ctx->lock);
1069 task_oncpu_function_call(task, __perf_switch_irq_data, counter);
1070 /* Might have failed, because task was scheduled out */
1071 if (counter->irqdata == oldirqdata)
1074 return counter->usrdata;
1077 static void put_context(struct perf_counter_context *ctx)
1080 put_task_struct(ctx->task);
1083 static struct perf_counter_context *find_get_context(pid_t pid, int cpu)
1085 struct perf_cpu_context *cpuctx;
1086 struct perf_counter_context *ctx;
1087 struct task_struct *task;
1090 * If cpu is not a wildcard then this is a percpu counter:
1093 /* Must be root to operate on a CPU counter: */
1094 if (!capable(CAP_SYS_ADMIN))
1095 return ERR_PTR(-EACCES);
1097 if (cpu < 0 || cpu > num_possible_cpus())
1098 return ERR_PTR(-EINVAL);
1101 * We could be clever and allow to attach a counter to an
1102 * offline CPU and activate it when the CPU comes up, but
1105 if (!cpu_isset(cpu, cpu_online_map))
1106 return ERR_PTR(-ENODEV);
1108 cpuctx = &per_cpu(perf_cpu_context, cpu);
1118 task = find_task_by_vpid(pid);
1120 get_task_struct(task);
1124 return ERR_PTR(-ESRCH);
1126 ctx = &task->perf_counter_ctx;
1129 /* Reuse ptrace permission checks for now. */
1130 if (!ptrace_may_access(task, PTRACE_MODE_READ)) {
1132 return ERR_PTR(-EACCES);
1139 * Called when the last reference to the file is gone.
1141 static int perf_release(struct inode *inode, struct file *file)
1143 struct perf_counter *counter = file->private_data;
1144 struct perf_counter_context *ctx = counter->ctx;
1146 file->private_data = NULL;
1148 mutex_lock(&ctx->mutex);
1149 mutex_lock(&counter->mutex);
1151 perf_counter_remove_from_context(counter);
1153 mutex_unlock(&counter->mutex);
1154 mutex_unlock(&ctx->mutex);
1163 * Read the performance counter - simple non blocking version for now
1166 perf_read_hw(struct perf_counter *counter, char __user *buf, size_t count)
1170 if (count != sizeof(cntval))
1174 * Return end-of-file for a read on a counter that is in
1175 * error state (i.e. because it was pinned but it couldn't be
1176 * scheduled on to the CPU at some point).
1178 if (counter->state == PERF_COUNTER_STATE_ERROR)
1181 mutex_lock(&counter->mutex);
1182 cntval = perf_counter_read(counter);
1183 mutex_unlock(&counter->mutex);
1185 return put_user(cntval, (u64 __user *) buf) ? -EFAULT : sizeof(cntval);
1189 perf_copy_usrdata(struct perf_data *usrdata, char __user *buf, size_t count)
1194 count = min(count, (size_t)usrdata->len);
1195 if (copy_to_user(buf, usrdata->data + usrdata->rd_idx, count))
1198 /* Adjust the counters */
1199 usrdata->len -= count;
1201 usrdata->rd_idx = 0;
1203 usrdata->rd_idx += count;
1209 perf_read_irq_data(struct perf_counter *counter,
1214 struct perf_data *irqdata, *usrdata;
1215 DECLARE_WAITQUEUE(wait, current);
1218 irqdata = counter->irqdata;
1219 usrdata = counter->usrdata;
1221 if (usrdata->len + irqdata->len >= count)
1227 spin_lock_irq(&counter->waitq.lock);
1228 __add_wait_queue(&counter->waitq, &wait);
1230 set_current_state(TASK_INTERRUPTIBLE);
1231 if (usrdata->len + irqdata->len >= count)
1234 if (signal_pending(current))
1237 if (counter->state == PERF_COUNTER_STATE_ERROR)
1240 spin_unlock_irq(&counter->waitq.lock);
1242 spin_lock_irq(&counter->waitq.lock);
1244 __remove_wait_queue(&counter->waitq, &wait);
1245 __set_current_state(TASK_RUNNING);
1246 spin_unlock_irq(&counter->waitq.lock);
1248 if (usrdata->len + irqdata->len < count &&
1249 counter->state != PERF_COUNTER_STATE_ERROR)
1250 return -ERESTARTSYS;
1252 mutex_lock(&counter->mutex);
1254 /* Drain pending data first: */
1255 res = perf_copy_usrdata(usrdata, buf, count);
1256 if (res < 0 || res == count)
1259 /* Switch irq buffer: */
1260 usrdata = perf_switch_irq_data(counter);
1261 res2 = perf_copy_usrdata(usrdata, buf + res, count - res);
1269 mutex_unlock(&counter->mutex);
1275 perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
1277 struct perf_counter *counter = file->private_data;
1279 switch (counter->hw_event.record_type) {
1280 case PERF_RECORD_SIMPLE:
1281 return perf_read_hw(counter, buf, count);
1283 case PERF_RECORD_IRQ:
1284 case PERF_RECORD_GROUP:
1285 return perf_read_irq_data(counter, buf, count,
1286 file->f_flags & O_NONBLOCK);
1291 static unsigned int perf_poll(struct file *file, poll_table *wait)
1293 struct perf_counter *counter = file->private_data;
1294 unsigned int events = 0;
1295 unsigned long flags;
1297 poll_wait(file, &counter->waitq, wait);
1299 spin_lock_irqsave(&counter->waitq.lock, flags);
1300 if (counter->usrdata->len || counter->irqdata->len)
1302 spin_unlock_irqrestore(&counter->waitq.lock, flags);
1307 static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1309 struct perf_counter *counter = file->private_data;
1313 case PERF_COUNTER_IOC_ENABLE:
1314 perf_counter_enable_family(counter);
1316 case PERF_COUNTER_IOC_DISABLE:
1317 perf_counter_disable_family(counter);
1325 static const struct file_operations perf_fops = {
1326 .release = perf_release,
1329 .unlocked_ioctl = perf_ioctl,
1330 .compat_ioctl = perf_ioctl,
1333 static int cpu_clock_perf_counter_enable(struct perf_counter *counter)
1335 int cpu = raw_smp_processor_id();
1337 atomic64_set(&counter->hw.prev_count, cpu_clock(cpu));
1341 static void cpu_clock_perf_counter_update(struct perf_counter *counter)
1343 int cpu = raw_smp_processor_id();
1347 now = cpu_clock(cpu);
1348 prev = atomic64_read(&counter->hw.prev_count);
1349 atomic64_set(&counter->hw.prev_count, now);
1350 atomic64_add(now - prev, &counter->count);
1353 static void cpu_clock_perf_counter_disable(struct perf_counter *counter)
1355 cpu_clock_perf_counter_update(counter);
1358 static void cpu_clock_perf_counter_read(struct perf_counter *counter)
1360 cpu_clock_perf_counter_update(counter);
1363 static const struct hw_perf_counter_ops perf_ops_cpu_clock = {
1364 .enable = cpu_clock_perf_counter_enable,
1365 .disable = cpu_clock_perf_counter_disable,
1366 .read = cpu_clock_perf_counter_read,
1370 * Called from within the scheduler:
1372 static u64 task_clock_perf_counter_val(struct perf_counter *counter, int update)
1374 struct task_struct *curr = counter->task;
1377 delta = __task_delta_exec(curr, update);
1379 return curr->se.sum_exec_runtime + delta;
1382 static void task_clock_perf_counter_update(struct perf_counter *counter, u64 now)
1387 prev = atomic64_read(&counter->hw.prev_count);
1389 atomic64_set(&counter->hw.prev_count, now);
1393 atomic64_add(delta, &counter->count);
1396 static void task_clock_perf_counter_read(struct perf_counter *counter)
1398 u64 now = task_clock_perf_counter_val(counter, 1);
1400 task_clock_perf_counter_update(counter, now);
1403 static int task_clock_perf_counter_enable(struct perf_counter *counter)
1405 if (counter->prev_state <= PERF_COUNTER_STATE_OFF)
1406 atomic64_set(&counter->hw.prev_count,
1407 task_clock_perf_counter_val(counter, 0));
1412 static void task_clock_perf_counter_disable(struct perf_counter *counter)
1414 u64 now = task_clock_perf_counter_val(counter, 0);
1416 task_clock_perf_counter_update(counter, now);
1419 static const struct hw_perf_counter_ops perf_ops_task_clock = {
1420 .enable = task_clock_perf_counter_enable,
1421 .disable = task_clock_perf_counter_disable,
1422 .read = task_clock_perf_counter_read,
1425 #ifdef CONFIG_VM_EVENT_COUNTERS
1426 #define cpu_page_faults() __get_cpu_var(vm_event_states).event[PGFAULT]
1428 #define cpu_page_faults() 0
1431 static u64 get_page_faults(struct perf_counter *counter)
1433 struct task_struct *curr = counter->ctx->task;
1436 return curr->maj_flt + curr->min_flt;
1437 return cpu_page_faults();
1440 static void page_faults_perf_counter_update(struct perf_counter *counter)
1445 prev = atomic64_read(&counter->hw.prev_count);
1446 now = get_page_faults(counter);
1448 atomic64_set(&counter->hw.prev_count, now);
1452 atomic64_add(delta, &counter->count);
1455 static void page_faults_perf_counter_read(struct perf_counter *counter)
1457 page_faults_perf_counter_update(counter);
1460 static int page_faults_perf_counter_enable(struct perf_counter *counter)
1462 if (counter->prev_state <= PERF_COUNTER_STATE_OFF)
1463 atomic64_set(&counter->hw.prev_count, get_page_faults(counter));
1467 static void page_faults_perf_counter_disable(struct perf_counter *counter)
1469 page_faults_perf_counter_update(counter);
1472 static const struct hw_perf_counter_ops perf_ops_page_faults = {
1473 .enable = page_faults_perf_counter_enable,
1474 .disable = page_faults_perf_counter_disable,
1475 .read = page_faults_perf_counter_read,
1478 static u64 get_context_switches(struct perf_counter *counter)
1480 struct task_struct *curr = counter->ctx->task;
1483 return curr->nvcsw + curr->nivcsw;
1484 return cpu_nr_switches(smp_processor_id());
1487 static void context_switches_perf_counter_update(struct perf_counter *counter)
1492 prev = atomic64_read(&counter->hw.prev_count);
1493 now = get_context_switches(counter);
1495 atomic64_set(&counter->hw.prev_count, now);
1499 atomic64_add(delta, &counter->count);
1502 static void context_switches_perf_counter_read(struct perf_counter *counter)
1504 context_switches_perf_counter_update(counter);
1507 static int context_switches_perf_counter_enable(struct perf_counter *counter)
1509 if (counter->prev_state <= PERF_COUNTER_STATE_OFF)
1510 atomic64_set(&counter->hw.prev_count,
1511 get_context_switches(counter));
1515 static void context_switches_perf_counter_disable(struct perf_counter *counter)
1517 context_switches_perf_counter_update(counter);
1520 static const struct hw_perf_counter_ops perf_ops_context_switches = {
1521 .enable = context_switches_perf_counter_enable,
1522 .disable = context_switches_perf_counter_disable,
1523 .read = context_switches_perf_counter_read,
1526 static inline u64 get_cpu_migrations(struct perf_counter *counter)
1528 struct task_struct *curr = counter->ctx->task;
1531 return curr->se.nr_migrations;
1532 return cpu_nr_migrations(smp_processor_id());
1535 static void cpu_migrations_perf_counter_update(struct perf_counter *counter)
1540 prev = atomic64_read(&counter->hw.prev_count);
1541 now = get_cpu_migrations(counter);
1543 atomic64_set(&counter->hw.prev_count, now);
1547 atomic64_add(delta, &counter->count);
1550 static void cpu_migrations_perf_counter_read(struct perf_counter *counter)
1552 cpu_migrations_perf_counter_update(counter);
1555 static int cpu_migrations_perf_counter_enable(struct perf_counter *counter)
1557 if (counter->prev_state <= PERF_COUNTER_STATE_OFF)
1558 atomic64_set(&counter->hw.prev_count,
1559 get_cpu_migrations(counter));
1563 static void cpu_migrations_perf_counter_disable(struct perf_counter *counter)
1565 cpu_migrations_perf_counter_update(counter);
1568 static const struct hw_perf_counter_ops perf_ops_cpu_migrations = {
1569 .enable = cpu_migrations_perf_counter_enable,
1570 .disable = cpu_migrations_perf_counter_disable,
1571 .read = cpu_migrations_perf_counter_read,
1574 static const struct hw_perf_counter_ops *
1575 sw_perf_counter_init(struct perf_counter *counter)
1577 const struct hw_perf_counter_ops *hw_ops = NULL;
1580 * Software counters (currently) can't in general distinguish
1581 * between user, kernel and hypervisor events.
1582 * However, context switches and cpu migrations are considered
1583 * to be kernel events, and page faults are never hypervisor
1586 switch (counter->hw_event.type) {
1587 case PERF_COUNT_CPU_CLOCK:
1588 if (!(counter->hw_event.exclude_user ||
1589 counter->hw_event.exclude_kernel ||
1590 counter->hw_event.exclude_hv))
1591 hw_ops = &perf_ops_cpu_clock;
1593 case PERF_COUNT_TASK_CLOCK:
1594 if (counter->hw_event.exclude_user ||
1595 counter->hw_event.exclude_kernel ||
1596 counter->hw_event.exclude_hv)
1599 * If the user instantiates this as a per-cpu counter,
1600 * use the cpu_clock counter instead.
1602 if (counter->ctx->task)
1603 hw_ops = &perf_ops_task_clock;
1605 hw_ops = &perf_ops_cpu_clock;
1607 case PERF_COUNT_PAGE_FAULTS:
1608 if (!(counter->hw_event.exclude_user ||
1609 counter->hw_event.exclude_kernel))
1610 hw_ops = &perf_ops_page_faults;
1612 case PERF_COUNT_CONTEXT_SWITCHES:
1613 if (!counter->hw_event.exclude_kernel)
1614 hw_ops = &perf_ops_context_switches;
1616 case PERF_COUNT_CPU_MIGRATIONS:
1617 if (!counter->hw_event.exclude_kernel)
1618 hw_ops = &perf_ops_cpu_migrations;
1627 * Allocate and initialize a counter structure
1629 static struct perf_counter *
1630 perf_counter_alloc(struct perf_counter_hw_event *hw_event,
1632 struct perf_counter_context *ctx,
1633 struct perf_counter *group_leader,
1636 const struct hw_perf_counter_ops *hw_ops;
1637 struct perf_counter *counter;
1639 counter = kzalloc(sizeof(*counter), gfpflags);
1644 * Single counters are their own group leaders, with an
1645 * empty sibling list:
1648 group_leader = counter;
1650 mutex_init(&counter->mutex);
1651 INIT_LIST_HEAD(&counter->list_entry);
1652 INIT_LIST_HEAD(&counter->sibling_list);
1653 init_waitqueue_head(&counter->waitq);
1655 INIT_LIST_HEAD(&counter->child_list);
1657 counter->irqdata = &counter->data[0];
1658 counter->usrdata = &counter->data[1];
1660 counter->hw_event = *hw_event;
1661 counter->wakeup_pending = 0;
1662 counter->group_leader = group_leader;
1663 counter->hw_ops = NULL;
1666 counter->state = PERF_COUNTER_STATE_INACTIVE;
1667 if (hw_event->disabled)
1668 counter->state = PERF_COUNTER_STATE_OFF;
1671 if (!hw_event->raw && hw_event->type < 0)
1672 hw_ops = sw_perf_counter_init(counter);
1674 hw_ops = hw_perf_counter_init(counter);
1680 counter->hw_ops = hw_ops;
1686 * sys_perf_task_open - open a performance counter, associate it to a task/cpu
1688 * @hw_event_uptr: event type attributes for monitoring/sampling
1691 * @group_fd: group leader counter fd
1694 sys_perf_counter_open(struct perf_counter_hw_event *hw_event_uptr __user,
1695 pid_t pid, int cpu, int group_fd)
1697 struct perf_counter *counter, *group_leader;
1698 struct perf_counter_hw_event hw_event;
1699 struct perf_counter_context *ctx;
1700 struct file *counter_file = NULL;
1701 struct file *group_file = NULL;
1702 int fput_needed = 0;
1703 int fput_needed2 = 0;
1706 if (copy_from_user(&hw_event, hw_event_uptr, sizeof(hw_event)) != 0)
1710 * Get the target context (task or percpu):
1712 ctx = find_get_context(pid, cpu);
1714 return PTR_ERR(ctx);
1717 * Look up the group leader (we will attach this counter to it):
1719 group_leader = NULL;
1720 if (group_fd != -1) {
1722 group_file = fget_light(group_fd, &fput_needed);
1724 goto err_put_context;
1725 if (group_file->f_op != &perf_fops)
1726 goto err_put_context;
1728 group_leader = group_file->private_data;
1730 * Do not allow a recursive hierarchy (this new sibling
1731 * becoming part of another group-sibling):
1733 if (group_leader->group_leader != group_leader)
1734 goto err_put_context;
1736 * Do not allow to attach to a group in a different
1737 * task or CPU context:
1739 if (group_leader->ctx != ctx)
1740 goto err_put_context;
1742 * Only a group leader can be exclusive or pinned
1744 if (hw_event.exclusive || hw_event.pinned)
1745 goto err_put_context;
1749 counter = perf_counter_alloc(&hw_event, cpu, ctx, group_leader,
1752 goto err_put_context;
1754 ret = anon_inode_getfd("[perf_counter]", &perf_fops, counter, 0);
1756 goto err_free_put_context;
1758 counter_file = fget_light(ret, &fput_needed2);
1760 goto err_free_put_context;
1762 counter->filp = counter_file;
1763 mutex_lock(&ctx->mutex);
1764 perf_install_in_context(ctx, counter, cpu);
1765 mutex_unlock(&ctx->mutex);
1767 fput_light(counter_file, fput_needed2);
1770 fput_light(group_file, fput_needed);
1774 err_free_put_context:
1784 * Initialize the perf_counter context in a task_struct:
1787 __perf_counter_init_context(struct perf_counter_context *ctx,
1788 struct task_struct *task)
1790 memset(ctx, 0, sizeof(*ctx));
1791 spin_lock_init(&ctx->lock);
1792 mutex_init(&ctx->mutex);
1793 INIT_LIST_HEAD(&ctx->counter_list);
1798 * inherit a counter from parent task to child task:
1800 static struct perf_counter *
1801 inherit_counter(struct perf_counter *parent_counter,
1802 struct task_struct *parent,
1803 struct perf_counter_context *parent_ctx,
1804 struct task_struct *child,
1805 struct perf_counter *group_leader,
1806 struct perf_counter_context *child_ctx)
1808 struct perf_counter *child_counter;
1811 * Instead of creating recursive hierarchies of counters,
1812 * we link inherited counters back to the original parent,
1813 * which has a filp for sure, which we use as the reference
1816 if (parent_counter->parent)
1817 parent_counter = parent_counter->parent;
1819 child_counter = perf_counter_alloc(&parent_counter->hw_event,
1820 parent_counter->cpu, child_ctx,
1821 group_leader, GFP_KERNEL);
1826 * Link it up in the child's context:
1828 child_counter->task = child;
1829 list_add_counter(child_counter, child_ctx);
1830 child_ctx->nr_counters++;
1832 child_counter->parent = parent_counter;
1834 * inherit into child's child as well:
1836 child_counter->hw_event.inherit = 1;
1839 * Get a reference to the parent filp - we will fput it
1840 * when the child counter exits. This is safe to do because
1841 * we are in the parent and we know that the filp still
1842 * exists and has a nonzero count:
1844 atomic_long_inc(&parent_counter->filp->f_count);
1847 * Link this into the parent counter's child list
1849 mutex_lock(&parent_counter->mutex);
1850 list_add_tail(&child_counter->child_list, &parent_counter->child_list);
1853 * Make the child state follow the state of the parent counter,
1854 * not its hw_event.disabled bit. We hold the parent's mutex,
1855 * so we won't race with perf_counter_{en,dis}able_family.
1857 if (parent_counter->state >= PERF_COUNTER_STATE_INACTIVE)
1858 child_counter->state = PERF_COUNTER_STATE_INACTIVE;
1860 child_counter->state = PERF_COUNTER_STATE_OFF;
1862 mutex_unlock(&parent_counter->mutex);
1864 return child_counter;
1867 static int inherit_group(struct perf_counter *parent_counter,
1868 struct task_struct *parent,
1869 struct perf_counter_context *parent_ctx,
1870 struct task_struct *child,
1871 struct perf_counter_context *child_ctx)
1873 struct perf_counter *leader;
1874 struct perf_counter *sub;
1876 leader = inherit_counter(parent_counter, parent, parent_ctx,
1877 child, NULL, child_ctx);
1880 list_for_each_entry(sub, &parent_counter->sibling_list, list_entry) {
1881 if (!inherit_counter(sub, parent, parent_ctx,
1882 child, leader, child_ctx))
1888 static void sync_child_counter(struct perf_counter *child_counter,
1889 struct perf_counter *parent_counter)
1891 u64 parent_val, child_val;
1893 parent_val = atomic64_read(&parent_counter->count);
1894 child_val = atomic64_read(&child_counter->count);
1897 * Add back the child's count to the parent's count:
1899 atomic64_add(child_val, &parent_counter->count);
1902 * Remove this counter from the parent's list
1904 mutex_lock(&parent_counter->mutex);
1905 list_del_init(&child_counter->child_list);
1906 mutex_unlock(&parent_counter->mutex);
1909 * Release the parent counter, if this was the last
1912 fput(parent_counter->filp);
1916 __perf_counter_exit_task(struct task_struct *child,
1917 struct perf_counter *child_counter,
1918 struct perf_counter_context *child_ctx)
1920 struct perf_counter *parent_counter;
1921 struct perf_counter *sub, *tmp;
1924 * If we do not self-reap then we have to wait for the
1925 * child task to unschedule (it will happen for sure),
1926 * so that its counter is at its final count. (This
1927 * condition triggers rarely - child tasks usually get
1928 * off their CPU before the parent has a chance to
1929 * get this far into the reaping action)
1931 if (child != current) {
1932 wait_task_inactive(child, 0);
1933 list_del_init(&child_counter->list_entry);
1935 struct perf_cpu_context *cpuctx;
1936 unsigned long flags;
1940 * Disable and unlink this counter.
1942 * Be careful about zapping the list - IRQ/NMI context
1943 * could still be processing it:
1945 curr_rq_lock_irq_save(&flags);
1946 perf_flags = hw_perf_save_disable();
1948 cpuctx = &__get_cpu_var(perf_cpu_context);
1950 group_sched_out(child_counter, cpuctx, child_ctx);
1952 list_del_init(&child_counter->list_entry);
1954 child_ctx->nr_counters--;
1956 hw_perf_restore(perf_flags);
1957 curr_rq_unlock_irq_restore(&flags);
1960 parent_counter = child_counter->parent;
1962 * It can happen that parent exits first, and has counters
1963 * that are still around due to the child reference. These
1964 * counters need to be zapped - but otherwise linger.
1966 if (parent_counter) {
1967 sync_child_counter(child_counter, parent_counter);
1968 list_for_each_entry_safe(sub, tmp, &child_counter->sibling_list,
1971 sync_child_counter(sub, sub->parent);
1975 kfree(child_counter);
1980 * When a child task exits, feed back counter values to parent counters.
1982 * Note: we may be running in child context, but the PID is not hashed
1983 * anymore so new counters will not be added.
1985 void perf_counter_exit_task(struct task_struct *child)
1987 struct perf_counter *child_counter, *tmp;
1988 struct perf_counter_context *child_ctx;
1990 child_ctx = &child->perf_counter_ctx;
1992 if (likely(!child_ctx->nr_counters))
1995 list_for_each_entry_safe(child_counter, tmp, &child_ctx->counter_list,
1997 __perf_counter_exit_task(child, child_counter, child_ctx);
2001 * Initialize the perf_counter context in task_struct
2003 void perf_counter_init_task(struct task_struct *child)
2005 struct perf_counter_context *child_ctx, *parent_ctx;
2006 struct perf_counter *counter;
2007 struct task_struct *parent = current;
2009 child_ctx = &child->perf_counter_ctx;
2010 parent_ctx = &parent->perf_counter_ctx;
2012 __perf_counter_init_context(child_ctx, child);
2015 * This is executed from the parent task context, so inherit
2016 * counters that have been marked for cloning:
2019 if (likely(!parent_ctx->nr_counters))
2023 * Lock the parent list. No need to lock the child - not PID
2024 * hashed yet and not running, so nobody can access it.
2026 mutex_lock(&parent_ctx->mutex);
2029 * We dont have to disable NMIs - we are only looking at
2030 * the list, not manipulating it:
2032 list_for_each_entry(counter, &parent_ctx->counter_list, list_entry) {
2033 if (!counter->hw_event.inherit)
2036 if (inherit_group(counter, parent,
2037 parent_ctx, child, child_ctx))
2041 mutex_unlock(&parent_ctx->mutex);
2044 static void __cpuinit perf_counter_init_cpu(int cpu)
2046 struct perf_cpu_context *cpuctx;
2048 cpuctx = &per_cpu(perf_cpu_context, cpu);
2049 __perf_counter_init_context(&cpuctx->ctx, NULL);
2051 mutex_lock(&perf_resource_mutex);
2052 cpuctx->max_pertask = perf_max_counters - perf_reserved_percpu;
2053 mutex_unlock(&perf_resource_mutex);
2055 hw_perf_counter_setup(cpu);
2058 #ifdef CONFIG_HOTPLUG_CPU
2059 static void __perf_counter_exit_cpu(void *info)
2061 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
2062 struct perf_counter_context *ctx = &cpuctx->ctx;
2063 struct perf_counter *counter, *tmp;
2065 list_for_each_entry_safe(counter, tmp, &ctx->counter_list, list_entry)
2066 __perf_counter_remove_from_context(counter);
2068 static void perf_counter_exit_cpu(int cpu)
2070 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
2071 struct perf_counter_context *ctx = &cpuctx->ctx;
2073 mutex_lock(&ctx->mutex);
2074 smp_call_function_single(cpu, __perf_counter_exit_cpu, NULL, 1);
2075 mutex_unlock(&ctx->mutex);
2078 static inline void perf_counter_exit_cpu(int cpu) { }
2081 static int __cpuinit
2082 perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
2084 unsigned int cpu = (long)hcpu;
2088 case CPU_UP_PREPARE:
2089 case CPU_UP_PREPARE_FROZEN:
2090 perf_counter_init_cpu(cpu);
2093 case CPU_DOWN_PREPARE:
2094 case CPU_DOWN_PREPARE_FROZEN:
2095 perf_counter_exit_cpu(cpu);
2105 static struct notifier_block __cpuinitdata perf_cpu_nb = {
2106 .notifier_call = perf_cpu_notify,
2109 static int __init perf_counter_init(void)
2111 perf_cpu_notify(&perf_cpu_nb, (unsigned long)CPU_UP_PREPARE,
2112 (void *)(long)smp_processor_id());
2113 register_cpu_notifier(&perf_cpu_nb);
2117 early_initcall(perf_counter_init);
2119 static ssize_t perf_show_reserve_percpu(struct sysdev_class *class, char *buf)
2121 return sprintf(buf, "%d\n", perf_reserved_percpu);
2125 perf_set_reserve_percpu(struct sysdev_class *class,
2129 struct perf_cpu_context *cpuctx;
2133 err = strict_strtoul(buf, 10, &val);
2136 if (val > perf_max_counters)
2139 mutex_lock(&perf_resource_mutex);
2140 perf_reserved_percpu = val;
2141 for_each_online_cpu(cpu) {
2142 cpuctx = &per_cpu(perf_cpu_context, cpu);
2143 spin_lock_irq(&cpuctx->ctx.lock);
2144 mpt = min(perf_max_counters - cpuctx->ctx.nr_counters,
2145 perf_max_counters - perf_reserved_percpu);
2146 cpuctx->max_pertask = mpt;
2147 spin_unlock_irq(&cpuctx->ctx.lock);
2149 mutex_unlock(&perf_resource_mutex);
2154 static ssize_t perf_show_overcommit(struct sysdev_class *class, char *buf)
2156 return sprintf(buf, "%d\n", perf_overcommit);
2160 perf_set_overcommit(struct sysdev_class *class, const char *buf, size_t count)
2165 err = strict_strtoul(buf, 10, &val);
2171 mutex_lock(&perf_resource_mutex);
2172 perf_overcommit = val;
2173 mutex_unlock(&perf_resource_mutex);
2178 static SYSDEV_CLASS_ATTR(
2181 perf_show_reserve_percpu,
2182 perf_set_reserve_percpu
2185 static SYSDEV_CLASS_ATTR(
2188 perf_show_overcommit,
2192 static struct attribute *perfclass_attrs[] = {
2193 &attr_reserve_percpu.attr,
2194 &attr_overcommit.attr,
2198 static struct attribute_group perfclass_attr_group = {
2199 .attrs = perfclass_attrs,
2200 .name = "perf_counters",
2203 static int __init perf_counter_sysfs_init(void)
2205 return sysfs_create_group(&cpu_sysdev_class.kset.kobj,
2206 &perfclass_attr_group);
2208 device_initcall(perf_counter_sysfs_init);