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
;
237 local_irq_save(flags
);
239 * If this is a task context, we need to check whether it is
240 * the current task context of this cpu. If not it has been
241 * scheduled out before the smp call arrived.
243 if (ctx
->task
&& cpuctx
->task_ctx
!= ctx
) {
244 local_irq_restore(flags
);
248 spin_lock(&ctx
->lock
);
250 * Protect the list operation against NMI by disabling the
251 * counters on a global level.
255 counter_sched_out(counter
, cpuctx
, ctx
);
257 list_del_counter(counter
, ctx
);
261 * Allow more per task counters with respect to the
264 cpuctx
->max_pertask
=
265 min(perf_max_counters
- ctx
->nr_counters
,
266 perf_max_counters
- perf_reserved_percpu
);
270 spin_unlock_irqrestore(&ctx
->lock
, flags
);
275 * Remove the counter from a task's (or a CPU's) list of counters.
277 * Must be called with ctx->mutex held.
279 * CPU counters are removed with a smp call. For task counters we only
280 * call when the task is on a CPU.
282 * If counter->ctx is a cloned context, callers must make sure that
283 * every task struct that counter->ctx->task could possibly point to
284 * remains valid. This is OK when called from perf_release since
285 * that only calls us on the top-level context, which can't be a clone.
286 * When called from perf_counter_exit_task, it's OK because the
287 * context has been detached from its task.
289 static void perf_counter_remove_from_context(struct perf_counter
*counter
)
291 struct perf_counter_context
*ctx
= counter
->ctx
;
292 struct task_struct
*task
= ctx
->task
;
296 * Per cpu counters are removed via an smp call and
297 * the removal is always sucessful.
299 smp_call_function_single(counter
->cpu
,
300 __perf_counter_remove_from_context
,
306 task_oncpu_function_call(task
, __perf_counter_remove_from_context
,
309 spin_lock_irq(&ctx
->lock
);
311 * If the context is active we need to retry the smp call.
313 if (ctx
->nr_active
&& !list_empty(&counter
->list_entry
)) {
314 spin_unlock_irq(&ctx
->lock
);
319 * The lock prevents that this context is scheduled in so we
320 * can remove the counter safely, if the call above did not
323 if (!list_empty(&counter
->list_entry
)) {
324 list_del_counter(counter
, ctx
);
326 spin_unlock_irq(&ctx
->lock
);
329 static inline u64
perf_clock(void)
331 return cpu_clock(smp_processor_id());
335 * Update the record of the current time in a context.
337 static void update_context_time(struct perf_counter_context
*ctx
)
339 u64 now
= perf_clock();
341 ctx
->time
+= now
- ctx
->timestamp
;
342 ctx
->timestamp
= now
;
346 * Update the total_time_enabled and total_time_running fields for a counter.
348 static void update_counter_times(struct perf_counter
*counter
)
350 struct perf_counter_context
*ctx
= counter
->ctx
;
353 if (counter
->state
< PERF_COUNTER_STATE_INACTIVE
)
356 counter
->total_time_enabled
= ctx
->time
- counter
->tstamp_enabled
;
358 if (counter
->state
== PERF_COUNTER_STATE_INACTIVE
)
359 run_end
= counter
->tstamp_stopped
;
363 counter
->total_time_running
= run_end
- counter
->tstamp_running
;
367 * Update total_time_enabled and total_time_running for all counters in a group.
369 static void update_group_times(struct perf_counter
*leader
)
371 struct perf_counter
*counter
;
373 update_counter_times(leader
);
374 list_for_each_entry(counter
, &leader
->sibling_list
, list_entry
)
375 update_counter_times(counter
);
379 * Cross CPU call to disable a performance counter
381 static void __perf_counter_disable(void *info
)
383 struct perf_counter
*counter
= info
;
384 struct perf_cpu_context
*cpuctx
= &__get_cpu_var(perf_cpu_context
);
385 struct perf_counter_context
*ctx
= counter
->ctx
;
388 local_irq_save(flags
);
390 * If this is a per-task counter, need to check whether this
391 * counter's task is the current task on this cpu.
393 if (ctx
->task
&& cpuctx
->task_ctx
!= ctx
) {
394 local_irq_restore(flags
);
398 spin_lock(&ctx
->lock
);
401 * If the counter is on, turn it off.
402 * If it is in error state, leave it in error state.
404 if (counter
->state
>= PERF_COUNTER_STATE_INACTIVE
) {
405 update_context_time(ctx
);
406 update_counter_times(counter
);
407 if (counter
== counter
->group_leader
)
408 group_sched_out(counter
, cpuctx
, ctx
);
410 counter_sched_out(counter
, cpuctx
, ctx
);
411 counter
->state
= PERF_COUNTER_STATE_OFF
;
414 spin_unlock_irqrestore(&ctx
->lock
, flags
);
420 * If counter->ctx is a cloned context, callers must make sure that
421 * every task struct that counter->ctx->task could possibly point to
422 * remains valid. This condition is satisifed when called through
423 * perf_counter_for_each_child or perf_counter_for_each because they
424 * hold the top-level counter's child_mutex, so any descendant that
425 * goes to exit will block in sync_child_counter.
426 * When called from perf_pending_counter it's OK because counter->ctx
427 * is the current context on this CPU and preemption is disabled,
428 * hence we can't get into perf_counter_task_sched_out for this context.
430 static void perf_counter_disable(struct perf_counter
*counter
)
432 struct perf_counter_context
*ctx
= counter
->ctx
;
433 struct task_struct
*task
= ctx
->task
;
437 * Disable the counter on the cpu that it's on
439 smp_call_function_single(counter
->cpu
, __perf_counter_disable
,
445 task_oncpu_function_call(task
, __perf_counter_disable
, counter
);
447 spin_lock_irq(&ctx
->lock
);
449 * If the counter is still active, we need to retry the cross-call.
451 if (counter
->state
== PERF_COUNTER_STATE_ACTIVE
) {
452 spin_unlock_irq(&ctx
->lock
);
457 * Since we have the lock this context can't be scheduled
458 * in, so we can change the state safely.
460 if (counter
->state
== PERF_COUNTER_STATE_INACTIVE
) {
461 update_counter_times(counter
);
462 counter
->state
= PERF_COUNTER_STATE_OFF
;
465 spin_unlock_irq(&ctx
->lock
);
469 counter_sched_in(struct perf_counter
*counter
,
470 struct perf_cpu_context
*cpuctx
,
471 struct perf_counter_context
*ctx
,
474 if (counter
->state
<= PERF_COUNTER_STATE_OFF
)
477 counter
->state
= PERF_COUNTER_STATE_ACTIVE
;
478 counter
->oncpu
= cpu
; /* TODO: put 'cpu' into cpuctx->cpu */
480 * The new state must be visible before we turn it on in the hardware:
484 if (counter
->pmu
->enable(counter
)) {
485 counter
->state
= PERF_COUNTER_STATE_INACTIVE
;
490 counter
->tstamp_running
+= ctx
->time
- counter
->tstamp_stopped
;
492 if (!is_software_counter(counter
))
493 cpuctx
->active_oncpu
++;
496 if (counter
->hw_event
.exclusive
)
497 cpuctx
->exclusive
= 1;
503 group_sched_in(struct perf_counter
*group_counter
,
504 struct perf_cpu_context
*cpuctx
,
505 struct perf_counter_context
*ctx
,
508 struct perf_counter
*counter
, *partial_group
;
511 if (group_counter
->state
== PERF_COUNTER_STATE_OFF
)
514 ret
= hw_perf_group_sched_in(group_counter
, cpuctx
, ctx
, cpu
);
516 return ret
< 0 ? ret
: 0;
518 group_counter
->prev_state
= group_counter
->state
;
519 if (counter_sched_in(group_counter
, cpuctx
, ctx
, cpu
))
523 * Schedule in siblings as one group (if any):
525 list_for_each_entry(counter
, &group_counter
->sibling_list
, list_entry
) {
526 counter
->prev_state
= counter
->state
;
527 if (counter_sched_in(counter
, cpuctx
, ctx
, cpu
)) {
528 partial_group
= counter
;
537 * Groups can be scheduled in as one unit only, so undo any
538 * partial group before returning:
540 list_for_each_entry(counter
, &group_counter
->sibling_list
, list_entry
) {
541 if (counter
== partial_group
)
543 counter_sched_out(counter
, cpuctx
, ctx
);
545 counter_sched_out(group_counter
, cpuctx
, ctx
);
551 * Return 1 for a group consisting entirely of software counters,
552 * 0 if the group contains any hardware counters.
554 static int is_software_only_group(struct perf_counter
*leader
)
556 struct perf_counter
*counter
;
558 if (!is_software_counter(leader
))
561 list_for_each_entry(counter
, &leader
->sibling_list
, list_entry
)
562 if (!is_software_counter(counter
))
569 * Work out whether we can put this counter group on the CPU now.
571 static int group_can_go_on(struct perf_counter
*counter
,
572 struct perf_cpu_context
*cpuctx
,
576 * Groups consisting entirely of software counters can always go on.
578 if (is_software_only_group(counter
))
581 * If an exclusive group is already on, no other hardware
582 * counters can go on.
584 if (cpuctx
->exclusive
)
587 * If this group is exclusive and there are already
588 * counters on the CPU, it can't go on.
590 if (counter
->hw_event
.exclusive
&& cpuctx
->active_oncpu
)
593 * Otherwise, try to add it if all previous groups were able
599 static void add_counter_to_ctx(struct perf_counter
*counter
,
600 struct perf_counter_context
*ctx
)
602 list_add_counter(counter
, ctx
);
603 counter
->prev_state
= PERF_COUNTER_STATE_OFF
;
604 counter
->tstamp_enabled
= ctx
->time
;
605 counter
->tstamp_running
= ctx
->time
;
606 counter
->tstamp_stopped
= ctx
->time
;
610 * Cross CPU call to install and enable a performance counter
612 * Must be called with ctx->mutex held
614 static void __perf_install_in_context(void *info
)
616 struct perf_cpu_context
*cpuctx
= &__get_cpu_var(perf_cpu_context
);
617 struct perf_counter
*counter
= info
;
618 struct perf_counter_context
*ctx
= counter
->ctx
;
619 struct perf_counter
*leader
= counter
->group_leader
;
620 int cpu
= smp_processor_id();
624 local_irq_save(flags
);
626 * If this is a task context, we need to check whether it is
627 * the current task context of this cpu. If not it has been
628 * scheduled out before the smp call arrived.
629 * Or possibly this is the right context but it isn't
630 * on this cpu because it had no counters.
632 if (ctx
->task
&& cpuctx
->task_ctx
!= ctx
) {
633 if (cpuctx
->task_ctx
|| ctx
->task
!= current
) {
634 local_irq_restore(flags
);
637 cpuctx
->task_ctx
= ctx
;
640 spin_lock(&ctx
->lock
);
642 update_context_time(ctx
);
645 * Protect the list operation against NMI by disabling the
646 * counters on a global level. NOP for non NMI based counters.
650 add_counter_to_ctx(counter
, ctx
);
653 * Don't put the counter on if it is disabled or if
654 * it is in a group and the group isn't on.
656 if (counter
->state
!= PERF_COUNTER_STATE_INACTIVE
||
657 (leader
!= counter
&& leader
->state
!= PERF_COUNTER_STATE_ACTIVE
))
661 * An exclusive counter can't go on if there are already active
662 * hardware counters, and no hardware counter can go on if there
663 * is already an exclusive counter on.
665 if (!group_can_go_on(counter
, cpuctx
, 1))
668 err
= counter_sched_in(counter
, cpuctx
, ctx
, cpu
);
672 * This counter couldn't go on. If it is in a group
673 * then we have to pull the whole group off.
674 * If the counter group is pinned then put it in error state.
676 if (leader
!= counter
)
677 group_sched_out(leader
, cpuctx
, ctx
);
678 if (leader
->hw_event
.pinned
) {
679 update_group_times(leader
);
680 leader
->state
= PERF_COUNTER_STATE_ERROR
;
684 if (!err
&& !ctx
->task
&& cpuctx
->max_pertask
)
685 cpuctx
->max_pertask
--;
690 spin_unlock_irqrestore(&ctx
->lock
, flags
);
694 * Attach a performance counter to a context
696 * First we add the counter to the list with the hardware enable bit
697 * in counter->hw_config cleared.
699 * If the counter is attached to a task which is on a CPU we use a smp
700 * call to enable it in the task context. The task might have been
701 * scheduled away, but we check this in the smp call again.
703 * Must be called with ctx->mutex held.
706 perf_install_in_context(struct perf_counter_context
*ctx
,
707 struct perf_counter
*counter
,
710 struct task_struct
*task
= ctx
->task
;
714 * Per cpu counters are installed via an smp call and
715 * the install is always sucessful.
717 smp_call_function_single(cpu
, __perf_install_in_context
,
723 task_oncpu_function_call(task
, __perf_install_in_context
,
726 spin_lock_irq(&ctx
->lock
);
728 * we need to retry the smp call.
730 if (ctx
->is_active
&& list_empty(&counter
->list_entry
)) {
731 spin_unlock_irq(&ctx
->lock
);
736 * The lock prevents that this context is scheduled in so we
737 * can add the counter safely, if it the call above did not
740 if (list_empty(&counter
->list_entry
))
741 add_counter_to_ctx(counter
, ctx
);
742 spin_unlock_irq(&ctx
->lock
);
746 * Cross CPU call to enable a performance counter
748 static void __perf_counter_enable(void *info
)
750 struct perf_counter
*counter
= info
;
751 struct perf_cpu_context
*cpuctx
= &__get_cpu_var(perf_cpu_context
);
752 struct perf_counter_context
*ctx
= counter
->ctx
;
753 struct perf_counter
*leader
= counter
->group_leader
;
757 local_irq_save(flags
);
759 * If this is a per-task counter, need to check whether this
760 * counter's task is the current task on this cpu.
762 if (ctx
->task
&& cpuctx
->task_ctx
!= ctx
) {
763 if (cpuctx
->task_ctx
|| ctx
->task
!= current
) {
764 local_irq_restore(flags
);
767 cpuctx
->task_ctx
= ctx
;
770 spin_lock(&ctx
->lock
);
772 update_context_time(ctx
);
774 counter
->prev_state
= counter
->state
;
775 if (counter
->state
>= PERF_COUNTER_STATE_INACTIVE
)
777 counter
->state
= PERF_COUNTER_STATE_INACTIVE
;
778 counter
->tstamp_enabled
= ctx
->time
- counter
->total_time_enabled
;
781 * If the counter is in a group and isn't the group leader,
782 * then don't put it on unless the group is on.
784 if (leader
!= counter
&& leader
->state
!= PERF_COUNTER_STATE_ACTIVE
)
787 if (!group_can_go_on(counter
, cpuctx
, 1)) {
791 if (counter
== leader
)
792 err
= group_sched_in(counter
, cpuctx
, ctx
,
795 err
= counter_sched_in(counter
, cpuctx
, ctx
,
802 * If this counter can't go on and it's part of a
803 * group, then the whole group has to come off.
805 if (leader
!= counter
)
806 group_sched_out(leader
, cpuctx
, ctx
);
807 if (leader
->hw_event
.pinned
) {
808 update_group_times(leader
);
809 leader
->state
= PERF_COUNTER_STATE_ERROR
;
814 spin_unlock_irqrestore(&ctx
->lock
, flags
);
820 * If counter->ctx is a cloned context, callers must make sure that
821 * every task struct that counter->ctx->task could possibly point to
822 * remains valid. This condition is satisfied when called through
823 * perf_counter_for_each_child or perf_counter_for_each as described
824 * for perf_counter_disable.
826 static void perf_counter_enable(struct perf_counter
*counter
)
828 struct perf_counter_context
*ctx
= counter
->ctx
;
829 struct task_struct
*task
= ctx
->task
;
833 * Enable the counter on the cpu that it's on
835 smp_call_function_single(counter
->cpu
, __perf_counter_enable
,
840 spin_lock_irq(&ctx
->lock
);
841 if (counter
->state
>= PERF_COUNTER_STATE_INACTIVE
)
845 * If the counter is in error state, clear that first.
846 * That way, if we see the counter in error state below, we
847 * know that it has gone back into error state, as distinct
848 * from the task having been scheduled away before the
849 * cross-call arrived.
851 if (counter
->state
== PERF_COUNTER_STATE_ERROR
)
852 counter
->state
= PERF_COUNTER_STATE_OFF
;
855 spin_unlock_irq(&ctx
->lock
);
856 task_oncpu_function_call(task
, __perf_counter_enable
, counter
);
858 spin_lock_irq(&ctx
->lock
);
861 * If the context is active and the counter is still off,
862 * we need to retry the cross-call.
864 if (ctx
->is_active
&& counter
->state
== PERF_COUNTER_STATE_OFF
)
868 * Since we have the lock this context can't be scheduled
869 * in, so we can change the state safely.
871 if (counter
->state
== PERF_COUNTER_STATE_OFF
) {
872 counter
->state
= PERF_COUNTER_STATE_INACTIVE
;
873 counter
->tstamp_enabled
=
874 ctx
->time
- counter
->total_time_enabled
;
877 spin_unlock_irq(&ctx
->lock
);
880 static int perf_counter_refresh(struct perf_counter
*counter
, int refresh
)
883 * not supported on inherited counters
885 if (counter
->hw_event
.inherit
)
888 atomic_add(refresh
, &counter
->event_limit
);
889 perf_counter_enable(counter
);
894 void __perf_counter_sched_out(struct perf_counter_context
*ctx
,
895 struct perf_cpu_context
*cpuctx
)
897 struct perf_counter
*counter
;
899 spin_lock(&ctx
->lock
);
901 if (likely(!ctx
->nr_counters
))
903 update_context_time(ctx
);
906 if (ctx
->nr_active
) {
907 list_for_each_entry(counter
, &ctx
->counter_list
, list_entry
) {
908 if (counter
!= counter
->group_leader
)
909 counter_sched_out(counter
, cpuctx
, ctx
);
911 group_sched_out(counter
, cpuctx
, ctx
);
916 spin_unlock(&ctx
->lock
);
920 * Test whether two contexts are equivalent, i.e. whether they
921 * have both been cloned from the same version of the same context
922 * and they both have the same number of enabled counters.
923 * If the number of enabled counters is the same, then the set
924 * of enabled counters should be the same, because these are both
925 * inherited contexts, therefore we can't access individual counters
926 * in them directly with an fd; we can only enable/disable all
927 * counters via prctl, or enable/disable all counters in a family
928 * via ioctl, which will have the same effect on both contexts.
930 static int context_equiv(struct perf_counter_context
*ctx1
,
931 struct perf_counter_context
*ctx2
)
933 return ctx1
->parent_ctx
&& ctx1
->parent_ctx
== ctx2
->parent_ctx
934 && ctx1
->parent_gen
== ctx2
->parent_gen
935 && ctx1
->parent_gen
!= ~0ull;
939 * Called from scheduler to remove the counters of the current task,
940 * with interrupts disabled.
942 * We stop each counter and update the counter value in counter->count.
944 * This does not protect us against NMI, but disable()
945 * sets the disabled bit in the control field of counter _before_
946 * accessing the counter control register. If a NMI hits, then it will
947 * not restart the counter.
949 void perf_counter_task_sched_out(struct task_struct
*task
,
950 struct task_struct
*next
, int cpu
)
952 struct perf_cpu_context
*cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
953 struct perf_counter_context
*ctx
= task
->perf_counter_ctxp
;
954 struct perf_counter_context
*next_ctx
;
955 struct perf_counter_context
*parent
;
956 struct pt_regs
*regs
;
959 regs
= task_pt_regs(task
);
960 perf_swcounter_event(PERF_COUNT_CONTEXT_SWITCHES
, 1, 1, regs
, 0);
962 if (likely(!ctx
|| !cpuctx
->task_ctx
))
965 update_context_time(ctx
);
968 parent
= rcu_dereference(ctx
->parent_ctx
);
969 next_ctx
= next
->perf_counter_ctxp
;
970 if (parent
&& next_ctx
&&
971 rcu_dereference(next_ctx
->parent_ctx
) == parent
) {
973 * Looks like the two contexts are clones, so we might be
974 * able to optimize the context switch. We lock both
975 * contexts and check that they are clones under the
976 * lock (including re-checking that neither has been
977 * uncloned in the meantime). It doesn't matter which
978 * order we take the locks because no other cpu could
979 * be trying to lock both of these tasks.
981 spin_lock(&ctx
->lock
);
982 spin_lock_nested(&next_ctx
->lock
, SINGLE_DEPTH_NESTING
);
983 if (context_equiv(ctx
, next_ctx
)) {
984 task
->perf_counter_ctxp
= next_ctx
;
985 next
->perf_counter_ctxp
= ctx
;
987 next_ctx
->task
= task
;
990 spin_unlock(&next_ctx
->lock
);
991 spin_unlock(&ctx
->lock
);
996 __perf_counter_sched_out(ctx
, cpuctx
);
997 cpuctx
->task_ctx
= NULL
;
1001 static void __perf_counter_task_sched_out(struct perf_counter_context
*ctx
)
1003 struct perf_cpu_context
*cpuctx
= &__get_cpu_var(perf_cpu_context
);
1005 if (!cpuctx
->task_ctx
)
1007 __perf_counter_sched_out(ctx
, cpuctx
);
1008 cpuctx
->task_ctx
= NULL
;
1011 static void perf_counter_cpu_sched_out(struct perf_cpu_context
*cpuctx
)
1013 __perf_counter_sched_out(&cpuctx
->ctx
, cpuctx
);
1017 __perf_counter_sched_in(struct perf_counter_context
*ctx
,
1018 struct perf_cpu_context
*cpuctx
, int cpu
)
1020 struct perf_counter
*counter
;
1023 spin_lock(&ctx
->lock
);
1025 if (likely(!ctx
->nr_counters
))
1028 ctx
->timestamp
= perf_clock();
1033 * First go through the list and put on any pinned groups
1034 * in order to give them the best chance of going on.
1036 list_for_each_entry(counter
, &ctx
->counter_list
, list_entry
) {
1037 if (counter
->state
<= PERF_COUNTER_STATE_OFF
||
1038 !counter
->hw_event
.pinned
)
1040 if (counter
->cpu
!= -1 && counter
->cpu
!= cpu
)
1043 if (counter
!= counter
->group_leader
)
1044 counter_sched_in(counter
, cpuctx
, ctx
, cpu
);
1046 if (group_can_go_on(counter
, cpuctx
, 1))
1047 group_sched_in(counter
, cpuctx
, ctx
, cpu
);
1051 * If this pinned group hasn't been scheduled,
1052 * put it in error state.
1054 if (counter
->state
== PERF_COUNTER_STATE_INACTIVE
) {
1055 update_group_times(counter
);
1056 counter
->state
= PERF_COUNTER_STATE_ERROR
;
1060 list_for_each_entry(counter
, &ctx
->counter_list
, list_entry
) {
1062 * Ignore counters in OFF or ERROR state, and
1063 * ignore pinned counters since we did them already.
1065 if (counter
->state
<= PERF_COUNTER_STATE_OFF
||
1066 counter
->hw_event
.pinned
)
1070 * Listen to the 'cpu' scheduling filter constraint
1073 if (counter
->cpu
!= -1 && counter
->cpu
!= cpu
)
1076 if (counter
!= counter
->group_leader
) {
1077 if (counter_sched_in(counter
, cpuctx
, ctx
, cpu
))
1080 if (group_can_go_on(counter
, cpuctx
, can_add_hw
)) {
1081 if (group_sched_in(counter
, cpuctx
, ctx
, cpu
))
1088 spin_unlock(&ctx
->lock
);
1092 * Called from scheduler to add the counters of the current task
1093 * with interrupts disabled.
1095 * We restore the counter value and then enable it.
1097 * This does not protect us against NMI, but enable()
1098 * sets the enabled bit in the control field of counter _before_
1099 * accessing the counter control register. If a NMI hits, then it will
1100 * keep the counter running.
1102 void perf_counter_task_sched_in(struct task_struct
*task
, int cpu
)
1104 struct perf_cpu_context
*cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
1105 struct perf_counter_context
*ctx
= task
->perf_counter_ctxp
;
1109 if (cpuctx
->task_ctx
== ctx
)
1111 __perf_counter_sched_in(ctx
, cpuctx
, cpu
);
1112 cpuctx
->task_ctx
= ctx
;
1115 static void perf_counter_cpu_sched_in(struct perf_cpu_context
*cpuctx
, int cpu
)
1117 struct perf_counter_context
*ctx
= &cpuctx
->ctx
;
1119 __perf_counter_sched_in(ctx
, cpuctx
, cpu
);
1122 #define MAX_INTERRUPTS (~0ULL)
1124 static void perf_log_throttle(struct perf_counter
*counter
, int enable
);
1125 static void perf_log_period(struct perf_counter
*counter
, u64 period
);
1127 static void perf_adjust_freq(struct perf_counter_context
*ctx
)
1129 struct perf_counter
*counter
;
1130 u64 interrupts
, irq_period
;
1134 spin_lock(&ctx
->lock
);
1135 list_for_each_entry(counter
, &ctx
->counter_list
, list_entry
) {
1136 if (counter
->state
!= PERF_COUNTER_STATE_ACTIVE
)
1139 interrupts
= counter
->hw
.interrupts
;
1140 counter
->hw
.interrupts
= 0;
1142 if (interrupts
== MAX_INTERRUPTS
) {
1143 perf_log_throttle(counter
, 1);
1144 counter
->pmu
->unthrottle(counter
);
1145 interrupts
= 2*sysctl_perf_counter_limit
/HZ
;
1148 if (!counter
->hw_event
.freq
|| !counter
->hw_event
.irq_freq
)
1151 events
= HZ
* interrupts
* counter
->hw
.irq_period
;
1152 period
= div64_u64(events
, counter
->hw_event
.irq_freq
);
1154 delta
= (s64
)(1 + period
- counter
->hw
.irq_period
);
1157 irq_period
= counter
->hw
.irq_period
+ delta
;
1162 perf_log_period(counter
, irq_period
);
1164 counter
->hw
.irq_period
= irq_period
;
1166 spin_unlock(&ctx
->lock
);
1170 * Round-robin a context's counters:
1172 static void rotate_ctx(struct perf_counter_context
*ctx
)
1174 struct perf_counter
*counter
;
1176 if (!ctx
->nr_counters
)
1179 spin_lock(&ctx
->lock
);
1181 * Rotate the first entry last (works just fine for group counters too):
1184 list_for_each_entry(counter
, &ctx
->counter_list
, list_entry
) {
1185 list_move_tail(&counter
->list_entry
, &ctx
->counter_list
);
1190 spin_unlock(&ctx
->lock
);
1193 void perf_counter_task_tick(struct task_struct
*curr
, int cpu
)
1195 struct perf_cpu_context
*cpuctx
;
1196 struct perf_counter_context
*ctx
;
1198 if (!atomic_read(&nr_counters
))
1201 cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
1202 ctx
= curr
->perf_counter_ctxp
;
1204 perf_adjust_freq(&cpuctx
->ctx
);
1206 perf_adjust_freq(ctx
);
1208 perf_counter_cpu_sched_out(cpuctx
);
1210 __perf_counter_task_sched_out(ctx
);
1212 rotate_ctx(&cpuctx
->ctx
);
1216 perf_counter_cpu_sched_in(cpuctx
, cpu
);
1218 perf_counter_task_sched_in(curr
, cpu
);
1222 * Cross CPU call to read the hardware counter
1224 static void __read(void *info
)
1226 struct perf_counter
*counter
= info
;
1227 struct perf_counter_context
*ctx
= counter
->ctx
;
1228 unsigned long flags
;
1230 local_irq_save(flags
);
1232 update_context_time(ctx
);
1233 counter
->pmu
->read(counter
);
1234 update_counter_times(counter
);
1235 local_irq_restore(flags
);
1238 static u64
perf_counter_read(struct perf_counter
*counter
)
1241 * If counter is enabled and currently active on a CPU, update the
1242 * value in the counter structure:
1244 if (counter
->state
== PERF_COUNTER_STATE_ACTIVE
) {
1245 smp_call_function_single(counter
->oncpu
,
1246 __read
, counter
, 1);
1247 } else if (counter
->state
== PERF_COUNTER_STATE_INACTIVE
) {
1248 update_counter_times(counter
);
1251 return atomic64_read(&counter
->count
);
1255 * Initialize the perf_counter context in a task_struct:
1258 __perf_counter_init_context(struct perf_counter_context
*ctx
,
1259 struct task_struct
*task
)
1261 memset(ctx
, 0, sizeof(*ctx
));
1262 spin_lock_init(&ctx
->lock
);
1263 mutex_init(&ctx
->mutex
);
1264 INIT_LIST_HEAD(&ctx
->counter_list
);
1265 INIT_LIST_HEAD(&ctx
->event_list
);
1266 atomic_set(&ctx
->refcount
, 1);
1270 static struct perf_counter_context
*find_get_context(pid_t pid
, int cpu
)
1272 struct perf_cpu_context
*cpuctx
;
1273 struct perf_counter_context
*ctx
;
1274 struct perf_counter_context
*parent_ctx
;
1275 struct task_struct
*task
;
1279 * If cpu is not a wildcard then this is a percpu counter:
1282 /* Must be root to operate on a CPU counter: */
1283 if (sysctl_perf_counter_priv
&& !capable(CAP_SYS_ADMIN
))
1284 return ERR_PTR(-EACCES
);
1286 if (cpu
< 0 || cpu
> num_possible_cpus())
1287 return ERR_PTR(-EINVAL
);
1290 * We could be clever and allow to attach a counter to an
1291 * offline CPU and activate it when the CPU comes up, but
1294 if (!cpu_isset(cpu
, cpu_online_map
))
1295 return ERR_PTR(-ENODEV
);
1297 cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
1308 task
= find_task_by_vpid(pid
);
1310 get_task_struct(task
);
1314 return ERR_PTR(-ESRCH
);
1317 * Can't attach counters to a dying task.
1320 if (task
->flags
& PF_EXITING
)
1323 /* Reuse ptrace permission checks for now. */
1325 if (!ptrace_may_access(task
, PTRACE_MODE_READ
))
1331 ctx
= rcu_dereference(task
->perf_counter_ctxp
);
1334 * If this context is a clone of another, it might
1335 * get swapped for another underneath us by
1336 * perf_counter_task_sched_out, though the
1337 * rcu_read_lock() protects us from any context
1338 * getting freed. Lock the context and check if it
1339 * got swapped before we could get the lock, and retry
1340 * if so. If we locked the right context, then it
1341 * can't get swapped on us any more and we can
1342 * unclone it if necessary.
1343 * Once it's not a clone things will be stable.
1345 spin_lock_irq(&ctx
->lock
);
1346 if (ctx
!= rcu_dereference(task
->perf_counter_ctxp
)) {
1347 spin_unlock_irq(&ctx
->lock
);
1350 parent_ctx
= ctx
->parent_ctx
;
1352 put_ctx(parent_ctx
);
1353 ctx
->parent_ctx
= NULL
; /* no longer a clone */
1356 * Get an extra reference before dropping the lock so that
1357 * this context won't get freed if the task exits.
1360 spin_unlock_irq(&ctx
->lock
);
1365 ctx
= kmalloc(sizeof(struct perf_counter_context
), GFP_KERNEL
);
1369 __perf_counter_init_context(ctx
, task
);
1371 if (cmpxchg(&task
->perf_counter_ctxp
, NULL
, ctx
)) {
1373 * We raced with some other task; use
1374 * the context they set.
1379 get_task_struct(task
);
1382 put_task_struct(task
);
1386 put_task_struct(task
);
1387 return ERR_PTR(err
);
1390 static void free_counter_rcu(struct rcu_head
*head
)
1392 struct perf_counter
*counter
;
1394 counter
= container_of(head
, struct perf_counter
, rcu_head
);
1398 static void perf_pending_sync(struct perf_counter
*counter
);
1400 static void free_counter(struct perf_counter
*counter
)
1402 perf_pending_sync(counter
);
1404 atomic_dec(&nr_counters
);
1405 if (counter
->hw_event
.mmap
)
1406 atomic_dec(&nr_mmap_tracking
);
1407 if (counter
->hw_event
.munmap
)
1408 atomic_dec(&nr_munmap_tracking
);
1409 if (counter
->hw_event
.comm
)
1410 atomic_dec(&nr_comm_tracking
);
1412 if (counter
->destroy
)
1413 counter
->destroy(counter
);
1415 put_ctx(counter
->ctx
);
1416 call_rcu(&counter
->rcu_head
, free_counter_rcu
);
1420 * Called when the last reference to the file is gone.
1422 static int perf_release(struct inode
*inode
, struct file
*file
)
1424 struct perf_counter
*counter
= file
->private_data
;
1425 struct perf_counter_context
*ctx
= counter
->ctx
;
1427 file
->private_data
= NULL
;
1429 WARN_ON_ONCE(ctx
->parent_ctx
);
1430 mutex_lock(&ctx
->mutex
);
1431 perf_counter_remove_from_context(counter
);
1432 mutex_unlock(&ctx
->mutex
);
1434 mutex_lock(&counter
->owner
->perf_counter_mutex
);
1435 list_del_init(&counter
->owner_entry
);
1436 mutex_unlock(&counter
->owner
->perf_counter_mutex
);
1437 put_task_struct(counter
->owner
);
1439 free_counter(counter
);
1445 * Read the performance counter - simple non blocking version for now
1448 perf_read_hw(struct perf_counter
*counter
, char __user
*buf
, size_t count
)
1454 * Return end-of-file for a read on a counter that is in
1455 * error state (i.e. because it was pinned but it couldn't be
1456 * scheduled on to the CPU at some point).
1458 if (counter
->state
== PERF_COUNTER_STATE_ERROR
)
1461 WARN_ON_ONCE(counter
->ctx
->parent_ctx
);
1462 mutex_lock(&counter
->child_mutex
);
1463 values
[0] = perf_counter_read(counter
);
1465 if (counter
->hw_event
.read_format
& PERF_FORMAT_TOTAL_TIME_ENABLED
)
1466 values
[n
++] = counter
->total_time_enabled
+
1467 atomic64_read(&counter
->child_total_time_enabled
);
1468 if (counter
->hw_event
.read_format
& PERF_FORMAT_TOTAL_TIME_RUNNING
)
1469 values
[n
++] = counter
->total_time_running
+
1470 atomic64_read(&counter
->child_total_time_running
);
1471 mutex_unlock(&counter
->child_mutex
);
1473 if (count
< n
* sizeof(u64
))
1475 count
= n
* sizeof(u64
);
1477 if (copy_to_user(buf
, values
, count
))
1484 perf_read(struct file
*file
, char __user
*buf
, size_t count
, loff_t
*ppos
)
1486 struct perf_counter
*counter
= file
->private_data
;
1488 return perf_read_hw(counter
, buf
, count
);
1491 static unsigned int perf_poll(struct file
*file
, poll_table
*wait
)
1493 struct perf_counter
*counter
= file
->private_data
;
1494 struct perf_mmap_data
*data
;
1495 unsigned int events
= POLL_HUP
;
1498 data
= rcu_dereference(counter
->data
);
1500 events
= atomic_xchg(&data
->poll
, 0);
1503 poll_wait(file
, &counter
->waitq
, wait
);
1508 static void perf_counter_reset(struct perf_counter
*counter
)
1510 (void)perf_counter_read(counter
);
1511 atomic64_set(&counter
->count
, 0);
1512 perf_counter_update_userpage(counter
);
1515 static void perf_counter_for_each_sibling(struct perf_counter
*counter
,
1516 void (*func
)(struct perf_counter
*))
1518 struct perf_counter_context
*ctx
= counter
->ctx
;
1519 struct perf_counter
*sibling
;
1521 WARN_ON_ONCE(ctx
->parent_ctx
);
1522 mutex_lock(&ctx
->mutex
);
1523 counter
= counter
->group_leader
;
1526 list_for_each_entry(sibling
, &counter
->sibling_list
, list_entry
)
1528 mutex_unlock(&ctx
->mutex
);
1532 * Holding the top-level counter's child_mutex means that any
1533 * descendant process that has inherited this counter will block
1534 * in sync_child_counter if it goes to exit, thus satisfying the
1535 * task existence requirements of perf_counter_enable/disable.
1537 static void perf_counter_for_each_child(struct perf_counter
*counter
,
1538 void (*func
)(struct perf_counter
*))
1540 struct perf_counter
*child
;
1542 WARN_ON_ONCE(counter
->ctx
->parent_ctx
);
1543 mutex_lock(&counter
->child_mutex
);
1545 list_for_each_entry(child
, &counter
->child_list
, child_list
)
1547 mutex_unlock(&counter
->child_mutex
);
1550 static void perf_counter_for_each(struct perf_counter
*counter
,
1551 void (*func
)(struct perf_counter
*))
1553 struct perf_counter
*child
;
1555 WARN_ON_ONCE(counter
->ctx
->parent_ctx
);
1556 mutex_lock(&counter
->child_mutex
);
1557 perf_counter_for_each_sibling(counter
, func
);
1558 list_for_each_entry(child
, &counter
->child_list
, child_list
)
1559 perf_counter_for_each_sibling(child
, func
);
1560 mutex_unlock(&counter
->child_mutex
);
1563 static long perf_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
1565 struct perf_counter
*counter
= file
->private_data
;
1566 void (*func
)(struct perf_counter
*);
1570 case PERF_COUNTER_IOC_ENABLE
:
1571 func
= perf_counter_enable
;
1573 case PERF_COUNTER_IOC_DISABLE
:
1574 func
= perf_counter_disable
;
1576 case PERF_COUNTER_IOC_RESET
:
1577 func
= perf_counter_reset
;
1580 case PERF_COUNTER_IOC_REFRESH
:
1581 return perf_counter_refresh(counter
, arg
);
1586 if (flags
& PERF_IOC_FLAG_GROUP
)
1587 perf_counter_for_each(counter
, func
);
1589 perf_counter_for_each_child(counter
, func
);
1594 int perf_counter_task_enable(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_enable
);
1601 mutex_unlock(¤t
->perf_counter_mutex
);
1606 int perf_counter_task_disable(void)
1608 struct perf_counter
*counter
;
1610 mutex_lock(¤t
->perf_counter_mutex
);
1611 list_for_each_entry(counter
, ¤t
->perf_counter_list
, owner_entry
)
1612 perf_counter_for_each_child(counter
, perf_counter_disable
);
1613 mutex_unlock(¤t
->perf_counter_mutex
);
1619 * Callers need to ensure there can be no nesting of this function, otherwise
1620 * the seqlock logic goes bad. We can not serialize this because the arch
1621 * code calls this from NMI context.
1623 void perf_counter_update_userpage(struct perf_counter
*counter
)
1625 struct perf_mmap_data
*data
;
1626 struct perf_counter_mmap_page
*userpg
;
1629 data
= rcu_dereference(counter
->data
);
1633 userpg
= data
->user_page
;
1636 * Disable preemption so as to not let the corresponding user-space
1637 * spin too long if we get preempted.
1642 userpg
->index
= counter
->hw
.idx
;
1643 userpg
->offset
= atomic64_read(&counter
->count
);
1644 if (counter
->state
== PERF_COUNTER_STATE_ACTIVE
)
1645 userpg
->offset
-= atomic64_read(&counter
->hw
.prev_count
);
1654 static int perf_mmap_fault(struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
1656 struct perf_counter
*counter
= vma
->vm_file
->private_data
;
1657 struct perf_mmap_data
*data
;
1658 int ret
= VM_FAULT_SIGBUS
;
1661 data
= rcu_dereference(counter
->data
);
1665 if (vmf
->pgoff
== 0) {
1666 vmf
->page
= virt_to_page(data
->user_page
);
1668 int nr
= vmf
->pgoff
- 1;
1670 if ((unsigned)nr
> data
->nr_pages
)
1673 vmf
->page
= virt_to_page(data
->data_pages
[nr
]);
1675 get_page(vmf
->page
);
1683 static int perf_mmap_data_alloc(struct perf_counter
*counter
, int nr_pages
)
1685 struct perf_mmap_data
*data
;
1689 WARN_ON(atomic_read(&counter
->mmap_count
));
1691 size
= sizeof(struct perf_mmap_data
);
1692 size
+= nr_pages
* sizeof(void *);
1694 data
= kzalloc(size
, GFP_KERNEL
);
1698 data
->user_page
= (void *)get_zeroed_page(GFP_KERNEL
);
1699 if (!data
->user_page
)
1700 goto fail_user_page
;
1702 for (i
= 0; i
< nr_pages
; i
++) {
1703 data
->data_pages
[i
] = (void *)get_zeroed_page(GFP_KERNEL
);
1704 if (!data
->data_pages
[i
])
1705 goto fail_data_pages
;
1708 data
->nr_pages
= nr_pages
;
1709 atomic_set(&data
->lock
, -1);
1711 rcu_assign_pointer(counter
->data
, data
);
1716 for (i
--; i
>= 0; i
--)
1717 free_page((unsigned long)data
->data_pages
[i
]);
1719 free_page((unsigned long)data
->user_page
);
1728 static void __perf_mmap_data_free(struct rcu_head
*rcu_head
)
1730 struct perf_mmap_data
*data
= container_of(rcu_head
,
1731 struct perf_mmap_data
, rcu_head
);
1734 free_page((unsigned long)data
->user_page
);
1735 for (i
= 0; i
< data
->nr_pages
; i
++)
1736 free_page((unsigned long)data
->data_pages
[i
]);
1740 static void perf_mmap_data_free(struct perf_counter
*counter
)
1742 struct perf_mmap_data
*data
= counter
->data
;
1744 WARN_ON(atomic_read(&counter
->mmap_count
));
1746 rcu_assign_pointer(counter
->data
, NULL
);
1747 call_rcu(&data
->rcu_head
, __perf_mmap_data_free
);
1750 static void perf_mmap_open(struct vm_area_struct
*vma
)
1752 struct perf_counter
*counter
= vma
->vm_file
->private_data
;
1754 atomic_inc(&counter
->mmap_count
);
1757 static void perf_mmap_close(struct vm_area_struct
*vma
)
1759 struct perf_counter
*counter
= vma
->vm_file
->private_data
;
1761 WARN_ON_ONCE(counter
->ctx
->parent_ctx
);
1762 if (atomic_dec_and_mutex_lock(&counter
->mmap_count
,
1763 &counter
->mmap_mutex
)) {
1764 struct user_struct
*user
= current_user();
1766 atomic_long_sub(counter
->data
->nr_pages
+ 1, &user
->locked_vm
);
1767 vma
->vm_mm
->locked_vm
-= counter
->data
->nr_locked
;
1768 perf_mmap_data_free(counter
);
1769 mutex_unlock(&counter
->mmap_mutex
);
1773 static struct vm_operations_struct perf_mmap_vmops
= {
1774 .open
= perf_mmap_open
,
1775 .close
= perf_mmap_close
,
1776 .fault
= perf_mmap_fault
,
1779 static int perf_mmap(struct file
*file
, struct vm_area_struct
*vma
)
1781 struct perf_counter
*counter
= file
->private_data
;
1782 struct user_struct
*user
= current_user();
1783 unsigned long vma_size
;
1784 unsigned long nr_pages
;
1785 unsigned long user_locked
, user_lock_limit
;
1786 unsigned long locked
, lock_limit
;
1787 long user_extra
, extra
;
1790 if (!(vma
->vm_flags
& VM_SHARED
) || (vma
->vm_flags
& VM_WRITE
))
1793 vma_size
= vma
->vm_end
- vma
->vm_start
;
1794 nr_pages
= (vma_size
/ PAGE_SIZE
) - 1;
1797 * If we have data pages ensure they're a power-of-two number, so we
1798 * can do bitmasks instead of modulo.
1800 if (nr_pages
!= 0 && !is_power_of_2(nr_pages
))
1803 if (vma_size
!= PAGE_SIZE
* (1 + nr_pages
))
1806 if (vma
->vm_pgoff
!= 0)
1809 WARN_ON_ONCE(counter
->ctx
->parent_ctx
);
1810 mutex_lock(&counter
->mmap_mutex
);
1811 if (atomic_inc_not_zero(&counter
->mmap_count
)) {
1812 if (nr_pages
!= counter
->data
->nr_pages
)
1817 user_extra
= nr_pages
+ 1;
1818 user_lock_limit
= sysctl_perf_counter_mlock
>> (PAGE_SHIFT
- 10);
1821 * Increase the limit linearly with more CPUs:
1823 user_lock_limit
*= num_online_cpus();
1825 user_locked
= atomic_long_read(&user
->locked_vm
) + user_extra
;
1828 if (user_locked
> user_lock_limit
)
1829 extra
= user_locked
- user_lock_limit
;
1831 lock_limit
= current
->signal
->rlim
[RLIMIT_MEMLOCK
].rlim_cur
;
1832 lock_limit
>>= PAGE_SHIFT
;
1833 locked
= vma
->vm_mm
->locked_vm
+ extra
;
1835 if ((locked
> lock_limit
) && !capable(CAP_IPC_LOCK
)) {
1840 WARN_ON(counter
->data
);
1841 ret
= perf_mmap_data_alloc(counter
, nr_pages
);
1845 atomic_set(&counter
->mmap_count
, 1);
1846 atomic_long_add(user_extra
, &user
->locked_vm
);
1847 vma
->vm_mm
->locked_vm
+= extra
;
1848 counter
->data
->nr_locked
= extra
;
1850 mutex_unlock(&counter
->mmap_mutex
);
1852 vma
->vm_flags
&= ~VM_MAYWRITE
;
1853 vma
->vm_flags
|= VM_RESERVED
;
1854 vma
->vm_ops
= &perf_mmap_vmops
;
1859 static int perf_fasync(int fd
, struct file
*filp
, int on
)
1861 struct perf_counter
*counter
= filp
->private_data
;
1862 struct inode
*inode
= filp
->f_path
.dentry
->d_inode
;
1865 mutex_lock(&inode
->i_mutex
);
1866 retval
= fasync_helper(fd
, filp
, on
, &counter
->fasync
);
1867 mutex_unlock(&inode
->i_mutex
);
1875 static const struct file_operations perf_fops
= {
1876 .release
= perf_release
,
1879 .unlocked_ioctl
= perf_ioctl
,
1880 .compat_ioctl
= perf_ioctl
,
1882 .fasync
= perf_fasync
,
1886 * Perf counter wakeup
1888 * If there's data, ensure we set the poll() state and publish everything
1889 * to user-space before waking everybody up.
1892 void perf_counter_wakeup(struct perf_counter
*counter
)
1894 wake_up_all(&counter
->waitq
);
1896 if (counter
->pending_kill
) {
1897 kill_fasync(&counter
->fasync
, SIGIO
, counter
->pending_kill
);
1898 counter
->pending_kill
= 0;
1905 * Handle the case where we need to wakeup up from NMI (or rq->lock) context.
1907 * The NMI bit means we cannot possibly take locks. Therefore, maintain a
1908 * single linked list and use cmpxchg() to add entries lockless.
1911 static void perf_pending_counter(struct perf_pending_entry
*entry
)
1913 struct perf_counter
*counter
= container_of(entry
,
1914 struct perf_counter
, pending
);
1916 if (counter
->pending_disable
) {
1917 counter
->pending_disable
= 0;
1918 perf_counter_disable(counter
);
1921 if (counter
->pending_wakeup
) {
1922 counter
->pending_wakeup
= 0;
1923 perf_counter_wakeup(counter
);
1927 #define PENDING_TAIL ((struct perf_pending_entry *)-1UL)
1929 static DEFINE_PER_CPU(struct perf_pending_entry
*, perf_pending_head
) = {
1933 static void perf_pending_queue(struct perf_pending_entry
*entry
,
1934 void (*func
)(struct perf_pending_entry
*))
1936 struct perf_pending_entry
**head
;
1938 if (cmpxchg(&entry
->next
, NULL
, PENDING_TAIL
) != NULL
)
1943 head
= &get_cpu_var(perf_pending_head
);
1946 entry
->next
= *head
;
1947 } while (cmpxchg(head
, entry
->next
, entry
) != entry
->next
);
1949 set_perf_counter_pending();
1951 put_cpu_var(perf_pending_head
);
1954 static int __perf_pending_run(void)
1956 struct perf_pending_entry
*list
;
1959 list
= xchg(&__get_cpu_var(perf_pending_head
), PENDING_TAIL
);
1960 while (list
!= PENDING_TAIL
) {
1961 void (*func
)(struct perf_pending_entry
*);
1962 struct perf_pending_entry
*entry
= list
;
1969 * Ensure we observe the unqueue before we issue the wakeup,
1970 * so that we won't be waiting forever.
1971 * -- see perf_not_pending().
1982 static inline int perf_not_pending(struct perf_counter
*counter
)
1985 * If we flush on whatever cpu we run, there is a chance we don't
1989 __perf_pending_run();
1993 * Ensure we see the proper queue state before going to sleep
1994 * so that we do not miss the wakeup. -- see perf_pending_handle()
1997 return counter
->pending
.next
== NULL
;
2000 static void perf_pending_sync(struct perf_counter
*counter
)
2002 wait_event(counter
->waitq
, perf_not_pending(counter
));
2005 void perf_counter_do_pending(void)
2007 __perf_pending_run();
2011 * Callchain support -- arch specific
2014 __weak
struct perf_callchain_entry
*perf_callchain(struct pt_regs
*regs
)
2023 struct perf_output_handle
{
2024 struct perf_counter
*counter
;
2025 struct perf_mmap_data
*data
;
2026 unsigned int offset
;
2031 unsigned long flags
;
2034 static void perf_output_wakeup(struct perf_output_handle
*handle
)
2036 atomic_set(&handle
->data
->poll
, POLL_IN
);
2039 handle
->counter
->pending_wakeup
= 1;
2040 perf_pending_queue(&handle
->counter
->pending
,
2041 perf_pending_counter
);
2043 perf_counter_wakeup(handle
->counter
);
2047 * Curious locking construct.
2049 * We need to ensure a later event doesn't publish a head when a former
2050 * event isn't done writing. However since we need to deal with NMIs we
2051 * cannot fully serialize things.
2053 * What we do is serialize between CPUs so we only have to deal with NMI
2054 * nesting on a single CPU.
2056 * We only publish the head (and generate a wakeup) when the outer-most
2059 static void perf_output_lock(struct perf_output_handle
*handle
)
2061 struct perf_mmap_data
*data
= handle
->data
;
2066 local_irq_save(handle
->flags
);
2067 cpu
= smp_processor_id();
2069 if (in_nmi() && atomic_read(&data
->lock
) == cpu
)
2072 while (atomic_cmpxchg(&data
->lock
, -1, cpu
) != -1)
2078 static void perf_output_unlock(struct perf_output_handle
*handle
)
2080 struct perf_mmap_data
*data
= handle
->data
;
2083 data
->done_head
= data
->head
;
2085 if (!handle
->locked
)
2090 * The xchg implies a full barrier that ensures all writes are done
2091 * before we publish the new head, matched by a rmb() in userspace when
2092 * reading this position.
2094 while ((head
= atomic_xchg(&data
->done_head
, 0)))
2095 data
->user_page
->data_head
= head
;
2098 * NMI can happen here, which means we can miss a done_head update.
2101 cpu
= atomic_xchg(&data
->lock
, -1);
2102 WARN_ON_ONCE(cpu
!= smp_processor_id());
2105 * Therefore we have to validate we did not indeed do so.
2107 if (unlikely(atomic_read(&data
->done_head
))) {
2109 * Since we had it locked, we can lock it again.
2111 while (atomic_cmpxchg(&data
->lock
, -1, cpu
) != -1)
2117 if (atomic_xchg(&data
->wakeup
, 0))
2118 perf_output_wakeup(handle
);
2120 local_irq_restore(handle
->flags
);
2123 static int perf_output_begin(struct perf_output_handle
*handle
,
2124 struct perf_counter
*counter
, unsigned int size
,
2125 int nmi
, int overflow
)
2127 struct perf_mmap_data
*data
;
2128 unsigned int offset
, head
;
2131 * For inherited counters we send all the output towards the parent.
2133 if (counter
->parent
)
2134 counter
= counter
->parent
;
2137 data
= rcu_dereference(counter
->data
);
2141 handle
->data
= data
;
2142 handle
->counter
= counter
;
2144 handle
->overflow
= overflow
;
2146 if (!data
->nr_pages
)
2149 perf_output_lock(handle
);
2152 offset
= head
= atomic_read(&data
->head
);
2154 } while (atomic_cmpxchg(&data
->head
, offset
, head
) != offset
);
2156 handle
->offset
= offset
;
2157 handle
->head
= head
;
2159 if ((offset
>> PAGE_SHIFT
) != (head
>> PAGE_SHIFT
))
2160 atomic_set(&data
->wakeup
, 1);
2165 perf_output_wakeup(handle
);
2172 static void perf_output_copy(struct perf_output_handle
*handle
,
2173 void *buf
, unsigned int len
)
2175 unsigned int pages_mask
;
2176 unsigned int offset
;
2180 offset
= handle
->offset
;
2181 pages_mask
= handle
->data
->nr_pages
- 1;
2182 pages
= handle
->data
->data_pages
;
2185 unsigned int page_offset
;
2188 nr
= (offset
>> PAGE_SHIFT
) & pages_mask
;
2189 page_offset
= offset
& (PAGE_SIZE
- 1);
2190 size
= min_t(unsigned int, PAGE_SIZE
- page_offset
, len
);
2192 memcpy(pages
[nr
] + page_offset
, buf
, size
);
2199 handle
->offset
= offset
;
2202 * Check we didn't copy past our reservation window, taking the
2203 * possible unsigned int wrap into account.
2205 WARN_ON_ONCE(((int)(handle
->head
- handle
->offset
)) < 0);
2208 #define perf_output_put(handle, x) \
2209 perf_output_copy((handle), &(x), sizeof(x))
2211 static void perf_output_end(struct perf_output_handle
*handle
)
2213 struct perf_counter
*counter
= handle
->counter
;
2214 struct perf_mmap_data
*data
= handle
->data
;
2216 int wakeup_events
= counter
->hw_event
.wakeup_events
;
2218 if (handle
->overflow
&& wakeup_events
) {
2219 int events
= atomic_inc_return(&data
->events
);
2220 if (events
>= wakeup_events
) {
2221 atomic_sub(wakeup_events
, &data
->events
);
2222 atomic_set(&data
->wakeup
, 1);
2226 perf_output_unlock(handle
);
2230 static void perf_counter_output(struct perf_counter
*counter
,
2231 int nmi
, struct pt_regs
*regs
, u64 addr
)
2234 u64 record_type
= counter
->hw_event
.record_type
;
2235 struct perf_output_handle handle
;
2236 struct perf_event_header header
;
2245 struct perf_callchain_entry
*callchain
= NULL
;
2246 int callchain_size
= 0;
2253 header
.size
= sizeof(header
);
2255 header
.misc
= PERF_EVENT_MISC_OVERFLOW
;
2256 header
.misc
|= perf_misc_flags(regs
);
2258 if (record_type
& PERF_RECORD_IP
) {
2259 ip
= perf_instruction_pointer(regs
);
2260 header
.type
|= PERF_RECORD_IP
;
2261 header
.size
+= sizeof(ip
);
2264 if (record_type
& PERF_RECORD_TID
) {
2265 /* namespace issues */
2266 tid_entry
.pid
= current
->group_leader
->pid
;
2267 tid_entry
.tid
= current
->pid
;
2269 header
.type
|= PERF_RECORD_TID
;
2270 header
.size
+= sizeof(tid_entry
);
2273 if (record_type
& PERF_RECORD_TIME
) {
2275 * Maybe do better on x86 and provide cpu_clock_nmi()
2277 time
= sched_clock();
2279 header
.type
|= PERF_RECORD_TIME
;
2280 header
.size
+= sizeof(u64
);
2283 if (record_type
& PERF_RECORD_ADDR
) {
2284 header
.type
|= PERF_RECORD_ADDR
;
2285 header
.size
+= sizeof(u64
);
2288 if (record_type
& PERF_RECORD_CONFIG
) {
2289 header
.type
|= PERF_RECORD_CONFIG
;
2290 header
.size
+= sizeof(u64
);
2293 if (record_type
& PERF_RECORD_CPU
) {
2294 header
.type
|= PERF_RECORD_CPU
;
2295 header
.size
+= sizeof(cpu_entry
);
2297 cpu_entry
.cpu
= raw_smp_processor_id();
2300 if (record_type
& PERF_RECORD_GROUP
) {
2301 header
.type
|= PERF_RECORD_GROUP
;
2302 header
.size
+= sizeof(u64
) +
2303 counter
->nr_siblings
* sizeof(group_entry
);
2306 if (record_type
& PERF_RECORD_CALLCHAIN
) {
2307 callchain
= perf_callchain(regs
);
2310 callchain_size
= (1 + callchain
->nr
) * sizeof(u64
);
2312 header
.type
|= PERF_RECORD_CALLCHAIN
;
2313 header
.size
+= callchain_size
;
2317 ret
= perf_output_begin(&handle
, counter
, header
.size
, nmi
, 1);
2321 perf_output_put(&handle
, header
);
2323 if (record_type
& PERF_RECORD_IP
)
2324 perf_output_put(&handle
, ip
);
2326 if (record_type
& PERF_RECORD_TID
)
2327 perf_output_put(&handle
, tid_entry
);
2329 if (record_type
& PERF_RECORD_TIME
)
2330 perf_output_put(&handle
, time
);
2332 if (record_type
& PERF_RECORD_ADDR
)
2333 perf_output_put(&handle
, addr
);
2335 if (record_type
& PERF_RECORD_CONFIG
)
2336 perf_output_put(&handle
, counter
->hw_event
.config
);
2338 if (record_type
& PERF_RECORD_CPU
)
2339 perf_output_put(&handle
, cpu_entry
);
2342 * XXX PERF_RECORD_GROUP vs inherited counters seems difficult.
2344 if (record_type
& PERF_RECORD_GROUP
) {
2345 struct perf_counter
*leader
, *sub
;
2346 u64 nr
= counter
->nr_siblings
;
2348 perf_output_put(&handle
, nr
);
2350 leader
= counter
->group_leader
;
2351 list_for_each_entry(sub
, &leader
->sibling_list
, list_entry
) {
2353 sub
->pmu
->read(sub
);
2355 group_entry
.event
= sub
->hw_event
.config
;
2356 group_entry
.counter
= atomic64_read(&sub
->count
);
2358 perf_output_put(&handle
, group_entry
);
2363 perf_output_copy(&handle
, callchain
, callchain_size
);
2365 perf_output_end(&handle
);
2372 struct perf_comm_event
{
2373 struct task_struct
*task
;
2378 struct perf_event_header header
;
2385 static void perf_counter_comm_output(struct perf_counter
*counter
,
2386 struct perf_comm_event
*comm_event
)
2388 struct perf_output_handle handle
;
2389 int size
= comm_event
->event
.header
.size
;
2390 int ret
= perf_output_begin(&handle
, counter
, size
, 0, 0);
2395 perf_output_put(&handle
, comm_event
->event
);
2396 perf_output_copy(&handle
, comm_event
->comm
,
2397 comm_event
->comm_size
);
2398 perf_output_end(&handle
);
2401 static int perf_counter_comm_match(struct perf_counter
*counter
,
2402 struct perf_comm_event
*comm_event
)
2404 if (counter
->hw_event
.comm
&&
2405 comm_event
->event
.header
.type
== PERF_EVENT_COMM
)
2411 static void perf_counter_comm_ctx(struct perf_counter_context
*ctx
,
2412 struct perf_comm_event
*comm_event
)
2414 struct perf_counter
*counter
;
2416 if (system_state
!= SYSTEM_RUNNING
|| list_empty(&ctx
->event_list
))
2420 list_for_each_entry_rcu(counter
, &ctx
->event_list
, event_entry
) {
2421 if (perf_counter_comm_match(counter
, comm_event
))
2422 perf_counter_comm_output(counter
, comm_event
);
2427 static void perf_counter_comm_event(struct perf_comm_event
*comm_event
)
2429 struct perf_cpu_context
*cpuctx
;
2431 char *comm
= comm_event
->task
->comm
;
2433 size
= ALIGN(strlen(comm
)+1, sizeof(u64
));
2435 comm_event
->comm
= comm
;
2436 comm_event
->comm_size
= size
;
2438 comm_event
->event
.header
.size
= sizeof(comm_event
->event
) + size
;
2440 cpuctx
= &get_cpu_var(perf_cpu_context
);
2441 perf_counter_comm_ctx(&cpuctx
->ctx
, comm_event
);
2442 put_cpu_var(perf_cpu_context
);
2444 perf_counter_comm_ctx(current
->perf_counter_ctxp
, comm_event
);
2447 void perf_counter_comm(struct task_struct
*task
)
2449 struct perf_comm_event comm_event
;
2451 if (!atomic_read(&nr_comm_tracking
))
2453 if (!current
->perf_counter_ctxp
)
2456 comm_event
= (struct perf_comm_event
){
2459 .header
= { .type
= PERF_EVENT_COMM
, },
2460 .pid
= task
->group_leader
->pid
,
2465 perf_counter_comm_event(&comm_event
);
2472 struct perf_mmap_event
{
2478 struct perf_event_header header
;
2488 static void perf_counter_mmap_output(struct perf_counter
*counter
,
2489 struct perf_mmap_event
*mmap_event
)
2491 struct perf_output_handle handle
;
2492 int size
= mmap_event
->event
.header
.size
;
2493 int ret
= perf_output_begin(&handle
, counter
, size
, 0, 0);
2498 perf_output_put(&handle
, mmap_event
->event
);
2499 perf_output_copy(&handle
, mmap_event
->file_name
,
2500 mmap_event
->file_size
);
2501 perf_output_end(&handle
);
2504 static int perf_counter_mmap_match(struct perf_counter
*counter
,
2505 struct perf_mmap_event
*mmap_event
)
2507 if (counter
->hw_event
.mmap
&&
2508 mmap_event
->event
.header
.type
== PERF_EVENT_MMAP
)
2511 if (counter
->hw_event
.munmap
&&
2512 mmap_event
->event
.header
.type
== PERF_EVENT_MUNMAP
)
2518 static void perf_counter_mmap_ctx(struct perf_counter_context
*ctx
,
2519 struct perf_mmap_event
*mmap_event
)
2521 struct perf_counter
*counter
;
2523 if (system_state
!= SYSTEM_RUNNING
|| list_empty(&ctx
->event_list
))
2527 list_for_each_entry_rcu(counter
, &ctx
->event_list
, event_entry
) {
2528 if (perf_counter_mmap_match(counter
, mmap_event
))
2529 perf_counter_mmap_output(counter
, mmap_event
);
2534 static void perf_counter_mmap_event(struct perf_mmap_event
*mmap_event
)
2536 struct perf_cpu_context
*cpuctx
;
2537 struct file
*file
= mmap_event
->file
;
2544 buf
= kzalloc(PATH_MAX
, GFP_KERNEL
);
2546 name
= strncpy(tmp
, "//enomem", sizeof(tmp
));
2549 name
= d_path(&file
->f_path
, buf
, PATH_MAX
);
2551 name
= strncpy(tmp
, "//toolong", sizeof(tmp
));
2555 name
= strncpy(tmp
, "//anon", sizeof(tmp
));
2560 size
= ALIGN(strlen(name
)+1, sizeof(u64
));
2562 mmap_event
->file_name
= name
;
2563 mmap_event
->file_size
= size
;
2565 mmap_event
->event
.header
.size
= sizeof(mmap_event
->event
) + size
;
2567 cpuctx
= &get_cpu_var(perf_cpu_context
);
2568 perf_counter_mmap_ctx(&cpuctx
->ctx
, mmap_event
);
2569 put_cpu_var(perf_cpu_context
);
2571 perf_counter_mmap_ctx(current
->perf_counter_ctxp
, mmap_event
);
2576 void perf_counter_mmap(unsigned long addr
, unsigned long len
,
2577 unsigned long pgoff
, struct file
*file
)
2579 struct perf_mmap_event mmap_event
;
2581 if (!atomic_read(&nr_mmap_tracking
))
2583 if (!current
->perf_counter_ctxp
)
2586 mmap_event
= (struct perf_mmap_event
){
2589 .header
= { .type
= PERF_EVENT_MMAP
, },
2590 .pid
= current
->group_leader
->pid
,
2591 .tid
= current
->pid
,
2598 perf_counter_mmap_event(&mmap_event
);
2601 void perf_counter_munmap(unsigned long addr
, unsigned long len
,
2602 unsigned long pgoff
, struct file
*file
)
2604 struct perf_mmap_event mmap_event
;
2606 if (!atomic_read(&nr_munmap_tracking
))
2609 mmap_event
= (struct perf_mmap_event
){
2612 .header
= { .type
= PERF_EVENT_MUNMAP
, },
2613 .pid
= current
->group_leader
->pid
,
2614 .tid
= current
->pid
,
2621 perf_counter_mmap_event(&mmap_event
);
2625 * Log irq_period changes so that analyzing tools can re-normalize the
2629 static void perf_log_period(struct perf_counter
*counter
, u64 period
)
2631 struct perf_output_handle handle
;
2635 struct perf_event_header header
;
2640 .type
= PERF_EVENT_PERIOD
,
2642 .size
= sizeof(freq_event
),
2644 .time
= sched_clock(),
2648 if (counter
->hw
.irq_period
== period
)
2651 ret
= perf_output_begin(&handle
, counter
, sizeof(freq_event
), 0, 0);
2655 perf_output_put(&handle
, freq_event
);
2656 perf_output_end(&handle
);
2660 * IRQ throttle logging
2663 static void perf_log_throttle(struct perf_counter
*counter
, int enable
)
2665 struct perf_output_handle handle
;
2669 struct perf_event_header header
;
2671 } throttle_event
= {
2673 .type
= PERF_EVENT_THROTTLE
+ 1,
2675 .size
= sizeof(throttle_event
),
2677 .time
= sched_clock(),
2680 ret
= perf_output_begin(&handle
, counter
, sizeof(throttle_event
), 1, 0);
2684 perf_output_put(&handle
, throttle_event
);
2685 perf_output_end(&handle
);
2689 * Generic counter overflow handling.
2692 int perf_counter_overflow(struct perf_counter
*counter
,
2693 int nmi
, struct pt_regs
*regs
, u64 addr
)
2695 int events
= atomic_read(&counter
->event_limit
);
2696 int throttle
= counter
->pmu
->unthrottle
!= NULL
;
2700 counter
->hw
.interrupts
++;
2701 } else if (counter
->hw
.interrupts
!= MAX_INTERRUPTS
) {
2702 counter
->hw
.interrupts
++;
2703 if (HZ
*counter
->hw
.interrupts
> (u64
)sysctl_perf_counter_limit
) {
2704 counter
->hw
.interrupts
= MAX_INTERRUPTS
;
2705 perf_log_throttle(counter
, 0);
2711 * XXX event_limit might not quite work as expected on inherited
2715 counter
->pending_kill
= POLL_IN
;
2716 if (events
&& atomic_dec_and_test(&counter
->event_limit
)) {
2718 counter
->pending_kill
= POLL_HUP
;
2720 counter
->pending_disable
= 1;
2721 perf_pending_queue(&counter
->pending
,
2722 perf_pending_counter
);
2724 perf_counter_disable(counter
);
2727 perf_counter_output(counter
, nmi
, regs
, addr
);
2732 * Generic software counter infrastructure
2735 static void perf_swcounter_update(struct perf_counter
*counter
)
2737 struct hw_perf_counter
*hwc
= &counter
->hw
;
2742 prev
= atomic64_read(&hwc
->prev_count
);
2743 now
= atomic64_read(&hwc
->count
);
2744 if (atomic64_cmpxchg(&hwc
->prev_count
, prev
, now
) != prev
)
2749 atomic64_add(delta
, &counter
->count
);
2750 atomic64_sub(delta
, &hwc
->period_left
);
2753 static void perf_swcounter_set_period(struct perf_counter
*counter
)
2755 struct hw_perf_counter
*hwc
= &counter
->hw
;
2756 s64 left
= atomic64_read(&hwc
->period_left
);
2757 s64 period
= hwc
->irq_period
;
2759 if (unlikely(left
<= -period
)) {
2761 atomic64_set(&hwc
->period_left
, left
);
2764 if (unlikely(left
<= 0)) {
2766 atomic64_add(period
, &hwc
->period_left
);
2769 atomic64_set(&hwc
->prev_count
, -left
);
2770 atomic64_set(&hwc
->count
, -left
);
2773 static enum hrtimer_restart
perf_swcounter_hrtimer(struct hrtimer
*hrtimer
)
2775 enum hrtimer_restart ret
= HRTIMER_RESTART
;
2776 struct perf_counter
*counter
;
2777 struct pt_regs
*regs
;
2780 counter
= container_of(hrtimer
, struct perf_counter
, hw
.hrtimer
);
2781 counter
->pmu
->read(counter
);
2783 regs
= get_irq_regs();
2785 * In case we exclude kernel IPs or are somehow not in interrupt
2786 * context, provide the next best thing, the user IP.
2788 if ((counter
->hw_event
.exclude_kernel
|| !regs
) &&
2789 !counter
->hw_event
.exclude_user
)
2790 regs
= task_pt_regs(current
);
2793 if (perf_counter_overflow(counter
, 0, regs
, 0))
2794 ret
= HRTIMER_NORESTART
;
2797 period
= max_t(u64
, 10000, counter
->hw
.irq_period
);
2798 hrtimer_forward_now(hrtimer
, ns_to_ktime(period
));
2803 static void perf_swcounter_overflow(struct perf_counter
*counter
,
2804 int nmi
, struct pt_regs
*regs
, u64 addr
)
2806 perf_swcounter_update(counter
);
2807 perf_swcounter_set_period(counter
);
2808 if (perf_counter_overflow(counter
, nmi
, regs
, addr
))
2809 /* soft-disable the counter */
2814 static int perf_swcounter_match(struct perf_counter
*counter
,
2815 enum perf_event_types type
,
2816 u32 event
, struct pt_regs
*regs
)
2818 if (counter
->state
!= PERF_COUNTER_STATE_ACTIVE
)
2821 if (perf_event_raw(&counter
->hw_event
))
2824 if (perf_event_type(&counter
->hw_event
) != type
)
2827 if (perf_event_id(&counter
->hw_event
) != event
)
2830 if (counter
->hw_event
.exclude_user
&& user_mode(regs
))
2833 if (counter
->hw_event
.exclude_kernel
&& !user_mode(regs
))
2839 static void perf_swcounter_add(struct perf_counter
*counter
, u64 nr
,
2840 int nmi
, struct pt_regs
*regs
, u64 addr
)
2842 int neg
= atomic64_add_negative(nr
, &counter
->hw
.count
);
2843 if (counter
->hw
.irq_period
&& !neg
)
2844 perf_swcounter_overflow(counter
, nmi
, regs
, addr
);
2847 static void perf_swcounter_ctx_event(struct perf_counter_context
*ctx
,
2848 enum perf_event_types type
, u32 event
,
2849 u64 nr
, int nmi
, struct pt_regs
*regs
,
2852 struct perf_counter
*counter
;
2854 if (system_state
!= SYSTEM_RUNNING
|| list_empty(&ctx
->event_list
))
2858 list_for_each_entry_rcu(counter
, &ctx
->event_list
, event_entry
) {
2859 if (perf_swcounter_match(counter
, type
, event
, regs
))
2860 perf_swcounter_add(counter
, nr
, nmi
, regs
, addr
);
2865 static int *perf_swcounter_recursion_context(struct perf_cpu_context
*cpuctx
)
2868 return &cpuctx
->recursion
[3];
2871 return &cpuctx
->recursion
[2];
2874 return &cpuctx
->recursion
[1];
2876 return &cpuctx
->recursion
[0];
2879 static void __perf_swcounter_event(enum perf_event_types type
, u32 event
,
2880 u64 nr
, int nmi
, struct pt_regs
*regs
,
2883 struct perf_cpu_context
*cpuctx
= &get_cpu_var(perf_cpu_context
);
2884 int *recursion
= perf_swcounter_recursion_context(cpuctx
);
2892 perf_swcounter_ctx_event(&cpuctx
->ctx
, type
, event
,
2893 nr
, nmi
, regs
, addr
);
2894 if (cpuctx
->task_ctx
) {
2895 perf_swcounter_ctx_event(cpuctx
->task_ctx
, type
, event
,
2896 nr
, nmi
, regs
, addr
);
2903 put_cpu_var(perf_cpu_context
);
2907 perf_swcounter_event(u32 event
, u64 nr
, int nmi
, struct pt_regs
*regs
, u64 addr
)
2909 __perf_swcounter_event(PERF_TYPE_SOFTWARE
, event
, nr
, nmi
, regs
, addr
);
2912 static void perf_swcounter_read(struct perf_counter
*counter
)
2914 perf_swcounter_update(counter
);
2917 static int perf_swcounter_enable(struct perf_counter
*counter
)
2919 perf_swcounter_set_period(counter
);
2923 static void perf_swcounter_disable(struct perf_counter
*counter
)
2925 perf_swcounter_update(counter
);
2928 static const struct pmu perf_ops_generic
= {
2929 .enable
= perf_swcounter_enable
,
2930 .disable
= perf_swcounter_disable
,
2931 .read
= perf_swcounter_read
,
2935 * Software counter: cpu wall time clock
2938 static void cpu_clock_perf_counter_update(struct perf_counter
*counter
)
2940 int cpu
= raw_smp_processor_id();
2944 now
= cpu_clock(cpu
);
2945 prev
= atomic64_read(&counter
->hw
.prev_count
);
2946 atomic64_set(&counter
->hw
.prev_count
, now
);
2947 atomic64_add(now
- prev
, &counter
->count
);
2950 static int cpu_clock_perf_counter_enable(struct perf_counter
*counter
)
2952 struct hw_perf_counter
*hwc
= &counter
->hw
;
2953 int cpu
= raw_smp_processor_id();
2955 atomic64_set(&hwc
->prev_count
, cpu_clock(cpu
));
2956 hrtimer_init(&hwc
->hrtimer
, CLOCK_MONOTONIC
, HRTIMER_MODE_REL
);
2957 hwc
->hrtimer
.function
= perf_swcounter_hrtimer
;
2958 if (hwc
->irq_period
) {
2959 u64 period
= max_t(u64
, 10000, hwc
->irq_period
);
2960 __hrtimer_start_range_ns(&hwc
->hrtimer
,
2961 ns_to_ktime(period
), 0,
2962 HRTIMER_MODE_REL
, 0);
2968 static void cpu_clock_perf_counter_disable(struct perf_counter
*counter
)
2970 if (counter
->hw
.irq_period
)
2971 hrtimer_cancel(&counter
->hw
.hrtimer
);
2972 cpu_clock_perf_counter_update(counter
);
2975 static void cpu_clock_perf_counter_read(struct perf_counter
*counter
)
2977 cpu_clock_perf_counter_update(counter
);
2980 static const struct pmu perf_ops_cpu_clock
= {
2981 .enable
= cpu_clock_perf_counter_enable
,
2982 .disable
= cpu_clock_perf_counter_disable
,
2983 .read
= cpu_clock_perf_counter_read
,
2987 * Software counter: task time clock
2990 static void task_clock_perf_counter_update(struct perf_counter
*counter
, u64 now
)
2995 prev
= atomic64_xchg(&counter
->hw
.prev_count
, now
);
2997 atomic64_add(delta
, &counter
->count
);
3000 static int task_clock_perf_counter_enable(struct perf_counter
*counter
)
3002 struct hw_perf_counter
*hwc
= &counter
->hw
;
3005 now
= counter
->ctx
->time
;
3007 atomic64_set(&hwc
->prev_count
, now
);
3008 hrtimer_init(&hwc
->hrtimer
, CLOCK_MONOTONIC
, HRTIMER_MODE_REL
);
3009 hwc
->hrtimer
.function
= perf_swcounter_hrtimer
;
3010 if (hwc
->irq_period
) {
3011 u64 period
= max_t(u64
, 10000, hwc
->irq_period
);
3012 __hrtimer_start_range_ns(&hwc
->hrtimer
,
3013 ns_to_ktime(period
), 0,
3014 HRTIMER_MODE_REL
, 0);
3020 static void task_clock_perf_counter_disable(struct perf_counter
*counter
)
3022 if (counter
->hw
.irq_period
)
3023 hrtimer_cancel(&counter
->hw
.hrtimer
);
3024 task_clock_perf_counter_update(counter
, counter
->ctx
->time
);
3028 static void task_clock_perf_counter_read(struct perf_counter
*counter
)
3033 update_context_time(counter
->ctx
);
3034 time
= counter
->ctx
->time
;
3036 u64 now
= perf_clock();
3037 u64 delta
= now
- counter
->ctx
->timestamp
;
3038 time
= counter
->ctx
->time
+ delta
;
3041 task_clock_perf_counter_update(counter
, time
);
3044 static const struct pmu perf_ops_task_clock
= {
3045 .enable
= task_clock_perf_counter_enable
,
3046 .disable
= task_clock_perf_counter_disable
,
3047 .read
= task_clock_perf_counter_read
,
3051 * Software counter: cpu migrations
3054 static inline u64
get_cpu_migrations(struct perf_counter
*counter
)
3056 struct task_struct
*curr
= counter
->ctx
->task
;
3059 return curr
->se
.nr_migrations
;
3060 return cpu_nr_migrations(smp_processor_id());
3063 static void cpu_migrations_perf_counter_update(struct perf_counter
*counter
)
3068 prev
= atomic64_read(&counter
->hw
.prev_count
);
3069 now
= get_cpu_migrations(counter
);
3071 atomic64_set(&counter
->hw
.prev_count
, now
);
3075 atomic64_add(delta
, &counter
->count
);
3078 static void cpu_migrations_perf_counter_read(struct perf_counter
*counter
)
3080 cpu_migrations_perf_counter_update(counter
);
3083 static int cpu_migrations_perf_counter_enable(struct perf_counter
*counter
)
3085 if (counter
->prev_state
<= PERF_COUNTER_STATE_OFF
)
3086 atomic64_set(&counter
->hw
.prev_count
,
3087 get_cpu_migrations(counter
));
3091 static void cpu_migrations_perf_counter_disable(struct perf_counter
*counter
)
3093 cpu_migrations_perf_counter_update(counter
);
3096 static const struct pmu perf_ops_cpu_migrations
= {
3097 .enable
= cpu_migrations_perf_counter_enable
,
3098 .disable
= cpu_migrations_perf_counter_disable
,
3099 .read
= cpu_migrations_perf_counter_read
,
3102 #ifdef CONFIG_EVENT_PROFILE
3103 void perf_tpcounter_event(int event_id
)
3105 struct pt_regs
*regs
= get_irq_regs();
3108 regs
= task_pt_regs(current
);
3110 __perf_swcounter_event(PERF_TYPE_TRACEPOINT
, event_id
, 1, 1, regs
, 0);
3112 EXPORT_SYMBOL_GPL(perf_tpcounter_event
);
3114 extern int ftrace_profile_enable(int);
3115 extern void ftrace_profile_disable(int);
3117 static void tp_perf_counter_destroy(struct perf_counter
*counter
)
3119 ftrace_profile_disable(perf_event_id(&counter
->hw_event
));
3122 static const struct pmu
*tp_perf_counter_init(struct perf_counter
*counter
)
3124 int event_id
= perf_event_id(&counter
->hw_event
);
3127 ret
= ftrace_profile_enable(event_id
);
3131 counter
->destroy
= tp_perf_counter_destroy
;
3132 counter
->hw
.irq_period
= counter
->hw_event
.irq_period
;
3134 return &perf_ops_generic
;
3137 static const struct pmu
*tp_perf_counter_init(struct perf_counter
*counter
)
3143 static const struct pmu
*sw_perf_counter_init(struct perf_counter
*counter
)
3145 const struct pmu
*pmu
= NULL
;
3148 * Software counters (currently) can't in general distinguish
3149 * between user, kernel and hypervisor events.
3150 * However, context switches and cpu migrations are considered
3151 * to be kernel events, and page faults are never hypervisor
3154 switch (perf_event_id(&counter
->hw_event
)) {
3155 case PERF_COUNT_CPU_CLOCK
:
3156 pmu
= &perf_ops_cpu_clock
;
3159 case PERF_COUNT_TASK_CLOCK
:
3161 * If the user instantiates this as a per-cpu counter,
3162 * use the cpu_clock counter instead.
3164 if (counter
->ctx
->task
)
3165 pmu
= &perf_ops_task_clock
;
3167 pmu
= &perf_ops_cpu_clock
;
3170 case PERF_COUNT_PAGE_FAULTS
:
3171 case PERF_COUNT_PAGE_FAULTS_MIN
:
3172 case PERF_COUNT_PAGE_FAULTS_MAJ
:
3173 case PERF_COUNT_CONTEXT_SWITCHES
:
3174 pmu
= &perf_ops_generic
;
3176 case PERF_COUNT_CPU_MIGRATIONS
:
3177 if (!counter
->hw_event
.exclude_kernel
)
3178 pmu
= &perf_ops_cpu_migrations
;
3186 * Allocate and initialize a counter structure
3188 static struct perf_counter
*
3189 perf_counter_alloc(struct perf_counter_hw_event
*hw_event
,
3191 struct perf_counter_context
*ctx
,
3192 struct perf_counter
*group_leader
,
3195 const struct pmu
*pmu
;
3196 struct perf_counter
*counter
;
3197 struct hw_perf_counter
*hwc
;
3200 counter
= kzalloc(sizeof(*counter
), gfpflags
);
3202 return ERR_PTR(-ENOMEM
);
3205 * Single counters are their own group leaders, with an
3206 * empty sibling list:
3209 group_leader
= counter
;
3211 mutex_init(&counter
->child_mutex
);
3212 INIT_LIST_HEAD(&counter
->child_list
);
3214 INIT_LIST_HEAD(&counter
->list_entry
);
3215 INIT_LIST_HEAD(&counter
->event_entry
);
3216 INIT_LIST_HEAD(&counter
->sibling_list
);
3217 init_waitqueue_head(&counter
->waitq
);
3219 mutex_init(&counter
->mmap_mutex
);
3222 counter
->hw_event
= *hw_event
;
3223 counter
->group_leader
= group_leader
;
3224 counter
->pmu
= NULL
;
3226 counter
->oncpu
= -1;
3228 counter
->state
= PERF_COUNTER_STATE_INACTIVE
;
3229 if (hw_event
->disabled
)
3230 counter
->state
= PERF_COUNTER_STATE_OFF
;
3235 if (hw_event
->freq
&& hw_event
->irq_freq
)
3236 hwc
->irq_period
= div64_u64(TICK_NSEC
, hw_event
->irq_freq
);
3238 hwc
->irq_period
= hw_event
->irq_period
;
3241 * we currently do not support PERF_RECORD_GROUP on inherited counters
3243 if (hw_event
->inherit
&& (hw_event
->record_type
& PERF_RECORD_GROUP
))
3246 if (perf_event_raw(hw_event
)) {
3247 pmu
= hw_perf_counter_init(counter
);
3251 switch (perf_event_type(hw_event
)) {
3252 case PERF_TYPE_HARDWARE
:
3253 pmu
= hw_perf_counter_init(counter
);
3256 case PERF_TYPE_SOFTWARE
:
3257 pmu
= sw_perf_counter_init(counter
);
3260 case PERF_TYPE_TRACEPOINT
:
3261 pmu
= tp_perf_counter_init(counter
);
3268 else if (IS_ERR(pmu
))
3273 return ERR_PTR(err
);
3278 atomic_inc(&nr_counters
);
3279 if (counter
->hw_event
.mmap
)
3280 atomic_inc(&nr_mmap_tracking
);
3281 if (counter
->hw_event
.munmap
)
3282 atomic_inc(&nr_munmap_tracking
);
3283 if (counter
->hw_event
.comm
)
3284 atomic_inc(&nr_comm_tracking
);
3290 * sys_perf_counter_open - open a performance counter, associate it to a task/cpu
3292 * @hw_event_uptr: event type attributes for monitoring/sampling
3295 * @group_fd: group leader counter fd
3297 SYSCALL_DEFINE5(perf_counter_open
,
3298 const struct perf_counter_hw_event __user
*, hw_event_uptr
,
3299 pid_t
, pid
, int, cpu
, int, group_fd
, unsigned long, flags
)
3301 struct perf_counter
*counter
, *group_leader
;
3302 struct perf_counter_hw_event hw_event
;
3303 struct perf_counter_context
*ctx
;
3304 struct file
*counter_file
= NULL
;
3305 struct file
*group_file
= NULL
;
3306 int fput_needed
= 0;
3307 int fput_needed2
= 0;
3310 /* for future expandability... */
3314 if (copy_from_user(&hw_event
, hw_event_uptr
, sizeof(hw_event
)) != 0)
3318 * Get the target context (task or percpu):
3320 ctx
= find_get_context(pid
, cpu
);
3322 return PTR_ERR(ctx
);
3325 * Look up the group leader (we will attach this counter to it):
3327 group_leader
= NULL
;
3328 if (group_fd
!= -1) {
3330 group_file
= fget_light(group_fd
, &fput_needed
);
3332 goto err_put_context
;
3333 if (group_file
->f_op
!= &perf_fops
)
3334 goto err_put_context
;
3336 group_leader
= group_file
->private_data
;
3338 * Do not allow a recursive hierarchy (this new sibling
3339 * becoming part of another group-sibling):
3341 if (group_leader
->group_leader
!= group_leader
)
3342 goto err_put_context
;
3344 * Do not allow to attach to a group in a different
3345 * task or CPU context:
3347 if (group_leader
->ctx
!= ctx
)
3348 goto err_put_context
;
3350 * Only a group leader can be exclusive or pinned
3352 if (hw_event
.exclusive
|| hw_event
.pinned
)
3353 goto err_put_context
;
3356 counter
= perf_counter_alloc(&hw_event
, cpu
, ctx
, group_leader
,
3358 ret
= PTR_ERR(counter
);
3359 if (IS_ERR(counter
))
3360 goto err_put_context
;
3362 ret
= anon_inode_getfd("[perf_counter]", &perf_fops
, counter
, 0);
3364 goto err_free_put_context
;
3366 counter_file
= fget_light(ret
, &fput_needed2
);
3368 goto err_free_put_context
;
3370 counter
->filp
= counter_file
;
3371 WARN_ON_ONCE(ctx
->parent_ctx
);
3372 mutex_lock(&ctx
->mutex
);
3373 perf_install_in_context(ctx
, counter
, cpu
);
3375 mutex_unlock(&ctx
->mutex
);
3377 counter
->owner
= current
;
3378 get_task_struct(current
);
3379 mutex_lock(¤t
->perf_counter_mutex
);
3380 list_add_tail(&counter
->owner_entry
, ¤t
->perf_counter_list
);
3381 mutex_unlock(¤t
->perf_counter_mutex
);
3383 fput_light(counter_file
, fput_needed2
);
3386 fput_light(group_file
, fput_needed
);
3390 err_free_put_context
:
3400 * inherit a counter from parent task to child task:
3402 static struct perf_counter
*
3403 inherit_counter(struct perf_counter
*parent_counter
,
3404 struct task_struct
*parent
,
3405 struct perf_counter_context
*parent_ctx
,
3406 struct task_struct
*child
,
3407 struct perf_counter
*group_leader
,
3408 struct perf_counter_context
*child_ctx
)
3410 struct perf_counter
*child_counter
;
3413 * Instead of creating recursive hierarchies of counters,
3414 * we link inherited counters back to the original parent,
3415 * which has a filp for sure, which we use as the reference
3418 if (parent_counter
->parent
)
3419 parent_counter
= parent_counter
->parent
;
3421 child_counter
= perf_counter_alloc(&parent_counter
->hw_event
,
3422 parent_counter
->cpu
, child_ctx
,
3423 group_leader
, GFP_KERNEL
);
3424 if (IS_ERR(child_counter
))
3425 return child_counter
;
3429 * Make the child state follow the state of the parent counter,
3430 * not its hw_event.disabled bit. We hold the parent's mutex,
3431 * so we won't race with perf_counter_{en,dis}able_family.
3433 if (parent_counter
->state
>= PERF_COUNTER_STATE_INACTIVE
)
3434 child_counter
->state
= PERF_COUNTER_STATE_INACTIVE
;
3436 child_counter
->state
= PERF_COUNTER_STATE_OFF
;
3439 * Link it up in the child's context:
3441 add_counter_to_ctx(child_counter
, child_ctx
);
3443 child_counter
->parent
= parent_counter
;
3445 * inherit into child's child as well:
3447 child_counter
->hw_event
.inherit
= 1;
3450 * Get a reference to the parent filp - we will fput it
3451 * when the child counter exits. This is safe to do because
3452 * we are in the parent and we know that the filp still
3453 * exists and has a nonzero count:
3455 atomic_long_inc(&parent_counter
->filp
->f_count
);
3458 * Link this into the parent counter's child list
3460 WARN_ON_ONCE(parent_counter
->ctx
->parent_ctx
);
3461 mutex_lock(&parent_counter
->child_mutex
);
3462 list_add_tail(&child_counter
->child_list
, &parent_counter
->child_list
);
3463 mutex_unlock(&parent_counter
->child_mutex
);
3465 return child_counter
;
3468 static int inherit_group(struct perf_counter
*parent_counter
,
3469 struct task_struct
*parent
,
3470 struct perf_counter_context
*parent_ctx
,
3471 struct task_struct
*child
,
3472 struct perf_counter_context
*child_ctx
)
3474 struct perf_counter
*leader
;
3475 struct perf_counter
*sub
;
3476 struct perf_counter
*child_ctr
;
3478 leader
= inherit_counter(parent_counter
, parent
, parent_ctx
,
3479 child
, NULL
, child_ctx
);
3481 return PTR_ERR(leader
);
3482 list_for_each_entry(sub
, &parent_counter
->sibling_list
, list_entry
) {
3483 child_ctr
= inherit_counter(sub
, parent
, parent_ctx
,
3484 child
, leader
, child_ctx
);
3485 if (IS_ERR(child_ctr
))
3486 return PTR_ERR(child_ctr
);
3491 static void sync_child_counter(struct perf_counter
*child_counter
,
3492 struct perf_counter
*parent_counter
)
3496 child_val
= atomic64_read(&child_counter
->count
);
3499 * Add back the child's count to the parent's count:
3501 atomic64_add(child_val
, &parent_counter
->count
);
3502 atomic64_add(child_counter
->total_time_enabled
,
3503 &parent_counter
->child_total_time_enabled
);
3504 atomic64_add(child_counter
->total_time_running
,
3505 &parent_counter
->child_total_time_running
);
3508 * Remove this counter from the parent's list
3510 WARN_ON_ONCE(parent_counter
->ctx
->parent_ctx
);
3511 mutex_lock(&parent_counter
->child_mutex
);
3512 list_del_init(&child_counter
->child_list
);
3513 mutex_unlock(&parent_counter
->child_mutex
);
3516 * Release the parent counter, if this was the last
3519 fput(parent_counter
->filp
);
3523 __perf_counter_exit_task(struct task_struct
*child
,
3524 struct perf_counter
*child_counter
,
3525 struct perf_counter_context
*child_ctx
)
3527 struct perf_counter
*parent_counter
;
3529 update_counter_times(child_counter
);
3530 perf_counter_remove_from_context(child_counter
);
3532 parent_counter
= child_counter
->parent
;
3534 * It can happen that parent exits first, and has counters
3535 * that are still around due to the child reference. These
3536 * counters need to be zapped - but otherwise linger.
3538 if (parent_counter
) {
3539 sync_child_counter(child_counter
, parent_counter
);
3540 free_counter(child_counter
);
3545 * When a child task exits, feed back counter values to parent counters.
3547 void perf_counter_exit_task(struct task_struct
*child
)
3549 struct perf_counter
*child_counter
, *tmp
;
3550 struct perf_counter_context
*child_ctx
;
3551 unsigned long flags
;
3553 if (likely(!child
->perf_counter_ctxp
))
3556 local_irq_save(flags
);
3558 * We can't reschedule here because interrupts are disabled,
3559 * and either child is current or it is a task that can't be
3560 * scheduled, so we are now safe from rescheduling changing
3563 child_ctx
= child
->perf_counter_ctxp
;
3564 __perf_counter_task_sched_out(child_ctx
);
3567 * Take the context lock here so that if find_get_context is
3568 * reading child->perf_counter_ctxp, we wait until it has
3569 * incremented the context's refcount before we do put_ctx below.
3571 spin_lock(&child_ctx
->lock
);
3572 child
->perf_counter_ctxp
= NULL
;
3573 if (child_ctx
->parent_ctx
) {
3575 * This context is a clone; unclone it so it can't get
3576 * swapped to another process while we're removing all
3577 * the counters from it.
3579 put_ctx(child_ctx
->parent_ctx
);
3580 child_ctx
->parent_ctx
= NULL
;
3582 spin_unlock(&child_ctx
->lock
);
3583 local_irq_restore(flags
);
3585 mutex_lock(&child_ctx
->mutex
);
3588 list_for_each_entry_safe(child_counter
, tmp
, &child_ctx
->counter_list
,
3590 __perf_counter_exit_task(child
, child_counter
, child_ctx
);
3593 * If the last counter was a group counter, it will have appended all
3594 * its siblings to the list, but we obtained 'tmp' before that which
3595 * will still point to the list head terminating the iteration.
3597 if (!list_empty(&child_ctx
->counter_list
))
3600 mutex_unlock(&child_ctx
->mutex
);
3606 * Initialize the perf_counter context in task_struct
3608 int perf_counter_init_task(struct task_struct
*child
)
3610 struct perf_counter_context
*child_ctx
, *parent_ctx
;
3611 struct perf_counter_context
*cloned_ctx
;
3612 struct perf_counter
*counter
;
3613 struct task_struct
*parent
= current
;
3614 int inherited_all
= 1;
3618 child
->perf_counter_ctxp
= NULL
;
3620 mutex_init(&child
->perf_counter_mutex
);
3621 INIT_LIST_HEAD(&child
->perf_counter_list
);
3623 if (likely(!parent
->perf_counter_ctxp
))
3627 * This is executed from the parent task context, so inherit
3628 * counters that have been marked for cloning.
3629 * First allocate and initialize a context for the child.
3632 child_ctx
= kmalloc(sizeof(struct perf_counter_context
), GFP_KERNEL
);
3636 __perf_counter_init_context(child_ctx
, child
);
3637 child
->perf_counter_ctxp
= child_ctx
;
3638 get_task_struct(child
);
3641 * If the parent's context is a clone, temporarily set its
3642 * parent_gen to an impossible value (all 1s) so it won't get
3643 * swapped under us. The rcu_read_lock makes sure that
3644 * parent_ctx continues to exist even if it gets swapped to
3645 * another process and then freed while we are trying to get
3650 parent_ctx
= rcu_dereference(parent
->perf_counter_ctxp
);
3652 * No need to check if parent_ctx != NULL here; since we saw
3653 * it non-NULL earlier, the only reason for it to become NULL
3654 * is if we exit, and since we're currently in the middle of
3655 * a fork we can't be exiting at the same time.
3657 spin_lock_irq(&parent_ctx
->lock
);
3658 if (parent_ctx
!= rcu_dereference(parent
->perf_counter_ctxp
)) {
3659 spin_unlock_irq(&parent_ctx
->lock
);
3662 cloned_gen
= parent_ctx
->parent_gen
;
3663 if (parent_ctx
->parent_ctx
)
3664 parent_ctx
->parent_gen
= ~0ull;
3665 spin_unlock_irq(&parent_ctx
->lock
);
3669 * Lock the parent list. No need to lock the child - not PID
3670 * hashed yet and not running, so nobody can access it.
3672 mutex_lock(&parent_ctx
->mutex
);
3675 * We dont have to disable NMIs - we are only looking at
3676 * the list, not manipulating it:
3678 list_for_each_entry_rcu(counter
, &parent_ctx
->event_list
, event_entry
) {
3679 if (counter
!= counter
->group_leader
)
3682 if (!counter
->hw_event
.inherit
) {
3687 ret
= inherit_group(counter
, parent
, parent_ctx
,
3695 if (inherited_all
) {
3697 * Mark the child context as a clone of the parent
3698 * context, or of whatever the parent is a clone of.
3699 * Note that if the parent is a clone, it could get
3700 * uncloned at any point, but that doesn't matter
3701 * because the list of counters and the generation
3702 * count can't have changed since we took the mutex.
3704 cloned_ctx
= rcu_dereference(parent_ctx
->parent_ctx
);
3706 child_ctx
->parent_ctx
= cloned_ctx
;
3707 child_ctx
->parent_gen
= cloned_gen
;
3709 child_ctx
->parent_ctx
= parent_ctx
;
3710 child_ctx
->parent_gen
= parent_ctx
->generation
;
3712 get_ctx(child_ctx
->parent_ctx
);
3715 mutex_unlock(&parent_ctx
->mutex
);
3718 * Restore the clone status of the parent.
3720 if (parent_ctx
->parent_ctx
) {
3721 spin_lock_irq(&parent_ctx
->lock
);
3722 if (parent_ctx
->parent_ctx
)
3723 parent_ctx
->parent_gen
= cloned_gen
;
3724 spin_unlock_irq(&parent_ctx
->lock
);
3730 static void __cpuinit
perf_counter_init_cpu(int cpu
)
3732 struct perf_cpu_context
*cpuctx
;
3734 cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
3735 __perf_counter_init_context(&cpuctx
->ctx
, NULL
);
3737 spin_lock(&perf_resource_lock
);
3738 cpuctx
->max_pertask
= perf_max_counters
- perf_reserved_percpu
;
3739 spin_unlock(&perf_resource_lock
);
3741 hw_perf_counter_setup(cpu
);
3744 #ifdef CONFIG_HOTPLUG_CPU
3745 static void __perf_counter_exit_cpu(void *info
)
3747 struct perf_cpu_context
*cpuctx
= &__get_cpu_var(perf_cpu_context
);
3748 struct perf_counter_context
*ctx
= &cpuctx
->ctx
;
3749 struct perf_counter
*counter
, *tmp
;
3751 list_for_each_entry_safe(counter
, tmp
, &ctx
->counter_list
, list_entry
)
3752 __perf_counter_remove_from_context(counter
);
3754 static void perf_counter_exit_cpu(int cpu
)
3756 struct perf_cpu_context
*cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
3757 struct perf_counter_context
*ctx
= &cpuctx
->ctx
;
3759 mutex_lock(&ctx
->mutex
);
3760 smp_call_function_single(cpu
, __perf_counter_exit_cpu
, NULL
, 1);
3761 mutex_unlock(&ctx
->mutex
);
3764 static inline void perf_counter_exit_cpu(int cpu
) { }
3767 static int __cpuinit
3768 perf_cpu_notify(struct notifier_block
*self
, unsigned long action
, void *hcpu
)
3770 unsigned int cpu
= (long)hcpu
;
3774 case CPU_UP_PREPARE
:
3775 case CPU_UP_PREPARE_FROZEN
:
3776 perf_counter_init_cpu(cpu
);
3779 case CPU_DOWN_PREPARE
:
3780 case CPU_DOWN_PREPARE_FROZEN
:
3781 perf_counter_exit_cpu(cpu
);
3791 static struct notifier_block __cpuinitdata perf_cpu_nb
= {
3792 .notifier_call
= perf_cpu_notify
,
3795 void __init
perf_counter_init(void)
3797 perf_cpu_notify(&perf_cpu_nb
, (unsigned long)CPU_UP_PREPARE
,
3798 (void *)(long)smp_processor_id());
3799 register_cpu_notifier(&perf_cpu_nb
);
3802 static ssize_t
perf_show_reserve_percpu(struct sysdev_class
*class, char *buf
)
3804 return sprintf(buf
, "%d\n", perf_reserved_percpu
);
3808 perf_set_reserve_percpu(struct sysdev_class
*class,
3812 struct perf_cpu_context
*cpuctx
;
3816 err
= strict_strtoul(buf
, 10, &val
);
3819 if (val
> perf_max_counters
)
3822 spin_lock(&perf_resource_lock
);
3823 perf_reserved_percpu
= val
;
3824 for_each_online_cpu(cpu
) {
3825 cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
3826 spin_lock_irq(&cpuctx
->ctx
.lock
);
3827 mpt
= min(perf_max_counters
- cpuctx
->ctx
.nr_counters
,
3828 perf_max_counters
- perf_reserved_percpu
);
3829 cpuctx
->max_pertask
= mpt
;
3830 spin_unlock_irq(&cpuctx
->ctx
.lock
);
3832 spin_unlock(&perf_resource_lock
);
3837 static ssize_t
perf_show_overcommit(struct sysdev_class
*class, char *buf
)
3839 return sprintf(buf
, "%d\n", perf_overcommit
);
3843 perf_set_overcommit(struct sysdev_class
*class, const char *buf
, size_t count
)
3848 err
= strict_strtoul(buf
, 10, &val
);
3854 spin_lock(&perf_resource_lock
);
3855 perf_overcommit
= val
;
3856 spin_unlock(&perf_resource_lock
);
3861 static SYSDEV_CLASS_ATTR(
3864 perf_show_reserve_percpu
,
3865 perf_set_reserve_percpu
3868 static SYSDEV_CLASS_ATTR(
3871 perf_show_overcommit
,
3875 static struct attribute
*perfclass_attrs
[] = {
3876 &attr_reserve_percpu
.attr
,
3877 &attr_overcommit
.attr
,
3881 static struct attribute_group perfclass_attr_group
= {
3882 .attrs
= perfclass_attrs
,
3883 .name
= "perf_counters",
3886 static int __init
perf_counter_sysfs_init(void)
3888 return sysfs_create_group(&cpu_sysdev_class
.kset
.kobj
,
3889 &perfclass_attr_group
);
3891 device_initcall(perf_counter_sysfs_init
);