2 * Performance counter core code
4 * Copyright(C) 2008 Thomas Gleixner <tglx@linutronix.de>
5 * Copyright(C) 2008 Red Hat, Inc., Ingo Molnar
8 * For licensing details see kernel-base/COPYING
13 #include <linux/cpu.h>
14 #include <linux/smp.h>
15 #include <linux/file.h>
16 #include <linux/poll.h>
17 #include <linux/sysfs.h>
18 #include <linux/ptrace.h>
19 #include <linux/percpu.h>
20 #include <linux/vmstat.h>
21 #include <linux/hardirq.h>
22 #include <linux/rculist.h>
23 #include <linux/uaccess.h>
24 #include <linux/syscalls.h>
25 #include <linux/anon_inodes.h>
26 #include <linux/kernel_stat.h>
27 #include <linux/perf_counter.h>
28 #include <linux/dcache.h>
30 #include <asm/irq_regs.h>
33 * Each CPU has a list of per CPU counters:
35 DEFINE_PER_CPU(struct perf_cpu_context, perf_cpu_context);
37 int perf_max_counters __read_mostly = 1;
38 static int perf_reserved_percpu __read_mostly;
39 static int perf_overcommit __read_mostly = 1;
41 static atomic_t nr_mmap_tracking __read_mostly;
42 static atomic_t nr_munmap_tracking __read_mostly;
43 static atomic_t nr_comm_tracking __read_mostly;
45 int sysctl_perf_counter_priv __read_mostly; /* do we need to be privileged */
48 * Mutex for (sysadmin-configurable) counter reservations:
50 static DEFINE_MUTEX(perf_resource_mutex);
53 * Architecture provided APIs - weak aliases:
55 extern __weak const struct pmu *hw_perf_counter_init(struct perf_counter *counter)
60 u64 __weak hw_perf_save_disable(void) { return 0; }
61 void __weak hw_perf_restore(u64 ctrl) { barrier(); }
62 void __weak hw_perf_counter_setup(int cpu) { barrier(); }
63 int __weak hw_perf_group_sched_in(struct perf_counter *group_leader,
64 struct perf_cpu_context *cpuctx,
65 struct perf_counter_context *ctx, int cpu)
70 void __weak perf_counter_print_debug(void) { }
73 list_add_counter(struct perf_counter *counter, struct perf_counter_context *ctx)
75 struct perf_counter *group_leader = counter->group_leader;
78 * Depending on whether it is a standalone or sibling counter,
79 * add it straight to the context's counter list, or to the group
80 * leader's sibling list:
82 if (counter->group_leader == counter)
83 list_add_tail(&counter->list_entry, &ctx->counter_list);
85 list_add_tail(&counter->list_entry, &group_leader->sibling_list);
86 group_leader->nr_siblings++;
89 list_add_rcu(&counter->event_entry, &ctx->event_list);
93 list_del_counter(struct perf_counter *counter, struct perf_counter_context *ctx)
95 struct perf_counter *sibling, *tmp;
97 list_del_init(&counter->list_entry);
98 list_del_rcu(&counter->event_entry);
100 if (counter->group_leader != counter)
101 counter->group_leader->nr_siblings--;
104 * If this was a group counter with sibling counters then
105 * upgrade the siblings to singleton counters by adding them
106 * to the context list directly:
108 list_for_each_entry_safe(sibling, tmp,
109 &counter->sibling_list, list_entry) {
111 list_move_tail(&sibling->list_entry, &ctx->counter_list);
112 sibling->group_leader = sibling;
117 counter_sched_out(struct perf_counter *counter,
118 struct perf_cpu_context *cpuctx,
119 struct perf_counter_context *ctx)
121 if (counter->state != PERF_COUNTER_STATE_ACTIVE)
124 counter->state = PERF_COUNTER_STATE_INACTIVE;
125 counter->tstamp_stopped = ctx->time;
126 counter->pmu->disable(counter);
129 if (!is_software_counter(counter))
130 cpuctx->active_oncpu--;
132 if (counter->hw_event.exclusive || !cpuctx->active_oncpu)
133 cpuctx->exclusive = 0;
137 group_sched_out(struct perf_counter *group_counter,
138 struct perf_cpu_context *cpuctx,
139 struct perf_counter_context *ctx)
141 struct perf_counter *counter;
143 if (group_counter->state != PERF_COUNTER_STATE_ACTIVE)
146 counter_sched_out(group_counter, cpuctx, ctx);
149 * Schedule out siblings (if any):
151 list_for_each_entry(counter, &group_counter->sibling_list, list_entry)
152 counter_sched_out(counter, cpuctx, ctx);
154 if (group_counter->hw_event.exclusive)
155 cpuctx->exclusive = 0;
159 * Cross CPU call to remove a performance counter
161 * We disable the counter on the hardware level first. After that we
162 * remove it from the context list.
164 static void __perf_counter_remove_from_context(void *info)
166 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
167 struct perf_counter *counter = info;
168 struct perf_counter_context *ctx = counter->ctx;
173 * If this is a task context, we need to check whether it is
174 * the current task context of this cpu. If not it has been
175 * scheduled out before the smp call arrived.
177 if (ctx->task && cpuctx->task_ctx != ctx)
180 spin_lock_irqsave(&ctx->lock, flags);
182 counter_sched_out(counter, cpuctx, ctx);
184 counter->task = NULL;
188 * Protect the list operation against NMI by disabling the
189 * counters on a global level. NOP for non NMI based counters.
191 perf_flags = hw_perf_save_disable();
192 list_del_counter(counter, ctx);
193 hw_perf_restore(perf_flags);
197 * Allow more per task counters with respect to the
200 cpuctx->max_pertask =
201 min(perf_max_counters - ctx->nr_counters,
202 perf_max_counters - perf_reserved_percpu);
205 spin_unlock_irqrestore(&ctx->lock, flags);
210 * Remove the counter from a task's (or a CPU's) list of counters.
212 * Must be called with counter->mutex and ctx->mutex held.
214 * CPU counters are removed with a smp call. For task counters we only
215 * call when the task is on a CPU.
217 static void perf_counter_remove_from_context(struct perf_counter *counter)
219 struct perf_counter_context *ctx = counter->ctx;
220 struct task_struct *task = ctx->task;
224 * Per cpu counters are removed via an smp call and
225 * the removal is always sucessful.
227 smp_call_function_single(counter->cpu,
228 __perf_counter_remove_from_context,
234 task_oncpu_function_call(task, __perf_counter_remove_from_context,
237 spin_lock_irq(&ctx->lock);
239 * If the context is active we need to retry the smp call.
241 if (ctx->nr_active && !list_empty(&counter->list_entry)) {
242 spin_unlock_irq(&ctx->lock);
247 * The lock prevents that this context is scheduled in so we
248 * can remove the counter safely, if the call above did not
251 if (!list_empty(&counter->list_entry)) {
253 list_del_counter(counter, ctx);
254 counter->task = NULL;
256 spin_unlock_irq(&ctx->lock);
259 static inline u64 perf_clock(void)
261 return cpu_clock(smp_processor_id());
265 * Update the record of the current time in a context.
267 static void update_context_time(struct perf_counter_context *ctx)
269 u64 now = perf_clock();
271 ctx->time += now - ctx->timestamp;
272 ctx->timestamp = now;
276 * Update the total_time_enabled and total_time_running fields for a counter.
278 static void update_counter_times(struct perf_counter *counter)
280 struct perf_counter_context *ctx = counter->ctx;
283 if (counter->state < PERF_COUNTER_STATE_INACTIVE)
286 counter->total_time_enabled = ctx->time - counter->tstamp_enabled;
288 if (counter->state == PERF_COUNTER_STATE_INACTIVE)
289 run_end = counter->tstamp_stopped;
293 counter->total_time_running = run_end - counter->tstamp_running;
297 * Update total_time_enabled and total_time_running for all counters in a group.
299 static void update_group_times(struct perf_counter *leader)
301 struct perf_counter *counter;
303 update_counter_times(leader);
304 list_for_each_entry(counter, &leader->sibling_list, list_entry)
305 update_counter_times(counter);
309 * Cross CPU call to disable a performance counter
311 static void __perf_counter_disable(void *info)
313 struct perf_counter *counter = info;
314 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
315 struct perf_counter_context *ctx = counter->ctx;
319 * If this is a per-task counter, need to check whether this
320 * counter's task is the current task on this cpu.
322 if (ctx->task && cpuctx->task_ctx != ctx)
325 spin_lock_irqsave(&ctx->lock, flags);
328 * If the counter is on, turn it off.
329 * If it is in error state, leave it in error state.
331 if (counter->state >= PERF_COUNTER_STATE_INACTIVE) {
332 update_context_time(ctx);
333 update_counter_times(counter);
334 if (counter == counter->group_leader)
335 group_sched_out(counter, cpuctx, ctx);
337 counter_sched_out(counter, cpuctx, ctx);
338 counter->state = PERF_COUNTER_STATE_OFF;
341 spin_unlock_irqrestore(&ctx->lock, flags);
347 static void perf_counter_disable(struct perf_counter *counter)
349 struct perf_counter_context *ctx = counter->ctx;
350 struct task_struct *task = ctx->task;
354 * Disable the counter on the cpu that it's on
356 smp_call_function_single(counter->cpu, __perf_counter_disable,
362 task_oncpu_function_call(task, __perf_counter_disable, counter);
364 spin_lock_irq(&ctx->lock);
366 * If the counter is still active, we need to retry the cross-call.
368 if (counter->state == PERF_COUNTER_STATE_ACTIVE) {
369 spin_unlock_irq(&ctx->lock);
374 * Since we have the lock this context can't be scheduled
375 * in, so we can change the state safely.
377 if (counter->state == PERF_COUNTER_STATE_INACTIVE) {
378 update_counter_times(counter);
379 counter->state = PERF_COUNTER_STATE_OFF;
382 spin_unlock_irq(&ctx->lock);
386 * Disable a counter and all its children.
388 static void perf_counter_disable_family(struct perf_counter *counter)
390 struct perf_counter *child;
392 perf_counter_disable(counter);
395 * Lock the mutex to protect the list of children
397 mutex_lock(&counter->mutex);
398 list_for_each_entry(child, &counter->child_list, child_list)
399 perf_counter_disable(child);
400 mutex_unlock(&counter->mutex);
404 counter_sched_in(struct perf_counter *counter,
405 struct perf_cpu_context *cpuctx,
406 struct perf_counter_context *ctx,
409 if (counter->state <= PERF_COUNTER_STATE_OFF)
412 counter->state = PERF_COUNTER_STATE_ACTIVE;
413 counter->oncpu = cpu; /* TODO: put 'cpu' into cpuctx->cpu */
415 * The new state must be visible before we turn it on in the hardware:
419 if (counter->pmu->enable(counter)) {
420 counter->state = PERF_COUNTER_STATE_INACTIVE;
425 counter->tstamp_running += ctx->time - counter->tstamp_stopped;
427 if (!is_software_counter(counter))
428 cpuctx->active_oncpu++;
431 if (counter->hw_event.exclusive)
432 cpuctx->exclusive = 1;
438 * Return 1 for a group consisting entirely of software counters,
439 * 0 if the group contains any hardware counters.
441 static int is_software_only_group(struct perf_counter *leader)
443 struct perf_counter *counter;
445 if (!is_software_counter(leader))
448 list_for_each_entry(counter, &leader->sibling_list, list_entry)
449 if (!is_software_counter(counter))
456 * Work out whether we can put this counter group on the CPU now.
458 static int group_can_go_on(struct perf_counter *counter,
459 struct perf_cpu_context *cpuctx,
463 * Groups consisting entirely of software counters can always go on.
465 if (is_software_only_group(counter))
468 * If an exclusive group is already on, no other hardware
469 * counters can go on.
471 if (cpuctx->exclusive)
474 * If this group is exclusive and there are already
475 * counters on the CPU, it can't go on.
477 if (counter->hw_event.exclusive && cpuctx->active_oncpu)
480 * Otherwise, try to add it if all previous groups were able
486 static void add_counter_to_ctx(struct perf_counter *counter,
487 struct perf_counter_context *ctx)
489 list_add_counter(counter, ctx);
491 counter->prev_state = PERF_COUNTER_STATE_OFF;
492 counter->tstamp_enabled = ctx->time;
493 counter->tstamp_running = ctx->time;
494 counter->tstamp_stopped = ctx->time;
498 * Cross CPU call to install and enable a performance counter
500 static void __perf_install_in_context(void *info)
502 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
503 struct perf_counter *counter = info;
504 struct perf_counter_context *ctx = counter->ctx;
505 struct perf_counter *leader = counter->group_leader;
506 int cpu = smp_processor_id();
512 * If this is a task context, we need to check whether it is
513 * the current task context of this cpu. If not it has been
514 * scheduled out before the smp call arrived.
516 if (ctx->task && cpuctx->task_ctx != ctx)
519 spin_lock_irqsave(&ctx->lock, flags);
520 update_context_time(ctx);
523 * Protect the list operation against NMI by disabling the
524 * counters on a global level. NOP for non NMI based counters.
526 perf_flags = hw_perf_save_disable();
528 add_counter_to_ctx(counter, ctx);
531 * Don't put the counter on if it is disabled or if
532 * it is in a group and the group isn't on.
534 if (counter->state != PERF_COUNTER_STATE_INACTIVE ||
535 (leader != counter && leader->state != PERF_COUNTER_STATE_ACTIVE))
539 * An exclusive counter can't go on if there are already active
540 * hardware counters, and no hardware counter can go on if there
541 * is already an exclusive counter on.
543 if (!group_can_go_on(counter, cpuctx, 1))
546 err = counter_sched_in(counter, cpuctx, ctx, cpu);
550 * This counter couldn't go on. If it is in a group
551 * then we have to pull the whole group off.
552 * If the counter group is pinned then put it in error state.
554 if (leader != counter)
555 group_sched_out(leader, cpuctx, ctx);
556 if (leader->hw_event.pinned) {
557 update_group_times(leader);
558 leader->state = PERF_COUNTER_STATE_ERROR;
562 if (!err && !ctx->task && cpuctx->max_pertask)
563 cpuctx->max_pertask--;
566 hw_perf_restore(perf_flags);
568 spin_unlock_irqrestore(&ctx->lock, flags);
572 * Attach a performance counter to a context
574 * First we add the counter to the list with the hardware enable bit
575 * in counter->hw_config cleared.
577 * If the counter is attached to a task which is on a CPU we use a smp
578 * call to enable it in the task context. The task might have been
579 * scheduled away, but we check this in the smp call again.
581 * Must be called with ctx->mutex held.
584 perf_install_in_context(struct perf_counter_context *ctx,
585 struct perf_counter *counter,
588 struct task_struct *task = ctx->task;
592 * Per cpu counters are installed via an smp call and
593 * the install is always sucessful.
595 smp_call_function_single(cpu, __perf_install_in_context,
600 counter->task = task;
602 task_oncpu_function_call(task, __perf_install_in_context,
605 spin_lock_irq(&ctx->lock);
607 * we need to retry the smp call.
609 if (ctx->is_active && list_empty(&counter->list_entry)) {
610 spin_unlock_irq(&ctx->lock);
615 * The lock prevents that this context is scheduled in so we
616 * can add the counter safely, if it the call above did not
619 if (list_empty(&counter->list_entry))
620 add_counter_to_ctx(counter, ctx);
621 spin_unlock_irq(&ctx->lock);
625 * Cross CPU call to enable a performance counter
627 static void __perf_counter_enable(void *info)
629 struct perf_counter *counter = info;
630 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
631 struct perf_counter_context *ctx = counter->ctx;
632 struct perf_counter *leader = counter->group_leader;
637 * If this is a per-task counter, need to check whether this
638 * counter's task is the current task on this cpu.
640 if (ctx->task && cpuctx->task_ctx != ctx)
643 spin_lock_irqsave(&ctx->lock, flags);
644 update_context_time(ctx);
646 counter->prev_state = counter->state;
647 if (counter->state >= PERF_COUNTER_STATE_INACTIVE)
649 counter->state = PERF_COUNTER_STATE_INACTIVE;
650 counter->tstamp_enabled = ctx->time - counter->total_time_enabled;
653 * If the counter is in a group and isn't the group leader,
654 * then don't put it on unless the group is on.
656 if (leader != counter && leader->state != PERF_COUNTER_STATE_ACTIVE)
659 if (!group_can_go_on(counter, cpuctx, 1))
662 err = counter_sched_in(counter, cpuctx, ctx,
667 * If this counter can't go on and it's part of a
668 * group, then the whole group has to come off.
670 if (leader != counter)
671 group_sched_out(leader, cpuctx, ctx);
672 if (leader->hw_event.pinned) {
673 update_group_times(leader);
674 leader->state = PERF_COUNTER_STATE_ERROR;
679 spin_unlock_irqrestore(&ctx->lock, flags);
685 static void perf_counter_enable(struct perf_counter *counter)
687 struct perf_counter_context *ctx = counter->ctx;
688 struct task_struct *task = ctx->task;
692 * Enable the counter on the cpu that it's on
694 smp_call_function_single(counter->cpu, __perf_counter_enable,
699 spin_lock_irq(&ctx->lock);
700 if (counter->state >= PERF_COUNTER_STATE_INACTIVE)
704 * If the counter is in error state, clear that first.
705 * That way, if we see the counter in error state below, we
706 * know that it has gone back into error state, as distinct
707 * from the task having been scheduled away before the
708 * cross-call arrived.
710 if (counter->state == PERF_COUNTER_STATE_ERROR)
711 counter->state = PERF_COUNTER_STATE_OFF;
714 spin_unlock_irq(&ctx->lock);
715 task_oncpu_function_call(task, __perf_counter_enable, counter);
717 spin_lock_irq(&ctx->lock);
720 * If the context is active and the counter is still off,
721 * we need to retry the cross-call.
723 if (ctx->is_active && counter->state == PERF_COUNTER_STATE_OFF)
727 * Since we have the lock this context can't be scheduled
728 * in, so we can change the state safely.
730 if (counter->state == PERF_COUNTER_STATE_OFF) {
731 counter->state = PERF_COUNTER_STATE_INACTIVE;
732 counter->tstamp_enabled =
733 ctx->time - counter->total_time_enabled;
736 spin_unlock_irq(&ctx->lock);
739 static void perf_counter_refresh(struct perf_counter *counter, int refresh)
741 atomic_add(refresh, &counter->event_limit);
742 perf_counter_enable(counter);
746 * Enable a counter and all its children.
748 static void perf_counter_enable_family(struct perf_counter *counter)
750 struct perf_counter *child;
752 perf_counter_enable(counter);
755 * Lock the mutex to protect the list of children
757 mutex_lock(&counter->mutex);
758 list_for_each_entry(child, &counter->child_list, child_list)
759 perf_counter_enable(child);
760 mutex_unlock(&counter->mutex);
763 void __perf_counter_sched_out(struct perf_counter_context *ctx,
764 struct perf_cpu_context *cpuctx)
766 struct perf_counter *counter;
769 spin_lock(&ctx->lock);
771 if (likely(!ctx->nr_counters))
773 update_context_time(ctx);
775 flags = hw_perf_save_disable();
776 if (ctx->nr_active) {
777 list_for_each_entry(counter, &ctx->counter_list, list_entry)
778 group_sched_out(counter, cpuctx, ctx);
780 hw_perf_restore(flags);
782 spin_unlock(&ctx->lock);
786 * Called from scheduler to remove the counters of the current task,
787 * with interrupts disabled.
789 * We stop each counter and update the counter value in counter->count.
791 * This does not protect us against NMI, but disable()
792 * sets the disabled bit in the control field of counter _before_
793 * accessing the counter control register. If a NMI hits, then it will
794 * not restart the counter.
796 void perf_counter_task_sched_out(struct task_struct *task, int cpu)
798 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
799 struct perf_counter_context *ctx = &task->perf_counter_ctx;
800 struct pt_regs *regs;
802 if (likely(!cpuctx->task_ctx))
805 update_context_time(ctx);
807 regs = task_pt_regs(task);
808 perf_swcounter_event(PERF_COUNT_CONTEXT_SWITCHES, 1, 1, regs, 0);
809 __perf_counter_sched_out(ctx, cpuctx);
811 cpuctx->task_ctx = NULL;
814 static void perf_counter_cpu_sched_out(struct perf_cpu_context *cpuctx)
816 __perf_counter_sched_out(&cpuctx->ctx, cpuctx);
820 group_sched_in(struct perf_counter *group_counter,
821 struct perf_cpu_context *cpuctx,
822 struct perf_counter_context *ctx,
825 struct perf_counter *counter, *partial_group;
828 if (group_counter->state == PERF_COUNTER_STATE_OFF)
831 ret = hw_perf_group_sched_in(group_counter, cpuctx, ctx, cpu);
833 return ret < 0 ? ret : 0;
835 group_counter->prev_state = group_counter->state;
836 if (counter_sched_in(group_counter, cpuctx, ctx, cpu))
840 * Schedule in siblings as one group (if any):
842 list_for_each_entry(counter, &group_counter->sibling_list, list_entry) {
843 counter->prev_state = counter->state;
844 if (counter_sched_in(counter, cpuctx, ctx, cpu)) {
845 partial_group = counter;
854 * Groups can be scheduled in as one unit only, so undo any
855 * partial group before returning:
857 list_for_each_entry(counter, &group_counter->sibling_list, list_entry) {
858 if (counter == partial_group)
860 counter_sched_out(counter, cpuctx, ctx);
862 counter_sched_out(group_counter, cpuctx, ctx);
868 __perf_counter_sched_in(struct perf_counter_context *ctx,
869 struct perf_cpu_context *cpuctx, int cpu)
871 struct perf_counter *counter;
875 spin_lock(&ctx->lock);
877 if (likely(!ctx->nr_counters))
880 ctx->timestamp = perf_clock();
882 flags = hw_perf_save_disable();
885 * First go through the list and put on any pinned groups
886 * in order to give them the best chance of going on.
888 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
889 if (counter->state <= PERF_COUNTER_STATE_OFF ||
890 !counter->hw_event.pinned)
892 if (counter->cpu != -1 && counter->cpu != cpu)
895 if (group_can_go_on(counter, cpuctx, 1))
896 group_sched_in(counter, cpuctx, ctx, cpu);
899 * If this pinned group hasn't been scheduled,
900 * put it in error state.
902 if (counter->state == PERF_COUNTER_STATE_INACTIVE) {
903 update_group_times(counter);
904 counter->state = PERF_COUNTER_STATE_ERROR;
908 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
910 * Ignore counters in OFF or ERROR state, and
911 * ignore pinned counters since we did them already.
913 if (counter->state <= PERF_COUNTER_STATE_OFF ||
914 counter->hw_event.pinned)
918 * Listen to the 'cpu' scheduling filter constraint
921 if (counter->cpu != -1 && counter->cpu != cpu)
924 if (group_can_go_on(counter, cpuctx, can_add_hw)) {
925 if (group_sched_in(counter, cpuctx, ctx, cpu))
929 hw_perf_restore(flags);
931 spin_unlock(&ctx->lock);
935 * Called from scheduler to add the counters of the current task
936 * with interrupts disabled.
938 * We restore the counter value and then enable it.
940 * This does not protect us against NMI, but enable()
941 * sets the enabled bit in the control field of counter _before_
942 * accessing the counter control register. If a NMI hits, then it will
943 * keep the counter running.
945 void perf_counter_task_sched_in(struct task_struct *task, int cpu)
947 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
948 struct perf_counter_context *ctx = &task->perf_counter_ctx;
950 __perf_counter_sched_in(ctx, cpuctx, cpu);
951 cpuctx->task_ctx = ctx;
954 static void perf_counter_cpu_sched_in(struct perf_cpu_context *cpuctx, int cpu)
956 struct perf_counter_context *ctx = &cpuctx->ctx;
958 __perf_counter_sched_in(ctx, cpuctx, cpu);
961 int perf_counter_task_disable(void)
963 struct task_struct *curr = current;
964 struct perf_counter_context *ctx = &curr->perf_counter_ctx;
965 struct perf_counter *counter;
970 if (likely(!ctx->nr_counters))
973 local_irq_save(flags);
974 cpu = smp_processor_id();
976 perf_counter_task_sched_out(curr, cpu);
978 spin_lock(&ctx->lock);
981 * Disable all the counters:
983 perf_flags = hw_perf_save_disable();
985 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
986 if (counter->state != PERF_COUNTER_STATE_ERROR) {
987 update_group_times(counter);
988 counter->state = PERF_COUNTER_STATE_OFF;
992 hw_perf_restore(perf_flags);
994 spin_unlock_irqrestore(&ctx->lock, flags);
999 int perf_counter_task_enable(void)
1001 struct task_struct *curr = current;
1002 struct perf_counter_context *ctx = &curr->perf_counter_ctx;
1003 struct perf_counter *counter;
1004 unsigned long flags;
1008 if (likely(!ctx->nr_counters))
1011 local_irq_save(flags);
1012 cpu = smp_processor_id();
1014 perf_counter_task_sched_out(curr, cpu);
1016 spin_lock(&ctx->lock);
1019 * Disable all the counters:
1021 perf_flags = hw_perf_save_disable();
1023 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
1024 if (counter->state > PERF_COUNTER_STATE_OFF)
1026 counter->state = PERF_COUNTER_STATE_INACTIVE;
1027 counter->tstamp_enabled =
1028 ctx->time - counter->total_time_enabled;
1029 counter->hw_event.disabled = 0;
1031 hw_perf_restore(perf_flags);
1033 spin_unlock(&ctx->lock);
1035 perf_counter_task_sched_in(curr, cpu);
1037 local_irq_restore(flags);
1043 * Round-robin a context's counters:
1045 static void rotate_ctx(struct perf_counter_context *ctx)
1047 struct perf_counter *counter;
1050 if (!ctx->nr_counters)
1053 spin_lock(&ctx->lock);
1055 * Rotate the first entry last (works just fine for group counters too):
1057 perf_flags = hw_perf_save_disable();
1058 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
1059 list_move_tail(&counter->list_entry, &ctx->counter_list);
1062 hw_perf_restore(perf_flags);
1064 spin_unlock(&ctx->lock);
1067 void perf_counter_task_tick(struct task_struct *curr, int cpu)
1069 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
1070 struct perf_counter_context *ctx = &curr->perf_counter_ctx;
1071 const int rotate_percpu = 0;
1074 perf_counter_cpu_sched_out(cpuctx);
1075 perf_counter_task_sched_out(curr, cpu);
1078 rotate_ctx(&cpuctx->ctx);
1082 perf_counter_cpu_sched_in(cpuctx, cpu);
1083 perf_counter_task_sched_in(curr, cpu);
1087 * Cross CPU call to read the hardware counter
1089 static void __read(void *info)
1091 struct perf_counter *counter = info;
1092 struct perf_counter_context *ctx = counter->ctx;
1093 unsigned long flags;
1095 local_irq_save(flags);
1097 update_context_time(ctx);
1098 counter->pmu->read(counter);
1099 update_counter_times(counter);
1100 local_irq_restore(flags);
1103 static u64 perf_counter_read(struct perf_counter *counter)
1106 * If counter is enabled and currently active on a CPU, update the
1107 * value in the counter structure:
1109 if (counter->state == PERF_COUNTER_STATE_ACTIVE) {
1110 smp_call_function_single(counter->oncpu,
1111 __read, counter, 1);
1112 } else if (counter->state == PERF_COUNTER_STATE_INACTIVE) {
1113 update_counter_times(counter);
1116 return atomic64_read(&counter->count);
1119 static void put_context(struct perf_counter_context *ctx)
1122 put_task_struct(ctx->task);
1125 static struct perf_counter_context *find_get_context(pid_t pid, int cpu)
1127 struct perf_cpu_context *cpuctx;
1128 struct perf_counter_context *ctx;
1129 struct task_struct *task;
1132 * If cpu is not a wildcard then this is a percpu counter:
1135 /* Must be root to operate on a CPU counter: */
1136 if (sysctl_perf_counter_priv && !capable(CAP_SYS_ADMIN))
1137 return ERR_PTR(-EACCES);
1139 if (cpu < 0 || cpu > num_possible_cpus())
1140 return ERR_PTR(-EINVAL);
1143 * We could be clever and allow to attach a counter to an
1144 * offline CPU and activate it when the CPU comes up, but
1147 if (!cpu_isset(cpu, cpu_online_map))
1148 return ERR_PTR(-ENODEV);
1150 cpuctx = &per_cpu(perf_cpu_context, cpu);
1160 task = find_task_by_vpid(pid);
1162 get_task_struct(task);
1166 return ERR_PTR(-ESRCH);
1168 ctx = &task->perf_counter_ctx;
1171 /* Reuse ptrace permission checks for now. */
1172 if (!ptrace_may_access(task, PTRACE_MODE_READ)) {
1174 return ERR_PTR(-EACCES);
1180 static void free_counter_rcu(struct rcu_head *head)
1182 struct perf_counter *counter;
1184 counter = container_of(head, struct perf_counter, rcu_head);
1188 static void perf_pending_sync(struct perf_counter *counter);
1190 static void free_counter(struct perf_counter *counter)
1192 perf_pending_sync(counter);
1194 if (counter->hw_event.mmap)
1195 atomic_dec(&nr_mmap_tracking);
1196 if (counter->hw_event.munmap)
1197 atomic_dec(&nr_munmap_tracking);
1198 if (counter->hw_event.comm)
1199 atomic_dec(&nr_comm_tracking);
1201 if (counter->destroy)
1202 counter->destroy(counter);
1204 call_rcu(&counter->rcu_head, free_counter_rcu);
1208 * Called when the last reference to the file is gone.
1210 static int perf_release(struct inode *inode, struct file *file)
1212 struct perf_counter *counter = file->private_data;
1213 struct perf_counter_context *ctx = counter->ctx;
1215 file->private_data = NULL;
1217 mutex_lock(&ctx->mutex);
1218 mutex_lock(&counter->mutex);
1220 perf_counter_remove_from_context(counter);
1222 mutex_unlock(&counter->mutex);
1223 mutex_unlock(&ctx->mutex);
1225 free_counter(counter);
1232 * Read the performance counter - simple non blocking version for now
1235 perf_read_hw(struct perf_counter *counter, char __user *buf, size_t count)
1241 * Return end-of-file for a read on a counter that is in
1242 * error state (i.e. because it was pinned but it couldn't be
1243 * scheduled on to the CPU at some point).
1245 if (counter->state == PERF_COUNTER_STATE_ERROR)
1248 mutex_lock(&counter->mutex);
1249 values[0] = perf_counter_read(counter);
1251 if (counter->hw_event.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
1252 values[n++] = counter->total_time_enabled +
1253 atomic64_read(&counter->child_total_time_enabled);
1254 if (counter->hw_event.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
1255 values[n++] = counter->total_time_running +
1256 atomic64_read(&counter->child_total_time_running);
1257 mutex_unlock(&counter->mutex);
1259 if (count < n * sizeof(u64))
1261 count = n * sizeof(u64);
1263 if (copy_to_user(buf, values, count))
1270 perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
1272 struct perf_counter *counter = file->private_data;
1274 return perf_read_hw(counter, buf, count);
1277 static unsigned int perf_poll(struct file *file, poll_table *wait)
1279 struct perf_counter *counter = file->private_data;
1280 struct perf_mmap_data *data;
1281 unsigned int events;
1284 data = rcu_dereference(counter->data);
1286 events = atomic_xchg(&data->wakeup, 0);
1291 poll_wait(file, &counter->waitq, wait);
1296 static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1298 struct perf_counter *counter = file->private_data;
1302 case PERF_COUNTER_IOC_ENABLE:
1303 perf_counter_enable_family(counter);
1305 case PERF_COUNTER_IOC_DISABLE:
1306 perf_counter_disable_family(counter);
1308 case PERF_COUNTER_IOC_REFRESH:
1309 perf_counter_refresh(counter, arg);
1318 * Callers need to ensure there can be no nesting of this function, otherwise
1319 * the seqlock logic goes bad. We can not serialize this because the arch
1320 * code calls this from NMI context.
1322 void perf_counter_update_userpage(struct perf_counter *counter)
1324 struct perf_mmap_data *data;
1325 struct perf_counter_mmap_page *userpg;
1328 data = rcu_dereference(counter->data);
1332 userpg = data->user_page;
1335 * Disable preemption so as to not let the corresponding user-space
1336 * spin too long if we get preempted.
1341 userpg->index = counter->hw.idx;
1342 userpg->offset = atomic64_read(&counter->count);
1343 if (counter->state == PERF_COUNTER_STATE_ACTIVE)
1344 userpg->offset -= atomic64_read(&counter->hw.prev_count);
1353 static int perf_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1355 struct perf_counter *counter = vma->vm_file->private_data;
1356 struct perf_mmap_data *data;
1357 int ret = VM_FAULT_SIGBUS;
1360 data = rcu_dereference(counter->data);
1364 if (vmf->pgoff == 0) {
1365 vmf->page = virt_to_page(data->user_page);
1367 int nr = vmf->pgoff - 1;
1369 if ((unsigned)nr > data->nr_pages)
1372 vmf->page = virt_to_page(data->data_pages[nr]);
1374 get_page(vmf->page);
1382 static int perf_mmap_data_alloc(struct perf_counter *counter, int nr_pages)
1384 struct perf_mmap_data *data;
1388 WARN_ON(atomic_read(&counter->mmap_count));
1390 size = sizeof(struct perf_mmap_data);
1391 size += nr_pages * sizeof(void *);
1393 data = kzalloc(size, GFP_KERNEL);
1397 data->user_page = (void *)get_zeroed_page(GFP_KERNEL);
1398 if (!data->user_page)
1399 goto fail_user_page;
1401 for (i = 0; i < nr_pages; i++) {
1402 data->data_pages[i] = (void *)get_zeroed_page(GFP_KERNEL);
1403 if (!data->data_pages[i])
1404 goto fail_data_pages;
1407 data->nr_pages = nr_pages;
1409 rcu_assign_pointer(counter->data, data);
1414 for (i--; i >= 0; i--)
1415 free_page((unsigned long)data->data_pages[i]);
1417 free_page((unsigned long)data->user_page);
1426 static void __perf_mmap_data_free(struct rcu_head *rcu_head)
1428 struct perf_mmap_data *data = container_of(rcu_head,
1429 struct perf_mmap_data, rcu_head);
1432 free_page((unsigned long)data->user_page);
1433 for (i = 0; i < data->nr_pages; i++)
1434 free_page((unsigned long)data->data_pages[i]);
1438 static void perf_mmap_data_free(struct perf_counter *counter)
1440 struct perf_mmap_data *data = counter->data;
1442 WARN_ON(atomic_read(&counter->mmap_count));
1444 rcu_assign_pointer(counter->data, NULL);
1445 call_rcu(&data->rcu_head, __perf_mmap_data_free);
1448 static void perf_mmap_open(struct vm_area_struct *vma)
1450 struct perf_counter *counter = vma->vm_file->private_data;
1452 atomic_inc(&counter->mmap_count);
1455 static void perf_mmap_close(struct vm_area_struct *vma)
1457 struct perf_counter *counter = vma->vm_file->private_data;
1459 if (atomic_dec_and_mutex_lock(&counter->mmap_count,
1460 &counter->mmap_mutex)) {
1461 vma->vm_mm->locked_vm -= counter->data->nr_pages + 1;
1462 perf_mmap_data_free(counter);
1463 mutex_unlock(&counter->mmap_mutex);
1467 static struct vm_operations_struct perf_mmap_vmops = {
1468 .open = perf_mmap_open,
1469 .close = perf_mmap_close,
1470 .fault = perf_mmap_fault,
1473 static int perf_mmap(struct file *file, struct vm_area_struct *vma)
1475 struct perf_counter *counter = file->private_data;
1476 unsigned long vma_size;
1477 unsigned long nr_pages;
1478 unsigned long locked, lock_limit;
1481 if (!(vma->vm_flags & VM_SHARED) || (vma->vm_flags & VM_WRITE))
1484 vma_size = vma->vm_end - vma->vm_start;
1485 nr_pages = (vma_size / PAGE_SIZE) - 1;
1488 * If we have data pages ensure they're a power-of-two number, so we
1489 * can do bitmasks instead of modulo.
1491 if (nr_pages != 0 && !is_power_of_2(nr_pages))
1494 if (vma_size != PAGE_SIZE * (1 + nr_pages))
1497 if (vma->vm_pgoff != 0)
1500 mutex_lock(&counter->mmap_mutex);
1501 if (atomic_inc_not_zero(&counter->mmap_count)) {
1502 if (nr_pages != counter->data->nr_pages)
1507 locked = vma->vm_mm->locked_vm;
1508 locked += nr_pages + 1;
1510 lock_limit = current->signal->rlim[RLIMIT_MEMLOCK].rlim_cur;
1511 lock_limit >>= PAGE_SHIFT;
1513 if ((locked > lock_limit) && !capable(CAP_IPC_LOCK)) {
1518 WARN_ON(counter->data);
1519 ret = perf_mmap_data_alloc(counter, nr_pages);
1523 atomic_set(&counter->mmap_count, 1);
1524 vma->vm_mm->locked_vm += nr_pages + 1;
1526 mutex_unlock(&counter->mmap_mutex);
1528 vma->vm_flags &= ~VM_MAYWRITE;
1529 vma->vm_flags |= VM_RESERVED;
1530 vma->vm_ops = &perf_mmap_vmops;
1535 static int perf_fasync(int fd, struct file *filp, int on)
1537 struct perf_counter *counter = filp->private_data;
1538 struct inode *inode = filp->f_path.dentry->d_inode;
1541 mutex_lock(&inode->i_mutex);
1542 retval = fasync_helper(fd, filp, on, &counter->fasync);
1543 mutex_unlock(&inode->i_mutex);
1551 static const struct file_operations perf_fops = {
1552 .release = perf_release,
1555 .unlocked_ioctl = perf_ioctl,
1556 .compat_ioctl = perf_ioctl,
1558 .fasync = perf_fasync,
1562 * Perf counter wakeup
1564 * If there's data, ensure we set the poll() state and publish everything
1565 * to user-space before waking everybody up.
1568 void perf_counter_wakeup(struct perf_counter *counter)
1570 struct perf_mmap_data *data;
1573 data = rcu_dereference(counter->data);
1575 atomic_set(&data->wakeup, POLL_IN);
1577 * Ensure all data writes are issued before updating the
1578 * user-space data head information. The matching rmb()
1579 * will be in userspace after reading this value.
1582 data->user_page->data_head = atomic_read(&data->head);
1586 wake_up_all(&counter->waitq);
1588 if (counter->pending_kill) {
1589 kill_fasync(&counter->fasync, SIGIO, counter->pending_kill);
1590 counter->pending_kill = 0;
1597 * Handle the case where we need to wakeup up from NMI (or rq->lock) context.
1599 * The NMI bit means we cannot possibly take locks. Therefore, maintain a
1600 * single linked list and use cmpxchg() to add entries lockless.
1603 static void perf_pending_counter(struct perf_pending_entry *entry)
1605 struct perf_counter *counter = container_of(entry,
1606 struct perf_counter, pending);
1608 if (counter->pending_disable) {
1609 counter->pending_disable = 0;
1610 perf_counter_disable(counter);
1613 if (counter->pending_wakeup) {
1614 counter->pending_wakeup = 0;
1615 perf_counter_wakeup(counter);
1619 #define PENDING_TAIL ((struct perf_pending_entry *)-1UL)
1621 static DEFINE_PER_CPU(struct perf_pending_entry *, perf_pending_head) = {
1625 static void perf_pending_queue(struct perf_pending_entry *entry,
1626 void (*func)(struct perf_pending_entry *))
1628 struct perf_pending_entry **head;
1630 if (cmpxchg(&entry->next, NULL, PENDING_TAIL) != NULL)
1635 head = &get_cpu_var(perf_pending_head);
1638 entry->next = *head;
1639 } while (cmpxchg(head, entry->next, entry) != entry->next);
1641 set_perf_counter_pending();
1643 put_cpu_var(perf_pending_head);
1646 static int __perf_pending_run(void)
1648 struct perf_pending_entry *list;
1651 list = xchg(&__get_cpu_var(perf_pending_head), PENDING_TAIL);
1652 while (list != PENDING_TAIL) {
1653 void (*func)(struct perf_pending_entry *);
1654 struct perf_pending_entry *entry = list;
1661 * Ensure we observe the unqueue before we issue the wakeup,
1662 * so that we won't be waiting forever.
1663 * -- see perf_not_pending().
1674 static inline int perf_not_pending(struct perf_counter *counter)
1677 * If we flush on whatever cpu we run, there is a chance we don't
1681 __perf_pending_run();
1685 * Ensure we see the proper queue state before going to sleep
1686 * so that we do not miss the wakeup. -- see perf_pending_handle()
1689 return counter->pending.next == NULL;
1692 static void perf_pending_sync(struct perf_counter *counter)
1694 wait_event(counter->waitq, perf_not_pending(counter));
1697 void perf_counter_do_pending(void)
1699 __perf_pending_run();
1703 * Callchain support -- arch specific
1706 __weak struct perf_callchain_entry *perf_callchain(struct pt_regs *regs)
1715 struct perf_output_handle {
1716 struct perf_counter *counter;
1717 struct perf_mmap_data *data;
1718 unsigned int offset;
1725 static inline void __perf_output_wakeup(struct perf_output_handle *handle)
1728 handle->counter->pending_wakeup = 1;
1729 perf_pending_queue(&handle->counter->pending,
1730 perf_pending_counter);
1732 perf_counter_wakeup(handle->counter);
1735 static int perf_output_begin(struct perf_output_handle *handle,
1736 struct perf_counter *counter, unsigned int size,
1737 int nmi, int overflow)
1739 struct perf_mmap_data *data;
1740 unsigned int offset, head;
1743 data = rcu_dereference(counter->data);
1747 handle->counter = counter;
1749 handle->overflow = overflow;
1751 if (!data->nr_pages)
1755 offset = head = atomic_read(&data->head);
1757 } while (atomic_cmpxchg(&data->head, offset, head) != offset);
1759 handle->data = data;
1760 handle->offset = offset;
1761 handle->head = head;
1762 handle->wakeup = (offset >> PAGE_SHIFT) != (head >> PAGE_SHIFT);
1767 __perf_output_wakeup(handle);
1774 static void perf_output_copy(struct perf_output_handle *handle,
1775 void *buf, unsigned int len)
1777 unsigned int pages_mask;
1778 unsigned int offset;
1782 offset = handle->offset;
1783 pages_mask = handle->data->nr_pages - 1;
1784 pages = handle->data->data_pages;
1787 unsigned int page_offset;
1790 nr = (offset >> PAGE_SHIFT) & pages_mask;
1791 page_offset = offset & (PAGE_SIZE - 1);
1792 size = min_t(unsigned int, PAGE_SIZE - page_offset, len);
1794 memcpy(pages[nr] + page_offset, buf, size);
1801 handle->offset = offset;
1803 WARN_ON_ONCE(handle->offset > handle->head);
1806 #define perf_output_put(handle, x) \
1807 perf_output_copy((handle), &(x), sizeof(x))
1809 static void perf_output_end(struct perf_output_handle *handle)
1811 int wakeup_events = handle->counter->hw_event.wakeup_events;
1813 if (handle->overflow && wakeup_events) {
1814 int events = atomic_inc_return(&handle->data->events);
1815 if (events >= wakeup_events) {
1816 atomic_sub(wakeup_events, &handle->data->events);
1817 __perf_output_wakeup(handle);
1819 } else if (handle->wakeup)
1820 __perf_output_wakeup(handle);
1824 static void perf_counter_output(struct perf_counter *counter,
1825 int nmi, struct pt_regs *regs, u64 addr)
1828 u64 record_type = counter->hw_event.record_type;
1829 struct perf_output_handle handle;
1830 struct perf_event_header header;
1839 struct perf_callchain_entry *callchain = NULL;
1840 int callchain_size = 0;
1844 header.size = sizeof(header);
1846 header.misc = PERF_EVENT_MISC_OVERFLOW;
1847 header.misc |= user_mode(regs) ?
1848 PERF_EVENT_MISC_USER : PERF_EVENT_MISC_KERNEL;
1850 if (record_type & PERF_RECORD_IP) {
1851 ip = instruction_pointer(regs);
1852 header.type |= PERF_RECORD_IP;
1853 header.size += sizeof(ip);
1856 if (record_type & PERF_RECORD_TID) {
1857 /* namespace issues */
1858 tid_entry.pid = current->group_leader->pid;
1859 tid_entry.tid = current->pid;
1861 header.type |= PERF_RECORD_TID;
1862 header.size += sizeof(tid_entry);
1865 if (record_type & PERF_RECORD_TIME) {
1867 * Maybe do better on x86 and provide cpu_clock_nmi()
1869 time = sched_clock();
1871 header.type |= PERF_RECORD_TIME;
1872 header.size += sizeof(u64);
1875 if (record_type & PERF_RECORD_ADDR) {
1876 header.type |= PERF_RECORD_ADDR;
1877 header.size += sizeof(u64);
1880 if (record_type & PERF_RECORD_GROUP) {
1881 header.type |= PERF_RECORD_GROUP;
1882 header.size += sizeof(u64) +
1883 counter->nr_siblings * sizeof(group_entry);
1886 if (record_type & PERF_RECORD_CALLCHAIN) {
1887 callchain = perf_callchain(regs);
1890 callchain_size = (1 + callchain->nr) * sizeof(u64);
1892 header.type |= PERF_RECORD_CALLCHAIN;
1893 header.size += callchain_size;
1897 ret = perf_output_begin(&handle, counter, header.size, nmi, 1);
1901 perf_output_put(&handle, header);
1903 if (record_type & PERF_RECORD_IP)
1904 perf_output_put(&handle, ip);
1906 if (record_type & PERF_RECORD_TID)
1907 perf_output_put(&handle, tid_entry);
1909 if (record_type & PERF_RECORD_TIME)
1910 perf_output_put(&handle, time);
1912 if (record_type & PERF_RECORD_ADDR)
1913 perf_output_put(&handle, addr);
1915 if (record_type & PERF_RECORD_GROUP) {
1916 struct perf_counter *leader, *sub;
1917 u64 nr = counter->nr_siblings;
1919 perf_output_put(&handle, nr);
1921 leader = counter->group_leader;
1922 list_for_each_entry(sub, &leader->sibling_list, list_entry) {
1924 sub->pmu->read(sub);
1926 group_entry.event = sub->hw_event.config;
1927 group_entry.counter = atomic64_read(&sub->count);
1929 perf_output_put(&handle, group_entry);
1934 perf_output_copy(&handle, callchain, callchain_size);
1936 perf_output_end(&handle);
1943 struct perf_comm_event {
1944 struct task_struct *task;
1949 struct perf_event_header header;
1956 static void perf_counter_comm_output(struct perf_counter *counter,
1957 struct perf_comm_event *comm_event)
1959 struct perf_output_handle handle;
1960 int size = comm_event->event.header.size;
1961 int ret = perf_output_begin(&handle, counter, size, 0, 0);
1966 perf_output_put(&handle, comm_event->event);
1967 perf_output_copy(&handle, comm_event->comm,
1968 comm_event->comm_size);
1969 perf_output_end(&handle);
1972 static int perf_counter_comm_match(struct perf_counter *counter,
1973 struct perf_comm_event *comm_event)
1975 if (counter->hw_event.comm &&
1976 comm_event->event.header.type == PERF_EVENT_COMM)
1982 static void perf_counter_comm_ctx(struct perf_counter_context *ctx,
1983 struct perf_comm_event *comm_event)
1985 struct perf_counter *counter;
1987 if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
1991 list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
1992 if (perf_counter_comm_match(counter, comm_event))
1993 perf_counter_comm_output(counter, comm_event);
1998 static void perf_counter_comm_event(struct perf_comm_event *comm_event)
2000 struct perf_cpu_context *cpuctx;
2002 char *comm = comm_event->task->comm;
2004 size = ALIGN(strlen(comm)+1, sizeof(u64));
2006 comm_event->comm = comm;
2007 comm_event->comm_size = size;
2009 comm_event->event.header.size = sizeof(comm_event->event) + size;
2011 cpuctx = &get_cpu_var(perf_cpu_context);
2012 perf_counter_comm_ctx(&cpuctx->ctx, comm_event);
2013 put_cpu_var(perf_cpu_context);
2015 perf_counter_comm_ctx(¤t->perf_counter_ctx, comm_event);
2018 void perf_counter_comm(struct task_struct *task)
2020 struct perf_comm_event comm_event;
2022 if (!atomic_read(&nr_comm_tracking))
2025 comm_event = (struct perf_comm_event){
2028 .header = { .type = PERF_EVENT_COMM, },
2029 .pid = task->group_leader->pid,
2034 perf_counter_comm_event(&comm_event);
2041 struct perf_mmap_event {
2047 struct perf_event_header header;
2057 static void perf_counter_mmap_output(struct perf_counter *counter,
2058 struct perf_mmap_event *mmap_event)
2060 struct perf_output_handle handle;
2061 int size = mmap_event->event.header.size;
2062 int ret = perf_output_begin(&handle, counter, size, 0, 0);
2067 perf_output_put(&handle, mmap_event->event);
2068 perf_output_copy(&handle, mmap_event->file_name,
2069 mmap_event->file_size);
2070 perf_output_end(&handle);
2073 static int perf_counter_mmap_match(struct perf_counter *counter,
2074 struct perf_mmap_event *mmap_event)
2076 if (counter->hw_event.mmap &&
2077 mmap_event->event.header.type == PERF_EVENT_MMAP)
2080 if (counter->hw_event.munmap &&
2081 mmap_event->event.header.type == PERF_EVENT_MUNMAP)
2087 static void perf_counter_mmap_ctx(struct perf_counter_context *ctx,
2088 struct perf_mmap_event *mmap_event)
2090 struct perf_counter *counter;
2092 if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
2096 list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
2097 if (perf_counter_mmap_match(counter, mmap_event))
2098 perf_counter_mmap_output(counter, mmap_event);
2103 static void perf_counter_mmap_event(struct perf_mmap_event *mmap_event)
2105 struct perf_cpu_context *cpuctx;
2106 struct file *file = mmap_event->file;
2113 buf = kzalloc(PATH_MAX, GFP_KERNEL);
2115 name = strncpy(tmp, "//enomem", sizeof(tmp));
2118 name = d_path(&file->f_path, buf, PATH_MAX);
2120 name = strncpy(tmp, "//toolong", sizeof(tmp));
2124 name = strncpy(tmp, "//anon", sizeof(tmp));
2129 size = ALIGN(strlen(name)+1, sizeof(u64));
2131 mmap_event->file_name = name;
2132 mmap_event->file_size = size;
2134 mmap_event->event.header.size = sizeof(mmap_event->event) + size;
2136 cpuctx = &get_cpu_var(perf_cpu_context);
2137 perf_counter_mmap_ctx(&cpuctx->ctx, mmap_event);
2138 put_cpu_var(perf_cpu_context);
2140 perf_counter_mmap_ctx(¤t->perf_counter_ctx, mmap_event);
2145 void perf_counter_mmap(unsigned long addr, unsigned long len,
2146 unsigned long pgoff, struct file *file)
2148 struct perf_mmap_event mmap_event;
2150 if (!atomic_read(&nr_mmap_tracking))
2153 mmap_event = (struct perf_mmap_event){
2156 .header = { .type = PERF_EVENT_MMAP, },
2157 .pid = current->group_leader->pid,
2158 .tid = current->pid,
2165 perf_counter_mmap_event(&mmap_event);
2168 void perf_counter_munmap(unsigned long addr, unsigned long len,
2169 unsigned long pgoff, struct file *file)
2171 struct perf_mmap_event mmap_event;
2173 if (!atomic_read(&nr_munmap_tracking))
2176 mmap_event = (struct perf_mmap_event){
2179 .header = { .type = PERF_EVENT_MUNMAP, },
2180 .pid = current->group_leader->pid,
2181 .tid = current->pid,
2188 perf_counter_mmap_event(&mmap_event);
2192 * Generic counter overflow handling.
2195 int perf_counter_overflow(struct perf_counter *counter,
2196 int nmi, struct pt_regs *regs, u64 addr)
2198 int events = atomic_read(&counter->event_limit);
2201 counter->pending_kill = POLL_IN;
2202 if (events && atomic_dec_and_test(&counter->event_limit)) {
2204 counter->pending_kill = POLL_HUP;
2206 counter->pending_disable = 1;
2207 perf_pending_queue(&counter->pending,
2208 perf_pending_counter);
2210 perf_counter_disable(counter);
2213 perf_counter_output(counter, nmi, regs, addr);
2218 * Generic software counter infrastructure
2221 static void perf_swcounter_update(struct perf_counter *counter)
2223 struct hw_perf_counter *hwc = &counter->hw;
2228 prev = atomic64_read(&hwc->prev_count);
2229 now = atomic64_read(&hwc->count);
2230 if (atomic64_cmpxchg(&hwc->prev_count, prev, now) != prev)
2235 atomic64_add(delta, &counter->count);
2236 atomic64_sub(delta, &hwc->period_left);
2239 static void perf_swcounter_set_period(struct perf_counter *counter)
2241 struct hw_perf_counter *hwc = &counter->hw;
2242 s64 left = atomic64_read(&hwc->period_left);
2243 s64 period = hwc->irq_period;
2245 if (unlikely(left <= -period)) {
2247 atomic64_set(&hwc->period_left, left);
2250 if (unlikely(left <= 0)) {
2252 atomic64_add(period, &hwc->period_left);
2255 atomic64_set(&hwc->prev_count, -left);
2256 atomic64_set(&hwc->count, -left);
2259 static enum hrtimer_restart perf_swcounter_hrtimer(struct hrtimer *hrtimer)
2261 enum hrtimer_restart ret = HRTIMER_RESTART;
2262 struct perf_counter *counter;
2263 struct pt_regs *regs;
2265 counter = container_of(hrtimer, struct perf_counter, hw.hrtimer);
2266 counter->pmu->read(counter);
2268 regs = get_irq_regs();
2270 * In case we exclude kernel IPs or are somehow not in interrupt
2271 * context, provide the next best thing, the user IP.
2273 if ((counter->hw_event.exclude_kernel || !regs) &&
2274 !counter->hw_event.exclude_user)
2275 regs = task_pt_regs(current);
2278 if (perf_counter_overflow(counter, 0, regs, 0))
2279 ret = HRTIMER_NORESTART;
2282 hrtimer_forward_now(hrtimer, ns_to_ktime(counter->hw.irq_period));
2287 static void perf_swcounter_overflow(struct perf_counter *counter,
2288 int nmi, struct pt_regs *regs, u64 addr)
2290 perf_swcounter_update(counter);
2291 perf_swcounter_set_period(counter);
2292 if (perf_counter_overflow(counter, nmi, regs, addr))
2293 /* soft-disable the counter */
2298 static int perf_swcounter_match(struct perf_counter *counter,
2299 enum perf_event_types type,
2300 u32 event, struct pt_regs *regs)
2302 if (counter->state != PERF_COUNTER_STATE_ACTIVE)
2305 if (perf_event_raw(&counter->hw_event))
2308 if (perf_event_type(&counter->hw_event) != type)
2311 if (perf_event_id(&counter->hw_event) != event)
2314 if (counter->hw_event.exclude_user && user_mode(regs))
2317 if (counter->hw_event.exclude_kernel && !user_mode(regs))
2323 static void perf_swcounter_add(struct perf_counter *counter, u64 nr,
2324 int nmi, struct pt_regs *regs, u64 addr)
2326 int neg = atomic64_add_negative(nr, &counter->hw.count);
2327 if (counter->hw.irq_period && !neg)
2328 perf_swcounter_overflow(counter, nmi, regs, addr);
2331 static void perf_swcounter_ctx_event(struct perf_counter_context *ctx,
2332 enum perf_event_types type, u32 event,
2333 u64 nr, int nmi, struct pt_regs *regs,
2336 struct perf_counter *counter;
2338 if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
2342 list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
2343 if (perf_swcounter_match(counter, type, event, regs))
2344 perf_swcounter_add(counter, nr, nmi, regs, addr);
2349 static int *perf_swcounter_recursion_context(struct perf_cpu_context *cpuctx)
2352 return &cpuctx->recursion[3];
2355 return &cpuctx->recursion[2];
2358 return &cpuctx->recursion[1];
2360 return &cpuctx->recursion[0];
2363 static void __perf_swcounter_event(enum perf_event_types type, u32 event,
2364 u64 nr, int nmi, struct pt_regs *regs,
2367 struct perf_cpu_context *cpuctx = &get_cpu_var(perf_cpu_context);
2368 int *recursion = perf_swcounter_recursion_context(cpuctx);
2376 perf_swcounter_ctx_event(&cpuctx->ctx, type, event,
2377 nr, nmi, regs, addr);
2378 if (cpuctx->task_ctx) {
2379 perf_swcounter_ctx_event(cpuctx->task_ctx, type, event,
2380 nr, nmi, regs, addr);
2387 put_cpu_var(perf_cpu_context);
2391 perf_swcounter_event(u32 event, u64 nr, int nmi, struct pt_regs *regs, u64 addr)
2393 __perf_swcounter_event(PERF_TYPE_SOFTWARE, event, nr, nmi, regs, addr);
2396 static void perf_swcounter_read(struct perf_counter *counter)
2398 perf_swcounter_update(counter);
2401 static int perf_swcounter_enable(struct perf_counter *counter)
2403 perf_swcounter_set_period(counter);
2407 static void perf_swcounter_disable(struct perf_counter *counter)
2409 perf_swcounter_update(counter);
2412 static const struct pmu perf_ops_generic = {
2413 .enable = perf_swcounter_enable,
2414 .disable = perf_swcounter_disable,
2415 .read = perf_swcounter_read,
2419 * Software counter: cpu wall time clock
2422 static void cpu_clock_perf_counter_update(struct perf_counter *counter)
2424 int cpu = raw_smp_processor_id();
2428 now = cpu_clock(cpu);
2429 prev = atomic64_read(&counter->hw.prev_count);
2430 atomic64_set(&counter->hw.prev_count, now);
2431 atomic64_add(now - prev, &counter->count);
2434 static int cpu_clock_perf_counter_enable(struct perf_counter *counter)
2436 struct hw_perf_counter *hwc = &counter->hw;
2437 int cpu = raw_smp_processor_id();
2439 atomic64_set(&hwc->prev_count, cpu_clock(cpu));
2440 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
2441 hwc->hrtimer.function = perf_swcounter_hrtimer;
2442 if (hwc->irq_period) {
2443 __hrtimer_start_range_ns(&hwc->hrtimer,
2444 ns_to_ktime(hwc->irq_period), 0,
2445 HRTIMER_MODE_REL, 0);
2451 static void cpu_clock_perf_counter_disable(struct perf_counter *counter)
2453 hrtimer_cancel(&counter->hw.hrtimer);
2454 cpu_clock_perf_counter_update(counter);
2457 static void cpu_clock_perf_counter_read(struct perf_counter *counter)
2459 cpu_clock_perf_counter_update(counter);
2462 static const struct pmu perf_ops_cpu_clock = {
2463 .enable = cpu_clock_perf_counter_enable,
2464 .disable = cpu_clock_perf_counter_disable,
2465 .read = cpu_clock_perf_counter_read,
2469 * Software counter: task time clock
2472 static void task_clock_perf_counter_update(struct perf_counter *counter, u64 now)
2477 prev = atomic64_xchg(&counter->hw.prev_count, now);
2479 atomic64_add(delta, &counter->count);
2482 static int task_clock_perf_counter_enable(struct perf_counter *counter)
2484 struct hw_perf_counter *hwc = &counter->hw;
2487 now = counter->ctx->time;
2489 atomic64_set(&hwc->prev_count, now);
2490 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
2491 hwc->hrtimer.function = perf_swcounter_hrtimer;
2492 if (hwc->irq_period) {
2493 __hrtimer_start_range_ns(&hwc->hrtimer,
2494 ns_to_ktime(hwc->irq_period), 0,
2495 HRTIMER_MODE_REL, 0);
2501 static void task_clock_perf_counter_disable(struct perf_counter *counter)
2503 hrtimer_cancel(&counter->hw.hrtimer);
2504 task_clock_perf_counter_update(counter, counter->ctx->time);
2508 static void task_clock_perf_counter_read(struct perf_counter *counter)
2513 update_context_time(counter->ctx);
2514 time = counter->ctx->time;
2516 u64 now = perf_clock();
2517 u64 delta = now - counter->ctx->timestamp;
2518 time = counter->ctx->time + delta;
2521 task_clock_perf_counter_update(counter, time);
2524 static const struct pmu perf_ops_task_clock = {
2525 .enable = task_clock_perf_counter_enable,
2526 .disable = task_clock_perf_counter_disable,
2527 .read = task_clock_perf_counter_read,
2531 * Software counter: cpu migrations
2534 static inline u64 get_cpu_migrations(struct perf_counter *counter)
2536 struct task_struct *curr = counter->ctx->task;
2539 return curr->se.nr_migrations;
2540 return cpu_nr_migrations(smp_processor_id());
2543 static void cpu_migrations_perf_counter_update(struct perf_counter *counter)
2548 prev = atomic64_read(&counter->hw.prev_count);
2549 now = get_cpu_migrations(counter);
2551 atomic64_set(&counter->hw.prev_count, now);
2555 atomic64_add(delta, &counter->count);
2558 static void cpu_migrations_perf_counter_read(struct perf_counter *counter)
2560 cpu_migrations_perf_counter_update(counter);
2563 static int cpu_migrations_perf_counter_enable(struct perf_counter *counter)
2565 if (counter->prev_state <= PERF_COUNTER_STATE_OFF)
2566 atomic64_set(&counter->hw.prev_count,
2567 get_cpu_migrations(counter));
2571 static void cpu_migrations_perf_counter_disable(struct perf_counter *counter)
2573 cpu_migrations_perf_counter_update(counter);
2576 static const struct pmu perf_ops_cpu_migrations = {
2577 .enable = cpu_migrations_perf_counter_enable,
2578 .disable = cpu_migrations_perf_counter_disable,
2579 .read = cpu_migrations_perf_counter_read,
2582 #ifdef CONFIG_EVENT_PROFILE
2583 void perf_tpcounter_event(int event_id)
2585 struct pt_regs *regs = get_irq_regs();
2588 regs = task_pt_regs(current);
2590 __perf_swcounter_event(PERF_TYPE_TRACEPOINT, event_id, 1, 1, regs, 0);
2592 EXPORT_SYMBOL_GPL(perf_tpcounter_event);
2594 extern int ftrace_profile_enable(int);
2595 extern void ftrace_profile_disable(int);
2597 static void tp_perf_counter_destroy(struct perf_counter *counter)
2599 ftrace_profile_disable(perf_event_id(&counter->hw_event));
2602 static const struct pmu *tp_perf_counter_init(struct perf_counter *counter)
2604 int event_id = perf_event_id(&counter->hw_event);
2607 ret = ftrace_profile_enable(event_id);
2611 counter->destroy = tp_perf_counter_destroy;
2612 counter->hw.irq_period = counter->hw_event.irq_period;
2614 return &perf_ops_generic;
2617 static const struct pmu *tp_perf_counter_init(struct perf_counter *counter)
2623 static const struct pmu *sw_perf_counter_init(struct perf_counter *counter)
2625 struct perf_counter_hw_event *hw_event = &counter->hw_event;
2626 const struct pmu *pmu = NULL;
2627 struct hw_perf_counter *hwc = &counter->hw;
2630 * Software counters (currently) can't in general distinguish
2631 * between user, kernel and hypervisor events.
2632 * However, context switches and cpu migrations are considered
2633 * to be kernel events, and page faults are never hypervisor
2636 switch (perf_event_id(&counter->hw_event)) {
2637 case PERF_COUNT_CPU_CLOCK:
2638 pmu = &perf_ops_cpu_clock;
2640 if (hw_event->irq_period && hw_event->irq_period < 10000)
2641 hw_event->irq_period = 10000;
2643 case PERF_COUNT_TASK_CLOCK:
2645 * If the user instantiates this as a per-cpu counter,
2646 * use the cpu_clock counter instead.
2648 if (counter->ctx->task)
2649 pmu = &perf_ops_task_clock;
2651 pmu = &perf_ops_cpu_clock;
2653 if (hw_event->irq_period && hw_event->irq_period < 10000)
2654 hw_event->irq_period = 10000;
2656 case PERF_COUNT_PAGE_FAULTS:
2657 case PERF_COUNT_PAGE_FAULTS_MIN:
2658 case PERF_COUNT_PAGE_FAULTS_MAJ:
2659 case PERF_COUNT_CONTEXT_SWITCHES:
2660 pmu = &perf_ops_generic;
2662 case PERF_COUNT_CPU_MIGRATIONS:
2663 if (!counter->hw_event.exclude_kernel)
2664 pmu = &perf_ops_cpu_migrations;
2669 hwc->irq_period = hw_event->irq_period;
2675 * Allocate and initialize a counter structure
2677 static struct perf_counter *
2678 perf_counter_alloc(struct perf_counter_hw_event *hw_event,
2680 struct perf_counter_context *ctx,
2681 struct perf_counter *group_leader,
2684 const struct pmu *pmu;
2685 struct perf_counter *counter;
2688 counter = kzalloc(sizeof(*counter), gfpflags);
2690 return ERR_PTR(-ENOMEM);
2693 * Single counters are their own group leaders, with an
2694 * empty sibling list:
2697 group_leader = counter;
2699 mutex_init(&counter->mutex);
2700 INIT_LIST_HEAD(&counter->list_entry);
2701 INIT_LIST_HEAD(&counter->event_entry);
2702 INIT_LIST_HEAD(&counter->sibling_list);
2703 init_waitqueue_head(&counter->waitq);
2705 mutex_init(&counter->mmap_mutex);
2707 INIT_LIST_HEAD(&counter->child_list);
2710 counter->hw_event = *hw_event;
2711 counter->group_leader = group_leader;
2712 counter->pmu = NULL;
2715 counter->state = PERF_COUNTER_STATE_INACTIVE;
2716 if (hw_event->disabled)
2717 counter->state = PERF_COUNTER_STATE_OFF;
2721 if (perf_event_raw(hw_event)) {
2722 pmu = hw_perf_counter_init(counter);
2726 switch (perf_event_type(hw_event)) {
2727 case PERF_TYPE_HARDWARE:
2728 pmu = hw_perf_counter_init(counter);
2731 case PERF_TYPE_SOFTWARE:
2732 pmu = sw_perf_counter_init(counter);
2735 case PERF_TYPE_TRACEPOINT:
2736 pmu = tp_perf_counter_init(counter);
2743 else if (IS_ERR(pmu))
2748 return ERR_PTR(err);
2753 if (counter->hw_event.mmap)
2754 atomic_inc(&nr_mmap_tracking);
2755 if (counter->hw_event.munmap)
2756 atomic_inc(&nr_munmap_tracking);
2757 if (counter->hw_event.comm)
2758 atomic_inc(&nr_comm_tracking);
2764 * sys_perf_counter_open - open a performance counter, associate it to a task/cpu
2766 * @hw_event_uptr: event type attributes for monitoring/sampling
2769 * @group_fd: group leader counter fd
2771 SYSCALL_DEFINE5(perf_counter_open,
2772 const struct perf_counter_hw_event __user *, hw_event_uptr,
2773 pid_t, pid, int, cpu, int, group_fd, unsigned long, flags)
2775 struct perf_counter *counter, *group_leader;
2776 struct perf_counter_hw_event hw_event;
2777 struct perf_counter_context *ctx;
2778 struct file *counter_file = NULL;
2779 struct file *group_file = NULL;
2780 int fput_needed = 0;
2781 int fput_needed2 = 0;
2784 /* for future expandability... */
2788 if (copy_from_user(&hw_event, hw_event_uptr, sizeof(hw_event)) != 0)
2792 * Get the target context (task or percpu):
2794 ctx = find_get_context(pid, cpu);
2796 return PTR_ERR(ctx);
2799 * Look up the group leader (we will attach this counter to it):
2801 group_leader = NULL;
2802 if (group_fd != -1) {
2804 group_file = fget_light(group_fd, &fput_needed);
2806 goto err_put_context;
2807 if (group_file->f_op != &perf_fops)
2808 goto err_put_context;
2810 group_leader = group_file->private_data;
2812 * Do not allow a recursive hierarchy (this new sibling
2813 * becoming part of another group-sibling):
2815 if (group_leader->group_leader != group_leader)
2816 goto err_put_context;
2818 * Do not allow to attach to a group in a different
2819 * task or CPU context:
2821 if (group_leader->ctx != ctx)
2822 goto err_put_context;
2824 * Only a group leader can be exclusive or pinned
2826 if (hw_event.exclusive || hw_event.pinned)
2827 goto err_put_context;
2830 counter = perf_counter_alloc(&hw_event, cpu, ctx, group_leader,
2832 ret = PTR_ERR(counter);
2833 if (IS_ERR(counter))
2834 goto err_put_context;
2836 ret = anon_inode_getfd("[perf_counter]", &perf_fops, counter, 0);
2838 goto err_free_put_context;
2840 counter_file = fget_light(ret, &fput_needed2);
2842 goto err_free_put_context;
2844 counter->filp = counter_file;
2845 mutex_lock(&ctx->mutex);
2846 perf_install_in_context(ctx, counter, cpu);
2847 mutex_unlock(&ctx->mutex);
2849 fput_light(counter_file, fput_needed2);
2852 fput_light(group_file, fput_needed);
2856 err_free_put_context:
2866 * Initialize the perf_counter context in a task_struct:
2869 __perf_counter_init_context(struct perf_counter_context *ctx,
2870 struct task_struct *task)
2872 memset(ctx, 0, sizeof(*ctx));
2873 spin_lock_init(&ctx->lock);
2874 mutex_init(&ctx->mutex);
2875 INIT_LIST_HEAD(&ctx->counter_list);
2876 INIT_LIST_HEAD(&ctx->event_list);
2881 * inherit a counter from parent task to child task:
2883 static struct perf_counter *
2884 inherit_counter(struct perf_counter *parent_counter,
2885 struct task_struct *parent,
2886 struct perf_counter_context *parent_ctx,
2887 struct task_struct *child,
2888 struct perf_counter *group_leader,
2889 struct perf_counter_context *child_ctx)
2891 struct perf_counter *child_counter;
2894 * Instead of creating recursive hierarchies of counters,
2895 * we link inherited counters back to the original parent,
2896 * which has a filp for sure, which we use as the reference
2899 if (parent_counter->parent)
2900 parent_counter = parent_counter->parent;
2902 child_counter = perf_counter_alloc(&parent_counter->hw_event,
2903 parent_counter->cpu, child_ctx,
2904 group_leader, GFP_KERNEL);
2905 if (IS_ERR(child_counter))
2906 return child_counter;
2909 * Link it up in the child's context:
2911 child_counter->task = child;
2912 add_counter_to_ctx(child_counter, child_ctx);
2914 child_counter->parent = parent_counter;
2916 * inherit into child's child as well:
2918 child_counter->hw_event.inherit = 1;
2921 * Get a reference to the parent filp - we will fput it
2922 * when the child counter exits. This is safe to do because
2923 * we are in the parent and we know that the filp still
2924 * exists and has a nonzero count:
2926 atomic_long_inc(&parent_counter->filp->f_count);
2929 * Link this into the parent counter's child list
2931 mutex_lock(&parent_counter->mutex);
2932 list_add_tail(&child_counter->child_list, &parent_counter->child_list);
2935 * Make the child state follow the state of the parent counter,
2936 * not its hw_event.disabled bit. We hold the parent's mutex,
2937 * so we won't race with perf_counter_{en,dis}able_family.
2939 if (parent_counter->state >= PERF_COUNTER_STATE_INACTIVE)
2940 child_counter->state = PERF_COUNTER_STATE_INACTIVE;
2942 child_counter->state = PERF_COUNTER_STATE_OFF;
2944 mutex_unlock(&parent_counter->mutex);
2946 return child_counter;
2949 static int inherit_group(struct perf_counter *parent_counter,
2950 struct task_struct *parent,
2951 struct perf_counter_context *parent_ctx,
2952 struct task_struct *child,
2953 struct perf_counter_context *child_ctx)
2955 struct perf_counter *leader;
2956 struct perf_counter *sub;
2957 struct perf_counter *child_ctr;
2959 leader = inherit_counter(parent_counter, parent, parent_ctx,
2960 child, NULL, child_ctx);
2962 return PTR_ERR(leader);
2963 list_for_each_entry(sub, &parent_counter->sibling_list, list_entry) {
2964 child_ctr = inherit_counter(sub, parent, parent_ctx,
2965 child, leader, child_ctx);
2966 if (IS_ERR(child_ctr))
2967 return PTR_ERR(child_ctr);
2972 static void sync_child_counter(struct perf_counter *child_counter,
2973 struct perf_counter *parent_counter)
2975 u64 parent_val, child_val;
2977 parent_val = atomic64_read(&parent_counter->count);
2978 child_val = atomic64_read(&child_counter->count);
2981 * Add back the child's count to the parent's count:
2983 atomic64_add(child_val, &parent_counter->count);
2984 atomic64_add(child_counter->total_time_enabled,
2985 &parent_counter->child_total_time_enabled);
2986 atomic64_add(child_counter->total_time_running,
2987 &parent_counter->child_total_time_running);
2990 * Remove this counter from the parent's list
2992 mutex_lock(&parent_counter->mutex);
2993 list_del_init(&child_counter->child_list);
2994 mutex_unlock(&parent_counter->mutex);
2997 * Release the parent counter, if this was the last
3000 fput(parent_counter->filp);
3004 __perf_counter_exit_task(struct task_struct *child,
3005 struct perf_counter *child_counter,
3006 struct perf_counter_context *child_ctx)
3008 struct perf_counter *parent_counter;
3009 struct perf_counter *sub, *tmp;
3012 * If we do not self-reap then we have to wait for the
3013 * child task to unschedule (it will happen for sure),
3014 * so that its counter is at its final count. (This
3015 * condition triggers rarely - child tasks usually get
3016 * off their CPU before the parent has a chance to
3017 * get this far into the reaping action)
3019 if (child != current) {
3020 wait_task_inactive(child, 0);
3021 list_del_init(&child_counter->list_entry);
3022 update_counter_times(child_counter);
3024 struct perf_cpu_context *cpuctx;
3025 unsigned long flags;
3029 * Disable and unlink this counter.
3031 * Be careful about zapping the list - IRQ/NMI context
3032 * could still be processing it:
3034 local_irq_save(flags);
3035 perf_flags = hw_perf_save_disable();
3037 cpuctx = &__get_cpu_var(perf_cpu_context);
3039 group_sched_out(child_counter, cpuctx, child_ctx);
3040 update_counter_times(child_counter);
3042 list_del_init(&child_counter->list_entry);
3044 child_ctx->nr_counters--;
3046 hw_perf_restore(perf_flags);
3047 local_irq_restore(flags);
3050 parent_counter = child_counter->parent;
3052 * It can happen that parent exits first, and has counters
3053 * that are still around due to the child reference. These
3054 * counters need to be zapped - but otherwise linger.
3056 if (parent_counter) {
3057 sync_child_counter(child_counter, parent_counter);
3058 list_for_each_entry_safe(sub, tmp, &child_counter->sibling_list,
3061 sync_child_counter(sub, sub->parent);
3065 free_counter(child_counter);
3070 * When a child task exits, feed back counter values to parent counters.
3072 * Note: we may be running in child context, but the PID is not hashed
3073 * anymore so new counters will not be added.
3075 void perf_counter_exit_task(struct task_struct *child)
3077 struct perf_counter *child_counter, *tmp;
3078 struct perf_counter_context *child_ctx;
3080 child_ctx = &child->perf_counter_ctx;
3082 if (likely(!child_ctx->nr_counters))
3085 list_for_each_entry_safe(child_counter, tmp, &child_ctx->counter_list,
3087 __perf_counter_exit_task(child, child_counter, child_ctx);
3091 * Initialize the perf_counter context in task_struct
3093 void perf_counter_init_task(struct task_struct *child)
3095 struct perf_counter_context *child_ctx, *parent_ctx;
3096 struct perf_counter *counter;
3097 struct task_struct *parent = current;
3099 child_ctx = &child->perf_counter_ctx;
3100 parent_ctx = &parent->perf_counter_ctx;
3102 __perf_counter_init_context(child_ctx, child);
3105 * This is executed from the parent task context, so inherit
3106 * counters that have been marked for cloning:
3109 if (likely(!parent_ctx->nr_counters))
3113 * Lock the parent list. No need to lock the child - not PID
3114 * hashed yet and not running, so nobody can access it.
3116 mutex_lock(&parent_ctx->mutex);
3119 * We dont have to disable NMIs - we are only looking at
3120 * the list, not manipulating it:
3122 list_for_each_entry(counter, &parent_ctx->counter_list, list_entry) {
3123 if (!counter->hw_event.inherit)
3126 if (inherit_group(counter, parent,
3127 parent_ctx, child, child_ctx))
3131 mutex_unlock(&parent_ctx->mutex);
3134 static void __cpuinit perf_counter_init_cpu(int cpu)
3136 struct perf_cpu_context *cpuctx;
3138 cpuctx = &per_cpu(perf_cpu_context, cpu);
3139 __perf_counter_init_context(&cpuctx->ctx, NULL);
3141 mutex_lock(&perf_resource_mutex);
3142 cpuctx->max_pertask = perf_max_counters - perf_reserved_percpu;
3143 mutex_unlock(&perf_resource_mutex);
3145 hw_perf_counter_setup(cpu);
3148 #ifdef CONFIG_HOTPLUG_CPU
3149 static void __perf_counter_exit_cpu(void *info)
3151 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
3152 struct perf_counter_context *ctx = &cpuctx->ctx;
3153 struct perf_counter *counter, *tmp;
3155 list_for_each_entry_safe(counter, tmp, &ctx->counter_list, list_entry)
3156 __perf_counter_remove_from_context(counter);
3158 static void perf_counter_exit_cpu(int cpu)
3160 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
3161 struct perf_counter_context *ctx = &cpuctx->ctx;
3163 mutex_lock(&ctx->mutex);
3164 smp_call_function_single(cpu, __perf_counter_exit_cpu, NULL, 1);
3165 mutex_unlock(&ctx->mutex);
3168 static inline void perf_counter_exit_cpu(int cpu) { }
3171 static int __cpuinit
3172 perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
3174 unsigned int cpu = (long)hcpu;
3178 case CPU_UP_PREPARE:
3179 case CPU_UP_PREPARE_FROZEN:
3180 perf_counter_init_cpu(cpu);
3183 case CPU_DOWN_PREPARE:
3184 case CPU_DOWN_PREPARE_FROZEN:
3185 perf_counter_exit_cpu(cpu);
3195 static struct notifier_block __cpuinitdata perf_cpu_nb = {
3196 .notifier_call = perf_cpu_notify,
3199 static int __init perf_counter_init(void)
3201 perf_cpu_notify(&perf_cpu_nb, (unsigned long)CPU_UP_PREPARE,
3202 (void *)(long)smp_processor_id());
3203 register_cpu_notifier(&perf_cpu_nb);
3207 early_initcall(perf_counter_init);
3209 static ssize_t perf_show_reserve_percpu(struct sysdev_class *class, char *buf)
3211 return sprintf(buf, "%d\n", perf_reserved_percpu);
3215 perf_set_reserve_percpu(struct sysdev_class *class,
3219 struct perf_cpu_context *cpuctx;
3223 err = strict_strtoul(buf, 10, &val);
3226 if (val > perf_max_counters)
3229 mutex_lock(&perf_resource_mutex);
3230 perf_reserved_percpu = val;
3231 for_each_online_cpu(cpu) {
3232 cpuctx = &per_cpu(perf_cpu_context, cpu);
3233 spin_lock_irq(&cpuctx->ctx.lock);
3234 mpt = min(perf_max_counters - cpuctx->ctx.nr_counters,
3235 perf_max_counters - perf_reserved_percpu);
3236 cpuctx->max_pertask = mpt;
3237 spin_unlock_irq(&cpuctx->ctx.lock);
3239 mutex_unlock(&perf_resource_mutex);
3244 static ssize_t perf_show_overcommit(struct sysdev_class *class, char *buf)
3246 return sprintf(buf, "%d\n", perf_overcommit);
3250 perf_set_overcommit(struct sysdev_class *class, const char *buf, size_t count)
3255 err = strict_strtoul(buf, 10, &val);
3261 mutex_lock(&perf_resource_mutex);
3262 perf_overcommit = val;
3263 mutex_unlock(&perf_resource_mutex);
3268 static SYSDEV_CLASS_ATTR(
3271 perf_show_reserve_percpu,
3272 perf_set_reserve_percpu
3275 static SYSDEV_CLASS_ATTR(
3278 perf_show_overcommit,
3282 static struct attribute *perfclass_attrs[] = {
3283 &attr_reserve_percpu.attr,
3284 &attr_overcommit.attr,
3288 static struct attribute_group perfclass_attr_group = {
3289 .attrs = perfclass_attrs,
3290 .name = "perf_counters",
3293 static int __init perf_counter_sysfs_init(void)
3295 return sysfs_create_group(&cpu_sysdev_class.kset.kobj,
3296 &perfclass_attr_group);
3298 device_initcall(perf_counter_sysfs_init);