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
;
47 * perf counter paranoia level:
49 * 1 - disallow cpu counters to unpriv
50 * 2 - disallow kernel profiling to unpriv
52 int sysctl_perf_counter_paranoid __read_mostly
;
54 static inline bool perf_paranoid_cpu(void)
56 return sysctl_perf_counter_paranoid
> 0;
59 static inline bool perf_paranoid_kernel(void)
61 return sysctl_perf_counter_paranoid
> 1;
64 int sysctl_perf_counter_mlock __read_mostly
= 512; /* 'free' kb per user */
67 * max perf counter sample rate
69 int sysctl_perf_counter_sample_rate __read_mostly
= 100000;
71 static atomic64_t perf_counter_id
;
74 * Lock for (sysadmin-configurable) counter reservations:
76 static DEFINE_SPINLOCK(perf_resource_lock
);
79 * Architecture provided APIs - weak aliases:
81 extern __weak
const struct pmu
*hw_perf_counter_init(struct perf_counter
*counter
)
86 void __weak
hw_perf_disable(void) { barrier(); }
87 void __weak
hw_perf_enable(void) { barrier(); }
89 void __weak
hw_perf_counter_setup(int cpu
) { barrier(); }
92 hw_perf_group_sched_in(struct perf_counter
*group_leader
,
93 struct perf_cpu_context
*cpuctx
,
94 struct perf_counter_context
*ctx
, int cpu
)
99 void __weak
perf_counter_print_debug(void) { }
101 static DEFINE_PER_CPU(int, disable_count
);
103 void __perf_disable(void)
105 __get_cpu_var(disable_count
)++;
108 bool __perf_enable(void)
110 return !--__get_cpu_var(disable_count
);
113 void perf_disable(void)
119 void perf_enable(void)
125 static void get_ctx(struct perf_counter_context
*ctx
)
127 WARN_ON(!atomic_inc_not_zero(&ctx
->refcount
));
130 static void free_ctx(struct rcu_head
*head
)
132 struct perf_counter_context
*ctx
;
134 ctx
= container_of(head
, struct perf_counter_context
, rcu_head
);
138 static void put_ctx(struct perf_counter_context
*ctx
)
140 if (atomic_dec_and_test(&ctx
->refcount
)) {
142 put_ctx(ctx
->parent_ctx
);
144 put_task_struct(ctx
->task
);
145 call_rcu(&ctx
->rcu_head
, free_ctx
);
149 static void unclone_ctx(struct perf_counter_context
*ctx
)
151 if (ctx
->parent_ctx
) {
152 put_ctx(ctx
->parent_ctx
);
153 ctx
->parent_ctx
= NULL
;
158 * If we inherit counters we want to return the parent counter id
161 static u64
primary_counter_id(struct perf_counter
*counter
)
163 u64 id
= counter
->id
;
166 id
= counter
->parent
->id
;
172 * Get the perf_counter_context for a task and lock it.
173 * This has to cope with with the fact that until it is locked,
174 * the context could get moved to another task.
176 static struct perf_counter_context
*
177 perf_lock_task_context(struct task_struct
*task
, unsigned long *flags
)
179 struct perf_counter_context
*ctx
;
183 ctx
= rcu_dereference(task
->perf_counter_ctxp
);
186 * If this context is a clone of another, it might
187 * get swapped for another underneath us by
188 * perf_counter_task_sched_out, though the
189 * rcu_read_lock() protects us from any context
190 * getting freed. Lock the context and check if it
191 * got swapped before we could get the lock, and retry
192 * if so. If we locked the right context, then it
193 * can't get swapped on us any more.
195 spin_lock_irqsave(&ctx
->lock
, *flags
);
196 if (ctx
!= rcu_dereference(task
->perf_counter_ctxp
)) {
197 spin_unlock_irqrestore(&ctx
->lock
, *flags
);
201 if (!atomic_inc_not_zero(&ctx
->refcount
)) {
202 spin_unlock_irqrestore(&ctx
->lock
, *flags
);
211 * Get the context for a task and increment its pin_count so it
212 * can't get swapped to another task. This also increments its
213 * reference count so that the context can't get freed.
215 static struct perf_counter_context
*perf_pin_task_context(struct task_struct
*task
)
217 struct perf_counter_context
*ctx
;
220 ctx
= perf_lock_task_context(task
, &flags
);
223 spin_unlock_irqrestore(&ctx
->lock
, flags
);
228 static void perf_unpin_context(struct perf_counter_context
*ctx
)
232 spin_lock_irqsave(&ctx
->lock
, flags
);
234 spin_unlock_irqrestore(&ctx
->lock
, flags
);
239 * Add a counter from the lists for its context.
240 * Must be called with ctx->mutex and ctx->lock held.
243 list_add_counter(struct perf_counter
*counter
, struct perf_counter_context
*ctx
)
245 struct perf_counter
*group_leader
= counter
->group_leader
;
248 * Depending on whether it is a standalone or sibling counter,
249 * add it straight to the context's counter list, or to the group
250 * leader's sibling list:
252 if (group_leader
== counter
)
253 list_add_tail(&counter
->list_entry
, &ctx
->counter_list
);
255 list_add_tail(&counter
->list_entry
, &group_leader
->sibling_list
);
256 group_leader
->nr_siblings
++;
259 list_add_rcu(&counter
->event_entry
, &ctx
->event_list
);
261 if (counter
->attr
.inherit_stat
)
266 * Remove a counter from the lists for its context.
267 * Must be called with ctx->mutex and ctx->lock held.
270 list_del_counter(struct perf_counter
*counter
, struct perf_counter_context
*ctx
)
272 struct perf_counter
*sibling
, *tmp
;
274 if (list_empty(&counter
->list_entry
))
277 if (counter
->attr
.inherit_stat
)
280 list_del_init(&counter
->list_entry
);
281 list_del_rcu(&counter
->event_entry
);
283 if (counter
->group_leader
!= counter
)
284 counter
->group_leader
->nr_siblings
--;
287 * If this was a group counter with sibling counters then
288 * upgrade the siblings to singleton counters by adding them
289 * to the context list directly:
291 list_for_each_entry_safe(sibling
, tmp
,
292 &counter
->sibling_list
, list_entry
) {
294 list_move_tail(&sibling
->list_entry
, &ctx
->counter_list
);
295 sibling
->group_leader
= sibling
;
300 counter_sched_out(struct perf_counter
*counter
,
301 struct perf_cpu_context
*cpuctx
,
302 struct perf_counter_context
*ctx
)
304 if (counter
->state
!= PERF_COUNTER_STATE_ACTIVE
)
307 counter
->state
= PERF_COUNTER_STATE_INACTIVE
;
308 counter
->tstamp_stopped
= ctx
->time
;
309 counter
->pmu
->disable(counter
);
312 if (!is_software_counter(counter
))
313 cpuctx
->active_oncpu
--;
315 if (counter
->attr
.exclusive
|| !cpuctx
->active_oncpu
)
316 cpuctx
->exclusive
= 0;
320 group_sched_out(struct perf_counter
*group_counter
,
321 struct perf_cpu_context
*cpuctx
,
322 struct perf_counter_context
*ctx
)
324 struct perf_counter
*counter
;
326 if (group_counter
->state
!= PERF_COUNTER_STATE_ACTIVE
)
329 counter_sched_out(group_counter
, cpuctx
, ctx
);
332 * Schedule out siblings (if any):
334 list_for_each_entry(counter
, &group_counter
->sibling_list
, list_entry
)
335 counter_sched_out(counter
, cpuctx
, ctx
);
337 if (group_counter
->attr
.exclusive
)
338 cpuctx
->exclusive
= 0;
342 * Cross CPU call to remove a performance counter
344 * We disable the counter on the hardware level first. After that we
345 * remove it from the context list.
347 static void __perf_counter_remove_from_context(void *info
)
349 struct perf_cpu_context
*cpuctx
= &__get_cpu_var(perf_cpu_context
);
350 struct perf_counter
*counter
= info
;
351 struct perf_counter_context
*ctx
= counter
->ctx
;
354 * If this is a task context, we need to check whether it is
355 * the current task context of this cpu. If not it has been
356 * scheduled out before the smp call arrived.
358 if (ctx
->task
&& cpuctx
->task_ctx
!= ctx
)
361 spin_lock(&ctx
->lock
);
363 * Protect the list operation against NMI by disabling the
364 * counters on a global level.
368 counter_sched_out(counter
, cpuctx
, ctx
);
370 list_del_counter(counter
, ctx
);
374 * Allow more per task counters with respect to the
377 cpuctx
->max_pertask
=
378 min(perf_max_counters
- ctx
->nr_counters
,
379 perf_max_counters
- perf_reserved_percpu
);
383 spin_unlock(&ctx
->lock
);
388 * Remove the counter from a task's (or a CPU's) list of counters.
390 * Must be called with ctx->mutex held.
392 * CPU counters are removed with a smp call. For task counters we only
393 * call when the task is on a CPU.
395 * If counter->ctx is a cloned context, callers must make sure that
396 * every task struct that counter->ctx->task could possibly point to
397 * remains valid. This is OK when called from perf_release since
398 * that only calls us on the top-level context, which can't be a clone.
399 * When called from perf_counter_exit_task, it's OK because the
400 * context has been detached from its task.
402 static void perf_counter_remove_from_context(struct perf_counter
*counter
)
404 struct perf_counter_context
*ctx
= counter
->ctx
;
405 struct task_struct
*task
= ctx
->task
;
409 * Per cpu counters are removed via an smp call and
410 * the removal is always sucessful.
412 smp_call_function_single(counter
->cpu
,
413 __perf_counter_remove_from_context
,
419 task_oncpu_function_call(task
, __perf_counter_remove_from_context
,
422 spin_lock_irq(&ctx
->lock
);
424 * If the context is active we need to retry the smp call.
426 if (ctx
->nr_active
&& !list_empty(&counter
->list_entry
)) {
427 spin_unlock_irq(&ctx
->lock
);
432 * The lock prevents that this context is scheduled in so we
433 * can remove the counter safely, if the call above did not
436 if (!list_empty(&counter
->list_entry
)) {
437 list_del_counter(counter
, ctx
);
439 spin_unlock_irq(&ctx
->lock
);
442 static inline u64
perf_clock(void)
444 return cpu_clock(smp_processor_id());
448 * Update the record of the current time in a context.
450 static void update_context_time(struct perf_counter_context
*ctx
)
452 u64 now
= perf_clock();
454 ctx
->time
+= now
- ctx
->timestamp
;
455 ctx
->timestamp
= now
;
459 * Update the total_time_enabled and total_time_running fields for a counter.
461 static void update_counter_times(struct perf_counter
*counter
)
463 struct perf_counter_context
*ctx
= counter
->ctx
;
466 if (counter
->state
< PERF_COUNTER_STATE_INACTIVE
)
469 counter
->total_time_enabled
= ctx
->time
- counter
->tstamp_enabled
;
471 if (counter
->state
== PERF_COUNTER_STATE_INACTIVE
)
472 run_end
= counter
->tstamp_stopped
;
476 counter
->total_time_running
= run_end
- counter
->tstamp_running
;
480 * Update total_time_enabled and total_time_running for all counters in a group.
482 static void update_group_times(struct perf_counter
*leader
)
484 struct perf_counter
*counter
;
486 update_counter_times(leader
);
487 list_for_each_entry(counter
, &leader
->sibling_list
, list_entry
)
488 update_counter_times(counter
);
492 * Cross CPU call to disable a performance counter
494 static void __perf_counter_disable(void *info
)
496 struct perf_counter
*counter
= info
;
497 struct perf_cpu_context
*cpuctx
= &__get_cpu_var(perf_cpu_context
);
498 struct perf_counter_context
*ctx
= counter
->ctx
;
501 * If this is a per-task counter, need to check whether this
502 * counter's task is the current task on this cpu.
504 if (ctx
->task
&& cpuctx
->task_ctx
!= ctx
)
507 spin_lock(&ctx
->lock
);
510 * If the counter is on, turn it off.
511 * If it is in error state, leave it in error state.
513 if (counter
->state
>= PERF_COUNTER_STATE_INACTIVE
) {
514 update_context_time(ctx
);
515 update_counter_times(counter
);
516 if (counter
== counter
->group_leader
)
517 group_sched_out(counter
, cpuctx
, ctx
);
519 counter_sched_out(counter
, cpuctx
, ctx
);
520 counter
->state
= PERF_COUNTER_STATE_OFF
;
523 spin_unlock(&ctx
->lock
);
529 * If counter->ctx is a cloned context, callers must make sure that
530 * every task struct that counter->ctx->task could possibly point to
531 * remains valid. This condition is satisifed when called through
532 * perf_counter_for_each_child or perf_counter_for_each because they
533 * hold the top-level counter's child_mutex, so any descendant that
534 * goes to exit will block in sync_child_counter.
535 * When called from perf_pending_counter it's OK because counter->ctx
536 * is the current context on this CPU and preemption is disabled,
537 * hence we can't get into perf_counter_task_sched_out for this context.
539 static void perf_counter_disable(struct perf_counter
*counter
)
541 struct perf_counter_context
*ctx
= counter
->ctx
;
542 struct task_struct
*task
= ctx
->task
;
546 * Disable the counter on the cpu that it's on
548 smp_call_function_single(counter
->cpu
, __perf_counter_disable
,
554 task_oncpu_function_call(task
, __perf_counter_disable
, counter
);
556 spin_lock_irq(&ctx
->lock
);
558 * If the counter is still active, we need to retry the cross-call.
560 if (counter
->state
== PERF_COUNTER_STATE_ACTIVE
) {
561 spin_unlock_irq(&ctx
->lock
);
566 * Since we have the lock this context can't be scheduled
567 * in, so we can change the state safely.
569 if (counter
->state
== PERF_COUNTER_STATE_INACTIVE
) {
570 update_counter_times(counter
);
571 counter
->state
= PERF_COUNTER_STATE_OFF
;
574 spin_unlock_irq(&ctx
->lock
);
578 counter_sched_in(struct perf_counter
*counter
,
579 struct perf_cpu_context
*cpuctx
,
580 struct perf_counter_context
*ctx
,
583 if (counter
->state
<= PERF_COUNTER_STATE_OFF
)
586 counter
->state
= PERF_COUNTER_STATE_ACTIVE
;
587 counter
->oncpu
= cpu
; /* TODO: put 'cpu' into cpuctx->cpu */
589 * The new state must be visible before we turn it on in the hardware:
593 if (counter
->pmu
->enable(counter
)) {
594 counter
->state
= PERF_COUNTER_STATE_INACTIVE
;
599 counter
->tstamp_running
+= ctx
->time
- counter
->tstamp_stopped
;
601 if (!is_software_counter(counter
))
602 cpuctx
->active_oncpu
++;
605 if (counter
->attr
.exclusive
)
606 cpuctx
->exclusive
= 1;
612 group_sched_in(struct perf_counter
*group_counter
,
613 struct perf_cpu_context
*cpuctx
,
614 struct perf_counter_context
*ctx
,
617 struct perf_counter
*counter
, *partial_group
;
620 if (group_counter
->state
== PERF_COUNTER_STATE_OFF
)
623 ret
= hw_perf_group_sched_in(group_counter
, cpuctx
, ctx
, cpu
);
625 return ret
< 0 ? ret
: 0;
627 if (counter_sched_in(group_counter
, cpuctx
, ctx
, cpu
))
631 * Schedule in siblings as one group (if any):
633 list_for_each_entry(counter
, &group_counter
->sibling_list
, list_entry
) {
634 if (counter_sched_in(counter
, cpuctx
, ctx
, cpu
)) {
635 partial_group
= counter
;
644 * Groups can be scheduled in as one unit only, so undo any
645 * partial group before returning:
647 list_for_each_entry(counter
, &group_counter
->sibling_list
, list_entry
) {
648 if (counter
== partial_group
)
650 counter_sched_out(counter
, cpuctx
, ctx
);
652 counter_sched_out(group_counter
, cpuctx
, ctx
);
658 * Return 1 for a group consisting entirely of software counters,
659 * 0 if the group contains any hardware counters.
661 static int is_software_only_group(struct perf_counter
*leader
)
663 struct perf_counter
*counter
;
665 if (!is_software_counter(leader
))
668 list_for_each_entry(counter
, &leader
->sibling_list
, list_entry
)
669 if (!is_software_counter(counter
))
676 * Work out whether we can put this counter group on the CPU now.
678 static int group_can_go_on(struct perf_counter
*counter
,
679 struct perf_cpu_context
*cpuctx
,
683 * Groups consisting entirely of software counters can always go on.
685 if (is_software_only_group(counter
))
688 * If an exclusive group is already on, no other hardware
689 * counters can go on.
691 if (cpuctx
->exclusive
)
694 * If this group is exclusive and there are already
695 * counters on the CPU, it can't go on.
697 if (counter
->attr
.exclusive
&& cpuctx
->active_oncpu
)
700 * Otherwise, try to add it if all previous groups were able
706 static void add_counter_to_ctx(struct perf_counter
*counter
,
707 struct perf_counter_context
*ctx
)
709 list_add_counter(counter
, ctx
);
710 counter
->tstamp_enabled
= ctx
->time
;
711 counter
->tstamp_running
= ctx
->time
;
712 counter
->tstamp_stopped
= ctx
->time
;
716 * Cross CPU call to install and enable a performance counter
718 * Must be called with ctx->mutex held
720 static void __perf_install_in_context(void *info
)
722 struct perf_cpu_context
*cpuctx
= &__get_cpu_var(perf_cpu_context
);
723 struct perf_counter
*counter
= info
;
724 struct perf_counter_context
*ctx
= counter
->ctx
;
725 struct perf_counter
*leader
= counter
->group_leader
;
726 int cpu
= smp_processor_id();
730 * If this is a task context, we need to check whether it is
731 * the current task context of this cpu. If not it has been
732 * scheduled out before the smp call arrived.
733 * Or possibly this is the right context but it isn't
734 * on this cpu because it had no counters.
736 if (ctx
->task
&& cpuctx
->task_ctx
!= ctx
) {
737 if (cpuctx
->task_ctx
|| ctx
->task
!= current
)
739 cpuctx
->task_ctx
= ctx
;
742 spin_lock(&ctx
->lock
);
744 update_context_time(ctx
);
747 * Protect the list operation against NMI by disabling the
748 * counters on a global level. NOP for non NMI based counters.
752 add_counter_to_ctx(counter
, ctx
);
755 * Don't put the counter on if it is disabled or if
756 * it is in a group and the group isn't on.
758 if (counter
->state
!= PERF_COUNTER_STATE_INACTIVE
||
759 (leader
!= counter
&& leader
->state
!= PERF_COUNTER_STATE_ACTIVE
))
763 * An exclusive counter can't go on if there are already active
764 * hardware counters, and no hardware counter can go on if there
765 * is already an exclusive counter on.
767 if (!group_can_go_on(counter
, cpuctx
, 1))
770 err
= counter_sched_in(counter
, cpuctx
, ctx
, cpu
);
774 * This counter couldn't go on. If it is in a group
775 * then we have to pull the whole group off.
776 * If the counter group is pinned then put it in error state.
778 if (leader
!= counter
)
779 group_sched_out(leader
, cpuctx
, ctx
);
780 if (leader
->attr
.pinned
) {
781 update_group_times(leader
);
782 leader
->state
= PERF_COUNTER_STATE_ERROR
;
786 if (!err
&& !ctx
->task
&& cpuctx
->max_pertask
)
787 cpuctx
->max_pertask
--;
792 spin_unlock(&ctx
->lock
);
796 * Attach a performance counter to a context
798 * First we add the counter to the list with the hardware enable bit
799 * in counter->hw_config cleared.
801 * If the counter is attached to a task which is on a CPU we use a smp
802 * call to enable it in the task context. The task might have been
803 * scheduled away, but we check this in the smp call again.
805 * Must be called with ctx->mutex held.
808 perf_install_in_context(struct perf_counter_context
*ctx
,
809 struct perf_counter
*counter
,
812 struct task_struct
*task
= ctx
->task
;
816 * Per cpu counters are installed via an smp call and
817 * the install is always sucessful.
819 smp_call_function_single(cpu
, __perf_install_in_context
,
825 task_oncpu_function_call(task
, __perf_install_in_context
,
828 spin_lock_irq(&ctx
->lock
);
830 * we need to retry the smp call.
832 if (ctx
->is_active
&& list_empty(&counter
->list_entry
)) {
833 spin_unlock_irq(&ctx
->lock
);
838 * The lock prevents that this context is scheduled in so we
839 * can add the counter safely, if it the call above did not
842 if (list_empty(&counter
->list_entry
))
843 add_counter_to_ctx(counter
, ctx
);
844 spin_unlock_irq(&ctx
->lock
);
848 * Cross CPU call to enable a performance counter
850 static void __perf_counter_enable(void *info
)
852 struct perf_counter
*counter
= info
;
853 struct perf_cpu_context
*cpuctx
= &__get_cpu_var(perf_cpu_context
);
854 struct perf_counter_context
*ctx
= counter
->ctx
;
855 struct perf_counter
*leader
= counter
->group_leader
;
859 * If this is a per-task counter, need to check whether this
860 * counter's task is the current task on this cpu.
862 if (ctx
->task
&& cpuctx
->task_ctx
!= ctx
) {
863 if (cpuctx
->task_ctx
|| ctx
->task
!= current
)
865 cpuctx
->task_ctx
= ctx
;
868 spin_lock(&ctx
->lock
);
870 update_context_time(ctx
);
872 if (counter
->state
>= PERF_COUNTER_STATE_INACTIVE
)
874 counter
->state
= PERF_COUNTER_STATE_INACTIVE
;
875 counter
->tstamp_enabled
= ctx
->time
- counter
->total_time_enabled
;
878 * If the counter is in a group and isn't the group leader,
879 * then don't put it on unless the group is on.
881 if (leader
!= counter
&& leader
->state
!= PERF_COUNTER_STATE_ACTIVE
)
884 if (!group_can_go_on(counter
, cpuctx
, 1)) {
888 if (counter
== leader
)
889 err
= group_sched_in(counter
, cpuctx
, ctx
,
892 err
= counter_sched_in(counter
, cpuctx
, ctx
,
899 * If this counter can't go on and it's part of a
900 * group, then the whole group has to come off.
902 if (leader
!= counter
)
903 group_sched_out(leader
, cpuctx
, ctx
);
904 if (leader
->attr
.pinned
) {
905 update_group_times(leader
);
906 leader
->state
= PERF_COUNTER_STATE_ERROR
;
911 spin_unlock(&ctx
->lock
);
917 * If counter->ctx is a cloned context, callers must make sure that
918 * every task struct that counter->ctx->task could possibly point to
919 * remains valid. This condition is satisfied when called through
920 * perf_counter_for_each_child or perf_counter_for_each as described
921 * for perf_counter_disable.
923 static void perf_counter_enable(struct perf_counter
*counter
)
925 struct perf_counter_context
*ctx
= counter
->ctx
;
926 struct task_struct
*task
= ctx
->task
;
930 * Enable the counter on the cpu that it's on
932 smp_call_function_single(counter
->cpu
, __perf_counter_enable
,
937 spin_lock_irq(&ctx
->lock
);
938 if (counter
->state
>= PERF_COUNTER_STATE_INACTIVE
)
942 * If the counter is in error state, clear that first.
943 * That way, if we see the counter in error state below, we
944 * know that it has gone back into error state, as distinct
945 * from the task having been scheduled away before the
946 * cross-call arrived.
948 if (counter
->state
== PERF_COUNTER_STATE_ERROR
)
949 counter
->state
= PERF_COUNTER_STATE_OFF
;
952 spin_unlock_irq(&ctx
->lock
);
953 task_oncpu_function_call(task
, __perf_counter_enable
, counter
);
955 spin_lock_irq(&ctx
->lock
);
958 * If the context is active and the counter is still off,
959 * we need to retry the cross-call.
961 if (ctx
->is_active
&& counter
->state
== PERF_COUNTER_STATE_OFF
)
965 * Since we have the lock this context can't be scheduled
966 * in, so we can change the state safely.
968 if (counter
->state
== PERF_COUNTER_STATE_OFF
) {
969 counter
->state
= PERF_COUNTER_STATE_INACTIVE
;
970 counter
->tstamp_enabled
=
971 ctx
->time
- counter
->total_time_enabled
;
974 spin_unlock_irq(&ctx
->lock
);
977 static int perf_counter_refresh(struct perf_counter
*counter
, int refresh
)
980 * not supported on inherited counters
982 if (counter
->attr
.inherit
)
985 atomic_add(refresh
, &counter
->event_limit
);
986 perf_counter_enable(counter
);
991 void __perf_counter_sched_out(struct perf_counter_context
*ctx
,
992 struct perf_cpu_context
*cpuctx
)
994 struct perf_counter
*counter
;
996 spin_lock(&ctx
->lock
);
998 if (likely(!ctx
->nr_counters
))
1000 update_context_time(ctx
);
1003 if (ctx
->nr_active
) {
1004 list_for_each_entry(counter
, &ctx
->counter_list
, list_entry
) {
1005 if (counter
!= counter
->group_leader
)
1006 counter_sched_out(counter
, cpuctx
, ctx
);
1008 group_sched_out(counter
, cpuctx
, ctx
);
1013 spin_unlock(&ctx
->lock
);
1017 * Test whether two contexts are equivalent, i.e. whether they
1018 * have both been cloned from the same version of the same context
1019 * and they both have the same number of enabled counters.
1020 * If the number of enabled counters is the same, then the set
1021 * of enabled counters should be the same, because these are both
1022 * inherited contexts, therefore we can't access individual counters
1023 * in them directly with an fd; we can only enable/disable all
1024 * counters via prctl, or enable/disable all counters in a family
1025 * via ioctl, which will have the same effect on both contexts.
1027 static int context_equiv(struct perf_counter_context
*ctx1
,
1028 struct perf_counter_context
*ctx2
)
1030 return ctx1
->parent_ctx
&& ctx1
->parent_ctx
== ctx2
->parent_ctx
1031 && ctx1
->parent_gen
== ctx2
->parent_gen
1032 && !ctx1
->pin_count
&& !ctx2
->pin_count
;
1035 static void __perf_counter_read(void *counter
);
1037 static void __perf_counter_sync_stat(struct perf_counter
*counter
,
1038 struct perf_counter
*next_counter
)
1042 if (!counter
->attr
.inherit_stat
)
1046 * Update the counter value, we cannot use perf_counter_read()
1047 * because we're in the middle of a context switch and have IRQs
1048 * disabled, which upsets smp_call_function_single(), however
1049 * we know the counter must be on the current CPU, therefore we
1050 * don't need to use it.
1052 switch (counter
->state
) {
1053 case PERF_COUNTER_STATE_ACTIVE
:
1054 __perf_counter_read(counter
);
1057 case PERF_COUNTER_STATE_INACTIVE
:
1058 update_counter_times(counter
);
1066 * In order to keep per-task stats reliable we need to flip the counter
1067 * values when we flip the contexts.
1069 value
= atomic64_read(&next_counter
->count
);
1070 value
= atomic64_xchg(&counter
->count
, value
);
1071 atomic64_set(&next_counter
->count
, value
);
1073 swap(counter
->total_time_enabled
, next_counter
->total_time_enabled
);
1074 swap(counter
->total_time_running
, next_counter
->total_time_running
);
1077 * Since we swizzled the values, update the user visible data too.
1079 perf_counter_update_userpage(counter
);
1080 perf_counter_update_userpage(next_counter
);
1083 #define list_next_entry(pos, member) \
1084 list_entry(pos->member.next, typeof(*pos), member)
1086 static void perf_counter_sync_stat(struct perf_counter_context
*ctx
,
1087 struct perf_counter_context
*next_ctx
)
1089 struct perf_counter
*counter
, *next_counter
;
1094 counter
= list_first_entry(&ctx
->event_list
,
1095 struct perf_counter
, event_entry
);
1097 next_counter
= list_first_entry(&next_ctx
->event_list
,
1098 struct perf_counter
, event_entry
);
1100 while (&counter
->event_entry
!= &ctx
->event_list
&&
1101 &next_counter
->event_entry
!= &next_ctx
->event_list
) {
1103 __perf_counter_sync_stat(counter
, next_counter
);
1105 counter
= list_next_entry(counter
, event_entry
);
1106 next_counter
= list_next_entry(counter
, event_entry
);
1111 * Called from scheduler to remove the counters of the current task,
1112 * with interrupts disabled.
1114 * We stop each counter and update the counter value in counter->count.
1116 * This does not protect us against NMI, but disable()
1117 * sets the disabled bit in the control field of counter _before_
1118 * accessing the counter control register. If a NMI hits, then it will
1119 * not restart the counter.
1121 void perf_counter_task_sched_out(struct task_struct
*task
,
1122 struct task_struct
*next
, int cpu
)
1124 struct perf_cpu_context
*cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
1125 struct perf_counter_context
*ctx
= task
->perf_counter_ctxp
;
1126 struct perf_counter_context
*next_ctx
;
1127 struct perf_counter_context
*parent
;
1128 struct pt_regs
*regs
;
1131 regs
= task_pt_regs(task
);
1132 perf_swcounter_event(PERF_COUNT_SW_CONTEXT_SWITCHES
, 1, 1, regs
, 0);
1134 if (likely(!ctx
|| !cpuctx
->task_ctx
))
1137 update_context_time(ctx
);
1140 parent
= rcu_dereference(ctx
->parent_ctx
);
1141 next_ctx
= next
->perf_counter_ctxp
;
1142 if (parent
&& next_ctx
&&
1143 rcu_dereference(next_ctx
->parent_ctx
) == parent
) {
1145 * Looks like the two contexts are clones, so we might be
1146 * able to optimize the context switch. We lock both
1147 * contexts and check that they are clones under the
1148 * lock (including re-checking that neither has been
1149 * uncloned in the meantime). It doesn't matter which
1150 * order we take the locks because no other cpu could
1151 * be trying to lock both of these tasks.
1153 spin_lock(&ctx
->lock
);
1154 spin_lock_nested(&next_ctx
->lock
, SINGLE_DEPTH_NESTING
);
1155 if (context_equiv(ctx
, next_ctx
)) {
1157 * XXX do we need a memory barrier of sorts
1158 * wrt to rcu_dereference() of perf_counter_ctxp
1160 task
->perf_counter_ctxp
= next_ctx
;
1161 next
->perf_counter_ctxp
= ctx
;
1163 next_ctx
->task
= task
;
1166 perf_counter_sync_stat(ctx
, next_ctx
);
1168 spin_unlock(&next_ctx
->lock
);
1169 spin_unlock(&ctx
->lock
);
1174 __perf_counter_sched_out(ctx
, cpuctx
);
1175 cpuctx
->task_ctx
= NULL
;
1180 * Called with IRQs disabled
1182 static void __perf_counter_task_sched_out(struct perf_counter_context
*ctx
)
1184 struct perf_cpu_context
*cpuctx
= &__get_cpu_var(perf_cpu_context
);
1186 if (!cpuctx
->task_ctx
)
1189 if (WARN_ON_ONCE(ctx
!= cpuctx
->task_ctx
))
1192 __perf_counter_sched_out(ctx
, cpuctx
);
1193 cpuctx
->task_ctx
= NULL
;
1197 * Called with IRQs disabled
1199 static void perf_counter_cpu_sched_out(struct perf_cpu_context
*cpuctx
)
1201 __perf_counter_sched_out(&cpuctx
->ctx
, cpuctx
);
1205 __perf_counter_sched_in(struct perf_counter_context
*ctx
,
1206 struct perf_cpu_context
*cpuctx
, int cpu
)
1208 struct perf_counter
*counter
;
1211 spin_lock(&ctx
->lock
);
1213 if (likely(!ctx
->nr_counters
))
1216 ctx
->timestamp
= perf_clock();
1221 * First go through the list and put on any pinned groups
1222 * in order to give them the best chance of going on.
1224 list_for_each_entry(counter
, &ctx
->counter_list
, list_entry
) {
1225 if (counter
->state
<= PERF_COUNTER_STATE_OFF
||
1226 !counter
->attr
.pinned
)
1228 if (counter
->cpu
!= -1 && counter
->cpu
!= cpu
)
1231 if (counter
!= counter
->group_leader
)
1232 counter_sched_in(counter
, cpuctx
, ctx
, cpu
);
1234 if (group_can_go_on(counter
, cpuctx
, 1))
1235 group_sched_in(counter
, cpuctx
, ctx
, cpu
);
1239 * If this pinned group hasn't been scheduled,
1240 * put it in error state.
1242 if (counter
->state
== PERF_COUNTER_STATE_INACTIVE
) {
1243 update_group_times(counter
);
1244 counter
->state
= PERF_COUNTER_STATE_ERROR
;
1248 list_for_each_entry(counter
, &ctx
->counter_list
, list_entry
) {
1250 * Ignore counters in OFF or ERROR state, and
1251 * ignore pinned counters since we did them already.
1253 if (counter
->state
<= PERF_COUNTER_STATE_OFF
||
1254 counter
->attr
.pinned
)
1258 * Listen to the 'cpu' scheduling filter constraint
1261 if (counter
->cpu
!= -1 && counter
->cpu
!= cpu
)
1264 if (counter
!= counter
->group_leader
) {
1265 if (counter_sched_in(counter
, cpuctx
, ctx
, cpu
))
1268 if (group_can_go_on(counter
, cpuctx
, can_add_hw
)) {
1269 if (group_sched_in(counter
, cpuctx
, ctx
, cpu
))
1276 spin_unlock(&ctx
->lock
);
1280 * Called from scheduler to add the counters of the current task
1281 * with interrupts disabled.
1283 * We restore the counter value and then enable it.
1285 * This does not protect us against NMI, but enable()
1286 * sets the enabled bit in the control field of counter _before_
1287 * accessing the counter control register. If a NMI hits, then it will
1288 * keep the counter running.
1290 void perf_counter_task_sched_in(struct task_struct
*task
, int cpu
)
1292 struct perf_cpu_context
*cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
1293 struct perf_counter_context
*ctx
= task
->perf_counter_ctxp
;
1297 if (cpuctx
->task_ctx
== ctx
)
1299 __perf_counter_sched_in(ctx
, cpuctx
, cpu
);
1300 cpuctx
->task_ctx
= ctx
;
1303 static void perf_counter_cpu_sched_in(struct perf_cpu_context
*cpuctx
, int cpu
)
1305 struct perf_counter_context
*ctx
= &cpuctx
->ctx
;
1307 __perf_counter_sched_in(ctx
, cpuctx
, cpu
);
1310 #define MAX_INTERRUPTS (~0ULL)
1312 static void perf_log_throttle(struct perf_counter
*counter
, int enable
);
1314 static void perf_adjust_period(struct perf_counter
*counter
, u64 events
)
1316 struct hw_perf_counter
*hwc
= &counter
->hw
;
1317 u64 period
, sample_period
;
1320 events
*= hwc
->sample_period
;
1321 period
= div64_u64(events
, counter
->attr
.sample_freq
);
1323 delta
= (s64
)(period
- hwc
->sample_period
);
1324 delta
= (delta
+ 7) / 8; /* low pass filter */
1326 sample_period
= hwc
->sample_period
+ delta
;
1331 hwc
->sample_period
= sample_period
;
1334 static void perf_ctx_adjust_freq(struct perf_counter_context
*ctx
)
1336 struct perf_counter
*counter
;
1337 struct hw_perf_counter
*hwc
;
1338 u64 interrupts
, freq
;
1340 spin_lock(&ctx
->lock
);
1341 list_for_each_entry(counter
, &ctx
->counter_list
, list_entry
) {
1342 if (counter
->state
!= PERF_COUNTER_STATE_ACTIVE
)
1347 interrupts
= hwc
->interrupts
;
1348 hwc
->interrupts
= 0;
1351 * unthrottle counters on the tick
1353 if (interrupts
== MAX_INTERRUPTS
) {
1354 perf_log_throttle(counter
, 1);
1355 counter
->pmu
->unthrottle(counter
);
1356 interrupts
= 2*sysctl_perf_counter_sample_rate
/HZ
;
1359 if (!counter
->attr
.freq
|| !counter
->attr
.sample_freq
)
1363 * if the specified freq < HZ then we need to skip ticks
1365 if (counter
->attr
.sample_freq
< HZ
) {
1366 freq
= counter
->attr
.sample_freq
;
1368 hwc
->freq_count
+= freq
;
1369 hwc
->freq_interrupts
+= interrupts
;
1371 if (hwc
->freq_count
< HZ
)
1374 interrupts
= hwc
->freq_interrupts
;
1375 hwc
->freq_interrupts
= 0;
1376 hwc
->freq_count
-= HZ
;
1380 perf_adjust_period(counter
, freq
* interrupts
);
1383 * In order to avoid being stalled by an (accidental) huge
1384 * sample period, force reset the sample period if we didn't
1385 * get any events in this freq period.
1389 counter
->pmu
->disable(counter
);
1390 atomic64_set(&hwc
->period_left
, 0);
1391 counter
->pmu
->enable(counter
);
1395 spin_unlock(&ctx
->lock
);
1399 * Round-robin a context's counters:
1401 static void rotate_ctx(struct perf_counter_context
*ctx
)
1403 struct perf_counter
*counter
;
1405 if (!ctx
->nr_counters
)
1408 spin_lock(&ctx
->lock
);
1410 * Rotate the first entry last (works just fine for group counters too):
1413 list_for_each_entry(counter
, &ctx
->counter_list
, list_entry
) {
1414 list_move_tail(&counter
->list_entry
, &ctx
->counter_list
);
1419 spin_unlock(&ctx
->lock
);
1422 void perf_counter_task_tick(struct task_struct
*curr
, int cpu
)
1424 struct perf_cpu_context
*cpuctx
;
1425 struct perf_counter_context
*ctx
;
1427 if (!atomic_read(&nr_counters
))
1430 cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
1431 ctx
= curr
->perf_counter_ctxp
;
1433 perf_ctx_adjust_freq(&cpuctx
->ctx
);
1435 perf_ctx_adjust_freq(ctx
);
1437 perf_counter_cpu_sched_out(cpuctx
);
1439 __perf_counter_task_sched_out(ctx
);
1441 rotate_ctx(&cpuctx
->ctx
);
1445 perf_counter_cpu_sched_in(cpuctx
, cpu
);
1447 perf_counter_task_sched_in(curr
, cpu
);
1451 * Enable all of a task's counters that have been marked enable-on-exec.
1452 * This expects task == current.
1454 static void perf_counter_enable_on_exec(struct task_struct
*task
)
1456 struct perf_counter_context
*ctx
;
1457 struct perf_counter
*counter
;
1458 unsigned long flags
;
1461 local_irq_save(flags
);
1462 ctx
= task
->perf_counter_ctxp
;
1463 if (!ctx
|| !ctx
->nr_counters
)
1466 __perf_counter_task_sched_out(ctx
);
1468 spin_lock(&ctx
->lock
);
1470 list_for_each_entry(counter
, &ctx
->counter_list
, list_entry
) {
1471 if (!counter
->attr
.enable_on_exec
)
1473 counter
->attr
.enable_on_exec
= 0;
1474 if (counter
->state
>= PERF_COUNTER_STATE_INACTIVE
)
1476 counter
->state
= PERF_COUNTER_STATE_INACTIVE
;
1477 counter
->tstamp_enabled
=
1478 ctx
->time
- counter
->total_time_enabled
;
1483 * Unclone this context if we enabled any counter.
1488 spin_unlock(&ctx
->lock
);
1490 perf_counter_task_sched_in(task
, smp_processor_id());
1492 local_irq_restore(flags
);
1496 * Cross CPU call to read the hardware counter
1498 static void __perf_counter_read(void *info
)
1500 struct perf_counter
*counter
= info
;
1501 struct perf_counter_context
*ctx
= counter
->ctx
;
1502 unsigned long flags
;
1504 local_irq_save(flags
);
1506 update_context_time(ctx
);
1507 counter
->pmu
->read(counter
);
1508 update_counter_times(counter
);
1509 local_irq_restore(flags
);
1512 static u64
perf_counter_read(struct perf_counter
*counter
)
1515 * If counter is enabled and currently active on a CPU, update the
1516 * value in the counter structure:
1518 if (counter
->state
== PERF_COUNTER_STATE_ACTIVE
) {
1519 smp_call_function_single(counter
->oncpu
,
1520 __perf_counter_read
, counter
, 1);
1521 } else if (counter
->state
== PERF_COUNTER_STATE_INACTIVE
) {
1522 update_counter_times(counter
);
1525 return atomic64_read(&counter
->count
);
1529 * Initialize the perf_counter context in a task_struct:
1532 __perf_counter_init_context(struct perf_counter_context
*ctx
,
1533 struct task_struct
*task
)
1535 memset(ctx
, 0, sizeof(*ctx
));
1536 spin_lock_init(&ctx
->lock
);
1537 mutex_init(&ctx
->mutex
);
1538 INIT_LIST_HEAD(&ctx
->counter_list
);
1539 INIT_LIST_HEAD(&ctx
->event_list
);
1540 atomic_set(&ctx
->refcount
, 1);
1544 static struct perf_counter_context
*find_get_context(pid_t pid
, int cpu
)
1546 struct perf_counter_context
*ctx
;
1547 struct perf_cpu_context
*cpuctx
;
1548 struct task_struct
*task
;
1549 unsigned long flags
;
1553 * If cpu is not a wildcard then this is a percpu counter:
1556 /* Must be root to operate on a CPU counter: */
1557 if (perf_paranoid_cpu() && !capable(CAP_SYS_ADMIN
))
1558 return ERR_PTR(-EACCES
);
1560 if (cpu
< 0 || cpu
> num_possible_cpus())
1561 return ERR_PTR(-EINVAL
);
1564 * We could be clever and allow to attach a counter to an
1565 * offline CPU and activate it when the CPU comes up, but
1568 if (!cpu_isset(cpu
, cpu_online_map
))
1569 return ERR_PTR(-ENODEV
);
1571 cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
1582 task
= find_task_by_vpid(pid
);
1584 get_task_struct(task
);
1588 return ERR_PTR(-ESRCH
);
1591 * Can't attach counters to a dying task.
1594 if (task
->flags
& PF_EXITING
)
1597 /* Reuse ptrace permission checks for now. */
1599 if (!ptrace_may_access(task
, PTRACE_MODE_READ
))
1603 ctx
= perf_lock_task_context(task
, &flags
);
1606 spin_unlock_irqrestore(&ctx
->lock
, flags
);
1610 ctx
= kmalloc(sizeof(struct perf_counter_context
), GFP_KERNEL
);
1614 __perf_counter_init_context(ctx
, task
);
1616 if (cmpxchg(&task
->perf_counter_ctxp
, NULL
, ctx
)) {
1618 * We raced with some other task; use
1619 * the context they set.
1624 get_task_struct(task
);
1627 put_task_struct(task
);
1631 put_task_struct(task
);
1632 return ERR_PTR(err
);
1635 static void free_counter_rcu(struct rcu_head
*head
)
1637 struct perf_counter
*counter
;
1639 counter
= container_of(head
, struct perf_counter
, rcu_head
);
1641 put_pid_ns(counter
->ns
);
1645 static void perf_pending_sync(struct perf_counter
*counter
);
1647 static void free_counter(struct perf_counter
*counter
)
1649 perf_pending_sync(counter
);
1651 if (!counter
->parent
) {
1652 atomic_dec(&nr_counters
);
1653 if (counter
->attr
.mmap
)
1654 atomic_dec(&nr_mmap_counters
);
1655 if (counter
->attr
.comm
)
1656 atomic_dec(&nr_comm_counters
);
1659 if (counter
->destroy
)
1660 counter
->destroy(counter
);
1662 put_ctx(counter
->ctx
);
1663 call_rcu(&counter
->rcu_head
, free_counter_rcu
);
1667 * Called when the last reference to the file is gone.
1669 static int perf_release(struct inode
*inode
, struct file
*file
)
1671 struct perf_counter
*counter
= file
->private_data
;
1672 struct perf_counter_context
*ctx
= counter
->ctx
;
1674 file
->private_data
= NULL
;
1676 WARN_ON_ONCE(ctx
->parent_ctx
);
1677 mutex_lock(&ctx
->mutex
);
1678 perf_counter_remove_from_context(counter
);
1679 mutex_unlock(&ctx
->mutex
);
1681 mutex_lock(&counter
->owner
->perf_counter_mutex
);
1682 list_del_init(&counter
->owner_entry
);
1683 mutex_unlock(&counter
->owner
->perf_counter_mutex
);
1684 put_task_struct(counter
->owner
);
1686 free_counter(counter
);
1692 * Read the performance counter - simple non blocking version for now
1695 perf_read_hw(struct perf_counter
*counter
, char __user
*buf
, size_t count
)
1701 * Return end-of-file for a read on a counter that is in
1702 * error state (i.e. because it was pinned but it couldn't be
1703 * scheduled on to the CPU at some point).
1705 if (counter
->state
== PERF_COUNTER_STATE_ERROR
)
1708 WARN_ON_ONCE(counter
->ctx
->parent_ctx
);
1709 mutex_lock(&counter
->child_mutex
);
1710 values
[0] = perf_counter_read(counter
);
1712 if (counter
->attr
.read_format
& PERF_FORMAT_TOTAL_TIME_ENABLED
)
1713 values
[n
++] = counter
->total_time_enabled
+
1714 atomic64_read(&counter
->child_total_time_enabled
);
1715 if (counter
->attr
.read_format
& PERF_FORMAT_TOTAL_TIME_RUNNING
)
1716 values
[n
++] = counter
->total_time_running
+
1717 atomic64_read(&counter
->child_total_time_running
);
1718 if (counter
->attr
.read_format
& PERF_FORMAT_ID
)
1719 values
[n
++] = primary_counter_id(counter
);
1720 mutex_unlock(&counter
->child_mutex
);
1722 if (count
< n
* sizeof(u64
))
1724 count
= n
* sizeof(u64
);
1726 if (copy_to_user(buf
, values
, count
))
1733 perf_read(struct file
*file
, char __user
*buf
, size_t count
, loff_t
*ppos
)
1735 struct perf_counter
*counter
= file
->private_data
;
1737 return perf_read_hw(counter
, buf
, count
);
1740 static unsigned int perf_poll(struct file
*file
, poll_table
*wait
)
1742 struct perf_counter
*counter
= file
->private_data
;
1743 struct perf_mmap_data
*data
;
1744 unsigned int events
= POLL_HUP
;
1747 data
= rcu_dereference(counter
->data
);
1749 events
= atomic_xchg(&data
->poll
, 0);
1752 poll_wait(file
, &counter
->waitq
, wait
);
1757 static void perf_counter_reset(struct perf_counter
*counter
)
1759 (void)perf_counter_read(counter
);
1760 atomic64_set(&counter
->count
, 0);
1761 perf_counter_update_userpage(counter
);
1765 * Holding the top-level counter's child_mutex means that any
1766 * descendant process that has inherited this counter will block
1767 * in sync_child_counter if it goes to exit, thus satisfying the
1768 * task existence requirements of perf_counter_enable/disable.
1770 static void perf_counter_for_each_child(struct perf_counter
*counter
,
1771 void (*func
)(struct perf_counter
*))
1773 struct perf_counter
*child
;
1775 WARN_ON_ONCE(counter
->ctx
->parent_ctx
);
1776 mutex_lock(&counter
->child_mutex
);
1778 list_for_each_entry(child
, &counter
->child_list
, child_list
)
1780 mutex_unlock(&counter
->child_mutex
);
1783 static void perf_counter_for_each(struct perf_counter
*counter
,
1784 void (*func
)(struct perf_counter
*))
1786 struct perf_counter_context
*ctx
= counter
->ctx
;
1787 struct perf_counter
*sibling
;
1789 WARN_ON_ONCE(ctx
->parent_ctx
);
1790 mutex_lock(&ctx
->mutex
);
1791 counter
= counter
->group_leader
;
1793 perf_counter_for_each_child(counter
, func
);
1795 list_for_each_entry(sibling
, &counter
->sibling_list
, list_entry
)
1796 perf_counter_for_each_child(counter
, func
);
1797 mutex_unlock(&ctx
->mutex
);
1800 static int perf_counter_period(struct perf_counter
*counter
, u64 __user
*arg
)
1802 struct perf_counter_context
*ctx
= counter
->ctx
;
1807 if (!counter
->attr
.sample_period
)
1810 size
= copy_from_user(&value
, arg
, sizeof(value
));
1811 if (size
!= sizeof(value
))
1817 spin_lock_irq(&ctx
->lock
);
1818 if (counter
->attr
.freq
) {
1819 if (value
> sysctl_perf_counter_sample_rate
) {
1824 counter
->attr
.sample_freq
= value
;
1826 counter
->attr
.sample_period
= value
;
1827 counter
->hw
.sample_period
= value
;
1830 spin_unlock_irq(&ctx
->lock
);
1835 static long perf_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
1837 struct perf_counter
*counter
= file
->private_data
;
1838 void (*func
)(struct perf_counter
*);
1842 case PERF_COUNTER_IOC_ENABLE
:
1843 func
= perf_counter_enable
;
1845 case PERF_COUNTER_IOC_DISABLE
:
1846 func
= perf_counter_disable
;
1848 case PERF_COUNTER_IOC_RESET
:
1849 func
= perf_counter_reset
;
1852 case PERF_COUNTER_IOC_REFRESH
:
1853 return perf_counter_refresh(counter
, arg
);
1855 case PERF_COUNTER_IOC_PERIOD
:
1856 return perf_counter_period(counter
, (u64 __user
*)arg
);
1862 if (flags
& PERF_IOC_FLAG_GROUP
)
1863 perf_counter_for_each(counter
, func
);
1865 perf_counter_for_each_child(counter
, func
);
1870 int perf_counter_task_enable(void)
1872 struct perf_counter
*counter
;
1874 mutex_lock(¤t
->perf_counter_mutex
);
1875 list_for_each_entry(counter
, ¤t
->perf_counter_list
, owner_entry
)
1876 perf_counter_for_each_child(counter
, perf_counter_enable
);
1877 mutex_unlock(¤t
->perf_counter_mutex
);
1882 int perf_counter_task_disable(void)
1884 struct perf_counter
*counter
;
1886 mutex_lock(¤t
->perf_counter_mutex
);
1887 list_for_each_entry(counter
, ¤t
->perf_counter_list
, owner_entry
)
1888 perf_counter_for_each_child(counter
, perf_counter_disable
);
1889 mutex_unlock(¤t
->perf_counter_mutex
);
1894 static int perf_counter_index(struct perf_counter
*counter
)
1896 if (counter
->state
!= PERF_COUNTER_STATE_ACTIVE
)
1899 return counter
->hw
.idx
+ 1 - PERF_COUNTER_INDEX_OFFSET
;
1903 * Callers need to ensure there can be no nesting of this function, otherwise
1904 * the seqlock logic goes bad. We can not serialize this because the arch
1905 * code calls this from NMI context.
1907 void perf_counter_update_userpage(struct perf_counter
*counter
)
1909 struct perf_counter_mmap_page
*userpg
;
1910 struct perf_mmap_data
*data
;
1913 data
= rcu_dereference(counter
->data
);
1917 userpg
= data
->user_page
;
1920 * Disable preemption so as to not let the corresponding user-space
1921 * spin too long if we get preempted.
1926 userpg
->index
= perf_counter_index(counter
);
1927 userpg
->offset
= atomic64_read(&counter
->count
);
1928 if (counter
->state
== PERF_COUNTER_STATE_ACTIVE
)
1929 userpg
->offset
-= atomic64_read(&counter
->hw
.prev_count
);
1931 userpg
->time_enabled
= counter
->total_time_enabled
+
1932 atomic64_read(&counter
->child_total_time_enabled
);
1934 userpg
->time_running
= counter
->total_time_running
+
1935 atomic64_read(&counter
->child_total_time_running
);
1944 static int perf_mmap_fault(struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
1946 struct perf_counter
*counter
= vma
->vm_file
->private_data
;
1947 struct perf_mmap_data
*data
;
1948 int ret
= VM_FAULT_SIGBUS
;
1950 if (vmf
->flags
& FAULT_FLAG_MKWRITE
) {
1951 if (vmf
->pgoff
== 0)
1957 data
= rcu_dereference(counter
->data
);
1961 if (vmf
->pgoff
== 0) {
1962 vmf
->page
= virt_to_page(data
->user_page
);
1964 int nr
= vmf
->pgoff
- 1;
1966 if ((unsigned)nr
> data
->nr_pages
)
1969 if (vmf
->flags
& FAULT_FLAG_WRITE
)
1972 vmf
->page
= virt_to_page(data
->data_pages
[nr
]);
1975 get_page(vmf
->page
);
1976 vmf
->page
->mapping
= vma
->vm_file
->f_mapping
;
1977 vmf
->page
->index
= vmf
->pgoff
;
1986 static int perf_mmap_data_alloc(struct perf_counter
*counter
, int nr_pages
)
1988 struct perf_mmap_data
*data
;
1992 WARN_ON(atomic_read(&counter
->mmap_count
));
1994 size
= sizeof(struct perf_mmap_data
);
1995 size
+= nr_pages
* sizeof(void *);
1997 data
= kzalloc(size
, GFP_KERNEL
);
2001 data
->user_page
= (void *)get_zeroed_page(GFP_KERNEL
);
2002 if (!data
->user_page
)
2003 goto fail_user_page
;
2005 for (i
= 0; i
< nr_pages
; i
++) {
2006 data
->data_pages
[i
] = (void *)get_zeroed_page(GFP_KERNEL
);
2007 if (!data
->data_pages
[i
])
2008 goto fail_data_pages
;
2011 data
->nr_pages
= nr_pages
;
2012 atomic_set(&data
->lock
, -1);
2014 rcu_assign_pointer(counter
->data
, data
);
2019 for (i
--; i
>= 0; i
--)
2020 free_page((unsigned long)data
->data_pages
[i
]);
2022 free_page((unsigned long)data
->user_page
);
2031 static void perf_mmap_free_page(unsigned long addr
)
2033 struct page
*page
= virt_to_page(addr
);
2035 page
->mapping
= NULL
;
2039 static void __perf_mmap_data_free(struct rcu_head
*rcu_head
)
2041 struct perf_mmap_data
*data
;
2044 data
= container_of(rcu_head
, struct perf_mmap_data
, rcu_head
);
2046 perf_mmap_free_page((unsigned long)data
->user_page
);
2047 for (i
= 0; i
< data
->nr_pages
; i
++)
2048 perf_mmap_free_page((unsigned long)data
->data_pages
[i
]);
2053 static void perf_mmap_data_free(struct perf_counter
*counter
)
2055 struct perf_mmap_data
*data
= counter
->data
;
2057 WARN_ON(atomic_read(&counter
->mmap_count
));
2059 rcu_assign_pointer(counter
->data
, NULL
);
2060 call_rcu(&data
->rcu_head
, __perf_mmap_data_free
);
2063 static void perf_mmap_open(struct vm_area_struct
*vma
)
2065 struct perf_counter
*counter
= vma
->vm_file
->private_data
;
2067 atomic_inc(&counter
->mmap_count
);
2070 static void perf_mmap_close(struct vm_area_struct
*vma
)
2072 struct perf_counter
*counter
= vma
->vm_file
->private_data
;
2074 WARN_ON_ONCE(counter
->ctx
->parent_ctx
);
2075 if (atomic_dec_and_mutex_lock(&counter
->mmap_count
, &counter
->mmap_mutex
)) {
2076 struct user_struct
*user
= current_user();
2078 atomic_long_sub(counter
->data
->nr_pages
+ 1, &user
->locked_vm
);
2079 vma
->vm_mm
->locked_vm
-= counter
->data
->nr_locked
;
2080 perf_mmap_data_free(counter
);
2081 mutex_unlock(&counter
->mmap_mutex
);
2085 static struct vm_operations_struct perf_mmap_vmops
= {
2086 .open
= perf_mmap_open
,
2087 .close
= perf_mmap_close
,
2088 .fault
= perf_mmap_fault
,
2089 .page_mkwrite
= perf_mmap_fault
,
2092 static int perf_mmap(struct file
*file
, struct vm_area_struct
*vma
)
2094 struct perf_counter
*counter
= file
->private_data
;
2095 unsigned long user_locked
, user_lock_limit
;
2096 struct user_struct
*user
= current_user();
2097 unsigned long locked
, lock_limit
;
2098 unsigned long vma_size
;
2099 unsigned long nr_pages
;
2100 long user_extra
, extra
;
2103 if (!(vma
->vm_flags
& VM_SHARED
))
2106 vma_size
= vma
->vm_end
- vma
->vm_start
;
2107 nr_pages
= (vma_size
/ PAGE_SIZE
) - 1;
2110 * If we have data pages ensure they're a power-of-two number, so we
2111 * can do bitmasks instead of modulo.
2113 if (nr_pages
!= 0 && !is_power_of_2(nr_pages
))
2116 if (vma_size
!= PAGE_SIZE
* (1 + nr_pages
))
2119 if (vma
->vm_pgoff
!= 0)
2122 WARN_ON_ONCE(counter
->ctx
->parent_ctx
);
2123 mutex_lock(&counter
->mmap_mutex
);
2124 if (atomic_inc_not_zero(&counter
->mmap_count
)) {
2125 if (nr_pages
!= counter
->data
->nr_pages
)
2130 user_extra
= nr_pages
+ 1;
2131 user_lock_limit
= sysctl_perf_counter_mlock
>> (PAGE_SHIFT
- 10);
2134 * Increase the limit linearly with more CPUs:
2136 user_lock_limit
*= num_online_cpus();
2138 user_locked
= atomic_long_read(&user
->locked_vm
) + user_extra
;
2141 if (user_locked
> user_lock_limit
)
2142 extra
= user_locked
- user_lock_limit
;
2144 lock_limit
= current
->signal
->rlim
[RLIMIT_MEMLOCK
].rlim_cur
;
2145 lock_limit
>>= PAGE_SHIFT
;
2146 locked
= vma
->vm_mm
->locked_vm
+ extra
;
2148 if ((locked
> lock_limit
) && !capable(CAP_IPC_LOCK
)) {
2153 WARN_ON(counter
->data
);
2154 ret
= perf_mmap_data_alloc(counter
, nr_pages
);
2158 atomic_set(&counter
->mmap_count
, 1);
2159 atomic_long_add(user_extra
, &user
->locked_vm
);
2160 vma
->vm_mm
->locked_vm
+= extra
;
2161 counter
->data
->nr_locked
= extra
;
2162 if (vma
->vm_flags
& VM_WRITE
)
2163 counter
->data
->writable
= 1;
2166 mutex_unlock(&counter
->mmap_mutex
);
2168 vma
->vm_flags
|= VM_RESERVED
;
2169 vma
->vm_ops
= &perf_mmap_vmops
;
2174 static int perf_fasync(int fd
, struct file
*filp
, int on
)
2176 struct inode
*inode
= filp
->f_path
.dentry
->d_inode
;
2177 struct perf_counter
*counter
= filp
->private_data
;
2180 mutex_lock(&inode
->i_mutex
);
2181 retval
= fasync_helper(fd
, filp
, on
, &counter
->fasync
);
2182 mutex_unlock(&inode
->i_mutex
);
2190 static const struct file_operations perf_fops
= {
2191 .release
= perf_release
,
2194 .unlocked_ioctl
= perf_ioctl
,
2195 .compat_ioctl
= perf_ioctl
,
2197 .fasync
= perf_fasync
,
2201 * Perf counter wakeup
2203 * If there's data, ensure we set the poll() state and publish everything
2204 * to user-space before waking everybody up.
2207 void perf_counter_wakeup(struct perf_counter
*counter
)
2209 wake_up_all(&counter
->waitq
);
2211 if (counter
->pending_kill
) {
2212 kill_fasync(&counter
->fasync
, SIGIO
, counter
->pending_kill
);
2213 counter
->pending_kill
= 0;
2220 * Handle the case where we need to wakeup up from NMI (or rq->lock) context.
2222 * The NMI bit means we cannot possibly take locks. Therefore, maintain a
2223 * single linked list and use cmpxchg() to add entries lockless.
2226 static void perf_pending_counter(struct perf_pending_entry
*entry
)
2228 struct perf_counter
*counter
= container_of(entry
,
2229 struct perf_counter
, pending
);
2231 if (counter
->pending_disable
) {
2232 counter
->pending_disable
= 0;
2233 perf_counter_disable(counter
);
2236 if (counter
->pending_wakeup
) {
2237 counter
->pending_wakeup
= 0;
2238 perf_counter_wakeup(counter
);
2242 #define PENDING_TAIL ((struct perf_pending_entry *)-1UL)
2244 static DEFINE_PER_CPU(struct perf_pending_entry
*, perf_pending_head
) = {
2248 static void perf_pending_queue(struct perf_pending_entry
*entry
,
2249 void (*func
)(struct perf_pending_entry
*))
2251 struct perf_pending_entry
**head
;
2253 if (cmpxchg(&entry
->next
, NULL
, PENDING_TAIL
) != NULL
)
2258 head
= &get_cpu_var(perf_pending_head
);
2261 entry
->next
= *head
;
2262 } while (cmpxchg(head
, entry
->next
, entry
) != entry
->next
);
2264 set_perf_counter_pending();
2266 put_cpu_var(perf_pending_head
);
2269 static int __perf_pending_run(void)
2271 struct perf_pending_entry
*list
;
2274 list
= xchg(&__get_cpu_var(perf_pending_head
), PENDING_TAIL
);
2275 while (list
!= PENDING_TAIL
) {
2276 void (*func
)(struct perf_pending_entry
*);
2277 struct perf_pending_entry
*entry
= list
;
2284 * Ensure we observe the unqueue before we issue the wakeup,
2285 * so that we won't be waiting forever.
2286 * -- see perf_not_pending().
2297 static inline int perf_not_pending(struct perf_counter
*counter
)
2300 * If we flush on whatever cpu we run, there is a chance we don't
2304 __perf_pending_run();
2308 * Ensure we see the proper queue state before going to sleep
2309 * so that we do not miss the wakeup. -- see perf_pending_handle()
2312 return counter
->pending
.next
== NULL
;
2315 static void perf_pending_sync(struct perf_counter
*counter
)
2317 wait_event(counter
->waitq
, perf_not_pending(counter
));
2320 void perf_counter_do_pending(void)
2322 __perf_pending_run();
2326 * Callchain support -- arch specific
2329 __weak
struct perf_callchain_entry
*perf_callchain(struct pt_regs
*regs
)
2338 struct perf_output_handle
{
2339 struct perf_counter
*counter
;
2340 struct perf_mmap_data
*data
;
2342 unsigned long offset
;
2346 unsigned long flags
;
2349 static bool perf_output_space(struct perf_mmap_data
*data
,
2350 unsigned int offset
, unsigned int head
)
2355 if (!data
->writable
)
2358 mask
= (data
->nr_pages
<< PAGE_SHIFT
) - 1;
2360 * Userspace could choose to issue a mb() before updating the tail
2361 * pointer. So that all reads will be completed before the write is
2364 tail
= ACCESS_ONCE(data
->user_page
->data_tail
);
2367 offset
= (offset
- tail
) & mask
;
2368 head
= (head
- tail
) & mask
;
2370 if ((int)(head
- offset
) < 0)
2376 static void perf_output_wakeup(struct perf_output_handle
*handle
)
2378 atomic_set(&handle
->data
->poll
, POLL_IN
);
2381 handle
->counter
->pending_wakeup
= 1;
2382 perf_pending_queue(&handle
->counter
->pending
,
2383 perf_pending_counter
);
2385 perf_counter_wakeup(handle
->counter
);
2389 * Curious locking construct.
2391 * We need to ensure a later event doesn't publish a head when a former
2392 * event isn't done writing. However since we need to deal with NMIs we
2393 * cannot fully serialize things.
2395 * What we do is serialize between CPUs so we only have to deal with NMI
2396 * nesting on a single CPU.
2398 * We only publish the head (and generate a wakeup) when the outer-most
2401 static void perf_output_lock(struct perf_output_handle
*handle
)
2403 struct perf_mmap_data
*data
= handle
->data
;
2408 local_irq_save(handle
->flags
);
2409 cpu
= smp_processor_id();
2411 if (in_nmi() && atomic_read(&data
->lock
) == cpu
)
2414 while (atomic_cmpxchg(&data
->lock
, -1, cpu
) != -1)
2420 static void perf_output_unlock(struct perf_output_handle
*handle
)
2422 struct perf_mmap_data
*data
= handle
->data
;
2426 data
->done_head
= data
->head
;
2428 if (!handle
->locked
)
2433 * The xchg implies a full barrier that ensures all writes are done
2434 * before we publish the new head, matched by a rmb() in userspace when
2435 * reading this position.
2437 while ((head
= atomic_long_xchg(&data
->done_head
, 0)))
2438 data
->user_page
->data_head
= head
;
2441 * NMI can happen here, which means we can miss a done_head update.
2444 cpu
= atomic_xchg(&data
->lock
, -1);
2445 WARN_ON_ONCE(cpu
!= smp_processor_id());
2448 * Therefore we have to validate we did not indeed do so.
2450 if (unlikely(atomic_long_read(&data
->done_head
))) {
2452 * Since we had it locked, we can lock it again.
2454 while (atomic_cmpxchg(&data
->lock
, -1, cpu
) != -1)
2460 if (atomic_xchg(&data
->wakeup
, 0))
2461 perf_output_wakeup(handle
);
2463 local_irq_restore(handle
->flags
);
2466 static void perf_output_copy(struct perf_output_handle
*handle
,
2467 const void *buf
, unsigned int len
)
2469 unsigned int pages_mask
;
2470 unsigned int offset
;
2474 offset
= handle
->offset
;
2475 pages_mask
= handle
->data
->nr_pages
- 1;
2476 pages
= handle
->data
->data_pages
;
2479 unsigned int page_offset
;
2482 nr
= (offset
>> PAGE_SHIFT
) & pages_mask
;
2483 page_offset
= offset
& (PAGE_SIZE
- 1);
2484 size
= min_t(unsigned int, PAGE_SIZE
- page_offset
, len
);
2486 memcpy(pages
[nr
] + page_offset
, buf
, size
);
2493 handle
->offset
= offset
;
2496 * Check we didn't copy past our reservation window, taking the
2497 * possible unsigned int wrap into account.
2499 WARN_ON_ONCE(((long)(handle
->head
- handle
->offset
)) < 0);
2502 #define perf_output_put(handle, x) \
2503 perf_output_copy((handle), &(x), sizeof(x))
2505 static int perf_output_begin(struct perf_output_handle
*handle
,
2506 struct perf_counter
*counter
, unsigned int size
,
2507 int nmi
, int sample
)
2509 struct perf_mmap_data
*data
;
2510 unsigned int offset
, head
;
2513 struct perf_event_header header
;
2519 * For inherited counters we send all the output towards the parent.
2521 if (counter
->parent
)
2522 counter
= counter
->parent
;
2525 data
= rcu_dereference(counter
->data
);
2529 handle
->data
= data
;
2530 handle
->counter
= counter
;
2532 handle
->sample
= sample
;
2534 if (!data
->nr_pages
)
2537 have_lost
= atomic_read(&data
->lost
);
2539 size
+= sizeof(lost_event
);
2541 perf_output_lock(handle
);
2544 offset
= head
= atomic_long_read(&data
->head
);
2546 if (unlikely(!perf_output_space(data
, offset
, head
)))
2548 } while (atomic_long_cmpxchg(&data
->head
, offset
, head
) != offset
);
2550 handle
->offset
= offset
;
2551 handle
->head
= head
;
2553 if ((offset
>> PAGE_SHIFT
) != (head
>> PAGE_SHIFT
))
2554 atomic_set(&data
->wakeup
, 1);
2557 lost_event
.header
.type
= PERF_EVENT_LOST
;
2558 lost_event
.header
.misc
= 0;
2559 lost_event
.header
.size
= sizeof(lost_event
);
2560 lost_event
.id
= counter
->id
;
2561 lost_event
.lost
= atomic_xchg(&data
->lost
, 0);
2563 perf_output_put(handle
, lost_event
);
2569 atomic_inc(&data
->lost
);
2570 perf_output_unlock(handle
);
2577 static void perf_output_end(struct perf_output_handle
*handle
)
2579 struct perf_counter
*counter
= handle
->counter
;
2580 struct perf_mmap_data
*data
= handle
->data
;
2582 int wakeup_events
= counter
->attr
.wakeup_events
;
2584 if (handle
->sample
&& wakeup_events
) {
2585 int events
= atomic_inc_return(&data
->events
);
2586 if (events
>= wakeup_events
) {
2587 atomic_sub(wakeup_events
, &data
->events
);
2588 atomic_set(&data
->wakeup
, 1);
2592 perf_output_unlock(handle
);
2596 static u32
perf_counter_pid(struct perf_counter
*counter
, struct task_struct
*p
)
2599 * only top level counters have the pid namespace they were created in
2601 if (counter
->parent
)
2602 counter
= counter
->parent
;
2604 return task_tgid_nr_ns(p
, counter
->ns
);
2607 static u32
perf_counter_tid(struct perf_counter
*counter
, struct task_struct
*p
)
2610 * only top level counters have the pid namespace they were created in
2612 if (counter
->parent
)
2613 counter
= counter
->parent
;
2615 return task_pid_nr_ns(p
, counter
->ns
);
2618 static void perf_counter_output(struct perf_counter
*counter
, int nmi
,
2619 struct perf_sample_data
*data
)
2622 u64 sample_type
= counter
->attr
.sample_type
;
2623 struct perf_output_handle handle
;
2624 struct perf_event_header header
;
2633 struct perf_callchain_entry
*callchain
= NULL
;
2634 int callchain_size
= 0;
2640 header
.type
= PERF_EVENT_SAMPLE
;
2641 header
.size
= sizeof(header
);
2644 header
.misc
|= perf_misc_flags(data
->regs
);
2646 if (sample_type
& PERF_SAMPLE_IP
) {
2647 ip
= perf_instruction_pointer(data
->regs
);
2648 header
.size
+= sizeof(ip
);
2651 if (sample_type
& PERF_SAMPLE_TID
) {
2652 /* namespace issues */
2653 tid_entry
.pid
= perf_counter_pid(counter
, current
);
2654 tid_entry
.tid
= perf_counter_tid(counter
, current
);
2656 header
.size
+= sizeof(tid_entry
);
2659 if (sample_type
& PERF_SAMPLE_TIME
) {
2661 * Maybe do better on x86 and provide cpu_clock_nmi()
2663 time
= sched_clock();
2665 header
.size
+= sizeof(u64
);
2668 if (sample_type
& PERF_SAMPLE_ADDR
)
2669 header
.size
+= sizeof(u64
);
2671 if (sample_type
& PERF_SAMPLE_ID
)
2672 header
.size
+= sizeof(u64
);
2674 if (sample_type
& PERF_SAMPLE_STREAM_ID
)
2675 header
.size
+= sizeof(u64
);
2677 if (sample_type
& PERF_SAMPLE_CPU
) {
2678 header
.size
+= sizeof(cpu_entry
);
2680 cpu_entry
.cpu
= raw_smp_processor_id();
2681 cpu_entry
.reserved
= 0;
2684 if (sample_type
& PERF_SAMPLE_PERIOD
)
2685 header
.size
+= sizeof(u64
);
2687 if (sample_type
& PERF_SAMPLE_GROUP
) {
2688 header
.size
+= sizeof(u64
) +
2689 counter
->nr_siblings
* sizeof(group_entry
);
2692 if (sample_type
& PERF_SAMPLE_CALLCHAIN
) {
2693 callchain
= perf_callchain(data
->regs
);
2696 callchain_size
= (1 + callchain
->nr
) * sizeof(u64
);
2697 header
.size
+= callchain_size
;
2699 header
.size
+= sizeof(u64
);
2702 ret
= perf_output_begin(&handle
, counter
, header
.size
, nmi
, 1);
2706 perf_output_put(&handle
, header
);
2708 if (sample_type
& PERF_SAMPLE_IP
)
2709 perf_output_put(&handle
, ip
);
2711 if (sample_type
& PERF_SAMPLE_TID
)
2712 perf_output_put(&handle
, tid_entry
);
2714 if (sample_type
& PERF_SAMPLE_TIME
)
2715 perf_output_put(&handle
, time
);
2717 if (sample_type
& PERF_SAMPLE_ADDR
)
2718 perf_output_put(&handle
, data
->addr
);
2720 if (sample_type
& PERF_SAMPLE_ID
) {
2721 u64 id
= primary_counter_id(counter
);
2723 perf_output_put(&handle
, id
);
2726 if (sample_type
& PERF_SAMPLE_STREAM_ID
)
2727 perf_output_put(&handle
, counter
->id
);
2729 if (sample_type
& PERF_SAMPLE_CPU
)
2730 perf_output_put(&handle
, cpu_entry
);
2732 if (sample_type
& PERF_SAMPLE_PERIOD
)
2733 perf_output_put(&handle
, data
->period
);
2736 * XXX PERF_SAMPLE_GROUP vs inherited counters seems difficult.
2738 if (sample_type
& PERF_SAMPLE_GROUP
) {
2739 struct perf_counter
*leader
, *sub
;
2740 u64 nr
= counter
->nr_siblings
;
2742 perf_output_put(&handle
, nr
);
2744 leader
= counter
->group_leader
;
2745 list_for_each_entry(sub
, &leader
->sibling_list
, list_entry
) {
2747 sub
->pmu
->read(sub
);
2749 group_entry
.id
= primary_counter_id(sub
);
2750 group_entry
.counter
= atomic64_read(&sub
->count
);
2752 perf_output_put(&handle
, group_entry
);
2756 if (sample_type
& PERF_SAMPLE_CALLCHAIN
) {
2758 perf_output_copy(&handle
, callchain
, callchain_size
);
2761 perf_output_put(&handle
, nr
);
2765 perf_output_end(&handle
);
2772 struct perf_read_event
{
2773 struct perf_event_header header
;
2782 perf_counter_read_event(struct perf_counter
*counter
,
2783 struct task_struct
*task
)
2785 struct perf_output_handle handle
;
2786 struct perf_read_event event
= {
2788 .type
= PERF_EVENT_READ
,
2790 .size
= sizeof(event
) - sizeof(event
.format
),
2792 .pid
= perf_counter_pid(counter
, task
),
2793 .tid
= perf_counter_tid(counter
, task
),
2794 .value
= atomic64_read(&counter
->count
),
2798 if (counter
->attr
.read_format
& PERF_FORMAT_TOTAL_TIME_ENABLED
) {
2799 event
.header
.size
+= sizeof(u64
);
2800 event
.format
[i
++] = counter
->total_time_enabled
;
2803 if (counter
->attr
.read_format
& PERF_FORMAT_TOTAL_TIME_RUNNING
) {
2804 event
.header
.size
+= sizeof(u64
);
2805 event
.format
[i
++] = counter
->total_time_running
;
2808 if (counter
->attr
.read_format
& PERF_FORMAT_ID
) {
2809 event
.header
.size
+= sizeof(u64
);
2810 event
.format
[i
++] = primary_counter_id(counter
);
2813 ret
= perf_output_begin(&handle
, counter
, event
.header
.size
, 0, 0);
2817 perf_output_copy(&handle
, &event
, event
.header
.size
);
2818 perf_output_end(&handle
);
2825 struct perf_fork_event
{
2826 struct task_struct
*task
;
2829 struct perf_event_header header
;
2836 static void perf_counter_fork_output(struct perf_counter
*counter
,
2837 struct perf_fork_event
*fork_event
)
2839 struct perf_output_handle handle
;
2840 int size
= fork_event
->event
.header
.size
;
2841 struct task_struct
*task
= fork_event
->task
;
2842 int ret
= perf_output_begin(&handle
, counter
, size
, 0, 0);
2847 fork_event
->event
.pid
= perf_counter_pid(counter
, task
);
2848 fork_event
->event
.ppid
= perf_counter_pid(counter
, task
->real_parent
);
2850 perf_output_put(&handle
, fork_event
->event
);
2851 perf_output_end(&handle
);
2854 static int perf_counter_fork_match(struct perf_counter
*counter
)
2856 if (counter
->attr
.comm
|| counter
->attr
.mmap
)
2862 static void perf_counter_fork_ctx(struct perf_counter_context
*ctx
,
2863 struct perf_fork_event
*fork_event
)
2865 struct perf_counter
*counter
;
2867 if (system_state
!= SYSTEM_RUNNING
|| list_empty(&ctx
->event_list
))
2871 list_for_each_entry_rcu(counter
, &ctx
->event_list
, event_entry
) {
2872 if (perf_counter_fork_match(counter
))
2873 perf_counter_fork_output(counter
, fork_event
);
2878 static void perf_counter_fork_event(struct perf_fork_event
*fork_event
)
2880 struct perf_cpu_context
*cpuctx
;
2881 struct perf_counter_context
*ctx
;
2883 cpuctx
= &get_cpu_var(perf_cpu_context
);
2884 perf_counter_fork_ctx(&cpuctx
->ctx
, fork_event
);
2885 put_cpu_var(perf_cpu_context
);
2889 * doesn't really matter which of the child contexts the
2890 * events ends up in.
2892 ctx
= rcu_dereference(current
->perf_counter_ctxp
);
2894 perf_counter_fork_ctx(ctx
, fork_event
);
2898 void perf_counter_fork(struct task_struct
*task
)
2900 struct perf_fork_event fork_event
;
2902 if (!atomic_read(&nr_comm_counters
) &&
2903 !atomic_read(&nr_mmap_counters
))
2906 fork_event
= (struct perf_fork_event
){
2910 .type
= PERF_EVENT_FORK
,
2912 .size
= sizeof(fork_event
.event
),
2919 perf_counter_fork_event(&fork_event
);
2926 struct perf_comm_event
{
2927 struct task_struct
*task
;
2932 struct perf_event_header header
;
2939 static void perf_counter_comm_output(struct perf_counter
*counter
,
2940 struct perf_comm_event
*comm_event
)
2942 struct perf_output_handle handle
;
2943 int size
= comm_event
->event
.header
.size
;
2944 int ret
= perf_output_begin(&handle
, counter
, size
, 0, 0);
2949 comm_event
->event
.pid
= perf_counter_pid(counter
, comm_event
->task
);
2950 comm_event
->event
.tid
= perf_counter_tid(counter
, comm_event
->task
);
2952 perf_output_put(&handle
, comm_event
->event
);
2953 perf_output_copy(&handle
, comm_event
->comm
,
2954 comm_event
->comm_size
);
2955 perf_output_end(&handle
);
2958 static int perf_counter_comm_match(struct perf_counter
*counter
)
2960 if (counter
->attr
.comm
)
2966 static void perf_counter_comm_ctx(struct perf_counter_context
*ctx
,
2967 struct perf_comm_event
*comm_event
)
2969 struct perf_counter
*counter
;
2971 if (system_state
!= SYSTEM_RUNNING
|| list_empty(&ctx
->event_list
))
2975 list_for_each_entry_rcu(counter
, &ctx
->event_list
, event_entry
) {
2976 if (perf_counter_comm_match(counter
))
2977 perf_counter_comm_output(counter
, comm_event
);
2982 static void perf_counter_comm_event(struct perf_comm_event
*comm_event
)
2984 struct perf_cpu_context
*cpuctx
;
2985 struct perf_counter_context
*ctx
;
2987 char comm
[TASK_COMM_LEN
];
2989 memset(comm
, 0, sizeof(comm
));
2990 strncpy(comm
, comm_event
->task
->comm
, sizeof(comm
));
2991 size
= ALIGN(strlen(comm
)+1, sizeof(u64
));
2993 comm_event
->comm
= comm
;
2994 comm_event
->comm_size
= size
;
2996 comm_event
->event
.header
.size
= sizeof(comm_event
->event
) + size
;
2998 cpuctx
= &get_cpu_var(perf_cpu_context
);
2999 perf_counter_comm_ctx(&cpuctx
->ctx
, comm_event
);
3000 put_cpu_var(perf_cpu_context
);
3004 * doesn't really matter which of the child contexts the
3005 * events ends up in.
3007 ctx
= rcu_dereference(current
->perf_counter_ctxp
);
3009 perf_counter_comm_ctx(ctx
, comm_event
);
3013 void perf_counter_comm(struct task_struct
*task
)
3015 struct perf_comm_event comm_event
;
3017 if (task
->perf_counter_ctxp
)
3018 perf_counter_enable_on_exec(task
);
3020 if (!atomic_read(&nr_comm_counters
))
3023 comm_event
= (struct perf_comm_event
){
3029 .type
= PERF_EVENT_COMM
,
3038 perf_counter_comm_event(&comm_event
);
3045 struct perf_mmap_event
{
3046 struct vm_area_struct
*vma
;
3048 const char *file_name
;
3052 struct perf_event_header header
;
3062 static void perf_counter_mmap_output(struct perf_counter
*counter
,
3063 struct perf_mmap_event
*mmap_event
)
3065 struct perf_output_handle handle
;
3066 int size
= mmap_event
->event
.header
.size
;
3067 int ret
= perf_output_begin(&handle
, counter
, size
, 0, 0);
3072 mmap_event
->event
.pid
= perf_counter_pid(counter
, current
);
3073 mmap_event
->event
.tid
= perf_counter_tid(counter
, current
);
3075 perf_output_put(&handle
, mmap_event
->event
);
3076 perf_output_copy(&handle
, mmap_event
->file_name
,
3077 mmap_event
->file_size
);
3078 perf_output_end(&handle
);
3081 static int perf_counter_mmap_match(struct perf_counter
*counter
,
3082 struct perf_mmap_event
*mmap_event
)
3084 if (counter
->attr
.mmap
)
3090 static void perf_counter_mmap_ctx(struct perf_counter_context
*ctx
,
3091 struct perf_mmap_event
*mmap_event
)
3093 struct perf_counter
*counter
;
3095 if (system_state
!= SYSTEM_RUNNING
|| list_empty(&ctx
->event_list
))
3099 list_for_each_entry_rcu(counter
, &ctx
->event_list
, event_entry
) {
3100 if (perf_counter_mmap_match(counter
, mmap_event
))
3101 perf_counter_mmap_output(counter
, mmap_event
);
3106 static void perf_counter_mmap_event(struct perf_mmap_event
*mmap_event
)
3108 struct perf_cpu_context
*cpuctx
;
3109 struct perf_counter_context
*ctx
;
3110 struct vm_area_struct
*vma
= mmap_event
->vma
;
3111 struct file
*file
= vma
->vm_file
;
3117 memset(tmp
, 0, sizeof(tmp
));
3121 * d_path works from the end of the buffer backwards, so we
3122 * need to add enough zero bytes after the string to handle
3123 * the 64bit alignment we do later.
3125 buf
= kzalloc(PATH_MAX
+ sizeof(u64
), GFP_KERNEL
);
3127 name
= strncpy(tmp
, "//enomem", sizeof(tmp
));
3130 name
= d_path(&file
->f_path
, buf
, PATH_MAX
);
3132 name
= strncpy(tmp
, "//toolong", sizeof(tmp
));
3136 if (arch_vma_name(mmap_event
->vma
)) {
3137 name
= strncpy(tmp
, arch_vma_name(mmap_event
->vma
),
3143 name
= strncpy(tmp
, "[vdso]", sizeof(tmp
));
3147 name
= strncpy(tmp
, "//anon", sizeof(tmp
));
3152 size
= ALIGN(strlen(name
)+1, sizeof(u64
));
3154 mmap_event
->file_name
= name
;
3155 mmap_event
->file_size
= size
;
3157 mmap_event
->event
.header
.size
= sizeof(mmap_event
->event
) + size
;
3159 cpuctx
= &get_cpu_var(perf_cpu_context
);
3160 perf_counter_mmap_ctx(&cpuctx
->ctx
, mmap_event
);
3161 put_cpu_var(perf_cpu_context
);
3165 * doesn't really matter which of the child contexts the
3166 * events ends up in.
3168 ctx
= rcu_dereference(current
->perf_counter_ctxp
);
3170 perf_counter_mmap_ctx(ctx
, mmap_event
);
3176 void __perf_counter_mmap(struct vm_area_struct
*vma
)
3178 struct perf_mmap_event mmap_event
;
3180 if (!atomic_read(&nr_mmap_counters
))
3183 mmap_event
= (struct perf_mmap_event
){
3189 .type
= PERF_EVENT_MMAP
,
3195 .start
= vma
->vm_start
,
3196 .len
= vma
->vm_end
- vma
->vm_start
,
3197 .pgoff
= vma
->vm_pgoff
,
3201 perf_counter_mmap_event(&mmap_event
);
3205 * IRQ throttle logging
3208 static void perf_log_throttle(struct perf_counter
*counter
, int enable
)
3210 struct perf_output_handle handle
;
3214 struct perf_event_header header
;
3218 } throttle_event
= {
3220 .type
= PERF_EVENT_THROTTLE
+ 1,
3222 .size
= sizeof(throttle_event
),
3224 .time
= sched_clock(),
3225 .id
= primary_counter_id(counter
),
3226 .stream_id
= counter
->id
,
3229 ret
= perf_output_begin(&handle
, counter
, sizeof(throttle_event
), 1, 0);
3233 perf_output_put(&handle
, throttle_event
);
3234 perf_output_end(&handle
);
3238 * Generic counter overflow handling, sampling.
3241 int perf_counter_overflow(struct perf_counter
*counter
, int nmi
,
3242 struct perf_sample_data
*data
)
3244 int events
= atomic_read(&counter
->event_limit
);
3245 int throttle
= counter
->pmu
->unthrottle
!= NULL
;
3246 struct hw_perf_counter
*hwc
= &counter
->hw
;
3252 if (hwc
->interrupts
!= MAX_INTERRUPTS
) {
3254 if (HZ
* hwc
->interrupts
>
3255 (u64
)sysctl_perf_counter_sample_rate
) {
3256 hwc
->interrupts
= MAX_INTERRUPTS
;
3257 perf_log_throttle(counter
, 0);
3262 * Keep re-disabling counters even though on the previous
3263 * pass we disabled it - just in case we raced with a
3264 * sched-in and the counter got enabled again:
3270 if (counter
->attr
.freq
) {
3271 u64 now
= sched_clock();
3272 s64 delta
= now
- hwc
->freq_stamp
;
3274 hwc
->freq_stamp
= now
;
3276 if (delta
> 0 && delta
< TICK_NSEC
)
3277 perf_adjust_period(counter
, NSEC_PER_SEC
/ (int)delta
);
3281 * XXX event_limit might not quite work as expected on inherited
3285 counter
->pending_kill
= POLL_IN
;
3286 if (events
&& atomic_dec_and_test(&counter
->event_limit
)) {
3288 counter
->pending_kill
= POLL_HUP
;
3290 counter
->pending_disable
= 1;
3291 perf_pending_queue(&counter
->pending
,
3292 perf_pending_counter
);
3294 perf_counter_disable(counter
);
3297 perf_counter_output(counter
, nmi
, data
);
3302 * Generic software counter infrastructure
3305 static void perf_swcounter_update(struct perf_counter
*counter
)
3307 struct hw_perf_counter
*hwc
= &counter
->hw
;
3312 prev
= atomic64_read(&hwc
->prev_count
);
3313 now
= atomic64_read(&hwc
->count
);
3314 if (atomic64_cmpxchg(&hwc
->prev_count
, prev
, now
) != prev
)
3319 atomic64_add(delta
, &counter
->count
);
3320 atomic64_sub(delta
, &hwc
->period_left
);
3323 static void perf_swcounter_set_period(struct perf_counter
*counter
)
3325 struct hw_perf_counter
*hwc
= &counter
->hw
;
3326 s64 left
= atomic64_read(&hwc
->period_left
);
3327 s64 period
= hwc
->sample_period
;
3329 if (unlikely(left
<= -period
)) {
3331 atomic64_set(&hwc
->period_left
, left
);
3332 hwc
->last_period
= period
;
3335 if (unlikely(left
<= 0)) {
3337 atomic64_add(period
, &hwc
->period_left
);
3338 hwc
->last_period
= period
;
3341 atomic64_set(&hwc
->prev_count
, -left
);
3342 atomic64_set(&hwc
->count
, -left
);
3345 static enum hrtimer_restart
perf_swcounter_hrtimer(struct hrtimer
*hrtimer
)
3347 enum hrtimer_restart ret
= HRTIMER_RESTART
;
3348 struct perf_sample_data data
;
3349 struct perf_counter
*counter
;
3352 counter
= container_of(hrtimer
, struct perf_counter
, hw
.hrtimer
);
3353 counter
->pmu
->read(counter
);
3356 data
.regs
= get_irq_regs();
3358 * In case we exclude kernel IPs or are somehow not in interrupt
3359 * context, provide the next best thing, the user IP.
3361 if ((counter
->attr
.exclude_kernel
|| !data
.regs
) &&
3362 !counter
->attr
.exclude_user
)
3363 data
.regs
= task_pt_regs(current
);
3366 if (perf_counter_overflow(counter
, 0, &data
))
3367 ret
= HRTIMER_NORESTART
;
3370 period
= max_t(u64
, 10000, counter
->hw
.sample_period
);
3371 hrtimer_forward_now(hrtimer
, ns_to_ktime(period
));
3376 static void perf_swcounter_overflow(struct perf_counter
*counter
,
3377 int nmi
, struct perf_sample_data
*data
)
3379 data
->period
= counter
->hw
.last_period
;
3381 perf_swcounter_update(counter
);
3382 perf_swcounter_set_period(counter
);
3383 if (perf_counter_overflow(counter
, nmi
, data
))
3384 /* soft-disable the counter */
3388 static int perf_swcounter_is_counting(struct perf_counter
*counter
)
3390 struct perf_counter_context
*ctx
;
3391 unsigned long flags
;
3394 if (counter
->state
== PERF_COUNTER_STATE_ACTIVE
)
3397 if (counter
->state
!= PERF_COUNTER_STATE_INACTIVE
)
3401 * If the counter is inactive, it could be just because
3402 * its task is scheduled out, or because it's in a group
3403 * which could not go on the PMU. We want to count in
3404 * the first case but not the second. If the context is
3405 * currently active then an inactive software counter must
3406 * be the second case. If it's not currently active then
3407 * we need to know whether the counter was active when the
3408 * context was last active, which we can determine by
3409 * comparing counter->tstamp_stopped with ctx->time.
3411 * We are within an RCU read-side critical section,
3412 * which protects the existence of *ctx.
3415 spin_lock_irqsave(&ctx
->lock
, flags
);
3417 /* Re-check state now we have the lock */
3418 if (counter
->state
< PERF_COUNTER_STATE_INACTIVE
||
3419 counter
->ctx
->is_active
||
3420 counter
->tstamp_stopped
< ctx
->time
)
3422 spin_unlock_irqrestore(&ctx
->lock
, flags
);
3426 static int perf_swcounter_match(struct perf_counter
*counter
,
3427 enum perf_type_id type
,
3428 u32 event
, struct pt_regs
*regs
)
3430 if (!perf_swcounter_is_counting(counter
))
3433 if (counter
->attr
.type
!= type
)
3435 if (counter
->attr
.config
!= event
)
3439 if (counter
->attr
.exclude_user
&& user_mode(regs
))
3442 if (counter
->attr
.exclude_kernel
&& !user_mode(regs
))
3449 static void perf_swcounter_add(struct perf_counter
*counter
, u64 nr
,
3450 int nmi
, struct perf_sample_data
*data
)
3452 int neg
= atomic64_add_negative(nr
, &counter
->hw
.count
);
3454 if (counter
->hw
.sample_period
&& !neg
&& data
->regs
)
3455 perf_swcounter_overflow(counter
, nmi
, data
);
3458 static void perf_swcounter_ctx_event(struct perf_counter_context
*ctx
,
3459 enum perf_type_id type
,
3460 u32 event
, u64 nr
, int nmi
,
3461 struct perf_sample_data
*data
)
3463 struct perf_counter
*counter
;
3465 if (system_state
!= SYSTEM_RUNNING
|| list_empty(&ctx
->event_list
))
3469 list_for_each_entry_rcu(counter
, &ctx
->event_list
, event_entry
) {
3470 if (perf_swcounter_match(counter
, type
, event
, data
->regs
))
3471 perf_swcounter_add(counter
, nr
, nmi
, data
);
3476 static int *perf_swcounter_recursion_context(struct perf_cpu_context
*cpuctx
)
3479 return &cpuctx
->recursion
[3];
3482 return &cpuctx
->recursion
[2];
3485 return &cpuctx
->recursion
[1];
3487 return &cpuctx
->recursion
[0];
3490 static void do_perf_swcounter_event(enum perf_type_id type
, u32 event
,
3492 struct perf_sample_data
*data
)
3494 struct perf_cpu_context
*cpuctx
= &get_cpu_var(perf_cpu_context
);
3495 int *recursion
= perf_swcounter_recursion_context(cpuctx
);
3496 struct perf_counter_context
*ctx
;
3504 perf_swcounter_ctx_event(&cpuctx
->ctx
, type
, event
,
3508 * doesn't really matter which of the child contexts the
3509 * events ends up in.
3511 ctx
= rcu_dereference(current
->perf_counter_ctxp
);
3513 perf_swcounter_ctx_event(ctx
, type
, event
, nr
, nmi
, data
);
3520 put_cpu_var(perf_cpu_context
);
3523 void __perf_swcounter_event(u32 event
, u64 nr
, int nmi
,
3524 struct pt_regs
*regs
, u64 addr
)
3526 struct perf_sample_data data
= {
3531 do_perf_swcounter_event(PERF_TYPE_SOFTWARE
, event
, nr
, nmi
, &data
);
3534 static void perf_swcounter_read(struct perf_counter
*counter
)
3536 perf_swcounter_update(counter
);
3539 static int perf_swcounter_enable(struct perf_counter
*counter
)
3541 perf_swcounter_set_period(counter
);
3545 static void perf_swcounter_disable(struct perf_counter
*counter
)
3547 perf_swcounter_update(counter
);
3550 static const struct pmu perf_ops_generic
= {
3551 .enable
= perf_swcounter_enable
,
3552 .disable
= perf_swcounter_disable
,
3553 .read
= perf_swcounter_read
,
3557 * Software counter: cpu wall time clock
3560 static void cpu_clock_perf_counter_update(struct perf_counter
*counter
)
3562 int cpu
= raw_smp_processor_id();
3566 now
= cpu_clock(cpu
);
3567 prev
= atomic64_read(&counter
->hw
.prev_count
);
3568 atomic64_set(&counter
->hw
.prev_count
, now
);
3569 atomic64_add(now
- prev
, &counter
->count
);
3572 static int cpu_clock_perf_counter_enable(struct perf_counter
*counter
)
3574 struct hw_perf_counter
*hwc
= &counter
->hw
;
3575 int cpu
= raw_smp_processor_id();
3577 atomic64_set(&hwc
->prev_count
, cpu_clock(cpu
));
3578 hrtimer_init(&hwc
->hrtimer
, CLOCK_MONOTONIC
, HRTIMER_MODE_REL
);
3579 hwc
->hrtimer
.function
= perf_swcounter_hrtimer
;
3580 if (hwc
->sample_period
) {
3581 u64 period
= max_t(u64
, 10000, hwc
->sample_period
);
3582 __hrtimer_start_range_ns(&hwc
->hrtimer
,
3583 ns_to_ktime(period
), 0,
3584 HRTIMER_MODE_REL
, 0);
3590 static void cpu_clock_perf_counter_disable(struct perf_counter
*counter
)
3592 if (counter
->hw
.sample_period
)
3593 hrtimer_cancel(&counter
->hw
.hrtimer
);
3594 cpu_clock_perf_counter_update(counter
);
3597 static void cpu_clock_perf_counter_read(struct perf_counter
*counter
)
3599 cpu_clock_perf_counter_update(counter
);
3602 static const struct pmu perf_ops_cpu_clock
= {
3603 .enable
= cpu_clock_perf_counter_enable
,
3604 .disable
= cpu_clock_perf_counter_disable
,
3605 .read
= cpu_clock_perf_counter_read
,
3609 * Software counter: task time clock
3612 static void task_clock_perf_counter_update(struct perf_counter
*counter
, u64 now
)
3617 prev
= atomic64_xchg(&counter
->hw
.prev_count
, now
);
3619 atomic64_add(delta
, &counter
->count
);
3622 static int task_clock_perf_counter_enable(struct perf_counter
*counter
)
3624 struct hw_perf_counter
*hwc
= &counter
->hw
;
3627 now
= counter
->ctx
->time
;
3629 atomic64_set(&hwc
->prev_count
, now
);
3630 hrtimer_init(&hwc
->hrtimer
, CLOCK_MONOTONIC
, HRTIMER_MODE_REL
);
3631 hwc
->hrtimer
.function
= perf_swcounter_hrtimer
;
3632 if (hwc
->sample_period
) {
3633 u64 period
= max_t(u64
, 10000, hwc
->sample_period
);
3634 __hrtimer_start_range_ns(&hwc
->hrtimer
,
3635 ns_to_ktime(period
), 0,
3636 HRTIMER_MODE_REL
, 0);
3642 static void task_clock_perf_counter_disable(struct perf_counter
*counter
)
3644 if (counter
->hw
.sample_period
)
3645 hrtimer_cancel(&counter
->hw
.hrtimer
);
3646 task_clock_perf_counter_update(counter
, counter
->ctx
->time
);
3650 static void task_clock_perf_counter_read(struct perf_counter
*counter
)
3655 update_context_time(counter
->ctx
);
3656 time
= counter
->ctx
->time
;
3658 u64 now
= perf_clock();
3659 u64 delta
= now
- counter
->ctx
->timestamp
;
3660 time
= counter
->ctx
->time
+ delta
;
3663 task_clock_perf_counter_update(counter
, time
);
3666 static const struct pmu perf_ops_task_clock
= {
3667 .enable
= task_clock_perf_counter_enable
,
3668 .disable
= task_clock_perf_counter_disable
,
3669 .read
= task_clock_perf_counter_read
,
3672 #ifdef CONFIG_EVENT_PROFILE
3673 void perf_tpcounter_event(int event_id
)
3675 struct perf_sample_data data
= {
3676 .regs
= get_irq_regs(),
3681 data
.regs
= task_pt_regs(current
);
3683 do_perf_swcounter_event(PERF_TYPE_TRACEPOINT
, event_id
, 1, 1, &data
);
3685 EXPORT_SYMBOL_GPL(perf_tpcounter_event
);
3687 extern int ftrace_profile_enable(int);
3688 extern void ftrace_profile_disable(int);
3690 static void tp_perf_counter_destroy(struct perf_counter
*counter
)
3692 ftrace_profile_disable(counter
->attr
.config
);
3695 static const struct pmu
*tp_perf_counter_init(struct perf_counter
*counter
)
3697 if (ftrace_profile_enable(counter
->attr
.config
))
3700 counter
->destroy
= tp_perf_counter_destroy
;
3702 return &perf_ops_generic
;
3705 static const struct pmu
*tp_perf_counter_init(struct perf_counter
*counter
)
3711 atomic_t perf_swcounter_enabled
[PERF_COUNT_SW_MAX
];
3713 static void sw_perf_counter_destroy(struct perf_counter
*counter
)
3715 u64 event
= counter
->attr
.config
;
3717 WARN_ON(counter
->parent
);
3719 atomic_dec(&perf_swcounter_enabled
[event
]);
3722 static const struct pmu
*sw_perf_counter_init(struct perf_counter
*counter
)
3724 const struct pmu
*pmu
= NULL
;
3725 u64 event
= counter
->attr
.config
;
3728 * Software counters (currently) can't in general distinguish
3729 * between user, kernel and hypervisor events.
3730 * However, context switches and cpu migrations are considered
3731 * to be kernel events, and page faults are never hypervisor
3735 case PERF_COUNT_SW_CPU_CLOCK
:
3736 pmu
= &perf_ops_cpu_clock
;
3739 case PERF_COUNT_SW_TASK_CLOCK
:
3741 * If the user instantiates this as a per-cpu counter,
3742 * use the cpu_clock counter instead.
3744 if (counter
->ctx
->task
)
3745 pmu
= &perf_ops_task_clock
;
3747 pmu
= &perf_ops_cpu_clock
;
3750 case PERF_COUNT_SW_PAGE_FAULTS
:
3751 case PERF_COUNT_SW_PAGE_FAULTS_MIN
:
3752 case PERF_COUNT_SW_PAGE_FAULTS_MAJ
:
3753 case PERF_COUNT_SW_CONTEXT_SWITCHES
:
3754 case PERF_COUNT_SW_CPU_MIGRATIONS
:
3755 if (!counter
->parent
) {
3756 atomic_inc(&perf_swcounter_enabled
[event
]);
3757 counter
->destroy
= sw_perf_counter_destroy
;
3759 pmu
= &perf_ops_generic
;
3767 * Allocate and initialize a counter structure
3769 static struct perf_counter
*
3770 perf_counter_alloc(struct perf_counter_attr
*attr
,
3772 struct perf_counter_context
*ctx
,
3773 struct perf_counter
*group_leader
,
3774 struct perf_counter
*parent_counter
,
3777 const struct pmu
*pmu
;
3778 struct perf_counter
*counter
;
3779 struct hw_perf_counter
*hwc
;
3782 counter
= kzalloc(sizeof(*counter
), gfpflags
);
3784 return ERR_PTR(-ENOMEM
);
3787 * Single counters are their own group leaders, with an
3788 * empty sibling list:
3791 group_leader
= counter
;
3793 mutex_init(&counter
->child_mutex
);
3794 INIT_LIST_HEAD(&counter
->child_list
);
3796 INIT_LIST_HEAD(&counter
->list_entry
);
3797 INIT_LIST_HEAD(&counter
->event_entry
);
3798 INIT_LIST_HEAD(&counter
->sibling_list
);
3799 init_waitqueue_head(&counter
->waitq
);
3801 mutex_init(&counter
->mmap_mutex
);
3804 counter
->attr
= *attr
;
3805 counter
->group_leader
= group_leader
;
3806 counter
->pmu
= NULL
;
3808 counter
->oncpu
= -1;
3810 counter
->parent
= parent_counter
;
3812 counter
->ns
= get_pid_ns(current
->nsproxy
->pid_ns
);
3813 counter
->id
= atomic64_inc_return(&perf_counter_id
);
3815 counter
->state
= PERF_COUNTER_STATE_INACTIVE
;
3818 counter
->state
= PERF_COUNTER_STATE_OFF
;
3823 hwc
->sample_period
= attr
->sample_period
;
3824 if (attr
->freq
&& attr
->sample_freq
)
3825 hwc
->sample_period
= 1;
3827 atomic64_set(&hwc
->period_left
, hwc
->sample_period
);
3830 * we currently do not support PERF_SAMPLE_GROUP on inherited counters
3832 if (attr
->inherit
&& (attr
->sample_type
& PERF_SAMPLE_GROUP
))
3835 switch (attr
->type
) {
3837 case PERF_TYPE_HARDWARE
:
3838 case PERF_TYPE_HW_CACHE
:
3839 pmu
= hw_perf_counter_init(counter
);
3842 case PERF_TYPE_SOFTWARE
:
3843 pmu
= sw_perf_counter_init(counter
);
3846 case PERF_TYPE_TRACEPOINT
:
3847 pmu
= tp_perf_counter_init(counter
);
3857 else if (IS_ERR(pmu
))
3862 put_pid_ns(counter
->ns
);
3864 return ERR_PTR(err
);
3869 if (!counter
->parent
) {
3870 atomic_inc(&nr_counters
);
3871 if (counter
->attr
.mmap
)
3872 atomic_inc(&nr_mmap_counters
);
3873 if (counter
->attr
.comm
)
3874 atomic_inc(&nr_comm_counters
);
3880 static int perf_copy_attr(struct perf_counter_attr __user
*uattr
,
3881 struct perf_counter_attr
*attr
)
3886 if (!access_ok(VERIFY_WRITE
, uattr
, PERF_ATTR_SIZE_VER0
))
3890 * zero the full structure, so that a short copy will be nice.
3892 memset(attr
, 0, sizeof(*attr
));
3894 ret
= get_user(size
, &uattr
->size
);
3898 if (size
> PAGE_SIZE
) /* silly large */
3901 if (!size
) /* abi compat */
3902 size
= PERF_ATTR_SIZE_VER0
;
3904 if (size
< PERF_ATTR_SIZE_VER0
)
3908 * If we're handed a bigger struct than we know of,
3909 * ensure all the unknown bits are 0.
3911 if (size
> sizeof(*attr
)) {
3913 unsigned long __user
*addr
;
3914 unsigned long __user
*end
;
3916 addr
= PTR_ALIGN((void __user
*)uattr
+ sizeof(*attr
),
3917 sizeof(unsigned long));
3918 end
= PTR_ALIGN((void __user
*)uattr
+ size
,
3919 sizeof(unsigned long));
3921 for (; addr
< end
; addr
+= sizeof(unsigned long)) {
3922 ret
= get_user(val
, addr
);
3930 ret
= copy_from_user(attr
, uattr
, size
);
3935 * If the type exists, the corresponding creation will verify
3938 if (attr
->type
>= PERF_TYPE_MAX
)
3941 if (attr
->__reserved_1
|| attr
->__reserved_2
|| attr
->__reserved_3
)
3944 if (attr
->sample_type
& ~(PERF_SAMPLE_MAX
-1))
3947 if (attr
->read_format
& ~(PERF_FORMAT_MAX
-1))
3954 put_user(sizeof(*attr
), &uattr
->size
);
3960 * sys_perf_counter_open - open a performance counter, associate it to a task/cpu
3962 * @attr_uptr: event type attributes for monitoring/sampling
3965 * @group_fd: group leader counter fd
3967 SYSCALL_DEFINE5(perf_counter_open
,
3968 struct perf_counter_attr __user
*, attr_uptr
,
3969 pid_t
, pid
, int, cpu
, int, group_fd
, unsigned long, flags
)
3971 struct perf_counter
*counter
, *group_leader
;
3972 struct perf_counter_attr attr
;
3973 struct perf_counter_context
*ctx
;
3974 struct file
*counter_file
= NULL
;
3975 struct file
*group_file
= NULL
;
3976 int fput_needed
= 0;
3977 int fput_needed2
= 0;
3980 /* for future expandability... */
3984 ret
= perf_copy_attr(attr_uptr
, &attr
);
3988 if (!attr
.exclude_kernel
) {
3989 if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN
))
3994 if (attr
.sample_freq
> sysctl_perf_counter_sample_rate
)
3999 * Get the target context (task or percpu):
4001 ctx
= find_get_context(pid
, cpu
);
4003 return PTR_ERR(ctx
);
4006 * Look up the group leader (we will attach this counter to it):
4008 group_leader
= NULL
;
4009 if (group_fd
!= -1) {
4011 group_file
= fget_light(group_fd
, &fput_needed
);
4013 goto err_put_context
;
4014 if (group_file
->f_op
!= &perf_fops
)
4015 goto err_put_context
;
4017 group_leader
= group_file
->private_data
;
4019 * Do not allow a recursive hierarchy (this new sibling
4020 * becoming part of another group-sibling):
4022 if (group_leader
->group_leader
!= group_leader
)
4023 goto err_put_context
;
4025 * Do not allow to attach to a group in a different
4026 * task or CPU context:
4028 if (group_leader
->ctx
!= ctx
)
4029 goto err_put_context
;
4031 * Only a group leader can be exclusive or pinned
4033 if (attr
.exclusive
|| attr
.pinned
)
4034 goto err_put_context
;
4037 counter
= perf_counter_alloc(&attr
, cpu
, ctx
, group_leader
,
4039 ret
= PTR_ERR(counter
);
4040 if (IS_ERR(counter
))
4041 goto err_put_context
;
4043 ret
= anon_inode_getfd("[perf_counter]", &perf_fops
, counter
, 0);
4045 goto err_free_put_context
;
4047 counter_file
= fget_light(ret
, &fput_needed2
);
4049 goto err_free_put_context
;
4051 counter
->filp
= counter_file
;
4052 WARN_ON_ONCE(ctx
->parent_ctx
);
4053 mutex_lock(&ctx
->mutex
);
4054 perf_install_in_context(ctx
, counter
, cpu
);
4056 mutex_unlock(&ctx
->mutex
);
4058 counter
->owner
= current
;
4059 get_task_struct(current
);
4060 mutex_lock(¤t
->perf_counter_mutex
);
4061 list_add_tail(&counter
->owner_entry
, ¤t
->perf_counter_list
);
4062 mutex_unlock(¤t
->perf_counter_mutex
);
4064 fput_light(counter_file
, fput_needed2
);
4067 fput_light(group_file
, fput_needed
);
4071 err_free_put_context
:
4081 * inherit a counter from parent task to child task:
4083 static struct perf_counter
*
4084 inherit_counter(struct perf_counter
*parent_counter
,
4085 struct task_struct
*parent
,
4086 struct perf_counter_context
*parent_ctx
,
4087 struct task_struct
*child
,
4088 struct perf_counter
*group_leader
,
4089 struct perf_counter_context
*child_ctx
)
4091 struct perf_counter
*child_counter
;
4094 * Instead of creating recursive hierarchies of counters,
4095 * we link inherited counters back to the original parent,
4096 * which has a filp for sure, which we use as the reference
4099 if (parent_counter
->parent
)
4100 parent_counter
= parent_counter
->parent
;
4102 child_counter
= perf_counter_alloc(&parent_counter
->attr
,
4103 parent_counter
->cpu
, child_ctx
,
4104 group_leader
, parent_counter
,
4106 if (IS_ERR(child_counter
))
4107 return child_counter
;
4111 * Make the child state follow the state of the parent counter,
4112 * not its attr.disabled bit. We hold the parent's mutex,
4113 * so we won't race with perf_counter_{en, dis}able_family.
4115 if (parent_counter
->state
>= PERF_COUNTER_STATE_INACTIVE
)
4116 child_counter
->state
= PERF_COUNTER_STATE_INACTIVE
;
4118 child_counter
->state
= PERF_COUNTER_STATE_OFF
;
4120 if (parent_counter
->attr
.freq
)
4121 child_counter
->hw
.sample_period
= parent_counter
->hw
.sample_period
;
4124 * Link it up in the child's context:
4126 add_counter_to_ctx(child_counter
, child_ctx
);
4129 * Get a reference to the parent filp - we will fput it
4130 * when the child counter exits. This is safe to do because
4131 * we are in the parent and we know that the filp still
4132 * exists and has a nonzero count:
4134 atomic_long_inc(&parent_counter
->filp
->f_count
);
4137 * Link this into the parent counter's child list
4139 WARN_ON_ONCE(parent_counter
->ctx
->parent_ctx
);
4140 mutex_lock(&parent_counter
->child_mutex
);
4141 list_add_tail(&child_counter
->child_list
, &parent_counter
->child_list
);
4142 mutex_unlock(&parent_counter
->child_mutex
);
4144 return child_counter
;
4147 static int inherit_group(struct perf_counter
*parent_counter
,
4148 struct task_struct
*parent
,
4149 struct perf_counter_context
*parent_ctx
,
4150 struct task_struct
*child
,
4151 struct perf_counter_context
*child_ctx
)
4153 struct perf_counter
*leader
;
4154 struct perf_counter
*sub
;
4155 struct perf_counter
*child_ctr
;
4157 leader
= inherit_counter(parent_counter
, parent
, parent_ctx
,
4158 child
, NULL
, child_ctx
);
4160 return PTR_ERR(leader
);
4161 list_for_each_entry(sub
, &parent_counter
->sibling_list
, list_entry
) {
4162 child_ctr
= inherit_counter(sub
, parent
, parent_ctx
,
4163 child
, leader
, child_ctx
);
4164 if (IS_ERR(child_ctr
))
4165 return PTR_ERR(child_ctr
);
4170 static void sync_child_counter(struct perf_counter
*child_counter
,
4171 struct task_struct
*child
)
4173 struct perf_counter
*parent_counter
= child_counter
->parent
;
4176 if (child_counter
->attr
.inherit_stat
)
4177 perf_counter_read_event(child_counter
, child
);
4179 child_val
= atomic64_read(&child_counter
->count
);
4182 * Add back the child's count to the parent's count:
4184 atomic64_add(child_val
, &parent_counter
->count
);
4185 atomic64_add(child_counter
->total_time_enabled
,
4186 &parent_counter
->child_total_time_enabled
);
4187 atomic64_add(child_counter
->total_time_running
,
4188 &parent_counter
->child_total_time_running
);
4191 * Remove this counter from the parent's list
4193 WARN_ON_ONCE(parent_counter
->ctx
->parent_ctx
);
4194 mutex_lock(&parent_counter
->child_mutex
);
4195 list_del_init(&child_counter
->child_list
);
4196 mutex_unlock(&parent_counter
->child_mutex
);
4199 * Release the parent counter, if this was the last
4202 fput(parent_counter
->filp
);
4206 __perf_counter_exit_task(struct perf_counter
*child_counter
,
4207 struct perf_counter_context
*child_ctx
,
4208 struct task_struct
*child
)
4210 struct perf_counter
*parent_counter
;
4212 update_counter_times(child_counter
);
4213 perf_counter_remove_from_context(child_counter
);
4215 parent_counter
= child_counter
->parent
;
4217 * It can happen that parent exits first, and has counters
4218 * that are still around due to the child reference. These
4219 * counters need to be zapped - but otherwise linger.
4221 if (parent_counter
) {
4222 sync_child_counter(child_counter
, child
);
4223 free_counter(child_counter
);
4228 * When a child task exits, feed back counter values to parent counters.
4230 void perf_counter_exit_task(struct task_struct
*child
)
4232 struct perf_counter
*child_counter
, *tmp
;
4233 struct perf_counter_context
*child_ctx
;
4234 unsigned long flags
;
4236 if (likely(!child
->perf_counter_ctxp
))
4239 local_irq_save(flags
);
4241 * We can't reschedule here because interrupts are disabled,
4242 * and either child is current or it is a task that can't be
4243 * scheduled, so we are now safe from rescheduling changing
4246 child_ctx
= child
->perf_counter_ctxp
;
4247 __perf_counter_task_sched_out(child_ctx
);
4250 * Take the context lock here so that if find_get_context is
4251 * reading child->perf_counter_ctxp, we wait until it has
4252 * incremented the context's refcount before we do put_ctx below.
4254 spin_lock(&child_ctx
->lock
);
4255 child
->perf_counter_ctxp
= NULL
;
4257 * If this context is a clone; unclone it so it can't get
4258 * swapped to another process while we're removing all
4259 * the counters from it.
4261 unclone_ctx(child_ctx
);
4262 spin_unlock(&child_ctx
->lock
);
4263 local_irq_restore(flags
);
4266 * We can recurse on the same lock type through:
4268 * __perf_counter_exit_task()
4269 * sync_child_counter()
4270 * fput(parent_counter->filp)
4272 * mutex_lock(&ctx->mutex)
4274 * But since its the parent context it won't be the same instance.
4276 mutex_lock_nested(&child_ctx
->mutex
, SINGLE_DEPTH_NESTING
);
4279 list_for_each_entry_safe(child_counter
, tmp
, &child_ctx
->counter_list
,
4281 __perf_counter_exit_task(child_counter
, child_ctx
, child
);
4284 * If the last counter was a group counter, it will have appended all
4285 * its siblings to the list, but we obtained 'tmp' before that which
4286 * will still point to the list head terminating the iteration.
4288 if (!list_empty(&child_ctx
->counter_list
))
4291 mutex_unlock(&child_ctx
->mutex
);
4297 * free an unexposed, unused context as created by inheritance by
4298 * init_task below, used by fork() in case of fail.
4300 void perf_counter_free_task(struct task_struct
*task
)
4302 struct perf_counter_context
*ctx
= task
->perf_counter_ctxp
;
4303 struct perf_counter
*counter
, *tmp
;
4308 mutex_lock(&ctx
->mutex
);
4310 list_for_each_entry_safe(counter
, tmp
, &ctx
->counter_list
, list_entry
) {
4311 struct perf_counter
*parent
= counter
->parent
;
4313 if (WARN_ON_ONCE(!parent
))
4316 mutex_lock(&parent
->child_mutex
);
4317 list_del_init(&counter
->child_list
);
4318 mutex_unlock(&parent
->child_mutex
);
4322 list_del_counter(counter
, ctx
);
4323 free_counter(counter
);
4326 if (!list_empty(&ctx
->counter_list
))
4329 mutex_unlock(&ctx
->mutex
);
4335 * Initialize the perf_counter context in task_struct
4337 int perf_counter_init_task(struct task_struct
*child
)
4339 struct perf_counter_context
*child_ctx
, *parent_ctx
;
4340 struct perf_counter_context
*cloned_ctx
;
4341 struct perf_counter
*counter
;
4342 struct task_struct
*parent
= current
;
4343 int inherited_all
= 1;
4346 child
->perf_counter_ctxp
= NULL
;
4348 mutex_init(&child
->perf_counter_mutex
);
4349 INIT_LIST_HEAD(&child
->perf_counter_list
);
4351 if (likely(!parent
->perf_counter_ctxp
))
4355 * This is executed from the parent task context, so inherit
4356 * counters that have been marked for cloning.
4357 * First allocate and initialize a context for the child.
4360 child_ctx
= kmalloc(sizeof(struct perf_counter_context
), GFP_KERNEL
);
4364 __perf_counter_init_context(child_ctx
, child
);
4365 child
->perf_counter_ctxp
= child_ctx
;
4366 get_task_struct(child
);
4369 * If the parent's context is a clone, pin it so it won't get
4372 parent_ctx
= perf_pin_task_context(parent
);
4375 * No need to check if parent_ctx != NULL here; since we saw
4376 * it non-NULL earlier, the only reason for it to become NULL
4377 * is if we exit, and since we're currently in the middle of
4378 * a fork we can't be exiting at the same time.
4382 * Lock the parent list. No need to lock the child - not PID
4383 * hashed yet and not running, so nobody can access it.
4385 mutex_lock(&parent_ctx
->mutex
);
4388 * We dont have to disable NMIs - we are only looking at
4389 * the list, not manipulating it:
4391 list_for_each_entry_rcu(counter
, &parent_ctx
->event_list
, event_entry
) {
4392 if (counter
!= counter
->group_leader
)
4395 if (!counter
->attr
.inherit
) {
4400 ret
= inherit_group(counter
, parent
, parent_ctx
,
4408 if (inherited_all
) {
4410 * Mark the child context as a clone of the parent
4411 * context, or of whatever the parent is a clone of.
4412 * Note that if the parent is a clone, it could get
4413 * uncloned at any point, but that doesn't matter
4414 * because the list of counters and the generation
4415 * count can't have changed since we took the mutex.
4417 cloned_ctx
= rcu_dereference(parent_ctx
->parent_ctx
);
4419 child_ctx
->parent_ctx
= cloned_ctx
;
4420 child_ctx
->parent_gen
= parent_ctx
->parent_gen
;
4422 child_ctx
->parent_ctx
= parent_ctx
;
4423 child_ctx
->parent_gen
= parent_ctx
->generation
;
4425 get_ctx(child_ctx
->parent_ctx
);
4428 mutex_unlock(&parent_ctx
->mutex
);
4430 perf_unpin_context(parent_ctx
);
4435 static void __cpuinit
perf_counter_init_cpu(int cpu
)
4437 struct perf_cpu_context
*cpuctx
;
4439 cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
4440 __perf_counter_init_context(&cpuctx
->ctx
, NULL
);
4442 spin_lock(&perf_resource_lock
);
4443 cpuctx
->max_pertask
= perf_max_counters
- perf_reserved_percpu
;
4444 spin_unlock(&perf_resource_lock
);
4446 hw_perf_counter_setup(cpu
);
4449 #ifdef CONFIG_HOTPLUG_CPU
4450 static void __perf_counter_exit_cpu(void *info
)
4452 struct perf_cpu_context
*cpuctx
= &__get_cpu_var(perf_cpu_context
);
4453 struct perf_counter_context
*ctx
= &cpuctx
->ctx
;
4454 struct perf_counter
*counter
, *tmp
;
4456 list_for_each_entry_safe(counter
, tmp
, &ctx
->counter_list
, list_entry
)
4457 __perf_counter_remove_from_context(counter
);
4459 static void perf_counter_exit_cpu(int cpu
)
4461 struct perf_cpu_context
*cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
4462 struct perf_counter_context
*ctx
= &cpuctx
->ctx
;
4464 mutex_lock(&ctx
->mutex
);
4465 smp_call_function_single(cpu
, __perf_counter_exit_cpu
, NULL
, 1);
4466 mutex_unlock(&ctx
->mutex
);
4469 static inline void perf_counter_exit_cpu(int cpu
) { }
4472 static int __cpuinit
4473 perf_cpu_notify(struct notifier_block
*self
, unsigned long action
, void *hcpu
)
4475 unsigned int cpu
= (long)hcpu
;
4479 case CPU_UP_PREPARE
:
4480 case CPU_UP_PREPARE_FROZEN
:
4481 perf_counter_init_cpu(cpu
);
4484 case CPU_DOWN_PREPARE
:
4485 case CPU_DOWN_PREPARE_FROZEN
:
4486 perf_counter_exit_cpu(cpu
);
4497 * This has to have a higher priority than migration_notifier in sched.c.
4499 static struct notifier_block __cpuinitdata perf_cpu_nb
= {
4500 .notifier_call
= perf_cpu_notify
,
4504 void __init
perf_counter_init(void)
4506 perf_cpu_notify(&perf_cpu_nb
, (unsigned long)CPU_UP_PREPARE
,
4507 (void *)(long)smp_processor_id());
4508 register_cpu_notifier(&perf_cpu_nb
);
4511 static ssize_t
perf_show_reserve_percpu(struct sysdev_class
*class, char *buf
)
4513 return sprintf(buf
, "%d\n", perf_reserved_percpu
);
4517 perf_set_reserve_percpu(struct sysdev_class
*class,
4521 struct perf_cpu_context
*cpuctx
;
4525 err
= strict_strtoul(buf
, 10, &val
);
4528 if (val
> perf_max_counters
)
4531 spin_lock(&perf_resource_lock
);
4532 perf_reserved_percpu
= val
;
4533 for_each_online_cpu(cpu
) {
4534 cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
4535 spin_lock_irq(&cpuctx
->ctx
.lock
);
4536 mpt
= min(perf_max_counters
- cpuctx
->ctx
.nr_counters
,
4537 perf_max_counters
- perf_reserved_percpu
);
4538 cpuctx
->max_pertask
= mpt
;
4539 spin_unlock_irq(&cpuctx
->ctx
.lock
);
4541 spin_unlock(&perf_resource_lock
);
4546 static ssize_t
perf_show_overcommit(struct sysdev_class
*class, char *buf
)
4548 return sprintf(buf
, "%d\n", perf_overcommit
);
4552 perf_set_overcommit(struct sysdev_class
*class, const char *buf
, size_t count
)
4557 err
= strict_strtoul(buf
, 10, &val
);
4563 spin_lock(&perf_resource_lock
);
4564 perf_overcommit
= val
;
4565 spin_unlock(&perf_resource_lock
);
4570 static SYSDEV_CLASS_ATTR(
4573 perf_show_reserve_percpu
,
4574 perf_set_reserve_percpu
4577 static SYSDEV_CLASS_ATTR(
4580 perf_show_overcommit
,
4584 static struct attribute
*perfclass_attrs
[] = {
4585 &attr_reserve_percpu
.attr
,
4586 &attr_overcommit
.attr
,
4590 static struct attribute_group perfclass_attr_group
= {
4591 .attrs
= perfclass_attrs
,
4592 .name
= "perf_counters",
4595 static int __init
perf_counter_sysfs_init(void)
4597 return sysfs_create_group(&cpu_sysdev_class
.kset
.kobj
,
4598 &perfclass_attr_group
);
4600 device_initcall(perf_counter_sysfs_init
);