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:
50 * 1 - disallow cpu counters to unpriv
51 * 2 - disallow kernel profiling to unpriv
53 int sysctl_perf_counter_paranoid __read_mostly
;
55 static inline bool perf_paranoid_cpu(void)
57 return sysctl_perf_counter_paranoid
> 0;
60 static inline bool perf_paranoid_kernel(void)
62 return sysctl_perf_counter_paranoid
> 1;
65 int sysctl_perf_counter_mlock __read_mostly
= 512; /* 'free' kb per user */
68 * max perf counter sample rate
70 int sysctl_perf_counter_sample_rate __read_mostly
= 100000;
72 static atomic64_t perf_counter_id
;
75 * Lock for (sysadmin-configurable) counter reservations:
77 static DEFINE_SPINLOCK(perf_resource_lock
);
80 * Architecture provided APIs - weak aliases:
82 extern __weak
const struct pmu
*hw_perf_counter_init(struct perf_counter
*counter
)
87 void __weak
hw_perf_disable(void) { barrier(); }
88 void __weak
hw_perf_enable(void) { barrier(); }
90 void __weak
hw_perf_counter_setup(int cpu
) { barrier(); }
91 void __weak
hw_perf_counter_setup_online(int cpu
) { barrier(); }
94 hw_perf_group_sched_in(struct perf_counter
*group_leader
,
95 struct perf_cpu_context
*cpuctx
,
96 struct perf_counter_context
*ctx
, int cpu
)
101 void __weak
perf_counter_print_debug(void) { }
103 static DEFINE_PER_CPU(int, disable_count
);
105 void __perf_disable(void)
107 __get_cpu_var(disable_count
)++;
110 bool __perf_enable(void)
112 return !--__get_cpu_var(disable_count
);
115 void perf_disable(void)
121 void perf_enable(void)
127 static void get_ctx(struct perf_counter_context
*ctx
)
129 WARN_ON(!atomic_inc_not_zero(&ctx
->refcount
));
132 static void free_ctx(struct rcu_head
*head
)
134 struct perf_counter_context
*ctx
;
136 ctx
= container_of(head
, struct perf_counter_context
, rcu_head
);
140 static void put_ctx(struct perf_counter_context
*ctx
)
142 if (atomic_dec_and_test(&ctx
->refcount
)) {
144 put_ctx(ctx
->parent_ctx
);
146 put_task_struct(ctx
->task
);
147 call_rcu(&ctx
->rcu_head
, free_ctx
);
151 static void unclone_ctx(struct perf_counter_context
*ctx
)
153 if (ctx
->parent_ctx
) {
154 put_ctx(ctx
->parent_ctx
);
155 ctx
->parent_ctx
= NULL
;
160 * If we inherit counters we want to return the parent counter id
163 static u64
primary_counter_id(struct perf_counter
*counter
)
165 u64 id
= counter
->id
;
168 id
= counter
->parent
->id
;
174 * Get the perf_counter_context for a task and lock it.
175 * This has to cope with with the fact that until it is locked,
176 * the context could get moved to another task.
178 static struct perf_counter_context
*
179 perf_lock_task_context(struct task_struct
*task
, unsigned long *flags
)
181 struct perf_counter_context
*ctx
;
185 ctx
= rcu_dereference(task
->perf_counter_ctxp
);
188 * If this context is a clone of another, it might
189 * get swapped for another underneath us by
190 * perf_counter_task_sched_out, though the
191 * rcu_read_lock() protects us from any context
192 * getting freed. Lock the context and check if it
193 * got swapped before we could get the lock, and retry
194 * if so. If we locked the right context, then it
195 * can't get swapped on us any more.
197 spin_lock_irqsave(&ctx
->lock
, *flags
);
198 if (ctx
!= rcu_dereference(task
->perf_counter_ctxp
)) {
199 spin_unlock_irqrestore(&ctx
->lock
, *flags
);
203 if (!atomic_inc_not_zero(&ctx
->refcount
)) {
204 spin_unlock_irqrestore(&ctx
->lock
, *flags
);
213 * Get the context for a task and increment its pin_count so it
214 * can't get swapped to another task. This also increments its
215 * reference count so that the context can't get freed.
217 static struct perf_counter_context
*perf_pin_task_context(struct task_struct
*task
)
219 struct perf_counter_context
*ctx
;
222 ctx
= perf_lock_task_context(task
, &flags
);
225 spin_unlock_irqrestore(&ctx
->lock
, flags
);
230 static void perf_unpin_context(struct perf_counter_context
*ctx
)
234 spin_lock_irqsave(&ctx
->lock
, flags
);
236 spin_unlock_irqrestore(&ctx
->lock
, flags
);
241 * Add a counter from the lists for its context.
242 * Must be called with ctx->mutex and ctx->lock held.
245 list_add_counter(struct perf_counter
*counter
, struct perf_counter_context
*ctx
)
247 struct perf_counter
*group_leader
= counter
->group_leader
;
250 * Depending on whether it is a standalone or sibling counter,
251 * add it straight to the context's counter list, or to the group
252 * leader's sibling list:
254 if (group_leader
== counter
)
255 list_add_tail(&counter
->list_entry
, &ctx
->counter_list
);
257 list_add_tail(&counter
->list_entry
, &group_leader
->sibling_list
);
258 group_leader
->nr_siblings
++;
261 list_add_rcu(&counter
->event_entry
, &ctx
->event_list
);
263 if (counter
->attr
.inherit_stat
)
268 * Remove a counter from the lists for its context.
269 * Must be called with ctx->mutex and ctx->lock held.
272 list_del_counter(struct perf_counter
*counter
, struct perf_counter_context
*ctx
)
274 struct perf_counter
*sibling
, *tmp
;
276 if (list_empty(&counter
->list_entry
))
279 if (counter
->attr
.inherit_stat
)
282 list_del_init(&counter
->list_entry
);
283 list_del_rcu(&counter
->event_entry
);
285 if (counter
->group_leader
!= counter
)
286 counter
->group_leader
->nr_siblings
--;
289 * If this was a group counter with sibling counters then
290 * upgrade the siblings to singleton counters by adding them
291 * to the context list directly:
293 list_for_each_entry_safe(sibling
, tmp
,
294 &counter
->sibling_list
, list_entry
) {
296 list_move_tail(&sibling
->list_entry
, &ctx
->counter_list
);
297 sibling
->group_leader
= sibling
;
302 counter_sched_out(struct perf_counter
*counter
,
303 struct perf_cpu_context
*cpuctx
,
304 struct perf_counter_context
*ctx
)
306 if (counter
->state
!= PERF_COUNTER_STATE_ACTIVE
)
309 counter
->state
= PERF_COUNTER_STATE_INACTIVE
;
310 if (counter
->pending_disable
) {
311 counter
->pending_disable
= 0;
312 counter
->state
= PERF_COUNTER_STATE_OFF
;
314 counter
->tstamp_stopped
= ctx
->time
;
315 counter
->pmu
->disable(counter
);
318 if (!is_software_counter(counter
))
319 cpuctx
->active_oncpu
--;
321 if (counter
->attr
.exclusive
|| !cpuctx
->active_oncpu
)
322 cpuctx
->exclusive
= 0;
326 group_sched_out(struct perf_counter
*group_counter
,
327 struct perf_cpu_context
*cpuctx
,
328 struct perf_counter_context
*ctx
)
330 struct perf_counter
*counter
;
332 if (group_counter
->state
!= PERF_COUNTER_STATE_ACTIVE
)
335 counter_sched_out(group_counter
, cpuctx
, ctx
);
338 * Schedule out siblings (if any):
340 list_for_each_entry(counter
, &group_counter
->sibling_list
, list_entry
)
341 counter_sched_out(counter
, cpuctx
, ctx
);
343 if (group_counter
->attr
.exclusive
)
344 cpuctx
->exclusive
= 0;
348 * Cross CPU call to remove a performance counter
350 * We disable the counter on the hardware level first. After that we
351 * remove it from the context list.
353 static void __perf_counter_remove_from_context(void *info
)
355 struct perf_cpu_context
*cpuctx
= &__get_cpu_var(perf_cpu_context
);
356 struct perf_counter
*counter
= info
;
357 struct perf_counter_context
*ctx
= counter
->ctx
;
360 * If this is a task context, we need to check whether it is
361 * the current task context of this cpu. If not it has been
362 * scheduled out before the smp call arrived.
364 if (ctx
->task
&& cpuctx
->task_ctx
!= ctx
)
367 spin_lock(&ctx
->lock
);
369 * Protect the list operation against NMI by disabling the
370 * counters on a global level.
374 counter_sched_out(counter
, cpuctx
, ctx
);
376 list_del_counter(counter
, ctx
);
380 * Allow more per task counters with respect to the
383 cpuctx
->max_pertask
=
384 min(perf_max_counters
- ctx
->nr_counters
,
385 perf_max_counters
- perf_reserved_percpu
);
389 spin_unlock(&ctx
->lock
);
394 * Remove the counter from a task's (or a CPU's) list of counters.
396 * Must be called with ctx->mutex held.
398 * CPU counters are removed with a smp call. For task counters we only
399 * call when the task is on a CPU.
401 * If counter->ctx is a cloned context, callers must make sure that
402 * every task struct that counter->ctx->task could possibly point to
403 * remains valid. This is OK when called from perf_release since
404 * that only calls us on the top-level context, which can't be a clone.
405 * When called from perf_counter_exit_task, it's OK because the
406 * context has been detached from its task.
408 static void perf_counter_remove_from_context(struct perf_counter
*counter
)
410 struct perf_counter_context
*ctx
= counter
->ctx
;
411 struct task_struct
*task
= ctx
->task
;
415 * Per cpu counters are removed via an smp call and
416 * the removal is always sucessful.
418 smp_call_function_single(counter
->cpu
,
419 __perf_counter_remove_from_context
,
425 task_oncpu_function_call(task
, __perf_counter_remove_from_context
,
428 spin_lock_irq(&ctx
->lock
);
430 * If the context is active we need to retry the smp call.
432 if (ctx
->nr_active
&& !list_empty(&counter
->list_entry
)) {
433 spin_unlock_irq(&ctx
->lock
);
438 * The lock prevents that this context is scheduled in so we
439 * can remove the counter safely, if the call above did not
442 if (!list_empty(&counter
->list_entry
)) {
443 list_del_counter(counter
, ctx
);
445 spin_unlock_irq(&ctx
->lock
);
448 static inline u64
perf_clock(void)
450 return cpu_clock(smp_processor_id());
454 * Update the record of the current time in a context.
456 static void update_context_time(struct perf_counter_context
*ctx
)
458 u64 now
= perf_clock();
460 ctx
->time
+= now
- ctx
->timestamp
;
461 ctx
->timestamp
= now
;
465 * Update the total_time_enabled and total_time_running fields for a counter.
467 static void update_counter_times(struct perf_counter
*counter
)
469 struct perf_counter_context
*ctx
= counter
->ctx
;
472 if (counter
->state
< PERF_COUNTER_STATE_INACTIVE
)
475 counter
->total_time_enabled
= ctx
->time
- counter
->tstamp_enabled
;
477 if (counter
->state
== PERF_COUNTER_STATE_INACTIVE
)
478 run_end
= counter
->tstamp_stopped
;
482 counter
->total_time_running
= run_end
- counter
->tstamp_running
;
486 * Update total_time_enabled and total_time_running for all counters in a group.
488 static void update_group_times(struct perf_counter
*leader
)
490 struct perf_counter
*counter
;
492 update_counter_times(leader
);
493 list_for_each_entry(counter
, &leader
->sibling_list
, list_entry
)
494 update_counter_times(counter
);
498 * Cross CPU call to disable a performance counter
500 static void __perf_counter_disable(void *info
)
502 struct perf_counter
*counter
= info
;
503 struct perf_cpu_context
*cpuctx
= &__get_cpu_var(perf_cpu_context
);
504 struct perf_counter_context
*ctx
= counter
->ctx
;
507 * If this is a per-task counter, need to check whether this
508 * counter's task is the current task on this cpu.
510 if (ctx
->task
&& cpuctx
->task_ctx
!= ctx
)
513 spin_lock(&ctx
->lock
);
516 * If the counter is on, turn it off.
517 * If it is in error state, leave it in error state.
519 if (counter
->state
>= PERF_COUNTER_STATE_INACTIVE
) {
520 update_context_time(ctx
);
521 update_counter_times(counter
);
522 if (counter
== counter
->group_leader
)
523 group_sched_out(counter
, cpuctx
, ctx
);
525 counter_sched_out(counter
, cpuctx
, ctx
);
526 counter
->state
= PERF_COUNTER_STATE_OFF
;
529 spin_unlock(&ctx
->lock
);
535 * If counter->ctx is a cloned context, callers must make sure that
536 * every task struct that counter->ctx->task could possibly point to
537 * remains valid. This condition is satisifed when called through
538 * perf_counter_for_each_child or perf_counter_for_each because they
539 * hold the top-level counter's child_mutex, so any descendant that
540 * goes to exit will block in sync_child_counter.
541 * When called from perf_pending_counter it's OK because counter->ctx
542 * is the current context on this CPU and preemption is disabled,
543 * hence we can't get into perf_counter_task_sched_out for this context.
545 static void perf_counter_disable(struct perf_counter
*counter
)
547 struct perf_counter_context
*ctx
= counter
->ctx
;
548 struct task_struct
*task
= ctx
->task
;
552 * Disable the counter on the cpu that it's on
554 smp_call_function_single(counter
->cpu
, __perf_counter_disable
,
560 task_oncpu_function_call(task
, __perf_counter_disable
, counter
);
562 spin_lock_irq(&ctx
->lock
);
564 * If the counter is still active, we need to retry the cross-call.
566 if (counter
->state
== PERF_COUNTER_STATE_ACTIVE
) {
567 spin_unlock_irq(&ctx
->lock
);
572 * Since we have the lock this context can't be scheduled
573 * in, so we can change the state safely.
575 if (counter
->state
== PERF_COUNTER_STATE_INACTIVE
) {
576 update_counter_times(counter
);
577 counter
->state
= PERF_COUNTER_STATE_OFF
;
580 spin_unlock_irq(&ctx
->lock
);
584 counter_sched_in(struct perf_counter
*counter
,
585 struct perf_cpu_context
*cpuctx
,
586 struct perf_counter_context
*ctx
,
589 if (counter
->state
<= PERF_COUNTER_STATE_OFF
)
592 counter
->state
= PERF_COUNTER_STATE_ACTIVE
;
593 counter
->oncpu
= cpu
; /* TODO: put 'cpu' into cpuctx->cpu */
595 * The new state must be visible before we turn it on in the hardware:
599 if (counter
->pmu
->enable(counter
)) {
600 counter
->state
= PERF_COUNTER_STATE_INACTIVE
;
605 counter
->tstamp_running
+= ctx
->time
- counter
->tstamp_stopped
;
607 if (!is_software_counter(counter
))
608 cpuctx
->active_oncpu
++;
611 if (counter
->attr
.exclusive
)
612 cpuctx
->exclusive
= 1;
618 group_sched_in(struct perf_counter
*group_counter
,
619 struct perf_cpu_context
*cpuctx
,
620 struct perf_counter_context
*ctx
,
623 struct perf_counter
*counter
, *partial_group
;
626 if (group_counter
->state
== PERF_COUNTER_STATE_OFF
)
629 ret
= hw_perf_group_sched_in(group_counter
, cpuctx
, ctx
, cpu
);
631 return ret
< 0 ? ret
: 0;
633 if (counter_sched_in(group_counter
, cpuctx
, ctx
, cpu
))
637 * Schedule in siblings as one group (if any):
639 list_for_each_entry(counter
, &group_counter
->sibling_list
, list_entry
) {
640 if (counter_sched_in(counter
, cpuctx
, ctx
, cpu
)) {
641 partial_group
= counter
;
650 * Groups can be scheduled in as one unit only, so undo any
651 * partial group before returning:
653 list_for_each_entry(counter
, &group_counter
->sibling_list
, list_entry
) {
654 if (counter
== partial_group
)
656 counter_sched_out(counter
, cpuctx
, ctx
);
658 counter_sched_out(group_counter
, cpuctx
, ctx
);
664 * Return 1 for a group consisting entirely of software counters,
665 * 0 if the group contains any hardware counters.
667 static int is_software_only_group(struct perf_counter
*leader
)
669 struct perf_counter
*counter
;
671 if (!is_software_counter(leader
))
674 list_for_each_entry(counter
, &leader
->sibling_list
, list_entry
)
675 if (!is_software_counter(counter
))
682 * Work out whether we can put this counter group on the CPU now.
684 static int group_can_go_on(struct perf_counter
*counter
,
685 struct perf_cpu_context
*cpuctx
,
689 * Groups consisting entirely of software counters can always go on.
691 if (is_software_only_group(counter
))
694 * If an exclusive group is already on, no other hardware
695 * counters can go on.
697 if (cpuctx
->exclusive
)
700 * If this group is exclusive and there are already
701 * counters on the CPU, it can't go on.
703 if (counter
->attr
.exclusive
&& cpuctx
->active_oncpu
)
706 * Otherwise, try to add it if all previous groups were able
712 static void add_counter_to_ctx(struct perf_counter
*counter
,
713 struct perf_counter_context
*ctx
)
715 list_add_counter(counter
, ctx
);
716 counter
->tstamp_enabled
= ctx
->time
;
717 counter
->tstamp_running
= ctx
->time
;
718 counter
->tstamp_stopped
= ctx
->time
;
722 * Cross CPU call to install and enable a performance counter
724 * Must be called with ctx->mutex held
726 static void __perf_install_in_context(void *info
)
728 struct perf_cpu_context
*cpuctx
= &__get_cpu_var(perf_cpu_context
);
729 struct perf_counter
*counter
= info
;
730 struct perf_counter_context
*ctx
= counter
->ctx
;
731 struct perf_counter
*leader
= counter
->group_leader
;
732 int cpu
= smp_processor_id();
736 * If this is a task context, we need to check whether it is
737 * the current task context of this cpu. If not it has been
738 * scheduled out before the smp call arrived.
739 * Or possibly this is the right context but it isn't
740 * on this cpu because it had no counters.
742 if (ctx
->task
&& cpuctx
->task_ctx
!= ctx
) {
743 if (cpuctx
->task_ctx
|| ctx
->task
!= current
)
745 cpuctx
->task_ctx
= ctx
;
748 spin_lock(&ctx
->lock
);
750 update_context_time(ctx
);
753 * Protect the list operation against NMI by disabling the
754 * counters on a global level. NOP for non NMI based counters.
758 add_counter_to_ctx(counter
, ctx
);
761 * Don't put the counter on if it is disabled or if
762 * it is in a group and the group isn't on.
764 if (counter
->state
!= PERF_COUNTER_STATE_INACTIVE
||
765 (leader
!= counter
&& leader
->state
!= PERF_COUNTER_STATE_ACTIVE
))
769 * An exclusive counter can't go on if there are already active
770 * hardware counters, and no hardware counter can go on if there
771 * is already an exclusive counter on.
773 if (!group_can_go_on(counter
, cpuctx
, 1))
776 err
= counter_sched_in(counter
, cpuctx
, ctx
, cpu
);
780 * This counter couldn't go on. If it is in a group
781 * then we have to pull the whole group off.
782 * If the counter group is pinned then put it in error state.
784 if (leader
!= counter
)
785 group_sched_out(leader
, cpuctx
, ctx
);
786 if (leader
->attr
.pinned
) {
787 update_group_times(leader
);
788 leader
->state
= PERF_COUNTER_STATE_ERROR
;
792 if (!err
&& !ctx
->task
&& cpuctx
->max_pertask
)
793 cpuctx
->max_pertask
--;
798 spin_unlock(&ctx
->lock
);
802 * Attach a performance counter to a context
804 * First we add the counter to the list with the hardware enable bit
805 * in counter->hw_config cleared.
807 * If the counter is attached to a task which is on a CPU we use a smp
808 * call to enable it in the task context. The task might have been
809 * scheduled away, but we check this in the smp call again.
811 * Must be called with ctx->mutex held.
814 perf_install_in_context(struct perf_counter_context
*ctx
,
815 struct perf_counter
*counter
,
818 struct task_struct
*task
= ctx
->task
;
822 * Per cpu counters are installed via an smp call and
823 * the install is always sucessful.
825 smp_call_function_single(cpu
, __perf_install_in_context
,
831 task_oncpu_function_call(task
, __perf_install_in_context
,
834 spin_lock_irq(&ctx
->lock
);
836 * we need to retry the smp call.
838 if (ctx
->is_active
&& list_empty(&counter
->list_entry
)) {
839 spin_unlock_irq(&ctx
->lock
);
844 * The lock prevents that this context is scheduled in so we
845 * can add the counter safely, if it the call above did not
848 if (list_empty(&counter
->list_entry
))
849 add_counter_to_ctx(counter
, ctx
);
850 spin_unlock_irq(&ctx
->lock
);
854 * Cross CPU call to enable a performance counter
856 static void __perf_counter_enable(void *info
)
858 struct perf_counter
*counter
= info
;
859 struct perf_cpu_context
*cpuctx
= &__get_cpu_var(perf_cpu_context
);
860 struct perf_counter_context
*ctx
= counter
->ctx
;
861 struct perf_counter
*leader
= counter
->group_leader
;
865 * If this is a per-task counter, need to check whether this
866 * counter's task is the current task on this cpu.
868 if (ctx
->task
&& cpuctx
->task_ctx
!= ctx
) {
869 if (cpuctx
->task_ctx
|| ctx
->task
!= current
)
871 cpuctx
->task_ctx
= ctx
;
874 spin_lock(&ctx
->lock
);
876 update_context_time(ctx
);
878 if (counter
->state
>= PERF_COUNTER_STATE_INACTIVE
)
880 counter
->state
= PERF_COUNTER_STATE_INACTIVE
;
881 counter
->tstamp_enabled
= ctx
->time
- counter
->total_time_enabled
;
884 * If the counter is in a group and isn't the group leader,
885 * then don't put it on unless the group is on.
887 if (leader
!= counter
&& leader
->state
!= PERF_COUNTER_STATE_ACTIVE
)
890 if (!group_can_go_on(counter
, cpuctx
, 1)) {
894 if (counter
== leader
)
895 err
= group_sched_in(counter
, cpuctx
, ctx
,
898 err
= counter_sched_in(counter
, cpuctx
, ctx
,
905 * If this counter can't go on and it's part of a
906 * group, then the whole group has to come off.
908 if (leader
!= counter
)
909 group_sched_out(leader
, cpuctx
, ctx
);
910 if (leader
->attr
.pinned
) {
911 update_group_times(leader
);
912 leader
->state
= PERF_COUNTER_STATE_ERROR
;
917 spin_unlock(&ctx
->lock
);
923 * If counter->ctx is a cloned context, callers must make sure that
924 * every task struct that counter->ctx->task could possibly point to
925 * remains valid. This condition is satisfied when called through
926 * perf_counter_for_each_child or perf_counter_for_each as described
927 * for perf_counter_disable.
929 static void perf_counter_enable(struct perf_counter
*counter
)
931 struct perf_counter_context
*ctx
= counter
->ctx
;
932 struct task_struct
*task
= ctx
->task
;
936 * Enable the counter on the cpu that it's on
938 smp_call_function_single(counter
->cpu
, __perf_counter_enable
,
943 spin_lock_irq(&ctx
->lock
);
944 if (counter
->state
>= PERF_COUNTER_STATE_INACTIVE
)
948 * If the counter is in error state, clear that first.
949 * That way, if we see the counter in error state below, we
950 * know that it has gone back into error state, as distinct
951 * from the task having been scheduled away before the
952 * cross-call arrived.
954 if (counter
->state
== PERF_COUNTER_STATE_ERROR
)
955 counter
->state
= PERF_COUNTER_STATE_OFF
;
958 spin_unlock_irq(&ctx
->lock
);
959 task_oncpu_function_call(task
, __perf_counter_enable
, counter
);
961 spin_lock_irq(&ctx
->lock
);
964 * If the context is active and the counter is still off,
965 * we need to retry the cross-call.
967 if (ctx
->is_active
&& counter
->state
== PERF_COUNTER_STATE_OFF
)
971 * Since we have the lock this context can't be scheduled
972 * in, so we can change the state safely.
974 if (counter
->state
== PERF_COUNTER_STATE_OFF
) {
975 counter
->state
= PERF_COUNTER_STATE_INACTIVE
;
976 counter
->tstamp_enabled
=
977 ctx
->time
- counter
->total_time_enabled
;
980 spin_unlock_irq(&ctx
->lock
);
983 static int perf_counter_refresh(struct perf_counter
*counter
, int refresh
)
986 * not supported on inherited counters
988 if (counter
->attr
.inherit
)
991 atomic_add(refresh
, &counter
->event_limit
);
992 perf_counter_enable(counter
);
997 void __perf_counter_sched_out(struct perf_counter_context
*ctx
,
998 struct perf_cpu_context
*cpuctx
)
1000 struct perf_counter
*counter
;
1002 spin_lock(&ctx
->lock
);
1004 if (likely(!ctx
->nr_counters
))
1006 update_context_time(ctx
);
1009 if (ctx
->nr_active
) {
1010 list_for_each_entry(counter
, &ctx
->counter_list
, list_entry
) {
1011 if (counter
!= counter
->group_leader
)
1012 counter_sched_out(counter
, cpuctx
, ctx
);
1014 group_sched_out(counter
, cpuctx
, ctx
);
1019 spin_unlock(&ctx
->lock
);
1023 * Test whether two contexts are equivalent, i.e. whether they
1024 * have both been cloned from the same version of the same context
1025 * and they both have the same number of enabled counters.
1026 * If the number of enabled counters is the same, then the set
1027 * of enabled counters should be the same, because these are both
1028 * inherited contexts, therefore we can't access individual counters
1029 * in them directly with an fd; we can only enable/disable all
1030 * counters via prctl, or enable/disable all counters in a family
1031 * via ioctl, which will have the same effect on both contexts.
1033 static int context_equiv(struct perf_counter_context
*ctx1
,
1034 struct perf_counter_context
*ctx2
)
1036 return ctx1
->parent_ctx
&& ctx1
->parent_ctx
== ctx2
->parent_ctx
1037 && ctx1
->parent_gen
== ctx2
->parent_gen
1038 && !ctx1
->pin_count
&& !ctx2
->pin_count
;
1041 static void __perf_counter_read(void *counter
);
1043 static void __perf_counter_sync_stat(struct perf_counter
*counter
,
1044 struct perf_counter
*next_counter
)
1048 if (!counter
->attr
.inherit_stat
)
1052 * Update the counter value, we cannot use perf_counter_read()
1053 * because we're in the middle of a context switch and have IRQs
1054 * disabled, which upsets smp_call_function_single(), however
1055 * we know the counter must be on the current CPU, therefore we
1056 * don't need to use it.
1058 switch (counter
->state
) {
1059 case PERF_COUNTER_STATE_ACTIVE
:
1060 __perf_counter_read(counter
);
1063 case PERF_COUNTER_STATE_INACTIVE
:
1064 update_counter_times(counter
);
1072 * In order to keep per-task stats reliable we need to flip the counter
1073 * values when we flip the contexts.
1075 value
= atomic64_read(&next_counter
->count
);
1076 value
= atomic64_xchg(&counter
->count
, value
);
1077 atomic64_set(&next_counter
->count
, value
);
1079 swap(counter
->total_time_enabled
, next_counter
->total_time_enabled
);
1080 swap(counter
->total_time_running
, next_counter
->total_time_running
);
1083 * Since we swizzled the values, update the user visible data too.
1085 perf_counter_update_userpage(counter
);
1086 perf_counter_update_userpage(next_counter
);
1089 #define list_next_entry(pos, member) \
1090 list_entry(pos->member.next, typeof(*pos), member)
1092 static void perf_counter_sync_stat(struct perf_counter_context
*ctx
,
1093 struct perf_counter_context
*next_ctx
)
1095 struct perf_counter
*counter
, *next_counter
;
1100 counter
= list_first_entry(&ctx
->event_list
,
1101 struct perf_counter
, event_entry
);
1103 next_counter
= list_first_entry(&next_ctx
->event_list
,
1104 struct perf_counter
, event_entry
);
1106 while (&counter
->event_entry
!= &ctx
->event_list
&&
1107 &next_counter
->event_entry
!= &next_ctx
->event_list
) {
1109 __perf_counter_sync_stat(counter
, next_counter
);
1111 counter
= list_next_entry(counter
, event_entry
);
1112 next_counter
= list_next_entry(next_counter
, event_entry
);
1117 * Called from scheduler to remove the counters of the current task,
1118 * with interrupts disabled.
1120 * We stop each counter and update the counter value in counter->count.
1122 * This does not protect us against NMI, but disable()
1123 * sets the disabled bit in the control field of counter _before_
1124 * accessing the counter control register. If a NMI hits, then it will
1125 * not restart the counter.
1127 void perf_counter_task_sched_out(struct task_struct
*task
,
1128 struct task_struct
*next
, int cpu
)
1130 struct perf_cpu_context
*cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
1131 struct perf_counter_context
*ctx
= task
->perf_counter_ctxp
;
1132 struct perf_counter_context
*next_ctx
;
1133 struct perf_counter_context
*parent
;
1134 struct pt_regs
*regs
;
1137 regs
= task_pt_regs(task
);
1138 perf_swcounter_event(PERF_COUNT_SW_CONTEXT_SWITCHES
, 1, 1, regs
, 0);
1140 if (likely(!ctx
|| !cpuctx
->task_ctx
))
1143 update_context_time(ctx
);
1146 parent
= rcu_dereference(ctx
->parent_ctx
);
1147 next_ctx
= next
->perf_counter_ctxp
;
1148 if (parent
&& next_ctx
&&
1149 rcu_dereference(next_ctx
->parent_ctx
) == parent
) {
1151 * Looks like the two contexts are clones, so we might be
1152 * able to optimize the context switch. We lock both
1153 * contexts and check that they are clones under the
1154 * lock (including re-checking that neither has been
1155 * uncloned in the meantime). It doesn't matter which
1156 * order we take the locks because no other cpu could
1157 * be trying to lock both of these tasks.
1159 spin_lock(&ctx
->lock
);
1160 spin_lock_nested(&next_ctx
->lock
, SINGLE_DEPTH_NESTING
);
1161 if (context_equiv(ctx
, next_ctx
)) {
1163 * XXX do we need a memory barrier of sorts
1164 * wrt to rcu_dereference() of perf_counter_ctxp
1166 task
->perf_counter_ctxp
= next_ctx
;
1167 next
->perf_counter_ctxp
= ctx
;
1169 next_ctx
->task
= task
;
1172 perf_counter_sync_stat(ctx
, next_ctx
);
1174 spin_unlock(&next_ctx
->lock
);
1175 spin_unlock(&ctx
->lock
);
1180 __perf_counter_sched_out(ctx
, cpuctx
);
1181 cpuctx
->task_ctx
= NULL
;
1186 * Called with IRQs disabled
1188 static void __perf_counter_task_sched_out(struct perf_counter_context
*ctx
)
1190 struct perf_cpu_context
*cpuctx
= &__get_cpu_var(perf_cpu_context
);
1192 if (!cpuctx
->task_ctx
)
1195 if (WARN_ON_ONCE(ctx
!= cpuctx
->task_ctx
))
1198 __perf_counter_sched_out(ctx
, cpuctx
);
1199 cpuctx
->task_ctx
= NULL
;
1203 * Called with IRQs disabled
1205 static void perf_counter_cpu_sched_out(struct perf_cpu_context
*cpuctx
)
1207 __perf_counter_sched_out(&cpuctx
->ctx
, cpuctx
);
1211 __perf_counter_sched_in(struct perf_counter_context
*ctx
,
1212 struct perf_cpu_context
*cpuctx
, int cpu
)
1214 struct perf_counter
*counter
;
1217 spin_lock(&ctx
->lock
);
1219 if (likely(!ctx
->nr_counters
))
1222 ctx
->timestamp
= perf_clock();
1227 * First go through the list and put on any pinned groups
1228 * in order to give them the best chance of going on.
1230 list_for_each_entry(counter
, &ctx
->counter_list
, list_entry
) {
1231 if (counter
->state
<= PERF_COUNTER_STATE_OFF
||
1232 !counter
->attr
.pinned
)
1234 if (counter
->cpu
!= -1 && counter
->cpu
!= cpu
)
1237 if (counter
!= counter
->group_leader
)
1238 counter_sched_in(counter
, cpuctx
, ctx
, cpu
);
1240 if (group_can_go_on(counter
, cpuctx
, 1))
1241 group_sched_in(counter
, cpuctx
, ctx
, cpu
);
1245 * If this pinned group hasn't been scheduled,
1246 * put it in error state.
1248 if (counter
->state
== PERF_COUNTER_STATE_INACTIVE
) {
1249 update_group_times(counter
);
1250 counter
->state
= PERF_COUNTER_STATE_ERROR
;
1254 list_for_each_entry(counter
, &ctx
->counter_list
, list_entry
) {
1256 * Ignore counters in OFF or ERROR state, and
1257 * ignore pinned counters since we did them already.
1259 if (counter
->state
<= PERF_COUNTER_STATE_OFF
||
1260 counter
->attr
.pinned
)
1264 * Listen to the 'cpu' scheduling filter constraint
1267 if (counter
->cpu
!= -1 && counter
->cpu
!= cpu
)
1270 if (counter
!= counter
->group_leader
) {
1271 if (counter_sched_in(counter
, cpuctx
, ctx
, cpu
))
1274 if (group_can_go_on(counter
, cpuctx
, can_add_hw
)) {
1275 if (group_sched_in(counter
, cpuctx
, ctx
, cpu
))
1282 spin_unlock(&ctx
->lock
);
1286 * Called from scheduler to add the counters of the current task
1287 * with interrupts disabled.
1289 * We restore the counter value and then enable it.
1291 * This does not protect us against NMI, but enable()
1292 * sets the enabled bit in the control field of counter _before_
1293 * accessing the counter control register. If a NMI hits, then it will
1294 * keep the counter running.
1296 void perf_counter_task_sched_in(struct task_struct
*task
, int cpu
)
1298 struct perf_cpu_context
*cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
1299 struct perf_counter_context
*ctx
= task
->perf_counter_ctxp
;
1303 if (cpuctx
->task_ctx
== ctx
)
1305 __perf_counter_sched_in(ctx
, cpuctx
, cpu
);
1306 cpuctx
->task_ctx
= ctx
;
1309 static void perf_counter_cpu_sched_in(struct perf_cpu_context
*cpuctx
, int cpu
)
1311 struct perf_counter_context
*ctx
= &cpuctx
->ctx
;
1313 __perf_counter_sched_in(ctx
, cpuctx
, cpu
);
1316 #define MAX_INTERRUPTS (~0ULL)
1318 static void perf_log_throttle(struct perf_counter
*counter
, int enable
);
1320 static void perf_adjust_period(struct perf_counter
*counter
, u64 events
)
1322 struct hw_perf_counter
*hwc
= &counter
->hw
;
1323 u64 period
, sample_period
;
1326 events
*= hwc
->sample_period
;
1327 period
= div64_u64(events
, counter
->attr
.sample_freq
);
1329 delta
= (s64
)(period
- hwc
->sample_period
);
1330 delta
= (delta
+ 7) / 8; /* low pass filter */
1332 sample_period
= hwc
->sample_period
+ delta
;
1337 hwc
->sample_period
= sample_period
;
1340 static void perf_ctx_adjust_freq(struct perf_counter_context
*ctx
)
1342 struct perf_counter
*counter
;
1343 struct hw_perf_counter
*hwc
;
1344 u64 interrupts
, freq
;
1346 spin_lock(&ctx
->lock
);
1347 list_for_each_entry(counter
, &ctx
->counter_list
, list_entry
) {
1348 if (counter
->state
!= PERF_COUNTER_STATE_ACTIVE
)
1353 interrupts
= hwc
->interrupts
;
1354 hwc
->interrupts
= 0;
1357 * unthrottle counters on the tick
1359 if (interrupts
== MAX_INTERRUPTS
) {
1360 perf_log_throttle(counter
, 1);
1361 counter
->pmu
->unthrottle(counter
);
1362 interrupts
= 2*sysctl_perf_counter_sample_rate
/HZ
;
1365 if (!counter
->attr
.freq
|| !counter
->attr
.sample_freq
)
1369 * if the specified freq < HZ then we need to skip ticks
1371 if (counter
->attr
.sample_freq
< HZ
) {
1372 freq
= counter
->attr
.sample_freq
;
1374 hwc
->freq_count
+= freq
;
1375 hwc
->freq_interrupts
+= interrupts
;
1377 if (hwc
->freq_count
< HZ
)
1380 interrupts
= hwc
->freq_interrupts
;
1381 hwc
->freq_interrupts
= 0;
1382 hwc
->freq_count
-= HZ
;
1386 perf_adjust_period(counter
, freq
* interrupts
);
1389 * In order to avoid being stalled by an (accidental) huge
1390 * sample period, force reset the sample period if we didn't
1391 * get any events in this freq period.
1395 counter
->pmu
->disable(counter
);
1396 atomic64_set(&hwc
->period_left
, 0);
1397 counter
->pmu
->enable(counter
);
1401 spin_unlock(&ctx
->lock
);
1405 * Round-robin a context's counters:
1407 static void rotate_ctx(struct perf_counter_context
*ctx
)
1409 struct perf_counter
*counter
;
1411 if (!ctx
->nr_counters
)
1414 spin_lock(&ctx
->lock
);
1416 * Rotate the first entry last (works just fine for group counters too):
1419 list_for_each_entry(counter
, &ctx
->counter_list
, list_entry
) {
1420 list_move_tail(&counter
->list_entry
, &ctx
->counter_list
);
1425 spin_unlock(&ctx
->lock
);
1428 void perf_counter_task_tick(struct task_struct
*curr
, int cpu
)
1430 struct perf_cpu_context
*cpuctx
;
1431 struct perf_counter_context
*ctx
;
1433 if (!atomic_read(&nr_counters
))
1436 cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
1437 ctx
= curr
->perf_counter_ctxp
;
1439 perf_ctx_adjust_freq(&cpuctx
->ctx
);
1441 perf_ctx_adjust_freq(ctx
);
1443 perf_counter_cpu_sched_out(cpuctx
);
1445 __perf_counter_task_sched_out(ctx
);
1447 rotate_ctx(&cpuctx
->ctx
);
1451 perf_counter_cpu_sched_in(cpuctx
, cpu
);
1453 perf_counter_task_sched_in(curr
, cpu
);
1457 * Enable all of a task's counters that have been marked enable-on-exec.
1458 * This expects task == current.
1460 static void perf_counter_enable_on_exec(struct task_struct
*task
)
1462 struct perf_counter_context
*ctx
;
1463 struct perf_counter
*counter
;
1464 unsigned long flags
;
1467 local_irq_save(flags
);
1468 ctx
= task
->perf_counter_ctxp
;
1469 if (!ctx
|| !ctx
->nr_counters
)
1472 __perf_counter_task_sched_out(ctx
);
1474 spin_lock(&ctx
->lock
);
1476 list_for_each_entry(counter
, &ctx
->counter_list
, list_entry
) {
1477 if (!counter
->attr
.enable_on_exec
)
1479 counter
->attr
.enable_on_exec
= 0;
1480 if (counter
->state
>= PERF_COUNTER_STATE_INACTIVE
)
1482 counter
->state
= PERF_COUNTER_STATE_INACTIVE
;
1483 counter
->tstamp_enabled
=
1484 ctx
->time
- counter
->total_time_enabled
;
1489 * Unclone this context if we enabled any counter.
1494 spin_unlock(&ctx
->lock
);
1496 perf_counter_task_sched_in(task
, smp_processor_id());
1498 local_irq_restore(flags
);
1502 * Cross CPU call to read the hardware counter
1504 static void __perf_counter_read(void *info
)
1506 struct perf_counter
*counter
= info
;
1507 struct perf_counter_context
*ctx
= counter
->ctx
;
1508 unsigned long flags
;
1510 local_irq_save(flags
);
1512 update_context_time(ctx
);
1513 counter
->pmu
->read(counter
);
1514 update_counter_times(counter
);
1515 local_irq_restore(flags
);
1518 static u64
perf_counter_read(struct perf_counter
*counter
)
1521 * If counter is enabled and currently active on a CPU, update the
1522 * value in the counter structure:
1524 if (counter
->state
== PERF_COUNTER_STATE_ACTIVE
) {
1525 smp_call_function_single(counter
->oncpu
,
1526 __perf_counter_read
, counter
, 1);
1527 } else if (counter
->state
== PERF_COUNTER_STATE_INACTIVE
) {
1528 update_counter_times(counter
);
1531 return atomic64_read(&counter
->count
);
1535 * Initialize the perf_counter context in a task_struct:
1538 __perf_counter_init_context(struct perf_counter_context
*ctx
,
1539 struct task_struct
*task
)
1541 memset(ctx
, 0, sizeof(*ctx
));
1542 spin_lock_init(&ctx
->lock
);
1543 mutex_init(&ctx
->mutex
);
1544 INIT_LIST_HEAD(&ctx
->counter_list
);
1545 INIT_LIST_HEAD(&ctx
->event_list
);
1546 atomic_set(&ctx
->refcount
, 1);
1550 static struct perf_counter_context
*find_get_context(pid_t pid
, int cpu
)
1552 struct perf_counter_context
*ctx
;
1553 struct perf_cpu_context
*cpuctx
;
1554 struct task_struct
*task
;
1555 unsigned long flags
;
1559 * If cpu is not a wildcard then this is a percpu counter:
1562 /* Must be root to operate on a CPU counter: */
1563 if (perf_paranoid_cpu() && !capable(CAP_SYS_ADMIN
))
1564 return ERR_PTR(-EACCES
);
1566 if (cpu
< 0 || cpu
> num_possible_cpus())
1567 return ERR_PTR(-EINVAL
);
1570 * We could be clever and allow to attach a counter to an
1571 * offline CPU and activate it when the CPU comes up, but
1574 if (!cpu_isset(cpu
, cpu_online_map
))
1575 return ERR_PTR(-ENODEV
);
1577 cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
1588 task
= find_task_by_vpid(pid
);
1590 get_task_struct(task
);
1594 return ERR_PTR(-ESRCH
);
1597 * Can't attach counters to a dying task.
1600 if (task
->flags
& PF_EXITING
)
1603 /* Reuse ptrace permission checks for now. */
1605 if (!ptrace_may_access(task
, PTRACE_MODE_READ
))
1609 ctx
= perf_lock_task_context(task
, &flags
);
1612 spin_unlock_irqrestore(&ctx
->lock
, flags
);
1616 ctx
= kmalloc(sizeof(struct perf_counter_context
), GFP_KERNEL
);
1620 __perf_counter_init_context(ctx
, task
);
1622 if (cmpxchg(&task
->perf_counter_ctxp
, NULL
, ctx
)) {
1624 * We raced with some other task; use
1625 * the context they set.
1630 get_task_struct(task
);
1633 put_task_struct(task
);
1637 put_task_struct(task
);
1638 return ERR_PTR(err
);
1641 static void free_counter_rcu(struct rcu_head
*head
)
1643 struct perf_counter
*counter
;
1645 counter
= container_of(head
, struct perf_counter
, rcu_head
);
1647 put_pid_ns(counter
->ns
);
1651 static void perf_pending_sync(struct perf_counter
*counter
);
1653 static void free_counter(struct perf_counter
*counter
)
1655 perf_pending_sync(counter
);
1657 if (!counter
->parent
) {
1658 atomic_dec(&nr_counters
);
1659 if (counter
->attr
.mmap
)
1660 atomic_dec(&nr_mmap_counters
);
1661 if (counter
->attr
.comm
)
1662 atomic_dec(&nr_comm_counters
);
1663 if (counter
->attr
.task
)
1664 atomic_dec(&nr_task_counters
);
1667 if (counter
->destroy
)
1668 counter
->destroy(counter
);
1670 put_ctx(counter
->ctx
);
1671 call_rcu(&counter
->rcu_head
, free_counter_rcu
);
1675 * Called when the last reference to the file is gone.
1677 static int perf_release(struct inode
*inode
, struct file
*file
)
1679 struct perf_counter
*counter
= file
->private_data
;
1680 struct perf_counter_context
*ctx
= counter
->ctx
;
1682 file
->private_data
= NULL
;
1684 WARN_ON_ONCE(ctx
->parent_ctx
);
1685 mutex_lock(&ctx
->mutex
);
1686 perf_counter_remove_from_context(counter
);
1687 mutex_unlock(&ctx
->mutex
);
1689 mutex_lock(&counter
->owner
->perf_counter_mutex
);
1690 list_del_init(&counter
->owner_entry
);
1691 mutex_unlock(&counter
->owner
->perf_counter_mutex
);
1692 put_task_struct(counter
->owner
);
1694 free_counter(counter
);
1699 static int perf_counter_read_size(struct perf_counter
*counter
)
1701 int entry
= sizeof(u64
); /* value */
1705 if (counter
->attr
.read_format
& PERF_FORMAT_TOTAL_TIME_ENABLED
)
1706 size
+= sizeof(u64
);
1708 if (counter
->attr
.read_format
& PERF_FORMAT_TOTAL_TIME_RUNNING
)
1709 size
+= sizeof(u64
);
1711 if (counter
->attr
.read_format
& PERF_FORMAT_ID
)
1712 entry
+= sizeof(u64
);
1714 if (counter
->attr
.read_format
& PERF_FORMAT_GROUP
) {
1715 nr
+= counter
->group_leader
->nr_siblings
;
1716 size
+= sizeof(u64
);
1724 static u64
perf_counter_read_value(struct perf_counter
*counter
)
1726 struct perf_counter
*child
;
1729 total
+= perf_counter_read(counter
);
1730 list_for_each_entry(child
, &counter
->child_list
, child_list
)
1731 total
+= perf_counter_read(child
);
1736 static int perf_counter_read_entry(struct perf_counter
*counter
,
1737 u64 read_format
, char __user
*buf
)
1739 int n
= 0, count
= 0;
1742 values
[n
++] = perf_counter_read_value(counter
);
1743 if (read_format
& PERF_FORMAT_ID
)
1744 values
[n
++] = primary_counter_id(counter
);
1746 count
= n
* sizeof(u64
);
1748 if (copy_to_user(buf
, values
, count
))
1754 static int perf_counter_read_group(struct perf_counter
*counter
,
1755 u64 read_format
, char __user
*buf
)
1757 struct perf_counter
*leader
= counter
->group_leader
, *sub
;
1758 int n
= 0, size
= 0, err
= -EFAULT
;
1761 values
[n
++] = 1 + leader
->nr_siblings
;
1762 if (read_format
& PERF_FORMAT_TOTAL_TIME_ENABLED
) {
1763 values
[n
++] = leader
->total_time_enabled
+
1764 atomic64_read(&leader
->child_total_time_enabled
);
1766 if (read_format
& PERF_FORMAT_TOTAL_TIME_RUNNING
) {
1767 values
[n
++] = leader
->total_time_running
+
1768 atomic64_read(&leader
->child_total_time_running
);
1771 size
= n
* sizeof(u64
);
1773 if (copy_to_user(buf
, values
, size
))
1776 err
= perf_counter_read_entry(leader
, read_format
, buf
+ size
);
1782 list_for_each_entry(sub
, &leader
->sibling_list
, list_entry
) {
1783 err
= perf_counter_read_entry(counter
, read_format
,
1794 static int perf_counter_read_one(struct perf_counter
*counter
,
1795 u64 read_format
, char __user
*buf
)
1800 values
[n
++] = perf_counter_read_value(counter
);
1801 if (read_format
& PERF_FORMAT_TOTAL_TIME_ENABLED
) {
1802 values
[n
++] = counter
->total_time_enabled
+
1803 atomic64_read(&counter
->child_total_time_enabled
);
1805 if (read_format
& PERF_FORMAT_TOTAL_TIME_RUNNING
) {
1806 values
[n
++] = counter
->total_time_running
+
1807 atomic64_read(&counter
->child_total_time_running
);
1809 if (read_format
& PERF_FORMAT_ID
)
1810 values
[n
++] = primary_counter_id(counter
);
1812 if (copy_to_user(buf
, values
, n
* sizeof(u64
)))
1815 return n
* sizeof(u64
);
1819 * Read the performance counter - simple non blocking version for now
1822 perf_read_hw(struct perf_counter
*counter
, char __user
*buf
, size_t count
)
1824 u64 read_format
= counter
->attr
.read_format
;
1828 * Return end-of-file for a read on a counter that is in
1829 * error state (i.e. because it was pinned but it couldn't be
1830 * scheduled on to the CPU at some point).
1832 if (counter
->state
== PERF_COUNTER_STATE_ERROR
)
1835 if (count
< perf_counter_read_size(counter
))
1838 WARN_ON_ONCE(counter
->ctx
->parent_ctx
);
1839 mutex_lock(&counter
->child_mutex
);
1840 if (read_format
& PERF_FORMAT_GROUP
)
1841 ret
= perf_counter_read_group(counter
, read_format
, buf
);
1843 ret
= perf_counter_read_one(counter
, read_format
, buf
);
1844 mutex_unlock(&counter
->child_mutex
);
1850 perf_read(struct file
*file
, char __user
*buf
, size_t count
, loff_t
*ppos
)
1852 struct perf_counter
*counter
= file
->private_data
;
1854 return perf_read_hw(counter
, buf
, count
);
1857 static unsigned int perf_poll(struct file
*file
, poll_table
*wait
)
1859 struct perf_counter
*counter
= file
->private_data
;
1860 struct perf_mmap_data
*data
;
1861 unsigned int events
= POLL_HUP
;
1864 data
= rcu_dereference(counter
->data
);
1866 events
= atomic_xchg(&data
->poll
, 0);
1869 poll_wait(file
, &counter
->waitq
, wait
);
1874 static void perf_counter_reset(struct perf_counter
*counter
)
1876 (void)perf_counter_read(counter
);
1877 atomic64_set(&counter
->count
, 0);
1878 perf_counter_update_userpage(counter
);
1882 * Holding the top-level counter's child_mutex means that any
1883 * descendant process that has inherited this counter will block
1884 * in sync_child_counter if it goes to exit, thus satisfying the
1885 * task existence requirements of perf_counter_enable/disable.
1887 static void perf_counter_for_each_child(struct perf_counter
*counter
,
1888 void (*func
)(struct perf_counter
*))
1890 struct perf_counter
*child
;
1892 WARN_ON_ONCE(counter
->ctx
->parent_ctx
);
1893 mutex_lock(&counter
->child_mutex
);
1895 list_for_each_entry(child
, &counter
->child_list
, child_list
)
1897 mutex_unlock(&counter
->child_mutex
);
1900 static void perf_counter_for_each(struct perf_counter
*counter
,
1901 void (*func
)(struct perf_counter
*))
1903 struct perf_counter_context
*ctx
= counter
->ctx
;
1904 struct perf_counter
*sibling
;
1906 WARN_ON_ONCE(ctx
->parent_ctx
);
1907 mutex_lock(&ctx
->mutex
);
1908 counter
= counter
->group_leader
;
1910 perf_counter_for_each_child(counter
, func
);
1912 list_for_each_entry(sibling
, &counter
->sibling_list
, list_entry
)
1913 perf_counter_for_each_child(counter
, func
);
1914 mutex_unlock(&ctx
->mutex
);
1917 static int perf_counter_period(struct perf_counter
*counter
, u64 __user
*arg
)
1919 struct perf_counter_context
*ctx
= counter
->ctx
;
1924 if (!counter
->attr
.sample_period
)
1927 size
= copy_from_user(&value
, arg
, sizeof(value
));
1928 if (size
!= sizeof(value
))
1934 spin_lock_irq(&ctx
->lock
);
1935 if (counter
->attr
.freq
) {
1936 if (value
> sysctl_perf_counter_sample_rate
) {
1941 counter
->attr
.sample_freq
= value
;
1943 counter
->attr
.sample_period
= value
;
1944 counter
->hw
.sample_period
= value
;
1947 spin_unlock_irq(&ctx
->lock
);
1952 static long perf_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
1954 struct perf_counter
*counter
= file
->private_data
;
1955 void (*func
)(struct perf_counter
*);
1959 case PERF_COUNTER_IOC_ENABLE
:
1960 func
= perf_counter_enable
;
1962 case PERF_COUNTER_IOC_DISABLE
:
1963 func
= perf_counter_disable
;
1965 case PERF_COUNTER_IOC_RESET
:
1966 func
= perf_counter_reset
;
1969 case PERF_COUNTER_IOC_REFRESH
:
1970 return perf_counter_refresh(counter
, arg
);
1972 case PERF_COUNTER_IOC_PERIOD
:
1973 return perf_counter_period(counter
, (u64 __user
*)arg
);
1979 if (flags
& PERF_IOC_FLAG_GROUP
)
1980 perf_counter_for_each(counter
, func
);
1982 perf_counter_for_each_child(counter
, func
);
1987 int perf_counter_task_enable(void)
1989 struct perf_counter
*counter
;
1991 mutex_lock(¤t
->perf_counter_mutex
);
1992 list_for_each_entry(counter
, ¤t
->perf_counter_list
, owner_entry
)
1993 perf_counter_for_each_child(counter
, perf_counter_enable
);
1994 mutex_unlock(¤t
->perf_counter_mutex
);
1999 int perf_counter_task_disable(void)
2001 struct perf_counter
*counter
;
2003 mutex_lock(¤t
->perf_counter_mutex
);
2004 list_for_each_entry(counter
, ¤t
->perf_counter_list
, owner_entry
)
2005 perf_counter_for_each_child(counter
, perf_counter_disable
);
2006 mutex_unlock(¤t
->perf_counter_mutex
);
2011 static int perf_counter_index(struct perf_counter
*counter
)
2013 if (counter
->state
!= PERF_COUNTER_STATE_ACTIVE
)
2016 return counter
->hw
.idx
+ 1 - PERF_COUNTER_INDEX_OFFSET
;
2020 * Callers need to ensure there can be no nesting of this function, otherwise
2021 * the seqlock logic goes bad. We can not serialize this because the arch
2022 * code calls this from NMI context.
2024 void perf_counter_update_userpage(struct perf_counter
*counter
)
2026 struct perf_counter_mmap_page
*userpg
;
2027 struct perf_mmap_data
*data
;
2030 data
= rcu_dereference(counter
->data
);
2034 userpg
= data
->user_page
;
2037 * Disable preemption so as to not let the corresponding user-space
2038 * spin too long if we get preempted.
2043 userpg
->index
= perf_counter_index(counter
);
2044 userpg
->offset
= atomic64_read(&counter
->count
);
2045 if (counter
->state
== PERF_COUNTER_STATE_ACTIVE
)
2046 userpg
->offset
-= atomic64_read(&counter
->hw
.prev_count
);
2048 userpg
->time_enabled
= counter
->total_time_enabled
+
2049 atomic64_read(&counter
->child_total_time_enabled
);
2051 userpg
->time_running
= counter
->total_time_running
+
2052 atomic64_read(&counter
->child_total_time_running
);
2061 static int perf_mmap_fault(struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
2063 struct perf_counter
*counter
= vma
->vm_file
->private_data
;
2064 struct perf_mmap_data
*data
;
2065 int ret
= VM_FAULT_SIGBUS
;
2067 if (vmf
->flags
& FAULT_FLAG_MKWRITE
) {
2068 if (vmf
->pgoff
== 0)
2074 data
= rcu_dereference(counter
->data
);
2078 if (vmf
->pgoff
== 0) {
2079 vmf
->page
= virt_to_page(data
->user_page
);
2081 int nr
= vmf
->pgoff
- 1;
2083 if ((unsigned)nr
> data
->nr_pages
)
2086 if (vmf
->flags
& FAULT_FLAG_WRITE
)
2089 vmf
->page
= virt_to_page(data
->data_pages
[nr
]);
2092 get_page(vmf
->page
);
2093 vmf
->page
->mapping
= vma
->vm_file
->f_mapping
;
2094 vmf
->page
->index
= vmf
->pgoff
;
2103 static int perf_mmap_data_alloc(struct perf_counter
*counter
, int nr_pages
)
2105 struct perf_mmap_data
*data
;
2109 WARN_ON(atomic_read(&counter
->mmap_count
));
2111 size
= sizeof(struct perf_mmap_data
);
2112 size
+= nr_pages
* sizeof(void *);
2114 data
= kzalloc(size
, GFP_KERNEL
);
2118 data
->user_page
= (void *)get_zeroed_page(GFP_KERNEL
);
2119 if (!data
->user_page
)
2120 goto fail_user_page
;
2122 for (i
= 0; i
< nr_pages
; i
++) {
2123 data
->data_pages
[i
] = (void *)get_zeroed_page(GFP_KERNEL
);
2124 if (!data
->data_pages
[i
])
2125 goto fail_data_pages
;
2128 data
->nr_pages
= nr_pages
;
2129 atomic_set(&data
->lock
, -1);
2131 rcu_assign_pointer(counter
->data
, data
);
2136 for (i
--; i
>= 0; i
--)
2137 free_page((unsigned long)data
->data_pages
[i
]);
2139 free_page((unsigned long)data
->user_page
);
2148 static void perf_mmap_free_page(unsigned long addr
)
2150 struct page
*page
= virt_to_page((void *)addr
);
2152 page
->mapping
= NULL
;
2156 static void __perf_mmap_data_free(struct rcu_head
*rcu_head
)
2158 struct perf_mmap_data
*data
;
2161 data
= container_of(rcu_head
, struct perf_mmap_data
, rcu_head
);
2163 perf_mmap_free_page((unsigned long)data
->user_page
);
2164 for (i
= 0; i
< data
->nr_pages
; i
++)
2165 perf_mmap_free_page((unsigned long)data
->data_pages
[i
]);
2170 static void perf_mmap_data_free(struct perf_counter
*counter
)
2172 struct perf_mmap_data
*data
= counter
->data
;
2174 WARN_ON(atomic_read(&counter
->mmap_count
));
2176 rcu_assign_pointer(counter
->data
, NULL
);
2177 call_rcu(&data
->rcu_head
, __perf_mmap_data_free
);
2180 static void perf_mmap_open(struct vm_area_struct
*vma
)
2182 struct perf_counter
*counter
= vma
->vm_file
->private_data
;
2184 atomic_inc(&counter
->mmap_count
);
2187 static void perf_mmap_close(struct vm_area_struct
*vma
)
2189 struct perf_counter
*counter
= vma
->vm_file
->private_data
;
2191 WARN_ON_ONCE(counter
->ctx
->parent_ctx
);
2192 if (atomic_dec_and_mutex_lock(&counter
->mmap_count
, &counter
->mmap_mutex
)) {
2193 struct user_struct
*user
= current_user();
2195 atomic_long_sub(counter
->data
->nr_pages
+ 1, &user
->locked_vm
);
2196 vma
->vm_mm
->locked_vm
-= counter
->data
->nr_locked
;
2197 perf_mmap_data_free(counter
);
2198 mutex_unlock(&counter
->mmap_mutex
);
2202 static struct vm_operations_struct perf_mmap_vmops
= {
2203 .open
= perf_mmap_open
,
2204 .close
= perf_mmap_close
,
2205 .fault
= perf_mmap_fault
,
2206 .page_mkwrite
= perf_mmap_fault
,
2209 static int perf_mmap(struct file
*file
, struct vm_area_struct
*vma
)
2211 struct perf_counter
*counter
= file
->private_data
;
2212 unsigned long user_locked
, user_lock_limit
;
2213 struct user_struct
*user
= current_user();
2214 unsigned long locked
, lock_limit
;
2215 unsigned long vma_size
;
2216 unsigned long nr_pages
;
2217 long user_extra
, extra
;
2220 if (!(vma
->vm_flags
& VM_SHARED
))
2223 vma_size
= vma
->vm_end
- vma
->vm_start
;
2224 nr_pages
= (vma_size
/ PAGE_SIZE
) - 1;
2227 * If we have data pages ensure they're a power-of-two number, so we
2228 * can do bitmasks instead of modulo.
2230 if (nr_pages
!= 0 && !is_power_of_2(nr_pages
))
2233 if (vma_size
!= PAGE_SIZE
* (1 + nr_pages
))
2236 if (vma
->vm_pgoff
!= 0)
2239 WARN_ON_ONCE(counter
->ctx
->parent_ctx
);
2240 mutex_lock(&counter
->mmap_mutex
);
2241 if (atomic_inc_not_zero(&counter
->mmap_count
)) {
2242 if (nr_pages
!= counter
->data
->nr_pages
)
2247 user_extra
= nr_pages
+ 1;
2248 user_lock_limit
= sysctl_perf_counter_mlock
>> (PAGE_SHIFT
- 10);
2251 * Increase the limit linearly with more CPUs:
2253 user_lock_limit
*= num_online_cpus();
2255 user_locked
= atomic_long_read(&user
->locked_vm
) + user_extra
;
2258 if (user_locked
> user_lock_limit
)
2259 extra
= user_locked
- user_lock_limit
;
2261 lock_limit
= current
->signal
->rlim
[RLIMIT_MEMLOCK
].rlim_cur
;
2262 lock_limit
>>= PAGE_SHIFT
;
2263 locked
= vma
->vm_mm
->locked_vm
+ extra
;
2265 if ((locked
> lock_limit
) && !capable(CAP_IPC_LOCK
)) {
2270 WARN_ON(counter
->data
);
2271 ret
= perf_mmap_data_alloc(counter
, nr_pages
);
2275 atomic_set(&counter
->mmap_count
, 1);
2276 atomic_long_add(user_extra
, &user
->locked_vm
);
2277 vma
->vm_mm
->locked_vm
+= extra
;
2278 counter
->data
->nr_locked
= extra
;
2279 if (vma
->vm_flags
& VM_WRITE
)
2280 counter
->data
->writable
= 1;
2283 mutex_unlock(&counter
->mmap_mutex
);
2285 vma
->vm_flags
|= VM_RESERVED
;
2286 vma
->vm_ops
= &perf_mmap_vmops
;
2291 static int perf_fasync(int fd
, struct file
*filp
, int on
)
2293 struct inode
*inode
= filp
->f_path
.dentry
->d_inode
;
2294 struct perf_counter
*counter
= filp
->private_data
;
2297 mutex_lock(&inode
->i_mutex
);
2298 retval
= fasync_helper(fd
, filp
, on
, &counter
->fasync
);
2299 mutex_unlock(&inode
->i_mutex
);
2307 static const struct file_operations perf_fops
= {
2308 .release
= perf_release
,
2311 .unlocked_ioctl
= perf_ioctl
,
2312 .compat_ioctl
= perf_ioctl
,
2314 .fasync
= perf_fasync
,
2318 * Perf counter wakeup
2320 * If there's data, ensure we set the poll() state and publish everything
2321 * to user-space before waking everybody up.
2324 void perf_counter_wakeup(struct perf_counter
*counter
)
2326 wake_up_all(&counter
->waitq
);
2328 if (counter
->pending_kill
) {
2329 kill_fasync(&counter
->fasync
, SIGIO
, counter
->pending_kill
);
2330 counter
->pending_kill
= 0;
2337 * Handle the case where we need to wakeup up from NMI (or rq->lock) context.
2339 * The NMI bit means we cannot possibly take locks. Therefore, maintain a
2340 * single linked list and use cmpxchg() to add entries lockless.
2343 static void perf_pending_counter(struct perf_pending_entry
*entry
)
2345 struct perf_counter
*counter
= container_of(entry
,
2346 struct perf_counter
, pending
);
2348 if (counter
->pending_disable
) {
2349 counter
->pending_disable
= 0;
2350 __perf_counter_disable(counter
);
2353 if (counter
->pending_wakeup
) {
2354 counter
->pending_wakeup
= 0;
2355 perf_counter_wakeup(counter
);
2359 #define PENDING_TAIL ((struct perf_pending_entry *)-1UL)
2361 static DEFINE_PER_CPU(struct perf_pending_entry
*, perf_pending_head
) = {
2365 static void perf_pending_queue(struct perf_pending_entry
*entry
,
2366 void (*func
)(struct perf_pending_entry
*))
2368 struct perf_pending_entry
**head
;
2370 if (cmpxchg(&entry
->next
, NULL
, PENDING_TAIL
) != NULL
)
2375 head
= &get_cpu_var(perf_pending_head
);
2378 entry
->next
= *head
;
2379 } while (cmpxchg(head
, entry
->next
, entry
) != entry
->next
);
2381 set_perf_counter_pending();
2383 put_cpu_var(perf_pending_head
);
2386 static int __perf_pending_run(void)
2388 struct perf_pending_entry
*list
;
2391 list
= xchg(&__get_cpu_var(perf_pending_head
), PENDING_TAIL
);
2392 while (list
!= PENDING_TAIL
) {
2393 void (*func
)(struct perf_pending_entry
*);
2394 struct perf_pending_entry
*entry
= list
;
2401 * Ensure we observe the unqueue before we issue the wakeup,
2402 * so that we won't be waiting forever.
2403 * -- see perf_not_pending().
2414 static inline int perf_not_pending(struct perf_counter
*counter
)
2417 * If we flush on whatever cpu we run, there is a chance we don't
2421 __perf_pending_run();
2425 * Ensure we see the proper queue state before going to sleep
2426 * so that we do not miss the wakeup. -- see perf_pending_handle()
2429 return counter
->pending
.next
== NULL
;
2432 static void perf_pending_sync(struct perf_counter
*counter
)
2434 wait_event(counter
->waitq
, perf_not_pending(counter
));
2437 void perf_counter_do_pending(void)
2439 __perf_pending_run();
2443 * Callchain support -- arch specific
2446 __weak
struct perf_callchain_entry
*perf_callchain(struct pt_regs
*regs
)
2455 struct perf_output_handle
{
2456 struct perf_counter
*counter
;
2457 struct perf_mmap_data
*data
;
2459 unsigned long offset
;
2463 unsigned long flags
;
2466 static bool perf_output_space(struct perf_mmap_data
*data
,
2467 unsigned int offset
, unsigned int head
)
2472 if (!data
->writable
)
2475 mask
= (data
->nr_pages
<< PAGE_SHIFT
) - 1;
2477 * Userspace could choose to issue a mb() before updating the tail
2478 * pointer. So that all reads will be completed before the write is
2481 tail
= ACCESS_ONCE(data
->user_page
->data_tail
);
2484 offset
= (offset
- tail
) & mask
;
2485 head
= (head
- tail
) & mask
;
2487 if ((int)(head
- offset
) < 0)
2493 static void perf_output_wakeup(struct perf_output_handle
*handle
)
2495 atomic_set(&handle
->data
->poll
, POLL_IN
);
2498 handle
->counter
->pending_wakeup
= 1;
2499 perf_pending_queue(&handle
->counter
->pending
,
2500 perf_pending_counter
);
2502 perf_counter_wakeup(handle
->counter
);
2506 * Curious locking construct.
2508 * We need to ensure a later event doesn't publish a head when a former
2509 * event isn't done writing. However since we need to deal with NMIs we
2510 * cannot fully serialize things.
2512 * What we do is serialize between CPUs so we only have to deal with NMI
2513 * nesting on a single CPU.
2515 * We only publish the head (and generate a wakeup) when the outer-most
2518 static void perf_output_lock(struct perf_output_handle
*handle
)
2520 struct perf_mmap_data
*data
= handle
->data
;
2525 local_irq_save(handle
->flags
);
2526 cpu
= smp_processor_id();
2528 if (in_nmi() && atomic_read(&data
->lock
) == cpu
)
2531 while (atomic_cmpxchg(&data
->lock
, -1, cpu
) != -1)
2537 static void perf_output_unlock(struct perf_output_handle
*handle
)
2539 struct perf_mmap_data
*data
= handle
->data
;
2543 data
->done_head
= data
->head
;
2545 if (!handle
->locked
)
2550 * The xchg implies a full barrier that ensures all writes are done
2551 * before we publish the new head, matched by a rmb() in userspace when
2552 * reading this position.
2554 while ((head
= atomic_long_xchg(&data
->done_head
, 0)))
2555 data
->user_page
->data_head
= head
;
2558 * NMI can happen here, which means we can miss a done_head update.
2561 cpu
= atomic_xchg(&data
->lock
, -1);
2562 WARN_ON_ONCE(cpu
!= smp_processor_id());
2565 * Therefore we have to validate we did not indeed do so.
2567 if (unlikely(atomic_long_read(&data
->done_head
))) {
2569 * Since we had it locked, we can lock it again.
2571 while (atomic_cmpxchg(&data
->lock
, -1, cpu
) != -1)
2577 if (atomic_xchg(&data
->wakeup
, 0))
2578 perf_output_wakeup(handle
);
2580 local_irq_restore(handle
->flags
);
2583 static void perf_output_copy(struct perf_output_handle
*handle
,
2584 const void *buf
, unsigned int len
)
2586 unsigned int pages_mask
;
2587 unsigned int offset
;
2591 offset
= handle
->offset
;
2592 pages_mask
= handle
->data
->nr_pages
- 1;
2593 pages
= handle
->data
->data_pages
;
2596 unsigned int page_offset
;
2599 nr
= (offset
>> PAGE_SHIFT
) & pages_mask
;
2600 page_offset
= offset
& (PAGE_SIZE
- 1);
2601 size
= min_t(unsigned int, PAGE_SIZE
- page_offset
, len
);
2603 memcpy(pages
[nr
] + page_offset
, buf
, size
);
2610 handle
->offset
= offset
;
2613 * Check we didn't copy past our reservation window, taking the
2614 * possible unsigned int wrap into account.
2616 WARN_ON_ONCE(((long)(handle
->head
- handle
->offset
)) < 0);
2619 #define perf_output_put(handle, x) \
2620 perf_output_copy((handle), &(x), sizeof(x))
2622 static int perf_output_begin(struct perf_output_handle
*handle
,
2623 struct perf_counter
*counter
, unsigned int size
,
2624 int nmi
, int sample
)
2626 struct perf_mmap_data
*data
;
2627 unsigned int offset
, head
;
2630 struct perf_event_header header
;
2636 * For inherited counters we send all the output towards the parent.
2638 if (counter
->parent
)
2639 counter
= counter
->parent
;
2642 data
= rcu_dereference(counter
->data
);
2646 handle
->data
= data
;
2647 handle
->counter
= counter
;
2649 handle
->sample
= sample
;
2651 if (!data
->nr_pages
)
2654 have_lost
= atomic_read(&data
->lost
);
2656 size
+= sizeof(lost_event
);
2658 perf_output_lock(handle
);
2661 offset
= head
= atomic_long_read(&data
->head
);
2663 if (unlikely(!perf_output_space(data
, offset
, head
)))
2665 } while (atomic_long_cmpxchg(&data
->head
, offset
, head
) != offset
);
2667 handle
->offset
= offset
;
2668 handle
->head
= head
;
2670 if ((offset
>> PAGE_SHIFT
) != (head
>> PAGE_SHIFT
))
2671 atomic_set(&data
->wakeup
, 1);
2674 lost_event
.header
.type
= PERF_EVENT_LOST
;
2675 lost_event
.header
.misc
= 0;
2676 lost_event
.header
.size
= sizeof(lost_event
);
2677 lost_event
.id
= counter
->id
;
2678 lost_event
.lost
= atomic_xchg(&data
->lost
, 0);
2680 perf_output_put(handle
, lost_event
);
2686 atomic_inc(&data
->lost
);
2687 perf_output_unlock(handle
);
2694 static void perf_output_end(struct perf_output_handle
*handle
)
2696 struct perf_counter
*counter
= handle
->counter
;
2697 struct perf_mmap_data
*data
= handle
->data
;
2699 int wakeup_events
= counter
->attr
.wakeup_events
;
2701 if (handle
->sample
&& wakeup_events
) {
2702 int events
= atomic_inc_return(&data
->events
);
2703 if (events
>= wakeup_events
) {
2704 atomic_sub(wakeup_events
, &data
->events
);
2705 atomic_set(&data
->wakeup
, 1);
2709 perf_output_unlock(handle
);
2713 static u32
perf_counter_pid(struct perf_counter
*counter
, struct task_struct
*p
)
2716 * only top level counters have the pid namespace they were created in
2718 if (counter
->parent
)
2719 counter
= counter
->parent
;
2721 return task_tgid_nr_ns(p
, counter
->ns
);
2724 static u32
perf_counter_tid(struct perf_counter
*counter
, struct task_struct
*p
)
2727 * only top level counters have the pid namespace they were created in
2729 if (counter
->parent
)
2730 counter
= counter
->parent
;
2732 return task_pid_nr_ns(p
, counter
->ns
);
2735 static void perf_output_read_one(struct perf_output_handle
*handle
,
2736 struct perf_counter
*counter
)
2738 u64 read_format
= counter
->attr
.read_format
;
2742 values
[n
++] = atomic64_read(&counter
->count
);
2743 if (read_format
& PERF_FORMAT_TOTAL_TIME_ENABLED
) {
2744 values
[n
++] = counter
->total_time_enabled
+
2745 atomic64_read(&counter
->child_total_time_enabled
);
2747 if (read_format
& PERF_FORMAT_TOTAL_TIME_RUNNING
) {
2748 values
[n
++] = counter
->total_time_running
+
2749 atomic64_read(&counter
->child_total_time_running
);
2751 if (read_format
& PERF_FORMAT_ID
)
2752 values
[n
++] = primary_counter_id(counter
);
2754 perf_output_copy(handle
, values
, n
* sizeof(u64
));
2758 * XXX PERF_FORMAT_GROUP vs inherited counters seems difficult.
2760 static void perf_output_read_group(struct perf_output_handle
*handle
,
2761 struct perf_counter
*counter
)
2763 struct perf_counter
*leader
= counter
->group_leader
, *sub
;
2764 u64 read_format
= counter
->attr
.read_format
;
2768 values
[n
++] = 1 + leader
->nr_siblings
;
2770 if (read_format
& PERF_FORMAT_TOTAL_TIME_ENABLED
)
2771 values
[n
++] = leader
->total_time_enabled
;
2773 if (read_format
& PERF_FORMAT_TOTAL_TIME_RUNNING
)
2774 values
[n
++] = leader
->total_time_running
;
2776 if (leader
!= counter
)
2777 leader
->pmu
->read(leader
);
2779 values
[n
++] = atomic64_read(&leader
->count
);
2780 if (read_format
& PERF_FORMAT_ID
)
2781 values
[n
++] = primary_counter_id(leader
);
2783 perf_output_copy(handle
, values
, n
* sizeof(u64
));
2785 list_for_each_entry(sub
, &leader
->sibling_list
, list_entry
) {
2789 sub
->pmu
->read(sub
);
2791 values
[n
++] = atomic64_read(&sub
->count
);
2792 if (read_format
& PERF_FORMAT_ID
)
2793 values
[n
++] = primary_counter_id(sub
);
2795 perf_output_copy(handle
, values
, n
* sizeof(u64
));
2799 static void perf_output_read(struct perf_output_handle
*handle
,
2800 struct perf_counter
*counter
)
2802 if (counter
->attr
.read_format
& PERF_FORMAT_GROUP
)
2803 perf_output_read_group(handle
, counter
);
2805 perf_output_read_one(handle
, counter
);
2808 void perf_counter_output(struct perf_counter
*counter
, int nmi
,
2809 struct perf_sample_data
*data
)
2812 u64 sample_type
= counter
->attr
.sample_type
;
2813 struct perf_output_handle handle
;
2814 struct perf_event_header header
;
2819 struct perf_callchain_entry
*callchain
= NULL
;
2820 int callchain_size
= 0;
2826 header
.type
= PERF_EVENT_SAMPLE
;
2827 header
.size
= sizeof(header
);
2830 header
.misc
|= perf_misc_flags(data
->regs
);
2832 if (sample_type
& PERF_SAMPLE_IP
) {
2833 ip
= perf_instruction_pointer(data
->regs
);
2834 header
.size
+= sizeof(ip
);
2837 if (sample_type
& PERF_SAMPLE_TID
) {
2838 /* namespace issues */
2839 tid_entry
.pid
= perf_counter_pid(counter
, current
);
2840 tid_entry
.tid
= perf_counter_tid(counter
, current
);
2842 header
.size
+= sizeof(tid_entry
);
2845 if (sample_type
& PERF_SAMPLE_TIME
) {
2847 * Maybe do better on x86 and provide cpu_clock_nmi()
2849 time
= sched_clock();
2851 header
.size
+= sizeof(u64
);
2854 if (sample_type
& PERF_SAMPLE_ADDR
)
2855 header
.size
+= sizeof(u64
);
2857 if (sample_type
& PERF_SAMPLE_ID
)
2858 header
.size
+= sizeof(u64
);
2860 if (sample_type
& PERF_SAMPLE_STREAM_ID
)
2861 header
.size
+= sizeof(u64
);
2863 if (sample_type
& PERF_SAMPLE_CPU
) {
2864 header
.size
+= sizeof(cpu_entry
);
2866 cpu_entry
.cpu
= raw_smp_processor_id();
2867 cpu_entry
.reserved
= 0;
2870 if (sample_type
& PERF_SAMPLE_PERIOD
)
2871 header
.size
+= sizeof(u64
);
2873 if (sample_type
& PERF_SAMPLE_READ
)
2874 header
.size
+= perf_counter_read_size(counter
);
2876 if (sample_type
& PERF_SAMPLE_CALLCHAIN
) {
2877 callchain
= perf_callchain(data
->regs
);
2880 callchain_size
= (1 + callchain
->nr
) * sizeof(u64
);
2881 header
.size
+= callchain_size
;
2883 header
.size
+= sizeof(u64
);
2886 if (sample_type
& PERF_SAMPLE_RAW
) {
2887 int size
= sizeof(u32
);
2890 size
+= data
->raw
->size
;
2892 size
+= sizeof(u32
);
2894 WARN_ON_ONCE(size
& (sizeof(u64
)-1));
2895 header
.size
+= size
;
2898 ret
= perf_output_begin(&handle
, counter
, header
.size
, nmi
, 1);
2902 perf_output_put(&handle
, header
);
2904 if (sample_type
& PERF_SAMPLE_IP
)
2905 perf_output_put(&handle
, ip
);
2907 if (sample_type
& PERF_SAMPLE_TID
)
2908 perf_output_put(&handle
, tid_entry
);
2910 if (sample_type
& PERF_SAMPLE_TIME
)
2911 perf_output_put(&handle
, time
);
2913 if (sample_type
& PERF_SAMPLE_ADDR
)
2914 perf_output_put(&handle
, data
->addr
);
2916 if (sample_type
& PERF_SAMPLE_ID
) {
2917 u64 id
= primary_counter_id(counter
);
2919 perf_output_put(&handle
, id
);
2922 if (sample_type
& PERF_SAMPLE_STREAM_ID
)
2923 perf_output_put(&handle
, counter
->id
);
2925 if (sample_type
& PERF_SAMPLE_CPU
)
2926 perf_output_put(&handle
, cpu_entry
);
2928 if (sample_type
& PERF_SAMPLE_PERIOD
)
2929 perf_output_put(&handle
, data
->period
);
2931 if (sample_type
& PERF_SAMPLE_READ
)
2932 perf_output_read(&handle
, counter
);
2934 if (sample_type
& PERF_SAMPLE_CALLCHAIN
) {
2936 perf_output_copy(&handle
, callchain
, callchain_size
);
2939 perf_output_put(&handle
, nr
);
2943 if (sample_type
& PERF_SAMPLE_RAW
) {
2945 perf_output_put(&handle
, data
->raw
->size
);
2946 perf_output_copy(&handle
, data
->raw
->data
, data
->raw
->size
);
2952 .size
= sizeof(u32
),
2955 perf_output_put(&handle
, raw
);
2959 perf_output_end(&handle
);
2966 struct perf_read_event
{
2967 struct perf_event_header header
;
2974 perf_counter_read_event(struct perf_counter
*counter
,
2975 struct task_struct
*task
)
2977 struct perf_output_handle handle
;
2978 struct perf_read_event event
= {
2980 .type
= PERF_EVENT_READ
,
2982 .size
= sizeof(event
) + perf_counter_read_size(counter
),
2984 .pid
= perf_counter_pid(counter
, task
),
2985 .tid
= perf_counter_tid(counter
, task
),
2989 ret
= perf_output_begin(&handle
, counter
, event
.header
.size
, 0, 0);
2993 perf_output_put(&handle
, event
);
2994 perf_output_read(&handle
, counter
);
2996 perf_output_end(&handle
);
3000 * task tracking -- fork/exit
3002 * enabled by: attr.comm | attr.mmap | attr.task
3005 struct perf_task_event
{
3006 struct task_struct
*task
;
3007 struct perf_counter_context
*task_ctx
;
3010 struct perf_event_header header
;
3019 static void perf_counter_task_output(struct perf_counter
*counter
,
3020 struct perf_task_event
*task_event
)
3022 struct perf_output_handle handle
;
3023 int size
= task_event
->event
.header
.size
;
3024 struct task_struct
*task
= task_event
->task
;
3025 int ret
= perf_output_begin(&handle
, counter
, size
, 0, 0);
3030 task_event
->event
.pid
= perf_counter_pid(counter
, task
);
3031 task_event
->event
.ppid
= perf_counter_pid(counter
, task
->real_parent
);
3033 task_event
->event
.tid
= perf_counter_tid(counter
, task
);
3034 task_event
->event
.ptid
= perf_counter_tid(counter
, task
->real_parent
);
3036 perf_output_put(&handle
, task_event
->event
);
3037 perf_output_end(&handle
);
3040 static int perf_counter_task_match(struct perf_counter
*counter
)
3042 if (counter
->attr
.comm
|| counter
->attr
.mmap
|| counter
->attr
.task
)
3048 static void perf_counter_task_ctx(struct perf_counter_context
*ctx
,
3049 struct perf_task_event
*task_event
)
3051 struct perf_counter
*counter
;
3053 if (system_state
!= SYSTEM_RUNNING
|| list_empty(&ctx
->event_list
))
3057 list_for_each_entry_rcu(counter
, &ctx
->event_list
, event_entry
) {
3058 if (perf_counter_task_match(counter
))
3059 perf_counter_task_output(counter
, task_event
);
3064 static void perf_counter_task_event(struct perf_task_event
*task_event
)
3066 struct perf_cpu_context
*cpuctx
;
3067 struct perf_counter_context
*ctx
= task_event
->task_ctx
;
3069 cpuctx
= &get_cpu_var(perf_cpu_context
);
3070 perf_counter_task_ctx(&cpuctx
->ctx
, task_event
);
3071 put_cpu_var(perf_cpu_context
);
3075 ctx
= rcu_dereference(task_event
->task
->perf_counter_ctxp
);
3077 perf_counter_task_ctx(ctx
, task_event
);
3081 static void perf_counter_task(struct task_struct
*task
,
3082 struct perf_counter_context
*task_ctx
,
3085 struct perf_task_event task_event
;
3087 if (!atomic_read(&nr_comm_counters
) &&
3088 !atomic_read(&nr_mmap_counters
) &&
3089 !atomic_read(&nr_task_counters
))
3092 task_event
= (struct perf_task_event
){
3094 .task_ctx
= task_ctx
,
3097 .type
= new ? PERF_EVENT_FORK
: PERF_EVENT_EXIT
,
3099 .size
= sizeof(task_event
.event
),
3108 perf_counter_task_event(&task_event
);
3111 void perf_counter_fork(struct task_struct
*task
)
3113 perf_counter_task(task
, NULL
, 1);
3120 struct perf_comm_event
{
3121 struct task_struct
*task
;
3126 struct perf_event_header header
;
3133 static void perf_counter_comm_output(struct perf_counter
*counter
,
3134 struct perf_comm_event
*comm_event
)
3136 struct perf_output_handle handle
;
3137 int size
= comm_event
->event
.header
.size
;
3138 int ret
= perf_output_begin(&handle
, counter
, size
, 0, 0);
3143 comm_event
->event
.pid
= perf_counter_pid(counter
, comm_event
->task
);
3144 comm_event
->event
.tid
= perf_counter_tid(counter
, comm_event
->task
);
3146 perf_output_put(&handle
, comm_event
->event
);
3147 perf_output_copy(&handle
, comm_event
->comm
,
3148 comm_event
->comm_size
);
3149 perf_output_end(&handle
);
3152 static int perf_counter_comm_match(struct perf_counter
*counter
)
3154 if (counter
->attr
.comm
)
3160 static void perf_counter_comm_ctx(struct perf_counter_context
*ctx
,
3161 struct perf_comm_event
*comm_event
)
3163 struct perf_counter
*counter
;
3165 if (system_state
!= SYSTEM_RUNNING
|| list_empty(&ctx
->event_list
))
3169 list_for_each_entry_rcu(counter
, &ctx
->event_list
, event_entry
) {
3170 if (perf_counter_comm_match(counter
))
3171 perf_counter_comm_output(counter
, comm_event
);
3176 static void perf_counter_comm_event(struct perf_comm_event
*comm_event
)
3178 struct perf_cpu_context
*cpuctx
;
3179 struct perf_counter_context
*ctx
;
3181 char comm
[TASK_COMM_LEN
];
3183 memset(comm
, 0, sizeof(comm
));
3184 strncpy(comm
, comm_event
->task
->comm
, sizeof(comm
));
3185 size
= ALIGN(strlen(comm
)+1, sizeof(u64
));
3187 comm_event
->comm
= comm
;
3188 comm_event
->comm_size
= size
;
3190 comm_event
->event
.header
.size
= sizeof(comm_event
->event
) + size
;
3192 cpuctx
= &get_cpu_var(perf_cpu_context
);
3193 perf_counter_comm_ctx(&cpuctx
->ctx
, comm_event
);
3194 put_cpu_var(perf_cpu_context
);
3198 * doesn't really matter which of the child contexts the
3199 * events ends up in.
3201 ctx
= rcu_dereference(current
->perf_counter_ctxp
);
3203 perf_counter_comm_ctx(ctx
, comm_event
);
3207 void perf_counter_comm(struct task_struct
*task
)
3209 struct perf_comm_event comm_event
;
3211 if (task
->perf_counter_ctxp
)
3212 perf_counter_enable_on_exec(task
);
3214 if (!atomic_read(&nr_comm_counters
))
3217 comm_event
= (struct perf_comm_event
){
3223 .type
= PERF_EVENT_COMM
,
3232 perf_counter_comm_event(&comm_event
);
3239 struct perf_mmap_event
{
3240 struct vm_area_struct
*vma
;
3242 const char *file_name
;
3246 struct perf_event_header header
;
3256 static void perf_counter_mmap_output(struct perf_counter
*counter
,
3257 struct perf_mmap_event
*mmap_event
)
3259 struct perf_output_handle handle
;
3260 int size
= mmap_event
->event
.header
.size
;
3261 int ret
= perf_output_begin(&handle
, counter
, size
, 0, 0);
3266 mmap_event
->event
.pid
= perf_counter_pid(counter
, current
);
3267 mmap_event
->event
.tid
= perf_counter_tid(counter
, current
);
3269 perf_output_put(&handle
, mmap_event
->event
);
3270 perf_output_copy(&handle
, mmap_event
->file_name
,
3271 mmap_event
->file_size
);
3272 perf_output_end(&handle
);
3275 static int perf_counter_mmap_match(struct perf_counter
*counter
,
3276 struct perf_mmap_event
*mmap_event
)
3278 if (counter
->attr
.mmap
)
3284 static void perf_counter_mmap_ctx(struct perf_counter_context
*ctx
,
3285 struct perf_mmap_event
*mmap_event
)
3287 struct perf_counter
*counter
;
3289 if (system_state
!= SYSTEM_RUNNING
|| list_empty(&ctx
->event_list
))
3293 list_for_each_entry_rcu(counter
, &ctx
->event_list
, event_entry
) {
3294 if (perf_counter_mmap_match(counter
, mmap_event
))
3295 perf_counter_mmap_output(counter
, mmap_event
);
3300 static void perf_counter_mmap_event(struct perf_mmap_event
*mmap_event
)
3302 struct perf_cpu_context
*cpuctx
;
3303 struct perf_counter_context
*ctx
;
3304 struct vm_area_struct
*vma
= mmap_event
->vma
;
3305 struct file
*file
= vma
->vm_file
;
3311 memset(tmp
, 0, sizeof(tmp
));
3315 * d_path works from the end of the buffer backwards, so we
3316 * need to add enough zero bytes after the string to handle
3317 * the 64bit alignment we do later.
3319 buf
= kzalloc(PATH_MAX
+ sizeof(u64
), GFP_KERNEL
);
3321 name
= strncpy(tmp
, "//enomem", sizeof(tmp
));
3324 name
= d_path(&file
->f_path
, buf
, PATH_MAX
);
3326 name
= strncpy(tmp
, "//toolong", sizeof(tmp
));
3330 if (arch_vma_name(mmap_event
->vma
)) {
3331 name
= strncpy(tmp
, arch_vma_name(mmap_event
->vma
),
3337 name
= strncpy(tmp
, "[vdso]", sizeof(tmp
));
3341 name
= strncpy(tmp
, "//anon", sizeof(tmp
));
3346 size
= ALIGN(strlen(name
)+1, sizeof(u64
));
3348 mmap_event
->file_name
= name
;
3349 mmap_event
->file_size
= size
;
3351 mmap_event
->event
.header
.size
= sizeof(mmap_event
->event
) + size
;
3353 cpuctx
= &get_cpu_var(perf_cpu_context
);
3354 perf_counter_mmap_ctx(&cpuctx
->ctx
, mmap_event
);
3355 put_cpu_var(perf_cpu_context
);
3359 * doesn't really matter which of the child contexts the
3360 * events ends up in.
3362 ctx
= rcu_dereference(current
->perf_counter_ctxp
);
3364 perf_counter_mmap_ctx(ctx
, mmap_event
);
3370 void __perf_counter_mmap(struct vm_area_struct
*vma
)
3372 struct perf_mmap_event mmap_event
;
3374 if (!atomic_read(&nr_mmap_counters
))
3377 mmap_event
= (struct perf_mmap_event
){
3383 .type
= PERF_EVENT_MMAP
,
3389 .start
= vma
->vm_start
,
3390 .len
= vma
->vm_end
- vma
->vm_start
,
3391 .pgoff
= vma
->vm_pgoff
,
3395 perf_counter_mmap_event(&mmap_event
);
3399 * IRQ throttle logging
3402 static void perf_log_throttle(struct perf_counter
*counter
, int enable
)
3404 struct perf_output_handle handle
;
3408 struct perf_event_header header
;
3412 } throttle_event
= {
3414 .type
= PERF_EVENT_THROTTLE
,
3416 .size
= sizeof(throttle_event
),
3418 .time
= sched_clock(),
3419 .id
= primary_counter_id(counter
),
3420 .stream_id
= counter
->id
,
3424 throttle_event
.header
.type
= PERF_EVENT_UNTHROTTLE
;
3426 ret
= perf_output_begin(&handle
, counter
, sizeof(throttle_event
), 1, 0);
3430 perf_output_put(&handle
, throttle_event
);
3431 perf_output_end(&handle
);
3435 * Generic counter overflow handling, sampling.
3438 int perf_counter_overflow(struct perf_counter
*counter
, int nmi
,
3439 struct perf_sample_data
*data
)
3441 int events
= atomic_read(&counter
->event_limit
);
3442 int throttle
= counter
->pmu
->unthrottle
!= NULL
;
3443 struct hw_perf_counter
*hwc
= &counter
->hw
;
3449 if (hwc
->interrupts
!= MAX_INTERRUPTS
) {
3451 if (HZ
* hwc
->interrupts
>
3452 (u64
)sysctl_perf_counter_sample_rate
) {
3453 hwc
->interrupts
= MAX_INTERRUPTS
;
3454 perf_log_throttle(counter
, 0);
3459 * Keep re-disabling counters even though on the previous
3460 * pass we disabled it - just in case we raced with a
3461 * sched-in and the counter got enabled again:
3467 if (counter
->attr
.freq
) {
3468 u64 now
= sched_clock();
3469 s64 delta
= now
- hwc
->freq_stamp
;
3471 hwc
->freq_stamp
= now
;
3473 if (delta
> 0 && delta
< TICK_NSEC
)
3474 perf_adjust_period(counter
, NSEC_PER_SEC
/ (int)delta
);
3478 * XXX event_limit might not quite work as expected on inherited
3482 counter
->pending_kill
= POLL_IN
;
3483 if (events
&& atomic_dec_and_test(&counter
->event_limit
)) {
3485 counter
->pending_kill
= POLL_HUP
;
3487 counter
->pending_disable
= 1;
3488 perf_pending_queue(&counter
->pending
,
3489 perf_pending_counter
);
3491 perf_counter_disable(counter
);
3494 perf_counter_output(counter
, nmi
, data
);
3499 * Generic software counter infrastructure
3503 * We directly increment counter->count and keep a second value in
3504 * counter->hw.period_left to count intervals. This period counter
3505 * is kept in the range [-sample_period, 0] so that we can use the
3509 static u64
perf_swcounter_set_period(struct perf_counter
*counter
)
3511 struct hw_perf_counter
*hwc
= &counter
->hw
;
3512 u64 period
= hwc
->last_period
;
3516 hwc
->last_period
= hwc
->sample_period
;
3519 old
= val
= atomic64_read(&hwc
->period_left
);
3523 nr
= div64_u64(period
+ val
, period
);
3524 offset
= nr
* period
;
3526 if (atomic64_cmpxchg(&hwc
->period_left
, old
, val
) != old
)
3532 static void perf_swcounter_overflow(struct perf_counter
*counter
,
3533 int nmi
, struct perf_sample_data
*data
)
3535 struct hw_perf_counter
*hwc
= &counter
->hw
;
3538 data
->period
= counter
->hw
.last_period
;
3539 overflow
= perf_swcounter_set_period(counter
);
3541 if (hwc
->interrupts
== MAX_INTERRUPTS
)
3544 for (; overflow
; overflow
--) {
3545 if (perf_counter_overflow(counter
, nmi
, data
)) {
3547 * We inhibit the overflow from happening when
3548 * hwc->interrupts == MAX_INTERRUPTS.
3555 static void perf_swcounter_unthrottle(struct perf_counter
*counter
)
3558 * Nothing to do, we already reset hwc->interrupts.
3562 static void perf_swcounter_add(struct perf_counter
*counter
, u64 nr
,
3563 int nmi
, struct perf_sample_data
*data
)
3565 struct hw_perf_counter
*hwc
= &counter
->hw
;
3567 atomic64_add(nr
, &counter
->count
);
3569 if (!hwc
->sample_period
)
3575 if (!atomic64_add_negative(nr
, &hwc
->period_left
))
3576 perf_swcounter_overflow(counter
, nmi
, data
);
3579 static int perf_swcounter_is_counting(struct perf_counter
*counter
)
3582 * The counter is active, we're good!
3584 if (counter
->state
== PERF_COUNTER_STATE_ACTIVE
)
3588 * The counter is off/error, not counting.
3590 if (counter
->state
!= PERF_COUNTER_STATE_INACTIVE
)
3594 * The counter is inactive, if the context is active
3595 * we're part of a group that didn't make it on the 'pmu',
3598 if (counter
->ctx
->is_active
)
3602 * We're inactive and the context is too, this means the
3603 * task is scheduled out, we're counting events that happen
3604 * to us, like migration events.
3609 static int perf_swcounter_match(struct perf_counter
*counter
,
3610 enum perf_type_id type
,
3611 u32 event
, struct pt_regs
*regs
)
3613 if (!perf_swcounter_is_counting(counter
))
3616 if (counter
->attr
.type
!= type
)
3618 if (counter
->attr
.config
!= event
)
3622 if (counter
->attr
.exclude_user
&& user_mode(regs
))
3625 if (counter
->attr
.exclude_kernel
&& !user_mode(regs
))
3632 static void perf_swcounter_ctx_event(struct perf_counter_context
*ctx
,
3633 enum perf_type_id type
,
3634 u32 event
, u64 nr
, int nmi
,
3635 struct perf_sample_data
*data
)
3637 struct perf_counter
*counter
;
3639 if (system_state
!= SYSTEM_RUNNING
|| list_empty(&ctx
->event_list
))
3643 list_for_each_entry_rcu(counter
, &ctx
->event_list
, event_entry
) {
3644 if (perf_swcounter_match(counter
, type
, event
, data
->regs
))
3645 perf_swcounter_add(counter
, nr
, nmi
, data
);
3650 static int *perf_swcounter_recursion_context(struct perf_cpu_context
*cpuctx
)
3653 return &cpuctx
->recursion
[3];
3656 return &cpuctx
->recursion
[2];
3659 return &cpuctx
->recursion
[1];
3661 return &cpuctx
->recursion
[0];
3664 static void do_perf_swcounter_event(enum perf_type_id type
, u32 event
,
3666 struct perf_sample_data
*data
)
3668 struct perf_cpu_context
*cpuctx
= &get_cpu_var(perf_cpu_context
);
3669 int *recursion
= perf_swcounter_recursion_context(cpuctx
);
3670 struct perf_counter_context
*ctx
;
3678 perf_swcounter_ctx_event(&cpuctx
->ctx
, type
, event
,
3682 * doesn't really matter which of the child contexts the
3683 * events ends up in.
3685 ctx
= rcu_dereference(current
->perf_counter_ctxp
);
3687 perf_swcounter_ctx_event(ctx
, type
, event
, nr
, nmi
, data
);
3694 put_cpu_var(perf_cpu_context
);
3697 void __perf_swcounter_event(u32 event
, u64 nr
, int nmi
,
3698 struct pt_regs
*regs
, u64 addr
)
3700 struct perf_sample_data data
= {
3705 do_perf_swcounter_event(PERF_TYPE_SOFTWARE
, event
, nr
, nmi
, &data
);
3708 static void perf_swcounter_read(struct perf_counter
*counter
)
3712 static int perf_swcounter_enable(struct perf_counter
*counter
)
3714 struct hw_perf_counter
*hwc
= &counter
->hw
;
3716 if (hwc
->sample_period
) {
3717 hwc
->last_period
= hwc
->sample_period
;
3718 perf_swcounter_set_period(counter
);
3723 static void perf_swcounter_disable(struct perf_counter
*counter
)
3727 static const struct pmu perf_ops_generic
= {
3728 .enable
= perf_swcounter_enable
,
3729 .disable
= perf_swcounter_disable
,
3730 .read
= perf_swcounter_read
,
3731 .unthrottle
= perf_swcounter_unthrottle
,
3735 * hrtimer based swcounter callback
3738 static enum hrtimer_restart
perf_swcounter_hrtimer(struct hrtimer
*hrtimer
)
3740 enum hrtimer_restart ret
= HRTIMER_RESTART
;
3741 struct perf_sample_data data
;
3742 struct perf_counter
*counter
;
3745 counter
= container_of(hrtimer
, struct perf_counter
, hw
.hrtimer
);
3746 counter
->pmu
->read(counter
);
3749 data
.regs
= get_irq_regs();
3751 * In case we exclude kernel IPs or are somehow not in interrupt
3752 * context, provide the next best thing, the user IP.
3754 if ((counter
->attr
.exclude_kernel
|| !data
.regs
) &&
3755 !counter
->attr
.exclude_user
)
3756 data
.regs
= task_pt_regs(current
);
3759 if (perf_counter_overflow(counter
, 0, &data
))
3760 ret
= HRTIMER_NORESTART
;
3763 period
= max_t(u64
, 10000, counter
->hw
.sample_period
);
3764 hrtimer_forward_now(hrtimer
, ns_to_ktime(period
));
3770 * Software counter: cpu wall time clock
3773 static void cpu_clock_perf_counter_update(struct perf_counter
*counter
)
3775 int cpu
= raw_smp_processor_id();
3779 now
= cpu_clock(cpu
);
3780 prev
= atomic64_read(&counter
->hw
.prev_count
);
3781 atomic64_set(&counter
->hw
.prev_count
, now
);
3782 atomic64_add(now
- prev
, &counter
->count
);
3785 static int cpu_clock_perf_counter_enable(struct perf_counter
*counter
)
3787 struct hw_perf_counter
*hwc
= &counter
->hw
;
3788 int cpu
= raw_smp_processor_id();
3790 atomic64_set(&hwc
->prev_count
, cpu_clock(cpu
));
3791 hrtimer_init(&hwc
->hrtimer
, CLOCK_MONOTONIC
, HRTIMER_MODE_REL
);
3792 hwc
->hrtimer
.function
= perf_swcounter_hrtimer
;
3793 if (hwc
->sample_period
) {
3794 u64 period
= max_t(u64
, 10000, hwc
->sample_period
);
3795 __hrtimer_start_range_ns(&hwc
->hrtimer
,
3796 ns_to_ktime(period
), 0,
3797 HRTIMER_MODE_REL
, 0);
3803 static void cpu_clock_perf_counter_disable(struct perf_counter
*counter
)
3805 if (counter
->hw
.sample_period
)
3806 hrtimer_cancel(&counter
->hw
.hrtimer
);
3807 cpu_clock_perf_counter_update(counter
);
3810 static void cpu_clock_perf_counter_read(struct perf_counter
*counter
)
3812 cpu_clock_perf_counter_update(counter
);
3815 static const struct pmu perf_ops_cpu_clock
= {
3816 .enable
= cpu_clock_perf_counter_enable
,
3817 .disable
= cpu_clock_perf_counter_disable
,
3818 .read
= cpu_clock_perf_counter_read
,
3822 * Software counter: task time clock
3825 static void task_clock_perf_counter_update(struct perf_counter
*counter
, u64 now
)
3830 prev
= atomic64_xchg(&counter
->hw
.prev_count
, now
);
3832 atomic64_add(delta
, &counter
->count
);
3835 static int task_clock_perf_counter_enable(struct perf_counter
*counter
)
3837 struct hw_perf_counter
*hwc
= &counter
->hw
;
3840 now
= counter
->ctx
->time
;
3842 atomic64_set(&hwc
->prev_count
, now
);
3843 hrtimer_init(&hwc
->hrtimer
, CLOCK_MONOTONIC
, HRTIMER_MODE_REL
);
3844 hwc
->hrtimer
.function
= perf_swcounter_hrtimer
;
3845 if (hwc
->sample_period
) {
3846 u64 period
= max_t(u64
, 10000, hwc
->sample_period
);
3847 __hrtimer_start_range_ns(&hwc
->hrtimer
,
3848 ns_to_ktime(period
), 0,
3849 HRTIMER_MODE_REL
, 0);
3855 static void task_clock_perf_counter_disable(struct perf_counter
*counter
)
3857 if (counter
->hw
.sample_period
)
3858 hrtimer_cancel(&counter
->hw
.hrtimer
);
3859 task_clock_perf_counter_update(counter
, counter
->ctx
->time
);
3863 static void task_clock_perf_counter_read(struct perf_counter
*counter
)
3868 update_context_time(counter
->ctx
);
3869 time
= counter
->ctx
->time
;
3871 u64 now
= perf_clock();
3872 u64 delta
= now
- counter
->ctx
->timestamp
;
3873 time
= counter
->ctx
->time
+ delta
;
3876 task_clock_perf_counter_update(counter
, time
);
3879 static const struct pmu perf_ops_task_clock
= {
3880 .enable
= task_clock_perf_counter_enable
,
3881 .disable
= task_clock_perf_counter_disable
,
3882 .read
= task_clock_perf_counter_read
,
3885 #ifdef CONFIG_EVENT_PROFILE
3886 void perf_tpcounter_event(int event_id
, u64 addr
, u64 count
, void *record
,
3889 struct perf_raw_record raw
= {
3894 struct perf_sample_data data
= {
3895 .regs
= get_irq_regs(),
3901 data
.regs
= task_pt_regs(current
);
3903 do_perf_swcounter_event(PERF_TYPE_TRACEPOINT
, event_id
, count
, 1, &data
);
3905 EXPORT_SYMBOL_GPL(perf_tpcounter_event
);
3907 extern int ftrace_profile_enable(int);
3908 extern void ftrace_profile_disable(int);
3910 static void tp_perf_counter_destroy(struct perf_counter
*counter
)
3912 ftrace_profile_disable(counter
->attr
.config
);
3915 static const struct pmu
*tp_perf_counter_init(struct perf_counter
*counter
)
3918 * Raw tracepoint data is a severe data leak, only allow root to
3921 if ((counter
->attr
.sample_type
& PERF_SAMPLE_RAW
) &&
3922 !capable(CAP_SYS_ADMIN
))
3923 return ERR_PTR(-EPERM
);
3925 if (ftrace_profile_enable(counter
->attr
.config
))
3928 counter
->destroy
= tp_perf_counter_destroy
;
3930 return &perf_ops_generic
;
3933 static const struct pmu
*tp_perf_counter_init(struct perf_counter
*counter
)
3939 atomic_t perf_swcounter_enabled
[PERF_COUNT_SW_MAX
];
3941 static void sw_perf_counter_destroy(struct perf_counter
*counter
)
3943 u64 event
= counter
->attr
.config
;
3945 WARN_ON(counter
->parent
);
3947 atomic_dec(&perf_swcounter_enabled
[event
]);
3950 static const struct pmu
*sw_perf_counter_init(struct perf_counter
*counter
)
3952 const struct pmu
*pmu
= NULL
;
3953 u64 event
= counter
->attr
.config
;
3956 * Software counters (currently) can't in general distinguish
3957 * between user, kernel and hypervisor events.
3958 * However, context switches and cpu migrations are considered
3959 * to be kernel events, and page faults are never hypervisor
3963 case PERF_COUNT_SW_CPU_CLOCK
:
3964 pmu
= &perf_ops_cpu_clock
;
3967 case PERF_COUNT_SW_TASK_CLOCK
:
3969 * If the user instantiates this as a per-cpu counter,
3970 * use the cpu_clock counter instead.
3972 if (counter
->ctx
->task
)
3973 pmu
= &perf_ops_task_clock
;
3975 pmu
= &perf_ops_cpu_clock
;
3978 case PERF_COUNT_SW_PAGE_FAULTS
:
3979 case PERF_COUNT_SW_PAGE_FAULTS_MIN
:
3980 case PERF_COUNT_SW_PAGE_FAULTS_MAJ
:
3981 case PERF_COUNT_SW_CONTEXT_SWITCHES
:
3982 case PERF_COUNT_SW_CPU_MIGRATIONS
:
3983 if (!counter
->parent
) {
3984 atomic_inc(&perf_swcounter_enabled
[event
]);
3985 counter
->destroy
= sw_perf_counter_destroy
;
3987 pmu
= &perf_ops_generic
;
3995 * Allocate and initialize a counter structure
3997 static struct perf_counter
*
3998 perf_counter_alloc(struct perf_counter_attr
*attr
,
4000 struct perf_counter_context
*ctx
,
4001 struct perf_counter
*group_leader
,
4002 struct perf_counter
*parent_counter
,
4005 const struct pmu
*pmu
;
4006 struct perf_counter
*counter
;
4007 struct hw_perf_counter
*hwc
;
4010 counter
= kzalloc(sizeof(*counter
), gfpflags
);
4012 return ERR_PTR(-ENOMEM
);
4015 * Single counters are their own group leaders, with an
4016 * empty sibling list:
4019 group_leader
= counter
;
4021 mutex_init(&counter
->child_mutex
);
4022 INIT_LIST_HEAD(&counter
->child_list
);
4024 INIT_LIST_HEAD(&counter
->list_entry
);
4025 INIT_LIST_HEAD(&counter
->event_entry
);
4026 INIT_LIST_HEAD(&counter
->sibling_list
);
4027 init_waitqueue_head(&counter
->waitq
);
4029 mutex_init(&counter
->mmap_mutex
);
4032 counter
->attr
= *attr
;
4033 counter
->group_leader
= group_leader
;
4034 counter
->pmu
= NULL
;
4036 counter
->oncpu
= -1;
4038 counter
->parent
= parent_counter
;
4040 counter
->ns
= get_pid_ns(current
->nsproxy
->pid_ns
);
4041 counter
->id
= atomic64_inc_return(&perf_counter_id
);
4043 counter
->state
= PERF_COUNTER_STATE_INACTIVE
;
4046 counter
->state
= PERF_COUNTER_STATE_OFF
;
4051 hwc
->sample_period
= attr
->sample_period
;
4052 if (attr
->freq
&& attr
->sample_freq
)
4053 hwc
->sample_period
= 1;
4055 atomic64_set(&hwc
->period_left
, hwc
->sample_period
);
4058 * we currently do not support PERF_FORMAT_GROUP on inherited counters
4060 if (attr
->inherit
&& (attr
->read_format
& PERF_FORMAT_GROUP
))
4063 switch (attr
->type
) {
4065 case PERF_TYPE_HARDWARE
:
4066 case PERF_TYPE_HW_CACHE
:
4067 pmu
= hw_perf_counter_init(counter
);
4070 case PERF_TYPE_SOFTWARE
:
4071 pmu
= sw_perf_counter_init(counter
);
4074 case PERF_TYPE_TRACEPOINT
:
4075 pmu
= tp_perf_counter_init(counter
);
4085 else if (IS_ERR(pmu
))
4090 put_pid_ns(counter
->ns
);
4092 return ERR_PTR(err
);
4097 if (!counter
->parent
) {
4098 atomic_inc(&nr_counters
);
4099 if (counter
->attr
.mmap
)
4100 atomic_inc(&nr_mmap_counters
);
4101 if (counter
->attr
.comm
)
4102 atomic_inc(&nr_comm_counters
);
4103 if (counter
->attr
.task
)
4104 atomic_inc(&nr_task_counters
);
4110 static int perf_copy_attr(struct perf_counter_attr __user
*uattr
,
4111 struct perf_counter_attr
*attr
)
4116 if (!access_ok(VERIFY_WRITE
, uattr
, PERF_ATTR_SIZE_VER0
))
4120 * zero the full structure, so that a short copy will be nice.
4122 memset(attr
, 0, sizeof(*attr
));
4124 ret
= get_user(size
, &uattr
->size
);
4128 if (size
> PAGE_SIZE
) /* silly large */
4131 if (!size
) /* abi compat */
4132 size
= PERF_ATTR_SIZE_VER0
;
4134 if (size
< PERF_ATTR_SIZE_VER0
)
4138 * If we're handed a bigger struct than we know of,
4139 * ensure all the unknown bits are 0.
4141 if (size
> sizeof(*attr
)) {
4143 unsigned long __user
*addr
;
4144 unsigned long __user
*end
;
4146 addr
= PTR_ALIGN((void __user
*)uattr
+ sizeof(*attr
),
4147 sizeof(unsigned long));
4148 end
= PTR_ALIGN((void __user
*)uattr
+ size
,
4149 sizeof(unsigned long));
4151 for (; addr
< end
; addr
+= sizeof(unsigned long)) {
4152 ret
= get_user(val
, addr
);
4160 ret
= copy_from_user(attr
, uattr
, size
);
4165 * If the type exists, the corresponding creation will verify
4168 if (attr
->type
>= PERF_TYPE_MAX
)
4171 if (attr
->__reserved_1
|| attr
->__reserved_2
|| attr
->__reserved_3
)
4174 if (attr
->sample_type
& ~(PERF_SAMPLE_MAX
-1))
4177 if (attr
->read_format
& ~(PERF_FORMAT_MAX
-1))
4184 put_user(sizeof(*attr
), &uattr
->size
);
4190 * sys_perf_counter_open - open a performance counter, associate it to a task/cpu
4192 * @attr_uptr: event type attributes for monitoring/sampling
4195 * @group_fd: group leader counter fd
4197 SYSCALL_DEFINE5(perf_counter_open
,
4198 struct perf_counter_attr __user
*, attr_uptr
,
4199 pid_t
, pid
, int, cpu
, int, group_fd
, unsigned long, flags
)
4201 struct perf_counter
*counter
, *group_leader
;
4202 struct perf_counter_attr attr
;
4203 struct perf_counter_context
*ctx
;
4204 struct file
*counter_file
= NULL
;
4205 struct file
*group_file
= NULL
;
4206 int fput_needed
= 0;
4207 int fput_needed2
= 0;
4210 /* for future expandability... */
4214 ret
= perf_copy_attr(attr_uptr
, &attr
);
4218 if (!attr
.exclude_kernel
) {
4219 if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN
))
4224 if (attr
.sample_freq
> sysctl_perf_counter_sample_rate
)
4229 * Get the target context (task or percpu):
4231 ctx
= find_get_context(pid
, cpu
);
4233 return PTR_ERR(ctx
);
4236 * Look up the group leader (we will attach this counter to it):
4238 group_leader
= NULL
;
4239 if (group_fd
!= -1) {
4241 group_file
= fget_light(group_fd
, &fput_needed
);
4243 goto err_put_context
;
4244 if (group_file
->f_op
!= &perf_fops
)
4245 goto err_put_context
;
4247 group_leader
= group_file
->private_data
;
4249 * Do not allow a recursive hierarchy (this new sibling
4250 * becoming part of another group-sibling):
4252 if (group_leader
->group_leader
!= group_leader
)
4253 goto err_put_context
;
4255 * Do not allow to attach to a group in a different
4256 * task or CPU context:
4258 if (group_leader
->ctx
!= ctx
)
4259 goto err_put_context
;
4261 * Only a group leader can be exclusive or pinned
4263 if (attr
.exclusive
|| attr
.pinned
)
4264 goto err_put_context
;
4267 counter
= perf_counter_alloc(&attr
, cpu
, ctx
, group_leader
,
4269 ret
= PTR_ERR(counter
);
4270 if (IS_ERR(counter
))
4271 goto err_put_context
;
4273 ret
= anon_inode_getfd("[perf_counter]", &perf_fops
, counter
, 0);
4275 goto err_free_put_context
;
4277 counter_file
= fget_light(ret
, &fput_needed2
);
4279 goto err_free_put_context
;
4281 counter
->filp
= counter_file
;
4282 WARN_ON_ONCE(ctx
->parent_ctx
);
4283 mutex_lock(&ctx
->mutex
);
4284 perf_install_in_context(ctx
, counter
, cpu
);
4286 mutex_unlock(&ctx
->mutex
);
4288 counter
->owner
= current
;
4289 get_task_struct(current
);
4290 mutex_lock(¤t
->perf_counter_mutex
);
4291 list_add_tail(&counter
->owner_entry
, ¤t
->perf_counter_list
);
4292 mutex_unlock(¤t
->perf_counter_mutex
);
4294 fput_light(counter_file
, fput_needed2
);
4297 fput_light(group_file
, fput_needed
);
4301 err_free_put_context
:
4311 * inherit a counter from parent task to child task:
4313 static struct perf_counter
*
4314 inherit_counter(struct perf_counter
*parent_counter
,
4315 struct task_struct
*parent
,
4316 struct perf_counter_context
*parent_ctx
,
4317 struct task_struct
*child
,
4318 struct perf_counter
*group_leader
,
4319 struct perf_counter_context
*child_ctx
)
4321 struct perf_counter
*child_counter
;
4324 * Instead of creating recursive hierarchies of counters,
4325 * we link inherited counters back to the original parent,
4326 * which has a filp for sure, which we use as the reference
4329 if (parent_counter
->parent
)
4330 parent_counter
= parent_counter
->parent
;
4332 child_counter
= perf_counter_alloc(&parent_counter
->attr
,
4333 parent_counter
->cpu
, child_ctx
,
4334 group_leader
, parent_counter
,
4336 if (IS_ERR(child_counter
))
4337 return child_counter
;
4341 * Make the child state follow the state of the parent counter,
4342 * not its attr.disabled bit. We hold the parent's mutex,
4343 * so we won't race with perf_counter_{en, dis}able_family.
4345 if (parent_counter
->state
>= PERF_COUNTER_STATE_INACTIVE
)
4346 child_counter
->state
= PERF_COUNTER_STATE_INACTIVE
;
4348 child_counter
->state
= PERF_COUNTER_STATE_OFF
;
4350 if (parent_counter
->attr
.freq
)
4351 child_counter
->hw
.sample_period
= parent_counter
->hw
.sample_period
;
4354 * Link it up in the child's context:
4356 add_counter_to_ctx(child_counter
, child_ctx
);
4359 * Get a reference to the parent filp - we will fput it
4360 * when the child counter exits. This is safe to do because
4361 * we are in the parent and we know that the filp still
4362 * exists and has a nonzero count:
4364 atomic_long_inc(&parent_counter
->filp
->f_count
);
4367 * Link this into the parent counter's child list
4369 WARN_ON_ONCE(parent_counter
->ctx
->parent_ctx
);
4370 mutex_lock(&parent_counter
->child_mutex
);
4371 list_add_tail(&child_counter
->child_list
, &parent_counter
->child_list
);
4372 mutex_unlock(&parent_counter
->child_mutex
);
4374 return child_counter
;
4377 static int inherit_group(struct perf_counter
*parent_counter
,
4378 struct task_struct
*parent
,
4379 struct perf_counter_context
*parent_ctx
,
4380 struct task_struct
*child
,
4381 struct perf_counter_context
*child_ctx
)
4383 struct perf_counter
*leader
;
4384 struct perf_counter
*sub
;
4385 struct perf_counter
*child_ctr
;
4387 leader
= inherit_counter(parent_counter
, parent
, parent_ctx
,
4388 child
, NULL
, child_ctx
);
4390 return PTR_ERR(leader
);
4391 list_for_each_entry(sub
, &parent_counter
->sibling_list
, list_entry
) {
4392 child_ctr
= inherit_counter(sub
, parent
, parent_ctx
,
4393 child
, leader
, child_ctx
);
4394 if (IS_ERR(child_ctr
))
4395 return PTR_ERR(child_ctr
);
4400 static void sync_child_counter(struct perf_counter
*child_counter
,
4401 struct task_struct
*child
)
4403 struct perf_counter
*parent_counter
= child_counter
->parent
;
4406 if (child_counter
->attr
.inherit_stat
)
4407 perf_counter_read_event(child_counter
, child
);
4409 child_val
= atomic64_read(&child_counter
->count
);
4412 * Add back the child's count to the parent's count:
4414 atomic64_add(child_val
, &parent_counter
->count
);
4415 atomic64_add(child_counter
->total_time_enabled
,
4416 &parent_counter
->child_total_time_enabled
);
4417 atomic64_add(child_counter
->total_time_running
,
4418 &parent_counter
->child_total_time_running
);
4421 * Remove this counter from the parent's list
4423 WARN_ON_ONCE(parent_counter
->ctx
->parent_ctx
);
4424 mutex_lock(&parent_counter
->child_mutex
);
4425 list_del_init(&child_counter
->child_list
);
4426 mutex_unlock(&parent_counter
->child_mutex
);
4429 * Release the parent counter, if this was the last
4432 fput(parent_counter
->filp
);
4436 __perf_counter_exit_task(struct perf_counter
*child_counter
,
4437 struct perf_counter_context
*child_ctx
,
4438 struct task_struct
*child
)
4440 struct perf_counter
*parent_counter
;
4442 update_counter_times(child_counter
);
4443 perf_counter_remove_from_context(child_counter
);
4445 parent_counter
= child_counter
->parent
;
4447 * It can happen that parent exits first, and has counters
4448 * that are still around due to the child reference. These
4449 * counters need to be zapped - but otherwise linger.
4451 if (parent_counter
) {
4452 sync_child_counter(child_counter
, child
);
4453 free_counter(child_counter
);
4458 * When a child task exits, feed back counter values to parent counters.
4460 void perf_counter_exit_task(struct task_struct
*child
)
4462 struct perf_counter
*child_counter
, *tmp
;
4463 struct perf_counter_context
*child_ctx
;
4464 unsigned long flags
;
4466 if (likely(!child
->perf_counter_ctxp
)) {
4467 perf_counter_task(child
, NULL
, 0);
4471 local_irq_save(flags
);
4473 * We can't reschedule here because interrupts are disabled,
4474 * and either child is current or it is a task that can't be
4475 * scheduled, so we are now safe from rescheduling changing
4478 child_ctx
= child
->perf_counter_ctxp
;
4479 __perf_counter_task_sched_out(child_ctx
);
4482 * Take the context lock here so that if find_get_context is
4483 * reading child->perf_counter_ctxp, we wait until it has
4484 * incremented the context's refcount before we do put_ctx below.
4486 spin_lock(&child_ctx
->lock
);
4487 child
->perf_counter_ctxp
= NULL
;
4489 * If this context is a clone; unclone it so it can't get
4490 * swapped to another process while we're removing all
4491 * the counters from it.
4493 unclone_ctx(child_ctx
);
4494 spin_unlock_irqrestore(&child_ctx
->lock
, flags
);
4497 * Report the task dead after unscheduling the counters so that we
4498 * won't get any samples after PERF_EVENT_EXIT. We can however still
4499 * get a few PERF_EVENT_READ events.
4501 perf_counter_task(child
, child_ctx
, 0);
4504 * We can recurse on the same lock type through:
4506 * __perf_counter_exit_task()
4507 * sync_child_counter()
4508 * fput(parent_counter->filp)
4510 * mutex_lock(&ctx->mutex)
4512 * But since its the parent context it won't be the same instance.
4514 mutex_lock_nested(&child_ctx
->mutex
, SINGLE_DEPTH_NESTING
);
4517 list_for_each_entry_safe(child_counter
, tmp
, &child_ctx
->counter_list
,
4519 __perf_counter_exit_task(child_counter
, child_ctx
, child
);
4522 * If the last counter was a group counter, it will have appended all
4523 * its siblings to the list, but we obtained 'tmp' before that which
4524 * will still point to the list head terminating the iteration.
4526 if (!list_empty(&child_ctx
->counter_list
))
4529 mutex_unlock(&child_ctx
->mutex
);
4535 * free an unexposed, unused context as created by inheritance by
4536 * init_task below, used by fork() in case of fail.
4538 void perf_counter_free_task(struct task_struct
*task
)
4540 struct perf_counter_context
*ctx
= task
->perf_counter_ctxp
;
4541 struct perf_counter
*counter
, *tmp
;
4546 mutex_lock(&ctx
->mutex
);
4548 list_for_each_entry_safe(counter
, tmp
, &ctx
->counter_list
, list_entry
) {
4549 struct perf_counter
*parent
= counter
->parent
;
4551 if (WARN_ON_ONCE(!parent
))
4554 mutex_lock(&parent
->child_mutex
);
4555 list_del_init(&counter
->child_list
);
4556 mutex_unlock(&parent
->child_mutex
);
4560 list_del_counter(counter
, ctx
);
4561 free_counter(counter
);
4564 if (!list_empty(&ctx
->counter_list
))
4567 mutex_unlock(&ctx
->mutex
);
4573 * Initialize the perf_counter context in task_struct
4575 int perf_counter_init_task(struct task_struct
*child
)
4577 struct perf_counter_context
*child_ctx
, *parent_ctx
;
4578 struct perf_counter_context
*cloned_ctx
;
4579 struct perf_counter
*counter
;
4580 struct task_struct
*parent
= current
;
4581 int inherited_all
= 1;
4584 child
->perf_counter_ctxp
= NULL
;
4586 mutex_init(&child
->perf_counter_mutex
);
4587 INIT_LIST_HEAD(&child
->perf_counter_list
);
4589 if (likely(!parent
->perf_counter_ctxp
))
4593 * This is executed from the parent task context, so inherit
4594 * counters that have been marked for cloning.
4595 * First allocate and initialize a context for the child.
4598 child_ctx
= kmalloc(sizeof(struct perf_counter_context
), GFP_KERNEL
);
4602 __perf_counter_init_context(child_ctx
, child
);
4603 child
->perf_counter_ctxp
= child_ctx
;
4604 get_task_struct(child
);
4607 * If the parent's context is a clone, pin it so it won't get
4610 parent_ctx
= perf_pin_task_context(parent
);
4613 * No need to check if parent_ctx != NULL here; since we saw
4614 * it non-NULL earlier, the only reason for it to become NULL
4615 * is if we exit, and since we're currently in the middle of
4616 * a fork we can't be exiting at the same time.
4620 * Lock the parent list. No need to lock the child - not PID
4621 * hashed yet and not running, so nobody can access it.
4623 mutex_lock(&parent_ctx
->mutex
);
4626 * We dont have to disable NMIs - we are only looking at
4627 * the list, not manipulating it:
4629 list_for_each_entry_rcu(counter
, &parent_ctx
->event_list
, event_entry
) {
4630 if (counter
!= counter
->group_leader
)
4633 if (!counter
->attr
.inherit
) {
4638 ret
= inherit_group(counter
, parent
, parent_ctx
,
4646 if (inherited_all
) {
4648 * Mark the child context as a clone of the parent
4649 * context, or of whatever the parent is a clone of.
4650 * Note that if the parent is a clone, it could get
4651 * uncloned at any point, but that doesn't matter
4652 * because the list of counters and the generation
4653 * count can't have changed since we took the mutex.
4655 cloned_ctx
= rcu_dereference(parent_ctx
->parent_ctx
);
4657 child_ctx
->parent_ctx
= cloned_ctx
;
4658 child_ctx
->parent_gen
= parent_ctx
->parent_gen
;
4660 child_ctx
->parent_ctx
= parent_ctx
;
4661 child_ctx
->parent_gen
= parent_ctx
->generation
;
4663 get_ctx(child_ctx
->parent_ctx
);
4666 mutex_unlock(&parent_ctx
->mutex
);
4668 perf_unpin_context(parent_ctx
);
4673 static void __cpuinit
perf_counter_init_cpu(int cpu
)
4675 struct perf_cpu_context
*cpuctx
;
4677 cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
4678 __perf_counter_init_context(&cpuctx
->ctx
, NULL
);
4680 spin_lock(&perf_resource_lock
);
4681 cpuctx
->max_pertask
= perf_max_counters
- perf_reserved_percpu
;
4682 spin_unlock(&perf_resource_lock
);
4684 hw_perf_counter_setup(cpu
);
4687 #ifdef CONFIG_HOTPLUG_CPU
4688 static void __perf_counter_exit_cpu(void *info
)
4690 struct perf_cpu_context
*cpuctx
= &__get_cpu_var(perf_cpu_context
);
4691 struct perf_counter_context
*ctx
= &cpuctx
->ctx
;
4692 struct perf_counter
*counter
, *tmp
;
4694 list_for_each_entry_safe(counter
, tmp
, &ctx
->counter_list
, list_entry
)
4695 __perf_counter_remove_from_context(counter
);
4697 static void perf_counter_exit_cpu(int cpu
)
4699 struct perf_cpu_context
*cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
4700 struct perf_counter_context
*ctx
= &cpuctx
->ctx
;
4702 mutex_lock(&ctx
->mutex
);
4703 smp_call_function_single(cpu
, __perf_counter_exit_cpu
, NULL
, 1);
4704 mutex_unlock(&ctx
->mutex
);
4707 static inline void perf_counter_exit_cpu(int cpu
) { }
4710 static int __cpuinit
4711 perf_cpu_notify(struct notifier_block
*self
, unsigned long action
, void *hcpu
)
4713 unsigned int cpu
= (long)hcpu
;
4717 case CPU_UP_PREPARE
:
4718 case CPU_UP_PREPARE_FROZEN
:
4719 perf_counter_init_cpu(cpu
);
4723 case CPU_ONLINE_FROZEN
:
4724 hw_perf_counter_setup_online(cpu
);
4727 case CPU_DOWN_PREPARE
:
4728 case CPU_DOWN_PREPARE_FROZEN
:
4729 perf_counter_exit_cpu(cpu
);
4740 * This has to have a higher priority than migration_notifier in sched.c.
4742 static struct notifier_block __cpuinitdata perf_cpu_nb
= {
4743 .notifier_call
= perf_cpu_notify
,
4747 void __init
perf_counter_init(void)
4749 perf_cpu_notify(&perf_cpu_nb
, (unsigned long)CPU_UP_PREPARE
,
4750 (void *)(long)smp_processor_id());
4751 perf_cpu_notify(&perf_cpu_nb
, (unsigned long)CPU_ONLINE
,
4752 (void *)(long)smp_processor_id());
4753 register_cpu_notifier(&perf_cpu_nb
);
4756 static ssize_t
perf_show_reserve_percpu(struct sysdev_class
*class, char *buf
)
4758 return sprintf(buf
, "%d\n", perf_reserved_percpu
);
4762 perf_set_reserve_percpu(struct sysdev_class
*class,
4766 struct perf_cpu_context
*cpuctx
;
4770 err
= strict_strtoul(buf
, 10, &val
);
4773 if (val
> perf_max_counters
)
4776 spin_lock(&perf_resource_lock
);
4777 perf_reserved_percpu
= val
;
4778 for_each_online_cpu(cpu
) {
4779 cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
4780 spin_lock_irq(&cpuctx
->ctx
.lock
);
4781 mpt
= min(perf_max_counters
- cpuctx
->ctx
.nr_counters
,
4782 perf_max_counters
- perf_reserved_percpu
);
4783 cpuctx
->max_pertask
= mpt
;
4784 spin_unlock_irq(&cpuctx
->ctx
.lock
);
4786 spin_unlock(&perf_resource_lock
);
4791 static ssize_t
perf_show_overcommit(struct sysdev_class
*class, char *buf
)
4793 return sprintf(buf
, "%d\n", perf_overcommit
);
4797 perf_set_overcommit(struct sysdev_class
*class, const char *buf
, size_t count
)
4802 err
= strict_strtoul(buf
, 10, &val
);
4808 spin_lock(&perf_resource_lock
);
4809 perf_overcommit
= val
;
4810 spin_unlock(&perf_resource_lock
);
4815 static SYSDEV_CLASS_ATTR(
4818 perf_show_reserve_percpu
,
4819 perf_set_reserve_percpu
4822 static SYSDEV_CLASS_ATTR(
4825 perf_show_overcommit
,
4829 static struct attribute
*perfclass_attrs
[] = {
4830 &attr_reserve_percpu
.attr
,
4831 &attr_overcommit
.attr
,
4835 static struct attribute_group perfclass_attr_group
= {
4836 .attrs
= perfclass_attrs
,
4837 .name
= "perf_counters",
4840 static int __init
perf_counter_sysfs_init(void)
4842 return sysfs_create_group(&cpu_sysdev_class
.kset
.kobj
,
4843 &perfclass_attr_group
);
4845 device_initcall(perf_counter_sysfs_init
);