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_mmap_tracking __read_mostly
;
43 static atomic_t nr_munmap_tracking __read_mostly
;
44 static atomic_t nr_comm_tracking __read_mostly
;
46 int sysctl_perf_counter_priv __read_mostly
; /* do we need to be privileged */
49 * Lock for (sysadmin-configurable) counter reservations:
51 static DEFINE_SPINLOCK(perf_resource_lock
);
54 * Architecture provided APIs - weak aliases:
56 extern __weak
const struct pmu
*hw_perf_counter_init(struct perf_counter
*counter
)
61 u64 __weak
hw_perf_save_disable(void) { return 0; }
62 void __weak
hw_perf_restore(u64 ctrl
) { barrier(); }
63 void __weak
hw_perf_counter_setup(int cpu
) { barrier(); }
64 int __weak
hw_perf_group_sched_in(struct perf_counter
*group_leader
,
65 struct perf_cpu_context
*cpuctx
,
66 struct perf_counter_context
*ctx
, int cpu
)
71 void __weak
perf_counter_print_debug(void) { }
74 list_add_counter(struct perf_counter
*counter
, struct perf_counter_context
*ctx
)
76 struct perf_counter
*group_leader
= counter
->group_leader
;
79 * Depending on whether it is a standalone or sibling counter,
80 * add it straight to the context's counter list, or to the group
81 * leader's sibling list:
83 if (counter
->group_leader
== counter
)
84 list_add_tail(&counter
->list_entry
, &ctx
->counter_list
);
86 list_add_tail(&counter
->list_entry
, &group_leader
->sibling_list
);
87 group_leader
->nr_siblings
++;
90 list_add_rcu(&counter
->event_entry
, &ctx
->event_list
);
94 list_del_counter(struct perf_counter
*counter
, struct perf_counter_context
*ctx
)
96 struct perf_counter
*sibling
, *tmp
;
98 list_del_init(&counter
->list_entry
);
99 list_del_rcu(&counter
->event_entry
);
101 if (counter
->group_leader
!= counter
)
102 counter
->group_leader
->nr_siblings
--;
105 * If this was a group counter with sibling counters then
106 * upgrade the siblings to singleton counters by adding them
107 * to the context list directly:
109 list_for_each_entry_safe(sibling
, tmp
,
110 &counter
->sibling_list
, list_entry
) {
112 list_move_tail(&sibling
->list_entry
, &ctx
->counter_list
);
113 sibling
->group_leader
= sibling
;
118 counter_sched_out(struct perf_counter
*counter
,
119 struct perf_cpu_context
*cpuctx
,
120 struct perf_counter_context
*ctx
)
122 if (counter
->state
!= PERF_COUNTER_STATE_ACTIVE
)
125 counter
->state
= PERF_COUNTER_STATE_INACTIVE
;
126 counter
->tstamp_stopped
= ctx
->time
;
127 counter
->pmu
->disable(counter
);
130 if (!is_software_counter(counter
))
131 cpuctx
->active_oncpu
--;
133 if (counter
->hw_event
.exclusive
|| !cpuctx
->active_oncpu
)
134 cpuctx
->exclusive
= 0;
138 group_sched_out(struct perf_counter
*group_counter
,
139 struct perf_cpu_context
*cpuctx
,
140 struct perf_counter_context
*ctx
)
142 struct perf_counter
*counter
;
144 if (group_counter
->state
!= PERF_COUNTER_STATE_ACTIVE
)
147 counter_sched_out(group_counter
, cpuctx
, ctx
);
150 * Schedule out siblings (if any):
152 list_for_each_entry(counter
, &group_counter
->sibling_list
, list_entry
)
153 counter_sched_out(counter
, cpuctx
, ctx
);
155 if (group_counter
->hw_event
.exclusive
)
156 cpuctx
->exclusive
= 0;
160 * Cross CPU call to remove a performance counter
162 * We disable the counter on the hardware level first. After that we
163 * remove it from the context list.
165 static void __perf_counter_remove_from_context(void *info
)
167 struct perf_cpu_context
*cpuctx
= &__get_cpu_var(perf_cpu_context
);
168 struct perf_counter
*counter
= info
;
169 struct perf_counter_context
*ctx
= counter
->ctx
;
174 * If this is a task context, we need to check whether it is
175 * the current task context of this cpu. If not it has been
176 * scheduled out before the smp call arrived.
178 if (ctx
->task
&& cpuctx
->task_ctx
!= ctx
)
181 spin_lock_irqsave(&ctx
->lock
, flags
);
183 counter_sched_out(counter
, cpuctx
, ctx
);
185 counter
->task
= NULL
;
189 * Protect the list operation against NMI by disabling the
190 * counters on a global level. NOP for non NMI based counters.
192 perf_flags
= hw_perf_save_disable();
193 list_del_counter(counter
, ctx
);
194 hw_perf_restore(perf_flags
);
198 * Allow more per task counters with respect to the
201 cpuctx
->max_pertask
=
202 min(perf_max_counters
- ctx
->nr_counters
,
203 perf_max_counters
- perf_reserved_percpu
);
206 spin_unlock_irqrestore(&ctx
->lock
, flags
);
211 * Remove the counter from a task's (or a CPU's) list of counters.
213 * Must be called with counter->mutex and ctx->mutex held.
215 * CPU counters are removed with a smp call. For task counters we only
216 * call when the task is on a CPU.
218 static void perf_counter_remove_from_context(struct perf_counter
*counter
)
220 struct perf_counter_context
*ctx
= counter
->ctx
;
221 struct task_struct
*task
= ctx
->task
;
225 * Per cpu counters are removed via an smp call and
226 * the removal is always sucessful.
228 smp_call_function_single(counter
->cpu
,
229 __perf_counter_remove_from_context
,
235 task_oncpu_function_call(task
, __perf_counter_remove_from_context
,
238 spin_lock_irq(&ctx
->lock
);
240 * If the context is active we need to retry the smp call.
242 if (ctx
->nr_active
&& !list_empty(&counter
->list_entry
)) {
243 spin_unlock_irq(&ctx
->lock
);
248 * The lock prevents that this context is scheduled in so we
249 * can remove the counter safely, if the call above did not
252 if (!list_empty(&counter
->list_entry
)) {
254 list_del_counter(counter
, ctx
);
255 counter
->task
= NULL
;
257 spin_unlock_irq(&ctx
->lock
);
260 static inline u64
perf_clock(void)
262 return cpu_clock(smp_processor_id());
266 * Update the record of the current time in a context.
268 static void update_context_time(struct perf_counter_context
*ctx
)
270 u64 now
= perf_clock();
272 ctx
->time
+= now
- ctx
->timestamp
;
273 ctx
->timestamp
= now
;
277 * Update the total_time_enabled and total_time_running fields for a counter.
279 static void update_counter_times(struct perf_counter
*counter
)
281 struct perf_counter_context
*ctx
= counter
->ctx
;
284 if (counter
->state
< PERF_COUNTER_STATE_INACTIVE
)
287 counter
->total_time_enabled
= ctx
->time
- counter
->tstamp_enabled
;
289 if (counter
->state
== PERF_COUNTER_STATE_INACTIVE
)
290 run_end
= counter
->tstamp_stopped
;
294 counter
->total_time_running
= run_end
- counter
->tstamp_running
;
298 * Update total_time_enabled and total_time_running for all counters in a group.
300 static void update_group_times(struct perf_counter
*leader
)
302 struct perf_counter
*counter
;
304 update_counter_times(leader
);
305 list_for_each_entry(counter
, &leader
->sibling_list
, list_entry
)
306 update_counter_times(counter
);
310 * Cross CPU call to disable a performance counter
312 static void __perf_counter_disable(void *info
)
314 struct perf_counter
*counter
= info
;
315 struct perf_cpu_context
*cpuctx
= &__get_cpu_var(perf_cpu_context
);
316 struct perf_counter_context
*ctx
= counter
->ctx
;
320 * If this is a per-task counter, need to check whether this
321 * counter's task is the current task on this cpu.
323 if (ctx
->task
&& cpuctx
->task_ctx
!= ctx
)
326 spin_lock_irqsave(&ctx
->lock
, flags
);
329 * If the counter is on, turn it off.
330 * If it is in error state, leave it in error state.
332 if (counter
->state
>= PERF_COUNTER_STATE_INACTIVE
) {
333 update_context_time(ctx
);
334 update_counter_times(counter
);
335 if (counter
== counter
->group_leader
)
336 group_sched_out(counter
, cpuctx
, ctx
);
338 counter_sched_out(counter
, cpuctx
, ctx
);
339 counter
->state
= PERF_COUNTER_STATE_OFF
;
342 spin_unlock_irqrestore(&ctx
->lock
, flags
);
348 static void perf_counter_disable(struct perf_counter
*counter
)
350 struct perf_counter_context
*ctx
= counter
->ctx
;
351 struct task_struct
*task
= ctx
->task
;
355 * Disable the counter on the cpu that it's on
357 smp_call_function_single(counter
->cpu
, __perf_counter_disable
,
363 task_oncpu_function_call(task
, __perf_counter_disable
, counter
);
365 spin_lock_irq(&ctx
->lock
);
367 * If the counter is still active, we need to retry the cross-call.
369 if (counter
->state
== PERF_COUNTER_STATE_ACTIVE
) {
370 spin_unlock_irq(&ctx
->lock
);
375 * Since we have the lock this context can't be scheduled
376 * in, so we can change the state safely.
378 if (counter
->state
== PERF_COUNTER_STATE_INACTIVE
) {
379 update_counter_times(counter
);
380 counter
->state
= PERF_COUNTER_STATE_OFF
;
383 spin_unlock_irq(&ctx
->lock
);
387 * Disable a counter and all its children.
389 static void perf_counter_disable_family(struct perf_counter
*counter
)
391 struct perf_counter
*child
;
393 perf_counter_disable(counter
);
396 * Lock the mutex to protect the list of children
398 mutex_lock(&counter
->mutex
);
399 list_for_each_entry(child
, &counter
->child_list
, child_list
)
400 perf_counter_disable(child
);
401 mutex_unlock(&counter
->mutex
);
405 counter_sched_in(struct perf_counter
*counter
,
406 struct perf_cpu_context
*cpuctx
,
407 struct perf_counter_context
*ctx
,
410 if (counter
->state
<= PERF_COUNTER_STATE_OFF
)
413 counter
->state
= PERF_COUNTER_STATE_ACTIVE
;
414 counter
->oncpu
= cpu
; /* TODO: put 'cpu' into cpuctx->cpu */
416 * The new state must be visible before we turn it on in the hardware:
420 if (counter
->pmu
->enable(counter
)) {
421 counter
->state
= PERF_COUNTER_STATE_INACTIVE
;
426 counter
->tstamp_running
+= ctx
->time
- counter
->tstamp_stopped
;
428 if (!is_software_counter(counter
))
429 cpuctx
->active_oncpu
++;
432 if (counter
->hw_event
.exclusive
)
433 cpuctx
->exclusive
= 1;
439 * Return 1 for a group consisting entirely of software counters,
440 * 0 if the group contains any hardware counters.
442 static int is_software_only_group(struct perf_counter
*leader
)
444 struct perf_counter
*counter
;
446 if (!is_software_counter(leader
))
449 list_for_each_entry(counter
, &leader
->sibling_list
, list_entry
)
450 if (!is_software_counter(counter
))
457 * Work out whether we can put this counter group on the CPU now.
459 static int group_can_go_on(struct perf_counter
*counter
,
460 struct perf_cpu_context
*cpuctx
,
464 * Groups consisting entirely of software counters can always go on.
466 if (is_software_only_group(counter
))
469 * If an exclusive group is already on, no other hardware
470 * counters can go on.
472 if (cpuctx
->exclusive
)
475 * If this group is exclusive and there are already
476 * counters on the CPU, it can't go on.
478 if (counter
->hw_event
.exclusive
&& cpuctx
->active_oncpu
)
481 * Otherwise, try to add it if all previous groups were able
487 static void add_counter_to_ctx(struct perf_counter
*counter
,
488 struct perf_counter_context
*ctx
)
490 list_add_counter(counter
, ctx
);
492 counter
->prev_state
= PERF_COUNTER_STATE_OFF
;
493 counter
->tstamp_enabled
= ctx
->time
;
494 counter
->tstamp_running
= ctx
->time
;
495 counter
->tstamp_stopped
= ctx
->time
;
499 * Cross CPU call to install and enable a performance counter
501 static void __perf_install_in_context(void *info
)
503 struct perf_cpu_context
*cpuctx
= &__get_cpu_var(perf_cpu_context
);
504 struct perf_counter
*counter
= info
;
505 struct perf_counter_context
*ctx
= counter
->ctx
;
506 struct perf_counter
*leader
= counter
->group_leader
;
507 int cpu
= smp_processor_id();
513 * If this is a task context, we need to check whether it is
514 * the current task context of this cpu. If not it has been
515 * scheduled out before the smp call arrived.
517 if (ctx
->task
&& cpuctx
->task_ctx
!= ctx
)
520 spin_lock_irqsave(&ctx
->lock
, flags
);
521 update_context_time(ctx
);
524 * Protect the list operation against NMI by disabling the
525 * counters on a global level. NOP for non NMI based counters.
527 perf_flags
= hw_perf_save_disable();
529 add_counter_to_ctx(counter
, ctx
);
532 * Don't put the counter on if it is disabled or if
533 * it is in a group and the group isn't on.
535 if (counter
->state
!= PERF_COUNTER_STATE_INACTIVE
||
536 (leader
!= counter
&& leader
->state
!= PERF_COUNTER_STATE_ACTIVE
))
540 * An exclusive counter can't go on if there are already active
541 * hardware counters, and no hardware counter can go on if there
542 * is already an exclusive counter on.
544 if (!group_can_go_on(counter
, cpuctx
, 1))
547 err
= counter_sched_in(counter
, cpuctx
, ctx
, cpu
);
551 * This counter couldn't go on. If it is in a group
552 * then we have to pull the whole group off.
553 * If the counter group is pinned then put it in error state.
555 if (leader
!= counter
)
556 group_sched_out(leader
, cpuctx
, ctx
);
557 if (leader
->hw_event
.pinned
) {
558 update_group_times(leader
);
559 leader
->state
= PERF_COUNTER_STATE_ERROR
;
563 if (!err
&& !ctx
->task
&& cpuctx
->max_pertask
)
564 cpuctx
->max_pertask
--;
567 hw_perf_restore(perf_flags
);
569 spin_unlock_irqrestore(&ctx
->lock
, flags
);
573 * Attach a performance counter to a context
575 * First we add the counter to the list with the hardware enable bit
576 * in counter->hw_config cleared.
578 * If the counter is attached to a task which is on a CPU we use a smp
579 * call to enable it in the task context. The task might have been
580 * scheduled away, but we check this in the smp call again.
582 * Must be called with ctx->mutex held.
585 perf_install_in_context(struct perf_counter_context
*ctx
,
586 struct perf_counter
*counter
,
589 struct task_struct
*task
= ctx
->task
;
593 * Per cpu counters are installed via an smp call and
594 * the install is always sucessful.
596 smp_call_function_single(cpu
, __perf_install_in_context
,
601 counter
->task
= task
;
603 task_oncpu_function_call(task
, __perf_install_in_context
,
606 spin_lock_irq(&ctx
->lock
);
608 * we need to retry the smp call.
610 if (ctx
->is_active
&& list_empty(&counter
->list_entry
)) {
611 spin_unlock_irq(&ctx
->lock
);
616 * The lock prevents that this context is scheduled in so we
617 * can add the counter safely, if it the call above did not
620 if (list_empty(&counter
->list_entry
))
621 add_counter_to_ctx(counter
, ctx
);
622 spin_unlock_irq(&ctx
->lock
);
626 * Cross CPU call to enable a performance counter
628 static void __perf_counter_enable(void *info
)
630 struct perf_counter
*counter
= info
;
631 struct perf_cpu_context
*cpuctx
= &__get_cpu_var(perf_cpu_context
);
632 struct perf_counter_context
*ctx
= counter
->ctx
;
633 struct perf_counter
*leader
= counter
->group_leader
;
638 * If this is a per-task counter, need to check whether this
639 * counter's task is the current task on this cpu.
641 if (ctx
->task
&& cpuctx
->task_ctx
!= ctx
)
644 spin_lock_irqsave(&ctx
->lock
, flags
);
645 update_context_time(ctx
);
647 counter
->prev_state
= counter
->state
;
648 if (counter
->state
>= PERF_COUNTER_STATE_INACTIVE
)
650 counter
->state
= PERF_COUNTER_STATE_INACTIVE
;
651 counter
->tstamp_enabled
= ctx
->time
- counter
->total_time_enabled
;
654 * If the counter is in a group and isn't the group leader,
655 * then don't put it on unless the group is on.
657 if (leader
!= counter
&& leader
->state
!= PERF_COUNTER_STATE_ACTIVE
)
660 if (!group_can_go_on(counter
, cpuctx
, 1))
663 err
= counter_sched_in(counter
, cpuctx
, ctx
,
668 * If this counter can't go on and it's part of a
669 * group, then the whole group has to come off.
671 if (leader
!= counter
)
672 group_sched_out(leader
, cpuctx
, ctx
);
673 if (leader
->hw_event
.pinned
) {
674 update_group_times(leader
);
675 leader
->state
= PERF_COUNTER_STATE_ERROR
;
680 spin_unlock_irqrestore(&ctx
->lock
, flags
);
686 static void perf_counter_enable(struct perf_counter
*counter
)
688 struct perf_counter_context
*ctx
= counter
->ctx
;
689 struct task_struct
*task
= ctx
->task
;
693 * Enable the counter on the cpu that it's on
695 smp_call_function_single(counter
->cpu
, __perf_counter_enable
,
700 spin_lock_irq(&ctx
->lock
);
701 if (counter
->state
>= PERF_COUNTER_STATE_INACTIVE
)
705 * If the counter is in error state, clear that first.
706 * That way, if we see the counter in error state below, we
707 * know that it has gone back into error state, as distinct
708 * from the task having been scheduled away before the
709 * cross-call arrived.
711 if (counter
->state
== PERF_COUNTER_STATE_ERROR
)
712 counter
->state
= PERF_COUNTER_STATE_OFF
;
715 spin_unlock_irq(&ctx
->lock
);
716 task_oncpu_function_call(task
, __perf_counter_enable
, counter
);
718 spin_lock_irq(&ctx
->lock
);
721 * If the context is active and the counter is still off,
722 * we need to retry the cross-call.
724 if (ctx
->is_active
&& counter
->state
== PERF_COUNTER_STATE_OFF
)
728 * Since we have the lock this context can't be scheduled
729 * in, so we can change the state safely.
731 if (counter
->state
== PERF_COUNTER_STATE_OFF
) {
732 counter
->state
= PERF_COUNTER_STATE_INACTIVE
;
733 counter
->tstamp_enabled
=
734 ctx
->time
- counter
->total_time_enabled
;
737 spin_unlock_irq(&ctx
->lock
);
740 static void perf_counter_refresh(struct perf_counter
*counter
, int refresh
)
742 atomic_add(refresh
, &counter
->event_limit
);
743 perf_counter_enable(counter
);
747 * Enable a counter and all its children.
749 static void perf_counter_enable_family(struct perf_counter
*counter
)
751 struct perf_counter
*child
;
753 perf_counter_enable(counter
);
756 * Lock the mutex to protect the list of children
758 mutex_lock(&counter
->mutex
);
759 list_for_each_entry(child
, &counter
->child_list
, child_list
)
760 perf_counter_enable(child
);
761 mutex_unlock(&counter
->mutex
);
764 void __perf_counter_sched_out(struct perf_counter_context
*ctx
,
765 struct perf_cpu_context
*cpuctx
)
767 struct perf_counter
*counter
;
770 spin_lock(&ctx
->lock
);
772 if (likely(!ctx
->nr_counters
))
774 update_context_time(ctx
);
776 flags
= hw_perf_save_disable();
777 if (ctx
->nr_active
) {
778 list_for_each_entry(counter
, &ctx
->counter_list
, list_entry
)
779 group_sched_out(counter
, cpuctx
, ctx
);
781 hw_perf_restore(flags
);
783 spin_unlock(&ctx
->lock
);
787 * Called from scheduler to remove the counters of the current task,
788 * with interrupts disabled.
790 * We stop each counter and update the counter value in counter->count.
792 * This does not protect us against NMI, but disable()
793 * sets the disabled bit in the control field of counter _before_
794 * accessing the counter control register. If a NMI hits, then it will
795 * not restart the counter.
797 void perf_counter_task_sched_out(struct task_struct
*task
, int cpu
)
799 struct perf_cpu_context
*cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
800 struct perf_counter_context
*ctx
= &task
->perf_counter_ctx
;
801 struct pt_regs
*regs
;
803 if (likely(!cpuctx
->task_ctx
))
806 update_context_time(ctx
);
808 regs
= task_pt_regs(task
);
809 perf_swcounter_event(PERF_COUNT_CONTEXT_SWITCHES
, 1, 1, regs
, 0);
810 __perf_counter_sched_out(ctx
, cpuctx
);
812 cpuctx
->task_ctx
= NULL
;
815 static void perf_counter_cpu_sched_out(struct perf_cpu_context
*cpuctx
)
817 __perf_counter_sched_out(&cpuctx
->ctx
, cpuctx
);
821 group_sched_in(struct perf_counter
*group_counter
,
822 struct perf_cpu_context
*cpuctx
,
823 struct perf_counter_context
*ctx
,
826 struct perf_counter
*counter
, *partial_group
;
829 if (group_counter
->state
== PERF_COUNTER_STATE_OFF
)
832 ret
= hw_perf_group_sched_in(group_counter
, cpuctx
, ctx
, cpu
);
834 return ret
< 0 ? ret
: 0;
836 group_counter
->prev_state
= group_counter
->state
;
837 if (counter_sched_in(group_counter
, cpuctx
, ctx
, cpu
))
841 * Schedule in siblings as one group (if any):
843 list_for_each_entry(counter
, &group_counter
->sibling_list
, list_entry
) {
844 counter
->prev_state
= counter
->state
;
845 if (counter_sched_in(counter
, cpuctx
, ctx
, cpu
)) {
846 partial_group
= counter
;
855 * Groups can be scheduled in as one unit only, so undo any
856 * partial group before returning:
858 list_for_each_entry(counter
, &group_counter
->sibling_list
, list_entry
) {
859 if (counter
== partial_group
)
861 counter_sched_out(counter
, cpuctx
, ctx
);
863 counter_sched_out(group_counter
, cpuctx
, ctx
);
869 __perf_counter_sched_in(struct perf_counter_context
*ctx
,
870 struct perf_cpu_context
*cpuctx
, int cpu
)
872 struct perf_counter
*counter
;
876 spin_lock(&ctx
->lock
);
878 if (likely(!ctx
->nr_counters
))
881 ctx
->timestamp
= perf_clock();
883 flags
= hw_perf_save_disable();
886 * First go through the list and put on any pinned groups
887 * in order to give them the best chance of going on.
889 list_for_each_entry(counter
, &ctx
->counter_list
, list_entry
) {
890 if (counter
->state
<= PERF_COUNTER_STATE_OFF
||
891 !counter
->hw_event
.pinned
)
893 if (counter
->cpu
!= -1 && counter
->cpu
!= cpu
)
896 if (group_can_go_on(counter
, cpuctx
, 1))
897 group_sched_in(counter
, cpuctx
, ctx
, cpu
);
900 * If this pinned group hasn't been scheduled,
901 * put it in error state.
903 if (counter
->state
== PERF_COUNTER_STATE_INACTIVE
) {
904 update_group_times(counter
);
905 counter
->state
= PERF_COUNTER_STATE_ERROR
;
909 list_for_each_entry(counter
, &ctx
->counter_list
, list_entry
) {
911 * Ignore counters in OFF or ERROR state, and
912 * ignore pinned counters since we did them already.
914 if (counter
->state
<= PERF_COUNTER_STATE_OFF
||
915 counter
->hw_event
.pinned
)
919 * Listen to the 'cpu' scheduling filter constraint
922 if (counter
->cpu
!= -1 && counter
->cpu
!= cpu
)
925 if (group_can_go_on(counter
, cpuctx
, can_add_hw
)) {
926 if (group_sched_in(counter
, cpuctx
, ctx
, cpu
))
930 hw_perf_restore(flags
);
932 spin_unlock(&ctx
->lock
);
936 * Called from scheduler to add the counters of the current task
937 * with interrupts disabled.
939 * We restore the counter value and then enable it.
941 * This does not protect us against NMI, but enable()
942 * sets the enabled bit in the control field of counter _before_
943 * accessing the counter control register. If a NMI hits, then it will
944 * keep the counter running.
946 void perf_counter_task_sched_in(struct task_struct
*task
, int cpu
)
948 struct perf_cpu_context
*cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
949 struct perf_counter_context
*ctx
= &task
->perf_counter_ctx
;
951 __perf_counter_sched_in(ctx
, cpuctx
, cpu
);
952 cpuctx
->task_ctx
= ctx
;
955 static void perf_counter_cpu_sched_in(struct perf_cpu_context
*cpuctx
, int cpu
)
957 struct perf_counter_context
*ctx
= &cpuctx
->ctx
;
959 __perf_counter_sched_in(ctx
, cpuctx
, cpu
);
962 int perf_counter_task_disable(void)
964 struct task_struct
*curr
= current
;
965 struct perf_counter_context
*ctx
= &curr
->perf_counter_ctx
;
966 struct perf_counter
*counter
;
971 if (likely(!ctx
->nr_counters
))
974 local_irq_save(flags
);
975 cpu
= smp_processor_id();
977 perf_counter_task_sched_out(curr
, cpu
);
979 spin_lock(&ctx
->lock
);
982 * Disable all the counters:
984 perf_flags
= hw_perf_save_disable();
986 list_for_each_entry(counter
, &ctx
->counter_list
, list_entry
) {
987 if (counter
->state
!= PERF_COUNTER_STATE_ERROR
) {
988 update_group_times(counter
);
989 counter
->state
= PERF_COUNTER_STATE_OFF
;
993 hw_perf_restore(perf_flags
);
995 spin_unlock_irqrestore(&ctx
->lock
, flags
);
1000 int perf_counter_task_enable(void)
1002 struct task_struct
*curr
= current
;
1003 struct perf_counter_context
*ctx
= &curr
->perf_counter_ctx
;
1004 struct perf_counter
*counter
;
1005 unsigned long flags
;
1009 if (likely(!ctx
->nr_counters
))
1012 local_irq_save(flags
);
1013 cpu
= smp_processor_id();
1015 perf_counter_task_sched_out(curr
, cpu
);
1017 spin_lock(&ctx
->lock
);
1020 * Disable all the counters:
1022 perf_flags
= hw_perf_save_disable();
1024 list_for_each_entry(counter
, &ctx
->counter_list
, list_entry
) {
1025 if (counter
->state
> PERF_COUNTER_STATE_OFF
)
1027 counter
->state
= PERF_COUNTER_STATE_INACTIVE
;
1028 counter
->tstamp_enabled
=
1029 ctx
->time
- counter
->total_time_enabled
;
1030 counter
->hw_event
.disabled
= 0;
1032 hw_perf_restore(perf_flags
);
1034 spin_unlock(&ctx
->lock
);
1036 perf_counter_task_sched_in(curr
, cpu
);
1038 local_irq_restore(flags
);
1044 * Round-robin a context's counters:
1046 static void rotate_ctx(struct perf_counter_context
*ctx
)
1048 struct perf_counter
*counter
;
1051 if (!ctx
->nr_counters
)
1054 spin_lock(&ctx
->lock
);
1056 * Rotate the first entry last (works just fine for group counters too):
1058 perf_flags
= hw_perf_save_disable();
1059 list_for_each_entry(counter
, &ctx
->counter_list
, list_entry
) {
1060 list_move_tail(&counter
->list_entry
, &ctx
->counter_list
);
1063 hw_perf_restore(perf_flags
);
1065 spin_unlock(&ctx
->lock
);
1068 void perf_counter_task_tick(struct task_struct
*curr
, int cpu
)
1070 struct perf_cpu_context
*cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
1071 struct perf_counter_context
*ctx
= &curr
->perf_counter_ctx
;
1073 perf_counter_cpu_sched_out(cpuctx
);
1074 perf_counter_task_sched_out(curr
, cpu
);
1076 rotate_ctx(&cpuctx
->ctx
);
1079 perf_counter_cpu_sched_in(cpuctx
, cpu
);
1080 perf_counter_task_sched_in(curr
, cpu
);
1084 * Cross CPU call to read the hardware counter
1086 static void __read(void *info
)
1088 struct perf_counter
*counter
= info
;
1089 struct perf_counter_context
*ctx
= counter
->ctx
;
1090 unsigned long flags
;
1092 local_irq_save(flags
);
1094 update_context_time(ctx
);
1095 counter
->pmu
->read(counter
);
1096 update_counter_times(counter
);
1097 local_irq_restore(flags
);
1100 static u64
perf_counter_read(struct perf_counter
*counter
)
1103 * If counter is enabled and currently active on a CPU, update the
1104 * value in the counter structure:
1106 if (counter
->state
== PERF_COUNTER_STATE_ACTIVE
) {
1107 smp_call_function_single(counter
->oncpu
,
1108 __read
, counter
, 1);
1109 } else if (counter
->state
== PERF_COUNTER_STATE_INACTIVE
) {
1110 update_counter_times(counter
);
1113 return atomic64_read(&counter
->count
);
1116 static void put_context(struct perf_counter_context
*ctx
)
1119 put_task_struct(ctx
->task
);
1122 static struct perf_counter_context
*find_get_context(pid_t pid
, int cpu
)
1124 struct perf_cpu_context
*cpuctx
;
1125 struct perf_counter_context
*ctx
;
1126 struct task_struct
*task
;
1129 * If cpu is not a wildcard then this is a percpu counter:
1132 /* Must be root to operate on a CPU counter: */
1133 if (sysctl_perf_counter_priv
&& !capable(CAP_SYS_ADMIN
))
1134 return ERR_PTR(-EACCES
);
1136 if (cpu
< 0 || cpu
> num_possible_cpus())
1137 return ERR_PTR(-EINVAL
);
1140 * We could be clever and allow to attach a counter to an
1141 * offline CPU and activate it when the CPU comes up, but
1144 if (!cpu_isset(cpu
, cpu_online_map
))
1145 return ERR_PTR(-ENODEV
);
1147 cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
1157 task
= find_task_by_vpid(pid
);
1159 get_task_struct(task
);
1163 return ERR_PTR(-ESRCH
);
1165 ctx
= &task
->perf_counter_ctx
;
1168 /* Reuse ptrace permission checks for now. */
1169 if (!ptrace_may_access(task
, PTRACE_MODE_READ
)) {
1171 return ERR_PTR(-EACCES
);
1177 static void free_counter_rcu(struct rcu_head
*head
)
1179 struct perf_counter
*counter
;
1181 counter
= container_of(head
, struct perf_counter
, rcu_head
);
1185 static void perf_pending_sync(struct perf_counter
*counter
);
1187 static void free_counter(struct perf_counter
*counter
)
1189 perf_pending_sync(counter
);
1191 if (counter
->hw_event
.mmap
)
1192 atomic_dec(&nr_mmap_tracking
);
1193 if (counter
->hw_event
.munmap
)
1194 atomic_dec(&nr_munmap_tracking
);
1195 if (counter
->hw_event
.comm
)
1196 atomic_dec(&nr_comm_tracking
);
1198 if (counter
->destroy
)
1199 counter
->destroy(counter
);
1201 call_rcu(&counter
->rcu_head
, free_counter_rcu
);
1205 * Called when the last reference to the file is gone.
1207 static int perf_release(struct inode
*inode
, struct file
*file
)
1209 struct perf_counter
*counter
= file
->private_data
;
1210 struct perf_counter_context
*ctx
= counter
->ctx
;
1212 file
->private_data
= NULL
;
1214 mutex_lock(&ctx
->mutex
);
1215 mutex_lock(&counter
->mutex
);
1217 perf_counter_remove_from_context(counter
);
1219 mutex_unlock(&counter
->mutex
);
1220 mutex_unlock(&ctx
->mutex
);
1222 free_counter(counter
);
1229 * Read the performance counter - simple non blocking version for now
1232 perf_read_hw(struct perf_counter
*counter
, char __user
*buf
, size_t count
)
1238 * Return end-of-file for a read on a counter that is in
1239 * error state (i.e. because it was pinned but it couldn't be
1240 * scheduled on to the CPU at some point).
1242 if (counter
->state
== PERF_COUNTER_STATE_ERROR
)
1245 mutex_lock(&counter
->mutex
);
1246 values
[0] = perf_counter_read(counter
);
1248 if (counter
->hw_event
.read_format
& PERF_FORMAT_TOTAL_TIME_ENABLED
)
1249 values
[n
++] = counter
->total_time_enabled
+
1250 atomic64_read(&counter
->child_total_time_enabled
);
1251 if (counter
->hw_event
.read_format
& PERF_FORMAT_TOTAL_TIME_RUNNING
)
1252 values
[n
++] = counter
->total_time_running
+
1253 atomic64_read(&counter
->child_total_time_running
);
1254 mutex_unlock(&counter
->mutex
);
1256 if (count
< n
* sizeof(u64
))
1258 count
= n
* sizeof(u64
);
1260 if (copy_to_user(buf
, values
, count
))
1267 perf_read(struct file
*file
, char __user
*buf
, size_t count
, loff_t
*ppos
)
1269 struct perf_counter
*counter
= file
->private_data
;
1271 return perf_read_hw(counter
, buf
, count
);
1274 static unsigned int perf_poll(struct file
*file
, poll_table
*wait
)
1276 struct perf_counter
*counter
= file
->private_data
;
1277 struct perf_mmap_data
*data
;
1278 unsigned int events
= POLL_HUP
;
1281 data
= rcu_dereference(counter
->data
);
1283 events
= atomic_xchg(&data
->poll
, 0);
1286 poll_wait(file
, &counter
->waitq
, wait
);
1291 static void perf_counter_reset(struct perf_counter
*counter
)
1293 atomic_set(&counter
->count
, 0);
1296 static long perf_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
1298 struct perf_counter
*counter
= file
->private_data
;
1302 case PERF_COUNTER_IOC_ENABLE
:
1303 perf_counter_enable_family(counter
);
1305 case PERF_COUNTER_IOC_DISABLE
:
1306 perf_counter_disable_family(counter
);
1308 case PERF_COUNTER_IOC_REFRESH
:
1309 perf_counter_refresh(counter
, arg
);
1311 case PERF_COUNTER_IOC_RESET
:
1312 perf_counter_reset(counter
);
1321 * Callers need to ensure there can be no nesting of this function, otherwise
1322 * the seqlock logic goes bad. We can not serialize this because the arch
1323 * code calls this from NMI context.
1325 void perf_counter_update_userpage(struct perf_counter
*counter
)
1327 struct perf_mmap_data
*data
;
1328 struct perf_counter_mmap_page
*userpg
;
1331 data
= rcu_dereference(counter
->data
);
1335 userpg
= data
->user_page
;
1338 * Disable preemption so as to not let the corresponding user-space
1339 * spin too long if we get preempted.
1344 userpg
->index
= counter
->hw
.idx
;
1345 userpg
->offset
= atomic64_read(&counter
->count
);
1346 if (counter
->state
== PERF_COUNTER_STATE_ACTIVE
)
1347 userpg
->offset
-= atomic64_read(&counter
->hw
.prev_count
);
1356 static int perf_mmap_fault(struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
1358 struct perf_counter
*counter
= vma
->vm_file
->private_data
;
1359 struct perf_mmap_data
*data
;
1360 int ret
= VM_FAULT_SIGBUS
;
1363 data
= rcu_dereference(counter
->data
);
1367 if (vmf
->pgoff
== 0) {
1368 vmf
->page
= virt_to_page(data
->user_page
);
1370 int nr
= vmf
->pgoff
- 1;
1372 if ((unsigned)nr
> data
->nr_pages
)
1375 vmf
->page
= virt_to_page(data
->data_pages
[nr
]);
1377 get_page(vmf
->page
);
1385 static int perf_mmap_data_alloc(struct perf_counter
*counter
, int nr_pages
)
1387 struct perf_mmap_data
*data
;
1391 WARN_ON(atomic_read(&counter
->mmap_count
));
1393 size
= sizeof(struct perf_mmap_data
);
1394 size
+= nr_pages
* sizeof(void *);
1396 data
= kzalloc(size
, GFP_KERNEL
);
1400 data
->user_page
= (void *)get_zeroed_page(GFP_KERNEL
);
1401 if (!data
->user_page
)
1402 goto fail_user_page
;
1404 for (i
= 0; i
< nr_pages
; i
++) {
1405 data
->data_pages
[i
] = (void *)get_zeroed_page(GFP_KERNEL
);
1406 if (!data
->data_pages
[i
])
1407 goto fail_data_pages
;
1410 data
->nr_pages
= nr_pages
;
1412 rcu_assign_pointer(counter
->data
, data
);
1417 for (i
--; i
>= 0; i
--)
1418 free_page((unsigned long)data
->data_pages
[i
]);
1420 free_page((unsigned long)data
->user_page
);
1429 static void __perf_mmap_data_free(struct rcu_head
*rcu_head
)
1431 struct perf_mmap_data
*data
= container_of(rcu_head
,
1432 struct perf_mmap_data
, rcu_head
);
1435 free_page((unsigned long)data
->user_page
);
1436 for (i
= 0; i
< data
->nr_pages
; i
++)
1437 free_page((unsigned long)data
->data_pages
[i
]);
1441 static void perf_mmap_data_free(struct perf_counter
*counter
)
1443 struct perf_mmap_data
*data
= counter
->data
;
1445 WARN_ON(atomic_read(&counter
->mmap_count
));
1447 rcu_assign_pointer(counter
->data
, NULL
);
1448 call_rcu(&data
->rcu_head
, __perf_mmap_data_free
);
1451 static void perf_mmap_open(struct vm_area_struct
*vma
)
1453 struct perf_counter
*counter
= vma
->vm_file
->private_data
;
1455 atomic_inc(&counter
->mmap_count
);
1458 static void perf_mmap_close(struct vm_area_struct
*vma
)
1460 struct perf_counter
*counter
= vma
->vm_file
->private_data
;
1462 if (atomic_dec_and_mutex_lock(&counter
->mmap_count
,
1463 &counter
->mmap_mutex
)) {
1464 vma
->vm_mm
->locked_vm
-= counter
->data
->nr_pages
+ 1;
1465 perf_mmap_data_free(counter
);
1466 mutex_unlock(&counter
->mmap_mutex
);
1470 static struct vm_operations_struct perf_mmap_vmops
= {
1471 .open
= perf_mmap_open
,
1472 .close
= perf_mmap_close
,
1473 .fault
= perf_mmap_fault
,
1476 static int perf_mmap(struct file
*file
, struct vm_area_struct
*vma
)
1478 struct perf_counter
*counter
= file
->private_data
;
1479 unsigned long vma_size
;
1480 unsigned long nr_pages
;
1481 unsigned long locked
, lock_limit
;
1484 if (!(vma
->vm_flags
& VM_SHARED
) || (vma
->vm_flags
& VM_WRITE
))
1487 vma_size
= vma
->vm_end
- vma
->vm_start
;
1488 nr_pages
= (vma_size
/ PAGE_SIZE
) - 1;
1491 * If we have data pages ensure they're a power-of-two number, so we
1492 * can do bitmasks instead of modulo.
1494 if (nr_pages
!= 0 && !is_power_of_2(nr_pages
))
1497 if (vma_size
!= PAGE_SIZE
* (1 + nr_pages
))
1500 if (vma
->vm_pgoff
!= 0)
1503 mutex_lock(&counter
->mmap_mutex
);
1504 if (atomic_inc_not_zero(&counter
->mmap_count
)) {
1505 if (nr_pages
!= counter
->data
->nr_pages
)
1510 locked
= vma
->vm_mm
->locked_vm
;
1511 locked
+= nr_pages
+ 1;
1513 lock_limit
= current
->signal
->rlim
[RLIMIT_MEMLOCK
].rlim_cur
;
1514 lock_limit
>>= PAGE_SHIFT
;
1516 if ((locked
> lock_limit
) && !capable(CAP_IPC_LOCK
)) {
1521 WARN_ON(counter
->data
);
1522 ret
= perf_mmap_data_alloc(counter
, nr_pages
);
1526 atomic_set(&counter
->mmap_count
, 1);
1527 vma
->vm_mm
->locked_vm
+= nr_pages
+ 1;
1529 mutex_unlock(&counter
->mmap_mutex
);
1531 vma
->vm_flags
&= ~VM_MAYWRITE
;
1532 vma
->vm_flags
|= VM_RESERVED
;
1533 vma
->vm_ops
= &perf_mmap_vmops
;
1538 static int perf_fasync(int fd
, struct file
*filp
, int on
)
1540 struct perf_counter
*counter
= filp
->private_data
;
1541 struct inode
*inode
= filp
->f_path
.dentry
->d_inode
;
1544 mutex_lock(&inode
->i_mutex
);
1545 retval
= fasync_helper(fd
, filp
, on
, &counter
->fasync
);
1546 mutex_unlock(&inode
->i_mutex
);
1554 static const struct file_operations perf_fops
= {
1555 .release
= perf_release
,
1558 .unlocked_ioctl
= perf_ioctl
,
1559 .compat_ioctl
= perf_ioctl
,
1561 .fasync
= perf_fasync
,
1565 * Perf counter wakeup
1567 * If there's data, ensure we set the poll() state and publish everything
1568 * to user-space before waking everybody up.
1571 void perf_counter_wakeup(struct perf_counter
*counter
)
1573 wake_up_all(&counter
->waitq
);
1575 if (counter
->pending_kill
) {
1576 kill_fasync(&counter
->fasync
, SIGIO
, counter
->pending_kill
);
1577 counter
->pending_kill
= 0;
1584 * Handle the case where we need to wakeup up from NMI (or rq->lock) context.
1586 * The NMI bit means we cannot possibly take locks. Therefore, maintain a
1587 * single linked list and use cmpxchg() to add entries lockless.
1590 static void perf_pending_counter(struct perf_pending_entry
*entry
)
1592 struct perf_counter
*counter
= container_of(entry
,
1593 struct perf_counter
, pending
);
1595 if (counter
->pending_disable
) {
1596 counter
->pending_disable
= 0;
1597 perf_counter_disable(counter
);
1600 if (counter
->pending_wakeup
) {
1601 counter
->pending_wakeup
= 0;
1602 perf_counter_wakeup(counter
);
1606 #define PENDING_TAIL ((struct perf_pending_entry *)-1UL)
1608 static DEFINE_PER_CPU(struct perf_pending_entry
*, perf_pending_head
) = {
1612 static void perf_pending_queue(struct perf_pending_entry
*entry
,
1613 void (*func
)(struct perf_pending_entry
*))
1615 struct perf_pending_entry
**head
;
1617 if (cmpxchg(&entry
->next
, NULL
, PENDING_TAIL
) != NULL
)
1622 head
= &get_cpu_var(perf_pending_head
);
1625 entry
->next
= *head
;
1626 } while (cmpxchg(head
, entry
->next
, entry
) != entry
->next
);
1628 set_perf_counter_pending();
1630 put_cpu_var(perf_pending_head
);
1633 static int __perf_pending_run(void)
1635 struct perf_pending_entry
*list
;
1638 list
= xchg(&__get_cpu_var(perf_pending_head
), PENDING_TAIL
);
1639 while (list
!= PENDING_TAIL
) {
1640 void (*func
)(struct perf_pending_entry
*);
1641 struct perf_pending_entry
*entry
= list
;
1648 * Ensure we observe the unqueue before we issue the wakeup,
1649 * so that we won't be waiting forever.
1650 * -- see perf_not_pending().
1661 static inline int perf_not_pending(struct perf_counter
*counter
)
1664 * If we flush on whatever cpu we run, there is a chance we don't
1668 __perf_pending_run();
1672 * Ensure we see the proper queue state before going to sleep
1673 * so that we do not miss the wakeup. -- see perf_pending_handle()
1676 return counter
->pending
.next
== NULL
;
1679 static void perf_pending_sync(struct perf_counter
*counter
)
1681 wait_event(counter
->waitq
, perf_not_pending(counter
));
1684 void perf_counter_do_pending(void)
1686 __perf_pending_run();
1690 * Callchain support -- arch specific
1693 __weak
struct perf_callchain_entry
*perf_callchain(struct pt_regs
*regs
)
1702 struct perf_output_handle
{
1703 struct perf_counter
*counter
;
1704 struct perf_mmap_data
*data
;
1705 unsigned int offset
;
1710 unsigned long flags
;
1713 static void perf_output_wakeup(struct perf_output_handle
*handle
)
1715 atomic_set(&handle
->data
->poll
, POLL_IN
);
1718 handle
->counter
->pending_wakeup
= 1;
1719 perf_pending_queue(&handle
->counter
->pending
,
1720 perf_pending_counter
);
1722 perf_counter_wakeup(handle
->counter
);
1726 * Curious locking construct.
1728 * We need to ensure a later event doesn't publish a head when a former
1729 * event isn't done writing. However since we need to deal with NMIs we
1730 * cannot fully serialize things.
1732 * What we do is serialize between CPUs so we only have to deal with NMI
1733 * nesting on a single CPU.
1735 * We only publish the head (and generate a wakeup) when the outer-most
1738 static void perf_output_lock(struct perf_output_handle
*handle
)
1740 struct perf_mmap_data
*data
= handle
->data
;
1745 local_irq_save(handle
->flags
);
1746 cpu
= smp_processor_id();
1748 if (in_nmi() && atomic_read(&data
->lock
) == cpu
)
1751 while (atomic_cmpxchg(&data
->lock
, 0, cpu
) != 0)
1757 static void perf_output_unlock(struct perf_output_handle
*handle
)
1759 struct perf_mmap_data
*data
= handle
->data
;
1762 data
->done_head
= data
->head
;
1764 if (!handle
->locked
)
1769 * The xchg implies a full barrier that ensures all writes are done
1770 * before we publish the new head, matched by a rmb() in userspace when
1771 * reading this position.
1773 while ((head
= atomic_xchg(&data
->done_head
, 0)))
1774 data
->user_page
->data_head
= head
;
1777 * NMI can happen here, which means we can miss a done_head update.
1780 cpu
= atomic_xchg(&data
->lock
, 0);
1781 WARN_ON_ONCE(cpu
!= smp_processor_id());
1784 * Therefore we have to validate we did not indeed do so.
1786 if (unlikely(atomic_read(&data
->done_head
))) {
1788 * Since we had it locked, we can lock it again.
1790 while (atomic_cmpxchg(&data
->lock
, 0, cpu
) != 0)
1796 if (atomic_xchg(&data
->wakeup
, 0))
1797 perf_output_wakeup(handle
);
1799 local_irq_restore(handle
->flags
);
1802 static int perf_output_begin(struct perf_output_handle
*handle
,
1803 struct perf_counter
*counter
, unsigned int size
,
1804 int nmi
, int overflow
)
1806 struct perf_mmap_data
*data
;
1807 unsigned int offset
, head
;
1810 data
= rcu_dereference(counter
->data
);
1814 handle
->data
= data
;
1815 handle
->counter
= counter
;
1817 handle
->overflow
= overflow
;
1819 if (!data
->nr_pages
)
1822 perf_output_lock(handle
);
1825 offset
= head
= atomic_read(&data
->head
);
1827 } while (atomic_cmpxchg(&data
->head
, offset
, head
) != offset
);
1829 handle
->offset
= offset
;
1830 handle
->head
= head
;
1832 if ((offset
>> PAGE_SHIFT
) != (head
>> PAGE_SHIFT
))
1833 atomic_set(&data
->wakeup
, 1);
1838 perf_output_wakeup(handle
);
1845 static void perf_output_copy(struct perf_output_handle
*handle
,
1846 void *buf
, unsigned int len
)
1848 unsigned int pages_mask
;
1849 unsigned int offset
;
1853 offset
= handle
->offset
;
1854 pages_mask
= handle
->data
->nr_pages
- 1;
1855 pages
= handle
->data
->data_pages
;
1858 unsigned int page_offset
;
1861 nr
= (offset
>> PAGE_SHIFT
) & pages_mask
;
1862 page_offset
= offset
& (PAGE_SIZE
- 1);
1863 size
= min_t(unsigned int, PAGE_SIZE
- page_offset
, len
);
1865 memcpy(pages
[nr
] + page_offset
, buf
, size
);
1872 handle
->offset
= offset
;
1874 WARN_ON_ONCE(handle
->offset
> handle
->head
);
1877 #define perf_output_put(handle, x) \
1878 perf_output_copy((handle), &(x), sizeof(x))
1880 static void perf_output_end(struct perf_output_handle
*handle
)
1882 struct perf_counter
*counter
= handle
->counter
;
1883 struct perf_mmap_data
*data
= handle
->data
;
1885 int wakeup_events
= counter
->hw_event
.wakeup_events
;
1887 if (handle
->overflow
&& wakeup_events
) {
1888 int events
= atomic_inc_return(&data
->events
);
1889 if (events
>= wakeup_events
) {
1890 atomic_sub(wakeup_events
, &data
->events
);
1891 atomic_set(&data
->wakeup
, 1);
1895 perf_output_unlock(handle
);
1899 static void perf_counter_output(struct perf_counter
*counter
,
1900 int nmi
, struct pt_regs
*regs
, u64 addr
)
1903 u64 record_type
= counter
->hw_event
.record_type
;
1904 struct perf_output_handle handle
;
1905 struct perf_event_header header
;
1914 struct perf_callchain_entry
*callchain
= NULL
;
1915 int callchain_size
= 0;
1919 header
.size
= sizeof(header
);
1921 header
.misc
= PERF_EVENT_MISC_OVERFLOW
;
1922 header
.misc
|= user_mode(regs
) ?
1923 PERF_EVENT_MISC_USER
: PERF_EVENT_MISC_KERNEL
;
1925 if (record_type
& PERF_RECORD_IP
) {
1926 ip
= instruction_pointer(regs
);
1927 header
.type
|= PERF_RECORD_IP
;
1928 header
.size
+= sizeof(ip
);
1931 if (record_type
& PERF_RECORD_TID
) {
1932 /* namespace issues */
1933 tid_entry
.pid
= current
->group_leader
->pid
;
1934 tid_entry
.tid
= current
->pid
;
1936 header
.type
|= PERF_RECORD_TID
;
1937 header
.size
+= sizeof(tid_entry
);
1940 if (record_type
& PERF_RECORD_TIME
) {
1942 * Maybe do better on x86 and provide cpu_clock_nmi()
1944 time
= sched_clock();
1946 header
.type
|= PERF_RECORD_TIME
;
1947 header
.size
+= sizeof(u64
);
1950 if (record_type
& PERF_RECORD_ADDR
) {
1951 header
.type
|= PERF_RECORD_ADDR
;
1952 header
.size
+= sizeof(u64
);
1955 if (record_type
& PERF_RECORD_GROUP
) {
1956 header
.type
|= PERF_RECORD_GROUP
;
1957 header
.size
+= sizeof(u64
) +
1958 counter
->nr_siblings
* sizeof(group_entry
);
1961 if (record_type
& PERF_RECORD_CALLCHAIN
) {
1962 callchain
= perf_callchain(regs
);
1965 callchain_size
= (1 + callchain
->nr
) * sizeof(u64
);
1967 header
.type
|= PERF_RECORD_CALLCHAIN
;
1968 header
.size
+= callchain_size
;
1972 ret
= perf_output_begin(&handle
, counter
, header
.size
, nmi
, 1);
1976 perf_output_put(&handle
, header
);
1978 if (record_type
& PERF_RECORD_IP
)
1979 perf_output_put(&handle
, ip
);
1981 if (record_type
& PERF_RECORD_TID
)
1982 perf_output_put(&handle
, tid_entry
);
1984 if (record_type
& PERF_RECORD_TIME
)
1985 perf_output_put(&handle
, time
);
1987 if (record_type
& PERF_RECORD_ADDR
)
1988 perf_output_put(&handle
, addr
);
1990 if (record_type
& PERF_RECORD_GROUP
) {
1991 struct perf_counter
*leader
, *sub
;
1992 u64 nr
= counter
->nr_siblings
;
1994 perf_output_put(&handle
, nr
);
1996 leader
= counter
->group_leader
;
1997 list_for_each_entry(sub
, &leader
->sibling_list
, list_entry
) {
1999 sub
->pmu
->read(sub
);
2001 group_entry
.event
= sub
->hw_event
.config
;
2002 group_entry
.counter
= atomic64_read(&sub
->count
);
2004 perf_output_put(&handle
, group_entry
);
2009 perf_output_copy(&handle
, callchain
, callchain_size
);
2011 perf_output_end(&handle
);
2018 struct perf_comm_event
{
2019 struct task_struct
*task
;
2024 struct perf_event_header header
;
2031 static void perf_counter_comm_output(struct perf_counter
*counter
,
2032 struct perf_comm_event
*comm_event
)
2034 struct perf_output_handle handle
;
2035 int size
= comm_event
->event
.header
.size
;
2036 int ret
= perf_output_begin(&handle
, counter
, size
, 0, 0);
2041 perf_output_put(&handle
, comm_event
->event
);
2042 perf_output_copy(&handle
, comm_event
->comm
,
2043 comm_event
->comm_size
);
2044 perf_output_end(&handle
);
2047 static int perf_counter_comm_match(struct perf_counter
*counter
,
2048 struct perf_comm_event
*comm_event
)
2050 if (counter
->hw_event
.comm
&&
2051 comm_event
->event
.header
.type
== PERF_EVENT_COMM
)
2057 static void perf_counter_comm_ctx(struct perf_counter_context
*ctx
,
2058 struct perf_comm_event
*comm_event
)
2060 struct perf_counter
*counter
;
2062 if (system_state
!= SYSTEM_RUNNING
|| list_empty(&ctx
->event_list
))
2066 list_for_each_entry_rcu(counter
, &ctx
->event_list
, event_entry
) {
2067 if (perf_counter_comm_match(counter
, comm_event
))
2068 perf_counter_comm_output(counter
, comm_event
);
2073 static void perf_counter_comm_event(struct perf_comm_event
*comm_event
)
2075 struct perf_cpu_context
*cpuctx
;
2077 char *comm
= comm_event
->task
->comm
;
2079 size
= ALIGN(strlen(comm
)+1, sizeof(u64
));
2081 comm_event
->comm
= comm
;
2082 comm_event
->comm_size
= size
;
2084 comm_event
->event
.header
.size
= sizeof(comm_event
->event
) + size
;
2086 cpuctx
= &get_cpu_var(perf_cpu_context
);
2087 perf_counter_comm_ctx(&cpuctx
->ctx
, comm_event
);
2088 put_cpu_var(perf_cpu_context
);
2090 perf_counter_comm_ctx(¤t
->perf_counter_ctx
, comm_event
);
2093 void perf_counter_comm(struct task_struct
*task
)
2095 struct perf_comm_event comm_event
;
2097 if (!atomic_read(&nr_comm_tracking
))
2100 comm_event
= (struct perf_comm_event
){
2103 .header
= { .type
= PERF_EVENT_COMM
, },
2104 .pid
= task
->group_leader
->pid
,
2109 perf_counter_comm_event(&comm_event
);
2116 struct perf_mmap_event
{
2122 struct perf_event_header header
;
2132 static void perf_counter_mmap_output(struct perf_counter
*counter
,
2133 struct perf_mmap_event
*mmap_event
)
2135 struct perf_output_handle handle
;
2136 int size
= mmap_event
->event
.header
.size
;
2137 int ret
= perf_output_begin(&handle
, counter
, size
, 0, 0);
2142 perf_output_put(&handle
, mmap_event
->event
);
2143 perf_output_copy(&handle
, mmap_event
->file_name
,
2144 mmap_event
->file_size
);
2145 perf_output_end(&handle
);
2148 static int perf_counter_mmap_match(struct perf_counter
*counter
,
2149 struct perf_mmap_event
*mmap_event
)
2151 if (counter
->hw_event
.mmap
&&
2152 mmap_event
->event
.header
.type
== PERF_EVENT_MMAP
)
2155 if (counter
->hw_event
.munmap
&&
2156 mmap_event
->event
.header
.type
== PERF_EVENT_MUNMAP
)
2162 static void perf_counter_mmap_ctx(struct perf_counter_context
*ctx
,
2163 struct perf_mmap_event
*mmap_event
)
2165 struct perf_counter
*counter
;
2167 if (system_state
!= SYSTEM_RUNNING
|| list_empty(&ctx
->event_list
))
2171 list_for_each_entry_rcu(counter
, &ctx
->event_list
, event_entry
) {
2172 if (perf_counter_mmap_match(counter
, mmap_event
))
2173 perf_counter_mmap_output(counter
, mmap_event
);
2178 static void perf_counter_mmap_event(struct perf_mmap_event
*mmap_event
)
2180 struct perf_cpu_context
*cpuctx
;
2181 struct file
*file
= mmap_event
->file
;
2188 buf
= kzalloc(PATH_MAX
, GFP_KERNEL
);
2190 name
= strncpy(tmp
, "//enomem", sizeof(tmp
));
2193 name
= d_path(&file
->f_path
, buf
, PATH_MAX
);
2195 name
= strncpy(tmp
, "//toolong", sizeof(tmp
));
2199 name
= strncpy(tmp
, "//anon", sizeof(tmp
));
2204 size
= ALIGN(strlen(name
)+1, sizeof(u64
));
2206 mmap_event
->file_name
= name
;
2207 mmap_event
->file_size
= size
;
2209 mmap_event
->event
.header
.size
= sizeof(mmap_event
->event
) + size
;
2211 cpuctx
= &get_cpu_var(perf_cpu_context
);
2212 perf_counter_mmap_ctx(&cpuctx
->ctx
, mmap_event
);
2213 put_cpu_var(perf_cpu_context
);
2215 perf_counter_mmap_ctx(¤t
->perf_counter_ctx
, mmap_event
);
2220 void perf_counter_mmap(unsigned long addr
, unsigned long len
,
2221 unsigned long pgoff
, struct file
*file
)
2223 struct perf_mmap_event mmap_event
;
2225 if (!atomic_read(&nr_mmap_tracking
))
2228 mmap_event
= (struct perf_mmap_event
){
2231 .header
= { .type
= PERF_EVENT_MMAP
, },
2232 .pid
= current
->group_leader
->pid
,
2233 .tid
= current
->pid
,
2240 perf_counter_mmap_event(&mmap_event
);
2243 void perf_counter_munmap(unsigned long addr
, unsigned long len
,
2244 unsigned long pgoff
, struct file
*file
)
2246 struct perf_mmap_event mmap_event
;
2248 if (!atomic_read(&nr_munmap_tracking
))
2251 mmap_event
= (struct perf_mmap_event
){
2254 .header
= { .type
= PERF_EVENT_MUNMAP
, },
2255 .pid
= current
->group_leader
->pid
,
2256 .tid
= current
->pid
,
2263 perf_counter_mmap_event(&mmap_event
);
2267 * Generic counter overflow handling.
2270 int perf_counter_overflow(struct perf_counter
*counter
,
2271 int nmi
, struct pt_regs
*regs
, u64 addr
)
2273 int events
= atomic_read(&counter
->event_limit
);
2276 counter
->pending_kill
= POLL_IN
;
2277 if (events
&& atomic_dec_and_test(&counter
->event_limit
)) {
2279 counter
->pending_kill
= POLL_HUP
;
2281 counter
->pending_disable
= 1;
2282 perf_pending_queue(&counter
->pending
,
2283 perf_pending_counter
);
2285 perf_counter_disable(counter
);
2288 perf_counter_output(counter
, nmi
, regs
, addr
);
2293 * Generic software counter infrastructure
2296 static void perf_swcounter_update(struct perf_counter
*counter
)
2298 struct hw_perf_counter
*hwc
= &counter
->hw
;
2303 prev
= atomic64_read(&hwc
->prev_count
);
2304 now
= atomic64_read(&hwc
->count
);
2305 if (atomic64_cmpxchg(&hwc
->prev_count
, prev
, now
) != prev
)
2310 atomic64_add(delta
, &counter
->count
);
2311 atomic64_sub(delta
, &hwc
->period_left
);
2314 static void perf_swcounter_set_period(struct perf_counter
*counter
)
2316 struct hw_perf_counter
*hwc
= &counter
->hw
;
2317 s64 left
= atomic64_read(&hwc
->period_left
);
2318 s64 period
= hwc
->irq_period
;
2320 if (unlikely(left
<= -period
)) {
2322 atomic64_set(&hwc
->period_left
, left
);
2325 if (unlikely(left
<= 0)) {
2327 atomic64_add(period
, &hwc
->period_left
);
2330 atomic64_set(&hwc
->prev_count
, -left
);
2331 atomic64_set(&hwc
->count
, -left
);
2334 static enum hrtimer_restart
perf_swcounter_hrtimer(struct hrtimer
*hrtimer
)
2336 enum hrtimer_restart ret
= HRTIMER_RESTART
;
2337 struct perf_counter
*counter
;
2338 struct pt_regs
*regs
;
2340 counter
= container_of(hrtimer
, struct perf_counter
, hw
.hrtimer
);
2341 counter
->pmu
->read(counter
);
2343 regs
= get_irq_regs();
2345 * In case we exclude kernel IPs or are somehow not in interrupt
2346 * context, provide the next best thing, the user IP.
2348 if ((counter
->hw_event
.exclude_kernel
|| !regs
) &&
2349 !counter
->hw_event
.exclude_user
)
2350 regs
= task_pt_regs(current
);
2353 if (perf_counter_overflow(counter
, 0, regs
, 0))
2354 ret
= HRTIMER_NORESTART
;
2357 hrtimer_forward_now(hrtimer
, ns_to_ktime(counter
->hw
.irq_period
));
2362 static void perf_swcounter_overflow(struct perf_counter
*counter
,
2363 int nmi
, struct pt_regs
*regs
, u64 addr
)
2365 perf_swcounter_update(counter
);
2366 perf_swcounter_set_period(counter
);
2367 if (perf_counter_overflow(counter
, nmi
, regs
, addr
))
2368 /* soft-disable the counter */
2373 static int perf_swcounter_match(struct perf_counter
*counter
,
2374 enum perf_event_types type
,
2375 u32 event
, struct pt_regs
*regs
)
2377 if (counter
->state
!= PERF_COUNTER_STATE_ACTIVE
)
2380 if (perf_event_raw(&counter
->hw_event
))
2383 if (perf_event_type(&counter
->hw_event
) != type
)
2386 if (perf_event_id(&counter
->hw_event
) != event
)
2389 if (counter
->hw_event
.exclude_user
&& user_mode(regs
))
2392 if (counter
->hw_event
.exclude_kernel
&& !user_mode(regs
))
2398 static void perf_swcounter_add(struct perf_counter
*counter
, u64 nr
,
2399 int nmi
, struct pt_regs
*regs
, u64 addr
)
2401 int neg
= atomic64_add_negative(nr
, &counter
->hw
.count
);
2402 if (counter
->hw
.irq_period
&& !neg
)
2403 perf_swcounter_overflow(counter
, nmi
, regs
, addr
);
2406 static void perf_swcounter_ctx_event(struct perf_counter_context
*ctx
,
2407 enum perf_event_types type
, u32 event
,
2408 u64 nr
, int nmi
, struct pt_regs
*regs
,
2411 struct perf_counter
*counter
;
2413 if (system_state
!= SYSTEM_RUNNING
|| list_empty(&ctx
->event_list
))
2417 list_for_each_entry_rcu(counter
, &ctx
->event_list
, event_entry
) {
2418 if (perf_swcounter_match(counter
, type
, event
, regs
))
2419 perf_swcounter_add(counter
, nr
, nmi
, regs
, addr
);
2424 static int *perf_swcounter_recursion_context(struct perf_cpu_context
*cpuctx
)
2427 return &cpuctx
->recursion
[3];
2430 return &cpuctx
->recursion
[2];
2433 return &cpuctx
->recursion
[1];
2435 return &cpuctx
->recursion
[0];
2438 static void __perf_swcounter_event(enum perf_event_types type
, u32 event
,
2439 u64 nr
, int nmi
, struct pt_regs
*regs
,
2442 struct perf_cpu_context
*cpuctx
= &get_cpu_var(perf_cpu_context
);
2443 int *recursion
= perf_swcounter_recursion_context(cpuctx
);
2451 perf_swcounter_ctx_event(&cpuctx
->ctx
, type
, event
,
2452 nr
, nmi
, regs
, addr
);
2453 if (cpuctx
->task_ctx
) {
2454 perf_swcounter_ctx_event(cpuctx
->task_ctx
, type
, event
,
2455 nr
, nmi
, regs
, addr
);
2462 put_cpu_var(perf_cpu_context
);
2466 perf_swcounter_event(u32 event
, u64 nr
, int nmi
, struct pt_regs
*regs
, u64 addr
)
2468 __perf_swcounter_event(PERF_TYPE_SOFTWARE
, event
, nr
, nmi
, regs
, addr
);
2471 static void perf_swcounter_read(struct perf_counter
*counter
)
2473 perf_swcounter_update(counter
);
2476 static int perf_swcounter_enable(struct perf_counter
*counter
)
2478 perf_swcounter_set_period(counter
);
2482 static void perf_swcounter_disable(struct perf_counter
*counter
)
2484 perf_swcounter_update(counter
);
2487 static const struct pmu perf_ops_generic
= {
2488 .enable
= perf_swcounter_enable
,
2489 .disable
= perf_swcounter_disable
,
2490 .read
= perf_swcounter_read
,
2494 * Software counter: cpu wall time clock
2497 static void cpu_clock_perf_counter_update(struct perf_counter
*counter
)
2499 int cpu
= raw_smp_processor_id();
2503 now
= cpu_clock(cpu
);
2504 prev
= atomic64_read(&counter
->hw
.prev_count
);
2505 atomic64_set(&counter
->hw
.prev_count
, now
);
2506 atomic64_add(now
- prev
, &counter
->count
);
2509 static int cpu_clock_perf_counter_enable(struct perf_counter
*counter
)
2511 struct hw_perf_counter
*hwc
= &counter
->hw
;
2512 int cpu
= raw_smp_processor_id();
2514 atomic64_set(&hwc
->prev_count
, cpu_clock(cpu
));
2515 hrtimer_init(&hwc
->hrtimer
, CLOCK_MONOTONIC
, HRTIMER_MODE_REL
);
2516 hwc
->hrtimer
.function
= perf_swcounter_hrtimer
;
2517 if (hwc
->irq_period
) {
2518 __hrtimer_start_range_ns(&hwc
->hrtimer
,
2519 ns_to_ktime(hwc
->irq_period
), 0,
2520 HRTIMER_MODE_REL
, 0);
2526 static void cpu_clock_perf_counter_disable(struct perf_counter
*counter
)
2528 hrtimer_cancel(&counter
->hw
.hrtimer
);
2529 cpu_clock_perf_counter_update(counter
);
2532 static void cpu_clock_perf_counter_read(struct perf_counter
*counter
)
2534 cpu_clock_perf_counter_update(counter
);
2537 static const struct pmu perf_ops_cpu_clock
= {
2538 .enable
= cpu_clock_perf_counter_enable
,
2539 .disable
= cpu_clock_perf_counter_disable
,
2540 .read
= cpu_clock_perf_counter_read
,
2544 * Software counter: task time clock
2547 static void task_clock_perf_counter_update(struct perf_counter
*counter
, u64 now
)
2552 prev
= atomic64_xchg(&counter
->hw
.prev_count
, now
);
2554 atomic64_add(delta
, &counter
->count
);
2557 static int task_clock_perf_counter_enable(struct perf_counter
*counter
)
2559 struct hw_perf_counter
*hwc
= &counter
->hw
;
2562 now
= counter
->ctx
->time
;
2564 atomic64_set(&hwc
->prev_count
, now
);
2565 hrtimer_init(&hwc
->hrtimer
, CLOCK_MONOTONIC
, HRTIMER_MODE_REL
);
2566 hwc
->hrtimer
.function
= perf_swcounter_hrtimer
;
2567 if (hwc
->irq_period
) {
2568 __hrtimer_start_range_ns(&hwc
->hrtimer
,
2569 ns_to_ktime(hwc
->irq_period
), 0,
2570 HRTIMER_MODE_REL
, 0);
2576 static void task_clock_perf_counter_disable(struct perf_counter
*counter
)
2578 hrtimer_cancel(&counter
->hw
.hrtimer
);
2579 task_clock_perf_counter_update(counter
, counter
->ctx
->time
);
2583 static void task_clock_perf_counter_read(struct perf_counter
*counter
)
2588 update_context_time(counter
->ctx
);
2589 time
= counter
->ctx
->time
;
2591 u64 now
= perf_clock();
2592 u64 delta
= now
- counter
->ctx
->timestamp
;
2593 time
= counter
->ctx
->time
+ delta
;
2596 task_clock_perf_counter_update(counter
, time
);
2599 static const struct pmu perf_ops_task_clock
= {
2600 .enable
= task_clock_perf_counter_enable
,
2601 .disable
= task_clock_perf_counter_disable
,
2602 .read
= task_clock_perf_counter_read
,
2606 * Software counter: cpu migrations
2609 static inline u64
get_cpu_migrations(struct perf_counter
*counter
)
2611 struct task_struct
*curr
= counter
->ctx
->task
;
2614 return curr
->se
.nr_migrations
;
2615 return cpu_nr_migrations(smp_processor_id());
2618 static void cpu_migrations_perf_counter_update(struct perf_counter
*counter
)
2623 prev
= atomic64_read(&counter
->hw
.prev_count
);
2624 now
= get_cpu_migrations(counter
);
2626 atomic64_set(&counter
->hw
.prev_count
, now
);
2630 atomic64_add(delta
, &counter
->count
);
2633 static void cpu_migrations_perf_counter_read(struct perf_counter
*counter
)
2635 cpu_migrations_perf_counter_update(counter
);
2638 static int cpu_migrations_perf_counter_enable(struct perf_counter
*counter
)
2640 if (counter
->prev_state
<= PERF_COUNTER_STATE_OFF
)
2641 atomic64_set(&counter
->hw
.prev_count
,
2642 get_cpu_migrations(counter
));
2646 static void cpu_migrations_perf_counter_disable(struct perf_counter
*counter
)
2648 cpu_migrations_perf_counter_update(counter
);
2651 static const struct pmu perf_ops_cpu_migrations
= {
2652 .enable
= cpu_migrations_perf_counter_enable
,
2653 .disable
= cpu_migrations_perf_counter_disable
,
2654 .read
= cpu_migrations_perf_counter_read
,
2657 #ifdef CONFIG_EVENT_PROFILE
2658 void perf_tpcounter_event(int event_id
)
2660 struct pt_regs
*regs
= get_irq_regs();
2663 regs
= task_pt_regs(current
);
2665 __perf_swcounter_event(PERF_TYPE_TRACEPOINT
, event_id
, 1, 1, regs
, 0);
2667 EXPORT_SYMBOL_GPL(perf_tpcounter_event
);
2669 extern int ftrace_profile_enable(int);
2670 extern void ftrace_profile_disable(int);
2672 static void tp_perf_counter_destroy(struct perf_counter
*counter
)
2674 ftrace_profile_disable(perf_event_id(&counter
->hw_event
));
2677 static const struct pmu
*tp_perf_counter_init(struct perf_counter
*counter
)
2679 int event_id
= perf_event_id(&counter
->hw_event
);
2682 ret
= ftrace_profile_enable(event_id
);
2686 counter
->destroy
= tp_perf_counter_destroy
;
2687 counter
->hw
.irq_period
= counter
->hw_event
.irq_period
;
2689 return &perf_ops_generic
;
2692 static const struct pmu
*tp_perf_counter_init(struct perf_counter
*counter
)
2698 static const struct pmu
*sw_perf_counter_init(struct perf_counter
*counter
)
2700 struct perf_counter_hw_event
*hw_event
= &counter
->hw_event
;
2701 const struct pmu
*pmu
= NULL
;
2702 struct hw_perf_counter
*hwc
= &counter
->hw
;
2705 * Software counters (currently) can't in general distinguish
2706 * between user, kernel and hypervisor events.
2707 * However, context switches and cpu migrations are considered
2708 * to be kernel events, and page faults are never hypervisor
2711 switch (perf_event_id(&counter
->hw_event
)) {
2712 case PERF_COUNT_CPU_CLOCK
:
2713 pmu
= &perf_ops_cpu_clock
;
2715 if (hw_event
->irq_period
&& hw_event
->irq_period
< 10000)
2716 hw_event
->irq_period
= 10000;
2718 case PERF_COUNT_TASK_CLOCK
:
2720 * If the user instantiates this as a per-cpu counter,
2721 * use the cpu_clock counter instead.
2723 if (counter
->ctx
->task
)
2724 pmu
= &perf_ops_task_clock
;
2726 pmu
= &perf_ops_cpu_clock
;
2728 if (hw_event
->irq_period
&& hw_event
->irq_period
< 10000)
2729 hw_event
->irq_period
= 10000;
2731 case PERF_COUNT_PAGE_FAULTS
:
2732 case PERF_COUNT_PAGE_FAULTS_MIN
:
2733 case PERF_COUNT_PAGE_FAULTS_MAJ
:
2734 case PERF_COUNT_CONTEXT_SWITCHES
:
2735 pmu
= &perf_ops_generic
;
2737 case PERF_COUNT_CPU_MIGRATIONS
:
2738 if (!counter
->hw_event
.exclude_kernel
)
2739 pmu
= &perf_ops_cpu_migrations
;
2744 hwc
->irq_period
= hw_event
->irq_period
;
2750 * Allocate and initialize a counter structure
2752 static struct perf_counter
*
2753 perf_counter_alloc(struct perf_counter_hw_event
*hw_event
,
2755 struct perf_counter_context
*ctx
,
2756 struct perf_counter
*group_leader
,
2759 const struct pmu
*pmu
;
2760 struct perf_counter
*counter
;
2763 counter
= kzalloc(sizeof(*counter
), gfpflags
);
2765 return ERR_PTR(-ENOMEM
);
2768 * Single counters are their own group leaders, with an
2769 * empty sibling list:
2772 group_leader
= counter
;
2774 mutex_init(&counter
->mutex
);
2775 INIT_LIST_HEAD(&counter
->list_entry
);
2776 INIT_LIST_HEAD(&counter
->event_entry
);
2777 INIT_LIST_HEAD(&counter
->sibling_list
);
2778 init_waitqueue_head(&counter
->waitq
);
2780 mutex_init(&counter
->mmap_mutex
);
2782 INIT_LIST_HEAD(&counter
->child_list
);
2785 counter
->hw_event
= *hw_event
;
2786 counter
->group_leader
= group_leader
;
2787 counter
->pmu
= NULL
;
2790 counter
->state
= PERF_COUNTER_STATE_INACTIVE
;
2791 if (hw_event
->disabled
)
2792 counter
->state
= PERF_COUNTER_STATE_OFF
;
2796 if (perf_event_raw(hw_event
)) {
2797 pmu
= hw_perf_counter_init(counter
);
2801 switch (perf_event_type(hw_event
)) {
2802 case PERF_TYPE_HARDWARE
:
2803 pmu
= hw_perf_counter_init(counter
);
2806 case PERF_TYPE_SOFTWARE
:
2807 pmu
= sw_perf_counter_init(counter
);
2810 case PERF_TYPE_TRACEPOINT
:
2811 pmu
= tp_perf_counter_init(counter
);
2818 else if (IS_ERR(pmu
))
2823 return ERR_PTR(err
);
2828 if (counter
->hw_event
.mmap
)
2829 atomic_inc(&nr_mmap_tracking
);
2830 if (counter
->hw_event
.munmap
)
2831 atomic_inc(&nr_munmap_tracking
);
2832 if (counter
->hw_event
.comm
)
2833 atomic_inc(&nr_comm_tracking
);
2839 * sys_perf_counter_open - open a performance counter, associate it to a task/cpu
2841 * @hw_event_uptr: event type attributes for monitoring/sampling
2844 * @group_fd: group leader counter fd
2846 SYSCALL_DEFINE5(perf_counter_open
,
2847 const struct perf_counter_hw_event __user
*, hw_event_uptr
,
2848 pid_t
, pid
, int, cpu
, int, group_fd
, unsigned long, flags
)
2850 struct perf_counter
*counter
, *group_leader
;
2851 struct perf_counter_hw_event hw_event
;
2852 struct perf_counter_context
*ctx
;
2853 struct file
*counter_file
= NULL
;
2854 struct file
*group_file
= NULL
;
2855 int fput_needed
= 0;
2856 int fput_needed2
= 0;
2859 /* for future expandability... */
2863 if (copy_from_user(&hw_event
, hw_event_uptr
, sizeof(hw_event
)) != 0)
2867 * Get the target context (task or percpu):
2869 ctx
= find_get_context(pid
, cpu
);
2871 return PTR_ERR(ctx
);
2874 * Look up the group leader (we will attach this counter to it):
2876 group_leader
= NULL
;
2877 if (group_fd
!= -1) {
2879 group_file
= fget_light(group_fd
, &fput_needed
);
2881 goto err_put_context
;
2882 if (group_file
->f_op
!= &perf_fops
)
2883 goto err_put_context
;
2885 group_leader
= group_file
->private_data
;
2887 * Do not allow a recursive hierarchy (this new sibling
2888 * becoming part of another group-sibling):
2890 if (group_leader
->group_leader
!= group_leader
)
2891 goto err_put_context
;
2893 * Do not allow to attach to a group in a different
2894 * task or CPU context:
2896 if (group_leader
->ctx
!= ctx
)
2897 goto err_put_context
;
2899 * Only a group leader can be exclusive or pinned
2901 if (hw_event
.exclusive
|| hw_event
.pinned
)
2902 goto err_put_context
;
2905 counter
= perf_counter_alloc(&hw_event
, cpu
, ctx
, group_leader
,
2907 ret
= PTR_ERR(counter
);
2908 if (IS_ERR(counter
))
2909 goto err_put_context
;
2911 ret
= anon_inode_getfd("[perf_counter]", &perf_fops
, counter
, 0);
2913 goto err_free_put_context
;
2915 counter_file
= fget_light(ret
, &fput_needed2
);
2917 goto err_free_put_context
;
2919 counter
->filp
= counter_file
;
2920 mutex_lock(&ctx
->mutex
);
2921 perf_install_in_context(ctx
, counter
, cpu
);
2922 mutex_unlock(&ctx
->mutex
);
2924 fput_light(counter_file
, fput_needed2
);
2927 fput_light(group_file
, fput_needed
);
2931 err_free_put_context
:
2941 * Initialize the perf_counter context in a task_struct:
2944 __perf_counter_init_context(struct perf_counter_context
*ctx
,
2945 struct task_struct
*task
)
2947 memset(ctx
, 0, sizeof(*ctx
));
2948 spin_lock_init(&ctx
->lock
);
2949 mutex_init(&ctx
->mutex
);
2950 INIT_LIST_HEAD(&ctx
->counter_list
);
2951 INIT_LIST_HEAD(&ctx
->event_list
);
2956 * inherit a counter from parent task to child task:
2958 static struct perf_counter
*
2959 inherit_counter(struct perf_counter
*parent_counter
,
2960 struct task_struct
*parent
,
2961 struct perf_counter_context
*parent_ctx
,
2962 struct task_struct
*child
,
2963 struct perf_counter
*group_leader
,
2964 struct perf_counter_context
*child_ctx
)
2966 struct perf_counter
*child_counter
;
2969 * Instead of creating recursive hierarchies of counters,
2970 * we link inherited counters back to the original parent,
2971 * which has a filp for sure, which we use as the reference
2974 if (parent_counter
->parent
)
2975 parent_counter
= parent_counter
->parent
;
2977 child_counter
= perf_counter_alloc(&parent_counter
->hw_event
,
2978 parent_counter
->cpu
, child_ctx
,
2979 group_leader
, GFP_KERNEL
);
2980 if (IS_ERR(child_counter
))
2981 return child_counter
;
2984 * Link it up in the child's context:
2986 child_counter
->task
= child
;
2987 add_counter_to_ctx(child_counter
, child_ctx
);
2989 child_counter
->parent
= parent_counter
;
2991 * inherit into child's child as well:
2993 child_counter
->hw_event
.inherit
= 1;
2996 * Get a reference to the parent filp - we will fput it
2997 * when the child counter exits. This is safe to do because
2998 * we are in the parent and we know that the filp still
2999 * exists and has a nonzero count:
3001 atomic_long_inc(&parent_counter
->filp
->f_count
);
3004 * Link this into the parent counter's child list
3006 mutex_lock(&parent_counter
->mutex
);
3007 list_add_tail(&child_counter
->child_list
, &parent_counter
->child_list
);
3010 * Make the child state follow the state of the parent counter,
3011 * not its hw_event.disabled bit. We hold the parent's mutex,
3012 * so we won't race with perf_counter_{en,dis}able_family.
3014 if (parent_counter
->state
>= PERF_COUNTER_STATE_INACTIVE
)
3015 child_counter
->state
= PERF_COUNTER_STATE_INACTIVE
;
3017 child_counter
->state
= PERF_COUNTER_STATE_OFF
;
3019 mutex_unlock(&parent_counter
->mutex
);
3021 return child_counter
;
3024 static int inherit_group(struct perf_counter
*parent_counter
,
3025 struct task_struct
*parent
,
3026 struct perf_counter_context
*parent_ctx
,
3027 struct task_struct
*child
,
3028 struct perf_counter_context
*child_ctx
)
3030 struct perf_counter
*leader
;
3031 struct perf_counter
*sub
;
3032 struct perf_counter
*child_ctr
;
3034 leader
= inherit_counter(parent_counter
, parent
, parent_ctx
,
3035 child
, NULL
, child_ctx
);
3037 return PTR_ERR(leader
);
3038 list_for_each_entry(sub
, &parent_counter
->sibling_list
, list_entry
) {
3039 child_ctr
= inherit_counter(sub
, parent
, parent_ctx
,
3040 child
, leader
, child_ctx
);
3041 if (IS_ERR(child_ctr
))
3042 return PTR_ERR(child_ctr
);
3047 static void sync_child_counter(struct perf_counter
*child_counter
,
3048 struct perf_counter
*parent_counter
)
3050 u64 parent_val
, child_val
;
3052 parent_val
= atomic64_read(&parent_counter
->count
);
3053 child_val
= atomic64_read(&child_counter
->count
);
3056 * Add back the child's count to the parent's count:
3058 atomic64_add(child_val
, &parent_counter
->count
);
3059 atomic64_add(child_counter
->total_time_enabled
,
3060 &parent_counter
->child_total_time_enabled
);
3061 atomic64_add(child_counter
->total_time_running
,
3062 &parent_counter
->child_total_time_running
);
3065 * Remove this counter from the parent's list
3067 mutex_lock(&parent_counter
->mutex
);
3068 list_del_init(&child_counter
->child_list
);
3069 mutex_unlock(&parent_counter
->mutex
);
3072 * Release the parent counter, if this was the last
3075 fput(parent_counter
->filp
);
3079 __perf_counter_exit_task(struct task_struct
*child
,
3080 struct perf_counter
*child_counter
,
3081 struct perf_counter_context
*child_ctx
)
3083 struct perf_counter
*parent_counter
;
3084 struct perf_counter
*sub
, *tmp
;
3087 * If we do not self-reap then we have to wait for the
3088 * child task to unschedule (it will happen for sure),
3089 * so that its counter is at its final count. (This
3090 * condition triggers rarely - child tasks usually get
3091 * off their CPU before the parent has a chance to
3092 * get this far into the reaping action)
3094 if (child
!= current
) {
3095 wait_task_inactive(child
, 0);
3096 list_del_init(&child_counter
->list_entry
);
3097 update_counter_times(child_counter
);
3099 struct perf_cpu_context
*cpuctx
;
3100 unsigned long flags
;
3104 * Disable and unlink this counter.
3106 * Be careful about zapping the list - IRQ/NMI context
3107 * could still be processing it:
3109 local_irq_save(flags
);
3110 perf_flags
= hw_perf_save_disable();
3112 cpuctx
= &__get_cpu_var(perf_cpu_context
);
3114 group_sched_out(child_counter
, cpuctx
, child_ctx
);
3115 update_counter_times(child_counter
);
3117 list_del_init(&child_counter
->list_entry
);
3119 child_ctx
->nr_counters
--;
3121 hw_perf_restore(perf_flags
);
3122 local_irq_restore(flags
);
3125 parent_counter
= child_counter
->parent
;
3127 * It can happen that parent exits first, and has counters
3128 * that are still around due to the child reference. These
3129 * counters need to be zapped - but otherwise linger.
3131 if (parent_counter
) {
3132 sync_child_counter(child_counter
, parent_counter
);
3133 list_for_each_entry_safe(sub
, tmp
, &child_counter
->sibling_list
,
3136 sync_child_counter(sub
, sub
->parent
);
3140 free_counter(child_counter
);
3145 * When a child task exits, feed back counter values to parent counters.
3147 * Note: we may be running in child context, but the PID is not hashed
3148 * anymore so new counters will not be added.
3150 void perf_counter_exit_task(struct task_struct
*child
)
3152 struct perf_counter
*child_counter
, *tmp
;
3153 struct perf_counter_context
*child_ctx
;
3155 child_ctx
= &child
->perf_counter_ctx
;
3157 if (likely(!child_ctx
->nr_counters
))
3160 list_for_each_entry_safe(child_counter
, tmp
, &child_ctx
->counter_list
,
3162 __perf_counter_exit_task(child
, child_counter
, child_ctx
);
3166 * Initialize the perf_counter context in task_struct
3168 void perf_counter_init_task(struct task_struct
*child
)
3170 struct perf_counter_context
*child_ctx
, *parent_ctx
;
3171 struct perf_counter
*counter
;
3172 struct task_struct
*parent
= current
;
3174 child_ctx
= &child
->perf_counter_ctx
;
3175 parent_ctx
= &parent
->perf_counter_ctx
;
3177 __perf_counter_init_context(child_ctx
, child
);
3180 * This is executed from the parent task context, so inherit
3181 * counters that have been marked for cloning:
3184 if (likely(!parent_ctx
->nr_counters
))
3188 * Lock the parent list. No need to lock the child - not PID
3189 * hashed yet and not running, so nobody can access it.
3191 mutex_lock(&parent_ctx
->mutex
);
3194 * We dont have to disable NMIs - we are only looking at
3195 * the list, not manipulating it:
3197 list_for_each_entry(counter
, &parent_ctx
->counter_list
, list_entry
) {
3198 if (!counter
->hw_event
.inherit
)
3201 if (inherit_group(counter
, parent
,
3202 parent_ctx
, child
, child_ctx
))
3206 mutex_unlock(&parent_ctx
->mutex
);
3209 static void __cpuinit
perf_counter_init_cpu(int cpu
)
3211 struct perf_cpu_context
*cpuctx
;
3213 cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
3214 __perf_counter_init_context(&cpuctx
->ctx
, NULL
);
3216 spin_lock(&perf_resource_lock
);
3217 cpuctx
->max_pertask
= perf_max_counters
- perf_reserved_percpu
;
3218 spin_unlock(&perf_resource_lock
);
3220 hw_perf_counter_setup(cpu
);
3223 #ifdef CONFIG_HOTPLUG_CPU
3224 static void __perf_counter_exit_cpu(void *info
)
3226 struct perf_cpu_context
*cpuctx
= &__get_cpu_var(perf_cpu_context
);
3227 struct perf_counter_context
*ctx
= &cpuctx
->ctx
;
3228 struct perf_counter
*counter
, *tmp
;
3230 list_for_each_entry_safe(counter
, tmp
, &ctx
->counter_list
, list_entry
)
3231 __perf_counter_remove_from_context(counter
);
3233 static void perf_counter_exit_cpu(int cpu
)
3235 struct perf_cpu_context
*cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
3236 struct perf_counter_context
*ctx
= &cpuctx
->ctx
;
3238 mutex_lock(&ctx
->mutex
);
3239 smp_call_function_single(cpu
, __perf_counter_exit_cpu
, NULL
, 1);
3240 mutex_unlock(&ctx
->mutex
);
3243 static inline void perf_counter_exit_cpu(int cpu
) { }
3246 static int __cpuinit
3247 perf_cpu_notify(struct notifier_block
*self
, unsigned long action
, void *hcpu
)
3249 unsigned int cpu
= (long)hcpu
;
3253 case CPU_UP_PREPARE
:
3254 case CPU_UP_PREPARE_FROZEN
:
3255 perf_counter_init_cpu(cpu
);
3258 case CPU_DOWN_PREPARE
:
3259 case CPU_DOWN_PREPARE_FROZEN
:
3260 perf_counter_exit_cpu(cpu
);
3270 static struct notifier_block __cpuinitdata perf_cpu_nb
= {
3271 .notifier_call
= perf_cpu_notify
,
3274 void __init
perf_counter_init(void)
3276 perf_cpu_notify(&perf_cpu_nb
, (unsigned long)CPU_UP_PREPARE
,
3277 (void *)(long)smp_processor_id());
3278 register_cpu_notifier(&perf_cpu_nb
);
3281 static ssize_t
perf_show_reserve_percpu(struct sysdev_class
*class, char *buf
)
3283 return sprintf(buf
, "%d\n", perf_reserved_percpu
);
3287 perf_set_reserve_percpu(struct sysdev_class
*class,
3291 struct perf_cpu_context
*cpuctx
;
3295 err
= strict_strtoul(buf
, 10, &val
);
3298 if (val
> perf_max_counters
)
3301 spin_lock(&perf_resource_lock
);
3302 perf_reserved_percpu
= val
;
3303 for_each_online_cpu(cpu
) {
3304 cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
3305 spin_lock_irq(&cpuctx
->ctx
.lock
);
3306 mpt
= min(perf_max_counters
- cpuctx
->ctx
.nr_counters
,
3307 perf_max_counters
- perf_reserved_percpu
);
3308 cpuctx
->max_pertask
= mpt
;
3309 spin_unlock_irq(&cpuctx
->ctx
.lock
);
3311 spin_unlock(&perf_resource_lock
);
3316 static ssize_t
perf_show_overcommit(struct sysdev_class
*class, char *buf
)
3318 return sprintf(buf
, "%d\n", perf_overcommit
);
3322 perf_set_overcommit(struct sysdev_class
*class, const char *buf
, size_t count
)
3327 err
= strict_strtoul(buf
, 10, &val
);
3333 spin_lock(&perf_resource_lock
);
3334 perf_overcommit
= val
;
3335 spin_unlock(&perf_resource_lock
);
3340 static SYSDEV_CLASS_ATTR(
3343 perf_show_reserve_percpu
,
3344 perf_set_reserve_percpu
3347 static SYSDEV_CLASS_ATTR(
3350 perf_show_overcommit
,
3354 static struct attribute
*perfclass_attrs
[] = {
3355 &attr_reserve_percpu
.attr
,
3356 &attr_overcommit
.attr
,
3360 static struct attribute_group perfclass_attr_group
= {
3361 .attrs
= perfclass_attrs
,
3362 .name
= "perf_counters",
3365 static int __init
perf_counter_sysfs_init(void)
3367 return sysfs_create_group(&cpu_sysdev_class
.kset
.kobj
,
3368 &perfclass_attr_group
);
3370 device_initcall(perf_counter_sysfs_init
);