2 * Performance counter core code
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>
9 * For licensing details see kernel-base/COPYING
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>
31 #include <asm/irq_regs.h>
34 * Each CPU has a list of per CPU counters:
36 DEFINE_PER_CPU(struct perf_cpu_context, perf_cpu_context);
38 int perf_max_counters __read_mostly = 1;
39 static int perf_reserved_percpu __read_mostly;
40 static int perf_overcommit __read_mostly = 1;
42 static atomic_t nr_counters __read_mostly;
43 static atomic_t nr_mmap_tracking __read_mostly;
44 static atomic_t nr_munmap_tracking __read_mostly;
45 static atomic_t nr_comm_tracking __read_mostly;
47 int sysctl_perf_counter_priv __read_mostly; /* do we need to be privileged */
48 int sysctl_perf_counter_mlock __read_mostly = 128; /* 'free' kb per counter */
51 * Lock for (sysadmin-configurable) counter reservations:
53 static DEFINE_SPINLOCK(perf_resource_lock);
56 * Architecture provided APIs - weak aliases:
58 extern __weak const struct pmu *hw_perf_counter_init(struct perf_counter *counter)
63 u64 __weak hw_perf_save_disable(void) { return 0; }
64 void __weak hw_perf_restore(u64 ctrl) { barrier(); }
65 void __weak hw_perf_counter_setup(int cpu) { barrier(); }
66 int __weak hw_perf_group_sched_in(struct perf_counter *group_leader,
67 struct perf_cpu_context *cpuctx,
68 struct perf_counter_context *ctx, int cpu)
73 void __weak perf_counter_print_debug(void) { }
76 list_add_counter(struct perf_counter *counter, struct perf_counter_context *ctx)
78 struct perf_counter *group_leader = counter->group_leader;
81 * Depending on whether it is a standalone or sibling counter,
82 * add it straight to the context's counter list, or to the group
83 * leader's sibling list:
85 if (counter->group_leader == counter)
86 list_add_tail(&counter->list_entry, &ctx->counter_list);
88 list_add_tail(&counter->list_entry, &group_leader->sibling_list);
89 group_leader->nr_siblings++;
92 list_add_rcu(&counter->event_entry, &ctx->event_list);
96 list_del_counter(struct perf_counter *counter, struct perf_counter_context *ctx)
98 struct perf_counter *sibling, *tmp;
100 list_del_init(&counter->list_entry);
101 list_del_rcu(&counter->event_entry);
103 if (counter->group_leader != counter)
104 counter->group_leader->nr_siblings--;
107 * If this was a group counter with sibling counters then
108 * upgrade the siblings to singleton counters by adding them
109 * to the context list directly:
111 list_for_each_entry_safe(sibling, tmp,
112 &counter->sibling_list, list_entry) {
114 list_move_tail(&sibling->list_entry, &ctx->counter_list);
115 sibling->group_leader = sibling;
120 counter_sched_out(struct perf_counter *counter,
121 struct perf_cpu_context *cpuctx,
122 struct perf_counter_context *ctx)
124 if (counter->state != PERF_COUNTER_STATE_ACTIVE)
127 counter->state = PERF_COUNTER_STATE_INACTIVE;
128 counter->tstamp_stopped = ctx->time;
129 counter->pmu->disable(counter);
132 if (!is_software_counter(counter))
133 cpuctx->active_oncpu--;
135 if (counter->hw_event.exclusive || !cpuctx->active_oncpu)
136 cpuctx->exclusive = 0;
140 group_sched_out(struct perf_counter *group_counter,
141 struct perf_cpu_context *cpuctx,
142 struct perf_counter_context *ctx)
144 struct perf_counter *counter;
146 if (group_counter->state != PERF_COUNTER_STATE_ACTIVE)
149 counter_sched_out(group_counter, cpuctx, ctx);
152 * Schedule out siblings (if any):
154 list_for_each_entry(counter, &group_counter->sibling_list, list_entry)
155 counter_sched_out(counter, cpuctx, ctx);
157 if (group_counter->hw_event.exclusive)
158 cpuctx->exclusive = 0;
162 * Cross CPU call to remove a performance counter
164 * We disable the counter on the hardware level first. After that we
165 * remove it from the context list.
167 static void __perf_counter_remove_from_context(void *info)
169 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
170 struct perf_counter *counter = info;
171 struct perf_counter_context *ctx = counter->ctx;
176 * If this is a task context, we need to check whether it is
177 * the current task context of this cpu. If not it has been
178 * scheduled out before the smp call arrived.
180 if (ctx->task && cpuctx->task_ctx != ctx)
183 spin_lock_irqsave(&ctx->lock, flags);
185 counter_sched_out(counter, cpuctx, ctx);
187 counter->task = NULL;
191 * Protect the list operation against NMI by disabling the
192 * counters on a global level. NOP for non NMI based counters.
194 perf_flags = hw_perf_save_disable();
195 list_del_counter(counter, ctx);
196 hw_perf_restore(perf_flags);
200 * Allow more per task counters with respect to the
203 cpuctx->max_pertask =
204 min(perf_max_counters - ctx->nr_counters,
205 perf_max_counters - perf_reserved_percpu);
208 spin_unlock_irqrestore(&ctx->lock, flags);
213 * Remove the counter from a task's (or a CPU's) list of counters.
215 * Must be called with counter->mutex and ctx->mutex held.
217 * CPU counters are removed with a smp call. For task counters we only
218 * call when the task is on a CPU.
220 static void perf_counter_remove_from_context(struct perf_counter *counter)
222 struct perf_counter_context *ctx = counter->ctx;
223 struct task_struct *task = ctx->task;
227 * Per cpu counters are removed via an smp call and
228 * the removal is always sucessful.
230 smp_call_function_single(counter->cpu,
231 __perf_counter_remove_from_context,
237 task_oncpu_function_call(task, __perf_counter_remove_from_context,
240 spin_lock_irq(&ctx->lock);
242 * If the context is active we need to retry the smp call.
244 if (ctx->nr_active && !list_empty(&counter->list_entry)) {
245 spin_unlock_irq(&ctx->lock);
250 * The lock prevents that this context is scheduled in so we
251 * can remove the counter safely, if the call above did not
254 if (!list_empty(&counter->list_entry)) {
256 list_del_counter(counter, ctx);
257 counter->task = NULL;
259 spin_unlock_irq(&ctx->lock);
262 static inline u64 perf_clock(void)
264 return cpu_clock(smp_processor_id());
268 * Update the record of the current time in a context.
270 static void update_context_time(struct perf_counter_context *ctx)
272 u64 now = perf_clock();
274 ctx->time += now - ctx->timestamp;
275 ctx->timestamp = now;
279 * Update the total_time_enabled and total_time_running fields for a counter.
281 static void update_counter_times(struct perf_counter *counter)
283 struct perf_counter_context *ctx = counter->ctx;
286 if (counter->state < PERF_COUNTER_STATE_INACTIVE)
289 counter->total_time_enabled = ctx->time - counter->tstamp_enabled;
291 if (counter->state == PERF_COUNTER_STATE_INACTIVE)
292 run_end = counter->tstamp_stopped;
296 counter->total_time_running = run_end - counter->tstamp_running;
300 * Update total_time_enabled and total_time_running for all counters in a group.
302 static void update_group_times(struct perf_counter *leader)
304 struct perf_counter *counter;
306 update_counter_times(leader);
307 list_for_each_entry(counter, &leader->sibling_list, list_entry)
308 update_counter_times(counter);
312 * Cross CPU call to disable a performance counter
314 static void __perf_counter_disable(void *info)
316 struct perf_counter *counter = info;
317 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
318 struct perf_counter_context *ctx = counter->ctx;
322 * If this is a per-task counter, need to check whether this
323 * counter's task is the current task on this cpu.
325 if (ctx->task && cpuctx->task_ctx != ctx)
328 spin_lock_irqsave(&ctx->lock, flags);
331 * If the counter is on, turn it off.
332 * If it is in error state, leave it in error state.
334 if (counter->state >= PERF_COUNTER_STATE_INACTIVE) {
335 update_context_time(ctx);
336 update_counter_times(counter);
337 if (counter == counter->group_leader)
338 group_sched_out(counter, cpuctx, ctx);
340 counter_sched_out(counter, cpuctx, ctx);
341 counter->state = PERF_COUNTER_STATE_OFF;
344 spin_unlock_irqrestore(&ctx->lock, flags);
350 static void perf_counter_disable(struct perf_counter *counter)
352 struct perf_counter_context *ctx = counter->ctx;
353 struct task_struct *task = ctx->task;
357 * Disable the counter on the cpu that it's on
359 smp_call_function_single(counter->cpu, __perf_counter_disable,
365 task_oncpu_function_call(task, __perf_counter_disable, counter);
367 spin_lock_irq(&ctx->lock);
369 * If the counter is still active, we need to retry the cross-call.
371 if (counter->state == PERF_COUNTER_STATE_ACTIVE) {
372 spin_unlock_irq(&ctx->lock);
377 * Since we have the lock this context can't be scheduled
378 * in, so we can change the state safely.
380 if (counter->state == PERF_COUNTER_STATE_INACTIVE) {
381 update_counter_times(counter);
382 counter->state = PERF_COUNTER_STATE_OFF;
385 spin_unlock_irq(&ctx->lock);
389 * Disable a counter and all its children.
391 static void perf_counter_disable_family(struct perf_counter *counter)
393 struct perf_counter *child;
395 perf_counter_disable(counter);
398 * Lock the mutex to protect the list of children
400 mutex_lock(&counter->mutex);
401 list_for_each_entry(child, &counter->child_list, child_list)
402 perf_counter_disable(child);
403 mutex_unlock(&counter->mutex);
407 counter_sched_in(struct perf_counter *counter,
408 struct perf_cpu_context *cpuctx,
409 struct perf_counter_context *ctx,
412 if (counter->state <= PERF_COUNTER_STATE_OFF)
415 counter->state = PERF_COUNTER_STATE_ACTIVE;
416 counter->oncpu = cpu; /* TODO: put 'cpu' into cpuctx->cpu */
418 * The new state must be visible before we turn it on in the hardware:
422 if (counter->pmu->enable(counter)) {
423 counter->state = PERF_COUNTER_STATE_INACTIVE;
428 counter->tstamp_running += ctx->time - counter->tstamp_stopped;
430 if (!is_software_counter(counter))
431 cpuctx->active_oncpu++;
434 if (counter->hw_event.exclusive)
435 cpuctx->exclusive = 1;
441 * Return 1 for a group consisting entirely of software counters,
442 * 0 if the group contains any hardware counters.
444 static int is_software_only_group(struct perf_counter *leader)
446 struct perf_counter *counter;
448 if (!is_software_counter(leader))
451 list_for_each_entry(counter, &leader->sibling_list, list_entry)
452 if (!is_software_counter(counter))
459 * Work out whether we can put this counter group on the CPU now.
461 static int group_can_go_on(struct perf_counter *counter,
462 struct perf_cpu_context *cpuctx,
466 * Groups consisting entirely of software counters can always go on.
468 if (is_software_only_group(counter))
471 * If an exclusive group is already on, no other hardware
472 * counters can go on.
474 if (cpuctx->exclusive)
477 * If this group is exclusive and there are already
478 * counters on the CPU, it can't go on.
480 if (counter->hw_event.exclusive && cpuctx->active_oncpu)
483 * Otherwise, try to add it if all previous groups were able
489 static void add_counter_to_ctx(struct perf_counter *counter,
490 struct perf_counter_context *ctx)
492 list_add_counter(counter, ctx);
494 counter->prev_state = PERF_COUNTER_STATE_OFF;
495 counter->tstamp_enabled = ctx->time;
496 counter->tstamp_running = ctx->time;
497 counter->tstamp_stopped = ctx->time;
501 * Cross CPU call to install and enable a performance counter
503 static void __perf_install_in_context(void *info)
505 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
506 struct perf_counter *counter = info;
507 struct perf_counter_context *ctx = counter->ctx;
508 struct perf_counter *leader = counter->group_leader;
509 int cpu = smp_processor_id();
515 * If this is a task context, we need to check whether it is
516 * the current task context of this cpu. If not it has been
517 * scheduled out before the smp call arrived.
519 if (ctx->task && cpuctx->task_ctx != ctx)
522 spin_lock_irqsave(&ctx->lock, flags);
523 update_context_time(ctx);
526 * Protect the list operation against NMI by disabling the
527 * counters on a global level. NOP for non NMI based counters.
529 perf_flags = hw_perf_save_disable();
531 add_counter_to_ctx(counter, ctx);
534 * Don't put the counter on if it is disabled or if
535 * it is in a group and the group isn't on.
537 if (counter->state != PERF_COUNTER_STATE_INACTIVE ||
538 (leader != counter && leader->state != PERF_COUNTER_STATE_ACTIVE))
542 * An exclusive counter can't go on if there are already active
543 * hardware counters, and no hardware counter can go on if there
544 * is already an exclusive counter on.
546 if (!group_can_go_on(counter, cpuctx, 1))
549 err = counter_sched_in(counter, cpuctx, ctx, cpu);
553 * This counter couldn't go on. If it is in a group
554 * then we have to pull the whole group off.
555 * If the counter group is pinned then put it in error state.
557 if (leader != counter)
558 group_sched_out(leader, cpuctx, ctx);
559 if (leader->hw_event.pinned) {
560 update_group_times(leader);
561 leader->state = PERF_COUNTER_STATE_ERROR;
565 if (!err && !ctx->task && cpuctx->max_pertask)
566 cpuctx->max_pertask--;
569 hw_perf_restore(perf_flags);
571 spin_unlock_irqrestore(&ctx->lock, flags);
575 * Attach a performance counter to a context
577 * First we add the counter to the list with the hardware enable bit
578 * in counter->hw_config cleared.
580 * If the counter is attached to a task which is on a CPU we use a smp
581 * call to enable it in the task context. The task might have been
582 * scheduled away, but we check this in the smp call again.
584 * Must be called with ctx->mutex held.
587 perf_install_in_context(struct perf_counter_context *ctx,
588 struct perf_counter *counter,
591 struct task_struct *task = ctx->task;
595 * Per cpu counters are installed via an smp call and
596 * the install is always sucessful.
598 smp_call_function_single(cpu, __perf_install_in_context,
603 counter->task = task;
605 task_oncpu_function_call(task, __perf_install_in_context,
608 spin_lock_irq(&ctx->lock);
610 * we need to retry the smp call.
612 if (ctx->is_active && list_empty(&counter->list_entry)) {
613 spin_unlock_irq(&ctx->lock);
618 * The lock prevents that this context is scheduled in so we
619 * can add the counter safely, if it the call above did not
622 if (list_empty(&counter->list_entry))
623 add_counter_to_ctx(counter, ctx);
624 spin_unlock_irq(&ctx->lock);
628 * Cross CPU call to enable a performance counter
630 static void __perf_counter_enable(void *info)
632 struct perf_counter *counter = info;
633 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
634 struct perf_counter_context *ctx = counter->ctx;
635 struct perf_counter *leader = counter->group_leader;
640 * If this is a per-task counter, need to check whether this
641 * counter's task is the current task on this cpu.
643 if (ctx->task && cpuctx->task_ctx != ctx)
646 spin_lock_irqsave(&ctx->lock, flags);
647 update_context_time(ctx);
649 counter->prev_state = counter->state;
650 if (counter->state >= PERF_COUNTER_STATE_INACTIVE)
652 counter->state = PERF_COUNTER_STATE_INACTIVE;
653 counter->tstamp_enabled = ctx->time - counter->total_time_enabled;
656 * If the counter is in a group and isn't the group leader,
657 * then don't put it on unless the group is on.
659 if (leader != counter && leader->state != PERF_COUNTER_STATE_ACTIVE)
662 if (!group_can_go_on(counter, cpuctx, 1))
665 err = counter_sched_in(counter, cpuctx, ctx,
670 * If this counter can't go on and it's part of a
671 * group, then the whole group has to come off.
673 if (leader != counter)
674 group_sched_out(leader, cpuctx, ctx);
675 if (leader->hw_event.pinned) {
676 update_group_times(leader);
677 leader->state = PERF_COUNTER_STATE_ERROR;
682 spin_unlock_irqrestore(&ctx->lock, flags);
688 static void perf_counter_enable(struct perf_counter *counter)
690 struct perf_counter_context *ctx = counter->ctx;
691 struct task_struct *task = ctx->task;
695 * Enable the counter on the cpu that it's on
697 smp_call_function_single(counter->cpu, __perf_counter_enable,
702 spin_lock_irq(&ctx->lock);
703 if (counter->state >= PERF_COUNTER_STATE_INACTIVE)
707 * If the counter is in error state, clear that first.
708 * That way, if we see the counter in error state below, we
709 * know that it has gone back into error state, as distinct
710 * from the task having been scheduled away before the
711 * cross-call arrived.
713 if (counter->state == PERF_COUNTER_STATE_ERROR)
714 counter->state = PERF_COUNTER_STATE_OFF;
717 spin_unlock_irq(&ctx->lock);
718 task_oncpu_function_call(task, __perf_counter_enable, counter);
720 spin_lock_irq(&ctx->lock);
723 * If the context is active and the counter is still off,
724 * we need to retry the cross-call.
726 if (ctx->is_active && counter->state == PERF_COUNTER_STATE_OFF)
730 * Since we have the lock this context can't be scheduled
731 * in, so we can change the state safely.
733 if (counter->state == PERF_COUNTER_STATE_OFF) {
734 counter->state = PERF_COUNTER_STATE_INACTIVE;
735 counter->tstamp_enabled =
736 ctx->time - counter->total_time_enabled;
739 spin_unlock_irq(&ctx->lock);
742 static int perf_counter_refresh(struct perf_counter *counter, int refresh)
745 * not supported on inherited counters
747 if (counter->hw_event.inherit)
750 atomic_add(refresh, &counter->event_limit);
751 perf_counter_enable(counter);
757 * Enable a counter and all its children.
759 static void perf_counter_enable_family(struct perf_counter *counter)
761 struct perf_counter *child;
763 perf_counter_enable(counter);
766 * Lock the mutex to protect the list of children
768 mutex_lock(&counter->mutex);
769 list_for_each_entry(child, &counter->child_list, child_list)
770 perf_counter_enable(child);
771 mutex_unlock(&counter->mutex);
774 void __perf_counter_sched_out(struct perf_counter_context *ctx,
775 struct perf_cpu_context *cpuctx)
777 struct perf_counter *counter;
780 spin_lock(&ctx->lock);
782 if (likely(!ctx->nr_counters))
784 update_context_time(ctx);
786 flags = hw_perf_save_disable();
787 if (ctx->nr_active) {
788 list_for_each_entry(counter, &ctx->counter_list, list_entry)
789 group_sched_out(counter, cpuctx, ctx);
791 hw_perf_restore(flags);
793 spin_unlock(&ctx->lock);
797 * Called from scheduler to remove the counters of the current task,
798 * with interrupts disabled.
800 * We stop each counter and update the counter value in counter->count.
802 * This does not protect us against NMI, but disable()
803 * sets the disabled bit in the control field of counter _before_
804 * accessing the counter control register. If a NMI hits, then it will
805 * not restart the counter.
807 void perf_counter_task_sched_out(struct task_struct *task, int cpu)
809 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
810 struct perf_counter_context *ctx = &task->perf_counter_ctx;
811 struct pt_regs *regs;
813 if (likely(!cpuctx->task_ctx))
816 update_context_time(ctx);
818 regs = task_pt_regs(task);
819 perf_swcounter_event(PERF_COUNT_CONTEXT_SWITCHES, 1, 1, regs, 0);
820 __perf_counter_sched_out(ctx, cpuctx);
822 cpuctx->task_ctx = NULL;
825 static void perf_counter_cpu_sched_out(struct perf_cpu_context *cpuctx)
827 __perf_counter_sched_out(&cpuctx->ctx, cpuctx);
831 group_sched_in(struct perf_counter *group_counter,
832 struct perf_cpu_context *cpuctx,
833 struct perf_counter_context *ctx,
836 struct perf_counter *counter, *partial_group;
839 if (group_counter->state == PERF_COUNTER_STATE_OFF)
842 ret = hw_perf_group_sched_in(group_counter, cpuctx, ctx, cpu);
844 return ret < 0 ? ret : 0;
846 group_counter->prev_state = group_counter->state;
847 if (counter_sched_in(group_counter, cpuctx, ctx, cpu))
851 * Schedule in siblings as one group (if any):
853 list_for_each_entry(counter, &group_counter->sibling_list, list_entry) {
854 counter->prev_state = counter->state;
855 if (counter_sched_in(counter, cpuctx, ctx, cpu)) {
856 partial_group = counter;
865 * Groups can be scheduled in as one unit only, so undo any
866 * partial group before returning:
868 list_for_each_entry(counter, &group_counter->sibling_list, list_entry) {
869 if (counter == partial_group)
871 counter_sched_out(counter, cpuctx, ctx);
873 counter_sched_out(group_counter, cpuctx, ctx);
879 __perf_counter_sched_in(struct perf_counter_context *ctx,
880 struct perf_cpu_context *cpuctx, int cpu)
882 struct perf_counter *counter;
886 spin_lock(&ctx->lock);
888 if (likely(!ctx->nr_counters))
891 ctx->timestamp = perf_clock();
893 flags = hw_perf_save_disable();
896 * First go through the list and put on any pinned groups
897 * in order to give them the best chance of going on.
899 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
900 if (counter->state <= PERF_COUNTER_STATE_OFF ||
901 !counter->hw_event.pinned)
903 if (counter->cpu != -1 && counter->cpu != cpu)
906 if (group_can_go_on(counter, cpuctx, 1))
907 group_sched_in(counter, cpuctx, ctx, cpu);
910 * If this pinned group hasn't been scheduled,
911 * put it in error state.
913 if (counter->state == PERF_COUNTER_STATE_INACTIVE) {
914 update_group_times(counter);
915 counter->state = PERF_COUNTER_STATE_ERROR;
919 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
921 * Ignore counters in OFF or ERROR state, and
922 * ignore pinned counters since we did them already.
924 if (counter->state <= PERF_COUNTER_STATE_OFF ||
925 counter->hw_event.pinned)
929 * Listen to the 'cpu' scheduling filter constraint
932 if (counter->cpu != -1 && counter->cpu != cpu)
935 if (group_can_go_on(counter, cpuctx, can_add_hw)) {
936 if (group_sched_in(counter, cpuctx, ctx, cpu))
940 hw_perf_restore(flags);
942 spin_unlock(&ctx->lock);
946 * Called from scheduler to add the counters of the current task
947 * with interrupts disabled.
949 * We restore the counter value and then enable it.
951 * This does not protect us against NMI, but enable()
952 * sets the enabled bit in the control field of counter _before_
953 * accessing the counter control register. If a NMI hits, then it will
954 * keep the counter running.
956 void perf_counter_task_sched_in(struct task_struct *task, int cpu)
958 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
959 struct perf_counter_context *ctx = &task->perf_counter_ctx;
961 __perf_counter_sched_in(ctx, cpuctx, cpu);
962 cpuctx->task_ctx = ctx;
965 static void perf_counter_cpu_sched_in(struct perf_cpu_context *cpuctx, int cpu)
967 struct perf_counter_context *ctx = &cpuctx->ctx;
969 __perf_counter_sched_in(ctx, cpuctx, cpu);
972 int perf_counter_task_disable(void)
974 struct task_struct *curr = current;
975 struct perf_counter_context *ctx = &curr->perf_counter_ctx;
976 struct perf_counter *counter;
981 if (likely(!ctx->nr_counters))
984 local_irq_save(flags);
985 cpu = smp_processor_id();
987 perf_counter_task_sched_out(curr, cpu);
989 spin_lock(&ctx->lock);
992 * Disable all the counters:
994 perf_flags = hw_perf_save_disable();
996 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
997 if (counter->state != PERF_COUNTER_STATE_ERROR) {
998 update_group_times(counter);
999 counter->state = PERF_COUNTER_STATE_OFF;
1003 hw_perf_restore(perf_flags);
1005 spin_unlock_irqrestore(&ctx->lock, flags);
1010 int perf_counter_task_enable(void)
1012 struct task_struct *curr = current;
1013 struct perf_counter_context *ctx = &curr->perf_counter_ctx;
1014 struct perf_counter *counter;
1015 unsigned long flags;
1019 if (likely(!ctx->nr_counters))
1022 local_irq_save(flags);
1023 cpu = smp_processor_id();
1025 perf_counter_task_sched_out(curr, cpu);
1027 spin_lock(&ctx->lock);
1030 * Disable all the counters:
1032 perf_flags = hw_perf_save_disable();
1034 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
1035 if (counter->state > PERF_COUNTER_STATE_OFF)
1037 counter->state = PERF_COUNTER_STATE_INACTIVE;
1038 counter->tstamp_enabled =
1039 ctx->time - counter->total_time_enabled;
1040 counter->hw_event.disabled = 0;
1042 hw_perf_restore(perf_flags);
1044 spin_unlock(&ctx->lock);
1046 perf_counter_task_sched_in(curr, cpu);
1048 local_irq_restore(flags);
1054 * Round-robin a context's counters:
1056 static void rotate_ctx(struct perf_counter_context *ctx)
1058 struct perf_counter *counter;
1061 if (!ctx->nr_counters)
1064 spin_lock(&ctx->lock);
1066 * Rotate the first entry last (works just fine for group counters too):
1068 perf_flags = hw_perf_save_disable();
1069 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
1070 list_move_tail(&counter->list_entry, &ctx->counter_list);
1073 hw_perf_restore(perf_flags);
1075 spin_unlock(&ctx->lock);
1078 void perf_counter_task_tick(struct task_struct *curr, int cpu)
1080 struct perf_cpu_context *cpuctx;
1081 struct perf_counter_context *ctx;
1083 if (!atomic_read(&nr_counters))
1086 cpuctx = &per_cpu(perf_cpu_context, cpu);
1087 ctx = &curr->perf_counter_ctx;
1089 perf_counter_cpu_sched_out(cpuctx);
1090 perf_counter_task_sched_out(curr, cpu);
1092 rotate_ctx(&cpuctx->ctx);
1095 perf_counter_cpu_sched_in(cpuctx, cpu);
1096 perf_counter_task_sched_in(curr, cpu);
1100 * Cross CPU call to read the hardware counter
1102 static void __read(void *info)
1104 struct perf_counter *counter = info;
1105 struct perf_counter_context *ctx = counter->ctx;
1106 unsigned long flags;
1108 local_irq_save(flags);
1110 update_context_time(ctx);
1111 counter->pmu->read(counter);
1112 update_counter_times(counter);
1113 local_irq_restore(flags);
1116 static u64 perf_counter_read(struct perf_counter *counter)
1119 * If counter is enabled and currently active on a CPU, update the
1120 * value in the counter structure:
1122 if (counter->state == PERF_COUNTER_STATE_ACTIVE) {
1123 smp_call_function_single(counter->oncpu,
1124 __read, counter, 1);
1125 } else if (counter->state == PERF_COUNTER_STATE_INACTIVE) {
1126 update_counter_times(counter);
1129 return atomic64_read(&counter->count);
1132 static void put_context(struct perf_counter_context *ctx)
1135 put_task_struct(ctx->task);
1138 static struct perf_counter_context *find_get_context(pid_t pid, int cpu)
1140 struct perf_cpu_context *cpuctx;
1141 struct perf_counter_context *ctx;
1142 struct task_struct *task;
1145 * If cpu is not a wildcard then this is a percpu counter:
1148 /* Must be root to operate on a CPU counter: */
1149 if (sysctl_perf_counter_priv && !capable(CAP_SYS_ADMIN))
1150 return ERR_PTR(-EACCES);
1152 if (cpu < 0 || cpu > num_possible_cpus())
1153 return ERR_PTR(-EINVAL);
1156 * We could be clever and allow to attach a counter to an
1157 * offline CPU and activate it when the CPU comes up, but
1160 if (!cpu_isset(cpu, cpu_online_map))
1161 return ERR_PTR(-ENODEV);
1163 cpuctx = &per_cpu(perf_cpu_context, cpu);
1173 task = find_task_by_vpid(pid);
1175 get_task_struct(task);
1179 return ERR_PTR(-ESRCH);
1181 ctx = &task->perf_counter_ctx;
1184 /* Reuse ptrace permission checks for now. */
1185 if (!ptrace_may_access(task, PTRACE_MODE_READ)) {
1187 return ERR_PTR(-EACCES);
1193 static void free_counter_rcu(struct rcu_head *head)
1195 struct perf_counter *counter;
1197 counter = container_of(head, struct perf_counter, rcu_head);
1201 static void perf_pending_sync(struct perf_counter *counter);
1203 static void free_counter(struct perf_counter *counter)
1205 perf_pending_sync(counter);
1207 atomic_dec(&nr_counters);
1208 if (counter->hw_event.mmap)
1209 atomic_dec(&nr_mmap_tracking);
1210 if (counter->hw_event.munmap)
1211 atomic_dec(&nr_munmap_tracking);
1212 if (counter->hw_event.comm)
1213 atomic_dec(&nr_comm_tracking);
1215 if (counter->destroy)
1216 counter->destroy(counter);
1218 call_rcu(&counter->rcu_head, free_counter_rcu);
1222 * Called when the last reference to the file is gone.
1224 static int perf_release(struct inode *inode, struct file *file)
1226 struct perf_counter *counter = file->private_data;
1227 struct perf_counter_context *ctx = counter->ctx;
1229 file->private_data = NULL;
1231 mutex_lock(&ctx->mutex);
1232 mutex_lock(&counter->mutex);
1234 perf_counter_remove_from_context(counter);
1236 mutex_unlock(&counter->mutex);
1237 mutex_unlock(&ctx->mutex);
1239 free_counter(counter);
1246 * Read the performance counter - simple non blocking version for now
1249 perf_read_hw(struct perf_counter *counter, char __user *buf, size_t count)
1255 * Return end-of-file for a read on a counter that is in
1256 * error state (i.e. because it was pinned but it couldn't be
1257 * scheduled on to the CPU at some point).
1259 if (counter->state == PERF_COUNTER_STATE_ERROR)
1262 mutex_lock(&counter->mutex);
1263 values[0] = perf_counter_read(counter);
1265 if (counter->hw_event.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
1266 values[n++] = counter->total_time_enabled +
1267 atomic64_read(&counter->child_total_time_enabled);
1268 if (counter->hw_event.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
1269 values[n++] = counter->total_time_running +
1270 atomic64_read(&counter->child_total_time_running);
1271 mutex_unlock(&counter->mutex);
1273 if (count < n * sizeof(u64))
1275 count = n * sizeof(u64);
1277 if (copy_to_user(buf, values, count))
1284 perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
1286 struct perf_counter *counter = file->private_data;
1288 return perf_read_hw(counter, buf, count);
1291 static unsigned int perf_poll(struct file *file, poll_table *wait)
1293 struct perf_counter *counter = file->private_data;
1294 struct perf_mmap_data *data;
1295 unsigned int events = POLL_HUP;
1298 data = rcu_dereference(counter->data);
1300 events = atomic_xchg(&data->poll, 0);
1303 poll_wait(file, &counter->waitq, wait);
1308 static void perf_counter_reset(struct perf_counter *counter)
1310 atomic_set(&counter->count, 0);
1313 static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1315 struct perf_counter *counter = file->private_data;
1319 case PERF_COUNTER_IOC_ENABLE:
1320 perf_counter_enable_family(counter);
1322 case PERF_COUNTER_IOC_DISABLE:
1323 perf_counter_disable_family(counter);
1325 case PERF_COUNTER_IOC_REFRESH:
1326 err = perf_counter_refresh(counter, arg);
1328 case PERF_COUNTER_IOC_RESET:
1329 perf_counter_reset(counter);
1338 * Callers need to ensure there can be no nesting of this function, otherwise
1339 * the seqlock logic goes bad. We can not serialize this because the arch
1340 * code calls this from NMI context.
1342 void perf_counter_update_userpage(struct perf_counter *counter)
1344 struct perf_mmap_data *data;
1345 struct perf_counter_mmap_page *userpg;
1348 data = rcu_dereference(counter->data);
1352 userpg = data->user_page;
1355 * Disable preemption so as to not let the corresponding user-space
1356 * spin too long if we get preempted.
1361 userpg->index = counter->hw.idx;
1362 userpg->offset = atomic64_read(&counter->count);
1363 if (counter->state == PERF_COUNTER_STATE_ACTIVE)
1364 userpg->offset -= atomic64_read(&counter->hw.prev_count);
1373 static int perf_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1375 struct perf_counter *counter = vma->vm_file->private_data;
1376 struct perf_mmap_data *data;
1377 int ret = VM_FAULT_SIGBUS;
1380 data = rcu_dereference(counter->data);
1384 if (vmf->pgoff == 0) {
1385 vmf->page = virt_to_page(data->user_page);
1387 int nr = vmf->pgoff - 1;
1389 if ((unsigned)nr > data->nr_pages)
1392 vmf->page = virt_to_page(data->data_pages[nr]);
1394 get_page(vmf->page);
1402 static int perf_mmap_data_alloc(struct perf_counter *counter, int nr_pages)
1404 struct perf_mmap_data *data;
1408 WARN_ON(atomic_read(&counter->mmap_count));
1410 size = sizeof(struct perf_mmap_data);
1411 size += nr_pages * sizeof(void *);
1413 data = kzalloc(size, GFP_KERNEL);
1417 data->user_page = (void *)get_zeroed_page(GFP_KERNEL);
1418 if (!data->user_page)
1419 goto fail_user_page;
1421 for (i = 0; i < nr_pages; i++) {
1422 data->data_pages[i] = (void *)get_zeroed_page(GFP_KERNEL);
1423 if (!data->data_pages[i])
1424 goto fail_data_pages;
1427 data->nr_pages = nr_pages;
1428 atomic_set(&data->lock, -1);
1430 rcu_assign_pointer(counter->data, data);
1435 for (i--; i >= 0; i--)
1436 free_page((unsigned long)data->data_pages[i]);
1438 free_page((unsigned long)data->user_page);
1447 static void __perf_mmap_data_free(struct rcu_head *rcu_head)
1449 struct perf_mmap_data *data = container_of(rcu_head,
1450 struct perf_mmap_data, rcu_head);
1453 free_page((unsigned long)data->user_page);
1454 for (i = 0; i < data->nr_pages; i++)
1455 free_page((unsigned long)data->data_pages[i]);
1459 static void perf_mmap_data_free(struct perf_counter *counter)
1461 struct perf_mmap_data *data = counter->data;
1463 WARN_ON(atomic_read(&counter->mmap_count));
1465 rcu_assign_pointer(counter->data, NULL);
1466 call_rcu(&data->rcu_head, __perf_mmap_data_free);
1469 static void perf_mmap_open(struct vm_area_struct *vma)
1471 struct perf_counter *counter = vma->vm_file->private_data;
1473 atomic_inc(&counter->mmap_count);
1476 static void perf_mmap_close(struct vm_area_struct *vma)
1478 struct perf_counter *counter = vma->vm_file->private_data;
1480 if (atomic_dec_and_mutex_lock(&counter->mmap_count,
1481 &counter->mmap_mutex)) {
1482 vma->vm_mm->locked_vm -= counter->data->nr_locked;
1483 perf_mmap_data_free(counter);
1484 mutex_unlock(&counter->mmap_mutex);
1488 static struct vm_operations_struct perf_mmap_vmops = {
1489 .open = perf_mmap_open,
1490 .close = perf_mmap_close,
1491 .fault = perf_mmap_fault,
1494 static int perf_mmap(struct file *file, struct vm_area_struct *vma)
1496 struct perf_counter *counter = file->private_data;
1497 unsigned long vma_size;
1498 unsigned long nr_pages;
1499 unsigned long locked, lock_limit;
1503 if (!(vma->vm_flags & VM_SHARED) || (vma->vm_flags & VM_WRITE))
1506 vma_size = vma->vm_end - vma->vm_start;
1507 nr_pages = (vma_size / PAGE_SIZE) - 1;
1510 * If we have data pages ensure they're a power-of-two number, so we
1511 * can do bitmasks instead of modulo.
1513 if (nr_pages != 0 && !is_power_of_2(nr_pages))
1516 if (vma_size != PAGE_SIZE * (1 + nr_pages))
1519 if (vma->vm_pgoff != 0)
1522 mutex_lock(&counter->mmap_mutex);
1523 if (atomic_inc_not_zero(&counter->mmap_count)) {
1524 if (nr_pages != counter->data->nr_pages)
1529 extra = nr_pages /* + 1 only account the data pages */;
1530 extra -= sysctl_perf_counter_mlock >> (PAGE_SHIFT - 10);
1534 locked = vma->vm_mm->locked_vm + extra;
1536 lock_limit = current->signal->rlim[RLIMIT_MEMLOCK].rlim_cur;
1537 lock_limit >>= PAGE_SHIFT;
1539 if ((locked > lock_limit) && !capable(CAP_IPC_LOCK)) {
1544 WARN_ON(counter->data);
1545 ret = perf_mmap_data_alloc(counter, nr_pages);
1549 atomic_set(&counter->mmap_count, 1);
1550 vma->vm_mm->locked_vm += extra;
1551 counter->data->nr_locked = extra;
1553 mutex_unlock(&counter->mmap_mutex);
1555 vma->vm_flags &= ~VM_MAYWRITE;
1556 vma->vm_flags |= VM_RESERVED;
1557 vma->vm_ops = &perf_mmap_vmops;
1562 static int perf_fasync(int fd, struct file *filp, int on)
1564 struct perf_counter *counter = filp->private_data;
1565 struct inode *inode = filp->f_path.dentry->d_inode;
1568 mutex_lock(&inode->i_mutex);
1569 retval = fasync_helper(fd, filp, on, &counter->fasync);
1570 mutex_unlock(&inode->i_mutex);
1578 static const struct file_operations perf_fops = {
1579 .release = perf_release,
1582 .unlocked_ioctl = perf_ioctl,
1583 .compat_ioctl = perf_ioctl,
1585 .fasync = perf_fasync,
1589 * Perf counter wakeup
1591 * If there's data, ensure we set the poll() state and publish everything
1592 * to user-space before waking everybody up.
1595 void perf_counter_wakeup(struct perf_counter *counter)
1597 wake_up_all(&counter->waitq);
1599 if (counter->pending_kill) {
1600 kill_fasync(&counter->fasync, SIGIO, counter->pending_kill);
1601 counter->pending_kill = 0;
1608 * Handle the case where we need to wakeup up from NMI (or rq->lock) context.
1610 * The NMI bit means we cannot possibly take locks. Therefore, maintain a
1611 * single linked list and use cmpxchg() to add entries lockless.
1614 static void perf_pending_counter(struct perf_pending_entry *entry)
1616 struct perf_counter *counter = container_of(entry,
1617 struct perf_counter, pending);
1619 if (counter->pending_disable) {
1620 counter->pending_disable = 0;
1621 perf_counter_disable(counter);
1624 if (counter->pending_wakeup) {
1625 counter->pending_wakeup = 0;
1626 perf_counter_wakeup(counter);
1630 #define PENDING_TAIL ((struct perf_pending_entry *)-1UL)
1632 static DEFINE_PER_CPU(struct perf_pending_entry *, perf_pending_head) = {
1636 static void perf_pending_queue(struct perf_pending_entry *entry,
1637 void (*func)(struct perf_pending_entry *))
1639 struct perf_pending_entry **head;
1641 if (cmpxchg(&entry->next, NULL, PENDING_TAIL) != NULL)
1646 head = &get_cpu_var(perf_pending_head);
1649 entry->next = *head;
1650 } while (cmpxchg(head, entry->next, entry) != entry->next);
1652 set_perf_counter_pending();
1654 put_cpu_var(perf_pending_head);
1657 static int __perf_pending_run(void)
1659 struct perf_pending_entry *list;
1662 list = xchg(&__get_cpu_var(perf_pending_head), PENDING_TAIL);
1663 while (list != PENDING_TAIL) {
1664 void (*func)(struct perf_pending_entry *);
1665 struct perf_pending_entry *entry = list;
1672 * Ensure we observe the unqueue before we issue the wakeup,
1673 * so that we won't be waiting forever.
1674 * -- see perf_not_pending().
1685 static inline int perf_not_pending(struct perf_counter *counter)
1688 * If we flush on whatever cpu we run, there is a chance we don't
1692 __perf_pending_run();
1696 * Ensure we see the proper queue state before going to sleep
1697 * so that we do not miss the wakeup. -- see perf_pending_handle()
1700 return counter->pending.next == NULL;
1703 static void perf_pending_sync(struct perf_counter *counter)
1705 wait_event(counter->waitq, perf_not_pending(counter));
1708 void perf_counter_do_pending(void)
1710 __perf_pending_run();
1714 * Callchain support -- arch specific
1717 __weak struct perf_callchain_entry *perf_callchain(struct pt_regs *regs)
1726 struct perf_output_handle {
1727 struct perf_counter *counter;
1728 struct perf_mmap_data *data;
1729 unsigned int offset;
1734 unsigned long flags;
1737 static void perf_output_wakeup(struct perf_output_handle *handle)
1739 atomic_set(&handle->data->poll, POLL_IN);
1742 handle->counter->pending_wakeup = 1;
1743 perf_pending_queue(&handle->counter->pending,
1744 perf_pending_counter);
1746 perf_counter_wakeup(handle->counter);
1750 * Curious locking construct.
1752 * We need to ensure a later event doesn't publish a head when a former
1753 * event isn't done writing. However since we need to deal with NMIs we
1754 * cannot fully serialize things.
1756 * What we do is serialize between CPUs so we only have to deal with NMI
1757 * nesting on a single CPU.
1759 * We only publish the head (and generate a wakeup) when the outer-most
1762 static void perf_output_lock(struct perf_output_handle *handle)
1764 struct perf_mmap_data *data = handle->data;
1769 local_irq_save(handle->flags);
1770 cpu = smp_processor_id();
1772 if (in_nmi() && atomic_read(&data->lock) == cpu)
1775 while (atomic_cmpxchg(&data->lock, -1, cpu) != -1)
1781 static void perf_output_unlock(struct perf_output_handle *handle)
1783 struct perf_mmap_data *data = handle->data;
1786 data->done_head = data->head;
1788 if (!handle->locked)
1793 * The xchg implies a full barrier that ensures all writes are done
1794 * before we publish the new head, matched by a rmb() in userspace when
1795 * reading this position.
1797 while ((head = atomic_xchg(&data->done_head, 0)))
1798 data->user_page->data_head = head;
1801 * NMI can happen here, which means we can miss a done_head update.
1804 cpu = atomic_xchg(&data->lock, -1);
1805 WARN_ON_ONCE(cpu != smp_processor_id());
1808 * Therefore we have to validate we did not indeed do so.
1810 if (unlikely(atomic_read(&data->done_head))) {
1812 * Since we had it locked, we can lock it again.
1814 while (atomic_cmpxchg(&data->lock, -1, cpu) != -1)
1820 if (atomic_xchg(&data->wakeup, 0))
1821 perf_output_wakeup(handle);
1823 local_irq_restore(handle->flags);
1826 static int perf_output_begin(struct perf_output_handle *handle,
1827 struct perf_counter *counter, unsigned int size,
1828 int nmi, int overflow)
1830 struct perf_mmap_data *data;
1831 unsigned int offset, head;
1834 * For inherited counters we send all the output towards the parent.
1836 if (counter->parent)
1837 counter = counter->parent;
1840 data = rcu_dereference(counter->data);
1844 handle->data = data;
1845 handle->counter = counter;
1847 handle->overflow = overflow;
1849 if (!data->nr_pages)
1852 perf_output_lock(handle);
1855 offset = head = atomic_read(&data->head);
1857 } while (atomic_cmpxchg(&data->head, offset, head) != offset);
1859 handle->offset = offset;
1860 handle->head = head;
1862 if ((offset >> PAGE_SHIFT) != (head >> PAGE_SHIFT))
1863 atomic_set(&data->wakeup, 1);
1868 perf_output_wakeup(handle);
1875 static void perf_output_copy(struct perf_output_handle *handle,
1876 void *buf, unsigned int len)
1878 unsigned int pages_mask;
1879 unsigned int offset;
1883 offset = handle->offset;
1884 pages_mask = handle->data->nr_pages - 1;
1885 pages = handle->data->data_pages;
1888 unsigned int page_offset;
1891 nr = (offset >> PAGE_SHIFT) & pages_mask;
1892 page_offset = offset & (PAGE_SIZE - 1);
1893 size = min_t(unsigned int, PAGE_SIZE - page_offset, len);
1895 memcpy(pages[nr] + page_offset, buf, size);
1902 handle->offset = offset;
1904 WARN_ON_ONCE(handle->offset > handle->head);
1907 #define perf_output_put(handle, x) \
1908 perf_output_copy((handle), &(x), sizeof(x))
1910 static void perf_output_end(struct perf_output_handle *handle)
1912 struct perf_counter *counter = handle->counter;
1913 struct perf_mmap_data *data = handle->data;
1915 int wakeup_events = counter->hw_event.wakeup_events;
1917 if (handle->overflow && wakeup_events) {
1918 int events = atomic_inc_return(&data->events);
1919 if (events >= wakeup_events) {
1920 atomic_sub(wakeup_events, &data->events);
1921 atomic_set(&data->wakeup, 1);
1925 perf_output_unlock(handle);
1929 static void perf_counter_output(struct perf_counter *counter,
1930 int nmi, struct pt_regs *regs, u64 addr)
1933 u64 record_type = counter->hw_event.record_type;
1934 struct perf_output_handle handle;
1935 struct perf_event_header header;
1944 struct perf_callchain_entry *callchain = NULL;
1945 int callchain_size = 0;
1949 header.size = sizeof(header);
1951 header.misc = PERF_EVENT_MISC_OVERFLOW;
1952 header.misc |= user_mode(regs) ?
1953 PERF_EVENT_MISC_USER : PERF_EVENT_MISC_KERNEL;
1955 if (record_type & PERF_RECORD_IP) {
1956 ip = instruction_pointer(regs);
1957 header.type |= PERF_RECORD_IP;
1958 header.size += sizeof(ip);
1961 if (record_type & PERF_RECORD_TID) {
1962 /* namespace issues */
1963 tid_entry.pid = current->group_leader->pid;
1964 tid_entry.tid = current->pid;
1966 header.type |= PERF_RECORD_TID;
1967 header.size += sizeof(tid_entry);
1970 if (record_type & PERF_RECORD_TIME) {
1972 * Maybe do better on x86 and provide cpu_clock_nmi()
1974 time = sched_clock();
1976 header.type |= PERF_RECORD_TIME;
1977 header.size += sizeof(u64);
1980 if (record_type & PERF_RECORD_ADDR) {
1981 header.type |= PERF_RECORD_ADDR;
1982 header.size += sizeof(u64);
1985 if (record_type & PERF_RECORD_GROUP) {
1986 header.type |= PERF_RECORD_GROUP;
1987 header.size += sizeof(u64) +
1988 counter->nr_siblings * sizeof(group_entry);
1991 if (record_type & PERF_RECORD_CALLCHAIN) {
1992 callchain = perf_callchain(regs);
1995 callchain_size = (1 + callchain->nr) * sizeof(u64);
1997 header.type |= PERF_RECORD_CALLCHAIN;
1998 header.size += callchain_size;
2002 ret = perf_output_begin(&handle, counter, header.size, nmi, 1);
2006 perf_output_put(&handle, header);
2008 if (record_type & PERF_RECORD_IP)
2009 perf_output_put(&handle, ip);
2011 if (record_type & PERF_RECORD_TID)
2012 perf_output_put(&handle, tid_entry);
2014 if (record_type & PERF_RECORD_TIME)
2015 perf_output_put(&handle, time);
2017 if (record_type & PERF_RECORD_ADDR)
2018 perf_output_put(&handle, addr);
2021 * XXX PERF_RECORD_GROUP vs inherited counters seems difficult.
2023 if (record_type & PERF_RECORD_GROUP) {
2024 struct perf_counter *leader, *sub;
2025 u64 nr = counter->nr_siblings;
2027 perf_output_put(&handle, nr);
2029 leader = counter->group_leader;
2030 list_for_each_entry(sub, &leader->sibling_list, list_entry) {
2032 sub->pmu->read(sub);
2034 group_entry.event = sub->hw_event.config;
2035 group_entry.counter = atomic64_read(&sub->count);
2037 perf_output_put(&handle, group_entry);
2042 perf_output_copy(&handle, callchain, callchain_size);
2044 perf_output_end(&handle);
2051 struct perf_comm_event {
2052 struct task_struct *task;
2057 struct perf_event_header header;
2064 static void perf_counter_comm_output(struct perf_counter *counter,
2065 struct perf_comm_event *comm_event)
2067 struct perf_output_handle handle;
2068 int size = comm_event->event.header.size;
2069 int ret = perf_output_begin(&handle, counter, size, 0, 0);
2074 perf_output_put(&handle, comm_event->event);
2075 perf_output_copy(&handle, comm_event->comm,
2076 comm_event->comm_size);
2077 perf_output_end(&handle);
2080 static int perf_counter_comm_match(struct perf_counter *counter,
2081 struct perf_comm_event *comm_event)
2083 if (counter->hw_event.comm &&
2084 comm_event->event.header.type == PERF_EVENT_COMM)
2090 static void perf_counter_comm_ctx(struct perf_counter_context *ctx,
2091 struct perf_comm_event *comm_event)
2093 struct perf_counter *counter;
2095 if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
2099 list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
2100 if (perf_counter_comm_match(counter, comm_event))
2101 perf_counter_comm_output(counter, comm_event);
2106 static void perf_counter_comm_event(struct perf_comm_event *comm_event)
2108 struct perf_cpu_context *cpuctx;
2110 char *comm = comm_event->task->comm;
2112 size = ALIGN(strlen(comm)+1, sizeof(u64));
2114 comm_event->comm = comm;
2115 comm_event->comm_size = size;
2117 comm_event->event.header.size = sizeof(comm_event->event) + size;
2119 cpuctx = &get_cpu_var(perf_cpu_context);
2120 perf_counter_comm_ctx(&cpuctx->ctx, comm_event);
2121 put_cpu_var(perf_cpu_context);
2123 perf_counter_comm_ctx(¤t->perf_counter_ctx, comm_event);
2126 void perf_counter_comm(struct task_struct *task)
2128 struct perf_comm_event comm_event;
2130 if (!atomic_read(&nr_comm_tracking))
2133 comm_event = (struct perf_comm_event){
2136 .header = { .type = PERF_EVENT_COMM, },
2137 .pid = task->group_leader->pid,
2142 perf_counter_comm_event(&comm_event);
2149 struct perf_mmap_event {
2155 struct perf_event_header header;
2165 static void perf_counter_mmap_output(struct perf_counter *counter,
2166 struct perf_mmap_event *mmap_event)
2168 struct perf_output_handle handle;
2169 int size = mmap_event->event.header.size;
2170 int ret = perf_output_begin(&handle, counter, size, 0, 0);
2175 perf_output_put(&handle, mmap_event->event);
2176 perf_output_copy(&handle, mmap_event->file_name,
2177 mmap_event->file_size);
2178 perf_output_end(&handle);
2181 static int perf_counter_mmap_match(struct perf_counter *counter,
2182 struct perf_mmap_event *mmap_event)
2184 if (counter->hw_event.mmap &&
2185 mmap_event->event.header.type == PERF_EVENT_MMAP)
2188 if (counter->hw_event.munmap &&
2189 mmap_event->event.header.type == PERF_EVENT_MUNMAP)
2195 static void perf_counter_mmap_ctx(struct perf_counter_context *ctx,
2196 struct perf_mmap_event *mmap_event)
2198 struct perf_counter *counter;
2200 if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
2204 list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
2205 if (perf_counter_mmap_match(counter, mmap_event))
2206 perf_counter_mmap_output(counter, mmap_event);
2211 static void perf_counter_mmap_event(struct perf_mmap_event *mmap_event)
2213 struct perf_cpu_context *cpuctx;
2214 struct file *file = mmap_event->file;
2221 buf = kzalloc(PATH_MAX, GFP_KERNEL);
2223 name = strncpy(tmp, "//enomem", sizeof(tmp));
2226 name = d_path(&file->f_path, buf, PATH_MAX);
2228 name = strncpy(tmp, "//toolong", sizeof(tmp));
2232 name = strncpy(tmp, "//anon", sizeof(tmp));
2237 size = ALIGN(strlen(name)+1, sizeof(u64));
2239 mmap_event->file_name = name;
2240 mmap_event->file_size = size;
2242 mmap_event->event.header.size = sizeof(mmap_event->event) + size;
2244 cpuctx = &get_cpu_var(perf_cpu_context);
2245 perf_counter_mmap_ctx(&cpuctx->ctx, mmap_event);
2246 put_cpu_var(perf_cpu_context);
2248 perf_counter_mmap_ctx(¤t->perf_counter_ctx, mmap_event);
2253 void perf_counter_mmap(unsigned long addr, unsigned long len,
2254 unsigned long pgoff, struct file *file)
2256 struct perf_mmap_event mmap_event;
2258 if (!atomic_read(&nr_mmap_tracking))
2261 mmap_event = (struct perf_mmap_event){
2264 .header = { .type = PERF_EVENT_MMAP, },
2265 .pid = current->group_leader->pid,
2266 .tid = current->pid,
2273 perf_counter_mmap_event(&mmap_event);
2276 void perf_counter_munmap(unsigned long addr, unsigned long len,
2277 unsigned long pgoff, struct file *file)
2279 struct perf_mmap_event mmap_event;
2281 if (!atomic_read(&nr_munmap_tracking))
2284 mmap_event = (struct perf_mmap_event){
2287 .header = { .type = PERF_EVENT_MUNMAP, },
2288 .pid = current->group_leader->pid,
2289 .tid = current->pid,
2296 perf_counter_mmap_event(&mmap_event);
2300 * Generic counter overflow handling.
2303 int perf_counter_overflow(struct perf_counter *counter,
2304 int nmi, struct pt_regs *regs, u64 addr)
2306 int events = atomic_read(&counter->event_limit);
2310 * XXX event_limit might not quite work as expected on inherited
2314 counter->pending_kill = POLL_IN;
2315 if (events && atomic_dec_and_test(&counter->event_limit)) {
2317 counter->pending_kill = POLL_HUP;
2319 counter->pending_disable = 1;
2320 perf_pending_queue(&counter->pending,
2321 perf_pending_counter);
2323 perf_counter_disable(counter);
2326 perf_counter_output(counter, nmi, regs, addr);
2331 * Generic software counter infrastructure
2334 static void perf_swcounter_update(struct perf_counter *counter)
2336 struct hw_perf_counter *hwc = &counter->hw;
2341 prev = atomic64_read(&hwc->prev_count);
2342 now = atomic64_read(&hwc->count);
2343 if (atomic64_cmpxchg(&hwc->prev_count, prev, now) != prev)
2348 atomic64_add(delta, &counter->count);
2349 atomic64_sub(delta, &hwc->period_left);
2352 static void perf_swcounter_set_period(struct perf_counter *counter)
2354 struct hw_perf_counter *hwc = &counter->hw;
2355 s64 left = atomic64_read(&hwc->period_left);
2356 s64 period = hwc->irq_period;
2358 if (unlikely(left <= -period)) {
2360 atomic64_set(&hwc->period_left, left);
2363 if (unlikely(left <= 0)) {
2365 atomic64_add(period, &hwc->period_left);
2368 atomic64_set(&hwc->prev_count, -left);
2369 atomic64_set(&hwc->count, -left);
2372 static enum hrtimer_restart perf_swcounter_hrtimer(struct hrtimer *hrtimer)
2374 enum hrtimer_restart ret = HRTIMER_RESTART;
2375 struct perf_counter *counter;
2376 struct pt_regs *regs;
2378 counter = container_of(hrtimer, struct perf_counter, hw.hrtimer);
2379 counter->pmu->read(counter);
2381 regs = get_irq_regs();
2383 * In case we exclude kernel IPs or are somehow not in interrupt
2384 * context, provide the next best thing, the user IP.
2386 if ((counter->hw_event.exclude_kernel || !regs) &&
2387 !counter->hw_event.exclude_user)
2388 regs = task_pt_regs(current);
2391 if (perf_counter_overflow(counter, 0, regs, 0))
2392 ret = HRTIMER_NORESTART;
2395 hrtimer_forward_now(hrtimer, ns_to_ktime(counter->hw.irq_period));
2400 static void perf_swcounter_overflow(struct perf_counter *counter,
2401 int nmi, struct pt_regs *regs, u64 addr)
2403 perf_swcounter_update(counter);
2404 perf_swcounter_set_period(counter);
2405 if (perf_counter_overflow(counter, nmi, regs, addr))
2406 /* soft-disable the counter */
2411 static int perf_swcounter_match(struct perf_counter *counter,
2412 enum perf_event_types type,
2413 u32 event, struct pt_regs *regs)
2415 if (counter->state != PERF_COUNTER_STATE_ACTIVE)
2418 if (perf_event_raw(&counter->hw_event))
2421 if (perf_event_type(&counter->hw_event) != type)
2424 if (perf_event_id(&counter->hw_event) != event)
2427 if (counter->hw_event.exclude_user && user_mode(regs))
2430 if (counter->hw_event.exclude_kernel && !user_mode(regs))
2436 static void perf_swcounter_add(struct perf_counter *counter, u64 nr,
2437 int nmi, struct pt_regs *regs, u64 addr)
2439 int neg = atomic64_add_negative(nr, &counter->hw.count);
2440 if (counter->hw.irq_period && !neg)
2441 perf_swcounter_overflow(counter, nmi, regs, addr);
2444 static void perf_swcounter_ctx_event(struct perf_counter_context *ctx,
2445 enum perf_event_types type, u32 event,
2446 u64 nr, int nmi, struct pt_regs *regs,
2449 struct perf_counter *counter;
2451 if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
2455 list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
2456 if (perf_swcounter_match(counter, type, event, regs))
2457 perf_swcounter_add(counter, nr, nmi, regs, addr);
2462 static int *perf_swcounter_recursion_context(struct perf_cpu_context *cpuctx)
2465 return &cpuctx->recursion[3];
2468 return &cpuctx->recursion[2];
2471 return &cpuctx->recursion[1];
2473 return &cpuctx->recursion[0];
2476 static void __perf_swcounter_event(enum perf_event_types type, u32 event,
2477 u64 nr, int nmi, struct pt_regs *regs,
2480 struct perf_cpu_context *cpuctx = &get_cpu_var(perf_cpu_context);
2481 int *recursion = perf_swcounter_recursion_context(cpuctx);
2489 perf_swcounter_ctx_event(&cpuctx->ctx, type, event,
2490 nr, nmi, regs, addr);
2491 if (cpuctx->task_ctx) {
2492 perf_swcounter_ctx_event(cpuctx->task_ctx, type, event,
2493 nr, nmi, regs, addr);
2500 put_cpu_var(perf_cpu_context);
2504 perf_swcounter_event(u32 event, u64 nr, int nmi, struct pt_regs *regs, u64 addr)
2506 __perf_swcounter_event(PERF_TYPE_SOFTWARE, event, nr, nmi, regs, addr);
2509 static void perf_swcounter_read(struct perf_counter *counter)
2511 perf_swcounter_update(counter);
2514 static int perf_swcounter_enable(struct perf_counter *counter)
2516 perf_swcounter_set_period(counter);
2520 static void perf_swcounter_disable(struct perf_counter *counter)
2522 perf_swcounter_update(counter);
2525 static const struct pmu perf_ops_generic = {
2526 .enable = perf_swcounter_enable,
2527 .disable = perf_swcounter_disable,
2528 .read = perf_swcounter_read,
2532 * Software counter: cpu wall time clock
2535 static void cpu_clock_perf_counter_update(struct perf_counter *counter)
2537 int cpu = raw_smp_processor_id();
2541 now = cpu_clock(cpu);
2542 prev = atomic64_read(&counter->hw.prev_count);
2543 atomic64_set(&counter->hw.prev_count, now);
2544 atomic64_add(now - prev, &counter->count);
2547 static int cpu_clock_perf_counter_enable(struct perf_counter *counter)
2549 struct hw_perf_counter *hwc = &counter->hw;
2550 int cpu = raw_smp_processor_id();
2552 atomic64_set(&hwc->prev_count, cpu_clock(cpu));
2553 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
2554 hwc->hrtimer.function = perf_swcounter_hrtimer;
2555 if (hwc->irq_period) {
2556 __hrtimer_start_range_ns(&hwc->hrtimer,
2557 ns_to_ktime(hwc->irq_period), 0,
2558 HRTIMER_MODE_REL, 0);
2564 static void cpu_clock_perf_counter_disable(struct perf_counter *counter)
2566 hrtimer_cancel(&counter->hw.hrtimer);
2567 cpu_clock_perf_counter_update(counter);
2570 static void cpu_clock_perf_counter_read(struct perf_counter *counter)
2572 cpu_clock_perf_counter_update(counter);
2575 static const struct pmu perf_ops_cpu_clock = {
2576 .enable = cpu_clock_perf_counter_enable,
2577 .disable = cpu_clock_perf_counter_disable,
2578 .read = cpu_clock_perf_counter_read,
2582 * Software counter: task time clock
2585 static void task_clock_perf_counter_update(struct perf_counter *counter, u64 now)
2590 prev = atomic64_xchg(&counter->hw.prev_count, now);
2592 atomic64_add(delta, &counter->count);
2595 static int task_clock_perf_counter_enable(struct perf_counter *counter)
2597 struct hw_perf_counter *hwc = &counter->hw;
2600 now = counter->ctx->time;
2602 atomic64_set(&hwc->prev_count, now);
2603 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
2604 hwc->hrtimer.function = perf_swcounter_hrtimer;
2605 if (hwc->irq_period) {
2606 __hrtimer_start_range_ns(&hwc->hrtimer,
2607 ns_to_ktime(hwc->irq_period), 0,
2608 HRTIMER_MODE_REL, 0);
2614 static void task_clock_perf_counter_disable(struct perf_counter *counter)
2616 hrtimer_cancel(&counter->hw.hrtimer);
2617 task_clock_perf_counter_update(counter, counter->ctx->time);
2621 static void task_clock_perf_counter_read(struct perf_counter *counter)
2626 update_context_time(counter->ctx);
2627 time = counter->ctx->time;
2629 u64 now = perf_clock();
2630 u64 delta = now - counter->ctx->timestamp;
2631 time = counter->ctx->time + delta;
2634 task_clock_perf_counter_update(counter, time);
2637 static const struct pmu perf_ops_task_clock = {
2638 .enable = task_clock_perf_counter_enable,
2639 .disable = task_clock_perf_counter_disable,
2640 .read = task_clock_perf_counter_read,
2644 * Software counter: cpu migrations
2647 static inline u64 get_cpu_migrations(struct perf_counter *counter)
2649 struct task_struct *curr = counter->ctx->task;
2652 return curr->se.nr_migrations;
2653 return cpu_nr_migrations(smp_processor_id());
2656 static void cpu_migrations_perf_counter_update(struct perf_counter *counter)
2661 prev = atomic64_read(&counter->hw.prev_count);
2662 now = get_cpu_migrations(counter);
2664 atomic64_set(&counter->hw.prev_count, now);
2668 atomic64_add(delta, &counter->count);
2671 static void cpu_migrations_perf_counter_read(struct perf_counter *counter)
2673 cpu_migrations_perf_counter_update(counter);
2676 static int cpu_migrations_perf_counter_enable(struct perf_counter *counter)
2678 if (counter->prev_state <= PERF_COUNTER_STATE_OFF)
2679 atomic64_set(&counter->hw.prev_count,
2680 get_cpu_migrations(counter));
2684 static void cpu_migrations_perf_counter_disable(struct perf_counter *counter)
2686 cpu_migrations_perf_counter_update(counter);
2689 static const struct pmu perf_ops_cpu_migrations = {
2690 .enable = cpu_migrations_perf_counter_enable,
2691 .disable = cpu_migrations_perf_counter_disable,
2692 .read = cpu_migrations_perf_counter_read,
2695 #ifdef CONFIG_EVENT_PROFILE
2696 void perf_tpcounter_event(int event_id)
2698 struct pt_regs *regs = get_irq_regs();
2701 regs = task_pt_regs(current);
2703 __perf_swcounter_event(PERF_TYPE_TRACEPOINT, event_id, 1, 1, regs, 0);
2705 EXPORT_SYMBOL_GPL(perf_tpcounter_event);
2707 extern int ftrace_profile_enable(int);
2708 extern void ftrace_profile_disable(int);
2710 static void tp_perf_counter_destroy(struct perf_counter *counter)
2712 ftrace_profile_disable(perf_event_id(&counter->hw_event));
2715 static const struct pmu *tp_perf_counter_init(struct perf_counter *counter)
2717 int event_id = perf_event_id(&counter->hw_event);
2720 ret = ftrace_profile_enable(event_id);
2724 counter->destroy = tp_perf_counter_destroy;
2725 counter->hw.irq_period = counter->hw_event.irq_period;
2727 return &perf_ops_generic;
2730 static const struct pmu *tp_perf_counter_init(struct perf_counter *counter)
2736 static const struct pmu *sw_perf_counter_init(struct perf_counter *counter)
2738 struct perf_counter_hw_event *hw_event = &counter->hw_event;
2739 const struct pmu *pmu = NULL;
2740 struct hw_perf_counter *hwc = &counter->hw;
2743 * Software counters (currently) can't in general distinguish
2744 * between user, kernel and hypervisor events.
2745 * However, context switches and cpu migrations are considered
2746 * to be kernel events, and page faults are never hypervisor
2749 switch (perf_event_id(&counter->hw_event)) {
2750 case PERF_COUNT_CPU_CLOCK:
2751 pmu = &perf_ops_cpu_clock;
2753 if (hw_event->irq_period && hw_event->irq_period < 10000)
2754 hw_event->irq_period = 10000;
2756 case PERF_COUNT_TASK_CLOCK:
2758 * If the user instantiates this as a per-cpu counter,
2759 * use the cpu_clock counter instead.
2761 if (counter->ctx->task)
2762 pmu = &perf_ops_task_clock;
2764 pmu = &perf_ops_cpu_clock;
2766 if (hw_event->irq_period && hw_event->irq_period < 10000)
2767 hw_event->irq_period = 10000;
2769 case PERF_COUNT_PAGE_FAULTS:
2770 case PERF_COUNT_PAGE_FAULTS_MIN:
2771 case PERF_COUNT_PAGE_FAULTS_MAJ:
2772 case PERF_COUNT_CONTEXT_SWITCHES:
2773 pmu = &perf_ops_generic;
2775 case PERF_COUNT_CPU_MIGRATIONS:
2776 if (!counter->hw_event.exclude_kernel)
2777 pmu = &perf_ops_cpu_migrations;
2782 hwc->irq_period = hw_event->irq_period;
2788 * Allocate and initialize a counter structure
2790 static struct perf_counter *
2791 perf_counter_alloc(struct perf_counter_hw_event *hw_event,
2793 struct perf_counter_context *ctx,
2794 struct perf_counter *group_leader,
2797 const struct pmu *pmu;
2798 struct perf_counter *counter;
2801 counter = kzalloc(sizeof(*counter), gfpflags);
2803 return ERR_PTR(-ENOMEM);
2806 * Single counters are their own group leaders, with an
2807 * empty sibling list:
2810 group_leader = counter;
2812 mutex_init(&counter->mutex);
2813 INIT_LIST_HEAD(&counter->list_entry);
2814 INIT_LIST_HEAD(&counter->event_entry);
2815 INIT_LIST_HEAD(&counter->sibling_list);
2816 init_waitqueue_head(&counter->waitq);
2818 mutex_init(&counter->mmap_mutex);
2820 INIT_LIST_HEAD(&counter->child_list);
2823 counter->hw_event = *hw_event;
2824 counter->group_leader = group_leader;
2825 counter->pmu = NULL;
2828 counter->state = PERF_COUNTER_STATE_INACTIVE;
2829 if (hw_event->disabled)
2830 counter->state = PERF_COUNTER_STATE_OFF;
2835 * we currently do not support PERF_RECORD_GROUP on inherited counters
2837 if (hw_event->inherit && (hw_event->record_type & PERF_RECORD_GROUP))
2840 if (perf_event_raw(hw_event)) {
2841 pmu = hw_perf_counter_init(counter);
2845 switch (perf_event_type(hw_event)) {
2846 case PERF_TYPE_HARDWARE:
2847 pmu = hw_perf_counter_init(counter);
2850 case PERF_TYPE_SOFTWARE:
2851 pmu = sw_perf_counter_init(counter);
2854 case PERF_TYPE_TRACEPOINT:
2855 pmu = tp_perf_counter_init(counter);
2862 else if (IS_ERR(pmu))
2867 return ERR_PTR(err);
2872 atomic_inc(&nr_counters);
2873 if (counter->hw_event.mmap)
2874 atomic_inc(&nr_mmap_tracking);
2875 if (counter->hw_event.munmap)
2876 atomic_inc(&nr_munmap_tracking);
2877 if (counter->hw_event.comm)
2878 atomic_inc(&nr_comm_tracking);
2884 * sys_perf_counter_open - open a performance counter, associate it to a task/cpu
2886 * @hw_event_uptr: event type attributes for monitoring/sampling
2889 * @group_fd: group leader counter fd
2891 SYSCALL_DEFINE5(perf_counter_open,
2892 const struct perf_counter_hw_event __user *, hw_event_uptr,
2893 pid_t, pid, int, cpu, int, group_fd, unsigned long, flags)
2895 struct perf_counter *counter, *group_leader;
2896 struct perf_counter_hw_event hw_event;
2897 struct perf_counter_context *ctx;
2898 struct file *counter_file = NULL;
2899 struct file *group_file = NULL;
2900 int fput_needed = 0;
2901 int fput_needed2 = 0;
2904 /* for future expandability... */
2908 if (copy_from_user(&hw_event, hw_event_uptr, sizeof(hw_event)) != 0)
2912 * Get the target context (task or percpu):
2914 ctx = find_get_context(pid, cpu);
2916 return PTR_ERR(ctx);
2919 * Look up the group leader (we will attach this counter to it):
2921 group_leader = NULL;
2922 if (group_fd != -1) {
2924 group_file = fget_light(group_fd, &fput_needed);
2926 goto err_put_context;
2927 if (group_file->f_op != &perf_fops)
2928 goto err_put_context;
2930 group_leader = group_file->private_data;
2932 * Do not allow a recursive hierarchy (this new sibling
2933 * becoming part of another group-sibling):
2935 if (group_leader->group_leader != group_leader)
2936 goto err_put_context;
2938 * Do not allow to attach to a group in a different
2939 * task or CPU context:
2941 if (group_leader->ctx != ctx)
2942 goto err_put_context;
2944 * Only a group leader can be exclusive or pinned
2946 if (hw_event.exclusive || hw_event.pinned)
2947 goto err_put_context;
2950 counter = perf_counter_alloc(&hw_event, cpu, ctx, group_leader,
2952 ret = PTR_ERR(counter);
2953 if (IS_ERR(counter))
2954 goto err_put_context;
2956 ret = anon_inode_getfd("[perf_counter]", &perf_fops, counter, 0);
2958 goto err_free_put_context;
2960 counter_file = fget_light(ret, &fput_needed2);
2962 goto err_free_put_context;
2964 counter->filp = counter_file;
2965 mutex_lock(&ctx->mutex);
2966 perf_install_in_context(ctx, counter, cpu);
2967 mutex_unlock(&ctx->mutex);
2969 fput_light(counter_file, fput_needed2);
2972 fput_light(group_file, fput_needed);
2976 err_free_put_context:
2986 * Initialize the perf_counter context in a task_struct:
2989 __perf_counter_init_context(struct perf_counter_context *ctx,
2990 struct task_struct *task)
2992 memset(ctx, 0, sizeof(*ctx));
2993 spin_lock_init(&ctx->lock);
2994 mutex_init(&ctx->mutex);
2995 INIT_LIST_HEAD(&ctx->counter_list);
2996 INIT_LIST_HEAD(&ctx->event_list);
3001 * inherit a counter from parent task to child task:
3003 static struct perf_counter *
3004 inherit_counter(struct perf_counter *parent_counter,
3005 struct task_struct *parent,
3006 struct perf_counter_context *parent_ctx,
3007 struct task_struct *child,
3008 struct perf_counter *group_leader,
3009 struct perf_counter_context *child_ctx)
3011 struct perf_counter *child_counter;
3014 * Instead of creating recursive hierarchies of counters,
3015 * we link inherited counters back to the original parent,
3016 * which has a filp for sure, which we use as the reference
3019 if (parent_counter->parent)
3020 parent_counter = parent_counter->parent;
3022 child_counter = perf_counter_alloc(&parent_counter->hw_event,
3023 parent_counter->cpu, child_ctx,
3024 group_leader, GFP_KERNEL);
3025 if (IS_ERR(child_counter))
3026 return child_counter;
3029 * Link it up in the child's context:
3031 child_counter->task = child;
3032 add_counter_to_ctx(child_counter, child_ctx);
3034 child_counter->parent = parent_counter;
3036 * inherit into child's child as well:
3038 child_counter->hw_event.inherit = 1;
3041 * Get a reference to the parent filp - we will fput it
3042 * when the child counter exits. This is safe to do because
3043 * we are in the parent and we know that the filp still
3044 * exists and has a nonzero count:
3046 atomic_long_inc(&parent_counter->filp->f_count);
3049 * Link this into the parent counter's child list
3051 mutex_lock(&parent_counter->mutex);
3052 list_add_tail(&child_counter->child_list, &parent_counter->child_list);
3055 * Make the child state follow the state of the parent counter,
3056 * not its hw_event.disabled bit. We hold the parent's mutex,
3057 * so we won't race with perf_counter_{en,dis}able_family.
3059 if (parent_counter->state >= PERF_COUNTER_STATE_INACTIVE)
3060 child_counter->state = PERF_COUNTER_STATE_INACTIVE;
3062 child_counter->state = PERF_COUNTER_STATE_OFF;
3064 mutex_unlock(&parent_counter->mutex);
3066 return child_counter;
3069 static int inherit_group(struct perf_counter *parent_counter,
3070 struct task_struct *parent,
3071 struct perf_counter_context *parent_ctx,
3072 struct task_struct *child,
3073 struct perf_counter_context *child_ctx)
3075 struct perf_counter *leader;
3076 struct perf_counter *sub;
3077 struct perf_counter *child_ctr;
3079 leader = inherit_counter(parent_counter, parent, parent_ctx,
3080 child, NULL, child_ctx);
3082 return PTR_ERR(leader);
3083 list_for_each_entry(sub, &parent_counter->sibling_list, list_entry) {
3084 child_ctr = inherit_counter(sub, parent, parent_ctx,
3085 child, leader, child_ctx);
3086 if (IS_ERR(child_ctr))
3087 return PTR_ERR(child_ctr);
3092 static void sync_child_counter(struct perf_counter *child_counter,
3093 struct perf_counter *parent_counter)
3095 u64 parent_val, child_val;
3097 parent_val = atomic64_read(&parent_counter->count);
3098 child_val = atomic64_read(&child_counter->count);
3101 * Add back the child's count to the parent's count:
3103 atomic64_add(child_val, &parent_counter->count);
3104 atomic64_add(child_counter->total_time_enabled,
3105 &parent_counter->child_total_time_enabled);
3106 atomic64_add(child_counter->total_time_running,
3107 &parent_counter->child_total_time_running);
3110 * Remove this counter from the parent's list
3112 mutex_lock(&parent_counter->mutex);
3113 list_del_init(&child_counter->child_list);
3114 mutex_unlock(&parent_counter->mutex);
3117 * Release the parent counter, if this was the last
3120 fput(parent_counter->filp);
3124 __perf_counter_exit_task(struct task_struct *child,
3125 struct perf_counter *child_counter,
3126 struct perf_counter_context *child_ctx)
3128 struct perf_counter *parent_counter;
3129 struct perf_counter *sub, *tmp;
3132 * If we do not self-reap then we have to wait for the
3133 * child task to unschedule (it will happen for sure),
3134 * so that its counter is at its final count. (This
3135 * condition triggers rarely - child tasks usually get
3136 * off their CPU before the parent has a chance to
3137 * get this far into the reaping action)
3139 if (child != current) {
3140 wait_task_inactive(child, 0);
3141 list_del_init(&child_counter->list_entry);
3142 update_counter_times(child_counter);
3144 struct perf_cpu_context *cpuctx;
3145 unsigned long flags;
3149 * Disable and unlink this counter.
3151 * Be careful about zapping the list - IRQ/NMI context
3152 * could still be processing it:
3154 local_irq_save(flags);
3155 perf_flags = hw_perf_save_disable();
3157 cpuctx = &__get_cpu_var(perf_cpu_context);
3159 group_sched_out(child_counter, cpuctx, child_ctx);
3160 update_counter_times(child_counter);
3162 list_del_init(&child_counter->list_entry);
3164 child_ctx->nr_counters--;
3166 hw_perf_restore(perf_flags);
3167 local_irq_restore(flags);
3170 parent_counter = child_counter->parent;
3172 * It can happen that parent exits first, and has counters
3173 * that are still around due to the child reference. These
3174 * counters need to be zapped - but otherwise linger.
3176 if (parent_counter) {
3177 sync_child_counter(child_counter, parent_counter);
3178 list_for_each_entry_safe(sub, tmp, &child_counter->sibling_list,
3181 sync_child_counter(sub, sub->parent);
3185 free_counter(child_counter);
3190 * When a child task exits, feed back counter values to parent counters.
3192 * Note: we may be running in child context, but the PID is not hashed
3193 * anymore so new counters will not be added.
3195 void perf_counter_exit_task(struct task_struct *child)
3197 struct perf_counter *child_counter, *tmp;
3198 struct perf_counter_context *child_ctx;
3200 child_ctx = &child->perf_counter_ctx;
3202 if (likely(!child_ctx->nr_counters))
3205 list_for_each_entry_safe(child_counter, tmp, &child_ctx->counter_list,
3207 __perf_counter_exit_task(child, child_counter, child_ctx);
3211 * Initialize the perf_counter context in task_struct
3213 void perf_counter_init_task(struct task_struct *child)
3215 struct perf_counter_context *child_ctx, *parent_ctx;
3216 struct perf_counter *counter;
3217 struct task_struct *parent = current;
3219 child_ctx = &child->perf_counter_ctx;
3220 parent_ctx = &parent->perf_counter_ctx;
3222 __perf_counter_init_context(child_ctx, child);
3225 * This is executed from the parent task context, so inherit
3226 * counters that have been marked for cloning:
3229 if (likely(!parent_ctx->nr_counters))
3233 * Lock the parent list. No need to lock the child - not PID
3234 * hashed yet and not running, so nobody can access it.
3236 mutex_lock(&parent_ctx->mutex);
3239 * We dont have to disable NMIs - we are only looking at
3240 * the list, not manipulating it:
3242 list_for_each_entry(counter, &parent_ctx->counter_list, list_entry) {
3243 if (!counter->hw_event.inherit)
3246 if (inherit_group(counter, parent,
3247 parent_ctx, child, child_ctx))
3251 mutex_unlock(&parent_ctx->mutex);
3254 static void __cpuinit perf_counter_init_cpu(int cpu)
3256 struct perf_cpu_context *cpuctx;
3258 cpuctx = &per_cpu(perf_cpu_context, cpu);
3259 __perf_counter_init_context(&cpuctx->ctx, NULL);
3261 spin_lock(&perf_resource_lock);
3262 cpuctx->max_pertask = perf_max_counters - perf_reserved_percpu;
3263 spin_unlock(&perf_resource_lock);
3265 hw_perf_counter_setup(cpu);
3268 #ifdef CONFIG_HOTPLUG_CPU
3269 static void __perf_counter_exit_cpu(void *info)
3271 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
3272 struct perf_counter_context *ctx = &cpuctx->ctx;
3273 struct perf_counter *counter, *tmp;
3275 list_for_each_entry_safe(counter, tmp, &ctx->counter_list, list_entry)
3276 __perf_counter_remove_from_context(counter);
3278 static void perf_counter_exit_cpu(int cpu)
3280 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
3281 struct perf_counter_context *ctx = &cpuctx->ctx;
3283 mutex_lock(&ctx->mutex);
3284 smp_call_function_single(cpu, __perf_counter_exit_cpu, NULL, 1);
3285 mutex_unlock(&ctx->mutex);
3288 static inline void perf_counter_exit_cpu(int cpu) { }
3291 static int __cpuinit
3292 perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
3294 unsigned int cpu = (long)hcpu;
3298 case CPU_UP_PREPARE:
3299 case CPU_UP_PREPARE_FROZEN:
3300 perf_counter_init_cpu(cpu);
3303 case CPU_DOWN_PREPARE:
3304 case CPU_DOWN_PREPARE_FROZEN:
3305 perf_counter_exit_cpu(cpu);
3315 static struct notifier_block __cpuinitdata perf_cpu_nb = {
3316 .notifier_call = perf_cpu_notify,
3319 void __init perf_counter_init(void)
3321 perf_cpu_notify(&perf_cpu_nb, (unsigned long)CPU_UP_PREPARE,
3322 (void *)(long)smp_processor_id());
3323 register_cpu_notifier(&perf_cpu_nb);
3326 static ssize_t perf_show_reserve_percpu(struct sysdev_class *class, char *buf)
3328 return sprintf(buf, "%d\n", perf_reserved_percpu);
3332 perf_set_reserve_percpu(struct sysdev_class *class,
3336 struct perf_cpu_context *cpuctx;
3340 err = strict_strtoul(buf, 10, &val);
3343 if (val > perf_max_counters)
3346 spin_lock(&perf_resource_lock);
3347 perf_reserved_percpu = val;
3348 for_each_online_cpu(cpu) {
3349 cpuctx = &per_cpu(perf_cpu_context, cpu);
3350 spin_lock_irq(&cpuctx->ctx.lock);
3351 mpt = min(perf_max_counters - cpuctx->ctx.nr_counters,
3352 perf_max_counters - perf_reserved_percpu);
3353 cpuctx->max_pertask = mpt;
3354 spin_unlock_irq(&cpuctx->ctx.lock);
3356 spin_unlock(&perf_resource_lock);
3361 static ssize_t perf_show_overcommit(struct sysdev_class *class, char *buf)
3363 return sprintf(buf, "%d\n", perf_overcommit);
3367 perf_set_overcommit(struct sysdev_class *class, const char *buf, size_t count)
3372 err = strict_strtoul(buf, 10, &val);
3378 spin_lock(&perf_resource_lock);
3379 perf_overcommit = val;
3380 spin_unlock(&perf_resource_lock);
3385 static SYSDEV_CLASS_ATTR(
3388 perf_show_reserve_percpu,
3389 perf_set_reserve_percpu
3392 static SYSDEV_CLASS_ATTR(
3395 perf_show_overcommit,
3399 static struct attribute *perfclass_attrs[] = {
3400 &attr_reserve_percpu.attr,
3401 &attr_overcommit.attr,
3405 static struct attribute_group perfclass_attr_group = {
3406 .attrs = perfclass_attrs,
3407 .name = "perf_counters",
3410 static int __init perf_counter_sysfs_init(void)
3412 return sysfs_create_group(&cpu_sysdev_class.kset.kobj,
3413 &perfclass_attr_group);
3415 device_initcall(perf_counter_sysfs_init);