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 */
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 void __weak hw_perf_disable(void) { barrier(); }
64 void __weak hw_perf_enable(void) { barrier(); }
66 void __weak hw_perf_counter_setup(int cpu) { barrier(); }
67 int __weak hw_perf_group_sched_in(struct perf_counter *group_leader,
68 struct perf_cpu_context *cpuctx,
69 struct perf_counter_context *ctx, int cpu)
74 void __weak perf_counter_print_debug(void) { }
76 static DEFINE_PER_CPU(int, disable_count);
78 void __perf_disable(void)
80 __get_cpu_var(disable_count)++;
83 bool __perf_enable(void)
85 return !--__get_cpu_var(disable_count);
88 void perf_disable(void)
94 void perf_enable(void)
100 static void get_ctx(struct perf_counter_context *ctx)
102 atomic_inc(&ctx->refcount);
105 static void put_ctx(struct perf_counter_context *ctx)
107 if (atomic_dec_and_test(&ctx->refcount)) {
109 put_ctx(ctx->parent_ctx);
115 * Add a counter from the lists for its context.
116 * Must be called with ctx->mutex and ctx->lock held.
119 list_add_counter(struct perf_counter *counter, struct perf_counter_context *ctx)
121 struct perf_counter *group_leader = counter->group_leader;
124 * Depending on whether it is a standalone or sibling counter,
125 * add it straight to the context's counter list, or to the group
126 * leader's sibling list:
128 if (group_leader == counter)
129 list_add_tail(&counter->list_entry, &ctx->counter_list);
131 list_add_tail(&counter->list_entry, &group_leader->sibling_list);
132 group_leader->nr_siblings++;
135 list_add_rcu(&counter->event_entry, &ctx->event_list);
137 if (counter->state >= PERF_COUNTER_STATE_INACTIVE)
142 * Remove a counter from the lists for its context.
143 * Must be called with ctx->mutex and ctx->lock held.
146 list_del_counter(struct perf_counter *counter, struct perf_counter_context *ctx)
148 struct perf_counter *sibling, *tmp;
150 if (list_empty(&counter->list_entry))
153 if (counter->state >= PERF_COUNTER_STATE_INACTIVE)
156 list_del_init(&counter->list_entry);
157 list_del_rcu(&counter->event_entry);
159 if (counter->group_leader != counter)
160 counter->group_leader->nr_siblings--;
163 * If this was a group counter with sibling counters then
164 * upgrade the siblings to singleton counters by adding them
165 * to the context list directly:
167 list_for_each_entry_safe(sibling, tmp,
168 &counter->sibling_list, list_entry) {
170 list_move_tail(&sibling->list_entry, &ctx->counter_list);
171 sibling->group_leader = sibling;
176 counter_sched_out(struct perf_counter *counter,
177 struct perf_cpu_context *cpuctx,
178 struct perf_counter_context *ctx)
180 if (counter->state != PERF_COUNTER_STATE_ACTIVE)
183 counter->state = PERF_COUNTER_STATE_INACTIVE;
184 counter->tstamp_stopped = ctx->time;
185 counter->pmu->disable(counter);
188 if (!is_software_counter(counter))
189 cpuctx->active_oncpu--;
191 if (counter->hw_event.exclusive || !cpuctx->active_oncpu)
192 cpuctx->exclusive = 0;
196 group_sched_out(struct perf_counter *group_counter,
197 struct perf_cpu_context *cpuctx,
198 struct perf_counter_context *ctx)
200 struct perf_counter *counter;
202 if (group_counter->state != PERF_COUNTER_STATE_ACTIVE)
205 counter_sched_out(group_counter, cpuctx, ctx);
208 * Schedule out siblings (if any):
210 list_for_each_entry(counter, &group_counter->sibling_list, list_entry)
211 counter_sched_out(counter, cpuctx, ctx);
213 if (group_counter->hw_event.exclusive)
214 cpuctx->exclusive = 0;
218 * Mark this context as not being a clone of another.
219 * Called when counters are added to or removed from this context.
220 * We also increment our generation number so that anything that
221 * was cloned from this context before this will not match anything
222 * cloned from this context after this.
224 static void unclone_ctx(struct perf_counter_context *ctx)
227 if (!ctx->parent_ctx)
229 put_ctx(ctx->parent_ctx);
230 ctx->parent_ctx = NULL;
234 * Cross CPU call to remove a performance counter
236 * We disable the counter on the hardware level first. After that we
237 * remove it from the context list.
239 static void __perf_counter_remove_from_context(void *info)
241 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
242 struct perf_counter *counter = info;
243 struct perf_counter_context *ctx = counter->ctx;
247 * If this is a task context, we need to check whether it is
248 * the current task context of this cpu. If not it has been
249 * scheduled out before the smp call arrived.
251 if (ctx->task && cpuctx->task_ctx != ctx)
254 spin_lock_irqsave(&ctx->lock, flags);
256 * Protect the list operation against NMI by disabling the
257 * counters on a global level.
261 counter_sched_out(counter, cpuctx, ctx);
263 list_del_counter(counter, ctx);
267 * Allow more per task counters with respect to the
270 cpuctx->max_pertask =
271 min(perf_max_counters - ctx->nr_counters,
272 perf_max_counters - perf_reserved_percpu);
276 spin_unlock_irqrestore(&ctx->lock, flags);
281 * Remove the counter from a task's (or a CPU's) list of counters.
283 * Must be called with ctx->mutex held.
285 * CPU counters are removed with a smp call. For task counters we only
286 * call when the task is on a CPU.
288 static void perf_counter_remove_from_context(struct perf_counter *counter)
290 struct perf_counter_context *ctx = counter->ctx;
291 struct task_struct *task = ctx->task;
296 * Per cpu counters are removed via an smp call and
297 * the removal is always sucessful.
299 smp_call_function_single(counter->cpu,
300 __perf_counter_remove_from_context,
306 task_oncpu_function_call(task, __perf_counter_remove_from_context,
309 spin_lock_irq(&ctx->lock);
311 * If the context is active we need to retry the smp call.
313 if (ctx->nr_active && !list_empty(&counter->list_entry)) {
314 spin_unlock_irq(&ctx->lock);
319 * The lock prevents that this context is scheduled in so we
320 * can remove the counter safely, if the call above did not
323 if (!list_empty(&counter->list_entry)) {
324 list_del_counter(counter, ctx);
326 spin_unlock_irq(&ctx->lock);
329 static inline u64 perf_clock(void)
331 return cpu_clock(smp_processor_id());
335 * Update the record of the current time in a context.
337 static void update_context_time(struct perf_counter_context *ctx)
339 u64 now = perf_clock();
341 ctx->time += now - ctx->timestamp;
342 ctx->timestamp = now;
346 * Update the total_time_enabled and total_time_running fields for a counter.
348 static void update_counter_times(struct perf_counter *counter)
350 struct perf_counter_context *ctx = counter->ctx;
353 if (counter->state < PERF_COUNTER_STATE_INACTIVE)
356 counter->total_time_enabled = ctx->time - counter->tstamp_enabled;
358 if (counter->state == PERF_COUNTER_STATE_INACTIVE)
359 run_end = counter->tstamp_stopped;
363 counter->total_time_running = run_end - counter->tstamp_running;
367 * Update total_time_enabled and total_time_running for all counters in a group.
369 static void update_group_times(struct perf_counter *leader)
371 struct perf_counter *counter;
373 update_counter_times(leader);
374 list_for_each_entry(counter, &leader->sibling_list, list_entry)
375 update_counter_times(counter);
379 * Cross CPU call to disable a performance counter
381 static void __perf_counter_disable(void *info)
383 struct perf_counter *counter = info;
384 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
385 struct perf_counter_context *ctx = counter->ctx;
389 * If this is a per-task counter, need to check whether this
390 * counter's task is the current task on this cpu.
392 if (ctx->task && cpuctx->task_ctx != ctx)
395 spin_lock_irqsave(&ctx->lock, flags);
398 * If the counter is on, turn it off.
399 * If it is in error state, leave it in error state.
401 if (counter->state >= PERF_COUNTER_STATE_INACTIVE) {
402 update_context_time(ctx);
403 update_counter_times(counter);
404 if (counter == counter->group_leader)
405 group_sched_out(counter, cpuctx, ctx);
407 counter_sched_out(counter, cpuctx, ctx);
408 counter->state = PERF_COUNTER_STATE_OFF;
412 spin_unlock_irqrestore(&ctx->lock, flags);
418 static void perf_counter_disable(struct perf_counter *counter)
420 struct perf_counter_context *ctx = counter->ctx;
421 struct task_struct *task = ctx->task;
425 * Disable the counter on the cpu that it's on
427 smp_call_function_single(counter->cpu, __perf_counter_disable,
433 task_oncpu_function_call(task, __perf_counter_disable, counter);
435 spin_lock_irq(&ctx->lock);
437 * If the counter is still active, we need to retry the cross-call.
439 if (counter->state == PERF_COUNTER_STATE_ACTIVE) {
440 spin_unlock_irq(&ctx->lock);
445 * Since we have the lock this context can't be scheduled
446 * in, so we can change the state safely.
448 if (counter->state == PERF_COUNTER_STATE_INACTIVE) {
449 update_counter_times(counter);
450 counter->state = PERF_COUNTER_STATE_OFF;
454 spin_unlock_irq(&ctx->lock);
458 counter_sched_in(struct perf_counter *counter,
459 struct perf_cpu_context *cpuctx,
460 struct perf_counter_context *ctx,
463 if (counter->state <= PERF_COUNTER_STATE_OFF)
466 counter->state = PERF_COUNTER_STATE_ACTIVE;
467 counter->oncpu = cpu; /* TODO: put 'cpu' into cpuctx->cpu */
469 * The new state must be visible before we turn it on in the hardware:
473 if (counter->pmu->enable(counter)) {
474 counter->state = PERF_COUNTER_STATE_INACTIVE;
479 counter->tstamp_running += ctx->time - counter->tstamp_stopped;
481 if (!is_software_counter(counter))
482 cpuctx->active_oncpu++;
485 if (counter->hw_event.exclusive)
486 cpuctx->exclusive = 1;
492 group_sched_in(struct perf_counter *group_counter,
493 struct perf_cpu_context *cpuctx,
494 struct perf_counter_context *ctx,
497 struct perf_counter *counter, *partial_group;
500 if (group_counter->state == PERF_COUNTER_STATE_OFF)
503 ret = hw_perf_group_sched_in(group_counter, cpuctx, ctx, cpu);
505 return ret < 0 ? ret : 0;
507 group_counter->prev_state = group_counter->state;
508 if (counter_sched_in(group_counter, cpuctx, ctx, cpu))
512 * Schedule in siblings as one group (if any):
514 list_for_each_entry(counter, &group_counter->sibling_list, list_entry) {
515 counter->prev_state = counter->state;
516 if (counter_sched_in(counter, cpuctx, ctx, cpu)) {
517 partial_group = counter;
526 * Groups can be scheduled in as one unit only, so undo any
527 * partial group before returning:
529 list_for_each_entry(counter, &group_counter->sibling_list, list_entry) {
530 if (counter == partial_group)
532 counter_sched_out(counter, cpuctx, ctx);
534 counter_sched_out(group_counter, cpuctx, ctx);
540 * Return 1 for a group consisting entirely of software counters,
541 * 0 if the group contains any hardware counters.
543 static int is_software_only_group(struct perf_counter *leader)
545 struct perf_counter *counter;
547 if (!is_software_counter(leader))
550 list_for_each_entry(counter, &leader->sibling_list, list_entry)
551 if (!is_software_counter(counter))
558 * Work out whether we can put this counter group on the CPU now.
560 static int group_can_go_on(struct perf_counter *counter,
561 struct perf_cpu_context *cpuctx,
565 * Groups consisting entirely of software counters can always go on.
567 if (is_software_only_group(counter))
570 * If an exclusive group is already on, no other hardware
571 * counters can go on.
573 if (cpuctx->exclusive)
576 * If this group is exclusive and there are already
577 * counters on the CPU, it can't go on.
579 if (counter->hw_event.exclusive && cpuctx->active_oncpu)
582 * Otherwise, try to add it if all previous groups were able
588 static void add_counter_to_ctx(struct perf_counter *counter,
589 struct perf_counter_context *ctx)
591 list_add_counter(counter, ctx);
592 counter->prev_state = PERF_COUNTER_STATE_OFF;
593 counter->tstamp_enabled = ctx->time;
594 counter->tstamp_running = ctx->time;
595 counter->tstamp_stopped = ctx->time;
599 * Cross CPU call to install and enable a performance counter
601 static void __perf_install_in_context(void *info)
603 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
604 struct perf_counter *counter = info;
605 struct perf_counter_context *ctx = counter->ctx;
606 struct perf_counter *leader = counter->group_leader;
607 int cpu = smp_processor_id();
612 * If this is a task context, we need to check whether it is
613 * the current task context of this cpu. If not it has been
614 * scheduled out before the smp call arrived.
615 * Or possibly this is the right context but it isn't
616 * on this cpu because it had no counters.
618 if (ctx->task && cpuctx->task_ctx != ctx) {
619 if (cpuctx->task_ctx || ctx->task != current)
621 cpuctx->task_ctx = ctx;
624 spin_lock_irqsave(&ctx->lock, flags);
626 update_context_time(ctx);
629 * Protect the list operation against NMI by disabling the
630 * counters on a global level. NOP for non NMI based counters.
634 add_counter_to_ctx(counter, ctx);
637 * Don't put the counter on if it is disabled or if
638 * it is in a group and the group isn't on.
640 if (counter->state != PERF_COUNTER_STATE_INACTIVE ||
641 (leader != counter && leader->state != PERF_COUNTER_STATE_ACTIVE))
645 * An exclusive counter can't go on if there are already active
646 * hardware counters, and no hardware counter can go on if there
647 * is already an exclusive counter on.
649 if (!group_can_go_on(counter, cpuctx, 1))
652 err = counter_sched_in(counter, cpuctx, ctx, cpu);
656 * This counter couldn't go on. If it is in a group
657 * then we have to pull the whole group off.
658 * If the counter group is pinned then put it in error state.
660 if (leader != counter)
661 group_sched_out(leader, cpuctx, ctx);
662 if (leader->hw_event.pinned) {
663 update_group_times(leader);
664 leader->state = PERF_COUNTER_STATE_ERROR;
668 if (!err && !ctx->task && cpuctx->max_pertask)
669 cpuctx->max_pertask--;
674 spin_unlock_irqrestore(&ctx->lock, flags);
678 * Attach a performance counter to a context
680 * First we add the counter to the list with the hardware enable bit
681 * in counter->hw_config cleared.
683 * If the counter is attached to a task which is on a CPU we use a smp
684 * call to enable it in the task context. The task might have been
685 * scheduled away, but we check this in the smp call again.
687 * Must be called with ctx->mutex held.
690 perf_install_in_context(struct perf_counter_context *ctx,
691 struct perf_counter *counter,
694 struct task_struct *task = ctx->task;
698 * Per cpu counters are installed via an smp call and
699 * the install is always sucessful.
701 smp_call_function_single(cpu, __perf_install_in_context,
707 task_oncpu_function_call(task, __perf_install_in_context,
710 spin_lock_irq(&ctx->lock);
712 * we need to retry the smp call.
714 if (ctx->is_active && list_empty(&counter->list_entry)) {
715 spin_unlock_irq(&ctx->lock);
720 * The lock prevents that this context is scheduled in so we
721 * can add the counter safely, if it the call above did not
724 if (list_empty(&counter->list_entry))
725 add_counter_to_ctx(counter, ctx);
726 spin_unlock_irq(&ctx->lock);
730 * Cross CPU call to enable a performance counter
732 static void __perf_counter_enable(void *info)
734 struct perf_counter *counter = info;
735 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
736 struct perf_counter_context *ctx = counter->ctx;
737 struct perf_counter *leader = counter->group_leader;
742 * If this is a per-task counter, need to check whether this
743 * counter's task is the current task on this cpu.
745 if (ctx->task && cpuctx->task_ctx != ctx) {
746 if (cpuctx->task_ctx || ctx->task != current)
748 cpuctx->task_ctx = ctx;
751 spin_lock_irqsave(&ctx->lock, flags);
753 update_context_time(ctx);
755 counter->prev_state = counter->state;
756 if (counter->state >= PERF_COUNTER_STATE_INACTIVE)
758 counter->state = PERF_COUNTER_STATE_INACTIVE;
759 counter->tstamp_enabled = ctx->time - counter->total_time_enabled;
763 * If the counter is in a group and isn't the group leader,
764 * then don't put it on unless the group is on.
766 if (leader != counter && leader->state != PERF_COUNTER_STATE_ACTIVE)
769 if (!group_can_go_on(counter, cpuctx, 1)) {
773 if (counter == leader)
774 err = group_sched_in(counter, cpuctx, ctx,
777 err = counter_sched_in(counter, cpuctx, ctx,
784 * If this counter can't go on and it's part of a
785 * group, then the whole group has to come off.
787 if (leader != counter)
788 group_sched_out(leader, cpuctx, ctx);
789 if (leader->hw_event.pinned) {
790 update_group_times(leader);
791 leader->state = PERF_COUNTER_STATE_ERROR;
796 spin_unlock_irqrestore(&ctx->lock, flags);
802 static void perf_counter_enable(struct perf_counter *counter)
804 struct perf_counter_context *ctx = counter->ctx;
805 struct task_struct *task = ctx->task;
809 * Enable the counter on the cpu that it's on
811 smp_call_function_single(counter->cpu, __perf_counter_enable,
816 spin_lock_irq(&ctx->lock);
817 if (counter->state >= PERF_COUNTER_STATE_INACTIVE)
821 * If the counter is in error state, clear that first.
822 * That way, if we see the counter in error state below, we
823 * know that it has gone back into error state, as distinct
824 * from the task having been scheduled away before the
825 * cross-call arrived.
827 if (counter->state == PERF_COUNTER_STATE_ERROR)
828 counter->state = PERF_COUNTER_STATE_OFF;
831 spin_unlock_irq(&ctx->lock);
832 task_oncpu_function_call(task, __perf_counter_enable, counter);
834 spin_lock_irq(&ctx->lock);
837 * If the context is active and the counter is still off,
838 * we need to retry the cross-call.
840 if (ctx->is_active && counter->state == PERF_COUNTER_STATE_OFF)
844 * Since we have the lock this context can't be scheduled
845 * in, so we can change the state safely.
847 if (counter->state == PERF_COUNTER_STATE_OFF) {
848 counter->state = PERF_COUNTER_STATE_INACTIVE;
849 counter->tstamp_enabled =
850 ctx->time - counter->total_time_enabled;
854 spin_unlock_irq(&ctx->lock);
857 static int perf_counter_refresh(struct perf_counter *counter, int refresh)
860 * not supported on inherited counters
862 if (counter->hw_event.inherit)
865 atomic_add(refresh, &counter->event_limit);
866 perf_counter_enable(counter);
871 void __perf_counter_sched_out(struct perf_counter_context *ctx,
872 struct perf_cpu_context *cpuctx)
874 struct perf_counter *counter;
876 spin_lock(&ctx->lock);
878 if (likely(!ctx->nr_counters))
880 update_context_time(ctx);
883 if (ctx->nr_active) {
884 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
885 if (counter != counter->group_leader)
886 counter_sched_out(counter, cpuctx, ctx);
888 group_sched_out(counter, cpuctx, ctx);
893 spin_unlock(&ctx->lock);
897 * Test whether two contexts are equivalent, i.e. whether they
898 * have both been cloned from the same version of the same context
899 * and they both have the same number of enabled counters.
900 * If the number of enabled counters is the same, then the set
901 * of enabled counters should be the same, because these are both
902 * inherited contexts, therefore we can't access individual counters
903 * in them directly with an fd; we can only enable/disable all
904 * counters via prctl, or enable/disable all counters in a family
905 * via ioctl, which will have the same effect on both contexts.
907 static int context_equiv(struct perf_counter_context *ctx1,
908 struct perf_counter_context *ctx2)
910 return ctx1->parent_ctx && ctx1->parent_ctx == ctx2->parent_ctx
911 && ctx1->parent_gen == ctx2->parent_gen
912 && ctx1->nr_enabled == ctx2->nr_enabled;
916 * Called from scheduler to remove the counters of the current task,
917 * with interrupts disabled.
919 * We stop each counter and update the counter value in counter->count.
921 * This does not protect us against NMI, but disable()
922 * sets the disabled bit in the control field of counter _before_
923 * accessing the counter control register. If a NMI hits, then it will
924 * not restart the counter.
926 void perf_counter_task_sched_out(struct task_struct *task,
927 struct task_struct *next, int cpu)
929 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
930 struct perf_counter_context *ctx = task->perf_counter_ctxp;
931 struct perf_counter_context *next_ctx;
932 struct pt_regs *regs;
934 if (likely(!ctx || !cpuctx->task_ctx))
937 update_context_time(ctx);
939 regs = task_pt_regs(task);
940 perf_swcounter_event(PERF_COUNT_CONTEXT_SWITCHES, 1, 1, regs, 0);
942 next_ctx = next->perf_counter_ctxp;
943 if (next_ctx && context_equiv(ctx, next_ctx)) {
944 task->perf_counter_ctxp = next_ctx;
945 next->perf_counter_ctxp = ctx;
947 next_ctx->task = task;
951 __perf_counter_sched_out(ctx, cpuctx);
953 cpuctx->task_ctx = NULL;
956 static void __perf_counter_task_sched_out(struct perf_counter_context *ctx)
958 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
960 if (!cpuctx->task_ctx)
962 __perf_counter_sched_out(ctx, cpuctx);
963 cpuctx->task_ctx = NULL;
966 static void perf_counter_cpu_sched_out(struct perf_cpu_context *cpuctx)
968 __perf_counter_sched_out(&cpuctx->ctx, cpuctx);
972 __perf_counter_sched_in(struct perf_counter_context *ctx,
973 struct perf_cpu_context *cpuctx, int cpu)
975 struct perf_counter *counter;
978 spin_lock(&ctx->lock);
980 if (likely(!ctx->nr_counters))
983 ctx->timestamp = perf_clock();
988 * First go through the list and put on any pinned groups
989 * in order to give them the best chance of going on.
991 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
992 if (counter->state <= PERF_COUNTER_STATE_OFF ||
993 !counter->hw_event.pinned)
995 if (counter->cpu != -1 && counter->cpu != cpu)
998 if (counter != counter->group_leader)
999 counter_sched_in(counter, cpuctx, ctx, cpu);
1001 if (group_can_go_on(counter, cpuctx, 1))
1002 group_sched_in(counter, cpuctx, ctx, cpu);
1006 * If this pinned group hasn't been scheduled,
1007 * put it in error state.
1009 if (counter->state == PERF_COUNTER_STATE_INACTIVE) {
1010 update_group_times(counter);
1011 counter->state = PERF_COUNTER_STATE_ERROR;
1015 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
1017 * Ignore counters in OFF or ERROR state, and
1018 * ignore pinned counters since we did them already.
1020 if (counter->state <= PERF_COUNTER_STATE_OFF ||
1021 counter->hw_event.pinned)
1025 * Listen to the 'cpu' scheduling filter constraint
1028 if (counter->cpu != -1 && counter->cpu != cpu)
1031 if (counter != counter->group_leader) {
1032 if (counter_sched_in(counter, cpuctx, ctx, cpu))
1035 if (group_can_go_on(counter, cpuctx, can_add_hw)) {
1036 if (group_sched_in(counter, cpuctx, ctx, cpu))
1043 spin_unlock(&ctx->lock);
1047 * Called from scheduler to add the counters of the current task
1048 * with interrupts disabled.
1050 * We restore the counter value and then enable it.
1052 * This does not protect us against NMI, but enable()
1053 * sets the enabled bit in the control field of counter _before_
1054 * accessing the counter control register. If a NMI hits, then it will
1055 * keep the counter running.
1057 void perf_counter_task_sched_in(struct task_struct *task, int cpu)
1059 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
1060 struct perf_counter_context *ctx = task->perf_counter_ctxp;
1064 if (cpuctx->task_ctx == ctx)
1066 __perf_counter_sched_in(ctx, cpuctx, cpu);
1067 cpuctx->task_ctx = ctx;
1070 static void perf_counter_cpu_sched_in(struct perf_cpu_context *cpuctx, int cpu)
1072 struct perf_counter_context *ctx = &cpuctx->ctx;
1074 __perf_counter_sched_in(ctx, cpuctx, cpu);
1077 int perf_counter_task_disable(void)
1079 struct task_struct *curr = current;
1080 struct perf_counter_context *ctx = curr->perf_counter_ctxp;
1081 struct perf_counter *counter;
1082 unsigned long flags;
1084 if (!ctx || !ctx->nr_counters)
1087 local_irq_save(flags);
1089 __perf_counter_task_sched_out(ctx);
1091 spin_lock(&ctx->lock);
1094 * Disable all the counters:
1098 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
1099 if (counter->state != PERF_COUNTER_STATE_ERROR) {
1100 update_group_times(counter);
1101 counter->state = PERF_COUNTER_STATE_OFF;
1107 spin_unlock_irqrestore(&ctx->lock, flags);
1112 int perf_counter_task_enable(void)
1114 struct task_struct *curr = current;
1115 struct perf_counter_context *ctx = curr->perf_counter_ctxp;
1116 struct perf_counter *counter;
1117 unsigned long flags;
1120 if (!ctx || !ctx->nr_counters)
1123 local_irq_save(flags);
1124 cpu = smp_processor_id();
1126 __perf_counter_task_sched_out(ctx);
1128 spin_lock(&ctx->lock);
1131 * Disable all the counters:
1135 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
1136 if (counter->state > PERF_COUNTER_STATE_OFF)
1138 counter->state = PERF_COUNTER_STATE_INACTIVE;
1139 counter->tstamp_enabled =
1140 ctx->time - counter->total_time_enabled;
1141 counter->hw_event.disabled = 0;
1145 spin_unlock(&ctx->lock);
1147 perf_counter_task_sched_in(curr, cpu);
1149 local_irq_restore(flags);
1154 static void perf_log_period(struct perf_counter *counter, u64 period);
1156 static void perf_adjust_freq(struct perf_counter_context *ctx)
1158 struct perf_counter *counter;
1163 spin_lock(&ctx->lock);
1164 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
1165 if (counter->state != PERF_COUNTER_STATE_ACTIVE)
1168 if (!counter->hw_event.freq || !counter->hw_event.irq_freq)
1171 events = HZ * counter->hw.interrupts * counter->hw.irq_period;
1172 period = div64_u64(events, counter->hw_event.irq_freq);
1174 delta = (s64)(1 + period - counter->hw.irq_period);
1177 irq_period = counter->hw.irq_period + delta;
1182 perf_log_period(counter, irq_period);
1184 counter->hw.irq_period = irq_period;
1185 counter->hw.interrupts = 0;
1187 spin_unlock(&ctx->lock);
1191 * Round-robin a context's counters:
1193 static void rotate_ctx(struct perf_counter_context *ctx)
1195 struct perf_counter *counter;
1197 if (!ctx->nr_counters)
1200 spin_lock(&ctx->lock);
1202 * Rotate the first entry last (works just fine for group counters too):
1205 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
1206 list_move_tail(&counter->list_entry, &ctx->counter_list);
1211 spin_unlock(&ctx->lock);
1214 void perf_counter_task_tick(struct task_struct *curr, int cpu)
1216 struct perf_cpu_context *cpuctx;
1217 struct perf_counter_context *ctx;
1219 if (!atomic_read(&nr_counters))
1222 cpuctx = &per_cpu(perf_cpu_context, cpu);
1223 ctx = curr->perf_counter_ctxp;
1225 perf_adjust_freq(&cpuctx->ctx);
1227 perf_adjust_freq(ctx);
1229 perf_counter_cpu_sched_out(cpuctx);
1231 __perf_counter_task_sched_out(ctx);
1233 rotate_ctx(&cpuctx->ctx);
1237 perf_counter_cpu_sched_in(cpuctx, cpu);
1239 perf_counter_task_sched_in(curr, cpu);
1243 * Cross CPU call to read the hardware counter
1245 static void __read(void *info)
1247 struct perf_counter *counter = info;
1248 struct perf_counter_context *ctx = counter->ctx;
1249 unsigned long flags;
1251 local_irq_save(flags);
1253 update_context_time(ctx);
1254 counter->pmu->read(counter);
1255 update_counter_times(counter);
1256 local_irq_restore(flags);
1259 static u64 perf_counter_read(struct perf_counter *counter)
1262 * If counter is enabled and currently active on a CPU, update the
1263 * value in the counter structure:
1265 if (counter->state == PERF_COUNTER_STATE_ACTIVE) {
1266 smp_call_function_single(counter->oncpu,
1267 __read, counter, 1);
1268 } else if (counter->state == PERF_COUNTER_STATE_INACTIVE) {
1269 update_counter_times(counter);
1272 return atomic64_read(&counter->count);
1276 * Initialize the perf_counter context in a task_struct:
1279 __perf_counter_init_context(struct perf_counter_context *ctx,
1280 struct task_struct *task)
1282 memset(ctx, 0, sizeof(*ctx));
1283 spin_lock_init(&ctx->lock);
1284 mutex_init(&ctx->mutex);
1285 INIT_LIST_HEAD(&ctx->counter_list);
1286 INIT_LIST_HEAD(&ctx->event_list);
1287 atomic_set(&ctx->refcount, 1);
1291 static void put_context(struct perf_counter_context *ctx)
1294 put_task_struct(ctx->task);
1297 static struct perf_counter_context *find_get_context(pid_t pid, int cpu)
1299 struct perf_cpu_context *cpuctx;
1300 struct perf_counter_context *ctx;
1301 struct perf_counter_context *tctx;
1302 struct task_struct *task;
1305 * If cpu is not a wildcard then this is a percpu counter:
1308 /* Must be root to operate on a CPU counter: */
1309 if (sysctl_perf_counter_priv && !capable(CAP_SYS_ADMIN))
1310 return ERR_PTR(-EACCES);
1312 if (cpu < 0 || cpu > num_possible_cpus())
1313 return ERR_PTR(-EINVAL);
1316 * We could be clever and allow to attach a counter to an
1317 * offline CPU and activate it when the CPU comes up, but
1320 if (!cpu_isset(cpu, cpu_online_map))
1321 return ERR_PTR(-ENODEV);
1323 cpuctx = &per_cpu(perf_cpu_context, cpu);
1333 task = find_task_by_vpid(pid);
1335 get_task_struct(task);
1339 return ERR_PTR(-ESRCH);
1341 /* Reuse ptrace permission checks for now. */
1342 if (!ptrace_may_access(task, PTRACE_MODE_READ)) {
1343 put_task_struct(task);
1344 return ERR_PTR(-EACCES);
1347 ctx = task->perf_counter_ctxp;
1349 ctx = kmalloc(sizeof(struct perf_counter_context), GFP_KERNEL);
1351 put_task_struct(task);
1352 return ERR_PTR(-ENOMEM);
1354 __perf_counter_init_context(ctx, task);
1356 * Make sure other cpus see correct values for *ctx
1357 * once task->perf_counter_ctxp is visible to them.
1360 tctx = cmpxchg(&task->perf_counter_ctxp, NULL, ctx);
1363 * We raced with some other task; use
1364 * the context they set.
1374 static void free_counter_rcu(struct rcu_head *head)
1376 struct perf_counter *counter;
1378 counter = container_of(head, struct perf_counter, rcu_head);
1379 put_ctx(counter->ctx);
1383 static void perf_pending_sync(struct perf_counter *counter);
1385 static void free_counter(struct perf_counter *counter)
1387 perf_pending_sync(counter);
1389 atomic_dec(&nr_counters);
1390 if (counter->hw_event.mmap)
1391 atomic_dec(&nr_mmap_tracking);
1392 if (counter->hw_event.munmap)
1393 atomic_dec(&nr_munmap_tracking);
1394 if (counter->hw_event.comm)
1395 atomic_dec(&nr_comm_tracking);
1397 if (counter->destroy)
1398 counter->destroy(counter);
1400 call_rcu(&counter->rcu_head, free_counter_rcu);
1404 * Called when the last reference to the file is gone.
1406 static int perf_release(struct inode *inode, struct file *file)
1408 struct perf_counter *counter = file->private_data;
1409 struct perf_counter_context *ctx = counter->ctx;
1411 file->private_data = NULL;
1413 mutex_lock(&ctx->mutex);
1414 perf_counter_remove_from_context(counter);
1415 mutex_unlock(&ctx->mutex);
1417 free_counter(counter);
1424 * Read the performance counter - simple non blocking version for now
1427 perf_read_hw(struct perf_counter *counter, char __user *buf, size_t count)
1433 * Return end-of-file for a read on a counter that is in
1434 * error state (i.e. because it was pinned but it couldn't be
1435 * scheduled on to the CPU at some point).
1437 if (counter->state == PERF_COUNTER_STATE_ERROR)
1440 mutex_lock(&counter->child_mutex);
1441 values[0] = perf_counter_read(counter);
1443 if (counter->hw_event.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
1444 values[n++] = counter->total_time_enabled +
1445 atomic64_read(&counter->child_total_time_enabled);
1446 if (counter->hw_event.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
1447 values[n++] = counter->total_time_running +
1448 atomic64_read(&counter->child_total_time_running);
1449 mutex_unlock(&counter->child_mutex);
1451 if (count < n * sizeof(u64))
1453 count = n * sizeof(u64);
1455 if (copy_to_user(buf, values, count))
1462 perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
1464 struct perf_counter *counter = file->private_data;
1466 return perf_read_hw(counter, buf, count);
1469 static unsigned int perf_poll(struct file *file, poll_table *wait)
1471 struct perf_counter *counter = file->private_data;
1472 struct perf_mmap_data *data;
1473 unsigned int events = POLL_HUP;
1476 data = rcu_dereference(counter->data);
1478 events = atomic_xchg(&data->poll, 0);
1481 poll_wait(file, &counter->waitq, wait);
1486 static void perf_counter_reset(struct perf_counter *counter)
1488 (void)perf_counter_read(counter);
1489 atomic64_set(&counter->count, 0);
1490 perf_counter_update_userpage(counter);
1493 static void perf_counter_for_each_sibling(struct perf_counter *counter,
1494 void (*func)(struct perf_counter *))
1496 struct perf_counter_context *ctx = counter->ctx;
1497 struct perf_counter *sibling;
1499 spin_lock_irq(&ctx->lock);
1500 counter = counter->group_leader;
1503 list_for_each_entry(sibling, &counter->sibling_list, list_entry)
1505 spin_unlock_irq(&ctx->lock);
1508 static void perf_counter_for_each_child(struct perf_counter *counter,
1509 void (*func)(struct perf_counter *))
1511 struct perf_counter *child;
1513 mutex_lock(&counter->child_mutex);
1515 list_for_each_entry(child, &counter->child_list, child_list)
1517 mutex_unlock(&counter->child_mutex);
1520 static void perf_counter_for_each(struct perf_counter *counter,
1521 void (*func)(struct perf_counter *))
1523 struct perf_counter *child;
1525 mutex_lock(&counter->child_mutex);
1526 perf_counter_for_each_sibling(counter, func);
1527 list_for_each_entry(child, &counter->child_list, child_list)
1528 perf_counter_for_each_sibling(child, func);
1529 mutex_unlock(&counter->child_mutex);
1532 static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1534 struct perf_counter *counter = file->private_data;
1535 void (*func)(struct perf_counter *);
1539 case PERF_COUNTER_IOC_ENABLE:
1540 func = perf_counter_enable;
1542 case PERF_COUNTER_IOC_DISABLE:
1543 func = perf_counter_disable;
1545 case PERF_COUNTER_IOC_RESET:
1546 func = perf_counter_reset;
1549 case PERF_COUNTER_IOC_REFRESH:
1550 return perf_counter_refresh(counter, arg);
1555 if (flags & PERF_IOC_FLAG_GROUP)
1556 perf_counter_for_each(counter, func);
1558 perf_counter_for_each_child(counter, func);
1564 * Callers need to ensure there can be no nesting of this function, otherwise
1565 * the seqlock logic goes bad. We can not serialize this because the arch
1566 * code calls this from NMI context.
1568 void perf_counter_update_userpage(struct perf_counter *counter)
1570 struct perf_mmap_data *data;
1571 struct perf_counter_mmap_page *userpg;
1574 data = rcu_dereference(counter->data);
1578 userpg = data->user_page;
1581 * Disable preemption so as to not let the corresponding user-space
1582 * spin too long if we get preempted.
1587 userpg->index = counter->hw.idx;
1588 userpg->offset = atomic64_read(&counter->count);
1589 if (counter->state == PERF_COUNTER_STATE_ACTIVE)
1590 userpg->offset -= atomic64_read(&counter->hw.prev_count);
1599 static int perf_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1601 struct perf_counter *counter = vma->vm_file->private_data;
1602 struct perf_mmap_data *data;
1603 int ret = VM_FAULT_SIGBUS;
1606 data = rcu_dereference(counter->data);
1610 if (vmf->pgoff == 0) {
1611 vmf->page = virt_to_page(data->user_page);
1613 int nr = vmf->pgoff - 1;
1615 if ((unsigned)nr > data->nr_pages)
1618 vmf->page = virt_to_page(data->data_pages[nr]);
1620 get_page(vmf->page);
1628 static int perf_mmap_data_alloc(struct perf_counter *counter, int nr_pages)
1630 struct perf_mmap_data *data;
1634 WARN_ON(atomic_read(&counter->mmap_count));
1636 size = sizeof(struct perf_mmap_data);
1637 size += nr_pages * sizeof(void *);
1639 data = kzalloc(size, GFP_KERNEL);
1643 data->user_page = (void *)get_zeroed_page(GFP_KERNEL);
1644 if (!data->user_page)
1645 goto fail_user_page;
1647 for (i = 0; i < nr_pages; i++) {
1648 data->data_pages[i] = (void *)get_zeroed_page(GFP_KERNEL);
1649 if (!data->data_pages[i])
1650 goto fail_data_pages;
1653 data->nr_pages = nr_pages;
1654 atomic_set(&data->lock, -1);
1656 rcu_assign_pointer(counter->data, data);
1661 for (i--; i >= 0; i--)
1662 free_page((unsigned long)data->data_pages[i]);
1664 free_page((unsigned long)data->user_page);
1673 static void __perf_mmap_data_free(struct rcu_head *rcu_head)
1675 struct perf_mmap_data *data = container_of(rcu_head,
1676 struct perf_mmap_data, rcu_head);
1679 free_page((unsigned long)data->user_page);
1680 for (i = 0; i < data->nr_pages; i++)
1681 free_page((unsigned long)data->data_pages[i]);
1685 static void perf_mmap_data_free(struct perf_counter *counter)
1687 struct perf_mmap_data *data = counter->data;
1689 WARN_ON(atomic_read(&counter->mmap_count));
1691 rcu_assign_pointer(counter->data, NULL);
1692 call_rcu(&data->rcu_head, __perf_mmap_data_free);
1695 static void perf_mmap_open(struct vm_area_struct *vma)
1697 struct perf_counter *counter = vma->vm_file->private_data;
1699 atomic_inc(&counter->mmap_count);
1702 static void perf_mmap_close(struct vm_area_struct *vma)
1704 struct perf_counter *counter = vma->vm_file->private_data;
1706 if (atomic_dec_and_mutex_lock(&counter->mmap_count,
1707 &counter->mmap_mutex)) {
1708 struct user_struct *user = current_user();
1710 atomic_long_sub(counter->data->nr_pages + 1, &user->locked_vm);
1711 vma->vm_mm->locked_vm -= counter->data->nr_locked;
1712 perf_mmap_data_free(counter);
1713 mutex_unlock(&counter->mmap_mutex);
1717 static struct vm_operations_struct perf_mmap_vmops = {
1718 .open = perf_mmap_open,
1719 .close = perf_mmap_close,
1720 .fault = perf_mmap_fault,
1723 static int perf_mmap(struct file *file, struct vm_area_struct *vma)
1725 struct perf_counter *counter = file->private_data;
1726 struct user_struct *user = current_user();
1727 unsigned long vma_size;
1728 unsigned long nr_pages;
1729 unsigned long user_locked, user_lock_limit;
1730 unsigned long locked, lock_limit;
1731 long user_extra, extra;
1734 if (!(vma->vm_flags & VM_SHARED) || (vma->vm_flags & VM_WRITE))
1737 vma_size = vma->vm_end - vma->vm_start;
1738 nr_pages = (vma_size / PAGE_SIZE) - 1;
1741 * If we have data pages ensure they're a power-of-two number, so we
1742 * can do bitmasks instead of modulo.
1744 if (nr_pages != 0 && !is_power_of_2(nr_pages))
1747 if (vma_size != PAGE_SIZE * (1 + nr_pages))
1750 if (vma->vm_pgoff != 0)
1753 mutex_lock(&counter->mmap_mutex);
1754 if (atomic_inc_not_zero(&counter->mmap_count)) {
1755 if (nr_pages != counter->data->nr_pages)
1760 user_extra = nr_pages + 1;
1761 user_lock_limit = sysctl_perf_counter_mlock >> (PAGE_SHIFT - 10);
1762 user_locked = atomic_long_read(&user->locked_vm) + user_extra;
1765 if (user_locked > user_lock_limit)
1766 extra = user_locked - user_lock_limit;
1768 lock_limit = current->signal->rlim[RLIMIT_MEMLOCK].rlim_cur;
1769 lock_limit >>= PAGE_SHIFT;
1770 locked = vma->vm_mm->locked_vm + extra;
1772 if ((locked > lock_limit) && !capable(CAP_IPC_LOCK)) {
1777 WARN_ON(counter->data);
1778 ret = perf_mmap_data_alloc(counter, nr_pages);
1782 atomic_set(&counter->mmap_count, 1);
1783 atomic_long_add(user_extra, &user->locked_vm);
1784 vma->vm_mm->locked_vm += extra;
1785 counter->data->nr_locked = extra;
1787 mutex_unlock(&counter->mmap_mutex);
1789 vma->vm_flags &= ~VM_MAYWRITE;
1790 vma->vm_flags |= VM_RESERVED;
1791 vma->vm_ops = &perf_mmap_vmops;
1796 static int perf_fasync(int fd, struct file *filp, int on)
1798 struct perf_counter *counter = filp->private_data;
1799 struct inode *inode = filp->f_path.dentry->d_inode;
1802 mutex_lock(&inode->i_mutex);
1803 retval = fasync_helper(fd, filp, on, &counter->fasync);
1804 mutex_unlock(&inode->i_mutex);
1812 static const struct file_operations perf_fops = {
1813 .release = perf_release,
1816 .unlocked_ioctl = perf_ioctl,
1817 .compat_ioctl = perf_ioctl,
1819 .fasync = perf_fasync,
1823 * Perf counter wakeup
1825 * If there's data, ensure we set the poll() state and publish everything
1826 * to user-space before waking everybody up.
1829 void perf_counter_wakeup(struct perf_counter *counter)
1831 wake_up_all(&counter->waitq);
1833 if (counter->pending_kill) {
1834 kill_fasync(&counter->fasync, SIGIO, counter->pending_kill);
1835 counter->pending_kill = 0;
1842 * Handle the case where we need to wakeup up from NMI (or rq->lock) context.
1844 * The NMI bit means we cannot possibly take locks. Therefore, maintain a
1845 * single linked list and use cmpxchg() to add entries lockless.
1848 static void perf_pending_counter(struct perf_pending_entry *entry)
1850 struct perf_counter *counter = container_of(entry,
1851 struct perf_counter, pending);
1853 if (counter->pending_disable) {
1854 counter->pending_disable = 0;
1855 perf_counter_disable(counter);
1858 if (counter->pending_wakeup) {
1859 counter->pending_wakeup = 0;
1860 perf_counter_wakeup(counter);
1864 #define PENDING_TAIL ((struct perf_pending_entry *)-1UL)
1866 static DEFINE_PER_CPU(struct perf_pending_entry *, perf_pending_head) = {
1870 static void perf_pending_queue(struct perf_pending_entry *entry,
1871 void (*func)(struct perf_pending_entry *))
1873 struct perf_pending_entry **head;
1875 if (cmpxchg(&entry->next, NULL, PENDING_TAIL) != NULL)
1880 head = &get_cpu_var(perf_pending_head);
1883 entry->next = *head;
1884 } while (cmpxchg(head, entry->next, entry) != entry->next);
1886 set_perf_counter_pending();
1888 put_cpu_var(perf_pending_head);
1891 static int __perf_pending_run(void)
1893 struct perf_pending_entry *list;
1896 list = xchg(&__get_cpu_var(perf_pending_head), PENDING_TAIL);
1897 while (list != PENDING_TAIL) {
1898 void (*func)(struct perf_pending_entry *);
1899 struct perf_pending_entry *entry = list;
1906 * Ensure we observe the unqueue before we issue the wakeup,
1907 * so that we won't be waiting forever.
1908 * -- see perf_not_pending().
1919 static inline int perf_not_pending(struct perf_counter *counter)
1922 * If we flush on whatever cpu we run, there is a chance we don't
1926 __perf_pending_run();
1930 * Ensure we see the proper queue state before going to sleep
1931 * so that we do not miss the wakeup. -- see perf_pending_handle()
1934 return counter->pending.next == NULL;
1937 static void perf_pending_sync(struct perf_counter *counter)
1939 wait_event(counter->waitq, perf_not_pending(counter));
1942 void perf_counter_do_pending(void)
1944 __perf_pending_run();
1948 * Callchain support -- arch specific
1951 __weak struct perf_callchain_entry *perf_callchain(struct pt_regs *regs)
1960 struct perf_output_handle {
1961 struct perf_counter *counter;
1962 struct perf_mmap_data *data;
1963 unsigned int offset;
1968 unsigned long flags;
1971 static void perf_output_wakeup(struct perf_output_handle *handle)
1973 atomic_set(&handle->data->poll, POLL_IN);
1976 handle->counter->pending_wakeup = 1;
1977 perf_pending_queue(&handle->counter->pending,
1978 perf_pending_counter);
1980 perf_counter_wakeup(handle->counter);
1984 * Curious locking construct.
1986 * We need to ensure a later event doesn't publish a head when a former
1987 * event isn't done writing. However since we need to deal with NMIs we
1988 * cannot fully serialize things.
1990 * What we do is serialize between CPUs so we only have to deal with NMI
1991 * nesting on a single CPU.
1993 * We only publish the head (and generate a wakeup) when the outer-most
1996 static void perf_output_lock(struct perf_output_handle *handle)
1998 struct perf_mmap_data *data = handle->data;
2003 local_irq_save(handle->flags);
2004 cpu = smp_processor_id();
2006 if (in_nmi() && atomic_read(&data->lock) == cpu)
2009 while (atomic_cmpxchg(&data->lock, -1, cpu) != -1)
2015 static void perf_output_unlock(struct perf_output_handle *handle)
2017 struct perf_mmap_data *data = handle->data;
2020 data->done_head = data->head;
2022 if (!handle->locked)
2027 * The xchg implies a full barrier that ensures all writes are done
2028 * before we publish the new head, matched by a rmb() in userspace when
2029 * reading this position.
2031 while ((head = atomic_xchg(&data->done_head, 0)))
2032 data->user_page->data_head = head;
2035 * NMI can happen here, which means we can miss a done_head update.
2038 cpu = atomic_xchg(&data->lock, -1);
2039 WARN_ON_ONCE(cpu != smp_processor_id());
2042 * Therefore we have to validate we did not indeed do so.
2044 if (unlikely(atomic_read(&data->done_head))) {
2046 * Since we had it locked, we can lock it again.
2048 while (atomic_cmpxchg(&data->lock, -1, cpu) != -1)
2054 if (atomic_xchg(&data->wakeup, 0))
2055 perf_output_wakeup(handle);
2057 local_irq_restore(handle->flags);
2060 static int perf_output_begin(struct perf_output_handle *handle,
2061 struct perf_counter *counter, unsigned int size,
2062 int nmi, int overflow)
2064 struct perf_mmap_data *data;
2065 unsigned int offset, head;
2068 * For inherited counters we send all the output towards the parent.
2070 if (counter->parent)
2071 counter = counter->parent;
2074 data = rcu_dereference(counter->data);
2078 handle->data = data;
2079 handle->counter = counter;
2081 handle->overflow = overflow;
2083 if (!data->nr_pages)
2086 perf_output_lock(handle);
2089 offset = head = atomic_read(&data->head);
2091 } while (atomic_cmpxchg(&data->head, offset, head) != offset);
2093 handle->offset = offset;
2094 handle->head = head;
2096 if ((offset >> PAGE_SHIFT) != (head >> PAGE_SHIFT))
2097 atomic_set(&data->wakeup, 1);
2102 perf_output_wakeup(handle);
2109 static void perf_output_copy(struct perf_output_handle *handle,
2110 void *buf, unsigned int len)
2112 unsigned int pages_mask;
2113 unsigned int offset;
2117 offset = handle->offset;
2118 pages_mask = handle->data->nr_pages - 1;
2119 pages = handle->data->data_pages;
2122 unsigned int page_offset;
2125 nr = (offset >> PAGE_SHIFT) & pages_mask;
2126 page_offset = offset & (PAGE_SIZE - 1);
2127 size = min_t(unsigned int, PAGE_SIZE - page_offset, len);
2129 memcpy(pages[nr] + page_offset, buf, size);
2136 handle->offset = offset;
2139 * Check we didn't copy past our reservation window, taking the
2140 * possible unsigned int wrap into account.
2142 WARN_ON_ONCE(((int)(handle->head - handle->offset)) < 0);
2145 #define perf_output_put(handle, x) \
2146 perf_output_copy((handle), &(x), sizeof(x))
2148 static void perf_output_end(struct perf_output_handle *handle)
2150 struct perf_counter *counter = handle->counter;
2151 struct perf_mmap_data *data = handle->data;
2153 int wakeup_events = counter->hw_event.wakeup_events;
2155 if (handle->overflow && wakeup_events) {
2156 int events = atomic_inc_return(&data->events);
2157 if (events >= wakeup_events) {
2158 atomic_sub(wakeup_events, &data->events);
2159 atomic_set(&data->wakeup, 1);
2163 perf_output_unlock(handle);
2167 static void perf_counter_output(struct perf_counter *counter,
2168 int nmi, struct pt_regs *regs, u64 addr)
2171 u64 record_type = counter->hw_event.record_type;
2172 struct perf_output_handle handle;
2173 struct perf_event_header header;
2182 struct perf_callchain_entry *callchain = NULL;
2183 int callchain_size = 0;
2190 header.size = sizeof(header);
2192 header.misc = PERF_EVENT_MISC_OVERFLOW;
2193 header.misc |= perf_misc_flags(regs);
2195 if (record_type & PERF_RECORD_IP) {
2196 ip = perf_instruction_pointer(regs);
2197 header.type |= PERF_RECORD_IP;
2198 header.size += sizeof(ip);
2201 if (record_type & PERF_RECORD_TID) {
2202 /* namespace issues */
2203 tid_entry.pid = current->group_leader->pid;
2204 tid_entry.tid = current->pid;
2206 header.type |= PERF_RECORD_TID;
2207 header.size += sizeof(tid_entry);
2210 if (record_type & PERF_RECORD_TIME) {
2212 * Maybe do better on x86 and provide cpu_clock_nmi()
2214 time = sched_clock();
2216 header.type |= PERF_RECORD_TIME;
2217 header.size += sizeof(u64);
2220 if (record_type & PERF_RECORD_ADDR) {
2221 header.type |= PERF_RECORD_ADDR;
2222 header.size += sizeof(u64);
2225 if (record_type & PERF_RECORD_CONFIG) {
2226 header.type |= PERF_RECORD_CONFIG;
2227 header.size += sizeof(u64);
2230 if (record_type & PERF_RECORD_CPU) {
2231 header.type |= PERF_RECORD_CPU;
2232 header.size += sizeof(cpu_entry);
2234 cpu_entry.cpu = raw_smp_processor_id();
2237 if (record_type & PERF_RECORD_GROUP) {
2238 header.type |= PERF_RECORD_GROUP;
2239 header.size += sizeof(u64) +
2240 counter->nr_siblings * sizeof(group_entry);
2243 if (record_type & PERF_RECORD_CALLCHAIN) {
2244 callchain = perf_callchain(regs);
2247 callchain_size = (1 + callchain->nr) * sizeof(u64);
2249 header.type |= PERF_RECORD_CALLCHAIN;
2250 header.size += callchain_size;
2254 ret = perf_output_begin(&handle, counter, header.size, nmi, 1);
2258 perf_output_put(&handle, header);
2260 if (record_type & PERF_RECORD_IP)
2261 perf_output_put(&handle, ip);
2263 if (record_type & PERF_RECORD_TID)
2264 perf_output_put(&handle, tid_entry);
2266 if (record_type & PERF_RECORD_TIME)
2267 perf_output_put(&handle, time);
2269 if (record_type & PERF_RECORD_ADDR)
2270 perf_output_put(&handle, addr);
2272 if (record_type & PERF_RECORD_CONFIG)
2273 perf_output_put(&handle, counter->hw_event.config);
2275 if (record_type & PERF_RECORD_CPU)
2276 perf_output_put(&handle, cpu_entry);
2279 * XXX PERF_RECORD_GROUP vs inherited counters seems difficult.
2281 if (record_type & PERF_RECORD_GROUP) {
2282 struct perf_counter *leader, *sub;
2283 u64 nr = counter->nr_siblings;
2285 perf_output_put(&handle, nr);
2287 leader = counter->group_leader;
2288 list_for_each_entry(sub, &leader->sibling_list, list_entry) {
2290 sub->pmu->read(sub);
2292 group_entry.event = sub->hw_event.config;
2293 group_entry.counter = atomic64_read(&sub->count);
2295 perf_output_put(&handle, group_entry);
2300 perf_output_copy(&handle, callchain, callchain_size);
2302 perf_output_end(&handle);
2309 struct perf_comm_event {
2310 struct task_struct *task;
2315 struct perf_event_header header;
2322 static void perf_counter_comm_output(struct perf_counter *counter,
2323 struct perf_comm_event *comm_event)
2325 struct perf_output_handle handle;
2326 int size = comm_event->event.header.size;
2327 int ret = perf_output_begin(&handle, counter, size, 0, 0);
2332 perf_output_put(&handle, comm_event->event);
2333 perf_output_copy(&handle, comm_event->comm,
2334 comm_event->comm_size);
2335 perf_output_end(&handle);
2338 static int perf_counter_comm_match(struct perf_counter *counter,
2339 struct perf_comm_event *comm_event)
2341 if (counter->hw_event.comm &&
2342 comm_event->event.header.type == PERF_EVENT_COMM)
2348 static void perf_counter_comm_ctx(struct perf_counter_context *ctx,
2349 struct perf_comm_event *comm_event)
2351 struct perf_counter *counter;
2353 if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
2357 list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
2358 if (perf_counter_comm_match(counter, comm_event))
2359 perf_counter_comm_output(counter, comm_event);
2364 static void perf_counter_comm_event(struct perf_comm_event *comm_event)
2366 struct perf_cpu_context *cpuctx;
2368 char *comm = comm_event->task->comm;
2370 size = ALIGN(strlen(comm)+1, sizeof(u64));
2372 comm_event->comm = comm;
2373 comm_event->comm_size = size;
2375 comm_event->event.header.size = sizeof(comm_event->event) + size;
2377 cpuctx = &get_cpu_var(perf_cpu_context);
2378 perf_counter_comm_ctx(&cpuctx->ctx, comm_event);
2379 put_cpu_var(perf_cpu_context);
2381 perf_counter_comm_ctx(current->perf_counter_ctxp, comm_event);
2384 void perf_counter_comm(struct task_struct *task)
2386 struct perf_comm_event comm_event;
2388 if (!atomic_read(&nr_comm_tracking))
2390 if (!current->perf_counter_ctxp)
2393 comm_event = (struct perf_comm_event){
2396 .header = { .type = PERF_EVENT_COMM, },
2397 .pid = task->group_leader->pid,
2402 perf_counter_comm_event(&comm_event);
2409 struct perf_mmap_event {
2415 struct perf_event_header header;
2425 static void perf_counter_mmap_output(struct perf_counter *counter,
2426 struct perf_mmap_event *mmap_event)
2428 struct perf_output_handle handle;
2429 int size = mmap_event->event.header.size;
2430 int ret = perf_output_begin(&handle, counter, size, 0, 0);
2435 perf_output_put(&handle, mmap_event->event);
2436 perf_output_copy(&handle, mmap_event->file_name,
2437 mmap_event->file_size);
2438 perf_output_end(&handle);
2441 static int perf_counter_mmap_match(struct perf_counter *counter,
2442 struct perf_mmap_event *mmap_event)
2444 if (counter->hw_event.mmap &&
2445 mmap_event->event.header.type == PERF_EVENT_MMAP)
2448 if (counter->hw_event.munmap &&
2449 mmap_event->event.header.type == PERF_EVENT_MUNMAP)
2455 static void perf_counter_mmap_ctx(struct perf_counter_context *ctx,
2456 struct perf_mmap_event *mmap_event)
2458 struct perf_counter *counter;
2460 if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
2464 list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
2465 if (perf_counter_mmap_match(counter, mmap_event))
2466 perf_counter_mmap_output(counter, mmap_event);
2471 static void perf_counter_mmap_event(struct perf_mmap_event *mmap_event)
2473 struct perf_cpu_context *cpuctx;
2474 struct file *file = mmap_event->file;
2481 buf = kzalloc(PATH_MAX, GFP_KERNEL);
2483 name = strncpy(tmp, "//enomem", sizeof(tmp));
2486 name = d_path(&file->f_path, buf, PATH_MAX);
2488 name = strncpy(tmp, "//toolong", sizeof(tmp));
2492 name = strncpy(tmp, "//anon", sizeof(tmp));
2497 size = ALIGN(strlen(name)+1, sizeof(u64));
2499 mmap_event->file_name = name;
2500 mmap_event->file_size = size;
2502 mmap_event->event.header.size = sizeof(mmap_event->event) + size;
2504 cpuctx = &get_cpu_var(perf_cpu_context);
2505 perf_counter_mmap_ctx(&cpuctx->ctx, mmap_event);
2506 put_cpu_var(perf_cpu_context);
2508 perf_counter_mmap_ctx(current->perf_counter_ctxp, mmap_event);
2513 void perf_counter_mmap(unsigned long addr, unsigned long len,
2514 unsigned long pgoff, struct file *file)
2516 struct perf_mmap_event mmap_event;
2518 if (!atomic_read(&nr_mmap_tracking))
2520 if (!current->perf_counter_ctxp)
2523 mmap_event = (struct perf_mmap_event){
2526 .header = { .type = PERF_EVENT_MMAP, },
2527 .pid = current->group_leader->pid,
2528 .tid = current->pid,
2535 perf_counter_mmap_event(&mmap_event);
2538 void perf_counter_munmap(unsigned long addr, unsigned long len,
2539 unsigned long pgoff, struct file *file)
2541 struct perf_mmap_event mmap_event;
2543 if (!atomic_read(&nr_munmap_tracking))
2546 mmap_event = (struct perf_mmap_event){
2549 .header = { .type = PERF_EVENT_MUNMAP, },
2550 .pid = current->group_leader->pid,
2551 .tid = current->pid,
2558 perf_counter_mmap_event(&mmap_event);
2562 * Log irq_period changes so that analyzing tools can re-normalize the
2566 static void perf_log_period(struct perf_counter *counter, u64 period)
2568 struct perf_output_handle handle;
2572 struct perf_event_header header;
2577 .type = PERF_EVENT_PERIOD,
2579 .size = sizeof(freq_event),
2581 .time = sched_clock(),
2585 if (counter->hw.irq_period == period)
2588 ret = perf_output_begin(&handle, counter, sizeof(freq_event), 0, 0);
2592 perf_output_put(&handle, freq_event);
2593 perf_output_end(&handle);
2597 * Generic counter overflow handling.
2600 int perf_counter_overflow(struct perf_counter *counter,
2601 int nmi, struct pt_regs *regs, u64 addr)
2603 int events = atomic_read(&counter->event_limit);
2606 counter->hw.interrupts++;
2609 * XXX event_limit might not quite work as expected on inherited
2613 counter->pending_kill = POLL_IN;
2614 if (events && atomic_dec_and_test(&counter->event_limit)) {
2616 counter->pending_kill = POLL_HUP;
2618 counter->pending_disable = 1;
2619 perf_pending_queue(&counter->pending,
2620 perf_pending_counter);
2622 perf_counter_disable(counter);
2625 perf_counter_output(counter, nmi, regs, addr);
2630 * Generic software counter infrastructure
2633 static void perf_swcounter_update(struct perf_counter *counter)
2635 struct hw_perf_counter *hwc = &counter->hw;
2640 prev = atomic64_read(&hwc->prev_count);
2641 now = atomic64_read(&hwc->count);
2642 if (atomic64_cmpxchg(&hwc->prev_count, prev, now) != prev)
2647 atomic64_add(delta, &counter->count);
2648 atomic64_sub(delta, &hwc->period_left);
2651 static void perf_swcounter_set_period(struct perf_counter *counter)
2653 struct hw_perf_counter *hwc = &counter->hw;
2654 s64 left = atomic64_read(&hwc->period_left);
2655 s64 period = hwc->irq_period;
2657 if (unlikely(left <= -period)) {
2659 atomic64_set(&hwc->period_left, left);
2662 if (unlikely(left <= 0)) {
2664 atomic64_add(period, &hwc->period_left);
2667 atomic64_set(&hwc->prev_count, -left);
2668 atomic64_set(&hwc->count, -left);
2671 static enum hrtimer_restart perf_swcounter_hrtimer(struct hrtimer *hrtimer)
2673 enum hrtimer_restart ret = HRTIMER_RESTART;
2674 struct perf_counter *counter;
2675 struct pt_regs *regs;
2678 counter = container_of(hrtimer, struct perf_counter, hw.hrtimer);
2679 counter->pmu->read(counter);
2681 regs = get_irq_regs();
2683 * In case we exclude kernel IPs or are somehow not in interrupt
2684 * context, provide the next best thing, the user IP.
2686 if ((counter->hw_event.exclude_kernel || !regs) &&
2687 !counter->hw_event.exclude_user)
2688 regs = task_pt_regs(current);
2691 if (perf_counter_overflow(counter, 0, regs, 0))
2692 ret = HRTIMER_NORESTART;
2695 period = max_t(u64, 10000, counter->hw.irq_period);
2696 hrtimer_forward_now(hrtimer, ns_to_ktime(period));
2701 static void perf_swcounter_overflow(struct perf_counter *counter,
2702 int nmi, struct pt_regs *regs, u64 addr)
2704 perf_swcounter_update(counter);
2705 perf_swcounter_set_period(counter);
2706 if (perf_counter_overflow(counter, nmi, regs, addr))
2707 /* soft-disable the counter */
2712 static int perf_swcounter_match(struct perf_counter *counter,
2713 enum perf_event_types type,
2714 u32 event, struct pt_regs *regs)
2716 if (counter->state != PERF_COUNTER_STATE_ACTIVE)
2719 if (perf_event_raw(&counter->hw_event))
2722 if (perf_event_type(&counter->hw_event) != type)
2725 if (perf_event_id(&counter->hw_event) != event)
2728 if (counter->hw_event.exclude_user && user_mode(regs))
2731 if (counter->hw_event.exclude_kernel && !user_mode(regs))
2737 static void perf_swcounter_add(struct perf_counter *counter, u64 nr,
2738 int nmi, struct pt_regs *regs, u64 addr)
2740 int neg = atomic64_add_negative(nr, &counter->hw.count);
2741 if (counter->hw.irq_period && !neg)
2742 perf_swcounter_overflow(counter, nmi, regs, addr);
2745 static void perf_swcounter_ctx_event(struct perf_counter_context *ctx,
2746 enum perf_event_types type, u32 event,
2747 u64 nr, int nmi, struct pt_regs *regs,
2750 struct perf_counter *counter;
2752 if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
2756 list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
2757 if (perf_swcounter_match(counter, type, event, regs))
2758 perf_swcounter_add(counter, nr, nmi, regs, addr);
2763 static int *perf_swcounter_recursion_context(struct perf_cpu_context *cpuctx)
2766 return &cpuctx->recursion[3];
2769 return &cpuctx->recursion[2];
2772 return &cpuctx->recursion[1];
2774 return &cpuctx->recursion[0];
2777 static void __perf_swcounter_event(enum perf_event_types type, u32 event,
2778 u64 nr, int nmi, struct pt_regs *regs,
2781 struct perf_cpu_context *cpuctx = &get_cpu_var(perf_cpu_context);
2782 int *recursion = perf_swcounter_recursion_context(cpuctx);
2790 perf_swcounter_ctx_event(&cpuctx->ctx, type, event,
2791 nr, nmi, regs, addr);
2792 if (cpuctx->task_ctx) {
2793 perf_swcounter_ctx_event(cpuctx->task_ctx, type, event,
2794 nr, nmi, regs, addr);
2801 put_cpu_var(perf_cpu_context);
2805 perf_swcounter_event(u32 event, u64 nr, int nmi, struct pt_regs *regs, u64 addr)
2807 __perf_swcounter_event(PERF_TYPE_SOFTWARE, event, nr, nmi, regs, addr);
2810 static void perf_swcounter_read(struct perf_counter *counter)
2812 perf_swcounter_update(counter);
2815 static int perf_swcounter_enable(struct perf_counter *counter)
2817 perf_swcounter_set_period(counter);
2821 static void perf_swcounter_disable(struct perf_counter *counter)
2823 perf_swcounter_update(counter);
2826 static const struct pmu perf_ops_generic = {
2827 .enable = perf_swcounter_enable,
2828 .disable = perf_swcounter_disable,
2829 .read = perf_swcounter_read,
2833 * Software counter: cpu wall time clock
2836 static void cpu_clock_perf_counter_update(struct perf_counter *counter)
2838 int cpu = raw_smp_processor_id();
2842 now = cpu_clock(cpu);
2843 prev = atomic64_read(&counter->hw.prev_count);
2844 atomic64_set(&counter->hw.prev_count, now);
2845 atomic64_add(now - prev, &counter->count);
2848 static int cpu_clock_perf_counter_enable(struct perf_counter *counter)
2850 struct hw_perf_counter *hwc = &counter->hw;
2851 int cpu = raw_smp_processor_id();
2853 atomic64_set(&hwc->prev_count, cpu_clock(cpu));
2854 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
2855 hwc->hrtimer.function = perf_swcounter_hrtimer;
2856 if (hwc->irq_period) {
2857 u64 period = max_t(u64, 10000, hwc->irq_period);
2858 __hrtimer_start_range_ns(&hwc->hrtimer,
2859 ns_to_ktime(period), 0,
2860 HRTIMER_MODE_REL, 0);
2866 static void cpu_clock_perf_counter_disable(struct perf_counter *counter)
2868 if (counter->hw.irq_period)
2869 hrtimer_cancel(&counter->hw.hrtimer);
2870 cpu_clock_perf_counter_update(counter);
2873 static void cpu_clock_perf_counter_read(struct perf_counter *counter)
2875 cpu_clock_perf_counter_update(counter);
2878 static const struct pmu perf_ops_cpu_clock = {
2879 .enable = cpu_clock_perf_counter_enable,
2880 .disable = cpu_clock_perf_counter_disable,
2881 .read = cpu_clock_perf_counter_read,
2885 * Software counter: task time clock
2888 static void task_clock_perf_counter_update(struct perf_counter *counter, u64 now)
2893 prev = atomic64_xchg(&counter->hw.prev_count, now);
2895 atomic64_add(delta, &counter->count);
2898 static int task_clock_perf_counter_enable(struct perf_counter *counter)
2900 struct hw_perf_counter *hwc = &counter->hw;
2903 now = counter->ctx->time;
2905 atomic64_set(&hwc->prev_count, now);
2906 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
2907 hwc->hrtimer.function = perf_swcounter_hrtimer;
2908 if (hwc->irq_period) {
2909 u64 period = max_t(u64, 10000, hwc->irq_period);
2910 __hrtimer_start_range_ns(&hwc->hrtimer,
2911 ns_to_ktime(period), 0,
2912 HRTIMER_MODE_REL, 0);
2918 static void task_clock_perf_counter_disable(struct perf_counter *counter)
2920 if (counter->hw.irq_period)
2921 hrtimer_cancel(&counter->hw.hrtimer);
2922 task_clock_perf_counter_update(counter, counter->ctx->time);
2926 static void task_clock_perf_counter_read(struct perf_counter *counter)
2931 update_context_time(counter->ctx);
2932 time = counter->ctx->time;
2934 u64 now = perf_clock();
2935 u64 delta = now - counter->ctx->timestamp;
2936 time = counter->ctx->time + delta;
2939 task_clock_perf_counter_update(counter, time);
2942 static const struct pmu perf_ops_task_clock = {
2943 .enable = task_clock_perf_counter_enable,
2944 .disable = task_clock_perf_counter_disable,
2945 .read = task_clock_perf_counter_read,
2949 * Software counter: cpu migrations
2952 static inline u64 get_cpu_migrations(struct perf_counter *counter)
2954 struct task_struct *curr = counter->ctx->task;
2957 return curr->se.nr_migrations;
2958 return cpu_nr_migrations(smp_processor_id());
2961 static void cpu_migrations_perf_counter_update(struct perf_counter *counter)
2966 prev = atomic64_read(&counter->hw.prev_count);
2967 now = get_cpu_migrations(counter);
2969 atomic64_set(&counter->hw.prev_count, now);
2973 atomic64_add(delta, &counter->count);
2976 static void cpu_migrations_perf_counter_read(struct perf_counter *counter)
2978 cpu_migrations_perf_counter_update(counter);
2981 static int cpu_migrations_perf_counter_enable(struct perf_counter *counter)
2983 if (counter->prev_state <= PERF_COUNTER_STATE_OFF)
2984 atomic64_set(&counter->hw.prev_count,
2985 get_cpu_migrations(counter));
2989 static void cpu_migrations_perf_counter_disable(struct perf_counter *counter)
2991 cpu_migrations_perf_counter_update(counter);
2994 static const struct pmu perf_ops_cpu_migrations = {
2995 .enable = cpu_migrations_perf_counter_enable,
2996 .disable = cpu_migrations_perf_counter_disable,
2997 .read = cpu_migrations_perf_counter_read,
3000 #ifdef CONFIG_EVENT_PROFILE
3001 void perf_tpcounter_event(int event_id)
3003 struct pt_regs *regs = get_irq_regs();
3006 regs = task_pt_regs(current);
3008 __perf_swcounter_event(PERF_TYPE_TRACEPOINT, event_id, 1, 1, regs, 0);
3010 EXPORT_SYMBOL_GPL(perf_tpcounter_event);
3012 extern int ftrace_profile_enable(int);
3013 extern void ftrace_profile_disable(int);
3015 static void tp_perf_counter_destroy(struct perf_counter *counter)
3017 ftrace_profile_disable(perf_event_id(&counter->hw_event));
3020 static const struct pmu *tp_perf_counter_init(struct perf_counter *counter)
3022 int event_id = perf_event_id(&counter->hw_event);
3025 ret = ftrace_profile_enable(event_id);
3029 counter->destroy = tp_perf_counter_destroy;
3030 counter->hw.irq_period = counter->hw_event.irq_period;
3032 return &perf_ops_generic;
3035 static const struct pmu *tp_perf_counter_init(struct perf_counter *counter)
3041 static const struct pmu *sw_perf_counter_init(struct perf_counter *counter)
3043 const struct pmu *pmu = NULL;
3046 * Software counters (currently) can't in general distinguish
3047 * between user, kernel and hypervisor events.
3048 * However, context switches and cpu migrations are considered
3049 * to be kernel events, and page faults are never hypervisor
3052 switch (perf_event_id(&counter->hw_event)) {
3053 case PERF_COUNT_CPU_CLOCK:
3054 pmu = &perf_ops_cpu_clock;
3057 case PERF_COUNT_TASK_CLOCK:
3059 * If the user instantiates this as a per-cpu counter,
3060 * use the cpu_clock counter instead.
3062 if (counter->ctx->task)
3063 pmu = &perf_ops_task_clock;
3065 pmu = &perf_ops_cpu_clock;
3068 case PERF_COUNT_PAGE_FAULTS:
3069 case PERF_COUNT_PAGE_FAULTS_MIN:
3070 case PERF_COUNT_PAGE_FAULTS_MAJ:
3071 case PERF_COUNT_CONTEXT_SWITCHES:
3072 pmu = &perf_ops_generic;
3074 case PERF_COUNT_CPU_MIGRATIONS:
3075 if (!counter->hw_event.exclude_kernel)
3076 pmu = &perf_ops_cpu_migrations;
3084 * Allocate and initialize a counter structure
3086 static struct perf_counter *
3087 perf_counter_alloc(struct perf_counter_hw_event *hw_event,
3089 struct perf_counter_context *ctx,
3090 struct perf_counter *group_leader,
3093 const struct pmu *pmu;
3094 struct perf_counter *counter;
3095 struct hw_perf_counter *hwc;
3098 counter = kzalloc(sizeof(*counter), gfpflags);
3100 return ERR_PTR(-ENOMEM);
3103 * Single counters are their own group leaders, with an
3104 * empty sibling list:
3107 group_leader = counter;
3109 mutex_init(&counter->child_mutex);
3110 INIT_LIST_HEAD(&counter->child_list);
3112 INIT_LIST_HEAD(&counter->list_entry);
3113 INIT_LIST_HEAD(&counter->event_entry);
3114 INIT_LIST_HEAD(&counter->sibling_list);
3115 init_waitqueue_head(&counter->waitq);
3117 mutex_init(&counter->mmap_mutex);
3120 counter->hw_event = *hw_event;
3121 counter->group_leader = group_leader;
3122 counter->pmu = NULL;
3126 counter->state = PERF_COUNTER_STATE_INACTIVE;
3127 if (hw_event->disabled)
3128 counter->state = PERF_COUNTER_STATE_OFF;
3133 if (hw_event->freq && hw_event->irq_freq)
3134 hwc->irq_period = div64_u64(TICK_NSEC, hw_event->irq_freq);
3136 hwc->irq_period = hw_event->irq_period;
3139 * we currently do not support PERF_RECORD_GROUP on inherited counters
3141 if (hw_event->inherit && (hw_event->record_type & PERF_RECORD_GROUP))
3144 if (perf_event_raw(hw_event)) {
3145 pmu = hw_perf_counter_init(counter);
3149 switch (perf_event_type(hw_event)) {
3150 case PERF_TYPE_HARDWARE:
3151 pmu = hw_perf_counter_init(counter);
3154 case PERF_TYPE_SOFTWARE:
3155 pmu = sw_perf_counter_init(counter);
3158 case PERF_TYPE_TRACEPOINT:
3159 pmu = tp_perf_counter_init(counter);
3166 else if (IS_ERR(pmu))
3171 return ERR_PTR(err);
3176 atomic_inc(&nr_counters);
3177 if (counter->hw_event.mmap)
3178 atomic_inc(&nr_mmap_tracking);
3179 if (counter->hw_event.munmap)
3180 atomic_inc(&nr_munmap_tracking);
3181 if (counter->hw_event.comm)
3182 atomic_inc(&nr_comm_tracking);
3188 * sys_perf_counter_open - open a performance counter, associate it to a task/cpu
3190 * @hw_event_uptr: event type attributes for monitoring/sampling
3193 * @group_fd: group leader counter fd
3195 SYSCALL_DEFINE5(perf_counter_open,
3196 const struct perf_counter_hw_event __user *, hw_event_uptr,
3197 pid_t, pid, int, cpu, int, group_fd, unsigned long, flags)
3199 struct perf_counter *counter, *group_leader;
3200 struct perf_counter_hw_event hw_event;
3201 struct perf_counter_context *ctx;
3202 struct file *counter_file = NULL;
3203 struct file *group_file = NULL;
3204 int fput_needed = 0;
3205 int fput_needed2 = 0;
3208 /* for future expandability... */
3212 if (copy_from_user(&hw_event, hw_event_uptr, sizeof(hw_event)) != 0)
3216 * Get the target context (task or percpu):
3218 ctx = find_get_context(pid, cpu);
3220 return PTR_ERR(ctx);
3223 * Look up the group leader (we will attach this counter to it):
3225 group_leader = NULL;
3226 if (group_fd != -1) {
3228 group_file = fget_light(group_fd, &fput_needed);
3230 goto err_put_context;
3231 if (group_file->f_op != &perf_fops)
3232 goto err_put_context;
3234 group_leader = group_file->private_data;
3236 * Do not allow a recursive hierarchy (this new sibling
3237 * becoming part of another group-sibling):
3239 if (group_leader->group_leader != group_leader)
3240 goto err_put_context;
3242 * Do not allow to attach to a group in a different
3243 * task or CPU context:
3245 if (group_leader->ctx != ctx)
3246 goto err_put_context;
3248 * Only a group leader can be exclusive or pinned
3250 if (hw_event.exclusive || hw_event.pinned)
3251 goto err_put_context;
3254 counter = perf_counter_alloc(&hw_event, cpu, ctx, group_leader,
3256 ret = PTR_ERR(counter);
3257 if (IS_ERR(counter))
3258 goto err_put_context;
3260 ret = anon_inode_getfd("[perf_counter]", &perf_fops, counter, 0);
3262 goto err_free_put_context;
3264 counter_file = fget_light(ret, &fput_needed2);
3266 goto err_free_put_context;
3268 counter->filp = counter_file;
3269 mutex_lock(&ctx->mutex);
3270 perf_install_in_context(ctx, counter, cpu);
3271 mutex_unlock(&ctx->mutex);
3273 fput_light(counter_file, fput_needed2);
3276 fput_light(group_file, fput_needed);
3280 err_free_put_context:
3290 * inherit a counter from parent task to child task:
3292 static struct perf_counter *
3293 inherit_counter(struct perf_counter *parent_counter,
3294 struct task_struct *parent,
3295 struct perf_counter_context *parent_ctx,
3296 struct task_struct *child,
3297 struct perf_counter *group_leader,
3298 struct perf_counter_context *child_ctx)
3300 struct perf_counter *child_counter;
3303 * Instead of creating recursive hierarchies of counters,
3304 * we link inherited counters back to the original parent,
3305 * which has a filp for sure, which we use as the reference
3308 if (parent_counter->parent)
3309 parent_counter = parent_counter->parent;
3311 child_counter = perf_counter_alloc(&parent_counter->hw_event,
3312 parent_counter->cpu, child_ctx,
3313 group_leader, GFP_KERNEL);
3314 if (IS_ERR(child_counter))
3315 return child_counter;
3318 * Make the child state follow the state of the parent counter,
3319 * not its hw_event.disabled bit. We hold the parent's mutex,
3320 * so we won't race with perf_counter_{en,dis}able_family.
3322 if (parent_counter->state >= PERF_COUNTER_STATE_INACTIVE)
3323 child_counter->state = PERF_COUNTER_STATE_INACTIVE;
3325 child_counter->state = PERF_COUNTER_STATE_OFF;
3328 * Link it up in the child's context:
3330 add_counter_to_ctx(child_counter, child_ctx);
3332 child_counter->parent = parent_counter;
3334 * inherit into child's child as well:
3336 child_counter->hw_event.inherit = 1;
3339 * Get a reference to the parent filp - we will fput it
3340 * when the child counter exits. This is safe to do because
3341 * we are in the parent and we know that the filp still
3342 * exists and has a nonzero count:
3344 atomic_long_inc(&parent_counter->filp->f_count);
3347 * Link this into the parent counter's child list
3349 mutex_lock(&parent_counter->child_mutex);
3350 list_add_tail(&child_counter->child_list, &parent_counter->child_list);
3351 mutex_unlock(&parent_counter->child_mutex);
3353 return child_counter;
3356 static int inherit_group(struct perf_counter *parent_counter,
3357 struct task_struct *parent,
3358 struct perf_counter_context *parent_ctx,
3359 struct task_struct *child,
3360 struct perf_counter_context *child_ctx)
3362 struct perf_counter *leader;
3363 struct perf_counter *sub;
3364 struct perf_counter *child_ctr;
3366 leader = inherit_counter(parent_counter, parent, parent_ctx,
3367 child, NULL, child_ctx);
3369 return PTR_ERR(leader);
3370 list_for_each_entry(sub, &parent_counter->sibling_list, list_entry) {
3371 child_ctr = inherit_counter(sub, parent, parent_ctx,
3372 child, leader, child_ctx);
3373 if (IS_ERR(child_ctr))
3374 return PTR_ERR(child_ctr);
3379 static void sync_child_counter(struct perf_counter *child_counter,
3380 struct perf_counter *parent_counter)
3384 child_val = atomic64_read(&child_counter->count);
3387 * Add back the child's count to the parent's count:
3389 atomic64_add(child_val, &parent_counter->count);
3390 atomic64_add(child_counter->total_time_enabled,
3391 &parent_counter->child_total_time_enabled);
3392 atomic64_add(child_counter->total_time_running,
3393 &parent_counter->child_total_time_running);
3396 * Remove this counter from the parent's list
3398 mutex_lock(&parent_counter->child_mutex);
3399 list_del_init(&child_counter->child_list);
3400 mutex_unlock(&parent_counter->child_mutex);
3403 * Release the parent counter, if this was the last
3406 fput(parent_counter->filp);
3410 __perf_counter_exit_task(struct task_struct *child,
3411 struct perf_counter *child_counter,
3412 struct perf_counter_context *child_ctx)
3414 struct perf_counter *parent_counter;
3416 update_counter_times(child_counter);
3417 list_del_counter(child_counter, child_ctx);
3419 parent_counter = child_counter->parent;
3421 * It can happen that parent exits first, and has counters
3422 * that are still around due to the child reference. These
3423 * counters need to be zapped - but otherwise linger.
3425 if (parent_counter) {
3426 sync_child_counter(child_counter, parent_counter);
3427 free_counter(child_counter);
3432 * When a child task exits, feed back counter values to parent counters.
3434 * Note: we may be running in child context, but the PID is not hashed
3435 * anymore so new counters will not be added.
3436 * (XXX not sure that is true when we get called from flush_old_exec.
3439 void perf_counter_exit_task(struct task_struct *child)
3441 struct perf_counter *child_counter, *tmp;
3442 struct perf_counter_context *child_ctx;
3443 unsigned long flags;
3445 WARN_ON_ONCE(child != current);
3447 child_ctx = child->perf_counter_ctxp;
3449 if (likely(!child_ctx))
3452 local_irq_save(flags);
3453 __perf_counter_task_sched_out(child_ctx);
3454 child->perf_counter_ctxp = NULL;
3455 local_irq_restore(flags);
3457 mutex_lock(&child_ctx->mutex);
3460 list_for_each_entry_safe(child_counter, tmp, &child_ctx->counter_list,
3462 __perf_counter_exit_task(child, child_counter, child_ctx);
3465 * If the last counter was a group counter, it will have appended all
3466 * its siblings to the list, but we obtained 'tmp' before that which
3467 * will still point to the list head terminating the iteration.
3469 if (!list_empty(&child_ctx->counter_list))
3472 mutex_unlock(&child_ctx->mutex);
3478 * Initialize the perf_counter context in task_struct
3480 void perf_counter_init_task(struct task_struct *child)
3482 struct perf_counter_context *child_ctx, *parent_ctx;
3483 struct perf_counter *counter;
3484 struct task_struct *parent = current;
3485 int inherited_all = 1;
3487 child->perf_counter_ctxp = NULL;
3490 * This is executed from the parent task context, so inherit
3491 * counters that have been marked for cloning.
3492 * First allocate and initialize a context for the child.
3495 child_ctx = kmalloc(sizeof(struct perf_counter_context), GFP_KERNEL);
3499 parent_ctx = parent->perf_counter_ctxp;
3500 if (likely(!parent_ctx || !parent_ctx->nr_counters))
3503 __perf_counter_init_context(child_ctx, child);
3504 child->perf_counter_ctxp = child_ctx;
3507 * Lock the parent list. No need to lock the child - not PID
3508 * hashed yet and not running, so nobody can access it.
3510 mutex_lock(&parent_ctx->mutex);
3513 * We dont have to disable NMIs - we are only looking at
3514 * the list, not manipulating it:
3516 list_for_each_entry_rcu(counter, &parent_ctx->event_list, event_entry) {
3517 if (counter != counter->group_leader)
3520 if (!counter->hw_event.inherit) {
3525 if (inherit_group(counter, parent,
3526 parent_ctx, child, child_ctx)) {
3532 if (inherited_all) {
3534 * Mark the child context as a clone of the parent
3535 * context, or of whatever the parent is a clone of.
3537 if (parent_ctx->parent_ctx) {
3538 child_ctx->parent_ctx = parent_ctx->parent_ctx;
3539 child_ctx->parent_gen = parent_ctx->parent_gen;
3541 child_ctx->parent_ctx = parent_ctx;
3542 child_ctx->parent_gen = parent_ctx->generation;
3544 get_ctx(child_ctx->parent_ctx);
3547 mutex_unlock(&parent_ctx->mutex);
3550 static void __cpuinit perf_counter_init_cpu(int cpu)
3552 struct perf_cpu_context *cpuctx;
3554 cpuctx = &per_cpu(perf_cpu_context, cpu);
3555 __perf_counter_init_context(&cpuctx->ctx, NULL);
3557 spin_lock(&perf_resource_lock);
3558 cpuctx->max_pertask = perf_max_counters - perf_reserved_percpu;
3559 spin_unlock(&perf_resource_lock);
3561 hw_perf_counter_setup(cpu);
3564 #ifdef CONFIG_HOTPLUG_CPU
3565 static void __perf_counter_exit_cpu(void *info)
3567 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
3568 struct perf_counter_context *ctx = &cpuctx->ctx;
3569 struct perf_counter *counter, *tmp;
3571 list_for_each_entry_safe(counter, tmp, &ctx->counter_list, list_entry)
3572 __perf_counter_remove_from_context(counter);
3574 static void perf_counter_exit_cpu(int cpu)
3576 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
3577 struct perf_counter_context *ctx = &cpuctx->ctx;
3579 mutex_lock(&ctx->mutex);
3580 smp_call_function_single(cpu, __perf_counter_exit_cpu, NULL, 1);
3581 mutex_unlock(&ctx->mutex);
3584 static inline void perf_counter_exit_cpu(int cpu) { }
3587 static int __cpuinit
3588 perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
3590 unsigned int cpu = (long)hcpu;
3594 case CPU_UP_PREPARE:
3595 case CPU_UP_PREPARE_FROZEN:
3596 perf_counter_init_cpu(cpu);
3599 case CPU_DOWN_PREPARE:
3600 case CPU_DOWN_PREPARE_FROZEN:
3601 perf_counter_exit_cpu(cpu);
3611 static struct notifier_block __cpuinitdata perf_cpu_nb = {
3612 .notifier_call = perf_cpu_notify,
3615 void __init perf_counter_init(void)
3617 perf_cpu_notify(&perf_cpu_nb, (unsigned long)CPU_UP_PREPARE,
3618 (void *)(long)smp_processor_id());
3619 register_cpu_notifier(&perf_cpu_nb);
3622 static ssize_t perf_show_reserve_percpu(struct sysdev_class *class, char *buf)
3624 return sprintf(buf, "%d\n", perf_reserved_percpu);
3628 perf_set_reserve_percpu(struct sysdev_class *class,
3632 struct perf_cpu_context *cpuctx;
3636 err = strict_strtoul(buf, 10, &val);
3639 if (val > perf_max_counters)
3642 spin_lock(&perf_resource_lock);
3643 perf_reserved_percpu = val;
3644 for_each_online_cpu(cpu) {
3645 cpuctx = &per_cpu(perf_cpu_context, cpu);
3646 spin_lock_irq(&cpuctx->ctx.lock);
3647 mpt = min(perf_max_counters - cpuctx->ctx.nr_counters,
3648 perf_max_counters - perf_reserved_percpu);
3649 cpuctx->max_pertask = mpt;
3650 spin_unlock_irq(&cpuctx->ctx.lock);
3652 spin_unlock(&perf_resource_lock);
3657 static ssize_t perf_show_overcommit(struct sysdev_class *class, char *buf)
3659 return sprintf(buf, "%d\n", perf_overcommit);
3663 perf_set_overcommit(struct sysdev_class *class, const char *buf, size_t count)
3668 err = strict_strtoul(buf, 10, &val);
3674 spin_lock(&perf_resource_lock);
3675 perf_overcommit = val;
3676 spin_unlock(&perf_resource_lock);
3681 static SYSDEV_CLASS_ATTR(
3684 perf_show_reserve_percpu,
3685 perf_set_reserve_percpu
3688 static SYSDEV_CLASS_ATTR(
3691 perf_show_overcommit,
3695 static struct attribute *perfclass_attrs[] = {
3696 &attr_reserve_percpu.attr,
3697 &attr_overcommit.attr,
3701 static struct attribute_group perfclass_attr_group = {
3702 .attrs = perfclass_attrs,
3703 .name = "perf_counters",
3706 static int __init perf_counter_sysfs_init(void)
3708 return sysfs_create_group(&cpu_sysdev_class.kset.kobj,
3709 &perfclass_attr_group);
3711 device_initcall(perf_counter_sysfs_init);