2 * Performance events 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/vmalloc.h>
24 #include <linux/hardirq.h>
25 #include <linux/rculist.h>
26 #include <linux/uaccess.h>
27 #include <linux/syscalls.h>
28 #include <linux/anon_inodes.h>
29 #include <linux/kernel_stat.h>
30 #include <linux/perf_event.h>
31 #include <linux/ftrace_event.h>
32 #include <linux/hw_breakpoint.h>
34 #include <asm/irq_regs.h>
37 * Each CPU has a list of per CPU events:
39 static DEFINE_PER_CPU(struct perf_cpu_context
, perf_cpu_context
);
41 int perf_max_events __read_mostly
= 1;
42 static int perf_reserved_percpu __read_mostly
;
43 static int perf_overcommit __read_mostly
= 1;
45 static atomic_t nr_events __read_mostly
;
46 static atomic_t nr_mmap_events __read_mostly
;
47 static atomic_t nr_comm_events __read_mostly
;
48 static atomic_t nr_task_events __read_mostly
;
51 * perf event paranoia level:
52 * -1 - not paranoid at all
53 * 0 - disallow raw tracepoint access for unpriv
54 * 1 - disallow cpu events for unpriv
55 * 2 - disallow kernel profiling for unpriv
57 int sysctl_perf_event_paranoid __read_mostly
= 1;
59 int sysctl_perf_event_mlock __read_mostly
= 512; /* 'free' kb per user */
62 * max perf event sample rate
64 int sysctl_perf_event_sample_rate __read_mostly
= 100000;
66 static atomic64_t perf_event_id
;
69 * Lock for (sysadmin-configurable) event reservations:
71 static DEFINE_SPINLOCK(perf_resource_lock
);
74 * Architecture provided APIs - weak aliases:
76 extern __weak
const struct pmu
*hw_perf_event_init(struct perf_event
*event
)
81 void __weak
hw_perf_disable(void) { barrier(); }
82 void __weak
hw_perf_enable(void) { barrier(); }
85 hw_perf_group_sched_in(struct perf_event
*group_leader
,
86 struct perf_cpu_context
*cpuctx
,
87 struct perf_event_context
*ctx
)
92 void __weak
perf_event_print_debug(void) { }
94 static DEFINE_PER_CPU(int, perf_disable_count
);
96 void __perf_disable(void)
98 __get_cpu_var(perf_disable_count
)++;
101 bool __perf_enable(void)
103 return !--__get_cpu_var(perf_disable_count
);
106 void perf_disable(void)
112 void perf_enable(void)
118 static void get_ctx(struct perf_event_context
*ctx
)
120 WARN_ON(!atomic_inc_not_zero(&ctx
->refcount
));
123 static void free_ctx(struct rcu_head
*head
)
125 struct perf_event_context
*ctx
;
127 ctx
= container_of(head
, struct perf_event_context
, rcu_head
);
131 static void put_ctx(struct perf_event_context
*ctx
)
133 if (atomic_dec_and_test(&ctx
->refcount
)) {
135 put_ctx(ctx
->parent_ctx
);
137 put_task_struct(ctx
->task
);
138 call_rcu(&ctx
->rcu_head
, free_ctx
);
142 static void unclone_ctx(struct perf_event_context
*ctx
)
144 if (ctx
->parent_ctx
) {
145 put_ctx(ctx
->parent_ctx
);
146 ctx
->parent_ctx
= NULL
;
151 * If we inherit events we want to return the parent event id
154 static u64
primary_event_id(struct perf_event
*event
)
159 id
= event
->parent
->id
;
165 * Get the perf_event_context for a task and lock it.
166 * This has to cope with with the fact that until it is locked,
167 * the context could get moved to another task.
169 static struct perf_event_context
*
170 perf_lock_task_context(struct task_struct
*task
, unsigned long *flags
)
172 struct perf_event_context
*ctx
;
176 ctx
= rcu_dereference(task
->perf_event_ctxp
);
179 * If this context is a clone of another, it might
180 * get swapped for another underneath us by
181 * perf_event_task_sched_out, though the
182 * rcu_read_lock() protects us from any context
183 * getting freed. Lock the context and check if it
184 * got swapped before we could get the lock, and retry
185 * if so. If we locked the right context, then it
186 * can't get swapped on us any more.
188 raw_spin_lock_irqsave(&ctx
->lock
, *flags
);
189 if (ctx
!= rcu_dereference(task
->perf_event_ctxp
)) {
190 raw_spin_unlock_irqrestore(&ctx
->lock
, *flags
);
194 if (!atomic_inc_not_zero(&ctx
->refcount
)) {
195 raw_spin_unlock_irqrestore(&ctx
->lock
, *flags
);
204 * Get the context for a task and increment its pin_count so it
205 * can't get swapped to another task. This also increments its
206 * reference count so that the context can't get freed.
208 static struct perf_event_context
*perf_pin_task_context(struct task_struct
*task
)
210 struct perf_event_context
*ctx
;
213 ctx
= perf_lock_task_context(task
, &flags
);
216 raw_spin_unlock_irqrestore(&ctx
->lock
, flags
);
221 static void perf_unpin_context(struct perf_event_context
*ctx
)
225 raw_spin_lock_irqsave(&ctx
->lock
, flags
);
227 raw_spin_unlock_irqrestore(&ctx
->lock
, flags
);
231 static inline u64
perf_clock(void)
233 return cpu_clock(raw_smp_processor_id());
237 * Update the record of the current time in a context.
239 static void update_context_time(struct perf_event_context
*ctx
)
241 u64 now
= perf_clock();
243 ctx
->time
+= now
- ctx
->timestamp
;
244 ctx
->timestamp
= now
;
248 * Update the total_time_enabled and total_time_running fields for a event.
250 static void update_event_times(struct perf_event
*event
)
252 struct perf_event_context
*ctx
= event
->ctx
;
255 if (event
->state
< PERF_EVENT_STATE_INACTIVE
||
256 event
->group_leader
->state
< PERF_EVENT_STATE_INACTIVE
)
262 run_end
= event
->tstamp_stopped
;
264 event
->total_time_enabled
= run_end
- event
->tstamp_enabled
;
266 if (event
->state
== PERF_EVENT_STATE_INACTIVE
)
267 run_end
= event
->tstamp_stopped
;
271 event
->total_time_running
= run_end
- event
->tstamp_running
;
274 static struct list_head
*
275 ctx_group_list(struct perf_event
*event
, struct perf_event_context
*ctx
)
277 if (event
->attr
.pinned
)
278 return &ctx
->pinned_groups
;
280 return &ctx
->flexible_groups
;
284 * Add a event from the lists for its context.
285 * Must be called with ctx->mutex and ctx->lock held.
288 list_add_event(struct perf_event
*event
, struct perf_event_context
*ctx
)
290 struct perf_event
*group_leader
= event
->group_leader
;
293 * Depending on whether it is a standalone or sibling event,
294 * add it straight to the context's event list, or to the group
295 * leader's sibling list:
297 if (group_leader
== event
) {
298 struct list_head
*list
;
300 if (is_software_event(event
))
301 event
->group_flags
|= PERF_GROUP_SOFTWARE
;
303 list
= ctx_group_list(event
, ctx
);
304 list_add_tail(&event
->group_entry
, list
);
306 if (group_leader
->group_flags
& PERF_GROUP_SOFTWARE
&&
307 !is_software_event(event
))
308 group_leader
->group_flags
&= ~PERF_GROUP_SOFTWARE
;
310 list_add_tail(&event
->group_entry
, &group_leader
->sibling_list
);
311 group_leader
->nr_siblings
++;
314 list_add_rcu(&event
->event_entry
, &ctx
->event_list
);
316 if (event
->attr
.inherit_stat
)
321 * Remove a event from the lists for its context.
322 * Must be called with ctx->mutex and ctx->lock held.
325 list_del_event(struct perf_event
*event
, struct perf_event_context
*ctx
)
327 struct perf_event
*sibling
, *tmp
;
329 if (list_empty(&event
->group_entry
))
332 if (event
->attr
.inherit_stat
)
335 list_del_init(&event
->group_entry
);
336 list_del_rcu(&event
->event_entry
);
338 if (event
->group_leader
!= event
)
339 event
->group_leader
->nr_siblings
--;
341 update_event_times(event
);
344 * If event was in error state, then keep it
345 * that way, otherwise bogus counts will be
346 * returned on read(). The only way to get out
347 * of error state is by explicit re-enabling
350 if (event
->state
> PERF_EVENT_STATE_OFF
)
351 event
->state
= PERF_EVENT_STATE_OFF
;
354 * If this was a group event with sibling events then
355 * upgrade the siblings to singleton events by adding them
356 * to the context list directly:
358 list_for_each_entry_safe(sibling
, tmp
, &event
->sibling_list
, group_entry
) {
359 struct list_head
*list
;
361 list
= ctx_group_list(event
, ctx
);
362 list_move_tail(&sibling
->group_entry
, list
);
363 sibling
->group_leader
= sibling
;
365 /* Inherit group flags from the previous leader */
366 sibling
->group_flags
= event
->group_flags
;
371 event_sched_out(struct perf_event
*event
,
372 struct perf_cpu_context
*cpuctx
,
373 struct perf_event_context
*ctx
)
375 if (event
->state
!= PERF_EVENT_STATE_ACTIVE
)
378 event
->state
= PERF_EVENT_STATE_INACTIVE
;
379 if (event
->pending_disable
) {
380 event
->pending_disable
= 0;
381 event
->state
= PERF_EVENT_STATE_OFF
;
383 event
->tstamp_stopped
= ctx
->time
;
384 event
->pmu
->disable(event
);
387 if (!is_software_event(event
))
388 cpuctx
->active_oncpu
--;
390 if (event
->attr
.exclusive
|| !cpuctx
->active_oncpu
)
391 cpuctx
->exclusive
= 0;
395 group_sched_out(struct perf_event
*group_event
,
396 struct perf_cpu_context
*cpuctx
,
397 struct perf_event_context
*ctx
)
399 struct perf_event
*event
;
401 if (group_event
->state
!= PERF_EVENT_STATE_ACTIVE
)
404 event_sched_out(group_event
, cpuctx
, ctx
);
407 * Schedule out siblings (if any):
409 list_for_each_entry(event
, &group_event
->sibling_list
, group_entry
)
410 event_sched_out(event
, cpuctx
, ctx
);
412 if (group_event
->attr
.exclusive
)
413 cpuctx
->exclusive
= 0;
417 * Cross CPU call to remove a performance event
419 * We disable the event on the hardware level first. After that we
420 * remove it from the context list.
422 static void __perf_event_remove_from_context(void *info
)
424 struct perf_cpu_context
*cpuctx
= &__get_cpu_var(perf_cpu_context
);
425 struct perf_event
*event
= info
;
426 struct perf_event_context
*ctx
= event
->ctx
;
429 * If this is a task context, we need to check whether it is
430 * the current task context of this cpu. If not it has been
431 * scheduled out before the smp call arrived.
433 if (ctx
->task
&& cpuctx
->task_ctx
!= ctx
)
436 raw_spin_lock(&ctx
->lock
);
438 * Protect the list operation against NMI by disabling the
439 * events on a global level.
443 event_sched_out(event
, cpuctx
, ctx
);
445 list_del_event(event
, ctx
);
449 * Allow more per task events with respect to the
452 cpuctx
->max_pertask
=
453 min(perf_max_events
- ctx
->nr_events
,
454 perf_max_events
- perf_reserved_percpu
);
458 raw_spin_unlock(&ctx
->lock
);
463 * Remove the event from a task's (or a CPU's) list of events.
465 * Must be called with ctx->mutex held.
467 * CPU events are removed with a smp call. For task events we only
468 * call when the task is on a CPU.
470 * If event->ctx is a cloned context, callers must make sure that
471 * every task struct that event->ctx->task could possibly point to
472 * remains valid. This is OK when called from perf_release since
473 * that only calls us on the top-level context, which can't be a clone.
474 * When called from perf_event_exit_task, it's OK because the
475 * context has been detached from its task.
477 static void perf_event_remove_from_context(struct perf_event
*event
)
479 struct perf_event_context
*ctx
= event
->ctx
;
480 struct task_struct
*task
= ctx
->task
;
484 * Per cpu events are removed via an smp call and
485 * the removal is always successful.
487 smp_call_function_single(event
->cpu
,
488 __perf_event_remove_from_context
,
494 task_oncpu_function_call(task
, __perf_event_remove_from_context
,
497 raw_spin_lock_irq(&ctx
->lock
);
499 * If the context is active we need to retry the smp call.
501 if (ctx
->nr_active
&& !list_empty(&event
->group_entry
)) {
502 raw_spin_unlock_irq(&ctx
->lock
);
507 * The lock prevents that this context is scheduled in so we
508 * can remove the event safely, if the call above did not
511 if (!list_empty(&event
->group_entry
))
512 list_del_event(event
, ctx
);
513 raw_spin_unlock_irq(&ctx
->lock
);
517 * Update total_time_enabled and total_time_running for all events in a group.
519 static void update_group_times(struct perf_event
*leader
)
521 struct perf_event
*event
;
523 update_event_times(leader
);
524 list_for_each_entry(event
, &leader
->sibling_list
, group_entry
)
525 update_event_times(event
);
529 * Cross CPU call to disable a performance event
531 static void __perf_event_disable(void *info
)
533 struct perf_event
*event
= info
;
534 struct perf_cpu_context
*cpuctx
= &__get_cpu_var(perf_cpu_context
);
535 struct perf_event_context
*ctx
= event
->ctx
;
538 * If this is a per-task event, need to check whether this
539 * event's task is the current task on this cpu.
541 if (ctx
->task
&& cpuctx
->task_ctx
!= ctx
)
544 raw_spin_lock(&ctx
->lock
);
547 * If the event is on, turn it off.
548 * If it is in error state, leave it in error state.
550 if (event
->state
>= PERF_EVENT_STATE_INACTIVE
) {
551 update_context_time(ctx
);
552 update_group_times(event
);
553 if (event
== event
->group_leader
)
554 group_sched_out(event
, cpuctx
, ctx
);
556 event_sched_out(event
, cpuctx
, ctx
);
557 event
->state
= PERF_EVENT_STATE_OFF
;
560 raw_spin_unlock(&ctx
->lock
);
566 * If event->ctx is a cloned context, callers must make sure that
567 * every task struct that event->ctx->task could possibly point to
568 * remains valid. This condition is satisifed when called through
569 * perf_event_for_each_child or perf_event_for_each because they
570 * hold the top-level event's child_mutex, so any descendant that
571 * goes to exit will block in sync_child_event.
572 * When called from perf_pending_event it's OK because event->ctx
573 * is the current context on this CPU and preemption is disabled,
574 * hence we can't get into perf_event_task_sched_out for this context.
576 void perf_event_disable(struct perf_event
*event
)
578 struct perf_event_context
*ctx
= event
->ctx
;
579 struct task_struct
*task
= ctx
->task
;
583 * Disable the event on the cpu that it's on
585 smp_call_function_single(event
->cpu
, __perf_event_disable
,
591 task_oncpu_function_call(task
, __perf_event_disable
, event
);
593 raw_spin_lock_irq(&ctx
->lock
);
595 * If the event is still active, we need to retry the cross-call.
597 if (event
->state
== PERF_EVENT_STATE_ACTIVE
) {
598 raw_spin_unlock_irq(&ctx
->lock
);
603 * Since we have the lock this context can't be scheduled
604 * in, so we can change the state safely.
606 if (event
->state
== PERF_EVENT_STATE_INACTIVE
) {
607 update_group_times(event
);
608 event
->state
= PERF_EVENT_STATE_OFF
;
611 raw_spin_unlock_irq(&ctx
->lock
);
615 event_sched_in(struct perf_event
*event
,
616 struct perf_cpu_context
*cpuctx
,
617 struct perf_event_context
*ctx
)
619 if (event
->state
<= PERF_EVENT_STATE_OFF
)
622 event
->state
= PERF_EVENT_STATE_ACTIVE
;
623 event
->oncpu
= smp_processor_id();
625 * The new state must be visible before we turn it on in the hardware:
629 if (event
->pmu
->enable(event
)) {
630 event
->state
= PERF_EVENT_STATE_INACTIVE
;
635 event
->tstamp_running
+= ctx
->time
- event
->tstamp_stopped
;
637 if (!is_software_event(event
))
638 cpuctx
->active_oncpu
++;
641 if (event
->attr
.exclusive
)
642 cpuctx
->exclusive
= 1;
648 group_sched_in(struct perf_event
*group_event
,
649 struct perf_cpu_context
*cpuctx
,
650 struct perf_event_context
*ctx
)
652 struct perf_event
*event
, *partial_group
;
655 if (group_event
->state
== PERF_EVENT_STATE_OFF
)
658 ret
= hw_perf_group_sched_in(group_event
, cpuctx
, ctx
);
660 return ret
< 0 ? ret
: 0;
662 if (event_sched_in(group_event
, cpuctx
, ctx
))
666 * Schedule in siblings as one group (if any):
668 list_for_each_entry(event
, &group_event
->sibling_list
, group_entry
) {
669 if (event_sched_in(event
, cpuctx
, ctx
)) {
670 partial_group
= event
;
679 * Groups can be scheduled in as one unit only, so undo any
680 * partial group before returning:
682 list_for_each_entry(event
, &group_event
->sibling_list
, group_entry
) {
683 if (event
== partial_group
)
685 event_sched_out(event
, cpuctx
, ctx
);
687 event_sched_out(group_event
, cpuctx
, ctx
);
693 * Work out whether we can put this event group on the CPU now.
695 static int group_can_go_on(struct perf_event
*event
,
696 struct perf_cpu_context
*cpuctx
,
700 * Groups consisting entirely of software events can always go on.
702 if (event
->group_flags
& PERF_GROUP_SOFTWARE
)
705 * If an exclusive group is already on, no other hardware
708 if (cpuctx
->exclusive
)
711 * If this group is exclusive and there are already
712 * events on the CPU, it can't go on.
714 if (event
->attr
.exclusive
&& cpuctx
->active_oncpu
)
717 * Otherwise, try to add it if all previous groups were able
723 static void add_event_to_ctx(struct perf_event
*event
,
724 struct perf_event_context
*ctx
)
726 list_add_event(event
, ctx
);
727 event
->tstamp_enabled
= ctx
->time
;
728 event
->tstamp_running
= ctx
->time
;
729 event
->tstamp_stopped
= ctx
->time
;
733 * Cross CPU call to install and enable a performance event
735 * Must be called with ctx->mutex held
737 static void __perf_install_in_context(void *info
)
739 struct perf_cpu_context
*cpuctx
= &__get_cpu_var(perf_cpu_context
);
740 struct perf_event
*event
= info
;
741 struct perf_event_context
*ctx
= event
->ctx
;
742 struct perf_event
*leader
= event
->group_leader
;
746 * If this is a task context, we need to check whether it is
747 * the current task context of this cpu. If not it has been
748 * scheduled out before the smp call arrived.
749 * Or possibly this is the right context but it isn't
750 * on this cpu because it had no events.
752 if (ctx
->task
&& cpuctx
->task_ctx
!= ctx
) {
753 if (cpuctx
->task_ctx
|| ctx
->task
!= current
)
755 cpuctx
->task_ctx
= ctx
;
758 raw_spin_lock(&ctx
->lock
);
760 update_context_time(ctx
);
763 * Protect the list operation against NMI by disabling the
764 * events on a global level. NOP for non NMI based events.
768 add_event_to_ctx(event
, ctx
);
770 if (event
->cpu
!= -1 && event
->cpu
!= smp_processor_id())
774 * Don't put the event on if it is disabled or if
775 * it is in a group and the group isn't on.
777 if (event
->state
!= PERF_EVENT_STATE_INACTIVE
||
778 (leader
!= event
&& leader
->state
!= PERF_EVENT_STATE_ACTIVE
))
782 * An exclusive event can't go on if there are already active
783 * hardware events, and no hardware event can go on if there
784 * is already an exclusive event on.
786 if (!group_can_go_on(event
, cpuctx
, 1))
789 err
= event_sched_in(event
, cpuctx
, ctx
);
793 * This event couldn't go on. If it is in a group
794 * then we have to pull the whole group off.
795 * If the event group is pinned then put it in error state.
798 group_sched_out(leader
, cpuctx
, ctx
);
799 if (leader
->attr
.pinned
) {
800 update_group_times(leader
);
801 leader
->state
= PERF_EVENT_STATE_ERROR
;
805 if (!err
&& !ctx
->task
&& cpuctx
->max_pertask
)
806 cpuctx
->max_pertask
--;
811 raw_spin_unlock(&ctx
->lock
);
815 * Attach a performance event to a context
817 * First we add the event to the list with the hardware enable bit
818 * in event->hw_config cleared.
820 * If the event is attached to a task which is on a CPU we use a smp
821 * call to enable it in the task context. The task might have been
822 * scheduled away, but we check this in the smp call again.
824 * Must be called with ctx->mutex held.
827 perf_install_in_context(struct perf_event_context
*ctx
,
828 struct perf_event
*event
,
831 struct task_struct
*task
= ctx
->task
;
835 * Per cpu events are installed via an smp call and
836 * the install is always successful.
838 smp_call_function_single(cpu
, __perf_install_in_context
,
844 task_oncpu_function_call(task
, __perf_install_in_context
,
847 raw_spin_lock_irq(&ctx
->lock
);
849 * we need to retry the smp call.
851 if (ctx
->is_active
&& list_empty(&event
->group_entry
)) {
852 raw_spin_unlock_irq(&ctx
->lock
);
857 * The lock prevents that this context is scheduled in so we
858 * can add the event safely, if it the call above did not
861 if (list_empty(&event
->group_entry
))
862 add_event_to_ctx(event
, ctx
);
863 raw_spin_unlock_irq(&ctx
->lock
);
867 * Put a event into inactive state and update time fields.
868 * Enabling the leader of a group effectively enables all
869 * the group members that aren't explicitly disabled, so we
870 * have to update their ->tstamp_enabled also.
871 * Note: this works for group members as well as group leaders
872 * since the non-leader members' sibling_lists will be empty.
874 static void __perf_event_mark_enabled(struct perf_event
*event
,
875 struct perf_event_context
*ctx
)
877 struct perf_event
*sub
;
879 event
->state
= PERF_EVENT_STATE_INACTIVE
;
880 event
->tstamp_enabled
= ctx
->time
- event
->total_time_enabled
;
881 list_for_each_entry(sub
, &event
->sibling_list
, group_entry
)
882 if (sub
->state
>= PERF_EVENT_STATE_INACTIVE
)
883 sub
->tstamp_enabled
=
884 ctx
->time
- sub
->total_time_enabled
;
888 * Cross CPU call to enable a performance event
890 static void __perf_event_enable(void *info
)
892 struct perf_event
*event
= info
;
893 struct perf_cpu_context
*cpuctx
= &__get_cpu_var(perf_cpu_context
);
894 struct perf_event_context
*ctx
= event
->ctx
;
895 struct perf_event
*leader
= event
->group_leader
;
899 * If this is a per-task event, need to check whether this
900 * event's task is the current task on this cpu.
902 if (ctx
->task
&& cpuctx
->task_ctx
!= ctx
) {
903 if (cpuctx
->task_ctx
|| ctx
->task
!= current
)
905 cpuctx
->task_ctx
= ctx
;
908 raw_spin_lock(&ctx
->lock
);
910 update_context_time(ctx
);
912 if (event
->state
>= PERF_EVENT_STATE_INACTIVE
)
914 __perf_event_mark_enabled(event
, ctx
);
916 if (event
->cpu
!= -1 && event
->cpu
!= smp_processor_id())
920 * If the event is in a group and isn't the group leader,
921 * then don't put it on unless the group is on.
923 if (leader
!= event
&& leader
->state
!= PERF_EVENT_STATE_ACTIVE
)
926 if (!group_can_go_on(event
, cpuctx
, 1)) {
931 err
= group_sched_in(event
, cpuctx
, ctx
);
933 err
= event_sched_in(event
, cpuctx
, ctx
);
939 * If this event can't go on and it's part of a
940 * group, then the whole group has to come off.
943 group_sched_out(leader
, cpuctx
, ctx
);
944 if (leader
->attr
.pinned
) {
945 update_group_times(leader
);
946 leader
->state
= PERF_EVENT_STATE_ERROR
;
951 raw_spin_unlock(&ctx
->lock
);
957 * If event->ctx is a cloned context, callers must make sure that
958 * every task struct that event->ctx->task could possibly point to
959 * remains valid. This condition is satisfied when called through
960 * perf_event_for_each_child or perf_event_for_each as described
961 * for perf_event_disable.
963 void perf_event_enable(struct perf_event
*event
)
965 struct perf_event_context
*ctx
= event
->ctx
;
966 struct task_struct
*task
= ctx
->task
;
970 * Enable the event on the cpu that it's on
972 smp_call_function_single(event
->cpu
, __perf_event_enable
,
977 raw_spin_lock_irq(&ctx
->lock
);
978 if (event
->state
>= PERF_EVENT_STATE_INACTIVE
)
982 * If the event is in error state, clear that first.
983 * That way, if we see the event in error state below, we
984 * know that it has gone back into error state, as distinct
985 * from the task having been scheduled away before the
986 * cross-call arrived.
988 if (event
->state
== PERF_EVENT_STATE_ERROR
)
989 event
->state
= PERF_EVENT_STATE_OFF
;
992 raw_spin_unlock_irq(&ctx
->lock
);
993 task_oncpu_function_call(task
, __perf_event_enable
, event
);
995 raw_spin_lock_irq(&ctx
->lock
);
998 * If the context is active and the event is still off,
999 * we need to retry the cross-call.
1001 if (ctx
->is_active
&& event
->state
== PERF_EVENT_STATE_OFF
)
1005 * Since we have the lock this context can't be scheduled
1006 * in, so we can change the state safely.
1008 if (event
->state
== PERF_EVENT_STATE_OFF
)
1009 __perf_event_mark_enabled(event
, ctx
);
1012 raw_spin_unlock_irq(&ctx
->lock
);
1015 static int perf_event_refresh(struct perf_event
*event
, int refresh
)
1018 * not supported on inherited events
1020 if (event
->attr
.inherit
)
1023 atomic_add(refresh
, &event
->event_limit
);
1024 perf_event_enable(event
);
1030 EVENT_FLEXIBLE
= 0x1,
1032 EVENT_ALL
= EVENT_FLEXIBLE
| EVENT_PINNED
,
1035 static void ctx_sched_out(struct perf_event_context
*ctx
,
1036 struct perf_cpu_context
*cpuctx
,
1037 enum event_type_t event_type
)
1039 struct perf_event
*event
;
1041 raw_spin_lock(&ctx
->lock
);
1043 if (likely(!ctx
->nr_events
))
1045 update_context_time(ctx
);
1048 if (!ctx
->nr_active
)
1051 if (event_type
& EVENT_PINNED
)
1052 list_for_each_entry(event
, &ctx
->pinned_groups
, group_entry
)
1053 group_sched_out(event
, cpuctx
, ctx
);
1055 if (event_type
& EVENT_FLEXIBLE
)
1056 list_for_each_entry(event
, &ctx
->flexible_groups
, group_entry
)
1057 group_sched_out(event
, cpuctx
, ctx
);
1062 raw_spin_unlock(&ctx
->lock
);
1066 * Test whether two contexts are equivalent, i.e. whether they
1067 * have both been cloned from the same version of the same context
1068 * and they both have the same number of enabled events.
1069 * If the number of enabled events is the same, then the set
1070 * of enabled events should be the same, because these are both
1071 * inherited contexts, therefore we can't access individual events
1072 * in them directly with an fd; we can only enable/disable all
1073 * events via prctl, or enable/disable all events in a family
1074 * via ioctl, which will have the same effect on both contexts.
1076 static int context_equiv(struct perf_event_context
*ctx1
,
1077 struct perf_event_context
*ctx2
)
1079 return ctx1
->parent_ctx
&& ctx1
->parent_ctx
== ctx2
->parent_ctx
1080 && ctx1
->parent_gen
== ctx2
->parent_gen
1081 && !ctx1
->pin_count
&& !ctx2
->pin_count
;
1084 static void __perf_event_sync_stat(struct perf_event
*event
,
1085 struct perf_event
*next_event
)
1089 if (!event
->attr
.inherit_stat
)
1093 * Update the event value, we cannot use perf_event_read()
1094 * because we're in the middle of a context switch and have IRQs
1095 * disabled, which upsets smp_call_function_single(), however
1096 * we know the event must be on the current CPU, therefore we
1097 * don't need to use it.
1099 switch (event
->state
) {
1100 case PERF_EVENT_STATE_ACTIVE
:
1101 event
->pmu
->read(event
);
1104 case PERF_EVENT_STATE_INACTIVE
:
1105 update_event_times(event
);
1113 * In order to keep per-task stats reliable we need to flip the event
1114 * values when we flip the contexts.
1116 value
= atomic64_read(&next_event
->count
);
1117 value
= atomic64_xchg(&event
->count
, value
);
1118 atomic64_set(&next_event
->count
, value
);
1120 swap(event
->total_time_enabled
, next_event
->total_time_enabled
);
1121 swap(event
->total_time_running
, next_event
->total_time_running
);
1124 * Since we swizzled the values, update the user visible data too.
1126 perf_event_update_userpage(event
);
1127 perf_event_update_userpage(next_event
);
1130 #define list_next_entry(pos, member) \
1131 list_entry(pos->member.next, typeof(*pos), member)
1133 static void perf_event_sync_stat(struct perf_event_context
*ctx
,
1134 struct perf_event_context
*next_ctx
)
1136 struct perf_event
*event
, *next_event
;
1141 update_context_time(ctx
);
1143 event
= list_first_entry(&ctx
->event_list
,
1144 struct perf_event
, event_entry
);
1146 next_event
= list_first_entry(&next_ctx
->event_list
,
1147 struct perf_event
, event_entry
);
1149 while (&event
->event_entry
!= &ctx
->event_list
&&
1150 &next_event
->event_entry
!= &next_ctx
->event_list
) {
1152 __perf_event_sync_stat(event
, next_event
);
1154 event
= list_next_entry(event
, event_entry
);
1155 next_event
= list_next_entry(next_event
, event_entry
);
1160 * Called from scheduler to remove the events of the current task,
1161 * with interrupts disabled.
1163 * We stop each event and update the event value in event->count.
1165 * This does not protect us against NMI, but disable()
1166 * sets the disabled bit in the control field of event _before_
1167 * accessing the event control register. If a NMI hits, then it will
1168 * not restart the event.
1170 void perf_event_task_sched_out(struct task_struct
*task
,
1171 struct task_struct
*next
)
1173 struct perf_cpu_context
*cpuctx
= &__get_cpu_var(perf_cpu_context
);
1174 struct perf_event_context
*ctx
= task
->perf_event_ctxp
;
1175 struct perf_event_context
*next_ctx
;
1176 struct perf_event_context
*parent
;
1177 struct pt_regs
*regs
;
1180 regs
= task_pt_regs(task
);
1181 perf_sw_event(PERF_COUNT_SW_CONTEXT_SWITCHES
, 1, 1, regs
, 0);
1183 if (likely(!ctx
|| !cpuctx
->task_ctx
))
1187 parent
= rcu_dereference(ctx
->parent_ctx
);
1188 next_ctx
= next
->perf_event_ctxp
;
1189 if (parent
&& next_ctx
&&
1190 rcu_dereference(next_ctx
->parent_ctx
) == parent
) {
1192 * Looks like the two contexts are clones, so we might be
1193 * able to optimize the context switch. We lock both
1194 * contexts and check that they are clones under the
1195 * lock (including re-checking that neither has been
1196 * uncloned in the meantime). It doesn't matter which
1197 * order we take the locks because no other cpu could
1198 * be trying to lock both of these tasks.
1200 raw_spin_lock(&ctx
->lock
);
1201 raw_spin_lock_nested(&next_ctx
->lock
, SINGLE_DEPTH_NESTING
);
1202 if (context_equiv(ctx
, next_ctx
)) {
1204 * XXX do we need a memory barrier of sorts
1205 * wrt to rcu_dereference() of perf_event_ctxp
1207 task
->perf_event_ctxp
= next_ctx
;
1208 next
->perf_event_ctxp
= ctx
;
1210 next_ctx
->task
= task
;
1213 perf_event_sync_stat(ctx
, next_ctx
);
1215 raw_spin_unlock(&next_ctx
->lock
);
1216 raw_spin_unlock(&ctx
->lock
);
1221 ctx_sched_out(ctx
, cpuctx
, EVENT_ALL
);
1222 cpuctx
->task_ctx
= NULL
;
1226 static void task_ctx_sched_out(struct perf_event_context
*ctx
,
1227 enum event_type_t event_type
)
1229 struct perf_cpu_context
*cpuctx
= &__get_cpu_var(perf_cpu_context
);
1231 if (!cpuctx
->task_ctx
)
1234 if (WARN_ON_ONCE(ctx
!= cpuctx
->task_ctx
))
1237 ctx_sched_out(ctx
, cpuctx
, event_type
);
1238 cpuctx
->task_ctx
= NULL
;
1242 * Called with IRQs disabled
1244 static void __perf_event_task_sched_out(struct perf_event_context
*ctx
)
1246 task_ctx_sched_out(ctx
, EVENT_ALL
);
1250 * Called with IRQs disabled
1252 static void cpu_ctx_sched_out(struct perf_cpu_context
*cpuctx
,
1253 enum event_type_t event_type
)
1255 ctx_sched_out(&cpuctx
->ctx
, cpuctx
, event_type
);
1259 ctx_pinned_sched_in(struct perf_event_context
*ctx
,
1260 struct perf_cpu_context
*cpuctx
)
1262 struct perf_event
*event
;
1264 list_for_each_entry(event
, &ctx
->pinned_groups
, group_entry
) {
1265 if (event
->state
<= PERF_EVENT_STATE_OFF
)
1267 if (event
->cpu
!= -1 && event
->cpu
!= smp_processor_id())
1270 if (group_can_go_on(event
, cpuctx
, 1))
1271 group_sched_in(event
, cpuctx
, ctx
);
1274 * If this pinned group hasn't been scheduled,
1275 * put it in error state.
1277 if (event
->state
== PERF_EVENT_STATE_INACTIVE
) {
1278 update_group_times(event
);
1279 event
->state
= PERF_EVENT_STATE_ERROR
;
1285 ctx_flexible_sched_in(struct perf_event_context
*ctx
,
1286 struct perf_cpu_context
*cpuctx
)
1288 struct perf_event
*event
;
1291 list_for_each_entry(event
, &ctx
->flexible_groups
, group_entry
) {
1292 /* Ignore events in OFF or ERROR state */
1293 if (event
->state
<= PERF_EVENT_STATE_OFF
)
1296 * Listen to the 'cpu' scheduling filter constraint
1299 if (event
->cpu
!= -1 && event
->cpu
!= smp_processor_id())
1302 if (group_can_go_on(event
, cpuctx
, can_add_hw
))
1303 if (group_sched_in(event
, cpuctx
, ctx
))
1309 ctx_sched_in(struct perf_event_context
*ctx
,
1310 struct perf_cpu_context
*cpuctx
,
1311 enum event_type_t event_type
)
1313 raw_spin_lock(&ctx
->lock
);
1315 if (likely(!ctx
->nr_events
))
1318 ctx
->timestamp
= perf_clock();
1323 * First go through the list and put on any pinned groups
1324 * in order to give them the best chance of going on.
1326 if (event_type
& EVENT_PINNED
)
1327 ctx_pinned_sched_in(ctx
, cpuctx
);
1329 /* Then walk through the lower prio flexible groups */
1330 if (event_type
& EVENT_FLEXIBLE
)
1331 ctx_flexible_sched_in(ctx
, cpuctx
);
1335 raw_spin_unlock(&ctx
->lock
);
1338 static void cpu_ctx_sched_in(struct perf_cpu_context
*cpuctx
,
1339 enum event_type_t event_type
)
1341 struct perf_event_context
*ctx
= &cpuctx
->ctx
;
1343 ctx_sched_in(ctx
, cpuctx
, event_type
);
1346 static void task_ctx_sched_in(struct task_struct
*task
,
1347 enum event_type_t event_type
)
1349 struct perf_cpu_context
*cpuctx
= &__get_cpu_var(perf_cpu_context
);
1350 struct perf_event_context
*ctx
= task
->perf_event_ctxp
;
1354 if (cpuctx
->task_ctx
== ctx
)
1356 ctx_sched_in(ctx
, cpuctx
, event_type
);
1357 cpuctx
->task_ctx
= ctx
;
1360 * Called from scheduler to add the events of the current task
1361 * with interrupts disabled.
1363 * We restore the event value and then enable it.
1365 * This does not protect us against NMI, but enable()
1366 * sets the enabled bit in the control field of event _before_
1367 * accessing the event control register. If a NMI hits, then it will
1368 * keep the event running.
1370 void perf_event_task_sched_in(struct task_struct
*task
)
1372 struct perf_cpu_context
*cpuctx
= &__get_cpu_var(perf_cpu_context
);
1373 struct perf_event_context
*ctx
= task
->perf_event_ctxp
;
1378 if (cpuctx
->task_ctx
== ctx
)
1382 * We want to keep the following priority order:
1383 * cpu pinned (that don't need to move), task pinned,
1384 * cpu flexible, task flexible.
1386 cpu_ctx_sched_out(cpuctx
, EVENT_FLEXIBLE
);
1388 ctx_sched_in(ctx
, cpuctx
, EVENT_PINNED
);
1389 cpu_ctx_sched_in(cpuctx
, EVENT_FLEXIBLE
);
1390 ctx_sched_in(ctx
, cpuctx
, EVENT_FLEXIBLE
);
1392 cpuctx
->task_ctx
= ctx
;
1395 #define MAX_INTERRUPTS (~0ULL)
1397 static void perf_log_throttle(struct perf_event
*event
, int enable
);
1399 static u64
perf_calculate_period(struct perf_event
*event
, u64 nsec
, u64 count
)
1401 u64 frequency
= event
->attr
.sample_freq
;
1402 u64 sec
= NSEC_PER_SEC
;
1403 u64 divisor
, dividend
;
1405 int count_fls
, nsec_fls
, frequency_fls
, sec_fls
;
1407 count_fls
= fls64(count
);
1408 nsec_fls
= fls64(nsec
);
1409 frequency_fls
= fls64(frequency
);
1413 * We got @count in @nsec, with a target of sample_freq HZ
1414 * the target period becomes:
1417 * period = -------------------
1418 * @nsec * sample_freq
1423 * Reduce accuracy by one bit such that @a and @b converge
1424 * to a similar magnitude.
1426 #define REDUCE_FLS(a, b) \
1428 if (a##_fls > b##_fls) { \
1438 * Reduce accuracy until either term fits in a u64, then proceed with
1439 * the other, so that finally we can do a u64/u64 division.
1441 while (count_fls
+ sec_fls
> 64 && nsec_fls
+ frequency_fls
> 64) {
1442 REDUCE_FLS(nsec
, frequency
);
1443 REDUCE_FLS(sec
, count
);
1446 if (count_fls
+ sec_fls
> 64) {
1447 divisor
= nsec
* frequency
;
1449 while (count_fls
+ sec_fls
> 64) {
1450 REDUCE_FLS(count
, sec
);
1454 dividend
= count
* sec
;
1456 dividend
= count
* sec
;
1458 while (nsec_fls
+ frequency_fls
> 64) {
1459 REDUCE_FLS(nsec
, frequency
);
1463 divisor
= nsec
* frequency
;
1466 return div64_u64(dividend
, divisor
);
1469 static void perf_event_stop(struct perf_event
*event
)
1471 if (!event
->pmu
->stop
)
1472 return event
->pmu
->disable(event
);
1474 return event
->pmu
->stop(event
);
1477 static int perf_event_start(struct perf_event
*event
)
1479 if (!event
->pmu
->start
)
1480 return event
->pmu
->enable(event
);
1482 return event
->pmu
->start(event
);
1485 static void perf_adjust_period(struct perf_event
*event
, u64 nsec
, u64 count
)
1487 struct hw_perf_event
*hwc
= &event
->hw
;
1488 u64 period
, sample_period
;
1491 period
= perf_calculate_period(event
, nsec
, count
);
1493 delta
= (s64
)(period
- hwc
->sample_period
);
1494 delta
= (delta
+ 7) / 8; /* low pass filter */
1496 sample_period
= hwc
->sample_period
+ delta
;
1501 hwc
->sample_period
= sample_period
;
1503 if (atomic64_read(&hwc
->period_left
) > 8*sample_period
) {
1505 perf_event_stop(event
);
1506 atomic64_set(&hwc
->period_left
, 0);
1507 perf_event_start(event
);
1512 static void perf_ctx_adjust_freq(struct perf_event_context
*ctx
)
1514 struct perf_event
*event
;
1515 struct hw_perf_event
*hwc
;
1516 u64 interrupts
, now
;
1519 raw_spin_lock(&ctx
->lock
);
1520 list_for_each_entry_rcu(event
, &ctx
->event_list
, event_entry
) {
1521 if (event
->state
!= PERF_EVENT_STATE_ACTIVE
)
1524 if (event
->cpu
!= -1 && event
->cpu
!= smp_processor_id())
1529 interrupts
= hwc
->interrupts
;
1530 hwc
->interrupts
= 0;
1533 * unthrottle events on the tick
1535 if (interrupts
== MAX_INTERRUPTS
) {
1536 perf_log_throttle(event
, 1);
1537 event
->pmu
->unthrottle(event
);
1540 if (!event
->attr
.freq
|| !event
->attr
.sample_freq
)
1543 event
->pmu
->read(event
);
1544 now
= atomic64_read(&event
->count
);
1545 delta
= now
- hwc
->freq_count_stamp
;
1546 hwc
->freq_count_stamp
= now
;
1549 perf_adjust_period(event
, TICK_NSEC
, delta
);
1551 raw_spin_unlock(&ctx
->lock
);
1555 * Round-robin a context's events:
1557 static void rotate_ctx(struct perf_event_context
*ctx
)
1559 if (!ctx
->nr_events
)
1562 raw_spin_lock(&ctx
->lock
);
1564 /* Rotate the first entry last of non-pinned groups */
1565 list_rotate_left(&ctx
->flexible_groups
);
1567 raw_spin_unlock(&ctx
->lock
);
1570 void perf_event_task_tick(struct task_struct
*curr
)
1572 struct perf_cpu_context
*cpuctx
;
1573 struct perf_event_context
*ctx
;
1575 if (!atomic_read(&nr_events
))
1578 cpuctx
= &__get_cpu_var(perf_cpu_context
);
1579 ctx
= curr
->perf_event_ctxp
;
1583 perf_ctx_adjust_freq(&cpuctx
->ctx
);
1585 perf_ctx_adjust_freq(ctx
);
1587 cpu_ctx_sched_out(cpuctx
, EVENT_FLEXIBLE
);
1589 task_ctx_sched_out(ctx
, EVENT_FLEXIBLE
);
1591 rotate_ctx(&cpuctx
->ctx
);
1595 cpu_ctx_sched_in(cpuctx
, EVENT_FLEXIBLE
);
1597 task_ctx_sched_in(curr
, EVENT_FLEXIBLE
);
1602 static int event_enable_on_exec(struct perf_event
*event
,
1603 struct perf_event_context
*ctx
)
1605 if (!event
->attr
.enable_on_exec
)
1608 event
->attr
.enable_on_exec
= 0;
1609 if (event
->state
>= PERF_EVENT_STATE_INACTIVE
)
1612 __perf_event_mark_enabled(event
, ctx
);
1618 * Enable all of a task's events that have been marked enable-on-exec.
1619 * This expects task == current.
1621 static void perf_event_enable_on_exec(struct task_struct
*task
)
1623 struct perf_event_context
*ctx
;
1624 struct perf_event
*event
;
1625 unsigned long flags
;
1629 local_irq_save(flags
);
1630 ctx
= task
->perf_event_ctxp
;
1631 if (!ctx
|| !ctx
->nr_events
)
1634 __perf_event_task_sched_out(ctx
);
1636 raw_spin_lock(&ctx
->lock
);
1638 list_for_each_entry(event
, &ctx
->pinned_groups
, group_entry
) {
1639 ret
= event_enable_on_exec(event
, ctx
);
1644 list_for_each_entry(event
, &ctx
->flexible_groups
, group_entry
) {
1645 ret
= event_enable_on_exec(event
, ctx
);
1651 * Unclone this context if we enabled any event.
1656 raw_spin_unlock(&ctx
->lock
);
1658 perf_event_task_sched_in(task
);
1660 local_irq_restore(flags
);
1664 * Cross CPU call to read the hardware event
1666 static void __perf_event_read(void *info
)
1668 struct perf_cpu_context
*cpuctx
= &__get_cpu_var(perf_cpu_context
);
1669 struct perf_event
*event
= info
;
1670 struct perf_event_context
*ctx
= event
->ctx
;
1673 * If this is a task context, we need to check whether it is
1674 * the current task context of this cpu. If not it has been
1675 * scheduled out before the smp call arrived. In that case
1676 * event->count would have been updated to a recent sample
1677 * when the event was scheduled out.
1679 if (ctx
->task
&& cpuctx
->task_ctx
!= ctx
)
1682 raw_spin_lock(&ctx
->lock
);
1683 update_context_time(ctx
);
1684 update_event_times(event
);
1685 raw_spin_unlock(&ctx
->lock
);
1687 event
->pmu
->read(event
);
1690 static u64
perf_event_read(struct perf_event
*event
)
1693 * If event is enabled and currently active on a CPU, update the
1694 * value in the event structure:
1696 if (event
->state
== PERF_EVENT_STATE_ACTIVE
) {
1697 smp_call_function_single(event
->oncpu
,
1698 __perf_event_read
, event
, 1);
1699 } else if (event
->state
== PERF_EVENT_STATE_INACTIVE
) {
1700 struct perf_event_context
*ctx
= event
->ctx
;
1701 unsigned long flags
;
1703 raw_spin_lock_irqsave(&ctx
->lock
, flags
);
1704 update_context_time(ctx
);
1705 update_event_times(event
);
1706 raw_spin_unlock_irqrestore(&ctx
->lock
, flags
);
1709 return atomic64_read(&event
->count
);
1713 * Initialize the perf_event context in a task_struct:
1716 __perf_event_init_context(struct perf_event_context
*ctx
,
1717 struct task_struct
*task
)
1719 raw_spin_lock_init(&ctx
->lock
);
1720 mutex_init(&ctx
->mutex
);
1721 INIT_LIST_HEAD(&ctx
->pinned_groups
);
1722 INIT_LIST_HEAD(&ctx
->flexible_groups
);
1723 INIT_LIST_HEAD(&ctx
->event_list
);
1724 atomic_set(&ctx
->refcount
, 1);
1728 static struct perf_event_context
*find_get_context(pid_t pid
, int cpu
)
1730 struct perf_event_context
*ctx
;
1731 struct perf_cpu_context
*cpuctx
;
1732 struct task_struct
*task
;
1733 unsigned long flags
;
1736 if (pid
== -1 && cpu
!= -1) {
1737 /* Must be root to operate on a CPU event: */
1738 if (perf_paranoid_cpu() && !capable(CAP_SYS_ADMIN
))
1739 return ERR_PTR(-EACCES
);
1741 if (cpu
< 0 || cpu
>= nr_cpumask_bits
)
1742 return ERR_PTR(-EINVAL
);
1745 * We could be clever and allow to attach a event to an
1746 * offline CPU and activate it when the CPU comes up, but
1749 if (!cpu_online(cpu
))
1750 return ERR_PTR(-ENODEV
);
1752 cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
1763 task
= find_task_by_vpid(pid
);
1765 get_task_struct(task
);
1769 return ERR_PTR(-ESRCH
);
1772 * Can't attach events to a dying task.
1775 if (task
->flags
& PF_EXITING
)
1778 /* Reuse ptrace permission checks for now. */
1780 if (!ptrace_may_access(task
, PTRACE_MODE_READ
))
1784 ctx
= perf_lock_task_context(task
, &flags
);
1787 raw_spin_unlock_irqrestore(&ctx
->lock
, flags
);
1791 ctx
= kzalloc(sizeof(struct perf_event_context
), GFP_KERNEL
);
1795 __perf_event_init_context(ctx
, task
);
1797 if (cmpxchg(&task
->perf_event_ctxp
, NULL
, ctx
)) {
1799 * We raced with some other task; use
1800 * the context they set.
1805 get_task_struct(task
);
1808 put_task_struct(task
);
1812 put_task_struct(task
);
1813 return ERR_PTR(err
);
1816 static void perf_event_free_filter(struct perf_event
*event
);
1818 static void free_event_rcu(struct rcu_head
*head
)
1820 struct perf_event
*event
;
1822 event
= container_of(head
, struct perf_event
, rcu_head
);
1824 put_pid_ns(event
->ns
);
1825 perf_event_free_filter(event
);
1829 static void perf_pending_sync(struct perf_event
*event
);
1831 static void free_event(struct perf_event
*event
)
1833 perf_pending_sync(event
);
1835 if (!event
->parent
) {
1836 atomic_dec(&nr_events
);
1837 if (event
->attr
.mmap
)
1838 atomic_dec(&nr_mmap_events
);
1839 if (event
->attr
.comm
)
1840 atomic_dec(&nr_comm_events
);
1841 if (event
->attr
.task
)
1842 atomic_dec(&nr_task_events
);
1845 if (event
->output
) {
1846 fput(event
->output
->filp
);
1847 event
->output
= NULL
;
1851 event
->destroy(event
);
1853 put_ctx(event
->ctx
);
1854 call_rcu(&event
->rcu_head
, free_event_rcu
);
1857 int perf_event_release_kernel(struct perf_event
*event
)
1859 struct perf_event_context
*ctx
= event
->ctx
;
1861 WARN_ON_ONCE(ctx
->parent_ctx
);
1862 mutex_lock(&ctx
->mutex
);
1863 perf_event_remove_from_context(event
);
1864 mutex_unlock(&ctx
->mutex
);
1866 mutex_lock(&event
->owner
->perf_event_mutex
);
1867 list_del_init(&event
->owner_entry
);
1868 mutex_unlock(&event
->owner
->perf_event_mutex
);
1869 put_task_struct(event
->owner
);
1875 EXPORT_SYMBOL_GPL(perf_event_release_kernel
);
1878 * Called when the last reference to the file is gone.
1880 static int perf_release(struct inode
*inode
, struct file
*file
)
1882 struct perf_event
*event
= file
->private_data
;
1884 file
->private_data
= NULL
;
1886 return perf_event_release_kernel(event
);
1889 static int perf_event_read_size(struct perf_event
*event
)
1891 int entry
= sizeof(u64
); /* value */
1895 if (event
->attr
.read_format
& PERF_FORMAT_TOTAL_TIME_ENABLED
)
1896 size
+= sizeof(u64
);
1898 if (event
->attr
.read_format
& PERF_FORMAT_TOTAL_TIME_RUNNING
)
1899 size
+= sizeof(u64
);
1901 if (event
->attr
.read_format
& PERF_FORMAT_ID
)
1902 entry
+= sizeof(u64
);
1904 if (event
->attr
.read_format
& PERF_FORMAT_GROUP
) {
1905 nr
+= event
->group_leader
->nr_siblings
;
1906 size
+= sizeof(u64
);
1914 u64
perf_event_read_value(struct perf_event
*event
, u64
*enabled
, u64
*running
)
1916 struct perf_event
*child
;
1922 mutex_lock(&event
->child_mutex
);
1923 total
+= perf_event_read(event
);
1924 *enabled
+= event
->total_time_enabled
+
1925 atomic64_read(&event
->child_total_time_enabled
);
1926 *running
+= event
->total_time_running
+
1927 atomic64_read(&event
->child_total_time_running
);
1929 list_for_each_entry(child
, &event
->child_list
, child_list
) {
1930 total
+= perf_event_read(child
);
1931 *enabled
+= child
->total_time_enabled
;
1932 *running
+= child
->total_time_running
;
1934 mutex_unlock(&event
->child_mutex
);
1938 EXPORT_SYMBOL_GPL(perf_event_read_value
);
1940 static int perf_event_read_group(struct perf_event
*event
,
1941 u64 read_format
, char __user
*buf
)
1943 struct perf_event
*leader
= event
->group_leader
, *sub
;
1944 int n
= 0, size
= 0, ret
= -EFAULT
;
1945 struct perf_event_context
*ctx
= leader
->ctx
;
1947 u64 count
, enabled
, running
;
1949 mutex_lock(&ctx
->mutex
);
1950 count
= perf_event_read_value(leader
, &enabled
, &running
);
1952 values
[n
++] = 1 + leader
->nr_siblings
;
1953 if (read_format
& PERF_FORMAT_TOTAL_TIME_ENABLED
)
1954 values
[n
++] = enabled
;
1955 if (read_format
& PERF_FORMAT_TOTAL_TIME_RUNNING
)
1956 values
[n
++] = running
;
1957 values
[n
++] = count
;
1958 if (read_format
& PERF_FORMAT_ID
)
1959 values
[n
++] = primary_event_id(leader
);
1961 size
= n
* sizeof(u64
);
1963 if (copy_to_user(buf
, values
, size
))
1968 list_for_each_entry(sub
, &leader
->sibling_list
, group_entry
) {
1971 values
[n
++] = perf_event_read_value(sub
, &enabled
, &running
);
1972 if (read_format
& PERF_FORMAT_ID
)
1973 values
[n
++] = primary_event_id(sub
);
1975 size
= n
* sizeof(u64
);
1977 if (copy_to_user(buf
+ ret
, values
, size
)) {
1985 mutex_unlock(&ctx
->mutex
);
1990 static int perf_event_read_one(struct perf_event
*event
,
1991 u64 read_format
, char __user
*buf
)
1993 u64 enabled
, running
;
1997 values
[n
++] = perf_event_read_value(event
, &enabled
, &running
);
1998 if (read_format
& PERF_FORMAT_TOTAL_TIME_ENABLED
)
1999 values
[n
++] = enabled
;
2000 if (read_format
& PERF_FORMAT_TOTAL_TIME_RUNNING
)
2001 values
[n
++] = running
;
2002 if (read_format
& PERF_FORMAT_ID
)
2003 values
[n
++] = primary_event_id(event
);
2005 if (copy_to_user(buf
, values
, n
* sizeof(u64
)))
2008 return n
* sizeof(u64
);
2012 * Read the performance event - simple non blocking version for now
2015 perf_read_hw(struct perf_event
*event
, char __user
*buf
, size_t count
)
2017 u64 read_format
= event
->attr
.read_format
;
2021 * Return end-of-file for a read on a event that is in
2022 * error state (i.e. because it was pinned but it couldn't be
2023 * scheduled on to the CPU at some point).
2025 if (event
->state
== PERF_EVENT_STATE_ERROR
)
2028 if (count
< perf_event_read_size(event
))
2031 WARN_ON_ONCE(event
->ctx
->parent_ctx
);
2032 if (read_format
& PERF_FORMAT_GROUP
)
2033 ret
= perf_event_read_group(event
, read_format
, buf
);
2035 ret
= perf_event_read_one(event
, read_format
, buf
);
2041 perf_read(struct file
*file
, char __user
*buf
, size_t count
, loff_t
*ppos
)
2043 struct perf_event
*event
= file
->private_data
;
2045 return perf_read_hw(event
, buf
, count
);
2048 static unsigned int perf_poll(struct file
*file
, poll_table
*wait
)
2050 struct perf_event
*event
= file
->private_data
;
2051 struct perf_mmap_data
*data
;
2052 unsigned int events
= POLL_HUP
;
2055 data
= rcu_dereference(event
->data
);
2057 events
= atomic_xchg(&data
->poll
, 0);
2060 poll_wait(file
, &event
->waitq
, wait
);
2065 static void perf_event_reset(struct perf_event
*event
)
2067 (void)perf_event_read(event
);
2068 atomic64_set(&event
->count
, 0);
2069 perf_event_update_userpage(event
);
2073 * Holding the top-level event's child_mutex means that any
2074 * descendant process that has inherited this event will block
2075 * in sync_child_event if it goes to exit, thus satisfying the
2076 * task existence requirements of perf_event_enable/disable.
2078 static void perf_event_for_each_child(struct perf_event
*event
,
2079 void (*func
)(struct perf_event
*))
2081 struct perf_event
*child
;
2083 WARN_ON_ONCE(event
->ctx
->parent_ctx
);
2084 mutex_lock(&event
->child_mutex
);
2086 list_for_each_entry(child
, &event
->child_list
, child_list
)
2088 mutex_unlock(&event
->child_mutex
);
2091 static void perf_event_for_each(struct perf_event
*event
,
2092 void (*func
)(struct perf_event
*))
2094 struct perf_event_context
*ctx
= event
->ctx
;
2095 struct perf_event
*sibling
;
2097 WARN_ON_ONCE(ctx
->parent_ctx
);
2098 mutex_lock(&ctx
->mutex
);
2099 event
= event
->group_leader
;
2101 perf_event_for_each_child(event
, func
);
2103 list_for_each_entry(sibling
, &event
->sibling_list
, group_entry
)
2104 perf_event_for_each_child(event
, func
);
2105 mutex_unlock(&ctx
->mutex
);
2108 static int perf_event_period(struct perf_event
*event
, u64 __user
*arg
)
2110 struct perf_event_context
*ctx
= event
->ctx
;
2115 if (!event
->attr
.sample_period
)
2118 size
= copy_from_user(&value
, arg
, sizeof(value
));
2119 if (size
!= sizeof(value
))
2125 raw_spin_lock_irq(&ctx
->lock
);
2126 if (event
->attr
.freq
) {
2127 if (value
> sysctl_perf_event_sample_rate
) {
2132 event
->attr
.sample_freq
= value
;
2134 event
->attr
.sample_period
= value
;
2135 event
->hw
.sample_period
= value
;
2138 raw_spin_unlock_irq(&ctx
->lock
);
2143 static int perf_event_set_output(struct perf_event
*event
, int output_fd
);
2144 static int perf_event_set_filter(struct perf_event
*event
, void __user
*arg
);
2146 static long perf_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
2148 struct perf_event
*event
= file
->private_data
;
2149 void (*func
)(struct perf_event
*);
2153 case PERF_EVENT_IOC_ENABLE
:
2154 func
= perf_event_enable
;
2156 case PERF_EVENT_IOC_DISABLE
:
2157 func
= perf_event_disable
;
2159 case PERF_EVENT_IOC_RESET
:
2160 func
= perf_event_reset
;
2163 case PERF_EVENT_IOC_REFRESH
:
2164 return perf_event_refresh(event
, arg
);
2166 case PERF_EVENT_IOC_PERIOD
:
2167 return perf_event_period(event
, (u64 __user
*)arg
);
2169 case PERF_EVENT_IOC_SET_OUTPUT
:
2170 return perf_event_set_output(event
, arg
);
2172 case PERF_EVENT_IOC_SET_FILTER
:
2173 return perf_event_set_filter(event
, (void __user
*)arg
);
2179 if (flags
& PERF_IOC_FLAG_GROUP
)
2180 perf_event_for_each(event
, func
);
2182 perf_event_for_each_child(event
, func
);
2187 int perf_event_task_enable(void)
2189 struct perf_event
*event
;
2191 mutex_lock(¤t
->perf_event_mutex
);
2192 list_for_each_entry(event
, ¤t
->perf_event_list
, owner_entry
)
2193 perf_event_for_each_child(event
, perf_event_enable
);
2194 mutex_unlock(¤t
->perf_event_mutex
);
2199 int perf_event_task_disable(void)
2201 struct perf_event
*event
;
2203 mutex_lock(¤t
->perf_event_mutex
);
2204 list_for_each_entry(event
, ¤t
->perf_event_list
, owner_entry
)
2205 perf_event_for_each_child(event
, perf_event_disable
);
2206 mutex_unlock(¤t
->perf_event_mutex
);
2211 #ifndef PERF_EVENT_INDEX_OFFSET
2212 # define PERF_EVENT_INDEX_OFFSET 0
2215 static int perf_event_index(struct perf_event
*event
)
2217 if (event
->state
!= PERF_EVENT_STATE_ACTIVE
)
2220 return event
->hw
.idx
+ 1 - PERF_EVENT_INDEX_OFFSET
;
2224 * Callers need to ensure there can be no nesting of this function, otherwise
2225 * the seqlock logic goes bad. We can not serialize this because the arch
2226 * code calls this from NMI context.
2228 void perf_event_update_userpage(struct perf_event
*event
)
2230 struct perf_event_mmap_page
*userpg
;
2231 struct perf_mmap_data
*data
;
2234 data
= rcu_dereference(event
->data
);
2238 userpg
= data
->user_page
;
2241 * Disable preemption so as to not let the corresponding user-space
2242 * spin too long if we get preempted.
2247 userpg
->index
= perf_event_index(event
);
2248 userpg
->offset
= atomic64_read(&event
->count
);
2249 if (event
->state
== PERF_EVENT_STATE_ACTIVE
)
2250 userpg
->offset
-= atomic64_read(&event
->hw
.prev_count
);
2252 userpg
->time_enabled
= event
->total_time_enabled
+
2253 atomic64_read(&event
->child_total_time_enabled
);
2255 userpg
->time_running
= event
->total_time_running
+
2256 atomic64_read(&event
->child_total_time_running
);
2265 static unsigned long perf_data_size(struct perf_mmap_data
*data
)
2267 return data
->nr_pages
<< (PAGE_SHIFT
+ data
->data_order
);
2270 #ifndef CONFIG_PERF_USE_VMALLOC
2273 * Back perf_mmap() with regular GFP_KERNEL-0 pages.
2276 static struct page
*
2277 perf_mmap_to_page(struct perf_mmap_data
*data
, unsigned long pgoff
)
2279 if (pgoff
> data
->nr_pages
)
2283 return virt_to_page(data
->user_page
);
2285 return virt_to_page(data
->data_pages
[pgoff
- 1]);
2288 static struct perf_mmap_data
*
2289 perf_mmap_data_alloc(struct perf_event
*event
, int nr_pages
)
2291 struct perf_mmap_data
*data
;
2295 WARN_ON(atomic_read(&event
->mmap_count
));
2297 size
= sizeof(struct perf_mmap_data
);
2298 size
+= nr_pages
* sizeof(void *);
2300 data
= kzalloc(size
, GFP_KERNEL
);
2304 data
->user_page
= (void *)get_zeroed_page(GFP_KERNEL
);
2305 if (!data
->user_page
)
2306 goto fail_user_page
;
2308 for (i
= 0; i
< nr_pages
; i
++) {
2309 data
->data_pages
[i
] = (void *)get_zeroed_page(GFP_KERNEL
);
2310 if (!data
->data_pages
[i
])
2311 goto fail_data_pages
;
2314 data
->data_order
= 0;
2315 data
->nr_pages
= nr_pages
;
2320 for (i
--; i
>= 0; i
--)
2321 free_page((unsigned long)data
->data_pages
[i
]);
2323 free_page((unsigned long)data
->user_page
);
2332 static void perf_mmap_free_page(unsigned long addr
)
2334 struct page
*page
= virt_to_page((void *)addr
);
2336 page
->mapping
= NULL
;
2340 static void perf_mmap_data_free(struct perf_mmap_data
*data
)
2344 perf_mmap_free_page((unsigned long)data
->user_page
);
2345 for (i
= 0; i
< data
->nr_pages
; i
++)
2346 perf_mmap_free_page((unsigned long)data
->data_pages
[i
]);
2353 * Back perf_mmap() with vmalloc memory.
2355 * Required for architectures that have d-cache aliasing issues.
2358 static struct page
*
2359 perf_mmap_to_page(struct perf_mmap_data
*data
, unsigned long pgoff
)
2361 if (pgoff
> (1UL << data
->data_order
))
2364 return vmalloc_to_page((void *)data
->user_page
+ pgoff
* PAGE_SIZE
);
2367 static void perf_mmap_unmark_page(void *addr
)
2369 struct page
*page
= vmalloc_to_page(addr
);
2371 page
->mapping
= NULL
;
2374 static void perf_mmap_data_free_work(struct work_struct
*work
)
2376 struct perf_mmap_data
*data
;
2380 data
= container_of(work
, struct perf_mmap_data
, work
);
2381 nr
= 1 << data
->data_order
;
2383 base
= data
->user_page
;
2384 for (i
= 0; i
< nr
+ 1; i
++)
2385 perf_mmap_unmark_page(base
+ (i
* PAGE_SIZE
));
2391 static void perf_mmap_data_free(struct perf_mmap_data
*data
)
2393 schedule_work(&data
->work
);
2396 static struct perf_mmap_data
*
2397 perf_mmap_data_alloc(struct perf_event
*event
, int nr_pages
)
2399 struct perf_mmap_data
*data
;
2403 WARN_ON(atomic_read(&event
->mmap_count
));
2405 size
= sizeof(struct perf_mmap_data
);
2406 size
+= sizeof(void *);
2408 data
= kzalloc(size
, GFP_KERNEL
);
2412 INIT_WORK(&data
->work
, perf_mmap_data_free_work
);
2414 all_buf
= vmalloc_user((nr_pages
+ 1) * PAGE_SIZE
);
2418 data
->user_page
= all_buf
;
2419 data
->data_pages
[0] = all_buf
+ PAGE_SIZE
;
2420 data
->data_order
= ilog2(nr_pages
);
2434 static int perf_mmap_fault(struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
2436 struct perf_event
*event
= vma
->vm_file
->private_data
;
2437 struct perf_mmap_data
*data
;
2438 int ret
= VM_FAULT_SIGBUS
;
2440 if (vmf
->flags
& FAULT_FLAG_MKWRITE
) {
2441 if (vmf
->pgoff
== 0)
2447 data
= rcu_dereference(event
->data
);
2451 if (vmf
->pgoff
&& (vmf
->flags
& FAULT_FLAG_WRITE
))
2454 vmf
->page
= perf_mmap_to_page(data
, vmf
->pgoff
);
2458 get_page(vmf
->page
);
2459 vmf
->page
->mapping
= vma
->vm_file
->f_mapping
;
2460 vmf
->page
->index
= vmf
->pgoff
;
2470 perf_mmap_data_init(struct perf_event
*event
, struct perf_mmap_data
*data
)
2472 long max_size
= perf_data_size(data
);
2474 atomic_set(&data
->lock
, -1);
2476 if (event
->attr
.watermark
) {
2477 data
->watermark
= min_t(long, max_size
,
2478 event
->attr
.wakeup_watermark
);
2481 if (!data
->watermark
)
2482 data
->watermark
= max_size
/ 2;
2485 rcu_assign_pointer(event
->data
, data
);
2488 static void perf_mmap_data_free_rcu(struct rcu_head
*rcu_head
)
2490 struct perf_mmap_data
*data
;
2492 data
= container_of(rcu_head
, struct perf_mmap_data
, rcu_head
);
2493 perf_mmap_data_free(data
);
2496 static void perf_mmap_data_release(struct perf_event
*event
)
2498 struct perf_mmap_data
*data
= event
->data
;
2500 WARN_ON(atomic_read(&event
->mmap_count
));
2502 rcu_assign_pointer(event
->data
, NULL
);
2503 call_rcu(&data
->rcu_head
, perf_mmap_data_free_rcu
);
2506 static void perf_mmap_open(struct vm_area_struct
*vma
)
2508 struct perf_event
*event
= vma
->vm_file
->private_data
;
2510 atomic_inc(&event
->mmap_count
);
2513 static void perf_mmap_close(struct vm_area_struct
*vma
)
2515 struct perf_event
*event
= vma
->vm_file
->private_data
;
2517 WARN_ON_ONCE(event
->ctx
->parent_ctx
);
2518 if (atomic_dec_and_mutex_lock(&event
->mmap_count
, &event
->mmap_mutex
)) {
2519 unsigned long size
= perf_data_size(event
->data
);
2520 struct user_struct
*user
= current_user();
2522 atomic_long_sub((size
>> PAGE_SHIFT
) + 1, &user
->locked_vm
);
2523 vma
->vm_mm
->locked_vm
-= event
->data
->nr_locked
;
2524 perf_mmap_data_release(event
);
2525 mutex_unlock(&event
->mmap_mutex
);
2529 static const struct vm_operations_struct perf_mmap_vmops
= {
2530 .open
= perf_mmap_open
,
2531 .close
= perf_mmap_close
,
2532 .fault
= perf_mmap_fault
,
2533 .page_mkwrite
= perf_mmap_fault
,
2536 static int perf_mmap(struct file
*file
, struct vm_area_struct
*vma
)
2538 struct perf_event
*event
= file
->private_data
;
2539 unsigned long user_locked
, user_lock_limit
;
2540 struct user_struct
*user
= current_user();
2541 unsigned long locked
, lock_limit
;
2542 struct perf_mmap_data
*data
;
2543 unsigned long vma_size
;
2544 unsigned long nr_pages
;
2545 long user_extra
, extra
;
2548 if (!(vma
->vm_flags
& VM_SHARED
))
2551 vma_size
= vma
->vm_end
- vma
->vm_start
;
2552 nr_pages
= (vma_size
/ PAGE_SIZE
) - 1;
2555 * If we have data pages ensure they're a power-of-two number, so we
2556 * can do bitmasks instead of modulo.
2558 if (nr_pages
!= 0 && !is_power_of_2(nr_pages
))
2561 if (vma_size
!= PAGE_SIZE
* (1 + nr_pages
))
2564 if (vma
->vm_pgoff
!= 0)
2567 WARN_ON_ONCE(event
->ctx
->parent_ctx
);
2568 mutex_lock(&event
->mmap_mutex
);
2569 if (event
->output
) {
2574 if (atomic_inc_not_zero(&event
->mmap_count
)) {
2575 if (nr_pages
!= event
->data
->nr_pages
)
2580 user_extra
= nr_pages
+ 1;
2581 user_lock_limit
= sysctl_perf_event_mlock
>> (PAGE_SHIFT
- 10);
2584 * Increase the limit linearly with more CPUs:
2586 user_lock_limit
*= num_online_cpus();
2588 user_locked
= atomic_long_read(&user
->locked_vm
) + user_extra
;
2591 if (user_locked
> user_lock_limit
)
2592 extra
= user_locked
- user_lock_limit
;
2594 lock_limit
= rlimit(RLIMIT_MEMLOCK
);
2595 lock_limit
>>= PAGE_SHIFT
;
2596 locked
= vma
->vm_mm
->locked_vm
+ extra
;
2598 if ((locked
> lock_limit
) && perf_paranoid_tracepoint_raw() &&
2599 !capable(CAP_IPC_LOCK
)) {
2604 WARN_ON(event
->data
);
2606 data
= perf_mmap_data_alloc(event
, nr_pages
);
2612 perf_mmap_data_init(event
, data
);
2614 atomic_set(&event
->mmap_count
, 1);
2615 atomic_long_add(user_extra
, &user
->locked_vm
);
2616 vma
->vm_mm
->locked_vm
+= extra
;
2617 event
->data
->nr_locked
= extra
;
2618 if (vma
->vm_flags
& VM_WRITE
)
2619 event
->data
->writable
= 1;
2622 mutex_unlock(&event
->mmap_mutex
);
2624 vma
->vm_flags
|= VM_RESERVED
;
2625 vma
->vm_ops
= &perf_mmap_vmops
;
2630 static int perf_fasync(int fd
, struct file
*filp
, int on
)
2632 struct inode
*inode
= filp
->f_path
.dentry
->d_inode
;
2633 struct perf_event
*event
= filp
->private_data
;
2636 mutex_lock(&inode
->i_mutex
);
2637 retval
= fasync_helper(fd
, filp
, on
, &event
->fasync
);
2638 mutex_unlock(&inode
->i_mutex
);
2646 static const struct file_operations perf_fops
= {
2647 .release
= perf_release
,
2650 .unlocked_ioctl
= perf_ioctl
,
2651 .compat_ioctl
= perf_ioctl
,
2653 .fasync
= perf_fasync
,
2659 * If there's data, ensure we set the poll() state and publish everything
2660 * to user-space before waking everybody up.
2663 void perf_event_wakeup(struct perf_event
*event
)
2665 wake_up_all(&event
->waitq
);
2667 if (event
->pending_kill
) {
2668 kill_fasync(&event
->fasync
, SIGIO
, event
->pending_kill
);
2669 event
->pending_kill
= 0;
2676 * Handle the case where we need to wakeup up from NMI (or rq->lock) context.
2678 * The NMI bit means we cannot possibly take locks. Therefore, maintain a
2679 * single linked list and use cmpxchg() to add entries lockless.
2682 static void perf_pending_event(struct perf_pending_entry
*entry
)
2684 struct perf_event
*event
= container_of(entry
,
2685 struct perf_event
, pending
);
2687 if (event
->pending_disable
) {
2688 event
->pending_disable
= 0;
2689 __perf_event_disable(event
);
2692 if (event
->pending_wakeup
) {
2693 event
->pending_wakeup
= 0;
2694 perf_event_wakeup(event
);
2698 #define PENDING_TAIL ((struct perf_pending_entry *)-1UL)
2700 static DEFINE_PER_CPU(struct perf_pending_entry
*, perf_pending_head
) = {
2704 static void perf_pending_queue(struct perf_pending_entry
*entry
,
2705 void (*func
)(struct perf_pending_entry
*))
2707 struct perf_pending_entry
**head
;
2709 if (cmpxchg(&entry
->next
, NULL
, PENDING_TAIL
) != NULL
)
2714 head
= &get_cpu_var(perf_pending_head
);
2717 entry
->next
= *head
;
2718 } while (cmpxchg(head
, entry
->next
, entry
) != entry
->next
);
2720 set_perf_event_pending();
2722 put_cpu_var(perf_pending_head
);
2725 static int __perf_pending_run(void)
2727 struct perf_pending_entry
*list
;
2730 list
= xchg(&__get_cpu_var(perf_pending_head
), PENDING_TAIL
);
2731 while (list
!= PENDING_TAIL
) {
2732 void (*func
)(struct perf_pending_entry
*);
2733 struct perf_pending_entry
*entry
= list
;
2740 * Ensure we observe the unqueue before we issue the wakeup,
2741 * so that we won't be waiting forever.
2742 * -- see perf_not_pending().
2753 static inline int perf_not_pending(struct perf_event
*event
)
2756 * If we flush on whatever cpu we run, there is a chance we don't
2760 __perf_pending_run();
2764 * Ensure we see the proper queue state before going to sleep
2765 * so that we do not miss the wakeup. -- see perf_pending_handle()
2768 return event
->pending
.next
== NULL
;
2771 static void perf_pending_sync(struct perf_event
*event
)
2773 wait_event(event
->waitq
, perf_not_pending(event
));
2776 void perf_event_do_pending(void)
2778 __perf_pending_run();
2782 * Callchain support -- arch specific
2785 __weak
struct perf_callchain_entry
*perf_callchain(struct pt_regs
*regs
)
2793 static bool perf_output_space(struct perf_mmap_data
*data
, unsigned long tail
,
2794 unsigned long offset
, unsigned long head
)
2798 if (!data
->writable
)
2801 mask
= perf_data_size(data
) - 1;
2803 offset
= (offset
- tail
) & mask
;
2804 head
= (head
- tail
) & mask
;
2806 if ((int)(head
- offset
) < 0)
2812 static void perf_output_wakeup(struct perf_output_handle
*handle
)
2814 atomic_set(&handle
->data
->poll
, POLL_IN
);
2817 handle
->event
->pending_wakeup
= 1;
2818 perf_pending_queue(&handle
->event
->pending
,
2819 perf_pending_event
);
2821 perf_event_wakeup(handle
->event
);
2825 * Curious locking construct.
2827 * We need to ensure a later event_id doesn't publish a head when a former
2828 * event_id isn't done writing. However since we need to deal with NMIs we
2829 * cannot fully serialize things.
2831 * What we do is serialize between CPUs so we only have to deal with NMI
2832 * nesting on a single CPU.
2834 * We only publish the head (and generate a wakeup) when the outer-most
2835 * event_id completes.
2837 static void perf_output_lock(struct perf_output_handle
*handle
)
2839 struct perf_mmap_data
*data
= handle
->data
;
2840 int cur
, cpu
= get_cpu();
2845 cur
= atomic_cmpxchg(&data
->lock
, -1, cpu
);
2857 static void perf_output_unlock(struct perf_output_handle
*handle
)
2859 struct perf_mmap_data
*data
= handle
->data
;
2863 data
->done_head
= data
->head
;
2865 if (!handle
->locked
)
2870 * The xchg implies a full barrier that ensures all writes are done
2871 * before we publish the new head, matched by a rmb() in userspace when
2872 * reading this position.
2874 while ((head
= atomic_long_xchg(&data
->done_head
, 0)))
2875 data
->user_page
->data_head
= head
;
2878 * NMI can happen here, which means we can miss a done_head update.
2881 cpu
= atomic_xchg(&data
->lock
, -1);
2882 WARN_ON_ONCE(cpu
!= smp_processor_id());
2885 * Therefore we have to validate we did not indeed do so.
2887 if (unlikely(atomic_long_read(&data
->done_head
))) {
2889 * Since we had it locked, we can lock it again.
2891 while (atomic_cmpxchg(&data
->lock
, -1, cpu
) != -1)
2897 if (atomic_xchg(&data
->wakeup
, 0))
2898 perf_output_wakeup(handle
);
2903 void perf_output_copy(struct perf_output_handle
*handle
,
2904 const void *buf
, unsigned int len
)
2906 unsigned int pages_mask
;
2907 unsigned long offset
;
2911 offset
= handle
->offset
;
2912 pages_mask
= handle
->data
->nr_pages
- 1;
2913 pages
= handle
->data
->data_pages
;
2916 unsigned long page_offset
;
2917 unsigned long page_size
;
2920 nr
= (offset
>> PAGE_SHIFT
) & pages_mask
;
2921 page_size
= 1UL << (handle
->data
->data_order
+ PAGE_SHIFT
);
2922 page_offset
= offset
& (page_size
- 1);
2923 size
= min_t(unsigned int, page_size
- page_offset
, len
);
2925 memcpy(pages
[nr
] + page_offset
, buf
, size
);
2932 handle
->offset
= offset
;
2935 * Check we didn't copy past our reservation window, taking the
2936 * possible unsigned int wrap into account.
2938 WARN_ON_ONCE(((long)(handle
->head
- handle
->offset
)) < 0);
2941 int perf_output_begin(struct perf_output_handle
*handle
,
2942 struct perf_event
*event
, unsigned int size
,
2943 int nmi
, int sample
)
2945 struct perf_event
*output_event
;
2946 struct perf_mmap_data
*data
;
2947 unsigned long tail
, offset
, head
;
2950 struct perf_event_header header
;
2957 * For inherited events we send all the output towards the parent.
2960 event
= event
->parent
;
2962 output_event
= rcu_dereference(event
->output
);
2964 event
= output_event
;
2966 data
= rcu_dereference(event
->data
);
2970 handle
->data
= data
;
2971 handle
->event
= event
;
2973 handle
->sample
= sample
;
2975 if (!data
->nr_pages
)
2978 have_lost
= atomic_read(&data
->lost
);
2980 size
+= sizeof(lost_event
);
2982 perf_output_lock(handle
);
2986 * Userspace could choose to issue a mb() before updating the
2987 * tail pointer. So that all reads will be completed before the
2990 tail
= ACCESS_ONCE(data
->user_page
->data_tail
);
2992 offset
= head
= atomic_long_read(&data
->head
);
2994 if (unlikely(!perf_output_space(data
, tail
, offset
, head
)))
2996 } while (atomic_long_cmpxchg(&data
->head
, offset
, head
) != offset
);
2998 handle
->offset
= offset
;
2999 handle
->head
= head
;
3001 if (head
- tail
> data
->watermark
)
3002 atomic_set(&data
->wakeup
, 1);
3005 lost_event
.header
.type
= PERF_RECORD_LOST
;
3006 lost_event
.header
.misc
= 0;
3007 lost_event
.header
.size
= sizeof(lost_event
);
3008 lost_event
.id
= event
->id
;
3009 lost_event
.lost
= atomic_xchg(&data
->lost
, 0);
3011 perf_output_put(handle
, lost_event
);
3017 atomic_inc(&data
->lost
);
3018 perf_output_unlock(handle
);
3025 void perf_output_end(struct perf_output_handle
*handle
)
3027 struct perf_event
*event
= handle
->event
;
3028 struct perf_mmap_data
*data
= handle
->data
;
3030 int wakeup_events
= event
->attr
.wakeup_events
;
3032 if (handle
->sample
&& wakeup_events
) {
3033 int events
= atomic_inc_return(&data
->events
);
3034 if (events
>= wakeup_events
) {
3035 atomic_sub(wakeup_events
, &data
->events
);
3036 atomic_set(&data
->wakeup
, 1);
3040 perf_output_unlock(handle
);
3044 static u32
perf_event_pid(struct perf_event
*event
, struct task_struct
*p
)
3047 * only top level events have the pid namespace they were created in
3050 event
= event
->parent
;
3052 return task_tgid_nr_ns(p
, event
->ns
);
3055 static u32
perf_event_tid(struct perf_event
*event
, struct task_struct
*p
)
3058 * only top level events have the pid namespace they were created in
3061 event
= event
->parent
;
3063 return task_pid_nr_ns(p
, event
->ns
);
3066 static void perf_output_read_one(struct perf_output_handle
*handle
,
3067 struct perf_event
*event
)
3069 u64 read_format
= event
->attr
.read_format
;
3073 values
[n
++] = atomic64_read(&event
->count
);
3074 if (read_format
& PERF_FORMAT_TOTAL_TIME_ENABLED
) {
3075 values
[n
++] = event
->total_time_enabled
+
3076 atomic64_read(&event
->child_total_time_enabled
);
3078 if (read_format
& PERF_FORMAT_TOTAL_TIME_RUNNING
) {
3079 values
[n
++] = event
->total_time_running
+
3080 atomic64_read(&event
->child_total_time_running
);
3082 if (read_format
& PERF_FORMAT_ID
)
3083 values
[n
++] = primary_event_id(event
);
3085 perf_output_copy(handle
, values
, n
* sizeof(u64
));
3089 * XXX PERF_FORMAT_GROUP vs inherited events seems difficult.
3091 static void perf_output_read_group(struct perf_output_handle
*handle
,
3092 struct perf_event
*event
)
3094 struct perf_event
*leader
= event
->group_leader
, *sub
;
3095 u64 read_format
= event
->attr
.read_format
;
3099 values
[n
++] = 1 + leader
->nr_siblings
;
3101 if (read_format
& PERF_FORMAT_TOTAL_TIME_ENABLED
)
3102 values
[n
++] = leader
->total_time_enabled
;
3104 if (read_format
& PERF_FORMAT_TOTAL_TIME_RUNNING
)
3105 values
[n
++] = leader
->total_time_running
;
3107 if (leader
!= event
)
3108 leader
->pmu
->read(leader
);
3110 values
[n
++] = atomic64_read(&leader
->count
);
3111 if (read_format
& PERF_FORMAT_ID
)
3112 values
[n
++] = primary_event_id(leader
);
3114 perf_output_copy(handle
, values
, n
* sizeof(u64
));
3116 list_for_each_entry(sub
, &leader
->sibling_list
, group_entry
) {
3120 sub
->pmu
->read(sub
);
3122 values
[n
++] = atomic64_read(&sub
->count
);
3123 if (read_format
& PERF_FORMAT_ID
)
3124 values
[n
++] = primary_event_id(sub
);
3126 perf_output_copy(handle
, values
, n
* sizeof(u64
));
3130 static void perf_output_read(struct perf_output_handle
*handle
,
3131 struct perf_event
*event
)
3133 if (event
->attr
.read_format
& PERF_FORMAT_GROUP
)
3134 perf_output_read_group(handle
, event
);
3136 perf_output_read_one(handle
, event
);
3139 void perf_output_sample(struct perf_output_handle
*handle
,
3140 struct perf_event_header
*header
,
3141 struct perf_sample_data
*data
,
3142 struct perf_event
*event
)
3144 u64 sample_type
= data
->type
;
3146 perf_output_put(handle
, *header
);
3148 if (sample_type
& PERF_SAMPLE_IP
)
3149 perf_output_put(handle
, data
->ip
);
3151 if (sample_type
& PERF_SAMPLE_TID
)
3152 perf_output_put(handle
, data
->tid_entry
);
3154 if (sample_type
& PERF_SAMPLE_TIME
)
3155 perf_output_put(handle
, data
->time
);
3157 if (sample_type
& PERF_SAMPLE_ADDR
)
3158 perf_output_put(handle
, data
->addr
);
3160 if (sample_type
& PERF_SAMPLE_ID
)
3161 perf_output_put(handle
, data
->id
);
3163 if (sample_type
& PERF_SAMPLE_STREAM_ID
)
3164 perf_output_put(handle
, data
->stream_id
);
3166 if (sample_type
& PERF_SAMPLE_CPU
)
3167 perf_output_put(handle
, data
->cpu_entry
);
3169 if (sample_type
& PERF_SAMPLE_PERIOD
)
3170 perf_output_put(handle
, data
->period
);
3172 if (sample_type
& PERF_SAMPLE_READ
)
3173 perf_output_read(handle
, event
);
3175 if (sample_type
& PERF_SAMPLE_CALLCHAIN
) {
3176 if (data
->callchain
) {
3179 if (data
->callchain
)
3180 size
+= data
->callchain
->nr
;
3182 size
*= sizeof(u64
);
3184 perf_output_copy(handle
, data
->callchain
, size
);
3187 perf_output_put(handle
, nr
);
3191 if (sample_type
& PERF_SAMPLE_RAW
) {
3193 perf_output_put(handle
, data
->raw
->size
);
3194 perf_output_copy(handle
, data
->raw
->data
,
3201 .size
= sizeof(u32
),
3204 perf_output_put(handle
, raw
);
3209 void perf_prepare_sample(struct perf_event_header
*header
,
3210 struct perf_sample_data
*data
,
3211 struct perf_event
*event
,
3212 struct pt_regs
*regs
)
3214 u64 sample_type
= event
->attr
.sample_type
;
3216 data
->type
= sample_type
;
3218 header
->type
= PERF_RECORD_SAMPLE
;
3219 header
->size
= sizeof(*header
);
3222 header
->misc
|= perf_misc_flags(regs
);
3224 if (sample_type
& PERF_SAMPLE_IP
) {
3225 data
->ip
= perf_instruction_pointer(regs
);
3227 header
->size
+= sizeof(data
->ip
);
3230 if (sample_type
& PERF_SAMPLE_TID
) {
3231 /* namespace issues */
3232 data
->tid_entry
.pid
= perf_event_pid(event
, current
);
3233 data
->tid_entry
.tid
= perf_event_tid(event
, current
);
3235 header
->size
+= sizeof(data
->tid_entry
);
3238 if (sample_type
& PERF_SAMPLE_TIME
) {
3239 data
->time
= perf_clock();
3241 header
->size
+= sizeof(data
->time
);
3244 if (sample_type
& PERF_SAMPLE_ADDR
)
3245 header
->size
+= sizeof(data
->addr
);
3247 if (sample_type
& PERF_SAMPLE_ID
) {
3248 data
->id
= primary_event_id(event
);
3250 header
->size
+= sizeof(data
->id
);
3253 if (sample_type
& PERF_SAMPLE_STREAM_ID
) {
3254 data
->stream_id
= event
->id
;
3256 header
->size
+= sizeof(data
->stream_id
);
3259 if (sample_type
& PERF_SAMPLE_CPU
) {
3260 data
->cpu_entry
.cpu
= raw_smp_processor_id();
3261 data
->cpu_entry
.reserved
= 0;
3263 header
->size
+= sizeof(data
->cpu_entry
);
3266 if (sample_type
& PERF_SAMPLE_PERIOD
)
3267 header
->size
+= sizeof(data
->period
);
3269 if (sample_type
& PERF_SAMPLE_READ
)
3270 header
->size
+= perf_event_read_size(event
);
3272 if (sample_type
& PERF_SAMPLE_CALLCHAIN
) {
3275 data
->callchain
= perf_callchain(regs
);
3277 if (data
->callchain
)
3278 size
+= data
->callchain
->nr
;
3280 header
->size
+= size
* sizeof(u64
);
3283 if (sample_type
& PERF_SAMPLE_RAW
) {
3284 int size
= sizeof(u32
);
3287 size
+= data
->raw
->size
;
3289 size
+= sizeof(u32
);
3291 WARN_ON_ONCE(size
& (sizeof(u64
)-1));
3292 header
->size
+= size
;
3296 static void perf_event_output(struct perf_event
*event
, int nmi
,
3297 struct perf_sample_data
*data
,
3298 struct pt_regs
*regs
)
3300 struct perf_output_handle handle
;
3301 struct perf_event_header header
;
3303 perf_prepare_sample(&header
, data
, event
, regs
);
3305 if (perf_output_begin(&handle
, event
, header
.size
, nmi
, 1))
3308 perf_output_sample(&handle
, &header
, data
, event
);
3310 perf_output_end(&handle
);
3317 struct perf_read_event
{
3318 struct perf_event_header header
;
3325 perf_event_read_event(struct perf_event
*event
,
3326 struct task_struct
*task
)
3328 struct perf_output_handle handle
;
3329 struct perf_read_event read_event
= {
3331 .type
= PERF_RECORD_READ
,
3333 .size
= sizeof(read_event
) + perf_event_read_size(event
),
3335 .pid
= perf_event_pid(event
, task
),
3336 .tid
= perf_event_tid(event
, task
),
3340 ret
= perf_output_begin(&handle
, event
, read_event
.header
.size
, 0, 0);
3344 perf_output_put(&handle
, read_event
);
3345 perf_output_read(&handle
, event
);
3347 perf_output_end(&handle
);
3351 * task tracking -- fork/exit
3353 * enabled by: attr.comm | attr.mmap | attr.task
3356 struct perf_task_event
{
3357 struct task_struct
*task
;
3358 struct perf_event_context
*task_ctx
;
3361 struct perf_event_header header
;
3371 static void perf_event_task_output(struct perf_event
*event
,
3372 struct perf_task_event
*task_event
)
3374 struct perf_output_handle handle
;
3376 struct task_struct
*task
= task_event
->task
;
3379 size
= task_event
->event_id
.header
.size
;
3380 ret
= perf_output_begin(&handle
, event
, size
, 0, 0);
3385 task_event
->event_id
.pid
= perf_event_pid(event
, task
);
3386 task_event
->event_id
.ppid
= perf_event_pid(event
, current
);
3388 task_event
->event_id
.tid
= perf_event_tid(event
, task
);
3389 task_event
->event_id
.ptid
= perf_event_tid(event
, current
);
3391 perf_output_put(&handle
, task_event
->event_id
);
3393 perf_output_end(&handle
);
3396 static int perf_event_task_match(struct perf_event
*event
)
3398 if (event
->state
< PERF_EVENT_STATE_INACTIVE
)
3401 if (event
->cpu
!= -1 && event
->cpu
!= smp_processor_id())
3404 if (event
->attr
.comm
|| event
->attr
.mmap
|| event
->attr
.task
)
3410 static void perf_event_task_ctx(struct perf_event_context
*ctx
,
3411 struct perf_task_event
*task_event
)
3413 struct perf_event
*event
;
3415 list_for_each_entry_rcu(event
, &ctx
->event_list
, event_entry
) {
3416 if (perf_event_task_match(event
))
3417 perf_event_task_output(event
, task_event
);
3421 static void perf_event_task_event(struct perf_task_event
*task_event
)
3423 struct perf_cpu_context
*cpuctx
;
3424 struct perf_event_context
*ctx
= task_event
->task_ctx
;
3427 cpuctx
= &get_cpu_var(perf_cpu_context
);
3428 perf_event_task_ctx(&cpuctx
->ctx
, task_event
);
3430 ctx
= rcu_dereference(current
->perf_event_ctxp
);
3432 perf_event_task_ctx(ctx
, task_event
);
3433 put_cpu_var(perf_cpu_context
);
3437 static void perf_event_task(struct task_struct
*task
,
3438 struct perf_event_context
*task_ctx
,
3441 struct perf_task_event task_event
;
3443 if (!atomic_read(&nr_comm_events
) &&
3444 !atomic_read(&nr_mmap_events
) &&
3445 !atomic_read(&nr_task_events
))
3448 task_event
= (struct perf_task_event
){
3450 .task_ctx
= task_ctx
,
3453 .type
= new ? PERF_RECORD_FORK
: PERF_RECORD_EXIT
,
3455 .size
= sizeof(task_event
.event_id
),
3461 .time
= perf_clock(),
3465 perf_event_task_event(&task_event
);
3468 void perf_event_fork(struct task_struct
*task
)
3470 perf_event_task(task
, NULL
, 1);
3477 struct perf_comm_event
{
3478 struct task_struct
*task
;
3483 struct perf_event_header header
;
3490 static void perf_event_comm_output(struct perf_event
*event
,
3491 struct perf_comm_event
*comm_event
)
3493 struct perf_output_handle handle
;
3494 int size
= comm_event
->event_id
.header
.size
;
3495 int ret
= perf_output_begin(&handle
, event
, size
, 0, 0);
3500 comm_event
->event_id
.pid
= perf_event_pid(event
, comm_event
->task
);
3501 comm_event
->event_id
.tid
= perf_event_tid(event
, comm_event
->task
);
3503 perf_output_put(&handle
, comm_event
->event_id
);
3504 perf_output_copy(&handle
, comm_event
->comm
,
3505 comm_event
->comm_size
);
3506 perf_output_end(&handle
);
3509 static int perf_event_comm_match(struct perf_event
*event
)
3511 if (event
->state
< PERF_EVENT_STATE_INACTIVE
)
3514 if (event
->cpu
!= -1 && event
->cpu
!= smp_processor_id())
3517 if (event
->attr
.comm
)
3523 static void perf_event_comm_ctx(struct perf_event_context
*ctx
,
3524 struct perf_comm_event
*comm_event
)
3526 struct perf_event
*event
;
3528 list_for_each_entry_rcu(event
, &ctx
->event_list
, event_entry
) {
3529 if (perf_event_comm_match(event
))
3530 perf_event_comm_output(event
, comm_event
);
3534 static void perf_event_comm_event(struct perf_comm_event
*comm_event
)
3536 struct perf_cpu_context
*cpuctx
;
3537 struct perf_event_context
*ctx
;
3539 char comm
[TASK_COMM_LEN
];
3541 memset(comm
, 0, sizeof(comm
));
3542 strlcpy(comm
, comm_event
->task
->comm
, sizeof(comm
));
3543 size
= ALIGN(strlen(comm
)+1, sizeof(u64
));
3545 comm_event
->comm
= comm
;
3546 comm_event
->comm_size
= size
;
3548 comm_event
->event_id
.header
.size
= sizeof(comm_event
->event_id
) + size
;
3551 cpuctx
= &get_cpu_var(perf_cpu_context
);
3552 perf_event_comm_ctx(&cpuctx
->ctx
, comm_event
);
3553 ctx
= rcu_dereference(current
->perf_event_ctxp
);
3555 perf_event_comm_ctx(ctx
, comm_event
);
3556 put_cpu_var(perf_cpu_context
);
3560 void perf_event_comm(struct task_struct
*task
)
3562 struct perf_comm_event comm_event
;
3564 if (task
->perf_event_ctxp
)
3565 perf_event_enable_on_exec(task
);
3567 if (!atomic_read(&nr_comm_events
))
3570 comm_event
= (struct perf_comm_event
){
3576 .type
= PERF_RECORD_COMM
,
3585 perf_event_comm_event(&comm_event
);
3592 struct perf_mmap_event
{
3593 struct vm_area_struct
*vma
;
3595 const char *file_name
;
3599 struct perf_event_header header
;
3609 static void perf_event_mmap_output(struct perf_event
*event
,
3610 struct perf_mmap_event
*mmap_event
)
3612 struct perf_output_handle handle
;
3613 int size
= mmap_event
->event_id
.header
.size
;
3614 int ret
= perf_output_begin(&handle
, event
, size
, 0, 0);
3619 mmap_event
->event_id
.pid
= perf_event_pid(event
, current
);
3620 mmap_event
->event_id
.tid
= perf_event_tid(event
, current
);
3622 perf_output_put(&handle
, mmap_event
->event_id
);
3623 perf_output_copy(&handle
, mmap_event
->file_name
,
3624 mmap_event
->file_size
);
3625 perf_output_end(&handle
);
3628 static int perf_event_mmap_match(struct perf_event
*event
,
3629 struct perf_mmap_event
*mmap_event
)
3631 if (event
->state
< PERF_EVENT_STATE_INACTIVE
)
3634 if (event
->cpu
!= -1 && event
->cpu
!= smp_processor_id())
3637 if (event
->attr
.mmap
)
3643 static void perf_event_mmap_ctx(struct perf_event_context
*ctx
,
3644 struct perf_mmap_event
*mmap_event
)
3646 struct perf_event
*event
;
3648 list_for_each_entry_rcu(event
, &ctx
->event_list
, event_entry
) {
3649 if (perf_event_mmap_match(event
, mmap_event
))
3650 perf_event_mmap_output(event
, mmap_event
);
3654 static void perf_event_mmap_event(struct perf_mmap_event
*mmap_event
)
3656 struct perf_cpu_context
*cpuctx
;
3657 struct perf_event_context
*ctx
;
3658 struct vm_area_struct
*vma
= mmap_event
->vma
;
3659 struct file
*file
= vma
->vm_file
;
3665 memset(tmp
, 0, sizeof(tmp
));
3669 * d_path works from the end of the buffer backwards, so we
3670 * need to add enough zero bytes after the string to handle
3671 * the 64bit alignment we do later.
3673 buf
= kzalloc(PATH_MAX
+ sizeof(u64
), GFP_KERNEL
);
3675 name
= strncpy(tmp
, "//enomem", sizeof(tmp
));
3678 name
= d_path(&file
->f_path
, buf
, PATH_MAX
);
3680 name
= strncpy(tmp
, "//toolong", sizeof(tmp
));
3684 if (arch_vma_name(mmap_event
->vma
)) {
3685 name
= strncpy(tmp
, arch_vma_name(mmap_event
->vma
),
3691 name
= strncpy(tmp
, "[vdso]", sizeof(tmp
));
3695 name
= strncpy(tmp
, "//anon", sizeof(tmp
));
3700 size
= ALIGN(strlen(name
)+1, sizeof(u64
));
3702 mmap_event
->file_name
= name
;
3703 mmap_event
->file_size
= size
;
3705 mmap_event
->event_id
.header
.size
= sizeof(mmap_event
->event_id
) + size
;
3708 cpuctx
= &get_cpu_var(perf_cpu_context
);
3709 perf_event_mmap_ctx(&cpuctx
->ctx
, mmap_event
);
3710 ctx
= rcu_dereference(current
->perf_event_ctxp
);
3712 perf_event_mmap_ctx(ctx
, mmap_event
);
3713 put_cpu_var(perf_cpu_context
);
3719 void __perf_event_mmap(struct vm_area_struct
*vma
)
3721 struct perf_mmap_event mmap_event
;
3723 if (!atomic_read(&nr_mmap_events
))
3726 mmap_event
= (struct perf_mmap_event
){
3732 .type
= PERF_RECORD_MMAP
,
3738 .start
= vma
->vm_start
,
3739 .len
= vma
->vm_end
- vma
->vm_start
,
3740 .pgoff
= (u64
)vma
->vm_pgoff
<< PAGE_SHIFT
,
3744 perf_event_mmap_event(&mmap_event
);
3748 * IRQ throttle logging
3751 static void perf_log_throttle(struct perf_event
*event
, int enable
)
3753 struct perf_output_handle handle
;
3757 struct perf_event_header header
;
3761 } throttle_event
= {
3763 .type
= PERF_RECORD_THROTTLE
,
3765 .size
= sizeof(throttle_event
),
3767 .time
= perf_clock(),
3768 .id
= primary_event_id(event
),
3769 .stream_id
= event
->id
,
3773 throttle_event
.header
.type
= PERF_RECORD_UNTHROTTLE
;
3775 ret
= perf_output_begin(&handle
, event
, sizeof(throttle_event
), 1, 0);
3779 perf_output_put(&handle
, throttle_event
);
3780 perf_output_end(&handle
);
3784 * Generic event overflow handling, sampling.
3787 static int __perf_event_overflow(struct perf_event
*event
, int nmi
,
3788 int throttle
, struct perf_sample_data
*data
,
3789 struct pt_regs
*regs
)
3791 int events
= atomic_read(&event
->event_limit
);
3792 struct hw_perf_event
*hwc
= &event
->hw
;
3795 throttle
= (throttle
&& event
->pmu
->unthrottle
!= NULL
);
3800 if (hwc
->interrupts
!= MAX_INTERRUPTS
) {
3802 if (HZ
* hwc
->interrupts
>
3803 (u64
)sysctl_perf_event_sample_rate
) {
3804 hwc
->interrupts
= MAX_INTERRUPTS
;
3805 perf_log_throttle(event
, 0);
3810 * Keep re-disabling events even though on the previous
3811 * pass we disabled it - just in case we raced with a
3812 * sched-in and the event got enabled again:
3818 if (event
->attr
.freq
) {
3819 u64 now
= perf_clock();
3820 s64 delta
= now
- hwc
->freq_time_stamp
;
3822 hwc
->freq_time_stamp
= now
;
3824 if (delta
> 0 && delta
< 2*TICK_NSEC
)
3825 perf_adjust_period(event
, delta
, hwc
->last_period
);
3829 * XXX event_limit might not quite work as expected on inherited
3833 event
->pending_kill
= POLL_IN
;
3834 if (events
&& atomic_dec_and_test(&event
->event_limit
)) {
3836 event
->pending_kill
= POLL_HUP
;
3838 event
->pending_disable
= 1;
3839 perf_pending_queue(&event
->pending
,
3840 perf_pending_event
);
3842 perf_event_disable(event
);
3845 if (event
->overflow_handler
)
3846 event
->overflow_handler(event
, nmi
, data
, regs
);
3848 perf_event_output(event
, nmi
, data
, regs
);
3853 int perf_event_overflow(struct perf_event
*event
, int nmi
,
3854 struct perf_sample_data
*data
,
3855 struct pt_regs
*regs
)
3857 return __perf_event_overflow(event
, nmi
, 1, data
, regs
);
3861 * Generic software event infrastructure
3865 * We directly increment event->count and keep a second value in
3866 * event->hw.period_left to count intervals. This period event
3867 * is kept in the range [-sample_period, 0] so that we can use the
3871 static u64
perf_swevent_set_period(struct perf_event
*event
)
3873 struct hw_perf_event
*hwc
= &event
->hw
;
3874 u64 period
= hwc
->last_period
;
3878 hwc
->last_period
= hwc
->sample_period
;
3881 old
= val
= atomic64_read(&hwc
->period_left
);
3885 nr
= div64_u64(period
+ val
, period
);
3886 offset
= nr
* period
;
3888 if (atomic64_cmpxchg(&hwc
->period_left
, old
, val
) != old
)
3894 static void perf_swevent_overflow(struct perf_event
*event
, u64 overflow
,
3895 int nmi
, struct perf_sample_data
*data
,
3896 struct pt_regs
*regs
)
3898 struct hw_perf_event
*hwc
= &event
->hw
;
3901 data
->period
= event
->hw
.last_period
;
3903 overflow
= perf_swevent_set_period(event
);
3905 if (hwc
->interrupts
== MAX_INTERRUPTS
)
3908 for (; overflow
; overflow
--) {
3909 if (__perf_event_overflow(event
, nmi
, throttle
,
3912 * We inhibit the overflow from happening when
3913 * hwc->interrupts == MAX_INTERRUPTS.
3921 static void perf_swevent_unthrottle(struct perf_event
*event
)
3924 * Nothing to do, we already reset hwc->interrupts.
3928 static void perf_swevent_add(struct perf_event
*event
, u64 nr
,
3929 int nmi
, struct perf_sample_data
*data
,
3930 struct pt_regs
*regs
)
3932 struct hw_perf_event
*hwc
= &event
->hw
;
3934 atomic64_add(nr
, &event
->count
);
3939 if (!hwc
->sample_period
)
3942 if (nr
== 1 && hwc
->sample_period
== 1 && !event
->attr
.freq
)
3943 return perf_swevent_overflow(event
, 1, nmi
, data
, regs
);
3945 if (atomic64_add_negative(nr
, &hwc
->period_left
))
3948 perf_swevent_overflow(event
, 0, nmi
, data
, regs
);
3951 static int perf_swevent_is_counting(struct perf_event
*event
)
3954 * The event is active, we're good!
3956 if (event
->state
== PERF_EVENT_STATE_ACTIVE
)
3960 * The event is off/error, not counting.
3962 if (event
->state
!= PERF_EVENT_STATE_INACTIVE
)
3966 * The event is inactive, if the context is active
3967 * we're part of a group that didn't make it on the 'pmu',
3970 if (event
->ctx
->is_active
)
3974 * We're inactive and the context is too, this means the
3975 * task is scheduled out, we're counting events that happen
3976 * to us, like migration events.
3981 static int perf_tp_event_match(struct perf_event
*event
,
3982 struct perf_sample_data
*data
);
3984 static int perf_exclude_event(struct perf_event
*event
,
3985 struct pt_regs
*regs
)
3988 if (event
->attr
.exclude_user
&& user_mode(regs
))
3991 if (event
->attr
.exclude_kernel
&& !user_mode(regs
))
3998 static int perf_swevent_match(struct perf_event
*event
,
3999 enum perf_type_id type
,
4001 struct perf_sample_data
*data
,
4002 struct pt_regs
*regs
)
4004 if (event
->cpu
!= -1 && event
->cpu
!= smp_processor_id())
4007 if (!perf_swevent_is_counting(event
))
4010 if (event
->attr
.type
!= type
)
4013 if (event
->attr
.config
!= event_id
)
4016 if (perf_exclude_event(event
, regs
))
4019 if (event
->attr
.type
== PERF_TYPE_TRACEPOINT
&&
4020 !perf_tp_event_match(event
, data
))
4026 static void perf_swevent_ctx_event(struct perf_event_context
*ctx
,
4027 enum perf_type_id type
,
4028 u32 event_id
, u64 nr
, int nmi
,
4029 struct perf_sample_data
*data
,
4030 struct pt_regs
*regs
)
4032 struct perf_event
*event
;
4034 list_for_each_entry_rcu(event
, &ctx
->event_list
, event_entry
) {
4035 if (perf_swevent_match(event
, type
, event_id
, data
, regs
))
4036 perf_swevent_add(event
, nr
, nmi
, data
, regs
);
4040 int perf_swevent_get_recursion_context(void)
4042 struct perf_cpu_context
*cpuctx
= &get_cpu_var(perf_cpu_context
);
4049 else if (in_softirq())
4054 if (cpuctx
->recursion
[rctx
]) {
4055 put_cpu_var(perf_cpu_context
);
4059 cpuctx
->recursion
[rctx
]++;
4064 EXPORT_SYMBOL_GPL(perf_swevent_get_recursion_context
);
4066 void perf_swevent_put_recursion_context(int rctx
)
4068 struct perf_cpu_context
*cpuctx
= &__get_cpu_var(perf_cpu_context
);
4070 cpuctx
->recursion
[rctx
]--;
4071 put_cpu_var(perf_cpu_context
);
4073 EXPORT_SYMBOL_GPL(perf_swevent_put_recursion_context
);
4075 static void do_perf_sw_event(enum perf_type_id type
, u32 event_id
,
4077 struct perf_sample_data
*data
,
4078 struct pt_regs
*regs
)
4080 struct perf_cpu_context
*cpuctx
;
4081 struct perf_event_context
*ctx
;
4083 cpuctx
= &__get_cpu_var(perf_cpu_context
);
4085 perf_swevent_ctx_event(&cpuctx
->ctx
, type
, event_id
,
4086 nr
, nmi
, data
, regs
);
4088 * doesn't really matter which of the child contexts the
4089 * events ends up in.
4091 ctx
= rcu_dereference(current
->perf_event_ctxp
);
4093 perf_swevent_ctx_event(ctx
, type
, event_id
, nr
, nmi
, data
, regs
);
4097 void __perf_sw_event(u32 event_id
, u64 nr
, int nmi
,
4098 struct pt_regs
*regs
, u64 addr
)
4100 struct perf_sample_data data
;
4103 rctx
= perf_swevent_get_recursion_context();
4107 perf_sample_data_init(&data
, addr
);
4109 do_perf_sw_event(PERF_TYPE_SOFTWARE
, event_id
, nr
, nmi
, &data
, regs
);
4111 perf_swevent_put_recursion_context(rctx
);
4114 static void perf_swevent_read(struct perf_event
*event
)
4118 static int perf_swevent_enable(struct perf_event
*event
)
4120 struct hw_perf_event
*hwc
= &event
->hw
;
4122 if (hwc
->sample_period
) {
4123 hwc
->last_period
= hwc
->sample_period
;
4124 perf_swevent_set_period(event
);
4129 static void perf_swevent_disable(struct perf_event
*event
)
4133 static const struct pmu perf_ops_generic
= {
4134 .enable
= perf_swevent_enable
,
4135 .disable
= perf_swevent_disable
,
4136 .read
= perf_swevent_read
,
4137 .unthrottle
= perf_swevent_unthrottle
,
4141 * hrtimer based swevent callback
4144 static enum hrtimer_restart
perf_swevent_hrtimer(struct hrtimer
*hrtimer
)
4146 enum hrtimer_restart ret
= HRTIMER_RESTART
;
4147 struct perf_sample_data data
;
4148 struct pt_regs
*regs
;
4149 struct perf_event
*event
;
4152 event
= container_of(hrtimer
, struct perf_event
, hw
.hrtimer
);
4153 event
->pmu
->read(event
);
4155 perf_sample_data_init(&data
, 0);
4156 data
.period
= event
->hw
.last_period
;
4157 regs
= get_irq_regs();
4159 * In case we exclude kernel IPs or are somehow not in interrupt
4160 * context, provide the next best thing, the user IP.
4162 if ((event
->attr
.exclude_kernel
|| !regs
) &&
4163 !event
->attr
.exclude_user
)
4164 regs
= task_pt_regs(current
);
4167 if (!(event
->attr
.exclude_idle
&& current
->pid
== 0))
4168 if (perf_event_overflow(event
, 0, &data
, regs
))
4169 ret
= HRTIMER_NORESTART
;
4172 period
= max_t(u64
, 10000, event
->hw
.sample_period
);
4173 hrtimer_forward_now(hrtimer
, ns_to_ktime(period
));
4178 static void perf_swevent_start_hrtimer(struct perf_event
*event
)
4180 struct hw_perf_event
*hwc
= &event
->hw
;
4182 hrtimer_init(&hwc
->hrtimer
, CLOCK_MONOTONIC
, HRTIMER_MODE_REL
);
4183 hwc
->hrtimer
.function
= perf_swevent_hrtimer
;
4184 if (hwc
->sample_period
) {
4187 if (hwc
->remaining
) {
4188 if (hwc
->remaining
< 0)
4191 period
= hwc
->remaining
;
4194 period
= max_t(u64
, 10000, hwc
->sample_period
);
4196 __hrtimer_start_range_ns(&hwc
->hrtimer
,
4197 ns_to_ktime(period
), 0,
4198 HRTIMER_MODE_REL
, 0);
4202 static void perf_swevent_cancel_hrtimer(struct perf_event
*event
)
4204 struct hw_perf_event
*hwc
= &event
->hw
;
4206 if (hwc
->sample_period
) {
4207 ktime_t remaining
= hrtimer_get_remaining(&hwc
->hrtimer
);
4208 hwc
->remaining
= ktime_to_ns(remaining
);
4210 hrtimer_cancel(&hwc
->hrtimer
);
4215 * Software event: cpu wall time clock
4218 static void cpu_clock_perf_event_update(struct perf_event
*event
)
4220 int cpu
= raw_smp_processor_id();
4224 now
= cpu_clock(cpu
);
4225 prev
= atomic64_xchg(&event
->hw
.prev_count
, now
);
4226 atomic64_add(now
- prev
, &event
->count
);
4229 static int cpu_clock_perf_event_enable(struct perf_event
*event
)
4231 struct hw_perf_event
*hwc
= &event
->hw
;
4232 int cpu
= raw_smp_processor_id();
4234 atomic64_set(&hwc
->prev_count
, cpu_clock(cpu
));
4235 perf_swevent_start_hrtimer(event
);
4240 static void cpu_clock_perf_event_disable(struct perf_event
*event
)
4242 perf_swevent_cancel_hrtimer(event
);
4243 cpu_clock_perf_event_update(event
);
4246 static void cpu_clock_perf_event_read(struct perf_event
*event
)
4248 cpu_clock_perf_event_update(event
);
4251 static const struct pmu perf_ops_cpu_clock
= {
4252 .enable
= cpu_clock_perf_event_enable
,
4253 .disable
= cpu_clock_perf_event_disable
,
4254 .read
= cpu_clock_perf_event_read
,
4258 * Software event: task time clock
4261 static void task_clock_perf_event_update(struct perf_event
*event
, u64 now
)
4266 prev
= atomic64_xchg(&event
->hw
.prev_count
, now
);
4268 atomic64_add(delta
, &event
->count
);
4271 static int task_clock_perf_event_enable(struct perf_event
*event
)
4273 struct hw_perf_event
*hwc
= &event
->hw
;
4276 now
= event
->ctx
->time
;
4278 atomic64_set(&hwc
->prev_count
, now
);
4280 perf_swevent_start_hrtimer(event
);
4285 static void task_clock_perf_event_disable(struct perf_event
*event
)
4287 perf_swevent_cancel_hrtimer(event
);
4288 task_clock_perf_event_update(event
, event
->ctx
->time
);
4292 static void task_clock_perf_event_read(struct perf_event
*event
)
4297 update_context_time(event
->ctx
);
4298 time
= event
->ctx
->time
;
4300 u64 now
= perf_clock();
4301 u64 delta
= now
- event
->ctx
->timestamp
;
4302 time
= event
->ctx
->time
+ delta
;
4305 task_clock_perf_event_update(event
, time
);
4308 static const struct pmu perf_ops_task_clock
= {
4309 .enable
= task_clock_perf_event_enable
,
4310 .disable
= task_clock_perf_event_disable
,
4311 .read
= task_clock_perf_event_read
,
4314 #ifdef CONFIG_EVENT_TRACING
4316 void perf_tp_event(int event_id
, u64 addr
, u64 count
, void *record
,
4319 struct pt_regs
*regs
= get_irq_regs();
4320 struct perf_sample_data data
;
4321 struct perf_raw_record raw
= {
4326 perf_sample_data_init(&data
, addr
);
4330 regs
= task_pt_regs(current
);
4332 /* Trace events already protected against recursion */
4333 do_perf_sw_event(PERF_TYPE_TRACEPOINT
, event_id
, count
, 1,
4336 EXPORT_SYMBOL_GPL(perf_tp_event
);
4338 static int perf_tp_event_match(struct perf_event
*event
,
4339 struct perf_sample_data
*data
)
4341 void *record
= data
->raw
->data
;
4343 if (likely(!event
->filter
) || filter_match_preds(event
->filter
, record
))
4348 static void tp_perf_event_destroy(struct perf_event
*event
)
4350 ftrace_profile_disable(event
->attr
.config
);
4353 static const struct pmu
*tp_perf_event_init(struct perf_event
*event
)
4356 * Raw tracepoint data is a severe data leak, only allow root to
4359 if ((event
->attr
.sample_type
& PERF_SAMPLE_RAW
) &&
4360 perf_paranoid_tracepoint_raw() &&
4361 !capable(CAP_SYS_ADMIN
))
4362 return ERR_PTR(-EPERM
);
4364 if (ftrace_profile_enable(event
->attr
.config
))
4367 event
->destroy
= tp_perf_event_destroy
;
4369 return &perf_ops_generic
;
4372 static int perf_event_set_filter(struct perf_event
*event
, void __user
*arg
)
4377 if (event
->attr
.type
!= PERF_TYPE_TRACEPOINT
)
4380 filter_str
= strndup_user(arg
, PAGE_SIZE
);
4381 if (IS_ERR(filter_str
))
4382 return PTR_ERR(filter_str
);
4384 ret
= ftrace_profile_set_filter(event
, event
->attr
.config
, filter_str
);
4390 static void perf_event_free_filter(struct perf_event
*event
)
4392 ftrace_profile_free_filter(event
);
4397 static int perf_tp_event_match(struct perf_event
*event
,
4398 struct perf_sample_data
*data
)
4403 static const struct pmu
*tp_perf_event_init(struct perf_event
*event
)
4408 static int perf_event_set_filter(struct perf_event
*event
, void __user
*arg
)
4413 static void perf_event_free_filter(struct perf_event
*event
)
4417 #endif /* CONFIG_EVENT_TRACING */
4419 #ifdef CONFIG_HAVE_HW_BREAKPOINT
4420 static void bp_perf_event_destroy(struct perf_event
*event
)
4422 release_bp_slot(event
);
4425 static const struct pmu
*bp_perf_event_init(struct perf_event
*bp
)
4429 err
= register_perf_hw_breakpoint(bp
);
4431 return ERR_PTR(err
);
4433 bp
->destroy
= bp_perf_event_destroy
;
4435 return &perf_ops_bp
;
4438 void perf_bp_event(struct perf_event
*bp
, void *data
)
4440 struct perf_sample_data sample
;
4441 struct pt_regs
*regs
= data
;
4443 perf_sample_data_init(&sample
, bp
->attr
.bp_addr
);
4445 if (!perf_exclude_event(bp
, regs
))
4446 perf_swevent_add(bp
, 1, 1, &sample
, regs
);
4449 static const struct pmu
*bp_perf_event_init(struct perf_event
*bp
)
4454 void perf_bp_event(struct perf_event
*bp
, void *regs
)
4459 atomic_t perf_swevent_enabled
[PERF_COUNT_SW_MAX
];
4461 static void sw_perf_event_destroy(struct perf_event
*event
)
4463 u64 event_id
= event
->attr
.config
;
4465 WARN_ON(event
->parent
);
4467 atomic_dec(&perf_swevent_enabled
[event_id
]);
4470 static const struct pmu
*sw_perf_event_init(struct perf_event
*event
)
4472 const struct pmu
*pmu
= NULL
;
4473 u64 event_id
= event
->attr
.config
;
4476 * Software events (currently) can't in general distinguish
4477 * between user, kernel and hypervisor events.
4478 * However, context switches and cpu migrations are considered
4479 * to be kernel events, and page faults are never hypervisor
4483 case PERF_COUNT_SW_CPU_CLOCK
:
4484 pmu
= &perf_ops_cpu_clock
;
4487 case PERF_COUNT_SW_TASK_CLOCK
:
4489 * If the user instantiates this as a per-cpu event,
4490 * use the cpu_clock event instead.
4492 if (event
->ctx
->task
)
4493 pmu
= &perf_ops_task_clock
;
4495 pmu
= &perf_ops_cpu_clock
;
4498 case PERF_COUNT_SW_PAGE_FAULTS
:
4499 case PERF_COUNT_SW_PAGE_FAULTS_MIN
:
4500 case PERF_COUNT_SW_PAGE_FAULTS_MAJ
:
4501 case PERF_COUNT_SW_CONTEXT_SWITCHES
:
4502 case PERF_COUNT_SW_CPU_MIGRATIONS
:
4503 case PERF_COUNT_SW_ALIGNMENT_FAULTS
:
4504 case PERF_COUNT_SW_EMULATION_FAULTS
:
4505 if (!event
->parent
) {
4506 atomic_inc(&perf_swevent_enabled
[event_id
]);
4507 event
->destroy
= sw_perf_event_destroy
;
4509 pmu
= &perf_ops_generic
;
4517 * Allocate and initialize a event structure
4519 static struct perf_event
*
4520 perf_event_alloc(struct perf_event_attr
*attr
,
4522 struct perf_event_context
*ctx
,
4523 struct perf_event
*group_leader
,
4524 struct perf_event
*parent_event
,
4525 perf_overflow_handler_t overflow_handler
,
4528 const struct pmu
*pmu
;
4529 struct perf_event
*event
;
4530 struct hw_perf_event
*hwc
;
4533 event
= kzalloc(sizeof(*event
), gfpflags
);
4535 return ERR_PTR(-ENOMEM
);
4538 * Single events are their own group leaders, with an
4539 * empty sibling list:
4542 group_leader
= event
;
4544 mutex_init(&event
->child_mutex
);
4545 INIT_LIST_HEAD(&event
->child_list
);
4547 INIT_LIST_HEAD(&event
->group_entry
);
4548 INIT_LIST_HEAD(&event
->event_entry
);
4549 INIT_LIST_HEAD(&event
->sibling_list
);
4550 init_waitqueue_head(&event
->waitq
);
4552 mutex_init(&event
->mmap_mutex
);
4555 event
->attr
= *attr
;
4556 event
->group_leader
= group_leader
;
4561 event
->parent
= parent_event
;
4563 event
->ns
= get_pid_ns(current
->nsproxy
->pid_ns
);
4564 event
->id
= atomic64_inc_return(&perf_event_id
);
4566 event
->state
= PERF_EVENT_STATE_INACTIVE
;
4568 if (!overflow_handler
&& parent_event
)
4569 overflow_handler
= parent_event
->overflow_handler
;
4571 event
->overflow_handler
= overflow_handler
;
4574 event
->state
= PERF_EVENT_STATE_OFF
;
4579 hwc
->sample_period
= attr
->sample_period
;
4580 if (attr
->freq
&& attr
->sample_freq
)
4581 hwc
->sample_period
= 1;
4582 hwc
->last_period
= hwc
->sample_period
;
4584 atomic64_set(&hwc
->period_left
, hwc
->sample_period
);
4587 * we currently do not support PERF_FORMAT_GROUP on inherited events
4589 if (attr
->inherit
&& (attr
->read_format
& PERF_FORMAT_GROUP
))
4592 switch (attr
->type
) {
4594 case PERF_TYPE_HARDWARE
:
4595 case PERF_TYPE_HW_CACHE
:
4596 pmu
= hw_perf_event_init(event
);
4599 case PERF_TYPE_SOFTWARE
:
4600 pmu
= sw_perf_event_init(event
);
4603 case PERF_TYPE_TRACEPOINT
:
4604 pmu
= tp_perf_event_init(event
);
4607 case PERF_TYPE_BREAKPOINT
:
4608 pmu
= bp_perf_event_init(event
);
4619 else if (IS_ERR(pmu
))
4624 put_pid_ns(event
->ns
);
4626 return ERR_PTR(err
);
4631 if (!event
->parent
) {
4632 atomic_inc(&nr_events
);
4633 if (event
->attr
.mmap
)
4634 atomic_inc(&nr_mmap_events
);
4635 if (event
->attr
.comm
)
4636 atomic_inc(&nr_comm_events
);
4637 if (event
->attr
.task
)
4638 atomic_inc(&nr_task_events
);
4644 static int perf_copy_attr(struct perf_event_attr __user
*uattr
,
4645 struct perf_event_attr
*attr
)
4650 if (!access_ok(VERIFY_WRITE
, uattr
, PERF_ATTR_SIZE_VER0
))
4654 * zero the full structure, so that a short copy will be nice.
4656 memset(attr
, 0, sizeof(*attr
));
4658 ret
= get_user(size
, &uattr
->size
);
4662 if (size
> PAGE_SIZE
) /* silly large */
4665 if (!size
) /* abi compat */
4666 size
= PERF_ATTR_SIZE_VER0
;
4668 if (size
< PERF_ATTR_SIZE_VER0
)
4672 * If we're handed a bigger struct than we know of,
4673 * ensure all the unknown bits are 0 - i.e. new
4674 * user-space does not rely on any kernel feature
4675 * extensions we dont know about yet.
4677 if (size
> sizeof(*attr
)) {
4678 unsigned char __user
*addr
;
4679 unsigned char __user
*end
;
4682 addr
= (void __user
*)uattr
+ sizeof(*attr
);
4683 end
= (void __user
*)uattr
+ size
;
4685 for (; addr
< end
; addr
++) {
4686 ret
= get_user(val
, addr
);
4692 size
= sizeof(*attr
);
4695 ret
= copy_from_user(attr
, uattr
, size
);
4700 * If the type exists, the corresponding creation will verify
4703 if (attr
->type
>= PERF_TYPE_MAX
)
4706 if (attr
->__reserved_1
)
4709 if (attr
->sample_type
& ~(PERF_SAMPLE_MAX
-1))
4712 if (attr
->read_format
& ~(PERF_FORMAT_MAX
-1))
4719 put_user(sizeof(*attr
), &uattr
->size
);
4724 static int perf_event_set_output(struct perf_event
*event
, int output_fd
)
4726 struct perf_event
*output_event
= NULL
;
4727 struct file
*output_file
= NULL
;
4728 struct perf_event
*old_output
;
4729 int fput_needed
= 0;
4735 output_file
= fget_light(output_fd
, &fput_needed
);
4739 if (output_file
->f_op
!= &perf_fops
)
4742 output_event
= output_file
->private_data
;
4744 /* Don't chain output fds */
4745 if (output_event
->output
)
4748 /* Don't set an output fd when we already have an output channel */
4752 atomic_long_inc(&output_file
->f_count
);
4755 mutex_lock(&event
->mmap_mutex
);
4756 old_output
= event
->output
;
4757 rcu_assign_pointer(event
->output
, output_event
);
4758 mutex_unlock(&event
->mmap_mutex
);
4762 * we need to make sure no existing perf_output_*()
4763 * is still referencing this event.
4766 fput(old_output
->filp
);
4771 fput_light(output_file
, fput_needed
);
4776 * sys_perf_event_open - open a performance event, associate it to a task/cpu
4778 * @attr_uptr: event_id type attributes for monitoring/sampling
4781 * @group_fd: group leader event fd
4783 SYSCALL_DEFINE5(perf_event_open
,
4784 struct perf_event_attr __user
*, attr_uptr
,
4785 pid_t
, pid
, int, cpu
, int, group_fd
, unsigned long, flags
)
4787 struct perf_event
*event
, *group_leader
;
4788 struct perf_event_attr attr
;
4789 struct perf_event_context
*ctx
;
4790 struct file
*event_file
= NULL
;
4791 struct file
*group_file
= NULL
;
4792 int fput_needed
= 0;
4793 int fput_needed2
= 0;
4796 /* for future expandability... */
4797 if (flags
& ~(PERF_FLAG_FD_NO_GROUP
| PERF_FLAG_FD_OUTPUT
))
4800 err
= perf_copy_attr(attr_uptr
, &attr
);
4804 if (!attr
.exclude_kernel
) {
4805 if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN
))
4810 if (attr
.sample_freq
> sysctl_perf_event_sample_rate
)
4815 * Get the target context (task or percpu):
4817 ctx
= find_get_context(pid
, cpu
);
4819 return PTR_ERR(ctx
);
4822 * Look up the group leader (we will attach this event to it):
4824 group_leader
= NULL
;
4825 if (group_fd
!= -1 && !(flags
& PERF_FLAG_FD_NO_GROUP
)) {
4827 group_file
= fget_light(group_fd
, &fput_needed
);
4829 goto err_put_context
;
4830 if (group_file
->f_op
!= &perf_fops
)
4831 goto err_put_context
;
4833 group_leader
= group_file
->private_data
;
4835 * Do not allow a recursive hierarchy (this new sibling
4836 * becoming part of another group-sibling):
4838 if (group_leader
->group_leader
!= group_leader
)
4839 goto err_put_context
;
4841 * Do not allow to attach to a group in a different
4842 * task or CPU context:
4844 if (group_leader
->ctx
!= ctx
)
4845 goto err_put_context
;
4847 * Only a group leader can be exclusive or pinned
4849 if (attr
.exclusive
|| attr
.pinned
)
4850 goto err_put_context
;
4853 event
= perf_event_alloc(&attr
, cpu
, ctx
, group_leader
,
4854 NULL
, NULL
, GFP_KERNEL
);
4855 err
= PTR_ERR(event
);
4857 goto err_put_context
;
4859 err
= anon_inode_getfd("[perf_event]", &perf_fops
, event
, O_RDWR
);
4861 goto err_free_put_context
;
4863 event_file
= fget_light(err
, &fput_needed2
);
4865 goto err_free_put_context
;
4867 if (flags
& PERF_FLAG_FD_OUTPUT
) {
4868 err
= perf_event_set_output(event
, group_fd
);
4870 goto err_fput_free_put_context
;
4873 event
->filp
= event_file
;
4874 WARN_ON_ONCE(ctx
->parent_ctx
);
4875 mutex_lock(&ctx
->mutex
);
4876 perf_install_in_context(ctx
, event
, cpu
);
4878 mutex_unlock(&ctx
->mutex
);
4880 event
->owner
= current
;
4881 get_task_struct(current
);
4882 mutex_lock(¤t
->perf_event_mutex
);
4883 list_add_tail(&event
->owner_entry
, ¤t
->perf_event_list
);
4884 mutex_unlock(¤t
->perf_event_mutex
);
4886 err_fput_free_put_context
:
4887 fput_light(event_file
, fput_needed2
);
4889 err_free_put_context
:
4897 fput_light(group_file
, fput_needed
);
4903 * perf_event_create_kernel_counter
4905 * @attr: attributes of the counter to create
4906 * @cpu: cpu in which the counter is bound
4907 * @pid: task to profile
4910 perf_event_create_kernel_counter(struct perf_event_attr
*attr
, int cpu
,
4912 perf_overflow_handler_t overflow_handler
)
4914 struct perf_event
*event
;
4915 struct perf_event_context
*ctx
;
4919 * Get the target context (task or percpu):
4922 ctx
= find_get_context(pid
, cpu
);
4928 event
= perf_event_alloc(attr
, cpu
, ctx
, NULL
,
4929 NULL
, overflow_handler
, GFP_KERNEL
);
4930 if (IS_ERR(event
)) {
4931 err
= PTR_ERR(event
);
4932 goto err_put_context
;
4936 WARN_ON_ONCE(ctx
->parent_ctx
);
4937 mutex_lock(&ctx
->mutex
);
4938 perf_install_in_context(ctx
, event
, cpu
);
4940 mutex_unlock(&ctx
->mutex
);
4942 event
->owner
= current
;
4943 get_task_struct(current
);
4944 mutex_lock(¤t
->perf_event_mutex
);
4945 list_add_tail(&event
->owner_entry
, ¤t
->perf_event_list
);
4946 mutex_unlock(¤t
->perf_event_mutex
);
4953 return ERR_PTR(err
);
4955 EXPORT_SYMBOL_GPL(perf_event_create_kernel_counter
);
4958 * inherit a event from parent task to child task:
4960 static struct perf_event
*
4961 inherit_event(struct perf_event
*parent_event
,
4962 struct task_struct
*parent
,
4963 struct perf_event_context
*parent_ctx
,
4964 struct task_struct
*child
,
4965 struct perf_event
*group_leader
,
4966 struct perf_event_context
*child_ctx
)
4968 struct perf_event
*child_event
;
4971 * Instead of creating recursive hierarchies of events,
4972 * we link inherited events back to the original parent,
4973 * which has a filp for sure, which we use as the reference
4976 if (parent_event
->parent
)
4977 parent_event
= parent_event
->parent
;
4979 child_event
= perf_event_alloc(&parent_event
->attr
,
4980 parent_event
->cpu
, child_ctx
,
4981 group_leader
, parent_event
,
4983 if (IS_ERR(child_event
))
4988 * Make the child state follow the state of the parent event,
4989 * not its attr.disabled bit. We hold the parent's mutex,
4990 * so we won't race with perf_event_{en, dis}able_family.
4992 if (parent_event
->state
>= PERF_EVENT_STATE_INACTIVE
)
4993 child_event
->state
= PERF_EVENT_STATE_INACTIVE
;
4995 child_event
->state
= PERF_EVENT_STATE_OFF
;
4997 if (parent_event
->attr
.freq
) {
4998 u64 sample_period
= parent_event
->hw
.sample_period
;
4999 struct hw_perf_event
*hwc
= &child_event
->hw
;
5001 hwc
->sample_period
= sample_period
;
5002 hwc
->last_period
= sample_period
;
5004 atomic64_set(&hwc
->period_left
, sample_period
);
5007 child_event
->overflow_handler
= parent_event
->overflow_handler
;
5010 * Link it up in the child's context:
5012 add_event_to_ctx(child_event
, child_ctx
);
5015 * Get a reference to the parent filp - we will fput it
5016 * when the child event exits. This is safe to do because
5017 * we are in the parent and we know that the filp still
5018 * exists and has a nonzero count:
5020 atomic_long_inc(&parent_event
->filp
->f_count
);
5023 * Link this into the parent event's child list
5025 WARN_ON_ONCE(parent_event
->ctx
->parent_ctx
);
5026 mutex_lock(&parent_event
->child_mutex
);
5027 list_add_tail(&child_event
->child_list
, &parent_event
->child_list
);
5028 mutex_unlock(&parent_event
->child_mutex
);
5033 static int inherit_group(struct perf_event
*parent_event
,
5034 struct task_struct
*parent
,
5035 struct perf_event_context
*parent_ctx
,
5036 struct task_struct
*child
,
5037 struct perf_event_context
*child_ctx
)
5039 struct perf_event
*leader
;
5040 struct perf_event
*sub
;
5041 struct perf_event
*child_ctr
;
5043 leader
= inherit_event(parent_event
, parent
, parent_ctx
,
5044 child
, NULL
, child_ctx
);
5046 return PTR_ERR(leader
);
5047 list_for_each_entry(sub
, &parent_event
->sibling_list
, group_entry
) {
5048 child_ctr
= inherit_event(sub
, parent
, parent_ctx
,
5049 child
, leader
, child_ctx
);
5050 if (IS_ERR(child_ctr
))
5051 return PTR_ERR(child_ctr
);
5056 static void sync_child_event(struct perf_event
*child_event
,
5057 struct task_struct
*child
)
5059 struct perf_event
*parent_event
= child_event
->parent
;
5062 if (child_event
->attr
.inherit_stat
)
5063 perf_event_read_event(child_event
, child
);
5065 child_val
= atomic64_read(&child_event
->count
);
5068 * Add back the child's count to the parent's count:
5070 atomic64_add(child_val
, &parent_event
->count
);
5071 atomic64_add(child_event
->total_time_enabled
,
5072 &parent_event
->child_total_time_enabled
);
5073 atomic64_add(child_event
->total_time_running
,
5074 &parent_event
->child_total_time_running
);
5077 * Remove this event from the parent's list
5079 WARN_ON_ONCE(parent_event
->ctx
->parent_ctx
);
5080 mutex_lock(&parent_event
->child_mutex
);
5081 list_del_init(&child_event
->child_list
);
5082 mutex_unlock(&parent_event
->child_mutex
);
5085 * Release the parent event, if this was the last
5088 fput(parent_event
->filp
);
5092 __perf_event_exit_task(struct perf_event
*child_event
,
5093 struct perf_event_context
*child_ctx
,
5094 struct task_struct
*child
)
5096 struct perf_event
*parent_event
;
5098 perf_event_remove_from_context(child_event
);
5100 parent_event
= child_event
->parent
;
5102 * It can happen that parent exits first, and has events
5103 * that are still around due to the child reference. These
5104 * events need to be zapped - but otherwise linger.
5107 sync_child_event(child_event
, child
);
5108 free_event(child_event
);
5113 * When a child task exits, feed back event values to parent events.
5115 void perf_event_exit_task(struct task_struct
*child
)
5117 struct perf_event
*child_event
, *tmp
;
5118 struct perf_event_context
*child_ctx
;
5119 unsigned long flags
;
5121 if (likely(!child
->perf_event_ctxp
)) {
5122 perf_event_task(child
, NULL
, 0);
5126 local_irq_save(flags
);
5128 * We can't reschedule here because interrupts are disabled,
5129 * and either child is current or it is a task that can't be
5130 * scheduled, so we are now safe from rescheduling changing
5133 child_ctx
= child
->perf_event_ctxp
;
5134 __perf_event_task_sched_out(child_ctx
);
5137 * Take the context lock here so that if find_get_context is
5138 * reading child->perf_event_ctxp, we wait until it has
5139 * incremented the context's refcount before we do put_ctx below.
5141 raw_spin_lock(&child_ctx
->lock
);
5142 child
->perf_event_ctxp
= NULL
;
5144 * If this context is a clone; unclone it so it can't get
5145 * swapped to another process while we're removing all
5146 * the events from it.
5148 unclone_ctx(child_ctx
);
5149 update_context_time(child_ctx
);
5150 raw_spin_unlock_irqrestore(&child_ctx
->lock
, flags
);
5153 * Report the task dead after unscheduling the events so that we
5154 * won't get any samples after PERF_RECORD_EXIT. We can however still
5155 * get a few PERF_RECORD_READ events.
5157 perf_event_task(child
, child_ctx
, 0);
5160 * We can recurse on the same lock type through:
5162 * __perf_event_exit_task()
5163 * sync_child_event()
5164 * fput(parent_event->filp)
5166 * mutex_lock(&ctx->mutex)
5168 * But since its the parent context it won't be the same instance.
5170 mutex_lock_nested(&child_ctx
->mutex
, SINGLE_DEPTH_NESTING
);
5173 list_for_each_entry_safe(child_event
, tmp
, &child_ctx
->pinned_groups
,
5175 __perf_event_exit_task(child_event
, child_ctx
, child
);
5177 list_for_each_entry_safe(child_event
, tmp
, &child_ctx
->flexible_groups
,
5179 __perf_event_exit_task(child_event
, child_ctx
, child
);
5182 * If the last event was a group event, it will have appended all
5183 * its siblings to the list, but we obtained 'tmp' before that which
5184 * will still point to the list head terminating the iteration.
5186 if (!list_empty(&child_ctx
->pinned_groups
) ||
5187 !list_empty(&child_ctx
->flexible_groups
))
5190 mutex_unlock(&child_ctx
->mutex
);
5195 static void perf_free_event(struct perf_event
*event
,
5196 struct perf_event_context
*ctx
)
5198 struct perf_event
*parent
= event
->parent
;
5200 if (WARN_ON_ONCE(!parent
))
5203 mutex_lock(&parent
->child_mutex
);
5204 list_del_init(&event
->child_list
);
5205 mutex_unlock(&parent
->child_mutex
);
5209 list_del_event(event
, ctx
);
5214 * free an unexposed, unused context as created by inheritance by
5215 * init_task below, used by fork() in case of fail.
5217 void perf_event_free_task(struct task_struct
*task
)
5219 struct perf_event_context
*ctx
= task
->perf_event_ctxp
;
5220 struct perf_event
*event
, *tmp
;
5225 mutex_lock(&ctx
->mutex
);
5227 list_for_each_entry_safe(event
, tmp
, &ctx
->pinned_groups
, group_entry
)
5228 perf_free_event(event
, ctx
);
5230 list_for_each_entry_safe(event
, tmp
, &ctx
->flexible_groups
,
5232 perf_free_event(event
, ctx
);
5234 if (!list_empty(&ctx
->pinned_groups
) ||
5235 !list_empty(&ctx
->flexible_groups
))
5238 mutex_unlock(&ctx
->mutex
);
5244 inherit_task_group(struct perf_event
*event
, struct task_struct
*parent
,
5245 struct perf_event_context
*parent_ctx
,
5246 struct task_struct
*child
,
5250 struct perf_event_context
*child_ctx
= child
->perf_event_ctxp
;
5252 if (!event
->attr
.inherit
) {
5259 * This is executed from the parent task context, so
5260 * inherit events that have been marked for cloning.
5261 * First allocate and initialize a context for the
5265 child_ctx
= kzalloc(sizeof(struct perf_event_context
),
5270 __perf_event_init_context(child_ctx
, child
);
5271 child
->perf_event_ctxp
= child_ctx
;
5272 get_task_struct(child
);
5275 ret
= inherit_group(event
, parent
, parent_ctx
,
5286 * Initialize the perf_event context in task_struct
5288 int perf_event_init_task(struct task_struct
*child
)
5290 struct perf_event_context
*child_ctx
, *parent_ctx
;
5291 struct perf_event_context
*cloned_ctx
;
5292 struct perf_event
*event
;
5293 struct task_struct
*parent
= current
;
5294 int inherited_all
= 1;
5297 child
->perf_event_ctxp
= NULL
;
5299 mutex_init(&child
->perf_event_mutex
);
5300 INIT_LIST_HEAD(&child
->perf_event_list
);
5302 if (likely(!parent
->perf_event_ctxp
))
5306 * If the parent's context is a clone, pin it so it won't get
5309 parent_ctx
= perf_pin_task_context(parent
);
5312 * No need to check if parent_ctx != NULL here; since we saw
5313 * it non-NULL earlier, the only reason for it to become NULL
5314 * is if we exit, and since we're currently in the middle of
5315 * a fork we can't be exiting at the same time.
5319 * Lock the parent list. No need to lock the child - not PID
5320 * hashed yet and not running, so nobody can access it.
5322 mutex_lock(&parent_ctx
->mutex
);
5325 * We dont have to disable NMIs - we are only looking at
5326 * the list, not manipulating it:
5328 list_for_each_entry(event
, &parent_ctx
->pinned_groups
, group_entry
) {
5329 ret
= inherit_task_group(event
, parent
, parent_ctx
, child
,
5335 list_for_each_entry(event
, &parent_ctx
->flexible_groups
, group_entry
) {
5336 ret
= inherit_task_group(event
, parent
, parent_ctx
, child
,
5342 child_ctx
= child
->perf_event_ctxp
;
5344 if (child_ctx
&& inherited_all
) {
5346 * Mark the child context as a clone of the parent
5347 * context, or of whatever the parent is a clone of.
5348 * Note that if the parent is a clone, it could get
5349 * uncloned at any point, but that doesn't matter
5350 * because the list of events and the generation
5351 * count can't have changed since we took the mutex.
5353 cloned_ctx
= rcu_dereference(parent_ctx
->parent_ctx
);
5355 child_ctx
->parent_ctx
= cloned_ctx
;
5356 child_ctx
->parent_gen
= parent_ctx
->parent_gen
;
5358 child_ctx
->parent_ctx
= parent_ctx
;
5359 child_ctx
->parent_gen
= parent_ctx
->generation
;
5361 get_ctx(child_ctx
->parent_ctx
);
5364 mutex_unlock(&parent_ctx
->mutex
);
5366 perf_unpin_context(parent_ctx
);
5371 static void __cpuinit
perf_event_init_cpu(int cpu
)
5373 struct perf_cpu_context
*cpuctx
;
5375 cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
5376 __perf_event_init_context(&cpuctx
->ctx
, NULL
);
5378 spin_lock(&perf_resource_lock
);
5379 cpuctx
->max_pertask
= perf_max_events
- perf_reserved_percpu
;
5380 spin_unlock(&perf_resource_lock
);
5383 #ifdef CONFIG_HOTPLUG_CPU
5384 static void __perf_event_exit_cpu(void *info
)
5386 struct perf_cpu_context
*cpuctx
= &__get_cpu_var(perf_cpu_context
);
5387 struct perf_event_context
*ctx
= &cpuctx
->ctx
;
5388 struct perf_event
*event
, *tmp
;
5390 list_for_each_entry_safe(event
, tmp
, &ctx
->pinned_groups
, group_entry
)
5391 __perf_event_remove_from_context(event
);
5392 list_for_each_entry_safe(event
, tmp
, &ctx
->flexible_groups
, group_entry
)
5393 __perf_event_remove_from_context(event
);
5395 static void perf_event_exit_cpu(int cpu
)
5397 struct perf_cpu_context
*cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
5398 struct perf_event_context
*ctx
= &cpuctx
->ctx
;
5400 mutex_lock(&ctx
->mutex
);
5401 smp_call_function_single(cpu
, __perf_event_exit_cpu
, NULL
, 1);
5402 mutex_unlock(&ctx
->mutex
);
5405 static inline void perf_event_exit_cpu(int cpu
) { }
5408 static int __cpuinit
5409 perf_cpu_notify(struct notifier_block
*self
, unsigned long action
, void *hcpu
)
5411 unsigned int cpu
= (long)hcpu
;
5415 case CPU_UP_PREPARE
:
5416 case CPU_UP_PREPARE_FROZEN
:
5417 perf_event_init_cpu(cpu
);
5420 case CPU_DOWN_PREPARE
:
5421 case CPU_DOWN_PREPARE_FROZEN
:
5422 perf_event_exit_cpu(cpu
);
5433 * This has to have a higher priority than migration_notifier in sched.c.
5435 static struct notifier_block __cpuinitdata perf_cpu_nb
= {
5436 .notifier_call
= perf_cpu_notify
,
5440 void __init
perf_event_init(void)
5442 perf_cpu_notify(&perf_cpu_nb
, (unsigned long)CPU_UP_PREPARE
,
5443 (void *)(long)smp_processor_id());
5444 perf_cpu_notify(&perf_cpu_nb
, (unsigned long)CPU_ONLINE
,
5445 (void *)(long)smp_processor_id());
5446 register_cpu_notifier(&perf_cpu_nb
);
5449 static ssize_t
perf_show_reserve_percpu(struct sysdev_class
*class,
5450 struct sysdev_class_attribute
*attr
,
5453 return sprintf(buf
, "%d\n", perf_reserved_percpu
);
5457 perf_set_reserve_percpu(struct sysdev_class
*class,
5458 struct sysdev_class_attribute
*attr
,
5462 struct perf_cpu_context
*cpuctx
;
5466 err
= strict_strtoul(buf
, 10, &val
);
5469 if (val
> perf_max_events
)
5472 spin_lock(&perf_resource_lock
);
5473 perf_reserved_percpu
= val
;
5474 for_each_online_cpu(cpu
) {
5475 cpuctx
= &per_cpu(perf_cpu_context
, cpu
);
5476 raw_spin_lock_irq(&cpuctx
->ctx
.lock
);
5477 mpt
= min(perf_max_events
- cpuctx
->ctx
.nr_events
,
5478 perf_max_events
- perf_reserved_percpu
);
5479 cpuctx
->max_pertask
= mpt
;
5480 raw_spin_unlock_irq(&cpuctx
->ctx
.lock
);
5482 spin_unlock(&perf_resource_lock
);
5487 static ssize_t
perf_show_overcommit(struct sysdev_class
*class,
5488 struct sysdev_class_attribute
*attr
,
5491 return sprintf(buf
, "%d\n", perf_overcommit
);
5495 perf_set_overcommit(struct sysdev_class
*class,
5496 struct sysdev_class_attribute
*attr
,
5497 const char *buf
, size_t count
)
5502 err
= strict_strtoul(buf
, 10, &val
);
5508 spin_lock(&perf_resource_lock
);
5509 perf_overcommit
= val
;
5510 spin_unlock(&perf_resource_lock
);
5515 static SYSDEV_CLASS_ATTR(
5518 perf_show_reserve_percpu
,
5519 perf_set_reserve_percpu
5522 static SYSDEV_CLASS_ATTR(
5525 perf_show_overcommit
,
5529 static struct attribute
*perfclass_attrs
[] = {
5530 &attr_reserve_percpu
.attr
,
5531 &attr_overcommit
.attr
,
5535 static struct attribute_group perfclass_attr_group
= {
5536 .attrs
= perfclass_attrs
,
5537 .name
= "perf_events",
5540 static int __init
perf_event_sysfs_init(void)
5542 return sysfs_create_group(&cpu_sysdev_class
.kset
.kobj
,
5543 &perfclass_attr_group
);
5545 device_initcall(perf_event_sysfs_init
);