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/dcache.h>
20 #include <linux/percpu.h>
21 #include <linux/ptrace.h>
22 #include <linux/vmstat.h>
23 #include <linux/hardirq.h>
24 #include <linux/rculist.h>
25 #include <linux/uaccess.h>
26 #include <linux/syscalls.h>
27 #include <linux/anon_inodes.h>
28 #include <linux/kernel_stat.h>
29 #include <linux/perf_counter.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_counters __read_mostly
;
44 static atomic_t nr_munmap_counters __read_mostly
;
45 static atomic_t nr_comm_counters __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 */
51 static atomic64_t perf_counter_id
;
54 * Lock for (sysadmin-configurable) counter reservations:
56 static DEFINE_SPINLOCK(perf_resource_lock
);
59 * Architecture provided APIs - weak aliases:
61 extern __weak
const struct pmu
*hw_perf_counter_init(struct perf_counter
*counter
)
66 void __weak
hw_perf_disable(void) { barrier(); }
67 void __weak
hw_perf_enable(void) { barrier(); }
69 void __weak
hw_perf_counter_setup(int cpu
) { barrier(); }
72 hw_perf_group_sched_in(struct perf_counter
*group_leader
,
73 struct perf_cpu_context
*cpuctx
,
74 struct perf_counter_context
*ctx
, int cpu
)
79 void __weak
perf_counter_print_debug(void) { }
81 static DEFINE_PER_CPU(int, disable_count
);
83 void __perf_disable(void)
85 __get_cpu_var(disable_count
)++;
88 bool __perf_enable(void)
90 return !--__get_cpu_var(disable_count
);
93 void perf_disable(void)
99 void perf_enable(void)
105 static void get_ctx(struct perf_counter_context
*ctx
)
107 atomic_inc(&ctx
->refcount
);
110 static void free_ctx(struct rcu_head
*head
)
112 struct perf_counter_context
*ctx
;
114 ctx
= container_of(head
, struct perf_counter_context
, rcu_head
);
118 static void put_ctx(struct perf_counter_context
*ctx
)
120 if (atomic_dec_and_test(&ctx
->refcount
)) {
122 put_ctx(ctx
->parent_ctx
);
124 put_task_struct(ctx
->task
);
125 call_rcu(&ctx
->rcu_head
, free_ctx
);
130 * Get the perf_counter_context for a task and lock it.
131 * This has to cope with with the fact that until it is locked,
132 * the context could get moved to another task.
134 static struct perf_counter_context
*
135 perf_lock_task_context(struct task_struct
*task
, unsigned long *flags
)
137 struct perf_counter_context
*ctx
;
141 ctx
= rcu_dereference(task
->perf_counter_ctxp
);
144 * If this context is a clone of another, it might
145 * get swapped for another underneath us by
146 * perf_counter_task_sched_out, though the
147 * rcu_read_lock() protects us from any context
148 * getting freed. Lock the context and check if it
149 * got swapped before we could get the lock, and retry
150 * if so. If we locked the right context, then it
151 * can't get swapped on us any more.
153 spin_lock_irqsave(&ctx
->lock
, *flags
);
154 if (ctx
!= rcu_dereference(task
->perf_counter_ctxp
)) {
155 spin_unlock_irqrestore(&ctx
->lock
, *flags
);
164 * Get the context for a task and increment its pin_count so it
165 * can't get swapped to another task. This also increments its
166 * reference count so that the context can't get freed.
168 static struct perf_counter_context
*perf_pin_task_context(struct task_struct
*task
)
170 struct perf_counter_context
*ctx
;
173 ctx
= perf_lock_task_context(task
, &flags
);
177 spin_unlock_irqrestore(&ctx
->lock
, flags
);
182 static void perf_unpin_context(struct perf_counter_context
*ctx
)
186 spin_lock_irqsave(&ctx
->lock
, flags
);
188 spin_unlock_irqrestore(&ctx
->lock
, flags
);
193 * Add a counter from the lists for its context.
194 * Must be called with ctx->mutex and ctx->lock held.
197 list_add_counter(struct perf_counter
*counter
, struct perf_counter_context
*ctx
)
199 struct perf_counter
*group_leader
= counter
->group_leader
;
202 * Depending on whether it is a standalone or sibling counter,
203 * add it straight to the context's counter list, or to the group
204 * leader's sibling list:
206 if (group_leader
== counter
)
207 list_add_tail(&counter
->list_entry
, &ctx
->counter_list
);
209 list_add_tail(&counter
->list_entry
, &group_leader
->sibling_list
);
210 group_leader
->nr_siblings
++;
213 list_add_rcu(&counter
->event_entry
, &ctx
->event_list
);
218 * Remove a counter from the lists for its context.
219 * Must be called with ctx->mutex and ctx->lock held.
222 list_del_counter(struct perf_counter
*counter
, struct perf_counter_context
*ctx
)
224 struct perf_counter
*sibling
, *tmp
;
226 if (list_empty(&counter
->list_entry
))
230 list_del_init(&counter
->list_entry
);
231 list_del_rcu(&counter
->event_entry
);
233 if (counter
->group_leader
!= counter
)
234 counter
->group_leader
->nr_siblings
--;
237 * If this was a group counter with sibling counters then
238 * upgrade the siblings to singleton counters by adding them
239 * to the context list directly:
241 list_for_each_entry_safe(sibling
, tmp
,
242 &counter
->sibling_list
, list_entry
) {
244 list_move_tail(&sibling
->list_entry
, &ctx
->counter_list
);
245 sibling
->group_leader
= sibling
;
250 counter_sched_out(struct perf_counter
*counter
,
251 struct perf_cpu_context
*cpuctx
,
252 struct perf_counter_context
*ctx
)
254 if (counter
->state
!= PERF_COUNTER_STATE_ACTIVE
)
257 counter
->state
= PERF_COUNTER_STATE_INACTIVE
;
258 counter
->tstamp_stopped
= ctx
->time
;
259 counter
->pmu
->disable(counter
);
262 if (!is_software_counter(counter
))
263 cpuctx
->active_oncpu
--;
265 if (counter
->attr
.exclusive
|| !cpuctx
->active_oncpu
)
266 cpuctx
->exclusive
= 0;
270 group_sched_out(struct perf_counter
*group_counter
,
271 struct perf_cpu_context
*cpuctx
,
272 struct perf_counter_context
*ctx
)
274 struct perf_counter
*counter
;
276 if (group_counter
->state
!= PERF_COUNTER_STATE_ACTIVE
)
279 counter_sched_out(group_counter
, cpuctx
, ctx
);
282 * Schedule out siblings (if any):
284 list_for_each_entry(counter
, &group_counter
->sibling_list
, list_entry
)
285 counter_sched_out(counter
, cpuctx
, ctx
);
287 if (group_counter
->attr
.exclusive
)
288 cpuctx
->exclusive
= 0;
292 * Cross CPU call to remove a performance counter
294 * We disable the counter on the hardware level first. After that we
295 * remove it from the context list.
297 static void __perf_counter_remove_from_context(void *info
)
299 struct perf_cpu_context
*cpuctx
= &__get_cpu_var(perf_cpu_context
);
300 struct perf_counter
*counter
= info
;
301 struct perf_counter_context
*ctx
= counter
->ctx
;
304 * If this is a task context, we need to check whether it is
305 * the current task context of this cpu. If not it has been
306 * scheduled out before the smp call arrived.
308 if (ctx
->task
&& cpuctx
->task_ctx
!= ctx
)
311 spin_lock(&ctx
->lock
);
313 * Protect the list operation against NMI by disabling the
314 * counters on a global level.
318 counter_sched_out(counter
, cpuctx
, ctx
);
320 list_del_counter(counter
, ctx
);
324 * Allow more per task counters with respect to the
327 cpuctx
->max_pertask
=
328 min(perf_max_counters
- ctx
->nr_counters
,
329 perf_max_counters
- perf_reserved_percpu
);
333 spin_unlock(&ctx
->lock
);
338 * Remove the counter from a task's (or a CPU's) list of counters.
340 * Must be called with ctx->mutex held.
342 * CPU counters are removed with a smp call. For task counters we only
343 * call when the task is on a CPU.
345 * If counter->ctx is a cloned context, callers must make sure that
346 * every task struct that counter->ctx->task could possibly point to
347 * remains valid. This is OK when called from perf_release since
348 * that only calls us on the top-level context, which can't be a clone.
349 * When called from perf_counter_exit_task, it's OK because the
350 * context has been detached from its task.
352 static void perf_counter_remove_from_context(struct perf_counter
*counter
)
354 struct perf_counter_context
*ctx
= counter
->ctx
;
355 struct task_struct
*task
= ctx
->task
;
359 * Per cpu counters are removed via an smp call and
360 * the removal is always sucessful.
362 smp_call_function_single(counter
->cpu
,
363 __perf_counter_remove_from_context
,
369 task_oncpu_function_call(task
, __perf_counter_remove_from_context
,
372 spin_lock_irq(&ctx
->lock
);
374 * If the context is active we need to retry the smp call.
376 if (ctx
->nr_active
&& !list_empty(&counter
->list_entry
)) {
377 spin_unlock_irq(&ctx
->lock
);
382 * The lock prevents that this context is scheduled in so we
383 * can remove the counter safely, if the call above did not
386 if (!list_empty(&counter
->list_entry
)) {
387 list_del_counter(counter
, ctx
);
389 spin_unlock_irq(&ctx
->lock
);
392 static inline u64
perf_clock(void)
394 return cpu_clock(smp_processor_id());
398 * Update the record of the current time in a context.
400 static void update_context_time(struct perf_counter_context
*ctx
)
402 u64 now
= perf_clock();
404 ctx
->time
+= now
- ctx
->timestamp
;
405 ctx
->timestamp
= now
;
409 * Update the total_time_enabled and total_time_running fields for a counter.
411 static void update_counter_times(struct perf_counter
*counter
)
413 struct perf_counter_context
*ctx
= counter
->ctx
;
416 if (counter
->state
< PERF_COUNTER_STATE_INACTIVE
)
419 counter
->total_time_enabled
= ctx
->time
- counter
->tstamp_enabled
;
421 if (counter
->state
== PERF_COUNTER_STATE_INACTIVE
)
422 run_end
= counter
->tstamp_stopped
;
426 counter
->total_time_running
= run_end
- counter
->tstamp_running
;
430 * Update total_time_enabled and total_time_running for all counters in a group.
432 static void update_group_times(struct perf_counter
*leader
)
434 struct perf_counter
*counter
;
436 update_counter_times(leader
);
437 list_for_each_entry(counter
, &leader
->sibling_list
, list_entry
)
438 update_counter_times(counter
);
442 * Cross CPU call to disable a performance counter
444 static void __perf_counter_disable(void *info
)
446 struct perf_counter
*counter
= info
;
447 struct perf_cpu_context
*cpuctx
= &__get_cpu_var(perf_cpu_context
);
448 struct perf_counter_context
*ctx
= counter
->ctx
;
451 * If this is a per-task counter, need to check whether this
452 * counter's task is the current task on this cpu.
454 if (ctx
->task
&& cpuctx
->task_ctx
!= ctx
)
457 spin_lock(&ctx
->lock
);
460 * If the counter is on, turn it off.
461 * If it is in error state, leave it in error state.
463 if (counter
->state
>= PERF_COUNTER_STATE_INACTIVE
) {
464 update_context_time(ctx
);
465 update_counter_times(counter
);
466 if (counter
== counter
->group_leader
)
467 group_sched_out(counter
, cpuctx
, ctx
);
469 counter_sched_out(counter
, cpuctx
, ctx
);
470 counter
->state
= PERF_COUNTER_STATE_OFF
;
473 spin_unlock(&ctx
->lock
);
479 * If counter->ctx is a cloned context, callers must make sure that
480 * every task struct that counter->ctx->task could possibly point to
481 * remains valid. This condition is satisifed when called through
482 * perf_counter_for_each_child or perf_counter_for_each because they
483 * hold the top-level counter's child_mutex, so any descendant that
484 * goes to exit will block in sync_child_counter.
485 * When called from perf_pending_counter it's OK because counter->ctx
486 * is the current context on this CPU and preemption is disabled,
487 * hence we can't get into perf_counter_task_sched_out for this context.
489 static void perf_counter_disable(struct perf_counter
*counter
)
491 struct perf_counter_context
*ctx
= counter
->ctx
;
492 struct task_struct
*task
= ctx
->task
;
496 * Disable the counter on the cpu that it's on
498 smp_call_function_single(counter
->cpu
, __perf_counter_disable
,
504 task_oncpu_function_call(task
, __perf_counter_disable
, counter
);
506 spin_lock_irq(&ctx
->lock
);
508 * If the counter is still active, we need to retry the cross-call.
510 if (counter
->state
== PERF_COUNTER_STATE_ACTIVE
) {
511 spin_unlock_irq(&ctx
->lock
);
516 * Since we have the lock this context can't be scheduled
517 * in, so we can change the state safely.
519 if (counter
->state
== PERF_COUNTER_STATE_INACTIVE
) {
520 update_counter_times(counter
);
521 counter
->state
= PERF_COUNTER_STATE_OFF
;
524 spin_unlock_irq(&ctx
->lock
);
528 counter_sched_in(struct perf_counter
*counter
,
529 struct perf_cpu_context
*cpuctx
,
530 struct perf_counter_context
*ctx
,
533 if (counter
->state
<= PERF_COUNTER_STATE_OFF
)
536 counter
->state
= PERF_COUNTER_STATE_ACTIVE
;
537 counter
->oncpu
= cpu
; /* TODO: put 'cpu' into cpuctx->cpu */
539 * The new state must be visible before we turn it on in the hardware:
543 if (counter
->pmu
->enable(counter
)) {
544 counter
->state
= PERF_COUNTER_STATE_INACTIVE
;
549 counter
->tstamp_running
+= ctx
->time
- counter
->tstamp_stopped
;
551 if (!is_software_counter(counter
))
552 cpuctx
->active_oncpu
++;
555 if (counter
->attr
.exclusive
)
556 cpuctx
->exclusive
= 1;
562 group_sched_in(struct perf_counter
*group_counter
,
563 struct perf_cpu_context
*cpuctx
,
564 struct perf_counter_context
*ctx
,
567 struct perf_counter
*counter
, *partial_group
;
570 if (group_counter
->state
== PERF_COUNTER_STATE_OFF
)
573 ret
= hw_perf_group_sched_in(group_counter
, cpuctx
, ctx
, cpu
);
575 return ret
< 0 ? ret
: 0;
577 if (counter_sched_in(group_counter
, cpuctx
, ctx
, cpu
))
581 * Schedule in siblings as one group (if any):
583 list_for_each_entry(counter
, &group_counter
->sibling_list
, list_entry
) {
584 if (counter_sched_in(counter
, cpuctx
, ctx
, cpu
)) {
585 partial_group
= counter
;
594 * Groups can be scheduled in as one unit only, so undo any
595 * partial group before returning:
597 list_for_each_entry(counter
, &group_counter
->sibling_list
, list_entry
) {
598 if (counter
== partial_group
)
600 counter_sched_out(counter
, cpuctx
, ctx
);
602 counter_sched_out(group_counter
, cpuctx
, ctx
);
608 * Return 1 for a group consisting entirely of software counters,
609 * 0 if the group contains any hardware counters.
611 static int is_software_only_group(struct perf_counter
*leader
)
613 struct perf_counter
*counter
;
615 if (!is_software_counter(leader
))
618 list_for_each_entry(counter
, &leader
->sibling_list
, list_entry
)
619 if (!is_software_counter(counter
))
626 * Work out whether we can put this counter group on the CPU now.
628 static int group_can_go_on(struct perf_counter
*counter
,
629 struct perf_cpu_context
*cpuctx
,
633 * Groups consisting entirely of software counters can always go on.
635 if (is_software_only_group(counter
))
638 * If an exclusive group is already on, no other hardware
639 * counters can go on.
641 if (cpuctx
->exclusive
)
644 * If this group is exclusive and there are already
645 * counters on the CPU, it can't go on.
647 if (counter
->attr
.exclusive
&& cpuctx
->active_oncpu
)
650 * Otherwise, try to add it if all previous groups were able
656 static void add_counter_to_ctx(struct perf_counter
*counter
,
657 struct perf_counter_context
*ctx
)
659 list_add_counter(counter
, ctx
);
660 counter
->tstamp_enabled
= ctx
->time
;
661 counter
->tstamp_running
= ctx
->time
;
662 counter
->tstamp_stopped
= ctx
->time
;
666 * Cross CPU call to install and enable a performance counter
668 * Must be called with ctx->mutex held
670 static void __perf_install_in_context(void *info
)
672 struct perf_cpu_context
*cpuctx
= &__get_cpu_var(perf_cpu_context
);
673 struct perf_counter
*counter
= info
;
674 struct perf_counter_context
*ctx
= counter
->ctx
;
675 struct perf_counter
*leader
= counter
->group_leader
;
676 int cpu
= smp_processor_id();
680 * If this is a task context, we need to check whether it is
681 * the current task context of this cpu. If not it has been
682 * scheduled out before the smp call arrived.
683 * Or possibly this is the right context but it isn't
684 * on this cpu because it had no counters.
686 if (ctx
->task
&& cpuctx
->task_ctx
!= ctx
) {
687 if (cpuctx
->task_ctx
|| ctx
->task
!= current
)
689 cpuctx
->task_ctx
= ctx
;
692 spin_lock(&ctx
->lock
);
694 update_context_time(ctx
);
697 * Protect the list operation against NMI by disabling the
698 * counters on a global level. NOP for non NMI based counters.
702 add_counter_to_ctx(counter
, ctx
);
705 * Don't put the counter on if it is disabled or if
706 * it is in a group and the group isn't on.
708 if (counter
->state
!= PERF_COUNTER_STATE_INACTIVE
||
709 (leader
!= counter
&& leader
->state
!= PERF_COUNTER_STATE_ACTIVE
))
713 * An exclusive counter can't go on if there are already active
714 * hardware counters, and no hardware counter can go on if there
715 * is already an exclusive counter on.
717 if (!group_can_go_on(counter
, cpuctx
, 1))
720 err
= counter_sched_in(counter
, cpuctx
, ctx
, cpu
);
724 * This counter couldn't go on. If it is in a group
725 * then we have to pull the whole group off.
726 * If the counter group is pinned then put it in error state.
728 if (leader
!= counter
)
729 group_sched_out(leader
, cpuctx
, ctx
);
730 if (leader
->attr
.pinned
) {
731 update_group_times(leader
);
732 leader
->state
= PERF_COUNTER_STATE_ERROR
;
736 if (!err
&& !ctx
->task
&& cpuctx
->max_pertask
)
737 cpuctx
->max_pertask
--;
742 spin_unlock(&ctx
->lock
);
746 * Attach a performance counter to a context
748 * First we add the counter to the list with the hardware enable bit
749 * in counter->hw_config cleared.
751 * If the counter is attached to a task which is on a CPU we use a smp
752 * call to enable it in the task context. The task might have been
753 * scheduled away, but we check this in the smp call again.
755 * Must be called with ctx->mutex held.
758 perf_install_in_context(struct perf_counter_context
*ctx
,
759 struct perf_counter
*counter
,
762 struct task_struct
*task
= ctx
->task
;
766 * Per cpu counters are installed via an smp call and
767 * the install is always sucessful.
769 smp_call_function_single(cpu
, __perf_install_in_context
,
775 task_oncpu_function_call(task
, __perf_install_in_context
,
778 spin_lock_irq(&ctx
->lock
);
780 * we need to retry the smp call.
782 if (ctx
->is_active
&& list_empty(&counter
->list_entry
)) {
783 spin_unlock_irq(&ctx
->lock
);
788 * The lock prevents that this context is scheduled in so we
789 * can add the counter safely, if it the call above did not
792 if (list_empty(&counter
->list_entry
))
793 add_counter_to_ctx(counter
, ctx
);
794 spin_unlock_irq(&ctx
->lock
);
798 * Cross CPU call to enable a performance counter
800 static void __perf_counter_enable(void *info
)
802 struct perf_counter
*counter
= info
;
803 struct perf_cpu_context
*cpuctx
= &__get_cpu_var(perf_cpu_context
);
804 struct perf_counter_context
*ctx
= counter
->ctx
;
805 struct perf_counter
*leader
= counter
->group_leader
;
809 * If this is a per-task counter, need to check whether this
810 * counter's task is the current task on this cpu.
812 if (ctx
->task
&& cpuctx
->task_ctx
!= ctx
) {
813 if (cpuctx
->task_ctx
|| ctx
->task
!= current
)
815 cpuctx
->task_ctx
= ctx
;
818 spin_lock(&ctx
->lock
);
820 update_context_time(ctx
);
822 if (counter
->state
>= PERF_COUNTER_STATE_INACTIVE
)
824 counter
->state
= PERF_COUNTER_STATE_INACTIVE
;
825 counter
->tstamp_enabled
= ctx
->time
- counter
->total_time_enabled
;
828 * If the counter is in a group and isn't the group leader,
829 * then don't put it on unless the group is on.
831 if (leader
!= counter
&& leader
->state
!= PERF_COUNTER_STATE_ACTIVE
)
834 if (!group_can_go_on(counter
, cpuctx
, 1)) {
838 if (counter
== leader
)
839 err
= group_sched_in(counter
, cpuctx
, ctx
,
842 err
= counter_sched_in(counter
, cpuctx
, ctx
,
849 * If this counter can't go on and it's part of a
850 * group, then the whole group has to come off.
852 if (leader
!= counter
)
853 group_sched_out(leader
, cpuctx
, ctx
);
854 if (leader
->attr
.pinned
) {
855 update_group_times(leader
);
856 leader
->state
= PERF_COUNTER_STATE_ERROR
;
861 spin_unlock(&ctx
->lock
);
867 * If counter->ctx is a cloned context, callers must make sure that
868 * every task struct that counter->ctx->task could possibly point to
869 * remains valid. This condition is satisfied when called through
870 * perf_counter_for_each_child or perf_counter_for_each as described
871 * for perf_counter_disable.
873 static void perf_counter_enable(struct perf_counter
*counter
)
875 struct perf_counter_context
*ctx
= counter
->ctx
;
876 struct task_struct
*task
= ctx
->task
;
880 * Enable the counter on the cpu that it's on
882 smp_call_function_single(counter
->cpu
, __perf_counter_enable
,
887 spin_lock_irq(&ctx
->lock
);
888 if (counter
->state
>= PERF_COUNTER_STATE_INACTIVE
)
892 * If the counter is in error state, clear that first.
893 * That way, if we see the counter in error state below, we
894 * know that it has gone back into error state, as distinct
895 * from the task having been scheduled away before the
896 * cross-call arrived.
898 if (counter
->state
== PERF_COUNTER_STATE_ERROR
)
899 counter
->state
= PERF_COUNTER_STATE_OFF
;
902 spin_unlock_irq(&ctx
->lock
);
903 task_oncpu_function_call(task
, __perf_counter_enable
, counter
);
905 spin_lock_irq(&ctx
->lock
);
908 * If the context is active and the counter is still off,
909 * we need to retry the cross-call.
911 if (ctx
->is_active
&& counter
->state
== PERF_COUNTER_STATE_OFF
)
915 * Since we have the lock this context can't be scheduled
916 * in, so we can change the state safely.
918 if (counter
->state
== PERF_COUNTER_STATE_OFF
) {
919 counter
->state
= PERF_COUNTER_STATE_INACTIVE
;
920 counter
->tstamp_enabled
=
921 ctx
->time
- counter
->total_time_enabled
;
924 spin_unlock_irq(&ctx
->lock
);
927 static int perf_counter_refresh(struct perf_counter
*counter
, int refresh
)
930 * not supported on inherited counters
932 if (counter
->attr
.inherit
)
935 atomic_add(refresh
, &counter
->event_limit
);
936 perf_counter_enable(counter
);
941 void __perf_counter_sched_out(struct perf_counter_context
*ctx
,
942 struct perf_cpu_context
*cpuctx
)
944 struct perf_counter
*counter
;
946 spin_lock(&ctx
->lock
);
948 if (likely(!ctx
->nr_counters
))
950 update_context_time(ctx
);
953 if (ctx
->nr_active
) {
954 list_for_each_entry(counter
, &ctx
->counter_list
, list_entry
) {
955 if (counter
!= counter
->group_leader
)
956 counter_sched_out(counter
, cpuctx
, ctx
);
958 group_sched_out(counter
, cpuctx
, ctx
);
963 spin_unlock(&ctx
->lock
);
967 * Test whether two contexts are equivalent, i.e. whether they
968 * have both been cloned from the same version of the same context
969 * and they both have the same number of enabled counters.
970 * If the number of enabled counters is the same, then the set
971 * of enabled counters should be the same, because these are both
972 * inherited contexts, therefore we can't access individual counters
973 * in them directly with an fd; we can only enable/disable all
974 * counters via prctl, or enable/disable all counters in a family
975 * via ioctl, which will have the same effect on both contexts.
977 static int context_equiv(struct perf_counter_context
*ctx1
,
978 struct perf_counter_context
*ctx2
)
980 return ctx1
->parent_ctx
&& ctx1
->parent_ctx
== ctx2
->parent_ctx
981 && ctx1
->parent_gen
== ctx2
->parent_gen
982 && !ctx1
->pin_count
&& !ctx2
->pin_count
;
986 * Called from scheduler to remove the counters of the current task,
987 * with interrupts disabled.
989 * We stop each counter and update the counter value in counter->count.
991 * This does not protect us against NMI, but disable()
992 * sets the disabled bit in the control field of counter _before_
993 * accessing the counter control register. If a NMI hits, then it will
994 * not restart the counter.
996 void perf_counter_task_sched_out(struct task_struct
*task
,
997 struct task_struct
*next
, int cpu
)
999 struct perf_cpu_context
*cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
1000 struct perf_counter_context
*ctx
= task
->perf_counter_ctxp
;
1001 struct perf_counter_context
*next_ctx
;
1002 struct perf_counter_context
*parent
;
1003 struct pt_regs
*regs
;
1006 regs
= task_pt_regs(task
);
1007 perf_swcounter_event(PERF_COUNT_CONTEXT_SWITCHES
, 1, 1, regs
, 0);
1009 if (likely(!ctx
|| !cpuctx
->task_ctx
))
1012 update_context_time(ctx
);
1015 parent
= rcu_dereference(ctx
->parent_ctx
);
1016 next_ctx
= next
->perf_counter_ctxp
;
1017 if (parent
&& next_ctx
&&
1018 rcu_dereference(next_ctx
->parent_ctx
) == parent
) {
1020 * Looks like the two contexts are clones, so we might be
1021 * able to optimize the context switch. We lock both
1022 * contexts and check that they are clones under the
1023 * lock (including re-checking that neither has been
1024 * uncloned in the meantime). It doesn't matter which
1025 * order we take the locks because no other cpu could
1026 * be trying to lock both of these tasks.
1028 spin_lock(&ctx
->lock
);
1029 spin_lock_nested(&next_ctx
->lock
, SINGLE_DEPTH_NESTING
);
1030 if (context_equiv(ctx
, next_ctx
)) {
1032 * XXX do we need a memory barrier of sorts
1033 * wrt to rcu_dereference() of perf_counter_ctxp
1035 task
->perf_counter_ctxp
= next_ctx
;
1036 next
->perf_counter_ctxp
= ctx
;
1038 next_ctx
->task
= task
;
1041 spin_unlock(&next_ctx
->lock
);
1042 spin_unlock(&ctx
->lock
);
1047 __perf_counter_sched_out(ctx
, cpuctx
);
1048 cpuctx
->task_ctx
= NULL
;
1053 * Called with IRQs disabled
1055 static void __perf_counter_task_sched_out(struct perf_counter_context
*ctx
)
1057 struct perf_cpu_context
*cpuctx
= &__get_cpu_var(perf_cpu_context
);
1059 if (!cpuctx
->task_ctx
)
1062 if (WARN_ON_ONCE(ctx
!= cpuctx
->task_ctx
))
1065 __perf_counter_sched_out(ctx
, cpuctx
);
1066 cpuctx
->task_ctx
= NULL
;
1070 * Called with IRQs disabled
1072 static void perf_counter_cpu_sched_out(struct perf_cpu_context
*cpuctx
)
1074 __perf_counter_sched_out(&cpuctx
->ctx
, cpuctx
);
1078 __perf_counter_sched_in(struct perf_counter_context
*ctx
,
1079 struct perf_cpu_context
*cpuctx
, int cpu
)
1081 struct perf_counter
*counter
;
1084 spin_lock(&ctx
->lock
);
1086 if (likely(!ctx
->nr_counters
))
1089 ctx
->timestamp
= perf_clock();
1094 * First go through the list and put on any pinned groups
1095 * in order to give them the best chance of going on.
1097 list_for_each_entry(counter
, &ctx
->counter_list
, list_entry
) {
1098 if (counter
->state
<= PERF_COUNTER_STATE_OFF
||
1099 !counter
->attr
.pinned
)
1101 if (counter
->cpu
!= -1 && counter
->cpu
!= cpu
)
1104 if (counter
!= counter
->group_leader
)
1105 counter_sched_in(counter
, cpuctx
, ctx
, cpu
);
1107 if (group_can_go_on(counter
, cpuctx
, 1))
1108 group_sched_in(counter
, cpuctx
, ctx
, cpu
);
1112 * If this pinned group hasn't been scheduled,
1113 * put it in error state.
1115 if (counter
->state
== PERF_COUNTER_STATE_INACTIVE
) {
1116 update_group_times(counter
);
1117 counter
->state
= PERF_COUNTER_STATE_ERROR
;
1121 list_for_each_entry(counter
, &ctx
->counter_list
, list_entry
) {
1123 * Ignore counters in OFF or ERROR state, and
1124 * ignore pinned counters since we did them already.
1126 if (counter
->state
<= PERF_COUNTER_STATE_OFF
||
1127 counter
->attr
.pinned
)
1131 * Listen to the 'cpu' scheduling filter constraint
1134 if (counter
->cpu
!= -1 && counter
->cpu
!= cpu
)
1137 if (counter
!= counter
->group_leader
) {
1138 if (counter_sched_in(counter
, cpuctx
, ctx
, cpu
))
1141 if (group_can_go_on(counter
, cpuctx
, can_add_hw
)) {
1142 if (group_sched_in(counter
, cpuctx
, ctx
, cpu
))
1149 spin_unlock(&ctx
->lock
);
1153 * Called from scheduler to add the counters of the current task
1154 * with interrupts disabled.
1156 * We restore the counter value and then enable it.
1158 * This does not protect us against NMI, but enable()
1159 * sets the enabled bit in the control field of counter _before_
1160 * accessing the counter control register. If a NMI hits, then it will
1161 * keep the counter running.
1163 void perf_counter_task_sched_in(struct task_struct
*task
, int cpu
)
1165 struct perf_cpu_context
*cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
1166 struct perf_counter_context
*ctx
= task
->perf_counter_ctxp
;
1170 if (cpuctx
->task_ctx
== ctx
)
1172 __perf_counter_sched_in(ctx
, cpuctx
, cpu
);
1173 cpuctx
->task_ctx
= ctx
;
1176 static void perf_counter_cpu_sched_in(struct perf_cpu_context
*cpuctx
, int cpu
)
1178 struct perf_counter_context
*ctx
= &cpuctx
->ctx
;
1180 __perf_counter_sched_in(ctx
, cpuctx
, cpu
);
1183 #define MAX_INTERRUPTS (~0ULL)
1185 static void perf_log_throttle(struct perf_counter
*counter
, int enable
);
1186 static void perf_log_period(struct perf_counter
*counter
, u64 period
);
1188 static void perf_adjust_freq(struct perf_counter_context
*ctx
)
1190 struct perf_counter
*counter
;
1191 u64 interrupts
, sample_period
;
1195 spin_lock(&ctx
->lock
);
1196 list_for_each_entry(counter
, &ctx
->counter_list
, list_entry
) {
1197 if (counter
->state
!= PERF_COUNTER_STATE_ACTIVE
)
1200 interrupts
= counter
->hw
.interrupts
;
1201 counter
->hw
.interrupts
= 0;
1203 if (interrupts
== MAX_INTERRUPTS
) {
1204 perf_log_throttle(counter
, 1);
1205 counter
->pmu
->unthrottle(counter
);
1206 interrupts
= 2*sysctl_perf_counter_limit
/HZ
;
1209 if (!counter
->attr
.freq
|| !counter
->attr
.sample_freq
)
1212 events
= HZ
* interrupts
* counter
->hw
.sample_period
;
1213 period
= div64_u64(events
, counter
->attr
.sample_freq
);
1215 delta
= (s64
)(1 + period
- counter
->hw
.sample_period
);
1218 sample_period
= counter
->hw
.sample_period
+ delta
;
1223 perf_log_period(counter
, sample_period
);
1225 counter
->hw
.sample_period
= sample_period
;
1227 spin_unlock(&ctx
->lock
);
1231 * Round-robin a context's counters:
1233 static void rotate_ctx(struct perf_counter_context
*ctx
)
1235 struct perf_counter
*counter
;
1237 if (!ctx
->nr_counters
)
1240 spin_lock(&ctx
->lock
);
1242 * Rotate the first entry last (works just fine for group counters too):
1245 list_for_each_entry(counter
, &ctx
->counter_list
, list_entry
) {
1246 list_move_tail(&counter
->list_entry
, &ctx
->counter_list
);
1251 spin_unlock(&ctx
->lock
);
1254 void perf_counter_task_tick(struct task_struct
*curr
, int cpu
)
1256 struct perf_cpu_context
*cpuctx
;
1257 struct perf_counter_context
*ctx
;
1259 if (!atomic_read(&nr_counters
))
1262 cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
1263 ctx
= curr
->perf_counter_ctxp
;
1265 perf_adjust_freq(&cpuctx
->ctx
);
1267 perf_adjust_freq(ctx
);
1269 perf_counter_cpu_sched_out(cpuctx
);
1271 __perf_counter_task_sched_out(ctx
);
1273 rotate_ctx(&cpuctx
->ctx
);
1277 perf_counter_cpu_sched_in(cpuctx
, cpu
);
1279 perf_counter_task_sched_in(curr
, cpu
);
1283 * Cross CPU call to read the hardware counter
1285 static void __read(void *info
)
1287 struct perf_counter
*counter
= info
;
1288 struct perf_counter_context
*ctx
= counter
->ctx
;
1289 unsigned long flags
;
1291 local_irq_save(flags
);
1293 update_context_time(ctx
);
1294 counter
->pmu
->read(counter
);
1295 update_counter_times(counter
);
1296 local_irq_restore(flags
);
1299 static u64
perf_counter_read(struct perf_counter
*counter
)
1302 * If counter is enabled and currently active on a CPU, update the
1303 * value in the counter structure:
1305 if (counter
->state
== PERF_COUNTER_STATE_ACTIVE
) {
1306 smp_call_function_single(counter
->oncpu
,
1307 __read
, counter
, 1);
1308 } else if (counter
->state
== PERF_COUNTER_STATE_INACTIVE
) {
1309 update_counter_times(counter
);
1312 return atomic64_read(&counter
->count
);
1316 * Initialize the perf_counter context in a task_struct:
1319 __perf_counter_init_context(struct perf_counter_context
*ctx
,
1320 struct task_struct
*task
)
1322 memset(ctx
, 0, sizeof(*ctx
));
1323 spin_lock_init(&ctx
->lock
);
1324 mutex_init(&ctx
->mutex
);
1325 INIT_LIST_HEAD(&ctx
->counter_list
);
1326 INIT_LIST_HEAD(&ctx
->event_list
);
1327 atomic_set(&ctx
->refcount
, 1);
1331 static struct perf_counter_context
*find_get_context(pid_t pid
, int cpu
)
1333 struct perf_counter_context
*parent_ctx
;
1334 struct perf_counter_context
*ctx
;
1335 struct perf_cpu_context
*cpuctx
;
1336 struct task_struct
*task
;
1337 unsigned long flags
;
1341 * If cpu is not a wildcard then this is a percpu counter:
1344 /* Must be root to operate on a CPU counter: */
1345 if (sysctl_perf_counter_priv
&& !capable(CAP_SYS_ADMIN
))
1346 return ERR_PTR(-EACCES
);
1348 if (cpu
< 0 || cpu
> num_possible_cpus())
1349 return ERR_PTR(-EINVAL
);
1352 * We could be clever and allow to attach a counter to an
1353 * offline CPU and activate it when the CPU comes up, but
1356 if (!cpu_isset(cpu
, cpu_online_map
))
1357 return ERR_PTR(-ENODEV
);
1359 cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
1370 task
= find_task_by_vpid(pid
);
1372 get_task_struct(task
);
1376 return ERR_PTR(-ESRCH
);
1379 * Can't attach counters to a dying task.
1382 if (task
->flags
& PF_EXITING
)
1385 /* Reuse ptrace permission checks for now. */
1387 if (!ptrace_may_access(task
, PTRACE_MODE_READ
))
1391 ctx
= perf_lock_task_context(task
, &flags
);
1393 parent_ctx
= ctx
->parent_ctx
;
1395 put_ctx(parent_ctx
);
1396 ctx
->parent_ctx
= NULL
; /* no longer a clone */
1399 * Get an extra reference before dropping the lock so that
1400 * this context won't get freed if the task exits.
1403 spin_unlock_irqrestore(&ctx
->lock
, flags
);
1407 ctx
= kmalloc(sizeof(struct perf_counter_context
), GFP_KERNEL
);
1411 __perf_counter_init_context(ctx
, task
);
1413 if (cmpxchg(&task
->perf_counter_ctxp
, NULL
, ctx
)) {
1415 * We raced with some other task; use
1416 * the context they set.
1421 get_task_struct(task
);
1424 put_task_struct(task
);
1428 put_task_struct(task
);
1429 return ERR_PTR(err
);
1432 static void free_counter_rcu(struct rcu_head
*head
)
1434 struct perf_counter
*counter
;
1436 counter
= container_of(head
, struct perf_counter
, rcu_head
);
1438 put_pid_ns(counter
->ns
);
1442 static void perf_pending_sync(struct perf_counter
*counter
);
1444 static void free_counter(struct perf_counter
*counter
)
1446 perf_pending_sync(counter
);
1448 atomic_dec(&nr_counters
);
1449 if (counter
->attr
.mmap
)
1450 atomic_dec(&nr_mmap_counters
);
1451 if (counter
->attr
.munmap
)
1452 atomic_dec(&nr_munmap_counters
);
1453 if (counter
->attr
.comm
)
1454 atomic_dec(&nr_comm_counters
);
1456 if (counter
->destroy
)
1457 counter
->destroy(counter
);
1459 put_ctx(counter
->ctx
);
1460 call_rcu(&counter
->rcu_head
, free_counter_rcu
);
1464 * Called when the last reference to the file is gone.
1466 static int perf_release(struct inode
*inode
, struct file
*file
)
1468 struct perf_counter
*counter
= file
->private_data
;
1469 struct perf_counter_context
*ctx
= counter
->ctx
;
1471 file
->private_data
= NULL
;
1473 WARN_ON_ONCE(ctx
->parent_ctx
);
1474 mutex_lock(&ctx
->mutex
);
1475 perf_counter_remove_from_context(counter
);
1476 mutex_unlock(&ctx
->mutex
);
1478 mutex_lock(&counter
->owner
->perf_counter_mutex
);
1479 list_del_init(&counter
->owner_entry
);
1480 mutex_unlock(&counter
->owner
->perf_counter_mutex
);
1481 put_task_struct(counter
->owner
);
1483 free_counter(counter
);
1489 * Read the performance counter - simple non blocking version for now
1492 perf_read_hw(struct perf_counter
*counter
, char __user
*buf
, size_t count
)
1498 * Return end-of-file for a read on a counter that is in
1499 * error state (i.e. because it was pinned but it couldn't be
1500 * scheduled on to the CPU at some point).
1502 if (counter
->state
== PERF_COUNTER_STATE_ERROR
)
1505 WARN_ON_ONCE(counter
->ctx
->parent_ctx
);
1506 mutex_lock(&counter
->child_mutex
);
1507 values
[0] = perf_counter_read(counter
);
1509 if (counter
->attr
.read_format
& PERF_FORMAT_TOTAL_TIME_ENABLED
)
1510 values
[n
++] = counter
->total_time_enabled
+
1511 atomic64_read(&counter
->child_total_time_enabled
);
1512 if (counter
->attr
.read_format
& PERF_FORMAT_TOTAL_TIME_RUNNING
)
1513 values
[n
++] = counter
->total_time_running
+
1514 atomic64_read(&counter
->child_total_time_running
);
1515 if (counter
->attr
.read_format
& PERF_FORMAT_ID
)
1516 values
[n
++] = counter
->id
;
1517 mutex_unlock(&counter
->child_mutex
);
1519 if (count
< n
* sizeof(u64
))
1521 count
= n
* sizeof(u64
);
1523 if (copy_to_user(buf
, values
, count
))
1530 perf_read(struct file
*file
, char __user
*buf
, size_t count
, loff_t
*ppos
)
1532 struct perf_counter
*counter
= file
->private_data
;
1534 return perf_read_hw(counter
, buf
, count
);
1537 static unsigned int perf_poll(struct file
*file
, poll_table
*wait
)
1539 struct perf_counter
*counter
= file
->private_data
;
1540 struct perf_mmap_data
*data
;
1541 unsigned int events
= POLL_HUP
;
1544 data
= rcu_dereference(counter
->data
);
1546 events
= atomic_xchg(&data
->poll
, 0);
1549 poll_wait(file
, &counter
->waitq
, wait
);
1554 static void perf_counter_reset(struct perf_counter
*counter
)
1556 (void)perf_counter_read(counter
);
1557 atomic64_set(&counter
->count
, 0);
1558 perf_counter_update_userpage(counter
);
1561 static void perf_counter_for_each_sibling(struct perf_counter
*counter
,
1562 void (*func
)(struct perf_counter
*))
1564 struct perf_counter_context
*ctx
= counter
->ctx
;
1565 struct perf_counter
*sibling
;
1567 WARN_ON_ONCE(ctx
->parent_ctx
);
1568 mutex_lock(&ctx
->mutex
);
1569 counter
= counter
->group_leader
;
1572 list_for_each_entry(sibling
, &counter
->sibling_list
, list_entry
)
1574 mutex_unlock(&ctx
->mutex
);
1578 * Holding the top-level counter's child_mutex means that any
1579 * descendant process that has inherited this counter will block
1580 * in sync_child_counter if it goes to exit, thus satisfying the
1581 * task existence requirements of perf_counter_enable/disable.
1583 static void perf_counter_for_each_child(struct perf_counter
*counter
,
1584 void (*func
)(struct perf_counter
*))
1586 struct perf_counter
*child
;
1588 WARN_ON_ONCE(counter
->ctx
->parent_ctx
);
1589 mutex_lock(&counter
->child_mutex
);
1591 list_for_each_entry(child
, &counter
->child_list
, child_list
)
1593 mutex_unlock(&counter
->child_mutex
);
1596 static void perf_counter_for_each(struct perf_counter
*counter
,
1597 void (*func
)(struct perf_counter
*))
1599 struct perf_counter
*child
;
1601 WARN_ON_ONCE(counter
->ctx
->parent_ctx
);
1602 mutex_lock(&counter
->child_mutex
);
1603 perf_counter_for_each_sibling(counter
, func
);
1604 list_for_each_entry(child
, &counter
->child_list
, child_list
)
1605 perf_counter_for_each_sibling(child
, func
);
1606 mutex_unlock(&counter
->child_mutex
);
1609 static int perf_counter_period(struct perf_counter
*counter
, u64 __user
*arg
)
1611 struct perf_counter_context
*ctx
= counter
->ctx
;
1616 if (!counter
->attr
.sample_period
)
1619 size
= copy_from_user(&value
, arg
, sizeof(value
));
1620 if (size
!= sizeof(value
))
1626 spin_lock_irq(&ctx
->lock
);
1627 if (counter
->attr
.freq
) {
1628 if (value
> sysctl_perf_counter_limit
) {
1633 counter
->attr
.sample_freq
= value
;
1635 counter
->attr
.sample_period
= value
;
1636 counter
->hw
.sample_period
= value
;
1638 perf_log_period(counter
, value
);
1641 spin_unlock_irq(&ctx
->lock
);
1646 static long perf_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
1648 struct perf_counter
*counter
= file
->private_data
;
1649 void (*func
)(struct perf_counter
*);
1653 case PERF_COUNTER_IOC_ENABLE
:
1654 func
= perf_counter_enable
;
1656 case PERF_COUNTER_IOC_DISABLE
:
1657 func
= perf_counter_disable
;
1659 case PERF_COUNTER_IOC_RESET
:
1660 func
= perf_counter_reset
;
1663 case PERF_COUNTER_IOC_REFRESH
:
1664 return perf_counter_refresh(counter
, arg
);
1666 case PERF_COUNTER_IOC_PERIOD
:
1667 return perf_counter_period(counter
, (u64 __user
*)arg
);
1673 if (flags
& PERF_IOC_FLAG_GROUP
)
1674 perf_counter_for_each(counter
, func
);
1676 perf_counter_for_each_child(counter
, func
);
1681 int perf_counter_task_enable(void)
1683 struct perf_counter
*counter
;
1685 mutex_lock(¤t
->perf_counter_mutex
);
1686 list_for_each_entry(counter
, ¤t
->perf_counter_list
, owner_entry
)
1687 perf_counter_for_each_child(counter
, perf_counter_enable
);
1688 mutex_unlock(¤t
->perf_counter_mutex
);
1693 int perf_counter_task_disable(void)
1695 struct perf_counter
*counter
;
1697 mutex_lock(¤t
->perf_counter_mutex
);
1698 list_for_each_entry(counter
, ¤t
->perf_counter_list
, owner_entry
)
1699 perf_counter_for_each_child(counter
, perf_counter_disable
);
1700 mutex_unlock(¤t
->perf_counter_mutex
);
1706 * Callers need to ensure there can be no nesting of this function, otherwise
1707 * the seqlock logic goes bad. We can not serialize this because the arch
1708 * code calls this from NMI context.
1710 void perf_counter_update_userpage(struct perf_counter
*counter
)
1712 struct perf_counter_mmap_page
*userpg
;
1713 struct perf_mmap_data
*data
;
1716 data
= rcu_dereference(counter
->data
);
1720 userpg
= data
->user_page
;
1723 * Disable preemption so as to not let the corresponding user-space
1724 * spin too long if we get preempted.
1729 userpg
->index
= counter
->hw
.idx
;
1730 userpg
->offset
= atomic64_read(&counter
->count
);
1731 if (counter
->state
== PERF_COUNTER_STATE_ACTIVE
)
1732 userpg
->offset
-= atomic64_read(&counter
->hw
.prev_count
);
1741 static int perf_mmap_fault(struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
1743 struct perf_counter
*counter
= vma
->vm_file
->private_data
;
1744 struct perf_mmap_data
*data
;
1745 int ret
= VM_FAULT_SIGBUS
;
1748 data
= rcu_dereference(counter
->data
);
1752 if (vmf
->pgoff
== 0) {
1753 vmf
->page
= virt_to_page(data
->user_page
);
1755 int nr
= vmf
->pgoff
- 1;
1757 if ((unsigned)nr
> data
->nr_pages
)
1760 vmf
->page
= virt_to_page(data
->data_pages
[nr
]);
1762 get_page(vmf
->page
);
1770 static int perf_mmap_data_alloc(struct perf_counter
*counter
, int nr_pages
)
1772 struct perf_mmap_data
*data
;
1776 WARN_ON(atomic_read(&counter
->mmap_count
));
1778 size
= sizeof(struct perf_mmap_data
);
1779 size
+= nr_pages
* sizeof(void *);
1781 data
= kzalloc(size
, GFP_KERNEL
);
1785 data
->user_page
= (void *)get_zeroed_page(GFP_KERNEL
);
1786 if (!data
->user_page
)
1787 goto fail_user_page
;
1789 for (i
= 0; i
< nr_pages
; i
++) {
1790 data
->data_pages
[i
] = (void *)get_zeroed_page(GFP_KERNEL
);
1791 if (!data
->data_pages
[i
])
1792 goto fail_data_pages
;
1795 data
->nr_pages
= nr_pages
;
1796 atomic_set(&data
->lock
, -1);
1798 rcu_assign_pointer(counter
->data
, data
);
1803 for (i
--; i
>= 0; i
--)
1804 free_page((unsigned long)data
->data_pages
[i
]);
1806 free_page((unsigned long)data
->user_page
);
1815 static void __perf_mmap_data_free(struct rcu_head
*rcu_head
)
1817 struct perf_mmap_data
*data
;
1820 data
= container_of(rcu_head
, struct perf_mmap_data
, rcu_head
);
1822 free_page((unsigned long)data
->user_page
);
1823 for (i
= 0; i
< data
->nr_pages
; i
++)
1824 free_page((unsigned long)data
->data_pages
[i
]);
1828 static void perf_mmap_data_free(struct perf_counter
*counter
)
1830 struct perf_mmap_data
*data
= counter
->data
;
1832 WARN_ON(atomic_read(&counter
->mmap_count
));
1834 rcu_assign_pointer(counter
->data
, NULL
);
1835 call_rcu(&data
->rcu_head
, __perf_mmap_data_free
);
1838 static void perf_mmap_open(struct vm_area_struct
*vma
)
1840 struct perf_counter
*counter
= vma
->vm_file
->private_data
;
1842 atomic_inc(&counter
->mmap_count
);
1845 static void perf_mmap_close(struct vm_area_struct
*vma
)
1847 struct perf_counter
*counter
= vma
->vm_file
->private_data
;
1849 WARN_ON_ONCE(counter
->ctx
->parent_ctx
);
1850 if (atomic_dec_and_mutex_lock(&counter
->mmap_count
, &counter
->mmap_mutex
)) {
1851 struct user_struct
*user
= current_user();
1853 atomic_long_sub(counter
->data
->nr_pages
+ 1, &user
->locked_vm
);
1854 vma
->vm_mm
->locked_vm
-= counter
->data
->nr_locked
;
1855 perf_mmap_data_free(counter
);
1856 mutex_unlock(&counter
->mmap_mutex
);
1860 static struct vm_operations_struct perf_mmap_vmops
= {
1861 .open
= perf_mmap_open
,
1862 .close
= perf_mmap_close
,
1863 .fault
= perf_mmap_fault
,
1866 static int perf_mmap(struct file
*file
, struct vm_area_struct
*vma
)
1868 struct perf_counter
*counter
= file
->private_data
;
1869 unsigned long user_locked
, user_lock_limit
;
1870 struct user_struct
*user
= current_user();
1871 unsigned long locked
, lock_limit
;
1872 unsigned long vma_size
;
1873 unsigned long nr_pages
;
1874 long user_extra
, extra
;
1877 if (!(vma
->vm_flags
& VM_SHARED
) || (vma
->vm_flags
& VM_WRITE
))
1880 vma_size
= vma
->vm_end
- vma
->vm_start
;
1881 nr_pages
= (vma_size
/ PAGE_SIZE
) - 1;
1884 * If we have data pages ensure they're a power-of-two number, so we
1885 * can do bitmasks instead of modulo.
1887 if (nr_pages
!= 0 && !is_power_of_2(nr_pages
))
1890 if (vma_size
!= PAGE_SIZE
* (1 + nr_pages
))
1893 if (vma
->vm_pgoff
!= 0)
1896 WARN_ON_ONCE(counter
->ctx
->parent_ctx
);
1897 mutex_lock(&counter
->mmap_mutex
);
1898 if (atomic_inc_not_zero(&counter
->mmap_count
)) {
1899 if (nr_pages
!= counter
->data
->nr_pages
)
1904 user_extra
= nr_pages
+ 1;
1905 user_lock_limit
= sysctl_perf_counter_mlock
>> (PAGE_SHIFT
- 10);
1908 * Increase the limit linearly with more CPUs:
1910 user_lock_limit
*= num_online_cpus();
1912 user_locked
= atomic_long_read(&user
->locked_vm
) + user_extra
;
1915 if (user_locked
> user_lock_limit
)
1916 extra
= user_locked
- user_lock_limit
;
1918 lock_limit
= current
->signal
->rlim
[RLIMIT_MEMLOCK
].rlim_cur
;
1919 lock_limit
>>= PAGE_SHIFT
;
1920 locked
= vma
->vm_mm
->locked_vm
+ extra
;
1922 if ((locked
> lock_limit
) && !capable(CAP_IPC_LOCK
)) {
1927 WARN_ON(counter
->data
);
1928 ret
= perf_mmap_data_alloc(counter
, nr_pages
);
1932 atomic_set(&counter
->mmap_count
, 1);
1933 atomic_long_add(user_extra
, &user
->locked_vm
);
1934 vma
->vm_mm
->locked_vm
+= extra
;
1935 counter
->data
->nr_locked
= extra
;
1937 mutex_unlock(&counter
->mmap_mutex
);
1939 vma
->vm_flags
&= ~VM_MAYWRITE
;
1940 vma
->vm_flags
|= VM_RESERVED
;
1941 vma
->vm_ops
= &perf_mmap_vmops
;
1946 static int perf_fasync(int fd
, struct file
*filp
, int on
)
1948 struct inode
*inode
= filp
->f_path
.dentry
->d_inode
;
1949 struct perf_counter
*counter
= filp
->private_data
;
1952 mutex_lock(&inode
->i_mutex
);
1953 retval
= fasync_helper(fd
, filp
, on
, &counter
->fasync
);
1954 mutex_unlock(&inode
->i_mutex
);
1962 static const struct file_operations perf_fops
= {
1963 .release
= perf_release
,
1966 .unlocked_ioctl
= perf_ioctl
,
1967 .compat_ioctl
= perf_ioctl
,
1969 .fasync
= perf_fasync
,
1973 * Perf counter wakeup
1975 * If there's data, ensure we set the poll() state and publish everything
1976 * to user-space before waking everybody up.
1979 void perf_counter_wakeup(struct perf_counter
*counter
)
1981 wake_up_all(&counter
->waitq
);
1983 if (counter
->pending_kill
) {
1984 kill_fasync(&counter
->fasync
, SIGIO
, counter
->pending_kill
);
1985 counter
->pending_kill
= 0;
1992 * Handle the case where we need to wakeup up from NMI (or rq->lock) context.
1994 * The NMI bit means we cannot possibly take locks. Therefore, maintain a
1995 * single linked list and use cmpxchg() to add entries lockless.
1998 static void perf_pending_counter(struct perf_pending_entry
*entry
)
2000 struct perf_counter
*counter
= container_of(entry
,
2001 struct perf_counter
, pending
);
2003 if (counter
->pending_disable
) {
2004 counter
->pending_disable
= 0;
2005 perf_counter_disable(counter
);
2008 if (counter
->pending_wakeup
) {
2009 counter
->pending_wakeup
= 0;
2010 perf_counter_wakeup(counter
);
2014 #define PENDING_TAIL ((struct perf_pending_entry *)-1UL)
2016 static DEFINE_PER_CPU(struct perf_pending_entry
*, perf_pending_head
) = {
2020 static void perf_pending_queue(struct perf_pending_entry
*entry
,
2021 void (*func
)(struct perf_pending_entry
*))
2023 struct perf_pending_entry
**head
;
2025 if (cmpxchg(&entry
->next
, NULL
, PENDING_TAIL
) != NULL
)
2030 head
= &get_cpu_var(perf_pending_head
);
2033 entry
->next
= *head
;
2034 } while (cmpxchg(head
, entry
->next
, entry
) != entry
->next
);
2036 set_perf_counter_pending();
2038 put_cpu_var(perf_pending_head
);
2041 static int __perf_pending_run(void)
2043 struct perf_pending_entry
*list
;
2046 list
= xchg(&__get_cpu_var(perf_pending_head
), PENDING_TAIL
);
2047 while (list
!= PENDING_TAIL
) {
2048 void (*func
)(struct perf_pending_entry
*);
2049 struct perf_pending_entry
*entry
= list
;
2056 * Ensure we observe the unqueue before we issue the wakeup,
2057 * so that we won't be waiting forever.
2058 * -- see perf_not_pending().
2069 static inline int perf_not_pending(struct perf_counter
*counter
)
2072 * If we flush on whatever cpu we run, there is a chance we don't
2076 __perf_pending_run();
2080 * Ensure we see the proper queue state before going to sleep
2081 * so that we do not miss the wakeup. -- see perf_pending_handle()
2084 return counter
->pending
.next
== NULL
;
2087 static void perf_pending_sync(struct perf_counter
*counter
)
2089 wait_event(counter
->waitq
, perf_not_pending(counter
));
2092 void perf_counter_do_pending(void)
2094 __perf_pending_run();
2098 * Callchain support -- arch specific
2101 __weak
struct perf_callchain_entry
*perf_callchain(struct pt_regs
*regs
)
2110 struct perf_output_handle
{
2111 struct perf_counter
*counter
;
2112 struct perf_mmap_data
*data
;
2114 unsigned long offset
;
2118 unsigned long flags
;
2121 static void perf_output_wakeup(struct perf_output_handle
*handle
)
2123 atomic_set(&handle
->data
->poll
, POLL_IN
);
2126 handle
->counter
->pending_wakeup
= 1;
2127 perf_pending_queue(&handle
->counter
->pending
,
2128 perf_pending_counter
);
2130 perf_counter_wakeup(handle
->counter
);
2134 * Curious locking construct.
2136 * We need to ensure a later event doesn't publish a head when a former
2137 * event isn't done writing. However since we need to deal with NMIs we
2138 * cannot fully serialize things.
2140 * What we do is serialize between CPUs so we only have to deal with NMI
2141 * nesting on a single CPU.
2143 * We only publish the head (and generate a wakeup) when the outer-most
2146 static void perf_output_lock(struct perf_output_handle
*handle
)
2148 struct perf_mmap_data
*data
= handle
->data
;
2153 local_irq_save(handle
->flags
);
2154 cpu
= smp_processor_id();
2156 if (in_nmi() && atomic_read(&data
->lock
) == cpu
)
2159 while (atomic_cmpxchg(&data
->lock
, -1, cpu
) != -1)
2165 static void perf_output_unlock(struct perf_output_handle
*handle
)
2167 struct perf_mmap_data
*data
= handle
->data
;
2171 data
->done_head
= data
->head
;
2173 if (!handle
->locked
)
2178 * The xchg implies a full barrier that ensures all writes are done
2179 * before we publish the new head, matched by a rmb() in userspace when
2180 * reading this position.
2182 while ((head
= atomic_long_xchg(&data
->done_head
, 0)))
2183 data
->user_page
->data_head
= head
;
2186 * NMI can happen here, which means we can miss a done_head update.
2189 cpu
= atomic_xchg(&data
->lock
, -1);
2190 WARN_ON_ONCE(cpu
!= smp_processor_id());
2193 * Therefore we have to validate we did not indeed do so.
2195 if (unlikely(atomic_long_read(&data
->done_head
))) {
2197 * Since we had it locked, we can lock it again.
2199 while (atomic_cmpxchg(&data
->lock
, -1, cpu
) != -1)
2205 if (atomic_xchg(&data
->wakeup
, 0))
2206 perf_output_wakeup(handle
);
2208 local_irq_restore(handle
->flags
);
2211 static int perf_output_begin(struct perf_output_handle
*handle
,
2212 struct perf_counter
*counter
, unsigned int size
,
2213 int nmi
, int overflow
)
2215 struct perf_mmap_data
*data
;
2216 unsigned int offset
, head
;
2219 * For inherited counters we send all the output towards the parent.
2221 if (counter
->parent
)
2222 counter
= counter
->parent
;
2225 data
= rcu_dereference(counter
->data
);
2229 handle
->data
= data
;
2230 handle
->counter
= counter
;
2232 handle
->overflow
= overflow
;
2234 if (!data
->nr_pages
)
2237 perf_output_lock(handle
);
2240 offset
= head
= atomic_read(&data
->head
);
2242 } while (atomic_long_cmpxchg(&data
->head
, offset
, head
) != offset
);
2244 handle
->offset
= offset
;
2245 handle
->head
= head
;
2247 if ((offset
>> PAGE_SHIFT
) != (head
>> PAGE_SHIFT
))
2248 atomic_set(&data
->wakeup
, 1);
2253 perf_output_wakeup(handle
);
2260 static void perf_output_copy(struct perf_output_handle
*handle
,
2261 void *buf
, unsigned int len
)
2263 unsigned int pages_mask
;
2264 unsigned int offset
;
2268 offset
= handle
->offset
;
2269 pages_mask
= handle
->data
->nr_pages
- 1;
2270 pages
= handle
->data
->data_pages
;
2273 unsigned int page_offset
;
2276 nr
= (offset
>> PAGE_SHIFT
) & pages_mask
;
2277 page_offset
= offset
& (PAGE_SIZE
- 1);
2278 size
= min_t(unsigned int, PAGE_SIZE
- page_offset
, len
);
2280 memcpy(pages
[nr
] + page_offset
, buf
, size
);
2287 handle
->offset
= offset
;
2290 * Check we didn't copy past our reservation window, taking the
2291 * possible unsigned int wrap into account.
2293 WARN_ON_ONCE(((long)(handle
->head
- handle
->offset
)) < 0);
2296 #define perf_output_put(handle, x) \
2297 perf_output_copy((handle), &(x), sizeof(x))
2299 static void perf_output_end(struct perf_output_handle
*handle
)
2301 struct perf_counter
*counter
= handle
->counter
;
2302 struct perf_mmap_data
*data
= handle
->data
;
2304 int wakeup_events
= counter
->attr
.wakeup_events
;
2306 if (handle
->overflow
&& wakeup_events
) {
2307 int events
= atomic_inc_return(&data
->events
);
2308 if (events
>= wakeup_events
) {
2309 atomic_sub(wakeup_events
, &data
->events
);
2310 atomic_set(&data
->wakeup
, 1);
2314 perf_output_unlock(handle
);
2318 static u32
perf_counter_pid(struct perf_counter
*counter
, struct task_struct
*p
)
2321 * only top level counters have the pid namespace they were created in
2323 if (counter
->parent
)
2324 counter
= counter
->parent
;
2326 return task_tgid_nr_ns(p
, counter
->ns
);
2329 static u32
perf_counter_tid(struct perf_counter
*counter
, struct task_struct
*p
)
2332 * only top level counters have the pid namespace they were created in
2334 if (counter
->parent
)
2335 counter
= counter
->parent
;
2337 return task_pid_nr_ns(p
, counter
->ns
);
2340 static void perf_counter_output(struct perf_counter
*counter
,
2341 int nmi
, struct pt_regs
*regs
, u64 addr
)
2344 u64 sample_type
= counter
->attr
.sample_type
;
2345 struct perf_output_handle handle
;
2346 struct perf_event_header header
;
2355 struct perf_callchain_entry
*callchain
= NULL
;
2356 int callchain_size
= 0;
2363 header
.size
= sizeof(header
);
2365 header
.misc
= PERF_EVENT_MISC_OVERFLOW
;
2366 header
.misc
|= perf_misc_flags(regs
);
2368 if (sample_type
& PERF_SAMPLE_IP
) {
2369 ip
= perf_instruction_pointer(regs
);
2370 header
.type
|= PERF_SAMPLE_IP
;
2371 header
.size
+= sizeof(ip
);
2374 if (sample_type
& PERF_SAMPLE_TID
) {
2375 /* namespace issues */
2376 tid_entry
.pid
= perf_counter_pid(counter
, current
);
2377 tid_entry
.tid
= perf_counter_tid(counter
, current
);
2379 header
.type
|= PERF_SAMPLE_TID
;
2380 header
.size
+= sizeof(tid_entry
);
2383 if (sample_type
& PERF_SAMPLE_TIME
) {
2385 * Maybe do better on x86 and provide cpu_clock_nmi()
2387 time
= sched_clock();
2389 header
.type
|= PERF_SAMPLE_TIME
;
2390 header
.size
+= sizeof(u64
);
2393 if (sample_type
& PERF_SAMPLE_ADDR
) {
2394 header
.type
|= PERF_SAMPLE_ADDR
;
2395 header
.size
+= sizeof(u64
);
2398 if (sample_type
& PERF_SAMPLE_CONFIG
) {
2399 header
.type
|= PERF_SAMPLE_CONFIG
;
2400 header
.size
+= sizeof(u64
);
2403 if (sample_type
& PERF_SAMPLE_CPU
) {
2404 header
.type
|= PERF_SAMPLE_CPU
;
2405 header
.size
+= sizeof(cpu_entry
);
2407 cpu_entry
.cpu
= raw_smp_processor_id();
2410 if (sample_type
& PERF_SAMPLE_GROUP
) {
2411 header
.type
|= PERF_SAMPLE_GROUP
;
2412 header
.size
+= sizeof(u64
) +
2413 counter
->nr_siblings
* sizeof(group_entry
);
2416 if (sample_type
& PERF_SAMPLE_CALLCHAIN
) {
2417 callchain
= perf_callchain(regs
);
2420 callchain_size
= (1 + callchain
->nr
) * sizeof(u64
);
2422 header
.type
|= PERF_SAMPLE_CALLCHAIN
;
2423 header
.size
+= callchain_size
;
2427 ret
= perf_output_begin(&handle
, counter
, header
.size
, nmi
, 1);
2431 perf_output_put(&handle
, header
);
2433 if (sample_type
& PERF_SAMPLE_IP
)
2434 perf_output_put(&handle
, ip
);
2436 if (sample_type
& PERF_SAMPLE_TID
)
2437 perf_output_put(&handle
, tid_entry
);
2439 if (sample_type
& PERF_SAMPLE_TIME
)
2440 perf_output_put(&handle
, time
);
2442 if (sample_type
& PERF_SAMPLE_ADDR
)
2443 perf_output_put(&handle
, addr
);
2445 if (sample_type
& PERF_SAMPLE_CONFIG
)
2446 perf_output_put(&handle
, counter
->attr
.config
);
2448 if (sample_type
& PERF_SAMPLE_CPU
)
2449 perf_output_put(&handle
, cpu_entry
);
2452 * XXX PERF_SAMPLE_GROUP vs inherited counters seems difficult.
2454 if (sample_type
& PERF_SAMPLE_GROUP
) {
2455 struct perf_counter
*leader
, *sub
;
2456 u64 nr
= counter
->nr_siblings
;
2458 perf_output_put(&handle
, nr
);
2460 leader
= counter
->group_leader
;
2461 list_for_each_entry(sub
, &leader
->sibling_list
, list_entry
) {
2463 sub
->pmu
->read(sub
);
2465 group_entry
.id
= sub
->id
;
2466 group_entry
.counter
= atomic64_read(&sub
->count
);
2468 perf_output_put(&handle
, group_entry
);
2473 perf_output_copy(&handle
, callchain
, callchain_size
);
2475 perf_output_end(&handle
);
2482 struct perf_fork_event
{
2483 struct task_struct
*task
;
2486 struct perf_event_header header
;
2493 static void perf_counter_fork_output(struct perf_counter
*counter
,
2494 struct perf_fork_event
*fork_event
)
2496 struct perf_output_handle handle
;
2497 int size
= fork_event
->event
.header
.size
;
2498 struct task_struct
*task
= fork_event
->task
;
2499 int ret
= perf_output_begin(&handle
, counter
, size
, 0, 0);
2504 fork_event
->event
.pid
= perf_counter_pid(counter
, task
);
2505 fork_event
->event
.ppid
= perf_counter_pid(counter
, task
->real_parent
);
2507 perf_output_put(&handle
, fork_event
->event
);
2508 perf_output_end(&handle
);
2511 static int perf_counter_fork_match(struct perf_counter
*counter
)
2513 if (counter
->attr
.comm
|| counter
->attr
.mmap
|| counter
->attr
.munmap
)
2519 static void perf_counter_fork_ctx(struct perf_counter_context
*ctx
,
2520 struct perf_fork_event
*fork_event
)
2522 struct perf_counter
*counter
;
2524 if (system_state
!= SYSTEM_RUNNING
|| list_empty(&ctx
->event_list
))
2528 list_for_each_entry_rcu(counter
, &ctx
->event_list
, event_entry
) {
2529 if (perf_counter_fork_match(counter
))
2530 perf_counter_fork_output(counter
, fork_event
);
2535 static void perf_counter_fork_event(struct perf_fork_event
*fork_event
)
2537 struct perf_cpu_context
*cpuctx
;
2538 struct perf_counter_context
*ctx
;
2540 cpuctx
= &get_cpu_var(perf_cpu_context
);
2541 perf_counter_fork_ctx(&cpuctx
->ctx
, fork_event
);
2542 put_cpu_var(perf_cpu_context
);
2546 * doesn't really matter which of the child contexts the
2547 * events ends up in.
2549 ctx
= rcu_dereference(current
->perf_counter_ctxp
);
2551 perf_counter_fork_ctx(ctx
, fork_event
);
2555 void perf_counter_fork(struct task_struct
*task
)
2557 struct perf_fork_event fork_event
;
2559 if (!atomic_read(&nr_comm_counters
) &&
2560 !atomic_read(&nr_mmap_counters
) &&
2561 !atomic_read(&nr_munmap_counters
))
2564 fork_event
= (struct perf_fork_event
){
2568 .type
= PERF_EVENT_FORK
,
2569 .size
= sizeof(fork_event
.event
),
2574 perf_counter_fork_event(&fork_event
);
2581 struct perf_comm_event
{
2582 struct task_struct
*task
;
2587 struct perf_event_header header
;
2594 static void perf_counter_comm_output(struct perf_counter
*counter
,
2595 struct perf_comm_event
*comm_event
)
2597 struct perf_output_handle handle
;
2598 int size
= comm_event
->event
.header
.size
;
2599 int ret
= perf_output_begin(&handle
, counter
, size
, 0, 0);
2604 comm_event
->event
.pid
= perf_counter_pid(counter
, comm_event
->task
);
2605 comm_event
->event
.tid
= perf_counter_tid(counter
, comm_event
->task
);
2607 perf_output_put(&handle
, comm_event
->event
);
2608 perf_output_copy(&handle
, comm_event
->comm
,
2609 comm_event
->comm_size
);
2610 perf_output_end(&handle
);
2613 static int perf_counter_comm_match(struct perf_counter
*counter
)
2615 if (counter
->attr
.comm
)
2621 static void perf_counter_comm_ctx(struct perf_counter_context
*ctx
,
2622 struct perf_comm_event
*comm_event
)
2624 struct perf_counter
*counter
;
2626 if (system_state
!= SYSTEM_RUNNING
|| list_empty(&ctx
->event_list
))
2630 list_for_each_entry_rcu(counter
, &ctx
->event_list
, event_entry
) {
2631 if (perf_counter_comm_match(counter
))
2632 perf_counter_comm_output(counter
, comm_event
);
2637 static void perf_counter_comm_event(struct perf_comm_event
*comm_event
)
2639 struct perf_cpu_context
*cpuctx
;
2640 struct perf_counter_context
*ctx
;
2642 char *comm
= comm_event
->task
->comm
;
2644 size
= ALIGN(strlen(comm
)+1, sizeof(u64
));
2646 comm_event
->comm
= comm
;
2647 comm_event
->comm_size
= size
;
2649 comm_event
->event
.header
.size
= sizeof(comm_event
->event
) + size
;
2651 cpuctx
= &get_cpu_var(perf_cpu_context
);
2652 perf_counter_comm_ctx(&cpuctx
->ctx
, comm_event
);
2653 put_cpu_var(perf_cpu_context
);
2657 * doesn't really matter which of the child contexts the
2658 * events ends up in.
2660 ctx
= rcu_dereference(current
->perf_counter_ctxp
);
2662 perf_counter_comm_ctx(ctx
, comm_event
);
2666 void perf_counter_comm(struct task_struct
*task
)
2668 struct perf_comm_event comm_event
;
2670 if (!atomic_read(&nr_comm_counters
))
2673 comm_event
= (struct perf_comm_event
){
2676 .header
= { .type
= PERF_EVENT_COMM
, },
2680 perf_counter_comm_event(&comm_event
);
2687 struct perf_mmap_event
{
2693 struct perf_event_header header
;
2703 static void perf_counter_mmap_output(struct perf_counter
*counter
,
2704 struct perf_mmap_event
*mmap_event
)
2706 struct perf_output_handle handle
;
2707 int size
= mmap_event
->event
.header
.size
;
2708 int ret
= perf_output_begin(&handle
, counter
, size
, 0, 0);
2713 mmap_event
->event
.pid
= perf_counter_pid(counter
, current
);
2714 mmap_event
->event
.tid
= perf_counter_tid(counter
, current
);
2716 perf_output_put(&handle
, mmap_event
->event
);
2717 perf_output_copy(&handle
, mmap_event
->file_name
,
2718 mmap_event
->file_size
);
2719 perf_output_end(&handle
);
2722 static int perf_counter_mmap_match(struct perf_counter
*counter
,
2723 struct perf_mmap_event
*mmap_event
)
2725 if (counter
->attr
.mmap
&&
2726 mmap_event
->event
.header
.type
== PERF_EVENT_MMAP
)
2729 if (counter
->attr
.munmap
&&
2730 mmap_event
->event
.header
.type
== PERF_EVENT_MUNMAP
)
2736 static void perf_counter_mmap_ctx(struct perf_counter_context
*ctx
,
2737 struct perf_mmap_event
*mmap_event
)
2739 struct perf_counter
*counter
;
2741 if (system_state
!= SYSTEM_RUNNING
|| list_empty(&ctx
->event_list
))
2745 list_for_each_entry_rcu(counter
, &ctx
->event_list
, event_entry
) {
2746 if (perf_counter_mmap_match(counter
, mmap_event
))
2747 perf_counter_mmap_output(counter
, mmap_event
);
2752 static void perf_counter_mmap_event(struct perf_mmap_event
*mmap_event
)
2754 struct perf_cpu_context
*cpuctx
;
2755 struct perf_counter_context
*ctx
;
2756 struct file
*file
= mmap_event
->file
;
2763 buf
= kzalloc(PATH_MAX
, GFP_KERNEL
);
2765 name
= strncpy(tmp
, "//enomem", sizeof(tmp
));
2768 name
= d_path(&file
->f_path
, buf
, PATH_MAX
);
2770 name
= strncpy(tmp
, "//toolong", sizeof(tmp
));
2774 name
= strncpy(tmp
, "//anon", sizeof(tmp
));
2779 size
= ALIGN(strlen(name
)+1, sizeof(u64
));
2781 mmap_event
->file_name
= name
;
2782 mmap_event
->file_size
= size
;
2784 mmap_event
->event
.header
.size
= sizeof(mmap_event
->event
) + size
;
2786 cpuctx
= &get_cpu_var(perf_cpu_context
);
2787 perf_counter_mmap_ctx(&cpuctx
->ctx
, mmap_event
);
2788 put_cpu_var(perf_cpu_context
);
2792 * doesn't really matter which of the child contexts the
2793 * events ends up in.
2795 ctx
= rcu_dereference(current
->perf_counter_ctxp
);
2797 perf_counter_mmap_ctx(ctx
, mmap_event
);
2803 void perf_counter_mmap(unsigned long addr
, unsigned long len
,
2804 unsigned long pgoff
, struct file
*file
)
2806 struct perf_mmap_event mmap_event
;
2808 if (!atomic_read(&nr_mmap_counters
))
2811 mmap_event
= (struct perf_mmap_event
){
2814 .header
= { .type
= PERF_EVENT_MMAP
, },
2821 perf_counter_mmap_event(&mmap_event
);
2824 void perf_counter_munmap(unsigned long addr
, unsigned long len
,
2825 unsigned long pgoff
, struct file
*file
)
2827 struct perf_mmap_event mmap_event
;
2829 if (!atomic_read(&nr_munmap_counters
))
2832 mmap_event
= (struct perf_mmap_event
){
2835 .header
= { .type
= PERF_EVENT_MUNMAP
, },
2842 perf_counter_mmap_event(&mmap_event
);
2846 * Log sample_period changes so that analyzing tools can re-normalize the
2850 static void perf_log_period(struct perf_counter
*counter
, u64 period
)
2852 struct perf_output_handle handle
;
2856 struct perf_event_header header
;
2861 .type
= PERF_EVENT_PERIOD
,
2863 .size
= sizeof(freq_event
),
2865 .time
= sched_clock(),
2869 if (counter
->hw
.sample_period
== period
)
2872 ret
= perf_output_begin(&handle
, counter
, sizeof(freq_event
), 0, 0);
2876 perf_output_put(&handle
, freq_event
);
2877 perf_output_end(&handle
);
2881 * IRQ throttle logging
2884 static void perf_log_throttle(struct perf_counter
*counter
, int enable
)
2886 struct perf_output_handle handle
;
2890 struct perf_event_header header
;
2892 } throttle_event
= {
2894 .type
= PERF_EVENT_THROTTLE
+ 1,
2896 .size
= sizeof(throttle_event
),
2898 .time
= sched_clock(),
2901 ret
= perf_output_begin(&handle
, counter
, sizeof(throttle_event
), 1, 0);
2905 perf_output_put(&handle
, throttle_event
);
2906 perf_output_end(&handle
);
2910 * Generic counter overflow handling.
2913 int perf_counter_overflow(struct perf_counter
*counter
,
2914 int nmi
, struct pt_regs
*regs
, u64 addr
)
2916 int events
= atomic_read(&counter
->event_limit
);
2917 int throttle
= counter
->pmu
->unthrottle
!= NULL
;
2921 counter
->hw
.interrupts
++;
2923 if (counter
->hw
.interrupts
!= MAX_INTERRUPTS
) {
2924 counter
->hw
.interrupts
++;
2925 if (HZ
*counter
->hw
.interrupts
> (u64
)sysctl_perf_counter_limit
) {
2926 counter
->hw
.interrupts
= MAX_INTERRUPTS
;
2927 perf_log_throttle(counter
, 0);
2932 * Keep re-disabling counters even though on the previous
2933 * pass we disabled it - just in case we raced with a
2934 * sched-in and the counter got enabled again:
2941 * XXX event_limit might not quite work as expected on inherited
2945 counter
->pending_kill
= POLL_IN
;
2946 if (events
&& atomic_dec_and_test(&counter
->event_limit
)) {
2948 counter
->pending_kill
= POLL_HUP
;
2950 counter
->pending_disable
= 1;
2951 perf_pending_queue(&counter
->pending
,
2952 perf_pending_counter
);
2954 perf_counter_disable(counter
);
2957 perf_counter_output(counter
, nmi
, regs
, addr
);
2962 * Generic software counter infrastructure
2965 static void perf_swcounter_update(struct perf_counter
*counter
)
2967 struct hw_perf_counter
*hwc
= &counter
->hw
;
2972 prev
= atomic64_read(&hwc
->prev_count
);
2973 now
= atomic64_read(&hwc
->count
);
2974 if (atomic64_cmpxchg(&hwc
->prev_count
, prev
, now
) != prev
)
2979 atomic64_add(delta
, &counter
->count
);
2980 atomic64_sub(delta
, &hwc
->period_left
);
2983 static void perf_swcounter_set_period(struct perf_counter
*counter
)
2985 struct hw_perf_counter
*hwc
= &counter
->hw
;
2986 s64 left
= atomic64_read(&hwc
->period_left
);
2987 s64 period
= hwc
->sample_period
;
2989 if (unlikely(left
<= -period
)) {
2991 atomic64_set(&hwc
->period_left
, left
);
2994 if (unlikely(left
<= 0)) {
2996 atomic64_add(period
, &hwc
->period_left
);
2999 atomic64_set(&hwc
->prev_count
, -left
);
3000 atomic64_set(&hwc
->count
, -left
);
3003 static enum hrtimer_restart
perf_swcounter_hrtimer(struct hrtimer
*hrtimer
)
3005 enum hrtimer_restart ret
= HRTIMER_RESTART
;
3006 struct perf_counter
*counter
;
3007 struct pt_regs
*regs
;
3010 counter
= container_of(hrtimer
, struct perf_counter
, hw
.hrtimer
);
3011 counter
->pmu
->read(counter
);
3013 regs
= get_irq_regs();
3015 * In case we exclude kernel IPs or are somehow not in interrupt
3016 * context, provide the next best thing, the user IP.
3018 if ((counter
->attr
.exclude_kernel
|| !regs
) &&
3019 !counter
->attr
.exclude_user
)
3020 regs
= task_pt_regs(current
);
3023 if (perf_counter_overflow(counter
, 0, regs
, 0))
3024 ret
= HRTIMER_NORESTART
;
3027 period
= max_t(u64
, 10000, counter
->hw
.sample_period
);
3028 hrtimer_forward_now(hrtimer
, ns_to_ktime(period
));
3033 static void perf_swcounter_overflow(struct perf_counter
*counter
,
3034 int nmi
, struct pt_regs
*regs
, u64 addr
)
3036 perf_swcounter_update(counter
);
3037 perf_swcounter_set_period(counter
);
3038 if (perf_counter_overflow(counter
, nmi
, regs
, addr
))
3039 /* soft-disable the counter */
3044 static int perf_swcounter_is_counting(struct perf_counter
*counter
)
3046 struct perf_counter_context
*ctx
;
3047 unsigned long flags
;
3050 if (counter
->state
== PERF_COUNTER_STATE_ACTIVE
)
3053 if (counter
->state
!= PERF_COUNTER_STATE_INACTIVE
)
3057 * If the counter is inactive, it could be just because
3058 * its task is scheduled out, or because it's in a group
3059 * which could not go on the PMU. We want to count in
3060 * the first case but not the second. If the context is
3061 * currently active then an inactive software counter must
3062 * be the second case. If it's not currently active then
3063 * we need to know whether the counter was active when the
3064 * context was last active, which we can determine by
3065 * comparing counter->tstamp_stopped with ctx->time.
3067 * We are within an RCU read-side critical section,
3068 * which protects the existence of *ctx.
3071 spin_lock_irqsave(&ctx
->lock
, flags
);
3073 /* Re-check state now we have the lock */
3074 if (counter
->state
< PERF_COUNTER_STATE_INACTIVE
||
3075 counter
->ctx
->is_active
||
3076 counter
->tstamp_stopped
< ctx
->time
)
3078 spin_unlock_irqrestore(&ctx
->lock
, flags
);
3082 static int perf_swcounter_match(struct perf_counter
*counter
,
3083 enum perf_event_types type
,
3084 u32 event
, struct pt_regs
*regs
)
3088 event_config
= ((u64
) type
<< PERF_COUNTER_TYPE_SHIFT
) | event
;
3090 if (!perf_swcounter_is_counting(counter
))
3093 if (counter
->attr
.config
!= event_config
)
3097 if (counter
->attr
.exclude_user
&& user_mode(regs
))
3100 if (counter
->attr
.exclude_kernel
&& !user_mode(regs
))
3107 static void perf_swcounter_add(struct perf_counter
*counter
, u64 nr
,
3108 int nmi
, struct pt_regs
*regs
, u64 addr
)
3110 int neg
= atomic64_add_negative(nr
, &counter
->hw
.count
);
3112 if (counter
->hw
.sample_period
&& !neg
&& regs
)
3113 perf_swcounter_overflow(counter
, nmi
, regs
, addr
);
3116 static void perf_swcounter_ctx_event(struct perf_counter_context
*ctx
,
3117 enum perf_event_types type
, u32 event
,
3118 u64 nr
, int nmi
, struct pt_regs
*regs
,
3121 struct perf_counter
*counter
;
3123 if (system_state
!= SYSTEM_RUNNING
|| list_empty(&ctx
->event_list
))
3127 list_for_each_entry_rcu(counter
, &ctx
->event_list
, event_entry
) {
3128 if (perf_swcounter_match(counter
, type
, event
, regs
))
3129 perf_swcounter_add(counter
, nr
, nmi
, regs
, addr
);
3134 static int *perf_swcounter_recursion_context(struct perf_cpu_context
*cpuctx
)
3137 return &cpuctx
->recursion
[3];
3140 return &cpuctx
->recursion
[2];
3143 return &cpuctx
->recursion
[1];
3145 return &cpuctx
->recursion
[0];
3148 static void __perf_swcounter_event(enum perf_event_types type
, u32 event
,
3149 u64 nr
, int nmi
, struct pt_regs
*regs
,
3152 struct perf_cpu_context
*cpuctx
= &get_cpu_var(perf_cpu_context
);
3153 int *recursion
= perf_swcounter_recursion_context(cpuctx
);
3154 struct perf_counter_context
*ctx
;
3162 perf_swcounter_ctx_event(&cpuctx
->ctx
, type
, event
,
3163 nr
, nmi
, regs
, addr
);
3166 * doesn't really matter which of the child contexts the
3167 * events ends up in.
3169 ctx
= rcu_dereference(current
->perf_counter_ctxp
);
3171 perf_swcounter_ctx_event(ctx
, type
, event
, nr
, nmi
, regs
, addr
);
3178 put_cpu_var(perf_cpu_context
);
3182 perf_swcounter_event(u32 event
, u64 nr
, int nmi
, struct pt_regs
*regs
, u64 addr
)
3184 __perf_swcounter_event(PERF_TYPE_SOFTWARE
, event
, nr
, nmi
, regs
, addr
);
3187 static void perf_swcounter_read(struct perf_counter
*counter
)
3189 perf_swcounter_update(counter
);
3192 static int perf_swcounter_enable(struct perf_counter
*counter
)
3194 perf_swcounter_set_period(counter
);
3198 static void perf_swcounter_disable(struct perf_counter
*counter
)
3200 perf_swcounter_update(counter
);
3203 static const struct pmu perf_ops_generic
= {
3204 .enable
= perf_swcounter_enable
,
3205 .disable
= perf_swcounter_disable
,
3206 .read
= perf_swcounter_read
,
3210 * Software counter: cpu wall time clock
3213 static void cpu_clock_perf_counter_update(struct perf_counter
*counter
)
3215 int cpu
= raw_smp_processor_id();
3219 now
= cpu_clock(cpu
);
3220 prev
= atomic64_read(&counter
->hw
.prev_count
);
3221 atomic64_set(&counter
->hw
.prev_count
, now
);
3222 atomic64_add(now
- prev
, &counter
->count
);
3225 static int cpu_clock_perf_counter_enable(struct perf_counter
*counter
)
3227 struct hw_perf_counter
*hwc
= &counter
->hw
;
3228 int cpu
= raw_smp_processor_id();
3230 atomic64_set(&hwc
->prev_count
, cpu_clock(cpu
));
3231 hrtimer_init(&hwc
->hrtimer
, CLOCK_MONOTONIC
, HRTIMER_MODE_REL
);
3232 hwc
->hrtimer
.function
= perf_swcounter_hrtimer
;
3233 if (hwc
->sample_period
) {
3234 u64 period
= max_t(u64
, 10000, hwc
->sample_period
);
3235 __hrtimer_start_range_ns(&hwc
->hrtimer
,
3236 ns_to_ktime(period
), 0,
3237 HRTIMER_MODE_REL
, 0);
3243 static void cpu_clock_perf_counter_disable(struct perf_counter
*counter
)
3245 if (counter
->hw
.sample_period
)
3246 hrtimer_cancel(&counter
->hw
.hrtimer
);
3247 cpu_clock_perf_counter_update(counter
);
3250 static void cpu_clock_perf_counter_read(struct perf_counter
*counter
)
3252 cpu_clock_perf_counter_update(counter
);
3255 static const struct pmu perf_ops_cpu_clock
= {
3256 .enable
= cpu_clock_perf_counter_enable
,
3257 .disable
= cpu_clock_perf_counter_disable
,
3258 .read
= cpu_clock_perf_counter_read
,
3262 * Software counter: task time clock
3265 static void task_clock_perf_counter_update(struct perf_counter
*counter
, u64 now
)
3270 prev
= atomic64_xchg(&counter
->hw
.prev_count
, now
);
3272 atomic64_add(delta
, &counter
->count
);
3275 static int task_clock_perf_counter_enable(struct perf_counter
*counter
)
3277 struct hw_perf_counter
*hwc
= &counter
->hw
;
3280 now
= counter
->ctx
->time
;
3282 atomic64_set(&hwc
->prev_count
, now
);
3283 hrtimer_init(&hwc
->hrtimer
, CLOCK_MONOTONIC
, HRTIMER_MODE_REL
);
3284 hwc
->hrtimer
.function
= perf_swcounter_hrtimer
;
3285 if (hwc
->sample_period
) {
3286 u64 period
= max_t(u64
, 10000, hwc
->sample_period
);
3287 __hrtimer_start_range_ns(&hwc
->hrtimer
,
3288 ns_to_ktime(period
), 0,
3289 HRTIMER_MODE_REL
, 0);
3295 static void task_clock_perf_counter_disable(struct perf_counter
*counter
)
3297 if (counter
->hw
.sample_period
)
3298 hrtimer_cancel(&counter
->hw
.hrtimer
);
3299 task_clock_perf_counter_update(counter
, counter
->ctx
->time
);
3303 static void task_clock_perf_counter_read(struct perf_counter
*counter
)
3308 update_context_time(counter
->ctx
);
3309 time
= counter
->ctx
->time
;
3311 u64 now
= perf_clock();
3312 u64 delta
= now
- counter
->ctx
->timestamp
;
3313 time
= counter
->ctx
->time
+ delta
;
3316 task_clock_perf_counter_update(counter
, time
);
3319 static const struct pmu perf_ops_task_clock
= {
3320 .enable
= task_clock_perf_counter_enable
,
3321 .disable
= task_clock_perf_counter_disable
,
3322 .read
= task_clock_perf_counter_read
,
3326 * Software counter: cpu migrations
3328 void perf_counter_task_migration(struct task_struct
*task
, int cpu
)
3330 struct perf_cpu_context
*cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
3331 struct perf_counter_context
*ctx
;
3333 perf_swcounter_ctx_event(&cpuctx
->ctx
, PERF_TYPE_SOFTWARE
,
3334 PERF_COUNT_CPU_MIGRATIONS
,
3337 ctx
= perf_pin_task_context(task
);
3339 perf_swcounter_ctx_event(ctx
, PERF_TYPE_SOFTWARE
,
3340 PERF_COUNT_CPU_MIGRATIONS
,
3342 perf_unpin_context(ctx
);
3346 #ifdef CONFIG_EVENT_PROFILE
3347 void perf_tpcounter_event(int event_id
)
3349 struct pt_regs
*regs
= get_irq_regs();
3352 regs
= task_pt_regs(current
);
3354 __perf_swcounter_event(PERF_TYPE_TRACEPOINT
, event_id
, 1, 1, regs
, 0);
3356 EXPORT_SYMBOL_GPL(perf_tpcounter_event
);
3358 extern int ftrace_profile_enable(int);
3359 extern void ftrace_profile_disable(int);
3361 static void tp_perf_counter_destroy(struct perf_counter
*counter
)
3363 ftrace_profile_disable(perf_event_id(&counter
->attr
));
3366 static const struct pmu
*tp_perf_counter_init(struct perf_counter
*counter
)
3368 int event_id
= perf_event_id(&counter
->attr
);
3371 ret
= ftrace_profile_enable(event_id
);
3375 counter
->destroy
= tp_perf_counter_destroy
;
3376 counter
->hw
.sample_period
= counter
->attr
.sample_period
;
3378 return &perf_ops_generic
;
3381 static const struct pmu
*tp_perf_counter_init(struct perf_counter
*counter
)
3387 static const struct pmu
*sw_perf_counter_init(struct perf_counter
*counter
)
3389 const struct pmu
*pmu
= NULL
;
3392 * Software counters (currently) can't in general distinguish
3393 * between user, kernel and hypervisor events.
3394 * However, context switches and cpu migrations are considered
3395 * to be kernel events, and page faults are never hypervisor
3398 switch (perf_event_id(&counter
->attr
)) {
3399 case PERF_COUNT_CPU_CLOCK
:
3400 pmu
= &perf_ops_cpu_clock
;
3403 case PERF_COUNT_TASK_CLOCK
:
3405 * If the user instantiates this as a per-cpu counter,
3406 * use the cpu_clock counter instead.
3408 if (counter
->ctx
->task
)
3409 pmu
= &perf_ops_task_clock
;
3411 pmu
= &perf_ops_cpu_clock
;
3414 case PERF_COUNT_PAGE_FAULTS
:
3415 case PERF_COUNT_PAGE_FAULTS_MIN
:
3416 case PERF_COUNT_PAGE_FAULTS_MAJ
:
3417 case PERF_COUNT_CONTEXT_SWITCHES
:
3418 case PERF_COUNT_CPU_MIGRATIONS
:
3419 pmu
= &perf_ops_generic
;
3427 * Allocate and initialize a counter structure
3429 static struct perf_counter
*
3430 perf_counter_alloc(struct perf_counter_attr
*attr
,
3432 struct perf_counter_context
*ctx
,
3433 struct perf_counter
*group_leader
,
3436 const struct pmu
*pmu
;
3437 struct perf_counter
*counter
;
3438 struct hw_perf_counter
*hwc
;
3441 counter
= kzalloc(sizeof(*counter
), gfpflags
);
3443 return ERR_PTR(-ENOMEM
);
3446 * Single counters are their own group leaders, with an
3447 * empty sibling list:
3450 group_leader
= counter
;
3452 mutex_init(&counter
->child_mutex
);
3453 INIT_LIST_HEAD(&counter
->child_list
);
3455 INIT_LIST_HEAD(&counter
->list_entry
);
3456 INIT_LIST_HEAD(&counter
->event_entry
);
3457 INIT_LIST_HEAD(&counter
->sibling_list
);
3458 init_waitqueue_head(&counter
->waitq
);
3460 mutex_init(&counter
->mmap_mutex
);
3463 counter
->attr
= *attr
;
3464 counter
->group_leader
= group_leader
;
3465 counter
->pmu
= NULL
;
3467 counter
->oncpu
= -1;
3469 counter
->ns
= get_pid_ns(current
->nsproxy
->pid_ns
);
3470 counter
->id
= atomic64_inc_return(&perf_counter_id
);
3472 counter
->state
= PERF_COUNTER_STATE_INACTIVE
;
3475 counter
->state
= PERF_COUNTER_STATE_OFF
;
3480 if (attr
->freq
&& attr
->sample_freq
)
3481 hwc
->sample_period
= div64_u64(TICK_NSEC
, attr
->sample_freq
);
3483 hwc
->sample_period
= attr
->sample_period
;
3486 * we currently do not support PERF_SAMPLE_GROUP on inherited counters
3488 if (attr
->inherit
&& (attr
->sample_type
& PERF_SAMPLE_GROUP
))
3491 if (perf_event_raw(attr
)) {
3492 pmu
= hw_perf_counter_init(counter
);
3496 switch (perf_event_type(attr
)) {
3497 case PERF_TYPE_HARDWARE
:
3498 pmu
= hw_perf_counter_init(counter
);
3501 case PERF_TYPE_SOFTWARE
:
3502 pmu
= sw_perf_counter_init(counter
);
3505 case PERF_TYPE_TRACEPOINT
:
3506 pmu
= tp_perf_counter_init(counter
);
3513 else if (IS_ERR(pmu
))
3518 put_pid_ns(counter
->ns
);
3520 return ERR_PTR(err
);
3525 atomic_inc(&nr_counters
);
3526 if (counter
->attr
.mmap
)
3527 atomic_inc(&nr_mmap_counters
);
3528 if (counter
->attr
.munmap
)
3529 atomic_inc(&nr_munmap_counters
);
3530 if (counter
->attr
.comm
)
3531 atomic_inc(&nr_comm_counters
);
3537 * sys_perf_counter_open - open a performance counter, associate it to a task/cpu
3539 * @attr_uptr: event type attributes for monitoring/sampling
3542 * @group_fd: group leader counter fd
3544 SYSCALL_DEFINE5(perf_counter_open
,
3545 const struct perf_counter_attr __user
*, attr_uptr
,
3546 pid_t
, pid
, int, cpu
, int, group_fd
, unsigned long, flags
)
3548 struct perf_counter
*counter
, *group_leader
;
3549 struct perf_counter_attr attr
;
3550 struct perf_counter_context
*ctx
;
3551 struct file
*counter_file
= NULL
;
3552 struct file
*group_file
= NULL
;
3553 int fput_needed
= 0;
3554 int fput_needed2
= 0;
3557 /* for future expandability... */
3561 if (copy_from_user(&attr
, attr_uptr
, sizeof(attr
)) != 0)
3565 * Get the target context (task or percpu):
3567 ctx
= find_get_context(pid
, cpu
);
3569 return PTR_ERR(ctx
);
3572 * Look up the group leader (we will attach this counter to it):
3574 group_leader
= NULL
;
3575 if (group_fd
!= -1) {
3577 group_file
= fget_light(group_fd
, &fput_needed
);
3579 goto err_put_context
;
3580 if (group_file
->f_op
!= &perf_fops
)
3581 goto err_put_context
;
3583 group_leader
= group_file
->private_data
;
3585 * Do not allow a recursive hierarchy (this new sibling
3586 * becoming part of another group-sibling):
3588 if (group_leader
->group_leader
!= group_leader
)
3589 goto err_put_context
;
3591 * Do not allow to attach to a group in a different
3592 * task or CPU context:
3594 if (group_leader
->ctx
!= ctx
)
3595 goto err_put_context
;
3597 * Only a group leader can be exclusive or pinned
3599 if (attr
.exclusive
|| attr
.pinned
)
3600 goto err_put_context
;
3603 counter
= perf_counter_alloc(&attr
, cpu
, ctx
, group_leader
,
3605 ret
= PTR_ERR(counter
);
3606 if (IS_ERR(counter
))
3607 goto err_put_context
;
3609 ret
= anon_inode_getfd("[perf_counter]", &perf_fops
, counter
, 0);
3611 goto err_free_put_context
;
3613 counter_file
= fget_light(ret
, &fput_needed2
);
3615 goto err_free_put_context
;
3617 counter
->filp
= counter_file
;
3618 WARN_ON_ONCE(ctx
->parent_ctx
);
3619 mutex_lock(&ctx
->mutex
);
3620 perf_install_in_context(ctx
, counter
, cpu
);
3622 mutex_unlock(&ctx
->mutex
);
3624 counter
->owner
= current
;
3625 get_task_struct(current
);
3626 mutex_lock(¤t
->perf_counter_mutex
);
3627 list_add_tail(&counter
->owner_entry
, ¤t
->perf_counter_list
);
3628 mutex_unlock(¤t
->perf_counter_mutex
);
3630 fput_light(counter_file
, fput_needed2
);
3633 fput_light(group_file
, fput_needed
);
3637 err_free_put_context
:
3647 * inherit a counter from parent task to child task:
3649 static struct perf_counter
*
3650 inherit_counter(struct perf_counter
*parent_counter
,
3651 struct task_struct
*parent
,
3652 struct perf_counter_context
*parent_ctx
,
3653 struct task_struct
*child
,
3654 struct perf_counter
*group_leader
,
3655 struct perf_counter_context
*child_ctx
)
3657 struct perf_counter
*child_counter
;
3660 * Instead of creating recursive hierarchies of counters,
3661 * we link inherited counters back to the original parent,
3662 * which has a filp for sure, which we use as the reference
3665 if (parent_counter
->parent
)
3666 parent_counter
= parent_counter
->parent
;
3668 child_counter
= perf_counter_alloc(&parent_counter
->attr
,
3669 parent_counter
->cpu
, child_ctx
,
3670 group_leader
, GFP_KERNEL
);
3671 if (IS_ERR(child_counter
))
3672 return child_counter
;
3676 * Make the child state follow the state of the parent counter,
3677 * not its attr.disabled bit. We hold the parent's mutex,
3678 * so we won't race with perf_counter_{en, dis}able_family.
3680 if (parent_counter
->state
>= PERF_COUNTER_STATE_INACTIVE
)
3681 child_counter
->state
= PERF_COUNTER_STATE_INACTIVE
;
3683 child_counter
->state
= PERF_COUNTER_STATE_OFF
;
3686 * Link it up in the child's context:
3688 add_counter_to_ctx(child_counter
, child_ctx
);
3690 child_counter
->parent
= parent_counter
;
3692 * inherit into child's child as well:
3694 child_counter
->attr
.inherit
= 1;
3697 * Get a reference to the parent filp - we will fput it
3698 * when the child counter exits. This is safe to do because
3699 * we are in the parent and we know that the filp still
3700 * exists and has a nonzero count:
3702 atomic_long_inc(&parent_counter
->filp
->f_count
);
3705 * Link this into the parent counter's child list
3707 WARN_ON_ONCE(parent_counter
->ctx
->parent_ctx
);
3708 mutex_lock(&parent_counter
->child_mutex
);
3709 list_add_tail(&child_counter
->child_list
, &parent_counter
->child_list
);
3710 mutex_unlock(&parent_counter
->child_mutex
);
3712 return child_counter
;
3715 static int inherit_group(struct perf_counter
*parent_counter
,
3716 struct task_struct
*parent
,
3717 struct perf_counter_context
*parent_ctx
,
3718 struct task_struct
*child
,
3719 struct perf_counter_context
*child_ctx
)
3721 struct perf_counter
*leader
;
3722 struct perf_counter
*sub
;
3723 struct perf_counter
*child_ctr
;
3725 leader
= inherit_counter(parent_counter
, parent
, parent_ctx
,
3726 child
, NULL
, child_ctx
);
3728 return PTR_ERR(leader
);
3729 list_for_each_entry(sub
, &parent_counter
->sibling_list
, list_entry
) {
3730 child_ctr
= inherit_counter(sub
, parent
, parent_ctx
,
3731 child
, leader
, child_ctx
);
3732 if (IS_ERR(child_ctr
))
3733 return PTR_ERR(child_ctr
);
3738 static void sync_child_counter(struct perf_counter
*child_counter
,
3739 struct perf_counter
*parent_counter
)
3743 child_val
= atomic64_read(&child_counter
->count
);
3746 * Add back the child's count to the parent's count:
3748 atomic64_add(child_val
, &parent_counter
->count
);
3749 atomic64_add(child_counter
->total_time_enabled
,
3750 &parent_counter
->child_total_time_enabled
);
3751 atomic64_add(child_counter
->total_time_running
,
3752 &parent_counter
->child_total_time_running
);
3755 * Remove this counter from the parent's list
3757 WARN_ON_ONCE(parent_counter
->ctx
->parent_ctx
);
3758 mutex_lock(&parent_counter
->child_mutex
);
3759 list_del_init(&child_counter
->child_list
);
3760 mutex_unlock(&parent_counter
->child_mutex
);
3763 * Release the parent counter, if this was the last
3766 fput(parent_counter
->filp
);
3770 __perf_counter_exit_task(struct perf_counter
*child_counter
,
3771 struct perf_counter_context
*child_ctx
)
3773 struct perf_counter
*parent_counter
;
3775 update_counter_times(child_counter
);
3776 perf_counter_remove_from_context(child_counter
);
3778 parent_counter
= child_counter
->parent
;
3780 * It can happen that parent exits first, and has counters
3781 * that are still around due to the child reference. These
3782 * counters need to be zapped - but otherwise linger.
3784 if (parent_counter
) {
3785 sync_child_counter(child_counter
, parent_counter
);
3786 free_counter(child_counter
);
3791 * When a child task exits, feed back counter values to parent counters.
3793 void perf_counter_exit_task(struct task_struct
*child
)
3795 struct perf_counter
*child_counter
, *tmp
;
3796 struct perf_counter_context
*child_ctx
;
3797 unsigned long flags
;
3799 if (likely(!child
->perf_counter_ctxp
))
3802 local_irq_save(flags
);
3804 * We can't reschedule here because interrupts are disabled,
3805 * and either child is current or it is a task that can't be
3806 * scheduled, so we are now safe from rescheduling changing
3809 child_ctx
= child
->perf_counter_ctxp
;
3810 __perf_counter_task_sched_out(child_ctx
);
3813 * Take the context lock here so that if find_get_context is
3814 * reading child->perf_counter_ctxp, we wait until it has
3815 * incremented the context's refcount before we do put_ctx below.
3817 spin_lock(&child_ctx
->lock
);
3818 child
->perf_counter_ctxp
= NULL
;
3819 if (child_ctx
->parent_ctx
) {
3821 * This context is a clone; unclone it so it can't get
3822 * swapped to another process while we're removing all
3823 * the counters from it.
3825 put_ctx(child_ctx
->parent_ctx
);
3826 child_ctx
->parent_ctx
= NULL
;
3828 spin_unlock(&child_ctx
->lock
);
3829 local_irq_restore(flags
);
3831 mutex_lock(&child_ctx
->mutex
);
3834 list_for_each_entry_safe(child_counter
, tmp
, &child_ctx
->counter_list
,
3836 __perf_counter_exit_task(child_counter
, child_ctx
);
3839 * If the last counter was a group counter, it will have appended all
3840 * its siblings to the list, but we obtained 'tmp' before that which
3841 * will still point to the list head terminating the iteration.
3843 if (!list_empty(&child_ctx
->counter_list
))
3846 mutex_unlock(&child_ctx
->mutex
);
3852 * free an unexposed, unused context as created by inheritance by
3853 * init_task below, used by fork() in case of fail.
3855 void perf_counter_free_task(struct task_struct
*task
)
3857 struct perf_counter_context
*ctx
= task
->perf_counter_ctxp
;
3858 struct perf_counter
*counter
, *tmp
;
3863 mutex_lock(&ctx
->mutex
);
3865 list_for_each_entry_safe(counter
, tmp
, &ctx
->counter_list
, list_entry
) {
3866 struct perf_counter
*parent
= counter
->parent
;
3868 if (WARN_ON_ONCE(!parent
))
3871 mutex_lock(&parent
->child_mutex
);
3872 list_del_init(&counter
->child_list
);
3873 mutex_unlock(&parent
->child_mutex
);
3877 list_del_counter(counter
, ctx
);
3878 free_counter(counter
);
3881 if (!list_empty(&ctx
->counter_list
))
3884 mutex_unlock(&ctx
->mutex
);
3890 * Initialize the perf_counter context in task_struct
3892 int perf_counter_init_task(struct task_struct
*child
)
3894 struct perf_counter_context
*child_ctx
, *parent_ctx
;
3895 struct perf_counter_context
*cloned_ctx
;
3896 struct perf_counter
*counter
;
3897 struct task_struct
*parent
= current
;
3898 int inherited_all
= 1;
3901 child
->perf_counter_ctxp
= NULL
;
3903 mutex_init(&child
->perf_counter_mutex
);
3904 INIT_LIST_HEAD(&child
->perf_counter_list
);
3906 if (likely(!parent
->perf_counter_ctxp
))
3910 * This is executed from the parent task context, so inherit
3911 * counters that have been marked for cloning.
3912 * First allocate and initialize a context for the child.
3915 child_ctx
= kmalloc(sizeof(struct perf_counter_context
), GFP_KERNEL
);
3919 __perf_counter_init_context(child_ctx
, child
);
3920 child
->perf_counter_ctxp
= child_ctx
;
3921 get_task_struct(child
);
3924 * If the parent's context is a clone, pin it so it won't get
3927 parent_ctx
= perf_pin_task_context(parent
);
3930 * No need to check if parent_ctx != NULL here; since we saw
3931 * it non-NULL earlier, the only reason for it to become NULL
3932 * is if we exit, and since we're currently in the middle of
3933 * a fork we can't be exiting at the same time.
3937 * Lock the parent list. No need to lock the child - not PID
3938 * hashed yet and not running, so nobody can access it.
3940 mutex_lock(&parent_ctx
->mutex
);
3943 * We dont have to disable NMIs - we are only looking at
3944 * the list, not manipulating it:
3946 list_for_each_entry_rcu(counter
, &parent_ctx
->event_list
, event_entry
) {
3947 if (counter
!= counter
->group_leader
)
3950 if (!counter
->attr
.inherit
) {
3955 ret
= inherit_group(counter
, parent
, parent_ctx
,
3963 if (inherited_all
) {
3965 * Mark the child context as a clone of the parent
3966 * context, or of whatever the parent is a clone of.
3967 * Note that if the parent is a clone, it could get
3968 * uncloned at any point, but that doesn't matter
3969 * because the list of counters and the generation
3970 * count can't have changed since we took the mutex.
3972 cloned_ctx
= rcu_dereference(parent_ctx
->parent_ctx
);
3974 child_ctx
->parent_ctx
= cloned_ctx
;
3975 child_ctx
->parent_gen
= parent_ctx
->parent_gen
;
3977 child_ctx
->parent_ctx
= parent_ctx
;
3978 child_ctx
->parent_gen
= parent_ctx
->generation
;
3980 get_ctx(child_ctx
->parent_ctx
);
3983 mutex_unlock(&parent_ctx
->mutex
);
3985 perf_unpin_context(parent_ctx
);
3990 static void __cpuinit
perf_counter_init_cpu(int cpu
)
3992 struct perf_cpu_context
*cpuctx
;
3994 cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
3995 __perf_counter_init_context(&cpuctx
->ctx
, NULL
);
3997 spin_lock(&perf_resource_lock
);
3998 cpuctx
->max_pertask
= perf_max_counters
- perf_reserved_percpu
;
3999 spin_unlock(&perf_resource_lock
);
4001 hw_perf_counter_setup(cpu
);
4004 #ifdef CONFIG_HOTPLUG_CPU
4005 static void __perf_counter_exit_cpu(void *info
)
4007 struct perf_cpu_context
*cpuctx
= &__get_cpu_var(perf_cpu_context
);
4008 struct perf_counter_context
*ctx
= &cpuctx
->ctx
;
4009 struct perf_counter
*counter
, *tmp
;
4011 list_for_each_entry_safe(counter
, tmp
, &ctx
->counter_list
, list_entry
)
4012 __perf_counter_remove_from_context(counter
);
4014 static void perf_counter_exit_cpu(int cpu
)
4016 struct perf_cpu_context
*cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
4017 struct perf_counter_context
*ctx
= &cpuctx
->ctx
;
4019 mutex_lock(&ctx
->mutex
);
4020 smp_call_function_single(cpu
, __perf_counter_exit_cpu
, NULL
, 1);
4021 mutex_unlock(&ctx
->mutex
);
4024 static inline void perf_counter_exit_cpu(int cpu
) { }
4027 static int __cpuinit
4028 perf_cpu_notify(struct notifier_block
*self
, unsigned long action
, void *hcpu
)
4030 unsigned int cpu
= (long)hcpu
;
4034 case CPU_UP_PREPARE
:
4035 case CPU_UP_PREPARE_FROZEN
:
4036 perf_counter_init_cpu(cpu
);
4039 case CPU_DOWN_PREPARE
:
4040 case CPU_DOWN_PREPARE_FROZEN
:
4041 perf_counter_exit_cpu(cpu
);
4052 * This has to have a higher priority than migration_notifier in sched.c.
4054 static struct notifier_block __cpuinitdata perf_cpu_nb
= {
4055 .notifier_call
= perf_cpu_notify
,
4059 void __init
perf_counter_init(void)
4061 perf_cpu_notify(&perf_cpu_nb
, (unsigned long)CPU_UP_PREPARE
,
4062 (void *)(long)smp_processor_id());
4063 register_cpu_notifier(&perf_cpu_nb
);
4066 static ssize_t
perf_show_reserve_percpu(struct sysdev_class
*class, char *buf
)
4068 return sprintf(buf
, "%d\n", perf_reserved_percpu
);
4072 perf_set_reserve_percpu(struct sysdev_class
*class,
4076 struct perf_cpu_context
*cpuctx
;
4080 err
= strict_strtoul(buf
, 10, &val
);
4083 if (val
> perf_max_counters
)
4086 spin_lock(&perf_resource_lock
);
4087 perf_reserved_percpu
= val
;
4088 for_each_online_cpu(cpu
) {
4089 cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
4090 spin_lock_irq(&cpuctx
->ctx
.lock
);
4091 mpt
= min(perf_max_counters
- cpuctx
->ctx
.nr_counters
,
4092 perf_max_counters
- perf_reserved_percpu
);
4093 cpuctx
->max_pertask
= mpt
;
4094 spin_unlock_irq(&cpuctx
->ctx
.lock
);
4096 spin_unlock(&perf_resource_lock
);
4101 static ssize_t
perf_show_overcommit(struct sysdev_class
*class, char *buf
)
4103 return sprintf(buf
, "%d\n", perf_overcommit
);
4107 perf_set_overcommit(struct sysdev_class
*class, const char *buf
, size_t count
)
4112 err
= strict_strtoul(buf
, 10, &val
);
4118 spin_lock(&perf_resource_lock
);
4119 perf_overcommit
= val
;
4120 spin_unlock(&perf_resource_lock
);
4125 static SYSDEV_CLASS_ATTR(
4128 perf_show_reserve_percpu
,
4129 perf_set_reserve_percpu
4132 static SYSDEV_CLASS_ATTR(
4135 perf_show_overcommit
,
4139 static struct attribute
*perfclass_attrs
[] = {
4140 &attr_reserve_percpu
.attr
,
4141 &attr_overcommit
.attr
,
4145 static struct attribute_group perfclass_attr_group
= {
4146 .attrs
= perfclass_attrs
,
4147 .name
= "perf_counters",
4150 static int __init
perf_counter_sysfs_init(void)
4152 return sysfs_create_group(&cpu_sysdev_class
.kset
.kobj
,
4153 &perfclass_attr_group
);
4155 device_initcall(perf_counter_sysfs_init
);