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 = 512; /* 'free' kb per user */
49 int sysctl_perf_counter_limit __read_mostly = 100000; /* max NMIs per second */
52 * Lock for (sysadmin-configurable) counter reservations:
54 static DEFINE_SPINLOCK(perf_resource_lock);
57 * Architecture provided APIs - weak aliases:
59 extern __weak const struct pmu *hw_perf_counter_init(struct perf_counter *counter)
64 void __weak hw_perf_disable(void) { barrier(); }
65 void __weak hw_perf_enable(void) { barrier(); }
67 void __weak hw_perf_counter_setup(int cpu) { barrier(); }
68 int __weak hw_perf_group_sched_in(struct perf_counter *group_leader,
69 struct perf_cpu_context *cpuctx,
70 struct perf_counter_context *ctx, int cpu)
75 void __weak perf_counter_print_debug(void) { }
77 static DEFINE_PER_CPU(int, disable_count);
79 void __perf_disable(void)
81 __get_cpu_var(disable_count)++;
84 bool __perf_enable(void)
86 return !--__get_cpu_var(disable_count);
89 void perf_disable(void)
95 void perf_enable(void)
101 static void get_ctx(struct perf_counter_context *ctx)
103 atomic_inc(&ctx->refcount);
106 static void put_ctx(struct perf_counter_context *ctx)
108 if (atomic_dec_and_test(&ctx->refcount)) {
110 put_ctx(ctx->parent_ctx);
116 * Add a counter from the lists for its context.
117 * Must be called with ctx->mutex and ctx->lock held.
120 list_add_counter(struct perf_counter *counter, struct perf_counter_context *ctx)
122 struct perf_counter *group_leader = counter->group_leader;
125 * Depending on whether it is a standalone or sibling counter,
126 * add it straight to the context's counter list, or to the group
127 * leader's sibling list:
129 if (group_leader == counter)
130 list_add_tail(&counter->list_entry, &ctx->counter_list);
132 list_add_tail(&counter->list_entry, &group_leader->sibling_list);
133 group_leader->nr_siblings++;
136 list_add_rcu(&counter->event_entry, &ctx->event_list);
141 * Remove a counter from the lists for its context.
142 * Must be called with ctx->mutex and ctx->lock held.
145 list_del_counter(struct perf_counter *counter, struct perf_counter_context *ctx)
147 struct perf_counter *sibling, *tmp;
149 if (list_empty(&counter->list_entry))
153 list_del_init(&counter->list_entry);
154 list_del_rcu(&counter->event_entry);
156 if (counter->group_leader != counter)
157 counter->group_leader->nr_siblings--;
160 * If this was a group counter with sibling counters then
161 * upgrade the siblings to singleton counters by adding them
162 * to the context list directly:
164 list_for_each_entry_safe(sibling, tmp,
165 &counter->sibling_list, list_entry) {
167 list_move_tail(&sibling->list_entry, &ctx->counter_list);
168 sibling->group_leader = sibling;
173 counter_sched_out(struct perf_counter *counter,
174 struct perf_cpu_context *cpuctx,
175 struct perf_counter_context *ctx)
177 if (counter->state != PERF_COUNTER_STATE_ACTIVE)
180 counter->state = PERF_COUNTER_STATE_INACTIVE;
181 counter->tstamp_stopped = ctx->time;
182 counter->pmu->disable(counter);
185 if (!is_software_counter(counter))
186 cpuctx->active_oncpu--;
188 if (counter->hw_event.exclusive || !cpuctx->active_oncpu)
189 cpuctx->exclusive = 0;
193 group_sched_out(struct perf_counter *group_counter,
194 struct perf_cpu_context *cpuctx,
195 struct perf_counter_context *ctx)
197 struct perf_counter *counter;
199 if (group_counter->state != PERF_COUNTER_STATE_ACTIVE)
202 counter_sched_out(group_counter, cpuctx, ctx);
205 * Schedule out siblings (if any):
207 list_for_each_entry(counter, &group_counter->sibling_list, list_entry)
208 counter_sched_out(counter, cpuctx, ctx);
210 if (group_counter->hw_event.exclusive)
211 cpuctx->exclusive = 0;
215 * Mark this context as not being a clone of another.
216 * Called when counters are added to or removed from this context.
217 * We also increment our generation number so that anything that
218 * was cloned from this context before this will not match anything
219 * cloned from this context after this.
221 static void unclone_ctx(struct perf_counter_context *ctx)
224 if (!ctx->parent_ctx)
226 put_ctx(ctx->parent_ctx);
227 ctx->parent_ctx = NULL;
231 * Cross CPU call to remove a performance counter
233 * We disable the counter on the hardware level first. After that we
234 * remove it from the context list.
236 static void __perf_counter_remove_from_context(void *info)
238 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
239 struct perf_counter *counter = info;
240 struct perf_counter_context *ctx = counter->ctx;
244 * If this is a task context, we need to check whether it is
245 * the current task context of this cpu. If not it has been
246 * scheduled out before the smp call arrived.
248 if (ctx->task && cpuctx->task_ctx != ctx)
251 spin_lock_irqsave(&ctx->lock, flags);
253 * Protect the list operation against NMI by disabling the
254 * counters on a global level.
258 counter_sched_out(counter, cpuctx, ctx);
260 list_del_counter(counter, ctx);
264 * Allow more per task counters with respect to the
267 cpuctx->max_pertask =
268 min(perf_max_counters - ctx->nr_counters,
269 perf_max_counters - perf_reserved_percpu);
273 spin_unlock_irqrestore(&ctx->lock, flags);
278 * Remove the counter from a task's (or a CPU's) list of counters.
280 * Must be called with ctx->mutex held.
282 * CPU counters are removed with a smp call. For task counters we only
283 * call when the task is on a CPU.
285 static void perf_counter_remove_from_context(struct perf_counter *counter)
287 struct perf_counter_context *ctx = counter->ctx;
288 struct task_struct *task = ctx->task;
293 * Per cpu counters are removed via an smp call and
294 * the removal is always sucessful.
296 smp_call_function_single(counter->cpu,
297 __perf_counter_remove_from_context,
303 task_oncpu_function_call(task, __perf_counter_remove_from_context,
306 spin_lock_irq(&ctx->lock);
308 * If the context is active we need to retry the smp call.
310 if (ctx->nr_active && !list_empty(&counter->list_entry)) {
311 spin_unlock_irq(&ctx->lock);
316 * The lock prevents that this context is scheduled in so we
317 * can remove the counter safely, if the call above did not
320 if (!list_empty(&counter->list_entry)) {
321 list_del_counter(counter, ctx);
323 spin_unlock_irq(&ctx->lock);
326 static inline u64 perf_clock(void)
328 return cpu_clock(smp_processor_id());
332 * Update the record of the current time in a context.
334 static void update_context_time(struct perf_counter_context *ctx)
336 u64 now = perf_clock();
338 ctx->time += now - ctx->timestamp;
339 ctx->timestamp = now;
343 * Update the total_time_enabled and total_time_running fields for a counter.
345 static void update_counter_times(struct perf_counter *counter)
347 struct perf_counter_context *ctx = counter->ctx;
350 if (counter->state < PERF_COUNTER_STATE_INACTIVE)
353 counter->total_time_enabled = ctx->time - counter->tstamp_enabled;
355 if (counter->state == PERF_COUNTER_STATE_INACTIVE)
356 run_end = counter->tstamp_stopped;
360 counter->total_time_running = run_end - counter->tstamp_running;
364 * Update total_time_enabled and total_time_running for all counters in a group.
366 static void update_group_times(struct perf_counter *leader)
368 struct perf_counter *counter;
370 update_counter_times(leader);
371 list_for_each_entry(counter, &leader->sibling_list, list_entry)
372 update_counter_times(counter);
376 * Cross CPU call to disable a performance counter
378 static void __perf_counter_disable(void *info)
380 struct perf_counter *counter = info;
381 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
382 struct perf_counter_context *ctx = counter->ctx;
386 * If this is a per-task counter, need to check whether this
387 * counter's task is the current task on this cpu.
389 if (ctx->task && cpuctx->task_ctx != ctx)
392 spin_lock_irqsave(&ctx->lock, flags);
395 * If the counter is on, turn it off.
396 * If it is in error state, leave it in error state.
398 if (counter->state >= PERF_COUNTER_STATE_INACTIVE) {
399 update_context_time(ctx);
400 update_counter_times(counter);
401 if (counter == counter->group_leader)
402 group_sched_out(counter, cpuctx, ctx);
404 counter_sched_out(counter, cpuctx, ctx);
405 counter->state = PERF_COUNTER_STATE_OFF;
408 spin_unlock_irqrestore(&ctx->lock, flags);
414 static void perf_counter_disable(struct perf_counter *counter)
416 struct perf_counter_context *ctx = counter->ctx;
417 struct task_struct *task = ctx->task;
421 * Disable the counter on the cpu that it's on
423 smp_call_function_single(counter->cpu, __perf_counter_disable,
429 task_oncpu_function_call(task, __perf_counter_disable, counter);
431 spin_lock_irq(&ctx->lock);
433 * If the counter is still active, we need to retry the cross-call.
435 if (counter->state == PERF_COUNTER_STATE_ACTIVE) {
436 spin_unlock_irq(&ctx->lock);
441 * Since we have the lock this context can't be scheduled
442 * in, so we can change the state safely.
444 if (counter->state == PERF_COUNTER_STATE_INACTIVE) {
445 update_counter_times(counter);
446 counter->state = PERF_COUNTER_STATE_OFF;
449 spin_unlock_irq(&ctx->lock);
453 counter_sched_in(struct perf_counter *counter,
454 struct perf_cpu_context *cpuctx,
455 struct perf_counter_context *ctx,
458 if (counter->state <= PERF_COUNTER_STATE_OFF)
461 counter->state = PERF_COUNTER_STATE_ACTIVE;
462 counter->oncpu = cpu; /* TODO: put 'cpu' into cpuctx->cpu */
464 * The new state must be visible before we turn it on in the hardware:
468 if (counter->pmu->enable(counter)) {
469 counter->state = PERF_COUNTER_STATE_INACTIVE;
474 counter->tstamp_running += ctx->time - counter->tstamp_stopped;
476 if (!is_software_counter(counter))
477 cpuctx->active_oncpu++;
480 if (counter->hw_event.exclusive)
481 cpuctx->exclusive = 1;
487 group_sched_in(struct perf_counter *group_counter,
488 struct perf_cpu_context *cpuctx,
489 struct perf_counter_context *ctx,
492 struct perf_counter *counter, *partial_group;
495 if (group_counter->state == PERF_COUNTER_STATE_OFF)
498 ret = hw_perf_group_sched_in(group_counter, cpuctx, ctx, cpu);
500 return ret < 0 ? ret : 0;
502 group_counter->prev_state = group_counter->state;
503 if (counter_sched_in(group_counter, cpuctx, ctx, cpu))
507 * Schedule in siblings as one group (if any):
509 list_for_each_entry(counter, &group_counter->sibling_list, list_entry) {
510 counter->prev_state = counter->state;
511 if (counter_sched_in(counter, cpuctx, ctx, cpu)) {
512 partial_group = counter;
521 * Groups can be scheduled in as one unit only, so undo any
522 * partial group before returning:
524 list_for_each_entry(counter, &group_counter->sibling_list, list_entry) {
525 if (counter == partial_group)
527 counter_sched_out(counter, cpuctx, ctx);
529 counter_sched_out(group_counter, cpuctx, ctx);
535 * Return 1 for a group consisting entirely of software counters,
536 * 0 if the group contains any hardware counters.
538 static int is_software_only_group(struct perf_counter *leader)
540 struct perf_counter *counter;
542 if (!is_software_counter(leader))
545 list_for_each_entry(counter, &leader->sibling_list, list_entry)
546 if (!is_software_counter(counter))
553 * Work out whether we can put this counter group on the CPU now.
555 static int group_can_go_on(struct perf_counter *counter,
556 struct perf_cpu_context *cpuctx,
560 * Groups consisting entirely of software counters can always go on.
562 if (is_software_only_group(counter))
565 * If an exclusive group is already on, no other hardware
566 * counters can go on.
568 if (cpuctx->exclusive)
571 * If this group is exclusive and there are already
572 * counters on the CPU, it can't go on.
574 if (counter->hw_event.exclusive && cpuctx->active_oncpu)
577 * Otherwise, try to add it if all previous groups were able
583 static void add_counter_to_ctx(struct perf_counter *counter,
584 struct perf_counter_context *ctx)
586 list_add_counter(counter, ctx);
587 counter->prev_state = PERF_COUNTER_STATE_OFF;
588 counter->tstamp_enabled = ctx->time;
589 counter->tstamp_running = ctx->time;
590 counter->tstamp_stopped = ctx->time;
594 * Cross CPU call to install and enable a performance counter
596 * Must be called with ctx->mutex held
598 static void __perf_install_in_context(void *info)
600 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
601 struct perf_counter *counter = info;
602 struct perf_counter_context *ctx = counter->ctx;
603 struct perf_counter *leader = counter->group_leader;
604 int cpu = smp_processor_id();
609 * If this is a task context, we need to check whether it is
610 * the current task context of this cpu. If not it has been
611 * scheduled out before the smp call arrived.
612 * Or possibly this is the right context but it isn't
613 * on this cpu because it had no counters.
615 if (ctx->task && cpuctx->task_ctx != ctx) {
616 if (cpuctx->task_ctx || ctx->task != current)
618 cpuctx->task_ctx = ctx;
621 spin_lock_irqsave(&ctx->lock, flags);
623 update_context_time(ctx);
626 * Protect the list operation against NMI by disabling the
627 * counters on a global level. NOP for non NMI based counters.
631 add_counter_to_ctx(counter, ctx);
634 * Don't put the counter on if it is disabled or if
635 * it is in a group and the group isn't on.
637 if (counter->state != PERF_COUNTER_STATE_INACTIVE ||
638 (leader != counter && leader->state != PERF_COUNTER_STATE_ACTIVE))
642 * An exclusive counter can't go on if there are already active
643 * hardware counters, and no hardware counter can go on if there
644 * is already an exclusive counter on.
646 if (!group_can_go_on(counter, cpuctx, 1))
649 err = counter_sched_in(counter, cpuctx, ctx, cpu);
653 * This counter couldn't go on. If it is in a group
654 * then we have to pull the whole group off.
655 * If the counter group is pinned then put it in error state.
657 if (leader != counter)
658 group_sched_out(leader, cpuctx, ctx);
659 if (leader->hw_event.pinned) {
660 update_group_times(leader);
661 leader->state = PERF_COUNTER_STATE_ERROR;
665 if (!err && !ctx->task && cpuctx->max_pertask)
666 cpuctx->max_pertask--;
671 spin_unlock_irqrestore(&ctx->lock, flags);
675 * Attach a performance counter to a context
677 * First we add the counter to the list with the hardware enable bit
678 * in counter->hw_config cleared.
680 * If the counter is attached to a task which is on a CPU we use a smp
681 * call to enable it in the task context. The task might have been
682 * scheduled away, but we check this in the smp call again.
684 * Must be called with ctx->mutex held.
687 perf_install_in_context(struct perf_counter_context *ctx,
688 struct perf_counter *counter,
691 struct task_struct *task = ctx->task;
695 * Per cpu counters are installed via an smp call and
696 * the install is always sucessful.
698 smp_call_function_single(cpu, __perf_install_in_context,
704 task_oncpu_function_call(task, __perf_install_in_context,
707 spin_lock_irq(&ctx->lock);
709 * we need to retry the smp call.
711 if (ctx->is_active && list_empty(&counter->list_entry)) {
712 spin_unlock_irq(&ctx->lock);
717 * The lock prevents that this context is scheduled in so we
718 * can add the counter safely, if it the call above did not
721 if (list_empty(&counter->list_entry))
722 add_counter_to_ctx(counter, ctx);
723 spin_unlock_irq(&ctx->lock);
727 * Cross CPU call to enable a performance counter
729 static void __perf_counter_enable(void *info)
731 struct perf_counter *counter = info;
732 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
733 struct perf_counter_context *ctx = counter->ctx;
734 struct perf_counter *leader = counter->group_leader;
739 * If this is a per-task counter, need to check whether this
740 * counter's task is the current task on this cpu.
742 if (ctx->task && cpuctx->task_ctx != ctx) {
743 if (cpuctx->task_ctx || ctx->task != current)
745 cpuctx->task_ctx = ctx;
748 spin_lock_irqsave(&ctx->lock, flags);
750 update_context_time(ctx);
752 counter->prev_state = counter->state;
753 if (counter->state >= PERF_COUNTER_STATE_INACTIVE)
755 counter->state = PERF_COUNTER_STATE_INACTIVE;
756 counter->tstamp_enabled = ctx->time - counter->total_time_enabled;
759 * If the counter is in a group and isn't the group leader,
760 * then don't put it on unless the group is on.
762 if (leader != counter && leader->state != PERF_COUNTER_STATE_ACTIVE)
765 if (!group_can_go_on(counter, cpuctx, 1)) {
769 if (counter == leader)
770 err = group_sched_in(counter, cpuctx, ctx,
773 err = counter_sched_in(counter, cpuctx, ctx,
780 * If this counter can't go on and it's part of a
781 * group, then the whole group has to come off.
783 if (leader != counter)
784 group_sched_out(leader, cpuctx, ctx);
785 if (leader->hw_event.pinned) {
786 update_group_times(leader);
787 leader->state = PERF_COUNTER_STATE_ERROR;
792 spin_unlock_irqrestore(&ctx->lock, flags);
798 static void perf_counter_enable(struct perf_counter *counter)
800 struct perf_counter_context *ctx = counter->ctx;
801 struct task_struct *task = ctx->task;
805 * Enable the counter on the cpu that it's on
807 smp_call_function_single(counter->cpu, __perf_counter_enable,
812 spin_lock_irq(&ctx->lock);
813 if (counter->state >= PERF_COUNTER_STATE_INACTIVE)
817 * If the counter is in error state, clear that first.
818 * That way, if we see the counter in error state below, we
819 * know that it has gone back into error state, as distinct
820 * from the task having been scheduled away before the
821 * cross-call arrived.
823 if (counter->state == PERF_COUNTER_STATE_ERROR)
824 counter->state = PERF_COUNTER_STATE_OFF;
827 spin_unlock_irq(&ctx->lock);
828 task_oncpu_function_call(task, __perf_counter_enable, counter);
830 spin_lock_irq(&ctx->lock);
833 * If the context is active and the counter is still off,
834 * we need to retry the cross-call.
836 if (ctx->is_active && counter->state == PERF_COUNTER_STATE_OFF)
840 * Since we have the lock this context can't be scheduled
841 * in, so we can change the state safely.
843 if (counter->state == PERF_COUNTER_STATE_OFF) {
844 counter->state = PERF_COUNTER_STATE_INACTIVE;
845 counter->tstamp_enabled =
846 ctx->time - counter->total_time_enabled;
849 spin_unlock_irq(&ctx->lock);
852 static int perf_counter_refresh(struct perf_counter *counter, int refresh)
855 * not supported on inherited counters
857 if (counter->hw_event.inherit)
860 atomic_add(refresh, &counter->event_limit);
861 perf_counter_enable(counter);
866 void __perf_counter_sched_out(struct perf_counter_context *ctx,
867 struct perf_cpu_context *cpuctx)
869 struct perf_counter *counter;
871 spin_lock(&ctx->lock);
873 if (likely(!ctx->nr_counters))
875 update_context_time(ctx);
878 if (ctx->nr_active) {
879 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
880 if (counter != counter->group_leader)
881 counter_sched_out(counter, cpuctx, ctx);
883 group_sched_out(counter, cpuctx, ctx);
888 spin_unlock(&ctx->lock);
892 * Test whether two contexts are equivalent, i.e. whether they
893 * have both been cloned from the same version of the same context
894 * and they both have the same number of enabled counters.
895 * If the number of enabled counters is the same, then the set
896 * of enabled counters should be the same, because these are both
897 * inherited contexts, therefore we can't access individual counters
898 * in them directly with an fd; we can only enable/disable all
899 * counters via prctl, or enable/disable all counters in a family
900 * via ioctl, which will have the same effect on both contexts.
902 static int context_equiv(struct perf_counter_context *ctx1,
903 struct perf_counter_context *ctx2)
905 return ctx1->parent_ctx && ctx1->parent_ctx == ctx2->parent_ctx
906 && ctx1->parent_gen == ctx2->parent_gen;
910 * Called from scheduler to remove the counters of the current task,
911 * with interrupts disabled.
913 * We stop each counter and update the counter value in counter->count.
915 * This does not protect us against NMI, but disable()
916 * sets the disabled bit in the control field of counter _before_
917 * accessing the counter control register. If a NMI hits, then it will
918 * not restart the counter.
920 void perf_counter_task_sched_out(struct task_struct *task,
921 struct task_struct *next, int cpu)
923 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
924 struct perf_counter_context *ctx = task->perf_counter_ctxp;
925 struct perf_counter_context *next_ctx;
926 struct pt_regs *regs;
928 regs = task_pt_regs(task);
929 perf_swcounter_event(PERF_COUNT_CONTEXT_SWITCHES, 1, 1, regs, 0);
931 if (likely(!ctx || !cpuctx->task_ctx))
934 update_context_time(ctx);
935 next_ctx = next->perf_counter_ctxp;
936 if (next_ctx && context_equiv(ctx, next_ctx)) {
937 task->perf_counter_ctxp = next_ctx;
938 next->perf_counter_ctxp = ctx;
940 next_ctx->task = task;
944 __perf_counter_sched_out(ctx, cpuctx);
946 cpuctx->task_ctx = NULL;
949 static void __perf_counter_task_sched_out(struct perf_counter_context *ctx)
951 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
953 if (!cpuctx->task_ctx)
955 __perf_counter_sched_out(ctx, cpuctx);
956 cpuctx->task_ctx = NULL;
959 static void perf_counter_cpu_sched_out(struct perf_cpu_context *cpuctx)
961 __perf_counter_sched_out(&cpuctx->ctx, cpuctx);
965 __perf_counter_sched_in(struct perf_counter_context *ctx,
966 struct perf_cpu_context *cpuctx, int cpu)
968 struct perf_counter *counter;
971 spin_lock(&ctx->lock);
973 if (likely(!ctx->nr_counters))
976 ctx->timestamp = perf_clock();
981 * First go through the list and put on any pinned groups
982 * in order to give them the best chance of going on.
984 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
985 if (counter->state <= PERF_COUNTER_STATE_OFF ||
986 !counter->hw_event.pinned)
988 if (counter->cpu != -1 && counter->cpu != cpu)
991 if (counter != counter->group_leader)
992 counter_sched_in(counter, cpuctx, ctx, cpu);
994 if (group_can_go_on(counter, cpuctx, 1))
995 group_sched_in(counter, cpuctx, ctx, cpu);
999 * If this pinned group hasn't been scheduled,
1000 * put it in error state.
1002 if (counter->state == PERF_COUNTER_STATE_INACTIVE) {
1003 update_group_times(counter);
1004 counter->state = PERF_COUNTER_STATE_ERROR;
1008 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
1010 * Ignore counters in OFF or ERROR state, and
1011 * ignore pinned counters since we did them already.
1013 if (counter->state <= PERF_COUNTER_STATE_OFF ||
1014 counter->hw_event.pinned)
1018 * Listen to the 'cpu' scheduling filter constraint
1021 if (counter->cpu != -1 && counter->cpu != cpu)
1024 if (counter != counter->group_leader) {
1025 if (counter_sched_in(counter, cpuctx, ctx, cpu))
1028 if (group_can_go_on(counter, cpuctx, can_add_hw)) {
1029 if (group_sched_in(counter, cpuctx, ctx, cpu))
1036 spin_unlock(&ctx->lock);
1040 * Called from scheduler to add the counters of the current task
1041 * with interrupts disabled.
1043 * We restore the counter value and then enable it.
1045 * This does not protect us against NMI, but enable()
1046 * sets the enabled bit in the control field of counter _before_
1047 * accessing the counter control register. If a NMI hits, then it will
1048 * keep the counter running.
1050 void perf_counter_task_sched_in(struct task_struct *task, int cpu)
1052 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
1053 struct perf_counter_context *ctx = task->perf_counter_ctxp;
1057 if (cpuctx->task_ctx == ctx)
1059 __perf_counter_sched_in(ctx, cpuctx, cpu);
1060 cpuctx->task_ctx = ctx;
1063 static void perf_counter_cpu_sched_in(struct perf_cpu_context *cpuctx, int cpu)
1065 struct perf_counter_context *ctx = &cpuctx->ctx;
1067 __perf_counter_sched_in(ctx, cpuctx, cpu);
1070 #define MAX_INTERRUPTS (~0ULL)
1072 static void perf_log_throttle(struct perf_counter *counter, int enable);
1073 static void perf_log_period(struct perf_counter *counter, u64 period);
1075 static void perf_adjust_freq(struct perf_counter_context *ctx)
1077 struct perf_counter *counter;
1078 u64 interrupts, irq_period;
1082 spin_lock(&ctx->lock);
1083 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
1084 if (counter->state != PERF_COUNTER_STATE_ACTIVE)
1087 interrupts = counter->hw.interrupts;
1088 counter->hw.interrupts = 0;
1090 if (interrupts == MAX_INTERRUPTS) {
1091 perf_log_throttle(counter, 1);
1092 counter->pmu->unthrottle(counter);
1093 interrupts = 2*sysctl_perf_counter_limit/HZ;
1096 if (!counter->hw_event.freq || !counter->hw_event.irq_freq)
1099 events = HZ * interrupts * counter->hw.irq_period;
1100 period = div64_u64(events, counter->hw_event.irq_freq);
1102 delta = (s64)(1 + period - counter->hw.irq_period);
1105 irq_period = counter->hw.irq_period + delta;
1110 perf_log_period(counter, irq_period);
1112 counter->hw.irq_period = irq_period;
1114 spin_unlock(&ctx->lock);
1118 * Round-robin a context's counters:
1120 static void rotate_ctx(struct perf_counter_context *ctx)
1122 struct perf_counter *counter;
1124 if (!ctx->nr_counters)
1127 spin_lock(&ctx->lock);
1129 * Rotate the first entry last (works just fine for group counters too):
1132 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
1133 list_move_tail(&counter->list_entry, &ctx->counter_list);
1138 spin_unlock(&ctx->lock);
1141 void perf_counter_task_tick(struct task_struct *curr, int cpu)
1143 struct perf_cpu_context *cpuctx;
1144 struct perf_counter_context *ctx;
1146 if (!atomic_read(&nr_counters))
1149 cpuctx = &per_cpu(perf_cpu_context, cpu);
1150 ctx = curr->perf_counter_ctxp;
1152 perf_adjust_freq(&cpuctx->ctx);
1154 perf_adjust_freq(ctx);
1156 perf_counter_cpu_sched_out(cpuctx);
1158 __perf_counter_task_sched_out(ctx);
1160 rotate_ctx(&cpuctx->ctx);
1164 perf_counter_cpu_sched_in(cpuctx, cpu);
1166 perf_counter_task_sched_in(curr, cpu);
1170 * Cross CPU call to read the hardware counter
1172 static void __read(void *info)
1174 struct perf_counter *counter = info;
1175 struct perf_counter_context *ctx = counter->ctx;
1176 unsigned long flags;
1178 local_irq_save(flags);
1180 update_context_time(ctx);
1181 counter->pmu->read(counter);
1182 update_counter_times(counter);
1183 local_irq_restore(flags);
1186 static u64 perf_counter_read(struct perf_counter *counter)
1189 * If counter is enabled and currently active on a CPU, update the
1190 * value in the counter structure:
1192 if (counter->state == PERF_COUNTER_STATE_ACTIVE) {
1193 smp_call_function_single(counter->oncpu,
1194 __read, counter, 1);
1195 } else if (counter->state == PERF_COUNTER_STATE_INACTIVE) {
1196 update_counter_times(counter);
1199 return atomic64_read(&counter->count);
1203 * Initialize the perf_counter context in a task_struct:
1206 __perf_counter_init_context(struct perf_counter_context *ctx,
1207 struct task_struct *task)
1209 memset(ctx, 0, sizeof(*ctx));
1210 spin_lock_init(&ctx->lock);
1211 mutex_init(&ctx->mutex);
1212 INIT_LIST_HEAD(&ctx->counter_list);
1213 INIT_LIST_HEAD(&ctx->event_list);
1214 atomic_set(&ctx->refcount, 1);
1218 static void put_context(struct perf_counter_context *ctx)
1221 put_task_struct(ctx->task);
1224 static struct perf_counter_context *find_get_context(pid_t pid, int cpu)
1226 struct perf_cpu_context *cpuctx;
1227 struct perf_counter_context *ctx;
1228 struct perf_counter_context *tctx;
1229 struct task_struct *task;
1232 * If cpu is not a wildcard then this is a percpu counter:
1235 /* Must be root to operate on a CPU counter: */
1236 if (sysctl_perf_counter_priv && !capable(CAP_SYS_ADMIN))
1237 return ERR_PTR(-EACCES);
1239 if (cpu < 0 || cpu > num_possible_cpus())
1240 return ERR_PTR(-EINVAL);
1243 * We could be clever and allow to attach a counter to an
1244 * offline CPU and activate it when the CPU comes up, but
1247 if (!cpu_isset(cpu, cpu_online_map))
1248 return ERR_PTR(-ENODEV);
1250 cpuctx = &per_cpu(perf_cpu_context, cpu);
1260 task = find_task_by_vpid(pid);
1262 get_task_struct(task);
1266 return ERR_PTR(-ESRCH);
1268 /* Reuse ptrace permission checks for now. */
1269 if (!ptrace_may_access(task, PTRACE_MODE_READ)) {
1270 put_task_struct(task);
1271 return ERR_PTR(-EACCES);
1274 ctx = task->perf_counter_ctxp;
1276 ctx = kmalloc(sizeof(struct perf_counter_context), GFP_KERNEL);
1278 put_task_struct(task);
1279 return ERR_PTR(-ENOMEM);
1281 __perf_counter_init_context(ctx, task);
1283 * Make sure other cpus see correct values for *ctx
1284 * once task->perf_counter_ctxp is visible to them.
1287 tctx = cmpxchg(&task->perf_counter_ctxp, NULL, ctx);
1290 * We raced with some other task; use
1291 * the context they set.
1301 static void free_counter_rcu(struct rcu_head *head)
1303 struct perf_counter *counter;
1305 counter = container_of(head, struct perf_counter, rcu_head);
1306 put_ctx(counter->ctx);
1310 static void perf_pending_sync(struct perf_counter *counter);
1312 static void free_counter(struct perf_counter *counter)
1314 perf_pending_sync(counter);
1316 atomic_dec(&nr_counters);
1317 if (counter->hw_event.mmap)
1318 atomic_dec(&nr_mmap_tracking);
1319 if (counter->hw_event.munmap)
1320 atomic_dec(&nr_munmap_tracking);
1321 if (counter->hw_event.comm)
1322 atomic_dec(&nr_comm_tracking);
1324 if (counter->destroy)
1325 counter->destroy(counter);
1327 call_rcu(&counter->rcu_head, free_counter_rcu);
1331 * Called when the last reference to the file is gone.
1333 static int perf_release(struct inode *inode, struct file *file)
1335 struct perf_counter *counter = file->private_data;
1336 struct perf_counter_context *ctx = counter->ctx;
1338 file->private_data = NULL;
1340 mutex_lock(&ctx->mutex);
1341 perf_counter_remove_from_context(counter);
1342 mutex_unlock(&ctx->mutex);
1344 mutex_lock(&counter->owner->perf_counter_mutex);
1345 list_del_init(&counter->owner_entry);
1346 mutex_unlock(&counter->owner->perf_counter_mutex);
1347 put_task_struct(counter->owner);
1349 free_counter(counter);
1356 * Read the performance counter - simple non blocking version for now
1359 perf_read_hw(struct perf_counter *counter, char __user *buf, size_t count)
1365 * Return end-of-file for a read on a counter that is in
1366 * error state (i.e. because it was pinned but it couldn't be
1367 * scheduled on to the CPU at some point).
1369 if (counter->state == PERF_COUNTER_STATE_ERROR)
1372 mutex_lock(&counter->child_mutex);
1373 values[0] = perf_counter_read(counter);
1375 if (counter->hw_event.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
1376 values[n++] = counter->total_time_enabled +
1377 atomic64_read(&counter->child_total_time_enabled);
1378 if (counter->hw_event.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
1379 values[n++] = counter->total_time_running +
1380 atomic64_read(&counter->child_total_time_running);
1381 mutex_unlock(&counter->child_mutex);
1383 if (count < n * sizeof(u64))
1385 count = n * sizeof(u64);
1387 if (copy_to_user(buf, values, count))
1394 perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
1396 struct perf_counter *counter = file->private_data;
1398 return perf_read_hw(counter, buf, count);
1401 static unsigned int perf_poll(struct file *file, poll_table *wait)
1403 struct perf_counter *counter = file->private_data;
1404 struct perf_mmap_data *data;
1405 unsigned int events = POLL_HUP;
1408 data = rcu_dereference(counter->data);
1410 events = atomic_xchg(&data->poll, 0);
1413 poll_wait(file, &counter->waitq, wait);
1418 static void perf_counter_reset(struct perf_counter *counter)
1420 (void)perf_counter_read(counter);
1421 atomic64_set(&counter->count, 0);
1422 perf_counter_update_userpage(counter);
1425 static void perf_counter_for_each_sibling(struct perf_counter *counter,
1426 void (*func)(struct perf_counter *))
1428 struct perf_counter_context *ctx = counter->ctx;
1429 struct perf_counter *sibling;
1431 mutex_lock(&ctx->mutex);
1432 counter = counter->group_leader;
1435 list_for_each_entry(sibling, &counter->sibling_list, list_entry)
1437 mutex_unlock(&ctx->mutex);
1440 static void perf_counter_for_each_child(struct perf_counter *counter,
1441 void (*func)(struct perf_counter *))
1443 struct perf_counter *child;
1445 mutex_lock(&counter->child_mutex);
1447 list_for_each_entry(child, &counter->child_list, child_list)
1449 mutex_unlock(&counter->child_mutex);
1452 static void perf_counter_for_each(struct perf_counter *counter,
1453 void (*func)(struct perf_counter *))
1455 struct perf_counter *child;
1457 mutex_lock(&counter->child_mutex);
1458 perf_counter_for_each_sibling(counter, func);
1459 list_for_each_entry(child, &counter->child_list, child_list)
1460 perf_counter_for_each_sibling(child, func);
1461 mutex_unlock(&counter->child_mutex);
1464 static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1466 struct perf_counter *counter = file->private_data;
1467 void (*func)(struct perf_counter *);
1471 case PERF_COUNTER_IOC_ENABLE:
1472 func = perf_counter_enable;
1474 case PERF_COUNTER_IOC_DISABLE:
1475 func = perf_counter_disable;
1477 case PERF_COUNTER_IOC_RESET:
1478 func = perf_counter_reset;
1481 case PERF_COUNTER_IOC_REFRESH:
1482 return perf_counter_refresh(counter, arg);
1487 if (flags & PERF_IOC_FLAG_GROUP)
1488 perf_counter_for_each(counter, func);
1490 perf_counter_for_each_child(counter, func);
1495 int perf_counter_task_enable(void)
1497 struct perf_counter *counter;
1499 mutex_lock(¤t->perf_counter_mutex);
1500 list_for_each_entry(counter, ¤t->perf_counter_list, owner_entry)
1501 perf_counter_for_each_child(counter, perf_counter_enable);
1502 mutex_unlock(¤t->perf_counter_mutex);
1507 int perf_counter_task_disable(void)
1509 struct perf_counter *counter;
1511 mutex_lock(¤t->perf_counter_mutex);
1512 list_for_each_entry(counter, ¤t->perf_counter_list, owner_entry)
1513 perf_counter_for_each_child(counter, perf_counter_disable);
1514 mutex_unlock(¤t->perf_counter_mutex);
1520 * Callers need to ensure there can be no nesting of this function, otherwise
1521 * the seqlock logic goes bad. We can not serialize this because the arch
1522 * code calls this from NMI context.
1524 void perf_counter_update_userpage(struct perf_counter *counter)
1526 struct perf_mmap_data *data;
1527 struct perf_counter_mmap_page *userpg;
1530 data = rcu_dereference(counter->data);
1534 userpg = data->user_page;
1537 * Disable preemption so as to not let the corresponding user-space
1538 * spin too long if we get preempted.
1543 userpg->index = counter->hw.idx;
1544 userpg->offset = atomic64_read(&counter->count);
1545 if (counter->state == PERF_COUNTER_STATE_ACTIVE)
1546 userpg->offset -= atomic64_read(&counter->hw.prev_count);
1555 static int perf_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1557 struct perf_counter *counter = vma->vm_file->private_data;
1558 struct perf_mmap_data *data;
1559 int ret = VM_FAULT_SIGBUS;
1562 data = rcu_dereference(counter->data);
1566 if (vmf->pgoff == 0) {
1567 vmf->page = virt_to_page(data->user_page);
1569 int nr = vmf->pgoff - 1;
1571 if ((unsigned)nr > data->nr_pages)
1574 vmf->page = virt_to_page(data->data_pages[nr]);
1576 get_page(vmf->page);
1584 static int perf_mmap_data_alloc(struct perf_counter *counter, int nr_pages)
1586 struct perf_mmap_data *data;
1590 WARN_ON(atomic_read(&counter->mmap_count));
1592 size = sizeof(struct perf_mmap_data);
1593 size += nr_pages * sizeof(void *);
1595 data = kzalloc(size, GFP_KERNEL);
1599 data->user_page = (void *)get_zeroed_page(GFP_KERNEL);
1600 if (!data->user_page)
1601 goto fail_user_page;
1603 for (i = 0; i < nr_pages; i++) {
1604 data->data_pages[i] = (void *)get_zeroed_page(GFP_KERNEL);
1605 if (!data->data_pages[i])
1606 goto fail_data_pages;
1609 data->nr_pages = nr_pages;
1610 atomic_set(&data->lock, -1);
1612 rcu_assign_pointer(counter->data, data);
1617 for (i--; i >= 0; i--)
1618 free_page((unsigned long)data->data_pages[i]);
1620 free_page((unsigned long)data->user_page);
1629 static void __perf_mmap_data_free(struct rcu_head *rcu_head)
1631 struct perf_mmap_data *data = container_of(rcu_head,
1632 struct perf_mmap_data, rcu_head);
1635 free_page((unsigned long)data->user_page);
1636 for (i = 0; i < data->nr_pages; i++)
1637 free_page((unsigned long)data->data_pages[i]);
1641 static void perf_mmap_data_free(struct perf_counter *counter)
1643 struct perf_mmap_data *data = counter->data;
1645 WARN_ON(atomic_read(&counter->mmap_count));
1647 rcu_assign_pointer(counter->data, NULL);
1648 call_rcu(&data->rcu_head, __perf_mmap_data_free);
1651 static void perf_mmap_open(struct vm_area_struct *vma)
1653 struct perf_counter *counter = vma->vm_file->private_data;
1655 atomic_inc(&counter->mmap_count);
1658 static void perf_mmap_close(struct vm_area_struct *vma)
1660 struct perf_counter *counter = vma->vm_file->private_data;
1662 if (atomic_dec_and_mutex_lock(&counter->mmap_count,
1663 &counter->mmap_mutex)) {
1664 struct user_struct *user = current_user();
1666 atomic_long_sub(counter->data->nr_pages + 1, &user->locked_vm);
1667 vma->vm_mm->locked_vm -= counter->data->nr_locked;
1668 perf_mmap_data_free(counter);
1669 mutex_unlock(&counter->mmap_mutex);
1673 static struct vm_operations_struct perf_mmap_vmops = {
1674 .open = perf_mmap_open,
1675 .close = perf_mmap_close,
1676 .fault = perf_mmap_fault,
1679 static int perf_mmap(struct file *file, struct vm_area_struct *vma)
1681 struct perf_counter *counter = file->private_data;
1682 struct user_struct *user = current_user();
1683 unsigned long vma_size;
1684 unsigned long nr_pages;
1685 unsigned long user_locked, user_lock_limit;
1686 unsigned long locked, lock_limit;
1687 long user_extra, extra;
1690 if (!(vma->vm_flags & VM_SHARED) || (vma->vm_flags & VM_WRITE))
1693 vma_size = vma->vm_end - vma->vm_start;
1694 nr_pages = (vma_size / PAGE_SIZE) - 1;
1697 * If we have data pages ensure they're a power-of-two number, so we
1698 * can do bitmasks instead of modulo.
1700 if (nr_pages != 0 && !is_power_of_2(nr_pages))
1703 if (vma_size != PAGE_SIZE * (1 + nr_pages))
1706 if (vma->vm_pgoff != 0)
1709 mutex_lock(&counter->mmap_mutex);
1710 if (atomic_inc_not_zero(&counter->mmap_count)) {
1711 if (nr_pages != counter->data->nr_pages)
1716 user_extra = nr_pages + 1;
1717 user_lock_limit = sysctl_perf_counter_mlock >> (PAGE_SHIFT - 10);
1720 * Increase the limit linearly with more CPUs:
1722 user_lock_limit *= num_online_cpus();
1724 user_locked = atomic_long_read(&user->locked_vm) + user_extra;
1727 if (user_locked > user_lock_limit)
1728 extra = user_locked - user_lock_limit;
1730 lock_limit = current->signal->rlim[RLIMIT_MEMLOCK].rlim_cur;
1731 lock_limit >>= PAGE_SHIFT;
1732 locked = vma->vm_mm->locked_vm + extra;
1734 if ((locked > lock_limit) && !capable(CAP_IPC_LOCK)) {
1739 WARN_ON(counter->data);
1740 ret = perf_mmap_data_alloc(counter, nr_pages);
1744 atomic_set(&counter->mmap_count, 1);
1745 atomic_long_add(user_extra, &user->locked_vm);
1746 vma->vm_mm->locked_vm += extra;
1747 counter->data->nr_locked = extra;
1749 mutex_unlock(&counter->mmap_mutex);
1751 vma->vm_flags &= ~VM_MAYWRITE;
1752 vma->vm_flags |= VM_RESERVED;
1753 vma->vm_ops = &perf_mmap_vmops;
1758 static int perf_fasync(int fd, struct file *filp, int on)
1760 struct perf_counter *counter = filp->private_data;
1761 struct inode *inode = filp->f_path.dentry->d_inode;
1764 mutex_lock(&inode->i_mutex);
1765 retval = fasync_helper(fd, filp, on, &counter->fasync);
1766 mutex_unlock(&inode->i_mutex);
1774 static const struct file_operations perf_fops = {
1775 .release = perf_release,
1778 .unlocked_ioctl = perf_ioctl,
1779 .compat_ioctl = perf_ioctl,
1781 .fasync = perf_fasync,
1785 * Perf counter wakeup
1787 * If there's data, ensure we set the poll() state and publish everything
1788 * to user-space before waking everybody up.
1791 void perf_counter_wakeup(struct perf_counter *counter)
1793 wake_up_all(&counter->waitq);
1795 if (counter->pending_kill) {
1796 kill_fasync(&counter->fasync, SIGIO, counter->pending_kill);
1797 counter->pending_kill = 0;
1804 * Handle the case where we need to wakeup up from NMI (or rq->lock) context.
1806 * The NMI bit means we cannot possibly take locks. Therefore, maintain a
1807 * single linked list and use cmpxchg() to add entries lockless.
1810 static void perf_pending_counter(struct perf_pending_entry *entry)
1812 struct perf_counter *counter = container_of(entry,
1813 struct perf_counter, pending);
1815 if (counter->pending_disable) {
1816 counter->pending_disable = 0;
1817 perf_counter_disable(counter);
1820 if (counter->pending_wakeup) {
1821 counter->pending_wakeup = 0;
1822 perf_counter_wakeup(counter);
1826 #define PENDING_TAIL ((struct perf_pending_entry *)-1UL)
1828 static DEFINE_PER_CPU(struct perf_pending_entry *, perf_pending_head) = {
1832 static void perf_pending_queue(struct perf_pending_entry *entry,
1833 void (*func)(struct perf_pending_entry *))
1835 struct perf_pending_entry **head;
1837 if (cmpxchg(&entry->next, NULL, PENDING_TAIL) != NULL)
1842 head = &get_cpu_var(perf_pending_head);
1845 entry->next = *head;
1846 } while (cmpxchg(head, entry->next, entry) != entry->next);
1848 set_perf_counter_pending();
1850 put_cpu_var(perf_pending_head);
1853 static int __perf_pending_run(void)
1855 struct perf_pending_entry *list;
1858 list = xchg(&__get_cpu_var(perf_pending_head), PENDING_TAIL);
1859 while (list != PENDING_TAIL) {
1860 void (*func)(struct perf_pending_entry *);
1861 struct perf_pending_entry *entry = list;
1868 * Ensure we observe the unqueue before we issue the wakeup,
1869 * so that we won't be waiting forever.
1870 * -- see perf_not_pending().
1881 static inline int perf_not_pending(struct perf_counter *counter)
1884 * If we flush on whatever cpu we run, there is a chance we don't
1888 __perf_pending_run();
1892 * Ensure we see the proper queue state before going to sleep
1893 * so that we do not miss the wakeup. -- see perf_pending_handle()
1896 return counter->pending.next == NULL;
1899 static void perf_pending_sync(struct perf_counter *counter)
1901 wait_event(counter->waitq, perf_not_pending(counter));
1904 void perf_counter_do_pending(void)
1906 __perf_pending_run();
1910 * Callchain support -- arch specific
1913 __weak struct perf_callchain_entry *perf_callchain(struct pt_regs *regs)
1922 struct perf_output_handle {
1923 struct perf_counter *counter;
1924 struct perf_mmap_data *data;
1925 unsigned int offset;
1930 unsigned long flags;
1933 static void perf_output_wakeup(struct perf_output_handle *handle)
1935 atomic_set(&handle->data->poll, POLL_IN);
1938 handle->counter->pending_wakeup = 1;
1939 perf_pending_queue(&handle->counter->pending,
1940 perf_pending_counter);
1942 perf_counter_wakeup(handle->counter);
1946 * Curious locking construct.
1948 * We need to ensure a later event doesn't publish a head when a former
1949 * event isn't done writing. However since we need to deal with NMIs we
1950 * cannot fully serialize things.
1952 * What we do is serialize between CPUs so we only have to deal with NMI
1953 * nesting on a single CPU.
1955 * We only publish the head (and generate a wakeup) when the outer-most
1958 static void perf_output_lock(struct perf_output_handle *handle)
1960 struct perf_mmap_data *data = handle->data;
1965 local_irq_save(handle->flags);
1966 cpu = smp_processor_id();
1968 if (in_nmi() && atomic_read(&data->lock) == cpu)
1971 while (atomic_cmpxchg(&data->lock, -1, cpu) != -1)
1977 static void perf_output_unlock(struct perf_output_handle *handle)
1979 struct perf_mmap_data *data = handle->data;
1982 data->done_head = data->head;
1984 if (!handle->locked)
1989 * The xchg implies a full barrier that ensures all writes are done
1990 * before we publish the new head, matched by a rmb() in userspace when
1991 * reading this position.
1993 while ((head = atomic_xchg(&data->done_head, 0)))
1994 data->user_page->data_head = head;
1997 * NMI can happen here, which means we can miss a done_head update.
2000 cpu = atomic_xchg(&data->lock, -1);
2001 WARN_ON_ONCE(cpu != smp_processor_id());
2004 * Therefore we have to validate we did not indeed do so.
2006 if (unlikely(atomic_read(&data->done_head))) {
2008 * Since we had it locked, we can lock it again.
2010 while (atomic_cmpxchg(&data->lock, -1, cpu) != -1)
2016 if (atomic_xchg(&data->wakeup, 0))
2017 perf_output_wakeup(handle);
2019 local_irq_restore(handle->flags);
2022 static int perf_output_begin(struct perf_output_handle *handle,
2023 struct perf_counter *counter, unsigned int size,
2024 int nmi, int overflow)
2026 struct perf_mmap_data *data;
2027 unsigned int offset, head;
2030 * For inherited counters we send all the output towards the parent.
2032 if (counter->parent)
2033 counter = counter->parent;
2036 data = rcu_dereference(counter->data);
2040 handle->data = data;
2041 handle->counter = counter;
2043 handle->overflow = overflow;
2045 if (!data->nr_pages)
2048 perf_output_lock(handle);
2051 offset = head = atomic_read(&data->head);
2053 } while (atomic_cmpxchg(&data->head, offset, head) != offset);
2055 handle->offset = offset;
2056 handle->head = head;
2058 if ((offset >> PAGE_SHIFT) != (head >> PAGE_SHIFT))
2059 atomic_set(&data->wakeup, 1);
2064 perf_output_wakeup(handle);
2071 static void perf_output_copy(struct perf_output_handle *handle,
2072 void *buf, unsigned int len)
2074 unsigned int pages_mask;
2075 unsigned int offset;
2079 offset = handle->offset;
2080 pages_mask = handle->data->nr_pages - 1;
2081 pages = handle->data->data_pages;
2084 unsigned int page_offset;
2087 nr = (offset >> PAGE_SHIFT) & pages_mask;
2088 page_offset = offset & (PAGE_SIZE - 1);
2089 size = min_t(unsigned int, PAGE_SIZE - page_offset, len);
2091 memcpy(pages[nr] + page_offset, buf, size);
2098 handle->offset = offset;
2101 * Check we didn't copy past our reservation window, taking the
2102 * possible unsigned int wrap into account.
2104 WARN_ON_ONCE(((int)(handle->head - handle->offset)) < 0);
2107 #define perf_output_put(handle, x) \
2108 perf_output_copy((handle), &(x), sizeof(x))
2110 static void perf_output_end(struct perf_output_handle *handle)
2112 struct perf_counter *counter = handle->counter;
2113 struct perf_mmap_data *data = handle->data;
2115 int wakeup_events = counter->hw_event.wakeup_events;
2117 if (handle->overflow && wakeup_events) {
2118 int events = atomic_inc_return(&data->events);
2119 if (events >= wakeup_events) {
2120 atomic_sub(wakeup_events, &data->events);
2121 atomic_set(&data->wakeup, 1);
2125 perf_output_unlock(handle);
2129 static void perf_counter_output(struct perf_counter *counter,
2130 int nmi, struct pt_regs *regs, u64 addr)
2133 u64 record_type = counter->hw_event.record_type;
2134 struct perf_output_handle handle;
2135 struct perf_event_header header;
2144 struct perf_callchain_entry *callchain = NULL;
2145 int callchain_size = 0;
2152 header.size = sizeof(header);
2154 header.misc = PERF_EVENT_MISC_OVERFLOW;
2155 header.misc |= perf_misc_flags(regs);
2157 if (record_type & PERF_RECORD_IP) {
2158 ip = perf_instruction_pointer(regs);
2159 header.type |= PERF_RECORD_IP;
2160 header.size += sizeof(ip);
2163 if (record_type & PERF_RECORD_TID) {
2164 /* namespace issues */
2165 tid_entry.pid = current->group_leader->pid;
2166 tid_entry.tid = current->pid;
2168 header.type |= PERF_RECORD_TID;
2169 header.size += sizeof(tid_entry);
2172 if (record_type & PERF_RECORD_TIME) {
2174 * Maybe do better on x86 and provide cpu_clock_nmi()
2176 time = sched_clock();
2178 header.type |= PERF_RECORD_TIME;
2179 header.size += sizeof(u64);
2182 if (record_type & PERF_RECORD_ADDR) {
2183 header.type |= PERF_RECORD_ADDR;
2184 header.size += sizeof(u64);
2187 if (record_type & PERF_RECORD_CONFIG) {
2188 header.type |= PERF_RECORD_CONFIG;
2189 header.size += sizeof(u64);
2192 if (record_type & PERF_RECORD_CPU) {
2193 header.type |= PERF_RECORD_CPU;
2194 header.size += sizeof(cpu_entry);
2196 cpu_entry.cpu = raw_smp_processor_id();
2199 if (record_type & PERF_RECORD_GROUP) {
2200 header.type |= PERF_RECORD_GROUP;
2201 header.size += sizeof(u64) +
2202 counter->nr_siblings * sizeof(group_entry);
2205 if (record_type & PERF_RECORD_CALLCHAIN) {
2206 callchain = perf_callchain(regs);
2209 callchain_size = (1 + callchain->nr) * sizeof(u64);
2211 header.type |= PERF_RECORD_CALLCHAIN;
2212 header.size += callchain_size;
2216 ret = perf_output_begin(&handle, counter, header.size, nmi, 1);
2220 perf_output_put(&handle, header);
2222 if (record_type & PERF_RECORD_IP)
2223 perf_output_put(&handle, ip);
2225 if (record_type & PERF_RECORD_TID)
2226 perf_output_put(&handle, tid_entry);
2228 if (record_type & PERF_RECORD_TIME)
2229 perf_output_put(&handle, time);
2231 if (record_type & PERF_RECORD_ADDR)
2232 perf_output_put(&handle, addr);
2234 if (record_type & PERF_RECORD_CONFIG)
2235 perf_output_put(&handle, counter->hw_event.config);
2237 if (record_type & PERF_RECORD_CPU)
2238 perf_output_put(&handle, cpu_entry);
2241 * XXX PERF_RECORD_GROUP vs inherited counters seems difficult.
2243 if (record_type & PERF_RECORD_GROUP) {
2244 struct perf_counter *leader, *sub;
2245 u64 nr = counter->nr_siblings;
2247 perf_output_put(&handle, nr);
2249 leader = counter->group_leader;
2250 list_for_each_entry(sub, &leader->sibling_list, list_entry) {
2252 sub->pmu->read(sub);
2254 group_entry.event = sub->hw_event.config;
2255 group_entry.counter = atomic64_read(&sub->count);
2257 perf_output_put(&handle, group_entry);
2262 perf_output_copy(&handle, callchain, callchain_size);
2264 perf_output_end(&handle);
2271 struct perf_comm_event {
2272 struct task_struct *task;
2277 struct perf_event_header header;
2284 static void perf_counter_comm_output(struct perf_counter *counter,
2285 struct perf_comm_event *comm_event)
2287 struct perf_output_handle handle;
2288 int size = comm_event->event.header.size;
2289 int ret = perf_output_begin(&handle, counter, size, 0, 0);
2294 perf_output_put(&handle, comm_event->event);
2295 perf_output_copy(&handle, comm_event->comm,
2296 comm_event->comm_size);
2297 perf_output_end(&handle);
2300 static int perf_counter_comm_match(struct perf_counter *counter,
2301 struct perf_comm_event *comm_event)
2303 if (counter->hw_event.comm &&
2304 comm_event->event.header.type == PERF_EVENT_COMM)
2310 static void perf_counter_comm_ctx(struct perf_counter_context *ctx,
2311 struct perf_comm_event *comm_event)
2313 struct perf_counter *counter;
2315 if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
2319 list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
2320 if (perf_counter_comm_match(counter, comm_event))
2321 perf_counter_comm_output(counter, comm_event);
2326 static void perf_counter_comm_event(struct perf_comm_event *comm_event)
2328 struct perf_cpu_context *cpuctx;
2330 char *comm = comm_event->task->comm;
2332 size = ALIGN(strlen(comm)+1, sizeof(u64));
2334 comm_event->comm = comm;
2335 comm_event->comm_size = size;
2337 comm_event->event.header.size = sizeof(comm_event->event) + size;
2339 cpuctx = &get_cpu_var(perf_cpu_context);
2340 perf_counter_comm_ctx(&cpuctx->ctx, comm_event);
2341 put_cpu_var(perf_cpu_context);
2343 perf_counter_comm_ctx(current->perf_counter_ctxp, comm_event);
2346 void perf_counter_comm(struct task_struct *task)
2348 struct perf_comm_event comm_event;
2350 if (!atomic_read(&nr_comm_tracking))
2352 if (!current->perf_counter_ctxp)
2355 comm_event = (struct perf_comm_event){
2358 .header = { .type = PERF_EVENT_COMM, },
2359 .pid = task->group_leader->pid,
2364 perf_counter_comm_event(&comm_event);
2371 struct perf_mmap_event {
2377 struct perf_event_header header;
2387 static void perf_counter_mmap_output(struct perf_counter *counter,
2388 struct perf_mmap_event *mmap_event)
2390 struct perf_output_handle handle;
2391 int size = mmap_event->event.header.size;
2392 int ret = perf_output_begin(&handle, counter, size, 0, 0);
2397 perf_output_put(&handle, mmap_event->event);
2398 perf_output_copy(&handle, mmap_event->file_name,
2399 mmap_event->file_size);
2400 perf_output_end(&handle);
2403 static int perf_counter_mmap_match(struct perf_counter *counter,
2404 struct perf_mmap_event *mmap_event)
2406 if (counter->hw_event.mmap &&
2407 mmap_event->event.header.type == PERF_EVENT_MMAP)
2410 if (counter->hw_event.munmap &&
2411 mmap_event->event.header.type == PERF_EVENT_MUNMAP)
2417 static void perf_counter_mmap_ctx(struct perf_counter_context *ctx,
2418 struct perf_mmap_event *mmap_event)
2420 struct perf_counter *counter;
2422 if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
2426 list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
2427 if (perf_counter_mmap_match(counter, mmap_event))
2428 perf_counter_mmap_output(counter, mmap_event);
2433 static void perf_counter_mmap_event(struct perf_mmap_event *mmap_event)
2435 struct perf_cpu_context *cpuctx;
2436 struct file *file = mmap_event->file;
2443 buf = kzalloc(PATH_MAX, GFP_KERNEL);
2445 name = strncpy(tmp, "//enomem", sizeof(tmp));
2448 name = d_path(&file->f_path, buf, PATH_MAX);
2450 name = strncpy(tmp, "//toolong", sizeof(tmp));
2454 name = strncpy(tmp, "//anon", sizeof(tmp));
2459 size = ALIGN(strlen(name)+1, sizeof(u64));
2461 mmap_event->file_name = name;
2462 mmap_event->file_size = size;
2464 mmap_event->event.header.size = sizeof(mmap_event->event) + size;
2466 cpuctx = &get_cpu_var(perf_cpu_context);
2467 perf_counter_mmap_ctx(&cpuctx->ctx, mmap_event);
2468 put_cpu_var(perf_cpu_context);
2470 perf_counter_mmap_ctx(current->perf_counter_ctxp, mmap_event);
2475 void perf_counter_mmap(unsigned long addr, unsigned long len,
2476 unsigned long pgoff, struct file *file)
2478 struct perf_mmap_event mmap_event;
2480 if (!atomic_read(&nr_mmap_tracking))
2482 if (!current->perf_counter_ctxp)
2485 mmap_event = (struct perf_mmap_event){
2488 .header = { .type = PERF_EVENT_MMAP, },
2489 .pid = current->group_leader->pid,
2490 .tid = current->pid,
2497 perf_counter_mmap_event(&mmap_event);
2500 void perf_counter_munmap(unsigned long addr, unsigned long len,
2501 unsigned long pgoff, struct file *file)
2503 struct perf_mmap_event mmap_event;
2505 if (!atomic_read(&nr_munmap_tracking))
2508 mmap_event = (struct perf_mmap_event){
2511 .header = { .type = PERF_EVENT_MUNMAP, },
2512 .pid = current->group_leader->pid,
2513 .tid = current->pid,
2520 perf_counter_mmap_event(&mmap_event);
2524 * Log irq_period changes so that analyzing tools can re-normalize the
2528 static void perf_log_period(struct perf_counter *counter, u64 period)
2530 struct perf_output_handle handle;
2534 struct perf_event_header header;
2539 .type = PERF_EVENT_PERIOD,
2541 .size = sizeof(freq_event),
2543 .time = sched_clock(),
2547 if (counter->hw.irq_period == period)
2550 ret = perf_output_begin(&handle, counter, sizeof(freq_event), 0, 0);
2554 perf_output_put(&handle, freq_event);
2555 perf_output_end(&handle);
2559 * IRQ throttle logging
2562 static void perf_log_throttle(struct perf_counter *counter, int enable)
2564 struct perf_output_handle handle;
2568 struct perf_event_header header;
2570 } throttle_event = {
2572 .type = PERF_EVENT_THROTTLE + 1,
2574 .size = sizeof(throttle_event),
2576 .time = sched_clock(),
2579 ret = perf_output_begin(&handle, counter, sizeof(throttle_event), 1, 0);
2583 perf_output_put(&handle, throttle_event);
2584 perf_output_end(&handle);
2588 * Generic counter overflow handling.
2591 int perf_counter_overflow(struct perf_counter *counter,
2592 int nmi, struct pt_regs *regs, u64 addr)
2594 int events = atomic_read(&counter->event_limit);
2595 int throttle = counter->pmu->unthrottle != NULL;
2599 counter->hw.interrupts++;
2600 } else if (counter->hw.interrupts != MAX_INTERRUPTS) {
2601 counter->hw.interrupts++;
2602 if (HZ*counter->hw.interrupts > (u64)sysctl_perf_counter_limit) {
2603 counter->hw.interrupts = MAX_INTERRUPTS;
2604 perf_log_throttle(counter, 0);
2610 * XXX event_limit might not quite work as expected on inherited
2614 counter->pending_kill = POLL_IN;
2615 if (events && atomic_dec_and_test(&counter->event_limit)) {
2617 counter->pending_kill = POLL_HUP;
2619 counter->pending_disable = 1;
2620 perf_pending_queue(&counter->pending,
2621 perf_pending_counter);
2623 perf_counter_disable(counter);
2626 perf_counter_output(counter, nmi, regs, addr);
2631 * Generic software counter infrastructure
2634 static void perf_swcounter_update(struct perf_counter *counter)
2636 struct hw_perf_counter *hwc = &counter->hw;
2641 prev = atomic64_read(&hwc->prev_count);
2642 now = atomic64_read(&hwc->count);
2643 if (atomic64_cmpxchg(&hwc->prev_count, prev, now) != prev)
2648 atomic64_add(delta, &counter->count);
2649 atomic64_sub(delta, &hwc->period_left);
2652 static void perf_swcounter_set_period(struct perf_counter *counter)
2654 struct hw_perf_counter *hwc = &counter->hw;
2655 s64 left = atomic64_read(&hwc->period_left);
2656 s64 period = hwc->irq_period;
2658 if (unlikely(left <= -period)) {
2660 atomic64_set(&hwc->period_left, left);
2663 if (unlikely(left <= 0)) {
2665 atomic64_add(period, &hwc->period_left);
2668 atomic64_set(&hwc->prev_count, -left);
2669 atomic64_set(&hwc->count, -left);
2672 static enum hrtimer_restart perf_swcounter_hrtimer(struct hrtimer *hrtimer)
2674 enum hrtimer_restart ret = HRTIMER_RESTART;
2675 struct perf_counter *counter;
2676 struct pt_regs *regs;
2679 counter = container_of(hrtimer, struct perf_counter, hw.hrtimer);
2680 counter->pmu->read(counter);
2682 regs = get_irq_regs();
2684 * In case we exclude kernel IPs or are somehow not in interrupt
2685 * context, provide the next best thing, the user IP.
2687 if ((counter->hw_event.exclude_kernel || !regs) &&
2688 !counter->hw_event.exclude_user)
2689 regs = task_pt_regs(current);
2692 if (perf_counter_overflow(counter, 0, regs, 0))
2693 ret = HRTIMER_NORESTART;
2696 period = max_t(u64, 10000, counter->hw.irq_period);
2697 hrtimer_forward_now(hrtimer, ns_to_ktime(period));
2702 static void perf_swcounter_overflow(struct perf_counter *counter,
2703 int nmi, struct pt_regs *regs, u64 addr)
2705 perf_swcounter_update(counter);
2706 perf_swcounter_set_period(counter);
2707 if (perf_counter_overflow(counter, nmi, regs, addr))
2708 /* soft-disable the counter */
2713 static int perf_swcounter_match(struct perf_counter *counter,
2714 enum perf_event_types type,
2715 u32 event, struct pt_regs *regs)
2717 if (counter->state != PERF_COUNTER_STATE_ACTIVE)
2720 if (perf_event_raw(&counter->hw_event))
2723 if (perf_event_type(&counter->hw_event) != type)
2726 if (perf_event_id(&counter->hw_event) != event)
2729 if (counter->hw_event.exclude_user && user_mode(regs))
2732 if (counter->hw_event.exclude_kernel && !user_mode(regs))
2738 static void perf_swcounter_add(struct perf_counter *counter, u64 nr,
2739 int nmi, struct pt_regs *regs, u64 addr)
2741 int neg = atomic64_add_negative(nr, &counter->hw.count);
2742 if (counter->hw.irq_period && !neg)
2743 perf_swcounter_overflow(counter, nmi, regs, addr);
2746 static void perf_swcounter_ctx_event(struct perf_counter_context *ctx,
2747 enum perf_event_types type, u32 event,
2748 u64 nr, int nmi, struct pt_regs *regs,
2751 struct perf_counter *counter;
2753 if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
2757 list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
2758 if (perf_swcounter_match(counter, type, event, regs))
2759 perf_swcounter_add(counter, nr, nmi, regs, addr);
2764 static int *perf_swcounter_recursion_context(struct perf_cpu_context *cpuctx)
2767 return &cpuctx->recursion[3];
2770 return &cpuctx->recursion[2];
2773 return &cpuctx->recursion[1];
2775 return &cpuctx->recursion[0];
2778 static void __perf_swcounter_event(enum perf_event_types type, u32 event,
2779 u64 nr, int nmi, struct pt_regs *regs,
2782 struct perf_cpu_context *cpuctx = &get_cpu_var(perf_cpu_context);
2783 int *recursion = perf_swcounter_recursion_context(cpuctx);
2791 perf_swcounter_ctx_event(&cpuctx->ctx, type, event,
2792 nr, nmi, regs, addr);
2793 if (cpuctx->task_ctx) {
2794 perf_swcounter_ctx_event(cpuctx->task_ctx, type, event,
2795 nr, nmi, regs, addr);
2802 put_cpu_var(perf_cpu_context);
2806 perf_swcounter_event(u32 event, u64 nr, int nmi, struct pt_regs *regs, u64 addr)
2808 __perf_swcounter_event(PERF_TYPE_SOFTWARE, event, nr, nmi, regs, addr);
2811 static void perf_swcounter_read(struct perf_counter *counter)
2813 perf_swcounter_update(counter);
2816 static int perf_swcounter_enable(struct perf_counter *counter)
2818 perf_swcounter_set_period(counter);
2822 static void perf_swcounter_disable(struct perf_counter *counter)
2824 perf_swcounter_update(counter);
2827 static const struct pmu perf_ops_generic = {
2828 .enable = perf_swcounter_enable,
2829 .disable = perf_swcounter_disable,
2830 .read = perf_swcounter_read,
2834 * Software counter: cpu wall time clock
2837 static void cpu_clock_perf_counter_update(struct perf_counter *counter)
2839 int cpu = raw_smp_processor_id();
2843 now = cpu_clock(cpu);
2844 prev = atomic64_read(&counter->hw.prev_count);
2845 atomic64_set(&counter->hw.prev_count, now);
2846 atomic64_add(now - prev, &counter->count);
2849 static int cpu_clock_perf_counter_enable(struct perf_counter *counter)
2851 struct hw_perf_counter *hwc = &counter->hw;
2852 int cpu = raw_smp_processor_id();
2854 atomic64_set(&hwc->prev_count, cpu_clock(cpu));
2855 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
2856 hwc->hrtimer.function = perf_swcounter_hrtimer;
2857 if (hwc->irq_period) {
2858 u64 period = max_t(u64, 10000, hwc->irq_period);
2859 __hrtimer_start_range_ns(&hwc->hrtimer,
2860 ns_to_ktime(period), 0,
2861 HRTIMER_MODE_REL, 0);
2867 static void cpu_clock_perf_counter_disable(struct perf_counter *counter)
2869 if (counter->hw.irq_period)
2870 hrtimer_cancel(&counter->hw.hrtimer);
2871 cpu_clock_perf_counter_update(counter);
2874 static void cpu_clock_perf_counter_read(struct perf_counter *counter)
2876 cpu_clock_perf_counter_update(counter);
2879 static const struct pmu perf_ops_cpu_clock = {
2880 .enable = cpu_clock_perf_counter_enable,
2881 .disable = cpu_clock_perf_counter_disable,
2882 .read = cpu_clock_perf_counter_read,
2886 * Software counter: task time clock
2889 static void task_clock_perf_counter_update(struct perf_counter *counter, u64 now)
2894 prev = atomic64_xchg(&counter->hw.prev_count, now);
2896 atomic64_add(delta, &counter->count);
2899 static int task_clock_perf_counter_enable(struct perf_counter *counter)
2901 struct hw_perf_counter *hwc = &counter->hw;
2904 now = counter->ctx->time;
2906 atomic64_set(&hwc->prev_count, now);
2907 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
2908 hwc->hrtimer.function = perf_swcounter_hrtimer;
2909 if (hwc->irq_period) {
2910 u64 period = max_t(u64, 10000, hwc->irq_period);
2911 __hrtimer_start_range_ns(&hwc->hrtimer,
2912 ns_to_ktime(period), 0,
2913 HRTIMER_MODE_REL, 0);
2919 static void task_clock_perf_counter_disable(struct perf_counter *counter)
2921 if (counter->hw.irq_period)
2922 hrtimer_cancel(&counter->hw.hrtimer);
2923 task_clock_perf_counter_update(counter, counter->ctx->time);
2927 static void task_clock_perf_counter_read(struct perf_counter *counter)
2932 update_context_time(counter->ctx);
2933 time = counter->ctx->time;
2935 u64 now = perf_clock();
2936 u64 delta = now - counter->ctx->timestamp;
2937 time = counter->ctx->time + delta;
2940 task_clock_perf_counter_update(counter, time);
2943 static const struct pmu perf_ops_task_clock = {
2944 .enable = task_clock_perf_counter_enable,
2945 .disable = task_clock_perf_counter_disable,
2946 .read = task_clock_perf_counter_read,
2950 * Software counter: cpu migrations
2953 static inline u64 get_cpu_migrations(struct perf_counter *counter)
2955 struct task_struct *curr = counter->ctx->task;
2958 return curr->se.nr_migrations;
2959 return cpu_nr_migrations(smp_processor_id());
2962 static void cpu_migrations_perf_counter_update(struct perf_counter *counter)
2967 prev = atomic64_read(&counter->hw.prev_count);
2968 now = get_cpu_migrations(counter);
2970 atomic64_set(&counter->hw.prev_count, now);
2974 atomic64_add(delta, &counter->count);
2977 static void cpu_migrations_perf_counter_read(struct perf_counter *counter)
2979 cpu_migrations_perf_counter_update(counter);
2982 static int cpu_migrations_perf_counter_enable(struct perf_counter *counter)
2984 if (counter->prev_state <= PERF_COUNTER_STATE_OFF)
2985 atomic64_set(&counter->hw.prev_count,
2986 get_cpu_migrations(counter));
2990 static void cpu_migrations_perf_counter_disable(struct perf_counter *counter)
2992 cpu_migrations_perf_counter_update(counter);
2995 static const struct pmu perf_ops_cpu_migrations = {
2996 .enable = cpu_migrations_perf_counter_enable,
2997 .disable = cpu_migrations_perf_counter_disable,
2998 .read = cpu_migrations_perf_counter_read,
3001 #ifdef CONFIG_EVENT_PROFILE
3002 void perf_tpcounter_event(int event_id)
3004 struct pt_regs *regs = get_irq_regs();
3007 regs = task_pt_regs(current);
3009 __perf_swcounter_event(PERF_TYPE_TRACEPOINT, event_id, 1, 1, regs, 0);
3011 EXPORT_SYMBOL_GPL(perf_tpcounter_event);
3013 extern int ftrace_profile_enable(int);
3014 extern void ftrace_profile_disable(int);
3016 static void tp_perf_counter_destroy(struct perf_counter *counter)
3018 ftrace_profile_disable(perf_event_id(&counter->hw_event));
3021 static const struct pmu *tp_perf_counter_init(struct perf_counter *counter)
3023 int event_id = perf_event_id(&counter->hw_event);
3026 ret = ftrace_profile_enable(event_id);
3030 counter->destroy = tp_perf_counter_destroy;
3031 counter->hw.irq_period = counter->hw_event.irq_period;
3033 return &perf_ops_generic;
3036 static const struct pmu *tp_perf_counter_init(struct perf_counter *counter)
3042 static const struct pmu *sw_perf_counter_init(struct perf_counter *counter)
3044 const struct pmu *pmu = NULL;
3047 * Software counters (currently) can't in general distinguish
3048 * between user, kernel and hypervisor events.
3049 * However, context switches and cpu migrations are considered
3050 * to be kernel events, and page faults are never hypervisor
3053 switch (perf_event_id(&counter->hw_event)) {
3054 case PERF_COUNT_CPU_CLOCK:
3055 pmu = &perf_ops_cpu_clock;
3058 case PERF_COUNT_TASK_CLOCK:
3060 * If the user instantiates this as a per-cpu counter,
3061 * use the cpu_clock counter instead.
3063 if (counter->ctx->task)
3064 pmu = &perf_ops_task_clock;
3066 pmu = &perf_ops_cpu_clock;
3069 case PERF_COUNT_PAGE_FAULTS:
3070 case PERF_COUNT_PAGE_FAULTS_MIN:
3071 case PERF_COUNT_PAGE_FAULTS_MAJ:
3072 case PERF_COUNT_CONTEXT_SWITCHES:
3073 pmu = &perf_ops_generic;
3075 case PERF_COUNT_CPU_MIGRATIONS:
3076 if (!counter->hw_event.exclude_kernel)
3077 pmu = &perf_ops_cpu_migrations;
3085 * Allocate and initialize a counter structure
3087 static struct perf_counter *
3088 perf_counter_alloc(struct perf_counter_hw_event *hw_event,
3090 struct perf_counter_context *ctx,
3091 struct perf_counter *group_leader,
3094 const struct pmu *pmu;
3095 struct perf_counter *counter;
3096 struct hw_perf_counter *hwc;
3099 counter = kzalloc(sizeof(*counter), gfpflags);
3101 return ERR_PTR(-ENOMEM);
3104 * Single counters are their own group leaders, with an
3105 * empty sibling list:
3108 group_leader = counter;
3110 mutex_init(&counter->child_mutex);
3111 INIT_LIST_HEAD(&counter->child_list);
3113 INIT_LIST_HEAD(&counter->list_entry);
3114 INIT_LIST_HEAD(&counter->event_entry);
3115 INIT_LIST_HEAD(&counter->sibling_list);
3116 init_waitqueue_head(&counter->waitq);
3118 mutex_init(&counter->mmap_mutex);
3121 counter->hw_event = *hw_event;
3122 counter->group_leader = group_leader;
3123 counter->pmu = NULL;
3127 counter->state = PERF_COUNTER_STATE_INACTIVE;
3128 if (hw_event->disabled)
3129 counter->state = PERF_COUNTER_STATE_OFF;
3134 if (hw_event->freq && hw_event->irq_freq)
3135 hwc->irq_period = div64_u64(TICK_NSEC, hw_event->irq_freq);
3137 hwc->irq_period = hw_event->irq_period;
3140 * we currently do not support PERF_RECORD_GROUP on inherited counters
3142 if (hw_event->inherit && (hw_event->record_type & PERF_RECORD_GROUP))
3145 if (perf_event_raw(hw_event)) {
3146 pmu = hw_perf_counter_init(counter);
3150 switch (perf_event_type(hw_event)) {
3151 case PERF_TYPE_HARDWARE:
3152 pmu = hw_perf_counter_init(counter);
3155 case PERF_TYPE_SOFTWARE:
3156 pmu = sw_perf_counter_init(counter);
3159 case PERF_TYPE_TRACEPOINT:
3160 pmu = tp_perf_counter_init(counter);
3167 else if (IS_ERR(pmu))
3172 return ERR_PTR(err);
3177 atomic_inc(&nr_counters);
3178 if (counter->hw_event.mmap)
3179 atomic_inc(&nr_mmap_tracking);
3180 if (counter->hw_event.munmap)
3181 atomic_inc(&nr_munmap_tracking);
3182 if (counter->hw_event.comm)
3183 atomic_inc(&nr_comm_tracking);
3189 * sys_perf_counter_open - open a performance counter, associate it to a task/cpu
3191 * @hw_event_uptr: event type attributes for monitoring/sampling
3194 * @group_fd: group leader counter fd
3196 SYSCALL_DEFINE5(perf_counter_open,
3197 const struct perf_counter_hw_event __user *, hw_event_uptr,
3198 pid_t, pid, int, cpu, int, group_fd, unsigned long, flags)
3200 struct perf_counter *counter, *group_leader;
3201 struct perf_counter_hw_event hw_event;
3202 struct perf_counter_context *ctx;
3203 struct file *counter_file = NULL;
3204 struct file *group_file = NULL;
3205 int fput_needed = 0;
3206 int fput_needed2 = 0;
3209 /* for future expandability... */
3213 if (copy_from_user(&hw_event, hw_event_uptr, sizeof(hw_event)) != 0)
3217 * Get the target context (task or percpu):
3219 ctx = find_get_context(pid, cpu);
3221 return PTR_ERR(ctx);
3224 * Look up the group leader (we will attach this counter to it):
3226 group_leader = NULL;
3227 if (group_fd != -1) {
3229 group_file = fget_light(group_fd, &fput_needed);
3231 goto err_put_context;
3232 if (group_file->f_op != &perf_fops)
3233 goto err_put_context;
3235 group_leader = group_file->private_data;
3237 * Do not allow a recursive hierarchy (this new sibling
3238 * becoming part of another group-sibling):
3240 if (group_leader->group_leader != group_leader)
3241 goto err_put_context;
3243 * Do not allow to attach to a group in a different
3244 * task or CPU context:
3246 if (group_leader->ctx != ctx)
3247 goto err_put_context;
3249 * Only a group leader can be exclusive or pinned
3251 if (hw_event.exclusive || hw_event.pinned)
3252 goto err_put_context;
3255 counter = perf_counter_alloc(&hw_event, cpu, ctx, group_leader,
3257 ret = PTR_ERR(counter);
3258 if (IS_ERR(counter))
3259 goto err_put_context;
3261 ret = anon_inode_getfd("[perf_counter]", &perf_fops, counter, 0);
3263 goto err_free_put_context;
3265 counter_file = fget_light(ret, &fput_needed2);
3267 goto err_free_put_context;
3269 counter->filp = counter_file;
3270 mutex_lock(&ctx->mutex);
3271 perf_install_in_context(ctx, counter, cpu);
3272 mutex_unlock(&ctx->mutex);
3274 counter->owner = current;
3275 get_task_struct(current);
3276 mutex_lock(¤t->perf_counter_mutex);
3277 list_add_tail(&counter->owner_entry, ¤t->perf_counter_list);
3278 mutex_unlock(¤t->perf_counter_mutex);
3280 fput_light(counter_file, fput_needed2);
3283 fput_light(group_file, fput_needed);
3287 err_free_put_context:
3297 * inherit a counter from parent task to child task:
3299 static struct perf_counter *
3300 inherit_counter(struct perf_counter *parent_counter,
3301 struct task_struct *parent,
3302 struct perf_counter_context *parent_ctx,
3303 struct task_struct *child,
3304 struct perf_counter *group_leader,
3305 struct perf_counter_context *child_ctx)
3307 struct perf_counter *child_counter;
3310 * Instead of creating recursive hierarchies of counters,
3311 * we link inherited counters back to the original parent,
3312 * which has a filp for sure, which we use as the reference
3315 if (parent_counter->parent)
3316 parent_counter = parent_counter->parent;
3318 child_counter = perf_counter_alloc(&parent_counter->hw_event,
3319 parent_counter->cpu, child_ctx,
3320 group_leader, GFP_KERNEL);
3321 if (IS_ERR(child_counter))
3322 return child_counter;
3325 * Make the child state follow the state of the parent counter,
3326 * not its hw_event.disabled bit. We hold the parent's mutex,
3327 * so we won't race with perf_counter_{en,dis}able_family.
3329 if (parent_counter->state >= PERF_COUNTER_STATE_INACTIVE)
3330 child_counter->state = PERF_COUNTER_STATE_INACTIVE;
3332 child_counter->state = PERF_COUNTER_STATE_OFF;
3335 * Link it up in the child's context:
3337 add_counter_to_ctx(child_counter, child_ctx);
3339 child_counter->parent = parent_counter;
3341 * inherit into child's child as well:
3343 child_counter->hw_event.inherit = 1;
3346 * Get a reference to the parent filp - we will fput it
3347 * when the child counter exits. This is safe to do because
3348 * we are in the parent and we know that the filp still
3349 * exists and has a nonzero count:
3351 atomic_long_inc(&parent_counter->filp->f_count);
3354 * Link this into the parent counter's child list
3356 mutex_lock(&parent_counter->child_mutex);
3357 list_add_tail(&child_counter->child_list, &parent_counter->child_list);
3358 mutex_unlock(&parent_counter->child_mutex);
3360 return child_counter;
3363 static int inherit_group(struct perf_counter *parent_counter,
3364 struct task_struct *parent,
3365 struct perf_counter_context *parent_ctx,
3366 struct task_struct *child,
3367 struct perf_counter_context *child_ctx)
3369 struct perf_counter *leader;
3370 struct perf_counter *sub;
3371 struct perf_counter *child_ctr;
3373 leader = inherit_counter(parent_counter, parent, parent_ctx,
3374 child, NULL, child_ctx);
3376 return PTR_ERR(leader);
3377 list_for_each_entry(sub, &parent_counter->sibling_list, list_entry) {
3378 child_ctr = inherit_counter(sub, parent, parent_ctx,
3379 child, leader, child_ctx);
3380 if (IS_ERR(child_ctr))
3381 return PTR_ERR(child_ctr);
3386 static void sync_child_counter(struct perf_counter *child_counter,
3387 struct perf_counter *parent_counter)
3391 child_val = atomic64_read(&child_counter->count);
3394 * Add back the child's count to the parent's count:
3396 atomic64_add(child_val, &parent_counter->count);
3397 atomic64_add(child_counter->total_time_enabled,
3398 &parent_counter->child_total_time_enabled);
3399 atomic64_add(child_counter->total_time_running,
3400 &parent_counter->child_total_time_running);
3403 * Remove this counter from the parent's list
3405 mutex_lock(&parent_counter->child_mutex);
3406 list_del_init(&child_counter->child_list);
3407 mutex_unlock(&parent_counter->child_mutex);
3410 * Release the parent counter, if this was the last
3413 fput(parent_counter->filp);
3417 __perf_counter_exit_task(struct task_struct *child,
3418 struct perf_counter *child_counter,
3419 struct perf_counter_context *child_ctx)
3421 struct perf_counter *parent_counter;
3423 update_counter_times(child_counter);
3424 perf_counter_remove_from_context(child_counter);
3426 parent_counter = child_counter->parent;
3428 * It can happen that parent exits first, and has counters
3429 * that are still around due to the child reference. These
3430 * counters need to be zapped - but otherwise linger.
3432 if (parent_counter) {
3433 sync_child_counter(child_counter, parent_counter);
3434 free_counter(child_counter);
3439 * When a child task exits, feed back counter values to parent counters.
3441 * Note: we may be running in child context, but the PID is not hashed
3442 * anymore so new counters will not be added.
3443 * (XXX not sure that is true when we get called from flush_old_exec.
3446 void perf_counter_exit_task(struct task_struct *child)
3448 struct perf_counter *child_counter, *tmp;
3449 struct perf_counter_context *child_ctx;
3450 unsigned long flags;
3452 child_ctx = child->perf_counter_ctxp;
3454 if (likely(!child_ctx))
3457 local_irq_save(flags);
3458 __perf_counter_task_sched_out(child_ctx);
3459 child->perf_counter_ctxp = NULL;
3460 local_irq_restore(flags);
3462 mutex_lock(&child_ctx->mutex);
3465 list_for_each_entry_safe(child_counter, tmp, &child_ctx->counter_list,
3467 __perf_counter_exit_task(child, child_counter, child_ctx);
3470 * If the last counter was a group counter, it will have appended all
3471 * its siblings to the list, but we obtained 'tmp' before that which
3472 * will still point to the list head terminating the iteration.
3474 if (!list_empty(&child_ctx->counter_list))
3477 mutex_unlock(&child_ctx->mutex);
3483 * Initialize the perf_counter context in task_struct
3485 int perf_counter_init_task(struct task_struct *child)
3487 struct perf_counter_context *child_ctx, *parent_ctx;
3488 struct perf_counter *counter;
3489 struct task_struct *parent = current;
3490 int inherited_all = 1;
3493 child->perf_counter_ctxp = NULL;
3495 mutex_init(&child->perf_counter_mutex);
3496 INIT_LIST_HEAD(&child->perf_counter_list);
3498 parent_ctx = parent->perf_counter_ctxp;
3499 if (likely(!parent_ctx || !parent_ctx->nr_counters))
3503 * This is executed from the parent task context, so inherit
3504 * counters that have been marked for cloning.
3505 * First allocate and initialize a context for the child.
3508 child_ctx = kmalloc(sizeof(struct perf_counter_context), GFP_KERNEL);
3512 __perf_counter_init_context(child_ctx, child);
3513 child->perf_counter_ctxp = child_ctx;
3516 * Lock the parent list. No need to lock the child - not PID
3517 * hashed yet and not running, so nobody can access it.
3519 mutex_lock(&parent_ctx->mutex);
3522 * We dont have to disable NMIs - we are only looking at
3523 * the list, not manipulating it:
3525 list_for_each_entry_rcu(counter, &parent_ctx->event_list, event_entry) {
3526 if (counter != counter->group_leader)
3529 if (!counter->hw_event.inherit) {
3534 ret = inherit_group(counter, parent, parent_ctx,
3542 if (inherited_all) {
3544 * Mark the child context as a clone of the parent
3545 * context, or of whatever the parent is a clone of.
3547 if (parent_ctx->parent_ctx) {
3548 child_ctx->parent_ctx = parent_ctx->parent_ctx;
3549 child_ctx->parent_gen = parent_ctx->parent_gen;
3551 child_ctx->parent_ctx = parent_ctx;
3552 child_ctx->parent_gen = parent_ctx->generation;
3554 get_ctx(child_ctx->parent_ctx);
3557 mutex_unlock(&parent_ctx->mutex);
3562 static void __cpuinit perf_counter_init_cpu(int cpu)
3564 struct perf_cpu_context *cpuctx;
3566 cpuctx = &per_cpu(perf_cpu_context, cpu);
3567 __perf_counter_init_context(&cpuctx->ctx, NULL);
3569 spin_lock(&perf_resource_lock);
3570 cpuctx->max_pertask = perf_max_counters - perf_reserved_percpu;
3571 spin_unlock(&perf_resource_lock);
3573 hw_perf_counter_setup(cpu);
3576 #ifdef CONFIG_HOTPLUG_CPU
3577 static void __perf_counter_exit_cpu(void *info)
3579 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
3580 struct perf_counter_context *ctx = &cpuctx->ctx;
3581 struct perf_counter *counter, *tmp;
3583 list_for_each_entry_safe(counter, tmp, &ctx->counter_list, list_entry)
3584 __perf_counter_remove_from_context(counter);
3586 static void perf_counter_exit_cpu(int cpu)
3588 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
3589 struct perf_counter_context *ctx = &cpuctx->ctx;
3591 mutex_lock(&ctx->mutex);
3592 smp_call_function_single(cpu, __perf_counter_exit_cpu, NULL, 1);
3593 mutex_unlock(&ctx->mutex);
3596 static inline void perf_counter_exit_cpu(int cpu) { }
3599 static int __cpuinit
3600 perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
3602 unsigned int cpu = (long)hcpu;
3606 case CPU_UP_PREPARE:
3607 case CPU_UP_PREPARE_FROZEN:
3608 perf_counter_init_cpu(cpu);
3611 case CPU_DOWN_PREPARE:
3612 case CPU_DOWN_PREPARE_FROZEN:
3613 perf_counter_exit_cpu(cpu);
3623 static struct notifier_block __cpuinitdata perf_cpu_nb = {
3624 .notifier_call = perf_cpu_notify,
3627 void __init perf_counter_init(void)
3629 perf_cpu_notify(&perf_cpu_nb, (unsigned long)CPU_UP_PREPARE,
3630 (void *)(long)smp_processor_id());
3631 register_cpu_notifier(&perf_cpu_nb);
3634 static ssize_t perf_show_reserve_percpu(struct sysdev_class *class, char *buf)
3636 return sprintf(buf, "%d\n", perf_reserved_percpu);
3640 perf_set_reserve_percpu(struct sysdev_class *class,
3644 struct perf_cpu_context *cpuctx;
3648 err = strict_strtoul(buf, 10, &val);
3651 if (val > perf_max_counters)
3654 spin_lock(&perf_resource_lock);
3655 perf_reserved_percpu = val;
3656 for_each_online_cpu(cpu) {
3657 cpuctx = &per_cpu(perf_cpu_context, cpu);
3658 spin_lock_irq(&cpuctx->ctx.lock);
3659 mpt = min(perf_max_counters - cpuctx->ctx.nr_counters,
3660 perf_max_counters - perf_reserved_percpu);
3661 cpuctx->max_pertask = mpt;
3662 spin_unlock_irq(&cpuctx->ctx.lock);
3664 spin_unlock(&perf_resource_lock);
3669 static ssize_t perf_show_overcommit(struct sysdev_class *class, char *buf)
3671 return sprintf(buf, "%d\n", perf_overcommit);
3675 perf_set_overcommit(struct sysdev_class *class, const char *buf, size_t count)
3680 err = strict_strtoul(buf, 10, &val);
3686 spin_lock(&perf_resource_lock);
3687 perf_overcommit = val;
3688 spin_unlock(&perf_resource_lock);
3693 static SYSDEV_CLASS_ATTR(
3696 perf_show_reserve_percpu,
3697 perf_set_reserve_percpu
3700 static SYSDEV_CLASS_ATTR(
3703 perf_show_overcommit,
3707 static struct attribute *perfclass_attrs[] = {
3708 &attr_reserve_percpu.attr,
3709 &attr_overcommit.attr,
3713 static struct attribute_group perfclass_attr_group = {
3714 .attrs = perfclass_attrs,
3715 .name = "perf_counters",
3718 static int __init perf_counter_sysfs_init(void)
3720 return sysfs_create_group(&cpu_sysdev_class.kset.kobj,
3721 &perfclass_attr_group);
3723 device_initcall(perf_counter_sysfs_init);