2 * Performance counter core code
4 * Copyright (C) 2008 Thomas Gleixner <tglx@linutronix.de>
5 * Copyright (C) 2008-2009 Red Hat, Inc., Ingo Molnar
6 * Copyright (C) 2008-2009 Red Hat, Inc., Peter Zijlstra <pzijlstr@redhat.com>
7 * Copyright © 2009 Paul Mackerras, IBM Corp. <paulus@au1.ibm.com>
9 * For licensing details see kernel-base/COPYING
14 #include <linux/cpu.h>
15 #include <linux/smp.h>
16 #include <linux/file.h>
17 #include <linux/poll.h>
18 #include <linux/sysfs.h>
19 #include <linux/ptrace.h>
20 #include <linux/percpu.h>
21 #include <linux/vmstat.h>
22 #include <linux/hardirq.h>
23 #include <linux/rculist.h>
24 #include <linux/uaccess.h>
25 #include <linux/syscalls.h>
26 #include <linux/anon_inodes.h>
27 #include <linux/kernel_stat.h>
28 #include <linux/perf_counter.h>
29 #include <linux/dcache.h>
31 #include <asm/irq_regs.h>
34 * Each CPU has a list of per CPU counters:
36 DEFINE_PER_CPU(struct perf_cpu_context, perf_cpu_context);
38 int perf_max_counters __read_mostly = 1;
39 static int perf_reserved_percpu __read_mostly;
40 static int perf_overcommit __read_mostly = 1;
42 static atomic_t nr_counters __read_mostly;
43 static atomic_t nr_mmap_tracking __read_mostly;
44 static atomic_t nr_munmap_tracking __read_mostly;
45 static atomic_t nr_comm_tracking __read_mostly;
47 int sysctl_perf_counter_priv __read_mostly; /* do we need to be privileged */
48 int sysctl_perf_counter_mlock __read_mostly = 128; /* 'free' kb per counter */
51 * Lock for (sysadmin-configurable) counter reservations:
53 static DEFINE_SPINLOCK(perf_resource_lock);
56 * Architecture provided APIs - weak aliases:
58 extern __weak const struct pmu *hw_perf_counter_init(struct perf_counter *counter)
63 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)
93 EXPORT_SYMBOL_GPL(perf_disable); /* ACPI idle */
95 void perf_enable(void)
100 EXPORT_SYMBOL_GPL(perf_enable); /* ACPI idle */
103 list_add_counter(struct perf_counter *counter, struct perf_counter_context *ctx)
105 struct perf_counter *group_leader = counter->group_leader;
108 * Depending on whether it is a standalone or sibling counter,
109 * add it straight to the context's counter list, or to the group
110 * leader's sibling list:
112 if (group_leader == counter)
113 list_add_tail(&counter->list_entry, &ctx->counter_list);
115 list_add_tail(&counter->list_entry, &group_leader->sibling_list);
116 group_leader->nr_siblings++;
119 list_add_rcu(&counter->event_entry, &ctx->event_list);
123 list_del_counter(struct perf_counter *counter, struct perf_counter_context *ctx)
125 struct perf_counter *sibling, *tmp;
127 list_del_init(&counter->list_entry);
128 list_del_rcu(&counter->event_entry);
130 if (counter->group_leader != counter)
131 counter->group_leader->nr_siblings--;
134 * If this was a group counter with sibling counters then
135 * upgrade the siblings to singleton counters by adding them
136 * to the context list directly:
138 list_for_each_entry_safe(sibling, tmp,
139 &counter->sibling_list, list_entry) {
141 list_move_tail(&sibling->list_entry, &ctx->counter_list);
142 sibling->group_leader = sibling;
147 counter_sched_out(struct perf_counter *counter,
148 struct perf_cpu_context *cpuctx,
149 struct perf_counter_context *ctx)
151 if (counter->state != PERF_COUNTER_STATE_ACTIVE)
154 counter->state = PERF_COUNTER_STATE_INACTIVE;
155 counter->tstamp_stopped = ctx->time;
156 counter->pmu->disable(counter);
159 if (!is_software_counter(counter))
160 cpuctx->active_oncpu--;
162 if (counter->hw_event.exclusive || !cpuctx->active_oncpu)
163 cpuctx->exclusive = 0;
167 group_sched_out(struct perf_counter *group_counter,
168 struct perf_cpu_context *cpuctx,
169 struct perf_counter_context *ctx)
171 struct perf_counter *counter;
173 if (group_counter->state != PERF_COUNTER_STATE_ACTIVE)
176 counter_sched_out(group_counter, cpuctx, ctx);
179 * Schedule out siblings (if any):
181 list_for_each_entry(counter, &group_counter->sibling_list, list_entry)
182 counter_sched_out(counter, cpuctx, ctx);
184 if (group_counter->hw_event.exclusive)
185 cpuctx->exclusive = 0;
189 * Cross CPU call to remove a performance counter
191 * We disable the counter on the hardware level first. After that we
192 * remove it from the context list.
194 static void __perf_counter_remove_from_context(void *info)
196 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
197 struct perf_counter *counter = info;
198 struct perf_counter_context *ctx = counter->ctx;
202 * If this is a task context, we need to check whether it is
203 * the current task context of this cpu. If not it has been
204 * scheduled out before the smp call arrived.
206 if (ctx->task && cpuctx->task_ctx != ctx)
209 spin_lock_irqsave(&ctx->lock, flags);
211 counter_sched_out(counter, cpuctx, ctx);
213 counter->task = NULL;
217 * Protect the list operation against NMI by disabling the
218 * counters on a global level. NOP for non NMI based counters.
221 list_del_counter(counter, ctx);
226 * Allow more per task counters with respect to the
229 cpuctx->max_pertask =
230 min(perf_max_counters - ctx->nr_counters,
231 perf_max_counters - perf_reserved_percpu);
234 spin_unlock_irqrestore(&ctx->lock, flags);
239 * Remove the counter from a task's (or a CPU's) list of counters.
241 * Must be called with counter->mutex and ctx->mutex held.
243 * CPU counters are removed with a smp call. For task counters we only
244 * call when the task is on a CPU.
246 static void perf_counter_remove_from_context(struct perf_counter *counter)
248 struct perf_counter_context *ctx = counter->ctx;
249 struct task_struct *task = ctx->task;
253 * Per cpu counters are removed via an smp call and
254 * the removal is always sucessful.
256 smp_call_function_single(counter->cpu,
257 __perf_counter_remove_from_context,
263 task_oncpu_function_call(task, __perf_counter_remove_from_context,
266 spin_lock_irq(&ctx->lock);
268 * If the context is active we need to retry the smp call.
270 if (ctx->nr_active && !list_empty(&counter->list_entry)) {
271 spin_unlock_irq(&ctx->lock);
276 * The lock prevents that this context is scheduled in so we
277 * can remove the counter safely, if the call above did not
280 if (!list_empty(&counter->list_entry)) {
282 list_del_counter(counter, ctx);
283 counter->task = NULL;
285 spin_unlock_irq(&ctx->lock);
288 static inline u64 perf_clock(void)
290 return cpu_clock(smp_processor_id());
294 * Update the record of the current time in a context.
296 static void update_context_time(struct perf_counter_context *ctx)
298 u64 now = perf_clock();
300 ctx->time += now - ctx->timestamp;
301 ctx->timestamp = now;
305 * Update the total_time_enabled and total_time_running fields for a counter.
307 static void update_counter_times(struct perf_counter *counter)
309 struct perf_counter_context *ctx = counter->ctx;
312 if (counter->state < PERF_COUNTER_STATE_INACTIVE)
315 counter->total_time_enabled = ctx->time - counter->tstamp_enabled;
317 if (counter->state == PERF_COUNTER_STATE_INACTIVE)
318 run_end = counter->tstamp_stopped;
322 counter->total_time_running = run_end - counter->tstamp_running;
326 * Update total_time_enabled and total_time_running for all counters in a group.
328 static void update_group_times(struct perf_counter *leader)
330 struct perf_counter *counter;
332 update_counter_times(leader);
333 list_for_each_entry(counter, &leader->sibling_list, list_entry)
334 update_counter_times(counter);
338 * Cross CPU call to disable a performance counter
340 static void __perf_counter_disable(void *info)
342 struct perf_counter *counter = info;
343 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
344 struct perf_counter_context *ctx = counter->ctx;
348 * If this is a per-task counter, need to check whether this
349 * counter's task is the current task on this cpu.
351 if (ctx->task && cpuctx->task_ctx != ctx)
354 spin_lock_irqsave(&ctx->lock, flags);
357 * If the counter is on, turn it off.
358 * If it is in error state, leave it in error state.
360 if (counter->state >= PERF_COUNTER_STATE_INACTIVE) {
361 update_context_time(ctx);
362 update_counter_times(counter);
363 if (counter == counter->group_leader)
364 group_sched_out(counter, cpuctx, ctx);
366 counter_sched_out(counter, cpuctx, ctx);
367 counter->state = PERF_COUNTER_STATE_OFF;
370 spin_unlock_irqrestore(&ctx->lock, flags);
376 static void perf_counter_disable(struct perf_counter *counter)
378 struct perf_counter_context *ctx = counter->ctx;
379 struct task_struct *task = ctx->task;
383 * Disable the counter on the cpu that it's on
385 smp_call_function_single(counter->cpu, __perf_counter_disable,
391 task_oncpu_function_call(task, __perf_counter_disable, counter);
393 spin_lock_irq(&ctx->lock);
395 * If the counter is still active, we need to retry the cross-call.
397 if (counter->state == PERF_COUNTER_STATE_ACTIVE) {
398 spin_unlock_irq(&ctx->lock);
403 * Since we have the lock this context can't be scheduled
404 * in, so we can change the state safely.
406 if (counter->state == PERF_COUNTER_STATE_INACTIVE) {
407 update_counter_times(counter);
408 counter->state = PERF_COUNTER_STATE_OFF;
411 spin_unlock_irq(&ctx->lock);
415 counter_sched_in(struct perf_counter *counter,
416 struct perf_cpu_context *cpuctx,
417 struct perf_counter_context *ctx,
420 if (counter->state <= PERF_COUNTER_STATE_OFF)
423 counter->state = PERF_COUNTER_STATE_ACTIVE;
424 counter->oncpu = cpu; /* TODO: put 'cpu' into cpuctx->cpu */
426 * The new state must be visible before we turn it on in the hardware:
430 if (counter->pmu->enable(counter)) {
431 counter->state = PERF_COUNTER_STATE_INACTIVE;
436 counter->tstamp_running += ctx->time - counter->tstamp_stopped;
438 if (!is_software_counter(counter))
439 cpuctx->active_oncpu++;
442 if (counter->hw_event.exclusive)
443 cpuctx->exclusive = 1;
449 group_sched_in(struct perf_counter *group_counter,
450 struct perf_cpu_context *cpuctx,
451 struct perf_counter_context *ctx,
454 struct perf_counter *counter, *partial_group;
457 if (group_counter->state == PERF_COUNTER_STATE_OFF)
460 ret = hw_perf_group_sched_in(group_counter, cpuctx, ctx, cpu);
462 return ret < 0 ? ret : 0;
464 group_counter->prev_state = group_counter->state;
465 if (counter_sched_in(group_counter, cpuctx, ctx, cpu))
469 * Schedule in siblings as one group (if any):
471 list_for_each_entry(counter, &group_counter->sibling_list, list_entry) {
472 counter->prev_state = counter->state;
473 if (counter_sched_in(counter, cpuctx, ctx, cpu)) {
474 partial_group = counter;
483 * Groups can be scheduled in as one unit only, so undo any
484 * partial group before returning:
486 list_for_each_entry(counter, &group_counter->sibling_list, list_entry) {
487 if (counter == partial_group)
489 counter_sched_out(counter, cpuctx, ctx);
491 counter_sched_out(group_counter, cpuctx, ctx);
497 * Return 1 for a group consisting entirely of software counters,
498 * 0 if the group contains any hardware counters.
500 static int is_software_only_group(struct perf_counter *leader)
502 struct perf_counter *counter;
504 if (!is_software_counter(leader))
507 list_for_each_entry(counter, &leader->sibling_list, list_entry)
508 if (!is_software_counter(counter))
515 * Work out whether we can put this counter group on the CPU now.
517 static int group_can_go_on(struct perf_counter *counter,
518 struct perf_cpu_context *cpuctx,
522 * Groups consisting entirely of software counters can always go on.
524 if (is_software_only_group(counter))
527 * If an exclusive group is already on, no other hardware
528 * counters can go on.
530 if (cpuctx->exclusive)
533 * If this group is exclusive and there are already
534 * counters on the CPU, it can't go on.
536 if (counter->hw_event.exclusive && cpuctx->active_oncpu)
539 * Otherwise, try to add it if all previous groups were able
545 static void add_counter_to_ctx(struct perf_counter *counter,
546 struct perf_counter_context *ctx)
548 list_add_counter(counter, ctx);
550 counter->prev_state = PERF_COUNTER_STATE_OFF;
551 counter->tstamp_enabled = ctx->time;
552 counter->tstamp_running = ctx->time;
553 counter->tstamp_stopped = ctx->time;
557 * Cross CPU call to install and enable a performance counter
559 static void __perf_install_in_context(void *info)
561 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
562 struct perf_counter *counter = info;
563 struct perf_counter_context *ctx = counter->ctx;
564 struct perf_counter *leader = counter->group_leader;
565 int cpu = smp_processor_id();
570 * If this is a task context, we need to check whether it is
571 * the current task context of this cpu. If not it has been
572 * scheduled out before the smp call arrived.
574 if (ctx->task && cpuctx->task_ctx != ctx)
577 spin_lock_irqsave(&ctx->lock, flags);
578 update_context_time(ctx);
581 * Protect the list operation against NMI by disabling the
582 * counters on a global level. NOP for non NMI based counters.
586 add_counter_to_ctx(counter, ctx);
589 * Don't put the counter on if it is disabled or if
590 * it is in a group and the group isn't on.
592 if (counter->state != PERF_COUNTER_STATE_INACTIVE ||
593 (leader != counter && leader->state != PERF_COUNTER_STATE_ACTIVE))
597 * An exclusive counter can't go on if there are already active
598 * hardware counters, and no hardware counter can go on if there
599 * is already an exclusive counter on.
601 if (!group_can_go_on(counter, cpuctx, 1))
604 err = counter_sched_in(counter, cpuctx, ctx, cpu);
608 * This counter couldn't go on. If it is in a group
609 * then we have to pull the whole group off.
610 * If the counter group is pinned then put it in error state.
612 if (leader != counter)
613 group_sched_out(leader, cpuctx, ctx);
614 if (leader->hw_event.pinned) {
615 update_group_times(leader);
616 leader->state = PERF_COUNTER_STATE_ERROR;
620 if (!err && !ctx->task && cpuctx->max_pertask)
621 cpuctx->max_pertask--;
626 spin_unlock_irqrestore(&ctx->lock, flags);
630 * Attach a performance counter to a context
632 * First we add the counter to the list with the hardware enable bit
633 * in counter->hw_config cleared.
635 * If the counter is attached to a task which is on a CPU we use a smp
636 * call to enable it in the task context. The task might have been
637 * scheduled away, but we check this in the smp call again.
639 * Must be called with ctx->mutex held.
642 perf_install_in_context(struct perf_counter_context *ctx,
643 struct perf_counter *counter,
646 struct task_struct *task = ctx->task;
650 * Per cpu counters are installed via an smp call and
651 * the install is always sucessful.
653 smp_call_function_single(cpu, __perf_install_in_context,
658 counter->task = task;
660 task_oncpu_function_call(task, __perf_install_in_context,
663 spin_lock_irq(&ctx->lock);
665 * we need to retry the smp call.
667 if (ctx->is_active && list_empty(&counter->list_entry)) {
668 spin_unlock_irq(&ctx->lock);
673 * The lock prevents that this context is scheduled in so we
674 * can add the counter safely, if it the call above did not
677 if (list_empty(&counter->list_entry))
678 add_counter_to_ctx(counter, ctx);
679 spin_unlock_irq(&ctx->lock);
683 * Cross CPU call to enable a performance counter
685 static void __perf_counter_enable(void *info)
687 struct perf_counter *counter = info;
688 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
689 struct perf_counter_context *ctx = counter->ctx;
690 struct perf_counter *leader = counter->group_leader;
695 * If this is a per-task counter, need to check whether this
696 * counter's task is the current task on this cpu.
698 if (ctx->task && cpuctx->task_ctx != ctx)
701 spin_lock_irqsave(&ctx->lock, flags);
702 update_context_time(ctx);
704 counter->prev_state = counter->state;
705 if (counter->state >= PERF_COUNTER_STATE_INACTIVE)
707 counter->state = PERF_COUNTER_STATE_INACTIVE;
708 counter->tstamp_enabled = ctx->time - counter->total_time_enabled;
711 * If the counter is in a group and isn't the group leader,
712 * then don't put it on unless the group is on.
714 if (leader != counter && leader->state != PERF_COUNTER_STATE_ACTIVE)
717 if (!group_can_go_on(counter, cpuctx, 1)) {
721 if (counter == leader)
722 err = group_sched_in(counter, cpuctx, ctx,
725 err = counter_sched_in(counter, cpuctx, ctx,
732 * If this counter can't go on and it's part of a
733 * group, then the whole group has to come off.
735 if (leader != counter)
736 group_sched_out(leader, cpuctx, ctx);
737 if (leader->hw_event.pinned) {
738 update_group_times(leader);
739 leader->state = PERF_COUNTER_STATE_ERROR;
744 spin_unlock_irqrestore(&ctx->lock, flags);
750 static void perf_counter_enable(struct perf_counter *counter)
752 struct perf_counter_context *ctx = counter->ctx;
753 struct task_struct *task = ctx->task;
757 * Enable the counter on the cpu that it's on
759 smp_call_function_single(counter->cpu, __perf_counter_enable,
764 spin_lock_irq(&ctx->lock);
765 if (counter->state >= PERF_COUNTER_STATE_INACTIVE)
769 * If the counter is in error state, clear that first.
770 * That way, if we see the counter in error state below, we
771 * know that it has gone back into error state, as distinct
772 * from the task having been scheduled away before the
773 * cross-call arrived.
775 if (counter->state == PERF_COUNTER_STATE_ERROR)
776 counter->state = PERF_COUNTER_STATE_OFF;
779 spin_unlock_irq(&ctx->lock);
780 task_oncpu_function_call(task, __perf_counter_enable, counter);
782 spin_lock_irq(&ctx->lock);
785 * If the context is active and the counter is still off,
786 * we need to retry the cross-call.
788 if (ctx->is_active && counter->state == PERF_COUNTER_STATE_OFF)
792 * Since we have the lock this context can't be scheduled
793 * in, so we can change the state safely.
795 if (counter->state == PERF_COUNTER_STATE_OFF) {
796 counter->state = PERF_COUNTER_STATE_INACTIVE;
797 counter->tstamp_enabled =
798 ctx->time - counter->total_time_enabled;
801 spin_unlock_irq(&ctx->lock);
804 static int perf_counter_refresh(struct perf_counter *counter, int refresh)
807 * not supported on inherited counters
809 if (counter->hw_event.inherit)
812 atomic_add(refresh, &counter->event_limit);
813 perf_counter_enable(counter);
818 void __perf_counter_sched_out(struct perf_counter_context *ctx,
819 struct perf_cpu_context *cpuctx)
821 struct perf_counter *counter;
823 spin_lock(&ctx->lock);
825 if (likely(!ctx->nr_counters))
827 update_context_time(ctx);
830 if (ctx->nr_active) {
831 list_for_each_entry(counter, &ctx->counter_list, list_entry)
832 group_sched_out(counter, cpuctx, ctx);
836 spin_unlock(&ctx->lock);
840 * Called from scheduler to remove the counters of the current task,
841 * with interrupts disabled.
843 * We stop each counter and update the counter value in counter->count.
845 * This does not protect us against NMI, but disable()
846 * sets the disabled bit in the control field of counter _before_
847 * accessing the counter control register. If a NMI hits, then it will
848 * not restart the counter.
850 void perf_counter_task_sched_out(struct task_struct *task, int cpu)
852 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
853 struct perf_counter_context *ctx = &task->perf_counter_ctx;
854 struct pt_regs *regs;
856 if (likely(!cpuctx->task_ctx))
859 update_context_time(ctx);
861 regs = task_pt_regs(task);
862 perf_swcounter_event(PERF_COUNT_CONTEXT_SWITCHES, 1, 1, regs, 0);
863 __perf_counter_sched_out(ctx, cpuctx);
865 cpuctx->task_ctx = NULL;
868 static void __perf_counter_task_sched_out(struct perf_counter_context *ctx)
870 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
872 __perf_counter_sched_out(ctx, cpuctx);
873 cpuctx->task_ctx = NULL;
876 static void perf_counter_cpu_sched_out(struct perf_cpu_context *cpuctx)
878 __perf_counter_sched_out(&cpuctx->ctx, cpuctx);
882 __perf_counter_sched_in(struct perf_counter_context *ctx,
883 struct perf_cpu_context *cpuctx, int cpu)
885 struct perf_counter *counter;
888 spin_lock(&ctx->lock);
890 if (likely(!ctx->nr_counters))
893 ctx->timestamp = perf_clock();
898 * First go through the list and put on any pinned groups
899 * in order to give them the best chance of going on.
901 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
902 if (counter->state <= PERF_COUNTER_STATE_OFF ||
903 !counter->hw_event.pinned)
905 if (counter->cpu != -1 && counter->cpu != cpu)
908 if (group_can_go_on(counter, cpuctx, 1))
909 group_sched_in(counter, cpuctx, ctx, cpu);
912 * If this pinned group hasn't been scheduled,
913 * put it in error state.
915 if (counter->state == PERF_COUNTER_STATE_INACTIVE) {
916 update_group_times(counter);
917 counter->state = PERF_COUNTER_STATE_ERROR;
921 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
923 * Ignore counters in OFF or ERROR state, and
924 * ignore pinned counters since we did them already.
926 if (counter->state <= PERF_COUNTER_STATE_OFF ||
927 counter->hw_event.pinned)
931 * Listen to the 'cpu' scheduling filter constraint
934 if (counter->cpu != -1 && counter->cpu != cpu)
937 if (group_can_go_on(counter, cpuctx, can_add_hw)) {
938 if (group_sched_in(counter, cpuctx, ctx, cpu))
944 spin_unlock(&ctx->lock);
948 * Called from scheduler to add the counters of the current task
949 * with interrupts disabled.
951 * We restore the counter value and then enable it.
953 * This does not protect us against NMI, but enable()
954 * sets the enabled bit in the control field of counter _before_
955 * accessing the counter control register. If a NMI hits, then it will
956 * keep the counter running.
958 void perf_counter_task_sched_in(struct task_struct *task, int cpu)
960 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
961 struct perf_counter_context *ctx = &task->perf_counter_ctx;
963 __perf_counter_sched_in(ctx, cpuctx, cpu);
964 cpuctx->task_ctx = ctx;
967 static void perf_counter_cpu_sched_in(struct perf_cpu_context *cpuctx, int cpu)
969 struct perf_counter_context *ctx = &cpuctx->ctx;
971 __perf_counter_sched_in(ctx, cpuctx, cpu);
974 int perf_counter_task_disable(void)
976 struct task_struct *curr = current;
977 struct perf_counter_context *ctx = &curr->perf_counter_ctx;
978 struct perf_counter *counter;
981 if (likely(!ctx->nr_counters))
984 local_irq_save(flags);
986 __perf_counter_task_sched_out(ctx);
988 spin_lock(&ctx->lock);
991 * Disable all the counters:
995 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
996 if (counter->state != PERF_COUNTER_STATE_ERROR) {
997 update_group_times(counter);
998 counter->state = PERF_COUNTER_STATE_OFF;
1004 spin_unlock_irqrestore(&ctx->lock, flags);
1009 int perf_counter_task_enable(void)
1011 struct task_struct *curr = current;
1012 struct perf_counter_context *ctx = &curr->perf_counter_ctx;
1013 struct perf_counter *counter;
1014 unsigned long flags;
1017 if (likely(!ctx->nr_counters))
1020 local_irq_save(flags);
1021 cpu = smp_processor_id();
1023 __perf_counter_task_sched_out(ctx);
1025 spin_lock(&ctx->lock);
1028 * Disable all the counters:
1032 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
1033 if (counter->state > PERF_COUNTER_STATE_OFF)
1035 counter->state = PERF_COUNTER_STATE_INACTIVE;
1036 counter->tstamp_enabled =
1037 ctx->time - counter->total_time_enabled;
1038 counter->hw_event.disabled = 0;
1042 spin_unlock(&ctx->lock);
1044 perf_counter_task_sched_in(curr, cpu);
1046 local_irq_restore(flags);
1052 * Round-robin a context's counters:
1054 static void rotate_ctx(struct perf_counter_context *ctx)
1056 struct perf_counter *counter;
1058 if (!ctx->nr_counters)
1061 spin_lock(&ctx->lock);
1063 * Rotate the first entry last (works just fine for group counters too):
1066 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
1067 list_move_tail(&counter->list_entry, &ctx->counter_list);
1072 spin_unlock(&ctx->lock);
1075 void perf_counter_task_tick(struct task_struct *curr, int cpu)
1077 struct perf_cpu_context *cpuctx;
1078 struct perf_counter_context *ctx;
1080 if (!atomic_read(&nr_counters))
1083 cpuctx = &per_cpu(perf_cpu_context, cpu);
1084 ctx = &curr->perf_counter_ctx;
1086 perf_counter_cpu_sched_out(cpuctx);
1087 __perf_counter_task_sched_out(ctx);
1089 rotate_ctx(&cpuctx->ctx);
1092 perf_counter_cpu_sched_in(cpuctx, cpu);
1093 perf_counter_task_sched_in(curr, cpu);
1097 * Cross CPU call to read the hardware counter
1099 static void __read(void *info)
1101 struct perf_counter *counter = info;
1102 struct perf_counter_context *ctx = counter->ctx;
1103 unsigned long flags;
1105 local_irq_save(flags);
1107 update_context_time(ctx);
1108 counter->pmu->read(counter);
1109 update_counter_times(counter);
1110 local_irq_restore(flags);
1113 static u64 perf_counter_read(struct perf_counter *counter)
1116 * If counter is enabled and currently active on a CPU, update the
1117 * value in the counter structure:
1119 if (counter->state == PERF_COUNTER_STATE_ACTIVE) {
1120 smp_call_function_single(counter->oncpu,
1121 __read, counter, 1);
1122 } else if (counter->state == PERF_COUNTER_STATE_INACTIVE) {
1123 update_counter_times(counter);
1126 return atomic64_read(&counter->count);
1129 static void put_context(struct perf_counter_context *ctx)
1132 put_task_struct(ctx->task);
1135 static struct perf_counter_context *find_get_context(pid_t pid, int cpu)
1137 struct perf_cpu_context *cpuctx;
1138 struct perf_counter_context *ctx;
1139 struct task_struct *task;
1142 * If cpu is not a wildcard then this is a percpu counter:
1145 /* Must be root to operate on a CPU counter: */
1146 if (sysctl_perf_counter_priv && !capable(CAP_SYS_ADMIN))
1147 return ERR_PTR(-EACCES);
1149 if (cpu < 0 || cpu > num_possible_cpus())
1150 return ERR_PTR(-EINVAL);
1153 * We could be clever and allow to attach a counter to an
1154 * offline CPU and activate it when the CPU comes up, but
1157 if (!cpu_isset(cpu, cpu_online_map))
1158 return ERR_PTR(-ENODEV);
1160 cpuctx = &per_cpu(perf_cpu_context, cpu);
1170 task = find_task_by_vpid(pid);
1172 get_task_struct(task);
1176 return ERR_PTR(-ESRCH);
1178 ctx = &task->perf_counter_ctx;
1181 /* Reuse ptrace permission checks for now. */
1182 if (!ptrace_may_access(task, PTRACE_MODE_READ)) {
1184 return ERR_PTR(-EACCES);
1190 static void free_counter_rcu(struct rcu_head *head)
1192 struct perf_counter *counter;
1194 counter = container_of(head, struct perf_counter, rcu_head);
1198 static void perf_pending_sync(struct perf_counter *counter);
1200 static void free_counter(struct perf_counter *counter)
1202 perf_pending_sync(counter);
1204 atomic_dec(&nr_counters);
1205 if (counter->hw_event.mmap)
1206 atomic_dec(&nr_mmap_tracking);
1207 if (counter->hw_event.munmap)
1208 atomic_dec(&nr_munmap_tracking);
1209 if (counter->hw_event.comm)
1210 atomic_dec(&nr_comm_tracking);
1212 if (counter->destroy)
1213 counter->destroy(counter);
1215 call_rcu(&counter->rcu_head, free_counter_rcu);
1219 * Called when the last reference to the file is gone.
1221 static int perf_release(struct inode *inode, struct file *file)
1223 struct perf_counter *counter = file->private_data;
1224 struct perf_counter_context *ctx = counter->ctx;
1226 file->private_data = NULL;
1228 mutex_lock(&ctx->mutex);
1229 mutex_lock(&counter->mutex);
1231 perf_counter_remove_from_context(counter);
1233 mutex_unlock(&counter->mutex);
1234 mutex_unlock(&ctx->mutex);
1236 free_counter(counter);
1243 * Read the performance counter - simple non blocking version for now
1246 perf_read_hw(struct perf_counter *counter, char __user *buf, size_t count)
1252 * Return end-of-file for a read on a counter that is in
1253 * error state (i.e. because it was pinned but it couldn't be
1254 * scheduled on to the CPU at some point).
1256 if (counter->state == PERF_COUNTER_STATE_ERROR)
1259 mutex_lock(&counter->mutex);
1260 values[0] = perf_counter_read(counter);
1262 if (counter->hw_event.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
1263 values[n++] = counter->total_time_enabled +
1264 atomic64_read(&counter->child_total_time_enabled);
1265 if (counter->hw_event.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
1266 values[n++] = counter->total_time_running +
1267 atomic64_read(&counter->child_total_time_running);
1268 mutex_unlock(&counter->mutex);
1270 if (count < n * sizeof(u64))
1272 count = n * sizeof(u64);
1274 if (copy_to_user(buf, values, count))
1281 perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
1283 struct perf_counter *counter = file->private_data;
1285 return perf_read_hw(counter, buf, count);
1288 static unsigned int perf_poll(struct file *file, poll_table *wait)
1290 struct perf_counter *counter = file->private_data;
1291 struct perf_mmap_data *data;
1292 unsigned int events = POLL_HUP;
1295 data = rcu_dereference(counter->data);
1297 events = atomic_xchg(&data->poll, 0);
1300 poll_wait(file, &counter->waitq, wait);
1305 static void perf_counter_reset(struct perf_counter *counter)
1307 (void)perf_counter_read(counter);
1308 atomic64_set(&counter->count, 0);
1309 perf_counter_update_userpage(counter);
1312 static void perf_counter_for_each_sibling(struct perf_counter *counter,
1313 void (*func)(struct perf_counter *))
1315 struct perf_counter_context *ctx = counter->ctx;
1316 struct perf_counter *sibling;
1318 spin_lock_irq(&ctx->lock);
1319 counter = counter->group_leader;
1322 list_for_each_entry(sibling, &counter->sibling_list, list_entry)
1324 spin_unlock_irq(&ctx->lock);
1327 static void perf_counter_for_each_child(struct perf_counter *counter,
1328 void (*func)(struct perf_counter *))
1330 struct perf_counter *child;
1332 mutex_lock(&counter->mutex);
1334 list_for_each_entry(child, &counter->child_list, child_list)
1336 mutex_unlock(&counter->mutex);
1339 static void perf_counter_for_each(struct perf_counter *counter,
1340 void (*func)(struct perf_counter *))
1342 struct perf_counter *child;
1344 mutex_lock(&counter->mutex);
1345 perf_counter_for_each_sibling(counter, func);
1346 list_for_each_entry(child, &counter->child_list, child_list)
1347 perf_counter_for_each_sibling(child, func);
1348 mutex_unlock(&counter->mutex);
1351 static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1353 struct perf_counter *counter = file->private_data;
1354 void (*func)(struct perf_counter *);
1358 case PERF_COUNTER_IOC_ENABLE:
1359 func = perf_counter_enable;
1361 case PERF_COUNTER_IOC_DISABLE:
1362 func = perf_counter_disable;
1364 case PERF_COUNTER_IOC_RESET:
1365 func = perf_counter_reset;
1368 case PERF_COUNTER_IOC_REFRESH:
1369 return perf_counter_refresh(counter, arg);
1374 if (flags & PERF_IOC_FLAG_GROUP)
1375 perf_counter_for_each(counter, func);
1377 perf_counter_for_each_child(counter, func);
1383 * Callers need to ensure there can be no nesting of this function, otherwise
1384 * the seqlock logic goes bad. We can not serialize this because the arch
1385 * code calls this from NMI context.
1387 void perf_counter_update_userpage(struct perf_counter *counter)
1389 struct perf_mmap_data *data;
1390 struct perf_counter_mmap_page *userpg;
1393 data = rcu_dereference(counter->data);
1397 userpg = data->user_page;
1400 * Disable preemption so as to not let the corresponding user-space
1401 * spin too long if we get preempted.
1406 userpg->index = counter->hw.idx;
1407 userpg->offset = atomic64_read(&counter->count);
1408 if (counter->state == PERF_COUNTER_STATE_ACTIVE)
1409 userpg->offset -= atomic64_read(&counter->hw.prev_count);
1418 static int perf_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1420 struct perf_counter *counter = vma->vm_file->private_data;
1421 struct perf_mmap_data *data;
1422 int ret = VM_FAULT_SIGBUS;
1425 data = rcu_dereference(counter->data);
1429 if (vmf->pgoff == 0) {
1430 vmf->page = virt_to_page(data->user_page);
1432 int nr = vmf->pgoff - 1;
1434 if ((unsigned)nr > data->nr_pages)
1437 vmf->page = virt_to_page(data->data_pages[nr]);
1439 get_page(vmf->page);
1447 static int perf_mmap_data_alloc(struct perf_counter *counter, int nr_pages)
1449 struct perf_mmap_data *data;
1453 WARN_ON(atomic_read(&counter->mmap_count));
1455 size = sizeof(struct perf_mmap_data);
1456 size += nr_pages * sizeof(void *);
1458 data = kzalloc(size, GFP_KERNEL);
1462 data->user_page = (void *)get_zeroed_page(GFP_KERNEL);
1463 if (!data->user_page)
1464 goto fail_user_page;
1466 for (i = 0; i < nr_pages; i++) {
1467 data->data_pages[i] = (void *)get_zeroed_page(GFP_KERNEL);
1468 if (!data->data_pages[i])
1469 goto fail_data_pages;
1472 data->nr_pages = nr_pages;
1473 atomic_set(&data->lock, -1);
1475 rcu_assign_pointer(counter->data, data);
1480 for (i--; i >= 0; i--)
1481 free_page((unsigned long)data->data_pages[i]);
1483 free_page((unsigned long)data->user_page);
1492 static void __perf_mmap_data_free(struct rcu_head *rcu_head)
1494 struct perf_mmap_data *data = container_of(rcu_head,
1495 struct perf_mmap_data, rcu_head);
1498 free_page((unsigned long)data->user_page);
1499 for (i = 0; i < data->nr_pages; i++)
1500 free_page((unsigned long)data->data_pages[i]);
1504 static void perf_mmap_data_free(struct perf_counter *counter)
1506 struct perf_mmap_data *data = counter->data;
1508 WARN_ON(atomic_read(&counter->mmap_count));
1510 rcu_assign_pointer(counter->data, NULL);
1511 call_rcu(&data->rcu_head, __perf_mmap_data_free);
1514 static void perf_mmap_open(struct vm_area_struct *vma)
1516 struct perf_counter *counter = vma->vm_file->private_data;
1518 atomic_inc(&counter->mmap_count);
1521 static void perf_mmap_close(struct vm_area_struct *vma)
1523 struct perf_counter *counter = vma->vm_file->private_data;
1525 if (atomic_dec_and_mutex_lock(&counter->mmap_count,
1526 &counter->mmap_mutex)) {
1527 vma->vm_mm->locked_vm -= counter->data->nr_locked;
1528 perf_mmap_data_free(counter);
1529 mutex_unlock(&counter->mmap_mutex);
1533 static struct vm_operations_struct perf_mmap_vmops = {
1534 .open = perf_mmap_open,
1535 .close = perf_mmap_close,
1536 .fault = perf_mmap_fault,
1539 static int perf_mmap(struct file *file, struct vm_area_struct *vma)
1541 struct perf_counter *counter = file->private_data;
1542 unsigned long vma_size;
1543 unsigned long nr_pages;
1544 unsigned long locked, lock_limit;
1548 if (!(vma->vm_flags & VM_SHARED) || (vma->vm_flags & VM_WRITE))
1551 vma_size = vma->vm_end - vma->vm_start;
1552 nr_pages = (vma_size / PAGE_SIZE) - 1;
1555 * If we have data pages ensure they're a power-of-two number, so we
1556 * can do bitmasks instead of modulo.
1558 if (nr_pages != 0 && !is_power_of_2(nr_pages))
1561 if (vma_size != PAGE_SIZE * (1 + nr_pages))
1564 if (vma->vm_pgoff != 0)
1567 mutex_lock(&counter->mmap_mutex);
1568 if (atomic_inc_not_zero(&counter->mmap_count)) {
1569 if (nr_pages != counter->data->nr_pages)
1574 extra = nr_pages /* + 1 only account the data pages */;
1575 extra -= sysctl_perf_counter_mlock >> (PAGE_SHIFT - 10);
1579 locked = vma->vm_mm->locked_vm + extra;
1581 lock_limit = current->signal->rlim[RLIMIT_MEMLOCK].rlim_cur;
1582 lock_limit >>= PAGE_SHIFT;
1584 if ((locked > lock_limit) && !capable(CAP_IPC_LOCK)) {
1589 WARN_ON(counter->data);
1590 ret = perf_mmap_data_alloc(counter, nr_pages);
1594 atomic_set(&counter->mmap_count, 1);
1595 vma->vm_mm->locked_vm += extra;
1596 counter->data->nr_locked = extra;
1598 mutex_unlock(&counter->mmap_mutex);
1600 vma->vm_flags &= ~VM_MAYWRITE;
1601 vma->vm_flags |= VM_RESERVED;
1602 vma->vm_ops = &perf_mmap_vmops;
1607 static int perf_fasync(int fd, struct file *filp, int on)
1609 struct perf_counter *counter = filp->private_data;
1610 struct inode *inode = filp->f_path.dentry->d_inode;
1613 mutex_lock(&inode->i_mutex);
1614 retval = fasync_helper(fd, filp, on, &counter->fasync);
1615 mutex_unlock(&inode->i_mutex);
1623 static const struct file_operations perf_fops = {
1624 .release = perf_release,
1627 .unlocked_ioctl = perf_ioctl,
1628 .compat_ioctl = perf_ioctl,
1630 .fasync = perf_fasync,
1634 * Perf counter wakeup
1636 * If there's data, ensure we set the poll() state and publish everything
1637 * to user-space before waking everybody up.
1640 void perf_counter_wakeup(struct perf_counter *counter)
1642 wake_up_all(&counter->waitq);
1644 if (counter->pending_kill) {
1645 kill_fasync(&counter->fasync, SIGIO, counter->pending_kill);
1646 counter->pending_kill = 0;
1653 * Handle the case where we need to wakeup up from NMI (or rq->lock) context.
1655 * The NMI bit means we cannot possibly take locks. Therefore, maintain a
1656 * single linked list and use cmpxchg() to add entries lockless.
1659 static void perf_pending_counter(struct perf_pending_entry *entry)
1661 struct perf_counter *counter = container_of(entry,
1662 struct perf_counter, pending);
1664 if (counter->pending_disable) {
1665 counter->pending_disable = 0;
1666 perf_counter_disable(counter);
1669 if (counter->pending_wakeup) {
1670 counter->pending_wakeup = 0;
1671 perf_counter_wakeup(counter);
1675 #define PENDING_TAIL ((struct perf_pending_entry *)-1UL)
1677 static DEFINE_PER_CPU(struct perf_pending_entry *, perf_pending_head) = {
1681 static void perf_pending_queue(struct perf_pending_entry *entry,
1682 void (*func)(struct perf_pending_entry *))
1684 struct perf_pending_entry **head;
1686 if (cmpxchg(&entry->next, NULL, PENDING_TAIL) != NULL)
1691 head = &get_cpu_var(perf_pending_head);
1694 entry->next = *head;
1695 } while (cmpxchg(head, entry->next, entry) != entry->next);
1697 set_perf_counter_pending();
1699 put_cpu_var(perf_pending_head);
1702 static int __perf_pending_run(void)
1704 struct perf_pending_entry *list;
1707 list = xchg(&__get_cpu_var(perf_pending_head), PENDING_TAIL);
1708 while (list != PENDING_TAIL) {
1709 void (*func)(struct perf_pending_entry *);
1710 struct perf_pending_entry *entry = list;
1717 * Ensure we observe the unqueue before we issue the wakeup,
1718 * so that we won't be waiting forever.
1719 * -- see perf_not_pending().
1730 static inline int perf_not_pending(struct perf_counter *counter)
1733 * If we flush on whatever cpu we run, there is a chance we don't
1737 __perf_pending_run();
1741 * Ensure we see the proper queue state before going to sleep
1742 * so that we do not miss the wakeup. -- see perf_pending_handle()
1745 return counter->pending.next == NULL;
1748 static void perf_pending_sync(struct perf_counter *counter)
1750 wait_event(counter->waitq, perf_not_pending(counter));
1753 void perf_counter_do_pending(void)
1755 __perf_pending_run();
1759 * Callchain support -- arch specific
1762 __weak struct perf_callchain_entry *perf_callchain(struct pt_regs *regs)
1771 struct perf_output_handle {
1772 struct perf_counter *counter;
1773 struct perf_mmap_data *data;
1774 unsigned int offset;
1779 unsigned long flags;
1782 static void perf_output_wakeup(struct perf_output_handle *handle)
1784 atomic_set(&handle->data->poll, POLL_IN);
1787 handle->counter->pending_wakeup = 1;
1788 perf_pending_queue(&handle->counter->pending,
1789 perf_pending_counter);
1791 perf_counter_wakeup(handle->counter);
1795 * Curious locking construct.
1797 * We need to ensure a later event doesn't publish a head when a former
1798 * event isn't done writing. However since we need to deal with NMIs we
1799 * cannot fully serialize things.
1801 * What we do is serialize between CPUs so we only have to deal with NMI
1802 * nesting on a single CPU.
1804 * We only publish the head (and generate a wakeup) when the outer-most
1807 static void perf_output_lock(struct perf_output_handle *handle)
1809 struct perf_mmap_data *data = handle->data;
1814 local_irq_save(handle->flags);
1815 cpu = smp_processor_id();
1817 if (in_nmi() && atomic_read(&data->lock) == cpu)
1820 while (atomic_cmpxchg(&data->lock, -1, cpu) != -1)
1826 static void perf_output_unlock(struct perf_output_handle *handle)
1828 struct perf_mmap_data *data = handle->data;
1831 data->done_head = data->head;
1833 if (!handle->locked)
1838 * The xchg implies a full barrier that ensures all writes are done
1839 * before we publish the new head, matched by a rmb() in userspace when
1840 * reading this position.
1842 while ((head = atomic_xchg(&data->done_head, 0)))
1843 data->user_page->data_head = head;
1846 * NMI can happen here, which means we can miss a done_head update.
1849 cpu = atomic_xchg(&data->lock, -1);
1850 WARN_ON_ONCE(cpu != smp_processor_id());
1853 * Therefore we have to validate we did not indeed do so.
1855 if (unlikely(atomic_read(&data->done_head))) {
1857 * Since we had it locked, we can lock it again.
1859 while (atomic_cmpxchg(&data->lock, -1, cpu) != -1)
1865 if (atomic_xchg(&data->wakeup, 0))
1866 perf_output_wakeup(handle);
1868 local_irq_restore(handle->flags);
1871 static int perf_output_begin(struct perf_output_handle *handle,
1872 struct perf_counter *counter, unsigned int size,
1873 int nmi, int overflow)
1875 struct perf_mmap_data *data;
1876 unsigned int offset, head;
1879 * For inherited counters we send all the output towards the parent.
1881 if (counter->parent)
1882 counter = counter->parent;
1885 data = rcu_dereference(counter->data);
1889 handle->data = data;
1890 handle->counter = counter;
1892 handle->overflow = overflow;
1894 if (!data->nr_pages)
1897 perf_output_lock(handle);
1900 offset = head = atomic_read(&data->head);
1902 } while (atomic_cmpxchg(&data->head, offset, head) != offset);
1904 handle->offset = offset;
1905 handle->head = head;
1907 if ((offset >> PAGE_SHIFT) != (head >> PAGE_SHIFT))
1908 atomic_set(&data->wakeup, 1);
1913 perf_output_wakeup(handle);
1920 static void perf_output_copy(struct perf_output_handle *handle,
1921 void *buf, unsigned int len)
1923 unsigned int pages_mask;
1924 unsigned int offset;
1928 offset = handle->offset;
1929 pages_mask = handle->data->nr_pages - 1;
1930 pages = handle->data->data_pages;
1933 unsigned int page_offset;
1936 nr = (offset >> PAGE_SHIFT) & pages_mask;
1937 page_offset = offset & (PAGE_SIZE - 1);
1938 size = min_t(unsigned int, PAGE_SIZE - page_offset, len);
1940 memcpy(pages[nr] + page_offset, buf, size);
1947 handle->offset = offset;
1950 * Check we didn't copy past our reservation window, taking the
1951 * possible unsigned int wrap into account.
1953 WARN_ON_ONCE(((int)(handle->head - handle->offset)) < 0);
1956 #define perf_output_put(handle, x) \
1957 perf_output_copy((handle), &(x), sizeof(x))
1959 static void perf_output_end(struct perf_output_handle *handle)
1961 struct perf_counter *counter = handle->counter;
1962 struct perf_mmap_data *data = handle->data;
1964 int wakeup_events = counter->hw_event.wakeup_events;
1966 if (handle->overflow && wakeup_events) {
1967 int events = atomic_inc_return(&data->events);
1968 if (events >= wakeup_events) {
1969 atomic_sub(wakeup_events, &data->events);
1970 atomic_set(&data->wakeup, 1);
1974 perf_output_unlock(handle);
1978 static void perf_counter_output(struct perf_counter *counter,
1979 int nmi, struct pt_regs *regs, u64 addr)
1982 u64 record_type = counter->hw_event.record_type;
1983 struct perf_output_handle handle;
1984 struct perf_event_header header;
1993 struct perf_callchain_entry *callchain = NULL;
1994 int callchain_size = 0;
2001 header.size = sizeof(header);
2003 header.misc = PERF_EVENT_MISC_OVERFLOW;
2004 header.misc |= user_mode(regs) ?
2005 PERF_EVENT_MISC_USER : PERF_EVENT_MISC_KERNEL;
2007 if (record_type & PERF_RECORD_IP) {
2008 ip = instruction_pointer(regs);
2009 header.type |= PERF_RECORD_IP;
2010 header.size += sizeof(ip);
2013 if (record_type & PERF_RECORD_TID) {
2014 /* namespace issues */
2015 tid_entry.pid = current->group_leader->pid;
2016 tid_entry.tid = current->pid;
2018 header.type |= PERF_RECORD_TID;
2019 header.size += sizeof(tid_entry);
2022 if (record_type & PERF_RECORD_TIME) {
2024 * Maybe do better on x86 and provide cpu_clock_nmi()
2026 time = sched_clock();
2028 header.type |= PERF_RECORD_TIME;
2029 header.size += sizeof(u64);
2032 if (record_type & PERF_RECORD_ADDR) {
2033 header.type |= PERF_RECORD_ADDR;
2034 header.size += sizeof(u64);
2037 if (record_type & PERF_RECORD_CONFIG) {
2038 header.type |= PERF_RECORD_CONFIG;
2039 header.size += sizeof(u64);
2042 if (record_type & PERF_RECORD_CPU) {
2043 header.type |= PERF_RECORD_CPU;
2044 header.size += sizeof(cpu_entry);
2046 cpu_entry.cpu = raw_smp_processor_id();
2049 if (record_type & PERF_RECORD_GROUP) {
2050 header.type |= PERF_RECORD_GROUP;
2051 header.size += sizeof(u64) +
2052 counter->nr_siblings * sizeof(group_entry);
2055 if (record_type & PERF_RECORD_CALLCHAIN) {
2056 callchain = perf_callchain(regs);
2059 callchain_size = (1 + callchain->nr) * sizeof(u64);
2061 header.type |= PERF_RECORD_CALLCHAIN;
2062 header.size += callchain_size;
2066 ret = perf_output_begin(&handle, counter, header.size, nmi, 1);
2070 perf_output_put(&handle, header);
2072 if (record_type & PERF_RECORD_IP)
2073 perf_output_put(&handle, ip);
2075 if (record_type & PERF_RECORD_TID)
2076 perf_output_put(&handle, tid_entry);
2078 if (record_type & PERF_RECORD_TIME)
2079 perf_output_put(&handle, time);
2081 if (record_type & PERF_RECORD_ADDR)
2082 perf_output_put(&handle, addr);
2084 if (record_type & PERF_RECORD_CONFIG)
2085 perf_output_put(&handle, counter->hw_event.config);
2087 if (record_type & PERF_RECORD_CPU)
2088 perf_output_put(&handle, cpu_entry);
2091 * XXX PERF_RECORD_GROUP vs inherited counters seems difficult.
2093 if (record_type & PERF_RECORD_GROUP) {
2094 struct perf_counter *leader, *sub;
2095 u64 nr = counter->nr_siblings;
2097 perf_output_put(&handle, nr);
2099 leader = counter->group_leader;
2100 list_for_each_entry(sub, &leader->sibling_list, list_entry) {
2102 sub->pmu->read(sub);
2104 group_entry.event = sub->hw_event.config;
2105 group_entry.counter = atomic64_read(&sub->count);
2107 perf_output_put(&handle, group_entry);
2112 perf_output_copy(&handle, callchain, callchain_size);
2114 perf_output_end(&handle);
2121 struct perf_comm_event {
2122 struct task_struct *task;
2127 struct perf_event_header header;
2134 static void perf_counter_comm_output(struct perf_counter *counter,
2135 struct perf_comm_event *comm_event)
2137 struct perf_output_handle handle;
2138 int size = comm_event->event.header.size;
2139 int ret = perf_output_begin(&handle, counter, size, 0, 0);
2144 perf_output_put(&handle, comm_event->event);
2145 perf_output_copy(&handle, comm_event->comm,
2146 comm_event->comm_size);
2147 perf_output_end(&handle);
2150 static int perf_counter_comm_match(struct perf_counter *counter,
2151 struct perf_comm_event *comm_event)
2153 if (counter->hw_event.comm &&
2154 comm_event->event.header.type == PERF_EVENT_COMM)
2160 static void perf_counter_comm_ctx(struct perf_counter_context *ctx,
2161 struct perf_comm_event *comm_event)
2163 struct perf_counter *counter;
2165 if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
2169 list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
2170 if (perf_counter_comm_match(counter, comm_event))
2171 perf_counter_comm_output(counter, comm_event);
2176 static void perf_counter_comm_event(struct perf_comm_event *comm_event)
2178 struct perf_cpu_context *cpuctx;
2180 char *comm = comm_event->task->comm;
2182 size = ALIGN(strlen(comm)+1, sizeof(u64));
2184 comm_event->comm = comm;
2185 comm_event->comm_size = size;
2187 comm_event->event.header.size = sizeof(comm_event->event) + size;
2189 cpuctx = &get_cpu_var(perf_cpu_context);
2190 perf_counter_comm_ctx(&cpuctx->ctx, comm_event);
2191 put_cpu_var(perf_cpu_context);
2193 perf_counter_comm_ctx(¤t->perf_counter_ctx, comm_event);
2196 void perf_counter_comm(struct task_struct *task)
2198 struct perf_comm_event comm_event;
2200 if (!atomic_read(&nr_comm_tracking))
2203 comm_event = (struct perf_comm_event){
2206 .header = { .type = PERF_EVENT_COMM, },
2207 .pid = task->group_leader->pid,
2212 perf_counter_comm_event(&comm_event);
2219 struct perf_mmap_event {
2225 struct perf_event_header header;
2235 static void perf_counter_mmap_output(struct perf_counter *counter,
2236 struct perf_mmap_event *mmap_event)
2238 struct perf_output_handle handle;
2239 int size = mmap_event->event.header.size;
2240 int ret = perf_output_begin(&handle, counter, size, 0, 0);
2245 perf_output_put(&handle, mmap_event->event);
2246 perf_output_copy(&handle, mmap_event->file_name,
2247 mmap_event->file_size);
2248 perf_output_end(&handle);
2251 static int perf_counter_mmap_match(struct perf_counter *counter,
2252 struct perf_mmap_event *mmap_event)
2254 if (counter->hw_event.mmap &&
2255 mmap_event->event.header.type == PERF_EVENT_MMAP)
2258 if (counter->hw_event.munmap &&
2259 mmap_event->event.header.type == PERF_EVENT_MUNMAP)
2265 static void perf_counter_mmap_ctx(struct perf_counter_context *ctx,
2266 struct perf_mmap_event *mmap_event)
2268 struct perf_counter *counter;
2270 if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
2274 list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
2275 if (perf_counter_mmap_match(counter, mmap_event))
2276 perf_counter_mmap_output(counter, mmap_event);
2281 static void perf_counter_mmap_event(struct perf_mmap_event *mmap_event)
2283 struct perf_cpu_context *cpuctx;
2284 struct file *file = mmap_event->file;
2291 buf = kzalloc(PATH_MAX, GFP_KERNEL);
2293 name = strncpy(tmp, "//enomem", sizeof(tmp));
2296 name = d_path(&file->f_path, buf, PATH_MAX);
2298 name = strncpy(tmp, "//toolong", sizeof(tmp));
2302 name = strncpy(tmp, "//anon", sizeof(tmp));
2307 size = ALIGN(strlen(name)+1, sizeof(u64));
2309 mmap_event->file_name = name;
2310 mmap_event->file_size = size;
2312 mmap_event->event.header.size = sizeof(mmap_event->event) + size;
2314 cpuctx = &get_cpu_var(perf_cpu_context);
2315 perf_counter_mmap_ctx(&cpuctx->ctx, mmap_event);
2316 put_cpu_var(perf_cpu_context);
2318 perf_counter_mmap_ctx(¤t->perf_counter_ctx, mmap_event);
2323 void perf_counter_mmap(unsigned long addr, unsigned long len,
2324 unsigned long pgoff, struct file *file)
2326 struct perf_mmap_event mmap_event;
2328 if (!atomic_read(&nr_mmap_tracking))
2331 mmap_event = (struct perf_mmap_event){
2334 .header = { .type = PERF_EVENT_MMAP, },
2335 .pid = current->group_leader->pid,
2336 .tid = current->pid,
2343 perf_counter_mmap_event(&mmap_event);
2346 void perf_counter_munmap(unsigned long addr, unsigned long len,
2347 unsigned long pgoff, struct file *file)
2349 struct perf_mmap_event mmap_event;
2351 if (!atomic_read(&nr_munmap_tracking))
2354 mmap_event = (struct perf_mmap_event){
2357 .header = { .type = PERF_EVENT_MUNMAP, },
2358 .pid = current->group_leader->pid,
2359 .tid = current->pid,
2366 perf_counter_mmap_event(&mmap_event);
2370 * Generic counter overflow handling.
2373 int perf_counter_overflow(struct perf_counter *counter,
2374 int nmi, struct pt_regs *regs, u64 addr)
2376 int events = atomic_read(&counter->event_limit);
2380 * XXX event_limit might not quite work as expected on inherited
2384 counter->pending_kill = POLL_IN;
2385 if (events && atomic_dec_and_test(&counter->event_limit)) {
2387 counter->pending_kill = POLL_HUP;
2389 counter->pending_disable = 1;
2390 perf_pending_queue(&counter->pending,
2391 perf_pending_counter);
2393 perf_counter_disable(counter);
2396 perf_counter_output(counter, nmi, regs, addr);
2401 * Generic software counter infrastructure
2404 static void perf_swcounter_update(struct perf_counter *counter)
2406 struct hw_perf_counter *hwc = &counter->hw;
2411 prev = atomic64_read(&hwc->prev_count);
2412 now = atomic64_read(&hwc->count);
2413 if (atomic64_cmpxchg(&hwc->prev_count, prev, now) != prev)
2418 atomic64_add(delta, &counter->count);
2419 atomic64_sub(delta, &hwc->period_left);
2422 static void perf_swcounter_set_period(struct perf_counter *counter)
2424 struct hw_perf_counter *hwc = &counter->hw;
2425 s64 left = atomic64_read(&hwc->period_left);
2426 s64 period = hwc->irq_period;
2428 if (unlikely(left <= -period)) {
2430 atomic64_set(&hwc->period_left, left);
2433 if (unlikely(left <= 0)) {
2435 atomic64_add(period, &hwc->period_left);
2438 atomic64_set(&hwc->prev_count, -left);
2439 atomic64_set(&hwc->count, -left);
2442 static enum hrtimer_restart perf_swcounter_hrtimer(struct hrtimer *hrtimer)
2444 enum hrtimer_restart ret = HRTIMER_RESTART;
2445 struct perf_counter *counter;
2446 struct pt_regs *regs;
2448 counter = container_of(hrtimer, struct perf_counter, hw.hrtimer);
2449 counter->pmu->read(counter);
2451 regs = get_irq_regs();
2453 * In case we exclude kernel IPs or are somehow not in interrupt
2454 * context, provide the next best thing, the user IP.
2456 if ((counter->hw_event.exclude_kernel || !regs) &&
2457 !counter->hw_event.exclude_user)
2458 regs = task_pt_regs(current);
2461 if (perf_counter_overflow(counter, 0, regs, 0))
2462 ret = HRTIMER_NORESTART;
2465 hrtimer_forward_now(hrtimer, ns_to_ktime(counter->hw.irq_period));
2470 static void perf_swcounter_overflow(struct perf_counter *counter,
2471 int nmi, struct pt_regs *regs, u64 addr)
2473 perf_swcounter_update(counter);
2474 perf_swcounter_set_period(counter);
2475 if (perf_counter_overflow(counter, nmi, regs, addr))
2476 /* soft-disable the counter */
2481 static int perf_swcounter_match(struct perf_counter *counter,
2482 enum perf_event_types type,
2483 u32 event, struct pt_regs *regs)
2485 if (counter->state != PERF_COUNTER_STATE_ACTIVE)
2488 if (perf_event_raw(&counter->hw_event))
2491 if (perf_event_type(&counter->hw_event) != type)
2494 if (perf_event_id(&counter->hw_event) != event)
2497 if (counter->hw_event.exclude_user && user_mode(regs))
2500 if (counter->hw_event.exclude_kernel && !user_mode(regs))
2506 static void perf_swcounter_add(struct perf_counter *counter, u64 nr,
2507 int nmi, struct pt_regs *regs, u64 addr)
2509 int neg = atomic64_add_negative(nr, &counter->hw.count);
2510 if (counter->hw.irq_period && !neg)
2511 perf_swcounter_overflow(counter, nmi, regs, addr);
2514 static void perf_swcounter_ctx_event(struct perf_counter_context *ctx,
2515 enum perf_event_types type, u32 event,
2516 u64 nr, int nmi, struct pt_regs *regs,
2519 struct perf_counter *counter;
2521 if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
2525 list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
2526 if (perf_swcounter_match(counter, type, event, regs))
2527 perf_swcounter_add(counter, nr, nmi, regs, addr);
2532 static int *perf_swcounter_recursion_context(struct perf_cpu_context *cpuctx)
2535 return &cpuctx->recursion[3];
2538 return &cpuctx->recursion[2];
2541 return &cpuctx->recursion[1];
2543 return &cpuctx->recursion[0];
2546 static void __perf_swcounter_event(enum perf_event_types type, u32 event,
2547 u64 nr, int nmi, struct pt_regs *regs,
2550 struct perf_cpu_context *cpuctx = &get_cpu_var(perf_cpu_context);
2551 int *recursion = perf_swcounter_recursion_context(cpuctx);
2559 perf_swcounter_ctx_event(&cpuctx->ctx, type, event,
2560 nr, nmi, regs, addr);
2561 if (cpuctx->task_ctx) {
2562 perf_swcounter_ctx_event(cpuctx->task_ctx, type, event,
2563 nr, nmi, regs, addr);
2570 put_cpu_var(perf_cpu_context);
2574 perf_swcounter_event(u32 event, u64 nr, int nmi, struct pt_regs *regs, u64 addr)
2576 __perf_swcounter_event(PERF_TYPE_SOFTWARE, event, nr, nmi, regs, addr);
2579 static void perf_swcounter_read(struct perf_counter *counter)
2581 perf_swcounter_update(counter);
2584 static int perf_swcounter_enable(struct perf_counter *counter)
2586 perf_swcounter_set_period(counter);
2590 static void perf_swcounter_disable(struct perf_counter *counter)
2592 perf_swcounter_update(counter);
2595 static const struct pmu perf_ops_generic = {
2596 .enable = perf_swcounter_enable,
2597 .disable = perf_swcounter_disable,
2598 .read = perf_swcounter_read,
2602 * Software counter: cpu wall time clock
2605 static void cpu_clock_perf_counter_update(struct perf_counter *counter)
2607 int cpu = raw_smp_processor_id();
2611 now = cpu_clock(cpu);
2612 prev = atomic64_read(&counter->hw.prev_count);
2613 atomic64_set(&counter->hw.prev_count, now);
2614 atomic64_add(now - prev, &counter->count);
2617 static int cpu_clock_perf_counter_enable(struct perf_counter *counter)
2619 struct hw_perf_counter *hwc = &counter->hw;
2620 int cpu = raw_smp_processor_id();
2622 atomic64_set(&hwc->prev_count, cpu_clock(cpu));
2623 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
2624 hwc->hrtimer.function = perf_swcounter_hrtimer;
2625 if (hwc->irq_period) {
2626 __hrtimer_start_range_ns(&hwc->hrtimer,
2627 ns_to_ktime(hwc->irq_period), 0,
2628 HRTIMER_MODE_REL, 0);
2634 static void cpu_clock_perf_counter_disable(struct perf_counter *counter)
2636 hrtimer_cancel(&counter->hw.hrtimer);
2637 cpu_clock_perf_counter_update(counter);
2640 static void cpu_clock_perf_counter_read(struct perf_counter *counter)
2642 cpu_clock_perf_counter_update(counter);
2645 static const struct pmu perf_ops_cpu_clock = {
2646 .enable = cpu_clock_perf_counter_enable,
2647 .disable = cpu_clock_perf_counter_disable,
2648 .read = cpu_clock_perf_counter_read,
2652 * Software counter: task time clock
2655 static void task_clock_perf_counter_update(struct perf_counter *counter, u64 now)
2660 prev = atomic64_xchg(&counter->hw.prev_count, now);
2662 atomic64_add(delta, &counter->count);
2665 static int task_clock_perf_counter_enable(struct perf_counter *counter)
2667 struct hw_perf_counter *hwc = &counter->hw;
2670 now = counter->ctx->time;
2672 atomic64_set(&hwc->prev_count, now);
2673 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
2674 hwc->hrtimer.function = perf_swcounter_hrtimer;
2675 if (hwc->irq_period) {
2676 __hrtimer_start_range_ns(&hwc->hrtimer,
2677 ns_to_ktime(hwc->irq_period), 0,
2678 HRTIMER_MODE_REL, 0);
2684 static void task_clock_perf_counter_disable(struct perf_counter *counter)
2686 hrtimer_cancel(&counter->hw.hrtimer);
2687 task_clock_perf_counter_update(counter, counter->ctx->time);
2691 static void task_clock_perf_counter_read(struct perf_counter *counter)
2696 update_context_time(counter->ctx);
2697 time = counter->ctx->time;
2699 u64 now = perf_clock();
2700 u64 delta = now - counter->ctx->timestamp;
2701 time = counter->ctx->time + delta;
2704 task_clock_perf_counter_update(counter, time);
2707 static const struct pmu perf_ops_task_clock = {
2708 .enable = task_clock_perf_counter_enable,
2709 .disable = task_clock_perf_counter_disable,
2710 .read = task_clock_perf_counter_read,
2714 * Software counter: cpu migrations
2717 static inline u64 get_cpu_migrations(struct perf_counter *counter)
2719 struct task_struct *curr = counter->ctx->task;
2722 return curr->se.nr_migrations;
2723 return cpu_nr_migrations(smp_processor_id());
2726 static void cpu_migrations_perf_counter_update(struct perf_counter *counter)
2731 prev = atomic64_read(&counter->hw.prev_count);
2732 now = get_cpu_migrations(counter);
2734 atomic64_set(&counter->hw.prev_count, now);
2738 atomic64_add(delta, &counter->count);
2741 static void cpu_migrations_perf_counter_read(struct perf_counter *counter)
2743 cpu_migrations_perf_counter_update(counter);
2746 static int cpu_migrations_perf_counter_enable(struct perf_counter *counter)
2748 if (counter->prev_state <= PERF_COUNTER_STATE_OFF)
2749 atomic64_set(&counter->hw.prev_count,
2750 get_cpu_migrations(counter));
2754 static void cpu_migrations_perf_counter_disable(struct perf_counter *counter)
2756 cpu_migrations_perf_counter_update(counter);
2759 static const struct pmu perf_ops_cpu_migrations = {
2760 .enable = cpu_migrations_perf_counter_enable,
2761 .disable = cpu_migrations_perf_counter_disable,
2762 .read = cpu_migrations_perf_counter_read,
2765 #ifdef CONFIG_EVENT_PROFILE
2766 void perf_tpcounter_event(int event_id)
2768 struct pt_regs *regs = get_irq_regs();
2771 regs = task_pt_regs(current);
2773 __perf_swcounter_event(PERF_TYPE_TRACEPOINT, event_id, 1, 1, regs, 0);
2775 EXPORT_SYMBOL_GPL(perf_tpcounter_event);
2777 extern int ftrace_profile_enable(int);
2778 extern void ftrace_profile_disable(int);
2780 static void tp_perf_counter_destroy(struct perf_counter *counter)
2782 ftrace_profile_disable(perf_event_id(&counter->hw_event));
2785 static const struct pmu *tp_perf_counter_init(struct perf_counter *counter)
2787 int event_id = perf_event_id(&counter->hw_event);
2790 ret = ftrace_profile_enable(event_id);
2794 counter->destroy = tp_perf_counter_destroy;
2795 counter->hw.irq_period = counter->hw_event.irq_period;
2797 return &perf_ops_generic;
2800 static const struct pmu *tp_perf_counter_init(struct perf_counter *counter)
2806 static const struct pmu *sw_perf_counter_init(struct perf_counter *counter)
2808 struct perf_counter_hw_event *hw_event = &counter->hw_event;
2809 const struct pmu *pmu = NULL;
2810 struct hw_perf_counter *hwc = &counter->hw;
2813 * Software counters (currently) can't in general distinguish
2814 * between user, kernel and hypervisor events.
2815 * However, context switches and cpu migrations are considered
2816 * to be kernel events, and page faults are never hypervisor
2819 switch (perf_event_id(&counter->hw_event)) {
2820 case PERF_COUNT_CPU_CLOCK:
2821 pmu = &perf_ops_cpu_clock;
2823 if (hw_event->irq_period && hw_event->irq_period < 10000)
2824 hw_event->irq_period = 10000;
2826 case PERF_COUNT_TASK_CLOCK:
2828 * If the user instantiates this as a per-cpu counter,
2829 * use the cpu_clock counter instead.
2831 if (counter->ctx->task)
2832 pmu = &perf_ops_task_clock;
2834 pmu = &perf_ops_cpu_clock;
2836 if (hw_event->irq_period && hw_event->irq_period < 10000)
2837 hw_event->irq_period = 10000;
2839 case PERF_COUNT_PAGE_FAULTS:
2840 case PERF_COUNT_PAGE_FAULTS_MIN:
2841 case PERF_COUNT_PAGE_FAULTS_MAJ:
2842 case PERF_COUNT_CONTEXT_SWITCHES:
2843 pmu = &perf_ops_generic;
2845 case PERF_COUNT_CPU_MIGRATIONS:
2846 if (!counter->hw_event.exclude_kernel)
2847 pmu = &perf_ops_cpu_migrations;
2852 hwc->irq_period = hw_event->irq_period;
2858 * Allocate and initialize a counter structure
2860 static struct perf_counter *
2861 perf_counter_alloc(struct perf_counter_hw_event *hw_event,
2863 struct perf_counter_context *ctx,
2864 struct perf_counter *group_leader,
2867 const struct pmu *pmu;
2868 struct perf_counter *counter;
2871 counter = kzalloc(sizeof(*counter), gfpflags);
2873 return ERR_PTR(-ENOMEM);
2876 * Single counters are their own group leaders, with an
2877 * empty sibling list:
2880 group_leader = counter;
2882 mutex_init(&counter->mutex);
2883 INIT_LIST_HEAD(&counter->list_entry);
2884 INIT_LIST_HEAD(&counter->event_entry);
2885 INIT_LIST_HEAD(&counter->sibling_list);
2886 init_waitqueue_head(&counter->waitq);
2888 mutex_init(&counter->mmap_mutex);
2890 INIT_LIST_HEAD(&counter->child_list);
2893 counter->hw_event = *hw_event;
2894 counter->group_leader = group_leader;
2895 counter->pmu = NULL;
2898 counter->state = PERF_COUNTER_STATE_INACTIVE;
2899 if (hw_event->disabled)
2900 counter->state = PERF_COUNTER_STATE_OFF;
2905 * we currently do not support PERF_RECORD_GROUP on inherited counters
2907 if (hw_event->inherit && (hw_event->record_type & PERF_RECORD_GROUP))
2910 if (perf_event_raw(hw_event)) {
2911 pmu = hw_perf_counter_init(counter);
2915 switch (perf_event_type(hw_event)) {
2916 case PERF_TYPE_HARDWARE:
2917 pmu = hw_perf_counter_init(counter);
2920 case PERF_TYPE_SOFTWARE:
2921 pmu = sw_perf_counter_init(counter);
2924 case PERF_TYPE_TRACEPOINT:
2925 pmu = tp_perf_counter_init(counter);
2932 else if (IS_ERR(pmu))
2937 return ERR_PTR(err);
2942 atomic_inc(&nr_counters);
2943 if (counter->hw_event.mmap)
2944 atomic_inc(&nr_mmap_tracking);
2945 if (counter->hw_event.munmap)
2946 atomic_inc(&nr_munmap_tracking);
2947 if (counter->hw_event.comm)
2948 atomic_inc(&nr_comm_tracking);
2954 * sys_perf_counter_open - open a performance counter, associate it to a task/cpu
2956 * @hw_event_uptr: event type attributes for monitoring/sampling
2959 * @group_fd: group leader counter fd
2961 SYSCALL_DEFINE5(perf_counter_open,
2962 const struct perf_counter_hw_event __user *, hw_event_uptr,
2963 pid_t, pid, int, cpu, int, group_fd, unsigned long, flags)
2965 struct perf_counter *counter, *group_leader;
2966 struct perf_counter_hw_event hw_event;
2967 struct perf_counter_context *ctx;
2968 struct file *counter_file = NULL;
2969 struct file *group_file = NULL;
2970 int fput_needed = 0;
2971 int fput_needed2 = 0;
2974 /* for future expandability... */
2978 if (copy_from_user(&hw_event, hw_event_uptr, sizeof(hw_event)) != 0)
2982 * Get the target context (task or percpu):
2984 ctx = find_get_context(pid, cpu);
2986 return PTR_ERR(ctx);
2989 * Look up the group leader (we will attach this counter to it):
2991 group_leader = NULL;
2992 if (group_fd != -1) {
2994 group_file = fget_light(group_fd, &fput_needed);
2996 goto err_put_context;
2997 if (group_file->f_op != &perf_fops)
2998 goto err_put_context;
3000 group_leader = group_file->private_data;
3002 * Do not allow a recursive hierarchy (this new sibling
3003 * becoming part of another group-sibling):
3005 if (group_leader->group_leader != group_leader)
3006 goto err_put_context;
3008 * Do not allow to attach to a group in a different
3009 * task or CPU context:
3011 if (group_leader->ctx != ctx)
3012 goto err_put_context;
3014 * Only a group leader can be exclusive or pinned
3016 if (hw_event.exclusive || hw_event.pinned)
3017 goto err_put_context;
3020 counter = perf_counter_alloc(&hw_event, cpu, ctx, group_leader,
3022 ret = PTR_ERR(counter);
3023 if (IS_ERR(counter))
3024 goto err_put_context;
3026 ret = anon_inode_getfd("[perf_counter]", &perf_fops, counter, 0);
3028 goto err_free_put_context;
3030 counter_file = fget_light(ret, &fput_needed2);
3032 goto err_free_put_context;
3034 counter->filp = counter_file;
3035 mutex_lock(&ctx->mutex);
3036 perf_install_in_context(ctx, counter, cpu);
3037 mutex_unlock(&ctx->mutex);
3039 fput_light(counter_file, fput_needed2);
3042 fput_light(group_file, fput_needed);
3046 err_free_put_context:
3056 * Initialize the perf_counter context in a task_struct:
3059 __perf_counter_init_context(struct perf_counter_context *ctx,
3060 struct task_struct *task)
3062 memset(ctx, 0, sizeof(*ctx));
3063 spin_lock_init(&ctx->lock);
3064 mutex_init(&ctx->mutex);
3065 INIT_LIST_HEAD(&ctx->counter_list);
3066 INIT_LIST_HEAD(&ctx->event_list);
3071 * inherit a counter from parent task to child task:
3073 static struct perf_counter *
3074 inherit_counter(struct perf_counter *parent_counter,
3075 struct task_struct *parent,
3076 struct perf_counter_context *parent_ctx,
3077 struct task_struct *child,
3078 struct perf_counter *group_leader,
3079 struct perf_counter_context *child_ctx)
3081 struct perf_counter *child_counter;
3084 * Instead of creating recursive hierarchies of counters,
3085 * we link inherited counters back to the original parent,
3086 * which has a filp for sure, which we use as the reference
3089 if (parent_counter->parent)
3090 parent_counter = parent_counter->parent;
3092 child_counter = perf_counter_alloc(&parent_counter->hw_event,
3093 parent_counter->cpu, child_ctx,
3094 group_leader, GFP_KERNEL);
3095 if (IS_ERR(child_counter))
3096 return child_counter;
3099 * Link it up in the child's context:
3101 child_counter->task = child;
3102 add_counter_to_ctx(child_counter, child_ctx);
3104 child_counter->parent = parent_counter;
3106 * inherit into child's child as well:
3108 child_counter->hw_event.inherit = 1;
3111 * Get a reference to the parent filp - we will fput it
3112 * when the child counter exits. This is safe to do because
3113 * we are in the parent and we know that the filp still
3114 * exists and has a nonzero count:
3116 atomic_long_inc(&parent_counter->filp->f_count);
3119 * Link this into the parent counter's child list
3121 mutex_lock(&parent_counter->mutex);
3122 list_add_tail(&child_counter->child_list, &parent_counter->child_list);
3125 * Make the child state follow the state of the parent counter,
3126 * not its hw_event.disabled bit. We hold the parent's mutex,
3127 * so we won't race with perf_counter_{en,dis}able_family.
3129 if (parent_counter->state >= PERF_COUNTER_STATE_INACTIVE)
3130 child_counter->state = PERF_COUNTER_STATE_INACTIVE;
3132 child_counter->state = PERF_COUNTER_STATE_OFF;
3134 mutex_unlock(&parent_counter->mutex);
3136 return child_counter;
3139 static int inherit_group(struct perf_counter *parent_counter,
3140 struct task_struct *parent,
3141 struct perf_counter_context *parent_ctx,
3142 struct task_struct *child,
3143 struct perf_counter_context *child_ctx)
3145 struct perf_counter *leader;
3146 struct perf_counter *sub;
3147 struct perf_counter *child_ctr;
3149 leader = inherit_counter(parent_counter, parent, parent_ctx,
3150 child, NULL, child_ctx);
3152 return PTR_ERR(leader);
3153 list_for_each_entry(sub, &parent_counter->sibling_list, list_entry) {
3154 child_ctr = inherit_counter(sub, parent, parent_ctx,
3155 child, leader, child_ctx);
3156 if (IS_ERR(child_ctr))
3157 return PTR_ERR(child_ctr);
3162 static void sync_child_counter(struct perf_counter *child_counter,
3163 struct perf_counter *parent_counter)
3165 u64 parent_val, child_val;
3167 parent_val = atomic64_read(&parent_counter->count);
3168 child_val = atomic64_read(&child_counter->count);
3171 * Add back the child's count to the parent's count:
3173 atomic64_add(child_val, &parent_counter->count);
3174 atomic64_add(child_counter->total_time_enabled,
3175 &parent_counter->child_total_time_enabled);
3176 atomic64_add(child_counter->total_time_running,
3177 &parent_counter->child_total_time_running);
3180 * Remove this counter from the parent's list
3182 mutex_lock(&parent_counter->mutex);
3183 list_del_init(&child_counter->child_list);
3184 mutex_unlock(&parent_counter->mutex);
3187 * Release the parent counter, if this was the last
3190 fput(parent_counter->filp);
3194 __perf_counter_exit_task(struct task_struct *child,
3195 struct perf_counter *child_counter,
3196 struct perf_counter_context *child_ctx)
3198 struct perf_counter *parent_counter;
3199 struct perf_counter *sub, *tmp;
3202 * If we do not self-reap then we have to wait for the
3203 * child task to unschedule (it will happen for sure),
3204 * so that its counter is at its final count. (This
3205 * condition triggers rarely - child tasks usually get
3206 * off their CPU before the parent has a chance to
3207 * get this far into the reaping action)
3209 if (child != current) {
3210 wait_task_inactive(child, 0);
3211 list_del_init(&child_counter->list_entry);
3212 update_counter_times(child_counter);
3214 struct perf_cpu_context *cpuctx;
3215 unsigned long flags;
3218 * Disable and unlink this counter.
3220 * Be careful about zapping the list - IRQ/NMI context
3221 * could still be processing it:
3223 local_irq_save(flags);
3226 cpuctx = &__get_cpu_var(perf_cpu_context);
3228 group_sched_out(child_counter, cpuctx, child_ctx);
3229 update_counter_times(child_counter);
3231 list_del_init(&child_counter->list_entry);
3233 child_ctx->nr_counters--;
3236 local_irq_restore(flags);
3239 parent_counter = child_counter->parent;
3241 * It can happen that parent exits first, and has counters
3242 * that are still around due to the child reference. These
3243 * counters need to be zapped - but otherwise linger.
3245 if (parent_counter) {
3246 sync_child_counter(child_counter, parent_counter);
3247 list_for_each_entry_safe(sub, tmp, &child_counter->sibling_list,
3250 sync_child_counter(sub, sub->parent);
3254 free_counter(child_counter);
3259 * When a child task exits, feed back counter values to parent counters.
3261 * Note: we may be running in child context, but the PID is not hashed
3262 * anymore so new counters will not be added.
3264 void perf_counter_exit_task(struct task_struct *child)
3266 struct perf_counter *child_counter, *tmp;
3267 struct perf_counter_context *child_ctx;
3269 child_ctx = &child->perf_counter_ctx;
3271 if (likely(!child_ctx->nr_counters))
3274 list_for_each_entry_safe(child_counter, tmp, &child_ctx->counter_list,
3276 __perf_counter_exit_task(child, child_counter, child_ctx);
3280 * Initialize the perf_counter context in task_struct
3282 void perf_counter_init_task(struct task_struct *child)
3284 struct perf_counter_context *child_ctx, *parent_ctx;
3285 struct perf_counter *counter;
3286 struct task_struct *parent = current;
3288 child_ctx = &child->perf_counter_ctx;
3289 parent_ctx = &parent->perf_counter_ctx;
3291 __perf_counter_init_context(child_ctx, child);
3294 * This is executed from the parent task context, so inherit
3295 * counters that have been marked for cloning:
3298 if (likely(!parent_ctx->nr_counters))
3302 * Lock the parent list. No need to lock the child - not PID
3303 * hashed yet and not running, so nobody can access it.
3305 mutex_lock(&parent_ctx->mutex);
3308 * We dont have to disable NMIs - we are only looking at
3309 * the list, not manipulating it:
3311 list_for_each_entry(counter, &parent_ctx->counter_list, list_entry) {
3312 if (!counter->hw_event.inherit)
3315 if (inherit_group(counter, parent,
3316 parent_ctx, child, child_ctx))
3320 mutex_unlock(&parent_ctx->mutex);
3323 static void __cpuinit perf_counter_init_cpu(int cpu)
3325 struct perf_cpu_context *cpuctx;
3327 cpuctx = &per_cpu(perf_cpu_context, cpu);
3328 __perf_counter_init_context(&cpuctx->ctx, NULL);
3330 spin_lock(&perf_resource_lock);
3331 cpuctx->max_pertask = perf_max_counters - perf_reserved_percpu;
3332 spin_unlock(&perf_resource_lock);
3334 hw_perf_counter_setup(cpu);
3337 #ifdef CONFIG_HOTPLUG_CPU
3338 static void __perf_counter_exit_cpu(void *info)
3340 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
3341 struct perf_counter_context *ctx = &cpuctx->ctx;
3342 struct perf_counter *counter, *tmp;
3344 list_for_each_entry_safe(counter, tmp, &ctx->counter_list, list_entry)
3345 __perf_counter_remove_from_context(counter);
3347 static void perf_counter_exit_cpu(int cpu)
3349 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
3350 struct perf_counter_context *ctx = &cpuctx->ctx;
3352 mutex_lock(&ctx->mutex);
3353 smp_call_function_single(cpu, __perf_counter_exit_cpu, NULL, 1);
3354 mutex_unlock(&ctx->mutex);
3357 static inline void perf_counter_exit_cpu(int cpu) { }
3360 static int __cpuinit
3361 perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
3363 unsigned int cpu = (long)hcpu;
3367 case CPU_UP_PREPARE:
3368 case CPU_UP_PREPARE_FROZEN:
3369 perf_counter_init_cpu(cpu);
3372 case CPU_DOWN_PREPARE:
3373 case CPU_DOWN_PREPARE_FROZEN:
3374 perf_counter_exit_cpu(cpu);
3384 static struct notifier_block __cpuinitdata perf_cpu_nb = {
3385 .notifier_call = perf_cpu_notify,
3388 void __init perf_counter_init(void)
3390 perf_cpu_notify(&perf_cpu_nb, (unsigned long)CPU_UP_PREPARE,
3391 (void *)(long)smp_processor_id());
3392 register_cpu_notifier(&perf_cpu_nb);
3395 static ssize_t perf_show_reserve_percpu(struct sysdev_class *class, char *buf)
3397 return sprintf(buf, "%d\n", perf_reserved_percpu);
3401 perf_set_reserve_percpu(struct sysdev_class *class,
3405 struct perf_cpu_context *cpuctx;
3409 err = strict_strtoul(buf, 10, &val);
3412 if (val > perf_max_counters)
3415 spin_lock(&perf_resource_lock);
3416 perf_reserved_percpu = val;
3417 for_each_online_cpu(cpu) {
3418 cpuctx = &per_cpu(perf_cpu_context, cpu);
3419 spin_lock_irq(&cpuctx->ctx.lock);
3420 mpt = min(perf_max_counters - cpuctx->ctx.nr_counters,
3421 perf_max_counters - perf_reserved_percpu);
3422 cpuctx->max_pertask = mpt;
3423 spin_unlock_irq(&cpuctx->ctx.lock);
3425 spin_unlock(&perf_resource_lock);
3430 static ssize_t perf_show_overcommit(struct sysdev_class *class, char *buf)
3432 return sprintf(buf, "%d\n", perf_overcommit);
3436 perf_set_overcommit(struct sysdev_class *class, const char *buf, size_t count)
3441 err = strict_strtoul(buf, 10, &val);
3447 spin_lock(&perf_resource_lock);
3448 perf_overcommit = val;
3449 spin_unlock(&perf_resource_lock);
3454 static SYSDEV_CLASS_ATTR(
3457 perf_show_reserve_percpu,
3458 perf_set_reserve_percpu
3461 static SYSDEV_CLASS_ATTR(
3464 perf_show_overcommit,
3468 static struct attribute *perfclass_attrs[] = {
3469 &attr_reserve_percpu.attr,
3470 &attr_overcommit.attr,
3474 static struct attribute_group perfclass_attr_group = {
3475 .attrs = perfclass_attrs,
3476 .name = "perf_counters",
3479 static int __init perf_counter_sysfs_init(void)
3481 return sysfs_create_group(&cpu_sysdev_class.kset.kobj,
3482 &perfclass_attr_group);
3484 device_initcall(perf_counter_sysfs_init);