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
);
121 list_del_counter(struct perf_counter
*counter
, struct perf_counter_context
*ctx
)
123 struct perf_counter
*sibling
, *tmp
;
125 list_del_init(&counter
->list_entry
);
126 list_del_rcu(&counter
->event_entry
);
128 if (counter
->group_leader
!= counter
)
129 counter
->group_leader
->nr_siblings
--;
132 * If this was a group counter with sibling counters then
133 * upgrade the siblings to singleton counters by adding them
134 * to the context list directly:
136 list_for_each_entry_safe(sibling
, tmp
,
137 &counter
->sibling_list
, list_entry
) {
139 list_move_tail(&sibling
->list_entry
, &ctx
->counter_list
);
140 sibling
->group_leader
= sibling
;
145 counter_sched_out(struct perf_counter
*counter
,
146 struct perf_cpu_context
*cpuctx
,
147 struct perf_counter_context
*ctx
)
149 if (counter
->state
!= PERF_COUNTER_STATE_ACTIVE
)
152 counter
->state
= PERF_COUNTER_STATE_INACTIVE
;
153 counter
->tstamp_stopped
= ctx
->time
;
154 counter
->pmu
->disable(counter
);
157 if (!is_software_counter(counter
))
158 cpuctx
->active_oncpu
--;
160 if (counter
->hw_event
.exclusive
|| !cpuctx
->active_oncpu
)
161 cpuctx
->exclusive
= 0;
165 group_sched_out(struct perf_counter
*group_counter
,
166 struct perf_cpu_context
*cpuctx
,
167 struct perf_counter_context
*ctx
)
169 struct perf_counter
*counter
;
171 if (group_counter
->state
!= PERF_COUNTER_STATE_ACTIVE
)
174 counter_sched_out(group_counter
, cpuctx
, ctx
);
177 * Schedule out siblings (if any):
179 list_for_each_entry(counter
, &group_counter
->sibling_list
, list_entry
)
180 counter_sched_out(counter
, cpuctx
, ctx
);
182 if (group_counter
->hw_event
.exclusive
)
183 cpuctx
->exclusive
= 0;
187 * Cross CPU call to remove a performance counter
189 * We disable the counter on the hardware level first. After that we
190 * remove it from the context list.
192 static void __perf_counter_remove_from_context(void *info
)
194 struct perf_cpu_context
*cpuctx
= &__get_cpu_var(perf_cpu_context
);
195 struct perf_counter
*counter
= info
;
196 struct perf_counter_context
*ctx
= counter
->ctx
;
200 * If this is a task context, we need to check whether it is
201 * the current task context of this cpu. If not it has been
202 * scheduled out before the smp call arrived.
204 if (ctx
->task
&& cpuctx
->task_ctx
!= ctx
)
207 spin_lock_irqsave(&ctx
->lock
, flags
);
209 counter_sched_out(counter
, cpuctx
, ctx
);
211 counter
->task
= NULL
;
215 * Protect the list operation against NMI by disabling the
216 * counters on a global level. NOP for non NMI based counters.
219 list_del_counter(counter
, ctx
);
224 * Allow more per task counters with respect to the
227 cpuctx
->max_pertask
=
228 min(perf_max_counters
- ctx
->nr_counters
,
229 perf_max_counters
- perf_reserved_percpu
);
232 spin_unlock_irqrestore(&ctx
->lock
, flags
);
237 * Remove the counter from a task's (or a CPU's) list of counters.
239 * Must be called with counter->mutex and ctx->mutex held.
241 * CPU counters are removed with a smp call. For task counters we only
242 * call when the task is on a CPU.
244 static void perf_counter_remove_from_context(struct perf_counter
*counter
)
246 struct perf_counter_context
*ctx
= counter
->ctx
;
247 struct task_struct
*task
= ctx
->task
;
251 * Per cpu counters are removed via an smp call and
252 * the removal is always sucessful.
254 smp_call_function_single(counter
->cpu
,
255 __perf_counter_remove_from_context
,
261 task_oncpu_function_call(task
, __perf_counter_remove_from_context
,
264 spin_lock_irq(&ctx
->lock
);
266 * If the context is active we need to retry the smp call.
268 if (ctx
->nr_active
&& !list_empty(&counter
->list_entry
)) {
269 spin_unlock_irq(&ctx
->lock
);
274 * The lock prevents that this context is scheduled in so we
275 * can remove the counter safely, if the call above did not
278 if (!list_empty(&counter
->list_entry
)) {
280 list_del_counter(counter
, ctx
);
281 counter
->task
= NULL
;
283 spin_unlock_irq(&ctx
->lock
);
286 static inline u64
perf_clock(void)
288 return cpu_clock(smp_processor_id());
292 * Update the record of the current time in a context.
294 static void update_context_time(struct perf_counter_context
*ctx
)
296 u64 now
= perf_clock();
298 ctx
->time
+= now
- ctx
->timestamp
;
299 ctx
->timestamp
= now
;
303 * Update the total_time_enabled and total_time_running fields for a counter.
305 static void update_counter_times(struct perf_counter
*counter
)
307 struct perf_counter_context
*ctx
= counter
->ctx
;
310 if (counter
->state
< PERF_COUNTER_STATE_INACTIVE
)
313 counter
->total_time_enabled
= ctx
->time
- counter
->tstamp_enabled
;
315 if (counter
->state
== PERF_COUNTER_STATE_INACTIVE
)
316 run_end
= counter
->tstamp_stopped
;
320 counter
->total_time_running
= run_end
- counter
->tstamp_running
;
324 * Update total_time_enabled and total_time_running for all counters in a group.
326 static void update_group_times(struct perf_counter
*leader
)
328 struct perf_counter
*counter
;
330 update_counter_times(leader
);
331 list_for_each_entry(counter
, &leader
->sibling_list
, list_entry
)
332 update_counter_times(counter
);
336 * Cross CPU call to disable a performance counter
338 static void __perf_counter_disable(void *info
)
340 struct perf_counter
*counter
= info
;
341 struct perf_cpu_context
*cpuctx
= &__get_cpu_var(perf_cpu_context
);
342 struct perf_counter_context
*ctx
= counter
->ctx
;
346 * If this is a per-task counter, need to check whether this
347 * counter's task is the current task on this cpu.
349 if (ctx
->task
&& cpuctx
->task_ctx
!= ctx
)
352 spin_lock_irqsave(&ctx
->lock
, flags
);
355 * If the counter is on, turn it off.
356 * If it is in error state, leave it in error state.
358 if (counter
->state
>= PERF_COUNTER_STATE_INACTIVE
) {
359 update_context_time(ctx
);
360 update_counter_times(counter
);
361 if (counter
== counter
->group_leader
)
362 group_sched_out(counter
, cpuctx
, ctx
);
364 counter_sched_out(counter
, cpuctx
, ctx
);
365 counter
->state
= PERF_COUNTER_STATE_OFF
;
368 spin_unlock_irqrestore(&ctx
->lock
, flags
);
374 static void perf_counter_disable(struct perf_counter
*counter
)
376 struct perf_counter_context
*ctx
= counter
->ctx
;
377 struct task_struct
*task
= ctx
->task
;
381 * Disable the counter on the cpu that it's on
383 smp_call_function_single(counter
->cpu
, __perf_counter_disable
,
389 task_oncpu_function_call(task
, __perf_counter_disable
, counter
);
391 spin_lock_irq(&ctx
->lock
);
393 * If the counter is still active, we need to retry the cross-call.
395 if (counter
->state
== PERF_COUNTER_STATE_ACTIVE
) {
396 spin_unlock_irq(&ctx
->lock
);
401 * Since we have the lock this context can't be scheduled
402 * in, so we can change the state safely.
404 if (counter
->state
== PERF_COUNTER_STATE_INACTIVE
) {
405 update_counter_times(counter
);
406 counter
->state
= PERF_COUNTER_STATE_OFF
;
409 spin_unlock_irq(&ctx
->lock
);
413 counter_sched_in(struct perf_counter
*counter
,
414 struct perf_cpu_context
*cpuctx
,
415 struct perf_counter_context
*ctx
,
418 if (counter
->state
<= PERF_COUNTER_STATE_OFF
)
421 counter
->state
= PERF_COUNTER_STATE_ACTIVE
;
422 counter
->oncpu
= cpu
; /* TODO: put 'cpu' into cpuctx->cpu */
424 * The new state must be visible before we turn it on in the hardware:
428 if (counter
->pmu
->enable(counter
)) {
429 counter
->state
= PERF_COUNTER_STATE_INACTIVE
;
434 counter
->tstamp_running
+= ctx
->time
- counter
->tstamp_stopped
;
436 if (!is_software_counter(counter
))
437 cpuctx
->active_oncpu
++;
440 if (counter
->hw_event
.exclusive
)
441 cpuctx
->exclusive
= 1;
447 group_sched_in(struct perf_counter
*group_counter
,
448 struct perf_cpu_context
*cpuctx
,
449 struct perf_counter_context
*ctx
,
452 struct perf_counter
*counter
, *partial_group
;
455 if (group_counter
->state
== PERF_COUNTER_STATE_OFF
)
458 ret
= hw_perf_group_sched_in(group_counter
, cpuctx
, ctx
, cpu
);
460 return ret
< 0 ? ret
: 0;
462 group_counter
->prev_state
= group_counter
->state
;
463 if (counter_sched_in(group_counter
, cpuctx
, ctx
, cpu
))
467 * Schedule in siblings as one group (if any):
469 list_for_each_entry(counter
, &group_counter
->sibling_list
, list_entry
) {
470 counter
->prev_state
= counter
->state
;
471 if (counter_sched_in(counter
, cpuctx
, ctx
, cpu
)) {
472 partial_group
= counter
;
481 * Groups can be scheduled in as one unit only, so undo any
482 * partial group before returning:
484 list_for_each_entry(counter
, &group_counter
->sibling_list
, list_entry
) {
485 if (counter
== partial_group
)
487 counter_sched_out(counter
, cpuctx
, ctx
);
489 counter_sched_out(group_counter
, cpuctx
, ctx
);
495 * Return 1 for a group consisting entirely of software counters,
496 * 0 if the group contains any hardware counters.
498 static int is_software_only_group(struct perf_counter
*leader
)
500 struct perf_counter
*counter
;
502 if (!is_software_counter(leader
))
505 list_for_each_entry(counter
, &leader
->sibling_list
, list_entry
)
506 if (!is_software_counter(counter
))
513 * Work out whether we can put this counter group on the CPU now.
515 static int group_can_go_on(struct perf_counter
*counter
,
516 struct perf_cpu_context
*cpuctx
,
520 * Groups consisting entirely of software counters can always go on.
522 if (is_software_only_group(counter
))
525 * If an exclusive group is already on, no other hardware
526 * counters can go on.
528 if (cpuctx
->exclusive
)
531 * If this group is exclusive and there are already
532 * counters on the CPU, it can't go on.
534 if (counter
->hw_event
.exclusive
&& cpuctx
->active_oncpu
)
537 * Otherwise, try to add it if all previous groups were able
543 static void add_counter_to_ctx(struct perf_counter
*counter
,
544 struct perf_counter_context
*ctx
)
546 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
);
1050 * Round-robin a context's counters:
1052 static void rotate_ctx(struct perf_counter_context
*ctx
)
1054 struct perf_counter
*counter
;
1056 if (!ctx
->nr_counters
)
1059 spin_lock(&ctx
->lock
);
1061 * Rotate the first entry last (works just fine for group counters too):
1064 list_for_each_entry(counter
, &ctx
->counter_list
, list_entry
) {
1065 list_move_tail(&counter
->list_entry
, &ctx
->counter_list
);
1070 spin_unlock(&ctx
->lock
);
1073 void perf_counter_task_tick(struct task_struct
*curr
, int cpu
)
1075 struct perf_cpu_context
*cpuctx
;
1076 struct perf_counter_context
*ctx
;
1078 if (!atomic_read(&nr_counters
))
1081 cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
1082 ctx
= &curr
->perf_counter_ctx
;
1084 perf_counter_cpu_sched_out(cpuctx
);
1085 __perf_counter_task_sched_out(ctx
);
1087 rotate_ctx(&cpuctx
->ctx
);
1090 perf_counter_cpu_sched_in(cpuctx
, cpu
);
1091 perf_counter_task_sched_in(curr
, cpu
);
1095 * Cross CPU call to read the hardware counter
1097 static void __read(void *info
)
1099 struct perf_counter
*counter
= info
;
1100 struct perf_counter_context
*ctx
= counter
->ctx
;
1101 unsigned long flags
;
1103 local_irq_save(flags
);
1105 update_context_time(ctx
);
1106 counter
->pmu
->read(counter
);
1107 update_counter_times(counter
);
1108 local_irq_restore(flags
);
1111 static u64
perf_counter_read(struct perf_counter
*counter
)
1114 * If counter is enabled and currently active on a CPU, update the
1115 * value in the counter structure:
1117 if (counter
->state
== PERF_COUNTER_STATE_ACTIVE
) {
1118 smp_call_function_single(counter
->oncpu
,
1119 __read
, counter
, 1);
1120 } else if (counter
->state
== PERF_COUNTER_STATE_INACTIVE
) {
1121 update_counter_times(counter
);
1124 return atomic64_read(&counter
->count
);
1127 static void put_context(struct perf_counter_context
*ctx
)
1130 put_task_struct(ctx
->task
);
1133 static struct perf_counter_context
*find_get_context(pid_t pid
, int cpu
)
1135 struct perf_cpu_context
*cpuctx
;
1136 struct perf_counter_context
*ctx
;
1137 struct task_struct
*task
;
1140 * If cpu is not a wildcard then this is a percpu counter:
1143 /* Must be root to operate on a CPU counter: */
1144 if (sysctl_perf_counter_priv
&& !capable(CAP_SYS_ADMIN
))
1145 return ERR_PTR(-EACCES
);
1147 if (cpu
< 0 || cpu
> num_possible_cpus())
1148 return ERR_PTR(-EINVAL
);
1151 * We could be clever and allow to attach a counter to an
1152 * offline CPU and activate it when the CPU comes up, but
1155 if (!cpu_isset(cpu
, cpu_online_map
))
1156 return ERR_PTR(-ENODEV
);
1158 cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
1168 task
= find_task_by_vpid(pid
);
1170 get_task_struct(task
);
1174 return ERR_PTR(-ESRCH
);
1176 ctx
= &task
->perf_counter_ctx
;
1179 /* Reuse ptrace permission checks for now. */
1180 if (!ptrace_may_access(task
, PTRACE_MODE_READ
)) {
1182 return ERR_PTR(-EACCES
);
1188 static void free_counter_rcu(struct rcu_head
*head
)
1190 struct perf_counter
*counter
;
1192 counter
= container_of(head
, struct perf_counter
, rcu_head
);
1196 static void perf_pending_sync(struct perf_counter
*counter
);
1198 static void free_counter(struct perf_counter
*counter
)
1200 perf_pending_sync(counter
);
1202 atomic_dec(&nr_counters
);
1203 if (counter
->hw_event
.mmap
)
1204 atomic_dec(&nr_mmap_tracking
);
1205 if (counter
->hw_event
.munmap
)
1206 atomic_dec(&nr_munmap_tracking
);
1207 if (counter
->hw_event
.comm
)
1208 atomic_dec(&nr_comm_tracking
);
1210 if (counter
->destroy
)
1211 counter
->destroy(counter
);
1213 call_rcu(&counter
->rcu_head
, free_counter_rcu
);
1217 * Called when the last reference to the file is gone.
1219 static int perf_release(struct inode
*inode
, struct file
*file
)
1221 struct perf_counter
*counter
= file
->private_data
;
1222 struct perf_counter_context
*ctx
= counter
->ctx
;
1224 file
->private_data
= NULL
;
1226 mutex_lock(&ctx
->mutex
);
1227 mutex_lock(&counter
->mutex
);
1229 perf_counter_remove_from_context(counter
);
1231 mutex_unlock(&counter
->mutex
);
1232 mutex_unlock(&ctx
->mutex
);
1234 free_counter(counter
);
1241 * Read the performance counter - simple non blocking version for now
1244 perf_read_hw(struct perf_counter
*counter
, char __user
*buf
, size_t count
)
1250 * Return end-of-file for a read on a counter that is in
1251 * error state (i.e. because it was pinned but it couldn't be
1252 * scheduled on to the CPU at some point).
1254 if (counter
->state
== PERF_COUNTER_STATE_ERROR
)
1257 mutex_lock(&counter
->mutex
);
1258 values
[0] = perf_counter_read(counter
);
1260 if (counter
->hw_event
.read_format
& PERF_FORMAT_TOTAL_TIME_ENABLED
)
1261 values
[n
++] = counter
->total_time_enabled
+
1262 atomic64_read(&counter
->child_total_time_enabled
);
1263 if (counter
->hw_event
.read_format
& PERF_FORMAT_TOTAL_TIME_RUNNING
)
1264 values
[n
++] = counter
->total_time_running
+
1265 atomic64_read(&counter
->child_total_time_running
);
1266 mutex_unlock(&counter
->mutex
);
1268 if (count
< n
* sizeof(u64
))
1270 count
= n
* sizeof(u64
);
1272 if (copy_to_user(buf
, values
, count
))
1279 perf_read(struct file
*file
, char __user
*buf
, size_t count
, loff_t
*ppos
)
1281 struct perf_counter
*counter
= file
->private_data
;
1283 return perf_read_hw(counter
, buf
, count
);
1286 static unsigned int perf_poll(struct file
*file
, poll_table
*wait
)
1288 struct perf_counter
*counter
= file
->private_data
;
1289 struct perf_mmap_data
*data
;
1290 unsigned int events
= POLL_HUP
;
1293 data
= rcu_dereference(counter
->data
);
1295 events
= atomic_xchg(&data
->poll
, 0);
1298 poll_wait(file
, &counter
->waitq
, wait
);
1303 static void perf_counter_reset(struct perf_counter
*counter
)
1305 (void)perf_counter_read(counter
);
1306 atomic64_set(&counter
->count
, 0);
1307 perf_counter_update_userpage(counter
);
1310 static void perf_counter_for_each_sibling(struct perf_counter
*counter
,
1311 void (*func
)(struct perf_counter
*))
1313 struct perf_counter_context
*ctx
= counter
->ctx
;
1314 struct perf_counter
*sibling
;
1316 spin_lock_irq(&ctx
->lock
);
1317 counter
= counter
->group_leader
;
1320 list_for_each_entry(sibling
, &counter
->sibling_list
, list_entry
)
1322 spin_unlock_irq(&ctx
->lock
);
1325 static void perf_counter_for_each_child(struct perf_counter
*counter
,
1326 void (*func
)(struct perf_counter
*))
1328 struct perf_counter
*child
;
1330 mutex_lock(&counter
->mutex
);
1332 list_for_each_entry(child
, &counter
->child_list
, child_list
)
1334 mutex_unlock(&counter
->mutex
);
1337 static void perf_counter_for_each(struct perf_counter
*counter
,
1338 void (*func
)(struct perf_counter
*))
1340 struct perf_counter
*child
;
1342 mutex_lock(&counter
->mutex
);
1343 perf_counter_for_each_sibling(counter
, func
);
1344 list_for_each_entry(child
, &counter
->child_list
, child_list
)
1345 perf_counter_for_each_sibling(child
, func
);
1346 mutex_unlock(&counter
->mutex
);
1349 static long perf_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
1351 struct perf_counter
*counter
= file
->private_data
;
1352 void (*func
)(struct perf_counter
*);
1356 case PERF_COUNTER_IOC_ENABLE
:
1357 func
= perf_counter_enable
;
1359 case PERF_COUNTER_IOC_DISABLE
:
1360 func
= perf_counter_disable
;
1362 case PERF_COUNTER_IOC_RESET
:
1363 func
= perf_counter_reset
;
1366 case PERF_COUNTER_IOC_REFRESH
:
1367 return perf_counter_refresh(counter
, arg
);
1372 if (flags
& PERF_IOC_FLAG_GROUP
)
1373 perf_counter_for_each(counter
, func
);
1375 perf_counter_for_each_child(counter
, func
);
1381 * Callers need to ensure there can be no nesting of this function, otherwise
1382 * the seqlock logic goes bad. We can not serialize this because the arch
1383 * code calls this from NMI context.
1385 void perf_counter_update_userpage(struct perf_counter
*counter
)
1387 struct perf_mmap_data
*data
;
1388 struct perf_counter_mmap_page
*userpg
;
1391 data
= rcu_dereference(counter
->data
);
1395 userpg
= data
->user_page
;
1398 * Disable preemption so as to not let the corresponding user-space
1399 * spin too long if we get preempted.
1404 userpg
->index
= counter
->hw
.idx
;
1405 userpg
->offset
= atomic64_read(&counter
->count
);
1406 if (counter
->state
== PERF_COUNTER_STATE_ACTIVE
)
1407 userpg
->offset
-= atomic64_read(&counter
->hw
.prev_count
);
1416 static int perf_mmap_fault(struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
1418 struct perf_counter
*counter
= vma
->vm_file
->private_data
;
1419 struct perf_mmap_data
*data
;
1420 int ret
= VM_FAULT_SIGBUS
;
1423 data
= rcu_dereference(counter
->data
);
1427 if (vmf
->pgoff
== 0) {
1428 vmf
->page
= virt_to_page(data
->user_page
);
1430 int nr
= vmf
->pgoff
- 1;
1432 if ((unsigned)nr
> data
->nr_pages
)
1435 vmf
->page
= virt_to_page(data
->data_pages
[nr
]);
1437 get_page(vmf
->page
);
1445 static int perf_mmap_data_alloc(struct perf_counter
*counter
, int nr_pages
)
1447 struct perf_mmap_data
*data
;
1451 WARN_ON(atomic_read(&counter
->mmap_count
));
1453 size
= sizeof(struct perf_mmap_data
);
1454 size
+= nr_pages
* sizeof(void *);
1456 data
= kzalloc(size
, GFP_KERNEL
);
1460 data
->user_page
= (void *)get_zeroed_page(GFP_KERNEL
);
1461 if (!data
->user_page
)
1462 goto fail_user_page
;
1464 for (i
= 0; i
< nr_pages
; i
++) {
1465 data
->data_pages
[i
] = (void *)get_zeroed_page(GFP_KERNEL
);
1466 if (!data
->data_pages
[i
])
1467 goto fail_data_pages
;
1470 data
->nr_pages
= nr_pages
;
1471 atomic_set(&data
->lock
, -1);
1473 rcu_assign_pointer(counter
->data
, data
);
1478 for (i
--; i
>= 0; i
--)
1479 free_page((unsigned long)data
->data_pages
[i
]);
1481 free_page((unsigned long)data
->user_page
);
1490 static void __perf_mmap_data_free(struct rcu_head
*rcu_head
)
1492 struct perf_mmap_data
*data
= container_of(rcu_head
,
1493 struct perf_mmap_data
, rcu_head
);
1496 free_page((unsigned long)data
->user_page
);
1497 for (i
= 0; i
< data
->nr_pages
; i
++)
1498 free_page((unsigned long)data
->data_pages
[i
]);
1502 static void perf_mmap_data_free(struct perf_counter
*counter
)
1504 struct perf_mmap_data
*data
= counter
->data
;
1506 WARN_ON(atomic_read(&counter
->mmap_count
));
1508 rcu_assign_pointer(counter
->data
, NULL
);
1509 call_rcu(&data
->rcu_head
, __perf_mmap_data_free
);
1512 static void perf_mmap_open(struct vm_area_struct
*vma
)
1514 struct perf_counter
*counter
= vma
->vm_file
->private_data
;
1516 atomic_inc(&counter
->mmap_count
);
1519 static void perf_mmap_close(struct vm_area_struct
*vma
)
1521 struct perf_counter
*counter
= vma
->vm_file
->private_data
;
1523 if (atomic_dec_and_mutex_lock(&counter
->mmap_count
,
1524 &counter
->mmap_mutex
)) {
1525 struct user_struct
*user
= current_user();
1527 atomic_long_sub(counter
->data
->nr_pages
+ 1, &user
->locked_vm
);
1528 vma
->vm_mm
->locked_vm
-= counter
->data
->nr_locked
;
1529 perf_mmap_data_free(counter
);
1530 mutex_unlock(&counter
->mmap_mutex
);
1534 static struct vm_operations_struct perf_mmap_vmops
= {
1535 .open
= perf_mmap_open
,
1536 .close
= perf_mmap_close
,
1537 .fault
= perf_mmap_fault
,
1540 static int perf_mmap(struct file
*file
, struct vm_area_struct
*vma
)
1542 struct perf_counter
*counter
= file
->private_data
;
1543 struct user_struct
*user
= current_user();
1544 unsigned long vma_size
;
1545 unsigned long nr_pages
;
1546 unsigned long user_locked
, user_lock_limit
;
1547 unsigned long locked
, lock_limit
;
1548 long user_extra
, extra
;
1551 if (!(vma
->vm_flags
& VM_SHARED
) || (vma
->vm_flags
& VM_WRITE
))
1554 vma_size
= vma
->vm_end
- vma
->vm_start
;
1555 nr_pages
= (vma_size
/ PAGE_SIZE
) - 1;
1558 * If we have data pages ensure they're a power-of-two number, so we
1559 * can do bitmasks instead of modulo.
1561 if (nr_pages
!= 0 && !is_power_of_2(nr_pages
))
1564 if (vma_size
!= PAGE_SIZE
* (1 + nr_pages
))
1567 if (vma
->vm_pgoff
!= 0)
1570 mutex_lock(&counter
->mmap_mutex
);
1571 if (atomic_inc_not_zero(&counter
->mmap_count
)) {
1572 if (nr_pages
!= counter
->data
->nr_pages
)
1577 user_extra
= nr_pages
+ 1;
1578 user_lock_limit
= sysctl_perf_counter_mlock
>> (PAGE_SHIFT
- 10);
1579 user_locked
= atomic_long_read(&user
->locked_vm
) + user_extra
;
1582 if (user_locked
> user_lock_limit
)
1583 extra
= user_locked
- user_lock_limit
;
1585 lock_limit
= current
->signal
->rlim
[RLIMIT_MEMLOCK
].rlim_cur
;
1586 lock_limit
>>= PAGE_SHIFT
;
1587 locked
= vma
->vm_mm
->locked_vm
+ extra
;
1589 if ((locked
> lock_limit
) && !capable(CAP_IPC_LOCK
)) {
1594 WARN_ON(counter
->data
);
1595 ret
= perf_mmap_data_alloc(counter
, nr_pages
);
1599 atomic_set(&counter
->mmap_count
, 1);
1600 atomic_long_add(user_extra
, &user
->locked_vm
);
1601 vma
->vm_mm
->locked_vm
+= extra
;
1602 counter
->data
->nr_locked
= extra
;
1604 mutex_unlock(&counter
->mmap_mutex
);
1606 vma
->vm_flags
&= ~VM_MAYWRITE
;
1607 vma
->vm_flags
|= VM_RESERVED
;
1608 vma
->vm_ops
= &perf_mmap_vmops
;
1613 static int perf_fasync(int fd
, struct file
*filp
, int on
)
1615 struct perf_counter
*counter
= filp
->private_data
;
1616 struct inode
*inode
= filp
->f_path
.dentry
->d_inode
;
1619 mutex_lock(&inode
->i_mutex
);
1620 retval
= fasync_helper(fd
, filp
, on
, &counter
->fasync
);
1621 mutex_unlock(&inode
->i_mutex
);
1629 static const struct file_operations perf_fops
= {
1630 .release
= perf_release
,
1633 .unlocked_ioctl
= perf_ioctl
,
1634 .compat_ioctl
= perf_ioctl
,
1636 .fasync
= perf_fasync
,
1640 * Perf counter wakeup
1642 * If there's data, ensure we set the poll() state and publish everything
1643 * to user-space before waking everybody up.
1646 void perf_counter_wakeup(struct perf_counter
*counter
)
1648 wake_up_all(&counter
->waitq
);
1650 if (counter
->pending_kill
) {
1651 kill_fasync(&counter
->fasync
, SIGIO
, counter
->pending_kill
);
1652 counter
->pending_kill
= 0;
1659 * Handle the case where we need to wakeup up from NMI (or rq->lock) context.
1661 * The NMI bit means we cannot possibly take locks. Therefore, maintain a
1662 * single linked list and use cmpxchg() to add entries lockless.
1665 static void perf_pending_counter(struct perf_pending_entry
*entry
)
1667 struct perf_counter
*counter
= container_of(entry
,
1668 struct perf_counter
, pending
);
1670 if (counter
->pending_disable
) {
1671 counter
->pending_disable
= 0;
1672 perf_counter_disable(counter
);
1675 if (counter
->pending_wakeup
) {
1676 counter
->pending_wakeup
= 0;
1677 perf_counter_wakeup(counter
);
1681 #define PENDING_TAIL ((struct perf_pending_entry *)-1UL)
1683 static DEFINE_PER_CPU(struct perf_pending_entry
*, perf_pending_head
) = {
1687 static void perf_pending_queue(struct perf_pending_entry
*entry
,
1688 void (*func
)(struct perf_pending_entry
*))
1690 struct perf_pending_entry
**head
;
1692 if (cmpxchg(&entry
->next
, NULL
, PENDING_TAIL
) != NULL
)
1697 head
= &get_cpu_var(perf_pending_head
);
1700 entry
->next
= *head
;
1701 } while (cmpxchg(head
, entry
->next
, entry
) != entry
->next
);
1703 set_perf_counter_pending();
1705 put_cpu_var(perf_pending_head
);
1708 static int __perf_pending_run(void)
1710 struct perf_pending_entry
*list
;
1713 list
= xchg(&__get_cpu_var(perf_pending_head
), PENDING_TAIL
);
1714 while (list
!= PENDING_TAIL
) {
1715 void (*func
)(struct perf_pending_entry
*);
1716 struct perf_pending_entry
*entry
= list
;
1723 * Ensure we observe the unqueue before we issue the wakeup,
1724 * so that we won't be waiting forever.
1725 * -- see perf_not_pending().
1736 static inline int perf_not_pending(struct perf_counter
*counter
)
1739 * If we flush on whatever cpu we run, there is a chance we don't
1743 __perf_pending_run();
1747 * Ensure we see the proper queue state before going to sleep
1748 * so that we do not miss the wakeup. -- see perf_pending_handle()
1751 return counter
->pending
.next
== NULL
;
1754 static void perf_pending_sync(struct perf_counter
*counter
)
1756 wait_event(counter
->waitq
, perf_not_pending(counter
));
1759 void perf_counter_do_pending(void)
1761 __perf_pending_run();
1765 * Callchain support -- arch specific
1768 __weak
struct perf_callchain_entry
*perf_callchain(struct pt_regs
*regs
)
1777 struct perf_output_handle
{
1778 struct perf_counter
*counter
;
1779 struct perf_mmap_data
*data
;
1780 unsigned int offset
;
1785 unsigned long flags
;
1788 static void perf_output_wakeup(struct perf_output_handle
*handle
)
1790 atomic_set(&handle
->data
->poll
, POLL_IN
);
1793 handle
->counter
->pending_wakeup
= 1;
1794 perf_pending_queue(&handle
->counter
->pending
,
1795 perf_pending_counter
);
1797 perf_counter_wakeup(handle
->counter
);
1801 * Curious locking construct.
1803 * We need to ensure a later event doesn't publish a head when a former
1804 * event isn't done writing. However since we need to deal with NMIs we
1805 * cannot fully serialize things.
1807 * What we do is serialize between CPUs so we only have to deal with NMI
1808 * nesting on a single CPU.
1810 * We only publish the head (and generate a wakeup) when the outer-most
1813 static void perf_output_lock(struct perf_output_handle
*handle
)
1815 struct perf_mmap_data
*data
= handle
->data
;
1820 local_irq_save(handle
->flags
);
1821 cpu
= smp_processor_id();
1823 if (in_nmi() && atomic_read(&data
->lock
) == cpu
)
1826 while (atomic_cmpxchg(&data
->lock
, -1, cpu
) != -1)
1832 static void perf_output_unlock(struct perf_output_handle
*handle
)
1834 struct perf_mmap_data
*data
= handle
->data
;
1837 data
->done_head
= data
->head
;
1839 if (!handle
->locked
)
1844 * The xchg implies a full barrier that ensures all writes are done
1845 * before we publish the new head, matched by a rmb() in userspace when
1846 * reading this position.
1848 while ((head
= atomic_xchg(&data
->done_head
, 0)))
1849 data
->user_page
->data_head
= head
;
1852 * NMI can happen here, which means we can miss a done_head update.
1855 cpu
= atomic_xchg(&data
->lock
, -1);
1856 WARN_ON_ONCE(cpu
!= smp_processor_id());
1859 * Therefore we have to validate we did not indeed do so.
1861 if (unlikely(atomic_read(&data
->done_head
))) {
1863 * Since we had it locked, we can lock it again.
1865 while (atomic_cmpxchg(&data
->lock
, -1, cpu
) != -1)
1871 if (atomic_xchg(&data
->wakeup
, 0))
1872 perf_output_wakeup(handle
);
1874 local_irq_restore(handle
->flags
);
1877 static int perf_output_begin(struct perf_output_handle
*handle
,
1878 struct perf_counter
*counter
, unsigned int size
,
1879 int nmi
, int overflow
)
1881 struct perf_mmap_data
*data
;
1882 unsigned int offset
, head
;
1885 * For inherited counters we send all the output towards the parent.
1887 if (counter
->parent
)
1888 counter
= counter
->parent
;
1891 data
= rcu_dereference(counter
->data
);
1895 handle
->data
= data
;
1896 handle
->counter
= counter
;
1898 handle
->overflow
= overflow
;
1900 if (!data
->nr_pages
)
1903 perf_output_lock(handle
);
1906 offset
= head
= atomic_read(&data
->head
);
1908 } while (atomic_cmpxchg(&data
->head
, offset
, head
) != offset
);
1910 handle
->offset
= offset
;
1911 handle
->head
= head
;
1913 if ((offset
>> PAGE_SHIFT
) != (head
>> PAGE_SHIFT
))
1914 atomic_set(&data
->wakeup
, 1);
1919 perf_output_wakeup(handle
);
1926 static void perf_output_copy(struct perf_output_handle
*handle
,
1927 void *buf
, unsigned int len
)
1929 unsigned int pages_mask
;
1930 unsigned int offset
;
1934 offset
= handle
->offset
;
1935 pages_mask
= handle
->data
->nr_pages
- 1;
1936 pages
= handle
->data
->data_pages
;
1939 unsigned int page_offset
;
1942 nr
= (offset
>> PAGE_SHIFT
) & pages_mask
;
1943 page_offset
= offset
& (PAGE_SIZE
- 1);
1944 size
= min_t(unsigned int, PAGE_SIZE
- page_offset
, len
);
1946 memcpy(pages
[nr
] + page_offset
, buf
, size
);
1953 handle
->offset
= offset
;
1956 * Check we didn't copy past our reservation window, taking the
1957 * possible unsigned int wrap into account.
1959 WARN_ON_ONCE(((int)(handle
->head
- handle
->offset
)) < 0);
1962 #define perf_output_put(handle, x) \
1963 perf_output_copy((handle), &(x), sizeof(x))
1965 static void perf_output_end(struct perf_output_handle
*handle
)
1967 struct perf_counter
*counter
= handle
->counter
;
1968 struct perf_mmap_data
*data
= handle
->data
;
1970 int wakeup_events
= counter
->hw_event
.wakeup_events
;
1972 if (handle
->overflow
&& wakeup_events
) {
1973 int events
= atomic_inc_return(&data
->events
);
1974 if (events
>= wakeup_events
) {
1975 atomic_sub(wakeup_events
, &data
->events
);
1976 atomic_set(&data
->wakeup
, 1);
1980 perf_output_unlock(handle
);
1984 static void perf_counter_output(struct perf_counter
*counter
,
1985 int nmi
, struct pt_regs
*regs
, u64 addr
)
1988 u64 record_type
= counter
->hw_event
.record_type
;
1989 struct perf_output_handle handle
;
1990 struct perf_event_header header
;
1999 struct perf_callchain_entry
*callchain
= NULL
;
2000 int callchain_size
= 0;
2007 header
.size
= sizeof(header
);
2009 header
.misc
= PERF_EVENT_MISC_OVERFLOW
;
2010 header
.misc
|= user_mode(regs
) ?
2011 PERF_EVENT_MISC_USER
: PERF_EVENT_MISC_KERNEL
;
2013 if (record_type
& PERF_RECORD_IP
) {
2014 ip
= instruction_pointer(regs
);
2015 header
.type
|= PERF_RECORD_IP
;
2016 header
.size
+= sizeof(ip
);
2019 if (record_type
& PERF_RECORD_TID
) {
2020 /* namespace issues */
2021 tid_entry
.pid
= current
->group_leader
->pid
;
2022 tid_entry
.tid
= current
->pid
;
2024 header
.type
|= PERF_RECORD_TID
;
2025 header
.size
+= sizeof(tid_entry
);
2028 if (record_type
& PERF_RECORD_TIME
) {
2030 * Maybe do better on x86 and provide cpu_clock_nmi()
2032 time
= sched_clock();
2034 header
.type
|= PERF_RECORD_TIME
;
2035 header
.size
+= sizeof(u64
);
2038 if (record_type
& PERF_RECORD_ADDR
) {
2039 header
.type
|= PERF_RECORD_ADDR
;
2040 header
.size
+= sizeof(u64
);
2043 if (record_type
& PERF_RECORD_CONFIG
) {
2044 header
.type
|= PERF_RECORD_CONFIG
;
2045 header
.size
+= sizeof(u64
);
2048 if (record_type
& PERF_RECORD_CPU
) {
2049 header
.type
|= PERF_RECORD_CPU
;
2050 header
.size
+= sizeof(cpu_entry
);
2052 cpu_entry
.cpu
= raw_smp_processor_id();
2055 if (record_type
& PERF_RECORD_GROUP
) {
2056 header
.type
|= PERF_RECORD_GROUP
;
2057 header
.size
+= sizeof(u64
) +
2058 counter
->nr_siblings
* sizeof(group_entry
);
2061 if (record_type
& PERF_RECORD_CALLCHAIN
) {
2062 callchain
= perf_callchain(regs
);
2065 callchain_size
= (1 + callchain
->nr
) * sizeof(u64
);
2067 header
.type
|= PERF_RECORD_CALLCHAIN
;
2068 header
.size
+= callchain_size
;
2072 ret
= perf_output_begin(&handle
, counter
, header
.size
, nmi
, 1);
2076 perf_output_put(&handle
, header
);
2078 if (record_type
& PERF_RECORD_IP
)
2079 perf_output_put(&handle
, ip
);
2081 if (record_type
& PERF_RECORD_TID
)
2082 perf_output_put(&handle
, tid_entry
);
2084 if (record_type
& PERF_RECORD_TIME
)
2085 perf_output_put(&handle
, time
);
2087 if (record_type
& PERF_RECORD_ADDR
)
2088 perf_output_put(&handle
, addr
);
2090 if (record_type
& PERF_RECORD_CONFIG
)
2091 perf_output_put(&handle
, counter
->hw_event
.config
);
2093 if (record_type
& PERF_RECORD_CPU
)
2094 perf_output_put(&handle
, cpu_entry
);
2097 * XXX PERF_RECORD_GROUP vs inherited counters seems difficult.
2099 if (record_type
& PERF_RECORD_GROUP
) {
2100 struct perf_counter
*leader
, *sub
;
2101 u64 nr
= counter
->nr_siblings
;
2103 perf_output_put(&handle
, nr
);
2105 leader
= counter
->group_leader
;
2106 list_for_each_entry(sub
, &leader
->sibling_list
, list_entry
) {
2108 sub
->pmu
->read(sub
);
2110 group_entry
.event
= sub
->hw_event
.config
;
2111 group_entry
.counter
= atomic64_read(&sub
->count
);
2113 perf_output_put(&handle
, group_entry
);
2118 perf_output_copy(&handle
, callchain
, callchain_size
);
2120 perf_output_end(&handle
);
2127 struct perf_comm_event
{
2128 struct task_struct
*task
;
2133 struct perf_event_header header
;
2140 static void perf_counter_comm_output(struct perf_counter
*counter
,
2141 struct perf_comm_event
*comm_event
)
2143 struct perf_output_handle handle
;
2144 int size
= comm_event
->event
.header
.size
;
2145 int ret
= perf_output_begin(&handle
, counter
, size
, 0, 0);
2150 perf_output_put(&handle
, comm_event
->event
);
2151 perf_output_copy(&handle
, comm_event
->comm
,
2152 comm_event
->comm_size
);
2153 perf_output_end(&handle
);
2156 static int perf_counter_comm_match(struct perf_counter
*counter
,
2157 struct perf_comm_event
*comm_event
)
2159 if (counter
->hw_event
.comm
&&
2160 comm_event
->event
.header
.type
== PERF_EVENT_COMM
)
2166 static void perf_counter_comm_ctx(struct perf_counter_context
*ctx
,
2167 struct perf_comm_event
*comm_event
)
2169 struct perf_counter
*counter
;
2171 if (system_state
!= SYSTEM_RUNNING
|| list_empty(&ctx
->event_list
))
2175 list_for_each_entry_rcu(counter
, &ctx
->event_list
, event_entry
) {
2176 if (perf_counter_comm_match(counter
, comm_event
))
2177 perf_counter_comm_output(counter
, comm_event
);
2182 static void perf_counter_comm_event(struct perf_comm_event
*comm_event
)
2184 struct perf_cpu_context
*cpuctx
;
2186 char *comm
= comm_event
->task
->comm
;
2188 size
= ALIGN(strlen(comm
)+1, sizeof(u64
));
2190 comm_event
->comm
= comm
;
2191 comm_event
->comm_size
= size
;
2193 comm_event
->event
.header
.size
= sizeof(comm_event
->event
) + size
;
2195 cpuctx
= &get_cpu_var(perf_cpu_context
);
2196 perf_counter_comm_ctx(&cpuctx
->ctx
, comm_event
);
2197 put_cpu_var(perf_cpu_context
);
2199 perf_counter_comm_ctx(¤t
->perf_counter_ctx
, comm_event
);
2202 void perf_counter_comm(struct task_struct
*task
)
2204 struct perf_comm_event comm_event
;
2206 if (!atomic_read(&nr_comm_tracking
))
2209 comm_event
= (struct perf_comm_event
){
2212 .header
= { .type
= PERF_EVENT_COMM
, },
2213 .pid
= task
->group_leader
->pid
,
2218 perf_counter_comm_event(&comm_event
);
2225 struct perf_mmap_event
{
2231 struct perf_event_header header
;
2241 static void perf_counter_mmap_output(struct perf_counter
*counter
,
2242 struct perf_mmap_event
*mmap_event
)
2244 struct perf_output_handle handle
;
2245 int size
= mmap_event
->event
.header
.size
;
2246 int ret
= perf_output_begin(&handle
, counter
, size
, 0, 0);
2251 perf_output_put(&handle
, mmap_event
->event
);
2252 perf_output_copy(&handle
, mmap_event
->file_name
,
2253 mmap_event
->file_size
);
2254 perf_output_end(&handle
);
2257 static int perf_counter_mmap_match(struct perf_counter
*counter
,
2258 struct perf_mmap_event
*mmap_event
)
2260 if (counter
->hw_event
.mmap
&&
2261 mmap_event
->event
.header
.type
== PERF_EVENT_MMAP
)
2264 if (counter
->hw_event
.munmap
&&
2265 mmap_event
->event
.header
.type
== PERF_EVENT_MUNMAP
)
2271 static void perf_counter_mmap_ctx(struct perf_counter_context
*ctx
,
2272 struct perf_mmap_event
*mmap_event
)
2274 struct perf_counter
*counter
;
2276 if (system_state
!= SYSTEM_RUNNING
|| list_empty(&ctx
->event_list
))
2280 list_for_each_entry_rcu(counter
, &ctx
->event_list
, event_entry
) {
2281 if (perf_counter_mmap_match(counter
, mmap_event
))
2282 perf_counter_mmap_output(counter
, mmap_event
);
2287 static void perf_counter_mmap_event(struct perf_mmap_event
*mmap_event
)
2289 struct perf_cpu_context
*cpuctx
;
2290 struct file
*file
= mmap_event
->file
;
2297 buf
= kzalloc(PATH_MAX
, GFP_KERNEL
);
2299 name
= strncpy(tmp
, "//enomem", sizeof(tmp
));
2302 name
= d_path(&file
->f_path
, buf
, PATH_MAX
);
2304 name
= strncpy(tmp
, "//toolong", sizeof(tmp
));
2308 name
= strncpy(tmp
, "//anon", sizeof(tmp
));
2313 size
= ALIGN(strlen(name
)+1, sizeof(u64
));
2315 mmap_event
->file_name
= name
;
2316 mmap_event
->file_size
= size
;
2318 mmap_event
->event
.header
.size
= sizeof(mmap_event
->event
) + size
;
2320 cpuctx
= &get_cpu_var(perf_cpu_context
);
2321 perf_counter_mmap_ctx(&cpuctx
->ctx
, mmap_event
);
2322 put_cpu_var(perf_cpu_context
);
2324 perf_counter_mmap_ctx(¤t
->perf_counter_ctx
, mmap_event
);
2329 void perf_counter_mmap(unsigned long addr
, unsigned long len
,
2330 unsigned long pgoff
, struct file
*file
)
2332 struct perf_mmap_event mmap_event
;
2334 if (!atomic_read(&nr_mmap_tracking
))
2337 mmap_event
= (struct perf_mmap_event
){
2340 .header
= { .type
= PERF_EVENT_MMAP
, },
2341 .pid
= current
->group_leader
->pid
,
2342 .tid
= current
->pid
,
2349 perf_counter_mmap_event(&mmap_event
);
2352 void perf_counter_munmap(unsigned long addr
, unsigned long len
,
2353 unsigned long pgoff
, struct file
*file
)
2355 struct perf_mmap_event mmap_event
;
2357 if (!atomic_read(&nr_munmap_tracking
))
2360 mmap_event
= (struct perf_mmap_event
){
2363 .header
= { .type
= PERF_EVENT_MUNMAP
, },
2364 .pid
= current
->group_leader
->pid
,
2365 .tid
= current
->pid
,
2372 perf_counter_mmap_event(&mmap_event
);
2376 * Generic counter overflow handling.
2379 int perf_counter_overflow(struct perf_counter
*counter
,
2380 int nmi
, struct pt_regs
*regs
, u64 addr
)
2382 int events
= atomic_read(&counter
->event_limit
);
2386 * XXX event_limit might not quite work as expected on inherited
2390 counter
->pending_kill
= POLL_IN
;
2391 if (events
&& atomic_dec_and_test(&counter
->event_limit
)) {
2393 counter
->pending_kill
= POLL_HUP
;
2395 counter
->pending_disable
= 1;
2396 perf_pending_queue(&counter
->pending
,
2397 perf_pending_counter
);
2399 perf_counter_disable(counter
);
2402 perf_counter_output(counter
, nmi
, regs
, addr
);
2407 * Generic software counter infrastructure
2410 static void perf_swcounter_update(struct perf_counter
*counter
)
2412 struct hw_perf_counter
*hwc
= &counter
->hw
;
2417 prev
= atomic64_read(&hwc
->prev_count
);
2418 now
= atomic64_read(&hwc
->count
);
2419 if (atomic64_cmpxchg(&hwc
->prev_count
, prev
, now
) != prev
)
2424 atomic64_add(delta
, &counter
->count
);
2425 atomic64_sub(delta
, &hwc
->period_left
);
2428 static void perf_swcounter_set_period(struct perf_counter
*counter
)
2430 struct hw_perf_counter
*hwc
= &counter
->hw
;
2431 s64 left
= atomic64_read(&hwc
->period_left
);
2432 s64 period
= hwc
->irq_period
;
2434 if (unlikely(left
<= -period
)) {
2436 atomic64_set(&hwc
->period_left
, left
);
2439 if (unlikely(left
<= 0)) {
2441 atomic64_add(period
, &hwc
->period_left
);
2444 atomic64_set(&hwc
->prev_count
, -left
);
2445 atomic64_set(&hwc
->count
, -left
);
2448 static enum hrtimer_restart
perf_swcounter_hrtimer(struct hrtimer
*hrtimer
)
2450 enum hrtimer_restart ret
= HRTIMER_RESTART
;
2451 struct perf_counter
*counter
;
2452 struct pt_regs
*regs
;
2454 counter
= container_of(hrtimer
, struct perf_counter
, hw
.hrtimer
);
2455 counter
->pmu
->read(counter
);
2457 regs
= get_irq_regs();
2459 * In case we exclude kernel IPs or are somehow not in interrupt
2460 * context, provide the next best thing, the user IP.
2462 if ((counter
->hw_event
.exclude_kernel
|| !regs
) &&
2463 !counter
->hw_event
.exclude_user
)
2464 regs
= task_pt_regs(current
);
2467 if (perf_counter_overflow(counter
, 0, regs
, 0))
2468 ret
= HRTIMER_NORESTART
;
2471 hrtimer_forward_now(hrtimer
, ns_to_ktime(counter
->hw
.irq_period
));
2476 static void perf_swcounter_overflow(struct perf_counter
*counter
,
2477 int nmi
, struct pt_regs
*regs
, u64 addr
)
2479 perf_swcounter_update(counter
);
2480 perf_swcounter_set_period(counter
);
2481 if (perf_counter_overflow(counter
, nmi
, regs
, addr
))
2482 /* soft-disable the counter */
2487 static int perf_swcounter_match(struct perf_counter
*counter
,
2488 enum perf_event_types type
,
2489 u32 event
, struct pt_regs
*regs
)
2491 if (counter
->state
!= PERF_COUNTER_STATE_ACTIVE
)
2494 if (perf_event_raw(&counter
->hw_event
))
2497 if (perf_event_type(&counter
->hw_event
) != type
)
2500 if (perf_event_id(&counter
->hw_event
) != event
)
2503 if (counter
->hw_event
.exclude_user
&& user_mode(regs
))
2506 if (counter
->hw_event
.exclude_kernel
&& !user_mode(regs
))
2512 static void perf_swcounter_add(struct perf_counter
*counter
, u64 nr
,
2513 int nmi
, struct pt_regs
*regs
, u64 addr
)
2515 int neg
= atomic64_add_negative(nr
, &counter
->hw
.count
);
2516 if (counter
->hw
.irq_period
&& !neg
)
2517 perf_swcounter_overflow(counter
, nmi
, regs
, addr
);
2520 static void perf_swcounter_ctx_event(struct perf_counter_context
*ctx
,
2521 enum perf_event_types type
, u32 event
,
2522 u64 nr
, int nmi
, struct pt_regs
*regs
,
2525 struct perf_counter
*counter
;
2527 if (system_state
!= SYSTEM_RUNNING
|| list_empty(&ctx
->event_list
))
2531 list_for_each_entry_rcu(counter
, &ctx
->event_list
, event_entry
) {
2532 if (perf_swcounter_match(counter
, type
, event
, regs
))
2533 perf_swcounter_add(counter
, nr
, nmi
, regs
, addr
);
2538 static int *perf_swcounter_recursion_context(struct perf_cpu_context
*cpuctx
)
2541 return &cpuctx
->recursion
[3];
2544 return &cpuctx
->recursion
[2];
2547 return &cpuctx
->recursion
[1];
2549 return &cpuctx
->recursion
[0];
2552 static void __perf_swcounter_event(enum perf_event_types type
, u32 event
,
2553 u64 nr
, int nmi
, struct pt_regs
*regs
,
2556 struct perf_cpu_context
*cpuctx
= &get_cpu_var(perf_cpu_context
);
2557 int *recursion
= perf_swcounter_recursion_context(cpuctx
);
2565 perf_swcounter_ctx_event(&cpuctx
->ctx
, type
, event
,
2566 nr
, nmi
, regs
, addr
);
2567 if (cpuctx
->task_ctx
) {
2568 perf_swcounter_ctx_event(cpuctx
->task_ctx
, type
, event
,
2569 nr
, nmi
, regs
, addr
);
2576 put_cpu_var(perf_cpu_context
);
2580 perf_swcounter_event(u32 event
, u64 nr
, int nmi
, struct pt_regs
*regs
, u64 addr
)
2582 __perf_swcounter_event(PERF_TYPE_SOFTWARE
, event
, nr
, nmi
, regs
, addr
);
2585 static void perf_swcounter_read(struct perf_counter
*counter
)
2587 perf_swcounter_update(counter
);
2590 static int perf_swcounter_enable(struct perf_counter
*counter
)
2592 perf_swcounter_set_period(counter
);
2596 static void perf_swcounter_disable(struct perf_counter
*counter
)
2598 perf_swcounter_update(counter
);
2601 static const struct pmu perf_ops_generic
= {
2602 .enable
= perf_swcounter_enable
,
2603 .disable
= perf_swcounter_disable
,
2604 .read
= perf_swcounter_read
,
2608 * Software counter: cpu wall time clock
2611 static void cpu_clock_perf_counter_update(struct perf_counter
*counter
)
2613 int cpu
= raw_smp_processor_id();
2617 now
= cpu_clock(cpu
);
2618 prev
= atomic64_read(&counter
->hw
.prev_count
);
2619 atomic64_set(&counter
->hw
.prev_count
, now
);
2620 atomic64_add(now
- prev
, &counter
->count
);
2623 static int cpu_clock_perf_counter_enable(struct perf_counter
*counter
)
2625 struct hw_perf_counter
*hwc
= &counter
->hw
;
2626 int cpu
= raw_smp_processor_id();
2628 atomic64_set(&hwc
->prev_count
, cpu_clock(cpu
));
2629 hrtimer_init(&hwc
->hrtimer
, CLOCK_MONOTONIC
, HRTIMER_MODE_REL
);
2630 hwc
->hrtimer
.function
= perf_swcounter_hrtimer
;
2631 if (hwc
->irq_period
) {
2632 __hrtimer_start_range_ns(&hwc
->hrtimer
,
2633 ns_to_ktime(hwc
->irq_period
), 0,
2634 HRTIMER_MODE_REL
, 0);
2640 static void cpu_clock_perf_counter_disable(struct perf_counter
*counter
)
2642 hrtimer_cancel(&counter
->hw
.hrtimer
);
2643 cpu_clock_perf_counter_update(counter
);
2646 static void cpu_clock_perf_counter_read(struct perf_counter
*counter
)
2648 cpu_clock_perf_counter_update(counter
);
2651 static const struct pmu perf_ops_cpu_clock
= {
2652 .enable
= cpu_clock_perf_counter_enable
,
2653 .disable
= cpu_clock_perf_counter_disable
,
2654 .read
= cpu_clock_perf_counter_read
,
2658 * Software counter: task time clock
2661 static void task_clock_perf_counter_update(struct perf_counter
*counter
, u64 now
)
2666 prev
= atomic64_xchg(&counter
->hw
.prev_count
, now
);
2668 atomic64_add(delta
, &counter
->count
);
2671 static int task_clock_perf_counter_enable(struct perf_counter
*counter
)
2673 struct hw_perf_counter
*hwc
= &counter
->hw
;
2676 now
= counter
->ctx
->time
;
2678 atomic64_set(&hwc
->prev_count
, now
);
2679 hrtimer_init(&hwc
->hrtimer
, CLOCK_MONOTONIC
, HRTIMER_MODE_REL
);
2680 hwc
->hrtimer
.function
= perf_swcounter_hrtimer
;
2681 if (hwc
->irq_period
) {
2682 __hrtimer_start_range_ns(&hwc
->hrtimer
,
2683 ns_to_ktime(hwc
->irq_period
), 0,
2684 HRTIMER_MODE_REL
, 0);
2690 static void task_clock_perf_counter_disable(struct perf_counter
*counter
)
2692 hrtimer_cancel(&counter
->hw
.hrtimer
);
2693 task_clock_perf_counter_update(counter
, counter
->ctx
->time
);
2697 static void task_clock_perf_counter_read(struct perf_counter
*counter
)
2702 update_context_time(counter
->ctx
);
2703 time
= counter
->ctx
->time
;
2705 u64 now
= perf_clock();
2706 u64 delta
= now
- counter
->ctx
->timestamp
;
2707 time
= counter
->ctx
->time
+ delta
;
2710 task_clock_perf_counter_update(counter
, time
);
2713 static const struct pmu perf_ops_task_clock
= {
2714 .enable
= task_clock_perf_counter_enable
,
2715 .disable
= task_clock_perf_counter_disable
,
2716 .read
= task_clock_perf_counter_read
,
2720 * Software counter: cpu migrations
2723 static inline u64
get_cpu_migrations(struct perf_counter
*counter
)
2725 struct task_struct
*curr
= counter
->ctx
->task
;
2728 return curr
->se
.nr_migrations
;
2729 return cpu_nr_migrations(smp_processor_id());
2732 static void cpu_migrations_perf_counter_update(struct perf_counter
*counter
)
2737 prev
= atomic64_read(&counter
->hw
.prev_count
);
2738 now
= get_cpu_migrations(counter
);
2740 atomic64_set(&counter
->hw
.prev_count
, now
);
2744 atomic64_add(delta
, &counter
->count
);
2747 static void cpu_migrations_perf_counter_read(struct perf_counter
*counter
)
2749 cpu_migrations_perf_counter_update(counter
);
2752 static int cpu_migrations_perf_counter_enable(struct perf_counter
*counter
)
2754 if (counter
->prev_state
<= PERF_COUNTER_STATE_OFF
)
2755 atomic64_set(&counter
->hw
.prev_count
,
2756 get_cpu_migrations(counter
));
2760 static void cpu_migrations_perf_counter_disable(struct perf_counter
*counter
)
2762 cpu_migrations_perf_counter_update(counter
);
2765 static const struct pmu perf_ops_cpu_migrations
= {
2766 .enable
= cpu_migrations_perf_counter_enable
,
2767 .disable
= cpu_migrations_perf_counter_disable
,
2768 .read
= cpu_migrations_perf_counter_read
,
2771 #ifdef CONFIG_EVENT_PROFILE
2772 void perf_tpcounter_event(int event_id
)
2774 struct pt_regs
*regs
= get_irq_regs();
2777 regs
= task_pt_regs(current
);
2779 __perf_swcounter_event(PERF_TYPE_TRACEPOINT
, event_id
, 1, 1, regs
, 0);
2781 EXPORT_SYMBOL_GPL(perf_tpcounter_event
);
2783 extern int ftrace_profile_enable(int);
2784 extern void ftrace_profile_disable(int);
2786 static void tp_perf_counter_destroy(struct perf_counter
*counter
)
2788 ftrace_profile_disable(perf_event_id(&counter
->hw_event
));
2791 static const struct pmu
*tp_perf_counter_init(struct perf_counter
*counter
)
2793 int event_id
= perf_event_id(&counter
->hw_event
);
2796 ret
= ftrace_profile_enable(event_id
);
2800 counter
->destroy
= tp_perf_counter_destroy
;
2801 counter
->hw
.irq_period
= counter
->hw_event
.irq_period
;
2803 return &perf_ops_generic
;
2806 static const struct pmu
*tp_perf_counter_init(struct perf_counter
*counter
)
2812 static const struct pmu
*sw_perf_counter_init(struct perf_counter
*counter
)
2814 struct perf_counter_hw_event
*hw_event
= &counter
->hw_event
;
2815 const struct pmu
*pmu
= NULL
;
2816 struct hw_perf_counter
*hwc
= &counter
->hw
;
2819 * Software counters (currently) can't in general distinguish
2820 * between user, kernel and hypervisor events.
2821 * However, context switches and cpu migrations are considered
2822 * to be kernel events, and page faults are never hypervisor
2825 switch (perf_event_id(&counter
->hw_event
)) {
2826 case PERF_COUNT_CPU_CLOCK
:
2827 pmu
= &perf_ops_cpu_clock
;
2829 if (hw_event
->irq_period
&& hw_event
->irq_period
< 10000)
2830 hw_event
->irq_period
= 10000;
2832 case PERF_COUNT_TASK_CLOCK
:
2834 * If the user instantiates this as a per-cpu counter,
2835 * use the cpu_clock counter instead.
2837 if (counter
->ctx
->task
)
2838 pmu
= &perf_ops_task_clock
;
2840 pmu
= &perf_ops_cpu_clock
;
2842 if (hw_event
->irq_period
&& hw_event
->irq_period
< 10000)
2843 hw_event
->irq_period
= 10000;
2845 case PERF_COUNT_PAGE_FAULTS
:
2846 case PERF_COUNT_PAGE_FAULTS_MIN
:
2847 case PERF_COUNT_PAGE_FAULTS_MAJ
:
2848 case PERF_COUNT_CONTEXT_SWITCHES
:
2849 pmu
= &perf_ops_generic
;
2851 case PERF_COUNT_CPU_MIGRATIONS
:
2852 if (!counter
->hw_event
.exclude_kernel
)
2853 pmu
= &perf_ops_cpu_migrations
;
2858 hwc
->irq_period
= hw_event
->irq_period
;
2864 * Allocate and initialize a counter structure
2866 static struct perf_counter
*
2867 perf_counter_alloc(struct perf_counter_hw_event
*hw_event
,
2869 struct perf_counter_context
*ctx
,
2870 struct perf_counter
*group_leader
,
2873 const struct pmu
*pmu
;
2874 struct perf_counter
*counter
;
2877 counter
= kzalloc(sizeof(*counter
), gfpflags
);
2879 return ERR_PTR(-ENOMEM
);
2882 * Single counters are their own group leaders, with an
2883 * empty sibling list:
2886 group_leader
= counter
;
2888 mutex_init(&counter
->mutex
);
2889 INIT_LIST_HEAD(&counter
->list_entry
);
2890 INIT_LIST_HEAD(&counter
->event_entry
);
2891 INIT_LIST_HEAD(&counter
->sibling_list
);
2892 init_waitqueue_head(&counter
->waitq
);
2894 mutex_init(&counter
->mmap_mutex
);
2896 INIT_LIST_HEAD(&counter
->child_list
);
2899 counter
->hw_event
= *hw_event
;
2900 counter
->group_leader
= group_leader
;
2901 counter
->pmu
= NULL
;
2904 counter
->state
= PERF_COUNTER_STATE_INACTIVE
;
2905 if (hw_event
->disabled
)
2906 counter
->state
= PERF_COUNTER_STATE_OFF
;
2911 * we currently do not support PERF_RECORD_GROUP on inherited counters
2913 if (hw_event
->inherit
&& (hw_event
->record_type
& PERF_RECORD_GROUP
))
2916 if (perf_event_raw(hw_event
)) {
2917 pmu
= hw_perf_counter_init(counter
);
2921 switch (perf_event_type(hw_event
)) {
2922 case PERF_TYPE_HARDWARE
:
2923 pmu
= hw_perf_counter_init(counter
);
2926 case PERF_TYPE_SOFTWARE
:
2927 pmu
= sw_perf_counter_init(counter
);
2930 case PERF_TYPE_TRACEPOINT
:
2931 pmu
= tp_perf_counter_init(counter
);
2938 else if (IS_ERR(pmu
))
2943 return ERR_PTR(err
);
2948 atomic_inc(&nr_counters
);
2949 if (counter
->hw_event
.mmap
)
2950 atomic_inc(&nr_mmap_tracking
);
2951 if (counter
->hw_event
.munmap
)
2952 atomic_inc(&nr_munmap_tracking
);
2953 if (counter
->hw_event
.comm
)
2954 atomic_inc(&nr_comm_tracking
);
2960 * sys_perf_counter_open - open a performance counter, associate it to a task/cpu
2962 * @hw_event_uptr: event type attributes for monitoring/sampling
2965 * @group_fd: group leader counter fd
2967 SYSCALL_DEFINE5(perf_counter_open
,
2968 const struct perf_counter_hw_event __user
*, hw_event_uptr
,
2969 pid_t
, pid
, int, cpu
, int, group_fd
, unsigned long, flags
)
2971 struct perf_counter
*counter
, *group_leader
;
2972 struct perf_counter_hw_event hw_event
;
2973 struct perf_counter_context
*ctx
;
2974 struct file
*counter_file
= NULL
;
2975 struct file
*group_file
= NULL
;
2976 int fput_needed
= 0;
2977 int fput_needed2
= 0;
2980 /* for future expandability... */
2984 if (copy_from_user(&hw_event
, hw_event_uptr
, sizeof(hw_event
)) != 0)
2988 * Get the target context (task or percpu):
2990 ctx
= find_get_context(pid
, cpu
);
2992 return PTR_ERR(ctx
);
2995 * Look up the group leader (we will attach this counter to it):
2997 group_leader
= NULL
;
2998 if (group_fd
!= -1) {
3000 group_file
= fget_light(group_fd
, &fput_needed
);
3002 goto err_put_context
;
3003 if (group_file
->f_op
!= &perf_fops
)
3004 goto err_put_context
;
3006 group_leader
= group_file
->private_data
;
3008 * Do not allow a recursive hierarchy (this new sibling
3009 * becoming part of another group-sibling):
3011 if (group_leader
->group_leader
!= group_leader
)
3012 goto err_put_context
;
3014 * Do not allow to attach to a group in a different
3015 * task or CPU context:
3017 if (group_leader
->ctx
!= ctx
)
3018 goto err_put_context
;
3020 * Only a group leader can be exclusive or pinned
3022 if (hw_event
.exclusive
|| hw_event
.pinned
)
3023 goto err_put_context
;
3026 counter
= perf_counter_alloc(&hw_event
, cpu
, ctx
, group_leader
,
3028 ret
= PTR_ERR(counter
);
3029 if (IS_ERR(counter
))
3030 goto err_put_context
;
3032 ret
= anon_inode_getfd("[perf_counter]", &perf_fops
, counter
, 0);
3034 goto err_free_put_context
;
3036 counter_file
= fget_light(ret
, &fput_needed2
);
3038 goto err_free_put_context
;
3040 counter
->filp
= counter_file
;
3041 mutex_lock(&ctx
->mutex
);
3042 perf_install_in_context(ctx
, counter
, cpu
);
3043 mutex_unlock(&ctx
->mutex
);
3045 fput_light(counter_file
, fput_needed2
);
3048 fput_light(group_file
, fput_needed
);
3052 err_free_put_context
:
3062 * Initialize the perf_counter context in a task_struct:
3065 __perf_counter_init_context(struct perf_counter_context
*ctx
,
3066 struct task_struct
*task
)
3068 memset(ctx
, 0, sizeof(*ctx
));
3069 spin_lock_init(&ctx
->lock
);
3070 mutex_init(&ctx
->mutex
);
3071 INIT_LIST_HEAD(&ctx
->counter_list
);
3072 INIT_LIST_HEAD(&ctx
->event_list
);
3077 * inherit a counter from parent task to child task:
3079 static struct perf_counter
*
3080 inherit_counter(struct perf_counter
*parent_counter
,
3081 struct task_struct
*parent
,
3082 struct perf_counter_context
*parent_ctx
,
3083 struct task_struct
*child
,
3084 struct perf_counter
*group_leader
,
3085 struct perf_counter_context
*child_ctx
)
3087 struct perf_counter
*child_counter
;
3090 * Instead of creating recursive hierarchies of counters,
3091 * we link inherited counters back to the original parent,
3092 * which has a filp for sure, which we use as the reference
3095 if (parent_counter
->parent
)
3096 parent_counter
= parent_counter
->parent
;
3098 child_counter
= perf_counter_alloc(&parent_counter
->hw_event
,
3099 parent_counter
->cpu
, child_ctx
,
3100 group_leader
, GFP_KERNEL
);
3101 if (IS_ERR(child_counter
))
3102 return child_counter
;
3105 * Link it up in the child's context:
3107 child_counter
->task
= child
;
3108 add_counter_to_ctx(child_counter
, child_ctx
);
3110 child_counter
->parent
= parent_counter
;
3112 * inherit into child's child as well:
3114 child_counter
->hw_event
.inherit
= 1;
3117 * Get a reference to the parent filp - we will fput it
3118 * when the child counter exits. This is safe to do because
3119 * we are in the parent and we know that the filp still
3120 * exists and has a nonzero count:
3122 atomic_long_inc(&parent_counter
->filp
->f_count
);
3125 * Link this into the parent counter's child list
3127 mutex_lock(&parent_counter
->mutex
);
3128 list_add_tail(&child_counter
->child_list
, &parent_counter
->child_list
);
3131 * Make the child state follow the state of the parent counter,
3132 * not its hw_event.disabled bit. We hold the parent's mutex,
3133 * so we won't race with perf_counter_{en,dis}able_family.
3135 if (parent_counter
->state
>= PERF_COUNTER_STATE_INACTIVE
)
3136 child_counter
->state
= PERF_COUNTER_STATE_INACTIVE
;
3138 child_counter
->state
= PERF_COUNTER_STATE_OFF
;
3140 mutex_unlock(&parent_counter
->mutex
);
3142 return child_counter
;
3145 static int inherit_group(struct perf_counter
*parent_counter
,
3146 struct task_struct
*parent
,
3147 struct perf_counter_context
*parent_ctx
,
3148 struct task_struct
*child
,
3149 struct perf_counter_context
*child_ctx
)
3151 struct perf_counter
*leader
;
3152 struct perf_counter
*sub
;
3153 struct perf_counter
*child_ctr
;
3155 leader
= inherit_counter(parent_counter
, parent
, parent_ctx
,
3156 child
, NULL
, child_ctx
);
3158 return PTR_ERR(leader
);
3159 list_for_each_entry(sub
, &parent_counter
->sibling_list
, list_entry
) {
3160 child_ctr
= inherit_counter(sub
, parent
, parent_ctx
,
3161 child
, leader
, child_ctx
);
3162 if (IS_ERR(child_ctr
))
3163 return PTR_ERR(child_ctr
);
3168 static void sync_child_counter(struct perf_counter
*child_counter
,
3169 struct perf_counter
*parent_counter
)
3171 u64 parent_val
, child_val
;
3173 parent_val
= atomic64_read(&parent_counter
->count
);
3174 child_val
= atomic64_read(&child_counter
->count
);
3177 * Add back the child's count to the parent's count:
3179 atomic64_add(child_val
, &parent_counter
->count
);
3180 atomic64_add(child_counter
->total_time_enabled
,
3181 &parent_counter
->child_total_time_enabled
);
3182 atomic64_add(child_counter
->total_time_running
,
3183 &parent_counter
->child_total_time_running
);
3186 * Remove this counter from the parent's list
3188 mutex_lock(&parent_counter
->mutex
);
3189 list_del_init(&child_counter
->child_list
);
3190 mutex_unlock(&parent_counter
->mutex
);
3193 * Release the parent counter, if this was the last
3196 fput(parent_counter
->filp
);
3200 __perf_counter_exit_task(struct task_struct
*child
,
3201 struct perf_counter
*child_counter
,
3202 struct perf_counter_context
*child_ctx
)
3204 struct perf_counter
*parent_counter
;
3205 struct perf_counter
*sub
, *tmp
;
3208 * If we do not self-reap then we have to wait for the
3209 * child task to unschedule (it will happen for sure),
3210 * so that its counter is at its final count. (This
3211 * condition triggers rarely - child tasks usually get
3212 * off their CPU before the parent has a chance to
3213 * get this far into the reaping action)
3215 if (child
!= current
) {
3216 wait_task_inactive(child
, 0);
3217 list_del_init(&child_counter
->list_entry
);
3218 update_counter_times(child_counter
);
3220 struct perf_cpu_context
*cpuctx
;
3221 unsigned long flags
;
3224 * Disable and unlink this counter.
3226 * Be careful about zapping the list - IRQ/NMI context
3227 * could still be processing it:
3229 local_irq_save(flags
);
3232 cpuctx
= &__get_cpu_var(perf_cpu_context
);
3234 group_sched_out(child_counter
, cpuctx
, child_ctx
);
3235 update_counter_times(child_counter
);
3237 list_del_init(&child_counter
->list_entry
);
3239 child_ctx
->nr_counters
--;
3242 local_irq_restore(flags
);
3245 parent_counter
= child_counter
->parent
;
3247 * It can happen that parent exits first, and has counters
3248 * that are still around due to the child reference. These
3249 * counters need to be zapped - but otherwise linger.
3251 if (parent_counter
) {
3252 sync_child_counter(child_counter
, parent_counter
);
3253 list_for_each_entry_safe(sub
, tmp
, &child_counter
->sibling_list
,
3256 sync_child_counter(sub
, sub
->parent
);
3260 free_counter(child_counter
);
3265 * When a child task exits, feed back counter values to parent counters.
3267 * Note: we may be running in child context, but the PID is not hashed
3268 * anymore so new counters will not be added.
3270 void perf_counter_exit_task(struct task_struct
*child
)
3272 struct perf_counter
*child_counter
, *tmp
;
3273 struct perf_counter_context
*child_ctx
;
3275 child_ctx
= &child
->perf_counter_ctx
;
3277 if (likely(!child_ctx
->nr_counters
))
3280 list_for_each_entry_safe(child_counter
, tmp
, &child_ctx
->counter_list
,
3282 __perf_counter_exit_task(child
, child_counter
, child_ctx
);
3286 * Initialize the perf_counter context in task_struct
3288 void perf_counter_init_task(struct task_struct
*child
)
3290 struct perf_counter_context
*child_ctx
, *parent_ctx
;
3291 struct perf_counter
*counter
;
3292 struct task_struct
*parent
= current
;
3294 child_ctx
= &child
->perf_counter_ctx
;
3295 parent_ctx
= &parent
->perf_counter_ctx
;
3297 __perf_counter_init_context(child_ctx
, child
);
3300 * This is executed from the parent task context, so inherit
3301 * counters that have been marked for cloning:
3304 if (likely(!parent_ctx
->nr_counters
))
3308 * Lock the parent list. No need to lock the child - not PID
3309 * hashed yet and not running, so nobody can access it.
3311 mutex_lock(&parent_ctx
->mutex
);
3314 * We dont have to disable NMIs - we are only looking at
3315 * the list, not manipulating it:
3317 list_for_each_entry(counter
, &parent_ctx
->counter_list
, list_entry
) {
3318 if (!counter
->hw_event
.inherit
)
3321 if (inherit_group(counter
, parent
,
3322 parent_ctx
, child
, child_ctx
))
3326 mutex_unlock(&parent_ctx
->mutex
);
3329 static void __cpuinit
perf_counter_init_cpu(int cpu
)
3331 struct perf_cpu_context
*cpuctx
;
3333 cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
3334 __perf_counter_init_context(&cpuctx
->ctx
, NULL
);
3336 spin_lock(&perf_resource_lock
);
3337 cpuctx
->max_pertask
= perf_max_counters
- perf_reserved_percpu
;
3338 spin_unlock(&perf_resource_lock
);
3340 hw_perf_counter_setup(cpu
);
3343 #ifdef CONFIG_HOTPLUG_CPU
3344 static void __perf_counter_exit_cpu(void *info
)
3346 struct perf_cpu_context
*cpuctx
= &__get_cpu_var(perf_cpu_context
);
3347 struct perf_counter_context
*ctx
= &cpuctx
->ctx
;
3348 struct perf_counter
*counter
, *tmp
;
3350 list_for_each_entry_safe(counter
, tmp
, &ctx
->counter_list
, list_entry
)
3351 __perf_counter_remove_from_context(counter
);
3353 static void perf_counter_exit_cpu(int cpu
)
3355 struct perf_cpu_context
*cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
3356 struct perf_counter_context
*ctx
= &cpuctx
->ctx
;
3358 mutex_lock(&ctx
->mutex
);
3359 smp_call_function_single(cpu
, __perf_counter_exit_cpu
, NULL
, 1);
3360 mutex_unlock(&ctx
->mutex
);
3363 static inline void perf_counter_exit_cpu(int cpu
) { }
3366 static int __cpuinit
3367 perf_cpu_notify(struct notifier_block
*self
, unsigned long action
, void *hcpu
)
3369 unsigned int cpu
= (long)hcpu
;
3373 case CPU_UP_PREPARE
:
3374 case CPU_UP_PREPARE_FROZEN
:
3375 perf_counter_init_cpu(cpu
);
3378 case CPU_DOWN_PREPARE
:
3379 case CPU_DOWN_PREPARE_FROZEN
:
3380 perf_counter_exit_cpu(cpu
);
3390 static struct notifier_block __cpuinitdata perf_cpu_nb
= {
3391 .notifier_call
= perf_cpu_notify
,
3394 void __init
perf_counter_init(void)
3396 perf_cpu_notify(&perf_cpu_nb
, (unsigned long)CPU_UP_PREPARE
,
3397 (void *)(long)smp_processor_id());
3398 register_cpu_notifier(&perf_cpu_nb
);
3401 static ssize_t
perf_show_reserve_percpu(struct sysdev_class
*class, char *buf
)
3403 return sprintf(buf
, "%d\n", perf_reserved_percpu
);
3407 perf_set_reserve_percpu(struct sysdev_class
*class,
3411 struct perf_cpu_context
*cpuctx
;
3415 err
= strict_strtoul(buf
, 10, &val
);
3418 if (val
> perf_max_counters
)
3421 spin_lock(&perf_resource_lock
);
3422 perf_reserved_percpu
= val
;
3423 for_each_online_cpu(cpu
) {
3424 cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
3425 spin_lock_irq(&cpuctx
->ctx
.lock
);
3426 mpt
= min(perf_max_counters
- cpuctx
->ctx
.nr_counters
,
3427 perf_max_counters
- perf_reserved_percpu
);
3428 cpuctx
->max_pertask
= mpt
;
3429 spin_unlock_irq(&cpuctx
->ctx
.lock
);
3431 spin_unlock(&perf_resource_lock
);
3436 static ssize_t
perf_show_overcommit(struct sysdev_class
*class, char *buf
)
3438 return sprintf(buf
, "%d\n", perf_overcommit
);
3442 perf_set_overcommit(struct sysdev_class
*class, const char *buf
, size_t count
)
3447 err
= strict_strtoul(buf
, 10, &val
);
3453 spin_lock(&perf_resource_lock
);
3454 perf_overcommit
= val
;
3455 spin_unlock(&perf_resource_lock
);
3460 static SYSDEV_CLASS_ATTR(
3463 perf_show_reserve_percpu
,
3464 perf_set_reserve_percpu
3467 static SYSDEV_CLASS_ATTR(
3470 perf_show_overcommit
,
3474 static struct attribute
*perfclass_attrs
[] = {
3475 &attr_reserve_percpu
.attr
,
3476 &attr_overcommit
.attr
,
3480 static struct attribute_group perfclass_attr_group
= {
3481 .attrs
= perfclass_attrs
,
3482 .name
= "perf_counters",
3485 static int __init
perf_counter_sysfs_init(void)
3487 return sysfs_create_group(&cpu_sysdev_class
.kset
.kobj
,
3488 &perfclass_attr_group
);
3490 device_initcall(perf_counter_sysfs_init
);