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 */
49 int sysctl_perf_counter_limit __read_mostly
= 100000; /* max NMIs per second */
52 * Lock for (sysadmin-configurable) counter reservations:
54 static DEFINE_SPINLOCK(perf_resource_lock
);
57 * Architecture provided APIs - weak aliases:
59 extern __weak
const struct pmu
*hw_perf_counter_init(struct perf_counter
*counter
)
64 void __weak
hw_perf_disable(void) { barrier(); }
65 void __weak
hw_perf_enable(void) { barrier(); }
67 void __weak
hw_perf_counter_setup(int cpu
) { barrier(); }
68 int __weak
hw_perf_group_sched_in(struct perf_counter
*group_leader
,
69 struct perf_cpu_context
*cpuctx
,
70 struct perf_counter_context
*ctx
, int cpu
)
75 void __weak
perf_counter_print_debug(void) { }
77 static DEFINE_PER_CPU(int, disable_count
);
79 void __perf_disable(void)
81 __get_cpu_var(disable_count
)++;
84 bool __perf_enable(void)
86 return !--__get_cpu_var(disable_count
);
89 void perf_disable(void)
95 void perf_enable(void)
101 static void get_ctx(struct perf_counter_context
*ctx
)
103 atomic_inc(&ctx
->refcount
);
106 static void free_ctx(struct rcu_head
*head
)
108 struct perf_counter_context
*ctx
;
110 ctx
= container_of(head
, struct perf_counter_context
, rcu_head
);
114 static void put_ctx(struct perf_counter_context
*ctx
)
116 if (atomic_dec_and_test(&ctx
->refcount
)) {
118 put_ctx(ctx
->parent_ctx
);
120 put_task_struct(ctx
->task
);
121 call_rcu(&ctx
->rcu_head
, free_ctx
);
126 * Add a counter from the lists for its context.
127 * Must be called with ctx->mutex and ctx->lock held.
130 list_add_counter(struct perf_counter
*counter
, struct perf_counter_context
*ctx
)
132 struct perf_counter
*group_leader
= counter
->group_leader
;
135 * Depending on whether it is a standalone or sibling counter,
136 * add it straight to the context's counter list, or to the group
137 * leader's sibling list:
139 if (group_leader
== counter
)
140 list_add_tail(&counter
->list_entry
, &ctx
->counter_list
);
142 list_add_tail(&counter
->list_entry
, &group_leader
->sibling_list
);
143 group_leader
->nr_siblings
++;
146 list_add_rcu(&counter
->event_entry
, &ctx
->event_list
);
151 * Remove a counter from the lists for its context.
152 * Must be called with ctx->mutex and ctx->lock held.
155 list_del_counter(struct perf_counter
*counter
, struct perf_counter_context
*ctx
)
157 struct perf_counter
*sibling
, *tmp
;
159 if (list_empty(&counter
->list_entry
))
163 list_del_init(&counter
->list_entry
);
164 list_del_rcu(&counter
->event_entry
);
166 if (counter
->group_leader
!= counter
)
167 counter
->group_leader
->nr_siblings
--;
170 * If this was a group counter with sibling counters then
171 * upgrade the siblings to singleton counters by adding them
172 * to the context list directly:
174 list_for_each_entry_safe(sibling
, tmp
,
175 &counter
->sibling_list
, list_entry
) {
177 list_move_tail(&sibling
->list_entry
, &ctx
->counter_list
);
178 sibling
->group_leader
= sibling
;
183 counter_sched_out(struct perf_counter
*counter
,
184 struct perf_cpu_context
*cpuctx
,
185 struct perf_counter_context
*ctx
)
187 if (counter
->state
!= PERF_COUNTER_STATE_ACTIVE
)
190 counter
->state
= PERF_COUNTER_STATE_INACTIVE
;
191 counter
->tstamp_stopped
= ctx
->time
;
192 counter
->pmu
->disable(counter
);
195 if (!is_software_counter(counter
))
196 cpuctx
->active_oncpu
--;
198 if (counter
->hw_event
.exclusive
|| !cpuctx
->active_oncpu
)
199 cpuctx
->exclusive
= 0;
203 group_sched_out(struct perf_counter
*group_counter
,
204 struct perf_cpu_context
*cpuctx
,
205 struct perf_counter_context
*ctx
)
207 struct perf_counter
*counter
;
209 if (group_counter
->state
!= PERF_COUNTER_STATE_ACTIVE
)
212 counter_sched_out(group_counter
, cpuctx
, ctx
);
215 * Schedule out siblings (if any):
217 list_for_each_entry(counter
, &group_counter
->sibling_list
, list_entry
)
218 counter_sched_out(counter
, cpuctx
, ctx
);
220 if (group_counter
->hw_event
.exclusive
)
221 cpuctx
->exclusive
= 0;
225 * Cross CPU call to remove a performance counter
227 * We disable the counter on the hardware level first. After that we
228 * remove it from the context list.
230 static void __perf_counter_remove_from_context(void *info
)
232 struct perf_cpu_context
*cpuctx
= &__get_cpu_var(perf_cpu_context
);
233 struct perf_counter
*counter
= info
;
234 struct perf_counter_context
*ctx
= counter
->ctx
;
238 * If this is a task context, we need to check whether it is
239 * the current task context of this cpu. If not it has been
240 * scheduled out before the smp call arrived.
242 if (ctx
->task
&& cpuctx
->task_ctx
!= ctx
)
245 spin_lock_irqsave(&ctx
->lock
, flags
);
247 * Protect the list operation against NMI by disabling the
248 * counters on a global level.
252 counter_sched_out(counter
, cpuctx
, ctx
);
254 list_del_counter(counter
, ctx
);
258 * Allow more per task counters with respect to the
261 cpuctx
->max_pertask
=
262 min(perf_max_counters
- ctx
->nr_counters
,
263 perf_max_counters
- perf_reserved_percpu
);
267 spin_unlock_irqrestore(&ctx
->lock
, flags
);
272 * Remove the counter from a task's (or a CPU's) list of counters.
274 * Must be called with ctx->mutex held.
276 * CPU counters are removed with a smp call. For task counters we only
277 * call when the task is on a CPU.
279 * If counter->ctx is a cloned context, callers must make sure that
280 * every task struct that counter->ctx->task could possibly point to
281 * remains valid. This is OK when called from perf_release since
282 * that only calls us on the top-level context, which can't be a clone.
283 * When called from perf_counter_exit_task, it's OK because the
284 * context has been detached from its task.
286 static void perf_counter_remove_from_context(struct perf_counter
*counter
)
288 struct perf_counter_context
*ctx
= counter
->ctx
;
289 struct task_struct
*task
= ctx
->task
;
293 * Per cpu counters are removed via an smp call and
294 * the removal is always sucessful.
296 smp_call_function_single(counter
->cpu
,
297 __perf_counter_remove_from_context
,
303 task_oncpu_function_call(task
, __perf_counter_remove_from_context
,
306 spin_lock_irq(&ctx
->lock
);
308 * If the context is active we need to retry the smp call.
310 if (ctx
->nr_active
&& !list_empty(&counter
->list_entry
)) {
311 spin_unlock_irq(&ctx
->lock
);
316 * The lock prevents that this context is scheduled in so we
317 * can remove the counter safely, if the call above did not
320 if (!list_empty(&counter
->list_entry
)) {
321 list_del_counter(counter
, ctx
);
323 spin_unlock_irq(&ctx
->lock
);
326 static inline u64
perf_clock(void)
328 return cpu_clock(smp_processor_id());
332 * Update the record of the current time in a context.
334 static void update_context_time(struct perf_counter_context
*ctx
)
336 u64 now
= perf_clock();
338 ctx
->time
+= now
- ctx
->timestamp
;
339 ctx
->timestamp
= now
;
343 * Update the total_time_enabled and total_time_running fields for a counter.
345 static void update_counter_times(struct perf_counter
*counter
)
347 struct perf_counter_context
*ctx
= counter
->ctx
;
350 if (counter
->state
< PERF_COUNTER_STATE_INACTIVE
)
353 counter
->total_time_enabled
= ctx
->time
- counter
->tstamp_enabled
;
355 if (counter
->state
== PERF_COUNTER_STATE_INACTIVE
)
356 run_end
= counter
->tstamp_stopped
;
360 counter
->total_time_running
= run_end
- counter
->tstamp_running
;
364 * Update total_time_enabled and total_time_running for all counters in a group.
366 static void update_group_times(struct perf_counter
*leader
)
368 struct perf_counter
*counter
;
370 update_counter_times(leader
);
371 list_for_each_entry(counter
, &leader
->sibling_list
, list_entry
)
372 update_counter_times(counter
);
376 * Cross CPU call to disable a performance counter
378 static void __perf_counter_disable(void *info
)
380 struct perf_counter
*counter
= info
;
381 struct perf_cpu_context
*cpuctx
= &__get_cpu_var(perf_cpu_context
);
382 struct perf_counter_context
*ctx
= counter
->ctx
;
386 * If this is a per-task counter, need to check whether this
387 * counter's task is the current task on this cpu.
389 if (ctx
->task
&& cpuctx
->task_ctx
!= ctx
)
392 spin_lock_irqsave(&ctx
->lock
, flags
);
395 * If the counter is on, turn it off.
396 * If it is in error state, leave it in error state.
398 if (counter
->state
>= PERF_COUNTER_STATE_INACTIVE
) {
399 update_context_time(ctx
);
400 update_counter_times(counter
);
401 if (counter
== counter
->group_leader
)
402 group_sched_out(counter
, cpuctx
, ctx
);
404 counter_sched_out(counter
, cpuctx
, ctx
);
405 counter
->state
= PERF_COUNTER_STATE_OFF
;
408 spin_unlock_irqrestore(&ctx
->lock
, flags
);
414 * If counter->ctx is a cloned context, callers must make sure that
415 * every task struct that counter->ctx->task could possibly point to
416 * remains valid. This condition is satisifed when called through
417 * perf_counter_for_each_child or perf_counter_for_each because they
418 * hold the top-level counter's child_mutex, so any descendant that
419 * goes to exit will block in sync_child_counter.
420 * When called from perf_pending_counter it's OK because counter->ctx
421 * is the current context on this CPU and preemption is disabled,
422 * hence we can't get into perf_counter_task_sched_out for this context.
424 static void perf_counter_disable(struct perf_counter
*counter
)
426 struct perf_counter_context
*ctx
= counter
->ctx
;
427 struct task_struct
*task
= ctx
->task
;
431 * Disable the counter on the cpu that it's on
433 smp_call_function_single(counter
->cpu
, __perf_counter_disable
,
439 task_oncpu_function_call(task
, __perf_counter_disable
, counter
);
441 spin_lock_irq(&ctx
->lock
);
443 * If the counter is still active, we need to retry the cross-call.
445 if (counter
->state
== PERF_COUNTER_STATE_ACTIVE
) {
446 spin_unlock_irq(&ctx
->lock
);
451 * Since we have the lock this context can't be scheduled
452 * in, so we can change the state safely.
454 if (counter
->state
== PERF_COUNTER_STATE_INACTIVE
) {
455 update_counter_times(counter
);
456 counter
->state
= PERF_COUNTER_STATE_OFF
;
459 spin_unlock_irq(&ctx
->lock
);
463 counter_sched_in(struct perf_counter
*counter
,
464 struct perf_cpu_context
*cpuctx
,
465 struct perf_counter_context
*ctx
,
468 if (counter
->state
<= PERF_COUNTER_STATE_OFF
)
471 counter
->state
= PERF_COUNTER_STATE_ACTIVE
;
472 counter
->oncpu
= cpu
; /* TODO: put 'cpu' into cpuctx->cpu */
474 * The new state must be visible before we turn it on in the hardware:
478 if (counter
->pmu
->enable(counter
)) {
479 counter
->state
= PERF_COUNTER_STATE_INACTIVE
;
484 counter
->tstamp_running
+= ctx
->time
- counter
->tstamp_stopped
;
486 if (!is_software_counter(counter
))
487 cpuctx
->active_oncpu
++;
490 if (counter
->hw_event
.exclusive
)
491 cpuctx
->exclusive
= 1;
497 group_sched_in(struct perf_counter
*group_counter
,
498 struct perf_cpu_context
*cpuctx
,
499 struct perf_counter_context
*ctx
,
502 struct perf_counter
*counter
, *partial_group
;
505 if (group_counter
->state
== PERF_COUNTER_STATE_OFF
)
508 ret
= hw_perf_group_sched_in(group_counter
, cpuctx
, ctx
, cpu
);
510 return ret
< 0 ? ret
: 0;
512 group_counter
->prev_state
= group_counter
->state
;
513 if (counter_sched_in(group_counter
, cpuctx
, ctx
, cpu
))
517 * Schedule in siblings as one group (if any):
519 list_for_each_entry(counter
, &group_counter
->sibling_list
, list_entry
) {
520 counter
->prev_state
= counter
->state
;
521 if (counter_sched_in(counter
, cpuctx
, ctx
, cpu
)) {
522 partial_group
= counter
;
531 * Groups can be scheduled in as one unit only, so undo any
532 * partial group before returning:
534 list_for_each_entry(counter
, &group_counter
->sibling_list
, list_entry
) {
535 if (counter
== partial_group
)
537 counter_sched_out(counter
, cpuctx
, ctx
);
539 counter_sched_out(group_counter
, cpuctx
, ctx
);
545 * Return 1 for a group consisting entirely of software counters,
546 * 0 if the group contains any hardware counters.
548 static int is_software_only_group(struct perf_counter
*leader
)
550 struct perf_counter
*counter
;
552 if (!is_software_counter(leader
))
555 list_for_each_entry(counter
, &leader
->sibling_list
, list_entry
)
556 if (!is_software_counter(counter
))
563 * Work out whether we can put this counter group on the CPU now.
565 static int group_can_go_on(struct perf_counter
*counter
,
566 struct perf_cpu_context
*cpuctx
,
570 * Groups consisting entirely of software counters can always go on.
572 if (is_software_only_group(counter
))
575 * If an exclusive group is already on, no other hardware
576 * counters can go on.
578 if (cpuctx
->exclusive
)
581 * If this group is exclusive and there are already
582 * counters on the CPU, it can't go on.
584 if (counter
->hw_event
.exclusive
&& cpuctx
->active_oncpu
)
587 * Otherwise, try to add it if all previous groups were able
593 static void add_counter_to_ctx(struct perf_counter
*counter
,
594 struct perf_counter_context
*ctx
)
596 list_add_counter(counter
, ctx
);
597 counter
->prev_state
= PERF_COUNTER_STATE_OFF
;
598 counter
->tstamp_enabled
= ctx
->time
;
599 counter
->tstamp_running
= ctx
->time
;
600 counter
->tstamp_stopped
= ctx
->time
;
604 * Cross CPU call to install and enable a performance counter
606 * Must be called with ctx->mutex held
608 static void __perf_install_in_context(void *info
)
610 struct perf_cpu_context
*cpuctx
= &__get_cpu_var(perf_cpu_context
);
611 struct perf_counter
*counter
= info
;
612 struct perf_counter_context
*ctx
= counter
->ctx
;
613 struct perf_counter
*leader
= counter
->group_leader
;
614 int cpu
= smp_processor_id();
619 * If this is a task context, we need to check whether it is
620 * the current task context of this cpu. If not it has been
621 * scheduled out before the smp call arrived.
622 * Or possibly this is the right context but it isn't
623 * on this cpu because it had no counters.
625 if (ctx
->task
&& cpuctx
->task_ctx
!= ctx
) {
626 if (cpuctx
->task_ctx
|| ctx
->task
!= current
)
628 cpuctx
->task_ctx
= ctx
;
631 spin_lock_irqsave(&ctx
->lock
, flags
);
633 update_context_time(ctx
);
636 * Protect the list operation against NMI by disabling the
637 * counters on a global level. NOP for non NMI based counters.
641 add_counter_to_ctx(counter
, ctx
);
644 * Don't put the counter on if it is disabled or if
645 * it is in a group and the group isn't on.
647 if (counter
->state
!= PERF_COUNTER_STATE_INACTIVE
||
648 (leader
!= counter
&& leader
->state
!= PERF_COUNTER_STATE_ACTIVE
))
652 * An exclusive counter can't go on if there are already active
653 * hardware counters, and no hardware counter can go on if there
654 * is already an exclusive counter on.
656 if (!group_can_go_on(counter
, cpuctx
, 1))
659 err
= counter_sched_in(counter
, cpuctx
, ctx
, cpu
);
663 * This counter couldn't go on. If it is in a group
664 * then we have to pull the whole group off.
665 * If the counter group is pinned then put it in error state.
667 if (leader
!= counter
)
668 group_sched_out(leader
, cpuctx
, ctx
);
669 if (leader
->hw_event
.pinned
) {
670 update_group_times(leader
);
671 leader
->state
= PERF_COUNTER_STATE_ERROR
;
675 if (!err
&& !ctx
->task
&& cpuctx
->max_pertask
)
676 cpuctx
->max_pertask
--;
681 spin_unlock_irqrestore(&ctx
->lock
, flags
);
685 * Attach a performance counter to a context
687 * First we add the counter to the list with the hardware enable bit
688 * in counter->hw_config cleared.
690 * If the counter is attached to a task which is on a CPU we use a smp
691 * call to enable it in the task context. The task might have been
692 * scheduled away, but we check this in the smp call again.
694 * Must be called with ctx->mutex held.
697 perf_install_in_context(struct perf_counter_context
*ctx
,
698 struct perf_counter
*counter
,
701 struct task_struct
*task
= ctx
->task
;
705 * Per cpu counters are installed via an smp call and
706 * the install is always sucessful.
708 smp_call_function_single(cpu
, __perf_install_in_context
,
714 task_oncpu_function_call(task
, __perf_install_in_context
,
717 spin_lock_irq(&ctx
->lock
);
719 * we need to retry the smp call.
721 if (ctx
->is_active
&& list_empty(&counter
->list_entry
)) {
722 spin_unlock_irq(&ctx
->lock
);
727 * The lock prevents that this context is scheduled in so we
728 * can add the counter safely, if it the call above did not
731 if (list_empty(&counter
->list_entry
))
732 add_counter_to_ctx(counter
, ctx
);
733 spin_unlock_irq(&ctx
->lock
);
737 * Cross CPU call to enable a performance counter
739 static void __perf_counter_enable(void *info
)
741 struct perf_counter
*counter
= info
;
742 struct perf_cpu_context
*cpuctx
= &__get_cpu_var(perf_cpu_context
);
743 struct perf_counter_context
*ctx
= counter
->ctx
;
744 struct perf_counter
*leader
= counter
->group_leader
;
749 * If this is a per-task counter, need to check whether this
750 * counter's task is the current task on this cpu.
752 if (ctx
->task
&& cpuctx
->task_ctx
!= ctx
) {
753 if (cpuctx
->task_ctx
|| ctx
->task
!= current
)
755 cpuctx
->task_ctx
= ctx
;
758 spin_lock_irqsave(&ctx
->lock
, flags
);
760 update_context_time(ctx
);
762 counter
->prev_state
= counter
->state
;
763 if (counter
->state
>= PERF_COUNTER_STATE_INACTIVE
)
765 counter
->state
= PERF_COUNTER_STATE_INACTIVE
;
766 counter
->tstamp_enabled
= ctx
->time
- counter
->total_time_enabled
;
769 * If the counter is in a group and isn't the group leader,
770 * then don't put it on unless the group is on.
772 if (leader
!= counter
&& leader
->state
!= PERF_COUNTER_STATE_ACTIVE
)
775 if (!group_can_go_on(counter
, cpuctx
, 1)) {
779 if (counter
== leader
)
780 err
= group_sched_in(counter
, cpuctx
, ctx
,
783 err
= counter_sched_in(counter
, cpuctx
, ctx
,
790 * If this counter can't go on and it's part of a
791 * group, then the whole group has to come off.
793 if (leader
!= counter
)
794 group_sched_out(leader
, cpuctx
, ctx
);
795 if (leader
->hw_event
.pinned
) {
796 update_group_times(leader
);
797 leader
->state
= PERF_COUNTER_STATE_ERROR
;
802 spin_unlock_irqrestore(&ctx
->lock
, flags
);
808 * If counter->ctx is a cloned context, callers must make sure that
809 * every task struct that counter->ctx->task could possibly point to
810 * remains valid. This condition is satisfied when called through
811 * perf_counter_for_each_child or perf_counter_for_each as described
812 * for perf_counter_disable.
814 static void perf_counter_enable(struct perf_counter
*counter
)
816 struct perf_counter_context
*ctx
= counter
->ctx
;
817 struct task_struct
*task
= ctx
->task
;
821 * Enable the counter on the cpu that it's on
823 smp_call_function_single(counter
->cpu
, __perf_counter_enable
,
828 spin_lock_irq(&ctx
->lock
);
829 if (counter
->state
>= PERF_COUNTER_STATE_INACTIVE
)
833 * If the counter is in error state, clear that first.
834 * That way, if we see the counter in error state below, we
835 * know that it has gone back into error state, as distinct
836 * from the task having been scheduled away before the
837 * cross-call arrived.
839 if (counter
->state
== PERF_COUNTER_STATE_ERROR
)
840 counter
->state
= PERF_COUNTER_STATE_OFF
;
843 spin_unlock_irq(&ctx
->lock
);
844 task_oncpu_function_call(task
, __perf_counter_enable
, counter
);
846 spin_lock_irq(&ctx
->lock
);
849 * If the context is active and the counter is still off,
850 * we need to retry the cross-call.
852 if (ctx
->is_active
&& counter
->state
== PERF_COUNTER_STATE_OFF
)
856 * Since we have the lock this context can't be scheduled
857 * in, so we can change the state safely.
859 if (counter
->state
== PERF_COUNTER_STATE_OFF
) {
860 counter
->state
= PERF_COUNTER_STATE_INACTIVE
;
861 counter
->tstamp_enabled
=
862 ctx
->time
- counter
->total_time_enabled
;
865 spin_unlock_irq(&ctx
->lock
);
868 static int perf_counter_refresh(struct perf_counter
*counter
, int refresh
)
871 * not supported on inherited counters
873 if (counter
->hw_event
.inherit
)
876 atomic_add(refresh
, &counter
->event_limit
);
877 perf_counter_enable(counter
);
882 void __perf_counter_sched_out(struct perf_counter_context
*ctx
,
883 struct perf_cpu_context
*cpuctx
)
885 struct perf_counter
*counter
;
887 spin_lock(&ctx
->lock
);
889 if (likely(!ctx
->nr_counters
))
891 update_context_time(ctx
);
894 if (ctx
->nr_active
) {
895 list_for_each_entry(counter
, &ctx
->counter_list
, list_entry
) {
896 if (counter
!= counter
->group_leader
)
897 counter_sched_out(counter
, cpuctx
, ctx
);
899 group_sched_out(counter
, cpuctx
, ctx
);
904 spin_unlock(&ctx
->lock
);
908 * Test whether two contexts are equivalent, i.e. whether they
909 * have both been cloned from the same version of the same context
910 * and they both have the same number of enabled counters.
911 * If the number of enabled counters is the same, then the set
912 * of enabled counters should be the same, because these are both
913 * inherited contexts, therefore we can't access individual counters
914 * in them directly with an fd; we can only enable/disable all
915 * counters via prctl, or enable/disable all counters in a family
916 * via ioctl, which will have the same effect on both contexts.
918 static int context_equiv(struct perf_counter_context
*ctx1
,
919 struct perf_counter_context
*ctx2
)
921 return ctx1
->parent_ctx
&& ctx1
->parent_ctx
== ctx2
->parent_ctx
922 && ctx1
->parent_gen
== ctx2
->parent_gen
923 && ctx1
->parent_gen
!= ~0ull;
927 * Called from scheduler to remove the counters of the current task,
928 * with interrupts disabled.
930 * We stop each counter and update the counter value in counter->count.
932 * This does not protect us against NMI, but disable()
933 * sets the disabled bit in the control field of counter _before_
934 * accessing the counter control register. If a NMI hits, then it will
935 * not restart the counter.
937 void perf_counter_task_sched_out(struct task_struct
*task
,
938 struct task_struct
*next
, int cpu
)
940 struct perf_cpu_context
*cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
941 struct perf_counter_context
*ctx
= task
->perf_counter_ctxp
;
942 struct perf_counter_context
*next_ctx
;
943 struct perf_counter_context
*parent
;
944 struct pt_regs
*regs
;
947 regs
= task_pt_regs(task
);
948 perf_swcounter_event(PERF_COUNT_CONTEXT_SWITCHES
, 1, 1, regs
, 0);
950 if (likely(!ctx
|| !cpuctx
->task_ctx
))
953 update_context_time(ctx
);
956 parent
= rcu_dereference(ctx
->parent_ctx
);
957 next_ctx
= next
->perf_counter_ctxp
;
958 if (parent
&& next_ctx
&&
959 rcu_dereference(next_ctx
->parent_ctx
) == parent
) {
961 * Looks like the two contexts are clones, so we might be
962 * able to optimize the context switch. We lock both
963 * contexts and check that they are clones under the
964 * lock (including re-checking that neither has been
965 * uncloned in the meantime). It doesn't matter which
966 * order we take the locks because no other cpu could
967 * be trying to lock both of these tasks.
969 spin_lock(&ctx
->lock
);
970 spin_lock_nested(&next_ctx
->lock
, SINGLE_DEPTH_NESTING
);
971 if (context_equiv(ctx
, next_ctx
)) {
972 task
->perf_counter_ctxp
= next_ctx
;
973 next
->perf_counter_ctxp
= ctx
;
975 next_ctx
->task
= task
;
978 spin_unlock(&next_ctx
->lock
);
979 spin_unlock(&ctx
->lock
);
984 __perf_counter_sched_out(ctx
, cpuctx
);
985 cpuctx
->task_ctx
= NULL
;
989 static void __perf_counter_task_sched_out(struct perf_counter_context
*ctx
)
991 struct perf_cpu_context
*cpuctx
= &__get_cpu_var(perf_cpu_context
);
993 if (!cpuctx
->task_ctx
)
995 __perf_counter_sched_out(ctx
, cpuctx
);
996 cpuctx
->task_ctx
= NULL
;
999 static void perf_counter_cpu_sched_out(struct perf_cpu_context
*cpuctx
)
1001 __perf_counter_sched_out(&cpuctx
->ctx
, cpuctx
);
1005 __perf_counter_sched_in(struct perf_counter_context
*ctx
,
1006 struct perf_cpu_context
*cpuctx
, int cpu
)
1008 struct perf_counter
*counter
;
1011 spin_lock(&ctx
->lock
);
1013 if (likely(!ctx
->nr_counters
))
1016 ctx
->timestamp
= perf_clock();
1021 * First go through the list and put on any pinned groups
1022 * in order to give them the best chance of going on.
1024 list_for_each_entry(counter
, &ctx
->counter_list
, list_entry
) {
1025 if (counter
->state
<= PERF_COUNTER_STATE_OFF
||
1026 !counter
->hw_event
.pinned
)
1028 if (counter
->cpu
!= -1 && counter
->cpu
!= cpu
)
1031 if (counter
!= counter
->group_leader
)
1032 counter_sched_in(counter
, cpuctx
, ctx
, cpu
);
1034 if (group_can_go_on(counter
, cpuctx
, 1))
1035 group_sched_in(counter
, cpuctx
, ctx
, cpu
);
1039 * If this pinned group hasn't been scheduled,
1040 * put it in error state.
1042 if (counter
->state
== PERF_COUNTER_STATE_INACTIVE
) {
1043 update_group_times(counter
);
1044 counter
->state
= PERF_COUNTER_STATE_ERROR
;
1048 list_for_each_entry(counter
, &ctx
->counter_list
, list_entry
) {
1050 * Ignore counters in OFF or ERROR state, and
1051 * ignore pinned counters since we did them already.
1053 if (counter
->state
<= PERF_COUNTER_STATE_OFF
||
1054 counter
->hw_event
.pinned
)
1058 * Listen to the 'cpu' scheduling filter constraint
1061 if (counter
->cpu
!= -1 && counter
->cpu
!= cpu
)
1064 if (counter
!= counter
->group_leader
) {
1065 if (counter_sched_in(counter
, cpuctx
, ctx
, cpu
))
1068 if (group_can_go_on(counter
, cpuctx
, can_add_hw
)) {
1069 if (group_sched_in(counter
, cpuctx
, ctx
, cpu
))
1076 spin_unlock(&ctx
->lock
);
1080 * Called from scheduler to add the counters of the current task
1081 * with interrupts disabled.
1083 * We restore the counter value and then enable it.
1085 * This does not protect us against NMI, but enable()
1086 * sets the enabled bit in the control field of counter _before_
1087 * accessing the counter control register. If a NMI hits, then it will
1088 * keep the counter running.
1090 void perf_counter_task_sched_in(struct task_struct
*task
, int cpu
)
1092 struct perf_cpu_context
*cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
1093 struct perf_counter_context
*ctx
= task
->perf_counter_ctxp
;
1097 if (cpuctx
->task_ctx
== ctx
)
1099 __perf_counter_sched_in(ctx
, cpuctx
, cpu
);
1100 cpuctx
->task_ctx
= ctx
;
1103 static void perf_counter_cpu_sched_in(struct perf_cpu_context
*cpuctx
, int cpu
)
1105 struct perf_counter_context
*ctx
= &cpuctx
->ctx
;
1107 __perf_counter_sched_in(ctx
, cpuctx
, cpu
);
1110 #define MAX_INTERRUPTS (~0ULL)
1112 static void perf_log_throttle(struct perf_counter
*counter
, int enable
);
1113 static void perf_log_period(struct perf_counter
*counter
, u64 period
);
1115 static void perf_adjust_freq(struct perf_counter_context
*ctx
)
1117 struct perf_counter
*counter
;
1118 u64 interrupts
, irq_period
;
1122 spin_lock(&ctx
->lock
);
1123 list_for_each_entry(counter
, &ctx
->counter_list
, list_entry
) {
1124 if (counter
->state
!= PERF_COUNTER_STATE_ACTIVE
)
1127 interrupts
= counter
->hw
.interrupts
;
1128 counter
->hw
.interrupts
= 0;
1130 if (interrupts
== MAX_INTERRUPTS
) {
1131 perf_log_throttle(counter
, 1);
1132 counter
->pmu
->unthrottle(counter
);
1133 interrupts
= 2*sysctl_perf_counter_limit
/HZ
;
1136 if (!counter
->hw_event
.freq
|| !counter
->hw_event
.irq_freq
)
1139 events
= HZ
* interrupts
* counter
->hw
.irq_period
;
1140 period
= div64_u64(events
, counter
->hw_event
.irq_freq
);
1142 delta
= (s64
)(1 + period
- counter
->hw
.irq_period
);
1145 irq_period
= counter
->hw
.irq_period
+ delta
;
1150 perf_log_period(counter
, irq_period
);
1152 counter
->hw
.irq_period
= irq_period
;
1154 spin_unlock(&ctx
->lock
);
1158 * Round-robin a context's counters:
1160 static void rotate_ctx(struct perf_counter_context
*ctx
)
1162 struct perf_counter
*counter
;
1164 if (!ctx
->nr_counters
)
1167 spin_lock(&ctx
->lock
);
1169 * Rotate the first entry last (works just fine for group counters too):
1172 list_for_each_entry(counter
, &ctx
->counter_list
, list_entry
) {
1173 list_move_tail(&counter
->list_entry
, &ctx
->counter_list
);
1178 spin_unlock(&ctx
->lock
);
1181 void perf_counter_task_tick(struct task_struct
*curr
, int cpu
)
1183 struct perf_cpu_context
*cpuctx
;
1184 struct perf_counter_context
*ctx
;
1186 if (!atomic_read(&nr_counters
))
1189 cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
1190 ctx
= curr
->perf_counter_ctxp
;
1192 perf_adjust_freq(&cpuctx
->ctx
);
1194 perf_adjust_freq(ctx
);
1196 perf_counter_cpu_sched_out(cpuctx
);
1198 __perf_counter_task_sched_out(ctx
);
1200 rotate_ctx(&cpuctx
->ctx
);
1204 perf_counter_cpu_sched_in(cpuctx
, cpu
);
1206 perf_counter_task_sched_in(curr
, cpu
);
1210 * Cross CPU call to read the hardware counter
1212 static void __read(void *info
)
1214 struct perf_counter
*counter
= info
;
1215 struct perf_counter_context
*ctx
= counter
->ctx
;
1216 unsigned long flags
;
1218 local_irq_save(flags
);
1220 update_context_time(ctx
);
1221 counter
->pmu
->read(counter
);
1222 update_counter_times(counter
);
1223 local_irq_restore(flags
);
1226 static u64
perf_counter_read(struct perf_counter
*counter
)
1229 * If counter is enabled and currently active on a CPU, update the
1230 * value in the counter structure:
1232 if (counter
->state
== PERF_COUNTER_STATE_ACTIVE
) {
1233 smp_call_function_single(counter
->oncpu
,
1234 __read
, counter
, 1);
1235 } else if (counter
->state
== PERF_COUNTER_STATE_INACTIVE
) {
1236 update_counter_times(counter
);
1239 return atomic64_read(&counter
->count
);
1243 * Initialize the perf_counter context in a task_struct:
1246 __perf_counter_init_context(struct perf_counter_context
*ctx
,
1247 struct task_struct
*task
)
1249 memset(ctx
, 0, sizeof(*ctx
));
1250 spin_lock_init(&ctx
->lock
);
1251 mutex_init(&ctx
->mutex
);
1252 INIT_LIST_HEAD(&ctx
->counter_list
);
1253 INIT_LIST_HEAD(&ctx
->event_list
);
1254 atomic_set(&ctx
->refcount
, 1);
1258 static struct perf_counter_context
*find_get_context(pid_t pid
, int cpu
)
1260 struct perf_cpu_context
*cpuctx
;
1261 struct perf_counter_context
*ctx
;
1262 struct perf_counter_context
*parent_ctx
;
1263 struct task_struct
*task
;
1267 * If cpu is not a wildcard then this is a percpu counter:
1270 /* Must be root to operate on a CPU counter: */
1271 if (sysctl_perf_counter_priv
&& !capable(CAP_SYS_ADMIN
))
1272 return ERR_PTR(-EACCES
);
1274 if (cpu
< 0 || cpu
> num_possible_cpus())
1275 return ERR_PTR(-EINVAL
);
1278 * We could be clever and allow to attach a counter to an
1279 * offline CPU and activate it when the CPU comes up, but
1282 if (!cpu_isset(cpu
, cpu_online_map
))
1283 return ERR_PTR(-ENODEV
);
1285 cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
1296 task
= find_task_by_vpid(pid
);
1298 get_task_struct(task
);
1302 return ERR_PTR(-ESRCH
);
1305 * Can't attach counters to a dying task.
1308 if (task
->flags
& PF_EXITING
)
1311 /* Reuse ptrace permission checks for now. */
1313 if (!ptrace_may_access(task
, PTRACE_MODE_READ
))
1319 ctx
= rcu_dereference(task
->perf_counter_ctxp
);
1322 * If this context is a clone of another, it might
1323 * get swapped for another underneath us by
1324 * perf_counter_task_sched_out, though the
1325 * rcu_read_lock() protects us from any context
1326 * getting freed. Lock the context and check if it
1327 * got swapped before we could get the lock, and retry
1328 * if so. If we locked the right context, then it
1329 * can't get swapped on us any more and we can
1330 * unclone it if necessary.
1331 * Once it's not a clone things will be stable.
1333 spin_lock_irq(&ctx
->lock
);
1334 if (ctx
!= rcu_dereference(task
->perf_counter_ctxp
)) {
1335 spin_unlock_irq(&ctx
->lock
);
1338 parent_ctx
= ctx
->parent_ctx
;
1340 put_ctx(parent_ctx
);
1341 ctx
->parent_ctx
= NULL
; /* no longer a clone */
1344 * Get an extra reference before dropping the lock so that
1345 * this context won't get freed if the task exits.
1348 spin_unlock_irq(&ctx
->lock
);
1353 ctx
= kmalloc(sizeof(struct perf_counter_context
), GFP_KERNEL
);
1357 __perf_counter_init_context(ctx
, task
);
1359 if (cmpxchg(&task
->perf_counter_ctxp
, NULL
, ctx
)) {
1361 * We raced with some other task; use
1362 * the context they set.
1367 get_task_struct(task
);
1370 put_task_struct(task
);
1374 put_task_struct(task
);
1375 return ERR_PTR(err
);
1378 static void free_counter_rcu(struct rcu_head
*head
)
1380 struct perf_counter
*counter
;
1382 counter
= container_of(head
, struct perf_counter
, rcu_head
);
1386 static void perf_pending_sync(struct perf_counter
*counter
);
1388 static void free_counter(struct perf_counter
*counter
)
1390 perf_pending_sync(counter
);
1392 atomic_dec(&nr_counters
);
1393 if (counter
->hw_event
.mmap
)
1394 atomic_dec(&nr_mmap_tracking
);
1395 if (counter
->hw_event
.munmap
)
1396 atomic_dec(&nr_munmap_tracking
);
1397 if (counter
->hw_event
.comm
)
1398 atomic_dec(&nr_comm_tracking
);
1400 if (counter
->destroy
)
1401 counter
->destroy(counter
);
1403 put_ctx(counter
->ctx
);
1404 call_rcu(&counter
->rcu_head
, free_counter_rcu
);
1408 * Called when the last reference to the file is gone.
1410 static int perf_release(struct inode
*inode
, struct file
*file
)
1412 struct perf_counter
*counter
= file
->private_data
;
1413 struct perf_counter_context
*ctx
= counter
->ctx
;
1415 file
->private_data
= NULL
;
1417 WARN_ON_ONCE(ctx
->parent_ctx
);
1418 mutex_lock(&ctx
->mutex
);
1419 perf_counter_remove_from_context(counter
);
1420 mutex_unlock(&ctx
->mutex
);
1422 mutex_lock(&counter
->owner
->perf_counter_mutex
);
1423 list_del_init(&counter
->owner_entry
);
1424 mutex_unlock(&counter
->owner
->perf_counter_mutex
);
1425 put_task_struct(counter
->owner
);
1427 free_counter(counter
);
1433 * Read the performance counter - simple non blocking version for now
1436 perf_read_hw(struct perf_counter
*counter
, char __user
*buf
, size_t count
)
1442 * Return end-of-file for a read on a counter that is in
1443 * error state (i.e. because it was pinned but it couldn't be
1444 * scheduled on to the CPU at some point).
1446 if (counter
->state
== PERF_COUNTER_STATE_ERROR
)
1449 WARN_ON_ONCE(counter
->ctx
->parent_ctx
);
1450 mutex_lock(&counter
->child_mutex
);
1451 values
[0] = perf_counter_read(counter
);
1453 if (counter
->hw_event
.read_format
& PERF_FORMAT_TOTAL_TIME_ENABLED
)
1454 values
[n
++] = counter
->total_time_enabled
+
1455 atomic64_read(&counter
->child_total_time_enabled
);
1456 if (counter
->hw_event
.read_format
& PERF_FORMAT_TOTAL_TIME_RUNNING
)
1457 values
[n
++] = counter
->total_time_running
+
1458 atomic64_read(&counter
->child_total_time_running
);
1459 mutex_unlock(&counter
->child_mutex
);
1461 if (count
< n
* sizeof(u64
))
1463 count
= n
* sizeof(u64
);
1465 if (copy_to_user(buf
, values
, count
))
1472 perf_read(struct file
*file
, char __user
*buf
, size_t count
, loff_t
*ppos
)
1474 struct perf_counter
*counter
= file
->private_data
;
1476 return perf_read_hw(counter
, buf
, count
);
1479 static unsigned int perf_poll(struct file
*file
, poll_table
*wait
)
1481 struct perf_counter
*counter
= file
->private_data
;
1482 struct perf_mmap_data
*data
;
1483 unsigned int events
= POLL_HUP
;
1486 data
= rcu_dereference(counter
->data
);
1488 events
= atomic_xchg(&data
->poll
, 0);
1491 poll_wait(file
, &counter
->waitq
, wait
);
1496 static void perf_counter_reset(struct perf_counter
*counter
)
1498 (void)perf_counter_read(counter
);
1499 atomic64_set(&counter
->count
, 0);
1500 perf_counter_update_userpage(counter
);
1503 static void perf_counter_for_each_sibling(struct perf_counter
*counter
,
1504 void (*func
)(struct perf_counter
*))
1506 struct perf_counter_context
*ctx
= counter
->ctx
;
1507 struct perf_counter
*sibling
;
1509 WARN_ON_ONCE(ctx
->parent_ctx
);
1510 mutex_lock(&ctx
->mutex
);
1511 counter
= counter
->group_leader
;
1514 list_for_each_entry(sibling
, &counter
->sibling_list
, list_entry
)
1516 mutex_unlock(&ctx
->mutex
);
1520 * Holding the top-level counter's child_mutex means that any
1521 * descendant process that has inherited this counter will block
1522 * in sync_child_counter if it goes to exit, thus satisfying the
1523 * task existence requirements of perf_counter_enable/disable.
1525 static void perf_counter_for_each_child(struct perf_counter
*counter
,
1526 void (*func
)(struct perf_counter
*))
1528 struct perf_counter
*child
;
1530 WARN_ON_ONCE(counter
->ctx
->parent_ctx
);
1531 mutex_lock(&counter
->child_mutex
);
1533 list_for_each_entry(child
, &counter
->child_list
, child_list
)
1535 mutex_unlock(&counter
->child_mutex
);
1538 static void perf_counter_for_each(struct perf_counter
*counter
,
1539 void (*func
)(struct perf_counter
*))
1541 struct perf_counter
*child
;
1543 WARN_ON_ONCE(counter
->ctx
->parent_ctx
);
1544 mutex_lock(&counter
->child_mutex
);
1545 perf_counter_for_each_sibling(counter
, func
);
1546 list_for_each_entry(child
, &counter
->child_list
, child_list
)
1547 perf_counter_for_each_sibling(child
, func
);
1548 mutex_unlock(&counter
->child_mutex
);
1551 static long perf_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
1553 struct perf_counter
*counter
= file
->private_data
;
1554 void (*func
)(struct perf_counter
*);
1558 case PERF_COUNTER_IOC_ENABLE
:
1559 func
= perf_counter_enable
;
1561 case PERF_COUNTER_IOC_DISABLE
:
1562 func
= perf_counter_disable
;
1564 case PERF_COUNTER_IOC_RESET
:
1565 func
= perf_counter_reset
;
1568 case PERF_COUNTER_IOC_REFRESH
:
1569 return perf_counter_refresh(counter
, arg
);
1574 if (flags
& PERF_IOC_FLAG_GROUP
)
1575 perf_counter_for_each(counter
, func
);
1577 perf_counter_for_each_child(counter
, func
);
1582 int perf_counter_task_enable(void)
1584 struct perf_counter
*counter
;
1586 mutex_lock(¤t
->perf_counter_mutex
);
1587 list_for_each_entry(counter
, ¤t
->perf_counter_list
, owner_entry
)
1588 perf_counter_for_each_child(counter
, perf_counter_enable
);
1589 mutex_unlock(¤t
->perf_counter_mutex
);
1594 int perf_counter_task_disable(void)
1596 struct perf_counter
*counter
;
1598 mutex_lock(¤t
->perf_counter_mutex
);
1599 list_for_each_entry(counter
, ¤t
->perf_counter_list
, owner_entry
)
1600 perf_counter_for_each_child(counter
, perf_counter_disable
);
1601 mutex_unlock(¤t
->perf_counter_mutex
);
1607 * Callers need to ensure there can be no nesting of this function, otherwise
1608 * the seqlock logic goes bad. We can not serialize this because the arch
1609 * code calls this from NMI context.
1611 void perf_counter_update_userpage(struct perf_counter
*counter
)
1613 struct perf_mmap_data
*data
;
1614 struct perf_counter_mmap_page
*userpg
;
1617 data
= rcu_dereference(counter
->data
);
1621 userpg
= data
->user_page
;
1624 * Disable preemption so as to not let the corresponding user-space
1625 * spin too long if we get preempted.
1630 userpg
->index
= counter
->hw
.idx
;
1631 userpg
->offset
= atomic64_read(&counter
->count
);
1632 if (counter
->state
== PERF_COUNTER_STATE_ACTIVE
)
1633 userpg
->offset
-= atomic64_read(&counter
->hw
.prev_count
);
1642 static int perf_mmap_fault(struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
1644 struct perf_counter
*counter
= vma
->vm_file
->private_data
;
1645 struct perf_mmap_data
*data
;
1646 int ret
= VM_FAULT_SIGBUS
;
1649 data
= rcu_dereference(counter
->data
);
1653 if (vmf
->pgoff
== 0) {
1654 vmf
->page
= virt_to_page(data
->user_page
);
1656 int nr
= vmf
->pgoff
- 1;
1658 if ((unsigned)nr
> data
->nr_pages
)
1661 vmf
->page
= virt_to_page(data
->data_pages
[nr
]);
1663 get_page(vmf
->page
);
1671 static int perf_mmap_data_alloc(struct perf_counter
*counter
, int nr_pages
)
1673 struct perf_mmap_data
*data
;
1677 WARN_ON(atomic_read(&counter
->mmap_count
));
1679 size
= sizeof(struct perf_mmap_data
);
1680 size
+= nr_pages
* sizeof(void *);
1682 data
= kzalloc(size
, GFP_KERNEL
);
1686 data
->user_page
= (void *)get_zeroed_page(GFP_KERNEL
);
1687 if (!data
->user_page
)
1688 goto fail_user_page
;
1690 for (i
= 0; i
< nr_pages
; i
++) {
1691 data
->data_pages
[i
] = (void *)get_zeroed_page(GFP_KERNEL
);
1692 if (!data
->data_pages
[i
])
1693 goto fail_data_pages
;
1696 data
->nr_pages
= nr_pages
;
1697 atomic_set(&data
->lock
, -1);
1699 rcu_assign_pointer(counter
->data
, data
);
1704 for (i
--; i
>= 0; i
--)
1705 free_page((unsigned long)data
->data_pages
[i
]);
1707 free_page((unsigned long)data
->user_page
);
1716 static void __perf_mmap_data_free(struct rcu_head
*rcu_head
)
1718 struct perf_mmap_data
*data
= container_of(rcu_head
,
1719 struct perf_mmap_data
, rcu_head
);
1722 free_page((unsigned long)data
->user_page
);
1723 for (i
= 0; i
< data
->nr_pages
; i
++)
1724 free_page((unsigned long)data
->data_pages
[i
]);
1728 static void perf_mmap_data_free(struct perf_counter
*counter
)
1730 struct perf_mmap_data
*data
= counter
->data
;
1732 WARN_ON(atomic_read(&counter
->mmap_count
));
1734 rcu_assign_pointer(counter
->data
, NULL
);
1735 call_rcu(&data
->rcu_head
, __perf_mmap_data_free
);
1738 static void perf_mmap_open(struct vm_area_struct
*vma
)
1740 struct perf_counter
*counter
= vma
->vm_file
->private_data
;
1742 atomic_inc(&counter
->mmap_count
);
1745 static void perf_mmap_close(struct vm_area_struct
*vma
)
1747 struct perf_counter
*counter
= vma
->vm_file
->private_data
;
1749 WARN_ON_ONCE(counter
->ctx
->parent_ctx
);
1750 if (atomic_dec_and_mutex_lock(&counter
->mmap_count
,
1751 &counter
->mmap_mutex
)) {
1752 struct user_struct
*user
= current_user();
1754 atomic_long_sub(counter
->data
->nr_pages
+ 1, &user
->locked_vm
);
1755 vma
->vm_mm
->locked_vm
-= counter
->data
->nr_locked
;
1756 perf_mmap_data_free(counter
);
1757 mutex_unlock(&counter
->mmap_mutex
);
1761 static struct vm_operations_struct perf_mmap_vmops
= {
1762 .open
= perf_mmap_open
,
1763 .close
= perf_mmap_close
,
1764 .fault
= perf_mmap_fault
,
1767 static int perf_mmap(struct file
*file
, struct vm_area_struct
*vma
)
1769 struct perf_counter
*counter
= file
->private_data
;
1770 struct user_struct
*user
= current_user();
1771 unsigned long vma_size
;
1772 unsigned long nr_pages
;
1773 unsigned long user_locked
, user_lock_limit
;
1774 unsigned long locked
, lock_limit
;
1775 long user_extra
, extra
;
1778 if (!(vma
->vm_flags
& VM_SHARED
) || (vma
->vm_flags
& VM_WRITE
))
1781 vma_size
= vma
->vm_end
- vma
->vm_start
;
1782 nr_pages
= (vma_size
/ PAGE_SIZE
) - 1;
1785 * If we have data pages ensure they're a power-of-two number, so we
1786 * can do bitmasks instead of modulo.
1788 if (nr_pages
!= 0 && !is_power_of_2(nr_pages
))
1791 if (vma_size
!= PAGE_SIZE
* (1 + nr_pages
))
1794 if (vma
->vm_pgoff
!= 0)
1797 WARN_ON_ONCE(counter
->ctx
->parent_ctx
);
1798 mutex_lock(&counter
->mmap_mutex
);
1799 if (atomic_inc_not_zero(&counter
->mmap_count
)) {
1800 if (nr_pages
!= counter
->data
->nr_pages
)
1805 user_extra
= nr_pages
+ 1;
1806 user_lock_limit
= sysctl_perf_counter_mlock
>> (PAGE_SHIFT
- 10);
1809 * Increase the limit linearly with more CPUs:
1811 user_lock_limit
*= num_online_cpus();
1813 user_locked
= atomic_long_read(&user
->locked_vm
) + user_extra
;
1816 if (user_locked
> user_lock_limit
)
1817 extra
= user_locked
- user_lock_limit
;
1819 lock_limit
= current
->signal
->rlim
[RLIMIT_MEMLOCK
].rlim_cur
;
1820 lock_limit
>>= PAGE_SHIFT
;
1821 locked
= vma
->vm_mm
->locked_vm
+ extra
;
1823 if ((locked
> lock_limit
) && !capable(CAP_IPC_LOCK
)) {
1828 WARN_ON(counter
->data
);
1829 ret
= perf_mmap_data_alloc(counter
, nr_pages
);
1833 atomic_set(&counter
->mmap_count
, 1);
1834 atomic_long_add(user_extra
, &user
->locked_vm
);
1835 vma
->vm_mm
->locked_vm
+= extra
;
1836 counter
->data
->nr_locked
= extra
;
1838 mutex_unlock(&counter
->mmap_mutex
);
1840 vma
->vm_flags
&= ~VM_MAYWRITE
;
1841 vma
->vm_flags
|= VM_RESERVED
;
1842 vma
->vm_ops
= &perf_mmap_vmops
;
1847 static int perf_fasync(int fd
, struct file
*filp
, int on
)
1849 struct perf_counter
*counter
= filp
->private_data
;
1850 struct inode
*inode
= filp
->f_path
.dentry
->d_inode
;
1853 mutex_lock(&inode
->i_mutex
);
1854 retval
= fasync_helper(fd
, filp
, on
, &counter
->fasync
);
1855 mutex_unlock(&inode
->i_mutex
);
1863 static const struct file_operations perf_fops
= {
1864 .release
= perf_release
,
1867 .unlocked_ioctl
= perf_ioctl
,
1868 .compat_ioctl
= perf_ioctl
,
1870 .fasync
= perf_fasync
,
1874 * Perf counter wakeup
1876 * If there's data, ensure we set the poll() state and publish everything
1877 * to user-space before waking everybody up.
1880 void perf_counter_wakeup(struct perf_counter
*counter
)
1882 wake_up_all(&counter
->waitq
);
1884 if (counter
->pending_kill
) {
1885 kill_fasync(&counter
->fasync
, SIGIO
, counter
->pending_kill
);
1886 counter
->pending_kill
= 0;
1893 * Handle the case where we need to wakeup up from NMI (or rq->lock) context.
1895 * The NMI bit means we cannot possibly take locks. Therefore, maintain a
1896 * single linked list and use cmpxchg() to add entries lockless.
1899 static void perf_pending_counter(struct perf_pending_entry
*entry
)
1901 struct perf_counter
*counter
= container_of(entry
,
1902 struct perf_counter
, pending
);
1904 if (counter
->pending_disable
) {
1905 counter
->pending_disable
= 0;
1906 perf_counter_disable(counter
);
1909 if (counter
->pending_wakeup
) {
1910 counter
->pending_wakeup
= 0;
1911 perf_counter_wakeup(counter
);
1915 #define PENDING_TAIL ((struct perf_pending_entry *)-1UL)
1917 static DEFINE_PER_CPU(struct perf_pending_entry
*, perf_pending_head
) = {
1921 static void perf_pending_queue(struct perf_pending_entry
*entry
,
1922 void (*func
)(struct perf_pending_entry
*))
1924 struct perf_pending_entry
**head
;
1926 if (cmpxchg(&entry
->next
, NULL
, PENDING_TAIL
) != NULL
)
1931 head
= &get_cpu_var(perf_pending_head
);
1934 entry
->next
= *head
;
1935 } while (cmpxchg(head
, entry
->next
, entry
) != entry
->next
);
1937 set_perf_counter_pending();
1939 put_cpu_var(perf_pending_head
);
1942 static int __perf_pending_run(void)
1944 struct perf_pending_entry
*list
;
1947 list
= xchg(&__get_cpu_var(perf_pending_head
), PENDING_TAIL
);
1948 while (list
!= PENDING_TAIL
) {
1949 void (*func
)(struct perf_pending_entry
*);
1950 struct perf_pending_entry
*entry
= list
;
1957 * Ensure we observe the unqueue before we issue the wakeup,
1958 * so that we won't be waiting forever.
1959 * -- see perf_not_pending().
1970 static inline int perf_not_pending(struct perf_counter
*counter
)
1973 * If we flush on whatever cpu we run, there is a chance we don't
1977 __perf_pending_run();
1981 * Ensure we see the proper queue state before going to sleep
1982 * so that we do not miss the wakeup. -- see perf_pending_handle()
1985 return counter
->pending
.next
== NULL
;
1988 static void perf_pending_sync(struct perf_counter
*counter
)
1990 wait_event(counter
->waitq
, perf_not_pending(counter
));
1993 void perf_counter_do_pending(void)
1995 __perf_pending_run();
1999 * Callchain support -- arch specific
2002 __weak
struct perf_callchain_entry
*perf_callchain(struct pt_regs
*regs
)
2011 struct perf_output_handle
{
2012 struct perf_counter
*counter
;
2013 struct perf_mmap_data
*data
;
2014 unsigned int offset
;
2019 unsigned long flags
;
2022 static void perf_output_wakeup(struct perf_output_handle
*handle
)
2024 atomic_set(&handle
->data
->poll
, POLL_IN
);
2027 handle
->counter
->pending_wakeup
= 1;
2028 perf_pending_queue(&handle
->counter
->pending
,
2029 perf_pending_counter
);
2031 perf_counter_wakeup(handle
->counter
);
2035 * Curious locking construct.
2037 * We need to ensure a later event doesn't publish a head when a former
2038 * event isn't done writing. However since we need to deal with NMIs we
2039 * cannot fully serialize things.
2041 * What we do is serialize between CPUs so we only have to deal with NMI
2042 * nesting on a single CPU.
2044 * We only publish the head (and generate a wakeup) when the outer-most
2047 static void perf_output_lock(struct perf_output_handle
*handle
)
2049 struct perf_mmap_data
*data
= handle
->data
;
2054 local_irq_save(handle
->flags
);
2055 cpu
= smp_processor_id();
2057 if (in_nmi() && atomic_read(&data
->lock
) == cpu
)
2060 while (atomic_cmpxchg(&data
->lock
, -1, cpu
) != -1)
2066 static void perf_output_unlock(struct perf_output_handle
*handle
)
2068 struct perf_mmap_data
*data
= handle
->data
;
2071 data
->done_head
= data
->head
;
2073 if (!handle
->locked
)
2078 * The xchg implies a full barrier that ensures all writes are done
2079 * before we publish the new head, matched by a rmb() in userspace when
2080 * reading this position.
2082 while ((head
= atomic_xchg(&data
->done_head
, 0)))
2083 data
->user_page
->data_head
= head
;
2086 * NMI can happen here, which means we can miss a done_head update.
2089 cpu
= atomic_xchg(&data
->lock
, -1);
2090 WARN_ON_ONCE(cpu
!= smp_processor_id());
2093 * Therefore we have to validate we did not indeed do so.
2095 if (unlikely(atomic_read(&data
->done_head
))) {
2097 * Since we had it locked, we can lock it again.
2099 while (atomic_cmpxchg(&data
->lock
, -1, cpu
) != -1)
2105 if (atomic_xchg(&data
->wakeup
, 0))
2106 perf_output_wakeup(handle
);
2108 local_irq_restore(handle
->flags
);
2111 static int perf_output_begin(struct perf_output_handle
*handle
,
2112 struct perf_counter
*counter
, unsigned int size
,
2113 int nmi
, int overflow
)
2115 struct perf_mmap_data
*data
;
2116 unsigned int offset
, head
;
2119 * For inherited counters we send all the output towards the parent.
2121 if (counter
->parent
)
2122 counter
= counter
->parent
;
2125 data
= rcu_dereference(counter
->data
);
2129 handle
->data
= data
;
2130 handle
->counter
= counter
;
2132 handle
->overflow
= overflow
;
2134 if (!data
->nr_pages
)
2137 perf_output_lock(handle
);
2140 offset
= head
= atomic_read(&data
->head
);
2142 } while (atomic_cmpxchg(&data
->head
, offset
, head
) != offset
);
2144 handle
->offset
= offset
;
2145 handle
->head
= head
;
2147 if ((offset
>> PAGE_SHIFT
) != (head
>> PAGE_SHIFT
))
2148 atomic_set(&data
->wakeup
, 1);
2153 perf_output_wakeup(handle
);
2160 static void perf_output_copy(struct perf_output_handle
*handle
,
2161 void *buf
, unsigned int len
)
2163 unsigned int pages_mask
;
2164 unsigned int offset
;
2168 offset
= handle
->offset
;
2169 pages_mask
= handle
->data
->nr_pages
- 1;
2170 pages
= handle
->data
->data_pages
;
2173 unsigned int page_offset
;
2176 nr
= (offset
>> PAGE_SHIFT
) & pages_mask
;
2177 page_offset
= offset
& (PAGE_SIZE
- 1);
2178 size
= min_t(unsigned int, PAGE_SIZE
- page_offset
, len
);
2180 memcpy(pages
[nr
] + page_offset
, buf
, size
);
2187 handle
->offset
= offset
;
2190 * Check we didn't copy past our reservation window, taking the
2191 * possible unsigned int wrap into account.
2193 WARN_ON_ONCE(((int)(handle
->head
- handle
->offset
)) < 0);
2196 #define perf_output_put(handle, x) \
2197 perf_output_copy((handle), &(x), sizeof(x))
2199 static void perf_output_end(struct perf_output_handle
*handle
)
2201 struct perf_counter
*counter
= handle
->counter
;
2202 struct perf_mmap_data
*data
= handle
->data
;
2204 int wakeup_events
= counter
->hw_event
.wakeup_events
;
2206 if (handle
->overflow
&& wakeup_events
) {
2207 int events
= atomic_inc_return(&data
->events
);
2208 if (events
>= wakeup_events
) {
2209 atomic_sub(wakeup_events
, &data
->events
);
2210 atomic_set(&data
->wakeup
, 1);
2214 perf_output_unlock(handle
);
2218 static void perf_counter_output(struct perf_counter
*counter
,
2219 int nmi
, struct pt_regs
*regs
, u64 addr
)
2222 u64 record_type
= counter
->hw_event
.record_type
;
2223 struct perf_output_handle handle
;
2224 struct perf_event_header header
;
2233 struct perf_callchain_entry
*callchain
= NULL
;
2234 int callchain_size
= 0;
2241 header
.size
= sizeof(header
);
2243 header
.misc
= PERF_EVENT_MISC_OVERFLOW
;
2244 header
.misc
|= perf_misc_flags(regs
);
2246 if (record_type
& PERF_RECORD_IP
) {
2247 ip
= perf_instruction_pointer(regs
);
2248 header
.type
|= PERF_RECORD_IP
;
2249 header
.size
+= sizeof(ip
);
2252 if (record_type
& PERF_RECORD_TID
) {
2253 /* namespace issues */
2254 tid_entry
.pid
= current
->group_leader
->pid
;
2255 tid_entry
.tid
= current
->pid
;
2257 header
.type
|= PERF_RECORD_TID
;
2258 header
.size
+= sizeof(tid_entry
);
2261 if (record_type
& PERF_RECORD_TIME
) {
2263 * Maybe do better on x86 and provide cpu_clock_nmi()
2265 time
= sched_clock();
2267 header
.type
|= PERF_RECORD_TIME
;
2268 header
.size
+= sizeof(u64
);
2271 if (record_type
& PERF_RECORD_ADDR
) {
2272 header
.type
|= PERF_RECORD_ADDR
;
2273 header
.size
+= sizeof(u64
);
2276 if (record_type
& PERF_RECORD_CONFIG
) {
2277 header
.type
|= PERF_RECORD_CONFIG
;
2278 header
.size
+= sizeof(u64
);
2281 if (record_type
& PERF_RECORD_CPU
) {
2282 header
.type
|= PERF_RECORD_CPU
;
2283 header
.size
+= sizeof(cpu_entry
);
2285 cpu_entry
.cpu
= raw_smp_processor_id();
2288 if (record_type
& PERF_RECORD_GROUP
) {
2289 header
.type
|= PERF_RECORD_GROUP
;
2290 header
.size
+= sizeof(u64
) +
2291 counter
->nr_siblings
* sizeof(group_entry
);
2294 if (record_type
& PERF_RECORD_CALLCHAIN
) {
2295 callchain
= perf_callchain(regs
);
2298 callchain_size
= (1 + callchain
->nr
) * sizeof(u64
);
2300 header
.type
|= PERF_RECORD_CALLCHAIN
;
2301 header
.size
+= callchain_size
;
2305 ret
= perf_output_begin(&handle
, counter
, header
.size
, nmi
, 1);
2309 perf_output_put(&handle
, header
);
2311 if (record_type
& PERF_RECORD_IP
)
2312 perf_output_put(&handle
, ip
);
2314 if (record_type
& PERF_RECORD_TID
)
2315 perf_output_put(&handle
, tid_entry
);
2317 if (record_type
& PERF_RECORD_TIME
)
2318 perf_output_put(&handle
, time
);
2320 if (record_type
& PERF_RECORD_ADDR
)
2321 perf_output_put(&handle
, addr
);
2323 if (record_type
& PERF_RECORD_CONFIG
)
2324 perf_output_put(&handle
, counter
->hw_event
.config
);
2326 if (record_type
& PERF_RECORD_CPU
)
2327 perf_output_put(&handle
, cpu_entry
);
2330 * XXX PERF_RECORD_GROUP vs inherited counters seems difficult.
2332 if (record_type
& PERF_RECORD_GROUP
) {
2333 struct perf_counter
*leader
, *sub
;
2334 u64 nr
= counter
->nr_siblings
;
2336 perf_output_put(&handle
, nr
);
2338 leader
= counter
->group_leader
;
2339 list_for_each_entry(sub
, &leader
->sibling_list
, list_entry
) {
2341 sub
->pmu
->read(sub
);
2343 group_entry
.event
= sub
->hw_event
.config
;
2344 group_entry
.counter
= atomic64_read(&sub
->count
);
2346 perf_output_put(&handle
, group_entry
);
2351 perf_output_copy(&handle
, callchain
, callchain_size
);
2353 perf_output_end(&handle
);
2360 struct perf_comm_event
{
2361 struct task_struct
*task
;
2366 struct perf_event_header header
;
2373 static void perf_counter_comm_output(struct perf_counter
*counter
,
2374 struct perf_comm_event
*comm_event
)
2376 struct perf_output_handle handle
;
2377 int size
= comm_event
->event
.header
.size
;
2378 int ret
= perf_output_begin(&handle
, counter
, size
, 0, 0);
2383 perf_output_put(&handle
, comm_event
->event
);
2384 perf_output_copy(&handle
, comm_event
->comm
,
2385 comm_event
->comm_size
);
2386 perf_output_end(&handle
);
2389 static int perf_counter_comm_match(struct perf_counter
*counter
,
2390 struct perf_comm_event
*comm_event
)
2392 if (counter
->hw_event
.comm
&&
2393 comm_event
->event
.header
.type
== PERF_EVENT_COMM
)
2399 static void perf_counter_comm_ctx(struct perf_counter_context
*ctx
,
2400 struct perf_comm_event
*comm_event
)
2402 struct perf_counter
*counter
;
2404 if (system_state
!= SYSTEM_RUNNING
|| list_empty(&ctx
->event_list
))
2408 list_for_each_entry_rcu(counter
, &ctx
->event_list
, event_entry
) {
2409 if (perf_counter_comm_match(counter
, comm_event
))
2410 perf_counter_comm_output(counter
, comm_event
);
2415 static void perf_counter_comm_event(struct perf_comm_event
*comm_event
)
2417 struct perf_cpu_context
*cpuctx
;
2419 char *comm
= comm_event
->task
->comm
;
2421 size
= ALIGN(strlen(comm
)+1, sizeof(u64
));
2423 comm_event
->comm
= comm
;
2424 comm_event
->comm_size
= size
;
2426 comm_event
->event
.header
.size
= sizeof(comm_event
->event
) + size
;
2428 cpuctx
= &get_cpu_var(perf_cpu_context
);
2429 perf_counter_comm_ctx(&cpuctx
->ctx
, comm_event
);
2430 put_cpu_var(perf_cpu_context
);
2432 perf_counter_comm_ctx(current
->perf_counter_ctxp
, comm_event
);
2435 void perf_counter_comm(struct task_struct
*task
)
2437 struct perf_comm_event comm_event
;
2439 if (!atomic_read(&nr_comm_tracking
))
2441 if (!current
->perf_counter_ctxp
)
2444 comm_event
= (struct perf_comm_event
){
2447 .header
= { .type
= PERF_EVENT_COMM
, },
2448 .pid
= task
->group_leader
->pid
,
2453 perf_counter_comm_event(&comm_event
);
2460 struct perf_mmap_event
{
2466 struct perf_event_header header
;
2476 static void perf_counter_mmap_output(struct perf_counter
*counter
,
2477 struct perf_mmap_event
*mmap_event
)
2479 struct perf_output_handle handle
;
2480 int size
= mmap_event
->event
.header
.size
;
2481 int ret
= perf_output_begin(&handle
, counter
, size
, 0, 0);
2486 perf_output_put(&handle
, mmap_event
->event
);
2487 perf_output_copy(&handle
, mmap_event
->file_name
,
2488 mmap_event
->file_size
);
2489 perf_output_end(&handle
);
2492 static int perf_counter_mmap_match(struct perf_counter
*counter
,
2493 struct perf_mmap_event
*mmap_event
)
2495 if (counter
->hw_event
.mmap
&&
2496 mmap_event
->event
.header
.type
== PERF_EVENT_MMAP
)
2499 if (counter
->hw_event
.munmap
&&
2500 mmap_event
->event
.header
.type
== PERF_EVENT_MUNMAP
)
2506 static void perf_counter_mmap_ctx(struct perf_counter_context
*ctx
,
2507 struct perf_mmap_event
*mmap_event
)
2509 struct perf_counter
*counter
;
2511 if (system_state
!= SYSTEM_RUNNING
|| list_empty(&ctx
->event_list
))
2515 list_for_each_entry_rcu(counter
, &ctx
->event_list
, event_entry
) {
2516 if (perf_counter_mmap_match(counter
, mmap_event
))
2517 perf_counter_mmap_output(counter
, mmap_event
);
2522 static void perf_counter_mmap_event(struct perf_mmap_event
*mmap_event
)
2524 struct perf_cpu_context
*cpuctx
;
2525 struct file
*file
= mmap_event
->file
;
2532 buf
= kzalloc(PATH_MAX
, GFP_KERNEL
);
2534 name
= strncpy(tmp
, "//enomem", sizeof(tmp
));
2537 name
= d_path(&file
->f_path
, buf
, PATH_MAX
);
2539 name
= strncpy(tmp
, "//toolong", sizeof(tmp
));
2543 name
= strncpy(tmp
, "//anon", sizeof(tmp
));
2548 size
= ALIGN(strlen(name
)+1, sizeof(u64
));
2550 mmap_event
->file_name
= name
;
2551 mmap_event
->file_size
= size
;
2553 mmap_event
->event
.header
.size
= sizeof(mmap_event
->event
) + size
;
2555 cpuctx
= &get_cpu_var(perf_cpu_context
);
2556 perf_counter_mmap_ctx(&cpuctx
->ctx
, mmap_event
);
2557 put_cpu_var(perf_cpu_context
);
2559 perf_counter_mmap_ctx(current
->perf_counter_ctxp
, mmap_event
);
2564 void perf_counter_mmap(unsigned long addr
, unsigned long len
,
2565 unsigned long pgoff
, struct file
*file
)
2567 struct perf_mmap_event mmap_event
;
2569 if (!atomic_read(&nr_mmap_tracking
))
2571 if (!current
->perf_counter_ctxp
)
2574 mmap_event
= (struct perf_mmap_event
){
2577 .header
= { .type
= PERF_EVENT_MMAP
, },
2578 .pid
= current
->group_leader
->pid
,
2579 .tid
= current
->pid
,
2586 perf_counter_mmap_event(&mmap_event
);
2589 void perf_counter_munmap(unsigned long addr
, unsigned long len
,
2590 unsigned long pgoff
, struct file
*file
)
2592 struct perf_mmap_event mmap_event
;
2594 if (!atomic_read(&nr_munmap_tracking
))
2597 mmap_event
= (struct perf_mmap_event
){
2600 .header
= { .type
= PERF_EVENT_MUNMAP
, },
2601 .pid
= current
->group_leader
->pid
,
2602 .tid
= current
->pid
,
2609 perf_counter_mmap_event(&mmap_event
);
2613 * Log irq_period changes so that analyzing tools can re-normalize the
2617 static void perf_log_period(struct perf_counter
*counter
, u64 period
)
2619 struct perf_output_handle handle
;
2623 struct perf_event_header header
;
2628 .type
= PERF_EVENT_PERIOD
,
2630 .size
= sizeof(freq_event
),
2632 .time
= sched_clock(),
2636 if (counter
->hw
.irq_period
== period
)
2639 ret
= perf_output_begin(&handle
, counter
, sizeof(freq_event
), 0, 0);
2643 perf_output_put(&handle
, freq_event
);
2644 perf_output_end(&handle
);
2648 * IRQ throttle logging
2651 static void perf_log_throttle(struct perf_counter
*counter
, int enable
)
2653 struct perf_output_handle handle
;
2657 struct perf_event_header header
;
2659 } throttle_event
= {
2661 .type
= PERF_EVENT_THROTTLE
+ 1,
2663 .size
= sizeof(throttle_event
),
2665 .time
= sched_clock(),
2668 ret
= perf_output_begin(&handle
, counter
, sizeof(throttle_event
), 1, 0);
2672 perf_output_put(&handle
, throttle_event
);
2673 perf_output_end(&handle
);
2677 * Generic counter overflow handling.
2680 int perf_counter_overflow(struct perf_counter
*counter
,
2681 int nmi
, struct pt_regs
*regs
, u64 addr
)
2683 int events
= atomic_read(&counter
->event_limit
);
2684 int throttle
= counter
->pmu
->unthrottle
!= NULL
;
2688 counter
->hw
.interrupts
++;
2689 } else if (counter
->hw
.interrupts
!= MAX_INTERRUPTS
) {
2690 counter
->hw
.interrupts
++;
2691 if (HZ
*counter
->hw
.interrupts
> (u64
)sysctl_perf_counter_limit
) {
2692 counter
->hw
.interrupts
= MAX_INTERRUPTS
;
2693 perf_log_throttle(counter
, 0);
2699 * XXX event_limit might not quite work as expected on inherited
2703 counter
->pending_kill
= POLL_IN
;
2704 if (events
&& atomic_dec_and_test(&counter
->event_limit
)) {
2706 counter
->pending_kill
= POLL_HUP
;
2708 counter
->pending_disable
= 1;
2709 perf_pending_queue(&counter
->pending
,
2710 perf_pending_counter
);
2712 perf_counter_disable(counter
);
2715 perf_counter_output(counter
, nmi
, regs
, addr
);
2720 * Generic software counter infrastructure
2723 static void perf_swcounter_update(struct perf_counter
*counter
)
2725 struct hw_perf_counter
*hwc
= &counter
->hw
;
2730 prev
= atomic64_read(&hwc
->prev_count
);
2731 now
= atomic64_read(&hwc
->count
);
2732 if (atomic64_cmpxchg(&hwc
->prev_count
, prev
, now
) != prev
)
2737 atomic64_add(delta
, &counter
->count
);
2738 atomic64_sub(delta
, &hwc
->period_left
);
2741 static void perf_swcounter_set_period(struct perf_counter
*counter
)
2743 struct hw_perf_counter
*hwc
= &counter
->hw
;
2744 s64 left
= atomic64_read(&hwc
->period_left
);
2745 s64 period
= hwc
->irq_period
;
2747 if (unlikely(left
<= -period
)) {
2749 atomic64_set(&hwc
->period_left
, left
);
2752 if (unlikely(left
<= 0)) {
2754 atomic64_add(period
, &hwc
->period_left
);
2757 atomic64_set(&hwc
->prev_count
, -left
);
2758 atomic64_set(&hwc
->count
, -left
);
2761 static enum hrtimer_restart
perf_swcounter_hrtimer(struct hrtimer
*hrtimer
)
2763 enum hrtimer_restart ret
= HRTIMER_RESTART
;
2764 struct perf_counter
*counter
;
2765 struct pt_regs
*regs
;
2768 counter
= container_of(hrtimer
, struct perf_counter
, hw
.hrtimer
);
2769 counter
->pmu
->read(counter
);
2771 regs
= get_irq_regs();
2773 * In case we exclude kernel IPs or are somehow not in interrupt
2774 * context, provide the next best thing, the user IP.
2776 if ((counter
->hw_event
.exclude_kernel
|| !regs
) &&
2777 !counter
->hw_event
.exclude_user
)
2778 regs
= task_pt_regs(current
);
2781 if (perf_counter_overflow(counter
, 0, regs
, 0))
2782 ret
= HRTIMER_NORESTART
;
2785 period
= max_t(u64
, 10000, counter
->hw
.irq_period
);
2786 hrtimer_forward_now(hrtimer
, ns_to_ktime(period
));
2791 static void perf_swcounter_overflow(struct perf_counter
*counter
,
2792 int nmi
, struct pt_regs
*regs
, u64 addr
)
2794 perf_swcounter_update(counter
);
2795 perf_swcounter_set_period(counter
);
2796 if (perf_counter_overflow(counter
, nmi
, regs
, addr
))
2797 /* soft-disable the counter */
2802 static int perf_swcounter_match(struct perf_counter
*counter
,
2803 enum perf_event_types type
,
2804 u32 event
, struct pt_regs
*regs
)
2806 if (counter
->state
!= PERF_COUNTER_STATE_ACTIVE
)
2809 if (perf_event_raw(&counter
->hw_event
))
2812 if (perf_event_type(&counter
->hw_event
) != type
)
2815 if (perf_event_id(&counter
->hw_event
) != event
)
2818 if (counter
->hw_event
.exclude_user
&& user_mode(regs
))
2821 if (counter
->hw_event
.exclude_kernel
&& !user_mode(regs
))
2827 static void perf_swcounter_add(struct perf_counter
*counter
, u64 nr
,
2828 int nmi
, struct pt_regs
*regs
, u64 addr
)
2830 int neg
= atomic64_add_negative(nr
, &counter
->hw
.count
);
2831 if (counter
->hw
.irq_period
&& !neg
)
2832 perf_swcounter_overflow(counter
, nmi
, regs
, addr
);
2835 static void perf_swcounter_ctx_event(struct perf_counter_context
*ctx
,
2836 enum perf_event_types type
, u32 event
,
2837 u64 nr
, int nmi
, struct pt_regs
*regs
,
2840 struct perf_counter
*counter
;
2842 if (system_state
!= SYSTEM_RUNNING
|| list_empty(&ctx
->event_list
))
2846 list_for_each_entry_rcu(counter
, &ctx
->event_list
, event_entry
) {
2847 if (perf_swcounter_match(counter
, type
, event
, regs
))
2848 perf_swcounter_add(counter
, nr
, nmi
, regs
, addr
);
2853 static int *perf_swcounter_recursion_context(struct perf_cpu_context
*cpuctx
)
2856 return &cpuctx
->recursion
[3];
2859 return &cpuctx
->recursion
[2];
2862 return &cpuctx
->recursion
[1];
2864 return &cpuctx
->recursion
[0];
2867 static void __perf_swcounter_event(enum perf_event_types type
, u32 event
,
2868 u64 nr
, int nmi
, struct pt_regs
*regs
,
2871 struct perf_cpu_context
*cpuctx
= &get_cpu_var(perf_cpu_context
);
2872 int *recursion
= perf_swcounter_recursion_context(cpuctx
);
2880 perf_swcounter_ctx_event(&cpuctx
->ctx
, type
, event
,
2881 nr
, nmi
, regs
, addr
);
2882 if (cpuctx
->task_ctx
) {
2883 perf_swcounter_ctx_event(cpuctx
->task_ctx
, type
, event
,
2884 nr
, nmi
, regs
, addr
);
2891 put_cpu_var(perf_cpu_context
);
2895 perf_swcounter_event(u32 event
, u64 nr
, int nmi
, struct pt_regs
*regs
, u64 addr
)
2897 __perf_swcounter_event(PERF_TYPE_SOFTWARE
, event
, nr
, nmi
, regs
, addr
);
2900 static void perf_swcounter_read(struct perf_counter
*counter
)
2902 perf_swcounter_update(counter
);
2905 static int perf_swcounter_enable(struct perf_counter
*counter
)
2907 perf_swcounter_set_period(counter
);
2911 static void perf_swcounter_disable(struct perf_counter
*counter
)
2913 perf_swcounter_update(counter
);
2916 static const struct pmu perf_ops_generic
= {
2917 .enable
= perf_swcounter_enable
,
2918 .disable
= perf_swcounter_disable
,
2919 .read
= perf_swcounter_read
,
2923 * Software counter: cpu wall time clock
2926 static void cpu_clock_perf_counter_update(struct perf_counter
*counter
)
2928 int cpu
= raw_smp_processor_id();
2932 now
= cpu_clock(cpu
);
2933 prev
= atomic64_read(&counter
->hw
.prev_count
);
2934 atomic64_set(&counter
->hw
.prev_count
, now
);
2935 atomic64_add(now
- prev
, &counter
->count
);
2938 static int cpu_clock_perf_counter_enable(struct perf_counter
*counter
)
2940 struct hw_perf_counter
*hwc
= &counter
->hw
;
2941 int cpu
= raw_smp_processor_id();
2943 atomic64_set(&hwc
->prev_count
, cpu_clock(cpu
));
2944 hrtimer_init(&hwc
->hrtimer
, CLOCK_MONOTONIC
, HRTIMER_MODE_REL
);
2945 hwc
->hrtimer
.function
= perf_swcounter_hrtimer
;
2946 if (hwc
->irq_period
) {
2947 u64 period
= max_t(u64
, 10000, hwc
->irq_period
);
2948 __hrtimer_start_range_ns(&hwc
->hrtimer
,
2949 ns_to_ktime(period
), 0,
2950 HRTIMER_MODE_REL
, 0);
2956 static void cpu_clock_perf_counter_disable(struct perf_counter
*counter
)
2958 if (counter
->hw
.irq_period
)
2959 hrtimer_cancel(&counter
->hw
.hrtimer
);
2960 cpu_clock_perf_counter_update(counter
);
2963 static void cpu_clock_perf_counter_read(struct perf_counter
*counter
)
2965 cpu_clock_perf_counter_update(counter
);
2968 static const struct pmu perf_ops_cpu_clock
= {
2969 .enable
= cpu_clock_perf_counter_enable
,
2970 .disable
= cpu_clock_perf_counter_disable
,
2971 .read
= cpu_clock_perf_counter_read
,
2975 * Software counter: task time clock
2978 static void task_clock_perf_counter_update(struct perf_counter
*counter
, u64 now
)
2983 prev
= atomic64_xchg(&counter
->hw
.prev_count
, now
);
2985 atomic64_add(delta
, &counter
->count
);
2988 static int task_clock_perf_counter_enable(struct perf_counter
*counter
)
2990 struct hw_perf_counter
*hwc
= &counter
->hw
;
2993 now
= counter
->ctx
->time
;
2995 atomic64_set(&hwc
->prev_count
, now
);
2996 hrtimer_init(&hwc
->hrtimer
, CLOCK_MONOTONIC
, HRTIMER_MODE_REL
);
2997 hwc
->hrtimer
.function
= perf_swcounter_hrtimer
;
2998 if (hwc
->irq_period
) {
2999 u64 period
= max_t(u64
, 10000, hwc
->irq_period
);
3000 __hrtimer_start_range_ns(&hwc
->hrtimer
,
3001 ns_to_ktime(period
), 0,
3002 HRTIMER_MODE_REL
, 0);
3008 static void task_clock_perf_counter_disable(struct perf_counter
*counter
)
3010 if (counter
->hw
.irq_period
)
3011 hrtimer_cancel(&counter
->hw
.hrtimer
);
3012 task_clock_perf_counter_update(counter
, counter
->ctx
->time
);
3016 static void task_clock_perf_counter_read(struct perf_counter
*counter
)
3021 update_context_time(counter
->ctx
);
3022 time
= counter
->ctx
->time
;
3024 u64 now
= perf_clock();
3025 u64 delta
= now
- counter
->ctx
->timestamp
;
3026 time
= counter
->ctx
->time
+ delta
;
3029 task_clock_perf_counter_update(counter
, time
);
3032 static const struct pmu perf_ops_task_clock
= {
3033 .enable
= task_clock_perf_counter_enable
,
3034 .disable
= task_clock_perf_counter_disable
,
3035 .read
= task_clock_perf_counter_read
,
3039 * Software counter: cpu migrations
3042 static inline u64
get_cpu_migrations(struct perf_counter
*counter
)
3044 struct task_struct
*curr
= counter
->ctx
->task
;
3047 return curr
->se
.nr_migrations
;
3048 return cpu_nr_migrations(smp_processor_id());
3051 static void cpu_migrations_perf_counter_update(struct perf_counter
*counter
)
3056 prev
= atomic64_read(&counter
->hw
.prev_count
);
3057 now
= get_cpu_migrations(counter
);
3059 atomic64_set(&counter
->hw
.prev_count
, now
);
3063 atomic64_add(delta
, &counter
->count
);
3066 static void cpu_migrations_perf_counter_read(struct perf_counter
*counter
)
3068 cpu_migrations_perf_counter_update(counter
);
3071 static int cpu_migrations_perf_counter_enable(struct perf_counter
*counter
)
3073 if (counter
->prev_state
<= PERF_COUNTER_STATE_OFF
)
3074 atomic64_set(&counter
->hw
.prev_count
,
3075 get_cpu_migrations(counter
));
3079 static void cpu_migrations_perf_counter_disable(struct perf_counter
*counter
)
3081 cpu_migrations_perf_counter_update(counter
);
3084 static const struct pmu perf_ops_cpu_migrations
= {
3085 .enable
= cpu_migrations_perf_counter_enable
,
3086 .disable
= cpu_migrations_perf_counter_disable
,
3087 .read
= cpu_migrations_perf_counter_read
,
3090 #ifdef CONFIG_EVENT_PROFILE
3091 void perf_tpcounter_event(int event_id
)
3093 struct pt_regs
*regs
= get_irq_regs();
3096 regs
= task_pt_regs(current
);
3098 __perf_swcounter_event(PERF_TYPE_TRACEPOINT
, event_id
, 1, 1, regs
, 0);
3100 EXPORT_SYMBOL_GPL(perf_tpcounter_event
);
3102 extern int ftrace_profile_enable(int);
3103 extern void ftrace_profile_disable(int);
3105 static void tp_perf_counter_destroy(struct perf_counter
*counter
)
3107 ftrace_profile_disable(perf_event_id(&counter
->hw_event
));
3110 static const struct pmu
*tp_perf_counter_init(struct perf_counter
*counter
)
3112 int event_id
= perf_event_id(&counter
->hw_event
);
3115 ret
= ftrace_profile_enable(event_id
);
3119 counter
->destroy
= tp_perf_counter_destroy
;
3120 counter
->hw
.irq_period
= counter
->hw_event
.irq_period
;
3122 return &perf_ops_generic
;
3125 static const struct pmu
*tp_perf_counter_init(struct perf_counter
*counter
)
3131 static const struct pmu
*sw_perf_counter_init(struct perf_counter
*counter
)
3133 const struct pmu
*pmu
= NULL
;
3136 * Software counters (currently) can't in general distinguish
3137 * between user, kernel and hypervisor events.
3138 * However, context switches and cpu migrations are considered
3139 * to be kernel events, and page faults are never hypervisor
3142 switch (perf_event_id(&counter
->hw_event
)) {
3143 case PERF_COUNT_CPU_CLOCK
:
3144 pmu
= &perf_ops_cpu_clock
;
3147 case PERF_COUNT_TASK_CLOCK
:
3149 * If the user instantiates this as a per-cpu counter,
3150 * use the cpu_clock counter instead.
3152 if (counter
->ctx
->task
)
3153 pmu
= &perf_ops_task_clock
;
3155 pmu
= &perf_ops_cpu_clock
;
3158 case PERF_COUNT_PAGE_FAULTS
:
3159 case PERF_COUNT_PAGE_FAULTS_MIN
:
3160 case PERF_COUNT_PAGE_FAULTS_MAJ
:
3161 case PERF_COUNT_CONTEXT_SWITCHES
:
3162 pmu
= &perf_ops_generic
;
3164 case PERF_COUNT_CPU_MIGRATIONS
:
3165 if (!counter
->hw_event
.exclude_kernel
)
3166 pmu
= &perf_ops_cpu_migrations
;
3174 * Allocate and initialize a counter structure
3176 static struct perf_counter
*
3177 perf_counter_alloc(struct perf_counter_hw_event
*hw_event
,
3179 struct perf_counter_context
*ctx
,
3180 struct perf_counter
*group_leader
,
3183 const struct pmu
*pmu
;
3184 struct perf_counter
*counter
;
3185 struct hw_perf_counter
*hwc
;
3188 counter
= kzalloc(sizeof(*counter
), gfpflags
);
3190 return ERR_PTR(-ENOMEM
);
3193 * Single counters are their own group leaders, with an
3194 * empty sibling list:
3197 group_leader
= counter
;
3199 mutex_init(&counter
->child_mutex
);
3200 INIT_LIST_HEAD(&counter
->child_list
);
3202 INIT_LIST_HEAD(&counter
->list_entry
);
3203 INIT_LIST_HEAD(&counter
->event_entry
);
3204 INIT_LIST_HEAD(&counter
->sibling_list
);
3205 init_waitqueue_head(&counter
->waitq
);
3207 mutex_init(&counter
->mmap_mutex
);
3210 counter
->hw_event
= *hw_event
;
3211 counter
->group_leader
= group_leader
;
3212 counter
->pmu
= NULL
;
3214 counter
->oncpu
= -1;
3216 counter
->state
= PERF_COUNTER_STATE_INACTIVE
;
3217 if (hw_event
->disabled
)
3218 counter
->state
= PERF_COUNTER_STATE_OFF
;
3223 if (hw_event
->freq
&& hw_event
->irq_freq
)
3224 hwc
->irq_period
= div64_u64(TICK_NSEC
, hw_event
->irq_freq
);
3226 hwc
->irq_period
= hw_event
->irq_period
;
3229 * we currently do not support PERF_RECORD_GROUP on inherited counters
3231 if (hw_event
->inherit
&& (hw_event
->record_type
& PERF_RECORD_GROUP
))
3234 if (perf_event_raw(hw_event
)) {
3235 pmu
= hw_perf_counter_init(counter
);
3239 switch (perf_event_type(hw_event
)) {
3240 case PERF_TYPE_HARDWARE
:
3241 pmu
= hw_perf_counter_init(counter
);
3244 case PERF_TYPE_SOFTWARE
:
3245 pmu
= sw_perf_counter_init(counter
);
3248 case PERF_TYPE_TRACEPOINT
:
3249 pmu
= tp_perf_counter_init(counter
);
3256 else if (IS_ERR(pmu
))
3261 return ERR_PTR(err
);
3266 atomic_inc(&nr_counters
);
3267 if (counter
->hw_event
.mmap
)
3268 atomic_inc(&nr_mmap_tracking
);
3269 if (counter
->hw_event
.munmap
)
3270 atomic_inc(&nr_munmap_tracking
);
3271 if (counter
->hw_event
.comm
)
3272 atomic_inc(&nr_comm_tracking
);
3278 * sys_perf_counter_open - open a performance counter, associate it to a task/cpu
3280 * @hw_event_uptr: event type attributes for monitoring/sampling
3283 * @group_fd: group leader counter fd
3285 SYSCALL_DEFINE5(perf_counter_open
,
3286 const struct perf_counter_hw_event __user
*, hw_event_uptr
,
3287 pid_t
, pid
, int, cpu
, int, group_fd
, unsigned long, flags
)
3289 struct perf_counter
*counter
, *group_leader
;
3290 struct perf_counter_hw_event hw_event
;
3291 struct perf_counter_context
*ctx
;
3292 struct file
*counter_file
= NULL
;
3293 struct file
*group_file
= NULL
;
3294 int fput_needed
= 0;
3295 int fput_needed2
= 0;
3298 /* for future expandability... */
3302 if (copy_from_user(&hw_event
, hw_event_uptr
, sizeof(hw_event
)) != 0)
3306 * Get the target context (task or percpu):
3308 ctx
= find_get_context(pid
, cpu
);
3310 return PTR_ERR(ctx
);
3313 * Look up the group leader (we will attach this counter to it):
3315 group_leader
= NULL
;
3316 if (group_fd
!= -1) {
3318 group_file
= fget_light(group_fd
, &fput_needed
);
3320 goto err_put_context
;
3321 if (group_file
->f_op
!= &perf_fops
)
3322 goto err_put_context
;
3324 group_leader
= group_file
->private_data
;
3326 * Do not allow a recursive hierarchy (this new sibling
3327 * becoming part of another group-sibling):
3329 if (group_leader
->group_leader
!= group_leader
)
3330 goto err_put_context
;
3332 * Do not allow to attach to a group in a different
3333 * task or CPU context:
3335 if (group_leader
->ctx
!= ctx
)
3336 goto err_put_context
;
3338 * Only a group leader can be exclusive or pinned
3340 if (hw_event
.exclusive
|| hw_event
.pinned
)
3341 goto err_put_context
;
3344 counter
= perf_counter_alloc(&hw_event
, cpu
, ctx
, group_leader
,
3346 ret
= PTR_ERR(counter
);
3347 if (IS_ERR(counter
))
3348 goto err_put_context
;
3350 ret
= anon_inode_getfd("[perf_counter]", &perf_fops
, counter
, 0);
3352 goto err_free_put_context
;
3354 counter_file
= fget_light(ret
, &fput_needed2
);
3356 goto err_free_put_context
;
3358 counter
->filp
= counter_file
;
3359 WARN_ON_ONCE(ctx
->parent_ctx
);
3360 mutex_lock(&ctx
->mutex
);
3361 perf_install_in_context(ctx
, counter
, cpu
);
3363 mutex_unlock(&ctx
->mutex
);
3365 counter
->owner
= current
;
3366 get_task_struct(current
);
3367 mutex_lock(¤t
->perf_counter_mutex
);
3368 list_add_tail(&counter
->owner_entry
, ¤t
->perf_counter_list
);
3369 mutex_unlock(¤t
->perf_counter_mutex
);
3371 fput_light(counter_file
, fput_needed2
);
3374 fput_light(group_file
, fput_needed
);
3378 err_free_put_context
:
3388 * inherit a counter from parent task to child task:
3390 static struct perf_counter
*
3391 inherit_counter(struct perf_counter
*parent_counter
,
3392 struct task_struct
*parent
,
3393 struct perf_counter_context
*parent_ctx
,
3394 struct task_struct
*child
,
3395 struct perf_counter
*group_leader
,
3396 struct perf_counter_context
*child_ctx
)
3398 struct perf_counter
*child_counter
;
3401 * Instead of creating recursive hierarchies of counters,
3402 * we link inherited counters back to the original parent,
3403 * which has a filp for sure, which we use as the reference
3406 if (parent_counter
->parent
)
3407 parent_counter
= parent_counter
->parent
;
3409 child_counter
= perf_counter_alloc(&parent_counter
->hw_event
,
3410 parent_counter
->cpu
, child_ctx
,
3411 group_leader
, GFP_KERNEL
);
3412 if (IS_ERR(child_counter
))
3413 return child_counter
;
3417 * Make the child state follow the state of the parent counter,
3418 * not its hw_event.disabled bit. We hold the parent's mutex,
3419 * so we won't race with perf_counter_{en,dis}able_family.
3421 if (parent_counter
->state
>= PERF_COUNTER_STATE_INACTIVE
)
3422 child_counter
->state
= PERF_COUNTER_STATE_INACTIVE
;
3424 child_counter
->state
= PERF_COUNTER_STATE_OFF
;
3427 * Link it up in the child's context:
3429 add_counter_to_ctx(child_counter
, child_ctx
);
3431 child_counter
->parent
= parent_counter
;
3433 * inherit into child's child as well:
3435 child_counter
->hw_event
.inherit
= 1;
3438 * Get a reference to the parent filp - we will fput it
3439 * when the child counter exits. This is safe to do because
3440 * we are in the parent and we know that the filp still
3441 * exists and has a nonzero count:
3443 atomic_long_inc(&parent_counter
->filp
->f_count
);
3446 * Link this into the parent counter's child list
3448 WARN_ON_ONCE(parent_counter
->ctx
->parent_ctx
);
3449 mutex_lock(&parent_counter
->child_mutex
);
3450 list_add_tail(&child_counter
->child_list
, &parent_counter
->child_list
);
3451 mutex_unlock(&parent_counter
->child_mutex
);
3453 return child_counter
;
3456 static int inherit_group(struct perf_counter
*parent_counter
,
3457 struct task_struct
*parent
,
3458 struct perf_counter_context
*parent_ctx
,
3459 struct task_struct
*child
,
3460 struct perf_counter_context
*child_ctx
)
3462 struct perf_counter
*leader
;
3463 struct perf_counter
*sub
;
3464 struct perf_counter
*child_ctr
;
3466 leader
= inherit_counter(parent_counter
, parent
, parent_ctx
,
3467 child
, NULL
, child_ctx
);
3469 return PTR_ERR(leader
);
3470 list_for_each_entry(sub
, &parent_counter
->sibling_list
, list_entry
) {
3471 child_ctr
= inherit_counter(sub
, parent
, parent_ctx
,
3472 child
, leader
, child_ctx
);
3473 if (IS_ERR(child_ctr
))
3474 return PTR_ERR(child_ctr
);
3479 static void sync_child_counter(struct perf_counter
*child_counter
,
3480 struct perf_counter
*parent_counter
)
3484 child_val
= atomic64_read(&child_counter
->count
);
3487 * Add back the child's count to the parent's count:
3489 atomic64_add(child_val
, &parent_counter
->count
);
3490 atomic64_add(child_counter
->total_time_enabled
,
3491 &parent_counter
->child_total_time_enabled
);
3492 atomic64_add(child_counter
->total_time_running
,
3493 &parent_counter
->child_total_time_running
);
3496 * Remove this counter from the parent's list
3498 WARN_ON_ONCE(parent_counter
->ctx
->parent_ctx
);
3499 mutex_lock(&parent_counter
->child_mutex
);
3500 list_del_init(&child_counter
->child_list
);
3501 mutex_unlock(&parent_counter
->child_mutex
);
3504 * Release the parent counter, if this was the last
3507 fput(parent_counter
->filp
);
3511 __perf_counter_exit_task(struct task_struct
*child
,
3512 struct perf_counter
*child_counter
,
3513 struct perf_counter_context
*child_ctx
)
3515 struct perf_counter
*parent_counter
;
3517 update_counter_times(child_counter
);
3518 perf_counter_remove_from_context(child_counter
);
3520 parent_counter
= child_counter
->parent
;
3522 * It can happen that parent exits first, and has counters
3523 * that are still around due to the child reference. These
3524 * counters need to be zapped - but otherwise linger.
3526 if (parent_counter
) {
3527 sync_child_counter(child_counter
, parent_counter
);
3528 free_counter(child_counter
);
3533 * When a child task exits, feed back counter values to parent counters.
3535 void perf_counter_exit_task(struct task_struct
*child
)
3537 struct perf_counter
*child_counter
, *tmp
;
3538 struct perf_counter_context
*child_ctx
;
3539 unsigned long flags
;
3541 if (likely(!child
->perf_counter_ctxp
))
3544 local_irq_save(flags
);
3546 * We can't reschedule here because interrupts are disabled,
3547 * and either child is current or it is a task that can't be
3548 * scheduled, so we are now safe from rescheduling changing
3551 child_ctx
= child
->perf_counter_ctxp
;
3552 __perf_counter_task_sched_out(child_ctx
);
3555 * Take the context lock here so that if find_get_context is
3556 * reading child->perf_counter_ctxp, we wait until it has
3557 * incremented the context's refcount before we do put_ctx below.
3559 spin_lock(&child_ctx
->lock
);
3560 child
->perf_counter_ctxp
= NULL
;
3561 if (child_ctx
->parent_ctx
) {
3563 * This context is a clone; unclone it so it can't get
3564 * swapped to another process while we're removing all
3565 * the counters from it.
3567 put_ctx(child_ctx
->parent_ctx
);
3568 child_ctx
->parent_ctx
= NULL
;
3570 spin_unlock(&child_ctx
->lock
);
3571 local_irq_restore(flags
);
3573 mutex_lock(&child_ctx
->mutex
);
3576 list_for_each_entry_safe(child_counter
, tmp
, &child_ctx
->counter_list
,
3578 __perf_counter_exit_task(child
, child_counter
, child_ctx
);
3581 * If the last counter was a group counter, it will have appended all
3582 * its siblings to the list, but we obtained 'tmp' before that which
3583 * will still point to the list head terminating the iteration.
3585 if (!list_empty(&child_ctx
->counter_list
))
3588 mutex_unlock(&child_ctx
->mutex
);
3594 * Initialize the perf_counter context in task_struct
3596 int perf_counter_init_task(struct task_struct
*child
)
3598 struct perf_counter_context
*child_ctx
, *parent_ctx
;
3599 struct perf_counter_context
*cloned_ctx
;
3600 struct perf_counter
*counter
;
3601 struct task_struct
*parent
= current
;
3602 int inherited_all
= 1;
3606 child
->perf_counter_ctxp
= NULL
;
3608 mutex_init(&child
->perf_counter_mutex
);
3609 INIT_LIST_HEAD(&child
->perf_counter_list
);
3611 if (likely(!parent
->perf_counter_ctxp
))
3615 * This is executed from the parent task context, so inherit
3616 * counters that have been marked for cloning.
3617 * First allocate and initialize a context for the child.
3620 child_ctx
= kmalloc(sizeof(struct perf_counter_context
), GFP_KERNEL
);
3624 __perf_counter_init_context(child_ctx
, child
);
3625 child
->perf_counter_ctxp
= child_ctx
;
3626 get_task_struct(child
);
3629 * If the parent's context is a clone, temporarily set its
3630 * parent_gen to an impossible value (all 1s) so it won't get
3631 * swapped under us. The rcu_read_lock makes sure that
3632 * parent_ctx continues to exist even if it gets swapped to
3633 * another process and then freed while we are trying to get
3638 parent_ctx
= rcu_dereference(parent
->perf_counter_ctxp
);
3640 * No need to check if parent_ctx != NULL here; since we saw
3641 * it non-NULL earlier, the only reason for it to become NULL
3642 * is if we exit, and since we're currently in the middle of
3643 * a fork we can't be exiting at the same time.
3645 spin_lock_irq(&parent_ctx
->lock
);
3646 if (parent_ctx
!= rcu_dereference(parent
->perf_counter_ctxp
)) {
3647 spin_unlock_irq(&parent_ctx
->lock
);
3650 cloned_gen
= parent_ctx
->parent_gen
;
3651 if (parent_ctx
->parent_ctx
)
3652 parent_ctx
->parent_gen
= ~0ull;
3653 spin_unlock_irq(&parent_ctx
->lock
);
3657 * Lock the parent list. No need to lock the child - not PID
3658 * hashed yet and not running, so nobody can access it.
3660 mutex_lock(&parent_ctx
->mutex
);
3663 * We dont have to disable NMIs - we are only looking at
3664 * the list, not manipulating it:
3666 list_for_each_entry_rcu(counter
, &parent_ctx
->event_list
, event_entry
) {
3667 if (counter
!= counter
->group_leader
)
3670 if (!counter
->hw_event
.inherit
) {
3675 ret
= inherit_group(counter
, parent
, parent_ctx
,
3683 if (inherited_all
) {
3685 * Mark the child context as a clone of the parent
3686 * context, or of whatever the parent is a clone of.
3687 * Note that if the parent is a clone, it could get
3688 * uncloned at any point, but that doesn't matter
3689 * because the list of counters and the generation
3690 * count can't have changed since we took the mutex.
3692 cloned_ctx
= rcu_dereference(parent_ctx
->parent_ctx
);
3694 child_ctx
->parent_ctx
= cloned_ctx
;
3695 child_ctx
->parent_gen
= cloned_gen
;
3697 child_ctx
->parent_ctx
= parent_ctx
;
3698 child_ctx
->parent_gen
= parent_ctx
->generation
;
3700 get_ctx(child_ctx
->parent_ctx
);
3703 mutex_unlock(&parent_ctx
->mutex
);
3706 * Restore the clone status of the parent.
3708 if (parent_ctx
->parent_ctx
) {
3709 spin_lock_irq(&parent_ctx
->lock
);
3710 if (parent_ctx
->parent_ctx
)
3711 parent_ctx
->parent_gen
= cloned_gen
;
3712 spin_unlock_irq(&parent_ctx
->lock
);
3718 static void __cpuinit
perf_counter_init_cpu(int cpu
)
3720 struct perf_cpu_context
*cpuctx
;
3722 cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
3723 __perf_counter_init_context(&cpuctx
->ctx
, NULL
);
3725 spin_lock(&perf_resource_lock
);
3726 cpuctx
->max_pertask
= perf_max_counters
- perf_reserved_percpu
;
3727 spin_unlock(&perf_resource_lock
);
3729 hw_perf_counter_setup(cpu
);
3732 #ifdef CONFIG_HOTPLUG_CPU
3733 static void __perf_counter_exit_cpu(void *info
)
3735 struct perf_cpu_context
*cpuctx
= &__get_cpu_var(perf_cpu_context
);
3736 struct perf_counter_context
*ctx
= &cpuctx
->ctx
;
3737 struct perf_counter
*counter
, *tmp
;
3739 list_for_each_entry_safe(counter
, tmp
, &ctx
->counter_list
, list_entry
)
3740 __perf_counter_remove_from_context(counter
);
3742 static void perf_counter_exit_cpu(int cpu
)
3744 struct perf_cpu_context
*cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
3745 struct perf_counter_context
*ctx
= &cpuctx
->ctx
;
3747 mutex_lock(&ctx
->mutex
);
3748 smp_call_function_single(cpu
, __perf_counter_exit_cpu
, NULL
, 1);
3749 mutex_unlock(&ctx
->mutex
);
3752 static inline void perf_counter_exit_cpu(int cpu
) { }
3755 static int __cpuinit
3756 perf_cpu_notify(struct notifier_block
*self
, unsigned long action
, void *hcpu
)
3758 unsigned int cpu
= (long)hcpu
;
3762 case CPU_UP_PREPARE
:
3763 case CPU_UP_PREPARE_FROZEN
:
3764 perf_counter_init_cpu(cpu
);
3767 case CPU_DOWN_PREPARE
:
3768 case CPU_DOWN_PREPARE_FROZEN
:
3769 perf_counter_exit_cpu(cpu
);
3779 static struct notifier_block __cpuinitdata perf_cpu_nb
= {
3780 .notifier_call
= perf_cpu_notify
,
3783 void __init
perf_counter_init(void)
3785 perf_cpu_notify(&perf_cpu_nb
, (unsigned long)CPU_UP_PREPARE
,
3786 (void *)(long)smp_processor_id());
3787 register_cpu_notifier(&perf_cpu_nb
);
3790 static ssize_t
perf_show_reserve_percpu(struct sysdev_class
*class, char *buf
)
3792 return sprintf(buf
, "%d\n", perf_reserved_percpu
);
3796 perf_set_reserve_percpu(struct sysdev_class
*class,
3800 struct perf_cpu_context
*cpuctx
;
3804 err
= strict_strtoul(buf
, 10, &val
);
3807 if (val
> perf_max_counters
)
3810 spin_lock(&perf_resource_lock
);
3811 perf_reserved_percpu
= val
;
3812 for_each_online_cpu(cpu
) {
3813 cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
3814 spin_lock_irq(&cpuctx
->ctx
.lock
);
3815 mpt
= min(perf_max_counters
- cpuctx
->ctx
.nr_counters
,
3816 perf_max_counters
- perf_reserved_percpu
);
3817 cpuctx
->max_pertask
= mpt
;
3818 spin_unlock_irq(&cpuctx
->ctx
.lock
);
3820 spin_unlock(&perf_resource_lock
);
3825 static ssize_t
perf_show_overcommit(struct sysdev_class
*class, char *buf
)
3827 return sprintf(buf
, "%d\n", perf_overcommit
);
3831 perf_set_overcommit(struct sysdev_class
*class, const char *buf
, size_t count
)
3836 err
= strict_strtoul(buf
, 10, &val
);
3842 spin_lock(&perf_resource_lock
);
3843 perf_overcommit
= val
;
3844 spin_unlock(&perf_resource_lock
);
3849 static SYSDEV_CLASS_ATTR(
3852 perf_show_reserve_percpu
,
3853 perf_set_reserve_percpu
3856 static SYSDEV_CLASS_ATTR(
3859 perf_show_overcommit
,
3863 static struct attribute
*perfclass_attrs
[] = {
3864 &attr_reserve_percpu
.attr
,
3865 &attr_overcommit
.attr
,
3869 static struct attribute_group perfclass_attr_group
= {
3870 .attrs
= perfclass_attrs
,
3871 .name
= "perf_counters",
3874 static int __init
perf_counter_sysfs_init(void)
3876 return sysfs_create_group(&cpu_sysdev_class
.kset
.kobj
,
3877 &perfclass_attr_group
);
3879 device_initcall(perf_counter_sysfs_init
);