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_comm_counters __read_mostly
;
45 static atomic_t nr_task_counters __read_mostly
;
48 * perf counter paranoia level:
49 * -1 - not paranoid at all
50 * 0 - disallow raw tracepoint access for unpriv
51 * 1 - disallow cpu counters for unpriv
52 * 2 - disallow kernel profiling for unpriv
54 int sysctl_perf_counter_paranoid __read_mostly
= 1;
56 static inline bool perf_paranoid_tracepoint_raw(void)
58 return sysctl_perf_counter_paranoid
> -1;
61 static inline bool perf_paranoid_cpu(void)
63 return sysctl_perf_counter_paranoid
> 0;
66 static inline bool perf_paranoid_kernel(void)
68 return sysctl_perf_counter_paranoid
> 1;
71 int sysctl_perf_counter_mlock __read_mostly
= 512; /* 'free' kb per user */
74 * max perf counter sample rate
76 int sysctl_perf_counter_sample_rate __read_mostly
= 100000;
78 static atomic64_t perf_counter_id
;
81 * Lock for (sysadmin-configurable) counter reservations:
83 static DEFINE_SPINLOCK(perf_resource_lock
);
86 * Architecture provided APIs - weak aliases:
88 extern __weak
const struct pmu
*hw_perf_counter_init(struct perf_counter
*counter
)
93 void __weak
hw_perf_disable(void) { barrier(); }
94 void __weak
hw_perf_enable(void) { barrier(); }
96 void __weak
hw_perf_counter_setup(int cpu
) { barrier(); }
97 void __weak
hw_perf_counter_setup_online(int cpu
) { barrier(); }
100 hw_perf_group_sched_in(struct perf_counter
*group_leader
,
101 struct perf_cpu_context
*cpuctx
,
102 struct perf_counter_context
*ctx
, int cpu
)
107 void __weak
perf_counter_print_debug(void) { }
109 static DEFINE_PER_CPU(int, disable_count
);
111 void __perf_disable(void)
113 __get_cpu_var(disable_count
)++;
116 bool __perf_enable(void)
118 return !--__get_cpu_var(disable_count
);
121 void perf_disable(void)
127 void perf_enable(void)
133 static void get_ctx(struct perf_counter_context
*ctx
)
135 WARN_ON(!atomic_inc_not_zero(&ctx
->refcount
));
138 static void free_ctx(struct rcu_head
*head
)
140 struct perf_counter_context
*ctx
;
142 ctx
= container_of(head
, struct perf_counter_context
, rcu_head
);
146 static void put_ctx(struct perf_counter_context
*ctx
)
148 if (atomic_dec_and_test(&ctx
->refcount
)) {
150 put_ctx(ctx
->parent_ctx
);
152 put_task_struct(ctx
->task
);
153 call_rcu(&ctx
->rcu_head
, free_ctx
);
157 static void unclone_ctx(struct perf_counter_context
*ctx
)
159 if (ctx
->parent_ctx
) {
160 put_ctx(ctx
->parent_ctx
);
161 ctx
->parent_ctx
= NULL
;
166 * If we inherit counters we want to return the parent counter id
169 static u64
primary_counter_id(struct perf_counter
*counter
)
171 u64 id
= counter
->id
;
174 id
= counter
->parent
->id
;
180 * Get the perf_counter_context for a task and lock it.
181 * This has to cope with with the fact that until it is locked,
182 * the context could get moved to another task.
184 static struct perf_counter_context
*
185 perf_lock_task_context(struct task_struct
*task
, unsigned long *flags
)
187 struct perf_counter_context
*ctx
;
191 ctx
= rcu_dereference(task
->perf_counter_ctxp
);
194 * If this context is a clone of another, it might
195 * get swapped for another underneath us by
196 * perf_counter_task_sched_out, though the
197 * rcu_read_lock() protects us from any context
198 * getting freed. Lock the context and check if it
199 * got swapped before we could get the lock, and retry
200 * if so. If we locked the right context, then it
201 * can't get swapped on us any more.
203 spin_lock_irqsave(&ctx
->lock
, *flags
);
204 if (ctx
!= rcu_dereference(task
->perf_counter_ctxp
)) {
205 spin_unlock_irqrestore(&ctx
->lock
, *flags
);
209 if (!atomic_inc_not_zero(&ctx
->refcount
)) {
210 spin_unlock_irqrestore(&ctx
->lock
, *flags
);
219 * Get the context for a task and increment its pin_count so it
220 * can't get swapped to another task. This also increments its
221 * reference count so that the context can't get freed.
223 static struct perf_counter_context
*perf_pin_task_context(struct task_struct
*task
)
225 struct perf_counter_context
*ctx
;
228 ctx
= perf_lock_task_context(task
, &flags
);
231 spin_unlock_irqrestore(&ctx
->lock
, flags
);
236 static void perf_unpin_context(struct perf_counter_context
*ctx
)
240 spin_lock_irqsave(&ctx
->lock
, flags
);
242 spin_unlock_irqrestore(&ctx
->lock
, flags
);
247 * Add a counter from the lists for its context.
248 * Must be called with ctx->mutex and ctx->lock held.
251 list_add_counter(struct perf_counter
*counter
, struct perf_counter_context
*ctx
)
253 struct perf_counter
*group_leader
= counter
->group_leader
;
256 * Depending on whether it is a standalone or sibling counter,
257 * add it straight to the context's counter list, or to the group
258 * leader's sibling list:
260 if (group_leader
== counter
)
261 list_add_tail(&counter
->list_entry
, &ctx
->counter_list
);
263 list_add_tail(&counter
->list_entry
, &group_leader
->sibling_list
);
264 group_leader
->nr_siblings
++;
267 list_add_rcu(&counter
->event_entry
, &ctx
->event_list
);
269 if (counter
->attr
.inherit_stat
)
274 * Remove a counter from the lists for its context.
275 * Must be called with ctx->mutex and ctx->lock held.
278 list_del_counter(struct perf_counter
*counter
, struct perf_counter_context
*ctx
)
280 struct perf_counter
*sibling
, *tmp
;
282 if (list_empty(&counter
->list_entry
))
285 if (counter
->attr
.inherit_stat
)
288 list_del_init(&counter
->list_entry
);
289 list_del_rcu(&counter
->event_entry
);
291 if (counter
->group_leader
!= counter
)
292 counter
->group_leader
->nr_siblings
--;
295 * If this was a group counter with sibling counters then
296 * upgrade the siblings to singleton counters by adding them
297 * to the context list directly:
299 list_for_each_entry_safe(sibling
, tmp
,
300 &counter
->sibling_list
, list_entry
) {
302 list_move_tail(&sibling
->list_entry
, &ctx
->counter_list
);
303 sibling
->group_leader
= sibling
;
308 counter_sched_out(struct perf_counter
*counter
,
309 struct perf_cpu_context
*cpuctx
,
310 struct perf_counter_context
*ctx
)
312 if (counter
->state
!= PERF_COUNTER_STATE_ACTIVE
)
315 counter
->state
= PERF_COUNTER_STATE_INACTIVE
;
316 if (counter
->pending_disable
) {
317 counter
->pending_disable
= 0;
318 counter
->state
= PERF_COUNTER_STATE_OFF
;
320 counter
->tstamp_stopped
= ctx
->time
;
321 counter
->pmu
->disable(counter
);
324 if (!is_software_counter(counter
))
325 cpuctx
->active_oncpu
--;
327 if (counter
->attr
.exclusive
|| !cpuctx
->active_oncpu
)
328 cpuctx
->exclusive
= 0;
332 group_sched_out(struct perf_counter
*group_counter
,
333 struct perf_cpu_context
*cpuctx
,
334 struct perf_counter_context
*ctx
)
336 struct perf_counter
*counter
;
338 if (group_counter
->state
!= PERF_COUNTER_STATE_ACTIVE
)
341 counter_sched_out(group_counter
, cpuctx
, ctx
);
344 * Schedule out siblings (if any):
346 list_for_each_entry(counter
, &group_counter
->sibling_list
, list_entry
)
347 counter_sched_out(counter
, cpuctx
, ctx
);
349 if (group_counter
->attr
.exclusive
)
350 cpuctx
->exclusive
= 0;
354 * Cross CPU call to remove a performance counter
356 * We disable the counter on the hardware level first. After that we
357 * remove it from the context list.
359 static void __perf_counter_remove_from_context(void *info
)
361 struct perf_cpu_context
*cpuctx
= &__get_cpu_var(perf_cpu_context
);
362 struct perf_counter
*counter
= info
;
363 struct perf_counter_context
*ctx
= counter
->ctx
;
366 * If this is a task context, we need to check whether it is
367 * the current task context of this cpu. If not it has been
368 * scheduled out before the smp call arrived.
370 if (ctx
->task
&& cpuctx
->task_ctx
!= ctx
)
373 spin_lock(&ctx
->lock
);
375 * Protect the list operation against NMI by disabling the
376 * counters on a global level.
380 counter_sched_out(counter
, cpuctx
, ctx
);
382 list_del_counter(counter
, ctx
);
386 * Allow more per task counters with respect to the
389 cpuctx
->max_pertask
=
390 min(perf_max_counters
- ctx
->nr_counters
,
391 perf_max_counters
- perf_reserved_percpu
);
395 spin_unlock(&ctx
->lock
);
400 * Remove the counter from a task's (or a CPU's) list of counters.
402 * Must be called with ctx->mutex held.
404 * CPU counters are removed with a smp call. For task counters we only
405 * call when the task is on a CPU.
407 * If counter->ctx is a cloned context, callers must make sure that
408 * every task struct that counter->ctx->task could possibly point to
409 * remains valid. This is OK when called from perf_release since
410 * that only calls us on the top-level context, which can't be a clone.
411 * When called from perf_counter_exit_task, it's OK because the
412 * context has been detached from its task.
414 static void perf_counter_remove_from_context(struct perf_counter
*counter
)
416 struct perf_counter_context
*ctx
= counter
->ctx
;
417 struct task_struct
*task
= ctx
->task
;
421 * Per cpu counters are removed via an smp call and
422 * the removal is always sucessful.
424 smp_call_function_single(counter
->cpu
,
425 __perf_counter_remove_from_context
,
431 task_oncpu_function_call(task
, __perf_counter_remove_from_context
,
434 spin_lock_irq(&ctx
->lock
);
436 * If the context is active we need to retry the smp call.
438 if (ctx
->nr_active
&& !list_empty(&counter
->list_entry
)) {
439 spin_unlock_irq(&ctx
->lock
);
444 * The lock prevents that this context is scheduled in so we
445 * can remove the counter safely, if the call above did not
448 if (!list_empty(&counter
->list_entry
)) {
449 list_del_counter(counter
, ctx
);
451 spin_unlock_irq(&ctx
->lock
);
454 static inline u64
perf_clock(void)
456 return cpu_clock(smp_processor_id());
460 * Update the record of the current time in a context.
462 static void update_context_time(struct perf_counter_context
*ctx
)
464 u64 now
= perf_clock();
466 ctx
->time
+= now
- ctx
->timestamp
;
467 ctx
->timestamp
= now
;
471 * Update the total_time_enabled and total_time_running fields for a counter.
473 static void update_counter_times(struct perf_counter
*counter
)
475 struct perf_counter_context
*ctx
= counter
->ctx
;
478 if (counter
->state
< PERF_COUNTER_STATE_INACTIVE
||
479 counter
->group_leader
->state
< PERF_COUNTER_STATE_INACTIVE
)
482 counter
->total_time_enabled
= ctx
->time
- counter
->tstamp_enabled
;
484 if (counter
->state
== PERF_COUNTER_STATE_INACTIVE
)
485 run_end
= counter
->tstamp_stopped
;
489 counter
->total_time_running
= run_end
- counter
->tstamp_running
;
493 * Update total_time_enabled and total_time_running for all counters in a group.
495 static void update_group_times(struct perf_counter
*leader
)
497 struct perf_counter
*counter
;
499 update_counter_times(leader
);
500 list_for_each_entry(counter
, &leader
->sibling_list
, list_entry
)
501 update_counter_times(counter
);
505 * Cross CPU call to disable a performance counter
507 static void __perf_counter_disable(void *info
)
509 struct perf_counter
*counter
= info
;
510 struct perf_cpu_context
*cpuctx
= &__get_cpu_var(perf_cpu_context
);
511 struct perf_counter_context
*ctx
= counter
->ctx
;
514 * If this is a per-task counter, need to check whether this
515 * counter's task is the current task on this cpu.
517 if (ctx
->task
&& cpuctx
->task_ctx
!= ctx
)
520 spin_lock(&ctx
->lock
);
523 * If the counter is on, turn it off.
524 * If it is in error state, leave it in error state.
526 if (counter
->state
>= PERF_COUNTER_STATE_INACTIVE
) {
527 update_context_time(ctx
);
528 update_group_times(counter
);
529 if (counter
== counter
->group_leader
)
530 group_sched_out(counter
, cpuctx
, ctx
);
532 counter_sched_out(counter
, cpuctx
, ctx
);
533 counter
->state
= PERF_COUNTER_STATE_OFF
;
536 spin_unlock(&ctx
->lock
);
542 * If counter->ctx is a cloned context, callers must make sure that
543 * every task struct that counter->ctx->task could possibly point to
544 * remains valid. This condition is satisifed when called through
545 * perf_counter_for_each_child or perf_counter_for_each because they
546 * hold the top-level counter's child_mutex, so any descendant that
547 * goes to exit will block in sync_child_counter.
548 * When called from perf_pending_counter it's OK because counter->ctx
549 * is the current context on this CPU and preemption is disabled,
550 * hence we can't get into perf_counter_task_sched_out for this context.
552 static void perf_counter_disable(struct perf_counter
*counter
)
554 struct perf_counter_context
*ctx
= counter
->ctx
;
555 struct task_struct
*task
= ctx
->task
;
559 * Disable the counter on the cpu that it's on
561 smp_call_function_single(counter
->cpu
, __perf_counter_disable
,
567 task_oncpu_function_call(task
, __perf_counter_disable
, counter
);
569 spin_lock_irq(&ctx
->lock
);
571 * If the counter is still active, we need to retry the cross-call.
573 if (counter
->state
== PERF_COUNTER_STATE_ACTIVE
) {
574 spin_unlock_irq(&ctx
->lock
);
579 * Since we have the lock this context can't be scheduled
580 * in, so we can change the state safely.
582 if (counter
->state
== PERF_COUNTER_STATE_INACTIVE
) {
583 update_group_times(counter
);
584 counter
->state
= PERF_COUNTER_STATE_OFF
;
587 spin_unlock_irq(&ctx
->lock
);
591 counter_sched_in(struct perf_counter
*counter
,
592 struct perf_cpu_context
*cpuctx
,
593 struct perf_counter_context
*ctx
,
596 if (counter
->state
<= PERF_COUNTER_STATE_OFF
)
599 counter
->state
= PERF_COUNTER_STATE_ACTIVE
;
600 counter
->oncpu
= cpu
; /* TODO: put 'cpu' into cpuctx->cpu */
602 * The new state must be visible before we turn it on in the hardware:
606 if (counter
->pmu
->enable(counter
)) {
607 counter
->state
= PERF_COUNTER_STATE_INACTIVE
;
612 counter
->tstamp_running
+= ctx
->time
- counter
->tstamp_stopped
;
614 if (!is_software_counter(counter
))
615 cpuctx
->active_oncpu
++;
618 if (counter
->attr
.exclusive
)
619 cpuctx
->exclusive
= 1;
625 group_sched_in(struct perf_counter
*group_counter
,
626 struct perf_cpu_context
*cpuctx
,
627 struct perf_counter_context
*ctx
,
630 struct perf_counter
*counter
, *partial_group
;
633 if (group_counter
->state
== PERF_COUNTER_STATE_OFF
)
636 ret
= hw_perf_group_sched_in(group_counter
, cpuctx
, ctx
, cpu
);
638 return ret
< 0 ? ret
: 0;
640 if (counter_sched_in(group_counter
, cpuctx
, ctx
, cpu
))
644 * Schedule in siblings as one group (if any):
646 list_for_each_entry(counter
, &group_counter
->sibling_list
, list_entry
) {
647 if (counter_sched_in(counter
, cpuctx
, ctx
, cpu
)) {
648 partial_group
= counter
;
657 * Groups can be scheduled in as one unit only, so undo any
658 * partial group before returning:
660 list_for_each_entry(counter
, &group_counter
->sibling_list
, list_entry
) {
661 if (counter
== partial_group
)
663 counter_sched_out(counter
, cpuctx
, ctx
);
665 counter_sched_out(group_counter
, cpuctx
, ctx
);
671 * Return 1 for a group consisting entirely of software counters,
672 * 0 if the group contains any hardware counters.
674 static int is_software_only_group(struct perf_counter
*leader
)
676 struct perf_counter
*counter
;
678 if (!is_software_counter(leader
))
681 list_for_each_entry(counter
, &leader
->sibling_list
, list_entry
)
682 if (!is_software_counter(counter
))
689 * Work out whether we can put this counter group on the CPU now.
691 static int group_can_go_on(struct perf_counter
*counter
,
692 struct perf_cpu_context
*cpuctx
,
696 * Groups consisting entirely of software counters can always go on.
698 if (is_software_only_group(counter
))
701 * If an exclusive group is already on, no other hardware
702 * counters can go on.
704 if (cpuctx
->exclusive
)
707 * If this group is exclusive and there are already
708 * counters on the CPU, it can't go on.
710 if (counter
->attr
.exclusive
&& cpuctx
->active_oncpu
)
713 * Otherwise, try to add it if all previous groups were able
719 static void add_counter_to_ctx(struct perf_counter
*counter
,
720 struct perf_counter_context
*ctx
)
722 list_add_counter(counter
, ctx
);
723 counter
->tstamp_enabled
= ctx
->time
;
724 counter
->tstamp_running
= ctx
->time
;
725 counter
->tstamp_stopped
= ctx
->time
;
729 * Cross CPU call to install and enable a performance counter
731 * Must be called with ctx->mutex held
733 static void __perf_install_in_context(void *info
)
735 struct perf_cpu_context
*cpuctx
= &__get_cpu_var(perf_cpu_context
);
736 struct perf_counter
*counter
= info
;
737 struct perf_counter_context
*ctx
= counter
->ctx
;
738 struct perf_counter
*leader
= counter
->group_leader
;
739 int cpu
= smp_processor_id();
743 * If this is a task context, we need to check whether it is
744 * the current task context of this cpu. If not it has been
745 * scheduled out before the smp call arrived.
746 * Or possibly this is the right context but it isn't
747 * on this cpu because it had no counters.
749 if (ctx
->task
&& cpuctx
->task_ctx
!= ctx
) {
750 if (cpuctx
->task_ctx
|| ctx
->task
!= current
)
752 cpuctx
->task_ctx
= ctx
;
755 spin_lock(&ctx
->lock
);
757 update_context_time(ctx
);
760 * Protect the list operation against NMI by disabling the
761 * counters on a global level. NOP for non NMI based counters.
765 add_counter_to_ctx(counter
, ctx
);
768 * Don't put the counter on if it is disabled or if
769 * it is in a group and the group isn't on.
771 if (counter
->state
!= PERF_COUNTER_STATE_INACTIVE
||
772 (leader
!= counter
&& leader
->state
!= PERF_COUNTER_STATE_ACTIVE
))
776 * An exclusive counter can't go on if there are already active
777 * hardware counters, and no hardware counter can go on if there
778 * is already an exclusive counter on.
780 if (!group_can_go_on(counter
, cpuctx
, 1))
783 err
= counter_sched_in(counter
, cpuctx
, ctx
, cpu
);
787 * This counter couldn't go on. If it is in a group
788 * then we have to pull the whole group off.
789 * If the counter group is pinned then put it in error state.
791 if (leader
!= counter
)
792 group_sched_out(leader
, cpuctx
, ctx
);
793 if (leader
->attr
.pinned
) {
794 update_group_times(leader
);
795 leader
->state
= PERF_COUNTER_STATE_ERROR
;
799 if (!err
&& !ctx
->task
&& cpuctx
->max_pertask
)
800 cpuctx
->max_pertask
--;
805 spin_unlock(&ctx
->lock
);
809 * Attach a performance counter to a context
811 * First we add the counter to the list with the hardware enable bit
812 * in counter->hw_config cleared.
814 * If the counter is attached to a task which is on a CPU we use a smp
815 * call to enable it in the task context. The task might have been
816 * scheduled away, but we check this in the smp call again.
818 * Must be called with ctx->mutex held.
821 perf_install_in_context(struct perf_counter_context
*ctx
,
822 struct perf_counter
*counter
,
825 struct task_struct
*task
= ctx
->task
;
829 * Per cpu counters are installed via an smp call and
830 * the install is always sucessful.
832 smp_call_function_single(cpu
, __perf_install_in_context
,
838 task_oncpu_function_call(task
, __perf_install_in_context
,
841 spin_lock_irq(&ctx
->lock
);
843 * we need to retry the smp call.
845 if (ctx
->is_active
&& list_empty(&counter
->list_entry
)) {
846 spin_unlock_irq(&ctx
->lock
);
851 * The lock prevents that this context is scheduled in so we
852 * can add the counter safely, if it the call above did not
855 if (list_empty(&counter
->list_entry
))
856 add_counter_to_ctx(counter
, ctx
);
857 spin_unlock_irq(&ctx
->lock
);
861 * Put a counter into inactive state and update time fields.
862 * Enabling the leader of a group effectively enables all
863 * the group members that aren't explicitly disabled, so we
864 * have to update their ->tstamp_enabled also.
865 * Note: this works for group members as well as group leaders
866 * since the non-leader members' sibling_lists will be empty.
868 static void __perf_counter_mark_enabled(struct perf_counter
*counter
,
869 struct perf_counter_context
*ctx
)
871 struct perf_counter
*sub
;
873 counter
->state
= PERF_COUNTER_STATE_INACTIVE
;
874 counter
->tstamp_enabled
= ctx
->time
- counter
->total_time_enabled
;
875 list_for_each_entry(sub
, &counter
->sibling_list
, list_entry
)
876 if (sub
->state
>= PERF_COUNTER_STATE_INACTIVE
)
877 sub
->tstamp_enabled
=
878 ctx
->time
- sub
->total_time_enabled
;
882 * Cross CPU call to enable a performance counter
884 static void __perf_counter_enable(void *info
)
886 struct perf_counter
*counter
= info
;
887 struct perf_cpu_context
*cpuctx
= &__get_cpu_var(perf_cpu_context
);
888 struct perf_counter_context
*ctx
= counter
->ctx
;
889 struct perf_counter
*leader
= counter
->group_leader
;
893 * If this is a per-task counter, need to check whether this
894 * counter's task is the current task on this cpu.
896 if (ctx
->task
&& cpuctx
->task_ctx
!= ctx
) {
897 if (cpuctx
->task_ctx
|| ctx
->task
!= current
)
899 cpuctx
->task_ctx
= ctx
;
902 spin_lock(&ctx
->lock
);
904 update_context_time(ctx
);
906 if (counter
->state
>= PERF_COUNTER_STATE_INACTIVE
)
908 __perf_counter_mark_enabled(counter
, ctx
);
911 * If the counter is in a group and isn't the group leader,
912 * then don't put it on unless the group is on.
914 if (leader
!= counter
&& leader
->state
!= PERF_COUNTER_STATE_ACTIVE
)
917 if (!group_can_go_on(counter
, cpuctx
, 1)) {
921 if (counter
== leader
)
922 err
= group_sched_in(counter
, cpuctx
, ctx
,
925 err
= counter_sched_in(counter
, cpuctx
, ctx
,
932 * If this counter can't go on and it's part of a
933 * group, then the whole group has to come off.
935 if (leader
!= counter
)
936 group_sched_out(leader
, cpuctx
, ctx
);
937 if (leader
->attr
.pinned
) {
938 update_group_times(leader
);
939 leader
->state
= PERF_COUNTER_STATE_ERROR
;
944 spin_unlock(&ctx
->lock
);
950 * If counter->ctx is a cloned context, callers must make sure that
951 * every task struct that counter->ctx->task could possibly point to
952 * remains valid. This condition is satisfied when called through
953 * perf_counter_for_each_child or perf_counter_for_each as described
954 * for perf_counter_disable.
956 static void perf_counter_enable(struct perf_counter
*counter
)
958 struct perf_counter_context
*ctx
= counter
->ctx
;
959 struct task_struct
*task
= ctx
->task
;
963 * Enable the counter on the cpu that it's on
965 smp_call_function_single(counter
->cpu
, __perf_counter_enable
,
970 spin_lock_irq(&ctx
->lock
);
971 if (counter
->state
>= PERF_COUNTER_STATE_INACTIVE
)
975 * If the counter is in error state, clear that first.
976 * That way, if we see the counter in error state below, we
977 * know that it has gone back into error state, as distinct
978 * from the task having been scheduled away before the
979 * cross-call arrived.
981 if (counter
->state
== PERF_COUNTER_STATE_ERROR
)
982 counter
->state
= PERF_COUNTER_STATE_OFF
;
985 spin_unlock_irq(&ctx
->lock
);
986 task_oncpu_function_call(task
, __perf_counter_enable
, counter
);
988 spin_lock_irq(&ctx
->lock
);
991 * If the context is active and the counter is still off,
992 * we need to retry the cross-call.
994 if (ctx
->is_active
&& counter
->state
== PERF_COUNTER_STATE_OFF
)
998 * Since we have the lock this context can't be scheduled
999 * in, so we can change the state safely.
1001 if (counter
->state
== PERF_COUNTER_STATE_OFF
)
1002 __perf_counter_mark_enabled(counter
, ctx
);
1005 spin_unlock_irq(&ctx
->lock
);
1008 static int perf_counter_refresh(struct perf_counter
*counter
, int refresh
)
1011 * not supported on inherited counters
1013 if (counter
->attr
.inherit
)
1016 atomic_add(refresh
, &counter
->event_limit
);
1017 perf_counter_enable(counter
);
1022 void __perf_counter_sched_out(struct perf_counter_context
*ctx
,
1023 struct perf_cpu_context
*cpuctx
)
1025 struct perf_counter
*counter
;
1027 spin_lock(&ctx
->lock
);
1029 if (likely(!ctx
->nr_counters
))
1031 update_context_time(ctx
);
1034 if (ctx
->nr_active
) {
1035 list_for_each_entry(counter
, &ctx
->counter_list
, list_entry
) {
1036 if (counter
!= counter
->group_leader
)
1037 counter_sched_out(counter
, cpuctx
, ctx
);
1039 group_sched_out(counter
, cpuctx
, ctx
);
1044 spin_unlock(&ctx
->lock
);
1048 * Test whether two contexts are equivalent, i.e. whether they
1049 * have both been cloned from the same version of the same context
1050 * and they both have the same number of enabled counters.
1051 * If the number of enabled counters is the same, then the set
1052 * of enabled counters should be the same, because these are both
1053 * inherited contexts, therefore we can't access individual counters
1054 * in them directly with an fd; we can only enable/disable all
1055 * counters via prctl, or enable/disable all counters in a family
1056 * via ioctl, which will have the same effect on both contexts.
1058 static int context_equiv(struct perf_counter_context
*ctx1
,
1059 struct perf_counter_context
*ctx2
)
1061 return ctx1
->parent_ctx
&& ctx1
->parent_ctx
== ctx2
->parent_ctx
1062 && ctx1
->parent_gen
== ctx2
->parent_gen
1063 && !ctx1
->pin_count
&& !ctx2
->pin_count
;
1066 static void __perf_counter_read(void *counter
);
1068 static void __perf_counter_sync_stat(struct perf_counter
*counter
,
1069 struct perf_counter
*next_counter
)
1073 if (!counter
->attr
.inherit_stat
)
1077 * Update the counter value, we cannot use perf_counter_read()
1078 * because we're in the middle of a context switch and have IRQs
1079 * disabled, which upsets smp_call_function_single(), however
1080 * we know the counter must be on the current CPU, therefore we
1081 * don't need to use it.
1083 switch (counter
->state
) {
1084 case PERF_COUNTER_STATE_ACTIVE
:
1085 __perf_counter_read(counter
);
1088 case PERF_COUNTER_STATE_INACTIVE
:
1089 update_counter_times(counter
);
1097 * In order to keep per-task stats reliable we need to flip the counter
1098 * values when we flip the contexts.
1100 value
= atomic64_read(&next_counter
->count
);
1101 value
= atomic64_xchg(&counter
->count
, value
);
1102 atomic64_set(&next_counter
->count
, value
);
1104 swap(counter
->total_time_enabled
, next_counter
->total_time_enabled
);
1105 swap(counter
->total_time_running
, next_counter
->total_time_running
);
1108 * Since we swizzled the values, update the user visible data too.
1110 perf_counter_update_userpage(counter
);
1111 perf_counter_update_userpage(next_counter
);
1114 #define list_next_entry(pos, member) \
1115 list_entry(pos->member.next, typeof(*pos), member)
1117 static void perf_counter_sync_stat(struct perf_counter_context
*ctx
,
1118 struct perf_counter_context
*next_ctx
)
1120 struct perf_counter
*counter
, *next_counter
;
1125 counter
= list_first_entry(&ctx
->event_list
,
1126 struct perf_counter
, event_entry
);
1128 next_counter
= list_first_entry(&next_ctx
->event_list
,
1129 struct perf_counter
, event_entry
);
1131 while (&counter
->event_entry
!= &ctx
->event_list
&&
1132 &next_counter
->event_entry
!= &next_ctx
->event_list
) {
1134 __perf_counter_sync_stat(counter
, next_counter
);
1136 counter
= list_next_entry(counter
, event_entry
);
1137 next_counter
= list_next_entry(next_counter
, event_entry
);
1142 * Called from scheduler to remove the counters of the current task,
1143 * with interrupts disabled.
1145 * We stop each counter and update the counter value in counter->count.
1147 * This does not protect us against NMI, but disable()
1148 * sets the disabled bit in the control field of counter _before_
1149 * accessing the counter control register. If a NMI hits, then it will
1150 * not restart the counter.
1152 void perf_counter_task_sched_out(struct task_struct
*task
,
1153 struct task_struct
*next
, int cpu
)
1155 struct perf_cpu_context
*cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
1156 struct perf_counter_context
*ctx
= task
->perf_counter_ctxp
;
1157 struct perf_counter_context
*next_ctx
;
1158 struct perf_counter_context
*parent
;
1159 struct pt_regs
*regs
;
1162 regs
= task_pt_regs(task
);
1163 perf_swcounter_event(PERF_COUNT_SW_CONTEXT_SWITCHES
, 1, 1, regs
, 0);
1165 if (likely(!ctx
|| !cpuctx
->task_ctx
))
1168 update_context_time(ctx
);
1171 parent
= rcu_dereference(ctx
->parent_ctx
);
1172 next_ctx
= next
->perf_counter_ctxp
;
1173 if (parent
&& next_ctx
&&
1174 rcu_dereference(next_ctx
->parent_ctx
) == parent
) {
1176 * Looks like the two contexts are clones, so we might be
1177 * able to optimize the context switch. We lock both
1178 * contexts and check that they are clones under the
1179 * lock (including re-checking that neither has been
1180 * uncloned in the meantime). It doesn't matter which
1181 * order we take the locks because no other cpu could
1182 * be trying to lock both of these tasks.
1184 spin_lock(&ctx
->lock
);
1185 spin_lock_nested(&next_ctx
->lock
, SINGLE_DEPTH_NESTING
);
1186 if (context_equiv(ctx
, next_ctx
)) {
1188 * XXX do we need a memory barrier of sorts
1189 * wrt to rcu_dereference() of perf_counter_ctxp
1191 task
->perf_counter_ctxp
= next_ctx
;
1192 next
->perf_counter_ctxp
= ctx
;
1194 next_ctx
->task
= task
;
1197 perf_counter_sync_stat(ctx
, next_ctx
);
1199 spin_unlock(&next_ctx
->lock
);
1200 spin_unlock(&ctx
->lock
);
1205 __perf_counter_sched_out(ctx
, cpuctx
);
1206 cpuctx
->task_ctx
= NULL
;
1211 * Called with IRQs disabled
1213 static void __perf_counter_task_sched_out(struct perf_counter_context
*ctx
)
1215 struct perf_cpu_context
*cpuctx
= &__get_cpu_var(perf_cpu_context
);
1217 if (!cpuctx
->task_ctx
)
1220 if (WARN_ON_ONCE(ctx
!= cpuctx
->task_ctx
))
1223 __perf_counter_sched_out(ctx
, cpuctx
);
1224 cpuctx
->task_ctx
= NULL
;
1228 * Called with IRQs disabled
1230 static void perf_counter_cpu_sched_out(struct perf_cpu_context
*cpuctx
)
1232 __perf_counter_sched_out(&cpuctx
->ctx
, cpuctx
);
1236 __perf_counter_sched_in(struct perf_counter_context
*ctx
,
1237 struct perf_cpu_context
*cpuctx
, int cpu
)
1239 struct perf_counter
*counter
;
1242 spin_lock(&ctx
->lock
);
1244 if (likely(!ctx
->nr_counters
))
1247 ctx
->timestamp
= perf_clock();
1252 * First go through the list and put on any pinned groups
1253 * in order to give them the best chance of going on.
1255 list_for_each_entry(counter
, &ctx
->counter_list
, list_entry
) {
1256 if (counter
->state
<= PERF_COUNTER_STATE_OFF
||
1257 !counter
->attr
.pinned
)
1259 if (counter
->cpu
!= -1 && counter
->cpu
!= cpu
)
1262 if (counter
!= counter
->group_leader
)
1263 counter_sched_in(counter
, cpuctx
, ctx
, cpu
);
1265 if (group_can_go_on(counter
, cpuctx
, 1))
1266 group_sched_in(counter
, cpuctx
, ctx
, cpu
);
1270 * If this pinned group hasn't been scheduled,
1271 * put it in error state.
1273 if (counter
->state
== PERF_COUNTER_STATE_INACTIVE
) {
1274 update_group_times(counter
);
1275 counter
->state
= PERF_COUNTER_STATE_ERROR
;
1279 list_for_each_entry(counter
, &ctx
->counter_list
, list_entry
) {
1281 * Ignore counters in OFF or ERROR state, and
1282 * ignore pinned counters since we did them already.
1284 if (counter
->state
<= PERF_COUNTER_STATE_OFF
||
1285 counter
->attr
.pinned
)
1289 * Listen to the 'cpu' scheduling filter constraint
1292 if (counter
->cpu
!= -1 && counter
->cpu
!= cpu
)
1295 if (counter
!= counter
->group_leader
) {
1296 if (counter_sched_in(counter
, cpuctx
, ctx
, cpu
))
1299 if (group_can_go_on(counter
, cpuctx
, can_add_hw
)) {
1300 if (group_sched_in(counter
, cpuctx
, ctx
, cpu
))
1307 spin_unlock(&ctx
->lock
);
1311 * Called from scheduler to add the counters of the current task
1312 * with interrupts disabled.
1314 * We restore the counter value and then enable it.
1316 * This does not protect us against NMI, but enable()
1317 * sets the enabled bit in the control field of counter _before_
1318 * accessing the counter control register. If a NMI hits, then it will
1319 * keep the counter running.
1321 void perf_counter_task_sched_in(struct task_struct
*task
, int cpu
)
1323 struct perf_cpu_context
*cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
1324 struct perf_counter_context
*ctx
= task
->perf_counter_ctxp
;
1328 if (cpuctx
->task_ctx
== ctx
)
1330 __perf_counter_sched_in(ctx
, cpuctx
, cpu
);
1331 cpuctx
->task_ctx
= ctx
;
1334 static void perf_counter_cpu_sched_in(struct perf_cpu_context
*cpuctx
, int cpu
)
1336 struct perf_counter_context
*ctx
= &cpuctx
->ctx
;
1338 __perf_counter_sched_in(ctx
, cpuctx
, cpu
);
1341 #define MAX_INTERRUPTS (~0ULL)
1343 static void perf_log_throttle(struct perf_counter
*counter
, int enable
);
1345 static void perf_adjust_period(struct perf_counter
*counter
, u64 events
)
1347 struct hw_perf_counter
*hwc
= &counter
->hw
;
1348 u64 period
, sample_period
;
1351 events
*= hwc
->sample_period
;
1352 period
= div64_u64(events
, counter
->attr
.sample_freq
);
1354 delta
= (s64
)(period
- hwc
->sample_period
);
1355 delta
= (delta
+ 7) / 8; /* low pass filter */
1357 sample_period
= hwc
->sample_period
+ delta
;
1362 hwc
->sample_period
= sample_period
;
1365 static void perf_ctx_adjust_freq(struct perf_counter_context
*ctx
)
1367 struct perf_counter
*counter
;
1368 struct hw_perf_counter
*hwc
;
1369 u64 interrupts
, freq
;
1371 spin_lock(&ctx
->lock
);
1372 list_for_each_entry(counter
, &ctx
->counter_list
, list_entry
) {
1373 if (counter
->state
!= PERF_COUNTER_STATE_ACTIVE
)
1378 interrupts
= hwc
->interrupts
;
1379 hwc
->interrupts
= 0;
1382 * unthrottle counters on the tick
1384 if (interrupts
== MAX_INTERRUPTS
) {
1385 perf_log_throttle(counter
, 1);
1386 counter
->pmu
->unthrottle(counter
);
1387 interrupts
= 2*sysctl_perf_counter_sample_rate
/HZ
;
1390 if (!counter
->attr
.freq
|| !counter
->attr
.sample_freq
)
1394 * if the specified freq < HZ then we need to skip ticks
1396 if (counter
->attr
.sample_freq
< HZ
) {
1397 freq
= counter
->attr
.sample_freq
;
1399 hwc
->freq_count
+= freq
;
1400 hwc
->freq_interrupts
+= interrupts
;
1402 if (hwc
->freq_count
< HZ
)
1405 interrupts
= hwc
->freq_interrupts
;
1406 hwc
->freq_interrupts
= 0;
1407 hwc
->freq_count
-= HZ
;
1411 perf_adjust_period(counter
, freq
* interrupts
);
1414 * In order to avoid being stalled by an (accidental) huge
1415 * sample period, force reset the sample period if we didn't
1416 * get any events in this freq period.
1420 counter
->pmu
->disable(counter
);
1421 atomic64_set(&hwc
->period_left
, 0);
1422 counter
->pmu
->enable(counter
);
1426 spin_unlock(&ctx
->lock
);
1430 * Round-robin a context's counters:
1432 static void rotate_ctx(struct perf_counter_context
*ctx
)
1434 struct perf_counter
*counter
;
1436 if (!ctx
->nr_counters
)
1439 spin_lock(&ctx
->lock
);
1441 * Rotate the first entry last (works just fine for group counters too):
1444 list_for_each_entry(counter
, &ctx
->counter_list
, list_entry
) {
1445 list_move_tail(&counter
->list_entry
, &ctx
->counter_list
);
1450 spin_unlock(&ctx
->lock
);
1453 void perf_counter_task_tick(struct task_struct
*curr
, int cpu
)
1455 struct perf_cpu_context
*cpuctx
;
1456 struct perf_counter_context
*ctx
;
1458 if (!atomic_read(&nr_counters
))
1461 cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
1462 ctx
= curr
->perf_counter_ctxp
;
1464 perf_ctx_adjust_freq(&cpuctx
->ctx
);
1466 perf_ctx_adjust_freq(ctx
);
1468 perf_counter_cpu_sched_out(cpuctx
);
1470 __perf_counter_task_sched_out(ctx
);
1472 rotate_ctx(&cpuctx
->ctx
);
1476 perf_counter_cpu_sched_in(cpuctx
, cpu
);
1478 perf_counter_task_sched_in(curr
, cpu
);
1482 * Enable all of a task's counters that have been marked enable-on-exec.
1483 * This expects task == current.
1485 static void perf_counter_enable_on_exec(struct task_struct
*task
)
1487 struct perf_counter_context
*ctx
;
1488 struct perf_counter
*counter
;
1489 unsigned long flags
;
1492 local_irq_save(flags
);
1493 ctx
= task
->perf_counter_ctxp
;
1494 if (!ctx
|| !ctx
->nr_counters
)
1497 __perf_counter_task_sched_out(ctx
);
1499 spin_lock(&ctx
->lock
);
1501 list_for_each_entry(counter
, &ctx
->counter_list
, list_entry
) {
1502 if (!counter
->attr
.enable_on_exec
)
1504 counter
->attr
.enable_on_exec
= 0;
1505 if (counter
->state
>= PERF_COUNTER_STATE_INACTIVE
)
1507 __perf_counter_mark_enabled(counter
, ctx
);
1512 * Unclone this context if we enabled any counter.
1517 spin_unlock(&ctx
->lock
);
1519 perf_counter_task_sched_in(task
, smp_processor_id());
1521 local_irq_restore(flags
);
1525 * Cross CPU call to read the hardware counter
1527 static void __perf_counter_read(void *info
)
1529 struct perf_cpu_context
*cpuctx
= &__get_cpu_var(perf_cpu_context
);
1530 struct perf_counter
*counter
= info
;
1531 struct perf_counter_context
*ctx
= counter
->ctx
;
1532 unsigned long flags
;
1535 * If this is a task context, we need to check whether it is
1536 * the current task context of this cpu. If not it has been
1537 * scheduled out before the smp call arrived. In that case
1538 * counter->count would have been updated to a recent sample
1539 * when the counter was scheduled out.
1541 if (ctx
->task
&& cpuctx
->task_ctx
!= ctx
)
1544 local_irq_save(flags
);
1546 update_context_time(ctx
);
1547 counter
->pmu
->read(counter
);
1548 update_counter_times(counter
);
1549 local_irq_restore(flags
);
1552 static u64
perf_counter_read(struct perf_counter
*counter
)
1555 * If counter is enabled and currently active on a CPU, update the
1556 * value in the counter structure:
1558 if (counter
->state
== PERF_COUNTER_STATE_ACTIVE
) {
1559 smp_call_function_single(counter
->oncpu
,
1560 __perf_counter_read
, counter
, 1);
1561 } else if (counter
->state
== PERF_COUNTER_STATE_INACTIVE
) {
1562 update_counter_times(counter
);
1565 return atomic64_read(&counter
->count
);
1569 * Initialize the perf_counter context in a task_struct:
1572 __perf_counter_init_context(struct perf_counter_context
*ctx
,
1573 struct task_struct
*task
)
1575 memset(ctx
, 0, sizeof(*ctx
));
1576 spin_lock_init(&ctx
->lock
);
1577 mutex_init(&ctx
->mutex
);
1578 INIT_LIST_HEAD(&ctx
->counter_list
);
1579 INIT_LIST_HEAD(&ctx
->event_list
);
1580 atomic_set(&ctx
->refcount
, 1);
1584 static struct perf_counter_context
*find_get_context(pid_t pid
, int cpu
)
1586 struct perf_counter_context
*ctx
;
1587 struct perf_cpu_context
*cpuctx
;
1588 struct task_struct
*task
;
1589 unsigned long flags
;
1593 * If cpu is not a wildcard then this is a percpu counter:
1596 /* Must be root to operate on a CPU counter: */
1597 if (perf_paranoid_cpu() && !capable(CAP_SYS_ADMIN
))
1598 return ERR_PTR(-EACCES
);
1600 if (cpu
< 0 || cpu
> num_possible_cpus())
1601 return ERR_PTR(-EINVAL
);
1604 * We could be clever and allow to attach a counter to an
1605 * offline CPU and activate it when the CPU comes up, but
1608 if (!cpu_isset(cpu
, cpu_online_map
))
1609 return ERR_PTR(-ENODEV
);
1611 cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
1622 task
= find_task_by_vpid(pid
);
1624 get_task_struct(task
);
1628 return ERR_PTR(-ESRCH
);
1631 * Can't attach counters to a dying task.
1634 if (task
->flags
& PF_EXITING
)
1637 /* Reuse ptrace permission checks for now. */
1639 if (!ptrace_may_access(task
, PTRACE_MODE_READ
))
1643 ctx
= perf_lock_task_context(task
, &flags
);
1646 spin_unlock_irqrestore(&ctx
->lock
, flags
);
1650 ctx
= kmalloc(sizeof(struct perf_counter_context
), GFP_KERNEL
);
1654 __perf_counter_init_context(ctx
, task
);
1656 if (cmpxchg(&task
->perf_counter_ctxp
, NULL
, ctx
)) {
1658 * We raced with some other task; use
1659 * the context they set.
1664 get_task_struct(task
);
1667 put_task_struct(task
);
1671 put_task_struct(task
);
1672 return ERR_PTR(err
);
1675 static void free_counter_rcu(struct rcu_head
*head
)
1677 struct perf_counter
*counter
;
1679 counter
= container_of(head
, struct perf_counter
, rcu_head
);
1681 put_pid_ns(counter
->ns
);
1685 static void perf_pending_sync(struct perf_counter
*counter
);
1687 static void free_counter(struct perf_counter
*counter
)
1689 perf_pending_sync(counter
);
1691 if (!counter
->parent
) {
1692 atomic_dec(&nr_counters
);
1693 if (counter
->attr
.mmap
)
1694 atomic_dec(&nr_mmap_counters
);
1695 if (counter
->attr
.comm
)
1696 atomic_dec(&nr_comm_counters
);
1697 if (counter
->attr
.task
)
1698 atomic_dec(&nr_task_counters
);
1701 if (counter
->output
) {
1702 fput(counter
->output
->filp
);
1703 counter
->output
= NULL
;
1706 if (counter
->destroy
)
1707 counter
->destroy(counter
);
1709 put_ctx(counter
->ctx
);
1710 call_rcu(&counter
->rcu_head
, free_counter_rcu
);
1714 * Called when the last reference to the file is gone.
1716 static int perf_release(struct inode
*inode
, struct file
*file
)
1718 struct perf_counter
*counter
= file
->private_data
;
1719 struct perf_counter_context
*ctx
= counter
->ctx
;
1721 file
->private_data
= NULL
;
1723 WARN_ON_ONCE(ctx
->parent_ctx
);
1724 mutex_lock(&ctx
->mutex
);
1725 perf_counter_remove_from_context(counter
);
1726 mutex_unlock(&ctx
->mutex
);
1728 mutex_lock(&counter
->owner
->perf_counter_mutex
);
1729 list_del_init(&counter
->owner_entry
);
1730 mutex_unlock(&counter
->owner
->perf_counter_mutex
);
1731 put_task_struct(counter
->owner
);
1733 free_counter(counter
);
1738 static int perf_counter_read_size(struct perf_counter
*counter
)
1740 int entry
= sizeof(u64
); /* value */
1744 if (counter
->attr
.read_format
& PERF_FORMAT_TOTAL_TIME_ENABLED
)
1745 size
+= sizeof(u64
);
1747 if (counter
->attr
.read_format
& PERF_FORMAT_TOTAL_TIME_RUNNING
)
1748 size
+= sizeof(u64
);
1750 if (counter
->attr
.read_format
& PERF_FORMAT_ID
)
1751 entry
+= sizeof(u64
);
1753 if (counter
->attr
.read_format
& PERF_FORMAT_GROUP
) {
1754 nr
+= counter
->group_leader
->nr_siblings
;
1755 size
+= sizeof(u64
);
1763 static u64
perf_counter_read_value(struct perf_counter
*counter
)
1765 struct perf_counter
*child
;
1768 total
+= perf_counter_read(counter
);
1769 list_for_each_entry(child
, &counter
->child_list
, child_list
)
1770 total
+= perf_counter_read(child
);
1775 static int perf_counter_read_entry(struct perf_counter
*counter
,
1776 u64 read_format
, char __user
*buf
)
1778 int n
= 0, count
= 0;
1781 values
[n
++] = perf_counter_read_value(counter
);
1782 if (read_format
& PERF_FORMAT_ID
)
1783 values
[n
++] = primary_counter_id(counter
);
1785 count
= n
* sizeof(u64
);
1787 if (copy_to_user(buf
, values
, count
))
1793 static int perf_counter_read_group(struct perf_counter
*counter
,
1794 u64 read_format
, char __user
*buf
)
1796 struct perf_counter
*leader
= counter
->group_leader
, *sub
;
1797 int n
= 0, size
= 0, err
= -EFAULT
;
1800 values
[n
++] = 1 + leader
->nr_siblings
;
1801 if (read_format
& PERF_FORMAT_TOTAL_TIME_ENABLED
) {
1802 values
[n
++] = leader
->total_time_enabled
+
1803 atomic64_read(&leader
->child_total_time_enabled
);
1805 if (read_format
& PERF_FORMAT_TOTAL_TIME_RUNNING
) {
1806 values
[n
++] = leader
->total_time_running
+
1807 atomic64_read(&leader
->child_total_time_running
);
1810 size
= n
* sizeof(u64
);
1812 if (copy_to_user(buf
, values
, size
))
1815 err
= perf_counter_read_entry(leader
, read_format
, buf
+ size
);
1821 list_for_each_entry(sub
, &leader
->sibling_list
, list_entry
) {
1822 err
= perf_counter_read_entry(sub
, read_format
,
1833 static int perf_counter_read_one(struct perf_counter
*counter
,
1834 u64 read_format
, char __user
*buf
)
1839 values
[n
++] = perf_counter_read_value(counter
);
1840 if (read_format
& PERF_FORMAT_TOTAL_TIME_ENABLED
) {
1841 values
[n
++] = counter
->total_time_enabled
+
1842 atomic64_read(&counter
->child_total_time_enabled
);
1844 if (read_format
& PERF_FORMAT_TOTAL_TIME_RUNNING
) {
1845 values
[n
++] = counter
->total_time_running
+
1846 atomic64_read(&counter
->child_total_time_running
);
1848 if (read_format
& PERF_FORMAT_ID
)
1849 values
[n
++] = primary_counter_id(counter
);
1851 if (copy_to_user(buf
, values
, n
* sizeof(u64
)))
1854 return n
* sizeof(u64
);
1858 * Read the performance counter - simple non blocking version for now
1861 perf_read_hw(struct perf_counter
*counter
, char __user
*buf
, size_t count
)
1863 u64 read_format
= counter
->attr
.read_format
;
1867 * Return end-of-file for a read on a counter that is in
1868 * error state (i.e. because it was pinned but it couldn't be
1869 * scheduled on to the CPU at some point).
1871 if (counter
->state
== PERF_COUNTER_STATE_ERROR
)
1874 if (count
< perf_counter_read_size(counter
))
1877 WARN_ON_ONCE(counter
->ctx
->parent_ctx
);
1878 mutex_lock(&counter
->child_mutex
);
1879 if (read_format
& PERF_FORMAT_GROUP
)
1880 ret
= perf_counter_read_group(counter
, read_format
, buf
);
1882 ret
= perf_counter_read_one(counter
, read_format
, buf
);
1883 mutex_unlock(&counter
->child_mutex
);
1889 perf_read(struct file
*file
, char __user
*buf
, size_t count
, loff_t
*ppos
)
1891 struct perf_counter
*counter
= file
->private_data
;
1893 return perf_read_hw(counter
, buf
, count
);
1896 static unsigned int perf_poll(struct file
*file
, poll_table
*wait
)
1898 struct perf_counter
*counter
= file
->private_data
;
1899 struct perf_mmap_data
*data
;
1900 unsigned int events
= POLL_HUP
;
1903 data
= rcu_dereference(counter
->data
);
1905 events
= atomic_xchg(&data
->poll
, 0);
1908 poll_wait(file
, &counter
->waitq
, wait
);
1913 static void perf_counter_reset(struct perf_counter
*counter
)
1915 (void)perf_counter_read(counter
);
1916 atomic64_set(&counter
->count
, 0);
1917 perf_counter_update_userpage(counter
);
1921 * Holding the top-level counter's child_mutex means that any
1922 * descendant process that has inherited this counter will block
1923 * in sync_child_counter if it goes to exit, thus satisfying the
1924 * task existence requirements of perf_counter_enable/disable.
1926 static void perf_counter_for_each_child(struct perf_counter
*counter
,
1927 void (*func
)(struct perf_counter
*))
1929 struct perf_counter
*child
;
1931 WARN_ON_ONCE(counter
->ctx
->parent_ctx
);
1932 mutex_lock(&counter
->child_mutex
);
1934 list_for_each_entry(child
, &counter
->child_list
, child_list
)
1936 mutex_unlock(&counter
->child_mutex
);
1939 static void perf_counter_for_each(struct perf_counter
*counter
,
1940 void (*func
)(struct perf_counter
*))
1942 struct perf_counter_context
*ctx
= counter
->ctx
;
1943 struct perf_counter
*sibling
;
1945 WARN_ON_ONCE(ctx
->parent_ctx
);
1946 mutex_lock(&ctx
->mutex
);
1947 counter
= counter
->group_leader
;
1949 perf_counter_for_each_child(counter
, func
);
1951 list_for_each_entry(sibling
, &counter
->sibling_list
, list_entry
)
1952 perf_counter_for_each_child(counter
, func
);
1953 mutex_unlock(&ctx
->mutex
);
1956 static int perf_counter_period(struct perf_counter
*counter
, u64 __user
*arg
)
1958 struct perf_counter_context
*ctx
= counter
->ctx
;
1963 if (!counter
->attr
.sample_period
)
1966 size
= copy_from_user(&value
, arg
, sizeof(value
));
1967 if (size
!= sizeof(value
))
1973 spin_lock_irq(&ctx
->lock
);
1974 if (counter
->attr
.freq
) {
1975 if (value
> sysctl_perf_counter_sample_rate
) {
1980 counter
->attr
.sample_freq
= value
;
1982 counter
->attr
.sample_period
= value
;
1983 counter
->hw
.sample_period
= value
;
1986 spin_unlock_irq(&ctx
->lock
);
1991 int perf_counter_set_output(struct perf_counter
*counter
, int output_fd
);
1993 static long perf_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
1995 struct perf_counter
*counter
= file
->private_data
;
1996 void (*func
)(struct perf_counter
*);
2000 case PERF_COUNTER_IOC_ENABLE
:
2001 func
= perf_counter_enable
;
2003 case PERF_COUNTER_IOC_DISABLE
:
2004 func
= perf_counter_disable
;
2006 case PERF_COUNTER_IOC_RESET
:
2007 func
= perf_counter_reset
;
2010 case PERF_COUNTER_IOC_REFRESH
:
2011 return perf_counter_refresh(counter
, arg
);
2013 case PERF_COUNTER_IOC_PERIOD
:
2014 return perf_counter_period(counter
, (u64 __user
*)arg
);
2016 case PERF_COUNTER_IOC_SET_OUTPUT
:
2017 return perf_counter_set_output(counter
, arg
);
2023 if (flags
& PERF_IOC_FLAG_GROUP
)
2024 perf_counter_for_each(counter
, func
);
2026 perf_counter_for_each_child(counter
, func
);
2031 int perf_counter_task_enable(void)
2033 struct perf_counter
*counter
;
2035 mutex_lock(¤t
->perf_counter_mutex
);
2036 list_for_each_entry(counter
, ¤t
->perf_counter_list
, owner_entry
)
2037 perf_counter_for_each_child(counter
, perf_counter_enable
);
2038 mutex_unlock(¤t
->perf_counter_mutex
);
2043 int perf_counter_task_disable(void)
2045 struct perf_counter
*counter
;
2047 mutex_lock(¤t
->perf_counter_mutex
);
2048 list_for_each_entry(counter
, ¤t
->perf_counter_list
, owner_entry
)
2049 perf_counter_for_each_child(counter
, perf_counter_disable
);
2050 mutex_unlock(¤t
->perf_counter_mutex
);
2055 #ifndef PERF_COUNTER_INDEX_OFFSET
2056 # define PERF_COUNTER_INDEX_OFFSET 0
2059 static int perf_counter_index(struct perf_counter
*counter
)
2061 if (counter
->state
!= PERF_COUNTER_STATE_ACTIVE
)
2064 return counter
->hw
.idx
+ 1 - PERF_COUNTER_INDEX_OFFSET
;
2068 * Callers need to ensure there can be no nesting of this function, otherwise
2069 * the seqlock logic goes bad. We can not serialize this because the arch
2070 * code calls this from NMI context.
2072 void perf_counter_update_userpage(struct perf_counter
*counter
)
2074 struct perf_counter_mmap_page
*userpg
;
2075 struct perf_mmap_data
*data
;
2078 data
= rcu_dereference(counter
->data
);
2082 userpg
= data
->user_page
;
2085 * Disable preemption so as to not let the corresponding user-space
2086 * spin too long if we get preempted.
2091 userpg
->index
= perf_counter_index(counter
);
2092 userpg
->offset
= atomic64_read(&counter
->count
);
2093 if (counter
->state
== PERF_COUNTER_STATE_ACTIVE
)
2094 userpg
->offset
-= atomic64_read(&counter
->hw
.prev_count
);
2096 userpg
->time_enabled
= counter
->total_time_enabled
+
2097 atomic64_read(&counter
->child_total_time_enabled
);
2099 userpg
->time_running
= counter
->total_time_running
+
2100 atomic64_read(&counter
->child_total_time_running
);
2109 static int perf_mmap_fault(struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
2111 struct perf_counter
*counter
= vma
->vm_file
->private_data
;
2112 struct perf_mmap_data
*data
;
2113 int ret
= VM_FAULT_SIGBUS
;
2115 if (vmf
->flags
& FAULT_FLAG_MKWRITE
) {
2116 if (vmf
->pgoff
== 0)
2122 data
= rcu_dereference(counter
->data
);
2126 if (vmf
->pgoff
== 0) {
2127 vmf
->page
= virt_to_page(data
->user_page
);
2129 int nr
= vmf
->pgoff
- 1;
2131 if ((unsigned)nr
> data
->nr_pages
)
2134 if (vmf
->flags
& FAULT_FLAG_WRITE
)
2137 vmf
->page
= virt_to_page(data
->data_pages
[nr
]);
2140 get_page(vmf
->page
);
2141 vmf
->page
->mapping
= vma
->vm_file
->f_mapping
;
2142 vmf
->page
->index
= vmf
->pgoff
;
2151 static int perf_mmap_data_alloc(struct perf_counter
*counter
, int nr_pages
)
2153 struct perf_mmap_data
*data
;
2157 WARN_ON(atomic_read(&counter
->mmap_count
));
2159 size
= sizeof(struct perf_mmap_data
);
2160 size
+= nr_pages
* sizeof(void *);
2162 data
= kzalloc(size
, GFP_KERNEL
);
2166 data
->user_page
= (void *)get_zeroed_page(GFP_KERNEL
);
2167 if (!data
->user_page
)
2168 goto fail_user_page
;
2170 for (i
= 0; i
< nr_pages
; i
++) {
2171 data
->data_pages
[i
] = (void *)get_zeroed_page(GFP_KERNEL
);
2172 if (!data
->data_pages
[i
])
2173 goto fail_data_pages
;
2176 data
->nr_pages
= nr_pages
;
2177 atomic_set(&data
->lock
, -1);
2179 if (counter
->attr
.watermark
) {
2180 data
->watermark
= min_t(long, PAGE_SIZE
* nr_pages
,
2181 counter
->attr
.wakeup_watermark
);
2183 if (!data
->watermark
)
2184 data
->watermark
= max(PAGE_SIZE
, PAGE_SIZE
* nr_pages
/ 4);
2186 rcu_assign_pointer(counter
->data
, data
);
2191 for (i
--; i
>= 0; i
--)
2192 free_page((unsigned long)data
->data_pages
[i
]);
2194 free_page((unsigned long)data
->user_page
);
2203 static void perf_mmap_free_page(unsigned long addr
)
2205 struct page
*page
= virt_to_page((void *)addr
);
2207 page
->mapping
= NULL
;
2211 static void __perf_mmap_data_free(struct rcu_head
*rcu_head
)
2213 struct perf_mmap_data
*data
;
2216 data
= container_of(rcu_head
, struct perf_mmap_data
, rcu_head
);
2218 perf_mmap_free_page((unsigned long)data
->user_page
);
2219 for (i
= 0; i
< data
->nr_pages
; i
++)
2220 perf_mmap_free_page((unsigned long)data
->data_pages
[i
]);
2225 static void perf_mmap_data_free(struct perf_counter
*counter
)
2227 struct perf_mmap_data
*data
= counter
->data
;
2229 WARN_ON(atomic_read(&counter
->mmap_count
));
2231 rcu_assign_pointer(counter
->data
, NULL
);
2232 call_rcu(&data
->rcu_head
, __perf_mmap_data_free
);
2235 static void perf_mmap_open(struct vm_area_struct
*vma
)
2237 struct perf_counter
*counter
= vma
->vm_file
->private_data
;
2239 atomic_inc(&counter
->mmap_count
);
2242 static void perf_mmap_close(struct vm_area_struct
*vma
)
2244 struct perf_counter
*counter
= vma
->vm_file
->private_data
;
2246 WARN_ON_ONCE(counter
->ctx
->parent_ctx
);
2247 if (atomic_dec_and_mutex_lock(&counter
->mmap_count
, &counter
->mmap_mutex
)) {
2248 struct user_struct
*user
= current_user();
2250 atomic_long_sub(counter
->data
->nr_pages
+ 1, &user
->locked_vm
);
2251 vma
->vm_mm
->locked_vm
-= counter
->data
->nr_locked
;
2252 perf_mmap_data_free(counter
);
2253 mutex_unlock(&counter
->mmap_mutex
);
2257 static struct vm_operations_struct perf_mmap_vmops
= {
2258 .open
= perf_mmap_open
,
2259 .close
= perf_mmap_close
,
2260 .fault
= perf_mmap_fault
,
2261 .page_mkwrite
= perf_mmap_fault
,
2264 static int perf_mmap(struct file
*file
, struct vm_area_struct
*vma
)
2266 struct perf_counter
*counter
= file
->private_data
;
2267 unsigned long user_locked
, user_lock_limit
;
2268 struct user_struct
*user
= current_user();
2269 unsigned long locked
, lock_limit
;
2270 unsigned long vma_size
;
2271 unsigned long nr_pages
;
2272 long user_extra
, extra
;
2275 if (!(vma
->vm_flags
& VM_SHARED
))
2278 vma_size
= vma
->vm_end
- vma
->vm_start
;
2279 nr_pages
= (vma_size
/ PAGE_SIZE
) - 1;
2282 * If we have data pages ensure they're a power-of-two number, so we
2283 * can do bitmasks instead of modulo.
2285 if (nr_pages
!= 0 && !is_power_of_2(nr_pages
))
2288 if (vma_size
!= PAGE_SIZE
* (1 + nr_pages
))
2291 if (vma
->vm_pgoff
!= 0)
2294 WARN_ON_ONCE(counter
->ctx
->parent_ctx
);
2295 mutex_lock(&counter
->mmap_mutex
);
2296 if (counter
->output
) {
2301 if (atomic_inc_not_zero(&counter
->mmap_count
)) {
2302 if (nr_pages
!= counter
->data
->nr_pages
)
2307 user_extra
= nr_pages
+ 1;
2308 user_lock_limit
= sysctl_perf_counter_mlock
>> (PAGE_SHIFT
- 10);
2311 * Increase the limit linearly with more CPUs:
2313 user_lock_limit
*= num_online_cpus();
2315 user_locked
= atomic_long_read(&user
->locked_vm
) + user_extra
;
2318 if (user_locked
> user_lock_limit
)
2319 extra
= user_locked
- user_lock_limit
;
2321 lock_limit
= current
->signal
->rlim
[RLIMIT_MEMLOCK
].rlim_cur
;
2322 lock_limit
>>= PAGE_SHIFT
;
2323 locked
= vma
->vm_mm
->locked_vm
+ extra
;
2325 if ((locked
> lock_limit
) && perf_paranoid_tracepoint_raw() &&
2326 !capable(CAP_IPC_LOCK
)) {
2331 WARN_ON(counter
->data
);
2332 ret
= perf_mmap_data_alloc(counter
, nr_pages
);
2336 atomic_set(&counter
->mmap_count
, 1);
2337 atomic_long_add(user_extra
, &user
->locked_vm
);
2338 vma
->vm_mm
->locked_vm
+= extra
;
2339 counter
->data
->nr_locked
= extra
;
2340 if (vma
->vm_flags
& VM_WRITE
)
2341 counter
->data
->writable
= 1;
2344 mutex_unlock(&counter
->mmap_mutex
);
2346 vma
->vm_flags
|= VM_RESERVED
;
2347 vma
->vm_ops
= &perf_mmap_vmops
;
2352 static int perf_fasync(int fd
, struct file
*filp
, int on
)
2354 struct inode
*inode
= filp
->f_path
.dentry
->d_inode
;
2355 struct perf_counter
*counter
= filp
->private_data
;
2358 mutex_lock(&inode
->i_mutex
);
2359 retval
= fasync_helper(fd
, filp
, on
, &counter
->fasync
);
2360 mutex_unlock(&inode
->i_mutex
);
2368 static const struct file_operations perf_fops
= {
2369 .release
= perf_release
,
2372 .unlocked_ioctl
= perf_ioctl
,
2373 .compat_ioctl
= perf_ioctl
,
2375 .fasync
= perf_fasync
,
2379 * Perf counter wakeup
2381 * If there's data, ensure we set the poll() state and publish everything
2382 * to user-space before waking everybody up.
2385 void perf_counter_wakeup(struct perf_counter
*counter
)
2387 wake_up_all(&counter
->waitq
);
2389 if (counter
->pending_kill
) {
2390 kill_fasync(&counter
->fasync
, SIGIO
, counter
->pending_kill
);
2391 counter
->pending_kill
= 0;
2398 * Handle the case where we need to wakeup up from NMI (or rq->lock) context.
2400 * The NMI bit means we cannot possibly take locks. Therefore, maintain a
2401 * single linked list and use cmpxchg() to add entries lockless.
2404 static void perf_pending_counter(struct perf_pending_entry
*entry
)
2406 struct perf_counter
*counter
= container_of(entry
,
2407 struct perf_counter
, pending
);
2409 if (counter
->pending_disable
) {
2410 counter
->pending_disable
= 0;
2411 __perf_counter_disable(counter
);
2414 if (counter
->pending_wakeup
) {
2415 counter
->pending_wakeup
= 0;
2416 perf_counter_wakeup(counter
);
2420 #define PENDING_TAIL ((struct perf_pending_entry *)-1UL)
2422 static DEFINE_PER_CPU(struct perf_pending_entry
*, perf_pending_head
) = {
2426 static void perf_pending_queue(struct perf_pending_entry
*entry
,
2427 void (*func
)(struct perf_pending_entry
*))
2429 struct perf_pending_entry
**head
;
2431 if (cmpxchg(&entry
->next
, NULL
, PENDING_TAIL
) != NULL
)
2436 head
= &get_cpu_var(perf_pending_head
);
2439 entry
->next
= *head
;
2440 } while (cmpxchg(head
, entry
->next
, entry
) != entry
->next
);
2442 set_perf_counter_pending();
2444 put_cpu_var(perf_pending_head
);
2447 static int __perf_pending_run(void)
2449 struct perf_pending_entry
*list
;
2452 list
= xchg(&__get_cpu_var(perf_pending_head
), PENDING_TAIL
);
2453 while (list
!= PENDING_TAIL
) {
2454 void (*func
)(struct perf_pending_entry
*);
2455 struct perf_pending_entry
*entry
= list
;
2462 * Ensure we observe the unqueue before we issue the wakeup,
2463 * so that we won't be waiting forever.
2464 * -- see perf_not_pending().
2475 static inline int perf_not_pending(struct perf_counter
*counter
)
2478 * If we flush on whatever cpu we run, there is a chance we don't
2482 __perf_pending_run();
2486 * Ensure we see the proper queue state before going to sleep
2487 * so that we do not miss the wakeup. -- see perf_pending_handle()
2490 return counter
->pending
.next
== NULL
;
2493 static void perf_pending_sync(struct perf_counter
*counter
)
2495 wait_event(counter
->waitq
, perf_not_pending(counter
));
2498 void perf_counter_do_pending(void)
2500 __perf_pending_run();
2504 * Callchain support -- arch specific
2507 __weak
struct perf_callchain_entry
*perf_callchain(struct pt_regs
*regs
)
2516 struct perf_output_handle
{
2517 struct perf_counter
*counter
;
2518 struct perf_mmap_data
*data
;
2520 unsigned long offset
;
2524 unsigned long flags
;
2527 static bool perf_output_space(struct perf_mmap_data
*data
, unsigned long tail
,
2528 unsigned long offset
, unsigned long head
)
2532 if (!data
->writable
)
2535 mask
= (data
->nr_pages
<< PAGE_SHIFT
) - 1;
2537 offset
= (offset
- tail
) & mask
;
2538 head
= (head
- tail
) & mask
;
2540 if ((int)(head
- offset
) < 0)
2546 static void perf_output_wakeup(struct perf_output_handle
*handle
)
2548 atomic_set(&handle
->data
->poll
, POLL_IN
);
2551 handle
->counter
->pending_wakeup
= 1;
2552 perf_pending_queue(&handle
->counter
->pending
,
2553 perf_pending_counter
);
2555 perf_counter_wakeup(handle
->counter
);
2559 * Curious locking construct.
2561 * We need to ensure a later event doesn't publish a head when a former
2562 * event isn't done writing. However since we need to deal with NMIs we
2563 * cannot fully serialize things.
2565 * What we do is serialize between CPUs so we only have to deal with NMI
2566 * nesting on a single CPU.
2568 * We only publish the head (and generate a wakeup) when the outer-most
2571 static void perf_output_lock(struct perf_output_handle
*handle
)
2573 struct perf_mmap_data
*data
= handle
->data
;
2578 local_irq_save(handle
->flags
);
2579 cpu
= smp_processor_id();
2581 if (in_nmi() && atomic_read(&data
->lock
) == cpu
)
2584 while (atomic_cmpxchg(&data
->lock
, -1, cpu
) != -1)
2590 static void perf_output_unlock(struct perf_output_handle
*handle
)
2592 struct perf_mmap_data
*data
= handle
->data
;
2596 data
->done_head
= data
->head
;
2598 if (!handle
->locked
)
2603 * The xchg implies a full barrier that ensures all writes are done
2604 * before we publish the new head, matched by a rmb() in userspace when
2605 * reading this position.
2607 while ((head
= atomic_long_xchg(&data
->done_head
, 0)))
2608 data
->user_page
->data_head
= head
;
2611 * NMI can happen here, which means we can miss a done_head update.
2614 cpu
= atomic_xchg(&data
->lock
, -1);
2615 WARN_ON_ONCE(cpu
!= smp_processor_id());
2618 * Therefore we have to validate we did not indeed do so.
2620 if (unlikely(atomic_long_read(&data
->done_head
))) {
2622 * Since we had it locked, we can lock it again.
2624 while (atomic_cmpxchg(&data
->lock
, -1, cpu
) != -1)
2630 if (atomic_xchg(&data
->wakeup
, 0))
2631 perf_output_wakeup(handle
);
2633 local_irq_restore(handle
->flags
);
2636 static void perf_output_copy(struct perf_output_handle
*handle
,
2637 const void *buf
, unsigned int len
)
2639 unsigned int pages_mask
;
2640 unsigned int offset
;
2644 offset
= handle
->offset
;
2645 pages_mask
= handle
->data
->nr_pages
- 1;
2646 pages
= handle
->data
->data_pages
;
2649 unsigned int page_offset
;
2652 nr
= (offset
>> PAGE_SHIFT
) & pages_mask
;
2653 page_offset
= offset
& (PAGE_SIZE
- 1);
2654 size
= min_t(unsigned int, PAGE_SIZE
- page_offset
, len
);
2656 memcpy(pages
[nr
] + page_offset
, buf
, size
);
2663 handle
->offset
= offset
;
2666 * Check we didn't copy past our reservation window, taking the
2667 * possible unsigned int wrap into account.
2669 WARN_ON_ONCE(((long)(handle
->head
- handle
->offset
)) < 0);
2672 #define perf_output_put(handle, x) \
2673 perf_output_copy((handle), &(x), sizeof(x))
2675 static int perf_output_begin(struct perf_output_handle
*handle
,
2676 struct perf_counter
*counter
, unsigned int size
,
2677 int nmi
, int sample
)
2679 struct perf_counter
*output_counter
;
2680 struct perf_mmap_data
*data
;
2681 unsigned long tail
, offset
, head
;
2684 struct perf_event_header header
;
2691 * For inherited counters we send all the output towards the parent.
2693 if (counter
->parent
)
2694 counter
= counter
->parent
;
2696 output_counter
= rcu_dereference(counter
->output
);
2698 counter
= output_counter
;
2700 data
= rcu_dereference(counter
->data
);
2704 handle
->data
= data
;
2705 handle
->counter
= counter
;
2707 handle
->sample
= sample
;
2709 if (!data
->nr_pages
)
2712 have_lost
= atomic_read(&data
->lost
);
2714 size
+= sizeof(lost_event
);
2716 perf_output_lock(handle
);
2720 * Userspace could choose to issue a mb() before updating the
2721 * tail pointer. So that all reads will be completed before the
2724 tail
= ACCESS_ONCE(data
->user_page
->data_tail
);
2726 offset
= head
= atomic_long_read(&data
->head
);
2728 if (unlikely(!perf_output_space(data
, tail
, offset
, head
)))
2730 } while (atomic_long_cmpxchg(&data
->head
, offset
, head
) != offset
);
2732 handle
->offset
= offset
;
2733 handle
->head
= head
;
2735 if (head
- tail
> data
->watermark
)
2736 atomic_set(&data
->wakeup
, 1);
2739 lost_event
.header
.type
= PERF_EVENT_LOST
;
2740 lost_event
.header
.misc
= 0;
2741 lost_event
.header
.size
= sizeof(lost_event
);
2742 lost_event
.id
= counter
->id
;
2743 lost_event
.lost
= atomic_xchg(&data
->lost
, 0);
2745 perf_output_put(handle
, lost_event
);
2751 atomic_inc(&data
->lost
);
2752 perf_output_unlock(handle
);
2759 static void perf_output_end(struct perf_output_handle
*handle
)
2761 struct perf_counter
*counter
= handle
->counter
;
2762 struct perf_mmap_data
*data
= handle
->data
;
2764 int wakeup_events
= counter
->attr
.wakeup_events
;
2766 if (handle
->sample
&& wakeup_events
) {
2767 int events
= atomic_inc_return(&data
->events
);
2768 if (events
>= wakeup_events
) {
2769 atomic_sub(wakeup_events
, &data
->events
);
2770 atomic_set(&data
->wakeup
, 1);
2774 perf_output_unlock(handle
);
2778 static u32
perf_counter_pid(struct perf_counter
*counter
, struct task_struct
*p
)
2781 * only top level counters have the pid namespace they were created in
2783 if (counter
->parent
)
2784 counter
= counter
->parent
;
2786 return task_tgid_nr_ns(p
, counter
->ns
);
2789 static u32
perf_counter_tid(struct perf_counter
*counter
, struct task_struct
*p
)
2792 * only top level counters have the pid namespace they were created in
2794 if (counter
->parent
)
2795 counter
= counter
->parent
;
2797 return task_pid_nr_ns(p
, counter
->ns
);
2800 static void perf_output_read_one(struct perf_output_handle
*handle
,
2801 struct perf_counter
*counter
)
2803 u64 read_format
= counter
->attr
.read_format
;
2807 values
[n
++] = atomic64_read(&counter
->count
);
2808 if (read_format
& PERF_FORMAT_TOTAL_TIME_ENABLED
) {
2809 values
[n
++] = counter
->total_time_enabled
+
2810 atomic64_read(&counter
->child_total_time_enabled
);
2812 if (read_format
& PERF_FORMAT_TOTAL_TIME_RUNNING
) {
2813 values
[n
++] = counter
->total_time_running
+
2814 atomic64_read(&counter
->child_total_time_running
);
2816 if (read_format
& PERF_FORMAT_ID
)
2817 values
[n
++] = primary_counter_id(counter
);
2819 perf_output_copy(handle
, values
, n
* sizeof(u64
));
2823 * XXX PERF_FORMAT_GROUP vs inherited counters seems difficult.
2825 static void perf_output_read_group(struct perf_output_handle
*handle
,
2826 struct perf_counter
*counter
)
2828 struct perf_counter
*leader
= counter
->group_leader
, *sub
;
2829 u64 read_format
= counter
->attr
.read_format
;
2833 values
[n
++] = 1 + leader
->nr_siblings
;
2835 if (read_format
& PERF_FORMAT_TOTAL_TIME_ENABLED
)
2836 values
[n
++] = leader
->total_time_enabled
;
2838 if (read_format
& PERF_FORMAT_TOTAL_TIME_RUNNING
)
2839 values
[n
++] = leader
->total_time_running
;
2841 if (leader
!= counter
)
2842 leader
->pmu
->read(leader
);
2844 values
[n
++] = atomic64_read(&leader
->count
);
2845 if (read_format
& PERF_FORMAT_ID
)
2846 values
[n
++] = primary_counter_id(leader
);
2848 perf_output_copy(handle
, values
, n
* sizeof(u64
));
2850 list_for_each_entry(sub
, &leader
->sibling_list
, list_entry
) {
2854 sub
->pmu
->read(sub
);
2856 values
[n
++] = atomic64_read(&sub
->count
);
2857 if (read_format
& PERF_FORMAT_ID
)
2858 values
[n
++] = primary_counter_id(sub
);
2860 perf_output_copy(handle
, values
, n
* sizeof(u64
));
2864 static void perf_output_read(struct perf_output_handle
*handle
,
2865 struct perf_counter
*counter
)
2867 if (counter
->attr
.read_format
& PERF_FORMAT_GROUP
)
2868 perf_output_read_group(handle
, counter
);
2870 perf_output_read_one(handle
, counter
);
2873 void perf_counter_output(struct perf_counter
*counter
, int nmi
,
2874 struct perf_sample_data
*data
)
2877 u64 sample_type
= counter
->attr
.sample_type
;
2878 struct perf_output_handle handle
;
2879 struct perf_event_header header
;
2884 struct perf_callchain_entry
*callchain
= NULL
;
2885 int callchain_size
= 0;
2891 header
.type
= PERF_EVENT_SAMPLE
;
2892 header
.size
= sizeof(header
);
2895 header
.misc
|= perf_misc_flags(data
->regs
);
2897 if (sample_type
& PERF_SAMPLE_IP
) {
2898 ip
= perf_instruction_pointer(data
->regs
);
2899 header
.size
+= sizeof(ip
);
2902 if (sample_type
& PERF_SAMPLE_TID
) {
2903 /* namespace issues */
2904 tid_entry
.pid
= perf_counter_pid(counter
, current
);
2905 tid_entry
.tid
= perf_counter_tid(counter
, current
);
2907 header
.size
+= sizeof(tid_entry
);
2910 if (sample_type
& PERF_SAMPLE_TIME
) {
2912 * Maybe do better on x86 and provide cpu_clock_nmi()
2914 time
= sched_clock();
2916 header
.size
+= sizeof(u64
);
2919 if (sample_type
& PERF_SAMPLE_ADDR
)
2920 header
.size
+= sizeof(u64
);
2922 if (sample_type
& PERF_SAMPLE_ID
)
2923 header
.size
+= sizeof(u64
);
2925 if (sample_type
& PERF_SAMPLE_STREAM_ID
)
2926 header
.size
+= sizeof(u64
);
2928 if (sample_type
& PERF_SAMPLE_CPU
) {
2929 header
.size
+= sizeof(cpu_entry
);
2931 cpu_entry
.cpu
= raw_smp_processor_id();
2932 cpu_entry
.reserved
= 0;
2935 if (sample_type
& PERF_SAMPLE_PERIOD
)
2936 header
.size
+= sizeof(u64
);
2938 if (sample_type
& PERF_SAMPLE_READ
)
2939 header
.size
+= perf_counter_read_size(counter
);
2941 if (sample_type
& PERF_SAMPLE_CALLCHAIN
) {
2942 callchain
= perf_callchain(data
->regs
);
2945 callchain_size
= (1 + callchain
->nr
) * sizeof(u64
);
2946 header
.size
+= callchain_size
;
2948 header
.size
+= sizeof(u64
);
2951 if (sample_type
& PERF_SAMPLE_RAW
) {
2952 int size
= sizeof(u32
);
2955 size
+= data
->raw
->size
;
2957 size
+= sizeof(u32
);
2959 WARN_ON_ONCE(size
& (sizeof(u64
)-1));
2960 header
.size
+= size
;
2963 ret
= perf_output_begin(&handle
, counter
, header
.size
, nmi
, 1);
2967 perf_output_put(&handle
, header
);
2969 if (sample_type
& PERF_SAMPLE_IP
)
2970 perf_output_put(&handle
, ip
);
2972 if (sample_type
& PERF_SAMPLE_TID
)
2973 perf_output_put(&handle
, tid_entry
);
2975 if (sample_type
& PERF_SAMPLE_TIME
)
2976 perf_output_put(&handle
, time
);
2978 if (sample_type
& PERF_SAMPLE_ADDR
)
2979 perf_output_put(&handle
, data
->addr
);
2981 if (sample_type
& PERF_SAMPLE_ID
) {
2982 u64 id
= primary_counter_id(counter
);
2984 perf_output_put(&handle
, id
);
2987 if (sample_type
& PERF_SAMPLE_STREAM_ID
)
2988 perf_output_put(&handle
, counter
->id
);
2990 if (sample_type
& PERF_SAMPLE_CPU
)
2991 perf_output_put(&handle
, cpu_entry
);
2993 if (sample_type
& PERF_SAMPLE_PERIOD
)
2994 perf_output_put(&handle
, data
->period
);
2996 if (sample_type
& PERF_SAMPLE_READ
)
2997 perf_output_read(&handle
, counter
);
2999 if (sample_type
& PERF_SAMPLE_CALLCHAIN
) {
3001 perf_output_copy(&handle
, callchain
, callchain_size
);
3004 perf_output_put(&handle
, nr
);
3008 if (sample_type
& PERF_SAMPLE_RAW
) {
3010 perf_output_put(&handle
, data
->raw
->size
);
3011 perf_output_copy(&handle
, data
->raw
->data
, data
->raw
->size
);
3017 .size
= sizeof(u32
),
3020 perf_output_put(&handle
, raw
);
3024 perf_output_end(&handle
);
3031 struct perf_read_event
{
3032 struct perf_event_header header
;
3039 perf_counter_read_event(struct perf_counter
*counter
,
3040 struct task_struct
*task
)
3042 struct perf_output_handle handle
;
3043 struct perf_read_event event
= {
3045 .type
= PERF_EVENT_READ
,
3047 .size
= sizeof(event
) + perf_counter_read_size(counter
),
3049 .pid
= perf_counter_pid(counter
, task
),
3050 .tid
= perf_counter_tid(counter
, task
),
3054 ret
= perf_output_begin(&handle
, counter
, event
.header
.size
, 0, 0);
3058 perf_output_put(&handle
, event
);
3059 perf_output_read(&handle
, counter
);
3061 perf_output_end(&handle
);
3065 * task tracking -- fork/exit
3067 * enabled by: attr.comm | attr.mmap | attr.task
3070 struct perf_task_event
{
3071 struct task_struct
*task
;
3072 struct perf_counter_context
*task_ctx
;
3075 struct perf_event_header header
;
3084 static void perf_counter_task_output(struct perf_counter
*counter
,
3085 struct perf_task_event
*task_event
)
3087 struct perf_output_handle handle
;
3088 int size
= task_event
->event
.header
.size
;
3089 struct task_struct
*task
= task_event
->task
;
3090 int ret
= perf_output_begin(&handle
, counter
, size
, 0, 0);
3095 task_event
->event
.pid
= perf_counter_pid(counter
, task
);
3096 task_event
->event
.ppid
= perf_counter_pid(counter
, current
);
3098 task_event
->event
.tid
= perf_counter_tid(counter
, task
);
3099 task_event
->event
.ptid
= perf_counter_tid(counter
, current
);
3101 perf_output_put(&handle
, task_event
->event
);
3102 perf_output_end(&handle
);
3105 static int perf_counter_task_match(struct perf_counter
*counter
)
3107 if (counter
->attr
.comm
|| counter
->attr
.mmap
|| counter
->attr
.task
)
3113 static void perf_counter_task_ctx(struct perf_counter_context
*ctx
,
3114 struct perf_task_event
*task_event
)
3116 struct perf_counter
*counter
;
3118 if (system_state
!= SYSTEM_RUNNING
|| list_empty(&ctx
->event_list
))
3122 list_for_each_entry_rcu(counter
, &ctx
->event_list
, event_entry
) {
3123 if (perf_counter_task_match(counter
))
3124 perf_counter_task_output(counter
, task_event
);
3129 static void perf_counter_task_event(struct perf_task_event
*task_event
)
3131 struct perf_cpu_context
*cpuctx
;
3132 struct perf_counter_context
*ctx
= task_event
->task_ctx
;
3134 cpuctx
= &get_cpu_var(perf_cpu_context
);
3135 perf_counter_task_ctx(&cpuctx
->ctx
, task_event
);
3136 put_cpu_var(perf_cpu_context
);
3140 ctx
= rcu_dereference(task_event
->task
->perf_counter_ctxp
);
3142 perf_counter_task_ctx(ctx
, task_event
);
3146 static void perf_counter_task(struct task_struct
*task
,
3147 struct perf_counter_context
*task_ctx
,
3150 struct perf_task_event task_event
;
3152 if (!atomic_read(&nr_comm_counters
) &&
3153 !atomic_read(&nr_mmap_counters
) &&
3154 !atomic_read(&nr_task_counters
))
3157 task_event
= (struct perf_task_event
){
3159 .task_ctx
= task_ctx
,
3162 .type
= new ? PERF_EVENT_FORK
: PERF_EVENT_EXIT
,
3164 .size
= sizeof(task_event
.event
),
3173 perf_counter_task_event(&task_event
);
3176 void perf_counter_fork(struct task_struct
*task
)
3178 perf_counter_task(task
, NULL
, 1);
3185 struct perf_comm_event
{
3186 struct task_struct
*task
;
3191 struct perf_event_header header
;
3198 static void perf_counter_comm_output(struct perf_counter
*counter
,
3199 struct perf_comm_event
*comm_event
)
3201 struct perf_output_handle handle
;
3202 int size
= comm_event
->event
.header
.size
;
3203 int ret
= perf_output_begin(&handle
, counter
, size
, 0, 0);
3208 comm_event
->event
.pid
= perf_counter_pid(counter
, comm_event
->task
);
3209 comm_event
->event
.tid
= perf_counter_tid(counter
, comm_event
->task
);
3211 perf_output_put(&handle
, comm_event
->event
);
3212 perf_output_copy(&handle
, comm_event
->comm
,
3213 comm_event
->comm_size
);
3214 perf_output_end(&handle
);
3217 static int perf_counter_comm_match(struct perf_counter
*counter
)
3219 if (counter
->attr
.comm
)
3225 static void perf_counter_comm_ctx(struct perf_counter_context
*ctx
,
3226 struct perf_comm_event
*comm_event
)
3228 struct perf_counter
*counter
;
3230 if (system_state
!= SYSTEM_RUNNING
|| list_empty(&ctx
->event_list
))
3234 list_for_each_entry_rcu(counter
, &ctx
->event_list
, event_entry
) {
3235 if (perf_counter_comm_match(counter
))
3236 perf_counter_comm_output(counter
, comm_event
);
3241 static void perf_counter_comm_event(struct perf_comm_event
*comm_event
)
3243 struct perf_cpu_context
*cpuctx
;
3244 struct perf_counter_context
*ctx
;
3246 char comm
[TASK_COMM_LEN
];
3248 memset(comm
, 0, sizeof(comm
));
3249 strncpy(comm
, comm_event
->task
->comm
, sizeof(comm
));
3250 size
= ALIGN(strlen(comm
)+1, sizeof(u64
));
3252 comm_event
->comm
= comm
;
3253 comm_event
->comm_size
= size
;
3255 comm_event
->event
.header
.size
= sizeof(comm_event
->event
) + size
;
3257 cpuctx
= &get_cpu_var(perf_cpu_context
);
3258 perf_counter_comm_ctx(&cpuctx
->ctx
, comm_event
);
3259 put_cpu_var(perf_cpu_context
);
3263 * doesn't really matter which of the child contexts the
3264 * events ends up in.
3266 ctx
= rcu_dereference(current
->perf_counter_ctxp
);
3268 perf_counter_comm_ctx(ctx
, comm_event
);
3272 void perf_counter_comm(struct task_struct
*task
)
3274 struct perf_comm_event comm_event
;
3276 if (task
->perf_counter_ctxp
)
3277 perf_counter_enable_on_exec(task
);
3279 if (!atomic_read(&nr_comm_counters
))
3282 comm_event
= (struct perf_comm_event
){
3288 .type
= PERF_EVENT_COMM
,
3297 perf_counter_comm_event(&comm_event
);
3304 struct perf_mmap_event
{
3305 struct vm_area_struct
*vma
;
3307 const char *file_name
;
3311 struct perf_event_header header
;
3321 static void perf_counter_mmap_output(struct perf_counter
*counter
,
3322 struct perf_mmap_event
*mmap_event
)
3324 struct perf_output_handle handle
;
3325 int size
= mmap_event
->event
.header
.size
;
3326 int ret
= perf_output_begin(&handle
, counter
, size
, 0, 0);
3331 mmap_event
->event
.pid
= perf_counter_pid(counter
, current
);
3332 mmap_event
->event
.tid
= perf_counter_tid(counter
, current
);
3334 perf_output_put(&handle
, mmap_event
->event
);
3335 perf_output_copy(&handle
, mmap_event
->file_name
,
3336 mmap_event
->file_size
);
3337 perf_output_end(&handle
);
3340 static int perf_counter_mmap_match(struct perf_counter
*counter
,
3341 struct perf_mmap_event
*mmap_event
)
3343 if (counter
->attr
.mmap
)
3349 static void perf_counter_mmap_ctx(struct perf_counter_context
*ctx
,
3350 struct perf_mmap_event
*mmap_event
)
3352 struct perf_counter
*counter
;
3354 if (system_state
!= SYSTEM_RUNNING
|| list_empty(&ctx
->event_list
))
3358 list_for_each_entry_rcu(counter
, &ctx
->event_list
, event_entry
) {
3359 if (perf_counter_mmap_match(counter
, mmap_event
))
3360 perf_counter_mmap_output(counter
, mmap_event
);
3365 static void perf_counter_mmap_event(struct perf_mmap_event
*mmap_event
)
3367 struct perf_cpu_context
*cpuctx
;
3368 struct perf_counter_context
*ctx
;
3369 struct vm_area_struct
*vma
= mmap_event
->vma
;
3370 struct file
*file
= vma
->vm_file
;
3376 memset(tmp
, 0, sizeof(tmp
));
3380 * d_path works from the end of the buffer backwards, so we
3381 * need to add enough zero bytes after the string to handle
3382 * the 64bit alignment we do later.
3384 buf
= kzalloc(PATH_MAX
+ sizeof(u64
), GFP_KERNEL
);
3386 name
= strncpy(tmp
, "//enomem", sizeof(tmp
));
3389 name
= d_path(&file
->f_path
, buf
, PATH_MAX
);
3391 name
= strncpy(tmp
, "//toolong", sizeof(tmp
));
3395 if (arch_vma_name(mmap_event
->vma
)) {
3396 name
= strncpy(tmp
, arch_vma_name(mmap_event
->vma
),
3402 name
= strncpy(tmp
, "[vdso]", sizeof(tmp
));
3406 name
= strncpy(tmp
, "//anon", sizeof(tmp
));
3411 size
= ALIGN(strlen(name
)+1, sizeof(u64
));
3413 mmap_event
->file_name
= name
;
3414 mmap_event
->file_size
= size
;
3416 mmap_event
->event
.header
.size
= sizeof(mmap_event
->event
) + size
;
3418 cpuctx
= &get_cpu_var(perf_cpu_context
);
3419 perf_counter_mmap_ctx(&cpuctx
->ctx
, mmap_event
);
3420 put_cpu_var(perf_cpu_context
);
3424 * doesn't really matter which of the child contexts the
3425 * events ends up in.
3427 ctx
= rcu_dereference(current
->perf_counter_ctxp
);
3429 perf_counter_mmap_ctx(ctx
, mmap_event
);
3435 void __perf_counter_mmap(struct vm_area_struct
*vma
)
3437 struct perf_mmap_event mmap_event
;
3439 if (!atomic_read(&nr_mmap_counters
))
3442 mmap_event
= (struct perf_mmap_event
){
3448 .type
= PERF_EVENT_MMAP
,
3454 .start
= vma
->vm_start
,
3455 .len
= vma
->vm_end
- vma
->vm_start
,
3456 .pgoff
= vma
->vm_pgoff
,
3460 perf_counter_mmap_event(&mmap_event
);
3464 * IRQ throttle logging
3467 static void perf_log_throttle(struct perf_counter
*counter
, int enable
)
3469 struct perf_output_handle handle
;
3473 struct perf_event_header header
;
3477 } throttle_event
= {
3479 .type
= PERF_EVENT_THROTTLE
,
3481 .size
= sizeof(throttle_event
),
3483 .time
= sched_clock(),
3484 .id
= primary_counter_id(counter
),
3485 .stream_id
= counter
->id
,
3489 throttle_event
.header
.type
= PERF_EVENT_UNTHROTTLE
;
3491 ret
= perf_output_begin(&handle
, counter
, sizeof(throttle_event
), 1, 0);
3495 perf_output_put(&handle
, throttle_event
);
3496 perf_output_end(&handle
);
3500 * Generic counter overflow handling, sampling.
3503 static int __perf_counter_overflow(struct perf_counter
*counter
, int nmi
,
3504 int throttle
, struct perf_sample_data
*data
)
3506 int events
= atomic_read(&counter
->event_limit
);
3507 struct hw_perf_counter
*hwc
= &counter
->hw
;
3510 throttle
= (throttle
&& counter
->pmu
->unthrottle
!= NULL
);
3515 if (hwc
->interrupts
!= MAX_INTERRUPTS
) {
3517 if (HZ
* hwc
->interrupts
>
3518 (u64
)sysctl_perf_counter_sample_rate
) {
3519 hwc
->interrupts
= MAX_INTERRUPTS
;
3520 perf_log_throttle(counter
, 0);
3525 * Keep re-disabling counters even though on the previous
3526 * pass we disabled it - just in case we raced with a
3527 * sched-in and the counter got enabled again:
3533 if (counter
->attr
.freq
) {
3534 u64 now
= sched_clock();
3535 s64 delta
= now
- hwc
->freq_stamp
;
3537 hwc
->freq_stamp
= now
;
3539 if (delta
> 0 && delta
< TICK_NSEC
)
3540 perf_adjust_period(counter
, NSEC_PER_SEC
/ (int)delta
);
3544 * XXX event_limit might not quite work as expected on inherited
3548 counter
->pending_kill
= POLL_IN
;
3549 if (events
&& atomic_dec_and_test(&counter
->event_limit
)) {
3551 counter
->pending_kill
= POLL_HUP
;
3553 counter
->pending_disable
= 1;
3554 perf_pending_queue(&counter
->pending
,
3555 perf_pending_counter
);
3557 perf_counter_disable(counter
);
3560 perf_counter_output(counter
, nmi
, data
);
3564 int perf_counter_overflow(struct perf_counter
*counter
, int nmi
,
3565 struct perf_sample_data
*data
)
3567 return __perf_counter_overflow(counter
, nmi
, 1, data
);
3571 * Generic software counter infrastructure
3575 * We directly increment counter->count and keep a second value in
3576 * counter->hw.period_left to count intervals. This period counter
3577 * is kept in the range [-sample_period, 0] so that we can use the
3581 static u64
perf_swcounter_set_period(struct perf_counter
*counter
)
3583 struct hw_perf_counter
*hwc
= &counter
->hw
;
3584 u64 period
= hwc
->last_period
;
3588 hwc
->last_period
= hwc
->sample_period
;
3591 old
= val
= atomic64_read(&hwc
->period_left
);
3595 nr
= div64_u64(period
+ val
, period
);
3596 offset
= nr
* period
;
3598 if (atomic64_cmpxchg(&hwc
->period_left
, old
, val
) != old
)
3604 static void perf_swcounter_overflow(struct perf_counter
*counter
,
3605 int nmi
, struct perf_sample_data
*data
)
3607 struct hw_perf_counter
*hwc
= &counter
->hw
;
3611 data
->period
= counter
->hw
.last_period
;
3612 overflow
= perf_swcounter_set_period(counter
);
3614 if (hwc
->interrupts
== MAX_INTERRUPTS
)
3617 for (; overflow
; overflow
--) {
3618 if (__perf_counter_overflow(counter
, nmi
, throttle
, data
)) {
3620 * We inhibit the overflow from happening when
3621 * hwc->interrupts == MAX_INTERRUPTS.
3629 static void perf_swcounter_unthrottle(struct perf_counter
*counter
)
3632 * Nothing to do, we already reset hwc->interrupts.
3636 static void perf_swcounter_add(struct perf_counter
*counter
, u64 nr
,
3637 int nmi
, struct perf_sample_data
*data
)
3639 struct hw_perf_counter
*hwc
= &counter
->hw
;
3641 atomic64_add(nr
, &counter
->count
);
3643 if (!hwc
->sample_period
)
3649 if (!atomic64_add_negative(nr
, &hwc
->period_left
))
3650 perf_swcounter_overflow(counter
, nmi
, data
);
3653 static int perf_swcounter_is_counting(struct perf_counter
*counter
)
3656 * The counter is active, we're good!
3658 if (counter
->state
== PERF_COUNTER_STATE_ACTIVE
)
3662 * The counter is off/error, not counting.
3664 if (counter
->state
!= PERF_COUNTER_STATE_INACTIVE
)
3668 * The counter is inactive, if the context is active
3669 * we're part of a group that didn't make it on the 'pmu',
3672 if (counter
->ctx
->is_active
)
3676 * We're inactive and the context is too, this means the
3677 * task is scheduled out, we're counting events that happen
3678 * to us, like migration events.
3683 static int perf_swcounter_match(struct perf_counter
*counter
,
3684 enum perf_type_id type
,
3685 u32 event
, struct pt_regs
*regs
)
3687 if (!perf_swcounter_is_counting(counter
))
3690 if (counter
->attr
.type
!= type
)
3692 if (counter
->attr
.config
!= event
)
3696 if (counter
->attr
.exclude_user
&& user_mode(regs
))
3699 if (counter
->attr
.exclude_kernel
&& !user_mode(regs
))
3706 static void perf_swcounter_ctx_event(struct perf_counter_context
*ctx
,
3707 enum perf_type_id type
,
3708 u32 event
, u64 nr
, int nmi
,
3709 struct perf_sample_data
*data
)
3711 struct perf_counter
*counter
;
3713 if (system_state
!= SYSTEM_RUNNING
|| list_empty(&ctx
->event_list
))
3717 list_for_each_entry_rcu(counter
, &ctx
->event_list
, event_entry
) {
3718 if (perf_swcounter_match(counter
, type
, event
, data
->regs
))
3719 perf_swcounter_add(counter
, nr
, nmi
, data
);
3724 static int *perf_swcounter_recursion_context(struct perf_cpu_context
*cpuctx
)
3727 return &cpuctx
->recursion
[3];
3730 return &cpuctx
->recursion
[2];
3733 return &cpuctx
->recursion
[1];
3735 return &cpuctx
->recursion
[0];
3738 static void do_perf_swcounter_event(enum perf_type_id type
, u32 event
,
3740 struct perf_sample_data
*data
)
3742 struct perf_cpu_context
*cpuctx
= &get_cpu_var(perf_cpu_context
);
3743 int *recursion
= perf_swcounter_recursion_context(cpuctx
);
3744 struct perf_counter_context
*ctx
;
3752 perf_swcounter_ctx_event(&cpuctx
->ctx
, type
, event
,
3756 * doesn't really matter which of the child contexts the
3757 * events ends up in.
3759 ctx
= rcu_dereference(current
->perf_counter_ctxp
);
3761 perf_swcounter_ctx_event(ctx
, type
, event
, nr
, nmi
, data
);
3768 put_cpu_var(perf_cpu_context
);
3771 void __perf_swcounter_event(u32 event
, u64 nr
, int nmi
,
3772 struct pt_regs
*regs
, u64 addr
)
3774 struct perf_sample_data data
= {
3779 do_perf_swcounter_event(PERF_TYPE_SOFTWARE
, event
, nr
, nmi
, &data
);
3782 static void perf_swcounter_read(struct perf_counter
*counter
)
3786 static int perf_swcounter_enable(struct perf_counter
*counter
)
3788 struct hw_perf_counter
*hwc
= &counter
->hw
;
3790 if (hwc
->sample_period
) {
3791 hwc
->last_period
= hwc
->sample_period
;
3792 perf_swcounter_set_period(counter
);
3797 static void perf_swcounter_disable(struct perf_counter
*counter
)
3801 static const struct pmu perf_ops_generic
= {
3802 .enable
= perf_swcounter_enable
,
3803 .disable
= perf_swcounter_disable
,
3804 .read
= perf_swcounter_read
,
3805 .unthrottle
= perf_swcounter_unthrottle
,
3809 * hrtimer based swcounter callback
3812 static enum hrtimer_restart
perf_swcounter_hrtimer(struct hrtimer
*hrtimer
)
3814 enum hrtimer_restart ret
= HRTIMER_RESTART
;
3815 struct perf_sample_data data
;
3816 struct perf_counter
*counter
;
3819 counter
= container_of(hrtimer
, struct perf_counter
, hw
.hrtimer
);
3820 counter
->pmu
->read(counter
);
3823 data
.regs
= get_irq_regs();
3825 * In case we exclude kernel IPs or are somehow not in interrupt
3826 * context, provide the next best thing, the user IP.
3828 if ((counter
->attr
.exclude_kernel
|| !data
.regs
) &&
3829 !counter
->attr
.exclude_user
)
3830 data
.regs
= task_pt_regs(current
);
3833 if (perf_counter_overflow(counter
, 0, &data
))
3834 ret
= HRTIMER_NORESTART
;
3837 period
= max_t(u64
, 10000, counter
->hw
.sample_period
);
3838 hrtimer_forward_now(hrtimer
, ns_to_ktime(period
));
3844 * Software counter: cpu wall time clock
3847 static void cpu_clock_perf_counter_update(struct perf_counter
*counter
)
3849 int cpu
= raw_smp_processor_id();
3853 now
= cpu_clock(cpu
);
3854 prev
= atomic64_read(&counter
->hw
.prev_count
);
3855 atomic64_set(&counter
->hw
.prev_count
, now
);
3856 atomic64_add(now
- prev
, &counter
->count
);
3859 static int cpu_clock_perf_counter_enable(struct perf_counter
*counter
)
3861 struct hw_perf_counter
*hwc
= &counter
->hw
;
3862 int cpu
= raw_smp_processor_id();
3864 atomic64_set(&hwc
->prev_count
, cpu_clock(cpu
));
3865 hrtimer_init(&hwc
->hrtimer
, CLOCK_MONOTONIC
, HRTIMER_MODE_REL
);
3866 hwc
->hrtimer
.function
= perf_swcounter_hrtimer
;
3867 if (hwc
->sample_period
) {
3868 u64 period
= max_t(u64
, 10000, hwc
->sample_period
);
3869 __hrtimer_start_range_ns(&hwc
->hrtimer
,
3870 ns_to_ktime(period
), 0,
3871 HRTIMER_MODE_REL
, 0);
3877 static void cpu_clock_perf_counter_disable(struct perf_counter
*counter
)
3879 if (counter
->hw
.sample_period
)
3880 hrtimer_cancel(&counter
->hw
.hrtimer
);
3881 cpu_clock_perf_counter_update(counter
);
3884 static void cpu_clock_perf_counter_read(struct perf_counter
*counter
)
3886 cpu_clock_perf_counter_update(counter
);
3889 static const struct pmu perf_ops_cpu_clock
= {
3890 .enable
= cpu_clock_perf_counter_enable
,
3891 .disable
= cpu_clock_perf_counter_disable
,
3892 .read
= cpu_clock_perf_counter_read
,
3896 * Software counter: task time clock
3899 static void task_clock_perf_counter_update(struct perf_counter
*counter
, u64 now
)
3904 prev
= atomic64_xchg(&counter
->hw
.prev_count
, now
);
3906 atomic64_add(delta
, &counter
->count
);
3909 static int task_clock_perf_counter_enable(struct perf_counter
*counter
)
3911 struct hw_perf_counter
*hwc
= &counter
->hw
;
3914 now
= counter
->ctx
->time
;
3916 atomic64_set(&hwc
->prev_count
, now
);
3917 hrtimer_init(&hwc
->hrtimer
, CLOCK_MONOTONIC
, HRTIMER_MODE_REL
);
3918 hwc
->hrtimer
.function
= perf_swcounter_hrtimer
;
3919 if (hwc
->sample_period
) {
3920 u64 period
= max_t(u64
, 10000, hwc
->sample_period
);
3921 __hrtimer_start_range_ns(&hwc
->hrtimer
,
3922 ns_to_ktime(period
), 0,
3923 HRTIMER_MODE_REL
, 0);
3929 static void task_clock_perf_counter_disable(struct perf_counter
*counter
)
3931 if (counter
->hw
.sample_period
)
3932 hrtimer_cancel(&counter
->hw
.hrtimer
);
3933 task_clock_perf_counter_update(counter
, counter
->ctx
->time
);
3937 static void task_clock_perf_counter_read(struct perf_counter
*counter
)
3942 update_context_time(counter
->ctx
);
3943 time
= counter
->ctx
->time
;
3945 u64 now
= perf_clock();
3946 u64 delta
= now
- counter
->ctx
->timestamp
;
3947 time
= counter
->ctx
->time
+ delta
;
3950 task_clock_perf_counter_update(counter
, time
);
3953 static const struct pmu perf_ops_task_clock
= {
3954 .enable
= task_clock_perf_counter_enable
,
3955 .disable
= task_clock_perf_counter_disable
,
3956 .read
= task_clock_perf_counter_read
,
3959 #ifdef CONFIG_EVENT_PROFILE
3960 void perf_tpcounter_event(int event_id
, u64 addr
, u64 count
, void *record
,
3963 struct perf_raw_record raw
= {
3968 struct perf_sample_data data
= {
3969 .regs
= get_irq_regs(),
3975 data
.regs
= task_pt_regs(current
);
3977 do_perf_swcounter_event(PERF_TYPE_TRACEPOINT
, event_id
, count
, 1, &data
);
3979 EXPORT_SYMBOL_GPL(perf_tpcounter_event
);
3981 extern int ftrace_profile_enable(int);
3982 extern void ftrace_profile_disable(int);
3984 static void tp_perf_counter_destroy(struct perf_counter
*counter
)
3986 ftrace_profile_disable(counter
->attr
.config
);
3989 static const struct pmu
*tp_perf_counter_init(struct perf_counter
*counter
)
3992 * Raw tracepoint data is a severe data leak, only allow root to
3995 if ((counter
->attr
.sample_type
& PERF_SAMPLE_RAW
) &&
3996 perf_paranoid_tracepoint_raw() &&
3997 !capable(CAP_SYS_ADMIN
))
3998 return ERR_PTR(-EPERM
);
4000 if (ftrace_profile_enable(counter
->attr
.config
))
4003 counter
->destroy
= tp_perf_counter_destroy
;
4005 return &perf_ops_generic
;
4008 static const struct pmu
*tp_perf_counter_init(struct perf_counter
*counter
)
4014 atomic_t perf_swcounter_enabled
[PERF_COUNT_SW_MAX
];
4016 static void sw_perf_counter_destroy(struct perf_counter
*counter
)
4018 u64 event
= counter
->attr
.config
;
4020 WARN_ON(counter
->parent
);
4022 atomic_dec(&perf_swcounter_enabled
[event
]);
4025 static const struct pmu
*sw_perf_counter_init(struct perf_counter
*counter
)
4027 const struct pmu
*pmu
= NULL
;
4028 u64 event
= counter
->attr
.config
;
4031 * Software counters (currently) can't in general distinguish
4032 * between user, kernel and hypervisor events.
4033 * However, context switches and cpu migrations are considered
4034 * to be kernel events, and page faults are never hypervisor
4038 case PERF_COUNT_SW_CPU_CLOCK
:
4039 pmu
= &perf_ops_cpu_clock
;
4042 case PERF_COUNT_SW_TASK_CLOCK
:
4044 * If the user instantiates this as a per-cpu counter,
4045 * use the cpu_clock counter instead.
4047 if (counter
->ctx
->task
)
4048 pmu
= &perf_ops_task_clock
;
4050 pmu
= &perf_ops_cpu_clock
;
4053 case PERF_COUNT_SW_PAGE_FAULTS
:
4054 case PERF_COUNT_SW_PAGE_FAULTS_MIN
:
4055 case PERF_COUNT_SW_PAGE_FAULTS_MAJ
:
4056 case PERF_COUNT_SW_CONTEXT_SWITCHES
:
4057 case PERF_COUNT_SW_CPU_MIGRATIONS
:
4058 if (!counter
->parent
) {
4059 atomic_inc(&perf_swcounter_enabled
[event
]);
4060 counter
->destroy
= sw_perf_counter_destroy
;
4062 pmu
= &perf_ops_generic
;
4070 * Allocate and initialize a counter structure
4072 static struct perf_counter
*
4073 perf_counter_alloc(struct perf_counter_attr
*attr
,
4075 struct perf_counter_context
*ctx
,
4076 struct perf_counter
*group_leader
,
4077 struct perf_counter
*parent_counter
,
4080 const struct pmu
*pmu
;
4081 struct perf_counter
*counter
;
4082 struct hw_perf_counter
*hwc
;
4085 counter
= kzalloc(sizeof(*counter
), gfpflags
);
4087 return ERR_PTR(-ENOMEM
);
4090 * Single counters are their own group leaders, with an
4091 * empty sibling list:
4094 group_leader
= counter
;
4096 mutex_init(&counter
->child_mutex
);
4097 INIT_LIST_HEAD(&counter
->child_list
);
4099 INIT_LIST_HEAD(&counter
->list_entry
);
4100 INIT_LIST_HEAD(&counter
->event_entry
);
4101 INIT_LIST_HEAD(&counter
->sibling_list
);
4102 init_waitqueue_head(&counter
->waitq
);
4104 mutex_init(&counter
->mmap_mutex
);
4107 counter
->attr
= *attr
;
4108 counter
->group_leader
= group_leader
;
4109 counter
->pmu
= NULL
;
4111 counter
->oncpu
= -1;
4113 counter
->parent
= parent_counter
;
4115 counter
->ns
= get_pid_ns(current
->nsproxy
->pid_ns
);
4116 counter
->id
= atomic64_inc_return(&perf_counter_id
);
4118 counter
->state
= PERF_COUNTER_STATE_INACTIVE
;
4121 counter
->state
= PERF_COUNTER_STATE_OFF
;
4126 hwc
->sample_period
= attr
->sample_period
;
4127 if (attr
->freq
&& attr
->sample_freq
)
4128 hwc
->sample_period
= 1;
4129 hwc
->last_period
= hwc
->sample_period
;
4131 atomic64_set(&hwc
->period_left
, hwc
->sample_period
);
4134 * we currently do not support PERF_FORMAT_GROUP on inherited counters
4136 if (attr
->inherit
&& (attr
->read_format
& PERF_FORMAT_GROUP
))
4139 switch (attr
->type
) {
4141 case PERF_TYPE_HARDWARE
:
4142 case PERF_TYPE_HW_CACHE
:
4143 pmu
= hw_perf_counter_init(counter
);
4146 case PERF_TYPE_SOFTWARE
:
4147 pmu
= sw_perf_counter_init(counter
);
4150 case PERF_TYPE_TRACEPOINT
:
4151 pmu
= tp_perf_counter_init(counter
);
4161 else if (IS_ERR(pmu
))
4166 put_pid_ns(counter
->ns
);
4168 return ERR_PTR(err
);
4173 if (!counter
->parent
) {
4174 atomic_inc(&nr_counters
);
4175 if (counter
->attr
.mmap
)
4176 atomic_inc(&nr_mmap_counters
);
4177 if (counter
->attr
.comm
)
4178 atomic_inc(&nr_comm_counters
);
4179 if (counter
->attr
.task
)
4180 atomic_inc(&nr_task_counters
);
4186 static int perf_copy_attr(struct perf_counter_attr __user
*uattr
,
4187 struct perf_counter_attr
*attr
)
4192 if (!access_ok(VERIFY_WRITE
, uattr
, PERF_ATTR_SIZE_VER0
))
4196 * zero the full structure, so that a short copy will be nice.
4198 memset(attr
, 0, sizeof(*attr
));
4200 ret
= get_user(size
, &uattr
->size
);
4204 if (size
> PAGE_SIZE
) /* silly large */
4207 if (!size
) /* abi compat */
4208 size
= PERF_ATTR_SIZE_VER0
;
4210 if (size
< PERF_ATTR_SIZE_VER0
)
4214 * If we're handed a bigger struct than we know of,
4215 * ensure all the unknown bits are 0.
4217 if (size
> sizeof(*attr
)) {
4219 unsigned long __user
*addr
;
4220 unsigned long __user
*end
;
4222 addr
= PTR_ALIGN((void __user
*)uattr
+ sizeof(*attr
),
4223 sizeof(unsigned long));
4224 end
= PTR_ALIGN((void __user
*)uattr
+ size
,
4225 sizeof(unsigned long));
4227 for (; addr
< end
; addr
+= sizeof(unsigned long)) {
4228 ret
= get_user(val
, addr
);
4236 ret
= copy_from_user(attr
, uattr
, size
);
4241 * If the type exists, the corresponding creation will verify
4244 if (attr
->type
>= PERF_TYPE_MAX
)
4247 if (attr
->__reserved_1
|| attr
->__reserved_2
|| attr
->__reserved_3
)
4250 if (attr
->sample_type
& ~(PERF_SAMPLE_MAX
-1))
4253 if (attr
->read_format
& ~(PERF_FORMAT_MAX
-1))
4260 put_user(sizeof(*attr
), &uattr
->size
);
4265 int perf_counter_set_output(struct perf_counter
*counter
, int output_fd
)
4267 struct perf_counter
*output_counter
= NULL
;
4268 struct file
*output_file
= NULL
;
4269 struct perf_counter
*old_output
;
4270 int fput_needed
= 0;
4276 output_file
= fget_light(output_fd
, &fput_needed
);
4280 if (output_file
->f_op
!= &perf_fops
)
4283 output_counter
= output_file
->private_data
;
4285 /* Don't chain output fds */
4286 if (output_counter
->output
)
4289 /* Don't set an output fd when we already have an output channel */
4293 atomic_long_inc(&output_file
->f_count
);
4296 mutex_lock(&counter
->mmap_mutex
);
4297 old_output
= counter
->output
;
4298 rcu_assign_pointer(counter
->output
, output_counter
);
4299 mutex_unlock(&counter
->mmap_mutex
);
4303 * we need to make sure no existing perf_output_*()
4304 * is still referencing this counter.
4307 fput(old_output
->filp
);
4312 fput_light(output_file
, fput_needed
);
4317 * sys_perf_counter_open - open a performance counter, associate it to a task/cpu
4319 * @attr_uptr: event type attributes for monitoring/sampling
4322 * @group_fd: group leader counter fd
4324 SYSCALL_DEFINE5(perf_counter_open
,
4325 struct perf_counter_attr __user
*, attr_uptr
,
4326 pid_t
, pid
, int, cpu
, int, group_fd
, unsigned long, flags
)
4328 struct perf_counter
*counter
, *group_leader
;
4329 struct perf_counter_attr attr
;
4330 struct perf_counter_context
*ctx
;
4331 struct file
*counter_file
= NULL
;
4332 struct file
*group_file
= NULL
;
4333 int fput_needed
= 0;
4334 int fput_needed2
= 0;
4337 /* for future expandability... */
4338 if (flags
& ~(PERF_FLAG_FD_NO_GROUP
| PERF_FLAG_FD_OUTPUT
))
4341 err
= perf_copy_attr(attr_uptr
, &attr
);
4345 if (!attr
.exclude_kernel
) {
4346 if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN
))
4351 if (attr
.sample_freq
> sysctl_perf_counter_sample_rate
)
4356 * Get the target context (task or percpu):
4358 ctx
= find_get_context(pid
, cpu
);
4360 return PTR_ERR(ctx
);
4363 * Look up the group leader (we will attach this counter to it):
4365 group_leader
= NULL
;
4366 if (group_fd
!= -1 && !(flags
& PERF_FLAG_FD_NO_GROUP
)) {
4368 group_file
= fget_light(group_fd
, &fput_needed
);
4370 goto err_put_context
;
4371 if (group_file
->f_op
!= &perf_fops
)
4372 goto err_put_context
;
4374 group_leader
= group_file
->private_data
;
4376 * Do not allow a recursive hierarchy (this new sibling
4377 * becoming part of another group-sibling):
4379 if (group_leader
->group_leader
!= group_leader
)
4380 goto err_put_context
;
4382 * Do not allow to attach to a group in a different
4383 * task or CPU context:
4385 if (group_leader
->ctx
!= ctx
)
4386 goto err_put_context
;
4388 * Only a group leader can be exclusive or pinned
4390 if (attr
.exclusive
|| attr
.pinned
)
4391 goto err_put_context
;
4394 counter
= perf_counter_alloc(&attr
, cpu
, ctx
, group_leader
,
4396 err
= PTR_ERR(counter
);
4397 if (IS_ERR(counter
))
4398 goto err_put_context
;
4400 err
= anon_inode_getfd("[perf_counter]", &perf_fops
, counter
, 0);
4402 goto err_free_put_context
;
4404 counter_file
= fget_light(err
, &fput_needed2
);
4406 goto err_free_put_context
;
4408 if (flags
& PERF_FLAG_FD_OUTPUT
) {
4409 err
= perf_counter_set_output(counter
, group_fd
);
4411 goto err_fput_free_put_context
;
4414 counter
->filp
= counter_file
;
4415 WARN_ON_ONCE(ctx
->parent_ctx
);
4416 mutex_lock(&ctx
->mutex
);
4417 perf_install_in_context(ctx
, counter
, cpu
);
4419 mutex_unlock(&ctx
->mutex
);
4421 counter
->owner
= current
;
4422 get_task_struct(current
);
4423 mutex_lock(¤t
->perf_counter_mutex
);
4424 list_add_tail(&counter
->owner_entry
, ¤t
->perf_counter_list
);
4425 mutex_unlock(¤t
->perf_counter_mutex
);
4427 err_fput_free_put_context
:
4428 fput_light(counter_file
, fput_needed2
);
4430 err_free_put_context
:
4438 fput_light(group_file
, fput_needed
);
4444 * inherit a counter from parent task to child task:
4446 static struct perf_counter
*
4447 inherit_counter(struct perf_counter
*parent_counter
,
4448 struct task_struct
*parent
,
4449 struct perf_counter_context
*parent_ctx
,
4450 struct task_struct
*child
,
4451 struct perf_counter
*group_leader
,
4452 struct perf_counter_context
*child_ctx
)
4454 struct perf_counter
*child_counter
;
4457 * Instead of creating recursive hierarchies of counters,
4458 * we link inherited counters back to the original parent,
4459 * which has a filp for sure, which we use as the reference
4462 if (parent_counter
->parent
)
4463 parent_counter
= parent_counter
->parent
;
4465 child_counter
= perf_counter_alloc(&parent_counter
->attr
,
4466 parent_counter
->cpu
, child_ctx
,
4467 group_leader
, parent_counter
,
4469 if (IS_ERR(child_counter
))
4470 return child_counter
;
4474 * Make the child state follow the state of the parent counter,
4475 * not its attr.disabled bit. We hold the parent's mutex,
4476 * so we won't race with perf_counter_{en, dis}able_family.
4478 if (parent_counter
->state
>= PERF_COUNTER_STATE_INACTIVE
)
4479 child_counter
->state
= PERF_COUNTER_STATE_INACTIVE
;
4481 child_counter
->state
= PERF_COUNTER_STATE_OFF
;
4483 if (parent_counter
->attr
.freq
)
4484 child_counter
->hw
.sample_period
= parent_counter
->hw
.sample_period
;
4487 * Link it up in the child's context:
4489 add_counter_to_ctx(child_counter
, child_ctx
);
4492 * Get a reference to the parent filp - we will fput it
4493 * when the child counter exits. This is safe to do because
4494 * we are in the parent and we know that the filp still
4495 * exists and has a nonzero count:
4497 atomic_long_inc(&parent_counter
->filp
->f_count
);
4500 * Link this into the parent counter's child list
4502 WARN_ON_ONCE(parent_counter
->ctx
->parent_ctx
);
4503 mutex_lock(&parent_counter
->child_mutex
);
4504 list_add_tail(&child_counter
->child_list
, &parent_counter
->child_list
);
4505 mutex_unlock(&parent_counter
->child_mutex
);
4507 return child_counter
;
4510 static int inherit_group(struct perf_counter
*parent_counter
,
4511 struct task_struct
*parent
,
4512 struct perf_counter_context
*parent_ctx
,
4513 struct task_struct
*child
,
4514 struct perf_counter_context
*child_ctx
)
4516 struct perf_counter
*leader
;
4517 struct perf_counter
*sub
;
4518 struct perf_counter
*child_ctr
;
4520 leader
= inherit_counter(parent_counter
, parent
, parent_ctx
,
4521 child
, NULL
, child_ctx
);
4523 return PTR_ERR(leader
);
4524 list_for_each_entry(sub
, &parent_counter
->sibling_list
, list_entry
) {
4525 child_ctr
= inherit_counter(sub
, parent
, parent_ctx
,
4526 child
, leader
, child_ctx
);
4527 if (IS_ERR(child_ctr
))
4528 return PTR_ERR(child_ctr
);
4533 static void sync_child_counter(struct perf_counter
*child_counter
,
4534 struct task_struct
*child
)
4536 struct perf_counter
*parent_counter
= child_counter
->parent
;
4539 if (child_counter
->attr
.inherit_stat
)
4540 perf_counter_read_event(child_counter
, child
);
4542 child_val
= atomic64_read(&child_counter
->count
);
4545 * Add back the child's count to the parent's count:
4547 atomic64_add(child_val
, &parent_counter
->count
);
4548 atomic64_add(child_counter
->total_time_enabled
,
4549 &parent_counter
->child_total_time_enabled
);
4550 atomic64_add(child_counter
->total_time_running
,
4551 &parent_counter
->child_total_time_running
);
4554 * Remove this counter from the parent's list
4556 WARN_ON_ONCE(parent_counter
->ctx
->parent_ctx
);
4557 mutex_lock(&parent_counter
->child_mutex
);
4558 list_del_init(&child_counter
->child_list
);
4559 mutex_unlock(&parent_counter
->child_mutex
);
4562 * Release the parent counter, if this was the last
4565 fput(parent_counter
->filp
);
4569 __perf_counter_exit_task(struct perf_counter
*child_counter
,
4570 struct perf_counter_context
*child_ctx
,
4571 struct task_struct
*child
)
4573 struct perf_counter
*parent_counter
;
4575 update_counter_times(child_counter
);
4576 perf_counter_remove_from_context(child_counter
);
4578 parent_counter
= child_counter
->parent
;
4580 * It can happen that parent exits first, and has counters
4581 * that are still around due to the child reference. These
4582 * counters need to be zapped - but otherwise linger.
4584 if (parent_counter
) {
4585 sync_child_counter(child_counter
, child
);
4586 free_counter(child_counter
);
4591 * When a child task exits, feed back counter values to parent counters.
4593 void perf_counter_exit_task(struct task_struct
*child
)
4595 struct perf_counter
*child_counter
, *tmp
;
4596 struct perf_counter_context
*child_ctx
;
4597 unsigned long flags
;
4599 if (likely(!child
->perf_counter_ctxp
)) {
4600 perf_counter_task(child
, NULL
, 0);
4604 local_irq_save(flags
);
4606 * We can't reschedule here because interrupts are disabled,
4607 * and either child is current or it is a task that can't be
4608 * scheduled, so we are now safe from rescheduling changing
4611 child_ctx
= child
->perf_counter_ctxp
;
4612 __perf_counter_task_sched_out(child_ctx
);
4615 * Take the context lock here so that if find_get_context is
4616 * reading child->perf_counter_ctxp, we wait until it has
4617 * incremented the context's refcount before we do put_ctx below.
4619 spin_lock(&child_ctx
->lock
);
4620 child
->perf_counter_ctxp
= NULL
;
4622 * If this context is a clone; unclone it so it can't get
4623 * swapped to another process while we're removing all
4624 * the counters from it.
4626 unclone_ctx(child_ctx
);
4627 spin_unlock_irqrestore(&child_ctx
->lock
, flags
);
4630 * Report the task dead after unscheduling the counters so that we
4631 * won't get any samples after PERF_EVENT_EXIT. We can however still
4632 * get a few PERF_EVENT_READ events.
4634 perf_counter_task(child
, child_ctx
, 0);
4637 * We can recurse on the same lock type through:
4639 * __perf_counter_exit_task()
4640 * sync_child_counter()
4641 * fput(parent_counter->filp)
4643 * mutex_lock(&ctx->mutex)
4645 * But since its the parent context it won't be the same instance.
4647 mutex_lock_nested(&child_ctx
->mutex
, SINGLE_DEPTH_NESTING
);
4650 list_for_each_entry_safe(child_counter
, tmp
, &child_ctx
->counter_list
,
4652 __perf_counter_exit_task(child_counter
, child_ctx
, child
);
4655 * If the last counter was a group counter, it will have appended all
4656 * its siblings to the list, but we obtained 'tmp' before that which
4657 * will still point to the list head terminating the iteration.
4659 if (!list_empty(&child_ctx
->counter_list
))
4662 mutex_unlock(&child_ctx
->mutex
);
4668 * free an unexposed, unused context as created by inheritance by
4669 * init_task below, used by fork() in case of fail.
4671 void perf_counter_free_task(struct task_struct
*task
)
4673 struct perf_counter_context
*ctx
= task
->perf_counter_ctxp
;
4674 struct perf_counter
*counter
, *tmp
;
4679 mutex_lock(&ctx
->mutex
);
4681 list_for_each_entry_safe(counter
, tmp
, &ctx
->counter_list
, list_entry
) {
4682 struct perf_counter
*parent
= counter
->parent
;
4684 if (WARN_ON_ONCE(!parent
))
4687 mutex_lock(&parent
->child_mutex
);
4688 list_del_init(&counter
->child_list
);
4689 mutex_unlock(&parent
->child_mutex
);
4693 list_del_counter(counter
, ctx
);
4694 free_counter(counter
);
4697 if (!list_empty(&ctx
->counter_list
))
4700 mutex_unlock(&ctx
->mutex
);
4706 * Initialize the perf_counter context in task_struct
4708 int perf_counter_init_task(struct task_struct
*child
)
4710 struct perf_counter_context
*child_ctx
, *parent_ctx
;
4711 struct perf_counter_context
*cloned_ctx
;
4712 struct perf_counter
*counter
;
4713 struct task_struct
*parent
= current
;
4714 int inherited_all
= 1;
4717 child
->perf_counter_ctxp
= NULL
;
4719 mutex_init(&child
->perf_counter_mutex
);
4720 INIT_LIST_HEAD(&child
->perf_counter_list
);
4722 if (likely(!parent
->perf_counter_ctxp
))
4726 * This is executed from the parent task context, so inherit
4727 * counters that have been marked for cloning.
4728 * First allocate and initialize a context for the child.
4731 child_ctx
= kmalloc(sizeof(struct perf_counter_context
), GFP_KERNEL
);
4735 __perf_counter_init_context(child_ctx
, child
);
4736 child
->perf_counter_ctxp
= child_ctx
;
4737 get_task_struct(child
);
4740 * If the parent's context is a clone, pin it so it won't get
4743 parent_ctx
= perf_pin_task_context(parent
);
4746 * No need to check if parent_ctx != NULL here; since we saw
4747 * it non-NULL earlier, the only reason for it to become NULL
4748 * is if we exit, and since we're currently in the middle of
4749 * a fork we can't be exiting at the same time.
4753 * Lock the parent list. No need to lock the child - not PID
4754 * hashed yet and not running, so nobody can access it.
4756 mutex_lock(&parent_ctx
->mutex
);
4759 * We dont have to disable NMIs - we are only looking at
4760 * the list, not manipulating it:
4762 list_for_each_entry_rcu(counter
, &parent_ctx
->event_list
, event_entry
) {
4763 if (counter
!= counter
->group_leader
)
4766 if (!counter
->attr
.inherit
) {
4771 ret
= inherit_group(counter
, parent
, parent_ctx
,
4779 if (inherited_all
) {
4781 * Mark the child context as a clone of the parent
4782 * context, or of whatever the parent is a clone of.
4783 * Note that if the parent is a clone, it could get
4784 * uncloned at any point, but that doesn't matter
4785 * because the list of counters and the generation
4786 * count can't have changed since we took the mutex.
4788 cloned_ctx
= rcu_dereference(parent_ctx
->parent_ctx
);
4790 child_ctx
->parent_ctx
= cloned_ctx
;
4791 child_ctx
->parent_gen
= parent_ctx
->parent_gen
;
4793 child_ctx
->parent_ctx
= parent_ctx
;
4794 child_ctx
->parent_gen
= parent_ctx
->generation
;
4796 get_ctx(child_ctx
->parent_ctx
);
4799 mutex_unlock(&parent_ctx
->mutex
);
4801 perf_unpin_context(parent_ctx
);
4806 static void __cpuinit
perf_counter_init_cpu(int cpu
)
4808 struct perf_cpu_context
*cpuctx
;
4810 cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
4811 __perf_counter_init_context(&cpuctx
->ctx
, NULL
);
4813 spin_lock(&perf_resource_lock
);
4814 cpuctx
->max_pertask
= perf_max_counters
- perf_reserved_percpu
;
4815 spin_unlock(&perf_resource_lock
);
4817 hw_perf_counter_setup(cpu
);
4820 #ifdef CONFIG_HOTPLUG_CPU
4821 static void __perf_counter_exit_cpu(void *info
)
4823 struct perf_cpu_context
*cpuctx
= &__get_cpu_var(perf_cpu_context
);
4824 struct perf_counter_context
*ctx
= &cpuctx
->ctx
;
4825 struct perf_counter
*counter
, *tmp
;
4827 list_for_each_entry_safe(counter
, tmp
, &ctx
->counter_list
, list_entry
)
4828 __perf_counter_remove_from_context(counter
);
4830 static void perf_counter_exit_cpu(int cpu
)
4832 struct perf_cpu_context
*cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
4833 struct perf_counter_context
*ctx
= &cpuctx
->ctx
;
4835 mutex_lock(&ctx
->mutex
);
4836 smp_call_function_single(cpu
, __perf_counter_exit_cpu
, NULL
, 1);
4837 mutex_unlock(&ctx
->mutex
);
4840 static inline void perf_counter_exit_cpu(int cpu
) { }
4843 static int __cpuinit
4844 perf_cpu_notify(struct notifier_block
*self
, unsigned long action
, void *hcpu
)
4846 unsigned int cpu
= (long)hcpu
;
4850 case CPU_UP_PREPARE
:
4851 case CPU_UP_PREPARE_FROZEN
:
4852 perf_counter_init_cpu(cpu
);
4856 case CPU_ONLINE_FROZEN
:
4857 hw_perf_counter_setup_online(cpu
);
4860 case CPU_DOWN_PREPARE
:
4861 case CPU_DOWN_PREPARE_FROZEN
:
4862 perf_counter_exit_cpu(cpu
);
4873 * This has to have a higher priority than migration_notifier in sched.c.
4875 static struct notifier_block __cpuinitdata perf_cpu_nb
= {
4876 .notifier_call
= perf_cpu_notify
,
4880 void __init
perf_counter_init(void)
4882 perf_cpu_notify(&perf_cpu_nb
, (unsigned long)CPU_UP_PREPARE
,
4883 (void *)(long)smp_processor_id());
4884 perf_cpu_notify(&perf_cpu_nb
, (unsigned long)CPU_ONLINE
,
4885 (void *)(long)smp_processor_id());
4886 register_cpu_notifier(&perf_cpu_nb
);
4889 static ssize_t
perf_show_reserve_percpu(struct sysdev_class
*class, char *buf
)
4891 return sprintf(buf
, "%d\n", perf_reserved_percpu
);
4895 perf_set_reserve_percpu(struct sysdev_class
*class,
4899 struct perf_cpu_context
*cpuctx
;
4903 err
= strict_strtoul(buf
, 10, &val
);
4906 if (val
> perf_max_counters
)
4909 spin_lock(&perf_resource_lock
);
4910 perf_reserved_percpu
= val
;
4911 for_each_online_cpu(cpu
) {
4912 cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
4913 spin_lock_irq(&cpuctx
->ctx
.lock
);
4914 mpt
= min(perf_max_counters
- cpuctx
->ctx
.nr_counters
,
4915 perf_max_counters
- perf_reserved_percpu
);
4916 cpuctx
->max_pertask
= mpt
;
4917 spin_unlock_irq(&cpuctx
->ctx
.lock
);
4919 spin_unlock(&perf_resource_lock
);
4924 static ssize_t
perf_show_overcommit(struct sysdev_class
*class, char *buf
)
4926 return sprintf(buf
, "%d\n", perf_overcommit
);
4930 perf_set_overcommit(struct sysdev_class
*class, const char *buf
, size_t count
)
4935 err
= strict_strtoul(buf
, 10, &val
);
4941 spin_lock(&perf_resource_lock
);
4942 perf_overcommit
= val
;
4943 spin_unlock(&perf_resource_lock
);
4948 static SYSDEV_CLASS_ATTR(
4951 perf_show_reserve_percpu
,
4952 perf_set_reserve_percpu
4955 static SYSDEV_CLASS_ATTR(
4958 perf_show_overcommit
,
4962 static struct attribute
*perfclass_attrs
[] = {
4963 &attr_reserve_percpu
.attr
,
4964 &attr_overcommit
.attr
,
4968 static struct attribute_group perfclass_attr_group
= {
4969 .attrs
= perfclass_attrs
,
4970 .name
= "perf_counters",
4973 static int __init
perf_counter_sysfs_init(void)
4975 return sysfs_create_group(&cpu_sysdev_class
.kset
.kobj
,
4976 &perfclass_attr_group
);
4978 device_initcall(perf_counter_sysfs_init
);