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 */
47 int sysctl_perf_counter_mlock __read_mostly
= 128; /* 'free' kb per counter */
50 * Lock for (sysadmin-configurable) counter reservations:
52 static DEFINE_SPINLOCK(perf_resource_lock
);
55 * Architecture provided APIs - weak aliases:
57 extern __weak
const struct pmu
*hw_perf_counter_init(struct perf_counter
*counter
)
62 u64 __weak
hw_perf_save_disable(void) { return 0; }
63 void __weak
hw_perf_restore(u64 ctrl
) { barrier(); }
64 void __weak
hw_perf_counter_setup(int cpu
) { barrier(); }
65 int __weak
hw_perf_group_sched_in(struct perf_counter
*group_leader
,
66 struct perf_cpu_context
*cpuctx
,
67 struct perf_counter_context
*ctx
, int cpu
)
72 void __weak
perf_counter_print_debug(void) { }
75 list_add_counter(struct perf_counter
*counter
, struct perf_counter_context
*ctx
)
77 struct perf_counter
*group_leader
= counter
->group_leader
;
80 * Depending on whether it is a standalone or sibling counter,
81 * add it straight to the context's counter list, or to the group
82 * leader's sibling list:
84 if (counter
->group_leader
== counter
)
85 list_add_tail(&counter
->list_entry
, &ctx
->counter_list
);
87 list_add_tail(&counter
->list_entry
, &group_leader
->sibling_list
);
88 group_leader
->nr_siblings
++;
91 list_add_rcu(&counter
->event_entry
, &ctx
->event_list
);
95 list_del_counter(struct perf_counter
*counter
, struct perf_counter_context
*ctx
)
97 struct perf_counter
*sibling
, *tmp
;
99 list_del_init(&counter
->list_entry
);
100 list_del_rcu(&counter
->event_entry
);
102 if (counter
->group_leader
!= counter
)
103 counter
->group_leader
->nr_siblings
--;
106 * If this was a group counter with sibling counters then
107 * upgrade the siblings to singleton counters by adding them
108 * to the context list directly:
110 list_for_each_entry_safe(sibling
, tmp
,
111 &counter
->sibling_list
, list_entry
) {
113 list_move_tail(&sibling
->list_entry
, &ctx
->counter_list
);
114 sibling
->group_leader
= sibling
;
119 counter_sched_out(struct perf_counter
*counter
,
120 struct perf_cpu_context
*cpuctx
,
121 struct perf_counter_context
*ctx
)
123 if (counter
->state
!= PERF_COUNTER_STATE_ACTIVE
)
126 counter
->state
= PERF_COUNTER_STATE_INACTIVE
;
127 counter
->tstamp_stopped
= ctx
->time
;
128 counter
->pmu
->disable(counter
);
131 if (!is_software_counter(counter
))
132 cpuctx
->active_oncpu
--;
134 if (counter
->hw_event
.exclusive
|| !cpuctx
->active_oncpu
)
135 cpuctx
->exclusive
= 0;
139 group_sched_out(struct perf_counter
*group_counter
,
140 struct perf_cpu_context
*cpuctx
,
141 struct perf_counter_context
*ctx
)
143 struct perf_counter
*counter
;
145 if (group_counter
->state
!= PERF_COUNTER_STATE_ACTIVE
)
148 counter_sched_out(group_counter
, cpuctx
, ctx
);
151 * Schedule out siblings (if any):
153 list_for_each_entry(counter
, &group_counter
->sibling_list
, list_entry
)
154 counter_sched_out(counter
, cpuctx
, ctx
);
156 if (group_counter
->hw_event
.exclusive
)
157 cpuctx
->exclusive
= 0;
161 * Cross CPU call to remove a performance counter
163 * We disable the counter on the hardware level first. After that we
164 * remove it from the context list.
166 static void __perf_counter_remove_from_context(void *info
)
168 struct perf_cpu_context
*cpuctx
= &__get_cpu_var(perf_cpu_context
);
169 struct perf_counter
*counter
= info
;
170 struct perf_counter_context
*ctx
= counter
->ctx
;
175 * If this is a task context, we need to check whether it is
176 * the current task context of this cpu. If not it has been
177 * scheduled out before the smp call arrived.
179 if (ctx
->task
&& cpuctx
->task_ctx
!= ctx
)
182 spin_lock_irqsave(&ctx
->lock
, flags
);
184 counter_sched_out(counter
, cpuctx
, ctx
);
186 counter
->task
= NULL
;
190 * Protect the list operation against NMI by disabling the
191 * counters on a global level. NOP for non NMI based counters.
193 perf_flags
= hw_perf_save_disable();
194 list_del_counter(counter
, ctx
);
195 hw_perf_restore(perf_flags
);
199 * Allow more per task counters with respect to the
202 cpuctx
->max_pertask
=
203 min(perf_max_counters
- ctx
->nr_counters
,
204 perf_max_counters
- perf_reserved_percpu
);
207 spin_unlock_irqrestore(&ctx
->lock
, flags
);
212 * Remove the counter from a task's (or a CPU's) list of counters.
214 * Must be called with counter->mutex and ctx->mutex held.
216 * CPU counters are removed with a smp call. For task counters we only
217 * call when the task is on a CPU.
219 static void perf_counter_remove_from_context(struct perf_counter
*counter
)
221 struct perf_counter_context
*ctx
= counter
->ctx
;
222 struct task_struct
*task
= ctx
->task
;
226 * Per cpu counters are removed via an smp call and
227 * the removal is always sucessful.
229 smp_call_function_single(counter
->cpu
,
230 __perf_counter_remove_from_context
,
236 task_oncpu_function_call(task
, __perf_counter_remove_from_context
,
239 spin_lock_irq(&ctx
->lock
);
241 * If the context is active we need to retry the smp call.
243 if (ctx
->nr_active
&& !list_empty(&counter
->list_entry
)) {
244 spin_unlock_irq(&ctx
->lock
);
249 * The lock prevents that this context is scheduled in so we
250 * can remove the counter safely, if the call above did not
253 if (!list_empty(&counter
->list_entry
)) {
255 list_del_counter(counter
, ctx
);
256 counter
->task
= NULL
;
258 spin_unlock_irq(&ctx
->lock
);
261 static inline u64
perf_clock(void)
263 return cpu_clock(smp_processor_id());
267 * Update the record of the current time in a context.
269 static void update_context_time(struct perf_counter_context
*ctx
)
271 u64 now
= perf_clock();
273 ctx
->time
+= now
- ctx
->timestamp
;
274 ctx
->timestamp
= now
;
278 * Update the total_time_enabled and total_time_running fields for a counter.
280 static void update_counter_times(struct perf_counter
*counter
)
282 struct perf_counter_context
*ctx
= counter
->ctx
;
285 if (counter
->state
< PERF_COUNTER_STATE_INACTIVE
)
288 counter
->total_time_enabled
= ctx
->time
- counter
->tstamp_enabled
;
290 if (counter
->state
== PERF_COUNTER_STATE_INACTIVE
)
291 run_end
= counter
->tstamp_stopped
;
295 counter
->total_time_running
= run_end
- counter
->tstamp_running
;
299 * Update total_time_enabled and total_time_running for all counters in a group.
301 static void update_group_times(struct perf_counter
*leader
)
303 struct perf_counter
*counter
;
305 update_counter_times(leader
);
306 list_for_each_entry(counter
, &leader
->sibling_list
, list_entry
)
307 update_counter_times(counter
);
311 * Cross CPU call to disable a performance counter
313 static void __perf_counter_disable(void *info
)
315 struct perf_counter
*counter
= info
;
316 struct perf_cpu_context
*cpuctx
= &__get_cpu_var(perf_cpu_context
);
317 struct perf_counter_context
*ctx
= counter
->ctx
;
321 * If this is a per-task counter, need to check whether this
322 * counter's task is the current task on this cpu.
324 if (ctx
->task
&& cpuctx
->task_ctx
!= ctx
)
327 spin_lock_irqsave(&ctx
->lock
, flags
);
330 * If the counter is on, turn it off.
331 * If it is in error state, leave it in error state.
333 if (counter
->state
>= PERF_COUNTER_STATE_INACTIVE
) {
334 update_context_time(ctx
);
335 update_counter_times(counter
);
336 if (counter
== counter
->group_leader
)
337 group_sched_out(counter
, cpuctx
, ctx
);
339 counter_sched_out(counter
, cpuctx
, ctx
);
340 counter
->state
= PERF_COUNTER_STATE_OFF
;
343 spin_unlock_irqrestore(&ctx
->lock
, flags
);
349 static void perf_counter_disable(struct perf_counter
*counter
)
351 struct perf_counter_context
*ctx
= counter
->ctx
;
352 struct task_struct
*task
= ctx
->task
;
356 * Disable the counter on the cpu that it's on
358 smp_call_function_single(counter
->cpu
, __perf_counter_disable
,
364 task_oncpu_function_call(task
, __perf_counter_disable
, counter
);
366 spin_lock_irq(&ctx
->lock
);
368 * If the counter is still active, we need to retry the cross-call.
370 if (counter
->state
== PERF_COUNTER_STATE_ACTIVE
) {
371 spin_unlock_irq(&ctx
->lock
);
376 * Since we have the lock this context can't be scheduled
377 * in, so we can change the state safely.
379 if (counter
->state
== PERF_COUNTER_STATE_INACTIVE
) {
380 update_counter_times(counter
);
381 counter
->state
= PERF_COUNTER_STATE_OFF
;
384 spin_unlock_irq(&ctx
->lock
);
388 * Disable a counter and all its children.
390 static void perf_counter_disable_family(struct perf_counter
*counter
)
392 struct perf_counter
*child
;
394 perf_counter_disable(counter
);
397 * Lock the mutex to protect the list of children
399 mutex_lock(&counter
->mutex
);
400 list_for_each_entry(child
, &counter
->child_list
, child_list
)
401 perf_counter_disable(child
);
402 mutex_unlock(&counter
->mutex
);
406 counter_sched_in(struct perf_counter
*counter
,
407 struct perf_cpu_context
*cpuctx
,
408 struct perf_counter_context
*ctx
,
411 if (counter
->state
<= PERF_COUNTER_STATE_OFF
)
414 counter
->state
= PERF_COUNTER_STATE_ACTIVE
;
415 counter
->oncpu
= cpu
; /* TODO: put 'cpu' into cpuctx->cpu */
417 * The new state must be visible before we turn it on in the hardware:
421 if (counter
->pmu
->enable(counter
)) {
422 counter
->state
= PERF_COUNTER_STATE_INACTIVE
;
427 counter
->tstamp_running
+= ctx
->time
- counter
->tstamp_stopped
;
429 if (!is_software_counter(counter
))
430 cpuctx
->active_oncpu
++;
433 if (counter
->hw_event
.exclusive
)
434 cpuctx
->exclusive
= 1;
440 * Return 1 for a group consisting entirely of software counters,
441 * 0 if the group contains any hardware counters.
443 static int is_software_only_group(struct perf_counter
*leader
)
445 struct perf_counter
*counter
;
447 if (!is_software_counter(leader
))
450 list_for_each_entry(counter
, &leader
->sibling_list
, list_entry
)
451 if (!is_software_counter(counter
))
458 * Work out whether we can put this counter group on the CPU now.
460 static int group_can_go_on(struct perf_counter
*counter
,
461 struct perf_cpu_context
*cpuctx
,
465 * Groups consisting entirely of software counters can always go on.
467 if (is_software_only_group(counter
))
470 * If an exclusive group is already on, no other hardware
471 * counters can go on.
473 if (cpuctx
->exclusive
)
476 * If this group is exclusive and there are already
477 * counters on the CPU, it can't go on.
479 if (counter
->hw_event
.exclusive
&& cpuctx
->active_oncpu
)
482 * Otherwise, try to add it if all previous groups were able
488 static void add_counter_to_ctx(struct perf_counter
*counter
,
489 struct perf_counter_context
*ctx
)
491 list_add_counter(counter
, ctx
);
493 counter
->prev_state
= PERF_COUNTER_STATE_OFF
;
494 counter
->tstamp_enabled
= ctx
->time
;
495 counter
->tstamp_running
= ctx
->time
;
496 counter
->tstamp_stopped
= ctx
->time
;
500 * Cross CPU call to install and enable a performance counter
502 static void __perf_install_in_context(void *info
)
504 struct perf_cpu_context
*cpuctx
= &__get_cpu_var(perf_cpu_context
);
505 struct perf_counter
*counter
= info
;
506 struct perf_counter_context
*ctx
= counter
->ctx
;
507 struct perf_counter
*leader
= counter
->group_leader
;
508 int cpu
= smp_processor_id();
514 * If this is a task context, we need to check whether it is
515 * the current task context of this cpu. If not it has been
516 * scheduled out before the smp call arrived.
518 if (ctx
->task
&& cpuctx
->task_ctx
!= ctx
)
521 spin_lock_irqsave(&ctx
->lock
, flags
);
522 update_context_time(ctx
);
525 * Protect the list operation against NMI by disabling the
526 * counters on a global level. NOP for non NMI based counters.
528 perf_flags
= hw_perf_save_disable();
530 add_counter_to_ctx(counter
, ctx
);
533 * Don't put the counter on if it is disabled or if
534 * it is in a group and the group isn't on.
536 if (counter
->state
!= PERF_COUNTER_STATE_INACTIVE
||
537 (leader
!= counter
&& leader
->state
!= PERF_COUNTER_STATE_ACTIVE
))
541 * An exclusive counter can't go on if there are already active
542 * hardware counters, and no hardware counter can go on if there
543 * is already an exclusive counter on.
545 if (!group_can_go_on(counter
, cpuctx
, 1))
548 err
= counter_sched_in(counter
, cpuctx
, ctx
, cpu
);
552 * This counter couldn't go on. If it is in a group
553 * then we have to pull the whole group off.
554 * If the counter group is pinned then put it in error state.
556 if (leader
!= counter
)
557 group_sched_out(leader
, cpuctx
, ctx
);
558 if (leader
->hw_event
.pinned
) {
559 update_group_times(leader
);
560 leader
->state
= PERF_COUNTER_STATE_ERROR
;
564 if (!err
&& !ctx
->task
&& cpuctx
->max_pertask
)
565 cpuctx
->max_pertask
--;
568 hw_perf_restore(perf_flags
);
570 spin_unlock_irqrestore(&ctx
->lock
, flags
);
574 * Attach a performance counter to a context
576 * First we add the counter to the list with the hardware enable bit
577 * in counter->hw_config cleared.
579 * If the counter is attached to a task which is on a CPU we use a smp
580 * call to enable it in the task context. The task might have been
581 * scheduled away, but we check this in the smp call again.
583 * Must be called with ctx->mutex held.
586 perf_install_in_context(struct perf_counter_context
*ctx
,
587 struct perf_counter
*counter
,
590 struct task_struct
*task
= ctx
->task
;
594 * Per cpu counters are installed via an smp call and
595 * the install is always sucessful.
597 smp_call_function_single(cpu
, __perf_install_in_context
,
602 counter
->task
= task
;
604 task_oncpu_function_call(task
, __perf_install_in_context
,
607 spin_lock_irq(&ctx
->lock
);
609 * we need to retry the smp call.
611 if (ctx
->is_active
&& list_empty(&counter
->list_entry
)) {
612 spin_unlock_irq(&ctx
->lock
);
617 * The lock prevents that this context is scheduled in so we
618 * can add the counter safely, if it the call above did not
621 if (list_empty(&counter
->list_entry
))
622 add_counter_to_ctx(counter
, ctx
);
623 spin_unlock_irq(&ctx
->lock
);
627 * Cross CPU call to enable a performance counter
629 static void __perf_counter_enable(void *info
)
631 struct perf_counter
*counter
= info
;
632 struct perf_cpu_context
*cpuctx
= &__get_cpu_var(perf_cpu_context
);
633 struct perf_counter_context
*ctx
= counter
->ctx
;
634 struct perf_counter
*leader
= counter
->group_leader
;
639 * If this is a per-task counter, need to check whether this
640 * counter's task is the current task on this cpu.
642 if (ctx
->task
&& cpuctx
->task_ctx
!= ctx
)
645 spin_lock_irqsave(&ctx
->lock
, flags
);
646 update_context_time(ctx
);
648 counter
->prev_state
= counter
->state
;
649 if (counter
->state
>= PERF_COUNTER_STATE_INACTIVE
)
651 counter
->state
= PERF_COUNTER_STATE_INACTIVE
;
652 counter
->tstamp_enabled
= ctx
->time
- counter
->total_time_enabled
;
655 * If the counter is in a group and isn't the group leader,
656 * then don't put it on unless the group is on.
658 if (leader
!= counter
&& leader
->state
!= PERF_COUNTER_STATE_ACTIVE
)
661 if (!group_can_go_on(counter
, cpuctx
, 1))
664 err
= counter_sched_in(counter
, cpuctx
, ctx
,
669 * If this counter can't go on and it's part of a
670 * group, then the whole group has to come off.
672 if (leader
!= counter
)
673 group_sched_out(leader
, cpuctx
, ctx
);
674 if (leader
->hw_event
.pinned
) {
675 update_group_times(leader
);
676 leader
->state
= PERF_COUNTER_STATE_ERROR
;
681 spin_unlock_irqrestore(&ctx
->lock
, flags
);
687 static void perf_counter_enable(struct perf_counter
*counter
)
689 struct perf_counter_context
*ctx
= counter
->ctx
;
690 struct task_struct
*task
= ctx
->task
;
694 * Enable the counter on the cpu that it's on
696 smp_call_function_single(counter
->cpu
, __perf_counter_enable
,
701 spin_lock_irq(&ctx
->lock
);
702 if (counter
->state
>= PERF_COUNTER_STATE_INACTIVE
)
706 * If the counter is in error state, clear that first.
707 * That way, if we see the counter in error state below, we
708 * know that it has gone back into error state, as distinct
709 * from the task having been scheduled away before the
710 * cross-call arrived.
712 if (counter
->state
== PERF_COUNTER_STATE_ERROR
)
713 counter
->state
= PERF_COUNTER_STATE_OFF
;
716 spin_unlock_irq(&ctx
->lock
);
717 task_oncpu_function_call(task
, __perf_counter_enable
, counter
);
719 spin_lock_irq(&ctx
->lock
);
722 * If the context is active and the counter is still off,
723 * we need to retry the cross-call.
725 if (ctx
->is_active
&& counter
->state
== PERF_COUNTER_STATE_OFF
)
729 * Since we have the lock this context can't be scheduled
730 * in, so we can change the state safely.
732 if (counter
->state
== PERF_COUNTER_STATE_OFF
) {
733 counter
->state
= PERF_COUNTER_STATE_INACTIVE
;
734 counter
->tstamp_enabled
=
735 ctx
->time
- counter
->total_time_enabled
;
738 spin_unlock_irq(&ctx
->lock
);
741 static void perf_counter_refresh(struct perf_counter
*counter
, int refresh
)
743 atomic_add(refresh
, &counter
->event_limit
);
744 perf_counter_enable(counter
);
748 * Enable a counter and all its children.
750 static void perf_counter_enable_family(struct perf_counter
*counter
)
752 struct perf_counter
*child
;
754 perf_counter_enable(counter
);
757 * Lock the mutex to protect the list of children
759 mutex_lock(&counter
->mutex
);
760 list_for_each_entry(child
, &counter
->child_list
, child_list
)
761 perf_counter_enable(child
);
762 mutex_unlock(&counter
->mutex
);
765 void __perf_counter_sched_out(struct perf_counter_context
*ctx
,
766 struct perf_cpu_context
*cpuctx
)
768 struct perf_counter
*counter
;
771 spin_lock(&ctx
->lock
);
773 if (likely(!ctx
->nr_counters
))
775 update_context_time(ctx
);
777 flags
= hw_perf_save_disable();
778 if (ctx
->nr_active
) {
779 list_for_each_entry(counter
, &ctx
->counter_list
, list_entry
)
780 group_sched_out(counter
, cpuctx
, ctx
);
782 hw_perf_restore(flags
);
784 spin_unlock(&ctx
->lock
);
788 * Called from scheduler to remove the counters of the current task,
789 * with interrupts disabled.
791 * We stop each counter and update the counter value in counter->count.
793 * This does not protect us against NMI, but disable()
794 * sets the disabled bit in the control field of counter _before_
795 * accessing the counter control register. If a NMI hits, then it will
796 * not restart the counter.
798 void perf_counter_task_sched_out(struct task_struct
*task
, int cpu
)
800 struct perf_cpu_context
*cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
801 struct perf_counter_context
*ctx
= &task
->perf_counter_ctx
;
802 struct pt_regs
*regs
;
804 if (likely(!cpuctx
->task_ctx
))
807 update_context_time(ctx
);
809 regs
= task_pt_regs(task
);
810 perf_swcounter_event(PERF_COUNT_CONTEXT_SWITCHES
, 1, 1, regs
, 0);
811 __perf_counter_sched_out(ctx
, cpuctx
);
813 cpuctx
->task_ctx
= NULL
;
816 static void perf_counter_cpu_sched_out(struct perf_cpu_context
*cpuctx
)
818 __perf_counter_sched_out(&cpuctx
->ctx
, cpuctx
);
822 group_sched_in(struct perf_counter
*group_counter
,
823 struct perf_cpu_context
*cpuctx
,
824 struct perf_counter_context
*ctx
,
827 struct perf_counter
*counter
, *partial_group
;
830 if (group_counter
->state
== PERF_COUNTER_STATE_OFF
)
833 ret
= hw_perf_group_sched_in(group_counter
, cpuctx
, ctx
, cpu
);
835 return ret
< 0 ? ret
: 0;
837 group_counter
->prev_state
= group_counter
->state
;
838 if (counter_sched_in(group_counter
, cpuctx
, ctx
, cpu
))
842 * Schedule in siblings as one group (if any):
844 list_for_each_entry(counter
, &group_counter
->sibling_list
, list_entry
) {
845 counter
->prev_state
= counter
->state
;
846 if (counter_sched_in(counter
, cpuctx
, ctx
, cpu
)) {
847 partial_group
= counter
;
856 * Groups can be scheduled in as one unit only, so undo any
857 * partial group before returning:
859 list_for_each_entry(counter
, &group_counter
->sibling_list
, list_entry
) {
860 if (counter
== partial_group
)
862 counter_sched_out(counter
, cpuctx
, ctx
);
864 counter_sched_out(group_counter
, cpuctx
, ctx
);
870 __perf_counter_sched_in(struct perf_counter_context
*ctx
,
871 struct perf_cpu_context
*cpuctx
, int cpu
)
873 struct perf_counter
*counter
;
877 spin_lock(&ctx
->lock
);
879 if (likely(!ctx
->nr_counters
))
882 ctx
->timestamp
= perf_clock();
884 flags
= hw_perf_save_disable();
887 * First go through the list and put on any pinned groups
888 * in order to give them the best chance of going on.
890 list_for_each_entry(counter
, &ctx
->counter_list
, list_entry
) {
891 if (counter
->state
<= PERF_COUNTER_STATE_OFF
||
892 !counter
->hw_event
.pinned
)
894 if (counter
->cpu
!= -1 && counter
->cpu
!= cpu
)
897 if (group_can_go_on(counter
, cpuctx
, 1))
898 group_sched_in(counter
, cpuctx
, ctx
, cpu
);
901 * If this pinned group hasn't been scheduled,
902 * put it in error state.
904 if (counter
->state
== PERF_COUNTER_STATE_INACTIVE
) {
905 update_group_times(counter
);
906 counter
->state
= PERF_COUNTER_STATE_ERROR
;
910 list_for_each_entry(counter
, &ctx
->counter_list
, list_entry
) {
912 * Ignore counters in OFF or ERROR state, and
913 * ignore pinned counters since we did them already.
915 if (counter
->state
<= PERF_COUNTER_STATE_OFF
||
916 counter
->hw_event
.pinned
)
920 * Listen to the 'cpu' scheduling filter constraint
923 if (counter
->cpu
!= -1 && counter
->cpu
!= cpu
)
926 if (group_can_go_on(counter
, cpuctx
, can_add_hw
)) {
927 if (group_sched_in(counter
, cpuctx
, ctx
, cpu
))
931 hw_perf_restore(flags
);
933 spin_unlock(&ctx
->lock
);
937 * Called from scheduler to add the counters of the current task
938 * with interrupts disabled.
940 * We restore the counter value and then enable it.
942 * This does not protect us against NMI, but enable()
943 * sets the enabled bit in the control field of counter _before_
944 * accessing the counter control register. If a NMI hits, then it will
945 * keep the counter running.
947 void perf_counter_task_sched_in(struct task_struct
*task
, int cpu
)
949 struct perf_cpu_context
*cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
950 struct perf_counter_context
*ctx
= &task
->perf_counter_ctx
;
952 __perf_counter_sched_in(ctx
, cpuctx
, cpu
);
953 cpuctx
->task_ctx
= ctx
;
956 static void perf_counter_cpu_sched_in(struct perf_cpu_context
*cpuctx
, int cpu
)
958 struct perf_counter_context
*ctx
= &cpuctx
->ctx
;
960 __perf_counter_sched_in(ctx
, cpuctx
, cpu
);
963 int perf_counter_task_disable(void)
965 struct task_struct
*curr
= current
;
966 struct perf_counter_context
*ctx
= &curr
->perf_counter_ctx
;
967 struct perf_counter
*counter
;
972 if (likely(!ctx
->nr_counters
))
975 local_irq_save(flags
);
976 cpu
= smp_processor_id();
978 perf_counter_task_sched_out(curr
, cpu
);
980 spin_lock(&ctx
->lock
);
983 * Disable all the counters:
985 perf_flags
= hw_perf_save_disable();
987 list_for_each_entry(counter
, &ctx
->counter_list
, list_entry
) {
988 if (counter
->state
!= PERF_COUNTER_STATE_ERROR
) {
989 update_group_times(counter
);
990 counter
->state
= PERF_COUNTER_STATE_OFF
;
994 hw_perf_restore(perf_flags
);
996 spin_unlock_irqrestore(&ctx
->lock
, flags
);
1001 int perf_counter_task_enable(void)
1003 struct task_struct
*curr
= current
;
1004 struct perf_counter_context
*ctx
= &curr
->perf_counter_ctx
;
1005 struct perf_counter
*counter
;
1006 unsigned long flags
;
1010 if (likely(!ctx
->nr_counters
))
1013 local_irq_save(flags
);
1014 cpu
= smp_processor_id();
1016 perf_counter_task_sched_out(curr
, cpu
);
1018 spin_lock(&ctx
->lock
);
1021 * Disable all the counters:
1023 perf_flags
= hw_perf_save_disable();
1025 list_for_each_entry(counter
, &ctx
->counter_list
, list_entry
) {
1026 if (counter
->state
> PERF_COUNTER_STATE_OFF
)
1028 counter
->state
= PERF_COUNTER_STATE_INACTIVE
;
1029 counter
->tstamp_enabled
=
1030 ctx
->time
- counter
->total_time_enabled
;
1031 counter
->hw_event
.disabled
= 0;
1033 hw_perf_restore(perf_flags
);
1035 spin_unlock(&ctx
->lock
);
1037 perf_counter_task_sched_in(curr
, cpu
);
1039 local_irq_restore(flags
);
1045 * Round-robin a context's counters:
1047 static void rotate_ctx(struct perf_counter_context
*ctx
)
1049 struct perf_counter
*counter
;
1052 if (!ctx
->nr_counters
)
1055 spin_lock(&ctx
->lock
);
1057 * Rotate the first entry last (works just fine for group counters too):
1059 perf_flags
= hw_perf_save_disable();
1060 list_for_each_entry(counter
, &ctx
->counter_list
, list_entry
) {
1061 list_move_tail(&counter
->list_entry
, &ctx
->counter_list
);
1064 hw_perf_restore(perf_flags
);
1066 spin_unlock(&ctx
->lock
);
1069 void perf_counter_task_tick(struct task_struct
*curr
, int cpu
)
1071 struct perf_cpu_context
*cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
1072 struct perf_counter_context
*ctx
= &curr
->perf_counter_ctx
;
1074 perf_counter_cpu_sched_out(cpuctx
);
1075 perf_counter_task_sched_out(curr
, cpu
);
1077 rotate_ctx(&cpuctx
->ctx
);
1080 perf_counter_cpu_sched_in(cpuctx
, cpu
);
1081 perf_counter_task_sched_in(curr
, cpu
);
1085 * Cross CPU call to read the hardware counter
1087 static void __read(void *info
)
1089 struct perf_counter
*counter
= info
;
1090 struct perf_counter_context
*ctx
= counter
->ctx
;
1091 unsigned long flags
;
1093 local_irq_save(flags
);
1095 update_context_time(ctx
);
1096 counter
->pmu
->read(counter
);
1097 update_counter_times(counter
);
1098 local_irq_restore(flags
);
1101 static u64
perf_counter_read(struct perf_counter
*counter
)
1104 * If counter is enabled and currently active on a CPU, update the
1105 * value in the counter structure:
1107 if (counter
->state
== PERF_COUNTER_STATE_ACTIVE
) {
1108 smp_call_function_single(counter
->oncpu
,
1109 __read
, counter
, 1);
1110 } else if (counter
->state
== PERF_COUNTER_STATE_INACTIVE
) {
1111 update_counter_times(counter
);
1114 return atomic64_read(&counter
->count
);
1117 static void put_context(struct perf_counter_context
*ctx
)
1120 put_task_struct(ctx
->task
);
1123 static struct perf_counter_context
*find_get_context(pid_t pid
, int cpu
)
1125 struct perf_cpu_context
*cpuctx
;
1126 struct perf_counter_context
*ctx
;
1127 struct task_struct
*task
;
1130 * If cpu is not a wildcard then this is a percpu counter:
1133 /* Must be root to operate on a CPU counter: */
1134 if (sysctl_perf_counter_priv
&& !capable(CAP_SYS_ADMIN
))
1135 return ERR_PTR(-EACCES
);
1137 if (cpu
< 0 || cpu
> num_possible_cpus())
1138 return ERR_PTR(-EINVAL
);
1141 * We could be clever and allow to attach a counter to an
1142 * offline CPU and activate it when the CPU comes up, but
1145 if (!cpu_isset(cpu
, cpu_online_map
))
1146 return ERR_PTR(-ENODEV
);
1148 cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
1158 task
= find_task_by_vpid(pid
);
1160 get_task_struct(task
);
1164 return ERR_PTR(-ESRCH
);
1166 ctx
= &task
->perf_counter_ctx
;
1169 /* Reuse ptrace permission checks for now. */
1170 if (!ptrace_may_access(task
, PTRACE_MODE_READ
)) {
1172 return ERR_PTR(-EACCES
);
1178 static void free_counter_rcu(struct rcu_head
*head
)
1180 struct perf_counter
*counter
;
1182 counter
= container_of(head
, struct perf_counter
, rcu_head
);
1186 static void perf_pending_sync(struct perf_counter
*counter
);
1188 static void free_counter(struct perf_counter
*counter
)
1190 perf_pending_sync(counter
);
1192 if (counter
->hw_event
.mmap
)
1193 atomic_dec(&nr_mmap_tracking
);
1194 if (counter
->hw_event
.munmap
)
1195 atomic_dec(&nr_munmap_tracking
);
1196 if (counter
->hw_event
.comm
)
1197 atomic_dec(&nr_comm_tracking
);
1199 if (counter
->destroy
)
1200 counter
->destroy(counter
);
1202 call_rcu(&counter
->rcu_head
, free_counter_rcu
);
1206 * Called when the last reference to the file is gone.
1208 static int perf_release(struct inode
*inode
, struct file
*file
)
1210 struct perf_counter
*counter
= file
->private_data
;
1211 struct perf_counter_context
*ctx
= counter
->ctx
;
1213 file
->private_data
= NULL
;
1215 mutex_lock(&ctx
->mutex
);
1216 mutex_lock(&counter
->mutex
);
1218 perf_counter_remove_from_context(counter
);
1220 mutex_unlock(&counter
->mutex
);
1221 mutex_unlock(&ctx
->mutex
);
1223 free_counter(counter
);
1230 * Read the performance counter - simple non blocking version for now
1233 perf_read_hw(struct perf_counter
*counter
, char __user
*buf
, size_t count
)
1239 * Return end-of-file for a read on a counter that is in
1240 * error state (i.e. because it was pinned but it couldn't be
1241 * scheduled on to the CPU at some point).
1243 if (counter
->state
== PERF_COUNTER_STATE_ERROR
)
1246 mutex_lock(&counter
->mutex
);
1247 values
[0] = perf_counter_read(counter
);
1249 if (counter
->hw_event
.read_format
& PERF_FORMAT_TOTAL_TIME_ENABLED
)
1250 values
[n
++] = counter
->total_time_enabled
+
1251 atomic64_read(&counter
->child_total_time_enabled
);
1252 if (counter
->hw_event
.read_format
& PERF_FORMAT_TOTAL_TIME_RUNNING
)
1253 values
[n
++] = counter
->total_time_running
+
1254 atomic64_read(&counter
->child_total_time_running
);
1255 mutex_unlock(&counter
->mutex
);
1257 if (count
< n
* sizeof(u64
))
1259 count
= n
* sizeof(u64
);
1261 if (copy_to_user(buf
, values
, count
))
1268 perf_read(struct file
*file
, char __user
*buf
, size_t count
, loff_t
*ppos
)
1270 struct perf_counter
*counter
= file
->private_data
;
1272 return perf_read_hw(counter
, buf
, count
);
1275 static unsigned int perf_poll(struct file
*file
, poll_table
*wait
)
1277 struct perf_counter
*counter
= file
->private_data
;
1278 struct perf_mmap_data
*data
;
1279 unsigned int events
= POLL_HUP
;
1282 data
= rcu_dereference(counter
->data
);
1284 events
= atomic_xchg(&data
->poll
, 0);
1287 poll_wait(file
, &counter
->waitq
, wait
);
1292 static void perf_counter_reset(struct perf_counter
*counter
)
1294 atomic_set(&counter
->count
, 0);
1297 static long perf_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
1299 struct perf_counter
*counter
= file
->private_data
;
1303 case PERF_COUNTER_IOC_ENABLE
:
1304 perf_counter_enable_family(counter
);
1306 case PERF_COUNTER_IOC_DISABLE
:
1307 perf_counter_disable_family(counter
);
1309 case PERF_COUNTER_IOC_REFRESH
:
1310 perf_counter_refresh(counter
, arg
);
1312 case PERF_COUNTER_IOC_RESET
:
1313 perf_counter_reset(counter
);
1322 * Callers need to ensure there can be no nesting of this function, otherwise
1323 * the seqlock logic goes bad. We can not serialize this because the arch
1324 * code calls this from NMI context.
1326 void perf_counter_update_userpage(struct perf_counter
*counter
)
1328 struct perf_mmap_data
*data
;
1329 struct perf_counter_mmap_page
*userpg
;
1332 data
= rcu_dereference(counter
->data
);
1336 userpg
= data
->user_page
;
1339 * Disable preemption so as to not let the corresponding user-space
1340 * spin too long if we get preempted.
1345 userpg
->index
= counter
->hw
.idx
;
1346 userpg
->offset
= atomic64_read(&counter
->count
);
1347 if (counter
->state
== PERF_COUNTER_STATE_ACTIVE
)
1348 userpg
->offset
-= atomic64_read(&counter
->hw
.prev_count
);
1357 static int perf_mmap_fault(struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
1359 struct perf_counter
*counter
= vma
->vm_file
->private_data
;
1360 struct perf_mmap_data
*data
;
1361 int ret
= VM_FAULT_SIGBUS
;
1364 data
= rcu_dereference(counter
->data
);
1368 if (vmf
->pgoff
== 0) {
1369 vmf
->page
= virt_to_page(data
->user_page
);
1371 int nr
= vmf
->pgoff
- 1;
1373 if ((unsigned)nr
> data
->nr_pages
)
1376 vmf
->page
= virt_to_page(data
->data_pages
[nr
]);
1378 get_page(vmf
->page
);
1386 static int perf_mmap_data_alloc(struct perf_counter
*counter
, int nr_pages
)
1388 struct perf_mmap_data
*data
;
1392 WARN_ON(atomic_read(&counter
->mmap_count
));
1394 size
= sizeof(struct perf_mmap_data
);
1395 size
+= nr_pages
* sizeof(void *);
1397 data
= kzalloc(size
, GFP_KERNEL
);
1401 data
->user_page
= (void *)get_zeroed_page(GFP_KERNEL
);
1402 if (!data
->user_page
)
1403 goto fail_user_page
;
1405 for (i
= 0; i
< nr_pages
; i
++) {
1406 data
->data_pages
[i
] = (void *)get_zeroed_page(GFP_KERNEL
);
1407 if (!data
->data_pages
[i
])
1408 goto fail_data_pages
;
1411 data
->nr_pages
= nr_pages
;
1413 rcu_assign_pointer(counter
->data
, data
);
1418 for (i
--; i
>= 0; i
--)
1419 free_page((unsigned long)data
->data_pages
[i
]);
1421 free_page((unsigned long)data
->user_page
);
1430 static void __perf_mmap_data_free(struct rcu_head
*rcu_head
)
1432 struct perf_mmap_data
*data
= container_of(rcu_head
,
1433 struct perf_mmap_data
, rcu_head
);
1436 free_page((unsigned long)data
->user_page
);
1437 for (i
= 0; i
< data
->nr_pages
; i
++)
1438 free_page((unsigned long)data
->data_pages
[i
]);
1442 static void perf_mmap_data_free(struct perf_counter
*counter
)
1444 struct perf_mmap_data
*data
= counter
->data
;
1446 WARN_ON(atomic_read(&counter
->mmap_count
));
1448 rcu_assign_pointer(counter
->data
, NULL
);
1449 call_rcu(&data
->rcu_head
, __perf_mmap_data_free
);
1452 static void perf_mmap_open(struct vm_area_struct
*vma
)
1454 struct perf_counter
*counter
= vma
->vm_file
->private_data
;
1456 atomic_inc(&counter
->mmap_count
);
1459 static void perf_mmap_close(struct vm_area_struct
*vma
)
1461 struct perf_counter
*counter
= vma
->vm_file
->private_data
;
1463 if (atomic_dec_and_mutex_lock(&counter
->mmap_count
,
1464 &counter
->mmap_mutex
)) {
1465 vma
->vm_mm
->locked_vm
-= counter
->data
->nr_locked
;
1466 perf_mmap_data_free(counter
);
1467 mutex_unlock(&counter
->mmap_mutex
);
1471 static struct vm_operations_struct perf_mmap_vmops
= {
1472 .open
= perf_mmap_open
,
1473 .close
= perf_mmap_close
,
1474 .fault
= perf_mmap_fault
,
1477 static int perf_mmap(struct file
*file
, struct vm_area_struct
*vma
)
1479 struct perf_counter
*counter
= file
->private_data
;
1480 unsigned long vma_size
;
1481 unsigned long nr_pages
;
1482 unsigned long locked
, lock_limit
;
1486 if (!(vma
->vm_flags
& VM_SHARED
) || (vma
->vm_flags
& VM_WRITE
))
1489 vma_size
= vma
->vm_end
- vma
->vm_start
;
1490 nr_pages
= (vma_size
/ PAGE_SIZE
) - 1;
1493 * If we have data pages ensure they're a power-of-two number, so we
1494 * can do bitmasks instead of modulo.
1496 if (nr_pages
!= 0 && !is_power_of_2(nr_pages
))
1499 if (vma_size
!= PAGE_SIZE
* (1 + nr_pages
))
1502 if (vma
->vm_pgoff
!= 0)
1505 mutex_lock(&counter
->mmap_mutex
);
1506 if (atomic_inc_not_zero(&counter
->mmap_count
)) {
1507 if (nr_pages
!= counter
->data
->nr_pages
)
1512 extra
= nr_pages
/* + 1 only account the data pages */;
1513 extra
-= sysctl_perf_counter_mlock
>> (PAGE_SHIFT
- 10);
1517 locked
= vma
->vm_mm
->locked_vm
+ extra
;
1519 lock_limit
= current
->signal
->rlim
[RLIMIT_MEMLOCK
].rlim_cur
;
1520 lock_limit
>>= PAGE_SHIFT
;
1522 if ((locked
> lock_limit
) && !capable(CAP_IPC_LOCK
)) {
1527 WARN_ON(counter
->data
);
1528 ret
= perf_mmap_data_alloc(counter
, nr_pages
);
1532 atomic_set(&counter
->mmap_count
, 1);
1533 vma
->vm_mm
->locked_vm
+= extra
;
1534 counter
->data
->nr_locked
= extra
;
1536 mutex_unlock(&counter
->mmap_mutex
);
1538 vma
->vm_flags
&= ~VM_MAYWRITE
;
1539 vma
->vm_flags
|= VM_RESERVED
;
1540 vma
->vm_ops
= &perf_mmap_vmops
;
1545 static int perf_fasync(int fd
, struct file
*filp
, int on
)
1547 struct perf_counter
*counter
= filp
->private_data
;
1548 struct inode
*inode
= filp
->f_path
.dentry
->d_inode
;
1551 mutex_lock(&inode
->i_mutex
);
1552 retval
= fasync_helper(fd
, filp
, on
, &counter
->fasync
);
1553 mutex_unlock(&inode
->i_mutex
);
1561 static const struct file_operations perf_fops
= {
1562 .release
= perf_release
,
1565 .unlocked_ioctl
= perf_ioctl
,
1566 .compat_ioctl
= perf_ioctl
,
1568 .fasync
= perf_fasync
,
1572 * Perf counter wakeup
1574 * If there's data, ensure we set the poll() state and publish everything
1575 * to user-space before waking everybody up.
1578 void perf_counter_wakeup(struct perf_counter
*counter
)
1580 wake_up_all(&counter
->waitq
);
1582 if (counter
->pending_kill
) {
1583 kill_fasync(&counter
->fasync
, SIGIO
, counter
->pending_kill
);
1584 counter
->pending_kill
= 0;
1591 * Handle the case where we need to wakeup up from NMI (or rq->lock) context.
1593 * The NMI bit means we cannot possibly take locks. Therefore, maintain a
1594 * single linked list and use cmpxchg() to add entries lockless.
1597 static void perf_pending_counter(struct perf_pending_entry
*entry
)
1599 struct perf_counter
*counter
= container_of(entry
,
1600 struct perf_counter
, pending
);
1602 if (counter
->pending_disable
) {
1603 counter
->pending_disable
= 0;
1604 perf_counter_disable(counter
);
1607 if (counter
->pending_wakeup
) {
1608 counter
->pending_wakeup
= 0;
1609 perf_counter_wakeup(counter
);
1613 #define PENDING_TAIL ((struct perf_pending_entry *)-1UL)
1615 static DEFINE_PER_CPU(struct perf_pending_entry
*, perf_pending_head
) = {
1619 static void perf_pending_queue(struct perf_pending_entry
*entry
,
1620 void (*func
)(struct perf_pending_entry
*))
1622 struct perf_pending_entry
**head
;
1624 if (cmpxchg(&entry
->next
, NULL
, PENDING_TAIL
) != NULL
)
1629 head
= &get_cpu_var(perf_pending_head
);
1632 entry
->next
= *head
;
1633 } while (cmpxchg(head
, entry
->next
, entry
) != entry
->next
);
1635 set_perf_counter_pending();
1637 put_cpu_var(perf_pending_head
);
1640 static int __perf_pending_run(void)
1642 struct perf_pending_entry
*list
;
1645 list
= xchg(&__get_cpu_var(perf_pending_head
), PENDING_TAIL
);
1646 while (list
!= PENDING_TAIL
) {
1647 void (*func
)(struct perf_pending_entry
*);
1648 struct perf_pending_entry
*entry
= list
;
1655 * Ensure we observe the unqueue before we issue the wakeup,
1656 * so that we won't be waiting forever.
1657 * -- see perf_not_pending().
1668 static inline int perf_not_pending(struct perf_counter
*counter
)
1671 * If we flush on whatever cpu we run, there is a chance we don't
1675 __perf_pending_run();
1679 * Ensure we see the proper queue state before going to sleep
1680 * so that we do not miss the wakeup. -- see perf_pending_handle()
1683 return counter
->pending
.next
== NULL
;
1686 static void perf_pending_sync(struct perf_counter
*counter
)
1688 wait_event(counter
->waitq
, perf_not_pending(counter
));
1691 void perf_counter_do_pending(void)
1693 __perf_pending_run();
1697 * Callchain support -- arch specific
1700 __weak
struct perf_callchain_entry
*perf_callchain(struct pt_regs
*regs
)
1709 struct perf_output_handle
{
1710 struct perf_counter
*counter
;
1711 struct perf_mmap_data
*data
;
1712 unsigned int offset
;
1717 unsigned long flags
;
1720 static void perf_output_wakeup(struct perf_output_handle
*handle
)
1722 atomic_set(&handle
->data
->poll
, POLL_IN
);
1725 handle
->counter
->pending_wakeup
= 1;
1726 perf_pending_queue(&handle
->counter
->pending
,
1727 perf_pending_counter
);
1729 perf_counter_wakeup(handle
->counter
);
1733 * Curious locking construct.
1735 * We need to ensure a later event doesn't publish a head when a former
1736 * event isn't done writing. However since we need to deal with NMIs we
1737 * cannot fully serialize things.
1739 * What we do is serialize between CPUs so we only have to deal with NMI
1740 * nesting on a single CPU.
1742 * We only publish the head (and generate a wakeup) when the outer-most
1745 static void perf_output_lock(struct perf_output_handle
*handle
)
1747 struct perf_mmap_data
*data
= handle
->data
;
1752 local_irq_save(handle
->flags
);
1753 cpu
= smp_processor_id();
1755 if (in_nmi() && atomic_read(&data
->lock
) == cpu
)
1758 while (atomic_cmpxchg(&data
->lock
, 0, cpu
) != 0)
1764 static void perf_output_unlock(struct perf_output_handle
*handle
)
1766 struct perf_mmap_data
*data
= handle
->data
;
1769 data
->done_head
= data
->head
;
1771 if (!handle
->locked
)
1776 * The xchg implies a full barrier that ensures all writes are done
1777 * before we publish the new head, matched by a rmb() in userspace when
1778 * reading this position.
1780 while ((head
= atomic_xchg(&data
->done_head
, 0)))
1781 data
->user_page
->data_head
= head
;
1784 * NMI can happen here, which means we can miss a done_head update.
1787 cpu
= atomic_xchg(&data
->lock
, 0);
1788 WARN_ON_ONCE(cpu
!= smp_processor_id());
1791 * Therefore we have to validate we did not indeed do so.
1793 if (unlikely(atomic_read(&data
->done_head
))) {
1795 * Since we had it locked, we can lock it again.
1797 while (atomic_cmpxchg(&data
->lock
, 0, cpu
) != 0)
1803 if (atomic_xchg(&data
->wakeup
, 0))
1804 perf_output_wakeup(handle
);
1806 local_irq_restore(handle
->flags
);
1809 static int perf_output_begin(struct perf_output_handle
*handle
,
1810 struct perf_counter
*counter
, unsigned int size
,
1811 int nmi
, int overflow
)
1813 struct perf_mmap_data
*data
;
1814 unsigned int offset
, head
;
1817 data
= rcu_dereference(counter
->data
);
1821 handle
->data
= data
;
1822 handle
->counter
= counter
;
1824 handle
->overflow
= overflow
;
1826 if (!data
->nr_pages
)
1829 perf_output_lock(handle
);
1832 offset
= head
= atomic_read(&data
->head
);
1834 } while (atomic_cmpxchg(&data
->head
, offset
, head
) != offset
);
1836 handle
->offset
= offset
;
1837 handle
->head
= head
;
1839 if ((offset
>> PAGE_SHIFT
) != (head
>> PAGE_SHIFT
))
1840 atomic_set(&data
->wakeup
, 1);
1845 perf_output_wakeup(handle
);
1852 static void perf_output_copy(struct perf_output_handle
*handle
,
1853 void *buf
, unsigned int len
)
1855 unsigned int pages_mask
;
1856 unsigned int offset
;
1860 offset
= handle
->offset
;
1861 pages_mask
= handle
->data
->nr_pages
- 1;
1862 pages
= handle
->data
->data_pages
;
1865 unsigned int page_offset
;
1868 nr
= (offset
>> PAGE_SHIFT
) & pages_mask
;
1869 page_offset
= offset
& (PAGE_SIZE
- 1);
1870 size
= min_t(unsigned int, PAGE_SIZE
- page_offset
, len
);
1872 memcpy(pages
[nr
] + page_offset
, buf
, size
);
1879 handle
->offset
= offset
;
1881 WARN_ON_ONCE(handle
->offset
> handle
->head
);
1884 #define perf_output_put(handle, x) \
1885 perf_output_copy((handle), &(x), sizeof(x))
1887 static void perf_output_end(struct perf_output_handle
*handle
)
1889 struct perf_counter
*counter
= handle
->counter
;
1890 struct perf_mmap_data
*data
= handle
->data
;
1892 int wakeup_events
= counter
->hw_event
.wakeup_events
;
1894 if (handle
->overflow
&& wakeup_events
) {
1895 int events
= atomic_inc_return(&data
->events
);
1896 if (events
>= wakeup_events
) {
1897 atomic_sub(wakeup_events
, &data
->events
);
1898 atomic_set(&data
->wakeup
, 1);
1902 perf_output_unlock(handle
);
1906 static void perf_counter_output(struct perf_counter
*counter
,
1907 int nmi
, struct pt_regs
*regs
, u64 addr
)
1910 u64 record_type
= counter
->hw_event
.record_type
;
1911 struct perf_output_handle handle
;
1912 struct perf_event_header header
;
1921 struct perf_callchain_entry
*callchain
= NULL
;
1922 int callchain_size
= 0;
1926 header
.size
= sizeof(header
);
1928 header
.misc
= PERF_EVENT_MISC_OVERFLOW
;
1929 header
.misc
|= user_mode(regs
) ?
1930 PERF_EVENT_MISC_USER
: PERF_EVENT_MISC_KERNEL
;
1932 if (record_type
& PERF_RECORD_IP
) {
1933 ip
= instruction_pointer(regs
);
1934 header
.type
|= PERF_RECORD_IP
;
1935 header
.size
+= sizeof(ip
);
1938 if (record_type
& PERF_RECORD_TID
) {
1939 /* namespace issues */
1940 tid_entry
.pid
= current
->group_leader
->pid
;
1941 tid_entry
.tid
= current
->pid
;
1943 header
.type
|= PERF_RECORD_TID
;
1944 header
.size
+= sizeof(tid_entry
);
1947 if (record_type
& PERF_RECORD_TIME
) {
1949 * Maybe do better on x86 and provide cpu_clock_nmi()
1951 time
= sched_clock();
1953 header
.type
|= PERF_RECORD_TIME
;
1954 header
.size
+= sizeof(u64
);
1957 if (record_type
& PERF_RECORD_ADDR
) {
1958 header
.type
|= PERF_RECORD_ADDR
;
1959 header
.size
+= sizeof(u64
);
1962 if (record_type
& PERF_RECORD_GROUP
) {
1963 header
.type
|= PERF_RECORD_GROUP
;
1964 header
.size
+= sizeof(u64
) +
1965 counter
->nr_siblings
* sizeof(group_entry
);
1968 if (record_type
& PERF_RECORD_CALLCHAIN
) {
1969 callchain
= perf_callchain(regs
);
1972 callchain_size
= (1 + callchain
->nr
) * sizeof(u64
);
1974 header
.type
|= PERF_RECORD_CALLCHAIN
;
1975 header
.size
+= callchain_size
;
1979 ret
= perf_output_begin(&handle
, counter
, header
.size
, nmi
, 1);
1983 perf_output_put(&handle
, header
);
1985 if (record_type
& PERF_RECORD_IP
)
1986 perf_output_put(&handle
, ip
);
1988 if (record_type
& PERF_RECORD_TID
)
1989 perf_output_put(&handle
, tid_entry
);
1991 if (record_type
& PERF_RECORD_TIME
)
1992 perf_output_put(&handle
, time
);
1994 if (record_type
& PERF_RECORD_ADDR
)
1995 perf_output_put(&handle
, addr
);
1997 if (record_type
& PERF_RECORD_GROUP
) {
1998 struct perf_counter
*leader
, *sub
;
1999 u64 nr
= counter
->nr_siblings
;
2001 perf_output_put(&handle
, nr
);
2003 leader
= counter
->group_leader
;
2004 list_for_each_entry(sub
, &leader
->sibling_list
, list_entry
) {
2006 sub
->pmu
->read(sub
);
2008 group_entry
.event
= sub
->hw_event
.config
;
2009 group_entry
.counter
= atomic64_read(&sub
->count
);
2011 perf_output_put(&handle
, group_entry
);
2016 perf_output_copy(&handle
, callchain
, callchain_size
);
2018 perf_output_end(&handle
);
2025 struct perf_comm_event
{
2026 struct task_struct
*task
;
2031 struct perf_event_header header
;
2038 static void perf_counter_comm_output(struct perf_counter
*counter
,
2039 struct perf_comm_event
*comm_event
)
2041 struct perf_output_handle handle
;
2042 int size
= comm_event
->event
.header
.size
;
2043 int ret
= perf_output_begin(&handle
, counter
, size
, 0, 0);
2048 perf_output_put(&handle
, comm_event
->event
);
2049 perf_output_copy(&handle
, comm_event
->comm
,
2050 comm_event
->comm_size
);
2051 perf_output_end(&handle
);
2054 static int perf_counter_comm_match(struct perf_counter
*counter
,
2055 struct perf_comm_event
*comm_event
)
2057 if (counter
->hw_event
.comm
&&
2058 comm_event
->event
.header
.type
== PERF_EVENT_COMM
)
2064 static void perf_counter_comm_ctx(struct perf_counter_context
*ctx
,
2065 struct perf_comm_event
*comm_event
)
2067 struct perf_counter
*counter
;
2069 if (system_state
!= SYSTEM_RUNNING
|| list_empty(&ctx
->event_list
))
2073 list_for_each_entry_rcu(counter
, &ctx
->event_list
, event_entry
) {
2074 if (perf_counter_comm_match(counter
, comm_event
))
2075 perf_counter_comm_output(counter
, comm_event
);
2080 static void perf_counter_comm_event(struct perf_comm_event
*comm_event
)
2082 struct perf_cpu_context
*cpuctx
;
2084 char *comm
= comm_event
->task
->comm
;
2086 size
= ALIGN(strlen(comm
)+1, sizeof(u64
));
2088 comm_event
->comm
= comm
;
2089 comm_event
->comm_size
= size
;
2091 comm_event
->event
.header
.size
= sizeof(comm_event
->event
) + size
;
2093 cpuctx
= &get_cpu_var(perf_cpu_context
);
2094 perf_counter_comm_ctx(&cpuctx
->ctx
, comm_event
);
2095 put_cpu_var(perf_cpu_context
);
2097 perf_counter_comm_ctx(¤t
->perf_counter_ctx
, comm_event
);
2100 void perf_counter_comm(struct task_struct
*task
)
2102 struct perf_comm_event comm_event
;
2104 if (!atomic_read(&nr_comm_tracking
))
2107 comm_event
= (struct perf_comm_event
){
2110 .header
= { .type
= PERF_EVENT_COMM
, },
2111 .pid
= task
->group_leader
->pid
,
2116 perf_counter_comm_event(&comm_event
);
2123 struct perf_mmap_event
{
2129 struct perf_event_header header
;
2139 static void perf_counter_mmap_output(struct perf_counter
*counter
,
2140 struct perf_mmap_event
*mmap_event
)
2142 struct perf_output_handle handle
;
2143 int size
= mmap_event
->event
.header
.size
;
2144 int ret
= perf_output_begin(&handle
, counter
, size
, 0, 0);
2149 perf_output_put(&handle
, mmap_event
->event
);
2150 perf_output_copy(&handle
, mmap_event
->file_name
,
2151 mmap_event
->file_size
);
2152 perf_output_end(&handle
);
2155 static int perf_counter_mmap_match(struct perf_counter
*counter
,
2156 struct perf_mmap_event
*mmap_event
)
2158 if (counter
->hw_event
.mmap
&&
2159 mmap_event
->event
.header
.type
== PERF_EVENT_MMAP
)
2162 if (counter
->hw_event
.munmap
&&
2163 mmap_event
->event
.header
.type
== PERF_EVENT_MUNMAP
)
2169 static void perf_counter_mmap_ctx(struct perf_counter_context
*ctx
,
2170 struct perf_mmap_event
*mmap_event
)
2172 struct perf_counter
*counter
;
2174 if (system_state
!= SYSTEM_RUNNING
|| list_empty(&ctx
->event_list
))
2178 list_for_each_entry_rcu(counter
, &ctx
->event_list
, event_entry
) {
2179 if (perf_counter_mmap_match(counter
, mmap_event
))
2180 perf_counter_mmap_output(counter
, mmap_event
);
2185 static void perf_counter_mmap_event(struct perf_mmap_event
*mmap_event
)
2187 struct perf_cpu_context
*cpuctx
;
2188 struct file
*file
= mmap_event
->file
;
2195 buf
= kzalloc(PATH_MAX
, GFP_KERNEL
);
2197 name
= strncpy(tmp
, "//enomem", sizeof(tmp
));
2200 name
= d_path(&file
->f_path
, buf
, PATH_MAX
);
2202 name
= strncpy(tmp
, "//toolong", sizeof(tmp
));
2206 name
= strncpy(tmp
, "//anon", sizeof(tmp
));
2211 size
= ALIGN(strlen(name
)+1, sizeof(u64
));
2213 mmap_event
->file_name
= name
;
2214 mmap_event
->file_size
= size
;
2216 mmap_event
->event
.header
.size
= sizeof(mmap_event
->event
) + size
;
2218 cpuctx
= &get_cpu_var(perf_cpu_context
);
2219 perf_counter_mmap_ctx(&cpuctx
->ctx
, mmap_event
);
2220 put_cpu_var(perf_cpu_context
);
2222 perf_counter_mmap_ctx(¤t
->perf_counter_ctx
, mmap_event
);
2227 void perf_counter_mmap(unsigned long addr
, unsigned long len
,
2228 unsigned long pgoff
, struct file
*file
)
2230 struct perf_mmap_event mmap_event
;
2232 if (!atomic_read(&nr_mmap_tracking
))
2235 mmap_event
= (struct perf_mmap_event
){
2238 .header
= { .type
= PERF_EVENT_MMAP
, },
2239 .pid
= current
->group_leader
->pid
,
2240 .tid
= current
->pid
,
2247 perf_counter_mmap_event(&mmap_event
);
2250 void perf_counter_munmap(unsigned long addr
, unsigned long len
,
2251 unsigned long pgoff
, struct file
*file
)
2253 struct perf_mmap_event mmap_event
;
2255 if (!atomic_read(&nr_munmap_tracking
))
2258 mmap_event
= (struct perf_mmap_event
){
2261 .header
= { .type
= PERF_EVENT_MUNMAP
, },
2262 .pid
= current
->group_leader
->pid
,
2263 .tid
= current
->pid
,
2270 perf_counter_mmap_event(&mmap_event
);
2274 * Generic counter overflow handling.
2277 int perf_counter_overflow(struct perf_counter
*counter
,
2278 int nmi
, struct pt_regs
*regs
, u64 addr
)
2280 int events
= atomic_read(&counter
->event_limit
);
2283 counter
->pending_kill
= POLL_IN
;
2284 if (events
&& atomic_dec_and_test(&counter
->event_limit
)) {
2286 counter
->pending_kill
= POLL_HUP
;
2288 counter
->pending_disable
= 1;
2289 perf_pending_queue(&counter
->pending
,
2290 perf_pending_counter
);
2292 perf_counter_disable(counter
);
2295 perf_counter_output(counter
, nmi
, regs
, addr
);
2300 * Generic software counter infrastructure
2303 static void perf_swcounter_update(struct perf_counter
*counter
)
2305 struct hw_perf_counter
*hwc
= &counter
->hw
;
2310 prev
= atomic64_read(&hwc
->prev_count
);
2311 now
= atomic64_read(&hwc
->count
);
2312 if (atomic64_cmpxchg(&hwc
->prev_count
, prev
, now
) != prev
)
2317 atomic64_add(delta
, &counter
->count
);
2318 atomic64_sub(delta
, &hwc
->period_left
);
2321 static void perf_swcounter_set_period(struct perf_counter
*counter
)
2323 struct hw_perf_counter
*hwc
= &counter
->hw
;
2324 s64 left
= atomic64_read(&hwc
->period_left
);
2325 s64 period
= hwc
->irq_period
;
2327 if (unlikely(left
<= -period
)) {
2329 atomic64_set(&hwc
->period_left
, left
);
2332 if (unlikely(left
<= 0)) {
2334 atomic64_add(period
, &hwc
->period_left
);
2337 atomic64_set(&hwc
->prev_count
, -left
);
2338 atomic64_set(&hwc
->count
, -left
);
2341 static enum hrtimer_restart
perf_swcounter_hrtimer(struct hrtimer
*hrtimer
)
2343 enum hrtimer_restart ret
= HRTIMER_RESTART
;
2344 struct perf_counter
*counter
;
2345 struct pt_regs
*regs
;
2347 counter
= container_of(hrtimer
, struct perf_counter
, hw
.hrtimer
);
2348 counter
->pmu
->read(counter
);
2350 regs
= get_irq_regs();
2352 * In case we exclude kernel IPs or are somehow not in interrupt
2353 * context, provide the next best thing, the user IP.
2355 if ((counter
->hw_event
.exclude_kernel
|| !regs
) &&
2356 !counter
->hw_event
.exclude_user
)
2357 regs
= task_pt_regs(current
);
2360 if (perf_counter_overflow(counter
, 0, regs
, 0))
2361 ret
= HRTIMER_NORESTART
;
2364 hrtimer_forward_now(hrtimer
, ns_to_ktime(counter
->hw
.irq_period
));
2369 static void perf_swcounter_overflow(struct perf_counter
*counter
,
2370 int nmi
, struct pt_regs
*regs
, u64 addr
)
2372 perf_swcounter_update(counter
);
2373 perf_swcounter_set_period(counter
);
2374 if (perf_counter_overflow(counter
, nmi
, regs
, addr
))
2375 /* soft-disable the counter */
2380 static int perf_swcounter_match(struct perf_counter
*counter
,
2381 enum perf_event_types type
,
2382 u32 event
, struct pt_regs
*regs
)
2384 if (counter
->state
!= PERF_COUNTER_STATE_ACTIVE
)
2387 if (perf_event_raw(&counter
->hw_event
))
2390 if (perf_event_type(&counter
->hw_event
) != type
)
2393 if (perf_event_id(&counter
->hw_event
) != event
)
2396 if (counter
->hw_event
.exclude_user
&& user_mode(regs
))
2399 if (counter
->hw_event
.exclude_kernel
&& !user_mode(regs
))
2405 static void perf_swcounter_add(struct perf_counter
*counter
, u64 nr
,
2406 int nmi
, struct pt_regs
*regs
, u64 addr
)
2408 int neg
= atomic64_add_negative(nr
, &counter
->hw
.count
);
2409 if (counter
->hw
.irq_period
&& !neg
)
2410 perf_swcounter_overflow(counter
, nmi
, regs
, addr
);
2413 static void perf_swcounter_ctx_event(struct perf_counter_context
*ctx
,
2414 enum perf_event_types type
, u32 event
,
2415 u64 nr
, int nmi
, struct pt_regs
*regs
,
2418 struct perf_counter
*counter
;
2420 if (system_state
!= SYSTEM_RUNNING
|| list_empty(&ctx
->event_list
))
2424 list_for_each_entry_rcu(counter
, &ctx
->event_list
, event_entry
) {
2425 if (perf_swcounter_match(counter
, type
, event
, regs
))
2426 perf_swcounter_add(counter
, nr
, nmi
, regs
, addr
);
2431 static int *perf_swcounter_recursion_context(struct perf_cpu_context
*cpuctx
)
2434 return &cpuctx
->recursion
[3];
2437 return &cpuctx
->recursion
[2];
2440 return &cpuctx
->recursion
[1];
2442 return &cpuctx
->recursion
[0];
2445 static void __perf_swcounter_event(enum perf_event_types type
, u32 event
,
2446 u64 nr
, int nmi
, struct pt_regs
*regs
,
2449 struct perf_cpu_context
*cpuctx
= &get_cpu_var(perf_cpu_context
);
2450 int *recursion
= perf_swcounter_recursion_context(cpuctx
);
2458 perf_swcounter_ctx_event(&cpuctx
->ctx
, type
, event
,
2459 nr
, nmi
, regs
, addr
);
2460 if (cpuctx
->task_ctx
) {
2461 perf_swcounter_ctx_event(cpuctx
->task_ctx
, type
, event
,
2462 nr
, nmi
, regs
, addr
);
2469 put_cpu_var(perf_cpu_context
);
2473 perf_swcounter_event(u32 event
, u64 nr
, int nmi
, struct pt_regs
*regs
, u64 addr
)
2475 __perf_swcounter_event(PERF_TYPE_SOFTWARE
, event
, nr
, nmi
, regs
, addr
);
2478 static void perf_swcounter_read(struct perf_counter
*counter
)
2480 perf_swcounter_update(counter
);
2483 static int perf_swcounter_enable(struct perf_counter
*counter
)
2485 perf_swcounter_set_period(counter
);
2489 static void perf_swcounter_disable(struct perf_counter
*counter
)
2491 perf_swcounter_update(counter
);
2494 static const struct pmu perf_ops_generic
= {
2495 .enable
= perf_swcounter_enable
,
2496 .disable
= perf_swcounter_disable
,
2497 .read
= perf_swcounter_read
,
2501 * Software counter: cpu wall time clock
2504 static void cpu_clock_perf_counter_update(struct perf_counter
*counter
)
2506 int cpu
= raw_smp_processor_id();
2510 now
= cpu_clock(cpu
);
2511 prev
= atomic64_read(&counter
->hw
.prev_count
);
2512 atomic64_set(&counter
->hw
.prev_count
, now
);
2513 atomic64_add(now
- prev
, &counter
->count
);
2516 static int cpu_clock_perf_counter_enable(struct perf_counter
*counter
)
2518 struct hw_perf_counter
*hwc
= &counter
->hw
;
2519 int cpu
= raw_smp_processor_id();
2521 atomic64_set(&hwc
->prev_count
, cpu_clock(cpu
));
2522 hrtimer_init(&hwc
->hrtimer
, CLOCK_MONOTONIC
, HRTIMER_MODE_REL
);
2523 hwc
->hrtimer
.function
= perf_swcounter_hrtimer
;
2524 if (hwc
->irq_period
) {
2525 __hrtimer_start_range_ns(&hwc
->hrtimer
,
2526 ns_to_ktime(hwc
->irq_period
), 0,
2527 HRTIMER_MODE_REL
, 0);
2533 static void cpu_clock_perf_counter_disable(struct perf_counter
*counter
)
2535 hrtimer_cancel(&counter
->hw
.hrtimer
);
2536 cpu_clock_perf_counter_update(counter
);
2539 static void cpu_clock_perf_counter_read(struct perf_counter
*counter
)
2541 cpu_clock_perf_counter_update(counter
);
2544 static const struct pmu perf_ops_cpu_clock
= {
2545 .enable
= cpu_clock_perf_counter_enable
,
2546 .disable
= cpu_clock_perf_counter_disable
,
2547 .read
= cpu_clock_perf_counter_read
,
2551 * Software counter: task time clock
2554 static void task_clock_perf_counter_update(struct perf_counter
*counter
, u64 now
)
2559 prev
= atomic64_xchg(&counter
->hw
.prev_count
, now
);
2561 atomic64_add(delta
, &counter
->count
);
2564 static int task_clock_perf_counter_enable(struct perf_counter
*counter
)
2566 struct hw_perf_counter
*hwc
= &counter
->hw
;
2569 now
= counter
->ctx
->time
;
2571 atomic64_set(&hwc
->prev_count
, now
);
2572 hrtimer_init(&hwc
->hrtimer
, CLOCK_MONOTONIC
, HRTIMER_MODE_REL
);
2573 hwc
->hrtimer
.function
= perf_swcounter_hrtimer
;
2574 if (hwc
->irq_period
) {
2575 __hrtimer_start_range_ns(&hwc
->hrtimer
,
2576 ns_to_ktime(hwc
->irq_period
), 0,
2577 HRTIMER_MODE_REL
, 0);
2583 static void task_clock_perf_counter_disable(struct perf_counter
*counter
)
2585 hrtimer_cancel(&counter
->hw
.hrtimer
);
2586 task_clock_perf_counter_update(counter
, counter
->ctx
->time
);
2590 static void task_clock_perf_counter_read(struct perf_counter
*counter
)
2595 update_context_time(counter
->ctx
);
2596 time
= counter
->ctx
->time
;
2598 u64 now
= perf_clock();
2599 u64 delta
= now
- counter
->ctx
->timestamp
;
2600 time
= counter
->ctx
->time
+ delta
;
2603 task_clock_perf_counter_update(counter
, time
);
2606 static const struct pmu perf_ops_task_clock
= {
2607 .enable
= task_clock_perf_counter_enable
,
2608 .disable
= task_clock_perf_counter_disable
,
2609 .read
= task_clock_perf_counter_read
,
2613 * Software counter: cpu migrations
2616 static inline u64
get_cpu_migrations(struct perf_counter
*counter
)
2618 struct task_struct
*curr
= counter
->ctx
->task
;
2621 return curr
->se
.nr_migrations
;
2622 return cpu_nr_migrations(smp_processor_id());
2625 static void cpu_migrations_perf_counter_update(struct perf_counter
*counter
)
2630 prev
= atomic64_read(&counter
->hw
.prev_count
);
2631 now
= get_cpu_migrations(counter
);
2633 atomic64_set(&counter
->hw
.prev_count
, now
);
2637 atomic64_add(delta
, &counter
->count
);
2640 static void cpu_migrations_perf_counter_read(struct perf_counter
*counter
)
2642 cpu_migrations_perf_counter_update(counter
);
2645 static int cpu_migrations_perf_counter_enable(struct perf_counter
*counter
)
2647 if (counter
->prev_state
<= PERF_COUNTER_STATE_OFF
)
2648 atomic64_set(&counter
->hw
.prev_count
,
2649 get_cpu_migrations(counter
));
2653 static void cpu_migrations_perf_counter_disable(struct perf_counter
*counter
)
2655 cpu_migrations_perf_counter_update(counter
);
2658 static const struct pmu perf_ops_cpu_migrations
= {
2659 .enable
= cpu_migrations_perf_counter_enable
,
2660 .disable
= cpu_migrations_perf_counter_disable
,
2661 .read
= cpu_migrations_perf_counter_read
,
2664 #ifdef CONFIG_EVENT_PROFILE
2665 void perf_tpcounter_event(int event_id
)
2667 struct pt_regs
*regs
= get_irq_regs();
2670 regs
= task_pt_regs(current
);
2672 __perf_swcounter_event(PERF_TYPE_TRACEPOINT
, event_id
, 1, 1, regs
, 0);
2674 EXPORT_SYMBOL_GPL(perf_tpcounter_event
);
2676 extern int ftrace_profile_enable(int);
2677 extern void ftrace_profile_disable(int);
2679 static void tp_perf_counter_destroy(struct perf_counter
*counter
)
2681 ftrace_profile_disable(perf_event_id(&counter
->hw_event
));
2684 static const struct pmu
*tp_perf_counter_init(struct perf_counter
*counter
)
2686 int event_id
= perf_event_id(&counter
->hw_event
);
2689 ret
= ftrace_profile_enable(event_id
);
2693 counter
->destroy
= tp_perf_counter_destroy
;
2694 counter
->hw
.irq_period
= counter
->hw_event
.irq_period
;
2696 return &perf_ops_generic
;
2699 static const struct pmu
*tp_perf_counter_init(struct perf_counter
*counter
)
2705 static const struct pmu
*sw_perf_counter_init(struct perf_counter
*counter
)
2707 struct perf_counter_hw_event
*hw_event
= &counter
->hw_event
;
2708 const struct pmu
*pmu
= NULL
;
2709 struct hw_perf_counter
*hwc
= &counter
->hw
;
2712 * Software counters (currently) can't in general distinguish
2713 * between user, kernel and hypervisor events.
2714 * However, context switches and cpu migrations are considered
2715 * to be kernel events, and page faults are never hypervisor
2718 switch (perf_event_id(&counter
->hw_event
)) {
2719 case PERF_COUNT_CPU_CLOCK
:
2720 pmu
= &perf_ops_cpu_clock
;
2722 if (hw_event
->irq_period
&& hw_event
->irq_period
< 10000)
2723 hw_event
->irq_period
= 10000;
2725 case PERF_COUNT_TASK_CLOCK
:
2727 * If the user instantiates this as a per-cpu counter,
2728 * use the cpu_clock counter instead.
2730 if (counter
->ctx
->task
)
2731 pmu
= &perf_ops_task_clock
;
2733 pmu
= &perf_ops_cpu_clock
;
2735 if (hw_event
->irq_period
&& hw_event
->irq_period
< 10000)
2736 hw_event
->irq_period
= 10000;
2738 case PERF_COUNT_PAGE_FAULTS
:
2739 case PERF_COUNT_PAGE_FAULTS_MIN
:
2740 case PERF_COUNT_PAGE_FAULTS_MAJ
:
2741 case PERF_COUNT_CONTEXT_SWITCHES
:
2742 pmu
= &perf_ops_generic
;
2744 case PERF_COUNT_CPU_MIGRATIONS
:
2745 if (!counter
->hw_event
.exclude_kernel
)
2746 pmu
= &perf_ops_cpu_migrations
;
2751 hwc
->irq_period
= hw_event
->irq_period
;
2757 * Allocate and initialize a counter structure
2759 static struct perf_counter
*
2760 perf_counter_alloc(struct perf_counter_hw_event
*hw_event
,
2762 struct perf_counter_context
*ctx
,
2763 struct perf_counter
*group_leader
,
2766 const struct pmu
*pmu
;
2767 struct perf_counter
*counter
;
2770 counter
= kzalloc(sizeof(*counter
), gfpflags
);
2772 return ERR_PTR(-ENOMEM
);
2775 * Single counters are their own group leaders, with an
2776 * empty sibling list:
2779 group_leader
= counter
;
2781 mutex_init(&counter
->mutex
);
2782 INIT_LIST_HEAD(&counter
->list_entry
);
2783 INIT_LIST_HEAD(&counter
->event_entry
);
2784 INIT_LIST_HEAD(&counter
->sibling_list
);
2785 init_waitqueue_head(&counter
->waitq
);
2787 mutex_init(&counter
->mmap_mutex
);
2789 INIT_LIST_HEAD(&counter
->child_list
);
2792 counter
->hw_event
= *hw_event
;
2793 counter
->group_leader
= group_leader
;
2794 counter
->pmu
= NULL
;
2797 counter
->state
= PERF_COUNTER_STATE_INACTIVE
;
2798 if (hw_event
->disabled
)
2799 counter
->state
= PERF_COUNTER_STATE_OFF
;
2803 if (perf_event_raw(hw_event
)) {
2804 pmu
= hw_perf_counter_init(counter
);
2808 switch (perf_event_type(hw_event
)) {
2809 case PERF_TYPE_HARDWARE
:
2810 pmu
= hw_perf_counter_init(counter
);
2813 case PERF_TYPE_SOFTWARE
:
2814 pmu
= sw_perf_counter_init(counter
);
2817 case PERF_TYPE_TRACEPOINT
:
2818 pmu
= tp_perf_counter_init(counter
);
2825 else if (IS_ERR(pmu
))
2830 return ERR_PTR(err
);
2835 if (counter
->hw_event
.mmap
)
2836 atomic_inc(&nr_mmap_tracking
);
2837 if (counter
->hw_event
.munmap
)
2838 atomic_inc(&nr_munmap_tracking
);
2839 if (counter
->hw_event
.comm
)
2840 atomic_inc(&nr_comm_tracking
);
2846 * sys_perf_counter_open - open a performance counter, associate it to a task/cpu
2848 * @hw_event_uptr: event type attributes for monitoring/sampling
2851 * @group_fd: group leader counter fd
2853 SYSCALL_DEFINE5(perf_counter_open
,
2854 const struct perf_counter_hw_event __user
*, hw_event_uptr
,
2855 pid_t
, pid
, int, cpu
, int, group_fd
, unsigned long, flags
)
2857 struct perf_counter
*counter
, *group_leader
;
2858 struct perf_counter_hw_event hw_event
;
2859 struct perf_counter_context
*ctx
;
2860 struct file
*counter_file
= NULL
;
2861 struct file
*group_file
= NULL
;
2862 int fput_needed
= 0;
2863 int fput_needed2
= 0;
2866 /* for future expandability... */
2870 if (copy_from_user(&hw_event
, hw_event_uptr
, sizeof(hw_event
)) != 0)
2874 * Get the target context (task or percpu):
2876 ctx
= find_get_context(pid
, cpu
);
2878 return PTR_ERR(ctx
);
2881 * Look up the group leader (we will attach this counter to it):
2883 group_leader
= NULL
;
2884 if (group_fd
!= -1) {
2886 group_file
= fget_light(group_fd
, &fput_needed
);
2888 goto err_put_context
;
2889 if (group_file
->f_op
!= &perf_fops
)
2890 goto err_put_context
;
2892 group_leader
= group_file
->private_data
;
2894 * Do not allow a recursive hierarchy (this new sibling
2895 * becoming part of another group-sibling):
2897 if (group_leader
->group_leader
!= group_leader
)
2898 goto err_put_context
;
2900 * Do not allow to attach to a group in a different
2901 * task or CPU context:
2903 if (group_leader
->ctx
!= ctx
)
2904 goto err_put_context
;
2906 * Only a group leader can be exclusive or pinned
2908 if (hw_event
.exclusive
|| hw_event
.pinned
)
2909 goto err_put_context
;
2912 counter
= perf_counter_alloc(&hw_event
, cpu
, ctx
, group_leader
,
2914 ret
= PTR_ERR(counter
);
2915 if (IS_ERR(counter
))
2916 goto err_put_context
;
2918 ret
= anon_inode_getfd("[perf_counter]", &perf_fops
, counter
, 0);
2920 goto err_free_put_context
;
2922 counter_file
= fget_light(ret
, &fput_needed2
);
2924 goto err_free_put_context
;
2926 counter
->filp
= counter_file
;
2927 mutex_lock(&ctx
->mutex
);
2928 perf_install_in_context(ctx
, counter
, cpu
);
2929 mutex_unlock(&ctx
->mutex
);
2931 fput_light(counter_file
, fput_needed2
);
2934 fput_light(group_file
, fput_needed
);
2938 err_free_put_context
:
2948 * Initialize the perf_counter context in a task_struct:
2951 __perf_counter_init_context(struct perf_counter_context
*ctx
,
2952 struct task_struct
*task
)
2954 memset(ctx
, 0, sizeof(*ctx
));
2955 spin_lock_init(&ctx
->lock
);
2956 mutex_init(&ctx
->mutex
);
2957 INIT_LIST_HEAD(&ctx
->counter_list
);
2958 INIT_LIST_HEAD(&ctx
->event_list
);
2963 * inherit a counter from parent task to child task:
2965 static struct perf_counter
*
2966 inherit_counter(struct perf_counter
*parent_counter
,
2967 struct task_struct
*parent
,
2968 struct perf_counter_context
*parent_ctx
,
2969 struct task_struct
*child
,
2970 struct perf_counter
*group_leader
,
2971 struct perf_counter_context
*child_ctx
)
2973 struct perf_counter
*child_counter
;
2976 * Instead of creating recursive hierarchies of counters,
2977 * we link inherited counters back to the original parent,
2978 * which has a filp for sure, which we use as the reference
2981 if (parent_counter
->parent
)
2982 parent_counter
= parent_counter
->parent
;
2984 child_counter
= perf_counter_alloc(&parent_counter
->hw_event
,
2985 parent_counter
->cpu
, child_ctx
,
2986 group_leader
, GFP_KERNEL
);
2987 if (IS_ERR(child_counter
))
2988 return child_counter
;
2991 * Link it up in the child's context:
2993 child_counter
->task
= child
;
2994 add_counter_to_ctx(child_counter
, child_ctx
);
2996 child_counter
->parent
= parent_counter
;
2998 * inherit into child's child as well:
3000 child_counter
->hw_event
.inherit
= 1;
3003 * Get a reference to the parent filp - we will fput it
3004 * when the child counter exits. This is safe to do because
3005 * we are in the parent and we know that the filp still
3006 * exists and has a nonzero count:
3008 atomic_long_inc(&parent_counter
->filp
->f_count
);
3011 * Link this into the parent counter's child list
3013 mutex_lock(&parent_counter
->mutex
);
3014 list_add_tail(&child_counter
->child_list
, &parent_counter
->child_list
);
3017 * Make the child state follow the state of the parent counter,
3018 * not its hw_event.disabled bit. We hold the parent's mutex,
3019 * so we won't race with perf_counter_{en,dis}able_family.
3021 if (parent_counter
->state
>= PERF_COUNTER_STATE_INACTIVE
)
3022 child_counter
->state
= PERF_COUNTER_STATE_INACTIVE
;
3024 child_counter
->state
= PERF_COUNTER_STATE_OFF
;
3026 mutex_unlock(&parent_counter
->mutex
);
3028 return child_counter
;
3031 static int inherit_group(struct perf_counter
*parent_counter
,
3032 struct task_struct
*parent
,
3033 struct perf_counter_context
*parent_ctx
,
3034 struct task_struct
*child
,
3035 struct perf_counter_context
*child_ctx
)
3037 struct perf_counter
*leader
;
3038 struct perf_counter
*sub
;
3039 struct perf_counter
*child_ctr
;
3041 leader
= inherit_counter(parent_counter
, parent
, parent_ctx
,
3042 child
, NULL
, child_ctx
);
3044 return PTR_ERR(leader
);
3045 list_for_each_entry(sub
, &parent_counter
->sibling_list
, list_entry
) {
3046 child_ctr
= inherit_counter(sub
, parent
, parent_ctx
,
3047 child
, leader
, child_ctx
);
3048 if (IS_ERR(child_ctr
))
3049 return PTR_ERR(child_ctr
);
3054 static void sync_child_counter(struct perf_counter
*child_counter
,
3055 struct perf_counter
*parent_counter
)
3057 u64 parent_val
, child_val
;
3059 parent_val
= atomic64_read(&parent_counter
->count
);
3060 child_val
= atomic64_read(&child_counter
->count
);
3063 * Add back the child's count to the parent's count:
3065 atomic64_add(child_val
, &parent_counter
->count
);
3066 atomic64_add(child_counter
->total_time_enabled
,
3067 &parent_counter
->child_total_time_enabled
);
3068 atomic64_add(child_counter
->total_time_running
,
3069 &parent_counter
->child_total_time_running
);
3072 * Remove this counter from the parent's list
3074 mutex_lock(&parent_counter
->mutex
);
3075 list_del_init(&child_counter
->child_list
);
3076 mutex_unlock(&parent_counter
->mutex
);
3079 * Release the parent counter, if this was the last
3082 fput(parent_counter
->filp
);
3086 __perf_counter_exit_task(struct task_struct
*child
,
3087 struct perf_counter
*child_counter
,
3088 struct perf_counter_context
*child_ctx
)
3090 struct perf_counter
*parent_counter
;
3091 struct perf_counter
*sub
, *tmp
;
3094 * If we do not self-reap then we have to wait for the
3095 * child task to unschedule (it will happen for sure),
3096 * so that its counter is at its final count. (This
3097 * condition triggers rarely - child tasks usually get
3098 * off their CPU before the parent has a chance to
3099 * get this far into the reaping action)
3101 if (child
!= current
) {
3102 wait_task_inactive(child
, 0);
3103 list_del_init(&child_counter
->list_entry
);
3104 update_counter_times(child_counter
);
3106 struct perf_cpu_context
*cpuctx
;
3107 unsigned long flags
;
3111 * Disable and unlink this counter.
3113 * Be careful about zapping the list - IRQ/NMI context
3114 * could still be processing it:
3116 local_irq_save(flags
);
3117 perf_flags
= hw_perf_save_disable();
3119 cpuctx
= &__get_cpu_var(perf_cpu_context
);
3121 group_sched_out(child_counter
, cpuctx
, child_ctx
);
3122 update_counter_times(child_counter
);
3124 list_del_init(&child_counter
->list_entry
);
3126 child_ctx
->nr_counters
--;
3128 hw_perf_restore(perf_flags
);
3129 local_irq_restore(flags
);
3132 parent_counter
= child_counter
->parent
;
3134 * It can happen that parent exits first, and has counters
3135 * that are still around due to the child reference. These
3136 * counters need to be zapped - but otherwise linger.
3138 if (parent_counter
) {
3139 sync_child_counter(child_counter
, parent_counter
);
3140 list_for_each_entry_safe(sub
, tmp
, &child_counter
->sibling_list
,
3143 sync_child_counter(sub
, sub
->parent
);
3147 free_counter(child_counter
);
3152 * When a child task exits, feed back counter values to parent counters.
3154 * Note: we may be running in child context, but the PID is not hashed
3155 * anymore so new counters will not be added.
3157 void perf_counter_exit_task(struct task_struct
*child
)
3159 struct perf_counter
*child_counter
, *tmp
;
3160 struct perf_counter_context
*child_ctx
;
3162 child_ctx
= &child
->perf_counter_ctx
;
3164 if (likely(!child_ctx
->nr_counters
))
3167 list_for_each_entry_safe(child_counter
, tmp
, &child_ctx
->counter_list
,
3169 __perf_counter_exit_task(child
, child_counter
, child_ctx
);
3173 * Initialize the perf_counter context in task_struct
3175 void perf_counter_init_task(struct task_struct
*child
)
3177 struct perf_counter_context
*child_ctx
, *parent_ctx
;
3178 struct perf_counter
*counter
;
3179 struct task_struct
*parent
= current
;
3181 child_ctx
= &child
->perf_counter_ctx
;
3182 parent_ctx
= &parent
->perf_counter_ctx
;
3184 __perf_counter_init_context(child_ctx
, child
);
3187 * This is executed from the parent task context, so inherit
3188 * counters that have been marked for cloning:
3191 if (likely(!parent_ctx
->nr_counters
))
3195 * Lock the parent list. No need to lock the child - not PID
3196 * hashed yet and not running, so nobody can access it.
3198 mutex_lock(&parent_ctx
->mutex
);
3201 * We dont have to disable NMIs - we are only looking at
3202 * the list, not manipulating it:
3204 list_for_each_entry(counter
, &parent_ctx
->counter_list
, list_entry
) {
3205 if (!counter
->hw_event
.inherit
)
3208 if (inherit_group(counter
, parent
,
3209 parent_ctx
, child
, child_ctx
))
3213 mutex_unlock(&parent_ctx
->mutex
);
3216 static void __cpuinit
perf_counter_init_cpu(int cpu
)
3218 struct perf_cpu_context
*cpuctx
;
3220 cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
3221 __perf_counter_init_context(&cpuctx
->ctx
, NULL
);
3223 spin_lock(&perf_resource_lock
);
3224 cpuctx
->max_pertask
= perf_max_counters
- perf_reserved_percpu
;
3225 spin_unlock(&perf_resource_lock
);
3227 hw_perf_counter_setup(cpu
);
3230 #ifdef CONFIG_HOTPLUG_CPU
3231 static void __perf_counter_exit_cpu(void *info
)
3233 struct perf_cpu_context
*cpuctx
= &__get_cpu_var(perf_cpu_context
);
3234 struct perf_counter_context
*ctx
= &cpuctx
->ctx
;
3235 struct perf_counter
*counter
, *tmp
;
3237 list_for_each_entry_safe(counter
, tmp
, &ctx
->counter_list
, list_entry
)
3238 __perf_counter_remove_from_context(counter
);
3240 static void perf_counter_exit_cpu(int cpu
)
3242 struct perf_cpu_context
*cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
3243 struct perf_counter_context
*ctx
= &cpuctx
->ctx
;
3245 mutex_lock(&ctx
->mutex
);
3246 smp_call_function_single(cpu
, __perf_counter_exit_cpu
, NULL
, 1);
3247 mutex_unlock(&ctx
->mutex
);
3250 static inline void perf_counter_exit_cpu(int cpu
) { }
3253 static int __cpuinit
3254 perf_cpu_notify(struct notifier_block
*self
, unsigned long action
, void *hcpu
)
3256 unsigned int cpu
= (long)hcpu
;
3260 case CPU_UP_PREPARE
:
3261 case CPU_UP_PREPARE_FROZEN
:
3262 perf_counter_init_cpu(cpu
);
3265 case CPU_DOWN_PREPARE
:
3266 case CPU_DOWN_PREPARE_FROZEN
:
3267 perf_counter_exit_cpu(cpu
);
3277 static struct notifier_block __cpuinitdata perf_cpu_nb
= {
3278 .notifier_call
= perf_cpu_notify
,
3281 void __init
perf_counter_init(void)
3283 perf_cpu_notify(&perf_cpu_nb
, (unsigned long)CPU_UP_PREPARE
,
3284 (void *)(long)smp_processor_id());
3285 register_cpu_notifier(&perf_cpu_nb
);
3288 static ssize_t
perf_show_reserve_percpu(struct sysdev_class
*class, char *buf
)
3290 return sprintf(buf
, "%d\n", perf_reserved_percpu
);
3294 perf_set_reserve_percpu(struct sysdev_class
*class,
3298 struct perf_cpu_context
*cpuctx
;
3302 err
= strict_strtoul(buf
, 10, &val
);
3305 if (val
> perf_max_counters
)
3308 spin_lock(&perf_resource_lock
);
3309 perf_reserved_percpu
= val
;
3310 for_each_online_cpu(cpu
) {
3311 cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
3312 spin_lock_irq(&cpuctx
->ctx
.lock
);
3313 mpt
= min(perf_max_counters
- cpuctx
->ctx
.nr_counters
,
3314 perf_max_counters
- perf_reserved_percpu
);
3315 cpuctx
->max_pertask
= mpt
;
3316 spin_unlock_irq(&cpuctx
->ctx
.lock
);
3318 spin_unlock(&perf_resource_lock
);
3323 static ssize_t
perf_show_overcommit(struct sysdev_class
*class, char *buf
)
3325 return sprintf(buf
, "%d\n", perf_overcommit
);
3329 perf_set_overcommit(struct sysdev_class
*class, const char *buf
, size_t count
)
3334 err
= strict_strtoul(buf
, 10, &val
);
3340 spin_lock(&perf_resource_lock
);
3341 perf_overcommit
= val
;
3342 spin_unlock(&perf_resource_lock
);
3347 static SYSDEV_CLASS_ATTR(
3350 perf_show_reserve_percpu
,
3351 perf_set_reserve_percpu
3354 static SYSDEV_CLASS_ATTR(
3357 perf_show_overcommit
,
3361 static struct attribute
*perfclass_attrs
[] = {
3362 &attr_reserve_percpu
.attr
,
3363 &attr_overcommit
.attr
,
3367 static struct attribute_group perfclass_attr_group
= {
3368 .attrs
= perfclass_attrs
,
3369 .name
= "perf_counters",
3372 static int __init
perf_counter_sysfs_init(void)
3374 return sysfs_create_group(&cpu_sysdev_class
.kset
.kobj
,
3375 &perfclass_attr_group
);
3377 device_initcall(perf_counter_sysfs_init
);