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(); }
93 hw_perf_group_sched_in(struct perf_counter
*group_leader
,
94 struct perf_cpu_context
*cpuctx
,
95 struct perf_counter_context
*ctx
, int cpu
)
100 void __weak
perf_counter_print_debug(void) { }
102 static DEFINE_PER_CPU(int, disable_count
);
104 void __perf_disable(void)
106 __get_cpu_var(disable_count
)++;
109 bool __perf_enable(void)
111 return !--__get_cpu_var(disable_count
);
114 void perf_disable(void)
120 void perf_enable(void)
126 static void get_ctx(struct perf_counter_context
*ctx
)
128 WARN_ON(!atomic_inc_not_zero(&ctx
->refcount
));
131 static void free_ctx(struct rcu_head
*head
)
133 struct perf_counter_context
*ctx
;
135 ctx
= container_of(head
, struct perf_counter_context
, rcu_head
);
139 static void put_ctx(struct perf_counter_context
*ctx
)
141 if (atomic_dec_and_test(&ctx
->refcount
)) {
143 put_ctx(ctx
->parent_ctx
);
145 put_task_struct(ctx
->task
);
146 call_rcu(&ctx
->rcu_head
, free_ctx
);
150 static void unclone_ctx(struct perf_counter_context
*ctx
)
152 if (ctx
->parent_ctx
) {
153 put_ctx(ctx
->parent_ctx
);
154 ctx
->parent_ctx
= NULL
;
159 * If we inherit counters we want to return the parent counter id
162 static u64
primary_counter_id(struct perf_counter
*counter
)
164 u64 id
= counter
->id
;
167 id
= counter
->parent
->id
;
173 * Get the perf_counter_context for a task and lock it.
174 * This has to cope with with the fact that until it is locked,
175 * the context could get moved to another task.
177 static struct perf_counter_context
*
178 perf_lock_task_context(struct task_struct
*task
, unsigned long *flags
)
180 struct perf_counter_context
*ctx
;
184 ctx
= rcu_dereference(task
->perf_counter_ctxp
);
187 * If this context is a clone of another, it might
188 * get swapped for another underneath us by
189 * perf_counter_task_sched_out, though the
190 * rcu_read_lock() protects us from any context
191 * getting freed. Lock the context and check if it
192 * got swapped before we could get the lock, and retry
193 * if so. If we locked the right context, then it
194 * can't get swapped on us any more.
196 spin_lock_irqsave(&ctx
->lock
, *flags
);
197 if (ctx
!= rcu_dereference(task
->perf_counter_ctxp
)) {
198 spin_unlock_irqrestore(&ctx
->lock
, *flags
);
202 if (!atomic_inc_not_zero(&ctx
->refcount
)) {
203 spin_unlock_irqrestore(&ctx
->lock
, *flags
);
212 * Get the context for a task and increment its pin_count so it
213 * can't get swapped to another task. This also increments its
214 * reference count so that the context can't get freed.
216 static struct perf_counter_context
*perf_pin_task_context(struct task_struct
*task
)
218 struct perf_counter_context
*ctx
;
221 ctx
= perf_lock_task_context(task
, &flags
);
224 spin_unlock_irqrestore(&ctx
->lock
, flags
);
229 static void perf_unpin_context(struct perf_counter_context
*ctx
)
233 spin_lock_irqsave(&ctx
->lock
, flags
);
235 spin_unlock_irqrestore(&ctx
->lock
, flags
);
240 * Add a counter from the lists for its context.
241 * Must be called with ctx->mutex and ctx->lock held.
244 list_add_counter(struct perf_counter
*counter
, struct perf_counter_context
*ctx
)
246 struct perf_counter
*group_leader
= counter
->group_leader
;
249 * Depending on whether it is a standalone or sibling counter,
250 * add it straight to the context's counter list, or to the group
251 * leader's sibling list:
253 if (group_leader
== counter
)
254 list_add_tail(&counter
->list_entry
, &ctx
->counter_list
);
256 list_add_tail(&counter
->list_entry
, &group_leader
->sibling_list
);
257 group_leader
->nr_siblings
++;
260 list_add_rcu(&counter
->event_entry
, &ctx
->event_list
);
262 if (counter
->attr
.inherit_stat
)
267 * Remove a counter from the lists for its context.
268 * Must be called with ctx->mutex and ctx->lock held.
271 list_del_counter(struct perf_counter
*counter
, struct perf_counter_context
*ctx
)
273 struct perf_counter
*sibling
, *tmp
;
275 if (list_empty(&counter
->list_entry
))
278 if (counter
->attr
.inherit_stat
)
281 list_del_init(&counter
->list_entry
);
282 list_del_rcu(&counter
->event_entry
);
284 if (counter
->group_leader
!= counter
)
285 counter
->group_leader
->nr_siblings
--;
288 * If this was a group counter with sibling counters then
289 * upgrade the siblings to singleton counters by adding them
290 * to the context list directly:
292 list_for_each_entry_safe(sibling
, tmp
,
293 &counter
->sibling_list
, list_entry
) {
295 list_move_tail(&sibling
->list_entry
, &ctx
->counter_list
);
296 sibling
->group_leader
= sibling
;
301 counter_sched_out(struct perf_counter
*counter
,
302 struct perf_cpu_context
*cpuctx
,
303 struct perf_counter_context
*ctx
)
305 if (counter
->state
!= PERF_COUNTER_STATE_ACTIVE
)
308 counter
->state
= PERF_COUNTER_STATE_INACTIVE
;
309 counter
->tstamp_stopped
= ctx
->time
;
310 counter
->pmu
->disable(counter
);
313 if (!is_software_counter(counter
))
314 cpuctx
->active_oncpu
--;
316 if (counter
->attr
.exclusive
|| !cpuctx
->active_oncpu
)
317 cpuctx
->exclusive
= 0;
321 group_sched_out(struct perf_counter
*group_counter
,
322 struct perf_cpu_context
*cpuctx
,
323 struct perf_counter_context
*ctx
)
325 struct perf_counter
*counter
;
327 if (group_counter
->state
!= PERF_COUNTER_STATE_ACTIVE
)
330 counter_sched_out(group_counter
, cpuctx
, ctx
);
333 * Schedule out siblings (if any):
335 list_for_each_entry(counter
, &group_counter
->sibling_list
, list_entry
)
336 counter_sched_out(counter
, cpuctx
, ctx
);
338 if (group_counter
->attr
.exclusive
)
339 cpuctx
->exclusive
= 0;
343 * Cross CPU call to remove a performance counter
345 * We disable the counter on the hardware level first. After that we
346 * remove it from the context list.
348 static void __perf_counter_remove_from_context(void *info
)
350 struct perf_cpu_context
*cpuctx
= &__get_cpu_var(perf_cpu_context
);
351 struct perf_counter
*counter
= info
;
352 struct perf_counter_context
*ctx
= counter
->ctx
;
355 * If this is a task context, we need to check whether it is
356 * the current task context of this cpu. If not it has been
357 * scheduled out before the smp call arrived.
359 if (ctx
->task
&& cpuctx
->task_ctx
!= ctx
)
362 spin_lock(&ctx
->lock
);
364 * Protect the list operation against NMI by disabling the
365 * counters on a global level.
369 counter_sched_out(counter
, cpuctx
, ctx
);
371 list_del_counter(counter
, ctx
);
375 * Allow more per task counters with respect to the
378 cpuctx
->max_pertask
=
379 min(perf_max_counters
- ctx
->nr_counters
,
380 perf_max_counters
- perf_reserved_percpu
);
384 spin_unlock(&ctx
->lock
);
389 * Remove the counter from a task's (or a CPU's) list of counters.
391 * Must be called with ctx->mutex held.
393 * CPU counters are removed with a smp call. For task counters we only
394 * call when the task is on a CPU.
396 * If counter->ctx is a cloned context, callers must make sure that
397 * every task struct that counter->ctx->task could possibly point to
398 * remains valid. This is OK when called from perf_release since
399 * that only calls us on the top-level context, which can't be a clone.
400 * When called from perf_counter_exit_task, it's OK because the
401 * context has been detached from its task.
403 static void perf_counter_remove_from_context(struct perf_counter
*counter
)
405 struct perf_counter_context
*ctx
= counter
->ctx
;
406 struct task_struct
*task
= ctx
->task
;
410 * Per cpu counters are removed via an smp call and
411 * the removal is always sucessful.
413 smp_call_function_single(counter
->cpu
,
414 __perf_counter_remove_from_context
,
420 task_oncpu_function_call(task
, __perf_counter_remove_from_context
,
423 spin_lock_irq(&ctx
->lock
);
425 * If the context is active we need to retry the smp call.
427 if (ctx
->nr_active
&& !list_empty(&counter
->list_entry
)) {
428 spin_unlock_irq(&ctx
->lock
);
433 * The lock prevents that this context is scheduled in so we
434 * can remove the counter safely, if the call above did not
437 if (!list_empty(&counter
->list_entry
)) {
438 list_del_counter(counter
, ctx
);
440 spin_unlock_irq(&ctx
->lock
);
443 static inline u64
perf_clock(void)
445 return cpu_clock(smp_processor_id());
449 * Update the record of the current time in a context.
451 static void update_context_time(struct perf_counter_context
*ctx
)
453 u64 now
= perf_clock();
455 ctx
->time
+= now
- ctx
->timestamp
;
456 ctx
->timestamp
= now
;
460 * Update the total_time_enabled and total_time_running fields for a counter.
462 static void update_counter_times(struct perf_counter
*counter
)
464 struct perf_counter_context
*ctx
= counter
->ctx
;
467 if (counter
->state
< PERF_COUNTER_STATE_INACTIVE
)
470 counter
->total_time_enabled
= ctx
->time
- counter
->tstamp_enabled
;
472 if (counter
->state
== PERF_COUNTER_STATE_INACTIVE
)
473 run_end
= counter
->tstamp_stopped
;
477 counter
->total_time_running
= run_end
- counter
->tstamp_running
;
481 * Update total_time_enabled and total_time_running for all counters in a group.
483 static void update_group_times(struct perf_counter
*leader
)
485 struct perf_counter
*counter
;
487 update_counter_times(leader
);
488 list_for_each_entry(counter
, &leader
->sibling_list
, list_entry
)
489 update_counter_times(counter
);
493 * Cross CPU call to disable a performance counter
495 static void __perf_counter_disable(void *info
)
497 struct perf_counter
*counter
= info
;
498 struct perf_cpu_context
*cpuctx
= &__get_cpu_var(perf_cpu_context
);
499 struct perf_counter_context
*ctx
= counter
->ctx
;
502 * If this is a per-task counter, need to check whether this
503 * counter's task is the current task on this cpu.
505 if (ctx
->task
&& cpuctx
->task_ctx
!= ctx
)
508 spin_lock(&ctx
->lock
);
511 * If the counter is on, turn it off.
512 * If it is in error state, leave it in error state.
514 if (counter
->state
>= PERF_COUNTER_STATE_INACTIVE
) {
515 update_context_time(ctx
);
516 update_counter_times(counter
);
517 if (counter
== counter
->group_leader
)
518 group_sched_out(counter
, cpuctx
, ctx
);
520 counter_sched_out(counter
, cpuctx
, ctx
);
521 counter
->state
= PERF_COUNTER_STATE_OFF
;
524 spin_unlock(&ctx
->lock
);
530 * If counter->ctx is a cloned context, callers must make sure that
531 * every task struct that counter->ctx->task could possibly point to
532 * remains valid. This condition is satisifed when called through
533 * perf_counter_for_each_child or perf_counter_for_each because they
534 * hold the top-level counter's child_mutex, so any descendant that
535 * goes to exit will block in sync_child_counter.
536 * When called from perf_pending_counter it's OK because counter->ctx
537 * is the current context on this CPU and preemption is disabled,
538 * hence we can't get into perf_counter_task_sched_out for this context.
540 static void perf_counter_disable(struct perf_counter
*counter
)
542 struct perf_counter_context
*ctx
= counter
->ctx
;
543 struct task_struct
*task
= ctx
->task
;
547 * Disable the counter on the cpu that it's on
549 smp_call_function_single(counter
->cpu
, __perf_counter_disable
,
555 task_oncpu_function_call(task
, __perf_counter_disable
, counter
);
557 spin_lock_irq(&ctx
->lock
);
559 * If the counter is still active, we need to retry the cross-call.
561 if (counter
->state
== PERF_COUNTER_STATE_ACTIVE
) {
562 spin_unlock_irq(&ctx
->lock
);
567 * Since we have the lock this context can't be scheduled
568 * in, so we can change the state safely.
570 if (counter
->state
== PERF_COUNTER_STATE_INACTIVE
) {
571 update_counter_times(counter
);
572 counter
->state
= PERF_COUNTER_STATE_OFF
;
575 spin_unlock_irq(&ctx
->lock
);
579 counter_sched_in(struct perf_counter
*counter
,
580 struct perf_cpu_context
*cpuctx
,
581 struct perf_counter_context
*ctx
,
584 if (counter
->state
<= PERF_COUNTER_STATE_OFF
)
587 counter
->state
= PERF_COUNTER_STATE_ACTIVE
;
588 counter
->oncpu
= cpu
; /* TODO: put 'cpu' into cpuctx->cpu */
590 * The new state must be visible before we turn it on in the hardware:
594 if (counter
->pmu
->enable(counter
)) {
595 counter
->state
= PERF_COUNTER_STATE_INACTIVE
;
600 counter
->tstamp_running
+= ctx
->time
- counter
->tstamp_stopped
;
602 if (!is_software_counter(counter
))
603 cpuctx
->active_oncpu
++;
606 if (counter
->attr
.exclusive
)
607 cpuctx
->exclusive
= 1;
613 group_sched_in(struct perf_counter
*group_counter
,
614 struct perf_cpu_context
*cpuctx
,
615 struct perf_counter_context
*ctx
,
618 struct perf_counter
*counter
, *partial_group
;
621 if (group_counter
->state
== PERF_COUNTER_STATE_OFF
)
624 ret
= hw_perf_group_sched_in(group_counter
, cpuctx
, ctx
, cpu
);
626 return ret
< 0 ? ret
: 0;
628 if (counter_sched_in(group_counter
, cpuctx
, ctx
, cpu
))
632 * Schedule in siblings as one group (if any):
634 list_for_each_entry(counter
, &group_counter
->sibling_list
, list_entry
) {
635 if (counter_sched_in(counter
, cpuctx
, ctx
, cpu
)) {
636 partial_group
= counter
;
645 * Groups can be scheduled in as one unit only, so undo any
646 * partial group before returning:
648 list_for_each_entry(counter
, &group_counter
->sibling_list
, list_entry
) {
649 if (counter
== partial_group
)
651 counter_sched_out(counter
, cpuctx
, ctx
);
653 counter_sched_out(group_counter
, cpuctx
, ctx
);
659 * Return 1 for a group consisting entirely of software counters,
660 * 0 if the group contains any hardware counters.
662 static int is_software_only_group(struct perf_counter
*leader
)
664 struct perf_counter
*counter
;
666 if (!is_software_counter(leader
))
669 list_for_each_entry(counter
, &leader
->sibling_list
, list_entry
)
670 if (!is_software_counter(counter
))
677 * Work out whether we can put this counter group on the CPU now.
679 static int group_can_go_on(struct perf_counter
*counter
,
680 struct perf_cpu_context
*cpuctx
,
684 * Groups consisting entirely of software counters can always go on.
686 if (is_software_only_group(counter
))
689 * If an exclusive group is already on, no other hardware
690 * counters can go on.
692 if (cpuctx
->exclusive
)
695 * If this group is exclusive and there are already
696 * counters on the CPU, it can't go on.
698 if (counter
->attr
.exclusive
&& cpuctx
->active_oncpu
)
701 * Otherwise, try to add it if all previous groups were able
707 static void add_counter_to_ctx(struct perf_counter
*counter
,
708 struct perf_counter_context
*ctx
)
710 list_add_counter(counter
, ctx
);
711 counter
->tstamp_enabled
= ctx
->time
;
712 counter
->tstamp_running
= ctx
->time
;
713 counter
->tstamp_stopped
= ctx
->time
;
717 * Cross CPU call to install and enable a performance counter
719 * Must be called with ctx->mutex held
721 static void __perf_install_in_context(void *info
)
723 struct perf_cpu_context
*cpuctx
= &__get_cpu_var(perf_cpu_context
);
724 struct perf_counter
*counter
= info
;
725 struct perf_counter_context
*ctx
= counter
->ctx
;
726 struct perf_counter
*leader
= counter
->group_leader
;
727 int cpu
= smp_processor_id();
731 * If this is a task context, we need to check whether it is
732 * the current task context of this cpu. If not it has been
733 * scheduled out before the smp call arrived.
734 * Or possibly this is the right context but it isn't
735 * on this cpu because it had no counters.
737 if (ctx
->task
&& cpuctx
->task_ctx
!= ctx
) {
738 if (cpuctx
->task_ctx
|| ctx
->task
!= current
)
740 cpuctx
->task_ctx
= ctx
;
743 spin_lock(&ctx
->lock
);
745 update_context_time(ctx
);
748 * Protect the list operation against NMI by disabling the
749 * counters on a global level. NOP for non NMI based counters.
753 add_counter_to_ctx(counter
, ctx
);
756 * Don't put the counter on if it is disabled or if
757 * it is in a group and the group isn't on.
759 if (counter
->state
!= PERF_COUNTER_STATE_INACTIVE
||
760 (leader
!= counter
&& leader
->state
!= PERF_COUNTER_STATE_ACTIVE
))
764 * An exclusive counter can't go on if there are already active
765 * hardware counters, and no hardware counter can go on if there
766 * is already an exclusive counter on.
768 if (!group_can_go_on(counter
, cpuctx
, 1))
771 err
= counter_sched_in(counter
, cpuctx
, ctx
, cpu
);
775 * This counter couldn't go on. If it is in a group
776 * then we have to pull the whole group off.
777 * If the counter group is pinned then put it in error state.
779 if (leader
!= counter
)
780 group_sched_out(leader
, cpuctx
, ctx
);
781 if (leader
->attr
.pinned
) {
782 update_group_times(leader
);
783 leader
->state
= PERF_COUNTER_STATE_ERROR
;
787 if (!err
&& !ctx
->task
&& cpuctx
->max_pertask
)
788 cpuctx
->max_pertask
--;
793 spin_unlock(&ctx
->lock
);
797 * Attach a performance counter to a context
799 * First we add the counter to the list with the hardware enable bit
800 * in counter->hw_config cleared.
802 * If the counter is attached to a task which is on a CPU we use a smp
803 * call to enable it in the task context. The task might have been
804 * scheduled away, but we check this in the smp call again.
806 * Must be called with ctx->mutex held.
809 perf_install_in_context(struct perf_counter_context
*ctx
,
810 struct perf_counter
*counter
,
813 struct task_struct
*task
= ctx
->task
;
817 * Per cpu counters are installed via an smp call and
818 * the install is always sucessful.
820 smp_call_function_single(cpu
, __perf_install_in_context
,
826 task_oncpu_function_call(task
, __perf_install_in_context
,
829 spin_lock_irq(&ctx
->lock
);
831 * we need to retry the smp call.
833 if (ctx
->is_active
&& list_empty(&counter
->list_entry
)) {
834 spin_unlock_irq(&ctx
->lock
);
839 * The lock prevents that this context is scheduled in so we
840 * can add the counter safely, if it the call above did not
843 if (list_empty(&counter
->list_entry
))
844 add_counter_to_ctx(counter
, ctx
);
845 spin_unlock_irq(&ctx
->lock
);
849 * Cross CPU call to enable a performance counter
851 static void __perf_counter_enable(void *info
)
853 struct perf_counter
*counter
= info
;
854 struct perf_cpu_context
*cpuctx
= &__get_cpu_var(perf_cpu_context
);
855 struct perf_counter_context
*ctx
= counter
->ctx
;
856 struct perf_counter
*leader
= counter
->group_leader
;
860 * If this is a per-task counter, need to check whether this
861 * counter's task is the current task on this cpu.
863 if (ctx
->task
&& cpuctx
->task_ctx
!= ctx
) {
864 if (cpuctx
->task_ctx
|| ctx
->task
!= current
)
866 cpuctx
->task_ctx
= ctx
;
869 spin_lock(&ctx
->lock
);
871 update_context_time(ctx
);
873 if (counter
->state
>= PERF_COUNTER_STATE_INACTIVE
)
875 counter
->state
= PERF_COUNTER_STATE_INACTIVE
;
876 counter
->tstamp_enabled
= ctx
->time
- counter
->total_time_enabled
;
879 * If the counter is in a group and isn't the group leader,
880 * then don't put it on unless the group is on.
882 if (leader
!= counter
&& leader
->state
!= PERF_COUNTER_STATE_ACTIVE
)
885 if (!group_can_go_on(counter
, cpuctx
, 1)) {
889 if (counter
== leader
)
890 err
= group_sched_in(counter
, cpuctx
, ctx
,
893 err
= counter_sched_in(counter
, cpuctx
, ctx
,
900 * If this counter can't go on and it's part of a
901 * group, then the whole group has to come off.
903 if (leader
!= counter
)
904 group_sched_out(leader
, cpuctx
, ctx
);
905 if (leader
->attr
.pinned
) {
906 update_group_times(leader
);
907 leader
->state
= PERF_COUNTER_STATE_ERROR
;
912 spin_unlock(&ctx
->lock
);
918 * If counter->ctx is a cloned context, callers must make sure that
919 * every task struct that counter->ctx->task could possibly point to
920 * remains valid. This condition is satisfied when called through
921 * perf_counter_for_each_child or perf_counter_for_each as described
922 * for perf_counter_disable.
924 static void perf_counter_enable(struct perf_counter
*counter
)
926 struct perf_counter_context
*ctx
= counter
->ctx
;
927 struct task_struct
*task
= ctx
->task
;
931 * Enable the counter on the cpu that it's on
933 smp_call_function_single(counter
->cpu
, __perf_counter_enable
,
938 spin_lock_irq(&ctx
->lock
);
939 if (counter
->state
>= PERF_COUNTER_STATE_INACTIVE
)
943 * If the counter is in error state, clear that first.
944 * That way, if we see the counter in error state below, we
945 * know that it has gone back into error state, as distinct
946 * from the task having been scheduled away before the
947 * cross-call arrived.
949 if (counter
->state
== PERF_COUNTER_STATE_ERROR
)
950 counter
->state
= PERF_COUNTER_STATE_OFF
;
953 spin_unlock_irq(&ctx
->lock
);
954 task_oncpu_function_call(task
, __perf_counter_enable
, counter
);
956 spin_lock_irq(&ctx
->lock
);
959 * If the context is active and the counter is still off,
960 * we need to retry the cross-call.
962 if (ctx
->is_active
&& counter
->state
== PERF_COUNTER_STATE_OFF
)
966 * Since we have the lock this context can't be scheduled
967 * in, so we can change the state safely.
969 if (counter
->state
== PERF_COUNTER_STATE_OFF
) {
970 counter
->state
= PERF_COUNTER_STATE_INACTIVE
;
971 counter
->tstamp_enabled
=
972 ctx
->time
- counter
->total_time_enabled
;
975 spin_unlock_irq(&ctx
->lock
);
978 static int perf_counter_refresh(struct perf_counter
*counter
, int refresh
)
981 * not supported on inherited counters
983 if (counter
->attr
.inherit
)
986 atomic_add(refresh
, &counter
->event_limit
);
987 perf_counter_enable(counter
);
992 void __perf_counter_sched_out(struct perf_counter_context
*ctx
,
993 struct perf_cpu_context
*cpuctx
)
995 struct perf_counter
*counter
;
997 spin_lock(&ctx
->lock
);
999 if (likely(!ctx
->nr_counters
))
1001 update_context_time(ctx
);
1004 if (ctx
->nr_active
) {
1005 list_for_each_entry(counter
, &ctx
->counter_list
, list_entry
) {
1006 if (counter
!= counter
->group_leader
)
1007 counter_sched_out(counter
, cpuctx
, ctx
);
1009 group_sched_out(counter
, cpuctx
, ctx
);
1014 spin_unlock(&ctx
->lock
);
1018 * Test whether two contexts are equivalent, i.e. whether they
1019 * have both been cloned from the same version of the same context
1020 * and they both have the same number of enabled counters.
1021 * If the number of enabled counters is the same, then the set
1022 * of enabled counters should be the same, because these are both
1023 * inherited contexts, therefore we can't access individual counters
1024 * in them directly with an fd; we can only enable/disable all
1025 * counters via prctl, or enable/disable all counters in a family
1026 * via ioctl, which will have the same effect on both contexts.
1028 static int context_equiv(struct perf_counter_context
*ctx1
,
1029 struct perf_counter_context
*ctx2
)
1031 return ctx1
->parent_ctx
&& ctx1
->parent_ctx
== ctx2
->parent_ctx
1032 && ctx1
->parent_gen
== ctx2
->parent_gen
1033 && !ctx1
->pin_count
&& !ctx2
->pin_count
;
1036 static void __perf_counter_read(void *counter
);
1038 static void __perf_counter_sync_stat(struct perf_counter
*counter
,
1039 struct perf_counter
*next_counter
)
1043 if (!counter
->attr
.inherit_stat
)
1047 * Update the counter value, we cannot use perf_counter_read()
1048 * because we're in the middle of a context switch and have IRQs
1049 * disabled, which upsets smp_call_function_single(), however
1050 * we know the counter must be on the current CPU, therefore we
1051 * don't need to use it.
1053 switch (counter
->state
) {
1054 case PERF_COUNTER_STATE_ACTIVE
:
1055 __perf_counter_read(counter
);
1058 case PERF_COUNTER_STATE_INACTIVE
:
1059 update_counter_times(counter
);
1067 * In order to keep per-task stats reliable we need to flip the counter
1068 * values when we flip the contexts.
1070 value
= atomic64_read(&next_counter
->count
);
1071 value
= atomic64_xchg(&counter
->count
, value
);
1072 atomic64_set(&next_counter
->count
, value
);
1074 swap(counter
->total_time_enabled
, next_counter
->total_time_enabled
);
1075 swap(counter
->total_time_running
, next_counter
->total_time_running
);
1078 * Since we swizzled the values, update the user visible data too.
1080 perf_counter_update_userpage(counter
);
1081 perf_counter_update_userpage(next_counter
);
1084 #define list_next_entry(pos, member) \
1085 list_entry(pos->member.next, typeof(*pos), member)
1087 static void perf_counter_sync_stat(struct perf_counter_context
*ctx
,
1088 struct perf_counter_context
*next_ctx
)
1090 struct perf_counter
*counter
, *next_counter
;
1095 counter
= list_first_entry(&ctx
->event_list
,
1096 struct perf_counter
, event_entry
);
1098 next_counter
= list_first_entry(&next_ctx
->event_list
,
1099 struct perf_counter
, event_entry
);
1101 while (&counter
->event_entry
!= &ctx
->event_list
&&
1102 &next_counter
->event_entry
!= &next_ctx
->event_list
) {
1104 __perf_counter_sync_stat(counter
, next_counter
);
1106 counter
= list_next_entry(counter
, event_entry
);
1107 next_counter
= list_next_entry(next_counter
, event_entry
);
1112 * Called from scheduler to remove the counters of the current task,
1113 * with interrupts disabled.
1115 * We stop each counter and update the counter value in counter->count.
1117 * This does not protect us against NMI, but disable()
1118 * sets the disabled bit in the control field of counter _before_
1119 * accessing the counter control register. If a NMI hits, then it will
1120 * not restart the counter.
1122 void perf_counter_task_sched_out(struct task_struct
*task
,
1123 struct task_struct
*next
, int cpu
)
1125 struct perf_cpu_context
*cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
1126 struct perf_counter_context
*ctx
= task
->perf_counter_ctxp
;
1127 struct perf_counter_context
*next_ctx
;
1128 struct perf_counter_context
*parent
;
1129 struct pt_regs
*regs
;
1132 regs
= task_pt_regs(task
);
1133 perf_swcounter_event(PERF_COUNT_SW_CONTEXT_SWITCHES
, 1, 1, regs
, 0);
1135 if (likely(!ctx
|| !cpuctx
->task_ctx
))
1138 update_context_time(ctx
);
1141 parent
= rcu_dereference(ctx
->parent_ctx
);
1142 next_ctx
= next
->perf_counter_ctxp
;
1143 if (parent
&& next_ctx
&&
1144 rcu_dereference(next_ctx
->parent_ctx
) == parent
) {
1146 * Looks like the two contexts are clones, so we might be
1147 * able to optimize the context switch. We lock both
1148 * contexts and check that they are clones under the
1149 * lock (including re-checking that neither has been
1150 * uncloned in the meantime). It doesn't matter which
1151 * order we take the locks because no other cpu could
1152 * be trying to lock both of these tasks.
1154 spin_lock(&ctx
->lock
);
1155 spin_lock_nested(&next_ctx
->lock
, SINGLE_DEPTH_NESTING
);
1156 if (context_equiv(ctx
, next_ctx
)) {
1158 * XXX do we need a memory barrier of sorts
1159 * wrt to rcu_dereference() of perf_counter_ctxp
1161 task
->perf_counter_ctxp
= next_ctx
;
1162 next
->perf_counter_ctxp
= ctx
;
1164 next_ctx
->task
= task
;
1167 perf_counter_sync_stat(ctx
, next_ctx
);
1169 spin_unlock(&next_ctx
->lock
);
1170 spin_unlock(&ctx
->lock
);
1175 __perf_counter_sched_out(ctx
, cpuctx
);
1176 cpuctx
->task_ctx
= NULL
;
1181 * Called with IRQs disabled
1183 static void __perf_counter_task_sched_out(struct perf_counter_context
*ctx
)
1185 struct perf_cpu_context
*cpuctx
= &__get_cpu_var(perf_cpu_context
);
1187 if (!cpuctx
->task_ctx
)
1190 if (WARN_ON_ONCE(ctx
!= cpuctx
->task_ctx
))
1193 __perf_counter_sched_out(ctx
, cpuctx
);
1194 cpuctx
->task_ctx
= NULL
;
1198 * Called with IRQs disabled
1200 static void perf_counter_cpu_sched_out(struct perf_cpu_context
*cpuctx
)
1202 __perf_counter_sched_out(&cpuctx
->ctx
, cpuctx
);
1206 __perf_counter_sched_in(struct perf_counter_context
*ctx
,
1207 struct perf_cpu_context
*cpuctx
, int cpu
)
1209 struct perf_counter
*counter
;
1212 spin_lock(&ctx
->lock
);
1214 if (likely(!ctx
->nr_counters
))
1217 ctx
->timestamp
= perf_clock();
1222 * First go through the list and put on any pinned groups
1223 * in order to give them the best chance of going on.
1225 list_for_each_entry(counter
, &ctx
->counter_list
, list_entry
) {
1226 if (counter
->state
<= PERF_COUNTER_STATE_OFF
||
1227 !counter
->attr
.pinned
)
1229 if (counter
->cpu
!= -1 && counter
->cpu
!= cpu
)
1232 if (counter
!= counter
->group_leader
)
1233 counter_sched_in(counter
, cpuctx
, ctx
, cpu
);
1235 if (group_can_go_on(counter
, cpuctx
, 1))
1236 group_sched_in(counter
, cpuctx
, ctx
, cpu
);
1240 * If this pinned group hasn't been scheduled,
1241 * put it in error state.
1243 if (counter
->state
== PERF_COUNTER_STATE_INACTIVE
) {
1244 update_group_times(counter
);
1245 counter
->state
= PERF_COUNTER_STATE_ERROR
;
1249 list_for_each_entry(counter
, &ctx
->counter_list
, list_entry
) {
1251 * Ignore counters in OFF or ERROR state, and
1252 * ignore pinned counters since we did them already.
1254 if (counter
->state
<= PERF_COUNTER_STATE_OFF
||
1255 counter
->attr
.pinned
)
1259 * Listen to the 'cpu' scheduling filter constraint
1262 if (counter
->cpu
!= -1 && counter
->cpu
!= cpu
)
1265 if (counter
!= counter
->group_leader
) {
1266 if (counter_sched_in(counter
, cpuctx
, ctx
, cpu
))
1269 if (group_can_go_on(counter
, cpuctx
, can_add_hw
)) {
1270 if (group_sched_in(counter
, cpuctx
, ctx
, cpu
))
1277 spin_unlock(&ctx
->lock
);
1281 * Called from scheduler to add the counters of the current task
1282 * with interrupts disabled.
1284 * We restore the counter value and then enable it.
1286 * This does not protect us against NMI, but enable()
1287 * sets the enabled bit in the control field of counter _before_
1288 * accessing the counter control register. If a NMI hits, then it will
1289 * keep the counter running.
1291 void perf_counter_task_sched_in(struct task_struct
*task
, int cpu
)
1293 struct perf_cpu_context
*cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
1294 struct perf_counter_context
*ctx
= task
->perf_counter_ctxp
;
1298 if (cpuctx
->task_ctx
== ctx
)
1300 __perf_counter_sched_in(ctx
, cpuctx
, cpu
);
1301 cpuctx
->task_ctx
= ctx
;
1304 static void perf_counter_cpu_sched_in(struct perf_cpu_context
*cpuctx
, int cpu
)
1306 struct perf_counter_context
*ctx
= &cpuctx
->ctx
;
1308 __perf_counter_sched_in(ctx
, cpuctx
, cpu
);
1311 #define MAX_INTERRUPTS (~0ULL)
1313 static void perf_log_throttle(struct perf_counter
*counter
, int enable
);
1315 static void perf_adjust_period(struct perf_counter
*counter
, u64 events
)
1317 struct hw_perf_counter
*hwc
= &counter
->hw
;
1318 u64 period
, sample_period
;
1321 events
*= hwc
->sample_period
;
1322 period
= div64_u64(events
, counter
->attr
.sample_freq
);
1324 delta
= (s64
)(period
- hwc
->sample_period
);
1325 delta
= (delta
+ 7) / 8; /* low pass filter */
1327 sample_period
= hwc
->sample_period
+ delta
;
1332 hwc
->sample_period
= sample_period
;
1335 static void perf_ctx_adjust_freq(struct perf_counter_context
*ctx
)
1337 struct perf_counter
*counter
;
1338 struct hw_perf_counter
*hwc
;
1339 u64 interrupts
, freq
;
1341 spin_lock(&ctx
->lock
);
1342 list_for_each_entry(counter
, &ctx
->counter_list
, list_entry
) {
1343 if (counter
->state
!= PERF_COUNTER_STATE_ACTIVE
)
1348 interrupts
= hwc
->interrupts
;
1349 hwc
->interrupts
= 0;
1352 * unthrottle counters on the tick
1354 if (interrupts
== MAX_INTERRUPTS
) {
1355 perf_log_throttle(counter
, 1);
1356 counter
->pmu
->unthrottle(counter
);
1357 interrupts
= 2*sysctl_perf_counter_sample_rate
/HZ
;
1360 if (!counter
->attr
.freq
|| !counter
->attr
.sample_freq
)
1364 * if the specified freq < HZ then we need to skip ticks
1366 if (counter
->attr
.sample_freq
< HZ
) {
1367 freq
= counter
->attr
.sample_freq
;
1369 hwc
->freq_count
+= freq
;
1370 hwc
->freq_interrupts
+= interrupts
;
1372 if (hwc
->freq_count
< HZ
)
1375 interrupts
= hwc
->freq_interrupts
;
1376 hwc
->freq_interrupts
= 0;
1377 hwc
->freq_count
-= HZ
;
1381 perf_adjust_period(counter
, freq
* interrupts
);
1384 * In order to avoid being stalled by an (accidental) huge
1385 * sample period, force reset the sample period if we didn't
1386 * get any events in this freq period.
1390 counter
->pmu
->disable(counter
);
1391 atomic64_set(&hwc
->period_left
, 0);
1392 counter
->pmu
->enable(counter
);
1396 spin_unlock(&ctx
->lock
);
1400 * Round-robin a context's counters:
1402 static void rotate_ctx(struct perf_counter_context
*ctx
)
1404 struct perf_counter
*counter
;
1406 if (!ctx
->nr_counters
)
1409 spin_lock(&ctx
->lock
);
1411 * Rotate the first entry last (works just fine for group counters too):
1414 list_for_each_entry(counter
, &ctx
->counter_list
, list_entry
) {
1415 list_move_tail(&counter
->list_entry
, &ctx
->counter_list
);
1420 spin_unlock(&ctx
->lock
);
1423 void perf_counter_task_tick(struct task_struct
*curr
, int cpu
)
1425 struct perf_cpu_context
*cpuctx
;
1426 struct perf_counter_context
*ctx
;
1428 if (!atomic_read(&nr_counters
))
1431 cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
1432 ctx
= curr
->perf_counter_ctxp
;
1434 perf_ctx_adjust_freq(&cpuctx
->ctx
);
1436 perf_ctx_adjust_freq(ctx
);
1438 perf_counter_cpu_sched_out(cpuctx
);
1440 __perf_counter_task_sched_out(ctx
);
1442 rotate_ctx(&cpuctx
->ctx
);
1446 perf_counter_cpu_sched_in(cpuctx
, cpu
);
1448 perf_counter_task_sched_in(curr
, cpu
);
1452 * Enable all of a task's counters that have been marked enable-on-exec.
1453 * This expects task == current.
1455 static void perf_counter_enable_on_exec(struct task_struct
*task
)
1457 struct perf_counter_context
*ctx
;
1458 struct perf_counter
*counter
;
1459 unsigned long flags
;
1462 local_irq_save(flags
);
1463 ctx
= task
->perf_counter_ctxp
;
1464 if (!ctx
|| !ctx
->nr_counters
)
1467 __perf_counter_task_sched_out(ctx
);
1469 spin_lock(&ctx
->lock
);
1471 list_for_each_entry(counter
, &ctx
->counter_list
, list_entry
) {
1472 if (!counter
->attr
.enable_on_exec
)
1474 counter
->attr
.enable_on_exec
= 0;
1475 if (counter
->state
>= PERF_COUNTER_STATE_INACTIVE
)
1477 counter
->state
= PERF_COUNTER_STATE_INACTIVE
;
1478 counter
->tstamp_enabled
=
1479 ctx
->time
- counter
->total_time_enabled
;
1484 * Unclone this context if we enabled any counter.
1489 spin_unlock(&ctx
->lock
);
1491 perf_counter_task_sched_in(task
, smp_processor_id());
1493 local_irq_restore(flags
);
1497 * Cross CPU call to read the hardware counter
1499 static void __perf_counter_read(void *info
)
1501 struct perf_counter
*counter
= info
;
1502 struct perf_counter_context
*ctx
= counter
->ctx
;
1503 unsigned long flags
;
1505 local_irq_save(flags
);
1507 update_context_time(ctx
);
1508 counter
->pmu
->read(counter
);
1509 update_counter_times(counter
);
1510 local_irq_restore(flags
);
1513 static u64
perf_counter_read(struct perf_counter
*counter
)
1516 * If counter is enabled and currently active on a CPU, update the
1517 * value in the counter structure:
1519 if (counter
->state
== PERF_COUNTER_STATE_ACTIVE
) {
1520 smp_call_function_single(counter
->oncpu
,
1521 __perf_counter_read
, counter
, 1);
1522 } else if (counter
->state
== PERF_COUNTER_STATE_INACTIVE
) {
1523 update_counter_times(counter
);
1526 return atomic64_read(&counter
->count
);
1530 * Initialize the perf_counter context in a task_struct:
1533 __perf_counter_init_context(struct perf_counter_context
*ctx
,
1534 struct task_struct
*task
)
1536 memset(ctx
, 0, sizeof(*ctx
));
1537 spin_lock_init(&ctx
->lock
);
1538 mutex_init(&ctx
->mutex
);
1539 INIT_LIST_HEAD(&ctx
->counter_list
);
1540 INIT_LIST_HEAD(&ctx
->event_list
);
1541 atomic_set(&ctx
->refcount
, 1);
1545 static struct perf_counter_context
*find_get_context(pid_t pid
, int cpu
)
1547 struct perf_counter_context
*ctx
;
1548 struct perf_cpu_context
*cpuctx
;
1549 struct task_struct
*task
;
1550 unsigned long flags
;
1554 * If cpu is not a wildcard then this is a percpu counter:
1557 /* Must be root to operate on a CPU counter: */
1558 if (perf_paranoid_cpu() && !capable(CAP_SYS_ADMIN
))
1559 return ERR_PTR(-EACCES
);
1561 if (cpu
< 0 || cpu
> num_possible_cpus())
1562 return ERR_PTR(-EINVAL
);
1565 * We could be clever and allow to attach a counter to an
1566 * offline CPU and activate it when the CPU comes up, but
1569 if (!cpu_isset(cpu
, cpu_online_map
))
1570 return ERR_PTR(-ENODEV
);
1572 cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
1583 task
= find_task_by_vpid(pid
);
1585 get_task_struct(task
);
1589 return ERR_PTR(-ESRCH
);
1592 * Can't attach counters to a dying task.
1595 if (task
->flags
& PF_EXITING
)
1598 /* Reuse ptrace permission checks for now. */
1600 if (!ptrace_may_access(task
, PTRACE_MODE_READ
))
1604 ctx
= perf_lock_task_context(task
, &flags
);
1607 spin_unlock_irqrestore(&ctx
->lock
, flags
);
1611 ctx
= kmalloc(sizeof(struct perf_counter_context
), GFP_KERNEL
);
1615 __perf_counter_init_context(ctx
, task
);
1617 if (cmpxchg(&task
->perf_counter_ctxp
, NULL
, ctx
)) {
1619 * We raced with some other task; use
1620 * the context they set.
1625 get_task_struct(task
);
1628 put_task_struct(task
);
1632 put_task_struct(task
);
1633 return ERR_PTR(err
);
1636 static void free_counter_rcu(struct rcu_head
*head
)
1638 struct perf_counter
*counter
;
1640 counter
= container_of(head
, struct perf_counter
, rcu_head
);
1642 put_pid_ns(counter
->ns
);
1646 static void perf_pending_sync(struct perf_counter
*counter
);
1648 static void free_counter(struct perf_counter
*counter
)
1650 perf_pending_sync(counter
);
1652 if (!counter
->parent
) {
1653 atomic_dec(&nr_counters
);
1654 if (counter
->attr
.mmap
)
1655 atomic_dec(&nr_mmap_counters
);
1656 if (counter
->attr
.comm
)
1657 atomic_dec(&nr_comm_counters
);
1658 if (counter
->attr
.task
)
1659 atomic_dec(&nr_task_counters
);
1662 if (counter
->destroy
)
1663 counter
->destroy(counter
);
1665 put_ctx(counter
->ctx
);
1666 call_rcu(&counter
->rcu_head
, free_counter_rcu
);
1670 * Called when the last reference to the file is gone.
1672 static int perf_release(struct inode
*inode
, struct file
*file
)
1674 struct perf_counter
*counter
= file
->private_data
;
1675 struct perf_counter_context
*ctx
= counter
->ctx
;
1677 file
->private_data
= NULL
;
1679 WARN_ON_ONCE(ctx
->parent_ctx
);
1680 mutex_lock(&ctx
->mutex
);
1681 perf_counter_remove_from_context(counter
);
1682 mutex_unlock(&ctx
->mutex
);
1684 mutex_lock(&counter
->owner
->perf_counter_mutex
);
1685 list_del_init(&counter
->owner_entry
);
1686 mutex_unlock(&counter
->owner
->perf_counter_mutex
);
1687 put_task_struct(counter
->owner
);
1689 free_counter(counter
);
1694 static u64
perf_counter_read_tree(struct perf_counter
*counter
)
1696 struct perf_counter
*child
;
1699 total
+= perf_counter_read(counter
);
1700 list_for_each_entry(child
, &counter
->child_list
, child_list
)
1701 total
+= perf_counter_read(child
);
1707 * Read the performance counter - simple non blocking version for now
1710 perf_read_hw(struct perf_counter
*counter
, char __user
*buf
, size_t count
)
1716 * Return end-of-file for a read on a counter that is in
1717 * error state (i.e. because it was pinned but it couldn't be
1718 * scheduled on to the CPU at some point).
1720 if (counter
->state
== PERF_COUNTER_STATE_ERROR
)
1723 WARN_ON_ONCE(counter
->ctx
->parent_ctx
);
1724 mutex_lock(&counter
->child_mutex
);
1725 values
[0] = perf_counter_read_tree(counter
);
1727 if (counter
->attr
.read_format
& PERF_FORMAT_TOTAL_TIME_ENABLED
)
1728 values
[n
++] = counter
->total_time_enabled
+
1729 atomic64_read(&counter
->child_total_time_enabled
);
1730 if (counter
->attr
.read_format
& PERF_FORMAT_TOTAL_TIME_RUNNING
)
1731 values
[n
++] = counter
->total_time_running
+
1732 atomic64_read(&counter
->child_total_time_running
);
1733 if (counter
->attr
.read_format
& PERF_FORMAT_ID
)
1734 values
[n
++] = primary_counter_id(counter
);
1735 mutex_unlock(&counter
->child_mutex
);
1737 if (count
< n
* sizeof(u64
))
1739 count
= n
* sizeof(u64
);
1741 if (copy_to_user(buf
, values
, count
))
1748 perf_read(struct file
*file
, char __user
*buf
, size_t count
, loff_t
*ppos
)
1750 struct perf_counter
*counter
= file
->private_data
;
1752 return perf_read_hw(counter
, buf
, count
);
1755 static unsigned int perf_poll(struct file
*file
, poll_table
*wait
)
1757 struct perf_counter
*counter
= file
->private_data
;
1758 struct perf_mmap_data
*data
;
1759 unsigned int events
= POLL_HUP
;
1762 data
= rcu_dereference(counter
->data
);
1764 events
= atomic_xchg(&data
->poll
, 0);
1767 poll_wait(file
, &counter
->waitq
, wait
);
1772 static void perf_counter_reset(struct perf_counter
*counter
)
1774 (void)perf_counter_read(counter
);
1775 atomic64_set(&counter
->count
, 0);
1776 perf_counter_update_userpage(counter
);
1780 * Holding the top-level counter's child_mutex means that any
1781 * descendant process that has inherited this counter will block
1782 * in sync_child_counter if it goes to exit, thus satisfying the
1783 * task existence requirements of perf_counter_enable/disable.
1785 static void perf_counter_for_each_child(struct perf_counter
*counter
,
1786 void (*func
)(struct perf_counter
*))
1788 struct perf_counter
*child
;
1790 WARN_ON_ONCE(counter
->ctx
->parent_ctx
);
1791 mutex_lock(&counter
->child_mutex
);
1793 list_for_each_entry(child
, &counter
->child_list
, child_list
)
1795 mutex_unlock(&counter
->child_mutex
);
1798 static void perf_counter_for_each(struct perf_counter
*counter
,
1799 void (*func
)(struct perf_counter
*))
1801 struct perf_counter_context
*ctx
= counter
->ctx
;
1802 struct perf_counter
*sibling
;
1804 WARN_ON_ONCE(ctx
->parent_ctx
);
1805 mutex_lock(&ctx
->mutex
);
1806 counter
= counter
->group_leader
;
1808 perf_counter_for_each_child(counter
, func
);
1810 list_for_each_entry(sibling
, &counter
->sibling_list
, list_entry
)
1811 perf_counter_for_each_child(counter
, func
);
1812 mutex_unlock(&ctx
->mutex
);
1815 static int perf_counter_period(struct perf_counter
*counter
, u64 __user
*arg
)
1817 struct perf_counter_context
*ctx
= counter
->ctx
;
1822 if (!counter
->attr
.sample_period
)
1825 size
= copy_from_user(&value
, arg
, sizeof(value
));
1826 if (size
!= sizeof(value
))
1832 spin_lock_irq(&ctx
->lock
);
1833 if (counter
->attr
.freq
) {
1834 if (value
> sysctl_perf_counter_sample_rate
) {
1839 counter
->attr
.sample_freq
= value
;
1841 counter
->attr
.sample_period
= value
;
1842 counter
->hw
.sample_period
= value
;
1845 spin_unlock_irq(&ctx
->lock
);
1850 static long perf_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
1852 struct perf_counter
*counter
= file
->private_data
;
1853 void (*func
)(struct perf_counter
*);
1857 case PERF_COUNTER_IOC_ENABLE
:
1858 func
= perf_counter_enable
;
1860 case PERF_COUNTER_IOC_DISABLE
:
1861 func
= perf_counter_disable
;
1863 case PERF_COUNTER_IOC_RESET
:
1864 func
= perf_counter_reset
;
1867 case PERF_COUNTER_IOC_REFRESH
:
1868 return perf_counter_refresh(counter
, arg
);
1870 case PERF_COUNTER_IOC_PERIOD
:
1871 return perf_counter_period(counter
, (u64 __user
*)arg
);
1877 if (flags
& PERF_IOC_FLAG_GROUP
)
1878 perf_counter_for_each(counter
, func
);
1880 perf_counter_for_each_child(counter
, func
);
1885 int perf_counter_task_enable(void)
1887 struct perf_counter
*counter
;
1889 mutex_lock(¤t
->perf_counter_mutex
);
1890 list_for_each_entry(counter
, ¤t
->perf_counter_list
, owner_entry
)
1891 perf_counter_for_each_child(counter
, perf_counter_enable
);
1892 mutex_unlock(¤t
->perf_counter_mutex
);
1897 int perf_counter_task_disable(void)
1899 struct perf_counter
*counter
;
1901 mutex_lock(¤t
->perf_counter_mutex
);
1902 list_for_each_entry(counter
, ¤t
->perf_counter_list
, owner_entry
)
1903 perf_counter_for_each_child(counter
, perf_counter_disable
);
1904 mutex_unlock(¤t
->perf_counter_mutex
);
1909 static int perf_counter_index(struct perf_counter
*counter
)
1911 if (counter
->state
!= PERF_COUNTER_STATE_ACTIVE
)
1914 return counter
->hw
.idx
+ 1 - PERF_COUNTER_INDEX_OFFSET
;
1918 * Callers need to ensure there can be no nesting of this function, otherwise
1919 * the seqlock logic goes bad. We can not serialize this because the arch
1920 * code calls this from NMI context.
1922 void perf_counter_update_userpage(struct perf_counter
*counter
)
1924 struct perf_counter_mmap_page
*userpg
;
1925 struct perf_mmap_data
*data
;
1928 data
= rcu_dereference(counter
->data
);
1932 userpg
= data
->user_page
;
1935 * Disable preemption so as to not let the corresponding user-space
1936 * spin too long if we get preempted.
1941 userpg
->index
= perf_counter_index(counter
);
1942 userpg
->offset
= atomic64_read(&counter
->count
);
1943 if (counter
->state
== PERF_COUNTER_STATE_ACTIVE
)
1944 userpg
->offset
-= atomic64_read(&counter
->hw
.prev_count
);
1946 userpg
->time_enabled
= counter
->total_time_enabled
+
1947 atomic64_read(&counter
->child_total_time_enabled
);
1949 userpg
->time_running
= counter
->total_time_running
+
1950 atomic64_read(&counter
->child_total_time_running
);
1959 static int perf_mmap_fault(struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
1961 struct perf_counter
*counter
= vma
->vm_file
->private_data
;
1962 struct perf_mmap_data
*data
;
1963 int ret
= VM_FAULT_SIGBUS
;
1965 if (vmf
->flags
& FAULT_FLAG_MKWRITE
) {
1966 if (vmf
->pgoff
== 0)
1972 data
= rcu_dereference(counter
->data
);
1976 if (vmf
->pgoff
== 0) {
1977 vmf
->page
= virt_to_page(data
->user_page
);
1979 int nr
= vmf
->pgoff
- 1;
1981 if ((unsigned)nr
> data
->nr_pages
)
1984 if (vmf
->flags
& FAULT_FLAG_WRITE
)
1987 vmf
->page
= virt_to_page(data
->data_pages
[nr
]);
1990 get_page(vmf
->page
);
1991 vmf
->page
->mapping
= vma
->vm_file
->f_mapping
;
1992 vmf
->page
->index
= vmf
->pgoff
;
2001 static int perf_mmap_data_alloc(struct perf_counter
*counter
, int nr_pages
)
2003 struct perf_mmap_data
*data
;
2007 WARN_ON(atomic_read(&counter
->mmap_count
));
2009 size
= sizeof(struct perf_mmap_data
);
2010 size
+= nr_pages
* sizeof(void *);
2012 data
= kzalloc(size
, GFP_KERNEL
);
2016 data
->user_page
= (void *)get_zeroed_page(GFP_KERNEL
);
2017 if (!data
->user_page
)
2018 goto fail_user_page
;
2020 for (i
= 0; i
< nr_pages
; i
++) {
2021 data
->data_pages
[i
] = (void *)get_zeroed_page(GFP_KERNEL
);
2022 if (!data
->data_pages
[i
])
2023 goto fail_data_pages
;
2026 data
->nr_pages
= nr_pages
;
2027 atomic_set(&data
->lock
, -1);
2029 rcu_assign_pointer(counter
->data
, data
);
2034 for (i
--; i
>= 0; i
--)
2035 free_page((unsigned long)data
->data_pages
[i
]);
2037 free_page((unsigned long)data
->user_page
);
2046 static void perf_mmap_free_page(unsigned long addr
)
2048 struct page
*page
= virt_to_page((void *)addr
);
2050 page
->mapping
= NULL
;
2054 static void __perf_mmap_data_free(struct rcu_head
*rcu_head
)
2056 struct perf_mmap_data
*data
;
2059 data
= container_of(rcu_head
, struct perf_mmap_data
, rcu_head
);
2061 perf_mmap_free_page((unsigned long)data
->user_page
);
2062 for (i
= 0; i
< data
->nr_pages
; i
++)
2063 perf_mmap_free_page((unsigned long)data
->data_pages
[i
]);
2068 static void perf_mmap_data_free(struct perf_counter
*counter
)
2070 struct perf_mmap_data
*data
= counter
->data
;
2072 WARN_ON(atomic_read(&counter
->mmap_count
));
2074 rcu_assign_pointer(counter
->data
, NULL
);
2075 call_rcu(&data
->rcu_head
, __perf_mmap_data_free
);
2078 static void perf_mmap_open(struct vm_area_struct
*vma
)
2080 struct perf_counter
*counter
= vma
->vm_file
->private_data
;
2082 atomic_inc(&counter
->mmap_count
);
2085 static void perf_mmap_close(struct vm_area_struct
*vma
)
2087 struct perf_counter
*counter
= vma
->vm_file
->private_data
;
2089 WARN_ON_ONCE(counter
->ctx
->parent_ctx
);
2090 if (atomic_dec_and_mutex_lock(&counter
->mmap_count
, &counter
->mmap_mutex
)) {
2091 struct user_struct
*user
= current_user();
2093 atomic_long_sub(counter
->data
->nr_pages
+ 1, &user
->locked_vm
);
2094 vma
->vm_mm
->locked_vm
-= counter
->data
->nr_locked
;
2095 perf_mmap_data_free(counter
);
2096 mutex_unlock(&counter
->mmap_mutex
);
2100 static struct vm_operations_struct perf_mmap_vmops
= {
2101 .open
= perf_mmap_open
,
2102 .close
= perf_mmap_close
,
2103 .fault
= perf_mmap_fault
,
2104 .page_mkwrite
= perf_mmap_fault
,
2107 static int perf_mmap(struct file
*file
, struct vm_area_struct
*vma
)
2109 struct perf_counter
*counter
= file
->private_data
;
2110 unsigned long user_locked
, user_lock_limit
;
2111 struct user_struct
*user
= current_user();
2112 unsigned long locked
, lock_limit
;
2113 unsigned long vma_size
;
2114 unsigned long nr_pages
;
2115 long user_extra
, extra
;
2118 if (!(vma
->vm_flags
& VM_SHARED
))
2121 vma_size
= vma
->vm_end
- vma
->vm_start
;
2122 nr_pages
= (vma_size
/ PAGE_SIZE
) - 1;
2125 * If we have data pages ensure they're a power-of-two number, so we
2126 * can do bitmasks instead of modulo.
2128 if (nr_pages
!= 0 && !is_power_of_2(nr_pages
))
2131 if (vma_size
!= PAGE_SIZE
* (1 + nr_pages
))
2134 if (vma
->vm_pgoff
!= 0)
2137 WARN_ON_ONCE(counter
->ctx
->parent_ctx
);
2138 mutex_lock(&counter
->mmap_mutex
);
2139 if (atomic_inc_not_zero(&counter
->mmap_count
)) {
2140 if (nr_pages
!= counter
->data
->nr_pages
)
2145 user_extra
= nr_pages
+ 1;
2146 user_lock_limit
= sysctl_perf_counter_mlock
>> (PAGE_SHIFT
- 10);
2149 * Increase the limit linearly with more CPUs:
2151 user_lock_limit
*= num_online_cpus();
2153 user_locked
= atomic_long_read(&user
->locked_vm
) + user_extra
;
2156 if (user_locked
> user_lock_limit
)
2157 extra
= user_locked
- user_lock_limit
;
2159 lock_limit
= current
->signal
->rlim
[RLIMIT_MEMLOCK
].rlim_cur
;
2160 lock_limit
>>= PAGE_SHIFT
;
2161 locked
= vma
->vm_mm
->locked_vm
+ extra
;
2163 if ((locked
> lock_limit
) && !capable(CAP_IPC_LOCK
)) {
2168 WARN_ON(counter
->data
);
2169 ret
= perf_mmap_data_alloc(counter
, nr_pages
);
2173 atomic_set(&counter
->mmap_count
, 1);
2174 atomic_long_add(user_extra
, &user
->locked_vm
);
2175 vma
->vm_mm
->locked_vm
+= extra
;
2176 counter
->data
->nr_locked
= extra
;
2177 if (vma
->vm_flags
& VM_WRITE
)
2178 counter
->data
->writable
= 1;
2181 mutex_unlock(&counter
->mmap_mutex
);
2183 vma
->vm_flags
|= VM_RESERVED
;
2184 vma
->vm_ops
= &perf_mmap_vmops
;
2189 static int perf_fasync(int fd
, struct file
*filp
, int on
)
2191 struct inode
*inode
= filp
->f_path
.dentry
->d_inode
;
2192 struct perf_counter
*counter
= filp
->private_data
;
2195 mutex_lock(&inode
->i_mutex
);
2196 retval
= fasync_helper(fd
, filp
, on
, &counter
->fasync
);
2197 mutex_unlock(&inode
->i_mutex
);
2205 static const struct file_operations perf_fops
= {
2206 .release
= perf_release
,
2209 .unlocked_ioctl
= perf_ioctl
,
2210 .compat_ioctl
= perf_ioctl
,
2212 .fasync
= perf_fasync
,
2216 * Perf counter wakeup
2218 * If there's data, ensure we set the poll() state and publish everything
2219 * to user-space before waking everybody up.
2222 void perf_counter_wakeup(struct perf_counter
*counter
)
2224 wake_up_all(&counter
->waitq
);
2226 if (counter
->pending_kill
) {
2227 kill_fasync(&counter
->fasync
, SIGIO
, counter
->pending_kill
);
2228 counter
->pending_kill
= 0;
2235 * Handle the case where we need to wakeup up from NMI (or rq->lock) context.
2237 * The NMI bit means we cannot possibly take locks. Therefore, maintain a
2238 * single linked list and use cmpxchg() to add entries lockless.
2241 static void perf_pending_counter(struct perf_pending_entry
*entry
)
2243 struct perf_counter
*counter
= container_of(entry
,
2244 struct perf_counter
, pending
);
2246 if (counter
->pending_disable
) {
2247 counter
->pending_disable
= 0;
2248 perf_counter_disable(counter
);
2251 if (counter
->pending_wakeup
) {
2252 counter
->pending_wakeup
= 0;
2253 perf_counter_wakeup(counter
);
2257 #define PENDING_TAIL ((struct perf_pending_entry *)-1UL)
2259 static DEFINE_PER_CPU(struct perf_pending_entry
*, perf_pending_head
) = {
2263 static void perf_pending_queue(struct perf_pending_entry
*entry
,
2264 void (*func
)(struct perf_pending_entry
*))
2266 struct perf_pending_entry
**head
;
2268 if (cmpxchg(&entry
->next
, NULL
, PENDING_TAIL
) != NULL
)
2273 head
= &get_cpu_var(perf_pending_head
);
2276 entry
->next
= *head
;
2277 } while (cmpxchg(head
, entry
->next
, entry
) != entry
->next
);
2279 set_perf_counter_pending();
2281 put_cpu_var(perf_pending_head
);
2284 static int __perf_pending_run(void)
2286 struct perf_pending_entry
*list
;
2289 list
= xchg(&__get_cpu_var(perf_pending_head
), PENDING_TAIL
);
2290 while (list
!= PENDING_TAIL
) {
2291 void (*func
)(struct perf_pending_entry
*);
2292 struct perf_pending_entry
*entry
= list
;
2299 * Ensure we observe the unqueue before we issue the wakeup,
2300 * so that we won't be waiting forever.
2301 * -- see perf_not_pending().
2312 static inline int perf_not_pending(struct perf_counter
*counter
)
2315 * If we flush on whatever cpu we run, there is a chance we don't
2319 __perf_pending_run();
2323 * Ensure we see the proper queue state before going to sleep
2324 * so that we do not miss the wakeup. -- see perf_pending_handle()
2327 return counter
->pending
.next
== NULL
;
2330 static void perf_pending_sync(struct perf_counter
*counter
)
2332 wait_event(counter
->waitq
, perf_not_pending(counter
));
2335 void perf_counter_do_pending(void)
2337 __perf_pending_run();
2341 * Callchain support -- arch specific
2344 __weak
struct perf_callchain_entry
*perf_callchain(struct pt_regs
*regs
)
2353 struct perf_output_handle
{
2354 struct perf_counter
*counter
;
2355 struct perf_mmap_data
*data
;
2357 unsigned long offset
;
2361 unsigned long flags
;
2364 static bool perf_output_space(struct perf_mmap_data
*data
,
2365 unsigned int offset
, unsigned int head
)
2370 if (!data
->writable
)
2373 mask
= (data
->nr_pages
<< PAGE_SHIFT
) - 1;
2375 * Userspace could choose to issue a mb() before updating the tail
2376 * pointer. So that all reads will be completed before the write is
2379 tail
= ACCESS_ONCE(data
->user_page
->data_tail
);
2382 offset
= (offset
- tail
) & mask
;
2383 head
= (head
- tail
) & mask
;
2385 if ((int)(head
- offset
) < 0)
2391 static void perf_output_wakeup(struct perf_output_handle
*handle
)
2393 atomic_set(&handle
->data
->poll
, POLL_IN
);
2396 handle
->counter
->pending_wakeup
= 1;
2397 perf_pending_queue(&handle
->counter
->pending
,
2398 perf_pending_counter
);
2400 perf_counter_wakeup(handle
->counter
);
2404 * Curious locking construct.
2406 * We need to ensure a later event doesn't publish a head when a former
2407 * event isn't done writing. However since we need to deal with NMIs we
2408 * cannot fully serialize things.
2410 * What we do is serialize between CPUs so we only have to deal with NMI
2411 * nesting on a single CPU.
2413 * We only publish the head (and generate a wakeup) when the outer-most
2416 static void perf_output_lock(struct perf_output_handle
*handle
)
2418 struct perf_mmap_data
*data
= handle
->data
;
2423 local_irq_save(handle
->flags
);
2424 cpu
= smp_processor_id();
2426 if (in_nmi() && atomic_read(&data
->lock
) == cpu
)
2429 while (atomic_cmpxchg(&data
->lock
, -1, cpu
) != -1)
2435 static void perf_output_unlock(struct perf_output_handle
*handle
)
2437 struct perf_mmap_data
*data
= handle
->data
;
2441 data
->done_head
= data
->head
;
2443 if (!handle
->locked
)
2448 * The xchg implies a full barrier that ensures all writes are done
2449 * before we publish the new head, matched by a rmb() in userspace when
2450 * reading this position.
2452 while ((head
= atomic_long_xchg(&data
->done_head
, 0)))
2453 data
->user_page
->data_head
= head
;
2456 * NMI can happen here, which means we can miss a done_head update.
2459 cpu
= atomic_xchg(&data
->lock
, -1);
2460 WARN_ON_ONCE(cpu
!= smp_processor_id());
2463 * Therefore we have to validate we did not indeed do so.
2465 if (unlikely(atomic_long_read(&data
->done_head
))) {
2467 * Since we had it locked, we can lock it again.
2469 while (atomic_cmpxchg(&data
->lock
, -1, cpu
) != -1)
2475 if (atomic_xchg(&data
->wakeup
, 0))
2476 perf_output_wakeup(handle
);
2478 local_irq_restore(handle
->flags
);
2481 static void perf_output_copy(struct perf_output_handle
*handle
,
2482 const void *buf
, unsigned int len
)
2484 unsigned int pages_mask
;
2485 unsigned int offset
;
2489 offset
= handle
->offset
;
2490 pages_mask
= handle
->data
->nr_pages
- 1;
2491 pages
= handle
->data
->data_pages
;
2494 unsigned int page_offset
;
2497 nr
= (offset
>> PAGE_SHIFT
) & pages_mask
;
2498 page_offset
= offset
& (PAGE_SIZE
- 1);
2499 size
= min_t(unsigned int, PAGE_SIZE
- page_offset
, len
);
2501 memcpy(pages
[nr
] + page_offset
, buf
, size
);
2508 handle
->offset
= offset
;
2511 * Check we didn't copy past our reservation window, taking the
2512 * possible unsigned int wrap into account.
2514 WARN_ON_ONCE(((long)(handle
->head
- handle
->offset
)) < 0);
2517 #define perf_output_put(handle, x) \
2518 perf_output_copy((handle), &(x), sizeof(x))
2520 static int perf_output_begin(struct perf_output_handle
*handle
,
2521 struct perf_counter
*counter
, unsigned int size
,
2522 int nmi
, int sample
)
2524 struct perf_mmap_data
*data
;
2525 unsigned int offset
, head
;
2528 struct perf_event_header header
;
2534 * For inherited counters we send all the output towards the parent.
2536 if (counter
->parent
)
2537 counter
= counter
->parent
;
2540 data
= rcu_dereference(counter
->data
);
2544 handle
->data
= data
;
2545 handle
->counter
= counter
;
2547 handle
->sample
= sample
;
2549 if (!data
->nr_pages
)
2552 have_lost
= atomic_read(&data
->lost
);
2554 size
+= sizeof(lost_event
);
2556 perf_output_lock(handle
);
2559 offset
= head
= atomic_long_read(&data
->head
);
2561 if (unlikely(!perf_output_space(data
, offset
, head
)))
2563 } while (atomic_long_cmpxchg(&data
->head
, offset
, head
) != offset
);
2565 handle
->offset
= offset
;
2566 handle
->head
= head
;
2568 if ((offset
>> PAGE_SHIFT
) != (head
>> PAGE_SHIFT
))
2569 atomic_set(&data
->wakeup
, 1);
2572 lost_event
.header
.type
= PERF_EVENT_LOST
;
2573 lost_event
.header
.misc
= 0;
2574 lost_event
.header
.size
= sizeof(lost_event
);
2575 lost_event
.id
= counter
->id
;
2576 lost_event
.lost
= atomic_xchg(&data
->lost
, 0);
2578 perf_output_put(handle
, lost_event
);
2584 atomic_inc(&data
->lost
);
2585 perf_output_unlock(handle
);
2592 static void perf_output_end(struct perf_output_handle
*handle
)
2594 struct perf_counter
*counter
= handle
->counter
;
2595 struct perf_mmap_data
*data
= handle
->data
;
2597 int wakeup_events
= counter
->attr
.wakeup_events
;
2599 if (handle
->sample
&& wakeup_events
) {
2600 int events
= atomic_inc_return(&data
->events
);
2601 if (events
>= wakeup_events
) {
2602 atomic_sub(wakeup_events
, &data
->events
);
2603 atomic_set(&data
->wakeup
, 1);
2607 perf_output_unlock(handle
);
2611 static u32
perf_counter_pid(struct perf_counter
*counter
, struct task_struct
*p
)
2614 * only top level counters have the pid namespace they were created in
2616 if (counter
->parent
)
2617 counter
= counter
->parent
;
2619 return task_tgid_nr_ns(p
, counter
->ns
);
2622 static u32
perf_counter_tid(struct perf_counter
*counter
, struct task_struct
*p
)
2625 * only top level counters have the pid namespace they were created in
2627 if (counter
->parent
)
2628 counter
= counter
->parent
;
2630 return task_pid_nr_ns(p
, counter
->ns
);
2633 static void perf_counter_output(struct perf_counter
*counter
, int nmi
,
2634 struct perf_sample_data
*data
)
2637 u64 sample_type
= counter
->attr
.sample_type
;
2638 struct perf_output_handle handle
;
2639 struct perf_event_header header
;
2648 struct perf_callchain_entry
*callchain
= NULL
;
2649 int callchain_size
= 0;
2655 header
.type
= PERF_EVENT_SAMPLE
;
2656 header
.size
= sizeof(header
);
2659 header
.misc
|= perf_misc_flags(data
->regs
);
2661 if (sample_type
& PERF_SAMPLE_IP
) {
2662 ip
= perf_instruction_pointer(data
->regs
);
2663 header
.size
+= sizeof(ip
);
2666 if (sample_type
& PERF_SAMPLE_TID
) {
2667 /* namespace issues */
2668 tid_entry
.pid
= perf_counter_pid(counter
, current
);
2669 tid_entry
.tid
= perf_counter_tid(counter
, current
);
2671 header
.size
+= sizeof(tid_entry
);
2674 if (sample_type
& PERF_SAMPLE_TIME
) {
2676 * Maybe do better on x86 and provide cpu_clock_nmi()
2678 time
= sched_clock();
2680 header
.size
+= sizeof(u64
);
2683 if (sample_type
& PERF_SAMPLE_ADDR
)
2684 header
.size
+= sizeof(u64
);
2686 if (sample_type
& PERF_SAMPLE_ID
)
2687 header
.size
+= sizeof(u64
);
2689 if (sample_type
& PERF_SAMPLE_STREAM_ID
)
2690 header
.size
+= sizeof(u64
);
2692 if (sample_type
& PERF_SAMPLE_CPU
) {
2693 header
.size
+= sizeof(cpu_entry
);
2695 cpu_entry
.cpu
= raw_smp_processor_id();
2696 cpu_entry
.reserved
= 0;
2699 if (sample_type
& PERF_SAMPLE_PERIOD
)
2700 header
.size
+= sizeof(u64
);
2702 if (sample_type
& PERF_SAMPLE_GROUP
) {
2703 header
.size
+= sizeof(u64
) +
2704 counter
->nr_siblings
* sizeof(group_entry
);
2707 if (sample_type
& PERF_SAMPLE_CALLCHAIN
) {
2708 callchain
= perf_callchain(data
->regs
);
2711 callchain_size
= (1 + callchain
->nr
) * sizeof(u64
);
2712 header
.size
+= callchain_size
;
2714 header
.size
+= sizeof(u64
);
2717 if (sample_type
& PERF_SAMPLE_RAW
) {
2718 int size
= sizeof(u32
);
2721 size
+= data
->raw
->size
;
2723 size
+= sizeof(u32
);
2725 WARN_ON_ONCE(size
& (sizeof(u64
)-1));
2726 header
.size
+= size
;
2729 ret
= perf_output_begin(&handle
, counter
, header
.size
, nmi
, 1);
2733 perf_output_put(&handle
, header
);
2735 if (sample_type
& PERF_SAMPLE_IP
)
2736 perf_output_put(&handle
, ip
);
2738 if (sample_type
& PERF_SAMPLE_TID
)
2739 perf_output_put(&handle
, tid_entry
);
2741 if (sample_type
& PERF_SAMPLE_TIME
)
2742 perf_output_put(&handle
, time
);
2744 if (sample_type
& PERF_SAMPLE_ADDR
)
2745 perf_output_put(&handle
, data
->addr
);
2747 if (sample_type
& PERF_SAMPLE_ID
) {
2748 u64 id
= primary_counter_id(counter
);
2750 perf_output_put(&handle
, id
);
2753 if (sample_type
& PERF_SAMPLE_STREAM_ID
)
2754 perf_output_put(&handle
, counter
->id
);
2756 if (sample_type
& PERF_SAMPLE_CPU
)
2757 perf_output_put(&handle
, cpu_entry
);
2759 if (sample_type
& PERF_SAMPLE_PERIOD
)
2760 perf_output_put(&handle
, data
->period
);
2763 * XXX PERF_SAMPLE_GROUP vs inherited counters seems difficult.
2765 if (sample_type
& PERF_SAMPLE_GROUP
) {
2766 struct perf_counter
*leader
, *sub
;
2767 u64 nr
= counter
->nr_siblings
;
2769 perf_output_put(&handle
, nr
);
2771 leader
= counter
->group_leader
;
2772 list_for_each_entry(sub
, &leader
->sibling_list
, list_entry
) {
2774 sub
->pmu
->read(sub
);
2776 group_entry
.id
= primary_counter_id(sub
);
2777 group_entry
.counter
= atomic64_read(&sub
->count
);
2779 perf_output_put(&handle
, group_entry
);
2783 if (sample_type
& PERF_SAMPLE_CALLCHAIN
) {
2785 perf_output_copy(&handle
, callchain
, callchain_size
);
2788 perf_output_put(&handle
, nr
);
2792 if (sample_type
& PERF_SAMPLE_RAW
) {
2794 perf_output_put(&handle
, data
->raw
->size
);
2795 perf_output_copy(&handle
, data
->raw
->data
, data
->raw
->size
);
2801 .size
= sizeof(u32
),
2804 perf_output_put(&handle
, raw
);
2808 perf_output_end(&handle
);
2815 struct perf_read_event
{
2816 struct perf_event_header header
;
2825 perf_counter_read_event(struct perf_counter
*counter
,
2826 struct task_struct
*task
)
2828 struct perf_output_handle handle
;
2829 struct perf_read_event event
= {
2831 .type
= PERF_EVENT_READ
,
2833 .size
= sizeof(event
) - sizeof(event
.format
),
2835 .pid
= perf_counter_pid(counter
, task
),
2836 .tid
= perf_counter_tid(counter
, task
),
2837 .value
= atomic64_read(&counter
->count
),
2841 if (counter
->attr
.read_format
& PERF_FORMAT_TOTAL_TIME_ENABLED
) {
2842 event
.header
.size
+= sizeof(u64
);
2843 event
.format
[i
++] = counter
->total_time_enabled
;
2846 if (counter
->attr
.read_format
& PERF_FORMAT_TOTAL_TIME_RUNNING
) {
2847 event
.header
.size
+= sizeof(u64
);
2848 event
.format
[i
++] = counter
->total_time_running
;
2851 if (counter
->attr
.read_format
& PERF_FORMAT_ID
) {
2852 event
.header
.size
+= sizeof(u64
);
2853 event
.format
[i
++] = primary_counter_id(counter
);
2856 ret
= perf_output_begin(&handle
, counter
, event
.header
.size
, 0, 0);
2860 perf_output_copy(&handle
, &event
, event
.header
.size
);
2861 perf_output_end(&handle
);
2865 * task tracking -- fork/exit
2867 * enabled by: attr.comm | attr.mmap | attr.task
2870 struct perf_task_event
{
2871 struct task_struct
*task
;
2872 struct perf_counter_context
*task_ctx
;
2875 struct perf_event_header header
;
2884 static void perf_counter_task_output(struct perf_counter
*counter
,
2885 struct perf_task_event
*task_event
)
2887 struct perf_output_handle handle
;
2888 int size
= task_event
->event
.header
.size
;
2889 struct task_struct
*task
= task_event
->task
;
2890 int ret
= perf_output_begin(&handle
, counter
, size
, 0, 0);
2895 task_event
->event
.pid
= perf_counter_pid(counter
, task
);
2896 task_event
->event
.ppid
= perf_counter_pid(counter
, task
->real_parent
);
2898 task_event
->event
.tid
= perf_counter_tid(counter
, task
);
2899 task_event
->event
.ptid
= perf_counter_tid(counter
, task
->real_parent
);
2901 perf_output_put(&handle
, task_event
->event
);
2902 perf_output_end(&handle
);
2905 static int perf_counter_task_match(struct perf_counter
*counter
)
2907 if (counter
->attr
.comm
|| counter
->attr
.mmap
|| counter
->attr
.task
)
2913 static void perf_counter_task_ctx(struct perf_counter_context
*ctx
,
2914 struct perf_task_event
*task_event
)
2916 struct perf_counter
*counter
;
2918 if (system_state
!= SYSTEM_RUNNING
|| list_empty(&ctx
->event_list
))
2922 list_for_each_entry_rcu(counter
, &ctx
->event_list
, event_entry
) {
2923 if (perf_counter_task_match(counter
))
2924 perf_counter_task_output(counter
, task_event
);
2929 static void perf_counter_task_event(struct perf_task_event
*task_event
)
2931 struct perf_cpu_context
*cpuctx
;
2932 struct perf_counter_context
*ctx
= task_event
->task_ctx
;
2934 cpuctx
= &get_cpu_var(perf_cpu_context
);
2935 perf_counter_task_ctx(&cpuctx
->ctx
, task_event
);
2936 put_cpu_var(perf_cpu_context
);
2940 ctx
= rcu_dereference(task_event
->task
->perf_counter_ctxp
);
2942 perf_counter_task_ctx(ctx
, task_event
);
2946 static void perf_counter_task(struct task_struct
*task
,
2947 struct perf_counter_context
*task_ctx
,
2950 struct perf_task_event task_event
;
2952 if (!atomic_read(&nr_comm_counters
) &&
2953 !atomic_read(&nr_mmap_counters
) &&
2954 !atomic_read(&nr_task_counters
))
2957 task_event
= (struct perf_task_event
){
2959 .task_ctx
= task_ctx
,
2962 .type
= new ? PERF_EVENT_FORK
: PERF_EVENT_EXIT
,
2964 .size
= sizeof(task_event
.event
),
2973 perf_counter_task_event(&task_event
);
2976 void perf_counter_fork(struct task_struct
*task
)
2978 perf_counter_task(task
, NULL
, 1);
2985 struct perf_comm_event
{
2986 struct task_struct
*task
;
2991 struct perf_event_header header
;
2998 static void perf_counter_comm_output(struct perf_counter
*counter
,
2999 struct perf_comm_event
*comm_event
)
3001 struct perf_output_handle handle
;
3002 int size
= comm_event
->event
.header
.size
;
3003 int ret
= perf_output_begin(&handle
, counter
, size
, 0, 0);
3008 comm_event
->event
.pid
= perf_counter_pid(counter
, comm_event
->task
);
3009 comm_event
->event
.tid
= perf_counter_tid(counter
, comm_event
->task
);
3011 perf_output_put(&handle
, comm_event
->event
);
3012 perf_output_copy(&handle
, comm_event
->comm
,
3013 comm_event
->comm_size
);
3014 perf_output_end(&handle
);
3017 static int perf_counter_comm_match(struct perf_counter
*counter
)
3019 if (counter
->attr
.comm
)
3025 static void perf_counter_comm_ctx(struct perf_counter_context
*ctx
,
3026 struct perf_comm_event
*comm_event
)
3028 struct perf_counter
*counter
;
3030 if (system_state
!= SYSTEM_RUNNING
|| list_empty(&ctx
->event_list
))
3034 list_for_each_entry_rcu(counter
, &ctx
->event_list
, event_entry
) {
3035 if (perf_counter_comm_match(counter
))
3036 perf_counter_comm_output(counter
, comm_event
);
3041 static void perf_counter_comm_event(struct perf_comm_event
*comm_event
)
3043 struct perf_cpu_context
*cpuctx
;
3044 struct perf_counter_context
*ctx
;
3046 char comm
[TASK_COMM_LEN
];
3048 memset(comm
, 0, sizeof(comm
));
3049 strncpy(comm
, comm_event
->task
->comm
, sizeof(comm
));
3050 size
= ALIGN(strlen(comm
)+1, sizeof(u64
));
3052 comm_event
->comm
= comm
;
3053 comm_event
->comm_size
= size
;
3055 comm_event
->event
.header
.size
= sizeof(comm_event
->event
) + size
;
3057 cpuctx
= &get_cpu_var(perf_cpu_context
);
3058 perf_counter_comm_ctx(&cpuctx
->ctx
, comm_event
);
3059 put_cpu_var(perf_cpu_context
);
3063 * doesn't really matter which of the child contexts the
3064 * events ends up in.
3066 ctx
= rcu_dereference(current
->perf_counter_ctxp
);
3068 perf_counter_comm_ctx(ctx
, comm_event
);
3072 void perf_counter_comm(struct task_struct
*task
)
3074 struct perf_comm_event comm_event
;
3076 if (task
->perf_counter_ctxp
)
3077 perf_counter_enable_on_exec(task
);
3079 if (!atomic_read(&nr_comm_counters
))
3082 comm_event
= (struct perf_comm_event
){
3088 .type
= PERF_EVENT_COMM
,
3097 perf_counter_comm_event(&comm_event
);
3104 struct perf_mmap_event
{
3105 struct vm_area_struct
*vma
;
3107 const char *file_name
;
3111 struct perf_event_header header
;
3121 static void perf_counter_mmap_output(struct perf_counter
*counter
,
3122 struct perf_mmap_event
*mmap_event
)
3124 struct perf_output_handle handle
;
3125 int size
= mmap_event
->event
.header
.size
;
3126 int ret
= perf_output_begin(&handle
, counter
, size
, 0, 0);
3131 mmap_event
->event
.pid
= perf_counter_pid(counter
, current
);
3132 mmap_event
->event
.tid
= perf_counter_tid(counter
, current
);
3134 perf_output_put(&handle
, mmap_event
->event
);
3135 perf_output_copy(&handle
, mmap_event
->file_name
,
3136 mmap_event
->file_size
);
3137 perf_output_end(&handle
);
3140 static int perf_counter_mmap_match(struct perf_counter
*counter
,
3141 struct perf_mmap_event
*mmap_event
)
3143 if (counter
->attr
.mmap
)
3149 static void perf_counter_mmap_ctx(struct perf_counter_context
*ctx
,
3150 struct perf_mmap_event
*mmap_event
)
3152 struct perf_counter
*counter
;
3154 if (system_state
!= SYSTEM_RUNNING
|| list_empty(&ctx
->event_list
))
3158 list_for_each_entry_rcu(counter
, &ctx
->event_list
, event_entry
) {
3159 if (perf_counter_mmap_match(counter
, mmap_event
))
3160 perf_counter_mmap_output(counter
, mmap_event
);
3165 static void perf_counter_mmap_event(struct perf_mmap_event
*mmap_event
)
3167 struct perf_cpu_context
*cpuctx
;
3168 struct perf_counter_context
*ctx
;
3169 struct vm_area_struct
*vma
= mmap_event
->vma
;
3170 struct file
*file
= vma
->vm_file
;
3176 memset(tmp
, 0, sizeof(tmp
));
3180 * d_path works from the end of the buffer backwards, so we
3181 * need to add enough zero bytes after the string to handle
3182 * the 64bit alignment we do later.
3184 buf
= kzalloc(PATH_MAX
+ sizeof(u64
), GFP_KERNEL
);
3186 name
= strncpy(tmp
, "//enomem", sizeof(tmp
));
3189 name
= d_path(&file
->f_path
, buf
, PATH_MAX
);
3191 name
= strncpy(tmp
, "//toolong", sizeof(tmp
));
3195 if (arch_vma_name(mmap_event
->vma
)) {
3196 name
= strncpy(tmp
, arch_vma_name(mmap_event
->vma
),
3202 name
= strncpy(tmp
, "[vdso]", sizeof(tmp
));
3206 name
= strncpy(tmp
, "//anon", sizeof(tmp
));
3211 size
= ALIGN(strlen(name
)+1, sizeof(u64
));
3213 mmap_event
->file_name
= name
;
3214 mmap_event
->file_size
= size
;
3216 mmap_event
->event
.header
.size
= sizeof(mmap_event
->event
) + size
;
3218 cpuctx
= &get_cpu_var(perf_cpu_context
);
3219 perf_counter_mmap_ctx(&cpuctx
->ctx
, mmap_event
);
3220 put_cpu_var(perf_cpu_context
);
3224 * doesn't really matter which of the child contexts the
3225 * events ends up in.
3227 ctx
= rcu_dereference(current
->perf_counter_ctxp
);
3229 perf_counter_mmap_ctx(ctx
, mmap_event
);
3235 void __perf_counter_mmap(struct vm_area_struct
*vma
)
3237 struct perf_mmap_event mmap_event
;
3239 if (!atomic_read(&nr_mmap_counters
))
3242 mmap_event
= (struct perf_mmap_event
){
3248 .type
= PERF_EVENT_MMAP
,
3254 .start
= vma
->vm_start
,
3255 .len
= vma
->vm_end
- vma
->vm_start
,
3256 .pgoff
= vma
->vm_pgoff
,
3260 perf_counter_mmap_event(&mmap_event
);
3264 * IRQ throttle logging
3267 static void perf_log_throttle(struct perf_counter
*counter
, int enable
)
3269 struct perf_output_handle handle
;
3273 struct perf_event_header header
;
3277 } throttle_event
= {
3279 .type
= PERF_EVENT_THROTTLE
,
3281 .size
= sizeof(throttle_event
),
3283 .time
= sched_clock(),
3284 .id
= primary_counter_id(counter
),
3285 .stream_id
= counter
->id
,
3289 throttle_event
.header
.type
= PERF_EVENT_UNTHROTTLE
;
3291 ret
= perf_output_begin(&handle
, counter
, sizeof(throttle_event
), 1, 0);
3295 perf_output_put(&handle
, throttle_event
);
3296 perf_output_end(&handle
);
3300 * Generic counter overflow handling, sampling.
3303 int perf_counter_overflow(struct perf_counter
*counter
, int nmi
,
3304 struct perf_sample_data
*data
)
3306 int events
= atomic_read(&counter
->event_limit
);
3307 int throttle
= counter
->pmu
->unthrottle
!= NULL
;
3308 struct hw_perf_counter
*hwc
= &counter
->hw
;
3314 if (hwc
->interrupts
!= MAX_INTERRUPTS
) {
3316 if (HZ
* hwc
->interrupts
>
3317 (u64
)sysctl_perf_counter_sample_rate
) {
3318 hwc
->interrupts
= MAX_INTERRUPTS
;
3319 perf_log_throttle(counter
, 0);
3324 * Keep re-disabling counters even though on the previous
3325 * pass we disabled it - just in case we raced with a
3326 * sched-in and the counter got enabled again:
3332 if (counter
->attr
.freq
) {
3333 u64 now
= sched_clock();
3334 s64 delta
= now
- hwc
->freq_stamp
;
3336 hwc
->freq_stamp
= now
;
3338 if (delta
> 0 && delta
< TICK_NSEC
)
3339 perf_adjust_period(counter
, NSEC_PER_SEC
/ (int)delta
);
3343 * XXX event_limit might not quite work as expected on inherited
3347 counter
->pending_kill
= POLL_IN
;
3348 if (events
&& atomic_dec_and_test(&counter
->event_limit
)) {
3350 counter
->pending_kill
= POLL_HUP
;
3352 counter
->pending_disable
= 1;
3353 perf_pending_queue(&counter
->pending
,
3354 perf_pending_counter
);
3356 perf_counter_disable(counter
);
3359 perf_counter_output(counter
, nmi
, data
);
3364 * Generic software counter infrastructure
3368 * We directly increment counter->count and keep a second value in
3369 * counter->hw.period_left to count intervals. This period counter
3370 * is kept in the range [-sample_period, 0] so that we can use the
3374 static u64
perf_swcounter_set_period(struct perf_counter
*counter
)
3376 struct hw_perf_counter
*hwc
= &counter
->hw
;
3377 u64 period
= hwc
->last_period
;
3381 hwc
->last_period
= hwc
->sample_period
;
3384 old
= val
= atomic64_read(&hwc
->period_left
);
3388 nr
= div64_u64(period
+ val
, period
);
3389 offset
= nr
* period
;
3391 if (atomic64_cmpxchg(&hwc
->period_left
, old
, val
) != old
)
3397 static void perf_swcounter_overflow(struct perf_counter
*counter
,
3398 int nmi
, struct perf_sample_data
*data
)
3400 struct hw_perf_counter
*hwc
= &counter
->hw
;
3403 data
->period
= counter
->hw
.last_period
;
3404 overflow
= perf_swcounter_set_period(counter
);
3406 if (hwc
->interrupts
== MAX_INTERRUPTS
)
3409 for (; overflow
; overflow
--) {
3410 if (perf_counter_overflow(counter
, nmi
, data
)) {
3412 * We inhibit the overflow from happening when
3413 * hwc->interrupts == MAX_INTERRUPTS.
3420 static void perf_swcounter_unthrottle(struct perf_counter
*counter
)
3423 * Nothing to do, we already reset hwc->interrupts.
3427 static void perf_swcounter_add(struct perf_counter
*counter
, u64 nr
,
3428 int nmi
, struct perf_sample_data
*data
)
3430 struct hw_perf_counter
*hwc
= &counter
->hw
;
3432 atomic64_add(nr
, &counter
->count
);
3434 if (!hwc
->sample_period
)
3440 if (!atomic64_add_negative(nr
, &hwc
->period_left
))
3441 perf_swcounter_overflow(counter
, nmi
, data
);
3444 static int perf_swcounter_is_counting(struct perf_counter
*counter
)
3446 struct perf_counter_context
*ctx
;
3447 unsigned long flags
;
3450 if (counter
->state
== PERF_COUNTER_STATE_ACTIVE
)
3453 if (counter
->state
!= PERF_COUNTER_STATE_INACTIVE
)
3457 * If the counter is inactive, it could be just because
3458 * its task is scheduled out, or because it's in a group
3459 * which could not go on the PMU. We want to count in
3460 * the first case but not the second. If the context is
3461 * currently active then an inactive software counter must
3462 * be the second case. If it's not currently active then
3463 * we need to know whether the counter was active when the
3464 * context was last active, which we can determine by
3465 * comparing counter->tstamp_stopped with ctx->time.
3467 * We are within an RCU read-side critical section,
3468 * which protects the existence of *ctx.
3471 spin_lock_irqsave(&ctx
->lock
, flags
);
3473 /* Re-check state now we have the lock */
3474 if (counter
->state
< PERF_COUNTER_STATE_INACTIVE
||
3475 counter
->ctx
->is_active
||
3476 counter
->tstamp_stopped
< ctx
->time
)
3478 spin_unlock_irqrestore(&ctx
->lock
, flags
);
3482 static int perf_swcounter_match(struct perf_counter
*counter
,
3483 enum perf_type_id type
,
3484 u32 event
, struct pt_regs
*regs
)
3486 if (!perf_swcounter_is_counting(counter
))
3489 if (counter
->attr
.type
!= type
)
3491 if (counter
->attr
.config
!= event
)
3495 if (counter
->attr
.exclude_user
&& user_mode(regs
))
3498 if (counter
->attr
.exclude_kernel
&& !user_mode(regs
))
3505 static void perf_swcounter_ctx_event(struct perf_counter_context
*ctx
,
3506 enum perf_type_id type
,
3507 u32 event
, u64 nr
, int nmi
,
3508 struct perf_sample_data
*data
)
3510 struct perf_counter
*counter
;
3512 if (system_state
!= SYSTEM_RUNNING
|| list_empty(&ctx
->event_list
))
3516 list_for_each_entry_rcu(counter
, &ctx
->event_list
, event_entry
) {
3517 if (perf_swcounter_match(counter
, type
, event
, data
->regs
))
3518 perf_swcounter_add(counter
, nr
, nmi
, data
);
3523 static int *perf_swcounter_recursion_context(struct perf_cpu_context
*cpuctx
)
3526 return &cpuctx
->recursion
[3];
3529 return &cpuctx
->recursion
[2];
3532 return &cpuctx
->recursion
[1];
3534 return &cpuctx
->recursion
[0];
3537 static void do_perf_swcounter_event(enum perf_type_id type
, u32 event
,
3539 struct perf_sample_data
*data
)
3541 struct perf_cpu_context
*cpuctx
= &get_cpu_var(perf_cpu_context
);
3542 int *recursion
= perf_swcounter_recursion_context(cpuctx
);
3543 struct perf_counter_context
*ctx
;
3551 perf_swcounter_ctx_event(&cpuctx
->ctx
, type
, event
,
3555 * doesn't really matter which of the child contexts the
3556 * events ends up in.
3558 ctx
= rcu_dereference(current
->perf_counter_ctxp
);
3560 perf_swcounter_ctx_event(ctx
, type
, event
, nr
, nmi
, data
);
3567 put_cpu_var(perf_cpu_context
);
3570 void __perf_swcounter_event(u32 event
, u64 nr
, int nmi
,
3571 struct pt_regs
*regs
, u64 addr
)
3573 struct perf_sample_data data
= {
3578 do_perf_swcounter_event(PERF_TYPE_SOFTWARE
, event
, nr
, nmi
, &data
);
3581 static void perf_swcounter_read(struct perf_counter
*counter
)
3585 static int perf_swcounter_enable(struct perf_counter
*counter
)
3587 struct hw_perf_counter
*hwc
= &counter
->hw
;
3589 if (hwc
->sample_period
) {
3590 hwc
->last_period
= hwc
->sample_period
;
3591 perf_swcounter_set_period(counter
);
3596 static void perf_swcounter_disable(struct perf_counter
*counter
)
3600 static const struct pmu perf_ops_generic
= {
3601 .enable
= perf_swcounter_enable
,
3602 .disable
= perf_swcounter_disable
,
3603 .read
= perf_swcounter_read
,
3604 .unthrottle
= perf_swcounter_unthrottle
,
3608 * hrtimer based swcounter callback
3611 static enum hrtimer_restart
perf_swcounter_hrtimer(struct hrtimer
*hrtimer
)
3613 enum hrtimer_restart ret
= HRTIMER_RESTART
;
3614 struct perf_sample_data data
;
3615 struct perf_counter
*counter
;
3618 counter
= container_of(hrtimer
, struct perf_counter
, hw
.hrtimer
);
3619 counter
->pmu
->read(counter
);
3622 data
.regs
= get_irq_regs();
3624 * In case we exclude kernel IPs or are somehow not in interrupt
3625 * context, provide the next best thing, the user IP.
3627 if ((counter
->attr
.exclude_kernel
|| !data
.regs
) &&
3628 !counter
->attr
.exclude_user
)
3629 data
.regs
= task_pt_regs(current
);
3632 if (perf_counter_overflow(counter
, 0, &data
))
3633 ret
= HRTIMER_NORESTART
;
3636 period
= max_t(u64
, 10000, counter
->hw
.sample_period
);
3637 hrtimer_forward_now(hrtimer
, ns_to_ktime(period
));
3643 * Software counter: cpu wall time clock
3646 static void cpu_clock_perf_counter_update(struct perf_counter
*counter
)
3648 int cpu
= raw_smp_processor_id();
3652 now
= cpu_clock(cpu
);
3653 prev
= atomic64_read(&counter
->hw
.prev_count
);
3654 atomic64_set(&counter
->hw
.prev_count
, now
);
3655 atomic64_add(now
- prev
, &counter
->count
);
3658 static int cpu_clock_perf_counter_enable(struct perf_counter
*counter
)
3660 struct hw_perf_counter
*hwc
= &counter
->hw
;
3661 int cpu
= raw_smp_processor_id();
3663 atomic64_set(&hwc
->prev_count
, cpu_clock(cpu
));
3664 hrtimer_init(&hwc
->hrtimer
, CLOCK_MONOTONIC
, HRTIMER_MODE_REL
);
3665 hwc
->hrtimer
.function
= perf_swcounter_hrtimer
;
3666 if (hwc
->sample_period
) {
3667 u64 period
= max_t(u64
, 10000, hwc
->sample_period
);
3668 __hrtimer_start_range_ns(&hwc
->hrtimer
,
3669 ns_to_ktime(period
), 0,
3670 HRTIMER_MODE_REL
, 0);
3676 static void cpu_clock_perf_counter_disable(struct perf_counter
*counter
)
3678 if (counter
->hw
.sample_period
)
3679 hrtimer_cancel(&counter
->hw
.hrtimer
);
3680 cpu_clock_perf_counter_update(counter
);
3683 static void cpu_clock_perf_counter_read(struct perf_counter
*counter
)
3685 cpu_clock_perf_counter_update(counter
);
3688 static const struct pmu perf_ops_cpu_clock
= {
3689 .enable
= cpu_clock_perf_counter_enable
,
3690 .disable
= cpu_clock_perf_counter_disable
,
3691 .read
= cpu_clock_perf_counter_read
,
3695 * Software counter: task time clock
3698 static void task_clock_perf_counter_update(struct perf_counter
*counter
, u64 now
)
3703 prev
= atomic64_xchg(&counter
->hw
.prev_count
, now
);
3705 atomic64_add(delta
, &counter
->count
);
3708 static int task_clock_perf_counter_enable(struct perf_counter
*counter
)
3710 struct hw_perf_counter
*hwc
= &counter
->hw
;
3713 now
= counter
->ctx
->time
;
3715 atomic64_set(&hwc
->prev_count
, now
);
3716 hrtimer_init(&hwc
->hrtimer
, CLOCK_MONOTONIC
, HRTIMER_MODE_REL
);
3717 hwc
->hrtimer
.function
= perf_swcounter_hrtimer
;
3718 if (hwc
->sample_period
) {
3719 u64 period
= max_t(u64
, 10000, hwc
->sample_period
);
3720 __hrtimer_start_range_ns(&hwc
->hrtimer
,
3721 ns_to_ktime(period
), 0,
3722 HRTIMER_MODE_REL
, 0);
3728 static void task_clock_perf_counter_disable(struct perf_counter
*counter
)
3730 if (counter
->hw
.sample_period
)
3731 hrtimer_cancel(&counter
->hw
.hrtimer
);
3732 task_clock_perf_counter_update(counter
, counter
->ctx
->time
);
3736 static void task_clock_perf_counter_read(struct perf_counter
*counter
)
3741 update_context_time(counter
->ctx
);
3742 time
= counter
->ctx
->time
;
3744 u64 now
= perf_clock();
3745 u64 delta
= now
- counter
->ctx
->timestamp
;
3746 time
= counter
->ctx
->time
+ delta
;
3749 task_clock_perf_counter_update(counter
, time
);
3752 static const struct pmu perf_ops_task_clock
= {
3753 .enable
= task_clock_perf_counter_enable
,
3754 .disable
= task_clock_perf_counter_disable
,
3755 .read
= task_clock_perf_counter_read
,
3758 #ifdef CONFIG_EVENT_PROFILE
3759 void perf_tpcounter_event(int event_id
, u64 addr
, u64 count
, void *record
,
3762 struct perf_raw_record raw
= {
3767 struct perf_sample_data data
= {
3768 .regs
= get_irq_regs(),
3774 data
.regs
= task_pt_regs(current
);
3776 do_perf_swcounter_event(PERF_TYPE_TRACEPOINT
, event_id
, count
, 1, &data
);
3778 EXPORT_SYMBOL_GPL(perf_tpcounter_event
);
3780 extern int ftrace_profile_enable(int);
3781 extern void ftrace_profile_disable(int);
3783 static void tp_perf_counter_destroy(struct perf_counter
*counter
)
3785 ftrace_profile_disable(counter
->attr
.config
);
3788 static const struct pmu
*tp_perf_counter_init(struct perf_counter
*counter
)
3791 * Raw tracepoint data is a severe data leak, only allow root to
3794 if ((counter
->attr
.sample_type
& PERF_SAMPLE_RAW
) &&
3795 !capable(CAP_SYS_ADMIN
))
3796 return ERR_PTR(-EPERM
);
3798 if (ftrace_profile_enable(counter
->attr
.config
))
3801 counter
->destroy
= tp_perf_counter_destroy
;
3803 return &perf_ops_generic
;
3806 static const struct pmu
*tp_perf_counter_init(struct perf_counter
*counter
)
3812 atomic_t perf_swcounter_enabled
[PERF_COUNT_SW_MAX
];
3814 static void sw_perf_counter_destroy(struct perf_counter
*counter
)
3816 u64 event
= counter
->attr
.config
;
3818 WARN_ON(counter
->parent
);
3820 atomic_dec(&perf_swcounter_enabled
[event
]);
3823 static const struct pmu
*sw_perf_counter_init(struct perf_counter
*counter
)
3825 const struct pmu
*pmu
= NULL
;
3826 u64 event
= counter
->attr
.config
;
3829 * Software counters (currently) can't in general distinguish
3830 * between user, kernel and hypervisor events.
3831 * However, context switches and cpu migrations are considered
3832 * to be kernel events, and page faults are never hypervisor
3836 case PERF_COUNT_SW_CPU_CLOCK
:
3837 pmu
= &perf_ops_cpu_clock
;
3840 case PERF_COUNT_SW_TASK_CLOCK
:
3842 * If the user instantiates this as a per-cpu counter,
3843 * use the cpu_clock counter instead.
3845 if (counter
->ctx
->task
)
3846 pmu
= &perf_ops_task_clock
;
3848 pmu
= &perf_ops_cpu_clock
;
3851 case PERF_COUNT_SW_PAGE_FAULTS
:
3852 case PERF_COUNT_SW_PAGE_FAULTS_MIN
:
3853 case PERF_COUNT_SW_PAGE_FAULTS_MAJ
:
3854 case PERF_COUNT_SW_CONTEXT_SWITCHES
:
3855 case PERF_COUNT_SW_CPU_MIGRATIONS
:
3856 if (!counter
->parent
) {
3857 atomic_inc(&perf_swcounter_enabled
[event
]);
3858 counter
->destroy
= sw_perf_counter_destroy
;
3860 pmu
= &perf_ops_generic
;
3868 * Allocate and initialize a counter structure
3870 static struct perf_counter
*
3871 perf_counter_alloc(struct perf_counter_attr
*attr
,
3873 struct perf_counter_context
*ctx
,
3874 struct perf_counter
*group_leader
,
3875 struct perf_counter
*parent_counter
,
3878 const struct pmu
*pmu
;
3879 struct perf_counter
*counter
;
3880 struct hw_perf_counter
*hwc
;
3883 counter
= kzalloc(sizeof(*counter
), gfpflags
);
3885 return ERR_PTR(-ENOMEM
);
3888 * Single counters are their own group leaders, with an
3889 * empty sibling list:
3892 group_leader
= counter
;
3894 mutex_init(&counter
->child_mutex
);
3895 INIT_LIST_HEAD(&counter
->child_list
);
3897 INIT_LIST_HEAD(&counter
->list_entry
);
3898 INIT_LIST_HEAD(&counter
->event_entry
);
3899 INIT_LIST_HEAD(&counter
->sibling_list
);
3900 init_waitqueue_head(&counter
->waitq
);
3902 mutex_init(&counter
->mmap_mutex
);
3905 counter
->attr
= *attr
;
3906 counter
->group_leader
= group_leader
;
3907 counter
->pmu
= NULL
;
3909 counter
->oncpu
= -1;
3911 counter
->parent
= parent_counter
;
3913 counter
->ns
= get_pid_ns(current
->nsproxy
->pid_ns
);
3914 counter
->id
= atomic64_inc_return(&perf_counter_id
);
3916 counter
->state
= PERF_COUNTER_STATE_INACTIVE
;
3919 counter
->state
= PERF_COUNTER_STATE_OFF
;
3924 hwc
->sample_period
= attr
->sample_period
;
3925 if (attr
->freq
&& attr
->sample_freq
)
3926 hwc
->sample_period
= 1;
3928 atomic64_set(&hwc
->period_left
, hwc
->sample_period
);
3931 * we currently do not support PERF_SAMPLE_GROUP on inherited counters
3933 if (attr
->inherit
&& (attr
->sample_type
& PERF_SAMPLE_GROUP
))
3936 switch (attr
->type
) {
3938 case PERF_TYPE_HARDWARE
:
3939 case PERF_TYPE_HW_CACHE
:
3940 pmu
= hw_perf_counter_init(counter
);
3943 case PERF_TYPE_SOFTWARE
:
3944 pmu
= sw_perf_counter_init(counter
);
3947 case PERF_TYPE_TRACEPOINT
:
3948 pmu
= tp_perf_counter_init(counter
);
3958 else if (IS_ERR(pmu
))
3963 put_pid_ns(counter
->ns
);
3965 return ERR_PTR(err
);
3970 if (!counter
->parent
) {
3971 atomic_inc(&nr_counters
);
3972 if (counter
->attr
.mmap
)
3973 atomic_inc(&nr_mmap_counters
);
3974 if (counter
->attr
.comm
)
3975 atomic_inc(&nr_comm_counters
);
3976 if (counter
->attr
.task
)
3977 atomic_inc(&nr_task_counters
);
3983 static int perf_copy_attr(struct perf_counter_attr __user
*uattr
,
3984 struct perf_counter_attr
*attr
)
3989 if (!access_ok(VERIFY_WRITE
, uattr
, PERF_ATTR_SIZE_VER0
))
3993 * zero the full structure, so that a short copy will be nice.
3995 memset(attr
, 0, sizeof(*attr
));
3997 ret
= get_user(size
, &uattr
->size
);
4001 if (size
> PAGE_SIZE
) /* silly large */
4004 if (!size
) /* abi compat */
4005 size
= PERF_ATTR_SIZE_VER0
;
4007 if (size
< PERF_ATTR_SIZE_VER0
)
4011 * If we're handed a bigger struct than we know of,
4012 * ensure all the unknown bits are 0.
4014 if (size
> sizeof(*attr
)) {
4016 unsigned long __user
*addr
;
4017 unsigned long __user
*end
;
4019 addr
= PTR_ALIGN((void __user
*)uattr
+ sizeof(*attr
),
4020 sizeof(unsigned long));
4021 end
= PTR_ALIGN((void __user
*)uattr
+ size
,
4022 sizeof(unsigned long));
4024 for (; addr
< end
; addr
+= sizeof(unsigned long)) {
4025 ret
= get_user(val
, addr
);
4033 ret
= copy_from_user(attr
, uattr
, size
);
4038 * If the type exists, the corresponding creation will verify
4041 if (attr
->type
>= PERF_TYPE_MAX
)
4044 if (attr
->__reserved_1
|| attr
->__reserved_2
|| attr
->__reserved_3
)
4047 if (attr
->sample_type
& ~(PERF_SAMPLE_MAX
-1))
4050 if (attr
->read_format
& ~(PERF_FORMAT_MAX
-1))
4057 put_user(sizeof(*attr
), &uattr
->size
);
4063 * sys_perf_counter_open - open a performance counter, associate it to a task/cpu
4065 * @attr_uptr: event type attributes for monitoring/sampling
4068 * @group_fd: group leader counter fd
4070 SYSCALL_DEFINE5(perf_counter_open
,
4071 struct perf_counter_attr __user
*, attr_uptr
,
4072 pid_t
, pid
, int, cpu
, int, group_fd
, unsigned long, flags
)
4074 struct perf_counter
*counter
, *group_leader
;
4075 struct perf_counter_attr attr
;
4076 struct perf_counter_context
*ctx
;
4077 struct file
*counter_file
= NULL
;
4078 struct file
*group_file
= NULL
;
4079 int fput_needed
= 0;
4080 int fput_needed2
= 0;
4083 /* for future expandability... */
4087 ret
= perf_copy_attr(attr_uptr
, &attr
);
4091 if (!attr
.exclude_kernel
) {
4092 if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN
))
4097 if (attr
.sample_freq
> sysctl_perf_counter_sample_rate
)
4102 * Get the target context (task or percpu):
4104 ctx
= find_get_context(pid
, cpu
);
4106 return PTR_ERR(ctx
);
4109 * Look up the group leader (we will attach this counter to it):
4111 group_leader
= NULL
;
4112 if (group_fd
!= -1) {
4114 group_file
= fget_light(group_fd
, &fput_needed
);
4116 goto err_put_context
;
4117 if (group_file
->f_op
!= &perf_fops
)
4118 goto err_put_context
;
4120 group_leader
= group_file
->private_data
;
4122 * Do not allow a recursive hierarchy (this new sibling
4123 * becoming part of another group-sibling):
4125 if (group_leader
->group_leader
!= group_leader
)
4126 goto err_put_context
;
4128 * Do not allow to attach to a group in a different
4129 * task or CPU context:
4131 if (group_leader
->ctx
!= ctx
)
4132 goto err_put_context
;
4134 * Only a group leader can be exclusive or pinned
4136 if (attr
.exclusive
|| attr
.pinned
)
4137 goto err_put_context
;
4140 counter
= perf_counter_alloc(&attr
, cpu
, ctx
, group_leader
,
4142 ret
= PTR_ERR(counter
);
4143 if (IS_ERR(counter
))
4144 goto err_put_context
;
4146 ret
= anon_inode_getfd("[perf_counter]", &perf_fops
, counter
, 0);
4148 goto err_free_put_context
;
4150 counter_file
= fget_light(ret
, &fput_needed2
);
4152 goto err_free_put_context
;
4154 counter
->filp
= counter_file
;
4155 WARN_ON_ONCE(ctx
->parent_ctx
);
4156 mutex_lock(&ctx
->mutex
);
4157 perf_install_in_context(ctx
, counter
, cpu
);
4159 mutex_unlock(&ctx
->mutex
);
4161 counter
->owner
= current
;
4162 get_task_struct(current
);
4163 mutex_lock(¤t
->perf_counter_mutex
);
4164 list_add_tail(&counter
->owner_entry
, ¤t
->perf_counter_list
);
4165 mutex_unlock(¤t
->perf_counter_mutex
);
4167 fput_light(counter_file
, fput_needed2
);
4170 fput_light(group_file
, fput_needed
);
4174 err_free_put_context
:
4184 * inherit a counter from parent task to child task:
4186 static struct perf_counter
*
4187 inherit_counter(struct perf_counter
*parent_counter
,
4188 struct task_struct
*parent
,
4189 struct perf_counter_context
*parent_ctx
,
4190 struct task_struct
*child
,
4191 struct perf_counter
*group_leader
,
4192 struct perf_counter_context
*child_ctx
)
4194 struct perf_counter
*child_counter
;
4197 * Instead of creating recursive hierarchies of counters,
4198 * we link inherited counters back to the original parent,
4199 * which has a filp for sure, which we use as the reference
4202 if (parent_counter
->parent
)
4203 parent_counter
= parent_counter
->parent
;
4205 child_counter
= perf_counter_alloc(&parent_counter
->attr
,
4206 parent_counter
->cpu
, child_ctx
,
4207 group_leader
, parent_counter
,
4209 if (IS_ERR(child_counter
))
4210 return child_counter
;
4214 * Make the child state follow the state of the parent counter,
4215 * not its attr.disabled bit. We hold the parent's mutex,
4216 * so we won't race with perf_counter_{en, dis}able_family.
4218 if (parent_counter
->state
>= PERF_COUNTER_STATE_INACTIVE
)
4219 child_counter
->state
= PERF_COUNTER_STATE_INACTIVE
;
4221 child_counter
->state
= PERF_COUNTER_STATE_OFF
;
4223 if (parent_counter
->attr
.freq
)
4224 child_counter
->hw
.sample_period
= parent_counter
->hw
.sample_period
;
4227 * Link it up in the child's context:
4229 add_counter_to_ctx(child_counter
, child_ctx
);
4232 * Get a reference to the parent filp - we will fput it
4233 * when the child counter exits. This is safe to do because
4234 * we are in the parent and we know that the filp still
4235 * exists and has a nonzero count:
4237 atomic_long_inc(&parent_counter
->filp
->f_count
);
4240 * Link this into the parent counter's child list
4242 WARN_ON_ONCE(parent_counter
->ctx
->parent_ctx
);
4243 mutex_lock(&parent_counter
->child_mutex
);
4244 list_add_tail(&child_counter
->child_list
, &parent_counter
->child_list
);
4245 mutex_unlock(&parent_counter
->child_mutex
);
4247 return child_counter
;
4250 static int inherit_group(struct perf_counter
*parent_counter
,
4251 struct task_struct
*parent
,
4252 struct perf_counter_context
*parent_ctx
,
4253 struct task_struct
*child
,
4254 struct perf_counter_context
*child_ctx
)
4256 struct perf_counter
*leader
;
4257 struct perf_counter
*sub
;
4258 struct perf_counter
*child_ctr
;
4260 leader
= inherit_counter(parent_counter
, parent
, parent_ctx
,
4261 child
, NULL
, child_ctx
);
4263 return PTR_ERR(leader
);
4264 list_for_each_entry(sub
, &parent_counter
->sibling_list
, list_entry
) {
4265 child_ctr
= inherit_counter(sub
, parent
, parent_ctx
,
4266 child
, leader
, child_ctx
);
4267 if (IS_ERR(child_ctr
))
4268 return PTR_ERR(child_ctr
);
4273 static void sync_child_counter(struct perf_counter
*child_counter
,
4274 struct task_struct
*child
)
4276 struct perf_counter
*parent_counter
= child_counter
->parent
;
4279 if (child_counter
->attr
.inherit_stat
)
4280 perf_counter_read_event(child_counter
, child
);
4282 child_val
= atomic64_read(&child_counter
->count
);
4285 * Add back the child's count to the parent's count:
4287 atomic64_add(child_val
, &parent_counter
->count
);
4288 atomic64_add(child_counter
->total_time_enabled
,
4289 &parent_counter
->child_total_time_enabled
);
4290 atomic64_add(child_counter
->total_time_running
,
4291 &parent_counter
->child_total_time_running
);
4294 * Remove this counter from the parent's list
4296 WARN_ON_ONCE(parent_counter
->ctx
->parent_ctx
);
4297 mutex_lock(&parent_counter
->child_mutex
);
4298 list_del_init(&child_counter
->child_list
);
4299 mutex_unlock(&parent_counter
->child_mutex
);
4302 * Release the parent counter, if this was the last
4305 fput(parent_counter
->filp
);
4309 __perf_counter_exit_task(struct perf_counter
*child_counter
,
4310 struct perf_counter_context
*child_ctx
,
4311 struct task_struct
*child
)
4313 struct perf_counter
*parent_counter
;
4315 update_counter_times(child_counter
);
4316 perf_counter_remove_from_context(child_counter
);
4318 parent_counter
= child_counter
->parent
;
4320 * It can happen that parent exits first, and has counters
4321 * that are still around due to the child reference. These
4322 * counters need to be zapped - but otherwise linger.
4324 if (parent_counter
) {
4325 sync_child_counter(child_counter
, child
);
4326 free_counter(child_counter
);
4331 * When a child task exits, feed back counter values to parent counters.
4333 void perf_counter_exit_task(struct task_struct
*child
)
4335 struct perf_counter
*child_counter
, *tmp
;
4336 struct perf_counter_context
*child_ctx
;
4337 unsigned long flags
;
4339 if (likely(!child
->perf_counter_ctxp
)) {
4340 perf_counter_task(child
, NULL
, 0);
4344 local_irq_save(flags
);
4346 * We can't reschedule here because interrupts are disabled,
4347 * and either child is current or it is a task that can't be
4348 * scheduled, so we are now safe from rescheduling changing
4351 child_ctx
= child
->perf_counter_ctxp
;
4352 __perf_counter_task_sched_out(child_ctx
);
4355 * Take the context lock here so that if find_get_context is
4356 * reading child->perf_counter_ctxp, we wait until it has
4357 * incremented the context's refcount before we do put_ctx below.
4359 spin_lock(&child_ctx
->lock
);
4360 child
->perf_counter_ctxp
= NULL
;
4362 * If this context is a clone; unclone it so it can't get
4363 * swapped to another process while we're removing all
4364 * the counters from it.
4366 unclone_ctx(child_ctx
);
4367 spin_unlock_irqrestore(&child_ctx
->lock
, flags
);
4370 * Report the task dead after unscheduling the counters so that we
4371 * won't get any samples after PERF_EVENT_EXIT. We can however still
4372 * get a few PERF_EVENT_READ events.
4374 perf_counter_task(child
, child_ctx
, 0);
4377 * We can recurse on the same lock type through:
4379 * __perf_counter_exit_task()
4380 * sync_child_counter()
4381 * fput(parent_counter->filp)
4383 * mutex_lock(&ctx->mutex)
4385 * But since its the parent context it won't be the same instance.
4387 mutex_lock_nested(&child_ctx
->mutex
, SINGLE_DEPTH_NESTING
);
4390 list_for_each_entry_safe(child_counter
, tmp
, &child_ctx
->counter_list
,
4392 __perf_counter_exit_task(child_counter
, child_ctx
, child
);
4395 * If the last counter was a group counter, it will have appended all
4396 * its siblings to the list, but we obtained 'tmp' before that which
4397 * will still point to the list head terminating the iteration.
4399 if (!list_empty(&child_ctx
->counter_list
))
4402 mutex_unlock(&child_ctx
->mutex
);
4408 * free an unexposed, unused context as created by inheritance by
4409 * init_task below, used by fork() in case of fail.
4411 void perf_counter_free_task(struct task_struct
*task
)
4413 struct perf_counter_context
*ctx
= task
->perf_counter_ctxp
;
4414 struct perf_counter
*counter
, *tmp
;
4419 mutex_lock(&ctx
->mutex
);
4421 list_for_each_entry_safe(counter
, tmp
, &ctx
->counter_list
, list_entry
) {
4422 struct perf_counter
*parent
= counter
->parent
;
4424 if (WARN_ON_ONCE(!parent
))
4427 mutex_lock(&parent
->child_mutex
);
4428 list_del_init(&counter
->child_list
);
4429 mutex_unlock(&parent
->child_mutex
);
4433 list_del_counter(counter
, ctx
);
4434 free_counter(counter
);
4437 if (!list_empty(&ctx
->counter_list
))
4440 mutex_unlock(&ctx
->mutex
);
4446 * Initialize the perf_counter context in task_struct
4448 int perf_counter_init_task(struct task_struct
*child
)
4450 struct perf_counter_context
*child_ctx
, *parent_ctx
;
4451 struct perf_counter_context
*cloned_ctx
;
4452 struct perf_counter
*counter
;
4453 struct task_struct
*parent
= current
;
4454 int inherited_all
= 1;
4457 child
->perf_counter_ctxp
= NULL
;
4459 mutex_init(&child
->perf_counter_mutex
);
4460 INIT_LIST_HEAD(&child
->perf_counter_list
);
4462 if (likely(!parent
->perf_counter_ctxp
))
4466 * This is executed from the parent task context, so inherit
4467 * counters that have been marked for cloning.
4468 * First allocate and initialize a context for the child.
4471 child_ctx
= kmalloc(sizeof(struct perf_counter_context
), GFP_KERNEL
);
4475 __perf_counter_init_context(child_ctx
, child
);
4476 child
->perf_counter_ctxp
= child_ctx
;
4477 get_task_struct(child
);
4480 * If the parent's context is a clone, pin it so it won't get
4483 parent_ctx
= perf_pin_task_context(parent
);
4486 * No need to check if parent_ctx != NULL here; since we saw
4487 * it non-NULL earlier, the only reason for it to become NULL
4488 * is if we exit, and since we're currently in the middle of
4489 * a fork we can't be exiting at the same time.
4493 * Lock the parent list. No need to lock the child - not PID
4494 * hashed yet and not running, so nobody can access it.
4496 mutex_lock(&parent_ctx
->mutex
);
4499 * We dont have to disable NMIs - we are only looking at
4500 * the list, not manipulating it:
4502 list_for_each_entry_rcu(counter
, &parent_ctx
->event_list
, event_entry
) {
4503 if (counter
!= counter
->group_leader
)
4506 if (!counter
->attr
.inherit
) {
4511 ret
= inherit_group(counter
, parent
, parent_ctx
,
4519 if (inherited_all
) {
4521 * Mark the child context as a clone of the parent
4522 * context, or of whatever the parent is a clone of.
4523 * Note that if the parent is a clone, it could get
4524 * uncloned at any point, but that doesn't matter
4525 * because the list of counters and the generation
4526 * count can't have changed since we took the mutex.
4528 cloned_ctx
= rcu_dereference(parent_ctx
->parent_ctx
);
4530 child_ctx
->parent_ctx
= cloned_ctx
;
4531 child_ctx
->parent_gen
= parent_ctx
->parent_gen
;
4533 child_ctx
->parent_ctx
= parent_ctx
;
4534 child_ctx
->parent_gen
= parent_ctx
->generation
;
4536 get_ctx(child_ctx
->parent_ctx
);
4539 mutex_unlock(&parent_ctx
->mutex
);
4541 perf_unpin_context(parent_ctx
);
4546 static void __cpuinit
perf_counter_init_cpu(int cpu
)
4548 struct perf_cpu_context
*cpuctx
;
4550 cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
4551 __perf_counter_init_context(&cpuctx
->ctx
, NULL
);
4553 spin_lock(&perf_resource_lock
);
4554 cpuctx
->max_pertask
= perf_max_counters
- perf_reserved_percpu
;
4555 spin_unlock(&perf_resource_lock
);
4557 hw_perf_counter_setup(cpu
);
4560 #ifdef CONFIG_HOTPLUG_CPU
4561 static void __perf_counter_exit_cpu(void *info
)
4563 struct perf_cpu_context
*cpuctx
= &__get_cpu_var(perf_cpu_context
);
4564 struct perf_counter_context
*ctx
= &cpuctx
->ctx
;
4565 struct perf_counter
*counter
, *tmp
;
4567 list_for_each_entry_safe(counter
, tmp
, &ctx
->counter_list
, list_entry
)
4568 __perf_counter_remove_from_context(counter
);
4570 static void perf_counter_exit_cpu(int cpu
)
4572 struct perf_cpu_context
*cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
4573 struct perf_counter_context
*ctx
= &cpuctx
->ctx
;
4575 mutex_lock(&ctx
->mutex
);
4576 smp_call_function_single(cpu
, __perf_counter_exit_cpu
, NULL
, 1);
4577 mutex_unlock(&ctx
->mutex
);
4580 static inline void perf_counter_exit_cpu(int cpu
) { }
4583 static int __cpuinit
4584 perf_cpu_notify(struct notifier_block
*self
, unsigned long action
, void *hcpu
)
4586 unsigned int cpu
= (long)hcpu
;
4590 case CPU_UP_PREPARE
:
4591 case CPU_UP_PREPARE_FROZEN
:
4592 perf_counter_init_cpu(cpu
);
4595 case CPU_DOWN_PREPARE
:
4596 case CPU_DOWN_PREPARE_FROZEN
:
4597 perf_counter_exit_cpu(cpu
);
4608 * This has to have a higher priority than migration_notifier in sched.c.
4610 static struct notifier_block __cpuinitdata perf_cpu_nb
= {
4611 .notifier_call
= perf_cpu_notify
,
4615 void __init
perf_counter_init(void)
4617 perf_cpu_notify(&perf_cpu_nb
, (unsigned long)CPU_UP_PREPARE
,
4618 (void *)(long)smp_processor_id());
4619 register_cpu_notifier(&perf_cpu_nb
);
4622 static ssize_t
perf_show_reserve_percpu(struct sysdev_class
*class, char *buf
)
4624 return sprintf(buf
, "%d\n", perf_reserved_percpu
);
4628 perf_set_reserve_percpu(struct sysdev_class
*class,
4632 struct perf_cpu_context
*cpuctx
;
4636 err
= strict_strtoul(buf
, 10, &val
);
4639 if (val
> perf_max_counters
)
4642 spin_lock(&perf_resource_lock
);
4643 perf_reserved_percpu
= val
;
4644 for_each_online_cpu(cpu
) {
4645 cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
4646 spin_lock_irq(&cpuctx
->ctx
.lock
);
4647 mpt
= min(perf_max_counters
- cpuctx
->ctx
.nr_counters
,
4648 perf_max_counters
- perf_reserved_percpu
);
4649 cpuctx
->max_pertask
= mpt
;
4650 spin_unlock_irq(&cpuctx
->ctx
.lock
);
4652 spin_unlock(&perf_resource_lock
);
4657 static ssize_t
perf_show_overcommit(struct sysdev_class
*class, char *buf
)
4659 return sprintf(buf
, "%d\n", perf_overcommit
);
4663 perf_set_overcommit(struct sysdev_class
*class, const char *buf
, size_t count
)
4668 err
= strict_strtoul(buf
, 10, &val
);
4674 spin_lock(&perf_resource_lock
);
4675 perf_overcommit
= val
;
4676 spin_unlock(&perf_resource_lock
);
4681 static SYSDEV_CLASS_ATTR(
4684 perf_show_reserve_percpu
,
4685 perf_set_reserve_percpu
4688 static SYSDEV_CLASS_ATTR(
4691 perf_show_overcommit
,
4695 static struct attribute
*perfclass_attrs
[] = {
4696 &attr_reserve_percpu
.attr
,
4697 &attr_overcommit
.attr
,
4701 static struct attribute_group perfclass_attr_group
= {
4702 .attrs
= perfclass_attrs
,
4703 .name
= "perf_counters",
4706 static int __init
perf_counter_sysfs_init(void)
4708 return sysfs_create_group(&cpu_sysdev_class
.kset
.kobj
,
4709 &perfclass_attr_group
);
4711 device_initcall(perf_counter_sysfs_init
);