2 * Performance counter core code
4 * Copyright (C) 2008 Thomas Gleixner <tglx@linutronix.de>
5 * Copyright (C) 2008-2009 Red Hat, Inc., Ingo Molnar
6 * Copyright (C) 2008-2009 Red Hat, Inc., Peter Zijlstra <pzijlstr@redhat.com>
7 * Copyright © 2009 Paul Mackerras, IBM Corp. <paulus@au1.ibm.com>
9 * For licensing details see kernel-base/COPYING
14 #include <linux/cpu.h>
15 #include <linux/smp.h>
16 #include <linux/file.h>
17 #include <linux/poll.h>
18 #include <linux/sysfs.h>
19 #include <linux/ptrace.h>
20 #include <linux/percpu.h>
21 #include <linux/vmstat.h>
22 #include <linux/hardirq.h>
23 #include <linux/rculist.h>
24 #include <linux/uaccess.h>
25 #include <linux/syscalls.h>
26 #include <linux/anon_inodes.h>
27 #include <linux/kernel_stat.h>
28 #include <linux/perf_counter.h>
29 #include <linux/dcache.h>
31 #include <asm/irq_regs.h>
34 * Each CPU has a list of per CPU counters:
36 DEFINE_PER_CPU(struct perf_cpu_context
, perf_cpu_context
);
38 int perf_max_counters __read_mostly
= 1;
39 static int perf_reserved_percpu __read_mostly
;
40 static int perf_overcommit __read_mostly
= 1;
42 static atomic_t nr_counters __read_mostly
;
43 static atomic_t nr_mmap_tracking __read_mostly
;
44 static atomic_t nr_munmap_tracking __read_mostly
;
45 static atomic_t nr_comm_tracking __read_mostly
;
47 int sysctl_perf_counter_priv __read_mostly
; /* do we need to be privileged */
48 int sysctl_perf_counter_mlock __read_mostly
= 512; /* 'free' kb per user */
51 * Lock for (sysadmin-configurable) counter reservations:
53 static DEFINE_SPINLOCK(perf_resource_lock
);
56 * Architecture provided APIs - weak aliases:
58 extern __weak
const struct pmu
*hw_perf_counter_init(struct perf_counter
*counter
)
63 void __weak
hw_perf_disable(void) { barrier(); }
64 void __weak
hw_perf_enable(void) { barrier(); }
66 void __weak
hw_perf_counter_setup(int cpu
) { barrier(); }
67 int __weak
hw_perf_group_sched_in(struct perf_counter
*group_leader
,
68 struct perf_cpu_context
*cpuctx
,
69 struct perf_counter_context
*ctx
, int cpu
)
74 void __weak
perf_counter_print_debug(void) { }
76 static DEFINE_PER_CPU(int, disable_count
);
78 void __perf_disable(void)
80 __get_cpu_var(disable_count
)++;
83 bool __perf_enable(void)
85 return !--__get_cpu_var(disable_count
);
88 void perf_disable(void)
94 void perf_enable(void)
101 list_add_counter(struct perf_counter
*counter
, struct perf_counter_context
*ctx
)
103 struct perf_counter
*group_leader
= counter
->group_leader
;
106 * Depending on whether it is a standalone or sibling counter,
107 * add it straight to the context's counter list, or to the group
108 * leader's sibling list:
110 if (group_leader
== counter
)
111 list_add_tail(&counter
->list_entry
, &ctx
->counter_list
);
113 list_add_tail(&counter
->list_entry
, &group_leader
->sibling_list
);
114 group_leader
->nr_siblings
++;
117 list_add_rcu(&counter
->event_entry
, &ctx
->event_list
);
122 list_del_counter(struct perf_counter
*counter
, struct perf_counter_context
*ctx
)
124 struct perf_counter
*sibling
, *tmp
;
128 list_del_init(&counter
->list_entry
);
129 list_del_rcu(&counter
->event_entry
);
131 if (counter
->group_leader
!= counter
)
132 counter
->group_leader
->nr_siblings
--;
135 * If this was a group counter with sibling counters then
136 * upgrade the siblings to singleton counters by adding them
137 * to the context list directly:
139 list_for_each_entry_safe(sibling
, tmp
,
140 &counter
->sibling_list
, list_entry
) {
142 list_move_tail(&sibling
->list_entry
, &ctx
->counter_list
);
143 sibling
->group_leader
= sibling
;
148 counter_sched_out(struct perf_counter
*counter
,
149 struct perf_cpu_context
*cpuctx
,
150 struct perf_counter_context
*ctx
)
152 if (counter
->state
!= PERF_COUNTER_STATE_ACTIVE
)
155 counter
->state
= PERF_COUNTER_STATE_INACTIVE
;
156 counter
->tstamp_stopped
= ctx
->time
;
157 counter
->pmu
->disable(counter
);
160 if (!is_software_counter(counter
))
161 cpuctx
->active_oncpu
--;
163 if (counter
->hw_event
.exclusive
|| !cpuctx
->active_oncpu
)
164 cpuctx
->exclusive
= 0;
168 group_sched_out(struct perf_counter
*group_counter
,
169 struct perf_cpu_context
*cpuctx
,
170 struct perf_counter_context
*ctx
)
172 struct perf_counter
*counter
;
174 if (group_counter
->state
!= PERF_COUNTER_STATE_ACTIVE
)
177 counter_sched_out(group_counter
, cpuctx
, ctx
);
180 * Schedule out siblings (if any):
182 list_for_each_entry(counter
, &group_counter
->sibling_list
, list_entry
)
183 counter_sched_out(counter
, cpuctx
, ctx
);
185 if (group_counter
->hw_event
.exclusive
)
186 cpuctx
->exclusive
= 0;
190 * Cross CPU call to remove a performance counter
192 * We disable the counter on the hardware level first. After that we
193 * remove it from the context list.
195 static void __perf_counter_remove_from_context(void *info
)
197 struct perf_cpu_context
*cpuctx
= &__get_cpu_var(perf_cpu_context
);
198 struct perf_counter
*counter
= info
;
199 struct perf_counter_context
*ctx
= counter
->ctx
;
203 * If this is a task context, we need to check whether it is
204 * the current task context of this cpu. If not it has been
205 * scheduled out before the smp call arrived.
207 if (ctx
->task
&& cpuctx
->task_ctx
!= ctx
)
210 spin_lock_irqsave(&ctx
->lock
, flags
);
212 counter_sched_out(counter
, cpuctx
, ctx
);
214 counter
->task
= NULL
;
217 * Protect the list operation against NMI by disabling the
218 * counters on a global level. NOP for non NMI based counters.
221 list_del_counter(counter
, ctx
);
226 * Allow more per task counters with respect to the
229 cpuctx
->max_pertask
=
230 min(perf_max_counters
- ctx
->nr_counters
,
231 perf_max_counters
- perf_reserved_percpu
);
234 spin_unlock_irqrestore(&ctx
->lock
, flags
);
239 * Remove the counter from a task's (or a CPU's) list of counters.
241 * Must be called with counter->mutex and ctx->mutex held.
243 * CPU counters are removed with a smp call. For task counters we only
244 * call when the task is on a CPU.
246 static void perf_counter_remove_from_context(struct perf_counter
*counter
)
248 struct perf_counter_context
*ctx
= counter
->ctx
;
249 struct task_struct
*task
= ctx
->task
;
253 * Per cpu counters are removed via an smp call and
254 * the removal is always sucessful.
256 smp_call_function_single(counter
->cpu
,
257 __perf_counter_remove_from_context
,
263 task_oncpu_function_call(task
, __perf_counter_remove_from_context
,
266 spin_lock_irq(&ctx
->lock
);
268 * If the context is active we need to retry the smp call.
270 if (ctx
->nr_active
&& !list_empty(&counter
->list_entry
)) {
271 spin_unlock_irq(&ctx
->lock
);
276 * The lock prevents that this context is scheduled in so we
277 * can remove the counter safely, if the call above did not
280 if (!list_empty(&counter
->list_entry
)) {
281 list_del_counter(counter
, ctx
);
282 counter
->task
= NULL
;
284 spin_unlock_irq(&ctx
->lock
);
287 static inline u64
perf_clock(void)
289 return cpu_clock(smp_processor_id());
293 * Update the record of the current time in a context.
295 static void update_context_time(struct perf_counter_context
*ctx
)
297 u64 now
= perf_clock();
299 ctx
->time
+= now
- ctx
->timestamp
;
300 ctx
->timestamp
= now
;
304 * Update the total_time_enabled and total_time_running fields for a counter.
306 static void update_counter_times(struct perf_counter
*counter
)
308 struct perf_counter_context
*ctx
= counter
->ctx
;
311 if (counter
->state
< PERF_COUNTER_STATE_INACTIVE
)
314 counter
->total_time_enabled
= ctx
->time
- counter
->tstamp_enabled
;
316 if (counter
->state
== PERF_COUNTER_STATE_INACTIVE
)
317 run_end
= counter
->tstamp_stopped
;
321 counter
->total_time_running
= run_end
- counter
->tstamp_running
;
325 * Update total_time_enabled and total_time_running for all counters in a group.
327 static void update_group_times(struct perf_counter
*leader
)
329 struct perf_counter
*counter
;
331 update_counter_times(leader
);
332 list_for_each_entry(counter
, &leader
->sibling_list
, list_entry
)
333 update_counter_times(counter
);
337 * Cross CPU call to disable a performance counter
339 static void __perf_counter_disable(void *info
)
341 struct perf_counter
*counter
= info
;
342 struct perf_cpu_context
*cpuctx
= &__get_cpu_var(perf_cpu_context
);
343 struct perf_counter_context
*ctx
= counter
->ctx
;
347 * If this is a per-task counter, need to check whether this
348 * counter's task is the current task on this cpu.
350 if (ctx
->task
&& cpuctx
->task_ctx
!= ctx
)
353 spin_lock_irqsave(&ctx
->lock
, flags
);
356 * If the counter is on, turn it off.
357 * If it is in error state, leave it in error state.
359 if (counter
->state
>= PERF_COUNTER_STATE_INACTIVE
) {
360 update_context_time(ctx
);
361 update_counter_times(counter
);
362 if (counter
== counter
->group_leader
)
363 group_sched_out(counter
, cpuctx
, ctx
);
365 counter_sched_out(counter
, cpuctx
, ctx
);
366 counter
->state
= PERF_COUNTER_STATE_OFF
;
369 spin_unlock_irqrestore(&ctx
->lock
, flags
);
375 static void perf_counter_disable(struct perf_counter
*counter
)
377 struct perf_counter_context
*ctx
= counter
->ctx
;
378 struct task_struct
*task
= ctx
->task
;
382 * Disable the counter on the cpu that it's on
384 smp_call_function_single(counter
->cpu
, __perf_counter_disable
,
390 task_oncpu_function_call(task
, __perf_counter_disable
, counter
);
392 spin_lock_irq(&ctx
->lock
);
394 * If the counter is still active, we need to retry the cross-call.
396 if (counter
->state
== PERF_COUNTER_STATE_ACTIVE
) {
397 spin_unlock_irq(&ctx
->lock
);
402 * Since we have the lock this context can't be scheduled
403 * in, so we can change the state safely.
405 if (counter
->state
== PERF_COUNTER_STATE_INACTIVE
) {
406 update_counter_times(counter
);
407 counter
->state
= PERF_COUNTER_STATE_OFF
;
410 spin_unlock_irq(&ctx
->lock
);
414 counter_sched_in(struct perf_counter
*counter
,
415 struct perf_cpu_context
*cpuctx
,
416 struct perf_counter_context
*ctx
,
419 if (counter
->state
<= PERF_COUNTER_STATE_OFF
)
422 counter
->state
= PERF_COUNTER_STATE_ACTIVE
;
423 counter
->oncpu
= cpu
; /* TODO: put 'cpu' into cpuctx->cpu */
425 * The new state must be visible before we turn it on in the hardware:
429 if (counter
->pmu
->enable(counter
)) {
430 counter
->state
= PERF_COUNTER_STATE_INACTIVE
;
435 counter
->tstamp_running
+= ctx
->time
- counter
->tstamp_stopped
;
437 if (!is_software_counter(counter
))
438 cpuctx
->active_oncpu
++;
441 if (counter
->hw_event
.exclusive
)
442 cpuctx
->exclusive
= 1;
448 group_sched_in(struct perf_counter
*group_counter
,
449 struct perf_cpu_context
*cpuctx
,
450 struct perf_counter_context
*ctx
,
453 struct perf_counter
*counter
, *partial_group
;
456 if (group_counter
->state
== PERF_COUNTER_STATE_OFF
)
459 ret
= hw_perf_group_sched_in(group_counter
, cpuctx
, ctx
, cpu
);
461 return ret
< 0 ? ret
: 0;
463 group_counter
->prev_state
= group_counter
->state
;
464 if (counter_sched_in(group_counter
, cpuctx
, ctx
, cpu
))
468 * Schedule in siblings as one group (if any):
470 list_for_each_entry(counter
, &group_counter
->sibling_list
, list_entry
) {
471 counter
->prev_state
= counter
->state
;
472 if (counter_sched_in(counter
, cpuctx
, ctx
, cpu
)) {
473 partial_group
= counter
;
482 * Groups can be scheduled in as one unit only, so undo any
483 * partial group before returning:
485 list_for_each_entry(counter
, &group_counter
->sibling_list
, list_entry
) {
486 if (counter
== partial_group
)
488 counter_sched_out(counter
, cpuctx
, ctx
);
490 counter_sched_out(group_counter
, cpuctx
, ctx
);
496 * Return 1 for a group consisting entirely of software counters,
497 * 0 if the group contains any hardware counters.
499 static int is_software_only_group(struct perf_counter
*leader
)
501 struct perf_counter
*counter
;
503 if (!is_software_counter(leader
))
506 list_for_each_entry(counter
, &leader
->sibling_list
, list_entry
)
507 if (!is_software_counter(counter
))
514 * Work out whether we can put this counter group on the CPU now.
516 static int group_can_go_on(struct perf_counter
*counter
,
517 struct perf_cpu_context
*cpuctx
,
521 * Groups consisting entirely of software counters can always go on.
523 if (is_software_only_group(counter
))
526 * If an exclusive group is already on, no other hardware
527 * counters can go on.
529 if (cpuctx
->exclusive
)
532 * If this group is exclusive and there are already
533 * counters on the CPU, it can't go on.
535 if (counter
->hw_event
.exclusive
&& cpuctx
->active_oncpu
)
538 * Otherwise, try to add it if all previous groups were able
544 static void add_counter_to_ctx(struct perf_counter
*counter
,
545 struct perf_counter_context
*ctx
)
547 list_add_counter(counter
, ctx
);
548 counter
->prev_state
= PERF_COUNTER_STATE_OFF
;
549 counter
->tstamp_enabled
= ctx
->time
;
550 counter
->tstamp_running
= ctx
->time
;
551 counter
->tstamp_stopped
= ctx
->time
;
555 * Cross CPU call to install and enable a performance counter
557 static void __perf_install_in_context(void *info
)
559 struct perf_cpu_context
*cpuctx
= &__get_cpu_var(perf_cpu_context
);
560 struct perf_counter
*counter
= info
;
561 struct perf_counter_context
*ctx
= counter
->ctx
;
562 struct perf_counter
*leader
= counter
->group_leader
;
563 int cpu
= smp_processor_id();
568 * If this is a task context, we need to check whether it is
569 * the current task context of this cpu. If not it has been
570 * scheduled out before the smp call arrived.
572 if (ctx
->task
&& cpuctx
->task_ctx
!= ctx
)
575 spin_lock_irqsave(&ctx
->lock
, flags
);
576 update_context_time(ctx
);
579 * Protect the list operation against NMI by disabling the
580 * counters on a global level. NOP for non NMI based counters.
584 add_counter_to_ctx(counter
, ctx
);
587 * Don't put the counter on if it is disabled or if
588 * it is in a group and the group isn't on.
590 if (counter
->state
!= PERF_COUNTER_STATE_INACTIVE
||
591 (leader
!= counter
&& leader
->state
!= PERF_COUNTER_STATE_ACTIVE
))
595 * An exclusive counter can't go on if there are already active
596 * hardware counters, and no hardware counter can go on if there
597 * is already an exclusive counter on.
599 if (!group_can_go_on(counter
, cpuctx
, 1))
602 err
= counter_sched_in(counter
, cpuctx
, ctx
, cpu
);
606 * This counter couldn't go on. If it is in a group
607 * then we have to pull the whole group off.
608 * If the counter group is pinned then put it in error state.
610 if (leader
!= counter
)
611 group_sched_out(leader
, cpuctx
, ctx
);
612 if (leader
->hw_event
.pinned
) {
613 update_group_times(leader
);
614 leader
->state
= PERF_COUNTER_STATE_ERROR
;
618 if (!err
&& !ctx
->task
&& cpuctx
->max_pertask
)
619 cpuctx
->max_pertask
--;
624 spin_unlock_irqrestore(&ctx
->lock
, flags
);
628 * Attach a performance counter to a context
630 * First we add the counter to the list with the hardware enable bit
631 * in counter->hw_config cleared.
633 * If the counter is attached to a task which is on a CPU we use a smp
634 * call to enable it in the task context. The task might have been
635 * scheduled away, but we check this in the smp call again.
637 * Must be called with ctx->mutex held.
640 perf_install_in_context(struct perf_counter_context
*ctx
,
641 struct perf_counter
*counter
,
644 struct task_struct
*task
= ctx
->task
;
648 * Per cpu counters are installed via an smp call and
649 * the install is always sucessful.
651 smp_call_function_single(cpu
, __perf_install_in_context
,
656 counter
->task
= task
;
658 task_oncpu_function_call(task
, __perf_install_in_context
,
661 spin_lock_irq(&ctx
->lock
);
663 * we need to retry the smp call.
665 if (ctx
->is_active
&& list_empty(&counter
->list_entry
)) {
666 spin_unlock_irq(&ctx
->lock
);
671 * The lock prevents that this context is scheduled in so we
672 * can add the counter safely, if it the call above did not
675 if (list_empty(&counter
->list_entry
))
676 add_counter_to_ctx(counter
, ctx
);
677 spin_unlock_irq(&ctx
->lock
);
681 * Cross CPU call to enable a performance counter
683 static void __perf_counter_enable(void *info
)
685 struct perf_counter
*counter
= info
;
686 struct perf_cpu_context
*cpuctx
= &__get_cpu_var(perf_cpu_context
);
687 struct perf_counter_context
*ctx
= counter
->ctx
;
688 struct perf_counter
*leader
= counter
->group_leader
;
693 * If this is a per-task counter, need to check whether this
694 * counter's task is the current task on this cpu.
696 if (ctx
->task
&& cpuctx
->task_ctx
!= ctx
)
699 spin_lock_irqsave(&ctx
->lock
, flags
);
700 update_context_time(ctx
);
702 counter
->prev_state
= counter
->state
;
703 if (counter
->state
>= PERF_COUNTER_STATE_INACTIVE
)
705 counter
->state
= PERF_COUNTER_STATE_INACTIVE
;
706 counter
->tstamp_enabled
= ctx
->time
- counter
->total_time_enabled
;
709 * If the counter is in a group and isn't the group leader,
710 * then don't put it on unless the group is on.
712 if (leader
!= counter
&& leader
->state
!= PERF_COUNTER_STATE_ACTIVE
)
715 if (!group_can_go_on(counter
, cpuctx
, 1)) {
719 if (counter
== leader
)
720 err
= group_sched_in(counter
, cpuctx
, ctx
,
723 err
= counter_sched_in(counter
, cpuctx
, ctx
,
730 * If this counter can't go on and it's part of a
731 * group, then the whole group has to come off.
733 if (leader
!= counter
)
734 group_sched_out(leader
, cpuctx
, ctx
);
735 if (leader
->hw_event
.pinned
) {
736 update_group_times(leader
);
737 leader
->state
= PERF_COUNTER_STATE_ERROR
;
742 spin_unlock_irqrestore(&ctx
->lock
, flags
);
748 static void perf_counter_enable(struct perf_counter
*counter
)
750 struct perf_counter_context
*ctx
= counter
->ctx
;
751 struct task_struct
*task
= ctx
->task
;
755 * Enable the counter on the cpu that it's on
757 smp_call_function_single(counter
->cpu
, __perf_counter_enable
,
762 spin_lock_irq(&ctx
->lock
);
763 if (counter
->state
>= PERF_COUNTER_STATE_INACTIVE
)
767 * If the counter is in error state, clear that first.
768 * That way, if we see the counter in error state below, we
769 * know that it has gone back into error state, as distinct
770 * from the task having been scheduled away before the
771 * cross-call arrived.
773 if (counter
->state
== PERF_COUNTER_STATE_ERROR
)
774 counter
->state
= PERF_COUNTER_STATE_OFF
;
777 spin_unlock_irq(&ctx
->lock
);
778 task_oncpu_function_call(task
, __perf_counter_enable
, counter
);
780 spin_lock_irq(&ctx
->lock
);
783 * If the context is active and the counter is still off,
784 * we need to retry the cross-call.
786 if (ctx
->is_active
&& counter
->state
== PERF_COUNTER_STATE_OFF
)
790 * Since we have the lock this context can't be scheduled
791 * in, so we can change the state safely.
793 if (counter
->state
== PERF_COUNTER_STATE_OFF
) {
794 counter
->state
= PERF_COUNTER_STATE_INACTIVE
;
795 counter
->tstamp_enabled
=
796 ctx
->time
- counter
->total_time_enabled
;
799 spin_unlock_irq(&ctx
->lock
);
802 static int perf_counter_refresh(struct perf_counter
*counter
, int refresh
)
805 * not supported on inherited counters
807 if (counter
->hw_event
.inherit
)
810 atomic_add(refresh
, &counter
->event_limit
);
811 perf_counter_enable(counter
);
816 void __perf_counter_sched_out(struct perf_counter_context
*ctx
,
817 struct perf_cpu_context
*cpuctx
)
819 struct perf_counter
*counter
;
821 spin_lock(&ctx
->lock
);
823 if (likely(!ctx
->nr_counters
))
825 update_context_time(ctx
);
828 if (ctx
->nr_active
) {
829 list_for_each_entry(counter
, &ctx
->counter_list
, list_entry
)
830 group_sched_out(counter
, cpuctx
, ctx
);
834 spin_unlock(&ctx
->lock
);
838 * Called from scheduler to remove the counters of the current task,
839 * with interrupts disabled.
841 * We stop each counter and update the counter value in counter->count.
843 * This does not protect us against NMI, but disable()
844 * sets the disabled bit in the control field of counter _before_
845 * accessing the counter control register. If a NMI hits, then it will
846 * not restart the counter.
848 void perf_counter_task_sched_out(struct task_struct
*task
, int cpu
)
850 struct perf_cpu_context
*cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
851 struct perf_counter_context
*ctx
= &task
->perf_counter_ctx
;
852 struct pt_regs
*regs
;
854 if (likely(!cpuctx
->task_ctx
))
857 update_context_time(ctx
);
859 regs
= task_pt_regs(task
);
860 perf_swcounter_event(PERF_COUNT_CONTEXT_SWITCHES
, 1, 1, regs
, 0);
861 __perf_counter_sched_out(ctx
, cpuctx
);
863 cpuctx
->task_ctx
= NULL
;
866 static void __perf_counter_task_sched_out(struct perf_counter_context
*ctx
)
868 struct perf_cpu_context
*cpuctx
= &__get_cpu_var(perf_cpu_context
);
870 __perf_counter_sched_out(ctx
, cpuctx
);
871 cpuctx
->task_ctx
= NULL
;
874 static void perf_counter_cpu_sched_out(struct perf_cpu_context
*cpuctx
)
876 __perf_counter_sched_out(&cpuctx
->ctx
, cpuctx
);
880 __perf_counter_sched_in(struct perf_counter_context
*ctx
,
881 struct perf_cpu_context
*cpuctx
, int cpu
)
883 struct perf_counter
*counter
;
886 spin_lock(&ctx
->lock
);
888 if (likely(!ctx
->nr_counters
))
891 ctx
->timestamp
= perf_clock();
896 * First go through the list and put on any pinned groups
897 * in order to give them the best chance of going on.
899 list_for_each_entry(counter
, &ctx
->counter_list
, list_entry
) {
900 if (counter
->state
<= PERF_COUNTER_STATE_OFF
||
901 !counter
->hw_event
.pinned
)
903 if (counter
->cpu
!= -1 && counter
->cpu
!= cpu
)
906 if (group_can_go_on(counter
, cpuctx
, 1))
907 group_sched_in(counter
, cpuctx
, ctx
, cpu
);
910 * If this pinned group hasn't been scheduled,
911 * put it in error state.
913 if (counter
->state
== PERF_COUNTER_STATE_INACTIVE
) {
914 update_group_times(counter
);
915 counter
->state
= PERF_COUNTER_STATE_ERROR
;
919 list_for_each_entry(counter
, &ctx
->counter_list
, list_entry
) {
921 * Ignore counters in OFF or ERROR state, and
922 * ignore pinned counters since we did them already.
924 if (counter
->state
<= PERF_COUNTER_STATE_OFF
||
925 counter
->hw_event
.pinned
)
929 * Listen to the 'cpu' scheduling filter constraint
932 if (counter
->cpu
!= -1 && counter
->cpu
!= cpu
)
935 if (group_can_go_on(counter
, cpuctx
, can_add_hw
)) {
936 if (group_sched_in(counter
, cpuctx
, ctx
, cpu
))
942 spin_unlock(&ctx
->lock
);
946 * Called from scheduler to add the counters of the current task
947 * with interrupts disabled.
949 * We restore the counter value and then enable it.
951 * This does not protect us against NMI, but enable()
952 * sets the enabled bit in the control field of counter _before_
953 * accessing the counter control register. If a NMI hits, then it will
954 * keep the counter running.
956 void perf_counter_task_sched_in(struct task_struct
*task
, int cpu
)
958 struct perf_cpu_context
*cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
959 struct perf_counter_context
*ctx
= &task
->perf_counter_ctx
;
961 __perf_counter_sched_in(ctx
, cpuctx
, cpu
);
962 cpuctx
->task_ctx
= ctx
;
965 static void perf_counter_cpu_sched_in(struct perf_cpu_context
*cpuctx
, int cpu
)
967 struct perf_counter_context
*ctx
= &cpuctx
->ctx
;
969 __perf_counter_sched_in(ctx
, cpuctx
, cpu
);
972 int perf_counter_task_disable(void)
974 struct task_struct
*curr
= current
;
975 struct perf_counter_context
*ctx
= &curr
->perf_counter_ctx
;
976 struct perf_counter
*counter
;
979 if (likely(!ctx
->nr_counters
))
982 local_irq_save(flags
);
984 __perf_counter_task_sched_out(ctx
);
986 spin_lock(&ctx
->lock
);
989 * Disable all the counters:
993 list_for_each_entry(counter
, &ctx
->counter_list
, list_entry
) {
994 if (counter
->state
!= PERF_COUNTER_STATE_ERROR
) {
995 update_group_times(counter
);
996 counter
->state
= PERF_COUNTER_STATE_OFF
;
1002 spin_unlock_irqrestore(&ctx
->lock
, flags
);
1007 int perf_counter_task_enable(void)
1009 struct task_struct
*curr
= current
;
1010 struct perf_counter_context
*ctx
= &curr
->perf_counter_ctx
;
1011 struct perf_counter
*counter
;
1012 unsigned long flags
;
1015 if (likely(!ctx
->nr_counters
))
1018 local_irq_save(flags
);
1019 cpu
= smp_processor_id();
1021 __perf_counter_task_sched_out(ctx
);
1023 spin_lock(&ctx
->lock
);
1026 * Disable all the counters:
1030 list_for_each_entry(counter
, &ctx
->counter_list
, list_entry
) {
1031 if (counter
->state
> PERF_COUNTER_STATE_OFF
)
1033 counter
->state
= PERF_COUNTER_STATE_INACTIVE
;
1034 counter
->tstamp_enabled
=
1035 ctx
->time
- counter
->total_time_enabled
;
1036 counter
->hw_event
.disabled
= 0;
1040 spin_unlock(&ctx
->lock
);
1042 perf_counter_task_sched_in(curr
, cpu
);
1044 local_irq_restore(flags
);
1049 static void perf_log_period(struct perf_counter
*counter
, u64 period
);
1051 static void perf_adjust_freq(struct perf_counter_context
*ctx
)
1053 struct perf_counter
*counter
;
1058 spin_lock(&ctx
->lock
);
1059 list_for_each_entry(counter
, &ctx
->counter_list
, list_entry
) {
1060 if (counter
->state
!= PERF_COUNTER_STATE_ACTIVE
)
1063 if (!counter
->hw_event
.freq
|| !counter
->hw_event
.irq_freq
)
1066 events
= HZ
* counter
->hw
.interrupts
* counter
->hw
.irq_period
;
1067 period
= div64_u64(events
, counter
->hw_event
.irq_freq
);
1069 delta
= (s64
)(1 + period
- counter
->hw
.irq_period
);
1072 irq_period
= counter
->hw
.irq_period
+ delta
;
1077 perf_log_period(counter
, irq_period
);
1079 counter
->hw
.irq_period
= irq_period
;
1080 counter
->hw
.interrupts
= 0;
1082 spin_unlock(&ctx
->lock
);
1086 * Round-robin a context's counters:
1088 static void rotate_ctx(struct perf_counter_context
*ctx
)
1090 struct perf_counter
*counter
;
1092 if (!ctx
->nr_counters
)
1095 spin_lock(&ctx
->lock
);
1097 * Rotate the first entry last (works just fine for group counters too):
1100 list_for_each_entry(counter
, &ctx
->counter_list
, list_entry
) {
1101 list_move_tail(&counter
->list_entry
, &ctx
->counter_list
);
1106 spin_unlock(&ctx
->lock
);
1109 void perf_counter_task_tick(struct task_struct
*curr
, int cpu
)
1111 struct perf_cpu_context
*cpuctx
;
1112 struct perf_counter_context
*ctx
;
1114 if (!atomic_read(&nr_counters
))
1117 cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
1118 ctx
= &curr
->perf_counter_ctx
;
1120 perf_adjust_freq(&cpuctx
->ctx
);
1121 perf_adjust_freq(ctx
);
1123 perf_counter_cpu_sched_out(cpuctx
);
1124 __perf_counter_task_sched_out(ctx
);
1126 rotate_ctx(&cpuctx
->ctx
);
1129 perf_counter_cpu_sched_in(cpuctx
, cpu
);
1130 perf_counter_task_sched_in(curr
, cpu
);
1134 * Cross CPU call to read the hardware counter
1136 static void __read(void *info
)
1138 struct perf_counter
*counter
= info
;
1139 struct perf_counter_context
*ctx
= counter
->ctx
;
1140 unsigned long flags
;
1142 local_irq_save(flags
);
1144 update_context_time(ctx
);
1145 counter
->pmu
->read(counter
);
1146 update_counter_times(counter
);
1147 local_irq_restore(flags
);
1150 static u64
perf_counter_read(struct perf_counter
*counter
)
1153 * If counter is enabled and currently active on a CPU, update the
1154 * value in the counter structure:
1156 if (counter
->state
== PERF_COUNTER_STATE_ACTIVE
) {
1157 smp_call_function_single(counter
->oncpu
,
1158 __read
, counter
, 1);
1159 } else if (counter
->state
== PERF_COUNTER_STATE_INACTIVE
) {
1160 update_counter_times(counter
);
1163 return atomic64_read(&counter
->count
);
1166 static void put_context(struct perf_counter_context
*ctx
)
1169 put_task_struct(ctx
->task
);
1172 static struct perf_counter_context
*find_get_context(pid_t pid
, int cpu
)
1174 struct perf_cpu_context
*cpuctx
;
1175 struct perf_counter_context
*ctx
;
1176 struct task_struct
*task
;
1179 * If cpu is not a wildcard then this is a percpu counter:
1182 /* Must be root to operate on a CPU counter: */
1183 if (sysctl_perf_counter_priv
&& !capable(CAP_SYS_ADMIN
))
1184 return ERR_PTR(-EACCES
);
1186 if (cpu
< 0 || cpu
> num_possible_cpus())
1187 return ERR_PTR(-EINVAL
);
1190 * We could be clever and allow to attach a counter to an
1191 * offline CPU and activate it when the CPU comes up, but
1194 if (!cpu_isset(cpu
, cpu_online_map
))
1195 return ERR_PTR(-ENODEV
);
1197 cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
1207 task
= find_task_by_vpid(pid
);
1209 get_task_struct(task
);
1213 return ERR_PTR(-ESRCH
);
1215 ctx
= &task
->perf_counter_ctx
;
1218 /* Reuse ptrace permission checks for now. */
1219 if (!ptrace_may_access(task
, PTRACE_MODE_READ
)) {
1221 return ERR_PTR(-EACCES
);
1227 static void free_counter_rcu(struct rcu_head
*head
)
1229 struct perf_counter
*counter
;
1231 counter
= container_of(head
, struct perf_counter
, rcu_head
);
1235 static void perf_pending_sync(struct perf_counter
*counter
);
1237 static void free_counter(struct perf_counter
*counter
)
1239 perf_pending_sync(counter
);
1241 atomic_dec(&nr_counters
);
1242 if (counter
->hw_event
.mmap
)
1243 atomic_dec(&nr_mmap_tracking
);
1244 if (counter
->hw_event
.munmap
)
1245 atomic_dec(&nr_munmap_tracking
);
1246 if (counter
->hw_event
.comm
)
1247 atomic_dec(&nr_comm_tracking
);
1249 if (counter
->destroy
)
1250 counter
->destroy(counter
);
1252 call_rcu(&counter
->rcu_head
, free_counter_rcu
);
1256 * Called when the last reference to the file is gone.
1258 static int perf_release(struct inode
*inode
, struct file
*file
)
1260 struct perf_counter
*counter
= file
->private_data
;
1261 struct perf_counter_context
*ctx
= counter
->ctx
;
1263 file
->private_data
= NULL
;
1265 mutex_lock(&ctx
->mutex
);
1266 mutex_lock(&counter
->mutex
);
1268 perf_counter_remove_from_context(counter
);
1270 mutex_unlock(&counter
->mutex
);
1271 mutex_unlock(&ctx
->mutex
);
1273 free_counter(counter
);
1280 * Read the performance counter - simple non blocking version for now
1283 perf_read_hw(struct perf_counter
*counter
, char __user
*buf
, size_t count
)
1289 * Return end-of-file for a read on a counter that is in
1290 * error state (i.e. because it was pinned but it couldn't be
1291 * scheduled on to the CPU at some point).
1293 if (counter
->state
== PERF_COUNTER_STATE_ERROR
)
1296 mutex_lock(&counter
->mutex
);
1297 values
[0] = perf_counter_read(counter
);
1299 if (counter
->hw_event
.read_format
& PERF_FORMAT_TOTAL_TIME_ENABLED
)
1300 values
[n
++] = counter
->total_time_enabled
+
1301 atomic64_read(&counter
->child_total_time_enabled
);
1302 if (counter
->hw_event
.read_format
& PERF_FORMAT_TOTAL_TIME_RUNNING
)
1303 values
[n
++] = counter
->total_time_running
+
1304 atomic64_read(&counter
->child_total_time_running
);
1305 mutex_unlock(&counter
->mutex
);
1307 if (count
< n
* sizeof(u64
))
1309 count
= n
* sizeof(u64
);
1311 if (copy_to_user(buf
, values
, count
))
1318 perf_read(struct file
*file
, char __user
*buf
, size_t count
, loff_t
*ppos
)
1320 struct perf_counter
*counter
= file
->private_data
;
1322 return perf_read_hw(counter
, buf
, count
);
1325 static unsigned int perf_poll(struct file
*file
, poll_table
*wait
)
1327 struct perf_counter
*counter
= file
->private_data
;
1328 struct perf_mmap_data
*data
;
1329 unsigned int events
= POLL_HUP
;
1332 data
= rcu_dereference(counter
->data
);
1334 events
= atomic_xchg(&data
->poll
, 0);
1337 poll_wait(file
, &counter
->waitq
, wait
);
1342 static void perf_counter_reset(struct perf_counter
*counter
)
1344 (void)perf_counter_read(counter
);
1345 atomic64_set(&counter
->count
, 0);
1346 perf_counter_update_userpage(counter
);
1349 static void perf_counter_for_each_sibling(struct perf_counter
*counter
,
1350 void (*func
)(struct perf_counter
*))
1352 struct perf_counter_context
*ctx
= counter
->ctx
;
1353 struct perf_counter
*sibling
;
1355 spin_lock_irq(&ctx
->lock
);
1356 counter
= counter
->group_leader
;
1359 list_for_each_entry(sibling
, &counter
->sibling_list
, list_entry
)
1361 spin_unlock_irq(&ctx
->lock
);
1364 static void perf_counter_for_each_child(struct perf_counter
*counter
,
1365 void (*func
)(struct perf_counter
*))
1367 struct perf_counter
*child
;
1369 mutex_lock(&counter
->mutex
);
1371 list_for_each_entry(child
, &counter
->child_list
, child_list
)
1373 mutex_unlock(&counter
->mutex
);
1376 static void perf_counter_for_each(struct perf_counter
*counter
,
1377 void (*func
)(struct perf_counter
*))
1379 struct perf_counter
*child
;
1381 mutex_lock(&counter
->mutex
);
1382 perf_counter_for_each_sibling(counter
, func
);
1383 list_for_each_entry(child
, &counter
->child_list
, child_list
)
1384 perf_counter_for_each_sibling(child
, func
);
1385 mutex_unlock(&counter
->mutex
);
1388 static long perf_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
1390 struct perf_counter
*counter
= file
->private_data
;
1391 void (*func
)(struct perf_counter
*);
1395 case PERF_COUNTER_IOC_ENABLE
:
1396 func
= perf_counter_enable
;
1398 case PERF_COUNTER_IOC_DISABLE
:
1399 func
= perf_counter_disable
;
1401 case PERF_COUNTER_IOC_RESET
:
1402 func
= perf_counter_reset
;
1405 case PERF_COUNTER_IOC_REFRESH
:
1406 return perf_counter_refresh(counter
, arg
);
1411 if (flags
& PERF_IOC_FLAG_GROUP
)
1412 perf_counter_for_each(counter
, func
);
1414 perf_counter_for_each_child(counter
, func
);
1420 * Callers need to ensure there can be no nesting of this function, otherwise
1421 * the seqlock logic goes bad. We can not serialize this because the arch
1422 * code calls this from NMI context.
1424 void perf_counter_update_userpage(struct perf_counter
*counter
)
1426 struct perf_mmap_data
*data
;
1427 struct perf_counter_mmap_page
*userpg
;
1430 data
= rcu_dereference(counter
->data
);
1434 userpg
= data
->user_page
;
1437 * Disable preemption so as to not let the corresponding user-space
1438 * spin too long if we get preempted.
1443 userpg
->index
= counter
->hw
.idx
;
1444 userpg
->offset
= atomic64_read(&counter
->count
);
1445 if (counter
->state
== PERF_COUNTER_STATE_ACTIVE
)
1446 userpg
->offset
-= atomic64_read(&counter
->hw
.prev_count
);
1455 static int perf_mmap_fault(struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
1457 struct perf_counter
*counter
= vma
->vm_file
->private_data
;
1458 struct perf_mmap_data
*data
;
1459 int ret
= VM_FAULT_SIGBUS
;
1462 data
= rcu_dereference(counter
->data
);
1466 if (vmf
->pgoff
== 0) {
1467 vmf
->page
= virt_to_page(data
->user_page
);
1469 int nr
= vmf
->pgoff
- 1;
1471 if ((unsigned)nr
> data
->nr_pages
)
1474 vmf
->page
= virt_to_page(data
->data_pages
[nr
]);
1476 get_page(vmf
->page
);
1484 static int perf_mmap_data_alloc(struct perf_counter
*counter
, int nr_pages
)
1486 struct perf_mmap_data
*data
;
1490 WARN_ON(atomic_read(&counter
->mmap_count
));
1492 size
= sizeof(struct perf_mmap_data
);
1493 size
+= nr_pages
* sizeof(void *);
1495 data
= kzalloc(size
, GFP_KERNEL
);
1499 data
->user_page
= (void *)get_zeroed_page(GFP_KERNEL
);
1500 if (!data
->user_page
)
1501 goto fail_user_page
;
1503 for (i
= 0; i
< nr_pages
; i
++) {
1504 data
->data_pages
[i
] = (void *)get_zeroed_page(GFP_KERNEL
);
1505 if (!data
->data_pages
[i
])
1506 goto fail_data_pages
;
1509 data
->nr_pages
= nr_pages
;
1510 atomic_set(&data
->lock
, -1);
1512 rcu_assign_pointer(counter
->data
, data
);
1517 for (i
--; i
>= 0; i
--)
1518 free_page((unsigned long)data
->data_pages
[i
]);
1520 free_page((unsigned long)data
->user_page
);
1529 static void __perf_mmap_data_free(struct rcu_head
*rcu_head
)
1531 struct perf_mmap_data
*data
= container_of(rcu_head
,
1532 struct perf_mmap_data
, rcu_head
);
1535 free_page((unsigned long)data
->user_page
);
1536 for (i
= 0; i
< data
->nr_pages
; i
++)
1537 free_page((unsigned long)data
->data_pages
[i
]);
1541 static void perf_mmap_data_free(struct perf_counter
*counter
)
1543 struct perf_mmap_data
*data
= counter
->data
;
1545 WARN_ON(atomic_read(&counter
->mmap_count
));
1547 rcu_assign_pointer(counter
->data
, NULL
);
1548 call_rcu(&data
->rcu_head
, __perf_mmap_data_free
);
1551 static void perf_mmap_open(struct vm_area_struct
*vma
)
1553 struct perf_counter
*counter
= vma
->vm_file
->private_data
;
1555 atomic_inc(&counter
->mmap_count
);
1558 static void perf_mmap_close(struct vm_area_struct
*vma
)
1560 struct perf_counter
*counter
= vma
->vm_file
->private_data
;
1562 if (atomic_dec_and_mutex_lock(&counter
->mmap_count
,
1563 &counter
->mmap_mutex
)) {
1564 struct user_struct
*user
= current_user();
1566 atomic_long_sub(counter
->data
->nr_pages
+ 1, &user
->locked_vm
);
1567 vma
->vm_mm
->locked_vm
-= counter
->data
->nr_locked
;
1568 perf_mmap_data_free(counter
);
1569 mutex_unlock(&counter
->mmap_mutex
);
1573 static struct vm_operations_struct perf_mmap_vmops
= {
1574 .open
= perf_mmap_open
,
1575 .close
= perf_mmap_close
,
1576 .fault
= perf_mmap_fault
,
1579 static int perf_mmap(struct file
*file
, struct vm_area_struct
*vma
)
1581 struct perf_counter
*counter
= file
->private_data
;
1582 struct user_struct
*user
= current_user();
1583 unsigned long vma_size
;
1584 unsigned long nr_pages
;
1585 unsigned long user_locked
, user_lock_limit
;
1586 unsigned long locked
, lock_limit
;
1587 long user_extra
, extra
;
1590 if (!(vma
->vm_flags
& VM_SHARED
) || (vma
->vm_flags
& VM_WRITE
))
1593 vma_size
= vma
->vm_end
- vma
->vm_start
;
1594 nr_pages
= (vma_size
/ PAGE_SIZE
) - 1;
1597 * If we have data pages ensure they're a power-of-two number, so we
1598 * can do bitmasks instead of modulo.
1600 if (nr_pages
!= 0 && !is_power_of_2(nr_pages
))
1603 if (vma_size
!= PAGE_SIZE
* (1 + nr_pages
))
1606 if (vma
->vm_pgoff
!= 0)
1609 mutex_lock(&counter
->mmap_mutex
);
1610 if (atomic_inc_not_zero(&counter
->mmap_count
)) {
1611 if (nr_pages
!= counter
->data
->nr_pages
)
1616 user_extra
= nr_pages
+ 1;
1617 user_lock_limit
= sysctl_perf_counter_mlock
>> (PAGE_SHIFT
- 10);
1618 user_locked
= atomic_long_read(&user
->locked_vm
) + user_extra
;
1621 if (user_locked
> user_lock_limit
)
1622 extra
= user_locked
- user_lock_limit
;
1624 lock_limit
= current
->signal
->rlim
[RLIMIT_MEMLOCK
].rlim_cur
;
1625 lock_limit
>>= PAGE_SHIFT
;
1626 locked
= vma
->vm_mm
->locked_vm
+ extra
;
1628 if ((locked
> lock_limit
) && !capable(CAP_IPC_LOCK
)) {
1633 WARN_ON(counter
->data
);
1634 ret
= perf_mmap_data_alloc(counter
, nr_pages
);
1638 atomic_set(&counter
->mmap_count
, 1);
1639 atomic_long_add(user_extra
, &user
->locked_vm
);
1640 vma
->vm_mm
->locked_vm
+= extra
;
1641 counter
->data
->nr_locked
= extra
;
1643 mutex_unlock(&counter
->mmap_mutex
);
1645 vma
->vm_flags
&= ~VM_MAYWRITE
;
1646 vma
->vm_flags
|= VM_RESERVED
;
1647 vma
->vm_ops
= &perf_mmap_vmops
;
1652 static int perf_fasync(int fd
, struct file
*filp
, int on
)
1654 struct perf_counter
*counter
= filp
->private_data
;
1655 struct inode
*inode
= filp
->f_path
.dentry
->d_inode
;
1658 mutex_lock(&inode
->i_mutex
);
1659 retval
= fasync_helper(fd
, filp
, on
, &counter
->fasync
);
1660 mutex_unlock(&inode
->i_mutex
);
1668 static const struct file_operations perf_fops
= {
1669 .release
= perf_release
,
1672 .unlocked_ioctl
= perf_ioctl
,
1673 .compat_ioctl
= perf_ioctl
,
1675 .fasync
= perf_fasync
,
1679 * Perf counter wakeup
1681 * If there's data, ensure we set the poll() state and publish everything
1682 * to user-space before waking everybody up.
1685 void perf_counter_wakeup(struct perf_counter
*counter
)
1687 wake_up_all(&counter
->waitq
);
1689 if (counter
->pending_kill
) {
1690 kill_fasync(&counter
->fasync
, SIGIO
, counter
->pending_kill
);
1691 counter
->pending_kill
= 0;
1698 * Handle the case where we need to wakeup up from NMI (or rq->lock) context.
1700 * The NMI bit means we cannot possibly take locks. Therefore, maintain a
1701 * single linked list and use cmpxchg() to add entries lockless.
1704 static void perf_pending_counter(struct perf_pending_entry
*entry
)
1706 struct perf_counter
*counter
= container_of(entry
,
1707 struct perf_counter
, pending
);
1709 if (counter
->pending_disable
) {
1710 counter
->pending_disable
= 0;
1711 perf_counter_disable(counter
);
1714 if (counter
->pending_wakeup
) {
1715 counter
->pending_wakeup
= 0;
1716 perf_counter_wakeup(counter
);
1720 #define PENDING_TAIL ((struct perf_pending_entry *)-1UL)
1722 static DEFINE_PER_CPU(struct perf_pending_entry
*, perf_pending_head
) = {
1726 static void perf_pending_queue(struct perf_pending_entry
*entry
,
1727 void (*func
)(struct perf_pending_entry
*))
1729 struct perf_pending_entry
**head
;
1731 if (cmpxchg(&entry
->next
, NULL
, PENDING_TAIL
) != NULL
)
1736 head
= &get_cpu_var(perf_pending_head
);
1739 entry
->next
= *head
;
1740 } while (cmpxchg(head
, entry
->next
, entry
) != entry
->next
);
1742 set_perf_counter_pending();
1744 put_cpu_var(perf_pending_head
);
1747 static int __perf_pending_run(void)
1749 struct perf_pending_entry
*list
;
1752 list
= xchg(&__get_cpu_var(perf_pending_head
), PENDING_TAIL
);
1753 while (list
!= PENDING_TAIL
) {
1754 void (*func
)(struct perf_pending_entry
*);
1755 struct perf_pending_entry
*entry
= list
;
1762 * Ensure we observe the unqueue before we issue the wakeup,
1763 * so that we won't be waiting forever.
1764 * -- see perf_not_pending().
1775 static inline int perf_not_pending(struct perf_counter
*counter
)
1778 * If we flush on whatever cpu we run, there is a chance we don't
1782 __perf_pending_run();
1786 * Ensure we see the proper queue state before going to sleep
1787 * so that we do not miss the wakeup. -- see perf_pending_handle()
1790 return counter
->pending
.next
== NULL
;
1793 static void perf_pending_sync(struct perf_counter
*counter
)
1795 wait_event(counter
->waitq
, perf_not_pending(counter
));
1798 void perf_counter_do_pending(void)
1800 __perf_pending_run();
1804 * Callchain support -- arch specific
1807 __weak
struct perf_callchain_entry
*perf_callchain(struct pt_regs
*regs
)
1816 struct perf_output_handle
{
1817 struct perf_counter
*counter
;
1818 struct perf_mmap_data
*data
;
1819 unsigned int offset
;
1824 unsigned long flags
;
1827 static void perf_output_wakeup(struct perf_output_handle
*handle
)
1829 atomic_set(&handle
->data
->poll
, POLL_IN
);
1832 handle
->counter
->pending_wakeup
= 1;
1833 perf_pending_queue(&handle
->counter
->pending
,
1834 perf_pending_counter
);
1836 perf_counter_wakeup(handle
->counter
);
1840 * Curious locking construct.
1842 * We need to ensure a later event doesn't publish a head when a former
1843 * event isn't done writing. However since we need to deal with NMIs we
1844 * cannot fully serialize things.
1846 * What we do is serialize between CPUs so we only have to deal with NMI
1847 * nesting on a single CPU.
1849 * We only publish the head (and generate a wakeup) when the outer-most
1852 static void perf_output_lock(struct perf_output_handle
*handle
)
1854 struct perf_mmap_data
*data
= handle
->data
;
1859 local_irq_save(handle
->flags
);
1860 cpu
= smp_processor_id();
1862 if (in_nmi() && atomic_read(&data
->lock
) == cpu
)
1865 while (atomic_cmpxchg(&data
->lock
, -1, cpu
) != -1)
1871 static void perf_output_unlock(struct perf_output_handle
*handle
)
1873 struct perf_mmap_data
*data
= handle
->data
;
1876 data
->done_head
= data
->head
;
1878 if (!handle
->locked
)
1883 * The xchg implies a full barrier that ensures all writes are done
1884 * before we publish the new head, matched by a rmb() in userspace when
1885 * reading this position.
1887 while ((head
= atomic_xchg(&data
->done_head
, 0)))
1888 data
->user_page
->data_head
= head
;
1891 * NMI can happen here, which means we can miss a done_head update.
1894 cpu
= atomic_xchg(&data
->lock
, -1);
1895 WARN_ON_ONCE(cpu
!= smp_processor_id());
1898 * Therefore we have to validate we did not indeed do so.
1900 if (unlikely(atomic_read(&data
->done_head
))) {
1902 * Since we had it locked, we can lock it again.
1904 while (atomic_cmpxchg(&data
->lock
, -1, cpu
) != -1)
1910 if (atomic_xchg(&data
->wakeup
, 0))
1911 perf_output_wakeup(handle
);
1913 local_irq_restore(handle
->flags
);
1916 static int perf_output_begin(struct perf_output_handle
*handle
,
1917 struct perf_counter
*counter
, unsigned int size
,
1918 int nmi
, int overflow
)
1920 struct perf_mmap_data
*data
;
1921 unsigned int offset
, head
;
1924 * For inherited counters we send all the output towards the parent.
1926 if (counter
->parent
)
1927 counter
= counter
->parent
;
1930 data
= rcu_dereference(counter
->data
);
1934 handle
->data
= data
;
1935 handle
->counter
= counter
;
1937 handle
->overflow
= overflow
;
1939 if (!data
->nr_pages
)
1942 perf_output_lock(handle
);
1945 offset
= head
= atomic_read(&data
->head
);
1947 } while (atomic_cmpxchg(&data
->head
, offset
, head
) != offset
);
1949 handle
->offset
= offset
;
1950 handle
->head
= head
;
1952 if ((offset
>> PAGE_SHIFT
) != (head
>> PAGE_SHIFT
))
1953 atomic_set(&data
->wakeup
, 1);
1958 perf_output_wakeup(handle
);
1965 static void perf_output_copy(struct perf_output_handle
*handle
,
1966 void *buf
, unsigned int len
)
1968 unsigned int pages_mask
;
1969 unsigned int offset
;
1973 offset
= handle
->offset
;
1974 pages_mask
= handle
->data
->nr_pages
- 1;
1975 pages
= handle
->data
->data_pages
;
1978 unsigned int page_offset
;
1981 nr
= (offset
>> PAGE_SHIFT
) & pages_mask
;
1982 page_offset
= offset
& (PAGE_SIZE
- 1);
1983 size
= min_t(unsigned int, PAGE_SIZE
- page_offset
, len
);
1985 memcpy(pages
[nr
] + page_offset
, buf
, size
);
1992 handle
->offset
= offset
;
1995 * Check we didn't copy past our reservation window, taking the
1996 * possible unsigned int wrap into account.
1998 WARN_ON_ONCE(((int)(handle
->head
- handle
->offset
)) < 0);
2001 #define perf_output_put(handle, x) \
2002 perf_output_copy((handle), &(x), sizeof(x))
2004 static void perf_output_end(struct perf_output_handle
*handle
)
2006 struct perf_counter
*counter
= handle
->counter
;
2007 struct perf_mmap_data
*data
= handle
->data
;
2009 int wakeup_events
= counter
->hw_event
.wakeup_events
;
2011 if (handle
->overflow
&& wakeup_events
) {
2012 int events
= atomic_inc_return(&data
->events
);
2013 if (events
>= wakeup_events
) {
2014 atomic_sub(wakeup_events
, &data
->events
);
2015 atomic_set(&data
->wakeup
, 1);
2019 perf_output_unlock(handle
);
2023 static void perf_counter_output(struct perf_counter
*counter
,
2024 int nmi
, struct pt_regs
*regs
, u64 addr
)
2027 u64 record_type
= counter
->hw_event
.record_type
;
2028 struct perf_output_handle handle
;
2029 struct perf_event_header header
;
2038 struct perf_callchain_entry
*callchain
= NULL
;
2039 int callchain_size
= 0;
2046 header
.size
= sizeof(header
);
2048 header
.misc
= PERF_EVENT_MISC_OVERFLOW
;
2049 header
.misc
|= perf_misc_flags(regs
);
2051 if (record_type
& PERF_RECORD_IP
) {
2052 ip
= perf_instruction_pointer(regs
);
2053 header
.type
|= PERF_RECORD_IP
;
2054 header
.size
+= sizeof(ip
);
2057 if (record_type
& PERF_RECORD_TID
) {
2058 /* namespace issues */
2059 tid_entry
.pid
= current
->group_leader
->pid
;
2060 tid_entry
.tid
= current
->pid
;
2062 header
.type
|= PERF_RECORD_TID
;
2063 header
.size
+= sizeof(tid_entry
);
2066 if (record_type
& PERF_RECORD_TIME
) {
2068 * Maybe do better on x86 and provide cpu_clock_nmi()
2070 time
= sched_clock();
2072 header
.type
|= PERF_RECORD_TIME
;
2073 header
.size
+= sizeof(u64
);
2076 if (record_type
& PERF_RECORD_ADDR
) {
2077 header
.type
|= PERF_RECORD_ADDR
;
2078 header
.size
+= sizeof(u64
);
2081 if (record_type
& PERF_RECORD_CONFIG
) {
2082 header
.type
|= PERF_RECORD_CONFIG
;
2083 header
.size
+= sizeof(u64
);
2086 if (record_type
& PERF_RECORD_CPU
) {
2087 header
.type
|= PERF_RECORD_CPU
;
2088 header
.size
+= sizeof(cpu_entry
);
2090 cpu_entry
.cpu
= raw_smp_processor_id();
2093 if (record_type
& PERF_RECORD_GROUP
) {
2094 header
.type
|= PERF_RECORD_GROUP
;
2095 header
.size
+= sizeof(u64
) +
2096 counter
->nr_siblings
* sizeof(group_entry
);
2099 if (record_type
& PERF_RECORD_CALLCHAIN
) {
2100 callchain
= perf_callchain(regs
);
2103 callchain_size
= (1 + callchain
->nr
) * sizeof(u64
);
2105 header
.type
|= PERF_RECORD_CALLCHAIN
;
2106 header
.size
+= callchain_size
;
2110 ret
= perf_output_begin(&handle
, counter
, header
.size
, nmi
, 1);
2114 perf_output_put(&handle
, header
);
2116 if (record_type
& PERF_RECORD_IP
)
2117 perf_output_put(&handle
, ip
);
2119 if (record_type
& PERF_RECORD_TID
)
2120 perf_output_put(&handle
, tid_entry
);
2122 if (record_type
& PERF_RECORD_TIME
)
2123 perf_output_put(&handle
, time
);
2125 if (record_type
& PERF_RECORD_ADDR
)
2126 perf_output_put(&handle
, addr
);
2128 if (record_type
& PERF_RECORD_CONFIG
)
2129 perf_output_put(&handle
, counter
->hw_event
.config
);
2131 if (record_type
& PERF_RECORD_CPU
)
2132 perf_output_put(&handle
, cpu_entry
);
2135 * XXX PERF_RECORD_GROUP vs inherited counters seems difficult.
2137 if (record_type
& PERF_RECORD_GROUP
) {
2138 struct perf_counter
*leader
, *sub
;
2139 u64 nr
= counter
->nr_siblings
;
2141 perf_output_put(&handle
, nr
);
2143 leader
= counter
->group_leader
;
2144 list_for_each_entry(sub
, &leader
->sibling_list
, list_entry
) {
2146 sub
->pmu
->read(sub
);
2148 group_entry
.event
= sub
->hw_event
.config
;
2149 group_entry
.counter
= atomic64_read(&sub
->count
);
2151 perf_output_put(&handle
, group_entry
);
2156 perf_output_copy(&handle
, callchain
, callchain_size
);
2158 perf_output_end(&handle
);
2165 struct perf_comm_event
{
2166 struct task_struct
*task
;
2171 struct perf_event_header header
;
2178 static void perf_counter_comm_output(struct perf_counter
*counter
,
2179 struct perf_comm_event
*comm_event
)
2181 struct perf_output_handle handle
;
2182 int size
= comm_event
->event
.header
.size
;
2183 int ret
= perf_output_begin(&handle
, counter
, size
, 0, 0);
2188 perf_output_put(&handle
, comm_event
->event
);
2189 perf_output_copy(&handle
, comm_event
->comm
,
2190 comm_event
->comm_size
);
2191 perf_output_end(&handle
);
2194 static int perf_counter_comm_match(struct perf_counter
*counter
,
2195 struct perf_comm_event
*comm_event
)
2197 if (counter
->hw_event
.comm
&&
2198 comm_event
->event
.header
.type
== PERF_EVENT_COMM
)
2204 static void perf_counter_comm_ctx(struct perf_counter_context
*ctx
,
2205 struct perf_comm_event
*comm_event
)
2207 struct perf_counter
*counter
;
2209 if (system_state
!= SYSTEM_RUNNING
|| list_empty(&ctx
->event_list
))
2213 list_for_each_entry_rcu(counter
, &ctx
->event_list
, event_entry
) {
2214 if (perf_counter_comm_match(counter
, comm_event
))
2215 perf_counter_comm_output(counter
, comm_event
);
2220 static void perf_counter_comm_event(struct perf_comm_event
*comm_event
)
2222 struct perf_cpu_context
*cpuctx
;
2224 char *comm
= comm_event
->task
->comm
;
2226 size
= ALIGN(strlen(comm
)+1, sizeof(u64
));
2228 comm_event
->comm
= comm
;
2229 comm_event
->comm_size
= size
;
2231 comm_event
->event
.header
.size
= sizeof(comm_event
->event
) + size
;
2233 cpuctx
= &get_cpu_var(perf_cpu_context
);
2234 perf_counter_comm_ctx(&cpuctx
->ctx
, comm_event
);
2235 put_cpu_var(perf_cpu_context
);
2237 perf_counter_comm_ctx(¤t
->perf_counter_ctx
, comm_event
);
2240 void perf_counter_comm(struct task_struct
*task
)
2242 struct perf_comm_event comm_event
;
2244 if (!atomic_read(&nr_comm_tracking
))
2247 comm_event
= (struct perf_comm_event
){
2250 .header
= { .type
= PERF_EVENT_COMM
, },
2251 .pid
= task
->group_leader
->pid
,
2256 perf_counter_comm_event(&comm_event
);
2263 struct perf_mmap_event
{
2269 struct perf_event_header header
;
2279 static void perf_counter_mmap_output(struct perf_counter
*counter
,
2280 struct perf_mmap_event
*mmap_event
)
2282 struct perf_output_handle handle
;
2283 int size
= mmap_event
->event
.header
.size
;
2284 int ret
= perf_output_begin(&handle
, counter
, size
, 0, 0);
2289 perf_output_put(&handle
, mmap_event
->event
);
2290 perf_output_copy(&handle
, mmap_event
->file_name
,
2291 mmap_event
->file_size
);
2292 perf_output_end(&handle
);
2295 static int perf_counter_mmap_match(struct perf_counter
*counter
,
2296 struct perf_mmap_event
*mmap_event
)
2298 if (counter
->hw_event
.mmap
&&
2299 mmap_event
->event
.header
.type
== PERF_EVENT_MMAP
)
2302 if (counter
->hw_event
.munmap
&&
2303 mmap_event
->event
.header
.type
== PERF_EVENT_MUNMAP
)
2309 static void perf_counter_mmap_ctx(struct perf_counter_context
*ctx
,
2310 struct perf_mmap_event
*mmap_event
)
2312 struct perf_counter
*counter
;
2314 if (system_state
!= SYSTEM_RUNNING
|| list_empty(&ctx
->event_list
))
2318 list_for_each_entry_rcu(counter
, &ctx
->event_list
, event_entry
) {
2319 if (perf_counter_mmap_match(counter
, mmap_event
))
2320 perf_counter_mmap_output(counter
, mmap_event
);
2325 static void perf_counter_mmap_event(struct perf_mmap_event
*mmap_event
)
2327 struct perf_cpu_context
*cpuctx
;
2328 struct file
*file
= mmap_event
->file
;
2335 buf
= kzalloc(PATH_MAX
, GFP_KERNEL
);
2337 name
= strncpy(tmp
, "//enomem", sizeof(tmp
));
2340 name
= d_path(&file
->f_path
, buf
, PATH_MAX
);
2342 name
= strncpy(tmp
, "//toolong", sizeof(tmp
));
2346 name
= strncpy(tmp
, "//anon", sizeof(tmp
));
2351 size
= ALIGN(strlen(name
)+1, sizeof(u64
));
2353 mmap_event
->file_name
= name
;
2354 mmap_event
->file_size
= size
;
2356 mmap_event
->event
.header
.size
= sizeof(mmap_event
->event
) + size
;
2358 cpuctx
= &get_cpu_var(perf_cpu_context
);
2359 perf_counter_mmap_ctx(&cpuctx
->ctx
, mmap_event
);
2360 put_cpu_var(perf_cpu_context
);
2362 perf_counter_mmap_ctx(¤t
->perf_counter_ctx
, mmap_event
);
2367 void perf_counter_mmap(unsigned long addr
, unsigned long len
,
2368 unsigned long pgoff
, struct file
*file
)
2370 struct perf_mmap_event mmap_event
;
2372 if (!atomic_read(&nr_mmap_tracking
))
2375 mmap_event
= (struct perf_mmap_event
){
2378 .header
= { .type
= PERF_EVENT_MMAP
, },
2379 .pid
= current
->group_leader
->pid
,
2380 .tid
= current
->pid
,
2387 perf_counter_mmap_event(&mmap_event
);
2390 void perf_counter_munmap(unsigned long addr
, unsigned long len
,
2391 unsigned long pgoff
, struct file
*file
)
2393 struct perf_mmap_event mmap_event
;
2395 if (!atomic_read(&nr_munmap_tracking
))
2398 mmap_event
= (struct perf_mmap_event
){
2401 .header
= { .type
= PERF_EVENT_MUNMAP
, },
2402 .pid
= current
->group_leader
->pid
,
2403 .tid
= current
->pid
,
2410 perf_counter_mmap_event(&mmap_event
);
2417 static void perf_log_period(struct perf_counter
*counter
, u64 period
)
2419 struct perf_output_handle handle
;
2423 struct perf_event_header header
;
2428 .type
= PERF_EVENT_PERIOD
,
2430 .size
= sizeof(freq_event
),
2432 .time
= sched_clock(),
2436 if (counter
->hw
.irq_period
== period
)
2439 ret
= perf_output_begin(&handle
, counter
, sizeof(freq_event
), 0, 0);
2443 perf_output_put(&handle
, freq_event
);
2444 perf_output_end(&handle
);
2448 * Generic counter overflow handling.
2451 int perf_counter_overflow(struct perf_counter
*counter
,
2452 int nmi
, struct pt_regs
*regs
, u64 addr
)
2454 int events
= atomic_read(&counter
->event_limit
);
2457 counter
->hw
.interrupts
++;
2460 * XXX event_limit might not quite work as expected on inherited
2464 counter
->pending_kill
= POLL_IN
;
2465 if (events
&& atomic_dec_and_test(&counter
->event_limit
)) {
2467 counter
->pending_kill
= POLL_HUP
;
2469 counter
->pending_disable
= 1;
2470 perf_pending_queue(&counter
->pending
,
2471 perf_pending_counter
);
2473 perf_counter_disable(counter
);
2476 perf_counter_output(counter
, nmi
, regs
, addr
);
2481 * Generic software counter infrastructure
2484 static void perf_swcounter_update(struct perf_counter
*counter
)
2486 struct hw_perf_counter
*hwc
= &counter
->hw
;
2491 prev
= atomic64_read(&hwc
->prev_count
);
2492 now
= atomic64_read(&hwc
->count
);
2493 if (atomic64_cmpxchg(&hwc
->prev_count
, prev
, now
) != prev
)
2498 atomic64_add(delta
, &counter
->count
);
2499 atomic64_sub(delta
, &hwc
->period_left
);
2502 static void perf_swcounter_set_period(struct perf_counter
*counter
)
2504 struct hw_perf_counter
*hwc
= &counter
->hw
;
2505 s64 left
= atomic64_read(&hwc
->period_left
);
2506 s64 period
= hwc
->irq_period
;
2508 if (unlikely(left
<= -period
)) {
2510 atomic64_set(&hwc
->period_left
, left
);
2513 if (unlikely(left
<= 0)) {
2515 atomic64_add(period
, &hwc
->period_left
);
2518 atomic64_set(&hwc
->prev_count
, -left
);
2519 atomic64_set(&hwc
->count
, -left
);
2522 static enum hrtimer_restart
perf_swcounter_hrtimer(struct hrtimer
*hrtimer
)
2524 enum hrtimer_restart ret
= HRTIMER_RESTART
;
2525 struct perf_counter
*counter
;
2526 struct pt_regs
*regs
;
2529 counter
= container_of(hrtimer
, struct perf_counter
, hw
.hrtimer
);
2530 counter
->pmu
->read(counter
);
2532 regs
= get_irq_regs();
2534 * In case we exclude kernel IPs or are somehow not in interrupt
2535 * context, provide the next best thing, the user IP.
2537 if ((counter
->hw_event
.exclude_kernel
|| !regs
) &&
2538 !counter
->hw_event
.exclude_user
)
2539 regs
= task_pt_regs(current
);
2542 if (perf_counter_overflow(counter
, 0, regs
, 0))
2543 ret
= HRTIMER_NORESTART
;
2546 period
= max_t(u64
, 10000, counter
->hw
.irq_period
);
2547 hrtimer_forward_now(hrtimer
, ns_to_ktime(period
));
2552 static void perf_swcounter_overflow(struct perf_counter
*counter
,
2553 int nmi
, struct pt_regs
*regs
, u64 addr
)
2555 perf_swcounter_update(counter
);
2556 perf_swcounter_set_period(counter
);
2557 if (perf_counter_overflow(counter
, nmi
, regs
, addr
))
2558 /* soft-disable the counter */
2563 static int perf_swcounter_match(struct perf_counter
*counter
,
2564 enum perf_event_types type
,
2565 u32 event
, struct pt_regs
*regs
)
2567 if (counter
->state
!= PERF_COUNTER_STATE_ACTIVE
)
2570 if (perf_event_raw(&counter
->hw_event
))
2573 if (perf_event_type(&counter
->hw_event
) != type
)
2576 if (perf_event_id(&counter
->hw_event
) != event
)
2579 if (counter
->hw_event
.exclude_user
&& user_mode(regs
))
2582 if (counter
->hw_event
.exclude_kernel
&& !user_mode(regs
))
2588 static void perf_swcounter_add(struct perf_counter
*counter
, u64 nr
,
2589 int nmi
, struct pt_regs
*regs
, u64 addr
)
2591 int neg
= atomic64_add_negative(nr
, &counter
->hw
.count
);
2592 if (counter
->hw
.irq_period
&& !neg
)
2593 perf_swcounter_overflow(counter
, nmi
, regs
, addr
);
2596 static void perf_swcounter_ctx_event(struct perf_counter_context
*ctx
,
2597 enum perf_event_types type
, u32 event
,
2598 u64 nr
, int nmi
, struct pt_regs
*regs
,
2601 struct perf_counter
*counter
;
2603 if (system_state
!= SYSTEM_RUNNING
|| list_empty(&ctx
->event_list
))
2607 list_for_each_entry_rcu(counter
, &ctx
->event_list
, event_entry
) {
2608 if (perf_swcounter_match(counter
, type
, event
, regs
))
2609 perf_swcounter_add(counter
, nr
, nmi
, regs
, addr
);
2614 static int *perf_swcounter_recursion_context(struct perf_cpu_context
*cpuctx
)
2617 return &cpuctx
->recursion
[3];
2620 return &cpuctx
->recursion
[2];
2623 return &cpuctx
->recursion
[1];
2625 return &cpuctx
->recursion
[0];
2628 static void __perf_swcounter_event(enum perf_event_types type
, u32 event
,
2629 u64 nr
, int nmi
, struct pt_regs
*regs
,
2632 struct perf_cpu_context
*cpuctx
= &get_cpu_var(perf_cpu_context
);
2633 int *recursion
= perf_swcounter_recursion_context(cpuctx
);
2641 perf_swcounter_ctx_event(&cpuctx
->ctx
, type
, event
,
2642 nr
, nmi
, regs
, addr
);
2643 if (cpuctx
->task_ctx
) {
2644 perf_swcounter_ctx_event(cpuctx
->task_ctx
, type
, event
,
2645 nr
, nmi
, regs
, addr
);
2652 put_cpu_var(perf_cpu_context
);
2656 perf_swcounter_event(u32 event
, u64 nr
, int nmi
, struct pt_regs
*regs
, u64 addr
)
2658 __perf_swcounter_event(PERF_TYPE_SOFTWARE
, event
, nr
, nmi
, regs
, addr
);
2661 static void perf_swcounter_read(struct perf_counter
*counter
)
2663 perf_swcounter_update(counter
);
2666 static int perf_swcounter_enable(struct perf_counter
*counter
)
2668 perf_swcounter_set_period(counter
);
2672 static void perf_swcounter_disable(struct perf_counter
*counter
)
2674 perf_swcounter_update(counter
);
2677 static const struct pmu perf_ops_generic
= {
2678 .enable
= perf_swcounter_enable
,
2679 .disable
= perf_swcounter_disable
,
2680 .read
= perf_swcounter_read
,
2684 * Software counter: cpu wall time clock
2687 static void cpu_clock_perf_counter_update(struct perf_counter
*counter
)
2689 int cpu
= raw_smp_processor_id();
2693 now
= cpu_clock(cpu
);
2694 prev
= atomic64_read(&counter
->hw
.prev_count
);
2695 atomic64_set(&counter
->hw
.prev_count
, now
);
2696 atomic64_add(now
- prev
, &counter
->count
);
2699 static int cpu_clock_perf_counter_enable(struct perf_counter
*counter
)
2701 struct hw_perf_counter
*hwc
= &counter
->hw
;
2702 int cpu
= raw_smp_processor_id();
2704 atomic64_set(&hwc
->prev_count
, cpu_clock(cpu
));
2705 hrtimer_init(&hwc
->hrtimer
, CLOCK_MONOTONIC
, HRTIMER_MODE_REL
);
2706 hwc
->hrtimer
.function
= perf_swcounter_hrtimer
;
2707 if (hwc
->irq_period
) {
2708 u64 period
= max_t(u64
, 10000, hwc
->irq_period
);
2709 __hrtimer_start_range_ns(&hwc
->hrtimer
,
2710 ns_to_ktime(period
), 0,
2711 HRTIMER_MODE_REL
, 0);
2717 static void cpu_clock_perf_counter_disable(struct perf_counter
*counter
)
2719 hrtimer_cancel(&counter
->hw
.hrtimer
);
2720 cpu_clock_perf_counter_update(counter
);
2723 static void cpu_clock_perf_counter_read(struct perf_counter
*counter
)
2725 cpu_clock_perf_counter_update(counter
);
2728 static const struct pmu perf_ops_cpu_clock
= {
2729 .enable
= cpu_clock_perf_counter_enable
,
2730 .disable
= cpu_clock_perf_counter_disable
,
2731 .read
= cpu_clock_perf_counter_read
,
2735 * Software counter: task time clock
2738 static void task_clock_perf_counter_update(struct perf_counter
*counter
, u64 now
)
2743 prev
= atomic64_xchg(&counter
->hw
.prev_count
, now
);
2745 atomic64_add(delta
, &counter
->count
);
2748 static int task_clock_perf_counter_enable(struct perf_counter
*counter
)
2750 struct hw_perf_counter
*hwc
= &counter
->hw
;
2753 now
= counter
->ctx
->time
;
2755 atomic64_set(&hwc
->prev_count
, now
);
2756 hrtimer_init(&hwc
->hrtimer
, CLOCK_MONOTONIC
, HRTIMER_MODE_REL
);
2757 hwc
->hrtimer
.function
= perf_swcounter_hrtimer
;
2758 if (hwc
->irq_period
) {
2759 u64 period
= max_t(u64
, 10000, hwc
->irq_period
);
2760 __hrtimer_start_range_ns(&hwc
->hrtimer
,
2761 ns_to_ktime(period
), 0,
2762 HRTIMER_MODE_REL
, 0);
2768 static void task_clock_perf_counter_disable(struct perf_counter
*counter
)
2770 hrtimer_cancel(&counter
->hw
.hrtimer
);
2771 task_clock_perf_counter_update(counter
, counter
->ctx
->time
);
2775 static void task_clock_perf_counter_read(struct perf_counter
*counter
)
2780 update_context_time(counter
->ctx
);
2781 time
= counter
->ctx
->time
;
2783 u64 now
= perf_clock();
2784 u64 delta
= now
- counter
->ctx
->timestamp
;
2785 time
= counter
->ctx
->time
+ delta
;
2788 task_clock_perf_counter_update(counter
, time
);
2791 static const struct pmu perf_ops_task_clock
= {
2792 .enable
= task_clock_perf_counter_enable
,
2793 .disable
= task_clock_perf_counter_disable
,
2794 .read
= task_clock_perf_counter_read
,
2798 * Software counter: cpu migrations
2801 static inline u64
get_cpu_migrations(struct perf_counter
*counter
)
2803 struct task_struct
*curr
= counter
->ctx
->task
;
2806 return curr
->se
.nr_migrations
;
2807 return cpu_nr_migrations(smp_processor_id());
2810 static void cpu_migrations_perf_counter_update(struct perf_counter
*counter
)
2815 prev
= atomic64_read(&counter
->hw
.prev_count
);
2816 now
= get_cpu_migrations(counter
);
2818 atomic64_set(&counter
->hw
.prev_count
, now
);
2822 atomic64_add(delta
, &counter
->count
);
2825 static void cpu_migrations_perf_counter_read(struct perf_counter
*counter
)
2827 cpu_migrations_perf_counter_update(counter
);
2830 static int cpu_migrations_perf_counter_enable(struct perf_counter
*counter
)
2832 if (counter
->prev_state
<= PERF_COUNTER_STATE_OFF
)
2833 atomic64_set(&counter
->hw
.prev_count
,
2834 get_cpu_migrations(counter
));
2838 static void cpu_migrations_perf_counter_disable(struct perf_counter
*counter
)
2840 cpu_migrations_perf_counter_update(counter
);
2843 static const struct pmu perf_ops_cpu_migrations
= {
2844 .enable
= cpu_migrations_perf_counter_enable
,
2845 .disable
= cpu_migrations_perf_counter_disable
,
2846 .read
= cpu_migrations_perf_counter_read
,
2849 #ifdef CONFIG_EVENT_PROFILE
2850 void perf_tpcounter_event(int event_id
)
2852 struct pt_regs
*regs
= get_irq_regs();
2855 regs
= task_pt_regs(current
);
2857 __perf_swcounter_event(PERF_TYPE_TRACEPOINT
, event_id
, 1, 1, regs
, 0);
2859 EXPORT_SYMBOL_GPL(perf_tpcounter_event
);
2861 extern int ftrace_profile_enable(int);
2862 extern void ftrace_profile_disable(int);
2864 static void tp_perf_counter_destroy(struct perf_counter
*counter
)
2866 ftrace_profile_disable(perf_event_id(&counter
->hw_event
));
2869 static const struct pmu
*tp_perf_counter_init(struct perf_counter
*counter
)
2871 int event_id
= perf_event_id(&counter
->hw_event
);
2874 ret
= ftrace_profile_enable(event_id
);
2878 counter
->destroy
= tp_perf_counter_destroy
;
2879 counter
->hw
.irq_period
= counter
->hw_event
.irq_period
;
2881 return &perf_ops_generic
;
2884 static const struct pmu
*tp_perf_counter_init(struct perf_counter
*counter
)
2890 static const struct pmu
*sw_perf_counter_init(struct perf_counter
*counter
)
2892 const struct pmu
*pmu
= NULL
;
2895 * Software counters (currently) can't in general distinguish
2896 * between user, kernel and hypervisor events.
2897 * However, context switches and cpu migrations are considered
2898 * to be kernel events, and page faults are never hypervisor
2901 switch (perf_event_id(&counter
->hw_event
)) {
2902 case PERF_COUNT_CPU_CLOCK
:
2903 pmu
= &perf_ops_cpu_clock
;
2906 case PERF_COUNT_TASK_CLOCK
:
2908 * If the user instantiates this as a per-cpu counter,
2909 * use the cpu_clock counter instead.
2911 if (counter
->ctx
->task
)
2912 pmu
= &perf_ops_task_clock
;
2914 pmu
= &perf_ops_cpu_clock
;
2917 case PERF_COUNT_PAGE_FAULTS
:
2918 case PERF_COUNT_PAGE_FAULTS_MIN
:
2919 case PERF_COUNT_PAGE_FAULTS_MAJ
:
2920 case PERF_COUNT_CONTEXT_SWITCHES
:
2921 pmu
= &perf_ops_generic
;
2923 case PERF_COUNT_CPU_MIGRATIONS
:
2924 if (!counter
->hw_event
.exclude_kernel
)
2925 pmu
= &perf_ops_cpu_migrations
;
2933 * Allocate and initialize a counter structure
2935 static struct perf_counter
*
2936 perf_counter_alloc(struct perf_counter_hw_event
*hw_event
,
2938 struct perf_counter_context
*ctx
,
2939 struct perf_counter
*group_leader
,
2942 const struct pmu
*pmu
;
2943 struct perf_counter
*counter
;
2944 struct hw_perf_counter
*hwc
;
2947 counter
= kzalloc(sizeof(*counter
), gfpflags
);
2949 return ERR_PTR(-ENOMEM
);
2952 * Single counters are their own group leaders, with an
2953 * empty sibling list:
2956 group_leader
= counter
;
2958 mutex_init(&counter
->mutex
);
2959 INIT_LIST_HEAD(&counter
->list_entry
);
2960 INIT_LIST_HEAD(&counter
->event_entry
);
2961 INIT_LIST_HEAD(&counter
->sibling_list
);
2962 init_waitqueue_head(&counter
->waitq
);
2964 mutex_init(&counter
->mmap_mutex
);
2966 INIT_LIST_HEAD(&counter
->child_list
);
2969 counter
->hw_event
= *hw_event
;
2970 counter
->group_leader
= group_leader
;
2971 counter
->pmu
= NULL
;
2974 counter
->state
= PERF_COUNTER_STATE_INACTIVE
;
2975 if (hw_event
->disabled
)
2976 counter
->state
= PERF_COUNTER_STATE_OFF
;
2981 if (hw_event
->freq
&& hw_event
->irq_freq
)
2982 hwc
->irq_period
= div64_u64(TICK_NSEC
, hw_event
->irq_freq
);
2984 hwc
->irq_period
= hw_event
->irq_period
;
2987 * we currently do not support PERF_RECORD_GROUP on inherited counters
2989 if (hw_event
->inherit
&& (hw_event
->record_type
& PERF_RECORD_GROUP
))
2992 if (perf_event_raw(hw_event
)) {
2993 pmu
= hw_perf_counter_init(counter
);
2997 switch (perf_event_type(hw_event
)) {
2998 case PERF_TYPE_HARDWARE
:
2999 pmu
= hw_perf_counter_init(counter
);
3002 case PERF_TYPE_SOFTWARE
:
3003 pmu
= sw_perf_counter_init(counter
);
3006 case PERF_TYPE_TRACEPOINT
:
3007 pmu
= tp_perf_counter_init(counter
);
3014 else if (IS_ERR(pmu
))
3019 return ERR_PTR(err
);
3024 atomic_inc(&nr_counters
);
3025 if (counter
->hw_event
.mmap
)
3026 atomic_inc(&nr_mmap_tracking
);
3027 if (counter
->hw_event
.munmap
)
3028 atomic_inc(&nr_munmap_tracking
);
3029 if (counter
->hw_event
.comm
)
3030 atomic_inc(&nr_comm_tracking
);
3036 * sys_perf_counter_open - open a performance counter, associate it to a task/cpu
3038 * @hw_event_uptr: event type attributes for monitoring/sampling
3041 * @group_fd: group leader counter fd
3043 SYSCALL_DEFINE5(perf_counter_open
,
3044 const struct perf_counter_hw_event __user
*, hw_event_uptr
,
3045 pid_t
, pid
, int, cpu
, int, group_fd
, unsigned long, flags
)
3047 struct perf_counter
*counter
, *group_leader
;
3048 struct perf_counter_hw_event hw_event
;
3049 struct perf_counter_context
*ctx
;
3050 struct file
*counter_file
= NULL
;
3051 struct file
*group_file
= NULL
;
3052 int fput_needed
= 0;
3053 int fput_needed2
= 0;
3056 /* for future expandability... */
3060 if (copy_from_user(&hw_event
, hw_event_uptr
, sizeof(hw_event
)) != 0)
3064 * Get the target context (task or percpu):
3066 ctx
= find_get_context(pid
, cpu
);
3068 return PTR_ERR(ctx
);
3071 * Look up the group leader (we will attach this counter to it):
3073 group_leader
= NULL
;
3074 if (group_fd
!= -1) {
3076 group_file
= fget_light(group_fd
, &fput_needed
);
3078 goto err_put_context
;
3079 if (group_file
->f_op
!= &perf_fops
)
3080 goto err_put_context
;
3082 group_leader
= group_file
->private_data
;
3084 * Do not allow a recursive hierarchy (this new sibling
3085 * becoming part of another group-sibling):
3087 if (group_leader
->group_leader
!= group_leader
)
3088 goto err_put_context
;
3090 * Do not allow to attach to a group in a different
3091 * task or CPU context:
3093 if (group_leader
->ctx
!= ctx
)
3094 goto err_put_context
;
3096 * Only a group leader can be exclusive or pinned
3098 if (hw_event
.exclusive
|| hw_event
.pinned
)
3099 goto err_put_context
;
3102 counter
= perf_counter_alloc(&hw_event
, cpu
, ctx
, group_leader
,
3104 ret
= PTR_ERR(counter
);
3105 if (IS_ERR(counter
))
3106 goto err_put_context
;
3108 ret
= anon_inode_getfd("[perf_counter]", &perf_fops
, counter
, 0);
3110 goto err_free_put_context
;
3112 counter_file
= fget_light(ret
, &fput_needed2
);
3114 goto err_free_put_context
;
3116 counter
->filp
= counter_file
;
3117 mutex_lock(&ctx
->mutex
);
3118 perf_install_in_context(ctx
, counter
, cpu
);
3119 mutex_unlock(&ctx
->mutex
);
3121 fput_light(counter_file
, fput_needed2
);
3124 fput_light(group_file
, fput_needed
);
3128 err_free_put_context
:
3138 * Initialize the perf_counter context in a task_struct:
3141 __perf_counter_init_context(struct perf_counter_context
*ctx
,
3142 struct task_struct
*task
)
3144 memset(ctx
, 0, sizeof(*ctx
));
3145 spin_lock_init(&ctx
->lock
);
3146 mutex_init(&ctx
->mutex
);
3147 INIT_LIST_HEAD(&ctx
->counter_list
);
3148 INIT_LIST_HEAD(&ctx
->event_list
);
3153 * inherit a counter from parent task to child task:
3155 static struct perf_counter
*
3156 inherit_counter(struct perf_counter
*parent_counter
,
3157 struct task_struct
*parent
,
3158 struct perf_counter_context
*parent_ctx
,
3159 struct task_struct
*child
,
3160 struct perf_counter
*group_leader
,
3161 struct perf_counter_context
*child_ctx
)
3163 struct perf_counter
*child_counter
;
3166 * Instead of creating recursive hierarchies of counters,
3167 * we link inherited counters back to the original parent,
3168 * which has a filp for sure, which we use as the reference
3171 if (parent_counter
->parent
)
3172 parent_counter
= parent_counter
->parent
;
3174 child_counter
= perf_counter_alloc(&parent_counter
->hw_event
,
3175 parent_counter
->cpu
, child_ctx
,
3176 group_leader
, GFP_KERNEL
);
3177 if (IS_ERR(child_counter
))
3178 return child_counter
;
3181 * Link it up in the child's context:
3183 child_counter
->task
= child
;
3184 add_counter_to_ctx(child_counter
, child_ctx
);
3186 child_counter
->parent
= parent_counter
;
3188 * inherit into child's child as well:
3190 child_counter
->hw_event
.inherit
= 1;
3193 * Get a reference to the parent filp - we will fput it
3194 * when the child counter exits. This is safe to do because
3195 * we are in the parent and we know that the filp still
3196 * exists and has a nonzero count:
3198 atomic_long_inc(&parent_counter
->filp
->f_count
);
3201 * Link this into the parent counter's child list
3203 mutex_lock(&parent_counter
->mutex
);
3204 list_add_tail(&child_counter
->child_list
, &parent_counter
->child_list
);
3207 * Make the child state follow the state of the parent counter,
3208 * not its hw_event.disabled bit. We hold the parent's mutex,
3209 * so we won't race with perf_counter_{en,dis}able_family.
3211 if (parent_counter
->state
>= PERF_COUNTER_STATE_INACTIVE
)
3212 child_counter
->state
= PERF_COUNTER_STATE_INACTIVE
;
3214 child_counter
->state
= PERF_COUNTER_STATE_OFF
;
3216 mutex_unlock(&parent_counter
->mutex
);
3218 return child_counter
;
3221 static int inherit_group(struct perf_counter
*parent_counter
,
3222 struct task_struct
*parent
,
3223 struct perf_counter_context
*parent_ctx
,
3224 struct task_struct
*child
,
3225 struct perf_counter_context
*child_ctx
)
3227 struct perf_counter
*leader
;
3228 struct perf_counter
*sub
;
3229 struct perf_counter
*child_ctr
;
3231 leader
= inherit_counter(parent_counter
, parent
, parent_ctx
,
3232 child
, NULL
, child_ctx
);
3234 return PTR_ERR(leader
);
3235 list_for_each_entry(sub
, &parent_counter
->sibling_list
, list_entry
) {
3236 child_ctr
= inherit_counter(sub
, parent
, parent_ctx
,
3237 child
, leader
, child_ctx
);
3238 if (IS_ERR(child_ctr
))
3239 return PTR_ERR(child_ctr
);
3244 static void sync_child_counter(struct perf_counter
*child_counter
,
3245 struct perf_counter
*parent_counter
)
3249 child_val
= atomic64_read(&child_counter
->count
);
3252 * Add back the child's count to the parent's count:
3254 atomic64_add(child_val
, &parent_counter
->count
);
3255 atomic64_add(child_counter
->total_time_enabled
,
3256 &parent_counter
->child_total_time_enabled
);
3257 atomic64_add(child_counter
->total_time_running
,
3258 &parent_counter
->child_total_time_running
);
3261 * Remove this counter from the parent's list
3263 mutex_lock(&parent_counter
->mutex
);
3264 list_del_init(&child_counter
->child_list
);
3265 mutex_unlock(&parent_counter
->mutex
);
3268 * Release the parent counter, if this was the last
3271 fput(parent_counter
->filp
);
3275 __perf_counter_exit_task(struct task_struct
*child
,
3276 struct perf_counter
*child_counter
,
3277 struct perf_counter_context
*child_ctx
)
3279 struct perf_counter
*parent_counter
;
3282 * If we do not self-reap then we have to wait for the
3283 * child task to unschedule (it will happen for sure),
3284 * so that its counter is at its final count. (This
3285 * condition triggers rarely - child tasks usually get
3286 * off their CPU before the parent has a chance to
3287 * get this far into the reaping action)
3289 if (child
!= current
) {
3290 wait_task_inactive(child
, 0);
3291 update_counter_times(child_counter
);
3292 list_del_counter(child_counter
, child_ctx
);
3294 struct perf_cpu_context
*cpuctx
;
3295 unsigned long flags
;
3298 * Disable and unlink this counter.
3300 * Be careful about zapping the list - IRQ/NMI context
3301 * could still be processing it:
3303 local_irq_save(flags
);
3306 cpuctx
= &__get_cpu_var(perf_cpu_context
);
3308 group_sched_out(child_counter
, cpuctx
, child_ctx
);
3309 update_counter_times(child_counter
);
3311 list_del_counter(child_counter
, child_ctx
);
3314 local_irq_restore(flags
);
3317 parent_counter
= child_counter
->parent
;
3319 * It can happen that parent exits first, and has counters
3320 * that are still around due to the child reference. These
3321 * counters need to be zapped - but otherwise linger.
3323 if (parent_counter
) {
3324 sync_child_counter(child_counter
, parent_counter
);
3325 free_counter(child_counter
);
3330 * When a child task exits, feed back counter values to parent counters.
3332 * Note: we may be running in child context, but the PID is not hashed
3333 * anymore so new counters will not be added.
3335 void perf_counter_exit_task(struct task_struct
*child
)
3337 struct perf_counter
*child_counter
, *tmp
;
3338 struct perf_counter_context
*child_ctx
;
3340 WARN_ON_ONCE(child
!= current
);
3342 child_ctx
= &child
->perf_counter_ctx
;
3344 if (likely(!child_ctx
->nr_counters
))
3348 list_for_each_entry_safe(child_counter
, tmp
, &child_ctx
->counter_list
,
3350 __perf_counter_exit_task(child
, child_counter
, child_ctx
);
3353 * If the last counter was a group counter, it will have appended all
3354 * its siblings to the list, but we obtained 'tmp' before that which
3355 * will still point to the list head terminating the iteration.
3357 if (!list_empty(&child_ctx
->counter_list
))
3362 * Initialize the perf_counter context in task_struct
3364 void perf_counter_init_task(struct task_struct
*child
)
3366 struct perf_counter_context
*child_ctx
, *parent_ctx
;
3367 struct perf_counter
*counter
;
3368 struct task_struct
*parent
= current
;
3370 child_ctx
= &child
->perf_counter_ctx
;
3371 parent_ctx
= &parent
->perf_counter_ctx
;
3373 __perf_counter_init_context(child_ctx
, child
);
3376 * This is executed from the parent task context, so inherit
3377 * counters that have been marked for cloning:
3380 if (likely(!parent_ctx
->nr_counters
))
3384 * Lock the parent list. No need to lock the child - not PID
3385 * hashed yet and not running, so nobody can access it.
3387 mutex_lock(&parent_ctx
->mutex
);
3390 * We dont have to disable NMIs - we are only looking at
3391 * the list, not manipulating it:
3393 list_for_each_entry_rcu(counter
, &parent_ctx
->event_list
, event_entry
) {
3394 if (counter
!= counter
->group_leader
)
3397 if (!counter
->hw_event
.inherit
)
3400 if (inherit_group(counter
, parent
,
3401 parent_ctx
, child
, child_ctx
))
3405 mutex_unlock(&parent_ctx
->mutex
);
3408 static void __cpuinit
perf_counter_init_cpu(int cpu
)
3410 struct perf_cpu_context
*cpuctx
;
3412 cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
3413 __perf_counter_init_context(&cpuctx
->ctx
, NULL
);
3415 spin_lock(&perf_resource_lock
);
3416 cpuctx
->max_pertask
= perf_max_counters
- perf_reserved_percpu
;
3417 spin_unlock(&perf_resource_lock
);
3419 hw_perf_counter_setup(cpu
);
3422 #ifdef CONFIG_HOTPLUG_CPU
3423 static void __perf_counter_exit_cpu(void *info
)
3425 struct perf_cpu_context
*cpuctx
= &__get_cpu_var(perf_cpu_context
);
3426 struct perf_counter_context
*ctx
= &cpuctx
->ctx
;
3427 struct perf_counter
*counter
, *tmp
;
3429 list_for_each_entry_safe(counter
, tmp
, &ctx
->counter_list
, list_entry
)
3430 __perf_counter_remove_from_context(counter
);
3432 static void perf_counter_exit_cpu(int cpu
)
3434 struct perf_cpu_context
*cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
3435 struct perf_counter_context
*ctx
= &cpuctx
->ctx
;
3437 mutex_lock(&ctx
->mutex
);
3438 smp_call_function_single(cpu
, __perf_counter_exit_cpu
, NULL
, 1);
3439 mutex_unlock(&ctx
->mutex
);
3442 static inline void perf_counter_exit_cpu(int cpu
) { }
3445 static int __cpuinit
3446 perf_cpu_notify(struct notifier_block
*self
, unsigned long action
, void *hcpu
)
3448 unsigned int cpu
= (long)hcpu
;
3452 case CPU_UP_PREPARE
:
3453 case CPU_UP_PREPARE_FROZEN
:
3454 perf_counter_init_cpu(cpu
);
3457 case CPU_DOWN_PREPARE
:
3458 case CPU_DOWN_PREPARE_FROZEN
:
3459 perf_counter_exit_cpu(cpu
);
3469 static struct notifier_block __cpuinitdata perf_cpu_nb
= {
3470 .notifier_call
= perf_cpu_notify
,
3473 void __init
perf_counter_init(void)
3475 perf_cpu_notify(&perf_cpu_nb
, (unsigned long)CPU_UP_PREPARE
,
3476 (void *)(long)smp_processor_id());
3477 register_cpu_notifier(&perf_cpu_nb
);
3480 static ssize_t
perf_show_reserve_percpu(struct sysdev_class
*class, char *buf
)
3482 return sprintf(buf
, "%d\n", perf_reserved_percpu
);
3486 perf_set_reserve_percpu(struct sysdev_class
*class,
3490 struct perf_cpu_context
*cpuctx
;
3494 err
= strict_strtoul(buf
, 10, &val
);
3497 if (val
> perf_max_counters
)
3500 spin_lock(&perf_resource_lock
);
3501 perf_reserved_percpu
= val
;
3502 for_each_online_cpu(cpu
) {
3503 cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
3504 spin_lock_irq(&cpuctx
->ctx
.lock
);
3505 mpt
= min(perf_max_counters
- cpuctx
->ctx
.nr_counters
,
3506 perf_max_counters
- perf_reserved_percpu
);
3507 cpuctx
->max_pertask
= mpt
;
3508 spin_unlock_irq(&cpuctx
->ctx
.lock
);
3510 spin_unlock(&perf_resource_lock
);
3515 static ssize_t
perf_show_overcommit(struct sysdev_class
*class, char *buf
)
3517 return sprintf(buf
, "%d\n", perf_overcommit
);
3521 perf_set_overcommit(struct sysdev_class
*class, const char *buf
, size_t count
)
3526 err
= strict_strtoul(buf
, 10, &val
);
3532 spin_lock(&perf_resource_lock
);
3533 perf_overcommit
= val
;
3534 spin_unlock(&perf_resource_lock
);
3539 static SYSDEV_CLASS_ATTR(
3542 perf_show_reserve_percpu
,
3543 perf_set_reserve_percpu
3546 static SYSDEV_CLASS_ATTR(
3549 perf_show_overcommit
,
3553 static struct attribute
*perfclass_attrs
[] = {
3554 &attr_reserve_percpu
.attr
,
3555 &attr_overcommit
.attr
,
3559 static struct attribute_group perfclass_attr_group
= {
3560 .attrs
= perfclass_attrs
,
3561 .name
= "perf_counters",
3564 static int __init
perf_counter_sysfs_init(void)
3566 return sysfs_create_group(&cpu_sysdev_class
.kset
.kobj
,
3567 &perfclass_attr_group
);
3569 device_initcall(perf_counter_sysfs_init
);