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);
140 * Remove a counter from the lists for its context.
141 * Must be called with ctx->mutex and ctx->lock held.
144 list_del_counter(struct perf_counter *counter, struct perf_counter_context *ctx)
146 struct perf_counter *sibling, *tmp;
148 if (list_empty(&counter->list_entry))
152 list_del_init(&counter->list_entry);
153 list_del_rcu(&counter->event_entry);
155 if (counter->group_leader != counter)
156 counter->group_leader->nr_siblings--;
159 * If this was a group counter with sibling counters then
160 * upgrade the siblings to singleton counters by adding them
161 * to the context list directly:
163 list_for_each_entry_safe(sibling, tmp,
164 &counter->sibling_list, list_entry) {
166 list_move_tail(&sibling->list_entry, &ctx->counter_list);
167 sibling->group_leader = sibling;
172 counter_sched_out(struct perf_counter *counter,
173 struct perf_cpu_context *cpuctx,
174 struct perf_counter_context *ctx)
176 if (counter->state != PERF_COUNTER_STATE_ACTIVE)
179 counter->state = PERF_COUNTER_STATE_INACTIVE;
180 counter->tstamp_stopped = ctx->time;
181 counter->pmu->disable(counter);
184 if (!is_software_counter(counter))
185 cpuctx->active_oncpu--;
187 if (counter->hw_event.exclusive || !cpuctx->active_oncpu)
188 cpuctx->exclusive = 0;
192 group_sched_out(struct perf_counter *group_counter,
193 struct perf_cpu_context *cpuctx,
194 struct perf_counter_context *ctx)
196 struct perf_counter *counter;
198 if (group_counter->state != PERF_COUNTER_STATE_ACTIVE)
201 counter_sched_out(group_counter, cpuctx, ctx);
204 * Schedule out siblings (if any):
206 list_for_each_entry(counter, &group_counter->sibling_list, list_entry)
207 counter_sched_out(counter, cpuctx, ctx);
209 if (group_counter->hw_event.exclusive)
210 cpuctx->exclusive = 0;
214 * Mark this context as not being a clone of another.
215 * Called when counters are added to or removed from this context.
216 * We also increment our generation number so that anything that
217 * was cloned from this context before this will not match anything
218 * cloned from this context after this.
220 static void unclone_ctx(struct perf_counter_context *ctx)
223 if (!ctx->parent_ctx)
225 put_ctx(ctx->parent_ctx);
226 ctx->parent_ctx = NULL;
230 * Cross CPU call to remove a performance counter
232 * We disable the counter on the hardware level first. After that we
233 * remove it from the context list.
235 static void __perf_counter_remove_from_context(void *info)
237 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
238 struct perf_counter *counter = info;
239 struct perf_counter_context *ctx = counter->ctx;
243 * If this is a task context, we need to check whether it is
244 * the current task context of this cpu. If not it has been
245 * scheduled out before the smp call arrived.
247 if (ctx->task && cpuctx->task_ctx != ctx)
250 spin_lock_irqsave(&ctx->lock, flags);
252 * Protect the list operation against NMI by disabling the
253 * counters on a global level.
257 counter_sched_out(counter, cpuctx, ctx);
259 list_del_counter(counter, ctx);
263 * Allow more per task counters with respect to the
266 cpuctx->max_pertask =
267 min(perf_max_counters - ctx->nr_counters,
268 perf_max_counters - perf_reserved_percpu);
272 spin_unlock_irqrestore(&ctx->lock, flags);
277 * Remove the counter from a task's (or a CPU's) list of counters.
279 * Must be called with ctx->mutex held.
281 * CPU counters are removed with a smp call. For task counters we only
282 * call when the task is on a CPU.
284 static void perf_counter_remove_from_context(struct perf_counter *counter)
286 struct perf_counter_context *ctx = counter->ctx;
287 struct task_struct *task = ctx->task;
292 * Per cpu counters are removed via an smp call and
293 * the removal is always sucessful.
295 smp_call_function_single(counter->cpu,
296 __perf_counter_remove_from_context,
302 task_oncpu_function_call(task, __perf_counter_remove_from_context,
305 spin_lock_irq(&ctx->lock);
307 * If the context is active we need to retry the smp call.
309 if (ctx->nr_active && !list_empty(&counter->list_entry)) {
310 spin_unlock_irq(&ctx->lock);
315 * The lock prevents that this context is scheduled in so we
316 * can remove the counter safely, if the call above did not
319 if (!list_empty(&counter->list_entry)) {
320 list_del_counter(counter, ctx);
322 spin_unlock_irq(&ctx->lock);
325 static inline u64 perf_clock(void)
327 return cpu_clock(smp_processor_id());
331 * Update the record of the current time in a context.
333 static void update_context_time(struct perf_counter_context *ctx)
335 u64 now = perf_clock();
337 ctx->time += now - ctx->timestamp;
338 ctx->timestamp = now;
342 * Update the total_time_enabled and total_time_running fields for a counter.
344 static void update_counter_times(struct perf_counter *counter)
346 struct perf_counter_context *ctx = counter->ctx;
349 if (counter->state < PERF_COUNTER_STATE_INACTIVE)
352 counter->total_time_enabled = ctx->time - counter->tstamp_enabled;
354 if (counter->state == PERF_COUNTER_STATE_INACTIVE)
355 run_end = counter->tstamp_stopped;
359 counter->total_time_running = run_end - counter->tstamp_running;
363 * Update total_time_enabled and total_time_running for all counters in a group.
365 static void update_group_times(struct perf_counter *leader)
367 struct perf_counter *counter;
369 update_counter_times(leader);
370 list_for_each_entry(counter, &leader->sibling_list, list_entry)
371 update_counter_times(counter);
375 * Cross CPU call to disable a performance counter
377 static void __perf_counter_disable(void *info)
379 struct perf_counter *counter = info;
380 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
381 struct perf_counter_context *ctx = counter->ctx;
385 * If this is a per-task counter, need to check whether this
386 * counter's task is the current task on this cpu.
388 if (ctx->task && cpuctx->task_ctx != ctx)
391 spin_lock_irqsave(&ctx->lock, flags);
394 * If the counter is on, turn it off.
395 * If it is in error state, leave it in error state.
397 if (counter->state >= PERF_COUNTER_STATE_INACTIVE) {
398 update_context_time(ctx);
399 update_counter_times(counter);
400 if (counter == counter->group_leader)
401 group_sched_out(counter, cpuctx, ctx);
403 counter_sched_out(counter, cpuctx, ctx);
404 counter->state = PERF_COUNTER_STATE_OFF;
407 spin_unlock_irqrestore(&ctx->lock, flags);
413 static void perf_counter_disable(struct perf_counter *counter)
415 struct perf_counter_context *ctx = counter->ctx;
416 struct task_struct *task = ctx->task;
420 * Disable the counter on the cpu that it's on
422 smp_call_function_single(counter->cpu, __perf_counter_disable,
428 task_oncpu_function_call(task, __perf_counter_disable, counter);
430 spin_lock_irq(&ctx->lock);
432 * If the counter is still active, we need to retry the cross-call.
434 if (counter->state == PERF_COUNTER_STATE_ACTIVE) {
435 spin_unlock_irq(&ctx->lock);
440 * Since we have the lock this context can't be scheduled
441 * in, so we can change the state safely.
443 if (counter->state == PERF_COUNTER_STATE_INACTIVE) {
444 update_counter_times(counter);
445 counter->state = PERF_COUNTER_STATE_OFF;
448 spin_unlock_irq(&ctx->lock);
452 counter_sched_in(struct perf_counter *counter,
453 struct perf_cpu_context *cpuctx,
454 struct perf_counter_context *ctx,
457 if (counter->state <= PERF_COUNTER_STATE_OFF)
460 counter->state = PERF_COUNTER_STATE_ACTIVE;
461 counter->oncpu = cpu; /* TODO: put 'cpu' into cpuctx->cpu */
463 * The new state must be visible before we turn it on in the hardware:
467 if (counter->pmu->enable(counter)) {
468 counter->state = PERF_COUNTER_STATE_INACTIVE;
473 counter->tstamp_running += ctx->time - counter->tstamp_stopped;
475 if (!is_software_counter(counter))
476 cpuctx->active_oncpu++;
479 if (counter->hw_event.exclusive)
480 cpuctx->exclusive = 1;
486 group_sched_in(struct perf_counter *group_counter,
487 struct perf_cpu_context *cpuctx,
488 struct perf_counter_context *ctx,
491 struct perf_counter *counter, *partial_group;
494 if (group_counter->state == PERF_COUNTER_STATE_OFF)
497 ret = hw_perf_group_sched_in(group_counter, cpuctx, ctx, cpu);
499 return ret < 0 ? ret : 0;
501 group_counter->prev_state = group_counter->state;
502 if (counter_sched_in(group_counter, cpuctx, ctx, cpu))
506 * Schedule in siblings as one group (if any):
508 list_for_each_entry(counter, &group_counter->sibling_list, list_entry) {
509 counter->prev_state = counter->state;
510 if (counter_sched_in(counter, cpuctx, ctx, cpu)) {
511 partial_group = counter;
520 * Groups can be scheduled in as one unit only, so undo any
521 * partial group before returning:
523 list_for_each_entry(counter, &group_counter->sibling_list, list_entry) {
524 if (counter == partial_group)
526 counter_sched_out(counter, cpuctx, ctx);
528 counter_sched_out(group_counter, cpuctx, ctx);
534 * Return 1 for a group consisting entirely of software counters,
535 * 0 if the group contains any hardware counters.
537 static int is_software_only_group(struct perf_counter *leader)
539 struct perf_counter *counter;
541 if (!is_software_counter(leader))
544 list_for_each_entry(counter, &leader->sibling_list, list_entry)
545 if (!is_software_counter(counter))
552 * Work out whether we can put this counter group on the CPU now.
554 static int group_can_go_on(struct perf_counter *counter,
555 struct perf_cpu_context *cpuctx,
559 * Groups consisting entirely of software counters can always go on.
561 if (is_software_only_group(counter))
564 * If an exclusive group is already on, no other hardware
565 * counters can go on.
567 if (cpuctx->exclusive)
570 * If this group is exclusive and there are already
571 * counters on the CPU, it can't go on.
573 if (counter->hw_event.exclusive && cpuctx->active_oncpu)
576 * Otherwise, try to add it if all previous groups were able
582 static void add_counter_to_ctx(struct perf_counter *counter,
583 struct perf_counter_context *ctx)
585 list_add_counter(counter, ctx);
586 counter->prev_state = PERF_COUNTER_STATE_OFF;
587 counter->tstamp_enabled = ctx->time;
588 counter->tstamp_running = ctx->time;
589 counter->tstamp_stopped = ctx->time;
593 * Cross CPU call to install and enable a performance counter
595 * Must be called with ctx->mutex held
597 static void __perf_install_in_context(void *info)
599 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
600 struct perf_counter *counter = info;
601 struct perf_counter_context *ctx = counter->ctx;
602 struct perf_counter *leader = counter->group_leader;
603 int cpu = smp_processor_id();
608 * If this is a task context, we need to check whether it is
609 * the current task context of this cpu. If not it has been
610 * scheduled out before the smp call arrived.
611 * Or possibly this is the right context but it isn't
612 * on this cpu because it had no counters.
614 if (ctx->task && cpuctx->task_ctx != ctx) {
615 if (cpuctx->task_ctx || ctx->task != current)
617 cpuctx->task_ctx = ctx;
620 spin_lock_irqsave(&ctx->lock, flags);
622 update_context_time(ctx);
625 * Protect the list operation against NMI by disabling the
626 * counters on a global level. NOP for non NMI based counters.
630 add_counter_to_ctx(counter, ctx);
633 * Don't put the counter on if it is disabled or if
634 * it is in a group and the group isn't on.
636 if (counter->state != PERF_COUNTER_STATE_INACTIVE ||
637 (leader != counter && leader->state != PERF_COUNTER_STATE_ACTIVE))
641 * An exclusive counter can't go on if there are already active
642 * hardware counters, and no hardware counter can go on if there
643 * is already an exclusive counter on.
645 if (!group_can_go_on(counter, cpuctx, 1))
648 err = counter_sched_in(counter, cpuctx, ctx, cpu);
652 * This counter couldn't go on. If it is in a group
653 * then we have to pull the whole group off.
654 * If the counter group is pinned then put it in error state.
656 if (leader != counter)
657 group_sched_out(leader, cpuctx, ctx);
658 if (leader->hw_event.pinned) {
659 update_group_times(leader);
660 leader->state = PERF_COUNTER_STATE_ERROR;
664 if (!err && !ctx->task && cpuctx->max_pertask)
665 cpuctx->max_pertask--;
670 spin_unlock_irqrestore(&ctx->lock, flags);
674 * Attach a performance counter to a context
676 * First we add the counter to the list with the hardware enable bit
677 * in counter->hw_config cleared.
679 * If the counter is attached to a task which is on a CPU we use a smp
680 * call to enable it in the task context. The task might have been
681 * scheduled away, but we check this in the smp call again.
683 * Must be called with ctx->mutex held.
686 perf_install_in_context(struct perf_counter_context *ctx,
687 struct perf_counter *counter,
690 struct task_struct *task = ctx->task;
694 * Per cpu counters are installed via an smp call and
695 * the install is always sucessful.
697 smp_call_function_single(cpu, __perf_install_in_context,
703 task_oncpu_function_call(task, __perf_install_in_context,
706 spin_lock_irq(&ctx->lock);
708 * we need to retry the smp call.
710 if (ctx->is_active && list_empty(&counter->list_entry)) {
711 spin_unlock_irq(&ctx->lock);
716 * The lock prevents that this context is scheduled in so we
717 * can add the counter safely, if it the call above did not
720 if (list_empty(&counter->list_entry))
721 add_counter_to_ctx(counter, ctx);
722 spin_unlock_irq(&ctx->lock);
726 * Cross CPU call to enable a performance counter
728 static void __perf_counter_enable(void *info)
730 struct perf_counter *counter = info;
731 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
732 struct perf_counter_context *ctx = counter->ctx;
733 struct perf_counter *leader = counter->group_leader;
738 * If this is a per-task counter, need to check whether this
739 * counter's task is the current task on this cpu.
741 if (ctx->task && cpuctx->task_ctx != ctx) {
742 if (cpuctx->task_ctx || ctx->task != current)
744 cpuctx->task_ctx = ctx;
747 spin_lock_irqsave(&ctx->lock, flags);
749 update_context_time(ctx);
751 counter->prev_state = counter->state;
752 if (counter->state >= PERF_COUNTER_STATE_INACTIVE)
754 counter->state = PERF_COUNTER_STATE_INACTIVE;
755 counter->tstamp_enabled = ctx->time - counter->total_time_enabled;
758 * If the counter is in a group and isn't the group leader,
759 * then don't put it on unless the group is on.
761 if (leader != counter && leader->state != PERF_COUNTER_STATE_ACTIVE)
764 if (!group_can_go_on(counter, cpuctx, 1)) {
768 if (counter == leader)
769 err = group_sched_in(counter, cpuctx, ctx,
772 err = counter_sched_in(counter, cpuctx, ctx,
779 * If this counter can't go on and it's part of a
780 * group, then the whole group has to come off.
782 if (leader != counter)
783 group_sched_out(leader, cpuctx, ctx);
784 if (leader->hw_event.pinned) {
785 update_group_times(leader);
786 leader->state = PERF_COUNTER_STATE_ERROR;
791 spin_unlock_irqrestore(&ctx->lock, flags);
797 static void perf_counter_enable(struct perf_counter *counter)
799 struct perf_counter_context *ctx = counter->ctx;
800 struct task_struct *task = ctx->task;
804 * Enable the counter on the cpu that it's on
806 smp_call_function_single(counter->cpu, __perf_counter_enable,
811 spin_lock_irq(&ctx->lock);
812 if (counter->state >= PERF_COUNTER_STATE_INACTIVE)
816 * If the counter is in error state, clear that first.
817 * That way, if we see the counter in error state below, we
818 * know that it has gone back into error state, as distinct
819 * from the task having been scheduled away before the
820 * cross-call arrived.
822 if (counter->state == PERF_COUNTER_STATE_ERROR)
823 counter->state = PERF_COUNTER_STATE_OFF;
826 spin_unlock_irq(&ctx->lock);
827 task_oncpu_function_call(task, __perf_counter_enable, counter);
829 spin_lock_irq(&ctx->lock);
832 * If the context is active and the counter is still off,
833 * we need to retry the cross-call.
835 if (ctx->is_active && counter->state == PERF_COUNTER_STATE_OFF)
839 * Since we have the lock this context can't be scheduled
840 * in, so we can change the state safely.
842 if (counter->state == PERF_COUNTER_STATE_OFF) {
843 counter->state = PERF_COUNTER_STATE_INACTIVE;
844 counter->tstamp_enabled =
845 ctx->time - counter->total_time_enabled;
848 spin_unlock_irq(&ctx->lock);
851 static int perf_counter_refresh(struct perf_counter *counter, int refresh)
854 * not supported on inherited counters
856 if (counter->hw_event.inherit)
859 atomic_add(refresh, &counter->event_limit);
860 perf_counter_enable(counter);
865 void __perf_counter_sched_out(struct perf_counter_context *ctx,
866 struct perf_cpu_context *cpuctx)
868 struct perf_counter *counter;
870 spin_lock(&ctx->lock);
872 if (likely(!ctx->nr_counters))
874 update_context_time(ctx);
877 if (ctx->nr_active) {
878 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
879 if (counter != counter->group_leader)
880 counter_sched_out(counter, cpuctx, ctx);
882 group_sched_out(counter, cpuctx, ctx);
887 spin_unlock(&ctx->lock);
891 * Test whether two contexts are equivalent, i.e. whether they
892 * have both been cloned from the same version of the same context
893 * and they both have the same number of enabled counters.
894 * If the number of enabled counters is the same, then the set
895 * of enabled counters should be the same, because these are both
896 * inherited contexts, therefore we can't access individual counters
897 * in them directly with an fd; we can only enable/disable all
898 * counters via prctl, or enable/disable all counters in a family
899 * via ioctl, which will have the same effect on both contexts.
901 static int context_equiv(struct perf_counter_context *ctx1,
902 struct perf_counter_context *ctx2)
904 return ctx1->parent_ctx && ctx1->parent_ctx == ctx2->parent_ctx
905 && ctx1->parent_gen == ctx2->parent_gen;
909 * Called from scheduler to remove the counters of the current task,
910 * with interrupts disabled.
912 * We stop each counter and update the counter value in counter->count.
914 * This does not protect us against NMI, but disable()
915 * sets the disabled bit in the control field of counter _before_
916 * accessing the counter control register. If a NMI hits, then it will
917 * not restart the counter.
919 void perf_counter_task_sched_out(struct task_struct *task,
920 struct task_struct *next, int cpu)
922 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
923 struct perf_counter_context *ctx = task->perf_counter_ctxp;
924 struct perf_counter_context *next_ctx;
925 struct pt_regs *regs;
927 if (likely(!ctx || !cpuctx->task_ctx))
930 update_context_time(ctx);
932 regs = task_pt_regs(task);
933 perf_swcounter_event(PERF_COUNT_CONTEXT_SWITCHES, 1, 1, regs, 0);
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 static void perf_log_period(struct perf_counter *counter, u64 period);
1072 static void perf_adjust_freq(struct perf_counter_context *ctx)
1074 struct perf_counter *counter;
1079 spin_lock(&ctx->lock);
1080 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
1081 if (counter->state != PERF_COUNTER_STATE_ACTIVE)
1084 if (!counter->hw_event.freq || !counter->hw_event.irq_freq)
1087 events = HZ * counter->hw.interrupts * counter->hw.irq_period;
1088 period = div64_u64(events, counter->hw_event.irq_freq);
1090 delta = (s64)(1 + period - counter->hw.irq_period);
1093 irq_period = counter->hw.irq_period + delta;
1098 perf_log_period(counter, irq_period);
1100 counter->hw.irq_period = irq_period;
1101 counter->hw.interrupts = 0;
1103 spin_unlock(&ctx->lock);
1107 * Round-robin a context's counters:
1109 static void rotate_ctx(struct perf_counter_context *ctx)
1111 struct perf_counter *counter;
1113 if (!ctx->nr_counters)
1116 spin_lock(&ctx->lock);
1118 * Rotate the first entry last (works just fine for group counters too):
1121 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
1122 list_move_tail(&counter->list_entry, &ctx->counter_list);
1127 spin_unlock(&ctx->lock);
1130 void perf_counter_task_tick(struct task_struct *curr, int cpu)
1132 struct perf_cpu_context *cpuctx;
1133 struct perf_counter_context *ctx;
1135 if (!atomic_read(&nr_counters))
1138 cpuctx = &per_cpu(perf_cpu_context, cpu);
1139 ctx = curr->perf_counter_ctxp;
1141 perf_adjust_freq(&cpuctx->ctx);
1143 perf_adjust_freq(ctx);
1145 perf_counter_cpu_sched_out(cpuctx);
1147 __perf_counter_task_sched_out(ctx);
1149 rotate_ctx(&cpuctx->ctx);
1153 perf_counter_cpu_sched_in(cpuctx, cpu);
1155 perf_counter_task_sched_in(curr, cpu);
1159 * Cross CPU call to read the hardware counter
1161 static void __read(void *info)
1163 struct perf_counter *counter = info;
1164 struct perf_counter_context *ctx = counter->ctx;
1165 unsigned long flags;
1167 local_irq_save(flags);
1169 update_context_time(ctx);
1170 counter->pmu->read(counter);
1171 update_counter_times(counter);
1172 local_irq_restore(flags);
1175 static u64 perf_counter_read(struct perf_counter *counter)
1178 * If counter is enabled and currently active on a CPU, update the
1179 * value in the counter structure:
1181 if (counter->state == PERF_COUNTER_STATE_ACTIVE) {
1182 smp_call_function_single(counter->oncpu,
1183 __read, counter, 1);
1184 } else if (counter->state == PERF_COUNTER_STATE_INACTIVE) {
1185 update_counter_times(counter);
1188 return atomic64_read(&counter->count);
1192 * Initialize the perf_counter context in a task_struct:
1195 __perf_counter_init_context(struct perf_counter_context *ctx,
1196 struct task_struct *task)
1198 memset(ctx, 0, sizeof(*ctx));
1199 spin_lock_init(&ctx->lock);
1200 mutex_init(&ctx->mutex);
1201 INIT_LIST_HEAD(&ctx->counter_list);
1202 INIT_LIST_HEAD(&ctx->event_list);
1203 atomic_set(&ctx->refcount, 1);
1207 static void put_context(struct perf_counter_context *ctx)
1210 put_task_struct(ctx->task);
1213 static struct perf_counter_context *find_get_context(pid_t pid, int cpu)
1215 struct perf_cpu_context *cpuctx;
1216 struct perf_counter_context *ctx;
1217 struct perf_counter_context *tctx;
1218 struct task_struct *task;
1221 * If cpu is not a wildcard then this is a percpu counter:
1224 /* Must be root to operate on a CPU counter: */
1225 if (sysctl_perf_counter_priv && !capable(CAP_SYS_ADMIN))
1226 return ERR_PTR(-EACCES);
1228 if (cpu < 0 || cpu > num_possible_cpus())
1229 return ERR_PTR(-EINVAL);
1232 * We could be clever and allow to attach a counter to an
1233 * offline CPU and activate it when the CPU comes up, but
1236 if (!cpu_isset(cpu, cpu_online_map))
1237 return ERR_PTR(-ENODEV);
1239 cpuctx = &per_cpu(perf_cpu_context, cpu);
1249 task = find_task_by_vpid(pid);
1251 get_task_struct(task);
1255 return ERR_PTR(-ESRCH);
1257 /* Reuse ptrace permission checks for now. */
1258 if (!ptrace_may_access(task, PTRACE_MODE_READ)) {
1259 put_task_struct(task);
1260 return ERR_PTR(-EACCES);
1263 ctx = task->perf_counter_ctxp;
1265 ctx = kmalloc(sizeof(struct perf_counter_context), GFP_KERNEL);
1267 put_task_struct(task);
1268 return ERR_PTR(-ENOMEM);
1270 __perf_counter_init_context(ctx, task);
1272 * Make sure other cpus see correct values for *ctx
1273 * once task->perf_counter_ctxp is visible to them.
1276 tctx = cmpxchg(&task->perf_counter_ctxp, NULL, ctx);
1279 * We raced with some other task; use
1280 * the context they set.
1290 static void free_counter_rcu(struct rcu_head *head)
1292 struct perf_counter *counter;
1294 counter = container_of(head, struct perf_counter, rcu_head);
1295 put_ctx(counter->ctx);
1299 static void perf_pending_sync(struct perf_counter *counter);
1301 static void free_counter(struct perf_counter *counter)
1303 perf_pending_sync(counter);
1305 atomic_dec(&nr_counters);
1306 if (counter->hw_event.mmap)
1307 atomic_dec(&nr_mmap_tracking);
1308 if (counter->hw_event.munmap)
1309 atomic_dec(&nr_munmap_tracking);
1310 if (counter->hw_event.comm)
1311 atomic_dec(&nr_comm_tracking);
1313 if (counter->destroy)
1314 counter->destroy(counter);
1316 call_rcu(&counter->rcu_head, free_counter_rcu);
1320 * Called when the last reference to the file is gone.
1322 static int perf_release(struct inode *inode, struct file *file)
1324 struct perf_counter *counter = file->private_data;
1325 struct perf_counter_context *ctx = counter->ctx;
1327 file->private_data = NULL;
1329 mutex_lock(&ctx->mutex);
1330 perf_counter_remove_from_context(counter);
1331 mutex_unlock(&ctx->mutex);
1333 mutex_lock(&counter->owner->perf_counter_mutex);
1334 list_del_init(&counter->owner_entry);
1335 mutex_unlock(&counter->owner->perf_counter_mutex);
1336 put_task_struct(counter->owner);
1338 free_counter(counter);
1345 * Read the performance counter - simple non blocking version for now
1348 perf_read_hw(struct perf_counter *counter, char __user *buf, size_t count)
1354 * Return end-of-file for a read on a counter that is in
1355 * error state (i.e. because it was pinned but it couldn't be
1356 * scheduled on to the CPU at some point).
1358 if (counter->state == PERF_COUNTER_STATE_ERROR)
1361 mutex_lock(&counter->child_mutex);
1362 values[0] = perf_counter_read(counter);
1364 if (counter->hw_event.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
1365 values[n++] = counter->total_time_enabled +
1366 atomic64_read(&counter->child_total_time_enabled);
1367 if (counter->hw_event.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
1368 values[n++] = counter->total_time_running +
1369 atomic64_read(&counter->child_total_time_running);
1370 mutex_unlock(&counter->child_mutex);
1372 if (count < n * sizeof(u64))
1374 count = n * sizeof(u64);
1376 if (copy_to_user(buf, values, count))
1383 perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
1385 struct perf_counter *counter = file->private_data;
1387 return perf_read_hw(counter, buf, count);
1390 static unsigned int perf_poll(struct file *file, poll_table *wait)
1392 struct perf_counter *counter = file->private_data;
1393 struct perf_mmap_data *data;
1394 unsigned int events = POLL_HUP;
1397 data = rcu_dereference(counter->data);
1399 events = atomic_xchg(&data->poll, 0);
1402 poll_wait(file, &counter->waitq, wait);
1407 static void perf_counter_reset(struct perf_counter *counter)
1409 (void)perf_counter_read(counter);
1410 atomic64_set(&counter->count, 0);
1411 perf_counter_update_userpage(counter);
1414 static void perf_counter_for_each_sibling(struct perf_counter *counter,
1415 void (*func)(struct perf_counter *))
1417 struct perf_counter_context *ctx = counter->ctx;
1418 struct perf_counter *sibling;
1420 mutex_lock(&ctx->mutex);
1421 counter = counter->group_leader;
1424 list_for_each_entry(sibling, &counter->sibling_list, list_entry)
1426 mutex_unlock(&ctx->mutex);
1429 static void perf_counter_for_each_child(struct perf_counter *counter,
1430 void (*func)(struct perf_counter *))
1432 struct perf_counter *child;
1434 mutex_lock(&counter->child_mutex);
1436 list_for_each_entry(child, &counter->child_list, child_list)
1438 mutex_unlock(&counter->child_mutex);
1441 static void perf_counter_for_each(struct perf_counter *counter,
1442 void (*func)(struct perf_counter *))
1444 struct perf_counter *child;
1446 mutex_lock(&counter->child_mutex);
1447 perf_counter_for_each_sibling(counter, func);
1448 list_for_each_entry(child, &counter->child_list, child_list)
1449 perf_counter_for_each_sibling(child, func);
1450 mutex_unlock(&counter->child_mutex);
1453 static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1455 struct perf_counter *counter = file->private_data;
1456 void (*func)(struct perf_counter *);
1460 case PERF_COUNTER_IOC_ENABLE:
1461 func = perf_counter_enable;
1463 case PERF_COUNTER_IOC_DISABLE:
1464 func = perf_counter_disable;
1466 case PERF_COUNTER_IOC_RESET:
1467 func = perf_counter_reset;
1470 case PERF_COUNTER_IOC_REFRESH:
1471 return perf_counter_refresh(counter, arg);
1476 if (flags & PERF_IOC_FLAG_GROUP)
1477 perf_counter_for_each(counter, func);
1479 perf_counter_for_each_child(counter, func);
1484 int perf_counter_task_enable(void)
1486 struct perf_counter *counter;
1488 mutex_lock(¤t->perf_counter_mutex);
1489 list_for_each_entry(counter, ¤t->perf_counter_list, owner_entry)
1490 perf_counter_for_each_child(counter, perf_counter_enable);
1491 mutex_unlock(¤t->perf_counter_mutex);
1496 int perf_counter_task_disable(void)
1498 struct perf_counter *counter;
1500 mutex_lock(¤t->perf_counter_mutex);
1501 list_for_each_entry(counter, ¤t->perf_counter_list, owner_entry)
1502 perf_counter_for_each_child(counter, perf_counter_disable);
1503 mutex_unlock(¤t->perf_counter_mutex);
1509 * Callers need to ensure there can be no nesting of this function, otherwise
1510 * the seqlock logic goes bad. We can not serialize this because the arch
1511 * code calls this from NMI context.
1513 void perf_counter_update_userpage(struct perf_counter *counter)
1515 struct perf_mmap_data *data;
1516 struct perf_counter_mmap_page *userpg;
1519 data = rcu_dereference(counter->data);
1523 userpg = data->user_page;
1526 * Disable preemption so as to not let the corresponding user-space
1527 * spin too long if we get preempted.
1532 userpg->index = counter->hw.idx;
1533 userpg->offset = atomic64_read(&counter->count);
1534 if (counter->state == PERF_COUNTER_STATE_ACTIVE)
1535 userpg->offset -= atomic64_read(&counter->hw.prev_count);
1544 static int perf_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1546 struct perf_counter *counter = vma->vm_file->private_data;
1547 struct perf_mmap_data *data;
1548 int ret = VM_FAULT_SIGBUS;
1551 data = rcu_dereference(counter->data);
1555 if (vmf->pgoff == 0) {
1556 vmf->page = virt_to_page(data->user_page);
1558 int nr = vmf->pgoff - 1;
1560 if ((unsigned)nr > data->nr_pages)
1563 vmf->page = virt_to_page(data->data_pages[nr]);
1565 get_page(vmf->page);
1573 static int perf_mmap_data_alloc(struct perf_counter *counter, int nr_pages)
1575 struct perf_mmap_data *data;
1579 WARN_ON(atomic_read(&counter->mmap_count));
1581 size = sizeof(struct perf_mmap_data);
1582 size += nr_pages * sizeof(void *);
1584 data = kzalloc(size, GFP_KERNEL);
1588 data->user_page = (void *)get_zeroed_page(GFP_KERNEL);
1589 if (!data->user_page)
1590 goto fail_user_page;
1592 for (i = 0; i < nr_pages; i++) {
1593 data->data_pages[i] = (void *)get_zeroed_page(GFP_KERNEL);
1594 if (!data->data_pages[i])
1595 goto fail_data_pages;
1598 data->nr_pages = nr_pages;
1599 atomic_set(&data->lock, -1);
1601 rcu_assign_pointer(counter->data, data);
1606 for (i--; i >= 0; i--)
1607 free_page((unsigned long)data->data_pages[i]);
1609 free_page((unsigned long)data->user_page);
1618 static void __perf_mmap_data_free(struct rcu_head *rcu_head)
1620 struct perf_mmap_data *data = container_of(rcu_head,
1621 struct perf_mmap_data, rcu_head);
1624 free_page((unsigned long)data->user_page);
1625 for (i = 0; i < data->nr_pages; i++)
1626 free_page((unsigned long)data->data_pages[i]);
1630 static void perf_mmap_data_free(struct perf_counter *counter)
1632 struct perf_mmap_data *data = counter->data;
1634 WARN_ON(atomic_read(&counter->mmap_count));
1636 rcu_assign_pointer(counter->data, NULL);
1637 call_rcu(&data->rcu_head, __perf_mmap_data_free);
1640 static void perf_mmap_open(struct vm_area_struct *vma)
1642 struct perf_counter *counter = vma->vm_file->private_data;
1644 atomic_inc(&counter->mmap_count);
1647 static void perf_mmap_close(struct vm_area_struct *vma)
1649 struct perf_counter *counter = vma->vm_file->private_data;
1651 if (atomic_dec_and_mutex_lock(&counter->mmap_count,
1652 &counter->mmap_mutex)) {
1653 struct user_struct *user = current_user();
1655 atomic_long_sub(counter->data->nr_pages + 1, &user->locked_vm);
1656 vma->vm_mm->locked_vm -= counter->data->nr_locked;
1657 perf_mmap_data_free(counter);
1658 mutex_unlock(&counter->mmap_mutex);
1662 static struct vm_operations_struct perf_mmap_vmops = {
1663 .open = perf_mmap_open,
1664 .close = perf_mmap_close,
1665 .fault = perf_mmap_fault,
1668 static int perf_mmap(struct file *file, struct vm_area_struct *vma)
1670 struct perf_counter *counter = file->private_data;
1671 struct user_struct *user = current_user();
1672 unsigned long vma_size;
1673 unsigned long nr_pages;
1674 unsigned long user_locked, user_lock_limit;
1675 unsigned long locked, lock_limit;
1676 long user_extra, extra;
1679 if (!(vma->vm_flags & VM_SHARED) || (vma->vm_flags & VM_WRITE))
1682 vma_size = vma->vm_end - vma->vm_start;
1683 nr_pages = (vma_size / PAGE_SIZE) - 1;
1686 * If we have data pages ensure they're a power-of-two number, so we
1687 * can do bitmasks instead of modulo.
1689 if (nr_pages != 0 && !is_power_of_2(nr_pages))
1692 if (vma_size != PAGE_SIZE * (1 + nr_pages))
1695 if (vma->vm_pgoff != 0)
1698 mutex_lock(&counter->mmap_mutex);
1699 if (atomic_inc_not_zero(&counter->mmap_count)) {
1700 if (nr_pages != counter->data->nr_pages)
1705 user_extra = nr_pages + 1;
1706 user_lock_limit = sysctl_perf_counter_mlock >> (PAGE_SHIFT - 10);
1709 * Increase the limit linearly with more CPUs:
1711 user_lock_limit *= num_online_cpus();
1713 user_locked = atomic_long_read(&user->locked_vm) + user_extra;
1716 if (user_locked > user_lock_limit)
1717 extra = user_locked - user_lock_limit;
1719 lock_limit = current->signal->rlim[RLIMIT_MEMLOCK].rlim_cur;
1720 lock_limit >>= PAGE_SHIFT;
1721 locked = vma->vm_mm->locked_vm + extra;
1723 if ((locked > lock_limit) && !capable(CAP_IPC_LOCK)) {
1728 WARN_ON(counter->data);
1729 ret = perf_mmap_data_alloc(counter, nr_pages);
1733 atomic_set(&counter->mmap_count, 1);
1734 atomic_long_add(user_extra, &user->locked_vm);
1735 vma->vm_mm->locked_vm += extra;
1736 counter->data->nr_locked = extra;
1738 mutex_unlock(&counter->mmap_mutex);
1740 vma->vm_flags &= ~VM_MAYWRITE;
1741 vma->vm_flags |= VM_RESERVED;
1742 vma->vm_ops = &perf_mmap_vmops;
1747 static int perf_fasync(int fd, struct file *filp, int on)
1749 struct perf_counter *counter = filp->private_data;
1750 struct inode *inode = filp->f_path.dentry->d_inode;
1753 mutex_lock(&inode->i_mutex);
1754 retval = fasync_helper(fd, filp, on, &counter->fasync);
1755 mutex_unlock(&inode->i_mutex);
1763 static const struct file_operations perf_fops = {
1764 .release = perf_release,
1767 .unlocked_ioctl = perf_ioctl,
1768 .compat_ioctl = perf_ioctl,
1770 .fasync = perf_fasync,
1774 * Perf counter wakeup
1776 * If there's data, ensure we set the poll() state and publish everything
1777 * to user-space before waking everybody up.
1780 void perf_counter_wakeup(struct perf_counter *counter)
1782 wake_up_all(&counter->waitq);
1784 if (counter->pending_kill) {
1785 kill_fasync(&counter->fasync, SIGIO, counter->pending_kill);
1786 counter->pending_kill = 0;
1793 * Handle the case where we need to wakeup up from NMI (or rq->lock) context.
1795 * The NMI bit means we cannot possibly take locks. Therefore, maintain a
1796 * single linked list and use cmpxchg() to add entries lockless.
1799 static void perf_pending_counter(struct perf_pending_entry *entry)
1801 struct perf_counter *counter = container_of(entry,
1802 struct perf_counter, pending);
1804 if (counter->pending_disable) {
1805 counter->pending_disable = 0;
1806 perf_counter_disable(counter);
1809 if (counter->pending_wakeup) {
1810 counter->pending_wakeup = 0;
1811 perf_counter_wakeup(counter);
1815 #define PENDING_TAIL ((struct perf_pending_entry *)-1UL)
1817 static DEFINE_PER_CPU(struct perf_pending_entry *, perf_pending_head) = {
1821 static void perf_pending_queue(struct perf_pending_entry *entry,
1822 void (*func)(struct perf_pending_entry *))
1824 struct perf_pending_entry **head;
1826 if (cmpxchg(&entry->next, NULL, PENDING_TAIL) != NULL)
1831 head = &get_cpu_var(perf_pending_head);
1834 entry->next = *head;
1835 } while (cmpxchg(head, entry->next, entry) != entry->next);
1837 set_perf_counter_pending();
1839 put_cpu_var(perf_pending_head);
1842 static int __perf_pending_run(void)
1844 struct perf_pending_entry *list;
1847 list = xchg(&__get_cpu_var(perf_pending_head), PENDING_TAIL);
1848 while (list != PENDING_TAIL) {
1849 void (*func)(struct perf_pending_entry *);
1850 struct perf_pending_entry *entry = list;
1857 * Ensure we observe the unqueue before we issue the wakeup,
1858 * so that we won't be waiting forever.
1859 * -- see perf_not_pending().
1870 static inline int perf_not_pending(struct perf_counter *counter)
1873 * If we flush on whatever cpu we run, there is a chance we don't
1877 __perf_pending_run();
1881 * Ensure we see the proper queue state before going to sleep
1882 * so that we do not miss the wakeup. -- see perf_pending_handle()
1885 return counter->pending.next == NULL;
1888 static void perf_pending_sync(struct perf_counter *counter)
1890 wait_event(counter->waitq, perf_not_pending(counter));
1893 void perf_counter_do_pending(void)
1895 __perf_pending_run();
1899 * Callchain support -- arch specific
1902 __weak struct perf_callchain_entry *perf_callchain(struct pt_regs *regs)
1911 struct perf_output_handle {
1912 struct perf_counter *counter;
1913 struct perf_mmap_data *data;
1914 unsigned int offset;
1919 unsigned long flags;
1922 static void perf_output_wakeup(struct perf_output_handle *handle)
1924 atomic_set(&handle->data->poll, POLL_IN);
1927 handle->counter->pending_wakeup = 1;
1928 perf_pending_queue(&handle->counter->pending,
1929 perf_pending_counter);
1931 perf_counter_wakeup(handle->counter);
1935 * Curious locking construct.
1937 * We need to ensure a later event doesn't publish a head when a former
1938 * event isn't done writing. However since we need to deal with NMIs we
1939 * cannot fully serialize things.
1941 * What we do is serialize between CPUs so we only have to deal with NMI
1942 * nesting on a single CPU.
1944 * We only publish the head (and generate a wakeup) when the outer-most
1947 static void perf_output_lock(struct perf_output_handle *handle)
1949 struct perf_mmap_data *data = handle->data;
1954 local_irq_save(handle->flags);
1955 cpu = smp_processor_id();
1957 if (in_nmi() && atomic_read(&data->lock) == cpu)
1960 while (atomic_cmpxchg(&data->lock, -1, cpu) != -1)
1966 static void perf_output_unlock(struct perf_output_handle *handle)
1968 struct perf_mmap_data *data = handle->data;
1971 data->done_head = data->head;
1973 if (!handle->locked)
1978 * The xchg implies a full barrier that ensures all writes are done
1979 * before we publish the new head, matched by a rmb() in userspace when
1980 * reading this position.
1982 while ((head = atomic_xchg(&data->done_head, 0)))
1983 data->user_page->data_head = head;
1986 * NMI can happen here, which means we can miss a done_head update.
1989 cpu = atomic_xchg(&data->lock, -1);
1990 WARN_ON_ONCE(cpu != smp_processor_id());
1993 * Therefore we have to validate we did not indeed do so.
1995 if (unlikely(atomic_read(&data->done_head))) {
1997 * Since we had it locked, we can lock it again.
1999 while (atomic_cmpxchg(&data->lock, -1, cpu) != -1)
2005 if (atomic_xchg(&data->wakeup, 0))
2006 perf_output_wakeup(handle);
2008 local_irq_restore(handle->flags);
2011 static int perf_output_begin(struct perf_output_handle *handle,
2012 struct perf_counter *counter, unsigned int size,
2013 int nmi, int overflow)
2015 struct perf_mmap_data *data;
2016 unsigned int offset, head;
2019 * For inherited counters we send all the output towards the parent.
2021 if (counter->parent)
2022 counter = counter->parent;
2025 data = rcu_dereference(counter->data);
2029 handle->data = data;
2030 handle->counter = counter;
2032 handle->overflow = overflow;
2034 if (!data->nr_pages)
2037 perf_output_lock(handle);
2040 offset = head = atomic_read(&data->head);
2042 } while (atomic_cmpxchg(&data->head, offset, head) != offset);
2044 handle->offset = offset;
2045 handle->head = head;
2047 if ((offset >> PAGE_SHIFT) != (head >> PAGE_SHIFT))
2048 atomic_set(&data->wakeup, 1);
2053 perf_output_wakeup(handle);
2060 static void perf_output_copy(struct perf_output_handle *handle,
2061 void *buf, unsigned int len)
2063 unsigned int pages_mask;
2064 unsigned int offset;
2068 offset = handle->offset;
2069 pages_mask = handle->data->nr_pages - 1;
2070 pages = handle->data->data_pages;
2073 unsigned int page_offset;
2076 nr = (offset >> PAGE_SHIFT) & pages_mask;
2077 page_offset = offset & (PAGE_SIZE - 1);
2078 size = min_t(unsigned int, PAGE_SIZE - page_offset, len);
2080 memcpy(pages[nr] + page_offset, buf, size);
2087 handle->offset = offset;
2090 * Check we didn't copy past our reservation window, taking the
2091 * possible unsigned int wrap into account.
2093 WARN_ON_ONCE(((int)(handle->head - handle->offset)) < 0);
2096 #define perf_output_put(handle, x) \
2097 perf_output_copy((handle), &(x), sizeof(x))
2099 static void perf_output_end(struct perf_output_handle *handle)
2101 struct perf_counter *counter = handle->counter;
2102 struct perf_mmap_data *data = handle->data;
2104 int wakeup_events = counter->hw_event.wakeup_events;
2106 if (handle->overflow && wakeup_events) {
2107 int events = atomic_inc_return(&data->events);
2108 if (events >= wakeup_events) {
2109 atomic_sub(wakeup_events, &data->events);
2110 atomic_set(&data->wakeup, 1);
2114 perf_output_unlock(handle);
2118 static void perf_counter_output(struct perf_counter *counter,
2119 int nmi, struct pt_regs *regs, u64 addr)
2122 u64 record_type = counter->hw_event.record_type;
2123 struct perf_output_handle handle;
2124 struct perf_event_header header;
2133 struct perf_callchain_entry *callchain = NULL;
2134 int callchain_size = 0;
2141 header.size = sizeof(header);
2143 header.misc = PERF_EVENT_MISC_OVERFLOW;
2144 header.misc |= perf_misc_flags(regs);
2146 if (record_type & PERF_RECORD_IP) {
2147 ip = perf_instruction_pointer(regs);
2148 header.type |= PERF_RECORD_IP;
2149 header.size += sizeof(ip);
2152 if (record_type & PERF_RECORD_TID) {
2153 /* namespace issues */
2154 tid_entry.pid = current->group_leader->pid;
2155 tid_entry.tid = current->pid;
2157 header.type |= PERF_RECORD_TID;
2158 header.size += sizeof(tid_entry);
2161 if (record_type & PERF_RECORD_TIME) {
2163 * Maybe do better on x86 and provide cpu_clock_nmi()
2165 time = sched_clock();
2167 header.type |= PERF_RECORD_TIME;
2168 header.size += sizeof(u64);
2171 if (record_type & PERF_RECORD_ADDR) {
2172 header.type |= PERF_RECORD_ADDR;
2173 header.size += sizeof(u64);
2176 if (record_type & PERF_RECORD_CONFIG) {
2177 header.type |= PERF_RECORD_CONFIG;
2178 header.size += sizeof(u64);
2181 if (record_type & PERF_RECORD_CPU) {
2182 header.type |= PERF_RECORD_CPU;
2183 header.size += sizeof(cpu_entry);
2185 cpu_entry.cpu = raw_smp_processor_id();
2188 if (record_type & PERF_RECORD_GROUP) {
2189 header.type |= PERF_RECORD_GROUP;
2190 header.size += sizeof(u64) +
2191 counter->nr_siblings * sizeof(group_entry);
2194 if (record_type & PERF_RECORD_CALLCHAIN) {
2195 callchain = perf_callchain(regs);
2198 callchain_size = (1 + callchain->nr) * sizeof(u64);
2200 header.type |= PERF_RECORD_CALLCHAIN;
2201 header.size += callchain_size;
2205 ret = perf_output_begin(&handle, counter, header.size, nmi, 1);
2209 perf_output_put(&handle, header);
2211 if (record_type & PERF_RECORD_IP)
2212 perf_output_put(&handle, ip);
2214 if (record_type & PERF_RECORD_TID)
2215 perf_output_put(&handle, tid_entry);
2217 if (record_type & PERF_RECORD_TIME)
2218 perf_output_put(&handle, time);
2220 if (record_type & PERF_RECORD_ADDR)
2221 perf_output_put(&handle, addr);
2223 if (record_type & PERF_RECORD_CONFIG)
2224 perf_output_put(&handle, counter->hw_event.config);
2226 if (record_type & PERF_RECORD_CPU)
2227 perf_output_put(&handle, cpu_entry);
2230 * XXX PERF_RECORD_GROUP vs inherited counters seems difficult.
2232 if (record_type & PERF_RECORD_GROUP) {
2233 struct perf_counter *leader, *sub;
2234 u64 nr = counter->nr_siblings;
2236 perf_output_put(&handle, nr);
2238 leader = counter->group_leader;
2239 list_for_each_entry(sub, &leader->sibling_list, list_entry) {
2241 sub->pmu->read(sub);
2243 group_entry.event = sub->hw_event.config;
2244 group_entry.counter = atomic64_read(&sub->count);
2246 perf_output_put(&handle, group_entry);
2251 perf_output_copy(&handle, callchain, callchain_size);
2253 perf_output_end(&handle);
2260 struct perf_comm_event {
2261 struct task_struct *task;
2266 struct perf_event_header header;
2273 static void perf_counter_comm_output(struct perf_counter *counter,
2274 struct perf_comm_event *comm_event)
2276 struct perf_output_handle handle;
2277 int size = comm_event->event.header.size;
2278 int ret = perf_output_begin(&handle, counter, size, 0, 0);
2283 perf_output_put(&handle, comm_event->event);
2284 perf_output_copy(&handle, comm_event->comm,
2285 comm_event->comm_size);
2286 perf_output_end(&handle);
2289 static int perf_counter_comm_match(struct perf_counter *counter,
2290 struct perf_comm_event *comm_event)
2292 if (counter->hw_event.comm &&
2293 comm_event->event.header.type == PERF_EVENT_COMM)
2299 static void perf_counter_comm_ctx(struct perf_counter_context *ctx,
2300 struct perf_comm_event *comm_event)
2302 struct perf_counter *counter;
2304 if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
2308 list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
2309 if (perf_counter_comm_match(counter, comm_event))
2310 perf_counter_comm_output(counter, comm_event);
2315 static void perf_counter_comm_event(struct perf_comm_event *comm_event)
2317 struct perf_cpu_context *cpuctx;
2319 char *comm = comm_event->task->comm;
2321 size = ALIGN(strlen(comm)+1, sizeof(u64));
2323 comm_event->comm = comm;
2324 comm_event->comm_size = size;
2326 comm_event->event.header.size = sizeof(comm_event->event) + size;
2328 cpuctx = &get_cpu_var(perf_cpu_context);
2329 perf_counter_comm_ctx(&cpuctx->ctx, comm_event);
2330 put_cpu_var(perf_cpu_context);
2332 perf_counter_comm_ctx(current->perf_counter_ctxp, comm_event);
2335 void perf_counter_comm(struct task_struct *task)
2337 struct perf_comm_event comm_event;
2339 if (!atomic_read(&nr_comm_tracking))
2341 if (!current->perf_counter_ctxp)
2344 comm_event = (struct perf_comm_event){
2347 .header = { .type = PERF_EVENT_COMM, },
2348 .pid = task->group_leader->pid,
2353 perf_counter_comm_event(&comm_event);
2360 struct perf_mmap_event {
2366 struct perf_event_header header;
2376 static void perf_counter_mmap_output(struct perf_counter *counter,
2377 struct perf_mmap_event *mmap_event)
2379 struct perf_output_handle handle;
2380 int size = mmap_event->event.header.size;
2381 int ret = perf_output_begin(&handle, counter, size, 0, 0);
2386 perf_output_put(&handle, mmap_event->event);
2387 perf_output_copy(&handle, mmap_event->file_name,
2388 mmap_event->file_size);
2389 perf_output_end(&handle);
2392 static int perf_counter_mmap_match(struct perf_counter *counter,
2393 struct perf_mmap_event *mmap_event)
2395 if (counter->hw_event.mmap &&
2396 mmap_event->event.header.type == PERF_EVENT_MMAP)
2399 if (counter->hw_event.munmap &&
2400 mmap_event->event.header.type == PERF_EVENT_MUNMAP)
2406 static void perf_counter_mmap_ctx(struct perf_counter_context *ctx,
2407 struct perf_mmap_event *mmap_event)
2409 struct perf_counter *counter;
2411 if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
2415 list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
2416 if (perf_counter_mmap_match(counter, mmap_event))
2417 perf_counter_mmap_output(counter, mmap_event);
2422 static void perf_counter_mmap_event(struct perf_mmap_event *mmap_event)
2424 struct perf_cpu_context *cpuctx;
2425 struct file *file = mmap_event->file;
2432 buf = kzalloc(PATH_MAX, GFP_KERNEL);
2434 name = strncpy(tmp, "//enomem", sizeof(tmp));
2437 name = d_path(&file->f_path, buf, PATH_MAX);
2439 name = strncpy(tmp, "//toolong", sizeof(tmp));
2443 name = strncpy(tmp, "//anon", sizeof(tmp));
2448 size = ALIGN(strlen(name)+1, sizeof(u64));
2450 mmap_event->file_name = name;
2451 mmap_event->file_size = size;
2453 mmap_event->event.header.size = sizeof(mmap_event->event) + size;
2455 cpuctx = &get_cpu_var(perf_cpu_context);
2456 perf_counter_mmap_ctx(&cpuctx->ctx, mmap_event);
2457 put_cpu_var(perf_cpu_context);
2459 perf_counter_mmap_ctx(current->perf_counter_ctxp, mmap_event);
2464 void perf_counter_mmap(unsigned long addr, unsigned long len,
2465 unsigned long pgoff, struct file *file)
2467 struct perf_mmap_event mmap_event;
2469 if (!atomic_read(&nr_mmap_tracking))
2471 if (!current->perf_counter_ctxp)
2474 mmap_event = (struct perf_mmap_event){
2477 .header = { .type = PERF_EVENT_MMAP, },
2478 .pid = current->group_leader->pid,
2479 .tid = current->pid,
2486 perf_counter_mmap_event(&mmap_event);
2489 void perf_counter_munmap(unsigned long addr, unsigned long len,
2490 unsigned long pgoff, struct file *file)
2492 struct perf_mmap_event mmap_event;
2494 if (!atomic_read(&nr_munmap_tracking))
2497 mmap_event = (struct perf_mmap_event){
2500 .header = { .type = PERF_EVENT_MUNMAP, },
2501 .pid = current->group_leader->pid,
2502 .tid = current->pid,
2509 perf_counter_mmap_event(&mmap_event);
2513 * Log irq_period changes so that analyzing tools can re-normalize the
2517 static void perf_log_period(struct perf_counter *counter, u64 period)
2519 struct perf_output_handle handle;
2523 struct perf_event_header header;
2528 .type = PERF_EVENT_PERIOD,
2530 .size = sizeof(freq_event),
2532 .time = sched_clock(),
2536 if (counter->hw.irq_period == period)
2539 ret = perf_output_begin(&handle, counter, sizeof(freq_event), 0, 0);
2543 perf_output_put(&handle, freq_event);
2544 perf_output_end(&handle);
2548 * Generic counter overflow handling.
2551 int perf_counter_overflow(struct perf_counter *counter,
2552 int nmi, struct pt_regs *regs, u64 addr)
2554 int events = atomic_read(&counter->event_limit);
2557 counter->hw.interrupts++;
2560 * XXX event_limit might not quite work as expected on inherited
2564 counter->pending_kill = POLL_IN;
2565 if (events && atomic_dec_and_test(&counter->event_limit)) {
2567 counter->pending_kill = POLL_HUP;
2569 counter->pending_disable = 1;
2570 perf_pending_queue(&counter->pending,
2571 perf_pending_counter);
2573 perf_counter_disable(counter);
2576 perf_counter_output(counter, nmi, regs, addr);
2581 * Generic software counter infrastructure
2584 static void perf_swcounter_update(struct perf_counter *counter)
2586 struct hw_perf_counter *hwc = &counter->hw;
2591 prev = atomic64_read(&hwc->prev_count);
2592 now = atomic64_read(&hwc->count);
2593 if (atomic64_cmpxchg(&hwc->prev_count, prev, now) != prev)
2598 atomic64_add(delta, &counter->count);
2599 atomic64_sub(delta, &hwc->period_left);
2602 static void perf_swcounter_set_period(struct perf_counter *counter)
2604 struct hw_perf_counter *hwc = &counter->hw;
2605 s64 left = atomic64_read(&hwc->period_left);
2606 s64 period = hwc->irq_period;
2608 if (unlikely(left <= -period)) {
2610 atomic64_set(&hwc->period_left, left);
2613 if (unlikely(left <= 0)) {
2615 atomic64_add(period, &hwc->period_left);
2618 atomic64_set(&hwc->prev_count, -left);
2619 atomic64_set(&hwc->count, -left);
2622 static enum hrtimer_restart perf_swcounter_hrtimer(struct hrtimer *hrtimer)
2624 enum hrtimer_restart ret = HRTIMER_RESTART;
2625 struct perf_counter *counter;
2626 struct pt_regs *regs;
2629 counter = container_of(hrtimer, struct perf_counter, hw.hrtimer);
2630 counter->pmu->read(counter);
2632 regs = get_irq_regs();
2634 * In case we exclude kernel IPs or are somehow not in interrupt
2635 * context, provide the next best thing, the user IP.
2637 if ((counter->hw_event.exclude_kernel || !regs) &&
2638 !counter->hw_event.exclude_user)
2639 regs = task_pt_regs(current);
2642 if (perf_counter_overflow(counter, 0, regs, 0))
2643 ret = HRTIMER_NORESTART;
2646 period = max_t(u64, 10000, counter->hw.irq_period);
2647 hrtimer_forward_now(hrtimer, ns_to_ktime(period));
2652 static void perf_swcounter_overflow(struct perf_counter *counter,
2653 int nmi, struct pt_regs *regs, u64 addr)
2655 perf_swcounter_update(counter);
2656 perf_swcounter_set_period(counter);
2657 if (perf_counter_overflow(counter, nmi, regs, addr))
2658 /* soft-disable the counter */
2663 static int perf_swcounter_match(struct perf_counter *counter,
2664 enum perf_event_types type,
2665 u32 event, struct pt_regs *regs)
2667 if (counter->state != PERF_COUNTER_STATE_ACTIVE)
2670 if (perf_event_raw(&counter->hw_event))
2673 if (perf_event_type(&counter->hw_event) != type)
2676 if (perf_event_id(&counter->hw_event) != event)
2679 if (counter->hw_event.exclude_user && user_mode(regs))
2682 if (counter->hw_event.exclude_kernel && !user_mode(regs))
2688 static void perf_swcounter_add(struct perf_counter *counter, u64 nr,
2689 int nmi, struct pt_regs *regs, u64 addr)
2691 int neg = atomic64_add_negative(nr, &counter->hw.count);
2692 if (counter->hw.irq_period && !neg)
2693 perf_swcounter_overflow(counter, nmi, regs, addr);
2696 static void perf_swcounter_ctx_event(struct perf_counter_context *ctx,
2697 enum perf_event_types type, u32 event,
2698 u64 nr, int nmi, struct pt_regs *regs,
2701 struct perf_counter *counter;
2703 if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
2707 list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
2708 if (perf_swcounter_match(counter, type, event, regs))
2709 perf_swcounter_add(counter, nr, nmi, regs, addr);
2714 static int *perf_swcounter_recursion_context(struct perf_cpu_context *cpuctx)
2717 return &cpuctx->recursion[3];
2720 return &cpuctx->recursion[2];
2723 return &cpuctx->recursion[1];
2725 return &cpuctx->recursion[0];
2728 static void __perf_swcounter_event(enum perf_event_types type, u32 event,
2729 u64 nr, int nmi, struct pt_regs *regs,
2732 struct perf_cpu_context *cpuctx = &get_cpu_var(perf_cpu_context);
2733 int *recursion = perf_swcounter_recursion_context(cpuctx);
2741 perf_swcounter_ctx_event(&cpuctx->ctx, type, event,
2742 nr, nmi, regs, addr);
2743 if (cpuctx->task_ctx) {
2744 perf_swcounter_ctx_event(cpuctx->task_ctx, type, event,
2745 nr, nmi, regs, addr);
2752 put_cpu_var(perf_cpu_context);
2756 perf_swcounter_event(u32 event, u64 nr, int nmi, struct pt_regs *regs, u64 addr)
2758 __perf_swcounter_event(PERF_TYPE_SOFTWARE, event, nr, nmi, regs, addr);
2761 static void perf_swcounter_read(struct perf_counter *counter)
2763 perf_swcounter_update(counter);
2766 static int perf_swcounter_enable(struct perf_counter *counter)
2768 perf_swcounter_set_period(counter);
2772 static void perf_swcounter_disable(struct perf_counter *counter)
2774 perf_swcounter_update(counter);
2777 static const struct pmu perf_ops_generic = {
2778 .enable = perf_swcounter_enable,
2779 .disable = perf_swcounter_disable,
2780 .read = perf_swcounter_read,
2784 * Software counter: cpu wall time clock
2787 static void cpu_clock_perf_counter_update(struct perf_counter *counter)
2789 int cpu = raw_smp_processor_id();
2793 now = cpu_clock(cpu);
2794 prev = atomic64_read(&counter->hw.prev_count);
2795 atomic64_set(&counter->hw.prev_count, now);
2796 atomic64_add(now - prev, &counter->count);
2799 static int cpu_clock_perf_counter_enable(struct perf_counter *counter)
2801 struct hw_perf_counter *hwc = &counter->hw;
2802 int cpu = raw_smp_processor_id();
2804 atomic64_set(&hwc->prev_count, cpu_clock(cpu));
2805 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
2806 hwc->hrtimer.function = perf_swcounter_hrtimer;
2807 if (hwc->irq_period) {
2808 u64 period = max_t(u64, 10000, hwc->irq_period);
2809 __hrtimer_start_range_ns(&hwc->hrtimer,
2810 ns_to_ktime(period), 0,
2811 HRTIMER_MODE_REL, 0);
2817 static void cpu_clock_perf_counter_disable(struct perf_counter *counter)
2819 if (counter->hw.irq_period)
2820 hrtimer_cancel(&counter->hw.hrtimer);
2821 cpu_clock_perf_counter_update(counter);
2824 static void cpu_clock_perf_counter_read(struct perf_counter *counter)
2826 cpu_clock_perf_counter_update(counter);
2829 static const struct pmu perf_ops_cpu_clock = {
2830 .enable = cpu_clock_perf_counter_enable,
2831 .disable = cpu_clock_perf_counter_disable,
2832 .read = cpu_clock_perf_counter_read,
2836 * Software counter: task time clock
2839 static void task_clock_perf_counter_update(struct perf_counter *counter, u64 now)
2844 prev = atomic64_xchg(&counter->hw.prev_count, now);
2846 atomic64_add(delta, &counter->count);
2849 static int task_clock_perf_counter_enable(struct perf_counter *counter)
2851 struct hw_perf_counter *hwc = &counter->hw;
2854 now = counter->ctx->time;
2856 atomic64_set(&hwc->prev_count, now);
2857 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
2858 hwc->hrtimer.function = perf_swcounter_hrtimer;
2859 if (hwc->irq_period) {
2860 u64 period = max_t(u64, 10000, hwc->irq_period);
2861 __hrtimer_start_range_ns(&hwc->hrtimer,
2862 ns_to_ktime(period), 0,
2863 HRTIMER_MODE_REL, 0);
2869 static void task_clock_perf_counter_disable(struct perf_counter *counter)
2871 if (counter->hw.irq_period)
2872 hrtimer_cancel(&counter->hw.hrtimer);
2873 task_clock_perf_counter_update(counter, counter->ctx->time);
2877 static void task_clock_perf_counter_read(struct perf_counter *counter)
2882 update_context_time(counter->ctx);
2883 time = counter->ctx->time;
2885 u64 now = perf_clock();
2886 u64 delta = now - counter->ctx->timestamp;
2887 time = counter->ctx->time + delta;
2890 task_clock_perf_counter_update(counter, time);
2893 static const struct pmu perf_ops_task_clock = {
2894 .enable = task_clock_perf_counter_enable,
2895 .disable = task_clock_perf_counter_disable,
2896 .read = task_clock_perf_counter_read,
2900 * Software counter: cpu migrations
2903 static inline u64 get_cpu_migrations(struct perf_counter *counter)
2905 struct task_struct *curr = counter->ctx->task;
2908 return curr->se.nr_migrations;
2909 return cpu_nr_migrations(smp_processor_id());
2912 static void cpu_migrations_perf_counter_update(struct perf_counter *counter)
2917 prev = atomic64_read(&counter->hw.prev_count);
2918 now = get_cpu_migrations(counter);
2920 atomic64_set(&counter->hw.prev_count, now);
2924 atomic64_add(delta, &counter->count);
2927 static void cpu_migrations_perf_counter_read(struct perf_counter *counter)
2929 cpu_migrations_perf_counter_update(counter);
2932 static int cpu_migrations_perf_counter_enable(struct perf_counter *counter)
2934 if (counter->prev_state <= PERF_COUNTER_STATE_OFF)
2935 atomic64_set(&counter->hw.prev_count,
2936 get_cpu_migrations(counter));
2940 static void cpu_migrations_perf_counter_disable(struct perf_counter *counter)
2942 cpu_migrations_perf_counter_update(counter);
2945 static const struct pmu perf_ops_cpu_migrations = {
2946 .enable = cpu_migrations_perf_counter_enable,
2947 .disable = cpu_migrations_perf_counter_disable,
2948 .read = cpu_migrations_perf_counter_read,
2951 #ifdef CONFIG_EVENT_PROFILE
2952 void perf_tpcounter_event(int event_id)
2954 struct pt_regs *regs = get_irq_regs();
2957 regs = task_pt_regs(current);
2959 __perf_swcounter_event(PERF_TYPE_TRACEPOINT, event_id, 1, 1, regs, 0);
2961 EXPORT_SYMBOL_GPL(perf_tpcounter_event);
2963 extern int ftrace_profile_enable(int);
2964 extern void ftrace_profile_disable(int);
2966 static void tp_perf_counter_destroy(struct perf_counter *counter)
2968 ftrace_profile_disable(perf_event_id(&counter->hw_event));
2971 static const struct pmu *tp_perf_counter_init(struct perf_counter *counter)
2973 int event_id = perf_event_id(&counter->hw_event);
2976 ret = ftrace_profile_enable(event_id);
2980 counter->destroy = tp_perf_counter_destroy;
2981 counter->hw.irq_period = counter->hw_event.irq_period;
2983 return &perf_ops_generic;
2986 static const struct pmu *tp_perf_counter_init(struct perf_counter *counter)
2992 static const struct pmu *sw_perf_counter_init(struct perf_counter *counter)
2994 const struct pmu *pmu = NULL;
2997 * Software counters (currently) can't in general distinguish
2998 * between user, kernel and hypervisor events.
2999 * However, context switches and cpu migrations are considered
3000 * to be kernel events, and page faults are never hypervisor
3003 switch (perf_event_id(&counter->hw_event)) {
3004 case PERF_COUNT_CPU_CLOCK:
3005 pmu = &perf_ops_cpu_clock;
3008 case PERF_COUNT_TASK_CLOCK:
3010 * If the user instantiates this as a per-cpu counter,
3011 * use the cpu_clock counter instead.
3013 if (counter->ctx->task)
3014 pmu = &perf_ops_task_clock;
3016 pmu = &perf_ops_cpu_clock;
3019 case PERF_COUNT_PAGE_FAULTS:
3020 case PERF_COUNT_PAGE_FAULTS_MIN:
3021 case PERF_COUNT_PAGE_FAULTS_MAJ:
3022 case PERF_COUNT_CONTEXT_SWITCHES:
3023 pmu = &perf_ops_generic;
3025 case PERF_COUNT_CPU_MIGRATIONS:
3026 if (!counter->hw_event.exclude_kernel)
3027 pmu = &perf_ops_cpu_migrations;
3035 * Allocate and initialize a counter structure
3037 static struct perf_counter *
3038 perf_counter_alloc(struct perf_counter_hw_event *hw_event,
3040 struct perf_counter_context *ctx,
3041 struct perf_counter *group_leader,
3044 const struct pmu *pmu;
3045 struct perf_counter *counter;
3046 struct hw_perf_counter *hwc;
3049 counter = kzalloc(sizeof(*counter), gfpflags);
3051 return ERR_PTR(-ENOMEM);
3054 * Single counters are their own group leaders, with an
3055 * empty sibling list:
3058 group_leader = counter;
3060 mutex_init(&counter->child_mutex);
3061 INIT_LIST_HEAD(&counter->child_list);
3063 INIT_LIST_HEAD(&counter->list_entry);
3064 INIT_LIST_HEAD(&counter->event_entry);
3065 INIT_LIST_HEAD(&counter->sibling_list);
3066 init_waitqueue_head(&counter->waitq);
3068 mutex_init(&counter->mmap_mutex);
3071 counter->hw_event = *hw_event;
3072 counter->group_leader = group_leader;
3073 counter->pmu = NULL;
3077 counter->state = PERF_COUNTER_STATE_INACTIVE;
3078 if (hw_event->disabled)
3079 counter->state = PERF_COUNTER_STATE_OFF;
3084 if (hw_event->freq && hw_event->irq_freq)
3085 hwc->irq_period = div64_u64(TICK_NSEC, hw_event->irq_freq);
3087 hwc->irq_period = hw_event->irq_period;
3090 * we currently do not support PERF_RECORD_GROUP on inherited counters
3092 if (hw_event->inherit && (hw_event->record_type & PERF_RECORD_GROUP))
3095 if (perf_event_raw(hw_event)) {
3096 pmu = hw_perf_counter_init(counter);
3100 switch (perf_event_type(hw_event)) {
3101 case PERF_TYPE_HARDWARE:
3102 pmu = hw_perf_counter_init(counter);
3105 case PERF_TYPE_SOFTWARE:
3106 pmu = sw_perf_counter_init(counter);
3109 case PERF_TYPE_TRACEPOINT:
3110 pmu = tp_perf_counter_init(counter);
3117 else if (IS_ERR(pmu))
3122 return ERR_PTR(err);
3127 atomic_inc(&nr_counters);
3128 if (counter->hw_event.mmap)
3129 atomic_inc(&nr_mmap_tracking);
3130 if (counter->hw_event.munmap)
3131 atomic_inc(&nr_munmap_tracking);
3132 if (counter->hw_event.comm)
3133 atomic_inc(&nr_comm_tracking);
3139 * sys_perf_counter_open - open a performance counter, associate it to a task/cpu
3141 * @hw_event_uptr: event type attributes for monitoring/sampling
3144 * @group_fd: group leader counter fd
3146 SYSCALL_DEFINE5(perf_counter_open,
3147 const struct perf_counter_hw_event __user *, hw_event_uptr,
3148 pid_t, pid, int, cpu, int, group_fd, unsigned long, flags)
3150 struct perf_counter *counter, *group_leader;
3151 struct perf_counter_hw_event hw_event;
3152 struct perf_counter_context *ctx;
3153 struct file *counter_file = NULL;
3154 struct file *group_file = NULL;
3155 int fput_needed = 0;
3156 int fput_needed2 = 0;
3159 /* for future expandability... */
3163 if (copy_from_user(&hw_event, hw_event_uptr, sizeof(hw_event)) != 0)
3167 * Get the target context (task or percpu):
3169 ctx = find_get_context(pid, cpu);
3171 return PTR_ERR(ctx);
3174 * Look up the group leader (we will attach this counter to it):
3176 group_leader = NULL;
3177 if (group_fd != -1) {
3179 group_file = fget_light(group_fd, &fput_needed);
3181 goto err_put_context;
3182 if (group_file->f_op != &perf_fops)
3183 goto err_put_context;
3185 group_leader = group_file->private_data;
3187 * Do not allow a recursive hierarchy (this new sibling
3188 * becoming part of another group-sibling):
3190 if (group_leader->group_leader != group_leader)
3191 goto err_put_context;
3193 * Do not allow to attach to a group in a different
3194 * task or CPU context:
3196 if (group_leader->ctx != ctx)
3197 goto err_put_context;
3199 * Only a group leader can be exclusive or pinned
3201 if (hw_event.exclusive || hw_event.pinned)
3202 goto err_put_context;
3205 counter = perf_counter_alloc(&hw_event, cpu, ctx, group_leader,
3207 ret = PTR_ERR(counter);
3208 if (IS_ERR(counter))
3209 goto err_put_context;
3211 ret = anon_inode_getfd("[perf_counter]", &perf_fops, counter, 0);
3213 goto err_free_put_context;
3215 counter_file = fget_light(ret, &fput_needed2);
3217 goto err_free_put_context;
3219 counter->filp = counter_file;
3220 mutex_lock(&ctx->mutex);
3221 perf_install_in_context(ctx, counter, cpu);
3222 mutex_unlock(&ctx->mutex);
3224 counter->owner = current;
3225 get_task_struct(current);
3226 mutex_lock(¤t->perf_counter_mutex);
3227 list_add_tail(&counter->owner_entry, ¤t->perf_counter_list);
3228 mutex_unlock(¤t->perf_counter_mutex);
3230 fput_light(counter_file, fput_needed2);
3233 fput_light(group_file, fput_needed);
3237 err_free_put_context:
3247 * inherit a counter from parent task to child task:
3249 static struct perf_counter *
3250 inherit_counter(struct perf_counter *parent_counter,
3251 struct task_struct *parent,
3252 struct perf_counter_context *parent_ctx,
3253 struct task_struct *child,
3254 struct perf_counter *group_leader,
3255 struct perf_counter_context *child_ctx)
3257 struct perf_counter *child_counter;
3260 * Instead of creating recursive hierarchies of counters,
3261 * we link inherited counters back to the original parent,
3262 * which has a filp for sure, which we use as the reference
3265 if (parent_counter->parent)
3266 parent_counter = parent_counter->parent;
3268 child_counter = perf_counter_alloc(&parent_counter->hw_event,
3269 parent_counter->cpu, child_ctx,
3270 group_leader, GFP_KERNEL);
3271 if (IS_ERR(child_counter))
3272 return child_counter;
3275 * Make the child state follow the state of the parent counter,
3276 * not its hw_event.disabled bit. We hold the parent's mutex,
3277 * so we won't race with perf_counter_{en,dis}able_family.
3279 if (parent_counter->state >= PERF_COUNTER_STATE_INACTIVE)
3280 child_counter->state = PERF_COUNTER_STATE_INACTIVE;
3282 child_counter->state = PERF_COUNTER_STATE_OFF;
3285 * Link it up in the child's context:
3287 add_counter_to_ctx(child_counter, child_ctx);
3289 child_counter->parent = parent_counter;
3291 * inherit into child's child as well:
3293 child_counter->hw_event.inherit = 1;
3296 * Get a reference to the parent filp - we will fput it
3297 * when the child counter exits. This is safe to do because
3298 * we are in the parent and we know that the filp still
3299 * exists and has a nonzero count:
3301 atomic_long_inc(&parent_counter->filp->f_count);
3304 * Link this into the parent counter's child list
3306 mutex_lock(&parent_counter->child_mutex);
3307 list_add_tail(&child_counter->child_list, &parent_counter->child_list);
3308 mutex_unlock(&parent_counter->child_mutex);
3310 return child_counter;
3313 static int inherit_group(struct perf_counter *parent_counter,
3314 struct task_struct *parent,
3315 struct perf_counter_context *parent_ctx,
3316 struct task_struct *child,
3317 struct perf_counter_context *child_ctx)
3319 struct perf_counter *leader;
3320 struct perf_counter *sub;
3321 struct perf_counter *child_ctr;
3323 leader = inherit_counter(parent_counter, parent, parent_ctx,
3324 child, NULL, child_ctx);
3326 return PTR_ERR(leader);
3327 list_for_each_entry(sub, &parent_counter->sibling_list, list_entry) {
3328 child_ctr = inherit_counter(sub, parent, parent_ctx,
3329 child, leader, child_ctx);
3330 if (IS_ERR(child_ctr))
3331 return PTR_ERR(child_ctr);
3336 static void sync_child_counter(struct perf_counter *child_counter,
3337 struct perf_counter *parent_counter)
3341 child_val = atomic64_read(&child_counter->count);
3344 * Add back the child's count to the parent's count:
3346 atomic64_add(child_val, &parent_counter->count);
3347 atomic64_add(child_counter->total_time_enabled,
3348 &parent_counter->child_total_time_enabled);
3349 atomic64_add(child_counter->total_time_running,
3350 &parent_counter->child_total_time_running);
3353 * Remove this counter from the parent's list
3355 mutex_lock(&parent_counter->child_mutex);
3356 list_del_init(&child_counter->child_list);
3357 mutex_unlock(&parent_counter->child_mutex);
3360 * Release the parent counter, if this was the last
3363 fput(parent_counter->filp);
3367 __perf_counter_exit_task(struct task_struct *child,
3368 struct perf_counter *child_counter,
3369 struct perf_counter_context *child_ctx)
3371 struct perf_counter *parent_counter;
3373 update_counter_times(child_counter);
3374 perf_counter_remove_from_context(child_counter);
3376 parent_counter = child_counter->parent;
3378 * It can happen that parent exits first, and has counters
3379 * that are still around due to the child reference. These
3380 * counters need to be zapped - but otherwise linger.
3382 if (parent_counter) {
3383 sync_child_counter(child_counter, parent_counter);
3384 free_counter(child_counter);
3389 * When a child task exits, feed back counter values to parent counters.
3391 * Note: we may be running in child context, but the PID is not hashed
3392 * anymore so new counters will not be added.
3393 * (XXX not sure that is true when we get called from flush_old_exec.
3396 void perf_counter_exit_task(struct task_struct *child)
3398 struct perf_counter *child_counter, *tmp;
3399 struct perf_counter_context *child_ctx;
3400 unsigned long flags;
3402 WARN_ON_ONCE(child != current);
3404 child_ctx = child->perf_counter_ctxp;
3406 if (likely(!child_ctx))
3409 local_irq_save(flags);
3410 __perf_counter_task_sched_out(child_ctx);
3411 child->perf_counter_ctxp = NULL;
3412 local_irq_restore(flags);
3414 mutex_lock(&child_ctx->mutex);
3417 list_for_each_entry_safe(child_counter, tmp, &child_ctx->counter_list,
3419 __perf_counter_exit_task(child, child_counter, child_ctx);
3422 * If the last counter was a group counter, it will have appended all
3423 * its siblings to the list, but we obtained 'tmp' before that which
3424 * will still point to the list head terminating the iteration.
3426 if (!list_empty(&child_ctx->counter_list))
3429 mutex_unlock(&child_ctx->mutex);
3435 * Initialize the perf_counter context in task_struct
3437 void perf_counter_init_task(struct task_struct *child)
3439 struct perf_counter_context *child_ctx, *parent_ctx;
3440 struct perf_counter *counter;
3441 struct task_struct *parent = current;
3442 int inherited_all = 1;
3444 child->perf_counter_ctxp = NULL;
3446 mutex_init(&child->perf_counter_mutex);
3447 INIT_LIST_HEAD(&child->perf_counter_list);
3450 * This is executed from the parent task context, so inherit
3451 * counters that have been marked for cloning.
3452 * First allocate and initialize a context for the child.
3455 child_ctx = kmalloc(sizeof(struct perf_counter_context), GFP_KERNEL);
3459 parent_ctx = parent->perf_counter_ctxp;
3460 if (likely(!parent_ctx || !parent_ctx->nr_counters))
3463 __perf_counter_init_context(child_ctx, child);
3464 child->perf_counter_ctxp = child_ctx;
3467 * Lock the parent list. No need to lock the child - not PID
3468 * hashed yet and not running, so nobody can access it.
3470 mutex_lock(&parent_ctx->mutex);
3473 * We dont have to disable NMIs - we are only looking at
3474 * the list, not manipulating it:
3476 list_for_each_entry_rcu(counter, &parent_ctx->event_list, event_entry) {
3477 if (counter != counter->group_leader)
3480 if (!counter->hw_event.inherit) {
3485 if (inherit_group(counter, parent,
3486 parent_ctx, child, child_ctx)) {
3492 if (inherited_all) {
3494 * Mark the child context as a clone of the parent
3495 * context, or of whatever the parent is a clone of.
3497 if (parent_ctx->parent_ctx) {
3498 child_ctx->parent_ctx = parent_ctx->parent_ctx;
3499 child_ctx->parent_gen = parent_ctx->parent_gen;
3501 child_ctx->parent_ctx = parent_ctx;
3502 child_ctx->parent_gen = parent_ctx->generation;
3504 get_ctx(child_ctx->parent_ctx);
3507 mutex_unlock(&parent_ctx->mutex);
3510 static void __cpuinit perf_counter_init_cpu(int cpu)
3512 struct perf_cpu_context *cpuctx;
3514 cpuctx = &per_cpu(perf_cpu_context, cpu);
3515 __perf_counter_init_context(&cpuctx->ctx, NULL);
3517 spin_lock(&perf_resource_lock);
3518 cpuctx->max_pertask = perf_max_counters - perf_reserved_percpu;
3519 spin_unlock(&perf_resource_lock);
3521 hw_perf_counter_setup(cpu);
3524 #ifdef CONFIG_HOTPLUG_CPU
3525 static void __perf_counter_exit_cpu(void *info)
3527 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
3528 struct perf_counter_context *ctx = &cpuctx->ctx;
3529 struct perf_counter *counter, *tmp;
3531 list_for_each_entry_safe(counter, tmp, &ctx->counter_list, list_entry)
3532 __perf_counter_remove_from_context(counter);
3534 static void perf_counter_exit_cpu(int cpu)
3536 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
3537 struct perf_counter_context *ctx = &cpuctx->ctx;
3539 mutex_lock(&ctx->mutex);
3540 smp_call_function_single(cpu, __perf_counter_exit_cpu, NULL, 1);
3541 mutex_unlock(&ctx->mutex);
3544 static inline void perf_counter_exit_cpu(int cpu) { }
3547 static int __cpuinit
3548 perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
3550 unsigned int cpu = (long)hcpu;
3554 case CPU_UP_PREPARE:
3555 case CPU_UP_PREPARE_FROZEN:
3556 perf_counter_init_cpu(cpu);
3559 case CPU_DOWN_PREPARE:
3560 case CPU_DOWN_PREPARE_FROZEN:
3561 perf_counter_exit_cpu(cpu);
3571 static struct notifier_block __cpuinitdata perf_cpu_nb = {
3572 .notifier_call = perf_cpu_notify,
3575 void __init perf_counter_init(void)
3577 perf_cpu_notify(&perf_cpu_nb, (unsigned long)CPU_UP_PREPARE,
3578 (void *)(long)smp_processor_id());
3579 register_cpu_notifier(&perf_cpu_nb);
3582 static ssize_t perf_show_reserve_percpu(struct sysdev_class *class, char *buf)
3584 return sprintf(buf, "%d\n", perf_reserved_percpu);
3588 perf_set_reserve_percpu(struct sysdev_class *class,
3592 struct perf_cpu_context *cpuctx;
3596 err = strict_strtoul(buf, 10, &val);
3599 if (val > perf_max_counters)
3602 spin_lock(&perf_resource_lock);
3603 perf_reserved_percpu = val;
3604 for_each_online_cpu(cpu) {
3605 cpuctx = &per_cpu(perf_cpu_context, cpu);
3606 spin_lock_irq(&cpuctx->ctx.lock);
3607 mpt = min(perf_max_counters - cpuctx->ctx.nr_counters,
3608 perf_max_counters - perf_reserved_percpu);
3609 cpuctx->max_pertask = mpt;
3610 spin_unlock_irq(&cpuctx->ctx.lock);
3612 spin_unlock(&perf_resource_lock);
3617 static ssize_t perf_show_overcommit(struct sysdev_class *class, char *buf)
3619 return sprintf(buf, "%d\n", perf_overcommit);
3623 perf_set_overcommit(struct sysdev_class *class, const char *buf, size_t count)
3628 err = strict_strtoul(buf, 10, &val);
3634 spin_lock(&perf_resource_lock);
3635 perf_overcommit = val;
3636 spin_unlock(&perf_resource_lock);
3641 static SYSDEV_CLASS_ATTR(
3644 perf_show_reserve_percpu,
3645 perf_set_reserve_percpu
3648 static SYSDEV_CLASS_ATTR(
3651 perf_show_overcommit,
3655 static struct attribute *perfclass_attrs[] = {
3656 &attr_reserve_percpu.attr,
3657 &attr_overcommit.attr,
3661 static struct attribute_group perfclass_attr_group = {
3662 .attrs = perfclass_attrs,
3663 .name = "perf_counters",
3666 static int __init perf_counter_sysfs_init(void)
3668 return sysfs_create_group(&cpu_sysdev_class.kset.kobj,
3669 &perfclass_attr_group);
3671 device_initcall(perf_counter_sysfs_init);