2 * Performance counter core code
4 * Copyright (C) 2008 Thomas Gleixner <tglx@linutronix.de>
5 * Copyright (C) 2008-2009 Red Hat, Inc., Ingo Molnar
6 * Copyright (C) 2008-2009 Red Hat, Inc., Peter Zijlstra <pzijlstr@redhat.com>
7 * Copyright © 2009 Paul Mackerras, IBM Corp. <paulus@au1.ibm.com>
9 * For licensing details see kernel-base/COPYING
14 #include <linux/cpu.h>
15 #include <linux/smp.h>
16 #include <linux/file.h>
17 #include <linux/poll.h>
18 #include <linux/sysfs.h>
19 #include <linux/ptrace.h>
20 #include <linux/percpu.h>
21 #include <linux/vmstat.h>
22 #include <linux/hardirq.h>
23 #include <linux/rculist.h>
24 #include <linux/uaccess.h>
25 #include <linux/syscalls.h>
26 #include <linux/anon_inodes.h>
27 #include <linux/kernel_stat.h>
28 #include <linux/perf_counter.h>
29 #include <linux/dcache.h>
31 #include <asm/irq_regs.h>
34 * Each CPU has a list of per CPU counters:
36 DEFINE_PER_CPU(struct perf_cpu_context
, perf_cpu_context
);
38 int perf_max_counters __read_mostly
= 1;
39 static int perf_reserved_percpu __read_mostly
;
40 static int perf_overcommit __read_mostly
= 1;
42 static atomic_t nr_counters __read_mostly
;
43 static atomic_t nr_mmap_tracking __read_mostly
;
44 static atomic_t nr_munmap_tracking __read_mostly
;
45 static atomic_t nr_comm_tracking __read_mostly
;
47 int sysctl_perf_counter_priv __read_mostly
; /* do we need to be privileged */
48 int sysctl_perf_counter_mlock __read_mostly
= 512; /* 'free' kb per user */
51 * Lock for (sysadmin-configurable) counter reservations:
53 static DEFINE_SPINLOCK(perf_resource_lock
);
56 * Architecture provided APIs - weak aliases:
58 extern __weak
const struct pmu
*hw_perf_counter_init(struct perf_counter
*counter
)
63 void __weak
hw_perf_disable(void) { barrier(); }
64 void __weak
hw_perf_enable(void) { barrier(); }
66 void __weak
hw_perf_counter_setup(int cpu
) { barrier(); }
67 int __weak
hw_perf_group_sched_in(struct perf_counter
*group_leader
,
68 struct perf_cpu_context
*cpuctx
,
69 struct perf_counter_context
*ctx
, int cpu
)
74 void __weak
perf_counter_print_debug(void) { }
76 static DEFINE_PER_CPU(int, disable_count
);
78 void __perf_disable(void)
80 __get_cpu_var(disable_count
)++;
83 bool __perf_enable(void)
85 return !--__get_cpu_var(disable_count
);
88 void perf_disable(void)
94 void perf_enable(void)
100 static void get_ctx(struct perf_counter_context
*ctx
)
102 atomic_inc(&ctx
->refcount
);
105 static void put_ctx(struct perf_counter_context
*ctx
)
107 if (atomic_dec_and_test(&ctx
->refcount
)) {
109 put_ctx(ctx
->parent_ctx
);
115 * Add a counter from the lists for its context.
116 * Must be called with ctx->mutex and ctx->lock held.
119 list_add_counter(struct perf_counter
*counter
, struct perf_counter_context
*ctx
)
121 struct perf_counter
*group_leader
= counter
->group_leader
;
124 * Depending on whether it is a standalone or sibling counter,
125 * add it straight to the context's counter list, or to the group
126 * leader's sibling list:
128 if (group_leader
== counter
)
129 list_add_tail(&counter
->list_entry
, &ctx
->counter_list
);
131 list_add_tail(&counter
->list_entry
, &group_leader
->sibling_list
);
132 group_leader
->nr_siblings
++;
135 list_add_rcu(&counter
->event_entry
, &ctx
->event_list
);
137 if (counter
->state
>= PERF_COUNTER_STATE_INACTIVE
)
142 * Remove a counter from the lists for its context.
143 * Must be called with ctx->mutex and ctx->lock held.
146 list_del_counter(struct perf_counter
*counter
, struct perf_counter_context
*ctx
)
148 struct perf_counter
*sibling
, *tmp
;
150 if (list_empty(&counter
->list_entry
))
153 if (counter
->state
>= PERF_COUNTER_STATE_INACTIVE
)
156 list_del_init(&counter
->list_entry
);
157 list_del_rcu(&counter
->event_entry
);
159 if (counter
->group_leader
!= counter
)
160 counter
->group_leader
->nr_siblings
--;
163 * If this was a group counter with sibling counters then
164 * upgrade the siblings to singleton counters by adding them
165 * to the context list directly:
167 list_for_each_entry_safe(sibling
, tmp
,
168 &counter
->sibling_list
, list_entry
) {
170 list_move_tail(&sibling
->list_entry
, &ctx
->counter_list
);
171 sibling
->group_leader
= sibling
;
176 counter_sched_out(struct perf_counter
*counter
,
177 struct perf_cpu_context
*cpuctx
,
178 struct perf_counter_context
*ctx
)
180 if (counter
->state
!= PERF_COUNTER_STATE_ACTIVE
)
183 counter
->state
= PERF_COUNTER_STATE_INACTIVE
;
184 counter
->tstamp_stopped
= ctx
->time
;
185 counter
->pmu
->disable(counter
);
188 if (!is_software_counter(counter
))
189 cpuctx
->active_oncpu
--;
191 if (counter
->hw_event
.exclusive
|| !cpuctx
->active_oncpu
)
192 cpuctx
->exclusive
= 0;
196 group_sched_out(struct perf_counter
*group_counter
,
197 struct perf_cpu_context
*cpuctx
,
198 struct perf_counter_context
*ctx
)
200 struct perf_counter
*counter
;
202 if (group_counter
->state
!= PERF_COUNTER_STATE_ACTIVE
)
205 counter_sched_out(group_counter
, cpuctx
, ctx
);
208 * Schedule out siblings (if any):
210 list_for_each_entry(counter
, &group_counter
->sibling_list
, list_entry
)
211 counter_sched_out(counter
, cpuctx
, ctx
);
213 if (group_counter
->hw_event
.exclusive
)
214 cpuctx
->exclusive
= 0;
218 * Mark this context as not being a clone of another.
219 * Called when counters are added to or removed from this context.
220 * We also increment our generation number so that anything that
221 * was cloned from this context before this will not match anything
222 * cloned from this context after this.
224 static void unclone_ctx(struct perf_counter_context
*ctx
)
227 if (!ctx
->parent_ctx
)
229 put_ctx(ctx
->parent_ctx
);
230 ctx
->parent_ctx
= NULL
;
234 * Cross CPU call to remove a performance counter
236 * We disable the counter on the hardware level first. After that we
237 * remove it from the context list.
239 static void __perf_counter_remove_from_context(void *info
)
241 struct perf_cpu_context
*cpuctx
= &__get_cpu_var(perf_cpu_context
);
242 struct perf_counter
*counter
= info
;
243 struct perf_counter_context
*ctx
= counter
->ctx
;
247 * If this is a task context, we need to check whether it is
248 * the current task context of this cpu. If not it has been
249 * scheduled out before the smp call arrived.
251 if (ctx
->task
&& cpuctx
->task_ctx
!= ctx
)
254 spin_lock_irqsave(&ctx
->lock
, flags
);
256 * Protect the list operation against NMI by disabling the
257 * counters on a global level.
261 counter_sched_out(counter
, cpuctx
, ctx
);
263 list_del_counter(counter
, ctx
);
267 * Allow more per task counters with respect to the
270 cpuctx
->max_pertask
=
271 min(perf_max_counters
- ctx
->nr_counters
,
272 perf_max_counters
- perf_reserved_percpu
);
276 spin_unlock_irqrestore(&ctx
->lock
, flags
);
281 * Remove the counter from a task's (or a CPU's) list of counters.
283 * Must be called with ctx->mutex held.
285 * CPU counters are removed with a smp call. For task counters we only
286 * call when the task is on a CPU.
288 static void perf_counter_remove_from_context(struct perf_counter
*counter
)
290 struct perf_counter_context
*ctx
= counter
->ctx
;
291 struct task_struct
*task
= ctx
->task
;
296 * Per cpu counters are removed via an smp call and
297 * the removal is always sucessful.
299 smp_call_function_single(counter
->cpu
,
300 __perf_counter_remove_from_context
,
306 task_oncpu_function_call(task
, __perf_counter_remove_from_context
,
309 spin_lock_irq(&ctx
->lock
);
311 * If the context is active we need to retry the smp call.
313 if (ctx
->nr_active
&& !list_empty(&counter
->list_entry
)) {
314 spin_unlock_irq(&ctx
->lock
);
319 * The lock prevents that this context is scheduled in so we
320 * can remove the counter safely, if the call above did not
323 if (!list_empty(&counter
->list_entry
)) {
324 list_del_counter(counter
, ctx
);
326 spin_unlock_irq(&ctx
->lock
);
329 static inline u64
perf_clock(void)
331 return cpu_clock(smp_processor_id());
335 * Update the record of the current time in a context.
337 static void update_context_time(struct perf_counter_context
*ctx
)
339 u64 now
= perf_clock();
341 ctx
->time
+= now
- ctx
->timestamp
;
342 ctx
->timestamp
= now
;
346 * Update the total_time_enabled and total_time_running fields for a counter.
348 static void update_counter_times(struct perf_counter
*counter
)
350 struct perf_counter_context
*ctx
= counter
->ctx
;
353 if (counter
->state
< PERF_COUNTER_STATE_INACTIVE
)
356 counter
->total_time_enabled
= ctx
->time
- counter
->tstamp_enabled
;
358 if (counter
->state
== PERF_COUNTER_STATE_INACTIVE
)
359 run_end
= counter
->tstamp_stopped
;
363 counter
->total_time_running
= run_end
- counter
->tstamp_running
;
367 * Update total_time_enabled and total_time_running for all counters in a group.
369 static void update_group_times(struct perf_counter
*leader
)
371 struct perf_counter
*counter
;
373 update_counter_times(leader
);
374 list_for_each_entry(counter
, &leader
->sibling_list
, list_entry
)
375 update_counter_times(counter
);
379 * Cross CPU call to disable a performance counter
381 static void __perf_counter_disable(void *info
)
383 struct perf_counter
*counter
= info
;
384 struct perf_cpu_context
*cpuctx
= &__get_cpu_var(perf_cpu_context
);
385 struct perf_counter_context
*ctx
= counter
->ctx
;
389 * If this is a per-task counter, need to check whether this
390 * counter's task is the current task on this cpu.
392 if (ctx
->task
&& cpuctx
->task_ctx
!= ctx
)
395 spin_lock_irqsave(&ctx
->lock
, flags
);
398 * If the counter is on, turn it off.
399 * If it is in error state, leave it in error state.
401 if (counter
->state
>= PERF_COUNTER_STATE_INACTIVE
) {
402 update_context_time(ctx
);
403 update_counter_times(counter
);
404 if (counter
== counter
->group_leader
)
405 group_sched_out(counter
, cpuctx
, ctx
);
407 counter_sched_out(counter
, cpuctx
, ctx
);
408 counter
->state
= PERF_COUNTER_STATE_OFF
;
412 spin_unlock_irqrestore(&ctx
->lock
, flags
);
418 static void perf_counter_disable(struct perf_counter
*counter
)
420 struct perf_counter_context
*ctx
= counter
->ctx
;
421 struct task_struct
*task
= ctx
->task
;
425 * Disable the counter on the cpu that it's on
427 smp_call_function_single(counter
->cpu
, __perf_counter_disable
,
433 task_oncpu_function_call(task
, __perf_counter_disable
, counter
);
435 spin_lock_irq(&ctx
->lock
);
437 * If the counter is still active, we need to retry the cross-call.
439 if (counter
->state
== PERF_COUNTER_STATE_ACTIVE
) {
440 spin_unlock_irq(&ctx
->lock
);
445 * Since we have the lock this context can't be scheduled
446 * in, so we can change the state safely.
448 if (counter
->state
== PERF_COUNTER_STATE_INACTIVE
) {
449 update_counter_times(counter
);
450 counter
->state
= PERF_COUNTER_STATE_OFF
;
454 spin_unlock_irq(&ctx
->lock
);
458 counter_sched_in(struct perf_counter
*counter
,
459 struct perf_cpu_context
*cpuctx
,
460 struct perf_counter_context
*ctx
,
463 if (counter
->state
<= PERF_COUNTER_STATE_OFF
)
466 counter
->state
= PERF_COUNTER_STATE_ACTIVE
;
467 counter
->oncpu
= cpu
; /* TODO: put 'cpu' into cpuctx->cpu */
469 * The new state must be visible before we turn it on in the hardware:
473 if (counter
->pmu
->enable(counter
)) {
474 counter
->state
= PERF_COUNTER_STATE_INACTIVE
;
479 counter
->tstamp_running
+= ctx
->time
- counter
->tstamp_stopped
;
481 if (!is_software_counter(counter
))
482 cpuctx
->active_oncpu
++;
485 if (counter
->hw_event
.exclusive
)
486 cpuctx
->exclusive
= 1;
492 group_sched_in(struct perf_counter
*group_counter
,
493 struct perf_cpu_context
*cpuctx
,
494 struct perf_counter_context
*ctx
,
497 struct perf_counter
*counter
, *partial_group
;
500 if (group_counter
->state
== PERF_COUNTER_STATE_OFF
)
503 ret
= hw_perf_group_sched_in(group_counter
, cpuctx
, ctx
, cpu
);
505 return ret
< 0 ? ret
: 0;
507 group_counter
->prev_state
= group_counter
->state
;
508 if (counter_sched_in(group_counter
, cpuctx
, ctx
, cpu
))
512 * Schedule in siblings as one group (if any):
514 list_for_each_entry(counter
, &group_counter
->sibling_list
, list_entry
) {
515 counter
->prev_state
= counter
->state
;
516 if (counter_sched_in(counter
, cpuctx
, ctx
, cpu
)) {
517 partial_group
= counter
;
526 * Groups can be scheduled in as one unit only, so undo any
527 * partial group before returning:
529 list_for_each_entry(counter
, &group_counter
->sibling_list
, list_entry
) {
530 if (counter
== partial_group
)
532 counter_sched_out(counter
, cpuctx
, ctx
);
534 counter_sched_out(group_counter
, cpuctx
, ctx
);
540 * Return 1 for a group consisting entirely of software counters,
541 * 0 if the group contains any hardware counters.
543 static int is_software_only_group(struct perf_counter
*leader
)
545 struct perf_counter
*counter
;
547 if (!is_software_counter(leader
))
550 list_for_each_entry(counter
, &leader
->sibling_list
, list_entry
)
551 if (!is_software_counter(counter
))
558 * Work out whether we can put this counter group on the CPU now.
560 static int group_can_go_on(struct perf_counter
*counter
,
561 struct perf_cpu_context
*cpuctx
,
565 * Groups consisting entirely of software counters can always go on.
567 if (is_software_only_group(counter
))
570 * If an exclusive group is already on, no other hardware
571 * counters can go on.
573 if (cpuctx
->exclusive
)
576 * If this group is exclusive and there are already
577 * counters on the CPU, it can't go on.
579 if (counter
->hw_event
.exclusive
&& cpuctx
->active_oncpu
)
582 * Otherwise, try to add it if all previous groups were able
588 static void add_counter_to_ctx(struct perf_counter
*counter
,
589 struct perf_counter_context
*ctx
)
591 list_add_counter(counter
, ctx
);
592 counter
->prev_state
= PERF_COUNTER_STATE_OFF
;
593 counter
->tstamp_enabled
= ctx
->time
;
594 counter
->tstamp_running
= ctx
->time
;
595 counter
->tstamp_stopped
= ctx
->time
;
599 * Cross CPU call to install and enable a performance counter
601 * Must be called with ctx->mutex held
603 static void __perf_install_in_context(void *info
)
605 struct perf_cpu_context
*cpuctx
= &__get_cpu_var(perf_cpu_context
);
606 struct perf_counter
*counter
= info
;
607 struct perf_counter_context
*ctx
= counter
->ctx
;
608 struct perf_counter
*leader
= counter
->group_leader
;
609 int cpu
= smp_processor_id();
614 * If this is a task context, we need to check whether it is
615 * the current task context of this cpu. If not it has been
616 * scheduled out before the smp call arrived.
617 * Or possibly this is the right context but it isn't
618 * on this cpu because it had no counters.
620 if (ctx
->task
&& cpuctx
->task_ctx
!= ctx
) {
621 if (cpuctx
->task_ctx
|| ctx
->task
!= current
)
623 cpuctx
->task_ctx
= ctx
;
626 spin_lock_irqsave(&ctx
->lock
, flags
);
628 update_context_time(ctx
);
631 * Protect the list operation against NMI by disabling the
632 * counters on a global level. NOP for non NMI based counters.
636 add_counter_to_ctx(counter
, ctx
);
639 * Don't put the counter on if it is disabled or if
640 * it is in a group and the group isn't on.
642 if (counter
->state
!= PERF_COUNTER_STATE_INACTIVE
||
643 (leader
!= counter
&& leader
->state
!= PERF_COUNTER_STATE_ACTIVE
))
647 * An exclusive counter can't go on if there are already active
648 * hardware counters, and no hardware counter can go on if there
649 * is already an exclusive counter on.
651 if (!group_can_go_on(counter
, cpuctx
, 1))
654 err
= counter_sched_in(counter
, cpuctx
, ctx
, cpu
);
658 * This counter couldn't go on. If it is in a group
659 * then we have to pull the whole group off.
660 * If the counter group is pinned then put it in error state.
662 if (leader
!= counter
)
663 group_sched_out(leader
, cpuctx
, ctx
);
664 if (leader
->hw_event
.pinned
) {
665 update_group_times(leader
);
666 leader
->state
= PERF_COUNTER_STATE_ERROR
;
670 if (!err
&& !ctx
->task
&& cpuctx
->max_pertask
)
671 cpuctx
->max_pertask
--;
676 spin_unlock_irqrestore(&ctx
->lock
, flags
);
680 * Attach a performance counter to a context
682 * First we add the counter to the list with the hardware enable bit
683 * in counter->hw_config cleared.
685 * If the counter is attached to a task which is on a CPU we use a smp
686 * call to enable it in the task context. The task might have been
687 * scheduled away, but we check this in the smp call again.
689 * Must be called with ctx->mutex held.
692 perf_install_in_context(struct perf_counter_context
*ctx
,
693 struct perf_counter
*counter
,
696 struct task_struct
*task
= ctx
->task
;
700 * Per cpu counters are installed via an smp call and
701 * the install is always sucessful.
703 smp_call_function_single(cpu
, __perf_install_in_context
,
709 task_oncpu_function_call(task
, __perf_install_in_context
,
712 spin_lock_irq(&ctx
->lock
);
714 * we need to retry the smp call.
716 if (ctx
->is_active
&& list_empty(&counter
->list_entry
)) {
717 spin_unlock_irq(&ctx
->lock
);
722 * The lock prevents that this context is scheduled in so we
723 * can add the counter safely, if it the call above did not
726 if (list_empty(&counter
->list_entry
))
727 add_counter_to_ctx(counter
, ctx
);
728 spin_unlock_irq(&ctx
->lock
);
732 * Cross CPU call to enable a performance counter
734 static void __perf_counter_enable(void *info
)
736 struct perf_counter
*counter
= info
;
737 struct perf_cpu_context
*cpuctx
= &__get_cpu_var(perf_cpu_context
);
738 struct perf_counter_context
*ctx
= counter
->ctx
;
739 struct perf_counter
*leader
= counter
->group_leader
;
744 * If this is a per-task counter, need to check whether this
745 * counter's task is the current task on this cpu.
747 if (ctx
->task
&& cpuctx
->task_ctx
!= ctx
) {
748 if (cpuctx
->task_ctx
|| ctx
->task
!= current
)
750 cpuctx
->task_ctx
= ctx
;
753 spin_lock_irqsave(&ctx
->lock
, flags
);
755 update_context_time(ctx
);
757 counter
->prev_state
= counter
->state
;
758 if (counter
->state
>= PERF_COUNTER_STATE_INACTIVE
)
760 counter
->state
= PERF_COUNTER_STATE_INACTIVE
;
761 counter
->tstamp_enabled
= ctx
->time
- counter
->total_time_enabled
;
765 * If the counter is in a group and isn't the group leader,
766 * then don't put it on unless the group is on.
768 if (leader
!= counter
&& leader
->state
!= PERF_COUNTER_STATE_ACTIVE
)
771 if (!group_can_go_on(counter
, cpuctx
, 1)) {
775 if (counter
== leader
)
776 err
= group_sched_in(counter
, cpuctx
, ctx
,
779 err
= counter_sched_in(counter
, cpuctx
, ctx
,
786 * If this counter can't go on and it's part of a
787 * group, then the whole group has to come off.
789 if (leader
!= counter
)
790 group_sched_out(leader
, cpuctx
, ctx
);
791 if (leader
->hw_event
.pinned
) {
792 update_group_times(leader
);
793 leader
->state
= PERF_COUNTER_STATE_ERROR
;
798 spin_unlock_irqrestore(&ctx
->lock
, flags
);
804 static void perf_counter_enable(struct perf_counter
*counter
)
806 struct perf_counter_context
*ctx
= counter
->ctx
;
807 struct task_struct
*task
= ctx
->task
;
811 * Enable the counter on the cpu that it's on
813 smp_call_function_single(counter
->cpu
, __perf_counter_enable
,
818 spin_lock_irq(&ctx
->lock
);
819 if (counter
->state
>= PERF_COUNTER_STATE_INACTIVE
)
823 * If the counter is in error state, clear that first.
824 * That way, if we see the counter in error state below, we
825 * know that it has gone back into error state, as distinct
826 * from the task having been scheduled away before the
827 * cross-call arrived.
829 if (counter
->state
== PERF_COUNTER_STATE_ERROR
)
830 counter
->state
= PERF_COUNTER_STATE_OFF
;
833 spin_unlock_irq(&ctx
->lock
);
834 task_oncpu_function_call(task
, __perf_counter_enable
, counter
);
836 spin_lock_irq(&ctx
->lock
);
839 * If the context is active and the counter is still off,
840 * we need to retry the cross-call.
842 if (ctx
->is_active
&& counter
->state
== PERF_COUNTER_STATE_OFF
)
846 * Since we have the lock this context can't be scheduled
847 * in, so we can change the state safely.
849 if (counter
->state
== PERF_COUNTER_STATE_OFF
) {
850 counter
->state
= PERF_COUNTER_STATE_INACTIVE
;
851 counter
->tstamp_enabled
=
852 ctx
->time
- counter
->total_time_enabled
;
856 spin_unlock_irq(&ctx
->lock
);
859 static int perf_counter_refresh(struct perf_counter
*counter
, int refresh
)
862 * not supported on inherited counters
864 if (counter
->hw_event
.inherit
)
867 atomic_add(refresh
, &counter
->event_limit
);
868 perf_counter_enable(counter
);
873 void __perf_counter_sched_out(struct perf_counter_context
*ctx
,
874 struct perf_cpu_context
*cpuctx
)
876 struct perf_counter
*counter
;
878 spin_lock(&ctx
->lock
);
880 if (likely(!ctx
->nr_counters
))
882 update_context_time(ctx
);
885 if (ctx
->nr_active
) {
886 list_for_each_entry(counter
, &ctx
->counter_list
, list_entry
) {
887 if (counter
!= counter
->group_leader
)
888 counter_sched_out(counter
, cpuctx
, ctx
);
890 group_sched_out(counter
, cpuctx
, ctx
);
895 spin_unlock(&ctx
->lock
);
899 * Test whether two contexts are equivalent, i.e. whether they
900 * have both been cloned from the same version of the same context
901 * and they both have the same number of enabled counters.
902 * If the number of enabled counters is the same, then the set
903 * of enabled counters should be the same, because these are both
904 * inherited contexts, therefore we can't access individual counters
905 * in them directly with an fd; we can only enable/disable all
906 * counters via prctl, or enable/disable all counters in a family
907 * via ioctl, which will have the same effect on both contexts.
909 static int context_equiv(struct perf_counter_context
*ctx1
,
910 struct perf_counter_context
*ctx2
)
912 return ctx1
->parent_ctx
&& ctx1
->parent_ctx
== ctx2
->parent_ctx
913 && ctx1
->parent_gen
== ctx2
->parent_gen
914 && ctx1
->nr_enabled
== ctx2
->nr_enabled
;
918 * Called from scheduler to remove the counters of the current task,
919 * with interrupts disabled.
921 * We stop each counter and update the counter value in counter->count.
923 * This does not protect us against NMI, but disable()
924 * sets the disabled bit in the control field of counter _before_
925 * accessing the counter control register. If a NMI hits, then it will
926 * not restart the counter.
928 void perf_counter_task_sched_out(struct task_struct
*task
,
929 struct task_struct
*next
, int cpu
)
931 struct perf_cpu_context
*cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
932 struct perf_counter_context
*ctx
= task
->perf_counter_ctxp
;
933 struct perf_counter_context
*next_ctx
;
934 struct pt_regs
*regs
;
936 if (likely(!ctx
|| !cpuctx
->task_ctx
))
939 update_context_time(ctx
);
941 regs
= task_pt_regs(task
);
942 perf_swcounter_event(PERF_COUNT_CONTEXT_SWITCHES
, 1, 1, regs
, 0);
944 next_ctx
= next
->perf_counter_ctxp
;
945 if (next_ctx
&& context_equiv(ctx
, next_ctx
)) {
946 task
->perf_counter_ctxp
= next_ctx
;
947 next
->perf_counter_ctxp
= ctx
;
949 next_ctx
->task
= task
;
953 __perf_counter_sched_out(ctx
, cpuctx
);
955 cpuctx
->task_ctx
= NULL
;
958 static void __perf_counter_task_sched_out(struct perf_counter_context
*ctx
)
960 struct perf_cpu_context
*cpuctx
= &__get_cpu_var(perf_cpu_context
);
962 if (!cpuctx
->task_ctx
)
964 __perf_counter_sched_out(ctx
, cpuctx
);
965 cpuctx
->task_ctx
= NULL
;
968 static void perf_counter_cpu_sched_out(struct perf_cpu_context
*cpuctx
)
970 __perf_counter_sched_out(&cpuctx
->ctx
, cpuctx
);
974 __perf_counter_sched_in(struct perf_counter_context
*ctx
,
975 struct perf_cpu_context
*cpuctx
, int cpu
)
977 struct perf_counter
*counter
;
980 spin_lock(&ctx
->lock
);
982 if (likely(!ctx
->nr_counters
))
985 ctx
->timestamp
= perf_clock();
990 * First go through the list and put on any pinned groups
991 * in order to give them the best chance of going on.
993 list_for_each_entry(counter
, &ctx
->counter_list
, list_entry
) {
994 if (counter
->state
<= PERF_COUNTER_STATE_OFF
||
995 !counter
->hw_event
.pinned
)
997 if (counter
->cpu
!= -1 && counter
->cpu
!= cpu
)
1000 if (counter
!= counter
->group_leader
)
1001 counter_sched_in(counter
, cpuctx
, ctx
, cpu
);
1003 if (group_can_go_on(counter
, cpuctx
, 1))
1004 group_sched_in(counter
, cpuctx
, ctx
, cpu
);
1008 * If this pinned group hasn't been scheduled,
1009 * put it in error state.
1011 if (counter
->state
== PERF_COUNTER_STATE_INACTIVE
) {
1012 update_group_times(counter
);
1013 counter
->state
= PERF_COUNTER_STATE_ERROR
;
1017 list_for_each_entry(counter
, &ctx
->counter_list
, list_entry
) {
1019 * Ignore counters in OFF or ERROR state, and
1020 * ignore pinned counters since we did them already.
1022 if (counter
->state
<= PERF_COUNTER_STATE_OFF
||
1023 counter
->hw_event
.pinned
)
1027 * Listen to the 'cpu' scheduling filter constraint
1030 if (counter
->cpu
!= -1 && counter
->cpu
!= cpu
)
1033 if (counter
!= counter
->group_leader
) {
1034 if (counter_sched_in(counter
, cpuctx
, ctx
, cpu
))
1037 if (group_can_go_on(counter
, cpuctx
, can_add_hw
)) {
1038 if (group_sched_in(counter
, cpuctx
, ctx
, cpu
))
1045 spin_unlock(&ctx
->lock
);
1049 * Called from scheduler to add the counters of the current task
1050 * with interrupts disabled.
1052 * We restore the counter value and then enable it.
1054 * This does not protect us against NMI, but enable()
1055 * sets the enabled bit in the control field of counter _before_
1056 * accessing the counter control register. If a NMI hits, then it will
1057 * keep the counter running.
1059 void perf_counter_task_sched_in(struct task_struct
*task
, int cpu
)
1061 struct perf_cpu_context
*cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
1062 struct perf_counter_context
*ctx
= task
->perf_counter_ctxp
;
1066 if (cpuctx
->task_ctx
== ctx
)
1068 __perf_counter_sched_in(ctx
, cpuctx
, cpu
);
1069 cpuctx
->task_ctx
= ctx
;
1072 static void perf_counter_cpu_sched_in(struct perf_cpu_context
*cpuctx
, int cpu
)
1074 struct perf_counter_context
*ctx
= &cpuctx
->ctx
;
1076 __perf_counter_sched_in(ctx
, cpuctx
, cpu
);
1079 int perf_counter_task_disable(void)
1081 struct task_struct
*curr
= current
;
1082 struct perf_counter_context
*ctx
= curr
->perf_counter_ctxp
;
1083 struct perf_counter
*counter
;
1084 unsigned long flags
;
1086 if (!ctx
|| !ctx
->nr_counters
)
1089 local_irq_save(flags
);
1091 __perf_counter_task_sched_out(ctx
);
1093 spin_lock(&ctx
->lock
);
1096 * Disable all the counters:
1100 list_for_each_entry(counter
, &ctx
->counter_list
, list_entry
) {
1101 if (counter
->state
!= PERF_COUNTER_STATE_ERROR
) {
1102 update_group_times(counter
);
1103 counter
->state
= PERF_COUNTER_STATE_OFF
;
1109 spin_unlock_irqrestore(&ctx
->lock
, flags
);
1114 int perf_counter_task_enable(void)
1116 struct task_struct
*curr
= current
;
1117 struct perf_counter_context
*ctx
= curr
->perf_counter_ctxp
;
1118 struct perf_counter
*counter
;
1119 unsigned long flags
;
1122 if (!ctx
|| !ctx
->nr_counters
)
1125 local_irq_save(flags
);
1126 cpu
= smp_processor_id();
1128 __perf_counter_task_sched_out(ctx
);
1130 spin_lock(&ctx
->lock
);
1133 * Disable all the counters:
1137 list_for_each_entry(counter
, &ctx
->counter_list
, list_entry
) {
1138 if (counter
->state
> PERF_COUNTER_STATE_OFF
)
1140 counter
->state
= PERF_COUNTER_STATE_INACTIVE
;
1141 counter
->tstamp_enabled
=
1142 ctx
->time
- counter
->total_time_enabled
;
1143 counter
->hw_event
.disabled
= 0;
1147 spin_unlock(&ctx
->lock
);
1149 perf_counter_task_sched_in(curr
, cpu
);
1151 local_irq_restore(flags
);
1156 static void perf_log_period(struct perf_counter
*counter
, u64 period
);
1158 static void perf_adjust_freq(struct perf_counter_context
*ctx
)
1160 struct perf_counter
*counter
;
1165 spin_lock(&ctx
->lock
);
1166 list_for_each_entry(counter
, &ctx
->counter_list
, list_entry
) {
1167 if (counter
->state
!= PERF_COUNTER_STATE_ACTIVE
)
1170 if (!counter
->hw_event
.freq
|| !counter
->hw_event
.irq_freq
)
1173 events
= HZ
* counter
->hw
.interrupts
* counter
->hw
.irq_period
;
1174 period
= div64_u64(events
, counter
->hw_event
.irq_freq
);
1176 delta
= (s64
)(1 + period
- counter
->hw
.irq_period
);
1179 irq_period
= counter
->hw
.irq_period
+ delta
;
1184 perf_log_period(counter
, irq_period
);
1186 counter
->hw
.irq_period
= irq_period
;
1187 counter
->hw
.interrupts
= 0;
1189 spin_unlock(&ctx
->lock
);
1193 * Round-robin a context's counters:
1195 static void rotate_ctx(struct perf_counter_context
*ctx
)
1197 struct perf_counter
*counter
;
1199 if (!ctx
->nr_counters
)
1202 spin_lock(&ctx
->lock
);
1204 * Rotate the first entry last (works just fine for group counters too):
1207 list_for_each_entry(counter
, &ctx
->counter_list
, list_entry
) {
1208 list_move_tail(&counter
->list_entry
, &ctx
->counter_list
);
1213 spin_unlock(&ctx
->lock
);
1216 void perf_counter_task_tick(struct task_struct
*curr
, int cpu
)
1218 struct perf_cpu_context
*cpuctx
;
1219 struct perf_counter_context
*ctx
;
1221 if (!atomic_read(&nr_counters
))
1224 cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
1225 ctx
= curr
->perf_counter_ctxp
;
1227 perf_adjust_freq(&cpuctx
->ctx
);
1229 perf_adjust_freq(ctx
);
1231 perf_counter_cpu_sched_out(cpuctx
);
1233 __perf_counter_task_sched_out(ctx
);
1235 rotate_ctx(&cpuctx
->ctx
);
1239 perf_counter_cpu_sched_in(cpuctx
, cpu
);
1241 perf_counter_task_sched_in(curr
, cpu
);
1245 * Cross CPU call to read the hardware counter
1247 static void __read(void *info
)
1249 struct perf_counter
*counter
= info
;
1250 struct perf_counter_context
*ctx
= counter
->ctx
;
1251 unsigned long flags
;
1253 local_irq_save(flags
);
1255 update_context_time(ctx
);
1256 counter
->pmu
->read(counter
);
1257 update_counter_times(counter
);
1258 local_irq_restore(flags
);
1261 static u64
perf_counter_read(struct perf_counter
*counter
)
1264 * If counter is enabled and currently active on a CPU, update the
1265 * value in the counter structure:
1267 if (counter
->state
== PERF_COUNTER_STATE_ACTIVE
) {
1268 smp_call_function_single(counter
->oncpu
,
1269 __read
, counter
, 1);
1270 } else if (counter
->state
== PERF_COUNTER_STATE_INACTIVE
) {
1271 update_counter_times(counter
);
1274 return atomic64_read(&counter
->count
);
1278 * Initialize the perf_counter context in a task_struct:
1281 __perf_counter_init_context(struct perf_counter_context
*ctx
,
1282 struct task_struct
*task
)
1284 memset(ctx
, 0, sizeof(*ctx
));
1285 spin_lock_init(&ctx
->lock
);
1286 mutex_init(&ctx
->mutex
);
1287 INIT_LIST_HEAD(&ctx
->counter_list
);
1288 INIT_LIST_HEAD(&ctx
->event_list
);
1289 atomic_set(&ctx
->refcount
, 1);
1293 static void put_context(struct perf_counter_context
*ctx
)
1296 put_task_struct(ctx
->task
);
1299 static struct perf_counter_context
*find_get_context(pid_t pid
, int cpu
)
1301 struct perf_cpu_context
*cpuctx
;
1302 struct perf_counter_context
*ctx
;
1303 struct perf_counter_context
*tctx
;
1304 struct task_struct
*task
;
1307 * If cpu is not a wildcard then this is a percpu counter:
1310 /* Must be root to operate on a CPU counter: */
1311 if (sysctl_perf_counter_priv
&& !capable(CAP_SYS_ADMIN
))
1312 return ERR_PTR(-EACCES
);
1314 if (cpu
< 0 || cpu
> num_possible_cpus())
1315 return ERR_PTR(-EINVAL
);
1318 * We could be clever and allow to attach a counter to an
1319 * offline CPU and activate it when the CPU comes up, but
1322 if (!cpu_isset(cpu
, cpu_online_map
))
1323 return ERR_PTR(-ENODEV
);
1325 cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
1335 task
= find_task_by_vpid(pid
);
1337 get_task_struct(task
);
1341 return ERR_PTR(-ESRCH
);
1343 /* Reuse ptrace permission checks for now. */
1344 if (!ptrace_may_access(task
, PTRACE_MODE_READ
)) {
1345 put_task_struct(task
);
1346 return ERR_PTR(-EACCES
);
1349 ctx
= task
->perf_counter_ctxp
;
1351 ctx
= kmalloc(sizeof(struct perf_counter_context
), GFP_KERNEL
);
1353 put_task_struct(task
);
1354 return ERR_PTR(-ENOMEM
);
1356 __perf_counter_init_context(ctx
, task
);
1358 * Make sure other cpus see correct values for *ctx
1359 * once task->perf_counter_ctxp is visible to them.
1362 tctx
= cmpxchg(&task
->perf_counter_ctxp
, NULL
, ctx
);
1365 * We raced with some other task; use
1366 * the context they set.
1376 static void free_counter_rcu(struct rcu_head
*head
)
1378 struct perf_counter
*counter
;
1380 counter
= container_of(head
, struct perf_counter
, rcu_head
);
1381 put_ctx(counter
->ctx
);
1385 static void perf_pending_sync(struct perf_counter
*counter
);
1387 static void free_counter(struct perf_counter
*counter
)
1389 perf_pending_sync(counter
);
1391 atomic_dec(&nr_counters
);
1392 if (counter
->hw_event
.mmap
)
1393 atomic_dec(&nr_mmap_tracking
);
1394 if (counter
->hw_event
.munmap
)
1395 atomic_dec(&nr_munmap_tracking
);
1396 if (counter
->hw_event
.comm
)
1397 atomic_dec(&nr_comm_tracking
);
1399 if (counter
->destroy
)
1400 counter
->destroy(counter
);
1402 call_rcu(&counter
->rcu_head
, free_counter_rcu
);
1406 * Called when the last reference to the file is gone.
1408 static int perf_release(struct inode
*inode
, struct file
*file
)
1410 struct perf_counter
*counter
= file
->private_data
;
1411 struct perf_counter_context
*ctx
= counter
->ctx
;
1413 file
->private_data
= NULL
;
1415 mutex_lock(&ctx
->mutex
);
1416 perf_counter_remove_from_context(counter
);
1417 mutex_unlock(&ctx
->mutex
);
1419 free_counter(counter
);
1426 * Read the performance counter - simple non blocking version for now
1429 perf_read_hw(struct perf_counter
*counter
, char __user
*buf
, size_t count
)
1435 * Return end-of-file for a read on a counter that is in
1436 * error state (i.e. because it was pinned but it couldn't be
1437 * scheduled on to the CPU at some point).
1439 if (counter
->state
== PERF_COUNTER_STATE_ERROR
)
1442 mutex_lock(&counter
->child_mutex
);
1443 values
[0] = perf_counter_read(counter
);
1445 if (counter
->hw_event
.read_format
& PERF_FORMAT_TOTAL_TIME_ENABLED
)
1446 values
[n
++] = counter
->total_time_enabled
+
1447 atomic64_read(&counter
->child_total_time_enabled
);
1448 if (counter
->hw_event
.read_format
& PERF_FORMAT_TOTAL_TIME_RUNNING
)
1449 values
[n
++] = counter
->total_time_running
+
1450 atomic64_read(&counter
->child_total_time_running
);
1451 mutex_unlock(&counter
->child_mutex
);
1453 if (count
< n
* sizeof(u64
))
1455 count
= n
* sizeof(u64
);
1457 if (copy_to_user(buf
, values
, count
))
1464 perf_read(struct file
*file
, char __user
*buf
, size_t count
, loff_t
*ppos
)
1466 struct perf_counter
*counter
= file
->private_data
;
1468 return perf_read_hw(counter
, buf
, count
);
1471 static unsigned int perf_poll(struct file
*file
, poll_table
*wait
)
1473 struct perf_counter
*counter
= file
->private_data
;
1474 struct perf_mmap_data
*data
;
1475 unsigned int events
= POLL_HUP
;
1478 data
= rcu_dereference(counter
->data
);
1480 events
= atomic_xchg(&data
->poll
, 0);
1483 poll_wait(file
, &counter
->waitq
, wait
);
1488 static void perf_counter_reset(struct perf_counter
*counter
)
1490 (void)perf_counter_read(counter
);
1491 atomic64_set(&counter
->count
, 0);
1492 perf_counter_update_userpage(counter
);
1495 static void perf_counter_for_each_sibling(struct perf_counter
*counter
,
1496 void (*func
)(struct perf_counter
*))
1498 struct perf_counter_context
*ctx
= counter
->ctx
;
1499 struct perf_counter
*sibling
;
1501 mutex_lock(&ctx
->mutex
);
1502 counter
= counter
->group_leader
;
1505 list_for_each_entry(sibling
, &counter
->sibling_list
, list_entry
)
1507 mutex_unlock(&ctx
->mutex
);
1510 static void perf_counter_for_each_child(struct perf_counter
*counter
,
1511 void (*func
)(struct perf_counter
*))
1513 struct perf_counter
*child
;
1515 mutex_lock(&counter
->child_mutex
);
1517 list_for_each_entry(child
, &counter
->child_list
, child_list
)
1519 mutex_unlock(&counter
->child_mutex
);
1522 static void perf_counter_for_each(struct perf_counter
*counter
,
1523 void (*func
)(struct perf_counter
*))
1525 struct perf_counter
*child
;
1527 mutex_lock(&counter
->child_mutex
);
1528 perf_counter_for_each_sibling(counter
, func
);
1529 list_for_each_entry(child
, &counter
->child_list
, child_list
)
1530 perf_counter_for_each_sibling(child
, func
);
1531 mutex_unlock(&counter
->child_mutex
);
1534 static long perf_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
1536 struct perf_counter
*counter
= file
->private_data
;
1537 void (*func
)(struct perf_counter
*);
1541 case PERF_COUNTER_IOC_ENABLE
:
1542 func
= perf_counter_enable
;
1544 case PERF_COUNTER_IOC_DISABLE
:
1545 func
= perf_counter_disable
;
1547 case PERF_COUNTER_IOC_RESET
:
1548 func
= perf_counter_reset
;
1551 case PERF_COUNTER_IOC_REFRESH
:
1552 return perf_counter_refresh(counter
, arg
);
1557 if (flags
& PERF_IOC_FLAG_GROUP
)
1558 perf_counter_for_each(counter
, func
);
1560 perf_counter_for_each_child(counter
, func
);
1566 * Callers need to ensure there can be no nesting of this function, otherwise
1567 * the seqlock logic goes bad. We can not serialize this because the arch
1568 * code calls this from NMI context.
1570 void perf_counter_update_userpage(struct perf_counter
*counter
)
1572 struct perf_mmap_data
*data
;
1573 struct perf_counter_mmap_page
*userpg
;
1576 data
= rcu_dereference(counter
->data
);
1580 userpg
= data
->user_page
;
1583 * Disable preemption so as to not let the corresponding user-space
1584 * spin too long if we get preempted.
1589 userpg
->index
= counter
->hw
.idx
;
1590 userpg
->offset
= atomic64_read(&counter
->count
);
1591 if (counter
->state
== PERF_COUNTER_STATE_ACTIVE
)
1592 userpg
->offset
-= atomic64_read(&counter
->hw
.prev_count
);
1601 static int perf_mmap_fault(struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
1603 struct perf_counter
*counter
= vma
->vm_file
->private_data
;
1604 struct perf_mmap_data
*data
;
1605 int ret
= VM_FAULT_SIGBUS
;
1608 data
= rcu_dereference(counter
->data
);
1612 if (vmf
->pgoff
== 0) {
1613 vmf
->page
= virt_to_page(data
->user_page
);
1615 int nr
= vmf
->pgoff
- 1;
1617 if ((unsigned)nr
> data
->nr_pages
)
1620 vmf
->page
= virt_to_page(data
->data_pages
[nr
]);
1622 get_page(vmf
->page
);
1630 static int perf_mmap_data_alloc(struct perf_counter
*counter
, int nr_pages
)
1632 struct perf_mmap_data
*data
;
1636 WARN_ON(atomic_read(&counter
->mmap_count
));
1638 size
= sizeof(struct perf_mmap_data
);
1639 size
+= nr_pages
* sizeof(void *);
1641 data
= kzalloc(size
, GFP_KERNEL
);
1645 data
->user_page
= (void *)get_zeroed_page(GFP_KERNEL
);
1646 if (!data
->user_page
)
1647 goto fail_user_page
;
1649 for (i
= 0; i
< nr_pages
; i
++) {
1650 data
->data_pages
[i
] = (void *)get_zeroed_page(GFP_KERNEL
);
1651 if (!data
->data_pages
[i
])
1652 goto fail_data_pages
;
1655 data
->nr_pages
= nr_pages
;
1656 atomic_set(&data
->lock
, -1);
1658 rcu_assign_pointer(counter
->data
, data
);
1663 for (i
--; i
>= 0; i
--)
1664 free_page((unsigned long)data
->data_pages
[i
]);
1666 free_page((unsigned long)data
->user_page
);
1675 static void __perf_mmap_data_free(struct rcu_head
*rcu_head
)
1677 struct perf_mmap_data
*data
= container_of(rcu_head
,
1678 struct perf_mmap_data
, rcu_head
);
1681 free_page((unsigned long)data
->user_page
);
1682 for (i
= 0; i
< data
->nr_pages
; i
++)
1683 free_page((unsigned long)data
->data_pages
[i
]);
1687 static void perf_mmap_data_free(struct perf_counter
*counter
)
1689 struct perf_mmap_data
*data
= counter
->data
;
1691 WARN_ON(atomic_read(&counter
->mmap_count
));
1693 rcu_assign_pointer(counter
->data
, NULL
);
1694 call_rcu(&data
->rcu_head
, __perf_mmap_data_free
);
1697 static void perf_mmap_open(struct vm_area_struct
*vma
)
1699 struct perf_counter
*counter
= vma
->vm_file
->private_data
;
1701 atomic_inc(&counter
->mmap_count
);
1704 static void perf_mmap_close(struct vm_area_struct
*vma
)
1706 struct perf_counter
*counter
= vma
->vm_file
->private_data
;
1708 if (atomic_dec_and_mutex_lock(&counter
->mmap_count
,
1709 &counter
->mmap_mutex
)) {
1710 struct user_struct
*user
= current_user();
1712 atomic_long_sub(counter
->data
->nr_pages
+ 1, &user
->locked_vm
);
1713 vma
->vm_mm
->locked_vm
-= counter
->data
->nr_locked
;
1714 perf_mmap_data_free(counter
);
1715 mutex_unlock(&counter
->mmap_mutex
);
1719 static struct vm_operations_struct perf_mmap_vmops
= {
1720 .open
= perf_mmap_open
,
1721 .close
= perf_mmap_close
,
1722 .fault
= perf_mmap_fault
,
1725 static int perf_mmap(struct file
*file
, struct vm_area_struct
*vma
)
1727 struct perf_counter
*counter
= file
->private_data
;
1728 struct user_struct
*user
= current_user();
1729 unsigned long vma_size
;
1730 unsigned long nr_pages
;
1731 unsigned long user_locked
, user_lock_limit
;
1732 unsigned long locked
, lock_limit
;
1733 long user_extra
, extra
;
1736 if (!(vma
->vm_flags
& VM_SHARED
) || (vma
->vm_flags
& VM_WRITE
))
1739 vma_size
= vma
->vm_end
- vma
->vm_start
;
1740 nr_pages
= (vma_size
/ PAGE_SIZE
) - 1;
1743 * If we have data pages ensure they're a power-of-two number, so we
1744 * can do bitmasks instead of modulo.
1746 if (nr_pages
!= 0 && !is_power_of_2(nr_pages
))
1749 if (vma_size
!= PAGE_SIZE
* (1 + nr_pages
))
1752 if (vma
->vm_pgoff
!= 0)
1755 mutex_lock(&counter
->mmap_mutex
);
1756 if (atomic_inc_not_zero(&counter
->mmap_count
)) {
1757 if (nr_pages
!= counter
->data
->nr_pages
)
1762 user_extra
= nr_pages
+ 1;
1763 user_lock_limit
= sysctl_perf_counter_mlock
>> (PAGE_SHIFT
- 10);
1764 user_locked
= atomic_long_read(&user
->locked_vm
) + user_extra
;
1767 if (user_locked
> user_lock_limit
)
1768 extra
= user_locked
- user_lock_limit
;
1770 lock_limit
= current
->signal
->rlim
[RLIMIT_MEMLOCK
].rlim_cur
;
1771 lock_limit
>>= PAGE_SHIFT
;
1772 locked
= vma
->vm_mm
->locked_vm
+ extra
;
1774 if ((locked
> lock_limit
) && !capable(CAP_IPC_LOCK
)) {
1779 WARN_ON(counter
->data
);
1780 ret
= perf_mmap_data_alloc(counter
, nr_pages
);
1784 atomic_set(&counter
->mmap_count
, 1);
1785 atomic_long_add(user_extra
, &user
->locked_vm
);
1786 vma
->vm_mm
->locked_vm
+= extra
;
1787 counter
->data
->nr_locked
= extra
;
1789 mutex_unlock(&counter
->mmap_mutex
);
1791 vma
->vm_flags
&= ~VM_MAYWRITE
;
1792 vma
->vm_flags
|= VM_RESERVED
;
1793 vma
->vm_ops
= &perf_mmap_vmops
;
1798 static int perf_fasync(int fd
, struct file
*filp
, int on
)
1800 struct perf_counter
*counter
= filp
->private_data
;
1801 struct inode
*inode
= filp
->f_path
.dentry
->d_inode
;
1804 mutex_lock(&inode
->i_mutex
);
1805 retval
= fasync_helper(fd
, filp
, on
, &counter
->fasync
);
1806 mutex_unlock(&inode
->i_mutex
);
1814 static const struct file_operations perf_fops
= {
1815 .release
= perf_release
,
1818 .unlocked_ioctl
= perf_ioctl
,
1819 .compat_ioctl
= perf_ioctl
,
1821 .fasync
= perf_fasync
,
1825 * Perf counter wakeup
1827 * If there's data, ensure we set the poll() state and publish everything
1828 * to user-space before waking everybody up.
1831 void perf_counter_wakeup(struct perf_counter
*counter
)
1833 wake_up_all(&counter
->waitq
);
1835 if (counter
->pending_kill
) {
1836 kill_fasync(&counter
->fasync
, SIGIO
, counter
->pending_kill
);
1837 counter
->pending_kill
= 0;
1844 * Handle the case where we need to wakeup up from NMI (or rq->lock) context.
1846 * The NMI bit means we cannot possibly take locks. Therefore, maintain a
1847 * single linked list and use cmpxchg() to add entries lockless.
1850 static void perf_pending_counter(struct perf_pending_entry
*entry
)
1852 struct perf_counter
*counter
= container_of(entry
,
1853 struct perf_counter
, pending
);
1855 if (counter
->pending_disable
) {
1856 counter
->pending_disable
= 0;
1857 perf_counter_disable(counter
);
1860 if (counter
->pending_wakeup
) {
1861 counter
->pending_wakeup
= 0;
1862 perf_counter_wakeup(counter
);
1866 #define PENDING_TAIL ((struct perf_pending_entry *)-1UL)
1868 static DEFINE_PER_CPU(struct perf_pending_entry
*, perf_pending_head
) = {
1872 static void perf_pending_queue(struct perf_pending_entry
*entry
,
1873 void (*func
)(struct perf_pending_entry
*))
1875 struct perf_pending_entry
**head
;
1877 if (cmpxchg(&entry
->next
, NULL
, PENDING_TAIL
) != NULL
)
1882 head
= &get_cpu_var(perf_pending_head
);
1885 entry
->next
= *head
;
1886 } while (cmpxchg(head
, entry
->next
, entry
) != entry
->next
);
1888 set_perf_counter_pending();
1890 put_cpu_var(perf_pending_head
);
1893 static int __perf_pending_run(void)
1895 struct perf_pending_entry
*list
;
1898 list
= xchg(&__get_cpu_var(perf_pending_head
), PENDING_TAIL
);
1899 while (list
!= PENDING_TAIL
) {
1900 void (*func
)(struct perf_pending_entry
*);
1901 struct perf_pending_entry
*entry
= list
;
1908 * Ensure we observe the unqueue before we issue the wakeup,
1909 * so that we won't be waiting forever.
1910 * -- see perf_not_pending().
1921 static inline int perf_not_pending(struct perf_counter
*counter
)
1924 * If we flush on whatever cpu we run, there is a chance we don't
1928 __perf_pending_run();
1932 * Ensure we see the proper queue state before going to sleep
1933 * so that we do not miss the wakeup. -- see perf_pending_handle()
1936 return counter
->pending
.next
== NULL
;
1939 static void perf_pending_sync(struct perf_counter
*counter
)
1941 wait_event(counter
->waitq
, perf_not_pending(counter
));
1944 void perf_counter_do_pending(void)
1946 __perf_pending_run();
1950 * Callchain support -- arch specific
1953 __weak
struct perf_callchain_entry
*perf_callchain(struct pt_regs
*regs
)
1962 struct perf_output_handle
{
1963 struct perf_counter
*counter
;
1964 struct perf_mmap_data
*data
;
1965 unsigned int offset
;
1970 unsigned long flags
;
1973 static void perf_output_wakeup(struct perf_output_handle
*handle
)
1975 atomic_set(&handle
->data
->poll
, POLL_IN
);
1978 handle
->counter
->pending_wakeup
= 1;
1979 perf_pending_queue(&handle
->counter
->pending
,
1980 perf_pending_counter
);
1982 perf_counter_wakeup(handle
->counter
);
1986 * Curious locking construct.
1988 * We need to ensure a later event doesn't publish a head when a former
1989 * event isn't done writing. However since we need to deal with NMIs we
1990 * cannot fully serialize things.
1992 * What we do is serialize between CPUs so we only have to deal with NMI
1993 * nesting on a single CPU.
1995 * We only publish the head (and generate a wakeup) when the outer-most
1998 static void perf_output_lock(struct perf_output_handle
*handle
)
2000 struct perf_mmap_data
*data
= handle
->data
;
2005 local_irq_save(handle
->flags
);
2006 cpu
= smp_processor_id();
2008 if (in_nmi() && atomic_read(&data
->lock
) == cpu
)
2011 while (atomic_cmpxchg(&data
->lock
, -1, cpu
) != -1)
2017 static void perf_output_unlock(struct perf_output_handle
*handle
)
2019 struct perf_mmap_data
*data
= handle
->data
;
2022 data
->done_head
= data
->head
;
2024 if (!handle
->locked
)
2029 * The xchg implies a full barrier that ensures all writes are done
2030 * before we publish the new head, matched by a rmb() in userspace when
2031 * reading this position.
2033 while ((head
= atomic_xchg(&data
->done_head
, 0)))
2034 data
->user_page
->data_head
= head
;
2037 * NMI can happen here, which means we can miss a done_head update.
2040 cpu
= atomic_xchg(&data
->lock
, -1);
2041 WARN_ON_ONCE(cpu
!= smp_processor_id());
2044 * Therefore we have to validate we did not indeed do so.
2046 if (unlikely(atomic_read(&data
->done_head
))) {
2048 * Since we had it locked, we can lock it again.
2050 while (atomic_cmpxchg(&data
->lock
, -1, cpu
) != -1)
2056 if (atomic_xchg(&data
->wakeup
, 0))
2057 perf_output_wakeup(handle
);
2059 local_irq_restore(handle
->flags
);
2062 static int perf_output_begin(struct perf_output_handle
*handle
,
2063 struct perf_counter
*counter
, unsigned int size
,
2064 int nmi
, int overflow
)
2066 struct perf_mmap_data
*data
;
2067 unsigned int offset
, head
;
2070 * For inherited counters we send all the output towards the parent.
2072 if (counter
->parent
)
2073 counter
= counter
->parent
;
2076 data
= rcu_dereference(counter
->data
);
2080 handle
->data
= data
;
2081 handle
->counter
= counter
;
2083 handle
->overflow
= overflow
;
2085 if (!data
->nr_pages
)
2088 perf_output_lock(handle
);
2091 offset
= head
= atomic_read(&data
->head
);
2093 } while (atomic_cmpxchg(&data
->head
, offset
, head
) != offset
);
2095 handle
->offset
= offset
;
2096 handle
->head
= head
;
2098 if ((offset
>> PAGE_SHIFT
) != (head
>> PAGE_SHIFT
))
2099 atomic_set(&data
->wakeup
, 1);
2104 perf_output_wakeup(handle
);
2111 static void perf_output_copy(struct perf_output_handle
*handle
,
2112 void *buf
, unsigned int len
)
2114 unsigned int pages_mask
;
2115 unsigned int offset
;
2119 offset
= handle
->offset
;
2120 pages_mask
= handle
->data
->nr_pages
- 1;
2121 pages
= handle
->data
->data_pages
;
2124 unsigned int page_offset
;
2127 nr
= (offset
>> PAGE_SHIFT
) & pages_mask
;
2128 page_offset
= offset
& (PAGE_SIZE
- 1);
2129 size
= min_t(unsigned int, PAGE_SIZE
- page_offset
, len
);
2131 memcpy(pages
[nr
] + page_offset
, buf
, size
);
2138 handle
->offset
= offset
;
2141 * Check we didn't copy past our reservation window, taking the
2142 * possible unsigned int wrap into account.
2144 WARN_ON_ONCE(((int)(handle
->head
- handle
->offset
)) < 0);
2147 #define perf_output_put(handle, x) \
2148 perf_output_copy((handle), &(x), sizeof(x))
2150 static void perf_output_end(struct perf_output_handle
*handle
)
2152 struct perf_counter
*counter
= handle
->counter
;
2153 struct perf_mmap_data
*data
= handle
->data
;
2155 int wakeup_events
= counter
->hw_event
.wakeup_events
;
2157 if (handle
->overflow
&& wakeup_events
) {
2158 int events
= atomic_inc_return(&data
->events
);
2159 if (events
>= wakeup_events
) {
2160 atomic_sub(wakeup_events
, &data
->events
);
2161 atomic_set(&data
->wakeup
, 1);
2165 perf_output_unlock(handle
);
2169 static void perf_counter_output(struct perf_counter
*counter
,
2170 int nmi
, struct pt_regs
*regs
, u64 addr
)
2173 u64 record_type
= counter
->hw_event
.record_type
;
2174 struct perf_output_handle handle
;
2175 struct perf_event_header header
;
2184 struct perf_callchain_entry
*callchain
= NULL
;
2185 int callchain_size
= 0;
2192 header
.size
= sizeof(header
);
2194 header
.misc
= PERF_EVENT_MISC_OVERFLOW
;
2195 header
.misc
|= perf_misc_flags(regs
);
2197 if (record_type
& PERF_RECORD_IP
) {
2198 ip
= perf_instruction_pointer(regs
);
2199 header
.type
|= PERF_RECORD_IP
;
2200 header
.size
+= sizeof(ip
);
2203 if (record_type
& PERF_RECORD_TID
) {
2204 /* namespace issues */
2205 tid_entry
.pid
= current
->group_leader
->pid
;
2206 tid_entry
.tid
= current
->pid
;
2208 header
.type
|= PERF_RECORD_TID
;
2209 header
.size
+= sizeof(tid_entry
);
2212 if (record_type
& PERF_RECORD_TIME
) {
2214 * Maybe do better on x86 and provide cpu_clock_nmi()
2216 time
= sched_clock();
2218 header
.type
|= PERF_RECORD_TIME
;
2219 header
.size
+= sizeof(u64
);
2222 if (record_type
& PERF_RECORD_ADDR
) {
2223 header
.type
|= PERF_RECORD_ADDR
;
2224 header
.size
+= sizeof(u64
);
2227 if (record_type
& PERF_RECORD_CONFIG
) {
2228 header
.type
|= PERF_RECORD_CONFIG
;
2229 header
.size
+= sizeof(u64
);
2232 if (record_type
& PERF_RECORD_CPU
) {
2233 header
.type
|= PERF_RECORD_CPU
;
2234 header
.size
+= sizeof(cpu_entry
);
2236 cpu_entry
.cpu
= raw_smp_processor_id();
2239 if (record_type
& PERF_RECORD_GROUP
) {
2240 header
.type
|= PERF_RECORD_GROUP
;
2241 header
.size
+= sizeof(u64
) +
2242 counter
->nr_siblings
* sizeof(group_entry
);
2245 if (record_type
& PERF_RECORD_CALLCHAIN
) {
2246 callchain
= perf_callchain(regs
);
2249 callchain_size
= (1 + callchain
->nr
) * sizeof(u64
);
2251 header
.type
|= PERF_RECORD_CALLCHAIN
;
2252 header
.size
+= callchain_size
;
2256 ret
= perf_output_begin(&handle
, counter
, header
.size
, nmi
, 1);
2260 perf_output_put(&handle
, header
);
2262 if (record_type
& PERF_RECORD_IP
)
2263 perf_output_put(&handle
, ip
);
2265 if (record_type
& PERF_RECORD_TID
)
2266 perf_output_put(&handle
, tid_entry
);
2268 if (record_type
& PERF_RECORD_TIME
)
2269 perf_output_put(&handle
, time
);
2271 if (record_type
& PERF_RECORD_ADDR
)
2272 perf_output_put(&handle
, addr
);
2274 if (record_type
& PERF_RECORD_CONFIG
)
2275 perf_output_put(&handle
, counter
->hw_event
.config
);
2277 if (record_type
& PERF_RECORD_CPU
)
2278 perf_output_put(&handle
, cpu_entry
);
2281 * XXX PERF_RECORD_GROUP vs inherited counters seems difficult.
2283 if (record_type
& PERF_RECORD_GROUP
) {
2284 struct perf_counter
*leader
, *sub
;
2285 u64 nr
= counter
->nr_siblings
;
2287 perf_output_put(&handle
, nr
);
2289 leader
= counter
->group_leader
;
2290 list_for_each_entry(sub
, &leader
->sibling_list
, list_entry
) {
2292 sub
->pmu
->read(sub
);
2294 group_entry
.event
= sub
->hw_event
.config
;
2295 group_entry
.counter
= atomic64_read(&sub
->count
);
2297 perf_output_put(&handle
, group_entry
);
2302 perf_output_copy(&handle
, callchain
, callchain_size
);
2304 perf_output_end(&handle
);
2311 struct perf_comm_event
{
2312 struct task_struct
*task
;
2317 struct perf_event_header header
;
2324 static void perf_counter_comm_output(struct perf_counter
*counter
,
2325 struct perf_comm_event
*comm_event
)
2327 struct perf_output_handle handle
;
2328 int size
= comm_event
->event
.header
.size
;
2329 int ret
= perf_output_begin(&handle
, counter
, size
, 0, 0);
2334 perf_output_put(&handle
, comm_event
->event
);
2335 perf_output_copy(&handle
, comm_event
->comm
,
2336 comm_event
->comm_size
);
2337 perf_output_end(&handle
);
2340 static int perf_counter_comm_match(struct perf_counter
*counter
,
2341 struct perf_comm_event
*comm_event
)
2343 if (counter
->hw_event
.comm
&&
2344 comm_event
->event
.header
.type
== PERF_EVENT_COMM
)
2350 static void perf_counter_comm_ctx(struct perf_counter_context
*ctx
,
2351 struct perf_comm_event
*comm_event
)
2353 struct perf_counter
*counter
;
2355 if (system_state
!= SYSTEM_RUNNING
|| list_empty(&ctx
->event_list
))
2359 list_for_each_entry_rcu(counter
, &ctx
->event_list
, event_entry
) {
2360 if (perf_counter_comm_match(counter
, comm_event
))
2361 perf_counter_comm_output(counter
, comm_event
);
2366 static void perf_counter_comm_event(struct perf_comm_event
*comm_event
)
2368 struct perf_cpu_context
*cpuctx
;
2370 char *comm
= comm_event
->task
->comm
;
2372 size
= ALIGN(strlen(comm
)+1, sizeof(u64
));
2374 comm_event
->comm
= comm
;
2375 comm_event
->comm_size
= size
;
2377 comm_event
->event
.header
.size
= sizeof(comm_event
->event
) + size
;
2379 cpuctx
= &get_cpu_var(perf_cpu_context
);
2380 perf_counter_comm_ctx(&cpuctx
->ctx
, comm_event
);
2381 put_cpu_var(perf_cpu_context
);
2383 perf_counter_comm_ctx(current
->perf_counter_ctxp
, comm_event
);
2386 void perf_counter_comm(struct task_struct
*task
)
2388 struct perf_comm_event comm_event
;
2390 if (!atomic_read(&nr_comm_tracking
))
2392 if (!current
->perf_counter_ctxp
)
2395 comm_event
= (struct perf_comm_event
){
2398 .header
= { .type
= PERF_EVENT_COMM
, },
2399 .pid
= task
->group_leader
->pid
,
2404 perf_counter_comm_event(&comm_event
);
2411 struct perf_mmap_event
{
2417 struct perf_event_header header
;
2427 static void perf_counter_mmap_output(struct perf_counter
*counter
,
2428 struct perf_mmap_event
*mmap_event
)
2430 struct perf_output_handle handle
;
2431 int size
= mmap_event
->event
.header
.size
;
2432 int ret
= perf_output_begin(&handle
, counter
, size
, 0, 0);
2437 perf_output_put(&handle
, mmap_event
->event
);
2438 perf_output_copy(&handle
, mmap_event
->file_name
,
2439 mmap_event
->file_size
);
2440 perf_output_end(&handle
);
2443 static int perf_counter_mmap_match(struct perf_counter
*counter
,
2444 struct perf_mmap_event
*mmap_event
)
2446 if (counter
->hw_event
.mmap
&&
2447 mmap_event
->event
.header
.type
== PERF_EVENT_MMAP
)
2450 if (counter
->hw_event
.munmap
&&
2451 mmap_event
->event
.header
.type
== PERF_EVENT_MUNMAP
)
2457 static void perf_counter_mmap_ctx(struct perf_counter_context
*ctx
,
2458 struct perf_mmap_event
*mmap_event
)
2460 struct perf_counter
*counter
;
2462 if (system_state
!= SYSTEM_RUNNING
|| list_empty(&ctx
->event_list
))
2466 list_for_each_entry_rcu(counter
, &ctx
->event_list
, event_entry
) {
2467 if (perf_counter_mmap_match(counter
, mmap_event
))
2468 perf_counter_mmap_output(counter
, mmap_event
);
2473 static void perf_counter_mmap_event(struct perf_mmap_event
*mmap_event
)
2475 struct perf_cpu_context
*cpuctx
;
2476 struct file
*file
= mmap_event
->file
;
2483 buf
= kzalloc(PATH_MAX
, GFP_KERNEL
);
2485 name
= strncpy(tmp
, "//enomem", sizeof(tmp
));
2488 name
= d_path(&file
->f_path
, buf
, PATH_MAX
);
2490 name
= strncpy(tmp
, "//toolong", sizeof(tmp
));
2494 name
= strncpy(tmp
, "//anon", sizeof(tmp
));
2499 size
= ALIGN(strlen(name
)+1, sizeof(u64
));
2501 mmap_event
->file_name
= name
;
2502 mmap_event
->file_size
= size
;
2504 mmap_event
->event
.header
.size
= sizeof(mmap_event
->event
) + size
;
2506 cpuctx
= &get_cpu_var(perf_cpu_context
);
2507 perf_counter_mmap_ctx(&cpuctx
->ctx
, mmap_event
);
2508 put_cpu_var(perf_cpu_context
);
2510 perf_counter_mmap_ctx(current
->perf_counter_ctxp
, mmap_event
);
2515 void perf_counter_mmap(unsigned long addr
, unsigned long len
,
2516 unsigned long pgoff
, struct file
*file
)
2518 struct perf_mmap_event mmap_event
;
2520 if (!atomic_read(&nr_mmap_tracking
))
2522 if (!current
->perf_counter_ctxp
)
2525 mmap_event
= (struct perf_mmap_event
){
2528 .header
= { .type
= PERF_EVENT_MMAP
, },
2529 .pid
= current
->group_leader
->pid
,
2530 .tid
= current
->pid
,
2537 perf_counter_mmap_event(&mmap_event
);
2540 void perf_counter_munmap(unsigned long addr
, unsigned long len
,
2541 unsigned long pgoff
, struct file
*file
)
2543 struct perf_mmap_event mmap_event
;
2545 if (!atomic_read(&nr_munmap_tracking
))
2548 mmap_event
= (struct perf_mmap_event
){
2551 .header
= { .type
= PERF_EVENT_MUNMAP
, },
2552 .pid
= current
->group_leader
->pid
,
2553 .tid
= current
->pid
,
2560 perf_counter_mmap_event(&mmap_event
);
2564 * Log irq_period changes so that analyzing tools can re-normalize the
2568 static void perf_log_period(struct perf_counter
*counter
, u64 period
)
2570 struct perf_output_handle handle
;
2574 struct perf_event_header header
;
2579 .type
= PERF_EVENT_PERIOD
,
2581 .size
= sizeof(freq_event
),
2583 .time
= sched_clock(),
2587 if (counter
->hw
.irq_period
== period
)
2590 ret
= perf_output_begin(&handle
, counter
, sizeof(freq_event
), 0, 0);
2594 perf_output_put(&handle
, freq_event
);
2595 perf_output_end(&handle
);
2599 * Generic counter overflow handling.
2602 int perf_counter_overflow(struct perf_counter
*counter
,
2603 int nmi
, struct pt_regs
*regs
, u64 addr
)
2605 int events
= atomic_read(&counter
->event_limit
);
2608 counter
->hw
.interrupts
++;
2611 * XXX event_limit might not quite work as expected on inherited
2615 counter
->pending_kill
= POLL_IN
;
2616 if (events
&& atomic_dec_and_test(&counter
->event_limit
)) {
2618 counter
->pending_kill
= POLL_HUP
;
2620 counter
->pending_disable
= 1;
2621 perf_pending_queue(&counter
->pending
,
2622 perf_pending_counter
);
2624 perf_counter_disable(counter
);
2627 perf_counter_output(counter
, nmi
, regs
, addr
);
2632 * Generic software counter infrastructure
2635 static void perf_swcounter_update(struct perf_counter
*counter
)
2637 struct hw_perf_counter
*hwc
= &counter
->hw
;
2642 prev
= atomic64_read(&hwc
->prev_count
);
2643 now
= atomic64_read(&hwc
->count
);
2644 if (atomic64_cmpxchg(&hwc
->prev_count
, prev
, now
) != prev
)
2649 atomic64_add(delta
, &counter
->count
);
2650 atomic64_sub(delta
, &hwc
->period_left
);
2653 static void perf_swcounter_set_period(struct perf_counter
*counter
)
2655 struct hw_perf_counter
*hwc
= &counter
->hw
;
2656 s64 left
= atomic64_read(&hwc
->period_left
);
2657 s64 period
= hwc
->irq_period
;
2659 if (unlikely(left
<= -period
)) {
2661 atomic64_set(&hwc
->period_left
, left
);
2664 if (unlikely(left
<= 0)) {
2666 atomic64_add(period
, &hwc
->period_left
);
2669 atomic64_set(&hwc
->prev_count
, -left
);
2670 atomic64_set(&hwc
->count
, -left
);
2673 static enum hrtimer_restart
perf_swcounter_hrtimer(struct hrtimer
*hrtimer
)
2675 enum hrtimer_restart ret
= HRTIMER_RESTART
;
2676 struct perf_counter
*counter
;
2677 struct pt_regs
*regs
;
2680 counter
= container_of(hrtimer
, struct perf_counter
, hw
.hrtimer
);
2681 counter
->pmu
->read(counter
);
2683 regs
= get_irq_regs();
2685 * In case we exclude kernel IPs or are somehow not in interrupt
2686 * context, provide the next best thing, the user IP.
2688 if ((counter
->hw_event
.exclude_kernel
|| !regs
) &&
2689 !counter
->hw_event
.exclude_user
)
2690 regs
= task_pt_regs(current
);
2693 if (perf_counter_overflow(counter
, 0, regs
, 0))
2694 ret
= HRTIMER_NORESTART
;
2697 period
= max_t(u64
, 10000, counter
->hw
.irq_period
);
2698 hrtimer_forward_now(hrtimer
, ns_to_ktime(period
));
2703 static void perf_swcounter_overflow(struct perf_counter
*counter
,
2704 int nmi
, struct pt_regs
*regs
, u64 addr
)
2706 perf_swcounter_update(counter
);
2707 perf_swcounter_set_period(counter
);
2708 if (perf_counter_overflow(counter
, nmi
, regs
, addr
))
2709 /* soft-disable the counter */
2714 static int perf_swcounter_match(struct perf_counter
*counter
,
2715 enum perf_event_types type
,
2716 u32 event
, struct pt_regs
*regs
)
2718 if (counter
->state
!= PERF_COUNTER_STATE_ACTIVE
)
2721 if (perf_event_raw(&counter
->hw_event
))
2724 if (perf_event_type(&counter
->hw_event
) != type
)
2727 if (perf_event_id(&counter
->hw_event
) != event
)
2730 if (counter
->hw_event
.exclude_user
&& user_mode(regs
))
2733 if (counter
->hw_event
.exclude_kernel
&& !user_mode(regs
))
2739 static void perf_swcounter_add(struct perf_counter
*counter
, u64 nr
,
2740 int nmi
, struct pt_regs
*regs
, u64 addr
)
2742 int neg
= atomic64_add_negative(nr
, &counter
->hw
.count
);
2743 if (counter
->hw
.irq_period
&& !neg
)
2744 perf_swcounter_overflow(counter
, nmi
, regs
, addr
);
2747 static void perf_swcounter_ctx_event(struct perf_counter_context
*ctx
,
2748 enum perf_event_types type
, u32 event
,
2749 u64 nr
, int nmi
, struct pt_regs
*regs
,
2752 struct perf_counter
*counter
;
2754 if (system_state
!= SYSTEM_RUNNING
|| list_empty(&ctx
->event_list
))
2758 list_for_each_entry_rcu(counter
, &ctx
->event_list
, event_entry
) {
2759 if (perf_swcounter_match(counter
, type
, event
, regs
))
2760 perf_swcounter_add(counter
, nr
, nmi
, regs
, addr
);
2765 static int *perf_swcounter_recursion_context(struct perf_cpu_context
*cpuctx
)
2768 return &cpuctx
->recursion
[3];
2771 return &cpuctx
->recursion
[2];
2774 return &cpuctx
->recursion
[1];
2776 return &cpuctx
->recursion
[0];
2779 static void __perf_swcounter_event(enum perf_event_types type
, u32 event
,
2780 u64 nr
, int nmi
, struct pt_regs
*regs
,
2783 struct perf_cpu_context
*cpuctx
= &get_cpu_var(perf_cpu_context
);
2784 int *recursion
= perf_swcounter_recursion_context(cpuctx
);
2792 perf_swcounter_ctx_event(&cpuctx
->ctx
, type
, event
,
2793 nr
, nmi
, regs
, addr
);
2794 if (cpuctx
->task_ctx
) {
2795 perf_swcounter_ctx_event(cpuctx
->task_ctx
, type
, event
,
2796 nr
, nmi
, regs
, addr
);
2803 put_cpu_var(perf_cpu_context
);
2807 perf_swcounter_event(u32 event
, u64 nr
, int nmi
, struct pt_regs
*regs
, u64 addr
)
2809 __perf_swcounter_event(PERF_TYPE_SOFTWARE
, event
, nr
, nmi
, regs
, addr
);
2812 static void perf_swcounter_read(struct perf_counter
*counter
)
2814 perf_swcounter_update(counter
);
2817 static int perf_swcounter_enable(struct perf_counter
*counter
)
2819 perf_swcounter_set_period(counter
);
2823 static void perf_swcounter_disable(struct perf_counter
*counter
)
2825 perf_swcounter_update(counter
);
2828 static const struct pmu perf_ops_generic
= {
2829 .enable
= perf_swcounter_enable
,
2830 .disable
= perf_swcounter_disable
,
2831 .read
= perf_swcounter_read
,
2835 * Software counter: cpu wall time clock
2838 static void cpu_clock_perf_counter_update(struct perf_counter
*counter
)
2840 int cpu
= raw_smp_processor_id();
2844 now
= cpu_clock(cpu
);
2845 prev
= atomic64_read(&counter
->hw
.prev_count
);
2846 atomic64_set(&counter
->hw
.prev_count
, now
);
2847 atomic64_add(now
- prev
, &counter
->count
);
2850 static int cpu_clock_perf_counter_enable(struct perf_counter
*counter
)
2852 struct hw_perf_counter
*hwc
= &counter
->hw
;
2853 int cpu
= raw_smp_processor_id();
2855 atomic64_set(&hwc
->prev_count
, cpu_clock(cpu
));
2856 hrtimer_init(&hwc
->hrtimer
, CLOCK_MONOTONIC
, HRTIMER_MODE_REL
);
2857 hwc
->hrtimer
.function
= perf_swcounter_hrtimer
;
2858 if (hwc
->irq_period
) {
2859 u64 period
= max_t(u64
, 10000, hwc
->irq_period
);
2860 __hrtimer_start_range_ns(&hwc
->hrtimer
,
2861 ns_to_ktime(period
), 0,
2862 HRTIMER_MODE_REL
, 0);
2868 static void cpu_clock_perf_counter_disable(struct perf_counter
*counter
)
2870 if (counter
->hw
.irq_period
)
2871 hrtimer_cancel(&counter
->hw
.hrtimer
);
2872 cpu_clock_perf_counter_update(counter
);
2875 static void cpu_clock_perf_counter_read(struct perf_counter
*counter
)
2877 cpu_clock_perf_counter_update(counter
);
2880 static const struct pmu perf_ops_cpu_clock
= {
2881 .enable
= cpu_clock_perf_counter_enable
,
2882 .disable
= cpu_clock_perf_counter_disable
,
2883 .read
= cpu_clock_perf_counter_read
,
2887 * Software counter: task time clock
2890 static void task_clock_perf_counter_update(struct perf_counter
*counter
, u64 now
)
2895 prev
= atomic64_xchg(&counter
->hw
.prev_count
, now
);
2897 atomic64_add(delta
, &counter
->count
);
2900 static int task_clock_perf_counter_enable(struct perf_counter
*counter
)
2902 struct hw_perf_counter
*hwc
= &counter
->hw
;
2905 now
= counter
->ctx
->time
;
2907 atomic64_set(&hwc
->prev_count
, now
);
2908 hrtimer_init(&hwc
->hrtimer
, CLOCK_MONOTONIC
, HRTIMER_MODE_REL
);
2909 hwc
->hrtimer
.function
= perf_swcounter_hrtimer
;
2910 if (hwc
->irq_period
) {
2911 u64 period
= max_t(u64
, 10000, hwc
->irq_period
);
2912 __hrtimer_start_range_ns(&hwc
->hrtimer
,
2913 ns_to_ktime(period
), 0,
2914 HRTIMER_MODE_REL
, 0);
2920 static void task_clock_perf_counter_disable(struct perf_counter
*counter
)
2922 if (counter
->hw
.irq_period
)
2923 hrtimer_cancel(&counter
->hw
.hrtimer
);
2924 task_clock_perf_counter_update(counter
, counter
->ctx
->time
);
2928 static void task_clock_perf_counter_read(struct perf_counter
*counter
)
2933 update_context_time(counter
->ctx
);
2934 time
= counter
->ctx
->time
;
2936 u64 now
= perf_clock();
2937 u64 delta
= now
- counter
->ctx
->timestamp
;
2938 time
= counter
->ctx
->time
+ delta
;
2941 task_clock_perf_counter_update(counter
, time
);
2944 static const struct pmu perf_ops_task_clock
= {
2945 .enable
= task_clock_perf_counter_enable
,
2946 .disable
= task_clock_perf_counter_disable
,
2947 .read
= task_clock_perf_counter_read
,
2951 * Software counter: cpu migrations
2954 static inline u64
get_cpu_migrations(struct perf_counter
*counter
)
2956 struct task_struct
*curr
= counter
->ctx
->task
;
2959 return curr
->se
.nr_migrations
;
2960 return cpu_nr_migrations(smp_processor_id());
2963 static void cpu_migrations_perf_counter_update(struct perf_counter
*counter
)
2968 prev
= atomic64_read(&counter
->hw
.prev_count
);
2969 now
= get_cpu_migrations(counter
);
2971 atomic64_set(&counter
->hw
.prev_count
, now
);
2975 atomic64_add(delta
, &counter
->count
);
2978 static void cpu_migrations_perf_counter_read(struct perf_counter
*counter
)
2980 cpu_migrations_perf_counter_update(counter
);
2983 static int cpu_migrations_perf_counter_enable(struct perf_counter
*counter
)
2985 if (counter
->prev_state
<= PERF_COUNTER_STATE_OFF
)
2986 atomic64_set(&counter
->hw
.prev_count
,
2987 get_cpu_migrations(counter
));
2991 static void cpu_migrations_perf_counter_disable(struct perf_counter
*counter
)
2993 cpu_migrations_perf_counter_update(counter
);
2996 static const struct pmu perf_ops_cpu_migrations
= {
2997 .enable
= cpu_migrations_perf_counter_enable
,
2998 .disable
= cpu_migrations_perf_counter_disable
,
2999 .read
= cpu_migrations_perf_counter_read
,
3002 #ifdef CONFIG_EVENT_PROFILE
3003 void perf_tpcounter_event(int event_id
)
3005 struct pt_regs
*regs
= get_irq_regs();
3008 regs
= task_pt_regs(current
);
3010 __perf_swcounter_event(PERF_TYPE_TRACEPOINT
, event_id
, 1, 1, regs
, 0);
3012 EXPORT_SYMBOL_GPL(perf_tpcounter_event
);
3014 extern int ftrace_profile_enable(int);
3015 extern void ftrace_profile_disable(int);
3017 static void tp_perf_counter_destroy(struct perf_counter
*counter
)
3019 ftrace_profile_disable(perf_event_id(&counter
->hw_event
));
3022 static const struct pmu
*tp_perf_counter_init(struct perf_counter
*counter
)
3024 int event_id
= perf_event_id(&counter
->hw_event
);
3027 ret
= ftrace_profile_enable(event_id
);
3031 counter
->destroy
= tp_perf_counter_destroy
;
3032 counter
->hw
.irq_period
= counter
->hw_event
.irq_period
;
3034 return &perf_ops_generic
;
3037 static const struct pmu
*tp_perf_counter_init(struct perf_counter
*counter
)
3043 static const struct pmu
*sw_perf_counter_init(struct perf_counter
*counter
)
3045 const struct pmu
*pmu
= NULL
;
3048 * Software counters (currently) can't in general distinguish
3049 * between user, kernel and hypervisor events.
3050 * However, context switches and cpu migrations are considered
3051 * to be kernel events, and page faults are never hypervisor
3054 switch (perf_event_id(&counter
->hw_event
)) {
3055 case PERF_COUNT_CPU_CLOCK
:
3056 pmu
= &perf_ops_cpu_clock
;
3059 case PERF_COUNT_TASK_CLOCK
:
3061 * If the user instantiates this as a per-cpu counter,
3062 * use the cpu_clock counter instead.
3064 if (counter
->ctx
->task
)
3065 pmu
= &perf_ops_task_clock
;
3067 pmu
= &perf_ops_cpu_clock
;
3070 case PERF_COUNT_PAGE_FAULTS
:
3071 case PERF_COUNT_PAGE_FAULTS_MIN
:
3072 case PERF_COUNT_PAGE_FAULTS_MAJ
:
3073 case PERF_COUNT_CONTEXT_SWITCHES
:
3074 pmu
= &perf_ops_generic
;
3076 case PERF_COUNT_CPU_MIGRATIONS
:
3077 if (!counter
->hw_event
.exclude_kernel
)
3078 pmu
= &perf_ops_cpu_migrations
;
3086 * Allocate and initialize a counter structure
3088 static struct perf_counter
*
3089 perf_counter_alloc(struct perf_counter_hw_event
*hw_event
,
3091 struct perf_counter_context
*ctx
,
3092 struct perf_counter
*group_leader
,
3095 const struct pmu
*pmu
;
3096 struct perf_counter
*counter
;
3097 struct hw_perf_counter
*hwc
;
3100 counter
= kzalloc(sizeof(*counter
), gfpflags
);
3102 return ERR_PTR(-ENOMEM
);
3105 * Single counters are their own group leaders, with an
3106 * empty sibling list:
3109 group_leader
= counter
;
3111 mutex_init(&counter
->child_mutex
);
3112 INIT_LIST_HEAD(&counter
->child_list
);
3114 INIT_LIST_HEAD(&counter
->list_entry
);
3115 INIT_LIST_HEAD(&counter
->event_entry
);
3116 INIT_LIST_HEAD(&counter
->sibling_list
);
3117 init_waitqueue_head(&counter
->waitq
);
3119 mutex_init(&counter
->mmap_mutex
);
3122 counter
->hw_event
= *hw_event
;
3123 counter
->group_leader
= group_leader
;
3124 counter
->pmu
= NULL
;
3128 counter
->state
= PERF_COUNTER_STATE_INACTIVE
;
3129 if (hw_event
->disabled
)
3130 counter
->state
= PERF_COUNTER_STATE_OFF
;
3135 if (hw_event
->freq
&& hw_event
->irq_freq
)
3136 hwc
->irq_period
= div64_u64(TICK_NSEC
, hw_event
->irq_freq
);
3138 hwc
->irq_period
= hw_event
->irq_period
;
3141 * we currently do not support PERF_RECORD_GROUP on inherited counters
3143 if (hw_event
->inherit
&& (hw_event
->record_type
& PERF_RECORD_GROUP
))
3146 if (perf_event_raw(hw_event
)) {
3147 pmu
= hw_perf_counter_init(counter
);
3151 switch (perf_event_type(hw_event
)) {
3152 case PERF_TYPE_HARDWARE
:
3153 pmu
= hw_perf_counter_init(counter
);
3156 case PERF_TYPE_SOFTWARE
:
3157 pmu
= sw_perf_counter_init(counter
);
3160 case PERF_TYPE_TRACEPOINT
:
3161 pmu
= tp_perf_counter_init(counter
);
3168 else if (IS_ERR(pmu
))
3173 return ERR_PTR(err
);
3178 atomic_inc(&nr_counters
);
3179 if (counter
->hw_event
.mmap
)
3180 atomic_inc(&nr_mmap_tracking
);
3181 if (counter
->hw_event
.munmap
)
3182 atomic_inc(&nr_munmap_tracking
);
3183 if (counter
->hw_event
.comm
)
3184 atomic_inc(&nr_comm_tracking
);
3190 * sys_perf_counter_open - open a performance counter, associate it to a task/cpu
3192 * @hw_event_uptr: event type attributes for monitoring/sampling
3195 * @group_fd: group leader counter fd
3197 SYSCALL_DEFINE5(perf_counter_open
,
3198 const struct perf_counter_hw_event __user
*, hw_event_uptr
,
3199 pid_t
, pid
, int, cpu
, int, group_fd
, unsigned long, flags
)
3201 struct perf_counter
*counter
, *group_leader
;
3202 struct perf_counter_hw_event hw_event
;
3203 struct perf_counter_context
*ctx
;
3204 struct file
*counter_file
= NULL
;
3205 struct file
*group_file
= NULL
;
3206 int fput_needed
= 0;
3207 int fput_needed2
= 0;
3210 /* for future expandability... */
3214 if (copy_from_user(&hw_event
, hw_event_uptr
, sizeof(hw_event
)) != 0)
3218 * Get the target context (task or percpu):
3220 ctx
= find_get_context(pid
, cpu
);
3222 return PTR_ERR(ctx
);
3225 * Look up the group leader (we will attach this counter to it):
3227 group_leader
= NULL
;
3228 if (group_fd
!= -1) {
3230 group_file
= fget_light(group_fd
, &fput_needed
);
3232 goto err_put_context
;
3233 if (group_file
->f_op
!= &perf_fops
)
3234 goto err_put_context
;
3236 group_leader
= group_file
->private_data
;
3238 * Do not allow a recursive hierarchy (this new sibling
3239 * becoming part of another group-sibling):
3241 if (group_leader
->group_leader
!= group_leader
)
3242 goto err_put_context
;
3244 * Do not allow to attach to a group in a different
3245 * task or CPU context:
3247 if (group_leader
->ctx
!= ctx
)
3248 goto err_put_context
;
3250 * Only a group leader can be exclusive or pinned
3252 if (hw_event
.exclusive
|| hw_event
.pinned
)
3253 goto err_put_context
;
3256 counter
= perf_counter_alloc(&hw_event
, cpu
, ctx
, group_leader
,
3258 ret
= PTR_ERR(counter
);
3259 if (IS_ERR(counter
))
3260 goto err_put_context
;
3262 ret
= anon_inode_getfd("[perf_counter]", &perf_fops
, counter
, 0);
3264 goto err_free_put_context
;
3266 counter_file
= fget_light(ret
, &fput_needed2
);
3268 goto err_free_put_context
;
3270 counter
->filp
= counter_file
;
3271 mutex_lock(&ctx
->mutex
);
3272 perf_install_in_context(ctx
, counter
, cpu
);
3273 mutex_unlock(&ctx
->mutex
);
3275 fput_light(counter_file
, fput_needed2
);
3278 fput_light(group_file
, fput_needed
);
3282 err_free_put_context
:
3292 * inherit a counter from parent task to child task:
3294 static struct perf_counter
*
3295 inherit_counter(struct perf_counter
*parent_counter
,
3296 struct task_struct
*parent
,
3297 struct perf_counter_context
*parent_ctx
,
3298 struct task_struct
*child
,
3299 struct perf_counter
*group_leader
,
3300 struct perf_counter_context
*child_ctx
)
3302 struct perf_counter
*child_counter
;
3305 * Instead of creating recursive hierarchies of counters,
3306 * we link inherited counters back to the original parent,
3307 * which has a filp for sure, which we use as the reference
3310 if (parent_counter
->parent
)
3311 parent_counter
= parent_counter
->parent
;
3313 child_counter
= perf_counter_alloc(&parent_counter
->hw_event
,
3314 parent_counter
->cpu
, child_ctx
,
3315 group_leader
, GFP_KERNEL
);
3316 if (IS_ERR(child_counter
))
3317 return child_counter
;
3320 * Make the child state follow the state of the parent counter,
3321 * not its hw_event.disabled bit. We hold the parent's mutex,
3322 * so we won't race with perf_counter_{en,dis}able_family.
3324 if (parent_counter
->state
>= PERF_COUNTER_STATE_INACTIVE
)
3325 child_counter
->state
= PERF_COUNTER_STATE_INACTIVE
;
3327 child_counter
->state
= PERF_COUNTER_STATE_OFF
;
3330 * Link it up in the child's context:
3332 add_counter_to_ctx(child_counter
, child_ctx
);
3334 child_counter
->parent
= parent_counter
;
3336 * inherit into child's child as well:
3338 child_counter
->hw_event
.inherit
= 1;
3341 * Get a reference to the parent filp - we will fput it
3342 * when the child counter exits. This is safe to do because
3343 * we are in the parent and we know that the filp still
3344 * exists and has a nonzero count:
3346 atomic_long_inc(&parent_counter
->filp
->f_count
);
3349 * Link this into the parent counter's child list
3351 mutex_lock(&parent_counter
->child_mutex
);
3352 list_add_tail(&child_counter
->child_list
, &parent_counter
->child_list
);
3353 mutex_unlock(&parent_counter
->child_mutex
);
3355 return child_counter
;
3358 static int inherit_group(struct perf_counter
*parent_counter
,
3359 struct task_struct
*parent
,
3360 struct perf_counter_context
*parent_ctx
,
3361 struct task_struct
*child
,
3362 struct perf_counter_context
*child_ctx
)
3364 struct perf_counter
*leader
;
3365 struct perf_counter
*sub
;
3366 struct perf_counter
*child_ctr
;
3368 leader
= inherit_counter(parent_counter
, parent
, parent_ctx
,
3369 child
, NULL
, child_ctx
);
3371 return PTR_ERR(leader
);
3372 list_for_each_entry(sub
, &parent_counter
->sibling_list
, list_entry
) {
3373 child_ctr
= inherit_counter(sub
, parent
, parent_ctx
,
3374 child
, leader
, child_ctx
);
3375 if (IS_ERR(child_ctr
))
3376 return PTR_ERR(child_ctr
);
3381 static void sync_child_counter(struct perf_counter
*child_counter
,
3382 struct perf_counter
*parent_counter
)
3386 child_val
= atomic64_read(&child_counter
->count
);
3389 * Add back the child's count to the parent's count:
3391 atomic64_add(child_val
, &parent_counter
->count
);
3392 atomic64_add(child_counter
->total_time_enabled
,
3393 &parent_counter
->child_total_time_enabled
);
3394 atomic64_add(child_counter
->total_time_running
,
3395 &parent_counter
->child_total_time_running
);
3398 * Remove this counter from the parent's list
3400 mutex_lock(&parent_counter
->child_mutex
);
3401 list_del_init(&child_counter
->child_list
);
3402 mutex_unlock(&parent_counter
->child_mutex
);
3405 * Release the parent counter, if this was the last
3408 fput(parent_counter
->filp
);
3412 __perf_counter_exit_task(struct task_struct
*child
,
3413 struct perf_counter
*child_counter
,
3414 struct perf_counter_context
*child_ctx
)
3416 struct perf_counter
*parent_counter
;
3418 update_counter_times(child_counter
);
3419 perf_counter_remove_from_context(child_counter
);
3421 parent_counter
= child_counter
->parent
;
3423 * It can happen that parent exits first, and has counters
3424 * that are still around due to the child reference. These
3425 * counters need to be zapped - but otherwise linger.
3427 if (parent_counter
) {
3428 sync_child_counter(child_counter
, parent_counter
);
3429 free_counter(child_counter
);
3434 * When a child task exits, feed back counter values to parent counters.
3436 * Note: we may be running in child context, but the PID is not hashed
3437 * anymore so new counters will not be added.
3438 * (XXX not sure that is true when we get called from flush_old_exec.
3441 void perf_counter_exit_task(struct task_struct
*child
)
3443 struct perf_counter
*child_counter
, *tmp
;
3444 struct perf_counter_context
*child_ctx
;
3445 unsigned long flags
;
3447 WARN_ON_ONCE(child
!= current
);
3449 child_ctx
= child
->perf_counter_ctxp
;
3451 if (likely(!child_ctx
))
3454 local_irq_save(flags
);
3455 __perf_counter_task_sched_out(child_ctx
);
3456 child
->perf_counter_ctxp
= NULL
;
3457 local_irq_restore(flags
);
3459 mutex_lock(&child_ctx
->mutex
);
3462 list_for_each_entry_safe(child_counter
, tmp
, &child_ctx
->counter_list
,
3464 __perf_counter_exit_task(child
, child_counter
, child_ctx
);
3467 * If the last counter was a group counter, it will have appended all
3468 * its siblings to the list, but we obtained 'tmp' before that which
3469 * will still point to the list head terminating the iteration.
3471 if (!list_empty(&child_ctx
->counter_list
))
3474 mutex_unlock(&child_ctx
->mutex
);
3480 * Initialize the perf_counter context in task_struct
3482 void perf_counter_init_task(struct task_struct
*child
)
3484 struct perf_counter_context
*child_ctx
, *parent_ctx
;
3485 struct perf_counter
*counter
;
3486 struct task_struct
*parent
= current
;
3487 int inherited_all
= 1;
3489 child
->perf_counter_ctxp
= NULL
;
3492 * This is executed from the parent task context, so inherit
3493 * counters that have been marked for cloning.
3494 * First allocate and initialize a context for the child.
3497 child_ctx
= kmalloc(sizeof(struct perf_counter_context
), GFP_KERNEL
);
3501 parent_ctx
= parent
->perf_counter_ctxp
;
3502 if (likely(!parent_ctx
|| !parent_ctx
->nr_counters
))
3505 __perf_counter_init_context(child_ctx
, child
);
3506 child
->perf_counter_ctxp
= child_ctx
;
3509 * Lock the parent list. No need to lock the child - not PID
3510 * hashed yet and not running, so nobody can access it.
3512 mutex_lock(&parent_ctx
->mutex
);
3515 * We dont have to disable NMIs - we are only looking at
3516 * the list, not manipulating it:
3518 list_for_each_entry_rcu(counter
, &parent_ctx
->event_list
, event_entry
) {
3519 if (counter
!= counter
->group_leader
)
3522 if (!counter
->hw_event
.inherit
) {
3527 if (inherit_group(counter
, parent
,
3528 parent_ctx
, child
, child_ctx
)) {
3534 if (inherited_all
) {
3536 * Mark the child context as a clone of the parent
3537 * context, or of whatever the parent is a clone of.
3539 if (parent_ctx
->parent_ctx
) {
3540 child_ctx
->parent_ctx
= parent_ctx
->parent_ctx
;
3541 child_ctx
->parent_gen
= parent_ctx
->parent_gen
;
3543 child_ctx
->parent_ctx
= parent_ctx
;
3544 child_ctx
->parent_gen
= parent_ctx
->generation
;
3546 get_ctx(child_ctx
->parent_ctx
);
3549 mutex_unlock(&parent_ctx
->mutex
);
3552 static void __cpuinit
perf_counter_init_cpu(int cpu
)
3554 struct perf_cpu_context
*cpuctx
;
3556 cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
3557 __perf_counter_init_context(&cpuctx
->ctx
, NULL
);
3559 spin_lock(&perf_resource_lock
);
3560 cpuctx
->max_pertask
= perf_max_counters
- perf_reserved_percpu
;
3561 spin_unlock(&perf_resource_lock
);
3563 hw_perf_counter_setup(cpu
);
3566 #ifdef CONFIG_HOTPLUG_CPU
3567 static void __perf_counter_exit_cpu(void *info
)
3569 struct perf_cpu_context
*cpuctx
= &__get_cpu_var(perf_cpu_context
);
3570 struct perf_counter_context
*ctx
= &cpuctx
->ctx
;
3571 struct perf_counter
*counter
, *tmp
;
3573 list_for_each_entry_safe(counter
, tmp
, &ctx
->counter_list
, list_entry
)
3574 __perf_counter_remove_from_context(counter
);
3576 static void perf_counter_exit_cpu(int cpu
)
3578 struct perf_cpu_context
*cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
3579 struct perf_counter_context
*ctx
= &cpuctx
->ctx
;
3581 mutex_lock(&ctx
->mutex
);
3582 smp_call_function_single(cpu
, __perf_counter_exit_cpu
, NULL
, 1);
3583 mutex_unlock(&ctx
->mutex
);
3586 static inline void perf_counter_exit_cpu(int cpu
) { }
3589 static int __cpuinit
3590 perf_cpu_notify(struct notifier_block
*self
, unsigned long action
, void *hcpu
)
3592 unsigned int cpu
= (long)hcpu
;
3596 case CPU_UP_PREPARE
:
3597 case CPU_UP_PREPARE_FROZEN
:
3598 perf_counter_init_cpu(cpu
);
3601 case CPU_DOWN_PREPARE
:
3602 case CPU_DOWN_PREPARE_FROZEN
:
3603 perf_counter_exit_cpu(cpu
);
3613 static struct notifier_block __cpuinitdata perf_cpu_nb
= {
3614 .notifier_call
= perf_cpu_notify
,
3617 void __init
perf_counter_init(void)
3619 perf_cpu_notify(&perf_cpu_nb
, (unsigned long)CPU_UP_PREPARE
,
3620 (void *)(long)smp_processor_id());
3621 register_cpu_notifier(&perf_cpu_nb
);
3624 static ssize_t
perf_show_reserve_percpu(struct sysdev_class
*class, char *buf
)
3626 return sprintf(buf
, "%d\n", perf_reserved_percpu
);
3630 perf_set_reserve_percpu(struct sysdev_class
*class,
3634 struct perf_cpu_context
*cpuctx
;
3638 err
= strict_strtoul(buf
, 10, &val
);
3641 if (val
> perf_max_counters
)
3644 spin_lock(&perf_resource_lock
);
3645 perf_reserved_percpu
= val
;
3646 for_each_online_cpu(cpu
) {
3647 cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
3648 spin_lock_irq(&cpuctx
->ctx
.lock
);
3649 mpt
= min(perf_max_counters
- cpuctx
->ctx
.nr_counters
,
3650 perf_max_counters
- perf_reserved_percpu
);
3651 cpuctx
->max_pertask
= mpt
;
3652 spin_unlock_irq(&cpuctx
->ctx
.lock
);
3654 spin_unlock(&perf_resource_lock
);
3659 static ssize_t
perf_show_overcommit(struct sysdev_class
*class, char *buf
)
3661 return sprintf(buf
, "%d\n", perf_overcommit
);
3665 perf_set_overcommit(struct sysdev_class
*class, const char *buf
, size_t count
)
3670 err
= strict_strtoul(buf
, 10, &val
);
3676 spin_lock(&perf_resource_lock
);
3677 perf_overcommit
= val
;
3678 spin_unlock(&perf_resource_lock
);
3683 static SYSDEV_CLASS_ATTR(
3686 perf_show_reserve_percpu
,
3687 perf_set_reserve_percpu
3690 static SYSDEV_CLASS_ATTR(
3693 perf_show_overcommit
,
3697 static struct attribute
*perfclass_attrs
[] = {
3698 &attr_reserve_percpu
.attr
,
3699 &attr_overcommit
.attr
,
3703 static struct attribute_group perfclass_attr_group
= {
3704 .attrs
= perfclass_attrs
,
3705 .name
= "perf_counters",
3708 static int __init
perf_counter_sysfs_init(void)
3710 return sysfs_create_group(&cpu_sysdev_class
.kset
.kobj
,
3711 &perfclass_attr_group
);
3713 device_initcall(perf_counter_sysfs_init
);