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 u64 __weak hw_perf_save_disable(void) { return 0; }
64 void __weak hw_perf_restore(u64 ctrl) { barrier(); }
65 void __weak hw_perf_counter_setup(int cpu) { barrier(); }
66 int __weak hw_perf_group_sched_in(struct perf_counter *group_leader,
67 struct perf_cpu_context *cpuctx,
68 struct perf_counter_context *ctx, int cpu)
73 void __weak perf_counter_print_debug(void) { }
76 list_add_counter(struct perf_counter *counter, struct perf_counter_context *ctx)
78 struct perf_counter *group_leader = counter->group_leader;
81 * Depending on whether it is a standalone or sibling counter,
82 * add it straight to the context's counter list, or to the group
83 * leader's sibling list:
85 if (group_leader == counter)
86 list_add_tail(&counter->list_entry, &ctx->counter_list);
88 list_add_tail(&counter->list_entry, &group_leader->sibling_list);
89 group_leader->nr_siblings++;
92 list_add_rcu(&counter->event_entry, &ctx->event_list);
96 list_del_counter(struct perf_counter *counter, struct perf_counter_context *ctx)
98 struct perf_counter *sibling, *tmp;
100 list_del_init(&counter->list_entry);
101 list_del_rcu(&counter->event_entry);
103 if (counter->group_leader != counter)
104 counter->group_leader->nr_siblings--;
107 * If this was a group counter with sibling counters then
108 * upgrade the siblings to singleton counters by adding them
109 * to the context list directly:
111 list_for_each_entry_safe(sibling, tmp,
112 &counter->sibling_list, list_entry) {
114 list_move_tail(&sibling->list_entry, &ctx->counter_list);
115 sibling->group_leader = sibling;
120 counter_sched_out(struct perf_counter *counter,
121 struct perf_cpu_context *cpuctx,
122 struct perf_counter_context *ctx)
124 if (counter->state != PERF_COUNTER_STATE_ACTIVE)
127 counter->state = PERF_COUNTER_STATE_INACTIVE;
128 counter->tstamp_stopped = ctx->time;
129 counter->pmu->disable(counter);
132 if (!is_software_counter(counter))
133 cpuctx->active_oncpu--;
135 if (counter->hw_event.exclusive || !cpuctx->active_oncpu)
136 cpuctx->exclusive = 0;
140 group_sched_out(struct perf_counter *group_counter,
141 struct perf_cpu_context *cpuctx,
142 struct perf_counter_context *ctx)
144 struct perf_counter *counter;
146 if (group_counter->state != PERF_COUNTER_STATE_ACTIVE)
149 counter_sched_out(group_counter, cpuctx, ctx);
152 * Schedule out siblings (if any):
154 list_for_each_entry(counter, &group_counter->sibling_list, list_entry)
155 counter_sched_out(counter, cpuctx, ctx);
157 if (group_counter->hw_event.exclusive)
158 cpuctx->exclusive = 0;
162 * Cross CPU call to remove a performance counter
164 * We disable the counter on the hardware level first. After that we
165 * remove it from the context list.
167 static void __perf_counter_remove_from_context(void *info)
169 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
170 struct perf_counter *counter = info;
171 struct perf_counter_context *ctx = counter->ctx;
176 * If this is a task context, we need to check whether it is
177 * the current task context of this cpu. If not it has been
178 * scheduled out before the smp call arrived.
180 if (ctx->task && cpuctx->task_ctx != ctx)
183 spin_lock_irqsave(&ctx->lock, flags);
185 counter_sched_out(counter, cpuctx, ctx);
187 counter->task = NULL;
191 * Protect the list operation against NMI by disabling the
192 * counters on a global level. NOP for non NMI based counters.
194 perf_flags = hw_perf_save_disable();
195 list_del_counter(counter, ctx);
196 hw_perf_restore(perf_flags);
200 * Allow more per task counters with respect to the
203 cpuctx->max_pertask =
204 min(perf_max_counters - ctx->nr_counters,
205 perf_max_counters - perf_reserved_percpu);
208 spin_unlock_irqrestore(&ctx->lock, flags);
213 * Remove the counter from a task's (or a CPU's) list of counters.
215 * Must be called with counter->mutex and ctx->mutex held.
217 * CPU counters are removed with a smp call. For task counters we only
218 * call when the task is on a CPU.
220 static void perf_counter_remove_from_context(struct perf_counter *counter)
222 struct perf_counter_context *ctx = counter->ctx;
223 struct task_struct *task = ctx->task;
227 * Per cpu counters are removed via an smp call and
228 * the removal is always sucessful.
230 smp_call_function_single(counter->cpu,
231 __perf_counter_remove_from_context,
237 task_oncpu_function_call(task, __perf_counter_remove_from_context,
240 spin_lock_irq(&ctx->lock);
242 * If the context is active we need to retry the smp call.
244 if (ctx->nr_active && !list_empty(&counter->list_entry)) {
245 spin_unlock_irq(&ctx->lock);
250 * The lock prevents that this context is scheduled in so we
251 * can remove the counter safely, if the call above did not
254 if (!list_empty(&counter->list_entry)) {
256 list_del_counter(counter, ctx);
257 counter->task = NULL;
259 spin_unlock_irq(&ctx->lock);
262 static inline u64 perf_clock(void)
264 return cpu_clock(smp_processor_id());
268 * Update the record of the current time in a context.
270 static void update_context_time(struct perf_counter_context *ctx)
272 u64 now = perf_clock();
274 ctx->time += now - ctx->timestamp;
275 ctx->timestamp = now;
279 * Update the total_time_enabled and total_time_running fields for a counter.
281 static void update_counter_times(struct perf_counter *counter)
283 struct perf_counter_context *ctx = counter->ctx;
286 if (counter->state < PERF_COUNTER_STATE_INACTIVE)
289 counter->total_time_enabled = ctx->time - counter->tstamp_enabled;
291 if (counter->state == PERF_COUNTER_STATE_INACTIVE)
292 run_end = counter->tstamp_stopped;
296 counter->total_time_running = run_end - counter->tstamp_running;
300 * Update total_time_enabled and total_time_running for all counters in a group.
302 static void update_group_times(struct perf_counter *leader)
304 struct perf_counter *counter;
306 update_counter_times(leader);
307 list_for_each_entry(counter, &leader->sibling_list, list_entry)
308 update_counter_times(counter);
312 * Cross CPU call to disable a performance counter
314 static void __perf_counter_disable(void *info)
316 struct perf_counter *counter = info;
317 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
318 struct perf_counter_context *ctx = counter->ctx;
322 * If this is a per-task counter, need to check whether this
323 * counter's task is the current task on this cpu.
325 if (ctx->task && cpuctx->task_ctx != ctx)
328 spin_lock_irqsave(&ctx->lock, flags);
331 * If the counter is on, turn it off.
332 * If it is in error state, leave it in error state.
334 if (counter->state >= PERF_COUNTER_STATE_INACTIVE) {
335 update_context_time(ctx);
336 update_counter_times(counter);
337 if (counter == counter->group_leader)
338 group_sched_out(counter, cpuctx, ctx);
340 counter_sched_out(counter, cpuctx, ctx);
341 counter->state = PERF_COUNTER_STATE_OFF;
344 spin_unlock_irqrestore(&ctx->lock, flags);
350 static void perf_counter_disable(struct perf_counter *counter)
352 struct perf_counter_context *ctx = counter->ctx;
353 struct task_struct *task = ctx->task;
357 * Disable the counter on the cpu that it's on
359 smp_call_function_single(counter->cpu, __perf_counter_disable,
365 task_oncpu_function_call(task, __perf_counter_disable, counter);
367 spin_lock_irq(&ctx->lock);
369 * If the counter is still active, we need to retry the cross-call.
371 if (counter->state == PERF_COUNTER_STATE_ACTIVE) {
372 spin_unlock_irq(&ctx->lock);
377 * Since we have the lock this context can't be scheduled
378 * in, so we can change the state safely.
380 if (counter->state == PERF_COUNTER_STATE_INACTIVE) {
381 update_counter_times(counter);
382 counter->state = PERF_COUNTER_STATE_OFF;
385 spin_unlock_irq(&ctx->lock);
389 counter_sched_in(struct perf_counter *counter,
390 struct perf_cpu_context *cpuctx,
391 struct perf_counter_context *ctx,
394 if (counter->state <= PERF_COUNTER_STATE_OFF)
397 counter->state = PERF_COUNTER_STATE_ACTIVE;
398 counter->oncpu = cpu; /* TODO: put 'cpu' into cpuctx->cpu */
400 * The new state must be visible before we turn it on in the hardware:
404 if (counter->pmu->enable(counter)) {
405 counter->state = PERF_COUNTER_STATE_INACTIVE;
410 counter->tstamp_running += ctx->time - counter->tstamp_stopped;
412 if (!is_software_counter(counter))
413 cpuctx->active_oncpu++;
416 if (counter->hw_event.exclusive)
417 cpuctx->exclusive = 1;
423 group_sched_in(struct perf_counter *group_counter,
424 struct perf_cpu_context *cpuctx,
425 struct perf_counter_context *ctx,
428 struct perf_counter *counter, *partial_group;
431 if (group_counter->state == PERF_COUNTER_STATE_OFF)
434 ret = hw_perf_group_sched_in(group_counter, cpuctx, ctx, cpu);
436 return ret < 0 ? ret : 0;
438 group_counter->prev_state = group_counter->state;
439 if (counter_sched_in(group_counter, cpuctx, ctx, cpu))
443 * Schedule in siblings as one group (if any):
445 list_for_each_entry(counter, &group_counter->sibling_list, list_entry) {
446 counter->prev_state = counter->state;
447 if (counter_sched_in(counter, cpuctx, ctx, cpu)) {
448 partial_group = counter;
457 * Groups can be scheduled in as one unit only, so undo any
458 * partial group before returning:
460 list_for_each_entry(counter, &group_counter->sibling_list, list_entry) {
461 if (counter == partial_group)
463 counter_sched_out(counter, cpuctx, ctx);
465 counter_sched_out(group_counter, cpuctx, ctx);
471 * Return 1 for a group consisting entirely of software counters,
472 * 0 if the group contains any hardware counters.
474 static int is_software_only_group(struct perf_counter *leader)
476 struct perf_counter *counter;
478 if (!is_software_counter(leader))
481 list_for_each_entry(counter, &leader->sibling_list, list_entry)
482 if (!is_software_counter(counter))
489 * Work out whether we can put this counter group on the CPU now.
491 static int group_can_go_on(struct perf_counter *counter,
492 struct perf_cpu_context *cpuctx,
496 * Groups consisting entirely of software counters can always go on.
498 if (is_software_only_group(counter))
501 * If an exclusive group is already on, no other hardware
502 * counters can go on.
504 if (cpuctx->exclusive)
507 * If this group is exclusive and there are already
508 * counters on the CPU, it can't go on.
510 if (counter->hw_event.exclusive && cpuctx->active_oncpu)
513 * Otherwise, try to add it if all previous groups were able
519 static void add_counter_to_ctx(struct perf_counter *counter,
520 struct perf_counter_context *ctx)
522 list_add_counter(counter, ctx);
524 counter->prev_state = PERF_COUNTER_STATE_OFF;
525 counter->tstamp_enabled = ctx->time;
526 counter->tstamp_running = ctx->time;
527 counter->tstamp_stopped = ctx->time;
531 * Cross CPU call to install and enable a performance counter
533 static void __perf_install_in_context(void *info)
535 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
536 struct perf_counter *counter = info;
537 struct perf_counter_context *ctx = counter->ctx;
538 struct perf_counter *leader = counter->group_leader;
539 int cpu = smp_processor_id();
545 * If this is a task context, we need to check whether it is
546 * the current task context of this cpu. If not it has been
547 * scheduled out before the smp call arrived.
549 if (ctx->task && cpuctx->task_ctx != ctx)
552 spin_lock_irqsave(&ctx->lock, flags);
553 update_context_time(ctx);
556 * Protect the list operation against NMI by disabling the
557 * counters on a global level. NOP for non NMI based counters.
559 perf_flags = hw_perf_save_disable();
561 add_counter_to_ctx(counter, ctx);
564 * Don't put the counter on if it is disabled or if
565 * it is in a group and the group isn't on.
567 if (counter->state != PERF_COUNTER_STATE_INACTIVE ||
568 (leader != counter && leader->state != PERF_COUNTER_STATE_ACTIVE))
572 * An exclusive counter can't go on if there are already active
573 * hardware counters, and no hardware counter can go on if there
574 * is already an exclusive counter on.
576 if (!group_can_go_on(counter, cpuctx, 1))
579 err = counter_sched_in(counter, cpuctx, ctx, cpu);
583 * This counter couldn't go on. If it is in a group
584 * then we have to pull the whole group off.
585 * If the counter group is pinned then put it in error state.
587 if (leader != counter)
588 group_sched_out(leader, cpuctx, ctx);
589 if (leader->hw_event.pinned) {
590 update_group_times(leader);
591 leader->state = PERF_COUNTER_STATE_ERROR;
595 if (!err && !ctx->task && cpuctx->max_pertask)
596 cpuctx->max_pertask--;
599 hw_perf_restore(perf_flags);
601 spin_unlock_irqrestore(&ctx->lock, flags);
605 * Attach a performance counter to a context
607 * First we add the counter to the list with the hardware enable bit
608 * in counter->hw_config cleared.
610 * If the counter is attached to a task which is on a CPU we use a smp
611 * call to enable it in the task context. The task might have been
612 * scheduled away, but we check this in the smp call again.
614 * Must be called with ctx->mutex held.
617 perf_install_in_context(struct perf_counter_context *ctx,
618 struct perf_counter *counter,
621 struct task_struct *task = ctx->task;
625 * Per cpu counters are installed via an smp call and
626 * the install is always sucessful.
628 smp_call_function_single(cpu, __perf_install_in_context,
633 counter->task = task;
635 task_oncpu_function_call(task, __perf_install_in_context,
638 spin_lock_irq(&ctx->lock);
640 * we need to retry the smp call.
642 if (ctx->is_active && list_empty(&counter->list_entry)) {
643 spin_unlock_irq(&ctx->lock);
648 * The lock prevents that this context is scheduled in so we
649 * can add the counter safely, if it the call above did not
652 if (list_empty(&counter->list_entry))
653 add_counter_to_ctx(counter, ctx);
654 spin_unlock_irq(&ctx->lock);
658 * Cross CPU call to enable a performance counter
660 static void __perf_counter_enable(void *info)
662 struct perf_counter *counter = info;
663 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
664 struct perf_counter_context *ctx = counter->ctx;
665 struct perf_counter *leader = counter->group_leader;
666 unsigned long pmuflags;
671 * If this is a per-task counter, need to check whether this
672 * counter's task is the current task on this cpu.
674 if (ctx->task && cpuctx->task_ctx != ctx)
677 spin_lock_irqsave(&ctx->lock, flags);
678 update_context_time(ctx);
680 counter->prev_state = counter->state;
681 if (counter->state >= PERF_COUNTER_STATE_INACTIVE)
683 counter->state = PERF_COUNTER_STATE_INACTIVE;
684 counter->tstamp_enabled = ctx->time - counter->total_time_enabled;
687 * If the counter is in a group and isn't the group leader,
688 * then don't put it on unless the group is on.
690 if (leader != counter && leader->state != PERF_COUNTER_STATE_ACTIVE)
693 if (!group_can_go_on(counter, cpuctx, 1)) {
696 pmuflags = hw_perf_save_disable();
697 if (counter == leader)
698 err = group_sched_in(counter, cpuctx, ctx,
701 err = counter_sched_in(counter, cpuctx, ctx,
703 hw_perf_restore(pmuflags);
708 * If this counter can't go on and it's part of a
709 * group, then the whole group has to come off.
711 if (leader != counter)
712 group_sched_out(leader, cpuctx, ctx);
713 if (leader->hw_event.pinned) {
714 update_group_times(leader);
715 leader->state = PERF_COUNTER_STATE_ERROR;
720 spin_unlock_irqrestore(&ctx->lock, flags);
726 static void perf_counter_enable(struct perf_counter *counter)
728 struct perf_counter_context *ctx = counter->ctx;
729 struct task_struct *task = ctx->task;
733 * Enable the counter on the cpu that it's on
735 smp_call_function_single(counter->cpu, __perf_counter_enable,
740 spin_lock_irq(&ctx->lock);
741 if (counter->state >= PERF_COUNTER_STATE_INACTIVE)
745 * If the counter is in error state, clear that first.
746 * That way, if we see the counter in error state below, we
747 * know that it has gone back into error state, as distinct
748 * from the task having been scheduled away before the
749 * cross-call arrived.
751 if (counter->state == PERF_COUNTER_STATE_ERROR)
752 counter->state = PERF_COUNTER_STATE_OFF;
755 spin_unlock_irq(&ctx->lock);
756 task_oncpu_function_call(task, __perf_counter_enable, counter);
758 spin_lock_irq(&ctx->lock);
761 * If the context is active and the counter is still off,
762 * we need to retry the cross-call.
764 if (ctx->is_active && counter->state == PERF_COUNTER_STATE_OFF)
768 * Since we have the lock this context can't be scheduled
769 * in, so we can change the state safely.
771 if (counter->state == PERF_COUNTER_STATE_OFF) {
772 counter->state = PERF_COUNTER_STATE_INACTIVE;
773 counter->tstamp_enabled =
774 ctx->time - counter->total_time_enabled;
777 spin_unlock_irq(&ctx->lock);
780 static int perf_counter_refresh(struct perf_counter *counter, int refresh)
783 * not supported on inherited counters
785 if (counter->hw_event.inherit)
788 atomic_add(refresh, &counter->event_limit);
789 perf_counter_enable(counter);
794 void __perf_counter_sched_out(struct perf_counter_context *ctx,
795 struct perf_cpu_context *cpuctx)
797 struct perf_counter *counter;
800 spin_lock(&ctx->lock);
802 if (likely(!ctx->nr_counters))
804 update_context_time(ctx);
806 flags = hw_perf_save_disable();
807 if (ctx->nr_active) {
808 list_for_each_entry(counter, &ctx->counter_list, list_entry)
809 group_sched_out(counter, cpuctx, ctx);
811 hw_perf_restore(flags);
813 spin_unlock(&ctx->lock);
817 * Called from scheduler to remove the counters of the current task,
818 * with interrupts disabled.
820 * We stop each counter and update the counter value in counter->count.
822 * This does not protect us against NMI, but disable()
823 * sets the disabled bit in the control field of counter _before_
824 * accessing the counter control register. If a NMI hits, then it will
825 * not restart the counter.
827 void perf_counter_task_sched_out(struct task_struct *task, int cpu)
829 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
830 struct perf_counter_context *ctx = &task->perf_counter_ctx;
831 struct pt_regs *regs;
833 if (likely(!cpuctx->task_ctx))
836 update_context_time(ctx);
838 regs = task_pt_regs(task);
839 perf_swcounter_event(PERF_COUNT_CONTEXT_SWITCHES, 1, 1, regs, 0);
840 __perf_counter_sched_out(ctx, cpuctx);
842 cpuctx->task_ctx = NULL;
845 static void __perf_counter_task_sched_out(struct perf_counter_context *ctx)
847 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
849 __perf_counter_sched_out(ctx, cpuctx);
850 cpuctx->task_ctx = NULL;
853 static void perf_counter_cpu_sched_out(struct perf_cpu_context *cpuctx)
855 __perf_counter_sched_out(&cpuctx->ctx, cpuctx);
859 __perf_counter_sched_in(struct perf_counter_context *ctx,
860 struct perf_cpu_context *cpuctx, int cpu)
862 struct perf_counter *counter;
866 spin_lock(&ctx->lock);
868 if (likely(!ctx->nr_counters))
871 ctx->timestamp = perf_clock();
873 flags = hw_perf_save_disable();
876 * First go through the list and put on any pinned groups
877 * in order to give them the best chance of going on.
879 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
880 if (counter->state <= PERF_COUNTER_STATE_OFF ||
881 !counter->hw_event.pinned)
883 if (counter->cpu != -1 && counter->cpu != cpu)
886 if (group_can_go_on(counter, cpuctx, 1))
887 group_sched_in(counter, cpuctx, ctx, cpu);
890 * If this pinned group hasn't been scheduled,
891 * put it in error state.
893 if (counter->state == PERF_COUNTER_STATE_INACTIVE) {
894 update_group_times(counter);
895 counter->state = PERF_COUNTER_STATE_ERROR;
899 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
901 * Ignore counters in OFF or ERROR state, and
902 * ignore pinned counters since we did them already.
904 if (counter->state <= PERF_COUNTER_STATE_OFF ||
905 counter->hw_event.pinned)
909 * Listen to the 'cpu' scheduling filter constraint
912 if (counter->cpu != -1 && counter->cpu != cpu)
915 if (group_can_go_on(counter, cpuctx, can_add_hw)) {
916 if (group_sched_in(counter, cpuctx, ctx, cpu))
920 hw_perf_restore(flags);
922 spin_unlock(&ctx->lock);
926 * Called from scheduler to add the counters of the current task
927 * with interrupts disabled.
929 * We restore the counter value and then enable it.
931 * This does not protect us against NMI, but enable()
932 * sets the enabled bit in the control field of counter _before_
933 * accessing the counter control register. If a NMI hits, then it will
934 * keep the counter running.
936 void perf_counter_task_sched_in(struct task_struct *task, int cpu)
938 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
939 struct perf_counter_context *ctx = &task->perf_counter_ctx;
941 __perf_counter_sched_in(ctx, cpuctx, cpu);
942 cpuctx->task_ctx = ctx;
945 static void perf_counter_cpu_sched_in(struct perf_cpu_context *cpuctx, int cpu)
947 struct perf_counter_context *ctx = &cpuctx->ctx;
949 __perf_counter_sched_in(ctx, cpuctx, cpu);
952 int perf_counter_task_disable(void)
954 struct task_struct *curr = current;
955 struct perf_counter_context *ctx = &curr->perf_counter_ctx;
956 struct perf_counter *counter;
960 if (likely(!ctx->nr_counters))
963 local_irq_save(flags);
965 __perf_counter_task_sched_out(ctx);
967 spin_lock(&ctx->lock);
970 * Disable all the counters:
972 perf_flags = hw_perf_save_disable();
974 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
975 if (counter->state != PERF_COUNTER_STATE_ERROR) {
976 update_group_times(counter);
977 counter->state = PERF_COUNTER_STATE_OFF;
981 hw_perf_restore(perf_flags);
983 spin_unlock_irqrestore(&ctx->lock, flags);
988 int perf_counter_task_enable(void)
990 struct task_struct *curr = current;
991 struct perf_counter_context *ctx = &curr->perf_counter_ctx;
992 struct perf_counter *counter;
997 if (likely(!ctx->nr_counters))
1000 local_irq_save(flags);
1001 cpu = smp_processor_id();
1003 __perf_counter_task_sched_out(ctx);
1005 spin_lock(&ctx->lock);
1008 * Disable all the counters:
1010 perf_flags = hw_perf_save_disable();
1012 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
1013 if (counter->state > PERF_COUNTER_STATE_OFF)
1015 counter->state = PERF_COUNTER_STATE_INACTIVE;
1016 counter->tstamp_enabled =
1017 ctx->time - counter->total_time_enabled;
1018 counter->hw_event.disabled = 0;
1020 hw_perf_restore(perf_flags);
1022 spin_unlock(&ctx->lock);
1024 perf_counter_task_sched_in(curr, cpu);
1026 local_irq_restore(flags);
1032 * Round-robin a context's counters:
1034 static void rotate_ctx(struct perf_counter_context *ctx)
1036 struct perf_counter *counter;
1039 if (!ctx->nr_counters)
1042 spin_lock(&ctx->lock);
1044 * Rotate the first entry last (works just fine for group counters too):
1046 perf_flags = hw_perf_save_disable();
1047 list_for_each_entry(counter, &ctx->counter_list, list_entry) {
1048 list_move_tail(&counter->list_entry, &ctx->counter_list);
1051 hw_perf_restore(perf_flags);
1053 spin_unlock(&ctx->lock);
1056 void perf_counter_task_tick(struct task_struct *curr, int cpu)
1058 struct perf_cpu_context *cpuctx;
1059 struct perf_counter_context *ctx;
1061 if (!atomic_read(&nr_counters))
1064 cpuctx = &per_cpu(perf_cpu_context, cpu);
1065 ctx = &curr->perf_counter_ctx;
1067 perf_counter_cpu_sched_out(cpuctx);
1068 __perf_counter_task_sched_out(ctx);
1070 rotate_ctx(&cpuctx->ctx);
1073 perf_counter_cpu_sched_in(cpuctx, cpu);
1074 perf_counter_task_sched_in(curr, cpu);
1078 * Cross CPU call to read the hardware counter
1080 static void __read(void *info)
1082 struct perf_counter *counter = info;
1083 struct perf_counter_context *ctx = counter->ctx;
1084 unsigned long flags;
1086 local_irq_save(flags);
1088 update_context_time(ctx);
1089 counter->pmu->read(counter);
1090 update_counter_times(counter);
1091 local_irq_restore(flags);
1094 static u64 perf_counter_read(struct perf_counter *counter)
1097 * If counter is enabled and currently active on a CPU, update the
1098 * value in the counter structure:
1100 if (counter->state == PERF_COUNTER_STATE_ACTIVE) {
1101 smp_call_function_single(counter->oncpu,
1102 __read, counter, 1);
1103 } else if (counter->state == PERF_COUNTER_STATE_INACTIVE) {
1104 update_counter_times(counter);
1107 return atomic64_read(&counter->count);
1110 static void put_context(struct perf_counter_context *ctx)
1113 put_task_struct(ctx->task);
1116 static struct perf_counter_context *find_get_context(pid_t pid, int cpu)
1118 struct perf_cpu_context *cpuctx;
1119 struct perf_counter_context *ctx;
1120 struct task_struct *task;
1123 * If cpu is not a wildcard then this is a percpu counter:
1126 /* Must be root to operate on a CPU counter: */
1127 if (sysctl_perf_counter_priv && !capable(CAP_SYS_ADMIN))
1128 return ERR_PTR(-EACCES);
1130 if (cpu < 0 || cpu > num_possible_cpus())
1131 return ERR_PTR(-EINVAL);
1134 * We could be clever and allow to attach a counter to an
1135 * offline CPU and activate it when the CPU comes up, but
1138 if (!cpu_isset(cpu, cpu_online_map))
1139 return ERR_PTR(-ENODEV);
1141 cpuctx = &per_cpu(perf_cpu_context, cpu);
1151 task = find_task_by_vpid(pid);
1153 get_task_struct(task);
1157 return ERR_PTR(-ESRCH);
1159 ctx = &task->perf_counter_ctx;
1162 /* Reuse ptrace permission checks for now. */
1163 if (!ptrace_may_access(task, PTRACE_MODE_READ)) {
1165 return ERR_PTR(-EACCES);
1171 static void free_counter_rcu(struct rcu_head *head)
1173 struct perf_counter *counter;
1175 counter = container_of(head, struct perf_counter, rcu_head);
1179 static void perf_pending_sync(struct perf_counter *counter);
1181 static void free_counter(struct perf_counter *counter)
1183 perf_pending_sync(counter);
1185 atomic_dec(&nr_counters);
1186 if (counter->hw_event.mmap)
1187 atomic_dec(&nr_mmap_tracking);
1188 if (counter->hw_event.munmap)
1189 atomic_dec(&nr_munmap_tracking);
1190 if (counter->hw_event.comm)
1191 atomic_dec(&nr_comm_tracking);
1193 if (counter->destroy)
1194 counter->destroy(counter);
1196 call_rcu(&counter->rcu_head, free_counter_rcu);
1200 * Called when the last reference to the file is gone.
1202 static int perf_release(struct inode *inode, struct file *file)
1204 struct perf_counter *counter = file->private_data;
1205 struct perf_counter_context *ctx = counter->ctx;
1207 file->private_data = NULL;
1209 mutex_lock(&ctx->mutex);
1210 mutex_lock(&counter->mutex);
1212 perf_counter_remove_from_context(counter);
1214 mutex_unlock(&counter->mutex);
1215 mutex_unlock(&ctx->mutex);
1217 free_counter(counter);
1224 * Read the performance counter - simple non blocking version for now
1227 perf_read_hw(struct perf_counter *counter, char __user *buf, size_t count)
1233 * Return end-of-file for a read on a counter that is in
1234 * error state (i.e. because it was pinned but it couldn't be
1235 * scheduled on to the CPU at some point).
1237 if (counter->state == PERF_COUNTER_STATE_ERROR)
1240 mutex_lock(&counter->mutex);
1241 values[0] = perf_counter_read(counter);
1243 if (counter->hw_event.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
1244 values[n++] = counter->total_time_enabled +
1245 atomic64_read(&counter->child_total_time_enabled);
1246 if (counter->hw_event.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
1247 values[n++] = counter->total_time_running +
1248 atomic64_read(&counter->child_total_time_running);
1249 mutex_unlock(&counter->mutex);
1251 if (count < n * sizeof(u64))
1253 count = n * sizeof(u64);
1255 if (copy_to_user(buf, values, count))
1262 perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
1264 struct perf_counter *counter = file->private_data;
1266 return perf_read_hw(counter, buf, count);
1269 static unsigned int perf_poll(struct file *file, poll_table *wait)
1271 struct perf_counter *counter = file->private_data;
1272 struct perf_mmap_data *data;
1273 unsigned int events = POLL_HUP;
1276 data = rcu_dereference(counter->data);
1278 events = atomic_xchg(&data->poll, 0);
1281 poll_wait(file, &counter->waitq, wait);
1286 static void perf_counter_reset(struct perf_counter *counter)
1288 (void)perf_counter_read(counter);
1289 atomic64_set(&counter->count, 0);
1290 perf_counter_update_userpage(counter);
1293 static void perf_counter_for_each_sibling(struct perf_counter *counter,
1294 void (*func)(struct perf_counter *))
1296 struct perf_counter_context *ctx = counter->ctx;
1297 struct perf_counter *sibling;
1299 spin_lock_irq(&ctx->lock);
1300 counter = counter->group_leader;
1303 list_for_each_entry(sibling, &counter->sibling_list, list_entry)
1305 spin_unlock_irq(&ctx->lock);
1308 static void perf_counter_for_each_child(struct perf_counter *counter,
1309 void (*func)(struct perf_counter *))
1311 struct perf_counter *child;
1313 mutex_lock(&counter->mutex);
1315 list_for_each_entry(child, &counter->child_list, child_list)
1317 mutex_unlock(&counter->mutex);
1320 static void perf_counter_for_each(struct perf_counter *counter,
1321 void (*func)(struct perf_counter *))
1323 struct perf_counter *child;
1325 mutex_lock(&counter->mutex);
1326 perf_counter_for_each_sibling(counter, func);
1327 list_for_each_entry(child, &counter->child_list, child_list)
1328 perf_counter_for_each_sibling(child, func);
1329 mutex_unlock(&counter->mutex);
1332 static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1334 struct perf_counter *counter = file->private_data;
1335 void (*func)(struct perf_counter *);
1339 case PERF_COUNTER_IOC_ENABLE:
1340 func = perf_counter_enable;
1342 case PERF_COUNTER_IOC_DISABLE:
1343 func = perf_counter_disable;
1345 case PERF_COUNTER_IOC_RESET:
1346 func = perf_counter_reset;
1349 case PERF_COUNTER_IOC_REFRESH:
1350 return perf_counter_refresh(counter, arg);
1355 if (flags & PERF_IOC_FLAG_GROUP)
1356 perf_counter_for_each(counter, func);
1358 perf_counter_for_each_child(counter, func);
1364 * Callers need to ensure there can be no nesting of this function, otherwise
1365 * the seqlock logic goes bad. We can not serialize this because the arch
1366 * code calls this from NMI context.
1368 void perf_counter_update_userpage(struct perf_counter *counter)
1370 struct perf_mmap_data *data;
1371 struct perf_counter_mmap_page *userpg;
1374 data = rcu_dereference(counter->data);
1378 userpg = data->user_page;
1381 * Disable preemption so as to not let the corresponding user-space
1382 * spin too long if we get preempted.
1387 userpg->index = counter->hw.idx;
1388 userpg->offset = atomic64_read(&counter->count);
1389 if (counter->state == PERF_COUNTER_STATE_ACTIVE)
1390 userpg->offset -= atomic64_read(&counter->hw.prev_count);
1399 static int perf_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1401 struct perf_counter *counter = vma->vm_file->private_data;
1402 struct perf_mmap_data *data;
1403 int ret = VM_FAULT_SIGBUS;
1406 data = rcu_dereference(counter->data);
1410 if (vmf->pgoff == 0) {
1411 vmf->page = virt_to_page(data->user_page);
1413 int nr = vmf->pgoff - 1;
1415 if ((unsigned)nr > data->nr_pages)
1418 vmf->page = virt_to_page(data->data_pages[nr]);
1420 get_page(vmf->page);
1428 static int perf_mmap_data_alloc(struct perf_counter *counter, int nr_pages)
1430 struct perf_mmap_data *data;
1434 WARN_ON(atomic_read(&counter->mmap_count));
1436 size = sizeof(struct perf_mmap_data);
1437 size += nr_pages * sizeof(void *);
1439 data = kzalloc(size, GFP_KERNEL);
1443 data->user_page = (void *)get_zeroed_page(GFP_KERNEL);
1444 if (!data->user_page)
1445 goto fail_user_page;
1447 for (i = 0; i < nr_pages; i++) {
1448 data->data_pages[i] = (void *)get_zeroed_page(GFP_KERNEL);
1449 if (!data->data_pages[i])
1450 goto fail_data_pages;
1453 data->nr_pages = nr_pages;
1454 atomic_set(&data->lock, -1);
1456 rcu_assign_pointer(counter->data, data);
1461 for (i--; i >= 0; i--)
1462 free_page((unsigned long)data->data_pages[i]);
1464 free_page((unsigned long)data->user_page);
1473 static void __perf_mmap_data_free(struct rcu_head *rcu_head)
1475 struct perf_mmap_data *data = container_of(rcu_head,
1476 struct perf_mmap_data, rcu_head);
1479 free_page((unsigned long)data->user_page);
1480 for (i = 0; i < data->nr_pages; i++)
1481 free_page((unsigned long)data->data_pages[i]);
1485 static void perf_mmap_data_free(struct perf_counter *counter)
1487 struct perf_mmap_data *data = counter->data;
1489 WARN_ON(atomic_read(&counter->mmap_count));
1491 rcu_assign_pointer(counter->data, NULL);
1492 call_rcu(&data->rcu_head, __perf_mmap_data_free);
1495 static void perf_mmap_open(struct vm_area_struct *vma)
1497 struct perf_counter *counter = vma->vm_file->private_data;
1499 atomic_inc(&counter->mmap_count);
1502 static void perf_mmap_close(struct vm_area_struct *vma)
1504 struct perf_counter *counter = vma->vm_file->private_data;
1506 if (atomic_dec_and_mutex_lock(&counter->mmap_count,
1507 &counter->mmap_mutex)) {
1508 vma->vm_mm->locked_vm -= counter->data->nr_locked;
1509 perf_mmap_data_free(counter);
1510 mutex_unlock(&counter->mmap_mutex);
1514 static struct vm_operations_struct perf_mmap_vmops = {
1515 .open = perf_mmap_open,
1516 .close = perf_mmap_close,
1517 .fault = perf_mmap_fault,
1520 static int perf_mmap(struct file *file, struct vm_area_struct *vma)
1522 struct perf_counter *counter = file->private_data;
1523 unsigned long vma_size;
1524 unsigned long nr_pages;
1525 unsigned long locked, lock_limit;
1529 if (!(vma->vm_flags & VM_SHARED) || (vma->vm_flags & VM_WRITE))
1532 vma_size = vma->vm_end - vma->vm_start;
1533 nr_pages = (vma_size / PAGE_SIZE) - 1;
1536 * If we have data pages ensure they're a power-of-two number, so we
1537 * can do bitmasks instead of modulo.
1539 if (nr_pages != 0 && !is_power_of_2(nr_pages))
1542 if (vma_size != PAGE_SIZE * (1 + nr_pages))
1545 if (vma->vm_pgoff != 0)
1548 mutex_lock(&counter->mmap_mutex);
1549 if (atomic_inc_not_zero(&counter->mmap_count)) {
1550 if (nr_pages != counter->data->nr_pages)
1555 extra = nr_pages /* + 1 only account the data pages */;
1556 extra -= sysctl_perf_counter_mlock >> (PAGE_SHIFT - 10);
1560 locked = vma->vm_mm->locked_vm + extra;
1562 lock_limit = current->signal->rlim[RLIMIT_MEMLOCK].rlim_cur;
1563 lock_limit >>= PAGE_SHIFT;
1565 if ((locked > lock_limit) && !capable(CAP_IPC_LOCK)) {
1570 WARN_ON(counter->data);
1571 ret = perf_mmap_data_alloc(counter, nr_pages);
1575 atomic_set(&counter->mmap_count, 1);
1576 vma->vm_mm->locked_vm += extra;
1577 counter->data->nr_locked = extra;
1579 mutex_unlock(&counter->mmap_mutex);
1581 vma->vm_flags &= ~VM_MAYWRITE;
1582 vma->vm_flags |= VM_RESERVED;
1583 vma->vm_ops = &perf_mmap_vmops;
1588 static int perf_fasync(int fd, struct file *filp, int on)
1590 struct perf_counter *counter = filp->private_data;
1591 struct inode *inode = filp->f_path.dentry->d_inode;
1594 mutex_lock(&inode->i_mutex);
1595 retval = fasync_helper(fd, filp, on, &counter->fasync);
1596 mutex_unlock(&inode->i_mutex);
1604 static const struct file_operations perf_fops = {
1605 .release = perf_release,
1608 .unlocked_ioctl = perf_ioctl,
1609 .compat_ioctl = perf_ioctl,
1611 .fasync = perf_fasync,
1615 * Perf counter wakeup
1617 * If there's data, ensure we set the poll() state and publish everything
1618 * to user-space before waking everybody up.
1621 void perf_counter_wakeup(struct perf_counter *counter)
1623 wake_up_all(&counter->waitq);
1625 if (counter->pending_kill) {
1626 kill_fasync(&counter->fasync, SIGIO, counter->pending_kill);
1627 counter->pending_kill = 0;
1634 * Handle the case where we need to wakeup up from NMI (or rq->lock) context.
1636 * The NMI bit means we cannot possibly take locks. Therefore, maintain a
1637 * single linked list and use cmpxchg() to add entries lockless.
1640 static void perf_pending_counter(struct perf_pending_entry *entry)
1642 struct perf_counter *counter = container_of(entry,
1643 struct perf_counter, pending);
1645 if (counter->pending_disable) {
1646 counter->pending_disable = 0;
1647 perf_counter_disable(counter);
1650 if (counter->pending_wakeup) {
1651 counter->pending_wakeup = 0;
1652 perf_counter_wakeup(counter);
1656 #define PENDING_TAIL ((struct perf_pending_entry *)-1UL)
1658 static DEFINE_PER_CPU(struct perf_pending_entry *, perf_pending_head) = {
1662 static void perf_pending_queue(struct perf_pending_entry *entry,
1663 void (*func)(struct perf_pending_entry *))
1665 struct perf_pending_entry **head;
1667 if (cmpxchg(&entry->next, NULL, PENDING_TAIL) != NULL)
1672 head = &get_cpu_var(perf_pending_head);
1675 entry->next = *head;
1676 } while (cmpxchg(head, entry->next, entry) != entry->next);
1678 set_perf_counter_pending();
1680 put_cpu_var(perf_pending_head);
1683 static int __perf_pending_run(void)
1685 struct perf_pending_entry *list;
1688 list = xchg(&__get_cpu_var(perf_pending_head), PENDING_TAIL);
1689 while (list != PENDING_TAIL) {
1690 void (*func)(struct perf_pending_entry *);
1691 struct perf_pending_entry *entry = list;
1698 * Ensure we observe the unqueue before we issue the wakeup,
1699 * so that we won't be waiting forever.
1700 * -- see perf_not_pending().
1711 static inline int perf_not_pending(struct perf_counter *counter)
1714 * If we flush on whatever cpu we run, there is a chance we don't
1718 __perf_pending_run();
1722 * Ensure we see the proper queue state before going to sleep
1723 * so that we do not miss the wakeup. -- see perf_pending_handle()
1726 return counter->pending.next == NULL;
1729 static void perf_pending_sync(struct perf_counter *counter)
1731 wait_event(counter->waitq, perf_not_pending(counter));
1734 void perf_counter_do_pending(void)
1736 __perf_pending_run();
1740 * Callchain support -- arch specific
1743 __weak struct perf_callchain_entry *perf_callchain(struct pt_regs *regs)
1752 struct perf_output_handle {
1753 struct perf_counter *counter;
1754 struct perf_mmap_data *data;
1755 unsigned int offset;
1760 unsigned long flags;
1763 static void perf_output_wakeup(struct perf_output_handle *handle)
1765 atomic_set(&handle->data->poll, POLL_IN);
1768 handle->counter->pending_wakeup = 1;
1769 perf_pending_queue(&handle->counter->pending,
1770 perf_pending_counter);
1772 perf_counter_wakeup(handle->counter);
1776 * Curious locking construct.
1778 * We need to ensure a later event doesn't publish a head when a former
1779 * event isn't done writing. However since we need to deal with NMIs we
1780 * cannot fully serialize things.
1782 * What we do is serialize between CPUs so we only have to deal with NMI
1783 * nesting on a single CPU.
1785 * We only publish the head (and generate a wakeup) when the outer-most
1788 static void perf_output_lock(struct perf_output_handle *handle)
1790 struct perf_mmap_data *data = handle->data;
1795 local_irq_save(handle->flags);
1796 cpu = smp_processor_id();
1798 if (in_nmi() && atomic_read(&data->lock) == cpu)
1801 while (atomic_cmpxchg(&data->lock, -1, cpu) != -1)
1807 static void perf_output_unlock(struct perf_output_handle *handle)
1809 struct perf_mmap_data *data = handle->data;
1812 data->done_head = data->head;
1814 if (!handle->locked)
1819 * The xchg implies a full barrier that ensures all writes are done
1820 * before we publish the new head, matched by a rmb() in userspace when
1821 * reading this position.
1823 while ((head = atomic_xchg(&data->done_head, 0)))
1824 data->user_page->data_head = head;
1827 * NMI can happen here, which means we can miss a done_head update.
1830 cpu = atomic_xchg(&data->lock, -1);
1831 WARN_ON_ONCE(cpu != smp_processor_id());
1834 * Therefore we have to validate we did not indeed do so.
1836 if (unlikely(atomic_read(&data->done_head))) {
1838 * Since we had it locked, we can lock it again.
1840 while (atomic_cmpxchg(&data->lock, -1, cpu) != -1)
1846 if (atomic_xchg(&data->wakeup, 0))
1847 perf_output_wakeup(handle);
1849 local_irq_restore(handle->flags);
1852 static int perf_output_begin(struct perf_output_handle *handle,
1853 struct perf_counter *counter, unsigned int size,
1854 int nmi, int overflow)
1856 struct perf_mmap_data *data;
1857 unsigned int offset, head;
1860 * For inherited counters we send all the output towards the parent.
1862 if (counter->parent)
1863 counter = counter->parent;
1866 data = rcu_dereference(counter->data);
1870 handle->data = data;
1871 handle->counter = counter;
1873 handle->overflow = overflow;
1875 if (!data->nr_pages)
1878 perf_output_lock(handle);
1881 offset = head = atomic_read(&data->head);
1883 } while (atomic_cmpxchg(&data->head, offset, head) != offset);
1885 handle->offset = offset;
1886 handle->head = head;
1888 if ((offset >> PAGE_SHIFT) != (head >> PAGE_SHIFT))
1889 atomic_set(&data->wakeup, 1);
1894 perf_output_wakeup(handle);
1901 static void perf_output_copy(struct perf_output_handle *handle,
1902 void *buf, unsigned int len)
1904 unsigned int pages_mask;
1905 unsigned int offset;
1909 offset = handle->offset;
1910 pages_mask = handle->data->nr_pages - 1;
1911 pages = handle->data->data_pages;
1914 unsigned int page_offset;
1917 nr = (offset >> PAGE_SHIFT) & pages_mask;
1918 page_offset = offset & (PAGE_SIZE - 1);
1919 size = min_t(unsigned int, PAGE_SIZE - page_offset, len);
1921 memcpy(pages[nr] + page_offset, buf, size);
1928 handle->offset = offset;
1930 WARN_ON_ONCE(handle->offset > handle->head);
1933 #define perf_output_put(handle, x) \
1934 perf_output_copy((handle), &(x), sizeof(x))
1936 static void perf_output_end(struct perf_output_handle *handle)
1938 struct perf_counter *counter = handle->counter;
1939 struct perf_mmap_data *data = handle->data;
1941 int wakeup_events = counter->hw_event.wakeup_events;
1943 if (handle->overflow && wakeup_events) {
1944 int events = atomic_inc_return(&data->events);
1945 if (events >= wakeup_events) {
1946 atomic_sub(wakeup_events, &data->events);
1947 atomic_set(&data->wakeup, 1);
1951 perf_output_unlock(handle);
1955 static void perf_counter_output(struct perf_counter *counter,
1956 int nmi, struct pt_regs *regs, u64 addr)
1959 u64 record_type = counter->hw_event.record_type;
1960 struct perf_output_handle handle;
1961 struct perf_event_header header;
1970 struct perf_callchain_entry *callchain = NULL;
1971 int callchain_size = 0;
1978 header.size = sizeof(header);
1980 header.misc = PERF_EVENT_MISC_OVERFLOW;
1981 header.misc |= user_mode(regs) ?
1982 PERF_EVENT_MISC_USER : PERF_EVENT_MISC_KERNEL;
1984 if (record_type & PERF_RECORD_IP) {
1985 ip = instruction_pointer(regs);
1986 header.type |= PERF_RECORD_IP;
1987 header.size += sizeof(ip);
1990 if (record_type & PERF_RECORD_TID) {
1991 /* namespace issues */
1992 tid_entry.pid = current->group_leader->pid;
1993 tid_entry.tid = current->pid;
1995 header.type |= PERF_RECORD_TID;
1996 header.size += sizeof(tid_entry);
1999 if (record_type & PERF_RECORD_TIME) {
2001 * Maybe do better on x86 and provide cpu_clock_nmi()
2003 time = sched_clock();
2005 header.type |= PERF_RECORD_TIME;
2006 header.size += sizeof(u64);
2009 if (record_type & PERF_RECORD_ADDR) {
2010 header.type |= PERF_RECORD_ADDR;
2011 header.size += sizeof(u64);
2014 if (record_type & PERF_RECORD_CONFIG) {
2015 header.type |= PERF_RECORD_CONFIG;
2016 header.size += sizeof(u64);
2019 if (record_type & PERF_RECORD_CPU) {
2020 header.type |= PERF_RECORD_CPU;
2021 header.size += sizeof(cpu_entry);
2023 cpu_entry.cpu = raw_smp_processor_id();
2026 if (record_type & PERF_RECORD_GROUP) {
2027 header.type |= PERF_RECORD_GROUP;
2028 header.size += sizeof(u64) +
2029 counter->nr_siblings * sizeof(group_entry);
2032 if (record_type & PERF_RECORD_CALLCHAIN) {
2033 callchain = perf_callchain(regs);
2036 callchain_size = (1 + callchain->nr) * sizeof(u64);
2038 header.type |= PERF_RECORD_CALLCHAIN;
2039 header.size += callchain_size;
2043 ret = perf_output_begin(&handle, counter, header.size, nmi, 1);
2047 perf_output_put(&handle, header);
2049 if (record_type & PERF_RECORD_IP)
2050 perf_output_put(&handle, ip);
2052 if (record_type & PERF_RECORD_TID)
2053 perf_output_put(&handle, tid_entry);
2055 if (record_type & PERF_RECORD_TIME)
2056 perf_output_put(&handle, time);
2058 if (record_type & PERF_RECORD_ADDR)
2059 perf_output_put(&handle, addr);
2061 if (record_type & PERF_RECORD_CONFIG)
2062 perf_output_put(&handle, counter->hw_event.config);
2064 if (record_type & PERF_RECORD_CPU)
2065 perf_output_put(&handle, cpu_entry);
2068 * XXX PERF_RECORD_GROUP vs inherited counters seems difficult.
2070 if (record_type & PERF_RECORD_GROUP) {
2071 struct perf_counter *leader, *sub;
2072 u64 nr = counter->nr_siblings;
2074 perf_output_put(&handle, nr);
2076 leader = counter->group_leader;
2077 list_for_each_entry(sub, &leader->sibling_list, list_entry) {
2079 sub->pmu->read(sub);
2081 group_entry.event = sub->hw_event.config;
2082 group_entry.counter = atomic64_read(&sub->count);
2084 perf_output_put(&handle, group_entry);
2089 perf_output_copy(&handle, callchain, callchain_size);
2091 perf_output_end(&handle);
2098 struct perf_comm_event {
2099 struct task_struct *task;
2104 struct perf_event_header header;
2111 static void perf_counter_comm_output(struct perf_counter *counter,
2112 struct perf_comm_event *comm_event)
2114 struct perf_output_handle handle;
2115 int size = comm_event->event.header.size;
2116 int ret = perf_output_begin(&handle, counter, size, 0, 0);
2121 perf_output_put(&handle, comm_event->event);
2122 perf_output_copy(&handle, comm_event->comm,
2123 comm_event->comm_size);
2124 perf_output_end(&handle);
2127 static int perf_counter_comm_match(struct perf_counter *counter,
2128 struct perf_comm_event *comm_event)
2130 if (counter->hw_event.comm &&
2131 comm_event->event.header.type == PERF_EVENT_COMM)
2137 static void perf_counter_comm_ctx(struct perf_counter_context *ctx,
2138 struct perf_comm_event *comm_event)
2140 struct perf_counter *counter;
2142 if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
2146 list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
2147 if (perf_counter_comm_match(counter, comm_event))
2148 perf_counter_comm_output(counter, comm_event);
2153 static void perf_counter_comm_event(struct perf_comm_event *comm_event)
2155 struct perf_cpu_context *cpuctx;
2157 char *comm = comm_event->task->comm;
2159 size = ALIGN(strlen(comm)+1, sizeof(u64));
2161 comm_event->comm = comm;
2162 comm_event->comm_size = size;
2164 comm_event->event.header.size = sizeof(comm_event->event) + size;
2166 cpuctx = &get_cpu_var(perf_cpu_context);
2167 perf_counter_comm_ctx(&cpuctx->ctx, comm_event);
2168 put_cpu_var(perf_cpu_context);
2170 perf_counter_comm_ctx(¤t->perf_counter_ctx, comm_event);
2173 void perf_counter_comm(struct task_struct *task)
2175 struct perf_comm_event comm_event;
2177 if (!atomic_read(&nr_comm_tracking))
2180 comm_event = (struct perf_comm_event){
2183 .header = { .type = PERF_EVENT_COMM, },
2184 .pid = task->group_leader->pid,
2189 perf_counter_comm_event(&comm_event);
2196 struct perf_mmap_event {
2202 struct perf_event_header header;
2212 static void perf_counter_mmap_output(struct perf_counter *counter,
2213 struct perf_mmap_event *mmap_event)
2215 struct perf_output_handle handle;
2216 int size = mmap_event->event.header.size;
2217 int ret = perf_output_begin(&handle, counter, size, 0, 0);
2222 perf_output_put(&handle, mmap_event->event);
2223 perf_output_copy(&handle, mmap_event->file_name,
2224 mmap_event->file_size);
2225 perf_output_end(&handle);
2228 static int perf_counter_mmap_match(struct perf_counter *counter,
2229 struct perf_mmap_event *mmap_event)
2231 if (counter->hw_event.mmap &&
2232 mmap_event->event.header.type == PERF_EVENT_MMAP)
2235 if (counter->hw_event.munmap &&
2236 mmap_event->event.header.type == PERF_EVENT_MUNMAP)
2242 static void perf_counter_mmap_ctx(struct perf_counter_context *ctx,
2243 struct perf_mmap_event *mmap_event)
2245 struct perf_counter *counter;
2247 if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
2251 list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
2252 if (perf_counter_mmap_match(counter, mmap_event))
2253 perf_counter_mmap_output(counter, mmap_event);
2258 static void perf_counter_mmap_event(struct perf_mmap_event *mmap_event)
2260 struct perf_cpu_context *cpuctx;
2261 struct file *file = mmap_event->file;
2268 buf = kzalloc(PATH_MAX, GFP_KERNEL);
2270 name = strncpy(tmp, "//enomem", sizeof(tmp));
2273 name = d_path(&file->f_path, buf, PATH_MAX);
2275 name = strncpy(tmp, "//toolong", sizeof(tmp));
2279 name = strncpy(tmp, "//anon", sizeof(tmp));
2284 size = ALIGN(strlen(name)+1, sizeof(u64));
2286 mmap_event->file_name = name;
2287 mmap_event->file_size = size;
2289 mmap_event->event.header.size = sizeof(mmap_event->event) + size;
2291 cpuctx = &get_cpu_var(perf_cpu_context);
2292 perf_counter_mmap_ctx(&cpuctx->ctx, mmap_event);
2293 put_cpu_var(perf_cpu_context);
2295 perf_counter_mmap_ctx(¤t->perf_counter_ctx, mmap_event);
2300 void perf_counter_mmap(unsigned long addr, unsigned long len,
2301 unsigned long pgoff, struct file *file)
2303 struct perf_mmap_event mmap_event;
2305 if (!atomic_read(&nr_mmap_tracking))
2308 mmap_event = (struct perf_mmap_event){
2311 .header = { .type = PERF_EVENT_MMAP, },
2312 .pid = current->group_leader->pid,
2313 .tid = current->pid,
2320 perf_counter_mmap_event(&mmap_event);
2323 void perf_counter_munmap(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_munmap_tracking))
2331 mmap_event = (struct perf_mmap_event){
2334 .header = { .type = PERF_EVENT_MUNMAP, },
2335 .pid = current->group_leader->pid,
2336 .tid = current->pid,
2343 perf_counter_mmap_event(&mmap_event);
2347 * Generic counter overflow handling.
2350 int perf_counter_overflow(struct perf_counter *counter,
2351 int nmi, struct pt_regs *regs, u64 addr)
2353 int events = atomic_read(&counter->event_limit);
2357 * XXX event_limit might not quite work as expected on inherited
2361 counter->pending_kill = POLL_IN;
2362 if (events && atomic_dec_and_test(&counter->event_limit)) {
2364 counter->pending_kill = POLL_HUP;
2366 counter->pending_disable = 1;
2367 perf_pending_queue(&counter->pending,
2368 perf_pending_counter);
2370 perf_counter_disable(counter);
2373 perf_counter_output(counter, nmi, regs, addr);
2378 * Generic software counter infrastructure
2381 static void perf_swcounter_update(struct perf_counter *counter)
2383 struct hw_perf_counter *hwc = &counter->hw;
2388 prev = atomic64_read(&hwc->prev_count);
2389 now = atomic64_read(&hwc->count);
2390 if (atomic64_cmpxchg(&hwc->prev_count, prev, now) != prev)
2395 atomic64_add(delta, &counter->count);
2396 atomic64_sub(delta, &hwc->period_left);
2399 static void perf_swcounter_set_period(struct perf_counter *counter)
2401 struct hw_perf_counter *hwc = &counter->hw;
2402 s64 left = atomic64_read(&hwc->period_left);
2403 s64 period = hwc->irq_period;
2405 if (unlikely(left <= -period)) {
2407 atomic64_set(&hwc->period_left, left);
2410 if (unlikely(left <= 0)) {
2412 atomic64_add(period, &hwc->period_left);
2415 atomic64_set(&hwc->prev_count, -left);
2416 atomic64_set(&hwc->count, -left);
2419 static enum hrtimer_restart perf_swcounter_hrtimer(struct hrtimer *hrtimer)
2421 enum hrtimer_restart ret = HRTIMER_RESTART;
2422 struct perf_counter *counter;
2423 struct pt_regs *regs;
2425 counter = container_of(hrtimer, struct perf_counter, hw.hrtimer);
2426 counter->pmu->read(counter);
2428 regs = get_irq_regs();
2430 * In case we exclude kernel IPs or are somehow not in interrupt
2431 * context, provide the next best thing, the user IP.
2433 if ((counter->hw_event.exclude_kernel || !regs) &&
2434 !counter->hw_event.exclude_user)
2435 regs = task_pt_regs(current);
2438 if (perf_counter_overflow(counter, 0, regs, 0))
2439 ret = HRTIMER_NORESTART;
2442 hrtimer_forward_now(hrtimer, ns_to_ktime(counter->hw.irq_period));
2447 static void perf_swcounter_overflow(struct perf_counter *counter,
2448 int nmi, struct pt_regs *regs, u64 addr)
2450 perf_swcounter_update(counter);
2451 perf_swcounter_set_period(counter);
2452 if (perf_counter_overflow(counter, nmi, regs, addr))
2453 /* soft-disable the counter */
2458 static int perf_swcounter_match(struct perf_counter *counter,
2459 enum perf_event_types type,
2460 u32 event, struct pt_regs *regs)
2462 if (counter->state != PERF_COUNTER_STATE_ACTIVE)
2465 if (perf_event_raw(&counter->hw_event))
2468 if (perf_event_type(&counter->hw_event) != type)
2471 if (perf_event_id(&counter->hw_event) != event)
2474 if (counter->hw_event.exclude_user && user_mode(regs))
2477 if (counter->hw_event.exclude_kernel && !user_mode(regs))
2483 static void perf_swcounter_add(struct perf_counter *counter, u64 nr,
2484 int nmi, struct pt_regs *regs, u64 addr)
2486 int neg = atomic64_add_negative(nr, &counter->hw.count);
2487 if (counter->hw.irq_period && !neg)
2488 perf_swcounter_overflow(counter, nmi, regs, addr);
2491 static void perf_swcounter_ctx_event(struct perf_counter_context *ctx,
2492 enum perf_event_types type, u32 event,
2493 u64 nr, int nmi, struct pt_regs *regs,
2496 struct perf_counter *counter;
2498 if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
2502 list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) {
2503 if (perf_swcounter_match(counter, type, event, regs))
2504 perf_swcounter_add(counter, nr, nmi, regs, addr);
2509 static int *perf_swcounter_recursion_context(struct perf_cpu_context *cpuctx)
2512 return &cpuctx->recursion[3];
2515 return &cpuctx->recursion[2];
2518 return &cpuctx->recursion[1];
2520 return &cpuctx->recursion[0];
2523 static void __perf_swcounter_event(enum perf_event_types type, u32 event,
2524 u64 nr, int nmi, struct pt_regs *regs,
2527 struct perf_cpu_context *cpuctx = &get_cpu_var(perf_cpu_context);
2528 int *recursion = perf_swcounter_recursion_context(cpuctx);
2536 perf_swcounter_ctx_event(&cpuctx->ctx, type, event,
2537 nr, nmi, regs, addr);
2538 if (cpuctx->task_ctx) {
2539 perf_swcounter_ctx_event(cpuctx->task_ctx, type, event,
2540 nr, nmi, regs, addr);
2547 put_cpu_var(perf_cpu_context);
2551 perf_swcounter_event(u32 event, u64 nr, int nmi, struct pt_regs *regs, u64 addr)
2553 __perf_swcounter_event(PERF_TYPE_SOFTWARE, event, nr, nmi, regs, addr);
2556 static void perf_swcounter_read(struct perf_counter *counter)
2558 perf_swcounter_update(counter);
2561 static int perf_swcounter_enable(struct perf_counter *counter)
2563 perf_swcounter_set_period(counter);
2567 static void perf_swcounter_disable(struct perf_counter *counter)
2569 perf_swcounter_update(counter);
2572 static const struct pmu perf_ops_generic = {
2573 .enable = perf_swcounter_enable,
2574 .disable = perf_swcounter_disable,
2575 .read = perf_swcounter_read,
2579 * Software counter: cpu wall time clock
2582 static void cpu_clock_perf_counter_update(struct perf_counter *counter)
2584 int cpu = raw_smp_processor_id();
2588 now = cpu_clock(cpu);
2589 prev = atomic64_read(&counter->hw.prev_count);
2590 atomic64_set(&counter->hw.prev_count, now);
2591 atomic64_add(now - prev, &counter->count);
2594 static int cpu_clock_perf_counter_enable(struct perf_counter *counter)
2596 struct hw_perf_counter *hwc = &counter->hw;
2597 int cpu = raw_smp_processor_id();
2599 atomic64_set(&hwc->prev_count, cpu_clock(cpu));
2600 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
2601 hwc->hrtimer.function = perf_swcounter_hrtimer;
2602 if (hwc->irq_period) {
2603 __hrtimer_start_range_ns(&hwc->hrtimer,
2604 ns_to_ktime(hwc->irq_period), 0,
2605 HRTIMER_MODE_REL, 0);
2611 static void cpu_clock_perf_counter_disable(struct perf_counter *counter)
2613 hrtimer_cancel(&counter->hw.hrtimer);
2614 cpu_clock_perf_counter_update(counter);
2617 static void cpu_clock_perf_counter_read(struct perf_counter *counter)
2619 cpu_clock_perf_counter_update(counter);
2622 static const struct pmu perf_ops_cpu_clock = {
2623 .enable = cpu_clock_perf_counter_enable,
2624 .disable = cpu_clock_perf_counter_disable,
2625 .read = cpu_clock_perf_counter_read,
2629 * Software counter: task time clock
2632 static void task_clock_perf_counter_update(struct perf_counter *counter, u64 now)
2637 prev = atomic64_xchg(&counter->hw.prev_count, now);
2639 atomic64_add(delta, &counter->count);
2642 static int task_clock_perf_counter_enable(struct perf_counter *counter)
2644 struct hw_perf_counter *hwc = &counter->hw;
2647 now = counter->ctx->time;
2649 atomic64_set(&hwc->prev_count, now);
2650 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
2651 hwc->hrtimer.function = perf_swcounter_hrtimer;
2652 if (hwc->irq_period) {
2653 __hrtimer_start_range_ns(&hwc->hrtimer,
2654 ns_to_ktime(hwc->irq_period), 0,
2655 HRTIMER_MODE_REL, 0);
2661 static void task_clock_perf_counter_disable(struct perf_counter *counter)
2663 hrtimer_cancel(&counter->hw.hrtimer);
2664 task_clock_perf_counter_update(counter, counter->ctx->time);
2668 static void task_clock_perf_counter_read(struct perf_counter *counter)
2673 update_context_time(counter->ctx);
2674 time = counter->ctx->time;
2676 u64 now = perf_clock();
2677 u64 delta = now - counter->ctx->timestamp;
2678 time = counter->ctx->time + delta;
2681 task_clock_perf_counter_update(counter, time);
2684 static const struct pmu perf_ops_task_clock = {
2685 .enable = task_clock_perf_counter_enable,
2686 .disable = task_clock_perf_counter_disable,
2687 .read = task_clock_perf_counter_read,
2691 * Software counter: cpu migrations
2694 static inline u64 get_cpu_migrations(struct perf_counter *counter)
2696 struct task_struct *curr = counter->ctx->task;
2699 return curr->se.nr_migrations;
2700 return cpu_nr_migrations(smp_processor_id());
2703 static void cpu_migrations_perf_counter_update(struct perf_counter *counter)
2708 prev = atomic64_read(&counter->hw.prev_count);
2709 now = get_cpu_migrations(counter);
2711 atomic64_set(&counter->hw.prev_count, now);
2715 atomic64_add(delta, &counter->count);
2718 static void cpu_migrations_perf_counter_read(struct perf_counter *counter)
2720 cpu_migrations_perf_counter_update(counter);
2723 static int cpu_migrations_perf_counter_enable(struct perf_counter *counter)
2725 if (counter->prev_state <= PERF_COUNTER_STATE_OFF)
2726 atomic64_set(&counter->hw.prev_count,
2727 get_cpu_migrations(counter));
2731 static void cpu_migrations_perf_counter_disable(struct perf_counter *counter)
2733 cpu_migrations_perf_counter_update(counter);
2736 static const struct pmu perf_ops_cpu_migrations = {
2737 .enable = cpu_migrations_perf_counter_enable,
2738 .disable = cpu_migrations_perf_counter_disable,
2739 .read = cpu_migrations_perf_counter_read,
2742 #ifdef CONFIG_EVENT_PROFILE
2743 void perf_tpcounter_event(int event_id)
2745 struct pt_regs *regs = get_irq_regs();
2748 regs = task_pt_regs(current);
2750 __perf_swcounter_event(PERF_TYPE_TRACEPOINT, event_id, 1, 1, regs, 0);
2752 EXPORT_SYMBOL_GPL(perf_tpcounter_event);
2754 extern int ftrace_profile_enable(int);
2755 extern void ftrace_profile_disable(int);
2757 static void tp_perf_counter_destroy(struct perf_counter *counter)
2759 ftrace_profile_disable(perf_event_id(&counter->hw_event));
2762 static const struct pmu *tp_perf_counter_init(struct perf_counter *counter)
2764 int event_id = perf_event_id(&counter->hw_event);
2767 ret = ftrace_profile_enable(event_id);
2771 counter->destroy = tp_perf_counter_destroy;
2772 counter->hw.irq_period = counter->hw_event.irq_period;
2774 return &perf_ops_generic;
2777 static const struct pmu *tp_perf_counter_init(struct perf_counter *counter)
2783 static const struct pmu *sw_perf_counter_init(struct perf_counter *counter)
2785 struct perf_counter_hw_event *hw_event = &counter->hw_event;
2786 const struct pmu *pmu = NULL;
2787 struct hw_perf_counter *hwc = &counter->hw;
2790 * Software counters (currently) can't in general distinguish
2791 * between user, kernel and hypervisor events.
2792 * However, context switches and cpu migrations are considered
2793 * to be kernel events, and page faults are never hypervisor
2796 switch (perf_event_id(&counter->hw_event)) {
2797 case PERF_COUNT_CPU_CLOCK:
2798 pmu = &perf_ops_cpu_clock;
2800 if (hw_event->irq_period && hw_event->irq_period < 10000)
2801 hw_event->irq_period = 10000;
2803 case PERF_COUNT_TASK_CLOCK:
2805 * If the user instantiates this as a per-cpu counter,
2806 * use the cpu_clock counter instead.
2808 if (counter->ctx->task)
2809 pmu = &perf_ops_task_clock;
2811 pmu = &perf_ops_cpu_clock;
2813 if (hw_event->irq_period && hw_event->irq_period < 10000)
2814 hw_event->irq_period = 10000;
2816 case PERF_COUNT_PAGE_FAULTS:
2817 case PERF_COUNT_PAGE_FAULTS_MIN:
2818 case PERF_COUNT_PAGE_FAULTS_MAJ:
2819 case PERF_COUNT_CONTEXT_SWITCHES:
2820 pmu = &perf_ops_generic;
2822 case PERF_COUNT_CPU_MIGRATIONS:
2823 if (!counter->hw_event.exclude_kernel)
2824 pmu = &perf_ops_cpu_migrations;
2829 hwc->irq_period = hw_event->irq_period;
2835 * Allocate and initialize a counter structure
2837 static struct perf_counter *
2838 perf_counter_alloc(struct perf_counter_hw_event *hw_event,
2840 struct perf_counter_context *ctx,
2841 struct perf_counter *group_leader,
2844 const struct pmu *pmu;
2845 struct perf_counter *counter;
2848 counter = kzalloc(sizeof(*counter), gfpflags);
2850 return ERR_PTR(-ENOMEM);
2853 * Single counters are their own group leaders, with an
2854 * empty sibling list:
2857 group_leader = counter;
2859 mutex_init(&counter->mutex);
2860 INIT_LIST_HEAD(&counter->list_entry);
2861 INIT_LIST_HEAD(&counter->event_entry);
2862 INIT_LIST_HEAD(&counter->sibling_list);
2863 init_waitqueue_head(&counter->waitq);
2865 mutex_init(&counter->mmap_mutex);
2867 INIT_LIST_HEAD(&counter->child_list);
2870 counter->hw_event = *hw_event;
2871 counter->group_leader = group_leader;
2872 counter->pmu = NULL;
2875 counter->state = PERF_COUNTER_STATE_INACTIVE;
2876 if (hw_event->disabled)
2877 counter->state = PERF_COUNTER_STATE_OFF;
2882 * we currently do not support PERF_RECORD_GROUP on inherited counters
2884 if (hw_event->inherit && (hw_event->record_type & PERF_RECORD_GROUP))
2887 if (perf_event_raw(hw_event)) {
2888 pmu = hw_perf_counter_init(counter);
2892 switch (perf_event_type(hw_event)) {
2893 case PERF_TYPE_HARDWARE:
2894 pmu = hw_perf_counter_init(counter);
2897 case PERF_TYPE_SOFTWARE:
2898 pmu = sw_perf_counter_init(counter);
2901 case PERF_TYPE_TRACEPOINT:
2902 pmu = tp_perf_counter_init(counter);
2909 else if (IS_ERR(pmu))
2914 return ERR_PTR(err);
2919 atomic_inc(&nr_counters);
2920 if (counter->hw_event.mmap)
2921 atomic_inc(&nr_mmap_tracking);
2922 if (counter->hw_event.munmap)
2923 atomic_inc(&nr_munmap_tracking);
2924 if (counter->hw_event.comm)
2925 atomic_inc(&nr_comm_tracking);
2931 * sys_perf_counter_open - open a performance counter, associate it to a task/cpu
2933 * @hw_event_uptr: event type attributes for monitoring/sampling
2936 * @group_fd: group leader counter fd
2938 SYSCALL_DEFINE5(perf_counter_open,
2939 const struct perf_counter_hw_event __user *, hw_event_uptr,
2940 pid_t, pid, int, cpu, int, group_fd, unsigned long, flags)
2942 struct perf_counter *counter, *group_leader;
2943 struct perf_counter_hw_event hw_event;
2944 struct perf_counter_context *ctx;
2945 struct file *counter_file = NULL;
2946 struct file *group_file = NULL;
2947 int fput_needed = 0;
2948 int fput_needed2 = 0;
2951 /* for future expandability... */
2955 if (copy_from_user(&hw_event, hw_event_uptr, sizeof(hw_event)) != 0)
2959 * Get the target context (task or percpu):
2961 ctx = find_get_context(pid, cpu);
2963 return PTR_ERR(ctx);
2966 * Look up the group leader (we will attach this counter to it):
2968 group_leader = NULL;
2969 if (group_fd != -1) {
2971 group_file = fget_light(group_fd, &fput_needed);
2973 goto err_put_context;
2974 if (group_file->f_op != &perf_fops)
2975 goto err_put_context;
2977 group_leader = group_file->private_data;
2979 * Do not allow a recursive hierarchy (this new sibling
2980 * becoming part of another group-sibling):
2982 if (group_leader->group_leader != group_leader)
2983 goto err_put_context;
2985 * Do not allow to attach to a group in a different
2986 * task or CPU context:
2988 if (group_leader->ctx != ctx)
2989 goto err_put_context;
2991 * Only a group leader can be exclusive or pinned
2993 if (hw_event.exclusive || hw_event.pinned)
2994 goto err_put_context;
2997 counter = perf_counter_alloc(&hw_event, cpu, ctx, group_leader,
2999 ret = PTR_ERR(counter);
3000 if (IS_ERR(counter))
3001 goto err_put_context;
3003 ret = anon_inode_getfd("[perf_counter]", &perf_fops, counter, 0);
3005 goto err_free_put_context;
3007 counter_file = fget_light(ret, &fput_needed2);
3009 goto err_free_put_context;
3011 counter->filp = counter_file;
3012 mutex_lock(&ctx->mutex);
3013 perf_install_in_context(ctx, counter, cpu);
3014 mutex_unlock(&ctx->mutex);
3016 fput_light(counter_file, fput_needed2);
3019 fput_light(group_file, fput_needed);
3023 err_free_put_context:
3033 * Initialize the perf_counter context in a task_struct:
3036 __perf_counter_init_context(struct perf_counter_context *ctx,
3037 struct task_struct *task)
3039 memset(ctx, 0, sizeof(*ctx));
3040 spin_lock_init(&ctx->lock);
3041 mutex_init(&ctx->mutex);
3042 INIT_LIST_HEAD(&ctx->counter_list);
3043 INIT_LIST_HEAD(&ctx->event_list);
3048 * inherit a counter from parent task to child task:
3050 static struct perf_counter *
3051 inherit_counter(struct perf_counter *parent_counter,
3052 struct task_struct *parent,
3053 struct perf_counter_context *parent_ctx,
3054 struct task_struct *child,
3055 struct perf_counter *group_leader,
3056 struct perf_counter_context *child_ctx)
3058 struct perf_counter *child_counter;
3061 * Instead of creating recursive hierarchies of counters,
3062 * we link inherited counters back to the original parent,
3063 * which has a filp for sure, which we use as the reference
3066 if (parent_counter->parent)
3067 parent_counter = parent_counter->parent;
3069 child_counter = perf_counter_alloc(&parent_counter->hw_event,
3070 parent_counter->cpu, child_ctx,
3071 group_leader, GFP_KERNEL);
3072 if (IS_ERR(child_counter))
3073 return child_counter;
3076 * Link it up in the child's context:
3078 child_counter->task = child;
3079 add_counter_to_ctx(child_counter, child_ctx);
3081 child_counter->parent = parent_counter;
3083 * inherit into child's child as well:
3085 child_counter->hw_event.inherit = 1;
3088 * Get a reference to the parent filp - we will fput it
3089 * when the child counter exits. This is safe to do because
3090 * we are in the parent and we know that the filp still
3091 * exists and has a nonzero count:
3093 atomic_long_inc(&parent_counter->filp->f_count);
3096 * Link this into the parent counter's child list
3098 mutex_lock(&parent_counter->mutex);
3099 list_add_tail(&child_counter->child_list, &parent_counter->child_list);
3102 * Make the child state follow the state of the parent counter,
3103 * not its hw_event.disabled bit. We hold the parent's mutex,
3104 * so we won't race with perf_counter_{en,dis}able_family.
3106 if (parent_counter->state >= PERF_COUNTER_STATE_INACTIVE)
3107 child_counter->state = PERF_COUNTER_STATE_INACTIVE;
3109 child_counter->state = PERF_COUNTER_STATE_OFF;
3111 mutex_unlock(&parent_counter->mutex);
3113 return child_counter;
3116 static int inherit_group(struct perf_counter *parent_counter,
3117 struct task_struct *parent,
3118 struct perf_counter_context *parent_ctx,
3119 struct task_struct *child,
3120 struct perf_counter_context *child_ctx)
3122 struct perf_counter *leader;
3123 struct perf_counter *sub;
3124 struct perf_counter *child_ctr;
3126 leader = inherit_counter(parent_counter, parent, parent_ctx,
3127 child, NULL, child_ctx);
3129 return PTR_ERR(leader);
3130 list_for_each_entry(sub, &parent_counter->sibling_list, list_entry) {
3131 child_ctr = inherit_counter(sub, parent, parent_ctx,
3132 child, leader, child_ctx);
3133 if (IS_ERR(child_ctr))
3134 return PTR_ERR(child_ctr);
3139 static void sync_child_counter(struct perf_counter *child_counter,
3140 struct perf_counter *parent_counter)
3142 u64 parent_val, child_val;
3144 parent_val = atomic64_read(&parent_counter->count);
3145 child_val = atomic64_read(&child_counter->count);
3148 * Add back the child's count to the parent's count:
3150 atomic64_add(child_val, &parent_counter->count);
3151 atomic64_add(child_counter->total_time_enabled,
3152 &parent_counter->child_total_time_enabled);
3153 atomic64_add(child_counter->total_time_running,
3154 &parent_counter->child_total_time_running);
3157 * Remove this counter from the parent's list
3159 mutex_lock(&parent_counter->mutex);
3160 list_del_init(&child_counter->child_list);
3161 mutex_unlock(&parent_counter->mutex);
3164 * Release the parent counter, if this was the last
3167 fput(parent_counter->filp);
3171 __perf_counter_exit_task(struct task_struct *child,
3172 struct perf_counter *child_counter,
3173 struct perf_counter_context *child_ctx)
3175 struct perf_counter *parent_counter;
3176 struct perf_counter *sub, *tmp;
3179 * If we do not self-reap then we have to wait for the
3180 * child task to unschedule (it will happen for sure),
3181 * so that its counter is at its final count. (This
3182 * condition triggers rarely - child tasks usually get
3183 * off their CPU before the parent has a chance to
3184 * get this far into the reaping action)
3186 if (child != current) {
3187 wait_task_inactive(child, 0);
3188 list_del_init(&child_counter->list_entry);
3189 update_counter_times(child_counter);
3191 struct perf_cpu_context *cpuctx;
3192 unsigned long flags;
3196 * Disable and unlink this counter.
3198 * Be careful about zapping the list - IRQ/NMI context
3199 * could still be processing it:
3201 local_irq_save(flags);
3202 perf_flags = hw_perf_save_disable();
3204 cpuctx = &__get_cpu_var(perf_cpu_context);
3206 group_sched_out(child_counter, cpuctx, child_ctx);
3207 update_counter_times(child_counter);
3209 list_del_init(&child_counter->list_entry);
3211 child_ctx->nr_counters--;
3213 hw_perf_restore(perf_flags);
3214 local_irq_restore(flags);
3217 parent_counter = child_counter->parent;
3219 * It can happen that parent exits first, and has counters
3220 * that are still around due to the child reference. These
3221 * counters need to be zapped - but otherwise linger.
3223 if (parent_counter) {
3224 sync_child_counter(child_counter, parent_counter);
3225 list_for_each_entry_safe(sub, tmp, &child_counter->sibling_list,
3228 sync_child_counter(sub, sub->parent);
3232 free_counter(child_counter);
3237 * When a child task exits, feed back counter values to parent counters.
3239 * Note: we may be running in child context, but the PID is not hashed
3240 * anymore so new counters will not be added.
3242 void perf_counter_exit_task(struct task_struct *child)
3244 struct perf_counter *child_counter, *tmp;
3245 struct perf_counter_context *child_ctx;
3247 child_ctx = &child->perf_counter_ctx;
3249 if (likely(!child_ctx->nr_counters))
3252 list_for_each_entry_safe(child_counter, tmp, &child_ctx->counter_list,
3254 __perf_counter_exit_task(child, child_counter, child_ctx);
3258 * Initialize the perf_counter context in task_struct
3260 void perf_counter_init_task(struct task_struct *child)
3262 struct perf_counter_context *child_ctx, *parent_ctx;
3263 struct perf_counter *counter;
3264 struct task_struct *parent = current;
3266 child_ctx = &child->perf_counter_ctx;
3267 parent_ctx = &parent->perf_counter_ctx;
3269 __perf_counter_init_context(child_ctx, child);
3272 * This is executed from the parent task context, so inherit
3273 * counters that have been marked for cloning:
3276 if (likely(!parent_ctx->nr_counters))
3280 * Lock the parent list. No need to lock the child - not PID
3281 * hashed yet and not running, so nobody can access it.
3283 mutex_lock(&parent_ctx->mutex);
3286 * We dont have to disable NMIs - we are only looking at
3287 * the list, not manipulating it:
3289 list_for_each_entry(counter, &parent_ctx->counter_list, list_entry) {
3290 if (!counter->hw_event.inherit)
3293 if (inherit_group(counter, parent,
3294 parent_ctx, child, child_ctx))
3298 mutex_unlock(&parent_ctx->mutex);
3301 static void __cpuinit perf_counter_init_cpu(int cpu)
3303 struct perf_cpu_context *cpuctx;
3305 cpuctx = &per_cpu(perf_cpu_context, cpu);
3306 __perf_counter_init_context(&cpuctx->ctx, NULL);
3308 spin_lock(&perf_resource_lock);
3309 cpuctx->max_pertask = perf_max_counters - perf_reserved_percpu;
3310 spin_unlock(&perf_resource_lock);
3312 hw_perf_counter_setup(cpu);
3315 #ifdef CONFIG_HOTPLUG_CPU
3316 static void __perf_counter_exit_cpu(void *info)
3318 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
3319 struct perf_counter_context *ctx = &cpuctx->ctx;
3320 struct perf_counter *counter, *tmp;
3322 list_for_each_entry_safe(counter, tmp, &ctx->counter_list, list_entry)
3323 __perf_counter_remove_from_context(counter);
3325 static void perf_counter_exit_cpu(int cpu)
3327 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
3328 struct perf_counter_context *ctx = &cpuctx->ctx;
3330 mutex_lock(&ctx->mutex);
3331 smp_call_function_single(cpu, __perf_counter_exit_cpu, NULL, 1);
3332 mutex_unlock(&ctx->mutex);
3335 static inline void perf_counter_exit_cpu(int cpu) { }
3338 static int __cpuinit
3339 perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
3341 unsigned int cpu = (long)hcpu;
3345 case CPU_UP_PREPARE:
3346 case CPU_UP_PREPARE_FROZEN:
3347 perf_counter_init_cpu(cpu);
3350 case CPU_DOWN_PREPARE:
3351 case CPU_DOWN_PREPARE_FROZEN:
3352 perf_counter_exit_cpu(cpu);
3362 static struct notifier_block __cpuinitdata perf_cpu_nb = {
3363 .notifier_call = perf_cpu_notify,
3366 void __init perf_counter_init(void)
3368 perf_cpu_notify(&perf_cpu_nb, (unsigned long)CPU_UP_PREPARE,
3369 (void *)(long)smp_processor_id());
3370 register_cpu_notifier(&perf_cpu_nb);
3373 static ssize_t perf_show_reserve_percpu(struct sysdev_class *class, char *buf)
3375 return sprintf(buf, "%d\n", perf_reserved_percpu);
3379 perf_set_reserve_percpu(struct sysdev_class *class,
3383 struct perf_cpu_context *cpuctx;
3387 err = strict_strtoul(buf, 10, &val);
3390 if (val > perf_max_counters)
3393 spin_lock(&perf_resource_lock);
3394 perf_reserved_percpu = val;
3395 for_each_online_cpu(cpu) {
3396 cpuctx = &per_cpu(perf_cpu_context, cpu);
3397 spin_lock_irq(&cpuctx->ctx.lock);
3398 mpt = min(perf_max_counters - cpuctx->ctx.nr_counters,
3399 perf_max_counters - perf_reserved_percpu);
3400 cpuctx->max_pertask = mpt;
3401 spin_unlock_irq(&cpuctx->ctx.lock);
3403 spin_unlock(&perf_resource_lock);
3408 static ssize_t perf_show_overcommit(struct sysdev_class *class, char *buf)
3410 return sprintf(buf, "%d\n", perf_overcommit);
3414 perf_set_overcommit(struct sysdev_class *class, const char *buf, size_t count)
3419 err = strict_strtoul(buf, 10, &val);
3425 spin_lock(&perf_resource_lock);
3426 perf_overcommit = val;
3427 spin_unlock(&perf_resource_lock);
3432 static SYSDEV_CLASS_ATTR(
3435 perf_show_reserve_percpu,
3436 perf_set_reserve_percpu
3439 static SYSDEV_CLASS_ATTR(
3442 perf_show_overcommit,
3446 static struct attribute *perfclass_attrs[] = {
3447 &attr_reserve_percpu.attr,
3448 &attr_overcommit.attr,
3452 static struct attribute_group perfclass_attr_group = {
3453 .attrs = perfclass_attrs,
3454 .name = "perf_counters",
3457 static int __init perf_counter_sysfs_init(void)
3459 return sysfs_create_group(&cpu_sysdev_class.kset.kobj,
3460 &perfclass_attr_group);
3462 device_initcall(perf_counter_sysfs_init);